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.h>
64 #include <linux/can/dev.h>
65 #include <linux/can/error.h>
66 #include <linux/can/dev.h>
70 #define DRV_NAME "sja1000"
72 MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
73 MODULE_LICENSE("Dual BSD/GPL");
74 MODULE_DESCRIPTION(DRV_NAME
"CAN netdevice driver");
76 static struct can_bittiming_const sja1000_bittiming_const
= {
88 static int sja1000_probe_chip(struct net_device
*dev
)
90 struct sja1000_priv
*priv
= netdev_priv(dev
);
92 if (priv
->reg_base
&& (priv
->read_reg(priv
, 0) == 0xFF)) {
93 printk(KERN_INFO
"%s: probing @0x%lX failed\n",
94 DRV_NAME
, dev
->base_addr
);
100 static void set_reset_mode(struct net_device
*dev
)
102 struct sja1000_priv
*priv
= netdev_priv(dev
);
103 unsigned char status
= priv
->read_reg(priv
, REG_MOD
);
106 /* disable interrupts */
107 priv
->write_reg(priv
, REG_IER
, IRQ_OFF
);
109 for (i
= 0; i
< 100; i
++) {
110 /* check reset bit */
111 if (status
& MOD_RM
) {
112 priv
->can
.state
= CAN_STATE_STOPPED
;
116 priv
->write_reg(priv
, REG_MOD
, MOD_RM
); /* reset chip */
118 status
= priv
->read_reg(priv
, REG_MOD
);
121 dev_err(dev
->dev
.parent
, "setting SJA1000 into reset mode failed!\n");
124 static void set_normal_mode(struct net_device
*dev
)
126 struct sja1000_priv
*priv
= netdev_priv(dev
);
127 unsigned char status
= priv
->read_reg(priv
, REG_MOD
);
130 for (i
= 0; i
< 100; i
++) {
131 /* check reset bit */
132 if ((status
& MOD_RM
) == 0) {
133 priv
->can
.state
= CAN_STATE_ERROR_ACTIVE
;
134 /* enable all interrupts */
135 priv
->write_reg(priv
, REG_IER
, IRQ_ALL
);
139 /* set chip to normal mode */
140 priv
->write_reg(priv
, REG_MOD
, 0x00);
142 status
= priv
->read_reg(priv
, REG_MOD
);
145 dev_err(dev
->dev
.parent
, "setting SJA1000 into normal mode failed!\n");
148 static void sja1000_start(struct net_device
*dev
)
150 struct sja1000_priv
*priv
= netdev_priv(dev
);
152 /* leave reset mode */
153 if (priv
->can
.state
!= CAN_STATE_STOPPED
)
156 /* Clear error counters and error code capture */
157 priv
->write_reg(priv
, REG_TXERR
, 0x0);
158 priv
->write_reg(priv
, REG_RXERR
, 0x0);
159 priv
->read_reg(priv
, REG_ECC
);
161 /* leave reset mode */
162 set_normal_mode(dev
);
165 static int sja1000_set_mode(struct net_device
*dev
, enum can_mode mode
)
167 struct sja1000_priv
*priv
= netdev_priv(dev
);
169 if (!priv
->open_time
)
175 if (netif_queue_stopped(dev
))
176 netif_wake_queue(dev
);
186 static int sja1000_set_bittiming(struct net_device
*dev
)
188 struct sja1000_priv
*priv
= netdev_priv(dev
);
189 struct can_bittiming
*bt
= &priv
->can
.bittiming
;
192 btr0
= ((bt
->brp
- 1) & 0x3f) | (((bt
->sjw
- 1) & 0x3) << 6);
193 btr1
= ((bt
->prop_seg
+ bt
->phase_seg1
- 1) & 0xf) |
194 (((bt
->phase_seg2
- 1) & 0x7) << 4);
195 if (priv
->can
.ctrlmode
& CAN_CTRLMODE_3_SAMPLES
)
198 dev_info(dev
->dev
.parent
,
199 "setting BTR0=0x%02x BTR1=0x%02x\n", btr0
, btr1
);
201 priv
->write_reg(priv
, REG_BTR0
, btr0
);
202 priv
->write_reg(priv
, REG_BTR1
, btr1
);
208 * initialize SJA1000 chip:
212 * - enable interrupts
213 * - start operating mode
215 static void chipset_init(struct net_device
*dev
)
217 struct sja1000_priv
*priv
= netdev_priv(dev
);
219 /* set clock divider and output control register */
220 priv
->write_reg(priv
, REG_CDR
, priv
->cdr
| CDR_PELICAN
);
222 /* set acceptance filter (accept all) */
223 priv
->write_reg(priv
, REG_ACCC0
, 0x00);
224 priv
->write_reg(priv
, REG_ACCC1
, 0x00);
225 priv
->write_reg(priv
, REG_ACCC2
, 0x00);
226 priv
->write_reg(priv
, REG_ACCC3
, 0x00);
228 priv
->write_reg(priv
, REG_ACCM0
, 0xFF);
229 priv
->write_reg(priv
, REG_ACCM1
, 0xFF);
230 priv
->write_reg(priv
, REG_ACCM2
, 0xFF);
231 priv
->write_reg(priv
, REG_ACCM3
, 0xFF);
233 priv
->write_reg(priv
, REG_OCR
, priv
->ocr
| OCR_MODE_NORMAL
);
237 * transmit a CAN message
238 * message layout in the sk_buff should be like this:
239 * xx xx xx xx ff ll 00 11 22 33 44 55 66 77
240 * [ can-id ] [flags] [len] [can data (up to 8 bytes]
242 static int sja1000_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
244 struct sja1000_priv
*priv
= netdev_priv(dev
);
245 struct net_device_stats
*stats
= &dev
->stats
;
246 struct can_frame
*cf
= (struct can_frame
*)skb
->data
;
253 netif_stop_queue(dev
);
255 fi
= dlc
= cf
->can_dlc
;
258 if (id
& CAN_RTR_FLAG
)
261 if (id
& CAN_EFF_FLAG
) {
264 priv
->write_reg(priv
, REG_FI
, fi
);
265 priv
->write_reg(priv
, REG_ID1
, (id
& 0x1fe00000) >> (5 + 16));
266 priv
->write_reg(priv
, REG_ID2
, (id
& 0x001fe000) >> (5 + 8));
267 priv
->write_reg(priv
, REG_ID3
, (id
& 0x00001fe0) >> 5);
268 priv
->write_reg(priv
, REG_ID4
, (id
& 0x0000001f) << 3);
271 priv
->write_reg(priv
, REG_FI
, fi
);
272 priv
->write_reg(priv
, REG_ID1
, (id
& 0x000007f8) >> 3);
273 priv
->write_reg(priv
, REG_ID2
, (id
& 0x00000007) << 5);
276 for (i
= 0; i
< dlc
; i
++)
277 priv
->write_reg(priv
, dreg
++, cf
->data
[i
]);
279 stats
->tx_bytes
+= dlc
;
280 dev
->trans_start
= jiffies
;
282 can_put_echo_skb(skb
, dev
, 0);
284 priv
->write_reg(priv
, REG_CMR
, CMD_TR
);
289 static void sja1000_rx(struct net_device
*dev
)
291 struct sja1000_priv
*priv
= netdev_priv(dev
);
292 struct net_device_stats
*stats
= &dev
->stats
;
293 struct can_frame
*cf
;
301 skb
= dev_alloc_skb(sizeof(struct can_frame
));
305 skb
->protocol
= htons(ETH_P_CAN
);
307 fi
= priv
->read_reg(priv
, REG_FI
);
311 /* extended frame format (EFF) */
313 id
= (priv
->read_reg(priv
, REG_ID1
) << (5 + 16))
314 | (priv
->read_reg(priv
, REG_ID2
) << (5 + 8))
315 | (priv
->read_reg(priv
, REG_ID3
) << 5)
316 | (priv
->read_reg(priv
, REG_ID4
) >> 3);
319 /* standard frame format (SFF) */
321 id
= (priv
->read_reg(priv
, REG_ID1
) << 3)
322 | (priv
->read_reg(priv
, REG_ID2
) >> 5);
328 cf
= (struct can_frame
*)skb_put(skb
, sizeof(struct can_frame
));
329 memset(cf
, 0, sizeof(struct can_frame
));
332 for (i
= 0; i
< dlc
; i
++)
333 cf
->data
[i
] = priv
->read_reg(priv
, dreg
++);
338 /* release receive buffer */
339 priv
->write_reg(priv
, REG_CMR
, CMD_RRB
);
343 dev
->last_rx
= jiffies
;
345 stats
->rx_bytes
+= dlc
;
348 static int sja1000_err(struct net_device
*dev
, uint8_t isrc
, uint8_t status
)
350 struct sja1000_priv
*priv
= netdev_priv(dev
);
351 struct net_device_stats
*stats
= &dev
->stats
;
352 struct can_frame
*cf
;
354 enum can_state state
= priv
->can
.state
;
357 skb
= dev_alloc_skb(sizeof(struct can_frame
));
361 skb
->protocol
= htons(ETH_P_CAN
);
362 cf
= (struct can_frame
*)skb_put(skb
, sizeof(struct can_frame
));
363 memset(cf
, 0, sizeof(struct can_frame
));
364 cf
->can_id
= CAN_ERR_FLAG
;
365 cf
->can_dlc
= CAN_ERR_DLC
;
367 if (isrc
& IRQ_DOI
) {
368 /* data overrun interrupt */
369 dev_dbg(dev
->dev
.parent
, "data overrun interrupt\n");
370 cf
->can_id
|= CAN_ERR_CRTL
;
371 cf
->data
[1] = CAN_ERR_CRTL_RX_OVERFLOW
;
372 stats
->rx_over_errors
++;
374 priv
->write_reg(priv
, REG_CMR
, CMD_CDO
); /* clear bit */
378 /* error warning interrupt */
379 dev_dbg(dev
->dev
.parent
, "error warning interrupt\n");
381 if (status
& SR_BS
) {
382 state
= CAN_STATE_BUS_OFF
;
383 cf
->can_id
|= CAN_ERR_BUSOFF
;
385 } else if (status
& SR_ES
) {
386 state
= CAN_STATE_ERROR_WARNING
;
388 state
= CAN_STATE_ERROR_ACTIVE
;
390 if (isrc
& IRQ_BEI
) {
391 /* bus error interrupt */
392 priv
->can
.can_stats
.bus_error
++;
395 ecc
= priv
->read_reg(priv
, REG_ECC
);
397 cf
->can_id
|= CAN_ERR_PROT
| CAN_ERR_BUSERROR
;
399 switch (ecc
& ECC_MASK
) {
401 cf
->data
[2] |= CAN_ERR_PROT_BIT
;
404 cf
->data
[2] |= CAN_ERR_PROT_FORM
;
407 cf
->data
[2] |= CAN_ERR_PROT_STUFF
;
410 cf
->data
[2] |= CAN_ERR_PROT_UNSPEC
;
411 cf
->data
[3] = ecc
& ECC_SEG
;
414 /* Error occured during transmission? */
415 if ((ecc
& ECC_DIR
) == 0)
416 cf
->data
[2] |= CAN_ERR_PROT_TX
;
418 if (isrc
& IRQ_EPI
) {
419 /* error passive interrupt */
420 dev_dbg(dev
->dev
.parent
, "error passive interrupt\n");
422 state
= CAN_STATE_ERROR_PASSIVE
;
424 state
= CAN_STATE_ERROR_ACTIVE
;
426 if (isrc
& IRQ_ALI
) {
427 /* arbitration lost interrupt */
428 dev_dbg(dev
->dev
.parent
, "arbitration lost interrupt\n");
429 alc
= priv
->read_reg(priv
, REG_ALC
);
430 priv
->can
.can_stats
.arbitration_lost
++;
432 cf
->can_id
|= CAN_ERR_LOSTARB
;
433 cf
->data
[0] = alc
& 0x1f;
436 if (state
!= priv
->can
.state
&& (state
== CAN_STATE_ERROR_WARNING
||
437 state
== CAN_STATE_ERROR_PASSIVE
)) {
438 uint8_t rxerr
= priv
->read_reg(priv
, REG_RXERR
);
439 uint8_t txerr
= priv
->read_reg(priv
, REG_TXERR
);
440 cf
->can_id
|= CAN_ERR_CRTL
;
441 if (state
== CAN_STATE_ERROR_WARNING
) {
442 priv
->can
.can_stats
.error_warning
++;
443 cf
->data
[1] = (txerr
> rxerr
) ?
444 CAN_ERR_CRTL_TX_WARNING
:
445 CAN_ERR_CRTL_RX_WARNING
;
447 priv
->can
.can_stats
.error_passive
++;
448 cf
->data
[1] = (txerr
> rxerr
) ?
449 CAN_ERR_CRTL_TX_PASSIVE
:
450 CAN_ERR_CRTL_RX_PASSIVE
;
454 priv
->can
.state
= state
;
458 dev
->last_rx
= jiffies
;
460 stats
->rx_bytes
+= cf
->can_dlc
;
465 irqreturn_t
sja1000_interrupt(int irq
, void *dev_id
)
467 struct net_device
*dev
= (struct net_device
*)dev_id
;
468 struct sja1000_priv
*priv
= netdev_priv(dev
);
469 struct net_device_stats
*stats
= &dev
->stats
;
470 uint8_t isrc
, status
;
473 /* Shared interrupts and IRQ off? */
474 if (priv
->read_reg(priv
, REG_IER
) == IRQ_OFF
)
480 while ((isrc
= priv
->read_reg(priv
, REG_IR
)) && (n
< SJA1000_MAX_IRQ
)) {
482 status
= priv
->read_reg(priv
, REG_SR
);
485 dev_warn(dev
->dev
.parent
, "wakeup interrupt\n");
488 /* transmission complete interrupt */
490 can_get_echo_skb(dev
, 0);
491 netif_wake_queue(dev
);
494 /* receive interrupt */
495 while (status
& SR_RBS
) {
497 status
= priv
->read_reg(priv
, REG_SR
);
500 if (isrc
& (IRQ_DOI
| IRQ_EI
| IRQ_BEI
| IRQ_EPI
| IRQ_ALI
)) {
501 /* error interrupt */
502 if (sja1000_err(dev
, isrc
, status
))
508 priv
->post_irq(priv
);
510 if (n
>= SJA1000_MAX_IRQ
)
511 dev_dbg(dev
->dev
.parent
, "%d messages handled in ISR", n
);
513 return (n
) ? IRQ_HANDLED
: IRQ_NONE
;
515 EXPORT_SYMBOL_GPL(sja1000_interrupt
);
517 static int sja1000_open(struct net_device
*dev
)
519 struct sja1000_priv
*priv
= netdev_priv(dev
);
522 /* set chip into reset mode */
526 err
= open_candev(dev
);
530 /* register interrupt handler, if not done by the device driver */
531 if (!(priv
->flags
& SJA1000_CUSTOM_IRQ_HANDLER
)) {
532 err
= request_irq(dev
->irq
, &sja1000_interrupt
, priv
->irq_flags
,
533 dev
->name
, (void *)dev
);
540 /* init and start chi */
542 priv
->open_time
= jiffies
;
544 netif_start_queue(dev
);
549 static int sja1000_close(struct net_device
*dev
)
551 struct sja1000_priv
*priv
= netdev_priv(dev
);
553 netif_stop_queue(dev
);
556 if (!(priv
->flags
& SJA1000_CUSTOM_IRQ_HANDLER
))
557 free_irq(dev
->irq
, (void *)dev
);
566 struct net_device
*alloc_sja1000dev(int sizeof_priv
)
568 struct net_device
*dev
;
569 struct sja1000_priv
*priv
;
571 dev
= alloc_candev(sizeof(struct sja1000_priv
) + sizeof_priv
);
575 priv
= netdev_priv(dev
);
578 priv
->can
.bittiming_const
= &sja1000_bittiming_const
;
579 priv
->can
.do_set_bittiming
= sja1000_set_bittiming
;
580 priv
->can
.do_set_mode
= sja1000_set_mode
;
583 priv
->priv
= (void *)priv
+ sizeof(struct sja1000_priv
);
587 EXPORT_SYMBOL_GPL(alloc_sja1000dev
);
589 void free_sja1000dev(struct net_device
*dev
)
593 EXPORT_SYMBOL_GPL(free_sja1000dev
);
595 static const struct net_device_ops sja1000_netdev_ops
= {
596 .ndo_open
= sja1000_open
,
597 .ndo_stop
= sja1000_close
,
598 .ndo_start_xmit
= sja1000_start_xmit
,
601 int register_sja1000dev(struct net_device
*dev
)
603 if (!sja1000_probe_chip(dev
))
606 dev
->flags
|= IFF_ECHO
; /* we support local echo */
607 dev
->netdev_ops
= &sja1000_netdev_ops
;
612 return register_candev(dev
);
614 EXPORT_SYMBOL_GPL(register_sja1000dev
);
616 void unregister_sja1000dev(struct net_device
*dev
)
619 unregister_candev(dev
);
621 EXPORT_SYMBOL_GPL(unregister_sja1000dev
);
623 static __init
int sja1000_init(void)
625 printk(KERN_INFO
"%s CAN netdevice driver\n", DRV_NAME
);
630 module_init(sja1000_init
);
632 static __exit
void sja1000_exit(void)
634 printk(KERN_INFO
"%s: driver removed\n", DRV_NAME
);
637 module_exit(sja1000_exit
);