2 * CAN bus driver for IFI CANFD controller
4 * Copyright (C) 2016 Marek Vasut <marex@denx.de>
6 * Details about this controller can be found at
7 * http://www.ifi-pld.de/IP/CANFD/canfd.html
9 * This file is licensed under the terms of the GNU General Public
10 * License version 2. This program is licensed "as is" without any
11 * warranty of any kind, whether express or implied.
14 #include <linux/clk.h>
15 #include <linux/delay.h>
16 #include <linux/interrupt.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/netdevice.h>
22 #include <linux/of_device.h>
23 #include <linux/platform_device.h>
25 #include <linux/can/dev.h>
27 #define IFI_CANFD_STCMD 0x0
28 #define IFI_CANFD_STCMD_HARDRESET 0xDEADCAFD
29 #define IFI_CANFD_STCMD_ENABLE BIT(0)
30 #define IFI_CANFD_STCMD_ERROR_ACTIVE BIT(2)
31 #define IFI_CANFD_STCMD_ERROR_PASSIVE BIT(3)
32 #define IFI_CANFD_STCMD_BUSOFF BIT(4)
33 #define IFI_CANFD_STCMD_ERROR_WARNING BIT(5)
34 #define IFI_CANFD_STCMD_BUSMONITOR BIT(16)
35 #define IFI_CANFD_STCMD_LOOPBACK BIT(18)
36 #define IFI_CANFD_STCMD_DISABLE_CANFD BIT(24)
37 #define IFI_CANFD_STCMD_ENABLE_ISO BIT(25)
38 #define IFI_CANFD_STCMD_ENABLE_7_9_8_8_TIMING BIT(26)
39 #define IFI_CANFD_STCMD_NORMAL_MODE ((u32)BIT(31))
41 #define IFI_CANFD_RXSTCMD 0x4
42 #define IFI_CANFD_RXSTCMD_REMOVE_MSG BIT(0)
43 #define IFI_CANFD_RXSTCMD_RESET BIT(7)
44 #define IFI_CANFD_RXSTCMD_EMPTY BIT(8)
45 #define IFI_CANFD_RXSTCMD_OVERFLOW BIT(13)
47 #define IFI_CANFD_TXSTCMD 0x8
48 #define IFI_CANFD_TXSTCMD_ADD_MSG BIT(0)
49 #define IFI_CANFD_TXSTCMD_HIGH_PRIO BIT(1)
50 #define IFI_CANFD_TXSTCMD_RESET BIT(7)
51 #define IFI_CANFD_TXSTCMD_EMPTY BIT(8)
52 #define IFI_CANFD_TXSTCMD_FULL BIT(12)
53 #define IFI_CANFD_TXSTCMD_OVERFLOW BIT(13)
55 #define IFI_CANFD_INTERRUPT 0xc
56 #define IFI_CANFD_INTERRUPT_ERROR_BUSOFF BIT(0)
57 #define IFI_CANFD_INTERRUPT_ERROR_WARNING BIT(1)
58 #define IFI_CANFD_INTERRUPT_ERROR_STATE_CHG BIT(2)
59 #define IFI_CANFD_INTERRUPT_ERROR_REC_TEC_INC BIT(3)
60 #define IFI_CANFD_INTERRUPT_ERROR_COUNTER BIT(10)
61 #define IFI_CANFD_INTERRUPT_TXFIFO_EMPTY BIT(16)
62 #define IFI_CANFD_INTERRUPT_TXFIFO_REMOVE BIT(22)
63 #define IFI_CANFD_INTERRUPT_RXFIFO_NEMPTY BIT(24)
64 #define IFI_CANFD_INTERRUPT_RXFIFO_NEMPTY_PER BIT(25)
65 #define IFI_CANFD_INTERRUPT_SET_IRQ ((u32)BIT(31))
67 #define IFI_CANFD_IRQMASK 0x10
68 #define IFI_CANFD_IRQMASK_ERROR_BUSOFF BIT(0)
69 #define IFI_CANFD_IRQMASK_ERROR_WARNING BIT(1)
70 #define IFI_CANFD_IRQMASK_ERROR_STATE_CHG BIT(2)
71 #define IFI_CANFD_IRQMASK_ERROR_REC_TEC_INC BIT(3)
72 #define IFI_CANFD_IRQMASK_SET_ERR BIT(7)
73 #define IFI_CANFD_IRQMASK_SET_TS BIT(15)
74 #define IFI_CANFD_IRQMASK_TXFIFO_EMPTY BIT(16)
75 #define IFI_CANFD_IRQMASK_SET_TX BIT(23)
76 #define IFI_CANFD_IRQMASK_RXFIFO_NEMPTY BIT(24)
77 #define IFI_CANFD_IRQMASK_SET_RX ((u32)BIT(31))
79 #define IFI_CANFD_TIME 0x14
80 #define IFI_CANFD_FTIME 0x18
81 #define IFI_CANFD_TIME_TIMEB_OFF 0
82 #define IFI_CANFD_TIME_TIMEA_OFF 8
83 #define IFI_CANFD_TIME_PRESCALE_OFF 16
84 #define IFI_CANFD_TIME_SJW_OFF_7_9_8_8 25
85 #define IFI_CANFD_TIME_SJW_OFF_4_12_6_6 28
86 #define IFI_CANFD_TIME_SET_SJW_4_12_6_6 BIT(6)
87 #define IFI_CANFD_TIME_SET_TIMEB_4_12_6_6 BIT(7)
88 #define IFI_CANFD_TIME_SET_PRESC_4_12_6_6 BIT(14)
89 #define IFI_CANFD_TIME_SET_TIMEA_4_12_6_6 BIT(15)
91 #define IFI_CANFD_TDELAY 0x1c
92 #define IFI_CANFD_TDELAY_DEFAULT 0xb
93 #define IFI_CANFD_TDELAY_MASK 0x3fff
94 #define IFI_CANFD_TDELAY_ABS BIT(14)
95 #define IFI_CANFD_TDELAY_EN BIT(15)
97 #define IFI_CANFD_ERROR 0x20
98 #define IFI_CANFD_ERROR_TX_OFFSET 0
99 #define IFI_CANFD_ERROR_TX_MASK 0xff
100 #define IFI_CANFD_ERROR_RX_OFFSET 16
101 #define IFI_CANFD_ERROR_RX_MASK 0xff
103 #define IFI_CANFD_ERRCNT 0x24
105 #define IFI_CANFD_SUSPEND 0x28
107 #define IFI_CANFD_REPEAT 0x2c
109 #define IFI_CANFD_TRAFFIC 0x30
111 #define IFI_CANFD_TSCONTROL 0x34
113 #define IFI_CANFD_TSC 0x38
115 #define IFI_CANFD_TST 0x3c
117 #define IFI_CANFD_RES1 0x40
119 #define IFI_CANFD_ERROR_CTR 0x44
120 #define IFI_CANFD_ERROR_CTR_UNLOCK_MAGIC 0x21302899
121 #define IFI_CANFD_ERROR_CTR_OVERLOAD_FIRST BIT(0)
122 #define IFI_CANFD_ERROR_CTR_ACK_ERROR_FIRST BIT(1)
123 #define IFI_CANFD_ERROR_CTR_BIT0_ERROR_FIRST BIT(2)
124 #define IFI_CANFD_ERROR_CTR_BIT1_ERROR_FIRST BIT(3)
125 #define IFI_CANFD_ERROR_CTR_STUFF_ERROR_FIRST BIT(4)
126 #define IFI_CANFD_ERROR_CTR_CRC_ERROR_FIRST BIT(5)
127 #define IFI_CANFD_ERROR_CTR_FORM_ERROR_FIRST BIT(6)
128 #define IFI_CANFD_ERROR_CTR_OVERLOAD_ALL BIT(8)
129 #define IFI_CANFD_ERROR_CTR_ACK_ERROR_ALL BIT(9)
130 #define IFI_CANFD_ERROR_CTR_BIT0_ERROR_ALL BIT(10)
131 #define IFI_CANFD_ERROR_CTR_BIT1_ERROR_ALL BIT(11)
132 #define IFI_CANFD_ERROR_CTR_STUFF_ERROR_ALL BIT(12)
133 #define IFI_CANFD_ERROR_CTR_CRC_ERROR_ALL BIT(13)
134 #define IFI_CANFD_ERROR_CTR_FORM_ERROR_ALL BIT(14)
135 #define IFI_CANFD_ERROR_CTR_BITPOSITION_OFFSET 16
136 #define IFI_CANFD_ERROR_CTR_BITPOSITION_MASK 0xff
137 #define IFI_CANFD_ERROR_CTR_ER_RESET BIT(30)
138 #define IFI_CANFD_ERROR_CTR_ER_ENABLE ((u32)BIT(31))
140 #define IFI_CANFD_PAR 0x48
142 #define IFI_CANFD_CANCLOCK 0x4c
144 #define IFI_CANFD_SYSCLOCK 0x50
146 #define IFI_CANFD_VER 0x54
147 #define IFI_CANFD_VER_REV_MASK 0xff
148 #define IFI_CANFD_VER_REV_MIN_SUPPORTED 0x15
150 #define IFI_CANFD_IP_ID 0x58
151 #define IFI_CANFD_IP_ID_VALUE 0xD073CAFD
153 #define IFI_CANFD_TEST 0x5c
155 #define IFI_CANFD_RXFIFO_TS_63_32 0x60
157 #define IFI_CANFD_RXFIFO_TS_31_0 0x64
159 #define IFI_CANFD_RXFIFO_DLC 0x68
160 #define IFI_CANFD_RXFIFO_DLC_DLC_OFFSET 0
161 #define IFI_CANFD_RXFIFO_DLC_DLC_MASK 0xf
162 #define IFI_CANFD_RXFIFO_DLC_RTR BIT(4)
163 #define IFI_CANFD_RXFIFO_DLC_EDL BIT(5)
164 #define IFI_CANFD_RXFIFO_DLC_BRS BIT(6)
165 #define IFI_CANFD_RXFIFO_DLC_ESI BIT(7)
166 #define IFI_CANFD_RXFIFO_DLC_OBJ_OFFSET 8
167 #define IFI_CANFD_RXFIFO_DLC_OBJ_MASK 0x1ff
168 #define IFI_CANFD_RXFIFO_DLC_FNR_OFFSET 24
169 #define IFI_CANFD_RXFIFO_DLC_FNR_MASK 0xff
171 #define IFI_CANFD_RXFIFO_ID 0x6c
172 #define IFI_CANFD_RXFIFO_ID_ID_OFFSET 0
173 #define IFI_CANFD_RXFIFO_ID_ID_STD_MASK CAN_SFF_MASK
174 #define IFI_CANFD_RXFIFO_ID_ID_STD_OFFSET 0
175 #define IFI_CANFD_RXFIFO_ID_ID_STD_WIDTH 10
176 #define IFI_CANFD_RXFIFO_ID_ID_XTD_MASK CAN_EFF_MASK
177 #define IFI_CANFD_RXFIFO_ID_ID_XTD_OFFSET 11
178 #define IFI_CANFD_RXFIFO_ID_ID_XTD_WIDTH 18
179 #define IFI_CANFD_RXFIFO_ID_IDE BIT(29)
181 #define IFI_CANFD_RXFIFO_DATA 0x70 /* 0x70..0xac */
183 #define IFI_CANFD_TXFIFO_SUSPEND_US 0xb0
185 #define IFI_CANFD_TXFIFO_REPEATCOUNT 0xb4
187 #define IFI_CANFD_TXFIFO_DLC 0xb8
188 #define IFI_CANFD_TXFIFO_DLC_DLC_OFFSET 0
189 #define IFI_CANFD_TXFIFO_DLC_DLC_MASK 0xf
190 #define IFI_CANFD_TXFIFO_DLC_RTR BIT(4)
191 #define IFI_CANFD_TXFIFO_DLC_EDL BIT(5)
192 #define IFI_CANFD_TXFIFO_DLC_BRS BIT(6)
193 #define IFI_CANFD_TXFIFO_DLC_FNR_OFFSET 24
194 #define IFI_CANFD_TXFIFO_DLC_FNR_MASK 0xff
196 #define IFI_CANFD_TXFIFO_ID 0xbc
197 #define IFI_CANFD_TXFIFO_ID_ID_OFFSET 0
198 #define IFI_CANFD_TXFIFO_ID_ID_STD_MASK CAN_SFF_MASK
199 #define IFI_CANFD_TXFIFO_ID_ID_STD_OFFSET 0
200 #define IFI_CANFD_TXFIFO_ID_ID_STD_WIDTH 10
201 #define IFI_CANFD_TXFIFO_ID_ID_XTD_MASK CAN_EFF_MASK
202 #define IFI_CANFD_TXFIFO_ID_ID_XTD_OFFSET 11
203 #define IFI_CANFD_TXFIFO_ID_ID_XTD_WIDTH 18
204 #define IFI_CANFD_TXFIFO_ID_IDE BIT(29)
206 #define IFI_CANFD_TXFIFO_DATA 0xc0 /* 0xb0..0xfc */
208 #define IFI_CANFD_FILTER_MASK(n) (0x800 + ((n) * 8) + 0)
209 #define IFI_CANFD_FILTER_MASK_EXT BIT(29)
210 #define IFI_CANFD_FILTER_MASK_EDL BIT(30)
211 #define IFI_CANFD_FILTER_MASK_VALID ((u32)BIT(31))
213 #define IFI_CANFD_FILTER_IDENT(n) (0x800 + ((n) * 8) + 4)
214 #define IFI_CANFD_FILTER_IDENT_IDE BIT(29)
215 #define IFI_CANFD_FILTER_IDENT_CANFD BIT(30)
216 #define IFI_CANFD_FILTER_IDENT_VALID ((u32)BIT(31))
218 /* IFI CANFD private data structure */
219 struct ifi_canfd_priv
{
220 struct can_priv can
; /* must be the first member */
221 struct napi_struct napi
;
222 struct net_device
*ndev
;
226 static void ifi_canfd_irq_enable(struct net_device
*ndev
, bool enable
)
228 struct ifi_canfd_priv
*priv
= netdev_priv(ndev
);
232 enirq
= IFI_CANFD_IRQMASK_TXFIFO_EMPTY
|
233 IFI_CANFD_IRQMASK_RXFIFO_NEMPTY
|
234 IFI_CANFD_IRQMASK_ERROR_STATE_CHG
|
235 IFI_CANFD_IRQMASK_ERROR_WARNING
|
236 IFI_CANFD_IRQMASK_ERROR_BUSOFF
;
237 if (priv
->can
.ctrlmode
& CAN_CTRLMODE_BERR_REPORTING
)
238 enirq
|= IFI_CANFD_INTERRUPT_ERROR_COUNTER
;
241 writel(IFI_CANFD_IRQMASK_SET_ERR
|
242 IFI_CANFD_IRQMASK_SET_TS
|
243 IFI_CANFD_IRQMASK_SET_TX
|
244 IFI_CANFD_IRQMASK_SET_RX
| enirq
,
245 priv
->base
+ IFI_CANFD_IRQMASK
);
248 static void ifi_canfd_read_fifo(struct net_device
*ndev
)
250 struct net_device_stats
*stats
= &ndev
->stats
;
251 struct ifi_canfd_priv
*priv
= netdev_priv(ndev
);
252 struct canfd_frame
*cf
;
254 const u32 rx_irq_mask
= IFI_CANFD_INTERRUPT_RXFIFO_NEMPTY
|
255 IFI_CANFD_INTERRUPT_RXFIFO_NEMPTY_PER
;
260 rxdlc
= readl(priv
->base
+ IFI_CANFD_RXFIFO_DLC
);
261 if (rxdlc
& IFI_CANFD_RXFIFO_DLC_EDL
)
262 skb
= alloc_canfd_skb(ndev
, &cf
);
264 skb
= alloc_can_skb(ndev
, (struct can_frame
**)&cf
);
271 dlc
= (rxdlc
>> IFI_CANFD_RXFIFO_DLC_DLC_OFFSET
) &
272 IFI_CANFD_RXFIFO_DLC_DLC_MASK
;
273 if (rxdlc
& IFI_CANFD_RXFIFO_DLC_EDL
)
274 cf
->len
= can_fd_dlc2len(dlc
);
276 cf
->len
= can_cc_dlc2len(dlc
);
278 rxid
= readl(priv
->base
+ IFI_CANFD_RXFIFO_ID
);
279 id
= (rxid
>> IFI_CANFD_RXFIFO_ID_ID_OFFSET
);
280 if (id
& IFI_CANFD_RXFIFO_ID_IDE
) {
281 id
&= IFI_CANFD_RXFIFO_ID_ID_XTD_MASK
;
283 * In case the Extended ID frame is received, the standard
284 * and extended part of the ID are swapped in the register,
285 * so swap them back to obtain the correct ID.
287 id
= (id
>> IFI_CANFD_RXFIFO_ID_ID_XTD_OFFSET
) |
288 ((id
& IFI_CANFD_RXFIFO_ID_ID_STD_MASK
) <<
289 IFI_CANFD_RXFIFO_ID_ID_XTD_WIDTH
);
292 id
&= IFI_CANFD_RXFIFO_ID_ID_STD_MASK
;
296 if (rxdlc
& IFI_CANFD_RXFIFO_DLC_ESI
) {
297 cf
->flags
|= CANFD_ESI
;
298 netdev_dbg(ndev
, "ESI Error\n");
301 if (!(rxdlc
& IFI_CANFD_RXFIFO_DLC_EDL
) &&
302 (rxdlc
& IFI_CANFD_RXFIFO_DLC_RTR
)) {
303 cf
->can_id
|= CAN_RTR_FLAG
;
305 if (rxdlc
& IFI_CANFD_RXFIFO_DLC_BRS
)
306 cf
->flags
|= CANFD_BRS
;
308 for (i
= 0; i
< cf
->len
; i
+= 4) {
309 *(u32
*)(cf
->data
+ i
) =
310 readl(priv
->base
+ IFI_CANFD_RXFIFO_DATA
+ i
);
314 /* Remove the packet from FIFO */
315 writel(IFI_CANFD_RXSTCMD_REMOVE_MSG
, priv
->base
+ IFI_CANFD_RXSTCMD
);
316 writel(rx_irq_mask
, priv
->base
+ IFI_CANFD_INTERRUPT
);
319 stats
->rx_bytes
+= cf
->len
;
321 netif_receive_skb(skb
);
324 static int ifi_canfd_do_rx_poll(struct net_device
*ndev
, int quota
)
326 struct ifi_canfd_priv
*priv
= netdev_priv(ndev
);
330 rxst
= readl(priv
->base
+ IFI_CANFD_RXSTCMD
);
331 if (rxst
& IFI_CANFD_RXSTCMD_EMPTY
) {
332 netdev_dbg(ndev
, "No messages in RX FIFO\n");
337 if (rxst
& IFI_CANFD_RXSTCMD_EMPTY
)
342 ifi_canfd_read_fifo(ndev
);
345 rxst
= readl(priv
->base
+ IFI_CANFD_RXSTCMD
);
349 can_led_event(ndev
, CAN_LED_EVENT_RX
);
354 static int ifi_canfd_handle_lost_msg(struct net_device
*ndev
)
356 struct net_device_stats
*stats
= &ndev
->stats
;
358 struct can_frame
*frame
;
360 netdev_err(ndev
, "RX FIFO overflow, message(s) lost.\n");
363 stats
->rx_over_errors
++;
365 skb
= alloc_can_err_skb(ndev
, &frame
);
369 frame
->can_id
|= CAN_ERR_CRTL
;
370 frame
->data
[1] = CAN_ERR_CRTL_RX_OVERFLOW
;
372 netif_receive_skb(skb
);
377 static int ifi_canfd_handle_lec_err(struct net_device
*ndev
)
379 struct ifi_canfd_priv
*priv
= netdev_priv(ndev
);
380 struct net_device_stats
*stats
= &ndev
->stats
;
381 struct can_frame
*cf
;
383 u32 errctr
= readl(priv
->base
+ IFI_CANFD_ERROR_CTR
);
384 const u32 errmask
= IFI_CANFD_ERROR_CTR_OVERLOAD_FIRST
|
385 IFI_CANFD_ERROR_CTR_ACK_ERROR_FIRST
|
386 IFI_CANFD_ERROR_CTR_BIT0_ERROR_FIRST
|
387 IFI_CANFD_ERROR_CTR_BIT1_ERROR_FIRST
|
388 IFI_CANFD_ERROR_CTR_STUFF_ERROR_FIRST
|
389 IFI_CANFD_ERROR_CTR_CRC_ERROR_FIRST
|
390 IFI_CANFD_ERROR_CTR_FORM_ERROR_FIRST
;
392 if (!(errctr
& errmask
)) /* No error happened. */
395 priv
->can
.can_stats
.bus_error
++;
398 /* Propagate the error condition to the CAN stack. */
399 skb
= alloc_can_err_skb(ndev
, &cf
);
403 /* Read the error counter register and check for new errors. */
404 cf
->can_id
|= CAN_ERR_PROT
| CAN_ERR_BUSERROR
;
406 if (errctr
& IFI_CANFD_ERROR_CTR_OVERLOAD_FIRST
)
407 cf
->data
[2] |= CAN_ERR_PROT_OVERLOAD
;
409 if (errctr
& IFI_CANFD_ERROR_CTR_ACK_ERROR_FIRST
)
410 cf
->data
[3] = CAN_ERR_PROT_LOC_ACK
;
412 if (errctr
& IFI_CANFD_ERROR_CTR_BIT0_ERROR_FIRST
)
413 cf
->data
[2] |= CAN_ERR_PROT_BIT0
;
415 if (errctr
& IFI_CANFD_ERROR_CTR_BIT1_ERROR_FIRST
)
416 cf
->data
[2] |= CAN_ERR_PROT_BIT1
;
418 if (errctr
& IFI_CANFD_ERROR_CTR_STUFF_ERROR_FIRST
)
419 cf
->data
[2] |= CAN_ERR_PROT_STUFF
;
421 if (errctr
& IFI_CANFD_ERROR_CTR_CRC_ERROR_FIRST
)
422 cf
->data
[3] = CAN_ERR_PROT_LOC_CRC_SEQ
;
424 if (errctr
& IFI_CANFD_ERROR_CTR_FORM_ERROR_FIRST
)
425 cf
->data
[2] |= CAN_ERR_PROT_FORM
;
427 /* Reset the error counter, ack the IRQ and re-enable the counter. */
428 writel(IFI_CANFD_ERROR_CTR_ER_RESET
, priv
->base
+ IFI_CANFD_ERROR_CTR
);
429 writel(IFI_CANFD_INTERRUPT_ERROR_COUNTER
,
430 priv
->base
+ IFI_CANFD_INTERRUPT
);
431 writel(IFI_CANFD_ERROR_CTR_ER_ENABLE
, priv
->base
+ IFI_CANFD_ERROR_CTR
);
434 stats
->rx_bytes
+= cf
->len
;
435 netif_receive_skb(skb
);
440 static int ifi_canfd_get_berr_counter(const struct net_device
*ndev
,
441 struct can_berr_counter
*bec
)
443 struct ifi_canfd_priv
*priv
= netdev_priv(ndev
);
446 err
= readl(priv
->base
+ IFI_CANFD_ERROR
);
447 bec
->rxerr
= (err
>> IFI_CANFD_ERROR_RX_OFFSET
) &
448 IFI_CANFD_ERROR_RX_MASK
;
449 bec
->txerr
= (err
>> IFI_CANFD_ERROR_TX_OFFSET
) &
450 IFI_CANFD_ERROR_TX_MASK
;
455 static int ifi_canfd_handle_state_change(struct net_device
*ndev
,
456 enum can_state new_state
)
458 struct ifi_canfd_priv
*priv
= netdev_priv(ndev
);
459 struct net_device_stats
*stats
= &ndev
->stats
;
460 struct can_frame
*cf
;
462 struct can_berr_counter bec
;
465 case CAN_STATE_ERROR_ACTIVE
:
466 /* error active state */
467 priv
->can
.can_stats
.error_warning
++;
468 priv
->can
.state
= CAN_STATE_ERROR_ACTIVE
;
470 case CAN_STATE_ERROR_WARNING
:
471 /* error warning state */
472 priv
->can
.can_stats
.error_warning
++;
473 priv
->can
.state
= CAN_STATE_ERROR_WARNING
;
475 case CAN_STATE_ERROR_PASSIVE
:
476 /* error passive state */
477 priv
->can
.can_stats
.error_passive
++;
478 priv
->can
.state
= CAN_STATE_ERROR_PASSIVE
;
480 case CAN_STATE_BUS_OFF
:
482 priv
->can
.state
= CAN_STATE_BUS_OFF
;
483 ifi_canfd_irq_enable(ndev
, 0);
484 priv
->can
.can_stats
.bus_off
++;
491 /* propagate the error condition to the CAN stack */
492 skb
= alloc_can_err_skb(ndev
, &cf
);
496 ifi_canfd_get_berr_counter(ndev
, &bec
);
499 case CAN_STATE_ERROR_WARNING
:
500 /* error warning state */
501 cf
->can_id
|= CAN_ERR_CRTL
;
502 cf
->data
[1] = (bec
.txerr
> bec
.rxerr
) ?
503 CAN_ERR_CRTL_TX_WARNING
:
504 CAN_ERR_CRTL_RX_WARNING
;
505 cf
->data
[6] = bec
.txerr
;
506 cf
->data
[7] = bec
.rxerr
;
508 case CAN_STATE_ERROR_PASSIVE
:
509 /* error passive state */
510 cf
->can_id
|= CAN_ERR_CRTL
;
511 cf
->data
[1] |= CAN_ERR_CRTL_RX_PASSIVE
;
513 cf
->data
[1] |= CAN_ERR_CRTL_TX_PASSIVE
;
514 cf
->data
[6] = bec
.txerr
;
515 cf
->data
[7] = bec
.rxerr
;
517 case CAN_STATE_BUS_OFF
:
519 cf
->can_id
|= CAN_ERR_BUSOFF
;
526 stats
->rx_bytes
+= cf
->len
;
527 netif_receive_skb(skb
);
532 static int ifi_canfd_handle_state_errors(struct net_device
*ndev
)
534 struct ifi_canfd_priv
*priv
= netdev_priv(ndev
);
535 u32 stcmd
= readl(priv
->base
+ IFI_CANFD_STCMD
);
538 if ((stcmd
& IFI_CANFD_STCMD_ERROR_ACTIVE
) &&
539 (priv
->can
.state
!= CAN_STATE_ERROR_ACTIVE
)) {
540 netdev_dbg(ndev
, "Error, entered active state\n");
541 work_done
+= ifi_canfd_handle_state_change(ndev
,
542 CAN_STATE_ERROR_ACTIVE
);
545 if ((stcmd
& IFI_CANFD_STCMD_ERROR_WARNING
) &&
546 (priv
->can
.state
!= CAN_STATE_ERROR_WARNING
)) {
547 netdev_dbg(ndev
, "Error, entered warning state\n");
548 work_done
+= ifi_canfd_handle_state_change(ndev
,
549 CAN_STATE_ERROR_WARNING
);
552 if ((stcmd
& IFI_CANFD_STCMD_ERROR_PASSIVE
) &&
553 (priv
->can
.state
!= CAN_STATE_ERROR_PASSIVE
)) {
554 netdev_dbg(ndev
, "Error, entered passive state\n");
555 work_done
+= ifi_canfd_handle_state_change(ndev
,
556 CAN_STATE_ERROR_PASSIVE
);
559 if ((stcmd
& IFI_CANFD_STCMD_BUSOFF
) &&
560 (priv
->can
.state
!= CAN_STATE_BUS_OFF
)) {
561 netdev_dbg(ndev
, "Error, entered bus-off state\n");
562 work_done
+= ifi_canfd_handle_state_change(ndev
,
569 static int ifi_canfd_poll(struct napi_struct
*napi
, int quota
)
571 struct net_device
*ndev
= napi
->dev
;
572 struct ifi_canfd_priv
*priv
= netdev_priv(ndev
);
573 u32 rxstcmd
= readl(priv
->base
+ IFI_CANFD_RXSTCMD
);
576 /* Handle bus state changes */
577 work_done
+= ifi_canfd_handle_state_errors(ndev
);
579 /* Handle lost messages on RX */
580 if (rxstcmd
& IFI_CANFD_RXSTCMD_OVERFLOW
)
581 work_done
+= ifi_canfd_handle_lost_msg(ndev
);
583 /* Handle lec errors on the bus */
584 if (priv
->can
.ctrlmode
& CAN_CTRLMODE_BERR_REPORTING
)
585 work_done
+= ifi_canfd_handle_lec_err(ndev
);
587 /* Handle normal messages on RX */
588 if (!(rxstcmd
& IFI_CANFD_RXSTCMD_EMPTY
))
589 work_done
+= ifi_canfd_do_rx_poll(ndev
, quota
- work_done
);
591 if (work_done
< quota
) {
592 napi_complete_done(napi
, work_done
);
593 ifi_canfd_irq_enable(ndev
, 1);
599 static irqreturn_t
ifi_canfd_isr(int irq
, void *dev_id
)
601 struct net_device
*ndev
= (struct net_device
*)dev_id
;
602 struct ifi_canfd_priv
*priv
= netdev_priv(ndev
);
603 struct net_device_stats
*stats
= &ndev
->stats
;
604 const u32 rx_irq_mask
= IFI_CANFD_INTERRUPT_RXFIFO_NEMPTY
|
605 IFI_CANFD_INTERRUPT_RXFIFO_NEMPTY_PER
|
606 IFI_CANFD_INTERRUPT_ERROR_COUNTER
|
607 IFI_CANFD_INTERRUPT_ERROR_STATE_CHG
|
608 IFI_CANFD_INTERRUPT_ERROR_WARNING
|
609 IFI_CANFD_INTERRUPT_ERROR_BUSOFF
;
610 const u32 tx_irq_mask
= IFI_CANFD_INTERRUPT_TXFIFO_EMPTY
|
611 IFI_CANFD_INTERRUPT_TXFIFO_REMOVE
;
612 const u32 clr_irq_mask
= ~((u32
)IFI_CANFD_INTERRUPT_SET_IRQ
);
615 isr
= readl(priv
->base
+ IFI_CANFD_INTERRUPT
);
621 /* Clear all pending interrupts but ErrWarn */
622 writel(clr_irq_mask
, priv
->base
+ IFI_CANFD_INTERRUPT
);
624 /* RX IRQ or bus warning, start NAPI */
625 if (isr
& rx_irq_mask
) {
626 ifi_canfd_irq_enable(ndev
, 0);
627 napi_schedule(&priv
->napi
);
631 if (isr
& IFI_CANFD_INTERRUPT_TXFIFO_REMOVE
) {
632 stats
->tx_bytes
+= can_get_echo_skb(ndev
, 0);
634 can_led_event(ndev
, CAN_LED_EVENT_TX
);
637 if (isr
& tx_irq_mask
)
638 netif_wake_queue(ndev
);
643 static const struct can_bittiming_const ifi_canfd_bittiming_const
= {
644 .name
= KBUILD_MODNAME
,
645 .tseg1_min
= 1, /* Time segment 1 = prop_seg + phase_seg1 */
647 .tseg2_min
= 2, /* Time segment 2 = phase_seg2 */
655 static void ifi_canfd_set_bittiming(struct net_device
*ndev
)
657 struct ifi_canfd_priv
*priv
= netdev_priv(ndev
);
658 const struct can_bittiming
*bt
= &priv
->can
.bittiming
;
659 const struct can_bittiming
*dbt
= &priv
->can
.data_bittiming
;
660 u16 brp
, sjw
, tseg1
, tseg2
, tdc
;
662 /* Configure bit timing */
665 tseg1
= bt
->prop_seg
+ bt
->phase_seg1
- 1;
666 tseg2
= bt
->phase_seg2
- 2;
667 writel((tseg2
<< IFI_CANFD_TIME_TIMEB_OFF
) |
668 (tseg1
<< IFI_CANFD_TIME_TIMEA_OFF
) |
669 (brp
<< IFI_CANFD_TIME_PRESCALE_OFF
) |
670 (sjw
<< IFI_CANFD_TIME_SJW_OFF_7_9_8_8
),
671 priv
->base
+ IFI_CANFD_TIME
);
673 /* Configure data bit timing */
676 tseg1
= dbt
->prop_seg
+ dbt
->phase_seg1
- 1;
677 tseg2
= dbt
->phase_seg2
- 2;
678 writel((tseg2
<< IFI_CANFD_TIME_TIMEB_OFF
) |
679 (tseg1
<< IFI_CANFD_TIME_TIMEA_OFF
) |
680 (brp
<< IFI_CANFD_TIME_PRESCALE_OFF
) |
681 (sjw
<< IFI_CANFD_TIME_SJW_OFF_7_9_8_8
),
682 priv
->base
+ IFI_CANFD_FTIME
);
684 /* Configure transmitter delay */
685 tdc
= dbt
->brp
* (dbt
->prop_seg
+ dbt
->phase_seg1
);
686 tdc
&= IFI_CANFD_TDELAY_MASK
;
687 writel(IFI_CANFD_TDELAY_EN
| tdc
, priv
->base
+ IFI_CANFD_TDELAY
);
690 static void ifi_canfd_set_filter(struct net_device
*ndev
, const u32 id
,
691 const u32 mask
, const u32 ident
)
693 struct ifi_canfd_priv
*priv
= netdev_priv(ndev
);
695 writel(mask
, priv
->base
+ IFI_CANFD_FILTER_MASK(id
));
696 writel(ident
, priv
->base
+ IFI_CANFD_FILTER_IDENT(id
));
699 static void ifi_canfd_set_filters(struct net_device
*ndev
)
701 /* Receive all CAN frames (standard ID) */
702 ifi_canfd_set_filter(ndev
, 0,
703 IFI_CANFD_FILTER_MASK_VALID
|
704 IFI_CANFD_FILTER_MASK_EXT
,
705 IFI_CANFD_FILTER_IDENT_VALID
);
707 /* Receive all CAN frames (extended ID) */
708 ifi_canfd_set_filter(ndev
, 1,
709 IFI_CANFD_FILTER_MASK_VALID
|
710 IFI_CANFD_FILTER_MASK_EXT
,
711 IFI_CANFD_FILTER_IDENT_VALID
|
712 IFI_CANFD_FILTER_IDENT_IDE
);
714 /* Receive all CANFD frames */
715 ifi_canfd_set_filter(ndev
, 2,
716 IFI_CANFD_FILTER_MASK_VALID
|
717 IFI_CANFD_FILTER_MASK_EDL
|
718 IFI_CANFD_FILTER_MASK_EXT
,
719 IFI_CANFD_FILTER_IDENT_VALID
|
720 IFI_CANFD_FILTER_IDENT_CANFD
|
721 IFI_CANFD_FILTER_IDENT_IDE
);
724 static void ifi_canfd_start(struct net_device
*ndev
)
726 struct ifi_canfd_priv
*priv
= netdev_priv(ndev
);
730 writel(IFI_CANFD_STCMD_HARDRESET
, priv
->base
+ IFI_CANFD_STCMD
);
731 writel(IFI_CANFD_STCMD_ENABLE_7_9_8_8_TIMING
,
732 priv
->base
+ IFI_CANFD_STCMD
);
734 ifi_canfd_set_bittiming(ndev
);
735 ifi_canfd_set_filters(ndev
);
738 writel(IFI_CANFD_RXSTCMD_RESET
, priv
->base
+ IFI_CANFD_RXSTCMD
);
739 writel(0, priv
->base
+ IFI_CANFD_RXSTCMD
);
740 writel(IFI_CANFD_TXSTCMD_RESET
, priv
->base
+ IFI_CANFD_TXSTCMD
);
741 writel(0, priv
->base
+ IFI_CANFD_TXSTCMD
);
743 /* Repeat transmission until successful */
744 writel(0, priv
->base
+ IFI_CANFD_REPEAT
);
745 writel(0, priv
->base
+ IFI_CANFD_SUSPEND
);
747 /* Clear all pending interrupts */
748 writel((u32
)(~IFI_CANFD_INTERRUPT_SET_IRQ
),
749 priv
->base
+ IFI_CANFD_INTERRUPT
);
751 stcmd
= IFI_CANFD_STCMD_ENABLE
| IFI_CANFD_STCMD_NORMAL_MODE
|
752 IFI_CANFD_STCMD_ENABLE_7_9_8_8_TIMING
;
754 if (priv
->can
.ctrlmode
& CAN_CTRLMODE_LISTENONLY
)
755 stcmd
|= IFI_CANFD_STCMD_BUSMONITOR
;
757 if (priv
->can
.ctrlmode
& CAN_CTRLMODE_LOOPBACK
)
758 stcmd
|= IFI_CANFD_STCMD_LOOPBACK
;
760 if ((priv
->can
.ctrlmode
& CAN_CTRLMODE_FD
) &&
761 !(priv
->can
.ctrlmode
& CAN_CTRLMODE_FD_NON_ISO
))
762 stcmd
|= IFI_CANFD_STCMD_ENABLE_ISO
;
764 if (!(priv
->can
.ctrlmode
& CAN_CTRLMODE_FD
))
765 stcmd
|= IFI_CANFD_STCMD_DISABLE_CANFD
;
767 priv
->can
.state
= CAN_STATE_ERROR_ACTIVE
;
769 ifi_canfd_irq_enable(ndev
, 1);
771 /* Unlock, reset and enable the error counter. */
772 writel(IFI_CANFD_ERROR_CTR_UNLOCK_MAGIC
,
773 priv
->base
+ IFI_CANFD_ERROR_CTR
);
774 writel(IFI_CANFD_ERROR_CTR_ER_RESET
, priv
->base
+ IFI_CANFD_ERROR_CTR
);
775 writel(IFI_CANFD_ERROR_CTR_ER_ENABLE
, priv
->base
+ IFI_CANFD_ERROR_CTR
);
777 /* Enable controller */
778 writel(stcmd
, priv
->base
+ IFI_CANFD_STCMD
);
781 static void ifi_canfd_stop(struct net_device
*ndev
)
783 struct ifi_canfd_priv
*priv
= netdev_priv(ndev
);
785 /* Reset and disable the error counter. */
786 writel(IFI_CANFD_ERROR_CTR_ER_RESET
, priv
->base
+ IFI_CANFD_ERROR_CTR
);
787 writel(0, priv
->base
+ IFI_CANFD_ERROR_CTR
);
790 writel(IFI_CANFD_STCMD_HARDRESET
, priv
->base
+ IFI_CANFD_STCMD
);
792 /* Mask all interrupts */
793 writel(~0, priv
->base
+ IFI_CANFD_IRQMASK
);
795 /* Clear all pending interrupts */
796 writel((u32
)(~IFI_CANFD_INTERRUPT_SET_IRQ
),
797 priv
->base
+ IFI_CANFD_INTERRUPT
);
799 /* Set the state as STOPPED */
800 priv
->can
.state
= CAN_STATE_STOPPED
;
803 static int ifi_canfd_set_mode(struct net_device
*ndev
, enum can_mode mode
)
807 ifi_canfd_start(ndev
);
808 netif_wake_queue(ndev
);
817 static int ifi_canfd_open(struct net_device
*ndev
)
819 struct ifi_canfd_priv
*priv
= netdev_priv(ndev
);
822 ret
= open_candev(ndev
);
824 netdev_err(ndev
, "Failed to open CAN device\n");
828 /* Register interrupt handler */
829 ret
= request_irq(ndev
->irq
, ifi_canfd_isr
, IRQF_SHARED
,
832 netdev_err(ndev
, "Failed to request interrupt\n");
836 ifi_canfd_start(ndev
);
838 can_led_event(ndev
, CAN_LED_EVENT_OPEN
);
839 napi_enable(&priv
->napi
);
840 netif_start_queue(ndev
);
848 static int ifi_canfd_close(struct net_device
*ndev
)
850 struct ifi_canfd_priv
*priv
= netdev_priv(ndev
);
852 netif_stop_queue(ndev
);
853 napi_disable(&priv
->napi
);
855 ifi_canfd_stop(ndev
);
857 free_irq(ndev
->irq
, ndev
);
861 can_led_event(ndev
, CAN_LED_EVENT_STOP
);
866 static netdev_tx_t
ifi_canfd_start_xmit(struct sk_buff
*skb
,
867 struct net_device
*ndev
)
869 struct ifi_canfd_priv
*priv
= netdev_priv(ndev
);
870 struct canfd_frame
*cf
= (struct canfd_frame
*)skb
->data
;
871 u32 txst
, txid
, txdlc
;
874 if (can_dropped_invalid_skb(ndev
, skb
))
877 /* Check if the TX buffer is full */
878 txst
= readl(priv
->base
+ IFI_CANFD_TXSTCMD
);
879 if (txst
& IFI_CANFD_TXSTCMD_FULL
) {
880 netif_stop_queue(ndev
);
881 netdev_err(ndev
, "BUG! TX FIFO full when queue awake!\n");
882 return NETDEV_TX_BUSY
;
885 netif_stop_queue(ndev
);
887 if (cf
->can_id
& CAN_EFF_FLAG
) {
888 txid
= cf
->can_id
& CAN_EFF_MASK
;
890 * In case the Extended ID frame is transmitted, the
891 * standard and extended part of the ID are swapped
892 * in the register, so swap them back to send the
895 txid
= (txid
>> IFI_CANFD_TXFIFO_ID_ID_XTD_WIDTH
) |
896 ((txid
& IFI_CANFD_TXFIFO_ID_ID_XTD_MASK
) <<
897 IFI_CANFD_TXFIFO_ID_ID_XTD_OFFSET
);
898 txid
|= IFI_CANFD_TXFIFO_ID_IDE
;
900 txid
= cf
->can_id
& CAN_SFF_MASK
;
903 txdlc
= can_fd_len2dlc(cf
->len
);
904 if ((priv
->can
.ctrlmode
& CAN_CTRLMODE_FD
) && can_is_canfd_skb(skb
)) {
905 txdlc
|= IFI_CANFD_TXFIFO_DLC_EDL
;
906 if (cf
->flags
& CANFD_BRS
)
907 txdlc
|= IFI_CANFD_TXFIFO_DLC_BRS
;
910 if (cf
->can_id
& CAN_RTR_FLAG
)
911 txdlc
|= IFI_CANFD_TXFIFO_DLC_RTR
;
913 /* message ram configuration */
914 writel(txid
, priv
->base
+ IFI_CANFD_TXFIFO_ID
);
915 writel(txdlc
, priv
->base
+ IFI_CANFD_TXFIFO_DLC
);
917 for (i
= 0; i
< cf
->len
; i
+= 4) {
918 writel(*(u32
*)(cf
->data
+ i
),
919 priv
->base
+ IFI_CANFD_TXFIFO_DATA
+ i
);
922 writel(0, priv
->base
+ IFI_CANFD_TXFIFO_REPEATCOUNT
);
923 writel(0, priv
->base
+ IFI_CANFD_TXFIFO_SUSPEND_US
);
925 can_put_echo_skb(skb
, ndev
, 0);
927 /* Start the transmission */
928 writel(IFI_CANFD_TXSTCMD_ADD_MSG
, priv
->base
+ IFI_CANFD_TXSTCMD
);
933 static const struct net_device_ops ifi_canfd_netdev_ops
= {
934 .ndo_open
= ifi_canfd_open
,
935 .ndo_stop
= ifi_canfd_close
,
936 .ndo_start_xmit
= ifi_canfd_start_xmit
,
937 .ndo_change_mtu
= can_change_mtu
,
940 static int ifi_canfd_plat_probe(struct platform_device
*pdev
)
942 struct device
*dev
= &pdev
->dev
;
943 struct net_device
*ndev
;
944 struct ifi_canfd_priv
*priv
;
949 addr
= devm_platform_ioremap_resource(pdev
, 0);
951 return PTR_ERR(addr
);
953 irq
= platform_get_irq(pdev
, 0);
957 id
= readl(addr
+ IFI_CANFD_IP_ID
);
958 if (id
!= IFI_CANFD_IP_ID_VALUE
) {
959 dev_err(dev
, "This block is not IFI CANFD, id=%08x\n", id
);
963 rev
= readl(addr
+ IFI_CANFD_VER
) & IFI_CANFD_VER_REV_MASK
;
964 if (rev
< IFI_CANFD_VER_REV_MIN_SUPPORTED
) {
965 dev_err(dev
, "This block is too old (rev %i), minimum supported is rev %i\n",
966 rev
, IFI_CANFD_VER_REV_MIN_SUPPORTED
);
970 ndev
= alloc_candev(sizeof(*priv
), 1);
975 ndev
->flags
|= IFF_ECHO
; /* we support local echo */
976 ndev
->netdev_ops
= &ifi_canfd_netdev_ops
;
978 priv
= netdev_priv(ndev
);
982 netif_napi_add(ndev
, &priv
->napi
, ifi_canfd_poll
, 64);
984 priv
->can
.state
= CAN_STATE_STOPPED
;
986 priv
->can
.clock
.freq
= readl(addr
+ IFI_CANFD_CANCLOCK
);
988 priv
->can
.bittiming_const
= &ifi_canfd_bittiming_const
;
989 priv
->can
.data_bittiming_const
= &ifi_canfd_bittiming_const
;
990 priv
->can
.do_set_mode
= ifi_canfd_set_mode
;
991 priv
->can
.do_get_berr_counter
= ifi_canfd_get_berr_counter
;
993 /* IFI CANFD can do both Bosch FD and ISO FD */
994 priv
->can
.ctrlmode
= CAN_CTRLMODE_FD
;
996 /* IFI CANFD can do both Bosch FD and ISO FD */
997 priv
->can
.ctrlmode_supported
= CAN_CTRLMODE_LOOPBACK
|
998 CAN_CTRLMODE_LISTENONLY
|
1000 CAN_CTRLMODE_FD_NON_ISO
|
1001 CAN_CTRLMODE_BERR_REPORTING
;
1003 platform_set_drvdata(pdev
, ndev
);
1004 SET_NETDEV_DEV(ndev
, dev
);
1006 ret
= register_candev(ndev
);
1008 dev_err(dev
, "Failed to register (ret=%d)\n", ret
);
1012 devm_can_led_init(ndev
);
1014 dev_info(dev
, "Driver registered: regs=%p, irq=%d, clock=%d\n",
1015 priv
->base
, ndev
->irq
, priv
->can
.clock
.freq
);
1024 static int ifi_canfd_plat_remove(struct platform_device
*pdev
)
1026 struct net_device
*ndev
= platform_get_drvdata(pdev
);
1028 unregister_candev(ndev
);
1029 platform_set_drvdata(pdev
, NULL
);
1035 static const struct of_device_id ifi_canfd_of_table
[] = {
1036 { .compatible
= "ifi,canfd-1.0", .data
= NULL
},
1039 MODULE_DEVICE_TABLE(of
, ifi_canfd_of_table
);
1041 static struct platform_driver ifi_canfd_plat_driver
= {
1043 .name
= KBUILD_MODNAME
,
1044 .of_match_table
= ifi_canfd_of_table
,
1046 .probe
= ifi_canfd_plat_probe
,
1047 .remove
= ifi_canfd_plat_remove
,
1050 module_platform_driver(ifi_canfd_plat_driver
);
1052 MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
1053 MODULE_LICENSE("GPL v2");
1054 MODULE_DESCRIPTION("CAN bus driver for IFI CANFD controller");