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