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>
69 #define DRV_NAME "sja1000"
71 MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
72 MODULE_LICENSE("Dual BSD/GPL");
73 MODULE_DESCRIPTION(DRV_NAME
"CAN netdevice driver");
75 static struct can_bittiming_const sja1000_bittiming_const
= {
87 static int sja1000_probe_chip(struct net_device
*dev
)
89 struct sja1000_priv
*priv
= netdev_priv(dev
);
91 if (priv
->reg_base
&& (priv
->read_reg(priv
, 0) == 0xFF)) {
92 printk(KERN_INFO
"%s: probing @0x%lX failed\n",
93 DRV_NAME
, dev
->base_addr
);
99 static void set_reset_mode(struct net_device
*dev
)
101 struct sja1000_priv
*priv
= netdev_priv(dev
);
102 unsigned char status
= priv
->read_reg(priv
, REG_MOD
);
105 /* disable interrupts */
106 priv
->write_reg(priv
, REG_IER
, IRQ_OFF
);
108 for (i
= 0; i
< 100; i
++) {
109 /* check reset bit */
110 if (status
& MOD_RM
) {
111 priv
->can
.state
= CAN_STATE_STOPPED
;
115 priv
->write_reg(priv
, REG_MOD
, MOD_RM
); /* reset chip */
117 status
= priv
->read_reg(priv
, REG_MOD
);
120 dev_err(dev
->dev
.parent
, "setting SJA1000 into reset mode failed!\n");
123 static void set_normal_mode(struct net_device
*dev
)
125 struct sja1000_priv
*priv
= netdev_priv(dev
);
126 unsigned char status
= priv
->read_reg(priv
, REG_MOD
);
129 for (i
= 0; i
< 100; i
++) {
130 /* check reset bit */
131 if ((status
& MOD_RM
) == 0) {
132 priv
->can
.state
= CAN_STATE_ERROR_ACTIVE
;
133 /* enable all interrupts */
134 priv
->write_reg(priv
, REG_IER
, IRQ_ALL
);
138 /* set chip to normal mode */
139 priv
->write_reg(priv
, REG_MOD
, 0x00);
141 status
= priv
->read_reg(priv
, REG_MOD
);
144 dev_err(dev
->dev
.parent
, "setting SJA1000 into normal mode failed!\n");
147 static void sja1000_start(struct net_device
*dev
)
149 struct sja1000_priv
*priv
= netdev_priv(dev
);
151 /* leave reset mode */
152 if (priv
->can
.state
!= CAN_STATE_STOPPED
)
155 /* Clear error counters and error code capture */
156 priv
->write_reg(priv
, REG_TXERR
, 0x0);
157 priv
->write_reg(priv
, REG_RXERR
, 0x0);
158 priv
->read_reg(priv
, REG_ECC
);
160 /* leave reset mode */
161 set_normal_mode(dev
);
164 static int sja1000_set_mode(struct net_device
*dev
, enum can_mode mode
)
166 struct sja1000_priv
*priv
= netdev_priv(dev
);
168 if (!priv
->open_time
)
174 if (netif_queue_stopped(dev
))
175 netif_wake_queue(dev
);
185 static int sja1000_set_bittiming(struct net_device
*dev
)
187 struct sja1000_priv
*priv
= netdev_priv(dev
);
188 struct can_bittiming
*bt
= &priv
->can
.bittiming
;
191 btr0
= ((bt
->brp
- 1) & 0x3f) | (((bt
->sjw
- 1) & 0x3) << 6);
192 btr1
= ((bt
->prop_seg
+ bt
->phase_seg1
- 1) & 0xf) |
193 (((bt
->phase_seg2
- 1) & 0x7) << 4);
194 if (priv
->can
.ctrlmode
& CAN_CTRLMODE_3_SAMPLES
)
197 dev_info(dev
->dev
.parent
,
198 "setting BTR0=0x%02x BTR1=0x%02x\n", btr0
, btr1
);
200 priv
->write_reg(priv
, REG_BTR0
, btr0
);
201 priv
->write_reg(priv
, REG_BTR1
, btr1
);
207 * initialize SJA1000 chip:
211 * - enable interrupts
212 * - start operating mode
214 static void chipset_init(struct net_device
*dev
)
216 struct sja1000_priv
*priv
= netdev_priv(dev
);
218 /* set clock divider and output control register */
219 priv
->write_reg(priv
, REG_CDR
, priv
->cdr
| CDR_PELICAN
);
221 /* set acceptance filter (accept all) */
222 priv
->write_reg(priv
, REG_ACCC0
, 0x00);
223 priv
->write_reg(priv
, REG_ACCC1
, 0x00);
224 priv
->write_reg(priv
, REG_ACCC2
, 0x00);
225 priv
->write_reg(priv
, REG_ACCC3
, 0x00);
227 priv
->write_reg(priv
, REG_ACCM0
, 0xFF);
228 priv
->write_reg(priv
, REG_ACCM1
, 0xFF);
229 priv
->write_reg(priv
, REG_ACCM2
, 0xFF);
230 priv
->write_reg(priv
, REG_ACCM3
, 0xFF);
232 priv
->write_reg(priv
, REG_OCR
, priv
->ocr
| OCR_MODE_NORMAL
);
236 * transmit a CAN message
237 * message layout in the sk_buff should be like this:
238 * xx xx xx xx ff ll 00 11 22 33 44 55 66 77
239 * [ can-id ] [flags] [len] [can data (up to 8 bytes]
241 static netdev_tx_t
sja1000_start_xmit(struct sk_buff
*skb
,
242 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
);
344 stats
->rx_bytes
+= dlc
;
347 static int sja1000_err(struct net_device
*dev
, uint8_t isrc
, uint8_t status
)
349 struct sja1000_priv
*priv
= netdev_priv(dev
);
350 struct net_device_stats
*stats
= &dev
->stats
;
351 struct can_frame
*cf
;
353 enum can_state state
= priv
->can
.state
;
356 skb
= dev_alloc_skb(sizeof(struct can_frame
));
360 skb
->protocol
= htons(ETH_P_CAN
);
361 cf
= (struct can_frame
*)skb_put(skb
, sizeof(struct can_frame
));
362 memset(cf
, 0, sizeof(struct can_frame
));
363 cf
->can_id
= CAN_ERR_FLAG
;
364 cf
->can_dlc
= CAN_ERR_DLC
;
366 if (isrc
& IRQ_DOI
) {
367 /* data overrun interrupt */
368 dev_dbg(dev
->dev
.parent
, "data overrun interrupt\n");
369 cf
->can_id
|= CAN_ERR_CRTL
;
370 cf
->data
[1] = CAN_ERR_CRTL_RX_OVERFLOW
;
371 stats
->rx_over_errors
++;
373 priv
->write_reg(priv
, REG_CMR
, CMD_CDO
); /* clear bit */
377 /* error warning interrupt */
378 dev_dbg(dev
->dev
.parent
, "error warning interrupt\n");
380 if (status
& SR_BS
) {
381 state
= CAN_STATE_BUS_OFF
;
382 cf
->can_id
|= CAN_ERR_BUSOFF
;
384 } else if (status
& SR_ES
) {
385 state
= CAN_STATE_ERROR_WARNING
;
387 state
= CAN_STATE_ERROR_ACTIVE
;
389 if (isrc
& IRQ_BEI
) {
390 /* bus error interrupt */
391 priv
->can
.can_stats
.bus_error
++;
394 ecc
= priv
->read_reg(priv
, REG_ECC
);
396 cf
->can_id
|= CAN_ERR_PROT
| CAN_ERR_BUSERROR
;
398 switch (ecc
& ECC_MASK
) {
400 cf
->data
[2] |= CAN_ERR_PROT_BIT
;
403 cf
->data
[2] |= CAN_ERR_PROT_FORM
;
406 cf
->data
[2] |= CAN_ERR_PROT_STUFF
;
409 cf
->data
[2] |= CAN_ERR_PROT_UNSPEC
;
410 cf
->data
[3] = ecc
& ECC_SEG
;
413 /* Error occured during transmission? */
414 if ((ecc
& ECC_DIR
) == 0)
415 cf
->data
[2] |= CAN_ERR_PROT_TX
;
417 if (isrc
& IRQ_EPI
) {
418 /* error passive interrupt */
419 dev_dbg(dev
->dev
.parent
, "error passive interrupt\n");
421 state
= CAN_STATE_ERROR_PASSIVE
;
423 state
= CAN_STATE_ERROR_ACTIVE
;
425 if (isrc
& IRQ_ALI
) {
426 /* arbitration lost interrupt */
427 dev_dbg(dev
->dev
.parent
, "arbitration lost interrupt\n");
428 alc
= priv
->read_reg(priv
, REG_ALC
);
429 priv
->can
.can_stats
.arbitration_lost
++;
431 cf
->can_id
|= CAN_ERR_LOSTARB
;
432 cf
->data
[0] = alc
& 0x1f;
435 if (state
!= priv
->can
.state
&& (state
== CAN_STATE_ERROR_WARNING
||
436 state
== CAN_STATE_ERROR_PASSIVE
)) {
437 uint8_t rxerr
= priv
->read_reg(priv
, REG_RXERR
);
438 uint8_t txerr
= priv
->read_reg(priv
, REG_TXERR
);
439 cf
->can_id
|= CAN_ERR_CRTL
;
440 if (state
== CAN_STATE_ERROR_WARNING
) {
441 priv
->can
.can_stats
.error_warning
++;
442 cf
->data
[1] = (txerr
> rxerr
) ?
443 CAN_ERR_CRTL_TX_WARNING
:
444 CAN_ERR_CRTL_RX_WARNING
;
446 priv
->can
.can_stats
.error_passive
++;
447 cf
->data
[1] = (txerr
> rxerr
) ?
448 CAN_ERR_CRTL_TX_PASSIVE
:
449 CAN_ERR_CRTL_RX_PASSIVE
;
453 priv
->can
.state
= state
;
458 stats
->rx_bytes
+= cf
->can_dlc
;
463 irqreturn_t
sja1000_interrupt(int irq
, void *dev_id
)
465 struct net_device
*dev
= (struct net_device
*)dev_id
;
466 struct sja1000_priv
*priv
= netdev_priv(dev
);
467 struct net_device_stats
*stats
= &dev
->stats
;
468 uint8_t isrc
, status
;
471 /* Shared interrupts and IRQ off? */
472 if (priv
->read_reg(priv
, REG_IER
) == IRQ_OFF
)
478 while ((isrc
= priv
->read_reg(priv
, REG_IR
)) && (n
< SJA1000_MAX_IRQ
)) {
480 status
= priv
->read_reg(priv
, REG_SR
);
483 dev_warn(dev
->dev
.parent
, "wakeup interrupt\n");
486 /* transmission complete interrupt */
488 can_get_echo_skb(dev
, 0);
489 netif_wake_queue(dev
);
492 /* receive interrupt */
493 while (status
& SR_RBS
) {
495 status
= priv
->read_reg(priv
, REG_SR
);
498 if (isrc
& (IRQ_DOI
| IRQ_EI
| IRQ_BEI
| IRQ_EPI
| IRQ_ALI
)) {
499 /* error interrupt */
500 if (sja1000_err(dev
, isrc
, status
))
506 priv
->post_irq(priv
);
508 if (n
>= SJA1000_MAX_IRQ
)
509 dev_dbg(dev
->dev
.parent
, "%d messages handled in ISR", n
);
511 return (n
) ? IRQ_HANDLED
: IRQ_NONE
;
513 EXPORT_SYMBOL_GPL(sja1000_interrupt
);
515 static int sja1000_open(struct net_device
*dev
)
517 struct sja1000_priv
*priv
= netdev_priv(dev
);
520 /* set chip into reset mode */
524 err
= open_candev(dev
);
528 /* register interrupt handler, if not done by the device driver */
529 if (!(priv
->flags
& SJA1000_CUSTOM_IRQ_HANDLER
)) {
530 err
= request_irq(dev
->irq
, &sja1000_interrupt
, priv
->irq_flags
,
531 dev
->name
, (void *)dev
);
538 /* init and start chi */
540 priv
->open_time
= jiffies
;
542 netif_start_queue(dev
);
547 static int sja1000_close(struct net_device
*dev
)
549 struct sja1000_priv
*priv
= netdev_priv(dev
);
551 netif_stop_queue(dev
);
554 if (!(priv
->flags
& SJA1000_CUSTOM_IRQ_HANDLER
))
555 free_irq(dev
->irq
, (void *)dev
);
564 struct net_device
*alloc_sja1000dev(int sizeof_priv
)
566 struct net_device
*dev
;
567 struct sja1000_priv
*priv
;
569 dev
= alloc_candev(sizeof(struct sja1000_priv
) + sizeof_priv
);
573 priv
= netdev_priv(dev
);
576 priv
->can
.bittiming_const
= &sja1000_bittiming_const
;
577 priv
->can
.do_set_bittiming
= sja1000_set_bittiming
;
578 priv
->can
.do_set_mode
= sja1000_set_mode
;
581 priv
->priv
= (void *)priv
+ sizeof(struct sja1000_priv
);
585 EXPORT_SYMBOL_GPL(alloc_sja1000dev
);
587 void free_sja1000dev(struct net_device
*dev
)
591 EXPORT_SYMBOL_GPL(free_sja1000dev
);
593 static const struct net_device_ops sja1000_netdev_ops
= {
594 .ndo_open
= sja1000_open
,
595 .ndo_stop
= sja1000_close
,
596 .ndo_start_xmit
= sja1000_start_xmit
,
599 int register_sja1000dev(struct net_device
*dev
)
601 if (!sja1000_probe_chip(dev
))
604 dev
->flags
|= IFF_ECHO
; /* we support local echo */
605 dev
->netdev_ops
= &sja1000_netdev_ops
;
610 return register_candev(dev
);
612 EXPORT_SYMBOL_GPL(register_sja1000dev
);
614 void unregister_sja1000dev(struct net_device
*dev
)
617 unregister_candev(dev
);
619 EXPORT_SYMBOL_GPL(unregister_sja1000dev
);
621 static __init
int sja1000_init(void)
623 printk(KERN_INFO
"%s CAN netdevice driver\n", DRV_NAME
);
628 module_init(sja1000_init
);
630 static __exit
void sja1000_exit(void)
632 printk(KERN_INFO
"%s: driver removed\n", DRV_NAME
);
635 module_exit(sja1000_exit
);