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 <socketcan/can.h>
31 #include <socketcan/can/dev.h>
32 #include <socketcan/can/error.h>
33 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
41 #include <socketcan/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
49 #define MSCAN_ECHO_SKB_MAX 3
51 #define BTR0_BRP_MASK 0x3f
52 #define BTR0_SJW_SHIFT 6
53 #define BTR0_SJW_MASK (0x3 << BTR0_SJW_SHIFT)
55 #define BTR1_TSEG1_MASK 0xf
56 #define BTR1_TSEG2_SHIFT 4
57 #define BTR1_TSEG2_MASK (0x7 << BTR1_TSEG2_SHIFT)
58 #define BTR1_SAM_SHIFT 7
60 #define BTR0_SET_BRP(brp) (((brp) - 1) & BTR0_BRP_MASK)
61 #define BTR0_SET_SJW(sjw) ((((sjw) - 1) << BTR0_SJW_SHIFT) & \
64 #define BTR1_SET_TSEG1(tseg1) (((tseg1) - 1) & BTR1_TSEG1_MASK)
65 #define BTR1_SET_TSEG2(tseg2) ((((tseg2) - 1) << BTR1_TSEG2_SHIFT) & \
67 #define BTR1_SET_SAM(sam) ((sam) ? 1 << BTR1_SAM_SHIFT : 0)
69 static struct can_bittiming_const mscan_bittiming_const = {
87 #define TX_QUEUE_SIZE 3
89 struct tx_queue_entry {
90 struct list_head list;
105 struct list_head tx_head;
106 struct tx_queue_entry tx_queue[TX_QUEUE_SIZE];
107 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,28)
108 struct napi_struct napi;
109 #elif LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
110 struct napi_struct napi;
111 struct net_device *dev;
115 #define F_RX_PROGRESS 0
116 #define F_TX_PROGRESS 1
117 #define F_TX_WAIT_ALL 2
119 static enum can_state state_map[] = {
120 CAN_STATE_ERROR_ACTIVE,
121 CAN_STATE_ERROR_WARNING,
122 CAN_STATE_ERROR_PASSIVE,
126 static int mscan_set_mode(struct net_device *dev, u8 mode)
128 struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
129 struct mscan_priv *priv = netdev_priv(dev);
134 if (mode != MSCAN_NORMAL_MODE) {
136 if (priv->tx_active) {
137 /* Abort transfers before going to sleep */#
138 out_8(®s->cantarq, priv->tx_active);
139 /* Suppress TX done interrupts */
140 out_8(®s->cantier, 0);
143 canctl1 = in_8(®s->canctl1);
144 if ((mode & MSCAN_SLPRQ) && (canctl1 & MSCAN_SLPAK) == 0) {
145 out_8(®s->canctl0,
146 in_8(®s->canctl0) | MSCAN_SLPRQ);
147 for (i = 0; i < MSCAN_SET_MODE_RETRIES; i++) {
148 if (in_8(®s->canctl1) & MSCAN_SLPAK)
152 if (i >= MSCAN_SET_MODE_RETRIES)
156 priv->can.state = CAN_STATE_SLEEPING;
158 if (!ret && (mode & MSCAN_INITRQ)
159 && (canctl1 & MSCAN_INITAK) == 0) {
160 out_8(®s->canctl0,
161 in_8(®s->canctl0) | MSCAN_INITRQ);
162 for (i = 0; i < MSCAN_SET_MODE_RETRIES; i++) {
163 if (in_8(®s->canctl1) & MSCAN_INITAK)
166 if (i >= MSCAN_SET_MODE_RETRIES)
170 priv->can.state = CAN_STATE_STOPPED;
172 if (!ret && (mode & MSCAN_CSWAI))
173 out_8(®s->canctl0,
174 in_8(®s->canctl0) | MSCAN_CSWAI);
177 canctl1 = in_8(®s->canctl1);
178 if (canctl1 & (MSCAN_SLPAK | MSCAN_INITAK)) {
179 out_8(®s->canctl0, in_8(®s->canctl0) &
180 ~(MSCAN_SLPRQ | MSCAN_INITRQ));
181 for (i = 0; i < MSCAN_SET_MODE_RETRIES; i++) {
182 canctl1 = in_8(®s->canctl1);
183 if (!(canctl1 & (MSCAN_INITAK | MSCAN_SLPAK)))
186 if (i >= MSCAN_SET_MODE_RETRIES)
189 priv->can.state = CAN_STATE_ERROR_ACTIVE;
195 static int mscan_start(struct net_device *dev)
197 struct mscan_priv *priv = netdev_priv(dev);
198 struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
202 out_8(®s->canrier, 0);
204 INIT_LIST_HEAD(&priv->tx_head);
205 priv->prev_buf_id = 0;
208 priv->shadow_canrier = 0;
211 err = mscan_set_mode(dev, MSCAN_NORMAL_MODE);
215 canrflg = in_8(®s->canrflg);
216 priv->shadow_statflg = canrflg & MSCAN_STAT_MSK;
217 priv->can.state = state_map[max(MSCAN_STATE_RX(canrflg),
218 MSCAN_STATE_TX(canrflg))];
219 out_8(®s->cantier, 0);
221 /* Enable receive interrupts. */
222 out_8(®s->canrier, MSCAN_OVRIE | MSCAN_RXFIE | MSCAN_CSCIE |
223 MSCAN_RSTATE1 | MSCAN_RSTATE0 | MSCAN_TSTATE1 | MSCAN_TSTATE0);
228 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,32)
229 static int mscan_start_xmit(struct sk_buff *skb, struct net_device *dev)
231 static netdev_tx_t mscan_start_xmit(struct sk_buff *skb, struct net_device *dev)
234 struct can_frame *frame = (struct can_frame *)skb->data;
235 struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
236 struct mscan_priv *priv = netdev_priv(dev);
240 if (frame->can_dlc > 8)
243 out_8(®s->cantier, 0);
245 i = ~priv->tx_active & MSCAN_TXE;
247 switch (hweight8(i)) {
249 netif_stop_queue(dev);
250 dev_err(ND2D(dev), "BUG! Tx Ring full when queue awake!\n");
251 return NETDEV_TX_BUSY;
253 /* if buf_id < 3, then current frame will be send out of order,
254 since buffer with lower id have higher priority (hell..) */
255 netif_stop_queue(dev);
257 if (buf_id < priv->prev_buf_id) {
259 if (priv->cur_pri == 0xff) {
260 set_bit(F_TX_WAIT_ALL, &priv->flags);
261 netif_stop_queue(dev);
264 set_bit(F_TX_PROGRESS, &priv->flags);
267 priv->prev_buf_id = buf_id;
268 out_8(®s->cantbsel, i);
270 rtr = frame->can_id & CAN_RTR_FLAG;
272 if (frame->can_id & CAN_EFF_FLAG) {
273 can_id = (frame->can_id & CAN_EFF_MASK) << 1;
276 out_be16(®s->tx.idr3_2, can_id);
279 can_id = (can_id & 0x7) | ((can_id << 2) & 0xffe0) | (3 << 3);
281 can_id = (frame->can_id & CAN_SFF_MASK) << 5;
285 out_be16(®s->tx.idr1_0, can_id);
288 void __iomem *data = ®s->tx.dsr1_0;
289 u16 *payload = (u16 *) frame->data;
290 /*Its safe to write into dsr[dlc+1] */
291 for (i = 0; i < (frame->can_dlc + 1) / 2; i++) {
292 out_be16(data, *payload++);
293 data += 2 + _MSCAN_RESERVED_DSR_SIZE;
297 out_8(®s->tx.dlr, frame->can_dlc);
298 out_8(®s->tx.tbpr, priv->cur_pri);
300 /* Start transmission. */
301 out_8(®s->cantflg, 1 << buf_id);
303 if (!test_bit(F_TX_PROGRESS, &priv->flags))
304 dev->trans_start = jiffies;
306 list_add_tail(&priv->tx_queue[buf_id].list, &priv->tx_head);
308 can_put_echo_skb(skb, dev, buf_id);
310 /* Enable interrupt. */
311 priv->tx_active |= 1 << buf_id;
312 out_8(®s->cantier, priv->tx_active);
317 static inline int check_set_state(struct net_device *dev, u8 canrflg)
319 struct mscan_priv *priv = netdev_priv(dev);
320 enum can_state state;
323 if (!(canrflg & MSCAN_CSCIF) || priv->can.state > CAN_STATE_BUS_OFF)
326 state = state_map[max(MSCAN_STATE_RX(canrflg),
327 MSCAN_STATE_TX(canrflg))];
328 if (priv->can.state < state)
330 priv->can.state = state;
334 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
335 static int mscan_rx_poll(struct napi_struct *napi, int quota)
337 static int mscan_rx_poll(struct net_device *dev, int *budget)
340 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,28)
341 struct mscan_priv *priv = container_of(napi, struct mscan_priv, napi);
342 struct net_device *dev = napi->dev;
343 #elif LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
344 struct mscan_priv *priv = container_of(napi, struct mscan_priv, napi);
345 struct net_device *dev = priv->dev;
347 struct mscan_priv *priv = netdev_priv(dev);
348 int quota = min(dev->quota, *budget);
350 struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
351 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
352 struct net_device_stats *stats = can_get_stats(dev);
354 struct net_device_stats *stats = &dev->stats;
359 struct can_frame *frame;
364 while (npackets < quota && ((canrflg = in_8(®s->canrflg)) &
365 (MSCAN_RXF | MSCAN_ERR_IF))) {
367 skb = dev_alloc_skb(sizeof(struct can_frame));
369 if (printk_ratelimit())
370 dev_notice(ND2D(dev), "packet dropped\n");
372 out_8(®s->canrflg, canrflg);
376 frame = (struct can_frame *)skb_put(skb, sizeof(*frame));
377 memset(frame, 0, sizeof(*frame));
379 if (canrflg & MSCAN_RXF) {
380 can_id = in_be16(®s->rx.idr1_0);
381 if (can_id & (1 << 3)) {
382 frame->can_id = CAN_EFF_FLAG;
383 can_id = ((can_id << 16) |
384 in_be16(®s->rx.idr3_2));
385 can_id = ((can_id & 0xffe00000) |
386 ((can_id & 0x7ffff) << 2)) >> 2;
392 frame->can_id |= can_id >> 1;
394 frame->can_id |= CAN_RTR_FLAG;
395 frame->can_dlc = in_8(®s->rx.dlr) & 0xf;
397 if (!(frame->can_id & CAN_RTR_FLAG)) {
398 void __iomem *data = ®s->rx.dsr1_0;
399 u16 *payload = (u16 *) frame->data;
400 for (i = 0; i < (frame->can_dlc + 1) / 2; i++) {
401 *payload++ = in_be16(data);
402 data += 2 + _MSCAN_RESERVED_DSR_SIZE;
406 out_8(®s->canrflg, MSCAN_RXF);
407 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,32)
408 dev->last_rx = jiffies;
411 stats->rx_bytes += frame->can_dlc;
412 } else if (canrflg & MSCAN_ERR_IF) {
413 dev_dbg(ND2D(dev), "error interrupt (canrflg=%#x)\n",
415 frame->can_id = CAN_ERR_FLAG;
417 if (canrflg & MSCAN_OVRIF) {
418 frame->can_id |= CAN_ERR_CRTL;
419 frame->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
420 stats->rx_over_errors++;
425 if (check_set_state(dev, canrflg)) {
426 switch (priv->can.state) {
427 case CAN_STATE_ERROR_WARNING:
428 frame->can_id |= CAN_ERR_CRTL;
429 priv->can.can_stats.error_warning++;
430 if ((priv->shadow_statflg &
432 (canrflg & MSCAN_RSTAT_MSK))
434 CAN_ERR_CRTL_RX_WARNING;
436 if ((priv->shadow_statflg &
438 (canrflg & MSCAN_TSTAT_MSK))
440 CAN_ERR_CRTL_TX_WARNING;
442 case CAN_STATE_ERROR_PASSIVE:
443 frame->can_id |= CAN_ERR_CRTL;
444 priv->can.can_stats.error_passive++;
446 CAN_ERR_CRTL_RX_PASSIVE;
448 case CAN_STATE_BUS_OFF:
449 frame->can_id |= CAN_ERR_BUSOFF;
456 priv->shadow_statflg = canrflg & MSCAN_STAT_MSK;
457 frame->can_dlc = CAN_ERR_DLC;
458 out_8(®s->canrflg, MSCAN_ERR_IF);
463 skb->protocol = __constant_htons(ETH_P_CAN);
464 skb->ip_summed = CHECKSUM_UNNECESSARY;
465 netif_receive_skb(skb);
468 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
470 dev->quota -= npackets;
473 if (!(in_8(®s->canrflg) & (MSCAN_RXF | MSCAN_ERR_IF))) {
474 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,28)
475 napi_complete(&priv->napi);
476 #elif LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
477 netif_rx_complete(dev, &priv->napi);
479 netif_rx_complete(dev);
481 clear_bit(F_RX_PROGRESS, &priv->flags);
482 if (priv->can.state < CAN_STATE_BUS_OFF)
483 out_8(®s->canrier, priv->shadow_canrier);
489 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
490 static irqreturn_t mscan_isr(int irq, void *dev_id, struct pt_regs *r)
492 static irqreturn_t mscan_isr(int irq, void *dev_id)
495 struct net_device *dev = (struct net_device *)dev_id;
496 struct mscan_priv *priv = netdev_priv(dev);
497 struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
498 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
499 struct net_device_stats *stats = can_get_stats(dev);
501 struct net_device_stats *stats = &dev->stats;
503 u8 cantier, cantflg, canrflg;
504 irqreturn_t ret = IRQ_NONE;
506 cantier = in_8(®s->cantier) & MSCAN_TXE;
507 cantflg = in_8(®s->cantflg) & cantier;
509 if (cantier && cantflg) {
511 struct list_head *tmp, *pos;
513 list_for_each_safe(pos, tmp, &priv->tx_head) {
514 struct tx_queue_entry *entry =
515 list_entry(pos, struct tx_queue_entry, list);
516 u8 mask = entry->mask;
518 if (!(cantflg & mask))
521 out_8(®s->cantbsel, mask);
522 stats->tx_bytes += in_8(®s->tx.dlr);
524 can_get_echo_skb(dev, entry->id);
525 priv->tx_active &= ~mask;
529 if (list_empty(&priv->tx_head)) {
530 clear_bit(F_TX_WAIT_ALL, &priv->flags);
531 clear_bit(F_TX_PROGRESS, &priv->flags);
534 dev->trans_start = jiffies;
536 if (!test_bit(F_TX_WAIT_ALL, &priv->flags))
537 netif_wake_queue(dev);
539 out_8(®s->cantier, priv->tx_active);
543 canrflg = in_8(®s->canrflg);
544 if ((canrflg & ~MSCAN_STAT_MSK) &&
545 !test_and_set_bit(F_RX_PROGRESS, &priv->flags)) {
546 if (canrflg & ~MSCAN_STAT_MSK) {
547 priv->shadow_canrier = in_8(®s->canrier);
548 out_8(®s->canrier, 0);
549 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,28)
550 napi_schedule(&priv->napi);
551 #elif LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
552 netif_rx_schedule(dev, &priv->napi);
554 netif_rx_schedule(dev);
558 clear_bit(F_RX_PROGRESS, &priv->flags);
563 static int mscan_do_set_mode(struct net_device *dev, enum can_mode mode)
566 struct mscan_priv *priv = netdev_priv(dev);
569 if (!priv->open_time)
575 netif_stop_queue(dev);
578 CAN_MODE_STOP) ? MSCAN_INIT_MODE :
582 if (priv->can.state <= CAN_STATE_BUS_OFF)
583 mscan_set_mode(dev, MSCAN_INIT_MODE);
584 ret = mscan_start(dev);
587 if (netif_queue_stopped(dev))
588 netif_wake_queue(dev);
598 static int mscan_do_set_bittiming(struct net_device *dev)
600 struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
601 struct mscan_priv *priv = netdev_priv(dev);
602 struct can_bittiming *bt = &priv->can.bittiming;
605 btr0 = BTR0_SET_BRP(bt->brp) | BTR0_SET_SJW(bt->sjw);
606 btr1 = (BTR1_SET_TSEG1(bt->prop_seg + bt->phase_seg1) |
607 BTR1_SET_TSEG2(bt->phase_seg2) |
608 BTR1_SET_SAM(priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES));
610 dev_info(ND2D(dev), "setting BTR0=0x%02x BTR1=0x%02x\n", btr0, btr1);
612 out_8(®s->canbtr0, btr0);
613 out_8(®s->canbtr1, btr1);
618 static int mscan_open(struct net_device *dev)
621 struct mscan_priv *priv = netdev_priv(dev);
622 struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
625 ret = open_candev(dev);
629 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
630 napi_enable(&priv->napi);
633 ret = request_irq(dev->irq, mscan_isr, 0, dev->name, dev);
635 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
636 napi_disable(&priv->napi);
638 printk(KERN_ERR "%s - failed to attach interrupt\n",
643 priv->open_time = jiffies;
645 out_8(®s->canctl1, in_8(®s->canctl1) & ~MSCAN_LISTEN);
647 ret = mscan_start(dev);
651 netif_start_queue(dev);
656 static int mscan_close(struct net_device *dev)
658 struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
659 struct mscan_priv *priv = netdev_priv(dev);
661 netif_stop_queue(dev);
662 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
663 napi_disable(&priv->napi);
666 out_8(®s->cantier, 0);
667 out_8(®s->canrier, 0);
668 mscan_set_mode(dev, MSCAN_INIT_MODE);
670 free_irq(dev->irq, dev);
676 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,28)
677 static const struct net_device_ops mscan_netdev_ops = {
678 .ndo_open = mscan_open,
679 .ndo_stop = mscan_close,
680 .ndo_start_xmit = mscan_start_xmit,
684 int register_mscandev(struct net_device *dev, int clock_src)
686 struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
689 ctl1 = in_8(®s->canctl1);
691 ctl1 |= MSCAN_CLKSRC;
693 ctl1 &= ~MSCAN_CLKSRC;
696 out_8(®s->canctl1, ctl1);
699 /* acceptance mask/acceptance code (accept everything) */
700 out_be16(®s->canidar1_0, 0);
701 out_be16(®s->canidar3_2, 0);
702 out_be16(®s->canidar5_4, 0);
703 out_be16(®s->canidar7_6, 0);
705 out_be16(®s->canidmr1_0, 0xffff);
706 out_be16(®s->canidmr3_2, 0xffff);
707 out_be16(®s->canidmr5_4, 0xffff);
708 out_be16(®s->canidmr7_6, 0xffff);
709 /* Two 32 bit Acceptance Filters */
710 out_8(®s->canidac, MSCAN_AF_32BIT);
712 mscan_set_mode(dev, MSCAN_INIT_MODE);
714 return register_candev(dev);
716 EXPORT_SYMBOL_GPL(register_mscandev);
718 void unregister_mscandev(struct net_device *dev)
720 struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
721 mscan_set_mode(dev, MSCAN_INIT_MODE);
722 out_8(®s->canctl1, in_8(®s->canctl1) & ~MSCAN_CANE);
723 unregister_candev(dev);
725 EXPORT_SYMBOL_GPL(unregister_mscandev);
727 struct net_device *alloc_mscandev(void)
729 struct net_device *dev;
730 struct mscan_priv *priv;
733 dev = alloc_candev(sizeof(struct mscan_priv), MSCAN_ECHO_SKB_MAX);
736 priv = netdev_priv(dev);
738 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,28)
739 dev->netdev_ops = &mscan_netdev_ops;
741 dev->open = mscan_open;
742 dev->stop = mscan_close;
743 dev->hard_start_xmit = mscan_start_xmit;
746 dev->flags |= IFF_ECHO; /* we support local echo */
748 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,28)
749 netif_napi_add(dev, &priv->napi, mscan_rx_poll, 8);
750 #elif LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
752 netif_napi_add(dev, &priv->napi, mscan_rx_poll, 8);
754 dev->poll = mscan_rx_poll;
758 priv->can.bittiming_const = &mscan_bittiming_const;
759 priv->can.do_set_bittiming = mscan_do_set_bittiming;
760 priv->can.do_set_mode = mscan_do_set_mode;
762 for (i = 0; i < TX_QUEUE_SIZE; i++) {
763 priv->tx_queue[i].id = i;
764 priv->tx_queue[i].mask = 1 << i;
769 EXPORT_SYMBOL_GPL(alloc_mscandev);
771 MODULE_AUTHOR("Andrey Volkov <avolkov@varma-el.com>");
772 MODULE_LICENSE("GPL v2");
773 MODULE_DESCRIPTION("CAN port driver for a MSCAN based chips");