1
2
3
4
5
6
7
8
9
10
11
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>
17
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>
23
24#include <linux/mfd/janz.h>
25#include <asm/io.h>
26
27
28#define DPM_NUM_PAGES 256
29#define DPM_PAGE_SIZE 256
30#define DPM_PAGE_ADDR(p) ((p) * DPM_PAGE_SIZE)
31
32
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
38
39
40#define MSYNC_PEER 0x00
41#define MSYNC_LOCL 0x01
42#define TARGET_RUNNING 0x02
43#define FIRMWARE_STAMP 0x60
44
45#define MSYNC_RB0 0x01
46#define MSYNC_RB1 0x02
47#define MSYNC_RBLW 0x04
48#define MSYNC_RB_MASK (MSYNC_RB0 | MSYNC_RB1)
49
50#define MSYNC_WB0 0x10
51#define MSYNC_WB1 0x20
52#define MSYNC_WBLW 0x40
53#define MSYNC_WB_MASK (MSYNC_WB0 | MSYNC_WB1)
54
55
56#define QUEUE_TOHOST 5
57#define QUEUE_FROMHOST_MID 6
58#define QUEUE_FROMHOST_HIGH 7
59#define QUEUE_FROMHOST_LOW 8
60
61
62#define DPM_FREE_START 9
63
64
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)
70
71
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
87#define MSG_NMTS 0xb0
88#define MSG_LMTS 0xb4
89
90
91
92
93
94
95
96
97
98#define INQUIRY_STATUS 0x00
99#define INQUIRY_TERMINATION 0x01
100#define INQUIRY_EXTENDED 0x04
101
102
103#define SETAFILMASK_REJECT 0x00
104#define SETAFILMASK_FASTIF 0x02
105
106
107#define HWCONF_TERMINATE_ON 0x01
108#define HWCONF_TERMINATE_OFF 0x00
109
110
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
116
117#define CEVTIND_CHIP_SJA1000 0x02
118
119#define ICAN3_BUSERR_QUOTA_MAX 255
120
121
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
127
128#define ICAN3_CAN_TYPE_MASK 0x0f
129#define ICAN3_CAN_TYPE_SFF 0x00
130#define ICAN3_CAN_TYPE_EFF 0x01
131
132#define ICAN3_CAN_DLC_MASK 0x0f
133
134
135#define NMTS_CREATE_NODE_REQ 0x0
136#define NMTS_SLAVE_STATE_IND 0x8
137#define NMTS_SLAVE_EVENT_IND 0x9
138
139
140#define LMTS_BUSON_REQ 0x0
141#define LMTS_BUSOFF_REQ 0x1
142#define LMTS_CAN_CONF_REQ 0x2
143
144
145#define NE_LOCAL_OCCURRED 0x3
146#define NE_LOCAL_RESOLVED 0x2
147#define NE_REMOTE_OCCURRED 0xc
148#define NE_REMOTE_RESOLVED 0x8
149
150
151
152
153
154
155
156
157#define SR_BS 0x80
158#define SR_ES 0x40
159#define SR_TS 0x20
160#define SR_RS 0x10
161#define SR_TCS 0x08
162#define SR_TBS 0x04
163#define SR_DOS 0x02
164#define SR_RBS 0x01
165
166#define SR_CRIT (SR_BS|SR_ES)
167
168
169#define ECC_SEG 0x1F
170#define ECC_DIR 0x20
171#define ECC_ERR 6
172#define ECC_BIT 0x00
173#define ECC_FORM 0x40
174#define ECC_STUFF 0x80
175#define ECC_MASK 0xc0
176
177
178#define ICAN3_NEW_BUFFERS 16
179
180
181#define ICAN3_TX_BUFFERS 512
182#define ICAN3_RX_BUFFERS 1024
183
184
185#define ICAN3_CAN_CLOCK 8000000
186
187
188enum ican3_fwtype {
189 ICAN3_FWTYPE_ICANOS,
190 ICAN3_FWTYPE_CAL_CANOPEN,
191};
192
193
194#define DRV_NAME "janz-ican3"
195
196
197struct ican3_dpm_control {
198
199 u8 window_address;
200 u8 unused1;
201
202
203
204
205
206 u8 interrupt;
207 u8 unused2;
208
209
210 u8 hwreset;
211 u8 unused3;
212
213
214 u8 tpuinterrupt;
215};
216
217struct ican3_dev {
218
219
220 struct can_priv can;
221
222
223 struct net_device *ndev;
224 struct napi_struct napi;
225
226
227 unsigned int num;
228
229
230 struct janz_cmodio_onboard_regs __iomem *ctrl;
231 struct ican3_dpm_control __iomem *dpmctrl;
232 void __iomem *dpm;
233 int irq;
234
235
236 struct completion termination_comp;
237 bool termination_enabled;
238
239
240 struct completion buserror_comp;
241 struct can_berr_counter bec;
242
243
244 enum ican3_fwtype fwtype;
245 char fwinfo[32];
246
247
248 unsigned int iftype;
249
250
251 struct sk_buff_head echoq;
252
253
254
255
256
257
258
259 spinlock_t lock;
260
261
262 unsigned int rx_int;
263 unsigned int rx_num;
264 unsigned int tx_num;
265
266
267 unsigned int fastrx_start;
268 unsigned int fastrx_num;
269 unsigned int fasttx_start;
270 unsigned int fasttx_num;
271
272
273 unsigned int free_page;
274};
275
276struct ican3_msg {
277 u8 control;
278 u8 spec;
279 __le16 len;
280 u8 data[252];
281};
282
283struct ican3_new_desc {
284 u8 control;
285 u8 pointer;
286};
287
288struct ican3_fast_desc {
289 u8 control;
290 u8 command;
291 u8 data[14];
292};
293
294
295static inline void ican3_set_page(struct ican3_dev *mod, unsigned int page)
296{
297 BUG_ON(page >= DPM_NUM_PAGES);
298 iowrite8(page, &mod->dpmctrl->window_address);
299}
300
301
302
303
304
305
306
307
308
309
310
311
312static int ican3_old_recv_msg(struct ican3_dev *mod, struct ican3_msg *msg)
313{
314 unsigned int mbox, mbox_page;
315 u8 locl, peer, xord;
316
317
318 ican3_set_page(mod, QUEUE_OLD_CONTROL);
319 peer = ioread8(mod->dpm + MSYNC_PEER);
320 locl = ioread8(mod->dpm + MSYNC_LOCL);
321 xord = locl ^ peer;
322
323 if ((xord & MSYNC_RB_MASK) == 0x00) {
324 netdev_dbg(mod->ndev, "no mbox for reading\n");
325 return -ENOMEM;
326 }
327
328
329 if ((xord & MSYNC_RB_MASK) == MSYNC_RB_MASK)
330 mbox = (xord & MSYNC_RBLW) ? MSYNC_RB0 : MSYNC_RB1;
331 else
332 mbox = (xord & MSYNC_RB0) ? MSYNC_RB0 : MSYNC_RB1;
333
334
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));
338
339
340
341
342
343 locl ^= mbox;
344
345 ican3_set_page(mod, QUEUE_OLD_CONTROL);
346 iowrite8(locl, mod->dpm + MSYNC_LOCL);
347 return 0;
348}
349
350
351
352
353
354
355
356
357static int ican3_old_send_msg(struct ican3_dev *mod, struct ican3_msg *msg)
358{
359 unsigned int mbox, mbox_page;
360 u8 locl, peer, xord;
361
362
363 ican3_set_page(mod, QUEUE_OLD_CONTROL);
364 peer = ioread8(mod->dpm + MSYNC_PEER);
365 locl = ioread8(mod->dpm + MSYNC_LOCL);
366 xord = locl ^ peer;
367
368 if ((xord & MSYNC_WB_MASK) == MSYNC_WB_MASK) {
369 netdev_err(mod->ndev, "no mbox for writing\n");
370 return -ENOMEM;
371 }
372
373
374 mbox = (xord & MSYNC_WB0) ? MSYNC_WB1 : MSYNC_WB0;
375
376
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));
380
381 locl ^= mbox;
382 if (mbox == MSYNC_WB1)
383 locl |= MSYNC_WBLW;
384
385 ican3_set_page(mod, QUEUE_OLD_CONTROL);
386 iowrite8(locl, mod->dpm + MSYNC_LOCL);
387 return 0;
388}
389
390
391
392
393
394static void ican3_init_new_host_interface(struct ican3_dev *mod)
395{
396 struct ican3_new_desc desc;
397 unsigned long flags;
398 void __iomem *dst;
399 int i;
400
401 spin_lock_irqsave(&mod->lock, flags);
402
403
404 mod->rx_num = 0;
405 mod->rx_int = 0;
406
407
408 ican3_set_page(mod, QUEUE_TOHOST);
409 dst = mod->dpm;
410
411
412 for (i = 0; i < ICAN3_NEW_BUFFERS; i++) {
413 desc.control = DESC_INTERRUPT | DESC_LEN(1);
414 desc.pointer = mod->free_page;
415
416
417 if (i == ICAN3_NEW_BUFFERS - 1)
418 desc.control |= DESC_WRAP;
419
420 memcpy_toio(dst, &desc, sizeof(desc));
421 dst += sizeof(desc);
422 mod->free_page++;
423 }
424
425
426 ican3_set_page(mod, QUEUE_FROMHOST_MID);
427 dst = mod->dpm;
428
429
430 mod->tx_num = 0;
431
432
433 for (i = 0; i < ICAN3_NEW_BUFFERS; i++) {
434 desc.control = DESC_VALID | DESC_LEN(1);
435 desc.pointer = mod->free_page;
436
437
438 if (i == ICAN3_NEW_BUFFERS - 1)
439 desc.control |= DESC_WRAP;
440
441 memcpy_toio(dst, &desc, sizeof(desc));
442 dst += sizeof(desc);
443 mod->free_page++;
444 }
445
446
447 ican3_set_page(mod, QUEUE_FROMHOST_HIGH);
448 dst = mod->dpm;
449
450
451 desc.control = DESC_VALID | DESC_WRAP | DESC_LEN(1);
452 desc.pointer = mod->free_page;
453 memcpy_toio(dst, &desc, sizeof(desc));
454 mod->free_page++;
455
456
457 ican3_set_page(mod, QUEUE_FROMHOST_LOW);
458 dst = mod->dpm;
459
460
461 desc.control = DESC_VALID | DESC_WRAP | DESC_LEN(1);
462 desc.pointer = mod->free_page;
463 memcpy_toio(dst, &desc, sizeof(desc));
464 mod->free_page++;
465
466 spin_unlock_irqrestore(&mod->lock, flags);
467}
468
469
470
471
472
473static void ican3_init_fast_host_interface(struct ican3_dev *mod)
474{
475 struct ican3_fast_desc desc;
476 unsigned long flags;
477 unsigned int addr;
478 void __iomem *dst;
479 int i;
480
481 spin_lock_irqsave(&mod->lock, flags);
482
483
484 mod->fastrx_start = mod->free_page;
485 mod->fastrx_num = 0;
486
487
488 memset(&desc, 0, sizeof(desc));
489 desc.control = 0x00;
490 desc.command = 1;
491
492
493 addr = 0;
494 for (i = 0; i < ICAN3_RX_BUFFERS; i++) {
495
496
497 if (i == ICAN3_RX_BUFFERS - 1)
498 desc.control |= DESC_WRAP;
499
500
501 ican3_set_page(mod, mod->free_page);
502
503
504 dst = mod->dpm + addr;
505 memcpy_toio(dst, &desc, sizeof(desc));
506 addr += sizeof(desc);
507
508
509 if (addr >= DPM_PAGE_SIZE) {
510 addr = 0;
511 mod->free_page++;
512 }
513 }
514
515
516 if (addr != 0)
517 mod->free_page++;
518
519
520 mod->fasttx_start = mod->free_page;
521 mod->fasttx_num = 0;
522
523
524 memset(&desc, 0, sizeof(desc));
525 desc.control = DESC_VALID;
526 desc.command = 1;
527
528
529 addr = 0;
530 for (i = 0; i < ICAN3_TX_BUFFERS; i++) {
531
532
533 if (i == ICAN3_TX_BUFFERS - 1)
534 desc.control |= DESC_WRAP;
535
536
537 ican3_set_page(mod, mod->free_page);
538
539
540 dst = mod->dpm + addr;
541 memcpy_toio(dst, &desc, sizeof(desc));
542 addr += sizeof(desc);
543
544
545 if (addr >= DPM_PAGE_SIZE) {
546 addr = 0;
547 mod->free_page++;
548 }
549 }
550
551 spin_unlock_irqrestore(&mod->lock, flags);
552}
553
554
555
556
557
558
559
560
561static int ican3_new_send_msg(struct ican3_dev *mod, struct ican3_msg *msg)
562{
563 struct ican3_new_desc desc;
564 void __iomem *desc_addr = mod->dpm + (mod->tx_num * sizeof(desc));
565
566
567 ican3_set_page(mod, QUEUE_FROMHOST_MID);
568 memcpy_fromio(&desc, desc_addr, sizeof(desc));
569
570 if (!(desc.control & DESC_VALID)) {
571 netdev_dbg(mod->ndev, "%s: no free buffers\n", __func__);
572 return -ENOMEM;
573 }
574
575
576 ican3_set_page(mod, desc.pointer);
577 memcpy_toio(mod->dpm, msg, sizeof(*msg));
578
579
580 ican3_set_page(mod, QUEUE_FROMHOST_MID);
581 desc.control ^= DESC_VALID;
582 memcpy_toio(desc_addr, &desc, sizeof(desc));
583
584
585 mod->tx_num = (desc.control & DESC_WRAP) ? 0 : (mod->tx_num + 1);
586 return 0;
587}
588
589
590
591
592static int ican3_new_recv_msg(struct ican3_dev *mod, struct ican3_msg *msg)
593{
594 struct ican3_new_desc desc;
595 void __iomem *desc_addr = mod->dpm + (mod->rx_num * sizeof(desc));
596
597
598 ican3_set_page(mod, QUEUE_TOHOST);
599 memcpy_fromio(&desc, desc_addr, sizeof(desc));
600
601 if (!(desc.control & DESC_VALID)) {
602 netdev_dbg(mod->ndev, "%s: no buffers to recv\n", __func__);
603 return -ENOMEM;
604 }
605
606
607 ican3_set_page(mod, desc.pointer);
608 memcpy_fromio(msg, mod->dpm, sizeof(*msg));
609
610
611 ican3_set_page(mod, QUEUE_TOHOST);
612 desc.control ^= DESC_VALID;
613 memcpy_toio(desc_addr, &desc, sizeof(desc));
614
615
616 mod->rx_num = (desc.control & DESC_WRAP) ? 0 : (mod->rx_num + 1);
617 return 0;
618}
619
620
621
622
623
624static int ican3_send_msg(struct ican3_dev *mod, struct ican3_msg *msg)
625{
626 unsigned long flags;
627 int ret;
628
629 spin_lock_irqsave(&mod->lock, flags);
630
631 if (mod->iftype == 0)
632 ret = ican3_old_send_msg(mod, msg);
633 else
634 ret = ican3_new_send_msg(mod, msg);
635
636 spin_unlock_irqrestore(&mod->lock, flags);
637 return ret;
638}
639
640static int ican3_recv_msg(struct ican3_dev *mod, struct ican3_msg *msg)
641{
642 unsigned long flags;
643 int ret;
644
645 spin_lock_irqsave(&mod->lock, flags);
646
647 if (mod->iftype == 0)
648 ret = ican3_old_recv_msg(mod, msg);
649 else
650 ret = ican3_new_recv_msg(mod, msg);
651
652 spin_unlock_irqrestore(&mod->lock, flags);
653 return ret;
654}
655
656
657
658
659
660static int ican3_msg_connect(struct ican3_dev *mod)
661{
662 struct ican3_msg msg;
663
664 memset(&msg, 0, sizeof(msg));
665 msg.spec = MSG_CONNECTI;
666 msg.len = cpu_to_le16(0);
667
668 return ican3_send_msg(mod, &msg);
669}
670
671static int ican3_msg_disconnect(struct ican3_dev *mod)
672{
673 struct ican3_msg msg;
674
675 memset(&msg, 0, sizeof(msg));
676 msg.spec = MSG_DISCONNECT;
677 msg.len = cpu_to_le16(0);
678
679 return ican3_send_msg(mod, &msg);
680}
681
682static int ican3_msg_newhostif(struct ican3_dev *mod)
683{
684 struct ican3_msg msg;
685 int ret;
686
687 memset(&msg, 0, sizeof(msg));
688 msg.spec = MSG_NEWHOSTIF;
689 msg.len = cpu_to_le16(0);
690
691
692 WARN_ON(mod->iftype != 0);
693
694 ret = ican3_send_msg(mod, &msg);
695 if (ret)
696 return ret;
697
698
699 mod->iftype = 1;
700 return 0;
701}
702
703static int ican3_msg_fasthostif(struct ican3_dev *mod)
704{
705 struct ican3_msg msg;
706 unsigned int addr;
707
708 memset(&msg, 0, sizeof(msg));
709 msg.spec = MSG_INITFDPMQUEUE;
710 msg.len = cpu_to_le16(8);
711
712
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;
718
719
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;
725
726
727 WARN_ON(mod->iftype != 1);
728
729 return ican3_send_msg(mod, &msg);
730}
731
732
733
734
735
736static int ican3_set_id_filter(struct ican3_dev *mod, bool accept)
737{
738 struct ican3_msg msg;
739 int ret;
740
741
742 memset(&msg, 0, sizeof(msg));
743 msg.spec = MSG_SETAFILMASK;
744 msg.len = cpu_to_le16(5);
745 msg.data[0] = 0x00;
746 msg.data[1] = 0x00;
747 msg.data[2] = 0xff;
748 msg.data[3] = 0x07;
749
750
751 msg.data[4] = accept ? SETAFILMASK_FASTIF : SETAFILMASK_REJECT;
752
753 ret = ican3_send_msg(mod, &msg);
754 if (ret)
755 return ret;
756
757
758 memset(&msg, 0, sizeof(msg));
759 msg.spec = MSG_SETAFILMASK;
760 msg.len = cpu_to_le16(13);
761 msg.data[0] = 0;
762 msg.data[1] = 0x00;
763 msg.data[2] = 0x00;
764 msg.data[3] = 0x00;
765 msg.data[4] = 0x20;
766 msg.data[5] = 0xff;
767 msg.data[6] = 0xff;
768 msg.data[7] = 0xff;
769 msg.data[8] = 0x3f;
770
771
772 msg.data[9] = accept ? SETAFILMASK_FASTIF : SETAFILMASK_REJECT;
773
774 return ican3_send_msg(mod, &msg);
775}
776
777
778
779
780static int ican3_set_bus_state(struct ican3_dev *mod, bool on)
781{
782 struct can_bittiming *bt = &mod->can.bittiming;
783 struct ican3_msg msg;
784 u8 btr0, btr1;
785 int res;
786
787
788
789
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)
794 btr1 |= 0x80;
795
796 if (mod->fwtype == ICAN3_FWTYPE_ICANOS) {
797 if (on) {
798
799 memset(&msg, 0, sizeof(msg));
800 msg.spec = MSG_CBTRREQ;
801 msg.len = cpu_to_le16(4);
802 msg.data[0] = 0x00;
803 msg.data[1] = 0x00;
804 msg.data[2] = btr0;
805 msg.data[3] = btr1;
806
807 res = ican3_send_msg(mod, &msg);
808 if (res)
809 return res;
810 }
811
812
813 memset(&msg, 0, sizeof(msg));
814 msg.spec = on ? MSG_CONREQ : MSG_COFFREQ;
815 msg.len = cpu_to_le16(0);
816
817 return ican3_send_msg(mod, &msg);
818
819 } else if (mod->fwtype == ICAN3_FWTYPE_CAL_CANOPEN) {
820
821 memset(&msg, 0, sizeof(msg));
822 msg.spec = MSG_LMTS;
823 if (on) {
824 msg.len = cpu_to_le16(4);
825 msg.data[0] = LMTS_BUSON_REQ;
826 msg.data[1] = 0;
827 msg.data[2] = btr0;
828 msg.data[3] = btr1;
829 } else {
830 msg.len = cpu_to_le16(2);
831 msg.data[0] = LMTS_BUSOFF_REQ;
832 msg.data[1] = 0;
833 }
834 res = ican3_send_msg(mod, &msg);
835 if (res)
836 return res;
837
838 if (on) {
839
840
841
842
843
844 memset(&msg, 0, sizeof(msg));
845 msg.spec = MSG_NMTS;
846 msg.len = cpu_to_le16(11);
847 msg.data[0] = NMTS_CREATE_NODE_REQ;
848 msg.data[1] = 0;
849 msg.data[2] = 2;
850 msg.data[3] = 1;
851 strcpy(msg.data + 4, "locnod1");
852 return ican3_send_msg(mod, &msg);
853 }
854 return 0;
855 }
856 return -ENOTSUPP;
857}
858
859static int ican3_set_termination(struct ican3_dev *mod, bool on)
860{
861 struct ican3_msg msg;
862
863 memset(&msg, 0, sizeof(msg));
864 msg.spec = MSG_HWCONF;
865 msg.len = cpu_to_le16(2);
866 msg.data[0] = 0x00;
867 msg.data[1] = on ? HWCONF_TERMINATE_ON : HWCONF_TERMINATE_OFF;
868
869 return ican3_send_msg(mod, &msg);
870}
871
872static int ican3_send_inquiry(struct ican3_dev *mod, u8 subspec)
873{
874 struct ican3_msg msg;
875
876 memset(&msg, 0, sizeof(msg));
877 msg.spec = MSG_INQUIRY;
878 msg.len = cpu_to_le16(2);
879 msg.data[0] = subspec;
880 msg.data[1] = 0x00;
881
882 return ican3_send_msg(mod, &msg);
883}
884
885static int ican3_set_buserror(struct ican3_dev *mod, u8 quota)
886{
887 struct ican3_msg msg;
888
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);
893 msg.data[0] = 0x00;
894 msg.data[1] = quota;
895 } else if (mod->fwtype == ICAN3_FWTYPE_CAL_CANOPEN) {
896 memset(&msg, 0, sizeof(msg));
897 msg.spec = MSG_LMTS;
898 msg.len = cpu_to_le16(4);
899 msg.data[0] = LMTS_CAN_CONF_REQ;
900 msg.data[1] = 0x00;
901 msg.data[2] = 0x00;
902 msg.data[3] = quota;
903 } else {
904 return -ENOTSUPP;
905 }
906 return ican3_send_msg(mod, &msg);
907}
908
909
910
911
912
913static void ican3_to_can_frame(struct ican3_dev *mod,
914 struct ican3_fast_desc *desc,
915 struct can_frame *cf)
916{
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;
920
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);
925 } else {
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;
929
930 if (desc->data[0] & ICAN3_EFF) {
931 cf->can_id |= CAN_EFF_FLAG;
932 cf->can_id |= desc->data[2] << 21;
933 cf->can_id |= desc->data[3] << 13;
934 cf->can_id |= desc->data[4] << 5;
935 cf->can_id |= (desc->data[5] & 0xf8) >> 3;
936 } else {
937 cf->can_id |= desc->data[2] << 3;
938 cf->can_id |= desc->data[3] >> 5;
939 }
940
941 memcpy(cf->data, &desc->data[6], cf->can_dlc);
942 }
943}
944
945static void can_frame_to_ican3(struct ican3_dev *mod,
946 struct can_frame *cf,
947 struct ican3_fast_desc *desc)
948{
949
950 memset(desc->data, 0, sizeof(desc->data));
951
952
953 desc->command = ICAN3_CAN_TYPE_EFF;
954 desc->data[0] |= cf->can_dlc;
955 desc->data[1] |= ICAN3_ECHO;
956
957
958 if (mod->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT)
959 desc->data[1] |= ICAN3_SNGL;
960
961 if (cf->can_id & CAN_RTR_FLAG)
962 desc->data[0] |= ICAN3_EFF_RTR;
963
964
965 if (cf->can_id & CAN_EFF_FLAG) {
966 desc->data[0] |= ICAN3_EFF;
967 desc->data[2] = (cf->can_id & 0x1fe00000) >> 21;
968 desc->data[3] = (cf->can_id & 0x001fe000) >> 13;
969 desc->data[4] = (cf->can_id & 0x00001fe0) >> 5;
970 desc->data[5] = (cf->can_id & 0x0000001f) << 3;
971 } else {
972 desc->data[2] = (cf->can_id & 0x7F8) >> 3;
973 desc->data[3] = (cf->can_id & 0x007) << 5;
974 }
975
976
977 memcpy(&desc->data[6], cf->data, cf->can_dlc);
978}
979
980
981
982
983
984
985
986
987
988
989static void ican3_handle_idvers(struct ican3_dev *mod, struct ican3_msg *msg)
990{
991 netdev_dbg(mod->ndev, "IDVERS response: %s\n", msg->data);
992}
993
994static void ican3_handle_msglost(struct ican3_dev *mod, struct ican3_msg *msg)
995{
996 struct net_device *dev = mod->ndev;
997 struct net_device_stats *stats = &dev->stats;
998 struct can_frame *cf;
999 struct sk_buff *skb;
1000
1001
1002
1003
1004
1005
1006 if (msg->spec == MSG_MSGLOST) {
1007 netdev_err(mod->ndev, "lost %d control messages\n", msg->data[0]);
1008 return;
1009 }
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019 skb = alloc_can_err_skb(dev, &cf);
1020 if (skb) {
1021 cf->can_id |= CAN_ERR_CRTL;
1022 cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
1023 stats->rx_over_errors++;
1024 stats->rx_errors++;
1025 netif_rx(skb);
1026 }
1027}
1028
1029
1030
1031
1032
1033
1034
1035
1036static int ican3_handle_cevtind(struct ican3_dev *mod, struct ican3_msg *msg)
1037{
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;
1044
1045
1046 if (msg->data[1] != CEVTIND_CHIP_SJA1000) {
1047 netdev_err(mod->ndev, "unable to handle errors on non-SJA1000\n");
1048 return -ENODEV;
1049 }
1050
1051
1052 if (le16_to_cpu(msg->len) < 6) {
1053 netdev_err(mod->ndev, "error message too short\n");
1054 return -EINVAL;
1055 }
1056
1057 isrc = msg->data[0];
1058 ecc = msg->data[2];
1059 status = msg->data[3];
1060 rxerr = msg->data[4];
1061 txerr = msg->data[5];
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073 if (isrc == CEVTIND_BEI) {
1074 int ret;
1075 netdev_dbg(mod->ndev, "bus error interrupt\n");
1076
1077
1078 if (!(ecc & ECC_DIR)) {
1079 kfree_skb(skb_dequeue(&mod->echoq));
1080 stats->tx_errors++;
1081 } else {
1082 stats->rx_errors++;
1083 }
1084
1085
1086
1087
1088
1089 ret = ican3_set_buserror(mod, 1);
1090 if (ret) {
1091 netdev_err(mod->ndev, "unable to re-enable bus-error\n");
1092 return ret;
1093 }
1094
1095
1096 if (!(mod->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING))
1097 return 0;
1098 }
1099
1100 skb = alloc_can_err_skb(dev, &cf);
1101 if (skb == NULL)
1102 return -ENOMEM;
1103
1104
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++;
1110 stats->rx_errors++;
1111 }
1112
1113
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++;
1120 can_bus_off(dev);
1121 } else if (status & SR_ES) {
1122 if (rxerr >= 128 || txerr >= 128)
1123 state = CAN_STATE_ERROR_PASSIVE;
1124 else
1125 state = CAN_STATE_ERROR_WARNING;
1126 } else {
1127 state = CAN_STATE_ERROR_ACTIVE;
1128 }
1129 }
1130
1131
1132 if (isrc == CEVTIND_BEI) {
1133 mod->can.can_stats.bus_error++;
1134 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
1135
1136 switch (ecc & ECC_MASK) {
1137 case ECC_BIT:
1138 cf->data[2] |= CAN_ERR_PROT_BIT;
1139 break;
1140 case ECC_FORM:
1141 cf->data[2] |= CAN_ERR_PROT_FORM;
1142 break;
1143 case ECC_STUFF:
1144 cf->data[2] |= CAN_ERR_PROT_STUFF;
1145 break;
1146 default:
1147 cf->data[3] = ecc & ECC_SEG;
1148 break;
1149 }
1150
1151 if (!(ecc & ECC_DIR))
1152 cf->data[2] |= CAN_ERR_PROT_TX;
1153
1154 cf->data[6] = txerr;
1155 cf->data[7] = rxerr;
1156 }
1157
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;
1166 } else {
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;
1171 }
1172
1173 cf->data[6] = txerr;
1174 cf->data[7] = rxerr;
1175 }
1176
1177 mod->can.state = state;
1178 netif_rx(skb);
1179 return 0;
1180}
1181
1182static void ican3_handle_inquiry(struct ican3_dev *mod, struct ican3_msg *msg)
1183{
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);
1190 break;
1191 case INQUIRY_TERMINATION:
1192 mod->termination_enabled = msg->data[6] & HWCONF_TERMINATE_ON;
1193 complete(&mod->termination_comp);
1194 break;
1195 default:
1196 netdev_err(mod->ndev, "received an unknown inquiry response\n");
1197 break;
1198 }
1199}
1200
1201
1202static void ican3_handle_nmtsind(struct ican3_dev *mod, struct ican3_msg *msg)
1203{
1204 u16 subspec;
1205
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
1212
1213
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);
1217 break;
1218 case NE_REMOTE_OCCURRED:
1219 case NE_REMOTE_RESOLVED:
1220
1221 break;
1222 default:
1223 netdev_warn(mod->ndev, "unknown NMTS event indication %x\n",
1224 msg->data[2]);
1225 break;
1226 }
1227 } else if (subspec == NMTS_SLAVE_STATE_IND) {
1228
1229 } else {
1230 netdev_warn(mod->ndev, "unhandled NMTS indication %x\n",
1231 subspec);
1232 return;
1233 }
1234}
1235
1236static void ican3_handle_unknown_message(struct ican3_dev *mod,
1237 struct ican3_msg *msg)
1238{
1239 netdev_warn(mod->ndev, "received unknown message: spec 0x%.2x length %d\n",
1240 msg->spec, le16_to_cpu(msg->len));
1241}
1242
1243
1244
1245
1246static void ican3_handle_message(struct ican3_dev *mod, struct ican3_msg *msg)
1247{
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));
1250
1251 switch (msg->spec) {
1252 case MSG_IDVERS:
1253 ican3_handle_idvers(mod, msg);
1254 break;
1255 case MSG_MSGLOST:
1256 case MSG_FMSGLOST:
1257 ican3_handle_msglost(mod, msg);
1258 break;
1259 case MSG_CEVTIND:
1260 ican3_handle_cevtind(mod, msg);
1261 break;
1262 case MSG_INQUIRY:
1263 ican3_handle_inquiry(mod, msg);
1264 break;
1265 case MSG_NMTS:
1266 ican3_handle_nmtsind(mod, msg);
1267 break;
1268 default:
1269 ican3_handle_unknown_message(mod, msg);
1270 break;
1271 }
1272}
1273
1274
1275
1276
1277
1278static void ican3_put_echo_skb(struct ican3_dev *mod, struct sk_buff *skb)
1279{
1280 skb = can_create_echo_skb(skb);
1281 if (!skb)
1282 return;
1283
1284
1285 skb_queue_tail(&mod->echoq, skb);
1286}
1287
1288static unsigned int ican3_get_echo_skb(struct ican3_dev *mod)
1289{
1290 struct sk_buff *skb = skb_dequeue(&mod->echoq);
1291 struct can_frame *cf;
1292 u8 dlc;
1293
1294
1295 if (!skb) {
1296 netdev_err(mod->ndev, "BUG: echo skb not occupied\n");
1297 return 0;
1298 }
1299
1300 cf = (struct can_frame *)skb->data;
1301 dlc = cf->can_dlc;
1302
1303
1304 if (skb->pkt_type != PACKET_LOOPBACK) {
1305 kfree_skb(skb);
1306 return dlc;
1307 }
1308
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);
1314 return dlc;
1315}
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326static bool ican3_echo_skb_matches(struct ican3_dev *mod, struct sk_buff *skb)
1327{
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;
1331
1332 if (!echo_skb)
1333 return false;
1334
1335 echo_cf = (struct can_frame *)echo_skb->data;
1336 if (cf->can_id != echo_cf->can_id)
1337 return false;
1338
1339 if (cf->can_dlc != echo_cf->can_dlc)
1340 return false;
1341
1342 return memcmp(cf->data, echo_cf->data, cf->can_dlc) == 0;
1343}
1344
1345
1346
1347
1348
1349
1350static bool ican3_txok(struct ican3_dev *mod)
1351{
1352 struct ican3_fast_desc __iomem *desc;
1353 u8 control;
1354
1355
1356 if (skb_queue_len(&mod->echoq) >= ICAN3_TX_BUFFERS)
1357 return false;
1358
1359
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);
1363
1364
1365 if (!(control & DESC_VALID))
1366 return false;
1367
1368 return true;
1369}
1370
1371
1372
1373
1374
1375
1376static int ican3_recv_skb(struct ican3_dev *mod)
1377{
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;
1385
1386 spin_lock_irqsave(&mod->lock, flags);
1387
1388
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));
1392
1393 spin_unlock_irqrestore(&mod->lock, flags);
1394
1395
1396 if (!(desc.control & DESC_VALID))
1397 return -ENOBUFS;
1398
1399
1400 skb = alloc_can_skb(ndev, &cf);
1401 if (unlikely(skb == NULL)) {
1402 stats->rx_dropped++;
1403 goto err_noalloc;
1404 }
1405
1406
1407 ican3_to_can_frame(mod, &desc, cf);
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419 if (ican3_echo_skb_matches(mod, skb)) {
1420 stats->tx_packets++;
1421 stats->tx_bytes += ican3_get_echo_skb(mod);
1422 kfree_skb(skb);
1423 goto err_noalloc;
1424 }
1425
1426
1427 stats->rx_packets++;
1428 stats->rx_bytes += cf->can_dlc;
1429 netif_receive_skb(skb);
1430
1431err_noalloc:
1432
1433 desc.control ^= DESC_VALID;
1434
1435 spin_lock_irqsave(&mod->lock, flags);
1436
1437 ican3_set_page(mod, mod->fastrx_start + (mod->fastrx_num / 16));
1438 memcpy_toio(desc_addr, &desc, 1);
1439
1440
1441 mod->fastrx_num = (desc.control & DESC_WRAP) ? 0
1442 : (mod->fastrx_num + 1);
1443
1444
1445 spin_unlock_irqrestore(&mod->lock, flags);
1446 return 0;
1447}
1448
1449static int ican3_napi(struct napi_struct *napi, int budget)
1450{
1451 struct ican3_dev *mod = container_of(napi, struct ican3_dev, napi);
1452 unsigned long flags;
1453 int received = 0;
1454 int ret;
1455
1456
1457 while (true) {
1458 struct ican3_msg uninitialized_var(msg);
1459 ret = ican3_recv_msg(mod, &msg);
1460 if (ret)
1461 break;
1462
1463 ican3_handle_message(mod, &msg);
1464 }
1465
1466
1467 while (received < budget) {
1468 ret = ican3_recv_skb(mod);
1469 if (ret)
1470 break;
1471
1472 received++;
1473 }
1474
1475
1476
1477 if (received < budget)
1478 napi_complete_done(napi, received);
1479
1480 spin_lock_irqsave(&mod->lock, flags);
1481
1482
1483 if (netif_queue_stopped(mod->ndev) && ican3_txok(mod))
1484 netif_wake_queue(mod->ndev);
1485
1486 spin_unlock_irqrestore(&mod->lock, flags);
1487
1488
1489 iowrite8(1 << mod->num, &mod->ctrl->int_enable);
1490 return received;
1491}
1492
1493static irqreturn_t ican3_irq(int irq, void *dev_id)
1494{
1495 struct ican3_dev *mod = dev_id;
1496 u8 stat;
1497
1498
1499
1500
1501
1502 stat = ioread8(&mod->ctrl->int_disable) & (1 << mod->num);
1503 if (stat == (1 << mod->num))
1504 return IRQ_NONE;
1505
1506
1507 ioread8(&mod->dpmctrl->interrupt);
1508
1509
1510 iowrite8(1 << mod->num, &mod->ctrl->int_disable);
1511 napi_schedule(&mod->napi);
1512 return IRQ_HANDLED;
1513}
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524static int ican3_reset_module(struct ican3_dev *mod)
1525{
1526 unsigned long start;
1527 u8 runold, runnew;
1528
1529
1530 iowrite8(1 << mod->num, &mod->ctrl->int_disable);
1531
1532
1533 mod->free_page = DPM_FREE_START;
1534
1535 ican3_set_page(mod, QUEUE_OLD_CONTROL);
1536 runold = ioread8(mod->dpm + TARGET_RUNNING);
1537
1538
1539 iowrite8(0x00, &mod->dpmctrl->hwreset);
1540
1541
1542 start = jiffies;
1543 do {
1544 ican3_set_page(mod, QUEUE_OLD_CONTROL);
1545 runnew = ioread8(mod->dpm + TARGET_RUNNING);
1546 if (runnew == (runold ^ 0xff))
1547 return 0;
1548
1549 msleep(10);
1550 } while (time_before(jiffies, start + HZ / 2));
1551
1552 netdev_err(mod->ndev, "failed to reset CAN module\n");
1553 return -ETIMEDOUT;
1554}
1555
1556static void ican3_shutdown_module(struct ican3_dev *mod)
1557{
1558 ican3_msg_disconnect(mod);
1559 ican3_reset_module(mod);
1560}
1561
1562
1563
1564
1565static int ican3_startup_module(struct ican3_dev *mod)
1566{
1567 int ret;
1568
1569 ret = ican3_reset_module(mod);
1570 if (ret) {
1571 netdev_err(mod->ndev, "unable to reset module\n");
1572 return ret;
1573 }
1574
1575
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);
1579 return -ENODEV;
1580 }
1581 if (strstr(mod->fwinfo, "CAL/CANopen"))
1582 mod->fwtype = ICAN3_FWTYPE_CAL_CANOPEN;
1583 else
1584 mod->fwtype = ICAN3_FWTYPE_ICANOS;
1585
1586
1587 iowrite8(1 << mod->num, &mod->ctrl->int_enable);
1588
1589 ret = ican3_msg_connect(mod);
1590 if (ret) {
1591 netdev_err(mod->ndev, "unable to connect to module\n");
1592 return ret;
1593 }
1594
1595 ican3_init_new_host_interface(mod);
1596 ret = ican3_msg_newhostif(mod);
1597 if (ret) {
1598 netdev_err(mod->ndev, "unable to switch to new-style interface\n");
1599 return ret;
1600 }
1601
1602
1603 ret = ican3_set_termination(mod, true);
1604 if (ret) {
1605 netdev_err(mod->ndev, "unable to enable termination\n");
1606 return ret;
1607 }
1608
1609
1610 ret = ican3_set_buserror(mod, 1);
1611 if (ret) {
1612 netdev_err(mod->ndev, "unable to set bus-error\n");
1613 return ret;
1614 }
1615
1616 ican3_init_fast_host_interface(mod);
1617 ret = ican3_msg_fasthostif(mod);
1618 if (ret) {
1619 netdev_err(mod->ndev, "unable to switch to fast host interface\n");
1620 return ret;
1621 }
1622
1623 ret = ican3_set_id_filter(mod, true);
1624 if (ret) {
1625 netdev_err(mod->ndev, "unable to set acceptance filter\n");
1626 return ret;
1627 }
1628
1629 return 0;
1630}
1631
1632
1633
1634
1635
1636static int ican3_open(struct net_device *ndev)
1637{
1638 struct ican3_dev *mod = netdev_priv(ndev);
1639 int ret;
1640
1641
1642 ret = open_candev(ndev);
1643 if (ret) {
1644 netdev_err(mod->ndev, "unable to start CAN layer\n");
1645 return ret;
1646 }
1647
1648
1649 ret = ican3_set_bus_state(mod, true);
1650 if (ret) {
1651 netdev_err(mod->ndev, "unable to set bus-on\n");
1652 close_candev(ndev);
1653 return ret;
1654 }
1655
1656
1657 mod->can.state = CAN_STATE_ERROR_ACTIVE;
1658 netif_start_queue(ndev);
1659
1660 return 0;
1661}
1662
1663static int ican3_stop(struct net_device *ndev)
1664{
1665 struct ican3_dev *mod = netdev_priv(ndev);
1666 int ret;
1667
1668
1669 netif_stop_queue(ndev);
1670 mod->can.state = CAN_STATE_STOPPED;
1671
1672
1673 ret = ican3_set_bus_state(mod, false);
1674 if (ret) {
1675 netdev_err(mod->ndev, "unable to set bus-off\n");
1676 return ret;
1677 }
1678
1679
1680 skb_queue_purge(&mod->echoq);
1681
1682
1683 close_candev(ndev);
1684 return 0;
1685}
1686
1687static int ican3_xmit(struct sk_buff *skb, struct net_device *ndev)
1688{
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;
1694
1695 if (can_dropped_invalid_skb(ndev, skb))
1696 return NETDEV_TX_OK;
1697
1698 spin_lock_irqsave(&mod->lock, flags);
1699
1700
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;
1705 }
1706
1707
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);
1712
1713
1714 can_frame_to_ican3(mod, cf, &desc);
1715
1716
1717
1718
1719
1720
1721
1722 ican3_put_echo_skb(mod, skb);
1723
1724
1725
1726
1727
1728
1729 desc.control |= DESC_IVALID;
1730 memcpy_toio(desc_addr, &desc, sizeof(desc));
1731
1732
1733 iowrite8(0x01, &mod->dpmctrl->interrupt);
1734
1735 desc.control ^= DESC_VALID;
1736 memcpy_toio(desc_addr, &desc, sizeof(desc));
1737
1738
1739 mod->fasttx_num = (desc.control & DESC_WRAP) ? 0
1740 : (mod->fasttx_num + 1);
1741
1742
1743 if (!ican3_txok(mod))
1744 netif_stop_queue(ndev);
1745
1746 spin_unlock_irqrestore(&mod->lock, flags);
1747 return NETDEV_TX_OK;
1748}
1749
1750static 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,
1755};
1756
1757
1758
1759
1760
1761
1762static const struct can_bittiming_const ican3_bittiming_const = {
1763 .name = DRV_NAME,
1764 .tseg1_min = 1,
1765 .tseg1_max = 16,
1766 .tseg2_min = 1,
1767 .tseg2_max = 8,
1768 .sjw_max = 4,
1769 .brp_min = 1,
1770 .brp_max = 64,
1771 .brp_inc = 1,
1772};
1773
1774static int ican3_set_mode(struct net_device *ndev, enum can_mode mode)
1775{
1776 struct ican3_dev *mod = netdev_priv(ndev);
1777 int ret;
1778
1779 if (mode != CAN_MODE_START)
1780 return -ENOTSUPP;
1781
1782
1783 ret = ican3_set_bus_state(mod, true);
1784 if (ret) {
1785 netdev_err(ndev, "unable to set bus-on\n");
1786 return ret;
1787 }
1788
1789
1790 mod->can.state = CAN_STATE_ERROR_ACTIVE;
1791
1792 if (netif_queue_stopped(ndev))
1793 netif_wake_queue(ndev);
1794
1795 return 0;
1796}
1797
1798static int ican3_get_berr_counter(const struct net_device *ndev,
1799 struct can_berr_counter *bec)
1800{
1801 struct ican3_dev *mod = netdev_priv(ndev);
1802 int ret;
1803
1804 ret = ican3_send_inquiry(mod, INQUIRY_STATUS);
1805 if (ret)
1806 return ret;
1807
1808 if (!wait_for_completion_timeout(&mod->buserror_comp, HZ)) {
1809 netdev_info(mod->ndev, "%s timed out\n", __func__);
1810 return -ETIMEDOUT;
1811 }
1812
1813 bec->rxerr = mod->bec.rxerr;
1814 bec->txerr = mod->bec.txerr;
1815 return 0;
1816}
1817
1818
1819
1820
1821
1822static ssize_t ican3_sysfs_show_term(struct device *dev,
1823 struct device_attribute *attr,
1824 char *buf)
1825{
1826 struct ican3_dev *mod = netdev_priv(to_net_dev(dev));
1827 int ret;
1828
1829 ret = ican3_send_inquiry(mod, INQUIRY_TERMINATION);
1830 if (ret)
1831 return ret;
1832
1833 if (!wait_for_completion_timeout(&mod->termination_comp, HZ)) {
1834 netdev_info(mod->ndev, "%s timed out\n", __func__);
1835 return -ETIMEDOUT;
1836 }
1837
1838 return snprintf(buf, PAGE_SIZE, "%u\n", mod->termination_enabled);
1839}
1840
1841static ssize_t ican3_sysfs_set_term(struct device *dev,
1842 struct device_attribute *attr,
1843 const char *buf, size_t count)
1844{
1845 struct ican3_dev *mod = netdev_priv(to_net_dev(dev));
1846 unsigned long enable;
1847 int ret;
1848
1849 if (kstrtoul(buf, 0, &enable))
1850 return -EINVAL;
1851
1852 ret = ican3_set_termination(mod, enable);
1853 if (ret)
1854 return ret;
1855
1856 return count;
1857}
1858
1859static ssize_t ican3_sysfs_show_fwinfo(struct device *dev,
1860 struct device_attribute *attr,
1861 char *buf)
1862{
1863 struct ican3_dev *mod = netdev_priv(to_net_dev(dev));
1864
1865 return scnprintf(buf, PAGE_SIZE, "%s\n", mod->fwinfo);
1866}
1867
1868static DEVICE_ATTR(termination, S_IWUSR | S_IRUGO, ican3_sysfs_show_term,
1869 ican3_sysfs_set_term);
1870static DEVICE_ATTR(fwinfo, S_IRUSR | S_IRUGO, ican3_sysfs_show_fwinfo, NULL);
1871
1872static struct attribute *ican3_sysfs_attrs[] = {
1873 &dev_attr_termination.attr,
1874 &dev_attr_fwinfo.attr,
1875 NULL,
1876};
1877
1878static const struct attribute_group ican3_sysfs_attr_group = {
1879 .attrs = ican3_sysfs_attrs,
1880};
1881
1882
1883
1884
1885
1886static int ican3_probe(struct platform_device *pdev)
1887{
1888 struct janz_platform_data *pdata;
1889 struct net_device *ndev;
1890 struct ican3_dev *mod;
1891 struct resource *res;
1892 struct device *dev;
1893 int ret;
1894
1895 pdata = dev_get_platdata(&pdev->dev);
1896 if (!pdata)
1897 return -ENXIO;
1898
1899 dev_dbg(&pdev->dev, "probe: module number %d\n", pdata->modno);
1900
1901
1902 dev = &pdev->dev;
1903
1904
1905 ndev = alloc_candev(sizeof(*mod), 0);
1906 if (!ndev) {
1907 dev_err(dev, "unable to allocate CANdev\n");
1908 ret = -ENOMEM;
1909 goto out_return;
1910 }
1911
1912 platform_set_drvdata(pdev, ndev);
1913 mod = netdev_priv(ndev);
1914 mod->ndev = 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);
1921
1922
1923 ndev->sysfs_groups[0] = &ican3_sysfs_attr_group;
1924
1925
1926 mod->free_page = DPM_FREE_START;
1927
1928 ndev->netdev_ops = &ican3_netdev_ops;
1929 ndev->flags |= IFF_ECHO;
1930 SET_NETDEV_DEV(ndev, &pdev->dev);
1931
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;
1939
1940
1941 mod->irq = platform_get_irq(pdev, 0);
1942 if (mod->irq < 0) {
1943 dev_err(dev, "IRQ line not found\n");
1944 ret = -ENODEV;
1945 goto out_free_ndev;
1946 }
1947
1948 ndev->irq = mod->irq;
1949
1950
1951 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1952 if (!res) {
1953 dev_err(dev, "MODULbus registers not found\n");
1954 ret = -ENODEV;
1955 goto out_free_ndev;
1956 }
1957
1958 mod->dpm = ioremap(res->start, resource_size(res));
1959 if (!mod->dpm) {
1960 dev_err(dev, "MODULbus registers not ioremap\n");
1961 ret = -ENOMEM;
1962 goto out_free_ndev;
1963 }
1964
1965 mod->dpmctrl = mod->dpm + DPM_PAGE_SIZE;
1966
1967
1968 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1969 if (!res) {
1970 dev_err(dev, "CONTROL registers not found\n");
1971 ret = -ENODEV;
1972 goto out_iounmap_dpm;
1973 }
1974
1975 mod->ctrl = ioremap(res->start, resource_size(res));
1976 if (!mod->ctrl) {
1977 dev_err(dev, "CONTROL registers not ioremap\n");
1978 ret = -ENOMEM;
1979 goto out_iounmap_dpm;
1980 }
1981
1982
1983 iowrite8(1 << mod->num, &mod->ctrl->int_disable);
1984 ret = request_irq(mod->irq, ican3_irq, IRQF_SHARED, DRV_NAME, mod);
1985 if (ret) {
1986 dev_err(dev, "unable to request IRQ\n");
1987 goto out_iounmap_ctrl;
1988 }
1989
1990
1991 napi_enable(&mod->napi);
1992 ret = ican3_startup_module(mod);
1993 if (ret) {
1994 dev_err(dev, "%s: unable to start CANdev\n", __func__);
1995 goto out_free_irq;
1996 }
1997
1998
1999 ret = register_candev(ndev);
2000 if (ret) {
2001 dev_err(dev, "%s: unable to register CANdev\n", __func__);
2002 goto out_free_irq;
2003 }
2004
2005 netdev_info(mod->ndev, "module %d: registered CAN device\n", pdata->modno);
2006 return 0;
2007
2008out_free_irq:
2009 napi_disable(&mod->napi);
2010 iowrite8(1 << mod->num, &mod->ctrl->int_disable);
2011 free_irq(mod->irq, mod);
2012out_iounmap_ctrl:
2013 iounmap(mod->ctrl);
2014out_iounmap_dpm:
2015 iounmap(mod->dpm);
2016out_free_ndev:
2017 free_candev(ndev);
2018out_return:
2019 return ret;
2020}
2021
2022static int ican3_remove(struct platform_device *pdev)
2023{
2024 struct net_device *ndev = platform_get_drvdata(pdev);
2025 struct ican3_dev *mod = netdev_priv(ndev);
2026
2027
2028 unregister_netdev(ndev);
2029 napi_disable(&mod->napi);
2030 iowrite8(1 << mod->num, &mod->ctrl->int_disable);
2031 free_irq(mod->irq, mod);
2032
2033
2034 ican3_shutdown_module(mod);
2035
2036
2037 iounmap(mod->ctrl);
2038 iounmap(mod->dpm);
2039
2040 free_candev(ndev);
2041
2042 return 0;
2043}
2044
2045static struct platform_driver ican3_driver = {
2046 .driver = {
2047 .name = DRV_NAME,
2048 },
2049 .probe = ican3_probe,
2050 .remove = ican3_remove,
2051};
2052
2053module_platform_driver(ican3_driver);
2054
2055MODULE_AUTHOR("Ira W. Snyder <iws@ovro.caltech.edu>");
2056MODULE_DESCRIPTION("Janz MODULbus VMOD-ICAN3 Driver");
2057MODULE_LICENSE("GPL");
2058MODULE_ALIAS("platform:janz-ican3");
2059