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>
39 #include <linux/can.h>
40 #include <linux/can/dev.h>
41 #include <linux/can/error.h>
42 #include <linux/can/led.h>
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)
50 /* control extension register D_CAN specific */
51 #define CONTROL_EX_PDR BIT(8)
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)
63 #define CONTROL_IRQMSK (CONTROL_EIE | CONTROL_IE | CONTROL_SIE)
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)
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)
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)
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)
99 /* brp extension register */
100 #define BRP_EXT_BRPE_MASK 0x0f
101 #define BRP_EXT_BRPE_SHIFT 0
103 /* IFx command request */
104 #define IF_COMR_BUSY BIT(15)
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)
117 /* TX buffer setup */
118 #define IF_COMM_TX (IF_COMM_ARB | IF_COMM_CONTROL | \
120 IF_COMM_DATAA | IF_COMM_DATAB)
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)
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)
131 /* Receive setup of message objects */
132 #define IF_COMM_RCV_SETUP (IF_COMM_MASK | IF_COMM_ARB | IF_COMM_CONTROL)
134 /* Invalidation of message objects */
135 #define IF_COMM_INVAL (IF_COMM_ARB | IF_COMM_CONTROL)
137 /* IFx arbitration */
138 #define IF_ARB_MSGVAL BIT(31)
139 #define IF_ARB_MSGXTD BIT(30)
140 #define IF_ARB_TRANSMIT BIT(29)
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
154 #define IF_MCONT_RCV (IF_MCONT_RXIE | IF_MCONT_UMASK)
155 #define IF_MCONT_RCV_EOB (IF_MCONT_RCV | IF_MCONT_EOB)
157 #define IF_MCONT_TX (IF_MCONT_TXIE | IF_MCONT_EOB)
160 * Use IF1 for RX and IF2 for TX
165 /* minimum timeout for checking BUSY status */
166 #define MIN_TIMEOUT_VALUE 6
168 /* Wait for ~1 sec for INIT bit */
169 #define INIT_WAIT_MS 1000
172 #define C_CAN_NAPI_WEIGHT C_CAN_MSG_OBJ_RX_NUM
174 /* c_can lec values */
175 enum c_can_lec_type
{
184 LEC_MASK
= LEC_UNUSED
,
189 * Bus errors (BUS_OFF, ERROR_WARNING, ERROR_PASSIVE) are supported
191 enum c_can_bus_error_types
{
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 */
202 .tseg2_min
= 1, /* Time segment 2 = phase_seg2 */
206 .brp_max
= 1024, /* 6-bit BRP field + 4-bit BRPE field*/
210 static inline void c_can_pm_runtime_enable(const struct c_can_priv
*priv
)
213 pm_runtime_enable(priv
->device
);
216 static inline void c_can_pm_runtime_disable(const struct c_can_priv
*priv
)
219 pm_runtime_disable(priv
->device
);
222 static inline void c_can_pm_runtime_get_sync(const struct c_can_priv
*priv
)
225 pm_runtime_get_sync(priv
->device
);
228 static inline void c_can_pm_runtime_put_sync(const struct c_can_priv
*priv
)
231 pm_runtime_put_sync(priv
->device
);
234 static inline void c_can_reset_ram(const struct c_can_priv
*priv
, bool enable
)
237 priv
->raminit(priv
, enable
);
240 static void c_can_irq_control(struct c_can_priv
*priv
, bool enable
)
242 u32 ctrl
= priv
->read_reg(priv
, C_CAN_CTRL_REG
) & ~CONTROL_IRQMSK
;
245 ctrl
|= CONTROL_IRQMSK
;
247 priv
->write_reg(priv
, C_CAN_CTRL_REG
, ctrl
);
250 static void c_can_obj_update(struct net_device
*dev
, int iface
, u32 cmd
, u32 obj
)
252 struct c_can_priv
*priv
= netdev_priv(dev
);
253 int cnt
, reg
= C_CAN_IFACE(COMREQ_REG
, iface
);
255 priv
->write_reg(priv
, reg
+ 1, cmd
);
256 priv
->write_reg(priv
, reg
, obj
);
258 for (cnt
= MIN_TIMEOUT_VALUE
; cnt
; cnt
--) {
259 if (!(priv
->read_reg(priv
, reg
) & IF_COMR_BUSY
))
263 netdev_err(dev
, "Updating object timed out\n");
267 static inline void c_can_object_get(struct net_device
*dev
, int iface
,
270 c_can_obj_update(dev
, iface
, cmd
, obj
);
273 static inline void c_can_object_put(struct net_device
*dev
, int iface
,
276 c_can_obj_update(dev
, iface
, cmd
| IF_COMM_WR
, obj
);
280 * Note: According to documentation clearing TXIE while MSGVAL is set
281 * is not allowed, but works nicely on C/DCAN. And that lowers the I/O
282 * load significantly.
284 static void c_can_inval_tx_object(struct net_device
*dev
, int iface
, int obj
)
286 struct c_can_priv
*priv
= netdev_priv(dev
);
288 priv
->write_reg(priv
, C_CAN_IFACE(MSGCTRL_REG
, iface
), 0);
289 c_can_object_put(dev
, iface
, obj
, IF_COMM_INVAL
);
292 static void c_can_inval_msg_object(struct net_device
*dev
, int iface
, int obj
)
294 struct c_can_priv
*priv
= netdev_priv(dev
);
296 priv
->write_reg(priv
, C_CAN_IFACE(ARB1_REG
, iface
), 0);
297 priv
->write_reg(priv
, C_CAN_IFACE(ARB2_REG
, iface
), 0);
298 c_can_inval_tx_object(dev
, iface
, obj
);
301 static void c_can_setup_tx_object(struct net_device
*dev
, int iface
,
302 struct can_frame
*frame
, int idx
)
304 struct c_can_priv
*priv
= netdev_priv(dev
);
305 u16 ctrl
= IF_MCONT_TX
| frame
->can_dlc
;
306 bool rtr
= frame
->can_id
& CAN_RTR_FLAG
;
307 u32 arb
= IF_ARB_MSGVAL
;
310 if (frame
->can_id
& CAN_EFF_FLAG
) {
311 arb
|= frame
->can_id
& CAN_EFF_MASK
;
312 arb
|= IF_ARB_MSGXTD
;
314 arb
|= (frame
->can_id
& CAN_SFF_MASK
) << 18;
318 arb
|= IF_ARB_TRANSMIT
;
321 * If we change the DIR bit, we need to invalidate the buffer
322 * first, i.e. clear the MSGVAL flag in the arbiter.
324 if (rtr
!= (bool)test_bit(idx
, &priv
->tx_dir
)) {
325 u32 obj
= idx
+ C_CAN_MSG_OBJ_TX_FIRST
;
327 c_can_inval_msg_object(dev
, iface
, obj
);
328 change_bit(idx
, &priv
->tx_dir
);
331 priv
->write_reg(priv
, C_CAN_IFACE(ARB1_REG
, iface
), arb
);
332 priv
->write_reg(priv
, C_CAN_IFACE(ARB2_REG
, iface
), arb
>> 16);
334 priv
->write_reg(priv
, C_CAN_IFACE(MSGCTRL_REG
, iface
), ctrl
);
336 for (i
= 0; i
< frame
->can_dlc
; i
+= 2) {
337 priv
->write_reg(priv
, C_CAN_IFACE(DATA1_REG
, iface
) + i
/ 2,
338 frame
->data
[i
] | (frame
->data
[i
+ 1] << 8));
342 static inline void c_can_activate_all_lower_rx_msg_obj(struct net_device
*dev
,
347 for (i
= C_CAN_MSG_OBJ_RX_FIRST
; i
<= C_CAN_MSG_RX_LOW_LAST
; i
++)
348 c_can_object_get(dev
, iface
, i
, IF_COMM_CLR_NEWDAT
);
351 static int c_can_handle_lost_msg_obj(struct net_device
*dev
,
352 int iface
, int objno
, u32 ctrl
)
354 struct net_device_stats
*stats
= &dev
->stats
;
355 struct c_can_priv
*priv
= netdev_priv(dev
);
356 struct can_frame
*frame
;
359 ctrl
&= ~(IF_MCONT_MSGLST
| IF_MCONT_INTPND
| IF_MCONT_NEWDAT
);
360 priv
->write_reg(priv
, C_CAN_IFACE(MSGCTRL_REG
, iface
), ctrl
);
361 c_can_object_put(dev
, iface
, objno
, IF_COMM_CONTROL
);
364 stats
->rx_over_errors
++;
366 /* create an error msg */
367 skb
= alloc_can_err_skb(dev
, &frame
);
371 frame
->can_id
|= CAN_ERR_CRTL
;
372 frame
->data
[1] = CAN_ERR_CRTL_RX_OVERFLOW
;
374 netif_receive_skb(skb
);
378 static int c_can_read_msg_object(struct net_device
*dev
, int iface
, u32 ctrl
)
380 struct net_device_stats
*stats
= &dev
->stats
;
381 struct c_can_priv
*priv
= netdev_priv(dev
);
382 struct can_frame
*frame
;
386 skb
= alloc_can_skb(dev
, &frame
);
392 frame
->can_dlc
= get_can_dlc(ctrl
& 0x0F);
394 arb
= priv
->read_reg(priv
, C_CAN_IFACE(ARB1_REG
, iface
));
395 arb
|= priv
->read_reg(priv
, C_CAN_IFACE(ARB2_REG
, iface
)) << 16;
397 if (arb
& IF_ARB_MSGXTD
)
398 frame
->can_id
= (arb
& CAN_EFF_MASK
) | CAN_EFF_FLAG
;
400 frame
->can_id
= (arb
>> 18) & CAN_SFF_MASK
;
402 if (arb
& IF_ARB_TRANSMIT
) {
403 frame
->can_id
|= CAN_RTR_FLAG
;
405 int i
, dreg
= C_CAN_IFACE(DATA1_REG
, iface
);
407 for (i
= 0; i
< frame
->can_dlc
; i
+= 2, dreg
++) {
408 data
= priv
->read_reg(priv
, dreg
);
409 frame
->data
[i
] = data
;
410 frame
->data
[i
+ 1] = data
>> 8;
415 stats
->rx_bytes
+= frame
->can_dlc
;
417 netif_receive_skb(skb
);
421 static void c_can_setup_receive_object(struct net_device
*dev
, int iface
,
422 u32 obj
, u32 mask
, u32 id
, u32 mcont
)
424 struct c_can_priv
*priv
= netdev_priv(dev
);
427 priv
->write_reg(priv
, C_CAN_IFACE(MASK1_REG
, iface
), mask
);
428 priv
->write_reg(priv
, C_CAN_IFACE(MASK2_REG
, iface
), mask
>> 16);
431 priv
->write_reg(priv
, C_CAN_IFACE(ARB1_REG
, iface
), id
);
432 priv
->write_reg(priv
, C_CAN_IFACE(ARB2_REG
, iface
), id
>> 16);
434 priv
->write_reg(priv
, C_CAN_IFACE(MSGCTRL_REG
, iface
), mcont
);
435 c_can_object_put(dev
, iface
, obj
, IF_COMM_RCV_SETUP
);
438 static netdev_tx_t
c_can_start_xmit(struct sk_buff
*skb
,
439 struct net_device
*dev
)
441 struct can_frame
*frame
= (struct can_frame
*)skb
->data
;
442 struct c_can_priv
*priv
= netdev_priv(dev
);
445 if (can_dropped_invalid_skb(dev
, skb
))
448 * This is not a FIFO. C/D_CAN sends out the buffers
449 * prioritized. The lowest buffer number wins.
451 idx
= fls(atomic_read(&priv
->tx_active
));
452 obj
= idx
+ C_CAN_MSG_OBJ_TX_FIRST
;
454 /* If this is the last buffer, stop the xmit queue */
455 if (idx
== C_CAN_MSG_OBJ_TX_NUM
- 1)
456 netif_stop_queue(dev
);
458 * Store the message in the interface so we can call
459 * can_put_echo_skb(). We must do this before we enable
460 * transmit as we might race against do_tx().
462 c_can_setup_tx_object(dev
, IF_TX
, frame
, idx
);
463 priv
->dlc
[idx
] = frame
->can_dlc
;
464 can_put_echo_skb(skb
, dev
, idx
);
466 /* Update the active bits */
467 atomic_add((1 << idx
), &priv
->tx_active
);
468 /* Start transmission */
469 c_can_object_put(dev
, IF_TX
, obj
, IF_COMM_TX
);
474 static int c_can_wait_for_ctrl_init(struct net_device
*dev
,
475 struct c_can_priv
*priv
, u32 init
)
479 while (init
!= (priv
->read_reg(priv
, C_CAN_CTRL_REG
) & CONTROL_INIT
)) {
481 if (retry
++ > 1000) {
482 netdev_err(dev
, "CCTRL: set CONTROL_INIT failed\n");
489 static int c_can_set_bittiming(struct net_device
*dev
)
491 unsigned int reg_btr
, reg_brpe
, ctrl_save
;
492 u8 brp
, brpe
, sjw
, tseg1
, tseg2
;
494 struct c_can_priv
*priv
= netdev_priv(dev
);
495 const struct can_bittiming
*bt
= &priv
->can
.bittiming
;
498 /* c_can provides a 6-bit brp and 4-bit brpe fields */
499 ten_bit_brp
= bt
->brp
- 1;
500 brp
= ten_bit_brp
& BTR_BRP_MASK
;
501 brpe
= ten_bit_brp
>> 6;
504 tseg1
= bt
->prop_seg
+ bt
->phase_seg1
- 1;
505 tseg2
= bt
->phase_seg2
- 1;
506 reg_btr
= brp
| (sjw
<< BTR_SJW_SHIFT
) | (tseg1
<< BTR_TSEG1_SHIFT
) |
507 (tseg2
<< BTR_TSEG2_SHIFT
);
508 reg_brpe
= brpe
& BRP_EXT_BRPE_MASK
;
511 "setting BTR=%04x BRPE=%04x\n", reg_btr
, reg_brpe
);
513 ctrl_save
= priv
->read_reg(priv
, C_CAN_CTRL_REG
);
514 ctrl_save
&= ~CONTROL_INIT
;
515 priv
->write_reg(priv
, C_CAN_CTRL_REG
, CONTROL_CCE
| CONTROL_INIT
);
516 res
= c_can_wait_for_ctrl_init(dev
, priv
, CONTROL_INIT
);
520 priv
->write_reg(priv
, C_CAN_BTR_REG
, reg_btr
);
521 priv
->write_reg(priv
, C_CAN_BRPEXT_REG
, reg_brpe
);
522 priv
->write_reg(priv
, C_CAN_CTRL_REG
, ctrl_save
);
524 return c_can_wait_for_ctrl_init(dev
, priv
, 0);
528 * Configure C_CAN message objects for Tx and Rx purposes:
529 * C_CAN provides a total of 32 message objects that can be configured
530 * either for Tx or Rx purposes. Here the first 16 message objects are used as
531 * a reception FIFO. The end of reception FIFO is signified by the EoB bit
532 * being SET. The remaining 16 message objects are kept aside for Tx purposes.
533 * See user guide document for further details on configuring message
536 static void c_can_configure_msg_objects(struct net_device
*dev
)
540 /* first invalidate all message objects */
541 for (i
= C_CAN_MSG_OBJ_RX_FIRST
; i
<= C_CAN_NO_OF_OBJECTS
; i
++)
542 c_can_inval_msg_object(dev
, IF_RX
, i
);
544 /* setup receive message objects */
545 for (i
= C_CAN_MSG_OBJ_RX_FIRST
; i
< C_CAN_MSG_OBJ_RX_LAST
; i
++)
546 c_can_setup_receive_object(dev
, IF_RX
, i
, 0, 0, IF_MCONT_RCV
);
548 c_can_setup_receive_object(dev
, IF_RX
, C_CAN_MSG_OBJ_RX_LAST
, 0, 0,
553 * Configure C_CAN chip:
554 * - enable/disable auto-retransmission
555 * - set operating mode
556 * - configure message objects
558 static int c_can_chip_config(struct net_device
*dev
)
560 struct c_can_priv
*priv
= netdev_priv(dev
);
562 /* enable automatic retransmission */
563 priv
->write_reg(priv
, C_CAN_CTRL_REG
, CONTROL_ENABLE_AR
);
565 if ((priv
->can
.ctrlmode
& CAN_CTRLMODE_LISTENONLY
) &&
566 (priv
->can
.ctrlmode
& CAN_CTRLMODE_LOOPBACK
)) {
567 /* loopback + silent mode : useful for hot self-test */
568 priv
->write_reg(priv
, C_CAN_CTRL_REG
, CONTROL_TEST
);
569 priv
->write_reg(priv
, C_CAN_TEST_REG
, TEST_LBACK
| TEST_SILENT
);
570 } else if (priv
->can
.ctrlmode
& CAN_CTRLMODE_LOOPBACK
) {
571 /* loopback mode : useful for self-test function */
572 priv
->write_reg(priv
, C_CAN_CTRL_REG
, CONTROL_TEST
);
573 priv
->write_reg(priv
, C_CAN_TEST_REG
, TEST_LBACK
);
574 } else if (priv
->can
.ctrlmode
& CAN_CTRLMODE_LISTENONLY
) {
575 /* silent mode : bus-monitoring mode */
576 priv
->write_reg(priv
, C_CAN_CTRL_REG
, CONTROL_TEST
);
577 priv
->write_reg(priv
, C_CAN_TEST_REG
, TEST_SILENT
);
580 /* configure message objects */
581 c_can_configure_msg_objects(dev
);
583 /* set a `lec` value so that we can check for updates later */
584 priv
->write_reg(priv
, C_CAN_STS_REG
, LEC_UNUSED
);
586 /* Clear all internal status */
587 atomic_set(&priv
->tx_active
, 0);
591 /* set bittiming params */
592 return c_can_set_bittiming(dev
);
595 static int c_can_start(struct net_device
*dev
)
597 struct c_can_priv
*priv
= netdev_priv(dev
);
600 /* basic c_can configuration */
601 err
= c_can_chip_config(dev
);
605 /* Setup the command for new messages */
606 priv
->comm_rcv_high
= priv
->type
!= BOSCH_D_CAN
?
607 IF_COMM_RCV_LOW
: IF_COMM_RCV_HIGH
;
609 priv
->can
.state
= CAN_STATE_ERROR_ACTIVE
;
614 static void c_can_stop(struct net_device
*dev
)
616 struct c_can_priv
*priv
= netdev_priv(dev
);
618 c_can_irq_control(priv
, false);
619 priv
->can
.state
= CAN_STATE_STOPPED
;
622 static int c_can_set_mode(struct net_device
*dev
, enum can_mode mode
)
624 struct c_can_priv
*priv
= netdev_priv(dev
);
629 err
= c_can_start(dev
);
632 netif_wake_queue(dev
);
633 c_can_irq_control(priv
, true);
642 static int __c_can_get_berr_counter(const struct net_device
*dev
,
643 struct can_berr_counter
*bec
)
645 unsigned int reg_err_counter
;
646 struct c_can_priv
*priv
= netdev_priv(dev
);
648 reg_err_counter
= priv
->read_reg(priv
, C_CAN_ERR_CNT_REG
);
649 bec
->rxerr
= (reg_err_counter
& ERR_CNT_REC_MASK
) >>
651 bec
->txerr
= reg_err_counter
& ERR_CNT_TEC_MASK
;
656 static int c_can_get_berr_counter(const struct net_device
*dev
,
657 struct can_berr_counter
*bec
)
659 struct c_can_priv
*priv
= netdev_priv(dev
);
662 c_can_pm_runtime_get_sync(priv
);
663 err
= __c_can_get_berr_counter(dev
, bec
);
664 c_can_pm_runtime_put_sync(priv
);
669 static void c_can_do_tx(struct net_device
*dev
)
671 struct c_can_priv
*priv
= netdev_priv(dev
);
672 struct net_device_stats
*stats
= &dev
->stats
;
673 u32 idx
, obj
, pkts
= 0, bytes
= 0, pend
, clr
;
675 clr
= pend
= priv
->read_reg(priv
, C_CAN_INTPND2_REG
);
677 while ((idx
= ffs(pend
))) {
680 obj
= idx
+ C_CAN_MSG_OBJ_TX_FIRST
;
681 c_can_inval_tx_object(dev
, IF_RX
, obj
);
682 can_get_echo_skb(dev
, idx
);
683 bytes
+= priv
->dlc
[idx
];
687 /* Clear the bits in the tx_active mask */
688 atomic_sub(clr
, &priv
->tx_active
);
690 if (clr
& (1 << (C_CAN_MSG_OBJ_TX_NUM
- 1)))
691 netif_wake_queue(dev
);
694 stats
->tx_bytes
+= bytes
;
695 stats
->tx_packets
+= pkts
;
696 can_led_event(dev
, CAN_LED_EVENT_TX
);
701 * If we have a gap in the pending bits, that means we either
702 * raced with the hardware or failed to readout all upper
703 * objects in the last run due to quota limit.
705 static u32
c_can_adjust_pending(u32 pend
)
709 if (pend
== RECEIVE_OBJECT_BITS
)
713 * If the last set bit is larger than the number of pending
714 * bits we have a gap.
716 weight
= hweight32(pend
);
719 /* If the bits are linear, nothing to do */
724 * Find the first set bit after the gap. We walk backwards
725 * from the last set bit.
727 for (lasts
--; pend
& (1 << (lasts
- 1)); lasts
--);
729 return pend
& ~((1 << lasts
) - 1);
732 static inline void c_can_rx_object_get(struct net_device
*dev
,
733 struct c_can_priv
*priv
, u32 obj
)
735 #ifdef CONFIG_CAN_C_CAN_STRICT_FRAME_ORDERING
736 if (obj
< C_CAN_MSG_RX_LOW_LAST
)
737 c_can_object_get(dev
, IF_RX
, obj
, IF_COMM_RCV_LOW
);
740 c_can_object_get(dev
, IF_RX
, obj
, priv
->comm_rcv_high
);
743 static inline void c_can_rx_finalize(struct net_device
*dev
,
744 struct c_can_priv
*priv
, u32 obj
)
746 #ifdef CONFIG_CAN_C_CAN_STRICT_FRAME_ORDERING
747 if (obj
< C_CAN_MSG_RX_LOW_LAST
)
748 priv
->rxmasked
|= BIT(obj
- 1);
749 else if (obj
== C_CAN_MSG_RX_LOW_LAST
) {
751 /* activate all lower message objects */
752 c_can_activate_all_lower_rx_msg_obj(dev
, IF_RX
);
755 if (priv
->type
!= BOSCH_D_CAN
)
756 c_can_object_get(dev
, IF_RX
, obj
, IF_COMM_CLR_NEWDAT
);
759 static int c_can_read_objects(struct net_device
*dev
, struct c_can_priv
*priv
,
762 u32 pkts
= 0, ctrl
, obj
;
764 while ((obj
= ffs(pend
)) && quota
> 0) {
765 pend
&= ~BIT(obj
- 1);
767 c_can_rx_object_get(dev
, priv
, obj
);
768 ctrl
= priv
->read_reg(priv
, C_CAN_IFACE(MSGCTRL_REG
, IF_RX
));
770 if (ctrl
& IF_MCONT_MSGLST
) {
771 int n
= c_can_handle_lost_msg_obj(dev
, IF_RX
, obj
, ctrl
);
779 * This really should not happen, but this covers some
780 * odd HW behaviour. Do not remove that unless you
781 * want to brick your machine.
783 if (!(ctrl
& IF_MCONT_NEWDAT
))
786 /* read the data from the message object */
787 c_can_read_msg_object(dev
, IF_RX
, ctrl
);
789 c_can_rx_finalize(dev
, priv
, obj
);
798 static inline u32
c_can_get_pending(struct c_can_priv
*priv
)
800 u32 pend
= priv
->read_reg(priv
, C_CAN_NEWDAT1_REG
);
802 #ifdef CONFIG_CAN_C_CAN_STRICT_FRAME_ORDERING
803 pend
&= ~priv
->rxmasked
;
809 * theory of operation:
811 * c_can core saves a received CAN message into the first free message
812 * object it finds free (starting with the lowest). Bits NEWDAT and
813 * INTPND are set for this message object indicating that a new message
814 * has arrived. To work-around this issue, we keep two groups of message
815 * objects whose partitioning is defined by C_CAN_MSG_OBJ_RX_SPLIT.
817 * If CONFIG_CAN_C_CAN_STRICT_FRAME_ORDERING = y
819 * To ensure in-order frame reception we use the following
820 * approach while re-activating a message object to receive further
822 * - if the current message object number is lower than
823 * C_CAN_MSG_RX_LOW_LAST, do not clear the NEWDAT bit while clearing
825 * - if the current message object number is equal to
826 * C_CAN_MSG_RX_LOW_LAST then clear the NEWDAT bit of all lower
827 * receive message objects.
828 * - if the current message object number is greater than
829 * C_CAN_MSG_RX_LOW_LAST then clear the NEWDAT bit of
830 * only this message object.
832 * This can cause packet loss!
834 * If CONFIG_CAN_C_CAN_STRICT_FRAME_ORDERING = n
836 * We clear the newdat bit right away.
838 * This can result in packet reordering when the readout is slow.
840 static int c_can_do_rx_poll(struct net_device
*dev
, int quota
)
842 struct c_can_priv
*priv
= netdev_priv(dev
);
843 u32 pkts
= 0, pend
= 0, toread
, n
;
846 * It is faster to read only one 16bit register. This is only possible
847 * for a maximum number of 16 objects.
849 BUILD_BUG_ON_MSG(C_CAN_MSG_OBJ_RX_LAST
> 16,
850 "Implementation does not support more message objects than 16");
854 pend
= c_can_get_pending(priv
);
858 * If the pending field has a gap, handle the
859 * bits above the gap first.
861 toread
= c_can_adjust_pending(pend
);
865 /* Remove the bits from pend */
867 /* Read the objects */
868 n
= c_can_read_objects(dev
, priv
, toread
, quota
);
874 can_led_event(dev
, CAN_LED_EVENT_RX
);
879 static int c_can_handle_state_change(struct net_device
*dev
,
880 enum c_can_bus_error_types error_type
)
882 unsigned int reg_err_counter
;
883 unsigned int rx_err_passive
;
884 struct c_can_priv
*priv
= netdev_priv(dev
);
885 struct net_device_stats
*stats
= &dev
->stats
;
886 struct can_frame
*cf
;
888 struct can_berr_counter bec
;
890 switch (error_type
) {
891 case C_CAN_ERROR_WARNING
:
892 /* error warning state */
893 priv
->can
.can_stats
.error_warning
++;
894 priv
->can
.state
= CAN_STATE_ERROR_WARNING
;
896 case C_CAN_ERROR_PASSIVE
:
897 /* error passive state */
898 priv
->can
.can_stats
.error_passive
++;
899 priv
->can
.state
= CAN_STATE_ERROR_PASSIVE
;
903 priv
->can
.state
= CAN_STATE_BUS_OFF
;
910 /* propagate the error condition to the CAN stack */
911 skb
= alloc_can_err_skb(dev
, &cf
);
915 __c_can_get_berr_counter(dev
, &bec
);
916 reg_err_counter
= priv
->read_reg(priv
, C_CAN_ERR_CNT_REG
);
917 rx_err_passive
= (reg_err_counter
& ERR_CNT_RP_MASK
) >>
920 switch (error_type
) {
921 case C_CAN_ERROR_WARNING
:
922 /* error warning state */
923 cf
->can_id
|= CAN_ERR_CRTL
;
924 cf
->data
[1] = (bec
.txerr
> bec
.rxerr
) ?
925 CAN_ERR_CRTL_TX_WARNING
:
926 CAN_ERR_CRTL_RX_WARNING
;
927 cf
->data
[6] = bec
.txerr
;
928 cf
->data
[7] = bec
.rxerr
;
931 case C_CAN_ERROR_PASSIVE
:
932 /* error passive state */
933 cf
->can_id
|= CAN_ERR_CRTL
;
935 cf
->data
[1] |= CAN_ERR_CRTL_RX_PASSIVE
;
937 cf
->data
[1] |= CAN_ERR_CRTL_TX_PASSIVE
;
939 cf
->data
[6] = bec
.txerr
;
940 cf
->data
[7] = bec
.rxerr
;
944 cf
->can_id
|= CAN_ERR_BUSOFF
;
952 stats
->rx_bytes
+= cf
->can_dlc
;
953 netif_receive_skb(skb
);
958 static int c_can_handle_bus_err(struct net_device
*dev
,
959 enum c_can_lec_type lec_type
)
961 struct c_can_priv
*priv
= netdev_priv(dev
);
962 struct net_device_stats
*stats
= &dev
->stats
;
963 struct can_frame
*cf
;
967 * early exit if no lec update or no error.
968 * no lec update means that no CAN bus event has been detected
969 * since CPU wrote 0x7 value to status reg.
971 if (lec_type
== LEC_UNUSED
|| lec_type
== LEC_NO_ERROR
)
974 if (!(priv
->can
.ctrlmode
& CAN_CTRLMODE_BERR_REPORTING
))
977 /* common for all type of bus errors */
978 priv
->can
.can_stats
.bus_error
++;
981 /* propagate the error condition to the CAN stack */
982 skb
= alloc_can_err_skb(dev
, &cf
);
987 * check for 'last error code' which tells us the
988 * type of the last error to occur on the CAN bus
990 cf
->can_id
|= CAN_ERR_PROT
| CAN_ERR_BUSERROR
;
991 cf
->data
[2] |= CAN_ERR_PROT_UNSPEC
;
994 case LEC_STUFF_ERROR
:
995 netdev_dbg(dev
, "stuff error\n");
996 cf
->data
[2] |= CAN_ERR_PROT_STUFF
;
999 netdev_dbg(dev
, "form error\n");
1000 cf
->data
[2] |= CAN_ERR_PROT_FORM
;
1003 netdev_dbg(dev
, "ack error\n");
1004 cf
->data
[3] |= (CAN_ERR_PROT_LOC_ACK
|
1005 CAN_ERR_PROT_LOC_ACK_DEL
);
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
|
1018 CAN_ERR_PROT_LOC_CRC_DEL
);
1024 stats
->rx_packets
++;
1025 stats
->rx_bytes
+= cf
->can_dlc
;
1026 netif_receive_skb(skb
);
1030 static int c_can_poll(struct napi_struct
*napi
, int quota
)
1032 struct net_device
*dev
= napi
->dev
;
1033 struct c_can_priv
*priv
= netdev_priv(dev
);
1034 u16 curr
, last
= priv
->last_status
;
1037 priv
->last_status
= curr
= priv
->read_reg(priv
, C_CAN_STS_REG
);
1038 /* Ack status on C_CAN. D_CAN is self clearing */
1039 if (priv
->type
!= BOSCH_D_CAN
)
1040 priv
->write_reg(priv
, C_CAN_STS_REG
, LEC_UNUSED
);
1042 /* handle state changes */
1043 if ((curr
& STATUS_EWARN
) && (!(last
& STATUS_EWARN
))) {
1044 netdev_dbg(dev
, "entered error warning state\n");
1045 work_done
+= c_can_handle_state_change(dev
, C_CAN_ERROR_WARNING
);
1048 if ((curr
& STATUS_EPASS
) && (!(last
& STATUS_EPASS
))) {
1049 netdev_dbg(dev
, "entered error passive state\n");
1050 work_done
+= c_can_handle_state_change(dev
, C_CAN_ERROR_PASSIVE
);
1053 if ((curr
& STATUS_BOFF
) && (!(last
& STATUS_BOFF
))) {
1054 netdev_dbg(dev
, "entered bus off state\n");
1055 work_done
+= c_can_handle_state_change(dev
, C_CAN_BUS_OFF
);
1059 /* handle bus recovery events */
1060 if ((!(curr
& STATUS_BOFF
)) && (last
& STATUS_BOFF
)) {
1061 netdev_dbg(dev
, "left bus off state\n");
1062 priv
->can
.state
= CAN_STATE_ERROR_ACTIVE
;
1064 if ((!(curr
& STATUS_EPASS
)) && (last
& STATUS_EPASS
)) {
1065 netdev_dbg(dev
, "left error passive state\n");
1066 priv
->can
.state
= CAN_STATE_ERROR_ACTIVE
;
1069 /* handle lec errors on the bus */
1070 work_done
+= c_can_handle_bus_err(dev
, curr
& LEC_MASK
);
1072 /* Handle Tx/Rx events. We do this unconditionally */
1073 work_done
+= c_can_do_rx_poll(dev
, (quota
- work_done
));
1077 if (work_done
< quota
) {
1078 napi_complete(napi
);
1079 /* enable all IRQs if we are not in bus off state */
1080 if (priv
->can
.state
!= CAN_STATE_BUS_OFF
)
1081 c_can_irq_control(priv
, true);
1087 static irqreturn_t
c_can_isr(int irq
, void *dev_id
)
1089 struct net_device
*dev
= (struct net_device
*)dev_id
;
1090 struct c_can_priv
*priv
= netdev_priv(dev
);
1092 if (!priv
->read_reg(priv
, C_CAN_INT_REG
))
1095 /* disable all interrupts and schedule the NAPI */
1096 c_can_irq_control(priv
, false);
1097 napi_schedule(&priv
->napi
);
1102 static int c_can_open(struct net_device
*dev
)
1105 struct c_can_priv
*priv
= netdev_priv(dev
);
1107 c_can_pm_runtime_get_sync(priv
);
1108 c_can_reset_ram(priv
, true);
1110 /* open the can device */
1111 err
= open_candev(dev
);
1113 netdev_err(dev
, "failed to open can device\n");
1114 goto exit_open_fail
;
1117 /* register interrupt handler */
1118 err
= request_irq(dev
->irq
, &c_can_isr
, IRQF_SHARED
, dev
->name
,
1121 netdev_err(dev
, "failed to request interrupt\n");
1125 /* start the c_can controller */
1126 err
= c_can_start(dev
);
1128 goto exit_start_fail
;
1130 can_led_event(dev
, CAN_LED_EVENT_OPEN
);
1132 napi_enable(&priv
->napi
);
1133 /* enable status change, error and module interrupts */
1134 c_can_irq_control(priv
, true);
1135 netif_start_queue(dev
);
1140 free_irq(dev
->irq
, dev
);
1144 c_can_reset_ram(priv
, false);
1145 c_can_pm_runtime_put_sync(priv
);
1149 static int c_can_close(struct net_device
*dev
)
1151 struct c_can_priv
*priv
= netdev_priv(dev
);
1153 netif_stop_queue(dev
);
1154 napi_disable(&priv
->napi
);
1156 free_irq(dev
->irq
, dev
);
1159 c_can_reset_ram(priv
, false);
1160 c_can_pm_runtime_put_sync(priv
);
1162 can_led_event(dev
, CAN_LED_EVENT_STOP
);
1167 struct net_device
*alloc_c_can_dev(void)
1169 struct net_device
*dev
;
1170 struct c_can_priv
*priv
;
1172 dev
= alloc_candev(sizeof(struct c_can_priv
), C_CAN_MSG_OBJ_TX_NUM
);
1176 priv
= netdev_priv(dev
);
1177 netif_napi_add(dev
, &priv
->napi
, c_can_poll
, C_CAN_NAPI_WEIGHT
);
1180 priv
->can
.bittiming_const
= &c_can_bittiming_const
;
1181 priv
->can
.do_set_mode
= c_can_set_mode
;
1182 priv
->can
.do_get_berr_counter
= c_can_get_berr_counter
;
1183 priv
->can
.ctrlmode_supported
= CAN_CTRLMODE_LOOPBACK
|
1184 CAN_CTRLMODE_LISTENONLY
|
1185 CAN_CTRLMODE_BERR_REPORTING
;
1189 EXPORT_SYMBOL_GPL(alloc_c_can_dev
);
1192 int c_can_power_down(struct net_device
*dev
)
1195 unsigned long time_out
;
1196 struct c_can_priv
*priv
= netdev_priv(dev
);
1198 if (!(dev
->flags
& IFF_UP
))
1201 WARN_ON(priv
->type
!= BOSCH_D_CAN
);
1203 /* set PDR value so the device goes to power down mode */
1204 val
= priv
->read_reg(priv
, C_CAN_CTRL_EX_REG
);
1205 val
|= CONTROL_EX_PDR
;
1206 priv
->write_reg(priv
, C_CAN_CTRL_EX_REG
, val
);
1208 /* Wait for the PDA bit to get set */
1209 time_out
= jiffies
+ msecs_to_jiffies(INIT_WAIT_MS
);
1210 while (!(priv
->read_reg(priv
, C_CAN_STS_REG
) & STATUS_PDA
) &&
1211 time_after(time_out
, jiffies
))
1214 if (time_after(jiffies
, time_out
))
1219 c_can_reset_ram(priv
, false);
1220 c_can_pm_runtime_put_sync(priv
);
1224 EXPORT_SYMBOL_GPL(c_can_power_down
);
1226 int c_can_power_up(struct net_device
*dev
)
1229 unsigned long time_out
;
1230 struct c_can_priv
*priv
= netdev_priv(dev
);
1233 if (!(dev
->flags
& IFF_UP
))
1236 WARN_ON(priv
->type
!= BOSCH_D_CAN
);
1238 c_can_pm_runtime_get_sync(priv
);
1239 c_can_reset_ram(priv
, true);
1241 /* Clear PDR and INIT bits */
1242 val
= priv
->read_reg(priv
, C_CAN_CTRL_EX_REG
);
1243 val
&= ~CONTROL_EX_PDR
;
1244 priv
->write_reg(priv
, C_CAN_CTRL_EX_REG
, val
);
1245 val
= priv
->read_reg(priv
, C_CAN_CTRL_REG
);
1246 val
&= ~CONTROL_INIT
;
1247 priv
->write_reg(priv
, C_CAN_CTRL_REG
, val
);
1249 /* Wait for the PDA bit to get clear */
1250 time_out
= jiffies
+ msecs_to_jiffies(INIT_WAIT_MS
);
1251 while ((priv
->read_reg(priv
, C_CAN_STS_REG
) & STATUS_PDA
) &&
1252 time_after(time_out
, jiffies
))
1255 if (time_after(jiffies
, time_out
))
1258 ret
= c_can_start(dev
);
1260 c_can_irq_control(priv
, true);
1264 EXPORT_SYMBOL_GPL(c_can_power_up
);
1267 void free_c_can_dev(struct net_device
*dev
)
1269 struct c_can_priv
*priv
= netdev_priv(dev
);
1271 netif_napi_del(&priv
->napi
);
1274 EXPORT_SYMBOL_GPL(free_c_can_dev
);
1276 static const struct net_device_ops c_can_netdev_ops
= {
1277 .ndo_open
= c_can_open
,
1278 .ndo_stop
= c_can_close
,
1279 .ndo_start_xmit
= c_can_start_xmit
,
1280 .ndo_change_mtu
= can_change_mtu
,
1283 int register_c_can_dev(struct net_device
*dev
)
1285 struct c_can_priv
*priv
= netdev_priv(dev
);
1288 c_can_pm_runtime_enable(priv
);
1290 dev
->flags
|= IFF_ECHO
; /* we support local echo */
1291 dev
->netdev_ops
= &c_can_netdev_ops
;
1293 err
= register_candev(dev
);
1295 c_can_pm_runtime_disable(priv
);
1297 devm_can_led_init(dev
);
1301 EXPORT_SYMBOL_GPL(register_c_can_dev
);
1303 void unregister_c_can_dev(struct net_device
*dev
)
1305 struct c_can_priv
*priv
= netdev_priv(dev
);
1307 unregister_candev(dev
);
1309 c_can_pm_runtime_disable(priv
);
1311 EXPORT_SYMBOL_GPL(unregister_c_can_dev
);
1313 MODULE_AUTHOR("Bhupesh Sharma <bhupesh.sharma@st.com>");
1314 MODULE_LICENSE("GPL v2");
1315 MODULE_DESCRIPTION("CAN bus driver for Bosch C_CAN controller");