diff options
author | Wolfgang Grandegger <wg@grandegger.com> | 2009-05-15 19:39:30 -0400 |
---|---|---|
committer | David S. Miller <davem@davemloft.net> | 2009-05-18 18:41:41 -0400 |
commit | 429da1cc841bc9f2e762fd7272fc2b80314b890a (patch) | |
tree | a9ae764bf413b6597f8ec8a957483bd17f3c2941 /drivers/net/can | |
parent | 39549eef3587f1c1e8c65c88a2400d10fd30ea17 (diff) |
can: Driver for the SJA1000 CAN controller
This patch adds the generic Socket-CAN driver for the Philips SJA1000
full CAN controller.
Signed-off-by: Wolfgang Grandegger <wg@grandegger.com>
Signed-off-by: Oliver Hartkopp <oliver.hartkopp@volkswagen.de>
Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'drivers/net/can')
-rw-r--r-- | drivers/net/can/Kconfig | 6 | ||||
-rw-r--r-- | drivers/net/can/Makefile | 2 | ||||
-rw-r--r-- | drivers/net/can/sja1000/Makefile | 7 | ||||
-rw-r--r-- | drivers/net/can/sja1000/sja1000.c | 637 | ||||
-rw-r--r-- | drivers/net/can/sja1000/sja1000.h | 180 |
5 files changed, 832 insertions, 0 deletions
diff --git a/drivers/net/can/Kconfig b/drivers/net/can/Kconfig index 77adb8ef6e4f..c7e5ce339ead 100644 --- a/drivers/net/can/Kconfig +++ b/drivers/net/can/Kconfig | |||
@@ -35,6 +35,12 @@ config CAN_CALC_BITTIMING | |||
35 | arguments "tq", "prop_seg", "phase_seg1", "phase_seg2" and "sjw". | 35 | arguments "tq", "prop_seg", "phase_seg1", "phase_seg2" and "sjw". |
36 | If unsure, say Y. | 36 | If unsure, say Y. |
37 | 37 | ||
38 | config CAN_SJA1000 | ||
39 | depends on CAN_DEV | ||
40 | tristate "Philips SJA1000" | ||
41 | ---help--- | ||
42 | Driver for the SJA1000 CAN controllers from Philips or NXP | ||
43 | |||
38 | config CAN_DEBUG_DEVICES | 44 | config CAN_DEBUG_DEVICES |
39 | bool "CAN devices debugging messages" | 45 | bool "CAN devices debugging messages" |
40 | depends on CAN | 46 | depends on CAN |
diff --git a/drivers/net/can/Makefile b/drivers/net/can/Makefile index 6c865475cd88..523a941b358b 100644 --- a/drivers/net/can/Makefile +++ b/drivers/net/can/Makefile | |||
@@ -7,4 +7,6 @@ obj-$(CONFIG_CAN_VCAN) += vcan.o | |||
7 | obj-$(CONFIG_CAN_DEV) += can-dev.o | 7 | obj-$(CONFIG_CAN_DEV) += can-dev.o |
8 | can-dev-y := dev.o | 8 | can-dev-y := dev.o |
9 | 9 | ||
10 | obj-$(CONFIG_CAN_SJA1000) += sja1000/ | ||
11 | |||
10 | ccflags-$(CONFIG_CAN_DEBUG_DEVICES) := -DDEBUG | 12 | ccflags-$(CONFIG_CAN_DEBUG_DEVICES) := -DDEBUG |
diff --git a/drivers/net/can/sja1000/Makefile b/drivers/net/can/sja1000/Makefile new file mode 100644 index 000000000000..893ab2cfe244 --- /dev/null +++ b/drivers/net/can/sja1000/Makefile | |||
@@ -0,0 +1,7 @@ | |||
1 | # | ||
2 | # Makefile for the SJA1000 CAN controller drivers. | ||
3 | # | ||
4 | |||
5 | obj-$(CONFIG_CAN_SJA1000) += sja1000.o | ||
6 | |||
7 | ccflags-$(CONFIG_CAN_DEBUG_DEVICES) := -DDEBUG | ||
diff --git a/drivers/net/can/sja1000/sja1000.c b/drivers/net/can/sja1000/sja1000.c new file mode 100644 index 000000000000..05b38dde648e --- /dev/null +++ b/drivers/net/can/sja1000/sja1000.c | |||
@@ -0,0 +1,637 @@ | |||
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 | #include <linux/can/dev.h> | ||
67 | |||
68 | #include "sja1000.h" | ||
69 | |||
70 | #define DRV_NAME "sja1000" | ||
71 | |||
72 | MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>"); | ||
73 | MODULE_LICENSE("Dual BSD/GPL"); | ||
74 | MODULE_DESCRIPTION(DRV_NAME "CAN netdevice driver"); | ||
75 | |||
76 | static struct can_bittiming_const sja1000_bittiming_const = { | ||
77 | .name = DRV_NAME, | ||
78 | .tseg1_min = 1, | ||
79 | .tseg1_max = 16, | ||
80 | .tseg2_min = 1, | ||
81 | .tseg2_max = 8, | ||
82 | .sjw_max = 4, | ||
83 | .brp_min = 1, | ||
84 | .brp_max = 64, | ||
85 | .brp_inc = 1, | ||
86 | }; | ||
87 | |||
88 | static int sja1000_probe_chip(struct net_device *dev) | ||
89 | { | ||
90 | struct sja1000_priv *priv = netdev_priv(dev); | ||
91 | |||
92 | if (dev->base_addr && (priv->read_reg(dev, 0) == 0xFF)) { | ||
93 | printk(KERN_INFO "%s: probing @0x%lX failed\n", | ||
94 | DRV_NAME, dev->base_addr); | ||
95 | return 0; | ||
96 | } | ||
97 | return -1; | ||
98 | } | ||
99 | |||
100 | static void set_reset_mode(struct net_device *dev) | ||
101 | { | ||
102 | struct sja1000_priv *priv = netdev_priv(dev); | ||
103 | unsigned char status = priv->read_reg(dev, REG_MOD); | ||
104 | int i; | ||
105 | |||
106 | /* disable interrupts */ | ||
107 | priv->write_reg(dev, REG_IER, IRQ_OFF); | ||
108 | |||
109 | for (i = 0; i < 100; i++) { | ||
110 | /* check reset bit */ | ||
111 | if (status & MOD_RM) { | ||
112 | priv->can.state = CAN_STATE_STOPPED; | ||
113 | return; | ||
114 | } | ||
115 | |||
116 | priv->write_reg(dev, REG_MOD, MOD_RM); /* reset chip */ | ||
117 | udelay(10); | ||
118 | status = priv->read_reg(dev, REG_MOD); | ||
119 | } | ||
120 | |||
121 | dev_err(dev->dev.parent, "setting SJA1000 into reset mode failed!\n"); | ||
122 | } | ||
123 | |||
124 | static void set_normal_mode(struct net_device *dev) | ||
125 | { | ||
126 | struct sja1000_priv *priv = netdev_priv(dev); | ||
127 | unsigned char status = priv->read_reg(dev, REG_MOD); | ||
128 | int i; | ||
129 | |||
130 | for (i = 0; i < 100; i++) { | ||
131 | /* check reset bit */ | ||
132 | if ((status & MOD_RM) == 0) { | ||
133 | priv->can.state = CAN_STATE_ERROR_ACTIVE; | ||
134 | /* enable all interrupts */ | ||
135 | priv->write_reg(dev, REG_IER, IRQ_ALL); | ||
136 | return; | ||
137 | } | ||
138 | |||
139 | /* set chip to normal mode */ | ||
140 | priv->write_reg(dev, REG_MOD, 0x00); | ||
141 | udelay(10); | ||
142 | status = priv->read_reg(dev, REG_MOD); | ||
143 | } | ||
144 | |||
145 | dev_err(dev->dev.parent, "setting SJA1000 into normal mode failed!\n"); | ||
146 | } | ||
147 | |||
148 | static void sja1000_start(struct net_device *dev) | ||
149 | { | ||
150 | struct sja1000_priv *priv = netdev_priv(dev); | ||
151 | |||
152 | /* leave reset mode */ | ||
153 | if (priv->can.state != CAN_STATE_STOPPED) | ||
154 | set_reset_mode(dev); | ||
155 | |||
156 | /* Clear error counters and error code capture */ | ||
157 | priv->write_reg(dev, REG_TXERR, 0x0); | ||
158 | priv->write_reg(dev, REG_RXERR, 0x0); | ||
159 | priv->read_reg(dev, REG_ECC); | ||
160 | |||
161 | /* leave reset mode */ | ||
162 | set_normal_mode(dev); | ||
163 | } | ||
164 | |||
165 | static int sja1000_set_mode(struct net_device *dev, enum can_mode mode) | ||
166 | { | ||
167 | struct sja1000_priv *priv = netdev_priv(dev); | ||
168 | |||
169 | if (!priv->open_time) | ||
170 | return -EINVAL; | ||
171 | |||
172 | switch (mode) { | ||
173 | case CAN_MODE_START: | ||
174 | sja1000_start(dev); | ||
175 | if (netif_queue_stopped(dev)) | ||
176 | netif_wake_queue(dev); | ||
177 | break; | ||
178 | |||
179 | default: | ||
180 | return -EOPNOTSUPP; | ||
181 | } | ||
182 | |||
183 | return 0; | ||
184 | } | ||
185 | |||
186 | static int sja1000_set_bittiming(struct net_device *dev) | ||
187 | { | ||
188 | struct sja1000_priv *priv = netdev_priv(dev); | ||
189 | struct can_bittiming *bt = &priv->can.bittiming; | ||
190 | u8 btr0, btr1; | ||
191 | |||
192 | btr0 = ((bt->brp - 1) & 0x3f) | (((bt->sjw - 1) & 0x3) << 6); | ||
193 | btr1 = ((bt->prop_seg + bt->phase_seg1 - 1) & 0xf) | | ||
194 | (((bt->phase_seg2 - 1) & 0x7) << 4); | ||
195 | if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES) | ||
196 | btr1 |= 0x80; | ||
197 | |||
198 | dev_info(dev->dev.parent, | ||
199 | "setting BTR0=0x%02x BTR1=0x%02x\n", btr0, btr1); | ||
200 | |||
201 | priv->write_reg(dev, REG_BTR0, btr0); | ||
202 | priv->write_reg(dev, REG_BTR1, btr1); | ||
203 | |||
204 | return 0; | ||
205 | } | ||
206 | |||
207 | /* | ||
208 | * initialize SJA1000 chip: | ||
209 | * - reset chip | ||
210 | * - set output mode | ||
211 | * - set baudrate | ||
212 | * - enable interrupts | ||
213 | * - start operating mode | ||
214 | */ | ||
215 | static void chipset_init(struct net_device *dev) | ||
216 | { | ||
217 | struct sja1000_priv *priv = netdev_priv(dev); | ||
218 | |||
219 | /* set clock divider and output control register */ | ||
220 | priv->write_reg(dev, REG_CDR, priv->cdr | CDR_PELICAN); | ||
221 | |||
222 | /* set acceptance filter (accept all) */ | ||
223 | priv->write_reg(dev, REG_ACCC0, 0x00); | ||
224 | priv->write_reg(dev, REG_ACCC1, 0x00); | ||
225 | priv->write_reg(dev, REG_ACCC2, 0x00); | ||
226 | priv->write_reg(dev, REG_ACCC3, 0x00); | ||
227 | |||
228 | priv->write_reg(dev, REG_ACCM0, 0xFF); | ||
229 | priv->write_reg(dev, REG_ACCM1, 0xFF); | ||
230 | priv->write_reg(dev, REG_ACCM2, 0xFF); | ||
231 | priv->write_reg(dev, REG_ACCM3, 0xFF); | ||
232 | |||
233 | priv->write_reg(dev, REG_OCR, priv->ocr | OCR_MODE_NORMAL); | ||
234 | } | ||
235 | |||
236 | /* | ||
237 | * transmit a CAN message | ||
238 | * message layout in the sk_buff should be like this: | ||
239 | * xx xx xx xx ff ll 00 11 22 33 44 55 66 77 | ||
240 | * [ can-id ] [flags] [len] [can data (up to 8 bytes] | ||
241 | */ | ||
242 | static int sja1000_start_xmit(struct sk_buff *skb, struct net_device *dev) | ||
243 | { | ||
244 | struct sja1000_priv *priv = netdev_priv(dev); | ||
245 | struct net_device_stats *stats = &dev->stats; | ||
246 | struct can_frame *cf = (struct can_frame *)skb->data; | ||
247 | uint8_t fi; | ||
248 | uint8_t dlc; | ||
249 | canid_t id; | ||
250 | uint8_t dreg; | ||
251 | int i; | ||
252 | |||
253 | netif_stop_queue(dev); | ||
254 | |||
255 | fi = dlc = cf->can_dlc; | ||
256 | id = cf->can_id; | ||
257 | |||
258 | if (id & CAN_RTR_FLAG) | ||
259 | fi |= FI_RTR; | ||
260 | |||
261 | if (id & CAN_EFF_FLAG) { | ||
262 | fi |= FI_FF; | ||
263 | dreg = EFF_BUF; | ||
264 | priv->write_reg(dev, REG_FI, fi); | ||
265 | priv->write_reg(dev, REG_ID1, (id & 0x1fe00000) >> (5 + 16)); | ||
266 | priv->write_reg(dev, REG_ID2, (id & 0x001fe000) >> (5 + 8)); | ||
267 | priv->write_reg(dev, REG_ID3, (id & 0x00001fe0) >> 5); | ||
268 | priv->write_reg(dev, REG_ID4, (id & 0x0000001f) << 3); | ||
269 | } else { | ||
270 | dreg = SFF_BUF; | ||
271 | priv->write_reg(dev, REG_FI, fi); | ||
272 | priv->write_reg(dev, REG_ID1, (id & 0x000007f8) >> 3); | ||
273 | priv->write_reg(dev, REG_ID2, (id & 0x00000007) << 5); | ||
274 | } | ||
275 | |||
276 | for (i = 0; i < dlc; i++) | ||
277 | priv->write_reg(dev, dreg++, cf->data[i]); | ||
278 | |||
279 | stats->tx_bytes += dlc; | ||
280 | dev->trans_start = jiffies; | ||
281 | |||
282 | can_put_echo_skb(skb, dev, 0); | ||
283 | |||
284 | priv->write_reg(dev, REG_CMR, CMD_TR); | ||
285 | |||
286 | return 0; | ||
287 | } | ||
288 | |||
289 | static void sja1000_rx(struct net_device *dev) | ||
290 | { | ||
291 | struct sja1000_priv *priv = netdev_priv(dev); | ||
292 | struct net_device_stats *stats = &dev->stats; | ||
293 | struct can_frame *cf; | ||
294 | struct sk_buff *skb; | ||
295 | uint8_t fi; | ||
296 | uint8_t dreg; | ||
297 | canid_t id; | ||
298 | uint8_t dlc; | ||
299 | int i; | ||
300 | |||
301 | skb = dev_alloc_skb(sizeof(struct can_frame)); | ||
302 | if (skb == NULL) | ||
303 | return; | ||
304 | skb->dev = dev; | ||
305 | skb->protocol = htons(ETH_P_CAN); | ||
306 | |||
307 | fi = priv->read_reg(dev, REG_FI); | ||
308 | dlc = fi & 0x0F; | ||
309 | |||
310 | if (fi & FI_FF) { | ||
311 | /* extended frame format (EFF) */ | ||
312 | dreg = EFF_BUF; | ||
313 | id = (priv->read_reg(dev, REG_ID1) << (5 + 16)) | ||
314 | | (priv->read_reg(dev, REG_ID2) << (5 + 8)) | ||
315 | | (priv->read_reg(dev, REG_ID3) << 5) | ||
316 | | (priv->read_reg(dev, REG_ID4) >> 3); | ||
317 | id |= CAN_EFF_FLAG; | ||
318 | } else { | ||
319 | /* standard frame format (SFF) */ | ||
320 | dreg = SFF_BUF; | ||
321 | id = (priv->read_reg(dev, REG_ID1) << 3) | ||
322 | | (priv->read_reg(dev, REG_ID2) >> 5); | ||
323 | } | ||
324 | |||
325 | if (fi & FI_RTR) | ||
326 | id |= CAN_RTR_FLAG; | ||
327 | |||
328 | cf = (struct can_frame *)skb_put(skb, sizeof(struct can_frame)); | ||
329 | memset(cf, 0, sizeof(struct can_frame)); | ||
330 | cf->can_id = id; | ||
331 | cf->can_dlc = dlc; | ||
332 | for (i = 0; i < dlc; i++) | ||
333 | cf->data[i] = priv->read_reg(dev, dreg++); | ||
334 | |||
335 | while (i < 8) | ||
336 | cf->data[i++] = 0; | ||
337 | |||
338 | /* release receive buffer */ | ||
339 | priv->write_reg(dev, REG_CMR, CMD_RRB); | ||
340 | |||
341 | netif_rx(skb); | ||
342 | |||
343 | dev->last_rx = jiffies; | ||
344 | stats->rx_packets++; | ||
345 | stats->rx_bytes += dlc; | ||
346 | } | ||
347 | |||
348 | static int sja1000_err(struct net_device *dev, uint8_t isrc, uint8_t status) | ||
349 | { | ||
350 | struct sja1000_priv *priv = netdev_priv(dev); | ||
351 | struct net_device_stats *stats = &dev->stats; | ||
352 | struct can_frame *cf; | ||
353 | struct sk_buff *skb; | ||
354 | enum can_state state = priv->can.state; | ||
355 | uint8_t ecc, alc; | ||
356 | |||
357 | skb = dev_alloc_skb(sizeof(struct can_frame)); | ||
358 | if (skb == NULL) | ||
359 | return -ENOMEM; | ||
360 | skb->dev = dev; | ||
361 | skb->protocol = htons(ETH_P_CAN); | ||
362 | cf = (struct can_frame *)skb_put(skb, sizeof(struct can_frame)); | ||
363 | memset(cf, 0, sizeof(struct can_frame)); | ||
364 | cf->can_id = CAN_ERR_FLAG; | ||
365 | cf->can_dlc = CAN_ERR_DLC; | ||
366 | |||
367 | if (isrc & IRQ_DOI) { | ||
368 | /* data overrun interrupt */ | ||
369 | dev_dbg(dev->dev.parent, "data overrun interrupt\n"); | ||
370 | cf->can_id |= CAN_ERR_CRTL; | ||
371 | cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW; | ||
372 | stats->rx_over_errors++; | ||
373 | stats->rx_errors++; | ||
374 | priv->write_reg(dev, REG_CMR, CMD_CDO); /* clear bit */ | ||
375 | } | ||
376 | |||
377 | if (isrc & IRQ_EI) { | ||
378 | /* error warning interrupt */ | ||
379 | dev_dbg(dev->dev.parent, "error warning interrupt\n"); | ||
380 | |||
381 | if (status & SR_BS) { | ||
382 | state = CAN_STATE_BUS_OFF; | ||
383 | cf->can_id |= CAN_ERR_BUSOFF; | ||
384 | can_bus_off(dev); | ||
385 | } else if (status & SR_ES) { | ||
386 | state = CAN_STATE_ERROR_WARNING; | ||
387 | } else | ||
388 | state = CAN_STATE_ERROR_ACTIVE; | ||
389 | } | ||
390 | if (isrc & IRQ_BEI) { | ||
391 | /* bus error interrupt */ | ||
392 | priv->can.can_stats.bus_error++; | ||
393 | stats->rx_errors++; | ||
394 | |||
395 | ecc = priv->read_reg(dev, REG_ECC); | ||
396 | |||
397 | cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR; | ||
398 | |||
399 | switch (ecc & ECC_MASK) { | ||
400 | case ECC_BIT: | ||
401 | cf->data[2] |= CAN_ERR_PROT_BIT; | ||
402 | break; | ||
403 | case ECC_FORM: | ||
404 | cf->data[2] |= CAN_ERR_PROT_FORM; | ||
405 | break; | ||
406 | case ECC_STUFF: | ||
407 | cf->data[2] |= CAN_ERR_PROT_STUFF; | ||
408 | break; | ||
409 | default: | ||
410 | cf->data[2] |= CAN_ERR_PROT_UNSPEC; | ||
411 | cf->data[3] = ecc & ECC_SEG; | ||
412 | break; | ||
413 | } | ||
414 | /* Error occured during transmission? */ | ||
415 | if ((ecc & ECC_DIR) == 0) | ||
416 | cf->data[2] |= CAN_ERR_PROT_TX; | ||
417 | } | ||
418 | if (isrc & IRQ_EPI) { | ||
419 | /* error passive interrupt */ | ||
420 | dev_dbg(dev->dev.parent, "error passive interrupt\n"); | ||
421 | if (status & SR_ES) | ||
422 | state = CAN_STATE_ERROR_PASSIVE; | ||
423 | else | ||
424 | state = CAN_STATE_ERROR_ACTIVE; | ||
425 | } | ||
426 | if (isrc & IRQ_ALI) { | ||
427 | /* arbitration lost interrupt */ | ||
428 | dev_dbg(dev->dev.parent, "arbitration lost interrupt\n"); | ||
429 | alc = priv->read_reg(dev, REG_ALC); | ||
430 | priv->can.can_stats.arbitration_lost++; | ||
431 | stats->rx_errors++; | ||
432 | cf->can_id |= CAN_ERR_LOSTARB; | ||
433 | cf->data[0] = alc & 0x1f; | ||
434 | } | ||
435 | |||
436 | if (state != priv->can.state && (state == CAN_STATE_ERROR_WARNING || | ||
437 | state == CAN_STATE_ERROR_PASSIVE)) { | ||
438 | uint8_t rxerr = priv->read_reg(dev, REG_RXERR); | ||
439 | uint8_t txerr = priv->read_reg(dev, REG_TXERR); | ||
440 | cf->can_id |= CAN_ERR_CRTL; | ||
441 | if (state == CAN_STATE_ERROR_WARNING) { | ||
442 | priv->can.can_stats.error_warning++; | ||
443 | cf->data[1] = (txerr > rxerr) ? | ||
444 | CAN_ERR_CRTL_TX_WARNING : | ||
445 | CAN_ERR_CRTL_RX_WARNING; | ||
446 | } else { | ||
447 | priv->can.can_stats.error_passive++; | ||
448 | cf->data[1] = (txerr > rxerr) ? | ||
449 | CAN_ERR_CRTL_TX_PASSIVE : | ||
450 | CAN_ERR_CRTL_RX_PASSIVE; | ||
451 | } | ||
452 | } | ||
453 | |||
454 | priv->can.state = state; | ||
455 | |||
456 | netif_rx(skb); | ||
457 | |||
458 | dev->last_rx = jiffies; | ||
459 | stats->rx_packets++; | ||
460 | stats->rx_bytes += cf->can_dlc; | ||
461 | |||
462 | return 0; | ||
463 | } | ||
464 | |||
465 | irqreturn_t sja1000_interrupt(int irq, void *dev_id) | ||
466 | { | ||
467 | struct net_device *dev = (struct net_device *)dev_id; | ||
468 | struct sja1000_priv *priv = netdev_priv(dev); | ||
469 | struct net_device_stats *stats = &dev->stats; | ||
470 | uint8_t isrc, status; | ||
471 | int n = 0; | ||
472 | |||
473 | /* Shared interrupts and IRQ off? */ | ||
474 | if (priv->read_reg(dev, REG_IER) == IRQ_OFF) | ||
475 | return IRQ_NONE; | ||
476 | |||
477 | if (priv->pre_irq) | ||
478 | priv->pre_irq(dev); | ||
479 | |||
480 | while ((isrc = priv->read_reg(dev, REG_IR)) && (n < SJA1000_MAX_IRQ)) { | ||
481 | n++; | ||
482 | status = priv->read_reg(dev, REG_SR); | ||
483 | |||
484 | if (isrc & IRQ_WUI) | ||
485 | dev_warn(dev->dev.parent, "wakeup interrupt\n"); | ||
486 | |||
487 | if (isrc & IRQ_TI) { | ||
488 | /* transmission complete interrupt */ | ||
489 | stats->tx_packets++; | ||
490 | can_get_echo_skb(dev, 0); | ||
491 | netif_wake_queue(dev); | ||
492 | } | ||
493 | if (isrc & IRQ_RI) { | ||
494 | /* receive interrupt */ | ||
495 | while (status & SR_RBS) { | ||
496 | sja1000_rx(dev); | ||
497 | status = priv->read_reg(dev, REG_SR); | ||
498 | } | ||
499 | } | ||
500 | if (isrc & (IRQ_DOI | IRQ_EI | IRQ_BEI | IRQ_EPI | IRQ_ALI)) { | ||
501 | /* error interrupt */ | ||
502 | if (sja1000_err(dev, isrc, status)) | ||
503 | break; | ||
504 | } | ||
505 | } | ||
506 | |||
507 | if (priv->post_irq) | ||
508 | priv->post_irq(dev); | ||
509 | |||
510 | if (n >= SJA1000_MAX_IRQ) | ||
511 | dev_dbg(dev->dev.parent, "%d messages handled in ISR", n); | ||
512 | |||
513 | return (n) ? IRQ_HANDLED : IRQ_NONE; | ||
514 | } | ||
515 | EXPORT_SYMBOL_GPL(sja1000_interrupt); | ||
516 | |||
517 | static int sja1000_open(struct net_device *dev) | ||
518 | { | ||
519 | struct sja1000_priv *priv = netdev_priv(dev); | ||
520 | int err; | ||
521 | |||
522 | /* set chip into reset mode */ | ||
523 | set_reset_mode(dev); | ||
524 | |||
525 | /* common open */ | ||
526 | err = open_candev(dev); | ||
527 | if (err) | ||
528 | return err; | ||
529 | |||
530 | /* register interrupt handler, if not done by the device driver */ | ||
531 | if (!(priv->flags & SJA1000_CUSTOM_IRQ_HANDLER)) { | ||
532 | err = request_irq(dev->irq, &sja1000_interrupt, priv->irq_flags, | ||
533 | dev->name, (void *)dev); | ||
534 | if (err) { | ||
535 | return -EAGAIN; | ||
536 | close_candev(dev); | ||
537 | } | ||
538 | } | ||
539 | |||
540 | /* init and start chi */ | ||
541 | sja1000_start(dev); | ||
542 | priv->open_time = jiffies; | ||
543 | |||
544 | netif_start_queue(dev); | ||
545 | |||
546 | return 0; | ||
547 | } | ||
548 | |||
549 | static int sja1000_close(struct net_device *dev) | ||
550 | { | ||
551 | struct sja1000_priv *priv = netdev_priv(dev); | ||
552 | |||
553 | netif_stop_queue(dev); | ||
554 | set_reset_mode(dev); | ||
555 | |||
556 | if (!(priv->flags & SJA1000_CUSTOM_IRQ_HANDLER)) | ||
557 | free_irq(dev->irq, (void *)dev); | ||
558 | |||
559 | close_candev(dev); | ||
560 | |||
561 | priv->open_time = 0; | ||
562 | |||
563 | return 0; | ||
564 | } | ||
565 | |||
566 | struct net_device *alloc_sja1000dev(int sizeof_priv) | ||
567 | { | ||
568 | struct net_device *dev; | ||
569 | struct sja1000_priv *priv; | ||
570 | |||
571 | dev = alloc_candev(sizeof(struct sja1000_priv) + sizeof_priv); | ||
572 | if (!dev) | ||
573 | return NULL; | ||
574 | |||
575 | priv = netdev_priv(dev); | ||
576 | |||
577 | priv->dev = dev; | ||
578 | priv->can.bittiming_const = &sja1000_bittiming_const; | ||
579 | priv->can.do_set_bittiming = sja1000_set_bittiming; | ||
580 | priv->can.do_set_mode = sja1000_set_mode; | ||
581 | |||
582 | if (sizeof_priv) | ||
583 | priv->priv = (void *)priv + sizeof(struct sja1000_priv); | ||
584 | |||
585 | return dev; | ||
586 | } | ||
587 | EXPORT_SYMBOL_GPL(alloc_sja1000dev); | ||
588 | |||
589 | void free_sja1000dev(struct net_device *dev) | ||
590 | { | ||
591 | free_candev(dev); | ||
592 | } | ||
593 | EXPORT_SYMBOL_GPL(free_sja1000dev); | ||
594 | |||
595 | static const struct net_device_ops sja1000_netdev_ops = { | ||
596 | .ndo_open = sja1000_open, | ||
597 | .ndo_stop = sja1000_close, | ||
598 | .ndo_start_xmit = sja1000_start_xmit, | ||
599 | }; | ||
600 | |||
601 | int register_sja1000dev(struct net_device *dev) | ||
602 | { | ||
603 | if (!sja1000_probe_chip(dev)) | ||
604 | return -ENODEV; | ||
605 | |||
606 | dev->flags |= IFF_ECHO; /* we support local echo */ | ||
607 | dev->netdev_ops = &sja1000_netdev_ops; | ||
608 | |||
609 | set_reset_mode(dev); | ||
610 | chipset_init(dev); | ||
611 | |||
612 | return register_candev(dev); | ||
613 | } | ||
614 | EXPORT_SYMBOL_GPL(register_sja1000dev); | ||
615 | |||
616 | void unregister_sja1000dev(struct net_device *dev) | ||
617 | { | ||
618 | set_reset_mode(dev); | ||
619 | unregister_candev(dev); | ||
620 | } | ||
621 | EXPORT_SYMBOL_GPL(unregister_sja1000dev); | ||
622 | |||
623 | static __init int sja1000_init(void) | ||
624 | { | ||
625 | printk(KERN_INFO "%s CAN netdevice driver\n", DRV_NAME); | ||
626 | |||
627 | return 0; | ||
628 | } | ||
629 | |||
630 | module_init(sja1000_init); | ||
631 | |||
632 | static __exit void sja1000_exit(void) | ||
633 | { | ||
634 | printk(KERN_INFO "%s: driver removed\n", DRV_NAME); | ||
635 | } | ||
636 | |||
637 | module_exit(sja1000_exit); | ||
diff --git a/drivers/net/can/sja1000/sja1000.h b/drivers/net/can/sja1000/sja1000.h new file mode 100644 index 000000000000..ccd302887964 --- /dev/null +++ b/drivers/net/can/sja1000/sja1000.h | |||
@@ -0,0 +1,180 @@ | |||
1 | /* | ||
2 | * sja1000.h - 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 | #ifndef SJA1000_DEV_H | ||
48 | #define SJA1000_DEV_H | ||
49 | |||
50 | #include <linux/can/dev.h> | ||
51 | #include <linux/can/platform/sja1000.h> | ||
52 | |||
53 | #define SJA1000_MAX_IRQ 20 /* max. number of interrupts handled in ISR */ | ||
54 | |||
55 | /* SJA1000 registers - manual section 6.4 (Pelican Mode) */ | ||
56 | #define REG_MOD 0x00 | ||
57 | #define REG_CMR 0x01 | ||
58 | #define REG_SR 0x02 | ||
59 | #define REG_IR 0x03 | ||
60 | #define REG_IER 0x04 | ||
61 | #define REG_ALC 0x0B | ||
62 | #define REG_ECC 0x0C | ||
63 | #define REG_EWL 0x0D | ||
64 | #define REG_RXERR 0x0E | ||
65 | #define REG_TXERR 0x0F | ||
66 | #define REG_ACCC0 0x10 | ||
67 | #define REG_ACCC1 0x11 | ||
68 | #define REG_ACCC2 0x12 | ||
69 | #define REG_ACCC3 0x13 | ||
70 | #define REG_ACCM0 0x14 | ||
71 | #define REG_ACCM1 0x15 | ||
72 | #define REG_ACCM2 0x16 | ||
73 | #define REG_ACCM3 0x17 | ||
74 | #define REG_RMC 0x1D | ||
75 | #define REG_RBSA 0x1E | ||
76 | |||
77 | /* Common registers - manual section 6.5 */ | ||
78 | #define REG_BTR0 0x06 | ||
79 | #define REG_BTR1 0x07 | ||
80 | #define REG_OCR 0x08 | ||
81 | #define REG_CDR 0x1F | ||
82 | |||
83 | #define REG_FI 0x10 | ||
84 | #define SFF_BUF 0x13 | ||
85 | #define EFF_BUF 0x15 | ||
86 | |||
87 | #define FI_FF 0x80 | ||
88 | #define FI_RTR 0x40 | ||
89 | |||
90 | #define REG_ID1 0x11 | ||
91 | #define REG_ID2 0x12 | ||
92 | #define REG_ID3 0x13 | ||
93 | #define REG_ID4 0x14 | ||
94 | |||
95 | #define CAN_RAM 0x20 | ||
96 | |||
97 | /* mode register */ | ||
98 | #define MOD_RM 0x01 | ||
99 | #define MOD_LOM 0x02 | ||
100 | #define MOD_STM 0x04 | ||
101 | #define MOD_AFM 0x08 | ||
102 | #define MOD_SM 0x10 | ||
103 | |||
104 | /* commands */ | ||
105 | #define CMD_SRR 0x10 | ||
106 | #define CMD_CDO 0x08 | ||
107 | #define CMD_RRB 0x04 | ||
108 | #define CMD_AT 0x02 | ||
109 | #define CMD_TR 0x01 | ||
110 | |||
111 | /* interrupt sources */ | ||
112 | #define IRQ_BEI 0x80 | ||
113 | #define IRQ_ALI 0x40 | ||
114 | #define IRQ_EPI 0x20 | ||
115 | #define IRQ_WUI 0x10 | ||
116 | #define IRQ_DOI 0x08 | ||
117 | #define IRQ_EI 0x04 | ||
118 | #define IRQ_TI 0x02 | ||
119 | #define IRQ_RI 0x01 | ||
120 | #define IRQ_ALL 0xFF | ||
121 | #define IRQ_OFF 0x00 | ||
122 | |||
123 | /* status register content */ | ||
124 | #define SR_BS 0x80 | ||
125 | #define SR_ES 0x40 | ||
126 | #define SR_TS 0x20 | ||
127 | #define SR_RS 0x10 | ||
128 | #define SR_TCS 0x08 | ||
129 | #define SR_TBS 0x04 | ||
130 | #define SR_DOS 0x02 | ||
131 | #define SR_RBS 0x01 | ||
132 | |||
133 | #define SR_CRIT (SR_BS|SR_ES) | ||
134 | |||
135 | /* ECC register */ | ||
136 | #define ECC_SEG 0x1F | ||
137 | #define ECC_DIR 0x20 | ||
138 | #define ECC_ERR 6 | ||
139 | #define ECC_BIT 0x00 | ||
140 | #define ECC_FORM 0x40 | ||
141 | #define ECC_STUFF 0x80 | ||
142 | #define ECC_MASK 0xc0 | ||
143 | |||
144 | /* | ||
145 | * Flags for sja1000priv.flags | ||
146 | */ | ||
147 | #define SJA1000_CUSTOM_IRQ_HANDLER 0x1 | ||
148 | |||
149 | /* | ||
150 | * SJA1000 private data structure | ||
151 | */ | ||
152 | struct sja1000_priv { | ||
153 | struct can_priv can; /* must be the first member */ | ||
154 | int open_time; | ||
155 | struct sk_buff *echo_skb; | ||
156 | |||
157 | /* the lower-layer is responsible for appropriate locking */ | ||
158 | u8 (*read_reg) (const struct net_device *dev, int reg); | ||
159 | void (*write_reg) (const struct net_device *dev, int reg, u8 val); | ||
160 | void (*pre_irq) (const struct net_device *dev); | ||
161 | void (*post_irq) (const struct net_device *dev); | ||
162 | |||
163 | void *priv; /* for board-specific data */ | ||
164 | struct net_device *dev; | ||
165 | |||
166 | unsigned long irq_flags; /* for request_irq() */ | ||
167 | |||
168 | u16 flags; /* custom mode flags */ | ||
169 | u8 ocr; /* output control register */ | ||
170 | u8 cdr; /* clock divider register */ | ||
171 | }; | ||
172 | |||
173 | struct net_device *alloc_sja1000dev(int sizeof_priv); | ||
174 | void free_sja1000dev(struct net_device *dev); | ||
175 | int register_sja1000dev(struct net_device *dev); | ||
176 | void unregister_sja1000dev(struct net_device *dev); | ||
177 | |||
178 | irqreturn_t sja1000_interrupt(int irq, void *dev_id); | ||
179 | |||
180 | #endif /* SJA1000_DEV_H */ | ||