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