4 * cc770.c - Bosch CC770 and Intel AN82527 network device driver
6 * Copyright (C) 2009 Wolfgang Grandegger <wg@grandegger.com>
8 * Derived from the old Socket-CAN i82527 driver:
10 * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
11 * All rights reserved.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
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.
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.
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.
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
46 * Send feedback to <socketcan-users@lists.berlios.de>
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>
66 #include <socketcan/can.h>
67 #include <socketcan/can/dev.h>
68 #include <socketcan/can/error.h>
69 #include <socketcan/can/dev.h>
73 #include <socketcan/can/version.h> /* for RCSID. Removed by mkpatch script */
74 #define DRV_NAME "cc770"
76 MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
77 MODULE_LICENSE("GPL v2");
78 MODULE_DESCRIPTION(DRV_NAME "CAN netdevice driver");
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:
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.
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.
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.
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)");
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");
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
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 |
128 static struct can_bittiming_const cc770_bittiming_const = {
140 static inline int intid2obj(unsigned int intid)
145 return MSGOBJ_LAST + 2 - intid;
148 static void enable_all_objs(const struct net_device *dev)
150 struct cc770_priv *priv = netdev_priv(dev);
152 unsigned char obj_flags;
155 for (o = 0; o < CC770_OBJ_MAX; o++) {
156 obj_flags = priv->obj_flags[o];
159 if (obj_flags & CC770_OBJ_FLAG_RX) {
161 * We don't need extra objects for RTR and EFF if
162 * the additional CC770 functions are enabled.
164 if (priv->control_normal_mode & CTRL_EAF) {
167 dev_dbg(ND2D(dev), "Message object %d for "
168 "RX data, RTR, SFF and EFF\n", mo);
171 "Message object %d for RX %s %s\n", mo,
172 obj_flags & CC770_OBJ_FLAG_RTR ?
174 obj_flags & CC770_OBJ_FLAG_EFF ?
178 if (obj_flags & CC770_OBJ_FLAG_EFF)
182 if (obj_flags & CC770_OBJ_FLAG_RTR)
183 msgcfg |= MSGCFG_DIR;
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);
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);
195 cc770_write_reg(priv, msgobj[mo].ctrl1,
196 NEWDAT_RES | MSGLST_RES |
197 TXRQST_RES | RMTPND_RES);
199 dev_dbg(ND2D(dev), "Message object %d for "
200 "TX data, RTR, SFF and EFF\n", mo);
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);
212 static void disable_all_objs(const struct cc770_priv *priv)
216 for (o = 0; o < CC770_OBJ_MAX; o++) {
219 if (priv->obj_flags[o] & CC770_OBJ_FLAG_RX) {
221 priv->control_normal_mode & CTRL_EAF)
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);
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);
242 static void set_reset_mode(struct net_device *dev)
244 struct cc770_priv *priv = netdev_priv(dev);
246 /* Enable configuration and puts chip in bus-off, disable interrupts */
247 cc770_write_reg(priv, control, CTRL_CCE | CTRL_INI);
249 priv->can.state = CAN_STATE_STOPPED;
251 /* Clear interrupts */
252 cc770_read_reg(priv, interrupt);
254 /* Clear status register */
255 cc770_write_reg(priv, status, 0);
257 /* Disable all used message objects */
258 disable_all_objs(priv);
261 static void set_normal_mode(struct net_device *dev)
263 struct cc770_priv *priv = netdev_priv(dev);
265 /* Clear interrupts */
266 cc770_read_reg(priv, interrupt);
268 /* Clear status register and pre-set last error code */
269 cc770_write_reg(priv, status, STAT_LEC_MASK);
271 /* Enable all used message objects*/
272 enable_all_objs(dev);
275 * Clear bus-off, interrupts only for errors,
276 * not for status change
278 cc770_write_reg(priv, control, priv->control_normal_mode);
280 priv->can.state = CAN_STATE_ERROR_ACTIVE;
283 static void chipset_init(struct cc770_priv *priv)
287 /* Enable configuration and put chip in bus-off, disable interrupts */
288 cc770_write_reg(priv, control, (CTRL_CCE | CTRL_INI));
290 /* Set CLKOUT divider and slew rates */
291 cc770_write_reg(priv, clkout, priv->clkout);
293 /* Configure CPU interface / CLKOUT enable */
294 cc770_write_reg(priv, cpu_interface, priv->cpu_interface | CPUIF_CEN);
296 /* Set bus configuration */
297 cc770_write_reg(priv, bus_config, priv->bus_config);
299 /* Clear interrupts */
300 cc770_read_reg(priv, interrupt);
302 /* Clear status register */
303 cc770_write_reg(priv, status, 0);
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);
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);
333 static int cc770_probe_chip(struct net_device *dev)
335 struct cc770_priv *priv = netdev_priv(dev);
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);
343 * Check if hardware reset is still inactive or maybe there
344 * is no chip in this address space
346 if (cc770_read_reg(priv, cpu_interface) & CPUIF_RST) {
347 dev_info(ND2D(dev), "probing @0x%p failed (reset)\n",
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",
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;
371 static void cc770_start(struct net_device *dev)
373 struct cc770_priv *priv = netdev_priv(dev);
375 /* leave reset mode */
376 if (priv->can.state != CAN_STATE_STOPPED)
379 /* leave reset mode */
380 set_normal_mode(dev);
383 static int cc770_set_mode(struct net_device *dev, enum can_mode mode)
385 struct cc770_priv *priv = netdev_priv(dev);
387 if (!priv->open_time)
393 if (netif_queue_stopped(dev))
394 netif_wake_queue(dev);
404 static int cc770_set_bittiming(struct net_device *dev)
406 struct cc770_priv *priv = netdev_priv(dev);
407 struct can_bittiming *bt = &priv->can.bittiming;
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)
417 "setting BTR0=0x%02x BTR1=0x%02x\n", btr0, btr1);
419 cc770_write_reg(priv, bit_timing_0, btr0);
420 cc770_write_reg(priv, bit_timing_1, btr1);
425 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,31)
426 static int cc770_start_xmit(struct sk_buff *skb, struct net_device *dev)
428 static netdev_tx_t cc770_start_xmit(struct sk_buff *skb, struct net_device *dev)
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);
435 struct net_device_stats *stats = &dev->stats;
437 struct can_frame *cf = (struct can_frame *)skb->data;
438 unsigned int mo = obj2msgobj(CC770_OBJ_TX);
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;
449 netif_stop_queue(dev);
453 if (cf->can_id & CAN_RTR_FLAG)
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) {
463 cc770_write_reg(priv, msgobj[mo].config,
464 (dlc << 4) + rtr + MSGCFG_XTD);
465 cc770_write_reg(priv, msgobj[mo].id[3],
467 cc770_write_reg(priv, msgobj[mo].id[2],
469 cc770_write_reg(priv, msgobj[mo].id[1],
471 cc770_write_reg(priv, msgobj[mo].id[0],
475 cc770_write_reg(priv, msgobj[mo].config,
477 cc770_write_reg(priv, msgobj[mo].id[0],
479 cc770_write_reg(priv, msgobj[mo].id[1],
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]);
487 cc770_write_reg(priv, msgobj[mo].ctrl1,
488 RMTPND_RES | TXRQST_SET | CPUUPD_RES | NEWDAT_UNC);
490 stats->tx_bytes += dlc;
491 dev->trans_start = jiffies;
493 can_put_echo_skb(skb, dev, 0);
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
500 cc770_write_reg(priv, msgobj[mo].ctrl0,
501 MSGVAL_UNC | TXIE_UNC | RXIE_UNC | INTPND_RES);
506 static void cc770_rx(struct net_device *dev, unsigned int mo, u8 ctrl1)
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);
512 struct net_device_stats *stats = &dev->stats;
514 struct can_frame *cf;
520 skb = dev_alloc_skb(sizeof(struct can_frame));
524 skb->protocol = htons(ETH_P_CAN);
526 cf = (struct can_frame *)skb_put(skb, sizeof(struct can_frame));
527 config = cc770_read_reg(priv, msgobj[mo].config);
529 if (ctrl1 & RMTPND_SET) {
531 * Unfortunately, the chip does not store the real message
532 * identifier of the received remote transmission request
533 * frame. Therefore we set it to 0.
535 cf->can_id = CAN_RTR_FLAG;
536 if (config & MSGCFG_XTD)
537 cf->can_id |= CAN_EFF_FLAG;
540 if (config & MSGCFG_XTD) {
541 id = cc770_read_reg(priv, msgobj[mo].id[3]);
542 id |= cc770_read_reg(priv, msgobj[mo].id[2]) << 8;
543 id |= cc770_read_reg(priv, msgobj[mo].id[1]) << 16;
544 id |= cc770_read_reg(priv, msgobj[mo].id[0]) << 24;
548 id = cc770_read_reg(priv, msgobj[mo].id[1]);
549 id |= cc770_read_reg(priv, msgobj[mo].id[0]) << 8;
553 dlc = (config & 0xf0) >> 4; /* strip length code */
555 dlc = 8; /* limit count to 8 bytes */
559 for (i = 0; i < dlc; i++)
560 cf->data[i] = cc770_read_reg(priv, msgobj[mo].data[i]);
564 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,31)
565 dev->last_rx = jiffies;
568 stats->rx_bytes += cf->can_dlc;
571 static int cc770_err(struct net_device *dev, u8 status)
573 struct cc770_priv *priv = netdev_priv(dev);
574 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
575 struct net_device_stats *stats = can_get_stats(dev);
577 struct net_device_stats *stats = &dev->stats;
579 struct can_frame *cf;
583 dev_dbg(ND2D(dev), "status interrupt (%#x)\n", status);
585 skb = dev_alloc_skb(sizeof(struct can_frame));
589 skb->protocol = htons(ETH_P_CAN);
590 cf = (struct can_frame *)skb_put(skb, sizeof(struct can_frame));
591 memset(cf, 0, sizeof(struct can_frame));
592 cf->can_id = CAN_ERR_FLAG;
593 cf->can_dlc = CAN_ERR_DLC;
595 if (status & STAT_BOFF) {
596 /* Disable interrupts */
597 cc770_write_reg(priv, control, CTRL_INI);
598 cf->can_id |= CAN_ERR_BUSOFF;
599 priv->can.state = CAN_STATE_BUS_OFF;
601 } else if (status & STAT_WARN) {
602 cf->can_id |= CAN_ERR_CRTL;
603 cf->data[1] = CAN_ERR_CRTL_RX_WARNING | CAN_ERR_CRTL_TX_WARNING;
604 priv->can.state = CAN_STATE_ERROR_WARNING;
605 priv->can.can_stats.error_warning++;
608 lec = status & STAT_LEC_MASK;
609 if (lec < 7 && lec > 0) {
610 if (lec == STAT_LEC_ACK) {
611 cf->can_id |= CAN_ERR_ACK;
613 cf->can_id |= CAN_ERR_PROT;
616 cf->data[2] |= CAN_ERR_PROT_STUFF;
619 cf->data[2] |= CAN_ERR_PROT_FORM;
622 cf->data[2] |= CAN_ERR_PROT_BIT1;
625 cf->data[2] |= CAN_ERR_PROT_BIT0;
628 cf->data[3] |= CAN_ERR_PROT_LOC_CRC_SEQ;
636 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,31)
637 dev->last_rx = jiffies;
640 stats->rx_bytes += cf->can_dlc;
645 static int cc770_status_interrupt(struct net_device *dev)
647 struct cc770_priv *priv = netdev_priv(dev);
650 status = cc770_read_reg(priv, status);
651 /* Reset the status register including RXOK and TXOK */
652 cc770_write_reg(priv, status, STAT_LEC_MASK);
654 if (status & (STAT_WARN | STAT_BOFF) ||
655 (status & STAT_LEC_MASK) != STAT_LEC_MASK) {
656 cc770_err(dev, status);
657 return status & STAT_BOFF;
663 static void cc770_rx_interrupt(struct net_device *dev, unsigned int o)
665 struct cc770_priv *priv = netdev_priv(dev);
666 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
667 struct net_device_stats *stats = can_get_stats(dev);
669 struct net_device_stats *stats = &dev->stats;
671 unsigned int mo = obj2msgobj(o);
675 ctrl1 = cc770_read_reg(priv, msgobj[mo].ctrl1);
677 if (!(ctrl1 & NEWDAT_SET)) {
678 /* Check for RTR if additional functions are enabled */
679 if (priv->control_normal_mode & CTRL_EAF) {
680 if (!(cc770_read_reg(priv, msgobj[mo].ctrl0) &
688 if (ctrl1 & MSGLST_SET) {
689 stats->rx_over_errors++;
692 if (mo < MSGOBJ_LAST)
693 cc770_write_reg(priv, msgobj[mo].ctrl1,
694 NEWDAT_RES | MSGLST_RES |
695 TXRQST_UNC | RMTPND_UNC);
696 cc770_rx(dev, mo, ctrl1);
698 cc770_write_reg(priv, msgobj[mo].ctrl0,
699 MSGVAL_SET | TXIE_RES |
700 RXIE_SET | INTPND_RES);
701 cc770_write_reg(priv, msgobj[mo].ctrl1,
702 NEWDAT_RES | MSGLST_RES |
703 TXRQST_RES | RMTPND_RES);
707 static void cc770_rtr_interrupt(struct net_device *dev, unsigned int o)
709 struct cc770_priv *priv = netdev_priv(dev);
710 unsigned int mo = obj2msgobj(o);
714 ctrl0 = cc770_read_reg(priv, msgobj[mo].ctrl0);
715 if (!(ctrl0 & INTPND_SET))
718 ctrl1 = cc770_read_reg(priv, msgobj[mo].ctrl1);
719 cc770_rx(dev, mo, ctrl1);
721 cc770_write_reg(priv, msgobj[mo].ctrl0,
722 MSGVAL_SET | TXIE_RES |
723 RXIE_SET | INTPND_RES);
724 cc770_write_reg(priv, msgobj[mo].ctrl1,
725 NEWDAT_RES | CPUUPD_SET |
726 TXRQST_RES | RMTPND_RES);
730 static void cc770_tx_interrupt(struct net_device *dev, unsigned int o)
732 struct cc770_priv *priv = netdev_priv(dev);
733 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
734 struct net_device_stats *stats = can_get_stats(dev);
736 struct net_device_stats *stats = &dev->stats;
738 unsigned int mo = obj2msgobj(o);
740 /* Nothing more to send, switch off interrupts */
741 cc770_write_reg(priv, msgobj[mo].ctrl0,
742 MSGVAL_RES | TXIE_RES | RXIE_RES | INTPND_RES);
744 * We had some cases of repeated IRQ so make sure the
745 * INT is acknowledged
747 cc770_write_reg(priv, msgobj[mo].ctrl0,
748 MSGVAL_UNC | TXIE_UNC | RXIE_UNC | INTPND_RES);
751 can_get_echo_skb(dev, 0);
752 netif_wake_queue(dev);
755 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
756 irqreturn_t cc770_interrupt(int irq, void *dev_id, struct pt_regs *regs)
758 irqreturn_t cc770_interrupt(int irq, void *dev_id)
761 struct net_device *dev = (struct net_device *)dev_id;
762 struct cc770_priv *priv = netdev_priv(dev);
766 /* Shared interrupts and IRQ off? */
767 if (priv->can.state == CAN_STATE_STOPPED)
773 while (n < CC770_MAX_IRQ) {
774 /* Read the highest pending interrupt request */
775 intid = cc770_read_reg(priv, interrupt);
781 /* Exit in case of bus-off */
782 if (cc770_status_interrupt(dev))
785 o = intid2obj(intid);
787 if (o >= CC770_OBJ_MAX) {
789 "Unexpected interrupt id %d\n", intid);
793 if (priv->obj_flags[o] & CC770_OBJ_FLAG_RTR)
794 cc770_rtr_interrupt(dev, o);
795 else if (priv->obj_flags[o] & CC770_OBJ_FLAG_RX)
796 cc770_rx_interrupt(dev, o);
798 cc770_tx_interrupt(dev, o);
803 priv->post_irq(priv);
805 if (n >= CC770_MAX_IRQ)
806 dev_dbg(ND2D(dev), "%d messages handled in ISR", n);
808 return (n) ? IRQ_HANDLED : IRQ_NONE;
811 static int cc770_open(struct net_device *dev)
813 struct cc770_priv *priv = netdev_priv(dev);
816 /* set chip into reset mode */
820 err = open_candev(dev);
824 err = request_irq(dev->irq, &cc770_interrupt, priv->irq_flags,
825 dev->name, (void *)dev);
831 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
832 /* clear statistics */
833 memset(&priv->can.net_stats, 0, sizeof(priv->can.net_stats));
836 /* init and start chip */
838 priv->open_time = jiffies;
840 netif_start_queue(dev);
845 static int cc770_close(struct net_device *dev)
847 struct cc770_priv *priv = netdev_priv(dev);
849 netif_stop_queue(dev);
852 free_irq(dev->irq, (void *)dev);
860 struct net_device *alloc_cc770dev(int sizeof_priv)
862 struct net_device *dev;
863 struct cc770_priv *priv;
865 dev = alloc_candev(sizeof(struct cc770_priv) + sizeof_priv);
869 priv = netdev_priv(dev);
872 priv->can.bittiming_const = &cc770_bittiming_const;
873 priv->can.do_set_bittiming = cc770_set_bittiming;
874 priv->can.do_set_mode = cc770_set_mode;
876 memcpy(priv->obj_flags, cc770_obj_flags, sizeof(cc770_obj_flags));
879 priv->priv = (void *)priv + sizeof(struct cc770_priv);
883 EXPORT_SYMBOL_GPL(alloc_cc770dev);
885 void free_cc770dev(struct net_device *dev)
889 EXPORT_SYMBOL_GPL(free_cc770dev);
891 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,28)
892 static const struct net_device_ops cc770_netdev_ops = {
893 .ndo_open = cc770_open,
894 .ndo_stop = cc770_close,
895 .ndo_start_xmit = cc770_start_xmit,
899 int register_cc770dev(struct net_device *dev)
901 struct cc770_priv *priv = netdev_priv(dev);
903 if (!cc770_probe_chip(dev))
906 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,28)
907 dev->netdev_ops = &cc770_netdev_ops;
909 dev->open = cc770_open;
910 dev->stop = cc770_close;
911 dev->hard_start_xmit = cc770_start_xmit;
914 dev->flags |= IFF_ECHO; /* we support local echo */
916 /* Should we use additional functions? */
917 if (!i82527_compat && priv->control_normal_mode & CTRL_EAF) {
918 priv->control_normal_mode = CTRL_IE | CTRL_EAF | CTRL_EIE;
919 dev_dbg(ND2D(dev), "i82527 mode with additional functions\n");
921 priv->control_normal_mode = CTRL_IE | CTRL_EIE;
922 dev_dbg(ND2D(dev), "strict i82527 compatibility mode\n");
928 return register_candev(dev);
930 EXPORT_SYMBOL_GPL(register_cc770dev);
932 void unregister_cc770dev(struct net_device *dev)
935 unregister_candev(dev);
937 EXPORT_SYMBOL_GPL(unregister_cc770dev);
939 static __init int cc770_init(void)
942 cc770_obj_flags[CC770_OBJ_RX0] |= CC770_OBJ_FLAG_EFF;
943 cc770_obj_flags[CC770_OBJ_RX1] &= ~CC770_OBJ_FLAG_EFF;
946 printk(KERN_INFO "%s CAN netdevice driver\n", DRV_NAME);
951 module_init(cc770_init);
953 static __exit void cc770_exit(void)
955 printk(KERN_INFO "%s: driver removed\n", DRV_NAME);
957 module_exit(cc770_exit);