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/kernel.h>
50 #include <linux/version.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>
62 #include <linux/delay.h>
64 #include <linux/can.h>
65 #include <linux/can/dev.h>
66 #include <linux/can/error.h>
67 #include <linux/can/dev.h>
71 #include <linux/can/version.h> /* for RCSID. Removed by mkpatch script */
72 RCSID("$Id: sja1000.c 531 2007-10-19 07:38:29Z hartkopp $");
74 #define DRV_NAME "sja1000"
76 MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
77 MODULE_LICENSE("Dual BSD/GPL");
78 MODULE_DESCRIPTION(DRV_NAME " CAN netdevice driver");
80 static struct can_bittiming_const sja1000_bittiming_const = {
91 static int sja1000_probe_chip(struct net_device *dev)
93 struct sja1000_priv *priv = netdev_priv(dev);
95 if (dev->base_addr && (priv->read_reg(dev, 0) == 0xFF)) {
96 printk(KERN_INFO "%s: probing @0x%lX failed\n",
97 DRV_NAME, dev->base_addr);
103 static int set_reset_mode(struct net_device *dev)
105 struct sja1000_priv *priv = netdev_priv(dev);
106 unsigned char status = priv->read_reg(dev, REG_MOD);
109 /* disable interrupts */
110 priv->write_reg(dev, REG_IER, IRQ_OFF);
112 for (i = 0; i < 100; i++) {
113 /* check reset bit */
114 if (status & MOD_RM) {
115 priv->can.state = CAN_STATE_STOPPED;
119 priv->write_reg(dev, REG_MOD, MOD_RM); /* reset chip */
120 status = priv->read_reg(dev, REG_MOD);
124 dev_err(ND2D(dev), "setting SJA1000 into reset mode failed!\n");
129 static int set_normal_mode(struct net_device *dev)
131 struct sja1000_priv *priv = netdev_priv(dev);
132 unsigned char status = priv->read_reg(dev, REG_MOD);
135 for (i = 0; i < 100; i++) {
136 /* check reset bit */
137 if ((status & MOD_RM) == 0) {
138 priv->can.state = CAN_STATE_ACTIVE;
139 /* enable all interrupts */
140 priv->write_reg(dev, REG_IER, IRQ_ALL);
145 /* set chip to normal mode */
146 priv->write_reg(dev, REG_MOD, 0x00);
147 status = priv->read_reg(dev, REG_MOD);
151 dev_err(ND2D(dev), "setting SJA1000 into normal mode failed!\n");
156 static void sja1000_start(struct net_device *dev)
158 struct sja1000_priv *priv = netdev_priv(dev);
160 /* leave reset mode */
161 if (priv->can.state != CAN_STATE_STOPPED)
164 /* Clear error counters and error code capture */
165 priv->write_reg(dev, REG_TXERR, 0x0);
166 priv->write_reg(dev, REG_RXERR, 0x0);
167 priv->read_reg(dev, REG_ECC);
169 /* leave reset mode */
170 set_normal_mode(dev);
173 static int sja1000_set_mode(struct net_device *dev, enum can_mode mode)
175 struct sja1000_priv *priv = netdev_priv(dev);
179 if (!priv->open_time)
183 if (netif_queue_stopped(dev))
184 netif_wake_queue(dev);
194 static int sja1000_get_state(struct net_device *dev, enum can_state *state)
196 struct sja1000_priv *priv = netdev_priv(dev);
199 /* FIXME: inspecting the status register to get the current state
200 * is not really necessary, because state changes are handled by
201 * in the ISR and the variable priv->can.state gets updated. The
202 * CAN devicde interface needs fixing!
205 spin_lock_irq(&priv->can.irq_lock);
207 if (priv->can.state == CAN_STATE_STOPPED) {
208 *state = CAN_STATE_STOPPED;
210 status = priv->read_reg(dev, REG_SR);
212 *state = CAN_STATE_BUS_OFF;
213 else if (status & SR_ES) {
214 if (priv->read_reg(dev, REG_TXERR) > 127 ||
215 priv->read_reg(dev, REG_RXERR) > 127)
216 *state = CAN_STATE_BUS_PASSIVE;
218 *state = CAN_STATE_BUS_WARNING;
220 *state = CAN_STATE_ACTIVE;
223 if (*state != priv->can.state)
225 "Oops, state mismatch: hard %d != soft %d\n",
226 *state, priv->can.state);
227 spin_unlock_irq(&priv->can.irq_lock);
231 static int sja1000_set_bittiming(struct net_device *dev)
233 struct sja1000_priv *priv = netdev_priv(dev);
234 struct can_bittiming *bt = &priv->can.bittiming;
237 btr0 = ((bt->brp - 1) & 0x3f) | (((bt->sjw - 1) & 0x3) << 6);
238 btr1 = ((bt->prop_seg + bt->phase_seg1 - 1) & 0xf) |
239 (((bt->phase_seg2 - 1) & 0x7) << 4) |
240 ((priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES) << 7);
242 dev_info(ND2D(dev), "setting BTR0=0x%02x BTR1=0x%02x\n", btr0, btr1);
244 priv->write_reg(dev, REG_BTR0, btr0);
245 priv->write_reg(dev, REG_BTR1, btr1);
251 * initialize SJA1000 chip:
255 * - enable interrupts
256 * - start operating mode
258 static void chipset_init(struct net_device *dev)
260 struct sja1000_priv *priv = netdev_priv(dev);
262 /* set clock divider and output control register */
263 priv->write_reg(dev, REG_CDR, priv->cdr | CDR_PELICAN);
265 /* set acceptance filter (accept all) */
266 priv->write_reg(dev, REG_ACCC0, 0x00);
267 priv->write_reg(dev, REG_ACCC1, 0x00);
268 priv->write_reg(dev, REG_ACCC2, 0x00);
269 priv->write_reg(dev, REG_ACCC3, 0x00);
271 priv->write_reg(dev, REG_ACCM0, 0xFF);
272 priv->write_reg(dev, REG_ACCM1, 0xFF);
273 priv->write_reg(dev, REG_ACCM2, 0xFF);
274 priv->write_reg(dev, REG_ACCM3, 0xFF);
276 priv->write_reg(dev, REG_OCR, priv->ocr | OCR_MODE_NORMAL);
280 * transmit a CAN message
281 * message layout in the sk_buff should be like this:
282 * xx xx xx xx ff ll 00 11 22 33 44 55 66 77
283 * [ can-id ] [flags] [len] [can data (up to 8 bytes]
285 static int sja1000_start_xmit(struct sk_buff *skb, struct net_device *dev)
287 struct sja1000_priv *priv = netdev_priv(dev);
288 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
289 struct net_device_stats *stats = can_get_stats(dev);
291 struct net_device_stats *stats = &dev->stats;
293 struct can_frame *cf = (struct can_frame *)skb->data;
300 netif_stop_queue(dev);
302 fi = dlc = cf->can_dlc;
305 if (id & CAN_RTR_FLAG)
308 if (id & CAN_EFF_FLAG) {
311 priv->write_reg(dev, REG_FI, fi);
312 priv->write_reg(dev, REG_ID1, (id & 0x1fe00000) >> (5 + 16));
313 priv->write_reg(dev, REG_ID2, (id & 0x001fe000) >> (5 + 8));
314 priv->write_reg(dev, REG_ID3, (id & 0x00001fe0) >> 5);
315 priv->write_reg(dev, REG_ID4, (id & 0x0000001f) << 3);
318 priv->write_reg(dev, REG_FI, fi);
319 priv->write_reg(dev, REG_ID1, (id & 0x000007f8) >> 3);
320 priv->write_reg(dev, REG_ID2, (id & 0x00000007) << 5);
323 for (i = 0; i < dlc; i++)
324 priv->write_reg(dev, dreg++, cf->data[i]);
326 stats->tx_bytes += dlc;
327 dev->trans_start = jiffies;
329 can_put_echo_skb(skb, dev, 0);
331 priv->write_reg(dev, REG_CMR, CMD_TR);
336 static void sja1000_rx(struct net_device *dev)
338 struct sja1000_priv *priv = netdev_priv(dev);
339 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
340 struct net_device_stats *stats = can_get_stats(dev);
342 struct net_device_stats *stats = &dev->stats;
344 struct can_frame *cf;
352 skb = dev_alloc_skb(sizeof(struct can_frame));
356 skb->protocol = htons(ETH_P_CAN);
358 fi = priv->read_reg(dev, REG_FI);
362 /* extended frame format (EFF) */
364 id = (priv->read_reg(dev, REG_ID1) << (5 + 16))
365 | (priv->read_reg(dev, REG_ID2) << (5 + 8))
366 | (priv->read_reg(dev, REG_ID3) << 5)
367 | (priv->read_reg(dev, REG_ID4) >> 3);
370 /* standard frame format (SFF) */
372 id = (priv->read_reg(dev, REG_ID1) << 3)
373 | (priv->read_reg(dev, REG_ID2) >> 5);
379 cf = (struct can_frame *)skb_put(skb, sizeof(struct can_frame));
380 memset(cf, 0, sizeof(struct can_frame));
383 for (i = 0; i < dlc; i++)
384 cf->data[i] = priv->read_reg(dev, dreg++);
389 /* release receive buffer */
390 priv->write_reg(dev, REG_CMR, CMD_RRB);
394 dev->last_rx = jiffies;
396 stats->rx_bytes += dlc;
399 static int sja1000_err(struct net_device *dev, uint8_t isrc, uint8_t status)
401 struct sja1000_priv *priv = netdev_priv(dev);
402 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
403 struct net_device_stats *stats = can_get_stats(dev);
405 struct net_device_stats *stats = &dev->stats;
407 struct can_frame *cf;
409 enum can_state state = priv->can.state;
412 skb = dev_alloc_skb(sizeof(struct can_frame));
416 skb->protocol = htons(ETH_P_CAN);
417 cf = (struct can_frame *)skb_put(skb, sizeof(struct can_frame));
418 memset(cf, 0, sizeof(struct can_frame));
419 cf->can_id = CAN_ERR_FLAG;
420 cf->can_dlc = CAN_ERR_DLC;
422 if (isrc & IRQ_DOI) {
423 /* data overrun interrupt */
424 dev_dbg(ND2D(dev), "data overrun interrupt\n");
425 cf->can_id |= CAN_ERR_CRTL;
426 cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
427 priv->can.can_stats.data_overrun++;
428 priv->write_reg(dev, REG_CMR, CMD_CDO); /* clear bit */
432 /* error warning interrupt */
433 priv->can.can_stats.error_warning++;
434 dev_dbg(ND2D(dev), "error warning interrupt\n");
436 if (status & SR_BS) {
437 state = CAN_STATE_BUS_OFF;
438 cf->can_id |= CAN_ERR_BUSOFF;
440 } else if (status & SR_ES) {
441 state = CAN_STATE_BUS_WARNING;
443 state = CAN_STATE_ACTIVE;
445 if (isrc & IRQ_BEI) {
446 /* bus error interrupt */
447 priv->can.can_stats.bus_error++;
448 ecc = priv->read_reg(dev, REG_ECC);
450 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
452 switch (ecc & ECC_MASK) {
454 cf->data[2] |= CAN_ERR_PROT_BIT;
457 cf->data[2] |= CAN_ERR_PROT_FORM;
460 cf->data[2] |= CAN_ERR_PROT_STUFF;
463 cf->data[2] |= CAN_ERR_PROT_UNSPEC;
464 cf->data[3] = ecc & ECC_SEG;
467 /* Error occured during transmission? */
468 if ((ecc & ECC_DIR) == 0)
469 cf->data[2] |= CAN_ERR_PROT_TX;
471 if (isrc & IRQ_EPI) {
472 /* error passive interrupt */
473 dev_dbg(ND2D(dev), "error passive interrupt\n");
474 priv->can.can_stats.error_passive++;
476 state = CAN_STATE_BUS_PASSIVE;
478 state = CAN_STATE_ACTIVE;
480 if (isrc & IRQ_ALI) {
481 /* arbitration lost interrupt */
482 dev_dbg(ND2D(dev), "arbitration lost interrupt\n");
483 alc = priv->read_reg(dev, REG_ALC);
484 priv->can.can_stats.arbitration_lost++;
485 cf->can_id |= CAN_ERR_LOSTARB;
486 cf->data[0] = alc & 0x1f;
489 if (state != priv->can.state && (state == CAN_STATE_BUS_WARNING ||
490 state == CAN_STATE_BUS_PASSIVE)) {
491 uint8_t rxerr = priv->read_reg(dev, REG_RXERR);
492 uint8_t txerr = priv->read_reg(dev, REG_TXERR);
493 cf->can_id |= CAN_ERR_CRTL;
494 if (state == CAN_STATE_BUS_WARNING)
495 cf->data[1] = (txerr > rxerr) ?
496 CAN_ERR_CRTL_TX_WARNING :
497 CAN_ERR_CRTL_RX_WARNING;
499 cf->data[1] = (txerr > rxerr) ?
500 CAN_ERR_CRTL_TX_PASSIVE :
501 CAN_ERR_CRTL_RX_PASSIVE;
504 priv->can.state = state;
508 dev->last_rx = jiffies;
510 stats->rx_bytes += cf->can_dlc;
515 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
516 irqreturn_t sja1000_interrupt(int irq, void *dev_id, struct pt_regs *regs)
518 irqreturn_t sja1000_interrupt(int irq, void *dev_id)
521 struct net_device *dev = (struct net_device *)dev_id;
522 struct sja1000_priv *priv = netdev_priv(dev);
523 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
524 struct net_device_stats *stats = can_get_stats(dev);
526 struct net_device_stats *stats = &dev->stats;
528 uint8_t isrc, status;
531 /* Shared interrupts and IRQ off? */
532 if (priv->read_reg(dev, REG_IER) == IRQ_OFF)
538 while ((isrc = priv->read_reg(dev, REG_IR)) && (n < SJA1000_MAX_IRQ)) {
540 status = priv->read_reg(dev, REG_SR);
542 if (isrc & IRQ_WUI) {
543 /* wake-up interrupt */
544 priv->can.can_stats.wakeup++;
547 /* transmission complete interrupt */
549 can_get_echo_skb(dev, 0);
550 netif_wake_queue(dev);
553 /* receive interrupt */
554 while (status & SR_RBS) {
556 status = priv->read_reg(dev, REG_SR);
559 if (isrc & (IRQ_DOI | IRQ_EI | IRQ_BEI | IRQ_EPI | IRQ_ALI)) {
560 /* error interrupt */
561 if (sja1000_err(dev, isrc, status))
569 if (n >= SJA1000_MAX_IRQ)
570 dev_dbg(ND2D(dev), "%d messages handled in ISR", n);
572 return (n) ? IRQ_HANDLED : IRQ_NONE;
574 EXPORT_SYMBOL_GPL(sja1000_interrupt);
576 static int sja1000_open(struct net_device *dev)
578 struct sja1000_priv *priv = netdev_priv(dev);
581 /* set chip into reset mode */
584 /* determine and set bittime */
585 err = can_set_bittiming(dev);
589 /* register interrupt handler, if not done by the device driver */
590 if (!(priv->flags & SJA1000_CUSTOM_IRQ_HANDLER)) {
591 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18)
592 err = request_irq(dev->irq, &sja1000_interrupt, SA_SHIRQ,
593 dev->name, (void *)dev);
595 err = request_irq(dev->irq, &sja1000_interrupt, IRQF_SHARED,
596 dev->name, (void *)dev);
602 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
603 /* clear statistics */
604 memset(&priv->can.net_stats, 0, sizeof(priv->can.net_stats));
607 /* init and start chi */
609 priv->open_time = jiffies;
611 netif_start_queue(dev);
616 static int sja1000_close(struct net_device *dev)
618 struct sja1000_priv *priv = netdev_priv(dev);
621 netif_stop_queue(dev);
623 can_close_cleanup(dev);
625 if (!(priv->flags & SJA1000_CUSTOM_IRQ_HANDLER))
626 free_irq(dev->irq, (void *)dev);
631 struct net_device *alloc_sja1000dev(int sizeof_priv)
633 struct net_device *dev;
634 struct sja1000_priv *priv;
636 dev = alloc_candev(sizeof(struct sja1000_priv) + sizeof_priv);
640 priv = netdev_priv(dev);
644 priv->priv = (void *)priv + sizeof(struct sja1000_priv);
648 EXPORT_SYMBOL_GPL(alloc_sja1000dev);
650 void free_sja1000dev(struct net_device *dev)
654 EXPORT_SYMBOL_GPL(free_sja1000dev);
656 int register_sja1000dev(struct net_device *dev)
658 struct sja1000_priv *priv = netdev_priv(dev);
661 if (!sja1000_probe_chip(dev))
664 dev->flags |= IFF_ECHO; /* we support local echo */
666 dev->open = sja1000_open;
667 dev->stop = sja1000_close;
669 dev->hard_start_xmit = sja1000_start_xmit;
671 priv->can.bittiming_const = &sja1000_bittiming_const;
672 priv->can.do_set_bittiming = sja1000_set_bittiming;
673 priv->can.do_get_state = sja1000_get_state;
674 priv->can.do_set_mode = sja1000_set_mode;
677 err = register_candev(dev);
680 "%s: registering netdev failed\n", DRV_NAME);
689 EXPORT_SYMBOL_GPL(register_sja1000dev);
691 void unregister_sja1000dev(struct net_device *dev)
694 unregister_candev(dev);
696 EXPORT_SYMBOL_GPL(unregister_sja1000dev);
698 static __init int sja1000_init(void)
700 printk(KERN_INFO "%s CAN netdevice driver\n", DRV_NAME);
705 module_init(sja1000_init);
707 static __exit void sja1000_exit(void)
709 printk(KERN_INFO "%s: driver removed\n", DRV_NAME);
712 module_exit(sja1000_exit);