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