1 [PATCH 9/9] Socket-CAN: driver for the Freescale MSCAN controller
3 This patch adds the generic Socket-CAN driver for the MSCAN controller
4 available on some Freescale processor chips like the PowerPC MPC5200,
5 MPC512x or the m86k MC68HC12. It also includes an OF platform driver
8 Signed-off-by: Wolfgang Grandegger <wg@grandegger.com>
10 drivers/net/can/Kconfig | 20 +
11 drivers/net/can/Makefile | 1
12 drivers/net/can/mscan/Makefile | 9
13 drivers/net/can/mscan/mpc52xx_can.c | 293 +++++++++++++++
14 drivers/net/can/mscan/mscan.c | 681 ++++++++++++++++++++++++++++++++++++
15 drivers/net/can/mscan/mscan.h | 237 ++++++++++++
16 6 files changed, 1241 insertions(+)
18 Index: net-next-2.6/drivers/net/can/Kconfig
19 ===================================================================
20 --- net-next-2.6.orig/drivers/net/can/Kconfig
21 +++ net-next-2.6/drivers/net/can/Kconfig
22 @@ -70,4 +70,24 @@ config CAN_KVASER_PCI
23 This driver is for the the PCIcanx and PCIcan cards (1, 2 or
24 4 channel) from Kvaser (http://www.kvaser.com).
27 + depends on CAN_DEV && (PPC || M68K || M68KNOMMU)
28 + tristate "Freescale MSCAN based chips"
30 + The Motorola Scalable Controller Area Network (MSCAN) definition
31 + is based on the MSCAN12 definition which is the specific
32 + implementation of the Motorola Scalable CAN concept targeted for
33 + the Motorola MC68HC12 Microcontroller Family.
36 + tristate "Freescale MPC5200 onboard CAN controller"
37 + depends on CAN_MSCAN && PPC_MPC52xx
40 + If you say yes here you get support for Freescale MPC5200
41 + onboard dualCAN controller.
43 + This driver can also be built as a module. If so, the module
44 + will be called mpc52xx_can.
47 Index: net-next-2.6/drivers/net/can/Makefile
48 ===================================================================
49 --- net-next-2.6.orig/drivers/net/can/Makefile
50 +++ net-next-2.6/drivers/net/can/Makefile
51 @@ -8,5 +8,6 @@ obj-$(CONFIG_CAN_DEV) += can-dev.o
52 can-dev-y := dev.o sysfs.o
54 obj-$(CONFIG_CAN_SJA1000) += sja1000/
55 +obj-$(CONFIG_CAN_MSCAN) += mscan/
57 ccflags-$(CONFIG_CAN_DEBUG_DEVICES) := -DDEBUG
58 Index: net-next-2.6/drivers/net/can/mscan/mpc52xx_can.c
59 ===================================================================
61 +++ net-next-2.6/drivers/net/can/mscan/mpc52xx_can.c
64 + * CAN bus driver for the Freescale MPC52xx embedded CPU.
66 + * Copyright (C) 2004-2005 Andrey Volkov <avolkov@varma-el.com>,
67 + * Varma Electronics Oy
68 + * Copyright (C) 2008-2009 Wolfgang Grandegger <wg@grandegger.com>
70 + * This program is free software; you can redistribute it and/or modify
71 + * it under the terms of the version 2 of the GNU General Public License
72 + * as published by the Free Software Foundation
74 + * This program is distributed in the hope that it will be useful, but
75 + * WITHOUT ANY WARRANTY; without even the implied warranty of
76 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
77 + * GNU General Public License for more details.
79 + * You should have received a copy of the GNU General Public License
80 + * along with this program; if not, write to the Free Software
81 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
84 +#include <linux/kernel.h>
85 +#include <linux/module.h>
86 +#include <linux/interrupt.h>
87 +#include <linux/platform_device.h>
88 +#include <linux/netdevice.h>
89 +#include <linux/can.h>
90 +#include <linux/can/dev.h>
91 +#include <linux/of_platform.h>
92 +#include <sysdev/fsl_soc.h>
93 +#include <linux/io.h>
94 +#include <asm/mpc52xx.h>
99 +#define DRV_NAME "mpc52xx_can"
101 +static struct of_device_id mpc52xx_cdm_ids[] __devinitdata = {
102 + { .compatible = "fsl,mpc5200-cdm", },
103 + { .compatible = "fsl,mpc5200b-cdm", },
108 + * Get the frequency of the external oscillator clock connected
109 + * to the SYS_XTAL_IN pin, or retrun 0 if it cannot be determined.
111 +static unsigned int __devinit mpc52xx_can_xtal_freq(struct device_node *np)
113 + struct mpc52xx_cdm __iomem *cdm;
114 + struct device_node *np_cdm;
118 + freq = mpc52xx_find_ipb_freq(np);
123 + * Detemine SYS_XTAL_IN frequency from the clock domain settings
125 + np_cdm = of_find_matching_node(NULL, mpc52xx_cdm_ids);
126 + cdm = of_iomap(np_cdm, 0);
127 + of_node_put(np_cdm);
129 + printk(KERN_ERR "%s() failed abnormally\n", __func__);
133 + if (in_8(&cdm->ipb_clk_sel) & 0x1)
135 + val = in_be32(&cdm->rstcfg);
136 + if (val & (1 << 5))
140 + if (val & (1 << 6))
151 + * Get frequency of the MSCAN clock source
153 + * Either the oscillator clock (SYS_XTAL_IN) or the IP bus clock (IP_CLK)
154 + * can be selected. According to the MPC5200 user's manual, the oscillator
155 + * clock is the better choice as it has less jitter but due to a hardware
156 + * bug, it can not be selected for the old MPC5200 Rev. A chips.
159 +static unsigned int __devinit mpc52xx_can_clock_freq(struct device_node *np,
164 + pvr = mfspr(SPRN_PVR);
166 + if (clock_src == MSCAN_CLKSRC_BUS || pvr == 0x80822011)
167 + return mpc52xx_find_ipb_freq(np);
169 + return mpc52xx_can_xtal_freq(np);
172 +static int __devinit mpc52xx_can_probe(struct of_device *ofdev,
173 + const struct of_device_id *id)
175 + struct device_node *np = ofdev->node;
176 + struct net_device *dev;
177 + struct can_priv *priv;
178 + struct resource res;
179 + void __iomem *base;
180 + int err, irq, res_size, clock_src;
182 + err = of_address_to_resource(np, 0, &res);
184 + dev_err(&ofdev->dev, "invalid address\n");
188 + res_size = res.end - res.start + 1;
190 + if (!request_mem_region(res.start, res_size, DRV_NAME)) {
191 + dev_err(&ofdev->dev, "couldn't request %#x..%#x\n",
192 + res.start, res.end);
196 + base = ioremap_nocache(res.start, res_size);
198 + dev_err(&ofdev->dev, "couldn't ioremap %#x..%#x\n",
199 + res.start, res.end);
201 + goto exit_release_mem;
204 + irq = irq_of_parse_and_map(np, 0);
205 + if (irq == NO_IRQ) {
206 + dev_err(&ofdev->dev, "no irq found\n");
208 + goto exit_unmap_mem;
211 + dev = alloc_mscandev();
214 + goto exit_dispose_irq;
217 + dev->base_addr = (unsigned long)base;
220 + priv = netdev_priv(dev);
223 + * Either the oscillator clock (SYS_XTAL_IN) or the IP bus clock
224 + * (IP_CLK) can be selected as MSCAN clock source. According to
225 + * the MPC5200 user's manual, the oscillator clock is the better
226 + * choice as it has less jitter. For this reason, it is selected
229 + if (of_get_property(np, "clock-ipb", NULL))
230 + clock_src = MSCAN_CLKSRC_BUS;
232 + clock_src = MSCAN_CLKSRC_XTAL;
233 + priv->bittiming.clock = mpc52xx_can_clock_freq(np, clock_src);
234 + if (!priv->bittiming.clock) {
235 + dev_err(&ofdev->dev, "couldn't get MSCAN clock frequency\n");
237 + goto exit_free_mscan;
240 + SET_NETDEV_DEV(dev, &ofdev->dev);
242 + err = register_mscandev(dev, clock_src);
244 + dev_err(&ofdev->dev, "registering %s failed (err=%d)\n",
246 + goto exit_free_mscan;
249 + dev_set_drvdata(&ofdev->dev, dev);
251 + dev_info(&ofdev->dev, "MSCAN at 0x%lx, irq %d, clock %dHZ\n",
252 + dev->base_addr, dev->irq, priv->bittiming.clock);
259 + irq_dispose_mapping(irq);
263 + release_mem_region(res.start, res_size);
268 +static int __devexit mpc52xx_can_remove(struct of_device *ofdev)
270 + struct net_device *dev = dev_get_drvdata(&ofdev->dev);
271 + struct device_node *np = ofdev->node;
272 + struct resource res;
274 + dev_set_drvdata(&ofdev->dev, NULL);
276 + unregister_mscandev(dev);
277 + iounmap((void __iomem *)dev->base_addr);
278 + irq_dispose_mapping(dev->irq);
281 + of_address_to_resource(np, 0, &res);
282 + release_mem_region(res.start, res.end - res.start + 1);
287 +static struct mscan_regs saved_regs;
288 +static int mpc52xx_can_suspend(struct of_device *ofdev, pm_message_t state)
290 + struct net_device *dev = dev_get_drvdata(&ofdev->dev);
291 + struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
293 + _memcpy_fromio(&saved_regs, regs, sizeof(*regs));
298 +static int mpc52xx_can_resume(struct of_device *ofdev)
300 + struct net_device *dev = dev_get_drvdata(&ofdev->dev);
301 + struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
303 + regs->canctl0 |= MSCAN_INITRQ;
304 + while ((regs->canctl1 & MSCAN_INITAK) == 0)
307 + regs->canctl1 = saved_regs.canctl1;
308 + regs->canbtr0 = saved_regs.canbtr0;
309 + regs->canbtr1 = saved_regs.canbtr1;
310 + regs->canidac = saved_regs.canidac;
312 + /* restore masks, buffers etc. */
313 + _memcpy_toio(®s->canidar1_0, (void *)&saved_regs.canidar1_0,
314 + sizeof(*regs) - offsetof(struct mscan_regs, canidar1_0));
316 + regs->canctl0 &= ~MSCAN_INITRQ;
317 + regs->cantbsel = saved_regs.cantbsel;
318 + regs->canrier = saved_regs.canrier;
319 + regs->cantier = saved_regs.cantier;
320 + regs->canctl0 = saved_regs.canctl0;
325 +static struct of_device_id __devinitdata mpc52xx_can_table[] = {
326 + {.compatible = "fsl,mpc5200-mscan"},
327 + {.compatible = "fsl,mpc5200b-mscan"},
331 +static struct of_platform_driver mpc52xx_can_driver = {
332 + .owner = THIS_MODULE,
333 + .name = "mpc52xx_can",
334 + .probe = mpc52xx_can_probe,
335 + .remove = __devexit_p(mpc52xx_can_remove),
336 + .suspend = mpc52xx_can_suspend,
337 + .resume = mpc52xx_can_resume,
338 + .match_table = mpc52xx_can_table,
341 +static int __init mpc52xx_can_init(void)
343 + return of_register_platform_driver(&mpc52xx_can_driver);
345 +module_init(mpc52xx_can_init);
347 +static void __exit mpc52xx_can_exit(void)
349 + return of_unregister_platform_driver(&mpc52xx_can_driver);
351 +module_exit(mpc52xx_can_exit);
353 +MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
354 +MODULE_DESCRIPTION("Freescale MPC5200 CAN driver");
355 +MODULE_LICENSE("GPL v2");
356 Index: net-next-2.6/drivers/net/can/mscan/mscan.c
357 ===================================================================
359 +++ net-next-2.6/drivers/net/can/mscan/mscan.c
362 + * CAN bus driver for the alone generic (as possible as) MSCAN controller.
364 + * Copyright (C) 2005-2006 Andrey Volkov <avolkov@varma-el.com>,
365 + * Varma Electronics Oy
366 + * Copyright (C) 2008-2009 Wolfgang Grandegger <wg@grandegger.com>
368 + * This program is free software; you can redistribute it and/or modify
369 + * it under the terms of the version 2 of the GNU General Public License
370 + * as published by the Free Software Foundation
372 + * This program is distributed in the hope that it will be useful,
373 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
374 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
375 + * GNU General Public License for more details.
377 + * You should have received a copy of the GNU General Public License
378 + * along with this program; if not, write to the Free Software
379 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
382 +#include <linux/kernel.h>
383 +#include <linux/module.h>
384 +#include <linux/interrupt.h>
385 +#include <linux/delay.h>
386 +#include <linux/netdevice.h>
387 +#include <linux/if_arp.h>
388 +#include <linux/if_ether.h>
389 +#include <linux/list.h>
390 +#include <linux/can.h>
391 +#include <linux/can/dev.h>
392 +#include <linux/can/error.h>
393 +#include <linux/io.h>
397 +#define MSCAN_NORMAL_MODE 0
398 +#define MSCAN_SLEEP_MODE MSCAN_SLPRQ
399 +#define MSCAN_INIT_MODE (MSCAN_INITRQ | MSCAN_SLPRQ)
400 +#define MSCAN_POWEROFF_MODE (MSCAN_CSWAI | MSCAN_SLPRQ)
401 +#define MSCAN_SET_MODE_RETRIES 255
403 +#define BTR0_BRP_MASK 0x3f
404 +#define BTR0_SJW_SHIFT 6
405 +#define BTR0_SJW_MASK (0x3 << BTR0_SJW_SHIFT)
407 +#define BTR1_TSEG1_MASK 0xf
408 +#define BTR1_TSEG2_SHIFT 4
409 +#define BTR1_TSEG2_MASK (0x7 << BTR1_TSEG2_SHIFT)
410 +#define BTR1_SAM_SHIFT 7
412 +#define BTR0_SET_BRP(brp) (((brp) - 1) & BTR0_BRP_MASK)
413 +#define BTR0_SET_SJW(sjw) ((((sjw) - 1) << BTR0_SJW_SHIFT) & \
416 +#define BTR1_SET_TSEG1(tseg1) (((tseg1) - 1) & BTR1_TSEG1_MASK)
417 +#define BTR1_SET_TSEG2(tseg2) ((((tseg2) - 1) << BTR1_TSEG2_SHIFT) & \
419 +#define BTR1_SET_SAM(sam) (((sam) & 1) << BTR1_SAM_SHIFT)
421 +static struct can_bittiming_const mscan_bittiming_const = {
432 +struct mscan_state {
438 +#define TX_QUEUE_SIZE 3
440 +struct tx_queue_entry {
441 + struct list_head list;
447 + struct can_priv can;
449 + unsigned long flags;
455 + struct list_head tx_head;
456 + struct tx_queue_entry tx_queue[TX_QUEUE_SIZE];
457 + struct napi_struct napi;
460 +#define F_RX_PROGRESS 0
461 +#define F_TX_PROGRESS 1
462 +#define F_TX_WAIT_ALL 2
464 +static enum can_state state_map[] = {
466 + CAN_STATE_BUS_WARNING,
467 + CAN_STATE_BUS_PASSIVE,
471 +static int mscan_set_mode(struct net_device *dev, u8 mode)
473 + struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
474 + struct mscan_priv *priv = netdev_priv(dev);
479 + if (mode != MSCAN_NORMAL_MODE) {
481 + if (priv->tx_active) {
482 + /* Abort transfers before going to sleep */
483 + out_8(®s->cantier, 0);
484 + out_8(®s->cantarq, priv->tx_active);
485 + out_8(®s->cantier, priv->tx_active);
488 + canctl1 = in_8(®s->canctl1);
489 + if ((mode & MSCAN_SLPRQ) && (canctl1 & MSCAN_SLPAK) == 0) {
490 + out_8(®s->canctl0,
491 + in_8(®s->canctl0) | MSCAN_SLPRQ);
492 + for (i = 0; i < MSCAN_SET_MODE_RETRIES; i++) {
493 + if (in_8(®s->canctl1) & MSCAN_SLPAK)
497 + if (i >= MSCAN_SET_MODE_RETRIES)
501 + priv->can.state = CAN_STATE_SLEEPING;
503 + if (!ret && (mode & MSCAN_INITRQ)
504 + && (canctl1 & MSCAN_INITAK) == 0) {
505 + out_8(®s->canctl0,
506 + in_8(®s->canctl0) | MSCAN_INITRQ);
507 + for (i = 0; i < MSCAN_SET_MODE_RETRIES; i++) {
508 + if (in_8(®s->canctl1) & MSCAN_INITAK)
511 + if (i >= MSCAN_SET_MODE_RETRIES)
515 + priv->can.state = CAN_STATE_STOPPED;
517 + if (!ret && (mode & MSCAN_CSWAI))
518 + out_8(®s->canctl0,
519 + in_8(®s->canctl0) | MSCAN_CSWAI);
522 + canctl1 = in_8(®s->canctl1);
523 + if (canctl1 & (MSCAN_SLPAK | MSCAN_INITAK)) {
524 + out_8(®s->canctl0, in_8(®s->canctl0) &
525 + ~(MSCAN_SLPRQ | MSCAN_INITRQ));
526 + for (i = 0; i < MSCAN_SET_MODE_RETRIES; i++) {
527 + canctl1 = in_8(®s->canctl1);
528 + if (!(canctl1 & (MSCAN_INITAK | MSCAN_SLPAK)))
531 + if (i >= MSCAN_SET_MODE_RETRIES)
534 + priv->can.state = CAN_STATE_ACTIVE;
540 +static int mscan_start(struct net_device *dev)
542 + struct mscan_priv *priv = netdev_priv(dev);
543 + struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
547 + out_8(®s->canrier, 0);
549 + INIT_LIST_HEAD(&priv->tx_head);
551 + priv->tx_active = 0;
552 + priv->shadow_canrier = 0;
555 + err = mscan_set_mode(dev, MSCAN_NORMAL_MODE);
559 + canrflg = in_8(®s->canrflg);
560 + priv->shadow_statflg = canrflg & MSCAN_STAT_MSK;
561 + priv->can.state = state_map[max(MSCAN_STATE_RX(canrflg),
562 + MSCAN_STATE_TX(canrflg))];
563 + out_8(®s->cantier, 0);
565 + /* Enable receive interrupts. */
566 + out_8(®s->canrier, MSCAN_OVRIE | MSCAN_RXFIE | MSCAN_CSCIE |
567 + MSCAN_RSTATE1 | MSCAN_RSTATE0 | MSCAN_TSTATE1 | MSCAN_TSTATE0);
572 +static int mscan_start_xmit(struct sk_buff *skb, struct net_device *dev)
574 + struct can_frame *frame = (struct can_frame *)skb->data;
575 + struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
576 + struct mscan_priv *priv = netdev_priv(dev);
577 + int i, rtr, buf_id;
580 + if (frame->can_dlc > 8)
583 + out_8(®s->cantier, 0);
585 + i = ~priv->tx_active & MSCAN_TXE;
586 + buf_id = ffs(i) - 1;
587 + switch (hweight8(i)) {
589 + netif_stop_queue(dev);
590 + dev_err(ND2D(dev), "BUG! Tx Ring full when queue awake!\n");
591 + return NETDEV_TX_BUSY;
593 + /* if buf_id < 3, then current frame will be send out of order,
594 + since buffer with lower id have higher priority (hell..) */
597 + if (priv->cur_pri == 0xff)
598 + set_bit(F_TX_WAIT_ALL, &priv->flags);
599 + netif_stop_queue(dev);
601 + set_bit(F_TX_PROGRESS, &priv->flags);
603 + out_8(®s->cantbsel, i);
605 + rtr = frame->can_id & CAN_RTR_FLAG;
607 + if (frame->can_id & CAN_EFF_FLAG) {
608 + can_id = (frame->can_id & CAN_EFF_MASK) << 1;
611 + out_be16(®s->tx.idr3_2, can_id);
614 + can_id = (can_id & 0x7) | ((can_id << 2) & 0xffe0) | (3 << 3);
616 + can_id = (frame->can_id & CAN_SFF_MASK) << 5;
620 + out_be16(®s->tx.idr1_0, can_id);
623 + void __iomem *data = ®s->tx.dsr1_0;
624 + u16 *payload = (u16 *) frame->data;
625 + /*Its safe to write into dsr[dlc+1] */
626 + for (i = 0; i < (frame->can_dlc + 1) / 2; i++) {
627 + out_be16(data, *payload++);
628 + data += 2 + _MSCAN_RESERVED_DSR_SIZE;
632 + out_8(®s->tx.dlr, frame->can_dlc);
633 + out_8(®s->tx.tbpr, priv->cur_pri);
635 + /* Start transmission. */
636 + out_8(®s->cantflg, 1 << buf_id);
638 + if (!test_bit(F_TX_PROGRESS, &priv->flags))
639 + dev->trans_start = jiffies;
641 + list_add_tail(&priv->tx_queue[buf_id].list, &priv->tx_head);
643 + can_put_echo_skb(skb, dev, buf_id);
645 + /* Enable interrupt. */
646 + priv->tx_active |= 1 << buf_id;
647 + out_8(®s->cantier, priv->tx_active);
649 + return NETDEV_TX_OK;
652 +static inline int check_set_state(struct net_device *dev, u8 canrflg)
654 + struct mscan_priv *priv = netdev_priv(dev);
655 + enum can_state state;
658 + if (!(canrflg & MSCAN_CSCIF) || priv->can.state > CAN_STATE_BUS_OFF)
661 + state = state_map[max(MSCAN_STATE_RX(canrflg),
662 + MSCAN_STATE_TX(canrflg))];
663 + if (priv->can.state < state)
665 + if (state == CAN_STATE_BUS_OFF)
667 + priv->can.state = state;
671 +static int mscan_rx_poll(struct napi_struct *napi, int quota)
673 + struct mscan_priv *priv = container_of(napi, struct mscan_priv, napi);
674 + struct net_device *dev = napi->dev;
675 + struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
676 + struct net_device_stats *stats = &dev->stats;
679 + struct sk_buff *skb;
680 + struct can_frame *frame;
685 + while (npackets < quota && ((canrflg = in_8(®s->canrflg)) &
686 + (MSCAN_RXF | MSCAN_ERR_IF))) {
688 + skb = dev_alloc_skb(sizeof(struct can_frame));
690 + if (printk_ratelimit())
691 + dev_notice(ND2D(dev), "packet dropped\n");
692 + stats->rx_dropped++;
693 + out_8(®s->canrflg, canrflg);
697 + frame = (struct can_frame *)skb_put(skb, sizeof(*frame));
698 + memset(frame, 0, sizeof(*frame));
700 + if (canrflg & MSCAN_RXF) {
701 + can_id = in_be16(®s->rx.idr1_0);
702 + if (can_id & (1 << 3)) {
703 + frame->can_id = CAN_EFF_FLAG;
704 + can_id = ((can_id << 16) |
705 + in_be16(®s->rx.idr3_2));
706 + can_id = ((can_id & 0xffe00000) |
707 + ((can_id & 0x7ffff) << 2)) >> 2;
713 + frame->can_id |= can_id >> 1;
715 + frame->can_id |= CAN_RTR_FLAG;
716 + frame->can_dlc = in_8(®s->rx.dlr) & 0xf;
718 + if (!(frame->can_id & CAN_RTR_FLAG)) {
719 + void __iomem *data = ®s->rx.dsr1_0;
720 + u16 *payload = (u16 *) frame->data;
721 + for (i = 0; i < (frame->can_dlc + 1) / 2; i++) {
722 + *payload++ = in_be16(data);
723 + data += 2 + _MSCAN_RESERVED_DSR_SIZE;
727 + out_8(®s->canrflg, MSCAN_RXF);
728 + dev->last_rx = jiffies;
729 + stats->rx_packets++;
730 + stats->rx_bytes += frame->can_dlc;
731 + } else if (canrflg & MSCAN_ERR_IF) {
732 + dev_dbg(ND2D(dev), "error interrupt (canrflg=%#x)\n",
734 + frame->can_id = CAN_ERR_FLAG;
736 + if (canrflg & MSCAN_OVRIF) {
737 + frame->can_id |= CAN_ERR_CRTL;
738 + frame->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
739 + stats->rx_over_errors++;
741 + frame->data[1] = 0;
743 + if (check_set_state(dev, canrflg)) {
744 + frame->can_id |= CAN_ERR_CRTL;
745 + switch (priv->can.state) {
746 + case CAN_STATE_BUS_WARNING:
747 + if ((priv->shadow_statflg &
749 + (canrflg & MSCAN_RSTAT_MSK))
751 + CAN_ERR_CRTL_RX_WARNING;
753 + if ((priv->shadow_statflg &
755 + (canrflg & MSCAN_TSTAT_MSK))
757 + CAN_ERR_CRTL_TX_WARNING;
759 + case CAN_STATE_BUS_PASSIVE:
761 + CAN_ERR_CRTL_RX_PASSIVE;
763 + case CAN_STATE_BUS_OFF:
764 + frame->can_id |= CAN_ERR_BUSOFF;
765 + frame->can_id &= ~CAN_ERR_CRTL;
771 + priv->shadow_statflg = canrflg & MSCAN_STAT_MSK;
772 + frame->can_dlc = CAN_ERR_DLC;
773 + out_8(®s->canrflg, MSCAN_ERR_IF);
778 + skb->protocol = __constant_htons(ETH_P_CAN);
779 + skb->ip_summed = CHECKSUM_UNNECESSARY;
780 + netif_receive_skb(skb);
783 + if (!(in_8(®s->canrflg) & (MSCAN_RXF | MSCAN_ERR_IF))) {
784 + napi_complete(&priv->napi);
785 + clear_bit(F_RX_PROGRESS, &priv->flags);
786 + if (priv->can.state < CAN_STATE_BUS_OFF)
787 + out_8(®s->canrier, priv->shadow_canrier);
793 +static irqreturn_t mscan_isr(int irq, void *dev_id)
795 + struct net_device *dev = (struct net_device *)dev_id;
796 + struct mscan_priv *priv = netdev_priv(dev);
797 + struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
798 + struct net_device_stats *stats = &dev->stats;
799 + u8 cantier, cantflg, canrflg;
800 + irqreturn_t ret = IRQ_NONE;
802 + cantier = in_8(®s->cantier) & MSCAN_TXE;
803 + cantflg = in_8(®s->cantflg) & cantier;
805 + if (cantier && cantflg) {
807 + struct list_head *tmp, *pos;
809 + list_for_each_safe(pos, tmp, &priv->tx_head) {
810 + struct tx_queue_entry *entry =
811 + list_entry(pos, struct tx_queue_entry, list);
812 + u8 mask = entry->mask;
814 + if (!(cantflg & mask))
817 + out_8(®s->cantbsel, mask);
818 + stats->tx_bytes += in_8(®s->tx.dlr);
819 + stats->tx_packets++;
820 + can_get_echo_skb(dev, entry->id);
821 + priv->tx_active &= ~mask;
825 + if (list_empty(&priv->tx_head)) {
826 + clear_bit(F_TX_WAIT_ALL, &priv->flags);
827 + clear_bit(F_TX_PROGRESS, &priv->flags);
830 + dev->trans_start = jiffies;
832 + if (!test_bit(F_TX_WAIT_ALL, &priv->flags))
833 + netif_wake_queue(dev);
835 + out_8(®s->cantier, priv->tx_active);
839 + canrflg = in_8(®s->canrflg);
840 + if ((canrflg & ~MSCAN_STAT_MSK) &&
841 + !test_and_set_bit(F_RX_PROGRESS, &priv->flags)) {
842 + if (canrflg & ~MSCAN_STAT_MSK) {
843 + priv->shadow_canrier = in_8(®s->canrier);
844 + out_8(®s->canrier, 0);
845 + napi_schedule(&priv->napi);
848 + clear_bit(F_RX_PROGRESS, &priv->flags);
853 +static int mscan_do_set_mode(struct net_device *dev, enum can_mode mode)
856 + struct mscan_priv *priv = netdev_priv(dev);
859 + if (!priv->open_time)
863 + case CAN_MODE_SLEEP:
864 + case CAN_MODE_STOP:
865 + netif_stop_queue(dev);
866 + mscan_set_mode(dev,
868 + CAN_MODE_STOP) ? MSCAN_INIT_MODE :
871 + case CAN_MODE_START:
872 + if (priv->can.state <= CAN_STATE_BUS_OFF)
873 + mscan_set_mode(dev, MSCAN_INIT_MODE);
874 + ret = mscan_start(dev);
877 + if (netif_queue_stopped(dev))
878 + netif_wake_queue(dev);
888 +static int mscan_do_set_bittiming(struct net_device *dev)
890 + struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
891 + struct mscan_priv *priv = netdev_priv(dev);
892 + struct can_bittiming *bt = &priv->can.bittiming;
895 + btr0 = BTR0_SET_BRP(bt->brp) | BTR0_SET_SJW(bt->sjw);
896 + btr1 = (BTR1_SET_TSEG1(bt->prop_seg + bt->phase_seg1) |
897 + BTR1_SET_TSEG2(bt->phase_seg2) |
898 + BTR1_SET_SAM(priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES));
900 + dev_info(ND2D(dev), "setting BTR0=0x%02x BTR1=0x%02x\n", btr0, btr1);
902 + out_8(®s->canbtr0, btr0);
903 + out_8(®s->canbtr1, btr1);
908 +static int mscan_open(struct net_device *dev)
911 + struct mscan_priv *priv = netdev_priv(dev);
912 + struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
914 + /* determine and set bittime */
915 + ret = can_set_bittiming(dev);
919 + napi_enable(&priv->napi);
920 + ret = request_irq(dev->irq, mscan_isr, 0, dev->name, dev);
923 + napi_disable(&priv->napi);
924 + printk(KERN_ERR "%s - failed to attach interrupt\n",
929 + priv->open_time = jiffies;
931 + out_8(®s->canctl1, in_8(®s->canctl1) & ~MSCAN_LISTEN);
933 + ret = mscan_start(dev);
937 + netif_start_queue(dev);
942 +static int mscan_close(struct net_device *dev)
944 + struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
945 + struct mscan_priv *priv = netdev_priv(dev);
947 + napi_disable(&priv->napi);
949 + out_8(®s->cantier, 0);
950 + out_8(®s->canrier, 0);
951 + free_irq(dev->irq, dev);
952 + mscan_set_mode(dev, MSCAN_INIT_MODE);
953 + can_close_cleanup(dev);
954 + netif_stop_queue(dev);
955 + priv->open_time = 0;
960 +static const struct net_device_ops mscan_netdev_ops = {
961 + .ndo_open = mscan_open,
962 + .ndo_stop = mscan_close,
963 + .ndo_start_xmit = mscan_start_xmit,
966 +int register_mscandev(struct net_device *dev, int clock_src)
968 + struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
971 + ctl1 = in_8(®s->canctl1);
973 + ctl1 |= MSCAN_CLKSRC;
975 + ctl1 &= ~MSCAN_CLKSRC;
977 + ctl1 |= MSCAN_CANE;
978 + out_8(®s->canctl1, ctl1);
981 + /* acceptance mask/acceptance code (accept everything) */
982 + out_be16(®s->canidar1_0, 0);
983 + out_be16(®s->canidar3_2, 0);
984 + out_be16(®s->canidar5_4, 0);
985 + out_be16(®s->canidar7_6, 0);
987 + out_be16(®s->canidmr1_0, 0xffff);
988 + out_be16(®s->canidmr3_2, 0xffff);
989 + out_be16(®s->canidmr5_4, 0xffff);
990 + out_be16(®s->canidmr7_6, 0xffff);
991 + /* Two 32 bit Acceptance Filters */
992 + out_8(®s->canidac, MSCAN_AF_32BIT);
994 + mscan_set_mode(dev, MSCAN_INIT_MODE);
996 + return register_candev(dev);
998 +EXPORT_SYMBOL(register_mscandev);
1000 +void unregister_mscandev(struct net_device *dev)
1002 + struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
1003 + mscan_set_mode(dev, MSCAN_INIT_MODE);
1004 + out_8(®s->canctl1, in_8(®s->canctl1) & ~MSCAN_CANE);
1005 + unregister_candev(dev);
1007 +EXPORT_SYMBOL(unregister_mscandev);
1009 +struct net_device *alloc_mscandev(void)
1011 + struct net_device *dev;
1012 + struct mscan_priv *priv;
1015 + dev = alloc_candev(sizeof(struct mscan_priv));
1018 + priv = netdev_priv(dev);
1020 + dev->netdev_ops = &mscan_netdev_ops;
1022 + dev->flags |= IFF_ECHO; /* we support local echo */
1024 + netif_napi_add(dev, &priv->napi, mscan_rx_poll, 8);
1026 + priv->can.bittiming_const = &mscan_bittiming_const;
1027 + priv->can.do_set_bittiming = mscan_do_set_bittiming;
1028 + priv->can.do_set_mode = mscan_do_set_mode;
1030 + for (i = 0; i < TX_QUEUE_SIZE; i++) {
1031 + priv->tx_queue[i].id = i;
1032 + priv->tx_queue[i].mask = 1 << i;
1037 +EXPORT_SYMBOL(alloc_mscandev);
1039 +MODULE_AUTHOR("Andrey Volkov <avolkov@varma-el.com>");
1040 +MODULE_LICENSE("GPL v2");
1041 +MODULE_DESCRIPTION("CAN port driver for a MSCAN based chips");
1042 Index: net-next-2.6/drivers/net/can/mscan/mscan.h
1043 ===================================================================
1045 +++ net-next-2.6/drivers/net/can/mscan/mscan.h
1048 + * Definitions of consts/structs to drive the Freescale MSCAN.
1050 + * Copyright (C) 2005-2006 Andrey Volkov <avolkov@varma-el.com>,
1051 + * Varma Electronics Oy
1053 + * This program is free software; you can redistribute it and/or modify
1054 + * it under the terms of the version 2 of the GNU General Public License
1055 + * as published by the Free Software Foundation
1057 + * This program is distributed in the hope that it will be useful,
1058 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1059 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1060 + * GNU General Public License for more details.
1062 + * You should have received a copy of the GNU General Public License
1063 + * along with this program; if not, write to the Free Software
1064 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1067 +#ifndef __MSCAN_H__
1068 +#define __MSCAN_H__
1070 +#include <linux/types.h>
1072 +/* MSCAN control register 0 (CANCTL0) bits */
1073 +#define MSCAN_RXFRM 0x80
1074 +#define MSCAN_RXACT 0x40
1075 +#define MSCAN_CSWAI 0x20
1076 +#define MSCAN_SYNCH 0x10
1077 +#define MSCAN_TIME 0x08
1078 +#define MSCAN_WUPE 0x04
1079 +#define MSCAN_SLPRQ 0x02
1080 +#define MSCAN_INITRQ 0x01
1082 +/* MSCAN control register 1 (CANCTL1) bits */
1083 +#define MSCAN_CANE 0x80
1084 +#define MSCAN_CLKSRC 0x40
1085 +#define MSCAN_LOOPB 0x20
1086 +#define MSCAN_LISTEN 0x10
1087 +#define MSCAN_WUPM 0x04
1088 +#define MSCAN_SLPAK 0x02
1089 +#define MSCAN_INITAK 0x01
1091 +/* Use the MPC5200 MSCAN variant? */
1093 +#define MSCAN_FOR_MPC5200
1096 +#ifdef MSCAN_FOR_MPC5200
1097 +#define MSCAN_CLKSRC_BUS 0
1098 +#define MSCAN_CLKSRC_XTAL MSCAN_CLKSRC
1100 +#define MSCAN_CLKSRC_BUS MSCAN_CLKSRC
1101 +#define MSCAN_CLKSRC_XTAL 0
1104 +/* MSCAN receiver flag register (CANRFLG) bits */
1105 +#define MSCAN_WUPIF 0x80
1106 +#define MSCAN_CSCIF 0x40
1107 +#define MSCAN_RSTAT1 0x20
1108 +#define MSCAN_RSTAT0 0x10
1109 +#define MSCAN_TSTAT1 0x08
1110 +#define MSCAN_TSTAT0 0x04
1111 +#define MSCAN_OVRIF 0x02
1112 +#define MSCAN_RXF 0x01
1113 +#define MSCAN_ERR_IF (MSCAN_OVRIF | MSCAN_CSCIF)
1114 +#define MSCAN_RSTAT_MSK (MSCAN_RSTAT1 | MSCAN_RSTAT0)
1115 +#define MSCAN_TSTAT_MSK (MSCAN_TSTAT1 | MSCAN_TSTAT0)
1116 +#define MSCAN_STAT_MSK (MSCAN_RSTAT_MSK | MSCAN_TSTAT_MSK)
1118 +#define MSCAN_STATE_BUS_OFF (MSCAN_RSTAT1 | MSCAN_RSTAT0 | \
1119 + MSCAN_TSTAT1 | MSCAN_TSTAT0)
1120 +#define MSCAN_STATE_TX(canrflg) (((canrflg)&MSCAN_TSTAT_MSK)>>2)
1121 +#define MSCAN_STATE_RX(canrflg) (((canrflg)&MSCAN_RSTAT_MSK)>>4)
1122 +#define MSCAN_STATE_ACTIVE 0
1123 +#define MSCAN_STATE_WARNING 1
1124 +#define MSCAN_STATE_PASSIVE 2
1125 +#define MSCAN_STATE_BUSOFF 3
1127 +/* MSCAN receiver interrupt enable register (CANRIER) bits */
1128 +#define MSCAN_WUPIE 0x80
1129 +#define MSCAN_CSCIE 0x40
1130 +#define MSCAN_RSTATE1 0x20
1131 +#define MSCAN_RSTATE0 0x10
1132 +#define MSCAN_TSTATE1 0x08
1133 +#define MSCAN_TSTATE0 0x04
1134 +#define MSCAN_OVRIE 0x02
1135 +#define MSCAN_RXFIE 0x01
1137 +/* MSCAN transmitter flag register (CANTFLG) bits */
1138 +#define MSCAN_TXE2 0x04
1139 +#define MSCAN_TXE1 0x02
1140 +#define MSCAN_TXE0 0x01
1141 +#define MSCAN_TXE (MSCAN_TXE2 | MSCAN_TXE1 | MSCAN_TXE0)
1143 +/* MSCAN transmitter interrupt enable register (CANTIER) bits */
1144 +#define MSCAN_TXIE2 0x04
1145 +#define MSCAN_TXIE1 0x02
1146 +#define MSCAN_TXIE0 0x01
1147 +#define MSCAN_TXIE (MSCAN_TXIE2 | MSCAN_TXIE1 | MSCAN_TXIE0)
1149 +/* MSCAN transmitter message abort request (CANTARQ) bits */
1150 +#define MSCAN_ABTRQ2 0x04
1151 +#define MSCAN_ABTRQ1 0x02
1152 +#define MSCAN_ABTRQ0 0x01
1154 +/* MSCAN transmitter message abort ack (CANTAAK) bits */
1155 +#define MSCAN_ABTAK2 0x04
1156 +#define MSCAN_ABTAK1 0x02
1157 +#define MSCAN_ABTAK0 0x01
1159 +/* MSCAN transmit buffer selection (CANTBSEL) bits */
1160 +#define MSCAN_TX2 0x04
1161 +#define MSCAN_TX1 0x02
1162 +#define MSCAN_TX0 0x01
1164 +/* MSCAN ID acceptance control register (CANIDAC) bits */
1165 +#define MSCAN_IDAM1 0x20
1166 +#define MSCAN_IDAM0 0x10
1167 +#define MSCAN_IDHIT2 0x04
1168 +#define MSCAN_IDHIT1 0x02
1169 +#define MSCAN_IDHIT0 0x01
1171 +#define MSCAN_AF_32BIT 0x00
1172 +#define MSCAN_AF_16BIT MSCAN_IDAM0
1173 +#define MSCAN_AF_8BIT MSCAN_IDAM1
1174 +#define MSCAN_AF_CLOSED (MSCAN_IDAM0|MSCAN_IDAM1)
1175 +#define MSCAN_AF_MASK (~(MSCAN_IDAM0|MSCAN_IDAM1))
1177 +/* MSCAN Miscellaneous Register (CANMISC) bits */
1178 +#define MSCAN_BOHOLD 0x01
1180 +#ifdef MSCAN_FOR_MPC5200
1181 +#define _MSCAN_RESERVED_(n, num) u8 _res##n[num]
1182 +#define _MSCAN_RESERVED_DSR_SIZE 2
1184 +#define _MSCAN_RESERVED_(n, num)
1185 +#define _MSCAN_RESERVED_DSR_SIZE 0
1188 +/* Structure of the hardware registers */
1189 +struct mscan_regs {
1190 + /* (see doco S12MSCANV3/D) MPC5200 MSCAN */
1191 + u8 canctl0; /* + 0x00 0x00 */
1192 + u8 canctl1; /* + 0x01 0x01 */
1193 + _MSCAN_RESERVED_(1, 2); /* + 0x02 */
1194 + u8 canbtr0; /* + 0x04 0x02 */
1195 + u8 canbtr1; /* + 0x05 0x03 */
1196 + _MSCAN_RESERVED_(2, 2); /* + 0x06 */
1197 + u8 canrflg; /* + 0x08 0x04 */
1198 + u8 canrier; /* + 0x09 0x05 */
1199 + _MSCAN_RESERVED_(3, 2); /* + 0x0a */
1200 + u8 cantflg; /* + 0x0c 0x06 */
1201 + u8 cantier; /* + 0x0d 0x07 */
1202 + _MSCAN_RESERVED_(4, 2); /* + 0x0e */
1203 + u8 cantarq; /* + 0x10 0x08 */
1204 + u8 cantaak; /* + 0x11 0x09 */
1205 + _MSCAN_RESERVED_(5, 2); /* + 0x12 */
1206 + u8 cantbsel; /* + 0x14 0x0a */
1207 + u8 canidac; /* + 0x15 0x0b */
1208 + u8 reserved; /* + 0x16 0x0c */
1209 + _MSCAN_RESERVED_(6, 5); /* + 0x17 */
1210 +#ifndef MSCAN_FOR_MPC5200
1211 + u8 canmisc; /* 0x0d */
1213 + u8 canrxerr; /* + 0x1c 0x0e */
1214 + u8 cantxerr; /* + 0x1d 0x0f */
1215 + _MSCAN_RESERVED_(7, 2); /* + 0x1e */
1216 + u16 canidar1_0; /* + 0x20 0x10 */
1217 + _MSCAN_RESERVED_(8, 2); /* + 0x22 */
1218 + u16 canidar3_2; /* + 0x24 0x12 */
1219 + _MSCAN_RESERVED_(9, 2); /* + 0x26 */
1220 + u16 canidmr1_0; /* + 0x28 0x14 */
1221 + _MSCAN_RESERVED_(10, 2); /* + 0x2a */
1222 + u16 canidmr3_2; /* + 0x2c 0x16 */
1223 + _MSCAN_RESERVED_(11, 2); /* + 0x2e */
1224 + u16 canidar5_4; /* + 0x30 0x18 */
1225 + _MSCAN_RESERVED_(12, 2); /* + 0x32 */
1226 + u16 canidar7_6; /* + 0x34 0x1a */
1227 + _MSCAN_RESERVED_(13, 2); /* + 0x36 */
1228 + u16 canidmr5_4; /* + 0x38 0x1c */
1229 + _MSCAN_RESERVED_(14, 2); /* + 0x3a */
1230 + u16 canidmr7_6; /* + 0x3c 0x1e */
1231 + _MSCAN_RESERVED_(15, 2); /* + 0x3e */
1233 + u16 idr1_0; /* + 0x40 0x20 */
1234 + _MSCAN_RESERVED_(16, 2); /* + 0x42 */
1235 + u16 idr3_2; /* + 0x44 0x22 */
1236 + _MSCAN_RESERVED_(17, 2); /* + 0x46 */
1237 + u16 dsr1_0; /* + 0x48 0x24 */
1238 + _MSCAN_RESERVED_(18, 2); /* + 0x4a */
1239 + u16 dsr3_2; /* + 0x4c 0x26 */
1240 + _MSCAN_RESERVED_(19, 2); /* + 0x4e */
1241 + u16 dsr5_4; /* + 0x50 0x28 */
1242 + _MSCAN_RESERVED_(20, 2); /* + 0x52 */
1243 + u16 dsr7_6; /* + 0x54 0x2a */
1244 + _MSCAN_RESERVED_(21, 2); /* + 0x56 */
1245 + u8 dlr; /* + 0x58 0x2c */
1246 + u8:8; /* + 0x59 0x2d */
1247 + _MSCAN_RESERVED_(22, 2); /* + 0x5a */
1248 + u16 time; /* + 0x5c 0x2e */
1250 + _MSCAN_RESERVED_(23, 2); /* + 0x5e */
1252 + u16 idr1_0; /* + 0x60 0x30 */
1253 + _MSCAN_RESERVED_(24, 2); /* + 0x62 */
1254 + u16 idr3_2; /* + 0x64 0x32 */
1255 + _MSCAN_RESERVED_(25, 2); /* + 0x66 */
1256 + u16 dsr1_0; /* + 0x68 0x34 */
1257 + _MSCAN_RESERVED_(26, 2); /* + 0x6a */
1258 + u16 dsr3_2; /* + 0x6c 0x36 */
1259 + _MSCAN_RESERVED_(27, 2); /* + 0x6e */
1260 + u16 dsr5_4; /* + 0x70 0x38 */
1261 + _MSCAN_RESERVED_(28, 2); /* + 0x72 */
1262 + u16 dsr7_6; /* + 0x74 0x3a */
1263 + _MSCAN_RESERVED_(29, 2); /* + 0x76 */
1264 + u8 dlr; /* + 0x78 0x3c */
1265 + u8 tbpr; /* + 0x79 0x3d */
1266 + _MSCAN_RESERVED_(30, 2); /* + 0x7a */
1267 + u16 time; /* + 0x7c 0x3e */
1269 + _MSCAN_RESERVED_(31, 2); /* + 0x7e */
1270 +} __attribute__ ((packed));
1272 +#undef _MSCAN_RESERVED_
1273 +#define MSCAN_REGION sizeof(struct mscan)
1275 +struct net_device *alloc_mscandev(void);
1277 + * 1 = The MSCAN clock source is the onchip Bus Clock.
1278 + * 0 = The MSCAN clock source is the chip Oscillator Clock.
1280 +extern int register_mscandev(struct net_device *dev, int clock_src);
1281 +extern void unregister_mscandev(struct net_device *dev);
1283 +#endif /* __MSCAN_H__ */
1284 Index: net-next-2.6/drivers/net/can/mscan/Makefile
1285 ===================================================================
1287 +++ net-next-2.6/drivers/net/can/mscan/Makefile
1290 +# Makefile for the MSCAN controller drivers.
1293 +obj-$(CONFIG_CAN_MPC52XX) += mscan-mpc52xx.o
1295 +mscan-mpc52xx-objs := mscan.o mpc52xx_can.o
1297 +ccflags-$(CONFIG_CAN_DEBUG_DEVICES) := -DDEBUG