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 <socketcan/can.h>
65 #include <socketcan/can/dev.h>
66 #include <socketcan/can/error.h>
67 #include <socketcan/can/dev.h>
71 #include <socketcan/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 = {
92 static int sja1000_probe_chip(struct net_device *dev)
94 struct sja1000_priv *priv = netdev_priv(dev);
96 if (priv->reg_base && (priv->read_reg(priv, 0) == 0xFF)) {
97 dev_info(ND2D(dev), "probing @0x%p failed\n",
104 static void set_reset_mode(struct net_device *dev)
106 struct sja1000_priv *priv = netdev_priv(dev);
107 unsigned char status = priv->read_reg(priv, REG_MOD);
110 /* disable interrupts */
111 priv->write_reg(priv, REG_IER, IRQ_OFF);
113 for (i = 0; i < 100; i++) {
114 /* check reset bit */
115 if (status & MOD_RM) {
116 priv->can.state = CAN_STATE_STOPPED;
120 priv->write_reg(priv, REG_MOD, MOD_RM); /* reset chip */
122 status = priv->read_reg(priv, REG_MOD);
125 dev_err(ND2D(dev), "setting SJA1000 into reset mode failed!\n");
128 static void set_normal_mode(struct net_device *dev)
130 struct sja1000_priv *priv = netdev_priv(dev);
131 unsigned char status = priv->read_reg(priv, REG_MOD);
134 for (i = 0; i < 100; i++) {
135 /* check reset bit */
136 if ((status & MOD_RM) == 0) {
137 priv->can.state = CAN_STATE_ERROR_ACTIVE;
138 /* enable all interrupts */
139 priv->write_reg(priv, REG_IER, IRQ_ALL);
143 /* set chip to normal mode */
144 priv->write_reg(priv, REG_MOD, 0x00);
146 status = priv->read_reg(priv, REG_MOD);
149 dev_err(ND2D(dev), "setting SJA1000 into normal mode failed!\n");
152 static void sja1000_start(struct net_device *dev)
154 struct sja1000_priv *priv = netdev_priv(dev);
156 /* leave reset mode */
157 if (priv->can.state != CAN_STATE_STOPPED)
160 /* Clear error counters and error code capture */
161 priv->write_reg(priv, REG_TXERR, 0x0);
162 priv->write_reg(priv, REG_RXERR, 0x0);
163 priv->read_reg(priv, REG_ECC);
165 /* leave reset mode */
166 set_normal_mode(dev);
169 static int sja1000_set_mode(struct net_device *dev, enum can_mode mode)
171 struct sja1000_priv *priv = netdev_priv(dev);
173 if (!priv->open_time)
179 if (netif_queue_stopped(dev))
180 netif_wake_queue(dev);
190 static int sja1000_set_bittiming(struct net_device *dev)
192 struct sja1000_priv *priv = netdev_priv(dev);
193 struct can_bittiming *bt = &priv->can.bittiming;
196 btr0 = ((bt->brp - 1) & 0x3f) | (((bt->sjw - 1) & 0x3) << 6);
197 btr1 = ((bt->prop_seg + bt->phase_seg1 - 1) & 0xf) |
198 (((bt->phase_seg2 - 1) & 0x7) << 4);
199 if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
203 "setting BTR0=0x%02x BTR1=0x%02x\n", btr0, btr1);
205 priv->write_reg(priv, REG_BTR0, btr0);
206 priv->write_reg(priv, REG_BTR1, btr1);
212 * initialize SJA1000 chip:
216 * - enable interrupts
217 * - start operating mode
219 static void chipset_init(struct net_device *dev)
221 struct sja1000_priv *priv = netdev_priv(dev);
223 /* set clock divider and output control register */
224 priv->write_reg(priv, REG_CDR, priv->cdr | CDR_PELICAN);
226 /* set acceptance filter (accept all) */
227 priv->write_reg(priv, REG_ACCC0, 0x00);
228 priv->write_reg(priv, REG_ACCC1, 0x00);
229 priv->write_reg(priv, REG_ACCC2, 0x00);
230 priv->write_reg(priv, REG_ACCC3, 0x00);
232 priv->write_reg(priv, REG_ACCM0, 0xFF);
233 priv->write_reg(priv, REG_ACCM1, 0xFF);
234 priv->write_reg(priv, REG_ACCM2, 0xFF);
235 priv->write_reg(priv, REG_ACCM3, 0xFF);
237 priv->write_reg(priv, REG_OCR, priv->ocr | OCR_MODE_NORMAL);
241 * transmit a CAN message
242 * message layout in the sk_buff should be like this:
243 * xx xx xx xx ff ll 00 11 22 33 44 55 66 77
244 * [ can-id ] [flags] [len] [can data (up to 8 bytes]
246 static int sja1000_start_xmit(struct sk_buff *skb, struct net_device *dev)
248 struct sja1000_priv *priv = netdev_priv(dev);
249 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
250 struct net_device_stats *stats = can_get_stats(dev);
252 struct net_device_stats *stats = &dev->stats;
254 struct can_frame *cf = (struct can_frame *)skb->data;
261 netif_stop_queue(dev);
263 fi = dlc = cf->can_dlc;
266 if (id & CAN_RTR_FLAG)
269 if (id & CAN_EFF_FLAG) {
272 priv->write_reg(priv, REG_FI, fi);
273 priv->write_reg(priv, REG_ID1, (id & 0x1fe00000) >> (5 + 16));
274 priv->write_reg(priv, REG_ID2, (id & 0x001fe000) >> (5 + 8));
275 priv->write_reg(priv, REG_ID3, (id & 0x00001fe0) >> 5);
276 priv->write_reg(priv, REG_ID4, (id & 0x0000001f) << 3);
279 priv->write_reg(priv, REG_FI, fi);
280 priv->write_reg(priv, REG_ID1, (id & 0x000007f8) >> 3);
281 priv->write_reg(priv, REG_ID2, (id & 0x00000007) << 5);
284 for (i = 0; i < dlc; i++)
285 priv->write_reg(priv, dreg++, cf->data[i]);
287 stats->tx_bytes += dlc;
288 dev->trans_start = jiffies;
290 can_put_echo_skb(skb, dev, 0);
292 priv->write_reg(priv, REG_CMR, CMD_TR);
297 static void sja1000_rx(struct net_device *dev)
299 struct sja1000_priv *priv = netdev_priv(dev);
300 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
301 struct net_device_stats *stats = can_get_stats(dev);
303 struct net_device_stats *stats = &dev->stats;
305 struct can_frame *cf;
313 skb = dev_alloc_skb(sizeof(struct can_frame));
317 skb->protocol = htons(ETH_P_CAN);
319 fi = priv->read_reg(priv, REG_FI);
323 /* extended frame format (EFF) */
325 id = (priv->read_reg(priv, REG_ID1) << (5 + 16))
326 | (priv->read_reg(priv, REG_ID2) << (5 + 8))
327 | (priv->read_reg(priv, REG_ID3) << 5)
328 | (priv->read_reg(priv, REG_ID4) >> 3);
331 /* standard frame format (SFF) */
333 id = (priv->read_reg(priv, REG_ID1) << 3)
334 | (priv->read_reg(priv, REG_ID2) >> 5);
340 cf = (struct can_frame *)skb_put(skb, sizeof(struct can_frame));
341 memset(cf, 0, sizeof(struct can_frame));
344 for (i = 0; i < dlc; i++)
345 cf->data[i] = priv->read_reg(priv, dreg++);
350 /* release receive buffer */
351 priv->write_reg(priv, REG_CMR, CMD_RRB);
355 dev->last_rx = jiffies;
357 stats->rx_bytes += dlc;
360 static int sja1000_err(struct net_device *dev, uint8_t isrc, uint8_t status)
362 struct sja1000_priv *priv = netdev_priv(dev);
363 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
364 struct net_device_stats *stats = can_get_stats(dev);
366 struct net_device_stats *stats = &dev->stats;
368 struct can_frame *cf;
370 enum can_state state = priv->can.state;
373 skb = dev_alloc_skb(sizeof(struct can_frame));
377 skb->protocol = htons(ETH_P_CAN);
378 cf = (struct can_frame *)skb_put(skb, sizeof(struct can_frame));
379 memset(cf, 0, sizeof(struct can_frame));
380 cf->can_id = CAN_ERR_FLAG;
381 cf->can_dlc = CAN_ERR_DLC;
383 if (isrc & IRQ_DOI) {
384 /* data overrun interrupt */
385 dev_dbg(ND2D(dev), "data overrun interrupt\n");
386 cf->can_id |= CAN_ERR_CRTL;
387 cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
388 stats->rx_over_errors++;
390 priv->write_reg(priv, REG_CMR, CMD_CDO); /* clear bit */
394 /* error warning interrupt */
395 dev_dbg(ND2D(dev), "error warning interrupt\n");
397 if (status & SR_BS) {
398 state = CAN_STATE_BUS_OFF;
399 cf->can_id |= CAN_ERR_BUSOFF;
401 } else if (status & SR_ES) {
402 state = CAN_STATE_ERROR_WARNING;
404 state = CAN_STATE_ERROR_ACTIVE;
406 if (isrc & IRQ_BEI) {
407 /* bus error interrupt */
408 priv->can.can_stats.bus_error++;
411 ecc = priv->read_reg(priv, REG_ECC);
413 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
415 switch (ecc & ECC_MASK) {
417 cf->data[2] |= CAN_ERR_PROT_BIT;
420 cf->data[2] |= CAN_ERR_PROT_FORM;
423 cf->data[2] |= CAN_ERR_PROT_STUFF;
426 cf->data[2] |= CAN_ERR_PROT_UNSPEC;
427 cf->data[3] = ecc & ECC_SEG;
430 /* Error occured during transmission? */
431 if ((ecc & ECC_DIR) == 0)
432 cf->data[2] |= CAN_ERR_PROT_TX;
434 if (isrc & IRQ_EPI) {
435 /* error passive interrupt */
436 dev_dbg(ND2D(dev), "error passive interrupt\n");
438 state = CAN_STATE_ERROR_PASSIVE;
440 state = CAN_STATE_ERROR_ACTIVE;
442 if (isrc & IRQ_ALI) {
443 /* arbitration lost interrupt */
444 dev_dbg(ND2D(dev), "arbitration lost interrupt\n");
445 alc = priv->read_reg(priv, REG_ALC);
446 priv->can.can_stats.arbitration_lost++;
448 cf->can_id |= CAN_ERR_LOSTARB;
449 cf->data[0] = alc & 0x1f;
452 if (state != priv->can.state && (state == CAN_STATE_ERROR_WARNING ||
453 state == CAN_STATE_ERROR_PASSIVE)) {
454 uint8_t rxerr = priv->read_reg(priv, REG_RXERR);
455 uint8_t txerr = priv->read_reg(priv, REG_TXERR);
456 cf->can_id |= CAN_ERR_CRTL;
457 if (state == CAN_STATE_ERROR_WARNING) {
458 priv->can.can_stats.error_warning++;
459 cf->data[1] = (txerr > rxerr) ?
460 CAN_ERR_CRTL_TX_WARNING :
461 CAN_ERR_CRTL_RX_WARNING;
463 priv->can.can_stats.error_passive++;
464 cf->data[1] = (txerr > rxerr) ?
465 CAN_ERR_CRTL_TX_PASSIVE :
466 CAN_ERR_CRTL_RX_PASSIVE;
470 priv->can.state = state;
474 dev->last_rx = jiffies;
476 stats->rx_bytes += cf->can_dlc;
481 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
482 irqreturn_t sja1000_interrupt(int irq, void *dev_id, struct pt_regs *regs)
484 irqreturn_t sja1000_interrupt(int irq, void *dev_id)
487 struct net_device *dev = (struct net_device *)dev_id;
488 struct sja1000_priv *priv = netdev_priv(dev);
489 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
490 struct net_device_stats *stats = can_get_stats(dev);
492 struct net_device_stats *stats = &dev->stats;
494 uint8_t isrc, status;
497 /* Shared interrupts and IRQ off? */
498 if (priv->read_reg(priv, REG_IER) == IRQ_OFF)
504 while ((isrc = priv->read_reg(priv, REG_IR)) && (n < SJA1000_MAX_IRQ)) {
506 status = priv->read_reg(priv, REG_SR);
509 dev_warn(ND2D(dev), "wakeup interrupt\n");
512 /* transmission complete interrupt */
514 can_get_echo_skb(dev, 0);
515 netif_wake_queue(dev);
518 /* receive interrupt */
519 while (status & SR_RBS) {
521 status = priv->read_reg(priv, REG_SR);
524 if (isrc & (IRQ_DOI | IRQ_EI | IRQ_BEI | IRQ_EPI | IRQ_ALI)) {
525 /* error interrupt */
526 if (sja1000_err(dev, isrc, status))
532 priv->post_irq(priv);
534 if (n >= SJA1000_MAX_IRQ)
535 dev_dbg(ND2D(dev), "%d messages handled in ISR", n);
537 return (n) ? IRQ_HANDLED : IRQ_NONE;
539 EXPORT_SYMBOL_GPL(sja1000_interrupt);
541 static int sja1000_open(struct net_device *dev)
543 struct sja1000_priv *priv = netdev_priv(dev);
546 /* set chip into reset mode */
550 err = open_candev(dev);
554 /* register interrupt handler, if not done by the device driver */
555 if (!(priv->flags & SJA1000_CUSTOM_IRQ_HANDLER)) {
556 err = request_irq(dev->irq, &sja1000_interrupt, priv->irq_flags,
557 dev->name, (void *)dev);
564 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
565 /* clear statistics */
566 memset(&priv->can.net_stats, 0, sizeof(priv->can.net_stats));
569 /* init and start chi */
571 priv->open_time = jiffies;
573 netif_start_queue(dev);
578 static int sja1000_close(struct net_device *dev)
580 struct sja1000_priv *priv = netdev_priv(dev);
582 netif_stop_queue(dev);
585 if (!(priv->flags & SJA1000_CUSTOM_IRQ_HANDLER))
586 free_irq(dev->irq, (void *)dev);
595 struct net_device *alloc_sja1000dev(int sizeof_priv)
597 struct net_device *dev;
598 struct sja1000_priv *priv;
600 dev = alloc_candev(sizeof(struct sja1000_priv) + sizeof_priv);
604 priv = netdev_priv(dev);
607 priv->can.bittiming_const = &sja1000_bittiming_const;
608 priv->can.do_set_bittiming = sja1000_set_bittiming;
609 priv->can.do_set_mode = sja1000_set_mode;
612 priv->priv = (void *)priv + sizeof(struct sja1000_priv);
616 EXPORT_SYMBOL_GPL(alloc_sja1000dev);
618 void free_sja1000dev(struct net_device *dev)
622 EXPORT_SYMBOL_GPL(free_sja1000dev);
624 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,28)
625 static const struct net_device_ops sja1000_netdev_ops = {
626 .ndo_open = sja1000_open,
627 .ndo_stop = sja1000_close,
628 .ndo_start_xmit = sja1000_start_xmit,
632 int register_sja1000dev(struct net_device *dev)
634 if (!sja1000_probe_chip(dev))
637 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,28)
638 dev->netdev_ops = &sja1000_netdev_ops;
640 dev->open = sja1000_open;
641 dev->stop = sja1000_close;
642 dev->hard_start_xmit = sja1000_start_xmit;
645 dev->flags |= IFF_ECHO; /* we support local echo */
650 return register_candev(dev);
652 EXPORT_SYMBOL_GPL(register_sja1000dev);
654 void unregister_sja1000dev(struct net_device *dev)
657 unregister_candev(dev);
659 EXPORT_SYMBOL_GPL(unregister_sja1000dev);
661 static __init int sja1000_init(void)
663 printk(KERN_INFO "%s CAN netdevice driver\n", DRV_NAME);
668 module_init(sja1000_init);
670 static __exit void sja1000_exit(void)
672 printk(KERN_INFO "%s: driver removed\n", DRV_NAME);
675 module_exit(sja1000_exit);