5 * CAN bus driver for the alone generic (as possible as) MSCAN controller.
8 * Andrey Volkov <avolkov@varma-el.com>
11 * 2005-2006, Varma Electronics Oy
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/interrupt.h>
33 #include <linux/delay.h>
34 #include <linux/netdevice.h>
35 #include <linux/if_arp.h>
36 #include <linux/if_ether.h>
37 #include <linux/list.h>
38 #include <linux/can.h>
39 #include <linux/can/dev.h>
40 #include <linux/can/error.h>
41 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
49 #include <linux/can/version.h> /* for RCSID. Removed by mkpatch script */
52 #define MSCAN_NORMAL_MODE 0
53 #define MSCAN_SLEEP_MODE MSCAN_SLPRQ
54 #define MSCAN_INIT_MODE (MSCAN_INITRQ | MSCAN_SLPRQ)
55 #define MSCAN_POWEROFF_MODE (MSCAN_CSWAI | MSCAN_SLPRQ)
56 #define MSCAN_SET_MODE_RETRIES 255
59 #define BTR0_BRP_MASK 0x3f
60 #define BTR0_SJW_SHIFT 6
61 #define BTR0_SJW_MASK (0x3 << BTR0_SJW_SHIFT)
63 #define BTR1_TSEG1_MASK 0xf
64 #define BTR1_TSEG2_SHIFT 4
65 #define BTR1_TSEG2_MASK (0x7 << BTR1_TSEG2_SHIFT)
66 #define BTR1_SAM_SHIFT 7
68 #define BTR0_SET_BRP(brp) (((brp) - 1) & BTR0_BRP_MASK)
69 #define BTR0_SET_SJW(sjw) ((((sjw) - 1) << BTR0_SJW_SHIFT) & \
72 #define BTR1_SET_TSEG1(tseg1) (((tseg1) - 1) & BTR1_TSEG1_MASK)
73 #define BTR1_SET_TSEG2(tseg2) ((((tseg2) - 1) << BTR1_TSEG2_SHIFT) & \
75 #define BTR1_SET_SAM(sam) (((sam) & 1) << BTR1_SAM_SHIFT)
83 #define TX_QUEUE_SIZE 3
85 struct tx_queue_entry {
86 struct list_head list;
94 volatile unsigned long flags;
100 struct list_head tx_head;
101 struct tx_queue_entry tx_queue[TX_QUEUE_SIZE];
102 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
103 struct napi_struct napi;
104 struct net_device *dev;
108 #define F_RX_PROGRESS 0
109 #define F_TX_PROGRESS 1
110 #define F_TX_WAIT_ALL 2
112 static enum can_state state_map[] = {
114 CAN_STATE_BUS_WARNING,
115 CAN_STATE_BUS_PASSIVE,
119 static int mscan_set_mode(struct net_device *dev, u8 mode)
121 struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
122 struct mscan_priv *priv = netdev_priv(dev);
127 if (mode != MSCAN_NORMAL_MODE) {
129 if (priv->tx_active) {
130 /* Abort transfers before going to sleep */
131 out_8(®s->cantier, 0);
132 out_8(®s->cantarq, priv->tx_active);
133 out_8(®s->cantier, priv->tx_active);
136 canctl1 = in_8(®s->canctl1);
137 if ((mode & MSCAN_SLPRQ) && (canctl1 & MSCAN_SLPAK) == 0) {
138 out_8(®s->canctl0,
139 in_8(®s->canctl0) | MSCAN_SLPRQ);
140 for (i = 0; i < MSCAN_SET_MODE_RETRIES; i++) {
141 if (in_8(®s->canctl1) & MSCAN_SLPAK)
145 if (i >= MSCAN_SET_MODE_RETRIES)
149 priv->can.state = CAN_STATE_SLEEPING;
151 if (!ret && (mode & MSCAN_INITRQ)
152 && (canctl1 & MSCAN_INITAK) == 0) {
153 out_8(®s->canctl0,
154 in_8(®s->canctl0) | MSCAN_INITRQ);
155 for (i = 0; i < MSCAN_SET_MODE_RETRIES; i++) {
156 if (in_8(®s->canctl1) & MSCAN_INITAK)
159 if (i >= MSCAN_SET_MODE_RETRIES)
163 priv->can.state = CAN_STATE_STOPPED;
165 if (!ret && (mode & MSCAN_CSWAI))
166 out_8(®s->canctl0,
167 in_8(®s->canctl0) | MSCAN_CSWAI);
170 canctl1 = in_8(®s->canctl1);
171 if (canctl1 & (MSCAN_SLPAK | MSCAN_INITAK)) {
172 out_8(®s->canctl0, in_8(®s->canctl0) &
173 ~(MSCAN_SLPRQ | MSCAN_INITRQ));
174 for (i = 0; i < MSCAN_SET_MODE_RETRIES; i++) {
175 canctl1 = in_8(®s->canctl1);
176 if (!(canctl1 & (MSCAN_INITAK | MSCAN_SLPAK)))
179 if (i >= MSCAN_SET_MODE_RETRIES)
182 priv->can.state = CAN_STATE_ACTIVE;
188 static int mscan_start(struct net_device *dev)
190 struct mscan_priv *priv = netdev_priv(dev);
191 struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
195 out_8(®s->canrier, 0);
197 INIT_LIST_HEAD(&priv->tx_head);
200 priv->shadow_canrier = 0;
203 err = mscan_set_mode(dev, MSCAN_NORMAL_MODE);
207 canrflg = in_8(®s->canrflg);
208 priv->shadow_statflg = canrflg & MSCAN_STAT_MSK;
209 priv->can.state = state_map[max(MSCAN_STATE_RX(canrflg),
210 MSCAN_STATE_TX(canrflg))];
211 out_8(®s->cantier, 0);
213 /* Enable receive interrupts. */
214 out_8(®s->canrier, MSCAN_OVRIE | MSCAN_RXFIE | MSCAN_CSCIE |
215 MSCAN_RSTATE1 | MSCAN_RSTATE0 | MSCAN_TSTATE1 | MSCAN_TSTATE0);
220 static int mscan_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
222 struct can_frame *frame = (struct can_frame *)skb->data;
223 struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
224 struct mscan_priv *priv = netdev_priv(dev);
228 if (frame->can_dlc > 8)
231 dev_dbg(ND2D(dev), "%s\n", __func__);
232 out_8(®s->cantier, 0);
234 i = ~priv->tx_active & MSCAN_TXE;
236 switch (hweight8(i)) {
238 netif_stop_queue(dev);
239 dev_err(ND2D(dev), "BUG! Tx Ring full when queue awake!\n");
240 return NETDEV_TX_BUSY;
242 /* if buf_id < 3, then current frame will be send out of order,
243 since buffer with lower id have higher priority (hell..) */
246 if (priv->cur_pri == 0xff)
247 set_bit(F_TX_WAIT_ALL, &priv->flags);
248 netif_stop_queue(dev);
250 set_bit(F_TX_PROGRESS, &priv->flags);
252 out_8(®s->cantbsel, i);
254 rtr = frame->can_id & CAN_RTR_FLAG;
256 if (frame->can_id & CAN_EFF_FLAG) {
257 dev_dbg(ND2D(dev), "sending extended frame\n");
259 can_id = (frame->can_id & CAN_EFF_MASK) << 1;
262 out_be16(®s->tx.idr3_2, can_id);
265 can_id = (can_id & 0x7) | ((can_id << 2) & 0xffe0) | (3 << 3);
267 dev_dbg(ND2D(dev), "sending standard frame\n");
268 can_id = (frame->can_id & CAN_SFF_MASK) << 5;
272 out_be16(®s->tx.idr1_0, can_id);
275 volatile 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,23)
330 struct mscan_priv *priv = container_of(napi, struct mscan_priv, napi);
331 struct net_device *dev = priv->dev;
333 struct mscan_priv *priv = netdev_priv(dev);
334 int quota = min(dev->quota, *budget);
336 struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
337 struct net_device_stats *stats = dev->get_stats(dev);
341 struct can_frame *frame;
346 while (npackets < quota && ((canrflg = in_8(®s->canrflg)) &
347 (MSCAN_RXF | MSCAN_ERR_IF))) {
349 skb = dev_alloc_skb(sizeof(struct can_frame));
351 if (printk_ratelimit())
352 dev_notice(ND2D(dev), "packet dropped\n");
354 out_8(®s->canrflg, canrflg);
358 frame = (struct can_frame *)skb_put(skb, sizeof(*frame));
359 memset(frame, 0, sizeof(*frame));
361 if (canrflg & MSCAN_RXF) {
362 can_id = in_be16(®s->rx.idr1_0);
363 if (can_id & (1 << 3)) {
364 frame->can_id = CAN_EFF_FLAG;
365 can_id = ((can_id << 16) |
366 in_be16(®s->rx.idr3_2));
367 can_id = ((can_id & 0xffe00000) |
368 ((can_id & 0x7ffff) << 2)) >> 2;
374 frame->can_id |= can_id >> 1;
376 frame->can_id |= CAN_RTR_FLAG;
377 frame->can_dlc = in_8(®s->rx.dlr) & 0xf;
379 if (!(frame->can_id & CAN_RTR_FLAG)) {
380 volatile void __iomem *data = ®s->rx.dsr1_0;
381 u16 *payload = (u16 *) frame->data;
382 for (i = 0; i < (frame->can_dlc + 1) / 2; i++) {
383 *payload++ = in_be16(data);
384 data += 2 + _MSCAN_RESERVED_DSR_SIZE;
389 "received pkt: id: %u dlc: %u data: ",
390 frame->can_id, frame->can_dlc);
393 i < frame->can_dlc && !(frame->can_id &
395 printk(KERN_DEBUG "%2x ", frame->data[i]);
396 printk(KERN_DEBUG "\n");
399 out_8(®s->canrflg, MSCAN_RXF);
400 dev->last_rx = jiffies;
402 stats->rx_bytes += frame->can_dlc;
403 } else if (canrflg & MSCAN_ERR_IF) {
404 frame->can_id = CAN_ERR_FLAG;
406 if (canrflg & MSCAN_OVRIF) {
407 frame->can_id |= CAN_ERR_CRTL;
408 frame->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
409 stats->rx_over_errors++;
413 if (check_set_state(dev, canrflg)) {
414 frame->can_id |= CAN_ERR_CRTL;
415 switch (priv->can.state) {
416 case CAN_STATE_BUS_WARNING:
417 if ((priv->shadow_statflg &
419 (canrflg & MSCAN_RSTAT_MSK))
421 CAN_ERR_CRTL_RX_WARNING;
423 if ((priv->shadow_statflg &
425 (canrflg & MSCAN_TSTAT_MSK))
427 CAN_ERR_CRTL_TX_WARNING;
429 case CAN_STATE_BUS_PASSIVE:
431 CAN_ERR_CRTL_RX_PASSIVE;
433 case CAN_STATE_BUS_OFF:
434 frame->can_id |= CAN_ERR_BUSOFF;
435 frame->can_id &= ~CAN_ERR_CRTL;
441 priv->shadow_statflg = canrflg & MSCAN_STAT_MSK;
442 frame->can_dlc = CAN_ERR_DLC;
443 out_8(®s->canrflg, MSCAN_ERR_IF);
448 skb->protocol = __constant_htons(ETH_P_CAN);
449 skb->ip_summed = CHECKSUM_UNNECESSARY;
450 netif_receive_skb(skb);
453 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
455 dev->quota -= npackets;
458 if (!(in_8(®s->canrflg) & (MSCAN_RXF | MSCAN_ERR_IF))) {
459 #if 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);
466 out_8(®s->canrier,
467 in_8(®s->canrier) | MSCAN_ERR_IF | MSCAN_RXFIE);
469 if (priv->can.state < CAN_STATE_BUS_OFF)
470 out_8(®s->canrier, priv->shadow_canrier);
478 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
479 static irqreturn_t mscan_isr(int irq, void *dev_id, struct pt_regs *r)
481 static irqreturn_t mscan_isr(int irq, void *dev_id)
484 struct net_device *dev = (struct net_device *)dev_id;
485 struct mscan_priv *priv = netdev_priv(dev);
486 struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
487 struct net_device_stats *stats = dev->get_stats(dev);
488 u8 cantier, cantflg, canrflg;
489 irqreturn_t ret = IRQ_NONE;
491 cantier = in_8(®s->cantier) & MSCAN_TXE;
492 cantflg = in_8(®s->cantflg) & cantier;
494 if (cantier && cantflg) {
496 struct list_head *tmp, *pos;
498 list_for_each_safe(pos, tmp, &priv->tx_head) {
499 struct tx_queue_entry *entry =
500 list_entry(pos, struct tx_queue_entry, list);
501 u8 mask = entry->mask;
503 if (!(cantflg & mask))
506 out_8(®s->cantbsel, mask);
507 stats->tx_bytes += in_8(®s->tx.dlr);
509 can_get_echo_skb(dev, entry->id);
510 priv->tx_active &= ~mask;
514 if (list_empty(&priv->tx_head)) {
515 clear_bit(F_TX_WAIT_ALL, &priv->flags);
516 clear_bit(F_TX_PROGRESS, &priv->flags);
519 dev->trans_start = jiffies;
521 if (!test_bit(F_TX_WAIT_ALL, &priv->flags))
522 netif_wake_queue(dev);
524 out_8(®s->cantier, priv->tx_active);
528 if ((((canrflg = in_8(®s->canrflg)) & ~MSCAN_STAT_MSK)) &&
529 !test_and_set_bit(F_RX_PROGRESS, &priv->flags)) {
531 printk(KERN_DEBUG "%s: canrflg=%#02x canrier=%#02x\n",
532 dev->name, canrflg, in_8(®s->canrier));
535 if (check_set_state(dev, canrflg)) {
536 out_8(®s->canrflg, MSCAN_CSCIF);
540 if (canrflg & ~MSCAN_STAT_MSK) {
541 priv->shadow_canrier = in_8(®s->canrier);
542 out_8(®s->canrier, 0);
543 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
544 netif_rx_schedule(dev, &priv->napi);
546 netif_rx_schedule(dev);
550 clear_bit(F_RX_PROGRESS, &priv->flags);
555 static int mscan_do_set_mode(struct net_device *dev, enum can_mode mode)
558 struct mscan_priv *priv = netdev_priv(dev);
561 if (!priv->open_time)
567 netif_stop_queue(dev);
570 CAN_MODE_STOP) ? MSCAN_INIT_MODE :
574 if (priv->can.state <= CAN_STATE_BUS_OFF)
575 mscan_set_mode(dev, MSCAN_INIT_MODE);
576 ret = mscan_start(dev);
579 if (netif_queue_stopped(dev))
580 netif_wake_queue(dev);
590 static int mscan_do_set_bittime(struct net_device *dev, struct can_bittime *bt)
592 struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
596 case CAN_BITTIME_BTR:
601 case CAN_BITTIME_STD:
602 btr0 = BTR0_SET_BRP(bt->std.brp) | BTR0_SET_SJW(bt->std.sjw);
603 btr1 = (BTR1_SET_TSEG1(bt->std.prop_seg + bt->std.phase_seg1) |
604 BTR1_SET_TSEG2(bt->std.phase_seg2) |
605 BTR1_SET_SAM(bt->std.sam));
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;
624 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
625 napi_enable(&priv->napi);
627 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18)
628 ret = request_irq(dev->irq, mscan_isr, SA_SHIRQ, dev->name, dev);
630 ret = request_irq(dev->irq, mscan_isr, IRQF_SHARED, dev->name, dev);
634 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
635 napi_disable(&priv->napi);
637 printk(KERN_ERR "%s - failed to attach interrupt\n",
642 priv->open_time = jiffies;
644 out_8(®s->canctl1, in_8(®s->canctl1) & ~MSCAN_LISTEN);
646 ret = mscan_start(dev);
650 netif_start_queue(dev);
655 static int mscan_close(struct net_device *dev)
657 struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
658 struct mscan_priv *priv = netdev_priv(dev);
660 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
661 napi_disable(&priv->napi);
664 out_8(®s->cantier, 0);
665 out_8(®s->canrier, 0);
666 free_irq(dev->irq, dev);
667 mscan_set_mode(dev, MSCAN_INIT_MODE);
668 can_close_cleanup(dev);
669 netif_stop_queue(dev);
675 int register_mscandev(struct net_device *dev, int clock_src)
677 struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
680 ctl1 = in_8(®s->canctl1);
682 ctl1 |= MSCAN_CLKSRC;
684 ctl1 &= ~MSCAN_CLKSRC;
687 out_8(®s->canctl1, ctl1);
690 /* acceptance mask/acceptance code (accept everything) */
691 out_be16(®s->canidar1_0, 0);
692 out_be16(®s->canidar3_2, 0);
693 out_be16(®s->canidar5_4, 0);
694 out_be16(®s->canidar7_6, 0);
696 out_be16(®s->canidmr1_0, 0xffff);
697 out_be16(®s->canidmr3_2, 0xffff);
698 out_be16(®s->canidmr5_4, 0xffff);
699 out_be16(®s->canidmr7_6, 0xffff);
700 /* Two 32 bit Acceptance Filters */
701 out_8(®s->canidac, MSCAN_AF_32BIT);
703 mscan_set_mode(dev, MSCAN_INIT_MODE);
705 return register_netdev(dev);
707 EXPORT_SYMBOL(register_mscandev);
709 void unregister_mscandev(struct net_device *dev)
711 struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
712 mscan_set_mode(dev, MSCAN_INIT_MODE);
713 out_8(®s->canctl1, in_8(®s->canctl1) & ~MSCAN_CANE);
714 unregister_netdev(dev);
716 EXPORT_SYMBOL(unregister_mscandev);
718 struct net_device *alloc_mscandev(void)
720 struct net_device *dev;
721 struct mscan_priv *priv;
724 dev = alloc_candev(sizeof(struct mscan_priv));
727 priv = netdev_priv(dev);
729 dev->open = mscan_open;
730 dev->stop = mscan_close;
731 dev->hard_start_xmit = mscan_hard_start_xmit;
733 dev->flags |= IFF_ECHO; /* we support local echo */
735 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
737 netif_napi_add(dev, &priv->napi, mscan_rx_poll, 8);
739 dev->poll = mscan_rx_poll;
743 priv->can.do_set_bittime = mscan_do_set_bittime;
744 priv->can.do_set_mode = mscan_do_set_mode;
746 for (i = 0; i < TX_QUEUE_SIZE; i++) {
747 priv->tx_queue[i].id = i;
748 priv->tx_queue[i].mask = 1 << i;
753 EXPORT_SYMBOL(alloc_mscandev);
755 MODULE_AUTHOR("Andrey Volkov <avolkov@varma-el.com>");
756 MODULE_LICENSE("GPL v2");
757 MODULE_DESCRIPTION("CAN port driver for a mscan based chips");