xref: /openbmc/linux/drivers/net/can/rcar/rcar_canfd.c (revision f59a3ee6)
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/ethtool.h>
31 #include <linux/netdevice.h>
32 #include <linux/platform_device.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 | CAN_ERR_CNT;
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 | CAN_ERR_CNT;
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 }
1132 
1133 static void rcar_canfd_handle_global_err(struct rcar_canfd_global *gpriv, u32 ch)
1134 {
1135 	struct rcar_canfd_channel *priv = gpriv->ch[ch];
1136 	struct net_device *ndev = priv->ndev;
1137 	u32 gerfl;
1138 
1139 	/* Handle global error interrupts */
1140 	gerfl = rcar_canfd_read(priv->base, RCANFD_GERFL);
1141 	if (unlikely(RCANFD_GERFL_ERR(gpriv, gerfl)))
1142 		rcar_canfd_global_error(ndev);
1143 }
1144 
1145 static irqreturn_t rcar_canfd_global_err_interrupt(int irq, void *dev_id)
1146 {
1147 	struct rcar_canfd_global *gpriv = dev_id;
1148 	u32 ch;
1149 
1150 	for_each_set_bit(ch, &gpriv->channels_mask, gpriv->max_channels)
1151 		rcar_canfd_handle_global_err(gpriv, ch);
1152 
1153 	return IRQ_HANDLED;
1154 }
1155 
1156 static void rcar_canfd_handle_global_receive(struct rcar_canfd_global *gpriv, u32 ch)
1157 {
1158 	struct rcar_canfd_channel *priv = gpriv->ch[ch];
1159 	u32 ridx = ch + RCANFD_RFFIFO_IDX;
1160 	u32 sts;
1161 
1162 	/* Handle Rx interrupts */
1163 	sts = rcar_canfd_read(priv->base, RCANFD_RFSTS(gpriv, ridx));
1164 	if (likely(sts & RCANFD_RFSTS_RFIF)) {
1165 		if (napi_schedule_prep(&priv->napi)) {
1166 			/* Disable Rx FIFO interrupts */
1167 			rcar_canfd_clear_bit(priv->base,
1168 					     RCANFD_RFCC(gpriv, ridx),
1169 					     RCANFD_RFCC_RFIE);
1170 			__napi_schedule(&priv->napi);
1171 		}
1172 	}
1173 }
1174 
1175 static irqreturn_t rcar_canfd_global_receive_fifo_interrupt(int irq, void *dev_id)
1176 {
1177 	struct rcar_canfd_global *gpriv = dev_id;
1178 	u32 ch;
1179 
1180 	for_each_set_bit(ch, &gpriv->channels_mask, gpriv->max_channels)
1181 		rcar_canfd_handle_global_receive(gpriv, ch);
1182 
1183 	return IRQ_HANDLED;
1184 }
1185 
1186 static irqreturn_t rcar_canfd_global_interrupt(int irq, void *dev_id)
1187 {
1188 	struct rcar_canfd_global *gpriv = dev_id;
1189 	u32 ch;
1190 
1191 	/* Global error interrupts still indicate a condition specific
1192 	 * to a channel. RxFIFO interrupt is a global interrupt.
1193 	 */
1194 	for_each_set_bit(ch, &gpriv->channels_mask, gpriv->max_channels) {
1195 		rcar_canfd_handle_global_err(gpriv, ch);
1196 		rcar_canfd_handle_global_receive(gpriv, ch);
1197 	}
1198 	return IRQ_HANDLED;
1199 }
1200 
1201 static void rcar_canfd_state_change(struct net_device *ndev,
1202 				    u16 txerr, u16 rxerr)
1203 {
1204 	struct rcar_canfd_channel *priv = netdev_priv(ndev);
1205 	struct net_device_stats *stats = &ndev->stats;
1206 	enum can_state rx_state, tx_state, state = priv->can.state;
1207 	struct can_frame *cf;
1208 	struct sk_buff *skb;
1209 
1210 	/* Handle transition from error to normal states */
1211 	if (txerr < 96 && rxerr < 96)
1212 		state = CAN_STATE_ERROR_ACTIVE;
1213 	else if (txerr < 128 && rxerr < 128)
1214 		state = CAN_STATE_ERROR_WARNING;
1215 
1216 	if (state != priv->can.state) {
1217 		netdev_dbg(ndev, "state: new %d, old %d: txerr %u, rxerr %u\n",
1218 			   state, priv->can.state, txerr, rxerr);
1219 		skb = alloc_can_err_skb(ndev, &cf);
1220 		if (!skb) {
1221 			stats->rx_dropped++;
1222 			return;
1223 		}
1224 		tx_state = txerr >= rxerr ? state : 0;
1225 		rx_state = txerr <= rxerr ? state : 0;
1226 
1227 		can_change_state(ndev, cf, tx_state, rx_state);
1228 		netif_rx(skb);
1229 	}
1230 }
1231 
1232 static void rcar_canfd_handle_channel_tx(struct rcar_canfd_global *gpriv, u32 ch)
1233 {
1234 	struct rcar_canfd_channel *priv = gpriv->ch[ch];
1235 	struct net_device *ndev = priv->ndev;
1236 	u32 sts;
1237 
1238 	/* Handle Tx interrupts */
1239 	sts = rcar_canfd_read(priv->base,
1240 			      RCANFD_CFSTS(gpriv, ch, RCANFD_CFFIFO_IDX));
1241 	if (likely(sts & RCANFD_CFSTS_CFTXIF))
1242 		rcar_canfd_tx_done(ndev);
1243 }
1244 
1245 static irqreturn_t rcar_canfd_channel_tx_interrupt(int irq, void *dev_id)
1246 {
1247 	struct rcar_canfd_global *gpriv = dev_id;
1248 	u32 ch;
1249 
1250 	for_each_set_bit(ch, &gpriv->channels_mask, gpriv->max_channels)
1251 		rcar_canfd_handle_channel_tx(gpriv, ch);
1252 
1253 	return IRQ_HANDLED;
1254 }
1255 
1256 static void rcar_canfd_handle_channel_err(struct rcar_canfd_global *gpriv, u32 ch)
1257 {
1258 	struct rcar_canfd_channel *priv = gpriv->ch[ch];
1259 	struct net_device *ndev = priv->ndev;
1260 	u16 txerr, rxerr;
1261 	u32 sts, cerfl;
1262 
1263 	/* Handle channel error interrupts */
1264 	cerfl = rcar_canfd_read(priv->base, RCANFD_CERFL(ch));
1265 	sts = rcar_canfd_read(priv->base, RCANFD_CSTS(ch));
1266 	txerr = RCANFD_CSTS_TECCNT(sts);
1267 	rxerr = RCANFD_CSTS_RECCNT(sts);
1268 	if (unlikely(RCANFD_CERFL_ERR(cerfl)))
1269 		rcar_canfd_error(ndev, cerfl, txerr, rxerr);
1270 
1271 	/* Handle state change to lower states */
1272 	if (unlikely(priv->can.state != CAN_STATE_ERROR_ACTIVE &&
1273 		     priv->can.state != CAN_STATE_BUS_OFF))
1274 		rcar_canfd_state_change(ndev, txerr, rxerr);
1275 }
1276 
1277 static irqreturn_t rcar_canfd_channel_err_interrupt(int irq, void *dev_id)
1278 {
1279 	struct rcar_canfd_global *gpriv = dev_id;
1280 	u32 ch;
1281 
1282 	for_each_set_bit(ch, &gpriv->channels_mask, gpriv->max_channels)
1283 		rcar_canfd_handle_channel_err(gpriv, ch);
1284 
1285 	return IRQ_HANDLED;
1286 }
1287 
1288 static irqreturn_t rcar_canfd_channel_interrupt(int irq, void *dev_id)
1289 {
1290 	struct rcar_canfd_global *gpriv = dev_id;
1291 	u32 ch;
1292 
1293 	/* Common FIFO is a per channel resource */
1294 	for_each_set_bit(ch, &gpriv->channels_mask, gpriv->max_channels) {
1295 		rcar_canfd_handle_channel_err(gpriv, ch);
1296 		rcar_canfd_handle_channel_tx(gpriv, ch);
1297 	}
1298 
1299 	return IRQ_HANDLED;
1300 }
1301 
1302 static void rcar_canfd_set_bittiming(struct net_device *dev)
1303 {
1304 	struct rcar_canfd_channel *priv = netdev_priv(dev);
1305 	struct rcar_canfd_global *gpriv = priv->gpriv;
1306 	const struct can_bittiming *bt = &priv->can.bittiming;
1307 	const struct can_bittiming *dbt = &priv->can.data_bittiming;
1308 	u16 brp, sjw, tseg1, tseg2;
1309 	u32 cfg;
1310 	u32 ch = priv->channel;
1311 
1312 	/* Nominal bit timing settings */
1313 	brp = bt->brp - 1;
1314 	sjw = bt->sjw - 1;
1315 	tseg1 = bt->prop_seg + bt->phase_seg1 - 1;
1316 	tseg2 = bt->phase_seg2 - 1;
1317 
1318 	if (priv->can.ctrlmode & CAN_CTRLMODE_FD) {
1319 		/* CAN FD only mode */
1320 		cfg = (RCANFD_NCFG_NTSEG1(gpriv, tseg1) | RCANFD_NCFG_NBRP(brp) |
1321 		       RCANFD_NCFG_NSJW(gpriv, sjw) | RCANFD_NCFG_NTSEG2(gpriv, tseg2));
1322 
1323 		rcar_canfd_write(priv->base, RCANFD_CCFG(ch), cfg);
1324 		netdev_dbg(priv->ndev, "nrate: brp %u, sjw %u, tseg1 %u, tseg2 %u\n",
1325 			   brp, sjw, tseg1, tseg2);
1326 
1327 		/* Data bit timing settings */
1328 		brp = dbt->brp - 1;
1329 		sjw = dbt->sjw - 1;
1330 		tseg1 = dbt->prop_seg + dbt->phase_seg1 - 1;
1331 		tseg2 = dbt->phase_seg2 - 1;
1332 
1333 		cfg = (RCANFD_DCFG_DTSEG1(gpriv, tseg1) | RCANFD_DCFG_DBRP(brp) |
1334 		       RCANFD_DCFG_DSJW(sjw) | RCANFD_DCFG_DTSEG2(gpriv, tseg2));
1335 
1336 		if (is_v3u(gpriv))
1337 			rcar_canfd_write(priv->base, RCANFD_V3U_DCFG(ch), cfg);
1338 		else
1339 			rcar_canfd_write(priv->base, RCANFD_F_DCFG(ch), cfg);
1340 		netdev_dbg(priv->ndev, "drate: brp %u, sjw %u, tseg1 %u, tseg2 %u\n",
1341 			   brp, sjw, tseg1, tseg2);
1342 	} else {
1343 		/* Classical CAN only mode */
1344 		if (is_v3u(gpriv)) {
1345 			cfg = (RCANFD_NCFG_NTSEG1(gpriv, tseg1) |
1346 			       RCANFD_NCFG_NBRP(brp) |
1347 			       RCANFD_NCFG_NSJW(gpriv, sjw) |
1348 			       RCANFD_NCFG_NTSEG2(gpriv, tseg2));
1349 		} else {
1350 			cfg = (RCANFD_CFG_TSEG1(tseg1) |
1351 			       RCANFD_CFG_BRP(brp) |
1352 			       RCANFD_CFG_SJW(sjw) |
1353 			       RCANFD_CFG_TSEG2(tseg2));
1354 		}
1355 
1356 		rcar_canfd_write(priv->base, RCANFD_CCFG(ch), cfg);
1357 		netdev_dbg(priv->ndev,
1358 			   "rate: brp %u, sjw %u, tseg1 %u, tseg2 %u\n",
1359 			   brp, sjw, tseg1, tseg2);
1360 	}
1361 }
1362 
1363 static int rcar_canfd_start(struct net_device *ndev)
1364 {
1365 	struct rcar_canfd_channel *priv = netdev_priv(ndev);
1366 	struct rcar_canfd_global *gpriv = priv->gpriv;
1367 	int err = -EOPNOTSUPP;
1368 	u32 sts, ch = priv->channel;
1369 	u32 ridx = ch + RCANFD_RFFIFO_IDX;
1370 
1371 	rcar_canfd_set_bittiming(ndev);
1372 
1373 	rcar_canfd_enable_channel_interrupts(priv);
1374 
1375 	/* Set channel to Operational mode */
1376 	rcar_canfd_update_bit(priv->base, RCANFD_CCTR(ch),
1377 			      RCANFD_CCTR_CHMDC_MASK, RCANFD_CCTR_CHDMC_COPM);
1378 
1379 	/* Verify channel mode change */
1380 	err = readl_poll_timeout((priv->base + RCANFD_CSTS(ch)), sts,
1381 				 (sts & RCANFD_CSTS_COMSTS), 2, 500000);
1382 	if (err) {
1383 		netdev_err(ndev, "channel %u communication state failed\n", ch);
1384 		goto fail_mode_change;
1385 	}
1386 
1387 	/* Enable Common & Rx FIFO */
1388 	rcar_canfd_set_bit(priv->base, RCANFD_CFCC(gpriv, ch, RCANFD_CFFIFO_IDX),
1389 			   RCANFD_CFCC_CFE);
1390 	rcar_canfd_set_bit(priv->base, RCANFD_RFCC(gpriv, ridx), RCANFD_RFCC_RFE);
1391 
1392 	priv->can.state = CAN_STATE_ERROR_ACTIVE;
1393 	return 0;
1394 
1395 fail_mode_change:
1396 	rcar_canfd_disable_channel_interrupts(priv);
1397 	return err;
1398 }
1399 
1400 static int rcar_canfd_open(struct net_device *ndev)
1401 {
1402 	struct rcar_canfd_channel *priv = netdev_priv(ndev);
1403 	struct rcar_canfd_global *gpriv = priv->gpriv;
1404 	int err;
1405 
1406 	/* Peripheral clock is already enabled in probe */
1407 	err = clk_prepare_enable(gpriv->can_clk);
1408 	if (err) {
1409 		netdev_err(ndev, "failed to enable CAN clock, error %d\n", err);
1410 		goto out_clock;
1411 	}
1412 
1413 	err = open_candev(ndev);
1414 	if (err) {
1415 		netdev_err(ndev, "open_candev() failed, error %d\n", err);
1416 		goto out_can_clock;
1417 	}
1418 
1419 	napi_enable(&priv->napi);
1420 	err = rcar_canfd_start(ndev);
1421 	if (err)
1422 		goto out_close;
1423 	netif_start_queue(ndev);
1424 	return 0;
1425 out_close:
1426 	napi_disable(&priv->napi);
1427 	close_candev(ndev);
1428 out_can_clock:
1429 	clk_disable_unprepare(gpriv->can_clk);
1430 out_clock:
1431 	return err;
1432 }
1433 
1434 static void rcar_canfd_stop(struct net_device *ndev)
1435 {
1436 	struct rcar_canfd_channel *priv = netdev_priv(ndev);
1437 	struct rcar_canfd_global *gpriv = priv->gpriv;
1438 	int err;
1439 	u32 sts, ch = priv->channel;
1440 	u32 ridx = ch + RCANFD_RFFIFO_IDX;
1441 
1442 	/* Transition to channel reset mode  */
1443 	rcar_canfd_update_bit(priv->base, RCANFD_CCTR(ch),
1444 			      RCANFD_CCTR_CHMDC_MASK, RCANFD_CCTR_CHDMC_CRESET);
1445 
1446 	/* Check Channel reset mode */
1447 	err = readl_poll_timeout((priv->base + RCANFD_CSTS(ch)), sts,
1448 				 (sts & RCANFD_CSTS_CRSTSTS), 2, 500000);
1449 	if (err)
1450 		netdev_err(ndev, "channel %u reset failed\n", ch);
1451 
1452 	rcar_canfd_disable_channel_interrupts(priv);
1453 
1454 	/* Disable Common & Rx FIFO */
1455 	rcar_canfd_clear_bit(priv->base, RCANFD_CFCC(gpriv, ch, RCANFD_CFFIFO_IDX),
1456 			     RCANFD_CFCC_CFE);
1457 	rcar_canfd_clear_bit(priv->base, RCANFD_RFCC(gpriv, ridx), RCANFD_RFCC_RFE);
1458 
1459 	/* Set the state as STOPPED */
1460 	priv->can.state = CAN_STATE_STOPPED;
1461 }
1462 
1463 static int rcar_canfd_close(struct net_device *ndev)
1464 {
1465 	struct rcar_canfd_channel *priv = netdev_priv(ndev);
1466 	struct rcar_canfd_global *gpriv = priv->gpriv;
1467 
1468 	netif_stop_queue(ndev);
1469 	rcar_canfd_stop(ndev);
1470 	napi_disable(&priv->napi);
1471 	clk_disable_unprepare(gpriv->can_clk);
1472 	close_candev(ndev);
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 	if (!(cf->can_id & CAN_RTR_FLAG))
1623 		stats->rx_bytes += cf->len;
1624 	stats->rx_packets++;
1625 	netif_receive_skb(skb);
1626 }
1627 
1628 static int rcar_canfd_rx_poll(struct napi_struct *napi, int quota)
1629 {
1630 	struct rcar_canfd_channel *priv =
1631 		container_of(napi, struct rcar_canfd_channel, napi);
1632 	struct rcar_canfd_global *gpriv = priv->gpriv;
1633 	int num_pkts;
1634 	u32 sts;
1635 	u32 ch = priv->channel;
1636 	u32 ridx = ch + RCANFD_RFFIFO_IDX;
1637 
1638 	for (num_pkts = 0; num_pkts < quota; num_pkts++) {
1639 		sts = rcar_canfd_read(priv->base, RCANFD_RFSTS(gpriv, ridx));
1640 		/* Check FIFO empty condition */
1641 		if (sts & RCANFD_RFSTS_RFEMP)
1642 			break;
1643 
1644 		rcar_canfd_rx_pkt(priv);
1645 
1646 		/* Clear interrupt bit */
1647 		if (sts & RCANFD_RFSTS_RFIF)
1648 			rcar_canfd_write(priv->base, RCANFD_RFSTS(gpriv, ridx),
1649 					 sts & ~RCANFD_RFSTS_RFIF);
1650 	}
1651 
1652 	/* All packets processed */
1653 	if (num_pkts < quota) {
1654 		if (napi_complete_done(napi, num_pkts)) {
1655 			/* Enable Rx FIFO interrupts */
1656 			rcar_canfd_set_bit(priv->base, RCANFD_RFCC(gpriv, ridx),
1657 					   RCANFD_RFCC_RFIE);
1658 		}
1659 	}
1660 	return num_pkts;
1661 }
1662 
1663 static int rcar_canfd_do_set_mode(struct net_device *ndev, enum can_mode mode)
1664 {
1665 	int err;
1666 
1667 	switch (mode) {
1668 	case CAN_MODE_START:
1669 		err = rcar_canfd_start(ndev);
1670 		if (err)
1671 			return err;
1672 		netif_wake_queue(ndev);
1673 		return 0;
1674 	default:
1675 		return -EOPNOTSUPP;
1676 	}
1677 }
1678 
1679 static int rcar_canfd_get_berr_counter(const struct net_device *dev,
1680 				       struct can_berr_counter *bec)
1681 {
1682 	struct rcar_canfd_channel *priv = netdev_priv(dev);
1683 	u32 val, ch = priv->channel;
1684 
1685 	/* Peripheral clock is already enabled in probe */
1686 	val = rcar_canfd_read(priv->base, RCANFD_CSTS(ch));
1687 	bec->txerr = RCANFD_CSTS_TECCNT(val);
1688 	bec->rxerr = RCANFD_CSTS_RECCNT(val);
1689 	return 0;
1690 }
1691 
1692 static const struct net_device_ops rcar_canfd_netdev_ops = {
1693 	.ndo_open = rcar_canfd_open,
1694 	.ndo_stop = rcar_canfd_close,
1695 	.ndo_start_xmit = rcar_canfd_start_xmit,
1696 	.ndo_change_mtu = can_change_mtu,
1697 };
1698 
1699 static const struct ethtool_ops rcar_canfd_ethtool_ops = {
1700 	.get_ts_info = ethtool_op_get_ts_info,
1701 };
1702 
1703 static int rcar_canfd_channel_probe(struct rcar_canfd_global *gpriv, u32 ch,
1704 				    u32 fcan_freq)
1705 {
1706 	struct platform_device *pdev = gpriv->pdev;
1707 	struct rcar_canfd_channel *priv;
1708 	struct net_device *ndev;
1709 	int err = -ENODEV;
1710 
1711 	ndev = alloc_candev(sizeof(*priv), RCANFD_FIFO_DEPTH);
1712 	if (!ndev) {
1713 		dev_err(&pdev->dev, "alloc_candev() failed\n");
1714 		return -ENOMEM;
1715 	}
1716 	priv = netdev_priv(ndev);
1717 
1718 	ndev->netdev_ops = &rcar_canfd_netdev_ops;
1719 	ndev->ethtool_ops = &rcar_canfd_ethtool_ops;
1720 	ndev->flags |= IFF_ECHO;
1721 	priv->ndev = ndev;
1722 	priv->base = gpriv->base;
1723 	priv->channel = ch;
1724 	priv->can.clock.freq = fcan_freq;
1725 	dev_info(&pdev->dev, "can_clk rate is %u\n", priv->can.clock.freq);
1726 
1727 	if (gpriv->chip_id == RENESAS_RZG2L) {
1728 		char *irq_name;
1729 		int err_irq;
1730 		int tx_irq;
1731 
1732 		err_irq = platform_get_irq_byname(pdev, ch == 0 ? "ch0_err" : "ch1_err");
1733 		if (err_irq < 0) {
1734 			err = err_irq;
1735 			goto fail;
1736 		}
1737 
1738 		tx_irq = platform_get_irq_byname(pdev, ch == 0 ? "ch0_trx" : "ch1_trx");
1739 		if (tx_irq < 0) {
1740 			err = tx_irq;
1741 			goto fail;
1742 		}
1743 
1744 		irq_name = devm_kasprintf(&pdev->dev, GFP_KERNEL,
1745 					  "canfd.ch%d_err", ch);
1746 		if (!irq_name) {
1747 			err = -ENOMEM;
1748 			goto fail;
1749 		}
1750 		err = devm_request_irq(&pdev->dev, err_irq,
1751 				       rcar_canfd_channel_err_interrupt, 0,
1752 				       irq_name, gpriv);
1753 		if (err) {
1754 			dev_err(&pdev->dev, "devm_request_irq CH Err(%d) failed, error %d\n",
1755 				err_irq, err);
1756 			goto fail;
1757 		}
1758 		irq_name = devm_kasprintf(&pdev->dev, GFP_KERNEL,
1759 					  "canfd.ch%d_trx", ch);
1760 		if (!irq_name) {
1761 			err = -ENOMEM;
1762 			goto fail;
1763 		}
1764 		err = devm_request_irq(&pdev->dev, tx_irq,
1765 				       rcar_canfd_channel_tx_interrupt, 0,
1766 				       irq_name, gpriv);
1767 		if (err) {
1768 			dev_err(&pdev->dev, "devm_request_irq Tx (%d) failed, error %d\n",
1769 				tx_irq, err);
1770 			goto fail;
1771 		}
1772 	}
1773 
1774 	if (gpriv->fdmode) {
1775 		priv->can.bittiming_const = &rcar_canfd_nom_bittiming_const;
1776 		priv->can.data_bittiming_const =
1777 			&rcar_canfd_data_bittiming_const;
1778 
1779 		/* Controller starts in CAN FD only mode */
1780 		err = can_set_static_ctrlmode(ndev, CAN_CTRLMODE_FD);
1781 		if (err)
1782 			goto fail;
1783 		priv->can.ctrlmode_supported = CAN_CTRLMODE_BERR_REPORTING;
1784 	} else {
1785 		/* Controller starts in Classical CAN only mode */
1786 		priv->can.bittiming_const = &rcar_canfd_bittiming_const;
1787 		priv->can.ctrlmode_supported = CAN_CTRLMODE_BERR_REPORTING;
1788 	}
1789 
1790 	priv->can.do_set_mode = rcar_canfd_do_set_mode;
1791 	priv->can.do_get_berr_counter = rcar_canfd_get_berr_counter;
1792 	priv->gpriv = gpriv;
1793 	SET_NETDEV_DEV(ndev, &pdev->dev);
1794 
1795 	netif_napi_add_weight(ndev, &priv->napi, rcar_canfd_rx_poll,
1796 			      RCANFD_NAPI_WEIGHT);
1797 	spin_lock_init(&priv->tx_lock);
1798 	gpriv->ch[priv->channel] = priv;
1799 	err = register_candev(ndev);
1800 	if (err) {
1801 		dev_err(&pdev->dev,
1802 			"register_candev() failed, error %d\n", err);
1803 		goto fail_candev;
1804 	}
1805 	dev_info(&pdev->dev, "device registered (channel %u)\n", priv->channel);
1806 	return 0;
1807 
1808 fail_candev:
1809 	netif_napi_del(&priv->napi);
1810 fail:
1811 	free_candev(ndev);
1812 	return err;
1813 }
1814 
1815 static void rcar_canfd_channel_remove(struct rcar_canfd_global *gpriv, u32 ch)
1816 {
1817 	struct rcar_canfd_channel *priv = gpriv->ch[ch];
1818 
1819 	if (priv) {
1820 		unregister_candev(priv->ndev);
1821 		netif_napi_del(&priv->napi);
1822 		free_candev(priv->ndev);
1823 	}
1824 }
1825 
1826 static int rcar_canfd_probe(struct platform_device *pdev)
1827 {
1828 	void __iomem *addr;
1829 	u32 sts, ch, fcan_freq;
1830 	struct rcar_canfd_global *gpriv;
1831 	struct device_node *of_child;
1832 	unsigned long channels_mask = 0;
1833 	int err, ch_irq, g_irq;
1834 	int g_err_irq, g_recc_irq;
1835 	bool fdmode = true;			/* CAN FD only mode - default */
1836 	enum rcanfd_chip_id chip_id;
1837 	int max_channels;
1838 	char name[9] = "channelX";
1839 	int i;
1840 
1841 	chip_id = (uintptr_t)of_device_get_match_data(&pdev->dev);
1842 	max_channels = chip_id == RENESAS_R8A779A0 ? 8 : 2;
1843 
1844 	if (of_property_read_bool(pdev->dev.of_node, "renesas,no-can-fd"))
1845 		fdmode = false;			/* Classical CAN only mode */
1846 
1847 	for (i = 0; i < max_channels; ++i) {
1848 		name[7] = '0' + i;
1849 		of_child = of_get_child_by_name(pdev->dev.of_node, name);
1850 		if (of_child && of_device_is_available(of_child))
1851 			channels_mask |= BIT(i);
1852 		of_node_put(of_child);
1853 	}
1854 
1855 	if (chip_id != RENESAS_RZG2L) {
1856 		ch_irq = platform_get_irq_byname_optional(pdev, "ch_int");
1857 		if (ch_irq < 0) {
1858 			/* For backward compatibility get irq by index */
1859 			ch_irq = platform_get_irq(pdev, 0);
1860 			if (ch_irq < 0)
1861 				return ch_irq;
1862 		}
1863 
1864 		g_irq = platform_get_irq_byname_optional(pdev, "g_int");
1865 		if (g_irq < 0) {
1866 			/* For backward compatibility get irq by index */
1867 			g_irq = platform_get_irq(pdev, 1);
1868 			if (g_irq < 0)
1869 				return g_irq;
1870 		}
1871 	} else {
1872 		g_err_irq = platform_get_irq_byname(pdev, "g_err");
1873 		if (g_err_irq < 0)
1874 			return g_err_irq;
1875 
1876 		g_recc_irq = platform_get_irq_byname(pdev, "g_recc");
1877 		if (g_recc_irq < 0)
1878 			return g_recc_irq;
1879 	}
1880 
1881 	/* Global controller context */
1882 	gpriv = devm_kzalloc(&pdev->dev, sizeof(*gpriv), GFP_KERNEL);
1883 	if (!gpriv)
1884 		return -ENOMEM;
1885 
1886 	gpriv->pdev = pdev;
1887 	gpriv->channels_mask = channels_mask;
1888 	gpriv->fdmode = fdmode;
1889 	gpriv->chip_id = chip_id;
1890 	gpriv->max_channels = max_channels;
1891 
1892 	if (gpriv->chip_id == RENESAS_RZG2L) {
1893 		gpriv->rstc1 = devm_reset_control_get_exclusive(&pdev->dev, "rstp_n");
1894 		if (IS_ERR(gpriv->rstc1))
1895 			return dev_err_probe(&pdev->dev, PTR_ERR(gpriv->rstc1),
1896 					     "failed to get rstp_n\n");
1897 
1898 		gpriv->rstc2 = devm_reset_control_get_exclusive(&pdev->dev, "rstc_n");
1899 		if (IS_ERR(gpriv->rstc2))
1900 			return dev_err_probe(&pdev->dev, PTR_ERR(gpriv->rstc2),
1901 					     "failed to get rstc_n\n");
1902 	}
1903 
1904 	/* Peripheral clock */
1905 	gpriv->clkp = devm_clk_get(&pdev->dev, "fck");
1906 	if (IS_ERR(gpriv->clkp))
1907 		return dev_err_probe(&pdev->dev, PTR_ERR(gpriv->clkp),
1908 				     "cannot get peripheral clock\n");
1909 
1910 	/* fCAN clock: Pick External clock. If not available fallback to
1911 	 * CANFD clock
1912 	 */
1913 	gpriv->can_clk = devm_clk_get(&pdev->dev, "can_clk");
1914 	if (IS_ERR(gpriv->can_clk) || (clk_get_rate(gpriv->can_clk) == 0)) {
1915 		gpriv->can_clk = devm_clk_get(&pdev->dev, "canfd");
1916 		if (IS_ERR(gpriv->can_clk))
1917 			return dev_err_probe(&pdev->dev, PTR_ERR(gpriv->can_clk),
1918 					     "cannot get canfd clock\n");
1919 
1920 		gpriv->fcan = RCANFD_CANFDCLK;
1921 
1922 	} else {
1923 		gpriv->fcan = RCANFD_EXTCLK;
1924 	}
1925 	fcan_freq = clk_get_rate(gpriv->can_clk);
1926 
1927 	if (gpriv->fcan == RCANFD_CANFDCLK && gpriv->chip_id != RENESAS_RZG2L)
1928 		/* CANFD clock is further divided by (1/2) within the IP */
1929 		fcan_freq /= 2;
1930 
1931 	addr = devm_platform_ioremap_resource(pdev, 0);
1932 	if (IS_ERR(addr)) {
1933 		err = PTR_ERR(addr);
1934 		goto fail_dev;
1935 	}
1936 	gpriv->base = addr;
1937 
1938 	/* Request IRQ that's common for both channels */
1939 	if (gpriv->chip_id != RENESAS_RZG2L) {
1940 		err = devm_request_irq(&pdev->dev, ch_irq,
1941 				       rcar_canfd_channel_interrupt, 0,
1942 				       "canfd.ch_int", gpriv);
1943 		if (err) {
1944 			dev_err(&pdev->dev, "devm_request_irq(%d) failed, error %d\n",
1945 				ch_irq, err);
1946 			goto fail_dev;
1947 		}
1948 
1949 		err = devm_request_irq(&pdev->dev, g_irq,
1950 				       rcar_canfd_global_interrupt, 0,
1951 				       "canfd.g_int", gpriv);
1952 		if (err) {
1953 			dev_err(&pdev->dev, "devm_request_irq(%d) failed, error %d\n",
1954 				g_irq, err);
1955 			goto fail_dev;
1956 		}
1957 	} else {
1958 		err = devm_request_irq(&pdev->dev, g_recc_irq,
1959 				       rcar_canfd_global_receive_fifo_interrupt, 0,
1960 				       "canfd.g_recc", gpriv);
1961 
1962 		if (err) {
1963 			dev_err(&pdev->dev, "devm_request_irq(%d) failed, error %d\n",
1964 				g_recc_irq, err);
1965 			goto fail_dev;
1966 		}
1967 
1968 		err = devm_request_irq(&pdev->dev, g_err_irq,
1969 				       rcar_canfd_global_err_interrupt, 0,
1970 				       "canfd.g_err", gpriv);
1971 		if (err) {
1972 			dev_err(&pdev->dev, "devm_request_irq(%d) failed, error %d\n",
1973 				g_err_irq, err);
1974 			goto fail_dev;
1975 		}
1976 	}
1977 
1978 	err = reset_control_reset(gpriv->rstc1);
1979 	if (err)
1980 		goto fail_dev;
1981 	err = reset_control_reset(gpriv->rstc2);
1982 	if (err) {
1983 		reset_control_assert(gpriv->rstc1);
1984 		goto fail_dev;
1985 	}
1986 
1987 	/* Enable peripheral clock for register access */
1988 	err = clk_prepare_enable(gpriv->clkp);
1989 	if (err) {
1990 		dev_err(&pdev->dev,
1991 			"failed to enable peripheral clock, error %d\n", err);
1992 		goto fail_reset;
1993 	}
1994 
1995 	err = rcar_canfd_reset_controller(gpriv);
1996 	if (err) {
1997 		dev_err(&pdev->dev, "reset controller failed\n");
1998 		goto fail_clk;
1999 	}
2000 
2001 	/* Controller in Global reset & Channel reset mode */
2002 	rcar_canfd_configure_controller(gpriv);
2003 
2004 	/* Configure per channel attributes */
2005 	for_each_set_bit(ch, &gpriv->channels_mask, max_channels) {
2006 		/* Configure Channel's Rx fifo */
2007 		rcar_canfd_configure_rx(gpriv, ch);
2008 
2009 		/* Configure Channel's Tx (Common) fifo */
2010 		rcar_canfd_configure_tx(gpriv, ch);
2011 
2012 		/* Configure receive rules */
2013 		rcar_canfd_configure_afl_rules(gpriv, ch);
2014 	}
2015 
2016 	/* Configure common interrupts */
2017 	rcar_canfd_enable_global_interrupts(gpriv);
2018 
2019 	/* Start Global operation mode */
2020 	rcar_canfd_update_bit(gpriv->base, RCANFD_GCTR, RCANFD_GCTR_GMDC_MASK,
2021 			      RCANFD_GCTR_GMDC_GOPM);
2022 
2023 	/* Verify mode change */
2024 	err = readl_poll_timeout((gpriv->base + RCANFD_GSTS), sts,
2025 				 !(sts & RCANFD_GSTS_GNOPM), 2, 500000);
2026 	if (err) {
2027 		dev_err(&pdev->dev, "global operational mode failed\n");
2028 		goto fail_mode;
2029 	}
2030 
2031 	for_each_set_bit(ch, &gpriv->channels_mask, max_channels) {
2032 		err = rcar_canfd_channel_probe(gpriv, ch, fcan_freq);
2033 		if (err)
2034 			goto fail_channel;
2035 	}
2036 
2037 	platform_set_drvdata(pdev, gpriv);
2038 	dev_info(&pdev->dev, "global operational state (clk %d, fdmode %d)\n",
2039 		 gpriv->fcan, gpriv->fdmode);
2040 	return 0;
2041 
2042 fail_channel:
2043 	for_each_set_bit(ch, &gpriv->channels_mask, max_channels)
2044 		rcar_canfd_channel_remove(gpriv, ch);
2045 fail_mode:
2046 	rcar_canfd_disable_global_interrupts(gpriv);
2047 fail_clk:
2048 	clk_disable_unprepare(gpriv->clkp);
2049 fail_reset:
2050 	reset_control_assert(gpriv->rstc1);
2051 	reset_control_assert(gpriv->rstc2);
2052 fail_dev:
2053 	return err;
2054 }
2055 
2056 static int rcar_canfd_remove(struct platform_device *pdev)
2057 {
2058 	struct rcar_canfd_global *gpriv = platform_get_drvdata(pdev);
2059 	u32 ch;
2060 
2061 	rcar_canfd_reset_controller(gpriv);
2062 	rcar_canfd_disable_global_interrupts(gpriv);
2063 
2064 	for_each_set_bit(ch, &gpriv->channels_mask, gpriv->max_channels) {
2065 		rcar_canfd_disable_channel_interrupts(gpriv->ch[ch]);
2066 		rcar_canfd_channel_remove(gpriv, ch);
2067 	}
2068 
2069 	/* Enter global sleep mode */
2070 	rcar_canfd_set_bit(gpriv->base, RCANFD_GCTR, RCANFD_GCTR_GSLPR);
2071 	clk_disable_unprepare(gpriv->clkp);
2072 	reset_control_assert(gpriv->rstc1);
2073 	reset_control_assert(gpriv->rstc2);
2074 
2075 	return 0;
2076 }
2077 
2078 static int __maybe_unused rcar_canfd_suspend(struct device *dev)
2079 {
2080 	return 0;
2081 }
2082 
2083 static int __maybe_unused rcar_canfd_resume(struct device *dev)
2084 {
2085 	return 0;
2086 }
2087 
2088 static SIMPLE_DEV_PM_OPS(rcar_canfd_pm_ops, rcar_canfd_suspend,
2089 			 rcar_canfd_resume);
2090 
2091 static const __maybe_unused struct of_device_id rcar_canfd_of_table[] = {
2092 	{ .compatible = "renesas,rcar-gen3-canfd", .data = (void *)RENESAS_RCAR_GEN3 },
2093 	{ .compatible = "renesas,rzg2l-canfd", .data = (void *)RENESAS_RZG2L },
2094 	{ .compatible = "renesas,r8a779a0-canfd", .data = (void *)RENESAS_R8A779A0 },
2095 	{ }
2096 };
2097 
2098 MODULE_DEVICE_TABLE(of, rcar_canfd_of_table);
2099 
2100 static struct platform_driver rcar_canfd_driver = {
2101 	.driver = {
2102 		.name = RCANFD_DRV_NAME,
2103 		.of_match_table = of_match_ptr(rcar_canfd_of_table),
2104 		.pm = &rcar_canfd_pm_ops,
2105 	},
2106 	.probe = rcar_canfd_probe,
2107 	.remove = rcar_canfd_remove,
2108 };
2109 
2110 module_platform_driver(rcar_canfd_driver);
2111 
2112 MODULE_AUTHOR("Ramesh Shanmugasundaram <ramesh.shanmugasundaram@bp.renesas.com>");
2113 MODULE_LICENSE("GPL");
2114 MODULE_DESCRIPTION("CAN FD driver for Renesas R-Car SoC");
2115 MODULE_ALIAS("platform:" RCANFD_DRV_NAME);
2116