can: sja1000: generic OF platform bus driver
[linux-2.6.git] / drivers / net / can / sja1000 / sja1000.c
1 /*
2  * sja1000.c -  Philips SJA1000 network device driver
3  *
4  * Copyright (c) 2003 Matthias Brukner, Trajet Gmbh, Rebenring 33,
5  * 38106 Braunschweig, GERMANY
6  *
7  * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
8  * All rights reserved.
9  *
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.
21  *
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.
26  *
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.
29  *
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.
42  *
43  * Send feedback to <socketcan-users@lists.berlios.de>
44  *
45  */
46
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>
62
63 #include <linux/can.h>
64 #include <linux/can/dev.h>
65 #include <linux/can/error.h>
66 #include <linux/can/dev.h>
67
68 #include "sja1000.h"
69
70 #define DRV_NAME "sja1000"
71
72 MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
73 MODULE_LICENSE("Dual BSD/GPL");
74 MODULE_DESCRIPTION(DRV_NAME "CAN netdevice driver");
75
76 static struct can_bittiming_const sja1000_bittiming_const = {
77         .name = DRV_NAME,
78         .tseg1_min = 1,
79         .tseg1_max = 16,
80         .tseg2_min = 1,
81         .tseg2_max = 8,
82         .sjw_max = 4,
83         .brp_min = 1,
84         .brp_max = 64,
85         .brp_inc = 1,
86 };
87
88 static int sja1000_probe_chip(struct net_device *dev)
89 {
90         struct sja1000_priv *priv = netdev_priv(dev);
91
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);
95                 return 0;
96         }
97         return -1;
98 }
99
100 static void set_reset_mode(struct net_device *dev)
101 {
102         struct sja1000_priv *priv = netdev_priv(dev);
103         unsigned char status = priv->read_reg(priv, REG_MOD);
104         int i;
105
106         /* disable interrupts */
107         priv->write_reg(priv, REG_IER, IRQ_OFF);
108
109         for (i = 0; i < 100; i++) {
110                 /* check reset bit */
111                 if (status & MOD_RM) {
112                         priv->can.state = CAN_STATE_STOPPED;
113                         return;
114                 }
115
116                 priv->write_reg(priv, REG_MOD, MOD_RM); /* reset chip */
117                 udelay(10);
118                 status = priv->read_reg(priv, REG_MOD);
119         }
120
121         dev_err(dev->dev.parent, "setting SJA1000 into reset mode failed!\n");
122 }
123
124 static void set_normal_mode(struct net_device *dev)
125 {
126         struct sja1000_priv *priv = netdev_priv(dev);
127         unsigned char status = priv->read_reg(priv, REG_MOD);
128         int i;
129
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);
136                         return;
137                 }
138
139                 /* set chip to normal mode */
140                 priv->write_reg(priv, REG_MOD, 0x00);
141                 udelay(10);
142                 status = priv->read_reg(priv, REG_MOD);
143         }
144
145         dev_err(dev->dev.parent, "setting SJA1000 into normal mode failed!\n");
146 }
147
148 static void sja1000_start(struct net_device *dev)
149 {
150         struct sja1000_priv *priv = netdev_priv(dev);
151
152         /* leave reset mode */
153         if (priv->can.state != CAN_STATE_STOPPED)
154                 set_reset_mode(dev);
155
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);
160
161         /* leave reset mode */
162         set_normal_mode(dev);
163 }
164
165 static int sja1000_set_mode(struct net_device *dev, enum can_mode mode)
166 {
167         struct sja1000_priv *priv = netdev_priv(dev);
168
169         if (!priv->open_time)
170                 return -EINVAL;
171
172         switch (mode) {
173         case CAN_MODE_START:
174                 sja1000_start(dev);
175                 if (netif_queue_stopped(dev))
176                         netif_wake_queue(dev);
177                 break;
178
179         default:
180                 return -EOPNOTSUPP;
181         }
182
183         return 0;
184 }
185
186 static int sja1000_set_bittiming(struct net_device *dev)
187 {
188         struct sja1000_priv *priv = netdev_priv(dev);
189         struct can_bittiming *bt = &priv->can.bittiming;
190         u8 btr0, btr1;
191
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)
196                 btr1 |= 0x80;
197
198         dev_info(dev->dev.parent,
199                  "setting BTR0=0x%02x BTR1=0x%02x\n", btr0, btr1);
200
201         priv->write_reg(priv, REG_BTR0, btr0);
202         priv->write_reg(priv, REG_BTR1, btr1);
203
204         return 0;
205 }
206
207 /*
208  * initialize SJA1000 chip:
209  *   - reset chip
210  *   - set output mode
211  *   - set baudrate
212  *   - enable interrupts
213  *   - start operating mode
214  */
215 static void chipset_init(struct net_device *dev)
216 {
217         struct sja1000_priv *priv = netdev_priv(dev);
218
219         /* set clock divider and output control register */
220         priv->write_reg(priv, REG_CDR, priv->cdr | CDR_PELICAN);
221
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);
227
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);
232
233         priv->write_reg(priv, REG_OCR, priv->ocr | OCR_MODE_NORMAL);
234 }
235
236 /*
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]
241  */
242 static int sja1000_start_xmit(struct sk_buff *skb, struct net_device *dev)
243 {
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;
247         uint8_t fi;
248         uint8_t dlc;
249         canid_t id;
250         uint8_t dreg;
251         int i;
252
253         netif_stop_queue(dev);
254
255         fi = dlc = cf->can_dlc;
256         id = cf->can_id;
257
258         if (id & CAN_RTR_FLAG)
259                 fi |= FI_RTR;
260
261         if (id & CAN_EFF_FLAG) {
262                 fi |= FI_FF;
263                 dreg = EFF_BUF;
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);
269         } else {
270                 dreg = SFF_BUF;
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);
274         }
275
276         for (i = 0; i < dlc; i++)
277                 priv->write_reg(priv, dreg++, cf->data[i]);
278
279         stats->tx_bytes += dlc;
280         dev->trans_start = jiffies;
281
282         can_put_echo_skb(skb, dev, 0);
283
284         priv->write_reg(priv, REG_CMR, CMD_TR);
285
286         return 0;
287 }
288
289 static void sja1000_rx(struct net_device *dev)
290 {
291         struct sja1000_priv *priv = netdev_priv(dev);
292         struct net_device_stats *stats = &dev->stats;
293         struct can_frame *cf;
294         struct sk_buff *skb;
295         uint8_t fi;
296         uint8_t dreg;
297         canid_t id;
298         uint8_t dlc;
299         int i;
300
301         skb = dev_alloc_skb(sizeof(struct can_frame));
302         if (skb == NULL)
303                 return;
304         skb->dev = dev;
305         skb->protocol = htons(ETH_P_CAN);
306
307         fi = priv->read_reg(priv, REG_FI);
308         dlc = fi & 0x0F;
309
310         if (fi & FI_FF) {
311                 /* extended frame format (EFF) */
312                 dreg = EFF_BUF;
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);
317                 id |= CAN_EFF_FLAG;
318         } else {
319                 /* standard frame format (SFF) */
320                 dreg = SFF_BUF;
321                 id = (priv->read_reg(priv, REG_ID1) << 3)
322                     | (priv->read_reg(priv, REG_ID2) >> 5);
323         }
324
325         if (fi & FI_RTR)
326                 id |= CAN_RTR_FLAG;
327
328         cf = (struct can_frame *)skb_put(skb, sizeof(struct can_frame));
329         memset(cf, 0, sizeof(struct can_frame));
330         cf->can_id = id;
331         cf->can_dlc = dlc;
332         for (i = 0; i < dlc; i++)
333                 cf->data[i] = priv->read_reg(priv, dreg++);
334
335         while (i < 8)
336                 cf->data[i++] = 0;
337
338         /* release receive buffer */
339         priv->write_reg(priv, REG_CMR, CMD_RRB);
340
341         netif_rx(skb);
342
343         dev->last_rx = jiffies;
344         stats->rx_packets++;
345         stats->rx_bytes += dlc;
346 }
347
348 static int sja1000_err(struct net_device *dev, uint8_t isrc, uint8_t status)
349 {
350         struct sja1000_priv *priv = netdev_priv(dev);
351         struct net_device_stats *stats = &dev->stats;
352         struct can_frame *cf;
353         struct sk_buff *skb;
354         enum can_state state = priv->can.state;
355         uint8_t ecc, alc;
356
357         skb = dev_alloc_skb(sizeof(struct can_frame));
358         if (skb == NULL)
359                 return -ENOMEM;
360         skb->dev = dev;
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;
366
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++;
373                 stats->rx_errors++;
374                 priv->write_reg(priv, REG_CMR, CMD_CDO);        /* clear bit */
375         }
376
377         if (isrc & IRQ_EI) {
378                 /* error warning interrupt */
379                 dev_dbg(dev->dev.parent, "error warning interrupt\n");
380
381                 if (status & SR_BS) {
382                         state = CAN_STATE_BUS_OFF;
383                         cf->can_id |= CAN_ERR_BUSOFF;
384                         can_bus_off(dev);
385                 } else if (status & SR_ES) {
386                         state = CAN_STATE_ERROR_WARNING;
387                 } else
388                         state = CAN_STATE_ERROR_ACTIVE;
389         }
390         if (isrc & IRQ_BEI) {
391                 /* bus error interrupt */
392                 priv->can.can_stats.bus_error++;
393                 stats->rx_errors++;
394
395                 ecc = priv->read_reg(priv, REG_ECC);
396
397                 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
398
399                 switch (ecc & ECC_MASK) {
400                 case ECC_BIT:
401                         cf->data[2] |= CAN_ERR_PROT_BIT;
402                         break;
403                 case ECC_FORM:
404                         cf->data[2] |= CAN_ERR_PROT_FORM;
405                         break;
406                 case ECC_STUFF:
407                         cf->data[2] |= CAN_ERR_PROT_STUFF;
408                         break;
409                 default:
410                         cf->data[2] |= CAN_ERR_PROT_UNSPEC;
411                         cf->data[3] = ecc & ECC_SEG;
412                         break;
413                 }
414                 /* Error occured during transmission? */
415                 if ((ecc & ECC_DIR) == 0)
416                         cf->data[2] |= CAN_ERR_PROT_TX;
417         }
418         if (isrc & IRQ_EPI) {
419                 /* error passive interrupt */
420                 dev_dbg(dev->dev.parent, "error passive interrupt\n");
421                 if (status & SR_ES)
422                         state = CAN_STATE_ERROR_PASSIVE;
423                 else
424                         state = CAN_STATE_ERROR_ACTIVE;
425         }
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++;
431                 stats->rx_errors++;
432                 cf->can_id |= CAN_ERR_LOSTARB;
433                 cf->data[0] = alc & 0x1f;
434         }
435
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;
446                 } else {
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;
451                 }
452         }
453
454         priv->can.state = state;
455
456         netif_rx(skb);
457
458         dev->last_rx = jiffies;
459         stats->rx_packets++;
460         stats->rx_bytes += cf->can_dlc;
461
462         return 0;
463 }
464
465 irqreturn_t sja1000_interrupt(int irq, void *dev_id)
466 {
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;
471         int n = 0;
472
473         /* Shared interrupts and IRQ off? */
474         if (priv->read_reg(priv, REG_IER) == IRQ_OFF)
475                 return IRQ_NONE;
476
477         if (priv->pre_irq)
478                 priv->pre_irq(priv);
479
480         while ((isrc = priv->read_reg(priv, REG_IR)) && (n < SJA1000_MAX_IRQ)) {
481                 n++;
482                 status = priv->read_reg(priv, REG_SR);
483
484                 if (isrc & IRQ_WUI)
485                         dev_warn(dev->dev.parent, "wakeup interrupt\n");
486
487                 if (isrc & IRQ_TI) {
488                         /* transmission complete interrupt */
489                         stats->tx_packets++;
490                         can_get_echo_skb(dev, 0);
491                         netif_wake_queue(dev);
492                 }
493                 if (isrc & IRQ_RI) {
494                         /* receive interrupt */
495                         while (status & SR_RBS) {
496                                 sja1000_rx(dev);
497                                 status = priv->read_reg(priv, REG_SR);
498                         }
499                 }
500                 if (isrc & (IRQ_DOI | IRQ_EI | IRQ_BEI | IRQ_EPI | IRQ_ALI)) {
501                         /* error interrupt */
502                         if (sja1000_err(dev, isrc, status))
503                                 break;
504                 }
505         }
506
507         if (priv->post_irq)
508                 priv->post_irq(priv);
509
510         if (n >= SJA1000_MAX_IRQ)
511                 dev_dbg(dev->dev.parent, "%d messages handled in ISR", n);
512
513         return (n) ? IRQ_HANDLED : IRQ_NONE;
514 }
515 EXPORT_SYMBOL_GPL(sja1000_interrupt);
516
517 static int sja1000_open(struct net_device *dev)
518 {
519         struct sja1000_priv *priv = netdev_priv(dev);
520         int err;
521
522         /* set chip into reset mode */
523         set_reset_mode(dev);
524
525         /* common open */
526         err = open_candev(dev);
527         if (err)
528                 return err;
529
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);
534                 if (err) {
535                         close_candev(dev);
536                         return -EAGAIN;
537                 }
538         }
539
540         /* init and start chi */
541         sja1000_start(dev);
542         priv->open_time = jiffies;
543
544         netif_start_queue(dev);
545
546         return 0;
547 }
548
549 static int sja1000_close(struct net_device *dev)
550 {
551         struct sja1000_priv *priv = netdev_priv(dev);
552
553         netif_stop_queue(dev);
554         set_reset_mode(dev);
555
556         if (!(priv->flags & SJA1000_CUSTOM_IRQ_HANDLER))
557                 free_irq(dev->irq, (void *)dev);
558
559         close_candev(dev);
560
561         priv->open_time = 0;
562
563         return 0;
564 }
565
566 struct net_device *alloc_sja1000dev(int sizeof_priv)
567 {
568         struct net_device *dev;
569         struct sja1000_priv *priv;
570
571         dev = alloc_candev(sizeof(struct sja1000_priv) + sizeof_priv);
572         if (!dev)
573                 return NULL;
574
575         priv = netdev_priv(dev);
576
577         priv->dev = 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;
581
582         if (sizeof_priv)
583                 priv->priv = (void *)priv + sizeof(struct sja1000_priv);
584
585         return dev;
586 }
587 EXPORT_SYMBOL_GPL(alloc_sja1000dev);
588
589 void free_sja1000dev(struct net_device *dev)
590 {
591         free_candev(dev);
592 }
593 EXPORT_SYMBOL_GPL(free_sja1000dev);
594
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,
599 };
600
601 int register_sja1000dev(struct net_device *dev)
602 {
603         if (!sja1000_probe_chip(dev))
604                 return -ENODEV;
605
606         dev->flags |= IFF_ECHO; /* we support local echo */
607         dev->netdev_ops = &sja1000_netdev_ops;
608
609         set_reset_mode(dev);
610         chipset_init(dev);
611
612         return register_candev(dev);
613 }
614 EXPORT_SYMBOL_GPL(register_sja1000dev);
615
616 void unregister_sja1000dev(struct net_device *dev)
617 {
618         set_reset_mode(dev);
619         unregister_candev(dev);
620 }
621 EXPORT_SYMBOL_GPL(unregister_sja1000dev);
622
623 static __init int sja1000_init(void)
624 {
625         printk(KERN_INFO "%s CAN netdevice driver\n", DRV_NAME);
626
627         return 0;
628 }
629
630 module_init(sja1000_init);
631
632 static __exit void sja1000_exit(void)
633 {
634         printk(KERN_INFO "%s: driver removed\n", DRV_NAME);
635 }
636
637 module_exit(sja1000_exit);