2 * Blackfin On-Chip CAN Driver
4 * Copyright 2004-2009 Analog Devices Inc.
6 * Enter bugs at http://blackfin.uclinux.org/
8 * Licensed under the GPL-2 or later.
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/kernel.h>
14 #include <linux/bitops.h>
15 #include <linux/interrupt.h>
16 #include <linux/errno.h>
17 #include <linux/netdevice.h>
18 #include <linux/skbuff.h>
19 #include <linux/platform_device.h>
21 #include <linux/can.h>
22 #include <linux/can/dev.h>
23 #include <linux/can/error.h>
25 #include <asm/portmux.h>
27 #define DRV_NAME "bfin_can"
28 #define BFIN_CAN_TIMEOUT 100
29 #define TX_ECHO_SKB_MAX 1
32 * transmit and receive channels
34 #define TRANSMIT_CHL 24
35 #define RECEIVE_STD_CHL 0
36 #define RECEIVE_EXT_CHL 4
37 #define RECEIVE_RTR_CHL 8
38 #define RECEIVE_EXT_RTR_CHL 12
39 #define MAX_CHL_NUMBER 32
42 * bfin can registers layout
44 struct bfin_can_mask_regs {
51 struct bfin_can_channel_regs {
63 struct bfin_can_regs {
65 * global control and status registers
67 u16 mc1; /* offset 0 */
69 u16 md1; /* offset 4 */
71 u16 mbtif1; /* offset 0x20 */
73 u16 mbrif1; /* offset 0x24 */
75 u16 mbim1; /* offset 0x28 */
77 u16 mc2; /* offset 0x40 */
79 u16 md2; /* offset 0x44 */
81 u16 trs2; /* offset 0x48 */
83 u16 mbtif2; /* offset 0x60 */
85 u16 mbrif2; /* offset 0x64 */
87 u16 mbim2; /* offset 0x68 */
89 u16 clk; /* offset 0x80 */
91 u16 timing; /* offset 0x84 */
93 u16 status; /* offset 0x8c */
95 u16 cec; /* offset 0x90 */
97 u16 gis; /* offset 0x94 */
99 u16 gim; /* offset 0x98 */
101 u16 ctrl; /* offset 0xa0 */
103 u16 intr; /* offset 0xa4 */
105 u16 esr; /* offset 0xb4 */
109 * channel(mailbox) mask and message registers
111 struct bfin_can_mask_regs msk[MAX_CHL_NUMBER]; /* offset 0x100 */
112 struct bfin_can_channel_regs chl[MAX_CHL_NUMBER]; /* offset 0x200 */
116 * bfin can private data
118 struct bfin_can_priv {
119 struct can_priv can; /* must be the first member */
120 struct net_device *dev;
121 void __iomem *membase;
125 unsigned short *pin_list;
129 * bfin can timing parameters
131 static struct can_bittiming_const bfin_can_bittiming_const = {
139 * Although the BRP field can be set to any value, it is recommended
140 * that the value be greater than or equal to 4, as restrictions
141 * apply to the bit timing configuration when BRP is less than 4.
148 static int bfin_can_set_bittiming(struct net_device *dev)
150 struct bfin_can_priv *priv = netdev_priv(dev);
151 struct bfin_can_regs __iomem *reg = priv->membase;
152 struct can_bittiming *bt = &priv->can.bittiming;
156 timing = ((bt->sjw - 1) << 8) | (bt->prop_seg + bt->phase_seg1 - 1) |
157 ((bt->phase_seg2 - 1) << 4);
160 * If the SAM bit is set, the input signal is oversampled three times
163 if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
166 bfin_write16(®->clk, clk);
167 bfin_write16(®->timing, timing);
169 dev_info(dev->dev.parent, "setting CLOCK=0x%04x TIMING=0x%04x\n",
175 static void bfin_can_set_reset_mode(struct net_device *dev)
177 struct bfin_can_priv *priv = netdev_priv(dev);
178 struct bfin_can_regs __iomem *reg = priv->membase;
179 int timeout = BFIN_CAN_TIMEOUT;
182 /* disable interrupts */
183 bfin_write16(®->mbim1, 0);
184 bfin_write16(®->mbim2, 0);
185 bfin_write16(®->gim, 0);
187 /* reset can and enter configuration mode */
188 bfin_write16(®->ctrl, SRS | CCR);
190 bfin_write16(®->ctrl, CCR);
192 while (!(bfin_read16(®->ctrl) & CCA)) {
194 if (--timeout == 0) {
195 dev_err(dev->dev.parent,
196 "fail to enter configuration mode\n");
202 * All mailbox configurations are marked as inactive
203 * by writing to CAN Mailbox Configuration Registers 1 and 2
204 * For all bits: 0 - Mailbox disabled, 1 - Mailbox enabled
206 bfin_write16(®->mc1, 0);
207 bfin_write16(®->mc2, 0);
209 /* Set Mailbox Direction */
210 bfin_write16(®->md1, 0xFFFF); /* mailbox 1-16 are RX */
211 bfin_write16(®->md2, 0); /* mailbox 17-32 are TX */
213 /* RECEIVE_STD_CHL */
214 for (i = 0; i < 2; i++) {
215 bfin_write16(®->chl[RECEIVE_STD_CHL + i].id0, 0);
216 bfin_write16(®->chl[RECEIVE_STD_CHL + i].id1, AME);
217 bfin_write16(®->chl[RECEIVE_STD_CHL + i].dlc, 0);
218 bfin_write16(®->msk[RECEIVE_STD_CHL + i].amh, 0x1FFF);
219 bfin_write16(®->msk[RECEIVE_STD_CHL + i].aml, 0xFFFF);
222 /* RECEIVE_EXT_CHL */
223 for (i = 0; i < 2; i++) {
224 bfin_write16(®->chl[RECEIVE_EXT_CHL + i].id0, 0);
225 bfin_write16(®->chl[RECEIVE_EXT_CHL + i].id1, AME | IDE);
226 bfin_write16(®->chl[RECEIVE_EXT_CHL + i].dlc, 0);
227 bfin_write16(®->msk[RECEIVE_EXT_CHL + i].amh, 0x1FFF);
228 bfin_write16(®->msk[RECEIVE_EXT_CHL + i].aml, 0xFFFF);
231 bfin_write16(®->mc2, BIT(TRANSMIT_CHL - 16));
232 bfin_write16(®->mc1, BIT(RECEIVE_STD_CHL) + BIT(RECEIVE_EXT_CHL));
235 priv->can.state = CAN_STATE_STOPPED;
238 static void bfin_can_set_normal_mode(struct net_device *dev)
240 struct bfin_can_priv *priv = netdev_priv(dev);
241 struct bfin_can_regs __iomem *reg = priv->membase;
242 int timeout = BFIN_CAN_TIMEOUT;
245 * leave configuration mode
247 bfin_write16(®->ctrl, bfin_read16(®->ctrl) & ~CCR);
249 while (bfin_read16(®->status) & CCA) {
251 if (--timeout == 0) {
252 dev_err(dev->dev.parent,
253 "fail to leave configuration mode\n");
259 * clear _All_ tx and rx interrupts
261 bfin_write16(®->mbtif1, 0xFFFF);
262 bfin_write16(®->mbtif2, 0xFFFF);
263 bfin_write16(®->mbrif1, 0xFFFF);
264 bfin_write16(®->mbrif2, 0xFFFF);
267 * clear global interrupt status register
269 bfin_write16(®->gis, 0x7FF); /* overwrites with '1' */
272 * Initialize Interrupts
273 * - set bits in the mailbox interrupt mask register
274 * - global interrupt mask
276 bfin_write16(®->mbim1, BIT(RECEIVE_STD_CHL) + BIT(RECEIVE_EXT_CHL));
277 bfin_write16(®->mbim2, BIT(TRANSMIT_CHL - 16));
279 bfin_write16(®->gim, EPIM | BOIM | RMLIM);
283 static void bfin_can_start(struct net_device *dev)
285 struct bfin_can_priv *priv = netdev_priv(dev);
287 /* enter reset mode */
288 if (priv->can.state != CAN_STATE_STOPPED)
289 bfin_can_set_reset_mode(dev);
291 /* leave reset mode */
292 bfin_can_set_normal_mode(dev);
295 static int bfin_can_set_mode(struct net_device *dev, enum can_mode mode)
300 if (netif_queue_stopped(dev))
301 netif_wake_queue(dev);
311 static int bfin_can_start_xmit(struct sk_buff *skb, struct net_device *dev)
313 struct bfin_can_priv *priv = netdev_priv(dev);
314 struct bfin_can_regs __iomem *reg = priv->membase;
315 struct can_frame *cf = (struct can_frame *)skb->data;
316 u8 dlc = cf->can_dlc;
317 canid_t id = cf->can_id;
322 if (can_dropped_invalid_skb(dev, skb))
325 netif_stop_queue(dev);
328 if (id & CAN_EFF_FLAG) {
329 bfin_write16(®->chl[TRANSMIT_CHL].id0, id);
330 if (id & CAN_RTR_FLAG)
331 writew(((id & 0x1FFF0000) >> 16) | IDE | AME | RTR,
332 ®->chl[TRANSMIT_CHL].id1);
334 writew(((id & 0x1FFF0000) >> 16) | IDE | AME,
335 ®->chl[TRANSMIT_CHL].id1);
338 if (id & CAN_RTR_FLAG)
339 writew((id << 2) | AME | RTR,
340 ®->chl[TRANSMIT_CHL].id1);
342 bfin_write16(®->chl[TRANSMIT_CHL].id1,
347 for (i = 0; i < 8; i += 2) {
348 val = ((7 - i) < dlc ? (data[7 - i]) : 0) +
349 ((6 - i) < dlc ? (data[6 - i] << 8) : 0);
350 bfin_write16(®->chl[TRANSMIT_CHL].data[i], val);
353 /* fill data length code */
354 bfin_write16(®->chl[TRANSMIT_CHL].dlc, dlc);
356 dev->trans_start = jiffies;
358 can_put_echo_skb(skb, dev, 0);
360 /* set transmit request */
361 bfin_write16(®->trs2, BIT(TRANSMIT_CHL - 16));
366 static void bfin_can_rx(struct net_device *dev, u16 isrc)
368 struct bfin_can_priv *priv = netdev_priv(dev);
369 struct net_device_stats *stats = &dev->stats;
370 struct bfin_can_regs __iomem *reg = priv->membase;
371 struct can_frame *cf;
377 skb = alloc_can_skb(dev, &cf);
382 if (isrc & BIT(RECEIVE_EXT_CHL)) {
383 /* extended frame format (EFF) */
384 cf->can_id = ((bfin_read16(®->chl[RECEIVE_EXT_CHL].id1)
386 + bfin_read16(®->chl[RECEIVE_EXT_CHL].id0);
387 cf->can_id |= CAN_EFF_FLAG;
388 obj = RECEIVE_EXT_CHL;
390 /* standard frame format (SFF) */
391 cf->can_id = (bfin_read16(®->chl[RECEIVE_STD_CHL].id1)
393 obj = RECEIVE_STD_CHL;
395 if (bfin_read16(®->chl[obj].id1) & RTR)
396 cf->can_id |= CAN_RTR_FLAG;
398 /* get data length code */
399 cf->can_dlc = get_can_dlc(bfin_read16(®->chl[obj].dlc) & 0xF);
402 for (i = 0; i < 8; i += 2) {
403 val = bfin_read16(®->chl[obj].data[i]);
404 cf->data[7 - i] = (7 - i) < cf->can_dlc ? val : 0;
405 cf->data[6 - i] = (6 - i) < cf->can_dlc ? (val >> 8) : 0;
411 stats->rx_bytes += cf->can_dlc;
414 static int bfin_can_err(struct net_device *dev, u16 isrc, u16 status)
416 struct bfin_can_priv *priv = netdev_priv(dev);
417 struct bfin_can_regs __iomem *reg = priv->membase;
418 struct net_device_stats *stats = &dev->stats;
419 struct can_frame *cf;
421 enum can_state state = priv->can.state;
423 skb = alloc_can_err_skb(dev, &cf);
428 /* data overrun interrupt */
429 dev_dbg(dev->dev.parent, "data overrun interrupt\n");
430 cf->can_id |= CAN_ERR_CRTL;
431 cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
432 stats->rx_over_errors++;
437 dev_dbg(dev->dev.parent, "bus-off mode interrupt\n");
438 state = CAN_STATE_BUS_OFF;
439 cf->can_id |= CAN_ERR_BUSOFF;
444 /* error passive interrupt */
445 dev_dbg(dev->dev.parent, "error passive interrupt\n");
446 state = CAN_STATE_ERROR_PASSIVE;
449 if ((isrc & EWTIS) || (isrc & EWRIS)) {
450 dev_dbg(dev->dev.parent,
451 "Error Warning Transmit/Receive Interrupt\n");
452 state = CAN_STATE_ERROR_WARNING;
455 if (state != priv->can.state && (state == CAN_STATE_ERROR_WARNING ||
456 state == CAN_STATE_ERROR_PASSIVE)) {
457 u16 cec = bfin_read16(®->cec);
461 cf->can_id |= CAN_ERR_CRTL;
462 if (state == CAN_STATE_ERROR_WARNING) {
463 priv->can.can_stats.error_warning++;
464 cf->data[1] = (txerr > rxerr) ?
465 CAN_ERR_CRTL_TX_WARNING :
466 CAN_ERR_CRTL_RX_WARNING;
468 priv->can.can_stats.error_passive++;
469 cf->data[1] = (txerr > rxerr) ?
470 CAN_ERR_CRTL_TX_PASSIVE :
471 CAN_ERR_CRTL_RX_PASSIVE;
476 priv->can.can_stats.bus_error++;
478 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
481 cf->data[2] |= CAN_ERR_PROT_BIT;
482 else if (status & FER)
483 cf->data[2] |= CAN_ERR_PROT_FORM;
484 else if (status & SER)
485 cf->data[2] |= CAN_ERR_PROT_STUFF;
487 cf->data[2] |= CAN_ERR_PROT_UNSPEC;
490 priv->can.state = state;
495 stats->rx_bytes += cf->can_dlc;
500 irqreturn_t bfin_can_interrupt(int irq, void *dev_id)
502 struct net_device *dev = dev_id;
503 struct bfin_can_priv *priv = netdev_priv(dev);
504 struct bfin_can_regs __iomem *reg = priv->membase;
505 struct net_device_stats *stats = &dev->stats;
508 if ((irq == priv->tx_irq) && bfin_read16(®->mbtif2)) {
509 /* transmission complete interrupt */
510 bfin_write16(®->mbtif2, 0xFFFF);
512 stats->tx_bytes += bfin_read16(®->chl[TRANSMIT_CHL].dlc);
513 can_get_echo_skb(dev, 0);
514 netif_wake_queue(dev);
515 } else if ((irq == priv->rx_irq) && bfin_read16(®->mbrif1)) {
516 /* receive interrupt */
517 isrc = bfin_read16(®->mbrif1);
518 bfin_write16(®->mbrif1, 0xFFFF);
519 bfin_can_rx(dev, isrc);
520 } else if ((irq == priv->err_irq) && bfin_read16(®->gis)) {
521 /* error interrupt */
522 isrc = bfin_read16(®->gis);
523 status = bfin_read16(®->esr);
524 bfin_write16(®->gis, 0x7FF);
525 bfin_can_err(dev, isrc, status);
533 static int bfin_can_open(struct net_device *dev)
535 struct bfin_can_priv *priv = netdev_priv(dev);
538 /* set chip into reset mode */
539 bfin_can_set_reset_mode(dev);
542 err = open_candev(dev);
546 /* register interrupt handler */
547 err = request_irq(priv->rx_irq, &bfin_can_interrupt, 0,
551 err = request_irq(priv->tx_irq, &bfin_can_interrupt, 0,
555 err = request_irq(priv->err_irq, &bfin_can_interrupt, 0,
556 "bfin-can-err", dev);
562 netif_start_queue(dev);
567 free_irq(priv->tx_irq, dev);
569 free_irq(priv->rx_irq, dev);
576 static int bfin_can_close(struct net_device *dev)
578 struct bfin_can_priv *priv = netdev_priv(dev);
580 netif_stop_queue(dev);
581 bfin_can_set_reset_mode(dev);
585 free_irq(priv->rx_irq, dev);
586 free_irq(priv->tx_irq, dev);
587 free_irq(priv->err_irq, dev);
592 struct net_device *alloc_bfin_candev(void)
594 struct net_device *dev;
595 struct bfin_can_priv *priv;
597 dev = alloc_candev(sizeof(*priv), TX_ECHO_SKB_MAX);
601 priv = netdev_priv(dev);
604 priv->can.bittiming_const = &bfin_can_bittiming_const;
605 priv->can.do_set_bittiming = bfin_can_set_bittiming;
606 priv->can.do_set_mode = bfin_can_set_mode;
607 priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES;
612 static const struct net_device_ops bfin_can_netdev_ops = {
613 .ndo_open = bfin_can_open,
614 .ndo_stop = bfin_can_close,
615 .ndo_start_xmit = bfin_can_start_xmit,
618 static int __devinit bfin_can_probe(struct platform_device *pdev)
621 struct net_device *dev;
622 struct bfin_can_priv *priv;
623 struct resource *res_mem, *rx_irq, *tx_irq, *err_irq;
624 unsigned short *pdata;
626 pdata = pdev->dev.platform_data;
628 dev_err(&pdev->dev, "No platform data provided!\n");
633 res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
634 rx_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
635 tx_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
636 err_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 2);
637 if (!res_mem || !rx_irq || !tx_irq || !err_irq) {
642 if (!request_mem_region(res_mem->start, resource_size(res_mem),
643 dev_name(&pdev->dev))) {
648 /* request peripheral pins */
649 err = peripheral_request_list(pdata, dev_name(&pdev->dev));
651 goto exit_mem_release;
653 dev = alloc_bfin_candev();
656 goto exit_peri_pin_free;
659 priv = netdev_priv(dev);
660 priv->membase = (void __iomem *)res_mem->start;
661 priv->rx_irq = rx_irq->start;
662 priv->tx_irq = tx_irq->start;
663 priv->err_irq = err_irq->start;
664 priv->pin_list = pdata;
665 priv->can.clock.freq = get_sclk();
667 dev_set_drvdata(&pdev->dev, dev);
668 SET_NETDEV_DEV(dev, &pdev->dev);
670 dev->flags |= IFF_ECHO; /* we support local echo */
671 dev->netdev_ops = &bfin_can_netdev_ops;
673 bfin_can_set_reset_mode(dev);
675 err = register_candev(dev);
677 dev_err(&pdev->dev, "registering failed (err=%d)\n", err);
678 goto exit_candev_free;
682 "%s device registered"
683 "(®_base=%p, rx_irq=%d, tx_irq=%d, err_irq=%d, sclk=%d)\n",
684 DRV_NAME, (void *)priv->membase, priv->rx_irq,
685 priv->tx_irq, priv->err_irq, priv->can.clock.freq);
691 peripheral_free_list(pdata);
693 release_mem_region(res_mem->start, resource_size(res_mem));
698 static int __devexit bfin_can_remove(struct platform_device *pdev)
700 struct net_device *dev = dev_get_drvdata(&pdev->dev);
701 struct bfin_can_priv *priv = netdev_priv(dev);
702 struct resource *res;
704 bfin_can_set_reset_mode(dev);
706 unregister_candev(dev);
708 dev_set_drvdata(&pdev->dev, NULL);
710 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
711 release_mem_region(res->start, resource_size(res));
713 peripheral_free_list(priv->pin_list);
720 static int bfin_can_suspend(struct platform_device *pdev, pm_message_t mesg)
722 struct net_device *dev = dev_get_drvdata(&pdev->dev);
723 struct bfin_can_priv *priv = netdev_priv(dev);
724 struct bfin_can_regs __iomem *reg = priv->membase;
725 int timeout = BFIN_CAN_TIMEOUT;
727 if (netif_running(dev)) {
728 /* enter sleep mode */
729 bfin_write16(®->ctrl, bfin_read16(®->ctrl) | SMR);
731 while (!(bfin_read16(®->intr) & SMACK)) {
733 if (--timeout == 0) {
734 dev_err(dev->dev.parent,
735 "fail to enter sleep mode\n");
744 static int bfin_can_resume(struct platform_device *pdev)
746 struct net_device *dev = dev_get_drvdata(&pdev->dev);
747 struct bfin_can_priv *priv = netdev_priv(dev);
748 struct bfin_can_regs __iomem *reg = priv->membase;
750 if (netif_running(dev)) {
751 /* leave sleep mode */
752 bfin_write16(®->intr, 0);
759 #define bfin_can_suspend NULL
760 #define bfin_can_resume NULL
761 #endif /* CONFIG_PM */
763 static struct platform_driver bfin_can_driver = {
764 .probe = bfin_can_probe,
765 .remove = __devexit_p(bfin_can_remove),
766 .suspend = bfin_can_suspend,
767 .resume = bfin_can_resume,
770 .owner = THIS_MODULE,
774 static int __init bfin_can_init(void)
776 return platform_driver_register(&bfin_can_driver);
778 module_init(bfin_can_init);
780 static void __exit bfin_can_exit(void)
782 platform_driver_unregister(&bfin_can_driver);
784 module_exit(bfin_can_exit);
786 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
787 MODULE_LICENSE("GPL");
788 MODULE_DESCRIPTION("Blackfin on-chip CAN netdevice driver");