1
2
3
4
5
6
7
8#include <linux/module.h>
9#include <linux/kernel.h>
10#include <linux/types.h>
11#include <linux/interrupt.h>
12#include <linux/errno.h>
13#include <linux/netdevice.h>
14#include <linux/platform_device.h>
15#include <linux/can/dev.h>
16#include <linux/clk.h>
17#include <linux/of.h>
18
19#define RCAR_CAN_DRV_NAME "rcar_can"
20
21
22enum CLKR {
23 CLKR_CLKP1 = 0,
24 CLKR_CLKP2 = 1,
25 CLKR_CLKEXT = 3,
26};
27
28#define RCAR_SUPPORTED_CLOCKS (BIT(CLKR_CLKP1) | BIT(CLKR_CLKP2) | \
29 BIT(CLKR_CLKEXT))
30
31
32
33
34
35
36#define RCAR_CAN_N_MBX 64
37#define RCAR_CAN_RX_FIFO_MBX 60
38#define RCAR_CAN_TX_FIFO_MBX 56
39#define RCAR_CAN_FIFO_DEPTH 4
40
41
42struct rcar_can_mbox_regs {
43 u32 id;
44 u8 stub;
45 u8 dlc;
46 u8 data[8];
47 u8 tsh;
48 u8 tsl;
49};
50
51struct rcar_can_regs {
52 struct rcar_can_mbox_regs mb[RCAR_CAN_N_MBX];
53 u32 mkr_2_9[8];
54 u32 fidcr[2];
55 u32 mkivlr1;
56 u32 mier1;
57 u32 mkr_0_1[2];
58 u32 mkivlr0;
59 u32 mier0;
60 u8 pad_440[0x3c0];
61 u8 mctl[64];
62 u16 ctlr;
63 u16 str;
64 u8 bcr[3];
65 u8 clkr;
66 u8 rfcr;
67 u8 rfpcr;
68 u8 tfcr;
69 u8 tfpcr;
70 u8 eier;
71 u8 eifr;
72 u8 recr;
73 u8 tecr;
74 u8 ecsr;
75 u8 cssr;
76 u8 mssr;
77 u8 msmr;
78 u16 tsr;
79 u8 afsr;
80 u8 pad_857;
81 u8 tcr;
82 u8 pad_859[7];
83 u8 ier;
84 u8 isr;
85 u8 pad_862;
86 u8 mbsmr;
87};
88
89struct rcar_can_priv {
90 struct can_priv can;
91 struct net_device *ndev;
92 struct napi_struct napi;
93 struct rcar_can_regs __iomem *regs;
94 struct clk *clk;
95 struct clk *can_clk;
96 u32 tx_head;
97 u32 tx_tail;
98 u8 clock_select;
99 u8 ier;
100};
101
102static const struct can_bittiming_const rcar_can_bittiming_const = {
103 .name = RCAR_CAN_DRV_NAME,
104 .tseg1_min = 4,
105 .tseg1_max = 16,
106 .tseg2_min = 2,
107 .tseg2_max = 8,
108 .sjw_max = 4,
109 .brp_min = 1,
110 .brp_max = 1024,
111 .brp_inc = 1,
112};
113
114
115#define RCAR_CAN_CTLR_BOM (3 << 11)
116#define RCAR_CAN_CTLR_BOM_ENT (1 << 11)
117
118#define RCAR_CAN_CTLR_SLPM (1 << 10)
119#define RCAR_CAN_CTLR_CANM (3 << 8)
120#define RCAR_CAN_CTLR_CANM_HALT (1 << 9)
121#define RCAR_CAN_CTLR_CANM_RESET (1 << 8)
122#define RCAR_CAN_CTLR_CANM_FORCE_RESET (3 << 8)
123#define RCAR_CAN_CTLR_MLM (1 << 3)
124#define RCAR_CAN_CTLR_IDFM (3 << 1)
125#define RCAR_CAN_CTLR_IDFM_MIXED (1 << 2)
126#define RCAR_CAN_CTLR_MBM (1 << 0)
127
128
129#define RCAR_CAN_STR_RSTST (1 << 8)
130
131
132#define RCAR_CAN_FIDCR_IDE (1 << 31)
133#define RCAR_CAN_FIDCR_RTR (1 << 30)
134
135
136#define RCAR_CAN_RFCR_RFEST (1 << 7)
137#define RCAR_CAN_RFCR_RFE (1 << 0)
138
139
140#define RCAR_CAN_TFCR_TFUST (7 << 1)
141
142#define RCAR_CAN_TFCR_TFUST_SHIFT 1
143
144#define RCAR_CAN_TFCR_TFE (1 << 0)
145
146#define RCAR_CAN_N_RX_MKREGS1 2
147
148#define RCAR_CAN_N_RX_MKREGS2 8
149
150
151#define RCAR_CAN_BCR_TSEG1(x) (((x) & 0x0f) << 20)
152#define RCAR_CAN_BCR_BPR(x) (((x) & 0x3ff) << 8)
153#define RCAR_CAN_BCR_SJW(x) (((x) & 0x3) << 4)
154#define RCAR_CAN_BCR_TSEG2(x) ((x) & 0x07)
155
156
157#define RCAR_CAN_IDE (1 << 31)
158#define RCAR_CAN_RTR (1 << 30)
159#define RCAR_CAN_SID_SHIFT 18
160
161
162#define RCAR_CAN_MIER1_RXFIE (1 << 28)
163#define RCAR_CAN_MIER1_TXFIE (1 << 24)
164
165
166#define RCAR_CAN_IER_ERSIE (1 << 5)
167#define RCAR_CAN_IER_RXFIE (1 << 4)
168
169#define RCAR_CAN_IER_TXFIE (1 << 3)
170
171
172#define RCAR_CAN_ISR_ERSF (1 << 5)
173#define RCAR_CAN_ISR_RXFF (1 << 4)
174
175#define RCAR_CAN_ISR_TXFF (1 << 3)
176
177
178
179#define RCAR_CAN_EIER_BLIE (1 << 7)
180#define RCAR_CAN_EIER_OLIE (1 << 6)
181
182#define RCAR_CAN_EIER_ORIE (1 << 5)
183#define RCAR_CAN_EIER_BORIE (1 << 4)
184#define RCAR_CAN_EIER_BOEIE (1 << 3)
185#define RCAR_CAN_EIER_EPIE (1 << 2)
186#define RCAR_CAN_EIER_EWIE (1 << 1)
187#define RCAR_CAN_EIER_BEIE (1 << 0)
188
189
190#define RCAR_CAN_EIFR_BLIF (1 << 7)
191#define RCAR_CAN_EIFR_OLIF (1 << 6)
192
193#define RCAR_CAN_EIFR_ORIF (1 << 5)
194#define RCAR_CAN_EIFR_BORIF (1 << 4)
195#define RCAR_CAN_EIFR_BOEIF (1 << 3)
196#define RCAR_CAN_EIFR_EPIF (1 << 2)
197#define RCAR_CAN_EIFR_EWIF (1 << 1)
198#define RCAR_CAN_EIFR_BEIF (1 << 0)
199
200
201#define RCAR_CAN_ECSR_EDPM (1 << 7)
202#define RCAR_CAN_ECSR_ADEF (1 << 6)
203#define RCAR_CAN_ECSR_BE0F (1 << 5)
204#define RCAR_CAN_ECSR_BE1F (1 << 4)
205#define RCAR_CAN_ECSR_CEF (1 << 3)
206#define RCAR_CAN_ECSR_AEF (1 << 2)
207#define RCAR_CAN_ECSR_FEF (1 << 1)
208#define RCAR_CAN_ECSR_SEF (1 << 0)
209
210#define RCAR_CAN_NAPI_WEIGHT 4
211#define MAX_STR_READS 0x100
212
213static void tx_failure_cleanup(struct net_device *ndev)
214{
215 int i;
216
217 for (i = 0; i < RCAR_CAN_FIFO_DEPTH; i++)
218 can_free_echo_skb(ndev, i, NULL);
219}
220
221static void rcar_can_error(struct net_device *ndev)
222{
223 struct rcar_can_priv *priv = netdev_priv(ndev);
224 struct can_frame *cf;
225 struct sk_buff *skb;
226 u8 eifr, txerr = 0, rxerr = 0;
227
228
229 skb = alloc_can_err_skb(ndev, &cf);
230
231 eifr = readb(&priv->regs->eifr);
232 if (eifr & (RCAR_CAN_EIFR_EWIF | RCAR_CAN_EIFR_EPIF)) {
233 txerr = readb(&priv->regs->tecr);
234 rxerr = readb(&priv->regs->recr);
235 if (skb) {
236 cf->can_id |= CAN_ERR_CRTL;
237 cf->data[6] = txerr;
238 cf->data[7] = rxerr;
239 }
240 }
241 if (eifr & RCAR_CAN_EIFR_BEIF) {
242 int rx_errors = 0, tx_errors = 0;
243 u8 ecsr;
244
245 netdev_dbg(priv->ndev, "Bus error interrupt:\n");
246 if (skb)
247 cf->can_id |= CAN_ERR_BUSERROR | CAN_ERR_PROT;
248
249 ecsr = readb(&priv->regs->ecsr);
250 if (ecsr & RCAR_CAN_ECSR_ADEF) {
251 netdev_dbg(priv->ndev, "ACK Delimiter Error\n");
252 tx_errors++;
253 writeb(~RCAR_CAN_ECSR_ADEF, &priv->regs->ecsr);
254 if (skb)
255 cf->data[3] = CAN_ERR_PROT_LOC_ACK_DEL;
256 }
257 if (ecsr & RCAR_CAN_ECSR_BE0F) {
258 netdev_dbg(priv->ndev, "Bit Error (dominant)\n");
259 tx_errors++;
260 writeb(~RCAR_CAN_ECSR_BE0F, &priv->regs->ecsr);
261 if (skb)
262 cf->data[2] |= CAN_ERR_PROT_BIT0;
263 }
264 if (ecsr & RCAR_CAN_ECSR_BE1F) {
265 netdev_dbg(priv->ndev, "Bit Error (recessive)\n");
266 tx_errors++;
267 writeb(~RCAR_CAN_ECSR_BE1F, &priv->regs->ecsr);
268 if (skb)
269 cf->data[2] |= CAN_ERR_PROT_BIT1;
270 }
271 if (ecsr & RCAR_CAN_ECSR_CEF) {
272 netdev_dbg(priv->ndev, "CRC Error\n");
273 rx_errors++;
274 writeb(~RCAR_CAN_ECSR_CEF, &priv->regs->ecsr);
275 if (skb)
276 cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ;
277 }
278 if (ecsr & RCAR_CAN_ECSR_AEF) {
279 netdev_dbg(priv->ndev, "ACK Error\n");
280 tx_errors++;
281 writeb(~RCAR_CAN_ECSR_AEF, &priv->regs->ecsr);
282 if (skb) {
283 cf->can_id |= CAN_ERR_ACK;
284 cf->data[3] = CAN_ERR_PROT_LOC_ACK;
285 }
286 }
287 if (ecsr & RCAR_CAN_ECSR_FEF) {
288 netdev_dbg(priv->ndev, "Form Error\n");
289 rx_errors++;
290 writeb(~RCAR_CAN_ECSR_FEF, &priv->regs->ecsr);
291 if (skb)
292 cf->data[2] |= CAN_ERR_PROT_FORM;
293 }
294 if (ecsr & RCAR_CAN_ECSR_SEF) {
295 netdev_dbg(priv->ndev, "Stuff Error\n");
296 rx_errors++;
297 writeb(~RCAR_CAN_ECSR_SEF, &priv->regs->ecsr);
298 if (skb)
299 cf->data[2] |= CAN_ERR_PROT_STUFF;
300 }
301
302 priv->can.can_stats.bus_error++;
303 ndev->stats.rx_errors += rx_errors;
304 ndev->stats.tx_errors += tx_errors;
305 writeb(~RCAR_CAN_EIFR_BEIF, &priv->regs->eifr);
306 }
307 if (eifr & RCAR_CAN_EIFR_EWIF) {
308 netdev_dbg(priv->ndev, "Error warning interrupt\n");
309 priv->can.state = CAN_STATE_ERROR_WARNING;
310 priv->can.can_stats.error_warning++;
311
312 writeb(~RCAR_CAN_EIFR_EWIF, &priv->regs->eifr);
313 if (skb)
314 cf->data[1] = txerr > rxerr ? CAN_ERR_CRTL_TX_WARNING :
315 CAN_ERR_CRTL_RX_WARNING;
316 }
317 if (eifr & RCAR_CAN_EIFR_EPIF) {
318 netdev_dbg(priv->ndev, "Error passive interrupt\n");
319 priv->can.state = CAN_STATE_ERROR_PASSIVE;
320 priv->can.can_stats.error_passive++;
321
322 writeb(~RCAR_CAN_EIFR_EPIF, &priv->regs->eifr);
323 if (skb)
324 cf->data[1] = txerr > rxerr ? CAN_ERR_CRTL_TX_PASSIVE :
325 CAN_ERR_CRTL_RX_PASSIVE;
326 }
327 if (eifr & RCAR_CAN_EIFR_BOEIF) {
328 netdev_dbg(priv->ndev, "Bus-off entry interrupt\n");
329 tx_failure_cleanup(ndev);
330 priv->ier = RCAR_CAN_IER_ERSIE;
331 writeb(priv->ier, &priv->regs->ier);
332 priv->can.state = CAN_STATE_BUS_OFF;
333
334 writeb(~RCAR_CAN_EIFR_BOEIF, &priv->regs->eifr);
335 priv->can.can_stats.bus_off++;
336 can_bus_off(ndev);
337 if (skb)
338 cf->can_id |= CAN_ERR_BUSOFF;
339 }
340 if (eifr & RCAR_CAN_EIFR_ORIF) {
341 netdev_dbg(priv->ndev, "Receive overrun error interrupt\n");
342 ndev->stats.rx_over_errors++;
343 ndev->stats.rx_errors++;
344 writeb(~RCAR_CAN_EIFR_ORIF, &priv->regs->eifr);
345 if (skb) {
346 cf->can_id |= CAN_ERR_CRTL;
347 cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
348 }
349 }
350 if (eifr & RCAR_CAN_EIFR_OLIF) {
351 netdev_dbg(priv->ndev,
352 "Overload Frame Transmission error interrupt\n");
353 ndev->stats.rx_over_errors++;
354 ndev->stats.rx_errors++;
355 writeb(~RCAR_CAN_EIFR_OLIF, &priv->regs->eifr);
356 if (skb) {
357 cf->can_id |= CAN_ERR_PROT;
358 cf->data[2] |= CAN_ERR_PROT_OVERLOAD;
359 }
360 }
361
362 if (skb)
363 netif_rx(skb);
364}
365
366static void rcar_can_tx_done(struct net_device *ndev)
367{
368 struct rcar_can_priv *priv = netdev_priv(ndev);
369 struct net_device_stats *stats = &ndev->stats;
370 u8 isr;
371
372 while (1) {
373 u8 unsent = readb(&priv->regs->tfcr);
374
375 unsent = (unsent & RCAR_CAN_TFCR_TFUST) >>
376 RCAR_CAN_TFCR_TFUST_SHIFT;
377 if (priv->tx_head - priv->tx_tail <= unsent)
378 break;
379 stats->tx_packets++;
380 stats->tx_bytes +=
381 can_get_echo_skb(ndev,
382 priv->tx_tail % RCAR_CAN_FIFO_DEPTH,
383 NULL);
384
385 priv->tx_tail++;
386 netif_wake_queue(ndev);
387 }
388
389 isr = readb(&priv->regs->isr);
390 writeb(isr & ~RCAR_CAN_ISR_TXFF, &priv->regs->isr);
391}
392
393static irqreturn_t rcar_can_interrupt(int irq, void *dev_id)
394{
395 struct net_device *ndev = dev_id;
396 struct rcar_can_priv *priv = netdev_priv(ndev);
397 u8 isr;
398
399 isr = readb(&priv->regs->isr);
400 if (!(isr & priv->ier))
401 return IRQ_NONE;
402
403 if (isr & RCAR_CAN_ISR_ERSF)
404 rcar_can_error(ndev);
405
406 if (isr & RCAR_CAN_ISR_TXFF)
407 rcar_can_tx_done(ndev);
408
409 if (isr & RCAR_CAN_ISR_RXFF) {
410 if (napi_schedule_prep(&priv->napi)) {
411
412 priv->ier &= ~RCAR_CAN_IER_RXFIE;
413 writeb(priv->ier, &priv->regs->ier);
414 __napi_schedule(&priv->napi);
415 }
416 }
417
418 return IRQ_HANDLED;
419}
420
421static void rcar_can_set_bittiming(struct net_device *dev)
422{
423 struct rcar_can_priv *priv = netdev_priv(dev);
424 struct can_bittiming *bt = &priv->can.bittiming;
425 u32 bcr;
426
427 bcr = RCAR_CAN_BCR_TSEG1(bt->phase_seg1 + bt->prop_seg - 1) |
428 RCAR_CAN_BCR_BPR(bt->brp - 1) | RCAR_CAN_BCR_SJW(bt->sjw - 1) |
429 RCAR_CAN_BCR_TSEG2(bt->phase_seg2 - 1);
430
431
432
433
434 writel((bcr << 8) | priv->clock_select, &priv->regs->bcr);
435}
436
437static void rcar_can_start(struct net_device *ndev)
438{
439 struct rcar_can_priv *priv = netdev_priv(ndev);
440 u16 ctlr;
441 int i;
442
443
444
445
446
447
448
449 ctlr = readw(&priv->regs->ctlr);
450 ctlr &= ~RCAR_CAN_CTLR_SLPM;
451 writew(ctlr, &priv->regs->ctlr);
452
453 ctlr |= RCAR_CAN_CTLR_CANM_FORCE_RESET;
454 writew(ctlr, &priv->regs->ctlr);
455 for (i = 0; i < MAX_STR_READS; i++) {
456 if (readw(&priv->regs->str) & RCAR_CAN_STR_RSTST)
457 break;
458 }
459 rcar_can_set_bittiming(ndev);
460 ctlr |= RCAR_CAN_CTLR_IDFM_MIXED;
461 ctlr |= RCAR_CAN_CTLR_BOM_ENT;
462
463 ctlr |= RCAR_CAN_CTLR_MBM;
464 ctlr |= RCAR_CAN_CTLR_MLM;
465 writew(ctlr, &priv->regs->ctlr);
466
467
468 writel(0, &priv->regs->mkr_2_9[6]);
469 writel(0, &priv->regs->mkr_2_9[7]);
470
471 writel(0, &priv->regs->mkivlr1);
472
473 writel(0, &priv->regs->fidcr[0]);
474 writel(RCAR_CAN_FIDCR_IDE | RCAR_CAN_FIDCR_RTR, &priv->regs->fidcr[1]);
475
476 writel(RCAR_CAN_MIER1_RXFIE | RCAR_CAN_MIER1_TXFIE, &priv->regs->mier1);
477
478 priv->ier = RCAR_CAN_IER_ERSIE | RCAR_CAN_IER_RXFIE |
479 RCAR_CAN_IER_TXFIE;
480 writeb(priv->ier, &priv->regs->ier);
481
482
483 writeb(RCAR_CAN_ECSR_EDPM, &priv->regs->ecsr);
484
485 writeb(RCAR_CAN_EIER_EWIE | RCAR_CAN_EIER_EPIE | RCAR_CAN_EIER_BOEIE |
486 (priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING ?
487 RCAR_CAN_EIER_BEIE : 0) | RCAR_CAN_EIER_ORIE |
488 RCAR_CAN_EIER_OLIE, &priv->regs->eier);
489 priv->can.state = CAN_STATE_ERROR_ACTIVE;
490
491
492 writew(ctlr & ~RCAR_CAN_CTLR_CANM, &priv->regs->ctlr);
493 for (i = 0; i < MAX_STR_READS; i++) {
494 if (!(readw(&priv->regs->str) & RCAR_CAN_STR_RSTST))
495 break;
496 }
497
498 writeb(RCAR_CAN_RFCR_RFE, &priv->regs->rfcr);
499 writeb(RCAR_CAN_TFCR_TFE, &priv->regs->tfcr);
500}
501
502static int rcar_can_open(struct net_device *ndev)
503{
504 struct rcar_can_priv *priv = netdev_priv(ndev);
505 int err;
506
507 err = clk_prepare_enable(priv->clk);
508 if (err) {
509 netdev_err(ndev,
510 "failed to enable peripheral clock, error %d\n",
511 err);
512 goto out;
513 }
514 err = clk_prepare_enable(priv->can_clk);
515 if (err) {
516 netdev_err(ndev, "failed to enable CAN clock, error %d\n",
517 err);
518 goto out_clock;
519 }
520 err = open_candev(ndev);
521 if (err) {
522 netdev_err(ndev, "open_candev() failed, error %d\n", err);
523 goto out_can_clock;
524 }
525 napi_enable(&priv->napi);
526 err = request_irq(ndev->irq, rcar_can_interrupt, 0, ndev->name, ndev);
527 if (err) {
528 netdev_err(ndev, "request_irq(%d) failed, error %d\n",
529 ndev->irq, err);
530 goto out_close;
531 }
532 rcar_can_start(ndev);
533 netif_start_queue(ndev);
534 return 0;
535out_close:
536 napi_disable(&priv->napi);
537 close_candev(ndev);
538out_can_clock:
539 clk_disable_unprepare(priv->can_clk);
540out_clock:
541 clk_disable_unprepare(priv->clk);
542out:
543 return err;
544}
545
546static void rcar_can_stop(struct net_device *ndev)
547{
548 struct rcar_can_priv *priv = netdev_priv(ndev);
549 u16 ctlr;
550 int i;
551
552
553 ctlr = readw(&priv->regs->ctlr);
554 ctlr |= RCAR_CAN_CTLR_CANM_FORCE_RESET;
555 writew(ctlr, &priv->regs->ctlr);
556 for (i = 0; i < MAX_STR_READS; i++) {
557 if (readw(&priv->regs->str) & RCAR_CAN_STR_RSTST)
558 break;
559 }
560 writel(0, &priv->regs->mier0);
561 writel(0, &priv->regs->mier1);
562 writeb(0, &priv->regs->ier);
563 writeb(0, &priv->regs->eier);
564
565 ctlr |= RCAR_CAN_CTLR_SLPM;
566 writew(ctlr, &priv->regs->ctlr);
567 priv->can.state = CAN_STATE_STOPPED;
568}
569
570static int rcar_can_close(struct net_device *ndev)
571{
572 struct rcar_can_priv *priv = netdev_priv(ndev);
573
574 netif_stop_queue(ndev);
575 rcar_can_stop(ndev);
576 free_irq(ndev->irq, ndev);
577 napi_disable(&priv->napi);
578 clk_disable_unprepare(priv->can_clk);
579 clk_disable_unprepare(priv->clk);
580 close_candev(ndev);
581 return 0;
582}
583
584static netdev_tx_t rcar_can_start_xmit(struct sk_buff *skb,
585 struct net_device *ndev)
586{
587 struct rcar_can_priv *priv = netdev_priv(ndev);
588 struct can_frame *cf = (struct can_frame *)skb->data;
589 u32 data, i;
590
591 if (can_dropped_invalid_skb(ndev, skb))
592 return NETDEV_TX_OK;
593
594 if (cf->can_id & CAN_EFF_FLAG)
595 data = (cf->can_id & CAN_EFF_MASK) | RCAR_CAN_IDE;
596 else
597 data = (cf->can_id & CAN_SFF_MASK) << RCAR_CAN_SID_SHIFT;
598
599 if (cf->can_id & CAN_RTR_FLAG) {
600 data |= RCAR_CAN_RTR;
601 } else {
602 for (i = 0; i < cf->len; i++)
603 writeb(cf->data[i],
604 &priv->regs->mb[RCAR_CAN_TX_FIFO_MBX].data[i]);
605 }
606
607 writel(data, &priv->regs->mb[RCAR_CAN_TX_FIFO_MBX].id);
608
609 writeb(cf->len, &priv->regs->mb[RCAR_CAN_TX_FIFO_MBX].dlc);
610
611 can_put_echo_skb(skb, ndev, priv->tx_head % RCAR_CAN_FIFO_DEPTH, 0);
612 priv->tx_head++;
613
614
615
616
617 writeb(0xff, &priv->regs->tfpcr);
618
619 if (priv->tx_head - priv->tx_tail >= RCAR_CAN_FIFO_DEPTH)
620 netif_stop_queue(ndev);
621
622 return NETDEV_TX_OK;
623}
624
625static const struct net_device_ops rcar_can_netdev_ops = {
626 .ndo_open = rcar_can_open,
627 .ndo_stop = rcar_can_close,
628 .ndo_start_xmit = rcar_can_start_xmit,
629 .ndo_change_mtu = can_change_mtu,
630};
631
632static void rcar_can_rx_pkt(struct rcar_can_priv *priv)
633{
634 struct net_device_stats *stats = &priv->ndev->stats;
635 struct can_frame *cf;
636 struct sk_buff *skb;
637 u32 data;
638 u8 dlc;
639
640 skb = alloc_can_skb(priv->ndev, &cf);
641 if (!skb) {
642 stats->rx_dropped++;
643 return;
644 }
645
646 data = readl(&priv->regs->mb[RCAR_CAN_RX_FIFO_MBX].id);
647 if (data & RCAR_CAN_IDE)
648 cf->can_id = (data & CAN_EFF_MASK) | CAN_EFF_FLAG;
649 else
650 cf->can_id = (data >> RCAR_CAN_SID_SHIFT) & CAN_SFF_MASK;
651
652 dlc = readb(&priv->regs->mb[RCAR_CAN_RX_FIFO_MBX].dlc);
653 cf->len = can_cc_dlc2len(dlc);
654 if (data & RCAR_CAN_RTR) {
655 cf->can_id |= CAN_RTR_FLAG;
656 } else {
657 for (dlc = 0; dlc < cf->len; dlc++)
658 cf->data[dlc] =
659 readb(&priv->regs->mb[RCAR_CAN_RX_FIFO_MBX].data[dlc]);
660
661 stats->rx_bytes += cf->len;
662 }
663 stats->rx_packets++;
664
665 netif_receive_skb(skb);
666}
667
668static int rcar_can_rx_poll(struct napi_struct *napi, int quota)
669{
670 struct rcar_can_priv *priv = container_of(napi,
671 struct rcar_can_priv, napi);
672 int num_pkts;
673
674 for (num_pkts = 0; num_pkts < quota; num_pkts++) {
675 u8 rfcr, isr;
676
677 isr = readb(&priv->regs->isr);
678
679 if (isr & RCAR_CAN_ISR_RXFF)
680 writeb(isr & ~RCAR_CAN_ISR_RXFF, &priv->regs->isr);
681 rfcr = readb(&priv->regs->rfcr);
682 if (rfcr & RCAR_CAN_RFCR_RFEST)
683 break;
684 rcar_can_rx_pkt(priv);
685
686
687
688
689 writeb(0xff, &priv->regs->rfpcr);
690 }
691
692 if (num_pkts < quota) {
693 napi_complete_done(napi, num_pkts);
694 priv->ier |= RCAR_CAN_IER_RXFIE;
695 writeb(priv->ier, &priv->regs->ier);
696 }
697 return num_pkts;
698}
699
700static int rcar_can_do_set_mode(struct net_device *ndev, enum can_mode mode)
701{
702 switch (mode) {
703 case CAN_MODE_START:
704 rcar_can_start(ndev);
705 netif_wake_queue(ndev);
706 return 0;
707 default:
708 return -EOPNOTSUPP;
709 }
710}
711
712static int rcar_can_get_berr_counter(const struct net_device *dev,
713 struct can_berr_counter *bec)
714{
715 struct rcar_can_priv *priv = netdev_priv(dev);
716 int err;
717
718 err = clk_prepare_enable(priv->clk);
719 if (err)
720 return err;
721 bec->txerr = readb(&priv->regs->tecr);
722 bec->rxerr = readb(&priv->regs->recr);
723 clk_disable_unprepare(priv->clk);
724 return 0;
725}
726
727static const char * const clock_names[] = {
728 [CLKR_CLKP1] = "clkp1",
729 [CLKR_CLKP2] = "clkp2",
730 [CLKR_CLKEXT] = "can_clk",
731};
732
733static int rcar_can_probe(struct platform_device *pdev)
734{
735 struct rcar_can_priv *priv;
736 struct net_device *ndev;
737 void __iomem *addr;
738 u32 clock_select = CLKR_CLKP1;
739 int err = -ENODEV;
740 int irq;
741
742 of_property_read_u32(pdev->dev.of_node, "renesas,can-clock-select",
743 &clock_select);
744
745 irq = platform_get_irq(pdev, 0);
746 if (irq < 0) {
747 err = irq;
748 goto fail;
749 }
750
751 addr = devm_platform_ioremap_resource(pdev, 0);
752 if (IS_ERR(addr)) {
753 err = PTR_ERR(addr);
754 goto fail;
755 }
756
757 ndev = alloc_candev(sizeof(struct rcar_can_priv), RCAR_CAN_FIFO_DEPTH);
758 if (!ndev) {
759 dev_err(&pdev->dev, "alloc_candev() failed\n");
760 err = -ENOMEM;
761 goto fail;
762 }
763
764 priv = netdev_priv(ndev);
765
766 priv->clk = devm_clk_get(&pdev->dev, "clkp1");
767 if (IS_ERR(priv->clk)) {
768 err = PTR_ERR(priv->clk);
769 dev_err(&pdev->dev, "cannot get peripheral clock, error %d\n",
770 err);
771 goto fail_clk;
772 }
773
774 if (!(BIT(clock_select) & RCAR_SUPPORTED_CLOCKS)) {
775 err = -EINVAL;
776 dev_err(&pdev->dev, "invalid CAN clock selected\n");
777 goto fail_clk;
778 }
779 priv->can_clk = devm_clk_get(&pdev->dev, clock_names[clock_select]);
780 if (IS_ERR(priv->can_clk)) {
781 err = PTR_ERR(priv->can_clk);
782 dev_err(&pdev->dev, "cannot get CAN clock, error %d\n", err);
783 goto fail_clk;
784 }
785
786 ndev->netdev_ops = &rcar_can_netdev_ops;
787 ndev->irq = irq;
788 ndev->flags |= IFF_ECHO;
789 priv->ndev = ndev;
790 priv->regs = addr;
791 priv->clock_select = clock_select;
792 priv->can.clock.freq = clk_get_rate(priv->can_clk);
793 priv->can.bittiming_const = &rcar_can_bittiming_const;
794 priv->can.do_set_mode = rcar_can_do_set_mode;
795 priv->can.do_get_berr_counter = rcar_can_get_berr_counter;
796 priv->can.ctrlmode_supported = CAN_CTRLMODE_BERR_REPORTING;
797 platform_set_drvdata(pdev, ndev);
798 SET_NETDEV_DEV(ndev, &pdev->dev);
799
800 netif_napi_add_weight(ndev, &priv->napi, rcar_can_rx_poll,
801 RCAR_CAN_NAPI_WEIGHT);
802 err = register_candev(ndev);
803 if (err) {
804 dev_err(&pdev->dev, "register_candev() failed, error %d\n",
805 err);
806 goto fail_candev;
807 }
808
809 dev_info(&pdev->dev, "device registered (IRQ%d)\n", ndev->irq);
810
811 return 0;
812fail_candev:
813 netif_napi_del(&priv->napi);
814fail_clk:
815 free_candev(ndev);
816fail:
817 return err;
818}
819
820static int rcar_can_remove(struct platform_device *pdev)
821{
822 struct net_device *ndev = platform_get_drvdata(pdev);
823 struct rcar_can_priv *priv = netdev_priv(ndev);
824
825 unregister_candev(ndev);
826 netif_napi_del(&priv->napi);
827 free_candev(ndev);
828 return 0;
829}
830
831static int __maybe_unused rcar_can_suspend(struct device *dev)
832{
833 struct net_device *ndev = dev_get_drvdata(dev);
834 struct rcar_can_priv *priv = netdev_priv(ndev);
835 u16 ctlr;
836
837 if (!netif_running(ndev))
838 return 0;
839
840 netif_stop_queue(ndev);
841 netif_device_detach(ndev);
842
843 ctlr = readw(&priv->regs->ctlr);
844 ctlr |= RCAR_CAN_CTLR_CANM_HALT;
845 writew(ctlr, &priv->regs->ctlr);
846 ctlr |= RCAR_CAN_CTLR_SLPM;
847 writew(ctlr, &priv->regs->ctlr);
848 priv->can.state = CAN_STATE_SLEEPING;
849
850 clk_disable(priv->clk);
851 return 0;
852}
853
854static int __maybe_unused rcar_can_resume(struct device *dev)
855{
856 struct net_device *ndev = dev_get_drvdata(dev);
857 struct rcar_can_priv *priv = netdev_priv(ndev);
858 u16 ctlr;
859 int err;
860
861 if (!netif_running(ndev))
862 return 0;
863
864 err = clk_enable(priv->clk);
865 if (err) {
866 netdev_err(ndev, "clk_enable() failed, error %d\n", err);
867 return err;
868 }
869
870 ctlr = readw(&priv->regs->ctlr);
871 ctlr &= ~RCAR_CAN_CTLR_SLPM;
872 writew(ctlr, &priv->regs->ctlr);
873 ctlr &= ~RCAR_CAN_CTLR_CANM;
874 writew(ctlr, &priv->regs->ctlr);
875 priv->can.state = CAN_STATE_ERROR_ACTIVE;
876
877 netif_device_attach(ndev);
878 netif_start_queue(ndev);
879
880 return 0;
881}
882
883static SIMPLE_DEV_PM_OPS(rcar_can_pm_ops, rcar_can_suspend, rcar_can_resume);
884
885static const struct of_device_id rcar_can_of_table[] __maybe_unused = {
886 { .compatible = "renesas,can-r8a7778" },
887 { .compatible = "renesas,can-r8a7779" },
888 { .compatible = "renesas,can-r8a7790" },
889 { .compatible = "renesas,can-r8a7791" },
890 { .compatible = "renesas,rcar-gen1-can" },
891 { .compatible = "renesas,rcar-gen2-can" },
892 { .compatible = "renesas,rcar-gen3-can" },
893 { }
894};
895MODULE_DEVICE_TABLE(of, rcar_can_of_table);
896
897static struct platform_driver rcar_can_driver = {
898 .driver = {
899 .name = RCAR_CAN_DRV_NAME,
900 .of_match_table = of_match_ptr(rcar_can_of_table),
901 .pm = &rcar_can_pm_ops,
902 },
903 .probe = rcar_can_probe,
904 .remove = rcar_can_remove,
905};
906
907module_platform_driver(rcar_can_driver);
908
909MODULE_AUTHOR("Cogent Embedded, Inc.");
910MODULE_LICENSE("GPL");
911MODULE_DESCRIPTION("CAN driver for Renesas R-Car SoC");
912MODULE_ALIAS("platform:" RCAR_CAN_DRV_NAME);
913