xref: /linux/drivers/net/can/rockchip/rockchip_canfd-core.c (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // Copyright (c) 2023, 2024 Pengutronix,
4 //               Marc Kleine-Budde <kernel@pengutronix.de>
5 //
6 // Based on:
7 //
8 // Rockchip CANFD driver
9 //
10 // Copyright (c) 2020 Rockchip Electronics Co. Ltd.
11 //
12 
13 #include <linux/delay.h>
14 #include <linux/errno.h>
15 #include <linux/init.h>
16 #include <linux/interrupt.h>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/of.h>
20 #include <linux/of_device.h>
21 #include <linux/platform_device.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/string.h>
24 
25 #include "rockchip_canfd.h"
26 
27 static const struct rkcanfd_devtype_data rkcanfd_devtype_data_rk3568v2 = {
28 	.model = RKCANFD_MODEL_RK3568V2,
29 	.quirks = RKCANFD_QUIRK_RK3568_ERRATUM_1 | RKCANFD_QUIRK_RK3568_ERRATUM_2 |
30 		RKCANFD_QUIRK_RK3568_ERRATUM_3 | RKCANFD_QUIRK_RK3568_ERRATUM_4 |
31 		RKCANFD_QUIRK_RK3568_ERRATUM_5 | RKCANFD_QUIRK_RK3568_ERRATUM_6 |
32 		RKCANFD_QUIRK_RK3568_ERRATUM_7 | RKCANFD_QUIRK_RK3568_ERRATUM_8 |
33 		RKCANFD_QUIRK_RK3568_ERRATUM_9 | RKCANFD_QUIRK_RK3568_ERRATUM_10 |
34 		RKCANFD_QUIRK_RK3568_ERRATUM_11 | RKCANFD_QUIRK_RK3568_ERRATUM_12 |
35 		RKCANFD_QUIRK_CANFD_BROKEN,
36 };
37 
38 /* The rk3568 CAN-FD errata sheet as of Tue 07 Nov 2023 11:25:31 +08:00
39  * states that only the rk3568v2 is affected by erratum 5, but tests
40  * with the rk3568v2 and rk3568v3 show that the RX_FIFO_CNT is
41  * sometimes too high. In contrast to the errata sheet mark rk3568v3
42  * as effected by erratum 5, too.
43  */
44 static const struct rkcanfd_devtype_data rkcanfd_devtype_data_rk3568v3 = {
45 	.model = RKCANFD_MODEL_RK3568V3,
46 	.quirks = RKCANFD_QUIRK_RK3568_ERRATUM_1 | RKCANFD_QUIRK_RK3568_ERRATUM_2 |
47 		RKCANFD_QUIRK_RK3568_ERRATUM_5 | RKCANFD_QUIRK_RK3568_ERRATUM_7 |
48 		RKCANFD_QUIRK_RK3568_ERRATUM_8 | RKCANFD_QUIRK_RK3568_ERRATUM_10 |
49 		RKCANFD_QUIRK_RK3568_ERRATUM_11 | RKCANFD_QUIRK_RK3568_ERRATUM_12 |
50 		RKCANFD_QUIRK_CANFD_BROKEN,
51 };
52 
53 static const char *__rkcanfd_get_model_str(enum rkcanfd_model model)
54 {
55 	switch (model) {
56 	case RKCANFD_MODEL_RK3568V2:
57 		return "rk3568v2";
58 	case RKCANFD_MODEL_RK3568V3:
59 		return "rk3568v3";
60 	}
61 
62 	return "<unknown>";
63 }
64 
65 static inline const char *
66 rkcanfd_get_model_str(const struct rkcanfd_priv *priv)
67 {
68 	return __rkcanfd_get_model_str(priv->devtype_data.model);
69 }
70 
71 /* Note:
72  *
73  * The formula to calculate the CAN System Clock is:
74  *
75  * Tsclk = 2 x Tclk x (brp + 1)
76  *
77  * Double the data sheet's brp_min, brp_max and brp_inc values (both
78  * for the arbitration and data bit timing) to take the "2 x" into
79  * account.
80  */
81 static const struct can_bittiming_const rkcanfd_bittiming_const = {
82 	.name = DEVICE_NAME,
83 	.tseg1_min = 1,
84 	.tseg1_max = 256,
85 	.tseg2_min = 1,
86 	.tseg2_max = 128,
87 	.sjw_max = 128,
88 	.brp_min = 2,	/* value from data sheet x2 */
89 	.brp_max = 512,	/* value from data sheet x2 */
90 	.brp_inc = 2,	/* value from data sheet x2 */
91 };
92 
93 static const struct can_bittiming_const rkcanfd_data_bittiming_const = {
94 	.name = DEVICE_NAME,
95 	.tseg1_min = 1,
96 	.tseg1_max = 32,
97 	.tseg2_min = 1,
98 	.tseg2_max = 16,
99 	.sjw_max = 16,
100 	.brp_min = 2,	/* value from data sheet x2 */
101 	.brp_max = 512,	/* value from data sheet x2 */
102 	.brp_inc = 2,	/* value from data sheet x2 */
103 };
104 
105 static void rkcanfd_chip_set_reset_mode(const struct rkcanfd_priv *priv)
106 {
107 	reset_control_assert(priv->reset);
108 	udelay(2);
109 	reset_control_deassert(priv->reset);
110 
111 	rkcanfd_write(priv, RKCANFD_REG_MODE, 0x0);
112 }
113 
114 static void rkcanfd_chip_set_work_mode(const struct rkcanfd_priv *priv)
115 {
116 	rkcanfd_write(priv, RKCANFD_REG_MODE, priv->reg_mode_default);
117 }
118 
119 static int rkcanfd_set_bittiming(struct rkcanfd_priv *priv)
120 {
121 	const struct can_bittiming *dbt = &priv->can.data_bittiming;
122 	const struct can_bittiming *bt = &priv->can.bittiming;
123 	u32 reg_nbt, reg_dbt, reg_tdc;
124 	u32 tdco;
125 
126 	reg_nbt = FIELD_PREP(RKCANFD_REG_FD_NOMINAL_BITTIMING_SJW,
127 			     bt->sjw - 1) |
128 		FIELD_PREP(RKCANFD_REG_FD_NOMINAL_BITTIMING_BRP,
129 			   (bt->brp / 2) - 1) |
130 		FIELD_PREP(RKCANFD_REG_FD_NOMINAL_BITTIMING_TSEG2,
131 			   bt->phase_seg2 - 1) |
132 		FIELD_PREP(RKCANFD_REG_FD_NOMINAL_BITTIMING_TSEG1,
133 			   bt->prop_seg + bt->phase_seg1 - 1);
134 
135 	rkcanfd_write(priv, RKCANFD_REG_FD_NOMINAL_BITTIMING, reg_nbt);
136 
137 	if (!(priv->can.ctrlmode & CAN_CTRLMODE_FD))
138 		return 0;
139 
140 	reg_dbt = FIELD_PREP(RKCANFD_REG_FD_DATA_BITTIMING_SJW,
141 			     dbt->sjw - 1) |
142 		FIELD_PREP(RKCANFD_REG_FD_DATA_BITTIMING_BRP,
143 			   (dbt->brp / 2) - 1) |
144 		FIELD_PREP(RKCANFD_REG_FD_DATA_BITTIMING_TSEG2,
145 			   dbt->phase_seg2 - 1) |
146 		FIELD_PREP(RKCANFD_REG_FD_DATA_BITTIMING_TSEG1,
147 			   dbt->prop_seg + dbt->phase_seg1 - 1);
148 
149 	rkcanfd_write(priv, RKCANFD_REG_FD_DATA_BITTIMING, reg_dbt);
150 
151 	tdco = (priv->can.clock.freq / dbt->bitrate) * 2 / 3;
152 	tdco = min(tdco, FIELD_MAX(RKCANFD_REG_TRANSMIT_DELAY_COMPENSATION_TDC_OFFSET));
153 
154 	reg_tdc = FIELD_PREP(RKCANFD_REG_TRANSMIT_DELAY_COMPENSATION_TDC_OFFSET, tdco) |
155 		RKCANFD_REG_TRANSMIT_DELAY_COMPENSATION_TDC_ENABLE;
156 	rkcanfd_write(priv, RKCANFD_REG_TRANSMIT_DELAY_COMPENSATION,
157 		      reg_tdc);
158 
159 	return 0;
160 }
161 
162 static void rkcanfd_get_berr_counter_corrected(struct rkcanfd_priv *priv,
163 					       struct can_berr_counter *bec)
164 {
165 	struct can_berr_counter bec_raw;
166 	u32 reg_state;
167 
168 	bec->rxerr = rkcanfd_read(priv, RKCANFD_REG_RXERRORCNT);
169 	bec->txerr = rkcanfd_read(priv, RKCANFD_REG_TXERRORCNT);
170 	bec_raw = *bec;
171 
172 	/* Tests show that sometimes both CAN bus error counters read
173 	 * 0x0, even if the controller is in warning mode
174 	 * (RKCANFD_REG_STATE_ERROR_WARNING_STATE in RKCANFD_REG_STATE
175 	 * set).
176 	 *
177 	 * In case both error counters read 0x0, use the struct
178 	 * priv->bec, otherwise save the read value to priv->bec.
179 	 *
180 	 * rkcanfd_handle_rx_int_one() handles the decrementing of
181 	 * priv->bec.rxerr for successfully RX'ed CAN frames.
182 	 *
183 	 * Luckily the controller doesn't decrement the RX CAN bus
184 	 * error counter in hardware for self received TX'ed CAN
185 	 * frames (RKCANFD_REG_MODE_RXSTX_MODE), so RXSTX doesn't
186 	 * interfere with proper RX CAN bus error counters.
187 	 *
188 	 * rkcanfd_handle_tx_done_one() handles the decrementing of
189 	 * priv->bec.txerr for successfully TX'ed CAN frames.
190 	 */
191 	if (!bec->rxerr && !bec->txerr)
192 		*bec = priv->bec;
193 	else
194 		priv->bec = *bec;
195 
196 	reg_state = rkcanfd_read(priv, RKCANFD_REG_STATE);
197 	netdev_vdbg(priv->ndev,
198 		    "%s: Raw/Cor: txerr=%3u/%3u rxerr=%3u/%3u Bus Off=%u Warning=%u\n",
199 		    __func__,
200 		    bec_raw.txerr, bec->txerr, bec_raw.rxerr, bec->rxerr,
201 		    !!(reg_state & RKCANFD_REG_STATE_BUS_OFF_STATE),
202 		    !!(reg_state & RKCANFD_REG_STATE_ERROR_WARNING_STATE));
203 }
204 
205 static int rkcanfd_get_berr_counter(const struct net_device *ndev,
206 				    struct can_berr_counter *bec)
207 {
208 	struct rkcanfd_priv *priv = netdev_priv(ndev);
209 	int err;
210 
211 	err = pm_runtime_resume_and_get(ndev->dev.parent);
212 	if (err)
213 		return err;
214 
215 	rkcanfd_get_berr_counter_corrected(priv, bec);
216 
217 	pm_runtime_put(ndev->dev.parent);
218 
219 	return 0;
220 }
221 
222 static void rkcanfd_chip_interrupts_enable(const struct rkcanfd_priv *priv)
223 {
224 	rkcanfd_write(priv, RKCANFD_REG_INT_MASK, priv->reg_int_mask_default);
225 
226 	netdev_dbg(priv->ndev, "%s: reg_int_mask=0x%08x\n", __func__,
227 		   rkcanfd_read(priv, RKCANFD_REG_INT_MASK));
228 }
229 
230 static void rkcanfd_chip_interrupts_disable(const struct rkcanfd_priv *priv)
231 {
232 	rkcanfd_write(priv, RKCANFD_REG_INT_MASK, RKCANFD_REG_INT_ALL);
233 }
234 
235 static void rkcanfd_chip_fifo_setup(struct rkcanfd_priv *priv)
236 {
237 	u32 reg;
238 
239 	/* TXE FIFO */
240 	reg = rkcanfd_read(priv, RKCANFD_REG_RX_FIFO_CTRL);
241 	reg |= RKCANFD_REG_RX_FIFO_CTRL_RX_FIFO_ENABLE;
242 	rkcanfd_write(priv, RKCANFD_REG_RX_FIFO_CTRL, reg);
243 
244 	/* RX FIFO */
245 	reg = rkcanfd_read(priv, RKCANFD_REG_RX_FIFO_CTRL);
246 	reg |= RKCANFD_REG_RX_FIFO_CTRL_RX_FIFO_ENABLE;
247 	rkcanfd_write(priv, RKCANFD_REG_RX_FIFO_CTRL, reg);
248 
249 	WRITE_ONCE(priv->tx_head, 0);
250 	WRITE_ONCE(priv->tx_tail, 0);
251 	netdev_reset_queue(priv->ndev);
252 }
253 
254 static void rkcanfd_chip_start(struct rkcanfd_priv *priv)
255 {
256 	u32 reg;
257 
258 	rkcanfd_chip_set_reset_mode(priv);
259 
260 	/* Receiving Filter: accept all */
261 	rkcanfd_write(priv, RKCANFD_REG_IDCODE, 0x0);
262 	rkcanfd_write(priv, RKCANFD_REG_IDMASK, RKCANFD_REG_IDCODE_EXTENDED_FRAME_ID);
263 
264 	/* enable:
265 	 * - CAN_FD: enable CAN-FD
266 	 * - AUTO_RETX_MODE: auto retransmission on TX error
267 	 * - COVER_MODE: RX-FIFO overwrite mode, do not send OVERLOAD frames
268 	 * - RXSTX_MODE: Receive Self Transmit data mode
269 	 * - WORK_MODE: transition from reset to working mode
270 	 */
271 	reg = rkcanfd_read(priv, RKCANFD_REG_MODE);
272 	priv->reg_mode_default = reg |
273 		RKCANFD_REG_MODE_CAN_FD_MODE_ENABLE |
274 		RKCANFD_REG_MODE_AUTO_RETX_MODE |
275 		RKCANFD_REG_MODE_COVER_MODE |
276 		RKCANFD_REG_MODE_RXSTX_MODE |
277 		RKCANFD_REG_MODE_WORK_MODE;
278 
279 	if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)
280 		priv->reg_mode_default |= RKCANFD_REG_MODE_LBACK_MODE |
281 			RKCANFD_REG_MODE_SILENT_MODE |
282 			RKCANFD_REG_MODE_SELF_TEST;
283 
284 	/* mask, i.e. ignore:
285 	 * - TIMESTAMP_COUNTER_OVERFLOW_INT - timestamp counter overflow interrupt
286 	 * - TX_ARBIT_FAIL_INT - TX arbitration fail interrupt
287 	 * - OVERLOAD_INT - CAN bus overload interrupt
288 	 * - TX_FINISH_INT - Transmit finish interrupt
289 	 */
290 	priv->reg_int_mask_default =
291 		RKCANFD_REG_INT_TIMESTAMP_COUNTER_OVERFLOW_INT |
292 		RKCANFD_REG_INT_TX_ARBIT_FAIL_INT |
293 		RKCANFD_REG_INT_OVERLOAD_INT |
294 		RKCANFD_REG_INT_TX_FINISH_INT;
295 
296 	/* Do not mask the bus error interrupt if the bus error
297 	 * reporting is requested.
298 	 */
299 	if (!(priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING))
300 		priv->reg_int_mask_default |= RKCANFD_REG_INT_ERROR_INT;
301 
302 	memset(&priv->bec, 0x0, sizeof(priv->bec));
303 
304 	rkcanfd_chip_fifo_setup(priv);
305 	rkcanfd_timestamp_init(priv);
306 	rkcanfd_timestamp_start(priv);
307 
308 	rkcanfd_set_bittiming(priv);
309 
310 	rkcanfd_chip_interrupts_disable(priv);
311 	rkcanfd_chip_set_work_mode(priv);
312 
313 	priv->can.state = CAN_STATE_ERROR_ACTIVE;
314 
315 	netdev_dbg(priv->ndev, "%s: reg_mode=0x%08x\n", __func__,
316 		   rkcanfd_read(priv, RKCANFD_REG_MODE));
317 }
318 
319 static void __rkcanfd_chip_stop(struct rkcanfd_priv *priv, const enum can_state state)
320 {
321 	priv->can.state = state;
322 
323 	rkcanfd_chip_set_reset_mode(priv);
324 	rkcanfd_chip_interrupts_disable(priv);
325 }
326 
327 static void rkcanfd_chip_stop(struct rkcanfd_priv *priv, const enum can_state state)
328 {
329 	priv->can.state = state;
330 
331 	rkcanfd_timestamp_stop(priv);
332 	__rkcanfd_chip_stop(priv, state);
333 }
334 
335 static void rkcanfd_chip_stop_sync(struct rkcanfd_priv *priv, const enum can_state state)
336 {
337 	priv->can.state = state;
338 
339 	rkcanfd_timestamp_stop_sync(priv);
340 	__rkcanfd_chip_stop(priv, state);
341 }
342 
343 static int rkcanfd_set_mode(struct net_device *ndev,
344 			    enum can_mode mode)
345 {
346 	struct rkcanfd_priv *priv = netdev_priv(ndev);
347 
348 	switch (mode) {
349 	case CAN_MODE_START:
350 		rkcanfd_chip_start(priv);
351 		rkcanfd_chip_interrupts_enable(priv);
352 		netif_wake_queue(ndev);
353 		break;
354 
355 	default:
356 		return -EOPNOTSUPP;
357 	}
358 
359 	return 0;
360 }
361 
362 static struct sk_buff *
363 rkcanfd_alloc_can_err_skb(struct rkcanfd_priv *priv,
364 			  struct can_frame **cf, u32 *timestamp)
365 {
366 	struct sk_buff *skb;
367 
368 	*timestamp = rkcanfd_get_timestamp(priv);
369 
370 	skb = alloc_can_err_skb(priv->ndev, cf);
371 	if (skb)
372 		rkcanfd_skb_set_timestamp(priv, skb, *timestamp);
373 
374 	return skb;
375 }
376 
377 static const char *rkcanfd_get_error_type_str(unsigned int type)
378 {
379 	switch (type) {
380 	case RKCANFD_REG_ERROR_CODE_TYPE_BIT:
381 		return "Bit";
382 	case RKCANFD_REG_ERROR_CODE_TYPE_STUFF:
383 		return "Stuff";
384 	case RKCANFD_REG_ERROR_CODE_TYPE_FORM:
385 		return "Form";
386 	case RKCANFD_REG_ERROR_CODE_TYPE_ACK:
387 		return "ACK";
388 	case RKCANFD_REG_ERROR_CODE_TYPE_CRC:
389 		return "CRC";
390 	}
391 
392 	return "<unknown>";
393 }
394 
395 #define RKCAN_ERROR_CODE(reg_ec, code) \
396 	((reg_ec) & RKCANFD_REG_ERROR_CODE_##code ? __stringify(code) " " : "")
397 
398 static void
399 rkcanfd_handle_error_int_reg_ec(struct rkcanfd_priv *priv, struct can_frame *cf,
400 				const u32 reg_ec)
401 {
402 	struct net_device_stats *stats = &priv->ndev->stats;
403 	unsigned int type;
404 	u32 reg_state, reg_cmd;
405 
406 	type = FIELD_GET(RKCANFD_REG_ERROR_CODE_TYPE, reg_ec);
407 	reg_cmd = rkcanfd_read(priv, RKCANFD_REG_CMD);
408 	reg_state = rkcanfd_read(priv, RKCANFD_REG_STATE);
409 
410 	netdev_dbg(priv->ndev, "%s Error in %s %s Phase: %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s(0x%08x) CMD=%u RX=%u TX=%u Error-Warning=%u Bus-Off=%u\n",
411 		   rkcanfd_get_error_type_str(type),
412 		   reg_ec & RKCANFD_REG_ERROR_CODE_DIRECTION_RX ? "RX" : "TX",
413 		   reg_ec & RKCANFD_REG_ERROR_CODE_PHASE ? "Data" : "Arbitration",
414 		   RKCAN_ERROR_CODE(reg_ec, TX_OVERLOAD),
415 		   RKCAN_ERROR_CODE(reg_ec, TX_ERROR),
416 		   RKCAN_ERROR_CODE(reg_ec, TX_ACK),
417 		   RKCAN_ERROR_CODE(reg_ec, TX_ACK_EOF),
418 		   RKCAN_ERROR_CODE(reg_ec, TX_CRC),
419 		   RKCAN_ERROR_CODE(reg_ec, TX_STUFF_COUNT),
420 		   RKCAN_ERROR_CODE(reg_ec, TX_DATA),
421 		   RKCAN_ERROR_CODE(reg_ec, TX_SOF_DLC),
422 		   RKCAN_ERROR_CODE(reg_ec, TX_IDLE),
423 		   RKCAN_ERROR_CODE(reg_ec, RX_BUF_INT),
424 		   RKCAN_ERROR_CODE(reg_ec, RX_SPACE),
425 		   RKCAN_ERROR_CODE(reg_ec, RX_EOF),
426 		   RKCAN_ERROR_CODE(reg_ec, RX_ACK_LIM),
427 		   RKCAN_ERROR_CODE(reg_ec, RX_ACK),
428 		   RKCAN_ERROR_CODE(reg_ec, RX_CRC_LIM),
429 		   RKCAN_ERROR_CODE(reg_ec, RX_CRC),
430 		   RKCAN_ERROR_CODE(reg_ec, RX_STUFF_COUNT),
431 		   RKCAN_ERROR_CODE(reg_ec, RX_DATA),
432 		   RKCAN_ERROR_CODE(reg_ec, RX_DLC),
433 		   RKCAN_ERROR_CODE(reg_ec, RX_BRS_ESI),
434 		   RKCAN_ERROR_CODE(reg_ec, RX_RES),
435 		   RKCAN_ERROR_CODE(reg_ec, RX_FDF),
436 		   RKCAN_ERROR_CODE(reg_ec, RX_ID2_RTR),
437 		   RKCAN_ERROR_CODE(reg_ec, RX_SOF_IDE),
438 		   RKCAN_ERROR_CODE(reg_ec, RX_IDLE),
439 		   reg_ec, reg_cmd,
440 		   !!(reg_state & RKCANFD_REG_STATE_RX_PERIOD),
441 		   !!(reg_state & RKCANFD_REG_STATE_TX_PERIOD),
442 		   !!(reg_state & RKCANFD_REG_STATE_ERROR_WARNING_STATE),
443 		   !!(reg_state & RKCANFD_REG_STATE_BUS_OFF_STATE));
444 
445 	priv->can.can_stats.bus_error++;
446 
447 	if (reg_ec & RKCANFD_REG_ERROR_CODE_DIRECTION_RX)
448 		stats->rx_errors++;
449 	else
450 		stats->tx_errors++;
451 
452 	if (!cf)
453 		return;
454 
455 	if (reg_ec & RKCANFD_REG_ERROR_CODE_DIRECTION_RX) {
456 		if (reg_ec & RKCANFD_REG_ERROR_CODE_RX_SOF_IDE)
457 			cf->data[3] = CAN_ERR_PROT_LOC_SOF;
458 		else if (reg_ec & RKCANFD_REG_ERROR_CODE_RX_ID2_RTR)
459 			cf->data[3] = CAN_ERR_PROT_LOC_RTR;
460 		/* RKCANFD_REG_ERROR_CODE_RX_FDF */
461 		else if (reg_ec & RKCANFD_REG_ERROR_CODE_RX_RES)
462 			cf->data[3] = CAN_ERR_PROT_LOC_RES0;
463 		/* RKCANFD_REG_ERROR_CODE_RX_BRS_ESI */
464 		else if (reg_ec & RKCANFD_REG_ERROR_CODE_RX_DLC)
465 			cf->data[3] = CAN_ERR_PROT_LOC_DLC;
466 		else if (reg_ec & RKCANFD_REG_ERROR_CODE_RX_DATA)
467 			cf->data[3] = CAN_ERR_PROT_LOC_DATA;
468 		/* RKCANFD_REG_ERROR_CODE_RX_STUFF_COUNT */
469 		else if (reg_ec & RKCANFD_REG_ERROR_CODE_RX_CRC)
470 			cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ;
471 		else if (reg_ec & RKCANFD_REG_ERROR_CODE_RX_CRC_LIM)
472 			cf->data[3] = CAN_ERR_PROT_LOC_ACK_DEL;
473 		else if (reg_ec & RKCANFD_REG_ERROR_CODE_RX_ACK)
474 			cf->data[3] = CAN_ERR_PROT_LOC_ACK;
475 		else if (reg_ec & RKCANFD_REG_ERROR_CODE_RX_ACK_LIM)
476 			cf->data[3] = CAN_ERR_PROT_LOC_ACK_DEL;
477 		else if (reg_ec & RKCANFD_REG_ERROR_CODE_RX_EOF)
478 			cf->data[3] = CAN_ERR_PROT_LOC_EOF;
479 		else if (reg_ec & RKCANFD_REG_ERROR_CODE_RX_SPACE)
480 			cf->data[3] = CAN_ERR_PROT_LOC_EOF;
481 		else if (reg_ec & RKCANFD_REG_ERROR_CODE_RX_BUF_INT)
482 			cf->data[3] = CAN_ERR_PROT_LOC_INTERM;
483 	} else {
484 		cf->data[2] |= CAN_ERR_PROT_TX;
485 
486 		if (reg_ec & RKCANFD_REG_ERROR_CODE_TX_SOF_DLC)
487 			cf->data[3] = CAN_ERR_PROT_LOC_SOF;
488 		else if (reg_ec & RKCANFD_REG_ERROR_CODE_TX_DATA)
489 			cf->data[3] = CAN_ERR_PROT_LOC_DATA;
490 		/* RKCANFD_REG_ERROR_CODE_TX_STUFF_COUNT */
491 		else if (reg_ec & RKCANFD_REG_ERROR_CODE_TX_CRC)
492 			cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ;
493 		else if (reg_ec & RKCANFD_REG_ERROR_CODE_TX_ACK_EOF)
494 			cf->data[3] = CAN_ERR_PROT_LOC_ACK_DEL;
495 		else if (reg_ec & RKCANFD_REG_ERROR_CODE_TX_ACK)
496 			cf->data[3] = CAN_ERR_PROT_LOC_ACK;
497 		/* RKCANFD_REG_ERROR_CODE_TX_ERROR */
498 		else if (reg_ec & RKCANFD_REG_ERROR_CODE_TX_OVERLOAD)
499 			cf->data[2] |= CAN_ERR_PROT_OVERLOAD;
500 	}
501 
502 	switch (reg_ec & RKCANFD_REG_ERROR_CODE_TYPE) {
503 	case FIELD_PREP_CONST(RKCANFD_REG_ERROR_CODE_TYPE,
504 			      RKCANFD_REG_ERROR_CODE_TYPE_BIT):
505 
506 		cf->data[2] |= CAN_ERR_PROT_BIT;
507 		break;
508 	case FIELD_PREP_CONST(RKCANFD_REG_ERROR_CODE_TYPE,
509 			      RKCANFD_REG_ERROR_CODE_TYPE_STUFF):
510 		cf->data[2] |= CAN_ERR_PROT_STUFF;
511 		break;
512 	case FIELD_PREP_CONST(RKCANFD_REG_ERROR_CODE_TYPE,
513 			      RKCANFD_REG_ERROR_CODE_TYPE_FORM):
514 		cf->data[2] |= CAN_ERR_PROT_FORM;
515 		break;
516 	case FIELD_PREP_CONST(RKCANFD_REG_ERROR_CODE_TYPE,
517 			      RKCANFD_REG_ERROR_CODE_TYPE_ACK):
518 		cf->can_id |= CAN_ERR_ACK;
519 		break;
520 	case FIELD_PREP_CONST(RKCANFD_REG_ERROR_CODE_TYPE,
521 			      RKCANFD_REG_ERROR_CODE_TYPE_CRC):
522 		cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ;
523 		break;
524 	}
525 }
526 
527 static int rkcanfd_handle_error_int(struct rkcanfd_priv *priv)
528 {
529 	struct net_device_stats *stats = &priv->ndev->stats;
530 	struct can_frame *cf = NULL;
531 	u32 reg_ec, timestamp;
532 	struct sk_buff *skb;
533 	int err;
534 
535 	reg_ec = rkcanfd_read(priv, RKCANFD_REG_ERROR_CODE);
536 
537 	if (!reg_ec)
538 		return 0;
539 
540 	if (priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING) {
541 		skb = rkcanfd_alloc_can_err_skb(priv, &cf, &timestamp);
542 		if (cf) {
543 			struct can_berr_counter bec;
544 
545 			rkcanfd_get_berr_counter_corrected(priv, &bec);
546 			cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR | CAN_ERR_CNT;
547 			cf->data[6] = bec.txerr;
548 			cf->data[7] = bec.rxerr;
549 		}
550 	}
551 
552 	rkcanfd_handle_error_int_reg_ec(priv, cf, reg_ec);
553 
554 	if (!cf)
555 		return 0;
556 
557 	err = can_rx_offload_queue_timestamp(&priv->offload, skb, timestamp);
558 	if (err)
559 		stats->rx_fifo_errors++;
560 
561 	return 0;
562 }
563 
564 static int rkcanfd_handle_state_error_int(struct rkcanfd_priv *priv)
565 {
566 	struct net_device_stats *stats = &priv->ndev->stats;
567 	enum can_state new_state, rx_state, tx_state;
568 	struct net_device *ndev = priv->ndev;
569 	struct can_berr_counter bec;
570 	struct can_frame *cf = NULL;
571 	struct sk_buff *skb;
572 	u32 timestamp;
573 	int err;
574 
575 	rkcanfd_get_berr_counter_corrected(priv, &bec);
576 	can_state_get_by_berr_counter(ndev, &bec, &tx_state, &rx_state);
577 
578 	new_state = max(tx_state, rx_state);
579 	if (new_state == priv->can.state)
580 		return 0;
581 
582 	/* The skb allocation might fail, but can_change_state()
583 	 * handles cf == NULL.
584 	 */
585 	skb = rkcanfd_alloc_can_err_skb(priv, &cf, &timestamp);
586 	can_change_state(ndev, cf, tx_state, rx_state);
587 
588 	if (new_state == CAN_STATE_BUS_OFF) {
589 		rkcanfd_chip_stop(priv, CAN_STATE_BUS_OFF);
590 		can_bus_off(ndev);
591 	}
592 
593 	if (!skb)
594 		return 0;
595 
596 	if (new_state != CAN_STATE_BUS_OFF) {
597 		cf->can_id |= CAN_ERR_CNT;
598 		cf->data[6] = bec.txerr;
599 		cf->data[7] = bec.rxerr;
600 	}
601 
602 	err = can_rx_offload_queue_timestamp(&priv->offload, skb, timestamp);
603 	if (err)
604 		stats->rx_fifo_errors++;
605 
606 	return 0;
607 }
608 
609 static int
610 rkcanfd_handle_rx_fifo_overflow_int(struct rkcanfd_priv *priv)
611 {
612 	struct net_device_stats *stats = &priv->ndev->stats;
613 	struct can_berr_counter bec;
614 	struct can_frame *cf = NULL;
615 	struct sk_buff *skb;
616 	u32 timestamp;
617 	int err;
618 
619 	stats->rx_over_errors++;
620 	stats->rx_errors++;
621 
622 	netdev_dbg(priv->ndev, "RX-FIFO overflow\n");
623 
624 	skb = rkcanfd_alloc_can_err_skb(priv, &cf, &timestamp);
625 	if (skb)
626 		return 0;
627 
628 	rkcanfd_get_berr_counter_corrected(priv, &bec);
629 
630 	cf->can_id |= CAN_ERR_CRTL | CAN_ERR_CNT;
631 	cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
632 	cf->data[6] = bec.txerr;
633 	cf->data[7] = bec.rxerr;
634 
635 	err = can_rx_offload_queue_timestamp(&priv->offload, skb, timestamp);
636 	if (err)
637 		stats->rx_fifo_errors++;
638 
639 	return 0;
640 }
641 
642 #define rkcanfd_handle(priv, irq, ...) \
643 ({ \
644 	struct rkcanfd_priv *_priv = (priv); \
645 	int err; \
646 \
647 	err = rkcanfd_handle_##irq(_priv, ## __VA_ARGS__); \
648 	if (err) \
649 		netdev_err(_priv->ndev, \
650 			"IRQ handler rkcanfd_handle_%s() returned error: %pe\n", \
651 			   __stringify(irq), ERR_PTR(err)); \
652 	err; \
653 })
654 
655 static irqreturn_t rkcanfd_irq(int irq, void *dev_id)
656 {
657 	struct rkcanfd_priv *priv = dev_id;
658 	u32 reg_int_unmasked, reg_int;
659 
660 	reg_int_unmasked = rkcanfd_read(priv, RKCANFD_REG_INT);
661 	reg_int = reg_int_unmasked & ~priv->reg_int_mask_default;
662 
663 	if (!reg_int)
664 		return IRQ_NONE;
665 
666 	/* First ACK then handle, to avoid lost-IRQ race condition on
667 	 * fast re-occurring interrupts.
668 	 */
669 	rkcanfd_write(priv, RKCANFD_REG_INT, reg_int);
670 
671 	if (reg_int & RKCANFD_REG_INT_RX_FINISH_INT)
672 		rkcanfd_handle(priv, rx_int);
673 
674 	if (reg_int & RKCANFD_REG_INT_ERROR_INT)
675 		rkcanfd_handle(priv, error_int);
676 
677 	if (reg_int & (RKCANFD_REG_INT_BUS_OFF_INT |
678 		       RKCANFD_REG_INT_PASSIVE_ERROR_INT |
679 		       RKCANFD_REG_INT_ERROR_WARNING_INT) ||
680 	    priv->can.state > CAN_STATE_ERROR_ACTIVE)
681 		rkcanfd_handle(priv, state_error_int);
682 
683 	if (reg_int & RKCANFD_REG_INT_RX_FIFO_OVERFLOW_INT)
684 		rkcanfd_handle(priv, rx_fifo_overflow_int);
685 
686 	if (reg_int & ~(RKCANFD_REG_INT_ALL_ERROR |
687 			RKCANFD_REG_INT_RX_FIFO_OVERFLOW_INT |
688 			RKCANFD_REG_INT_RX_FINISH_INT))
689 		netdev_err(priv->ndev, "%s: int=0x%08x\n", __func__, reg_int);
690 
691 	if (reg_int & RKCANFD_REG_INT_WAKEUP_INT)
692 		netdev_info(priv->ndev, "%s: WAKEUP_INT\n", __func__);
693 
694 	if (reg_int & RKCANFD_REG_INT_TXE_FIFO_FULL_INT)
695 		netdev_info(priv->ndev, "%s: TXE_FIFO_FULL_INT\n", __func__);
696 
697 	if (reg_int & RKCANFD_REG_INT_TXE_FIFO_OV_INT)
698 		netdev_info(priv->ndev, "%s: TXE_FIFO_OV_INT\n", __func__);
699 
700 	if (reg_int & RKCANFD_REG_INT_BUS_OFF_RECOVERY_INT)
701 		netdev_info(priv->ndev, "%s: BUS_OFF_RECOVERY_INT\n", __func__);
702 
703 	if (reg_int & RKCANFD_REG_INT_RX_FIFO_FULL_INT)
704 		netdev_info(priv->ndev, "%s: RX_FIFO_FULL_INT\n", __func__);
705 
706 	if (reg_int & RKCANFD_REG_INT_OVERLOAD_INT)
707 		netdev_info(priv->ndev, "%s: OVERLOAD_INT\n", __func__);
708 
709 	can_rx_offload_irq_finish(&priv->offload);
710 
711 	return IRQ_HANDLED;
712 }
713 
714 static int rkcanfd_open(struct net_device *ndev)
715 {
716 	struct rkcanfd_priv *priv = netdev_priv(ndev);
717 	int err;
718 
719 	err = open_candev(ndev);
720 	if (err)
721 		return err;
722 
723 	err = pm_runtime_resume_and_get(ndev->dev.parent);
724 	if (err)
725 		goto out_close_candev;
726 
727 	rkcanfd_chip_start(priv);
728 	can_rx_offload_enable(&priv->offload);
729 
730 	err = request_irq(ndev->irq, rkcanfd_irq, IRQF_SHARED, ndev->name, priv);
731 	if (err)
732 		goto out_rkcanfd_chip_stop;
733 
734 	rkcanfd_chip_interrupts_enable(priv);
735 
736 	netif_start_queue(ndev);
737 
738 	return 0;
739 
740 out_rkcanfd_chip_stop:
741 	rkcanfd_chip_stop_sync(priv, CAN_STATE_STOPPED);
742 	pm_runtime_put(ndev->dev.parent);
743 out_close_candev:
744 	close_candev(ndev);
745 	return err;
746 }
747 
748 static int rkcanfd_stop(struct net_device *ndev)
749 {
750 	struct rkcanfd_priv *priv = netdev_priv(ndev);
751 
752 	netif_stop_queue(ndev);
753 
754 	rkcanfd_chip_interrupts_disable(priv);
755 	free_irq(ndev->irq, priv);
756 	can_rx_offload_disable(&priv->offload);
757 	rkcanfd_chip_stop_sync(priv, CAN_STATE_STOPPED);
758 	close_candev(ndev);
759 
760 	pm_runtime_put(ndev->dev.parent);
761 
762 	return 0;
763 }
764 
765 static const struct net_device_ops rkcanfd_netdev_ops = {
766 	.ndo_open = rkcanfd_open,
767 	.ndo_stop = rkcanfd_stop,
768 	.ndo_start_xmit = rkcanfd_start_xmit,
769 	.ndo_change_mtu = can_change_mtu,
770 };
771 
772 static int __maybe_unused rkcanfd_runtime_suspend(struct device *dev)
773 {
774 	struct rkcanfd_priv *priv = dev_get_drvdata(dev);
775 
776 	clk_bulk_disable_unprepare(priv->clks_num, priv->clks);
777 
778 	return 0;
779 }
780 
781 static int __maybe_unused rkcanfd_runtime_resume(struct device *dev)
782 {
783 	struct rkcanfd_priv *priv = dev_get_drvdata(dev);
784 
785 	return clk_bulk_prepare_enable(priv->clks_num, priv->clks);
786 }
787 
788 static void rkcanfd_register_done(const struct rkcanfd_priv *priv)
789 {
790 	u32 dev_id;
791 
792 	dev_id = rkcanfd_read(priv, RKCANFD_REG_RTL_VERSION);
793 
794 	netdev_info(priv->ndev,
795 		    "Rockchip-CANFD %s rev%lu.%lu (errata 0x%04x) found\n",
796 		    rkcanfd_get_model_str(priv),
797 		    FIELD_GET(RKCANFD_REG_RTL_VERSION_MAJOR, dev_id),
798 		    FIELD_GET(RKCANFD_REG_RTL_VERSION_MINOR, dev_id),
799 		    priv->devtype_data.quirks);
800 
801 	if (priv->devtype_data.quirks & RKCANFD_QUIRK_RK3568_ERRATUM_5 &&
802 	    priv->can.clock.freq < RKCANFD_ERRATUM_5_SYSCLOCK_HZ_MIN)
803 		netdev_info(priv->ndev,
804 			    "Erratum 5: CAN clock frequency (%luMHz) lower than known good (%luMHz), expect degraded performance\n",
805 			    priv->can.clock.freq / MEGA,
806 			    RKCANFD_ERRATUM_5_SYSCLOCK_HZ_MIN / MEGA);
807 }
808 
809 static int rkcanfd_register(struct rkcanfd_priv *priv)
810 {
811 	struct net_device *ndev = priv->ndev;
812 	int err;
813 
814 	pm_runtime_enable(ndev->dev.parent);
815 
816 	err = pm_runtime_resume_and_get(ndev->dev.parent);
817 	if (err)
818 		goto out_pm_runtime_disable;
819 
820 	rkcanfd_ethtool_init(priv);
821 
822 	err = register_candev(ndev);
823 	if (err)
824 		goto out_pm_runtime_put_sync;
825 
826 	rkcanfd_register_done(priv);
827 
828 	pm_runtime_put(ndev->dev.parent);
829 
830 	return 0;
831 
832 out_pm_runtime_put_sync:
833 	pm_runtime_put_sync(ndev->dev.parent);
834 out_pm_runtime_disable:
835 	pm_runtime_disable(ndev->dev.parent);
836 
837 	return err;
838 }
839 
840 static inline void rkcanfd_unregister(struct rkcanfd_priv *priv)
841 {
842 	struct net_device *ndev	= priv->ndev;
843 
844 	unregister_candev(ndev);
845 	pm_runtime_disable(ndev->dev.parent);
846 }
847 
848 static const struct of_device_id rkcanfd_of_match[] = {
849 	{
850 		.compatible = "rockchip,rk3568v2-canfd",
851 		.data = &rkcanfd_devtype_data_rk3568v2,
852 	}, {
853 		.compatible = "rockchip,rk3568v3-canfd",
854 		.data = &rkcanfd_devtype_data_rk3568v3,
855 	}, {
856 		/* sentinel */
857 	},
858 };
859 MODULE_DEVICE_TABLE(of, rkcanfd_of_match);
860 
861 static int rkcanfd_probe(struct platform_device *pdev)
862 {
863 	struct rkcanfd_priv *priv;
864 	struct net_device *ndev;
865 	const void *match;
866 	int err;
867 
868 	ndev = alloc_candev(sizeof(struct rkcanfd_priv), RKCANFD_TXFIFO_DEPTH);
869 	if (!ndev)
870 		return -ENOMEM;
871 
872 	priv = netdev_priv(ndev);
873 
874 	ndev->irq = platform_get_irq(pdev, 0);
875 	if (ndev->irq < 0) {
876 		err = ndev->irq;
877 		goto out_free_candev;
878 	}
879 
880 	priv->clks_num = devm_clk_bulk_get_all(&pdev->dev, &priv->clks);
881 	if (priv->clks_num < 0) {
882 		err = priv->clks_num;
883 		goto out_free_candev;
884 	}
885 
886 	priv->regs = devm_platform_ioremap_resource(pdev, 0);
887 	if (IS_ERR(priv->regs)) {
888 		err = PTR_ERR(priv->regs);
889 		goto out_free_candev;
890 	}
891 
892 	priv->reset = devm_reset_control_array_get_exclusive(&pdev->dev);
893 	if (IS_ERR(priv->reset)) {
894 		err = dev_err_probe(&pdev->dev, PTR_ERR(priv->reset),
895 				    "Failed to get reset line\n");
896 		goto out_free_candev;
897 	}
898 
899 	SET_NETDEV_DEV(ndev, &pdev->dev);
900 
901 	ndev->netdev_ops = &rkcanfd_netdev_ops;
902 	ndev->flags |= IFF_ECHO;
903 
904 	platform_set_drvdata(pdev, priv);
905 	priv->can.clock.freq = clk_get_rate(priv->clks[0].clk);
906 	priv->can.bittiming_const = &rkcanfd_bittiming_const;
907 	priv->can.data_bittiming_const = &rkcanfd_data_bittiming_const;
908 	priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK |
909 		CAN_CTRLMODE_BERR_REPORTING;
910 	if (!(priv->devtype_data.quirks & RKCANFD_QUIRK_CANFD_BROKEN))
911 		priv->can.ctrlmode_supported |= CAN_CTRLMODE_FD;
912 	priv->can.do_set_mode = rkcanfd_set_mode;
913 	priv->can.do_get_berr_counter = rkcanfd_get_berr_counter;
914 	priv->ndev = ndev;
915 
916 	match = device_get_match_data(&pdev->dev);
917 	if (match)
918 		priv->devtype_data = *(struct rkcanfd_devtype_data *)match;
919 
920 	err = can_rx_offload_add_manual(ndev, &priv->offload,
921 					RKCANFD_NAPI_WEIGHT);
922 	if (err)
923 		goto out_free_candev;
924 
925 	err = rkcanfd_register(priv);
926 	if (err)
927 		goto out_can_rx_offload_del;
928 
929 	return 0;
930 
931 out_can_rx_offload_del:
932 	can_rx_offload_del(&priv->offload);
933 out_free_candev:
934 	free_candev(ndev);
935 
936 	return err;
937 }
938 
939 static void rkcanfd_remove(struct platform_device *pdev)
940 {
941 	struct rkcanfd_priv *priv = platform_get_drvdata(pdev);
942 	struct net_device *ndev = priv->ndev;
943 
944 	can_rx_offload_del(&priv->offload);
945 	rkcanfd_unregister(priv);
946 	free_candev(ndev);
947 }
948 
949 static const struct dev_pm_ops rkcanfd_pm_ops = {
950 	SET_RUNTIME_PM_OPS(rkcanfd_runtime_suspend,
951 			   rkcanfd_runtime_resume, NULL)
952 };
953 
954 static struct platform_driver rkcanfd_driver = {
955 	.driver = {
956 		.name = DEVICE_NAME,
957 		.pm = &rkcanfd_pm_ops,
958 		.of_match_table = rkcanfd_of_match,
959 	},
960 	.probe = rkcanfd_probe,
961 	.remove = rkcanfd_remove,
962 };
963 module_platform_driver(rkcanfd_driver);
964 
965 MODULE_AUTHOR("Marc Kleine-Budde <mkl@pengutronix.de>");
966 MODULE_DESCRIPTION("Rockchip CAN-FD Driver");
967 MODULE_LICENSE("GPL");
968