2 * Janz MODULbus VMOD-ICAN3 CAN Interface Driver
4 * Copyright (c) 2010 Ira W. Snyder <iws@ovro.caltech.edu>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version.
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/interrupt.h>
15 #include <linux/delay.h>
16 #include <linux/platform_device.h>
18 #include <linux/netdevice.h>
19 #include <linux/can.h>
20 #include <linux/can/dev.h>
21 #include <linux/can/skb.h>
22 #include <linux/can/error.h>
24 #include <linux/mfd/janz.h>
27 /* the DPM has 64k of memory, organized into 256x 256 byte pages */
28 #define DPM_NUM_PAGES 256
29 #define DPM_PAGE_SIZE 256
30 #define DPM_PAGE_ADDR(p) ((p) * DPM_PAGE_SIZE)
32 /* JANZ ICAN3 "old-style" host interface queue page numbers */
33 #define QUEUE_OLD_CONTROL 0
34 #define QUEUE_OLD_RB0 1
35 #define QUEUE_OLD_RB1 2
36 #define QUEUE_OLD_WB0 3
37 #define QUEUE_OLD_WB1 4
39 /* Janz ICAN3 "old-style" host interface control registers */
40 #define MSYNC_PEER 0x00 /* ICAN only */
41 #define MSYNC_LOCL 0x01 /* host only */
42 #define TARGET_RUNNING 0x02
43 #define FIRMWARE_STAMP 0x60 /* big endian firmware stamp */
45 #define MSYNC_RB0 0x01
46 #define MSYNC_RB1 0x02
47 #define MSYNC_RBLW 0x04
48 #define MSYNC_RB_MASK (MSYNC_RB0 | MSYNC_RB1)
50 #define MSYNC_WB0 0x10
51 #define MSYNC_WB1 0x20
52 #define MSYNC_WBLW 0x40
53 #define MSYNC_WB_MASK (MSYNC_WB0 | MSYNC_WB1)
55 /* Janz ICAN3 "new-style" host interface queue page numbers */
56 #define QUEUE_TOHOST 5
57 #define QUEUE_FROMHOST_MID 6
58 #define QUEUE_FROMHOST_HIGH 7
59 #define QUEUE_FROMHOST_LOW 8
61 /* The first free page in the DPM is #9 */
62 #define DPM_FREE_START 9
64 /* Janz ICAN3 "new-style" and "fast" host interface descriptor flags */
65 #define DESC_VALID 0x80
66 #define DESC_WRAP 0x40
67 #define DESC_INTERRUPT 0x20
68 #define DESC_IVALID 0x10
69 #define DESC_LEN(len) (len)
71 /* Janz ICAN3 Firmware Messages */
72 #define MSG_CONNECTI 0x02
73 #define MSG_DISCONNECT 0x03
74 #define MSG_IDVERS 0x04
75 #define MSG_MSGLOST 0x05
76 #define MSG_NEWHOSTIF 0x08
77 #define MSG_INQUIRY 0x0a
78 #define MSG_SETAFILMASK 0x10
79 #define MSG_INITFDPMQUEUE 0x11
80 #define MSG_HWCONF 0x12
81 #define MSG_FMSGLOST 0x15
82 #define MSG_CEVTIND 0x37
83 #define MSG_CBTRREQ 0x41
84 #define MSG_COFFREQ 0x42
85 #define MSG_CONREQ 0x43
86 #define MSG_CCONFREQ 0x47
91 * Janz ICAN3 CAN Inquiry Message Types
93 * NOTE: there appears to be a firmware bug here. You must send
94 * NOTE: INQUIRY_STATUS and expect to receive an INQUIRY_EXTENDED
95 * NOTE: response. The controller never responds to a message with
96 * NOTE: the INQUIRY_EXTENDED subspec :(
98 #define INQUIRY_STATUS 0x00
99 #define INQUIRY_TERMINATION 0x01
100 #define INQUIRY_EXTENDED 0x04
102 /* Janz ICAN3 CAN Set Acceptance Filter Mask Message Types */
103 #define SETAFILMASK_REJECT 0x00
104 #define SETAFILMASK_FASTIF 0x02
106 /* Janz ICAN3 CAN Hardware Configuration Message Types */
107 #define HWCONF_TERMINATE_ON 0x01
108 #define HWCONF_TERMINATE_OFF 0x00
110 /* Janz ICAN3 CAN Event Indication Message Types */
111 #define CEVTIND_EI 0x01
112 #define CEVTIND_DOI 0x02
113 #define CEVTIND_LOST 0x04
114 #define CEVTIND_FULL 0x08
115 #define CEVTIND_BEI 0x10
117 #define CEVTIND_CHIP_SJA1000 0x02
119 #define ICAN3_BUSERR_QUOTA_MAX 255
121 /* Janz ICAN3 CAN Frame Conversion */
122 #define ICAN3_SNGL 0x02
123 #define ICAN3_ECHO 0x10
124 #define ICAN3_EFF_RTR 0x40
125 #define ICAN3_SFF_RTR 0x10
126 #define ICAN3_EFF 0x80
128 #define ICAN3_CAN_TYPE_MASK 0x0f
129 #define ICAN3_CAN_TYPE_SFF 0x00
130 #define ICAN3_CAN_TYPE_EFF 0x01
132 #define ICAN3_CAN_DLC_MASK 0x0f
134 /* Janz ICAN3 NMTS subtypes */
135 #define NMTS_CREATE_NODE_REQ 0x0
136 #define NMTS_SLAVE_STATE_IND 0x8
137 #define NMTS_SLAVE_EVENT_IND 0x9
139 /* Janz ICAN3 LMTS subtypes */
140 #define LMTS_BUSON_REQ 0x0
141 #define LMTS_BUSOFF_REQ 0x1
142 #define LMTS_CAN_CONF_REQ 0x2
144 /* Janz ICAN3 NMTS Event indications */
145 #define NE_LOCAL_OCCURRED 0x3
146 #define NE_LOCAL_RESOLVED 0x2
147 #define NE_REMOTE_OCCURRED 0xc
148 #define NE_REMOTE_RESOLVED 0x8
151 * SJA1000 Status and Error Register Definitions
153 * Copied from drivers/net/can/sja1000/sja1000.h
156 /* status register content */
166 #define SR_CRIT (SR_BS|SR_ES)
173 #define ECC_FORM 0x40
174 #define ECC_STUFF 0x80
175 #define ECC_MASK 0xc0
177 /* Number of buffers for use in the "new-style" host interface */
178 #define ICAN3_NEW_BUFFERS 16
180 /* Number of buffers for use in the "fast" host interface */
181 #define ICAN3_TX_BUFFERS 512
182 #define ICAN3_RX_BUFFERS 1024
184 /* SJA1000 Clock Input */
185 #define ICAN3_CAN_CLOCK 8000000
187 /* Janz ICAN3 firmware types */
190 ICAN3_FWTYPE_CAL_CANOPEN
,
194 #define DRV_NAME "janz-ican3"
196 /* DPM Control Registers -- starts at offset 0x100 in the MODULbus registers */
197 struct ican3_dpm_control
{
198 /* window address register */
203 * Read access: clear interrupt from microcontroller
204 * Write access: send interrupt to microcontroller
209 /* write-only: reset all hardware on the module */
213 /* write-only: generate an interrupt to the TPU */
219 /* must be the first member */
222 /* CAN network device */
223 struct net_device
*ndev
;
224 struct napi_struct napi
;
229 /* base address of registers and IRQ */
230 struct janz_cmodio_onboard_regs __iomem
*ctrl
;
231 struct ican3_dpm_control __iomem
*dpmctrl
;
235 /* CAN bus termination status */
236 struct completion termination_comp
;
237 bool termination_enabled
;
239 /* CAN bus error status registers */
240 struct completion buserror_comp
;
241 struct can_berr_counter bec
;
244 enum ican3_fwtype fwtype
;
247 /* old and new style host interface */
250 /* queue for echo packets */
251 struct sk_buff_head echoq
;
254 * Any function which changes the current DPM page must hold this
255 * lock while it is performing data accesses. This ensures that the
256 * function will not be preempted and end up reading data from a
257 * different DPM page than it expects.
261 /* new host interface */
266 /* fast host interface */
267 unsigned int fastrx_start
;
268 unsigned int fastrx_num
;
269 unsigned int fasttx_start
;
270 unsigned int fasttx_num
;
272 /* first free DPM page */
273 unsigned int free_page
;
283 struct ican3_new_desc
{
288 struct ican3_fast_desc
{
294 /* write to the window basic address register */
295 static inline void ican3_set_page(struct ican3_dev
*mod
, unsigned int page
)
297 BUG_ON(page
>= DPM_NUM_PAGES
);
298 iowrite8(page
, &mod
->dpmctrl
->window_address
);
302 * ICAN3 "old-style" host interface
306 * Receive a message from the ICAN3 "old-style" firmware interface
308 * LOCKING: must hold mod->lock
310 * returns 0 on success, -ENOMEM when no message exists
312 static int ican3_old_recv_msg(struct ican3_dev
*mod
, struct ican3_msg
*msg
)
314 unsigned int mbox
, mbox_page
;
317 /* get the MSYNC registers */
318 ican3_set_page(mod
, QUEUE_OLD_CONTROL
);
319 peer
= ioread8(mod
->dpm
+ MSYNC_PEER
);
320 locl
= ioread8(mod
->dpm
+ MSYNC_LOCL
);
323 if ((xord
& MSYNC_RB_MASK
) == 0x00) {
324 netdev_dbg(mod
->ndev
, "no mbox for reading\n");
328 /* find the first free mbox to read */
329 if ((xord
& MSYNC_RB_MASK
) == MSYNC_RB_MASK
)
330 mbox
= (xord
& MSYNC_RBLW
) ? MSYNC_RB0
: MSYNC_RB1
;
332 mbox
= (xord
& MSYNC_RB0
) ? MSYNC_RB0
: MSYNC_RB1
;
334 /* copy the message */
335 mbox_page
= (mbox
== MSYNC_RB0
) ? QUEUE_OLD_RB0
: QUEUE_OLD_RB1
;
336 ican3_set_page(mod
, mbox_page
);
337 memcpy_fromio(msg
, mod
->dpm
, sizeof(*msg
));
340 * notify the firmware that the read buffer is available
341 * for it to fill again
345 ican3_set_page(mod
, QUEUE_OLD_CONTROL
);
346 iowrite8(locl
, mod
->dpm
+ MSYNC_LOCL
);
351 * Send a message through the "old-style" firmware interface
353 * LOCKING: must hold mod->lock
355 * returns 0 on success, -ENOMEM when no free space exists
357 static int ican3_old_send_msg(struct ican3_dev
*mod
, struct ican3_msg
*msg
)
359 unsigned int mbox
, mbox_page
;
362 /* get the MSYNC registers */
363 ican3_set_page(mod
, QUEUE_OLD_CONTROL
);
364 peer
= ioread8(mod
->dpm
+ MSYNC_PEER
);
365 locl
= ioread8(mod
->dpm
+ MSYNC_LOCL
);
368 if ((xord
& MSYNC_WB_MASK
) == MSYNC_WB_MASK
) {
369 netdev_err(mod
->ndev
, "no mbox for writing\n");
373 /* calculate a free mbox to use */
374 mbox
= (xord
& MSYNC_WB0
) ? MSYNC_WB1
: MSYNC_WB0
;
376 /* copy the message to the DPM */
377 mbox_page
= (mbox
== MSYNC_WB0
) ? QUEUE_OLD_WB0
: QUEUE_OLD_WB1
;
378 ican3_set_page(mod
, mbox_page
);
379 memcpy_toio(mod
->dpm
, msg
, sizeof(*msg
));
382 if (mbox
== MSYNC_WB1
)
385 ican3_set_page(mod
, QUEUE_OLD_CONTROL
);
386 iowrite8(locl
, mod
->dpm
+ MSYNC_LOCL
);
391 * ICAN3 "new-style" Host Interface Setup
394 static void ican3_init_new_host_interface(struct ican3_dev
*mod
)
396 struct ican3_new_desc desc
;
401 spin_lock_irqsave(&mod
->lock
, flags
);
403 /* setup the internal datastructures for RX */
407 /* tohost queue descriptors are in page 5 */
408 ican3_set_page(mod
, QUEUE_TOHOST
);
411 /* initialize the tohost (rx) queue descriptors: pages 9-24 */
412 for (i
= 0; i
< ICAN3_NEW_BUFFERS
; i
++) {
413 desc
.control
= DESC_INTERRUPT
| DESC_LEN(1); /* I L=1 */
414 desc
.pointer
= mod
->free_page
;
416 /* set wrap flag on last buffer */
417 if (i
== ICAN3_NEW_BUFFERS
- 1)
418 desc
.control
|= DESC_WRAP
;
420 memcpy_toio(dst
, &desc
, sizeof(desc
));
425 /* fromhost (tx) mid queue descriptors are in page 6 */
426 ican3_set_page(mod
, QUEUE_FROMHOST_MID
);
429 /* setup the internal datastructures for TX */
432 /* initialize the fromhost mid queue descriptors: pages 25-40 */
433 for (i
= 0; i
< ICAN3_NEW_BUFFERS
; i
++) {
434 desc
.control
= DESC_VALID
| DESC_LEN(1); /* V L=1 */
435 desc
.pointer
= mod
->free_page
;
437 /* set wrap flag on last buffer */
438 if (i
== ICAN3_NEW_BUFFERS
- 1)
439 desc
.control
|= DESC_WRAP
;
441 memcpy_toio(dst
, &desc
, sizeof(desc
));
446 /* fromhost hi queue descriptors are in page 7 */
447 ican3_set_page(mod
, QUEUE_FROMHOST_HIGH
);
450 /* initialize only a single buffer in the fromhost hi queue (unused) */
451 desc
.control
= DESC_VALID
| DESC_WRAP
| DESC_LEN(1); /* VW L=1 */
452 desc
.pointer
= mod
->free_page
;
453 memcpy_toio(dst
, &desc
, sizeof(desc
));
456 /* fromhost low queue descriptors are in page 8 */
457 ican3_set_page(mod
, QUEUE_FROMHOST_LOW
);
460 /* initialize only a single buffer in the fromhost low queue (unused) */
461 desc
.control
= DESC_VALID
| DESC_WRAP
| DESC_LEN(1); /* VW L=1 */
462 desc
.pointer
= mod
->free_page
;
463 memcpy_toio(dst
, &desc
, sizeof(desc
));
466 spin_unlock_irqrestore(&mod
->lock
, flags
);
470 * ICAN3 Fast Host Interface Setup
473 static void ican3_init_fast_host_interface(struct ican3_dev
*mod
)
475 struct ican3_fast_desc desc
;
481 spin_lock_irqsave(&mod
->lock
, flags
);
483 /* save the start recv page */
484 mod
->fastrx_start
= mod
->free_page
;
487 /* build a single fast tohost queue descriptor */
488 memset(&desc
, 0, sizeof(desc
));
492 /* build the tohost queue descriptor ring in memory */
494 for (i
= 0; i
< ICAN3_RX_BUFFERS
; i
++) {
496 /* set the wrap bit on the last buffer */
497 if (i
== ICAN3_RX_BUFFERS
- 1)
498 desc
.control
|= DESC_WRAP
;
500 /* switch to the correct page */
501 ican3_set_page(mod
, mod
->free_page
);
503 /* copy the descriptor to the DPM */
504 dst
= mod
->dpm
+ addr
;
505 memcpy_toio(dst
, &desc
, sizeof(desc
));
506 addr
+= sizeof(desc
);
508 /* move to the next page if necessary */
509 if (addr
>= DPM_PAGE_SIZE
) {
515 /* make sure we page-align the next queue */
519 /* save the start xmit page */
520 mod
->fasttx_start
= mod
->free_page
;
523 /* build a single fast fromhost queue descriptor */
524 memset(&desc
, 0, sizeof(desc
));
525 desc
.control
= DESC_VALID
;
528 /* build the fromhost queue descriptor ring in memory */
530 for (i
= 0; i
< ICAN3_TX_BUFFERS
; i
++) {
532 /* set the wrap bit on the last buffer */
533 if (i
== ICAN3_TX_BUFFERS
- 1)
534 desc
.control
|= DESC_WRAP
;
536 /* switch to the correct page */
537 ican3_set_page(mod
, mod
->free_page
);
539 /* copy the descriptor to the DPM */
540 dst
= mod
->dpm
+ addr
;
541 memcpy_toio(dst
, &desc
, sizeof(desc
));
542 addr
+= sizeof(desc
);
544 /* move to the next page if necessary */
545 if (addr
>= DPM_PAGE_SIZE
) {
551 spin_unlock_irqrestore(&mod
->lock
, flags
);
555 * ICAN3 "new-style" Host Interface Message Helpers
559 * LOCKING: must hold mod->lock
561 static int ican3_new_send_msg(struct ican3_dev
*mod
, struct ican3_msg
*msg
)
563 struct ican3_new_desc desc
;
564 void __iomem
*desc_addr
= mod
->dpm
+ (mod
->tx_num
* sizeof(desc
));
566 /* switch to the fromhost mid queue, and read the buffer descriptor */
567 ican3_set_page(mod
, QUEUE_FROMHOST_MID
);
568 memcpy_fromio(&desc
, desc_addr
, sizeof(desc
));
570 if (!(desc
.control
& DESC_VALID
)) {
571 netdev_dbg(mod
->ndev
, "%s: no free buffers\n", __func__
);
575 /* switch to the data page, copy the data */
576 ican3_set_page(mod
, desc
.pointer
);
577 memcpy_toio(mod
->dpm
, msg
, sizeof(*msg
));
579 /* switch back to the descriptor, set the valid bit, write it back */
580 ican3_set_page(mod
, QUEUE_FROMHOST_MID
);
581 desc
.control
^= DESC_VALID
;
582 memcpy_toio(desc_addr
, &desc
, sizeof(desc
));
584 /* update the tx number */
585 mod
->tx_num
= (desc
.control
& DESC_WRAP
) ? 0 : (mod
->tx_num
+ 1);
590 * LOCKING: must hold mod->lock
592 static int ican3_new_recv_msg(struct ican3_dev
*mod
, struct ican3_msg
*msg
)
594 struct ican3_new_desc desc
;
595 void __iomem
*desc_addr
= mod
->dpm
+ (mod
->rx_num
* sizeof(desc
));
597 /* switch to the tohost queue, and read the buffer descriptor */
598 ican3_set_page(mod
, QUEUE_TOHOST
);
599 memcpy_fromio(&desc
, desc_addr
, sizeof(desc
));
601 if (!(desc
.control
& DESC_VALID
)) {
602 netdev_dbg(mod
->ndev
, "%s: no buffers to recv\n", __func__
);
606 /* switch to the data page, copy the data */
607 ican3_set_page(mod
, desc
.pointer
);
608 memcpy_fromio(msg
, mod
->dpm
, sizeof(*msg
));
610 /* switch back to the descriptor, toggle the valid bit, write it back */
611 ican3_set_page(mod
, QUEUE_TOHOST
);
612 desc
.control
^= DESC_VALID
;
613 memcpy_toio(desc_addr
, &desc
, sizeof(desc
));
615 /* update the rx number */
616 mod
->rx_num
= (desc
.control
& DESC_WRAP
) ? 0 : (mod
->rx_num
+ 1);
621 * Message Send / Recv Helpers
624 static int ican3_send_msg(struct ican3_dev
*mod
, struct ican3_msg
*msg
)
629 spin_lock_irqsave(&mod
->lock
, flags
);
631 if (mod
->iftype
== 0)
632 ret
= ican3_old_send_msg(mod
, msg
);
634 ret
= ican3_new_send_msg(mod
, msg
);
636 spin_unlock_irqrestore(&mod
->lock
, flags
);
640 static int ican3_recv_msg(struct ican3_dev
*mod
, struct ican3_msg
*msg
)
645 spin_lock_irqsave(&mod
->lock
, flags
);
647 if (mod
->iftype
== 0)
648 ret
= ican3_old_recv_msg(mod
, msg
);
650 ret
= ican3_new_recv_msg(mod
, msg
);
652 spin_unlock_irqrestore(&mod
->lock
, flags
);
657 * Quick Pre-constructed Messages
660 static int ican3_msg_connect(struct ican3_dev
*mod
)
662 struct ican3_msg msg
;
664 memset(&msg
, 0, sizeof(msg
));
665 msg
.spec
= MSG_CONNECTI
;
666 msg
.len
= cpu_to_le16(0);
668 return ican3_send_msg(mod
, &msg
);
671 static int ican3_msg_disconnect(struct ican3_dev
*mod
)
673 struct ican3_msg msg
;
675 memset(&msg
, 0, sizeof(msg
));
676 msg
.spec
= MSG_DISCONNECT
;
677 msg
.len
= cpu_to_le16(0);
679 return ican3_send_msg(mod
, &msg
);
682 static int ican3_msg_newhostif(struct ican3_dev
*mod
)
684 struct ican3_msg msg
;
687 memset(&msg
, 0, sizeof(msg
));
688 msg
.spec
= MSG_NEWHOSTIF
;
689 msg
.len
= cpu_to_le16(0);
691 /* If we're not using the old interface, switching seems bogus */
692 WARN_ON(mod
->iftype
!= 0);
694 ret
= ican3_send_msg(mod
, &msg
);
698 /* mark the module as using the new host interface */
703 static int ican3_msg_fasthostif(struct ican3_dev
*mod
)
705 struct ican3_msg msg
;
708 memset(&msg
, 0, sizeof(msg
));
709 msg
.spec
= MSG_INITFDPMQUEUE
;
710 msg
.len
= cpu_to_le16(8);
712 /* write the tohost queue start address */
713 addr
= DPM_PAGE_ADDR(mod
->fastrx_start
);
714 msg
.data
[0] = addr
& 0xff;
715 msg
.data
[1] = (addr
>> 8) & 0xff;
716 msg
.data
[2] = (addr
>> 16) & 0xff;
717 msg
.data
[3] = (addr
>> 24) & 0xff;
719 /* write the fromhost queue start address */
720 addr
= DPM_PAGE_ADDR(mod
->fasttx_start
);
721 msg
.data
[4] = addr
& 0xff;
722 msg
.data
[5] = (addr
>> 8) & 0xff;
723 msg
.data
[6] = (addr
>> 16) & 0xff;
724 msg
.data
[7] = (addr
>> 24) & 0xff;
726 /* If we're not using the new interface yet, we cannot do this */
727 WARN_ON(mod
->iftype
!= 1);
729 return ican3_send_msg(mod
, &msg
);
733 * Setup the CAN filter to either accept or reject all
734 * messages from the CAN bus.
736 static int ican3_set_id_filter(struct ican3_dev
*mod
, bool accept
)
738 struct ican3_msg msg
;
741 /* Standard Frame Format */
742 memset(&msg
, 0, sizeof(msg
));
743 msg
.spec
= MSG_SETAFILMASK
;
744 msg
.len
= cpu_to_le16(5);
745 msg
.data
[0] = 0x00; /* IDLo LSB */
746 msg
.data
[1] = 0x00; /* IDLo MSB */
747 msg
.data
[2] = 0xff; /* IDHi LSB */
748 msg
.data
[3] = 0x07; /* IDHi MSB */
750 /* accept all frames for fast host if, or reject all frames */
751 msg
.data
[4] = accept
? SETAFILMASK_FASTIF
: SETAFILMASK_REJECT
;
753 ret
= ican3_send_msg(mod
, &msg
);
757 /* Extended Frame Format */
758 memset(&msg
, 0, sizeof(msg
));
759 msg
.spec
= MSG_SETAFILMASK
;
760 msg
.len
= cpu_to_le16(13);
761 msg
.data
[0] = 0; /* MUX = 0 */
762 msg
.data
[1] = 0x00; /* IDLo LSB */
765 msg
.data
[4] = 0x20; /* IDLo MSB */
766 msg
.data
[5] = 0xff; /* IDHi LSB */
769 msg
.data
[8] = 0x3f; /* IDHi MSB */
771 /* accept all frames for fast host if, or reject all frames */
772 msg
.data
[9] = accept
? SETAFILMASK_FASTIF
: SETAFILMASK_REJECT
;
774 return ican3_send_msg(mod
, &msg
);
778 * Bring the CAN bus online or offline
780 static int ican3_set_bus_state(struct ican3_dev
*mod
, bool on
)
782 struct can_bittiming
*bt
= &mod
->can
.bittiming
;
783 struct ican3_msg msg
;
787 /* This algorithm was stolen from drivers/net/can/sja1000/sja1000.c */
788 /* The bittiming register command for the ICAN3 just sets the bit timing */
789 /* registers on the SJA1000 chip directly */
790 btr0
= ((bt
->brp
- 1) & 0x3f) | (((bt
->sjw
- 1) & 0x3) << 6);
791 btr1
= ((bt
->prop_seg
+ bt
->phase_seg1
- 1) & 0xf) |
792 (((bt
->phase_seg2
- 1) & 0x7) << 4);
793 if (mod
->can
.ctrlmode
& CAN_CTRLMODE_3_SAMPLES
)
796 if (mod
->fwtype
== ICAN3_FWTYPE_ICANOS
) {
799 memset(&msg
, 0, sizeof(msg
));
800 msg
.spec
= MSG_CBTRREQ
;
801 msg
.len
= cpu_to_le16(4);
807 res
= ican3_send_msg(mod
, &msg
);
812 /* can-on/off request */
813 memset(&msg
, 0, sizeof(msg
));
814 msg
.spec
= on
? MSG_CONREQ
: MSG_COFFREQ
;
815 msg
.len
= cpu_to_le16(0);
817 return ican3_send_msg(mod
, &msg
);
819 } else if (mod
->fwtype
== ICAN3_FWTYPE_CAL_CANOPEN
) {
820 /* bittiming + can-on/off request */
821 memset(&msg
, 0, sizeof(msg
));
824 msg
.len
= cpu_to_le16(4);
825 msg
.data
[0] = LMTS_BUSON_REQ
;
830 msg
.len
= cpu_to_le16(2);
831 msg
.data
[0] = LMTS_BUSOFF_REQ
;
834 res
= ican3_send_msg(mod
, &msg
);
839 /* create NMT Slave Node for error processing
840 * class 2 (with error capability, see CiA/DS203-1)
842 * name locnod1 (must be exactly 7 bytes)
844 memset(&msg
, 0, sizeof(msg
));
846 msg
.len
= cpu_to_le16(11);
847 msg
.data
[0] = NMTS_CREATE_NODE_REQ
;
849 msg
.data
[2] = 2; /* node class */
850 msg
.data
[3] = 1; /* node id */
851 strcpy(msg
.data
+ 4, "locnod1"); /* node name */
852 return ican3_send_msg(mod
, &msg
);
859 static int ican3_set_termination(struct ican3_dev
*mod
, bool on
)
861 struct ican3_msg msg
;
863 memset(&msg
, 0, sizeof(msg
));
864 msg
.spec
= MSG_HWCONF
;
865 msg
.len
= cpu_to_le16(2);
867 msg
.data
[1] = on
? HWCONF_TERMINATE_ON
: HWCONF_TERMINATE_OFF
;
869 return ican3_send_msg(mod
, &msg
);
872 static int ican3_send_inquiry(struct ican3_dev
*mod
, u8 subspec
)
874 struct ican3_msg msg
;
876 memset(&msg
, 0, sizeof(msg
));
877 msg
.spec
= MSG_INQUIRY
;
878 msg
.len
= cpu_to_le16(2);
879 msg
.data
[0] = subspec
;
882 return ican3_send_msg(mod
, &msg
);
885 static int ican3_set_buserror(struct ican3_dev
*mod
, u8 quota
)
887 struct ican3_msg msg
;
889 if (mod
->fwtype
== ICAN3_FWTYPE_ICANOS
) {
890 memset(&msg
, 0, sizeof(msg
));
891 msg
.spec
= MSG_CCONFREQ
;
892 msg
.len
= cpu_to_le16(2);
895 } else if (mod
->fwtype
== ICAN3_FWTYPE_CAL_CANOPEN
) {
896 memset(&msg
, 0, sizeof(msg
));
898 msg
.len
= cpu_to_le16(4);
899 msg
.data
[0] = LMTS_CAN_CONF_REQ
;
906 return ican3_send_msg(mod
, &msg
);
910 * ICAN3 to Linux CAN Frame Conversion
913 static void ican3_to_can_frame(struct ican3_dev
*mod
,
914 struct ican3_fast_desc
*desc
,
915 struct can_frame
*cf
)
917 if ((desc
->command
& ICAN3_CAN_TYPE_MASK
) == ICAN3_CAN_TYPE_SFF
) {
918 if (desc
->data
[1] & ICAN3_SFF_RTR
)
919 cf
->can_id
|= CAN_RTR_FLAG
;
921 cf
->can_id
|= desc
->data
[0] << 3;
922 cf
->can_id
|= (desc
->data
[1] & 0xe0) >> 5;
923 cf
->can_dlc
= get_can_dlc(desc
->data
[1] & ICAN3_CAN_DLC_MASK
);
924 memcpy(cf
->data
, &desc
->data
[2], cf
->can_dlc
);
926 cf
->can_dlc
= get_can_dlc(desc
->data
[0] & ICAN3_CAN_DLC_MASK
);
927 if (desc
->data
[0] & ICAN3_EFF_RTR
)
928 cf
->can_id
|= CAN_RTR_FLAG
;
930 if (desc
->data
[0] & ICAN3_EFF
) {
931 cf
->can_id
|= CAN_EFF_FLAG
;
932 cf
->can_id
|= desc
->data
[2] << 21; /* 28-21 */
933 cf
->can_id
|= desc
->data
[3] << 13; /* 20-13 */
934 cf
->can_id
|= desc
->data
[4] << 5; /* 12-5 */
935 cf
->can_id
|= (desc
->data
[5] & 0xf8) >> 3;
937 cf
->can_id
|= desc
->data
[2] << 3; /* 10-3 */
938 cf
->can_id
|= desc
->data
[3] >> 5; /* 2-0 */
941 memcpy(cf
->data
, &desc
->data
[6], cf
->can_dlc
);
945 static void can_frame_to_ican3(struct ican3_dev
*mod
,
946 struct can_frame
*cf
,
947 struct ican3_fast_desc
*desc
)
949 /* clear out any stale data in the descriptor */
950 memset(desc
->data
, 0, sizeof(desc
->data
));
952 /* we always use the extended format, with the ECHO flag set */
953 desc
->command
= ICAN3_CAN_TYPE_EFF
;
954 desc
->data
[0] |= cf
->can_dlc
;
955 desc
->data
[1] |= ICAN3_ECHO
;
957 /* support single transmission (no retries) mode */
958 if (mod
->can
.ctrlmode
& CAN_CTRLMODE_ONE_SHOT
)
959 desc
->data
[1] |= ICAN3_SNGL
;
961 if (cf
->can_id
& CAN_RTR_FLAG
)
962 desc
->data
[0] |= ICAN3_EFF_RTR
;
964 /* pack the id into the correct places */
965 if (cf
->can_id
& CAN_EFF_FLAG
) {
966 desc
->data
[0] |= ICAN3_EFF
;
967 desc
->data
[2] = (cf
->can_id
& 0x1fe00000) >> 21; /* 28-21 */
968 desc
->data
[3] = (cf
->can_id
& 0x001fe000) >> 13; /* 20-13 */
969 desc
->data
[4] = (cf
->can_id
& 0x00001fe0) >> 5; /* 12-5 */
970 desc
->data
[5] = (cf
->can_id
& 0x0000001f) << 3; /* 4-0 */
972 desc
->data
[2] = (cf
->can_id
& 0x7F8) >> 3; /* bits 10-3 */
973 desc
->data
[3] = (cf
->can_id
& 0x007) << 5; /* bits 2-0 */
976 /* copy the data bits into the descriptor */
977 memcpy(&desc
->data
[6], cf
->data
, cf
->can_dlc
);
985 * Handle an ID + Version message response from the firmware. We never generate
986 * this message in production code, but it is very useful when debugging to be
987 * able to display this message.
989 static void ican3_handle_idvers(struct ican3_dev
*mod
, struct ican3_msg
*msg
)
991 netdev_dbg(mod
->ndev
, "IDVERS response: %s\n", msg
->data
);
994 static void ican3_handle_msglost(struct ican3_dev
*mod
, struct ican3_msg
*msg
)
996 struct net_device
*dev
= mod
->ndev
;
997 struct net_device_stats
*stats
= &dev
->stats
;
998 struct can_frame
*cf
;
1002 * Report that communication messages with the microcontroller firmware
1003 * are being lost. These are never CAN frames, so we do not generate an
1004 * error frame for userspace
1006 if (msg
->spec
== MSG_MSGLOST
) {
1007 netdev_err(mod
->ndev
, "lost %d control messages\n", msg
->data
[0]);
1012 * Oops, this indicates that we have lost messages in the fast queue,
1013 * which are exclusively CAN messages. Our driver isn't reading CAN
1014 * frames fast enough.
1016 * We'll pretend that the SJA1000 told us that it ran out of buffer
1017 * space, because there is not a better message for this.
1019 skb
= alloc_can_err_skb(dev
, &cf
);
1021 cf
->can_id
|= CAN_ERR_CRTL
;
1022 cf
->data
[1] = CAN_ERR_CRTL_RX_OVERFLOW
;
1023 stats
->rx_over_errors
++;
1030 * Handle CAN Event Indication Messages from the firmware
1032 * The ICAN3 firmware provides the values of some SJA1000 registers when it
1033 * generates this message. The code below is largely copied from the
1034 * drivers/net/can/sja1000/sja1000.c file, and adapted as necessary
1036 static int ican3_handle_cevtind(struct ican3_dev
*mod
, struct ican3_msg
*msg
)
1038 struct net_device
*dev
= mod
->ndev
;
1039 struct net_device_stats
*stats
= &dev
->stats
;
1040 enum can_state state
= mod
->can
.state
;
1041 u8 isrc
, ecc
, status
, rxerr
, txerr
;
1042 struct can_frame
*cf
;
1043 struct sk_buff
*skb
;
1045 /* we can only handle the SJA1000 part */
1046 if (msg
->data
[1] != CEVTIND_CHIP_SJA1000
) {
1047 netdev_err(mod
->ndev
, "unable to handle errors on non-SJA1000\n");
1051 /* check the message length for sanity */
1052 if (le16_to_cpu(msg
->len
) < 6) {
1053 netdev_err(mod
->ndev
, "error message too short\n");
1057 isrc
= msg
->data
[0];
1059 status
= msg
->data
[3];
1060 rxerr
= msg
->data
[4];
1061 txerr
= msg
->data
[5];
1064 * This hardware lacks any support other than bus error messages to
1065 * determine if packet transmission has failed.
1067 * When TX errors happen, one echo skb needs to be dropped from the
1068 * front of the queue.
1070 * A small bit of code is duplicated here and below, to avoid error
1071 * skb allocation when it will just be freed immediately.
1073 if (isrc
== CEVTIND_BEI
) {
1075 netdev_dbg(mod
->ndev
, "bus error interrupt\n");
1078 if (!(ecc
& ECC_DIR
)) {
1079 kfree_skb(skb_dequeue(&mod
->echoq
));
1086 * The controller automatically disables bus-error interrupts
1087 * and therefore we must re-enable them.
1089 ret
= ican3_set_buserror(mod
, 1);
1091 netdev_err(mod
->ndev
, "unable to re-enable bus-error\n");
1095 /* bus error reporting is off, return immediately */
1096 if (!(mod
->can
.ctrlmode
& CAN_CTRLMODE_BERR_REPORTING
))
1100 skb
= alloc_can_err_skb(dev
, &cf
);
1104 /* data overrun interrupt */
1105 if (isrc
== CEVTIND_DOI
|| isrc
== CEVTIND_LOST
) {
1106 netdev_dbg(mod
->ndev
, "data overrun interrupt\n");
1107 cf
->can_id
|= CAN_ERR_CRTL
;
1108 cf
->data
[1] = CAN_ERR_CRTL_RX_OVERFLOW
;
1109 stats
->rx_over_errors
++;
1113 /* error warning + passive interrupt */
1114 if (isrc
== CEVTIND_EI
) {
1115 netdev_dbg(mod
->ndev
, "error warning + passive interrupt\n");
1116 if (status
& SR_BS
) {
1117 state
= CAN_STATE_BUS_OFF
;
1118 cf
->can_id
|= CAN_ERR_BUSOFF
;
1119 mod
->can
.can_stats
.bus_off
++;
1121 } else if (status
& SR_ES
) {
1122 if (rxerr
>= 128 || txerr
>= 128)
1123 state
= CAN_STATE_ERROR_PASSIVE
;
1125 state
= CAN_STATE_ERROR_WARNING
;
1127 state
= CAN_STATE_ERROR_ACTIVE
;
1131 /* bus error interrupt */
1132 if (isrc
== CEVTIND_BEI
) {
1133 mod
->can
.can_stats
.bus_error
++;
1134 cf
->can_id
|= CAN_ERR_PROT
| CAN_ERR_BUSERROR
;
1136 switch (ecc
& ECC_MASK
) {
1138 cf
->data
[2] |= CAN_ERR_PROT_BIT
;
1141 cf
->data
[2] |= CAN_ERR_PROT_FORM
;
1144 cf
->data
[2] |= CAN_ERR_PROT_STUFF
;
1147 cf
->data
[3] = ecc
& ECC_SEG
;
1151 if (!(ecc
& ECC_DIR
))
1152 cf
->data
[2] |= CAN_ERR_PROT_TX
;
1154 cf
->data
[6] = txerr
;
1155 cf
->data
[7] = rxerr
;
1158 if (state
!= mod
->can
.state
&& (state
== CAN_STATE_ERROR_WARNING
||
1159 state
== CAN_STATE_ERROR_PASSIVE
)) {
1160 cf
->can_id
|= CAN_ERR_CRTL
;
1161 if (state
== CAN_STATE_ERROR_WARNING
) {
1162 mod
->can
.can_stats
.error_warning
++;
1163 cf
->data
[1] = (txerr
> rxerr
) ?
1164 CAN_ERR_CRTL_TX_WARNING
:
1165 CAN_ERR_CRTL_RX_WARNING
;
1167 mod
->can
.can_stats
.error_passive
++;
1168 cf
->data
[1] = (txerr
> rxerr
) ?
1169 CAN_ERR_CRTL_TX_PASSIVE
:
1170 CAN_ERR_CRTL_RX_PASSIVE
;
1173 cf
->data
[6] = txerr
;
1174 cf
->data
[7] = rxerr
;
1177 mod
->can
.state
= state
;
1182 static void ican3_handle_inquiry(struct ican3_dev
*mod
, struct ican3_msg
*msg
)
1184 switch (msg
->data
[0]) {
1185 case INQUIRY_STATUS
:
1186 case INQUIRY_EXTENDED
:
1187 mod
->bec
.rxerr
= msg
->data
[5];
1188 mod
->bec
.txerr
= msg
->data
[6];
1189 complete(&mod
->buserror_comp
);
1191 case INQUIRY_TERMINATION
:
1192 mod
->termination_enabled
= msg
->data
[6] & HWCONF_TERMINATE_ON
;
1193 complete(&mod
->termination_comp
);
1196 netdev_err(mod
->ndev
, "received an unknown inquiry response\n");
1201 /* Handle NMTS Slave Event Indication Messages from the firmware */
1202 static void ican3_handle_nmtsind(struct ican3_dev
*mod
, struct ican3_msg
*msg
)
1206 subspec
= msg
->data
[0] + msg
->data
[1] * 0x100;
1207 if (subspec
== NMTS_SLAVE_EVENT_IND
) {
1208 switch (msg
->data
[2]) {
1209 case NE_LOCAL_OCCURRED
:
1210 case NE_LOCAL_RESOLVED
:
1211 /* now follows the same message as Raw ICANOS CEVTIND
1212 * shift the data at the same place and call this method
1214 le16_add_cpu(&msg
->len
, -3);
1215 memmove(msg
->data
, msg
->data
+ 3, le16_to_cpu(msg
->len
));
1216 ican3_handle_cevtind(mod
, msg
);
1218 case NE_REMOTE_OCCURRED
:
1219 case NE_REMOTE_RESOLVED
:
1220 /* should not occurre, ignore */
1223 netdev_warn(mod
->ndev
, "unknown NMTS event indication %x\n",
1227 } else if (subspec
== NMTS_SLAVE_STATE_IND
) {
1228 /* ignore state indications */
1230 netdev_warn(mod
->ndev
, "unhandled NMTS indication %x\n",
1236 static void ican3_handle_unknown_message(struct ican3_dev
*mod
,
1237 struct ican3_msg
*msg
)
1239 netdev_warn(mod
->ndev
, "received unknown message: spec 0x%.2x length %d\n",
1240 msg
->spec
, le16_to_cpu(msg
->len
));
1244 * Handle a control message from the firmware
1246 static void ican3_handle_message(struct ican3_dev
*mod
, struct ican3_msg
*msg
)
1248 netdev_dbg(mod
->ndev
, "%s: modno %d spec 0x%.2x len %d bytes\n", __func__
,
1249 mod
->num
, msg
->spec
, le16_to_cpu(msg
->len
));
1251 switch (msg
->spec
) {
1253 ican3_handle_idvers(mod
, msg
);
1257 ican3_handle_msglost(mod
, msg
);
1260 ican3_handle_cevtind(mod
, msg
);
1263 ican3_handle_inquiry(mod
, msg
);
1266 ican3_handle_nmtsind(mod
, msg
);
1269 ican3_handle_unknown_message(mod
, msg
);
1275 * The ican3 needs to store all echo skbs, and therefore cannot
1276 * use the generic infrastructure for this.
1278 static void ican3_put_echo_skb(struct ican3_dev
*mod
, struct sk_buff
*skb
)
1280 skb
= can_create_echo_skb(skb
);
1284 /* save this skb for tx interrupt echo handling */
1285 skb_queue_tail(&mod
->echoq
, skb
);
1288 static unsigned int ican3_get_echo_skb(struct ican3_dev
*mod
)
1290 struct sk_buff
*skb
= skb_dequeue(&mod
->echoq
);
1291 struct can_frame
*cf
;
1294 /* this should never trigger unless there is a driver bug */
1296 netdev_err(mod
->ndev
, "BUG: echo skb not occupied\n");
1300 cf
= (struct can_frame
*)skb
->data
;
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
->can_dlc
!= echo_cf
->can_dlc
)
1342 return memcmp(cf
->data
, echo_cf
->data
, cf
->can_dlc
) == 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 stats
->rx_bytes
+= cf
->can_dlc
;
1429 netif_receive_skb(skb
);
1432 /* toggle the valid bit and return the descriptor to the ring */
1433 desc
.control
^= DESC_VALID
;
1435 spin_lock_irqsave(&mod
->lock
, flags
);
1437 ican3_set_page(mod
, mod
->fastrx_start
+ (mod
->fastrx_num
/ 16));
1438 memcpy_toio(desc_addr
, &desc
, 1);
1440 /* update the next buffer pointer */
1441 mod
->fastrx_num
= (desc
.control
& DESC_WRAP
) ? 0
1442 : (mod
->fastrx_num
+ 1);
1444 /* there are still more buffers to process */
1445 spin_unlock_irqrestore(&mod
->lock
, flags
);
1449 static int ican3_napi(struct napi_struct
*napi
, int budget
)
1451 struct ican3_dev
*mod
= container_of(napi
, struct ican3_dev
, napi
);
1452 unsigned long flags
;
1456 /* process all communication messages */
1458 struct ican3_msg
uninitialized_var(msg
);
1459 ret
= ican3_recv_msg(mod
, &msg
);
1463 ican3_handle_message(mod
, &msg
);
1466 /* process all CAN frames from the fast interface */
1467 while (received
< budget
) {
1468 ret
= ican3_recv_skb(mod
);
1475 /* We have processed all packets that the adapter had, but it
1476 * was less than our budget, stop polling */
1477 if (received
< budget
)
1478 napi_complete_done(napi
, received
);
1480 spin_lock_irqsave(&mod
->lock
, flags
);
1482 /* Wake up the transmit queue if necessary */
1483 if (netif_queue_stopped(mod
->ndev
) && ican3_txok(mod
))
1484 netif_wake_queue(mod
->ndev
);
1486 spin_unlock_irqrestore(&mod
->lock
, flags
);
1488 /* re-enable interrupt generation */
1489 iowrite8(1 << mod
->num
, &mod
->ctrl
->int_enable
);
1493 static irqreturn_t
ican3_irq(int irq
, void *dev_id
)
1495 struct ican3_dev
*mod
= dev_id
;
1499 * The interrupt status register on this device reports interrupts
1500 * as zeroes instead of using ones like most other devices
1502 stat
= ioread8(&mod
->ctrl
->int_disable
) & (1 << mod
->num
);
1503 if (stat
== (1 << mod
->num
))
1506 /* clear the MODULbus interrupt from the microcontroller */
1507 ioread8(&mod
->dpmctrl
->interrupt
);
1509 /* disable interrupt generation, schedule the NAPI poller */
1510 iowrite8(1 << mod
->num
, &mod
->ctrl
->int_disable
);
1511 napi_schedule(&mod
->napi
);
1516 * Firmware reset, startup, and shutdown
1520 * Reset an ICAN module to its power-on state
1522 * CONTEXT: no network device registered
1524 static int ican3_reset_module(struct ican3_dev
*mod
)
1526 unsigned long start
;
1529 /* disable interrupts so no more work is scheduled */
1530 iowrite8(1 << mod
->num
, &mod
->ctrl
->int_disable
);
1532 /* the first unallocated page in the DPM is #9 */
1533 mod
->free_page
= DPM_FREE_START
;
1535 ican3_set_page(mod
, QUEUE_OLD_CONTROL
);
1536 runold
= ioread8(mod
->dpm
+ TARGET_RUNNING
);
1538 /* reset the module */
1539 iowrite8(0x00, &mod
->dpmctrl
->hwreset
);
1541 /* wait until the module has finished resetting and is running */
1544 ican3_set_page(mod
, QUEUE_OLD_CONTROL
);
1545 runnew
= ioread8(mod
->dpm
+ TARGET_RUNNING
);
1546 if (runnew
== (runold
^ 0xff))
1550 } while (time_before(jiffies
, start
+ HZ
/ 2));
1552 netdev_err(mod
->ndev
, "failed to reset CAN module\n");
1556 static void ican3_shutdown_module(struct ican3_dev
*mod
)
1558 ican3_msg_disconnect(mod
);
1559 ican3_reset_module(mod
);
1563 * Startup an ICAN module, bringing it into fast mode
1565 static int ican3_startup_module(struct ican3_dev
*mod
)
1569 ret
= ican3_reset_module(mod
);
1571 netdev_err(mod
->ndev
, "unable to reset module\n");
1575 /* detect firmware */
1576 memcpy_fromio(mod
->fwinfo
, mod
->dpm
+ FIRMWARE_STAMP
, sizeof(mod
->fwinfo
) - 1);
1577 if (strncmp(mod
->fwinfo
, "JANZ-ICAN3", 10)) {
1578 netdev_err(mod
->ndev
, "ICAN3 not detected (found %s)\n", mod
->fwinfo
);
1581 if (strstr(mod
->fwinfo
, "CAL/CANopen"))
1582 mod
->fwtype
= ICAN3_FWTYPE_CAL_CANOPEN
;
1584 mod
->fwtype
= ICAN3_FWTYPE_ICANOS
;
1586 /* re-enable interrupts so we can send messages */
1587 iowrite8(1 << mod
->num
, &mod
->ctrl
->int_enable
);
1589 ret
= ican3_msg_connect(mod
);
1591 netdev_err(mod
->ndev
, "unable to connect to module\n");
1595 ican3_init_new_host_interface(mod
);
1596 ret
= ican3_msg_newhostif(mod
);
1598 netdev_err(mod
->ndev
, "unable to switch to new-style interface\n");
1602 /* default to "termination on" */
1603 ret
= ican3_set_termination(mod
, true);
1605 netdev_err(mod
->ndev
, "unable to enable termination\n");
1609 /* default to "bus errors enabled" */
1610 ret
= ican3_set_buserror(mod
, 1);
1612 netdev_err(mod
->ndev
, "unable to set bus-error\n");
1616 ican3_init_fast_host_interface(mod
);
1617 ret
= ican3_msg_fasthostif(mod
);
1619 netdev_err(mod
->ndev
, "unable to switch to fast host interface\n");
1623 ret
= ican3_set_id_filter(mod
, true);
1625 netdev_err(mod
->ndev
, "unable to set acceptance filter\n");
1633 * CAN Network Device
1636 static int ican3_open(struct net_device
*ndev
)
1638 struct ican3_dev
*mod
= netdev_priv(ndev
);
1641 /* open the CAN layer */
1642 ret
= open_candev(ndev
);
1644 netdev_err(mod
->ndev
, "unable to start CAN layer\n");
1648 /* bring the bus online */
1649 ret
= ican3_set_bus_state(mod
, true);
1651 netdev_err(mod
->ndev
, "unable to set bus-on\n");
1656 /* start up the network device */
1657 mod
->can
.state
= CAN_STATE_ERROR_ACTIVE
;
1658 netif_start_queue(ndev
);
1663 static int ican3_stop(struct net_device
*ndev
)
1665 struct ican3_dev
*mod
= netdev_priv(ndev
);
1668 /* stop the network device xmit routine */
1669 netif_stop_queue(ndev
);
1670 mod
->can
.state
= CAN_STATE_STOPPED
;
1672 /* bring the bus offline, stop receiving packets */
1673 ret
= ican3_set_bus_state(mod
, false);
1675 netdev_err(mod
->ndev
, "unable to set bus-off\n");
1679 /* drop all outstanding echo skbs */
1680 skb_queue_purge(&mod
->echoq
);
1682 /* close the CAN layer */
1687 static int ican3_xmit(struct sk_buff
*skb
, struct net_device
*ndev
)
1689 struct ican3_dev
*mod
= netdev_priv(ndev
);
1690 struct can_frame
*cf
= (struct can_frame
*)skb
->data
;
1691 struct ican3_fast_desc desc
;
1692 void __iomem
*desc_addr
;
1693 unsigned long flags
;
1695 if (can_dropped_invalid_skb(ndev
, skb
))
1696 return NETDEV_TX_OK
;
1698 spin_lock_irqsave(&mod
->lock
, flags
);
1700 /* check that we can actually transmit */
1701 if (!ican3_txok(mod
)) {
1702 netdev_err(mod
->ndev
, "BUG: no free descriptors\n");
1703 spin_unlock_irqrestore(&mod
->lock
, flags
);
1704 return NETDEV_TX_BUSY
;
1707 /* copy the control bits of the descriptor */
1708 ican3_set_page(mod
, mod
->fasttx_start
+ (mod
->fasttx_num
/ 16));
1709 desc_addr
= mod
->dpm
+ ((mod
->fasttx_num
% 16) * sizeof(desc
));
1710 memset(&desc
, 0, sizeof(desc
));
1711 memcpy_fromio(&desc
, desc_addr
, 1);
1713 /* convert the Linux CAN frame into ICAN3 format */
1714 can_frame_to_ican3(mod
, cf
, &desc
);
1717 * This hardware doesn't have TX-done notifications, so we'll try and
1718 * emulate it the best we can using ECHO skbs. Add the skb to the ECHO
1719 * stack. Upon packet reception, check if the ECHO skb and received
1720 * skb match, and use that to wake the queue.
1722 ican3_put_echo_skb(mod
, skb
);
1725 * the programming manual says that you must set the IVALID bit, then
1726 * interrupt, then set the valid bit. Quite weird, but it seems to be
1727 * required for this to work
1729 desc
.control
|= DESC_IVALID
;
1730 memcpy_toio(desc_addr
, &desc
, sizeof(desc
));
1732 /* generate a MODULbus interrupt to the microcontroller */
1733 iowrite8(0x01, &mod
->dpmctrl
->interrupt
);
1735 desc
.control
^= DESC_VALID
;
1736 memcpy_toio(desc_addr
, &desc
, sizeof(desc
));
1738 /* update the next buffer pointer */
1739 mod
->fasttx_num
= (desc
.control
& DESC_WRAP
) ? 0
1740 : (mod
->fasttx_num
+ 1);
1742 /* if there is no free descriptor space, stop the transmit queue */
1743 if (!ican3_txok(mod
))
1744 netif_stop_queue(ndev
);
1746 spin_unlock_irqrestore(&mod
->lock
, flags
);
1747 return NETDEV_TX_OK
;
1750 static const struct net_device_ops ican3_netdev_ops
= {
1751 .ndo_open
= ican3_open
,
1752 .ndo_stop
= ican3_stop
,
1753 .ndo_start_xmit
= ican3_xmit
,
1754 .ndo_change_mtu
= can_change_mtu
,
1758 * Low-level CAN Device
1761 /* This structure was stolen from drivers/net/can/sja1000/sja1000.c */
1762 static const struct can_bittiming_const ican3_bittiming_const
= {
1774 static int ican3_set_mode(struct net_device
*ndev
, enum can_mode mode
)
1776 struct ican3_dev
*mod
= netdev_priv(ndev
);
1779 if (mode
!= CAN_MODE_START
)
1782 /* bring the bus online */
1783 ret
= ican3_set_bus_state(mod
, true);
1785 netdev_err(ndev
, "unable to set bus-on\n");
1789 /* start up the network device */
1790 mod
->can
.state
= CAN_STATE_ERROR_ACTIVE
;
1792 if (netif_queue_stopped(ndev
))
1793 netif_wake_queue(ndev
);
1798 static int ican3_get_berr_counter(const struct net_device
*ndev
,
1799 struct can_berr_counter
*bec
)
1801 struct ican3_dev
*mod
= netdev_priv(ndev
);
1804 ret
= ican3_send_inquiry(mod
, INQUIRY_STATUS
);
1808 if (!wait_for_completion_timeout(&mod
->buserror_comp
, HZ
)) {
1809 netdev_info(mod
->ndev
, "%s timed out\n", __func__
);
1813 bec
->rxerr
= mod
->bec
.rxerr
;
1814 bec
->txerr
= mod
->bec
.txerr
;
1822 static ssize_t
ican3_sysfs_show_term(struct device
*dev
,
1823 struct device_attribute
*attr
,
1826 struct ican3_dev
*mod
= netdev_priv(to_net_dev(dev
));
1829 ret
= ican3_send_inquiry(mod
, INQUIRY_TERMINATION
);
1833 if (!wait_for_completion_timeout(&mod
->termination_comp
, HZ
)) {
1834 netdev_info(mod
->ndev
, "%s timed out\n", __func__
);
1838 return snprintf(buf
, PAGE_SIZE
, "%u\n", mod
->termination_enabled
);
1841 static ssize_t
ican3_sysfs_set_term(struct device
*dev
,
1842 struct device_attribute
*attr
,
1843 const char *buf
, size_t count
)
1845 struct ican3_dev
*mod
= netdev_priv(to_net_dev(dev
));
1846 unsigned long enable
;
1849 if (kstrtoul(buf
, 0, &enable
))
1852 ret
= ican3_set_termination(mod
, enable
);
1859 static ssize_t
ican3_sysfs_show_fwinfo(struct device
*dev
,
1860 struct device_attribute
*attr
,
1863 struct ican3_dev
*mod
= netdev_priv(to_net_dev(dev
));
1865 return scnprintf(buf
, PAGE_SIZE
, "%s\n", mod
->fwinfo
);
1868 static DEVICE_ATTR(termination
, S_IWUSR
| S_IRUGO
, ican3_sysfs_show_term
,
1869 ican3_sysfs_set_term
);
1870 static DEVICE_ATTR(fwinfo
, S_IRUSR
| S_IRUGO
, ican3_sysfs_show_fwinfo
, NULL
);
1872 static struct attribute
*ican3_sysfs_attrs
[] = {
1873 &dev_attr_termination
.attr
,
1874 &dev_attr_fwinfo
.attr
,
1878 static const struct attribute_group ican3_sysfs_attr_group
= {
1879 .attrs
= ican3_sysfs_attrs
,
1886 static int ican3_probe(struct platform_device
*pdev
)
1888 struct janz_platform_data
*pdata
;
1889 struct net_device
*ndev
;
1890 struct ican3_dev
*mod
;
1891 struct resource
*res
;
1895 pdata
= dev_get_platdata(&pdev
->dev
);
1899 dev_dbg(&pdev
->dev
, "probe: module number %d\n", pdata
->modno
);
1901 /* save the struct device for printing */
1904 /* allocate the CAN device and private data */
1905 ndev
= alloc_candev(sizeof(*mod
), 0);
1907 dev_err(dev
, "unable to allocate CANdev\n");
1912 platform_set_drvdata(pdev
, ndev
);
1913 mod
= netdev_priv(ndev
);
1915 mod
->num
= pdata
->modno
;
1916 netif_napi_add(ndev
, &mod
->napi
, ican3_napi
, ICAN3_RX_BUFFERS
);
1917 skb_queue_head_init(&mod
->echoq
);
1918 spin_lock_init(&mod
->lock
);
1919 init_completion(&mod
->termination_comp
);
1920 init_completion(&mod
->buserror_comp
);
1922 /* setup device-specific sysfs attributes */
1923 ndev
->sysfs_groups
[0] = &ican3_sysfs_attr_group
;
1925 /* the first unallocated page in the DPM is 9 */
1926 mod
->free_page
= DPM_FREE_START
;
1928 ndev
->netdev_ops
= &ican3_netdev_ops
;
1929 ndev
->flags
|= IFF_ECHO
;
1930 SET_NETDEV_DEV(ndev
, &pdev
->dev
);
1932 mod
->can
.clock
.freq
= ICAN3_CAN_CLOCK
;
1933 mod
->can
.bittiming_const
= &ican3_bittiming_const
;
1934 mod
->can
.do_set_mode
= ican3_set_mode
;
1935 mod
->can
.do_get_berr_counter
= ican3_get_berr_counter
;
1936 mod
->can
.ctrlmode_supported
= CAN_CTRLMODE_3_SAMPLES
1937 | CAN_CTRLMODE_BERR_REPORTING
1938 | CAN_CTRLMODE_ONE_SHOT
;
1940 /* find our IRQ number */
1941 mod
->irq
= platform_get_irq(pdev
, 0);
1943 dev_err(dev
, "IRQ line not found\n");
1948 ndev
->irq
= mod
->irq
;
1950 /* get access to the MODULbus registers for this module */
1951 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1953 dev_err(dev
, "MODULbus registers not found\n");
1958 mod
->dpm
= ioremap(res
->start
, resource_size(res
));
1960 dev_err(dev
, "MODULbus registers not ioremap\n");
1965 mod
->dpmctrl
= mod
->dpm
+ DPM_PAGE_SIZE
;
1967 /* get access to the control registers for this module */
1968 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
1970 dev_err(dev
, "CONTROL registers not found\n");
1972 goto out_iounmap_dpm
;
1975 mod
->ctrl
= ioremap(res
->start
, resource_size(res
));
1977 dev_err(dev
, "CONTROL registers not ioremap\n");
1979 goto out_iounmap_dpm
;
1982 /* disable our IRQ, then hookup the IRQ handler */
1983 iowrite8(1 << mod
->num
, &mod
->ctrl
->int_disable
);
1984 ret
= request_irq(mod
->irq
, ican3_irq
, IRQF_SHARED
, DRV_NAME
, mod
);
1986 dev_err(dev
, "unable to request IRQ\n");
1987 goto out_iounmap_ctrl
;
1990 /* reset and initialize the CAN controller into fast mode */
1991 napi_enable(&mod
->napi
);
1992 ret
= ican3_startup_module(mod
);
1994 dev_err(dev
, "%s: unable to start CANdev\n", __func__
);
1998 /* register with the Linux CAN layer */
1999 ret
= register_candev(ndev
);
2001 dev_err(dev
, "%s: unable to register CANdev\n", __func__
);
2005 netdev_info(mod
->ndev
, "module %d: registered CAN device\n", pdata
->modno
);
2009 napi_disable(&mod
->napi
);
2010 iowrite8(1 << mod
->num
, &mod
->ctrl
->int_disable
);
2011 free_irq(mod
->irq
, mod
);
2022 static int ican3_remove(struct platform_device
*pdev
)
2024 struct net_device
*ndev
= platform_get_drvdata(pdev
);
2025 struct ican3_dev
*mod
= netdev_priv(ndev
);
2027 /* unregister the netdevice, stop interrupts */
2028 unregister_netdev(ndev
);
2029 napi_disable(&mod
->napi
);
2030 iowrite8(1 << mod
->num
, &mod
->ctrl
->int_disable
);
2031 free_irq(mod
->irq
, mod
);
2033 /* put the module into reset */
2034 ican3_shutdown_module(mod
);
2036 /* unmap all registers */
2045 static struct platform_driver ican3_driver
= {
2049 .probe
= ican3_probe
,
2050 .remove
= ican3_remove
,
2053 module_platform_driver(ican3_driver
);
2055 MODULE_AUTHOR("Ira W. Snyder <iws@ovro.caltech.edu>");
2056 MODULE_DESCRIPTION("Janz MODULbus VMOD-ICAN3 Driver");
2057 MODULE_LICENSE("GPL");
2058 MODULE_ALIAS("platform:janz-ican3");