include: replace linux/module.h with "struct module" wherever possible
[linux-2.6/next.git] / drivers / net / can / sja1000 / sja1000.c
blobf501bba1fc6fc9d48cb923f9c963bea83cd3b406
1 /*
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
8 * All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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
41 * DAMAGE.
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>
66 #include "sja1000.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 = {
75 .name = DRV_NAME,
76 .tseg1_min = 1,
77 .tseg1_max = 16,
78 .tseg2_min = 1,
79 .tseg2_max = 8,
80 .sjw_max = 4,
81 .brp_min = 1,
82 .brp_max = 64,
83 .brp_inc = 1,
86 static void sja1000_write_cmdreg(struct sja1000_priv *priv, u8 val)
88 unsigned long flags;
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);
107 return 0;
109 return -1;
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);
116 int i;
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;
125 return;
128 priv->write_reg(priv, REG_MOD, MOD_RM); /* reset chip */
129 udelay(10);
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);
140 int i;
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);
149 else
150 priv->write_reg(priv, REG_IER,
151 IRQ_ALL & ~IRQ_BEI);
152 return;
155 /* set chip to normal mode */
156 priv->write_reg(priv, REG_MOD, 0x00);
157 udelay(10);
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)
170 set_reset_mode(dev);
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)
186 return -EINVAL;
188 switch (mode) {
189 case CAN_MODE_START:
190 sja1000_start(dev);
191 if (netif_queue_stopped(dev))
192 netif_wake_queue(dev);
193 break;
195 default:
196 return -EOPNOTSUPP;
199 return 0;
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;
206 u8 btr0, btr1;
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)
212 btr1 |= 0x80;
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);
220 return 0;
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);
231 return 0;
235 * initialize SJA1000 chip:
236 * - reset chip
237 * - set output mode
238 * - set baudrate
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;
274 uint8_t fi;
275 uint8_t dlc;
276 canid_t id;
277 uint8_t dreg;
278 int i;
280 if (can_dropped_invalid_skb(dev, skb))
281 return NETDEV_TX_OK;
283 netif_stop_queue(dev);
285 fi = dlc = cf->can_dlc;
286 id = cf->can_id;
288 if (id & CAN_RTR_FLAG)
289 fi |= FI_RTR;
291 if (id & CAN_EFF_FLAG) {
292 fi |= FI_FF;
293 dreg = EFF_BUF;
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);
299 } else {
300 dreg = SFF_BUF;
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);
313 return NETDEV_TX_OK;
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;
321 struct sk_buff *skb;
322 uint8_t fi;
323 uint8_t dreg;
324 canid_t id;
325 int i;
327 /* create zero'ed CAN frame buffer */
328 skb = alloc_can_skb(dev, &cf);
329 if (skb == NULL)
330 return;
332 fi = priv->read_reg(priv, REG_FI);
334 if (fi & FI_FF) {
335 /* extended frame format (EFF) */
336 dreg = EFF_BUF;
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);
341 id |= CAN_EFF_FLAG;
342 } else {
343 /* standard frame format (SFF) */
344 dreg = SFF_BUF;
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);
350 if (fi & FI_RTR) {
351 id |= CAN_RTR_FLAG;
352 } else {
353 for (i = 0; i < cf->can_dlc; i++)
354 cf->data[i] = priv->read_reg(priv, dreg++);
357 cf->can_id = id;
359 /* release receive buffer */
360 sja1000_write_cmdreg(priv, CMD_RRB);
362 netif_rx(skb);
364 stats->rx_packets++;
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;
373 struct sk_buff *skb;
374 enum can_state state = priv->can.state;
375 uint8_t ecc, alc;
377 skb = alloc_can_err_skb(dev, &cf);
378 if (skb == NULL)
379 return -ENOMEM;
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++;
387 stats->rx_errors++;
388 sja1000_write_cmdreg(priv, CMD_CDO); /* clear bit */
391 if (isrc & IRQ_EI) {
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;
398 can_bus_off(dev);
399 } else if (status & SR_ES) {
400 state = CAN_STATE_ERROR_WARNING;
401 } else
402 state = CAN_STATE_ERROR_ACTIVE;
404 if (isrc & IRQ_BEI) {
405 /* bus error interrupt */
406 priv->can.can_stats.bus_error++;
407 stats->rx_errors++;
409 ecc = priv->read_reg(priv, REG_ECC);
411 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
413 switch (ecc & ECC_MASK) {
414 case ECC_BIT:
415 cf->data[2] |= CAN_ERR_PROT_BIT;
416 break;
417 case ECC_FORM:
418 cf->data[2] |= CAN_ERR_PROT_FORM;
419 break;
420 case ECC_STUFF:
421 cf->data[2] |= CAN_ERR_PROT_STUFF;
422 break;
423 default:
424 cf->data[2] |= CAN_ERR_PROT_UNSPEC;
425 cf->data[3] = ecc & ECC_SEG;
426 break;
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");
435 if (status & SR_ES)
436 state = CAN_STATE_ERROR_PASSIVE;
437 else
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++;
445 stats->tx_errors++;
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;
460 } else {
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;
466 cf->data[6] = txerr;
467 cf->data[7] = rxerr;
470 priv->can.state = state;
472 netif_rx(skb);
474 stats->rx_packets++;
475 stats->rx_bytes += cf->can_dlc;
477 return 0;
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;
486 int n = 0;
488 /* Shared interrupts and IRQ off? */
489 if (priv->read_reg(priv, REG_IER) == IRQ_OFF)
490 return IRQ_NONE;
492 if (priv->pre_irq)
493 priv->pre_irq(priv);
495 while ((isrc = priv->read_reg(priv, REG_IR)) && (n < SJA1000_MAX_IRQ)) {
496 n++;
497 status = priv->read_reg(priv, REG_SR);
499 if (isrc & IRQ_WUI)
500 dev_warn(dev->dev.parent, "wakeup interrupt\n");
502 if (isrc & IRQ_TI) {
503 /* transmission complete interrupt */
504 stats->tx_bytes += priv->read_reg(priv, REG_FI) & 0xf;
505 stats->tx_packets++;
506 can_get_echo_skb(dev, 0);
507 netif_wake_queue(dev);
509 if (isrc & IRQ_RI) {
510 /* receive interrupt */
511 while (status & SR_RBS) {
512 sja1000_rx(dev);
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))
519 break;
523 if (priv->post_irq)
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);
536 int err;
538 /* set chip into reset mode */
539 set_reset_mode(dev);
541 /* common open */
542 err = open_candev(dev);
543 if (err)
544 return err;
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);
550 if (err) {
551 close_candev(dev);
552 return -EAGAIN;
556 /* init and start chi */
557 sja1000_start(dev);
558 priv->open_time = jiffies;
560 netif_start_queue(dev);
562 return 0;
565 static int sja1000_close(struct net_device *dev)
567 struct sja1000_priv *priv = netdev_priv(dev);
569 netif_stop_queue(dev);
570 set_reset_mode(dev);
572 if (!(priv->flags & SJA1000_CUSTOM_IRQ_HANDLER))
573 free_irq(dev->irq, (void *)dev);
575 close_candev(dev);
577 priv->open_time = 0;
579 return 0;
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);
589 if (!dev)
590 return NULL;
592 priv = netdev_priv(dev);
594 priv->dev = 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);
604 if (sizeof_priv)
605 priv->priv = (void *)priv + sizeof(struct sja1000_priv);
607 return dev;
609 EXPORT_SYMBOL_GPL(alloc_sja1000dev);
611 void free_sja1000dev(struct net_device *dev)
613 free_candev(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))
626 return -ENODEV;
628 dev->flags |= IFF_ECHO; /* we support local echo */
629 dev->netdev_ops = &sja1000_netdev_ops;
631 set_reset_mode(dev);
632 chipset_init(dev);
634 return register_candev(dev);
636 EXPORT_SYMBOL_GPL(register_sja1000dev);
638 void unregister_sja1000dev(struct net_device *dev)
640 set_reset_mode(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);
649 return 0;
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);