2 * sja1000.c - Philips SJA1000 network device driver
4 * Copyright (c) 2003 Matthias Brukner, Trajet Gmbh, Rebenring 33,
5 * 38106 Braunschweig, GERMANY
7 * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of Volkswagen nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * Alternatively, provided that this notice is retained in full, this
23 * software may be distributed under the terms of the GNU General
24 * Public License ("GPL") version 2, in which case the provisions of the
25 * GPL apply INSTEAD OF those given above.
27 * The provided data structures and external interfaces from this code
28 * are not restricted to be used by modules with a GPL compatible license.
30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
33 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
34 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
36 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
40 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
43 * Send feedback to <socketcan-users@lists.berlios.de>
47 #include <linux/module.h>
48 #include <linux/init.h>
49 #include <linux/version.h>
50 #include <linux/kernel.h>
51 #include <linux/sched.h>
52 #include <linux/types.h>
53 #include <linux/fcntl.h>
54 #include <linux/interrupt.h>
55 #include <linux/ptrace.h>
56 #include <linux/string.h>
57 #include <linux/errno.h>
58 #include <linux/netdevice.h>
59 #include <linux/if_arp.h>
60 #include <linux/if_ether.h>
61 #include <linux/skbuff.h>
63 #include <linux/can.h>
64 #include <linux/can/ioctl.h> /* for struct can_device_stats */
68 #include <linux/can/version.h> /* for RCSID. Removed by mkpatch script */
71 MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
72 MODULE_LICENSE("Dual BSD/GPL");
73 MODULE_DESCRIPTION("LLCF/socketcan '" CHIP_NAME "' network device driver");
75 #ifdef CONFIG_CAN_DEBUG_DEVICES
76 #define DBG(args...) ((priv->debug > 0) ? printk(args) : 0)
77 /* logging in interrupt context! */
78 #define iDBG(args...) ((priv->debug > 1) ? printk(args) : 0)
79 #define iiDBG(args...) ((priv->debug > 2) ? printk(args) : 0)
83 #define iiDBG(args...)
86 char drv_name[DRV_NAME_LEN] = "undefined";
88 /* driver and version information */
89 static const char *drv_version = "0.1.1";
90 static const char *drv_reldate = "2007-04-13";
92 #ifdef CONFIG_CAN_DEBUG_DEVICES
93 static const char *ecc_errors[] = {
113 "tolerate dominant bits",
116 "passive error flag",
121 "acknowledge delimiter",
128 static const char *ecc_types[] = {
132 "other type of error"
136 /* array of all can chips */
137 struct net_device *can_dev[MAXDEV];
139 /* module parameters */
140 unsigned long base[MAXDEV] = { 0 }; /* hardware address */
141 unsigned long rbase[MAXDEV] = { 0 }; /* (remapped) device address */
142 unsigned int irq[MAXDEV] = { 0 };
144 unsigned int speed[MAXDEV] = { DEFAULT_SPEED, DEFAULT_SPEED };
145 unsigned int btr[MAXDEV] = { 0 };
147 static int rx_probe[MAXDEV] = { 0 };
148 static int clk = DEFAULT_HW_CLK;
149 static int debug = 0;
150 static int restart_ms = 100;
157 static int rx_probe_n;
159 module_param_array(base, int, &base_n, 0);
160 module_param_array(irq, int, &irq_n, 0);
161 module_param_array(speed, int, &speed_n, 0);
162 module_param_array(btr, int, &btr_n, 0);
163 module_param_array(rx_probe, int, &rx_probe_n, 0);
165 module_param(clk, int, 0);
166 module_param(debug, int, 0);
167 module_param(restart_ms, int, 0);
168 module_param(echo, int, S_IRUGO);
170 MODULE_PARM_DESC(base, "CAN controller base address");
171 MODULE_PARM_DESC(irq, "CAN controller interrupt");
172 MODULE_PARM_DESC(speed, "CAN bus bitrate");
173 MODULE_PARM_DESC(btr, "Bit Timing Register value 0x<btr0><btr1>, e.g. 0x4014");
174 MODULE_PARM_DESC(rx_probe, "switch to trx mode after correct msg receiption. (default off)");
176 MODULE_PARM_DESC(clk, "CAN controller chip clock (default: 16MHz)");
177 MODULE_PARM_DESC(debug, "set debug mask (default: 0)");
178 MODULE_PARM_DESC(restart_ms, "restart chip on heavy bus errors / bus off after x ms (default 100ms)");
179 MODULE_PARM_DESC(echo, "Echo sent frames. default: 1 (On)");
182 * CAN network devices *should* support a local echo functionality
183 * (see Documentation/networking/can.txt). To test the handling of CAN
184 * interfaces that do not support the local echo both driver types are
185 * implemented inside this sja1000 driver. In the case that the driver does
186 * not support the echo the IFF_ECHO remains clear in dev->flags.
187 * This causes the PF_CAN core to perform the echo as a fallback solution.
190 /* function declarations */
192 static void can_restart_dev(unsigned long data);
193 static void chipset_init(struct net_device *dev, int wake);
194 static void chipset_init_rx(struct net_device *dev);
195 static void chipset_init_trx(struct net_device *dev);
196 static void can_netdev_setup(struct net_device *dev);
197 static struct net_device* can_create_netdev(int dev_num, int hw_regs);
198 static int can_set_drv_name(void);
199 int set_reset_mode(struct net_device *dev);
201 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
202 static struct net_device_stats *can_get_stats(struct net_device *dev)
204 struct can_priv *priv = netdev_priv(dev);
206 /* TODO: read statistics from chip */
211 static int sja1000_probe_chip(unsigned long base)
213 if (base && (hw_readreg(base, 0) == 0xFF)) {
214 printk(KERN_INFO "%s: probing @0x%lX failed\n",
222 * set baud rate divisor values
224 static void set_btr(struct net_device *dev, int btr0, int btr1)
226 struct can_priv *priv = netdev_priv(dev);
228 /* no bla bla when restarting the device */
229 if (priv->state == STATE_UNINITIALIZED)
230 printk(KERN_INFO "%s: setting BTR0=%02X BTR1=%02X\n",
231 dev->name, btr0, btr1);
233 hw_writereg(dev->base_addr, REG_BTR0, btr0);
234 hw_writereg(dev->base_addr, REG_BTR1, btr1);
238 * calculate baud rate divisor values
240 static void set_baud(struct net_device *dev, int baud, int clock)
242 struct can_priv *priv = netdev_priv(dev);
250 int best_error = 1000000000;
255 int SAM = (baud > 100000 ? 0 : 1);
259 for (tseg = (0 + 0 + 2) * 2;
260 tseg <= (MAX_TSEG2 + MAX_TSEG1 + 2) * 2 + 1;
262 brp = clock / ((1 + tseg / 2) * baud) + tseg % 2;
263 if ((brp > 0) && (brp <= 64)) {
264 error = baud - clock / (brp * (1 + tseg / 2));
268 if (error <= best_error) {
270 best_tseg = tseg / 2;
272 best_baud = clock / (brp * (1 + tseg / 2));
276 if (best_error && (baud / best_error < 10)) {
277 printk("%s: unable to set baud rate %d (ext clock %dHz)\n",
278 dev->name, baud, clock * 2);
282 tseg2 = best_tseg - (SAMPLE_POINT * (best_tseg + 1)) / 100;
285 } else if (tseg2 > MAX_TSEG2) {
288 tseg1 = best_tseg - tseg2 - 2;
289 if (tseg1 > MAX_TSEG1) {
291 tseg2 = best_tseg - tseg1 - 2;
294 priv->btr = ((best_brp | JUMPWIDTH)<<8) +
295 ((SAM << 7) | (tseg2 << 4) | tseg1);
297 printk(KERN_INFO "%s: calculated best baudrate: %d / btr is 0x%04X\n",
298 dev->name, best_baud, priv->btr);
300 set_btr(dev, (priv->btr>>8) & 0xFF, priv->btr & 0xFF);
301 // set_btr(dev, best_brp | JUMPWIDTH, (SAM << 7) | (tseg2 << 4) | tseg1);
304 int set_reset_mode(struct net_device *dev)
306 struct can_priv *priv = netdev_priv(dev);
307 unsigned char status = hw_readreg(dev->base_addr, REG_MOD);
310 priv->can_stats.bus_error_at_init = priv->can_stats.bus_error;
312 /* disable interrupts */
313 hw_writereg(dev->base_addr, REG_IER, IRQ_OFF);
315 for (i = 0; i < 10; i++) {
316 /* check reset bit */
317 if (status & MOD_RM) {
319 iDBG(KERN_INFO "%s: %s looped %d times\n",
320 dev->name, __FUNCTION__, i);
322 priv->state = STATE_RESET_MODE;
326 hw_writereg(dev->base_addr, REG_MOD, MOD_RM); /* reset chip */
327 status = hw_readreg(dev->base_addr, REG_MOD);
331 printk(KERN_ERR "%s: setting sja1000 into reset mode failed!\n",
337 static int set_normal_mode(struct net_device *dev)
339 unsigned char status = hw_readreg(dev->base_addr, REG_MOD);
342 for (i = 0; i < 10; i++) {
343 /* check reset bit */
344 if ((status & MOD_RM) == 0) {
345 #ifdef CONFIG_CAN_DEBUG_DEVICES
347 struct can_priv *priv = netdev_priv(dev);
348 iDBG(KERN_INFO "%s: %s looped %d times\n",
349 dev->name, __FUNCTION__, i);
355 /* set chip to normal mode */
356 hw_writereg(dev->base_addr, REG_MOD, 0x00);
357 status = hw_readreg(dev->base_addr, REG_MOD);
360 printk(KERN_ERR "%s: setting sja1000 into normal mode failed!\n",
366 static int set_listen_mode(struct net_device *dev)
368 unsigned char status = hw_readreg(dev->base_addr, REG_MOD);
371 for (i = 0; i < 10; i++) {
372 /* check reset mode bit */
373 if ((status & MOD_RM) == 0) {
374 #ifdef CONFIG_CAN_DEBUG_DEVICES
376 struct can_priv *priv = netdev_priv(dev);
377 iDBG(KERN_INFO "%s: %s looped %d times\n",
378 dev->name, __FUNCTION__, i);
384 /* set listen only mode, clear reset */
385 hw_writereg(dev->base_addr, REG_MOD, MOD_LOM);
386 status = hw_readreg(dev->base_addr, REG_MOD);
389 printk(KERN_ERR "%s: setting sja1000 into listen mode failed!\n",
396 * initialize SJA1000 chip:
400 * - enable interrupts
401 * - start operating mode
403 static void chipset_init_regs(struct net_device *dev)
405 struct can_priv *priv = netdev_priv(dev);
406 unsigned long base = dev->base_addr;
408 /* go into Pelican mode, disable clkout, disable comparator */
409 hw_writereg(base, REG_CDR, 0xCF);
412 /* connected to external transceiver */
413 hw_writereg(base, REG_OCR, 0x1A);
415 /* set acceptance filter (accept all) */
416 hw_writereg(base, REG_ACCC0, 0x00);
417 hw_writereg(base, REG_ACCC1, 0x00);
418 hw_writereg(base, REG_ACCC2, 0x00);
419 hw_writereg(base, REG_ACCC3, 0x00);
421 hw_writereg(base, REG_ACCM0, 0xFF);
422 hw_writereg(base, REG_ACCM1, 0xFF);
423 hw_writereg(base, REG_ACCM2, 0xFF);
424 hw_writereg(base, REG_ACCM3, 0xFF);
427 if (priv->btr) { /* no calculation when btr is provided */
428 set_btr(dev, (priv->btr>>8) & 0xFF, priv->btr & 0xFF);
430 if (priv->speed == 0) {
431 priv->speed = DEFAULT_SPEED;
433 set_baud(dev, priv->speed * 1000, priv->clock);
437 /* connected to external transceiver */
438 hw_writereg(base, REG_OCR, 0x1A);
441 static void chipset_init(struct net_device *dev, int wake)
443 struct can_priv *priv = netdev_priv(dev);
446 chipset_init_rx(dev); /* wait for valid reception first */
448 chipset_init_trx(dev);
450 if ((wake) && netif_queue_stopped(dev)) {
451 if (priv->echo_skb) { /* pending echo? */
452 kfree_skb(priv->echo_skb);
453 priv->echo_skb = NULL;
455 netif_wake_queue(dev);
459 static void chipset_init_rx(struct net_device *dev)
461 struct can_priv *priv = netdev_priv(dev);
463 iDBG(KERN_INFO "%s: %s()\n", dev->name, __FUNCTION__);
465 /* set chip into reset mode */
469 chipset_init_regs(dev);
471 /* automatic bit rate detection */
472 set_listen_mode(dev);
474 priv->state = STATE_PROBE;
476 /* enable receive and error interrupts */
477 hw_writereg(dev->base_addr, REG_IER, IRQ_RI | IRQ_EI);
480 static void chipset_init_trx(struct net_device *dev)
482 struct can_priv *priv = netdev_priv(dev);
484 iDBG(KERN_INFO "%s: %s()\n", dev->name, __FUNCTION__);
486 /* set chip into reset mode */
490 chipset_init_regs(dev);
492 /* leave reset mode */
493 set_normal_mode(dev);
495 priv->state = STATE_ACTIVE;
497 /* enable all interrupts */
498 hw_writereg(dev->base_addr, REG_IER, IRQ_ALL);
502 * transmit a CAN message
503 * message layout in the sk_buff should be like this:
504 * xx xx xx xx ff ll 00 11 22 33 44 55 66 77
505 * [ can-id ] [flags] [len] [can data (up to 8 bytes]
507 static int can_start_xmit(struct sk_buff *skb, struct net_device *dev)
509 struct can_priv *priv = netdev_priv(dev);
510 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
511 struct net_device_stats *stats = can_get_stats(dev);
513 struct net_device_stats *stats = &dev->stats;
515 struct can_frame *cf = (struct can_frame*)skb->data;
516 unsigned long base = dev->base_addr;
524 netif_stop_queue(dev);
526 fi = dlc = cf->can_dlc;
529 if (id & CAN_RTR_FLAG)
532 if (id & CAN_EFF_FLAG) {
535 hw_writereg(base, REG_FI, fi);
536 hw_writereg(base, REG_ID1, (id & 0x1fe00000) >> (5 + 16));
537 hw_writereg(base, REG_ID2, (id & 0x001fe000) >> (5 + 8));
538 hw_writereg(base, REG_ID3, (id & 0x00001fe0) >> 5);
539 hw_writereg(base, REG_ID4, (id & 0x0000001f) << 3);
542 hw_writereg(base, REG_FI, fi);
543 hw_writereg(base, REG_ID1, (id & 0x000007f8) >> 3);
544 hw_writereg(base, REG_ID2, (id & 0x00000007) << 5);
547 for (i = 0; i < dlc; i++) {
548 hw_writereg(base, dreg++, cf->data[i]);
551 hw_writereg(base, REG_CMR, CMD_TR);
553 stats->tx_bytes += dlc;
555 dev->trans_start = jiffies;
557 /* set flag whether this packet has to be looped back */
558 loop = skb->pkt_type == PACKET_LOOPBACK;
560 if (!echo || !loop) {
565 if (!priv->echo_skb) {
566 struct sock *srcsk = skb->sk;
568 if (atomic_read(&skb->users) != 1) {
569 struct sk_buff *old_skb = skb;
571 skb = skb_clone(old_skb, GFP_ATOMIC);
572 DBG(KERN_INFO "%s: %s: freeing old skbuff %p, "
573 "using new skbuff %p\n",
574 dev->name, __FUNCTION__, old_skb, skb);
584 /* make settings for echo to reduce code in irq context */
585 skb->protocol = htons(ETH_P_CAN);
586 skb->pkt_type = PACKET_BROADCAST;
587 skb->ip_summed = CHECKSUM_UNNECESSARY;
590 /* save this skb for tx interrupt echo handling */
591 priv->echo_skb = skb;
594 /* locking problem with netif_stop_queue() ?? */
595 printk(KERN_ERR "%s: %s: occupied echo_skb!\n",
596 dev->name, __FUNCTION__ );
603 static void can_tx_timeout(struct net_device *dev)
605 struct can_priv *priv = netdev_priv(dev);
606 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
607 struct net_device_stats *stats = can_get_stats(dev);
609 struct net_device_stats *stats = &dev->stats;
614 /* do not conflict with e.g. bus error handling */
615 if (!(priv->timer.expires)){ /* no restart on the run */
616 chipset_init_trx(dev); /* no tx queue wakeup */
617 if (priv->echo_skb) { /* pending echo? */
618 kfree_skb(priv->echo_skb);
619 priv->echo_skb = NULL;
621 netif_wake_queue(dev); /* wakeup here */
624 DBG(KERN_INFO "%s: %s: can_restart_dev already active.\n",
625 dev->name, __FUNCTION__ );
629 static void can_restart_on(struct net_device *dev)
631 struct can_priv *priv = netdev_priv(dev);
633 if (!(priv->timer.expires)){ /* no restart on the run */
637 priv->timer.function = can_restart_dev;
638 priv->timer.data = (unsigned long) dev;
640 /* restart chip on persistent error in <xxx> ms */
641 priv->timer.expires = jiffies + (priv->restart_ms * HZ) / 1000;
642 add_timer(&priv->timer);
644 iDBG(KERN_INFO "%s: %s start (%ld)\n",
645 dev->name, __FUNCTION__ , jiffies);
647 iDBG(KERN_INFO "%s: %s already (%ld)\n",
648 dev->name, __FUNCTION__ , jiffies);
651 static void can_restart_dev(unsigned long data)
653 struct net_device *dev = (struct net_device*) data;
654 struct can_priv *priv = netdev_priv(dev);
656 DBG(KERN_INFO "%s: can_restart_dev (%ld)\n",
659 /* mark inactive timer */
660 priv->timer.expires = 0;
662 if (priv->state != STATE_UNINITIALIZED) {
664 /* count number of restarts */
665 priv->can_stats.restarts++;
667 chipset_init(dev, 1);
672 /* the timerless version */
674 static void can_restart_now(struct net_device *dev)
676 struct can_priv *priv = netdev_priv(dev);
678 if (priv->state != STATE_UNINITIALIZED) {
680 /* count number of restarts */
681 priv->can_stats.restarts++;
683 chipset_init(dev, 1);
688 static void can_rx(struct net_device *dev)
690 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
691 struct net_device_stats *stats = can_get_stats(dev);
693 struct net_device_stats *stats = &dev->stats;
695 unsigned long base = dev->base_addr;
696 struct can_frame *cf;
704 skb = dev_alloc_skb(sizeof(struct can_frame));
709 skb->protocol = htons(ETH_P_CAN);
711 fi = hw_readreg(base, REG_FI);
715 /* extended frame format (EFF) */
717 id = (hw_readreg(base, REG_ID1) << (5+16))
718 | (hw_readreg(base, REG_ID2) << (5+8))
719 | (hw_readreg(base, REG_ID3) << 5)
720 | (hw_readreg(base, REG_ID4) >> 3);
723 /* standard frame format (SFF) */
725 id = (hw_readreg(base, REG_ID1) << 3)
726 | (hw_readreg(base, REG_ID2) >> 5);
732 cf = (struct can_frame*)skb_put(skb, sizeof(struct can_frame));
733 memset(cf, 0, sizeof(struct can_frame));
736 for (i = 0; i < dlc; i++) {
737 cf->data[i] = hw_readreg(base, dreg++);
742 /* release receive buffer */
743 hw_writereg(base, REG_CMR, CMD_RRB);
747 dev->last_rx = jiffies;
749 stats->rx_bytes += dlc;
753 * SJA1000 interrupt handler
755 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
756 static irqreturn_t can_interrupt(int irq, void *dev_id, struct pt_regs *regs)
758 static irqreturn_t can_interrupt(int irq, void *dev_id)
761 struct net_device *dev = (struct net_device*)dev_id;
762 struct can_priv *priv = netdev_priv(dev);
763 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
764 struct net_device_stats *stats = can_get_stats(dev);
766 struct net_device_stats *stats = &dev->stats;
768 unsigned long base = dev->base_addr;
769 uint8_t isrc, status, ecc, alc;
774 iiDBG(KERN_INFO "%s: interrupt\n", dev->name);
776 if (priv->state == STATE_UNINITIALIZED) {
777 printk(KERN_ERR "%s: %s: uninitialized controller!\n",
778 dev->name, __FUNCTION__);
779 chipset_init(dev, 1); /* should be possible at this stage */
783 if (priv->state == STATE_RESET_MODE) {
784 iiDBG(KERN_ERR "%s: %s: controller is in reset mode! "
785 "MOD=0x%02X IER=0x%02X IR=0x%02X SR=0x%02X!\n",
786 dev->name, __FUNCTION__, hw_readreg(base, REG_MOD),
787 hw_readreg(base, REG_IER), hw_readreg(base, REG_IR),
788 hw_readreg(base, REG_SR));
792 while ((isrc = hw_readreg(base, REG_IR)) && (n < 20)) {
794 status = hw_readreg(base, REG_SR);
796 if (isrc & IRQ_WUI) {
797 /* wake-up interrupt */
798 priv->can_stats.wakeup++;
801 /* transmission complete interrupt */
804 if (echo && priv->echo_skb) {
805 netif_rx(priv->echo_skb);
806 priv->echo_skb = NULL;
809 netif_wake_queue(dev);
812 /* receive interrupt */
814 while (status & SR_RBS) {
816 status = hw_readreg(base, REG_SR);
818 if (priv->state == STATE_PROBE) {
819 /* valid RX -> switch to trx-mode */
820 iDBG(KERN_INFO "%s: RI #%d#\n", dev->name, n);
821 chipset_init_trx(dev); /* no tx queue wakeup */
822 break; /* check again after init controller */
825 if (isrc & IRQ_DOI) {
826 /* data overrun interrupt */
827 iiDBG(KERN_INFO "%s: data overrun isrc=0x%02X "
829 dev->name, isrc, status);
830 iDBG(KERN_INFO "%s: DOI #%d#\n", dev->name, n);
831 priv->can_stats.data_overrun++;
832 hw_writereg(base, REG_CMR, CMD_CDO); /* clear bit */
835 /* error warning interrupt */
836 iiDBG(KERN_INFO "%s: error warning isrc=0x%02X "
838 dev->name, isrc, status);
839 iDBG(KERN_INFO "%s: EI #%d#\n", dev->name, n);
840 priv->can_stats.error_warning++;
841 if (status & SR_BS) {
842 printk(KERN_INFO "%s: BUS OFF, "
843 "restarting device\n", dev->name);
845 /* controller has been restarted: leave here */
847 } else if (status & SR_ES) {
848 iDBG(KERN_INFO "%s: error\n", dev->name);
851 if (isrc & IRQ_BEI) {
852 /* bus error interrupt */
853 iiDBG(KERN_INFO "%s: bus error isrc=0x%02X "
855 dev->name, isrc, status);
856 iDBG(KERN_INFO "%s: BEI #%d# [%d]\n", dev->name, n,
857 priv->can_stats.bus_error -
858 priv->can_stats.bus_error_at_init);
859 priv->can_stats.bus_error++;
860 ecc = hw_readreg(base, REG_ECC);
861 iDBG(KERN_INFO "%s: ECC = 0x%02X (%s, %s, %s)\n",
863 (ecc & ECC_DIR) ? "RX" : "TX",
864 ecc_types[ecc >> ECC_ERR],
865 ecc_errors[ecc & ECC_SEG]);
867 /* when the bus errors flood the system, */
868 /* restart the controller */
869 if (priv->can_stats.bus_error_at_init +
870 MAX_BUS_ERRORS < priv->can_stats.bus_error) {
871 iDBG(KERN_INFO "%s: heavy bus errors,"
872 " restarting device\n", dev->name);
874 /* controller has been restarted: leave here */
878 /* don't know, if this is a good idea, */
879 /* but it works fine ... */
880 if (hw_readreg(base, REG_RXERR) > 128) {
881 iDBG(KERN_INFO "%s: RX_ERR > 128,"
882 " restarting device\n", dev->name);
884 /* controller has been restarted: leave here */
889 if (isrc & IRQ_EPI) {
890 /* error passive interrupt */
891 iiDBG(KERN_INFO "%s: error passive isrc=0x%02X"
893 dev->name, isrc, status);
894 iDBG(KERN_INFO "%s: EPI #%d#\n", dev->name, n);
895 priv->can_stats.error_passive++;
896 if (status & SR_ES) {
897 iDBG(KERN_INFO "%s: -> ERROR PASSIVE, "
898 "restarting device\n", dev->name);
900 /* controller has been restarted: leave here */
903 iDBG(KERN_INFO "%s: -> ERROR ACTIVE\n",
907 if (isrc & IRQ_ALI) {
908 /* arbitration lost interrupt */
909 iiDBG(KERN_INFO "%s: error arbitration lost "
910 "isrc=0x%02X status=0x%02X\n",
911 dev->name, isrc, status);
912 iDBG(KERN_INFO "%s: ALI #%d#\n", dev->name, n);
913 priv->can_stats.arbitration_lost++;
914 alc = hw_readreg(base, REG_ALC);
915 iDBG(KERN_INFO "%s: ALC = 0x%02X\n", dev->name, alc);
919 iDBG(KERN_INFO "%s: handled %d IRQs\n", dev->name, n);
924 return n == 0 ? IRQ_NONE : IRQ_HANDLED;
928 * initialize CAN bus driver
930 static int can_open(struct net_device *dev)
932 struct can_priv *priv = netdev_priv(dev);
934 /* set chip into reset mode */
937 priv->state = STATE_UNINITIALIZED;
939 /* register interrupt handler */
940 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18)
941 if (request_irq(dev->irq, &can_interrupt, SA_SHIRQ,
942 dev->name, (void*)dev)) {
944 if (request_irq(dev->irq, &can_interrupt, IRQF_SHARED,
945 dev->name, (void*)dev)) {
950 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
951 /* clear statistics */
952 memset(&priv->stats, 0, sizeof(priv->stats));
956 chipset_init(dev, 0);
957 priv->open_time = jiffies;
959 netif_start_queue(dev);
965 * stop CAN bus activity
967 static int can_close(struct net_device *dev)
969 struct can_priv *priv = netdev_priv(dev);
971 /* set chip into reset mode */
976 if (priv->timer.expires) {
977 del_timer(&priv->timer);
978 priv->timer.expires = 0;
981 free_irq(dev->irq, (void*)dev);
982 priv->state = STATE_UNINITIALIZED;
984 netif_stop_queue(dev);
990 static void test_if(struct net_device *dev)
996 hw_writereg(base, REG_CDR, 0xCF);
997 for (i = 0; i < 10000; i++) {
998 for (j = 0; j < 256; j++) {
999 hw_writereg(base, REG_EWL, j);
1000 x = hw_readreg(base, REG_EWL);
1002 printk(KERN_INFO "%s: is: %02X expected: "
1003 "%02X (%d)\n", dev->name, x, j, i);
1010 void can_netdev_setup(struct net_device *dev)
1012 /* Fill in the the fields of the device structure
1013 with CAN netdev generic values */
1015 dev->change_mtu = NULL;
1016 dev->set_mac_address = NULL;
1017 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
1018 dev->hard_header = NULL;
1019 dev->rebuild_header = NULL;
1020 dev->hard_header_cache = NULL;
1021 dev->header_cache_update = NULL;
1022 dev->hard_header_parse = NULL;
1024 dev->header_ops = NULL;
1027 dev->type = ARPHRD_CAN;
1028 dev->hard_header_len = 0;
1029 dev->mtu = sizeof(struct can_frame);
1031 dev->tx_queue_len = 10;
1033 dev->flags = IFF_NOARP;
1035 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25)
1036 #define IFF_ECHO IFF_LOOPBACK
1038 /* set flags according to driver capabilities */
1040 dev->flags |= IFF_ECHO;
1042 dev->features = NETIF_F_NO_CSUM;
1044 dev->open = can_open;
1045 dev->stop = can_close;
1046 dev->hard_start_xmit = can_start_xmit;
1047 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
1048 dev->get_stats = can_get_stats;
1051 dev->tx_timeout = can_tx_timeout;
1052 dev->watchdog_timeo = TX_TIMEOUT;
1055 static struct net_device* can_create_netdev(int dev_num, int hw_regs)
1057 struct net_device *dev;
1058 struct can_priv *priv;
1060 if (!(dev = alloc_netdev(sizeof(struct can_priv), CAN_NETDEV_NAME,
1061 can_netdev_setup))) {
1062 printk(KERN_ERR "%s: out of memory\n", CHIP_NAME);
1066 printk(KERN_INFO "%s: base 0x%lX / irq %d / speed %d / "
1067 "btr 0x%X / rx_probe %d\n",
1068 drv_name, rbase[dev_num], irq[dev_num],
1069 speed[dev_num], btr[dev_num], rx_probe[dev_num]);
1071 /* fill net_device structure */
1073 priv = netdev_priv(dev);
1075 dev->irq = irq[dev_num];
1076 dev->base_addr = rbase[dev_num];
1078 priv->speed = speed[dev_num];
1079 priv->btr = btr[dev_num];
1080 priv->rx_probe = rx_probe[dev_num];
1082 priv->hw_regs = hw_regs;
1083 priv->restart_ms = restart_ms;
1084 priv->debug = debug;
1086 init_timer(&priv->timer);
1087 priv->timer.expires = 0;
1089 if (register_netdev(dev)) {
1090 printk(KERN_INFO "%s: register netdev failed\n", CHIP_NAME);
1098 int can_set_drv_name(void)
1100 char *hname = hal_name();
1102 if (strlen(CHIP_NAME) + strlen(hname) >= DRV_NAME_LEN-1) {
1103 printk(KERN_ERR "%s: driver name too long!\n", CHIP_NAME);
1106 sprintf(drv_name, "%s-%s", CHIP_NAME, hname);
1110 static void sja1000_exit_module(void)
1114 for (i = 0; i < MAXDEV; i++) {
1115 if (can_dev[i] != NULL) {
1116 struct can_priv *priv = netdev_priv(can_dev[i]);
1117 unregister_netdev(can_dev[i]);
1118 del_timer(&priv->timer);
1120 hal_release_region(i, SJA1000_IO_SIZE_BASIC);
1121 free_netdev(can_dev[i]);
1124 can_proc_remove(drv_name);
1126 if ((ret = hal_exit()))
1127 printk(KERN_INFO "%s: hal_exit error %d.\n", drv_name, ret);
1130 static __init int sja1000_init_module(void)
1133 struct net_device *dev;
1135 if ((ret = hal_init()))
1138 if ((ret = can_set_drv_name()))
1141 if (clk < 1000 ) /* MHz command line value */
1144 if (clk < 1000000 ) /* kHz command line value */
1147 printk(KERN_INFO "%s driver v%s (%s)\n",
1148 drv_name, drv_version, drv_reldate);
1149 printk(KERN_INFO "%s - options [clk %d.%06d MHz] [restart_ms %dms]"
1151 drv_name, clk/1000000, clk%1000000, restart_ms, debug);
1154 printk(KERN_INFO "%s: loading defaults.\n", drv_name);
1158 for (i = 0; base[i]; i++) {
1159 printk(KERN_DEBUG "%s: checking for %s on address 0x%lX ...\n",
1160 drv_name, CHIP_NAME, base[i]);
1162 if (!hal_request_region(i, SJA1000_IO_SIZE_BASIC, drv_name)) {
1163 printk(KERN_ERR "%s: memory already in use\n",
1165 sja1000_exit_module();
1172 if (!sja1000_probe_chip(rbase[i])) {
1173 printk(KERN_ERR "%s: probably missing controller"
1174 " hardware\n", drv_name);
1176 hal_release_region(i, SJA1000_IO_SIZE_BASIC);
1177 sja1000_exit_module();
1181 dev = can_create_netdev(i, SJA1000_IO_SIZE_BASIC);
1185 set_reset_mode(dev);
1186 can_proc_create(drv_name);
1190 hal_release_region(i, SJA1000_IO_SIZE_BASIC);
1196 module_init(sja1000_init_module);
1197 module_exit(sja1000_exit_module);