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