1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2008-2010
5 * - Kurt Van Dijck, EIA Electronics
8 #include <linux/firmware.h>
9 #include <linux/sched/signal.h>
10 #include <asm/div64.h>
16 * low level DPRAM command.
17 * Make sure that card->dpram[DPRAM_FCT_HOST] is preset
19 static int _softing_fct_cmd(struct softing
*card
, int16_t cmd
, uint16_t vector
,
25 iowrite16(cmd
, &card
->dpram
[DPRAM_FCT_PARAM
]);
26 iowrite8(vector
>> 8, &card
->dpram
[DPRAM_FCT_HOST
+ 1]);
27 iowrite8(vector
, &card
->dpram
[DPRAM_FCT_HOST
]);
28 /* be sure to flush this to the card */
30 stamp
= jiffies
+ 1 * HZ
;
33 /* DPRAM_FCT_HOST is _not_ aligned */
34 ret
= ioread8(&card
->dpram
[DPRAM_FCT_HOST
]) +
35 (ioread8(&card
->dpram
[DPRAM_FCT_HOST
+ 1]) << 8);
36 /* don't have any cached variables */
39 /* read return-value now */
40 return ioread16(&card
->dpram
[DPRAM_FCT_RESULT
]);
42 if ((ret
!= vector
) || time_after(jiffies
, stamp
))
44 /* process context => relax */
45 usleep_range(500, 10000);
48 ret
= (ret
== RES_NONE
) ? -ETIMEDOUT
: -ECANCELED
;
49 dev_alert(&card
->pdev
->dev
, "firmware %s failed (%i)\n", msg
, ret
);
53 static int softing_fct_cmd(struct softing
*card
, int16_t cmd
, const char *msg
)
57 ret
= _softing_fct_cmd(card
, cmd
, 0, msg
);
59 dev_alert(&card
->pdev
->dev
, "%s returned %u\n", msg
, ret
);
65 int softing_bootloader_command(struct softing
*card
, int16_t cmd
,
71 iowrite16(RES_NONE
, &card
->dpram
[DPRAM_RECEIPT
]);
72 iowrite16(cmd
, &card
->dpram
[DPRAM_COMMAND
]);
73 /* be sure to flush this to the card */
75 stamp
= jiffies
+ 3 * HZ
;
78 ret
= ioread16(&card
->dpram
[DPRAM_RECEIPT
]);
79 /* don't have any cached variables */
83 if (time_after(jiffies
, stamp
))
85 /* process context => relax */
86 usleep_range(500, 10000);
87 } while (!signal_pending(current
));
89 ret
= (ret
== RES_NONE
) ? -ETIMEDOUT
: -ECANCELED
;
90 dev_alert(&card
->pdev
->dev
, "bootloader %s failed (%i)\n", msg
, ret
);
94 static int fw_parse(const uint8_t **pmem
, uint16_t *ptype
, uint32_t *paddr
,
95 uint16_t *plen
, const uint8_t **pdat
)
102 * firmware records are a binary, unaligned stream composed of:
108 * all values in little endian.
109 * We could define a struct for this, with __attribute__((packed)),
110 * but would that solve the alignment in _all_ cases (cfr. the
111 * struct itself may be an odd address)?
113 * I chose to use leXX_to_cpup() since this solves both
114 * endianness & alignment.
117 *ptype
= le16_to_cpup((void *)&mem
[0]);
118 *paddr
= le32_to_cpup((void *)&mem
[2]);
119 *plen
= le16_to_cpup((void *)&mem
[6]);
121 /* verify checksum */
122 end
= &mem
[8 + *plen
];
123 checksum
[0] = le16_to_cpup((void *)end
);
124 for (checksum
[1] = 0; mem
< end
; ++mem
)
126 if (checksum
[0] != checksum
[1])
133 int softing_load_fw(const char *file
, struct softing
*card
,
134 __iomem
uint8_t *dpram
, unsigned int size
, int offset
)
136 const struct firmware
*fw
;
138 const uint8_t *mem
, *end
, *dat
;
141 uint8_t *buf
= NULL
, *new_buf
;
145 ret
= request_firmware(&fw
, file
, &card
->pdev
->dev
);
148 dev_dbg(&card
->pdev
->dev
, "%s, firmware(%s) got %u bytes"
149 ", offset %c0x%04x\n",
150 card
->pdat
->name
, file
, (unsigned int)fw
->size
,
151 (offset
>= 0) ? '+' : '-', (unsigned int)abs(offset
));
152 /* parse the firmware */
154 end
= &mem
[fw
->size
];
155 /* look for header record */
156 ret
= fw_parse(&mem
, &type
, &addr
, &len
, &dat
);
161 if (strncmp("Structured Binary Format, Softing GmbH" , dat
, len
)) {
165 /* ok, we had a header */
167 ret
= fw_parse(&mem
, &type
, &addr
, &len
, &dat
);
171 /* start address, not used here */
173 } else if (type
== 1) {
177 } else if (type
!= 0) {
182 if ((addr
+ len
+ offset
) > size
)
184 memcpy_toio(&dpram
[addr
+ offset
], dat
, len
);
185 /* be sure to flush caches from IO space */
189 buflen
= (len
+ (1024-1)) & ~(1024-1);
190 new_buf
= krealloc(buf
, buflen
, GFP_KERNEL
);
197 /* verify record data */
198 memcpy_fromio(buf
, &dpram
[addr
+ offset
], len
);
199 if (memcmp(buf
, dat
, len
)) {
201 dev_alert(&card
->pdev
->dev
, "DPRAM readback failed\n");
207 /* no end record seen */
212 release_firmware(fw
);
214 dev_info(&card
->pdev
->dev
, "firmware %s failed\n", file
);
218 int softing_load_app_fw(const char *file
, struct softing
*card
)
220 const struct firmware
*fw
;
221 const uint8_t *mem
, *end
, *dat
;
224 uint32_t addr
, start_addr
= 0;
225 unsigned int sum
, rx_sum
;
226 int8_t type_end
= 0, type_entrypoint
= 0;
228 ret
= request_firmware(&fw
, file
, &card
->pdev
->dev
);
230 dev_alert(&card
->pdev
->dev
, "request_firmware(%s) got %i\n",
234 dev_dbg(&card
->pdev
->dev
, "firmware(%s) got %lu bytes\n",
235 file
, (unsigned long)fw
->size
);
236 /* parse the firmware */
238 end
= &mem
[fw
->size
];
239 /* look for header record */
240 ret
= fw_parse(&mem
, &type
, &addr
, &len
, &dat
);
244 if (type
!= 0xffff) {
245 dev_alert(&card
->pdev
->dev
, "firmware starts with type 0x%x\n",
249 if (strncmp("Structured Binary Format, Softing GmbH", dat
, len
)) {
250 dev_alert(&card
->pdev
->dev
, "firmware string '%.*s' fault\n",
254 /* ok, we had a header */
256 ret
= fw_parse(&mem
, &type
, &addr
, &len
, &dat
);
265 } else if (type
== 1) {
269 } else if (type
!= 0) {
270 dev_alert(&card
->pdev
->dev
,
271 "unknown record type 0x%04x\n", type
);
277 for (sum
= 0, j
= 0; j
< len
; ++j
)
279 /* work in 16bit (target) */
282 memcpy_toio(&card
->dpram
[card
->pdat
->app
.offs
], dat
, len
);
283 iowrite32(card
->pdat
->app
.offs
+ card
->pdat
->app
.addr
,
284 &card
->dpram
[DPRAM_COMMAND
+ 2]);
285 iowrite32(addr
, &card
->dpram
[DPRAM_COMMAND
+ 6]);
286 iowrite16(len
, &card
->dpram
[DPRAM_COMMAND
+ 10]);
287 iowrite8(1, &card
->dpram
[DPRAM_COMMAND
+ 12]);
288 ret
= softing_bootloader_command(card
, 1, "loading app.");
291 /* verify checksum */
292 rx_sum
= ioread16(&card
->dpram
[DPRAM_RECEIPT
+ 2]);
294 dev_alert(&card
->pdev
->dev
, "SRAM seems to be damaged"
295 ", wanted 0x%04x, got 0x%04x\n", sum
, rx_sum
);
300 if (!type_end
|| !type_entrypoint
)
302 /* start application in card */
303 iowrite32(start_addr
, &card
->dpram
[DPRAM_COMMAND
+ 2]);
304 iowrite8(1, &card
->dpram
[DPRAM_COMMAND
+ 6]);
305 ret
= softing_bootloader_command(card
, 3, "start app.");
310 release_firmware(fw
);
312 dev_info(&card
->pdev
->dev
, "firmware %s failed\n", file
);
316 static int softing_reset_chip(struct softing
*card
)
322 iowrite8(0, &card
->dpram
[DPRAM_RESET_RX_FIFO
]);
323 iowrite8(0, &card
->dpram
[DPRAM_RESET_RX_FIFO
+1]);
324 iowrite8(1, &card
->dpram
[DPRAM_RESET
]);
325 iowrite8(0, &card
->dpram
[DPRAM_RESET
+1]);
327 ret
= softing_fct_cmd(card
, 0, "reset_can");
330 if (signal_pending(current
))
331 /* don't wait any longer */
334 card
->tx
.pending
= 0;
338 int softing_chip_poweron(struct softing
*card
)
342 ret
= _softing_fct_cmd(card
, 99, 0x55, "sync-a");
346 ret
= _softing_fct_cmd(card
, 99, 0xaa, "sync-b");
350 ret
= softing_reset_chip(card
);
354 ret
= softing_fct_cmd(card
, 43, "get_serial_number");
357 card
->id
.serial
= ioread32(&card
->dpram
[DPRAM_FCT_PARAM
]);
359 ret
= softing_fct_cmd(card
, 12, "get_version");
362 card
->id
.fw_version
= ioread16(&card
->dpram
[DPRAM_FCT_PARAM
+ 2]);
363 card
->id
.hw_version
= ioread16(&card
->dpram
[DPRAM_FCT_PARAM
+ 4]);
364 card
->id
.license
= ioread16(&card
->dpram
[DPRAM_FCT_PARAM
+ 6]);
365 card
->id
.chip
[0] = ioread16(&card
->dpram
[DPRAM_FCT_PARAM
+ 8]);
366 card
->id
.chip
[1] = ioread16(&card
->dpram
[DPRAM_FCT_PARAM
+ 10]);
372 static void softing_initialize_timestamp(struct softing
*card
)
376 card
->ts_ref
= ktime_get();
378 /* 16MHz is the reference */
379 ovf
= 0x100000000ULL
* 16;
380 do_div(ovf
, card
->pdat
->freq
?: 16);
382 card
->ts_overflow
= ktime_add_us(0, ovf
);
385 ktime_t
softing_raw2ktime(struct softing
*card
, u32 raw
)
388 ktime_t now
, real_offset
;
393 real_offset
= ktime_sub(ktime_get_real(), now
);
395 /* find nsec from card */
397 do_div(rawl
, card
->pdat
->freq
?: 16);
398 target
= ktime_add_us(card
->ts_ref
, rawl
);
399 /* test for overflows */
400 tmp
= ktime_add(target
, card
->ts_overflow
);
401 while (unlikely(ktime_to_ns(tmp
) > ktime_to_ns(now
))) {
402 card
->ts_ref
= ktime_add(card
->ts_ref
, card
->ts_overflow
);
404 tmp
= ktime_add(target
, card
->ts_overflow
);
406 return ktime_add(target
, real_offset
);
409 static inline int softing_error_reporting(struct net_device
*netdev
)
411 struct softing_priv
*priv
= netdev_priv(netdev
);
413 return (priv
->can
.ctrlmode
& CAN_CTRLMODE_BERR_REPORTING
)
417 int softing_startstop(struct net_device
*dev
, int up
)
420 struct softing
*card
;
421 struct softing_priv
*priv
;
422 struct net_device
*netdev
;
423 int bus_bitmask_start
;
424 int j
, error_reporting
;
425 struct can_frame msg
;
426 const struct can_bittiming
*bt
;
428 priv
= netdev_priv(dev
);
434 ret
= mutex_lock_interruptible(&card
->fw
.lock
);
438 bus_bitmask_start
= 0;
440 /* prepare to start this bus as well */
441 bus_bitmask_start
|= (1 << priv
->index
);
442 /* bring netdevs down */
443 for (j
= 0; j
< ARRAY_SIZE(card
->net
); ++j
) {
444 netdev
= card
->net
[j
];
447 priv
= netdev_priv(netdev
);
450 netif_stop_queue(netdev
);
452 if (netif_running(netdev
)) {
454 bus_bitmask_start
|= (1 << j
);
455 priv
->tx
.pending
= 0;
456 priv
->tx
.echo_put
= 0;
457 priv
->tx
.echo_get
= 0;
459 * this bus' may just have called open_candev()
460 * which is rather stupid to call close_candev()
462 * but we may come here from busoff recovery too
463 * in which case the echo_skb _needs_ flushing too.
464 * just be sure to call open_candev() again
466 close_candev(netdev
);
468 priv
->can
.state
= CAN_STATE_STOPPED
;
470 card
->tx
.pending
= 0;
472 softing_enable_irq(card
, 0);
473 ret
= softing_reset_chip(card
);
476 if (!bus_bitmask_start
)
477 /* no buses to be brought up */
480 if ((bus_bitmask_start
& 1) && (bus_bitmask_start
& 2)
481 && (softing_error_reporting(card
->net
[0])
482 != softing_error_reporting(card
->net
[1]))) {
483 dev_alert(&card
->pdev
->dev
,
484 "err_reporting flag differs for buses\n");
488 if (bus_bitmask_start
& 1) {
489 netdev
= card
->net
[0];
490 priv
= netdev_priv(netdev
);
491 error_reporting
+= softing_error_reporting(netdev
);
493 bt
= &priv
->can
.bittiming
;
494 iowrite16(bt
->brp
, &card
->dpram
[DPRAM_FCT_PARAM
+ 2]);
495 iowrite16(bt
->sjw
, &card
->dpram
[DPRAM_FCT_PARAM
+ 4]);
496 iowrite16(bt
->phase_seg1
+ bt
->prop_seg
,
497 &card
->dpram
[DPRAM_FCT_PARAM
+ 6]);
498 iowrite16(bt
->phase_seg2
, &card
->dpram
[DPRAM_FCT_PARAM
+ 8]);
499 iowrite16((priv
->can
.ctrlmode
& CAN_CTRLMODE_3_SAMPLES
) ? 1 : 0,
500 &card
->dpram
[DPRAM_FCT_PARAM
+ 10]);
501 ret
= softing_fct_cmd(card
, 1, "initialize_chip[0]");
505 iowrite16(0, &card
->dpram
[DPRAM_FCT_PARAM
+ 2]);
506 iowrite16(0, &card
->dpram
[DPRAM_FCT_PARAM
+ 4]);
507 ret
= softing_fct_cmd(card
, 3, "set_mode[0]");
511 /* 11bit id & mask */
512 iowrite16(0x0000, &card
->dpram
[DPRAM_FCT_PARAM
+ 2]);
513 iowrite16(0x07ff, &card
->dpram
[DPRAM_FCT_PARAM
+ 4]);
514 /* 29bit id.lo & mask.lo & id.hi & mask.hi */
515 iowrite16(0x0000, &card
->dpram
[DPRAM_FCT_PARAM
+ 6]);
516 iowrite16(0xffff, &card
->dpram
[DPRAM_FCT_PARAM
+ 8]);
517 iowrite16(0x0000, &card
->dpram
[DPRAM_FCT_PARAM
+ 10]);
518 iowrite16(0x1fff, &card
->dpram
[DPRAM_FCT_PARAM
+ 12]);
519 ret
= softing_fct_cmd(card
, 7, "set_filter[0]");
522 /* set output control */
523 iowrite16(priv
->output
, &card
->dpram
[DPRAM_FCT_PARAM
+ 2]);
524 ret
= softing_fct_cmd(card
, 5, "set_output[0]");
528 if (bus_bitmask_start
& 2) {
529 netdev
= card
->net
[1];
530 priv
= netdev_priv(netdev
);
531 error_reporting
+= softing_error_reporting(netdev
);
533 bt
= &priv
->can
.bittiming
;
534 iowrite16(bt
->brp
, &card
->dpram
[DPRAM_FCT_PARAM
+ 2]);
535 iowrite16(bt
->sjw
, &card
->dpram
[DPRAM_FCT_PARAM
+ 4]);
536 iowrite16(bt
->phase_seg1
+ bt
->prop_seg
,
537 &card
->dpram
[DPRAM_FCT_PARAM
+ 6]);
538 iowrite16(bt
->phase_seg2
, &card
->dpram
[DPRAM_FCT_PARAM
+ 8]);
539 iowrite16((priv
->can
.ctrlmode
& CAN_CTRLMODE_3_SAMPLES
) ? 1 : 0,
540 &card
->dpram
[DPRAM_FCT_PARAM
+ 10]);
541 ret
= softing_fct_cmd(card
, 2, "initialize_chip[1]");
545 iowrite16(0, &card
->dpram
[DPRAM_FCT_PARAM
+ 2]);
546 iowrite16(0, &card
->dpram
[DPRAM_FCT_PARAM
+ 4]);
547 ret
= softing_fct_cmd(card
, 4, "set_mode[1]");
551 /* 11bit id & mask */
552 iowrite16(0x0000, &card
->dpram
[DPRAM_FCT_PARAM
+ 2]);
553 iowrite16(0x07ff, &card
->dpram
[DPRAM_FCT_PARAM
+ 4]);
554 /* 29bit id.lo & mask.lo & id.hi & mask.hi */
555 iowrite16(0x0000, &card
->dpram
[DPRAM_FCT_PARAM
+ 6]);
556 iowrite16(0xffff, &card
->dpram
[DPRAM_FCT_PARAM
+ 8]);
557 iowrite16(0x0000, &card
->dpram
[DPRAM_FCT_PARAM
+ 10]);
558 iowrite16(0x1fff, &card
->dpram
[DPRAM_FCT_PARAM
+ 12]);
559 ret
= softing_fct_cmd(card
, 8, "set_filter[1]");
562 /* set output control2 */
563 iowrite16(priv
->output
, &card
->dpram
[DPRAM_FCT_PARAM
+ 2]);
564 ret
= softing_fct_cmd(card
, 6, "set_output[1]");
568 /* enable_error_frame */
570 * Error reporting is switched off at the moment since
571 * the receiving of them is not yet 100% verified
572 * This should be enabled sooner or later
574 if (error_reporting) {
575 ret = softing_fct_cmd(card, 51, "enable_error_frame");
580 /* initialize interface */
581 iowrite16(1, &card
->dpram
[DPRAM_FCT_PARAM
+ 2]);
582 iowrite16(1, &card
->dpram
[DPRAM_FCT_PARAM
+ 4]);
583 iowrite16(1, &card
->dpram
[DPRAM_FCT_PARAM
+ 6]);
584 iowrite16(1, &card
->dpram
[DPRAM_FCT_PARAM
+ 8]);
585 iowrite16(1, &card
->dpram
[DPRAM_FCT_PARAM
+ 10]);
586 iowrite16(1, &card
->dpram
[DPRAM_FCT_PARAM
+ 12]);
587 iowrite16(1, &card
->dpram
[DPRAM_FCT_PARAM
+ 14]);
588 iowrite16(1, &card
->dpram
[DPRAM_FCT_PARAM
+ 16]);
589 iowrite16(1, &card
->dpram
[DPRAM_FCT_PARAM
+ 18]);
590 iowrite16(1, &card
->dpram
[DPRAM_FCT_PARAM
+ 20]);
591 ret
= softing_fct_cmd(card
, 17, "initialize_interface");
595 ret
= softing_fct_cmd(card
, 36, "enable_fifo");
598 /* enable fifo tx ack */
599 ret
= softing_fct_cmd(card
, 13, "fifo_tx_ack[0]");
602 /* enable fifo tx ack2 */
603 ret
= softing_fct_cmd(card
, 14, "fifo_tx_ack[1]");
607 ret
= softing_fct_cmd(card
, 11, "start_chip");
610 iowrite8(0, &card
->dpram
[DPRAM_INFO_BUSSTATE
]);
611 iowrite8(0, &card
->dpram
[DPRAM_INFO_BUSSTATE2
]);
612 if (card
->pdat
->generation
< 2) {
613 iowrite8(0, &card
->dpram
[DPRAM_V2_IRQ_TOHOST
]);
614 /* flush the DPRAM caches */
618 softing_initialize_timestamp(card
);
621 * do socketcan notifications/status changes
622 * from here, no errors should occur, or the failed: part
625 memset(&msg
, 0, sizeof(msg
));
626 msg
.can_id
= CAN_ERR_FLAG
| CAN_ERR_RESTARTED
;
627 msg
.len
= CAN_ERR_DLC
;
628 for (j
= 0; j
< ARRAY_SIZE(card
->net
); ++j
) {
629 if (!(bus_bitmask_start
& (1 << j
)))
631 netdev
= card
->net
[j
];
634 priv
= netdev_priv(netdev
);
635 priv
->can
.state
= CAN_STATE_ERROR_ACTIVE
;
638 /* notify other buses on the restart */
639 softing_netdev_rx(netdev
, &msg
, 0);
640 ++priv
->can
.can_stats
.restarts
;
642 netif_wake_queue(netdev
);
645 /* enable interrupts */
646 ret
= softing_enable_irq(card
, 1);
650 mutex_unlock(&card
->fw
.lock
);
655 softing_enable_irq(card
, 0);
656 softing_reset_chip(card
);
657 mutex_unlock(&card
->fw
.lock
);
658 /* bring all other interfaces down */
659 for (j
= 0; j
< ARRAY_SIZE(card
->net
); ++j
) {
660 netdev
= card
->net
[j
];
668 int softing_default_output(struct net_device
*netdev
)
670 struct softing_priv
*priv
= netdev_priv(netdev
);
671 struct softing
*card
= priv
->card
;
673 switch (priv
->chip
) {
675 return (card
->pdat
->generation
< 2) ? 0xfb : 0xfa;