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 netdev_err(dev
, "probing failed\n");
115 static void set_reset_mode(struct net_device
*dev
)
117 struct sja1000_priv
*priv
= netdev_priv(dev
);
118 unsigned char status
= priv
->read_reg(priv
, SJA1000_MOD
);
121 /* disable interrupts */
122 priv
->write_reg(priv
, SJA1000_IER
, IRQ_OFF
);
124 for (i
= 0; i
< 100; i
++) {
125 /* check reset bit */
126 if (status
& MOD_RM
) {
127 priv
->can
.state
= CAN_STATE_STOPPED
;
132 priv
->write_reg(priv
, SJA1000_MOD
, MOD_RM
);
134 status
= priv
->read_reg(priv
, SJA1000_MOD
);
137 netdev_err(dev
, "setting SJA1000 into reset mode failed!\n");
140 static void set_normal_mode(struct net_device
*dev
)
142 struct sja1000_priv
*priv
= netdev_priv(dev
);
143 unsigned char status
= priv
->read_reg(priv
, SJA1000_MOD
);
144 u8 mod_reg_val
= 0x00;
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 mod_reg_val
|= MOD_LOM
;
163 if (priv
->can
.ctrlmode
& CAN_CTRLMODE_PRESUME_ACK
)
164 mod_reg_val
|= MOD_STM
;
165 priv
->write_reg(priv
, SJA1000_MOD
, mod_reg_val
);
169 status
= priv
->read_reg(priv
, SJA1000_MOD
);
172 netdev_err(dev
, "setting SJA1000 into normal mode failed!\n");
176 * initialize SJA1000 chip:
180 * - enable interrupts
181 * - start operating mode
183 static void chipset_init(struct net_device
*dev
)
185 struct sja1000_priv
*priv
= netdev_priv(dev
);
187 /* set clock divider and output control register */
188 priv
->write_reg(priv
, SJA1000_CDR
, priv
->cdr
| CDR_PELICAN
);
190 /* set acceptance filter (accept all) */
191 priv
->write_reg(priv
, SJA1000_ACCC0
, 0x00);
192 priv
->write_reg(priv
, SJA1000_ACCC1
, 0x00);
193 priv
->write_reg(priv
, SJA1000_ACCC2
, 0x00);
194 priv
->write_reg(priv
, SJA1000_ACCC3
, 0x00);
196 priv
->write_reg(priv
, SJA1000_ACCM0
, 0xFF);
197 priv
->write_reg(priv
, SJA1000_ACCM1
, 0xFF);
198 priv
->write_reg(priv
, SJA1000_ACCM2
, 0xFF);
199 priv
->write_reg(priv
, SJA1000_ACCM3
, 0xFF);
201 priv
->write_reg(priv
, SJA1000_OCR
, priv
->ocr
| OCR_MODE_NORMAL
);
204 static void sja1000_start(struct net_device
*dev
)
206 struct sja1000_priv
*priv
= netdev_priv(dev
);
208 /* leave reset mode */
209 if (priv
->can
.state
!= CAN_STATE_STOPPED
)
212 /* Initialize chip if uninitialized at this stage */
213 if (!(priv
->read_reg(priv
, SJA1000_CDR
) & CDR_PELICAN
))
216 /* Clear error counters and error code capture */
217 priv
->write_reg(priv
, SJA1000_TXERR
, 0x0);
218 priv
->write_reg(priv
, SJA1000_RXERR
, 0x0);
219 priv
->read_reg(priv
, SJA1000_ECC
);
221 /* clear interrupt flags */
222 priv
->read_reg(priv
, SJA1000_IR
);
224 /* leave reset mode */
225 set_normal_mode(dev
);
228 static int sja1000_set_mode(struct net_device
*dev
, enum can_mode mode
)
233 if (netif_queue_stopped(dev
))
234 netif_wake_queue(dev
);
244 static int sja1000_set_bittiming(struct net_device
*dev
)
246 struct sja1000_priv
*priv
= netdev_priv(dev
);
247 struct can_bittiming
*bt
= &priv
->can
.bittiming
;
250 btr0
= ((bt
->brp
- 1) & 0x3f) | (((bt
->sjw
- 1) & 0x3) << 6);
251 btr1
= ((bt
->prop_seg
+ bt
->phase_seg1
- 1) & 0xf) |
252 (((bt
->phase_seg2
- 1) & 0x7) << 4);
253 if (priv
->can
.ctrlmode
& CAN_CTRLMODE_3_SAMPLES
)
256 netdev_info(dev
, "setting BTR0=0x%02x BTR1=0x%02x\n", btr0
, btr1
);
258 priv
->write_reg(priv
, SJA1000_BTR0
, btr0
);
259 priv
->write_reg(priv
, SJA1000_BTR1
, btr1
);
264 static int sja1000_get_berr_counter(const struct net_device
*dev
,
265 struct can_berr_counter
*bec
)
267 struct sja1000_priv
*priv
= netdev_priv(dev
);
269 bec
->txerr
= priv
->read_reg(priv
, SJA1000_TXERR
);
270 bec
->rxerr
= priv
->read_reg(priv
, SJA1000_RXERR
);
276 * transmit a CAN message
277 * message layout in the sk_buff should be like this:
278 * xx xx xx xx ff ll 00 11 22 33 44 55 66 77
279 * [ can-id ] [flags] [len] [can data (up to 8 bytes]
281 static netdev_tx_t
sja1000_start_xmit(struct sk_buff
*skb
,
282 struct net_device
*dev
)
284 struct sja1000_priv
*priv
= netdev_priv(dev
);
285 struct can_frame
*cf
= (struct can_frame
*)skb
->data
;
290 u8 cmd_reg_val
= 0x00;
293 if (can_dropped_invalid_skb(dev
, skb
))
296 netif_stop_queue(dev
);
298 fi
= dlc
= cf
->can_dlc
;
301 if (id
& CAN_RTR_FLAG
)
302 fi
|= SJA1000_FI_RTR
;
304 if (id
& CAN_EFF_FLAG
) {
306 dreg
= SJA1000_EFF_BUF
;
307 priv
->write_reg(priv
, SJA1000_FI
, fi
);
308 priv
->write_reg(priv
, SJA1000_ID1
, (id
& 0x1fe00000) >> 21);
309 priv
->write_reg(priv
, SJA1000_ID2
, (id
& 0x001fe000) >> 13);
310 priv
->write_reg(priv
, SJA1000_ID3
, (id
& 0x00001fe0) >> 5);
311 priv
->write_reg(priv
, SJA1000_ID4
, (id
& 0x0000001f) << 3);
313 dreg
= SJA1000_SFF_BUF
;
314 priv
->write_reg(priv
, SJA1000_FI
, fi
);
315 priv
->write_reg(priv
, SJA1000_ID1
, (id
& 0x000007f8) >> 3);
316 priv
->write_reg(priv
, SJA1000_ID2
, (id
& 0x00000007) << 5);
319 for (i
= 0; i
< dlc
; i
++)
320 priv
->write_reg(priv
, dreg
++, cf
->data
[i
]);
322 can_put_echo_skb(skb
, dev
, 0);
324 if (priv
->can
.ctrlmode
& CAN_CTRLMODE_ONE_SHOT
)
325 cmd_reg_val
|= CMD_AT
;
327 if (priv
->can
.ctrlmode
& CAN_CTRLMODE_LOOPBACK
)
328 cmd_reg_val
|= CMD_SRR
;
330 cmd_reg_val
|= CMD_TR
;
332 sja1000_write_cmdreg(priv
, cmd_reg_val
);
337 static void sja1000_rx(struct net_device
*dev
)
339 struct sja1000_priv
*priv
= netdev_priv(dev
);
340 struct net_device_stats
*stats
= &dev
->stats
;
341 struct can_frame
*cf
;
348 /* create zero'ed CAN frame buffer */
349 skb
= alloc_can_skb(dev
, &cf
);
353 fi
= priv
->read_reg(priv
, SJA1000_FI
);
355 if (fi
& SJA1000_FI_FF
) {
356 /* extended frame format (EFF) */
357 dreg
= SJA1000_EFF_BUF
;
358 id
= (priv
->read_reg(priv
, SJA1000_ID1
) << 21)
359 | (priv
->read_reg(priv
, SJA1000_ID2
) << 13)
360 | (priv
->read_reg(priv
, SJA1000_ID3
) << 5)
361 | (priv
->read_reg(priv
, SJA1000_ID4
) >> 3);
364 /* standard frame format (SFF) */
365 dreg
= SJA1000_SFF_BUF
;
366 id
= (priv
->read_reg(priv
, SJA1000_ID1
) << 3)
367 | (priv
->read_reg(priv
, SJA1000_ID2
) >> 5);
370 cf
->can_dlc
= get_can_dlc(fi
& 0x0F);
371 if (fi
& SJA1000_FI_RTR
) {
374 for (i
= 0; i
< cf
->can_dlc
; i
++)
375 cf
->data
[i
] = priv
->read_reg(priv
, dreg
++);
380 /* release receive buffer */
381 sja1000_write_cmdreg(priv
, CMD_RRB
);
384 stats
->rx_bytes
+= cf
->can_dlc
;
387 can_led_event(dev
, CAN_LED_EVENT_RX
);
390 static int sja1000_err(struct net_device
*dev
, uint8_t isrc
, uint8_t status
)
392 struct sja1000_priv
*priv
= netdev_priv(dev
);
393 struct net_device_stats
*stats
= &dev
->stats
;
394 struct can_frame
*cf
;
396 enum can_state state
= priv
->can
.state
;
397 enum can_state rx_state
, tx_state
;
398 unsigned int rxerr
, txerr
;
401 skb
= alloc_can_err_skb(dev
, &cf
);
405 txerr
= priv
->read_reg(priv
, SJA1000_TXERR
);
406 rxerr
= priv
->read_reg(priv
, SJA1000_RXERR
);
411 if (isrc
& IRQ_DOI
) {
412 /* data overrun interrupt */
413 netdev_dbg(dev
, "data overrun interrupt\n");
414 cf
->can_id
|= CAN_ERR_CRTL
;
415 cf
->data
[1] = CAN_ERR_CRTL_RX_OVERFLOW
;
416 stats
->rx_over_errors
++;
418 sja1000_write_cmdreg(priv
, CMD_CDO
); /* clear bit */
422 /* error warning interrupt */
423 netdev_dbg(dev
, "error warning interrupt\n");
426 state
= CAN_STATE_BUS_OFF
;
427 else if (status
& SR_ES
)
428 state
= CAN_STATE_ERROR_WARNING
;
430 state
= CAN_STATE_ERROR_ACTIVE
;
432 if (isrc
& IRQ_BEI
) {
433 /* bus error interrupt */
434 priv
->can
.can_stats
.bus_error
++;
437 ecc
= priv
->read_reg(priv
, SJA1000_ECC
);
439 cf
->can_id
|= CAN_ERR_PROT
| CAN_ERR_BUSERROR
;
442 switch (ecc
& ECC_MASK
) {
444 cf
->data
[2] |= CAN_ERR_PROT_BIT
;
447 cf
->data
[2] |= CAN_ERR_PROT_FORM
;
450 cf
->data
[2] |= CAN_ERR_PROT_STUFF
;
456 /* set error location */
457 cf
->data
[3] = ecc
& ECC_SEG
;
459 /* Error occurred during transmission? */
460 if ((ecc
& ECC_DIR
) == 0)
461 cf
->data
[2] |= CAN_ERR_PROT_TX
;
463 if (isrc
& IRQ_EPI
) {
464 /* error passive interrupt */
465 netdev_dbg(dev
, "error passive interrupt\n");
467 if (state
== CAN_STATE_ERROR_PASSIVE
)
468 state
= CAN_STATE_ERROR_WARNING
;
470 state
= CAN_STATE_ERROR_PASSIVE
;
472 if (isrc
& IRQ_ALI
) {
473 /* arbitration lost interrupt */
474 netdev_dbg(dev
, "arbitration lost interrupt\n");
475 alc
= priv
->read_reg(priv
, SJA1000_ALC
);
476 priv
->can
.can_stats
.arbitration_lost
++;
478 cf
->can_id
|= CAN_ERR_LOSTARB
;
479 cf
->data
[0] = alc
& 0x1f;
482 if (state
!= priv
->can
.state
) {
483 tx_state
= txerr
>= rxerr
? state
: 0;
484 rx_state
= txerr
<= rxerr
? state
: 0;
486 can_change_state(dev
, cf
, tx_state
, rx_state
);
488 if(state
== CAN_STATE_BUS_OFF
)
493 stats
->rx_bytes
+= cf
->can_dlc
;
499 irqreturn_t
sja1000_interrupt(int irq
, void *dev_id
)
501 struct net_device
*dev
= (struct net_device
*)dev_id
;
502 struct sja1000_priv
*priv
= netdev_priv(dev
);
503 struct net_device_stats
*stats
= &dev
->stats
;
504 uint8_t isrc
, status
;
510 /* Shared interrupts and IRQ off? */
511 if (priv
->read_reg(priv
, SJA1000_IER
) == IRQ_OFF
)
514 while ((isrc
= priv
->read_reg(priv
, SJA1000_IR
)) &&
515 (n
< SJA1000_MAX_IRQ
)) {
517 status
= priv
->read_reg(priv
, SJA1000_SR
);
518 /* check for absent controller due to hw unplug */
519 if (status
== 0xFF && sja1000_is_absent(priv
))
523 netdev_warn(dev
, "wakeup interrupt\n");
526 /* transmission buffer released */
527 if (priv
->can
.ctrlmode
& CAN_CTRLMODE_ONE_SHOT
&&
528 !(status
& SR_TCS
)) {
530 can_free_echo_skb(dev
, 0);
532 /* transmission complete */
534 priv
->read_reg(priv
, SJA1000_FI
) & 0xf;
536 can_get_echo_skb(dev
, 0);
538 netif_wake_queue(dev
);
539 can_led_event(dev
, CAN_LED_EVENT_TX
);
542 /* receive interrupt */
543 while (status
& SR_RBS
) {
545 status
= priv
->read_reg(priv
, SJA1000_SR
);
546 /* check for absent controller */
547 if (status
== 0xFF && sja1000_is_absent(priv
))
551 if (isrc
& (IRQ_DOI
| IRQ_EI
| IRQ_BEI
| IRQ_EPI
| IRQ_ALI
)) {
552 /* error interrupt */
553 if (sja1000_err(dev
, isrc
, status
))
560 priv
->post_irq(priv
);
562 if (n
>= SJA1000_MAX_IRQ
)
563 netdev_dbg(dev
, "%d messages handled in ISR", n
);
565 return (n
) ? IRQ_HANDLED
: IRQ_NONE
;
567 EXPORT_SYMBOL_GPL(sja1000_interrupt
);
569 static int sja1000_open(struct net_device
*dev
)
571 struct sja1000_priv
*priv
= netdev_priv(dev
);
574 /* set chip into reset mode */
578 err
= open_candev(dev
);
582 /* register interrupt handler, if not done by the device driver */
583 if (!(priv
->flags
& SJA1000_CUSTOM_IRQ_HANDLER
)) {
584 err
= request_irq(dev
->irq
, sja1000_interrupt
, priv
->irq_flags
,
585 dev
->name
, (void *)dev
);
592 /* init and start chi */
595 can_led_event(dev
, CAN_LED_EVENT_OPEN
);
597 netif_start_queue(dev
);
602 static int sja1000_close(struct net_device
*dev
)
604 struct sja1000_priv
*priv
= netdev_priv(dev
);
606 netif_stop_queue(dev
);
609 if (!(priv
->flags
& SJA1000_CUSTOM_IRQ_HANDLER
))
610 free_irq(dev
->irq
, (void *)dev
);
614 can_led_event(dev
, CAN_LED_EVENT_STOP
);
619 struct net_device
*alloc_sja1000dev(int sizeof_priv
)
621 struct net_device
*dev
;
622 struct sja1000_priv
*priv
;
624 dev
= alloc_candev(sizeof(struct sja1000_priv
) + sizeof_priv
,
625 SJA1000_ECHO_SKB_MAX
);
629 priv
= netdev_priv(dev
);
632 priv
->can
.bittiming_const
= &sja1000_bittiming_const
;
633 priv
->can
.do_set_bittiming
= sja1000_set_bittiming
;
634 priv
->can
.do_set_mode
= sja1000_set_mode
;
635 priv
->can
.do_get_berr_counter
= sja1000_get_berr_counter
;
636 priv
->can
.ctrlmode_supported
= CAN_CTRLMODE_LOOPBACK
|
637 CAN_CTRLMODE_LISTENONLY
|
638 CAN_CTRLMODE_3_SAMPLES
|
639 CAN_CTRLMODE_ONE_SHOT
|
640 CAN_CTRLMODE_BERR_REPORTING
|
641 CAN_CTRLMODE_PRESUME_ACK
;
643 spin_lock_init(&priv
->cmdreg_lock
);
646 priv
->priv
= (void *)priv
+ sizeof(struct sja1000_priv
);
650 EXPORT_SYMBOL_GPL(alloc_sja1000dev
);
652 void free_sja1000dev(struct net_device
*dev
)
656 EXPORT_SYMBOL_GPL(free_sja1000dev
);
658 static const struct net_device_ops sja1000_netdev_ops
= {
659 .ndo_open
= sja1000_open
,
660 .ndo_stop
= sja1000_close
,
661 .ndo_start_xmit
= sja1000_start_xmit
,
662 .ndo_change_mtu
= can_change_mtu
,
665 int register_sja1000dev(struct net_device
*dev
)
669 if (!sja1000_probe_chip(dev
))
672 dev
->flags
|= IFF_ECHO
; /* we support local echo */
673 dev
->netdev_ops
= &sja1000_netdev_ops
;
678 ret
= register_candev(dev
);
681 devm_can_led_init(dev
);
685 EXPORT_SYMBOL_GPL(register_sja1000dev
);
687 void unregister_sja1000dev(struct net_device
*dev
)
690 unregister_candev(dev
);
692 EXPORT_SYMBOL_GPL(unregister_sja1000dev
);
694 static __init
int sja1000_init(void)
696 printk(KERN_INFO
"%s CAN netdevice driver\n", DRV_NAME
);
701 module_init(sja1000_init
);
703 static __exit
void sja1000_exit(void)
705 printk(KERN_INFO
"%s: driver removed\n", DRV_NAME
);
708 module_exit(sja1000_exit
);