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)
77 static struct can_bittiming_const mscan_bittiming_const = {
94 #define TX_QUEUE_SIZE 3
96 struct tx_queue_entry {
97 struct list_head list;
105 volatile unsigned long flags;
111 struct list_head tx_head;
112 struct tx_queue_entry tx_queue[TX_QUEUE_SIZE];
113 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
114 struct napi_struct napi;
115 struct net_device *dev;
119 #define F_RX_PROGRESS 0
120 #define F_TX_PROGRESS 1
121 #define F_TX_WAIT_ALL 2
123 static enum can_state state_map[] = {
125 CAN_STATE_BUS_WARNING,
126 CAN_STATE_BUS_PASSIVE,
130 static int mscan_set_mode(struct net_device *dev, u8 mode)
132 struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
133 struct mscan_priv *priv = netdev_priv(dev);
138 if (mode != MSCAN_NORMAL_MODE) {
140 if (priv->tx_active) {
141 /* Abort transfers before going to sleep */
142 out_8(®s->cantier, 0);
143 out_8(®s->cantarq, priv->tx_active);
144 out_8(®s->cantier, priv->tx_active);
147 canctl1 = in_8(®s->canctl1);
148 if ((mode & MSCAN_SLPRQ) && (canctl1 & MSCAN_SLPAK) == 0) {
149 out_8(®s->canctl0,
150 in_8(®s->canctl0) | MSCAN_SLPRQ);
151 for (i = 0; i < MSCAN_SET_MODE_RETRIES; i++) {
152 if (in_8(®s->canctl1) & MSCAN_SLPAK)
156 if (i >= MSCAN_SET_MODE_RETRIES)
160 priv->can.state = CAN_STATE_SLEEPING;
162 if (!ret && (mode & MSCAN_INITRQ)
163 && (canctl1 & MSCAN_INITAK) == 0) {
164 out_8(®s->canctl0,
165 in_8(®s->canctl0) | MSCAN_INITRQ);
166 for (i = 0; i < MSCAN_SET_MODE_RETRIES; i++) {
167 if (in_8(®s->canctl1) & MSCAN_INITAK)
170 if (i >= MSCAN_SET_MODE_RETRIES)
174 priv->can.state = CAN_STATE_STOPPED;
176 if (!ret && (mode & MSCAN_CSWAI))
177 out_8(®s->canctl0,
178 in_8(®s->canctl0) | MSCAN_CSWAI);
181 canctl1 = in_8(®s->canctl1);
182 if (canctl1 & (MSCAN_SLPAK | MSCAN_INITAK)) {
183 out_8(®s->canctl0, in_8(®s->canctl0) &
184 ~(MSCAN_SLPRQ | MSCAN_INITRQ));
185 for (i = 0; i < MSCAN_SET_MODE_RETRIES; i++) {
186 canctl1 = in_8(®s->canctl1);
187 if (!(canctl1 & (MSCAN_INITAK | MSCAN_SLPAK)))
190 if (i >= MSCAN_SET_MODE_RETRIES)
193 priv->can.state = CAN_STATE_ACTIVE;
199 static int mscan_start(struct net_device *dev)
201 struct mscan_priv *priv = netdev_priv(dev);
202 struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
206 out_8(®s->canrier, 0);
208 INIT_LIST_HEAD(&priv->tx_head);
211 priv->shadow_canrier = 0;
214 err = mscan_set_mode(dev, MSCAN_NORMAL_MODE);
218 canrflg = in_8(®s->canrflg);
219 priv->shadow_statflg = canrflg & MSCAN_STAT_MSK;
220 priv->can.state = state_map[max(MSCAN_STATE_RX(canrflg),
221 MSCAN_STATE_TX(canrflg))];
222 out_8(®s->cantier, 0);
224 /* Enable receive interrupts. */
225 out_8(®s->canrier, MSCAN_OVRIE | MSCAN_RXFIE | MSCAN_CSCIE |
226 MSCAN_RSTATE1 | MSCAN_RSTATE0 | MSCAN_TSTATE1 | MSCAN_TSTATE0);
231 static int mscan_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
233 struct can_frame *frame = (struct can_frame *)skb->data;
234 struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
235 struct mscan_priv *priv = netdev_priv(dev);
239 if (frame->can_dlc > 8)
242 dev_dbg(ND2D(dev), "%s\n", __func__);
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..) */
257 if (priv->cur_pri == 0xff)
258 set_bit(F_TX_WAIT_ALL, &priv->flags);
259 netif_stop_queue(dev);
261 set_bit(F_TX_PROGRESS, &priv->flags);
263 out_8(®s->cantbsel, i);
265 rtr = frame->can_id & CAN_RTR_FLAG;
267 if (frame->can_id & CAN_EFF_FLAG) {
268 dev_dbg(ND2D(dev), "sending extended frame\n");
270 can_id = (frame->can_id & CAN_EFF_MASK) << 1;
273 out_be16(®s->tx.idr3_2, can_id);
276 can_id = (can_id & 0x7) | ((can_id << 2) & 0xffe0) | (3 << 3);
278 dev_dbg(ND2D(dev), "sending standard frame\n");
279 can_id = (frame->can_id & CAN_SFF_MASK) << 5;
283 out_be16(®s->tx.idr1_0, can_id);
286 volatile void __iomem *data = ®s->tx.dsr1_0;
287 u16 *payload = (u16 *) frame->data;
288 /*Its safe to write into dsr[dlc+1] */
289 for (i = 0; i < (frame->can_dlc + 1) / 2; i++) {
290 out_be16(data, *payload++);
291 data += 2 + _MSCAN_RESERVED_DSR_SIZE;
295 out_8(®s->tx.dlr, frame->can_dlc);
296 out_8(®s->tx.tbpr, priv->cur_pri);
298 /* Start transmission. */
299 out_8(®s->cantflg, 1 << buf_id);
301 if (!test_bit(F_TX_PROGRESS, &priv->flags))
302 dev->trans_start = jiffies;
304 list_add_tail(&priv->tx_queue[buf_id].list, &priv->tx_head);
306 can_put_echo_skb(skb, dev, buf_id);
308 /* Enable interrupt. */
309 priv->tx_active |= 1 << buf_id;
310 out_8(®s->cantier, priv->tx_active);
315 static inline int check_set_state(struct net_device *dev, u8 canrflg)
317 struct mscan_priv *priv = netdev_priv(dev);
318 enum can_state state;
321 if (!(canrflg & MSCAN_CSCIF) || priv->can.state > CAN_STATE_BUS_OFF)
324 state = state_map[max(MSCAN_STATE_RX(canrflg),
325 MSCAN_STATE_TX(canrflg))];
326 if (priv->can.state < state)
328 if (state == CAN_STATE_BUS_OFF)
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,23)
341 struct mscan_priv *priv = container_of(napi, struct mscan_priv, napi);
342 struct net_device *dev = priv->dev;
344 struct mscan_priv *priv = netdev_priv(dev);
345 int quota = min(dev->quota, *budget);
347 struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
348 struct net_device_stats *stats = dev->get_stats(dev);
352 struct can_frame *frame;
357 while (npackets < quota && ((canrflg = in_8(®s->canrflg)) &
358 (MSCAN_RXF | MSCAN_ERR_IF))) {
360 skb = dev_alloc_skb(sizeof(struct can_frame));
362 if (printk_ratelimit())
363 dev_notice(ND2D(dev), "packet dropped\n");
365 out_8(®s->canrflg, canrflg);
369 frame = (struct can_frame *)skb_put(skb, sizeof(*frame));
370 memset(frame, 0, sizeof(*frame));
372 if (canrflg & MSCAN_RXF) {
373 can_id = in_be16(®s->rx.idr1_0);
374 if (can_id & (1 << 3)) {
375 frame->can_id = CAN_EFF_FLAG;
376 can_id = ((can_id << 16) |
377 in_be16(®s->rx.idr3_2));
378 can_id = ((can_id & 0xffe00000) |
379 ((can_id & 0x7ffff) << 2)) >> 2;
385 frame->can_id |= can_id >> 1;
387 frame->can_id |= CAN_RTR_FLAG;
388 frame->can_dlc = in_8(®s->rx.dlr) & 0xf;
390 if (!(frame->can_id & CAN_RTR_FLAG)) {
391 volatile void __iomem *data = ®s->rx.dsr1_0;
392 u16 *payload = (u16 *) frame->data;
393 for (i = 0; i < (frame->can_dlc + 1) / 2; i++) {
394 *payload++ = in_be16(data);
395 data += 2 + _MSCAN_RESERVED_DSR_SIZE;
400 "received pkt: id: %u dlc: %u data: ",
401 frame->can_id, frame->can_dlc);
404 i < frame->can_dlc && !(frame->can_id &
406 printk(KERN_DEBUG "%2x ", frame->data[i]);
407 printk(KERN_DEBUG "\n");
410 out_8(®s->canrflg, MSCAN_RXF);
411 dev->last_rx = jiffies;
413 stats->rx_bytes += frame->can_dlc;
414 } else if (canrflg & MSCAN_ERR_IF) {
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++;
424 if (check_set_state(dev, canrflg)) {
425 frame->can_id |= CAN_ERR_CRTL;
426 switch (priv->can.state) {
427 case CAN_STATE_BUS_WARNING:
428 if ((priv->shadow_statflg &
430 (canrflg & MSCAN_RSTAT_MSK))
432 CAN_ERR_CRTL_RX_WARNING;
434 if ((priv->shadow_statflg &
436 (canrflg & MSCAN_TSTAT_MSK))
438 CAN_ERR_CRTL_TX_WARNING;
440 case CAN_STATE_BUS_PASSIVE:
442 CAN_ERR_CRTL_RX_PASSIVE;
444 case CAN_STATE_BUS_OFF:
445 frame->can_id |= CAN_ERR_BUSOFF;
446 frame->can_id &= ~CAN_ERR_CRTL;
452 priv->shadow_statflg = canrflg & MSCAN_STAT_MSK;
453 frame->can_dlc = CAN_ERR_DLC;
454 out_8(®s->canrflg, MSCAN_ERR_IF);
459 skb->protocol = __constant_htons(ETH_P_CAN);
460 skb->ip_summed = CHECKSUM_UNNECESSARY;
461 netif_receive_skb(skb);
464 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
466 dev->quota -= npackets;
469 if (!(in_8(®s->canrflg) & (MSCAN_RXF | MSCAN_ERR_IF))) {
470 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
471 netif_rx_complete(dev, &priv->napi);
473 netif_rx_complete(dev);
475 clear_bit(F_RX_PROGRESS, &priv->flags);
477 out_8(®s->canrier,
478 in_8(®s->canrier) | MSCAN_ERR_IF | MSCAN_RXFIE);
480 if (priv->can.state < CAN_STATE_BUS_OFF)
481 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 struct net_device_stats *stats = dev->get_stats(dev);
499 u8 cantier, cantflg, canrflg;
500 irqreturn_t ret = IRQ_NONE;
502 cantier = in_8(®s->cantier) & MSCAN_TXE;
503 cantflg = in_8(®s->cantflg) & cantier;
505 if (cantier && cantflg) {
507 struct list_head *tmp, *pos;
509 list_for_each_safe(pos, tmp, &priv->tx_head) {
510 struct tx_queue_entry *entry =
511 list_entry(pos, struct tx_queue_entry, list);
512 u8 mask = entry->mask;
514 if (!(cantflg & mask))
517 out_8(®s->cantbsel, mask);
518 stats->tx_bytes += in_8(®s->tx.dlr);
520 can_get_echo_skb(dev, entry->id);
521 priv->tx_active &= ~mask;
525 if (list_empty(&priv->tx_head)) {
526 clear_bit(F_TX_WAIT_ALL, &priv->flags);
527 clear_bit(F_TX_PROGRESS, &priv->flags);
530 dev->trans_start = jiffies;
532 if (!test_bit(F_TX_WAIT_ALL, &priv->flags))
533 netif_wake_queue(dev);
535 out_8(®s->cantier, priv->tx_active);
539 canrflg = in_8(®s->canrflg);
540 if ((canrflg & ~MSCAN_STAT_MSK) &&
541 !test_and_set_bit(F_RX_PROGRESS, &priv->flags)) {
543 printk(KERN_DEBUG "%s: canrflg=%#02x canrier=%#02x\n",
544 dev->name, canrflg, in_8(®s->canrier));
547 if (check_set_state(dev, canrflg)) {
548 out_8(®s->canrflg, MSCAN_CSCIF);
552 if (canrflg & ~MSCAN_STAT_MSK) {
553 priv->shadow_canrier = in_8(®s->canrier);
554 out_8(®s->canrier, 0);
555 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
556 netif_rx_schedule(dev, &priv->napi);
558 netif_rx_schedule(dev);
562 clear_bit(F_RX_PROGRESS, &priv->flags);
567 static int mscan_do_set_mode(struct net_device *dev, enum can_mode mode)
570 struct mscan_priv *priv = netdev_priv(dev);
573 if (!priv->open_time)
579 netif_stop_queue(dev);
582 CAN_MODE_STOP) ? MSCAN_INIT_MODE :
586 if (priv->can.state <= CAN_STATE_BUS_OFF)
587 mscan_set_mode(dev, MSCAN_INIT_MODE);
588 ret = mscan_start(dev);
591 if (netif_queue_stopped(dev))
592 netif_wake_queue(dev);
602 static int mscan_do_set_bittiming(struct net_device *dev)
604 struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
605 struct mscan_priv *priv = netdev_priv(dev);
606 struct can_bittiming *bt = &priv->can.bittiming;
609 btr0 = BTR0_SET_BRP(bt->brp) | BTR0_SET_SJW(bt->sjw);
610 btr1 = (BTR1_SET_TSEG1(bt->prop_seg + bt->phase_seg1) |
611 BTR1_SET_TSEG2(bt->phase_seg2) |
612 BTR1_SET_SAM(priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES));
614 dev_info(ND2D(dev), "BTR0=0x%02x BTR1=0x%02x\n", btr0, btr1);
616 out_8(®s->canbtr0, btr0);
617 out_8(®s->canbtr1, btr1);
622 static int mscan_open(struct net_device *dev)
625 struct mscan_priv *priv = netdev_priv(dev);
626 struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
628 /* determine and set bittime */
629 ret = can_set_bittiming(dev);
633 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
634 napi_enable(&priv->napi);
636 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18)
637 ret = request_irq(dev->irq, mscan_isr, SA_SHIRQ, dev->name, dev);
639 ret = request_irq(dev->irq, mscan_isr, IRQF_SHARED, dev->name, dev);
643 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
644 napi_disable(&priv->napi);
646 printk(KERN_ERR "%s - failed to attach interrupt\n",
651 priv->open_time = jiffies;
653 out_8(®s->canctl1, in_8(®s->canctl1) & ~MSCAN_LISTEN);
655 ret = mscan_start(dev);
659 netif_start_queue(dev);
664 static int mscan_close(struct net_device *dev)
666 struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
667 struct mscan_priv *priv = netdev_priv(dev);
669 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
670 napi_disable(&priv->napi);
673 out_8(®s->cantier, 0);
674 out_8(®s->canrier, 0);
675 free_irq(dev->irq, dev);
676 mscan_set_mode(dev, MSCAN_INIT_MODE);
677 can_close_cleanup(dev);
678 netif_stop_queue(dev);
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_netdev(dev);
716 EXPORT_SYMBOL(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_netdev(dev);
725 EXPORT_SYMBOL(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));
736 priv = netdev_priv(dev);
738 dev->open = mscan_open;
739 dev->stop = mscan_close;
740 dev->hard_start_xmit = mscan_hard_start_xmit;
742 dev->flags |= IFF_ECHO; /* we support local echo */
744 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
746 netif_napi_add(dev, &priv->napi, mscan_rx_poll, 8);
748 dev->poll = mscan_rx_poll;
752 priv->can.bittiming_const = &mscan_bittiming_const;
753 priv->can.do_set_bittiming = mscan_do_set_bittiming;
754 priv->can.do_set_mode = mscan_do_set_mode;
756 for (i = 0; i < TX_QUEUE_SIZE; i++) {
757 priv->tx_queue[i].id = i;
758 priv->tx_queue[i].mask = 1 << i;
763 EXPORT_SYMBOL(alloc_mscandev);
765 MODULE_AUTHOR("Andrey Volkov <avolkov@varma-el.com>");
766 MODULE_LICENSE("GPL v2");
767 MODULE_DESCRIPTION("CAN port driver for a mscan based chips");