2 * CAN bus driver for Bosch C_CAN controller
4 * Copyright (C) 2010 ST Microelectronics
7 * Borrowed heavily from the C_CAN driver originally written by:
12 * TX and RX NAPI implementation has been borrowed from at91 CAN driver
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
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.
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>
37 #include <linux/pm_runtime.h>
38 #include <linux/pinctrl/consumer.h>
40 #include <linux/can.h>
41 #include <linux/can/dev.h>
42 #include <linux/can/error.h>
43 #include <linux/can/led.h>
47 /* Number of interface registers */
48 #define IF_ENUM_REG_LEN 11
49 #define C_CAN_IFACE(reg, iface) (C_CAN_IF1_##reg + (iface) * IF_ENUM_REG_LEN)
51 /* control extension register D_CAN specific */
52 #define CONTROL_EX_PDR BIT(8)
54 /* control register */
55 #define CONTROL_SWR BIT(15)
56 #define CONTROL_TEST BIT(7)
57 #define CONTROL_CCE BIT(6)
58 #define CONTROL_DISABLE_AR BIT(5)
59 #define CONTROL_ENABLE_AR (0 << 5)
60 #define CONTROL_EIE BIT(3)
61 #define CONTROL_SIE BIT(2)
62 #define CONTROL_IE BIT(1)
63 #define CONTROL_INIT BIT(0)
65 #define CONTROL_IRQMSK (CONTROL_EIE | CONTROL_IE | CONTROL_SIE)
68 #define TEST_RX BIT(7)
69 #define TEST_TX1 BIT(6)
70 #define TEST_TX2 BIT(5)
71 #define TEST_LBACK BIT(4)
72 #define TEST_SILENT BIT(3)
73 #define TEST_BASIC BIT(2)
76 #define STATUS_PDA BIT(10)
77 #define STATUS_BOFF BIT(7)
78 #define STATUS_EWARN BIT(6)
79 #define STATUS_EPASS BIT(5)
80 #define STATUS_RXOK BIT(4)
81 #define STATUS_TXOK BIT(3)
83 /* error counter register */
84 #define ERR_CNT_TEC_MASK 0xff
85 #define ERR_CNT_TEC_SHIFT 0
86 #define ERR_CNT_REC_SHIFT 8
87 #define ERR_CNT_REC_MASK (0x7f << ERR_CNT_REC_SHIFT)
88 #define ERR_CNT_RP_SHIFT 15
89 #define ERR_CNT_RP_MASK (0x1 << ERR_CNT_RP_SHIFT)
91 /* bit-timing register */
92 #define BTR_BRP_MASK 0x3f
93 #define BTR_BRP_SHIFT 0
94 #define BTR_SJW_SHIFT 6
95 #define BTR_SJW_MASK (0x3 << BTR_SJW_SHIFT)
96 #define BTR_TSEG1_SHIFT 8
97 #define BTR_TSEG1_MASK (0xf << BTR_TSEG1_SHIFT)
98 #define BTR_TSEG2_SHIFT 12
99 #define BTR_TSEG2_MASK (0x7 << BTR_TSEG2_SHIFT)
101 /* interrupt register */
102 #define INT_STS_PENDING 0x8000
104 /* brp extension register */
105 #define BRP_EXT_BRPE_MASK 0x0f
106 #define BRP_EXT_BRPE_SHIFT 0
108 /* IFx command request */
109 #define IF_COMR_BUSY BIT(15)
111 /* IFx command mask */
112 #define IF_COMM_WR BIT(7)
113 #define IF_COMM_MASK BIT(6)
114 #define IF_COMM_ARB BIT(5)
115 #define IF_COMM_CONTROL BIT(4)
116 #define IF_COMM_CLR_INT_PND BIT(3)
117 #define IF_COMM_TXRQST BIT(2)
118 #define IF_COMM_CLR_NEWDAT IF_COMM_TXRQST
119 #define IF_COMM_DATAA BIT(1)
120 #define IF_COMM_DATAB BIT(0)
122 /* TX buffer setup */
123 #define IF_COMM_TX (IF_COMM_ARB | IF_COMM_CONTROL | \
125 IF_COMM_DATAA | IF_COMM_DATAB)
127 /* For the low buffers we clear the interrupt bit, but keep newdat */
128 #define IF_COMM_RCV_LOW (IF_COMM_MASK | IF_COMM_ARB | \
129 IF_COMM_CONTROL | IF_COMM_CLR_INT_PND | \
130 IF_COMM_DATAA | IF_COMM_DATAB)
132 /* For the high buffers we clear the interrupt bit and newdat */
133 #define IF_COMM_RCV_HIGH (IF_COMM_RCV_LOW | IF_COMM_CLR_NEWDAT)
135 /* Receive setup of message objects */
136 #define IF_COMM_RCV_SETUP (IF_COMM_MASK | IF_COMM_ARB | IF_COMM_CONTROL)
138 /* Invalidation of message objects */
139 #define IF_COMM_INVAL (IF_COMM_ARB | IF_COMM_CONTROL)
141 /* IFx arbitration */
142 #define IF_ARB_MSGVAL BIT(31)
143 #define IF_ARB_MSGXTD BIT(30)
144 #define IF_ARB_TRANSMIT BIT(29)
146 /* IFx message control */
147 #define IF_MCONT_NEWDAT BIT(15)
148 #define IF_MCONT_MSGLST BIT(14)
149 #define IF_MCONT_INTPND BIT(13)
150 #define IF_MCONT_UMASK BIT(12)
151 #define IF_MCONT_TXIE BIT(11)
152 #define IF_MCONT_RXIE BIT(10)
153 #define IF_MCONT_RMTEN BIT(9)
154 #define IF_MCONT_TXRQST BIT(8)
155 #define IF_MCONT_EOB BIT(7)
156 #define IF_MCONT_DLC_MASK 0xf
158 #define IF_MCONT_RCV (IF_MCONT_RXIE | IF_MCONT_UMASK)
159 #define IF_MCONT_RCV_EOB (IF_MCONT_RCV | IF_MCONT_EOB)
161 #define IF_MCONT_TX (IF_MCONT_TXIE | IF_MCONT_EOB)
163 /* Use IF1 for RX and IF2 for TX */
167 /* minimum timeout for checking BUSY status */
168 #define MIN_TIMEOUT_VALUE 6
170 /* Wait for ~1 sec for INIT bit */
171 #define INIT_WAIT_MS 1000
173 /* c_can lec values */
174 enum c_can_lec_type {
183 LEC_MASK = LEC_UNUSED,
186 /* c_can error types:
187 * Bus errors (BUS_OFF, ERROR_WARNING, ERROR_PASSIVE) are supported
189 enum c_can_bus_error_types {
196 static const struct can_bittiming_const c_can_bittiming_const = {
197 .name = KBUILD_MODNAME,
198 .tseg1_min = 2, /* Time segment 1 = prop_seg + phase_seg1 */
200 .tseg2_min = 1, /* Time segment 2 = phase_seg2 */
204 .brp_max = 1024, /* 6-bit BRP field + 4-bit BRPE field*/
208 static inline void c_can_pm_runtime_get_sync(const struct c_can_priv *priv)
211 pm_runtime_get_sync(priv->device);
214 static inline void c_can_pm_runtime_put_sync(const struct c_can_priv *priv)
217 pm_runtime_put_sync(priv->device);
220 static inline void c_can_reset_ram(const struct c_can_priv *priv, bool enable)
223 priv->raminit(priv, enable);
226 static void c_can_irq_control(struct c_can_priv *priv, bool enable)
228 u32 ctrl = priv->read_reg(priv, C_CAN_CTRL_REG) & ~CONTROL_IRQMSK;
231 ctrl |= CONTROL_IRQMSK;
233 priv->write_reg(priv, C_CAN_CTRL_REG, ctrl);
236 static void c_can_obj_update(struct net_device *dev, int iface, u32 cmd, u32 obj)
238 struct c_can_priv *priv = netdev_priv(dev);
239 int cnt, reg = C_CAN_IFACE(COMREQ_REG, iface);
241 priv->write_reg32(priv, reg, (cmd << 16) | obj);
243 for (cnt = MIN_TIMEOUT_VALUE; cnt; cnt--) {
244 if (!(priv->read_reg(priv, reg) & IF_COMR_BUSY))
248 netdev_err(dev, "Updating object timed out\n");
251 static inline void c_can_object_get(struct net_device *dev, int iface,
254 c_can_obj_update(dev, iface, cmd, obj);
257 static inline void c_can_object_put(struct net_device *dev, int iface,
260 c_can_obj_update(dev, iface, cmd | IF_COMM_WR, obj);
263 /* Note: According to documentation clearing TXIE while MSGVAL is set
264 * is not allowed, but works nicely on C/DCAN. And that lowers the I/O
265 * load significantly.
267 static void c_can_inval_tx_object(struct net_device *dev, int iface, int obj)
269 struct c_can_priv *priv = netdev_priv(dev);
271 priv->write_reg(priv, C_CAN_IFACE(MSGCTRL_REG, iface), 0);
272 c_can_object_put(dev, iface, obj, IF_COMM_INVAL);
275 static void c_can_inval_msg_object(struct net_device *dev, int iface, int obj)
277 struct c_can_priv *priv = netdev_priv(dev);
279 priv->write_reg32(priv, C_CAN_IFACE(ARB1_REG, iface), 0);
280 c_can_inval_tx_object(dev, iface, obj);
283 static void c_can_setup_tx_object(struct net_device *dev, int iface,
284 struct can_frame *frame, int idx)
286 struct c_can_priv *priv = netdev_priv(dev);
287 u16 ctrl = IF_MCONT_TX | frame->len;
288 bool rtr = frame->can_id & CAN_RTR_FLAG;
289 u32 arb = IF_ARB_MSGVAL;
292 if (frame->can_id & CAN_EFF_FLAG) {
293 arb |= frame->can_id & CAN_EFF_MASK;
294 arb |= IF_ARB_MSGXTD;
296 arb |= (frame->can_id & CAN_SFF_MASK) << 18;
300 arb |= IF_ARB_TRANSMIT;
302 /* If we change the DIR bit, we need to invalidate the buffer
303 * first, i.e. clear the MSGVAL flag in the arbiter.
305 if (rtr != (bool)test_bit(idx, &priv->tx_dir)) {
306 u32 obj = idx + priv->msg_obj_tx_first;
308 c_can_inval_msg_object(dev, iface, obj);
309 change_bit(idx, &priv->tx_dir);
312 priv->write_reg32(priv, C_CAN_IFACE(ARB1_REG, iface), arb);
314 priv->write_reg(priv, C_CAN_IFACE(MSGCTRL_REG, iface), ctrl);
316 if (priv->type == BOSCH_D_CAN) {
317 u32 data = 0, dreg = C_CAN_IFACE(DATA1_REG, iface);
319 for (i = 0; i < frame->len; i += 4, dreg += 2) {
320 data = (u32)frame->data[i];
321 data |= (u32)frame->data[i + 1] << 8;
322 data |= (u32)frame->data[i + 2] << 16;
323 data |= (u32)frame->data[i + 3] << 24;
324 priv->write_reg32(priv, dreg, data);
327 for (i = 0; i < frame->len; i += 2) {
328 priv->write_reg(priv,
329 C_CAN_IFACE(DATA1_REG, iface) + i / 2,
331 (frame->data[i + 1] << 8));
336 static int c_can_handle_lost_msg_obj(struct net_device *dev,
337 int iface, int objno, u32 ctrl)
339 struct net_device_stats *stats = &dev->stats;
340 struct c_can_priv *priv = netdev_priv(dev);
341 struct can_frame *frame;
344 ctrl &= ~(IF_MCONT_MSGLST | IF_MCONT_INTPND | IF_MCONT_NEWDAT);
345 priv->write_reg(priv, C_CAN_IFACE(MSGCTRL_REG, iface), ctrl);
346 c_can_object_put(dev, iface, objno, IF_COMM_CONTROL);
349 stats->rx_over_errors++;
351 /* create an error msg */
352 skb = alloc_can_err_skb(dev, &frame);
356 frame->can_id |= CAN_ERR_CRTL;
357 frame->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
359 netif_receive_skb(skb);
363 static int c_can_read_msg_object(struct net_device *dev, int iface, u32 ctrl)
365 struct net_device_stats *stats = &dev->stats;
366 struct c_can_priv *priv = netdev_priv(dev);
367 struct can_frame *frame;
371 skb = alloc_can_skb(dev, &frame);
377 frame->len = can_cc_dlc2len(ctrl & 0x0F);
379 arb = priv->read_reg32(priv, C_CAN_IFACE(ARB1_REG, iface));
381 if (arb & IF_ARB_MSGXTD)
382 frame->can_id = (arb & CAN_EFF_MASK) | CAN_EFF_FLAG;
384 frame->can_id = (arb >> 18) & CAN_SFF_MASK;
386 if (arb & IF_ARB_TRANSMIT) {
387 frame->can_id |= CAN_RTR_FLAG;
389 int i, dreg = C_CAN_IFACE(DATA1_REG, iface);
391 if (priv->type == BOSCH_D_CAN) {
392 for (i = 0; i < frame->len; i += 4, dreg += 2) {
393 data = priv->read_reg32(priv, dreg);
394 frame->data[i] = data;
395 frame->data[i + 1] = data >> 8;
396 frame->data[i + 2] = data >> 16;
397 frame->data[i + 3] = data >> 24;
400 for (i = 0; i < frame->len; i += 2, dreg++) {
401 data = priv->read_reg(priv, dreg);
402 frame->data[i] = data;
403 frame->data[i + 1] = data >> 8;
409 stats->rx_bytes += frame->len;
411 netif_receive_skb(skb);
415 static void c_can_setup_receive_object(struct net_device *dev, int iface,
416 u32 obj, u32 mask, u32 id, u32 mcont)
418 struct c_can_priv *priv = netdev_priv(dev);
421 priv->write_reg32(priv, C_CAN_IFACE(MASK1_REG, iface), mask);
424 priv->write_reg32(priv, C_CAN_IFACE(ARB1_REG, iface), id);
426 priv->write_reg(priv, C_CAN_IFACE(MSGCTRL_REG, iface), mcont);
427 c_can_object_put(dev, iface, obj, IF_COMM_RCV_SETUP);
430 static netdev_tx_t c_can_start_xmit(struct sk_buff *skb,
431 struct net_device *dev)
433 struct can_frame *frame = (struct can_frame *)skb->data;
434 struct c_can_priv *priv = netdev_priv(dev);
437 if (can_dropped_invalid_skb(dev, skb))
439 /* This is not a FIFO. C/D_CAN sends out the buffers
440 * prioritized. The lowest buffer number wins.
442 idx = fls(atomic_read(&priv->tx_active));
443 obj = idx + priv->msg_obj_tx_first;
445 /* If this is the last buffer, stop the xmit queue */
446 if (idx == priv->msg_obj_tx_num - 1)
447 netif_stop_queue(dev);
448 /* Store the message in the interface so we can call
449 * can_put_echo_skb(). We must do this before we enable
450 * transmit as we might race against do_tx().
452 c_can_setup_tx_object(dev, IF_TX, frame, idx);
453 priv->dlc[idx] = frame->len;
454 can_put_echo_skb(skb, dev, idx, 0);
456 /* Update the active bits */
457 atomic_add(BIT(idx), &priv->tx_active);
458 /* Start transmission */
459 c_can_object_put(dev, IF_TX, obj, IF_COMM_TX);
464 static int c_can_wait_for_ctrl_init(struct net_device *dev,
465 struct c_can_priv *priv, u32 init)
469 while (init != (priv->read_reg(priv, C_CAN_CTRL_REG) & CONTROL_INIT)) {
471 if (retry++ > 1000) {
472 netdev_err(dev, "CCTRL: set CONTROL_INIT failed\n");
479 static int c_can_set_bittiming(struct net_device *dev)
481 unsigned int reg_btr, reg_brpe, ctrl_save;
482 u8 brp, brpe, sjw, tseg1, tseg2;
484 struct c_can_priv *priv = netdev_priv(dev);
485 const struct can_bittiming *bt = &priv->can.bittiming;
488 /* c_can provides a 6-bit brp and 4-bit brpe fields */
489 ten_bit_brp = bt->brp - 1;
490 brp = ten_bit_brp & BTR_BRP_MASK;
491 brpe = ten_bit_brp >> 6;
494 tseg1 = bt->prop_seg + bt->phase_seg1 - 1;
495 tseg2 = bt->phase_seg2 - 1;
496 reg_btr = brp | (sjw << BTR_SJW_SHIFT) | (tseg1 << BTR_TSEG1_SHIFT) |
497 (tseg2 << BTR_TSEG2_SHIFT);
498 reg_brpe = brpe & BRP_EXT_BRPE_MASK;
501 "setting BTR=%04x BRPE=%04x\n", reg_btr, reg_brpe);
503 ctrl_save = priv->read_reg(priv, C_CAN_CTRL_REG);
504 ctrl_save &= ~CONTROL_INIT;
505 priv->write_reg(priv, C_CAN_CTRL_REG, CONTROL_CCE | CONTROL_INIT);
506 res = c_can_wait_for_ctrl_init(dev, priv, CONTROL_INIT);
510 priv->write_reg(priv, C_CAN_BTR_REG, reg_btr);
511 priv->write_reg(priv, C_CAN_BRPEXT_REG, reg_brpe);
512 priv->write_reg(priv, C_CAN_CTRL_REG, ctrl_save);
514 return c_can_wait_for_ctrl_init(dev, priv, 0);
517 /* Configure C_CAN message objects for Tx and Rx purposes:
518 * C_CAN provides a total of 32 message objects that can be configured
519 * either for Tx or Rx purposes. Here the first 16 message objects are used as
520 * a reception FIFO. The end of reception FIFO is signified by the EoB bit
521 * being SET. The remaining 16 message objects are kept aside for Tx purposes.
522 * See user guide document for further details on configuring message
525 static void c_can_configure_msg_objects(struct net_device *dev)
527 struct c_can_priv *priv = netdev_priv(dev);
530 /* first invalidate all message objects */
531 for (i = priv->msg_obj_rx_first; i <= priv->msg_obj_num; i++)
532 c_can_inval_msg_object(dev, IF_RX, i);
534 /* setup receive message objects */
535 for (i = priv->msg_obj_rx_first; i < priv->msg_obj_rx_last; i++)
536 c_can_setup_receive_object(dev, IF_RX, i, 0, 0, IF_MCONT_RCV);
538 c_can_setup_receive_object(dev, IF_RX, priv->msg_obj_rx_last, 0, 0,
542 static int c_can_software_reset(struct net_device *dev)
544 struct c_can_priv *priv = netdev_priv(dev);
547 if (priv->type != BOSCH_D_CAN)
550 priv->write_reg(priv, C_CAN_CTRL_REG, CONTROL_SWR | CONTROL_INIT);
551 while (priv->read_reg(priv, C_CAN_CTRL_REG) & CONTROL_SWR) {
554 netdev_err(dev, "CCTRL: software reset failed\n");
562 /* Configure C_CAN chip:
563 * - enable/disable auto-retransmission
564 * - set operating mode
565 * - configure message objects
567 static int c_can_chip_config(struct net_device *dev)
569 struct c_can_priv *priv = netdev_priv(dev);
572 err = c_can_software_reset(dev);
576 /* enable automatic retransmission */
577 priv->write_reg(priv, C_CAN_CTRL_REG, CONTROL_ENABLE_AR);
579 if ((priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) &&
580 (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)) {
581 /* loopback + silent mode : useful for hot self-test */
582 priv->write_reg(priv, C_CAN_CTRL_REG, CONTROL_TEST);
583 priv->write_reg(priv, C_CAN_TEST_REG, TEST_LBACK | TEST_SILENT);
584 } else if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK) {
585 /* loopback mode : useful for self-test function */
586 priv->write_reg(priv, C_CAN_CTRL_REG, CONTROL_TEST);
587 priv->write_reg(priv, C_CAN_TEST_REG, TEST_LBACK);
588 } else if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) {
589 /* silent mode : bus-monitoring mode */
590 priv->write_reg(priv, C_CAN_CTRL_REG, CONTROL_TEST);
591 priv->write_reg(priv, C_CAN_TEST_REG, TEST_SILENT);
594 /* configure message objects */
595 c_can_configure_msg_objects(dev);
597 /* set a `lec` value so that we can check for updates later */
598 priv->write_reg(priv, C_CAN_STS_REG, LEC_UNUSED);
600 /* Clear all internal status */
601 atomic_set(&priv->tx_active, 0);
605 /* set bittiming params */
606 return c_can_set_bittiming(dev);
609 static int c_can_start(struct net_device *dev)
611 struct c_can_priv *priv = netdev_priv(dev);
615 /* basic c_can configuration */
616 err = c_can_chip_config(dev);
620 /* Setup the command for new messages */
621 priv->comm_rcv_high = priv->type != BOSCH_D_CAN ?
622 IF_COMM_RCV_LOW : IF_COMM_RCV_HIGH;
624 priv->can.state = CAN_STATE_ERROR_ACTIVE;
626 /* Attempt to use "active" if available else use "default" */
627 p = pinctrl_get_select(priv->device, "active");
631 pinctrl_pm_select_default_state(priv->device);
636 static void c_can_stop(struct net_device *dev)
638 struct c_can_priv *priv = netdev_priv(dev);
640 c_can_irq_control(priv, false);
642 /* put ctrl to init on stop to end ongoing transmission */
643 priv->write_reg(priv, C_CAN_CTRL_REG, CONTROL_INIT);
645 /* deactivate pins */
646 pinctrl_pm_select_sleep_state(dev->dev.parent);
647 priv->can.state = CAN_STATE_STOPPED;
650 static int c_can_set_mode(struct net_device *dev, enum can_mode mode)
652 struct c_can_priv *priv = netdev_priv(dev);
657 err = c_can_start(dev);
660 netif_wake_queue(dev);
661 c_can_irq_control(priv, true);
670 static int __c_can_get_berr_counter(const struct net_device *dev,
671 struct can_berr_counter *bec)
673 unsigned int reg_err_counter;
674 struct c_can_priv *priv = netdev_priv(dev);
676 reg_err_counter = priv->read_reg(priv, C_CAN_ERR_CNT_REG);
677 bec->rxerr = (reg_err_counter & ERR_CNT_REC_MASK) >>
679 bec->txerr = reg_err_counter & ERR_CNT_TEC_MASK;
684 static int c_can_get_berr_counter(const struct net_device *dev,
685 struct can_berr_counter *bec)
687 struct c_can_priv *priv = netdev_priv(dev);
690 c_can_pm_runtime_get_sync(priv);
691 err = __c_can_get_berr_counter(dev, bec);
692 c_can_pm_runtime_put_sync(priv);
697 static void c_can_do_tx(struct net_device *dev)
699 struct c_can_priv *priv = netdev_priv(dev);
700 struct net_device_stats *stats = &dev->stats;
701 u32 idx, obj, pkts = 0, bytes = 0, pend, clr;
703 if (priv->msg_obj_tx_last > 32)
704 pend = priv->read_reg32(priv, C_CAN_INTPND3_REG);
706 pend = priv->read_reg(priv, C_CAN_INTPND2_REG);
709 while ((idx = ffs(pend))) {
712 obj = idx + priv->msg_obj_tx_first;
714 /* We use IF_RX interface instead of IF_TX because we
715 * are called from c_can_poll(), which runs inside
716 * NAPI. We are not trasmitting.
718 c_can_inval_tx_object(dev, IF_RX, obj);
719 can_get_echo_skb(dev, idx, NULL);
720 bytes += priv->dlc[idx];
724 /* Clear the bits in the tx_active mask */
725 atomic_sub(clr, &priv->tx_active);
727 if (clr & BIT(priv->msg_obj_tx_num - 1))
728 netif_wake_queue(dev);
731 stats->tx_bytes += bytes;
732 stats->tx_packets += pkts;
733 can_led_event(dev, CAN_LED_EVENT_TX);
737 /* If we have a gap in the pending bits, that means we either
738 * raced with the hardware or failed to readout all upper
739 * objects in the last run due to quota limit.
741 static u32 c_can_adjust_pending(u32 pend, u32 rx_mask)
748 /* If the last set bit is larger than the number of pending
749 * bits we have a gap.
751 weight = hweight32(pend);
754 /* If the bits are linear, nothing to do */
758 /* Find the first set bit after the gap. We walk backwards
759 * from the last set bit.
761 for (lasts--; pend & BIT(lasts - 1); lasts--)
764 return pend & ~GENMASK(lasts - 1, 0);
767 static inline void c_can_rx_object_get(struct net_device *dev,
768 struct c_can_priv *priv, u32 obj)
770 c_can_object_get(dev, IF_RX, obj, priv->comm_rcv_high);
773 static inline void c_can_rx_finalize(struct net_device *dev,
774 struct c_can_priv *priv, u32 obj)
776 if (priv->type != BOSCH_D_CAN)
777 c_can_object_get(dev, IF_RX, obj, IF_COMM_CLR_NEWDAT);
780 static int c_can_read_objects(struct net_device *dev, struct c_can_priv *priv,
783 u32 pkts = 0, ctrl, obj;
785 while ((obj = ffs(pend)) && quota > 0) {
786 pend &= ~BIT(obj - 1);
788 c_can_rx_object_get(dev, priv, obj);
789 ctrl = priv->read_reg(priv, C_CAN_IFACE(MSGCTRL_REG, IF_RX));
791 if (ctrl & IF_MCONT_MSGLST) {
792 int n = c_can_handle_lost_msg_obj(dev, IF_RX, obj, ctrl);
799 /* This really should not happen, but this covers some
800 * odd HW behaviour. Do not remove that unless you
801 * want to brick your machine.
803 if (!(ctrl & IF_MCONT_NEWDAT))
806 /* read the data from the message object */
807 c_can_read_msg_object(dev, IF_RX, ctrl);
809 c_can_rx_finalize(dev, priv, obj);
818 static inline u32 c_can_get_pending(struct c_can_priv *priv)
822 if (priv->msg_obj_rx_last > 16)
823 pend = priv->read_reg32(priv, C_CAN_NEWDAT1_REG);
825 pend = priv->read_reg(priv, C_CAN_NEWDAT1_REG);
830 /* theory of operation:
832 * c_can core saves a received CAN message into the first free message
833 * object it finds free (starting with the lowest). Bits NEWDAT and
834 * INTPND are set for this message object indicating that a new message
837 * We clear the newdat bit right away.
839 * This can result in packet reordering when the readout is slow.
841 static int c_can_do_rx_poll(struct net_device *dev, int quota)
843 struct c_can_priv *priv = netdev_priv(dev);
844 u32 pkts = 0, pend = 0, toread, n;
848 pend = c_can_get_pending(priv);
851 /* If the pending field has a gap, handle the
852 * bits above the gap first.
854 toread = c_can_adjust_pending(pend,
855 priv->msg_obj_rx_mask);
859 /* Remove the bits from pend */
861 /* Read the objects */
862 n = c_can_read_objects(dev, priv, toread, quota);
868 can_led_event(dev, CAN_LED_EVENT_RX);
873 static int c_can_handle_state_change(struct net_device *dev,
874 enum c_can_bus_error_types error_type)
876 unsigned int reg_err_counter;
877 unsigned int rx_err_passive;
878 struct c_can_priv *priv = netdev_priv(dev);
879 struct net_device_stats *stats = &dev->stats;
880 struct can_frame *cf;
882 struct can_berr_counter bec;
884 switch (error_type) {
886 priv->can.state = CAN_STATE_ERROR_ACTIVE;
888 case C_CAN_ERROR_WARNING:
889 /* error warning state */
890 priv->can.can_stats.error_warning++;
891 priv->can.state = CAN_STATE_ERROR_WARNING;
893 case C_CAN_ERROR_PASSIVE:
894 /* error passive state */
895 priv->can.can_stats.error_passive++;
896 priv->can.state = CAN_STATE_ERROR_PASSIVE;
900 priv->can.state = CAN_STATE_BUS_OFF;
901 priv->can.can_stats.bus_off++;
907 /* propagate the error condition to the CAN stack */
908 skb = alloc_can_err_skb(dev, &cf);
912 __c_can_get_berr_counter(dev, &bec);
913 reg_err_counter = priv->read_reg(priv, C_CAN_ERR_CNT_REG);
914 rx_err_passive = (reg_err_counter & ERR_CNT_RP_MASK) >>
917 switch (error_type) {
919 /* error warning state */
920 cf->can_id |= CAN_ERR_CRTL;
921 cf->data[1] = CAN_ERR_CRTL_ACTIVE;
922 cf->data[6] = bec.txerr;
923 cf->data[7] = bec.rxerr;
925 case C_CAN_ERROR_WARNING:
926 /* error warning state */
927 cf->can_id |= CAN_ERR_CRTL;
928 cf->data[1] = (bec.txerr > bec.rxerr) ?
929 CAN_ERR_CRTL_TX_WARNING :
930 CAN_ERR_CRTL_RX_WARNING;
931 cf->data[6] = bec.txerr;
932 cf->data[7] = bec.rxerr;
935 case C_CAN_ERROR_PASSIVE:
936 /* error passive state */
937 cf->can_id |= CAN_ERR_CRTL;
939 cf->data[1] |= CAN_ERR_CRTL_RX_PASSIVE;
941 cf->data[1] |= CAN_ERR_CRTL_TX_PASSIVE;
943 cf->data[6] = bec.txerr;
944 cf->data[7] = bec.rxerr;
948 cf->can_id |= CAN_ERR_BUSOFF;
956 stats->rx_bytes += cf->len;
957 netif_receive_skb(skb);
962 static int c_can_handle_bus_err(struct net_device *dev,
963 enum c_can_lec_type lec_type)
965 struct c_can_priv *priv = netdev_priv(dev);
966 struct net_device_stats *stats = &dev->stats;
967 struct can_frame *cf;
970 /* early exit if no lec update or no error.
971 * no lec update means that no CAN bus event has been detected
972 * since CPU wrote 0x7 value to status reg.
974 if (lec_type == LEC_UNUSED || lec_type == LEC_NO_ERROR)
977 if (!(priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING))
980 /* common for all type of bus errors */
981 priv->can.can_stats.bus_error++;
984 /* propagate the error condition to the CAN stack */
985 skb = alloc_can_err_skb(dev, &cf);
989 /* check for 'last error code' which tells us the
990 * type of the last error to occur on the CAN bus
992 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
995 case LEC_STUFF_ERROR:
996 netdev_dbg(dev, "stuff error\n");
997 cf->data[2] |= CAN_ERR_PROT_STUFF;
1000 netdev_dbg(dev, "form error\n");
1001 cf->data[2] |= CAN_ERR_PROT_FORM;
1004 netdev_dbg(dev, "ack error\n");
1005 cf->data[3] = CAN_ERR_PROT_LOC_ACK;
1007 case LEC_BIT1_ERROR:
1008 netdev_dbg(dev, "bit1 error\n");
1009 cf->data[2] |= CAN_ERR_PROT_BIT1;
1011 case LEC_BIT0_ERROR:
1012 netdev_dbg(dev, "bit0 error\n");
1013 cf->data[2] |= CAN_ERR_PROT_BIT0;
1016 netdev_dbg(dev, "CRC error\n");
1017 cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ;
1023 stats->rx_packets++;
1024 stats->rx_bytes += cf->len;
1025 netif_receive_skb(skb);
1029 static int c_can_poll(struct napi_struct *napi, int quota)
1031 struct net_device *dev = napi->dev;
1032 struct c_can_priv *priv = netdev_priv(dev);
1033 u16 curr, last = priv->last_status;
1036 /* Only read the status register if a status interrupt was pending */
1037 if (atomic_xchg(&priv->sie_pending, 0)) {
1038 priv->last_status = priv->read_reg(priv, C_CAN_STS_REG);
1039 curr = priv->last_status;
1040 /* Ack status on C_CAN. D_CAN is self clearing */
1041 if (priv->type != BOSCH_D_CAN)
1042 priv->write_reg(priv, C_CAN_STS_REG, LEC_UNUSED);
1044 /* no change detected ... */
1048 /* handle state changes */
1049 if ((curr & STATUS_EWARN) && (!(last & STATUS_EWARN))) {
1050 netdev_dbg(dev, "entered error warning state\n");
1051 work_done += c_can_handle_state_change(dev, C_CAN_ERROR_WARNING);
1054 if ((curr & STATUS_EPASS) && (!(last & STATUS_EPASS))) {
1055 netdev_dbg(dev, "entered error passive state\n");
1056 work_done += c_can_handle_state_change(dev, C_CAN_ERROR_PASSIVE);
1059 if ((curr & STATUS_BOFF) && (!(last & STATUS_BOFF))) {
1060 netdev_dbg(dev, "entered bus off state\n");
1061 work_done += c_can_handle_state_change(dev, C_CAN_BUS_OFF);
1065 /* handle bus recovery events */
1066 if ((!(curr & STATUS_BOFF)) && (last & STATUS_BOFF)) {
1067 netdev_dbg(dev, "left bus off state\n");
1068 work_done += c_can_handle_state_change(dev, C_CAN_ERROR_PASSIVE);
1071 if ((!(curr & STATUS_EPASS)) && (last & STATUS_EPASS)) {
1072 netdev_dbg(dev, "left error passive state\n");
1073 work_done += c_can_handle_state_change(dev, C_CAN_ERROR_WARNING);
1076 if ((!(curr & STATUS_EWARN)) && (last & STATUS_EWARN)) {
1077 netdev_dbg(dev, "left error warning state\n");
1078 work_done += c_can_handle_state_change(dev, C_CAN_NO_ERROR);
1081 /* handle lec errors on the bus */
1082 work_done += c_can_handle_bus_err(dev, curr & LEC_MASK);
1084 /* Handle Tx/Rx events. We do this unconditionally */
1085 work_done += c_can_do_rx_poll(dev, (quota - work_done));
1089 if (work_done < quota) {
1090 napi_complete_done(napi, work_done);
1091 /* enable all IRQs if we are not in bus off state */
1092 if (priv->can.state != CAN_STATE_BUS_OFF)
1093 c_can_irq_control(priv, true);
1099 static irqreturn_t c_can_isr(int irq, void *dev_id)
1101 struct net_device *dev = (struct net_device *)dev_id;
1102 struct c_can_priv *priv = netdev_priv(dev);
1105 reg_int = priv->read_reg(priv, C_CAN_INT_REG);
1109 /* save for later use */
1110 if (reg_int & INT_STS_PENDING)
1111 atomic_set(&priv->sie_pending, 1);
1113 /* disable all interrupts and schedule the NAPI */
1114 c_can_irq_control(priv, false);
1115 napi_schedule(&priv->napi);
1120 static int c_can_open(struct net_device *dev)
1123 struct c_can_priv *priv = netdev_priv(dev);
1125 c_can_pm_runtime_get_sync(priv);
1126 c_can_reset_ram(priv, true);
1128 /* open the can device */
1129 err = open_candev(dev);
1131 netdev_err(dev, "failed to open can device\n");
1132 goto exit_open_fail;
1135 /* register interrupt handler */
1136 err = request_irq(dev->irq, &c_can_isr, IRQF_SHARED, dev->name,
1139 netdev_err(dev, "failed to request interrupt\n");
1143 /* start the c_can controller */
1144 err = c_can_start(dev);
1146 goto exit_start_fail;
1148 can_led_event(dev, CAN_LED_EVENT_OPEN);
1150 napi_enable(&priv->napi);
1151 /* enable status change, error and module interrupts */
1152 c_can_irq_control(priv, true);
1153 netif_start_queue(dev);
1158 free_irq(dev->irq, dev);
1162 c_can_reset_ram(priv, false);
1163 c_can_pm_runtime_put_sync(priv);
1167 static int c_can_close(struct net_device *dev)
1169 struct c_can_priv *priv = netdev_priv(dev);
1171 netif_stop_queue(dev);
1172 napi_disable(&priv->napi);
1174 free_irq(dev->irq, dev);
1177 c_can_reset_ram(priv, false);
1178 c_can_pm_runtime_put_sync(priv);
1180 can_led_event(dev, CAN_LED_EVENT_STOP);
1185 struct net_device *alloc_c_can_dev(int msg_obj_num)
1187 struct net_device *dev;
1188 struct c_can_priv *priv;
1189 int msg_obj_tx_num = msg_obj_num / 2;
1191 dev = alloc_candev(struct_size(priv, dlc, msg_obj_tx_num),
1196 priv = netdev_priv(dev);
1197 priv->msg_obj_num = msg_obj_num;
1198 priv->msg_obj_rx_num = msg_obj_num - msg_obj_tx_num;
1199 priv->msg_obj_rx_first = 1;
1200 priv->msg_obj_rx_last =
1201 priv->msg_obj_rx_first + priv->msg_obj_rx_num - 1;
1202 priv->msg_obj_rx_mask = GENMASK(priv->msg_obj_rx_num - 1, 0);
1204 priv->msg_obj_tx_num = msg_obj_tx_num;
1205 priv->msg_obj_tx_first = priv->msg_obj_rx_last + 1;
1206 priv->msg_obj_tx_last =
1207 priv->msg_obj_tx_first + priv->msg_obj_tx_num - 1;
1209 netif_napi_add(dev, &priv->napi, c_can_poll, priv->msg_obj_rx_num);
1212 priv->can.bittiming_const = &c_can_bittiming_const;
1213 priv->can.do_set_mode = c_can_set_mode;
1214 priv->can.do_get_berr_counter = c_can_get_berr_counter;
1215 priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK |
1216 CAN_CTRLMODE_LISTENONLY |
1217 CAN_CTRLMODE_BERR_REPORTING;
1221 EXPORT_SYMBOL_GPL(alloc_c_can_dev);
1224 int c_can_power_down(struct net_device *dev)
1227 unsigned long time_out;
1228 struct c_can_priv *priv = netdev_priv(dev);
1230 if (!(dev->flags & IFF_UP))
1233 WARN_ON(priv->type != BOSCH_D_CAN);
1235 /* set PDR value so the device goes to power down mode */
1236 val = priv->read_reg(priv, C_CAN_CTRL_EX_REG);
1237 val |= CONTROL_EX_PDR;
1238 priv->write_reg(priv, C_CAN_CTRL_EX_REG, val);
1240 /* Wait for the PDA bit to get set */
1241 time_out = jiffies + msecs_to_jiffies(INIT_WAIT_MS);
1242 while (!(priv->read_reg(priv, C_CAN_STS_REG) & STATUS_PDA) &&
1243 time_after(time_out, jiffies))
1246 if (time_after(jiffies, time_out))
1251 c_can_reset_ram(priv, false);
1252 c_can_pm_runtime_put_sync(priv);
1256 EXPORT_SYMBOL_GPL(c_can_power_down);
1258 int c_can_power_up(struct net_device *dev)
1261 unsigned long time_out;
1262 struct c_can_priv *priv = netdev_priv(dev);
1265 if (!(dev->flags & IFF_UP))
1268 WARN_ON(priv->type != BOSCH_D_CAN);
1270 c_can_pm_runtime_get_sync(priv);
1271 c_can_reset_ram(priv, true);
1273 /* Clear PDR and INIT bits */
1274 val = priv->read_reg(priv, C_CAN_CTRL_EX_REG);
1275 val &= ~CONTROL_EX_PDR;
1276 priv->write_reg(priv, C_CAN_CTRL_EX_REG, val);
1277 val = priv->read_reg(priv, C_CAN_CTRL_REG);
1278 val &= ~CONTROL_INIT;
1279 priv->write_reg(priv, C_CAN_CTRL_REG, val);
1281 /* Wait for the PDA bit to get clear */
1282 time_out = jiffies + msecs_to_jiffies(INIT_WAIT_MS);
1283 while ((priv->read_reg(priv, C_CAN_STS_REG) & STATUS_PDA) &&
1284 time_after(time_out, jiffies))
1287 if (time_after(jiffies, time_out)) {
1292 ret = c_can_start(dev);
1296 c_can_irq_control(priv, true);
1301 c_can_reset_ram(priv, false);
1302 c_can_pm_runtime_put_sync(priv);
1306 EXPORT_SYMBOL_GPL(c_can_power_up);
1309 void free_c_can_dev(struct net_device *dev)
1311 struct c_can_priv *priv = netdev_priv(dev);
1313 netif_napi_del(&priv->napi);
1316 EXPORT_SYMBOL_GPL(free_c_can_dev);
1318 static const struct net_device_ops c_can_netdev_ops = {
1319 .ndo_open = c_can_open,
1320 .ndo_stop = c_can_close,
1321 .ndo_start_xmit = c_can_start_xmit,
1322 .ndo_change_mtu = can_change_mtu,
1325 int register_c_can_dev(struct net_device *dev)
1329 /* Deactivate pins to prevent DRA7 DCAN IP from being
1330 * stuck in transition when module is disabled.
1331 * Pins are activated in c_can_start() and deactivated
1334 pinctrl_pm_select_sleep_state(dev->dev.parent);
1336 dev->flags |= IFF_ECHO; /* we support local echo */
1337 dev->netdev_ops = &c_can_netdev_ops;
1339 err = register_candev(dev);
1341 devm_can_led_init(dev);
1344 EXPORT_SYMBOL_GPL(register_c_can_dev);
1346 void unregister_c_can_dev(struct net_device *dev)
1348 unregister_candev(dev);
1350 EXPORT_SYMBOL_GPL(unregister_c_can_dev);
1353 MODULE_LICENSE("GPL v2");
1354 MODULE_DESCRIPTION("CAN bus driver for Bosch C_CAN controller");