1 /*
2  * Copyright (C) 2008-2010
3  *
4  * - Kurt Van Dijck, EIA Electronics
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the version 2 of the GNU General Public License
8  * as published by the Free Software Foundation
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */
19 
20 #include <linux/firmware.h>
21 #include <linux/sched.h>
22 #include <asm/div64.h>
23 #include <asm/io.h>
24 
25 #include "softing.h"
26 
27 /*
28  * low level DPRAM command.
29  * Make sure that card->dpram[DPRAM_FCT_HOST] is preset
30  */
31 static int _softing_fct_cmd(struct softing *card, int16_t cmd, uint16_t vector,
32 		const char *msg)
33 {
34 	int ret;
35 	unsigned long stamp;
36 
37 	iowrite16(cmd, &card->dpram[DPRAM_FCT_PARAM]);
38 	iowrite8(vector >> 8, &card->dpram[DPRAM_FCT_HOST + 1]);
39 	iowrite8(vector, &card->dpram[DPRAM_FCT_HOST]);
40 	/* be sure to flush this to the card */
41 	wmb();
42 	stamp = jiffies + 1 * HZ;
43 	/* wait for card */
44 	do {
45 		/* DPRAM_FCT_HOST is _not_ aligned */
46 		ret = ioread8(&card->dpram[DPRAM_FCT_HOST]) +
47 			(ioread8(&card->dpram[DPRAM_FCT_HOST + 1]) << 8);
48 		/* don't have any cached variables */
49 		rmb();
50 		if (ret == RES_OK)
51 			/* read return-value now */
52 			return ioread16(&card->dpram[DPRAM_FCT_RESULT]);
53 
54 		if ((ret != vector) || time_after(jiffies, stamp))
55 			break;
56 		/* process context => relax */
57 		usleep_range(500, 10000);
58 	} while (1);
59 
60 	ret = (ret == RES_NONE) ? -ETIMEDOUT : -ECANCELED;
61 	dev_alert(&card->pdev->dev, "firmware %s failed (%i)\n", msg, ret);
62 	return ret;
63 }
64 
65 static int softing_fct_cmd(struct softing *card, int16_t cmd, const char *msg)
66 {
67 	int ret;
68 
69 	ret = _softing_fct_cmd(card, cmd, 0, msg);
70 	if (ret > 0) {
71 		dev_alert(&card->pdev->dev, "%s returned %u\n", msg, ret);
72 		ret = -EIO;
73 	}
74 	return ret;
75 }
76 
77 int softing_bootloader_command(struct softing *card, int16_t cmd,
78 		const char *msg)
79 {
80 	int ret;
81 	unsigned long stamp;
82 
83 	iowrite16(RES_NONE, &card->dpram[DPRAM_RECEIPT]);
84 	iowrite16(cmd, &card->dpram[DPRAM_COMMAND]);
85 	/* be sure to flush this to the card */
86 	wmb();
87 	stamp = jiffies + 3 * HZ;
88 	/* wait for card */
89 	do {
90 		ret = ioread16(&card->dpram[DPRAM_RECEIPT]);
91 		/* don't have any cached variables */
92 		rmb();
93 		if (ret == RES_OK)
94 			return 0;
95 		if (time_after(jiffies, stamp))
96 			break;
97 		/* process context => relax */
98 		usleep_range(500, 10000);
99 	} while (!signal_pending(current));
100 
101 	ret = (ret == RES_NONE) ? -ETIMEDOUT : -ECANCELED;
102 	dev_alert(&card->pdev->dev, "bootloader %s failed (%i)\n", msg, ret);
103 	return ret;
104 }
105 
106 static int fw_parse(const uint8_t **pmem, uint16_t *ptype, uint32_t *paddr,
107 		uint16_t *plen, const uint8_t **pdat)
108 {
109 	uint16_t checksum[2];
110 	const uint8_t *mem;
111 	const uint8_t *end;
112 
113 	/*
114 	 * firmware records are a binary, unaligned stream composed of:
115 	 * uint16_t type;
116 	 * uint32_t addr;
117 	 * uint16_t len;
118 	 * uint8_t dat[len];
119 	 * uint16_t checksum;
120 	 * all values in little endian.
121 	 * We could define a struct for this, with __attribute__((packed)),
122 	 * but would that solve the alignment in _all_ cases (cfr. the
123 	 * struct itself may be an odd address)?
124 	 *
125 	 * I chose to use leXX_to_cpup() since this solves both
126 	 * endianness & alignment.
127 	 */
128 	mem = *pmem;
129 	*ptype = le16_to_cpup((void *)&mem[0]);
130 	*paddr = le32_to_cpup((void *)&mem[2]);
131 	*plen = le16_to_cpup((void *)&mem[6]);
132 	*pdat = &mem[8];
133 	/* verify checksum */
134 	end = &mem[8 + *plen];
135 	checksum[0] = le16_to_cpup((void *)end);
136 	for (checksum[1] = 0; mem < end; ++mem)
137 		checksum[1] += *mem;
138 	if (checksum[0] != checksum[1])
139 		return -EINVAL;
140 	/* increment */
141 	*pmem += 10 + *plen;
142 	return 0;
143 }
144 
145 int softing_load_fw(const char *file, struct softing *card,
146 		__iomem uint8_t *dpram, unsigned int size, int offset)
147 {
148 	const struct firmware *fw;
149 	int ret;
150 	const uint8_t *mem, *end, *dat;
151 	uint16_t type, len;
152 	uint32_t addr;
153 	uint8_t *buf = NULL, *new_buf;
154 	int buflen = 0;
155 	int8_t type_end = 0;
156 
157 	ret = request_firmware(&fw, file, &card->pdev->dev);
158 	if (ret < 0)
159 		return ret;
160 	dev_dbg(&card->pdev->dev, "%s, firmware(%s) got %u bytes"
161 		", offset %c0x%04x\n",
162 		card->pdat->name, file, (unsigned int)fw->size,
163 		(offset >= 0) ? '+' : '-', (unsigned int)abs(offset));
164 	/* parse the firmware */
165 	mem = fw->data;
166 	end = &mem[fw->size];
167 	/* look for header record */
168 	ret = fw_parse(&mem, &type, &addr, &len, &dat);
169 	if (ret < 0)
170 		goto failed;
171 	if (type != 0xffff)
172 		goto failed;
173 	if (strncmp("Structured Binary Format, Softing GmbH" , dat, len)) {
174 		ret = -EINVAL;
175 		goto failed;
176 	}
177 	/* ok, we had a header */
178 	while (mem < end) {
179 		ret = fw_parse(&mem, &type, &addr, &len, &dat);
180 		if (ret < 0)
181 			goto failed;
182 		if (type == 3) {
183 			/* start address, not used here */
184 			continue;
185 		} else if (type == 1) {
186 			/* eof */
187 			type_end = 1;
188 			break;
189 		} else if (type != 0) {
190 			ret = -EINVAL;
191 			goto failed;
192 		}
193 
194 		if ((addr + len + offset) > size)
195 			goto failed;
196 		memcpy_toio(&dpram[addr + offset], dat, len);
197 		/* be sure to flush caches from IO space */
198 		mb();
199 		if (len > buflen) {
200 			/* align buflen */
201 			buflen = (len + (1024-1)) & ~(1024-1);
202 			new_buf = krealloc(buf, buflen, GFP_KERNEL);
203 			if (!new_buf) {
204 				ret = -ENOMEM;
205 				goto failed;
206 			}
207 			buf = new_buf;
208 		}
209 		/* verify record data */
210 		memcpy_fromio(buf, &dpram[addr + offset], len);
211 		if (memcmp(buf, dat, len)) {
212 			/* is not ok */
213 			dev_alert(&card->pdev->dev, "DPRAM readback failed\n");
214 			ret = -EIO;
215 			goto failed;
216 		}
217 	}
218 	if (!type_end)
219 		/* no end record seen */
220 		goto failed;
221 	ret = 0;
222 failed:
223 	kfree(buf);
224 	release_firmware(fw);
225 	if (ret < 0)
226 		dev_info(&card->pdev->dev, "firmware %s failed\n", file);
227 	return ret;
228 }
229 
230 int softing_load_app_fw(const char *file, struct softing *card)
231 {
232 	const struct firmware *fw;
233 	const uint8_t *mem, *end, *dat;
234 	int ret, j;
235 	uint16_t type, len;
236 	uint32_t addr, start_addr = 0;
237 	unsigned int sum, rx_sum;
238 	int8_t type_end = 0, type_entrypoint = 0;
239 
240 	ret = request_firmware(&fw, file, &card->pdev->dev);
241 	if (ret) {
242 		dev_alert(&card->pdev->dev, "request_firmware(%s) got %i\n",
243 			file, ret);
244 		return ret;
245 	}
246 	dev_dbg(&card->pdev->dev, "firmware(%s) got %lu bytes\n",
247 		file, (unsigned long)fw->size);
248 	/* parse the firmware */
249 	mem = fw->data;
250 	end = &mem[fw->size];
251 	/* look for header record */
252 	ret = fw_parse(&mem, &type, &addr, &len, &dat);
253 	if (ret)
254 		goto failed;
255 	ret = -EINVAL;
256 	if (type != 0xffff) {
257 		dev_alert(&card->pdev->dev, "firmware starts with type 0x%x\n",
258 			type);
259 		goto failed;
260 	}
261 	if (strncmp("Structured Binary Format, Softing GmbH", dat, len)) {
262 		dev_alert(&card->pdev->dev, "firmware string '%.*s' fault\n",
263 				len, dat);
264 		goto failed;
265 	}
266 	/* ok, we had a header */
267 	while (mem < end) {
268 		ret = fw_parse(&mem, &type, &addr, &len, &dat);
269 		if (ret)
270 			goto failed;
271 
272 		if (type == 3) {
273 			/* start address */
274 			start_addr = addr;
275 			type_entrypoint = 1;
276 			continue;
277 		} else if (type == 1) {
278 			/* eof */
279 			type_end = 1;
280 			break;
281 		} else if (type != 0) {
282 			dev_alert(&card->pdev->dev,
283 					"unknown record type 0x%04x\n", type);
284 			ret = -EINVAL;
285 			goto failed;
286 		}
287 
288 		/* regualar data */
289 		for (sum = 0, j = 0; j < len; ++j)
290 			sum += dat[j];
291 		/* work in 16bit (target) */
292 		sum &= 0xffff;
293 
294 		memcpy_toio(&card->dpram[card->pdat->app.offs], dat, len);
295 		iowrite32(card->pdat->app.offs + card->pdat->app.addr,
296 				&card->dpram[DPRAM_COMMAND + 2]);
297 		iowrite32(addr, &card->dpram[DPRAM_COMMAND + 6]);
298 		iowrite16(len, &card->dpram[DPRAM_COMMAND + 10]);
299 		iowrite8(1, &card->dpram[DPRAM_COMMAND + 12]);
300 		ret = softing_bootloader_command(card, 1, "loading app.");
301 		if (ret < 0)
302 			goto failed;
303 		/* verify checksum */
304 		rx_sum = ioread16(&card->dpram[DPRAM_RECEIPT + 2]);
305 		if (rx_sum != sum) {
306 			dev_alert(&card->pdev->dev, "SRAM seems to be damaged"
307 				", wanted 0x%04x, got 0x%04x\n", sum, rx_sum);
308 			ret = -EIO;
309 			goto failed;
310 		}
311 	}
312 	if (!type_end || !type_entrypoint)
313 		goto failed;
314 	/* start application in card */
315 	iowrite32(start_addr, &card->dpram[DPRAM_COMMAND + 2]);
316 	iowrite8(1, &card->dpram[DPRAM_COMMAND + 6]);
317 	ret = softing_bootloader_command(card, 3, "start app.");
318 	if (ret < 0)
319 		goto failed;
320 	ret = 0;
321 failed:
322 	release_firmware(fw);
323 	if (ret < 0)
324 		dev_info(&card->pdev->dev, "firmware %s failed\n", file);
325 	return ret;
326 }
327 
328 static int softing_reset_chip(struct softing *card)
329 {
330 	int ret;
331 
332 	do {
333 		/* reset chip */
334 		iowrite8(0, &card->dpram[DPRAM_RESET_RX_FIFO]);
335 		iowrite8(0, &card->dpram[DPRAM_RESET_RX_FIFO+1]);
336 		iowrite8(1, &card->dpram[DPRAM_RESET]);
337 		iowrite8(0, &card->dpram[DPRAM_RESET+1]);
338 
339 		ret = softing_fct_cmd(card, 0, "reset_can");
340 		if (!ret)
341 			break;
342 		if (signal_pending(current))
343 			/* don't wait any longer */
344 			break;
345 	} while (1);
346 	card->tx.pending = 0;
347 	return ret;
348 }
349 
350 int softing_chip_poweron(struct softing *card)
351 {
352 	int ret;
353 	/* sync */
354 	ret = _softing_fct_cmd(card, 99, 0x55, "sync-a");
355 	if (ret < 0)
356 		goto failed;
357 
358 	ret = _softing_fct_cmd(card, 99, 0xaa, "sync-b");
359 	if (ret < 0)
360 		goto failed;
361 
362 	ret = softing_reset_chip(card);
363 	if (ret < 0)
364 		goto failed;
365 	/* get_serial */
366 	ret = softing_fct_cmd(card, 43, "get_serial_number");
367 	if (ret < 0)
368 		goto failed;
369 	card->id.serial = ioread32(&card->dpram[DPRAM_FCT_PARAM]);
370 	/* get_version */
371 	ret = softing_fct_cmd(card, 12, "get_version");
372 	if (ret < 0)
373 		goto failed;
374 	card->id.fw_version = ioread16(&card->dpram[DPRAM_FCT_PARAM + 2]);
375 	card->id.hw_version = ioread16(&card->dpram[DPRAM_FCT_PARAM + 4]);
376 	card->id.license = ioread16(&card->dpram[DPRAM_FCT_PARAM + 6]);
377 	card->id.chip[0] = ioread16(&card->dpram[DPRAM_FCT_PARAM + 8]);
378 	card->id.chip[1] = ioread16(&card->dpram[DPRAM_FCT_PARAM + 10]);
379 	return 0;
380 failed:
381 	return ret;
382 }
383 
384 static void softing_initialize_timestamp(struct softing *card)
385 {
386 	uint64_t ovf;
387 
388 	card->ts_ref = ktime_get();
389 
390 	/* 16MHz is the reference */
391 	ovf = 0x100000000ULL * 16;
392 	do_div(ovf, card->pdat->freq ?: 16);
393 
394 	card->ts_overflow = ktime_add_us(ktime_set(0, 0), ovf);
395 }
396 
397 ktime_t softing_raw2ktime(struct softing *card, u32 raw)
398 {
399 	uint64_t rawl;
400 	ktime_t now, real_offset;
401 	ktime_t target;
402 	ktime_t tmp;
403 
404 	now = ktime_get();
405 	real_offset = ktime_sub(ktime_get_real(), now);
406 
407 	/* find nsec from card */
408 	rawl = raw * 16;
409 	do_div(rawl, card->pdat->freq ?: 16);
410 	target = ktime_add_us(card->ts_ref, rawl);
411 	/* test for overflows */
412 	tmp = ktime_add(target, card->ts_overflow);
413 	while (unlikely(ktime_to_ns(tmp) > ktime_to_ns(now))) {
414 		card->ts_ref = ktime_add(card->ts_ref, card->ts_overflow);
415 		target = tmp;
416 		tmp = ktime_add(target, card->ts_overflow);
417 	}
418 	return ktime_add(target, real_offset);
419 }
420 
421 static inline int softing_error_reporting(struct net_device *netdev)
422 {
423 	struct softing_priv *priv = netdev_priv(netdev);
424 
425 	return (priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING)
426 		? 1 : 0;
427 }
428 
429 int softing_startstop(struct net_device *dev, int up)
430 {
431 	int ret;
432 	struct softing *card;
433 	struct softing_priv *priv;
434 	struct net_device *netdev;
435 	int bus_bitmask_start;
436 	int j, error_reporting;
437 	struct can_frame msg;
438 	const struct can_bittiming *bt;
439 
440 	priv = netdev_priv(dev);
441 	card = priv->card;
442 
443 	if (!card->fw.up)
444 		return -EIO;
445 
446 	ret = mutex_lock_interruptible(&card->fw.lock);
447 	if (ret)
448 		return ret;
449 
450 	bus_bitmask_start = 0;
451 	if (dev && up)
452 		/* prepare to start this bus as well */
453 		bus_bitmask_start |= (1 << priv->index);
454 	/* bring netdevs down */
455 	for (j = 0; j < ARRAY_SIZE(card->net); ++j) {
456 		netdev = card->net[j];
457 		if (!netdev)
458 			continue;
459 		priv = netdev_priv(netdev);
460 
461 		if (dev != netdev)
462 			netif_stop_queue(netdev);
463 
464 		if (netif_running(netdev)) {
465 			if (dev != netdev)
466 				bus_bitmask_start |= (1 << j);
467 			priv->tx.pending = 0;
468 			priv->tx.echo_put = 0;
469 			priv->tx.echo_get = 0;
470 			/*
471 			 * this bus' may just have called open_candev()
472 			 * which is rather stupid to call close_candev()
473 			 * already
474 			 * but we may come here from busoff recovery too
475 			 * in which case the echo_skb _needs_ flushing too.
476 			 * just be sure to call open_candev() again
477 			 */
478 			close_candev(netdev);
479 		}
480 		priv->can.state = CAN_STATE_STOPPED;
481 	}
482 	card->tx.pending = 0;
483 
484 	softing_enable_irq(card, 0);
485 	ret = softing_reset_chip(card);
486 	if (ret)
487 		goto failed;
488 	if (!bus_bitmask_start)
489 		/* no busses to be brought up */
490 		goto card_done;
491 
492 	if ((bus_bitmask_start & 1) && (bus_bitmask_start & 2)
493 			&& (softing_error_reporting(card->net[0])
494 				!= softing_error_reporting(card->net[1]))) {
495 		dev_alert(&card->pdev->dev,
496 				"err_reporting flag differs for busses\n");
497 		goto invalid;
498 	}
499 	error_reporting = 0;
500 	if (bus_bitmask_start & 1) {
501 		netdev = card->net[0];
502 		priv = netdev_priv(netdev);
503 		error_reporting += softing_error_reporting(netdev);
504 		/* init chip 1 */
505 		bt = &priv->can.bittiming;
506 		iowrite16(bt->brp, &card->dpram[DPRAM_FCT_PARAM + 2]);
507 		iowrite16(bt->sjw, &card->dpram[DPRAM_FCT_PARAM + 4]);
508 		iowrite16(bt->phase_seg1 + bt->prop_seg,
509 				&card->dpram[DPRAM_FCT_PARAM + 6]);
510 		iowrite16(bt->phase_seg2, &card->dpram[DPRAM_FCT_PARAM + 8]);
511 		iowrite16((priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES) ? 1 : 0,
512 				&card->dpram[DPRAM_FCT_PARAM + 10]);
513 		ret = softing_fct_cmd(card, 1, "initialize_chip[0]");
514 		if (ret < 0)
515 			goto failed;
516 		/* set mode */
517 		iowrite16(0, &card->dpram[DPRAM_FCT_PARAM + 2]);
518 		iowrite16(0, &card->dpram[DPRAM_FCT_PARAM + 4]);
519 		ret = softing_fct_cmd(card, 3, "set_mode[0]");
520 		if (ret < 0)
521 			goto failed;
522 		/* set filter */
523 		/* 11bit id & mask */
524 		iowrite16(0x0000, &card->dpram[DPRAM_FCT_PARAM + 2]);
525 		iowrite16(0x07ff, &card->dpram[DPRAM_FCT_PARAM + 4]);
526 		/* 29bit id.lo & mask.lo & id.hi & mask.hi */
527 		iowrite16(0x0000, &card->dpram[DPRAM_FCT_PARAM + 6]);
528 		iowrite16(0xffff, &card->dpram[DPRAM_FCT_PARAM + 8]);
529 		iowrite16(0x0000, &card->dpram[DPRAM_FCT_PARAM + 10]);
530 		iowrite16(0x1fff, &card->dpram[DPRAM_FCT_PARAM + 12]);
531 		ret = softing_fct_cmd(card, 7, "set_filter[0]");
532 		if (ret < 0)
533 			goto failed;
534 		/* set output control */
535 		iowrite16(priv->output, &card->dpram[DPRAM_FCT_PARAM + 2]);
536 		ret = softing_fct_cmd(card, 5, "set_output[0]");
537 		if (ret < 0)
538 			goto failed;
539 	}
540 	if (bus_bitmask_start & 2) {
541 		netdev = card->net[1];
542 		priv = netdev_priv(netdev);
543 		error_reporting += softing_error_reporting(netdev);
544 		/* init chip2 */
545 		bt = &priv->can.bittiming;
546 		iowrite16(bt->brp, &card->dpram[DPRAM_FCT_PARAM + 2]);
547 		iowrite16(bt->sjw, &card->dpram[DPRAM_FCT_PARAM + 4]);
548 		iowrite16(bt->phase_seg1 + bt->prop_seg,
549 				&card->dpram[DPRAM_FCT_PARAM + 6]);
550 		iowrite16(bt->phase_seg2, &card->dpram[DPRAM_FCT_PARAM + 8]);
551 		iowrite16((priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES) ? 1 : 0,
552 				&card->dpram[DPRAM_FCT_PARAM + 10]);
553 		ret = softing_fct_cmd(card, 2, "initialize_chip[1]");
554 		if (ret < 0)
555 			goto failed;
556 		/* set mode2 */
557 		iowrite16(0, &card->dpram[DPRAM_FCT_PARAM + 2]);
558 		iowrite16(0, &card->dpram[DPRAM_FCT_PARAM + 4]);
559 		ret = softing_fct_cmd(card, 4, "set_mode[1]");
560 		if (ret < 0)
561 			goto failed;
562 		/* set filter2 */
563 		/* 11bit id & mask */
564 		iowrite16(0x0000, &card->dpram[DPRAM_FCT_PARAM + 2]);
565 		iowrite16(0x07ff, &card->dpram[DPRAM_FCT_PARAM + 4]);
566 		/* 29bit id.lo & mask.lo & id.hi & mask.hi */
567 		iowrite16(0x0000, &card->dpram[DPRAM_FCT_PARAM + 6]);
568 		iowrite16(0xffff, &card->dpram[DPRAM_FCT_PARAM + 8]);
569 		iowrite16(0x0000, &card->dpram[DPRAM_FCT_PARAM + 10]);
570 		iowrite16(0x1fff, &card->dpram[DPRAM_FCT_PARAM + 12]);
571 		ret = softing_fct_cmd(card, 8, "set_filter[1]");
572 		if (ret < 0)
573 			goto failed;
574 		/* set output control2 */
575 		iowrite16(priv->output, &card->dpram[DPRAM_FCT_PARAM + 2]);
576 		ret = softing_fct_cmd(card, 6, "set_output[1]");
577 		if (ret < 0)
578 			goto failed;
579 	}
580 	/* enable_error_frame */
581 	/*
582 	 * Error reporting is switched off at the moment since
583 	 * the receiving of them is not yet 100% verified
584 	 * This should be enabled sooner or later
585 	 *
586 	if (error_reporting) {
587 		ret = softing_fct_cmd(card, 51, "enable_error_frame");
588 		if (ret < 0)
589 			goto failed;
590 	}
591 	*/
592 	/* initialize interface */
593 	iowrite16(1, &card->dpram[DPRAM_FCT_PARAM + 2]);
594 	iowrite16(1, &card->dpram[DPRAM_FCT_PARAM + 4]);
595 	iowrite16(1, &card->dpram[DPRAM_FCT_PARAM + 6]);
596 	iowrite16(1, &card->dpram[DPRAM_FCT_PARAM + 8]);
597 	iowrite16(1, &card->dpram[DPRAM_FCT_PARAM + 10]);
598 	iowrite16(1, &card->dpram[DPRAM_FCT_PARAM + 12]);
599 	iowrite16(1, &card->dpram[DPRAM_FCT_PARAM + 14]);
600 	iowrite16(1, &card->dpram[DPRAM_FCT_PARAM + 16]);
601 	iowrite16(1, &card->dpram[DPRAM_FCT_PARAM + 18]);
602 	iowrite16(1, &card->dpram[DPRAM_FCT_PARAM + 20]);
603 	ret = softing_fct_cmd(card, 17, "initialize_interface");
604 	if (ret < 0)
605 		goto failed;
606 	/* enable_fifo */
607 	ret = softing_fct_cmd(card, 36, "enable_fifo");
608 	if (ret < 0)
609 		goto failed;
610 	/* enable fifo tx ack */
611 	ret = softing_fct_cmd(card, 13, "fifo_tx_ack[0]");
612 	if (ret < 0)
613 		goto failed;
614 	/* enable fifo tx ack2 */
615 	ret = softing_fct_cmd(card, 14, "fifo_tx_ack[1]");
616 	if (ret < 0)
617 		goto failed;
618 	/* start_chip */
619 	ret = softing_fct_cmd(card, 11, "start_chip");
620 	if (ret < 0)
621 		goto failed;
622 	iowrite8(0, &card->dpram[DPRAM_INFO_BUSSTATE]);
623 	iowrite8(0, &card->dpram[DPRAM_INFO_BUSSTATE2]);
624 	if (card->pdat->generation < 2) {
625 		iowrite8(0, &card->dpram[DPRAM_V2_IRQ_TOHOST]);
626 		/* flush the DPRAM caches */
627 		wmb();
628 	}
629 
630 	softing_initialize_timestamp(card);
631 
632 	/*
633 	 * do socketcan notifications/status changes
634 	 * from here, no errors should occur, or the failed: part
635 	 * must be reviewed
636 	 */
637 	memset(&msg, 0, sizeof(msg));
638 	msg.can_id = CAN_ERR_FLAG | CAN_ERR_RESTARTED;
639 	msg.can_dlc = CAN_ERR_DLC;
640 	for (j = 0; j < ARRAY_SIZE(card->net); ++j) {
641 		if (!(bus_bitmask_start & (1 << j)))
642 			continue;
643 		netdev = card->net[j];
644 		if (!netdev)
645 			continue;
646 		priv = netdev_priv(netdev);
647 		priv->can.state = CAN_STATE_ERROR_ACTIVE;
648 		open_candev(netdev);
649 		if (dev != netdev) {
650 			/* notify other busses on the restart */
651 			softing_netdev_rx(netdev, &msg, ktime_set(0, 0));
652 			++priv->can.can_stats.restarts;
653 		}
654 		netif_wake_queue(netdev);
655 	}
656 
657 	/* enable interrupts */
658 	ret = softing_enable_irq(card, 1);
659 	if (ret)
660 		goto failed;
661 card_done:
662 	mutex_unlock(&card->fw.lock);
663 	return 0;
664 invalid:
665 	ret = -EINVAL;
666 failed:
667 	softing_enable_irq(card, 0);
668 	softing_reset_chip(card);
669 	mutex_unlock(&card->fw.lock);
670 	/* bring all other interfaces down */
671 	for (j = 0; j < ARRAY_SIZE(card->net); ++j) {
672 		netdev = card->net[j];
673 		if (!netdev)
674 			continue;
675 		dev_close(netdev);
676 	}
677 	return ret;
678 }
679 
680 int softing_default_output(struct net_device *netdev)
681 {
682 	struct softing_priv *priv = netdev_priv(netdev);
683 	struct softing *card = priv->card;
684 
685 	switch (priv->chip) {
686 	case 1000:
687 		return (card->pdat->generation < 2) ? 0xfb : 0xfa;
688 	case 5:
689 		return 0x60;
690 	default:
691 		return 0x40;
692 	}
693 }
694