1 From c44de58d0972d05851512ba8cbd928b7adef5187 Mon Sep 17 00:00:00 2001
2 From: Kurt Mahan <kmahan@freescale.com>
3 Date: Tue, 8 Jul 2008 17:11:33 -0600
4 Subject: [PATCH] Add FlexCAN support.
6 LTIBName: mcfv4e-flexcan
7 Signed-off-by: Kurt Mahan <kmahan@freescale.com>
8 Signed-off-by: Huan Wang <b18965@freescale.com>
10 drivers/net/can/Kconfig | 13 ++
11 drivers/net/can/Makefile | 1 +
12 drivers/net/can/flexcan/Makefile | 5 +
13 drivers/net/can/flexcan/flexcan.c | 378 +++++++++++++++++++++++++++++++++
14 drivers/net/can/flexcan/flexcan.h | 148 +++++++++++++
15 drivers/net/can/flexcan/mcf548x_can.c | 213 ++++++++++++++++++
16 include/asm-m68k/m5485sim.h | 2 +
17 include/linux/can/dev.h | 62 ++++++
18 include/linux/can/ioctl.h | 152 +++++++++++++
19 include/linux/can/version.h | 22 ++
20 net/can/Makefile | 3 +
21 net/can/dev.c | 292 +++++++++++++++++++++++++
22 12 files changed, 1291 insertions(+), 0 deletions(-)
23 create mode 100644 drivers/net/can/flexcan/Makefile
24 create mode 100644 drivers/net/can/flexcan/flexcan.c
25 create mode 100644 drivers/net/can/flexcan/flexcan.h
26 create mode 100644 drivers/net/can/flexcan/mcf548x_can.c
27 create mode 100644 include/linux/can/dev.h
28 create mode 100644 include/linux/can/ioctl.h
29 create mode 100644 include/linux/can/version.h
30 create mode 100644 net/can/dev.c
32 --- a/drivers/net/can/Kconfig
33 +++ b/drivers/net/can/Kconfig
34 @@ -12,6 +12,19 @@ config CAN_VCAN
35 This driver can also be built as a module. If so, the module
39 + tristate "Support for Freescale FLEXCAN based chips"
40 + depends on CAN && (PPC || M68K || M68KNOMMU)
42 + Say Y here if you want to support for Freescale FlexCAN.
44 +config CAN_MCF547X_8X
45 + tristate "Freescale MCF547X/MCF548X onboard CAN controller"
46 + depends on CAN_FLEXCAN && (M547X || M548X)
48 + Say Y here if you want to support for Freescale MCF547x/MCF548x
49 + onboard dualCAN controller.
51 config CAN_DEBUG_DEVICES
52 bool "CAN devices debugging messages"
54 --- a/drivers/net/can/Makefile
55 +++ b/drivers/net/can/Makefile
59 obj-$(CONFIG_CAN_VCAN) += vcan.o
60 +obj-$(CONFIG_CAN_FLEXCAN) += flexcan/
62 +++ b/drivers/net/can/flexcan/Makefile
65 +obj-$(CONFIG_CAN_MCF547X_8X) += flexcan-mcf548x.o
67 +flexcan-mcf548x-objs := flexcan.o mcf548x_can.o
70 +++ b/drivers/net/can/flexcan/flexcan.c
76 + * CAN bus driver for the alone generic (as possible as) FLEXCAN controller.
79 + * Andrey Volkov <avolkov@varma-el.com>
82 + * 2005-2006, Varma Electronics Oy
85 + * This program is free software; you can redistribute it and/or modify
86 + * it under the terms of the GNU General Public License as published by
87 + * the Free Software Foundation; either version 2 of the License, or
88 + * (at your option) any later version.
90 + * This program is distributed in the hope that it will be useful,
91 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
92 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
93 + * GNU General Public License for more details.
95 + * You should have received a copy of the GNU General Public License
96 + * along with this program; if not, write to the Free Software
97 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
100 + * 2008-06-23 Support for MCF548x's FlexCAN
101 + * Huan, Wang <b18965@freescale.com>
106 +#include <linux/kernel.h>
107 +#include <linux/module.h>
108 +#include <linux/interrupt.h>
109 +#include <linux/delay.h>
110 +#include <linux/netdevice.h>
111 +#include <linux/if_arp.h>
112 +#include <linux/if_ether.h>
113 +#include <linux/can.h>
114 +#include <linux/list.h>
115 +#include <linux/io.h>
117 +#include <linux/can/dev.h>
118 +#include <linux/can/error.h>
119 +#include "flexcan.h"
120 +#include <asm/coldfire.h>
121 +#include <asm/m5485sim.h>
122 +#include <linux/can/version.h> /* for RCSID. Removed by mkpatch script */
125 +struct flexcan_priv {
126 + struct can_priv can;
127 + volatile unsigned long flags;
133 + struct list_head tx_head;
134 + struct napi_struct napi;
135 + struct net_device *dev;
139 +static int flexcan_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
141 + struct can_frame *frame = (struct can_frame *)skb->data;
142 + struct flexcan_regs *regs = (struct flexcan_regs *)dev->base_addr;
145 + u32 can_id, can_ext, tmp, tmp1;
147 + /* Transmission inactive */
148 + regs->cantxfg[txbuf].can_dlc = MB_CNT_CODE(0x08);
150 + can_ext = frame->can_id;
151 + if (can_ext & CAN_EFF_FLAG) {
152 + /* Frame format is extended */
153 + regs->cantxfg[txbuf].can_dlc |= (1 << 21);
154 + regs->cantxfg[txbuf].can_dlc |= (1 << 22);
155 + can_id = frame->can_id & MB_ID_EXT;
156 + if (frame->can_id & CAN_RTR_FLAG)
157 + regs->cantxfg[txbuf].can_dlc |= (1 << 20);
159 + tmp = (can_id & CAN_SFF_MASK) << 18;
160 + tmp1 = can_id >> 11;
161 + can_id = tmp | tmp1;
162 + regs->cantxfg[txbuf].can_id = can_id;
164 + /* Frame format is standard */
165 + can_id = frame->can_id & MB_ID_EXT;
166 + if (frame->can_id & CAN_RTR_FLAG)
167 + regs->cantxfg[txbuf].can_dlc |= (1 << 20);
169 + regs->cantxfg[txbuf].can_id = can_id << 18;
173 + for (i = 0; i < len; i++)
174 + regs->cantxfg[txbuf].data[i] = frame->data[i];
176 + regs->cantxfg[txbuf].can_dlc |= len << 16;
177 + /* Transmission active */
178 + regs->cantxfg[txbuf].can_dlc |= MB_CNT_CODE(0x0c);
180 + return NETDEV_TX_OK;
183 +static void flexcan_tx_timeout(struct net_device *dev)
185 + struct sk_buff *skb;
186 + struct flexcan_regs *regs = (struct flexcan_regs *)dev->base_addr;
187 + struct can_frame *frame;
190 + /* Diable the interrupts */
191 + regs->imask = IMASK_BUFF_DISABLE_ALL;
193 + skb = dev_alloc_skb(sizeof(struct can_frame));
195 + if (printk_ratelimit())
196 + dev_notice(ND2D(dev), "TIMEOUT packet dropped.\n");
199 + frame = (struct can_frame *)skb_put(skb, sizeof(struct can_frame));
201 + frame->can_dlc = length;
204 + skb->protocol = __constant_htons(ETH_P_CAN);
205 + skb->pkt_type = PACKET_BROADCAST;
206 + skb->ip_summed = CHECKSUM_UNNECESSARY;
211 +static irqreturn_t flexcan_isr(int irq, void *dev_id)
213 + struct net_device *dev = (struct net_device *)dev_id;
214 + struct flexcan_regs *regs = (struct flexcan_regs *)dev->base_addr;
215 + struct net_device_stats *stats = dev->get_stats(dev);
216 + struct sk_buff *skb;
217 + struct can_frame *frame;
218 + u32 iflags, oflags;
222 + iflags = regs->iflag;
224 + for (i = 0; i < 16; i++) {
225 + if (iflags & (0x01 << i)) {
226 + struct flexcan_mb *mb = ®s->cantxfg[i];
227 + int ctrl = mb->can_dlc;
228 + int code = (ctrl >> 24) & 0x0f;
229 + int length = (ctrl >> 16) & 0x0f;
232 + if (code < 8 && (length > 0)) {
233 + /* receive frame */
234 + skb = dev_alloc_skb(sizeof(struct can_frame));
236 + dev_notice(ND2D(dev),
237 + "Packets dropped.\n");
239 + frame = (struct can_frame *)skb_put(skb,
240 + sizeof(struct can_frame));
242 + frame->can_id &= 0x0;
243 + frame->can_dlc = length;
244 + tmp1 = mb->can_id & MB_ID_EXT;
245 + if (ctrl & MB_CNT_IDE) {
247 + tmp = (tmp >> 18) & CAN_SFF_MASK;
248 + frame->can_id = (tmp1 << 11) | tmp;
249 + frame->can_id &= CAN_EFF_MASK;
250 + frame->can_id |= CAN_EFF_FLAG;
251 + if (ctrl & MB_CNT_RTR)
252 + frame->can_id |= CAN_RTR_FLAG;
254 + frame->can_id = tmp1 >> 18;
255 + if (ctrl & MB_CNT_RTR)
256 + frame->can_id |= CAN_RTR_FLAG;
259 + for (k = 0; k < 8; k++)
260 + frame->data[k] = mb->data[k];
262 + mb->can_dlc &= MB_CODE_MASK;
263 + mb->can_dlc |= MB_CNT_CODE(0x04);
265 + stats->rx_packets++;
266 + stats->rx_bytes += frame->can_dlc;
268 + skb->protocol = __constant_htons(ETH_P_CAN);
269 + skb->ip_summed = CHECKSUM_UNNECESSARY;
271 + retval = netif_rx(skb);
272 + if (retval == NET_RX_DROP)
273 + dev_notice(ND2D(dev),
274 + "Packets dropped.\n");
276 + /* transmit frame */
277 + mb->can_dlc = MB_CNT_CODE(0x04);
281 + regs->iflag = oflags;
283 + return IRQ_HANDLED;
286 +static int flexcan_do_set_bit_time(struct net_device *dev,
287 + struct can_bittime *bt)
289 + struct flexcan_priv *priv = netdev_priv(dev);
290 + struct flexcan_regs *regs = (struct flexcan_regs *)dev->base_addr;
294 + if (bt->type != CAN_BITTIME_STD)
297 + spin_lock_irq(&priv->can.irq_lock);
299 + reg = CANCTRL_PRESDIV(bt->std.brp) | CANCTRL_PSEG1(bt->std.phase_seg1
300 + - 1) | CANCTRL_PSEG2(bt->std.phase_seg2 - 1);
301 + regs->canctrl &= CANCTRL_BITTIME;
302 + regs->canctrl |= (reg | CANCTRL_SAMP(bt->std.sam) |
303 + CANCTRL_PROPSEG(bt->std.prop_seg - 1));
305 + spin_unlock_irq(&priv->can.irq_lock);
310 +static int flexcan_open(struct net_device *dev)
313 + struct flexcan_regs *regs = (struct flexcan_regs *)dev->base_addr;
315 +#if defined(CONFIG_M547X_8X)
316 + MCF_PAR_TIMER = MCF_PAR_TIMER | 0x28;
317 + MCF_PAR_TIMER = MCF_PAR_TIMER & 0xf8;
318 + MCF_PAR_DSPI = MCF_PAR_DSPI | 0x0a00;
319 + MCF_PAR_FECI2CIRQ = MCF_PAR_FECI2CIRQ | 0x0283;
320 + MCF_PAR_PSCn(2) = MCF_PAR_PSCn(2) & 0x0f;
321 + MCF_PAR_PSCn(2) = MCF_PAR_PSCn(2) | 0x50;
324 + regs->canmcr |= CANMCR_SOFTRST;
325 + regs->canmcr |= CANMCR_MDIS;
328 + if ((regs->canmcr & CANMCR_SOFTRST) != 0x0) {
329 + dev_err(ND2D(dev), "Failed to softreset can module.\n");
333 + /* Enable error and bus off interrupt */
334 + regs->canctrl |= (CANCTRL_RJW(3) | CANCTRL_ERRMSK |
337 + /* Set lowest buffer transmitted first */
338 + regs->canctrl |= CANCTRL_LBUF;
340 + for (i = 0; i < 16; i++) {
341 + regs->cantxfg[i].can_dlc = 0;
342 + regs->cantxfg[i].can_id = 0;
343 + for (j = 0; j < 8; j++)
344 + regs->cantxfg[i].data[j] = 0;
346 + /* Put MB into rx queue */
347 + regs->cantxfg[i].can_dlc = MB_CNT_CODE(0x04);
350 + /* acceptance mask/acceptance code (accept everything) */
351 + regs->rxgmask = 0x00000000;
352 + regs->rx14mask = 0x00000000;
353 + regs->rx15mask = 0x00000000;
354 + /* extended frame */
355 + regs->cantxfg[14].can_dlc |= 0x600000;
356 + /* Enable flexcan module */
357 + regs->canmcr &= ~CANMCR_MDIS;
358 + /* Synchronize with the can bus */
359 + regs->canmcr &= ~CANMCR_HALT;
361 +#if defined(CONFIG_M547X_8X)
362 + for (i = 0; i < 2; i++) {
363 + MCF_ICR(ISC_CANn_MBOR(i)) = 0x33;
364 + MCF_ICR(ISC_CANn_ERR(i)) = 0x33;
365 + MCF_ICR(ISC_CANn_BUSOFF(i)) = 0x33;
368 + ret = request_irq(dev->irq + 64, flexcan_isr, IRQF_DISABLED,
370 + ret = request_irq(dev->irq + 1 + 64, flexcan_isr, IRQF_DISABLED,
372 + ret = request_irq(dev->irq + 2 + 64, flexcan_isr, IRQF_DISABLED,
375 + printk(KERN_ERR "%s - failed to attach interrupt.\n",
381 + /* Enable all interrupts */
382 + regs->imask = IMASK_BUFF_ENABLE_ALL;
383 + netif_start_queue(dev);
387 +static int flexcan_close(struct net_device *dev)
389 + struct flexcan_regs *regs = (struct flexcan_regs *)dev->base_addr;
391 + netif_stop_queue(dev);
393 + /* Disable all interrupts */
394 + regs->imask = IMASK_BUFF_DISABLE_ALL;
395 + free_irq(dev->irq + 64, dev);
396 + free_irq(dev->irq + 1 + 64, dev);
397 + free_irq(dev->irq + 2 + 64, dev);
399 + /* Disable module */
400 + regs->canmcr |= CANMCR_MDIS;
404 +int register_flexcandev(struct net_device *dev, int clock_src)
406 + struct flexcan_regs *regs = (struct flexcan_regs *)dev->base_addr;
408 + regs->canmcr &= ~CANMCR_MDIS;
410 + regs->canmcr |= (CANMCR_FRZ | CANMCR_HALT);
411 + return register_netdev(dev);
413 +EXPORT_SYMBOL(register_flexcandev);
415 +void unregister_flexcandev(struct net_device *dev)
417 + struct flexcan_regs *regs = (struct flexcan_regs *)dev->base_addr;
419 + regs->canmcr |= (CANMCR_FRZ | CANMCR_HALT);
420 + regs->canmcr |= CANMCR_MDIS;
422 + unregister_netdev(dev);
424 +EXPORT_SYMBOL(unregister_flexcandev);
426 +struct net_device *alloc_flexcandev(void)
428 + struct net_device *dev;
429 + struct flexcan_priv *priv;
431 + dev = alloc_candev(sizeof(struct flexcan_priv));
435 + priv = netdev_priv(dev);
437 + dev->open = flexcan_open;
438 + dev->stop = flexcan_close;
439 + dev->hard_start_xmit = flexcan_hard_start_xmit;
440 + dev->tx_timeout = flexcan_tx_timeout;
441 + dev->flags |= IFF_NOARP;
442 + priv->can.do_set_bit_time = flexcan_do_set_bit_time;
445 +EXPORT_SYMBOL(alloc_flexcandev);
447 +MODULE_AUTHOR("Andrey Volkov <avolkov@varma-el.com>");
448 +MODULE_LICENSE("GPL v2");
449 +MODULE_DESCRIPTION("CAN port driver for flexcan based chip");
451 +++ b/drivers/net/can/flexcan/flexcan.h
457 + * Definitions of consts/structs to drive the Freescale FLEXCAN.
461 +#ifndef __FLEXCAN_H__
462 +#define __FLEXCAN_H__
464 +#include <linux/autoconf.h>
465 +#include <linux/types.h>
467 +/* FLEXCAN module configuration register (CANMCR) bits */
468 +#define CANMCR_MDIS 0x80000000
469 +#define CANMCR_FRZ 0x40000000
470 +#define CANMCR_HALT 0x10000000
471 +#define CANMCR_SOFTRST 0x02000000
472 +#define CANMCR_FRZACK 0x01000000
473 +#define CANMCR_SUPV 0x00800000
474 +#define CANMCR_MAXMB(x) ((x)&0x0f)
476 +/* FLEXCAN control register (CANCTRL) bits */
477 +#define CANCTRL_PRESDIV(x) (((x)&0xff)<<24)
478 +#define CANCTRL_RJW(x) (((x)&0x03)<<22)
479 +#define CANCTRL_PSEG1(x) (((x)&0x07)<<19)
480 +#define CANCTRL_PSEG2(x) (((x)&0x07)<<16)
481 +#define CANCTRL_BOFFMSK 0x00008000
482 +#define CANCTRL_ERRMSK 0x00004000
483 +#define CANCTRL_LPB 0x00001000
484 +#define CANCTRL_SAMP(x) (((x)&0x1)<<7)
485 +#define CANCTRL_BOFFREC 0x00000040
486 +#define CANCTRL_TSYNC 0x00000020
487 +#define CANCTRL_LBUF 0x00000010
488 +#define CANCTRL_LOM 0x00000008
489 +#define CANCTRL_PROPSEG(x) ((x)&0x07)
490 +#define CANCTRL_BITTIME 0x00c0d078
492 +/* FLEXCAN error counter register (ERRCNT) bits */
493 +#define ERRCNT_REXECTR(x) (((x)&0xff)<<8)
494 +#define ERRCNT_TXECTR(x) ((x)&0xff)
496 +/* FLEXCAN error and status register (ERRSTAT) bits */
497 +#define ERRSTAT_BITERR(x) (((x)&0x03)<<14)
498 +#define ERRSTAT_ACKERR 0x00002000
499 +#define ERRSTAT_CRCERR 0x00001000
500 +#define ERRSTAT_FRMERR 0x00000800
501 +#define ERRSTAT_STFERR 0x00000400
502 +#define ERRSTAT_TXWRN 0x00000200
503 +#define ERRSTAT_RXWRN 0x00000100
504 +#define ERRSTAT_IDLE 0x00000080
505 +#define ERRSTAT_TXRX 0x00000040
506 +#define ERRSTAT_FLTCONF(x) (((x)&0x03)<<4)
507 +#define ERRSTAT_BOFFINT 0x00000004
508 +#define ERRSTAT_ERRINT 0x00000002
510 +/* FLEXCAN interrupt mask register (IMASK) bits */
511 +#define IMASK_BUF15M 0x8000
512 +#define IMASK_BUF14M 0x4000
513 +#define IMASK_BUF13M 0x2000
514 +#define IMASK_BUF12M 0x1000
515 +#define IMASK_BUF11M 0x0800
516 +#define IMASK_BUF10M 0x0400
517 +#define IMASK_BUF9M 0x0200
518 +#define IMASK_BUF8M 0x0100
519 +#define IMASK_BUF7M 0x0080
520 +#define IMASK_BUF6M 0x0040
521 +#define IMASK_BUF5M 0x0020
522 +#define IMASK_BUF4M 0x0010
523 +#define IMASK_BUF3M 0x0008
524 +#define IMASK_BUF2M 0x0004
525 +#define IMASK_BUF1M 0x0002
526 +#define IMASK_BUF0M 0x0001
527 +#define IMASK_BUFnM(x) (0x1<<(x))
528 +#define IMASK_BUFF_ENABLE_ALL 0xffff
529 +#define IMASK_BUFF_DISABLE_ALL 0x0000
531 +/* FLEXCAN interrupt flag register (IFLAG) bits */
532 +#define IFLAG_BUF15M 0x8000
533 +#define IFLAG_BUF14M 0x4000
534 +#define IFLAG_BUF13M 0x2000
535 +#define IFLAG_BUF12M 0x1000
536 +#define IFLAG_BUF11M 0x0800
537 +#define IFLAG_BUF10M 0x0400
538 +#define IFLAG_BUF9M 0x0200
539 +#define IFLAG_BUF8M 0x0100
540 +#define IFLAG_BUF7M 0x0080
541 +#define IFLAG_BUF6M 0x0040
542 +#define IFLAG_BUF5M 0x0020
543 +#define IFLAG_BUF4M 0x0010
544 +#define IFLAG_BUF3M 0x0008
545 +#define IFLAG_BUF2M 0x0004
546 +#define IFLAG_BUF1M 0x0002
547 +#define IFLAG_BUF0M 0x0001
548 +#define IFLAG_BUFnM(x) (0x1<<(x))
549 +#define IFLAG_BUFF_SET_ALL 0xffff
550 +#define IFLAG_BUFF_DISABLE_ALL 0x0000
552 +/* FLEXCAN message buffers */
553 +#define MB_CNT_CODE(x) (((x)&0x0f)<<24)
554 +#define MB_CNT_SRR 0x00400000
555 +#define MB_CNT_IDE 0x00200000
556 +#define MB_CNT_RTR 0x00100000
557 +#define MB_CNT_LENGTH(x) (((x)&0x0f)<<16)
558 +#define MB_CNT_TIMESTAMP(x) ((x)&0xffff)
560 +#define MB_ID_STD ((0x7ff)<<18)
561 +#define MB_ID_EXT 0x1fffffff
562 +#define MB_CODE_MASK 0xf0ffffff
564 +/* Structure of the message buffer */
571 +/* Structure of the hardware registers */
572 +struct flexcan_regs {
587 + struct flexcan_mb cantxfg[16];
590 +struct flexcan_platform_data {
591 + u8 clock_src; /* FLEXCAN clock source CRIN or SYSCLK */
592 + u32 clock_frq; /* can ref. clock, in Hz */
595 +struct net_device *alloc_flexcandev(void);
597 +extern int register_flexcandev(struct net_device *dev, int clock_src);
598 +extern void unregister_flexcandev(struct net_device *dev);
600 +#endif /* __FLEXCAN_H__ */
602 +++ b/drivers/net/can/flexcan/mcf548x_can.c
606 + * CAN bus driver for the Freescale MCF548x embedded CPU.
609 + * Andrey Volkov <avolkov@varma-el.com>
612 + * 2004-2005, Varma Electronics Oy
615 + * This program is free software; you can redistribute it and/or modify
616 + * it under the terms of the GNU General Public License as published by
617 + * the Free Software Foundation; either version 2 of the License, or
618 + * (at your option) any later version.
620 + * This program is distributed in the hope that it will be useful,
621 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
622 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
623 + * GNU General Public License for more details.
625 + * You should have received a copy of the GNU General Public License
626 + * along with this program; if not, write to the Free Software
627 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
630 + * 2008-06-23 support for MCF548x's FlexCAN
631 + * Huan, Wang <b18965@freescale.com>
632 + * 2005-02-03 created
636 +#include <linux/kernel.h>
637 +#include <linux/module.h>
638 +#include <linux/interrupt.h>
639 +#include <linux/platform_device.h>
640 +#include <linux/netdevice.h>
641 +#include <linux/can.h>
642 +#include <linux/can/dev.h>
643 +#include <linux/io.h>
645 +#include "flexcan.h"
646 +#include <asm/coldfire.h>
647 +#include <asm/m5485sim.h>
648 +#include <linux/can/version.h> /* for RCSID. Removed by mkpatch script */
654 +struct platform_device *pdev[PDEV_MAX];
656 +static int __devinit mcf548x_can_probe(struct platform_device *pdev)
658 + struct resource *mem;
659 + struct net_device *dev;
660 + struct flexcan_platform_data *pdata = pdev->dev.platform_data;
661 + struct can_priv *can;
668 + dev = alloc_flexcandev();
671 + can = netdev_priv(dev);
673 + mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
675 + dev->irq = platform_get_irq(pdev, 0);
676 + if (!mem || !dev->irq)
679 + mem_size = mem->end - mem->start + 1;
680 + if (!request_mem_region(mem->start, mem_size, pdev->dev.driver->name)) {
681 + dev_err(&pdev->dev, "resource unavailable\n");
684 + SET_NETDEV_DEV(dev, &pdev->dev);
686 + dev->base_addr = (unsigned long)ioremap_nocache(mem->start, mem_size);
687 + if (!dev->base_addr) {
688 + dev_err(&pdev->dev, "failed to map can port\n");
692 + can->can_sys_clock = pdata->clock_frq;
693 + platform_set_drvdata(pdev, dev);
694 + ret = register_flexcandev(dev, pdata->clock_src);
696 + dev_info(&pdev->dev, "probe for port 0x%lX done\n",
701 + iounmap((unsigned long *)dev->base_addr);
703 + release_mem_region(mem->start, mem_size);
706 + dev_err(&pdev->dev, "probe failed\n");
710 +static int __devexit mcf548x_can_remove(struct platform_device *pdev)
712 + struct net_device *dev = platform_get_drvdata(pdev);
713 + struct resource *mem;
715 + platform_set_drvdata(pdev, NULL);
716 + unregister_flexcandev(dev);
717 + iounmap((unsigned long *)dev->base_addr);
719 + mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
720 + release_mem_region(mem->start, mem->end - mem->start + 1);
725 +static struct platform_driver mcf548x_can_driver = {
727 + .name = "mcf548x-flexcan",
729 + .probe = mcf548x_can_probe,
730 + .remove = __devexit_p(mcf548x_can_remove),
733 +static struct resource mcf548x_can0_resources[] = {
735 + .start = MCF_MBAR + 0x0000A000,
736 + .end = MCF_MBAR + 0x0000A7FF,
737 + .flags = IORESOURCE_MEM,
742 + .flags = IORESOURCE_IRQ,
746 +static struct resource mcf548x_can1_resources[] = {
748 + .start = MCF_MBAR + 0x0000A800,
749 + .end = MCF_MBAR + 0x0000AFFF,
750 + .flags = IORESOURCE_MEM,
755 + .flags = IORESOURCE_IRQ,
760 +static int __init mcf548x_of_to_pdev(void)
764 + struct flexcan_platform_data pdata;
766 + pdev[0] = platform_device_register_simple("mcf548x-flexcan", 0,
767 + mcf548x_can0_resources, 2);
768 + if (IS_ERR(pdev[0])) {
769 + err = PTR_ERR(pdev[0]);
772 + pdev[1] = platform_device_register_simple("mcf548x-flexcan", 1,
773 + mcf548x_can1_resources, 2);
774 + if (IS_ERR(pdev[1])) {
775 + err = PTR_ERR(pdev[1]);
779 + /* FlexCAN clock */
780 + pdata.clock_frq = 100000000;
782 + for (i = 0; i < PDEV_MAX; i++) {
783 + err = platform_device_add_data(pdev[i], &pdata, sizeof(pdata));
790 +int __init mcf548x_can_init(void)
792 + int err = mcf548x_of_to_pdev();
795 + printk(KERN_ERR "%s init failed with err=%d\n",
796 + mcf548x_can_driver.driver.name, err);
800 + return platform_driver_register(&mcf548x_can_driver);
803 +void __exit mcf548x_can_exit(void)
806 + platform_driver_unregister(&mcf548x_can_driver);
807 + for (i = 0; i < PDEV_MAX; i++)
808 + platform_device_unregister(pdev[i]);
811 +module_init(mcf548x_can_init);
812 +module_exit(mcf548x_can_exit);
814 +MODULE_AUTHOR("Andrey Volkov <avolkov@varma-el.com>");
815 +MODULE_DESCRIPTION("Freescale MCF548x CAN driver");
816 +MODULE_LICENSE("GPL v2");
817 --- a/include/asm-m68k/m5485sim.h
818 +++ b/include/asm-m68k/m5485sim.h
820 #define MCF_PAR_PCIBR MCF_REG16(0x000A4A)
821 #define MCF_PAR_PSCn(x) MCF_REG08(0x000A4F-((x)&0x3))
822 #define MCF_PAR_FECI2CIRQ MCF_REG16(0x000A44)
823 +#define MCF_PAR_DSPI MCF_REG16(0x000A50)
824 +#define MCF_PAR_TIMER MCF_REG08(0X000A52)
825 #define MCF_EPPAR MCF_REG16(0x000F00)
826 #define MCF_EPIER MCF_REG08(0x000F05)
827 #define MCF_EPFR MCF_REG08(0x000F0C)
829 +++ b/include/linux/can/dev.h
834 + * Definitions for CAN controller network devices lib (work in progress)
838 + * Author: Andrey Volkov <avolkov@varma-el.com>
839 + * Copyright (c) 2006 Varma Electronics Oy
843 +#ifndef CAN_DEVICE_H
844 +#define CAN_DEVICE_H
846 +#include <linux/version.h>
847 +#include <linux/can/error.h>
848 +#include <linux/can/ioctl.h>
851 + struct can_device_stats can_stats;
853 + /* can-bus oscillator frequency, in Hz,
854 + BE CAREFUL! SOME CONTROLLERS (LIKE SJA1000)
855 + FOOLISH ABOUT THIS FRQ (for sja1000 as ex. this
856 + clock must be xtal clock divided by 2). */
859 + /* by default max_brp is equal 64,
860 + but for a Freescale TouCAN, as ex., it can be 255*/
862 + /* For the mostly all controllers, max_sjw is equal 4, but
863 + some, hmm, CAN implementations hardwared it to 1 */
866 + u32 baudrate; /* in bauds */
867 + struct can_bittime bit_time;
869 + spinlock_t irq_lock;
870 + /* Please hold this lock when touching net_stats/can_stats*/
871 + spinlock_t stats_lock;
875 + can_ctrlmode_t ctrlmode;
877 + int (*do_set_bit_time)(struct net_device *dev, struct can_bittime *br);
878 + int (*do_get_state) (struct net_device *dev, can_state_t *state);
879 + int (*do_set_mode) (struct net_device *dev, can_mode_t mode);
880 + int (*do_set_ctrlmode)(struct net_device *dev, can_ctrlmode_t ctrlmode);
881 + int (*do_get_ctrlmode)(struct net_device *dev, can_ctrlmode_t *ctrlmode);
884 +#define ND2D(_ndev) (_ndev->dev.parent)
886 +struct net_device *alloc_candev(int sizeof_priv);
887 +void free_candev(struct net_device *dev);
889 +int can_calc_bit_time(struct can_priv *can, u32 baudrate,
890 + struct can_bittime_std *bit_time);
892 +#endif /* CAN_DEVICE_H */
894 +++ b/include/linux/can/ioctl.h
897 + * linux/can/ioctl.h
899 + * Definitions for CAN controller setup (work in progress)
903 + * Send feedback to <socketcan-users@lists.berlios.de>
910 +#include <linux/sockios.h>
913 +/* max. 16 private ioctls */
915 +#define SIOCSCANBAUDRATE (SIOCDEVPRIVATE+0)
916 +#define SIOCGCANBAUDRATE (SIOCDEVPRIVATE+1)
918 +#define SIOCSCANCUSTOMBITTIME (SIOCDEVPRIVATE+2)
919 +#define SIOCGCANCUSTOMBITTIME (SIOCDEVPRIVATE+3)
921 +#define SIOCSCANMODE (SIOCDEVPRIVATE+4)
922 +#define SIOCGCANMODE (SIOCDEVPRIVATE+5)
924 +#define SIOCSCANCTRLMODE (SIOCDEVPRIVATE+6)
925 +#define SIOCGCANCTRLMODE (SIOCDEVPRIVATE+7)
927 +#define SIOCSCANFILTER (SIOCDEVPRIVATE+8)
928 +#define SIOCGCANFILTER (SIOCDEVPRIVATE+9)
930 +#define SIOCGCANSTATE (SIOCDEVPRIVATE+10)
931 +#define SIOCGCANSTATS (SIOCDEVPRIVATE+11)
933 +#define SIOCSCANERRORCONFIG (SIOCDEVPRIVATE+12)
934 +#define SIOCGCANERRORCONFIG (SIOCDEVPRIVATE+13)
936 +/* parameters for ioctls */
938 +/* SIOC[SG]CANBAUDRATE */
939 +/* baudrate for CAN-controller in bits per second. */
940 +/* 0 = Scan for baudrate (Autobaud) */
942 +typedef __u32 can_baudrate_t;
945 +/* SIOC[SG]CANCUSTOMBITTIME */
947 +typedef enum CAN_BITTIME_TYPE {
950 +} can_bittime_type_t;
952 +/* TSEG1 of controllers usually is a sum of synch_seg (always 1),
953 + * prop_seg and phase_seg1, TSEG2 = phase_seg2 */
955 +struct can_bittime_std {
956 + __u32 brp; /* baud rate prescaler */
957 + __u8 prop_seg; /* from 1 to 8 */
958 + __u8 phase_seg1; /* from 1 to 8 */
959 + __u8 phase_seg2; /* from 1 to 8 */
960 + __u8 sjw:7; /* from 1 to 4 */
961 + __u8 sam:1; /* 1 - enable triple sampling */
964 +struct can_bittime_btr {
969 +struct can_bittime {
970 + can_bittime_type_t type;
972 + struct can_bittime_std std;
973 + struct can_bittime_btr btr;
977 +#define CAN_BAUDRATE_UNCONFIGURED ((__u32) 0xFFFFFFFFU)
978 +#define CAN_BAUDRATE_UNKNOWN 0
980 +/* SIOC[SG]CANMODE */
982 +typedef __u32 can_mode_t;
984 +#define CAN_MODE_STOP 0
985 +#define CAN_MODE_START 1
986 +#define CAN_MODE_SLEEP 2
989 +/* SIOC[SG]CANCTRLMODE */
991 +typedef __u32 can_ctrlmode_t;
993 +#define CAN_CTRLMODE_LOOPBACK 0x1
994 +#define CAN_CTRLMODE_LISTENONLY 0x2
997 +/* SIOCGCANFILTER */
999 +typedef __u32 can_filter_t;
1001 +/* filter modes (may vary due to controller specific capabilities) */
1002 +#define CAN_FILTER_CAPAB 0 /* get filter type capabilities
1004 +#define CAN_FILTER_MASK_VALUE 1 /* easy bit filter (see struct can_filter) */
1005 +#define CAN_FILTER_SFF_BITMASK 2 /* bitfield with 2048 bit SFF filter */
1006 + /* filters 3 - 31 currently undefined */
1008 +#define CAN_FILTER_MAX 31 /* max. filter type value */
1011 +/* SIOCGCANSTATE */
1013 +typedef __u32 can_state_t;
1015 +#define CAN_STATE_ACTIVE 0
1016 +#define CAN_STATE_BUS_WARNING 1
1017 +#define CAN_STATE_BUS_PASSIVE 2
1018 +#define CAN_STATE_BUS_OFF 3
1019 +#define CAN_STATE_SCANNING_BAUDRATE 4
1020 +#define CAN_STATE_STOPPED 5
1021 +#define CAN_STATE_SLEEPING 6
1024 +/* SIOCGCANSTATS */
1026 +struct can_device_stats {
1027 + int error_warning;
1031 + int error_passive;
1032 + int arbitration_lost;
1034 + int bus_error_at_init;
1037 +/* SIOC[SG]CANERRORCONFIG */
1039 +typedef enum CAN_ERRCFG_TYPE {
1041 + CAN_ERRCFG_BUSERR,
1043 +} can_errcfg_type_t;
1047 +#endif /* CAN_IOCTL_H */
1049 +++ b/include/linux/can/version.h
1052 + * linux/can/version.h
1054 + * Version information for the CAN network layer implementation
1056 + * Author: Urs Thuermann <urs.thuermann@volkswagen.de>
1057 + * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
1058 + * All rights reserved.
1060 + * Send feedback to <socketcan-users@lists.berlios.de>
1064 +#ifndef CAN_VERSION_H
1065 +#define CAN_VERSION_H
1067 +#define RCSID(s) asm(".section .rodata.str1.1,\"aMS\",@progbits,1\n\t" \
1068 + ".string \"" s "\"\n\t.previous\n")
1072 +#endif /* CAN_VERSION_H */
1073 --- a/net/can/Makefile
1074 +++ b/net/can/Makefile
1075 @@ -10,3 +10,6 @@ can-raw-objs := raw.o
1077 obj-$(CONFIG_CAN_BCM) += can-bcm.o
1078 can-bcm-objs := bcm.o
1080 +obj-$(CONFIG_CAN) += candev.o
1081 +candev-objs := dev.o
1088 + * Copyright (C) 2005 Marc Kleine-Budde, Pengutronix
1089 + * Copyright (C) 2006 Andrey Volkov, Varma Electronics
1091 + * This program is free software; you can redistribute it and/or modify
1092 + * it under the terms of the version 2 of the GNU General Public License
1093 + * as published by the Free Software Foundation
1095 + * This program is distributed in the hope that it will be useful,
1096 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1097 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1098 + * GNU General Public License for more details.
1100 + * You should have received a copy of the GNU General Public License
1101 + * along with this program; if not, write to the Free Software
1102 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1105 +#include <linux/module.h>
1106 +#include <linux/netdevice.h>
1107 +#include <linux/if_arp.h>
1108 +#include <linux/can.h>
1109 +#include <linux/can/dev.h>
1111 +MODULE_DESCRIPTION("CAN netdevice library");
1112 +MODULE_LICENSE("GPL v2");
1113 +MODULE_AUTHOR("Marc Kleine-Budde <mkl@pengutronix.de>, "
1114 + "Andrey Volkov <avolkov@varma-el.com>");
1118 + Baud rate calculated with next formula:
1119 + baud = frq/(brp*(1 + prop_seg+ phase_seg1 + phase_seg2))
1121 + This calc function based on work of Florian Hartwich and Armin Bassemi
1122 + "The Configuration of the CAN Bit Timing"
1123 + (http://www.semiconductors.bosch.de/pdf/CiA99Paper.pdf)
1127 + bit_time_nsec - expected bit time in nanosecs
1130 + bit_time - calculated time segments, for meaning of
1131 + each field read CAN standard.
1134 +#define DEFAULT_MAX_BRP 64U
1135 +#define DEFAULT_MAX_SJW 4U
1137 +/* All below values in tq units */
1138 +#define MAX_BIT_TIME 25U
1139 +#define MIN_BIT_TIME 8U
1140 +#define MAX_PROP_SEG 8U
1141 +#define MAX_PHASE_SEG1 8U
1142 +#define MAX_PHASE_SEG2 8U
1144 +int can_calc_bit_time(struct can_priv *can, u32 baudrate,
1145 + struct can_bittime_std *bit_time)
1147 + int best_error = -1; /* Ariphmetic error */
1148 + int df, best_df = -1; /* oscillator's tolerance range */
1149 + u32 quanta; /*in tq units*/
1150 + u32 brp, phase_seg1, phase_seg2, sjw, prop_seg;
1151 + u32 brp_min, brp_max, brp_expected;
1154 + /* baudrate range [1baud,1Mbaud] */
1155 + if (baudrate == 0 || baudrate > 1000000UL)
1158 + tmp = (u64)can->can_sys_clock*1000;
1159 + do_div(tmp, baudrate);
1160 + brp_expected = (u32)tmp;
1162 + brp_min = brp_expected / (1000 * MAX_BIT_TIME);
1165 + if (brp_min > can->max_brp)
1168 + brp_max = (brp_expected + 500 * MIN_BIT_TIME) / (1000 * MIN_BIT_TIME);
1171 + if (brp_max > can->max_brp)
1172 + brp_max = can->max_brp;
1174 + for (brp = brp_min; brp <= brp_max; brp++) {
1175 + quanta = brp_expected / (brp * 1000);
1176 + if (quanta < MAX_BIT_TIME && quanta * brp * 1000 !=
1179 + if (quanta < MIN_BIT_TIME || quanta > MAX_BIT_TIME)
1182 + phase_seg2 = min((quanta - 3) / 2, MAX_PHASE_SEG2);
1183 + for (sjw = can->max_sjw; sjw > 0; sjw--) {
1184 + for (; phase_seg2 > sjw; phase_seg2--) {
1186 + phase_seg1 = phase_seg2 % 2 ?
1187 + phase_seg2-1 : phase_seg2;
1188 + prop_seg = quanta-1 - phase_seg2 - phase_seg1;
1190 + * FIXME: support of longer lines
1191 + * (i.e. bigger prop_seg) is more prefered
1192 + * than support of cheap oscillators
1193 + * (i.e. bigger df/phase_seg1/phase_seg2)
1196 + if (prop_seg < phase_seg1)
1198 + if (prop_seg > MAX_PROP_SEG)
1201 + err1 = phase_seg1 * brp * 500 * 1000 /
1202 + (13 * brp_expected - phase_seg2 *
1204 + err2 = sjw * brp * 50 * 1000 / brp_expected;
1206 + df = min(err1, err2);
1207 + if (df >= best_df) {
1208 + unsigned error = abs(brp_expected * 10 /
1209 + (brp * (1 + prop_seg +
1211 + phase_seg2)) - 10000);
1213 + if (error > 10 || error > best_error)
1216 + if (error == best_error && prop_seg <
1217 + bit_time->prop_seg)
1220 + best_error = error;
1222 + bit_time->brp = brp;
1223 + bit_time->prop_seg = prop_seg;
1224 + bit_time->phase_seg1 = phase_seg1;
1225 + bit_time->phase_seg2 = phase_seg2;
1226 + bit_time->sjw = sjw;
1228 + (bit_time->phase_seg1 > 3);
1235 + if (best_error < 0)
1239 +EXPORT_SYMBOL(can_calc_bit_time);
1241 +static int can_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1243 + struct can_priv *can = netdev_priv(dev);
1244 + struct can_bittime *bt = (struct can_bittime *)&ifr->ifr_ifru;
1245 + ulong *baudrate = (ulong *)&ifr->ifr_ifru;
1246 + int ret = -EOPNOTSUPP;
1248 + dev_dbg(ND2D(dev), "(%s) 0x%08x %p\n", __func__, cmd, &ifr->ifr_ifru);
1251 + case SIOCSCANBAUDRATE:
1252 + if (can->do_set_bit_time) {
1253 + struct can_bittime bit_time;
1254 + ret = can_calc_bit_time(can, *baudrate, &bit_time.std);
1257 + bit_time.type = CAN_BITTIME_STD;
1258 + ret = can->do_set_bit_time(dev, &bit_time);
1260 + can->baudrate = *baudrate;
1261 + can->bit_time = bit_time;
1265 + case SIOCGCANBAUDRATE:
1266 + *baudrate = can->baudrate;
1269 + case SIOCSCANCUSTOMBITTIME:
1270 + if (can->do_set_bit_time) {
1271 + ret = can->do_set_bit_time(dev, bt);
1273 + can->bit_time = *bt;
1274 + if (bt->type == CAN_BITTIME_STD && bt->std.brp) {
1275 + can->baudrate = can->can_sys_clock /
1276 + (bt->std.brp * (1 + bt->std.prop_seg +
1277 + bt->std.phase_seg1 +
1278 + bt->std.phase_seg2));
1280 + can->baudrate = CAN_BAUDRATE_UNKNOWN;
1284 + case SIOCGCANCUSTOMBITTIME:
1285 + *bt = can->bit_time;
1288 + case SIOCSCANMODE:
1289 + if (can->do_set_mode) {
1291 + *((can_mode_t *)(&ifr->ifr_ifru));
1292 + if (mode == CAN_MODE_START &&
1293 + can->baudrate == CAN_BAUDRATE_UNCONFIGURED) {
1294 + dev_info(ND2D(dev), "Impossible to start \
1295 + on UNKNOWN speed\n");
1298 + return can->do_set_mode(dev, mode);
1301 + case SIOCGCANMODE:
1302 + *((can_mode_t *)(&ifr->ifr_ifru)) = can->mode;
1305 + case SIOCSCANCTRLMODE:
1306 + if (can->do_set_ctrlmode) {
1307 + can_ctrlmode_t ctrlmode =
1308 + *((can_ctrlmode_t *)(&ifr->ifr_ifru));
1309 + return can->do_set_ctrlmode(dev, ctrlmode);
1312 + case SIOCGCANCTRLMODE:
1313 + *((can_ctrlmode_t *)(&ifr->ifr_ifru)) = can->ctrlmode;
1316 + case SIOCSCANFILTER:
1318 + case SIOCGCANFILTER:
1320 + case SIOCGCANSTATE:
1321 + if (can->do_get_state)
1322 + return can->do_get_state(dev,
1323 + (can_state_t *)(&ifr->ifr_ifru));
1325 + case SIOCGCANSTATS:
1326 + *((struct can_device_stats *)(&ifr->ifr_ifru)) = can->can_stats;
1334 +static void can_setup(struct net_device *dev)
1336 + dev->type = ARPHRD_CAN;
1337 + dev->mtu = sizeof(struct can_frame);
1338 + dev->do_ioctl = can_ioctl;
1339 + dev->hard_header_len = 0;
1340 + dev->addr_len = 0;
1341 + dev->tx_queue_len = 10;
1343 + /* New-style flags. */
1344 + dev->flags = IFF_NOARP;
1345 + dev->features = NETIF_F_NO_CSUM;
1349 + * Function alloc_candev
1350 + * Allocates and sets up an CAN device
1352 +struct net_device *alloc_candev(int sizeof_priv)
1354 + struct net_device *dev;
1355 + struct can_priv *priv;
1357 + dev = alloc_netdev(sizeof_priv, "can%d", can_setup);
1361 + priv = netdev_priv(dev);
1363 + priv->baudrate = CAN_BAUDRATE_UNCONFIGURED;
1364 + priv->max_brp = DEFAULT_MAX_BRP;
1365 + priv->max_sjw = DEFAULT_MAX_SJW;
1366 + spin_lock_init(&priv->irq_lock);
1370 +EXPORT_SYMBOL(alloc_candev);
1372 +void free_candev(struct net_device *dev)
1376 +EXPORT_SYMBOL(free_candev);