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/sched.h>
51 #include <linux/types.h>
52 #include <linux/fcntl.h>
53 #include <linux/interrupt.h>
54 #include <linux/ptrace.h>
55 #include <linux/string.h>
56 #include <linux/errno.h>
57 #include <linux/netdevice.h>
58 #include <linux/if_arp.h>
59 #include <linux/if_ether.h>
60 #include <linux/skbuff.h>
61 #include <linux/delay.h>
63 #include <linux/can/dev.h>
64 #include <linux/can/error.h>
68 #define DRV_NAME "sja1000"
70 MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
71 MODULE_LICENSE("Dual BSD/GPL");
72 MODULE_DESCRIPTION(DRV_NAME
"CAN netdevice driver");
74 static struct can_bittiming_const sja1000_bittiming_const
= {
86 static void sja1000_write_cmdreg(struct sja1000_priv
*priv
, u8 val
)
91 * The command register needs some locking and time to settle
92 * the write_reg() operation - especially on SMP systems.
94 spin_lock_irqsave(&priv
->cmdreg_lock
, flags
);
95 priv
->write_reg(priv
, REG_CMR
, val
);
96 priv
->read_reg(priv
, REG_SR
);
97 spin_unlock_irqrestore(&priv
->cmdreg_lock
, flags
);
100 static int sja1000_probe_chip(struct net_device
*dev
)
102 struct sja1000_priv
*priv
= netdev_priv(dev
);
104 if (priv
->reg_base
&& (priv
->read_reg(priv
, 0) == 0xFF)) {
105 printk(KERN_INFO
"%s: probing @0x%lX failed\n",
106 DRV_NAME
, dev
->base_addr
);
112 static void set_reset_mode(struct net_device
*dev
)
114 struct sja1000_priv
*priv
= netdev_priv(dev
);
115 unsigned char status
= priv
->read_reg(priv
, REG_MOD
);
118 /* disable interrupts */
119 priv
->write_reg(priv
, REG_IER
, IRQ_OFF
);
121 for (i
= 0; i
< 100; i
++) {
122 /* check reset bit */
123 if (status
& MOD_RM
) {
124 priv
->can
.state
= CAN_STATE_STOPPED
;
128 priv
->write_reg(priv
, REG_MOD
, MOD_RM
); /* reset chip */
130 status
= priv
->read_reg(priv
, REG_MOD
);
133 dev_err(dev
->dev
.parent
, "setting SJA1000 into reset mode failed!\n");
136 static void set_normal_mode(struct net_device
*dev
)
138 struct sja1000_priv
*priv
= netdev_priv(dev
);
139 unsigned char status
= priv
->read_reg(priv
, REG_MOD
);
142 for (i
= 0; i
< 100; i
++) {
143 /* check reset bit */
144 if ((status
& MOD_RM
) == 0) {
145 priv
->can
.state
= CAN_STATE_ERROR_ACTIVE
;
146 /* enable interrupts */
147 if (priv
->can
.ctrlmode
& CAN_CTRLMODE_BERR_REPORTING
)
148 priv
->write_reg(priv
, REG_IER
, IRQ_ALL
);
150 priv
->write_reg(priv
, REG_IER
,
155 /* set chip to normal mode */
156 priv
->write_reg(priv
, REG_MOD
, 0x00);
158 status
= priv
->read_reg(priv
, REG_MOD
);
161 dev_err(dev
->dev
.parent
, "setting SJA1000 into normal mode failed!\n");
164 static void sja1000_start(struct net_device
*dev
)
166 struct sja1000_priv
*priv
= netdev_priv(dev
);
168 /* leave reset mode */
169 if (priv
->can
.state
!= CAN_STATE_STOPPED
)
172 /* Clear error counters and error code capture */
173 priv
->write_reg(priv
, REG_TXERR
, 0x0);
174 priv
->write_reg(priv
, REG_RXERR
, 0x0);
175 priv
->read_reg(priv
, REG_ECC
);
177 /* leave reset mode */
178 set_normal_mode(dev
);
181 static int sja1000_set_mode(struct net_device
*dev
, enum can_mode mode
)
183 struct sja1000_priv
*priv
= netdev_priv(dev
);
185 if (!priv
->open_time
)
191 if (netif_queue_stopped(dev
))
192 netif_wake_queue(dev
);
202 static int sja1000_set_bittiming(struct net_device
*dev
)
204 struct sja1000_priv
*priv
= netdev_priv(dev
);
205 struct can_bittiming
*bt
= &priv
->can
.bittiming
;
208 btr0
= ((bt
->brp
- 1) & 0x3f) | (((bt
->sjw
- 1) & 0x3) << 6);
209 btr1
= ((bt
->prop_seg
+ bt
->phase_seg1
- 1) & 0xf) |
210 (((bt
->phase_seg2
- 1) & 0x7) << 4);
211 if (priv
->can
.ctrlmode
& CAN_CTRLMODE_3_SAMPLES
)
214 dev_info(dev
->dev
.parent
,
215 "setting BTR0=0x%02x BTR1=0x%02x\n", btr0
, btr1
);
217 priv
->write_reg(priv
, REG_BTR0
, btr0
);
218 priv
->write_reg(priv
, REG_BTR1
, btr1
);
223 static int sja1000_get_berr_counter(const struct net_device
*dev
,
224 struct can_berr_counter
*bec
)
226 struct sja1000_priv
*priv
= netdev_priv(dev
);
228 bec
->txerr
= priv
->read_reg(priv
, REG_TXERR
);
229 bec
->rxerr
= priv
->read_reg(priv
, REG_RXERR
);
235 * initialize SJA1000 chip:
239 * - enable interrupts
240 * - start operating mode
242 static void chipset_init(struct net_device
*dev
)
244 struct sja1000_priv
*priv
= netdev_priv(dev
);
246 /* set clock divider and output control register */
247 priv
->write_reg(priv
, REG_CDR
, priv
->cdr
| CDR_PELICAN
);
249 /* set acceptance filter (accept all) */
250 priv
->write_reg(priv
, REG_ACCC0
, 0x00);
251 priv
->write_reg(priv
, REG_ACCC1
, 0x00);
252 priv
->write_reg(priv
, REG_ACCC2
, 0x00);
253 priv
->write_reg(priv
, REG_ACCC3
, 0x00);
255 priv
->write_reg(priv
, REG_ACCM0
, 0xFF);
256 priv
->write_reg(priv
, REG_ACCM1
, 0xFF);
257 priv
->write_reg(priv
, REG_ACCM2
, 0xFF);
258 priv
->write_reg(priv
, REG_ACCM3
, 0xFF);
260 priv
->write_reg(priv
, REG_OCR
, priv
->ocr
| OCR_MODE_NORMAL
);
264 * transmit a CAN message
265 * message layout in the sk_buff should be like this:
266 * xx xx xx xx ff ll 00 11 22 33 44 55 66 77
267 * [ can-id ] [flags] [len] [can data (up to 8 bytes]
269 static netdev_tx_t
sja1000_start_xmit(struct sk_buff
*skb
,
270 struct net_device
*dev
)
272 struct sja1000_priv
*priv
= netdev_priv(dev
);
273 struct can_frame
*cf
= (struct can_frame
*)skb
->data
;
280 if (can_dropped_invalid_skb(dev
, skb
))
283 netif_stop_queue(dev
);
285 fi
= dlc
= cf
->can_dlc
;
288 if (id
& CAN_RTR_FLAG
)
291 if (id
& CAN_EFF_FLAG
) {
294 priv
->write_reg(priv
, REG_FI
, fi
);
295 priv
->write_reg(priv
, REG_ID1
, (id
& 0x1fe00000) >> (5 + 16));
296 priv
->write_reg(priv
, REG_ID2
, (id
& 0x001fe000) >> (5 + 8));
297 priv
->write_reg(priv
, REG_ID3
, (id
& 0x00001fe0) >> 5);
298 priv
->write_reg(priv
, REG_ID4
, (id
& 0x0000001f) << 3);
301 priv
->write_reg(priv
, REG_FI
, fi
);
302 priv
->write_reg(priv
, REG_ID1
, (id
& 0x000007f8) >> 3);
303 priv
->write_reg(priv
, REG_ID2
, (id
& 0x00000007) << 5);
306 for (i
= 0; i
< dlc
; i
++)
307 priv
->write_reg(priv
, dreg
++, cf
->data
[i
]);
309 can_put_echo_skb(skb
, dev
, 0);
311 sja1000_write_cmdreg(priv
, CMD_TR
);
316 static void sja1000_rx(struct net_device
*dev
)
318 struct sja1000_priv
*priv
= netdev_priv(dev
);
319 struct net_device_stats
*stats
= &dev
->stats
;
320 struct can_frame
*cf
;
327 /* create zero'ed CAN frame buffer */
328 skb
= alloc_can_skb(dev
, &cf
);
332 fi
= priv
->read_reg(priv
, REG_FI
);
335 /* extended frame format (EFF) */
337 id
= (priv
->read_reg(priv
, REG_ID1
) << (5 + 16))
338 | (priv
->read_reg(priv
, REG_ID2
) << (5 + 8))
339 | (priv
->read_reg(priv
, REG_ID3
) << 5)
340 | (priv
->read_reg(priv
, REG_ID4
) >> 3);
343 /* standard frame format (SFF) */
345 id
= (priv
->read_reg(priv
, REG_ID1
) << 3)
346 | (priv
->read_reg(priv
, REG_ID2
) >> 5);
349 cf
->can_dlc
= get_can_dlc(fi
& 0x0F);
353 for (i
= 0; i
< cf
->can_dlc
; i
++)
354 cf
->data
[i
] = priv
->read_reg(priv
, dreg
++);
359 /* release receive buffer */
360 sja1000_write_cmdreg(priv
, CMD_RRB
);
365 stats
->rx_bytes
+= cf
->can_dlc
;
368 static int sja1000_err(struct net_device
*dev
, uint8_t isrc
, uint8_t status
)
370 struct sja1000_priv
*priv
= netdev_priv(dev
);
371 struct net_device_stats
*stats
= &dev
->stats
;
372 struct can_frame
*cf
;
374 enum can_state state
= priv
->can
.state
;
377 skb
= alloc_can_err_skb(dev
, &cf
);
381 if (isrc
& IRQ_DOI
) {
382 /* data overrun interrupt */
383 dev_dbg(dev
->dev
.parent
, "data overrun interrupt\n");
384 cf
->can_id
|= CAN_ERR_CRTL
;
385 cf
->data
[1] = CAN_ERR_CRTL_RX_OVERFLOW
;
386 stats
->rx_over_errors
++;
388 sja1000_write_cmdreg(priv
, CMD_CDO
); /* clear bit */
392 /* error warning interrupt */
393 dev_dbg(dev
->dev
.parent
, "error warning interrupt\n");
395 if (status
& SR_BS
) {
396 state
= CAN_STATE_BUS_OFF
;
397 cf
->can_id
|= CAN_ERR_BUSOFF
;
399 } else if (status
& SR_ES
) {
400 state
= CAN_STATE_ERROR_WARNING
;
402 state
= CAN_STATE_ERROR_ACTIVE
;
404 if (isrc
& IRQ_BEI
) {
405 /* bus error interrupt */
406 priv
->can
.can_stats
.bus_error
++;
409 ecc
= priv
->read_reg(priv
, REG_ECC
);
411 cf
->can_id
|= CAN_ERR_PROT
| CAN_ERR_BUSERROR
;
413 switch (ecc
& ECC_MASK
) {
415 cf
->data
[2] |= CAN_ERR_PROT_BIT
;
418 cf
->data
[2] |= CAN_ERR_PROT_FORM
;
421 cf
->data
[2] |= CAN_ERR_PROT_STUFF
;
424 cf
->data
[2] |= CAN_ERR_PROT_UNSPEC
;
425 cf
->data
[3] = ecc
& ECC_SEG
;
428 /* Error occurred during transmission? */
429 if ((ecc
& ECC_DIR
) == 0)
430 cf
->data
[2] |= CAN_ERR_PROT_TX
;
432 if (isrc
& IRQ_EPI
) {
433 /* error passive interrupt */
434 dev_dbg(dev
->dev
.parent
, "error passive interrupt\n");
436 state
= CAN_STATE_ERROR_PASSIVE
;
438 state
= CAN_STATE_ERROR_ACTIVE
;
440 if (isrc
& IRQ_ALI
) {
441 /* arbitration lost interrupt */
442 dev_dbg(dev
->dev
.parent
, "arbitration lost interrupt\n");
443 alc
= priv
->read_reg(priv
, REG_ALC
);
444 priv
->can
.can_stats
.arbitration_lost
++;
446 cf
->can_id
|= CAN_ERR_LOSTARB
;
447 cf
->data
[0] = alc
& 0x1f;
450 if (state
!= priv
->can
.state
&& (state
== CAN_STATE_ERROR_WARNING
||
451 state
== CAN_STATE_ERROR_PASSIVE
)) {
452 uint8_t rxerr
= priv
->read_reg(priv
, REG_RXERR
);
453 uint8_t txerr
= priv
->read_reg(priv
, REG_TXERR
);
454 cf
->can_id
|= CAN_ERR_CRTL
;
455 if (state
== CAN_STATE_ERROR_WARNING
) {
456 priv
->can
.can_stats
.error_warning
++;
457 cf
->data
[1] = (txerr
> rxerr
) ?
458 CAN_ERR_CRTL_TX_WARNING
:
459 CAN_ERR_CRTL_RX_WARNING
;
461 priv
->can
.can_stats
.error_passive
++;
462 cf
->data
[1] = (txerr
> rxerr
) ?
463 CAN_ERR_CRTL_TX_PASSIVE
:
464 CAN_ERR_CRTL_RX_PASSIVE
;
470 priv
->can
.state
= state
;
475 stats
->rx_bytes
+= cf
->can_dlc
;
480 irqreturn_t
sja1000_interrupt(int irq
, void *dev_id
)
482 struct net_device
*dev
= (struct net_device
*)dev_id
;
483 struct sja1000_priv
*priv
= netdev_priv(dev
);
484 struct net_device_stats
*stats
= &dev
->stats
;
485 uint8_t isrc
, status
;
488 /* Shared interrupts and IRQ off? */
489 if (priv
->read_reg(priv
, REG_IER
) == IRQ_OFF
)
495 while ((isrc
= priv
->read_reg(priv
, REG_IR
)) && (n
< SJA1000_MAX_IRQ
)) {
497 status
= priv
->read_reg(priv
, REG_SR
);
500 dev_warn(dev
->dev
.parent
, "wakeup interrupt\n");
503 /* transmission complete interrupt */
504 stats
->tx_bytes
+= priv
->read_reg(priv
, REG_FI
) & 0xf;
506 can_get_echo_skb(dev
, 0);
507 netif_wake_queue(dev
);
510 /* receive interrupt */
511 while (status
& SR_RBS
) {
513 status
= priv
->read_reg(priv
, REG_SR
);
516 if (isrc
& (IRQ_DOI
| IRQ_EI
| IRQ_BEI
| IRQ_EPI
| IRQ_ALI
)) {
517 /* error interrupt */
518 if (sja1000_err(dev
, isrc
, status
))
524 priv
->post_irq(priv
);
526 if (n
>= SJA1000_MAX_IRQ
)
527 dev_dbg(dev
->dev
.parent
, "%d messages handled in ISR", n
);
529 return (n
) ? IRQ_HANDLED
: IRQ_NONE
;
531 EXPORT_SYMBOL_GPL(sja1000_interrupt
);
533 static int sja1000_open(struct net_device
*dev
)
535 struct sja1000_priv
*priv
= netdev_priv(dev
);
538 /* set chip into reset mode */
542 err
= open_candev(dev
);
546 /* register interrupt handler, if not done by the device driver */
547 if (!(priv
->flags
& SJA1000_CUSTOM_IRQ_HANDLER
)) {
548 err
= request_irq(dev
->irq
, sja1000_interrupt
, priv
->irq_flags
,
549 dev
->name
, (void *)dev
);
556 /* init and start chi */
558 priv
->open_time
= jiffies
;
560 netif_start_queue(dev
);
565 static int sja1000_close(struct net_device
*dev
)
567 struct sja1000_priv
*priv
= netdev_priv(dev
);
569 netif_stop_queue(dev
);
572 if (!(priv
->flags
& SJA1000_CUSTOM_IRQ_HANDLER
))
573 free_irq(dev
->irq
, (void *)dev
);
582 struct net_device
*alloc_sja1000dev(int sizeof_priv
)
584 struct net_device
*dev
;
585 struct sja1000_priv
*priv
;
587 dev
= alloc_candev(sizeof(struct sja1000_priv
) + sizeof_priv
,
588 SJA1000_ECHO_SKB_MAX
);
592 priv
= netdev_priv(dev
);
595 priv
->can
.bittiming_const
= &sja1000_bittiming_const
;
596 priv
->can
.do_set_bittiming
= sja1000_set_bittiming
;
597 priv
->can
.do_set_mode
= sja1000_set_mode
;
598 priv
->can
.do_get_berr_counter
= sja1000_get_berr_counter
;
599 priv
->can
.ctrlmode_supported
= CAN_CTRLMODE_3_SAMPLES
|
600 CAN_CTRLMODE_BERR_REPORTING
;
602 spin_lock_init(&priv
->cmdreg_lock
);
605 priv
->priv
= (void *)priv
+ sizeof(struct sja1000_priv
);
609 EXPORT_SYMBOL_GPL(alloc_sja1000dev
);
611 void free_sja1000dev(struct net_device
*dev
)
615 EXPORT_SYMBOL_GPL(free_sja1000dev
);
617 static const struct net_device_ops sja1000_netdev_ops
= {
618 .ndo_open
= sja1000_open
,
619 .ndo_stop
= sja1000_close
,
620 .ndo_start_xmit
= sja1000_start_xmit
,
623 int register_sja1000dev(struct net_device
*dev
)
625 if (!sja1000_probe_chip(dev
))
628 dev
->flags
|= IFF_ECHO
; /* we support local echo */
629 dev
->netdev_ops
= &sja1000_netdev_ops
;
634 return register_candev(dev
);
636 EXPORT_SYMBOL_GPL(register_sja1000dev
);
638 void unregister_sja1000dev(struct net_device
*dev
)
641 unregister_candev(dev
);
643 EXPORT_SYMBOL_GPL(unregister_sja1000dev
);
645 static __init
int sja1000_init(void)
647 printk(KERN_INFO
"%s CAN netdevice driver\n", DRV_NAME
);
652 module_init(sja1000_init
);
654 static __exit
void sja1000_exit(void)
656 printk(KERN_INFO
"%s: driver removed\n", DRV_NAME
);
659 module_exit(sja1000_exit
);