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
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 : 0)
68 static struct can_bittiming_const mscan_bittiming_const = {
86 #define TX_QUEUE_SIZE 3
88 struct tx_queue_entry {
89 struct list_head list;
103 struct list_head tx_head;
104 struct tx_queue_entry tx_queue[TX_QUEUE_SIZE];
105 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,28)
106 struct napi_struct napi;
107 #elif LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
108 struct napi_struct napi;
109 struct net_device *dev;
113 #define F_RX_PROGRESS 0
114 #define F_TX_PROGRESS 1
115 #define F_TX_WAIT_ALL 2
117 static enum can_state state_map[] = {
118 CAN_STATE_ERROR_ACTIVE,
119 CAN_STATE_ERROR_WARNING,
120 CAN_STATE_ERROR_PASSIVE,
124 static int mscan_set_mode(struct net_device *dev, u8 mode)
126 struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
127 struct mscan_priv *priv = netdev_priv(dev);
132 if (mode != MSCAN_NORMAL_MODE) {
134 if (priv->tx_active) {
135 /* Abort transfers before going to sleep */#
136 out_8(®s->cantarq, priv->tx_active);
137 /* Suppress TX done interrupts */
138 out_8(®s->cantier, 0);
141 canctl1 = in_8(®s->canctl1);
142 if ((mode & MSCAN_SLPRQ) && (canctl1 & MSCAN_SLPAK) == 0) {
143 out_8(®s->canctl0,
144 in_8(®s->canctl0) | MSCAN_SLPRQ);
145 for (i = 0; i < MSCAN_SET_MODE_RETRIES; i++) {
146 if (in_8(®s->canctl1) & MSCAN_SLPAK)
150 if (i >= MSCAN_SET_MODE_RETRIES)
154 priv->can.state = CAN_STATE_SLEEPING;
156 if (!ret && (mode & MSCAN_INITRQ)
157 && (canctl1 & MSCAN_INITAK) == 0) {
158 out_8(®s->canctl0,
159 in_8(®s->canctl0) | MSCAN_INITRQ);
160 for (i = 0; i < MSCAN_SET_MODE_RETRIES; i++) {
161 if (in_8(®s->canctl1) & MSCAN_INITAK)
164 if (i >= MSCAN_SET_MODE_RETRIES)
168 priv->can.state = CAN_STATE_STOPPED;
170 if (!ret && (mode & MSCAN_CSWAI))
171 out_8(®s->canctl0,
172 in_8(®s->canctl0) | MSCAN_CSWAI);
175 canctl1 = in_8(®s->canctl1);
176 if (canctl1 & (MSCAN_SLPAK | MSCAN_INITAK)) {
177 out_8(®s->canctl0, in_8(®s->canctl0) &
178 ~(MSCAN_SLPRQ | MSCAN_INITRQ));
179 for (i = 0; i < MSCAN_SET_MODE_RETRIES; i++) {
180 canctl1 = in_8(®s->canctl1);
181 if (!(canctl1 & (MSCAN_INITAK | MSCAN_SLPAK)))
184 if (i >= MSCAN_SET_MODE_RETRIES)
187 priv->can.state = CAN_STATE_ERROR_ACTIVE;
193 static int mscan_start(struct net_device *dev)
195 struct mscan_priv *priv = netdev_priv(dev);
196 struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
200 out_8(®s->canrier, 0);
202 INIT_LIST_HEAD(&priv->tx_head);
205 priv->shadow_canrier = 0;
208 err = mscan_set_mode(dev, MSCAN_NORMAL_MODE);
212 canrflg = in_8(®s->canrflg);
213 priv->shadow_statflg = canrflg & MSCAN_STAT_MSK;
214 priv->can.state = state_map[max(MSCAN_STATE_RX(canrflg),
215 MSCAN_STATE_TX(canrflg))];
216 out_8(®s->cantier, 0);
218 /* Enable receive interrupts. */
219 out_8(®s->canrier, MSCAN_OVRIE | MSCAN_RXFIE | MSCAN_CSCIE |
220 MSCAN_RSTATE1 | MSCAN_RSTATE0 | MSCAN_TSTATE1 | MSCAN_TSTATE0);
225 static int mscan_start_xmit(struct sk_buff *skb, struct net_device *dev)
227 struct can_frame *frame = (struct can_frame *)skb->data;
228 struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
229 struct mscan_priv *priv = netdev_priv(dev);
233 if (frame->can_dlc > 8)
236 out_8(®s->cantier, 0);
238 i = ~priv->tx_active & MSCAN_TXE;
240 switch (hweight8(i)) {
242 netif_stop_queue(dev);
243 dev_err(ND2D(dev), "BUG! Tx Ring full when queue awake!\n");
244 return NETDEV_TX_BUSY;
246 /* if buf_id < 3, then current frame will be send out of order,
247 since buffer with lower id have higher priority (hell..) */
250 if (priv->cur_pri == 0xff)
251 set_bit(F_TX_WAIT_ALL, &priv->flags);
252 netif_stop_queue(dev);
254 set_bit(F_TX_PROGRESS, &priv->flags);
256 out_8(®s->cantbsel, i);
258 rtr = frame->can_id & CAN_RTR_FLAG;
260 if (frame->can_id & CAN_EFF_FLAG) {
261 can_id = (frame->can_id & CAN_EFF_MASK) << 1;
264 out_be16(®s->tx.idr3_2, can_id);
267 can_id = (can_id & 0x7) | ((can_id << 2) & 0xffe0) | (3 << 3);
269 can_id = (frame->can_id & CAN_SFF_MASK) << 5;
273 out_be16(®s->tx.idr1_0, can_id);
276 void __iomem *data = ®s->tx.dsr1_0;
277 u16 *payload = (u16 *) frame->data;
278 /*Its safe to write into dsr[dlc+1] */
279 for (i = 0; i < (frame->can_dlc + 1) / 2; i++) {
280 out_be16(data, *payload++);
281 data += 2 + _MSCAN_RESERVED_DSR_SIZE;
285 out_8(®s->tx.dlr, frame->can_dlc);
286 out_8(®s->tx.tbpr, priv->cur_pri);
288 /* Start transmission. */
289 out_8(®s->cantflg, 1 << buf_id);
291 if (!test_bit(F_TX_PROGRESS, &priv->flags))
292 dev->trans_start = jiffies;
294 list_add_tail(&priv->tx_queue[buf_id].list, &priv->tx_head);
296 can_put_echo_skb(skb, dev, buf_id);
298 /* Enable interrupt. */
299 priv->tx_active |= 1 << buf_id;
300 out_8(®s->cantier, priv->tx_active);
305 static inline int check_set_state(struct net_device *dev, u8 canrflg)
307 struct mscan_priv *priv = netdev_priv(dev);
308 enum can_state state;
311 if (!(canrflg & MSCAN_CSCIF) || priv->can.state > CAN_STATE_BUS_OFF)
314 state = state_map[max(MSCAN_STATE_RX(canrflg),
315 MSCAN_STATE_TX(canrflg))];
316 if (priv->can.state < state)
318 priv->can.state = state;
322 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
323 static int mscan_rx_poll(struct napi_struct *napi, int quota)
325 static int mscan_rx_poll(struct net_device *dev, int *budget)
328 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,28)
329 struct mscan_priv *priv = container_of(napi, struct mscan_priv, napi);
330 struct net_device *dev = napi->dev;
331 #elif LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
332 struct mscan_priv *priv = container_of(napi, struct mscan_priv, napi);
333 struct net_device *dev = priv->dev;
335 struct mscan_priv *priv = netdev_priv(dev);
336 int quota = min(dev->quota, *budget);
338 struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
339 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
340 struct net_device_stats *stats = can_get_stats(dev);
342 struct net_device_stats *stats = &dev->stats;
347 struct can_frame *frame;
352 while (npackets < quota && ((canrflg = in_8(®s->canrflg)) &
353 (MSCAN_RXF | MSCAN_ERR_IF))) {
355 skb = dev_alloc_skb(sizeof(struct can_frame));
357 if (printk_ratelimit())
358 dev_notice(ND2D(dev), "packet dropped\n");
360 out_8(®s->canrflg, canrflg);
364 frame = (struct can_frame *)skb_put(skb, sizeof(*frame));
365 memset(frame, 0, sizeof(*frame));
367 if (canrflg & MSCAN_RXF) {
368 can_id = in_be16(®s->rx.idr1_0);
369 if (can_id & (1 << 3)) {
370 frame->can_id = CAN_EFF_FLAG;
371 can_id = ((can_id << 16) |
372 in_be16(®s->rx.idr3_2));
373 can_id = ((can_id & 0xffe00000) |
374 ((can_id & 0x7ffff) << 2)) >> 2;
380 frame->can_id |= can_id >> 1;
382 frame->can_id |= CAN_RTR_FLAG;
383 frame->can_dlc = in_8(®s->rx.dlr) & 0xf;
385 if (!(frame->can_id & CAN_RTR_FLAG)) {
386 void __iomem *data = ®s->rx.dsr1_0;
387 u16 *payload = (u16 *) frame->data;
388 for (i = 0; i < (frame->can_dlc + 1) / 2; i++) {
389 *payload++ = in_be16(data);
390 data += 2 + _MSCAN_RESERVED_DSR_SIZE;
394 out_8(®s->canrflg, MSCAN_RXF);
395 dev->last_rx = jiffies;
397 stats->rx_bytes += frame->can_dlc;
398 } else if (canrflg & MSCAN_ERR_IF) {
399 dev_dbg(ND2D(dev), "error interrupt (canrflg=%#x)\n",
401 frame->can_id = CAN_ERR_FLAG;
403 if (canrflg & MSCAN_OVRIF) {
404 frame->can_id |= CAN_ERR_CRTL;
405 frame->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
406 stats->rx_over_errors++;
411 if (check_set_state(dev, canrflg)) {
412 switch (priv->can.state) {
413 case CAN_STATE_ERROR_WARNING:
414 frame->can_id |= CAN_ERR_CRTL;
415 priv->can.can_stats.error_warning++;
416 if ((priv->shadow_statflg &
418 (canrflg & MSCAN_RSTAT_MSK))
420 CAN_ERR_CRTL_RX_WARNING;
422 if ((priv->shadow_statflg &
424 (canrflg & MSCAN_TSTAT_MSK))
426 CAN_ERR_CRTL_TX_WARNING;
428 case CAN_STATE_ERROR_PASSIVE:
429 frame->can_id |= CAN_ERR_CRTL;
430 priv->can.can_stats.error_passive++;
432 CAN_ERR_CRTL_RX_PASSIVE;
434 case CAN_STATE_BUS_OFF:
435 frame->can_id |= CAN_ERR_BUSOFF;
442 priv->shadow_statflg = canrflg & MSCAN_STAT_MSK;
443 frame->can_dlc = CAN_ERR_DLC;
444 out_8(®s->canrflg, MSCAN_ERR_IF);
449 skb->protocol = __constant_htons(ETH_P_CAN);
450 skb->ip_summed = CHECKSUM_UNNECESSARY;
451 netif_receive_skb(skb);
454 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
456 dev->quota -= npackets;
459 if (!(in_8(®s->canrflg) & (MSCAN_RXF | MSCAN_ERR_IF))) {
460 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,28)
461 napi_complete(&priv->napi);
462 #elif LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
463 netif_rx_complete(dev, &priv->napi);
465 netif_rx_complete(dev);
467 clear_bit(F_RX_PROGRESS, &priv->flags);
468 if (priv->can.state < CAN_STATE_BUS_OFF)
469 out_8(®s->canrier, priv->shadow_canrier);
475 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
476 static irqreturn_t mscan_isr(int irq, void *dev_id, struct pt_regs *r)
478 static irqreturn_t mscan_isr(int irq, void *dev_id)
481 struct net_device *dev = (struct net_device *)dev_id;
482 struct mscan_priv *priv = netdev_priv(dev);
483 struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
484 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
485 struct net_device_stats *stats = can_get_stats(dev);
487 struct net_device_stats *stats = &dev->stats;
489 u8 cantier, cantflg, canrflg;
490 irqreturn_t ret = IRQ_NONE;
492 cantier = in_8(®s->cantier) & MSCAN_TXE;
493 cantflg = in_8(®s->cantflg) & cantier;
495 if (cantier && cantflg) {
497 struct list_head *tmp, *pos;
499 list_for_each_safe(pos, tmp, &priv->tx_head) {
500 struct tx_queue_entry *entry =
501 list_entry(pos, struct tx_queue_entry, list);
502 u8 mask = entry->mask;
504 if (!(cantflg & mask))
507 out_8(®s->cantbsel, mask);
508 stats->tx_bytes += in_8(®s->tx.dlr);
510 can_get_echo_skb(dev, entry->id);
511 priv->tx_active &= ~mask;
515 if (list_empty(&priv->tx_head)) {
516 clear_bit(F_TX_WAIT_ALL, &priv->flags);
517 clear_bit(F_TX_PROGRESS, &priv->flags);
520 dev->trans_start = jiffies;
522 if (!test_bit(F_TX_WAIT_ALL, &priv->flags))
523 netif_wake_queue(dev);
525 out_8(®s->cantier, priv->tx_active);
529 canrflg = in_8(®s->canrflg);
530 if ((canrflg & ~MSCAN_STAT_MSK) &&
531 !test_and_set_bit(F_RX_PROGRESS, &priv->flags)) {
532 if (canrflg & ~MSCAN_STAT_MSK) {
533 priv->shadow_canrier = in_8(®s->canrier);
534 out_8(®s->canrier, 0);
535 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,28)
536 napi_schedule(&priv->napi);
537 #elif LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
538 netif_rx_schedule(dev, &priv->napi);
540 netif_rx_schedule(dev);
544 clear_bit(F_RX_PROGRESS, &priv->flags);
549 static int mscan_do_set_mode(struct net_device *dev, enum can_mode mode)
552 struct mscan_priv *priv = netdev_priv(dev);
555 if (!priv->open_time)
561 netif_stop_queue(dev);
564 CAN_MODE_STOP) ? MSCAN_INIT_MODE :
568 if (priv->can.state <= CAN_STATE_BUS_OFF)
569 mscan_set_mode(dev, MSCAN_INIT_MODE);
570 ret = mscan_start(dev);
573 if (netif_queue_stopped(dev))
574 netif_wake_queue(dev);
584 static int mscan_do_set_bittiming(struct net_device *dev)
586 struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
587 struct mscan_priv *priv = netdev_priv(dev);
588 struct can_bittiming *bt = &priv->can.bittiming;
591 btr0 = BTR0_SET_BRP(bt->brp) | BTR0_SET_SJW(bt->sjw);
592 btr1 = (BTR1_SET_TSEG1(bt->prop_seg + bt->phase_seg1) |
593 BTR1_SET_TSEG2(bt->phase_seg2) |
594 BTR1_SET_SAM(priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES));
596 dev_info(ND2D(dev), "setting BTR0=0x%02x BTR1=0x%02x\n", btr0, btr1);
598 out_8(®s->canbtr0, btr0);
599 out_8(®s->canbtr1, btr1);
604 static int mscan_open(struct net_device *dev)
607 struct mscan_priv *priv = netdev_priv(dev);
608 struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
611 ret = open_candev(dev);
615 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
616 napi_enable(&priv->napi);
619 ret = request_irq(dev->irq, mscan_isr, 0, dev->name, dev);
621 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
622 napi_disable(&priv->napi);
624 printk(KERN_ERR "%s - failed to attach interrupt\n",
629 priv->open_time = jiffies;
631 out_8(®s->canctl1, in_8(®s->canctl1) & ~MSCAN_LISTEN);
633 ret = mscan_start(dev);
637 netif_start_queue(dev);
642 static int mscan_close(struct net_device *dev)
644 struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
645 struct mscan_priv *priv = netdev_priv(dev);
647 netif_stop_queue(dev);
648 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
649 napi_disable(&priv->napi);
652 out_8(®s->cantier, 0);
653 out_8(®s->canrier, 0);
654 mscan_set_mode(dev, MSCAN_INIT_MODE);
656 free_irq(dev->irq, dev);
662 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,28)
663 static const struct net_device_ops mscan_netdev_ops = {
664 .ndo_open = mscan_open,
665 .ndo_stop = mscan_close,
666 .ndo_start_xmit = mscan_start_xmit,
670 int register_mscandev(struct net_device *dev, int clock_src)
672 struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
675 ctl1 = in_8(®s->canctl1);
677 ctl1 |= MSCAN_CLKSRC;
679 ctl1 &= ~MSCAN_CLKSRC;
682 out_8(®s->canctl1, ctl1);
685 /* acceptance mask/acceptance code (accept everything) */
686 out_be16(®s->canidar1_0, 0);
687 out_be16(®s->canidar3_2, 0);
688 out_be16(®s->canidar5_4, 0);
689 out_be16(®s->canidar7_6, 0);
691 out_be16(®s->canidmr1_0, 0xffff);
692 out_be16(®s->canidmr3_2, 0xffff);
693 out_be16(®s->canidmr5_4, 0xffff);
694 out_be16(®s->canidmr7_6, 0xffff);
695 /* Two 32 bit Acceptance Filters */
696 out_8(®s->canidac, MSCAN_AF_32BIT);
698 mscan_set_mode(dev, MSCAN_INIT_MODE);
700 return register_candev(dev);
702 EXPORT_SYMBOL_GPL(register_mscandev);
704 void unregister_mscandev(struct net_device *dev)
706 struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
707 mscan_set_mode(dev, MSCAN_INIT_MODE);
708 out_8(®s->canctl1, in_8(®s->canctl1) & ~MSCAN_CANE);
709 unregister_candev(dev);
711 EXPORT_SYMBOL_GPL(unregister_mscandev);
713 struct net_device *alloc_mscandev(void)
715 struct net_device *dev;
716 struct mscan_priv *priv;
719 dev = alloc_candev(sizeof(struct mscan_priv));
722 priv = netdev_priv(dev);
724 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,28)
725 dev->netdev_ops = &mscan_netdev_ops;
727 dev->open = mscan_open;
728 dev->stop = mscan_close;
729 dev->hard_start_xmit = mscan_start_xmit;
732 dev->flags |= IFF_ECHO; /* we support local echo */
734 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,28)
735 netif_napi_add(dev, &priv->napi, mscan_rx_poll, 8);
736 #elif LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
738 netif_napi_add(dev, &priv->napi, mscan_rx_poll, 8);
740 dev->poll = mscan_rx_poll;
744 priv->can.bittiming_const = &mscan_bittiming_const;
745 priv->can.do_set_bittiming = mscan_do_set_bittiming;
746 priv->can.do_set_mode = mscan_do_set_mode;
748 for (i = 0; i < TX_QUEUE_SIZE; i++) {
749 priv->tx_queue[i].id = i;
750 priv->tx_queue[i].mask = 1 << i;
755 EXPORT_SYMBOL_GPL(alloc_mscandev);
757 MODULE_AUTHOR("Andrey Volkov <avolkov@varma-el.com>");
758 MODULE_LICENSE("GPL v2");
759 MODULE_DESCRIPTION("CAN port driver for a MSCAN based chips");