2 * CAN bus driver for Bosch C_CAN controller
4 * Copyright (C) 2010 ST Microelectronics
5 * Bhupesh Sharma <bhupesh.sharma@st.com>
7 * Borrowed heavily from the C_CAN driver originally written by:
9 * - Sascha Hauer, Marc Kleine-Budde, Pengutronix <s.hauer@pengutronix.de>
10 * - Simon Kallweit, intefo AG <simon.kallweit@intefo.ch>
12 * TX and RX NAPI implementation has been borrowed from at91 CAN driver
15 * (C) 2007 by Hans J. Koch <hjk@hansjkoch.de>
16 * (C) 2008, 2009 by Marc Kleine-Budde <kernel@pengutronix.de>
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)
136 /* Receive setup of message objects */
137 #define IF_COMM_RCV_SETUP (IF_COMM_MASK | IF_COMM_ARB | IF_COMM_CONTROL)
139 /* Invalidation of message objects */
140 #define IF_COMM_INVAL (IF_COMM_ARB | IF_COMM_CONTROL)
142 /* IFx arbitration */
143 #define IF_ARB_MSGVAL BIT(31)
144 #define IF_ARB_MSGXTD BIT(30)
145 #define IF_ARB_TRANSMIT BIT(29)
147 /* IFx message control */
148 #define IF_MCONT_NEWDAT BIT(15)
149 #define IF_MCONT_MSGLST BIT(14)
150 #define IF_MCONT_INTPND BIT(13)
151 #define IF_MCONT_UMASK BIT(12)
152 #define IF_MCONT_TXIE BIT(11)
153 #define IF_MCONT_RXIE BIT(10)
154 #define IF_MCONT_RMTEN BIT(9)
155 #define IF_MCONT_TXRQST BIT(8)
156 #define IF_MCONT_EOB BIT(7)
157 #define IF_MCONT_DLC_MASK 0xf
159 #define IF_MCONT_RCV (IF_MCONT_RXIE | IF_MCONT_UMASK)
160 #define IF_MCONT_RCV_EOB (IF_MCONT_RCV | IF_MCONT_EOB)
162 #define IF_MCONT_TX (IF_MCONT_TXIE | IF_MCONT_EOB)
165 * Use IF1 for RX and IF2 for TX
170 /* minimum timeout for checking BUSY status */
171 #define MIN_TIMEOUT_VALUE 6
173 /* Wait for ~1 sec for INIT bit */
174 #define INIT_WAIT_MS 1000
177 #define C_CAN_NAPI_WEIGHT C_CAN_MSG_OBJ_RX_NUM
179 /* c_can lec values */
180 enum c_can_lec_type
{
189 LEC_MASK
= LEC_UNUSED
,
194 * Bus errors (BUS_OFF, ERROR_WARNING, ERROR_PASSIVE) are supported
196 enum c_can_bus_error_types
{
203 static const struct can_bittiming_const c_can_bittiming_const
= {
204 .name
= KBUILD_MODNAME
,
205 .tseg1_min
= 2, /* Time segment 1 = prop_seg + phase_seg1 */
207 .tseg2_min
= 1, /* Time segment 2 = phase_seg2 */
211 .brp_max
= 1024, /* 6-bit BRP field + 4-bit BRPE field*/
215 static inline void c_can_pm_runtime_enable(const struct c_can_priv
*priv
)
218 pm_runtime_enable(priv
->device
);
221 static inline void c_can_pm_runtime_disable(const struct c_can_priv
*priv
)
224 pm_runtime_disable(priv
->device
);
227 static inline void c_can_pm_runtime_get_sync(const struct c_can_priv
*priv
)
230 pm_runtime_get_sync(priv
->device
);
233 static inline void c_can_pm_runtime_put_sync(const struct c_can_priv
*priv
)
236 pm_runtime_put_sync(priv
->device
);
239 static inline void c_can_reset_ram(const struct c_can_priv
*priv
, bool enable
)
242 priv
->raminit(priv
, enable
);
245 static void c_can_irq_control(struct c_can_priv
*priv
, bool enable
)
247 u32 ctrl
= priv
->read_reg(priv
, C_CAN_CTRL_REG
) & ~CONTROL_IRQMSK
;
250 ctrl
|= CONTROL_IRQMSK
;
252 priv
->write_reg(priv
, C_CAN_CTRL_REG
, ctrl
);
255 static void c_can_obj_update(struct net_device
*dev
, int iface
, u32 cmd
, u32 obj
)
257 struct c_can_priv
*priv
= netdev_priv(dev
);
258 int cnt
, reg
= C_CAN_IFACE(COMREQ_REG
, iface
);
260 priv
->write_reg32(priv
, reg
, (cmd
<< 16) | obj
);
262 for (cnt
= MIN_TIMEOUT_VALUE
; cnt
; cnt
--) {
263 if (!(priv
->read_reg(priv
, reg
) & IF_COMR_BUSY
))
267 netdev_err(dev
, "Updating object timed out\n");
271 static inline void c_can_object_get(struct net_device
*dev
, int iface
,
274 c_can_obj_update(dev
, iface
, cmd
, obj
);
277 static inline void c_can_object_put(struct net_device
*dev
, int iface
,
280 c_can_obj_update(dev
, iface
, cmd
| IF_COMM_WR
, obj
);
284 * Note: According to documentation clearing TXIE while MSGVAL is set
285 * is not allowed, but works nicely on C/DCAN. And that lowers the I/O
286 * load significantly.
288 static void c_can_inval_tx_object(struct net_device
*dev
, int iface
, int obj
)
290 struct c_can_priv
*priv
= netdev_priv(dev
);
292 priv
->write_reg(priv
, C_CAN_IFACE(MSGCTRL_REG
, iface
), 0);
293 c_can_object_put(dev
, iface
, obj
, IF_COMM_INVAL
);
296 static void c_can_inval_msg_object(struct net_device
*dev
, int iface
, int obj
)
298 struct c_can_priv
*priv
= netdev_priv(dev
);
300 priv
->write_reg(priv
, C_CAN_IFACE(ARB1_REG
, iface
), 0);
301 priv
->write_reg(priv
, C_CAN_IFACE(ARB2_REG
, iface
), 0);
302 c_can_inval_tx_object(dev
, iface
, obj
);
305 static void c_can_setup_tx_object(struct net_device
*dev
, int iface
,
306 struct can_frame
*frame
, int idx
)
308 struct c_can_priv
*priv
= netdev_priv(dev
);
309 u16 ctrl
= IF_MCONT_TX
| frame
->can_dlc
;
310 bool rtr
= frame
->can_id
& CAN_RTR_FLAG
;
311 u32 arb
= IF_ARB_MSGVAL
;
314 if (frame
->can_id
& CAN_EFF_FLAG
) {
315 arb
|= frame
->can_id
& CAN_EFF_MASK
;
316 arb
|= IF_ARB_MSGXTD
;
318 arb
|= (frame
->can_id
& CAN_SFF_MASK
) << 18;
322 arb
|= IF_ARB_TRANSMIT
;
325 * If we change the DIR bit, we need to invalidate the buffer
326 * first, i.e. clear the MSGVAL flag in the arbiter.
328 if (rtr
!= (bool)test_bit(idx
, &priv
->tx_dir
)) {
329 u32 obj
= idx
+ C_CAN_MSG_OBJ_TX_FIRST
;
331 c_can_inval_msg_object(dev
, iface
, obj
);
332 change_bit(idx
, &priv
->tx_dir
);
335 priv
->write_reg32(priv
, C_CAN_IFACE(ARB1_REG
, iface
), arb
);
337 priv
->write_reg(priv
, C_CAN_IFACE(MSGCTRL_REG
, iface
), ctrl
);
339 if (priv
->type
== BOSCH_D_CAN
) {
340 u32 data
= 0, dreg
= C_CAN_IFACE(DATA1_REG
, iface
);
342 for (i
= 0; i
< frame
->can_dlc
; i
+= 4, dreg
+= 2) {
343 data
= (u32
)frame
->data
[i
];
344 data
|= (u32
)frame
->data
[i
+ 1] << 8;
345 data
|= (u32
)frame
->data
[i
+ 2] << 16;
346 data
|= (u32
)frame
->data
[i
+ 3] << 24;
347 priv
->write_reg32(priv
, dreg
, data
);
350 for (i
= 0; i
< frame
->can_dlc
; i
+= 2) {
351 priv
->write_reg(priv
,
352 C_CAN_IFACE(DATA1_REG
, iface
) + i
/ 2,
354 (frame
->data
[i
+ 1] << 8));
359 static inline void c_can_activate_all_lower_rx_msg_obj(struct net_device
*dev
,
364 for (i
= C_CAN_MSG_OBJ_RX_FIRST
; i
<= C_CAN_MSG_RX_LOW_LAST
; i
++)
365 c_can_object_get(dev
, iface
, i
, IF_COMM_CLR_NEWDAT
);
368 static int c_can_handle_lost_msg_obj(struct net_device
*dev
,
369 int iface
, int objno
, u32 ctrl
)
371 struct net_device_stats
*stats
= &dev
->stats
;
372 struct c_can_priv
*priv
= netdev_priv(dev
);
373 struct can_frame
*frame
;
376 ctrl
&= ~(IF_MCONT_MSGLST
| IF_MCONT_INTPND
| IF_MCONT_NEWDAT
);
377 priv
->write_reg(priv
, C_CAN_IFACE(MSGCTRL_REG
, iface
), ctrl
);
378 c_can_object_put(dev
, iface
, objno
, IF_COMM_CONTROL
);
381 stats
->rx_over_errors
++;
383 /* create an error msg */
384 skb
= alloc_can_err_skb(dev
, &frame
);
388 frame
->can_id
|= CAN_ERR_CRTL
;
389 frame
->data
[1] = CAN_ERR_CRTL_RX_OVERFLOW
;
391 netif_receive_skb(skb
);
395 static int c_can_read_msg_object(struct net_device
*dev
, int iface
, u32 ctrl
)
397 struct net_device_stats
*stats
= &dev
->stats
;
398 struct c_can_priv
*priv
= netdev_priv(dev
);
399 struct can_frame
*frame
;
403 skb
= alloc_can_skb(dev
, &frame
);
409 frame
->can_dlc
= get_can_dlc(ctrl
& 0x0F);
411 arb
= priv
->read_reg32(priv
, C_CAN_IFACE(ARB1_REG
, iface
));
413 if (arb
& IF_ARB_MSGXTD
)
414 frame
->can_id
= (arb
& CAN_EFF_MASK
) | CAN_EFF_FLAG
;
416 frame
->can_id
= (arb
>> 18) & CAN_SFF_MASK
;
418 if (arb
& IF_ARB_TRANSMIT
) {
419 frame
->can_id
|= CAN_RTR_FLAG
;
421 int i
, dreg
= C_CAN_IFACE(DATA1_REG
, iface
);
423 if (priv
->type
== BOSCH_D_CAN
) {
424 for (i
= 0; i
< frame
->can_dlc
; i
+= 4, dreg
+= 2) {
425 data
= priv
->read_reg32(priv
, dreg
);
426 frame
->data
[i
] = data
;
427 frame
->data
[i
+ 1] = data
>> 8;
428 frame
->data
[i
+ 2] = data
>> 16;
429 frame
->data
[i
+ 3] = data
>> 24;
432 for (i
= 0; i
< frame
->can_dlc
; i
+= 2, dreg
++) {
433 data
= priv
->read_reg(priv
, dreg
);
434 frame
->data
[i
] = data
;
435 frame
->data
[i
+ 1] = data
>> 8;
441 stats
->rx_bytes
+= frame
->can_dlc
;
443 netif_receive_skb(skb
);
447 static void c_can_setup_receive_object(struct net_device
*dev
, int iface
,
448 u32 obj
, u32 mask
, u32 id
, u32 mcont
)
450 struct c_can_priv
*priv
= netdev_priv(dev
);
453 priv
->write_reg32(priv
, C_CAN_IFACE(MASK1_REG
, iface
), mask
);
456 priv
->write_reg32(priv
, C_CAN_IFACE(ARB1_REG
, iface
), id
);
458 priv
->write_reg(priv
, C_CAN_IFACE(MSGCTRL_REG
, iface
), mcont
);
459 c_can_object_put(dev
, iface
, obj
, IF_COMM_RCV_SETUP
);
462 static netdev_tx_t
c_can_start_xmit(struct sk_buff
*skb
,
463 struct net_device
*dev
)
465 struct can_frame
*frame
= (struct can_frame
*)skb
->data
;
466 struct c_can_priv
*priv
= netdev_priv(dev
);
469 if (can_dropped_invalid_skb(dev
, skb
))
472 * This is not a FIFO. C/D_CAN sends out the buffers
473 * prioritized. The lowest buffer number wins.
475 idx
= fls(atomic_read(&priv
->tx_active
));
476 obj
= idx
+ C_CAN_MSG_OBJ_TX_FIRST
;
478 /* If this is the last buffer, stop the xmit queue */
479 if (idx
== C_CAN_MSG_OBJ_TX_NUM
- 1)
480 netif_stop_queue(dev
);
482 * Store the message in the interface so we can call
483 * can_put_echo_skb(). We must do this before we enable
484 * transmit as we might race against do_tx().
486 c_can_setup_tx_object(dev
, IF_TX
, frame
, idx
);
487 priv
->dlc
[idx
] = frame
->can_dlc
;
488 can_put_echo_skb(skb
, dev
, idx
);
490 /* Update the active bits */
491 atomic_add((1 << idx
), &priv
->tx_active
);
492 /* Start transmission */
493 c_can_object_put(dev
, IF_TX
, obj
, IF_COMM_TX
);
498 static int c_can_wait_for_ctrl_init(struct net_device
*dev
,
499 struct c_can_priv
*priv
, u32 init
)
503 while (init
!= (priv
->read_reg(priv
, C_CAN_CTRL_REG
) & CONTROL_INIT
)) {
505 if (retry
++ > 1000) {
506 netdev_err(dev
, "CCTRL: set CONTROL_INIT failed\n");
513 static int c_can_set_bittiming(struct net_device
*dev
)
515 unsigned int reg_btr
, reg_brpe
, ctrl_save
;
516 u8 brp
, brpe
, sjw
, tseg1
, tseg2
;
518 struct c_can_priv
*priv
= netdev_priv(dev
);
519 const struct can_bittiming
*bt
= &priv
->can
.bittiming
;
522 /* c_can provides a 6-bit brp and 4-bit brpe fields */
523 ten_bit_brp
= bt
->brp
- 1;
524 brp
= ten_bit_brp
& BTR_BRP_MASK
;
525 brpe
= ten_bit_brp
>> 6;
528 tseg1
= bt
->prop_seg
+ bt
->phase_seg1
- 1;
529 tseg2
= bt
->phase_seg2
- 1;
530 reg_btr
= brp
| (sjw
<< BTR_SJW_SHIFT
) | (tseg1
<< BTR_TSEG1_SHIFT
) |
531 (tseg2
<< BTR_TSEG2_SHIFT
);
532 reg_brpe
= brpe
& BRP_EXT_BRPE_MASK
;
535 "setting BTR=%04x BRPE=%04x\n", reg_btr
, reg_brpe
);
537 ctrl_save
= priv
->read_reg(priv
, C_CAN_CTRL_REG
);
538 ctrl_save
&= ~CONTROL_INIT
;
539 priv
->write_reg(priv
, C_CAN_CTRL_REG
, CONTROL_CCE
| CONTROL_INIT
);
540 res
= c_can_wait_for_ctrl_init(dev
, priv
, CONTROL_INIT
);
544 priv
->write_reg(priv
, C_CAN_BTR_REG
, reg_btr
);
545 priv
->write_reg(priv
, C_CAN_BRPEXT_REG
, reg_brpe
);
546 priv
->write_reg(priv
, C_CAN_CTRL_REG
, ctrl_save
);
548 return c_can_wait_for_ctrl_init(dev
, priv
, 0);
552 * Configure C_CAN message objects for Tx and Rx purposes:
553 * C_CAN provides a total of 32 message objects that can be configured
554 * either for Tx or Rx purposes. Here the first 16 message objects are used as
555 * a reception FIFO. The end of reception FIFO is signified by the EoB bit
556 * being SET. The remaining 16 message objects are kept aside for Tx purposes.
557 * See user guide document for further details on configuring message
560 static void c_can_configure_msg_objects(struct net_device
*dev
)
564 /* first invalidate all message objects */
565 for (i
= C_CAN_MSG_OBJ_RX_FIRST
; i
<= C_CAN_NO_OF_OBJECTS
; i
++)
566 c_can_inval_msg_object(dev
, IF_RX
, i
);
568 /* setup receive message objects */
569 for (i
= C_CAN_MSG_OBJ_RX_FIRST
; i
< C_CAN_MSG_OBJ_RX_LAST
; i
++)
570 c_can_setup_receive_object(dev
, IF_RX
, i
, 0, 0, IF_MCONT_RCV
);
572 c_can_setup_receive_object(dev
, IF_RX
, C_CAN_MSG_OBJ_RX_LAST
, 0, 0,
576 static int c_can_software_reset(struct net_device
*dev
)
578 struct c_can_priv
*priv
= netdev_priv(dev
);
581 if (priv
->type
!= BOSCH_D_CAN
)
584 priv
->write_reg(priv
, C_CAN_CTRL_REG
, CONTROL_SWR
| CONTROL_INIT
);
585 while (priv
->read_reg(priv
, C_CAN_CTRL_REG
) & CONTROL_SWR
) {
588 netdev_err(dev
, "CCTRL: software reset failed\n");
597 * Configure C_CAN chip:
598 * - enable/disable auto-retransmission
599 * - set operating mode
600 * - configure message objects
602 static int c_can_chip_config(struct net_device
*dev
)
604 struct c_can_priv
*priv
= netdev_priv(dev
);
607 err
= c_can_software_reset(dev
);
611 /* enable automatic retransmission */
612 priv
->write_reg(priv
, C_CAN_CTRL_REG
, CONTROL_ENABLE_AR
);
614 if ((priv
->can
.ctrlmode
& CAN_CTRLMODE_LISTENONLY
) &&
615 (priv
->can
.ctrlmode
& CAN_CTRLMODE_LOOPBACK
)) {
616 /* loopback + silent mode : useful for hot self-test */
617 priv
->write_reg(priv
, C_CAN_CTRL_REG
, CONTROL_TEST
);
618 priv
->write_reg(priv
, C_CAN_TEST_REG
, TEST_LBACK
| TEST_SILENT
);
619 } else if (priv
->can
.ctrlmode
& CAN_CTRLMODE_LOOPBACK
) {
620 /* loopback mode : useful for self-test function */
621 priv
->write_reg(priv
, C_CAN_CTRL_REG
, CONTROL_TEST
);
622 priv
->write_reg(priv
, C_CAN_TEST_REG
, TEST_LBACK
);
623 } else if (priv
->can
.ctrlmode
& CAN_CTRLMODE_LISTENONLY
) {
624 /* silent mode : bus-monitoring mode */
625 priv
->write_reg(priv
, C_CAN_CTRL_REG
, CONTROL_TEST
);
626 priv
->write_reg(priv
, C_CAN_TEST_REG
, TEST_SILENT
);
629 /* configure message objects */
630 c_can_configure_msg_objects(dev
);
632 /* set a `lec` value so that we can check for updates later */
633 priv
->write_reg(priv
, C_CAN_STS_REG
, LEC_UNUSED
);
635 /* Clear all internal status */
636 atomic_set(&priv
->tx_active
, 0);
640 /* set bittiming params */
641 return c_can_set_bittiming(dev
);
644 static int c_can_start(struct net_device
*dev
)
646 struct c_can_priv
*priv
= netdev_priv(dev
);
650 /* basic c_can configuration */
651 err
= c_can_chip_config(dev
);
655 /* Setup the command for new messages */
656 priv
->comm_rcv_high
= priv
->type
!= BOSCH_D_CAN
?
657 IF_COMM_RCV_LOW
: IF_COMM_RCV_HIGH
;
659 priv
->can
.state
= CAN_STATE_ERROR_ACTIVE
;
661 /* Attempt to use "active" if available else use "default" */
662 p
= pinctrl_get_select(priv
->device
, "active");
666 pinctrl_pm_select_default_state(priv
->device
);
671 static void c_can_stop(struct net_device
*dev
)
673 struct c_can_priv
*priv
= netdev_priv(dev
);
675 c_can_irq_control(priv
, false);
677 /* put ctrl to init on stop to end ongoing transmission */
678 priv
->write_reg(priv
, C_CAN_CTRL_REG
, CONTROL_INIT
);
680 /* deactivate pins */
681 pinctrl_pm_select_sleep_state(dev
->dev
.parent
);
682 priv
->can
.state
= CAN_STATE_STOPPED
;
685 static int c_can_set_mode(struct net_device
*dev
, enum can_mode mode
)
687 struct c_can_priv
*priv
= netdev_priv(dev
);
692 err
= c_can_start(dev
);
695 netif_wake_queue(dev
);
696 c_can_irq_control(priv
, true);
705 static int __c_can_get_berr_counter(const struct net_device
*dev
,
706 struct can_berr_counter
*bec
)
708 unsigned int reg_err_counter
;
709 struct c_can_priv
*priv
= netdev_priv(dev
);
711 reg_err_counter
= priv
->read_reg(priv
, C_CAN_ERR_CNT_REG
);
712 bec
->rxerr
= (reg_err_counter
& ERR_CNT_REC_MASK
) >>
714 bec
->txerr
= reg_err_counter
& ERR_CNT_TEC_MASK
;
719 static int c_can_get_berr_counter(const struct net_device
*dev
,
720 struct can_berr_counter
*bec
)
722 struct c_can_priv
*priv
= netdev_priv(dev
);
725 c_can_pm_runtime_get_sync(priv
);
726 err
= __c_can_get_berr_counter(dev
, bec
);
727 c_can_pm_runtime_put_sync(priv
);
732 static void c_can_do_tx(struct net_device
*dev
)
734 struct c_can_priv
*priv
= netdev_priv(dev
);
735 struct net_device_stats
*stats
= &dev
->stats
;
736 u32 idx
, obj
, pkts
= 0, bytes
= 0, pend
, clr
;
738 clr
= pend
= priv
->read_reg(priv
, C_CAN_INTPND2_REG
);
740 while ((idx
= ffs(pend
))) {
743 obj
= idx
+ C_CAN_MSG_OBJ_TX_FIRST
;
744 c_can_inval_tx_object(dev
, IF_RX
, obj
);
745 can_get_echo_skb(dev
, idx
);
746 bytes
+= priv
->dlc
[idx
];
750 /* Clear the bits in the tx_active mask */
751 atomic_sub(clr
, &priv
->tx_active
);
753 if (clr
& (1 << (C_CAN_MSG_OBJ_TX_NUM
- 1)))
754 netif_wake_queue(dev
);
757 stats
->tx_bytes
+= bytes
;
758 stats
->tx_packets
+= pkts
;
759 can_led_event(dev
, CAN_LED_EVENT_TX
);
764 * If we have a gap in the pending bits, that means we either
765 * raced with the hardware or failed to readout all upper
766 * objects in the last run due to quota limit.
768 static u32
c_can_adjust_pending(u32 pend
)
772 if (pend
== RECEIVE_OBJECT_BITS
)
776 * If the last set bit is larger than the number of pending
777 * bits we have a gap.
779 weight
= hweight32(pend
);
782 /* If the bits are linear, nothing to do */
787 * Find the first set bit after the gap. We walk backwards
788 * from the last set bit.
790 for (lasts
--; pend
& (1 << (lasts
- 1)); lasts
--);
792 return pend
& ~((1 << lasts
) - 1);
795 static inline void c_can_rx_object_get(struct net_device
*dev
,
796 struct c_can_priv
*priv
, u32 obj
)
798 c_can_object_get(dev
, IF_RX
, obj
, priv
->comm_rcv_high
);
801 static inline void c_can_rx_finalize(struct net_device
*dev
,
802 struct c_can_priv
*priv
, u32 obj
)
804 if (priv
->type
!= BOSCH_D_CAN
)
805 c_can_object_get(dev
, IF_RX
, obj
, IF_COMM_CLR_NEWDAT
);
808 static int c_can_read_objects(struct net_device
*dev
, struct c_can_priv
*priv
,
811 u32 pkts
= 0, ctrl
, obj
;
813 while ((obj
= ffs(pend
)) && quota
> 0) {
814 pend
&= ~BIT(obj
- 1);
816 c_can_rx_object_get(dev
, priv
, obj
);
817 ctrl
= priv
->read_reg(priv
, C_CAN_IFACE(MSGCTRL_REG
, IF_RX
));
819 if (ctrl
& IF_MCONT_MSGLST
) {
820 int n
= c_can_handle_lost_msg_obj(dev
, IF_RX
, obj
, ctrl
);
828 * This really should not happen, but this covers some
829 * odd HW behaviour. Do not remove that unless you
830 * want to brick your machine.
832 if (!(ctrl
& IF_MCONT_NEWDAT
))
835 /* read the data from the message object */
836 c_can_read_msg_object(dev
, IF_RX
, ctrl
);
838 c_can_rx_finalize(dev
, priv
, obj
);
847 static inline u32
c_can_get_pending(struct c_can_priv
*priv
)
849 u32 pend
= priv
->read_reg(priv
, C_CAN_NEWDAT1_REG
);
855 * theory of operation:
857 * c_can core saves a received CAN message into the first free message
858 * object it finds free (starting with the lowest). Bits NEWDAT and
859 * INTPND are set for this message object indicating that a new message
860 * has arrived. To work-around this issue, we keep two groups of message
861 * objects whose partitioning is defined by C_CAN_MSG_OBJ_RX_SPLIT.
863 * We clear the newdat bit right away.
865 * This can result in packet reordering when the readout is slow.
867 static int c_can_do_rx_poll(struct net_device
*dev
, int quota
)
869 struct c_can_priv
*priv
= netdev_priv(dev
);
870 u32 pkts
= 0, pend
= 0, toread
, n
;
873 * It is faster to read only one 16bit register. This is only possible
874 * for a maximum number of 16 objects.
876 BUILD_BUG_ON_MSG(C_CAN_MSG_OBJ_RX_LAST
> 16,
877 "Implementation does not support more message objects than 16");
881 pend
= c_can_get_pending(priv
);
885 * If the pending field has a gap, handle the
886 * bits above the gap first.
888 toread
= c_can_adjust_pending(pend
);
892 /* Remove the bits from pend */
894 /* Read the objects */
895 n
= c_can_read_objects(dev
, priv
, toread
, quota
);
901 can_led_event(dev
, CAN_LED_EVENT_RX
);
906 static int c_can_handle_state_change(struct net_device
*dev
,
907 enum c_can_bus_error_types error_type
)
909 unsigned int reg_err_counter
;
910 unsigned int rx_err_passive
;
911 struct c_can_priv
*priv
= netdev_priv(dev
);
912 struct net_device_stats
*stats
= &dev
->stats
;
913 struct can_frame
*cf
;
915 struct can_berr_counter bec
;
917 switch (error_type
) {
919 priv
->can
.state
= CAN_STATE_ERROR_ACTIVE
;
921 case C_CAN_ERROR_WARNING
:
922 /* error warning state */
923 priv
->can
.can_stats
.error_warning
++;
924 priv
->can
.state
= CAN_STATE_ERROR_WARNING
;
926 case C_CAN_ERROR_PASSIVE
:
927 /* error passive state */
928 priv
->can
.can_stats
.error_passive
++;
929 priv
->can
.state
= CAN_STATE_ERROR_PASSIVE
;
933 priv
->can
.state
= CAN_STATE_BUS_OFF
;
934 priv
->can
.can_stats
.bus_off
++;
940 /* propagate the error condition to the CAN stack */
941 skb
= alloc_can_err_skb(dev
, &cf
);
945 __c_can_get_berr_counter(dev
, &bec
);
946 reg_err_counter
= priv
->read_reg(priv
, C_CAN_ERR_CNT_REG
);
947 rx_err_passive
= (reg_err_counter
& ERR_CNT_RP_MASK
) >>
950 switch (error_type
) {
952 /* error warning state */
953 cf
->can_id
|= CAN_ERR_CRTL
;
954 cf
->data
[1] = CAN_ERR_CRTL_ACTIVE
;
955 cf
->data
[6] = bec
.txerr
;
956 cf
->data
[7] = bec
.rxerr
;
958 case C_CAN_ERROR_WARNING
:
959 /* error warning state */
960 cf
->can_id
|= CAN_ERR_CRTL
;
961 cf
->data
[1] = (bec
.txerr
> bec
.rxerr
) ?
962 CAN_ERR_CRTL_TX_WARNING
:
963 CAN_ERR_CRTL_RX_WARNING
;
964 cf
->data
[6] = bec
.txerr
;
965 cf
->data
[7] = bec
.rxerr
;
968 case C_CAN_ERROR_PASSIVE
:
969 /* error passive state */
970 cf
->can_id
|= CAN_ERR_CRTL
;
972 cf
->data
[1] |= CAN_ERR_CRTL_RX_PASSIVE
;
974 cf
->data
[1] |= CAN_ERR_CRTL_TX_PASSIVE
;
976 cf
->data
[6] = bec
.txerr
;
977 cf
->data
[7] = bec
.rxerr
;
981 cf
->can_id
|= CAN_ERR_BUSOFF
;
989 stats
->rx_bytes
+= cf
->can_dlc
;
990 netif_receive_skb(skb
);
995 static int c_can_handle_bus_err(struct net_device
*dev
,
996 enum c_can_lec_type lec_type
)
998 struct c_can_priv
*priv
= netdev_priv(dev
);
999 struct net_device_stats
*stats
= &dev
->stats
;
1000 struct can_frame
*cf
;
1001 struct sk_buff
*skb
;
1004 * early exit if no lec update or no error.
1005 * no lec update means that no CAN bus event has been detected
1006 * since CPU wrote 0x7 value to status reg.
1008 if (lec_type
== LEC_UNUSED
|| lec_type
== LEC_NO_ERROR
)
1011 if (!(priv
->can
.ctrlmode
& CAN_CTRLMODE_BERR_REPORTING
))
1014 /* common for all type of bus errors */
1015 priv
->can
.can_stats
.bus_error
++;
1018 /* propagate the error condition to the CAN stack */
1019 skb
= alloc_can_err_skb(dev
, &cf
);
1024 * check for 'last error code' which tells us the
1025 * type of the last error to occur on the CAN bus
1027 cf
->can_id
|= CAN_ERR_PROT
| CAN_ERR_BUSERROR
;
1030 case LEC_STUFF_ERROR
:
1031 netdev_dbg(dev
, "stuff error\n");
1032 cf
->data
[2] |= CAN_ERR_PROT_STUFF
;
1034 case LEC_FORM_ERROR
:
1035 netdev_dbg(dev
, "form error\n");
1036 cf
->data
[2] |= CAN_ERR_PROT_FORM
;
1039 netdev_dbg(dev
, "ack error\n");
1040 cf
->data
[3] = CAN_ERR_PROT_LOC_ACK
;
1042 case LEC_BIT1_ERROR
:
1043 netdev_dbg(dev
, "bit1 error\n");
1044 cf
->data
[2] |= CAN_ERR_PROT_BIT1
;
1046 case LEC_BIT0_ERROR
:
1047 netdev_dbg(dev
, "bit0 error\n");
1048 cf
->data
[2] |= CAN_ERR_PROT_BIT0
;
1051 netdev_dbg(dev
, "CRC error\n");
1052 cf
->data
[3] = CAN_ERR_PROT_LOC_CRC_SEQ
;
1058 stats
->rx_packets
++;
1059 stats
->rx_bytes
+= cf
->can_dlc
;
1060 netif_receive_skb(skb
);
1064 static int c_can_poll(struct napi_struct
*napi
, int quota
)
1066 struct net_device
*dev
= napi
->dev
;
1067 struct c_can_priv
*priv
= netdev_priv(dev
);
1068 u16 curr
, last
= priv
->last_status
;
1071 /* Only read the status register if a status interrupt was pending */
1072 if (atomic_xchg(&priv
->sie_pending
, 0)) {
1073 priv
->last_status
= curr
= priv
->read_reg(priv
, C_CAN_STS_REG
);
1074 /* Ack status on C_CAN. D_CAN is self clearing */
1075 if (priv
->type
!= BOSCH_D_CAN
)
1076 priv
->write_reg(priv
, C_CAN_STS_REG
, LEC_UNUSED
);
1078 /* no change detected ... */
1082 /* handle state changes */
1083 if ((curr
& STATUS_EWARN
) && (!(last
& STATUS_EWARN
))) {
1084 netdev_dbg(dev
, "entered error warning state\n");
1085 work_done
+= c_can_handle_state_change(dev
, C_CAN_ERROR_WARNING
);
1088 if ((curr
& STATUS_EPASS
) && (!(last
& STATUS_EPASS
))) {
1089 netdev_dbg(dev
, "entered error passive state\n");
1090 work_done
+= c_can_handle_state_change(dev
, C_CAN_ERROR_PASSIVE
);
1093 if ((curr
& STATUS_BOFF
) && (!(last
& STATUS_BOFF
))) {
1094 netdev_dbg(dev
, "entered bus off state\n");
1095 work_done
+= c_can_handle_state_change(dev
, C_CAN_BUS_OFF
);
1099 /* handle bus recovery events */
1100 if ((!(curr
& STATUS_BOFF
)) && (last
& STATUS_BOFF
)) {
1101 netdev_dbg(dev
, "left bus off state\n");
1102 work_done
+= c_can_handle_state_change(dev
, C_CAN_ERROR_PASSIVE
);
1105 if ((!(curr
& STATUS_EPASS
)) && (last
& STATUS_EPASS
)) {
1106 netdev_dbg(dev
, "left error passive state\n");
1107 work_done
+= c_can_handle_state_change(dev
, C_CAN_ERROR_WARNING
);
1110 if ((!(curr
& STATUS_EWARN
)) && (last
& STATUS_EWARN
)) {
1111 netdev_dbg(dev
, "left error warning state\n");
1112 work_done
+= c_can_handle_state_change(dev
, C_CAN_NO_ERROR
);
1115 /* handle lec errors on the bus */
1116 work_done
+= c_can_handle_bus_err(dev
, curr
& LEC_MASK
);
1118 /* Handle Tx/Rx events. We do this unconditionally */
1119 work_done
+= c_can_do_rx_poll(dev
, (quota
- work_done
));
1123 if (work_done
< quota
) {
1124 napi_complete_done(napi
, work_done
);
1125 /* enable all IRQs if we are not in bus off state */
1126 if (priv
->can
.state
!= CAN_STATE_BUS_OFF
)
1127 c_can_irq_control(priv
, true);
1133 static irqreturn_t
c_can_isr(int irq
, void *dev_id
)
1135 struct net_device
*dev
= (struct net_device
*)dev_id
;
1136 struct c_can_priv
*priv
= netdev_priv(dev
);
1139 reg_int
= priv
->read_reg(priv
, C_CAN_INT_REG
);
1143 /* save for later use */
1144 if (reg_int
& INT_STS_PENDING
)
1145 atomic_set(&priv
->sie_pending
, 1);
1147 /* disable all interrupts and schedule the NAPI */
1148 c_can_irq_control(priv
, false);
1149 napi_schedule(&priv
->napi
);
1154 static int c_can_open(struct net_device
*dev
)
1157 struct c_can_priv
*priv
= netdev_priv(dev
);
1159 c_can_pm_runtime_get_sync(priv
);
1160 c_can_reset_ram(priv
, true);
1162 /* open the can device */
1163 err
= open_candev(dev
);
1165 netdev_err(dev
, "failed to open can device\n");
1166 goto exit_open_fail
;
1169 /* register interrupt handler */
1170 err
= request_irq(dev
->irq
, &c_can_isr
, IRQF_SHARED
, dev
->name
,
1173 netdev_err(dev
, "failed to request interrupt\n");
1177 /* start the c_can controller */
1178 err
= c_can_start(dev
);
1180 goto exit_start_fail
;
1182 can_led_event(dev
, CAN_LED_EVENT_OPEN
);
1184 napi_enable(&priv
->napi
);
1185 /* enable status change, error and module interrupts */
1186 c_can_irq_control(priv
, true);
1187 netif_start_queue(dev
);
1192 free_irq(dev
->irq
, dev
);
1196 c_can_reset_ram(priv
, false);
1197 c_can_pm_runtime_put_sync(priv
);
1201 static int c_can_close(struct net_device
*dev
)
1203 struct c_can_priv
*priv
= netdev_priv(dev
);
1205 netif_stop_queue(dev
);
1206 napi_disable(&priv
->napi
);
1208 free_irq(dev
->irq
, dev
);
1211 c_can_reset_ram(priv
, false);
1212 c_can_pm_runtime_put_sync(priv
);
1214 can_led_event(dev
, CAN_LED_EVENT_STOP
);
1219 struct net_device
*alloc_c_can_dev(void)
1221 struct net_device
*dev
;
1222 struct c_can_priv
*priv
;
1224 dev
= alloc_candev(sizeof(struct c_can_priv
), C_CAN_MSG_OBJ_TX_NUM
);
1228 priv
= netdev_priv(dev
);
1229 netif_napi_add(dev
, &priv
->napi
, c_can_poll
, C_CAN_NAPI_WEIGHT
);
1232 priv
->can
.bittiming_const
= &c_can_bittiming_const
;
1233 priv
->can
.do_set_mode
= c_can_set_mode
;
1234 priv
->can
.do_get_berr_counter
= c_can_get_berr_counter
;
1235 priv
->can
.ctrlmode_supported
= CAN_CTRLMODE_LOOPBACK
|
1236 CAN_CTRLMODE_LISTENONLY
|
1237 CAN_CTRLMODE_BERR_REPORTING
;
1241 EXPORT_SYMBOL_GPL(alloc_c_can_dev
);
1244 int c_can_power_down(struct net_device
*dev
)
1247 unsigned long time_out
;
1248 struct c_can_priv
*priv
= netdev_priv(dev
);
1250 if (!(dev
->flags
& IFF_UP
))
1253 WARN_ON(priv
->type
!= BOSCH_D_CAN
);
1255 /* set PDR value so the device goes to power down mode */
1256 val
= priv
->read_reg(priv
, C_CAN_CTRL_EX_REG
);
1257 val
|= CONTROL_EX_PDR
;
1258 priv
->write_reg(priv
, C_CAN_CTRL_EX_REG
, val
);
1260 /* Wait for the PDA bit to get set */
1261 time_out
= jiffies
+ msecs_to_jiffies(INIT_WAIT_MS
);
1262 while (!(priv
->read_reg(priv
, C_CAN_STS_REG
) & STATUS_PDA
) &&
1263 time_after(time_out
, jiffies
))
1266 if (time_after(jiffies
, time_out
))
1271 c_can_reset_ram(priv
, false);
1272 c_can_pm_runtime_put_sync(priv
);
1276 EXPORT_SYMBOL_GPL(c_can_power_down
);
1278 int c_can_power_up(struct net_device
*dev
)
1281 unsigned long time_out
;
1282 struct c_can_priv
*priv
= netdev_priv(dev
);
1285 if (!(dev
->flags
& IFF_UP
))
1288 WARN_ON(priv
->type
!= BOSCH_D_CAN
);
1290 c_can_pm_runtime_get_sync(priv
);
1291 c_can_reset_ram(priv
, true);
1293 /* Clear PDR and INIT bits */
1294 val
= priv
->read_reg(priv
, C_CAN_CTRL_EX_REG
);
1295 val
&= ~CONTROL_EX_PDR
;
1296 priv
->write_reg(priv
, C_CAN_CTRL_EX_REG
, val
);
1297 val
= priv
->read_reg(priv
, C_CAN_CTRL_REG
);
1298 val
&= ~CONTROL_INIT
;
1299 priv
->write_reg(priv
, C_CAN_CTRL_REG
, val
);
1301 /* Wait for the PDA bit to get clear */
1302 time_out
= jiffies
+ msecs_to_jiffies(INIT_WAIT_MS
);
1303 while ((priv
->read_reg(priv
, C_CAN_STS_REG
) & STATUS_PDA
) &&
1304 time_after(time_out
, jiffies
))
1307 if (time_after(jiffies
, time_out
))
1310 ret
= c_can_start(dev
);
1312 c_can_irq_control(priv
, true);
1316 EXPORT_SYMBOL_GPL(c_can_power_up
);
1319 void free_c_can_dev(struct net_device
*dev
)
1321 struct c_can_priv
*priv
= netdev_priv(dev
);
1323 netif_napi_del(&priv
->napi
);
1326 EXPORT_SYMBOL_GPL(free_c_can_dev
);
1328 static const struct net_device_ops c_can_netdev_ops
= {
1329 .ndo_open
= c_can_open
,
1330 .ndo_stop
= c_can_close
,
1331 .ndo_start_xmit
= c_can_start_xmit
,
1332 .ndo_change_mtu
= can_change_mtu
,
1335 int register_c_can_dev(struct net_device
*dev
)
1337 struct c_can_priv
*priv
= netdev_priv(dev
);
1340 /* Deactivate pins to prevent DRA7 DCAN IP from being
1341 * stuck in transition when module is disabled.
1342 * Pins are activated in c_can_start() and deactivated
1345 pinctrl_pm_select_sleep_state(dev
->dev
.parent
);
1347 c_can_pm_runtime_enable(priv
);
1349 dev
->flags
|= IFF_ECHO
; /* we support local echo */
1350 dev
->netdev_ops
= &c_can_netdev_ops
;
1352 err
= register_candev(dev
);
1354 c_can_pm_runtime_disable(priv
);
1356 devm_can_led_init(dev
);
1360 EXPORT_SYMBOL_GPL(register_c_can_dev
);
1362 void unregister_c_can_dev(struct net_device
*dev
)
1364 struct c_can_priv
*priv
= netdev_priv(dev
);
1366 unregister_candev(dev
);
1368 c_can_pm_runtime_disable(priv
);
1370 EXPORT_SYMBOL_GPL(unregister_c_can_dev
);
1372 MODULE_AUTHOR("Bhupesh Sharma <bhupesh.sharma@st.com>");
1373 MODULE_LICENSE("GPL v2");
1374 MODULE_DESCRIPTION("CAN bus driver for Bosch C_CAN controller");