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
31 * transmit and receive channels
33 #define TRANSMIT_CHL 24
34 #define RECEIVE_STD_CHL 0
35 #define RECEIVE_EXT_CHL 4
36 #define RECEIVE_RTR_CHL 8
37 #define RECEIVE_EXT_RTR_CHL 12
38 #define MAX_CHL_NUMBER 32
41 * bfin can registers layout
43 struct bfin_can_mask_regs {
50 struct bfin_can_channel_regs {
62 struct bfin_can_regs {
64 * global control and status registers
66 u16 mc1; /* offset 0 */
68 u16 md1; /* offset 4 */
70 u16 mbtif1; /* offset 0x20 */
72 u16 mbrif1; /* offset 0x24 */
74 u16 mbim1; /* offset 0x28 */
76 u16 mc2; /* offset 0x40 */
78 u16 md2; /* offset 0x44 */
80 u16 trs2; /* offset 0x48 */
82 u16 mbtif2; /* offset 0x60 */
84 u16 mbrif2; /* offset 0x64 */
86 u16 mbim2; /* offset 0x68 */
88 u16 clk; /* offset 0x80 */
90 u16 timing; /* offset 0x84 */
92 u16 status; /* offset 0x8c */
94 u16 cec; /* offset 0x90 */
96 u16 gis; /* offset 0x94 */
98 u16 gim; /* offset 0x98 */
100 u16 ctrl; /* offset 0xa0 */
102 u16 intr; /* offset 0xa4 */
104 u16 esr; /* offset 0xb4 */
108 * channel(mailbox) mask and message registers
110 struct bfin_can_mask_regs msk[MAX_CHL_NUMBER]; /* offset 0x100 */
111 struct bfin_can_channel_regs chl[MAX_CHL_NUMBER]; /* offset 0x200 */
115 * bfin can private data
117 struct bfin_can_priv {
118 struct can_priv can; /* must be the first member */
119 struct net_device *dev;
120 void __iomem *membase;
124 unsigned short *pin_list;
128 * bfin can timing parameters
130 static struct can_bittiming_const bfin_can_bittiming_const = {
138 * Although the BRP field can be set to any value, it is recommended
139 * that the value be greater than or equal to 4, as restrictions
140 * apply to the bit timing configuration when BRP is less than 4.
147 static int bfin_can_set_bittiming(struct net_device *dev)
149 struct bfin_can_priv *priv = netdev_priv(dev);
150 struct bfin_can_regs __iomem *reg = priv->membase;
151 struct can_bittiming *bt = &priv->can.bittiming;
155 timing = ((bt->sjw - 1) << 8) | (bt->prop_seg + bt->phase_seg1 - 1) |
156 ((bt->phase_seg2 - 1) << 4);
159 * If the SAM bit is set, the input signal is oversampled three times
162 if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
165 bfin_write16(®->clk, clk);
166 bfin_write16(®->timing, timing);
168 dev_info(dev->dev.parent, "setting CLOCK=0x%04x TIMING=0x%04x\n",
174 static void bfin_can_set_reset_mode(struct net_device *dev)
176 struct bfin_can_priv *priv = netdev_priv(dev);
177 struct bfin_can_regs __iomem *reg = priv->membase;
178 int timeout = BFIN_CAN_TIMEOUT;
181 /* disable interrupts */
182 bfin_write16(®->mbim1, 0);
183 bfin_write16(®->mbim2, 0);
184 bfin_write16(®->gim, 0);
186 /* reset can and enter configuration mode */
187 bfin_write16(®->ctrl, SRS | CCR);
189 bfin_write16(®->ctrl, CCR);
191 while (!(bfin_read16(®->ctrl) & CCA)) {
193 if (--timeout == 0) {
194 dev_err(dev->dev.parent,
195 "fail to enter configuration mode\n");
201 * All mailbox configurations are marked as inactive
202 * by writing to CAN Mailbox Configuration Registers 1 and 2
203 * For all bits: 0 - Mailbox disabled, 1 - Mailbox enabled
205 bfin_write16(®->mc1, 0);
206 bfin_write16(®->mc2, 0);
208 /* Set Mailbox Direction */
209 bfin_write16(®->md1, 0xFFFF); /* mailbox 1-16 are RX */
210 bfin_write16(®->md2, 0); /* mailbox 17-32 are TX */
212 /* RECEIVE_STD_CHL */
213 for (i = 0; i < 2; i++) {
214 bfin_write16(®->chl[RECEIVE_STD_CHL + i].id0, 0);
215 bfin_write16(®->chl[RECEIVE_STD_CHL + i].id1, AME);
216 bfin_write16(®->chl[RECEIVE_STD_CHL + i].dlc, 0);
217 bfin_write16(®->msk[RECEIVE_STD_CHL + i].amh, 0x1FFF);
218 bfin_write16(®->msk[RECEIVE_STD_CHL + i].aml, 0xFFFF);
221 /* RECEIVE_EXT_CHL */
222 for (i = 0; i < 2; i++) {
223 bfin_write16(®->chl[RECEIVE_EXT_CHL + i].id0, 0);
224 bfin_write16(®->chl[RECEIVE_EXT_CHL + i].id1, AME | IDE);
225 bfin_write16(®->chl[RECEIVE_EXT_CHL + i].dlc, 0);
226 bfin_write16(®->msk[RECEIVE_EXT_CHL + i].amh, 0x1FFF);
227 bfin_write16(®->msk[RECEIVE_EXT_CHL + i].aml, 0xFFFF);
230 bfin_write16(®->mc2, BIT(TRANSMIT_CHL - 16));
231 bfin_write16(®->mc1, BIT(RECEIVE_STD_CHL) + BIT(RECEIVE_EXT_CHL));
234 priv->can.state = CAN_STATE_STOPPED;
237 static void bfin_can_set_normal_mode(struct net_device *dev)
239 struct bfin_can_priv *priv = netdev_priv(dev);
240 struct bfin_can_regs __iomem *reg = priv->membase;
241 int timeout = BFIN_CAN_TIMEOUT;
244 * leave configuration mode
246 bfin_write16(®->ctrl, bfin_read16(®->ctrl) & ~CCR);
248 while (bfin_read16(®->status) & CCA) {
250 if (--timeout == 0) {
251 dev_err(dev->dev.parent,
252 "fail to leave configuration mode\n");
258 * clear _All_ tx and rx interrupts
260 bfin_write16(®->mbtif1, 0xFFFF);
261 bfin_write16(®->mbtif2, 0xFFFF);
262 bfin_write16(®->mbrif1, 0xFFFF);
263 bfin_write16(®->mbrif2, 0xFFFF);
266 * clear global interrupt status register
268 bfin_write16(®->gis, 0x7FF); /* overwrites with '1' */
271 * Initialize Interrupts
272 * - set bits in the mailbox interrupt mask register
273 * - global interrupt mask
275 bfin_write16(®->mbim1, BIT(RECEIVE_STD_CHL) + BIT(RECEIVE_EXT_CHL));
276 bfin_write16(®->mbim2, BIT(TRANSMIT_CHL - 16));
278 bfin_write16(®->gim, EPIM | BOIM | RMLIM);
282 static void bfin_can_start(struct net_device *dev)
284 struct bfin_can_priv *priv = netdev_priv(dev);
286 /* enter reset mode */
287 if (priv->can.state != CAN_STATE_STOPPED)
288 bfin_can_set_reset_mode(dev);
290 /* leave reset mode */
291 bfin_can_set_normal_mode(dev);
294 static int bfin_can_set_mode(struct net_device *dev, enum can_mode mode)
299 if (netif_queue_stopped(dev))
300 netif_wake_queue(dev);
310 static int bfin_can_start_xmit(struct sk_buff *skb, struct net_device *dev)
312 struct bfin_can_priv *priv = netdev_priv(dev);
313 struct bfin_can_regs __iomem *reg = priv->membase;
314 struct can_frame *cf = (struct can_frame *)skb->data;
315 u8 dlc = cf->can_dlc;
316 canid_t id = cf->can_id;
321 netif_stop_queue(dev);
324 if (id & CAN_EFF_FLAG) {
325 bfin_write16(®->chl[TRANSMIT_CHL].id0, id);
326 if (id & CAN_RTR_FLAG)
327 writew(((id & 0x1FFF0000) >> 16) | IDE | AME | RTR,
328 ®->chl[TRANSMIT_CHL].id1);
330 writew(((id & 0x1FFF0000) >> 16) | IDE | AME,
331 ®->chl[TRANSMIT_CHL].id1);
334 if (id & CAN_RTR_FLAG)
335 writew((id << 2) | AME | RTR,
336 ®->chl[TRANSMIT_CHL].id1);
338 bfin_write16(®->chl[TRANSMIT_CHL].id1,
343 for (i = 0; i < 8; i += 2) {
344 val = ((7 - i) < dlc ? (data[7 - i]) : 0) +
345 ((6 - i) < dlc ? (data[6 - i] << 8) : 0);
346 bfin_write16(®->chl[TRANSMIT_CHL].data[i], val);
349 /* fill data length code */
350 bfin_write16(®->chl[TRANSMIT_CHL].dlc, dlc);
352 dev->trans_start = jiffies;
354 can_put_echo_skb(skb, dev, 0);
356 /* set transmit request */
357 bfin_write16(®->trs2, BIT(TRANSMIT_CHL - 16));
362 static void bfin_can_rx(struct net_device *dev, u16 isrc)
364 struct bfin_can_priv *priv = netdev_priv(dev);
365 struct net_device_stats *stats = &dev->stats;
366 struct bfin_can_regs __iomem *reg = priv->membase;
367 struct can_frame *cf;
373 skb = alloc_can_skb(dev, &cf);
378 if (isrc & BIT(RECEIVE_EXT_CHL)) {
379 /* extended frame format (EFF) */
380 cf->can_id = ((bfin_read16(®->chl[RECEIVE_EXT_CHL].id1)
382 + bfin_read16(®->chl[RECEIVE_EXT_CHL].id0);
383 cf->can_id |= CAN_EFF_FLAG;
384 obj = RECEIVE_EXT_CHL;
386 /* standard frame format (SFF) */
387 cf->can_id = (bfin_read16(®->chl[RECEIVE_STD_CHL].id1)
389 obj = RECEIVE_STD_CHL;
391 if (bfin_read16(®->chl[obj].id1) & RTR)
392 cf->can_id |= CAN_RTR_FLAG;
394 /* get data length code */
395 cf->can_dlc = get_can_dlc(bfin_read16(®->chl[obj].dlc) & 0xF);
398 for (i = 0; i < 8; i += 2) {
399 val = bfin_read16(®->chl[obj].data[i]);
400 cf->data[7 - i] = (7 - i) < cf->can_dlc ? val : 0;
401 cf->data[6 - i] = (6 - i) < cf->can_dlc ? (val >> 8) : 0;
407 stats->rx_bytes += cf->can_dlc;
410 static int bfin_can_err(struct net_device *dev, u16 isrc, u16 status)
412 struct bfin_can_priv *priv = netdev_priv(dev);
413 struct bfin_can_regs __iomem *reg = priv->membase;
414 struct net_device_stats *stats = &dev->stats;
415 struct can_frame *cf;
417 enum can_state state = priv->can.state;
419 skb = alloc_can_err_skb(dev, &cf);
424 /* data overrun interrupt */
425 dev_dbg(dev->dev.parent, "data overrun interrupt\n");
426 cf->can_id |= CAN_ERR_CRTL;
427 cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
428 stats->rx_over_errors++;
433 dev_dbg(dev->dev.parent, "bus-off mode interrupt\n");
434 state = CAN_STATE_BUS_OFF;
435 cf->can_id |= CAN_ERR_BUSOFF;
440 /* error passive interrupt */
441 dev_dbg(dev->dev.parent, "error passive interrupt\n");
442 state = CAN_STATE_ERROR_PASSIVE;
445 if ((isrc & EWTIS) || (isrc & EWRIS)) {
446 dev_dbg(dev->dev.parent,
447 "Error Warning Transmit/Receive Interrupt\n");
448 state = CAN_STATE_ERROR_WARNING;
451 if (state != priv->can.state && (state == CAN_STATE_ERROR_WARNING ||
452 state == CAN_STATE_ERROR_PASSIVE)) {
453 u16 cec = bfin_read16(®->cec);
457 cf->can_id |= CAN_ERR_CRTL;
458 if (state == CAN_STATE_ERROR_WARNING) {
459 priv->can.can_stats.error_warning++;
460 cf->data[1] = (txerr > rxerr) ?
461 CAN_ERR_CRTL_TX_WARNING :
462 CAN_ERR_CRTL_RX_WARNING;
464 priv->can.can_stats.error_passive++;
465 cf->data[1] = (txerr > rxerr) ?
466 CAN_ERR_CRTL_TX_PASSIVE :
467 CAN_ERR_CRTL_RX_PASSIVE;
472 priv->can.can_stats.bus_error++;
474 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
477 cf->data[2] |= CAN_ERR_PROT_BIT;
478 else if (status & FER)
479 cf->data[2] |= CAN_ERR_PROT_FORM;
480 else if (status & SER)
481 cf->data[2] |= CAN_ERR_PROT_STUFF;
483 cf->data[2] |= CAN_ERR_PROT_UNSPEC;
486 priv->can.state = state;
491 stats->rx_bytes += cf->can_dlc;
496 irqreturn_t bfin_can_interrupt(int irq, void *dev_id)
498 struct net_device *dev = dev_id;
499 struct bfin_can_priv *priv = netdev_priv(dev);
500 struct bfin_can_regs __iomem *reg = priv->membase;
501 struct net_device_stats *stats = &dev->stats;
504 if ((irq == priv->tx_irq) && bfin_read16(®->mbtif2)) {
505 /* transmission complete interrupt */
506 bfin_write16(®->mbtif2, 0xFFFF);
508 stats->tx_bytes += bfin_read16(®->chl[TRANSMIT_CHL].dlc);
509 can_get_echo_skb(dev, 0);
510 netif_wake_queue(dev);
511 } else if ((irq == priv->rx_irq) && bfin_read16(®->mbrif1)) {
512 /* receive interrupt */
513 isrc = bfin_read16(®->mbrif1);
514 bfin_write16(®->mbrif1, 0xFFFF);
515 bfin_can_rx(dev, isrc);
516 } else if ((irq == priv->err_irq) && bfin_read16(®->gis)) {
517 /* error interrupt */
518 isrc = bfin_read16(®->gis);
519 status = bfin_read16(®->esr);
520 bfin_write16(®->gis, 0x7FF);
521 bfin_can_err(dev, isrc, status);
529 static int bfin_can_open(struct net_device *dev)
531 struct bfin_can_priv *priv = netdev_priv(dev);
534 /* set chip into reset mode */
535 bfin_can_set_reset_mode(dev);
538 err = open_candev(dev);
542 /* register interrupt handler */
543 err = request_irq(priv->rx_irq, &bfin_can_interrupt, 0,
547 err = request_irq(priv->tx_irq, &bfin_can_interrupt, 0,
551 err = request_irq(priv->err_irq, &bfin_can_interrupt, 0,
552 "bfin-can-err", dev);
558 netif_start_queue(dev);
563 free_irq(priv->tx_irq, dev);
565 free_irq(priv->rx_irq, dev);
572 static int bfin_can_close(struct net_device *dev)
574 struct bfin_can_priv *priv = netdev_priv(dev);
576 netif_stop_queue(dev);
577 bfin_can_set_reset_mode(dev);
581 free_irq(priv->rx_irq, dev);
582 free_irq(priv->tx_irq, dev);
583 free_irq(priv->err_irq, dev);
588 struct net_device *alloc_bfin_candev(void)
590 struct net_device *dev;
591 struct bfin_can_priv *priv;
593 dev = alloc_candev(sizeof(*priv));
597 priv = netdev_priv(dev);
600 priv->can.bittiming_const = &bfin_can_bittiming_const;
601 priv->can.do_set_bittiming = bfin_can_set_bittiming;
602 priv->can.do_set_mode = bfin_can_set_mode;
607 static const struct net_device_ops bfin_can_netdev_ops = {
608 .ndo_open = bfin_can_open,
609 .ndo_stop = bfin_can_close,
610 .ndo_start_xmit = bfin_can_start_xmit,
613 static int __devinit bfin_can_probe(struct platform_device *pdev)
616 struct net_device *dev;
617 struct bfin_can_priv *priv;
618 struct resource *res_mem, *rx_irq, *tx_irq, *err_irq;
619 unsigned short *pdata;
621 pdata = pdev->dev.platform_data;
623 dev_err(&pdev->dev, "No platform data provided!\n");
628 res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
629 rx_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
630 tx_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
631 err_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 2);
632 if (!res_mem || !rx_irq || !tx_irq || !err_irq) {
637 if (!request_mem_region(res_mem->start, resource_size(res_mem),
638 dev_name(&pdev->dev))) {
643 /* request peripheral pins */
644 err = peripheral_request_list(pdata, dev_name(&pdev->dev));
646 goto exit_mem_release;
648 dev = alloc_bfin_candev();
651 goto exit_peri_pin_free;
654 priv = netdev_priv(dev);
655 priv->membase = (void __iomem *)res_mem->start;
656 priv->rx_irq = rx_irq->start;
657 priv->tx_irq = tx_irq->start;
658 priv->err_irq = err_irq->start;
659 priv->pin_list = pdata;
660 priv->can.clock.freq = get_sclk();
662 dev_set_drvdata(&pdev->dev, dev);
663 SET_NETDEV_DEV(dev, &pdev->dev);
665 dev->flags |= IFF_ECHO; /* we support local echo */
666 dev->netdev_ops = &bfin_can_netdev_ops;
668 bfin_can_set_reset_mode(dev);
670 err = register_candev(dev);
672 dev_err(&pdev->dev, "registering failed (err=%d)\n", err);
673 goto exit_candev_free;
677 "%s device registered"
678 "(®_base=%p, rx_irq=%d, tx_irq=%d, err_irq=%d, sclk=%d)\n",
679 DRV_NAME, (void *)priv->membase, priv->rx_irq,
680 priv->tx_irq, priv->err_irq, priv->can.clock.freq);
686 peripheral_free_list(pdata);
688 release_mem_region(res_mem->start, resource_size(res_mem));
693 static int __devexit bfin_can_remove(struct platform_device *pdev)
695 struct net_device *dev = dev_get_drvdata(&pdev->dev);
696 struct bfin_can_priv *priv = netdev_priv(dev);
697 struct resource *res;
699 bfin_can_set_reset_mode(dev);
701 unregister_candev(dev);
703 dev_set_drvdata(&pdev->dev, NULL);
705 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
706 release_mem_region(res->start, resource_size(res));
708 peripheral_free_list(priv->pin_list);
715 static int bfin_can_suspend(struct platform_device *pdev, pm_message_t mesg)
717 struct net_device *dev = dev_get_drvdata(&pdev->dev);
718 struct bfin_can_priv *priv = netdev_priv(dev);
719 struct bfin_can_regs __iomem *reg = priv->membase;
720 int timeout = BFIN_CAN_TIMEOUT;
722 if (netif_running(dev)) {
723 /* enter sleep mode */
724 bfin_write16(®->ctrl, bfin_read16(®->ctrl) | SMR);
726 while (!(bfin_read16(®->intr) & SMACK)) {
728 if (--timeout == 0) {
729 dev_err(dev->dev.parent,
730 "fail to enter sleep mode\n");
739 static int bfin_can_resume(struct platform_device *pdev)
741 struct net_device *dev = dev_get_drvdata(&pdev->dev);
742 struct bfin_can_priv *priv = netdev_priv(dev);
743 struct bfin_can_regs __iomem *reg = priv->membase;
745 if (netif_running(dev)) {
746 /* leave sleep mode */
747 bfin_write16(®->intr, 0);
754 #define bfin_can_suspend NULL
755 #define bfin_can_resume NULL
756 #endif /* CONFIG_PM */
758 static struct platform_driver bfin_can_driver = {
759 .probe = bfin_can_probe,
760 .remove = __devexit_p(bfin_can_remove),
761 .suspend = bfin_can_suspend,
762 .resume = bfin_can_resume,
765 .owner = THIS_MODULE,
769 static int __init bfin_can_init(void)
771 return platform_driver_register(&bfin_can_driver);
773 module_init(bfin_can_init);
775 static void __exit bfin_can_exit(void)
777 platform_driver_unregister(&bfin_can_driver);
779 module_exit(bfin_can_exit);
781 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
782 MODULE_LICENSE("GPL");
783 MODULE_DESCRIPTION("Blackfin on-chip CAN netdevice driver");