xref: /linux/drivers/net/can/rcar/rcar_canfd.c (revision b8321ed4a40c02054f930ca59d3570caa27bc86c)
1 // SPDX-License-Identifier: GPL-2.0+
2 /* Renesas R-Car CAN FD device driver
3  *
4  * Copyright (C) 2015 Renesas Electronics Corp.
5  */
6 
7 /* The R-Car CAN FD controller can operate in either one of the below two modes
8  *  - CAN FD only mode
9  *  - Classical CAN (CAN 2.0) only mode
10  *
11  * This driver puts the controller in CAN FD only mode by default. In this
12  * mode, the controller acts as a CAN FD node that can also interoperate with
13  * CAN 2.0 nodes.
14  *
15  * To switch the controller to Classical CAN (CAN 2.0) only mode, add
16  * "renesas,no-can-fd" optional property to the device tree node. A h/w reset is
17  * also required to switch modes.
18  *
19  * Note: The h/w manual register naming convention is clumsy and not acceptable
20  * to use as it is in the driver. However, those names are added as comments
21  * wherever it is modified to a readable name.
22  */
23 
24 #include <linux/module.h>
25 #include <linux/moduleparam.h>
26 #include <linux/kernel.h>
27 #include <linux/types.h>
28 #include <linux/interrupt.h>
29 #include <linux/errno.h>
30 #include <linux/netdevice.h>
31 #include <linux/platform_device.h>
32 #include <linux/can/led.h>
33 #include <linux/can/dev.h>
34 #include <linux/clk.h>
35 #include <linux/of.h>
36 #include <linux/of_device.h>
37 #include <linux/bitmap.h>
38 #include <linux/bitops.h>
39 #include <linux/iopoll.h>
40 #include <linux/reset.h>
41 
42 #define RCANFD_DRV_NAME			"rcar_canfd"
43 
44 enum rcanfd_chip_id {
45 	RENESAS_RCAR_GEN3 = 0,
46 	RENESAS_RZG2L,
47 	RENESAS_R8A779A0,
48 };
49 
50 /* Global register bits */
51 
52 /* RSCFDnCFDGRMCFG */
53 #define RCANFD_GRMCFG_RCMC		BIT(0)
54 
55 /* RSCFDnCFDGCFG / RSCFDnGCFG */
56 #define RCANFD_GCFG_EEFE		BIT(6)
57 #define RCANFD_GCFG_CMPOC		BIT(5)	/* CAN FD only */
58 #define RCANFD_GCFG_DCS			BIT(4)
59 #define RCANFD_GCFG_DCE			BIT(1)
60 #define RCANFD_GCFG_TPRI		BIT(0)
61 
62 /* RSCFDnCFDGCTR / RSCFDnGCTR */
63 #define RCANFD_GCTR_TSRST		BIT(16)
64 #define RCANFD_GCTR_CFMPOFIE		BIT(11)	/* CAN FD only */
65 #define RCANFD_GCTR_THLEIE		BIT(10)
66 #define RCANFD_GCTR_MEIE		BIT(9)
67 #define RCANFD_GCTR_DEIE		BIT(8)
68 #define RCANFD_GCTR_GSLPR		BIT(2)
69 #define RCANFD_GCTR_GMDC_MASK		(0x3)
70 #define RCANFD_GCTR_GMDC_GOPM		(0x0)
71 #define RCANFD_GCTR_GMDC_GRESET		(0x1)
72 #define RCANFD_GCTR_GMDC_GTEST		(0x2)
73 
74 /* RSCFDnCFDGSTS / RSCFDnGSTS */
75 #define RCANFD_GSTS_GRAMINIT		BIT(3)
76 #define RCANFD_GSTS_GSLPSTS		BIT(2)
77 #define RCANFD_GSTS_GHLTSTS		BIT(1)
78 #define RCANFD_GSTS_GRSTSTS		BIT(0)
79 /* Non-operational status */
80 #define RCANFD_GSTS_GNOPM		(BIT(0) | BIT(1) | BIT(2) | BIT(3))
81 
82 /* RSCFDnCFDGERFL / RSCFDnGERFL */
83 #define RCANFD_GERFL_EEF0_7		GENMASK(23, 16)
84 #define RCANFD_GERFL_EEF1		BIT(17)
85 #define RCANFD_GERFL_EEF0		BIT(16)
86 #define RCANFD_GERFL_CMPOF		BIT(3)	/* CAN FD only */
87 #define RCANFD_GERFL_THLES		BIT(2)
88 #define RCANFD_GERFL_MES		BIT(1)
89 #define RCANFD_GERFL_DEF		BIT(0)
90 
91 #define RCANFD_GERFL_ERR(gpriv, x) \
92 	((x) & (reg_v3u(gpriv, RCANFD_GERFL_EEF0_7, \
93 			RCANFD_GERFL_EEF0 | RCANFD_GERFL_EEF1) | \
94 		RCANFD_GERFL_MES | \
95 		((gpriv)->fdmode ? RCANFD_GERFL_CMPOF : 0)))
96 
97 /* AFL Rx rules registers */
98 
99 /* RSCFDnCFDGAFLCFG0 / RSCFDnGAFLCFG0 */
100 #define RCANFD_GAFLCFG_SETRNC(gpriv, n, x) \
101 	(((x) & reg_v3u(gpriv, 0x1ff, 0xff)) << \
102 	 (reg_v3u(gpriv, 16, 24) - (n) * reg_v3u(gpriv, 16, 8)))
103 
104 #define RCANFD_GAFLCFG_GETRNC(gpriv, n, x) \
105 	(((x) >> (reg_v3u(gpriv, 16, 24) - (n) * reg_v3u(gpriv, 16, 8))) & \
106 	 reg_v3u(gpriv, 0x1ff, 0xff))
107 
108 /* RSCFDnCFDGAFLECTR / RSCFDnGAFLECTR */
109 #define RCANFD_GAFLECTR_AFLDAE		BIT(8)
110 #define RCANFD_GAFLECTR_AFLPN(gpriv, x)	((x) & reg_v3u(gpriv, 0x7f, 0x1f))
111 
112 /* RSCFDnCFDGAFLIDj / RSCFDnGAFLIDj */
113 #define RCANFD_GAFLID_GAFLLB		BIT(29)
114 
115 /* RSCFDnCFDGAFLP1_j / RSCFDnGAFLP1_j */
116 #define RCANFD_GAFLP1_GAFLFDP(x)	(1 << (x))
117 
118 /* Channel register bits */
119 
120 /* RSCFDnCmCFG - Classical CAN only */
121 #define RCANFD_CFG_SJW(x)		(((x) & 0x3) << 24)
122 #define RCANFD_CFG_TSEG2(x)		(((x) & 0x7) << 20)
123 #define RCANFD_CFG_TSEG1(x)		(((x) & 0xf) << 16)
124 #define RCANFD_CFG_BRP(x)		(((x) & 0x3ff) << 0)
125 
126 /* RSCFDnCFDCmNCFG - CAN FD only */
127 #define RCANFD_NCFG_NTSEG2(gpriv, x) \
128 	(((x) & reg_v3u(gpriv, 0x7f, 0x1f)) << reg_v3u(gpriv, 25, 24))
129 
130 #define RCANFD_NCFG_NTSEG1(gpriv, x) \
131 	(((x) & reg_v3u(gpriv, 0xff, 0x7f)) << reg_v3u(gpriv, 17, 16))
132 
133 #define RCANFD_NCFG_NSJW(gpriv, x) \
134 	(((x) & reg_v3u(gpriv, 0x7f, 0x1f)) << reg_v3u(gpriv, 10, 11))
135 
136 #define RCANFD_NCFG_NBRP(x)		(((x) & 0x3ff) << 0)
137 
138 /* RSCFDnCFDCmCTR / RSCFDnCmCTR */
139 #define RCANFD_CCTR_CTME		BIT(24)
140 #define RCANFD_CCTR_ERRD		BIT(23)
141 #define RCANFD_CCTR_BOM_MASK		(0x3 << 21)
142 #define RCANFD_CCTR_BOM_ISO		(0x0 << 21)
143 #define RCANFD_CCTR_BOM_BENTRY		(0x1 << 21)
144 #define RCANFD_CCTR_BOM_BEND		(0x2 << 21)
145 #define RCANFD_CCTR_TDCVFIE		BIT(19)
146 #define RCANFD_CCTR_SOCOIE		BIT(18)
147 #define RCANFD_CCTR_EOCOIE		BIT(17)
148 #define RCANFD_CCTR_TAIE		BIT(16)
149 #define RCANFD_CCTR_ALIE		BIT(15)
150 #define RCANFD_CCTR_BLIE		BIT(14)
151 #define RCANFD_CCTR_OLIE		BIT(13)
152 #define RCANFD_CCTR_BORIE		BIT(12)
153 #define RCANFD_CCTR_BOEIE		BIT(11)
154 #define RCANFD_CCTR_EPIE		BIT(10)
155 #define RCANFD_CCTR_EWIE		BIT(9)
156 #define RCANFD_CCTR_BEIE		BIT(8)
157 #define RCANFD_CCTR_CSLPR		BIT(2)
158 #define RCANFD_CCTR_CHMDC_MASK		(0x3)
159 #define RCANFD_CCTR_CHDMC_COPM		(0x0)
160 #define RCANFD_CCTR_CHDMC_CRESET	(0x1)
161 #define RCANFD_CCTR_CHDMC_CHLT		(0x2)
162 
163 /* RSCFDnCFDCmSTS / RSCFDnCmSTS */
164 #define RCANFD_CSTS_COMSTS		BIT(7)
165 #define RCANFD_CSTS_RECSTS		BIT(6)
166 #define RCANFD_CSTS_TRMSTS		BIT(5)
167 #define RCANFD_CSTS_BOSTS		BIT(4)
168 #define RCANFD_CSTS_EPSTS		BIT(3)
169 #define RCANFD_CSTS_SLPSTS		BIT(2)
170 #define RCANFD_CSTS_HLTSTS		BIT(1)
171 #define RCANFD_CSTS_CRSTSTS		BIT(0)
172 
173 #define RCANFD_CSTS_TECCNT(x)		(((x) >> 24) & 0xff)
174 #define RCANFD_CSTS_RECCNT(x)		(((x) >> 16) & 0xff)
175 
176 /* RSCFDnCFDCmERFL / RSCFDnCmERFL */
177 #define RCANFD_CERFL_ADERR		BIT(14)
178 #define RCANFD_CERFL_B0ERR		BIT(13)
179 #define RCANFD_CERFL_B1ERR		BIT(12)
180 #define RCANFD_CERFL_CERR		BIT(11)
181 #define RCANFD_CERFL_AERR		BIT(10)
182 #define RCANFD_CERFL_FERR		BIT(9)
183 #define RCANFD_CERFL_SERR		BIT(8)
184 #define RCANFD_CERFL_ALF		BIT(7)
185 #define RCANFD_CERFL_BLF		BIT(6)
186 #define RCANFD_CERFL_OVLF		BIT(5)
187 #define RCANFD_CERFL_BORF		BIT(4)
188 #define RCANFD_CERFL_BOEF		BIT(3)
189 #define RCANFD_CERFL_EPF		BIT(2)
190 #define RCANFD_CERFL_EWF		BIT(1)
191 #define RCANFD_CERFL_BEF		BIT(0)
192 
193 #define RCANFD_CERFL_ERR(x)		((x) & (0x7fff)) /* above bits 14:0 */
194 
195 /* RSCFDnCFDCmDCFG */
196 #define RCANFD_DCFG_DSJW(x)		(((x) & 0x7) << 24)
197 
198 #define RCANFD_DCFG_DTSEG2(gpriv, x) \
199 	(((x) & reg_v3u(gpriv, 0x0f, 0x7)) << reg_v3u(gpriv, 16, 20))
200 
201 #define RCANFD_DCFG_DTSEG1(gpriv, x) \
202 	(((x) & reg_v3u(gpriv, 0x1f, 0xf)) << reg_v3u(gpriv, 8, 16))
203 
204 #define RCANFD_DCFG_DBRP(x)		(((x) & 0xff) << 0)
205 
206 /* RSCFDnCFDCmFDCFG */
207 #define RCANFD_FDCFG_CLOE		BIT(30)
208 #define RCANFD_FDCFG_FDOE		BIT(28)
209 #define RCANFD_FDCFG_TDCE		BIT(9)
210 #define RCANFD_FDCFG_TDCOC		BIT(8)
211 #define RCANFD_FDCFG_TDCO(x)		(((x) & 0x7f) >> 16)
212 
213 /* RSCFDnCFDRFCCx */
214 #define RCANFD_RFCC_RFIM		BIT(12)
215 #define RCANFD_RFCC_RFDC(x)		(((x) & 0x7) << 8)
216 #define RCANFD_RFCC_RFPLS(x)		(((x) & 0x7) << 4)
217 #define RCANFD_RFCC_RFIE		BIT(1)
218 #define RCANFD_RFCC_RFE			BIT(0)
219 
220 /* RSCFDnCFDRFSTSx */
221 #define RCANFD_RFSTS_RFIF		BIT(3)
222 #define RCANFD_RFSTS_RFMLT		BIT(2)
223 #define RCANFD_RFSTS_RFFLL		BIT(1)
224 #define RCANFD_RFSTS_RFEMP		BIT(0)
225 
226 /* RSCFDnCFDRFIDx */
227 #define RCANFD_RFID_RFIDE		BIT(31)
228 #define RCANFD_RFID_RFRTR		BIT(30)
229 
230 /* RSCFDnCFDRFPTRx */
231 #define RCANFD_RFPTR_RFDLC(x)		(((x) >> 28) & 0xf)
232 #define RCANFD_RFPTR_RFPTR(x)		(((x) >> 16) & 0xfff)
233 #define RCANFD_RFPTR_RFTS(x)		(((x) >> 0) & 0xffff)
234 
235 /* RSCFDnCFDRFFDSTSx */
236 #define RCANFD_RFFDSTS_RFFDF		BIT(2)
237 #define RCANFD_RFFDSTS_RFBRS		BIT(1)
238 #define RCANFD_RFFDSTS_RFESI		BIT(0)
239 
240 /* Common FIFO bits */
241 
242 /* RSCFDnCFDCFCCk */
243 #define RCANFD_CFCC_CFTML(gpriv, x)	(((x) & 0xf) << reg_v3u(gpriv, 16, 20))
244 #define RCANFD_CFCC_CFM(gpriv, x)	(((x) & 0x3) << reg_v3u(gpriv,  8, 16))
245 #define RCANFD_CFCC_CFIM		BIT(12)
246 #define RCANFD_CFCC_CFDC(gpriv, x)	(((x) & 0x7) << reg_v3u(gpriv, 21,  8))
247 #define RCANFD_CFCC_CFPLS(x)		(((x) & 0x7) << 4)
248 #define RCANFD_CFCC_CFTXIE		BIT(2)
249 #define RCANFD_CFCC_CFE			BIT(0)
250 
251 /* RSCFDnCFDCFSTSk */
252 #define RCANFD_CFSTS_CFMC(x)		(((x) >> 8) & 0xff)
253 #define RCANFD_CFSTS_CFTXIF		BIT(4)
254 #define RCANFD_CFSTS_CFMLT		BIT(2)
255 #define RCANFD_CFSTS_CFFLL		BIT(1)
256 #define RCANFD_CFSTS_CFEMP		BIT(0)
257 
258 /* RSCFDnCFDCFIDk */
259 #define RCANFD_CFID_CFIDE		BIT(31)
260 #define RCANFD_CFID_CFRTR		BIT(30)
261 #define RCANFD_CFID_CFID_MASK(x)	((x) & 0x1fffffff)
262 
263 /* RSCFDnCFDCFPTRk */
264 #define RCANFD_CFPTR_CFDLC(x)		(((x) & 0xf) << 28)
265 #define RCANFD_CFPTR_CFPTR(x)		(((x) & 0xfff) << 16)
266 #define RCANFD_CFPTR_CFTS(x)		(((x) & 0xff) << 0)
267 
268 /* RSCFDnCFDCFFDCSTSk */
269 #define RCANFD_CFFDCSTS_CFFDF		BIT(2)
270 #define RCANFD_CFFDCSTS_CFBRS		BIT(1)
271 #define RCANFD_CFFDCSTS_CFESI		BIT(0)
272 
273 /* This controller supports either Classical CAN only mode or CAN FD only mode.
274  * These modes are supported in two separate set of register maps & names.
275  * However, some of the register offsets are common for both modes. Those
276  * offsets are listed below as Common registers.
277  *
278  * The CAN FD only mode specific registers & Classical CAN only mode specific
279  * registers are listed separately. Their register names starts with
280  * RCANFD_F_xxx & RCANFD_C_xxx respectively.
281  */
282 
283 /* Common registers */
284 
285 /* RSCFDnCFDCmNCFG / RSCFDnCmCFG */
286 #define RCANFD_CCFG(m)			(0x0000 + (0x10 * (m)))
287 /* RSCFDnCFDCmCTR / RSCFDnCmCTR */
288 #define RCANFD_CCTR(m)			(0x0004 + (0x10 * (m)))
289 /* RSCFDnCFDCmSTS / RSCFDnCmSTS */
290 #define RCANFD_CSTS(m)			(0x0008 + (0x10 * (m)))
291 /* RSCFDnCFDCmERFL / RSCFDnCmERFL */
292 #define RCANFD_CERFL(m)			(0x000C + (0x10 * (m)))
293 
294 /* RSCFDnCFDGCFG / RSCFDnGCFG */
295 #define RCANFD_GCFG			(0x0084)
296 /* RSCFDnCFDGCTR / RSCFDnGCTR */
297 #define RCANFD_GCTR			(0x0088)
298 /* RSCFDnCFDGCTS / RSCFDnGCTS */
299 #define RCANFD_GSTS			(0x008c)
300 /* RSCFDnCFDGERFL / RSCFDnGERFL */
301 #define RCANFD_GERFL			(0x0090)
302 /* RSCFDnCFDGTSC / RSCFDnGTSC */
303 #define RCANFD_GTSC			(0x0094)
304 /* RSCFDnCFDGAFLECTR / RSCFDnGAFLECTR */
305 #define RCANFD_GAFLECTR			(0x0098)
306 /* RSCFDnCFDGAFLCFG / RSCFDnGAFLCFG */
307 #define RCANFD_GAFLCFG(ch)		(0x009c + (0x04 * ((ch) / 2)))
308 /* RSCFDnCFDRMNB / RSCFDnRMNB */
309 #define RCANFD_RMNB			(0x00a4)
310 /* RSCFDnCFDRMND / RSCFDnRMND */
311 #define RCANFD_RMND(y)			(0x00a8 + (0x04 * (y)))
312 
313 /* RSCFDnCFDRFCCx / RSCFDnRFCCx */
314 #define RCANFD_RFCC(gpriv, x)		(reg_v3u(gpriv, 0x00c0, 0x00b8) + (0x04 * (x)))
315 /* RSCFDnCFDRFSTSx / RSCFDnRFSTSx */
316 #define RCANFD_RFSTS(gpriv, x)		(RCANFD_RFCC(gpriv, x) + 0x20)
317 /* RSCFDnCFDRFPCTRx / RSCFDnRFPCTRx */
318 #define RCANFD_RFPCTR(gpriv, x)		(RCANFD_RFCC(gpriv, x) + 0x40)
319 
320 /* Common FIFO Control registers */
321 
322 /* RSCFDnCFDCFCCx / RSCFDnCFCCx */
323 #define RCANFD_CFCC(gpriv, ch, idx) \
324 	(reg_v3u(gpriv, 0x0120, 0x0118) + (0x0c * (ch)) + (0x04 * (idx)))
325 /* RSCFDnCFDCFSTSx / RSCFDnCFSTSx */
326 #define RCANFD_CFSTS(gpriv, ch, idx) \
327 	(reg_v3u(gpriv, 0x01e0, 0x0178) + (0x0c * (ch)) + (0x04 * (idx)))
328 /* RSCFDnCFDCFPCTRx / RSCFDnCFPCTRx */
329 #define RCANFD_CFPCTR(gpriv, ch, idx) \
330 	(reg_v3u(gpriv, 0x0240, 0x01d8) + (0x0c * (ch)) + (0x04 * (idx)))
331 
332 /* RSCFDnCFDFESTS / RSCFDnFESTS */
333 #define RCANFD_FESTS			(0x0238)
334 /* RSCFDnCFDFFSTS / RSCFDnFFSTS */
335 #define RCANFD_FFSTS			(0x023c)
336 /* RSCFDnCFDFMSTS / RSCFDnFMSTS */
337 #define RCANFD_FMSTS			(0x0240)
338 /* RSCFDnCFDRFISTS / RSCFDnRFISTS */
339 #define RCANFD_RFISTS			(0x0244)
340 /* RSCFDnCFDCFRISTS / RSCFDnCFRISTS */
341 #define RCANFD_CFRISTS			(0x0248)
342 /* RSCFDnCFDCFTISTS / RSCFDnCFTISTS */
343 #define RCANFD_CFTISTS			(0x024c)
344 
345 /* RSCFDnCFDTMCp / RSCFDnTMCp */
346 #define RCANFD_TMC(p)			(0x0250 + (0x01 * (p)))
347 /* RSCFDnCFDTMSTSp / RSCFDnTMSTSp */
348 #define RCANFD_TMSTS(p)			(0x02d0 + (0x01 * (p)))
349 
350 /* RSCFDnCFDTMTRSTSp / RSCFDnTMTRSTSp */
351 #define RCANFD_TMTRSTS(y)		(0x0350 + (0x04 * (y)))
352 /* RSCFDnCFDTMTARSTSp / RSCFDnTMTARSTSp */
353 #define RCANFD_TMTARSTS(y)		(0x0360 + (0x04 * (y)))
354 /* RSCFDnCFDTMTCSTSp / RSCFDnTMTCSTSp */
355 #define RCANFD_TMTCSTS(y)		(0x0370 + (0x04 * (y)))
356 /* RSCFDnCFDTMTASTSp / RSCFDnTMTASTSp */
357 #define RCANFD_TMTASTS(y)		(0x0380 + (0x04 * (y)))
358 /* RSCFDnCFDTMIECy / RSCFDnTMIECy */
359 #define RCANFD_TMIEC(y)			(0x0390 + (0x04 * (y)))
360 
361 /* RSCFDnCFDTXQCCm / RSCFDnTXQCCm */
362 #define RCANFD_TXQCC(m)			(0x03a0 + (0x04 * (m)))
363 /* RSCFDnCFDTXQSTSm / RSCFDnTXQSTSm */
364 #define RCANFD_TXQSTS(m)		(0x03c0 + (0x04 * (m)))
365 /* RSCFDnCFDTXQPCTRm / RSCFDnTXQPCTRm */
366 #define RCANFD_TXQPCTR(m)		(0x03e0 + (0x04 * (m)))
367 
368 /* RSCFDnCFDTHLCCm / RSCFDnTHLCCm */
369 #define RCANFD_THLCC(m)			(0x0400 + (0x04 * (m)))
370 /* RSCFDnCFDTHLSTSm / RSCFDnTHLSTSm */
371 #define RCANFD_THLSTS(m)		(0x0420 + (0x04 * (m)))
372 /* RSCFDnCFDTHLPCTRm / RSCFDnTHLPCTRm */
373 #define RCANFD_THLPCTR(m)		(0x0440 + (0x04 * (m)))
374 
375 /* RSCFDnCFDGTINTSTS0 / RSCFDnGTINTSTS0 */
376 #define RCANFD_GTINTSTS0		(0x0460)
377 /* RSCFDnCFDGTINTSTS1 / RSCFDnGTINTSTS1 */
378 #define RCANFD_GTINTSTS1		(0x0464)
379 /* RSCFDnCFDGTSTCFG / RSCFDnGTSTCFG */
380 #define RCANFD_GTSTCFG			(0x0468)
381 /* RSCFDnCFDGTSTCTR / RSCFDnGTSTCTR */
382 #define RCANFD_GTSTCTR			(0x046c)
383 /* RSCFDnCFDGLOCKK / RSCFDnGLOCKK */
384 #define RCANFD_GLOCKK			(0x047c)
385 /* RSCFDnCFDGRMCFG */
386 #define RCANFD_GRMCFG			(0x04fc)
387 
388 /* RSCFDnCFDGAFLIDj / RSCFDnGAFLIDj */
389 #define RCANFD_GAFLID(offset, j)	((offset) + (0x10 * (j)))
390 /* RSCFDnCFDGAFLMj / RSCFDnGAFLMj */
391 #define RCANFD_GAFLM(offset, j)		((offset) + 0x04 + (0x10 * (j)))
392 /* RSCFDnCFDGAFLP0j / RSCFDnGAFLP0j */
393 #define RCANFD_GAFLP0(offset, j)	((offset) + 0x08 + (0x10 * (j)))
394 /* RSCFDnCFDGAFLP1j / RSCFDnGAFLP1j */
395 #define RCANFD_GAFLP1(offset, j)	((offset) + 0x0c + (0x10 * (j)))
396 
397 /* Classical CAN only mode register map */
398 
399 /* RSCFDnGAFLXXXj offset */
400 #define RCANFD_C_GAFL_OFFSET		(0x0500)
401 
402 /* RSCFDnRMXXXq -> RCANFD_C_RMXXX(q) */
403 #define RCANFD_C_RMID(q)		(0x0600 + (0x10 * (q)))
404 #define RCANFD_C_RMPTR(q)		(0x0604 + (0x10 * (q)))
405 #define RCANFD_C_RMDF0(q)		(0x0608 + (0x10 * (q)))
406 #define RCANFD_C_RMDF1(q)		(0x060c + (0x10 * (q)))
407 
408 /* RSCFDnRFXXx -> RCANFD_C_RFXX(x) */
409 #define RCANFD_C_RFOFFSET	(0x0e00)
410 #define RCANFD_C_RFID(x)	(RCANFD_C_RFOFFSET + (0x10 * (x)))
411 #define RCANFD_C_RFPTR(x)	(RCANFD_C_RFOFFSET + 0x04 + (0x10 * (x)))
412 #define RCANFD_C_RFDF(x, df) \
413 		(RCANFD_C_RFOFFSET + 0x08 + (0x10 * (x)) + (0x04 * (df)))
414 
415 /* RSCFDnCFXXk -> RCANFD_C_CFXX(ch, k) */
416 #define RCANFD_C_CFOFFSET		(0x0e80)
417 
418 #define RCANFD_C_CFID(ch, idx) \
419 	(RCANFD_C_CFOFFSET + (0x30 * (ch)) + (0x10 * (idx)))
420 
421 #define RCANFD_C_CFPTR(ch, idx)	\
422 	(RCANFD_C_CFOFFSET + 0x04 + (0x30 * (ch)) + (0x10 * (idx)))
423 
424 #define RCANFD_C_CFDF(ch, idx, df) \
425 	(RCANFD_C_CFOFFSET + 0x08 + (0x30 * (ch)) + (0x10 * (idx)) + (0x04 * (df)))
426 
427 /* RSCFDnTMXXp -> RCANFD_C_TMXX(p) */
428 #define RCANFD_C_TMID(p)		(0x1000 + (0x10 * (p)))
429 #define RCANFD_C_TMPTR(p)		(0x1004 + (0x10 * (p)))
430 #define RCANFD_C_TMDF0(p)		(0x1008 + (0x10 * (p)))
431 #define RCANFD_C_TMDF1(p)		(0x100c + (0x10 * (p)))
432 
433 /* RSCFDnTHLACCm */
434 #define RCANFD_C_THLACC(m)		(0x1800 + (0x04 * (m)))
435 /* RSCFDnRPGACCr */
436 #define RCANFD_C_RPGACC(r)		(0x1900 + (0x04 * (r)))
437 
438 /* R-Car V3U Classical and CAN FD mode specific register map */
439 #define RCANFD_V3U_CFDCFG		(0x1314)
440 #define RCANFD_V3U_DCFG(m)		(0x1400 + (0x20 * (m)))
441 
442 #define RCANFD_V3U_GAFL_OFFSET		(0x1800)
443 
444 /* CAN FD mode specific register map */
445 
446 /* RSCFDnCFDCmXXX -> RCANFD_F_XXX(m) */
447 #define RCANFD_F_DCFG(m)		(0x0500 + (0x20 * (m)))
448 #define RCANFD_F_CFDCFG(m)		(0x0504 + (0x20 * (m)))
449 #define RCANFD_F_CFDCTR(m)		(0x0508 + (0x20 * (m)))
450 #define RCANFD_F_CFDSTS(m)		(0x050c + (0x20 * (m)))
451 #define RCANFD_F_CFDCRC(m)		(0x0510 + (0x20 * (m)))
452 
453 /* RSCFDnCFDGAFLXXXj offset */
454 #define RCANFD_F_GAFL_OFFSET		(0x1000)
455 
456 /* RSCFDnCFDRMXXXq -> RCANFD_F_RMXXX(q) */
457 #define RCANFD_F_RMID(q)		(0x2000 + (0x20 * (q)))
458 #define RCANFD_F_RMPTR(q)		(0x2004 + (0x20 * (q)))
459 #define RCANFD_F_RMFDSTS(q)		(0x2008 + (0x20 * (q)))
460 #define RCANFD_F_RMDF(q, b)		(0x200c + (0x04 * (b)) + (0x20 * (q)))
461 
462 /* RSCFDnCFDRFXXx -> RCANFD_F_RFXX(x) */
463 #define RCANFD_F_RFOFFSET(gpriv)	reg_v3u(gpriv, 0x6000, 0x3000)
464 #define RCANFD_F_RFID(gpriv, x)		(RCANFD_F_RFOFFSET(gpriv) + (0x80 * (x)))
465 #define RCANFD_F_RFPTR(gpriv, x)	(RCANFD_F_RFOFFSET(gpriv) + 0x04 + (0x80 * (x)))
466 #define RCANFD_F_RFFDSTS(gpriv, x)	(RCANFD_F_RFOFFSET(gpriv) + 0x08 + (0x80 * (x)))
467 #define RCANFD_F_RFDF(gpriv, x, df) \
468 	(RCANFD_F_RFOFFSET(gpriv) + 0x0c + (0x80 * (x)) + (0x04 * (df)))
469 
470 /* RSCFDnCFDCFXXk -> RCANFD_F_CFXX(ch, k) */
471 #define RCANFD_F_CFOFFSET(gpriv)	reg_v3u(gpriv, 0x6400, 0x3400)
472 
473 #define RCANFD_F_CFID(gpriv, ch, idx) \
474 	(RCANFD_F_CFOFFSET(gpriv) + (0x180 * (ch)) + (0x80 * (idx)))
475 
476 #define RCANFD_F_CFPTR(gpriv, ch, idx) \
477 	(RCANFD_F_CFOFFSET(gpriv) + 0x04 + (0x180 * (ch)) + (0x80 * (idx)))
478 
479 #define RCANFD_F_CFFDCSTS(gpriv, ch, idx) \
480 	(RCANFD_F_CFOFFSET(gpriv) + 0x08 + (0x180 * (ch)) + (0x80 * (idx)))
481 
482 #define RCANFD_F_CFDF(gpriv, ch, idx, df) \
483 	(RCANFD_F_CFOFFSET(gpriv) + 0x0c + (0x180 * (ch)) + (0x80 * (idx)) + \
484 	 (0x04 * (df)))
485 
486 /* RSCFDnCFDTMXXp -> RCANFD_F_TMXX(p) */
487 #define RCANFD_F_TMID(p)		(0x4000 + (0x20 * (p)))
488 #define RCANFD_F_TMPTR(p)		(0x4004 + (0x20 * (p)))
489 #define RCANFD_F_TMFDCTR(p)		(0x4008 + (0x20 * (p)))
490 #define RCANFD_F_TMDF(p, b)		(0x400c + (0x20 * (p)) + (0x04 * (b)))
491 
492 /* RSCFDnCFDTHLACCm */
493 #define RCANFD_F_THLACC(m)		(0x6000 + (0x04 * (m)))
494 /* RSCFDnCFDRPGACCr */
495 #define RCANFD_F_RPGACC(r)		(0x6400 + (0x04 * (r)))
496 
497 /* Constants */
498 #define RCANFD_FIFO_DEPTH		8	/* Tx FIFO depth */
499 #define RCANFD_NAPI_WEIGHT		8	/* Rx poll quota */
500 
501 #define RCANFD_NUM_CHANNELS		8	/* Eight channels max */
502 #define RCANFD_CHANNELS_MASK		BIT((RCANFD_NUM_CHANNELS) - 1)
503 
504 #define RCANFD_GAFL_PAGENUM(entry)	((entry) / 16)
505 #define RCANFD_CHANNEL_NUMRULES		1	/* only one rule per channel */
506 
507 /* Rx FIFO is a global resource of the controller. There are 8 such FIFOs
508  * available. Each channel gets a dedicated Rx FIFO (i.e.) the channel
509  * number is added to RFFIFO index.
510  */
511 #define RCANFD_RFFIFO_IDX		0
512 
513 /* Tx/Rx or Common FIFO is a per channel resource. Each channel has 3 Common
514  * FIFOs dedicated to them. Use the first (index 0) FIFO out of the 3 for Tx.
515  */
516 #define RCANFD_CFFIFO_IDX		0
517 
518 /* fCAN clock select register settings */
519 enum rcar_canfd_fcanclk {
520 	RCANFD_CANFDCLK = 0,		/* CANFD clock */
521 	RCANFD_EXTCLK,			/* Externally input clock */
522 };
523 
524 struct rcar_canfd_global;
525 
526 /* Channel priv data */
527 struct rcar_canfd_channel {
528 	struct can_priv can;			/* Must be the first member */
529 	struct net_device *ndev;
530 	struct rcar_canfd_global *gpriv;	/* Controller reference */
531 	void __iomem *base;			/* Register base address */
532 	struct napi_struct napi;
533 	u32 tx_head;				/* Incremented on xmit */
534 	u32 tx_tail;				/* Incremented on xmit done */
535 	u32 channel;				/* Channel number */
536 	spinlock_t tx_lock;			/* To protect tx path */
537 };
538 
539 /* Global priv data */
540 struct rcar_canfd_global {
541 	struct rcar_canfd_channel *ch[RCANFD_NUM_CHANNELS];
542 	void __iomem *base;		/* Register base address */
543 	struct platform_device *pdev;	/* Respective platform device */
544 	struct clk *clkp;		/* Peripheral clock */
545 	struct clk *can_clk;		/* fCAN clock */
546 	enum rcar_canfd_fcanclk fcan;	/* CANFD or Ext clock */
547 	unsigned long channels_mask;	/* Enabled channels mask */
548 	bool fdmode;			/* CAN FD or Classical CAN only mode */
549 	struct reset_control *rstc1;
550 	struct reset_control *rstc2;
551 	enum rcanfd_chip_id chip_id;
552 	u32 max_channels;
553 };
554 
555 /* CAN FD mode nominal rate constants */
556 static const struct can_bittiming_const rcar_canfd_nom_bittiming_const = {
557 	.name = RCANFD_DRV_NAME,
558 	.tseg1_min = 2,
559 	.tseg1_max = 128,
560 	.tseg2_min = 2,
561 	.tseg2_max = 32,
562 	.sjw_max = 32,
563 	.brp_min = 1,
564 	.brp_max = 1024,
565 	.brp_inc = 1,
566 };
567 
568 /* CAN FD mode data rate constants */
569 static const struct can_bittiming_const rcar_canfd_data_bittiming_const = {
570 	.name = RCANFD_DRV_NAME,
571 	.tseg1_min = 2,
572 	.tseg1_max = 16,
573 	.tseg2_min = 2,
574 	.tseg2_max = 8,
575 	.sjw_max = 8,
576 	.brp_min = 1,
577 	.brp_max = 256,
578 	.brp_inc = 1,
579 };
580 
581 /* Classical CAN mode bitrate constants */
582 static const struct can_bittiming_const rcar_canfd_bittiming_const = {
583 	.name = RCANFD_DRV_NAME,
584 	.tseg1_min = 4,
585 	.tseg1_max = 16,
586 	.tseg2_min = 2,
587 	.tseg2_max = 8,
588 	.sjw_max = 4,
589 	.brp_min = 1,
590 	.brp_max = 1024,
591 	.brp_inc = 1,
592 };
593 
594 /* Helper functions */
595 static inline bool is_v3u(struct rcar_canfd_global *gpriv)
596 {
597 	return gpriv->chip_id == RENESAS_R8A779A0;
598 }
599 
600 static inline u32 reg_v3u(struct rcar_canfd_global *gpriv,
601 			  u32 v3u, u32 not_v3u)
602 {
603 	return is_v3u(gpriv) ? v3u : not_v3u;
604 }
605 
606 static inline void rcar_canfd_update(u32 mask, u32 val, u32 __iomem *reg)
607 {
608 	u32 data = readl(reg);
609 
610 	data &= ~mask;
611 	data |= (val & mask);
612 	writel(data, reg);
613 }
614 
615 static inline u32 rcar_canfd_read(void __iomem *base, u32 offset)
616 {
617 	return readl(base + (offset));
618 }
619 
620 static inline void rcar_canfd_write(void __iomem *base, u32 offset, u32 val)
621 {
622 	writel(val, base + (offset));
623 }
624 
625 static void rcar_canfd_set_bit(void __iomem *base, u32 reg, u32 val)
626 {
627 	rcar_canfd_update(val, val, base + (reg));
628 }
629 
630 static void rcar_canfd_clear_bit(void __iomem *base, u32 reg, u32 val)
631 {
632 	rcar_canfd_update(val, 0, base + (reg));
633 }
634 
635 static void rcar_canfd_update_bit(void __iomem *base, u32 reg,
636 				  u32 mask, u32 val)
637 {
638 	rcar_canfd_update(mask, val, base + (reg));
639 }
640 
641 static void rcar_canfd_get_data(struct rcar_canfd_channel *priv,
642 				struct canfd_frame *cf, u32 off)
643 {
644 	u32 i, lwords;
645 
646 	lwords = DIV_ROUND_UP(cf->len, sizeof(u32));
647 	for (i = 0; i < lwords; i++)
648 		*((u32 *)cf->data + i) =
649 			rcar_canfd_read(priv->base, off + (i * sizeof(u32)));
650 }
651 
652 static void rcar_canfd_put_data(struct rcar_canfd_channel *priv,
653 				struct canfd_frame *cf, u32 off)
654 {
655 	u32 i, lwords;
656 
657 	lwords = DIV_ROUND_UP(cf->len, sizeof(u32));
658 	for (i = 0; i < lwords; i++)
659 		rcar_canfd_write(priv->base, off + (i * sizeof(u32)),
660 				 *((u32 *)cf->data + i));
661 }
662 
663 static void rcar_canfd_tx_failure_cleanup(struct net_device *ndev)
664 {
665 	u32 i;
666 
667 	for (i = 0; i < RCANFD_FIFO_DEPTH; i++)
668 		can_free_echo_skb(ndev, i, NULL);
669 }
670 
671 static void rcar_canfd_set_mode(struct rcar_canfd_global *gpriv)
672 {
673 	if (is_v3u(gpriv)) {
674 		if (gpriv->fdmode)
675 			rcar_canfd_set_bit(gpriv->base, RCANFD_V3U_CFDCFG,
676 					   RCANFD_FDCFG_FDOE);
677 		else
678 			rcar_canfd_set_bit(gpriv->base, RCANFD_V3U_CFDCFG,
679 					   RCANFD_FDCFG_CLOE);
680 	} else {
681 		if (gpriv->fdmode)
682 			rcar_canfd_set_bit(gpriv->base, RCANFD_GRMCFG,
683 					   RCANFD_GRMCFG_RCMC);
684 		else
685 			rcar_canfd_clear_bit(gpriv->base, RCANFD_GRMCFG,
686 					     RCANFD_GRMCFG_RCMC);
687 	}
688 }
689 
690 static int rcar_canfd_reset_controller(struct rcar_canfd_global *gpriv)
691 {
692 	u32 sts, ch;
693 	int err;
694 
695 	/* Check RAMINIT flag as CAN RAM initialization takes place
696 	 * after the MCU reset
697 	 */
698 	err = readl_poll_timeout((gpriv->base + RCANFD_GSTS), sts,
699 				 !(sts & RCANFD_GSTS_GRAMINIT), 2, 500000);
700 	if (err) {
701 		dev_dbg(&gpriv->pdev->dev, "global raminit failed\n");
702 		return err;
703 	}
704 
705 	/* Transition to Global Reset mode */
706 	rcar_canfd_clear_bit(gpriv->base, RCANFD_GCTR, RCANFD_GCTR_GSLPR);
707 	rcar_canfd_update_bit(gpriv->base, RCANFD_GCTR,
708 			      RCANFD_GCTR_GMDC_MASK, RCANFD_GCTR_GMDC_GRESET);
709 
710 	/* Ensure Global reset mode */
711 	err = readl_poll_timeout((gpriv->base + RCANFD_GSTS), sts,
712 				 (sts & RCANFD_GSTS_GRSTSTS), 2, 500000);
713 	if (err) {
714 		dev_dbg(&gpriv->pdev->dev, "global reset failed\n");
715 		return err;
716 	}
717 
718 	/* Reset Global error flags */
719 	rcar_canfd_write(gpriv->base, RCANFD_GERFL, 0x0);
720 
721 	/* Set the controller into appropriate mode */
722 	rcar_canfd_set_mode(gpriv);
723 
724 	/* Transition all Channels to reset mode */
725 	for_each_set_bit(ch, &gpriv->channels_mask, gpriv->max_channels) {
726 		rcar_canfd_clear_bit(gpriv->base,
727 				     RCANFD_CCTR(ch), RCANFD_CCTR_CSLPR);
728 
729 		rcar_canfd_update_bit(gpriv->base, RCANFD_CCTR(ch),
730 				      RCANFD_CCTR_CHMDC_MASK,
731 				      RCANFD_CCTR_CHDMC_CRESET);
732 
733 		/* Ensure Channel reset mode */
734 		err = readl_poll_timeout((gpriv->base + RCANFD_CSTS(ch)), sts,
735 					 (sts & RCANFD_CSTS_CRSTSTS),
736 					 2, 500000);
737 		if (err) {
738 			dev_dbg(&gpriv->pdev->dev,
739 				"channel %u reset failed\n", ch);
740 			return err;
741 		}
742 	}
743 	return 0;
744 }
745 
746 static void rcar_canfd_configure_controller(struct rcar_canfd_global *gpriv)
747 {
748 	u32 cfg, ch;
749 
750 	/* Global configuration settings */
751 
752 	/* ECC Error flag Enable */
753 	cfg = RCANFD_GCFG_EEFE;
754 
755 	if (gpriv->fdmode)
756 		/* Truncate payload to configured message size RFPLS */
757 		cfg |= RCANFD_GCFG_CMPOC;
758 
759 	/* Set External Clock if selected */
760 	if (gpriv->fcan != RCANFD_CANFDCLK)
761 		cfg |= RCANFD_GCFG_DCS;
762 
763 	rcar_canfd_set_bit(gpriv->base, RCANFD_GCFG, cfg);
764 
765 	/* Channel configuration settings */
766 	for_each_set_bit(ch, &gpriv->channels_mask, gpriv->max_channels) {
767 		rcar_canfd_set_bit(gpriv->base, RCANFD_CCTR(ch),
768 				   RCANFD_CCTR_ERRD);
769 		rcar_canfd_update_bit(gpriv->base, RCANFD_CCTR(ch),
770 				      RCANFD_CCTR_BOM_MASK,
771 				      RCANFD_CCTR_BOM_BENTRY);
772 	}
773 }
774 
775 static void rcar_canfd_configure_afl_rules(struct rcar_canfd_global *gpriv,
776 					   u32 ch)
777 {
778 	u32 cfg;
779 	int offset, start, page, num_rules = RCANFD_CHANNEL_NUMRULES;
780 	u32 ridx = ch + RCANFD_RFFIFO_IDX;
781 
782 	if (ch == 0) {
783 		start = 0; /* Channel 0 always starts from 0th rule */
784 	} else {
785 		/* Get number of Channel 0 rules and adjust */
786 		cfg = rcar_canfd_read(gpriv->base, RCANFD_GAFLCFG(ch));
787 		start = RCANFD_GAFLCFG_GETRNC(gpriv, 0, cfg);
788 	}
789 
790 	/* Enable write access to entry */
791 	page = RCANFD_GAFL_PAGENUM(start);
792 	rcar_canfd_set_bit(gpriv->base, RCANFD_GAFLECTR,
793 			   (RCANFD_GAFLECTR_AFLPN(gpriv, page) |
794 			    RCANFD_GAFLECTR_AFLDAE));
795 
796 	/* Write number of rules for channel */
797 	rcar_canfd_set_bit(gpriv->base, RCANFD_GAFLCFG(ch),
798 			   RCANFD_GAFLCFG_SETRNC(gpriv, ch, num_rules));
799 	if (is_v3u(gpriv))
800 		offset = RCANFD_V3U_GAFL_OFFSET;
801 	else if (gpriv->fdmode)
802 		offset = RCANFD_F_GAFL_OFFSET;
803 	else
804 		offset = RCANFD_C_GAFL_OFFSET;
805 
806 	/* Accept all IDs */
807 	rcar_canfd_write(gpriv->base, RCANFD_GAFLID(offset, start), 0);
808 	/* IDE or RTR is not considered for matching */
809 	rcar_canfd_write(gpriv->base, RCANFD_GAFLM(offset, start), 0);
810 	/* Any data length accepted */
811 	rcar_canfd_write(gpriv->base, RCANFD_GAFLP0(offset, start), 0);
812 	/* Place the msg in corresponding Rx FIFO entry */
813 	rcar_canfd_set_bit(gpriv->base, RCANFD_GAFLP1(offset, start),
814 			   RCANFD_GAFLP1_GAFLFDP(ridx));
815 
816 	/* Disable write access to page */
817 	rcar_canfd_clear_bit(gpriv->base,
818 			     RCANFD_GAFLECTR, RCANFD_GAFLECTR_AFLDAE);
819 }
820 
821 static void rcar_canfd_configure_rx(struct rcar_canfd_global *gpriv, u32 ch)
822 {
823 	/* Rx FIFO is used for reception */
824 	u32 cfg;
825 	u16 rfdc, rfpls;
826 
827 	/* Select Rx FIFO based on channel */
828 	u32 ridx = ch + RCANFD_RFFIFO_IDX;
829 
830 	rfdc = 2;		/* b010 - 8 messages Rx FIFO depth */
831 	if (gpriv->fdmode)
832 		rfpls = 7;	/* b111 - Max 64 bytes payload */
833 	else
834 		rfpls = 0;	/* b000 - Max 8 bytes payload */
835 
836 	cfg = (RCANFD_RFCC_RFIM | RCANFD_RFCC_RFDC(rfdc) |
837 		RCANFD_RFCC_RFPLS(rfpls) | RCANFD_RFCC_RFIE);
838 	rcar_canfd_write(gpriv->base, RCANFD_RFCC(gpriv, ridx), cfg);
839 }
840 
841 static void rcar_canfd_configure_tx(struct rcar_canfd_global *gpriv, u32 ch)
842 {
843 	/* Tx/Rx(Common) FIFO configured in Tx mode is
844 	 * used for transmission
845 	 *
846 	 * Each channel has 3 Common FIFO dedicated to them.
847 	 * Use the 1st (index 0) out of 3
848 	 */
849 	u32 cfg;
850 	u16 cftml, cfm, cfdc, cfpls;
851 
852 	cftml = 0;		/* 0th buffer */
853 	cfm = 1;		/* b01 - Transmit mode */
854 	cfdc = 2;		/* b010 - 8 messages Tx FIFO depth */
855 	if (gpriv->fdmode)
856 		cfpls = 7;	/* b111 - Max 64 bytes payload */
857 	else
858 		cfpls = 0;	/* b000 - Max 8 bytes payload */
859 
860 	cfg = (RCANFD_CFCC_CFTML(gpriv, cftml) | RCANFD_CFCC_CFM(gpriv, cfm) |
861 		RCANFD_CFCC_CFIM | RCANFD_CFCC_CFDC(gpriv, cfdc) |
862 		RCANFD_CFCC_CFPLS(cfpls) | RCANFD_CFCC_CFTXIE);
863 	rcar_canfd_write(gpriv->base, RCANFD_CFCC(gpriv, ch, RCANFD_CFFIFO_IDX), cfg);
864 
865 	if (gpriv->fdmode)
866 		/* Clear FD mode specific control/status register */
867 		rcar_canfd_write(gpriv->base,
868 				 RCANFD_F_CFFDCSTS(gpriv, ch, RCANFD_CFFIFO_IDX), 0);
869 }
870 
871 static void rcar_canfd_enable_global_interrupts(struct rcar_canfd_global *gpriv)
872 {
873 	u32 ctr;
874 
875 	/* Clear any stray error interrupt flags */
876 	rcar_canfd_write(gpriv->base, RCANFD_GERFL, 0);
877 
878 	/* Global interrupts setup */
879 	ctr = RCANFD_GCTR_MEIE;
880 	if (gpriv->fdmode)
881 		ctr |= RCANFD_GCTR_CFMPOFIE;
882 
883 	rcar_canfd_set_bit(gpriv->base, RCANFD_GCTR, ctr);
884 }
885 
886 static void rcar_canfd_disable_global_interrupts(struct rcar_canfd_global
887 						 *gpriv)
888 {
889 	/* Disable all interrupts */
890 	rcar_canfd_write(gpriv->base, RCANFD_GCTR, 0);
891 
892 	/* Clear any stray error interrupt flags */
893 	rcar_canfd_write(gpriv->base, RCANFD_GERFL, 0);
894 }
895 
896 static void rcar_canfd_enable_channel_interrupts(struct rcar_canfd_channel
897 						 *priv)
898 {
899 	u32 ctr, ch = priv->channel;
900 
901 	/* Clear any stray error flags */
902 	rcar_canfd_write(priv->base, RCANFD_CERFL(ch), 0);
903 
904 	/* Channel interrupts setup */
905 	ctr = (RCANFD_CCTR_TAIE |
906 	       RCANFD_CCTR_ALIE | RCANFD_CCTR_BLIE |
907 	       RCANFD_CCTR_OLIE | RCANFD_CCTR_BORIE |
908 	       RCANFD_CCTR_BOEIE | RCANFD_CCTR_EPIE |
909 	       RCANFD_CCTR_EWIE | RCANFD_CCTR_BEIE);
910 	rcar_canfd_set_bit(priv->base, RCANFD_CCTR(ch), ctr);
911 }
912 
913 static void rcar_canfd_disable_channel_interrupts(struct rcar_canfd_channel
914 						  *priv)
915 {
916 	u32 ctr, ch = priv->channel;
917 
918 	ctr = (RCANFD_CCTR_TAIE |
919 	       RCANFD_CCTR_ALIE | RCANFD_CCTR_BLIE |
920 	       RCANFD_CCTR_OLIE | RCANFD_CCTR_BORIE |
921 	       RCANFD_CCTR_BOEIE | RCANFD_CCTR_EPIE |
922 	       RCANFD_CCTR_EWIE | RCANFD_CCTR_BEIE);
923 	rcar_canfd_clear_bit(priv->base, RCANFD_CCTR(ch), ctr);
924 
925 	/* Clear any stray error flags */
926 	rcar_canfd_write(priv->base, RCANFD_CERFL(ch), 0);
927 }
928 
929 static void rcar_canfd_global_error(struct net_device *ndev)
930 {
931 	struct rcar_canfd_channel *priv = netdev_priv(ndev);
932 	struct rcar_canfd_global *gpriv = priv->gpriv;
933 	struct net_device_stats *stats = &ndev->stats;
934 	u32 ch = priv->channel;
935 	u32 gerfl, sts;
936 	u32 ridx = ch + RCANFD_RFFIFO_IDX;
937 
938 	gerfl = rcar_canfd_read(priv->base, RCANFD_GERFL);
939 	if ((gerfl & RCANFD_GERFL_EEF0) && (ch == 0)) {
940 		netdev_dbg(ndev, "Ch0: ECC Error flag\n");
941 		stats->tx_dropped++;
942 	}
943 	if ((gerfl & RCANFD_GERFL_EEF1) && (ch == 1)) {
944 		netdev_dbg(ndev, "Ch1: ECC Error flag\n");
945 		stats->tx_dropped++;
946 	}
947 	if (gerfl & RCANFD_GERFL_MES) {
948 		sts = rcar_canfd_read(priv->base,
949 				      RCANFD_CFSTS(gpriv, ch, RCANFD_CFFIFO_IDX));
950 		if (sts & RCANFD_CFSTS_CFMLT) {
951 			netdev_dbg(ndev, "Tx Message Lost flag\n");
952 			stats->tx_dropped++;
953 			rcar_canfd_write(priv->base,
954 					 RCANFD_CFSTS(gpriv, ch, RCANFD_CFFIFO_IDX),
955 					 sts & ~RCANFD_CFSTS_CFMLT);
956 		}
957 
958 		sts = rcar_canfd_read(priv->base, RCANFD_RFSTS(gpriv, ridx));
959 		if (sts & RCANFD_RFSTS_RFMLT) {
960 			netdev_dbg(ndev, "Rx Message Lost flag\n");
961 			stats->rx_dropped++;
962 			rcar_canfd_write(priv->base, RCANFD_RFSTS(gpriv, ridx),
963 					 sts & ~RCANFD_RFSTS_RFMLT);
964 		}
965 	}
966 	if (gpriv->fdmode && gerfl & RCANFD_GERFL_CMPOF) {
967 		/* Message Lost flag will be set for respective channel
968 		 * when this condition happens with counters and flags
969 		 * already updated.
970 		 */
971 		netdev_dbg(ndev, "global payload overflow interrupt\n");
972 	}
973 
974 	/* Clear all global error interrupts. Only affected channels bits
975 	 * get cleared
976 	 */
977 	rcar_canfd_write(priv->base, RCANFD_GERFL, 0);
978 }
979 
980 static void rcar_canfd_error(struct net_device *ndev, u32 cerfl,
981 			     u16 txerr, u16 rxerr)
982 {
983 	struct rcar_canfd_channel *priv = netdev_priv(ndev);
984 	struct net_device_stats *stats = &ndev->stats;
985 	struct can_frame *cf;
986 	struct sk_buff *skb;
987 	u32 ch = priv->channel;
988 
989 	netdev_dbg(ndev, "ch erfl %x txerr %u rxerr %u\n", cerfl, txerr, rxerr);
990 
991 	/* Propagate the error condition to the CAN stack */
992 	skb = alloc_can_err_skb(ndev, &cf);
993 	if (!skb) {
994 		stats->rx_dropped++;
995 		return;
996 	}
997 
998 	/* Channel error interrupts */
999 	if (cerfl & RCANFD_CERFL_BEF) {
1000 		netdev_dbg(ndev, "Bus error\n");
1001 		cf->can_id |= CAN_ERR_BUSERROR | CAN_ERR_PROT;
1002 		cf->data[2] = CAN_ERR_PROT_UNSPEC;
1003 		priv->can.can_stats.bus_error++;
1004 	}
1005 	if (cerfl & RCANFD_CERFL_ADERR) {
1006 		netdev_dbg(ndev, "ACK Delimiter Error\n");
1007 		stats->tx_errors++;
1008 		cf->data[3] |= CAN_ERR_PROT_LOC_ACK_DEL;
1009 	}
1010 	if (cerfl & RCANFD_CERFL_B0ERR) {
1011 		netdev_dbg(ndev, "Bit Error (dominant)\n");
1012 		stats->tx_errors++;
1013 		cf->data[2] |= CAN_ERR_PROT_BIT0;
1014 	}
1015 	if (cerfl & RCANFD_CERFL_B1ERR) {
1016 		netdev_dbg(ndev, "Bit Error (recessive)\n");
1017 		stats->tx_errors++;
1018 		cf->data[2] |= CAN_ERR_PROT_BIT1;
1019 	}
1020 	if (cerfl & RCANFD_CERFL_CERR) {
1021 		netdev_dbg(ndev, "CRC Error\n");
1022 		stats->rx_errors++;
1023 		cf->data[3] |= CAN_ERR_PROT_LOC_CRC_SEQ;
1024 	}
1025 	if (cerfl & RCANFD_CERFL_AERR) {
1026 		netdev_dbg(ndev, "ACK Error\n");
1027 		stats->tx_errors++;
1028 		cf->can_id |= CAN_ERR_ACK;
1029 		cf->data[3] |= CAN_ERR_PROT_LOC_ACK;
1030 	}
1031 	if (cerfl & RCANFD_CERFL_FERR) {
1032 		netdev_dbg(ndev, "Form Error\n");
1033 		stats->rx_errors++;
1034 		cf->data[2] |= CAN_ERR_PROT_FORM;
1035 	}
1036 	if (cerfl & RCANFD_CERFL_SERR) {
1037 		netdev_dbg(ndev, "Stuff Error\n");
1038 		stats->rx_errors++;
1039 		cf->data[2] |= CAN_ERR_PROT_STUFF;
1040 	}
1041 	if (cerfl & RCANFD_CERFL_ALF) {
1042 		netdev_dbg(ndev, "Arbitration lost Error\n");
1043 		priv->can.can_stats.arbitration_lost++;
1044 		cf->can_id |= CAN_ERR_LOSTARB;
1045 		cf->data[0] |= CAN_ERR_LOSTARB_UNSPEC;
1046 	}
1047 	if (cerfl & RCANFD_CERFL_BLF) {
1048 		netdev_dbg(ndev, "Bus Lock Error\n");
1049 		stats->rx_errors++;
1050 		cf->can_id |= CAN_ERR_BUSERROR;
1051 	}
1052 	if (cerfl & RCANFD_CERFL_EWF) {
1053 		netdev_dbg(ndev, "Error warning interrupt\n");
1054 		priv->can.state = CAN_STATE_ERROR_WARNING;
1055 		priv->can.can_stats.error_warning++;
1056 		cf->can_id |= CAN_ERR_CRTL;
1057 		cf->data[1] = txerr > rxerr ? CAN_ERR_CRTL_TX_WARNING :
1058 			CAN_ERR_CRTL_RX_WARNING;
1059 		cf->data[6] = txerr;
1060 		cf->data[7] = rxerr;
1061 	}
1062 	if (cerfl & RCANFD_CERFL_EPF) {
1063 		netdev_dbg(ndev, "Error passive interrupt\n");
1064 		priv->can.state = CAN_STATE_ERROR_PASSIVE;
1065 		priv->can.can_stats.error_passive++;
1066 		cf->can_id |= CAN_ERR_CRTL;
1067 		cf->data[1] = txerr > rxerr ? CAN_ERR_CRTL_TX_PASSIVE :
1068 			CAN_ERR_CRTL_RX_PASSIVE;
1069 		cf->data[6] = txerr;
1070 		cf->data[7] = rxerr;
1071 	}
1072 	if (cerfl & RCANFD_CERFL_BOEF) {
1073 		netdev_dbg(ndev, "Bus-off entry interrupt\n");
1074 		rcar_canfd_tx_failure_cleanup(ndev);
1075 		priv->can.state = CAN_STATE_BUS_OFF;
1076 		priv->can.can_stats.bus_off++;
1077 		can_bus_off(ndev);
1078 		cf->can_id |= CAN_ERR_BUSOFF;
1079 	}
1080 	if (cerfl & RCANFD_CERFL_OVLF) {
1081 		netdev_dbg(ndev,
1082 			   "Overload Frame Transmission error interrupt\n");
1083 		stats->tx_errors++;
1084 		cf->can_id |= CAN_ERR_PROT;
1085 		cf->data[2] |= CAN_ERR_PROT_OVERLOAD;
1086 	}
1087 
1088 	/* Clear channel error interrupts that are handled */
1089 	rcar_canfd_write(priv->base, RCANFD_CERFL(ch),
1090 			 RCANFD_CERFL_ERR(~cerfl));
1091 	netif_rx(skb);
1092 }
1093 
1094 static void rcar_canfd_tx_done(struct net_device *ndev)
1095 {
1096 	struct rcar_canfd_channel *priv = netdev_priv(ndev);
1097 	struct rcar_canfd_global *gpriv = priv->gpriv;
1098 	struct net_device_stats *stats = &ndev->stats;
1099 	u32 sts;
1100 	unsigned long flags;
1101 	u32 ch = priv->channel;
1102 
1103 	do {
1104 		u8 unsent, sent;
1105 
1106 		sent = priv->tx_tail % RCANFD_FIFO_DEPTH;
1107 		stats->tx_packets++;
1108 		stats->tx_bytes += can_get_echo_skb(ndev, sent, NULL);
1109 
1110 		spin_lock_irqsave(&priv->tx_lock, flags);
1111 		priv->tx_tail++;
1112 		sts = rcar_canfd_read(priv->base,
1113 				      RCANFD_CFSTS(gpriv, ch, RCANFD_CFFIFO_IDX));
1114 		unsent = RCANFD_CFSTS_CFMC(sts);
1115 
1116 		/* Wake producer only when there is room */
1117 		if (unsent != RCANFD_FIFO_DEPTH)
1118 			netif_wake_queue(ndev);
1119 
1120 		if (priv->tx_head - priv->tx_tail <= unsent) {
1121 			spin_unlock_irqrestore(&priv->tx_lock, flags);
1122 			break;
1123 		}
1124 		spin_unlock_irqrestore(&priv->tx_lock, flags);
1125 
1126 	} while (1);
1127 
1128 	/* Clear interrupt */
1129 	rcar_canfd_write(priv->base, RCANFD_CFSTS(gpriv, ch, RCANFD_CFFIFO_IDX),
1130 			 sts & ~RCANFD_CFSTS_CFTXIF);
1131 	can_led_event(ndev, CAN_LED_EVENT_TX);
1132 }
1133 
1134 static void rcar_canfd_handle_global_err(struct rcar_canfd_global *gpriv, u32 ch)
1135 {
1136 	struct rcar_canfd_channel *priv = gpriv->ch[ch];
1137 	struct net_device *ndev = priv->ndev;
1138 	u32 gerfl;
1139 
1140 	/* Handle global error interrupts */
1141 	gerfl = rcar_canfd_read(priv->base, RCANFD_GERFL);
1142 	if (unlikely(RCANFD_GERFL_ERR(gpriv, gerfl)))
1143 		rcar_canfd_global_error(ndev);
1144 }
1145 
1146 static irqreturn_t rcar_canfd_global_err_interrupt(int irq, void *dev_id)
1147 {
1148 	struct rcar_canfd_global *gpriv = dev_id;
1149 	u32 ch;
1150 
1151 	for_each_set_bit(ch, &gpriv->channels_mask, gpriv->max_channels)
1152 		rcar_canfd_handle_global_err(gpriv, ch);
1153 
1154 	return IRQ_HANDLED;
1155 }
1156 
1157 static void rcar_canfd_handle_global_receive(struct rcar_canfd_global *gpriv, u32 ch)
1158 {
1159 	struct rcar_canfd_channel *priv = gpriv->ch[ch];
1160 	u32 ridx = ch + RCANFD_RFFIFO_IDX;
1161 	u32 sts;
1162 
1163 	/* Handle Rx interrupts */
1164 	sts = rcar_canfd_read(priv->base, RCANFD_RFSTS(gpriv, ridx));
1165 	if (likely(sts & RCANFD_RFSTS_RFIF)) {
1166 		if (napi_schedule_prep(&priv->napi)) {
1167 			/* Disable Rx FIFO interrupts */
1168 			rcar_canfd_clear_bit(priv->base,
1169 					     RCANFD_RFCC(gpriv, ridx),
1170 					     RCANFD_RFCC_RFIE);
1171 			__napi_schedule(&priv->napi);
1172 		}
1173 	}
1174 }
1175 
1176 static irqreturn_t rcar_canfd_global_receive_fifo_interrupt(int irq, void *dev_id)
1177 {
1178 	struct rcar_canfd_global *gpriv = dev_id;
1179 	u32 ch;
1180 
1181 	for_each_set_bit(ch, &gpriv->channels_mask, gpriv->max_channels)
1182 		rcar_canfd_handle_global_receive(gpriv, ch);
1183 
1184 	return IRQ_HANDLED;
1185 }
1186 
1187 static irqreturn_t rcar_canfd_global_interrupt(int irq, void *dev_id)
1188 {
1189 	struct rcar_canfd_global *gpriv = dev_id;
1190 	u32 ch;
1191 
1192 	/* Global error interrupts still indicate a condition specific
1193 	 * to a channel. RxFIFO interrupt is a global interrupt.
1194 	 */
1195 	for_each_set_bit(ch, &gpriv->channels_mask, gpriv->max_channels) {
1196 		rcar_canfd_handle_global_err(gpriv, ch);
1197 		rcar_canfd_handle_global_receive(gpriv, ch);
1198 	}
1199 	return IRQ_HANDLED;
1200 }
1201 
1202 static void rcar_canfd_state_change(struct net_device *ndev,
1203 				    u16 txerr, u16 rxerr)
1204 {
1205 	struct rcar_canfd_channel *priv = netdev_priv(ndev);
1206 	struct net_device_stats *stats = &ndev->stats;
1207 	enum can_state rx_state, tx_state, state = priv->can.state;
1208 	struct can_frame *cf;
1209 	struct sk_buff *skb;
1210 
1211 	/* Handle transition from error to normal states */
1212 	if (txerr < 96 && rxerr < 96)
1213 		state = CAN_STATE_ERROR_ACTIVE;
1214 	else if (txerr < 128 && rxerr < 128)
1215 		state = CAN_STATE_ERROR_WARNING;
1216 
1217 	if (state != priv->can.state) {
1218 		netdev_dbg(ndev, "state: new %d, old %d: txerr %u, rxerr %u\n",
1219 			   state, priv->can.state, txerr, rxerr);
1220 		skb = alloc_can_err_skb(ndev, &cf);
1221 		if (!skb) {
1222 			stats->rx_dropped++;
1223 			return;
1224 		}
1225 		tx_state = txerr >= rxerr ? state : 0;
1226 		rx_state = txerr <= rxerr ? state : 0;
1227 
1228 		can_change_state(ndev, cf, tx_state, rx_state);
1229 		netif_rx(skb);
1230 	}
1231 }
1232 
1233 static void rcar_canfd_handle_channel_tx(struct rcar_canfd_global *gpriv, u32 ch)
1234 {
1235 	struct rcar_canfd_channel *priv = gpriv->ch[ch];
1236 	struct net_device *ndev = priv->ndev;
1237 	u32 sts;
1238 
1239 	/* Handle Tx interrupts */
1240 	sts = rcar_canfd_read(priv->base,
1241 			      RCANFD_CFSTS(gpriv, ch, RCANFD_CFFIFO_IDX));
1242 	if (likely(sts & RCANFD_CFSTS_CFTXIF))
1243 		rcar_canfd_tx_done(ndev);
1244 }
1245 
1246 static irqreturn_t rcar_canfd_channel_tx_interrupt(int irq, void *dev_id)
1247 {
1248 	struct rcar_canfd_global *gpriv = dev_id;
1249 	u32 ch;
1250 
1251 	for_each_set_bit(ch, &gpriv->channels_mask, gpriv->max_channels)
1252 		rcar_canfd_handle_channel_tx(gpriv, ch);
1253 
1254 	return IRQ_HANDLED;
1255 }
1256 
1257 static void rcar_canfd_handle_channel_err(struct rcar_canfd_global *gpriv, u32 ch)
1258 {
1259 	struct rcar_canfd_channel *priv = gpriv->ch[ch];
1260 	struct net_device *ndev = priv->ndev;
1261 	u16 txerr, rxerr;
1262 	u32 sts, cerfl;
1263 
1264 	/* Handle channel error interrupts */
1265 	cerfl = rcar_canfd_read(priv->base, RCANFD_CERFL(ch));
1266 	sts = rcar_canfd_read(priv->base, RCANFD_CSTS(ch));
1267 	txerr = RCANFD_CSTS_TECCNT(sts);
1268 	rxerr = RCANFD_CSTS_RECCNT(sts);
1269 	if (unlikely(RCANFD_CERFL_ERR(cerfl)))
1270 		rcar_canfd_error(ndev, cerfl, txerr, rxerr);
1271 
1272 	/* Handle state change to lower states */
1273 	if (unlikely(priv->can.state != CAN_STATE_ERROR_ACTIVE &&
1274 		     priv->can.state != CAN_STATE_BUS_OFF))
1275 		rcar_canfd_state_change(ndev, txerr, rxerr);
1276 }
1277 
1278 static irqreturn_t rcar_canfd_channel_err_interrupt(int irq, void *dev_id)
1279 {
1280 	struct rcar_canfd_global *gpriv = dev_id;
1281 	u32 ch;
1282 
1283 	for_each_set_bit(ch, &gpriv->channels_mask, gpriv->max_channels)
1284 		rcar_canfd_handle_channel_err(gpriv, ch);
1285 
1286 	return IRQ_HANDLED;
1287 }
1288 
1289 static irqreturn_t rcar_canfd_channel_interrupt(int irq, void *dev_id)
1290 {
1291 	struct rcar_canfd_global *gpriv = dev_id;
1292 	u32 ch;
1293 
1294 	/* Common FIFO is a per channel resource */
1295 	for_each_set_bit(ch, &gpriv->channels_mask, gpriv->max_channels) {
1296 		rcar_canfd_handle_channel_err(gpriv, ch);
1297 		rcar_canfd_handle_channel_tx(gpriv, ch);
1298 	}
1299 
1300 	return IRQ_HANDLED;
1301 }
1302 
1303 static void rcar_canfd_set_bittiming(struct net_device *dev)
1304 {
1305 	struct rcar_canfd_channel *priv = netdev_priv(dev);
1306 	struct rcar_canfd_global *gpriv = priv->gpriv;
1307 	const struct can_bittiming *bt = &priv->can.bittiming;
1308 	const struct can_bittiming *dbt = &priv->can.data_bittiming;
1309 	u16 brp, sjw, tseg1, tseg2;
1310 	u32 cfg;
1311 	u32 ch = priv->channel;
1312 
1313 	/* Nominal bit timing settings */
1314 	brp = bt->brp - 1;
1315 	sjw = bt->sjw - 1;
1316 	tseg1 = bt->prop_seg + bt->phase_seg1 - 1;
1317 	tseg2 = bt->phase_seg2 - 1;
1318 
1319 	if (priv->can.ctrlmode & CAN_CTRLMODE_FD) {
1320 		/* CAN FD only mode */
1321 		cfg = (RCANFD_NCFG_NTSEG1(gpriv, tseg1) | RCANFD_NCFG_NBRP(brp) |
1322 		       RCANFD_NCFG_NSJW(gpriv, sjw) | RCANFD_NCFG_NTSEG2(gpriv, tseg2));
1323 
1324 		rcar_canfd_write(priv->base, RCANFD_CCFG(ch), cfg);
1325 		netdev_dbg(priv->ndev, "nrate: brp %u, sjw %u, tseg1 %u, tseg2 %u\n",
1326 			   brp, sjw, tseg1, tseg2);
1327 
1328 		/* Data bit timing settings */
1329 		brp = dbt->brp - 1;
1330 		sjw = dbt->sjw - 1;
1331 		tseg1 = dbt->prop_seg + dbt->phase_seg1 - 1;
1332 		tseg2 = dbt->phase_seg2 - 1;
1333 
1334 		cfg = (RCANFD_DCFG_DTSEG1(gpriv, tseg1) | RCANFD_DCFG_DBRP(brp) |
1335 		       RCANFD_DCFG_DSJW(sjw) | RCANFD_DCFG_DTSEG2(gpriv, tseg2));
1336 
1337 		rcar_canfd_write(priv->base, RCANFD_F_DCFG(ch), cfg);
1338 		netdev_dbg(priv->ndev, "drate: brp %u, sjw %u, tseg1 %u, tseg2 %u\n",
1339 			   brp, sjw, tseg1, tseg2);
1340 	} else {
1341 		/* Classical CAN only mode */
1342 		if (is_v3u(gpriv)) {
1343 			cfg = (RCANFD_NCFG_NTSEG1(gpriv, tseg1) |
1344 			       RCANFD_NCFG_NBRP(brp) |
1345 			       RCANFD_NCFG_NSJW(gpriv, sjw) |
1346 			       RCANFD_NCFG_NTSEG2(gpriv, tseg2));
1347 		} else {
1348 			cfg = (RCANFD_CFG_TSEG1(tseg1) |
1349 			       RCANFD_CFG_BRP(brp) |
1350 			       RCANFD_CFG_SJW(sjw) |
1351 			       RCANFD_CFG_TSEG2(tseg2));
1352 		}
1353 
1354 		rcar_canfd_write(priv->base, RCANFD_CCFG(ch), cfg);
1355 		netdev_dbg(priv->ndev,
1356 			   "rate: brp %u, sjw %u, tseg1 %u, tseg2 %u\n",
1357 			   brp, sjw, tseg1, tseg2);
1358 	}
1359 }
1360 
1361 static int rcar_canfd_start(struct net_device *ndev)
1362 {
1363 	struct rcar_canfd_channel *priv = netdev_priv(ndev);
1364 	struct rcar_canfd_global *gpriv = priv->gpriv;
1365 	int err = -EOPNOTSUPP;
1366 	u32 sts, ch = priv->channel;
1367 	u32 ridx = ch + RCANFD_RFFIFO_IDX;
1368 
1369 	rcar_canfd_set_bittiming(ndev);
1370 
1371 	rcar_canfd_enable_channel_interrupts(priv);
1372 
1373 	/* Set channel to Operational mode */
1374 	rcar_canfd_update_bit(priv->base, RCANFD_CCTR(ch),
1375 			      RCANFD_CCTR_CHMDC_MASK, RCANFD_CCTR_CHDMC_COPM);
1376 
1377 	/* Verify channel mode change */
1378 	err = readl_poll_timeout((priv->base + RCANFD_CSTS(ch)), sts,
1379 				 (sts & RCANFD_CSTS_COMSTS), 2, 500000);
1380 	if (err) {
1381 		netdev_err(ndev, "channel %u communication state failed\n", ch);
1382 		goto fail_mode_change;
1383 	}
1384 
1385 	/* Enable Common & Rx FIFO */
1386 	rcar_canfd_set_bit(priv->base, RCANFD_CFCC(gpriv, ch, RCANFD_CFFIFO_IDX),
1387 			   RCANFD_CFCC_CFE);
1388 	rcar_canfd_set_bit(priv->base, RCANFD_RFCC(gpriv, ridx), RCANFD_RFCC_RFE);
1389 
1390 	priv->can.state = CAN_STATE_ERROR_ACTIVE;
1391 	return 0;
1392 
1393 fail_mode_change:
1394 	rcar_canfd_disable_channel_interrupts(priv);
1395 	return err;
1396 }
1397 
1398 static int rcar_canfd_open(struct net_device *ndev)
1399 {
1400 	struct rcar_canfd_channel *priv = netdev_priv(ndev);
1401 	struct rcar_canfd_global *gpriv = priv->gpriv;
1402 	int err;
1403 
1404 	/* Peripheral clock is already enabled in probe */
1405 	err = clk_prepare_enable(gpriv->can_clk);
1406 	if (err) {
1407 		netdev_err(ndev, "failed to enable CAN clock, error %d\n", err);
1408 		goto out_clock;
1409 	}
1410 
1411 	err = open_candev(ndev);
1412 	if (err) {
1413 		netdev_err(ndev, "open_candev() failed, error %d\n", err);
1414 		goto out_can_clock;
1415 	}
1416 
1417 	napi_enable(&priv->napi);
1418 	err = rcar_canfd_start(ndev);
1419 	if (err)
1420 		goto out_close;
1421 	netif_start_queue(ndev);
1422 	can_led_event(ndev, CAN_LED_EVENT_OPEN);
1423 	return 0;
1424 out_close:
1425 	napi_disable(&priv->napi);
1426 	close_candev(ndev);
1427 out_can_clock:
1428 	clk_disable_unprepare(gpriv->can_clk);
1429 out_clock:
1430 	return err;
1431 }
1432 
1433 static void rcar_canfd_stop(struct net_device *ndev)
1434 {
1435 	struct rcar_canfd_channel *priv = netdev_priv(ndev);
1436 	struct rcar_canfd_global *gpriv = priv->gpriv;
1437 	int err;
1438 	u32 sts, ch = priv->channel;
1439 	u32 ridx = ch + RCANFD_RFFIFO_IDX;
1440 
1441 	/* Transition to channel reset mode  */
1442 	rcar_canfd_update_bit(priv->base, RCANFD_CCTR(ch),
1443 			      RCANFD_CCTR_CHMDC_MASK, RCANFD_CCTR_CHDMC_CRESET);
1444 
1445 	/* Check Channel reset mode */
1446 	err = readl_poll_timeout((priv->base + RCANFD_CSTS(ch)), sts,
1447 				 (sts & RCANFD_CSTS_CRSTSTS), 2, 500000);
1448 	if (err)
1449 		netdev_err(ndev, "channel %u reset failed\n", ch);
1450 
1451 	rcar_canfd_disable_channel_interrupts(priv);
1452 
1453 	/* Disable Common & Rx FIFO */
1454 	rcar_canfd_clear_bit(priv->base, RCANFD_CFCC(gpriv, ch, RCANFD_CFFIFO_IDX),
1455 			     RCANFD_CFCC_CFE);
1456 	rcar_canfd_clear_bit(priv->base, RCANFD_RFCC(gpriv, ridx), RCANFD_RFCC_RFE);
1457 
1458 	/* Set the state as STOPPED */
1459 	priv->can.state = CAN_STATE_STOPPED;
1460 }
1461 
1462 static int rcar_canfd_close(struct net_device *ndev)
1463 {
1464 	struct rcar_canfd_channel *priv = netdev_priv(ndev);
1465 	struct rcar_canfd_global *gpriv = priv->gpriv;
1466 
1467 	netif_stop_queue(ndev);
1468 	rcar_canfd_stop(ndev);
1469 	napi_disable(&priv->napi);
1470 	clk_disable_unprepare(gpriv->can_clk);
1471 	close_candev(ndev);
1472 	can_led_event(ndev, CAN_LED_EVENT_STOP);
1473 	return 0;
1474 }
1475 
1476 static netdev_tx_t rcar_canfd_start_xmit(struct sk_buff *skb,
1477 					 struct net_device *ndev)
1478 {
1479 	struct rcar_canfd_channel *priv = netdev_priv(ndev);
1480 	struct rcar_canfd_global *gpriv = priv->gpriv;
1481 	struct canfd_frame *cf = (struct canfd_frame *)skb->data;
1482 	u32 sts = 0, id, dlc;
1483 	unsigned long flags;
1484 	u32 ch = priv->channel;
1485 
1486 	if (can_dropped_invalid_skb(ndev, skb))
1487 		return NETDEV_TX_OK;
1488 
1489 	if (cf->can_id & CAN_EFF_FLAG) {
1490 		id = cf->can_id & CAN_EFF_MASK;
1491 		id |= RCANFD_CFID_CFIDE;
1492 	} else {
1493 		id = cf->can_id & CAN_SFF_MASK;
1494 	}
1495 
1496 	if (cf->can_id & CAN_RTR_FLAG)
1497 		id |= RCANFD_CFID_CFRTR;
1498 
1499 	dlc = RCANFD_CFPTR_CFDLC(can_fd_len2dlc(cf->len));
1500 
1501 	if ((priv->can.ctrlmode & CAN_CTRLMODE_FD) || is_v3u(gpriv)) {
1502 		rcar_canfd_write(priv->base,
1503 				 RCANFD_F_CFID(gpriv, ch, RCANFD_CFFIFO_IDX), id);
1504 		rcar_canfd_write(priv->base,
1505 				 RCANFD_F_CFPTR(gpriv, ch, RCANFD_CFFIFO_IDX), dlc);
1506 
1507 		if (can_is_canfd_skb(skb)) {
1508 			/* CAN FD frame format */
1509 			sts |= RCANFD_CFFDCSTS_CFFDF;
1510 			if (cf->flags & CANFD_BRS)
1511 				sts |= RCANFD_CFFDCSTS_CFBRS;
1512 
1513 			if (priv->can.state == CAN_STATE_ERROR_PASSIVE)
1514 				sts |= RCANFD_CFFDCSTS_CFESI;
1515 		}
1516 
1517 		rcar_canfd_write(priv->base,
1518 				 RCANFD_F_CFFDCSTS(gpriv, ch, RCANFD_CFFIFO_IDX), sts);
1519 
1520 		rcar_canfd_put_data(priv, cf,
1521 				    RCANFD_F_CFDF(gpriv, ch, RCANFD_CFFIFO_IDX, 0));
1522 	} else {
1523 		rcar_canfd_write(priv->base,
1524 				 RCANFD_C_CFID(ch, RCANFD_CFFIFO_IDX), id);
1525 		rcar_canfd_write(priv->base,
1526 				 RCANFD_C_CFPTR(ch, RCANFD_CFFIFO_IDX), dlc);
1527 		rcar_canfd_put_data(priv, cf,
1528 				    RCANFD_C_CFDF(ch, RCANFD_CFFIFO_IDX, 0));
1529 	}
1530 
1531 	can_put_echo_skb(skb, ndev, priv->tx_head % RCANFD_FIFO_DEPTH, 0);
1532 
1533 	spin_lock_irqsave(&priv->tx_lock, flags);
1534 	priv->tx_head++;
1535 
1536 	/* Stop the queue if we've filled all FIFO entries */
1537 	if (priv->tx_head - priv->tx_tail >= RCANFD_FIFO_DEPTH)
1538 		netif_stop_queue(ndev);
1539 
1540 	/* Start Tx: Write 0xff to CFPC to increment the CPU-side
1541 	 * pointer for the Common FIFO
1542 	 */
1543 	rcar_canfd_write(priv->base,
1544 			 RCANFD_CFPCTR(gpriv, ch, RCANFD_CFFIFO_IDX), 0xff);
1545 
1546 	spin_unlock_irqrestore(&priv->tx_lock, flags);
1547 	return NETDEV_TX_OK;
1548 }
1549 
1550 static void rcar_canfd_rx_pkt(struct rcar_canfd_channel *priv)
1551 {
1552 	struct net_device_stats *stats = &priv->ndev->stats;
1553 	struct rcar_canfd_global *gpriv = priv->gpriv;
1554 	struct canfd_frame *cf;
1555 	struct sk_buff *skb;
1556 	u32 sts = 0, id, dlc;
1557 	u32 ch = priv->channel;
1558 	u32 ridx = ch + RCANFD_RFFIFO_IDX;
1559 
1560 	if ((priv->can.ctrlmode & CAN_CTRLMODE_FD) || is_v3u(gpriv)) {
1561 		id = rcar_canfd_read(priv->base, RCANFD_F_RFID(gpriv, ridx));
1562 		dlc = rcar_canfd_read(priv->base, RCANFD_F_RFPTR(gpriv, ridx));
1563 
1564 		sts = rcar_canfd_read(priv->base, RCANFD_F_RFFDSTS(gpriv, ridx));
1565 
1566 		if ((priv->can.ctrlmode & CAN_CTRLMODE_FD) &&
1567 		    sts & RCANFD_RFFDSTS_RFFDF)
1568 			skb = alloc_canfd_skb(priv->ndev, &cf);
1569 		else
1570 			skb = alloc_can_skb(priv->ndev,
1571 					    (struct can_frame **)&cf);
1572 	} else {
1573 		id = rcar_canfd_read(priv->base, RCANFD_C_RFID(ridx));
1574 		dlc = rcar_canfd_read(priv->base, RCANFD_C_RFPTR(ridx));
1575 		skb = alloc_can_skb(priv->ndev, (struct can_frame **)&cf);
1576 	}
1577 
1578 	if (!skb) {
1579 		stats->rx_dropped++;
1580 		return;
1581 	}
1582 
1583 	if (id & RCANFD_RFID_RFIDE)
1584 		cf->can_id = (id & CAN_EFF_MASK) | CAN_EFF_FLAG;
1585 	else
1586 		cf->can_id = id & CAN_SFF_MASK;
1587 
1588 	if (priv->can.ctrlmode & CAN_CTRLMODE_FD) {
1589 		if (sts & RCANFD_RFFDSTS_RFFDF)
1590 			cf->len = can_fd_dlc2len(RCANFD_RFPTR_RFDLC(dlc));
1591 		else
1592 			cf->len = can_cc_dlc2len(RCANFD_RFPTR_RFDLC(dlc));
1593 
1594 		if (sts & RCANFD_RFFDSTS_RFESI) {
1595 			cf->flags |= CANFD_ESI;
1596 			netdev_dbg(priv->ndev, "ESI Error\n");
1597 		}
1598 
1599 		if (!(sts & RCANFD_RFFDSTS_RFFDF) && (id & RCANFD_RFID_RFRTR)) {
1600 			cf->can_id |= CAN_RTR_FLAG;
1601 		} else {
1602 			if (sts & RCANFD_RFFDSTS_RFBRS)
1603 				cf->flags |= CANFD_BRS;
1604 
1605 			rcar_canfd_get_data(priv, cf, RCANFD_F_RFDF(gpriv, ridx, 0));
1606 		}
1607 	} else {
1608 		cf->len = can_cc_dlc2len(RCANFD_RFPTR_RFDLC(dlc));
1609 		if (id & RCANFD_RFID_RFRTR)
1610 			cf->can_id |= CAN_RTR_FLAG;
1611 		else if (is_v3u(gpriv))
1612 			rcar_canfd_get_data(priv, cf, RCANFD_F_RFDF(gpriv, ridx, 0));
1613 		else
1614 			rcar_canfd_get_data(priv, cf, RCANFD_C_RFDF(ridx, 0));
1615 	}
1616 
1617 	/* Write 0xff to RFPC to increment the CPU-side
1618 	 * pointer of the Rx FIFO
1619 	 */
1620 	rcar_canfd_write(priv->base, RCANFD_RFPCTR(gpriv, ridx), 0xff);
1621 
1622 	can_led_event(priv->ndev, CAN_LED_EVENT_RX);
1623 
1624 	if (!(cf->can_id & CAN_RTR_FLAG))
1625 		stats->rx_bytes += cf->len;
1626 	stats->rx_packets++;
1627 	netif_receive_skb(skb);
1628 }
1629 
1630 static int rcar_canfd_rx_poll(struct napi_struct *napi, int quota)
1631 {
1632 	struct rcar_canfd_channel *priv =
1633 		container_of(napi, struct rcar_canfd_channel, napi);
1634 	struct rcar_canfd_global *gpriv = priv->gpriv;
1635 	int num_pkts;
1636 	u32 sts;
1637 	u32 ch = priv->channel;
1638 	u32 ridx = ch + RCANFD_RFFIFO_IDX;
1639 
1640 	for (num_pkts = 0; num_pkts < quota; num_pkts++) {
1641 		sts = rcar_canfd_read(priv->base, RCANFD_RFSTS(gpriv, ridx));
1642 		/* Check FIFO empty condition */
1643 		if (sts & RCANFD_RFSTS_RFEMP)
1644 			break;
1645 
1646 		rcar_canfd_rx_pkt(priv);
1647 
1648 		/* Clear interrupt bit */
1649 		if (sts & RCANFD_RFSTS_RFIF)
1650 			rcar_canfd_write(priv->base, RCANFD_RFSTS(gpriv, ridx),
1651 					 sts & ~RCANFD_RFSTS_RFIF);
1652 	}
1653 
1654 	/* All packets processed */
1655 	if (num_pkts < quota) {
1656 		if (napi_complete_done(napi, num_pkts)) {
1657 			/* Enable Rx FIFO interrupts */
1658 			rcar_canfd_set_bit(priv->base, RCANFD_RFCC(gpriv, ridx),
1659 					   RCANFD_RFCC_RFIE);
1660 		}
1661 	}
1662 	return num_pkts;
1663 }
1664 
1665 static int rcar_canfd_do_set_mode(struct net_device *ndev, enum can_mode mode)
1666 {
1667 	int err;
1668 
1669 	switch (mode) {
1670 	case CAN_MODE_START:
1671 		err = rcar_canfd_start(ndev);
1672 		if (err)
1673 			return err;
1674 		netif_wake_queue(ndev);
1675 		return 0;
1676 	default:
1677 		return -EOPNOTSUPP;
1678 	}
1679 }
1680 
1681 static int rcar_canfd_get_berr_counter(const struct net_device *dev,
1682 				       struct can_berr_counter *bec)
1683 {
1684 	struct rcar_canfd_channel *priv = netdev_priv(dev);
1685 	u32 val, ch = priv->channel;
1686 
1687 	/* Peripheral clock is already enabled in probe */
1688 	val = rcar_canfd_read(priv->base, RCANFD_CSTS(ch));
1689 	bec->txerr = RCANFD_CSTS_TECCNT(val);
1690 	bec->rxerr = RCANFD_CSTS_RECCNT(val);
1691 	return 0;
1692 }
1693 
1694 static const struct net_device_ops rcar_canfd_netdev_ops = {
1695 	.ndo_open = rcar_canfd_open,
1696 	.ndo_stop = rcar_canfd_close,
1697 	.ndo_start_xmit = rcar_canfd_start_xmit,
1698 	.ndo_change_mtu = can_change_mtu,
1699 };
1700 
1701 static int rcar_canfd_channel_probe(struct rcar_canfd_global *gpriv, u32 ch,
1702 				    u32 fcan_freq)
1703 {
1704 	struct platform_device *pdev = gpriv->pdev;
1705 	struct rcar_canfd_channel *priv;
1706 	struct net_device *ndev;
1707 	int err = -ENODEV;
1708 
1709 	ndev = alloc_candev(sizeof(*priv), RCANFD_FIFO_DEPTH);
1710 	if (!ndev) {
1711 		dev_err(&pdev->dev, "alloc_candev() failed\n");
1712 		return -ENOMEM;
1713 	}
1714 	priv = netdev_priv(ndev);
1715 
1716 	ndev->netdev_ops = &rcar_canfd_netdev_ops;
1717 	ndev->flags |= IFF_ECHO;
1718 	priv->ndev = ndev;
1719 	priv->base = gpriv->base;
1720 	priv->channel = ch;
1721 	priv->can.clock.freq = fcan_freq;
1722 	dev_info(&pdev->dev, "can_clk rate is %u\n", priv->can.clock.freq);
1723 
1724 	if (gpriv->chip_id == RENESAS_RZG2L) {
1725 		char *irq_name;
1726 		int err_irq;
1727 		int tx_irq;
1728 
1729 		err_irq = platform_get_irq_byname(pdev, ch == 0 ? "ch0_err" : "ch1_err");
1730 		if (err_irq < 0) {
1731 			err = err_irq;
1732 			goto fail;
1733 		}
1734 
1735 		tx_irq = platform_get_irq_byname(pdev, ch == 0 ? "ch0_trx" : "ch1_trx");
1736 		if (tx_irq < 0) {
1737 			err = tx_irq;
1738 			goto fail;
1739 		}
1740 
1741 		irq_name = devm_kasprintf(&pdev->dev, GFP_KERNEL,
1742 					  "canfd.ch%d_err", ch);
1743 		if (!irq_name) {
1744 			err = -ENOMEM;
1745 			goto fail;
1746 		}
1747 		err = devm_request_irq(&pdev->dev, err_irq,
1748 				       rcar_canfd_channel_err_interrupt, 0,
1749 				       irq_name, gpriv);
1750 		if (err) {
1751 			dev_err(&pdev->dev, "devm_request_irq CH Err(%d) failed, error %d\n",
1752 				err_irq, err);
1753 			goto fail;
1754 		}
1755 		irq_name = devm_kasprintf(&pdev->dev, GFP_KERNEL,
1756 					  "canfd.ch%d_trx", ch);
1757 		if (!irq_name) {
1758 			err = -ENOMEM;
1759 			goto fail;
1760 		}
1761 		err = devm_request_irq(&pdev->dev, tx_irq,
1762 				       rcar_canfd_channel_tx_interrupt, 0,
1763 				       irq_name, gpriv);
1764 		if (err) {
1765 			dev_err(&pdev->dev, "devm_request_irq Tx (%d) failed, error %d\n",
1766 				tx_irq, err);
1767 			goto fail;
1768 		}
1769 	}
1770 
1771 	if (gpriv->fdmode) {
1772 		priv->can.bittiming_const = &rcar_canfd_nom_bittiming_const;
1773 		priv->can.data_bittiming_const =
1774 			&rcar_canfd_data_bittiming_const;
1775 
1776 		/* Controller starts in CAN FD only mode */
1777 		err = can_set_static_ctrlmode(ndev, CAN_CTRLMODE_FD);
1778 		if (err)
1779 			goto fail;
1780 		priv->can.ctrlmode_supported = CAN_CTRLMODE_BERR_REPORTING;
1781 	} else {
1782 		/* Controller starts in Classical CAN only mode */
1783 		priv->can.bittiming_const = &rcar_canfd_bittiming_const;
1784 		priv->can.ctrlmode_supported = CAN_CTRLMODE_BERR_REPORTING;
1785 	}
1786 
1787 	priv->can.do_set_mode = rcar_canfd_do_set_mode;
1788 	priv->can.do_get_berr_counter = rcar_canfd_get_berr_counter;
1789 	priv->gpriv = gpriv;
1790 	SET_NETDEV_DEV(ndev, &pdev->dev);
1791 
1792 	netif_napi_add(ndev, &priv->napi, rcar_canfd_rx_poll,
1793 		       RCANFD_NAPI_WEIGHT);
1794 	spin_lock_init(&priv->tx_lock);
1795 	devm_can_led_init(ndev);
1796 	gpriv->ch[priv->channel] = priv;
1797 	err = register_candev(ndev);
1798 	if (err) {
1799 		dev_err(&pdev->dev,
1800 			"register_candev() failed, error %d\n", err);
1801 		goto fail_candev;
1802 	}
1803 	dev_info(&pdev->dev, "device registered (channel %u)\n", priv->channel);
1804 	return 0;
1805 
1806 fail_candev:
1807 	netif_napi_del(&priv->napi);
1808 fail:
1809 	free_candev(ndev);
1810 	return err;
1811 }
1812 
1813 static void rcar_canfd_channel_remove(struct rcar_canfd_global *gpriv, u32 ch)
1814 {
1815 	struct rcar_canfd_channel *priv = gpriv->ch[ch];
1816 
1817 	if (priv) {
1818 		unregister_candev(priv->ndev);
1819 		netif_napi_del(&priv->napi);
1820 		free_candev(priv->ndev);
1821 	}
1822 }
1823 
1824 static int rcar_canfd_probe(struct platform_device *pdev)
1825 {
1826 	void __iomem *addr;
1827 	u32 sts, ch, fcan_freq;
1828 	struct rcar_canfd_global *gpriv;
1829 	struct device_node *of_child;
1830 	unsigned long channels_mask = 0;
1831 	int err, ch_irq, g_irq;
1832 	int g_err_irq, g_recc_irq;
1833 	bool fdmode = true;			/* CAN FD only mode - default */
1834 	enum rcanfd_chip_id chip_id;
1835 	int max_channels;
1836 	char name[9] = "channelX";
1837 	int i;
1838 
1839 	chip_id = (uintptr_t)of_device_get_match_data(&pdev->dev);
1840 	max_channels = chip_id == RENESAS_R8A779A0 ? 8 : 2;
1841 
1842 	if (of_property_read_bool(pdev->dev.of_node, "renesas,no-can-fd"))
1843 		fdmode = false;			/* Classical CAN only mode */
1844 
1845 	for (i = 0; i < max_channels; ++i) {
1846 		name[7] = '0' + i;
1847 		of_child = of_get_child_by_name(pdev->dev.of_node, name);
1848 		if (of_child && of_device_is_available(of_child))
1849 			channels_mask |= BIT(i);
1850 	}
1851 
1852 	if (chip_id != RENESAS_RZG2L) {
1853 		ch_irq = platform_get_irq_byname_optional(pdev, "ch_int");
1854 		if (ch_irq < 0) {
1855 			/* For backward compatibility get irq by index */
1856 			ch_irq = platform_get_irq(pdev, 0);
1857 			if (ch_irq < 0)
1858 				return ch_irq;
1859 		}
1860 
1861 		g_irq = platform_get_irq_byname_optional(pdev, "g_int");
1862 		if (g_irq < 0) {
1863 			/* For backward compatibility get irq by index */
1864 			g_irq = platform_get_irq(pdev, 1);
1865 			if (g_irq < 0)
1866 				return g_irq;
1867 		}
1868 	} else {
1869 		g_err_irq = platform_get_irq_byname(pdev, "g_err");
1870 		if (g_err_irq < 0)
1871 			return g_err_irq;
1872 
1873 		g_recc_irq = platform_get_irq_byname(pdev, "g_recc");
1874 		if (g_recc_irq < 0)
1875 			return g_recc_irq;
1876 	}
1877 
1878 	/* Global controller context */
1879 	gpriv = devm_kzalloc(&pdev->dev, sizeof(*gpriv), GFP_KERNEL);
1880 	if (!gpriv) {
1881 		err = -ENOMEM;
1882 		goto fail_dev;
1883 	}
1884 	gpriv->pdev = pdev;
1885 	gpriv->channels_mask = channels_mask;
1886 	gpriv->fdmode = fdmode;
1887 	gpriv->chip_id = chip_id;
1888 	gpriv->max_channels = max_channels;
1889 
1890 	if (gpriv->chip_id == RENESAS_RZG2L) {
1891 		gpriv->rstc1 = devm_reset_control_get_exclusive(&pdev->dev, "rstp_n");
1892 		if (IS_ERR(gpriv->rstc1))
1893 			return dev_err_probe(&pdev->dev, PTR_ERR(gpriv->rstc1),
1894 					     "failed to get rstp_n\n");
1895 
1896 		gpriv->rstc2 = devm_reset_control_get_exclusive(&pdev->dev, "rstc_n");
1897 		if (IS_ERR(gpriv->rstc2))
1898 			return dev_err_probe(&pdev->dev, PTR_ERR(gpriv->rstc2),
1899 					     "failed to get rstc_n\n");
1900 	}
1901 
1902 	/* Peripheral clock */
1903 	gpriv->clkp = devm_clk_get(&pdev->dev, "fck");
1904 	if (IS_ERR(gpriv->clkp)) {
1905 		err = PTR_ERR(gpriv->clkp);
1906 		dev_err(&pdev->dev, "cannot get peripheral clock, error %d\n",
1907 			err);
1908 		goto fail_dev;
1909 	}
1910 
1911 	/* fCAN clock: Pick External clock. If not available fallback to
1912 	 * CANFD clock
1913 	 */
1914 	gpriv->can_clk = devm_clk_get(&pdev->dev, "can_clk");
1915 	if (IS_ERR(gpriv->can_clk) || (clk_get_rate(gpriv->can_clk) == 0)) {
1916 		gpriv->can_clk = devm_clk_get(&pdev->dev, "canfd");
1917 		if (IS_ERR(gpriv->can_clk)) {
1918 			err = PTR_ERR(gpriv->can_clk);
1919 			dev_err(&pdev->dev,
1920 				"cannot get canfd clock, error %d\n", err);
1921 			goto fail_dev;
1922 		}
1923 		gpriv->fcan = RCANFD_CANFDCLK;
1924 
1925 	} else {
1926 		gpriv->fcan = RCANFD_EXTCLK;
1927 	}
1928 	fcan_freq = clk_get_rate(gpriv->can_clk);
1929 
1930 	if (gpriv->fcan == RCANFD_CANFDCLK && gpriv->chip_id != RENESAS_RZG2L)
1931 		/* CANFD clock is further divided by (1/2) within the IP */
1932 		fcan_freq /= 2;
1933 
1934 	addr = devm_platform_ioremap_resource(pdev, 0);
1935 	if (IS_ERR(addr)) {
1936 		err = PTR_ERR(addr);
1937 		goto fail_dev;
1938 	}
1939 	gpriv->base = addr;
1940 
1941 	/* Request IRQ that's common for both channels */
1942 	if (gpriv->chip_id != RENESAS_RZG2L) {
1943 		err = devm_request_irq(&pdev->dev, ch_irq,
1944 				       rcar_canfd_channel_interrupt, 0,
1945 				       "canfd.ch_int", gpriv);
1946 		if (err) {
1947 			dev_err(&pdev->dev, "devm_request_irq(%d) failed, error %d\n",
1948 				ch_irq, err);
1949 			goto fail_dev;
1950 		}
1951 
1952 		err = devm_request_irq(&pdev->dev, g_irq,
1953 				       rcar_canfd_global_interrupt, 0,
1954 				       "canfd.g_int", gpriv);
1955 		if (err) {
1956 			dev_err(&pdev->dev, "devm_request_irq(%d) failed, error %d\n",
1957 				g_irq, err);
1958 			goto fail_dev;
1959 		}
1960 	} else {
1961 		err = devm_request_irq(&pdev->dev, g_recc_irq,
1962 				       rcar_canfd_global_receive_fifo_interrupt, 0,
1963 				       "canfd.g_recc", gpriv);
1964 
1965 		if (err) {
1966 			dev_err(&pdev->dev, "devm_request_irq(%d) failed, error %d\n",
1967 				g_recc_irq, err);
1968 			goto fail_dev;
1969 		}
1970 
1971 		err = devm_request_irq(&pdev->dev, g_err_irq,
1972 				       rcar_canfd_global_err_interrupt, 0,
1973 				       "canfd.g_err", gpriv);
1974 		if (err) {
1975 			dev_err(&pdev->dev, "devm_request_irq(%d) failed, error %d\n",
1976 				g_err_irq, err);
1977 			goto fail_dev;
1978 		}
1979 	}
1980 
1981 	err = reset_control_reset(gpriv->rstc1);
1982 	if (err)
1983 		goto fail_dev;
1984 	err = reset_control_reset(gpriv->rstc2);
1985 	if (err) {
1986 		reset_control_assert(gpriv->rstc1);
1987 		goto fail_dev;
1988 	}
1989 
1990 	/* Enable peripheral clock for register access */
1991 	err = clk_prepare_enable(gpriv->clkp);
1992 	if (err) {
1993 		dev_err(&pdev->dev,
1994 			"failed to enable peripheral clock, error %d\n", err);
1995 		goto fail_reset;
1996 	}
1997 
1998 	err = rcar_canfd_reset_controller(gpriv);
1999 	if (err) {
2000 		dev_err(&pdev->dev, "reset controller failed\n");
2001 		goto fail_clk;
2002 	}
2003 
2004 	/* Controller in Global reset & Channel reset mode */
2005 	rcar_canfd_configure_controller(gpriv);
2006 
2007 	/* Configure per channel attributes */
2008 	for_each_set_bit(ch, &gpriv->channels_mask, max_channels) {
2009 		/* Configure Channel's Rx fifo */
2010 		rcar_canfd_configure_rx(gpriv, ch);
2011 
2012 		/* Configure Channel's Tx (Common) fifo */
2013 		rcar_canfd_configure_tx(gpriv, ch);
2014 
2015 		/* Configure receive rules */
2016 		rcar_canfd_configure_afl_rules(gpriv, ch);
2017 	}
2018 
2019 	/* Configure common interrupts */
2020 	rcar_canfd_enable_global_interrupts(gpriv);
2021 
2022 	/* Start Global operation mode */
2023 	rcar_canfd_update_bit(gpriv->base, RCANFD_GCTR, RCANFD_GCTR_GMDC_MASK,
2024 			      RCANFD_GCTR_GMDC_GOPM);
2025 
2026 	/* Verify mode change */
2027 	err = readl_poll_timeout((gpriv->base + RCANFD_GSTS), sts,
2028 				 !(sts & RCANFD_GSTS_GNOPM), 2, 500000);
2029 	if (err) {
2030 		dev_err(&pdev->dev, "global operational mode failed\n");
2031 		goto fail_mode;
2032 	}
2033 
2034 	for_each_set_bit(ch, &gpriv->channels_mask, max_channels) {
2035 		err = rcar_canfd_channel_probe(gpriv, ch, fcan_freq);
2036 		if (err)
2037 			goto fail_channel;
2038 	}
2039 
2040 	platform_set_drvdata(pdev, gpriv);
2041 	dev_info(&pdev->dev, "global operational state (clk %d, fdmode %d)\n",
2042 		 gpriv->fcan, gpriv->fdmode);
2043 	return 0;
2044 
2045 fail_channel:
2046 	for_each_set_bit(ch, &gpriv->channels_mask, max_channels)
2047 		rcar_canfd_channel_remove(gpriv, ch);
2048 fail_mode:
2049 	rcar_canfd_disable_global_interrupts(gpriv);
2050 fail_clk:
2051 	clk_disable_unprepare(gpriv->clkp);
2052 fail_reset:
2053 	reset_control_assert(gpriv->rstc1);
2054 	reset_control_assert(gpriv->rstc2);
2055 fail_dev:
2056 	return err;
2057 }
2058 
2059 static int rcar_canfd_remove(struct platform_device *pdev)
2060 {
2061 	struct rcar_canfd_global *gpriv = platform_get_drvdata(pdev);
2062 	u32 ch;
2063 
2064 	rcar_canfd_reset_controller(gpriv);
2065 	rcar_canfd_disable_global_interrupts(gpriv);
2066 
2067 	for_each_set_bit(ch, &gpriv->channels_mask, gpriv->max_channels) {
2068 		rcar_canfd_disable_channel_interrupts(gpriv->ch[ch]);
2069 		rcar_canfd_channel_remove(gpriv, ch);
2070 	}
2071 
2072 	/* Enter global sleep mode */
2073 	rcar_canfd_set_bit(gpriv->base, RCANFD_GCTR, RCANFD_GCTR_GSLPR);
2074 	clk_disable_unprepare(gpriv->clkp);
2075 	reset_control_assert(gpriv->rstc1);
2076 	reset_control_assert(gpriv->rstc2);
2077 
2078 	return 0;
2079 }
2080 
2081 static int __maybe_unused rcar_canfd_suspend(struct device *dev)
2082 {
2083 	return 0;
2084 }
2085 
2086 static int __maybe_unused rcar_canfd_resume(struct device *dev)
2087 {
2088 	return 0;
2089 }
2090 
2091 static SIMPLE_DEV_PM_OPS(rcar_canfd_pm_ops, rcar_canfd_suspend,
2092 			 rcar_canfd_resume);
2093 
2094 static const __maybe_unused struct of_device_id rcar_canfd_of_table[] = {
2095 	{ .compatible = "renesas,rcar-gen3-canfd", .data = (void *)RENESAS_RCAR_GEN3 },
2096 	{ .compatible = "renesas,rzg2l-canfd", .data = (void *)RENESAS_RZG2L },
2097 	{ .compatible = "renesas,r8a779a0-canfd", .data = (void *)RENESAS_R8A779A0 },
2098 	{ }
2099 };
2100 
2101 MODULE_DEVICE_TABLE(of, rcar_canfd_of_table);
2102 
2103 static struct platform_driver rcar_canfd_driver = {
2104 	.driver = {
2105 		.name = RCANFD_DRV_NAME,
2106 		.of_match_table = of_match_ptr(rcar_canfd_of_table),
2107 		.pm = &rcar_canfd_pm_ops,
2108 	},
2109 	.probe = rcar_canfd_probe,
2110 	.remove = rcar_canfd_remove,
2111 };
2112 
2113 module_platform_driver(rcar_canfd_driver);
2114 
2115 MODULE_AUTHOR("Ramesh Shanmugasundaram <ramesh.shanmugasundaram@bp.renesas.com>");
2116 MODULE_LICENSE("GPL");
2117 MODULE_DESCRIPTION("CAN FD driver for Renesas R-Car SoC");
2118 MODULE_ALIAS("platform:" RCANFD_DRV_NAME);
2119