xref: /openbmc/linux/drivers/net/can/cc770/cc770.c (revision 96ac6d43)
1 /*
2  * Core driver for the CC770 and AN82527 CAN controllers
3  *
4  * Copyright (C) 2009, 2011 Wolfgang Grandegger <wg@grandegger.com>
5  *
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
9  *
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.
14  */
15 
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17 
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>
33 
34 #include <linux/can.h>
35 #include <linux/can/dev.h>
36 #include <linux/can/error.h>
37 #include <linux/can/platform/cc770.h>
38 
39 #include "cc770.h"
40 
41 MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
42 MODULE_LICENSE("GPL v2");
43 MODULE_DESCRIPTION(KBUILD_MODNAME "CAN netdevice driver");
44 
45 /*
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:
49  *
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.
53  *
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.
60  *
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.
68  */
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)");
73 
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");
78 
79 /*
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
82  * message object.
83  */
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 |
89 			      CC770_OBJ_FLAG_EFF,
90 	[CC770_OBJ_TX] = 0,
91 };
92 
93 static const struct can_bittiming_const cc770_bittiming_const = {
94 	.name = KBUILD_MODNAME,
95 	.tseg1_min = 1,
96 	.tseg1_max = 16,
97 	.tseg2_min = 1,
98 	.tseg2_max = 8,
99 	.sjw_max = 4,
100 	.brp_min = 1,
101 	.brp_max = 64,
102 	.brp_inc = 1,
103 };
104 
105 static inline int intid2obj(unsigned int intid)
106 {
107 	if (intid == 2)
108 		return 0;
109 	else
110 		return MSGOBJ_LAST + 2 - intid;
111 }
112 
113 static void enable_all_objs(const struct net_device *dev)
114 {
115 	struct cc770_priv *priv = netdev_priv(dev);
116 	u8 msgcfg;
117 	unsigned char obj_flags;
118 	unsigned int o, mo;
119 
120 	for (o = 0; o < ARRAY_SIZE(priv->obj_flags); o++) {
121 		obj_flags = priv->obj_flags[o];
122 		mo = obj2msgobj(o);
123 
124 		if (obj_flags & CC770_OBJ_FLAG_RX) {
125 			/*
126 			 * We don't need extra objects for RTR and EFF if
127 			 * the additional CC770 functions are enabled.
128 			 */
129 			if (priv->control_normal_mode & CTRL_EAF) {
130 				if (o > 0)
131 					continue;
132 				netdev_dbg(dev, "Message object %d for "
133 					   "RX data, RTR, SFF and EFF\n", mo);
134 			} else {
135 				netdev_dbg(dev,
136 					   "Message object %d for RX %s %s\n",
137 					   mo, obj_flags & CC770_OBJ_FLAG_RTR ?
138 					   "RTR" : "data",
139 					   obj_flags & CC770_OBJ_FLAG_EFF ?
140 					   "EFF" : "SFF");
141 			}
142 
143 			if (obj_flags & CC770_OBJ_FLAG_EFF)
144 				msgcfg = MSGCFG_XTD;
145 			else
146 				msgcfg = 0;
147 			if (obj_flags & CC770_OBJ_FLAG_RTR)
148 				msgcfg |= MSGCFG_DIR;
149 
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);
154 
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);
159 			else
160 				cc770_write_reg(priv, msgobj[mo].ctrl1,
161 						NEWDAT_RES | MSGLST_RES |
162 						TXRQST_RES | RMTPND_RES);
163 		} else {
164 			netdev_dbg(dev, "Message object %d for "
165 				   "TX data, RTR, SFF and EFF\n", mo);
166 
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);
173 		}
174 	}
175 }
176 
177 static void disable_all_objs(const struct cc770_priv *priv)
178 {
179 	int o, mo;
180 
181 	for (o = 0; o <  ARRAY_SIZE(priv->obj_flags); o++) {
182 		mo = obj2msgobj(o);
183 
184 		if (priv->obj_flags[o] & CC770_OBJ_FLAG_RX) {
185 			if (o > 0 && priv->control_normal_mode & CTRL_EAF)
186 				continue;
187 
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);
194 		} else {
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);
202 		}
203 	}
204 }
205 
206 static void set_reset_mode(struct net_device *dev)
207 {
208 	struct cc770_priv *priv = netdev_priv(dev);
209 
210 	/* Enable configuration and puts chip in bus-off, disable interrupts */
211 	cc770_write_reg(priv, control, CTRL_CCE | CTRL_INI);
212 
213 	priv->can.state = CAN_STATE_STOPPED;
214 
215 	/* Clear interrupts */
216 	cc770_read_reg(priv, interrupt);
217 
218 	/* Clear status register */
219 	cc770_write_reg(priv, status, 0);
220 
221 	/* Disable all used message objects */
222 	disable_all_objs(priv);
223 }
224 
225 static void set_normal_mode(struct net_device *dev)
226 {
227 	struct cc770_priv *priv = netdev_priv(dev);
228 
229 	/* Clear interrupts */
230 	cc770_read_reg(priv, interrupt);
231 
232 	/* Clear status register and pre-set last error code */
233 	cc770_write_reg(priv, status, STAT_LEC_MASK);
234 
235 	/* Enable all used message objects*/
236 	enable_all_objs(dev);
237 
238 	/*
239 	 * Clear bus-off, interrupts only for errors,
240 	 * not for status change
241 	 */
242 	cc770_write_reg(priv, control, priv->control_normal_mode);
243 
244 	priv->can.state = CAN_STATE_ERROR_ACTIVE;
245 }
246 
247 static void chipset_init(struct cc770_priv *priv)
248 {
249 	int mo, id, data;
250 
251 	/* Enable configuration and put chip in bus-off, disable interrupts */
252 	cc770_write_reg(priv, control, (CTRL_CCE | CTRL_INI));
253 
254 	/* Set CLKOUT divider and slew rates */
255 	cc770_write_reg(priv, clkout, priv->clkout);
256 
257 	/* Configure CPU interface / CLKOUT enable */
258 	cc770_write_reg(priv, cpu_interface, priv->cpu_interface);
259 
260 	/* Set bus configuration  */
261 	cc770_write_reg(priv, bus_config, priv->bus_config);
262 
263 	/* Clear interrupts */
264 	cc770_read_reg(priv, interrupt);
265 
266 	/* Clear status register */
267 	cc770_write_reg(priv, status, 0);
268 
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);
285 	}
286 
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);
294 
295 }
296 
297 static int cc770_probe_chip(struct net_device *dev)
298 {
299 	struct cc770_priv *priv = netdev_priv(dev);
300 
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);
305 
306 	/*
307 	 * Check if hardware reset is still inactive or maybe there
308 	 * is no chip in this address space
309 	 */
310 	if (cc770_read_reg(priv, cpu_interface) & CPUIF_RST) {
311 		netdev_info(dev, "probing @0x%p failed (reset)\n",
312 			    priv->reg_base);
313 		return -ENODEV;
314 	}
315 
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",
324 			    priv->reg_base);
325 		return -ENODEV;
326 	}
327 
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;
331 
332 	return 0;
333 }
334 
335 static void cc770_start(struct net_device *dev)
336 {
337 	struct cc770_priv *priv = netdev_priv(dev);
338 
339 	/* leave reset mode */
340 	if (priv->can.state != CAN_STATE_STOPPED)
341 		set_reset_mode(dev);
342 
343 	/* leave reset mode */
344 	set_normal_mode(dev);
345 }
346 
347 static int cc770_set_mode(struct net_device *dev, enum can_mode mode)
348 {
349 	switch (mode) {
350 	case CAN_MODE_START:
351 		cc770_start(dev);
352 		netif_wake_queue(dev);
353 		break;
354 
355 	default:
356 		return -EOPNOTSUPP;
357 	}
358 
359 	return 0;
360 }
361 
362 static int cc770_set_bittiming(struct net_device *dev)
363 {
364 	struct cc770_priv *priv = netdev_priv(dev);
365 	struct can_bittiming *bt = &priv->can.bittiming;
366 	u8 btr0, btr1;
367 
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)
372 		btr1 |= 0x80;
373 
374 	netdev_info(dev, "setting BTR0=0x%02x BTR1=0x%02x\n", btr0, btr1);
375 
376 	cc770_write_reg(priv, bit_timing_0, btr0);
377 	cc770_write_reg(priv, bit_timing_1, btr1);
378 
379 	return 0;
380 }
381 
382 static int cc770_get_berr_counter(const struct net_device *dev,
383 				  struct can_berr_counter *bec)
384 {
385 	struct cc770_priv *priv = netdev_priv(dev);
386 
387 	bec->txerr = cc770_read_reg(priv, tx_error_counter);
388 	bec->rxerr = cc770_read_reg(priv, rx_error_counter);
389 
390 	return 0;
391 }
392 
393 static void cc770_tx(struct net_device *dev, int mo)
394 {
395 	struct cc770_priv *priv = netdev_priv(dev);
396 	struct can_frame *cf = (struct can_frame *)priv->tx_skb->data;
397 	u8 dlc, rtr;
398 	u32 id;
399 	int i;
400 
401 	dlc = cf->can_dlc;
402 	id = cf->can_id;
403 	rtr = cf->can_id & CAN_RTR_FLAG ? 0 : MSGCFG_DIR;
404 
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);
409 
410 	if (id & CAN_EFF_FLAG) {
411 		id &= CAN_EFF_MASK;
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);
418 	} else {
419 		id &= CAN_SFF_MASK;
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);
423 	}
424 
425 	for (i = 0; i < dlc; i++)
426 		cc770_write_reg(priv, msgobj[mo].data[i], cf->data[i]);
427 
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);
432 }
433 
434 static netdev_tx_t cc770_start_xmit(struct sk_buff *skb, struct net_device *dev)
435 {
436 	struct cc770_priv *priv = netdev_priv(dev);
437 	unsigned int mo = obj2msgobj(CC770_OBJ_TX);
438 
439 	if (can_dropped_invalid_skb(dev, skb))
440 		return NETDEV_TX_OK;
441 
442 	netif_stop_queue(dev);
443 
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;
448 	}
449 
450 	priv->tx_skb = skb;
451 	cc770_tx(dev, mo);
452 
453 	return NETDEV_TX_OK;
454 }
455 
456 static void cc770_rx(struct net_device *dev, unsigned int mo, u8 ctrl1)
457 {
458 	struct cc770_priv *priv = netdev_priv(dev);
459 	struct net_device_stats *stats = &dev->stats;
460 	struct can_frame *cf;
461 	struct sk_buff *skb;
462 	u8 config;
463 	u32 id;
464 	int i;
465 
466 	skb = alloc_can_skb(dev, &cf);
467 	if (!skb)
468 		return;
469 
470 	config = cc770_read_reg(priv, msgobj[mo].config);
471 
472 	if (ctrl1 & RMTPND_SET) {
473 		/*
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.
477 		 */
478 		cf->can_id = CAN_RTR_FLAG;
479 		if (config & MSGCFG_XTD)
480 			cf->can_id |= CAN_EFF_FLAG;
481 		cf->can_dlc = 0;
482 	} else {
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;
488 			id >>= 3;
489 			id |= CAN_EFF_FLAG;
490 		} else {
491 			id = cc770_read_reg(priv, msgobj[mo].id[1]);
492 			id |= cc770_read_reg(priv, msgobj[mo].id[0]) << 8;
493 			id >>= 5;
494 		}
495 
496 		cf->can_id = id;
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]);
500 	}
501 
502 	stats->rx_packets++;
503 	stats->rx_bytes += cf->can_dlc;
504 	netif_rx(skb);
505 }
506 
507 static int cc770_err(struct net_device *dev, u8 status)
508 {
509 	struct cc770_priv *priv = netdev_priv(dev);
510 	struct net_device_stats *stats = &dev->stats;
511 	struct can_frame *cf;
512 	struct sk_buff *skb;
513 	u8 lec;
514 
515 	netdev_dbg(dev, "status interrupt (%#x)\n", status);
516 
517 	skb = alloc_can_err_skb(dev, &cf);
518 	if (!skb)
519 		return -ENOMEM;
520 
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);
525 	}
526 
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++;
533 		can_bus_off(dev);
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++;
542 		} else {
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++;
547 		}
548 	} else {
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;
553 	}
554 
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;
559 		} else {
560 			cf->can_id |= CAN_ERR_PROT;
561 			switch (lec) {
562 			case STAT_LEC_STUFF:
563 				cf->data[2] |= CAN_ERR_PROT_STUFF;
564 				break;
565 			case STAT_LEC_FORM:
566 				cf->data[2] |= CAN_ERR_PROT_FORM;
567 				break;
568 			case STAT_LEC_BIT1:
569 				cf->data[2] |= CAN_ERR_PROT_BIT1;
570 				break;
571 			case STAT_LEC_BIT0:
572 				cf->data[2] |= CAN_ERR_PROT_BIT0;
573 				break;
574 			case STAT_LEC_CRC:
575 				cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ;
576 				break;
577 			}
578 		}
579 	}
580 
581 
582 	stats->rx_packets++;
583 	stats->rx_bytes += cf->can_dlc;
584 	netif_rx(skb);
585 
586 	return 0;
587 }
588 
589 static int cc770_status_interrupt(struct net_device *dev)
590 {
591 	struct cc770_priv *priv = netdev_priv(dev);
592 	u8 status;
593 
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);
597 
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;
602 	}
603 
604 	return 0;
605 }
606 
607 static void cc770_rx_interrupt(struct net_device *dev, unsigned int o)
608 {
609 	struct cc770_priv *priv = netdev_priv(dev);
610 	struct net_device_stats *stats = &dev->stats;
611 	unsigned int mo = obj2msgobj(o);
612 	u8 ctrl1;
613 	int n = CC770_MAX_MSG;
614 
615 	while (n--) {
616 		ctrl1 = cc770_read_reg(priv, msgobj[mo].ctrl1);
617 
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) &
622 				      INTPND_SET))
623 					break;
624 			} else {
625 				break;
626 			}
627 		}
628 
629 		if (ctrl1 & MSGLST_SET) {
630 			stats->rx_over_errors++;
631 			stats->rx_errors++;
632 		}
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);
638 
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);
645 	}
646 }
647 
648 static void cc770_rtr_interrupt(struct net_device *dev, unsigned int o)
649 {
650 	struct cc770_priv *priv = netdev_priv(dev);
651 	unsigned int mo = obj2msgobj(o);
652 	u8 ctrl0, ctrl1;
653 	int n = CC770_MAX_MSG;
654 
655 	while (n--) {
656 		ctrl0 = cc770_read_reg(priv, msgobj[mo].ctrl0);
657 		if (!(ctrl0 & INTPND_SET))
658 			break;
659 
660 		ctrl1 = cc770_read_reg(priv, msgobj[mo].ctrl1);
661 		cc770_rx(dev, mo, ctrl1);
662 
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);
669 	}
670 }
671 
672 static void cc770_tx_interrupt(struct net_device *dev, unsigned int o)
673 {
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;
678 	u8 ctrl1;
679 
680 	ctrl1 = cc770_read_reg(priv, msgobj[mo].ctrl1);
681 
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);
686 
687 	if (unlikely(!priv->tx_skb)) {
688 		netdev_err(dev, "missing tx skb in tx interrupt\n");
689 		return;
690 	}
691 
692 	if (unlikely(ctrl1 & MSGLST_SET)) {
693 		stats->rx_over_errors++;
694 		stats->rx_errors++;
695 	}
696 
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
701 	 * again.
702 	 */
703 	if (unlikely(ctrl1 & NEWDAT_SET)) {
704 		cc770_rx(dev, mo, ctrl1);
705 		cc770_tx(dev, mo);
706 		return;
707 	}
708 
709 	cf = (struct can_frame *)priv->tx_skb->data;
710 	stats->tx_bytes += cf->can_dlc;
711 	stats->tx_packets++;
712 
713 	can_put_echo_skb(priv->tx_skb, dev, 0);
714 	can_get_echo_skb(dev, 0);
715 	priv->tx_skb = NULL;
716 
717 	netif_wake_queue(dev);
718 }
719 
720 static irqreturn_t cc770_interrupt(int irq, void *dev_id)
721 {
722 	struct net_device *dev = (struct net_device *)dev_id;
723 	struct cc770_priv *priv = netdev_priv(dev);
724 	u8 intid;
725 	int o, n = 0;
726 
727 	/* Shared interrupts and IRQ off? */
728 	if (priv->can.state == CAN_STATE_STOPPED)
729 		return IRQ_NONE;
730 
731 	if (priv->pre_irq)
732 		priv->pre_irq(priv);
733 
734 	while (n < CC770_MAX_IRQ) {
735 		/* Read the highest pending interrupt request */
736 		intid = cc770_read_reg(priv, interrupt);
737 		if (!intid)
738 			break;
739 		n++;
740 
741 		if (intid == 1) {
742 			/* Exit in case of bus-off */
743 			if (cc770_status_interrupt(dev))
744 				break;
745 		} else {
746 			o = intid2obj(intid);
747 
748 			if (o >= CC770_OBJ_MAX) {
749 				netdev_err(dev, "Unexpected interrupt id %d\n",
750 					   intid);
751 				continue;
752 			}
753 
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);
758 			else
759 				cc770_tx_interrupt(dev, o);
760 		}
761 	}
762 
763 	if (priv->post_irq)
764 		priv->post_irq(priv);
765 
766 	if (n >= CC770_MAX_IRQ)
767 		netdev_dbg(dev, "%d messages handled in ISR", n);
768 
769 	return (n) ? IRQ_HANDLED : IRQ_NONE;
770 }
771 
772 static int cc770_open(struct net_device *dev)
773 {
774 	struct cc770_priv *priv = netdev_priv(dev);
775 	int err;
776 
777 	/* set chip into reset mode */
778 	set_reset_mode(dev);
779 
780 	/* common open */
781 	err = open_candev(dev);
782 	if (err)
783 		return err;
784 
785 	err = request_irq(dev->irq, &cc770_interrupt, priv->irq_flags,
786 			  dev->name, dev);
787 	if (err) {
788 		close_candev(dev);
789 		return -EAGAIN;
790 	}
791 
792 	/* init and start chip */
793 	cc770_start(dev);
794 
795 	netif_start_queue(dev);
796 
797 	return 0;
798 }
799 
800 static int cc770_close(struct net_device *dev)
801 {
802 	netif_stop_queue(dev);
803 	set_reset_mode(dev);
804 
805 	free_irq(dev->irq, dev);
806 	close_candev(dev);
807 
808 	return 0;
809 }
810 
811 struct net_device *alloc_cc770dev(int sizeof_priv)
812 {
813 	struct net_device *dev;
814 	struct cc770_priv *priv;
815 
816 	dev = alloc_candev(sizeof(struct cc770_priv) + sizeof_priv,
817 			   CC770_ECHO_SKB_MAX);
818 	if (!dev)
819 		return NULL;
820 
821 	priv = netdev_priv(dev);
822 
823 	priv->dev = 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;
828 	priv->tx_skb = NULL;
829 
830 	memcpy(priv->obj_flags, cc770_obj_flags, sizeof(cc770_obj_flags));
831 
832 	if (sizeof_priv)
833 		priv->priv = (void *)priv + sizeof(struct cc770_priv);
834 
835 	return dev;
836 }
837 EXPORT_SYMBOL_GPL(alloc_cc770dev);
838 
839 void free_cc770dev(struct net_device *dev)
840 {
841 	free_candev(dev);
842 }
843 EXPORT_SYMBOL_GPL(free_cc770dev);
844 
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,
850 };
851 
852 int register_cc770dev(struct net_device *dev)
853 {
854 	struct cc770_priv *priv = netdev_priv(dev);
855 	int err;
856 
857 	err = cc770_probe_chip(dev);
858 	if (err)
859 		return err;
860 
861 	dev->netdev_ops = &cc770_netdev_ops;
862 
863 	dev->flags |= IFF_ECHO;	/* we support local echo */
864 
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");
870 	} else {
871 		priv->control_normal_mode = CTRL_IE | CTRL_EIE;
872 		netdev_dbg(dev, "strict i82527 compatibility mode\n");
873 	}
874 
875 	chipset_init(priv);
876 	set_reset_mode(dev);
877 
878 	return register_candev(dev);
879 }
880 EXPORT_SYMBOL_GPL(register_cc770dev);
881 
882 void unregister_cc770dev(struct net_device *dev)
883 {
884 	set_reset_mode(dev);
885 	unregister_candev(dev);
886 }
887 EXPORT_SYMBOL_GPL(unregister_cc770dev);
888 
889 static __init int cc770_init(void)
890 {
891 	if (msgobj15_eff) {
892 		cc770_obj_flags[CC770_OBJ_RX0] |= CC770_OBJ_FLAG_EFF;
893 		cc770_obj_flags[CC770_OBJ_RX1] &= ~CC770_OBJ_FLAG_EFF;
894 	}
895 
896 	pr_info("CAN netdevice driver\n");
897 
898 	return 0;
899 }
900 module_init(cc770_init);
901 
902 static __exit void cc770_exit(void)
903 {
904 	pr_info("driver removed\n");
905 }
906 module_exit(cc770_exit);
907