2 * Copyright (C) 2008-2010
4 * - Kurt Van Dijck, EIA Electronics
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
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.
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
20 #include <linux/firmware.h>
21 #include <linux/sched.h>
22 #include <asm/div64.h>
28 * low level DPRAM command.
29 * Make sure that card->dpram[DPRAM_FCT_HOST] is preset
31 static int _softing_fct_cmd(struct softing
*card
, int16_t cmd
, uint16_t vector
,
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 */
42 stamp
= jiffies
+ 1 * HZ
;
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 */
51 /* read return-value now */
52 return ioread16(&card
->dpram
[DPRAM_FCT_RESULT
]);
54 if ((ret
!= vector
) || time_after(jiffies
, stamp
))
56 /* process context => relax */
57 usleep_range(500, 10000);
60 ret
= (ret
== RES_NONE
) ? -ETIMEDOUT
: -ECANCELED
;
61 dev_alert(&card
->pdev
->dev
, "firmware %s failed (%i)\n", msg
, ret
);
65 static int softing_fct_cmd(struct softing
*card
, int16_t cmd
, const char *msg
)
69 ret
= _softing_fct_cmd(card
, cmd
, 0, msg
);
71 dev_alert(&card
->pdev
->dev
, "%s returned %u\n", msg
, ret
);
77 int softing_bootloader_command(struct softing
*card
, int16_t cmd
,
83 iowrite16(RES_NONE
, &card
->dpram
[DPRAM_RECEIPT
]);
84 iowrite16(cmd
, &card
->dpram
[DPRAM_COMMAND
]);
85 /* be sure to flush this to the card */
87 stamp
= jiffies
+ 3 * HZ
;
90 ret
= ioread16(&card
->dpram
[DPRAM_RECEIPT
]);
91 /* don't have any cached variables */
95 if (time_after(jiffies
, stamp
))
97 /* process context => relax */
98 usleep_range(500, 10000);
99 } while (!signal_pending(current
));
101 ret
= (ret
== RES_NONE
) ? -ETIMEDOUT
: -ECANCELED
;
102 dev_alert(&card
->pdev
->dev
, "bootloader %s failed (%i)\n", msg
, ret
);
106 static int fw_parse(const uint8_t **pmem
, uint16_t *ptype
, uint32_t *paddr
,
107 uint16_t *plen
, const uint8_t **pdat
)
109 uint16_t checksum
[2];
114 * firmware records are a binary, unaligned stream composed of:
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)?
125 * I chose to use leXX_to_cpup() since this solves both
126 * endianness & alignment.
129 *ptype
= le16_to_cpup((void *)&mem
[0]);
130 *paddr
= le32_to_cpup((void *)&mem
[2]);
131 *plen
= le16_to_cpup((void *)&mem
[6]);
133 /* verify checksum */
134 end
= &mem
[8 + *plen
];
135 checksum
[0] = le16_to_cpup((void *)end
);
136 for (checksum
[1] = 0; mem
< end
; ++mem
)
138 if (checksum
[0] != checksum
[1])
145 int softing_load_fw(const char *file
, struct softing
*card
,
146 __iomem
uint8_t *dpram
, unsigned int size
, int offset
)
148 const struct firmware
*fw
;
150 const uint8_t *mem
, *end
, *dat
;
153 uint8_t *buf
= NULL
, *new_buf
;
157 ret
= request_firmware(&fw
, file
, &card
->pdev
->dev
);
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 */
166 end
= &mem
[fw
->size
];
167 /* look for header record */
168 ret
= fw_parse(&mem
, &type
, &addr
, &len
, &dat
);
173 if (strncmp("Structured Binary Format, Softing GmbH" , dat
, len
)) {
177 /* ok, we had a header */
179 ret
= fw_parse(&mem
, &type
, &addr
, &len
, &dat
);
183 /* start address, not used here */
185 } else if (type
== 1) {
189 } else if (type
!= 0) {
194 if ((addr
+ len
+ offset
) > size
)
196 memcpy_toio(&dpram
[addr
+ offset
], dat
, len
);
197 /* be sure to flush caches from IO space */
201 buflen
= (len
+ (1024-1)) & ~(1024-1);
202 new_buf
= krealloc(buf
, buflen
, GFP_KERNEL
);
209 /* verify record data */
210 memcpy_fromio(buf
, &dpram
[addr
+ offset
], len
);
211 if (memcmp(buf
, dat
, len
)) {
213 dev_alert(&card
->pdev
->dev
, "DPRAM readback failed\n");
219 /* no end record seen */
224 release_firmware(fw
);
226 dev_info(&card
->pdev
->dev
, "firmware %s failed\n", file
);
230 int softing_load_app_fw(const char *file
, struct softing
*card
)
232 const struct firmware
*fw
;
233 const uint8_t *mem
, *end
, *dat
;
236 uint32_t addr
, start_addr
= 0;
237 unsigned int sum
, rx_sum
;
238 int8_t type_end
= 0, type_entrypoint
= 0;
240 ret
= request_firmware(&fw
, file
, &card
->pdev
->dev
);
242 dev_alert(&card
->pdev
->dev
, "request_firmware(%s) got %i\n",
246 dev_dbg(&card
->pdev
->dev
, "firmware(%s) got %lu bytes\n",
247 file
, (unsigned long)fw
->size
);
248 /* parse the firmware */
250 end
= &mem
[fw
->size
];
251 /* look for header record */
252 ret
= fw_parse(&mem
, &type
, &addr
, &len
, &dat
);
256 if (type
!= 0xffff) {
257 dev_alert(&card
->pdev
->dev
, "firmware starts with type 0x%x\n",
261 if (strncmp("Structured Binary Format, Softing GmbH", dat
, len
)) {
262 dev_alert(&card
->pdev
->dev
, "firmware string '%.*s' fault\n",
266 /* ok, we had a header */
268 ret
= fw_parse(&mem
, &type
, &addr
, &len
, &dat
);
277 } else if (type
== 1) {
281 } else if (type
!= 0) {
282 dev_alert(&card
->pdev
->dev
,
283 "unknown record type 0x%04x\n", type
);
289 for (sum
= 0, j
= 0; j
< len
; ++j
)
291 /* work in 16bit (target) */
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.");
303 /* verify checksum */
304 rx_sum
= ioread16(&card
->dpram
[DPRAM_RECEIPT
+ 2]);
306 dev_alert(&card
->pdev
->dev
, "SRAM seems to be damaged"
307 ", wanted 0x%04x, got 0x%04x\n", sum
, rx_sum
);
312 if (!type_end
|| !type_entrypoint
)
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.");
322 release_firmware(fw
);
324 dev_info(&card
->pdev
->dev
, "firmware %s failed\n", file
);
328 static int softing_reset_chip(struct softing
*card
)
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]);
339 ret
= softing_fct_cmd(card
, 0, "reset_can");
342 if (signal_pending(current
))
343 /* don't wait any longer */
346 card
->tx
.pending
= 0;
350 int softing_chip_poweron(struct softing
*card
)
354 ret
= _softing_fct_cmd(card
, 99, 0x55, "sync-a");
358 ret
= _softing_fct_cmd(card
, 99, 0xaa, "sync-b");
362 ret
= softing_reset_chip(card
);
366 ret
= softing_fct_cmd(card
, 43, "get_serial_number");
369 card
->id
.serial
= ioread32(&card
->dpram
[DPRAM_FCT_PARAM
]);
371 ret
= softing_fct_cmd(card
, 12, "get_version");
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]);
384 static void softing_initialize_timestamp(struct softing
*card
)
388 card
->ts_ref
= ktime_get();
390 /* 16MHz is the reference */
391 ovf
= 0x100000000ULL
* 16;
392 do_div(ovf
, card
->pdat
->freq
?: 16);
394 card
->ts_overflow
= ktime_add_us(ktime_set(0, 0), ovf
);
397 ktime_t
softing_raw2ktime(struct softing
*card
, u32 raw
)
400 ktime_t now
, real_offset
;
405 real_offset
= ktime_sub(ktime_get_real(), now
);
407 /* find nsec from card */
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
);
416 tmp
= ktime_add(target
, card
->ts_overflow
);
418 return ktime_add(target
, real_offset
);
421 static inline int softing_error_reporting(struct net_device
*netdev
)
423 struct softing_priv
*priv
= netdev_priv(netdev
);
425 return (priv
->can
.ctrlmode
& CAN_CTRLMODE_BERR_REPORTING
)
429 int softing_startstop(struct net_device
*dev
, int up
)
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
;
440 priv
= netdev_priv(dev
);
446 ret
= mutex_lock_interruptible(&card
->fw
.lock
);
450 bus_bitmask_start
= 0;
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
];
459 priv
= netdev_priv(netdev
);
462 netif_stop_queue(netdev
);
464 if (netif_running(netdev
)) {
466 bus_bitmask_start
|= (1 << j
);
467 priv
->tx
.pending
= 0;
468 priv
->tx
.echo_put
= 0;
469 priv
->tx
.echo_get
= 0;
471 * this bus' may just have called open_candev()
472 * which is rather stupid to call close_candev()
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
478 close_candev(netdev
);
480 priv
->can
.state
= CAN_STATE_STOPPED
;
482 card
->tx
.pending
= 0;
484 softing_enable_irq(card
, 0);
485 ret
= softing_reset_chip(card
);
488 if (!bus_bitmask_start
)
489 /* no busses to be brought up */
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");
500 if (bus_bitmask_start
& 1) {
501 netdev
= card
->net
[0];
502 priv
= netdev_priv(netdev
);
503 error_reporting
+= softing_error_reporting(netdev
);
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]");
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]");
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]");
534 /* set output control */
535 iowrite16(priv
->output
, &card
->dpram
[DPRAM_FCT_PARAM
+ 2]);
536 ret
= softing_fct_cmd(card
, 5, "set_output[0]");
540 if (bus_bitmask_start
& 2) {
541 netdev
= card
->net
[1];
542 priv
= netdev_priv(netdev
);
543 error_reporting
+= softing_error_reporting(netdev
);
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]");
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]");
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]");
574 /* set output control2 */
575 iowrite16(priv
->output
, &card
->dpram
[DPRAM_FCT_PARAM
+ 2]);
576 ret
= softing_fct_cmd(card
, 6, "set_output[1]");
580 /* enable_error_frame */
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
586 if (error_reporting) {
587 ret = softing_fct_cmd(card, 51, "enable_error_frame");
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");
607 ret
= softing_fct_cmd(card
, 36, "enable_fifo");
610 /* enable fifo tx ack */
611 ret
= softing_fct_cmd(card
, 13, "fifo_tx_ack[0]");
614 /* enable fifo tx ack2 */
615 ret
= softing_fct_cmd(card
, 14, "fifo_tx_ack[1]");
619 ret
= softing_fct_cmd(card
, 11, "start_chip");
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 */
630 softing_initialize_timestamp(card
);
633 * do socketcan notifications/status changes
634 * from here, no errors should occur, or the failed: part
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
)))
643 netdev
= card
->net
[j
];
646 priv
= netdev_priv(netdev
);
647 priv
->can
.state
= CAN_STATE_ERROR_ACTIVE
;
650 /* notify other busses on the restart */
651 softing_netdev_rx(netdev
, &msg
, ktime_set(0, 0));
652 ++priv
->can
.can_stats
.restarts
;
654 netif_wake_queue(netdev
);
657 /* enable interrupts */
658 ret
= softing_enable_irq(card
, 1);
662 mutex_unlock(&card
->fw
.lock
);
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
];
680 int softing_default_output(struct net_device
*netdev
)
682 struct softing_priv
*priv
= netdev_priv(netdev
);
683 struct softing
*card
= priv
->card
;
685 switch (priv
->chip
) {
687 return (card
->pdat
->generation
< 2) ? 0xfb : 0xfa;