xref: /openbmc/linux/drivers/net/can/sja1000/sja1000.c (revision b6dcefde)
1 /*
2  * sja1000.c -  Philips SJA1000 network device driver
3  *
4  * Copyright (c) 2003 Matthias Brukner, Trajet Gmbh, Rebenring 33,
5  * 38106 Braunschweig, GERMANY
6  *
7  * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of Volkswagen nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * Alternatively, provided that this notice is retained in full, this
23  * software may be distributed under the terms of the GNU General
24  * Public License ("GPL") version 2, in which case the provisions of the
25  * GPL apply INSTEAD OF those given above.
26  *
27  * The provided data structures and external interfaces from this code
28  * are not restricted to be used by modules with a GPL compatible license.
29  *
30  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
33  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
34  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
36  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
40  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
41  * DAMAGE.
42  *
43  * Send feedback to <socketcan-users@lists.berlios.de>
44  *
45  */
46 
47 #include <linux/module.h>
48 #include <linux/init.h>
49 #include <linux/kernel.h>
50 #include <linux/sched.h>
51 #include <linux/types.h>
52 #include <linux/fcntl.h>
53 #include <linux/interrupt.h>
54 #include <linux/ptrace.h>
55 #include <linux/string.h>
56 #include <linux/errno.h>
57 #include <linux/netdevice.h>
58 #include <linux/if_arp.h>
59 #include <linux/if_ether.h>
60 #include <linux/skbuff.h>
61 #include <linux/delay.h>
62 
63 #include <linux/can.h>
64 #include <linux/can/dev.h>
65 #include <linux/can/error.h>
66 
67 #include "sja1000.h"
68 
69 #define DRV_NAME "sja1000"
70 
71 MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
72 MODULE_LICENSE("Dual BSD/GPL");
73 MODULE_DESCRIPTION(DRV_NAME "CAN netdevice driver");
74 
75 static struct can_bittiming_const sja1000_bittiming_const = {
76 	.name = DRV_NAME,
77 	.tseg1_min = 1,
78 	.tseg1_max = 16,
79 	.tseg2_min = 1,
80 	.tseg2_max = 8,
81 	.sjw_max = 4,
82 	.brp_min = 1,
83 	.brp_max = 64,
84 	.brp_inc = 1,
85 };
86 
87 static int sja1000_probe_chip(struct net_device *dev)
88 {
89 	struct sja1000_priv *priv = netdev_priv(dev);
90 
91 	if (priv->reg_base && (priv->read_reg(priv, 0) == 0xFF)) {
92 		printk(KERN_INFO "%s: probing @0x%lX failed\n",
93 		       DRV_NAME, dev->base_addr);
94 		return 0;
95 	}
96 	return -1;
97 }
98 
99 static void set_reset_mode(struct net_device *dev)
100 {
101 	struct sja1000_priv *priv = netdev_priv(dev);
102 	unsigned char status = priv->read_reg(priv, REG_MOD);
103 	int i;
104 
105 	/* disable interrupts */
106 	priv->write_reg(priv, REG_IER, IRQ_OFF);
107 
108 	for (i = 0; i < 100; i++) {
109 		/* check reset bit */
110 		if (status & MOD_RM) {
111 			priv->can.state = CAN_STATE_STOPPED;
112 			return;
113 		}
114 
115 		priv->write_reg(priv, REG_MOD, MOD_RM);	/* reset chip */
116 		udelay(10);
117 		status = priv->read_reg(priv, REG_MOD);
118 	}
119 
120 	dev_err(dev->dev.parent, "setting SJA1000 into reset mode failed!\n");
121 }
122 
123 static void set_normal_mode(struct net_device *dev)
124 {
125 	struct sja1000_priv *priv = netdev_priv(dev);
126 	unsigned char status = priv->read_reg(priv, REG_MOD);
127 	int i;
128 
129 	for (i = 0; i < 100; i++) {
130 		/* check reset bit */
131 		if ((status & MOD_RM) == 0) {
132 			priv->can.state = CAN_STATE_ERROR_ACTIVE;
133 			/* enable all interrupts */
134 			priv->write_reg(priv, REG_IER, IRQ_ALL);
135 			return;
136 		}
137 
138 		/* set chip to normal mode */
139 		priv->write_reg(priv, REG_MOD, 0x00);
140 		udelay(10);
141 		status = priv->read_reg(priv, REG_MOD);
142 	}
143 
144 	dev_err(dev->dev.parent, "setting SJA1000 into normal mode failed!\n");
145 }
146 
147 static void sja1000_start(struct net_device *dev)
148 {
149 	struct sja1000_priv *priv = netdev_priv(dev);
150 
151 	/* leave reset mode */
152 	if (priv->can.state != CAN_STATE_STOPPED)
153 		set_reset_mode(dev);
154 
155 	/* Clear error counters and error code capture */
156 	priv->write_reg(priv, REG_TXERR, 0x0);
157 	priv->write_reg(priv, REG_RXERR, 0x0);
158 	priv->read_reg(priv, REG_ECC);
159 
160 	/* leave reset mode */
161 	set_normal_mode(dev);
162 }
163 
164 static int sja1000_set_mode(struct net_device *dev, enum can_mode mode)
165 {
166 	struct sja1000_priv *priv = netdev_priv(dev);
167 
168 	if (!priv->open_time)
169 		return -EINVAL;
170 
171 	switch (mode) {
172 	case CAN_MODE_START:
173 		sja1000_start(dev);
174 		if (netif_queue_stopped(dev))
175 			netif_wake_queue(dev);
176 		break;
177 
178 	default:
179 		return -EOPNOTSUPP;
180 	}
181 
182 	return 0;
183 }
184 
185 static int sja1000_set_bittiming(struct net_device *dev)
186 {
187 	struct sja1000_priv *priv = netdev_priv(dev);
188 	struct can_bittiming *bt = &priv->can.bittiming;
189 	u8 btr0, btr1;
190 
191 	btr0 = ((bt->brp - 1) & 0x3f) | (((bt->sjw - 1) & 0x3) << 6);
192 	btr1 = ((bt->prop_seg + bt->phase_seg1 - 1) & 0xf) |
193 		(((bt->phase_seg2 - 1) & 0x7) << 4);
194 	if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
195 		btr1 |= 0x80;
196 
197 	dev_info(dev->dev.parent,
198 		 "setting BTR0=0x%02x BTR1=0x%02x\n", btr0, btr1);
199 
200 	priv->write_reg(priv, REG_BTR0, btr0);
201 	priv->write_reg(priv, REG_BTR1, btr1);
202 
203 	return 0;
204 }
205 
206 /*
207  * initialize SJA1000 chip:
208  *   - reset chip
209  *   - set output mode
210  *   - set baudrate
211  *   - enable interrupts
212  *   - start operating mode
213  */
214 static void chipset_init(struct net_device *dev)
215 {
216 	struct sja1000_priv *priv = netdev_priv(dev);
217 
218 	/* set clock divider and output control register */
219 	priv->write_reg(priv, REG_CDR, priv->cdr | CDR_PELICAN);
220 
221 	/* set acceptance filter (accept all) */
222 	priv->write_reg(priv, REG_ACCC0, 0x00);
223 	priv->write_reg(priv, REG_ACCC1, 0x00);
224 	priv->write_reg(priv, REG_ACCC2, 0x00);
225 	priv->write_reg(priv, REG_ACCC3, 0x00);
226 
227 	priv->write_reg(priv, REG_ACCM0, 0xFF);
228 	priv->write_reg(priv, REG_ACCM1, 0xFF);
229 	priv->write_reg(priv, REG_ACCM2, 0xFF);
230 	priv->write_reg(priv, REG_ACCM3, 0xFF);
231 
232 	priv->write_reg(priv, REG_OCR, priv->ocr | OCR_MODE_NORMAL);
233 }
234 
235 /*
236  * transmit a CAN message
237  * message layout in the sk_buff should be like this:
238  * xx xx xx xx	 ff	 ll   00 11 22 33 44 55 66 77
239  * [  can-id ] [flags] [len] [can data (up to 8 bytes]
240  */
241 static netdev_tx_t sja1000_start_xmit(struct sk_buff *skb,
242 					    struct net_device *dev)
243 {
244 	struct sja1000_priv *priv = netdev_priv(dev);
245 	struct can_frame *cf = (struct can_frame *)skb->data;
246 	uint8_t fi;
247 	uint8_t dlc;
248 	canid_t id;
249 	uint8_t dreg;
250 	int i;
251 
252 	netif_stop_queue(dev);
253 
254 	fi = dlc = cf->can_dlc;
255 	id = cf->can_id;
256 
257 	if (id & CAN_RTR_FLAG)
258 		fi |= FI_RTR;
259 
260 	if (id & CAN_EFF_FLAG) {
261 		fi |= FI_FF;
262 		dreg = EFF_BUF;
263 		priv->write_reg(priv, REG_FI, fi);
264 		priv->write_reg(priv, REG_ID1, (id & 0x1fe00000) >> (5 + 16));
265 		priv->write_reg(priv, REG_ID2, (id & 0x001fe000) >> (5 + 8));
266 		priv->write_reg(priv, REG_ID3, (id & 0x00001fe0) >> 5);
267 		priv->write_reg(priv, REG_ID4, (id & 0x0000001f) << 3);
268 	} else {
269 		dreg = SFF_BUF;
270 		priv->write_reg(priv, REG_FI, fi);
271 		priv->write_reg(priv, REG_ID1, (id & 0x000007f8) >> 3);
272 		priv->write_reg(priv, REG_ID2, (id & 0x00000007) << 5);
273 	}
274 
275 	for (i = 0; i < dlc; i++)
276 		priv->write_reg(priv, dreg++, cf->data[i]);
277 
278 	dev->trans_start = jiffies;
279 
280 	can_put_echo_skb(skb, dev, 0);
281 
282 	priv->write_reg(priv, REG_CMR, CMD_TR);
283 
284 	return NETDEV_TX_OK;
285 }
286 
287 static void sja1000_rx(struct net_device *dev)
288 {
289 	struct sja1000_priv *priv = netdev_priv(dev);
290 	struct net_device_stats *stats = &dev->stats;
291 	struct can_frame *cf;
292 	struct sk_buff *skb;
293 	uint8_t fi;
294 	uint8_t dreg;
295 	canid_t id;
296 	int i;
297 
298 	/* create zero'ed CAN frame buffer */
299 	skb = alloc_can_skb(dev, &cf);
300 	if (skb == NULL)
301 		return;
302 
303 	fi = priv->read_reg(priv, REG_FI);
304 
305 	if (fi & FI_FF) {
306 		/* extended frame format (EFF) */
307 		dreg = EFF_BUF;
308 		id = (priv->read_reg(priv, REG_ID1) << (5 + 16))
309 		    | (priv->read_reg(priv, REG_ID2) << (5 + 8))
310 		    | (priv->read_reg(priv, REG_ID3) << 5)
311 		    | (priv->read_reg(priv, REG_ID4) >> 3);
312 		id |= CAN_EFF_FLAG;
313 	} else {
314 		/* standard frame format (SFF) */
315 		dreg = SFF_BUF;
316 		id = (priv->read_reg(priv, REG_ID1) << 3)
317 		    | (priv->read_reg(priv, REG_ID2) >> 5);
318 	}
319 
320 	if (fi & FI_RTR) {
321 		id |= CAN_RTR_FLAG;
322 	} else {
323 		cf->can_dlc = get_can_dlc(fi & 0x0F);
324 		for (i = 0; i < cf->can_dlc; i++)
325 			cf->data[i] = priv->read_reg(priv, dreg++);
326 	}
327 
328 	cf->can_id = id;
329 
330 	/* release receive buffer */
331 	priv->write_reg(priv, REG_CMR, CMD_RRB);
332 
333 	netif_rx(skb);
334 
335 	stats->rx_packets++;
336 	stats->rx_bytes += cf->can_dlc;
337 }
338 
339 static int sja1000_err(struct net_device *dev, uint8_t isrc, uint8_t status)
340 {
341 	struct sja1000_priv *priv = netdev_priv(dev);
342 	struct net_device_stats *stats = &dev->stats;
343 	struct can_frame *cf;
344 	struct sk_buff *skb;
345 	enum can_state state = priv->can.state;
346 	uint8_t ecc, alc;
347 
348 	skb = alloc_can_err_skb(dev, &cf);
349 	if (skb == NULL)
350 		return -ENOMEM;
351 
352 	if (isrc & IRQ_DOI) {
353 		/* data overrun interrupt */
354 		dev_dbg(dev->dev.parent, "data overrun interrupt\n");
355 		cf->can_id |= CAN_ERR_CRTL;
356 		cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
357 		stats->rx_over_errors++;
358 		stats->rx_errors++;
359 		priv->write_reg(priv, REG_CMR, CMD_CDO);	/* clear bit */
360 	}
361 
362 	if (isrc & IRQ_EI) {
363 		/* error warning interrupt */
364 		dev_dbg(dev->dev.parent, "error warning interrupt\n");
365 
366 		if (status & SR_BS) {
367 			state = CAN_STATE_BUS_OFF;
368 			cf->can_id |= CAN_ERR_BUSOFF;
369 			can_bus_off(dev);
370 		} else if (status & SR_ES) {
371 			state = CAN_STATE_ERROR_WARNING;
372 		} else
373 			state = CAN_STATE_ERROR_ACTIVE;
374 	}
375 	if (isrc & IRQ_BEI) {
376 		/* bus error interrupt */
377 		priv->can.can_stats.bus_error++;
378 		stats->rx_errors++;
379 
380 		ecc = priv->read_reg(priv, REG_ECC);
381 
382 		cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
383 
384 		switch (ecc & ECC_MASK) {
385 		case ECC_BIT:
386 			cf->data[2] |= CAN_ERR_PROT_BIT;
387 			break;
388 		case ECC_FORM:
389 			cf->data[2] |= CAN_ERR_PROT_FORM;
390 			break;
391 		case ECC_STUFF:
392 			cf->data[2] |= CAN_ERR_PROT_STUFF;
393 			break;
394 		default:
395 			cf->data[2] |= CAN_ERR_PROT_UNSPEC;
396 			cf->data[3] = ecc & ECC_SEG;
397 			break;
398 		}
399 		/* Error occured during transmission? */
400 		if ((ecc & ECC_DIR) == 0)
401 			cf->data[2] |= CAN_ERR_PROT_TX;
402 	}
403 	if (isrc & IRQ_EPI) {
404 		/* error passive interrupt */
405 		dev_dbg(dev->dev.parent, "error passive interrupt\n");
406 		if (status & SR_ES)
407 			state = CAN_STATE_ERROR_PASSIVE;
408 		else
409 			state = CAN_STATE_ERROR_ACTIVE;
410 	}
411 	if (isrc & IRQ_ALI) {
412 		/* arbitration lost interrupt */
413 		dev_dbg(dev->dev.parent, "arbitration lost interrupt\n");
414 		alc = priv->read_reg(priv, REG_ALC);
415 		priv->can.can_stats.arbitration_lost++;
416 		stats->tx_errors++;
417 		cf->can_id |= CAN_ERR_LOSTARB;
418 		cf->data[0] = alc & 0x1f;
419 	}
420 
421 	if (state != priv->can.state && (state == CAN_STATE_ERROR_WARNING ||
422 					 state == CAN_STATE_ERROR_PASSIVE)) {
423 		uint8_t rxerr = priv->read_reg(priv, REG_RXERR);
424 		uint8_t txerr = priv->read_reg(priv, REG_TXERR);
425 		cf->can_id |= CAN_ERR_CRTL;
426 		if (state == CAN_STATE_ERROR_WARNING) {
427 			priv->can.can_stats.error_warning++;
428 			cf->data[1] = (txerr > rxerr) ?
429 				CAN_ERR_CRTL_TX_WARNING :
430 				CAN_ERR_CRTL_RX_WARNING;
431 		} else {
432 			priv->can.can_stats.error_passive++;
433 			cf->data[1] = (txerr > rxerr) ?
434 				CAN_ERR_CRTL_TX_PASSIVE :
435 				CAN_ERR_CRTL_RX_PASSIVE;
436 		}
437 	}
438 
439 	priv->can.state = state;
440 
441 	netif_rx(skb);
442 
443 	stats->rx_packets++;
444 	stats->rx_bytes += cf->can_dlc;
445 
446 	return 0;
447 }
448 
449 irqreturn_t sja1000_interrupt(int irq, void *dev_id)
450 {
451 	struct net_device *dev = (struct net_device *)dev_id;
452 	struct sja1000_priv *priv = netdev_priv(dev);
453 	struct net_device_stats *stats = &dev->stats;
454 	uint8_t isrc, status;
455 	int n = 0;
456 
457 	/* Shared interrupts and IRQ off? */
458 	if (priv->read_reg(priv, REG_IER) == IRQ_OFF)
459 		return IRQ_NONE;
460 
461 	if (priv->pre_irq)
462 		priv->pre_irq(priv);
463 
464 	while ((isrc = priv->read_reg(priv, REG_IR)) && (n < SJA1000_MAX_IRQ)) {
465 		n++;
466 		status = priv->read_reg(priv, REG_SR);
467 
468 		if (isrc & IRQ_WUI)
469 			dev_warn(dev->dev.parent, "wakeup interrupt\n");
470 
471 		if (isrc & IRQ_TI) {
472 			/* transmission complete interrupt */
473 			stats->tx_bytes += priv->read_reg(priv, REG_FI) & 0xf;
474 			stats->tx_packets++;
475 			can_get_echo_skb(dev, 0);
476 			netif_wake_queue(dev);
477 		}
478 		if (isrc & IRQ_RI) {
479 			/* receive interrupt */
480 			while (status & SR_RBS) {
481 				sja1000_rx(dev);
482 				status = priv->read_reg(priv, REG_SR);
483 			}
484 		}
485 		if (isrc & (IRQ_DOI | IRQ_EI | IRQ_BEI | IRQ_EPI | IRQ_ALI)) {
486 			/* error interrupt */
487 			if (sja1000_err(dev, isrc, status))
488 				break;
489 		}
490 	}
491 
492 	if (priv->post_irq)
493 		priv->post_irq(priv);
494 
495 	if (n >= SJA1000_MAX_IRQ)
496 		dev_dbg(dev->dev.parent, "%d messages handled in ISR", n);
497 
498 	return (n) ? IRQ_HANDLED : IRQ_NONE;
499 }
500 EXPORT_SYMBOL_GPL(sja1000_interrupt);
501 
502 static int sja1000_open(struct net_device *dev)
503 {
504 	struct sja1000_priv *priv = netdev_priv(dev);
505 	int err;
506 
507 	/* set chip into reset mode */
508 	set_reset_mode(dev);
509 
510 	/* common open */
511 	err = open_candev(dev);
512 	if (err)
513 		return err;
514 
515 	/* register interrupt handler, if not done by the device driver */
516 	if (!(priv->flags & SJA1000_CUSTOM_IRQ_HANDLER)) {
517 		err = request_irq(dev->irq, sja1000_interrupt, priv->irq_flags,
518 				  dev->name, (void *)dev);
519 		if (err) {
520 			close_candev(dev);
521 			return -EAGAIN;
522 		}
523 	}
524 
525 	/* init and start chi */
526 	sja1000_start(dev);
527 	priv->open_time = jiffies;
528 
529 	netif_start_queue(dev);
530 
531 	return 0;
532 }
533 
534 static int sja1000_close(struct net_device *dev)
535 {
536 	struct sja1000_priv *priv = netdev_priv(dev);
537 
538 	netif_stop_queue(dev);
539 	set_reset_mode(dev);
540 
541 	if (!(priv->flags & SJA1000_CUSTOM_IRQ_HANDLER))
542 		free_irq(dev->irq, (void *)dev);
543 
544 	close_candev(dev);
545 
546 	priv->open_time = 0;
547 
548 	return 0;
549 }
550 
551 struct net_device *alloc_sja1000dev(int sizeof_priv)
552 {
553 	struct net_device *dev;
554 	struct sja1000_priv *priv;
555 
556 	dev = alloc_candev(sizeof(struct sja1000_priv) + sizeof_priv,
557 		SJA1000_ECHO_SKB_MAX);
558 	if (!dev)
559 		return NULL;
560 
561 	priv = netdev_priv(dev);
562 
563 	priv->dev = dev;
564 	priv->can.bittiming_const = &sja1000_bittiming_const;
565 	priv->can.do_set_bittiming = sja1000_set_bittiming;
566 	priv->can.do_set_mode = sja1000_set_mode;
567 
568 	if (sizeof_priv)
569 		priv->priv = (void *)priv + sizeof(struct sja1000_priv);
570 
571 	return dev;
572 }
573 EXPORT_SYMBOL_GPL(alloc_sja1000dev);
574 
575 void free_sja1000dev(struct net_device *dev)
576 {
577 	free_candev(dev);
578 }
579 EXPORT_SYMBOL_GPL(free_sja1000dev);
580 
581 static const struct net_device_ops sja1000_netdev_ops = {
582        .ndo_open               = sja1000_open,
583        .ndo_stop               = sja1000_close,
584        .ndo_start_xmit         = sja1000_start_xmit,
585 };
586 
587 int register_sja1000dev(struct net_device *dev)
588 {
589 	if (!sja1000_probe_chip(dev))
590 		return -ENODEV;
591 
592 	dev->flags |= IFF_ECHO;	/* we support local echo */
593 	dev->netdev_ops = &sja1000_netdev_ops;
594 
595 	set_reset_mode(dev);
596 	chipset_init(dev);
597 
598 	return register_candev(dev);
599 }
600 EXPORT_SYMBOL_GPL(register_sja1000dev);
601 
602 void unregister_sja1000dev(struct net_device *dev)
603 {
604 	set_reset_mode(dev);
605 	unregister_candev(dev);
606 }
607 EXPORT_SYMBOL_GPL(unregister_sja1000dev);
608 
609 static __init int sja1000_init(void)
610 {
611 	printk(KERN_INFO "%s CAN netdevice driver\n", DRV_NAME);
612 
613 	return 0;
614 }
615 
616 module_init(sja1000_init);
617 
618 static __exit void sja1000_exit(void)
619 {
620 	printk(KERN_INFO "%s: driver removed\n", DRV_NAME);
621 }
622 
623 module_exit(sja1000_exit);
624