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
45 #include <linux/module.h>
46 #include <linux/init.h>
47 #include <linux/kernel.h>
48 #include <linux/sched.h>
49 #include <linux/types.h>
50 #include <linux/fcntl.h>
51 #include <linux/interrupt.h>
52 #include <linux/ptrace.h>
53 #include <linux/string.h>
54 #include <linux/errno.h>
55 #include <linux/netdevice.h>
56 #include <linux/if_arp.h>
57 #include <linux/if_ether.h>
58 #include <linux/skbuff.h>
59 #include <linux/delay.h>
61 #include <linux/can/dev.h>
62 #include <linux/can/error.h>
63 #include <linux/can/led.h>
67 #define DRV_NAME "sja1000"
69 MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
70 MODULE_LICENSE("Dual BSD/GPL");
71 MODULE_DESCRIPTION(DRV_NAME
"CAN netdevice driver");
73 static const struct can_bittiming_const sja1000_bittiming_const
= {
85 static void sja1000_write_cmdreg(struct sja1000_priv
*priv
, u8 val
)
90 * The command register needs some locking and time to settle
91 * the write_reg() operation - especially on SMP systems.
93 spin_lock_irqsave(&priv
->cmdreg_lock
, flags
);
94 priv
->write_reg(priv
, SJA1000_CMR
, val
);
95 priv
->read_reg(priv
, SJA1000_SR
);
96 spin_unlock_irqrestore(&priv
->cmdreg_lock
, flags
);
99 static int sja1000_is_absent(struct sja1000_priv
*priv
)
101 return (priv
->read_reg(priv
, SJA1000_MOD
) == 0xFF);
104 static int sja1000_probe_chip(struct net_device
*dev
)
106 struct sja1000_priv
*priv
= netdev_priv(dev
);
108 if (priv
->reg_base
&& sja1000_is_absent(priv
)) {
109 printk(KERN_INFO
"%s: probing @0x%lX failed\n",
110 DRV_NAME
, dev
->base_addr
);
116 static void set_reset_mode(struct net_device
*dev
)
118 struct sja1000_priv
*priv
= netdev_priv(dev
);
119 unsigned char status
= priv
->read_reg(priv
, SJA1000_MOD
);
122 /* disable interrupts */
123 priv
->write_reg(priv
, SJA1000_IER
, IRQ_OFF
);
125 for (i
= 0; i
< 100; i
++) {
126 /* check reset bit */
127 if (status
& MOD_RM
) {
128 priv
->can
.state
= CAN_STATE_STOPPED
;
133 priv
->write_reg(priv
, SJA1000_MOD
, MOD_RM
);
135 status
= priv
->read_reg(priv
, SJA1000_MOD
);
138 netdev_err(dev
, "setting SJA1000 into reset mode failed!\n");
141 static void set_normal_mode(struct net_device
*dev
)
143 struct sja1000_priv
*priv
= netdev_priv(dev
);
144 unsigned char status
= priv
->read_reg(priv
, SJA1000_MOD
);
147 for (i
= 0; i
< 100; i
++) {
148 /* check reset bit */
149 if ((status
& MOD_RM
) == 0) {
150 priv
->can
.state
= CAN_STATE_ERROR_ACTIVE
;
151 /* enable interrupts */
152 if (priv
->can
.ctrlmode
& CAN_CTRLMODE_BERR_REPORTING
)
153 priv
->write_reg(priv
, SJA1000_IER
, IRQ_ALL
);
155 priv
->write_reg(priv
, SJA1000_IER
,
160 /* set chip to normal mode */
161 if (priv
->can
.ctrlmode
& CAN_CTRLMODE_LISTENONLY
)
162 priv
->write_reg(priv
, SJA1000_MOD
, MOD_LOM
);
164 priv
->write_reg(priv
, SJA1000_MOD
, 0x00);
168 status
= priv
->read_reg(priv
, SJA1000_MOD
);
171 netdev_err(dev
, "setting SJA1000 into normal mode failed!\n");
174 static void sja1000_start(struct net_device
*dev
)
176 struct sja1000_priv
*priv
= netdev_priv(dev
);
178 /* leave reset mode */
179 if (priv
->can
.state
!= CAN_STATE_STOPPED
)
182 /* Clear error counters and error code capture */
183 priv
->write_reg(priv
, SJA1000_TXERR
, 0x0);
184 priv
->write_reg(priv
, SJA1000_RXERR
, 0x0);
185 priv
->read_reg(priv
, SJA1000_ECC
);
187 /* clear interrupt flags */
188 priv
->read_reg(priv
, SJA1000_IR
);
190 /* leave reset mode */
191 set_normal_mode(dev
);
194 static int sja1000_set_mode(struct net_device
*dev
, enum can_mode mode
)
199 if (netif_queue_stopped(dev
))
200 netif_wake_queue(dev
);
210 static int sja1000_set_bittiming(struct net_device
*dev
)
212 struct sja1000_priv
*priv
= netdev_priv(dev
);
213 struct can_bittiming
*bt
= &priv
->can
.bittiming
;
216 btr0
= ((bt
->brp
- 1) & 0x3f) | (((bt
->sjw
- 1) & 0x3) << 6);
217 btr1
= ((bt
->prop_seg
+ bt
->phase_seg1
- 1) & 0xf) |
218 (((bt
->phase_seg2
- 1) & 0x7) << 4);
219 if (priv
->can
.ctrlmode
& CAN_CTRLMODE_3_SAMPLES
)
222 netdev_info(dev
, "setting BTR0=0x%02x BTR1=0x%02x\n", btr0
, btr1
);
224 priv
->write_reg(priv
, SJA1000_BTR0
, btr0
);
225 priv
->write_reg(priv
, SJA1000_BTR1
, btr1
);
230 static int sja1000_get_berr_counter(const struct net_device
*dev
,
231 struct can_berr_counter
*bec
)
233 struct sja1000_priv
*priv
= netdev_priv(dev
);
235 bec
->txerr
= priv
->read_reg(priv
, SJA1000_TXERR
);
236 bec
->rxerr
= priv
->read_reg(priv
, SJA1000_RXERR
);
242 * initialize SJA1000 chip:
246 * - enable interrupts
247 * - start operating mode
249 static void chipset_init(struct net_device
*dev
)
251 struct sja1000_priv
*priv
= netdev_priv(dev
);
253 /* set clock divider and output control register */
254 priv
->write_reg(priv
, SJA1000_CDR
, priv
->cdr
| CDR_PELICAN
);
256 /* set acceptance filter (accept all) */
257 priv
->write_reg(priv
, SJA1000_ACCC0
, 0x00);
258 priv
->write_reg(priv
, SJA1000_ACCC1
, 0x00);
259 priv
->write_reg(priv
, SJA1000_ACCC2
, 0x00);
260 priv
->write_reg(priv
, SJA1000_ACCC3
, 0x00);
262 priv
->write_reg(priv
, SJA1000_ACCM0
, 0xFF);
263 priv
->write_reg(priv
, SJA1000_ACCM1
, 0xFF);
264 priv
->write_reg(priv
, SJA1000_ACCM2
, 0xFF);
265 priv
->write_reg(priv
, SJA1000_ACCM3
, 0xFF);
267 priv
->write_reg(priv
, SJA1000_OCR
, priv
->ocr
| OCR_MODE_NORMAL
);
271 * transmit a CAN message
272 * message layout in the sk_buff should be like this:
273 * xx xx xx xx ff ll 00 11 22 33 44 55 66 77
274 * [ can-id ] [flags] [len] [can data (up to 8 bytes]
276 static netdev_tx_t
sja1000_start_xmit(struct sk_buff
*skb
,
277 struct net_device
*dev
)
279 struct sja1000_priv
*priv
= netdev_priv(dev
);
280 struct can_frame
*cf
= (struct can_frame
*)skb
->data
;
287 if (can_dropped_invalid_skb(dev
, skb
))
290 netif_stop_queue(dev
);
292 fi
= dlc
= cf
->can_dlc
;
295 if (id
& CAN_RTR_FLAG
)
296 fi
|= SJA1000_FI_RTR
;
298 if (id
& CAN_EFF_FLAG
) {
300 dreg
= SJA1000_EFF_BUF
;
301 priv
->write_reg(priv
, SJA1000_FI
, fi
);
302 priv
->write_reg(priv
, SJA1000_ID1
, (id
& 0x1fe00000) >> 21);
303 priv
->write_reg(priv
, SJA1000_ID2
, (id
& 0x001fe000) >> 13);
304 priv
->write_reg(priv
, SJA1000_ID3
, (id
& 0x00001fe0) >> 5);
305 priv
->write_reg(priv
, SJA1000_ID4
, (id
& 0x0000001f) << 3);
307 dreg
= SJA1000_SFF_BUF
;
308 priv
->write_reg(priv
, SJA1000_FI
, fi
);
309 priv
->write_reg(priv
, SJA1000_ID1
, (id
& 0x000007f8) >> 3);
310 priv
->write_reg(priv
, SJA1000_ID2
, (id
& 0x00000007) << 5);
313 for (i
= 0; i
< dlc
; i
++)
314 priv
->write_reg(priv
, dreg
++, cf
->data
[i
]);
316 can_put_echo_skb(skb
, dev
, 0);
318 if (priv
->can
.ctrlmode
& CAN_CTRLMODE_ONE_SHOT
)
319 sja1000_write_cmdreg(priv
, CMD_TR
| CMD_AT
);
321 sja1000_write_cmdreg(priv
, CMD_TR
);
326 static void sja1000_rx(struct net_device
*dev
)
328 struct sja1000_priv
*priv
= netdev_priv(dev
);
329 struct net_device_stats
*stats
= &dev
->stats
;
330 struct can_frame
*cf
;
337 /* create zero'ed CAN frame buffer */
338 skb
= alloc_can_skb(dev
, &cf
);
342 fi
= priv
->read_reg(priv
, SJA1000_FI
);
344 if (fi
& SJA1000_FI_FF
) {
345 /* extended frame format (EFF) */
346 dreg
= SJA1000_EFF_BUF
;
347 id
= (priv
->read_reg(priv
, SJA1000_ID1
) << 21)
348 | (priv
->read_reg(priv
, SJA1000_ID2
) << 13)
349 | (priv
->read_reg(priv
, SJA1000_ID3
) << 5)
350 | (priv
->read_reg(priv
, SJA1000_ID4
) >> 3);
353 /* standard frame format (SFF) */
354 dreg
= SJA1000_SFF_BUF
;
355 id
= (priv
->read_reg(priv
, SJA1000_ID1
) << 3)
356 | (priv
->read_reg(priv
, SJA1000_ID2
) >> 5);
359 cf
->can_dlc
= get_can_dlc(fi
& 0x0F);
360 if (fi
& SJA1000_FI_RTR
) {
363 for (i
= 0; i
< cf
->can_dlc
; i
++)
364 cf
->data
[i
] = priv
->read_reg(priv
, dreg
++);
369 /* release receive buffer */
370 sja1000_write_cmdreg(priv
, CMD_RRB
);
375 stats
->rx_bytes
+= cf
->can_dlc
;
377 can_led_event(dev
, CAN_LED_EVENT_RX
);
380 static int sja1000_err(struct net_device
*dev
, uint8_t isrc
, uint8_t status
)
382 struct sja1000_priv
*priv
= netdev_priv(dev
);
383 struct net_device_stats
*stats
= &dev
->stats
;
384 struct can_frame
*cf
;
386 enum can_state state
= priv
->can
.state
;
389 skb
= alloc_can_err_skb(dev
, &cf
);
393 if (isrc
& IRQ_DOI
) {
394 /* data overrun interrupt */
395 netdev_dbg(dev
, "data overrun interrupt\n");
396 cf
->can_id
|= CAN_ERR_CRTL
;
397 cf
->data
[1] = CAN_ERR_CRTL_RX_OVERFLOW
;
398 stats
->rx_over_errors
++;
400 sja1000_write_cmdreg(priv
, CMD_CDO
); /* clear bit */
404 /* error warning interrupt */
405 netdev_dbg(dev
, "error warning interrupt\n");
407 if (status
& SR_BS
) {
408 state
= CAN_STATE_BUS_OFF
;
409 cf
->can_id
|= CAN_ERR_BUSOFF
;
411 } else if (status
& SR_ES
) {
412 state
= CAN_STATE_ERROR_WARNING
;
414 state
= CAN_STATE_ERROR_ACTIVE
;
416 if (isrc
& IRQ_BEI
) {
417 /* bus error interrupt */
418 priv
->can
.can_stats
.bus_error
++;
421 ecc
= priv
->read_reg(priv
, SJA1000_ECC
);
423 cf
->can_id
|= CAN_ERR_PROT
| CAN_ERR_BUSERROR
;
425 switch (ecc
& ECC_MASK
) {
427 cf
->data
[2] |= CAN_ERR_PROT_BIT
;
430 cf
->data
[2] |= CAN_ERR_PROT_FORM
;
433 cf
->data
[2] |= CAN_ERR_PROT_STUFF
;
436 cf
->data
[2] |= CAN_ERR_PROT_UNSPEC
;
437 cf
->data
[3] = ecc
& ECC_SEG
;
440 /* Error occurred during transmission? */
441 if ((ecc
& ECC_DIR
) == 0)
442 cf
->data
[2] |= CAN_ERR_PROT_TX
;
444 if (isrc
& IRQ_EPI
) {
445 /* error passive interrupt */
446 netdev_dbg(dev
, "error passive interrupt\n");
448 state
= CAN_STATE_ERROR_PASSIVE
;
450 state
= CAN_STATE_ERROR_ACTIVE
;
452 if (isrc
& IRQ_ALI
) {
453 /* arbitration lost interrupt */
454 netdev_dbg(dev
, "arbitration lost interrupt\n");
455 alc
= priv
->read_reg(priv
, SJA1000_ALC
);
456 priv
->can
.can_stats
.arbitration_lost
++;
458 cf
->can_id
|= CAN_ERR_LOSTARB
;
459 cf
->data
[0] = alc
& 0x1f;
462 if (state
!= priv
->can
.state
&& (state
== CAN_STATE_ERROR_WARNING
||
463 state
== CAN_STATE_ERROR_PASSIVE
)) {
464 uint8_t rxerr
= priv
->read_reg(priv
, SJA1000_RXERR
);
465 uint8_t txerr
= priv
->read_reg(priv
, SJA1000_TXERR
);
466 cf
->can_id
|= CAN_ERR_CRTL
;
467 if (state
== CAN_STATE_ERROR_WARNING
) {
468 priv
->can
.can_stats
.error_warning
++;
469 cf
->data
[1] = (txerr
> rxerr
) ?
470 CAN_ERR_CRTL_TX_WARNING
:
471 CAN_ERR_CRTL_RX_WARNING
;
473 priv
->can
.can_stats
.error_passive
++;
474 cf
->data
[1] = (txerr
> rxerr
) ?
475 CAN_ERR_CRTL_TX_PASSIVE
:
476 CAN_ERR_CRTL_RX_PASSIVE
;
482 priv
->can
.state
= state
;
487 stats
->rx_bytes
+= cf
->can_dlc
;
492 irqreturn_t
sja1000_interrupt(int irq
, void *dev_id
)
494 struct net_device
*dev
= (struct net_device
*)dev_id
;
495 struct sja1000_priv
*priv
= netdev_priv(dev
);
496 struct net_device_stats
*stats
= &dev
->stats
;
497 uint8_t isrc
, status
;
503 /* Shared interrupts and IRQ off? */
504 if (priv
->read_reg(priv
, SJA1000_IER
) == IRQ_OFF
)
507 while ((isrc
= priv
->read_reg(priv
, SJA1000_IR
)) &&
508 (n
< SJA1000_MAX_IRQ
)) {
510 status
= priv
->read_reg(priv
, SJA1000_SR
);
511 /* check for absent controller due to hw unplug */
512 if (status
== 0xFF && sja1000_is_absent(priv
))
516 netdev_warn(dev
, "wakeup interrupt\n");
519 /* transmission buffer released */
520 if (priv
->can
.ctrlmode
& CAN_CTRLMODE_ONE_SHOT
&&
521 !(status
& SR_TCS
)) {
523 can_free_echo_skb(dev
, 0);
525 /* transmission complete */
527 priv
->read_reg(priv
, SJA1000_FI
) & 0xf;
529 can_get_echo_skb(dev
, 0);
531 netif_wake_queue(dev
);
532 can_led_event(dev
, CAN_LED_EVENT_TX
);
535 /* receive interrupt */
536 while (status
& SR_RBS
) {
538 status
= priv
->read_reg(priv
, SJA1000_SR
);
539 /* check for absent controller */
540 if (status
== 0xFF && sja1000_is_absent(priv
))
544 if (isrc
& (IRQ_DOI
| IRQ_EI
| IRQ_BEI
| IRQ_EPI
| IRQ_ALI
)) {
545 /* error interrupt */
546 if (sja1000_err(dev
, isrc
, status
))
553 priv
->post_irq(priv
);
555 if (n
>= SJA1000_MAX_IRQ
)
556 netdev_dbg(dev
, "%d messages handled in ISR", n
);
558 return (n
) ? IRQ_HANDLED
: IRQ_NONE
;
560 EXPORT_SYMBOL_GPL(sja1000_interrupt
);
562 static int sja1000_open(struct net_device
*dev
)
564 struct sja1000_priv
*priv
= netdev_priv(dev
);
567 /* set chip into reset mode */
571 err
= open_candev(dev
);
575 /* register interrupt handler, if not done by the device driver */
576 if (!(priv
->flags
& SJA1000_CUSTOM_IRQ_HANDLER
)) {
577 err
= request_irq(dev
->irq
, sja1000_interrupt
, priv
->irq_flags
,
578 dev
->name
, (void *)dev
);
585 /* init and start chi */
588 can_led_event(dev
, CAN_LED_EVENT_OPEN
);
590 netif_start_queue(dev
);
595 static int sja1000_close(struct net_device
*dev
)
597 struct sja1000_priv
*priv
= netdev_priv(dev
);
599 netif_stop_queue(dev
);
602 if (!(priv
->flags
& SJA1000_CUSTOM_IRQ_HANDLER
))
603 free_irq(dev
->irq
, (void *)dev
);
607 can_led_event(dev
, CAN_LED_EVENT_STOP
);
612 struct net_device
*alloc_sja1000dev(int sizeof_priv
)
614 struct net_device
*dev
;
615 struct sja1000_priv
*priv
;
617 dev
= alloc_candev(sizeof(struct sja1000_priv
) + sizeof_priv
,
618 SJA1000_ECHO_SKB_MAX
);
622 priv
= netdev_priv(dev
);
625 priv
->can
.bittiming_const
= &sja1000_bittiming_const
;
626 priv
->can
.do_set_bittiming
= sja1000_set_bittiming
;
627 priv
->can
.do_set_mode
= sja1000_set_mode
;
628 priv
->can
.do_get_berr_counter
= sja1000_get_berr_counter
;
629 priv
->can
.ctrlmode_supported
= CAN_CTRLMODE_3_SAMPLES
|
630 CAN_CTRLMODE_BERR_REPORTING
| CAN_CTRLMODE_LISTENONLY
|
631 CAN_CTRLMODE_ONE_SHOT
;
633 spin_lock_init(&priv
->cmdreg_lock
);
636 priv
->priv
= (void *)priv
+ sizeof(struct sja1000_priv
);
640 EXPORT_SYMBOL_GPL(alloc_sja1000dev
);
642 void free_sja1000dev(struct net_device
*dev
)
646 EXPORT_SYMBOL_GPL(free_sja1000dev
);
648 static const struct net_device_ops sja1000_netdev_ops
= {
649 .ndo_open
= sja1000_open
,
650 .ndo_stop
= sja1000_close
,
651 .ndo_start_xmit
= sja1000_start_xmit
,
654 int register_sja1000dev(struct net_device
*dev
)
658 if (!sja1000_probe_chip(dev
))
661 dev
->flags
|= IFF_ECHO
; /* we support local echo */
662 dev
->netdev_ops
= &sja1000_netdev_ops
;
667 ret
= register_candev(dev
);
670 devm_can_led_init(dev
);
674 EXPORT_SYMBOL_GPL(register_sja1000dev
);
676 void unregister_sja1000dev(struct net_device
*dev
)
679 unregister_candev(dev
);
681 EXPORT_SYMBOL_GPL(unregister_sja1000dev
);
683 static __init
int sja1000_init(void)
685 printk(KERN_INFO
"%s CAN netdevice driver\n", DRV_NAME
);
690 module_init(sja1000_init
);
692 static __exit
void sja1000_exit(void)
694 printk(KERN_INFO
"%s: driver removed\n", DRV_NAME
);
697 module_exit(sja1000_exit
);