Avoid beyond bounds copy while caching ACL
[zen-stable.git] / drivers / net / can / cc770 / cc770.c
blobc30f0e6f104875a3b5fad4789904d0e048504da6
1 /*
2 * Core driver for the CC770 and AN82527 CAN controllers
4 * Copyright (C) 2009, 2011 Wolfgang Grandegger <wg@grandegger.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the version 2 of the GNU General Public License
8 * as published by the Free Software Foundation
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/kernel.h>
21 #include <linux/sched.h>
22 #include <linux/types.h>
23 #include <linux/fcntl.h>
24 #include <linux/interrupt.h>
25 #include <linux/ptrace.h>
26 #include <linux/string.h>
27 #include <linux/errno.h>
28 #include <linux/netdevice.h>
29 #include <linux/if_arp.h>
30 #include <linux/if_ether.h>
31 #include <linux/skbuff.h>
32 #include <linux/delay.h>
34 #include <linux/can.h>
35 #include <linux/can/dev.h>
36 #include <linux/can/error.h>
37 #include <linux/can/dev.h>
38 #include <linux/can/platform/cc770.h>
40 #include "cc770.h"
42 MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
43 MODULE_LICENSE("GPL v2");
44 MODULE_DESCRIPTION(KBUILD_MODNAME "CAN netdevice driver");
47 * The CC770 is a CAN controller from Bosch, which is 100% compatible
48 * with the AN82527 from Intel, but with "bugs" being fixed and some
49 * additional functionality, mainly:
51 * 1. RX and TX error counters are readable.
52 * 2. Support of silent (listen-only) mode.
53 * 3. Message object 15 can receive all types of frames, also RTR and EFF.
55 * Details are available from Bosch's "CC770_Product_Info_2007-01.pdf",
56 * which explains in detail the compatibility between the CC770 and the
57 * 82527. This driver use the additional functionality 3. on real CC770
58 * devices. Unfortunately, the CC770 does still not store the message
59 * identifier of received remote transmission request frames and
60 * therefore it's set to 0.
62 * The message objects 1..14 can be used for TX and RX while the message
63 * objects 15 is optimized for RX. It has a shadow register for reliable
64 * data receiption under heavy bus load. Therefore it makes sense to use
65 * this message object for the needed use case. The frame type (EFF/SFF)
66 * for the message object 15 can be defined via kernel module parameter
67 * "msgobj15_eff". If not equal 0, it will receive 29-bit EFF frames,
68 * otherwise 11 bit SFF messages.
70 static int msgobj15_eff;
71 module_param(msgobj15_eff, int, S_IRUGO);
72 MODULE_PARM_DESC(msgobj15_eff, "Extended 29-bit frames for message object 15 "
73 "(default: 11-bit standard frames)");
75 static int i82527_compat;
76 module_param(i82527_compat, int, S_IRUGO);
77 MODULE_PARM_DESC(i82527_compat, "Strict Intel 82527 comptibility mode "
78 "without using additional functions");
81 * This driver uses the last 5 message objects 11..15. The definitions
82 * and structure below allows to configure and assign them to the real
83 * message object.
85 static unsigned char cc770_obj_flags[CC770_OBJ_MAX] = {
86 [CC770_OBJ_RX0] = CC770_OBJ_FLAG_RX,
87 [CC770_OBJ_RX1] = CC770_OBJ_FLAG_RX | CC770_OBJ_FLAG_EFF,
88 [CC770_OBJ_RX_RTR0] = CC770_OBJ_FLAG_RX | CC770_OBJ_FLAG_RTR,
89 [CC770_OBJ_RX_RTR1] = CC770_OBJ_FLAG_RX | CC770_OBJ_FLAG_RTR |
90 CC770_OBJ_FLAG_EFF,
91 [CC770_OBJ_TX] = 0,
94 static struct can_bittiming_const cc770_bittiming_const = {
95 .name = KBUILD_MODNAME,
96 .tseg1_min = 1,
97 .tseg1_max = 16,
98 .tseg2_min = 1,
99 .tseg2_max = 8,
100 .sjw_max = 4,
101 .brp_min = 1,
102 .brp_max = 64,
103 .brp_inc = 1,
106 static inline int intid2obj(unsigned int intid)
108 if (intid == 2)
109 return 0;
110 else
111 return MSGOBJ_LAST + 2 - intid;
114 static void enable_all_objs(const struct net_device *dev)
116 struct cc770_priv *priv = netdev_priv(dev);
117 u8 msgcfg;
118 unsigned char obj_flags;
119 unsigned int o, mo;
121 for (o = 0; o < ARRAY_SIZE(priv->obj_flags); o++) {
122 obj_flags = priv->obj_flags[o];
123 mo = obj2msgobj(o);
125 if (obj_flags & CC770_OBJ_FLAG_RX) {
127 * We don't need extra objects for RTR and EFF if
128 * the additional CC770 functions are enabled.
130 if (priv->control_normal_mode & CTRL_EAF) {
131 if (o > 0)
132 continue;
133 netdev_dbg(dev, "Message object %d for "
134 "RX data, RTR, SFF and EFF\n", mo);
135 } else {
136 netdev_dbg(dev,
137 "Message object %d for RX %s %s\n",
138 mo, obj_flags & CC770_OBJ_FLAG_RTR ?
139 "RTR" : "data",
140 obj_flags & CC770_OBJ_FLAG_EFF ?
141 "EFF" : "SFF");
144 if (obj_flags & CC770_OBJ_FLAG_EFF)
145 msgcfg = MSGCFG_XTD;
146 else
147 msgcfg = 0;
148 if (obj_flags & CC770_OBJ_FLAG_RTR)
149 msgcfg |= MSGCFG_DIR;
151 cc770_write_reg(priv, msgobj[mo].config, msgcfg);
152 cc770_write_reg(priv, msgobj[mo].ctrl0,
153 MSGVAL_SET | TXIE_RES |
154 RXIE_SET | INTPND_RES);
156 if (obj_flags & CC770_OBJ_FLAG_RTR)
157 cc770_write_reg(priv, msgobj[mo].ctrl1,
158 NEWDAT_RES | CPUUPD_SET |
159 TXRQST_RES | RMTPND_RES);
160 else
161 cc770_write_reg(priv, msgobj[mo].ctrl1,
162 NEWDAT_RES | MSGLST_RES |
163 TXRQST_RES | RMTPND_RES);
164 } else {
165 netdev_dbg(dev, "Message object %d for "
166 "TX data, RTR, SFF and EFF\n", mo);
168 cc770_write_reg(priv, msgobj[mo].ctrl1,
169 RMTPND_RES | TXRQST_RES |
170 CPUUPD_RES | NEWDAT_RES);
171 cc770_write_reg(priv, msgobj[mo].ctrl0,
172 MSGVAL_RES | TXIE_RES |
173 RXIE_RES | INTPND_RES);
178 static void disable_all_objs(const struct cc770_priv *priv)
180 int o, mo;
182 for (o = 0; o < ARRAY_SIZE(priv->obj_flags); o++) {
183 mo = obj2msgobj(o);
185 if (priv->obj_flags[o] & CC770_OBJ_FLAG_RX) {
186 if (o > 0 && priv->control_normal_mode & CTRL_EAF)
187 continue;
189 cc770_write_reg(priv, msgobj[mo].ctrl1,
190 NEWDAT_RES | MSGLST_RES |
191 TXRQST_RES | RMTPND_RES);
192 cc770_write_reg(priv, msgobj[mo].ctrl0,
193 MSGVAL_RES | TXIE_RES |
194 RXIE_RES | INTPND_RES);
195 } else {
196 /* Clear message object for send */
197 cc770_write_reg(priv, msgobj[mo].ctrl1,
198 RMTPND_RES | TXRQST_RES |
199 CPUUPD_RES | NEWDAT_RES);
200 cc770_write_reg(priv, msgobj[mo].ctrl0,
201 MSGVAL_RES | TXIE_RES |
202 RXIE_RES | INTPND_RES);
207 static void set_reset_mode(struct net_device *dev)
209 struct cc770_priv *priv = netdev_priv(dev);
211 /* Enable configuration and puts chip in bus-off, disable interrupts */
212 cc770_write_reg(priv, control, CTRL_CCE | CTRL_INI);
214 priv->can.state = CAN_STATE_STOPPED;
216 /* Clear interrupts */
217 cc770_read_reg(priv, interrupt);
219 /* Clear status register */
220 cc770_write_reg(priv, status, 0);
222 /* Disable all used message objects */
223 disable_all_objs(priv);
226 static void set_normal_mode(struct net_device *dev)
228 struct cc770_priv *priv = netdev_priv(dev);
230 /* Clear interrupts */
231 cc770_read_reg(priv, interrupt);
233 /* Clear status register and pre-set last error code */
234 cc770_write_reg(priv, status, STAT_LEC_MASK);
236 /* Enable all used message objects*/
237 enable_all_objs(dev);
240 * Clear bus-off, interrupts only for errors,
241 * not for status change
243 cc770_write_reg(priv, control, priv->control_normal_mode);
245 priv->can.state = CAN_STATE_ERROR_ACTIVE;
248 static void chipset_init(struct cc770_priv *priv)
250 int mo, id, data;
252 /* Enable configuration and put chip in bus-off, disable interrupts */
253 cc770_write_reg(priv, control, (CTRL_CCE | CTRL_INI));
255 /* Set CLKOUT divider and slew rates */
256 cc770_write_reg(priv, clkout, priv->clkout);
258 /* Configure CPU interface / CLKOUT enable */
259 cc770_write_reg(priv, cpu_interface, priv->cpu_interface);
261 /* Set bus configuration */
262 cc770_write_reg(priv, bus_config, priv->bus_config);
264 /* Clear interrupts */
265 cc770_read_reg(priv, interrupt);
267 /* Clear status register */
268 cc770_write_reg(priv, status, 0);
270 /* Clear and invalidate message objects */
271 for (mo = MSGOBJ_FIRST; mo <= MSGOBJ_LAST; mo++) {
272 cc770_write_reg(priv, msgobj[mo].ctrl0,
273 INTPND_UNC | RXIE_RES |
274 TXIE_RES | MSGVAL_RES);
275 cc770_write_reg(priv, msgobj[mo].ctrl0,
276 INTPND_RES | RXIE_RES |
277 TXIE_RES | MSGVAL_RES);
278 cc770_write_reg(priv, msgobj[mo].ctrl1,
279 NEWDAT_RES | MSGLST_RES |
280 TXRQST_RES | RMTPND_RES);
281 for (data = 0; data < 8; data++)
282 cc770_write_reg(priv, msgobj[mo].data[data], 0);
283 for (id = 0; id < 4; id++)
284 cc770_write_reg(priv, msgobj[mo].id[id], 0);
285 cc770_write_reg(priv, msgobj[mo].config, 0);
288 /* Set all global ID masks to "don't care" */
289 cc770_write_reg(priv, global_mask_std[0], 0);
290 cc770_write_reg(priv, global_mask_std[1], 0);
291 cc770_write_reg(priv, global_mask_ext[0], 0);
292 cc770_write_reg(priv, global_mask_ext[1], 0);
293 cc770_write_reg(priv, global_mask_ext[2], 0);
294 cc770_write_reg(priv, global_mask_ext[3], 0);
298 static int cc770_probe_chip(struct net_device *dev)
300 struct cc770_priv *priv = netdev_priv(dev);
302 /* Enable configuration, put chip in bus-off, disable ints */
303 cc770_write_reg(priv, control, CTRL_CCE | CTRL_EAF | CTRL_INI);
304 /* Configure cpu interface / CLKOUT disable */
305 cc770_write_reg(priv, cpu_interface, priv->cpu_interface);
308 * Check if hardware reset is still inactive or maybe there
309 * is no chip in this address space
311 if (cc770_read_reg(priv, cpu_interface) & CPUIF_RST) {
312 netdev_info(dev, "probing @0x%p failed (reset)\n",
313 priv->reg_base);
314 return -ENODEV;
317 /* Write and read back test pattern (some arbitrary values) */
318 cc770_write_reg(priv, msgobj[1].data[1], 0x25);
319 cc770_write_reg(priv, msgobj[2].data[3], 0x52);
320 cc770_write_reg(priv, msgobj[10].data[6], 0xc3);
321 if ((cc770_read_reg(priv, msgobj[1].data[1]) != 0x25) ||
322 (cc770_read_reg(priv, msgobj[2].data[3]) != 0x52) ||
323 (cc770_read_reg(priv, msgobj[10].data[6]) != 0xc3)) {
324 netdev_info(dev, "probing @0x%p failed (pattern)\n",
325 priv->reg_base);
326 return -ENODEV;
329 /* Check if this chip is a CC770 supporting additional functions */
330 if (cc770_read_reg(priv, control) & CTRL_EAF)
331 priv->control_normal_mode |= CTRL_EAF;
333 return 0;
336 static void cc770_start(struct net_device *dev)
338 struct cc770_priv *priv = netdev_priv(dev);
340 /* leave reset mode */
341 if (priv->can.state != CAN_STATE_STOPPED)
342 set_reset_mode(dev);
344 /* leave reset mode */
345 set_normal_mode(dev);
348 static int cc770_set_mode(struct net_device *dev, enum can_mode mode)
350 switch (mode) {
351 case CAN_MODE_START:
352 cc770_start(dev);
353 netif_wake_queue(dev);
354 break;
356 default:
357 return -EOPNOTSUPP;
360 return 0;
363 static int cc770_set_bittiming(struct net_device *dev)
365 struct cc770_priv *priv = netdev_priv(dev);
366 struct can_bittiming *bt = &priv->can.bittiming;
367 u8 btr0, btr1;
369 btr0 = ((bt->brp - 1) & 0x3f) | (((bt->sjw - 1) & 0x3) << 6);
370 btr1 = ((bt->prop_seg + bt->phase_seg1 - 1) & 0xf) |
371 (((bt->phase_seg2 - 1) & 0x7) << 4);
372 if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
373 btr1 |= 0x80;
375 netdev_info(dev, "setting BTR0=0x%02x BTR1=0x%02x\n", btr0, btr1);
377 cc770_write_reg(priv, bit_timing_0, btr0);
378 cc770_write_reg(priv, bit_timing_1, btr1);
380 return 0;
383 static int cc770_get_berr_counter(const struct net_device *dev,
384 struct can_berr_counter *bec)
386 struct cc770_priv *priv = netdev_priv(dev);
388 bec->txerr = cc770_read_reg(priv, tx_error_counter);
389 bec->rxerr = cc770_read_reg(priv, rx_error_counter);
391 return 0;
394 static netdev_tx_t cc770_start_xmit(struct sk_buff *skb, struct net_device *dev)
396 struct cc770_priv *priv = netdev_priv(dev);
397 struct net_device_stats *stats = &dev->stats;
398 struct can_frame *cf = (struct can_frame *)skb->data;
399 unsigned int mo = obj2msgobj(CC770_OBJ_TX);
400 u8 dlc, rtr;
401 u32 id;
402 int i;
404 if (can_dropped_invalid_skb(dev, skb))
405 return NETDEV_TX_OK;
407 if ((cc770_read_reg(priv,
408 msgobj[mo].ctrl1) & TXRQST_UNC) == TXRQST_SET) {
409 netdev_err(dev, "TX register is still occupied!\n");
410 return NETDEV_TX_BUSY;
413 netif_stop_queue(dev);
415 dlc = cf->can_dlc;
416 id = cf->can_id;
417 if (cf->can_id & CAN_RTR_FLAG)
418 rtr = 0;
419 else
420 rtr = MSGCFG_DIR;
421 cc770_write_reg(priv, msgobj[mo].ctrl1,
422 RMTPND_RES | TXRQST_RES | CPUUPD_SET | NEWDAT_RES);
423 cc770_write_reg(priv, msgobj[mo].ctrl0,
424 MSGVAL_SET | TXIE_SET | RXIE_RES | INTPND_RES);
425 if (id & CAN_EFF_FLAG) {
426 id &= CAN_EFF_MASK;
427 cc770_write_reg(priv, msgobj[mo].config,
428 (dlc << 4) | rtr | MSGCFG_XTD);
429 cc770_write_reg(priv, msgobj[mo].id[3], id << 3);
430 cc770_write_reg(priv, msgobj[mo].id[2], id >> 5);
431 cc770_write_reg(priv, msgobj[mo].id[1], id >> 13);
432 cc770_write_reg(priv, msgobj[mo].id[0], id >> 21);
433 } else {
434 id &= CAN_SFF_MASK;
435 cc770_write_reg(priv, msgobj[mo].config, (dlc << 4) | rtr);
436 cc770_write_reg(priv, msgobj[mo].id[0], id >> 3);
437 cc770_write_reg(priv, msgobj[mo].id[1], id << 5);
440 for (i = 0; i < dlc; i++)
441 cc770_write_reg(priv, msgobj[mo].data[i], cf->data[i]);
443 /* Store echo skb before starting the transfer */
444 can_put_echo_skb(skb, dev, 0);
446 cc770_write_reg(priv, msgobj[mo].ctrl1,
447 RMTPND_RES | TXRQST_SET | CPUUPD_RES | NEWDAT_UNC);
449 stats->tx_bytes += dlc;
453 * HM: We had some cases of repeated IRQs so make sure the
454 * INT is acknowledged I know it's already further up, but
455 * doing again fixed the issue
457 cc770_write_reg(priv, msgobj[mo].ctrl0,
458 MSGVAL_UNC | TXIE_UNC | RXIE_UNC | INTPND_RES);
460 return NETDEV_TX_OK;
463 static void cc770_rx(struct net_device *dev, unsigned int mo, u8 ctrl1)
465 struct cc770_priv *priv = netdev_priv(dev);
466 struct net_device_stats *stats = &dev->stats;
467 struct can_frame *cf;
468 struct sk_buff *skb;
469 u8 config;
470 u32 id;
471 int i;
473 skb = alloc_can_skb(dev, &cf);
474 if (!skb)
475 return;
477 config = cc770_read_reg(priv, msgobj[mo].config);
479 if (ctrl1 & RMTPND_SET) {
481 * Unfortunately, the chip does not store the real message
482 * identifier of the received remote transmission request
483 * frame. Therefore we set it to 0.
485 cf->can_id = CAN_RTR_FLAG;
486 if (config & MSGCFG_XTD)
487 cf->can_id |= CAN_EFF_FLAG;
488 cf->can_dlc = 0;
489 } else {
490 if (config & MSGCFG_XTD) {
491 id = cc770_read_reg(priv, msgobj[mo].id[3]);
492 id |= cc770_read_reg(priv, msgobj[mo].id[2]) << 8;
493 id |= cc770_read_reg(priv, msgobj[mo].id[1]) << 16;
494 id |= cc770_read_reg(priv, msgobj[mo].id[0]) << 24;
495 id >>= 3;
496 id |= CAN_EFF_FLAG;
497 } else {
498 id = cc770_read_reg(priv, msgobj[mo].id[1]);
499 id |= cc770_read_reg(priv, msgobj[mo].id[0]) << 8;
500 id >>= 5;
503 cf->can_id = id;
504 cf->can_dlc = get_can_dlc((config & 0xf0) >> 4);
505 for (i = 0; i < cf->can_dlc; i++)
506 cf->data[i] = cc770_read_reg(priv, msgobj[mo].data[i]);
508 netif_rx(skb);
510 stats->rx_packets++;
511 stats->rx_bytes += cf->can_dlc;
514 static int cc770_err(struct net_device *dev, u8 status)
516 struct cc770_priv *priv = netdev_priv(dev);
517 struct net_device_stats *stats = &dev->stats;
518 struct can_frame *cf;
519 struct sk_buff *skb;
520 u8 lec;
522 netdev_dbg(dev, "status interrupt (%#x)\n", status);
524 skb = alloc_can_err_skb(dev, &cf);
525 if (!skb)
526 return -ENOMEM;
528 /* Use extended functions of the CC770 */
529 if (priv->control_normal_mode & CTRL_EAF) {
530 cf->data[6] = cc770_read_reg(priv, tx_error_counter);
531 cf->data[7] = cc770_read_reg(priv, rx_error_counter);
534 if (status & STAT_BOFF) {
535 /* Disable interrupts */
536 cc770_write_reg(priv, control, CTRL_INI);
537 cf->can_id |= CAN_ERR_BUSOFF;
538 priv->can.state = CAN_STATE_BUS_OFF;
539 can_bus_off(dev);
540 } else if (status & STAT_WARN) {
541 cf->can_id |= CAN_ERR_CRTL;
542 /* Only the CC770 does show error passive */
543 if (cf->data[7] > 127) {
544 cf->data[1] = CAN_ERR_CRTL_RX_PASSIVE |
545 CAN_ERR_CRTL_TX_PASSIVE;
546 priv->can.state = CAN_STATE_ERROR_PASSIVE;
547 priv->can.can_stats.error_passive++;
548 } else {
549 cf->data[1] = CAN_ERR_CRTL_RX_WARNING |
550 CAN_ERR_CRTL_TX_WARNING;
551 priv->can.state = CAN_STATE_ERROR_WARNING;
552 priv->can.can_stats.error_warning++;
554 } else {
555 /* Back to error avtive */
556 cf->can_id |= CAN_ERR_PROT;
557 cf->data[2] = CAN_ERR_PROT_ACTIVE;
558 priv->can.state = CAN_STATE_ERROR_ACTIVE;
561 lec = status & STAT_LEC_MASK;
562 if (lec < 7 && lec > 0) {
563 if (lec == STAT_LEC_ACK) {
564 cf->can_id |= CAN_ERR_ACK;
565 } else {
566 cf->can_id |= CAN_ERR_PROT;
567 switch (lec) {
568 case STAT_LEC_STUFF:
569 cf->data[2] |= CAN_ERR_PROT_STUFF;
570 break;
571 case STAT_LEC_FORM:
572 cf->data[2] |= CAN_ERR_PROT_FORM;
573 break;
574 case STAT_LEC_BIT1:
575 cf->data[2] |= CAN_ERR_PROT_BIT1;
576 break;
577 case STAT_LEC_BIT0:
578 cf->data[2] |= CAN_ERR_PROT_BIT0;
579 break;
580 case STAT_LEC_CRC:
581 cf->data[3] |= CAN_ERR_PROT_LOC_CRC_SEQ;
582 break;
587 netif_rx(skb);
589 stats->rx_packets++;
590 stats->rx_bytes += cf->can_dlc;
592 return 0;
595 static int cc770_status_interrupt(struct net_device *dev)
597 struct cc770_priv *priv = netdev_priv(dev);
598 u8 status;
600 status = cc770_read_reg(priv, status);
601 /* Reset the status register including RXOK and TXOK */
602 cc770_write_reg(priv, status, STAT_LEC_MASK);
604 if (status & (STAT_WARN | STAT_BOFF) ||
605 (status & STAT_LEC_MASK) != STAT_LEC_MASK) {
606 cc770_err(dev, status);
607 return status & STAT_BOFF;
610 return 0;
613 static void cc770_rx_interrupt(struct net_device *dev, unsigned int o)
615 struct cc770_priv *priv = netdev_priv(dev);
616 struct net_device_stats *stats = &dev->stats;
617 unsigned int mo = obj2msgobj(o);
618 u8 ctrl1;
619 int n = CC770_MAX_MSG;
621 while (n--) {
622 ctrl1 = cc770_read_reg(priv, msgobj[mo].ctrl1);
624 if (!(ctrl1 & NEWDAT_SET)) {
625 /* Check for RTR if additional functions are enabled */
626 if (priv->control_normal_mode & CTRL_EAF) {
627 if (!(cc770_read_reg(priv, msgobj[mo].ctrl0) &
628 INTPND_SET))
629 break;
630 } else {
631 break;
635 if (ctrl1 & MSGLST_SET) {
636 stats->rx_over_errors++;
637 stats->rx_errors++;
639 if (mo < MSGOBJ_LAST)
640 cc770_write_reg(priv, msgobj[mo].ctrl1,
641 NEWDAT_RES | MSGLST_RES |
642 TXRQST_UNC | RMTPND_UNC);
643 cc770_rx(dev, mo, ctrl1);
645 cc770_write_reg(priv, msgobj[mo].ctrl0,
646 MSGVAL_SET | TXIE_RES |
647 RXIE_SET | INTPND_RES);
648 cc770_write_reg(priv, msgobj[mo].ctrl1,
649 NEWDAT_RES | MSGLST_RES |
650 TXRQST_RES | RMTPND_RES);
654 static void cc770_rtr_interrupt(struct net_device *dev, unsigned int o)
656 struct cc770_priv *priv = netdev_priv(dev);
657 unsigned int mo = obj2msgobj(o);
658 u8 ctrl0, ctrl1;
659 int n = CC770_MAX_MSG;
661 while (n--) {
662 ctrl0 = cc770_read_reg(priv, msgobj[mo].ctrl0);
663 if (!(ctrl0 & INTPND_SET))
664 break;
666 ctrl1 = cc770_read_reg(priv, msgobj[mo].ctrl1);
667 cc770_rx(dev, mo, ctrl1);
669 cc770_write_reg(priv, msgobj[mo].ctrl0,
670 MSGVAL_SET | TXIE_RES |
671 RXIE_SET | INTPND_RES);
672 cc770_write_reg(priv, msgobj[mo].ctrl1,
673 NEWDAT_RES | CPUUPD_SET |
674 TXRQST_RES | RMTPND_RES);
678 static void cc770_tx_interrupt(struct net_device *dev, unsigned int o)
680 struct cc770_priv *priv = netdev_priv(dev);
681 struct net_device_stats *stats = &dev->stats;
682 unsigned int mo = obj2msgobj(o);
684 /* Nothing more to send, switch off interrupts */
685 cc770_write_reg(priv, msgobj[mo].ctrl0,
686 MSGVAL_RES | TXIE_RES | RXIE_RES | INTPND_RES);
688 * We had some cases of repeated IRQ so make sure the
689 * INT is acknowledged
691 cc770_write_reg(priv, msgobj[mo].ctrl0,
692 MSGVAL_UNC | TXIE_UNC | RXIE_UNC | INTPND_RES);
694 stats->tx_packets++;
695 can_get_echo_skb(dev, 0);
696 netif_wake_queue(dev);
699 irqreturn_t cc770_interrupt(int irq, void *dev_id)
701 struct net_device *dev = (struct net_device *)dev_id;
702 struct cc770_priv *priv = netdev_priv(dev);
703 u8 intid;
704 int o, n = 0;
706 /* Shared interrupts and IRQ off? */
707 if (priv->can.state == CAN_STATE_STOPPED)
708 return IRQ_NONE;
710 if (priv->pre_irq)
711 priv->pre_irq(priv);
713 while (n < CC770_MAX_IRQ) {
714 /* Read the highest pending interrupt request */
715 intid = cc770_read_reg(priv, interrupt);
716 if (!intid)
717 break;
718 n++;
720 if (intid == 1) {
721 /* Exit in case of bus-off */
722 if (cc770_status_interrupt(dev))
723 break;
724 } else {
725 o = intid2obj(intid);
727 if (o >= CC770_OBJ_MAX) {
728 netdev_err(dev, "Unexpected interrupt id %d\n",
729 intid);
730 continue;
733 if (priv->obj_flags[o] & CC770_OBJ_FLAG_RTR)
734 cc770_rtr_interrupt(dev, o);
735 else if (priv->obj_flags[o] & CC770_OBJ_FLAG_RX)
736 cc770_rx_interrupt(dev, o);
737 else
738 cc770_tx_interrupt(dev, o);
742 if (priv->post_irq)
743 priv->post_irq(priv);
745 if (n >= CC770_MAX_IRQ)
746 netdev_dbg(dev, "%d messages handled in ISR", n);
748 return (n) ? IRQ_HANDLED : IRQ_NONE;
751 static int cc770_open(struct net_device *dev)
753 struct cc770_priv *priv = netdev_priv(dev);
754 int err;
756 /* set chip into reset mode */
757 set_reset_mode(dev);
759 /* common open */
760 err = open_candev(dev);
761 if (err)
762 return err;
764 err = request_irq(dev->irq, &cc770_interrupt, priv->irq_flags,
765 dev->name, dev);
766 if (err) {
767 close_candev(dev);
768 return -EAGAIN;
771 /* init and start chip */
772 cc770_start(dev);
774 netif_start_queue(dev);
776 return 0;
779 static int cc770_close(struct net_device *dev)
781 netif_stop_queue(dev);
782 set_reset_mode(dev);
784 free_irq(dev->irq, dev);
785 close_candev(dev);
787 return 0;
790 struct net_device *alloc_cc770dev(int sizeof_priv)
792 struct net_device *dev;
793 struct cc770_priv *priv;
795 dev = alloc_candev(sizeof(struct cc770_priv) + sizeof_priv,
796 CC770_ECHO_SKB_MAX);
797 if (!dev)
798 return NULL;
800 priv = netdev_priv(dev);
802 priv->dev = dev;
803 priv->can.bittiming_const = &cc770_bittiming_const;
804 priv->can.do_set_bittiming = cc770_set_bittiming;
805 priv->can.do_set_mode = cc770_set_mode;
806 priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES;
808 memcpy(priv->obj_flags, cc770_obj_flags, sizeof(cc770_obj_flags));
810 if (sizeof_priv)
811 priv->priv = (void *)priv + sizeof(struct cc770_priv);
813 return dev;
815 EXPORT_SYMBOL_GPL(alloc_cc770dev);
817 void free_cc770dev(struct net_device *dev)
819 free_candev(dev);
821 EXPORT_SYMBOL_GPL(free_cc770dev);
823 static const struct net_device_ops cc770_netdev_ops = {
824 .ndo_open = cc770_open,
825 .ndo_stop = cc770_close,
826 .ndo_start_xmit = cc770_start_xmit,
829 int register_cc770dev(struct net_device *dev)
831 struct cc770_priv *priv = netdev_priv(dev);
832 int err;
834 err = cc770_probe_chip(dev);
835 if (err)
836 return err;
838 dev->netdev_ops = &cc770_netdev_ops;
840 dev->flags |= IFF_ECHO; /* we support local echo */
842 /* Should we use additional functions? */
843 if (!i82527_compat && priv->control_normal_mode & CTRL_EAF) {
844 priv->can.do_get_berr_counter = cc770_get_berr_counter;
845 priv->control_normal_mode = CTRL_IE | CTRL_EAF | CTRL_EIE;
846 netdev_dbg(dev, "i82527 mode with additional functions\n");
847 } else {
848 priv->control_normal_mode = CTRL_IE | CTRL_EIE;
849 netdev_dbg(dev, "strict i82527 compatibility mode\n");
852 chipset_init(priv);
853 set_reset_mode(dev);
855 return register_candev(dev);
857 EXPORT_SYMBOL_GPL(register_cc770dev);
859 void unregister_cc770dev(struct net_device *dev)
861 set_reset_mode(dev);
862 unregister_candev(dev);
864 EXPORT_SYMBOL_GPL(unregister_cc770dev);
866 static __init int cc770_init(void)
868 if (msgobj15_eff) {
869 cc770_obj_flags[CC770_OBJ_RX0] |= CC770_OBJ_FLAG_EFF;
870 cc770_obj_flags[CC770_OBJ_RX1] &= ~CC770_OBJ_FLAG_EFF;
873 pr_info("CAN netdevice driver\n");
875 return 0;
877 module_init(cc770_init);
879 static __exit void cc770_exit(void)
881 pr_info("driver removed\n");
883 module_exit(cc770_exit);