1
2
3
4
5
6
7
8
9
10
11#include <linux/module.h>
12#include <linux/kernel.h>
13#include <linux/bitops.h>
14#include <linux/interrupt.h>
15#include <linux/errno.h>
16#include <linux/netdevice.h>
17#include <linux/skbuff.h>
18#include <linux/platform_device.h>
19
20#include <linux/can/dev.h>
21#include <linux/can/error.h>
22
23#include <asm/portmux.h>
24
25#define DRV_NAME "bfin_can"
26#define BFIN_CAN_TIMEOUT 100
27#define TX_ECHO_SKB_MAX 1
28
29
30#define TRANSMIT_CHL 24
31#define RECEIVE_STD_CHL 0
32#define RECEIVE_EXT_CHL 4
33#define RECEIVE_RTR_CHL 8
34#define RECEIVE_EXT_RTR_CHL 12
35#define MAX_CHL_NUMBER 32
36
37
38
39
40#define __BFP(m) u16 m; u16 __pad_##m
41
42
43struct bfin_can_mask_regs {
44 __BFP(aml);
45 __BFP(amh);
46};
47
48struct bfin_can_channel_regs {
49
50 u16 data[8];
51 __BFP(dlc);
52 __BFP(tsv);
53 __BFP(id0);
54 __BFP(id1);
55};
56
57struct bfin_can_regs {
58
59 __BFP(mc1);
60 __BFP(md1);
61 __BFP(trs1);
62 __BFP(trr1);
63 __BFP(ta1);
64 __BFP(aa1);
65 __BFP(rmp1);
66 __BFP(rml1);
67 __BFP(mbtif1);
68 __BFP(mbrif1);
69 __BFP(mbim1);
70 __BFP(rfh1);
71 __BFP(opss1);
72 u32 __pad1[3];
73 __BFP(mc2);
74 __BFP(md2);
75 __BFP(trs2);
76 __BFP(trr2);
77 __BFP(ta2);
78 __BFP(aa2);
79 __BFP(rmp2);
80 __BFP(rml2);
81 __BFP(mbtif2);
82 __BFP(mbrif2);
83 __BFP(mbim2);
84 __BFP(rfh2);
85 __BFP(opss2);
86 u32 __pad2[3];
87 __BFP(clock);
88 __BFP(timing);
89 __BFP(debug);
90 __BFP(status);
91 __BFP(cec);
92 __BFP(gis);
93 __BFP(gim);
94 __BFP(gif);
95 __BFP(control);
96 __BFP(intr);
97 __BFP(version);
98 __BFP(mbtd);
99 __BFP(ewr);
100 __BFP(esr);
101 u32 __pad3[2];
102 __BFP(ucreg);
103 __BFP(uccnt);
104 __BFP(ucrc);
105 __BFP(uccnf);
106 u32 __pad4[1];
107 __BFP(version2);
108 u32 __pad5[10];
109
110
111 struct bfin_can_mask_regs msk[MAX_CHL_NUMBER];
112 struct bfin_can_channel_regs chl[MAX_CHL_NUMBER];
113};
114
115#undef __BFP
116
117#define SRS 0x0001
118#define SER 0x0008
119#define BOIM 0x0008
120#define CCR 0x0080
121#define CCA 0x0080
122#define SAM 0x0080
123#define AME 0x8000
124#define RMLIM 0x0080
125#define RMLIS 0x0080
126#define RTR 0x4000
127#define BOIS 0x0008
128#define IDE 0x2000
129#define EPIS 0x0004
130#define EPIM 0x0004
131#define EWTIS 0x0001
132#define EWRIS 0x0002
133#define BEF 0x0040
134#define FER 0x0080
135#define SMR 0x0020
136#define SMACK 0x0008
137
138
139
140
141struct bfin_can_priv {
142 struct can_priv can;
143 struct net_device *dev;
144 void __iomem *membase;
145 int rx_irq;
146 int tx_irq;
147 int err_irq;
148 unsigned short *pin_list;
149};
150
151
152
153
154static const struct can_bittiming_const bfin_can_bittiming_const = {
155 .name = DRV_NAME,
156 .tseg1_min = 1,
157 .tseg1_max = 16,
158 .tseg2_min = 1,
159 .tseg2_max = 8,
160 .sjw_max = 4,
161
162
163
164
165
166 .brp_min = 4,
167 .brp_max = 1024,
168 .brp_inc = 1,
169};
170
171static int bfin_can_set_bittiming(struct net_device *dev)
172{
173 struct bfin_can_priv *priv = netdev_priv(dev);
174 struct bfin_can_regs __iomem *reg = priv->membase;
175 struct can_bittiming *bt = &priv->can.bittiming;
176 u16 clk, timing;
177
178 clk = bt->brp - 1;
179 timing = ((bt->sjw - 1) << 8) | (bt->prop_seg + bt->phase_seg1 - 1) |
180 ((bt->phase_seg2 - 1) << 4);
181
182
183
184
185
186 if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
187 timing |= SAM;
188
189 writew(clk, ®->clock);
190 writew(timing, ®->timing);
191
192 netdev_info(dev, "setting CLOCK=0x%04x TIMING=0x%04x\n", clk, timing);
193
194 return 0;
195}
196
197static void bfin_can_set_reset_mode(struct net_device *dev)
198{
199 struct bfin_can_priv *priv = netdev_priv(dev);
200 struct bfin_can_regs __iomem *reg = priv->membase;
201 int timeout = BFIN_CAN_TIMEOUT;
202 int i;
203
204
205 writew(0, ®->mbim1);
206 writew(0, ®->mbim2);
207 writew(0, ®->gim);
208
209
210 writew(SRS | CCR, ®->control);
211 writew(CCR, ®->control);
212 while (!(readw(®->control) & CCA)) {
213 udelay(10);
214 if (--timeout == 0) {
215 netdev_err(dev, "fail to enter configuration mode\n");
216 BUG();
217 }
218 }
219
220
221
222
223
224
225 writew(0, ®->mc1);
226 writew(0, ®->mc2);
227
228
229 writew(0xFFFF, ®->md1);
230 writew(0, ®->md2);
231
232
233 for (i = 0; i < 2; i++) {
234 writew(0, ®->chl[RECEIVE_STD_CHL + i].id0);
235 writew(AME, ®->chl[RECEIVE_STD_CHL + i].id1);
236 writew(0, ®->chl[RECEIVE_STD_CHL + i].dlc);
237 writew(0x1FFF, ®->msk[RECEIVE_STD_CHL + i].amh);
238 writew(0xFFFF, ®->msk[RECEIVE_STD_CHL + i].aml);
239 }
240
241
242 for (i = 0; i < 2; i++) {
243 writew(0, ®->chl[RECEIVE_EXT_CHL + i].id0);
244 writew(AME | IDE, ®->chl[RECEIVE_EXT_CHL + i].id1);
245 writew(0, ®->chl[RECEIVE_EXT_CHL + i].dlc);
246 writew(0x1FFF, ®->msk[RECEIVE_EXT_CHL + i].amh);
247 writew(0xFFFF, ®->msk[RECEIVE_EXT_CHL + i].aml);
248 }
249
250 writew(BIT(TRANSMIT_CHL - 16), ®->mc2);
251 writew(BIT(RECEIVE_STD_CHL) + BIT(RECEIVE_EXT_CHL), ®->mc1);
252
253 priv->can.state = CAN_STATE_STOPPED;
254}
255
256static void bfin_can_set_normal_mode(struct net_device *dev)
257{
258 struct bfin_can_priv *priv = netdev_priv(dev);
259 struct bfin_can_regs __iomem *reg = priv->membase;
260 int timeout = BFIN_CAN_TIMEOUT;
261
262
263
264
265 writew(readw(®->control) & ~CCR, ®->control);
266
267 while (readw(®->status) & CCA) {
268 udelay(10);
269 if (--timeout == 0) {
270 netdev_err(dev, "fail to leave configuration mode\n");
271 BUG();
272 }
273 }
274
275
276
277
278 writew(0xFFFF, ®->mbtif1);
279 writew(0xFFFF, ®->mbtif2);
280 writew(0xFFFF, ®->mbrif1);
281 writew(0xFFFF, ®->mbrif2);
282
283
284
285
286 writew(0x7FF, ®->gis);
287
288
289
290
291
292
293 writew(BIT(RECEIVE_STD_CHL) + BIT(RECEIVE_EXT_CHL), ®->mbim1);
294 writew(BIT(TRANSMIT_CHL - 16), ®->mbim2);
295
296 writew(EPIM | BOIM | RMLIM, ®->gim);
297}
298
299static void bfin_can_start(struct net_device *dev)
300{
301 struct bfin_can_priv *priv = netdev_priv(dev);
302
303
304 if (priv->can.state != CAN_STATE_STOPPED)
305 bfin_can_set_reset_mode(dev);
306
307
308 bfin_can_set_normal_mode(dev);
309}
310
311static int bfin_can_set_mode(struct net_device *dev, enum can_mode mode)
312{
313 switch (mode) {
314 case CAN_MODE_START:
315 bfin_can_start(dev);
316 if (netif_queue_stopped(dev))
317 netif_wake_queue(dev);
318 break;
319
320 default:
321 return -EOPNOTSUPP;
322 }
323
324 return 0;
325}
326
327static int bfin_can_get_berr_counter(const struct net_device *dev,
328 struct can_berr_counter *bec)
329{
330 struct bfin_can_priv *priv = netdev_priv(dev);
331 struct bfin_can_regs __iomem *reg = priv->membase;
332
333 u16 cec = readw(®->cec);
334
335 bec->txerr = cec >> 8;
336 bec->rxerr = cec;
337
338 return 0;
339}
340
341static int bfin_can_start_xmit(struct sk_buff *skb, struct net_device *dev)
342{
343 struct bfin_can_priv *priv = netdev_priv(dev);
344 struct bfin_can_regs __iomem *reg = priv->membase;
345 struct can_frame *cf = (struct can_frame *)skb->data;
346 u8 dlc = cf->can_dlc;
347 canid_t id = cf->can_id;
348 u8 *data = cf->data;
349 u16 val;
350 int i;
351
352 if (can_dropped_invalid_skb(dev, skb))
353 return NETDEV_TX_OK;
354
355 netif_stop_queue(dev);
356
357
358 if (id & CAN_EFF_FLAG) {
359 writew(id, ®->chl[TRANSMIT_CHL].id0);
360 val = ((id & 0x1FFF0000) >> 16) | IDE;
361 } else
362 val = (id << 2);
363 if (id & CAN_RTR_FLAG)
364 val |= RTR;
365 writew(val | AME, ®->chl[TRANSMIT_CHL].id1);
366
367
368 for (i = 0; i < 8; i += 2) {
369 val = ((7 - i) < dlc ? (data[7 - i]) : 0) +
370 ((6 - i) < dlc ? (data[6 - i] << 8) : 0);
371 writew(val, ®->chl[TRANSMIT_CHL].data[i]);
372 }
373
374
375 writew(dlc, ®->chl[TRANSMIT_CHL].dlc);
376
377 can_put_echo_skb(skb, dev, 0);
378
379
380 writew(BIT(TRANSMIT_CHL - 16), ®->trs2);
381
382 return 0;
383}
384
385static void bfin_can_rx(struct net_device *dev, u16 isrc)
386{
387 struct bfin_can_priv *priv = netdev_priv(dev);
388 struct net_device_stats *stats = &dev->stats;
389 struct bfin_can_regs __iomem *reg = priv->membase;
390 struct can_frame *cf;
391 struct sk_buff *skb;
392 int obj;
393 int i;
394 u16 val;
395
396 skb = alloc_can_skb(dev, &cf);
397 if (skb == NULL)
398 return;
399
400
401 if (isrc & BIT(RECEIVE_EXT_CHL)) {
402
403 cf->can_id = ((readw(®->chl[RECEIVE_EXT_CHL].id1)
404 & 0x1FFF) << 16)
405 + readw(®->chl[RECEIVE_EXT_CHL].id0);
406 cf->can_id |= CAN_EFF_FLAG;
407 obj = RECEIVE_EXT_CHL;
408 } else {
409
410 cf->can_id = (readw(®->chl[RECEIVE_STD_CHL].id1)
411 & 0x1ffc) >> 2;
412 obj = RECEIVE_STD_CHL;
413 }
414 if (readw(®->chl[obj].id1) & RTR)
415 cf->can_id |= CAN_RTR_FLAG;
416
417
418 cf->can_dlc = get_can_dlc(readw(®->chl[obj].dlc) & 0xF);
419
420
421 for (i = 0; i < 8; i += 2) {
422 val = readw(®->chl[obj].data[i]);
423 cf->data[7 - i] = (7 - i) < cf->can_dlc ? val : 0;
424 cf->data[6 - i] = (6 - i) < cf->can_dlc ? (val >> 8) : 0;
425 }
426
427 stats->rx_packets++;
428 stats->rx_bytes += cf->can_dlc;
429 netif_rx(skb);
430}
431
432static int bfin_can_err(struct net_device *dev, u16 isrc, u16 status)
433{
434 struct bfin_can_priv *priv = netdev_priv(dev);
435 struct bfin_can_regs __iomem *reg = priv->membase;
436 struct net_device_stats *stats = &dev->stats;
437 struct can_frame *cf;
438 struct sk_buff *skb;
439 enum can_state state = priv->can.state;
440
441 skb = alloc_can_err_skb(dev, &cf);
442 if (skb == NULL)
443 return -ENOMEM;
444
445 if (isrc & RMLIS) {
446
447 netdev_dbg(dev, "data overrun interrupt\n");
448 cf->can_id |= CAN_ERR_CRTL;
449 cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
450 stats->rx_over_errors++;
451 stats->rx_errors++;
452 }
453
454 if (isrc & BOIS) {
455 netdev_dbg(dev, "bus-off mode interrupt\n");
456 state = CAN_STATE_BUS_OFF;
457 cf->can_id |= CAN_ERR_BUSOFF;
458 priv->can.can_stats.bus_off++;
459 can_bus_off(dev);
460 }
461
462 if (isrc & EPIS) {
463
464 netdev_dbg(dev, "error passive interrupt\n");
465 state = CAN_STATE_ERROR_PASSIVE;
466 }
467
468 if ((isrc & EWTIS) || (isrc & EWRIS)) {
469 netdev_dbg(dev, "Error Warning Transmit/Receive Interrupt\n");
470 state = CAN_STATE_ERROR_WARNING;
471 }
472
473 if (state != priv->can.state && (state == CAN_STATE_ERROR_WARNING ||
474 state == CAN_STATE_ERROR_PASSIVE)) {
475 u16 cec = readw(®->cec);
476 u8 rxerr = cec;
477 u8 txerr = cec >> 8;
478
479 cf->can_id |= CAN_ERR_CRTL;
480 if (state == CAN_STATE_ERROR_WARNING) {
481 priv->can.can_stats.error_warning++;
482 cf->data[1] = (txerr > rxerr) ?
483 CAN_ERR_CRTL_TX_WARNING :
484 CAN_ERR_CRTL_RX_WARNING;
485 } else {
486 priv->can.can_stats.error_passive++;
487 cf->data[1] = (txerr > rxerr) ?
488 CAN_ERR_CRTL_TX_PASSIVE :
489 CAN_ERR_CRTL_RX_PASSIVE;
490 }
491 }
492
493 if (status) {
494 priv->can.can_stats.bus_error++;
495
496 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
497
498 if (status & BEF)
499 cf->data[2] |= CAN_ERR_PROT_BIT;
500 else if (status & FER)
501 cf->data[2] |= CAN_ERR_PROT_FORM;
502 else if (status & SER)
503 cf->data[2] |= CAN_ERR_PROT_STUFF;
504 }
505
506 priv->can.state = state;
507
508 stats->rx_packets++;
509 stats->rx_bytes += cf->can_dlc;
510 netif_rx(skb);
511
512 return 0;
513}
514
515static irqreturn_t bfin_can_interrupt(int irq, void *dev_id)
516{
517 struct net_device *dev = dev_id;
518 struct bfin_can_priv *priv = netdev_priv(dev);
519 struct bfin_can_regs __iomem *reg = priv->membase;
520 struct net_device_stats *stats = &dev->stats;
521 u16 status, isrc;
522
523 if ((irq == priv->tx_irq) && readw(®->mbtif2)) {
524
525 writew(0xFFFF, ®->mbtif2);
526 stats->tx_packets++;
527 stats->tx_bytes += readw(®->chl[TRANSMIT_CHL].dlc);
528 can_get_echo_skb(dev, 0);
529 netif_wake_queue(dev);
530 } else if ((irq == priv->rx_irq) && readw(®->mbrif1)) {
531
532 isrc = readw(®->mbrif1);
533 writew(0xFFFF, ®->mbrif1);
534 bfin_can_rx(dev, isrc);
535 } else if ((irq == priv->err_irq) && readw(®->gis)) {
536
537 isrc = readw(®->gis);
538 status = readw(®->esr);
539 writew(0x7FF, ®->gis);
540 bfin_can_err(dev, isrc, status);
541 } else {
542 return IRQ_NONE;
543 }
544
545 return IRQ_HANDLED;
546}
547
548static int bfin_can_open(struct net_device *dev)
549{
550 struct bfin_can_priv *priv = netdev_priv(dev);
551 int err;
552
553
554 bfin_can_set_reset_mode(dev);
555
556
557 err = open_candev(dev);
558 if (err)
559 goto exit_open;
560
561
562 err = request_irq(priv->rx_irq, &bfin_can_interrupt, 0,
563 "bfin-can-rx", dev);
564 if (err)
565 goto exit_rx_irq;
566 err = request_irq(priv->tx_irq, &bfin_can_interrupt, 0,
567 "bfin-can-tx", dev);
568 if (err)
569 goto exit_tx_irq;
570 err = request_irq(priv->err_irq, &bfin_can_interrupt, 0,
571 "bfin-can-err", dev);
572 if (err)
573 goto exit_err_irq;
574
575 bfin_can_start(dev);
576
577 netif_start_queue(dev);
578
579 return 0;
580
581exit_err_irq:
582 free_irq(priv->tx_irq, dev);
583exit_tx_irq:
584 free_irq(priv->rx_irq, dev);
585exit_rx_irq:
586 close_candev(dev);
587exit_open:
588 return err;
589}
590
591static int bfin_can_close(struct net_device *dev)
592{
593 struct bfin_can_priv *priv = netdev_priv(dev);
594
595 netif_stop_queue(dev);
596 bfin_can_set_reset_mode(dev);
597
598 close_candev(dev);
599
600 free_irq(priv->rx_irq, dev);
601 free_irq(priv->tx_irq, dev);
602 free_irq(priv->err_irq, dev);
603
604 return 0;
605}
606
607static struct net_device *alloc_bfin_candev(void)
608{
609 struct net_device *dev;
610 struct bfin_can_priv *priv;
611
612 dev = alloc_candev(sizeof(*priv), TX_ECHO_SKB_MAX);
613 if (!dev)
614 return NULL;
615
616 priv = netdev_priv(dev);
617
618 priv->dev = dev;
619 priv->can.bittiming_const = &bfin_can_bittiming_const;
620 priv->can.do_set_bittiming = bfin_can_set_bittiming;
621 priv->can.do_set_mode = bfin_can_set_mode;
622 priv->can.do_get_berr_counter = bfin_can_get_berr_counter;
623 priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES;
624
625 return dev;
626}
627
628static const struct net_device_ops bfin_can_netdev_ops = {
629 .ndo_open = bfin_can_open,
630 .ndo_stop = bfin_can_close,
631 .ndo_start_xmit = bfin_can_start_xmit,
632 .ndo_change_mtu = can_change_mtu,
633};
634
635static int bfin_can_probe(struct platform_device *pdev)
636{
637 int err;
638 struct net_device *dev;
639 struct bfin_can_priv *priv;
640 struct resource *res_mem, *rx_irq, *tx_irq, *err_irq;
641 unsigned short *pdata;
642
643 pdata = dev_get_platdata(&pdev->dev);
644 if (!pdata) {
645 dev_err(&pdev->dev, "No platform data provided!\n");
646 err = -EINVAL;
647 goto exit;
648 }
649
650 res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
651 rx_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
652 tx_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
653 err_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 2);
654 if (!res_mem || !rx_irq || !tx_irq || !err_irq) {
655 err = -EINVAL;
656 goto exit;
657 }
658
659
660 err = peripheral_request_list(pdata, dev_name(&pdev->dev));
661 if (err)
662 goto exit;
663
664 dev = alloc_bfin_candev();
665 if (!dev) {
666 err = -ENOMEM;
667 goto exit_peri_pin_free;
668 }
669
670 priv = netdev_priv(dev);
671
672 priv->membase = devm_ioremap_resource(&pdev->dev, res_mem);
673 if (IS_ERR(priv->membase)) {
674 err = PTR_ERR(priv->membase);
675 goto exit_peri_pin_free;
676 }
677
678 priv->rx_irq = rx_irq->start;
679 priv->tx_irq = tx_irq->start;
680 priv->err_irq = err_irq->start;
681 priv->pin_list = pdata;
682 priv->can.clock.freq = get_sclk();
683
684 platform_set_drvdata(pdev, dev);
685 SET_NETDEV_DEV(dev, &pdev->dev);
686
687 dev->flags |= IFF_ECHO;
688 dev->netdev_ops = &bfin_can_netdev_ops;
689
690 bfin_can_set_reset_mode(dev);
691
692 err = register_candev(dev);
693 if (err) {
694 dev_err(&pdev->dev, "registering failed (err=%d)\n", err);
695 goto exit_candev_free;
696 }
697
698 dev_info(&pdev->dev,
699 "%s device registered"
700 "(®_base=%p, rx_irq=%d, tx_irq=%d, err_irq=%d, sclk=%d)\n",
701 DRV_NAME, priv->membase, priv->rx_irq,
702 priv->tx_irq, priv->err_irq, priv->can.clock.freq);
703 return 0;
704
705exit_candev_free:
706 free_candev(dev);
707exit_peri_pin_free:
708 peripheral_free_list(pdata);
709exit:
710 return err;
711}
712
713static int bfin_can_remove(struct platform_device *pdev)
714{
715 struct net_device *dev = platform_get_drvdata(pdev);
716 struct bfin_can_priv *priv = netdev_priv(dev);
717
718 bfin_can_set_reset_mode(dev);
719
720 unregister_candev(dev);
721
722 peripheral_free_list(priv->pin_list);
723
724 free_candev(dev);
725 return 0;
726}
727
728#ifdef CONFIG_PM
729static int bfin_can_suspend(struct platform_device *pdev, pm_message_t mesg)
730{
731 struct net_device *dev = platform_get_drvdata(pdev);
732 struct bfin_can_priv *priv = netdev_priv(dev);
733 struct bfin_can_regs __iomem *reg = priv->membase;
734 int timeout = BFIN_CAN_TIMEOUT;
735
736 if (netif_running(dev)) {
737
738 writew(readw(®->control) | SMR, ®->control);
739 while (!(readw(®->intr) & SMACK)) {
740 udelay(10);
741 if (--timeout == 0) {
742 netdev_err(dev, "fail to enter sleep mode\n");
743 BUG();
744 }
745 }
746 }
747
748 return 0;
749}
750
751static int bfin_can_resume(struct platform_device *pdev)
752{
753 struct net_device *dev = platform_get_drvdata(pdev);
754 struct bfin_can_priv *priv = netdev_priv(dev);
755 struct bfin_can_regs __iomem *reg = priv->membase;
756
757 if (netif_running(dev)) {
758
759 writew(0, ®->intr);
760 }
761
762 return 0;
763}
764#else
765#define bfin_can_suspend NULL
766#define bfin_can_resume NULL
767#endif
768
769static struct platform_driver bfin_can_driver = {
770 .probe = bfin_can_probe,
771 .remove = bfin_can_remove,
772 .suspend = bfin_can_suspend,
773 .resume = bfin_can_resume,
774 .driver = {
775 .name = DRV_NAME,
776 },
777};
778
779module_platform_driver(bfin_can_driver);
780
781MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
782MODULE_LICENSE("GPL");
783MODULE_DESCRIPTION("Blackfin on-chip CAN netdevice driver");
784MODULE_ALIAS("platform:" DRV_NAME);
785