]>
Commit | Line | Data |
---|---|---|
429da1cc WG |
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 | * | |
429da1cc WG |
43 | */ |
44 | ||
45 | #include <linux/module.h> | |
46 | #include <linux/init.h> | |
47 | #include <linux/kernel.h> | |
48 | #include <linux/sched.h> | |
49 | #include <linux/types.h> | |
50 | #include <linux/fcntl.h> | |
51 | #include <linux/interrupt.h> | |
52 | #include <linux/ptrace.h> | |
53 | #include <linux/string.h> | |
54 | #include <linux/errno.h> | |
55 | #include <linux/netdevice.h> | |
56 | #include <linux/if_arp.h> | |
57 | #include <linux/if_ether.h> | |
58 | #include <linux/skbuff.h> | |
59 | #include <linux/delay.h> | |
60 | ||
429da1cc WG |
61 | #include <linux/can/dev.h> |
62 | #include <linux/can/error.h> | |
a04282f5 | 63 | #include <linux/can/led.h> |
429da1cc WG |
64 | |
65 | #include "sja1000.h" | |
66 | ||
67 | #define DRV_NAME "sja1000" | |
68 | ||
69 | MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>"); | |
70 | MODULE_LICENSE("Dual BSD/GPL"); | |
71 | MODULE_DESCRIPTION(DRV_NAME "CAN netdevice driver"); | |
72 | ||
194b9a4c | 73 | static const struct can_bittiming_const sja1000_bittiming_const = { |
429da1cc WG |
74 | .name = DRV_NAME, |
75 | .tseg1_min = 1, | |
76 | .tseg1_max = 16, | |
77 | .tseg2_min = 1, | |
78 | .tseg2_max = 8, | |
79 | .sjw_max = 4, | |
80 | .brp_min = 1, | |
81 | .brp_max = 64, | |
82 | .brp_inc = 1, | |
83 | }; | |
84 | ||
57c8a456 OH |
85 | static void sja1000_write_cmdreg(struct sja1000_priv *priv, u8 val) |
86 | { | |
87 | unsigned long flags; | |
88 | ||
89 | /* | |
90 | * The command register needs some locking and time to settle | |
91 | * the write_reg() operation - especially on SMP systems. | |
92 | */ | |
93 | spin_lock_irqsave(&priv->cmdreg_lock, flags); | |
06e1d1d7 OH |
94 | priv->write_reg(priv, SJA1000_CMR, val); |
95 | priv->read_reg(priv, SJA1000_SR); | |
57c8a456 OH |
96 | spin_unlock_irqrestore(&priv->cmdreg_lock, flags); |
97 | } | |
98 | ||
a7762b10 OH |
99 | static int sja1000_is_absent(struct sja1000_priv *priv) |
100 | { | |
06e1d1d7 | 101 | return (priv->read_reg(priv, SJA1000_MOD) == 0xFF); |
a7762b10 OH |
102 | } |
103 | ||
429da1cc WG |
104 | static int sja1000_probe_chip(struct net_device *dev) |
105 | { | |
106 | struct sja1000_priv *priv = netdev_priv(dev); | |
107 | ||
a7762b10 | 108 | if (priv->reg_base && sja1000_is_absent(priv)) { |
909285c4 | 109 | netdev_err(dev, "probing failed\n"); |
429da1cc WG |
110 | return 0; |
111 | } | |
112 | return -1; | |
113 | } | |
114 | ||
115 | static void set_reset_mode(struct net_device *dev) | |
116 | { | |
117 | struct sja1000_priv *priv = netdev_priv(dev); | |
06e1d1d7 | 118 | unsigned char status = priv->read_reg(priv, SJA1000_MOD); |
429da1cc WG |
119 | int i; |
120 | ||
121 | /* disable interrupts */ | |
06e1d1d7 | 122 | priv->write_reg(priv, SJA1000_IER, IRQ_OFF); |
429da1cc WG |
123 | |
124 | for (i = 0; i < 100; i++) { | |
125 | /* check reset bit */ | |
126 | if (status & MOD_RM) { | |
127 | priv->can.state = CAN_STATE_STOPPED; | |
128 | return; | |
129 | } | |
130 | ||
06e1d1d7 OH |
131 | /* reset chip */ |
132 | priv->write_reg(priv, SJA1000_MOD, MOD_RM); | |
429da1cc | 133 | udelay(10); |
06e1d1d7 | 134 | status = priv->read_reg(priv, SJA1000_MOD); |
429da1cc WG |
135 | } |
136 | ||
aabdfd6a | 137 | netdev_err(dev, "setting SJA1000 into reset mode failed!\n"); |
429da1cc WG |
138 | } |
139 | ||
140 | static void set_normal_mode(struct net_device *dev) | |
141 | { | |
142 | struct sja1000_priv *priv = netdev_priv(dev); | |
06e1d1d7 | 143 | unsigned char status = priv->read_reg(priv, SJA1000_MOD); |
5b853ec3 | 144 | u8 mod_reg_val = 0x00; |
429da1cc WG |
145 | int i; |
146 | ||
147 | for (i = 0; i < 100; i++) { | |
148 | /* check reset bit */ | |
149 | if ((status & MOD_RM) == 0) { | |
150 | priv->can.state = CAN_STATE_ERROR_ACTIVE; | |
52c793f2 WG |
151 | /* enable interrupts */ |
152 | if (priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING) | |
06e1d1d7 | 153 | priv->write_reg(priv, SJA1000_IER, IRQ_ALL); |
52c793f2 | 154 | else |
06e1d1d7 | 155 | priv->write_reg(priv, SJA1000_IER, |
52c793f2 | 156 | IRQ_ALL & ~IRQ_BEI); |
429da1cc WG |
157 | return; |
158 | } | |
159 | ||
160 | /* set chip to normal mode */ | |
7146b2d9 | 161 | if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) |
5b853ec3 NEB |
162 | mod_reg_val |= MOD_LOM; |
163 | if (priv->can.ctrlmode & CAN_CTRLMODE_PRESUME_ACK) | |
164 | mod_reg_val |= MOD_STM; | |
165 | priv->write_reg(priv, SJA1000_MOD, mod_reg_val); | |
7146b2d9 | 166 | |
429da1cc | 167 | udelay(10); |
7146b2d9 | 168 | |
06e1d1d7 | 169 | status = priv->read_reg(priv, SJA1000_MOD); |
429da1cc WG |
170 | } |
171 | ||
aabdfd6a | 172 | netdev_err(dev, "setting SJA1000 into normal mode failed!\n"); |
429da1cc WG |
173 | } |
174 | ||
9e37bc6c MK |
175 | /* |
176 | * initialize SJA1000 chip: | |
177 | * - reset chip | |
178 | * - set output mode | |
179 | * - set baudrate | |
180 | * - enable interrupts | |
181 | * - start operating mode | |
182 | */ | |
183 | static void chipset_init(struct net_device *dev) | |
184 | { | |
185 | struct sja1000_priv *priv = netdev_priv(dev); | |
186 | ||
187 | /* set clock divider and output control register */ | |
188 | priv->write_reg(priv, SJA1000_CDR, priv->cdr | CDR_PELICAN); | |
189 | ||
190 | /* set acceptance filter (accept all) */ | |
191 | priv->write_reg(priv, SJA1000_ACCC0, 0x00); | |
192 | priv->write_reg(priv, SJA1000_ACCC1, 0x00); | |
193 | priv->write_reg(priv, SJA1000_ACCC2, 0x00); | |
194 | priv->write_reg(priv, SJA1000_ACCC3, 0x00); | |
195 | ||
196 | priv->write_reg(priv, SJA1000_ACCM0, 0xFF); | |
197 | priv->write_reg(priv, SJA1000_ACCM1, 0xFF); | |
198 | priv->write_reg(priv, SJA1000_ACCM2, 0xFF); | |
199 | priv->write_reg(priv, SJA1000_ACCM3, 0xFF); | |
200 | ||
201 | priv->write_reg(priv, SJA1000_OCR, priv->ocr | OCR_MODE_NORMAL); | |
202 | } | |
203 | ||
429da1cc WG |
204 | static void sja1000_start(struct net_device *dev) |
205 | { | |
206 | struct sja1000_priv *priv = netdev_priv(dev); | |
207 | ||
208 | /* leave reset mode */ | |
209 | if (priv->can.state != CAN_STATE_STOPPED) | |
210 | set_reset_mode(dev); | |
211 | ||
9e37bc6c MK |
212 | /* Initialize chip if uninitialized at this stage */ |
213 | if (!(priv->read_reg(priv, SJA1000_CDR) & CDR_PELICAN)) | |
214 | chipset_init(dev); | |
215 | ||
429da1cc | 216 | /* Clear error counters and error code capture */ |
06e1d1d7 OH |
217 | priv->write_reg(priv, SJA1000_TXERR, 0x0); |
218 | priv->write_reg(priv, SJA1000_RXERR, 0x0); | |
219 | priv->read_reg(priv, SJA1000_ECC); | |
429da1cc WG |
220 | |
221 | /* leave reset mode */ | |
222 | set_normal_mode(dev); | |
223 | } | |
224 | ||
225 | static int sja1000_set_mode(struct net_device *dev, enum can_mode mode) | |
226 | { | |
429da1cc WG |
227 | switch (mode) { |
228 | case CAN_MODE_START: | |
229 | sja1000_start(dev); | |
230 | if (netif_queue_stopped(dev)) | |
231 | netif_wake_queue(dev); | |
232 | break; | |
233 | ||
234 | default: | |
235 | return -EOPNOTSUPP; | |
236 | } | |
237 | ||
238 | return 0; | |
239 | } | |
240 | ||
241 | static int sja1000_set_bittiming(struct net_device *dev) | |
242 | { | |
243 | struct sja1000_priv *priv = netdev_priv(dev); | |
244 | struct can_bittiming *bt = &priv->can.bittiming; | |
245 | u8 btr0, btr1; | |
246 | ||
247 | btr0 = ((bt->brp - 1) & 0x3f) | (((bt->sjw - 1) & 0x3) << 6); | |
248 | btr1 = ((bt->prop_seg + bt->phase_seg1 - 1) & 0xf) | | |
249 | (((bt->phase_seg2 - 1) & 0x7) << 4); | |
250 | if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES) | |
251 | btr1 |= 0x80; | |
252 | ||
aabdfd6a | 253 | netdev_info(dev, "setting BTR0=0x%02x BTR1=0x%02x\n", btr0, btr1); |
429da1cc | 254 | |
06e1d1d7 OH |
255 | priv->write_reg(priv, SJA1000_BTR0, btr0); |
256 | priv->write_reg(priv, SJA1000_BTR1, btr1); | |
429da1cc WG |
257 | |
258 | return 0; | |
259 | } | |
260 | ||
52c793f2 WG |
261 | static int sja1000_get_berr_counter(const struct net_device *dev, |
262 | struct can_berr_counter *bec) | |
263 | { | |
264 | struct sja1000_priv *priv = netdev_priv(dev); | |
265 | ||
06e1d1d7 OH |
266 | bec->txerr = priv->read_reg(priv, SJA1000_TXERR); |
267 | bec->rxerr = priv->read_reg(priv, SJA1000_RXERR); | |
52c793f2 WG |
268 | |
269 | return 0; | |
270 | } | |
271 | ||
429da1cc WG |
272 | /* |
273 | * transmit a CAN message | |
274 | * message layout in the sk_buff should be like this: | |
275 | * xx xx xx xx ff ll 00 11 22 33 44 55 66 77 | |
276 | * [ can-id ] [flags] [len] [can data (up to 8 bytes] | |
277 | */ | |
61357325 SH |
278 | static netdev_tx_t sja1000_start_xmit(struct sk_buff *skb, |
279 | struct net_device *dev) | |
429da1cc WG |
280 | { |
281 | struct sja1000_priv *priv = netdev_priv(dev); | |
429da1cc WG |
282 | struct can_frame *cf = (struct can_frame *)skb->data; |
283 | uint8_t fi; | |
284 | uint8_t dlc; | |
285 | canid_t id; | |
286 | uint8_t dreg; | |
dcf9e152 | 287 | u8 cmd_reg_val = 0x00; |
429da1cc WG |
288 | int i; |
289 | ||
3ccd4c61 OH |
290 | if (can_dropped_invalid_skb(dev, skb)) |
291 | return NETDEV_TX_OK; | |
292 | ||
429da1cc WG |
293 | netif_stop_queue(dev); |
294 | ||
295 | fi = dlc = cf->can_dlc; | |
296 | id = cf->can_id; | |
297 | ||
298 | if (id & CAN_RTR_FLAG) | |
06e1d1d7 | 299 | fi |= SJA1000_FI_RTR; |
429da1cc WG |
300 | |
301 | if (id & CAN_EFF_FLAG) { | |
06e1d1d7 OH |
302 | fi |= SJA1000_FI_FF; |
303 | dreg = SJA1000_EFF_BUF; | |
304 | priv->write_reg(priv, SJA1000_FI, fi); | |
305 | priv->write_reg(priv, SJA1000_ID1, (id & 0x1fe00000) >> 21); | |
306 | priv->write_reg(priv, SJA1000_ID2, (id & 0x001fe000) >> 13); | |
307 | priv->write_reg(priv, SJA1000_ID3, (id & 0x00001fe0) >> 5); | |
308 | priv->write_reg(priv, SJA1000_ID4, (id & 0x0000001f) << 3); | |
429da1cc | 309 | } else { |
06e1d1d7 OH |
310 | dreg = SJA1000_SFF_BUF; |
311 | priv->write_reg(priv, SJA1000_FI, fi); | |
312 | priv->write_reg(priv, SJA1000_ID1, (id & 0x000007f8) >> 3); | |
313 | priv->write_reg(priv, SJA1000_ID2, (id & 0x00000007) << 5); | |
429da1cc WG |
314 | } |
315 | ||
316 | for (i = 0; i < dlc; i++) | |
255a9154 | 317 | priv->write_reg(priv, dreg++, cf->data[i]); |
429da1cc | 318 | |
429da1cc WG |
319 | can_put_echo_skb(skb, dev, 0); |
320 | ||
7146b2d9 | 321 | if (priv->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT) |
dcf9e152 NEB |
322 | cmd_reg_val |= CMD_AT; |
323 | ||
324 | if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK) | |
325 | cmd_reg_val |= CMD_SRR; | |
7146b2d9 | 326 | else |
dcf9e152 NEB |
327 | cmd_reg_val |= CMD_TR; |
328 | ||
329 | sja1000_write_cmdreg(priv, cmd_reg_val); | |
429da1cc | 330 | |
6ed10654 | 331 | return NETDEV_TX_OK; |
429da1cc WG |
332 | } |
333 | ||
334 | static void sja1000_rx(struct net_device *dev) | |
335 | { | |
336 | struct sja1000_priv *priv = netdev_priv(dev); | |
337 | struct net_device_stats *stats = &dev->stats; | |
338 | struct can_frame *cf; | |
339 | struct sk_buff *skb; | |
340 | uint8_t fi; | |
341 | uint8_t dreg; | |
342 | canid_t id; | |
429da1cc WG |
343 | int i; |
344 | ||
c7cd606f | 345 | /* create zero'ed CAN frame buffer */ |
7b6856a0 | 346 | skb = alloc_can_skb(dev, &cf); |
429da1cc WG |
347 | if (skb == NULL) |
348 | return; | |
429da1cc | 349 | |
06e1d1d7 | 350 | fi = priv->read_reg(priv, SJA1000_FI); |
429da1cc | 351 | |
06e1d1d7 | 352 | if (fi & SJA1000_FI_FF) { |
429da1cc | 353 | /* extended frame format (EFF) */ |
06e1d1d7 OH |
354 | dreg = SJA1000_EFF_BUF; |
355 | id = (priv->read_reg(priv, SJA1000_ID1) << 21) | |
356 | | (priv->read_reg(priv, SJA1000_ID2) << 13) | |
357 | | (priv->read_reg(priv, SJA1000_ID3) << 5) | |
358 | | (priv->read_reg(priv, SJA1000_ID4) >> 3); | |
429da1cc WG |
359 | id |= CAN_EFF_FLAG; |
360 | } else { | |
361 | /* standard frame format (SFF) */ | |
06e1d1d7 OH |
362 | dreg = SJA1000_SFF_BUF; |
363 | id = (priv->read_reg(priv, SJA1000_ID1) << 3) | |
364 | | (priv->read_reg(priv, SJA1000_ID2) >> 5); | |
429da1cc WG |
365 | } |
366 | ||
87e9af6c | 367 | cf->can_dlc = get_can_dlc(fi & 0x0F); |
06e1d1d7 | 368 | if (fi & SJA1000_FI_RTR) { |
429da1cc | 369 | id |= CAN_RTR_FLAG; |
c7cd606f | 370 | } else { |
c7cd606f OH |
371 | for (i = 0; i < cf->can_dlc; i++) |
372 | cf->data[i] = priv->read_reg(priv, dreg++); | |
373 | } | |
429da1cc | 374 | |
429da1cc | 375 | cf->can_id = id; |
429da1cc WG |
376 | |
377 | /* release receive buffer */ | |
57c8a456 | 378 | sja1000_write_cmdreg(priv, CMD_RRB); |
429da1cc | 379 | |
429da1cc | 380 | stats->rx_packets++; |
c7cd606f | 381 | stats->rx_bytes += cf->can_dlc; |
889dd06e | 382 | netif_rx(skb); |
a04282f5 FB |
383 | |
384 | can_led_event(dev, CAN_LED_EVENT_RX); | |
429da1cc WG |
385 | } |
386 | ||
387 | static int sja1000_err(struct net_device *dev, uint8_t isrc, uint8_t status) | |
388 | { | |
389 | struct sja1000_priv *priv = netdev_priv(dev); | |
390 | struct net_device_stats *stats = &dev->stats; | |
391 | struct can_frame *cf; | |
392 | struct sk_buff *skb; | |
393 | enum can_state state = priv->can.state; | |
215db185 AY |
394 | enum can_state rx_state, tx_state; |
395 | unsigned int rxerr, txerr; | |
429da1cc WG |
396 | uint8_t ecc, alc; |
397 | ||
7b6856a0 | 398 | skb = alloc_can_err_skb(dev, &cf); |
429da1cc WG |
399 | if (skb == NULL) |
400 | return -ENOMEM; | |
429da1cc | 401 | |
215db185 AY |
402 | txerr = priv->read_reg(priv, SJA1000_TXERR); |
403 | rxerr = priv->read_reg(priv, SJA1000_RXERR); | |
404 | ||
405 | cf->data[6] = txerr; | |
406 | cf->data[7] = rxerr; | |
407 | ||
429da1cc WG |
408 | if (isrc & IRQ_DOI) { |
409 | /* data overrun interrupt */ | |
aabdfd6a | 410 | netdev_dbg(dev, "data overrun interrupt\n"); |
429da1cc WG |
411 | cf->can_id |= CAN_ERR_CRTL; |
412 | cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW; | |
413 | stats->rx_over_errors++; | |
414 | stats->rx_errors++; | |
57c8a456 | 415 | sja1000_write_cmdreg(priv, CMD_CDO); /* clear bit */ |
429da1cc WG |
416 | } |
417 | ||
418 | if (isrc & IRQ_EI) { | |
419 | /* error warning interrupt */ | |
aabdfd6a | 420 | netdev_dbg(dev, "error warning interrupt\n"); |
429da1cc | 421 | |
215db185 | 422 | if (status & SR_BS) |
429da1cc | 423 | state = CAN_STATE_BUS_OFF; |
215db185 | 424 | else if (status & SR_ES) |
429da1cc | 425 | state = CAN_STATE_ERROR_WARNING; |
215db185 | 426 | else |
429da1cc WG |
427 | state = CAN_STATE_ERROR_ACTIVE; |
428 | } | |
429 | if (isrc & IRQ_BEI) { | |
430 | /* bus error interrupt */ | |
431 | priv->can.can_stats.bus_error++; | |
432 | stats->rx_errors++; | |
433 | ||
06e1d1d7 | 434 | ecc = priv->read_reg(priv, SJA1000_ECC); |
429da1cc WG |
435 | |
436 | cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR; | |
437 | ||
438 | switch (ecc & ECC_MASK) { | |
439 | case ECC_BIT: | |
440 | cf->data[2] |= CAN_ERR_PROT_BIT; | |
441 | break; | |
442 | case ECC_FORM: | |
443 | cf->data[2] |= CAN_ERR_PROT_FORM; | |
444 | break; | |
445 | case ECC_STUFF: | |
446 | cf->data[2] |= CAN_ERR_PROT_STUFF; | |
447 | break; | |
448 | default: | |
449 | cf->data[2] |= CAN_ERR_PROT_UNSPEC; | |
450 | cf->data[3] = ecc & ECC_SEG; | |
451 | break; | |
452 | } | |
25985edc | 453 | /* Error occurred during transmission? */ |
429da1cc WG |
454 | if ((ecc & ECC_DIR) == 0) |
455 | cf->data[2] |= CAN_ERR_PROT_TX; | |
456 | } | |
457 | if (isrc & IRQ_EPI) { | |
458 | /* error passive interrupt */ | |
aabdfd6a | 459 | netdev_dbg(dev, "error passive interrupt\n"); |
215db185 AY |
460 | |
461 | if (state == CAN_STATE_ERROR_PASSIVE) | |
462 | state = CAN_STATE_ERROR_WARNING; | |
429da1cc | 463 | else |
215db185 | 464 | state = CAN_STATE_ERROR_PASSIVE; |
429da1cc WG |
465 | } |
466 | if (isrc & IRQ_ALI) { | |
467 | /* arbitration lost interrupt */ | |
aabdfd6a | 468 | netdev_dbg(dev, "arbitration lost interrupt\n"); |
06e1d1d7 | 469 | alc = priv->read_reg(priv, SJA1000_ALC); |
429da1cc | 470 | priv->can.can_stats.arbitration_lost++; |
8935f57e | 471 | stats->tx_errors++; |
429da1cc WG |
472 | cf->can_id |= CAN_ERR_LOSTARB; |
473 | cf->data[0] = alc & 0x1f; | |
474 | } | |
475 | ||
215db185 AY |
476 | if (state != priv->can.state) { |
477 | tx_state = txerr >= rxerr ? state : 0; | |
478 | rx_state = txerr <= rxerr ? state : 0; | |
429da1cc | 479 | |
215db185 AY |
480 | can_change_state(dev, cf, tx_state, rx_state); |
481 | ||
482 | if(state == CAN_STATE_BUS_OFF) | |
483 | can_bus_off(dev); | |
484 | } | |
429da1cc | 485 | |
429da1cc WG |
486 | stats->rx_packets++; |
487 | stats->rx_bytes += cf->can_dlc; | |
889dd06e | 488 | netif_rx(skb); |
429da1cc WG |
489 | |
490 | return 0; | |
491 | } | |
492 | ||
493 | irqreturn_t sja1000_interrupt(int irq, void *dev_id) | |
494 | { | |
495 | struct net_device *dev = (struct net_device *)dev_id; | |
496 | struct sja1000_priv *priv = netdev_priv(dev); | |
497 | struct net_device_stats *stats = &dev->stats; | |
498 | uint8_t isrc, status; | |
499 | int n = 0; | |
500 | ||
429da1cc | 501 | if (priv->pre_irq) |
255a9154 | 502 | priv->pre_irq(priv); |
429da1cc | 503 | |
2fea6cd3 OH |
504 | /* Shared interrupts and IRQ off? */ |
505 | if (priv->read_reg(priv, SJA1000_IER) == IRQ_OFF) | |
506 | goto out; | |
507 | ||
06e1d1d7 OH |
508 | while ((isrc = priv->read_reg(priv, SJA1000_IR)) && |
509 | (n < SJA1000_MAX_IRQ)) { | |
2fea6cd3 | 510 | |
06e1d1d7 | 511 | status = priv->read_reg(priv, SJA1000_SR); |
a7762b10 OH |
512 | /* check for absent controller due to hw unplug */ |
513 | if (status == 0xFF && sja1000_is_absent(priv)) | |
2fea6cd3 | 514 | goto out; |
429da1cc WG |
515 | |
516 | if (isrc & IRQ_WUI) | |
aabdfd6a | 517 | netdev_warn(dev, "wakeup interrupt\n"); |
429da1cc WG |
518 | |
519 | if (isrc & IRQ_TI) { | |
7146b2d9 AL |
520 | /* transmission buffer released */ |
521 | if (priv->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT && | |
522 | !(status & SR_TCS)) { | |
523 | stats->tx_errors++; | |
524 | can_free_echo_skb(dev, 0); | |
525 | } else { | |
526 | /* transmission complete */ | |
527 | stats->tx_bytes += | |
06e1d1d7 | 528 | priv->read_reg(priv, SJA1000_FI) & 0xf; |
7146b2d9 AL |
529 | stats->tx_packets++; |
530 | can_get_echo_skb(dev, 0); | |
531 | } | |
429da1cc | 532 | netif_wake_queue(dev); |
a04282f5 | 533 | can_led_event(dev, CAN_LED_EVENT_TX); |
429da1cc WG |
534 | } |
535 | if (isrc & IRQ_RI) { | |
536 | /* receive interrupt */ | |
537 | while (status & SR_RBS) { | |
538 | sja1000_rx(dev); | |
06e1d1d7 | 539 | status = priv->read_reg(priv, SJA1000_SR); |
a7762b10 OH |
540 | /* check for absent controller */ |
541 | if (status == 0xFF && sja1000_is_absent(priv)) | |
2fea6cd3 | 542 | goto out; |
429da1cc WG |
543 | } |
544 | } | |
545 | if (isrc & (IRQ_DOI | IRQ_EI | IRQ_BEI | IRQ_EPI | IRQ_ALI)) { | |
546 | /* error interrupt */ | |
547 | if (sja1000_err(dev, isrc, status)) | |
548 | break; | |
549 | } | |
2fea6cd3 | 550 | n++; |
429da1cc | 551 | } |
2fea6cd3 | 552 | out: |
429da1cc | 553 | if (priv->post_irq) |
255a9154 | 554 | priv->post_irq(priv); |
429da1cc WG |
555 | |
556 | if (n >= SJA1000_MAX_IRQ) | |
aabdfd6a | 557 | netdev_dbg(dev, "%d messages handled in ISR", n); |
429da1cc WG |
558 | |
559 | return (n) ? IRQ_HANDLED : IRQ_NONE; | |
560 | } | |
561 | EXPORT_SYMBOL_GPL(sja1000_interrupt); | |
562 | ||
563 | static int sja1000_open(struct net_device *dev) | |
564 | { | |
565 | struct sja1000_priv *priv = netdev_priv(dev); | |
566 | int err; | |
567 | ||
568 | /* set chip into reset mode */ | |
569 | set_reset_mode(dev); | |
570 | ||
571 | /* common open */ | |
572 | err = open_candev(dev); | |
573 | if (err) | |
574 | return err; | |
575 | ||
576 | /* register interrupt handler, if not done by the device driver */ | |
577 | if (!(priv->flags & SJA1000_CUSTOM_IRQ_HANDLER)) { | |
4c9ba61e | 578 | err = request_irq(dev->irq, sja1000_interrupt, priv->irq_flags, |
429da1cc WG |
579 | dev->name, (void *)dev); |
580 | if (err) { | |
429da1cc | 581 | close_candev(dev); |
128ced8f | 582 | return -EAGAIN; |
429da1cc WG |
583 | } |
584 | } | |
585 | ||
586 | /* init and start chi */ | |
587 | sja1000_start(dev); | |
429da1cc | 588 | |
a04282f5 FB |
589 | can_led_event(dev, CAN_LED_EVENT_OPEN); |
590 | ||
429da1cc WG |
591 | netif_start_queue(dev); |
592 | ||
593 | return 0; | |
594 | } | |
595 | ||
596 | static int sja1000_close(struct net_device *dev) | |
597 | { | |
598 | struct sja1000_priv *priv = netdev_priv(dev); | |
599 | ||
600 | netif_stop_queue(dev); | |
601 | set_reset_mode(dev); | |
602 | ||
603 | if (!(priv->flags & SJA1000_CUSTOM_IRQ_HANDLER)) | |
604 | free_irq(dev->irq, (void *)dev); | |
605 | ||
606 | close_candev(dev); | |
607 | ||
a04282f5 FB |
608 | can_led_event(dev, CAN_LED_EVENT_STOP); |
609 | ||
429da1cc WG |
610 | return 0; |
611 | } | |
612 | ||
613 | struct net_device *alloc_sja1000dev(int sizeof_priv) | |
614 | { | |
615 | struct net_device *dev; | |
616 | struct sja1000_priv *priv; | |
617 | ||
a6e4bc53 WG |
618 | dev = alloc_candev(sizeof(struct sja1000_priv) + sizeof_priv, |
619 | SJA1000_ECHO_SKB_MAX); | |
429da1cc WG |
620 | if (!dev) |
621 | return NULL; | |
622 | ||
623 | priv = netdev_priv(dev); | |
624 | ||
625 | priv->dev = dev; | |
626 | priv->can.bittiming_const = &sja1000_bittiming_const; | |
627 | priv->can.do_set_bittiming = sja1000_set_bittiming; | |
628 | priv->can.do_set_mode = sja1000_set_mode; | |
52c793f2 | 629 | priv->can.do_get_berr_counter = sja1000_get_berr_counter; |
dcf9e152 NEB |
630 | priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK | |
631 | CAN_CTRLMODE_LISTENONLY | | |
632 | CAN_CTRLMODE_3_SAMPLES | | |
633 | CAN_CTRLMODE_ONE_SHOT | | |
5b853ec3 NEB |
634 | CAN_CTRLMODE_BERR_REPORTING | |
635 | CAN_CTRLMODE_PRESUME_ACK; | |
429da1cc | 636 | |
1f01bfd2 OH |
637 | spin_lock_init(&priv->cmdreg_lock); |
638 | ||
429da1cc WG |
639 | if (sizeof_priv) |
640 | priv->priv = (void *)priv + sizeof(struct sja1000_priv); | |
641 | ||
642 | return dev; | |
643 | } | |
644 | EXPORT_SYMBOL_GPL(alloc_sja1000dev); | |
645 | ||
646 | void free_sja1000dev(struct net_device *dev) | |
647 | { | |
648 | free_candev(dev); | |
649 | } | |
650 | EXPORT_SYMBOL_GPL(free_sja1000dev); | |
651 | ||
652 | static const struct net_device_ops sja1000_netdev_ops = { | |
c971fa2a OH |
653 | .ndo_open = sja1000_open, |
654 | .ndo_stop = sja1000_close, | |
655 | .ndo_start_xmit = sja1000_start_xmit, | |
656 | .ndo_change_mtu = can_change_mtu, | |
429da1cc WG |
657 | }; |
658 | ||
659 | int register_sja1000dev(struct net_device *dev) | |
660 | { | |
a04282f5 FB |
661 | int ret; |
662 | ||
429da1cc WG |
663 | if (!sja1000_probe_chip(dev)) |
664 | return -ENODEV; | |
665 | ||
666 | dev->flags |= IFF_ECHO; /* we support local echo */ | |
667 | dev->netdev_ops = &sja1000_netdev_ops; | |
668 | ||
669 | set_reset_mode(dev); | |
670 | chipset_init(dev); | |
671 | ||
a04282f5 FB |
672 | ret = register_candev(dev); |
673 | ||
674 | if (!ret) | |
675 | devm_can_led_init(dev); | |
676 | ||
677 | return ret; | |
429da1cc WG |
678 | } |
679 | EXPORT_SYMBOL_GPL(register_sja1000dev); | |
680 | ||
681 | void unregister_sja1000dev(struct net_device *dev) | |
682 | { | |
683 | set_reset_mode(dev); | |
684 | unregister_candev(dev); | |
685 | } | |
686 | EXPORT_SYMBOL_GPL(unregister_sja1000dev); | |
687 | ||
688 | static __init int sja1000_init(void) | |
689 | { | |
690 | printk(KERN_INFO "%s CAN netdevice driver\n", DRV_NAME); | |
691 | ||
692 | return 0; | |
693 | } | |
694 | ||
695 | module_init(sja1000_init); | |
696 | ||
697 | static __exit void sja1000_exit(void) | |
698 | { | |
699 | printk(KERN_INFO "%s: driver removed\n", DRV_NAME); | |
700 | } | |
701 | ||
702 | module_exit(sja1000_exit); |