xref: /openbmc/linux/drivers/net/can/janz-ican3.c (revision 874c8ca1)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Janz MODULbus VMOD-ICAN3 CAN Interface Driver
4  *
5  * Copyright (c) 2010 Ira W. Snyder <iws@ovro.caltech.edu>
6  */
7 
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/interrupt.h>
11 #include <linux/delay.h>
12 #include <linux/platform_device.h>
13 
14 #include <linux/netdevice.h>
15 #include <linux/can.h>
16 #include <linux/can/dev.h>
17 #include <linux/can/skb.h>
18 #include <linux/can/error.h>
19 
20 #include <linux/mfd/janz.h>
21 #include <asm/io.h>
22 
23 /* the DPM has 64k of memory, organized into 256x 256 byte pages */
24 #define DPM_NUM_PAGES		256
25 #define DPM_PAGE_SIZE		256
26 #define DPM_PAGE_ADDR(p)	((p) * DPM_PAGE_SIZE)
27 
28 /* JANZ ICAN3 "old-style" host interface queue page numbers */
29 #define QUEUE_OLD_CONTROL	0
30 #define QUEUE_OLD_RB0		1
31 #define QUEUE_OLD_RB1		2
32 #define QUEUE_OLD_WB0		3
33 #define QUEUE_OLD_WB1		4
34 
35 /* Janz ICAN3 "old-style" host interface control registers */
36 #define MSYNC_PEER		0x00		/* ICAN only */
37 #define MSYNC_LOCL		0x01		/* host only */
38 #define TARGET_RUNNING		0x02
39 #define FIRMWARE_STAMP		0x60		/* big endian firmware stamp */
40 
41 #define MSYNC_RB0		0x01
42 #define MSYNC_RB1		0x02
43 #define MSYNC_RBLW		0x04
44 #define MSYNC_RB_MASK		(MSYNC_RB0 | MSYNC_RB1)
45 
46 #define MSYNC_WB0		0x10
47 #define MSYNC_WB1		0x20
48 #define MSYNC_WBLW		0x40
49 #define MSYNC_WB_MASK		(MSYNC_WB0 | MSYNC_WB1)
50 
51 /* Janz ICAN3 "new-style" host interface queue page numbers */
52 #define QUEUE_TOHOST		5
53 #define QUEUE_FROMHOST_MID	6
54 #define QUEUE_FROMHOST_HIGH	7
55 #define QUEUE_FROMHOST_LOW	8
56 
57 /* The first free page in the DPM is #9 */
58 #define DPM_FREE_START		9
59 
60 /* Janz ICAN3 "new-style" and "fast" host interface descriptor flags */
61 #define DESC_VALID		0x80
62 #define DESC_WRAP		0x40
63 #define DESC_INTERRUPT		0x20
64 #define DESC_IVALID		0x10
65 #define DESC_LEN(len)		(len)
66 
67 /* Janz ICAN3 Firmware Messages */
68 #define MSG_CONNECTI		0x02
69 #define MSG_DISCONNECT		0x03
70 #define MSG_IDVERS		0x04
71 #define MSG_MSGLOST		0x05
72 #define MSG_NEWHOSTIF		0x08
73 #define MSG_INQUIRY		0x0a
74 #define MSG_SETAFILMASK		0x10
75 #define MSG_INITFDPMQUEUE	0x11
76 #define MSG_HWCONF		0x12
77 #define MSG_FMSGLOST		0x15
78 #define MSG_CEVTIND		0x37
79 #define MSG_CBTRREQ		0x41
80 #define MSG_COFFREQ		0x42
81 #define MSG_CONREQ		0x43
82 #define MSG_CCONFREQ		0x47
83 #define MSG_NMTS		0xb0
84 #define MSG_LMTS		0xb4
85 
86 /*
87  * Janz ICAN3 CAN Inquiry Message Types
88  *
89  * NOTE: there appears to be a firmware bug here. You must send
90  * NOTE: INQUIRY_STATUS and expect to receive an INQUIRY_EXTENDED
91  * NOTE: response. The controller never responds to a message with
92  * NOTE: the INQUIRY_EXTENDED subspec :(
93  */
94 #define INQUIRY_STATUS		0x00
95 #define INQUIRY_TERMINATION	0x01
96 #define INQUIRY_EXTENDED	0x04
97 
98 /* Janz ICAN3 CAN Set Acceptance Filter Mask Message Types */
99 #define SETAFILMASK_REJECT	0x00
100 #define SETAFILMASK_FASTIF	0x02
101 
102 /* Janz ICAN3 CAN Hardware Configuration Message Types */
103 #define HWCONF_TERMINATE_ON	0x01
104 #define HWCONF_TERMINATE_OFF	0x00
105 
106 /* Janz ICAN3 CAN Event Indication Message Types */
107 #define CEVTIND_EI		0x01
108 #define CEVTIND_DOI		0x02
109 #define CEVTIND_LOST		0x04
110 #define CEVTIND_FULL		0x08
111 #define CEVTIND_BEI		0x10
112 
113 #define CEVTIND_CHIP_SJA1000	0x02
114 
115 #define ICAN3_BUSERR_QUOTA_MAX	255
116 
117 /* Janz ICAN3 CAN Frame Conversion */
118 #define ICAN3_SNGL	0x02
119 #define ICAN3_ECHO	0x10
120 #define ICAN3_EFF_RTR	0x40
121 #define ICAN3_SFF_RTR	0x10
122 #define ICAN3_EFF	0x80
123 
124 #define ICAN3_CAN_TYPE_MASK	0x0f
125 #define ICAN3_CAN_TYPE_SFF	0x00
126 #define ICAN3_CAN_TYPE_EFF	0x01
127 
128 #define ICAN3_CAN_DLC_MASK	0x0f
129 
130 /* Janz ICAN3 NMTS subtypes */
131 #define NMTS_CREATE_NODE_REQ	0x0
132 #define NMTS_SLAVE_STATE_IND	0x8
133 #define NMTS_SLAVE_EVENT_IND	0x9
134 
135 /* Janz ICAN3 LMTS subtypes */
136 #define LMTS_BUSON_REQ		0x0
137 #define LMTS_BUSOFF_REQ		0x1
138 #define LMTS_CAN_CONF_REQ	0x2
139 
140 /* Janz ICAN3 NMTS Event indications */
141 #define NE_LOCAL_OCCURRED	0x3
142 #define NE_LOCAL_RESOLVED	0x2
143 #define NE_REMOTE_OCCURRED	0xc
144 #define NE_REMOTE_RESOLVED	0x8
145 
146 /*
147  * SJA1000 Status and Error Register Definitions
148  *
149  * Copied from drivers/net/can/sja1000/sja1000.h
150  */
151 
152 /* status register content */
153 #define SR_BS		0x80
154 #define SR_ES		0x40
155 #define SR_TS		0x20
156 #define SR_RS		0x10
157 #define SR_TCS		0x08
158 #define SR_TBS		0x04
159 #define SR_DOS		0x02
160 #define SR_RBS		0x01
161 
162 #define SR_CRIT (SR_BS|SR_ES)
163 
164 /* ECC register */
165 #define ECC_SEG		0x1F
166 #define ECC_DIR		0x20
167 #define ECC_ERR		6
168 #define ECC_BIT		0x00
169 #define ECC_FORM	0x40
170 #define ECC_STUFF	0x80
171 #define ECC_MASK	0xc0
172 
173 /* Number of buffers for use in the "new-style" host interface */
174 #define ICAN3_NEW_BUFFERS	16
175 
176 /* Number of buffers for use in the "fast" host interface */
177 #define ICAN3_TX_BUFFERS	512
178 #define ICAN3_RX_BUFFERS	1024
179 
180 /* SJA1000 Clock Input */
181 #define ICAN3_CAN_CLOCK		8000000
182 
183 /* Janz ICAN3 firmware types */
184 enum ican3_fwtype {
185 	ICAN3_FWTYPE_ICANOS,
186 	ICAN3_FWTYPE_CAL_CANOPEN,
187 };
188 
189 /* Driver Name */
190 #define DRV_NAME "janz-ican3"
191 
192 /* DPM Control Registers -- starts at offset 0x100 in the MODULbus registers */
193 struct ican3_dpm_control {
194 	/* window address register */
195 	u8 window_address;
196 	u8 unused1;
197 
198 	/*
199 	 * Read access: clear interrupt from microcontroller
200 	 * Write access: send interrupt to microcontroller
201 	 */
202 	u8 interrupt;
203 	u8 unused2;
204 
205 	/* write-only: reset all hardware on the module */
206 	u8 hwreset;
207 	u8 unused3;
208 
209 	/* write-only: generate an interrupt to the TPU */
210 	u8 tpuinterrupt;
211 };
212 
213 struct ican3_dev {
214 
215 	/* must be the first member */
216 	struct can_priv can;
217 
218 	/* CAN network device */
219 	struct net_device *ndev;
220 	struct napi_struct napi;
221 
222 	/* module number */
223 	unsigned int num;
224 
225 	/* base address of registers and IRQ */
226 	struct janz_cmodio_onboard_regs __iomem *ctrl;
227 	struct ican3_dpm_control __iomem *dpmctrl;
228 	void __iomem *dpm;
229 	int irq;
230 
231 	/* CAN bus termination status */
232 	struct completion termination_comp;
233 	bool termination_enabled;
234 
235 	/* CAN bus error status registers */
236 	struct completion buserror_comp;
237 	struct can_berr_counter bec;
238 
239 	/* firmware type */
240 	enum ican3_fwtype fwtype;
241 	char fwinfo[32];
242 
243 	/* old and new style host interface */
244 	unsigned int iftype;
245 
246 	/* queue for echo packets */
247 	struct sk_buff_head echoq;
248 
249 	/*
250 	 * Any function which changes the current DPM page must hold this
251 	 * lock while it is performing data accesses. This ensures that the
252 	 * function will not be preempted and end up reading data from a
253 	 * different DPM page than it expects.
254 	 */
255 	spinlock_t lock;
256 
257 	/* new host interface */
258 	unsigned int rx_int;
259 	unsigned int rx_num;
260 	unsigned int tx_num;
261 
262 	/* fast host interface */
263 	unsigned int fastrx_start;
264 	unsigned int fastrx_num;
265 	unsigned int fasttx_start;
266 	unsigned int fasttx_num;
267 
268 	/* first free DPM page */
269 	unsigned int free_page;
270 };
271 
272 struct ican3_msg {
273 	u8 control;
274 	u8 spec;
275 	__le16 len;
276 	u8 data[252];
277 };
278 
279 struct ican3_new_desc {
280 	u8 control;
281 	u8 pointer;
282 };
283 
284 struct ican3_fast_desc {
285 	u8 control;
286 	u8 command;
287 	u8 data[14];
288 };
289 
290 /* write to the window basic address register */
291 static inline void ican3_set_page(struct ican3_dev *mod, unsigned int page)
292 {
293 	BUG_ON(page >= DPM_NUM_PAGES);
294 	iowrite8(page, &mod->dpmctrl->window_address);
295 }
296 
297 /*
298  * ICAN3 "old-style" host interface
299  */
300 
301 /*
302  * Receive a message from the ICAN3 "old-style" firmware interface
303  *
304  * LOCKING: must hold mod->lock
305  *
306  * returns 0 on success, -ENOMEM when no message exists
307  */
308 static int ican3_old_recv_msg(struct ican3_dev *mod, struct ican3_msg *msg)
309 {
310 	unsigned int mbox, mbox_page;
311 	u8 locl, peer, xord;
312 
313 	/* get the MSYNC registers */
314 	ican3_set_page(mod, QUEUE_OLD_CONTROL);
315 	peer = ioread8(mod->dpm + MSYNC_PEER);
316 	locl = ioread8(mod->dpm + MSYNC_LOCL);
317 	xord = locl ^ peer;
318 
319 	if ((xord & MSYNC_RB_MASK) == 0x00) {
320 		netdev_dbg(mod->ndev, "no mbox for reading\n");
321 		return -ENOMEM;
322 	}
323 
324 	/* find the first free mbox to read */
325 	if ((xord & MSYNC_RB_MASK) == MSYNC_RB_MASK)
326 		mbox = (xord & MSYNC_RBLW) ? MSYNC_RB0 : MSYNC_RB1;
327 	else
328 		mbox = (xord & MSYNC_RB0) ? MSYNC_RB0 : MSYNC_RB1;
329 
330 	/* copy the message */
331 	mbox_page = (mbox == MSYNC_RB0) ? QUEUE_OLD_RB0 : QUEUE_OLD_RB1;
332 	ican3_set_page(mod, mbox_page);
333 	memcpy_fromio(msg, mod->dpm, sizeof(*msg));
334 
335 	/*
336 	 * notify the firmware that the read buffer is available
337 	 * for it to fill again
338 	 */
339 	locl ^= mbox;
340 
341 	ican3_set_page(mod, QUEUE_OLD_CONTROL);
342 	iowrite8(locl, mod->dpm + MSYNC_LOCL);
343 	return 0;
344 }
345 
346 /*
347  * Send a message through the "old-style" firmware interface
348  *
349  * LOCKING: must hold mod->lock
350  *
351  * returns 0 on success, -ENOMEM when no free space exists
352  */
353 static int ican3_old_send_msg(struct ican3_dev *mod, struct ican3_msg *msg)
354 {
355 	unsigned int mbox, mbox_page;
356 	u8 locl, peer, xord;
357 
358 	/* get the MSYNC registers */
359 	ican3_set_page(mod, QUEUE_OLD_CONTROL);
360 	peer = ioread8(mod->dpm + MSYNC_PEER);
361 	locl = ioread8(mod->dpm + MSYNC_LOCL);
362 	xord = locl ^ peer;
363 
364 	if ((xord & MSYNC_WB_MASK) == MSYNC_WB_MASK) {
365 		netdev_err(mod->ndev, "no mbox for writing\n");
366 		return -ENOMEM;
367 	}
368 
369 	/* calculate a free mbox to use */
370 	mbox = (xord & MSYNC_WB0) ? MSYNC_WB1 : MSYNC_WB0;
371 
372 	/* copy the message to the DPM */
373 	mbox_page = (mbox == MSYNC_WB0) ? QUEUE_OLD_WB0 : QUEUE_OLD_WB1;
374 	ican3_set_page(mod, mbox_page);
375 	memcpy_toio(mod->dpm, msg, sizeof(*msg));
376 
377 	locl ^= mbox;
378 	if (mbox == MSYNC_WB1)
379 		locl |= MSYNC_WBLW;
380 
381 	ican3_set_page(mod, QUEUE_OLD_CONTROL);
382 	iowrite8(locl, mod->dpm + MSYNC_LOCL);
383 	return 0;
384 }
385 
386 /*
387  * ICAN3 "new-style" Host Interface Setup
388  */
389 
390 static void ican3_init_new_host_interface(struct ican3_dev *mod)
391 {
392 	struct ican3_new_desc desc;
393 	unsigned long flags;
394 	void __iomem *dst;
395 	int i;
396 
397 	spin_lock_irqsave(&mod->lock, flags);
398 
399 	/* setup the internal datastructures for RX */
400 	mod->rx_num = 0;
401 	mod->rx_int = 0;
402 
403 	/* tohost queue descriptors are in page 5 */
404 	ican3_set_page(mod, QUEUE_TOHOST);
405 	dst = mod->dpm;
406 
407 	/* initialize the tohost (rx) queue descriptors: pages 9-24 */
408 	for (i = 0; i < ICAN3_NEW_BUFFERS; i++) {
409 		desc.control = DESC_INTERRUPT | DESC_LEN(1); /* I L=1 */
410 		desc.pointer = mod->free_page;
411 
412 		/* set wrap flag on last buffer */
413 		if (i == ICAN3_NEW_BUFFERS - 1)
414 			desc.control |= DESC_WRAP;
415 
416 		memcpy_toio(dst, &desc, sizeof(desc));
417 		dst += sizeof(desc);
418 		mod->free_page++;
419 	}
420 
421 	/* fromhost (tx) mid queue descriptors are in page 6 */
422 	ican3_set_page(mod, QUEUE_FROMHOST_MID);
423 	dst = mod->dpm;
424 
425 	/* setup the internal datastructures for TX */
426 	mod->tx_num = 0;
427 
428 	/* initialize the fromhost mid queue descriptors: pages 25-40 */
429 	for (i = 0; i < ICAN3_NEW_BUFFERS; i++) {
430 		desc.control = DESC_VALID | DESC_LEN(1); /* V L=1 */
431 		desc.pointer = mod->free_page;
432 
433 		/* set wrap flag on last buffer */
434 		if (i == ICAN3_NEW_BUFFERS - 1)
435 			desc.control |= DESC_WRAP;
436 
437 		memcpy_toio(dst, &desc, sizeof(desc));
438 		dst += sizeof(desc);
439 		mod->free_page++;
440 	}
441 
442 	/* fromhost hi queue descriptors are in page 7 */
443 	ican3_set_page(mod, QUEUE_FROMHOST_HIGH);
444 	dst = mod->dpm;
445 
446 	/* initialize only a single buffer in the fromhost hi queue (unused) */
447 	desc.control = DESC_VALID | DESC_WRAP | DESC_LEN(1); /* VW L=1 */
448 	desc.pointer = mod->free_page;
449 	memcpy_toio(dst, &desc, sizeof(desc));
450 	mod->free_page++;
451 
452 	/* fromhost low queue descriptors are in page 8 */
453 	ican3_set_page(mod, QUEUE_FROMHOST_LOW);
454 	dst = mod->dpm;
455 
456 	/* initialize only a single buffer in the fromhost low queue (unused) */
457 	desc.control = DESC_VALID | DESC_WRAP | DESC_LEN(1); /* VW L=1 */
458 	desc.pointer = mod->free_page;
459 	memcpy_toio(dst, &desc, sizeof(desc));
460 	mod->free_page++;
461 
462 	spin_unlock_irqrestore(&mod->lock, flags);
463 }
464 
465 /*
466  * ICAN3 Fast Host Interface Setup
467  */
468 
469 static void ican3_init_fast_host_interface(struct ican3_dev *mod)
470 {
471 	struct ican3_fast_desc desc;
472 	unsigned long flags;
473 	unsigned int addr;
474 	void __iomem *dst;
475 	int i;
476 
477 	spin_lock_irqsave(&mod->lock, flags);
478 
479 	/* save the start recv page */
480 	mod->fastrx_start = mod->free_page;
481 	mod->fastrx_num = 0;
482 
483 	/* build a single fast tohost queue descriptor */
484 	memset(&desc, 0, sizeof(desc));
485 	desc.control = 0x00;
486 	desc.command = 1;
487 
488 	/* build the tohost queue descriptor ring in memory */
489 	addr = 0;
490 	for (i = 0; i < ICAN3_RX_BUFFERS; i++) {
491 
492 		/* set the wrap bit on the last buffer */
493 		if (i == ICAN3_RX_BUFFERS - 1)
494 			desc.control |= DESC_WRAP;
495 
496 		/* switch to the correct page */
497 		ican3_set_page(mod, mod->free_page);
498 
499 		/* copy the descriptor to the DPM */
500 		dst = mod->dpm + addr;
501 		memcpy_toio(dst, &desc, sizeof(desc));
502 		addr += sizeof(desc);
503 
504 		/* move to the next page if necessary */
505 		if (addr >= DPM_PAGE_SIZE) {
506 			addr = 0;
507 			mod->free_page++;
508 		}
509 	}
510 
511 	/* make sure we page-align the next queue */
512 	if (addr != 0)
513 		mod->free_page++;
514 
515 	/* save the start xmit page */
516 	mod->fasttx_start = mod->free_page;
517 	mod->fasttx_num = 0;
518 
519 	/* build a single fast fromhost queue descriptor */
520 	memset(&desc, 0, sizeof(desc));
521 	desc.control = DESC_VALID;
522 	desc.command = 1;
523 
524 	/* build the fromhost queue descriptor ring in memory */
525 	addr = 0;
526 	for (i = 0; i < ICAN3_TX_BUFFERS; i++) {
527 
528 		/* set the wrap bit on the last buffer */
529 		if (i == ICAN3_TX_BUFFERS - 1)
530 			desc.control |= DESC_WRAP;
531 
532 		/* switch to the correct page */
533 		ican3_set_page(mod, mod->free_page);
534 
535 		/* copy the descriptor to the DPM */
536 		dst = mod->dpm + addr;
537 		memcpy_toio(dst, &desc, sizeof(desc));
538 		addr += sizeof(desc);
539 
540 		/* move to the next page if necessary */
541 		if (addr >= DPM_PAGE_SIZE) {
542 			addr = 0;
543 			mod->free_page++;
544 		}
545 	}
546 
547 	spin_unlock_irqrestore(&mod->lock, flags);
548 }
549 
550 /*
551  * ICAN3 "new-style" Host Interface Message Helpers
552  */
553 
554 /*
555  * LOCKING: must hold mod->lock
556  */
557 static int ican3_new_send_msg(struct ican3_dev *mod, struct ican3_msg *msg)
558 {
559 	struct ican3_new_desc desc;
560 	void __iomem *desc_addr = mod->dpm + (mod->tx_num * sizeof(desc));
561 
562 	/* switch to the fromhost mid queue, and read the buffer descriptor */
563 	ican3_set_page(mod, QUEUE_FROMHOST_MID);
564 	memcpy_fromio(&desc, desc_addr, sizeof(desc));
565 
566 	if (!(desc.control & DESC_VALID)) {
567 		netdev_dbg(mod->ndev, "%s: no free buffers\n", __func__);
568 		return -ENOMEM;
569 	}
570 
571 	/* switch to the data page, copy the data */
572 	ican3_set_page(mod, desc.pointer);
573 	memcpy_toio(mod->dpm, msg, sizeof(*msg));
574 
575 	/* switch back to the descriptor, set the valid bit, write it back */
576 	ican3_set_page(mod, QUEUE_FROMHOST_MID);
577 	desc.control ^= DESC_VALID;
578 	memcpy_toio(desc_addr, &desc, sizeof(desc));
579 
580 	/* update the tx number */
581 	mod->tx_num = (desc.control & DESC_WRAP) ? 0 : (mod->tx_num + 1);
582 	return 0;
583 }
584 
585 /*
586  * LOCKING: must hold mod->lock
587  */
588 static int ican3_new_recv_msg(struct ican3_dev *mod, struct ican3_msg *msg)
589 {
590 	struct ican3_new_desc desc;
591 	void __iomem *desc_addr = mod->dpm + (mod->rx_num * sizeof(desc));
592 
593 	/* switch to the tohost queue, and read the buffer descriptor */
594 	ican3_set_page(mod, QUEUE_TOHOST);
595 	memcpy_fromio(&desc, desc_addr, sizeof(desc));
596 
597 	if (!(desc.control & DESC_VALID)) {
598 		netdev_dbg(mod->ndev, "%s: no buffers to recv\n", __func__);
599 		return -ENOMEM;
600 	}
601 
602 	/* switch to the data page, copy the data */
603 	ican3_set_page(mod, desc.pointer);
604 	memcpy_fromio(msg, mod->dpm, sizeof(*msg));
605 
606 	/* switch back to the descriptor, toggle the valid bit, write it back */
607 	ican3_set_page(mod, QUEUE_TOHOST);
608 	desc.control ^= DESC_VALID;
609 	memcpy_toio(desc_addr, &desc, sizeof(desc));
610 
611 	/* update the rx number */
612 	mod->rx_num = (desc.control & DESC_WRAP) ? 0 : (mod->rx_num + 1);
613 	return 0;
614 }
615 
616 /*
617  * Message Send / Recv Helpers
618  */
619 
620 static int ican3_send_msg(struct ican3_dev *mod, struct ican3_msg *msg)
621 {
622 	unsigned long flags;
623 	int ret;
624 
625 	spin_lock_irqsave(&mod->lock, flags);
626 
627 	if (mod->iftype == 0)
628 		ret = ican3_old_send_msg(mod, msg);
629 	else
630 		ret = ican3_new_send_msg(mod, msg);
631 
632 	spin_unlock_irqrestore(&mod->lock, flags);
633 	return ret;
634 }
635 
636 static int ican3_recv_msg(struct ican3_dev *mod, struct ican3_msg *msg)
637 {
638 	unsigned long flags;
639 	int ret;
640 
641 	spin_lock_irqsave(&mod->lock, flags);
642 
643 	if (mod->iftype == 0)
644 		ret = ican3_old_recv_msg(mod, msg);
645 	else
646 		ret = ican3_new_recv_msg(mod, msg);
647 
648 	spin_unlock_irqrestore(&mod->lock, flags);
649 	return ret;
650 }
651 
652 /*
653  * Quick Pre-constructed Messages
654  */
655 
656 static int ican3_msg_connect(struct ican3_dev *mod)
657 {
658 	struct ican3_msg msg;
659 
660 	memset(&msg, 0, sizeof(msg));
661 	msg.spec = MSG_CONNECTI;
662 	msg.len = cpu_to_le16(0);
663 
664 	return ican3_send_msg(mod, &msg);
665 }
666 
667 static int ican3_msg_disconnect(struct ican3_dev *mod)
668 {
669 	struct ican3_msg msg;
670 
671 	memset(&msg, 0, sizeof(msg));
672 	msg.spec = MSG_DISCONNECT;
673 	msg.len = cpu_to_le16(0);
674 
675 	return ican3_send_msg(mod, &msg);
676 }
677 
678 static int ican3_msg_newhostif(struct ican3_dev *mod)
679 {
680 	struct ican3_msg msg;
681 	int ret;
682 
683 	memset(&msg, 0, sizeof(msg));
684 	msg.spec = MSG_NEWHOSTIF;
685 	msg.len = cpu_to_le16(0);
686 
687 	/* If we're not using the old interface, switching seems bogus */
688 	WARN_ON(mod->iftype != 0);
689 
690 	ret = ican3_send_msg(mod, &msg);
691 	if (ret)
692 		return ret;
693 
694 	/* mark the module as using the new host interface */
695 	mod->iftype = 1;
696 	return 0;
697 }
698 
699 static int ican3_msg_fasthostif(struct ican3_dev *mod)
700 {
701 	struct ican3_msg msg;
702 	unsigned int addr;
703 
704 	memset(&msg, 0, sizeof(msg));
705 	msg.spec = MSG_INITFDPMQUEUE;
706 	msg.len = cpu_to_le16(8);
707 
708 	/* write the tohost queue start address */
709 	addr = DPM_PAGE_ADDR(mod->fastrx_start);
710 	msg.data[0] = addr & 0xff;
711 	msg.data[1] = (addr >> 8) & 0xff;
712 	msg.data[2] = (addr >> 16) & 0xff;
713 	msg.data[3] = (addr >> 24) & 0xff;
714 
715 	/* write the fromhost queue start address */
716 	addr = DPM_PAGE_ADDR(mod->fasttx_start);
717 	msg.data[4] = addr & 0xff;
718 	msg.data[5] = (addr >> 8) & 0xff;
719 	msg.data[6] = (addr >> 16) & 0xff;
720 	msg.data[7] = (addr >> 24) & 0xff;
721 
722 	/* If we're not using the new interface yet, we cannot do this */
723 	WARN_ON(mod->iftype != 1);
724 
725 	return ican3_send_msg(mod, &msg);
726 }
727 
728 /*
729  * Setup the CAN filter to either accept or reject all
730  * messages from the CAN bus.
731  */
732 static int ican3_set_id_filter(struct ican3_dev *mod, bool accept)
733 {
734 	struct ican3_msg msg;
735 	int ret;
736 
737 	/* Standard Frame Format */
738 	memset(&msg, 0, sizeof(msg));
739 	msg.spec = MSG_SETAFILMASK;
740 	msg.len = cpu_to_le16(5);
741 	msg.data[0] = 0x00; /* IDLo LSB */
742 	msg.data[1] = 0x00; /* IDLo MSB */
743 	msg.data[2] = 0xff; /* IDHi LSB */
744 	msg.data[3] = 0x07; /* IDHi MSB */
745 
746 	/* accept all frames for fast host if, or reject all frames */
747 	msg.data[4] = accept ? SETAFILMASK_FASTIF : SETAFILMASK_REJECT;
748 
749 	ret = ican3_send_msg(mod, &msg);
750 	if (ret)
751 		return ret;
752 
753 	/* Extended Frame Format */
754 	memset(&msg, 0, sizeof(msg));
755 	msg.spec = MSG_SETAFILMASK;
756 	msg.len = cpu_to_le16(13);
757 	msg.data[0] = 0;    /* MUX = 0 */
758 	msg.data[1] = 0x00; /* IDLo LSB */
759 	msg.data[2] = 0x00;
760 	msg.data[3] = 0x00;
761 	msg.data[4] = 0x20; /* IDLo MSB */
762 	msg.data[5] = 0xff; /* IDHi LSB */
763 	msg.data[6] = 0xff;
764 	msg.data[7] = 0xff;
765 	msg.data[8] = 0x3f; /* IDHi MSB */
766 
767 	/* accept all frames for fast host if, or reject all frames */
768 	msg.data[9] = accept ? SETAFILMASK_FASTIF : SETAFILMASK_REJECT;
769 
770 	return ican3_send_msg(mod, &msg);
771 }
772 
773 /*
774  * Bring the CAN bus online or offline
775  */
776 static int ican3_set_bus_state(struct ican3_dev *mod, bool on)
777 {
778 	struct can_bittiming *bt = &mod->can.bittiming;
779 	struct ican3_msg msg;
780 	u8 btr0, btr1;
781 	int res;
782 
783 	/* This algorithm was stolen from drivers/net/can/sja1000/sja1000.c      */
784 	/* The bittiming register command for the ICAN3 just sets the bit timing */
785 	/* registers on the SJA1000 chip directly                                */
786 	btr0 = ((bt->brp - 1) & 0x3f) | (((bt->sjw - 1) & 0x3) << 6);
787 	btr1 = ((bt->prop_seg + bt->phase_seg1 - 1) & 0xf) |
788 		(((bt->phase_seg2 - 1) & 0x7) << 4);
789 	if (mod->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
790 		btr1 |= 0x80;
791 
792 	if (mod->fwtype == ICAN3_FWTYPE_ICANOS) {
793 		if (on) {
794 			/* set bittiming */
795 			memset(&msg, 0, sizeof(msg));
796 			msg.spec = MSG_CBTRREQ;
797 			msg.len = cpu_to_le16(4);
798 			msg.data[0] = 0x00;
799 			msg.data[1] = 0x00;
800 			msg.data[2] = btr0;
801 			msg.data[3] = btr1;
802 
803 			res = ican3_send_msg(mod, &msg);
804 			if (res)
805 				return res;
806 		}
807 
808 		/* can-on/off request */
809 		memset(&msg, 0, sizeof(msg));
810 		msg.spec = on ? MSG_CONREQ : MSG_COFFREQ;
811 		msg.len = cpu_to_le16(0);
812 
813 		return ican3_send_msg(mod, &msg);
814 
815 	} else if (mod->fwtype == ICAN3_FWTYPE_CAL_CANOPEN) {
816 		/* bittiming + can-on/off request */
817 		memset(&msg, 0, sizeof(msg));
818 		msg.spec = MSG_LMTS;
819 		if (on) {
820 			msg.len = cpu_to_le16(4);
821 			msg.data[0] = LMTS_BUSON_REQ;
822 			msg.data[1] = 0;
823 			msg.data[2] = btr0;
824 			msg.data[3] = btr1;
825 		} else {
826 			msg.len = cpu_to_le16(2);
827 			msg.data[0] = LMTS_BUSOFF_REQ;
828 			msg.data[1] = 0;
829 		}
830 		res = ican3_send_msg(mod, &msg);
831 		if (res)
832 			return res;
833 
834 		if (on) {
835 			/* create NMT Slave Node for error processing
836 			 *   class 2 (with error capability, see CiA/DS203-1)
837 			 *   id    1
838 			 *   name  locnod1 (must be exactly 7 bytes)
839 			 */
840 			memset(&msg, 0, sizeof(msg));
841 			msg.spec = MSG_NMTS;
842 			msg.len = cpu_to_le16(11);
843 			msg.data[0] = NMTS_CREATE_NODE_REQ;
844 			msg.data[1] = 0;
845 			msg.data[2] = 2;                 /* node class */
846 			msg.data[3] = 1;                 /* node id */
847 			strcpy(msg.data + 4, "locnod1"); /* node name  */
848 			return ican3_send_msg(mod, &msg);
849 		}
850 		return 0;
851 	}
852 	return -ENOTSUPP;
853 }
854 
855 static int ican3_set_termination(struct ican3_dev *mod, bool on)
856 {
857 	struct ican3_msg msg;
858 
859 	memset(&msg, 0, sizeof(msg));
860 	msg.spec = MSG_HWCONF;
861 	msg.len = cpu_to_le16(2);
862 	msg.data[0] = 0x00;
863 	msg.data[1] = on ? HWCONF_TERMINATE_ON : HWCONF_TERMINATE_OFF;
864 
865 	return ican3_send_msg(mod, &msg);
866 }
867 
868 static int ican3_send_inquiry(struct ican3_dev *mod, u8 subspec)
869 {
870 	struct ican3_msg msg;
871 
872 	memset(&msg, 0, sizeof(msg));
873 	msg.spec = MSG_INQUIRY;
874 	msg.len = cpu_to_le16(2);
875 	msg.data[0] = subspec;
876 	msg.data[1] = 0x00;
877 
878 	return ican3_send_msg(mod, &msg);
879 }
880 
881 static int ican3_set_buserror(struct ican3_dev *mod, u8 quota)
882 {
883 	struct ican3_msg msg;
884 
885 	if (mod->fwtype == ICAN3_FWTYPE_ICANOS) {
886 		memset(&msg, 0, sizeof(msg));
887 		msg.spec = MSG_CCONFREQ;
888 		msg.len = cpu_to_le16(2);
889 		msg.data[0] = 0x00;
890 		msg.data[1] = quota;
891 	} else if (mod->fwtype == ICAN3_FWTYPE_CAL_CANOPEN) {
892 		memset(&msg, 0, sizeof(msg));
893 		msg.spec = MSG_LMTS;
894 		msg.len = cpu_to_le16(4);
895 		msg.data[0] = LMTS_CAN_CONF_REQ;
896 		msg.data[1] = 0x00;
897 		msg.data[2] = 0x00;
898 		msg.data[3] = quota;
899 	} else {
900 		return -ENOTSUPP;
901 	}
902 	return ican3_send_msg(mod, &msg);
903 }
904 
905 /*
906  * ICAN3 to Linux CAN Frame Conversion
907  */
908 
909 static void ican3_to_can_frame(struct ican3_dev *mod,
910 			       struct ican3_fast_desc *desc,
911 			       struct can_frame *cf)
912 {
913 	if ((desc->command & ICAN3_CAN_TYPE_MASK) == ICAN3_CAN_TYPE_SFF) {
914 		if (desc->data[1] & ICAN3_SFF_RTR)
915 			cf->can_id |= CAN_RTR_FLAG;
916 
917 		cf->can_id |= desc->data[0] << 3;
918 		cf->can_id |= (desc->data[1] & 0xe0) >> 5;
919 		cf->len = can_cc_dlc2len(desc->data[1] & ICAN3_CAN_DLC_MASK);
920 		memcpy(cf->data, &desc->data[2], cf->len);
921 	} else {
922 		cf->len = can_cc_dlc2len(desc->data[0] & ICAN3_CAN_DLC_MASK);
923 		if (desc->data[0] & ICAN3_EFF_RTR)
924 			cf->can_id |= CAN_RTR_FLAG;
925 
926 		if (desc->data[0] & ICAN3_EFF) {
927 			cf->can_id |= CAN_EFF_FLAG;
928 			cf->can_id |= desc->data[2] << 21; /* 28-21 */
929 			cf->can_id |= desc->data[3] << 13; /* 20-13 */
930 			cf->can_id |= desc->data[4] << 5;  /* 12-5  */
931 			cf->can_id |= (desc->data[5] & 0xf8) >> 3;
932 		} else {
933 			cf->can_id |= desc->data[2] << 3;  /* 10-3  */
934 			cf->can_id |= desc->data[3] >> 5;  /* 2-0   */
935 		}
936 
937 		memcpy(cf->data, &desc->data[6], cf->len);
938 	}
939 }
940 
941 static void can_frame_to_ican3(struct ican3_dev *mod,
942 			       struct can_frame *cf,
943 			       struct ican3_fast_desc *desc)
944 {
945 	/* clear out any stale data in the descriptor */
946 	memset(desc->data, 0, sizeof(desc->data));
947 
948 	/* we always use the extended format, with the ECHO flag set */
949 	desc->command = ICAN3_CAN_TYPE_EFF;
950 	desc->data[0] |= cf->len;
951 	desc->data[1] |= ICAN3_ECHO;
952 
953 	/* support single transmission (no retries) mode */
954 	if (mod->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT)
955 		desc->data[1] |= ICAN3_SNGL;
956 
957 	if (cf->can_id & CAN_RTR_FLAG)
958 		desc->data[0] |= ICAN3_EFF_RTR;
959 
960 	/* pack the id into the correct places */
961 	if (cf->can_id & CAN_EFF_FLAG) {
962 		desc->data[0] |= ICAN3_EFF;
963 		desc->data[2] = (cf->can_id & 0x1fe00000) >> 21; /* 28-21 */
964 		desc->data[3] = (cf->can_id & 0x001fe000) >> 13; /* 20-13 */
965 		desc->data[4] = (cf->can_id & 0x00001fe0) >> 5;  /* 12-5  */
966 		desc->data[5] = (cf->can_id & 0x0000001f) << 3;  /* 4-0   */
967 	} else {
968 		desc->data[2] = (cf->can_id & 0x7F8) >> 3; /* bits 10-3 */
969 		desc->data[3] = (cf->can_id & 0x007) << 5; /* bits 2-0  */
970 	}
971 
972 	/* copy the data bits into the descriptor */
973 	memcpy(&desc->data[6], cf->data, cf->len);
974 }
975 
976 /*
977  * Interrupt Handling
978  */
979 
980 /*
981  * Handle an ID + Version message response from the firmware. We never generate
982  * this message in production code, but it is very useful when debugging to be
983  * able to display this message.
984  */
985 static void ican3_handle_idvers(struct ican3_dev *mod, struct ican3_msg *msg)
986 {
987 	netdev_dbg(mod->ndev, "IDVERS response: %s\n", msg->data);
988 }
989 
990 static void ican3_handle_msglost(struct ican3_dev *mod, struct ican3_msg *msg)
991 {
992 	struct net_device *dev = mod->ndev;
993 	struct net_device_stats *stats = &dev->stats;
994 	struct can_frame *cf;
995 	struct sk_buff *skb;
996 
997 	/*
998 	 * Report that communication messages with the microcontroller firmware
999 	 * are being lost. These are never CAN frames, so we do not generate an
1000 	 * error frame for userspace
1001 	 */
1002 	if (msg->spec == MSG_MSGLOST) {
1003 		netdev_err(mod->ndev, "lost %d control messages\n", msg->data[0]);
1004 		return;
1005 	}
1006 
1007 	/*
1008 	 * Oops, this indicates that we have lost messages in the fast queue,
1009 	 * which are exclusively CAN messages. Our driver isn't reading CAN
1010 	 * frames fast enough.
1011 	 *
1012 	 * We'll pretend that the SJA1000 told us that it ran out of buffer
1013 	 * space, because there is not a better message for this.
1014 	 */
1015 	skb = alloc_can_err_skb(dev, &cf);
1016 	if (skb) {
1017 		cf->can_id |= CAN_ERR_CRTL;
1018 		cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
1019 		stats->rx_over_errors++;
1020 		stats->rx_errors++;
1021 		netif_rx(skb);
1022 	}
1023 }
1024 
1025 /*
1026  * Handle CAN Event Indication Messages from the firmware
1027  *
1028  * The ICAN3 firmware provides the values of some SJA1000 registers when it
1029  * generates this message. The code below is largely copied from the
1030  * drivers/net/can/sja1000/sja1000.c file, and adapted as necessary
1031  */
1032 static int ican3_handle_cevtind(struct ican3_dev *mod, struct ican3_msg *msg)
1033 {
1034 	struct net_device *dev = mod->ndev;
1035 	struct net_device_stats *stats = &dev->stats;
1036 	enum can_state state = mod->can.state;
1037 	u8 isrc, ecc, status, rxerr, txerr;
1038 	struct can_frame *cf;
1039 	struct sk_buff *skb;
1040 
1041 	/* we can only handle the SJA1000 part */
1042 	if (msg->data[1] != CEVTIND_CHIP_SJA1000) {
1043 		netdev_err(mod->ndev, "unable to handle errors on non-SJA1000\n");
1044 		return -ENODEV;
1045 	}
1046 
1047 	/* check the message length for sanity */
1048 	if (le16_to_cpu(msg->len) < 6) {
1049 		netdev_err(mod->ndev, "error message too short\n");
1050 		return -EINVAL;
1051 	}
1052 
1053 	isrc = msg->data[0];
1054 	ecc = msg->data[2];
1055 	status = msg->data[3];
1056 	rxerr = msg->data[4];
1057 	txerr = msg->data[5];
1058 
1059 	/*
1060 	 * This hardware lacks any support other than bus error messages to
1061 	 * determine if packet transmission has failed.
1062 	 *
1063 	 * When TX errors happen, one echo skb needs to be dropped from the
1064 	 * front of the queue.
1065 	 *
1066 	 * A small bit of code is duplicated here and below, to avoid error
1067 	 * skb allocation when it will just be freed immediately.
1068 	 */
1069 	if (isrc == CEVTIND_BEI) {
1070 		int ret;
1071 		netdev_dbg(mod->ndev, "bus error interrupt\n");
1072 
1073 		/* TX error */
1074 		if (!(ecc & ECC_DIR)) {
1075 			kfree_skb(skb_dequeue(&mod->echoq));
1076 			stats->tx_errors++;
1077 		} else {
1078 			stats->rx_errors++;
1079 		}
1080 
1081 		/*
1082 		 * The controller automatically disables bus-error interrupts
1083 		 * and therefore we must re-enable them.
1084 		 */
1085 		ret = ican3_set_buserror(mod, 1);
1086 		if (ret) {
1087 			netdev_err(mod->ndev, "unable to re-enable bus-error\n");
1088 			return ret;
1089 		}
1090 
1091 		/* bus error reporting is off, return immediately */
1092 		if (!(mod->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING))
1093 			return 0;
1094 	}
1095 
1096 	skb = alloc_can_err_skb(dev, &cf);
1097 	if (skb == NULL)
1098 		return -ENOMEM;
1099 
1100 	/* data overrun interrupt */
1101 	if (isrc == CEVTIND_DOI || isrc == CEVTIND_LOST) {
1102 		netdev_dbg(mod->ndev, "data overrun interrupt\n");
1103 		cf->can_id |= CAN_ERR_CRTL;
1104 		cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
1105 		stats->rx_over_errors++;
1106 		stats->rx_errors++;
1107 	}
1108 
1109 	/* error warning + passive interrupt */
1110 	if (isrc == CEVTIND_EI) {
1111 		netdev_dbg(mod->ndev, "error warning + passive interrupt\n");
1112 		if (status & SR_BS) {
1113 			state = CAN_STATE_BUS_OFF;
1114 			cf->can_id |= CAN_ERR_BUSOFF;
1115 			mod->can.can_stats.bus_off++;
1116 			can_bus_off(dev);
1117 		} else if (status & SR_ES) {
1118 			if (rxerr >= 128 || txerr >= 128)
1119 				state = CAN_STATE_ERROR_PASSIVE;
1120 			else
1121 				state = CAN_STATE_ERROR_WARNING;
1122 		} else {
1123 			state = CAN_STATE_ERROR_ACTIVE;
1124 		}
1125 	}
1126 
1127 	/* bus error interrupt */
1128 	if (isrc == CEVTIND_BEI) {
1129 		mod->can.can_stats.bus_error++;
1130 		cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
1131 
1132 		switch (ecc & ECC_MASK) {
1133 		case ECC_BIT:
1134 			cf->data[2] |= CAN_ERR_PROT_BIT;
1135 			break;
1136 		case ECC_FORM:
1137 			cf->data[2] |= CAN_ERR_PROT_FORM;
1138 			break;
1139 		case ECC_STUFF:
1140 			cf->data[2] |= CAN_ERR_PROT_STUFF;
1141 			break;
1142 		default:
1143 			cf->data[3] = ecc & ECC_SEG;
1144 			break;
1145 		}
1146 
1147 		if (!(ecc & ECC_DIR))
1148 			cf->data[2] |= CAN_ERR_PROT_TX;
1149 
1150 		cf->data[6] = txerr;
1151 		cf->data[7] = rxerr;
1152 	}
1153 
1154 	if (state != mod->can.state && (state == CAN_STATE_ERROR_WARNING ||
1155 					state == CAN_STATE_ERROR_PASSIVE)) {
1156 		cf->can_id |= CAN_ERR_CRTL;
1157 		if (state == CAN_STATE_ERROR_WARNING) {
1158 			mod->can.can_stats.error_warning++;
1159 			cf->data[1] = (txerr > rxerr) ?
1160 				CAN_ERR_CRTL_TX_WARNING :
1161 				CAN_ERR_CRTL_RX_WARNING;
1162 		} else {
1163 			mod->can.can_stats.error_passive++;
1164 			cf->data[1] = (txerr > rxerr) ?
1165 				CAN_ERR_CRTL_TX_PASSIVE :
1166 				CAN_ERR_CRTL_RX_PASSIVE;
1167 		}
1168 
1169 		cf->data[6] = txerr;
1170 		cf->data[7] = rxerr;
1171 	}
1172 
1173 	mod->can.state = state;
1174 	netif_rx(skb);
1175 	return 0;
1176 }
1177 
1178 static void ican3_handle_inquiry(struct ican3_dev *mod, struct ican3_msg *msg)
1179 {
1180 	switch (msg->data[0]) {
1181 	case INQUIRY_STATUS:
1182 	case INQUIRY_EXTENDED:
1183 		mod->bec.rxerr = msg->data[5];
1184 		mod->bec.txerr = msg->data[6];
1185 		complete(&mod->buserror_comp);
1186 		break;
1187 	case INQUIRY_TERMINATION:
1188 		mod->termination_enabled = msg->data[6] & HWCONF_TERMINATE_ON;
1189 		complete(&mod->termination_comp);
1190 		break;
1191 	default:
1192 		netdev_err(mod->ndev, "received an unknown inquiry response\n");
1193 		break;
1194 	}
1195 }
1196 
1197 /* Handle NMTS Slave Event Indication Messages from the firmware */
1198 static void ican3_handle_nmtsind(struct ican3_dev *mod, struct ican3_msg *msg)
1199 {
1200 	u16 subspec;
1201 
1202 	subspec = msg->data[0] + msg->data[1] * 0x100;
1203 	if (subspec == NMTS_SLAVE_EVENT_IND) {
1204 		switch (msg->data[2]) {
1205 		case NE_LOCAL_OCCURRED:
1206 		case NE_LOCAL_RESOLVED:
1207 			/* now follows the same message as Raw ICANOS CEVTIND
1208 			 * shift the data at the same place and call this method
1209 			 */
1210 			le16_add_cpu(&msg->len, -3);
1211 			memmove(msg->data, msg->data + 3, le16_to_cpu(msg->len));
1212 			ican3_handle_cevtind(mod, msg);
1213 			break;
1214 		case NE_REMOTE_OCCURRED:
1215 		case NE_REMOTE_RESOLVED:
1216 			/* should not occurre, ignore */
1217 			break;
1218 		default:
1219 			netdev_warn(mod->ndev, "unknown NMTS event indication %x\n",
1220 				    msg->data[2]);
1221 			break;
1222 		}
1223 	} else if (subspec == NMTS_SLAVE_STATE_IND) {
1224 		/* ignore state indications */
1225 	} else {
1226 		netdev_warn(mod->ndev, "unhandled NMTS indication %x\n",
1227 			    subspec);
1228 		return;
1229 	}
1230 }
1231 
1232 static void ican3_handle_unknown_message(struct ican3_dev *mod,
1233 					struct ican3_msg *msg)
1234 {
1235 	netdev_warn(mod->ndev, "received unknown message: spec 0x%.2x length %d\n",
1236 			   msg->spec, le16_to_cpu(msg->len));
1237 }
1238 
1239 /*
1240  * Handle a control message from the firmware
1241  */
1242 static void ican3_handle_message(struct ican3_dev *mod, struct ican3_msg *msg)
1243 {
1244 	netdev_dbg(mod->ndev, "%s: modno %d spec 0x%.2x len %d bytes\n", __func__,
1245 			   mod->num, msg->spec, le16_to_cpu(msg->len));
1246 
1247 	switch (msg->spec) {
1248 	case MSG_IDVERS:
1249 		ican3_handle_idvers(mod, msg);
1250 		break;
1251 	case MSG_MSGLOST:
1252 	case MSG_FMSGLOST:
1253 		ican3_handle_msglost(mod, msg);
1254 		break;
1255 	case MSG_CEVTIND:
1256 		ican3_handle_cevtind(mod, msg);
1257 		break;
1258 	case MSG_INQUIRY:
1259 		ican3_handle_inquiry(mod, msg);
1260 		break;
1261 	case MSG_NMTS:
1262 		ican3_handle_nmtsind(mod, msg);
1263 		break;
1264 	default:
1265 		ican3_handle_unknown_message(mod, msg);
1266 		break;
1267 	}
1268 }
1269 
1270 /*
1271  * The ican3 needs to store all echo skbs, and therefore cannot
1272  * use the generic infrastructure for this.
1273  */
1274 static void ican3_put_echo_skb(struct ican3_dev *mod, struct sk_buff *skb)
1275 {
1276 	skb = can_create_echo_skb(skb);
1277 	if (!skb)
1278 		return;
1279 
1280 	/* save this skb for tx interrupt echo handling */
1281 	skb_queue_tail(&mod->echoq, skb);
1282 }
1283 
1284 static unsigned int ican3_get_echo_skb(struct ican3_dev *mod)
1285 {
1286 	struct sk_buff *skb = skb_dequeue(&mod->echoq);
1287 	struct can_frame *cf;
1288 	u8 dlc = 0;
1289 
1290 	/* this should never trigger unless there is a driver bug */
1291 	if (!skb) {
1292 		netdev_err(mod->ndev, "BUG: echo skb not occupied\n");
1293 		return 0;
1294 	}
1295 
1296 	cf = (struct can_frame *)skb->data;
1297 	if (!(cf->can_id & CAN_RTR_FLAG))
1298 		dlc = cf->len;
1299 
1300 	/* check flag whether this packet has to be looped back */
1301 	if (skb->pkt_type != PACKET_LOOPBACK) {
1302 		kfree_skb(skb);
1303 		return dlc;
1304 	}
1305 
1306 	skb->protocol = htons(ETH_P_CAN);
1307 	skb->pkt_type = PACKET_BROADCAST;
1308 	skb->ip_summed = CHECKSUM_UNNECESSARY;
1309 	skb->dev = mod->ndev;
1310 	netif_receive_skb(skb);
1311 	return dlc;
1312 }
1313 
1314 /*
1315  * Compare an skb with an existing echo skb
1316  *
1317  * This function will be used on devices which have a hardware loopback.
1318  * On these devices, this function can be used to compare a received skb
1319  * with the saved echo skbs so that the hardware echo skb can be dropped.
1320  *
1321  * Returns true if the skb's are identical, false otherwise.
1322  */
1323 static bool ican3_echo_skb_matches(struct ican3_dev *mod, struct sk_buff *skb)
1324 {
1325 	struct can_frame *cf = (struct can_frame *)skb->data;
1326 	struct sk_buff *echo_skb = skb_peek(&mod->echoq);
1327 	struct can_frame *echo_cf;
1328 
1329 	if (!echo_skb)
1330 		return false;
1331 
1332 	echo_cf = (struct can_frame *)echo_skb->data;
1333 	if (cf->can_id != echo_cf->can_id)
1334 		return false;
1335 
1336 	if (cf->len != echo_cf->len)
1337 		return false;
1338 
1339 	return memcmp(cf->data, echo_cf->data, cf->len) == 0;
1340 }
1341 
1342 /*
1343  * Check that there is room in the TX ring to transmit another skb
1344  *
1345  * LOCKING: must hold mod->lock
1346  */
1347 static bool ican3_txok(struct ican3_dev *mod)
1348 {
1349 	struct ican3_fast_desc __iomem *desc;
1350 	u8 control;
1351 
1352 	/* check that we have echo queue space */
1353 	if (skb_queue_len(&mod->echoq) >= ICAN3_TX_BUFFERS)
1354 		return false;
1355 
1356 	/* copy the control bits of the descriptor */
1357 	ican3_set_page(mod, mod->fasttx_start + (mod->fasttx_num / 16));
1358 	desc = mod->dpm + ((mod->fasttx_num % 16) * sizeof(*desc));
1359 	control = ioread8(&desc->control);
1360 
1361 	/* if the control bits are not valid, then we have no more space */
1362 	if (!(control & DESC_VALID))
1363 		return false;
1364 
1365 	return true;
1366 }
1367 
1368 /*
1369  * Receive one CAN frame from the hardware
1370  *
1371  * CONTEXT: must be called from user context
1372  */
1373 static int ican3_recv_skb(struct ican3_dev *mod)
1374 {
1375 	struct net_device *ndev = mod->ndev;
1376 	struct net_device_stats *stats = &ndev->stats;
1377 	struct ican3_fast_desc desc;
1378 	void __iomem *desc_addr;
1379 	struct can_frame *cf;
1380 	struct sk_buff *skb;
1381 	unsigned long flags;
1382 
1383 	spin_lock_irqsave(&mod->lock, flags);
1384 
1385 	/* copy the whole descriptor */
1386 	ican3_set_page(mod, mod->fastrx_start + (mod->fastrx_num / 16));
1387 	desc_addr = mod->dpm + ((mod->fastrx_num % 16) * sizeof(desc));
1388 	memcpy_fromio(&desc, desc_addr, sizeof(desc));
1389 
1390 	spin_unlock_irqrestore(&mod->lock, flags);
1391 
1392 	/* check that we actually have a CAN frame */
1393 	if (!(desc.control & DESC_VALID))
1394 		return -ENOBUFS;
1395 
1396 	/* allocate an skb */
1397 	skb = alloc_can_skb(ndev, &cf);
1398 	if (unlikely(skb == NULL)) {
1399 		stats->rx_dropped++;
1400 		goto err_noalloc;
1401 	}
1402 
1403 	/* convert the ICAN3 frame into Linux CAN format */
1404 	ican3_to_can_frame(mod, &desc, cf);
1405 
1406 	/*
1407 	 * If this is an ECHO frame received from the hardware loopback
1408 	 * feature, use the skb saved in the ECHO stack instead. This allows
1409 	 * the Linux CAN core to support CAN_RAW_RECV_OWN_MSGS correctly.
1410 	 *
1411 	 * Since this is a confirmation of a successfully transmitted packet
1412 	 * sent from this host, update the transmit statistics.
1413 	 *
1414 	 * Also, the netdevice queue needs to be allowed to send packets again.
1415 	 */
1416 	if (ican3_echo_skb_matches(mod, skb)) {
1417 		stats->tx_packets++;
1418 		stats->tx_bytes += ican3_get_echo_skb(mod);
1419 		kfree_skb(skb);
1420 		goto err_noalloc;
1421 	}
1422 
1423 	/* update statistics, receive the skb */
1424 	stats->rx_packets++;
1425 	if (!(cf->can_id & CAN_RTR_FLAG))
1426 		stats->rx_bytes += cf->len;
1427 	netif_receive_skb(skb);
1428 
1429 err_noalloc:
1430 	/* toggle the valid bit and return the descriptor to the ring */
1431 	desc.control ^= DESC_VALID;
1432 
1433 	spin_lock_irqsave(&mod->lock, flags);
1434 
1435 	ican3_set_page(mod, mod->fastrx_start + (mod->fastrx_num / 16));
1436 	memcpy_toio(desc_addr, &desc, 1);
1437 
1438 	/* update the next buffer pointer */
1439 	mod->fastrx_num = (desc.control & DESC_WRAP) ? 0
1440 						     : (mod->fastrx_num + 1);
1441 
1442 	/* there are still more buffers to process */
1443 	spin_unlock_irqrestore(&mod->lock, flags);
1444 	return 0;
1445 }
1446 
1447 static int ican3_napi(struct napi_struct *napi, int budget)
1448 {
1449 	struct ican3_dev *mod = container_of(napi, struct ican3_dev, napi);
1450 	unsigned long flags;
1451 	int received = 0;
1452 	int ret;
1453 
1454 	/* process all communication messages */
1455 	while (true) {
1456 		struct ican3_msg msg;
1457 		ret = ican3_recv_msg(mod, &msg);
1458 		if (ret)
1459 			break;
1460 
1461 		ican3_handle_message(mod, &msg);
1462 	}
1463 
1464 	/* process all CAN frames from the fast interface */
1465 	while (received < budget) {
1466 		ret = ican3_recv_skb(mod);
1467 		if (ret)
1468 			break;
1469 
1470 		received++;
1471 	}
1472 
1473 	/* We have processed all packets that the adapter had, but it
1474 	 * was less than our budget, stop polling */
1475 	if (received < budget)
1476 		napi_complete_done(napi, received);
1477 
1478 	spin_lock_irqsave(&mod->lock, flags);
1479 
1480 	/* Wake up the transmit queue if necessary */
1481 	if (netif_queue_stopped(mod->ndev) && ican3_txok(mod))
1482 		netif_wake_queue(mod->ndev);
1483 
1484 	spin_unlock_irqrestore(&mod->lock, flags);
1485 
1486 	/* re-enable interrupt generation */
1487 	iowrite8(1 << mod->num, &mod->ctrl->int_enable);
1488 	return received;
1489 }
1490 
1491 static irqreturn_t ican3_irq(int irq, void *dev_id)
1492 {
1493 	struct ican3_dev *mod = dev_id;
1494 	u8 stat;
1495 
1496 	/*
1497 	 * The interrupt status register on this device reports interrupts
1498 	 * as zeroes instead of using ones like most other devices
1499 	 */
1500 	stat = ioread8(&mod->ctrl->int_disable) & (1 << mod->num);
1501 	if (stat == (1 << mod->num))
1502 		return IRQ_NONE;
1503 
1504 	/* clear the MODULbus interrupt from the microcontroller */
1505 	ioread8(&mod->dpmctrl->interrupt);
1506 
1507 	/* disable interrupt generation, schedule the NAPI poller */
1508 	iowrite8(1 << mod->num, &mod->ctrl->int_disable);
1509 	napi_schedule(&mod->napi);
1510 	return IRQ_HANDLED;
1511 }
1512 
1513 /*
1514  * Firmware reset, startup, and shutdown
1515  */
1516 
1517 /*
1518  * Reset an ICAN module to its power-on state
1519  *
1520  * CONTEXT: no network device registered
1521  */
1522 static int ican3_reset_module(struct ican3_dev *mod)
1523 {
1524 	unsigned long start;
1525 	u8 runold, runnew;
1526 
1527 	/* disable interrupts so no more work is scheduled */
1528 	iowrite8(1 << mod->num, &mod->ctrl->int_disable);
1529 
1530 	/* the first unallocated page in the DPM is #9 */
1531 	mod->free_page = DPM_FREE_START;
1532 
1533 	ican3_set_page(mod, QUEUE_OLD_CONTROL);
1534 	runold = ioread8(mod->dpm + TARGET_RUNNING);
1535 
1536 	/* reset the module */
1537 	iowrite8(0x00, &mod->dpmctrl->hwreset);
1538 
1539 	/* wait until the module has finished resetting and is running */
1540 	start = jiffies;
1541 	do {
1542 		ican3_set_page(mod, QUEUE_OLD_CONTROL);
1543 		runnew = ioread8(mod->dpm + TARGET_RUNNING);
1544 		if (runnew == (runold ^ 0xff))
1545 			return 0;
1546 
1547 		msleep(10);
1548 	} while (time_before(jiffies, start + HZ / 2));
1549 
1550 	netdev_err(mod->ndev, "failed to reset CAN module\n");
1551 	return -ETIMEDOUT;
1552 }
1553 
1554 static void ican3_shutdown_module(struct ican3_dev *mod)
1555 {
1556 	ican3_msg_disconnect(mod);
1557 	ican3_reset_module(mod);
1558 }
1559 
1560 /*
1561  * Startup an ICAN module, bringing it into fast mode
1562  */
1563 static int ican3_startup_module(struct ican3_dev *mod)
1564 {
1565 	int ret;
1566 
1567 	ret = ican3_reset_module(mod);
1568 	if (ret) {
1569 		netdev_err(mod->ndev, "unable to reset module\n");
1570 		return ret;
1571 	}
1572 
1573 	/* detect firmware */
1574 	memcpy_fromio(mod->fwinfo, mod->dpm + FIRMWARE_STAMP, sizeof(mod->fwinfo) - 1);
1575 	if (strncmp(mod->fwinfo, "JANZ-ICAN3", 10)) {
1576 		netdev_err(mod->ndev, "ICAN3 not detected (found %s)\n", mod->fwinfo);
1577 		return -ENODEV;
1578 	}
1579 	if (strstr(mod->fwinfo, "CAL/CANopen"))
1580 		mod->fwtype = ICAN3_FWTYPE_CAL_CANOPEN;
1581 	else
1582 		mod->fwtype = ICAN3_FWTYPE_ICANOS;
1583 
1584 	/* re-enable interrupts so we can send messages */
1585 	iowrite8(1 << mod->num, &mod->ctrl->int_enable);
1586 
1587 	ret = ican3_msg_connect(mod);
1588 	if (ret) {
1589 		netdev_err(mod->ndev, "unable to connect to module\n");
1590 		return ret;
1591 	}
1592 
1593 	ican3_init_new_host_interface(mod);
1594 	ret = ican3_msg_newhostif(mod);
1595 	if (ret) {
1596 		netdev_err(mod->ndev, "unable to switch to new-style interface\n");
1597 		return ret;
1598 	}
1599 
1600 	/* default to "termination on" */
1601 	ret = ican3_set_termination(mod, true);
1602 	if (ret) {
1603 		netdev_err(mod->ndev, "unable to enable termination\n");
1604 		return ret;
1605 	}
1606 
1607 	/* default to "bus errors enabled" */
1608 	ret = ican3_set_buserror(mod, 1);
1609 	if (ret) {
1610 		netdev_err(mod->ndev, "unable to set bus-error\n");
1611 		return ret;
1612 	}
1613 
1614 	ican3_init_fast_host_interface(mod);
1615 	ret = ican3_msg_fasthostif(mod);
1616 	if (ret) {
1617 		netdev_err(mod->ndev, "unable to switch to fast host interface\n");
1618 		return ret;
1619 	}
1620 
1621 	ret = ican3_set_id_filter(mod, true);
1622 	if (ret) {
1623 		netdev_err(mod->ndev, "unable to set acceptance filter\n");
1624 		return ret;
1625 	}
1626 
1627 	return 0;
1628 }
1629 
1630 /*
1631  * CAN Network Device
1632  */
1633 
1634 static int ican3_open(struct net_device *ndev)
1635 {
1636 	struct ican3_dev *mod = netdev_priv(ndev);
1637 	int ret;
1638 
1639 	/* open the CAN layer */
1640 	ret = open_candev(ndev);
1641 	if (ret) {
1642 		netdev_err(mod->ndev, "unable to start CAN layer\n");
1643 		return ret;
1644 	}
1645 
1646 	/* bring the bus online */
1647 	ret = ican3_set_bus_state(mod, true);
1648 	if (ret) {
1649 		netdev_err(mod->ndev, "unable to set bus-on\n");
1650 		close_candev(ndev);
1651 		return ret;
1652 	}
1653 
1654 	/* start up the network device */
1655 	mod->can.state = CAN_STATE_ERROR_ACTIVE;
1656 	netif_start_queue(ndev);
1657 
1658 	return 0;
1659 }
1660 
1661 static int ican3_stop(struct net_device *ndev)
1662 {
1663 	struct ican3_dev *mod = netdev_priv(ndev);
1664 	int ret;
1665 
1666 	/* stop the network device xmit routine */
1667 	netif_stop_queue(ndev);
1668 	mod->can.state = CAN_STATE_STOPPED;
1669 
1670 	/* bring the bus offline, stop receiving packets */
1671 	ret = ican3_set_bus_state(mod, false);
1672 	if (ret) {
1673 		netdev_err(mod->ndev, "unable to set bus-off\n");
1674 		return ret;
1675 	}
1676 
1677 	/* drop all outstanding echo skbs */
1678 	skb_queue_purge(&mod->echoq);
1679 
1680 	/* close the CAN layer */
1681 	close_candev(ndev);
1682 	return 0;
1683 }
1684 
1685 static netdev_tx_t ican3_xmit(struct sk_buff *skb, struct net_device *ndev)
1686 {
1687 	struct ican3_dev *mod = netdev_priv(ndev);
1688 	struct can_frame *cf = (struct can_frame *)skb->data;
1689 	struct ican3_fast_desc desc;
1690 	void __iomem *desc_addr;
1691 	unsigned long flags;
1692 
1693 	if (can_dropped_invalid_skb(ndev, skb))
1694 		return NETDEV_TX_OK;
1695 
1696 	spin_lock_irqsave(&mod->lock, flags);
1697 
1698 	/* check that we can actually transmit */
1699 	if (!ican3_txok(mod)) {
1700 		netdev_err(mod->ndev, "BUG: no free descriptors\n");
1701 		spin_unlock_irqrestore(&mod->lock, flags);
1702 		return NETDEV_TX_BUSY;
1703 	}
1704 
1705 	/* copy the control bits of the descriptor */
1706 	ican3_set_page(mod, mod->fasttx_start + (mod->fasttx_num / 16));
1707 	desc_addr = mod->dpm + ((mod->fasttx_num % 16) * sizeof(desc));
1708 	memset(&desc, 0, sizeof(desc));
1709 	memcpy_fromio(&desc, desc_addr, 1);
1710 
1711 	/* convert the Linux CAN frame into ICAN3 format */
1712 	can_frame_to_ican3(mod, cf, &desc);
1713 
1714 	/*
1715 	 * This hardware doesn't have TX-done notifications, so we'll try and
1716 	 * emulate it the best we can using ECHO skbs. Add the skb to the ECHO
1717 	 * stack. Upon packet reception, check if the ECHO skb and received
1718 	 * skb match, and use that to wake the queue.
1719 	 */
1720 	ican3_put_echo_skb(mod, skb);
1721 
1722 	/*
1723 	 * the programming manual says that you must set the IVALID bit, then
1724 	 * interrupt, then set the valid bit. Quite weird, but it seems to be
1725 	 * required for this to work
1726 	 */
1727 	desc.control |= DESC_IVALID;
1728 	memcpy_toio(desc_addr, &desc, sizeof(desc));
1729 
1730 	/* generate a MODULbus interrupt to the microcontroller */
1731 	iowrite8(0x01, &mod->dpmctrl->interrupt);
1732 
1733 	desc.control ^= DESC_VALID;
1734 	memcpy_toio(desc_addr, &desc, sizeof(desc));
1735 
1736 	/* update the next buffer pointer */
1737 	mod->fasttx_num = (desc.control & DESC_WRAP) ? 0
1738 						     : (mod->fasttx_num + 1);
1739 
1740 	/* if there is no free descriptor space, stop the transmit queue */
1741 	if (!ican3_txok(mod))
1742 		netif_stop_queue(ndev);
1743 
1744 	spin_unlock_irqrestore(&mod->lock, flags);
1745 	return NETDEV_TX_OK;
1746 }
1747 
1748 static const struct net_device_ops ican3_netdev_ops = {
1749 	.ndo_open	= ican3_open,
1750 	.ndo_stop	= ican3_stop,
1751 	.ndo_start_xmit	= ican3_xmit,
1752 	.ndo_change_mtu = can_change_mtu,
1753 };
1754 
1755 /*
1756  * Low-level CAN Device
1757  */
1758 
1759 /* This structure was stolen from drivers/net/can/sja1000/sja1000.c */
1760 static const struct can_bittiming_const ican3_bittiming_const = {
1761 	.name = DRV_NAME,
1762 	.tseg1_min = 1,
1763 	.tseg1_max = 16,
1764 	.tseg2_min = 1,
1765 	.tseg2_max = 8,
1766 	.sjw_max = 4,
1767 	.brp_min = 1,
1768 	.brp_max = 64,
1769 	.brp_inc = 1,
1770 };
1771 
1772 static int ican3_set_mode(struct net_device *ndev, enum can_mode mode)
1773 {
1774 	struct ican3_dev *mod = netdev_priv(ndev);
1775 	int ret;
1776 
1777 	if (mode != CAN_MODE_START)
1778 		return -ENOTSUPP;
1779 
1780 	/* bring the bus online */
1781 	ret = ican3_set_bus_state(mod, true);
1782 	if (ret) {
1783 		netdev_err(ndev, "unable to set bus-on\n");
1784 		return ret;
1785 	}
1786 
1787 	/* start up the network device */
1788 	mod->can.state = CAN_STATE_ERROR_ACTIVE;
1789 
1790 	if (netif_queue_stopped(ndev))
1791 		netif_wake_queue(ndev);
1792 
1793 	return 0;
1794 }
1795 
1796 static int ican3_get_berr_counter(const struct net_device *ndev,
1797 				  struct can_berr_counter *bec)
1798 {
1799 	struct ican3_dev *mod = netdev_priv(ndev);
1800 	int ret;
1801 
1802 	ret = ican3_send_inquiry(mod, INQUIRY_STATUS);
1803 	if (ret)
1804 		return ret;
1805 
1806 	if (!wait_for_completion_timeout(&mod->buserror_comp, HZ)) {
1807 		netdev_info(mod->ndev, "%s timed out\n", __func__);
1808 		return -ETIMEDOUT;
1809 	}
1810 
1811 	bec->rxerr = mod->bec.rxerr;
1812 	bec->txerr = mod->bec.txerr;
1813 	return 0;
1814 }
1815 
1816 /*
1817  * Sysfs Attributes
1818  */
1819 
1820 static ssize_t termination_show(struct device *dev,
1821 				struct device_attribute *attr,
1822 				char *buf)
1823 {
1824 	struct ican3_dev *mod = netdev_priv(to_net_dev(dev));
1825 	int ret;
1826 
1827 	ret = ican3_send_inquiry(mod, INQUIRY_TERMINATION);
1828 	if (ret)
1829 		return ret;
1830 
1831 	if (!wait_for_completion_timeout(&mod->termination_comp, HZ)) {
1832 		netdev_info(mod->ndev, "%s timed out\n", __func__);
1833 		return -ETIMEDOUT;
1834 	}
1835 
1836 	return sysfs_emit(buf, "%u\n", mod->termination_enabled);
1837 }
1838 
1839 static ssize_t termination_store(struct device *dev,
1840 				 struct device_attribute *attr,
1841 				 const char *buf, size_t count)
1842 {
1843 	struct ican3_dev *mod = netdev_priv(to_net_dev(dev));
1844 	unsigned long enable;
1845 	int ret;
1846 
1847 	if (kstrtoul(buf, 0, &enable))
1848 		return -EINVAL;
1849 
1850 	ret = ican3_set_termination(mod, enable);
1851 	if (ret)
1852 		return ret;
1853 
1854 	return count;
1855 }
1856 
1857 static ssize_t fwinfo_show(struct device *dev,
1858 			   struct device_attribute *attr,
1859 			   char *buf)
1860 {
1861 	struct ican3_dev *mod = netdev_priv(to_net_dev(dev));
1862 
1863 	return scnprintf(buf, PAGE_SIZE, "%s\n", mod->fwinfo);
1864 }
1865 
1866 static DEVICE_ATTR_RW(termination);
1867 static DEVICE_ATTR_RO(fwinfo);
1868 
1869 static struct attribute *ican3_sysfs_attrs[] = {
1870 	&dev_attr_termination.attr,
1871 	&dev_attr_fwinfo.attr,
1872 	NULL,
1873 };
1874 
1875 static const struct attribute_group ican3_sysfs_attr_group = {
1876 	.attrs = ican3_sysfs_attrs,
1877 };
1878 
1879 /*
1880  * PCI Subsystem
1881  */
1882 
1883 static int ican3_probe(struct platform_device *pdev)
1884 {
1885 	struct janz_platform_data *pdata;
1886 	struct net_device *ndev;
1887 	struct ican3_dev *mod;
1888 	struct resource *res;
1889 	struct device *dev;
1890 	int ret;
1891 
1892 	pdata = dev_get_platdata(&pdev->dev);
1893 	if (!pdata)
1894 		return -ENXIO;
1895 
1896 	dev_dbg(&pdev->dev, "probe: module number %d\n", pdata->modno);
1897 
1898 	/* save the struct device for printing */
1899 	dev = &pdev->dev;
1900 
1901 	/* allocate the CAN device and private data */
1902 	ndev = alloc_candev(sizeof(*mod), 0);
1903 	if (!ndev) {
1904 		dev_err(dev, "unable to allocate CANdev\n");
1905 		ret = -ENOMEM;
1906 		goto out_return;
1907 	}
1908 
1909 	platform_set_drvdata(pdev, ndev);
1910 	mod = netdev_priv(ndev);
1911 	mod->ndev = ndev;
1912 	mod->num = pdata->modno;
1913 	netif_napi_add_weight(ndev, &mod->napi, ican3_napi, ICAN3_RX_BUFFERS);
1914 	skb_queue_head_init(&mod->echoq);
1915 	spin_lock_init(&mod->lock);
1916 	init_completion(&mod->termination_comp);
1917 	init_completion(&mod->buserror_comp);
1918 
1919 	/* setup device-specific sysfs attributes */
1920 	ndev->sysfs_groups[0] = &ican3_sysfs_attr_group;
1921 
1922 	/* the first unallocated page in the DPM is 9 */
1923 	mod->free_page = DPM_FREE_START;
1924 
1925 	ndev->netdev_ops = &ican3_netdev_ops;
1926 	ndev->flags |= IFF_ECHO;
1927 	SET_NETDEV_DEV(ndev, &pdev->dev);
1928 
1929 	mod->can.clock.freq = ICAN3_CAN_CLOCK;
1930 	mod->can.bittiming_const = &ican3_bittiming_const;
1931 	mod->can.do_set_mode = ican3_set_mode;
1932 	mod->can.do_get_berr_counter = ican3_get_berr_counter;
1933 	mod->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES
1934 				    | CAN_CTRLMODE_BERR_REPORTING
1935 				    | CAN_CTRLMODE_ONE_SHOT;
1936 
1937 	/* find our IRQ number */
1938 	mod->irq = platform_get_irq(pdev, 0);
1939 	if (mod->irq < 0) {
1940 		ret = -ENODEV;
1941 		goto out_free_ndev;
1942 	}
1943 
1944 	ndev->irq = mod->irq;
1945 
1946 	/* get access to the MODULbus registers for this module */
1947 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1948 	if (!res) {
1949 		dev_err(dev, "MODULbus registers not found\n");
1950 		ret = -ENODEV;
1951 		goto out_free_ndev;
1952 	}
1953 
1954 	mod->dpm = ioremap(res->start, resource_size(res));
1955 	if (!mod->dpm) {
1956 		dev_err(dev, "MODULbus registers not ioremap\n");
1957 		ret = -ENOMEM;
1958 		goto out_free_ndev;
1959 	}
1960 
1961 	mod->dpmctrl = mod->dpm + DPM_PAGE_SIZE;
1962 
1963 	/* get access to the control registers for this module */
1964 	res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1965 	if (!res) {
1966 		dev_err(dev, "CONTROL registers not found\n");
1967 		ret = -ENODEV;
1968 		goto out_iounmap_dpm;
1969 	}
1970 
1971 	mod->ctrl = ioremap(res->start, resource_size(res));
1972 	if (!mod->ctrl) {
1973 		dev_err(dev, "CONTROL registers not ioremap\n");
1974 		ret = -ENOMEM;
1975 		goto out_iounmap_dpm;
1976 	}
1977 
1978 	/* disable our IRQ, then hookup the IRQ handler */
1979 	iowrite8(1 << mod->num, &mod->ctrl->int_disable);
1980 	ret = request_irq(mod->irq, ican3_irq, IRQF_SHARED, DRV_NAME, mod);
1981 	if (ret) {
1982 		dev_err(dev, "unable to request IRQ\n");
1983 		goto out_iounmap_ctrl;
1984 	}
1985 
1986 	/* reset and initialize the CAN controller into fast mode */
1987 	napi_enable(&mod->napi);
1988 	ret = ican3_startup_module(mod);
1989 	if (ret) {
1990 		dev_err(dev, "%s: unable to start CANdev\n", __func__);
1991 		goto out_free_irq;
1992 	}
1993 
1994 	/* register with the Linux CAN layer */
1995 	ret = register_candev(ndev);
1996 	if (ret) {
1997 		dev_err(dev, "%s: unable to register CANdev\n", __func__);
1998 		goto out_free_irq;
1999 	}
2000 
2001 	netdev_info(mod->ndev, "module %d: registered CAN device\n", pdata->modno);
2002 	return 0;
2003 
2004 out_free_irq:
2005 	napi_disable(&mod->napi);
2006 	iowrite8(1 << mod->num, &mod->ctrl->int_disable);
2007 	free_irq(mod->irq, mod);
2008 out_iounmap_ctrl:
2009 	iounmap(mod->ctrl);
2010 out_iounmap_dpm:
2011 	iounmap(mod->dpm);
2012 out_free_ndev:
2013 	free_candev(ndev);
2014 out_return:
2015 	return ret;
2016 }
2017 
2018 static int ican3_remove(struct platform_device *pdev)
2019 {
2020 	struct net_device *ndev = platform_get_drvdata(pdev);
2021 	struct ican3_dev *mod = netdev_priv(ndev);
2022 
2023 	/* unregister the netdevice, stop interrupts */
2024 	unregister_netdev(ndev);
2025 	napi_disable(&mod->napi);
2026 	iowrite8(1 << mod->num, &mod->ctrl->int_disable);
2027 	free_irq(mod->irq, mod);
2028 
2029 	/* put the module into reset */
2030 	ican3_shutdown_module(mod);
2031 
2032 	/* unmap all registers */
2033 	iounmap(mod->ctrl);
2034 	iounmap(mod->dpm);
2035 
2036 	free_candev(ndev);
2037 
2038 	return 0;
2039 }
2040 
2041 static struct platform_driver ican3_driver = {
2042 	.driver		= {
2043 		.name	= DRV_NAME,
2044 	},
2045 	.probe		= ican3_probe,
2046 	.remove		= ican3_remove,
2047 };
2048 
2049 module_platform_driver(ican3_driver);
2050 
2051 MODULE_AUTHOR("Ira W. Snyder <iws@ovro.caltech.edu>");
2052 MODULE_DESCRIPTION("Janz MODULbus VMOD-ICAN3 Driver");
2053 MODULE_LICENSE("GPL");
2054 MODULE_ALIAS("platform:janz-ican3");
2055