]> rtime.felk.cvut.cz Git - socketcan-devel.git/blob - kernel/2.6/drivers/net/can/cc770/cc770.c
5ea7bedeab6a634f97cb01f55a95a9b1881b966d
[socketcan-devel.git] / kernel / 2.6 / drivers / net / can / cc770 / cc770.c
1 /*
2  * $Id:  $
3  *
4  * cc770.c - Bosch CC770 and Intel AN82527 network device driver
5  *
6  * Copyright (C) 2009 Wolfgang Grandegger <wg@grandegger.com>
7  *
8  * Derived from the old Socket-CAN i82527 driver:
9  *
10  * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
11  * All rights reserved.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. Neither the name of Volkswagen nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * Alternatively, provided that this notice is retained in full, this
26  * software may be distributed under the terms of the GNU General
27  * Public License ("GPL") version 2, in which case the provisions of the
28  * GPL apply INSTEAD OF those given above.
29  *
30  * The provided data structures and external interfaces from this code
31  * are not restricted to be used by modules with a GPL compatible license.
32  *
33  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
34  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
35  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
36  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
37  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
38  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
39  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
40  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
41  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
43  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
44  * DAMAGE.
45  *
46  * Send feedback to <socketcan-users@lists.berlios.de>
47  */
48
49 #include <linux/module.h>
50 #include <linux/init.h>
51 #include <linux/kernel.h>
52 #include <linux/version.h>
53 #include <linux/sched.h>
54 #include <linux/types.h>
55 #include <linux/fcntl.h>
56 #include <linux/interrupt.h>
57 #include <linux/ptrace.h>
58 #include <linux/string.h>
59 #include <linux/errno.h>
60 #include <linux/netdevice.h>
61 #include <linux/if_arp.h>
62 #include <linux/if_ether.h>
63 #include <linux/skbuff.h>
64 #include <linux/delay.h>
65
66 #include <socketcan/can.h>
67 #include <socketcan/can/dev.h>
68 #include <socketcan/can/error.h>
69 #include <socketcan/can/dev.h>
70
71 #include "cc770.h"
72
73 #include <socketcan/can/version.h>      /* for RCSID. Removed by mkpatch script */
74 #define DRV_NAME  "cc770"
75
76 MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
77 MODULE_LICENSE("GPL v2");
78 MODULE_DESCRIPTION(DRV_NAME "CAN netdevice driver");
79
80 /*
81  * The CC770 is a CAN controller from Bosch, which is 100% compatible
82  * with the AN82527 from Intel, but with "bugs" being fixed and some
83  * additional functionality, mainly:
84  *
85  * 1. RX and TX error counters are readable.
86  * 2. Support of silent (listen-only) mode.
87  * 3. Message object 15 can receive all types of frames, also RTR and EFF.
88  *
89  * Details are available from Bosch's "CC770_Product_Info_2007-01.pdf",
90  * which explains in detail the compatibility between the CC770 and the
91  * 82527. This driver use the additional functionality 3. on real CC770
92  * devices. Unfortunately, the CC770 does still not store the message
93  * identifier of received remote transmission request frames and
94  * therefore it's set to 0.
95  *
96  * The message objects 1..14 can be used for TX and RX while the message
97  * objects 15 is optimized for RX. It has a shadow register for reliable
98  * data receiption under heavy bus load. Therefore it makes sense to use
99  * this message object for the needed use case. The frame type (EFF/SFF)
100  * for the message object 15 can be defined via kernel module parameter
101  * "msgobj15_eff". If not equal 0, it will receive 29-bit EFF frames,
102  * otherwise 11 bit SFF messages.
103  */
104 static int msgobj15_eff;
105 module_param(msgobj15_eff, int, S_IRUGO);
106 MODULE_PARM_DESC(msgobj15_eff, "Extended 29-bit frames for message object 15 "
107                  "(default: 11-bit standard frames)");
108
109 static int i82527_compat;
110 module_param(i82527_compat, int, S_IRUGO);
111 MODULE_PARM_DESC(i82527_compat, "Strict Intel 82527 comptibility mode "
112                  "without using additional functions");
113
114 /*
115  * This driver uses the last 5 message objects 11..15. The definitions
116  * and structure below allows to configure and assign them to the real
117  * message object.
118  */
119 static unsigned char cc770_obj_flags[CC770_OBJ_MAX] = {
120         [CC770_OBJ_RX0]     = CC770_OBJ_FLAG_RX,
121         [CC770_OBJ_RX1]     = CC770_OBJ_FLAG_RX | CC770_OBJ_FLAG_EFF,
122         [CC770_OBJ_RX_RTR0] = CC770_OBJ_FLAG_RX | CC770_OBJ_FLAG_RTR,
123         [CC770_OBJ_RX_RTR1] = CC770_OBJ_FLAG_RX | CC770_OBJ_FLAG_RTR |
124                               CC770_OBJ_FLAG_EFF,
125         [CC770_OBJ_TX]      = 0,
126 };
127
128 static struct can_bittiming_const cc770_bittiming_const = {
129         .name = DRV_NAME,
130         .tseg1_min = 1,
131         .tseg1_max = 16,
132         .tseg2_min = 1,
133         .tseg2_max = 8,
134         .sjw_max = 4,
135         .brp_min = 1,
136         .brp_max = 64,
137         .brp_inc = 1,
138 };
139
140 static inline int intid2obj(unsigned int intid)
141 {
142         if (intid == 2)
143                 return 0;
144         else
145                 return MSGOBJ_LAST + 2 - intid;
146 }
147
148 static void enable_all_objs(const struct net_device *dev)
149 {
150         struct cc770_priv *priv = netdev_priv(dev);
151         u8 msgcfg;
152         unsigned char obj_flags;
153         unsigned int o, mo;
154
155         for (o = 0; o <  CC770_OBJ_MAX; o++) {
156                 obj_flags = priv->obj_flags[o];
157                 mo = obj2msgobj(o);
158
159                 if (obj_flags & CC770_OBJ_FLAG_RX) {
160                         /*
161                          * We don't need extra objects for RTR and EFF if
162                          * the additional CC770 functions are enabled.
163                          */
164                         if (priv->control_normal_mode & CTRL_EAF) {
165                                 if (o > 0)
166                                         continue;
167                                 dev_dbg(ND2D(dev), "Message object %d for "
168                                         "RX data, RTR, SFF and EFF\n", mo);
169                         } else {
170                                 dev_dbg(ND2D(dev),
171                                         "Message object %d for RX %s %s\n", mo,
172                                         obj_flags & CC770_OBJ_FLAG_RTR ?
173                                         "RTR" : "data",
174                                         obj_flags & CC770_OBJ_FLAG_EFF ?
175                                           "EFF" : "SFF");
176                         }
177
178                         if (obj_flags & CC770_OBJ_FLAG_EFF)
179                                 msgcfg = MSGCFG_XTD;
180                         else
181                                 msgcfg = 0;
182                         if (obj_flags & CC770_OBJ_FLAG_RTR)
183                                 msgcfg |= MSGCFG_DIR;
184
185                         cc770_write_reg(priv, msgobj[mo].config, msgcfg);
186                         cc770_write_reg(priv, msgobj[mo].ctrl0,
187                                         MSGVAL_SET | TXIE_RES |
188                                         RXIE_SET | INTPND_RES);
189
190                         if (obj_flags & CC770_OBJ_FLAG_RTR)
191                                 cc770_write_reg(priv, msgobj[mo].ctrl1,
192                                                 NEWDAT_RES | CPUUPD_SET |
193                                                 TXRQST_RES | RMTPND_RES);
194                         else
195                                 cc770_write_reg(priv, msgobj[mo].ctrl1,
196                                                 NEWDAT_RES | MSGLST_RES |
197                                                 TXRQST_RES | RMTPND_RES);
198                 } else {
199                         dev_dbg(ND2D(dev), "Message object %d for "
200                                 "TX data, RTR, SFF and EFF\n", mo);
201
202                         cc770_write_reg(priv, msgobj[mo].ctrl1,
203                                         RMTPND_RES | TXRQST_RES |
204                                         CPUUPD_RES | NEWDAT_RES);
205                         cc770_write_reg(priv, msgobj[mo].ctrl0,
206                                         MSGVAL_RES | TXIE_RES |
207                                         RXIE_RES | INTPND_RES);
208                 }
209         }
210 }
211
212 static void disable_all_objs(const struct cc770_priv *priv)
213 {
214         int o, mo;
215
216         for (o = 0; o <  CC770_OBJ_MAX; o++) {
217                 mo = obj2msgobj(o);
218
219                 if (priv->obj_flags[o] & CC770_OBJ_FLAG_RX) {
220                         if (o > 0 &&
221                             priv->control_normal_mode & CTRL_EAF)
222                                 continue;
223
224                         cc770_write_reg(priv, msgobj[mo].ctrl1,
225                                         NEWDAT_RES | MSGLST_RES |
226                                         TXRQST_RES | RMTPND_RES);
227                         cc770_write_reg(priv, msgobj[mo].ctrl0,
228                                         MSGVAL_RES | TXIE_RES |
229                                         RXIE_RES | INTPND_RES);
230                 } else {
231                         /* Clear message object for send */
232                         cc770_write_reg(priv, msgobj[mo].ctrl1,
233                                         RMTPND_RES | TXRQST_RES |
234                                         CPUUPD_RES | NEWDAT_RES);
235                         cc770_write_reg(priv, msgobj[mo].ctrl0,
236                                         MSGVAL_RES | TXIE_RES |
237                                         RXIE_RES | INTPND_RES);
238                 }
239         }
240 }
241
242 static void set_reset_mode(struct net_device *dev)
243 {
244         struct cc770_priv *priv = netdev_priv(dev);
245
246         /* Enable configuration and puts chip in bus-off, disable interrupts */
247         cc770_write_reg(priv, control, CTRL_CCE | CTRL_INI);
248
249         priv->can.state = CAN_STATE_STOPPED;
250
251         /* Clear interrupts */
252         cc770_read_reg(priv, interrupt);
253
254         /* Clear status register */
255         cc770_write_reg(priv, status, 0);
256
257         /* Disable all used message objects */
258         disable_all_objs(priv);
259 }
260
261 static void set_normal_mode(struct net_device *dev)
262 {
263         struct cc770_priv *priv = netdev_priv(dev);
264
265         /* Clear interrupts */
266         cc770_read_reg(priv, interrupt);
267
268         /* Clear status register and pre-set last error code */
269         cc770_write_reg(priv, status, STAT_LEC_MASK);
270
271         /* Enable all used message objects*/
272         enable_all_objs(dev);
273
274         /*
275          * Clear bus-off, interrupts only for errors,
276          * not for status change
277          */
278         cc770_write_reg(priv, control, priv->control_normal_mode);
279
280         priv->can.state = CAN_STATE_ERROR_ACTIVE;
281 }
282
283 static void chipset_init(struct cc770_priv *priv)
284 {
285         int mo, id, data;
286
287         /* Enable configuration and put chip in bus-off, disable interrupts */
288         cc770_write_reg(priv, control, (CTRL_CCE | CTRL_INI));
289
290         /* Set CLKOUT divider and slew rates */
291         cc770_write_reg(priv, clkout, priv->clkout);
292
293         /* Configure CPU interface / CLKOUT enable */
294         cc770_write_reg(priv, cpu_interface, priv->cpu_interface | CPUIF_CEN);
295
296         /* Set bus configuration  */
297         cc770_write_reg(priv, bus_config, priv->bus_config);
298
299         /* Clear interrupts */
300         cc770_read_reg(priv, interrupt);
301
302         /* Clear status register */
303         cc770_write_reg(priv, status, 0);
304
305         /* Clear and invalidate message objects */
306         for (mo = MSGOBJ_FIRST; mo <= MSGOBJ_LAST; mo++) {
307                 cc770_write_reg(priv, msgobj[mo].ctrl0,
308                                 INTPND_UNC | RXIE_RES |
309                                 TXIE_RES | MSGVAL_RES);
310                 cc770_write_reg(priv, msgobj[mo].ctrl0,
311                                 INTPND_RES | RXIE_RES |
312                                 TXIE_RES | MSGVAL_RES);
313                 cc770_write_reg(priv, msgobj[mo].ctrl1,
314                                 NEWDAT_RES | MSGLST_RES |
315                                 TXRQST_RES | RMTPND_RES);
316                 for (data = 0; data < 8; data++)
317                         cc770_write_reg(priv, msgobj[mo].data[data], 0);
318                 for (id = 0; id < 4; id++)
319                         cc770_write_reg(priv, msgobj[mo].id[id], 0);
320                 cc770_write_reg(priv, msgobj[mo].config, 0);
321         }
322
323         /* Set all global ID masks to "don't care" */
324         cc770_write_reg(priv, global_mask_std[0], 0);
325         cc770_write_reg(priv, global_mask_std[1], 0);
326         cc770_write_reg(priv, global_mask_ext[0], 0);
327         cc770_write_reg(priv, global_mask_ext[1], 0);
328         cc770_write_reg(priv, global_mask_ext[2], 0);
329         cc770_write_reg(priv, global_mask_ext[3], 0);
330
331 }
332
333 static int cc770_probe_chip(struct net_device *dev)
334 {
335         struct cc770_priv *priv = netdev_priv(dev);
336
337         /* Enable configuration, put chip in bus-off, disable ints */
338         cc770_write_reg(priv, control, CTRL_CCE | CTRL_EAF | CTRL_INI);
339         /* Configure cpu interface / CLKOUT disable */
340         cc770_write_reg(priv, cpu_interface, priv->cpu_interface);
341
342         /*
343          * Check if hardware reset is still inactive or maybe there
344          * is no chip in this address space
345          */
346         if (cc770_read_reg(priv, cpu_interface) & CPUIF_RST) {
347                 dev_info(ND2D(dev), "probing @0x%p failed (reset)\n",
348                          priv->reg_base);
349                 return 0;
350         }
351
352         /* Write and read back test pattern */
353         cc770_write_reg(priv, msgobj[1].data[1], 0x25);
354         cc770_write_reg(priv, msgobj[2].data[3], 0x52);
355         cc770_write_reg(priv, msgobj[10].data[6], 0xc3);
356         if ((cc770_read_reg(priv, msgobj[1].data[1]) != 0x25) ||
357             (cc770_read_reg(priv, msgobj[2].data[3]) != 0x52) ||
358             (cc770_read_reg(priv, msgobj[10].data[6]) != 0xc3)) {
359                 dev_info(ND2D(dev), "probing @0x%p failed (pattern)\n",
360                          priv->reg_base);
361                 return 0;
362         }
363
364         /* Check if this chip is a CC770 supporting additional functions */
365         if (cc770_read_reg(priv, control) & CTRL_EAF)
366                 priv->control_normal_mode |= CTRL_EAF;
367
368         return 1;
369 }
370
371 static void cc770_start(struct net_device *dev)
372 {
373         struct cc770_priv *priv = netdev_priv(dev);
374
375         /* leave reset mode */
376         if (priv->can.state != CAN_STATE_STOPPED)
377                 set_reset_mode(dev);
378
379         /* leave reset mode */
380         set_normal_mode(dev);
381 }
382
383 static int cc770_set_mode(struct net_device *dev, enum can_mode mode)
384 {
385         struct cc770_priv *priv = netdev_priv(dev);
386
387         if (!priv->open_time)
388                 return -EINVAL;
389
390         switch (mode) {
391         case CAN_MODE_START:
392                 cc770_start(dev);
393                 if (netif_queue_stopped(dev))
394                         netif_wake_queue(dev);
395                 break;
396
397         default:
398                 return -EOPNOTSUPP;
399         }
400
401         return 0;
402 }
403
404 static int cc770_set_bittiming(struct net_device *dev)
405 {
406         struct cc770_priv *priv = netdev_priv(dev);
407         struct can_bittiming *bt = &priv->can.bittiming;
408         u8 btr0, btr1;
409
410         btr0 = ((bt->brp - 1) & 0x3f) | (((bt->sjw - 1) & 0x3) << 6);
411         btr1 = ((bt->prop_seg + bt->phase_seg1 - 1) & 0xf) |
412                 (((bt->phase_seg2 - 1) & 0x7) << 4);
413         if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
414                 btr1 |= 0x80;
415
416         dev_info(ND2D(dev),
417                  "setting BTR0=0x%02x BTR1=0x%02x\n", btr0, btr1);
418
419         cc770_write_reg(priv, bit_timing_0, btr0);
420         cc770_write_reg(priv, bit_timing_1, btr1);
421
422         return 0;
423 }
424
425 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,32)
426 static int cc770_start_xmit(struct sk_buff *skb, struct net_device *dev)
427 #else
428 static netdev_tx_t cc770_start_xmit(struct sk_buff *skb, struct net_device *dev)
429 #endif
430 {
431         struct cc770_priv *priv = netdev_priv(dev);
432 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
433         struct net_device_stats *stats = can_get_stats(dev);
434 #else
435         struct net_device_stats *stats = &dev->stats;
436 #endif
437         struct can_frame *cf = (struct can_frame *)skb->data;
438         unsigned int mo = obj2msgobj(CC770_OBJ_TX);
439         u8 dlc, rtr;
440         u32 id;
441         int i;
442
443         if ((cc770_read_reg(priv,
444                             msgobj[mo].ctrl1) & TXRQST_UNC) == TXRQST_SET) {
445                 dev_err(ND2D(dev), "TX register is still occupied!\n");
446                 return NETDEV_TX_BUSY;
447         }
448
449         netif_stop_queue(dev);
450
451         dlc = cf->can_dlc;
452         id = cf->can_id;
453         if (cf->can_id & CAN_RTR_FLAG)
454                 rtr = 0;
455         else
456                 rtr = MSGCFG_DIR;
457         cc770_write_reg(priv, msgobj[mo].ctrl1,
458                         RMTPND_RES | TXRQST_RES | CPUUPD_SET | NEWDAT_RES);
459         cc770_write_reg(priv, msgobj[mo].ctrl0,
460                         MSGVAL_SET | TXIE_SET | RXIE_RES | INTPND_RES);
461         if (id & CAN_EFF_FLAG) {
462                 id &= CAN_EFF_MASK;
463                 cc770_write_reg(priv, msgobj[mo].config,
464                                 (dlc << 4) + rtr + MSGCFG_XTD);
465                 cc770_write_reg(priv, msgobj[mo].id[3],
466                                 (id << 3) & 0xFFU);
467                 cc770_write_reg(priv, msgobj[mo].id[2],
468                                 (id >> 5) & 0xFFU);
469                 cc770_write_reg(priv, msgobj[mo].id[1],
470                                 (id >> 13) & 0xFFU);
471                 cc770_write_reg(priv, msgobj[mo].id[0],
472                                 (id >> 21) & 0xFFU);
473         } else {
474                 id &= CAN_SFF_MASK;
475                 cc770_write_reg(priv, msgobj[mo].config,
476                                 (dlc << 4) + rtr);
477                 cc770_write_reg(priv, msgobj[mo].id[0],
478                                 (id >> 3) & 0xFFU);
479                 cc770_write_reg(priv, msgobj[mo].id[1],
480                                 (id << 5) & 0xFFU);
481         }
482
483         dlc &= 0x0f;            /* restore length only */
484         for (i = 0; i < dlc; i++)
485                 cc770_write_reg(priv, msgobj[mo].data[i], cf->data[i]);
486
487         cc770_write_reg(priv, msgobj[mo].ctrl1,
488                         RMTPND_RES | TXRQST_SET | CPUUPD_RES | NEWDAT_UNC);
489
490         stats->tx_bytes += dlc;
491         dev->trans_start = jiffies;
492
493         can_put_echo_skb(skb, dev, 0);
494
495         /*
496          * HM: We had some cases of repeated IRQs so make sure the
497          * INT is acknowledged I know it's already further up, but
498          * doing again fixed the issue
499          */
500         cc770_write_reg(priv, msgobj[mo].ctrl0,
501                         MSGVAL_UNC | TXIE_UNC | RXIE_UNC | INTPND_RES);
502
503         return NETDEV_TX_OK;
504 }
505
506 static void cc770_rx(struct net_device *dev, unsigned int mo, u8 ctrl1)
507 {
508         struct cc770_priv *priv = netdev_priv(dev);
509 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
510         struct net_device_stats *stats = can_get_stats(dev);
511 #else
512         struct net_device_stats *stats = &dev->stats;
513 #endif
514         struct can_frame *cf;
515         struct sk_buff *skb;
516         u8 config;
517         u32 id;
518         int i;
519
520         skb = alloc_can_skb(dev, &cf);
521         if (skb == NULL)
522                 return;
523
524         config = cc770_read_reg(priv, msgobj[mo].config);
525
526         if (ctrl1 & RMTPND_SET) {
527                 /*
528                  * Unfortunately, the chip does not store the real message
529                  * identifier of the received remote transmission request
530                  * frame. Therefore we set it to 0.
531                  */
532                 cf->can_id = CAN_RTR_FLAG;
533                 if (config & MSGCFG_XTD)
534                         cf->can_id |= CAN_EFF_FLAG;
535                 cf->can_dlc = 0;
536         } else {
537                 if (config & MSGCFG_XTD) {
538                         id = cc770_read_reg(priv, msgobj[mo].id[3]);
539                         id |= cc770_read_reg(priv, msgobj[mo].id[2]) << 8;
540                         id |= cc770_read_reg(priv, msgobj[mo].id[1]) << 16;
541                         id |= cc770_read_reg(priv, msgobj[mo].id[0]) << 24;
542                         id >>= 3;
543                         id |= CAN_EFF_FLAG;
544                 } else {
545                         id = cc770_read_reg(priv, msgobj[mo].id[1]);
546                         id |= cc770_read_reg(priv, msgobj[mo].id[0]) << 8;
547                         id >>= 5;
548                 }
549
550                 cf->can_id = id;
551                 cf->can_dlc = GET_CAN_DLC((config & 0xf0) >> 4);
552                 for (i = 0; i < cf->can_dlc; i++)
553                         cf->data[i] = cc770_read_reg(priv, msgobj[mo].data[i]);
554         }
555         netif_rx(skb);
556
557 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,32)
558         dev->last_rx = jiffies;
559 #endif
560         stats->rx_packets++;
561         stats->rx_bytes += cf->can_dlc;
562 }
563
564 static int cc770_err(struct net_device *dev, u8 status)
565 {
566         struct cc770_priv *priv = netdev_priv(dev);
567 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
568         struct net_device_stats *stats = can_get_stats(dev);
569 #else
570         struct net_device_stats *stats = &dev->stats;
571 #endif
572         struct can_frame *cf;
573         struct sk_buff *skb;
574         u8 lec;
575
576         dev_dbg(ND2D(dev), "status interrupt (%#x)\n", status);
577
578         skb = alloc_can_err_skb(dev, &cf);
579         if (skb == NULL)
580                 return -ENOMEM;
581
582         if (status & STAT_BOFF) {
583                 /* Disable interrupts */
584                 cc770_write_reg(priv, control, CTRL_INI);
585                 cf->can_id |= CAN_ERR_BUSOFF;
586                 priv->can.state = CAN_STATE_BUS_OFF;
587                 can_bus_off(dev);
588         } else if (status & STAT_WARN) {
589                 cf->can_id |= CAN_ERR_CRTL;
590                 cf->data[1] = CAN_ERR_CRTL_RX_WARNING | CAN_ERR_CRTL_TX_WARNING;
591                 priv->can.state = CAN_STATE_ERROR_WARNING;
592                 priv->can.can_stats.error_warning++;
593         }
594
595         lec = status & STAT_LEC_MASK;
596         if (lec < 7 && lec > 0) {
597                 if (lec == STAT_LEC_ACK) {
598                         cf->can_id |= CAN_ERR_ACK;
599                 } else {
600                         cf->can_id |= CAN_ERR_PROT;
601                         switch (lec) {
602                         case STAT_LEC_STUFF:
603                                 cf->data[2] |= CAN_ERR_PROT_STUFF;
604                                 break;
605                         case STAT_LEC_FORM:
606                                 cf->data[2] |= CAN_ERR_PROT_FORM;
607                                 break;
608                         case STAT_LEC_BIT1:
609                                 cf->data[2] |= CAN_ERR_PROT_BIT1;
610                                 break;
611                         case STAT_LEC_BIT0:
612                                 cf->data[2] |= CAN_ERR_PROT_BIT0;
613                                 break;
614                         case STAT_LEC_CRC:
615                                 cf->data[3] |= CAN_ERR_PROT_LOC_CRC_SEQ;
616                                 break;
617                         }
618                 }
619         }
620
621         netif_rx(skb);
622
623 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,32)
624         dev->last_rx = jiffies;
625 #endif
626         stats->rx_packets++;
627         stats->rx_bytes += cf->can_dlc;
628
629         return 0;
630 }
631
632 static int cc770_status_interrupt(struct net_device *dev)
633 {
634         struct cc770_priv *priv = netdev_priv(dev);
635         u8 status;
636
637         status = cc770_read_reg(priv, status);
638         /* Reset the status register including RXOK and TXOK */
639         cc770_write_reg(priv, status, STAT_LEC_MASK);
640
641         if (status & (STAT_WARN | STAT_BOFF) ||
642             (status & STAT_LEC_MASK) != STAT_LEC_MASK) {
643                 cc770_err(dev, status);
644                 return status & STAT_BOFF;
645         }
646
647         return 0;
648 }
649
650 static void cc770_rx_interrupt(struct net_device *dev, unsigned int o)
651 {
652         struct cc770_priv *priv = netdev_priv(dev);
653 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
654         struct net_device_stats *stats = can_get_stats(dev);
655 #else
656         struct net_device_stats *stats = &dev->stats;
657 #endif
658         unsigned int mo = obj2msgobj(o);
659         u8 ctrl1;
660
661         while (1) {
662                 ctrl1 = cc770_read_reg(priv, msgobj[mo].ctrl1);
663
664                 if (!(ctrl1 & NEWDAT_SET))  {
665                         /* Check for RTR if additional functions are enabled */
666                         if (priv->control_normal_mode & CTRL_EAF) {
667                                 if (!(cc770_read_reg(priv, msgobj[mo].ctrl0) &
668                                       INTPND_SET))
669                                         break;
670                         } else {
671                                 break;
672                         }
673                 }
674
675                 if (ctrl1 & MSGLST_SET) {
676                         stats->rx_over_errors++;
677                         stats->rx_errors++;
678                 }
679                 if (mo < MSGOBJ_LAST)
680                         cc770_write_reg(priv, msgobj[mo].ctrl1,
681                                         NEWDAT_RES | MSGLST_RES |
682                                         TXRQST_UNC | RMTPND_UNC);
683                 cc770_rx(dev, mo, ctrl1);
684
685                 cc770_write_reg(priv, msgobj[mo].ctrl0,
686                                 MSGVAL_SET | TXIE_RES |
687                                 RXIE_SET | INTPND_RES);
688                 cc770_write_reg(priv, msgobj[mo].ctrl1,
689                                 NEWDAT_RES | MSGLST_RES |
690                                 TXRQST_RES | RMTPND_RES);
691         }
692 }
693
694 static void cc770_rtr_interrupt(struct net_device *dev, unsigned int o)
695 {
696         struct cc770_priv *priv = netdev_priv(dev);
697         unsigned int mo = obj2msgobj(o);
698         u8 ctrl0, ctrl1;
699
700         while (1) {
701                 ctrl0 = cc770_read_reg(priv, msgobj[mo].ctrl0);
702                 if (!(ctrl0 & INTPND_SET))
703                         break;
704
705                 ctrl1 = cc770_read_reg(priv, msgobj[mo].ctrl1);
706                 cc770_rx(dev, mo, ctrl1);
707
708                 cc770_write_reg(priv, msgobj[mo].ctrl0,
709                                 MSGVAL_SET | TXIE_RES |
710                                 RXIE_SET | INTPND_RES);
711                 cc770_write_reg(priv, msgobj[mo].ctrl1,
712                                 NEWDAT_RES | CPUUPD_SET |
713                                 TXRQST_RES | RMTPND_RES);
714         }
715 }
716
717 static void cc770_tx_interrupt(struct net_device *dev, unsigned int o)
718 {
719         struct cc770_priv *priv = netdev_priv(dev);
720 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
721         struct net_device_stats *stats = can_get_stats(dev);
722 #else
723         struct net_device_stats *stats = &dev->stats;
724 #endif
725         unsigned int mo = obj2msgobj(o);
726
727         /* Nothing more to send, switch off interrupts */
728         cc770_write_reg(priv, msgobj[mo].ctrl0,
729                         MSGVAL_RES | TXIE_RES | RXIE_RES | INTPND_RES);
730         /*
731          * We had some cases of repeated IRQ so make sure the
732          * INT is acknowledged
733          */
734         cc770_write_reg(priv, msgobj[mo].ctrl0,
735                         MSGVAL_UNC | TXIE_UNC | RXIE_UNC | INTPND_RES);
736
737         stats->tx_packets++;
738         can_get_echo_skb(dev, 0);
739         netif_wake_queue(dev);
740 }
741
742 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
743 irqreturn_t cc770_interrupt(int irq, void *dev_id, struct pt_regs *regs)
744 #else
745 irqreturn_t cc770_interrupt(int irq, void *dev_id)
746 #endif
747 {
748         struct net_device *dev = (struct net_device *)dev_id;
749         struct cc770_priv *priv = netdev_priv(dev);
750         u8 intid;
751         int o, n = 0;
752
753         /* Shared interrupts and IRQ off? */
754         if (priv->can.state == CAN_STATE_STOPPED)
755                 return IRQ_NONE;
756
757         if (priv->pre_irq)
758                 priv->pre_irq(priv);
759
760         while (n < CC770_MAX_IRQ) {
761                 /* Read the highest pending interrupt request */
762                 intid = cc770_read_reg(priv, interrupt);
763                 if (!intid)
764                         break;
765                 n++;
766
767                 if (intid == 1) {
768                         /* Exit in case of bus-off */
769                         if (cc770_status_interrupt(dev))
770                                 break;
771                 } else {
772                         o = intid2obj(intid);
773
774                         if (o >= CC770_OBJ_MAX) {
775                                 dev_err(ND2D(dev),
776                                         "Unexpected interrupt id %d\n", intid);
777                                 continue;
778                         }
779
780                         if (priv->obj_flags[o] & CC770_OBJ_FLAG_RTR)
781                                 cc770_rtr_interrupt(dev, o);
782                         else if (priv->obj_flags[o] & CC770_OBJ_FLAG_RX)
783                                 cc770_rx_interrupt(dev, o);
784                         else
785                                 cc770_tx_interrupt(dev, o);
786                 }
787         }
788
789         if (priv->post_irq)
790                 priv->post_irq(priv);
791
792         if (n >= CC770_MAX_IRQ)
793                 dev_dbg(ND2D(dev), "%d messages handled in ISR", n);
794
795         return (n) ? IRQ_HANDLED : IRQ_NONE;
796 }
797
798 static int cc770_open(struct net_device *dev)
799 {
800         struct cc770_priv *priv = netdev_priv(dev);
801         int err;
802
803         /* set chip into reset mode */
804         set_reset_mode(dev);
805
806         /* common open */
807         err = open_candev(dev);
808         if (err)
809                 return err;
810
811         err = request_irq(dev->irq, &cc770_interrupt, priv->irq_flags,
812                           dev->name, (void *)dev);
813         if (err) {
814                 close_candev(dev);
815                 return -EAGAIN;
816         }
817
818 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
819         /* clear statistics */
820         memset(&priv->can.net_stats, 0, sizeof(priv->can.net_stats));
821 #endif
822
823         /* init and start chip */
824         cc770_start(dev);
825         priv->open_time = jiffies;
826
827         netif_start_queue(dev);
828
829         return 0;
830 }
831
832 static int cc770_close(struct net_device *dev)
833 {
834         struct cc770_priv *priv = netdev_priv(dev);
835
836         netif_stop_queue(dev);
837         set_reset_mode(dev);
838
839         free_irq(dev->irq, (void *)dev);
840         close_candev(dev);
841
842         priv->open_time = 0;
843
844         return 0;
845 }
846
847 struct net_device *alloc_cc770dev(int sizeof_priv)
848 {
849         struct net_device *dev;
850         struct cc770_priv *priv;
851
852         dev = alloc_candev(sizeof(struct cc770_priv) + sizeof_priv,
853                            CC770_ECHO_SKB_MAX);
854         if (!dev)
855                 return NULL;
856
857         priv = netdev_priv(dev);
858
859         priv->dev = dev;
860         priv->can.bittiming_const = &cc770_bittiming_const;
861         priv->can.do_set_bittiming = cc770_set_bittiming;
862         priv->can.do_set_mode = cc770_set_mode;
863
864         memcpy(priv->obj_flags, cc770_obj_flags, sizeof(cc770_obj_flags));
865
866         if (sizeof_priv)
867                 priv->priv = (void *)priv + sizeof(struct cc770_priv);
868
869         return dev;
870 }
871 EXPORT_SYMBOL_GPL(alloc_cc770dev);
872
873 void free_cc770dev(struct net_device *dev)
874 {
875         free_candev(dev);
876 }
877 EXPORT_SYMBOL_GPL(free_cc770dev);
878
879 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,28)
880 static const struct net_device_ops cc770_netdev_ops = {
881         .ndo_open               = cc770_open,
882         .ndo_stop               = cc770_close,
883         .ndo_start_xmit         = cc770_start_xmit,
884 };
885 #endif
886
887 int register_cc770dev(struct net_device *dev)
888 {
889         struct cc770_priv *priv = netdev_priv(dev);
890
891         if (!cc770_probe_chip(dev))
892                 return -ENODEV;
893
894 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,28)
895         dev->netdev_ops = &cc770_netdev_ops;
896 #else
897         dev->open = cc770_open;
898         dev->stop = cc770_close;
899         dev->hard_start_xmit = cc770_start_xmit;
900 #endif
901
902         dev->flags |= IFF_ECHO; /* we support local echo */
903
904         /* Should we use additional functions? */
905         if (!i82527_compat && priv->control_normal_mode & CTRL_EAF) {
906                 priv->control_normal_mode = CTRL_IE | CTRL_EAF | CTRL_EIE;
907                 dev_dbg(ND2D(dev), "i82527 mode with additional functions\n");
908         } else {
909                 priv->control_normal_mode = CTRL_IE | CTRL_EIE;
910                 dev_dbg(ND2D(dev), "strict i82527 compatibility mode\n");
911         }
912
913         chipset_init(priv);
914         set_reset_mode(dev);
915
916         return register_candev(dev);
917 }
918 EXPORT_SYMBOL_GPL(register_cc770dev);
919
920 void unregister_cc770dev(struct net_device *dev)
921 {
922         set_reset_mode(dev);
923         unregister_candev(dev);
924 }
925 EXPORT_SYMBOL_GPL(unregister_cc770dev);
926
927 static __init int cc770_init(void)
928 {
929         if (msgobj15_eff) {
930                 cc770_obj_flags[CC770_OBJ_RX0] |= CC770_OBJ_FLAG_EFF;
931                 cc770_obj_flags[CC770_OBJ_RX1] &= ~CC770_OBJ_FLAG_EFF;
932         }
933
934         printk(KERN_INFO "%s CAN netdevice driver\n", DRV_NAME);
935
936         return 0;
937 }
938
939 module_init(cc770_init);
940
941 static __exit void cc770_exit(void)
942 {
943         printk(KERN_INFO "%s: driver removed\n", DRV_NAME);
944 }
945 module_exit(cc770_exit);