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/can.h>
38 #include <linux/list.h>
41 #include <linux/can/dev.h>
42 #include <linux/can/error.h>
45 #include <linux/can/version.h> /* for RCSID. Removed by mkpatch script */
48 #define MSCAN_NORMAL_MODE 0
49 #define MSCAN_SLEEP_MODE MSCAN_SLPRQ
50 #define MSCAN_INIT_MODE (MSCAN_INITRQ | MSCAN_SLPRQ)
51 #define MSCAN_POWEROFF_MODE (MSCAN_CSWAI | MSCAN_SLPRQ)
52 #define MSCAN_SET_MODE_RETRIES 255
55 #define BTR0_BRP_MASK 0x3f
56 #define BTR0_SJW_SHIFT 6
57 #define BTR0_SJW_MASK (0x3 << BTR0_SJW_SHIFT)
59 #define BTR1_TSEG1_MASK 0xf
60 #define BTR1_TSEG2_SHIFT 4
61 #define BTR1_TSEG2_MASK (0x7 << BTR1_TSEG2_SHIFT)
62 #define BTR1_SAM_SHIFT 7
64 #define BTR0_SET_BRP(brp) (((brp) - 1) & BTR0_BRP_MASK)
65 #define BTR0_SET_SJW(sjw) ((((sjw) - 1) << BTR0_SJW_SHIFT) & \
68 #define BTR1_SET_TSEG1(tseg1) (((tseg1) - 1) & BTR1_TSEG1_MASK)
69 #define BTR1_SET_TSEG2(tseg2) ((((tseg2) - 1) << BTR1_TSEG2_SHIFT) & \
71 #define BTR1_SET_SAM(sam) (((sam) & 1) << BTR1_SAM_SHIFT)
79 #define TX_QUEUE_SIZE 3
82 struct list_head list;
90 volatile unsigned long flags;
96 struct list_head tx_head;
97 tx_queue_entry_t tx_queue[TX_QUEUE_SIZE];
98 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
99 struct napi_struct napi;
100 struct net_device *dev;
104 #define F_RX_PROGRESS 0
105 #define F_TX_PROGRESS 1
106 #define F_TX_WAIT_ALL 2
108 static can_state_t state_map[] = {
110 CAN_STATE_BUS_WARNING,
111 CAN_STATE_BUS_PASSIVE,
115 static int mscan_set_mode(struct net_device *dev, u8 mode)
117 struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
118 struct mscan_priv *priv = netdev_priv(dev);
123 if (mode != MSCAN_NORMAL_MODE) {
125 if (priv->tx_active) {
126 /* Abort transfers before going to sleep */
127 out_8(®s->cantier, 0);
128 out_8(®s->cantarq, priv->tx_active);
129 out_8(®s->cantier, priv->tx_active);
132 canctl1 = in_8(®s->canctl1);
133 if ((mode & MSCAN_SLPRQ) && (canctl1 & MSCAN_SLPAK) == 0) {
134 out_8(®s->canctl0,
135 in_8(®s->canctl0) | MSCAN_SLPRQ);
136 for (i = 0; i < MSCAN_SET_MODE_RETRIES; i++) {
137 if (in_8(®s->canctl1) & MSCAN_SLPAK)
141 if (i >= MSCAN_SET_MODE_RETRIES)
145 priv->can.state = CAN_STATE_SLEEPING;
147 if (!ret && (mode & MSCAN_INITRQ)
148 && (canctl1 & MSCAN_INITAK) == 0) {
149 out_8(®s->canctl0,
150 in_8(®s->canctl0) | MSCAN_INITRQ);
151 for (i = 0; i < MSCAN_SET_MODE_RETRIES; i++) {
152 if (in_8(®s->canctl1) & MSCAN_INITAK)
155 if (i >= MSCAN_SET_MODE_RETRIES)
159 priv->can.state = CAN_STATE_STOPPED;
161 if (!ret && (mode & MSCAN_CSWAI))
162 out_8(®s->canctl0,
163 in_8(®s->canctl0) | MSCAN_CSWAI);
166 canctl1 = in_8(®s->canctl1);
167 if (canctl1 & (MSCAN_SLPAK | MSCAN_INITAK)) {
168 out_8(®s->canctl0, in_8(®s->canctl0) &
169 ~(MSCAN_SLPRQ | MSCAN_INITRQ));
170 for (i = 0; i < MSCAN_SET_MODE_RETRIES; i++) {
171 canctl1 = in_8(®s->canctl1);
172 if (!(canctl1 & (MSCAN_INITAK | MSCAN_SLPAK)))
175 if (i >= MSCAN_SET_MODE_RETRIES)
178 priv->can.state = CAN_STATE_ACTIVE;
184 static int mscan_start(struct net_device *dev)
186 struct mscan_priv *priv = netdev_priv(dev);
187 struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
191 out_8(®s->canrier, 0);
193 INIT_LIST_HEAD(&priv->tx_head);
196 priv->shadow_canrier = 0;
199 if ((err = mscan_set_mode(dev, MSCAN_NORMAL_MODE)))
202 canrflg = in_8(®s->canrflg);
203 priv->shadow_statflg = canrflg & MSCAN_STAT_MSK;
204 priv->can.state = state_map[max(MSCAN_STATE_RX(canrflg),
205 MSCAN_STATE_TX(canrflg))];
206 out_8(®s->cantier, 0);
208 /* Enable receive interrupts. */
209 out_8(®s->canrier, MSCAN_OVRIE | MSCAN_RXFIE | MSCAN_CSCIE |
210 MSCAN_RSTATE1 | MSCAN_RSTATE0 | MSCAN_TSTATE1 | MSCAN_TSTATE0);
215 static int mscan_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
217 struct can_frame *frame = (struct can_frame *)skb->data;
218 struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
219 struct mscan_priv *priv = netdev_priv(dev);
223 if (frame->can_dlc > 8)
226 dev_dbg(ND2D(dev), "%s\n", __FUNCTION__);
227 out_8(®s->cantier, 0);
229 i = ~priv->tx_active & MSCAN_TXE;
231 switch (hweight8(i)) {
233 netif_stop_queue(dev);
234 dev_err(ND2D(dev), "BUG! Tx Ring full when queue awake!\n");
235 return NETDEV_TX_BUSY;
237 /* if buf_id < 3, then current frame will be send out of order,
238 since buffer with lower id have higher priority (hell..) */
241 if (priv->cur_pri == 0xff)
242 set_bit(F_TX_WAIT_ALL, &priv->flags);
243 netif_stop_queue(dev);
245 set_bit(F_TX_PROGRESS, &priv->flags);
247 out_8(®s->cantbsel, i);
249 rtr = frame->can_id & CAN_RTR_FLAG;
251 if (frame->can_id & CAN_EFF_FLAG) {
252 dev_dbg(ND2D(dev), "sending extended frame\n");
254 can_id = (frame->can_id & CAN_EFF_MASK) << 1;
257 out_be16(®s->tx.idr3_2, can_id);
260 can_id = (can_id & 0x7) | ((can_id << 2) & 0xffe0) | (3 << 3);
262 dev_dbg(ND2D(dev), "sending standard frame\n");
263 can_id = (frame->can_id & CAN_SFF_MASK) << 5;
267 out_be16(®s->tx.idr1_0, can_id);
270 volatile void __iomem *data = ®s->tx.dsr1_0;
271 u16 *payload = (u16 *) frame->data;
272 /*Its safe to write into dsr[dlc+1] */
273 for (i = 0; i < (frame->can_dlc + 1) / 2; i++) {
274 out_be16(data, *payload++);
275 data += 2 + _MSCAN_RESERVED_DSR_SIZE;
279 out_8(®s->tx.dlr, frame->can_dlc);
280 out_8(®s->tx.tbpr, priv->cur_pri);
282 /* Start transmission. */
283 out_8(®s->cantflg, 1 << buf_id);
285 if (!test_bit(F_TX_PROGRESS, &priv->flags))
286 dev->trans_start = jiffies;
288 list_add_tail(&priv->tx_queue[buf_id].list, &priv->tx_head);
290 can_put_echo_skb(skb, dev, buf_id);
292 /* Enable interrupt. */
293 priv->tx_active |= 1 << buf_id;
294 out_8(®s->cantier, priv->tx_active);
299 static inline int check_set_state(struct net_device *dev, u8 canrflg)
301 struct mscan_priv *priv = netdev_priv(dev);
305 if (!(canrflg & MSCAN_CSCIF) || priv->can.state > CAN_STATE_BUS_OFF)
308 state = state_map[max(MSCAN_STATE_RX(canrflg),
309 MSCAN_STATE_TX(canrflg))];
310 if (priv->can.state < state)
312 if (state == CAN_STATE_BUS_OFF)
314 priv->can.state = state;
318 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
319 static int mscan_rx_poll(struct napi_struct *napi, int quota)
321 static int mscan_rx_poll(struct net_device *dev, int *budget)
324 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
325 struct mscan_priv *priv = container_of(napi, struct mscan_priv, napi);
326 struct net_device *dev = priv->dev;
328 struct mscan_priv *priv = netdev_priv(dev);
329 int quota = min(dev->quota, *budget);
331 struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
332 struct net_device_stats *stats = dev->get_stats(dev);
336 struct can_frame *frame;
341 while (npackets < quota && ((canrflg = in_8(®s->canrflg)) &
342 (MSCAN_RXF | MSCAN_ERR_IF))) {
344 skb = dev_alloc_skb(sizeof(struct can_frame));
346 if (printk_ratelimit())
347 dev_notice(ND2D(dev), "packet dropped\n");
349 out_8(®s->canrflg, canrflg);
353 frame = (struct can_frame *)skb_put(skb, sizeof(*frame));
354 memset(frame, 0, sizeof(*frame));
356 if (canrflg & MSCAN_RXF) {
357 can_id = in_be16(®s->rx.idr1_0);
358 if (can_id & (1 << 3)) {
359 frame->can_id = CAN_EFF_FLAG;
360 can_id = ((can_id << 16) |
361 in_be16(®s->rx.idr3_2));
362 can_id = ((can_id & 0xffe00000) |
363 ((can_id & 0x7ffff) << 2)) >> 2;
369 frame->can_id |= can_id >> 1;
371 frame->can_id |= CAN_RTR_FLAG;
372 frame->can_dlc = in_8(®s->rx.dlr) & 0xf;
374 if (!(frame->can_id & CAN_RTR_FLAG)) {
375 volatile void __iomem *data = ®s->rx.dsr1_0;
376 u16 *payload = (u16 *) frame->data;
377 for (i = 0; i < (frame->can_dlc + 1) / 2; i++) {
378 *payload++ = in_be16(data);
379 data += 2 + _MSCAN_RESERVED_DSR_SIZE;
384 "received pkt: id: %u dlc: %u data: ",
385 frame->can_id, frame->can_dlc);
388 i < frame->can_dlc && !(frame->can_id &
390 printk("%2x ", frame->data[i]);
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 frame->can_id = CAN_ERR_FLAG;
401 if (canrflg & MSCAN_OVRIF) {
402 frame->can_id |= CAN_ERR_CRTL;
403 frame->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
404 stats->rx_over_errors++;
408 if (check_set_state(dev, canrflg)) {
409 frame->can_id |= CAN_ERR_CRTL;
410 switch (priv->can.state) {
411 case CAN_STATE_BUS_WARNING:
412 if ((priv->shadow_statflg &
414 (canrflg & MSCAN_RSTAT_MSK))
416 CAN_ERR_CRTL_RX_WARNING;
418 if ((priv->shadow_statflg &
420 (canrflg & MSCAN_TSTAT_MSK))
422 CAN_ERR_CRTL_TX_WARNING;
424 case CAN_STATE_BUS_PASSIVE:
426 CAN_ERR_CRTL_RX_PASSIVE;
428 case CAN_STATE_BUS_OFF:
429 frame->can_id |= CAN_ERR_BUSOFF;
430 frame->can_id &= ~CAN_ERR_CRTL;
436 priv->shadow_statflg = canrflg & MSCAN_STAT_MSK;
437 frame->can_dlc = CAN_ERR_DLC;
438 out_8(®s->canrflg, MSCAN_ERR_IF);
443 skb->protocol = __constant_htons(ETH_P_CAN);
444 skb->ip_summed = CHECKSUM_UNNECESSARY;
445 netif_receive_skb(skb);
448 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
450 dev->quota -= npackets;
453 if (!(in_8(®s->canrflg) & (MSCAN_RXF | MSCAN_ERR_IF))) {
454 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
455 netif_rx_complete(dev, &priv->napi);
457 netif_rx_complete(dev);
459 clear_bit(F_RX_PROGRESS, &priv->flags);
461 out_8(®s->canrier,
462 in_8(®s->canrier) | MSCAN_ERR_IF | MSCAN_RXFIE);
464 if (priv->can.state < CAN_STATE_BUS_OFF)
465 out_8(®s->canrier, priv->shadow_canrier);
473 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
474 static irqreturn_t mscan_isr(int irq, void *dev_id, struct pt_regs *r)
476 static irqreturn_t mscan_isr(int irq, void *dev_id)
479 struct net_device *dev = (struct net_device *)dev_id;
480 struct mscan_priv *priv = netdev_priv(dev);
481 struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
482 struct net_device_stats *stats = dev->get_stats(dev);
483 u8 cantier, cantflg, canrflg;
484 irqreturn_t ret = IRQ_NONE;
486 if ((cantier = in_8(®s->cantier) & MSCAN_TXE) &&
487 (cantflg = in_8(®s->cantflg) & cantier)) {
488 struct list_head *tmp, *pos;
490 list_for_each_safe(pos, tmp, &priv->tx_head) {
491 tx_queue_entry_t *entry =
492 list_entry(pos, tx_queue_entry_t, list);
493 u8 mask = entry->mask;
495 if (!(cantflg & mask))
498 out_8(®s->cantbsel, mask);
499 stats->tx_bytes += in_8(®s->tx.dlr);
501 can_get_echo_skb(dev, entry->id);
502 priv->tx_active &= ~mask;
506 if (list_empty(&priv->tx_head)) {
507 clear_bit(F_TX_WAIT_ALL, &priv->flags);
508 clear_bit(F_TX_PROGRESS, &priv->flags);
511 dev->trans_start = jiffies;
513 if (!test_bit(F_TX_WAIT_ALL, &priv->flags))
514 netif_wake_queue(dev);
516 out_8(®s->cantier, priv->tx_active);
520 if ((((canrflg = in_8(®s->canrflg)) & ~MSCAN_STAT_MSK)) &&
521 !test_and_set_bit(F_RX_PROGRESS, &priv->flags)) {
523 printk("%s: canrflg=%#02x canrier=%#02x\n", dev->name, canrflg,
524 in_8(®s->canrier));
527 if (check_set_state(dev, canrflg)) {
528 out_8(®s->canrflg, MSCAN_CSCIF);
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,23)
536 netif_rx_schedule(dev, &priv->napi);
538 netif_rx_schedule(dev);
542 clear_bit(F_RX_PROGRESS, &priv->flags);
547 static int mscan_do_set_mode(struct net_device *dev, can_mode_t mode)
550 struct mscan_priv *priv = netdev_priv(dev);
553 if (!priv->open_time)
559 netif_stop_queue(dev);
562 CAN_MODE_STOP) ? MSCAN_INIT_MODE :
566 if (priv->can.state <= CAN_STATE_BUS_OFF)
567 mscan_set_mode(dev, MSCAN_INIT_MODE);
568 if ((ret = mscan_start(dev)))
570 if (netif_queue_stopped(dev))
571 netif_wake_queue(dev);
581 static int mscan_do_set_bittime(struct net_device *dev, struct can_bittime *bt)
583 struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
587 case CAN_BITTIME_BTR:
592 case CAN_BITTIME_STD:
593 btr0 = BTR0_SET_BRP(bt->std.brp) | BTR0_SET_SJW(bt->std.sjw);
594 btr1 = (BTR1_SET_TSEG1(bt->std.prop_seg + bt->std.phase_seg1) |
595 BTR1_SET_TSEG2(bt->std.phase_seg2) |
596 BTR1_SET_SAM(bt->std.sam));
603 out_8(®s->canbtr0, btr0);
604 out_8(®s->canbtr1, btr1);
609 static int mscan_open(struct net_device *dev)
612 struct mscan_priv *priv = netdev_priv(dev);
613 struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
615 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
616 napi_enable(&priv->napi);
618 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18)
619 ret = request_irq(dev->irq, mscan_isr, SA_SHIRQ, dev->name, dev);
621 ret = request_irq(dev->irq, mscan_isr, IRQF_SHARED, dev->name, dev);
625 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
626 napi_disable(&priv->napi);
628 printk(KERN_ERR "%s - failed to attach interrupt\n",
633 priv->open_time = jiffies;
635 out_8(®s->canctl1, in_8(®s->canctl1) & ~MSCAN_LISTEN);
637 if ((ret = mscan_start(dev)))
639 netif_start_queue(dev);
644 static int mscan_close(struct net_device *dev)
646 struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
647 struct mscan_priv *priv = netdev_priv(dev);
649 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
650 napi_disable(&priv->napi);
653 out_8(®s->cantier, 0);
654 out_8(®s->canrier, 0);
655 free_irq(dev->irq, dev);
656 mscan_set_mode(dev, MSCAN_INIT_MODE);
657 can_close_cleanup(dev);
658 netif_stop_queue(dev);
664 int register_mscandev(struct net_device *dev, int clock_src)
666 struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
669 ctl1 = in_8(®s->canctl1);
671 ctl1 |= MSCAN_CLKSRC;
673 ctl1 &= ~MSCAN_CLKSRC;
676 out_8(®s->canctl1, ctl1);
679 /* acceptance mask/acceptance code (accept everything) */
680 out_be16(®s->canidar1_0, 0);
681 out_be16(®s->canidar3_2, 0);
682 out_be16(®s->canidar5_4, 0);
683 out_be16(®s->canidar7_6, 0);
685 out_be16(®s->canidmr1_0, 0xffff);
686 out_be16(®s->canidmr3_2, 0xffff);
687 out_be16(®s->canidmr5_4, 0xffff);
688 out_be16(®s->canidmr7_6, 0xffff);
689 /* Two 32 bit Acceptance Filters */
690 out_8(®s->canidac, MSCAN_AF_32BIT);
692 mscan_set_mode(dev, MSCAN_INIT_MODE);
694 return register_netdev(dev);
697 EXPORT_SYMBOL(register_mscandev);
699 void unregister_mscandev(struct net_device *dev)
701 struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
702 mscan_set_mode(dev, MSCAN_INIT_MODE);
703 out_8(®s->canctl1, in_8(®s->canctl1) & ~MSCAN_CANE);
704 unregister_netdev(dev);
707 EXPORT_SYMBOL(unregister_mscandev);
709 struct net_device *alloc_mscandev(void)
711 struct net_device *dev;
712 struct mscan_priv *priv;
715 dev = alloc_candev(sizeof(struct mscan_priv));
718 priv = netdev_priv(dev);
720 dev->open = mscan_open;
721 dev->stop = mscan_close;
722 dev->hard_start_xmit = mscan_hard_start_xmit;
724 dev->flags |= IFF_ECHO; /* we support local echo */
726 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
728 netif_napi_add(dev, &priv->napi, mscan_rx_poll, 8);
730 dev->poll = mscan_rx_poll;
734 priv->can.do_set_bittime = mscan_do_set_bittime;
735 priv->can.do_set_mode = mscan_do_set_mode;
737 for (i = 0; i < TX_QUEUE_SIZE; i++) {
738 priv->tx_queue[i].id = i;
739 priv->tx_queue[i].mask = 1 << i;
745 EXPORT_SYMBOL(alloc_mscandev);
747 MODULE_AUTHOR("Andrey Volkov <avolkov@varma-el.com>");
748 MODULE_LICENSE("GPL v2");
749 MODULE_DESCRIPTION("CAN port driver for a mscan based chips");