2 * CAN bus driver for the alone generic (as possible as) MSCAN controller.
4 * Copyright (C) 2005-2006 Andrey Volkov <avolkov@varma-el.com>,
6 * Copyright (C) 2008-2009 Wolfgang Grandegger <wg@grandegger.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the version 2 of the GNU General Public License
10 * as published by the Free Software Foundation
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/interrupt.h>
25 #include <linux/delay.h>
26 #include <linux/netdevice.h>
27 #include <linux/if_arp.h>
28 #include <linux/if_ether.h>
29 #include <linux/list.h>
30 #include <linux/can.h>
31 #include <linux/can/dev.h>
32 #include <linux/can/error.h>
33 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
41 #include <linux/can/version.h> /* for RCSID. Removed by mkpatch script */
44 #define MSCAN_NORMAL_MODE 0
45 #define MSCAN_SLEEP_MODE MSCAN_SLPRQ
46 #define MSCAN_INIT_MODE (MSCAN_INITRQ | MSCAN_SLPRQ)
47 #define MSCAN_POWEROFF_MODE (MSCAN_CSWAI | MSCAN_SLPRQ)
48 #define MSCAN_SET_MODE_RETRIES 255
50 #define BTR0_BRP_MASK 0x3f
51 #define BTR0_SJW_SHIFT 6
52 #define BTR0_SJW_MASK (0x3 << BTR0_SJW_SHIFT)
54 #define BTR1_TSEG1_MASK 0xf
55 #define BTR1_TSEG2_SHIFT 4
56 #define BTR1_TSEG2_MASK (0x7 << BTR1_TSEG2_SHIFT)
57 #define BTR1_SAM_SHIFT 7
59 #define BTR0_SET_BRP(brp) (((brp) - 1) & BTR0_BRP_MASK)
60 #define BTR0_SET_SJW(sjw) ((((sjw) - 1) << BTR0_SJW_SHIFT) & \
63 #define BTR1_SET_TSEG1(tseg1) (((tseg1) - 1) & BTR1_TSEG1_MASK)
64 #define BTR1_SET_TSEG2(tseg2) ((((tseg2) - 1) << BTR1_TSEG2_SHIFT) & \
66 #define BTR1_SET_SAM(sam) (((sam) & 1) << BTR1_SAM_SHIFT)
68 static struct can_bittiming_const mscan_bittiming_const = {
85 #define TX_QUEUE_SIZE 3
87 struct tx_queue_entry {
88 struct list_head list;
102 struct list_head tx_head;
103 struct tx_queue_entry tx_queue[TX_QUEUE_SIZE];
104 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,28)
105 struct napi_struct napi;
106 #elif LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
107 struct napi_struct napi;
108 struct net_device *dev;
112 #define F_RX_PROGRESS 0
113 #define F_TX_PROGRESS 1
114 #define F_TX_WAIT_ALL 2
116 static enum can_state state_map[] = {
118 CAN_STATE_BUS_WARNING,
119 CAN_STATE_BUS_PASSIVE,
123 static int mscan_set_mode(struct net_device *dev, u8 mode)
125 struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
126 struct mscan_priv *priv = netdev_priv(dev);
131 if (mode != MSCAN_NORMAL_MODE) {
133 if (priv->tx_active) {
134 /* Abort transfers before going to sleep */
135 out_8(®s->cantier, 0);
136 out_8(®s->cantarq, priv->tx_active);
137 out_8(®s->cantier, priv->tx_active);
140 canctl1 = in_8(®s->canctl1);
141 if ((mode & MSCAN_SLPRQ) && (canctl1 & MSCAN_SLPAK) == 0) {
142 out_8(®s->canctl0,
143 in_8(®s->canctl0) | MSCAN_SLPRQ);
144 for (i = 0; i < MSCAN_SET_MODE_RETRIES; i++) {
145 if (in_8(®s->canctl1) & MSCAN_SLPAK)
149 if (i >= MSCAN_SET_MODE_RETRIES)
153 priv->can.state = CAN_STATE_SLEEPING;
155 if (!ret && (mode & MSCAN_INITRQ)
156 && (canctl1 & MSCAN_INITAK) == 0) {
157 out_8(®s->canctl0,
158 in_8(®s->canctl0) | MSCAN_INITRQ);
159 for (i = 0; i < MSCAN_SET_MODE_RETRIES; i++) {
160 if (in_8(®s->canctl1) & MSCAN_INITAK)
163 if (i >= MSCAN_SET_MODE_RETRIES)
167 priv->can.state = CAN_STATE_STOPPED;
169 if (!ret && (mode & MSCAN_CSWAI))
170 out_8(®s->canctl0,
171 in_8(®s->canctl0) | MSCAN_CSWAI);
174 canctl1 = in_8(®s->canctl1);
175 if (canctl1 & (MSCAN_SLPAK | MSCAN_INITAK)) {
176 out_8(®s->canctl0, in_8(®s->canctl0) &
177 ~(MSCAN_SLPRQ | MSCAN_INITRQ));
178 for (i = 0; i < MSCAN_SET_MODE_RETRIES; i++) {
179 canctl1 = in_8(®s->canctl1);
180 if (!(canctl1 & (MSCAN_INITAK | MSCAN_SLPAK)))
183 if (i >= MSCAN_SET_MODE_RETRIES)
186 priv->can.state = CAN_STATE_ACTIVE;
192 static int mscan_start(struct net_device *dev)
194 struct mscan_priv *priv = netdev_priv(dev);
195 struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
199 out_8(®s->canrier, 0);
201 INIT_LIST_HEAD(&priv->tx_head);
204 priv->shadow_canrier = 0;
207 err = mscan_set_mode(dev, MSCAN_NORMAL_MODE);
211 canrflg = in_8(®s->canrflg);
212 priv->shadow_statflg = canrflg & MSCAN_STAT_MSK;
213 priv->can.state = state_map[max(MSCAN_STATE_RX(canrflg),
214 MSCAN_STATE_TX(canrflg))];
215 out_8(®s->cantier, 0);
217 /* Enable receive interrupts. */
218 out_8(®s->canrier, MSCAN_OVRIE | MSCAN_RXFIE | MSCAN_CSCIE |
219 MSCAN_RSTATE1 | MSCAN_RSTATE0 | MSCAN_TSTATE1 | MSCAN_TSTATE0);
224 static int mscan_start_xmit(struct sk_buff *skb, struct net_device *dev)
226 struct can_frame *frame = (struct can_frame *)skb->data;
227 struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
228 struct mscan_priv *priv = netdev_priv(dev);
232 if (frame->can_dlc > 8)
235 out_8(®s->cantier, 0);
237 i = ~priv->tx_active & MSCAN_TXE;
239 switch (hweight8(i)) {
241 netif_stop_queue(dev);
242 dev_err(ND2D(dev), "BUG! Tx Ring full when queue awake!\n");
243 return NETDEV_TX_BUSY;
245 /* if buf_id < 3, then current frame will be send out of order,
246 since buffer with lower id have higher priority (hell..) */
249 if (priv->cur_pri == 0xff)
250 set_bit(F_TX_WAIT_ALL, &priv->flags);
251 netif_stop_queue(dev);
253 set_bit(F_TX_PROGRESS, &priv->flags);
255 out_8(®s->cantbsel, i);
257 rtr = frame->can_id & CAN_RTR_FLAG;
259 if (frame->can_id & CAN_EFF_FLAG) {
260 can_id = (frame->can_id & CAN_EFF_MASK) << 1;
263 out_be16(®s->tx.idr3_2, can_id);
266 can_id = (can_id & 0x7) | ((can_id << 2) & 0xffe0) | (3 << 3);
268 can_id = (frame->can_id & CAN_SFF_MASK) << 5;
272 out_be16(®s->tx.idr1_0, can_id);
275 void __iomem *data = ®s->tx.dsr1_0;
276 u16 *payload = (u16 *) frame->data;
277 /*Its safe to write into dsr[dlc+1] */
278 for (i = 0; i < (frame->can_dlc + 1) / 2; i++) {
279 out_be16(data, *payload++);
280 data += 2 + _MSCAN_RESERVED_DSR_SIZE;
284 out_8(®s->tx.dlr, frame->can_dlc);
285 out_8(®s->tx.tbpr, priv->cur_pri);
287 /* Start transmission. */
288 out_8(®s->cantflg, 1 << buf_id);
290 if (!test_bit(F_TX_PROGRESS, &priv->flags))
291 dev->trans_start = jiffies;
293 list_add_tail(&priv->tx_queue[buf_id].list, &priv->tx_head);
295 can_put_echo_skb(skb, dev, buf_id);
297 /* Enable interrupt. */
298 priv->tx_active |= 1 << buf_id;
299 out_8(®s->cantier, priv->tx_active);
304 static inline int check_set_state(struct net_device *dev, u8 canrflg)
306 struct mscan_priv *priv = netdev_priv(dev);
307 enum can_state state;
310 if (!(canrflg & MSCAN_CSCIF) || priv->can.state > CAN_STATE_BUS_OFF)
313 state = state_map[max(MSCAN_STATE_RX(canrflg),
314 MSCAN_STATE_TX(canrflg))];
315 if (priv->can.state < state)
317 if (state == CAN_STATE_BUS_OFF)
319 priv->can.state = state;
323 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
324 static int mscan_rx_poll(struct napi_struct *napi, int quota)
326 static int mscan_rx_poll(struct net_device *dev, int *budget)
329 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,28)
330 struct mscan_priv *priv = container_of(napi, struct mscan_priv, napi);
331 struct net_device *dev = napi->dev;
332 #elif LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
333 struct mscan_priv *priv = container_of(napi, struct mscan_priv, napi);
334 struct net_device *dev = priv->dev;
336 struct mscan_priv *priv = netdev_priv(dev);
337 int quota = min(dev->quota, *budget);
339 struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
340 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
341 struct net_device_stats *stats = can_get_stats(dev);
343 struct net_device_stats *stats = &dev->stats;
348 struct can_frame *frame;
353 while (npackets < quota && ((canrflg = in_8(®s->canrflg)) &
354 (MSCAN_RXF | MSCAN_ERR_IF))) {
356 skb = dev_alloc_skb(sizeof(struct can_frame));
358 if (printk_ratelimit())
359 dev_notice(ND2D(dev), "packet dropped\n");
361 out_8(®s->canrflg, canrflg);
365 frame = (struct can_frame *)skb_put(skb, sizeof(*frame));
366 memset(frame, 0, sizeof(*frame));
368 if (canrflg & MSCAN_RXF) {
369 can_id = in_be16(®s->rx.idr1_0);
370 if (can_id & (1 << 3)) {
371 frame->can_id = CAN_EFF_FLAG;
372 can_id = ((can_id << 16) |
373 in_be16(®s->rx.idr3_2));
374 can_id = ((can_id & 0xffe00000) |
375 ((can_id & 0x7ffff) << 2)) >> 2;
381 frame->can_id |= can_id >> 1;
383 frame->can_id |= CAN_RTR_FLAG;
384 frame->can_dlc = in_8(®s->rx.dlr) & 0xf;
386 if (!(frame->can_id & CAN_RTR_FLAG)) {
387 void __iomem *data = ®s->rx.dsr1_0;
388 u16 *payload = (u16 *) frame->data;
389 for (i = 0; i < (frame->can_dlc + 1) / 2; i++) {
390 *payload++ = in_be16(data);
391 data += 2 + _MSCAN_RESERVED_DSR_SIZE;
395 out_8(®s->canrflg, MSCAN_RXF);
396 dev->last_rx = jiffies;
398 stats->rx_bytes += frame->can_dlc;
399 } else if (canrflg & MSCAN_ERR_IF) {
400 dev_dbg(ND2D(dev), "error interrupt (canrflg=%#x)\n",
402 frame->can_id = CAN_ERR_FLAG;
404 if (canrflg & MSCAN_OVRIF) {
405 frame->can_id |= CAN_ERR_CRTL;
406 frame->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
407 stats->rx_over_errors++;
411 if (check_set_state(dev, canrflg)) {
412 frame->can_id |= CAN_ERR_CRTL;
413 switch (priv->can.state) {
414 case CAN_STATE_BUS_WARNING:
415 if ((priv->shadow_statflg &
417 (canrflg & MSCAN_RSTAT_MSK))
419 CAN_ERR_CRTL_RX_WARNING;
421 if ((priv->shadow_statflg &
423 (canrflg & MSCAN_TSTAT_MSK))
425 CAN_ERR_CRTL_TX_WARNING;
427 case CAN_STATE_BUS_PASSIVE:
429 CAN_ERR_CRTL_RX_PASSIVE;
431 case CAN_STATE_BUS_OFF:
432 frame->can_id |= CAN_ERR_BUSOFF;
433 frame->can_id &= ~CAN_ERR_CRTL;
439 priv->shadow_statflg = canrflg & MSCAN_STAT_MSK;
440 frame->can_dlc = CAN_ERR_DLC;
441 out_8(®s->canrflg, MSCAN_ERR_IF);
446 skb->protocol = __constant_htons(ETH_P_CAN);
447 skb->ip_summed = CHECKSUM_UNNECESSARY;
448 netif_receive_skb(skb);
451 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
453 dev->quota -= npackets;
456 if (!(in_8(®s->canrflg) & (MSCAN_RXF | MSCAN_ERR_IF))) {
457 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,28)
458 napi_complete(&priv->napi);
459 #elif LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
460 netif_rx_complete(dev, &priv->napi);
462 netif_rx_complete(dev);
464 clear_bit(F_RX_PROGRESS, &priv->flags);
465 if (priv->can.state < CAN_STATE_BUS_OFF)
466 out_8(®s->canrier, priv->shadow_canrier);
472 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
473 static irqreturn_t mscan_isr(int irq, void *dev_id, struct pt_regs *r)
475 static irqreturn_t mscan_isr(int irq, void *dev_id)
478 struct net_device *dev = (struct net_device *)dev_id;
479 struct mscan_priv *priv = netdev_priv(dev);
480 struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
481 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
482 struct net_device_stats *stats = can_get_stats(dev);
484 struct net_device_stats *stats = &dev->stats;
486 u8 cantier, cantflg, canrflg;
487 irqreturn_t ret = IRQ_NONE;
489 cantier = in_8(®s->cantier) & MSCAN_TXE;
490 cantflg = in_8(®s->cantflg) & cantier;
492 if (cantier && cantflg) {
494 struct list_head *tmp, *pos;
496 list_for_each_safe(pos, tmp, &priv->tx_head) {
497 struct tx_queue_entry *entry =
498 list_entry(pos, struct tx_queue_entry, list);
499 u8 mask = entry->mask;
501 if (!(cantflg & mask))
504 out_8(®s->cantbsel, mask);
505 stats->tx_bytes += in_8(®s->tx.dlr);
507 can_get_echo_skb(dev, entry->id);
508 priv->tx_active &= ~mask;
512 if (list_empty(&priv->tx_head)) {
513 clear_bit(F_TX_WAIT_ALL, &priv->flags);
514 clear_bit(F_TX_PROGRESS, &priv->flags);
517 dev->trans_start = jiffies;
519 if (!test_bit(F_TX_WAIT_ALL, &priv->flags))
520 netif_wake_queue(dev);
522 out_8(®s->cantier, priv->tx_active);
526 canrflg = in_8(®s->canrflg);
527 if ((canrflg & ~MSCAN_STAT_MSK) &&
528 !test_and_set_bit(F_RX_PROGRESS, &priv->flags)) {
529 if (canrflg & ~MSCAN_STAT_MSK) {
530 priv->shadow_canrier = in_8(®s->canrier);
531 out_8(®s->canrier, 0);
532 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,28)
533 napi_schedule(&priv->napi);
534 #elif LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
535 netif_rx_schedule(dev, &priv->napi);
537 netif_rx_schedule(dev);
541 clear_bit(F_RX_PROGRESS, &priv->flags);
546 static int mscan_do_set_mode(struct net_device *dev, enum can_mode mode)
549 struct mscan_priv *priv = netdev_priv(dev);
552 if (!priv->open_time)
558 netif_stop_queue(dev);
561 CAN_MODE_STOP) ? MSCAN_INIT_MODE :
565 if (priv->can.state <= CAN_STATE_BUS_OFF)
566 mscan_set_mode(dev, MSCAN_INIT_MODE);
567 ret = mscan_start(dev);
570 if (netif_queue_stopped(dev))
571 netif_wake_queue(dev);
581 static int mscan_do_set_bittiming(struct net_device *dev)
583 struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
584 struct mscan_priv *priv = netdev_priv(dev);
585 struct can_bittiming *bt = &priv->can.bittiming;
588 btr0 = BTR0_SET_BRP(bt->brp) | BTR0_SET_SJW(bt->sjw);
589 btr1 = (BTR1_SET_TSEG1(bt->prop_seg + bt->phase_seg1) |
590 BTR1_SET_TSEG2(bt->phase_seg2) |
591 BTR1_SET_SAM(priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES));
593 dev_info(ND2D(dev), "setting BTR0=0x%02x BTR1=0x%02x\n", btr0, btr1);
595 out_8(®s->canbtr0, btr0);
596 out_8(®s->canbtr1, btr1);
601 static int mscan_open(struct net_device *dev)
604 struct mscan_priv *priv = netdev_priv(dev);
605 struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
607 /* determine and set bittime */
608 ret = can_set_bittiming(dev);
612 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
613 napi_enable(&priv->napi);
615 ret = request_irq(dev->irq, mscan_isr, 0, dev->name, dev);
618 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
619 napi_disable(&priv->napi);
621 printk(KERN_ERR "%s - failed to attach interrupt\n",
626 priv->open_time = jiffies;
628 out_8(®s->canctl1, in_8(®s->canctl1) & ~MSCAN_LISTEN);
630 ret = mscan_start(dev);
634 netif_start_queue(dev);
639 static int mscan_close(struct net_device *dev)
641 struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
642 struct mscan_priv *priv = netdev_priv(dev);
644 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
645 napi_disable(&priv->napi);
648 out_8(®s->cantier, 0);
649 out_8(®s->canrier, 0);
650 free_irq(dev->irq, dev);
651 mscan_set_mode(dev, MSCAN_INIT_MODE);
652 can_close_cleanup(dev);
653 netif_stop_queue(dev);
659 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,28)
660 static const struct net_device_ops mscan_netdev_ops = {
661 .ndo_open = mscan_open,
662 .ndo_stop = mscan_close,
663 .ndo_start_xmit = mscan_start_xmit,
667 int register_mscandev(struct net_device *dev, int clock_src)
669 struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
672 ctl1 = in_8(®s->canctl1);
674 ctl1 |= MSCAN_CLKSRC;
676 ctl1 &= ~MSCAN_CLKSRC;
679 out_8(®s->canctl1, ctl1);
682 /* acceptance mask/acceptance code (accept everything) */
683 out_be16(®s->canidar1_0, 0);
684 out_be16(®s->canidar3_2, 0);
685 out_be16(®s->canidar5_4, 0);
686 out_be16(®s->canidar7_6, 0);
688 out_be16(®s->canidmr1_0, 0xffff);
689 out_be16(®s->canidmr3_2, 0xffff);
690 out_be16(®s->canidmr5_4, 0xffff);
691 out_be16(®s->canidmr7_6, 0xffff);
692 /* Two 32 bit Acceptance Filters */
693 out_8(®s->canidac, MSCAN_AF_32BIT);
695 mscan_set_mode(dev, MSCAN_INIT_MODE);
697 return register_candev(dev);
699 EXPORT_SYMBOL_GPL(register_mscandev);
701 void unregister_mscandev(struct net_device *dev)
703 struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
704 mscan_set_mode(dev, MSCAN_INIT_MODE);
705 out_8(®s->canctl1, in_8(®s->canctl1) & ~MSCAN_CANE);
706 unregister_candev(dev);
708 EXPORT_SYMBOL_GPL(unregister_mscandev);
710 struct net_device *alloc_mscandev(void)
712 struct net_device *dev;
713 struct mscan_priv *priv;
716 dev = alloc_candev(sizeof(struct mscan_priv));
719 priv = netdev_priv(dev);
721 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,28)
722 dev->netdev_ops = &mscan_netdev_ops;
724 dev->open = mscan_open;
725 dev->stop = mscan_close;
726 dev->hard_start_xmit = mscan_start_xmit;
729 dev->flags |= IFF_ECHO; /* we support local echo */
731 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,28)
732 netif_napi_add(dev, &priv->napi, mscan_rx_poll, 8);
733 #elif LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
735 netif_napi_add(dev, &priv->napi, mscan_rx_poll, 8);
737 dev->poll = mscan_rx_poll;
741 priv->can.bittiming_const = &mscan_bittiming_const;
742 priv->can.do_set_bittiming = mscan_do_set_bittiming;
743 priv->can.do_set_mode = mscan_do_set_mode;
745 for (i = 0; i < TX_QUEUE_SIZE; i++) {
746 priv->tx_queue[i].id = i;
747 priv->tx_queue[i].mask = 1 << i;
752 EXPORT_SYMBOL_GPL(alloc_mscandev);
754 MODULE_AUTHOR("Andrey Volkov <avolkov@varma-el.com>");
755 MODULE_LICENSE("GPL v2");
756 MODULE_DESCRIPTION("CAN port driver for a MSCAN based chips");