This source file includes following definitions.
- rcar_canfd_update
- rcar_canfd_read
- rcar_canfd_write
- rcar_canfd_set_bit
- rcar_canfd_clear_bit
- rcar_canfd_update_bit
- rcar_canfd_get_data
- rcar_canfd_put_data
- rcar_canfd_tx_failure_cleanup
- rcar_canfd_reset_controller
- rcar_canfd_configure_controller
- rcar_canfd_configure_afl_rules
- rcar_canfd_configure_rx
- rcar_canfd_configure_tx
- rcar_canfd_enable_global_interrupts
- rcar_canfd_disable_global_interrupts
- rcar_canfd_enable_channel_interrupts
- rcar_canfd_disable_channel_interrupts
- rcar_canfd_global_error
- rcar_canfd_error
- rcar_canfd_tx_done
- rcar_canfd_global_interrupt
- rcar_canfd_state_change
- rcar_canfd_channel_interrupt
- rcar_canfd_set_bittiming
- rcar_canfd_start
- rcar_canfd_open
- rcar_canfd_stop
- rcar_canfd_close
- rcar_canfd_start_xmit
- rcar_canfd_rx_pkt
- rcar_canfd_rx_poll
- rcar_canfd_do_set_mode
- rcar_canfd_get_berr_counter
- rcar_canfd_channel_probe
- rcar_canfd_channel_remove
- rcar_canfd_probe
- rcar_canfd_remove
- rcar_canfd_suspend
- rcar_canfd_resume
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
41 #define RCANFD_DRV_NAME "rcar_canfd"
42
43
44
45
46 #define RCANFD_GRMCFG_RCMC BIT(0)
47
48
49 #define RCANFD_GCFG_EEFE BIT(6)
50 #define RCANFD_GCFG_CMPOC BIT(5)
51 #define RCANFD_GCFG_DCS BIT(4)
52 #define RCANFD_GCFG_DCE BIT(1)
53 #define RCANFD_GCFG_TPRI BIT(0)
54
55
56 #define RCANFD_GCTR_TSRST BIT(16)
57 #define RCANFD_GCTR_CFMPOFIE BIT(11)
58 #define RCANFD_GCTR_THLEIE BIT(10)
59 #define RCANFD_GCTR_MEIE BIT(9)
60 #define RCANFD_GCTR_DEIE BIT(8)
61 #define RCANFD_GCTR_GSLPR BIT(2)
62 #define RCANFD_GCTR_GMDC_MASK (0x3)
63 #define RCANFD_GCTR_GMDC_GOPM (0x0)
64 #define RCANFD_GCTR_GMDC_GRESET (0x1)
65 #define RCANFD_GCTR_GMDC_GTEST (0x2)
66
67
68 #define RCANFD_GSTS_GRAMINIT BIT(3)
69 #define RCANFD_GSTS_GSLPSTS BIT(2)
70 #define RCANFD_GSTS_GHLTSTS BIT(1)
71 #define RCANFD_GSTS_GRSTSTS BIT(0)
72
73 #define RCANFD_GSTS_GNOPM (BIT(0) | BIT(1) | BIT(2) | BIT(3))
74
75
76 #define RCANFD_GERFL_EEF1 BIT(17)
77 #define RCANFD_GERFL_EEF0 BIT(16)
78 #define RCANFD_GERFL_CMPOF BIT(3)
79 #define RCANFD_GERFL_THLES BIT(2)
80 #define RCANFD_GERFL_MES BIT(1)
81 #define RCANFD_GERFL_DEF BIT(0)
82
83 #define RCANFD_GERFL_ERR(gpriv, x) ((x) & (RCANFD_GERFL_EEF1 |\
84 RCANFD_GERFL_EEF0 | RCANFD_GERFL_MES |\
85 (gpriv->fdmode ?\
86 RCANFD_GERFL_CMPOF : 0)))
87
88
89
90
91 #define RCANFD_GAFLCFG_SETRNC(n, x) (((x) & 0xff) << (24 - n * 8))
92 #define RCANFD_GAFLCFG_GETRNC(n, x) (((x) >> (24 - n * 8)) & 0xff)
93
94
95 #define RCANFD_GAFLECTR_AFLDAE BIT(8)
96 #define RCANFD_GAFLECTR_AFLPN(x) ((x) & 0x1f)
97
98
99 #define RCANFD_GAFLID_GAFLLB BIT(29)
100
101
102 #define RCANFD_GAFLP1_GAFLFDP(x) (1 << (x))
103
104
105
106
107 #define RCANFD_CFG_SJW(x) (((x) & 0x3) << 24)
108 #define RCANFD_CFG_TSEG2(x) (((x) & 0x7) << 20)
109 #define RCANFD_CFG_TSEG1(x) (((x) & 0xf) << 16)
110 #define RCANFD_CFG_BRP(x) (((x) & 0x3ff) << 0)
111
112
113 #define RCANFD_NCFG_NTSEG2(x) (((x) & 0x1f) << 24)
114 #define RCANFD_NCFG_NTSEG1(x) (((x) & 0x7f) << 16)
115 #define RCANFD_NCFG_NSJW(x) (((x) & 0x1f) << 11)
116 #define RCANFD_NCFG_NBRP(x) (((x) & 0x3ff) << 0)
117
118
119 #define RCANFD_CCTR_CTME BIT(24)
120 #define RCANFD_CCTR_ERRD BIT(23)
121 #define RCANFD_CCTR_BOM_MASK (0x3 << 21)
122 #define RCANFD_CCTR_BOM_ISO (0x0 << 21)
123 #define RCANFD_CCTR_BOM_BENTRY (0x1 << 21)
124 #define RCANFD_CCTR_BOM_BEND (0x2 << 21)
125 #define RCANFD_CCTR_TDCVFIE BIT(19)
126 #define RCANFD_CCTR_SOCOIE BIT(18)
127 #define RCANFD_CCTR_EOCOIE BIT(17)
128 #define RCANFD_CCTR_TAIE BIT(16)
129 #define RCANFD_CCTR_ALIE BIT(15)
130 #define RCANFD_CCTR_BLIE BIT(14)
131 #define RCANFD_CCTR_OLIE BIT(13)
132 #define RCANFD_CCTR_BORIE BIT(12)
133 #define RCANFD_CCTR_BOEIE BIT(11)
134 #define RCANFD_CCTR_EPIE BIT(10)
135 #define RCANFD_CCTR_EWIE BIT(9)
136 #define RCANFD_CCTR_BEIE BIT(8)
137 #define RCANFD_CCTR_CSLPR BIT(2)
138 #define RCANFD_CCTR_CHMDC_MASK (0x3)
139 #define RCANFD_CCTR_CHDMC_COPM (0x0)
140 #define RCANFD_CCTR_CHDMC_CRESET (0x1)
141 #define RCANFD_CCTR_CHDMC_CHLT (0x2)
142
143
144 #define RCANFD_CSTS_COMSTS BIT(7)
145 #define RCANFD_CSTS_RECSTS BIT(6)
146 #define RCANFD_CSTS_TRMSTS BIT(5)
147 #define RCANFD_CSTS_BOSTS BIT(4)
148 #define RCANFD_CSTS_EPSTS BIT(3)
149 #define RCANFD_CSTS_SLPSTS BIT(2)
150 #define RCANFD_CSTS_HLTSTS BIT(1)
151 #define RCANFD_CSTS_CRSTSTS BIT(0)
152
153 #define RCANFD_CSTS_TECCNT(x) (((x) >> 24) & 0xff)
154 #define RCANFD_CSTS_RECCNT(x) (((x) >> 16) & 0xff)
155
156
157 #define RCANFD_CERFL_ADERR BIT(14)
158 #define RCANFD_CERFL_B0ERR BIT(13)
159 #define RCANFD_CERFL_B1ERR BIT(12)
160 #define RCANFD_CERFL_CERR BIT(11)
161 #define RCANFD_CERFL_AERR BIT(10)
162 #define RCANFD_CERFL_FERR BIT(9)
163 #define RCANFD_CERFL_SERR BIT(8)
164 #define RCANFD_CERFL_ALF BIT(7)
165 #define RCANFD_CERFL_BLF BIT(6)
166 #define RCANFD_CERFL_OVLF BIT(5)
167 #define RCANFD_CERFL_BORF BIT(4)
168 #define RCANFD_CERFL_BOEF BIT(3)
169 #define RCANFD_CERFL_EPF BIT(2)
170 #define RCANFD_CERFL_EWF BIT(1)
171 #define RCANFD_CERFL_BEF BIT(0)
172
173 #define RCANFD_CERFL_ERR(x) ((x) & (0x7fff))
174
175
176 #define RCANFD_DCFG_DSJW(x) (((x) & 0x7) << 24)
177 #define RCANFD_DCFG_DTSEG2(x) (((x) & 0x7) << 20)
178 #define RCANFD_DCFG_DTSEG1(x) (((x) & 0xf) << 16)
179 #define RCANFD_DCFG_DBRP(x) (((x) & 0xff) << 0)
180
181
182 #define RCANFD_FDCFG_TDCE BIT(9)
183 #define RCANFD_FDCFG_TDCOC BIT(8)
184 #define RCANFD_FDCFG_TDCO(x) (((x) & 0x7f) >> 16)
185
186
187 #define RCANFD_RFCC_RFIM BIT(12)
188 #define RCANFD_RFCC_RFDC(x) (((x) & 0x7) << 8)
189 #define RCANFD_RFCC_RFPLS(x) (((x) & 0x7) << 4)
190 #define RCANFD_RFCC_RFIE BIT(1)
191 #define RCANFD_RFCC_RFE BIT(0)
192
193
194 #define RCANFD_RFSTS_RFIF BIT(3)
195 #define RCANFD_RFSTS_RFMLT BIT(2)
196 #define RCANFD_RFSTS_RFFLL BIT(1)
197 #define RCANFD_RFSTS_RFEMP BIT(0)
198
199
200 #define RCANFD_RFID_RFIDE BIT(31)
201 #define RCANFD_RFID_RFRTR BIT(30)
202
203
204 #define RCANFD_RFPTR_RFDLC(x) (((x) >> 28) & 0xf)
205 #define RCANFD_RFPTR_RFPTR(x) (((x) >> 16) & 0xfff)
206 #define RCANFD_RFPTR_RFTS(x) (((x) >> 0) & 0xffff)
207
208
209 #define RCANFD_RFFDSTS_RFFDF BIT(2)
210 #define RCANFD_RFFDSTS_RFBRS BIT(1)
211 #define RCANFD_RFFDSTS_RFESI BIT(0)
212
213
214
215
216 #define RCANFD_CFCC_CFTML(x) (((x) & 0xf) << 20)
217 #define RCANFD_CFCC_CFM(x) (((x) & 0x3) << 16)
218 #define RCANFD_CFCC_CFIM BIT(12)
219 #define RCANFD_CFCC_CFDC(x) (((x) & 0x7) << 8)
220 #define RCANFD_CFCC_CFPLS(x) (((x) & 0x7) << 4)
221 #define RCANFD_CFCC_CFTXIE BIT(2)
222 #define RCANFD_CFCC_CFE BIT(0)
223
224
225 #define RCANFD_CFSTS_CFMC(x) (((x) >> 8) & 0xff)
226 #define RCANFD_CFSTS_CFTXIF BIT(4)
227 #define RCANFD_CFSTS_CFMLT BIT(2)
228 #define RCANFD_CFSTS_CFFLL BIT(1)
229 #define RCANFD_CFSTS_CFEMP BIT(0)
230
231
232 #define RCANFD_CFID_CFIDE BIT(31)
233 #define RCANFD_CFID_CFRTR BIT(30)
234 #define RCANFD_CFID_CFID_MASK(x) ((x) & 0x1fffffff)
235
236
237 #define RCANFD_CFPTR_CFDLC(x) (((x) & 0xf) << 28)
238 #define RCANFD_CFPTR_CFPTR(x) (((x) & 0xfff) << 16)
239 #define RCANFD_CFPTR_CFTS(x) (((x) & 0xff) << 0)
240
241
242 #define RCANFD_CFFDCSTS_CFFDF BIT(2)
243 #define RCANFD_CFFDCSTS_CFBRS BIT(1)
244 #define RCANFD_CFFDCSTS_CFESI BIT(0)
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259 #define RCANFD_CCFG(m) (0x0000 + (0x10 * (m)))
260
261 #define RCANFD_CCTR(m) (0x0004 + (0x10 * (m)))
262
263 #define RCANFD_CSTS(m) (0x0008 + (0x10 * (m)))
264
265 #define RCANFD_CERFL(m) (0x000C + (0x10 * (m)))
266
267
268 #define RCANFD_GCFG (0x0084)
269
270 #define RCANFD_GCTR (0x0088)
271
272 #define RCANFD_GSTS (0x008c)
273
274 #define RCANFD_GERFL (0x0090)
275
276 #define RCANFD_GTSC (0x0094)
277
278 #define RCANFD_GAFLECTR (0x0098)
279
280 #define RCANFD_GAFLCFG0 (0x009c)
281
282 #define RCANFD_GAFLCFG1 (0x00a0)
283
284 #define RCANFD_RMNB (0x00a4)
285
286 #define RCANFD_RMND(y) (0x00a8 + (0x04 * (y)))
287
288
289 #define RCANFD_RFCC(x) (0x00b8 + (0x04 * (x)))
290
291 #define RCANFD_RFSTS(x) (0x00d8 + (0x04 * (x)))
292
293 #define RCANFD_RFPCTR(x) (0x00f8 + (0x04 * (x)))
294
295
296
297
298 #define RCANFD_CFCC(ch, idx) (0x0118 + (0x0c * (ch)) + \
299 (0x04 * (idx)))
300
301 #define RCANFD_CFSTS(ch, idx) (0x0178 + (0x0c * (ch)) + \
302 (0x04 * (idx)))
303
304 #define RCANFD_CFPCTR(ch, idx) (0x01d8 + (0x0c * (ch)) + \
305 (0x04 * (idx)))
306
307
308 #define RCANFD_FESTS (0x0238)
309
310 #define RCANFD_FFSTS (0x023c)
311
312 #define RCANFD_FMSTS (0x0240)
313
314 #define RCANFD_RFISTS (0x0244)
315
316 #define RCANFD_CFRISTS (0x0248)
317
318 #define RCANFD_CFTISTS (0x024c)
319
320
321 #define RCANFD_TMC(p) (0x0250 + (0x01 * (p)))
322
323 #define RCANFD_TMSTS(p) (0x02d0 + (0x01 * (p)))
324
325
326 #define RCANFD_TMTRSTS(y) (0x0350 + (0x04 * (y)))
327
328 #define RCANFD_TMTARSTS(y) (0x0360 + (0x04 * (y)))
329
330 #define RCANFD_TMTCSTS(y) (0x0370 + (0x04 * (y)))
331
332 #define RCANFD_TMTASTS(y) (0x0380 + (0x04 * (y)))
333
334 #define RCANFD_TMIEC(y) (0x0390 + (0x04 * (y)))
335
336
337 #define RCANFD_TXQCC(m) (0x03a0 + (0x04 * (m)))
338
339 #define RCANFD_TXQSTS(m) (0x03c0 + (0x04 * (m)))
340
341 #define RCANFD_TXQPCTR(m) (0x03e0 + (0x04 * (m)))
342
343
344 #define RCANFD_THLCC(m) (0x0400 + (0x04 * (m)))
345
346 #define RCANFD_THLSTS(m) (0x0420 + (0x04 * (m)))
347
348 #define RCANFD_THLPCTR(m) (0x0440 + (0x04 * (m)))
349
350
351 #define RCANFD_GTINTSTS0 (0x0460)
352
353 #define RCANFD_GTINTSTS1 (0x0464)
354
355 #define RCANFD_GTSTCFG (0x0468)
356
357 #define RCANFD_GTSTCTR (0x046c)
358
359 #define RCANFD_GLOCKK (0x047c)
360
361 #define RCANFD_GRMCFG (0x04fc)
362
363
364 #define RCANFD_GAFLID(offset, j) ((offset) + (0x10 * (j)))
365
366 #define RCANFD_GAFLM(offset, j) ((offset) + 0x04 + (0x10 * (j)))
367
368 #define RCANFD_GAFLP0(offset, j) ((offset) + 0x08 + (0x10 * (j)))
369
370 #define RCANFD_GAFLP1(offset, j) ((offset) + 0x0c + (0x10 * (j)))
371
372
373
374
375 #define RCANFD_C_GAFL_OFFSET (0x0500)
376
377
378 #define RCANFD_C_RMID(q) (0x0600 + (0x10 * (q)))
379 #define RCANFD_C_RMPTR(q) (0x0604 + (0x10 * (q)))
380 #define RCANFD_C_RMDF0(q) (0x0608 + (0x10 * (q)))
381 #define RCANFD_C_RMDF1(q) (0x060c + (0x10 * (q)))
382
383
384 #define RCANFD_C_RFOFFSET (0x0e00)
385 #define RCANFD_C_RFID(x) (RCANFD_C_RFOFFSET + (0x10 * (x)))
386 #define RCANFD_C_RFPTR(x) (RCANFD_C_RFOFFSET + 0x04 + \
387 (0x10 * (x)))
388 #define RCANFD_C_RFDF(x, df) (RCANFD_C_RFOFFSET + 0x08 + \
389 (0x10 * (x)) + (0x04 * (df)))
390
391
392 #define RCANFD_C_CFOFFSET (0x0e80)
393 #define RCANFD_C_CFID(ch, idx) (RCANFD_C_CFOFFSET + (0x30 * (ch)) + \
394 (0x10 * (idx)))
395 #define RCANFD_C_CFPTR(ch, idx) (RCANFD_C_CFOFFSET + 0x04 + \
396 (0x30 * (ch)) + (0x10 * (idx)))
397 #define RCANFD_C_CFDF(ch, idx, df) (RCANFD_C_CFOFFSET + 0x08 + \
398 (0x30 * (ch)) + (0x10 * (idx)) + \
399 (0x04 * (df)))
400
401
402 #define RCANFD_C_TMID(p) (0x1000 + (0x10 * (p)))
403 #define RCANFD_C_TMPTR(p) (0x1004 + (0x10 * (p)))
404 #define RCANFD_C_TMDF0(p) (0x1008 + (0x10 * (p)))
405 #define RCANFD_C_TMDF1(p) (0x100c + (0x10 * (p)))
406
407
408 #define RCANFD_C_THLACC(m) (0x1800 + (0x04 * (m)))
409
410 #define RCANFD_C_RPGACC(r) (0x1900 + (0x04 * (r)))
411
412
413
414
415 #define RCANFD_F_DCFG(m) (0x0500 + (0x20 * (m)))
416 #define RCANFD_F_CFDCFG(m) (0x0504 + (0x20 * (m)))
417 #define RCANFD_F_CFDCTR(m) (0x0508 + (0x20 * (m)))
418 #define RCANFD_F_CFDSTS(m) (0x050c + (0x20 * (m)))
419 #define RCANFD_F_CFDCRC(m) (0x0510 + (0x20 * (m)))
420
421
422 #define RCANFD_F_GAFL_OFFSET (0x1000)
423
424
425 #define RCANFD_F_RMID(q) (0x2000 + (0x20 * (q)))
426 #define RCANFD_F_RMPTR(q) (0x2004 + (0x20 * (q)))
427 #define RCANFD_F_RMFDSTS(q) (0x2008 + (0x20 * (q)))
428 #define RCANFD_F_RMDF(q, b) (0x200c + (0x04 * (b)) + (0x20 * (q)))
429
430
431 #define RCANFD_F_RFOFFSET (0x3000)
432 #define RCANFD_F_RFID(x) (RCANFD_F_RFOFFSET + (0x80 * (x)))
433 #define RCANFD_F_RFPTR(x) (RCANFD_F_RFOFFSET + 0x04 + \
434 (0x80 * (x)))
435 #define RCANFD_F_RFFDSTS(x) (RCANFD_F_RFOFFSET + 0x08 + \
436 (0x80 * (x)))
437 #define RCANFD_F_RFDF(x, df) (RCANFD_F_RFOFFSET + 0x0c + \
438 (0x80 * (x)) + (0x04 * (df)))
439
440
441 #define RCANFD_F_CFOFFSET (0x3400)
442 #define RCANFD_F_CFID(ch, idx) (RCANFD_F_CFOFFSET + (0x180 * (ch)) + \
443 (0x80 * (idx)))
444 #define RCANFD_F_CFPTR(ch, idx) (RCANFD_F_CFOFFSET + 0x04 + \
445 (0x180 * (ch)) + (0x80 * (idx)))
446 #define RCANFD_F_CFFDCSTS(ch, idx) (RCANFD_F_CFOFFSET + 0x08 + \
447 (0x180 * (ch)) + (0x80 * (idx)))
448 #define RCANFD_F_CFDF(ch, idx, df) (RCANFD_F_CFOFFSET + 0x0c + \
449 (0x180 * (ch)) + (0x80 * (idx)) + \
450 (0x04 * (df)))
451
452
453 #define RCANFD_F_TMID(p) (0x4000 + (0x20 * (p)))
454 #define RCANFD_F_TMPTR(p) (0x4004 + (0x20 * (p)))
455 #define RCANFD_F_TMFDCTR(p) (0x4008 + (0x20 * (p)))
456 #define RCANFD_F_TMDF(p, b) (0x400c + (0x20 * (p)) + (0x04 * (b)))
457
458
459 #define RCANFD_F_THLACC(m) (0x6000 + (0x04 * (m)))
460
461 #define RCANFD_F_RPGACC(r) (0x6400 + (0x04 * (r)))
462
463
464 #define RCANFD_FIFO_DEPTH 8
465 #define RCANFD_NAPI_WEIGHT 8
466
467 #define RCANFD_NUM_CHANNELS 2
468 #define RCANFD_CHANNELS_MASK BIT((RCANFD_NUM_CHANNELS) - 1)
469
470 #define RCANFD_GAFL_PAGENUM(entry) ((entry) / 16)
471 #define RCANFD_CHANNEL_NUMRULES 1
472
473
474
475
476
477 #define RCANFD_RFFIFO_IDX 0
478
479
480
481
482 #define RCANFD_CFFIFO_IDX 0
483
484
485 enum rcar_canfd_fcanclk {
486 RCANFD_CANFDCLK = 0,
487 RCANFD_EXTCLK,
488 };
489
490 struct rcar_canfd_global;
491
492
493 struct rcar_canfd_channel {
494 struct can_priv can;
495 struct net_device *ndev;
496 struct rcar_canfd_global *gpriv;
497 void __iomem *base;
498 struct napi_struct napi;
499 u8 tx_len[RCANFD_FIFO_DEPTH];
500 u32 tx_head;
501 u32 tx_tail;
502 u32 channel;
503 spinlock_t tx_lock;
504 };
505
506
507 struct rcar_canfd_global {
508 struct rcar_canfd_channel *ch[RCANFD_NUM_CHANNELS];
509 void __iomem *base;
510 struct platform_device *pdev;
511 struct clk *clkp;
512 struct clk *can_clk;
513 enum rcar_canfd_fcanclk fcan;
514 unsigned long channels_mask;
515 bool fdmode;
516 };
517
518
519 static const struct can_bittiming_const rcar_canfd_nom_bittiming_const = {
520 .name = RCANFD_DRV_NAME,
521 .tseg1_min = 2,
522 .tseg1_max = 128,
523 .tseg2_min = 2,
524 .tseg2_max = 32,
525 .sjw_max = 32,
526 .brp_min = 1,
527 .brp_max = 1024,
528 .brp_inc = 1,
529 };
530
531
532 static const struct can_bittiming_const rcar_canfd_data_bittiming_const = {
533 .name = RCANFD_DRV_NAME,
534 .tseg1_min = 2,
535 .tseg1_max = 16,
536 .tseg2_min = 2,
537 .tseg2_max = 8,
538 .sjw_max = 8,
539 .brp_min = 1,
540 .brp_max = 256,
541 .brp_inc = 1,
542 };
543
544
545 static const struct can_bittiming_const rcar_canfd_bittiming_const = {
546 .name = RCANFD_DRV_NAME,
547 .tseg1_min = 4,
548 .tseg1_max = 16,
549 .tseg2_min = 2,
550 .tseg2_max = 8,
551 .sjw_max = 4,
552 .brp_min = 1,
553 .brp_max = 1024,
554 .brp_inc = 1,
555 };
556
557
558 static inline void rcar_canfd_update(u32 mask, u32 val, u32 __iomem *reg)
559 {
560 u32 data = readl(reg);
561
562 data &= ~mask;
563 data |= (val & mask);
564 writel(data, reg);
565 }
566
567 static inline u32 rcar_canfd_read(void __iomem *base, u32 offset)
568 {
569 return readl(base + (offset));
570 }
571
572 static inline void rcar_canfd_write(void __iomem *base, u32 offset, u32 val)
573 {
574 writel(val, base + (offset));
575 }
576
577 static void rcar_canfd_set_bit(void __iomem *base, u32 reg, u32 val)
578 {
579 rcar_canfd_update(val, val, base + (reg));
580 }
581
582 static void rcar_canfd_clear_bit(void __iomem *base, u32 reg, u32 val)
583 {
584 rcar_canfd_update(val, 0, base + (reg));
585 }
586
587 static void rcar_canfd_update_bit(void __iomem *base, u32 reg,
588 u32 mask, u32 val)
589 {
590 rcar_canfd_update(mask, val, base + (reg));
591 }
592
593 static void rcar_canfd_get_data(struct rcar_canfd_channel *priv,
594 struct canfd_frame *cf, u32 off)
595 {
596 u32 i, lwords;
597
598 lwords = DIV_ROUND_UP(cf->len, sizeof(u32));
599 for (i = 0; i < lwords; i++)
600 *((u32 *)cf->data + i) =
601 rcar_canfd_read(priv->base, off + (i * sizeof(u32)));
602 }
603
604 static void rcar_canfd_put_data(struct rcar_canfd_channel *priv,
605 struct canfd_frame *cf, u32 off)
606 {
607 u32 i, lwords;
608
609 lwords = DIV_ROUND_UP(cf->len, sizeof(u32));
610 for (i = 0; i < lwords; i++)
611 rcar_canfd_write(priv->base, off + (i * sizeof(u32)),
612 *((u32 *)cf->data + i));
613 }
614
615 static void rcar_canfd_tx_failure_cleanup(struct net_device *ndev)
616 {
617 u32 i;
618
619 for (i = 0; i < RCANFD_FIFO_DEPTH; i++)
620 can_free_echo_skb(ndev, i);
621 }
622
623 static int rcar_canfd_reset_controller(struct rcar_canfd_global *gpriv)
624 {
625 u32 sts, ch;
626 int err;
627
628
629
630
631 err = readl_poll_timeout((gpriv->base + RCANFD_GSTS), sts,
632 !(sts & RCANFD_GSTS_GRAMINIT), 2, 500000);
633 if (err) {
634 dev_dbg(&gpriv->pdev->dev, "global raminit failed\n");
635 return err;
636 }
637
638
639 rcar_canfd_clear_bit(gpriv->base, RCANFD_GCTR, RCANFD_GCTR_GSLPR);
640 rcar_canfd_update_bit(gpriv->base, RCANFD_GCTR,
641 RCANFD_GCTR_GMDC_MASK, RCANFD_GCTR_GMDC_GRESET);
642
643
644 err = readl_poll_timeout((gpriv->base + RCANFD_GSTS), sts,
645 (sts & RCANFD_GSTS_GRSTSTS), 2, 500000);
646 if (err) {
647 dev_dbg(&gpriv->pdev->dev, "global reset failed\n");
648 return err;
649 }
650
651
652 rcar_canfd_write(gpriv->base, RCANFD_GERFL, 0x0);
653
654
655 if (gpriv->fdmode)
656 rcar_canfd_set_bit(gpriv->base, RCANFD_GRMCFG,
657 RCANFD_GRMCFG_RCMC);
658 else
659 rcar_canfd_clear_bit(gpriv->base, RCANFD_GRMCFG,
660 RCANFD_GRMCFG_RCMC);
661
662
663 for_each_set_bit(ch, &gpriv->channels_mask, RCANFD_NUM_CHANNELS) {
664 rcar_canfd_clear_bit(gpriv->base,
665 RCANFD_CCTR(ch), RCANFD_CCTR_CSLPR);
666
667 rcar_canfd_update_bit(gpriv->base, RCANFD_CCTR(ch),
668 RCANFD_CCTR_CHMDC_MASK,
669 RCANFD_CCTR_CHDMC_CRESET);
670
671
672 err = readl_poll_timeout((gpriv->base + RCANFD_CSTS(ch)), sts,
673 (sts & RCANFD_CSTS_CRSTSTS),
674 2, 500000);
675 if (err) {
676 dev_dbg(&gpriv->pdev->dev,
677 "channel %u reset failed\n", ch);
678 return err;
679 }
680 }
681 return 0;
682 }
683
684 static void rcar_canfd_configure_controller(struct rcar_canfd_global *gpriv)
685 {
686 u32 cfg, ch;
687
688
689
690
691 cfg = RCANFD_GCFG_EEFE;
692
693 if (gpriv->fdmode)
694
695 cfg |= RCANFD_GCFG_CMPOC;
696
697
698 if (gpriv->fcan != RCANFD_CANFDCLK)
699 cfg |= RCANFD_GCFG_DCS;
700
701 rcar_canfd_set_bit(gpriv->base, RCANFD_GCFG, cfg);
702
703
704 for_each_set_bit(ch, &gpriv->channels_mask, RCANFD_NUM_CHANNELS) {
705 rcar_canfd_set_bit(gpriv->base, RCANFD_CCTR(ch),
706 RCANFD_CCTR_ERRD);
707 rcar_canfd_update_bit(gpriv->base, RCANFD_CCTR(ch),
708 RCANFD_CCTR_BOM_MASK,
709 RCANFD_CCTR_BOM_BENTRY);
710 }
711 }
712
713 static void rcar_canfd_configure_afl_rules(struct rcar_canfd_global *gpriv,
714 u32 ch)
715 {
716 u32 cfg;
717 int offset, start, page, num_rules = RCANFD_CHANNEL_NUMRULES;
718 u32 ridx = ch + RCANFD_RFFIFO_IDX;
719
720 if (ch == 0) {
721 start = 0;
722 } else {
723
724 cfg = rcar_canfd_read(gpriv->base, RCANFD_GAFLCFG0);
725 start = RCANFD_GAFLCFG_GETRNC(0, cfg);
726 }
727
728
729 page = RCANFD_GAFL_PAGENUM(start);
730 rcar_canfd_set_bit(gpriv->base, RCANFD_GAFLECTR,
731 (RCANFD_GAFLECTR_AFLPN(page) |
732 RCANFD_GAFLECTR_AFLDAE));
733
734
735 rcar_canfd_set_bit(gpriv->base, RCANFD_GAFLCFG0,
736 RCANFD_GAFLCFG_SETRNC(ch, num_rules));
737 if (gpriv->fdmode)
738 offset = RCANFD_F_GAFL_OFFSET;
739 else
740 offset = RCANFD_C_GAFL_OFFSET;
741
742
743 rcar_canfd_write(gpriv->base, RCANFD_GAFLID(offset, start), 0);
744
745 rcar_canfd_write(gpriv->base, RCANFD_GAFLM(offset, start), 0);
746
747 rcar_canfd_write(gpriv->base, RCANFD_GAFLP0(offset, start), 0);
748
749 rcar_canfd_write(gpriv->base, RCANFD_GAFLP1(offset, start),
750 RCANFD_GAFLP1_GAFLFDP(ridx));
751
752
753 rcar_canfd_clear_bit(gpriv->base,
754 RCANFD_GAFLECTR, RCANFD_GAFLECTR_AFLDAE);
755 }
756
757 static void rcar_canfd_configure_rx(struct rcar_canfd_global *gpriv, u32 ch)
758 {
759
760 u32 cfg;
761 u16 rfdc, rfpls;
762
763
764 u32 ridx = ch + RCANFD_RFFIFO_IDX;
765
766 rfdc = 2;
767 if (gpriv->fdmode)
768 rfpls = 7;
769 else
770 rfpls = 0;
771
772 cfg = (RCANFD_RFCC_RFIM | RCANFD_RFCC_RFDC(rfdc) |
773 RCANFD_RFCC_RFPLS(rfpls) | RCANFD_RFCC_RFIE);
774 rcar_canfd_write(gpriv->base, RCANFD_RFCC(ridx), cfg);
775 }
776
777 static void rcar_canfd_configure_tx(struct rcar_canfd_global *gpriv, u32 ch)
778 {
779
780
781
782
783
784
785 u32 cfg;
786 u16 cftml, cfm, cfdc, cfpls;
787
788 cftml = 0;
789 cfm = 1;
790 cfdc = 2;
791 if (gpriv->fdmode)
792 cfpls = 7;
793 else
794 cfpls = 0;
795
796 cfg = (RCANFD_CFCC_CFTML(cftml) | RCANFD_CFCC_CFM(cfm) |
797 RCANFD_CFCC_CFIM | RCANFD_CFCC_CFDC(cfdc) |
798 RCANFD_CFCC_CFPLS(cfpls) | RCANFD_CFCC_CFTXIE);
799 rcar_canfd_write(gpriv->base, RCANFD_CFCC(ch, RCANFD_CFFIFO_IDX), cfg);
800
801 if (gpriv->fdmode)
802
803 rcar_canfd_write(gpriv->base,
804 RCANFD_F_CFFDCSTS(ch, RCANFD_CFFIFO_IDX), 0);
805 }
806
807 static void rcar_canfd_enable_global_interrupts(struct rcar_canfd_global *gpriv)
808 {
809 u32 ctr;
810
811
812 rcar_canfd_write(gpriv->base, RCANFD_GERFL, 0);
813
814
815 ctr = RCANFD_GCTR_MEIE;
816 if (gpriv->fdmode)
817 ctr |= RCANFD_GCTR_CFMPOFIE;
818
819 rcar_canfd_set_bit(gpriv->base, RCANFD_GCTR, ctr);
820 }
821
822 static void rcar_canfd_disable_global_interrupts(struct rcar_canfd_global
823 *gpriv)
824 {
825
826 rcar_canfd_write(gpriv->base, RCANFD_GCTR, 0);
827
828
829 rcar_canfd_write(gpriv->base, RCANFD_GERFL, 0);
830 }
831
832 static void rcar_canfd_enable_channel_interrupts(struct rcar_canfd_channel
833 *priv)
834 {
835 u32 ctr, ch = priv->channel;
836
837
838 rcar_canfd_write(priv->base, RCANFD_CERFL(ch), 0);
839
840
841 ctr = (RCANFD_CCTR_TAIE |
842 RCANFD_CCTR_ALIE | RCANFD_CCTR_BLIE |
843 RCANFD_CCTR_OLIE | RCANFD_CCTR_BORIE |
844 RCANFD_CCTR_BOEIE | RCANFD_CCTR_EPIE |
845 RCANFD_CCTR_EWIE | RCANFD_CCTR_BEIE);
846 rcar_canfd_set_bit(priv->base, RCANFD_CCTR(ch), ctr);
847 }
848
849 static void rcar_canfd_disable_channel_interrupts(struct rcar_canfd_channel
850 *priv)
851 {
852 u32 ctr, ch = priv->channel;
853
854 ctr = (RCANFD_CCTR_TAIE |
855 RCANFD_CCTR_ALIE | RCANFD_CCTR_BLIE |
856 RCANFD_CCTR_OLIE | RCANFD_CCTR_BORIE |
857 RCANFD_CCTR_BOEIE | RCANFD_CCTR_EPIE |
858 RCANFD_CCTR_EWIE | RCANFD_CCTR_BEIE);
859 rcar_canfd_clear_bit(priv->base, RCANFD_CCTR(ch), ctr);
860
861
862 rcar_canfd_write(priv->base, RCANFD_CERFL(ch), 0);
863 }
864
865 static void rcar_canfd_global_error(struct net_device *ndev)
866 {
867 struct rcar_canfd_channel *priv = netdev_priv(ndev);
868 struct rcar_canfd_global *gpriv = priv->gpriv;
869 struct net_device_stats *stats = &ndev->stats;
870 u32 ch = priv->channel;
871 u32 gerfl, sts;
872 u32 ridx = ch + RCANFD_RFFIFO_IDX;
873
874 gerfl = rcar_canfd_read(priv->base, RCANFD_GERFL);
875 if ((gerfl & RCANFD_GERFL_EEF0) && (ch == 0)) {
876 netdev_dbg(ndev, "Ch0: ECC Error flag\n");
877 stats->tx_dropped++;
878 }
879 if ((gerfl & RCANFD_GERFL_EEF1) && (ch == 1)) {
880 netdev_dbg(ndev, "Ch1: ECC Error flag\n");
881 stats->tx_dropped++;
882 }
883 if (gerfl & RCANFD_GERFL_MES) {
884 sts = rcar_canfd_read(priv->base,
885 RCANFD_CFSTS(ch, RCANFD_CFFIFO_IDX));
886 if (sts & RCANFD_CFSTS_CFMLT) {
887 netdev_dbg(ndev, "Tx Message Lost flag\n");
888 stats->tx_dropped++;
889 rcar_canfd_write(priv->base,
890 RCANFD_CFSTS(ch, RCANFD_CFFIFO_IDX),
891 sts & ~RCANFD_CFSTS_CFMLT);
892 }
893
894 sts = rcar_canfd_read(priv->base, RCANFD_RFSTS(ridx));
895 if (sts & RCANFD_RFSTS_RFMLT) {
896 netdev_dbg(ndev, "Rx Message Lost flag\n");
897 stats->rx_dropped++;
898 rcar_canfd_write(priv->base, RCANFD_RFSTS(ridx),
899 sts & ~RCANFD_RFSTS_RFMLT);
900 }
901 }
902 if (gpriv->fdmode && gerfl & RCANFD_GERFL_CMPOF) {
903
904
905
906
907 netdev_dbg(ndev, "global payload overflow interrupt\n");
908 }
909
910
911
912
913 rcar_canfd_write(priv->base, RCANFD_GERFL, 0);
914 }
915
916 static void rcar_canfd_error(struct net_device *ndev, u32 cerfl,
917 u16 txerr, u16 rxerr)
918 {
919 struct rcar_canfd_channel *priv = netdev_priv(ndev);
920 struct net_device_stats *stats = &ndev->stats;
921 struct can_frame *cf;
922 struct sk_buff *skb;
923 u32 ch = priv->channel;
924
925 netdev_dbg(ndev, "ch erfl %x txerr %u rxerr %u\n", cerfl, txerr, rxerr);
926
927
928 skb = alloc_can_err_skb(ndev, &cf);
929 if (!skb) {
930 stats->rx_dropped++;
931 return;
932 }
933
934
935 if (cerfl & RCANFD_CERFL_BEF) {
936 netdev_dbg(ndev, "Bus error\n");
937 cf->can_id |= CAN_ERR_BUSERROR | CAN_ERR_PROT;
938 cf->data[2] = CAN_ERR_PROT_UNSPEC;
939 priv->can.can_stats.bus_error++;
940 }
941 if (cerfl & RCANFD_CERFL_ADERR) {
942 netdev_dbg(ndev, "ACK Delimiter Error\n");
943 stats->tx_errors++;
944 cf->data[3] |= CAN_ERR_PROT_LOC_ACK_DEL;
945 }
946 if (cerfl & RCANFD_CERFL_B0ERR) {
947 netdev_dbg(ndev, "Bit Error (dominant)\n");
948 stats->tx_errors++;
949 cf->data[2] |= CAN_ERR_PROT_BIT0;
950 }
951 if (cerfl & RCANFD_CERFL_B1ERR) {
952 netdev_dbg(ndev, "Bit Error (recessive)\n");
953 stats->tx_errors++;
954 cf->data[2] |= CAN_ERR_PROT_BIT1;
955 }
956 if (cerfl & RCANFD_CERFL_CERR) {
957 netdev_dbg(ndev, "CRC Error\n");
958 stats->rx_errors++;
959 cf->data[3] |= CAN_ERR_PROT_LOC_CRC_SEQ;
960 }
961 if (cerfl & RCANFD_CERFL_AERR) {
962 netdev_dbg(ndev, "ACK Error\n");
963 stats->tx_errors++;
964 cf->can_id |= CAN_ERR_ACK;
965 cf->data[3] |= CAN_ERR_PROT_LOC_ACK;
966 }
967 if (cerfl & RCANFD_CERFL_FERR) {
968 netdev_dbg(ndev, "Form Error\n");
969 stats->rx_errors++;
970 cf->data[2] |= CAN_ERR_PROT_FORM;
971 }
972 if (cerfl & RCANFD_CERFL_SERR) {
973 netdev_dbg(ndev, "Stuff Error\n");
974 stats->rx_errors++;
975 cf->data[2] |= CAN_ERR_PROT_STUFF;
976 }
977 if (cerfl & RCANFD_CERFL_ALF) {
978 netdev_dbg(ndev, "Arbitration lost Error\n");
979 priv->can.can_stats.arbitration_lost++;
980 cf->can_id |= CAN_ERR_LOSTARB;
981 cf->data[0] |= CAN_ERR_LOSTARB_UNSPEC;
982 }
983 if (cerfl & RCANFD_CERFL_BLF) {
984 netdev_dbg(ndev, "Bus Lock Error\n");
985 stats->rx_errors++;
986 cf->can_id |= CAN_ERR_BUSERROR;
987 }
988 if (cerfl & RCANFD_CERFL_EWF) {
989 netdev_dbg(ndev, "Error warning interrupt\n");
990 priv->can.state = CAN_STATE_ERROR_WARNING;
991 priv->can.can_stats.error_warning++;
992 cf->can_id |= CAN_ERR_CRTL;
993 cf->data[1] = txerr > rxerr ? CAN_ERR_CRTL_TX_WARNING :
994 CAN_ERR_CRTL_RX_WARNING;
995 cf->data[6] = txerr;
996 cf->data[7] = rxerr;
997 }
998 if (cerfl & RCANFD_CERFL_EPF) {
999 netdev_dbg(ndev, "Error passive interrupt\n");
1000 priv->can.state = CAN_STATE_ERROR_PASSIVE;
1001 priv->can.can_stats.error_passive++;
1002 cf->can_id |= CAN_ERR_CRTL;
1003 cf->data[1] = txerr > rxerr ? CAN_ERR_CRTL_TX_PASSIVE :
1004 CAN_ERR_CRTL_RX_PASSIVE;
1005 cf->data[6] = txerr;
1006 cf->data[7] = rxerr;
1007 }
1008 if (cerfl & RCANFD_CERFL_BOEF) {
1009 netdev_dbg(ndev, "Bus-off entry interrupt\n");
1010 rcar_canfd_tx_failure_cleanup(ndev);
1011 priv->can.state = CAN_STATE_BUS_OFF;
1012 priv->can.can_stats.bus_off++;
1013 can_bus_off(ndev);
1014 cf->can_id |= CAN_ERR_BUSOFF;
1015 }
1016 if (cerfl & RCANFD_CERFL_OVLF) {
1017 netdev_dbg(ndev,
1018 "Overload Frame Transmission error interrupt\n");
1019 stats->tx_errors++;
1020 cf->can_id |= CAN_ERR_PROT;
1021 cf->data[2] |= CAN_ERR_PROT_OVERLOAD;
1022 }
1023
1024
1025 rcar_canfd_write(priv->base, RCANFD_CERFL(ch),
1026 RCANFD_CERFL_ERR(~cerfl));
1027 stats->rx_packets++;
1028 stats->rx_bytes += cf->can_dlc;
1029 netif_rx(skb);
1030 }
1031
1032 static void rcar_canfd_tx_done(struct net_device *ndev)
1033 {
1034 struct rcar_canfd_channel *priv = netdev_priv(ndev);
1035 struct net_device_stats *stats = &ndev->stats;
1036 u32 sts;
1037 unsigned long flags;
1038 u32 ch = priv->channel;
1039
1040 do {
1041 u8 unsent, sent;
1042
1043 sent = priv->tx_tail % RCANFD_FIFO_DEPTH;
1044 stats->tx_packets++;
1045 stats->tx_bytes += priv->tx_len[sent];
1046 priv->tx_len[sent] = 0;
1047 can_get_echo_skb(ndev, sent);
1048
1049 spin_lock_irqsave(&priv->tx_lock, flags);
1050 priv->tx_tail++;
1051 sts = rcar_canfd_read(priv->base,
1052 RCANFD_CFSTS(ch, RCANFD_CFFIFO_IDX));
1053 unsent = RCANFD_CFSTS_CFMC(sts);
1054
1055
1056 if (unsent != RCANFD_FIFO_DEPTH)
1057 netif_wake_queue(ndev);
1058
1059 if (priv->tx_head - priv->tx_tail <= unsent) {
1060 spin_unlock_irqrestore(&priv->tx_lock, flags);
1061 break;
1062 }
1063 spin_unlock_irqrestore(&priv->tx_lock, flags);
1064
1065 } while (1);
1066
1067
1068 rcar_canfd_write(priv->base, RCANFD_CFSTS(ch, RCANFD_CFFIFO_IDX),
1069 sts & ~RCANFD_CFSTS_CFTXIF);
1070 can_led_event(ndev, CAN_LED_EVENT_TX);
1071 }
1072
1073 static irqreturn_t rcar_canfd_global_interrupt(int irq, void *dev_id)
1074 {
1075 struct rcar_canfd_global *gpriv = dev_id;
1076 struct net_device *ndev;
1077 struct rcar_canfd_channel *priv;
1078 u32 sts, gerfl;
1079 u32 ch, ridx;
1080
1081
1082
1083
1084 for_each_set_bit(ch, &gpriv->channels_mask, RCANFD_NUM_CHANNELS) {
1085 priv = gpriv->ch[ch];
1086 ndev = priv->ndev;
1087 ridx = ch + RCANFD_RFFIFO_IDX;
1088
1089
1090 gerfl = rcar_canfd_read(priv->base, RCANFD_GERFL);
1091 if (unlikely(RCANFD_GERFL_ERR(gpriv, gerfl)))
1092 rcar_canfd_global_error(ndev);
1093
1094
1095 sts = rcar_canfd_read(priv->base, RCANFD_RFSTS(ridx));
1096 if (likely(sts & RCANFD_RFSTS_RFIF)) {
1097 if (napi_schedule_prep(&priv->napi)) {
1098
1099 rcar_canfd_clear_bit(priv->base,
1100 RCANFD_RFCC(ridx),
1101 RCANFD_RFCC_RFIE);
1102 __napi_schedule(&priv->napi);
1103 }
1104 }
1105 }
1106 return IRQ_HANDLED;
1107 }
1108
1109 static void rcar_canfd_state_change(struct net_device *ndev,
1110 u16 txerr, u16 rxerr)
1111 {
1112 struct rcar_canfd_channel *priv = netdev_priv(ndev);
1113 struct net_device_stats *stats = &ndev->stats;
1114 enum can_state rx_state, tx_state, state = priv->can.state;
1115 struct can_frame *cf;
1116 struct sk_buff *skb;
1117
1118
1119 if (txerr < 96 && rxerr < 96)
1120 state = CAN_STATE_ERROR_ACTIVE;
1121 else if (txerr < 128 && rxerr < 128)
1122 state = CAN_STATE_ERROR_WARNING;
1123
1124 if (state != priv->can.state) {
1125 netdev_dbg(ndev, "state: new %d, old %d: txerr %u, rxerr %u\n",
1126 state, priv->can.state, txerr, rxerr);
1127 skb = alloc_can_err_skb(ndev, &cf);
1128 if (!skb) {
1129 stats->rx_dropped++;
1130 return;
1131 }
1132 tx_state = txerr >= rxerr ? state : 0;
1133 rx_state = txerr <= rxerr ? state : 0;
1134
1135 can_change_state(ndev, cf, tx_state, rx_state);
1136 stats->rx_packets++;
1137 stats->rx_bytes += cf->can_dlc;
1138 netif_rx(skb);
1139 }
1140 }
1141
1142 static irqreturn_t rcar_canfd_channel_interrupt(int irq, void *dev_id)
1143 {
1144 struct rcar_canfd_global *gpriv = dev_id;
1145 struct net_device *ndev;
1146 struct rcar_canfd_channel *priv;
1147 u32 sts, ch, cerfl;
1148 u16 txerr, rxerr;
1149
1150
1151 for_each_set_bit(ch, &gpriv->channels_mask, RCANFD_NUM_CHANNELS) {
1152 priv = gpriv->ch[ch];
1153 ndev = priv->ndev;
1154
1155
1156 cerfl = rcar_canfd_read(priv->base, RCANFD_CERFL(ch));
1157 sts = rcar_canfd_read(priv->base, RCANFD_CSTS(ch));
1158 txerr = RCANFD_CSTS_TECCNT(sts);
1159 rxerr = RCANFD_CSTS_RECCNT(sts);
1160 if (unlikely(RCANFD_CERFL_ERR(cerfl)))
1161 rcar_canfd_error(ndev, cerfl, txerr, rxerr);
1162
1163
1164 if (unlikely((priv->can.state != CAN_STATE_ERROR_ACTIVE) &&
1165 (priv->can.state != CAN_STATE_BUS_OFF)))
1166 rcar_canfd_state_change(ndev, txerr, rxerr);
1167
1168
1169 sts = rcar_canfd_read(priv->base,
1170 RCANFD_CFSTS(ch, RCANFD_CFFIFO_IDX));
1171 if (likely(sts & RCANFD_CFSTS_CFTXIF))
1172 rcar_canfd_tx_done(ndev);
1173 }
1174 return IRQ_HANDLED;
1175 }
1176
1177 static void rcar_canfd_set_bittiming(struct net_device *dev)
1178 {
1179 struct rcar_canfd_channel *priv = netdev_priv(dev);
1180 const struct can_bittiming *bt = &priv->can.bittiming;
1181 const struct can_bittiming *dbt = &priv->can.data_bittiming;
1182 u16 brp, sjw, tseg1, tseg2;
1183 u32 cfg;
1184 u32 ch = priv->channel;
1185
1186
1187 brp = bt->brp - 1;
1188 sjw = bt->sjw - 1;
1189 tseg1 = bt->prop_seg + bt->phase_seg1 - 1;
1190 tseg2 = bt->phase_seg2 - 1;
1191
1192 if (priv->can.ctrlmode & CAN_CTRLMODE_FD) {
1193
1194 cfg = (RCANFD_NCFG_NTSEG1(tseg1) | RCANFD_NCFG_NBRP(brp) |
1195 RCANFD_NCFG_NSJW(sjw) | RCANFD_NCFG_NTSEG2(tseg2));
1196
1197 rcar_canfd_write(priv->base, RCANFD_CCFG(ch), cfg);
1198 netdev_dbg(priv->ndev, "nrate: brp %u, sjw %u, tseg1 %u, tseg2 %u\n",
1199 brp, sjw, tseg1, tseg2);
1200
1201
1202 brp = dbt->brp - 1;
1203 sjw = dbt->sjw - 1;
1204 tseg1 = dbt->prop_seg + dbt->phase_seg1 - 1;
1205 tseg2 = dbt->phase_seg2 - 1;
1206
1207 cfg = (RCANFD_DCFG_DTSEG1(tseg1) | RCANFD_DCFG_DBRP(brp) |
1208 RCANFD_DCFG_DSJW(sjw) | RCANFD_DCFG_DTSEG2(tseg2));
1209
1210 rcar_canfd_write(priv->base, RCANFD_F_DCFG(ch), cfg);
1211 netdev_dbg(priv->ndev, "drate: brp %u, sjw %u, tseg1 %u, tseg2 %u\n",
1212 brp, sjw, tseg1, tseg2);
1213 } else {
1214
1215 cfg = (RCANFD_CFG_TSEG1(tseg1) | RCANFD_CFG_BRP(brp) |
1216 RCANFD_CFG_SJW(sjw) | RCANFD_CFG_TSEG2(tseg2));
1217
1218 rcar_canfd_write(priv->base, RCANFD_CCFG(ch), cfg);
1219 netdev_dbg(priv->ndev,
1220 "rate: brp %u, sjw %u, tseg1 %u, tseg2 %u\n",
1221 brp, sjw, tseg1, tseg2);
1222 }
1223 }
1224
1225 static int rcar_canfd_start(struct net_device *ndev)
1226 {
1227 struct rcar_canfd_channel *priv = netdev_priv(ndev);
1228 int err = -EOPNOTSUPP;
1229 u32 sts, ch = priv->channel;
1230 u32 ridx = ch + RCANFD_RFFIFO_IDX;
1231
1232 rcar_canfd_set_bittiming(ndev);
1233
1234 rcar_canfd_enable_channel_interrupts(priv);
1235
1236
1237 rcar_canfd_update_bit(priv->base, RCANFD_CCTR(ch),
1238 RCANFD_CCTR_CHMDC_MASK, RCANFD_CCTR_CHDMC_COPM);
1239
1240
1241 err = readl_poll_timeout((priv->base + RCANFD_CSTS(ch)), sts,
1242 (sts & RCANFD_CSTS_COMSTS), 2, 500000);
1243 if (err) {
1244 netdev_err(ndev, "channel %u communication state failed\n", ch);
1245 goto fail_mode_change;
1246 }
1247
1248
1249 rcar_canfd_set_bit(priv->base, RCANFD_CFCC(ch, RCANFD_CFFIFO_IDX),
1250 RCANFD_CFCC_CFE);
1251 rcar_canfd_set_bit(priv->base, RCANFD_RFCC(ridx), RCANFD_RFCC_RFE);
1252
1253 priv->can.state = CAN_STATE_ERROR_ACTIVE;
1254 return 0;
1255
1256 fail_mode_change:
1257 rcar_canfd_disable_channel_interrupts(priv);
1258 return err;
1259 }
1260
1261 static int rcar_canfd_open(struct net_device *ndev)
1262 {
1263 struct rcar_canfd_channel *priv = netdev_priv(ndev);
1264 struct rcar_canfd_global *gpriv = priv->gpriv;
1265 int err;
1266
1267
1268 err = clk_prepare_enable(gpriv->can_clk);
1269 if (err) {
1270 netdev_err(ndev, "failed to enable CAN clock, error %d\n", err);
1271 goto out_clock;
1272 }
1273
1274 err = open_candev(ndev);
1275 if (err) {
1276 netdev_err(ndev, "open_candev() failed, error %d\n", err);
1277 goto out_can_clock;
1278 }
1279
1280 napi_enable(&priv->napi);
1281 err = rcar_canfd_start(ndev);
1282 if (err)
1283 goto out_close;
1284 netif_start_queue(ndev);
1285 can_led_event(ndev, CAN_LED_EVENT_OPEN);
1286 return 0;
1287 out_close:
1288 napi_disable(&priv->napi);
1289 close_candev(ndev);
1290 out_can_clock:
1291 clk_disable_unprepare(gpriv->can_clk);
1292 out_clock:
1293 return err;
1294 }
1295
1296 static void rcar_canfd_stop(struct net_device *ndev)
1297 {
1298 struct rcar_canfd_channel *priv = netdev_priv(ndev);
1299 int err;
1300 u32 sts, ch = priv->channel;
1301 u32 ridx = ch + RCANFD_RFFIFO_IDX;
1302
1303
1304 rcar_canfd_update_bit(priv->base, RCANFD_CCTR(ch),
1305 RCANFD_CCTR_CHMDC_MASK, RCANFD_CCTR_CHDMC_CRESET);
1306
1307
1308 err = readl_poll_timeout((priv->base + RCANFD_CSTS(ch)), sts,
1309 (sts & RCANFD_CSTS_CRSTSTS), 2, 500000);
1310 if (err)
1311 netdev_err(ndev, "channel %u reset failed\n", ch);
1312
1313 rcar_canfd_disable_channel_interrupts(priv);
1314
1315
1316 rcar_canfd_clear_bit(priv->base, RCANFD_CFCC(ch, RCANFD_CFFIFO_IDX),
1317 RCANFD_CFCC_CFE);
1318 rcar_canfd_clear_bit(priv->base, RCANFD_RFCC(ridx), RCANFD_RFCC_RFE);
1319
1320
1321 priv->can.state = CAN_STATE_STOPPED;
1322 }
1323
1324 static int rcar_canfd_close(struct net_device *ndev)
1325 {
1326 struct rcar_canfd_channel *priv = netdev_priv(ndev);
1327 struct rcar_canfd_global *gpriv = priv->gpriv;
1328
1329 netif_stop_queue(ndev);
1330 rcar_canfd_stop(ndev);
1331 napi_disable(&priv->napi);
1332 clk_disable_unprepare(gpriv->can_clk);
1333 close_candev(ndev);
1334 can_led_event(ndev, CAN_LED_EVENT_STOP);
1335 return 0;
1336 }
1337
1338 static netdev_tx_t rcar_canfd_start_xmit(struct sk_buff *skb,
1339 struct net_device *ndev)
1340 {
1341 struct rcar_canfd_channel *priv = netdev_priv(ndev);
1342 struct canfd_frame *cf = (struct canfd_frame *)skb->data;
1343 u32 sts = 0, id, dlc;
1344 unsigned long flags;
1345 u32 ch = priv->channel;
1346
1347 if (can_dropped_invalid_skb(ndev, skb))
1348 return NETDEV_TX_OK;
1349
1350 if (cf->can_id & CAN_EFF_FLAG) {
1351 id = cf->can_id & CAN_EFF_MASK;
1352 id |= RCANFD_CFID_CFIDE;
1353 } else {
1354 id = cf->can_id & CAN_SFF_MASK;
1355 }
1356
1357 if (cf->can_id & CAN_RTR_FLAG)
1358 id |= RCANFD_CFID_CFRTR;
1359
1360 dlc = RCANFD_CFPTR_CFDLC(can_len2dlc(cf->len));
1361
1362 if (priv->can.ctrlmode & CAN_CTRLMODE_FD) {
1363 rcar_canfd_write(priv->base,
1364 RCANFD_F_CFID(ch, RCANFD_CFFIFO_IDX), id);
1365 rcar_canfd_write(priv->base,
1366 RCANFD_F_CFPTR(ch, RCANFD_CFFIFO_IDX), dlc);
1367
1368 if (can_is_canfd_skb(skb)) {
1369
1370 sts |= RCANFD_CFFDCSTS_CFFDF;
1371 if (cf->flags & CANFD_BRS)
1372 sts |= RCANFD_CFFDCSTS_CFBRS;
1373
1374 if (priv->can.state == CAN_STATE_ERROR_PASSIVE)
1375 sts |= RCANFD_CFFDCSTS_CFESI;
1376 }
1377
1378 rcar_canfd_write(priv->base,
1379 RCANFD_F_CFFDCSTS(ch, RCANFD_CFFIFO_IDX), sts);
1380
1381 rcar_canfd_put_data(priv, cf,
1382 RCANFD_F_CFDF(ch, RCANFD_CFFIFO_IDX, 0));
1383 } else {
1384 rcar_canfd_write(priv->base,
1385 RCANFD_C_CFID(ch, RCANFD_CFFIFO_IDX), id);
1386 rcar_canfd_write(priv->base,
1387 RCANFD_C_CFPTR(ch, RCANFD_CFFIFO_IDX), dlc);
1388 rcar_canfd_put_data(priv, cf,
1389 RCANFD_C_CFDF(ch, RCANFD_CFFIFO_IDX, 0));
1390 }
1391
1392 priv->tx_len[priv->tx_head % RCANFD_FIFO_DEPTH] = cf->len;
1393 can_put_echo_skb(skb, ndev, priv->tx_head % RCANFD_FIFO_DEPTH);
1394
1395 spin_lock_irqsave(&priv->tx_lock, flags);
1396 priv->tx_head++;
1397
1398
1399 if (priv->tx_head - priv->tx_tail >= RCANFD_FIFO_DEPTH)
1400 netif_stop_queue(ndev);
1401
1402
1403
1404
1405 rcar_canfd_write(priv->base,
1406 RCANFD_CFPCTR(ch, RCANFD_CFFIFO_IDX), 0xff);
1407
1408 spin_unlock_irqrestore(&priv->tx_lock, flags);
1409 return NETDEV_TX_OK;
1410 }
1411
1412 static void rcar_canfd_rx_pkt(struct rcar_canfd_channel *priv)
1413 {
1414 struct net_device_stats *stats = &priv->ndev->stats;
1415 struct canfd_frame *cf;
1416 struct sk_buff *skb;
1417 u32 sts = 0, id, dlc;
1418 u32 ch = priv->channel;
1419 u32 ridx = ch + RCANFD_RFFIFO_IDX;
1420
1421 if (priv->can.ctrlmode & CAN_CTRLMODE_FD) {
1422 id = rcar_canfd_read(priv->base, RCANFD_F_RFID(ridx));
1423 dlc = rcar_canfd_read(priv->base, RCANFD_F_RFPTR(ridx));
1424
1425 sts = rcar_canfd_read(priv->base, RCANFD_F_RFFDSTS(ridx));
1426 if (sts & RCANFD_RFFDSTS_RFFDF)
1427 skb = alloc_canfd_skb(priv->ndev, &cf);
1428 else
1429 skb = alloc_can_skb(priv->ndev,
1430 (struct can_frame **)&cf);
1431 } else {
1432 id = rcar_canfd_read(priv->base, RCANFD_C_RFID(ridx));
1433 dlc = rcar_canfd_read(priv->base, RCANFD_C_RFPTR(ridx));
1434 skb = alloc_can_skb(priv->ndev, (struct can_frame **)&cf);
1435 }
1436
1437 if (!skb) {
1438 stats->rx_dropped++;
1439 return;
1440 }
1441
1442 if (id & RCANFD_RFID_RFIDE)
1443 cf->can_id = (id & CAN_EFF_MASK) | CAN_EFF_FLAG;
1444 else
1445 cf->can_id = id & CAN_SFF_MASK;
1446
1447 if (priv->can.ctrlmode & CAN_CTRLMODE_FD) {
1448 if (sts & RCANFD_RFFDSTS_RFFDF)
1449 cf->len = can_dlc2len(RCANFD_RFPTR_RFDLC(dlc));
1450 else
1451 cf->len = get_can_dlc(RCANFD_RFPTR_RFDLC(dlc));
1452
1453 if (sts & RCANFD_RFFDSTS_RFESI) {
1454 cf->flags |= CANFD_ESI;
1455 netdev_dbg(priv->ndev, "ESI Error\n");
1456 }
1457
1458 if (!(sts & RCANFD_RFFDSTS_RFFDF) && (id & RCANFD_RFID_RFRTR)) {
1459 cf->can_id |= CAN_RTR_FLAG;
1460 } else {
1461 if (sts & RCANFD_RFFDSTS_RFBRS)
1462 cf->flags |= CANFD_BRS;
1463
1464 rcar_canfd_get_data(priv, cf, RCANFD_F_RFDF(ridx, 0));
1465 }
1466 } else {
1467 cf->len = get_can_dlc(RCANFD_RFPTR_RFDLC(dlc));
1468 if (id & RCANFD_RFID_RFRTR)
1469 cf->can_id |= CAN_RTR_FLAG;
1470 else
1471 rcar_canfd_get_data(priv, cf, RCANFD_C_RFDF(ridx, 0));
1472 }
1473
1474
1475
1476
1477 rcar_canfd_write(priv->base, RCANFD_RFPCTR(ridx), 0xff);
1478
1479 can_led_event(priv->ndev, CAN_LED_EVENT_RX);
1480
1481 stats->rx_bytes += cf->len;
1482 stats->rx_packets++;
1483 netif_receive_skb(skb);
1484 }
1485
1486 static int rcar_canfd_rx_poll(struct napi_struct *napi, int quota)
1487 {
1488 struct rcar_canfd_channel *priv =
1489 container_of(napi, struct rcar_canfd_channel, napi);
1490 int num_pkts;
1491 u32 sts;
1492 u32 ch = priv->channel;
1493 u32 ridx = ch + RCANFD_RFFIFO_IDX;
1494
1495 for (num_pkts = 0; num_pkts < quota; num_pkts++) {
1496 sts = rcar_canfd_read(priv->base, RCANFD_RFSTS(ridx));
1497
1498 if (sts & RCANFD_RFSTS_RFEMP)
1499 break;
1500
1501 rcar_canfd_rx_pkt(priv);
1502
1503
1504 if (sts & RCANFD_RFSTS_RFIF)
1505 rcar_canfd_write(priv->base, RCANFD_RFSTS(ridx),
1506 sts & ~RCANFD_RFSTS_RFIF);
1507 }
1508
1509
1510 if (num_pkts < quota) {
1511 if (napi_complete_done(napi, num_pkts)) {
1512
1513 rcar_canfd_set_bit(priv->base, RCANFD_RFCC(ridx),
1514 RCANFD_RFCC_RFIE);
1515 }
1516 }
1517 return num_pkts;
1518 }
1519
1520 static int rcar_canfd_do_set_mode(struct net_device *ndev, enum can_mode mode)
1521 {
1522 int err;
1523
1524 switch (mode) {
1525 case CAN_MODE_START:
1526 err = rcar_canfd_start(ndev);
1527 if (err)
1528 return err;
1529 netif_wake_queue(ndev);
1530 return 0;
1531 default:
1532 return -EOPNOTSUPP;
1533 }
1534 }
1535
1536 static int rcar_canfd_get_berr_counter(const struct net_device *dev,
1537 struct can_berr_counter *bec)
1538 {
1539 struct rcar_canfd_channel *priv = netdev_priv(dev);
1540 u32 val, ch = priv->channel;
1541
1542
1543 val = rcar_canfd_read(priv->base, RCANFD_CSTS(ch));
1544 bec->txerr = RCANFD_CSTS_TECCNT(val);
1545 bec->rxerr = RCANFD_CSTS_RECCNT(val);
1546 return 0;
1547 }
1548
1549 static const struct net_device_ops rcar_canfd_netdev_ops = {
1550 .ndo_open = rcar_canfd_open,
1551 .ndo_stop = rcar_canfd_close,
1552 .ndo_start_xmit = rcar_canfd_start_xmit,
1553 .ndo_change_mtu = can_change_mtu,
1554 };
1555
1556 static int rcar_canfd_channel_probe(struct rcar_canfd_global *gpriv, u32 ch,
1557 u32 fcan_freq)
1558 {
1559 struct platform_device *pdev = gpriv->pdev;
1560 struct rcar_canfd_channel *priv;
1561 struct net_device *ndev;
1562 int err = -ENODEV;
1563
1564 ndev = alloc_candev(sizeof(*priv), RCANFD_FIFO_DEPTH);
1565 if (!ndev) {
1566 dev_err(&pdev->dev, "alloc_candev() failed\n");
1567 err = -ENOMEM;
1568 goto fail;
1569 }
1570 priv = netdev_priv(ndev);
1571
1572 ndev->netdev_ops = &rcar_canfd_netdev_ops;
1573 ndev->flags |= IFF_ECHO;
1574 priv->ndev = ndev;
1575 priv->base = gpriv->base;
1576 priv->channel = ch;
1577 priv->can.clock.freq = fcan_freq;
1578 dev_info(&pdev->dev, "can_clk rate is %u\n", priv->can.clock.freq);
1579
1580 if (gpriv->fdmode) {
1581 priv->can.bittiming_const = &rcar_canfd_nom_bittiming_const;
1582 priv->can.data_bittiming_const =
1583 &rcar_canfd_data_bittiming_const;
1584
1585
1586 can_set_static_ctrlmode(ndev, CAN_CTRLMODE_FD);
1587 priv->can.ctrlmode_supported = CAN_CTRLMODE_BERR_REPORTING;
1588 } else {
1589
1590 priv->can.bittiming_const = &rcar_canfd_bittiming_const;
1591 priv->can.ctrlmode_supported = CAN_CTRLMODE_BERR_REPORTING;
1592 }
1593
1594 priv->can.do_set_mode = rcar_canfd_do_set_mode;
1595 priv->can.do_get_berr_counter = rcar_canfd_get_berr_counter;
1596 priv->gpriv = gpriv;
1597 SET_NETDEV_DEV(ndev, &pdev->dev);
1598
1599 netif_napi_add(ndev, &priv->napi, rcar_canfd_rx_poll,
1600 RCANFD_NAPI_WEIGHT);
1601 err = register_candev(ndev);
1602 if (err) {
1603 dev_err(&pdev->dev,
1604 "register_candev() failed, error %d\n", err);
1605 goto fail_candev;
1606 }
1607 spin_lock_init(&priv->tx_lock);
1608 devm_can_led_init(ndev);
1609 gpriv->ch[priv->channel] = priv;
1610 dev_info(&pdev->dev, "device registered (channel %u)\n", priv->channel);
1611 return 0;
1612
1613 fail_candev:
1614 netif_napi_del(&priv->napi);
1615 free_candev(ndev);
1616 fail:
1617 return err;
1618 }
1619
1620 static void rcar_canfd_channel_remove(struct rcar_canfd_global *gpriv, u32 ch)
1621 {
1622 struct rcar_canfd_channel *priv = gpriv->ch[ch];
1623
1624 if (priv) {
1625 unregister_candev(priv->ndev);
1626 netif_napi_del(&priv->napi);
1627 free_candev(priv->ndev);
1628 }
1629 }
1630
1631 static int rcar_canfd_probe(struct platform_device *pdev)
1632 {
1633 struct resource *mem;
1634 void __iomem *addr;
1635 u32 sts, ch, fcan_freq;
1636 struct rcar_canfd_global *gpriv;
1637 struct device_node *of_child;
1638 unsigned long channels_mask = 0;
1639 int err, ch_irq, g_irq;
1640 bool fdmode = true;
1641
1642 if (of_property_read_bool(pdev->dev.of_node, "renesas,no-can-fd"))
1643 fdmode = false;
1644
1645 of_child = of_get_child_by_name(pdev->dev.of_node, "channel0");
1646 if (of_child && of_device_is_available(of_child))
1647 channels_mask |= BIT(0);
1648
1649 of_child = of_get_child_by_name(pdev->dev.of_node, "channel1");
1650 if (of_child && of_device_is_available(of_child))
1651 channels_mask |= BIT(1);
1652
1653 ch_irq = platform_get_irq(pdev, 0);
1654 if (ch_irq < 0) {
1655 err = ch_irq;
1656 goto fail_dev;
1657 }
1658
1659 g_irq = platform_get_irq(pdev, 1);
1660 if (g_irq < 0) {
1661 err = g_irq;
1662 goto fail_dev;
1663 }
1664
1665
1666 gpriv = devm_kzalloc(&pdev->dev, sizeof(*gpriv), GFP_KERNEL);
1667 if (!gpriv) {
1668 err = -ENOMEM;
1669 goto fail_dev;
1670 }
1671 gpriv->pdev = pdev;
1672 gpriv->channels_mask = channels_mask;
1673 gpriv->fdmode = fdmode;
1674
1675
1676 gpriv->clkp = devm_clk_get(&pdev->dev, "fck");
1677 if (IS_ERR(gpriv->clkp)) {
1678 err = PTR_ERR(gpriv->clkp);
1679 dev_err(&pdev->dev, "cannot get peripheral clock, error %d\n",
1680 err);
1681 goto fail_dev;
1682 }
1683
1684
1685
1686
1687 gpriv->can_clk = devm_clk_get(&pdev->dev, "can_clk");
1688 if (IS_ERR(gpriv->can_clk) || (clk_get_rate(gpriv->can_clk) == 0)) {
1689 gpriv->can_clk = devm_clk_get(&pdev->dev, "canfd");
1690 if (IS_ERR(gpriv->can_clk)) {
1691 err = PTR_ERR(gpriv->can_clk);
1692 dev_err(&pdev->dev,
1693 "cannot get canfd clock, error %d\n", err);
1694 goto fail_dev;
1695 }
1696 gpriv->fcan = RCANFD_CANFDCLK;
1697
1698 } else {
1699 gpriv->fcan = RCANFD_EXTCLK;
1700 }
1701 fcan_freq = clk_get_rate(gpriv->can_clk);
1702
1703 if (gpriv->fcan == RCANFD_CANFDCLK)
1704
1705 fcan_freq /= 2;
1706
1707 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1708 addr = devm_ioremap_resource(&pdev->dev, mem);
1709 if (IS_ERR(addr)) {
1710 err = PTR_ERR(addr);
1711 goto fail_dev;
1712 }
1713 gpriv->base = addr;
1714
1715
1716 err = devm_request_irq(&pdev->dev, ch_irq,
1717 rcar_canfd_channel_interrupt, 0,
1718 "canfd.chn", gpriv);
1719 if (err) {
1720 dev_err(&pdev->dev, "devm_request_irq(%d) failed, error %d\n",
1721 ch_irq, err);
1722 goto fail_dev;
1723 }
1724 err = devm_request_irq(&pdev->dev, g_irq,
1725 rcar_canfd_global_interrupt, 0,
1726 "canfd.gbl", gpriv);
1727 if (err) {
1728 dev_err(&pdev->dev, "devm_request_irq(%d) failed, error %d\n",
1729 g_irq, err);
1730 goto fail_dev;
1731 }
1732
1733
1734 err = clk_prepare_enable(gpriv->clkp);
1735 if (err) {
1736 dev_err(&pdev->dev,
1737 "failed to enable peripheral clock, error %d\n", err);
1738 goto fail_dev;
1739 }
1740
1741 err = rcar_canfd_reset_controller(gpriv);
1742 if (err) {
1743 dev_err(&pdev->dev, "reset controller failed\n");
1744 goto fail_clk;
1745 }
1746
1747
1748 rcar_canfd_configure_controller(gpriv);
1749
1750
1751 for_each_set_bit(ch, &gpriv->channels_mask, RCANFD_NUM_CHANNELS) {
1752
1753 rcar_canfd_configure_rx(gpriv, ch);
1754
1755
1756 rcar_canfd_configure_tx(gpriv, ch);
1757
1758
1759 rcar_canfd_configure_afl_rules(gpriv, ch);
1760 }
1761
1762
1763 rcar_canfd_enable_global_interrupts(gpriv);
1764
1765
1766 rcar_canfd_update_bit(gpriv->base, RCANFD_GCTR, RCANFD_GCTR_GMDC_MASK,
1767 RCANFD_GCTR_GMDC_GOPM);
1768
1769
1770 err = readl_poll_timeout((gpriv->base + RCANFD_GSTS), sts,
1771 !(sts & RCANFD_GSTS_GNOPM), 2, 500000);
1772 if (err) {
1773 dev_err(&pdev->dev, "global operational mode failed\n");
1774 goto fail_mode;
1775 }
1776
1777 for_each_set_bit(ch, &gpriv->channels_mask, RCANFD_NUM_CHANNELS) {
1778 err = rcar_canfd_channel_probe(gpriv, ch, fcan_freq);
1779 if (err)
1780 goto fail_channel;
1781 }
1782
1783 platform_set_drvdata(pdev, gpriv);
1784 dev_info(&pdev->dev, "global operational state (clk %d, fdmode %d)\n",
1785 gpriv->fcan, gpriv->fdmode);
1786 return 0;
1787
1788 fail_channel:
1789 for_each_set_bit(ch, &gpriv->channels_mask, RCANFD_NUM_CHANNELS)
1790 rcar_canfd_channel_remove(gpriv, ch);
1791 fail_mode:
1792 rcar_canfd_disable_global_interrupts(gpriv);
1793 fail_clk:
1794 clk_disable_unprepare(gpriv->clkp);
1795 fail_dev:
1796 return err;
1797 }
1798
1799 static int rcar_canfd_remove(struct platform_device *pdev)
1800 {
1801 struct rcar_canfd_global *gpriv = platform_get_drvdata(pdev);
1802 u32 ch;
1803
1804 rcar_canfd_reset_controller(gpriv);
1805 rcar_canfd_disable_global_interrupts(gpriv);
1806
1807 for_each_set_bit(ch, &gpriv->channels_mask, RCANFD_NUM_CHANNELS) {
1808 rcar_canfd_disable_channel_interrupts(gpriv->ch[ch]);
1809 rcar_canfd_channel_remove(gpriv, ch);
1810 }
1811
1812
1813 rcar_canfd_set_bit(gpriv->base, RCANFD_GCTR, RCANFD_GCTR_GSLPR);
1814 clk_disable_unprepare(gpriv->clkp);
1815 return 0;
1816 }
1817
1818 static int __maybe_unused rcar_canfd_suspend(struct device *dev)
1819 {
1820 return 0;
1821 }
1822
1823 static int __maybe_unused rcar_canfd_resume(struct device *dev)
1824 {
1825 return 0;
1826 }
1827
1828 static SIMPLE_DEV_PM_OPS(rcar_canfd_pm_ops, rcar_canfd_suspend,
1829 rcar_canfd_resume);
1830
1831 static const struct of_device_id rcar_canfd_of_table[] = {
1832 { .compatible = "renesas,rcar-gen3-canfd" },
1833 { }
1834 };
1835
1836 MODULE_DEVICE_TABLE(of, rcar_canfd_of_table);
1837
1838 static struct platform_driver rcar_canfd_driver = {
1839 .driver = {
1840 .name = RCANFD_DRV_NAME,
1841 .of_match_table = of_match_ptr(rcar_canfd_of_table),
1842 .pm = &rcar_canfd_pm_ops,
1843 },
1844 .probe = rcar_canfd_probe,
1845 .remove = rcar_canfd_remove,
1846 };
1847
1848 module_platform_driver(rcar_canfd_driver);
1849
1850 MODULE_AUTHOR("Ramesh Shanmugasundaram <ramesh.shanmugasundaram@bp.renesas.com>");
1851 MODULE_LICENSE("GPL");
1852 MODULE_DESCRIPTION("CAN FD driver for Renesas R-Car SoC");
1853 MODULE_ALIAS("platform:" RCANFD_DRV_NAME);