1 // SPDX-License-Identifier: GPL-2.0-only
3 * Core driver for the CC770 and AN82527 CAN controllers
5 * Copyright (C) 2009, 2011 Wolfgang Grandegger <wg@grandegger.com>
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/kernel.h>
13 #include <linux/sched.h>
14 #include <linux/types.h>
15 #include <linux/fcntl.h>
16 #include <linux/interrupt.h>
17 #include <linux/ptrace.h>
18 #include <linux/string.h>
19 #include <linux/errno.h>
20 #include <linux/ethtool.h>
21 #include <linux/netdevice.h>
22 #include <linux/if_arp.h>
23 #include <linux/if_ether.h>
24 #include <linux/skbuff.h>
25 #include <linux/delay.h>
27 #include <linux/can.h>
28 #include <linux/can/dev.h>
29 #include <linux/can/error.h>
30 #include <linux/can/platform/cc770.h>
34 MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
35 MODULE_LICENSE("GPL v2");
36 MODULE_DESCRIPTION(KBUILD_MODNAME
"CAN netdevice driver");
39 * The CC770 is a CAN controller from Bosch, which is 100% compatible
40 * with the AN82527 from Intel, but with "bugs" being fixed and some
41 * additional functionality, mainly:
43 * 1. RX and TX error counters are readable.
44 * 2. Support of silent (listen-only) mode.
45 * 3. Message object 15 can receive all types of frames, also RTR and EFF.
47 * Details are available from Bosch's "CC770_Product_Info_2007-01.pdf",
48 * which explains in detail the compatibility between the CC770 and the
49 * 82527. This driver use the additional functionality 3. on real CC770
50 * devices. Unfortunately, the CC770 does still not store the message
51 * identifier of received remote transmission request frames and
52 * therefore it's set to 0.
54 * The message objects 1..14 can be used for TX and RX while the message
55 * objects 15 is optimized for RX. It has a shadow register for reliable
56 * data reception under heavy bus load. Therefore it makes sense to use
57 * this message object for the needed use case. The frame type (EFF/SFF)
58 * for the message object 15 can be defined via kernel module parameter
59 * "msgobj15_eff". If not equal 0, it will receive 29-bit EFF frames,
60 * otherwise 11 bit SFF messages.
62 static int msgobj15_eff
;
63 module_param(msgobj15_eff
, int, 0444);
64 MODULE_PARM_DESC(msgobj15_eff
, "Extended 29-bit frames for message object 15 "
65 "(default: 11-bit standard frames)");
67 static int i82527_compat
;
68 module_param(i82527_compat
, int, 0444);
69 MODULE_PARM_DESC(i82527_compat
, "Strict Intel 82527 compatibility mode "
70 "without using additional functions");
73 * This driver uses the last 5 message objects 11..15. The definitions
74 * and structure below allows to configure and assign them to the real
77 static unsigned char cc770_obj_flags
[CC770_OBJ_MAX
] = {
78 [CC770_OBJ_RX0
] = CC770_OBJ_FLAG_RX
,
79 [CC770_OBJ_RX1
] = CC770_OBJ_FLAG_RX
| CC770_OBJ_FLAG_EFF
,
80 [CC770_OBJ_RX_RTR0
] = CC770_OBJ_FLAG_RX
| CC770_OBJ_FLAG_RTR
,
81 [CC770_OBJ_RX_RTR1
] = CC770_OBJ_FLAG_RX
| CC770_OBJ_FLAG_RTR
|
86 static const struct can_bittiming_const cc770_bittiming_const
= {
87 .name
= KBUILD_MODNAME
,
98 static inline int intid2obj(unsigned int intid
)
103 return MSGOBJ_LAST
+ 2 - intid
;
106 static void enable_all_objs(const struct net_device
*dev
)
108 struct cc770_priv
*priv
= netdev_priv(dev
);
110 unsigned char obj_flags
;
113 for (o
= 0; o
< ARRAY_SIZE(priv
->obj_flags
); o
++) {
114 obj_flags
= priv
->obj_flags
[o
];
117 if (obj_flags
& CC770_OBJ_FLAG_RX
) {
119 * We don't need extra objects for RTR and EFF if
120 * the additional CC770 functions are enabled.
122 if (priv
->control_normal_mode
& CTRL_EAF
) {
125 netdev_dbg(dev
, "Message object %d for "
126 "RX data, RTR, SFF and EFF\n", mo
);
129 "Message object %d for RX %s %s\n",
130 mo
, obj_flags
& CC770_OBJ_FLAG_RTR
?
132 obj_flags
& CC770_OBJ_FLAG_EFF
?
136 if (obj_flags
& CC770_OBJ_FLAG_EFF
)
140 if (obj_flags
& CC770_OBJ_FLAG_RTR
)
141 msgcfg
|= MSGCFG_DIR
;
143 cc770_write_reg(priv
, msgobj
[mo
].config
, msgcfg
);
144 cc770_write_reg(priv
, msgobj
[mo
].ctrl0
,
145 MSGVAL_SET
| TXIE_RES
|
146 RXIE_SET
| INTPND_RES
);
148 if (obj_flags
& CC770_OBJ_FLAG_RTR
)
149 cc770_write_reg(priv
, msgobj
[mo
].ctrl1
,
150 NEWDAT_RES
| CPUUPD_SET
|
151 TXRQST_RES
| RMTPND_RES
);
153 cc770_write_reg(priv
, msgobj
[mo
].ctrl1
,
154 NEWDAT_RES
| MSGLST_RES
|
155 TXRQST_RES
| RMTPND_RES
);
157 netdev_dbg(dev
, "Message object %d for "
158 "TX data, RTR, SFF and EFF\n", mo
);
160 cc770_write_reg(priv
, msgobj
[mo
].ctrl1
,
161 RMTPND_RES
| TXRQST_RES
|
162 CPUUPD_RES
| NEWDAT_RES
);
163 cc770_write_reg(priv
, msgobj
[mo
].ctrl0
,
164 MSGVAL_RES
| TXIE_RES
|
165 RXIE_RES
| INTPND_RES
);
170 static void disable_all_objs(const struct cc770_priv
*priv
)
174 for (o
= 0; o
< ARRAY_SIZE(priv
->obj_flags
); o
++) {
177 if (priv
->obj_flags
[o
] & CC770_OBJ_FLAG_RX
) {
178 if (o
> 0 && priv
->control_normal_mode
& CTRL_EAF
)
181 cc770_write_reg(priv
, msgobj
[mo
].ctrl1
,
182 NEWDAT_RES
| MSGLST_RES
|
183 TXRQST_RES
| RMTPND_RES
);
184 cc770_write_reg(priv
, msgobj
[mo
].ctrl0
,
185 MSGVAL_RES
| TXIE_RES
|
186 RXIE_RES
| INTPND_RES
);
188 /* Clear message object for send */
189 cc770_write_reg(priv
, msgobj
[mo
].ctrl1
,
190 RMTPND_RES
| TXRQST_RES
|
191 CPUUPD_RES
| NEWDAT_RES
);
192 cc770_write_reg(priv
, msgobj
[mo
].ctrl0
,
193 MSGVAL_RES
| TXIE_RES
|
194 RXIE_RES
| INTPND_RES
);
199 static void set_reset_mode(struct net_device
*dev
)
201 struct cc770_priv
*priv
= netdev_priv(dev
);
203 /* Enable configuration and puts chip in bus-off, disable interrupts */
204 cc770_write_reg(priv
, control
, CTRL_CCE
| CTRL_INI
);
206 priv
->can
.state
= CAN_STATE_STOPPED
;
208 /* Clear interrupts */
209 cc770_read_reg(priv
, interrupt
);
211 /* Clear status register */
212 cc770_write_reg(priv
, status
, 0);
214 /* Disable all used message objects */
215 disable_all_objs(priv
);
218 static void set_normal_mode(struct net_device
*dev
)
220 struct cc770_priv
*priv
= netdev_priv(dev
);
222 /* Clear interrupts */
223 cc770_read_reg(priv
, interrupt
);
225 /* Clear status register and pre-set last error code */
226 cc770_write_reg(priv
, status
, STAT_LEC_MASK
);
228 /* Enable all used message objects*/
229 enable_all_objs(dev
);
232 * Clear bus-off, interrupts only for errors,
233 * not for status change
235 cc770_write_reg(priv
, control
, priv
->control_normal_mode
);
237 priv
->can
.state
= CAN_STATE_ERROR_ACTIVE
;
240 static void chipset_init(struct cc770_priv
*priv
)
244 /* Enable configuration and put chip in bus-off, disable interrupts */
245 cc770_write_reg(priv
, control
, (CTRL_CCE
| CTRL_INI
));
247 /* Set CLKOUT divider and slew rates */
248 cc770_write_reg(priv
, clkout
, priv
->clkout
);
250 /* Configure CPU interface / CLKOUT enable */
251 cc770_write_reg(priv
, cpu_interface
, priv
->cpu_interface
);
253 /* Set bus configuration */
254 cc770_write_reg(priv
, bus_config
, priv
->bus_config
);
256 /* Clear interrupts */
257 cc770_read_reg(priv
, interrupt
);
259 /* Clear status register */
260 cc770_write_reg(priv
, status
, 0);
262 /* Clear and invalidate message objects */
263 for (mo
= MSGOBJ_FIRST
; mo
<= MSGOBJ_LAST
; mo
++) {
264 cc770_write_reg(priv
, msgobj
[mo
].ctrl0
,
265 INTPND_UNC
| RXIE_RES
|
266 TXIE_RES
| MSGVAL_RES
);
267 cc770_write_reg(priv
, msgobj
[mo
].ctrl0
,
268 INTPND_RES
| RXIE_RES
|
269 TXIE_RES
| MSGVAL_RES
);
270 cc770_write_reg(priv
, msgobj
[mo
].ctrl1
,
271 NEWDAT_RES
| MSGLST_RES
|
272 TXRQST_RES
| RMTPND_RES
);
273 for (data
= 0; data
< 8; data
++)
274 cc770_write_reg(priv
, msgobj
[mo
].data
[data
], 0);
275 for (id
= 0; id
< 4; id
++)
276 cc770_write_reg(priv
, msgobj
[mo
].id
[id
], 0);
277 cc770_write_reg(priv
, msgobj
[mo
].config
, 0);
280 /* Set all global ID masks to "don't care" */
281 cc770_write_reg(priv
, global_mask_std
[0], 0);
282 cc770_write_reg(priv
, global_mask_std
[1], 0);
283 cc770_write_reg(priv
, global_mask_ext
[0], 0);
284 cc770_write_reg(priv
, global_mask_ext
[1], 0);
285 cc770_write_reg(priv
, global_mask_ext
[2], 0);
286 cc770_write_reg(priv
, global_mask_ext
[3], 0);
290 static int cc770_probe_chip(struct net_device
*dev
)
292 struct cc770_priv
*priv
= netdev_priv(dev
);
294 /* Enable configuration, put chip in bus-off, disable ints */
295 cc770_write_reg(priv
, control
, CTRL_CCE
| CTRL_EAF
| CTRL_INI
);
296 /* Configure cpu interface / CLKOUT disable */
297 cc770_write_reg(priv
, cpu_interface
, priv
->cpu_interface
);
300 * Check if hardware reset is still inactive or maybe there
301 * is no chip in this address space
303 if (cc770_read_reg(priv
, cpu_interface
) & CPUIF_RST
) {
304 netdev_info(dev
, "probing @0x%p failed (reset)\n",
309 /* Write and read back test pattern (some arbitrary values) */
310 cc770_write_reg(priv
, msgobj
[1].data
[1], 0x25);
311 cc770_write_reg(priv
, msgobj
[2].data
[3], 0x52);
312 cc770_write_reg(priv
, msgobj
[10].data
[6], 0xc3);
313 if ((cc770_read_reg(priv
, msgobj
[1].data
[1]) != 0x25) ||
314 (cc770_read_reg(priv
, msgobj
[2].data
[3]) != 0x52) ||
315 (cc770_read_reg(priv
, msgobj
[10].data
[6]) != 0xc3)) {
316 netdev_info(dev
, "probing @0x%p failed (pattern)\n",
321 /* Check if this chip is a CC770 supporting additional functions */
322 if (cc770_read_reg(priv
, control
) & CTRL_EAF
)
323 priv
->control_normal_mode
|= CTRL_EAF
;
328 static void cc770_start(struct net_device
*dev
)
330 struct cc770_priv
*priv
= netdev_priv(dev
);
332 /* leave reset mode */
333 if (priv
->can
.state
!= CAN_STATE_STOPPED
)
336 /* leave reset mode */
337 set_normal_mode(dev
);
340 static int cc770_set_mode(struct net_device
*dev
, enum can_mode mode
)
345 netif_wake_queue(dev
);
355 static int cc770_set_bittiming(struct net_device
*dev
)
357 struct cc770_priv
*priv
= netdev_priv(dev
);
358 struct can_bittiming
*bt
= &priv
->can
.bittiming
;
361 btr0
= ((bt
->brp
- 1) & 0x3f) | (((bt
->sjw
- 1) & 0x3) << 6);
362 btr1
= ((bt
->prop_seg
+ bt
->phase_seg1
- 1) & 0xf) |
363 (((bt
->phase_seg2
- 1) & 0x7) << 4);
364 if (priv
->can
.ctrlmode
& CAN_CTRLMODE_3_SAMPLES
)
367 netdev_info(dev
, "setting BTR0=0x%02x BTR1=0x%02x\n", btr0
, btr1
);
369 cc770_write_reg(priv
, bit_timing_0
, btr0
);
370 cc770_write_reg(priv
, bit_timing_1
, btr1
);
375 static int cc770_get_berr_counter(const struct net_device
*dev
,
376 struct can_berr_counter
*bec
)
378 struct cc770_priv
*priv
= netdev_priv(dev
);
380 bec
->txerr
= cc770_read_reg(priv
, tx_error_counter
);
381 bec
->rxerr
= cc770_read_reg(priv
, rx_error_counter
);
386 static void cc770_tx(struct net_device
*dev
, int mo
)
388 struct cc770_priv
*priv
= netdev_priv(dev
);
389 struct can_frame
*cf
= (struct can_frame
*)priv
->tx_skb
->data
;
396 rtr
= cf
->can_id
& CAN_RTR_FLAG
? 0 : MSGCFG_DIR
;
398 cc770_write_reg(priv
, msgobj
[mo
].ctrl0
,
399 MSGVAL_RES
| TXIE_RES
| RXIE_RES
| INTPND_RES
);
400 cc770_write_reg(priv
, msgobj
[mo
].ctrl1
,
401 RMTPND_RES
| TXRQST_RES
| CPUUPD_SET
| NEWDAT_RES
);
403 if (id
& CAN_EFF_FLAG
) {
405 cc770_write_reg(priv
, msgobj
[mo
].config
,
406 (dlc
<< 4) | rtr
| MSGCFG_XTD
);
407 cc770_write_reg(priv
, msgobj
[mo
].id
[3], id
<< 3);
408 cc770_write_reg(priv
, msgobj
[mo
].id
[2], id
>> 5);
409 cc770_write_reg(priv
, msgobj
[mo
].id
[1], id
>> 13);
410 cc770_write_reg(priv
, msgobj
[mo
].id
[0], id
>> 21);
413 cc770_write_reg(priv
, msgobj
[mo
].config
, (dlc
<< 4) | rtr
);
414 cc770_write_reg(priv
, msgobj
[mo
].id
[0], id
>> 3);
415 cc770_write_reg(priv
, msgobj
[mo
].id
[1], id
<< 5);
418 for (i
= 0; i
< dlc
; i
++)
419 cc770_write_reg(priv
, msgobj
[mo
].data
[i
], cf
->data
[i
]);
421 cc770_write_reg(priv
, msgobj
[mo
].ctrl1
,
422 RMTPND_UNC
| TXRQST_SET
| CPUUPD_RES
| NEWDAT_UNC
);
423 cc770_write_reg(priv
, msgobj
[mo
].ctrl0
,
424 MSGVAL_SET
| TXIE_SET
| RXIE_SET
| INTPND_UNC
);
427 static netdev_tx_t
cc770_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
429 struct cc770_priv
*priv
= netdev_priv(dev
);
430 unsigned int mo
= obj2msgobj(CC770_OBJ_TX
);
432 if (can_dev_dropped_skb(dev
, skb
))
435 netif_stop_queue(dev
);
437 if ((cc770_read_reg(priv
,
438 msgobj
[mo
].ctrl1
) & TXRQST_UNC
) == TXRQST_SET
) {
439 netdev_err(dev
, "TX register is still occupied!\n");
440 return NETDEV_TX_BUSY
;
449 static void cc770_rx(struct net_device
*dev
, unsigned int mo
, u8 ctrl1
)
451 struct cc770_priv
*priv
= netdev_priv(dev
);
452 struct net_device_stats
*stats
= &dev
->stats
;
453 struct can_frame
*cf
;
459 skb
= alloc_can_skb(dev
, &cf
);
463 config
= cc770_read_reg(priv
, msgobj
[mo
].config
);
465 if (ctrl1
& RMTPND_SET
) {
467 * Unfortunately, the chip does not store the real message
468 * identifier of the received remote transmission request
469 * frame. Therefore we set it to 0.
471 cf
->can_id
= CAN_RTR_FLAG
;
472 if (config
& MSGCFG_XTD
)
473 cf
->can_id
|= CAN_EFF_FLAG
;
476 if (config
& MSGCFG_XTD
) {
477 id
= cc770_read_reg(priv
, msgobj
[mo
].id
[3]);
478 id
|= cc770_read_reg(priv
, msgobj
[mo
].id
[2]) << 8;
479 id
|= cc770_read_reg(priv
, msgobj
[mo
].id
[1]) << 16;
480 id
|= cc770_read_reg(priv
, msgobj
[mo
].id
[0]) << 24;
484 id
= cc770_read_reg(priv
, msgobj
[mo
].id
[1]);
485 id
|= cc770_read_reg(priv
, msgobj
[mo
].id
[0]) << 8;
490 cf
->len
= can_cc_dlc2len((config
& 0xf0) >> 4);
491 for (i
= 0; i
< cf
->len
; i
++)
492 cf
->data
[i
] = cc770_read_reg(priv
, msgobj
[mo
].data
[i
]);
494 stats
->rx_bytes
+= cf
->len
;
501 static int cc770_err(struct net_device
*dev
, u8 status
)
503 struct cc770_priv
*priv
= netdev_priv(dev
);
504 struct can_frame
*cf
;
508 netdev_dbg(dev
, "status interrupt (%#x)\n", status
);
510 skb
= alloc_can_err_skb(dev
, &cf
);
514 /* Use extended functions of the CC770 */
515 if (priv
->control_normal_mode
& CTRL_EAF
) {
516 cf
->can_id
|= CAN_ERR_CNT
;
517 cf
->data
[6] = cc770_read_reg(priv
, tx_error_counter
);
518 cf
->data
[7] = cc770_read_reg(priv
, rx_error_counter
);
521 if (status
& STAT_BOFF
) {
522 /* Disable interrupts */
523 cc770_write_reg(priv
, control
, CTRL_INI
);
524 cf
->can_id
|= CAN_ERR_BUSOFF
;
525 priv
->can
.state
= CAN_STATE_BUS_OFF
;
526 priv
->can
.can_stats
.bus_off
++;
528 } else if (status
& STAT_WARN
) {
529 cf
->can_id
|= CAN_ERR_CRTL
;
530 /* Only the CC770 does show error passive */
531 if (cf
->data
[7] > 127) {
532 cf
->data
[1] = CAN_ERR_CRTL_RX_PASSIVE
|
533 CAN_ERR_CRTL_TX_PASSIVE
;
534 priv
->can
.state
= CAN_STATE_ERROR_PASSIVE
;
535 priv
->can
.can_stats
.error_passive
++;
537 cf
->data
[1] = CAN_ERR_CRTL_RX_WARNING
|
538 CAN_ERR_CRTL_TX_WARNING
;
539 priv
->can
.state
= CAN_STATE_ERROR_WARNING
;
540 priv
->can
.can_stats
.error_warning
++;
543 /* Back to error active */
544 cf
->can_id
|= CAN_ERR_PROT
;
545 cf
->data
[2] = CAN_ERR_PROT_ACTIVE
;
546 priv
->can
.state
= CAN_STATE_ERROR_ACTIVE
;
549 lec
= status
& STAT_LEC_MASK
;
550 if (lec
< 7 && lec
> 0) {
551 if (lec
== STAT_LEC_ACK
) {
552 cf
->can_id
|= CAN_ERR_ACK
;
554 cf
->can_id
|= CAN_ERR_PROT
;
557 cf
->data
[2] |= CAN_ERR_PROT_STUFF
;
560 cf
->data
[2] |= CAN_ERR_PROT_FORM
;
563 cf
->data
[2] |= CAN_ERR_PROT_BIT1
;
566 cf
->data
[2] |= CAN_ERR_PROT_BIT0
;
569 cf
->data
[3] = CAN_ERR_PROT_LOC_CRC_SEQ
;
581 static int cc770_status_interrupt(struct net_device
*dev
)
583 struct cc770_priv
*priv
= netdev_priv(dev
);
586 status
= cc770_read_reg(priv
, status
);
587 /* Reset the status register including RXOK and TXOK */
588 cc770_write_reg(priv
, status
, STAT_LEC_MASK
);
590 if (status
& (STAT_WARN
| STAT_BOFF
) ||
591 (status
& STAT_LEC_MASK
) != STAT_LEC_MASK
) {
592 cc770_err(dev
, status
);
593 return status
& STAT_BOFF
;
599 static void cc770_rx_interrupt(struct net_device
*dev
, unsigned int o
)
601 struct cc770_priv
*priv
= netdev_priv(dev
);
602 struct net_device_stats
*stats
= &dev
->stats
;
603 unsigned int mo
= obj2msgobj(o
);
605 int n
= CC770_MAX_MSG
;
608 ctrl1
= cc770_read_reg(priv
, msgobj
[mo
].ctrl1
);
610 if (!(ctrl1
& NEWDAT_SET
)) {
611 /* Check for RTR if additional functions are enabled */
612 if (priv
->control_normal_mode
& CTRL_EAF
) {
613 if (!(cc770_read_reg(priv
, msgobj
[mo
].ctrl0
) &
621 if (ctrl1
& MSGLST_SET
) {
622 stats
->rx_over_errors
++;
625 if (mo
< MSGOBJ_LAST
)
626 cc770_write_reg(priv
, msgobj
[mo
].ctrl1
,
627 NEWDAT_RES
| MSGLST_RES
|
628 TXRQST_UNC
| RMTPND_UNC
);
629 cc770_rx(dev
, mo
, ctrl1
);
631 cc770_write_reg(priv
, msgobj
[mo
].ctrl0
,
632 MSGVAL_SET
| TXIE_RES
|
633 RXIE_SET
| INTPND_RES
);
634 cc770_write_reg(priv
, msgobj
[mo
].ctrl1
,
635 NEWDAT_RES
| MSGLST_RES
|
636 TXRQST_RES
| RMTPND_RES
);
640 static void cc770_rtr_interrupt(struct net_device
*dev
, unsigned int o
)
642 struct cc770_priv
*priv
= netdev_priv(dev
);
643 unsigned int mo
= obj2msgobj(o
);
645 int n
= CC770_MAX_MSG
;
648 ctrl0
= cc770_read_reg(priv
, msgobj
[mo
].ctrl0
);
649 if (!(ctrl0
& INTPND_SET
))
652 ctrl1
= cc770_read_reg(priv
, msgobj
[mo
].ctrl1
);
653 cc770_rx(dev
, mo
, ctrl1
);
655 cc770_write_reg(priv
, msgobj
[mo
].ctrl0
,
656 MSGVAL_SET
| TXIE_RES
|
657 RXIE_SET
| INTPND_RES
);
658 cc770_write_reg(priv
, msgobj
[mo
].ctrl1
,
659 NEWDAT_RES
| CPUUPD_SET
|
660 TXRQST_RES
| RMTPND_RES
);
664 static void cc770_tx_interrupt(struct net_device
*dev
, unsigned int o
)
666 struct cc770_priv
*priv
= netdev_priv(dev
);
667 struct net_device_stats
*stats
= &dev
->stats
;
668 unsigned int mo
= obj2msgobj(o
);
671 ctrl1
= cc770_read_reg(priv
, msgobj
[mo
].ctrl1
);
673 cc770_write_reg(priv
, msgobj
[mo
].ctrl0
,
674 MSGVAL_RES
| TXIE_RES
| RXIE_RES
| INTPND_RES
);
675 cc770_write_reg(priv
, msgobj
[mo
].ctrl1
,
676 RMTPND_RES
| TXRQST_RES
| MSGLST_RES
| NEWDAT_RES
);
678 if (unlikely(!priv
->tx_skb
)) {
679 netdev_err(dev
, "missing tx skb in tx interrupt\n");
683 if (unlikely(ctrl1
& MSGLST_SET
)) {
684 stats
->rx_over_errors
++;
688 /* When the CC770 is sending an RTR message and it receives a regular
689 * message that matches the id of the RTR message, it will overwrite the
690 * outgoing message in the TX register. When this happens we must
691 * process the received message and try to transmit the outgoing skb
694 if (unlikely(ctrl1
& NEWDAT_SET
)) {
695 cc770_rx(dev
, mo
, ctrl1
);
700 can_put_echo_skb(priv
->tx_skb
, dev
, 0, 0);
701 stats
->tx_bytes
+= can_get_echo_skb(dev
, 0, NULL
);
705 netif_wake_queue(dev
);
708 static irqreturn_t
cc770_interrupt(int irq
, void *dev_id
)
710 struct net_device
*dev
= (struct net_device
*)dev_id
;
711 struct cc770_priv
*priv
= netdev_priv(dev
);
715 /* Shared interrupts and IRQ off? */
716 if (priv
->can
.state
== CAN_STATE_STOPPED
)
722 while (n
< CC770_MAX_IRQ
) {
723 /* Read the highest pending interrupt request */
724 intid
= cc770_read_reg(priv
, interrupt
);
730 /* Exit in case of bus-off */
731 if (cc770_status_interrupt(dev
))
734 o
= intid2obj(intid
);
736 if (o
>= CC770_OBJ_MAX
) {
737 netdev_err(dev
, "Unexpected interrupt id %d\n",
742 if (priv
->obj_flags
[o
] & CC770_OBJ_FLAG_RTR
)
743 cc770_rtr_interrupt(dev
, o
);
744 else if (priv
->obj_flags
[o
] & CC770_OBJ_FLAG_RX
)
745 cc770_rx_interrupt(dev
, o
);
747 cc770_tx_interrupt(dev
, o
);
752 priv
->post_irq(priv
);
754 if (n
>= CC770_MAX_IRQ
)
755 netdev_dbg(dev
, "%d messages handled in ISR", n
);
757 return (n
) ? IRQ_HANDLED
: IRQ_NONE
;
760 static int cc770_open(struct net_device
*dev
)
762 struct cc770_priv
*priv
= netdev_priv(dev
);
765 /* set chip into reset mode */
769 err
= open_candev(dev
);
773 err
= request_irq(dev
->irq
, &cc770_interrupt
, priv
->irq_flags
,
780 /* init and start chip */
783 netif_start_queue(dev
);
788 static int cc770_close(struct net_device
*dev
)
790 netif_stop_queue(dev
);
793 free_irq(dev
->irq
, dev
);
799 struct net_device
*alloc_cc770dev(int sizeof_priv
)
801 struct net_device
*dev
;
802 struct cc770_priv
*priv
;
804 dev
= alloc_candev(sizeof(struct cc770_priv
) + sizeof_priv
,
809 priv
= netdev_priv(dev
);
812 priv
->can
.bittiming_const
= &cc770_bittiming_const
;
813 priv
->can
.do_set_bittiming
= cc770_set_bittiming
;
814 priv
->can
.do_set_mode
= cc770_set_mode
;
815 priv
->can
.ctrlmode_supported
= CAN_CTRLMODE_3_SAMPLES
;
818 memcpy(priv
->obj_flags
, cc770_obj_flags
, sizeof(cc770_obj_flags
));
821 priv
->priv
= (void *)priv
+ sizeof(struct cc770_priv
);
825 EXPORT_SYMBOL_GPL(alloc_cc770dev
);
827 void free_cc770dev(struct net_device
*dev
)
831 EXPORT_SYMBOL_GPL(free_cc770dev
);
833 static const struct net_device_ops cc770_netdev_ops
= {
834 .ndo_open
= cc770_open
,
835 .ndo_stop
= cc770_close
,
836 .ndo_start_xmit
= cc770_start_xmit
,
837 .ndo_change_mtu
= can_change_mtu
,
840 static const struct ethtool_ops cc770_ethtool_ops
= {
841 .get_ts_info
= ethtool_op_get_ts_info
,
844 int register_cc770dev(struct net_device
*dev
)
846 struct cc770_priv
*priv
= netdev_priv(dev
);
849 err
= cc770_probe_chip(dev
);
853 dev
->netdev_ops
= &cc770_netdev_ops
;
854 dev
->ethtool_ops
= &cc770_ethtool_ops
;
856 dev
->flags
|= IFF_ECHO
; /* we support local echo */
858 /* Should we use additional functions? */
859 if (!i82527_compat
&& priv
->control_normal_mode
& CTRL_EAF
) {
860 priv
->can
.do_get_berr_counter
= cc770_get_berr_counter
;
861 priv
->control_normal_mode
= CTRL_IE
| CTRL_EAF
| CTRL_EIE
;
862 netdev_dbg(dev
, "i82527 mode with additional functions\n");
864 priv
->control_normal_mode
= CTRL_IE
| CTRL_EIE
;
865 netdev_dbg(dev
, "strict i82527 compatibility mode\n");
871 return register_candev(dev
);
873 EXPORT_SYMBOL_GPL(register_cc770dev
);
875 void unregister_cc770dev(struct net_device
*dev
)
878 unregister_candev(dev
);
880 EXPORT_SYMBOL_GPL(unregister_cc770dev
);
882 static __init
int cc770_init(void)
885 cc770_obj_flags
[CC770_OBJ_RX0
] |= CC770_OBJ_FLAG_EFF
;
886 cc770_obj_flags
[CC770_OBJ_RX1
] &= ~CC770_OBJ_FLAG_EFF
;
889 pr_info("CAN netdevice driver\n");
893 module_init(cc770_init
);
895 static __exit
void cc770_exit(void)
897 pr_info("driver removed\n");
899 module_exit(cc770_exit
);