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;
88 volatile unsigned long flags;
94 struct list_head tx_head;
95 tx_queue_entry_t tx_queue[TX_QUEUE_SIZE];
96 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
97 struct napi_struct napi;
98 struct net_device *dev;
102 #define F_RX_PROGRESS 0
103 #define F_TX_PROGRESS 1
104 #define F_TX_WAIT_ALL 2
106 static int mscan_set_mode(struct net_device *dev, u8 mode)
108 struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
113 if (mode != MSCAN_NORMAL_MODE) {
114 canctl1 = in_8(®s->canctl1);
115 if ((mode & MSCAN_SLPRQ) && (canctl1 & MSCAN_SLPAK) == 0) {
116 out_8(®s->canctl0,
117 in_8(®s->canctl0) | MSCAN_SLPRQ);
118 for (i = 0; i < MSCAN_SET_MODE_RETRIES; i++) {
119 if (in_8(®s->canctl1) & MSCAN_SLPAK)
123 if (i >= MSCAN_SET_MODE_RETRIES)
127 if (!ret && (mode & MSCAN_INITRQ)
128 && (canctl1 & MSCAN_INITAK) == 0) {
129 out_8(®s->canctl0,
130 in_8(®s->canctl0) | MSCAN_INITRQ);
131 for (i = 0; i < MSCAN_SET_MODE_RETRIES; i++) {
132 if (in_8(®s->canctl1) & MSCAN_INITAK)
135 if (i >= MSCAN_SET_MODE_RETRIES)
139 if (!ret && (mode & MSCAN_CSWAI))
140 out_8(®s->canctl0,
141 in_8(®s->canctl0) | MSCAN_CSWAI);
144 canctl1 = in_8(®s->canctl1);
145 if (canctl1 & (MSCAN_SLPAK | MSCAN_INITAK)) {
146 out_8(®s->canctl0, in_8(®s->canctl0) &
147 ~(MSCAN_SLPRQ | MSCAN_INITRQ));
148 for (i = 0; i < MSCAN_SET_MODE_RETRIES; i++) {
149 canctl1 = in_8(®s->canctl1);
150 if (!(canctl1 & (MSCAN_INITAK | MSCAN_SLPAK)))
153 if (i >= MSCAN_SET_MODE_RETRIES)
160 static void mscan_push_state(struct net_device *dev, struct mscan_state *state)
162 struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
164 state->mode = in_8(®s->canctl0) & (MSCAN_SLPRQ | MSCAN_INITRQ |
166 state->canrier = in_8(®s->canrier);
167 state->cantier = in_8(®s->cantier);
170 static int mscan_pop_state(struct net_device *dev, struct mscan_state *state)
172 struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
175 ret = mscan_set_mode(dev, state->mode);
177 out_8(®s->canrier, state->canrier);
178 out_8(®s->cantier, state->cantier);
183 static int mscan_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
185 struct can_frame *frame = (struct can_frame *)skb->data;
186 struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
187 struct mscan_priv *priv = netdev_priv(dev);
192 if (frame->can_dlc > 8)
195 dev_dbg(ND2D(dev), "%s\n", __FUNCTION__);
196 out_8(®s->cantier, 0);
198 i = ~priv->tx_active & MSCAN_TXE;
200 switch (hweight8(i)) {
202 netif_stop_queue(dev);
203 dev_err(ND2D(dev), "BUG! Tx Ring full when queue awake!\n");
204 return NETDEV_TX_BUSY;
206 /* if buf_id < 3, then current frame will be send out of order,
207 since buffer with lower id have higher priority (hell..) */
210 if (priv->cur_pri == 0xff)
211 set_bit(F_TX_WAIT_ALL, &priv->flags);
212 netif_stop_queue(dev);
214 set_bit(F_TX_PROGRESS, &priv->flags);
216 out_8(®s->cantbsel, i);
218 rtr = frame->can_id & CAN_RTR_FLAG;
220 if (frame->can_id & CAN_EFF_FLAG) {
221 dev_dbg(ND2D(dev), "sending extended frame\n");
223 can_id = (frame->can_id & CAN_EFF_MASK) << 1;
226 out_be16(®s->tx.idr3_2, can_id);
229 can_id = (can_id & 0x7) | ((can_id << 2) & 0xffe0) | (3 << 3);
231 dev_dbg(ND2D(dev), "sending standard frame\n");
232 can_id = (frame->can_id & CAN_SFF_MASK) << 5;
236 out_be16(®s->tx.idr1_0, can_id);
239 volatile void __iomem *data = ®s->tx.dsr1_0;
240 u16 *payload = (u16 *) frame->data;
241 /*Its safe to write into dsr[dlc+1] */
242 for (i = 0; i < (frame->can_dlc + 1) / 2; i++) {
243 out_be16(data, *payload++);
244 data += 2 + _MSCAN_RESERVED_DSR_SIZE;
248 out_8(®s->tx.dlr, frame->can_dlc);
249 out_8(®s->tx.tbpr, priv->cur_pri);
251 /* Start transmission. */
252 out_8(®s->cantflg, 1 << buf_id);
254 if (!test_bit(F_TX_PROGRESS, &priv->flags))
255 dev->trans_start = jiffies;
257 list_add_tail(&priv->tx_queue[buf_id].list, &priv->tx_head);
261 /* Enable interrupt. */
262 priv->tx_active |= 1 << buf_id;
263 out_8(®s->cantier, priv->tx_active);
268 static void mscan_tx_timeout(struct net_device *dev)
271 struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
272 struct mscan_priv *priv = netdev_priv(dev);
273 struct can_frame *frame;
276 printk("%s\n", __FUNCTION__);
278 out_8(®s->cantier, 0);
280 mask = list_entry(priv->tx_head.next, tx_queue_entry_t, list)->mask;
281 dev->trans_start = jiffies;
282 out_8(®s->cantarq, mask);
283 out_8(®s->cantier, priv->tx_active);
285 skb = dev_alloc_skb(sizeof(struct can_frame));
287 if (printk_ratelimit())
288 dev_notice(ND2D(dev), "TIMEOUT packet dropped\n");
291 frame = (struct can_frame *)skb_put(skb, sizeof(struct can_frame));
293 frame->can_id = CAN_ERR_FLAG | CAN_ERR_TX_TIMEOUT;
294 frame->can_dlc = CAN_ERR_DLC;
297 skb->protocol = __constant_htons(ETH_P_CAN);
298 skb->pkt_type = PACKET_BROADCAST;
299 skb->ip_summed = CHECKSUM_UNNECESSARY;
305 static can_state_t state_map[] = {
307 CAN_STATE_BUS_WARNING,
308 CAN_STATE_BUS_PASSIVE,
312 static inline int check_set_state(struct net_device *dev, u8 canrflg)
314 struct mscan_priv *priv = netdev_priv(dev);
318 if (!(canrflg & MSCAN_CSCIF) || priv->can.state > CAN_STATE_BUS_OFF)
322 state_map[max(MSCAN_STATE_RX(canrflg), MSCAN_STATE_TX(canrflg))];
323 if (priv->can.state < state)
325 if (state == CAN_STATE_BUS_OFF)
326 netif_carrier_off(dev);
327 else if (priv->can.state == CAN_STATE_BUS_OFF
328 && state != CAN_STATE_BUS_OFF)
329 netif_carrier_on(dev);
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,
370 sizeof(struct can_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("%2x ", frame->data[i]);
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;
450 priv->shadow_statflg = canrflg & MSCAN_STAT_MSK;
451 frame->can_dlc = CAN_ERR_DLC;
452 out_8(®s->canrflg, MSCAN_ERR_IF);
457 skb->protocol = __constant_htons(ETH_P_CAN);
458 skb->ip_summed = CHECKSUM_UNNECESSARY;
459 netif_receive_skb(skb);
462 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
464 dev->quota -= npackets;
467 if (!(in_8(®s->canrflg) & (MSCAN_RXF | MSCAN_ERR_IF))) {
468 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
469 netif_rx_complete(dev, &priv->napi);
471 netif_rx_complete(dev);
473 clear_bit(F_RX_PROGRESS, &priv->flags);
474 out_8(®s->canrier,
475 in_8(®s->canrier) | MSCAN_ERR_IF | MSCAN_RXFIE);
482 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
483 static irqreturn_t mscan_isr(int irq, void *dev_id, struct pt_regs *r)
485 static irqreturn_t mscan_isr(int irq, void *dev_id)
488 struct net_device *dev = (struct net_device *)dev_id;
489 struct mscan_priv *priv = netdev_priv(dev);
490 struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
491 struct net_device_stats *stats = dev->get_stats(dev);
493 irqreturn_t ret = IRQ_NONE;
495 if (in_8(®s->cantier) & MSCAN_TXE) {
496 struct list_head *tmp, *pos;
498 cantflg = in_8(®s->cantflg) & MSCAN_TXE;
500 list_for_each_safe(pos, tmp, &priv->tx_head) {
501 tx_queue_entry_t *entry =
502 list_entry(pos, tx_queue_entry_t, list);
503 u8 mask = entry->mask;
505 if (!(cantflg & mask))
508 if (in_8(®s->cantaak) & mask) {
510 stats->tx_aborted_errors++;
512 out_8(®s->cantbsel, mask);
517 priv->tx_active &= ~mask;
521 if (list_empty(&priv->tx_head)) {
522 clear_bit(F_TX_WAIT_ALL, &priv->flags);
523 clear_bit(F_TX_PROGRESS, &priv->flags);
526 dev->trans_start = jiffies;
528 if (!test_bit(F_TX_WAIT_ALL, &priv->flags))
529 netif_wake_queue(dev);
531 out_8(®s->cantier, priv->tx_active);
535 if ((((canrflg = in_8(®s->canrflg)) & ~MSCAN_STAT_MSK)) &&
536 !test_and_set_bit(F_RX_PROGRESS, &priv->flags)) {
537 if (check_set_state(dev, canrflg)) {
538 out_8(®s->canrflg, MSCAN_CSCIF);
541 if (canrflg & ~MSCAN_STAT_MSK) {
542 priv->shadow_canrier = in_8(®s->canrier);
543 out_8(®s->canrier, 0);
544 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
545 netif_rx_schedule(dev, &priv->napi);
547 netif_rx_schedule(dev);
551 clear_bit(F_RX_PROGRESS, &priv->flags);
556 static int mscan_do_set_mode(struct net_device *dev, can_mode_t mode)
561 netif_stop_queue(dev);
564 CAN_MODE_STOP) ? MSCAN_INIT_MODE :
568 printk("%s: CAN_MODE_START requested\n", __FUNCTION__);
569 mscan_set_mode(dev, MSCAN_NORMAL_MODE);
570 netif_wake_queue(dev);
579 static int mscan_do_set_bit_time(struct net_device *dev,
580 struct can_bittime *bt)
582 struct mscan_priv *priv = netdev_priv(dev);
583 struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
586 struct mscan_state state;
588 if (bt->type != CAN_BITTIME_STD)
591 spin_lock_irq(&priv->can.irq_lock);
593 mscan_push_state(dev, &state);
594 ret = mscan_set_mode(dev, MSCAN_INIT_MODE);
596 reg = BTR0_SET_BRP(bt->std.brp) | BTR0_SET_SJW(bt->std.sjw);
597 out_8(®s->canbtr0, reg);
599 reg = (BTR1_SET_TSEG1(bt->std.prop_seg + bt->std.phase_seg1) |
600 BTR1_SET_TSEG2(bt->std.phase_seg2) |
601 BTR1_SET_SAM(bt->std.sam));
602 out_8(®s->canbtr1, reg);
604 ret = mscan_pop_state(dev, &state);
607 spin_unlock_irq(&priv->can.irq_lock);
611 static int mscan_open(struct net_device *dev)
614 struct mscan_priv *priv = netdev_priv(dev);
615 struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
617 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
618 napi_enable(&priv->napi);
620 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18)
621 ret = request_irq(dev->irq, mscan_isr, SA_SHIRQ, dev->name, dev);
623 ret = request_irq(dev->irq, mscan_isr, IRQF_SHARED, dev->name, dev);
627 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
628 napi_disable(&priv->napi);
630 printk(KERN_ERR "%s - failed to attach interrupt\n",
635 INIT_LIST_HEAD(&priv->tx_head);
636 /* acceptance mask/acceptance code (accept everything) */
637 out_be16(®s->canidar1_0, 0);
638 out_be16(®s->canidar3_2, 0);
639 out_be16(®s->canidar5_4, 0);
640 out_be16(®s->canidar7_6, 0);
642 out_be16(®s->canidmr1_0, 0xffff);
643 out_be16(®s->canidmr3_2, 0xffff);
644 out_be16(®s->canidmr5_4, 0xffff);
645 out_be16(®s->canidmr7_6, 0xffff);
646 /* Two 32 bit Acceptance Filters */
647 out_8(®s->canidac, MSCAN_AF_32BIT);
649 out_8(®s->canctl1, in_8(®s->canctl1) & ~MSCAN_LISTEN);
650 mscan_set_mode(dev, MSCAN_NORMAL_MODE);
652 priv->shadow_statflg = in_8(®s->canrflg) & MSCAN_STAT_MSK;
656 out_8(®s->cantier, 0);
657 /* Enable receive interrupts. */
658 out_8(®s->canrier, MSCAN_OVRIE | MSCAN_RXFIE | MSCAN_CSCIE |
659 MSCAN_RSTATE1 | MSCAN_RSTATE0 | MSCAN_TSTATE1 | MSCAN_TSTATE0);
661 netif_start_queue(dev);
666 static int mscan_close(struct net_device *dev)
668 struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
670 netif_stop_queue(dev);
672 /* disable interrupts */
673 out_8(®s->cantier, 0);
674 out_8(®s->canrier, 0);
675 free_irq(dev->irq, dev);
677 mscan_set_mode(dev, MSCAN_INIT_MODE);
681 int register_mscandev(struct net_device *dev, int clock_src)
683 struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
686 ctl1 = in_8(®s->canctl1);
688 ctl1 |= MSCAN_CLKSRC;
690 ctl1 &= ~MSCAN_CLKSRC;
693 out_8(®s->canctl1, ctl1);
696 mscan_set_mode(dev, MSCAN_INIT_MODE);
698 return register_netdev(dev);
701 EXPORT_SYMBOL(register_mscandev);
703 void unregister_mscandev(struct net_device *dev)
705 struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
706 mscan_set_mode(dev, MSCAN_INIT_MODE);
707 out_8(®s->canctl1, in_8(®s->canctl1) & ~MSCAN_CANE);
708 unregister_netdev(dev);
711 EXPORT_SYMBOL(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 dev->watchdog_timeo = MSCAN_WATCHDOG_TIMEOUT;
725 dev->open = mscan_open;
726 dev->stop = mscan_close;
727 dev->hard_start_xmit = mscan_hard_start_xmit;
728 dev->tx_timeout = mscan_tx_timeout;
730 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
732 netif_napi_add(dev, &priv->napi, mscan_rx_poll, 8);
734 dev->poll = mscan_rx_poll;
738 priv->can.do_set_bit_time = mscan_do_set_bit_time;
739 priv->can.do_set_mode = mscan_do_set_mode;
741 for (i = 0; i < TX_QUEUE_SIZE; i++)
742 priv->tx_queue[i].mask = 1 << i;
747 EXPORT_SYMBOL(alloc_mscandev);
749 MODULE_AUTHOR("Andrey Volkov <avolkov@varma-el.com>");
750 MODULE_LICENSE("GPL v2");
751 MODULE_DESCRIPTION("CAN port driver for a mscan based chips");