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/platform/cc770.h>
41 MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
42 MODULE_LICENSE("GPL v2");
43 MODULE_DESCRIPTION(KBUILD_MODNAME
"CAN netdevice driver");
46 * The CC770 is a CAN controller from Bosch, which is 100% compatible
47 * with the AN82527 from Intel, but with "bugs" being fixed and some
48 * additional functionality, mainly:
50 * 1. RX and TX error counters are readable.
51 * 2. Support of silent (listen-only) mode.
52 * 3. Message object 15 can receive all types of frames, also RTR and EFF.
54 * Details are available from Bosch's "CC770_Product_Info_2007-01.pdf",
55 * which explains in detail the compatibility between the CC770 and the
56 * 82527. This driver use the additional functionality 3. on real CC770
57 * devices. Unfortunately, the CC770 does still not store the message
58 * identifier of received remote transmission request frames and
59 * therefore it's set to 0.
61 * The message objects 1..14 can be used for TX and RX while the message
62 * objects 15 is optimized for RX. It has a shadow register for reliable
63 * data reception under heavy bus load. Therefore it makes sense to use
64 * this message object for the needed use case. The frame type (EFF/SFF)
65 * for the message object 15 can be defined via kernel module parameter
66 * "msgobj15_eff". If not equal 0, it will receive 29-bit EFF frames,
67 * otherwise 11 bit SFF messages.
69 static int msgobj15_eff
;
70 module_param(msgobj15_eff
, int, 0444);
71 MODULE_PARM_DESC(msgobj15_eff
, "Extended 29-bit frames for message object 15 "
72 "(default: 11-bit standard frames)");
74 static int i82527_compat
;
75 module_param(i82527_compat
, int, 0444);
76 MODULE_PARM_DESC(i82527_compat
, "Strict Intel 82527 compatibility mode "
77 "without using additional functions");
80 * This driver uses the last 5 message objects 11..15. The definitions
81 * and structure below allows to configure and assign them to the real
84 static unsigned char cc770_obj_flags
[CC770_OBJ_MAX
] = {
85 [CC770_OBJ_RX0
] = CC770_OBJ_FLAG_RX
,
86 [CC770_OBJ_RX1
] = CC770_OBJ_FLAG_RX
| CC770_OBJ_FLAG_EFF
,
87 [CC770_OBJ_RX_RTR0
] = CC770_OBJ_FLAG_RX
| CC770_OBJ_FLAG_RTR
,
88 [CC770_OBJ_RX_RTR1
] = CC770_OBJ_FLAG_RX
| CC770_OBJ_FLAG_RTR
|
93 static const struct can_bittiming_const cc770_bittiming_const
= {
94 .name
= KBUILD_MODNAME
,
105 static inline int intid2obj(unsigned int intid
)
110 return MSGOBJ_LAST
+ 2 - intid
;
113 static void enable_all_objs(const struct net_device
*dev
)
115 struct cc770_priv
*priv
= netdev_priv(dev
);
117 unsigned char obj_flags
;
120 for (o
= 0; o
< ARRAY_SIZE(priv
->obj_flags
); o
++) {
121 obj_flags
= priv
->obj_flags
[o
];
124 if (obj_flags
& CC770_OBJ_FLAG_RX
) {
126 * We don't need extra objects for RTR and EFF if
127 * the additional CC770 functions are enabled.
129 if (priv
->control_normal_mode
& CTRL_EAF
) {
132 netdev_dbg(dev
, "Message object %d for "
133 "RX data, RTR, SFF and EFF\n", mo
);
136 "Message object %d for RX %s %s\n",
137 mo
, obj_flags
& CC770_OBJ_FLAG_RTR
?
139 obj_flags
& CC770_OBJ_FLAG_EFF
?
143 if (obj_flags
& CC770_OBJ_FLAG_EFF
)
147 if (obj_flags
& CC770_OBJ_FLAG_RTR
)
148 msgcfg
|= MSGCFG_DIR
;
150 cc770_write_reg(priv
, msgobj
[mo
].config
, msgcfg
);
151 cc770_write_reg(priv
, msgobj
[mo
].ctrl0
,
152 MSGVAL_SET
| TXIE_RES
|
153 RXIE_SET
| INTPND_RES
);
155 if (obj_flags
& CC770_OBJ_FLAG_RTR
)
156 cc770_write_reg(priv
, msgobj
[mo
].ctrl1
,
157 NEWDAT_RES
| CPUUPD_SET
|
158 TXRQST_RES
| RMTPND_RES
);
160 cc770_write_reg(priv
, msgobj
[mo
].ctrl1
,
161 NEWDAT_RES
| MSGLST_RES
|
162 TXRQST_RES
| RMTPND_RES
);
164 netdev_dbg(dev
, "Message object %d for "
165 "TX data, RTR, SFF and EFF\n", mo
);
167 cc770_write_reg(priv
, msgobj
[mo
].ctrl1
,
168 RMTPND_RES
| TXRQST_RES
|
169 CPUUPD_RES
| NEWDAT_RES
);
170 cc770_write_reg(priv
, msgobj
[mo
].ctrl0
,
171 MSGVAL_RES
| TXIE_RES
|
172 RXIE_RES
| INTPND_RES
);
177 static void disable_all_objs(const struct cc770_priv
*priv
)
181 for (o
= 0; o
< ARRAY_SIZE(priv
->obj_flags
); o
++) {
184 if (priv
->obj_flags
[o
] & CC770_OBJ_FLAG_RX
) {
185 if (o
> 0 && priv
->control_normal_mode
& CTRL_EAF
)
188 cc770_write_reg(priv
, msgobj
[mo
].ctrl1
,
189 NEWDAT_RES
| MSGLST_RES
|
190 TXRQST_RES
| RMTPND_RES
);
191 cc770_write_reg(priv
, msgobj
[mo
].ctrl0
,
192 MSGVAL_RES
| TXIE_RES
|
193 RXIE_RES
| INTPND_RES
);
195 /* Clear message object for send */
196 cc770_write_reg(priv
, msgobj
[mo
].ctrl1
,
197 RMTPND_RES
| TXRQST_RES
|
198 CPUUPD_RES
| NEWDAT_RES
);
199 cc770_write_reg(priv
, msgobj
[mo
].ctrl0
,
200 MSGVAL_RES
| TXIE_RES
|
201 RXIE_RES
| INTPND_RES
);
206 static void set_reset_mode(struct net_device
*dev
)
208 struct cc770_priv
*priv
= netdev_priv(dev
);
210 /* Enable configuration and puts chip in bus-off, disable interrupts */
211 cc770_write_reg(priv
, control
, CTRL_CCE
| CTRL_INI
);
213 priv
->can
.state
= CAN_STATE_STOPPED
;
215 /* Clear interrupts */
216 cc770_read_reg(priv
, interrupt
);
218 /* Clear status register */
219 cc770_write_reg(priv
, status
, 0);
221 /* Disable all used message objects */
222 disable_all_objs(priv
);
225 static void set_normal_mode(struct net_device
*dev
)
227 struct cc770_priv
*priv
= netdev_priv(dev
);
229 /* Clear interrupts */
230 cc770_read_reg(priv
, interrupt
);
232 /* Clear status register and pre-set last error code */
233 cc770_write_reg(priv
, status
, STAT_LEC_MASK
);
235 /* Enable all used message objects*/
236 enable_all_objs(dev
);
239 * Clear bus-off, interrupts only for errors,
240 * not for status change
242 cc770_write_reg(priv
, control
, priv
->control_normal_mode
);
244 priv
->can
.state
= CAN_STATE_ERROR_ACTIVE
;
247 static void chipset_init(struct cc770_priv
*priv
)
251 /* Enable configuration and put chip in bus-off, disable interrupts */
252 cc770_write_reg(priv
, control
, (CTRL_CCE
| CTRL_INI
));
254 /* Set CLKOUT divider and slew rates */
255 cc770_write_reg(priv
, clkout
, priv
->clkout
);
257 /* Configure CPU interface / CLKOUT enable */
258 cc770_write_reg(priv
, cpu_interface
, priv
->cpu_interface
);
260 /* Set bus configuration */
261 cc770_write_reg(priv
, bus_config
, priv
->bus_config
);
263 /* Clear interrupts */
264 cc770_read_reg(priv
, interrupt
);
266 /* Clear status register */
267 cc770_write_reg(priv
, status
, 0);
269 /* Clear and invalidate message objects */
270 for (mo
= MSGOBJ_FIRST
; mo
<= MSGOBJ_LAST
; mo
++) {
271 cc770_write_reg(priv
, msgobj
[mo
].ctrl0
,
272 INTPND_UNC
| RXIE_RES
|
273 TXIE_RES
| MSGVAL_RES
);
274 cc770_write_reg(priv
, msgobj
[mo
].ctrl0
,
275 INTPND_RES
| RXIE_RES
|
276 TXIE_RES
| MSGVAL_RES
);
277 cc770_write_reg(priv
, msgobj
[mo
].ctrl1
,
278 NEWDAT_RES
| MSGLST_RES
|
279 TXRQST_RES
| RMTPND_RES
);
280 for (data
= 0; data
< 8; data
++)
281 cc770_write_reg(priv
, msgobj
[mo
].data
[data
], 0);
282 for (id
= 0; id
< 4; id
++)
283 cc770_write_reg(priv
, msgobj
[mo
].id
[id
], 0);
284 cc770_write_reg(priv
, msgobj
[mo
].config
, 0);
287 /* Set all global ID masks to "don't care" */
288 cc770_write_reg(priv
, global_mask_std
[0], 0);
289 cc770_write_reg(priv
, global_mask_std
[1], 0);
290 cc770_write_reg(priv
, global_mask_ext
[0], 0);
291 cc770_write_reg(priv
, global_mask_ext
[1], 0);
292 cc770_write_reg(priv
, global_mask_ext
[2], 0);
293 cc770_write_reg(priv
, global_mask_ext
[3], 0);
297 static int cc770_probe_chip(struct net_device
*dev
)
299 struct cc770_priv
*priv
= netdev_priv(dev
);
301 /* Enable configuration, put chip in bus-off, disable ints */
302 cc770_write_reg(priv
, control
, CTRL_CCE
| CTRL_EAF
| CTRL_INI
);
303 /* Configure cpu interface / CLKOUT disable */
304 cc770_write_reg(priv
, cpu_interface
, priv
->cpu_interface
);
307 * Check if hardware reset is still inactive or maybe there
308 * is no chip in this address space
310 if (cc770_read_reg(priv
, cpu_interface
) & CPUIF_RST
) {
311 netdev_info(dev
, "probing @0x%p failed (reset)\n",
316 /* Write and read back test pattern (some arbitrary values) */
317 cc770_write_reg(priv
, msgobj
[1].data
[1], 0x25);
318 cc770_write_reg(priv
, msgobj
[2].data
[3], 0x52);
319 cc770_write_reg(priv
, msgobj
[10].data
[6], 0xc3);
320 if ((cc770_read_reg(priv
, msgobj
[1].data
[1]) != 0x25) ||
321 (cc770_read_reg(priv
, msgobj
[2].data
[3]) != 0x52) ||
322 (cc770_read_reg(priv
, msgobj
[10].data
[6]) != 0xc3)) {
323 netdev_info(dev
, "probing @0x%p failed (pattern)\n",
328 /* Check if this chip is a CC770 supporting additional functions */
329 if (cc770_read_reg(priv
, control
) & CTRL_EAF
)
330 priv
->control_normal_mode
|= CTRL_EAF
;
335 static void cc770_start(struct net_device
*dev
)
337 struct cc770_priv
*priv
= netdev_priv(dev
);
339 /* leave reset mode */
340 if (priv
->can
.state
!= CAN_STATE_STOPPED
)
343 /* leave reset mode */
344 set_normal_mode(dev
);
347 static int cc770_set_mode(struct net_device
*dev
, enum can_mode mode
)
352 netif_wake_queue(dev
);
362 static int cc770_set_bittiming(struct net_device
*dev
)
364 struct cc770_priv
*priv
= netdev_priv(dev
);
365 struct can_bittiming
*bt
= &priv
->can
.bittiming
;
368 btr0
= ((bt
->brp
- 1) & 0x3f) | (((bt
->sjw
- 1) & 0x3) << 6);
369 btr1
= ((bt
->prop_seg
+ bt
->phase_seg1
- 1) & 0xf) |
370 (((bt
->phase_seg2
- 1) & 0x7) << 4);
371 if (priv
->can
.ctrlmode
& CAN_CTRLMODE_3_SAMPLES
)
374 netdev_info(dev
, "setting BTR0=0x%02x BTR1=0x%02x\n", btr0
, btr1
);
376 cc770_write_reg(priv
, bit_timing_0
, btr0
);
377 cc770_write_reg(priv
, bit_timing_1
, btr1
);
382 static int cc770_get_berr_counter(const struct net_device
*dev
,
383 struct can_berr_counter
*bec
)
385 struct cc770_priv
*priv
= netdev_priv(dev
);
387 bec
->txerr
= cc770_read_reg(priv
, tx_error_counter
);
388 bec
->rxerr
= cc770_read_reg(priv
, rx_error_counter
);
393 static void cc770_tx(struct net_device
*dev
, int mo
)
395 struct cc770_priv
*priv
= netdev_priv(dev
);
396 struct can_frame
*cf
= (struct can_frame
*)priv
->tx_skb
->data
;
403 rtr
= cf
->can_id
& CAN_RTR_FLAG
? 0 : MSGCFG_DIR
;
405 cc770_write_reg(priv
, msgobj
[mo
].ctrl0
,
406 MSGVAL_RES
| TXIE_RES
| RXIE_RES
| INTPND_RES
);
407 cc770_write_reg(priv
, msgobj
[mo
].ctrl1
,
408 RMTPND_RES
| TXRQST_RES
| CPUUPD_SET
| NEWDAT_RES
);
410 if (id
& CAN_EFF_FLAG
) {
412 cc770_write_reg(priv
, msgobj
[mo
].config
,
413 (dlc
<< 4) | rtr
| MSGCFG_XTD
);
414 cc770_write_reg(priv
, msgobj
[mo
].id
[3], id
<< 3);
415 cc770_write_reg(priv
, msgobj
[mo
].id
[2], id
>> 5);
416 cc770_write_reg(priv
, msgobj
[mo
].id
[1], id
>> 13);
417 cc770_write_reg(priv
, msgobj
[mo
].id
[0], id
>> 21);
420 cc770_write_reg(priv
, msgobj
[mo
].config
, (dlc
<< 4) | rtr
);
421 cc770_write_reg(priv
, msgobj
[mo
].id
[0], id
>> 3);
422 cc770_write_reg(priv
, msgobj
[mo
].id
[1], id
<< 5);
425 for (i
= 0; i
< dlc
; i
++)
426 cc770_write_reg(priv
, msgobj
[mo
].data
[i
], cf
->data
[i
]);
428 cc770_write_reg(priv
, msgobj
[mo
].ctrl1
,
429 RMTPND_UNC
| TXRQST_SET
| CPUUPD_RES
| NEWDAT_UNC
);
430 cc770_write_reg(priv
, msgobj
[mo
].ctrl0
,
431 MSGVAL_SET
| TXIE_SET
| RXIE_SET
| INTPND_UNC
);
434 static netdev_tx_t
cc770_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
436 struct cc770_priv
*priv
= netdev_priv(dev
);
437 unsigned int mo
= obj2msgobj(CC770_OBJ_TX
);
439 if (can_dropped_invalid_skb(dev
, skb
))
442 netif_stop_queue(dev
);
444 if ((cc770_read_reg(priv
,
445 msgobj
[mo
].ctrl1
) & TXRQST_UNC
) == TXRQST_SET
) {
446 netdev_err(dev
, "TX register is still occupied!\n");
447 return NETDEV_TX_BUSY
;
456 static void cc770_rx(struct net_device
*dev
, unsigned int mo
, u8 ctrl1
)
458 struct cc770_priv
*priv
= netdev_priv(dev
);
459 struct net_device_stats
*stats
= &dev
->stats
;
460 struct can_frame
*cf
;
466 skb
= alloc_can_skb(dev
, &cf
);
470 config
= cc770_read_reg(priv
, msgobj
[mo
].config
);
472 if (ctrl1
& RMTPND_SET
) {
474 * Unfortunately, the chip does not store the real message
475 * identifier of the received remote transmission request
476 * frame. Therefore we set it to 0.
478 cf
->can_id
= CAN_RTR_FLAG
;
479 if (config
& MSGCFG_XTD
)
480 cf
->can_id
|= CAN_EFF_FLAG
;
483 if (config
& MSGCFG_XTD
) {
484 id
= cc770_read_reg(priv
, msgobj
[mo
].id
[3]);
485 id
|= cc770_read_reg(priv
, msgobj
[mo
].id
[2]) << 8;
486 id
|= cc770_read_reg(priv
, msgobj
[mo
].id
[1]) << 16;
487 id
|= cc770_read_reg(priv
, msgobj
[mo
].id
[0]) << 24;
491 id
= cc770_read_reg(priv
, msgobj
[mo
].id
[1]);
492 id
|= cc770_read_reg(priv
, msgobj
[mo
].id
[0]) << 8;
497 cf
->can_dlc
= get_can_dlc((config
& 0xf0) >> 4);
498 for (i
= 0; i
< cf
->can_dlc
; i
++)
499 cf
->data
[i
] = cc770_read_reg(priv
, msgobj
[mo
].data
[i
]);
503 stats
->rx_bytes
+= cf
->can_dlc
;
507 static int cc770_err(struct net_device
*dev
, u8 status
)
509 struct cc770_priv
*priv
= netdev_priv(dev
);
510 struct net_device_stats
*stats
= &dev
->stats
;
511 struct can_frame
*cf
;
515 netdev_dbg(dev
, "status interrupt (%#x)\n", status
);
517 skb
= alloc_can_err_skb(dev
, &cf
);
521 /* Use extended functions of the CC770 */
522 if (priv
->control_normal_mode
& CTRL_EAF
) {
523 cf
->data
[6] = cc770_read_reg(priv
, tx_error_counter
);
524 cf
->data
[7] = cc770_read_reg(priv
, rx_error_counter
);
527 if (status
& STAT_BOFF
) {
528 /* Disable interrupts */
529 cc770_write_reg(priv
, control
, CTRL_INI
);
530 cf
->can_id
|= CAN_ERR_BUSOFF
;
531 priv
->can
.state
= CAN_STATE_BUS_OFF
;
532 priv
->can
.can_stats
.bus_off
++;
534 } else if (status
& STAT_WARN
) {
535 cf
->can_id
|= CAN_ERR_CRTL
;
536 /* Only the CC770 does show error passive */
537 if (cf
->data
[7] > 127) {
538 cf
->data
[1] = CAN_ERR_CRTL_RX_PASSIVE
|
539 CAN_ERR_CRTL_TX_PASSIVE
;
540 priv
->can
.state
= CAN_STATE_ERROR_PASSIVE
;
541 priv
->can
.can_stats
.error_passive
++;
543 cf
->data
[1] = CAN_ERR_CRTL_RX_WARNING
|
544 CAN_ERR_CRTL_TX_WARNING
;
545 priv
->can
.state
= CAN_STATE_ERROR_WARNING
;
546 priv
->can
.can_stats
.error_warning
++;
549 /* Back to error avtive */
550 cf
->can_id
|= CAN_ERR_PROT
;
551 cf
->data
[2] = CAN_ERR_PROT_ACTIVE
;
552 priv
->can
.state
= CAN_STATE_ERROR_ACTIVE
;
555 lec
= status
& STAT_LEC_MASK
;
556 if (lec
< 7 && lec
> 0) {
557 if (lec
== STAT_LEC_ACK
) {
558 cf
->can_id
|= CAN_ERR_ACK
;
560 cf
->can_id
|= CAN_ERR_PROT
;
563 cf
->data
[2] |= CAN_ERR_PROT_STUFF
;
566 cf
->data
[2] |= CAN_ERR_PROT_FORM
;
569 cf
->data
[2] |= CAN_ERR_PROT_BIT1
;
572 cf
->data
[2] |= CAN_ERR_PROT_BIT0
;
575 cf
->data
[3] = CAN_ERR_PROT_LOC_CRC_SEQ
;
583 stats
->rx_bytes
+= cf
->can_dlc
;
589 static int cc770_status_interrupt(struct net_device
*dev
)
591 struct cc770_priv
*priv
= netdev_priv(dev
);
594 status
= cc770_read_reg(priv
, status
);
595 /* Reset the status register including RXOK and TXOK */
596 cc770_write_reg(priv
, status
, STAT_LEC_MASK
);
598 if (status
& (STAT_WARN
| STAT_BOFF
) ||
599 (status
& STAT_LEC_MASK
) != STAT_LEC_MASK
) {
600 cc770_err(dev
, status
);
601 return status
& STAT_BOFF
;
607 static void cc770_rx_interrupt(struct net_device
*dev
, unsigned int o
)
609 struct cc770_priv
*priv
= netdev_priv(dev
);
610 struct net_device_stats
*stats
= &dev
->stats
;
611 unsigned int mo
= obj2msgobj(o
);
613 int n
= CC770_MAX_MSG
;
616 ctrl1
= cc770_read_reg(priv
, msgobj
[mo
].ctrl1
);
618 if (!(ctrl1
& NEWDAT_SET
)) {
619 /* Check for RTR if additional functions are enabled */
620 if (priv
->control_normal_mode
& CTRL_EAF
) {
621 if (!(cc770_read_reg(priv
, msgobj
[mo
].ctrl0
) &
629 if (ctrl1
& MSGLST_SET
) {
630 stats
->rx_over_errors
++;
633 if (mo
< MSGOBJ_LAST
)
634 cc770_write_reg(priv
, msgobj
[mo
].ctrl1
,
635 NEWDAT_RES
| MSGLST_RES
|
636 TXRQST_UNC
| RMTPND_UNC
);
637 cc770_rx(dev
, mo
, ctrl1
);
639 cc770_write_reg(priv
, msgobj
[mo
].ctrl0
,
640 MSGVAL_SET
| TXIE_RES
|
641 RXIE_SET
| INTPND_RES
);
642 cc770_write_reg(priv
, msgobj
[mo
].ctrl1
,
643 NEWDAT_RES
| MSGLST_RES
|
644 TXRQST_RES
| RMTPND_RES
);
648 static void cc770_rtr_interrupt(struct net_device
*dev
, unsigned int o
)
650 struct cc770_priv
*priv
= netdev_priv(dev
);
651 unsigned int mo
= obj2msgobj(o
);
653 int n
= CC770_MAX_MSG
;
656 ctrl0
= cc770_read_reg(priv
, msgobj
[mo
].ctrl0
);
657 if (!(ctrl0
& INTPND_SET
))
660 ctrl1
= cc770_read_reg(priv
, msgobj
[mo
].ctrl1
);
661 cc770_rx(dev
, mo
, ctrl1
);
663 cc770_write_reg(priv
, msgobj
[mo
].ctrl0
,
664 MSGVAL_SET
| TXIE_RES
|
665 RXIE_SET
| INTPND_RES
);
666 cc770_write_reg(priv
, msgobj
[mo
].ctrl1
,
667 NEWDAT_RES
| CPUUPD_SET
|
668 TXRQST_RES
| RMTPND_RES
);
672 static void cc770_tx_interrupt(struct net_device
*dev
, unsigned int o
)
674 struct cc770_priv
*priv
= netdev_priv(dev
);
675 struct net_device_stats
*stats
= &dev
->stats
;
676 unsigned int mo
= obj2msgobj(o
);
677 struct can_frame
*cf
;
680 ctrl1
= cc770_read_reg(priv
, msgobj
[mo
].ctrl1
);
682 cc770_write_reg(priv
, msgobj
[mo
].ctrl0
,
683 MSGVAL_RES
| TXIE_RES
| RXIE_RES
| INTPND_RES
);
684 cc770_write_reg(priv
, msgobj
[mo
].ctrl1
,
685 RMTPND_RES
| TXRQST_RES
| MSGLST_RES
| NEWDAT_RES
);
687 if (unlikely(!priv
->tx_skb
)) {
688 netdev_err(dev
, "missing tx skb in tx interrupt\n");
692 if (unlikely(ctrl1
& MSGLST_SET
)) {
693 stats
->rx_over_errors
++;
697 /* When the CC770 is sending an RTR message and it receives a regular
698 * message that matches the id of the RTR message, it will overwrite the
699 * outgoing message in the TX register. When this happens we must
700 * process the received message and try to transmit the outgoing skb
703 if (unlikely(ctrl1
& NEWDAT_SET
)) {
704 cc770_rx(dev
, mo
, ctrl1
);
709 cf
= (struct can_frame
*)priv
->tx_skb
->data
;
710 stats
->tx_bytes
+= cf
->can_dlc
;
713 can_put_echo_skb(priv
->tx_skb
, dev
, 0);
714 can_get_echo_skb(dev
, 0);
717 netif_wake_queue(dev
);
720 static irqreturn_t
cc770_interrupt(int irq
, void *dev_id
)
722 struct net_device
*dev
= (struct net_device
*)dev_id
;
723 struct cc770_priv
*priv
= netdev_priv(dev
);
727 /* Shared interrupts and IRQ off? */
728 if (priv
->can
.state
== CAN_STATE_STOPPED
)
734 while (n
< CC770_MAX_IRQ
) {
735 /* Read the highest pending interrupt request */
736 intid
= cc770_read_reg(priv
, interrupt
);
742 /* Exit in case of bus-off */
743 if (cc770_status_interrupt(dev
))
746 o
= intid2obj(intid
);
748 if (o
>= CC770_OBJ_MAX
) {
749 netdev_err(dev
, "Unexpected interrupt id %d\n",
754 if (priv
->obj_flags
[o
] & CC770_OBJ_FLAG_RTR
)
755 cc770_rtr_interrupt(dev
, o
);
756 else if (priv
->obj_flags
[o
] & CC770_OBJ_FLAG_RX
)
757 cc770_rx_interrupt(dev
, o
);
759 cc770_tx_interrupt(dev
, o
);
764 priv
->post_irq(priv
);
766 if (n
>= CC770_MAX_IRQ
)
767 netdev_dbg(dev
, "%d messages handled in ISR", n
);
769 return (n
) ? IRQ_HANDLED
: IRQ_NONE
;
772 static int cc770_open(struct net_device
*dev
)
774 struct cc770_priv
*priv
= netdev_priv(dev
);
777 /* set chip into reset mode */
781 err
= open_candev(dev
);
785 err
= request_irq(dev
->irq
, &cc770_interrupt
, priv
->irq_flags
,
792 /* init and start chip */
795 netif_start_queue(dev
);
800 static int cc770_close(struct net_device
*dev
)
802 netif_stop_queue(dev
);
805 free_irq(dev
->irq
, dev
);
811 struct net_device
*alloc_cc770dev(int sizeof_priv
)
813 struct net_device
*dev
;
814 struct cc770_priv
*priv
;
816 dev
= alloc_candev(sizeof(struct cc770_priv
) + sizeof_priv
,
821 priv
= netdev_priv(dev
);
824 priv
->can
.bittiming_const
= &cc770_bittiming_const
;
825 priv
->can
.do_set_bittiming
= cc770_set_bittiming
;
826 priv
->can
.do_set_mode
= cc770_set_mode
;
827 priv
->can
.ctrlmode_supported
= CAN_CTRLMODE_3_SAMPLES
;
830 memcpy(priv
->obj_flags
, cc770_obj_flags
, sizeof(cc770_obj_flags
));
833 priv
->priv
= (void *)priv
+ sizeof(struct cc770_priv
);
837 EXPORT_SYMBOL_GPL(alloc_cc770dev
);
839 void free_cc770dev(struct net_device
*dev
)
843 EXPORT_SYMBOL_GPL(free_cc770dev
);
845 static const struct net_device_ops cc770_netdev_ops
= {
846 .ndo_open
= cc770_open
,
847 .ndo_stop
= cc770_close
,
848 .ndo_start_xmit
= cc770_start_xmit
,
849 .ndo_change_mtu
= can_change_mtu
,
852 int register_cc770dev(struct net_device
*dev
)
854 struct cc770_priv
*priv
= netdev_priv(dev
);
857 err
= cc770_probe_chip(dev
);
861 dev
->netdev_ops
= &cc770_netdev_ops
;
863 dev
->flags
|= IFF_ECHO
; /* we support local echo */
865 /* Should we use additional functions? */
866 if (!i82527_compat
&& priv
->control_normal_mode
& CTRL_EAF
) {
867 priv
->can
.do_get_berr_counter
= cc770_get_berr_counter
;
868 priv
->control_normal_mode
= CTRL_IE
| CTRL_EAF
| CTRL_EIE
;
869 netdev_dbg(dev
, "i82527 mode with additional functions\n");
871 priv
->control_normal_mode
= CTRL_IE
| CTRL_EIE
;
872 netdev_dbg(dev
, "strict i82527 compatibility mode\n");
878 return register_candev(dev
);
880 EXPORT_SYMBOL_GPL(register_cc770dev
);
882 void unregister_cc770dev(struct net_device
*dev
)
885 unregister_candev(dev
);
887 EXPORT_SYMBOL_GPL(unregister_cc770dev
);
889 static __init
int cc770_init(void)
892 cc770_obj_flags
[CC770_OBJ_RX0
] |= CC770_OBJ_FLAG_EFF
;
893 cc770_obj_flags
[CC770_OBJ_RX1
] &= ~CC770_OBJ_FLAG_EFF
;
896 pr_info("CAN netdevice driver\n");
900 module_init(cc770_init
);
902 static __exit
void cc770_exit(void)
904 pr_info("driver removed\n");
906 module_exit(cc770_exit
);