1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45#include <linux/module.h>
46#include <linux/init.h>
47#include <linux/kernel.h>
48#include <linux/sched.h>
49#include <linux/types.h>
50#include <linux/fcntl.h>
51#include <linux/interrupt.h>
52#include <linux/ptrace.h>
53#include <linux/string.h>
54#include <linux/errno.h>
55#include <linux/netdevice.h>
56#include <linux/if_arp.h>
57#include <linux/if_ether.h>
58#include <linux/skbuff.h>
59#include <linux/delay.h>
60
61#include <linux/can/dev.h>
62#include <linux/can/error.h>
63#include <linux/can/led.h>
64
65#include "sja1000.h"
66
67#define DRV_NAME "sja1000"
68
69MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
70MODULE_LICENSE("Dual BSD/GPL");
71MODULE_DESCRIPTION(DRV_NAME "CAN netdevice driver");
72
73static const struct can_bittiming_const sja1000_bittiming_const = {
74 .name = DRV_NAME,
75 .tseg1_min = 1,
76 .tseg1_max = 16,
77 .tseg2_min = 1,
78 .tseg2_max = 8,
79 .sjw_max = 4,
80 .brp_min = 1,
81 .brp_max = 64,
82 .brp_inc = 1,
83};
84
85static void sja1000_write_cmdreg(struct sja1000_priv *priv, u8 val)
86{
87 unsigned long flags;
88
89
90
91
92
93 spin_lock_irqsave(&priv->cmdreg_lock, flags);
94 priv->write_reg(priv, SJA1000_CMR, val);
95 priv->read_reg(priv, SJA1000_SR);
96 spin_unlock_irqrestore(&priv->cmdreg_lock, flags);
97}
98
99static int sja1000_is_absent(struct sja1000_priv *priv)
100{
101 return (priv->read_reg(priv, SJA1000_MOD) == 0xFF);
102}
103
104static int sja1000_probe_chip(struct net_device *dev)
105{
106 struct sja1000_priv *priv = netdev_priv(dev);
107
108 if (priv->reg_base && sja1000_is_absent(priv)) {
109 netdev_err(dev, "probing failed\n");
110 return 0;
111 }
112 return -1;
113}
114
115static void set_reset_mode(struct net_device *dev)
116{
117 struct sja1000_priv *priv = netdev_priv(dev);
118 unsigned char status = priv->read_reg(priv, SJA1000_MOD);
119 int i;
120
121
122 priv->write_reg(priv, SJA1000_IER, IRQ_OFF);
123
124 for (i = 0; i < 100; i++) {
125
126 if (status & MOD_RM) {
127 priv->can.state = CAN_STATE_STOPPED;
128 return;
129 }
130
131
132 priv->write_reg(priv, SJA1000_MOD, MOD_RM);
133 udelay(10);
134 status = priv->read_reg(priv, SJA1000_MOD);
135 }
136
137 netdev_err(dev, "setting SJA1000 into reset mode failed!\n");
138}
139
140static void set_normal_mode(struct net_device *dev)
141{
142 struct sja1000_priv *priv = netdev_priv(dev);
143 unsigned char status = priv->read_reg(priv, SJA1000_MOD);
144 u8 mod_reg_val = 0x00;
145 int i;
146
147 for (i = 0; i < 100; i++) {
148
149 if ((status & MOD_RM) == 0) {
150 priv->can.state = CAN_STATE_ERROR_ACTIVE;
151
152 if (priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING)
153 priv->write_reg(priv, SJA1000_IER, IRQ_ALL);
154 else
155 priv->write_reg(priv, SJA1000_IER,
156 IRQ_ALL & ~IRQ_BEI);
157 return;
158 }
159
160
161 if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
162 mod_reg_val |= MOD_LOM;
163 if (priv->can.ctrlmode & CAN_CTRLMODE_PRESUME_ACK)
164 mod_reg_val |= MOD_STM;
165 priv->write_reg(priv, SJA1000_MOD, mod_reg_val);
166
167 udelay(10);
168
169 status = priv->read_reg(priv, SJA1000_MOD);
170 }
171
172 netdev_err(dev, "setting SJA1000 into normal mode failed!\n");
173}
174
175
176
177
178
179
180
181
182
183static void chipset_init(struct net_device *dev)
184{
185 struct sja1000_priv *priv = netdev_priv(dev);
186
187
188 priv->write_reg(priv, SJA1000_CDR, priv->cdr | CDR_PELICAN);
189
190
191 priv->write_reg(priv, SJA1000_ACCC0, 0x00);
192 priv->write_reg(priv, SJA1000_ACCC1, 0x00);
193 priv->write_reg(priv, SJA1000_ACCC2, 0x00);
194 priv->write_reg(priv, SJA1000_ACCC3, 0x00);
195
196 priv->write_reg(priv, SJA1000_ACCM0, 0xFF);
197 priv->write_reg(priv, SJA1000_ACCM1, 0xFF);
198 priv->write_reg(priv, SJA1000_ACCM2, 0xFF);
199 priv->write_reg(priv, SJA1000_ACCM3, 0xFF);
200
201 priv->write_reg(priv, SJA1000_OCR, priv->ocr | OCR_MODE_NORMAL);
202}
203
204static void sja1000_start(struct net_device *dev)
205{
206 struct sja1000_priv *priv = netdev_priv(dev);
207
208
209 if (priv->can.state != CAN_STATE_STOPPED)
210 set_reset_mode(dev);
211
212
213 if (!(priv->read_reg(priv, SJA1000_CDR) & CDR_PELICAN))
214 chipset_init(dev);
215
216
217 priv->write_reg(priv, SJA1000_TXERR, 0x0);
218 priv->write_reg(priv, SJA1000_RXERR, 0x0);
219 priv->read_reg(priv, SJA1000_ECC);
220
221
222 priv->read_reg(priv, SJA1000_IR);
223
224
225 set_normal_mode(dev);
226}
227
228static int sja1000_set_mode(struct net_device *dev, enum can_mode mode)
229{
230 switch (mode) {
231 case CAN_MODE_START:
232 sja1000_start(dev);
233 if (netif_queue_stopped(dev))
234 netif_wake_queue(dev);
235 break;
236
237 default:
238 return -EOPNOTSUPP;
239 }
240
241 return 0;
242}
243
244static int sja1000_set_bittiming(struct net_device *dev)
245{
246 struct sja1000_priv *priv = netdev_priv(dev);
247 struct can_bittiming *bt = &priv->can.bittiming;
248 u8 btr0, btr1;
249
250 btr0 = ((bt->brp - 1) & 0x3f) | (((bt->sjw - 1) & 0x3) << 6);
251 btr1 = ((bt->prop_seg + bt->phase_seg1 - 1) & 0xf) |
252 (((bt->phase_seg2 - 1) & 0x7) << 4);
253 if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
254 btr1 |= 0x80;
255
256 netdev_info(dev, "setting BTR0=0x%02x BTR1=0x%02x\n", btr0, btr1);
257
258 priv->write_reg(priv, SJA1000_BTR0, btr0);
259 priv->write_reg(priv, SJA1000_BTR1, btr1);
260
261 return 0;
262}
263
264static int sja1000_get_berr_counter(const struct net_device *dev,
265 struct can_berr_counter *bec)
266{
267 struct sja1000_priv *priv = netdev_priv(dev);
268
269 bec->txerr = priv->read_reg(priv, SJA1000_TXERR);
270 bec->rxerr = priv->read_reg(priv, SJA1000_RXERR);
271
272 return 0;
273}
274
275
276
277
278
279
280
281static netdev_tx_t sja1000_start_xmit(struct sk_buff *skb,
282 struct net_device *dev)
283{
284 struct sja1000_priv *priv = netdev_priv(dev);
285 struct can_frame *cf = (struct can_frame *)skb->data;
286 uint8_t fi;
287 uint8_t dlc;
288 canid_t id;
289 uint8_t dreg;
290 u8 cmd_reg_val = 0x00;
291 int i;
292
293 if (can_dropped_invalid_skb(dev, skb))
294 return NETDEV_TX_OK;
295
296 netif_stop_queue(dev);
297
298 fi = dlc = cf->can_dlc;
299 id = cf->can_id;
300
301 if (id & CAN_RTR_FLAG)
302 fi |= SJA1000_FI_RTR;
303
304 if (id & CAN_EFF_FLAG) {
305 fi |= SJA1000_FI_FF;
306 dreg = SJA1000_EFF_BUF;
307 priv->write_reg(priv, SJA1000_FI, fi);
308 priv->write_reg(priv, SJA1000_ID1, (id & 0x1fe00000) >> 21);
309 priv->write_reg(priv, SJA1000_ID2, (id & 0x001fe000) >> 13);
310 priv->write_reg(priv, SJA1000_ID3, (id & 0x00001fe0) >> 5);
311 priv->write_reg(priv, SJA1000_ID4, (id & 0x0000001f) << 3);
312 } else {
313 dreg = SJA1000_SFF_BUF;
314 priv->write_reg(priv, SJA1000_FI, fi);
315 priv->write_reg(priv, SJA1000_ID1, (id & 0x000007f8) >> 3);
316 priv->write_reg(priv, SJA1000_ID2, (id & 0x00000007) << 5);
317 }
318
319 for (i = 0; i < dlc; i++)
320 priv->write_reg(priv, dreg++, cf->data[i]);
321
322 can_put_echo_skb(skb, dev, 0);
323
324 if (priv->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT)
325 cmd_reg_val |= CMD_AT;
326
327 if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)
328 cmd_reg_val |= CMD_SRR;
329 else
330 cmd_reg_val |= CMD_TR;
331
332 sja1000_write_cmdreg(priv, cmd_reg_val);
333
334 return NETDEV_TX_OK;
335}
336
337static void sja1000_rx(struct net_device *dev)
338{
339 struct sja1000_priv *priv = netdev_priv(dev);
340 struct net_device_stats *stats = &dev->stats;
341 struct can_frame *cf;
342 struct sk_buff *skb;
343 uint8_t fi;
344 uint8_t dreg;
345 canid_t id;
346 int i;
347
348
349 skb = alloc_can_skb(dev, &cf);
350 if (skb == NULL)
351 return;
352
353 fi = priv->read_reg(priv, SJA1000_FI);
354
355 if (fi & SJA1000_FI_FF) {
356
357 dreg = SJA1000_EFF_BUF;
358 id = (priv->read_reg(priv, SJA1000_ID1) << 21)
359 | (priv->read_reg(priv, SJA1000_ID2) << 13)
360 | (priv->read_reg(priv, SJA1000_ID3) << 5)
361 | (priv->read_reg(priv, SJA1000_ID4) >> 3);
362 id |= CAN_EFF_FLAG;
363 } else {
364
365 dreg = SJA1000_SFF_BUF;
366 id = (priv->read_reg(priv, SJA1000_ID1) << 3)
367 | (priv->read_reg(priv, SJA1000_ID2) >> 5);
368 }
369
370 cf->can_dlc = get_can_dlc(fi & 0x0F);
371 if (fi & SJA1000_FI_RTR) {
372 id |= CAN_RTR_FLAG;
373 } else {
374 for (i = 0; i < cf->can_dlc; i++)
375 cf->data[i] = priv->read_reg(priv, dreg++);
376 }
377
378 cf->can_id = id;
379
380
381 sja1000_write_cmdreg(priv, CMD_RRB);
382
383 stats->rx_packets++;
384 stats->rx_bytes += cf->can_dlc;
385 netif_rx(skb);
386
387 can_led_event(dev, CAN_LED_EVENT_RX);
388}
389
390static int sja1000_err(struct net_device *dev, uint8_t isrc, uint8_t status)
391{
392 struct sja1000_priv *priv = netdev_priv(dev);
393 struct net_device_stats *stats = &dev->stats;
394 struct can_frame *cf;
395 struct sk_buff *skb;
396 enum can_state state = priv->can.state;
397 enum can_state rx_state, tx_state;
398 unsigned int rxerr, txerr;
399 uint8_t ecc, alc;
400
401 skb = alloc_can_err_skb(dev, &cf);
402 if (skb == NULL)
403 return -ENOMEM;
404
405 txerr = priv->read_reg(priv, SJA1000_TXERR);
406 rxerr = priv->read_reg(priv, SJA1000_RXERR);
407
408 cf->data[6] = txerr;
409 cf->data[7] = rxerr;
410
411 if (isrc & IRQ_DOI) {
412
413 netdev_dbg(dev, "data overrun interrupt\n");
414 cf->can_id |= CAN_ERR_CRTL;
415 cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
416 stats->rx_over_errors++;
417 stats->rx_errors++;
418 sja1000_write_cmdreg(priv, CMD_CDO);
419 }
420
421 if (isrc & IRQ_EI) {
422
423 netdev_dbg(dev, "error warning interrupt\n");
424
425 if (status & SR_BS)
426 state = CAN_STATE_BUS_OFF;
427 else if (status & SR_ES)
428 state = CAN_STATE_ERROR_WARNING;
429 else
430 state = CAN_STATE_ERROR_ACTIVE;
431 }
432 if (isrc & IRQ_BEI) {
433
434 priv->can.can_stats.bus_error++;
435 stats->rx_errors++;
436
437 ecc = priv->read_reg(priv, SJA1000_ECC);
438
439 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
440
441 switch (ecc & ECC_MASK) {
442 case ECC_BIT:
443 cf->data[2] |= CAN_ERR_PROT_BIT;
444 break;
445 case ECC_FORM:
446 cf->data[2] |= CAN_ERR_PROT_FORM;
447 break;
448 case ECC_STUFF:
449 cf->data[2] |= CAN_ERR_PROT_STUFF;
450 break;
451 default:
452 cf->data[3] = ecc & ECC_SEG;
453 break;
454 }
455
456 if ((ecc & ECC_DIR) == 0)
457 cf->data[2] |= CAN_ERR_PROT_TX;
458 }
459 if (isrc & IRQ_EPI) {
460
461 netdev_dbg(dev, "error passive interrupt\n");
462
463 if (state == CAN_STATE_ERROR_PASSIVE)
464 state = CAN_STATE_ERROR_WARNING;
465 else
466 state = CAN_STATE_ERROR_PASSIVE;
467 }
468 if (isrc & IRQ_ALI) {
469
470 netdev_dbg(dev, "arbitration lost interrupt\n");
471 alc = priv->read_reg(priv, SJA1000_ALC);
472 priv->can.can_stats.arbitration_lost++;
473 stats->tx_errors++;
474 cf->can_id |= CAN_ERR_LOSTARB;
475 cf->data[0] = alc & 0x1f;
476 }
477
478 if (state != priv->can.state) {
479 tx_state = txerr >= rxerr ? state : 0;
480 rx_state = txerr <= rxerr ? state : 0;
481
482 can_change_state(dev, cf, tx_state, rx_state);
483
484 if(state == CAN_STATE_BUS_OFF)
485 can_bus_off(dev);
486 }
487
488 stats->rx_packets++;
489 stats->rx_bytes += cf->can_dlc;
490 netif_rx(skb);
491
492 return 0;
493}
494
495irqreturn_t sja1000_interrupt(int irq, void *dev_id)
496{
497 struct net_device *dev = (struct net_device *)dev_id;
498 struct sja1000_priv *priv = netdev_priv(dev);
499 struct net_device_stats *stats = &dev->stats;
500 uint8_t isrc, status;
501 int n = 0;
502
503 if (priv->pre_irq)
504 priv->pre_irq(priv);
505
506
507 if (priv->read_reg(priv, SJA1000_IER) == IRQ_OFF)
508 goto out;
509
510 while ((isrc = priv->read_reg(priv, SJA1000_IR)) &&
511 (n < SJA1000_MAX_IRQ)) {
512
513 status = priv->read_reg(priv, SJA1000_SR);
514
515 if (status == 0xFF && sja1000_is_absent(priv))
516 goto out;
517
518 if (isrc & IRQ_WUI)
519 netdev_warn(dev, "wakeup interrupt\n");
520
521 if (isrc & IRQ_TI) {
522
523 if (priv->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT &&
524 !(status & SR_TCS)) {
525 stats->tx_errors++;
526 can_free_echo_skb(dev, 0);
527 } else {
528
529 stats->tx_bytes +=
530 priv->read_reg(priv, SJA1000_FI) & 0xf;
531 stats->tx_packets++;
532 can_get_echo_skb(dev, 0);
533 }
534 netif_wake_queue(dev);
535 can_led_event(dev, CAN_LED_EVENT_TX);
536 }
537 if (isrc & IRQ_RI) {
538
539 while (status & SR_RBS) {
540 sja1000_rx(dev);
541 status = priv->read_reg(priv, SJA1000_SR);
542
543 if (status == 0xFF && sja1000_is_absent(priv))
544 goto out;
545 }
546 }
547 if (isrc & (IRQ_DOI | IRQ_EI | IRQ_BEI | IRQ_EPI | IRQ_ALI)) {
548
549 if (sja1000_err(dev, isrc, status))
550 break;
551 }
552 n++;
553 }
554out:
555 if (priv->post_irq)
556 priv->post_irq(priv);
557
558 if (n >= SJA1000_MAX_IRQ)
559 netdev_dbg(dev, "%d messages handled in ISR", n);
560
561 return (n) ? IRQ_HANDLED : IRQ_NONE;
562}
563EXPORT_SYMBOL_GPL(sja1000_interrupt);
564
565static int sja1000_open(struct net_device *dev)
566{
567 struct sja1000_priv *priv = netdev_priv(dev);
568 int err;
569
570
571 set_reset_mode(dev);
572
573
574 err = open_candev(dev);
575 if (err)
576 return err;
577
578
579 if (!(priv->flags & SJA1000_CUSTOM_IRQ_HANDLER)) {
580 err = request_irq(dev->irq, sja1000_interrupt, priv->irq_flags,
581 dev->name, (void *)dev);
582 if (err) {
583 close_candev(dev);
584 return -EAGAIN;
585 }
586 }
587
588
589 sja1000_start(dev);
590
591 can_led_event(dev, CAN_LED_EVENT_OPEN);
592
593 netif_start_queue(dev);
594
595 return 0;
596}
597
598static int sja1000_close(struct net_device *dev)
599{
600 struct sja1000_priv *priv = netdev_priv(dev);
601
602 netif_stop_queue(dev);
603 set_reset_mode(dev);
604
605 if (!(priv->flags & SJA1000_CUSTOM_IRQ_HANDLER))
606 free_irq(dev->irq, (void *)dev);
607
608 close_candev(dev);
609
610 can_led_event(dev, CAN_LED_EVENT_STOP);
611
612 return 0;
613}
614
615struct net_device *alloc_sja1000dev(int sizeof_priv)
616{
617 struct net_device *dev;
618 struct sja1000_priv *priv;
619
620 dev = alloc_candev(sizeof(struct sja1000_priv) + sizeof_priv,
621 SJA1000_ECHO_SKB_MAX);
622 if (!dev)
623 return NULL;
624
625 priv = netdev_priv(dev);
626
627 priv->dev = dev;
628 priv->can.bittiming_const = &sja1000_bittiming_const;
629 priv->can.do_set_bittiming = sja1000_set_bittiming;
630 priv->can.do_set_mode = sja1000_set_mode;
631 priv->can.do_get_berr_counter = sja1000_get_berr_counter;
632 priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK |
633 CAN_CTRLMODE_LISTENONLY |
634 CAN_CTRLMODE_3_SAMPLES |
635 CAN_CTRLMODE_ONE_SHOT |
636 CAN_CTRLMODE_BERR_REPORTING |
637 CAN_CTRLMODE_PRESUME_ACK;
638
639 spin_lock_init(&priv->cmdreg_lock);
640
641 if (sizeof_priv)
642 priv->priv = (void *)priv + sizeof(struct sja1000_priv);
643
644 return dev;
645}
646EXPORT_SYMBOL_GPL(alloc_sja1000dev);
647
648void free_sja1000dev(struct net_device *dev)
649{
650 free_candev(dev);
651}
652EXPORT_SYMBOL_GPL(free_sja1000dev);
653
654static const struct net_device_ops sja1000_netdev_ops = {
655 .ndo_open = sja1000_open,
656 .ndo_stop = sja1000_close,
657 .ndo_start_xmit = sja1000_start_xmit,
658 .ndo_change_mtu = can_change_mtu,
659};
660
661int register_sja1000dev(struct net_device *dev)
662{
663 int ret;
664
665 if (!sja1000_probe_chip(dev))
666 return -ENODEV;
667
668 dev->flags |= IFF_ECHO;
669 dev->netdev_ops = &sja1000_netdev_ops;
670
671 set_reset_mode(dev);
672 chipset_init(dev);
673
674 ret = register_candev(dev);
675
676 if (!ret)
677 devm_can_led_init(dev);
678
679 return ret;
680}
681EXPORT_SYMBOL_GPL(register_sja1000dev);
682
683void unregister_sja1000dev(struct net_device *dev)
684{
685 set_reset_mode(dev);
686 unregister_candev(dev);
687}
688EXPORT_SYMBOL_GPL(unregister_sja1000dev);
689
690static __init int sja1000_init(void)
691{
692 printk(KERN_INFO "%s CAN netdevice driver\n", DRV_NAME);
693
694 return 0;
695}
696
697module_init(sja1000_init);
698
699static __exit void sja1000_exit(void)
700{
701 printk(KERN_INFO "%s: driver removed\n", DRV_NAME);
702}
703
704module_exit(sja1000_exit);
705