Staging: netwave: delete the driver
[linux/fpc-iii.git] / drivers / net / can / sja1000 / sja1000.c
blob145b1a731a53aa99f9c8c902df96bf8453c59796
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.h>
64 #include <linux/can/dev.h>
65 #include <linux/can/error.h>
67 #include "sja1000.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 = {
76 .name = DRV_NAME,
77 .tseg1_min = 1,
78 .tseg1_max = 16,
79 .tseg2_min = 1,
80 .tseg2_max = 8,
81 .sjw_max = 4,
82 .brp_min = 1,
83 .brp_max = 64,
84 .brp_inc = 1,
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);
94 return 0;
96 return -1;
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);
103 int i;
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;
112 return;
115 priv->write_reg(priv, REG_MOD, MOD_RM); /* reset chip */
116 udelay(10);
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);
127 int i;
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 interrupts */
134 if (priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING)
135 priv->write_reg(priv, REG_IER, IRQ_ALL);
136 else
137 priv->write_reg(priv, REG_IER,
138 IRQ_ALL & ~IRQ_BEI);
139 return;
142 /* set chip to normal mode */
143 priv->write_reg(priv, REG_MOD, 0x00);
144 udelay(10);
145 status = priv->read_reg(priv, REG_MOD);
148 dev_err(dev->dev.parent, "setting SJA1000 into normal mode failed!\n");
151 static void sja1000_start(struct net_device *dev)
153 struct sja1000_priv *priv = netdev_priv(dev);
155 /* leave reset mode */
156 if (priv->can.state != CAN_STATE_STOPPED)
157 set_reset_mode(dev);
159 /* Clear error counters and error code capture */
160 priv->write_reg(priv, REG_TXERR, 0x0);
161 priv->write_reg(priv, REG_RXERR, 0x0);
162 priv->read_reg(priv, REG_ECC);
164 /* leave reset mode */
165 set_normal_mode(dev);
168 static int sja1000_set_mode(struct net_device *dev, enum can_mode mode)
170 struct sja1000_priv *priv = netdev_priv(dev);
172 if (!priv->open_time)
173 return -EINVAL;
175 switch (mode) {
176 case CAN_MODE_START:
177 sja1000_start(dev);
178 if (netif_queue_stopped(dev))
179 netif_wake_queue(dev);
180 break;
182 default:
183 return -EOPNOTSUPP;
186 return 0;
189 static int sja1000_set_bittiming(struct net_device *dev)
191 struct sja1000_priv *priv = netdev_priv(dev);
192 struct can_bittiming *bt = &priv->can.bittiming;
193 u8 btr0, btr1;
195 btr0 = ((bt->brp - 1) & 0x3f) | (((bt->sjw - 1) & 0x3) << 6);
196 btr1 = ((bt->prop_seg + bt->phase_seg1 - 1) & 0xf) |
197 (((bt->phase_seg2 - 1) & 0x7) << 4);
198 if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
199 btr1 |= 0x80;
201 dev_info(dev->dev.parent,
202 "setting BTR0=0x%02x BTR1=0x%02x\n", btr0, btr1);
204 priv->write_reg(priv, REG_BTR0, btr0);
205 priv->write_reg(priv, REG_BTR1, btr1);
207 return 0;
210 static int sja1000_get_berr_counter(const struct net_device *dev,
211 struct can_berr_counter *bec)
213 struct sja1000_priv *priv = netdev_priv(dev);
215 bec->txerr = priv->read_reg(priv, REG_TXERR);
216 bec->rxerr = priv->read_reg(priv, REG_RXERR);
218 return 0;
222 * initialize SJA1000 chip:
223 * - reset chip
224 * - set output mode
225 * - set baudrate
226 * - enable interrupts
227 * - start operating mode
229 static void chipset_init(struct net_device *dev)
231 struct sja1000_priv *priv = netdev_priv(dev);
233 /* set clock divider and output control register */
234 priv->write_reg(priv, REG_CDR, priv->cdr | CDR_PELICAN);
236 /* set acceptance filter (accept all) */
237 priv->write_reg(priv, REG_ACCC0, 0x00);
238 priv->write_reg(priv, REG_ACCC1, 0x00);
239 priv->write_reg(priv, REG_ACCC2, 0x00);
240 priv->write_reg(priv, REG_ACCC3, 0x00);
242 priv->write_reg(priv, REG_ACCM0, 0xFF);
243 priv->write_reg(priv, REG_ACCM1, 0xFF);
244 priv->write_reg(priv, REG_ACCM2, 0xFF);
245 priv->write_reg(priv, REG_ACCM3, 0xFF);
247 priv->write_reg(priv, REG_OCR, priv->ocr | OCR_MODE_NORMAL);
251 * transmit a CAN message
252 * message layout in the sk_buff should be like this:
253 * xx xx xx xx ff ll 00 11 22 33 44 55 66 77
254 * [ can-id ] [flags] [len] [can data (up to 8 bytes]
256 static netdev_tx_t sja1000_start_xmit(struct sk_buff *skb,
257 struct net_device *dev)
259 struct sja1000_priv *priv = netdev_priv(dev);
260 struct can_frame *cf = (struct can_frame *)skb->data;
261 uint8_t fi;
262 uint8_t dlc;
263 canid_t id;
264 uint8_t dreg;
265 int i;
267 if (can_dropped_invalid_skb(dev, skb))
268 return NETDEV_TX_OK;
270 netif_stop_queue(dev);
272 fi = dlc = cf->can_dlc;
273 id = cf->can_id;
275 if (id & CAN_RTR_FLAG)
276 fi |= FI_RTR;
278 if (id & CAN_EFF_FLAG) {
279 fi |= FI_FF;
280 dreg = EFF_BUF;
281 priv->write_reg(priv, REG_FI, fi);
282 priv->write_reg(priv, REG_ID1, (id & 0x1fe00000) >> (5 + 16));
283 priv->write_reg(priv, REG_ID2, (id & 0x001fe000) >> (5 + 8));
284 priv->write_reg(priv, REG_ID3, (id & 0x00001fe0) >> 5);
285 priv->write_reg(priv, REG_ID4, (id & 0x0000001f) << 3);
286 } else {
287 dreg = SFF_BUF;
288 priv->write_reg(priv, REG_FI, fi);
289 priv->write_reg(priv, REG_ID1, (id & 0x000007f8) >> 3);
290 priv->write_reg(priv, REG_ID2, (id & 0x00000007) << 5);
293 for (i = 0; i < dlc; i++)
294 priv->write_reg(priv, dreg++, cf->data[i]);
296 dev->trans_start = jiffies;
298 can_put_echo_skb(skb, dev, 0);
300 priv->write_reg(priv, REG_CMR, CMD_TR);
302 return NETDEV_TX_OK;
305 static void sja1000_rx(struct net_device *dev)
307 struct sja1000_priv *priv = netdev_priv(dev);
308 struct net_device_stats *stats = &dev->stats;
309 struct can_frame *cf;
310 struct sk_buff *skb;
311 uint8_t fi;
312 uint8_t dreg;
313 canid_t id;
314 int i;
316 /* create zero'ed CAN frame buffer */
317 skb = alloc_can_skb(dev, &cf);
318 if (skb == NULL)
319 return;
321 fi = priv->read_reg(priv, REG_FI);
323 if (fi & FI_FF) {
324 /* extended frame format (EFF) */
325 dreg = EFF_BUF;
326 id = (priv->read_reg(priv, REG_ID1) << (5 + 16))
327 | (priv->read_reg(priv, REG_ID2) << (5 + 8))
328 | (priv->read_reg(priv, REG_ID3) << 5)
329 | (priv->read_reg(priv, REG_ID4) >> 3);
330 id |= CAN_EFF_FLAG;
331 } else {
332 /* standard frame format (SFF) */
333 dreg = SFF_BUF;
334 id = (priv->read_reg(priv, REG_ID1) << 3)
335 | (priv->read_reg(priv, REG_ID2) >> 5);
338 if (fi & FI_RTR) {
339 id |= CAN_RTR_FLAG;
340 } else {
341 cf->can_dlc = get_can_dlc(fi & 0x0F);
342 for (i = 0; i < cf->can_dlc; i++)
343 cf->data[i] = priv->read_reg(priv, dreg++);
346 cf->can_id = id;
348 /* release receive buffer */
349 priv->write_reg(priv, REG_CMR, CMD_RRB);
351 netif_rx(skb);
353 stats->rx_packets++;
354 stats->rx_bytes += cf->can_dlc;
357 static int sja1000_err(struct net_device *dev, uint8_t isrc, uint8_t status)
359 struct sja1000_priv *priv = netdev_priv(dev);
360 struct net_device_stats *stats = &dev->stats;
361 struct can_frame *cf;
362 struct sk_buff *skb;
363 enum can_state state = priv->can.state;
364 uint8_t ecc, alc;
366 skb = alloc_can_err_skb(dev, &cf);
367 if (skb == NULL)
368 return -ENOMEM;
370 if (isrc & IRQ_DOI) {
371 /* data overrun interrupt */
372 dev_dbg(dev->dev.parent, "data overrun interrupt\n");
373 cf->can_id |= CAN_ERR_CRTL;
374 cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
375 stats->rx_over_errors++;
376 stats->rx_errors++;
377 priv->write_reg(priv, REG_CMR, CMD_CDO); /* clear bit */
380 if (isrc & IRQ_EI) {
381 /* error warning interrupt */
382 dev_dbg(dev->dev.parent, "error warning interrupt\n");
384 if (status & SR_BS) {
385 state = CAN_STATE_BUS_OFF;
386 cf->can_id |= CAN_ERR_BUSOFF;
387 can_bus_off(dev);
388 } else if (status & SR_ES) {
389 state = CAN_STATE_ERROR_WARNING;
390 } else
391 state = CAN_STATE_ERROR_ACTIVE;
393 if (isrc & IRQ_BEI) {
394 /* bus error interrupt */
395 priv->can.can_stats.bus_error++;
396 stats->rx_errors++;
398 ecc = priv->read_reg(priv, REG_ECC);
400 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
402 switch (ecc & ECC_MASK) {
403 case ECC_BIT:
404 cf->data[2] |= CAN_ERR_PROT_BIT;
405 break;
406 case ECC_FORM:
407 cf->data[2] |= CAN_ERR_PROT_FORM;
408 break;
409 case ECC_STUFF:
410 cf->data[2] |= CAN_ERR_PROT_STUFF;
411 break;
412 default:
413 cf->data[2] |= CAN_ERR_PROT_UNSPEC;
414 cf->data[3] = ecc & ECC_SEG;
415 break;
417 /* Error occured during transmission? */
418 if ((ecc & ECC_DIR) == 0)
419 cf->data[2] |= CAN_ERR_PROT_TX;
421 if (isrc & IRQ_EPI) {
422 /* error passive interrupt */
423 dev_dbg(dev->dev.parent, "error passive interrupt\n");
424 if (status & SR_ES)
425 state = CAN_STATE_ERROR_PASSIVE;
426 else
427 state = CAN_STATE_ERROR_ACTIVE;
429 if (isrc & IRQ_ALI) {
430 /* arbitration lost interrupt */
431 dev_dbg(dev->dev.parent, "arbitration lost interrupt\n");
432 alc = priv->read_reg(priv, REG_ALC);
433 priv->can.can_stats.arbitration_lost++;
434 stats->tx_errors++;
435 cf->can_id |= CAN_ERR_LOSTARB;
436 cf->data[0] = alc & 0x1f;
439 if (state != priv->can.state && (state == CAN_STATE_ERROR_WARNING ||
440 state == CAN_STATE_ERROR_PASSIVE)) {
441 uint8_t rxerr = priv->read_reg(priv, REG_RXERR);
442 uint8_t txerr = priv->read_reg(priv, REG_TXERR);
443 cf->can_id |= CAN_ERR_CRTL;
444 if (state == CAN_STATE_ERROR_WARNING) {
445 priv->can.can_stats.error_warning++;
446 cf->data[1] = (txerr > rxerr) ?
447 CAN_ERR_CRTL_TX_WARNING :
448 CAN_ERR_CRTL_RX_WARNING;
449 } else {
450 priv->can.can_stats.error_passive++;
451 cf->data[1] = (txerr > rxerr) ?
452 CAN_ERR_CRTL_TX_PASSIVE :
453 CAN_ERR_CRTL_RX_PASSIVE;
455 cf->data[6] = txerr;
456 cf->data[7] = rxerr;
459 priv->can.state = state;
461 netif_rx(skb);
463 stats->rx_packets++;
464 stats->rx_bytes += cf->can_dlc;
466 return 0;
469 irqreturn_t sja1000_interrupt(int irq, void *dev_id)
471 struct net_device *dev = (struct net_device *)dev_id;
472 struct sja1000_priv *priv = netdev_priv(dev);
473 struct net_device_stats *stats = &dev->stats;
474 uint8_t isrc, status;
475 int n = 0;
477 /* Shared interrupts and IRQ off? */
478 if (priv->read_reg(priv, REG_IER) == IRQ_OFF)
479 return IRQ_NONE;
481 if (priv->pre_irq)
482 priv->pre_irq(priv);
484 while ((isrc = priv->read_reg(priv, REG_IR)) && (n < SJA1000_MAX_IRQ)) {
485 n++;
486 status = priv->read_reg(priv, REG_SR);
488 if (isrc & IRQ_WUI)
489 dev_warn(dev->dev.parent, "wakeup interrupt\n");
491 if (isrc & IRQ_TI) {
492 /* transmission complete interrupt */
493 stats->tx_bytes += priv->read_reg(priv, REG_FI) & 0xf;
494 stats->tx_packets++;
495 can_get_echo_skb(dev, 0);
496 netif_wake_queue(dev);
498 if (isrc & IRQ_RI) {
499 /* receive interrupt */
500 while (status & SR_RBS) {
501 sja1000_rx(dev);
502 status = priv->read_reg(priv, REG_SR);
505 if (isrc & (IRQ_DOI | IRQ_EI | IRQ_BEI | IRQ_EPI | IRQ_ALI)) {
506 /* error interrupt */
507 if (sja1000_err(dev, isrc, status))
508 break;
512 if (priv->post_irq)
513 priv->post_irq(priv);
515 if (n >= SJA1000_MAX_IRQ)
516 dev_dbg(dev->dev.parent, "%d messages handled in ISR", n);
518 return (n) ? IRQ_HANDLED : IRQ_NONE;
520 EXPORT_SYMBOL_GPL(sja1000_interrupt);
522 static int sja1000_open(struct net_device *dev)
524 struct sja1000_priv *priv = netdev_priv(dev);
525 int err;
527 /* set chip into reset mode */
528 set_reset_mode(dev);
530 /* common open */
531 err = open_candev(dev);
532 if (err)
533 return err;
535 /* register interrupt handler, if not done by the device driver */
536 if (!(priv->flags & SJA1000_CUSTOM_IRQ_HANDLER)) {
537 err = request_irq(dev->irq, sja1000_interrupt, priv->irq_flags,
538 dev->name, (void *)dev);
539 if (err) {
540 close_candev(dev);
541 return -EAGAIN;
545 /* init and start chi */
546 sja1000_start(dev);
547 priv->open_time = jiffies;
549 netif_start_queue(dev);
551 return 0;
554 static int sja1000_close(struct net_device *dev)
556 struct sja1000_priv *priv = netdev_priv(dev);
558 netif_stop_queue(dev);
559 set_reset_mode(dev);
561 if (!(priv->flags & SJA1000_CUSTOM_IRQ_HANDLER))
562 free_irq(dev->irq, (void *)dev);
564 close_candev(dev);
566 priv->open_time = 0;
568 return 0;
571 struct net_device *alloc_sja1000dev(int sizeof_priv)
573 struct net_device *dev;
574 struct sja1000_priv *priv;
576 dev = alloc_candev(sizeof(struct sja1000_priv) + sizeof_priv,
577 SJA1000_ECHO_SKB_MAX);
578 if (!dev)
579 return NULL;
581 priv = netdev_priv(dev);
583 priv->dev = dev;
584 priv->can.bittiming_const = &sja1000_bittiming_const;
585 priv->can.do_set_bittiming = sja1000_set_bittiming;
586 priv->can.do_set_mode = sja1000_set_mode;
587 priv->can.do_get_berr_counter = sja1000_get_berr_counter;
588 priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES |
589 CAN_CTRLMODE_BERR_REPORTING;
591 if (sizeof_priv)
592 priv->priv = (void *)priv + sizeof(struct sja1000_priv);
594 return dev;
596 EXPORT_SYMBOL_GPL(alloc_sja1000dev);
598 void free_sja1000dev(struct net_device *dev)
600 free_candev(dev);
602 EXPORT_SYMBOL_GPL(free_sja1000dev);
604 static const struct net_device_ops sja1000_netdev_ops = {
605 .ndo_open = sja1000_open,
606 .ndo_stop = sja1000_close,
607 .ndo_start_xmit = sja1000_start_xmit,
610 int register_sja1000dev(struct net_device *dev)
612 if (!sja1000_probe_chip(dev))
613 return -ENODEV;
615 dev->flags |= IFF_ECHO; /* we support local echo */
616 dev->netdev_ops = &sja1000_netdev_ops;
618 set_reset_mode(dev);
619 chipset_init(dev);
621 return register_candev(dev);
623 EXPORT_SYMBOL_GPL(register_sja1000dev);
625 void unregister_sja1000dev(struct net_device *dev)
627 set_reset_mode(dev);
628 unregister_candev(dev);
630 EXPORT_SYMBOL_GPL(unregister_sja1000dev);
632 static __init int sja1000_init(void)
634 printk(KERN_INFO "%s CAN netdevice driver\n", DRV_NAME);
636 return 0;
639 module_init(sja1000_init);
641 static __exit void sja1000_exit(void)
643 printk(KERN_INFO "%s: driver removed\n", DRV_NAME);
646 module_exit(sja1000_exit);