]> git.karo-electronics.de Git - linux-beck.git/blob - drivers/net/can/c_can/c_can.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[linux-beck.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_reg32(priv, reg, (cmd << 16) | obj);
256
257         for (cnt = MIN_TIMEOUT_VALUE; cnt; cnt--) {
258                 if (!(priv->read_reg(priv, reg) & IF_COMR_BUSY))
259                         return;
260                 udelay(1);
261         }
262         netdev_err(dev, "Updating object timed out\n");
263
264 }
265
266 static inline void c_can_object_get(struct net_device *dev, int iface,
267                                     u32 obj, u32 cmd)
268 {
269         c_can_obj_update(dev, iface, cmd, obj);
270 }
271
272 static inline void c_can_object_put(struct net_device *dev, int iface,
273                                     u32 obj, u32 cmd)
274 {
275         c_can_obj_update(dev, iface, cmd | IF_COMM_WR, obj);
276 }
277
278 /*
279  * Note: According to documentation clearing TXIE while MSGVAL is set
280  * is not allowed, but works nicely on C/DCAN. And that lowers the I/O
281  * load significantly.
282  */
283 static void c_can_inval_tx_object(struct net_device *dev, int iface, int obj)
284 {
285         struct c_can_priv *priv = netdev_priv(dev);
286
287         priv->write_reg(priv, C_CAN_IFACE(MSGCTRL_REG, iface), 0);
288         c_can_object_put(dev, iface, obj, IF_COMM_INVAL);
289 }
290
291 static void c_can_inval_msg_object(struct net_device *dev, int iface, int obj)
292 {
293         struct c_can_priv *priv = netdev_priv(dev);
294
295         priv->write_reg(priv, C_CAN_IFACE(ARB1_REG, iface), 0);
296         priv->write_reg(priv, C_CAN_IFACE(ARB2_REG, iface), 0);
297         c_can_inval_tx_object(dev, iface, obj);
298 }
299
300 static void c_can_setup_tx_object(struct net_device *dev, int iface,
301                                   struct can_frame *frame, int idx)
302 {
303         struct c_can_priv *priv = netdev_priv(dev);
304         u16 ctrl = IF_MCONT_TX | frame->can_dlc;
305         bool rtr = frame->can_id & CAN_RTR_FLAG;
306         u32 arb = IF_ARB_MSGVAL;
307         int i;
308
309         if (frame->can_id & CAN_EFF_FLAG) {
310                 arb |= frame->can_id & CAN_EFF_MASK;
311                 arb |= IF_ARB_MSGXTD;
312         } else {
313                 arb |= (frame->can_id & CAN_SFF_MASK) << 18;
314         }
315
316         if (!rtr)
317                 arb |= IF_ARB_TRANSMIT;
318
319         /*
320          * If we change the DIR bit, we need to invalidate the buffer
321          * first, i.e. clear the MSGVAL flag in the arbiter.
322          */
323         if (rtr != (bool)test_bit(idx, &priv->tx_dir)) {
324                 u32 obj = idx + C_CAN_MSG_OBJ_TX_FIRST;
325
326                 c_can_inval_msg_object(dev, iface, obj);
327                 change_bit(idx, &priv->tx_dir);
328         }
329
330         priv->write_reg32(priv, C_CAN_IFACE(ARB1_REG, iface), arb);
331
332         priv->write_reg(priv, C_CAN_IFACE(MSGCTRL_REG, iface), ctrl);
333
334         for (i = 0; i < frame->can_dlc; i += 2) {
335                 priv->write_reg(priv, C_CAN_IFACE(DATA1_REG, iface) + i / 2,
336                                 frame->data[i] | (frame->data[i + 1] << 8));
337         }
338 }
339
340 static inline void c_can_activate_all_lower_rx_msg_obj(struct net_device *dev,
341                                                        int iface)
342 {
343         int i;
344
345         for (i = C_CAN_MSG_OBJ_RX_FIRST; i <= C_CAN_MSG_RX_LOW_LAST; i++)
346                 c_can_object_get(dev, iface, i, IF_COMM_CLR_NEWDAT);
347 }
348
349 static int c_can_handle_lost_msg_obj(struct net_device *dev,
350                                      int iface, int objno, u32 ctrl)
351 {
352         struct net_device_stats *stats = &dev->stats;
353         struct c_can_priv *priv = netdev_priv(dev);
354         struct can_frame *frame;
355         struct sk_buff *skb;
356
357         ctrl &= ~(IF_MCONT_MSGLST | IF_MCONT_INTPND | IF_MCONT_NEWDAT);
358         priv->write_reg(priv, C_CAN_IFACE(MSGCTRL_REG, iface), ctrl);
359         c_can_object_put(dev, iface, objno, IF_COMM_CONTROL);
360
361         stats->rx_errors++;
362         stats->rx_over_errors++;
363
364         /* create an error msg */
365         skb = alloc_can_err_skb(dev, &frame);
366         if (unlikely(!skb))
367                 return 0;
368
369         frame->can_id |= CAN_ERR_CRTL;
370         frame->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
371
372         netif_receive_skb(skb);
373         return 1;
374 }
375
376 static int c_can_read_msg_object(struct net_device *dev, int iface, u32 ctrl)
377 {
378         struct net_device_stats *stats = &dev->stats;
379         struct c_can_priv *priv = netdev_priv(dev);
380         struct can_frame *frame;
381         struct sk_buff *skb;
382         u32 arb, data;
383
384         skb = alloc_can_skb(dev, &frame);
385         if (!skb) {
386                 stats->rx_dropped++;
387                 return -ENOMEM;
388         }
389
390         frame->can_dlc = get_can_dlc(ctrl & 0x0F);
391
392         arb = priv->read_reg32(priv, C_CAN_IFACE(ARB1_REG, iface));
393
394         if (arb & IF_ARB_MSGXTD)
395                 frame->can_id = (arb & CAN_EFF_MASK) | CAN_EFF_FLAG;
396         else
397                 frame->can_id = (arb >> 18) & CAN_SFF_MASK;
398
399         if (arb & IF_ARB_TRANSMIT) {
400                 frame->can_id |= CAN_RTR_FLAG;
401         } else {
402                 int i, dreg = C_CAN_IFACE(DATA1_REG, iface);
403
404                 for (i = 0; i < frame->can_dlc; i += 2, dreg ++) {
405                         data = priv->read_reg(priv, dreg);
406                         frame->data[i] = data;
407                         frame->data[i + 1] = data >> 8;
408                 }
409         }
410
411         stats->rx_packets++;
412         stats->rx_bytes += frame->can_dlc;
413
414         netif_receive_skb(skb);
415         return 0;
416 }
417
418 static void c_can_setup_receive_object(struct net_device *dev, int iface,
419                                        u32 obj, u32 mask, u32 id, u32 mcont)
420 {
421         struct c_can_priv *priv = netdev_priv(dev);
422
423         mask |= BIT(29);
424         priv->write_reg32(priv, C_CAN_IFACE(MASK1_REG, iface), mask);
425
426         id |= IF_ARB_MSGVAL;
427         priv->write_reg32(priv, C_CAN_IFACE(ARB1_REG, iface), id);
428
429         priv->write_reg(priv, C_CAN_IFACE(MSGCTRL_REG, iface), mcont);
430         c_can_object_put(dev, iface, obj, IF_COMM_RCV_SETUP);
431 }
432
433 static netdev_tx_t c_can_start_xmit(struct sk_buff *skb,
434                                     struct net_device *dev)
435 {
436         struct can_frame *frame = (struct can_frame *)skb->data;
437         struct c_can_priv *priv = netdev_priv(dev);
438         u32 idx, obj;
439
440         if (can_dropped_invalid_skb(dev, skb))
441                 return NETDEV_TX_OK;
442         /*
443          * This is not a FIFO. C/D_CAN sends out the buffers
444          * prioritized. The lowest buffer number wins.
445          */
446         idx = fls(atomic_read(&priv->tx_active));
447         obj = idx + C_CAN_MSG_OBJ_TX_FIRST;
448
449         /* If this is the last buffer, stop the xmit queue */
450         if (idx == C_CAN_MSG_OBJ_TX_NUM - 1)
451                 netif_stop_queue(dev);
452         /*
453          * Store the message in the interface so we can call
454          * can_put_echo_skb(). We must do this before we enable
455          * transmit as we might race against do_tx().
456          */
457         c_can_setup_tx_object(dev, IF_TX, frame, idx);
458         priv->dlc[idx] = frame->can_dlc;
459         can_put_echo_skb(skb, dev, idx);
460
461         /* Update the active bits */
462         atomic_add((1 << idx), &priv->tx_active);
463         /* Start transmission */
464         c_can_object_put(dev, IF_TX, obj, IF_COMM_TX);
465
466         return NETDEV_TX_OK;
467 }
468
469 static int c_can_wait_for_ctrl_init(struct net_device *dev,
470                                     struct c_can_priv *priv, u32 init)
471 {
472         int retry = 0;
473
474         while (init != (priv->read_reg(priv, C_CAN_CTRL_REG) & CONTROL_INIT)) {
475                 udelay(10);
476                 if (retry++ > 1000) {
477                         netdev_err(dev, "CCTRL: set CONTROL_INIT failed\n");
478                         return -EIO;
479                 }
480         }
481         return 0;
482 }
483
484 static int c_can_set_bittiming(struct net_device *dev)
485 {
486         unsigned int reg_btr, reg_brpe, ctrl_save;
487         u8 brp, brpe, sjw, tseg1, tseg2;
488         u32 ten_bit_brp;
489         struct c_can_priv *priv = netdev_priv(dev);
490         const struct can_bittiming *bt = &priv->can.bittiming;
491         int res;
492
493         /* c_can provides a 6-bit brp and 4-bit brpe fields */
494         ten_bit_brp = bt->brp - 1;
495         brp = ten_bit_brp & BTR_BRP_MASK;
496         brpe = ten_bit_brp >> 6;
497
498         sjw = bt->sjw - 1;
499         tseg1 = bt->prop_seg + bt->phase_seg1 - 1;
500         tseg2 = bt->phase_seg2 - 1;
501         reg_btr = brp | (sjw << BTR_SJW_SHIFT) | (tseg1 << BTR_TSEG1_SHIFT) |
502                         (tseg2 << BTR_TSEG2_SHIFT);
503         reg_brpe = brpe & BRP_EXT_BRPE_MASK;
504
505         netdev_info(dev,
506                 "setting BTR=%04x BRPE=%04x\n", reg_btr, reg_brpe);
507
508         ctrl_save = priv->read_reg(priv, C_CAN_CTRL_REG);
509         ctrl_save &= ~CONTROL_INIT;
510         priv->write_reg(priv, C_CAN_CTRL_REG, CONTROL_CCE | CONTROL_INIT);
511         res = c_can_wait_for_ctrl_init(dev, priv, CONTROL_INIT);
512         if (res)
513                 return res;
514
515         priv->write_reg(priv, C_CAN_BTR_REG, reg_btr);
516         priv->write_reg(priv, C_CAN_BRPEXT_REG, reg_brpe);
517         priv->write_reg(priv, C_CAN_CTRL_REG, ctrl_save);
518
519         return c_can_wait_for_ctrl_init(dev, priv, 0);
520 }
521
522 /*
523  * Configure C_CAN message objects for Tx and Rx purposes:
524  * C_CAN provides a total of 32 message objects that can be configured
525  * either for Tx or Rx purposes. Here the first 16 message objects are used as
526  * a reception FIFO. The end of reception FIFO is signified by the EoB bit
527  * being SET. The remaining 16 message objects are kept aside for Tx purposes.
528  * See user guide document for further details on configuring message
529  * objects.
530  */
531 static void c_can_configure_msg_objects(struct net_device *dev)
532 {
533         int i;
534
535         /* first invalidate all message objects */
536         for (i = C_CAN_MSG_OBJ_RX_FIRST; i <= C_CAN_NO_OF_OBJECTS; i++)
537                 c_can_inval_msg_object(dev, IF_RX, i);
538
539         /* setup receive message objects */
540         for (i = C_CAN_MSG_OBJ_RX_FIRST; i < C_CAN_MSG_OBJ_RX_LAST; i++)
541                 c_can_setup_receive_object(dev, IF_RX, i, 0, 0, IF_MCONT_RCV);
542
543         c_can_setup_receive_object(dev, IF_RX, C_CAN_MSG_OBJ_RX_LAST, 0, 0,
544                                    IF_MCONT_RCV_EOB);
545 }
546
547 /*
548  * Configure C_CAN chip:
549  * - enable/disable auto-retransmission
550  * - set operating mode
551  * - configure message objects
552  */
553 static int c_can_chip_config(struct net_device *dev)
554 {
555         struct c_can_priv *priv = netdev_priv(dev);
556
557         /* enable automatic retransmission */
558         priv->write_reg(priv, C_CAN_CTRL_REG, CONTROL_ENABLE_AR);
559
560         if ((priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) &&
561             (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)) {
562                 /* loopback + silent mode : useful for hot self-test */
563                 priv->write_reg(priv, C_CAN_CTRL_REG, CONTROL_TEST);
564                 priv->write_reg(priv, C_CAN_TEST_REG, TEST_LBACK | TEST_SILENT);
565         } else if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK) {
566                 /* loopback mode : useful for self-test function */
567                 priv->write_reg(priv, C_CAN_CTRL_REG, CONTROL_TEST);
568                 priv->write_reg(priv, C_CAN_TEST_REG, TEST_LBACK);
569         } else if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) {
570                 /* silent mode : bus-monitoring mode */
571                 priv->write_reg(priv, C_CAN_CTRL_REG, CONTROL_TEST);
572                 priv->write_reg(priv, C_CAN_TEST_REG, TEST_SILENT);
573         }
574
575         /* configure message objects */
576         c_can_configure_msg_objects(dev);
577
578         /* set a `lec` value so that we can check for updates later */
579         priv->write_reg(priv, C_CAN_STS_REG, LEC_UNUSED);
580
581         /* Clear all internal status */
582         atomic_set(&priv->tx_active, 0);
583         priv->rxmasked = 0;
584         priv->tx_dir = 0;
585
586         /* set bittiming params */
587         return c_can_set_bittiming(dev);
588 }
589
590 static int c_can_start(struct net_device *dev)
591 {
592         struct c_can_priv *priv = netdev_priv(dev);
593         int err;
594
595         /* basic c_can configuration */
596         err = c_can_chip_config(dev);
597         if (err)
598                 return err;
599
600         /* Setup the command for new messages */
601         priv->comm_rcv_high = priv->type != BOSCH_D_CAN ?
602                 IF_COMM_RCV_LOW : IF_COMM_RCV_HIGH;
603
604         priv->can.state = CAN_STATE_ERROR_ACTIVE;
605
606         return 0;
607 }
608
609 static void c_can_stop(struct net_device *dev)
610 {
611         struct c_can_priv *priv = netdev_priv(dev);
612
613         c_can_irq_control(priv, false);
614         priv->can.state = CAN_STATE_STOPPED;
615 }
616
617 static int c_can_set_mode(struct net_device *dev, enum can_mode mode)
618 {
619         struct c_can_priv *priv = netdev_priv(dev);
620         int err;
621
622         switch (mode) {
623         case CAN_MODE_START:
624                 err = c_can_start(dev);
625                 if (err)
626                         return err;
627                 netif_wake_queue(dev);
628                 c_can_irq_control(priv, true);
629                 break;
630         default:
631                 return -EOPNOTSUPP;
632         }
633
634         return 0;
635 }
636
637 static int __c_can_get_berr_counter(const struct net_device *dev,
638                                     struct can_berr_counter *bec)
639 {
640         unsigned int reg_err_counter;
641         struct c_can_priv *priv = netdev_priv(dev);
642
643         reg_err_counter = priv->read_reg(priv, C_CAN_ERR_CNT_REG);
644         bec->rxerr = (reg_err_counter & ERR_CNT_REC_MASK) >>
645                                 ERR_CNT_REC_SHIFT;
646         bec->txerr = reg_err_counter & ERR_CNT_TEC_MASK;
647
648         return 0;
649 }
650
651 static int c_can_get_berr_counter(const struct net_device *dev,
652                                   struct can_berr_counter *bec)
653 {
654         struct c_can_priv *priv = netdev_priv(dev);
655         int err;
656
657         c_can_pm_runtime_get_sync(priv);
658         err = __c_can_get_berr_counter(dev, bec);
659         c_can_pm_runtime_put_sync(priv);
660
661         return err;
662 }
663
664 static void c_can_do_tx(struct net_device *dev)
665 {
666         struct c_can_priv *priv = netdev_priv(dev);
667         struct net_device_stats *stats = &dev->stats;
668         u32 idx, obj, pkts = 0, bytes = 0, pend, clr;
669
670         clr = pend = priv->read_reg(priv, C_CAN_INTPND2_REG);
671
672         while ((idx = ffs(pend))) {
673                 idx--;
674                 pend &= ~(1 << idx);
675                 obj = idx + C_CAN_MSG_OBJ_TX_FIRST;
676                 c_can_inval_tx_object(dev, IF_RX, obj);
677                 can_get_echo_skb(dev, idx);
678                 bytes += priv->dlc[idx];
679                 pkts++;
680         }
681
682         /* Clear the bits in the tx_active mask */
683         atomic_sub(clr, &priv->tx_active);
684
685         if (clr & (1 << (C_CAN_MSG_OBJ_TX_NUM - 1)))
686                 netif_wake_queue(dev);
687
688         if (pkts) {
689                 stats->tx_bytes += bytes;
690                 stats->tx_packets += pkts;
691                 can_led_event(dev, CAN_LED_EVENT_TX);
692         }
693 }
694
695 /*
696  * If we have a gap in the pending bits, that means we either
697  * raced with the hardware or failed to readout all upper
698  * objects in the last run due to quota limit.
699  */
700 static u32 c_can_adjust_pending(u32 pend)
701 {
702         u32 weight, lasts;
703
704         if (pend == RECEIVE_OBJECT_BITS)
705                 return pend;
706
707         /*
708          * If the last set bit is larger than the number of pending
709          * bits we have a gap.
710          */
711         weight = hweight32(pend);
712         lasts = fls(pend);
713
714         /* If the bits are linear, nothing to do */
715         if (lasts == weight)
716                 return pend;
717
718         /*
719          * Find the first set bit after the gap. We walk backwards
720          * from the last set bit.
721          */
722         for (lasts--; pend & (1 << (lasts - 1)); lasts--);
723
724         return pend & ~((1 << lasts) - 1);
725 }
726
727 static inline void c_can_rx_object_get(struct net_device *dev,
728                                        struct c_can_priv *priv, u32 obj)
729 {
730                 c_can_object_get(dev, IF_RX, obj, priv->comm_rcv_high);
731 }
732
733 static inline void c_can_rx_finalize(struct net_device *dev,
734                                      struct c_can_priv *priv, u32 obj)
735 {
736         if (priv->type != BOSCH_D_CAN)
737                 c_can_object_get(dev, IF_RX, obj, IF_COMM_CLR_NEWDAT);
738 }
739
740 static int c_can_read_objects(struct net_device *dev, struct c_can_priv *priv,
741                               u32 pend, int quota)
742 {
743         u32 pkts = 0, ctrl, obj;
744
745         while ((obj = ffs(pend)) && quota > 0) {
746                 pend &= ~BIT(obj - 1);
747
748                 c_can_rx_object_get(dev, priv, obj);
749                 ctrl = priv->read_reg(priv, C_CAN_IFACE(MSGCTRL_REG, IF_RX));
750
751                 if (ctrl & IF_MCONT_MSGLST) {
752                         int n = c_can_handle_lost_msg_obj(dev, IF_RX, obj, ctrl);
753
754                         pkts += n;
755                         quota -= n;
756                         continue;
757                 }
758
759                 /*
760                  * This really should not happen, but this covers some
761                  * odd HW behaviour. Do not remove that unless you
762                  * want to brick your machine.
763                  */
764                 if (!(ctrl & IF_MCONT_NEWDAT))
765                         continue;
766
767                 /* read the data from the message object */
768                 c_can_read_msg_object(dev, IF_RX, ctrl);
769
770                 c_can_rx_finalize(dev, priv, obj);
771
772                 pkts++;
773                 quota--;
774         }
775
776         return pkts;
777 }
778
779 static inline u32 c_can_get_pending(struct c_can_priv *priv)
780 {
781         u32 pend = priv->read_reg(priv, C_CAN_NEWDAT1_REG);
782
783         return pend;
784 }
785
786 /*
787  * theory of operation:
788  *
789  * c_can core saves a received CAN message into the first free message
790  * object it finds free (starting with the lowest). Bits NEWDAT and
791  * INTPND are set for this message object indicating that a new message
792  * has arrived. To work-around this issue, we keep two groups of message
793  * objects whose partitioning is defined by C_CAN_MSG_OBJ_RX_SPLIT.
794  *
795  * We clear the newdat bit right away.
796  *
797  * This can result in packet reordering when the readout is slow.
798  */
799 static int c_can_do_rx_poll(struct net_device *dev, int quota)
800 {
801         struct c_can_priv *priv = netdev_priv(dev);
802         u32 pkts = 0, pend = 0, toread, n;
803
804         /*
805          * It is faster to read only one 16bit register. This is only possible
806          * for a maximum number of 16 objects.
807          */
808         BUILD_BUG_ON_MSG(C_CAN_MSG_OBJ_RX_LAST > 16,
809                         "Implementation does not support more message objects than 16");
810
811         while (quota > 0) {
812                 if (!pend) {
813                         pend = c_can_get_pending(priv);
814                         if (!pend)
815                                 break;
816                         /*
817                          * If the pending field has a gap, handle the
818                          * bits above the gap first.
819                          */
820                         toread = c_can_adjust_pending(pend);
821                 } else {
822                         toread = pend;
823                 }
824                 /* Remove the bits from pend */
825                 pend &= ~toread;
826                 /* Read the objects */
827                 n = c_can_read_objects(dev, priv, toread, quota);
828                 pkts += n;
829                 quota -= n;
830         }
831
832         if (pkts)
833                 can_led_event(dev, CAN_LED_EVENT_RX);
834
835         return pkts;
836 }
837
838 static int c_can_handle_state_change(struct net_device *dev,
839                                 enum c_can_bus_error_types error_type)
840 {
841         unsigned int reg_err_counter;
842         unsigned int rx_err_passive;
843         struct c_can_priv *priv = netdev_priv(dev);
844         struct net_device_stats *stats = &dev->stats;
845         struct can_frame *cf;
846         struct sk_buff *skb;
847         struct can_berr_counter bec;
848
849         switch (error_type) {
850         case C_CAN_ERROR_WARNING:
851                 /* error warning state */
852                 priv->can.can_stats.error_warning++;
853                 priv->can.state = CAN_STATE_ERROR_WARNING;
854                 break;
855         case C_CAN_ERROR_PASSIVE:
856                 /* error passive state */
857                 priv->can.can_stats.error_passive++;
858                 priv->can.state = CAN_STATE_ERROR_PASSIVE;
859                 break;
860         case C_CAN_BUS_OFF:
861                 /* bus-off state */
862                 priv->can.state = CAN_STATE_BUS_OFF;
863                 can_bus_off(dev);
864                 break;
865         default:
866                 break;
867         }
868
869         /* propagate the error condition to the CAN stack */
870         skb = alloc_can_err_skb(dev, &cf);
871         if (unlikely(!skb))
872                 return 0;
873
874         __c_can_get_berr_counter(dev, &bec);
875         reg_err_counter = priv->read_reg(priv, C_CAN_ERR_CNT_REG);
876         rx_err_passive = (reg_err_counter & ERR_CNT_RP_MASK) >>
877                                 ERR_CNT_RP_SHIFT;
878
879         switch (error_type) {
880         case C_CAN_ERROR_WARNING:
881                 /* error warning state */
882                 cf->can_id |= CAN_ERR_CRTL;
883                 cf->data[1] = (bec.txerr > bec.rxerr) ?
884                         CAN_ERR_CRTL_TX_WARNING :
885                         CAN_ERR_CRTL_RX_WARNING;
886                 cf->data[6] = bec.txerr;
887                 cf->data[7] = bec.rxerr;
888
889                 break;
890         case C_CAN_ERROR_PASSIVE:
891                 /* error passive state */
892                 cf->can_id |= CAN_ERR_CRTL;
893                 if (rx_err_passive)
894                         cf->data[1] |= CAN_ERR_CRTL_RX_PASSIVE;
895                 if (bec.txerr > 127)
896                         cf->data[1] |= CAN_ERR_CRTL_TX_PASSIVE;
897
898                 cf->data[6] = bec.txerr;
899                 cf->data[7] = bec.rxerr;
900                 break;
901         case C_CAN_BUS_OFF:
902                 /* bus-off state */
903                 cf->can_id |= CAN_ERR_BUSOFF;
904                 can_bus_off(dev);
905                 break;
906         default:
907                 break;
908         }
909
910         stats->rx_packets++;
911         stats->rx_bytes += cf->can_dlc;
912         netif_receive_skb(skb);
913
914         return 1;
915 }
916
917 static int c_can_handle_bus_err(struct net_device *dev,
918                                 enum c_can_lec_type lec_type)
919 {
920         struct c_can_priv *priv = netdev_priv(dev);
921         struct net_device_stats *stats = &dev->stats;
922         struct can_frame *cf;
923         struct sk_buff *skb;
924
925         /*
926          * early exit if no lec update or no error.
927          * no lec update means that no CAN bus event has been detected
928          * since CPU wrote 0x7 value to status reg.
929          */
930         if (lec_type == LEC_UNUSED || lec_type == LEC_NO_ERROR)
931                 return 0;
932
933         if (!(priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING))
934                 return 0;
935
936         /* common for all type of bus errors */
937         priv->can.can_stats.bus_error++;
938         stats->rx_errors++;
939
940         /* propagate the error condition to the CAN stack */
941         skb = alloc_can_err_skb(dev, &cf);
942         if (unlikely(!skb))
943                 return 0;
944
945         /*
946          * check for 'last error code' which tells us the
947          * type of the last error to occur on the CAN bus
948          */
949         cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
950         cf->data[2] |= CAN_ERR_PROT_UNSPEC;
951
952         switch (lec_type) {
953         case LEC_STUFF_ERROR:
954                 netdev_dbg(dev, "stuff error\n");
955                 cf->data[2] |= CAN_ERR_PROT_STUFF;
956                 break;
957         case LEC_FORM_ERROR:
958                 netdev_dbg(dev, "form error\n");
959                 cf->data[2] |= CAN_ERR_PROT_FORM;
960                 break;
961         case LEC_ACK_ERROR:
962                 netdev_dbg(dev, "ack error\n");
963                 cf->data[3] |= (CAN_ERR_PROT_LOC_ACK |
964                                 CAN_ERR_PROT_LOC_ACK_DEL);
965                 break;
966         case LEC_BIT1_ERROR:
967                 netdev_dbg(dev, "bit1 error\n");
968                 cf->data[2] |= CAN_ERR_PROT_BIT1;
969                 break;
970         case LEC_BIT0_ERROR:
971                 netdev_dbg(dev, "bit0 error\n");
972                 cf->data[2] |= CAN_ERR_PROT_BIT0;
973                 break;
974         case LEC_CRC_ERROR:
975                 netdev_dbg(dev, "CRC error\n");
976                 cf->data[3] |= (CAN_ERR_PROT_LOC_CRC_SEQ |
977                                 CAN_ERR_PROT_LOC_CRC_DEL);
978                 break;
979         default:
980                 break;
981         }
982
983         stats->rx_packets++;
984         stats->rx_bytes += cf->can_dlc;
985         netif_receive_skb(skb);
986         return 1;
987 }
988
989 static int c_can_poll(struct napi_struct *napi, int quota)
990 {
991         struct net_device *dev = napi->dev;
992         struct c_can_priv *priv = netdev_priv(dev);
993         u16 curr, last = priv->last_status;
994         int work_done = 0;
995
996         priv->last_status = curr = priv->read_reg(priv, C_CAN_STS_REG);
997         /* Ack status on C_CAN. D_CAN is self clearing */
998         if (priv->type != BOSCH_D_CAN)
999                 priv->write_reg(priv, C_CAN_STS_REG, LEC_UNUSED);
1000
1001         /* handle state changes */
1002         if ((curr & STATUS_EWARN) && (!(last & STATUS_EWARN))) {
1003                 netdev_dbg(dev, "entered error warning state\n");
1004                 work_done += c_can_handle_state_change(dev, C_CAN_ERROR_WARNING);
1005         }
1006
1007         if ((curr & STATUS_EPASS) && (!(last & STATUS_EPASS))) {
1008                 netdev_dbg(dev, "entered error passive state\n");
1009                 work_done += c_can_handle_state_change(dev, C_CAN_ERROR_PASSIVE);
1010         }
1011
1012         if ((curr & STATUS_BOFF) && (!(last & STATUS_BOFF))) {
1013                 netdev_dbg(dev, "entered bus off state\n");
1014                 work_done += c_can_handle_state_change(dev, C_CAN_BUS_OFF);
1015                 goto end;
1016         }
1017
1018         /* handle bus recovery events */
1019         if ((!(curr & STATUS_BOFF)) && (last & STATUS_BOFF)) {
1020                 netdev_dbg(dev, "left bus off state\n");
1021                 priv->can.state = CAN_STATE_ERROR_ACTIVE;
1022         }
1023         if ((!(curr & STATUS_EPASS)) && (last & STATUS_EPASS)) {
1024                 netdev_dbg(dev, "left error passive state\n");
1025                 priv->can.state = CAN_STATE_ERROR_ACTIVE;
1026         }
1027
1028         /* handle lec errors on the bus */
1029         work_done += c_can_handle_bus_err(dev, curr & LEC_MASK);
1030
1031         /* Handle Tx/Rx events. We do this unconditionally */
1032         work_done += c_can_do_rx_poll(dev, (quota - work_done));
1033         c_can_do_tx(dev);
1034
1035 end:
1036         if (work_done < quota) {
1037                 napi_complete(napi);
1038                 /* enable all IRQs if we are not in bus off state */
1039                 if (priv->can.state != CAN_STATE_BUS_OFF)
1040                         c_can_irq_control(priv, true);
1041         }
1042
1043         return work_done;
1044 }
1045
1046 static irqreturn_t c_can_isr(int irq, void *dev_id)
1047 {
1048         struct net_device *dev = (struct net_device *)dev_id;
1049         struct c_can_priv *priv = netdev_priv(dev);
1050
1051         if (!priv->read_reg(priv, C_CAN_INT_REG))
1052                 return IRQ_NONE;
1053
1054         /* disable all interrupts and schedule the NAPI */
1055         c_can_irq_control(priv, false);
1056         napi_schedule(&priv->napi);
1057
1058         return IRQ_HANDLED;
1059 }
1060
1061 static int c_can_open(struct net_device *dev)
1062 {
1063         int err;
1064         struct c_can_priv *priv = netdev_priv(dev);
1065
1066         c_can_pm_runtime_get_sync(priv);
1067         c_can_reset_ram(priv, true);
1068
1069         /* open the can device */
1070         err = open_candev(dev);
1071         if (err) {
1072                 netdev_err(dev, "failed to open can device\n");
1073                 goto exit_open_fail;
1074         }
1075
1076         /* register interrupt handler */
1077         err = request_irq(dev->irq, &c_can_isr, IRQF_SHARED, dev->name,
1078                                 dev);
1079         if (err < 0) {
1080                 netdev_err(dev, "failed to request interrupt\n");
1081                 goto exit_irq_fail;
1082         }
1083
1084         /* start the c_can controller */
1085         err = c_can_start(dev);
1086         if (err)
1087                 goto exit_start_fail;
1088
1089         can_led_event(dev, CAN_LED_EVENT_OPEN);
1090
1091         napi_enable(&priv->napi);
1092         /* enable status change, error and module interrupts */
1093         c_can_irq_control(priv, true);
1094         netif_start_queue(dev);
1095
1096         return 0;
1097
1098 exit_start_fail:
1099         free_irq(dev->irq, dev);
1100 exit_irq_fail:
1101         close_candev(dev);
1102 exit_open_fail:
1103         c_can_reset_ram(priv, false);
1104         c_can_pm_runtime_put_sync(priv);
1105         return err;
1106 }
1107
1108 static int c_can_close(struct net_device *dev)
1109 {
1110         struct c_can_priv *priv = netdev_priv(dev);
1111
1112         netif_stop_queue(dev);
1113         napi_disable(&priv->napi);
1114         c_can_stop(dev);
1115         free_irq(dev->irq, dev);
1116         close_candev(dev);
1117
1118         c_can_reset_ram(priv, false);
1119         c_can_pm_runtime_put_sync(priv);
1120
1121         can_led_event(dev, CAN_LED_EVENT_STOP);
1122
1123         return 0;
1124 }
1125
1126 struct net_device *alloc_c_can_dev(void)
1127 {
1128         struct net_device *dev;
1129         struct c_can_priv *priv;
1130
1131         dev = alloc_candev(sizeof(struct c_can_priv), C_CAN_MSG_OBJ_TX_NUM);
1132         if (!dev)
1133                 return NULL;
1134
1135         priv = netdev_priv(dev);
1136         netif_napi_add(dev, &priv->napi, c_can_poll, C_CAN_NAPI_WEIGHT);
1137
1138         priv->dev = dev;
1139         priv->can.bittiming_const = &c_can_bittiming_const;
1140         priv->can.do_set_mode = c_can_set_mode;
1141         priv->can.do_get_berr_counter = c_can_get_berr_counter;
1142         priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK |
1143                                         CAN_CTRLMODE_LISTENONLY |
1144                                         CAN_CTRLMODE_BERR_REPORTING;
1145
1146         return dev;
1147 }
1148 EXPORT_SYMBOL_GPL(alloc_c_can_dev);
1149
1150 #ifdef CONFIG_PM
1151 int c_can_power_down(struct net_device *dev)
1152 {
1153         u32 val;
1154         unsigned long time_out;
1155         struct c_can_priv *priv = netdev_priv(dev);
1156
1157         if (!(dev->flags & IFF_UP))
1158                 return 0;
1159
1160         WARN_ON(priv->type != BOSCH_D_CAN);
1161
1162         /* set PDR value so the device goes to power down mode */
1163         val = priv->read_reg(priv, C_CAN_CTRL_EX_REG);
1164         val |= CONTROL_EX_PDR;
1165         priv->write_reg(priv, C_CAN_CTRL_EX_REG, val);
1166
1167         /* Wait for the PDA bit to get set */
1168         time_out = jiffies + msecs_to_jiffies(INIT_WAIT_MS);
1169         while (!(priv->read_reg(priv, C_CAN_STS_REG) & STATUS_PDA) &&
1170                                 time_after(time_out, jiffies))
1171                 cpu_relax();
1172
1173         if (time_after(jiffies, time_out))
1174                 return -ETIMEDOUT;
1175
1176         c_can_stop(dev);
1177
1178         c_can_reset_ram(priv, false);
1179         c_can_pm_runtime_put_sync(priv);
1180
1181         return 0;
1182 }
1183 EXPORT_SYMBOL_GPL(c_can_power_down);
1184
1185 int c_can_power_up(struct net_device *dev)
1186 {
1187         u32 val;
1188         unsigned long time_out;
1189         struct c_can_priv *priv = netdev_priv(dev);
1190         int ret;
1191
1192         if (!(dev->flags & IFF_UP))
1193                 return 0;
1194
1195         WARN_ON(priv->type != BOSCH_D_CAN);
1196
1197         c_can_pm_runtime_get_sync(priv);
1198         c_can_reset_ram(priv, true);
1199
1200         /* Clear PDR and INIT bits */
1201         val = priv->read_reg(priv, C_CAN_CTRL_EX_REG);
1202         val &= ~CONTROL_EX_PDR;
1203         priv->write_reg(priv, C_CAN_CTRL_EX_REG, val);
1204         val = priv->read_reg(priv, C_CAN_CTRL_REG);
1205         val &= ~CONTROL_INIT;
1206         priv->write_reg(priv, C_CAN_CTRL_REG, val);
1207
1208         /* Wait for the PDA bit to get clear */
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         ret = c_can_start(dev);
1218         if (!ret)
1219                 c_can_irq_control(priv, true);
1220
1221         return ret;
1222 }
1223 EXPORT_SYMBOL_GPL(c_can_power_up);
1224 #endif
1225
1226 void free_c_can_dev(struct net_device *dev)
1227 {
1228         struct c_can_priv *priv = netdev_priv(dev);
1229
1230         netif_napi_del(&priv->napi);
1231         free_candev(dev);
1232 }
1233 EXPORT_SYMBOL_GPL(free_c_can_dev);
1234
1235 static const struct net_device_ops c_can_netdev_ops = {
1236         .ndo_open = c_can_open,
1237         .ndo_stop = c_can_close,
1238         .ndo_start_xmit = c_can_start_xmit,
1239         .ndo_change_mtu = can_change_mtu,
1240 };
1241
1242 int register_c_can_dev(struct net_device *dev)
1243 {
1244         struct c_can_priv *priv = netdev_priv(dev);
1245         int err;
1246
1247         c_can_pm_runtime_enable(priv);
1248
1249         dev->flags |= IFF_ECHO; /* we support local echo */
1250         dev->netdev_ops = &c_can_netdev_ops;
1251
1252         err = register_candev(dev);
1253         if (err)
1254                 c_can_pm_runtime_disable(priv);
1255         else
1256                 devm_can_led_init(dev);
1257
1258         return err;
1259 }
1260 EXPORT_SYMBOL_GPL(register_c_can_dev);
1261
1262 void unregister_c_can_dev(struct net_device *dev)
1263 {
1264         struct c_can_priv *priv = netdev_priv(dev);
1265
1266         unregister_candev(dev);
1267
1268         c_can_pm_runtime_disable(priv);
1269 }
1270 EXPORT_SYMBOL_GPL(unregister_c_can_dev);
1271
1272 MODULE_AUTHOR("Bhupesh Sharma <bhupesh.sharma@st.com>");
1273 MODULE_LICENSE("GPL v2");
1274 MODULE_DESCRIPTION("CAN bus driver for Bosch C_CAN controller");