xref: /linux/drivers/net/can/rockchip/rockchip_canfd.h (revision a5605d61c7dd9473498d402a725ca49c915b0ac4)
1 /* SPDX-License-Identifier: GPL-2.0
2  *
3  * Copyright (c) 2023, 2024 Pengutronix,
4  *               Marc Kleine-Budde <kernel@pengutronix.de>
5  */
6 
7 #ifndef _ROCKCHIP_CANFD_H
8 #define _ROCKCHIP_CANFD_H
9 
10 #include <linux/bitfield.h>
11 #include <linux/can/dev.h>
12 #include <linux/can/rx-offload.h>
13 #include <linux/clk.h>
14 #include <linux/io.h>
15 #include <linux/netdevice.h>
16 #include <linux/reset.h>
17 #include <linux/skbuff.h>
18 #include <linux/types.h>
19 #include <linux/u64_stats_sync.h>
20 #include <linux/units.h>
21 
22 #define RKCANFD_REG_MODE 0x000
23 #define RKCANFD_REG_MODE_CAN_FD_MODE_ENABLE BIT(15)
24 #define RKCANFD_REG_MODE_DPEE BIT(14)
25 #define RKCANFD_REG_MODE_BRSD BIT(13)
26 #define RKCANFD_REG_MODE_SPACE_RX_MODE BIT(12)
27 #define RKCANFD_REG_MODE_AUTO_BUS_ON BIT(11)
28 #define RKCANFD_REG_MODE_AUTO_RETX_MODE BIT(10)
29 #define RKCANFD_REG_MODE_OVLD_MODE BIT(9)
30 #define RKCANFD_REG_MODE_COVER_MODE BIT(8)
31 #define RKCANFD_REG_MODE_RXSORT_MODE BIT(7)
32 #define RKCANFD_REG_MODE_TXORDER_MODE BIT(6)
33 #define RKCANFD_REG_MODE_RXSTX_MODE BIT(5)
34 #define RKCANFD_REG_MODE_LBACK_MODE BIT(4)
35 #define RKCANFD_REG_MODE_SILENT_MODE BIT(3)
36 #define RKCANFD_REG_MODE_SELF_TEST BIT(2)
37 #define RKCANFD_REG_MODE_SLEEP_MODE BIT(1)
38 #define RKCANFD_REG_MODE_WORK_MODE BIT(0)
39 
40 #define RKCANFD_REG_CMD 0x004
41 #define RKCANFD_REG_CMD_TX1_REQ BIT(1)
42 #define RKCANFD_REG_CMD_TX0_REQ BIT(0)
43 #define RKCANFD_REG_CMD_TX_REQ(i) (RKCANFD_REG_CMD_TX0_REQ << (i))
44 
45 #define RKCANFD_REG_STATE 0x008
46 #define RKCANFD_REG_STATE_SLEEP_STATE BIT(6)
47 #define RKCANFD_REG_STATE_BUS_OFF_STATE BIT(5)
48 #define RKCANFD_REG_STATE_ERROR_WARNING_STATE BIT(4)
49 #define RKCANFD_REG_STATE_TX_PERIOD BIT(3)
50 #define RKCANFD_REG_STATE_RX_PERIOD BIT(2)
51 #define RKCANFD_REG_STATE_TX_BUFFER_FULL BIT(1)
52 #define RKCANFD_REG_STATE_RX_BUFFER_FULL BIT(0)
53 
54 #define RKCANFD_REG_INT 0x00c
55 #define RKCANFD_REG_INT_WAKEUP_INT BIT(14)
56 #define RKCANFD_REG_INT_TXE_FIFO_FULL_INT BIT(13)
57 #define RKCANFD_REG_INT_TXE_FIFO_OV_INT BIT(12)
58 #define RKCANFD_REG_INT_TIMESTAMP_COUNTER_OVERFLOW_INT BIT(11)
59 #define RKCANFD_REG_INT_BUS_OFF_RECOVERY_INT BIT(10)
60 #define RKCANFD_REG_INT_BUS_OFF_INT BIT(9)
61 #define RKCANFD_REG_INT_RX_FIFO_OVERFLOW_INT BIT(8)
62 #define RKCANFD_REG_INT_RX_FIFO_FULL_INT BIT(7)
63 #define RKCANFD_REG_INT_ERROR_INT BIT(6)
64 #define RKCANFD_REG_INT_TX_ARBIT_FAIL_INT BIT(5)
65 #define RKCANFD_REG_INT_PASSIVE_ERROR_INT BIT(4)
66 #define RKCANFD_REG_INT_OVERLOAD_INT BIT(3)
67 #define RKCANFD_REG_INT_ERROR_WARNING_INT BIT(2)
68 #define RKCANFD_REG_INT_TX_FINISH_INT BIT(1)
69 #define RKCANFD_REG_INT_RX_FINISH_INT BIT(0)
70 
71 #define RKCANFD_REG_INT_ALL \
72 	(RKCANFD_REG_INT_WAKEUP_INT | \
73 	 RKCANFD_REG_INT_TXE_FIFO_FULL_INT | \
74 	 RKCANFD_REG_INT_TXE_FIFO_OV_INT | \
75 	 RKCANFD_REG_INT_TIMESTAMP_COUNTER_OVERFLOW_INT | \
76 	 RKCANFD_REG_INT_BUS_OFF_RECOVERY_INT | \
77 	 RKCANFD_REG_INT_BUS_OFF_INT | \
78 	 RKCANFD_REG_INT_RX_FIFO_OVERFLOW_INT | \
79 	 RKCANFD_REG_INT_RX_FIFO_FULL_INT | \
80 	 RKCANFD_REG_INT_ERROR_INT | \
81 	 RKCANFD_REG_INT_TX_ARBIT_FAIL_INT | \
82 	 RKCANFD_REG_INT_PASSIVE_ERROR_INT | \
83 	 RKCANFD_REG_INT_OVERLOAD_INT | \
84 	 RKCANFD_REG_INT_ERROR_WARNING_INT | \
85 	 RKCANFD_REG_INT_TX_FINISH_INT | \
86 	 RKCANFD_REG_INT_RX_FINISH_INT)
87 
88 #define RKCANFD_REG_INT_ALL_ERROR \
89 	(RKCANFD_REG_INT_BUS_OFF_INT | \
90 	 RKCANFD_REG_INT_ERROR_INT | \
91 	 RKCANFD_REG_INT_PASSIVE_ERROR_INT | \
92 	 RKCANFD_REG_INT_ERROR_WARNING_INT)
93 
94 #define RKCANFD_REG_INT_MASK 0x010
95 
96 #define RKCANFD_REG_DMA_CTL 0x014
97 #define RKCANFD_REG_DMA_CTL_DMA_RX_MODE BIT(1)
98 #define RKCANFD_REG_DMA_CTL_DMA_TX_MODE BIT(9)
99 
100 #define RKCANFD_REG_BITTIMING 0x018
101 #define RKCANFD_REG_BITTIMING_SAMPLE_MODE BIT(16)
102 #define RKCANFD_REG_BITTIMING_SJW GENMASK(15, 14)
103 #define RKCANFD_REG_BITTIMING_BRP GENMASK(13, 8)
104 #define RKCANFD_REG_BITTIMING_TSEG2 GENMASK(6, 4)
105 #define RKCANFD_REG_BITTIMING_TSEG1 GENMASK(3, 0)
106 
107 #define RKCANFD_REG_ARBITFAIL 0x028
108 #define RKCANFD_REG_ARBITFAIL_ARBIT_FAIL_CODE GENMASK(6, 0)
109 
110 /* Register seems to be clear or read */
111 #define RKCANFD_REG_ERROR_CODE 0x02c
112 #define RKCANFD_REG_ERROR_CODE_PHASE BIT(29)
113 #define RKCANFD_REG_ERROR_CODE_TYPE GENMASK(28, 26)
114 #define RKCANFD_REG_ERROR_CODE_TYPE_BIT 0x0
115 #define RKCANFD_REG_ERROR_CODE_TYPE_STUFF 0x1
116 #define RKCANFD_REG_ERROR_CODE_TYPE_FORM 0x2
117 #define RKCANFD_REG_ERROR_CODE_TYPE_ACK 0x3
118 #define RKCANFD_REG_ERROR_CODE_TYPE_CRC 0x4
119 #define RKCANFD_REG_ERROR_CODE_DIRECTION_RX BIT(25)
120 #define RKCANFD_REG_ERROR_CODE_TX GENMASK(24, 16)
121 #define RKCANFD_REG_ERROR_CODE_TX_OVERLOAD BIT(24)
122 #define RKCANFD_REG_ERROR_CODE_TX_ERROR BIT(23)
123 #define RKCANFD_REG_ERROR_CODE_TX_ACK BIT(22)
124 #define RKCANFD_REG_ERROR_CODE_TX_ACK_EOF BIT(21)
125 #define RKCANFD_REG_ERROR_CODE_TX_CRC BIT(20)
126 #define RKCANFD_REG_ERROR_CODE_TX_STUFF_COUNT BIT(19)
127 #define RKCANFD_REG_ERROR_CODE_TX_DATA BIT(18)
128 #define RKCANFD_REG_ERROR_CODE_TX_SOF_DLC BIT(17)
129 #define RKCANFD_REG_ERROR_CODE_TX_IDLE BIT(16)
130 #define RKCANFD_REG_ERROR_CODE_RX GENMASK(15, 0)
131 #define RKCANFD_REG_ERROR_CODE_RX_BUF_INT BIT(15)
132 #define RKCANFD_REG_ERROR_CODE_RX_SPACE BIT(14)
133 #define RKCANFD_REG_ERROR_CODE_RX_EOF BIT(13)
134 #define RKCANFD_REG_ERROR_CODE_RX_ACK_LIM BIT(12)
135 #define RKCANFD_REG_ERROR_CODE_RX_ACK BIT(11)
136 #define RKCANFD_REG_ERROR_CODE_RX_CRC_LIM BIT(10)
137 #define RKCANFD_REG_ERROR_CODE_RX_CRC BIT(9)
138 #define RKCANFD_REG_ERROR_CODE_RX_STUFF_COUNT BIT(8)
139 #define RKCANFD_REG_ERROR_CODE_RX_DATA BIT(7)
140 #define RKCANFD_REG_ERROR_CODE_RX_DLC BIT(6)
141 #define RKCANFD_REG_ERROR_CODE_RX_BRS_ESI BIT(5)
142 #define RKCANFD_REG_ERROR_CODE_RX_RES BIT(4)
143 #define RKCANFD_REG_ERROR_CODE_RX_FDF BIT(3)
144 #define RKCANFD_REG_ERROR_CODE_RX_ID2_RTR BIT(2)
145 #define RKCANFD_REG_ERROR_CODE_RX_SOF_IDE BIT(1)
146 #define RKCANFD_REG_ERROR_CODE_RX_IDLE BIT(0)
147 
148 #define RKCANFD_REG_ERROR_CODE_NOACK \
149 	(FIELD_PREP(RKCANFD_REG_ERROR_CODE_TYPE, \
150 		    RKCANFD_REG_ERROR_CODE_TYPE_ACK) | \
151 	 RKCANFD_REG_ERROR_CODE_TX_ACK_EOF | \
152 	 RKCANFD_REG_ERROR_CODE_RX_ACK)
153 
154 #define RKCANFD_REG_RXERRORCNT 0x034
155 #define RKCANFD_REG_RXERRORCNT_RX_ERR_CNT GENMASK(7, 0)
156 
157 #define RKCANFD_REG_TXERRORCNT 0x038
158 #define RKCANFD_REG_TXERRORCNT_TX_ERR_CNT GENMASK(8, 0)
159 
160 #define RKCANFD_REG_IDCODE 0x03c
161 #define RKCANFD_REG_IDCODE_STANDARD_FRAME_ID GENMASK(10, 0)
162 #define RKCANFD_REG_IDCODE_EXTENDED_FRAME_ID GENMASK(28, 0)
163 
164 #define RKCANFD_REG_IDMASK 0x040
165 
166 #define RKCANFD_REG_TXFRAMEINFO 0x050
167 #define RKCANFD_REG_FRAMEINFO_FRAME_FORMAT BIT(7)
168 #define RKCANFD_REG_FRAMEINFO_RTR BIT(6)
169 #define RKCANFD_REG_FRAMEINFO_DATA_LENGTH GENMASK(3, 0)
170 
171 #define RKCANFD_REG_TXID 0x054
172 #define RKCANFD_REG_TXID_TX_ID GENMASK(28, 0)
173 
174 #define RKCANFD_REG_TXDATA0 0x058
175 #define RKCANFD_REG_TXDATA1 0x05C
176 #define RKCANFD_REG_RXFRAMEINFO 0x060
177 #define RKCANFD_REG_RXID 0x064
178 #define RKCANFD_REG_RXDATA0 0x068
179 #define RKCANFD_REG_RXDATA1 0x06c
180 
181 #define RKCANFD_REG_RTL_VERSION 0x070
182 #define RKCANFD_REG_RTL_VERSION_MAJOR GENMASK(7, 4)
183 #define RKCANFD_REG_RTL_VERSION_MINOR GENMASK(3, 0)
184 
185 #define RKCANFD_REG_FD_NOMINAL_BITTIMING 0x100
186 #define RKCANFD_REG_FD_NOMINAL_BITTIMING_SAMPLE_MODE BIT(31)
187 #define RKCANFD_REG_FD_NOMINAL_BITTIMING_SJW GENMASK(30, 24)
188 #define RKCANFD_REG_FD_NOMINAL_BITTIMING_BRP GENMASK(23, 16)
189 #define RKCANFD_REG_FD_NOMINAL_BITTIMING_TSEG2 GENMASK(14, 8)
190 #define RKCANFD_REG_FD_NOMINAL_BITTIMING_TSEG1 GENMASK(7, 0)
191 
192 #define RKCANFD_REG_FD_DATA_BITTIMING 0x104
193 #define RKCANFD_REG_FD_DATA_BITTIMING_SAMPLE_MODE BIT(21)
194 #define RKCANFD_REG_FD_DATA_BITTIMING_SJW GENMASK(20, 17)
195 #define RKCANFD_REG_FD_DATA_BITTIMING_BRP GENMASK(16, 9)
196 #define RKCANFD_REG_FD_DATA_BITTIMING_TSEG2 GENMASK(8, 5)
197 #define RKCANFD_REG_FD_DATA_BITTIMING_TSEG1 GENMASK(4, 0)
198 
199 #define RKCANFD_REG_TRANSMIT_DELAY_COMPENSATION 0x108
200 #define RKCANFD_REG_TRANSMIT_DELAY_COMPENSATION_TDC_OFFSET GENMASK(6, 1)
201 #define RKCANFD_REG_TRANSMIT_DELAY_COMPENSATION_TDC_ENABLE BIT(0)
202 
203 #define RKCANFD_REG_TIMESTAMP_CTRL 0x10c
204 /* datasheet says 6:1, which is wrong */
205 #define RKCANFD_REG_TIMESTAMP_CTRL_TIME_BASE_COUNTER_PRESCALE GENMASK(5, 1)
206 #define RKCANFD_REG_TIMESTAMP_CTRL_TIME_BASE_COUNTER_ENABLE BIT(0)
207 
208 #define RKCANFD_REG_TIMESTAMP 0x110
209 
210 #define RKCANFD_REG_TXEVENT_FIFO_CTRL 0x114
211 #define RKCANFD_REG_TXEVENT_FIFO_CTRL_TXE_FIFO_CNT GENMASK(8, 5)
212 #define RKCANFD_REG_TXEVENT_FIFO_CTRL_TXE_FIFO_WATERMARK GENMASK(4, 1)
213 #define RKCANFD_REG_TXEVENT_FIFO_CTRL_TXE_FIFO_ENABLE BIT(0)
214 
215 #define RKCANFD_REG_RX_FIFO_CTRL 0x118
216 #define RKCANFD_REG_RX_FIFO_CTRL_RX_FIFO_CNT GENMASK(6, 4)
217 #define RKCANFD_REG_RX_FIFO_CTRL_RX_FIFO_FULL_WATERMARK GENMASK(3, 1)
218 #define RKCANFD_REG_RX_FIFO_CTRL_RX_FIFO_ENABLE BIT(0)
219 
220 #define RKCANFD_REG_AFC_CTRL 0x11c
221 #define RKCANFD_REG_AFC_CTRL_UAF5 BIT(4)
222 #define RKCANFD_REG_AFC_CTRL_UAF4 BIT(3)
223 #define RKCANFD_REG_AFC_CTRL_UAF3 BIT(2)
224 #define RKCANFD_REG_AFC_CTRL_UAF2 BIT(1)
225 #define RKCANFD_REG_AFC_CTRL_UAF1 BIT(0)
226 
227 #define RKCANFD_REG_IDCODE0 0x120
228 #define RKCANFD_REG_IDMASK0 0x124
229 #define RKCANFD_REG_IDCODE1 0x128
230 #define RKCANFD_REG_IDMASK1 0x12c
231 #define RKCANFD_REG_IDCODE2 0x130
232 #define RKCANFD_REG_IDMASK2 0x134
233 #define RKCANFD_REG_IDCODE3 0x138
234 #define RKCANFD_REG_IDMASK3 0x13c
235 #define RKCANFD_REG_IDCODE4 0x140
236 #define RKCANFD_REG_IDMASK4 0x144
237 
238 #define RKCANFD_REG_FD_TXFRAMEINFO 0x200
239 #define RKCANFD_REG_FD_FRAMEINFO_FRAME_FORMAT BIT(7)
240 #define RKCANFD_REG_FD_FRAMEINFO_RTR BIT(6)
241 #define RKCANFD_REG_FD_FRAMEINFO_FDF BIT(5)
242 #define RKCANFD_REG_FD_FRAMEINFO_BRS BIT(4)
243 #define RKCANFD_REG_FD_FRAMEINFO_DATA_LENGTH GENMASK(3, 0)
244 
245 #define RKCANFD_REG_FD_TXID 0x204
246 #define RKCANFD_REG_FD_ID_EFF GENMASK(28, 0)
247 #define RKCANFD_REG_FD_ID_SFF GENMASK(11, 0)
248 
249 #define RKCANFD_REG_FD_TXDATA0 0x208
250 #define RKCANFD_REG_FD_TXDATA1 0x20c
251 #define RKCANFD_REG_FD_TXDATA2 0x210
252 #define RKCANFD_REG_FD_TXDATA3 0x214
253 #define RKCANFD_REG_FD_TXDATA4 0x218
254 #define RKCANFD_REG_FD_TXDATA5 0x21c
255 #define RKCANFD_REG_FD_TXDATA6 0x220
256 #define RKCANFD_REG_FD_TXDATA7 0x224
257 #define RKCANFD_REG_FD_TXDATA8 0x228
258 #define RKCANFD_REG_FD_TXDATA9 0x22c
259 #define RKCANFD_REG_FD_TXDATA10 0x230
260 #define RKCANFD_REG_FD_TXDATA11 0x234
261 #define RKCANFD_REG_FD_TXDATA12 0x238
262 #define RKCANFD_REG_FD_TXDATA13 0x23c
263 #define RKCANFD_REG_FD_TXDATA14 0x240
264 #define RKCANFD_REG_FD_TXDATA15 0x244
265 
266 #define RKCANFD_REG_FD_RXFRAMEINFO 0x300
267 #define RKCANFD_REG_FD_RXID 0x304
268 #define RKCANFD_REG_FD_RXTIMESTAMP 0x308
269 #define RKCANFD_REG_FD_RXDATA0 0x30c
270 #define RKCANFD_REG_FD_RXDATA1 0x310
271 #define RKCANFD_REG_FD_RXDATA2 0x314
272 #define RKCANFD_REG_FD_RXDATA3 0x318
273 #define RKCANFD_REG_FD_RXDATA4 0x31c
274 #define RKCANFD_REG_FD_RXDATA5 0x320
275 #define RKCANFD_REG_FD_RXDATA6 0x320
276 #define RKCANFD_REG_FD_RXDATA7 0x328
277 #define RKCANFD_REG_FD_RXDATA8 0x32c
278 #define RKCANFD_REG_FD_RXDATA9 0x330
279 #define RKCANFD_REG_FD_RXDATA10 0x334
280 #define RKCANFD_REG_FD_RXDATA11 0x338
281 #define RKCANFD_REG_FD_RXDATA12 0x33c
282 #define RKCANFD_REG_FD_RXDATA13 0x340
283 #define RKCANFD_REG_FD_RXDATA14 0x344
284 #define RKCANFD_REG_FD_RXDATA15 0x348
285 
286 #define RKCANFD_REG_RX_FIFO_RDATA 0x400
287 #define RKCANFD_REG_TXE_FIFO_RDATA 0x500
288 
289 #define DEVICE_NAME "rockchip_canfd"
290 #define RKCANFD_NAPI_WEIGHT 32
291 #define RKCANFD_TXFIFO_DEPTH 2
292 #define RKCANFD_TX_STOP_THRESHOLD 1
293 #define RKCANFD_TX_START_THRESHOLD 1
294 
295 #define RKCANFD_TIMESTAMP_WORK_MAX_DELAY_SEC 60
296 #define RKCANFD_ERRATUM_5_SYSCLOCK_HZ_MIN (300 * MEGA)
297 
298 /* rk3568 CAN-FD Errata, as of Tue 07 Nov 2023 11:25:31 +08:00 */
299 
300 /* Erratum 1: The error frame sent by the CAN controller has an
301  * abnormal format.
302  */
303 #define RKCANFD_QUIRK_RK3568_ERRATUM_1 BIT(0)
304 
305 /* Erratum 2: The error frame sent after detecting a CRC error has an
306  * abnormal position.
307  */
308 #define RKCANFD_QUIRK_RK3568_ERRATUM_2 BIT(1)
309 
310 /* Erratum 3: Intermittent CRC calculation errors. */
311 #define RKCANFD_QUIRK_RK3568_ERRATUM_3 BIT(2)
312 
313 /* Erratum 4: Intermittent occurrence of stuffing errors. */
314 #define RKCANFD_QUIRK_RK3568_ERRATUM_4 BIT(3)
315 
316 /* Erratum 5: Counters related to the TXFIFO and RXFIFO exhibit
317  * abnormal counting behavior.
318  *
319  * The rk3568 CAN-FD errata sheet as of Tue 07 Nov 2023 11:25:31 +08:00
320  * states that only the rk3568v2 is affected by this erratum, but
321  * tests with the rk3568v2 and rk3568v3 show that the RX_FIFO_CNT is
322  * sometimes too high. This leads to CAN frames being read from the
323  * FIFO, which is then already empty.
324  *
325  * Further tests on the rk3568v2 and rk3568v3 show that in this
326  * situation (i.e. empty FIFO) all elements of the FIFO header
327  * (frameinfo, id, ts) contain the same data.
328  *
329  * On the rk3568v2 and rk3568v3, this problem only occurs extremely
330  * rarely with the standard clock of 300 MHz, but almost immediately
331  * at 80 MHz.
332  *
333  * To workaround this problem, check for empty FIFO with
334  * rkcanfd_fifo_header_empty() in rkcanfd_handle_rx_int_one() and exit
335  * early.
336  *
337  * To reproduce:
338  * assigned-clocks = <&cru CLK_CANx>;
339  * assigned-clock-rates = <80000000>;
340  */
341 #define RKCANFD_QUIRK_RK3568_ERRATUM_5 BIT(4)
342 
343 /* Erratum 6: The CAN controller's transmission of extended frames may
344  * intermittently change into standard frames
345  *
346  * Work around this issue by activating self reception (RXSTX). If we
347  * have pending TX CAN frames, check all RX'ed CAN frames in
348  * rkcanfd_rxstx_filter().
349  *
350  * If it's a frame we've send and it's OK, call the TX complete
351  * handler: rkcanfd_handle_tx_done_one(). Mask the TX complete IRQ.
352  *
353  * If it's a frame we've send, but the CAN-ID is mangled, resend the
354  * original extended frame.
355  *
356  * To reproduce:
357  * host:
358  *   canfdtest -evx -g can0
359  *   candump any,0:80000000 -cexdtA
360  * dut:
361  *   canfdtest -evx can0
362  *   ethtool -S can0
363  */
364 #define RKCANFD_QUIRK_RK3568_ERRATUM_6 BIT(5)
365 
366 /* Erratum 7: In the passive error state, the CAN controller's
367  * interframe space segment counting is inaccurate.
368  */
369 #define RKCANFD_QUIRK_RK3568_ERRATUM_7 BIT(6)
370 
371 /* Erratum 8: The Format-Error error flag is transmitted one bit
372  * later.
373  */
374 #define RKCANFD_QUIRK_RK3568_ERRATUM_8 BIT(7)
375 
376 /* Erratum 9: In the arbitration segment, the CAN controller will
377  * identify stuffing errors as arbitration failures.
378  */
379 #define RKCANFD_QUIRK_RK3568_ERRATUM_9 BIT(8)
380 
381 /* Erratum 10: Does not support the BUSOFF slow recovery mechanism. */
382 #define RKCANFD_QUIRK_RK3568_ERRATUM_10 BIT(9)
383 
384 /* Erratum 11: Arbitration error. */
385 #define RKCANFD_QUIRK_RK3568_ERRATUM_11 BIT(10)
386 
387 /* Erratum 12: A dominant bit at the third bit of the intermission may
388  * cause a transmission error.
389  */
390 #define RKCANFD_QUIRK_RK3568_ERRATUM_12 BIT(11)
391 
392 /* Tests on the rk3568v2 and rk3568v3 show that receiving certain
393  * CAN-FD frames trigger an Error Interrupt.
394  *
395  * - Form Error in RX Arbitration Phase: TX_IDLE RX_STUFF_COUNT (0x0a010100) CMD=0 RX=0 TX=0
396  *   Error-Warning=1 Bus-Off=0
397  *   To reproduce:
398  *   host:
399  *     cansend can0 002##01f
400  *   DUT:
401  *     candump any,0:0,#FFFFFFFF -cexdHtA
402  *
403  * - Form Error in RX Arbitration Phase: TX_IDLE RX_CRC (0x0a010200) CMD=0 RX=0 TX=0
404  *   Error-Warning=1 Bus-Off=0
405  *   To reproduce:
406  *   host:
407  *     cansend can0 002##07217010000000000
408  *   DUT:
409  *     candump any,0:0,#FFFFFFFF -cexdHtA
410  */
411 #define RKCANFD_QUIRK_CANFD_BROKEN BIT(12)
412 
413 /* known issues with rk3568v3:
414  *
415  * - Overload situation during high bus load
416  *   To reproduce:
417  *   host:
418  *     # add a 2nd CAN adapter to the CAN bus
419  *     cangen can0 -I 1 -Li -Di -p10 -g 0.3
420  *     cansequence -rve
421  *   DUT:
422  *     cangen can0 -I2 -L1 -Di -p10 -c10 -g 1 -e
423  *     cansequence -rv -i 1
424  *
425  * - TX starvation after repeated Bus-Off
426  *   To reproduce:
427  *   host:
428  *     sleep 3 && cangen can0 -I2 -Li -Di -p10 -g 0.0
429  *   DUT:
430  *     cangen can0 -I2 -Li -Di -p10 -g 0.05
431  */
432 
433 enum rkcanfd_model {
434 	RKCANFD_MODEL_RK3568V2 = 0x35682,
435 	RKCANFD_MODEL_RK3568V3 = 0x35683,
436 };
437 
438 struct rkcanfd_devtype_data {
439 	enum rkcanfd_model model;
440 	u32 quirks;
441 };
442 
443 struct rkcanfd_fifo_header {
444 	u32 frameinfo;
445 	u32 id;
446 	u32 ts;
447 };
448 
449 struct rkcanfd_stats {
450 	struct u64_stats_sync syncp;
451 
452 	/* Erratum 5 */
453 	u64_stats_t rx_fifo_empty_errors;
454 
455 	/* Erratum 6 */
456 	u64_stats_t tx_extended_as_standard_errors;
457 };
458 
459 struct rkcanfd_priv {
460 	struct can_priv can;
461 	struct can_rx_offload offload;
462 	struct net_device *ndev;
463 
464 	void __iomem *regs;
465 	unsigned int tx_head;
466 	unsigned int tx_tail;
467 
468 	u32 reg_mode_default;
469 	u32 reg_int_mask_default;
470 	struct rkcanfd_devtype_data devtype_data;
471 
472 	struct can_berr_counter bec;
473 
474 	struct rkcanfd_stats stats;
475 
476 	struct reset_control *reset;
477 	struct clk_bulk_data *clks;
478 	int clks_num;
479 };
480 
481 static inline u32
482 rkcanfd_read(const struct rkcanfd_priv *priv, u32 reg)
483 {
484 	return readl(priv->regs + reg);
485 }
486 
487 static inline void
488 rkcanfd_read_rep(const struct rkcanfd_priv *priv, u32 reg,
489 		 void *buf, unsigned int len)
490 {
491 	readsl(priv->regs + reg, buf, len / sizeof(u32));
492 }
493 
494 static inline void
495 rkcanfd_write(const struct rkcanfd_priv *priv, u32 reg, u32 val)
496 {
497 	writel(val, priv->regs + reg);
498 }
499 
500 static inline u32
501 rkcanfd_get_timestamp(const struct rkcanfd_priv *priv)
502 {
503 	return rkcanfd_read(priv, RKCANFD_REG_TIMESTAMP);
504 }
505 
506 static inline unsigned int
507 rkcanfd_get_tx_head(const struct rkcanfd_priv *priv)
508 {
509 	return READ_ONCE(priv->tx_head) & (RKCANFD_TXFIFO_DEPTH - 1);
510 }
511 
512 static inline unsigned int
513 rkcanfd_get_tx_tail(const struct rkcanfd_priv *priv)
514 {
515 	return READ_ONCE(priv->tx_tail) & (RKCANFD_TXFIFO_DEPTH - 1);
516 }
517 
518 static inline unsigned int
519 rkcanfd_get_tx_pending(const struct rkcanfd_priv *priv)
520 {
521 	return READ_ONCE(priv->tx_head) - READ_ONCE(priv->tx_tail);
522 }
523 
524 static inline unsigned int
525 rkcanfd_get_tx_free(const struct rkcanfd_priv *priv)
526 {
527 	return RKCANFD_TXFIFO_DEPTH - rkcanfd_get_tx_pending(priv);
528 }
529 
530 void rkcanfd_ethtool_init(struct rkcanfd_priv *priv);
531 
532 int rkcanfd_handle_rx_int(struct rkcanfd_priv *priv);
533 
534 void rkcanfd_timestamp_init(struct rkcanfd_priv *priv);
535 
536 unsigned int rkcanfd_get_effective_tx_free(const struct rkcanfd_priv *priv);
537 void rkcanfd_xmit_retry(struct rkcanfd_priv *priv);
538 int rkcanfd_start_xmit(struct sk_buff *skb, struct net_device *ndev);
539 void rkcanfd_handle_tx_done_one(struct rkcanfd_priv *priv, const u32 ts,
540 				unsigned int *frame_len_p);
541 
542 #endif
543