2 * flexcan.c - FLEXCAN CAN controller driver
4 * Copyright (c) 2005-2006 Varma Electronics Oy
5 * Copyright (c) 2009 Sascha Hauer, Pengutronix
6 * Copyright (c) 2010 Marc Kleine-Budde, Pengutronix
8 * Based on code originally by Andrey Volkov <avolkov@varma-el.com>
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation version 2.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
22 #include <linux/netdevice.h>
23 #include <linux/can.h>
24 #include <linux/can/dev.h>
25 #include <linux/can/error.h>
26 #include <linux/can/led.h>
27 #include <linux/clk.h>
28 #include <linux/delay.h>
29 #include <linux/if_arp.h>
30 #include <linux/if_ether.h>
31 #include <linux/interrupt.h>
33 #include <linux/kernel.h>
34 #include <linux/list.h>
35 #include <linux/module.h>
37 #include <linux/of_device.h>
38 #include <linux/platform_device.h>
39 #include <linux/regulator/consumer.h>
41 #define DRV_NAME "flexcan"
43 /* 8 for RX fifo and 2 error handling */
44 #define FLEXCAN_NAPI_WEIGHT (8 + 2)
46 /* FLEXCAN module configuration register (CANMCR) bits */
47 #define FLEXCAN_MCR_MDIS BIT(31)
48 #define FLEXCAN_MCR_FRZ BIT(30)
49 #define FLEXCAN_MCR_FEN BIT(29)
50 #define FLEXCAN_MCR_HALT BIT(28)
51 #define FLEXCAN_MCR_NOT_RDY BIT(27)
52 #define FLEXCAN_MCR_WAK_MSK BIT(26)
53 #define FLEXCAN_MCR_SOFTRST BIT(25)
54 #define FLEXCAN_MCR_FRZ_ACK BIT(24)
55 #define FLEXCAN_MCR_SUPV BIT(23)
56 #define FLEXCAN_MCR_SLF_WAK BIT(22)
57 #define FLEXCAN_MCR_WRN_EN BIT(21)
58 #define FLEXCAN_MCR_LPM_ACK BIT(20)
59 #define FLEXCAN_MCR_WAK_SRC BIT(19)
60 #define FLEXCAN_MCR_DOZE BIT(18)
61 #define FLEXCAN_MCR_SRX_DIS BIT(17)
62 #define FLEXCAN_MCR_BCC BIT(16)
63 #define FLEXCAN_MCR_LPRIO_EN BIT(13)
64 #define FLEXCAN_MCR_AEN BIT(12)
65 #define FLEXCAN_MCR_MAXMB(x) ((x) & 0x1f)
66 #define FLEXCAN_MCR_IDAM_A (0 << 8)
67 #define FLEXCAN_MCR_IDAM_B (1 << 8)
68 #define FLEXCAN_MCR_IDAM_C (2 << 8)
69 #define FLEXCAN_MCR_IDAM_D (3 << 8)
71 /* FLEXCAN control register (CANCTRL) bits */
72 #define FLEXCAN_CTRL_PRESDIV(x) (((x) & 0xff) << 24)
73 #define FLEXCAN_CTRL_RJW(x) (((x) & 0x03) << 22)
74 #define FLEXCAN_CTRL_PSEG1(x) (((x) & 0x07) << 19)
75 #define FLEXCAN_CTRL_PSEG2(x) (((x) & 0x07) << 16)
76 #define FLEXCAN_CTRL_BOFF_MSK BIT(15)
77 #define FLEXCAN_CTRL_ERR_MSK BIT(14)
78 #define FLEXCAN_CTRL_CLK_SRC BIT(13)
79 #define FLEXCAN_CTRL_LPB BIT(12)
80 #define FLEXCAN_CTRL_TWRN_MSK BIT(11)
81 #define FLEXCAN_CTRL_RWRN_MSK BIT(10)
82 #define FLEXCAN_CTRL_SMP BIT(7)
83 #define FLEXCAN_CTRL_BOFF_REC BIT(6)
84 #define FLEXCAN_CTRL_TSYN BIT(5)
85 #define FLEXCAN_CTRL_LBUF BIT(4)
86 #define FLEXCAN_CTRL_LOM BIT(3)
87 #define FLEXCAN_CTRL_PROPSEG(x) ((x) & 0x07)
88 #define FLEXCAN_CTRL_ERR_BUS (FLEXCAN_CTRL_ERR_MSK)
89 #define FLEXCAN_CTRL_ERR_STATE \
90 (FLEXCAN_CTRL_TWRN_MSK | FLEXCAN_CTRL_RWRN_MSK | \
91 FLEXCAN_CTRL_BOFF_MSK)
92 #define FLEXCAN_CTRL_ERR_ALL \
93 (FLEXCAN_CTRL_ERR_BUS | FLEXCAN_CTRL_ERR_STATE)
95 /* FLEXCAN error and status register (ESR) bits */
96 #define FLEXCAN_ESR_TWRN_INT BIT(17)
97 #define FLEXCAN_ESR_RWRN_INT BIT(16)
98 #define FLEXCAN_ESR_BIT1_ERR BIT(15)
99 #define FLEXCAN_ESR_BIT0_ERR BIT(14)
100 #define FLEXCAN_ESR_ACK_ERR BIT(13)
101 #define FLEXCAN_ESR_CRC_ERR BIT(12)
102 #define FLEXCAN_ESR_FRM_ERR BIT(11)
103 #define FLEXCAN_ESR_STF_ERR BIT(10)
104 #define FLEXCAN_ESR_TX_WRN BIT(9)
105 #define FLEXCAN_ESR_RX_WRN BIT(8)
106 #define FLEXCAN_ESR_IDLE BIT(7)
107 #define FLEXCAN_ESR_TXRX BIT(6)
108 #define FLEXCAN_EST_FLT_CONF_SHIFT (4)
109 #define FLEXCAN_ESR_FLT_CONF_MASK (0x3 << FLEXCAN_EST_FLT_CONF_SHIFT)
110 #define FLEXCAN_ESR_FLT_CONF_ACTIVE (0x0 << FLEXCAN_EST_FLT_CONF_SHIFT)
111 #define FLEXCAN_ESR_FLT_CONF_PASSIVE (0x1 << FLEXCAN_EST_FLT_CONF_SHIFT)
112 #define FLEXCAN_ESR_BOFF_INT BIT(2)
113 #define FLEXCAN_ESR_ERR_INT BIT(1)
114 #define FLEXCAN_ESR_WAK_INT BIT(0)
115 #define FLEXCAN_ESR_ERR_BUS \
116 (FLEXCAN_ESR_BIT1_ERR | FLEXCAN_ESR_BIT0_ERR | \
117 FLEXCAN_ESR_ACK_ERR | FLEXCAN_ESR_CRC_ERR | \
118 FLEXCAN_ESR_FRM_ERR | FLEXCAN_ESR_STF_ERR)
119 #define FLEXCAN_ESR_ERR_STATE \
120 (FLEXCAN_ESR_TWRN_INT | FLEXCAN_ESR_RWRN_INT | FLEXCAN_ESR_BOFF_INT)
121 #define FLEXCAN_ESR_ERR_ALL \
122 (FLEXCAN_ESR_ERR_BUS | FLEXCAN_ESR_ERR_STATE)
123 #define FLEXCAN_ESR_ALL_INT \
124 (FLEXCAN_ESR_TWRN_INT | FLEXCAN_ESR_RWRN_INT | \
125 FLEXCAN_ESR_BOFF_INT | FLEXCAN_ESR_ERR_INT)
127 /* FLEXCAN interrupt flag register (IFLAG) bits */
128 #define FLEXCAN_TX_BUF_ID 8
129 #define FLEXCAN_IFLAG_BUF(x) BIT(x)
130 #define FLEXCAN_IFLAG_RX_FIFO_OVERFLOW BIT(7)
131 #define FLEXCAN_IFLAG_RX_FIFO_WARN BIT(6)
132 #define FLEXCAN_IFLAG_RX_FIFO_AVAILABLE BIT(5)
133 #define FLEXCAN_IFLAG_DEFAULT \
134 (FLEXCAN_IFLAG_RX_FIFO_OVERFLOW | FLEXCAN_IFLAG_RX_FIFO_AVAILABLE | \
135 FLEXCAN_IFLAG_BUF(FLEXCAN_TX_BUF_ID))
137 /* FLEXCAN message buffers */
138 #define FLEXCAN_MB_CNT_CODE(x) (((x) & 0xf) << 24)
139 #define FLEXCAN_MB_CNT_SRR BIT(22)
140 #define FLEXCAN_MB_CNT_IDE BIT(21)
141 #define FLEXCAN_MB_CNT_RTR BIT(20)
142 #define FLEXCAN_MB_CNT_LENGTH(x) (((x) & 0xf) << 16)
143 #define FLEXCAN_MB_CNT_TIMESTAMP(x) ((x) & 0xffff)
145 #define FLEXCAN_MB_CODE_MASK (0xf0ffffff)
147 #define FLEXCAN_TIMEOUT_US (50)
150 * FLEXCAN hardware feature flags
152 * Below is some version info we got:
153 * SOC Version IP-Version Glitch- [TR]WRN_INT
155 * MX25 FlexCAN2 03.00.00.00 no no
156 * MX28 FlexCAN2 03.00.04.00 yes yes
157 * MX35 FlexCAN2 03.00.00.00 no no
158 * MX53 FlexCAN2 03.00.00.00 yes no
159 * MX6s FlexCAN3 10.00.12.00 yes yes
161 * Some SOCs do not have the RX_WARN & TX_WARN interrupt line connected.
163 #define FLEXCAN_HAS_V10_FEATURES BIT(1) /* For core version >= 10 */
164 #define FLEXCAN_HAS_BROKEN_ERR_STATE BIT(2) /* [TR]WRN_INT not connected */
166 /* Structure of the message buffer */
173 /* Structure of the hardware registers */
174 struct flexcan_regs {
177 u32 timer; /* 0x08 */
178 u32 _reserved1; /* 0x0c */
179 u32 rxgmask; /* 0x10 */
180 u32 rx14mask; /* 0x14 */
181 u32 rx15mask; /* 0x18 */
184 u32 imask2; /* 0x24 */
185 u32 imask1; /* 0x28 */
186 u32 iflag2; /* 0x2c */
187 u32 iflag1; /* 0x30 */
190 u32 imeur; /* 0x3c */
193 u32 rxfgmask; /* 0x48 */
194 u32 rxfir; /* 0x4c */
196 struct flexcan_mb cantxfg[64];
199 struct flexcan_devtype_data {
200 u32 features; /* hardware controller features */
203 struct flexcan_priv {
205 struct net_device *dev;
206 struct napi_struct napi;
210 u32 reg_ctrl_default;
214 struct flexcan_platform_data *pdata;
215 const struct flexcan_devtype_data *devtype_data;
216 struct regulator *reg_xceiver;
219 static struct flexcan_devtype_data fsl_p1010_devtype_data = {
220 .features = FLEXCAN_HAS_BROKEN_ERR_STATE,
222 static struct flexcan_devtype_data fsl_imx28_devtype_data;
223 static struct flexcan_devtype_data fsl_imx6q_devtype_data = {
224 .features = FLEXCAN_HAS_V10_FEATURES,
227 static const struct can_bittiming_const flexcan_bittiming_const = {
240 * Abstract off the read/write for arm versus ppc. This
241 * assumes that PPC uses big-endian registers and everything
242 * else uses little-endian registers, independent of CPU
245 #if defined(CONFIG_PPC)
246 static inline u32 flexcan_read(void __iomem *addr)
248 return in_be32(addr);
251 static inline void flexcan_write(u32 val, void __iomem *addr)
256 static inline u32 flexcan_read(void __iomem *addr)
261 static inline void flexcan_write(u32 val, void __iomem *addr)
267 static inline int flexcan_has_and_handle_berr(const struct flexcan_priv *priv,
270 return (priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING) &&
271 (reg_esr & FLEXCAN_ESR_ERR_BUS);
274 static int flexcan_chip_enable(struct flexcan_priv *priv)
276 struct flexcan_regs __iomem *regs = priv->base;
277 unsigned int timeout = FLEXCAN_TIMEOUT_US / 10;
280 reg = flexcan_read(®s->mcr);
281 reg &= ~FLEXCAN_MCR_MDIS;
282 flexcan_write(reg, ®s->mcr);
284 while (timeout-- && (flexcan_read(®s->mcr) & FLEXCAN_MCR_LPM_ACK))
285 usleep_range(10, 20);
287 if (flexcan_read(®s->mcr) & FLEXCAN_MCR_LPM_ACK)
293 static int flexcan_chip_disable(struct flexcan_priv *priv)
295 struct flexcan_regs __iomem *regs = priv->base;
296 unsigned int timeout = FLEXCAN_TIMEOUT_US / 10;
299 reg = flexcan_read(®s->mcr);
300 reg |= FLEXCAN_MCR_MDIS;
301 flexcan_write(reg, ®s->mcr);
303 while (timeout-- && !(flexcan_read(®s->mcr) & FLEXCAN_MCR_LPM_ACK))
304 usleep_range(10, 20);
306 if (!(flexcan_read(®s->mcr) & FLEXCAN_MCR_LPM_ACK))
312 static int flexcan_get_berr_counter(const struct net_device *dev,
313 struct can_berr_counter *bec)
315 const struct flexcan_priv *priv = netdev_priv(dev);
316 struct flexcan_regs __iomem *regs = priv->base;
317 u32 reg = flexcan_read(®s->ecr);
319 bec->txerr = (reg >> 0) & 0xff;
320 bec->rxerr = (reg >> 8) & 0xff;
325 static int flexcan_start_xmit(struct sk_buff *skb, struct net_device *dev)
327 const struct flexcan_priv *priv = netdev_priv(dev);
328 struct flexcan_regs __iomem *regs = priv->base;
329 struct can_frame *cf = (struct can_frame *)skb->data;
331 u32 ctrl = FLEXCAN_MB_CNT_CODE(0xc) | (cf->can_dlc << 16);
333 if (can_dropped_invalid_skb(dev, skb))
336 netif_stop_queue(dev);
338 if (cf->can_id & CAN_EFF_FLAG) {
339 can_id = cf->can_id & CAN_EFF_MASK;
340 ctrl |= FLEXCAN_MB_CNT_IDE | FLEXCAN_MB_CNT_SRR;
342 can_id = (cf->can_id & CAN_SFF_MASK) << 18;
345 if (cf->can_id & CAN_RTR_FLAG)
346 ctrl |= FLEXCAN_MB_CNT_RTR;
348 if (cf->can_dlc > 0) {
349 u32 data = be32_to_cpup((__be32 *)&cf->data[0]);
350 flexcan_write(data, ®s->cantxfg[FLEXCAN_TX_BUF_ID].data[0]);
352 if (cf->can_dlc > 3) {
353 u32 data = be32_to_cpup((__be32 *)&cf->data[4]);
354 flexcan_write(data, ®s->cantxfg[FLEXCAN_TX_BUF_ID].data[1]);
357 can_put_echo_skb(skb, dev, 0);
359 flexcan_write(can_id, ®s->cantxfg[FLEXCAN_TX_BUF_ID].can_id);
360 flexcan_write(ctrl, ®s->cantxfg[FLEXCAN_TX_BUF_ID].can_ctrl);
365 static void do_bus_err(struct net_device *dev,
366 struct can_frame *cf, u32 reg_esr)
368 struct flexcan_priv *priv = netdev_priv(dev);
369 int rx_errors = 0, tx_errors = 0;
371 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
373 if (reg_esr & FLEXCAN_ESR_BIT1_ERR) {
374 netdev_dbg(dev, "BIT1_ERR irq\n");
375 cf->data[2] |= CAN_ERR_PROT_BIT1;
378 if (reg_esr & FLEXCAN_ESR_BIT0_ERR) {
379 netdev_dbg(dev, "BIT0_ERR irq\n");
380 cf->data[2] |= CAN_ERR_PROT_BIT0;
383 if (reg_esr & FLEXCAN_ESR_ACK_ERR) {
384 netdev_dbg(dev, "ACK_ERR irq\n");
385 cf->can_id |= CAN_ERR_ACK;
386 cf->data[3] |= CAN_ERR_PROT_LOC_ACK;
389 if (reg_esr & FLEXCAN_ESR_CRC_ERR) {
390 netdev_dbg(dev, "CRC_ERR irq\n");
391 cf->data[2] |= CAN_ERR_PROT_BIT;
392 cf->data[3] |= CAN_ERR_PROT_LOC_CRC_SEQ;
395 if (reg_esr & FLEXCAN_ESR_FRM_ERR) {
396 netdev_dbg(dev, "FRM_ERR irq\n");
397 cf->data[2] |= CAN_ERR_PROT_FORM;
400 if (reg_esr & FLEXCAN_ESR_STF_ERR) {
401 netdev_dbg(dev, "STF_ERR irq\n");
402 cf->data[2] |= CAN_ERR_PROT_STUFF;
406 priv->can.can_stats.bus_error++;
408 dev->stats.rx_errors++;
410 dev->stats.tx_errors++;
413 static int flexcan_poll_bus_err(struct net_device *dev, u32 reg_esr)
416 struct can_frame *cf;
418 skb = alloc_can_err_skb(dev, &cf);
422 do_bus_err(dev, cf, reg_esr);
423 netif_receive_skb(skb);
425 dev->stats.rx_packets++;
426 dev->stats.rx_bytes += cf->can_dlc;
431 static void do_state(struct net_device *dev,
432 struct can_frame *cf, enum can_state new_state)
434 struct flexcan_priv *priv = netdev_priv(dev);
435 struct can_berr_counter bec;
437 flexcan_get_berr_counter(dev, &bec);
439 switch (priv->can.state) {
440 case CAN_STATE_ERROR_ACTIVE:
443 * to : ERROR_WARNING, ERROR_PASSIVE, BUS_OFF
444 * => : there was a warning int
446 if (new_state >= CAN_STATE_ERROR_WARNING &&
447 new_state <= CAN_STATE_BUS_OFF) {
448 netdev_dbg(dev, "Error Warning IRQ\n");
449 priv->can.can_stats.error_warning++;
451 cf->can_id |= CAN_ERR_CRTL;
452 cf->data[1] = (bec.txerr > bec.rxerr) ?
453 CAN_ERR_CRTL_TX_WARNING :
454 CAN_ERR_CRTL_RX_WARNING;
456 case CAN_STATE_ERROR_WARNING: /* fallthrough */
458 * from: ERROR_ACTIVE, ERROR_WARNING
459 * to : ERROR_PASSIVE, BUS_OFF
460 * => : error passive int
462 if (new_state >= CAN_STATE_ERROR_PASSIVE &&
463 new_state <= CAN_STATE_BUS_OFF) {
464 netdev_dbg(dev, "Error Passive IRQ\n");
465 priv->can.can_stats.error_passive++;
467 cf->can_id |= CAN_ERR_CRTL;
468 cf->data[1] = (bec.txerr > bec.rxerr) ?
469 CAN_ERR_CRTL_TX_PASSIVE :
470 CAN_ERR_CRTL_RX_PASSIVE;
473 case CAN_STATE_BUS_OFF:
474 netdev_err(dev, "BUG! "
475 "hardware recovered automatically from BUS_OFF\n");
481 /* process state changes depending on the new state */
483 case CAN_STATE_ERROR_ACTIVE:
484 netdev_dbg(dev, "Error Active\n");
485 cf->can_id |= CAN_ERR_PROT;
486 cf->data[2] = CAN_ERR_PROT_ACTIVE;
488 case CAN_STATE_BUS_OFF:
489 cf->can_id |= CAN_ERR_BUSOFF;
497 static int flexcan_poll_state(struct net_device *dev, u32 reg_esr)
499 struct flexcan_priv *priv = netdev_priv(dev);
501 struct can_frame *cf;
502 enum can_state new_state;
505 flt = reg_esr & FLEXCAN_ESR_FLT_CONF_MASK;
506 if (likely(flt == FLEXCAN_ESR_FLT_CONF_ACTIVE)) {
507 if (likely(!(reg_esr & (FLEXCAN_ESR_TX_WRN |
508 FLEXCAN_ESR_RX_WRN))))
509 new_state = CAN_STATE_ERROR_ACTIVE;
511 new_state = CAN_STATE_ERROR_WARNING;
512 } else if (unlikely(flt == FLEXCAN_ESR_FLT_CONF_PASSIVE))
513 new_state = CAN_STATE_ERROR_PASSIVE;
515 new_state = CAN_STATE_BUS_OFF;
517 /* state hasn't changed */
518 if (likely(new_state == priv->can.state))
521 skb = alloc_can_err_skb(dev, &cf);
525 do_state(dev, cf, new_state);
526 priv->can.state = new_state;
527 netif_receive_skb(skb);
529 dev->stats.rx_packets++;
530 dev->stats.rx_bytes += cf->can_dlc;
535 static void flexcan_read_fifo(const struct net_device *dev,
536 struct can_frame *cf)
538 const struct flexcan_priv *priv = netdev_priv(dev);
539 struct flexcan_regs __iomem *regs = priv->base;
540 struct flexcan_mb __iomem *mb = ®s->cantxfg[0];
541 u32 reg_ctrl, reg_id;
543 reg_ctrl = flexcan_read(&mb->can_ctrl);
544 reg_id = flexcan_read(&mb->can_id);
545 if (reg_ctrl & FLEXCAN_MB_CNT_IDE)
546 cf->can_id = ((reg_id >> 0) & CAN_EFF_MASK) | CAN_EFF_FLAG;
548 cf->can_id = (reg_id >> 18) & CAN_SFF_MASK;
550 if (reg_ctrl & FLEXCAN_MB_CNT_RTR)
551 cf->can_id |= CAN_RTR_FLAG;
552 cf->can_dlc = get_can_dlc((reg_ctrl >> 16) & 0xf);
554 *(__be32 *)(cf->data + 0) = cpu_to_be32(flexcan_read(&mb->data[0]));
555 *(__be32 *)(cf->data + 4) = cpu_to_be32(flexcan_read(&mb->data[1]));
558 flexcan_write(FLEXCAN_IFLAG_RX_FIFO_AVAILABLE, ®s->iflag1);
559 flexcan_read(®s->timer);
562 static int flexcan_read_frame(struct net_device *dev)
564 struct net_device_stats *stats = &dev->stats;
565 struct can_frame *cf;
568 skb = alloc_can_skb(dev, &cf);
569 if (unlikely(!skb)) {
574 flexcan_read_fifo(dev, cf);
575 netif_receive_skb(skb);
578 stats->rx_bytes += cf->can_dlc;
580 can_led_event(dev, CAN_LED_EVENT_RX);
585 static int flexcan_poll(struct napi_struct *napi, int quota)
587 struct net_device *dev = napi->dev;
588 const struct flexcan_priv *priv = netdev_priv(dev);
589 struct flexcan_regs __iomem *regs = priv->base;
590 u32 reg_iflag1, reg_esr;
594 * The error bits are cleared on read,
595 * use saved value from irq handler.
597 reg_esr = flexcan_read(®s->esr) | priv->reg_esr;
599 /* handle state changes */
600 work_done += flexcan_poll_state(dev, reg_esr);
603 reg_iflag1 = flexcan_read(®s->iflag1);
604 while (reg_iflag1 & FLEXCAN_IFLAG_RX_FIFO_AVAILABLE &&
606 work_done += flexcan_read_frame(dev);
607 reg_iflag1 = flexcan_read(®s->iflag1);
610 /* report bus errors */
611 if (flexcan_has_and_handle_berr(priv, reg_esr) && work_done < quota)
612 work_done += flexcan_poll_bus_err(dev, reg_esr);
614 if (work_done < quota) {
617 flexcan_write(FLEXCAN_IFLAG_DEFAULT, ®s->imask1);
618 flexcan_write(priv->reg_ctrl_default, ®s->ctrl);
624 static irqreturn_t flexcan_irq(int irq, void *dev_id)
626 struct net_device *dev = dev_id;
627 struct net_device_stats *stats = &dev->stats;
628 struct flexcan_priv *priv = netdev_priv(dev);
629 struct flexcan_regs __iomem *regs = priv->base;
630 u32 reg_iflag1, reg_esr;
632 reg_iflag1 = flexcan_read(®s->iflag1);
633 reg_esr = flexcan_read(®s->esr);
634 /* ACK all bus error and state change IRQ sources */
635 if (reg_esr & FLEXCAN_ESR_ALL_INT)
636 flexcan_write(reg_esr & FLEXCAN_ESR_ALL_INT, ®s->esr);
639 * schedule NAPI in case of:
642 * - bus error IRQ and bus error reporting is activated
644 if ((reg_iflag1 & FLEXCAN_IFLAG_RX_FIFO_AVAILABLE) ||
645 (reg_esr & FLEXCAN_ESR_ERR_STATE) ||
646 flexcan_has_and_handle_berr(priv, reg_esr)) {
648 * The error bits are cleared on read,
649 * save them for later use.
651 priv->reg_esr = reg_esr & FLEXCAN_ESR_ERR_BUS;
652 flexcan_write(FLEXCAN_IFLAG_DEFAULT &
653 ~FLEXCAN_IFLAG_RX_FIFO_AVAILABLE, ®s->imask1);
654 flexcan_write(priv->reg_ctrl_default & ~FLEXCAN_CTRL_ERR_ALL,
656 napi_schedule(&priv->napi);
660 if (reg_iflag1 & FLEXCAN_IFLAG_RX_FIFO_OVERFLOW) {
661 flexcan_write(FLEXCAN_IFLAG_RX_FIFO_OVERFLOW, ®s->iflag1);
662 dev->stats.rx_over_errors++;
663 dev->stats.rx_errors++;
666 /* transmission complete interrupt */
667 if (reg_iflag1 & (1 << FLEXCAN_TX_BUF_ID)) {
668 stats->tx_bytes += can_get_echo_skb(dev, 0);
670 can_led_event(dev, CAN_LED_EVENT_TX);
671 flexcan_write((1 << FLEXCAN_TX_BUF_ID), ®s->iflag1);
672 netif_wake_queue(dev);
678 static void flexcan_set_bittiming(struct net_device *dev)
680 const struct flexcan_priv *priv = netdev_priv(dev);
681 const struct can_bittiming *bt = &priv->can.bittiming;
682 struct flexcan_regs __iomem *regs = priv->base;
685 reg = flexcan_read(®s->ctrl);
686 reg &= ~(FLEXCAN_CTRL_PRESDIV(0xff) |
687 FLEXCAN_CTRL_RJW(0x3) |
688 FLEXCAN_CTRL_PSEG1(0x7) |
689 FLEXCAN_CTRL_PSEG2(0x7) |
690 FLEXCAN_CTRL_PROPSEG(0x7) |
695 reg |= FLEXCAN_CTRL_PRESDIV(bt->brp - 1) |
696 FLEXCAN_CTRL_PSEG1(bt->phase_seg1 - 1) |
697 FLEXCAN_CTRL_PSEG2(bt->phase_seg2 - 1) |
698 FLEXCAN_CTRL_RJW(bt->sjw - 1) |
699 FLEXCAN_CTRL_PROPSEG(bt->prop_seg - 1);
701 if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)
702 reg |= FLEXCAN_CTRL_LPB;
703 if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
704 reg |= FLEXCAN_CTRL_LOM;
705 if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
706 reg |= FLEXCAN_CTRL_SMP;
708 netdev_info(dev, "writing ctrl=0x%08x\n", reg);
709 flexcan_write(reg, ®s->ctrl);
711 /* print chip status */
712 netdev_dbg(dev, "%s: mcr=0x%08x ctrl=0x%08x\n", __func__,
713 flexcan_read(®s->mcr), flexcan_read(®s->ctrl));
719 * this functions is entered with clocks enabled
722 static int flexcan_chip_start(struct net_device *dev)
724 struct flexcan_priv *priv = netdev_priv(dev);
725 struct flexcan_regs __iomem *regs = priv->base;
727 u32 reg_mcr, reg_ctrl;
730 err = flexcan_chip_enable(priv);
735 flexcan_write(FLEXCAN_MCR_SOFTRST, ®s->mcr);
738 reg_mcr = flexcan_read(®s->mcr);
739 if (reg_mcr & FLEXCAN_MCR_SOFTRST) {
740 netdev_err(dev, "Failed to softreset can module (mcr=0x%08x)\n",
746 flexcan_set_bittiming(dev);
754 * only supervisor access
760 reg_mcr = flexcan_read(®s->mcr);
761 reg_mcr &= ~FLEXCAN_MCR_MAXMB(0xff);
762 reg_mcr |= FLEXCAN_MCR_FRZ | FLEXCAN_MCR_FEN | FLEXCAN_MCR_HALT |
763 FLEXCAN_MCR_SUPV | FLEXCAN_MCR_WRN_EN |
764 FLEXCAN_MCR_IDAM_C | FLEXCAN_MCR_SRX_DIS |
765 FLEXCAN_MCR_MAXMB(FLEXCAN_TX_BUF_ID);
766 netdev_dbg(dev, "%s: writing mcr=0x%08x", __func__, reg_mcr);
767 flexcan_write(reg_mcr, ®s->mcr);
772 * disable timer sync feature
774 * disable auto busoff recovery
775 * transmit lowest buffer first
777 * enable tx and rx warning interrupt
778 * enable bus off interrupt
779 * (== FLEXCAN_CTRL_ERR_STATE)
781 reg_ctrl = flexcan_read(®s->ctrl);
782 reg_ctrl &= ~FLEXCAN_CTRL_TSYN;
783 reg_ctrl |= FLEXCAN_CTRL_BOFF_REC | FLEXCAN_CTRL_LBUF |
784 FLEXCAN_CTRL_ERR_STATE;
786 * enable the "error interrupt" (FLEXCAN_CTRL_ERR_MSK),
787 * on most Flexcan cores, too. Otherwise we don't get
788 * any error warning or passive interrupts.
790 if (priv->devtype_data->features & FLEXCAN_HAS_BROKEN_ERR_STATE ||
791 priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING)
792 reg_ctrl |= FLEXCAN_CTRL_ERR_MSK;
794 /* save for later use */
795 priv->reg_ctrl_default = reg_ctrl;
796 netdev_dbg(dev, "%s: writing ctrl=0x%08x", __func__, reg_ctrl);
797 flexcan_write(reg_ctrl, ®s->ctrl);
799 /* Abort any pending TX, mark Mailbox as INACTIVE */
800 flexcan_write(FLEXCAN_MB_CNT_CODE(0x4),
801 ®s->cantxfg[FLEXCAN_TX_BUF_ID].can_ctrl);
803 /* acceptance mask/acceptance code (accept everything) */
804 flexcan_write(0x0, ®s->rxgmask);
805 flexcan_write(0x0, ®s->rx14mask);
806 flexcan_write(0x0, ®s->rx15mask);
808 if (priv->devtype_data->features & FLEXCAN_HAS_V10_FEATURES)
809 flexcan_write(0x0, ®s->rxfgmask);
811 if (priv->reg_xceiver) {
812 err = regulator_enable(priv->reg_xceiver);
817 /* synchronize with the can bus */
818 reg_mcr = flexcan_read(®s->mcr);
819 reg_mcr &= ~FLEXCAN_MCR_HALT;
820 flexcan_write(reg_mcr, ®s->mcr);
822 priv->can.state = CAN_STATE_ERROR_ACTIVE;
824 /* enable FIFO interrupts */
825 flexcan_write(FLEXCAN_IFLAG_DEFAULT, ®s->imask1);
827 /* print chip status */
828 netdev_dbg(dev, "%s: reading mcr=0x%08x ctrl=0x%08x\n", __func__,
829 flexcan_read(®s->mcr), flexcan_read(®s->ctrl));
834 flexcan_chip_disable(priv);
841 * this functions is entered with clocks enabled
844 static void flexcan_chip_stop(struct net_device *dev)
846 struct flexcan_priv *priv = netdev_priv(dev);
847 struct flexcan_regs __iomem *regs = priv->base;
850 /* Disable + halt module */
851 reg = flexcan_read(®s->mcr);
852 reg |= FLEXCAN_MCR_MDIS | FLEXCAN_MCR_HALT;
853 flexcan_write(reg, ®s->mcr);
855 /* Disable all interrupts */
856 flexcan_write(0, ®s->imask1);
857 flexcan_write(priv->reg_ctrl_default & ~FLEXCAN_CTRL_ERR_ALL,
860 if (priv->reg_xceiver)
861 regulator_disable(priv->reg_xceiver);
862 priv->can.state = CAN_STATE_STOPPED;
867 static int flexcan_open(struct net_device *dev)
869 struct flexcan_priv *priv = netdev_priv(dev);
872 err = clk_prepare_enable(priv->clk_ipg);
876 err = clk_prepare_enable(priv->clk_per);
878 goto out_disable_ipg;
880 err = open_candev(dev);
882 goto out_disable_per;
884 err = request_irq(dev->irq, flexcan_irq, IRQF_SHARED, dev->name, dev);
888 /* start chip and queuing */
889 err = flexcan_chip_start(dev);
893 can_led_event(dev, CAN_LED_EVENT_OPEN);
895 napi_enable(&priv->napi);
896 netif_start_queue(dev);
901 free_irq(dev->irq, dev);
905 clk_disable_unprepare(priv->clk_per);
907 clk_disable_unprepare(priv->clk_ipg);
912 static int flexcan_close(struct net_device *dev)
914 struct flexcan_priv *priv = netdev_priv(dev);
916 netif_stop_queue(dev);
917 napi_disable(&priv->napi);
918 flexcan_chip_stop(dev);
920 free_irq(dev->irq, dev);
921 clk_disable_unprepare(priv->clk_per);
922 clk_disable_unprepare(priv->clk_ipg);
926 can_led_event(dev, CAN_LED_EVENT_STOP);
931 static int flexcan_set_mode(struct net_device *dev, enum can_mode mode)
937 err = flexcan_chip_start(dev);
941 netif_wake_queue(dev);
951 static const struct net_device_ops flexcan_netdev_ops = {
952 .ndo_open = flexcan_open,
953 .ndo_stop = flexcan_close,
954 .ndo_start_xmit = flexcan_start_xmit,
957 static int register_flexcandev(struct net_device *dev)
959 struct flexcan_priv *priv = netdev_priv(dev);
960 struct flexcan_regs __iomem *regs = priv->base;
963 err = clk_prepare_enable(priv->clk_ipg);
967 err = clk_prepare_enable(priv->clk_per);
969 goto out_disable_ipg;
971 /* select "bus clock", chip must be disabled */
972 err = flexcan_chip_disable(priv);
974 goto out_disable_per;
975 reg = flexcan_read(®s->ctrl);
976 reg |= FLEXCAN_CTRL_CLK_SRC;
977 flexcan_write(reg, ®s->ctrl);
979 err = flexcan_chip_enable(priv);
981 goto out_chip_disable;
983 /* set freeze, halt and activate FIFO, restrict register access */
984 reg = flexcan_read(®s->mcr);
985 reg |= FLEXCAN_MCR_FRZ | FLEXCAN_MCR_HALT |
986 FLEXCAN_MCR_FEN | FLEXCAN_MCR_SUPV;
987 flexcan_write(reg, ®s->mcr);
990 * Currently we only support newer versions of this core
991 * featuring a RX FIFO. Older cores found on some Coldfire
992 * derivates are not yet supported.
994 reg = flexcan_read(®s->mcr);
995 if (!(reg & FLEXCAN_MCR_FEN)) {
996 netdev_err(dev, "Could not enable RX FIFO, unsupported core\n");
998 goto out_chip_disable;
1001 err = register_candev(dev);
1003 /* disable core and turn off clocks */
1005 flexcan_chip_disable(priv);
1007 clk_disable_unprepare(priv->clk_per);
1009 clk_disable_unprepare(priv->clk_ipg);
1014 static void unregister_flexcandev(struct net_device *dev)
1016 unregister_candev(dev);
1019 static const struct of_device_id flexcan_of_match[] = {
1020 { .compatible = "fsl,imx6q-flexcan", .data = &fsl_imx6q_devtype_data, },
1021 { .compatible = "fsl,imx28-flexcan", .data = &fsl_imx28_devtype_data, },
1022 { .compatible = "fsl,p1010-flexcan", .data = &fsl_p1010_devtype_data, },
1025 MODULE_DEVICE_TABLE(of, flexcan_of_match);
1027 static const struct platform_device_id flexcan_id_table[] = {
1028 { .name = "flexcan", .driver_data = (kernel_ulong_t)&fsl_p1010_devtype_data, },
1031 MODULE_DEVICE_TABLE(platform, flexcan_id_table);
1033 static int flexcan_probe(struct platform_device *pdev)
1035 const struct of_device_id *of_id;
1036 const struct flexcan_devtype_data *devtype_data;
1037 struct net_device *dev;
1038 struct flexcan_priv *priv;
1039 struct resource *mem;
1040 struct clk *clk_ipg = NULL, *clk_per = NULL;
1045 if (pdev->dev.of_node)
1046 of_property_read_u32(pdev->dev.of_node,
1047 "clock-frequency", &clock_freq);
1050 clk_ipg = devm_clk_get(&pdev->dev, "ipg");
1051 if (IS_ERR(clk_ipg)) {
1052 dev_err(&pdev->dev, "no ipg clock defined\n");
1053 return PTR_ERR(clk_ipg);
1056 clk_per = devm_clk_get(&pdev->dev, "per");
1057 if (IS_ERR(clk_per)) {
1058 dev_err(&pdev->dev, "no per clock defined\n");
1059 return PTR_ERR(clk_per);
1061 clock_freq = clk_get_rate(clk_per);
1064 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1065 irq = platform_get_irq(pdev, 0);
1069 base = devm_ioremap_resource(&pdev->dev, mem);
1071 return PTR_ERR(base);
1073 of_id = of_match_device(flexcan_of_match, &pdev->dev);
1075 devtype_data = of_id->data;
1076 } else if (pdev->id_entry->driver_data) {
1077 devtype_data = (struct flexcan_devtype_data *)
1078 pdev->id_entry->driver_data;
1083 dev = alloc_candev(sizeof(struct flexcan_priv), 1);
1087 dev->netdev_ops = &flexcan_netdev_ops;
1089 dev->flags |= IFF_ECHO;
1091 priv = netdev_priv(dev);
1092 priv->can.clock.freq = clock_freq;
1093 priv->can.bittiming_const = &flexcan_bittiming_const;
1094 priv->can.do_set_mode = flexcan_set_mode;
1095 priv->can.do_get_berr_counter = flexcan_get_berr_counter;
1096 priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK |
1097 CAN_CTRLMODE_LISTENONLY | CAN_CTRLMODE_3_SAMPLES |
1098 CAN_CTRLMODE_BERR_REPORTING;
1101 priv->clk_ipg = clk_ipg;
1102 priv->clk_per = clk_per;
1103 priv->pdata = dev_get_platdata(&pdev->dev);
1104 priv->devtype_data = devtype_data;
1106 priv->reg_xceiver = devm_regulator_get(&pdev->dev, "xceiver");
1107 if (IS_ERR(priv->reg_xceiver))
1108 priv->reg_xceiver = NULL;
1110 netif_napi_add(dev, &priv->napi, flexcan_poll, FLEXCAN_NAPI_WEIGHT);
1112 platform_set_drvdata(pdev, dev);
1113 SET_NETDEV_DEV(dev, &pdev->dev);
1115 err = register_flexcandev(dev);
1117 dev_err(&pdev->dev, "registering netdev failed\n");
1118 goto failed_register;
1121 devm_can_led_init(dev);
1123 dev_info(&pdev->dev, "device registered (reg_base=%p, irq=%d)\n",
1124 priv->base, dev->irq);
1133 static int flexcan_remove(struct platform_device *pdev)
1135 struct net_device *dev = platform_get_drvdata(pdev);
1137 unregister_flexcandev(dev);
1144 #ifdef CONFIG_PM_SLEEP
1145 static int flexcan_suspend(struct device *device)
1147 struct net_device *dev = dev_get_drvdata(device);
1148 struct flexcan_priv *priv = netdev_priv(dev);
1151 err = flexcan_chip_disable(priv);
1155 if (netif_running(dev)) {
1156 netif_stop_queue(dev);
1157 netif_device_detach(dev);
1159 priv->can.state = CAN_STATE_SLEEPING;
1164 static int flexcan_resume(struct device *device)
1166 struct net_device *dev = dev_get_drvdata(device);
1167 struct flexcan_priv *priv = netdev_priv(dev);
1169 priv->can.state = CAN_STATE_ERROR_ACTIVE;
1170 if (netif_running(dev)) {
1171 netif_device_attach(dev);
1172 netif_start_queue(dev);
1174 return flexcan_chip_enable(priv);
1176 #endif /* CONFIG_PM_SLEEP */
1178 static SIMPLE_DEV_PM_OPS(flexcan_pm_ops, flexcan_suspend, flexcan_resume);
1180 static struct platform_driver flexcan_driver = {
1183 .owner = THIS_MODULE,
1184 .pm = &flexcan_pm_ops,
1185 .of_match_table = flexcan_of_match,
1187 .probe = flexcan_probe,
1188 .remove = flexcan_remove,
1189 .id_table = flexcan_id_table,
1192 module_platform_driver(flexcan_driver);
1194 MODULE_AUTHOR("Sascha Hauer <kernel@pengutronix.de>, "
1195 "Marc Kleine-Budde <kernel@pengutronix.de>");
1196 MODULE_LICENSE("GPL v2");
1197 MODULE_DESCRIPTION("CAN port driver for flexcan based chip");