1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Janz MODULbus VMOD-ICAN3 CAN Interface Driver
5 * Copyright (c) 2010 Ira W. Snyder <iws@ovro.caltech.edu>
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/interrupt.h>
11 #include <linux/delay.h>
12 #include <linux/ethtool.h>
13 #include <linux/platform_device.h>
15 #include <linux/netdevice.h>
16 #include <linux/can.h>
17 #include <linux/can/dev.h>
18 #include <linux/can/skb.h>
19 #include <linux/can/error.h>
21 #include <linux/mfd/janz.h>
24 /* the DPM has 64k of memory, organized into 256x 256 byte pages */
25 #define DPM_NUM_PAGES 256
26 #define DPM_PAGE_SIZE 256
27 #define DPM_PAGE_ADDR(p) ((p) * DPM_PAGE_SIZE)
29 /* JANZ ICAN3 "old-style" host interface queue page numbers */
30 #define QUEUE_OLD_CONTROL 0
31 #define QUEUE_OLD_RB0 1
32 #define QUEUE_OLD_RB1 2
33 #define QUEUE_OLD_WB0 3
34 #define QUEUE_OLD_WB1 4
36 /* Janz ICAN3 "old-style" host interface control registers */
37 #define MSYNC_PEER 0x00 /* ICAN only */
38 #define MSYNC_LOCL 0x01 /* host only */
39 #define TARGET_RUNNING 0x02
40 #define FIRMWARE_STAMP 0x60 /* big endian firmware stamp */
42 #define MSYNC_RB0 0x01
43 #define MSYNC_RB1 0x02
44 #define MSYNC_RBLW 0x04
45 #define MSYNC_RB_MASK (MSYNC_RB0 | MSYNC_RB1)
47 #define MSYNC_WB0 0x10
48 #define MSYNC_WB1 0x20
49 #define MSYNC_WBLW 0x40
50 #define MSYNC_WB_MASK (MSYNC_WB0 | MSYNC_WB1)
52 /* Janz ICAN3 "new-style" host interface queue page numbers */
53 #define QUEUE_TOHOST 5
54 #define QUEUE_FROMHOST_MID 6
55 #define QUEUE_FROMHOST_HIGH 7
56 #define QUEUE_FROMHOST_LOW 8
58 /* The first free page in the DPM is #9 */
59 #define DPM_FREE_START 9
61 /* Janz ICAN3 "new-style" and "fast" host interface descriptor flags */
62 #define DESC_VALID 0x80
63 #define DESC_WRAP 0x40
64 #define DESC_INTERRUPT 0x20
65 #define DESC_IVALID 0x10
66 #define DESC_LEN(len) (len)
68 /* Janz ICAN3 Firmware Messages */
69 #define MSG_CONNECTI 0x02
70 #define MSG_DISCONNECT 0x03
71 #define MSG_IDVERS 0x04
72 #define MSG_MSGLOST 0x05
73 #define MSG_NEWHOSTIF 0x08
74 #define MSG_INQUIRY 0x0a
75 #define MSG_SETAFILMASK 0x10
76 #define MSG_INITFDPMQUEUE 0x11
77 #define MSG_HWCONF 0x12
78 #define MSG_FMSGLOST 0x15
79 #define MSG_CEVTIND 0x37
80 #define MSG_CBTRREQ 0x41
81 #define MSG_COFFREQ 0x42
82 #define MSG_CONREQ 0x43
83 #define MSG_CCONFREQ 0x47
88 * Janz ICAN3 CAN Inquiry Message Types
90 * NOTE: there appears to be a firmware bug here. You must send
91 * NOTE: INQUIRY_STATUS and expect to receive an INQUIRY_EXTENDED
92 * NOTE: response. The controller never responds to a message with
93 * NOTE: the INQUIRY_EXTENDED subspec :(
95 #define INQUIRY_STATUS 0x00
96 #define INQUIRY_TERMINATION 0x01
97 #define INQUIRY_EXTENDED 0x04
99 /* Janz ICAN3 CAN Set Acceptance Filter Mask Message Types */
100 #define SETAFILMASK_REJECT 0x00
101 #define SETAFILMASK_FASTIF 0x02
103 /* Janz ICAN3 CAN Hardware Configuration Message Types */
104 #define HWCONF_TERMINATE_ON 0x01
105 #define HWCONF_TERMINATE_OFF 0x00
107 /* Janz ICAN3 CAN Event Indication Message Types */
108 #define CEVTIND_EI 0x01
109 #define CEVTIND_DOI 0x02
110 #define CEVTIND_LOST 0x04
111 #define CEVTIND_FULL 0x08
112 #define CEVTIND_BEI 0x10
114 #define CEVTIND_CHIP_SJA1000 0x02
116 #define ICAN3_BUSERR_QUOTA_MAX 255
118 /* Janz ICAN3 CAN Frame Conversion */
119 #define ICAN3_SNGL 0x02
120 #define ICAN3_ECHO 0x10
121 #define ICAN3_EFF_RTR 0x40
122 #define ICAN3_SFF_RTR 0x10
123 #define ICAN3_EFF 0x80
125 #define ICAN3_CAN_TYPE_MASK 0x0f
126 #define ICAN3_CAN_TYPE_SFF 0x00
127 #define ICAN3_CAN_TYPE_EFF 0x01
129 #define ICAN3_CAN_DLC_MASK 0x0f
131 /* Janz ICAN3 NMTS subtypes */
132 #define NMTS_CREATE_NODE_REQ 0x0
133 #define NMTS_SLAVE_STATE_IND 0x8
134 #define NMTS_SLAVE_EVENT_IND 0x9
136 /* Janz ICAN3 LMTS subtypes */
137 #define LMTS_BUSON_REQ 0x0
138 #define LMTS_BUSOFF_REQ 0x1
139 #define LMTS_CAN_CONF_REQ 0x2
141 /* Janz ICAN3 NMTS Event indications */
142 #define NE_LOCAL_OCCURRED 0x3
143 #define NE_LOCAL_RESOLVED 0x2
144 #define NE_REMOTE_OCCURRED 0xc
145 #define NE_REMOTE_RESOLVED 0x8
148 * SJA1000 Status and Error Register Definitions
150 * Copied from drivers/net/can/sja1000/sja1000.h
153 /* status register content */
163 #define SR_CRIT (SR_BS|SR_ES)
170 #define ECC_FORM 0x40
171 #define ECC_STUFF 0x80
172 #define ECC_MASK 0xc0
174 /* Number of buffers for use in the "new-style" host interface */
175 #define ICAN3_NEW_BUFFERS 16
177 /* Number of buffers for use in the "fast" host interface */
178 #define ICAN3_TX_BUFFERS 512
179 #define ICAN3_RX_BUFFERS 1024
181 /* SJA1000 Clock Input */
182 #define ICAN3_CAN_CLOCK 8000000
184 /* Janz ICAN3 firmware types */
187 ICAN3_FWTYPE_CAL_CANOPEN
,
191 #define DRV_NAME "janz-ican3"
193 /* DPM Control Registers -- starts at offset 0x100 in the MODULbus registers */
194 struct ican3_dpm_control
{
195 /* window address register */
200 * Read access: clear interrupt from microcontroller
201 * Write access: send interrupt to microcontroller
206 /* write-only: reset all hardware on the module */
210 /* write-only: generate an interrupt to the TPU */
216 /* must be the first member */
219 /* CAN network device */
220 struct net_device
*ndev
;
221 struct napi_struct napi
;
226 /* base address of registers and IRQ */
227 struct janz_cmodio_onboard_regs __iomem
*ctrl
;
228 struct ican3_dpm_control __iomem
*dpmctrl
;
232 /* CAN bus termination status */
233 struct completion termination_comp
;
234 bool termination_enabled
;
236 /* CAN bus error status registers */
237 struct completion buserror_comp
;
238 struct can_berr_counter bec
;
241 enum ican3_fwtype fwtype
;
244 /* old and new style host interface */
247 /* queue for echo packets */
248 struct sk_buff_head echoq
;
251 * Any function which changes the current DPM page must hold this
252 * lock while it is performing data accesses. This ensures that the
253 * function will not be preempted and end up reading data from a
254 * different DPM page than it expects.
258 /* new host interface */
263 /* fast host interface */
264 unsigned int fastrx_start
;
265 unsigned int fastrx_num
;
266 unsigned int fasttx_start
;
267 unsigned int fasttx_num
;
269 /* first free DPM page */
270 unsigned int free_page
;
280 struct ican3_new_desc
{
285 struct ican3_fast_desc
{
291 /* write to the window basic address register */
292 static inline void ican3_set_page(struct ican3_dev
*mod
, unsigned int page
)
294 BUG_ON(page
>= DPM_NUM_PAGES
);
295 iowrite8(page
, &mod
->dpmctrl
->window_address
);
299 * ICAN3 "old-style" host interface
303 * Receive a message from the ICAN3 "old-style" firmware interface
305 * LOCKING: must hold mod->lock
307 * returns 0 on success, -ENOMEM when no message exists
309 static int ican3_old_recv_msg(struct ican3_dev
*mod
, struct ican3_msg
*msg
)
311 unsigned int mbox
, mbox_page
;
314 /* get the MSYNC registers */
315 ican3_set_page(mod
, QUEUE_OLD_CONTROL
);
316 peer
= ioread8(mod
->dpm
+ MSYNC_PEER
);
317 locl
= ioread8(mod
->dpm
+ MSYNC_LOCL
);
320 if ((xord
& MSYNC_RB_MASK
) == 0x00) {
321 netdev_dbg(mod
->ndev
, "no mbox for reading\n");
325 /* find the first free mbox to read */
326 if ((xord
& MSYNC_RB_MASK
) == MSYNC_RB_MASK
)
327 mbox
= (xord
& MSYNC_RBLW
) ? MSYNC_RB0
: MSYNC_RB1
;
329 mbox
= (xord
& MSYNC_RB0
) ? MSYNC_RB0
: MSYNC_RB1
;
331 /* copy the message */
332 mbox_page
= (mbox
== MSYNC_RB0
) ? QUEUE_OLD_RB0
: QUEUE_OLD_RB1
;
333 ican3_set_page(mod
, mbox_page
);
334 memcpy_fromio(msg
, mod
->dpm
, sizeof(*msg
));
337 * notify the firmware that the read buffer is available
338 * for it to fill again
342 ican3_set_page(mod
, QUEUE_OLD_CONTROL
);
343 iowrite8(locl
, mod
->dpm
+ MSYNC_LOCL
);
348 * Send a message through the "old-style" firmware interface
350 * LOCKING: must hold mod->lock
352 * returns 0 on success, -ENOMEM when no free space exists
354 static int ican3_old_send_msg(struct ican3_dev
*mod
, struct ican3_msg
*msg
)
356 unsigned int mbox
, mbox_page
;
359 /* get the MSYNC registers */
360 ican3_set_page(mod
, QUEUE_OLD_CONTROL
);
361 peer
= ioread8(mod
->dpm
+ MSYNC_PEER
);
362 locl
= ioread8(mod
->dpm
+ MSYNC_LOCL
);
365 if ((xord
& MSYNC_WB_MASK
) == MSYNC_WB_MASK
) {
366 netdev_err(mod
->ndev
, "no mbox for writing\n");
370 /* calculate a free mbox to use */
371 mbox
= (xord
& MSYNC_WB0
) ? MSYNC_WB1
: MSYNC_WB0
;
373 /* copy the message to the DPM */
374 mbox_page
= (mbox
== MSYNC_WB0
) ? QUEUE_OLD_WB0
: QUEUE_OLD_WB1
;
375 ican3_set_page(mod
, mbox_page
);
376 memcpy_toio(mod
->dpm
, msg
, sizeof(*msg
));
379 if (mbox
== MSYNC_WB1
)
382 ican3_set_page(mod
, QUEUE_OLD_CONTROL
);
383 iowrite8(locl
, mod
->dpm
+ MSYNC_LOCL
);
388 * ICAN3 "new-style" Host Interface Setup
391 static void ican3_init_new_host_interface(struct ican3_dev
*mod
)
393 struct ican3_new_desc desc
;
398 spin_lock_irqsave(&mod
->lock
, flags
);
400 /* setup the internal datastructures for RX */
404 /* tohost queue descriptors are in page 5 */
405 ican3_set_page(mod
, QUEUE_TOHOST
);
408 /* initialize the tohost (rx) queue descriptors: pages 9-24 */
409 for (i
= 0; i
< ICAN3_NEW_BUFFERS
; i
++) {
410 desc
.control
= DESC_INTERRUPT
| DESC_LEN(1); /* I L=1 */
411 desc
.pointer
= mod
->free_page
;
413 /* set wrap flag on last buffer */
414 if (i
== ICAN3_NEW_BUFFERS
- 1)
415 desc
.control
|= DESC_WRAP
;
417 memcpy_toio(dst
, &desc
, sizeof(desc
));
422 /* fromhost (tx) mid queue descriptors are in page 6 */
423 ican3_set_page(mod
, QUEUE_FROMHOST_MID
);
426 /* setup the internal datastructures for TX */
429 /* initialize the fromhost mid queue descriptors: pages 25-40 */
430 for (i
= 0; i
< ICAN3_NEW_BUFFERS
; i
++) {
431 desc
.control
= DESC_VALID
| DESC_LEN(1); /* V L=1 */
432 desc
.pointer
= mod
->free_page
;
434 /* set wrap flag on last buffer */
435 if (i
== ICAN3_NEW_BUFFERS
- 1)
436 desc
.control
|= DESC_WRAP
;
438 memcpy_toio(dst
, &desc
, sizeof(desc
));
443 /* fromhost hi queue descriptors are in page 7 */
444 ican3_set_page(mod
, QUEUE_FROMHOST_HIGH
);
447 /* initialize only a single buffer in the fromhost hi queue (unused) */
448 desc
.control
= DESC_VALID
| DESC_WRAP
| DESC_LEN(1); /* VW L=1 */
449 desc
.pointer
= mod
->free_page
;
450 memcpy_toio(dst
, &desc
, sizeof(desc
));
453 /* fromhost low queue descriptors are in page 8 */
454 ican3_set_page(mod
, QUEUE_FROMHOST_LOW
);
457 /* initialize only a single buffer in the fromhost low queue (unused) */
458 desc
.control
= DESC_VALID
| DESC_WRAP
| DESC_LEN(1); /* VW L=1 */
459 desc
.pointer
= mod
->free_page
;
460 memcpy_toio(dst
, &desc
, sizeof(desc
));
463 spin_unlock_irqrestore(&mod
->lock
, flags
);
467 * ICAN3 Fast Host Interface Setup
470 static void ican3_init_fast_host_interface(struct ican3_dev
*mod
)
472 struct ican3_fast_desc desc
;
478 spin_lock_irqsave(&mod
->lock
, flags
);
480 /* save the start recv page */
481 mod
->fastrx_start
= mod
->free_page
;
484 /* build a single fast tohost queue descriptor */
485 memset(&desc
, 0, sizeof(desc
));
489 /* build the tohost queue descriptor ring in memory */
491 for (i
= 0; i
< ICAN3_RX_BUFFERS
; i
++) {
493 /* set the wrap bit on the last buffer */
494 if (i
== ICAN3_RX_BUFFERS
- 1)
495 desc
.control
|= DESC_WRAP
;
497 /* switch to the correct page */
498 ican3_set_page(mod
, mod
->free_page
);
500 /* copy the descriptor to the DPM */
501 dst
= mod
->dpm
+ addr
;
502 memcpy_toio(dst
, &desc
, sizeof(desc
));
503 addr
+= sizeof(desc
);
505 /* move to the next page if necessary */
506 if (addr
>= DPM_PAGE_SIZE
) {
512 /* make sure we page-align the next queue */
516 /* save the start xmit page */
517 mod
->fasttx_start
= mod
->free_page
;
520 /* build a single fast fromhost queue descriptor */
521 memset(&desc
, 0, sizeof(desc
));
522 desc
.control
= DESC_VALID
;
525 /* build the fromhost queue descriptor ring in memory */
527 for (i
= 0; i
< ICAN3_TX_BUFFERS
; i
++) {
529 /* set the wrap bit on the last buffer */
530 if (i
== ICAN3_TX_BUFFERS
- 1)
531 desc
.control
|= DESC_WRAP
;
533 /* switch to the correct page */
534 ican3_set_page(mod
, mod
->free_page
);
536 /* copy the descriptor to the DPM */
537 dst
= mod
->dpm
+ addr
;
538 memcpy_toio(dst
, &desc
, sizeof(desc
));
539 addr
+= sizeof(desc
);
541 /* move to the next page if necessary */
542 if (addr
>= DPM_PAGE_SIZE
) {
548 spin_unlock_irqrestore(&mod
->lock
, flags
);
552 * ICAN3 "new-style" Host Interface Message Helpers
556 * LOCKING: must hold mod->lock
558 static int ican3_new_send_msg(struct ican3_dev
*mod
, struct ican3_msg
*msg
)
560 struct ican3_new_desc desc
;
561 void __iomem
*desc_addr
= mod
->dpm
+ (mod
->tx_num
* sizeof(desc
));
563 /* switch to the fromhost mid queue, and read the buffer descriptor */
564 ican3_set_page(mod
, QUEUE_FROMHOST_MID
);
565 memcpy_fromio(&desc
, desc_addr
, sizeof(desc
));
567 if (!(desc
.control
& DESC_VALID
)) {
568 netdev_dbg(mod
->ndev
, "%s: no free buffers\n", __func__
);
572 /* switch to the data page, copy the data */
573 ican3_set_page(mod
, desc
.pointer
);
574 memcpy_toio(mod
->dpm
, msg
, sizeof(*msg
));
576 /* switch back to the descriptor, set the valid bit, write it back */
577 ican3_set_page(mod
, QUEUE_FROMHOST_MID
);
578 desc
.control
^= DESC_VALID
;
579 memcpy_toio(desc_addr
, &desc
, sizeof(desc
));
581 /* update the tx number */
582 mod
->tx_num
= (desc
.control
& DESC_WRAP
) ? 0 : (mod
->tx_num
+ 1);
587 * LOCKING: must hold mod->lock
589 static int ican3_new_recv_msg(struct ican3_dev
*mod
, struct ican3_msg
*msg
)
591 struct ican3_new_desc desc
;
592 void __iomem
*desc_addr
= mod
->dpm
+ (mod
->rx_num
* sizeof(desc
));
594 /* switch to the tohost queue, and read the buffer descriptor */
595 ican3_set_page(mod
, QUEUE_TOHOST
);
596 memcpy_fromio(&desc
, desc_addr
, sizeof(desc
));
598 if (!(desc
.control
& DESC_VALID
)) {
599 netdev_dbg(mod
->ndev
, "%s: no buffers to recv\n", __func__
);
603 /* switch to the data page, copy the data */
604 ican3_set_page(mod
, desc
.pointer
);
605 memcpy_fromio(msg
, mod
->dpm
, sizeof(*msg
));
607 /* switch back to the descriptor, toggle the valid bit, write it back */
608 ican3_set_page(mod
, QUEUE_TOHOST
);
609 desc
.control
^= DESC_VALID
;
610 memcpy_toio(desc_addr
, &desc
, sizeof(desc
));
612 /* update the rx number */
613 mod
->rx_num
= (desc
.control
& DESC_WRAP
) ? 0 : (mod
->rx_num
+ 1);
618 * Message Send / Recv Helpers
621 static int ican3_send_msg(struct ican3_dev
*mod
, struct ican3_msg
*msg
)
626 spin_lock_irqsave(&mod
->lock
, flags
);
628 if (mod
->iftype
== 0)
629 ret
= ican3_old_send_msg(mod
, msg
);
631 ret
= ican3_new_send_msg(mod
, msg
);
633 spin_unlock_irqrestore(&mod
->lock
, flags
);
637 static int ican3_recv_msg(struct ican3_dev
*mod
, struct ican3_msg
*msg
)
642 spin_lock_irqsave(&mod
->lock
, flags
);
644 if (mod
->iftype
== 0)
645 ret
= ican3_old_recv_msg(mod
, msg
);
647 ret
= ican3_new_recv_msg(mod
, msg
);
649 spin_unlock_irqrestore(&mod
->lock
, flags
);
654 * Quick Pre-constructed Messages
657 static int ican3_msg_connect(struct ican3_dev
*mod
)
659 struct ican3_msg msg
;
661 memset(&msg
, 0, sizeof(msg
));
662 msg
.spec
= MSG_CONNECTI
;
663 msg
.len
= cpu_to_le16(0);
665 return ican3_send_msg(mod
, &msg
);
668 static int ican3_msg_disconnect(struct ican3_dev
*mod
)
670 struct ican3_msg msg
;
672 memset(&msg
, 0, sizeof(msg
));
673 msg
.spec
= MSG_DISCONNECT
;
674 msg
.len
= cpu_to_le16(0);
676 return ican3_send_msg(mod
, &msg
);
679 static int ican3_msg_newhostif(struct ican3_dev
*mod
)
681 struct ican3_msg msg
;
684 memset(&msg
, 0, sizeof(msg
));
685 msg
.spec
= MSG_NEWHOSTIF
;
686 msg
.len
= cpu_to_le16(0);
688 /* If we're not using the old interface, switching seems bogus */
689 WARN_ON(mod
->iftype
!= 0);
691 ret
= ican3_send_msg(mod
, &msg
);
695 /* mark the module as using the new host interface */
700 static int ican3_msg_fasthostif(struct ican3_dev
*mod
)
702 struct ican3_msg msg
;
705 memset(&msg
, 0, sizeof(msg
));
706 msg
.spec
= MSG_INITFDPMQUEUE
;
707 msg
.len
= cpu_to_le16(8);
709 /* write the tohost queue start address */
710 addr
= DPM_PAGE_ADDR(mod
->fastrx_start
);
711 msg
.data
[0] = addr
& 0xff;
712 msg
.data
[1] = (addr
>> 8) & 0xff;
713 msg
.data
[2] = (addr
>> 16) & 0xff;
714 msg
.data
[3] = (addr
>> 24) & 0xff;
716 /* write the fromhost queue start address */
717 addr
= DPM_PAGE_ADDR(mod
->fasttx_start
);
718 msg
.data
[4] = addr
& 0xff;
719 msg
.data
[5] = (addr
>> 8) & 0xff;
720 msg
.data
[6] = (addr
>> 16) & 0xff;
721 msg
.data
[7] = (addr
>> 24) & 0xff;
723 /* If we're not using the new interface yet, we cannot do this */
724 WARN_ON(mod
->iftype
!= 1);
726 return ican3_send_msg(mod
, &msg
);
730 * Setup the CAN filter to either accept or reject all
731 * messages from the CAN bus.
733 static int ican3_set_id_filter(struct ican3_dev
*mod
, bool accept
)
735 struct ican3_msg msg
;
738 /* Standard Frame Format */
739 memset(&msg
, 0, sizeof(msg
));
740 msg
.spec
= MSG_SETAFILMASK
;
741 msg
.len
= cpu_to_le16(5);
742 msg
.data
[0] = 0x00; /* IDLo LSB */
743 msg
.data
[1] = 0x00; /* IDLo MSB */
744 msg
.data
[2] = 0xff; /* IDHi LSB */
745 msg
.data
[3] = 0x07; /* IDHi MSB */
747 /* accept all frames for fast host if, or reject all frames */
748 msg
.data
[4] = accept
? SETAFILMASK_FASTIF
: SETAFILMASK_REJECT
;
750 ret
= ican3_send_msg(mod
, &msg
);
754 /* Extended Frame Format */
755 memset(&msg
, 0, sizeof(msg
));
756 msg
.spec
= MSG_SETAFILMASK
;
757 msg
.len
= cpu_to_le16(13);
758 msg
.data
[0] = 0; /* MUX = 0 */
759 msg
.data
[1] = 0x00; /* IDLo LSB */
762 msg
.data
[4] = 0x20; /* IDLo MSB */
763 msg
.data
[5] = 0xff; /* IDHi LSB */
766 msg
.data
[8] = 0x3f; /* IDHi MSB */
768 /* accept all frames for fast host if, or reject all frames */
769 msg
.data
[9] = accept
? SETAFILMASK_FASTIF
: SETAFILMASK_REJECT
;
771 return ican3_send_msg(mod
, &msg
);
775 * Bring the CAN bus online or offline
777 static int ican3_set_bus_state(struct ican3_dev
*mod
, bool on
)
779 struct can_bittiming
*bt
= &mod
->can
.bittiming
;
780 struct ican3_msg msg
;
784 /* This algorithm was stolen from drivers/net/can/sja1000/sja1000.c */
785 /* The bittiming register command for the ICAN3 just sets the bit timing */
786 /* registers on the SJA1000 chip directly */
787 btr0
= ((bt
->brp
- 1) & 0x3f) | (((bt
->sjw
- 1) & 0x3) << 6);
788 btr1
= ((bt
->prop_seg
+ bt
->phase_seg1
- 1) & 0xf) |
789 (((bt
->phase_seg2
- 1) & 0x7) << 4);
790 if (mod
->can
.ctrlmode
& CAN_CTRLMODE_3_SAMPLES
)
793 if (mod
->fwtype
== ICAN3_FWTYPE_ICANOS
) {
796 memset(&msg
, 0, sizeof(msg
));
797 msg
.spec
= MSG_CBTRREQ
;
798 msg
.len
= cpu_to_le16(4);
804 res
= ican3_send_msg(mod
, &msg
);
809 /* can-on/off request */
810 memset(&msg
, 0, sizeof(msg
));
811 msg
.spec
= on
? MSG_CONREQ
: MSG_COFFREQ
;
812 msg
.len
= cpu_to_le16(0);
814 return ican3_send_msg(mod
, &msg
);
816 } else if (mod
->fwtype
== ICAN3_FWTYPE_CAL_CANOPEN
) {
817 /* bittiming + can-on/off request */
818 memset(&msg
, 0, sizeof(msg
));
821 msg
.len
= cpu_to_le16(4);
822 msg
.data
[0] = LMTS_BUSON_REQ
;
827 msg
.len
= cpu_to_le16(2);
828 msg
.data
[0] = LMTS_BUSOFF_REQ
;
831 res
= ican3_send_msg(mod
, &msg
);
836 /* create NMT Slave Node for error processing
837 * class 2 (with error capability, see CiA/DS203-1)
839 * name locnod1 (must be exactly 7 bytes)
841 memset(&msg
, 0, sizeof(msg
));
843 msg
.len
= cpu_to_le16(11);
844 msg
.data
[0] = NMTS_CREATE_NODE_REQ
;
846 msg
.data
[2] = 2; /* node class */
847 msg
.data
[3] = 1; /* node id */
848 strcpy(msg
.data
+ 4, "locnod1"); /* node name */
849 return ican3_send_msg(mod
, &msg
);
856 static int ican3_set_termination(struct ican3_dev
*mod
, bool on
)
858 struct ican3_msg msg
;
860 memset(&msg
, 0, sizeof(msg
));
861 msg
.spec
= MSG_HWCONF
;
862 msg
.len
= cpu_to_le16(2);
864 msg
.data
[1] = on
? HWCONF_TERMINATE_ON
: HWCONF_TERMINATE_OFF
;
866 return ican3_send_msg(mod
, &msg
);
869 static int ican3_send_inquiry(struct ican3_dev
*mod
, u8 subspec
)
871 struct ican3_msg msg
;
873 memset(&msg
, 0, sizeof(msg
));
874 msg
.spec
= MSG_INQUIRY
;
875 msg
.len
= cpu_to_le16(2);
876 msg
.data
[0] = subspec
;
879 return ican3_send_msg(mod
, &msg
);
882 static int ican3_set_buserror(struct ican3_dev
*mod
, u8 quota
)
884 struct ican3_msg msg
;
886 if (mod
->fwtype
== ICAN3_FWTYPE_ICANOS
) {
887 memset(&msg
, 0, sizeof(msg
));
888 msg
.spec
= MSG_CCONFREQ
;
889 msg
.len
= cpu_to_le16(2);
892 } else if (mod
->fwtype
== ICAN3_FWTYPE_CAL_CANOPEN
) {
893 memset(&msg
, 0, sizeof(msg
));
895 msg
.len
= cpu_to_le16(4);
896 msg
.data
[0] = LMTS_CAN_CONF_REQ
;
903 return ican3_send_msg(mod
, &msg
);
907 * ICAN3 to Linux CAN Frame Conversion
910 static void ican3_to_can_frame(struct ican3_dev
*mod
,
911 struct ican3_fast_desc
*desc
,
912 struct can_frame
*cf
)
914 if ((desc
->command
& ICAN3_CAN_TYPE_MASK
) == ICAN3_CAN_TYPE_SFF
) {
915 if (desc
->data
[1] & ICAN3_SFF_RTR
)
916 cf
->can_id
|= CAN_RTR_FLAG
;
918 cf
->can_id
|= desc
->data
[0] << 3;
919 cf
->can_id
|= (desc
->data
[1] & 0xe0) >> 5;
920 cf
->len
= can_cc_dlc2len(desc
->data
[1] & ICAN3_CAN_DLC_MASK
);
921 memcpy(cf
->data
, &desc
->data
[2], cf
->len
);
923 cf
->len
= can_cc_dlc2len(desc
->data
[0] & ICAN3_CAN_DLC_MASK
);
924 if (desc
->data
[0] & ICAN3_EFF_RTR
)
925 cf
->can_id
|= CAN_RTR_FLAG
;
927 if (desc
->data
[0] & ICAN3_EFF
) {
928 cf
->can_id
|= CAN_EFF_FLAG
;
929 cf
->can_id
|= desc
->data
[2] << 21; /* 28-21 */
930 cf
->can_id
|= desc
->data
[3] << 13; /* 20-13 */
931 cf
->can_id
|= desc
->data
[4] << 5; /* 12-5 */
932 cf
->can_id
|= (desc
->data
[5] & 0xf8) >> 3;
934 cf
->can_id
|= desc
->data
[2] << 3; /* 10-3 */
935 cf
->can_id
|= desc
->data
[3] >> 5; /* 2-0 */
938 memcpy(cf
->data
, &desc
->data
[6], cf
->len
);
942 static void can_frame_to_ican3(struct ican3_dev
*mod
,
943 struct can_frame
*cf
,
944 struct ican3_fast_desc
*desc
)
946 /* clear out any stale data in the descriptor */
947 memset(desc
->data
, 0, sizeof(desc
->data
));
949 /* we always use the extended format, with the ECHO flag set */
950 desc
->command
= ICAN3_CAN_TYPE_EFF
;
951 desc
->data
[0] |= cf
->len
;
952 desc
->data
[1] |= ICAN3_ECHO
;
954 /* support single transmission (no retries) mode */
955 if (mod
->can
.ctrlmode
& CAN_CTRLMODE_ONE_SHOT
)
956 desc
->data
[1] |= ICAN3_SNGL
;
958 if (cf
->can_id
& CAN_RTR_FLAG
)
959 desc
->data
[0] |= ICAN3_EFF_RTR
;
961 /* pack the id into the correct places */
962 if (cf
->can_id
& CAN_EFF_FLAG
) {
963 desc
->data
[0] |= ICAN3_EFF
;
964 desc
->data
[2] = (cf
->can_id
& 0x1fe00000) >> 21; /* 28-21 */
965 desc
->data
[3] = (cf
->can_id
& 0x001fe000) >> 13; /* 20-13 */
966 desc
->data
[4] = (cf
->can_id
& 0x00001fe0) >> 5; /* 12-5 */
967 desc
->data
[5] = (cf
->can_id
& 0x0000001f) << 3; /* 4-0 */
969 desc
->data
[2] = (cf
->can_id
& 0x7F8) >> 3; /* bits 10-3 */
970 desc
->data
[3] = (cf
->can_id
& 0x007) << 5; /* bits 2-0 */
973 /* copy the data bits into the descriptor */
974 memcpy(&desc
->data
[6], cf
->data
, cf
->len
);
982 * Handle an ID + Version message response from the firmware. We never generate
983 * this message in production code, but it is very useful when debugging to be
984 * able to display this message.
986 static void ican3_handle_idvers(struct ican3_dev
*mod
, struct ican3_msg
*msg
)
988 netdev_dbg(mod
->ndev
, "IDVERS response: %s\n", msg
->data
);
991 static void ican3_handle_msglost(struct ican3_dev
*mod
, struct ican3_msg
*msg
)
993 struct net_device
*dev
= mod
->ndev
;
994 struct net_device_stats
*stats
= &dev
->stats
;
995 struct can_frame
*cf
;
999 * Report that communication messages with the microcontroller firmware
1000 * are being lost. These are never CAN frames, so we do not generate an
1001 * error frame for userspace
1003 if (msg
->spec
== MSG_MSGLOST
) {
1004 netdev_err(mod
->ndev
, "lost %d control messages\n", msg
->data
[0]);
1009 * Oops, this indicates that we have lost messages in the fast queue,
1010 * which are exclusively CAN messages. Our driver isn't reading CAN
1011 * frames fast enough.
1013 * We'll pretend that the SJA1000 told us that it ran out of buffer
1014 * space, because there is not a better message for this.
1016 skb
= alloc_can_err_skb(dev
, &cf
);
1018 cf
->can_id
|= CAN_ERR_CRTL
;
1019 cf
->data
[1] = CAN_ERR_CRTL_RX_OVERFLOW
;
1020 stats
->rx_over_errors
++;
1027 * Handle CAN Event Indication Messages from the firmware
1029 * The ICAN3 firmware provides the values of some SJA1000 registers when it
1030 * generates this message. The code below is largely copied from the
1031 * drivers/net/can/sja1000/sja1000.c file, and adapted as necessary
1033 static int ican3_handle_cevtind(struct ican3_dev
*mod
, struct ican3_msg
*msg
)
1035 struct net_device
*dev
= mod
->ndev
;
1036 struct net_device_stats
*stats
= &dev
->stats
;
1037 enum can_state state
= mod
->can
.state
;
1038 u8 isrc
, ecc
, status
, rxerr
, txerr
;
1039 struct can_frame
*cf
;
1040 struct sk_buff
*skb
;
1042 /* we can only handle the SJA1000 part */
1043 if (msg
->data
[1] != CEVTIND_CHIP_SJA1000
) {
1044 netdev_err(mod
->ndev
, "unable to handle errors on non-SJA1000\n");
1048 /* check the message length for sanity */
1049 if (le16_to_cpu(msg
->len
) < 6) {
1050 netdev_err(mod
->ndev
, "error message too short\n");
1054 isrc
= msg
->data
[0];
1056 status
= msg
->data
[3];
1057 rxerr
= msg
->data
[4];
1058 txerr
= msg
->data
[5];
1061 * This hardware lacks any support other than bus error messages to
1062 * determine if packet transmission has failed.
1064 * When TX errors happen, one echo skb needs to be dropped from the
1065 * front of the queue.
1067 * A small bit of code is duplicated here and below, to avoid error
1068 * skb allocation when it will just be freed immediately.
1070 if (isrc
== CEVTIND_BEI
) {
1072 netdev_dbg(mod
->ndev
, "bus error interrupt\n");
1075 if (!(ecc
& ECC_DIR
)) {
1076 kfree_skb(skb_dequeue(&mod
->echoq
));
1083 * The controller automatically disables bus-error interrupts
1084 * and therefore we must re-enable them.
1086 ret
= ican3_set_buserror(mod
, 1);
1088 netdev_err(mod
->ndev
, "unable to re-enable bus-error\n");
1092 /* bus error reporting is off, return immediately */
1093 if (!(mod
->can
.ctrlmode
& CAN_CTRLMODE_BERR_REPORTING
))
1097 skb
= alloc_can_err_skb(dev
, &cf
);
1101 /* data overrun interrupt */
1102 if (isrc
== CEVTIND_DOI
|| isrc
== CEVTIND_LOST
) {
1103 netdev_dbg(mod
->ndev
, "data overrun interrupt\n");
1104 cf
->can_id
|= CAN_ERR_CRTL
;
1105 cf
->data
[1] = CAN_ERR_CRTL_RX_OVERFLOW
;
1106 stats
->rx_over_errors
++;
1110 /* error warning + passive interrupt */
1111 if (isrc
== CEVTIND_EI
) {
1112 netdev_dbg(mod
->ndev
, "error warning + passive interrupt\n");
1113 if (status
& SR_BS
) {
1114 state
= CAN_STATE_BUS_OFF
;
1115 cf
->can_id
|= CAN_ERR_BUSOFF
;
1116 mod
->can
.can_stats
.bus_off
++;
1118 } else if (status
& SR_ES
) {
1119 if (rxerr
>= 128 || txerr
>= 128)
1120 state
= CAN_STATE_ERROR_PASSIVE
;
1122 state
= CAN_STATE_ERROR_WARNING
;
1124 state
= CAN_STATE_ERROR_ACTIVE
;
1128 /* bus error interrupt */
1129 if (isrc
== CEVTIND_BEI
) {
1130 mod
->can
.can_stats
.bus_error
++;
1131 cf
->can_id
|= CAN_ERR_PROT
| CAN_ERR_BUSERROR
| CAN_ERR_CNT
;
1133 switch (ecc
& ECC_MASK
) {
1135 cf
->data
[2] |= CAN_ERR_PROT_BIT
;
1138 cf
->data
[2] |= CAN_ERR_PROT_FORM
;
1141 cf
->data
[2] |= CAN_ERR_PROT_STUFF
;
1144 cf
->data
[3] = ecc
& ECC_SEG
;
1148 if (!(ecc
& ECC_DIR
))
1149 cf
->data
[2] |= CAN_ERR_PROT_TX
;
1151 cf
->data
[6] = txerr
;
1152 cf
->data
[7] = rxerr
;
1155 if (state
!= mod
->can
.state
&& (state
== CAN_STATE_ERROR_WARNING
||
1156 state
== CAN_STATE_ERROR_PASSIVE
)) {
1157 cf
->can_id
|= CAN_ERR_CRTL
| CAN_ERR_CNT
;
1158 if (state
== CAN_STATE_ERROR_WARNING
) {
1159 mod
->can
.can_stats
.error_warning
++;
1160 cf
->data
[1] = (txerr
> rxerr
) ?
1161 CAN_ERR_CRTL_TX_WARNING
:
1162 CAN_ERR_CRTL_RX_WARNING
;
1164 mod
->can
.can_stats
.error_passive
++;
1165 cf
->data
[1] = (txerr
> rxerr
) ?
1166 CAN_ERR_CRTL_TX_PASSIVE
:
1167 CAN_ERR_CRTL_RX_PASSIVE
;
1170 cf
->data
[6] = txerr
;
1171 cf
->data
[7] = rxerr
;
1174 mod
->can
.state
= state
;
1179 static void ican3_handle_inquiry(struct ican3_dev
*mod
, struct ican3_msg
*msg
)
1181 switch (msg
->data
[0]) {
1182 case INQUIRY_STATUS
:
1183 case INQUIRY_EXTENDED
:
1184 mod
->bec
.rxerr
= msg
->data
[5];
1185 mod
->bec
.txerr
= msg
->data
[6];
1186 complete(&mod
->buserror_comp
);
1188 case INQUIRY_TERMINATION
:
1189 mod
->termination_enabled
= msg
->data
[6] & HWCONF_TERMINATE_ON
;
1190 complete(&mod
->termination_comp
);
1193 netdev_err(mod
->ndev
, "received an unknown inquiry response\n");
1198 /* Handle NMTS Slave Event Indication Messages from the firmware */
1199 static void ican3_handle_nmtsind(struct ican3_dev
*mod
, struct ican3_msg
*msg
)
1203 subspec
= msg
->data
[0] + msg
->data
[1] * 0x100;
1204 if (subspec
== NMTS_SLAVE_EVENT_IND
) {
1205 switch (msg
->data
[2]) {
1206 case NE_LOCAL_OCCURRED
:
1207 case NE_LOCAL_RESOLVED
:
1208 /* now follows the same message as Raw ICANOS CEVTIND
1209 * shift the data at the same place and call this method
1211 le16_add_cpu(&msg
->len
, -3);
1212 memmove(msg
->data
, msg
->data
+ 3, le16_to_cpu(msg
->len
));
1213 ican3_handle_cevtind(mod
, msg
);
1215 case NE_REMOTE_OCCURRED
:
1216 case NE_REMOTE_RESOLVED
:
1217 /* should not occurre, ignore */
1220 netdev_warn(mod
->ndev
, "unknown NMTS event indication %x\n",
1224 } else if (subspec
== NMTS_SLAVE_STATE_IND
) {
1225 /* ignore state indications */
1227 netdev_warn(mod
->ndev
, "unhandled NMTS indication %x\n",
1233 static void ican3_handle_unknown_message(struct ican3_dev
*mod
,
1234 struct ican3_msg
*msg
)
1236 netdev_warn(mod
->ndev
, "received unknown message: spec 0x%.2x length %d\n",
1237 msg
->spec
, le16_to_cpu(msg
->len
));
1241 * Handle a control message from the firmware
1243 static void ican3_handle_message(struct ican3_dev
*mod
, struct ican3_msg
*msg
)
1245 netdev_dbg(mod
->ndev
, "%s: modno %d spec 0x%.2x len %d bytes\n", __func__
,
1246 mod
->num
, msg
->spec
, le16_to_cpu(msg
->len
));
1248 switch (msg
->spec
) {
1250 ican3_handle_idvers(mod
, msg
);
1254 ican3_handle_msglost(mod
, msg
);
1257 ican3_handle_cevtind(mod
, msg
);
1260 ican3_handle_inquiry(mod
, msg
);
1263 ican3_handle_nmtsind(mod
, msg
);
1266 ican3_handle_unknown_message(mod
, msg
);
1272 * The ican3 needs to store all echo skbs, and therefore cannot
1273 * use the generic infrastructure for this.
1275 static void ican3_put_echo_skb(struct ican3_dev
*mod
, struct sk_buff
*skb
)
1277 skb
= can_create_echo_skb(skb
);
1281 skb_tx_timestamp(skb
);
1283 /* save this skb for tx interrupt echo handling */
1284 skb_queue_tail(&mod
->echoq
, skb
);
1287 static unsigned int ican3_get_echo_skb(struct ican3_dev
*mod
)
1289 struct sk_buff
*skb
= skb_dequeue(&mod
->echoq
);
1290 struct can_frame
*cf
;
1293 /* this should never trigger unless there is a driver bug */
1295 netdev_err(mod
->ndev
, "BUG: echo skb not occupied\n");
1299 cf
= (struct can_frame
*)skb
->data
;
1300 if (!(cf
->can_id
& CAN_RTR_FLAG
))
1303 /* check flag whether this packet has to be looped back */
1304 if (skb
->pkt_type
!= PACKET_LOOPBACK
) {
1309 skb
->protocol
= htons(ETH_P_CAN
);
1310 skb
->pkt_type
= PACKET_BROADCAST
;
1311 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
1312 skb
->dev
= mod
->ndev
;
1313 netif_receive_skb(skb
);
1318 * Compare an skb with an existing echo skb
1320 * This function will be used on devices which have a hardware loopback.
1321 * On these devices, this function can be used to compare a received skb
1322 * with the saved echo skbs so that the hardware echo skb can be dropped.
1324 * Returns true if the skb's are identical, false otherwise.
1326 static bool ican3_echo_skb_matches(struct ican3_dev
*mod
, struct sk_buff
*skb
)
1328 struct can_frame
*cf
= (struct can_frame
*)skb
->data
;
1329 struct sk_buff
*echo_skb
= skb_peek(&mod
->echoq
);
1330 struct can_frame
*echo_cf
;
1335 echo_cf
= (struct can_frame
*)echo_skb
->data
;
1336 if (cf
->can_id
!= echo_cf
->can_id
)
1339 if (cf
->len
!= echo_cf
->len
)
1342 return memcmp(cf
->data
, echo_cf
->data
, cf
->len
) == 0;
1346 * Check that there is room in the TX ring to transmit another skb
1348 * LOCKING: must hold mod->lock
1350 static bool ican3_txok(struct ican3_dev
*mod
)
1352 struct ican3_fast_desc __iomem
*desc
;
1355 /* check that we have echo queue space */
1356 if (skb_queue_len(&mod
->echoq
) >= ICAN3_TX_BUFFERS
)
1359 /* copy the control bits of the descriptor */
1360 ican3_set_page(mod
, mod
->fasttx_start
+ (mod
->fasttx_num
/ 16));
1361 desc
= mod
->dpm
+ ((mod
->fasttx_num
% 16) * sizeof(*desc
));
1362 control
= ioread8(&desc
->control
);
1364 /* if the control bits are not valid, then we have no more space */
1365 if (!(control
& DESC_VALID
))
1372 * Receive one CAN frame from the hardware
1374 * CONTEXT: must be called from user context
1376 static int ican3_recv_skb(struct ican3_dev
*mod
)
1378 struct net_device
*ndev
= mod
->ndev
;
1379 struct net_device_stats
*stats
= &ndev
->stats
;
1380 struct ican3_fast_desc desc
;
1381 void __iomem
*desc_addr
;
1382 struct can_frame
*cf
;
1383 struct sk_buff
*skb
;
1384 unsigned long flags
;
1386 spin_lock_irqsave(&mod
->lock
, flags
);
1388 /* copy the whole descriptor */
1389 ican3_set_page(mod
, mod
->fastrx_start
+ (mod
->fastrx_num
/ 16));
1390 desc_addr
= mod
->dpm
+ ((mod
->fastrx_num
% 16) * sizeof(desc
));
1391 memcpy_fromio(&desc
, desc_addr
, sizeof(desc
));
1393 spin_unlock_irqrestore(&mod
->lock
, flags
);
1395 /* check that we actually have a CAN frame */
1396 if (!(desc
.control
& DESC_VALID
))
1399 /* allocate an skb */
1400 skb
= alloc_can_skb(ndev
, &cf
);
1401 if (unlikely(skb
== NULL
)) {
1402 stats
->rx_dropped
++;
1406 /* convert the ICAN3 frame into Linux CAN format */
1407 ican3_to_can_frame(mod
, &desc
, cf
);
1410 * If this is an ECHO frame received from the hardware loopback
1411 * feature, use the skb saved in the ECHO stack instead. This allows
1412 * the Linux CAN core to support CAN_RAW_RECV_OWN_MSGS correctly.
1414 * Since this is a confirmation of a successfully transmitted packet
1415 * sent from this host, update the transmit statistics.
1417 * Also, the netdevice queue needs to be allowed to send packets again.
1419 if (ican3_echo_skb_matches(mod
, skb
)) {
1420 stats
->tx_packets
++;
1421 stats
->tx_bytes
+= ican3_get_echo_skb(mod
);
1426 /* update statistics, receive the skb */
1427 stats
->rx_packets
++;
1428 if (!(cf
->can_id
& CAN_RTR_FLAG
))
1429 stats
->rx_bytes
+= cf
->len
;
1430 netif_receive_skb(skb
);
1433 /* toggle the valid bit and return the descriptor to the ring */
1434 desc
.control
^= DESC_VALID
;
1436 spin_lock_irqsave(&mod
->lock
, flags
);
1438 ican3_set_page(mod
, mod
->fastrx_start
+ (mod
->fastrx_num
/ 16));
1439 memcpy_toio(desc_addr
, &desc
, 1);
1441 /* update the next buffer pointer */
1442 mod
->fastrx_num
= (desc
.control
& DESC_WRAP
) ? 0
1443 : (mod
->fastrx_num
+ 1);
1445 /* there are still more buffers to process */
1446 spin_unlock_irqrestore(&mod
->lock
, flags
);
1450 static int ican3_napi(struct napi_struct
*napi
, int budget
)
1452 struct ican3_dev
*mod
= container_of(napi
, struct ican3_dev
, napi
);
1453 unsigned long flags
;
1457 /* process all communication messages */
1459 struct ican3_msg msg
;
1460 ret
= ican3_recv_msg(mod
, &msg
);
1464 ican3_handle_message(mod
, &msg
);
1467 /* process all CAN frames from the fast interface */
1468 while (received
< budget
) {
1469 ret
= ican3_recv_skb(mod
);
1476 /* We have processed all packets that the adapter had, but it
1477 * was less than our budget, stop polling */
1478 if (received
< budget
)
1479 napi_complete_done(napi
, received
);
1481 spin_lock_irqsave(&mod
->lock
, flags
);
1483 /* Wake up the transmit queue if necessary */
1484 if (netif_queue_stopped(mod
->ndev
) && ican3_txok(mod
))
1485 netif_wake_queue(mod
->ndev
);
1487 spin_unlock_irqrestore(&mod
->lock
, flags
);
1489 /* re-enable interrupt generation */
1490 iowrite8(1 << mod
->num
, &mod
->ctrl
->int_enable
);
1494 static irqreturn_t
ican3_irq(int irq
, void *dev_id
)
1496 struct ican3_dev
*mod
= dev_id
;
1500 * The interrupt status register on this device reports interrupts
1501 * as zeroes instead of using ones like most other devices
1503 stat
= ioread8(&mod
->ctrl
->int_disable
) & (1 << mod
->num
);
1504 if (stat
== (1 << mod
->num
))
1507 /* clear the MODULbus interrupt from the microcontroller */
1508 ioread8(&mod
->dpmctrl
->interrupt
);
1510 /* disable interrupt generation, schedule the NAPI poller */
1511 iowrite8(1 << mod
->num
, &mod
->ctrl
->int_disable
);
1512 napi_schedule(&mod
->napi
);
1517 * Firmware reset, startup, and shutdown
1521 * Reset an ICAN module to its power-on state
1523 * CONTEXT: no network device registered
1525 static int ican3_reset_module(struct ican3_dev
*mod
)
1527 unsigned long start
;
1530 /* disable interrupts so no more work is scheduled */
1531 iowrite8(1 << mod
->num
, &mod
->ctrl
->int_disable
);
1533 /* the first unallocated page in the DPM is #9 */
1534 mod
->free_page
= DPM_FREE_START
;
1536 ican3_set_page(mod
, QUEUE_OLD_CONTROL
);
1537 runold
= ioread8(mod
->dpm
+ TARGET_RUNNING
);
1539 /* reset the module */
1540 iowrite8(0x00, &mod
->dpmctrl
->hwreset
);
1542 /* wait until the module has finished resetting and is running */
1545 ican3_set_page(mod
, QUEUE_OLD_CONTROL
);
1546 runnew
= ioread8(mod
->dpm
+ TARGET_RUNNING
);
1547 if (runnew
== (runold
^ 0xff))
1551 } while (time_before(jiffies
, start
+ HZ
/ 2));
1553 netdev_err(mod
->ndev
, "failed to reset CAN module\n");
1557 static void ican3_shutdown_module(struct ican3_dev
*mod
)
1559 ican3_msg_disconnect(mod
);
1560 ican3_reset_module(mod
);
1564 * Startup an ICAN module, bringing it into fast mode
1566 static int ican3_startup_module(struct ican3_dev
*mod
)
1570 ret
= ican3_reset_module(mod
);
1572 netdev_err(mod
->ndev
, "unable to reset module\n");
1576 /* detect firmware */
1577 memcpy_fromio(mod
->fwinfo
, mod
->dpm
+ FIRMWARE_STAMP
, sizeof(mod
->fwinfo
) - 1);
1578 if (strncmp(mod
->fwinfo
, "JANZ-ICAN3", 10)) {
1579 netdev_err(mod
->ndev
, "ICAN3 not detected (found %s)\n", mod
->fwinfo
);
1582 if (strstr(mod
->fwinfo
, "CAL/CANopen"))
1583 mod
->fwtype
= ICAN3_FWTYPE_CAL_CANOPEN
;
1585 mod
->fwtype
= ICAN3_FWTYPE_ICANOS
;
1587 /* re-enable interrupts so we can send messages */
1588 iowrite8(1 << mod
->num
, &mod
->ctrl
->int_enable
);
1590 ret
= ican3_msg_connect(mod
);
1592 netdev_err(mod
->ndev
, "unable to connect to module\n");
1596 ican3_init_new_host_interface(mod
);
1597 ret
= ican3_msg_newhostif(mod
);
1599 netdev_err(mod
->ndev
, "unable to switch to new-style interface\n");
1603 /* default to "termination on" */
1604 ret
= ican3_set_termination(mod
, true);
1606 netdev_err(mod
->ndev
, "unable to enable termination\n");
1610 /* default to "bus errors enabled" */
1611 ret
= ican3_set_buserror(mod
, 1);
1613 netdev_err(mod
->ndev
, "unable to set bus-error\n");
1617 ican3_init_fast_host_interface(mod
);
1618 ret
= ican3_msg_fasthostif(mod
);
1620 netdev_err(mod
->ndev
, "unable to switch to fast host interface\n");
1624 ret
= ican3_set_id_filter(mod
, true);
1626 netdev_err(mod
->ndev
, "unable to set acceptance filter\n");
1634 * CAN Network Device
1637 static int ican3_open(struct net_device
*ndev
)
1639 struct ican3_dev
*mod
= netdev_priv(ndev
);
1642 /* open the CAN layer */
1643 ret
= open_candev(ndev
);
1645 netdev_err(mod
->ndev
, "unable to start CAN layer\n");
1649 /* bring the bus online */
1650 ret
= ican3_set_bus_state(mod
, true);
1652 netdev_err(mod
->ndev
, "unable to set bus-on\n");
1657 /* start up the network device */
1658 mod
->can
.state
= CAN_STATE_ERROR_ACTIVE
;
1659 netif_start_queue(ndev
);
1664 static int ican3_stop(struct net_device
*ndev
)
1666 struct ican3_dev
*mod
= netdev_priv(ndev
);
1669 /* stop the network device xmit routine */
1670 netif_stop_queue(ndev
);
1671 mod
->can
.state
= CAN_STATE_STOPPED
;
1673 /* bring the bus offline, stop receiving packets */
1674 ret
= ican3_set_bus_state(mod
, false);
1676 netdev_err(mod
->ndev
, "unable to set bus-off\n");
1680 /* drop all outstanding echo skbs */
1681 skb_queue_purge(&mod
->echoq
);
1683 /* close the CAN layer */
1688 static netdev_tx_t
ican3_xmit(struct sk_buff
*skb
, struct net_device
*ndev
)
1690 struct ican3_dev
*mod
= netdev_priv(ndev
);
1691 struct can_frame
*cf
= (struct can_frame
*)skb
->data
;
1692 struct ican3_fast_desc desc
;
1693 void __iomem
*desc_addr
;
1694 unsigned long flags
;
1696 if (can_dev_dropped_skb(ndev
, skb
))
1697 return NETDEV_TX_OK
;
1699 spin_lock_irqsave(&mod
->lock
, flags
);
1701 /* check that we can actually transmit */
1702 if (!ican3_txok(mod
)) {
1703 netdev_err(mod
->ndev
, "BUG: no free descriptors\n");
1704 spin_unlock_irqrestore(&mod
->lock
, flags
);
1705 return NETDEV_TX_BUSY
;
1708 /* copy the control bits of the descriptor */
1709 ican3_set_page(mod
, mod
->fasttx_start
+ (mod
->fasttx_num
/ 16));
1710 desc_addr
= mod
->dpm
+ ((mod
->fasttx_num
% 16) * sizeof(desc
));
1711 memset(&desc
, 0, sizeof(desc
));
1712 memcpy_fromio(&desc
, desc_addr
, 1);
1714 /* convert the Linux CAN frame into ICAN3 format */
1715 can_frame_to_ican3(mod
, cf
, &desc
);
1718 * This hardware doesn't have TX-done notifications, so we'll try and
1719 * emulate it the best we can using ECHO skbs. Add the skb to the ECHO
1720 * stack. Upon packet reception, check if the ECHO skb and received
1721 * skb match, and use that to wake the queue.
1723 ican3_put_echo_skb(mod
, skb
);
1726 * the programming manual says that you must set the IVALID bit, then
1727 * interrupt, then set the valid bit. Quite weird, but it seems to be
1728 * required for this to work
1730 desc
.control
|= DESC_IVALID
;
1731 memcpy_toio(desc_addr
, &desc
, sizeof(desc
));
1733 /* generate a MODULbus interrupt to the microcontroller */
1734 iowrite8(0x01, &mod
->dpmctrl
->interrupt
);
1736 desc
.control
^= DESC_VALID
;
1737 memcpy_toio(desc_addr
, &desc
, sizeof(desc
));
1739 /* update the next buffer pointer */
1740 mod
->fasttx_num
= (desc
.control
& DESC_WRAP
) ? 0
1741 : (mod
->fasttx_num
+ 1);
1743 /* if there is no free descriptor space, stop the transmit queue */
1744 if (!ican3_txok(mod
))
1745 netif_stop_queue(ndev
);
1747 spin_unlock_irqrestore(&mod
->lock
, flags
);
1748 return NETDEV_TX_OK
;
1751 static const struct net_device_ops ican3_netdev_ops
= {
1752 .ndo_open
= ican3_open
,
1753 .ndo_stop
= ican3_stop
,
1754 .ndo_start_xmit
= ican3_xmit
,
1755 .ndo_change_mtu
= can_change_mtu
,
1758 static const struct ethtool_ops ican3_ethtool_ops
= {
1759 .get_ts_info
= ethtool_op_get_ts_info
,
1763 * Low-level CAN Device
1766 /* This structure was stolen from drivers/net/can/sja1000/sja1000.c */
1767 static const struct can_bittiming_const ican3_bittiming_const
= {
1779 static int ican3_set_mode(struct net_device
*ndev
, enum can_mode mode
)
1781 struct ican3_dev
*mod
= netdev_priv(ndev
);
1784 if (mode
!= CAN_MODE_START
)
1787 /* bring the bus online */
1788 ret
= ican3_set_bus_state(mod
, true);
1790 netdev_err(ndev
, "unable to set bus-on\n");
1794 /* start up the network device */
1795 mod
->can
.state
= CAN_STATE_ERROR_ACTIVE
;
1797 if (netif_queue_stopped(ndev
))
1798 netif_wake_queue(ndev
);
1803 static int ican3_get_berr_counter(const struct net_device
*ndev
,
1804 struct can_berr_counter
*bec
)
1806 struct ican3_dev
*mod
= netdev_priv(ndev
);
1809 ret
= ican3_send_inquiry(mod
, INQUIRY_STATUS
);
1813 if (!wait_for_completion_timeout(&mod
->buserror_comp
, HZ
)) {
1814 netdev_info(mod
->ndev
, "%s timed out\n", __func__
);
1818 bec
->rxerr
= mod
->bec
.rxerr
;
1819 bec
->txerr
= mod
->bec
.txerr
;
1827 static ssize_t
termination_show(struct device
*dev
,
1828 struct device_attribute
*attr
,
1831 struct ican3_dev
*mod
= netdev_priv(to_net_dev(dev
));
1834 ret
= ican3_send_inquiry(mod
, INQUIRY_TERMINATION
);
1838 if (!wait_for_completion_timeout(&mod
->termination_comp
, HZ
)) {
1839 netdev_info(mod
->ndev
, "%s timed out\n", __func__
);
1843 return sysfs_emit(buf
, "%u\n", mod
->termination_enabled
);
1846 static ssize_t
termination_store(struct device
*dev
,
1847 struct device_attribute
*attr
,
1848 const char *buf
, size_t count
)
1850 struct ican3_dev
*mod
= netdev_priv(to_net_dev(dev
));
1851 unsigned long enable
;
1854 if (kstrtoul(buf
, 0, &enable
))
1857 ret
= ican3_set_termination(mod
, enable
);
1864 static ssize_t
fwinfo_show(struct device
*dev
,
1865 struct device_attribute
*attr
,
1868 struct ican3_dev
*mod
= netdev_priv(to_net_dev(dev
));
1870 return scnprintf(buf
, PAGE_SIZE
, "%s\n", mod
->fwinfo
);
1873 static DEVICE_ATTR_RW(termination
);
1874 static DEVICE_ATTR_RO(fwinfo
);
1876 static struct attribute
*ican3_sysfs_attrs
[] = {
1877 &dev_attr_termination
.attr
,
1878 &dev_attr_fwinfo
.attr
,
1882 static const struct attribute_group ican3_sysfs_attr_group
= {
1883 .attrs
= ican3_sysfs_attrs
,
1890 static int ican3_probe(struct platform_device
*pdev
)
1892 struct janz_platform_data
*pdata
;
1893 struct net_device
*ndev
;
1894 struct ican3_dev
*mod
;
1895 struct resource
*res
;
1899 pdata
= dev_get_platdata(&pdev
->dev
);
1903 dev_dbg(&pdev
->dev
, "probe: module number %d\n", pdata
->modno
);
1905 /* save the struct device for printing */
1908 /* allocate the CAN device and private data */
1909 ndev
= alloc_candev(sizeof(*mod
), 0);
1911 dev_err(dev
, "unable to allocate CANdev\n");
1916 platform_set_drvdata(pdev
, ndev
);
1917 mod
= netdev_priv(ndev
);
1919 mod
->num
= pdata
->modno
;
1920 netif_napi_add_weight(ndev
, &mod
->napi
, ican3_napi
, ICAN3_RX_BUFFERS
);
1921 skb_queue_head_init(&mod
->echoq
);
1922 spin_lock_init(&mod
->lock
);
1923 init_completion(&mod
->termination_comp
);
1924 init_completion(&mod
->buserror_comp
);
1926 /* setup device-specific sysfs attributes */
1927 ndev
->sysfs_groups
[0] = &ican3_sysfs_attr_group
;
1929 /* the first unallocated page in the DPM is 9 */
1930 mod
->free_page
= DPM_FREE_START
;
1932 ndev
->netdev_ops
= &ican3_netdev_ops
;
1933 ndev
->ethtool_ops
= &ican3_ethtool_ops
;
1934 ndev
->flags
|= IFF_ECHO
;
1935 SET_NETDEV_DEV(ndev
, &pdev
->dev
);
1937 mod
->can
.clock
.freq
= ICAN3_CAN_CLOCK
;
1938 mod
->can
.bittiming_const
= &ican3_bittiming_const
;
1939 mod
->can
.do_set_mode
= ican3_set_mode
;
1940 mod
->can
.do_get_berr_counter
= ican3_get_berr_counter
;
1941 mod
->can
.ctrlmode_supported
= CAN_CTRLMODE_3_SAMPLES
1942 | CAN_CTRLMODE_BERR_REPORTING
1943 | CAN_CTRLMODE_ONE_SHOT
;
1945 /* find our IRQ number */
1946 mod
->irq
= platform_get_irq(pdev
, 0);
1952 ndev
->irq
= mod
->irq
;
1954 /* get access to the MODULbus registers for this module */
1955 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1957 dev_err(dev
, "MODULbus registers not found\n");
1962 mod
->dpm
= ioremap(res
->start
, resource_size(res
));
1964 dev_err(dev
, "MODULbus registers not ioremap\n");
1969 mod
->dpmctrl
= mod
->dpm
+ DPM_PAGE_SIZE
;
1971 /* get access to the control registers for this module */
1972 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
1974 dev_err(dev
, "CONTROL registers not found\n");
1976 goto out_iounmap_dpm
;
1979 mod
->ctrl
= ioremap(res
->start
, resource_size(res
));
1981 dev_err(dev
, "CONTROL registers not ioremap\n");
1983 goto out_iounmap_dpm
;
1986 /* disable our IRQ, then hookup the IRQ handler */
1987 iowrite8(1 << mod
->num
, &mod
->ctrl
->int_disable
);
1988 ret
= request_irq(mod
->irq
, ican3_irq
, IRQF_SHARED
, DRV_NAME
, mod
);
1990 dev_err(dev
, "unable to request IRQ\n");
1991 goto out_iounmap_ctrl
;
1994 /* reset and initialize the CAN controller into fast mode */
1995 napi_enable(&mod
->napi
);
1996 ret
= ican3_startup_module(mod
);
1998 dev_err(dev
, "%s: unable to start CANdev\n", __func__
);
2002 /* register with the Linux CAN layer */
2003 ret
= register_candev(ndev
);
2005 dev_err(dev
, "%s: unable to register CANdev\n", __func__
);
2009 netdev_info(mod
->ndev
, "module %d: registered CAN device\n", pdata
->modno
);
2013 napi_disable(&mod
->napi
);
2014 iowrite8(1 << mod
->num
, &mod
->ctrl
->int_disable
);
2015 free_irq(mod
->irq
, mod
);
2026 static void ican3_remove(struct platform_device
*pdev
)
2028 struct net_device
*ndev
= platform_get_drvdata(pdev
);
2029 struct ican3_dev
*mod
= netdev_priv(ndev
);
2031 /* unregister the netdevice, stop interrupts */
2032 unregister_netdev(ndev
);
2033 napi_disable(&mod
->napi
);
2034 iowrite8(1 << mod
->num
, &mod
->ctrl
->int_disable
);
2035 free_irq(mod
->irq
, mod
);
2037 /* put the module into reset */
2038 ican3_shutdown_module(mod
);
2040 /* unmap all registers */
2047 static struct platform_driver ican3_driver
= {
2051 .probe
= ican3_probe
,
2052 .remove
= ican3_remove
,
2055 module_platform_driver(ican3_driver
);
2057 MODULE_AUTHOR("Ira W. Snyder <iws@ovro.caltech.edu>");
2058 MODULE_DESCRIPTION("Janz MODULbus VMOD-ICAN3 Driver");
2059 MODULE_LICENSE("GPL");
2060 MODULE_ALIAS("platform:janz-ican3");