iommu/exynos: Fix checkpatch warning
[deliverable/linux.git] / drivers / net / can / c_can / c_can.c
1 /*
2 * CAN bus driver for Bosch C_CAN controller
3 *
4 * Copyright (C) 2010 ST Microelectronics
5 * Bhupesh Sharma <bhupesh.sharma@st.com>
6 *
7 * Borrowed heavily from the C_CAN driver originally written by:
8 * Copyright (C) 2007
9 * - Sascha Hauer, Marc Kleine-Budde, Pengutronix <s.hauer@pengutronix.de>
10 * - Simon Kallweit, intefo AG <simon.kallweit@intefo.ch>
11 *
12 * TX and RX NAPI implementation has been borrowed from at91 CAN driver
13 * written by:
14 * Copyright
15 * (C) 2007 by Hans J. Koch <hjk@hansjkoch.de>
16 * (C) 2008, 2009 by Marc Kleine-Budde <kernel@pengutronix.de>
17 *
18 * Bosch C_CAN controller is compliant to CAN protocol version 2.0 part A and B.
19 * Bosch C_CAN user manual can be obtained from:
20 * http://www.semiconductors.bosch.de/media/en/pdf/ipmodules_1/c_can/
21 * users_manual_c_can.pdf
22 *
23 * This file is licensed under the terms of the GNU General Public
24 * License version 2. This program is licensed "as is" without any
25 * warranty of any kind, whether express or implied.
26 */
27
28 #include <linux/kernel.h>
29 #include <linux/module.h>
30 #include <linux/interrupt.h>
31 #include <linux/delay.h>
32 #include <linux/netdevice.h>
33 #include <linux/if_arp.h>
34 #include <linux/if_ether.h>
35 #include <linux/list.h>
36 #include <linux/io.h>
37 #include <linux/pm_runtime.h>
38
39 #include <linux/can.h>
40 #include <linux/can/dev.h>
41 #include <linux/can/error.h>
42 #include <linux/can/led.h>
43
44 #include "c_can.h"
45
46 /* Number of interface registers */
47 #define IF_ENUM_REG_LEN 11
48 #define C_CAN_IFACE(reg, iface) (C_CAN_IF1_##reg + (iface) * IF_ENUM_REG_LEN)
49
50 /* control extension register D_CAN specific */
51 #define CONTROL_EX_PDR BIT(8)
52
53 /* control register */
54 #define CONTROL_TEST BIT(7)
55 #define CONTROL_CCE BIT(6)
56 #define CONTROL_DISABLE_AR BIT(5)
57 #define CONTROL_ENABLE_AR (0 << 5)
58 #define CONTROL_EIE BIT(3)
59 #define CONTROL_SIE BIT(2)
60 #define CONTROL_IE BIT(1)
61 #define CONTROL_INIT BIT(0)
62
63 #define CONTROL_IRQMSK (CONTROL_EIE | CONTROL_IE | CONTROL_SIE)
64
65 /* test register */
66 #define TEST_RX BIT(7)
67 #define TEST_TX1 BIT(6)
68 #define TEST_TX2 BIT(5)
69 #define TEST_LBACK BIT(4)
70 #define TEST_SILENT BIT(3)
71 #define TEST_BASIC BIT(2)
72
73 /* status register */
74 #define STATUS_PDA BIT(10)
75 #define STATUS_BOFF BIT(7)
76 #define STATUS_EWARN BIT(6)
77 #define STATUS_EPASS BIT(5)
78 #define STATUS_RXOK BIT(4)
79 #define STATUS_TXOK BIT(3)
80
81 /* error counter register */
82 #define ERR_CNT_TEC_MASK 0xff
83 #define ERR_CNT_TEC_SHIFT 0
84 #define ERR_CNT_REC_SHIFT 8
85 #define ERR_CNT_REC_MASK (0x7f << ERR_CNT_REC_SHIFT)
86 #define ERR_CNT_RP_SHIFT 15
87 #define ERR_CNT_RP_MASK (0x1 << ERR_CNT_RP_SHIFT)
88
89 /* bit-timing register */
90 #define BTR_BRP_MASK 0x3f
91 #define BTR_BRP_SHIFT 0
92 #define BTR_SJW_SHIFT 6
93 #define BTR_SJW_MASK (0x3 << BTR_SJW_SHIFT)
94 #define BTR_TSEG1_SHIFT 8
95 #define BTR_TSEG1_MASK (0xf << BTR_TSEG1_SHIFT)
96 #define BTR_TSEG2_SHIFT 12
97 #define BTR_TSEG2_MASK (0x7 << BTR_TSEG2_SHIFT)
98
99 /* brp extension register */
100 #define BRP_EXT_BRPE_MASK 0x0f
101 #define BRP_EXT_BRPE_SHIFT 0
102
103 /* IFx command request */
104 #define IF_COMR_BUSY BIT(15)
105
106 /* IFx command mask */
107 #define IF_COMM_WR BIT(7)
108 #define IF_COMM_MASK BIT(6)
109 #define IF_COMM_ARB BIT(5)
110 #define IF_COMM_CONTROL BIT(4)
111 #define IF_COMM_CLR_INT_PND BIT(3)
112 #define IF_COMM_TXRQST BIT(2)
113 #define IF_COMM_CLR_NEWDAT IF_COMM_TXRQST
114 #define IF_COMM_DATAA BIT(1)
115 #define IF_COMM_DATAB BIT(0)
116
117 /* TX buffer setup */
118 #define IF_COMM_TX (IF_COMM_ARB | IF_COMM_CONTROL | \
119 IF_COMM_TXRQST | \
120 IF_COMM_DATAA | IF_COMM_DATAB)
121
122 /* For the low buffers we clear the interrupt bit, but keep newdat */
123 #define IF_COMM_RCV_LOW (IF_COMM_MASK | IF_COMM_ARB | \
124 IF_COMM_CONTROL | IF_COMM_CLR_INT_PND | \
125 IF_COMM_DATAA | IF_COMM_DATAB)
126
127 /* For the high buffers we clear the interrupt bit and newdat */
128 #define IF_COMM_RCV_HIGH (IF_COMM_RCV_LOW | IF_COMM_CLR_NEWDAT)
129
130
131 /* Receive setup of message objects */
132 #define IF_COMM_RCV_SETUP (IF_COMM_MASK | IF_COMM_ARB | IF_COMM_CONTROL)
133
134 /* Invalidation of message objects */
135 #define IF_COMM_INVAL (IF_COMM_ARB | IF_COMM_CONTROL)
136
137 /* IFx arbitration */
138 #define IF_ARB_MSGVAL BIT(31)
139 #define IF_ARB_MSGXTD BIT(30)
140 #define IF_ARB_TRANSMIT BIT(29)
141
142 /* IFx message control */
143 #define IF_MCONT_NEWDAT BIT(15)
144 #define IF_MCONT_MSGLST BIT(14)
145 #define IF_MCONT_INTPND BIT(13)
146 #define IF_MCONT_UMASK BIT(12)
147 #define IF_MCONT_TXIE BIT(11)
148 #define IF_MCONT_RXIE BIT(10)
149 #define IF_MCONT_RMTEN BIT(9)
150 #define IF_MCONT_TXRQST BIT(8)
151 #define IF_MCONT_EOB BIT(7)
152 #define IF_MCONT_DLC_MASK 0xf
153
154 #define IF_MCONT_RCV (IF_MCONT_RXIE | IF_MCONT_UMASK)
155 #define IF_MCONT_RCV_EOB (IF_MCONT_RCV | IF_MCONT_EOB)
156
157 #define IF_MCONT_TX (IF_MCONT_TXIE | IF_MCONT_EOB)
158
159 /*
160 * Use IF1 for RX and IF2 for TX
161 */
162 #define IF_RX 0
163 #define IF_TX 1
164
165 /* minimum timeout for checking BUSY status */
166 #define MIN_TIMEOUT_VALUE 6
167
168 /* Wait for ~1 sec for INIT bit */
169 #define INIT_WAIT_MS 1000
170
171 /* napi related */
172 #define C_CAN_NAPI_WEIGHT C_CAN_MSG_OBJ_RX_NUM
173
174 /* c_can lec values */
175 enum c_can_lec_type {
176 LEC_NO_ERROR = 0,
177 LEC_STUFF_ERROR,
178 LEC_FORM_ERROR,
179 LEC_ACK_ERROR,
180 LEC_BIT1_ERROR,
181 LEC_BIT0_ERROR,
182 LEC_CRC_ERROR,
183 LEC_UNUSED,
184 LEC_MASK = LEC_UNUSED,
185 };
186
187 /*
188 * c_can error types:
189 * Bus errors (BUS_OFF, ERROR_WARNING, ERROR_PASSIVE) are supported
190 */
191 enum c_can_bus_error_types {
192 C_CAN_NO_ERROR = 0,
193 C_CAN_BUS_OFF,
194 C_CAN_ERROR_WARNING,
195 C_CAN_ERROR_PASSIVE,
196 };
197
198 static const struct can_bittiming_const c_can_bittiming_const = {
199 .name = KBUILD_MODNAME,
200 .tseg1_min = 2, /* Time segment 1 = prop_seg + phase_seg1 */
201 .tseg1_max = 16,
202 .tseg2_min = 1, /* Time segment 2 = phase_seg2 */
203 .tseg2_max = 8,
204 .sjw_max = 4,
205 .brp_min = 1,
206 .brp_max = 1024, /* 6-bit BRP field + 4-bit BRPE field*/
207 .brp_inc = 1,
208 };
209
210 static inline void c_can_pm_runtime_enable(const struct c_can_priv *priv)
211 {
212 if (priv->device)
213 pm_runtime_enable(priv->device);
214 }
215
216 static inline void c_can_pm_runtime_disable(const struct c_can_priv *priv)
217 {
218 if (priv->device)
219 pm_runtime_disable(priv->device);
220 }
221
222 static inline void c_can_pm_runtime_get_sync(const struct c_can_priv *priv)
223 {
224 if (priv->device)
225 pm_runtime_get_sync(priv->device);
226 }
227
228 static inline void c_can_pm_runtime_put_sync(const struct c_can_priv *priv)
229 {
230 if (priv->device)
231 pm_runtime_put_sync(priv->device);
232 }
233
234 static inline void c_can_reset_ram(const struct c_can_priv *priv, bool enable)
235 {
236 if (priv->raminit)
237 priv->raminit(priv, enable);
238 }
239
240 static void c_can_irq_control(struct c_can_priv *priv, bool enable)
241 {
242 u32 ctrl = priv->read_reg(priv, C_CAN_CTRL_REG) & ~CONTROL_IRQMSK;
243
244 if (enable)
245 ctrl |= CONTROL_IRQMSK;
246
247 priv->write_reg(priv, C_CAN_CTRL_REG, ctrl);
248 }
249
250 static void c_can_obj_update(struct net_device *dev, int iface, u32 cmd, u32 obj)
251 {
252 struct c_can_priv *priv = netdev_priv(dev);
253 int cnt, reg = C_CAN_IFACE(COMREQ_REG, iface);
254
255 priv->write_reg(priv, reg + 1, cmd);
256 priv->write_reg(priv, reg, obj);
257
258 for (cnt = MIN_TIMEOUT_VALUE; cnt; cnt--) {
259 if (!(priv->read_reg(priv, reg) & IF_COMR_BUSY))
260 return;
261 udelay(1);
262 }
263 netdev_err(dev, "Updating object timed out\n");
264
265 }
266
267 static inline void c_can_object_get(struct net_device *dev, int iface,
268 u32 obj, u32 cmd)
269 {
270 c_can_obj_update(dev, iface, cmd, obj);
271 }
272
273 static inline void c_can_object_put(struct net_device *dev, int iface,
274 u32 obj, u32 cmd)
275 {
276 c_can_obj_update(dev, iface, cmd | IF_COMM_WR, obj);
277 }
278
279 /*
280 * Note: According to documentation clearing TXIE while MSGVAL is set
281 * is not allowed, but works nicely on C/DCAN. And that lowers the I/O
282 * load significantly.
283 */
284 static void c_can_inval_tx_object(struct net_device *dev, int iface, int obj)
285 {
286 struct c_can_priv *priv = netdev_priv(dev);
287
288 priv->write_reg(priv, C_CAN_IFACE(MSGCTRL_REG, iface), 0);
289 c_can_object_put(dev, iface, obj, IF_COMM_INVAL);
290 }
291
292 static void c_can_inval_msg_object(struct net_device *dev, int iface, int obj)
293 {
294 struct c_can_priv *priv = netdev_priv(dev);
295
296 priv->write_reg(priv, C_CAN_IFACE(ARB1_REG, iface), 0);
297 priv->write_reg(priv, C_CAN_IFACE(ARB2_REG, iface), 0);
298 c_can_inval_tx_object(dev, iface, obj);
299 }
300
301 static void c_can_setup_tx_object(struct net_device *dev, int iface,
302 struct can_frame *frame, int idx)
303 {
304 struct c_can_priv *priv = netdev_priv(dev);
305 u16 ctrl = IF_MCONT_TX | frame->can_dlc;
306 bool rtr = frame->can_id & CAN_RTR_FLAG;
307 u32 arb = IF_ARB_MSGVAL;
308 int i;
309
310 if (frame->can_id & CAN_EFF_FLAG) {
311 arb |= frame->can_id & CAN_EFF_MASK;
312 arb |= IF_ARB_MSGXTD;
313 } else {
314 arb |= (frame->can_id & CAN_SFF_MASK) << 18;
315 }
316
317 if (!rtr)
318 arb |= IF_ARB_TRANSMIT;
319
320 /*
321 * If we change the DIR bit, we need to invalidate the buffer
322 * first, i.e. clear the MSGVAL flag in the arbiter.
323 */
324 if (rtr != (bool)test_bit(idx, &priv->tx_dir)) {
325 u32 obj = idx + C_CAN_MSG_OBJ_TX_FIRST;
326
327 c_can_inval_msg_object(dev, iface, obj);
328 change_bit(idx, &priv->tx_dir);
329 }
330
331 priv->write_reg(priv, C_CAN_IFACE(ARB1_REG, iface), arb);
332 priv->write_reg(priv, C_CAN_IFACE(ARB2_REG, iface), arb >> 16);
333
334 priv->write_reg(priv, C_CAN_IFACE(MSGCTRL_REG, iface), ctrl);
335
336 for (i = 0; i < frame->can_dlc; i += 2) {
337 priv->write_reg(priv, C_CAN_IFACE(DATA1_REG, iface) + i / 2,
338 frame->data[i] | (frame->data[i + 1] << 8));
339 }
340 }
341
342 static inline void c_can_activate_all_lower_rx_msg_obj(struct net_device *dev,
343 int iface)
344 {
345 int i;
346
347 for (i = C_CAN_MSG_OBJ_RX_FIRST; i <= C_CAN_MSG_RX_LOW_LAST; i++)
348 c_can_object_get(dev, iface, i, IF_COMM_CLR_NEWDAT);
349 }
350
351 static int c_can_handle_lost_msg_obj(struct net_device *dev,
352 int iface, int objno, u32 ctrl)
353 {
354 struct net_device_stats *stats = &dev->stats;
355 struct c_can_priv *priv = netdev_priv(dev);
356 struct can_frame *frame;
357 struct sk_buff *skb;
358
359 ctrl &= ~(IF_MCONT_MSGLST | IF_MCONT_INTPND | IF_MCONT_NEWDAT);
360 priv->write_reg(priv, C_CAN_IFACE(MSGCTRL_REG, iface), ctrl);
361 c_can_object_put(dev, iface, objno, IF_COMM_CONTROL);
362
363 stats->rx_errors++;
364 stats->rx_over_errors++;
365
366 /* create an error msg */
367 skb = alloc_can_err_skb(dev, &frame);
368 if (unlikely(!skb))
369 return 0;
370
371 frame->can_id |= CAN_ERR_CRTL;
372 frame->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
373
374 netif_receive_skb(skb);
375 return 1;
376 }
377
378 static int c_can_read_msg_object(struct net_device *dev, int iface, u32 ctrl)
379 {
380 struct net_device_stats *stats = &dev->stats;
381 struct c_can_priv *priv = netdev_priv(dev);
382 struct can_frame *frame;
383 struct sk_buff *skb;
384 u32 arb, data;
385
386 skb = alloc_can_skb(dev, &frame);
387 if (!skb) {
388 stats->rx_dropped++;
389 return -ENOMEM;
390 }
391
392 frame->can_dlc = get_can_dlc(ctrl & 0x0F);
393
394 arb = priv->read_reg(priv, C_CAN_IFACE(ARB1_REG, iface));
395 arb |= priv->read_reg(priv, C_CAN_IFACE(ARB2_REG, iface)) << 16;
396
397 if (arb & IF_ARB_MSGXTD)
398 frame->can_id = (arb & CAN_EFF_MASK) | CAN_EFF_FLAG;
399 else
400 frame->can_id = (arb >> 18) & CAN_SFF_MASK;
401
402 if (arb & IF_ARB_TRANSMIT) {
403 frame->can_id |= CAN_RTR_FLAG;
404 } else {
405 int i, dreg = C_CAN_IFACE(DATA1_REG, iface);
406
407 for (i = 0; i < frame->can_dlc; i += 2, dreg ++) {
408 data = priv->read_reg(priv, dreg);
409 frame->data[i] = data;
410 frame->data[i + 1] = data >> 8;
411 }
412 }
413
414 stats->rx_packets++;
415 stats->rx_bytes += frame->can_dlc;
416
417 netif_receive_skb(skb);
418 return 0;
419 }
420
421 static void c_can_setup_receive_object(struct net_device *dev, int iface,
422 u32 obj, u32 mask, u32 id, u32 mcont)
423 {
424 struct c_can_priv *priv = netdev_priv(dev);
425
426 mask |= BIT(29);
427 priv->write_reg(priv, C_CAN_IFACE(MASK1_REG, iface), mask);
428 priv->write_reg(priv, C_CAN_IFACE(MASK2_REG, iface), mask >> 16);
429
430 id |= IF_ARB_MSGVAL;
431 priv->write_reg(priv, C_CAN_IFACE(ARB1_REG, iface), id);
432 priv->write_reg(priv, C_CAN_IFACE(ARB2_REG, iface), id >> 16);
433
434 priv->write_reg(priv, C_CAN_IFACE(MSGCTRL_REG, iface), mcont);
435 c_can_object_put(dev, iface, obj, IF_COMM_RCV_SETUP);
436 }
437
438 static netdev_tx_t c_can_start_xmit(struct sk_buff *skb,
439 struct net_device *dev)
440 {
441 struct can_frame *frame = (struct can_frame *)skb->data;
442 struct c_can_priv *priv = netdev_priv(dev);
443 u32 idx, obj;
444
445 if (can_dropped_invalid_skb(dev, skb))
446 return NETDEV_TX_OK;
447 /*
448 * This is not a FIFO. C/D_CAN sends out the buffers
449 * prioritized. The lowest buffer number wins.
450 */
451 idx = fls(atomic_read(&priv->tx_active));
452 obj = idx + C_CAN_MSG_OBJ_TX_FIRST;
453
454 /* If this is the last buffer, stop the xmit queue */
455 if (idx == C_CAN_MSG_OBJ_TX_NUM - 1)
456 netif_stop_queue(dev);
457 /*
458 * Store the message in the interface so we can call
459 * can_put_echo_skb(). We must do this before we enable
460 * transmit as we might race against do_tx().
461 */
462 c_can_setup_tx_object(dev, IF_TX, frame, idx);
463 priv->dlc[idx] = frame->can_dlc;
464 can_put_echo_skb(skb, dev, idx);
465
466 /* Update the active bits */
467 atomic_add((1 << idx), &priv->tx_active);
468 /* Start transmission */
469 c_can_object_put(dev, IF_TX, obj, IF_COMM_TX);
470
471 return NETDEV_TX_OK;
472 }
473
474 static int c_can_wait_for_ctrl_init(struct net_device *dev,
475 struct c_can_priv *priv, u32 init)
476 {
477 int retry = 0;
478
479 while (init != (priv->read_reg(priv, C_CAN_CTRL_REG) & CONTROL_INIT)) {
480 udelay(10);
481 if (retry++ > 1000) {
482 netdev_err(dev, "CCTRL: set CONTROL_INIT failed\n");
483 return -EIO;
484 }
485 }
486 return 0;
487 }
488
489 static int c_can_set_bittiming(struct net_device *dev)
490 {
491 unsigned int reg_btr, reg_brpe, ctrl_save;
492 u8 brp, brpe, sjw, tseg1, tseg2;
493 u32 ten_bit_brp;
494 struct c_can_priv *priv = netdev_priv(dev);
495 const struct can_bittiming *bt = &priv->can.bittiming;
496 int res;
497
498 /* c_can provides a 6-bit brp and 4-bit brpe fields */
499 ten_bit_brp = bt->brp - 1;
500 brp = ten_bit_brp & BTR_BRP_MASK;
501 brpe = ten_bit_brp >> 6;
502
503 sjw = bt->sjw - 1;
504 tseg1 = bt->prop_seg + bt->phase_seg1 - 1;
505 tseg2 = bt->phase_seg2 - 1;
506 reg_btr = brp | (sjw << BTR_SJW_SHIFT) | (tseg1 << BTR_TSEG1_SHIFT) |
507 (tseg2 << BTR_TSEG2_SHIFT);
508 reg_brpe = brpe & BRP_EXT_BRPE_MASK;
509
510 netdev_info(dev,
511 "setting BTR=%04x BRPE=%04x\n", reg_btr, reg_brpe);
512
513 ctrl_save = priv->read_reg(priv, C_CAN_CTRL_REG);
514 ctrl_save &= ~CONTROL_INIT;
515 priv->write_reg(priv, C_CAN_CTRL_REG, CONTROL_CCE | CONTROL_INIT);
516 res = c_can_wait_for_ctrl_init(dev, priv, CONTROL_INIT);
517 if (res)
518 return res;
519
520 priv->write_reg(priv, C_CAN_BTR_REG, reg_btr);
521 priv->write_reg(priv, C_CAN_BRPEXT_REG, reg_brpe);
522 priv->write_reg(priv, C_CAN_CTRL_REG, ctrl_save);
523
524 return c_can_wait_for_ctrl_init(dev, priv, 0);
525 }
526
527 /*
528 * Configure C_CAN message objects for Tx and Rx purposes:
529 * C_CAN provides a total of 32 message objects that can be configured
530 * either for Tx or Rx purposes. Here the first 16 message objects are used as
531 * a reception FIFO. The end of reception FIFO is signified by the EoB bit
532 * being SET. The remaining 16 message objects are kept aside for Tx purposes.
533 * See user guide document for further details on configuring message
534 * objects.
535 */
536 static void c_can_configure_msg_objects(struct net_device *dev)
537 {
538 int i;
539
540 /* first invalidate all message objects */
541 for (i = C_CAN_MSG_OBJ_RX_FIRST; i <= C_CAN_NO_OF_OBJECTS; i++)
542 c_can_inval_msg_object(dev, IF_RX, i);
543
544 /* setup receive message objects */
545 for (i = C_CAN_MSG_OBJ_RX_FIRST; i < C_CAN_MSG_OBJ_RX_LAST; i++)
546 c_can_setup_receive_object(dev, IF_RX, i, 0, 0, IF_MCONT_RCV);
547
548 c_can_setup_receive_object(dev, IF_RX, C_CAN_MSG_OBJ_RX_LAST, 0, 0,
549 IF_MCONT_RCV_EOB);
550 }
551
552 /*
553 * Configure C_CAN chip:
554 * - enable/disable auto-retransmission
555 * - set operating mode
556 * - configure message objects
557 */
558 static int c_can_chip_config(struct net_device *dev)
559 {
560 struct c_can_priv *priv = netdev_priv(dev);
561
562 /* enable automatic retransmission */
563 priv->write_reg(priv, C_CAN_CTRL_REG, CONTROL_ENABLE_AR);
564
565 if ((priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) &&
566 (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)) {
567 /* loopback + silent mode : useful for hot self-test */
568 priv->write_reg(priv, C_CAN_CTRL_REG, CONTROL_TEST);
569 priv->write_reg(priv, C_CAN_TEST_REG, TEST_LBACK | TEST_SILENT);
570 } else if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK) {
571 /* loopback mode : useful for self-test function */
572 priv->write_reg(priv, C_CAN_CTRL_REG, CONTROL_TEST);
573 priv->write_reg(priv, C_CAN_TEST_REG, TEST_LBACK);
574 } else if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) {
575 /* silent mode : bus-monitoring mode */
576 priv->write_reg(priv, C_CAN_CTRL_REG, CONTROL_TEST);
577 priv->write_reg(priv, C_CAN_TEST_REG, TEST_SILENT);
578 }
579
580 /* configure message objects */
581 c_can_configure_msg_objects(dev);
582
583 /* set a `lec` value so that we can check for updates later */
584 priv->write_reg(priv, C_CAN_STS_REG, LEC_UNUSED);
585
586 /* Clear all internal status */
587 atomic_set(&priv->tx_active, 0);
588 priv->rxmasked = 0;
589 priv->tx_dir = 0;
590
591 /* set bittiming params */
592 return c_can_set_bittiming(dev);
593 }
594
595 static int c_can_start(struct net_device *dev)
596 {
597 struct c_can_priv *priv = netdev_priv(dev);
598 int err;
599
600 /* basic c_can configuration */
601 err = c_can_chip_config(dev);
602 if (err)
603 return err;
604
605 /* Setup the command for new messages */
606 priv->comm_rcv_high = priv->type != BOSCH_D_CAN ?
607 IF_COMM_RCV_LOW : IF_COMM_RCV_HIGH;
608
609 priv->can.state = CAN_STATE_ERROR_ACTIVE;
610
611 return 0;
612 }
613
614 static void c_can_stop(struct net_device *dev)
615 {
616 struct c_can_priv *priv = netdev_priv(dev);
617
618 c_can_irq_control(priv, false);
619 priv->can.state = CAN_STATE_STOPPED;
620 }
621
622 static int c_can_set_mode(struct net_device *dev, enum can_mode mode)
623 {
624 struct c_can_priv *priv = netdev_priv(dev);
625 int err;
626
627 switch (mode) {
628 case CAN_MODE_START:
629 err = c_can_start(dev);
630 if (err)
631 return err;
632 netif_wake_queue(dev);
633 c_can_irq_control(priv, true);
634 break;
635 default:
636 return -EOPNOTSUPP;
637 }
638
639 return 0;
640 }
641
642 static int __c_can_get_berr_counter(const struct net_device *dev,
643 struct can_berr_counter *bec)
644 {
645 unsigned int reg_err_counter;
646 struct c_can_priv *priv = netdev_priv(dev);
647
648 reg_err_counter = priv->read_reg(priv, C_CAN_ERR_CNT_REG);
649 bec->rxerr = (reg_err_counter & ERR_CNT_REC_MASK) >>
650 ERR_CNT_REC_SHIFT;
651 bec->txerr = reg_err_counter & ERR_CNT_TEC_MASK;
652
653 return 0;
654 }
655
656 static int c_can_get_berr_counter(const struct net_device *dev,
657 struct can_berr_counter *bec)
658 {
659 struct c_can_priv *priv = netdev_priv(dev);
660 int err;
661
662 c_can_pm_runtime_get_sync(priv);
663 err = __c_can_get_berr_counter(dev, bec);
664 c_can_pm_runtime_put_sync(priv);
665
666 return err;
667 }
668
669 static void c_can_do_tx(struct net_device *dev)
670 {
671 struct c_can_priv *priv = netdev_priv(dev);
672 struct net_device_stats *stats = &dev->stats;
673 u32 idx, obj, pkts = 0, bytes = 0, pend, clr;
674
675 clr = pend = priv->read_reg(priv, C_CAN_INTPND2_REG);
676
677 while ((idx = ffs(pend))) {
678 idx--;
679 pend &= ~(1 << idx);
680 obj = idx + C_CAN_MSG_OBJ_TX_FIRST;
681 c_can_inval_tx_object(dev, IF_RX, obj);
682 can_get_echo_skb(dev, idx);
683 bytes += priv->dlc[idx];
684 pkts++;
685 }
686
687 /* Clear the bits in the tx_active mask */
688 atomic_sub(clr, &priv->tx_active);
689
690 if (clr & (1 << (C_CAN_MSG_OBJ_TX_NUM - 1)))
691 netif_wake_queue(dev);
692
693 if (pkts) {
694 stats->tx_bytes += bytes;
695 stats->tx_packets += pkts;
696 can_led_event(dev, CAN_LED_EVENT_TX);
697 }
698 }
699
700 /*
701 * If we have a gap in the pending bits, that means we either
702 * raced with the hardware or failed to readout all upper
703 * objects in the last run due to quota limit.
704 */
705 static u32 c_can_adjust_pending(u32 pend)
706 {
707 u32 weight, lasts;
708
709 if (pend == RECEIVE_OBJECT_BITS)
710 return pend;
711
712 /*
713 * If the last set bit is larger than the number of pending
714 * bits we have a gap.
715 */
716 weight = hweight32(pend);
717 lasts = fls(pend);
718
719 /* If the bits are linear, nothing to do */
720 if (lasts == weight)
721 return pend;
722
723 /*
724 * Find the first set bit after the gap. We walk backwards
725 * from the last set bit.
726 */
727 for (lasts--; pend & (1 << (lasts - 1)); lasts--);
728
729 return pend & ~((1 << lasts) - 1);
730 }
731
732 static inline void c_can_rx_object_get(struct net_device *dev,
733 struct c_can_priv *priv, u32 obj)
734 {
735 #ifdef CONFIG_CAN_C_CAN_STRICT_FRAME_ORDERING
736 if (obj < C_CAN_MSG_RX_LOW_LAST)
737 c_can_object_get(dev, IF_RX, obj, IF_COMM_RCV_LOW);
738 else
739 #endif
740 c_can_object_get(dev, IF_RX, obj, priv->comm_rcv_high);
741 }
742
743 static inline void c_can_rx_finalize(struct net_device *dev,
744 struct c_can_priv *priv, u32 obj)
745 {
746 #ifdef CONFIG_CAN_C_CAN_STRICT_FRAME_ORDERING
747 if (obj < C_CAN_MSG_RX_LOW_LAST)
748 priv->rxmasked |= BIT(obj - 1);
749 else if (obj == C_CAN_MSG_RX_LOW_LAST) {
750 priv->rxmasked = 0;
751 /* activate all lower message objects */
752 c_can_activate_all_lower_rx_msg_obj(dev, IF_RX);
753 }
754 #endif
755 if (priv->type != BOSCH_D_CAN)
756 c_can_object_get(dev, IF_RX, obj, IF_COMM_CLR_NEWDAT);
757 }
758
759 static int c_can_read_objects(struct net_device *dev, struct c_can_priv *priv,
760 u32 pend, int quota)
761 {
762 u32 pkts = 0, ctrl, obj;
763
764 while ((obj = ffs(pend)) && quota > 0) {
765 pend &= ~BIT(obj - 1);
766
767 c_can_rx_object_get(dev, priv, obj);
768 ctrl = priv->read_reg(priv, C_CAN_IFACE(MSGCTRL_REG, IF_RX));
769
770 if (ctrl & IF_MCONT_MSGLST) {
771 int n = c_can_handle_lost_msg_obj(dev, IF_RX, obj, ctrl);
772
773 pkts += n;
774 quota -= n;
775 continue;
776 }
777
778 /*
779 * This really should not happen, but this covers some
780 * odd HW behaviour. Do not remove that unless you
781 * want to brick your machine.
782 */
783 if (!(ctrl & IF_MCONT_NEWDAT))
784 continue;
785
786 /* read the data from the message object */
787 c_can_read_msg_object(dev, IF_RX, ctrl);
788
789 c_can_rx_finalize(dev, priv, obj);
790
791 pkts++;
792 quota--;
793 }
794
795 return pkts;
796 }
797
798 static inline u32 c_can_get_pending(struct c_can_priv *priv)
799 {
800 u32 pend = priv->read_reg(priv, C_CAN_NEWDAT1_REG);
801
802 #ifdef CONFIG_CAN_C_CAN_STRICT_FRAME_ORDERING
803 pend &= ~priv->rxmasked;
804 #endif
805 return pend;
806 }
807
808 /*
809 * theory of operation:
810 *
811 * c_can core saves a received CAN message into the first free message
812 * object it finds free (starting with the lowest). Bits NEWDAT and
813 * INTPND are set for this message object indicating that a new message
814 * has arrived. To work-around this issue, we keep two groups of message
815 * objects whose partitioning is defined by C_CAN_MSG_OBJ_RX_SPLIT.
816 *
817 * If CONFIG_CAN_C_CAN_STRICT_FRAME_ORDERING = y
818 *
819 * To ensure in-order frame reception we use the following
820 * approach while re-activating a message object to receive further
821 * frames:
822 * - if the current message object number is lower than
823 * C_CAN_MSG_RX_LOW_LAST, do not clear the NEWDAT bit while clearing
824 * the INTPND bit.
825 * - if the current message object number is equal to
826 * C_CAN_MSG_RX_LOW_LAST then clear the NEWDAT bit of all lower
827 * receive message objects.
828 * - if the current message object number is greater than
829 * C_CAN_MSG_RX_LOW_LAST then clear the NEWDAT bit of
830 * only this message object.
831 *
832 * This can cause packet loss!
833 *
834 * If CONFIG_CAN_C_CAN_STRICT_FRAME_ORDERING = n
835 *
836 * We clear the newdat bit right away.
837 *
838 * This can result in packet reordering when the readout is slow.
839 */
840 static int c_can_do_rx_poll(struct net_device *dev, int quota)
841 {
842 struct c_can_priv *priv = netdev_priv(dev);
843 u32 pkts = 0, pend = 0, toread, n;
844
845 /*
846 * It is faster to read only one 16bit register. This is only possible
847 * for a maximum number of 16 objects.
848 */
849 BUILD_BUG_ON_MSG(C_CAN_MSG_OBJ_RX_LAST > 16,
850 "Implementation does not support more message objects than 16");
851
852 while (quota > 0) {
853 if (!pend) {
854 pend = c_can_get_pending(priv);
855 if (!pend)
856 break;
857 /*
858 * If the pending field has a gap, handle the
859 * bits above the gap first.
860 */
861 toread = c_can_adjust_pending(pend);
862 } else {
863 toread = pend;
864 }
865 /* Remove the bits from pend */
866 pend &= ~toread;
867 /* Read the objects */
868 n = c_can_read_objects(dev, priv, toread, quota);
869 pkts += n;
870 quota -= n;
871 }
872
873 if (pkts)
874 can_led_event(dev, CAN_LED_EVENT_RX);
875
876 return pkts;
877 }
878
879 static int c_can_handle_state_change(struct net_device *dev,
880 enum c_can_bus_error_types error_type)
881 {
882 unsigned int reg_err_counter;
883 unsigned int rx_err_passive;
884 struct c_can_priv *priv = netdev_priv(dev);
885 struct net_device_stats *stats = &dev->stats;
886 struct can_frame *cf;
887 struct sk_buff *skb;
888 struct can_berr_counter bec;
889
890 switch (error_type) {
891 case C_CAN_ERROR_WARNING:
892 /* error warning state */
893 priv->can.can_stats.error_warning++;
894 priv->can.state = CAN_STATE_ERROR_WARNING;
895 break;
896 case C_CAN_ERROR_PASSIVE:
897 /* error passive state */
898 priv->can.can_stats.error_passive++;
899 priv->can.state = CAN_STATE_ERROR_PASSIVE;
900 break;
901 case C_CAN_BUS_OFF:
902 /* bus-off state */
903 priv->can.state = CAN_STATE_BUS_OFF;
904 can_bus_off(dev);
905 break;
906 default:
907 break;
908 }
909
910 /* propagate the error condition to the CAN stack */
911 skb = alloc_can_err_skb(dev, &cf);
912 if (unlikely(!skb))
913 return 0;
914
915 __c_can_get_berr_counter(dev, &bec);
916 reg_err_counter = priv->read_reg(priv, C_CAN_ERR_CNT_REG);
917 rx_err_passive = (reg_err_counter & ERR_CNT_RP_MASK) >>
918 ERR_CNT_RP_SHIFT;
919
920 switch (error_type) {
921 case C_CAN_ERROR_WARNING:
922 /* error warning state */
923 cf->can_id |= CAN_ERR_CRTL;
924 cf->data[1] = (bec.txerr > bec.rxerr) ?
925 CAN_ERR_CRTL_TX_WARNING :
926 CAN_ERR_CRTL_RX_WARNING;
927 cf->data[6] = bec.txerr;
928 cf->data[7] = bec.rxerr;
929
930 break;
931 case C_CAN_ERROR_PASSIVE:
932 /* error passive state */
933 cf->can_id |= CAN_ERR_CRTL;
934 if (rx_err_passive)
935 cf->data[1] |= CAN_ERR_CRTL_RX_PASSIVE;
936 if (bec.txerr > 127)
937 cf->data[1] |= CAN_ERR_CRTL_TX_PASSIVE;
938
939 cf->data[6] = bec.txerr;
940 cf->data[7] = bec.rxerr;
941 break;
942 case C_CAN_BUS_OFF:
943 /* bus-off state */
944 cf->can_id |= CAN_ERR_BUSOFF;
945 can_bus_off(dev);
946 break;
947 default:
948 break;
949 }
950
951 stats->rx_packets++;
952 stats->rx_bytes += cf->can_dlc;
953 netif_receive_skb(skb);
954
955 return 1;
956 }
957
958 static int c_can_handle_bus_err(struct net_device *dev,
959 enum c_can_lec_type lec_type)
960 {
961 struct c_can_priv *priv = netdev_priv(dev);
962 struct net_device_stats *stats = &dev->stats;
963 struct can_frame *cf;
964 struct sk_buff *skb;
965
966 /*
967 * early exit if no lec update or no error.
968 * no lec update means that no CAN bus event has been detected
969 * since CPU wrote 0x7 value to status reg.
970 */
971 if (lec_type == LEC_UNUSED || lec_type == LEC_NO_ERROR)
972 return 0;
973
974 if (!(priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING))
975 return 0;
976
977 /* common for all type of bus errors */
978 priv->can.can_stats.bus_error++;
979 stats->rx_errors++;
980
981 /* propagate the error condition to the CAN stack */
982 skb = alloc_can_err_skb(dev, &cf);
983 if (unlikely(!skb))
984 return 0;
985
986 /*
987 * check for 'last error code' which tells us the
988 * type of the last error to occur on the CAN bus
989 */
990 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
991 cf->data[2] |= CAN_ERR_PROT_UNSPEC;
992
993 switch (lec_type) {
994 case LEC_STUFF_ERROR:
995 netdev_dbg(dev, "stuff error\n");
996 cf->data[2] |= CAN_ERR_PROT_STUFF;
997 break;
998 case LEC_FORM_ERROR:
999 netdev_dbg(dev, "form error\n");
1000 cf->data[2] |= CAN_ERR_PROT_FORM;
1001 break;
1002 case LEC_ACK_ERROR:
1003 netdev_dbg(dev, "ack error\n");
1004 cf->data[3] |= (CAN_ERR_PROT_LOC_ACK |
1005 CAN_ERR_PROT_LOC_ACK_DEL);
1006 break;
1007 case LEC_BIT1_ERROR:
1008 netdev_dbg(dev, "bit1 error\n");
1009 cf->data[2] |= CAN_ERR_PROT_BIT1;
1010 break;
1011 case LEC_BIT0_ERROR:
1012 netdev_dbg(dev, "bit0 error\n");
1013 cf->data[2] |= CAN_ERR_PROT_BIT0;
1014 break;
1015 case LEC_CRC_ERROR:
1016 netdev_dbg(dev, "CRC error\n");
1017 cf->data[3] |= (CAN_ERR_PROT_LOC_CRC_SEQ |
1018 CAN_ERR_PROT_LOC_CRC_DEL);
1019 break;
1020 default:
1021 break;
1022 }
1023
1024 stats->rx_packets++;
1025 stats->rx_bytes += cf->can_dlc;
1026 netif_receive_skb(skb);
1027 return 1;
1028 }
1029
1030 static int c_can_poll(struct napi_struct *napi, int quota)
1031 {
1032 struct net_device *dev = napi->dev;
1033 struct c_can_priv *priv = netdev_priv(dev);
1034 u16 curr, last = priv->last_status;
1035 int work_done = 0;
1036
1037 priv->last_status = curr = priv->read_reg(priv, C_CAN_STS_REG);
1038 /* Ack status on C_CAN. D_CAN is self clearing */
1039 if (priv->type != BOSCH_D_CAN)
1040 priv->write_reg(priv, C_CAN_STS_REG, LEC_UNUSED);
1041
1042 /* handle state changes */
1043 if ((curr & STATUS_EWARN) && (!(last & STATUS_EWARN))) {
1044 netdev_dbg(dev, "entered error warning state\n");
1045 work_done += c_can_handle_state_change(dev, C_CAN_ERROR_WARNING);
1046 }
1047
1048 if ((curr & STATUS_EPASS) && (!(last & STATUS_EPASS))) {
1049 netdev_dbg(dev, "entered error passive state\n");
1050 work_done += c_can_handle_state_change(dev, C_CAN_ERROR_PASSIVE);
1051 }
1052
1053 if ((curr & STATUS_BOFF) && (!(last & STATUS_BOFF))) {
1054 netdev_dbg(dev, "entered bus off state\n");
1055 work_done += c_can_handle_state_change(dev, C_CAN_BUS_OFF);
1056 goto end;
1057 }
1058
1059 /* handle bus recovery events */
1060 if ((!(curr & STATUS_BOFF)) && (last & STATUS_BOFF)) {
1061 netdev_dbg(dev, "left bus off state\n");
1062 priv->can.state = CAN_STATE_ERROR_ACTIVE;
1063 }
1064 if ((!(curr & STATUS_EPASS)) && (last & STATUS_EPASS)) {
1065 netdev_dbg(dev, "left error passive state\n");
1066 priv->can.state = CAN_STATE_ERROR_ACTIVE;
1067 }
1068
1069 /* handle lec errors on the bus */
1070 work_done += c_can_handle_bus_err(dev, curr & LEC_MASK);
1071
1072 /* Handle Tx/Rx events. We do this unconditionally */
1073 work_done += c_can_do_rx_poll(dev, (quota - work_done));
1074 c_can_do_tx(dev);
1075
1076 end:
1077 if (work_done < quota) {
1078 napi_complete(napi);
1079 /* enable all IRQs if we are not in bus off state */
1080 if (priv->can.state != CAN_STATE_BUS_OFF)
1081 c_can_irq_control(priv, true);
1082 }
1083
1084 return work_done;
1085 }
1086
1087 static irqreturn_t c_can_isr(int irq, void *dev_id)
1088 {
1089 struct net_device *dev = (struct net_device *)dev_id;
1090 struct c_can_priv *priv = netdev_priv(dev);
1091
1092 if (!priv->read_reg(priv, C_CAN_INT_REG))
1093 return IRQ_NONE;
1094
1095 /* disable all interrupts and schedule the NAPI */
1096 c_can_irq_control(priv, false);
1097 napi_schedule(&priv->napi);
1098
1099 return IRQ_HANDLED;
1100 }
1101
1102 static int c_can_open(struct net_device *dev)
1103 {
1104 int err;
1105 struct c_can_priv *priv = netdev_priv(dev);
1106
1107 c_can_pm_runtime_get_sync(priv);
1108 c_can_reset_ram(priv, true);
1109
1110 /* open the can device */
1111 err = open_candev(dev);
1112 if (err) {
1113 netdev_err(dev, "failed to open can device\n");
1114 goto exit_open_fail;
1115 }
1116
1117 /* register interrupt handler */
1118 err = request_irq(dev->irq, &c_can_isr, IRQF_SHARED, dev->name,
1119 dev);
1120 if (err < 0) {
1121 netdev_err(dev, "failed to request interrupt\n");
1122 goto exit_irq_fail;
1123 }
1124
1125 /* start the c_can controller */
1126 err = c_can_start(dev);
1127 if (err)
1128 goto exit_start_fail;
1129
1130 can_led_event(dev, CAN_LED_EVENT_OPEN);
1131
1132 napi_enable(&priv->napi);
1133 /* enable status change, error and module interrupts */
1134 c_can_irq_control(priv, true);
1135 netif_start_queue(dev);
1136
1137 return 0;
1138
1139 exit_start_fail:
1140 free_irq(dev->irq, dev);
1141 exit_irq_fail:
1142 close_candev(dev);
1143 exit_open_fail:
1144 c_can_reset_ram(priv, false);
1145 c_can_pm_runtime_put_sync(priv);
1146 return err;
1147 }
1148
1149 static int c_can_close(struct net_device *dev)
1150 {
1151 struct c_can_priv *priv = netdev_priv(dev);
1152
1153 netif_stop_queue(dev);
1154 napi_disable(&priv->napi);
1155 c_can_stop(dev);
1156 free_irq(dev->irq, dev);
1157 close_candev(dev);
1158
1159 c_can_reset_ram(priv, false);
1160 c_can_pm_runtime_put_sync(priv);
1161
1162 can_led_event(dev, CAN_LED_EVENT_STOP);
1163
1164 return 0;
1165 }
1166
1167 struct net_device *alloc_c_can_dev(void)
1168 {
1169 struct net_device *dev;
1170 struct c_can_priv *priv;
1171
1172 dev = alloc_candev(sizeof(struct c_can_priv), C_CAN_MSG_OBJ_TX_NUM);
1173 if (!dev)
1174 return NULL;
1175
1176 priv = netdev_priv(dev);
1177 netif_napi_add(dev, &priv->napi, c_can_poll, C_CAN_NAPI_WEIGHT);
1178
1179 priv->dev = dev;
1180 priv->can.bittiming_const = &c_can_bittiming_const;
1181 priv->can.do_set_mode = c_can_set_mode;
1182 priv->can.do_get_berr_counter = c_can_get_berr_counter;
1183 priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK |
1184 CAN_CTRLMODE_LISTENONLY |
1185 CAN_CTRLMODE_BERR_REPORTING;
1186
1187 return dev;
1188 }
1189 EXPORT_SYMBOL_GPL(alloc_c_can_dev);
1190
1191 #ifdef CONFIG_PM
1192 int c_can_power_down(struct net_device *dev)
1193 {
1194 u32 val;
1195 unsigned long time_out;
1196 struct c_can_priv *priv = netdev_priv(dev);
1197
1198 if (!(dev->flags & IFF_UP))
1199 return 0;
1200
1201 WARN_ON(priv->type != BOSCH_D_CAN);
1202
1203 /* set PDR value so the device goes to power down mode */
1204 val = priv->read_reg(priv, C_CAN_CTRL_EX_REG);
1205 val |= CONTROL_EX_PDR;
1206 priv->write_reg(priv, C_CAN_CTRL_EX_REG, val);
1207
1208 /* Wait for the PDA bit to get set */
1209 time_out = jiffies + msecs_to_jiffies(INIT_WAIT_MS);
1210 while (!(priv->read_reg(priv, C_CAN_STS_REG) & STATUS_PDA) &&
1211 time_after(time_out, jiffies))
1212 cpu_relax();
1213
1214 if (time_after(jiffies, time_out))
1215 return -ETIMEDOUT;
1216
1217 c_can_stop(dev);
1218
1219 c_can_reset_ram(priv, false);
1220 c_can_pm_runtime_put_sync(priv);
1221
1222 return 0;
1223 }
1224 EXPORT_SYMBOL_GPL(c_can_power_down);
1225
1226 int c_can_power_up(struct net_device *dev)
1227 {
1228 u32 val;
1229 unsigned long time_out;
1230 struct c_can_priv *priv = netdev_priv(dev);
1231 int ret;
1232
1233 if (!(dev->flags & IFF_UP))
1234 return 0;
1235
1236 WARN_ON(priv->type != BOSCH_D_CAN);
1237
1238 c_can_pm_runtime_get_sync(priv);
1239 c_can_reset_ram(priv, true);
1240
1241 /* Clear PDR and INIT bits */
1242 val = priv->read_reg(priv, C_CAN_CTRL_EX_REG);
1243 val &= ~CONTROL_EX_PDR;
1244 priv->write_reg(priv, C_CAN_CTRL_EX_REG, val);
1245 val = priv->read_reg(priv, C_CAN_CTRL_REG);
1246 val &= ~CONTROL_INIT;
1247 priv->write_reg(priv, C_CAN_CTRL_REG, val);
1248
1249 /* Wait for the PDA bit to get clear */
1250 time_out = jiffies + msecs_to_jiffies(INIT_WAIT_MS);
1251 while ((priv->read_reg(priv, C_CAN_STS_REG) & STATUS_PDA) &&
1252 time_after(time_out, jiffies))
1253 cpu_relax();
1254
1255 if (time_after(jiffies, time_out))
1256 return -ETIMEDOUT;
1257
1258 ret = c_can_start(dev);
1259 if (!ret)
1260 c_can_irq_control(priv, true);
1261
1262 return ret;
1263 }
1264 EXPORT_SYMBOL_GPL(c_can_power_up);
1265 #endif
1266
1267 void free_c_can_dev(struct net_device *dev)
1268 {
1269 struct c_can_priv *priv = netdev_priv(dev);
1270
1271 netif_napi_del(&priv->napi);
1272 free_candev(dev);
1273 }
1274 EXPORT_SYMBOL_GPL(free_c_can_dev);
1275
1276 static const struct net_device_ops c_can_netdev_ops = {
1277 .ndo_open = c_can_open,
1278 .ndo_stop = c_can_close,
1279 .ndo_start_xmit = c_can_start_xmit,
1280 .ndo_change_mtu = can_change_mtu,
1281 };
1282
1283 int register_c_can_dev(struct net_device *dev)
1284 {
1285 struct c_can_priv *priv = netdev_priv(dev);
1286 int err;
1287
1288 c_can_pm_runtime_enable(priv);
1289
1290 dev->flags |= IFF_ECHO; /* we support local echo */
1291 dev->netdev_ops = &c_can_netdev_ops;
1292
1293 err = register_candev(dev);
1294 if (err)
1295 c_can_pm_runtime_disable(priv);
1296 else
1297 devm_can_led_init(dev);
1298
1299 return err;
1300 }
1301 EXPORT_SYMBOL_GPL(register_c_can_dev);
1302
1303 void unregister_c_can_dev(struct net_device *dev)
1304 {
1305 struct c_can_priv *priv = netdev_priv(dev);
1306
1307 unregister_candev(dev);
1308
1309 c_can_pm_runtime_disable(priv);
1310 }
1311 EXPORT_SYMBOL_GPL(unregister_c_can_dev);
1312
1313 MODULE_AUTHOR("Bhupesh Sharma <bhupesh.sharma@st.com>");
1314 MODULE_LICENSE("GPL v2");
1315 MODULE_DESCRIPTION("CAN bus driver for Bosch C_CAN controller");
This page took 0.091761 seconds and 5 git commands to generate.