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)
148 * FLEXCAN hardware feature flags
150 * Below is some version info we got:
151 * SOC Version IP-Version Glitch- [TR]WRN_INT
153 * MX25 FlexCAN2 03.00.00.00 no no
154 * MX28 FlexCAN2 03.00.04.00 yes yes
155 * MX35 FlexCAN2 03.00.00.00 no no
156 * MX53 FlexCAN2 03.00.00.00 yes no
157 * MX6s FlexCAN3 10.00.12.00 yes yes
159 * Some SOCs do not have the RX_WARN & TX_WARN interrupt line connected.
161 #define FLEXCAN_HAS_V10_FEATURES BIT(1) /* For core version >= 10 */
162 #define FLEXCAN_HAS_BROKEN_ERR_STATE BIT(2) /* [TR]WRN_INT not connected */
164 /* Structure of the message buffer */
171 /* Structure of the hardware registers */
172 struct flexcan_regs
{
175 u32 timer
; /* 0x08 */
176 u32 _reserved1
; /* 0x0c */
177 u32 rxgmask
; /* 0x10 */
178 u32 rx14mask
; /* 0x14 */
179 u32 rx15mask
; /* 0x18 */
182 u32 imask2
; /* 0x24 */
183 u32 imask1
; /* 0x28 */
184 u32 iflag2
; /* 0x2c */
185 u32 iflag1
; /* 0x30 */
188 u32 imeur
; /* 0x3c */
191 u32 rxfgmask
; /* 0x48 */
192 u32 rxfir
; /* 0x4c */
194 struct flexcan_mb cantxfg
[64];
197 struct flexcan_devtype_data
{
198 u32 features
; /* hardware controller features */
201 struct flexcan_priv
{
203 struct net_device
*dev
;
204 struct napi_struct napi
;
208 u32 reg_ctrl_default
;
212 struct flexcan_platform_data
*pdata
;
213 const struct flexcan_devtype_data
*devtype_data
;
214 struct regulator
*reg_xceiver
;
217 static struct flexcan_devtype_data fsl_p1010_devtype_data
= {
218 .features
= FLEXCAN_HAS_BROKEN_ERR_STATE
,
220 static struct flexcan_devtype_data fsl_imx28_devtype_data
;
221 static struct flexcan_devtype_data fsl_imx6q_devtype_data
= {
222 .features
= FLEXCAN_HAS_V10_FEATURES
,
225 static const struct can_bittiming_const flexcan_bittiming_const
= {
238 * Abstract off the read/write for arm versus ppc. This
239 * assumes that PPC uses big-endian registers and everything
240 * else uses little-endian registers, independent of CPU
243 #if defined(CONFIG_PPC)
244 static inline u32
flexcan_read(void __iomem
*addr
)
246 return in_be32(addr
);
249 static inline void flexcan_write(u32 val
, void __iomem
*addr
)
254 static inline u32
flexcan_read(void __iomem
*addr
)
259 static inline void flexcan_write(u32 val
, void __iomem
*addr
)
265 static inline int flexcan_has_and_handle_berr(const struct flexcan_priv
*priv
,
268 return (priv
->can
.ctrlmode
& CAN_CTRLMODE_BERR_REPORTING
) &&
269 (reg_esr
& FLEXCAN_ESR_ERR_BUS
);
272 static inline void flexcan_chip_enable(struct flexcan_priv
*priv
)
274 struct flexcan_regs __iomem
*regs
= priv
->base
;
277 reg
= flexcan_read(®s
->mcr
);
278 reg
&= ~FLEXCAN_MCR_MDIS
;
279 flexcan_write(reg
, ®s
->mcr
);
284 static inline void flexcan_chip_disable(struct flexcan_priv
*priv
)
286 struct flexcan_regs __iomem
*regs
= priv
->base
;
289 reg
= flexcan_read(®s
->mcr
);
290 reg
|= FLEXCAN_MCR_MDIS
;
291 flexcan_write(reg
, ®s
->mcr
);
294 static int flexcan_get_berr_counter(const struct net_device
*dev
,
295 struct can_berr_counter
*bec
)
297 const struct flexcan_priv
*priv
= netdev_priv(dev
);
298 struct flexcan_regs __iomem
*regs
= priv
->base
;
299 u32 reg
= flexcan_read(®s
->ecr
);
301 bec
->txerr
= (reg
>> 0) & 0xff;
302 bec
->rxerr
= (reg
>> 8) & 0xff;
307 static int flexcan_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
309 const struct flexcan_priv
*priv
= netdev_priv(dev
);
310 struct flexcan_regs __iomem
*regs
= priv
->base
;
311 struct can_frame
*cf
= (struct can_frame
*)skb
->data
;
313 u32 ctrl
= FLEXCAN_MB_CNT_CODE(0xc) | (cf
->can_dlc
<< 16);
315 if (can_dropped_invalid_skb(dev
, skb
))
318 netif_stop_queue(dev
);
320 if (cf
->can_id
& CAN_EFF_FLAG
) {
321 can_id
= cf
->can_id
& CAN_EFF_MASK
;
322 ctrl
|= FLEXCAN_MB_CNT_IDE
| FLEXCAN_MB_CNT_SRR
;
324 can_id
= (cf
->can_id
& CAN_SFF_MASK
) << 18;
327 if (cf
->can_id
& CAN_RTR_FLAG
)
328 ctrl
|= FLEXCAN_MB_CNT_RTR
;
330 if (cf
->can_dlc
> 0) {
331 u32 data
= be32_to_cpup((__be32
*)&cf
->data
[0]);
332 flexcan_write(data
, ®s
->cantxfg
[FLEXCAN_TX_BUF_ID
].data
[0]);
334 if (cf
->can_dlc
> 3) {
335 u32 data
= be32_to_cpup((__be32
*)&cf
->data
[4]);
336 flexcan_write(data
, ®s
->cantxfg
[FLEXCAN_TX_BUF_ID
].data
[1]);
339 can_put_echo_skb(skb
, dev
, 0);
341 flexcan_write(can_id
, ®s
->cantxfg
[FLEXCAN_TX_BUF_ID
].can_id
);
342 flexcan_write(ctrl
, ®s
->cantxfg
[FLEXCAN_TX_BUF_ID
].can_ctrl
);
347 static void do_bus_err(struct net_device
*dev
,
348 struct can_frame
*cf
, u32 reg_esr
)
350 struct flexcan_priv
*priv
= netdev_priv(dev
);
351 int rx_errors
= 0, tx_errors
= 0;
353 cf
->can_id
|= CAN_ERR_PROT
| CAN_ERR_BUSERROR
;
355 if (reg_esr
& FLEXCAN_ESR_BIT1_ERR
) {
356 netdev_dbg(dev
, "BIT1_ERR irq\n");
357 cf
->data
[2] |= CAN_ERR_PROT_BIT1
;
360 if (reg_esr
& FLEXCAN_ESR_BIT0_ERR
) {
361 netdev_dbg(dev
, "BIT0_ERR irq\n");
362 cf
->data
[2] |= CAN_ERR_PROT_BIT0
;
365 if (reg_esr
& FLEXCAN_ESR_ACK_ERR
) {
366 netdev_dbg(dev
, "ACK_ERR irq\n");
367 cf
->can_id
|= CAN_ERR_ACK
;
368 cf
->data
[3] |= CAN_ERR_PROT_LOC_ACK
;
371 if (reg_esr
& FLEXCAN_ESR_CRC_ERR
) {
372 netdev_dbg(dev
, "CRC_ERR irq\n");
373 cf
->data
[2] |= CAN_ERR_PROT_BIT
;
374 cf
->data
[3] |= CAN_ERR_PROT_LOC_CRC_SEQ
;
377 if (reg_esr
& FLEXCAN_ESR_FRM_ERR
) {
378 netdev_dbg(dev
, "FRM_ERR irq\n");
379 cf
->data
[2] |= CAN_ERR_PROT_FORM
;
382 if (reg_esr
& FLEXCAN_ESR_STF_ERR
) {
383 netdev_dbg(dev
, "STF_ERR irq\n");
384 cf
->data
[2] |= CAN_ERR_PROT_STUFF
;
388 priv
->can
.can_stats
.bus_error
++;
390 dev
->stats
.rx_errors
++;
392 dev
->stats
.tx_errors
++;
395 static int flexcan_poll_bus_err(struct net_device
*dev
, u32 reg_esr
)
398 struct can_frame
*cf
;
400 skb
= alloc_can_err_skb(dev
, &cf
);
404 do_bus_err(dev
, cf
, reg_esr
);
405 netif_receive_skb(skb
);
407 dev
->stats
.rx_packets
++;
408 dev
->stats
.rx_bytes
+= cf
->can_dlc
;
413 static void do_state(struct net_device
*dev
,
414 struct can_frame
*cf
, enum can_state new_state
)
416 struct flexcan_priv
*priv
= netdev_priv(dev
);
417 struct can_berr_counter bec
;
419 flexcan_get_berr_counter(dev
, &bec
);
421 switch (priv
->can
.state
) {
422 case CAN_STATE_ERROR_ACTIVE
:
425 * to : ERROR_WARNING, ERROR_PASSIVE, BUS_OFF
426 * => : there was a warning int
428 if (new_state
>= CAN_STATE_ERROR_WARNING
&&
429 new_state
<= CAN_STATE_BUS_OFF
) {
430 netdev_dbg(dev
, "Error Warning IRQ\n");
431 priv
->can
.can_stats
.error_warning
++;
433 cf
->can_id
|= CAN_ERR_CRTL
;
434 cf
->data
[1] = (bec
.txerr
> bec
.rxerr
) ?
435 CAN_ERR_CRTL_TX_WARNING
:
436 CAN_ERR_CRTL_RX_WARNING
;
438 case CAN_STATE_ERROR_WARNING
: /* fallthrough */
440 * from: ERROR_ACTIVE, ERROR_WARNING
441 * to : ERROR_PASSIVE, BUS_OFF
442 * => : error passive int
444 if (new_state
>= CAN_STATE_ERROR_PASSIVE
&&
445 new_state
<= CAN_STATE_BUS_OFF
) {
446 netdev_dbg(dev
, "Error Passive IRQ\n");
447 priv
->can
.can_stats
.error_passive
++;
449 cf
->can_id
|= CAN_ERR_CRTL
;
450 cf
->data
[1] = (bec
.txerr
> bec
.rxerr
) ?
451 CAN_ERR_CRTL_TX_PASSIVE
:
452 CAN_ERR_CRTL_RX_PASSIVE
;
455 case CAN_STATE_BUS_OFF
:
456 netdev_err(dev
, "BUG! "
457 "hardware recovered automatically from BUS_OFF\n");
463 /* process state changes depending on the new state */
465 case CAN_STATE_ERROR_ACTIVE
:
466 netdev_dbg(dev
, "Error Active\n");
467 cf
->can_id
|= CAN_ERR_PROT
;
468 cf
->data
[2] = CAN_ERR_PROT_ACTIVE
;
470 case CAN_STATE_BUS_OFF
:
471 cf
->can_id
|= CAN_ERR_BUSOFF
;
479 static int flexcan_poll_state(struct net_device
*dev
, u32 reg_esr
)
481 struct flexcan_priv
*priv
= netdev_priv(dev
);
483 struct can_frame
*cf
;
484 enum can_state new_state
;
487 flt
= reg_esr
& FLEXCAN_ESR_FLT_CONF_MASK
;
488 if (likely(flt
== FLEXCAN_ESR_FLT_CONF_ACTIVE
)) {
489 if (likely(!(reg_esr
& (FLEXCAN_ESR_TX_WRN
|
490 FLEXCAN_ESR_RX_WRN
))))
491 new_state
= CAN_STATE_ERROR_ACTIVE
;
493 new_state
= CAN_STATE_ERROR_WARNING
;
494 } else if (unlikely(flt
== FLEXCAN_ESR_FLT_CONF_PASSIVE
))
495 new_state
= CAN_STATE_ERROR_PASSIVE
;
497 new_state
= CAN_STATE_BUS_OFF
;
499 /* state hasn't changed */
500 if (likely(new_state
== priv
->can
.state
))
503 skb
= alloc_can_err_skb(dev
, &cf
);
507 do_state(dev
, cf
, new_state
);
508 priv
->can
.state
= new_state
;
509 netif_receive_skb(skb
);
511 dev
->stats
.rx_packets
++;
512 dev
->stats
.rx_bytes
+= cf
->can_dlc
;
517 static void flexcan_read_fifo(const struct net_device
*dev
,
518 struct can_frame
*cf
)
520 const struct flexcan_priv
*priv
= netdev_priv(dev
);
521 struct flexcan_regs __iomem
*regs
= priv
->base
;
522 struct flexcan_mb __iomem
*mb
= ®s
->cantxfg
[0];
523 u32 reg_ctrl
, reg_id
;
525 reg_ctrl
= flexcan_read(&mb
->can_ctrl
);
526 reg_id
= flexcan_read(&mb
->can_id
);
527 if (reg_ctrl
& FLEXCAN_MB_CNT_IDE
)
528 cf
->can_id
= ((reg_id
>> 0) & CAN_EFF_MASK
) | CAN_EFF_FLAG
;
530 cf
->can_id
= (reg_id
>> 18) & CAN_SFF_MASK
;
532 if (reg_ctrl
& FLEXCAN_MB_CNT_RTR
)
533 cf
->can_id
|= CAN_RTR_FLAG
;
534 cf
->can_dlc
= get_can_dlc((reg_ctrl
>> 16) & 0xf);
536 *(__be32
*)(cf
->data
+ 0) = cpu_to_be32(flexcan_read(&mb
->data
[0]));
537 *(__be32
*)(cf
->data
+ 4) = cpu_to_be32(flexcan_read(&mb
->data
[1]));
540 flexcan_write(FLEXCAN_IFLAG_RX_FIFO_AVAILABLE
, ®s
->iflag1
);
541 flexcan_read(®s
->timer
);
544 static int flexcan_read_frame(struct net_device
*dev
)
546 struct net_device_stats
*stats
= &dev
->stats
;
547 struct can_frame
*cf
;
550 skb
= alloc_can_skb(dev
, &cf
);
551 if (unlikely(!skb
)) {
556 flexcan_read_fifo(dev
, cf
);
557 netif_receive_skb(skb
);
560 stats
->rx_bytes
+= cf
->can_dlc
;
562 can_led_event(dev
, CAN_LED_EVENT_RX
);
567 static int flexcan_poll(struct napi_struct
*napi
, int quota
)
569 struct net_device
*dev
= napi
->dev
;
570 const struct flexcan_priv
*priv
= netdev_priv(dev
);
571 struct flexcan_regs __iomem
*regs
= priv
->base
;
572 u32 reg_iflag1
, reg_esr
;
576 * The error bits are cleared on read,
577 * use saved value from irq handler.
579 reg_esr
= flexcan_read(®s
->esr
) | priv
->reg_esr
;
581 /* handle state changes */
582 work_done
+= flexcan_poll_state(dev
, reg_esr
);
585 reg_iflag1
= flexcan_read(®s
->iflag1
);
586 while (reg_iflag1
& FLEXCAN_IFLAG_RX_FIFO_AVAILABLE
&&
588 work_done
+= flexcan_read_frame(dev
);
589 reg_iflag1
= flexcan_read(®s
->iflag1
);
592 /* report bus errors */
593 if (flexcan_has_and_handle_berr(priv
, reg_esr
) && work_done
< quota
)
594 work_done
+= flexcan_poll_bus_err(dev
, reg_esr
);
596 if (work_done
< quota
) {
599 flexcan_write(FLEXCAN_IFLAG_DEFAULT
, ®s
->imask1
);
600 flexcan_write(priv
->reg_ctrl_default
, ®s
->ctrl
);
606 static irqreturn_t
flexcan_irq(int irq
, void *dev_id
)
608 struct net_device
*dev
= dev_id
;
609 struct net_device_stats
*stats
= &dev
->stats
;
610 struct flexcan_priv
*priv
= netdev_priv(dev
);
611 struct flexcan_regs __iomem
*regs
= priv
->base
;
612 u32 reg_iflag1
, reg_esr
;
614 reg_iflag1
= flexcan_read(®s
->iflag1
);
615 reg_esr
= flexcan_read(®s
->esr
);
616 /* ACK all bus error and state change IRQ sources */
617 if (reg_esr
& FLEXCAN_ESR_ALL_INT
)
618 flexcan_write(reg_esr
& FLEXCAN_ESR_ALL_INT
, ®s
->esr
);
621 * schedule NAPI in case of:
624 * - bus error IRQ and bus error reporting is activated
626 if ((reg_iflag1
& FLEXCAN_IFLAG_RX_FIFO_AVAILABLE
) ||
627 (reg_esr
& FLEXCAN_ESR_ERR_STATE
) ||
628 flexcan_has_and_handle_berr(priv
, reg_esr
)) {
630 * The error bits are cleared on read,
631 * save them for later use.
633 priv
->reg_esr
= reg_esr
& FLEXCAN_ESR_ERR_BUS
;
634 flexcan_write(FLEXCAN_IFLAG_DEFAULT
&
635 ~FLEXCAN_IFLAG_RX_FIFO_AVAILABLE
, ®s
->imask1
);
636 flexcan_write(priv
->reg_ctrl_default
& ~FLEXCAN_CTRL_ERR_ALL
,
638 napi_schedule(&priv
->napi
);
642 if (reg_iflag1
& FLEXCAN_IFLAG_RX_FIFO_OVERFLOW
) {
643 flexcan_write(FLEXCAN_IFLAG_RX_FIFO_OVERFLOW
, ®s
->iflag1
);
644 dev
->stats
.rx_over_errors
++;
645 dev
->stats
.rx_errors
++;
648 /* transmission complete interrupt */
649 if (reg_iflag1
& (1 << FLEXCAN_TX_BUF_ID
)) {
650 stats
->tx_bytes
+= can_get_echo_skb(dev
, 0);
652 can_led_event(dev
, CAN_LED_EVENT_TX
);
653 flexcan_write((1 << FLEXCAN_TX_BUF_ID
), ®s
->iflag1
);
654 netif_wake_queue(dev
);
660 static void flexcan_set_bittiming(struct net_device
*dev
)
662 const struct flexcan_priv
*priv
= netdev_priv(dev
);
663 const struct can_bittiming
*bt
= &priv
->can
.bittiming
;
664 struct flexcan_regs __iomem
*regs
= priv
->base
;
667 reg
= flexcan_read(®s
->ctrl
);
668 reg
&= ~(FLEXCAN_CTRL_PRESDIV(0xff) |
669 FLEXCAN_CTRL_RJW(0x3) |
670 FLEXCAN_CTRL_PSEG1(0x7) |
671 FLEXCAN_CTRL_PSEG2(0x7) |
672 FLEXCAN_CTRL_PROPSEG(0x7) |
677 reg
|= FLEXCAN_CTRL_PRESDIV(bt
->brp
- 1) |
678 FLEXCAN_CTRL_PSEG1(bt
->phase_seg1
- 1) |
679 FLEXCAN_CTRL_PSEG2(bt
->phase_seg2
- 1) |
680 FLEXCAN_CTRL_RJW(bt
->sjw
- 1) |
681 FLEXCAN_CTRL_PROPSEG(bt
->prop_seg
- 1);
683 if (priv
->can
.ctrlmode
& CAN_CTRLMODE_LOOPBACK
)
684 reg
|= FLEXCAN_CTRL_LPB
;
685 if (priv
->can
.ctrlmode
& CAN_CTRLMODE_LISTENONLY
)
686 reg
|= FLEXCAN_CTRL_LOM
;
687 if (priv
->can
.ctrlmode
& CAN_CTRLMODE_3_SAMPLES
)
688 reg
|= FLEXCAN_CTRL_SMP
;
690 netdev_info(dev
, "writing ctrl=0x%08x\n", reg
);
691 flexcan_write(reg
, ®s
->ctrl
);
693 /* print chip status */
694 netdev_dbg(dev
, "%s: mcr=0x%08x ctrl=0x%08x\n", __func__
,
695 flexcan_read(®s
->mcr
), flexcan_read(®s
->ctrl
));
701 * this functions is entered with clocks enabled
704 static int flexcan_chip_start(struct net_device
*dev
)
706 struct flexcan_priv
*priv
= netdev_priv(dev
);
707 struct flexcan_regs __iomem
*regs
= priv
->base
;
709 u32 reg_mcr
, reg_ctrl
;
712 flexcan_chip_enable(priv
);
715 flexcan_write(FLEXCAN_MCR_SOFTRST
, ®s
->mcr
);
718 reg_mcr
= flexcan_read(®s
->mcr
);
719 if (reg_mcr
& FLEXCAN_MCR_SOFTRST
) {
720 netdev_err(dev
, "Failed to softreset can module (mcr=0x%08x)\n",
726 flexcan_set_bittiming(dev
);
734 * only supervisor access
740 reg_mcr
= flexcan_read(®s
->mcr
);
741 reg_mcr
&= ~FLEXCAN_MCR_MAXMB(0xff);
742 reg_mcr
|= FLEXCAN_MCR_FRZ
| FLEXCAN_MCR_FEN
| FLEXCAN_MCR_HALT
|
743 FLEXCAN_MCR_SUPV
| FLEXCAN_MCR_WRN_EN
|
744 FLEXCAN_MCR_IDAM_C
| FLEXCAN_MCR_SRX_DIS
|
745 FLEXCAN_MCR_MAXMB(FLEXCAN_TX_BUF_ID
);
746 netdev_dbg(dev
, "%s: writing mcr=0x%08x", __func__
, reg_mcr
);
747 flexcan_write(reg_mcr
, ®s
->mcr
);
752 * disable timer sync feature
754 * disable auto busoff recovery
755 * transmit lowest buffer first
757 * enable tx and rx warning interrupt
758 * enable bus off interrupt
759 * (== FLEXCAN_CTRL_ERR_STATE)
761 reg_ctrl
= flexcan_read(®s
->ctrl
);
762 reg_ctrl
&= ~FLEXCAN_CTRL_TSYN
;
763 reg_ctrl
|= FLEXCAN_CTRL_BOFF_REC
| FLEXCAN_CTRL_LBUF
|
764 FLEXCAN_CTRL_ERR_STATE
;
766 * enable the "error interrupt" (FLEXCAN_CTRL_ERR_MSK),
767 * on most Flexcan cores, too. Otherwise we don't get
768 * any error warning or passive interrupts.
770 if (priv
->devtype_data
->features
& FLEXCAN_HAS_BROKEN_ERR_STATE
||
771 priv
->can
.ctrlmode
& CAN_CTRLMODE_BERR_REPORTING
)
772 reg_ctrl
|= FLEXCAN_CTRL_ERR_MSK
;
774 /* save for later use */
775 priv
->reg_ctrl_default
= reg_ctrl
;
776 netdev_dbg(dev
, "%s: writing ctrl=0x%08x", __func__
, reg_ctrl
);
777 flexcan_write(reg_ctrl
, ®s
->ctrl
);
779 /* Abort any pending TX, mark Mailbox as INACTIVE */
780 flexcan_write(FLEXCAN_MB_CNT_CODE(0x4),
781 ®s
->cantxfg
[FLEXCAN_TX_BUF_ID
].can_ctrl
);
783 /* acceptance mask/acceptance code (accept everything) */
784 flexcan_write(0x0, ®s
->rxgmask
);
785 flexcan_write(0x0, ®s
->rx14mask
);
786 flexcan_write(0x0, ®s
->rx15mask
);
788 if (priv
->devtype_data
->features
& FLEXCAN_HAS_V10_FEATURES
)
789 flexcan_write(0x0, ®s
->rxfgmask
);
791 if (priv
->reg_xceiver
) {
792 err
= regulator_enable(priv
->reg_xceiver
);
797 /* synchronize with the can bus */
798 reg_mcr
= flexcan_read(®s
->mcr
);
799 reg_mcr
&= ~FLEXCAN_MCR_HALT
;
800 flexcan_write(reg_mcr
, ®s
->mcr
);
802 priv
->can
.state
= CAN_STATE_ERROR_ACTIVE
;
804 /* enable FIFO interrupts */
805 flexcan_write(FLEXCAN_IFLAG_DEFAULT
, ®s
->imask1
);
807 /* print chip status */
808 netdev_dbg(dev
, "%s: reading mcr=0x%08x ctrl=0x%08x\n", __func__
,
809 flexcan_read(®s
->mcr
), flexcan_read(®s
->ctrl
));
814 flexcan_chip_disable(priv
);
821 * this functions is entered with clocks enabled
824 static void flexcan_chip_stop(struct net_device
*dev
)
826 struct flexcan_priv
*priv
= netdev_priv(dev
);
827 struct flexcan_regs __iomem
*regs
= priv
->base
;
830 /* Disable all interrupts */
831 flexcan_write(0, ®s
->imask1
);
833 /* Disable + halt module */
834 reg
= flexcan_read(®s
->mcr
);
835 reg
|= FLEXCAN_MCR_MDIS
| FLEXCAN_MCR_HALT
;
836 flexcan_write(reg
, ®s
->mcr
);
838 if (priv
->reg_xceiver
)
839 regulator_disable(priv
->reg_xceiver
);
840 priv
->can
.state
= CAN_STATE_STOPPED
;
845 static int flexcan_open(struct net_device
*dev
)
847 struct flexcan_priv
*priv
= netdev_priv(dev
);
850 err
= clk_prepare_enable(priv
->clk_ipg
);
854 err
= clk_prepare_enable(priv
->clk_per
);
856 goto out_disable_ipg
;
858 err
= open_candev(dev
);
860 goto out_disable_per
;
862 err
= request_irq(dev
->irq
, flexcan_irq
, IRQF_SHARED
, dev
->name
, dev
);
866 /* start chip and queuing */
867 err
= flexcan_chip_start(dev
);
871 can_led_event(dev
, CAN_LED_EVENT_OPEN
);
873 napi_enable(&priv
->napi
);
874 netif_start_queue(dev
);
881 clk_disable_unprepare(priv
->clk_per
);
883 clk_disable_unprepare(priv
->clk_ipg
);
888 static int flexcan_close(struct net_device
*dev
)
890 struct flexcan_priv
*priv
= netdev_priv(dev
);
892 netif_stop_queue(dev
);
893 napi_disable(&priv
->napi
);
894 flexcan_chip_stop(dev
);
896 free_irq(dev
->irq
, dev
);
897 clk_disable_unprepare(priv
->clk_per
);
898 clk_disable_unprepare(priv
->clk_ipg
);
902 can_led_event(dev
, CAN_LED_EVENT_STOP
);
907 static int flexcan_set_mode(struct net_device
*dev
, enum can_mode mode
)
913 err
= flexcan_chip_start(dev
);
917 netif_wake_queue(dev
);
927 static const struct net_device_ops flexcan_netdev_ops
= {
928 .ndo_open
= flexcan_open
,
929 .ndo_stop
= flexcan_close
,
930 .ndo_start_xmit
= flexcan_start_xmit
,
933 static int register_flexcandev(struct net_device
*dev
)
935 struct flexcan_priv
*priv
= netdev_priv(dev
);
936 struct flexcan_regs __iomem
*regs
= priv
->base
;
939 err
= clk_prepare_enable(priv
->clk_ipg
);
943 err
= clk_prepare_enable(priv
->clk_per
);
945 goto out_disable_ipg
;
947 /* select "bus clock", chip must be disabled */
948 flexcan_chip_disable(priv
);
949 reg
= flexcan_read(®s
->ctrl
);
950 reg
|= FLEXCAN_CTRL_CLK_SRC
;
951 flexcan_write(reg
, ®s
->ctrl
);
953 flexcan_chip_enable(priv
);
955 /* set freeze, halt and activate FIFO, restrict register access */
956 reg
= flexcan_read(®s
->mcr
);
957 reg
|= FLEXCAN_MCR_FRZ
| FLEXCAN_MCR_HALT
|
958 FLEXCAN_MCR_FEN
| FLEXCAN_MCR_SUPV
;
959 flexcan_write(reg
, ®s
->mcr
);
962 * Currently we only support newer versions of this core
963 * featuring a RX FIFO. Older cores found on some Coldfire
964 * derivates are not yet supported.
966 reg
= flexcan_read(®s
->mcr
);
967 if (!(reg
& FLEXCAN_MCR_FEN
)) {
968 netdev_err(dev
, "Could not enable RX FIFO, unsupported core\n");
970 goto out_disable_per
;
973 err
= register_candev(dev
);
976 /* disable core and turn off clocks */
977 flexcan_chip_disable(priv
);
978 clk_disable_unprepare(priv
->clk_per
);
980 clk_disable_unprepare(priv
->clk_ipg
);
985 static void unregister_flexcandev(struct net_device
*dev
)
987 unregister_candev(dev
);
990 static const struct of_device_id flexcan_of_match
[] = {
991 { .compatible
= "fsl,imx6q-flexcan", .data
= &fsl_imx6q_devtype_data
, },
992 { .compatible
= "fsl,imx28-flexcan", .data
= &fsl_imx28_devtype_data
, },
993 { .compatible
= "fsl,p1010-flexcan", .data
= &fsl_p1010_devtype_data
, },
996 MODULE_DEVICE_TABLE(of
, flexcan_of_match
);
998 static const struct platform_device_id flexcan_id_table
[] = {
999 { .name
= "flexcan", .driver_data
= (kernel_ulong_t
)&fsl_p1010_devtype_data
, },
1002 MODULE_DEVICE_TABLE(platform
, flexcan_id_table
);
1004 static int flexcan_probe(struct platform_device
*pdev
)
1006 const struct of_device_id
*of_id
;
1007 const struct flexcan_devtype_data
*devtype_data
;
1008 struct net_device
*dev
;
1009 struct flexcan_priv
*priv
;
1010 struct resource
*mem
;
1011 struct clk
*clk_ipg
= NULL
, *clk_per
= NULL
;
1016 if (pdev
->dev
.of_node
)
1017 of_property_read_u32(pdev
->dev
.of_node
,
1018 "clock-frequency", &clock_freq
);
1021 clk_ipg
= devm_clk_get(&pdev
->dev
, "ipg");
1022 if (IS_ERR(clk_ipg
)) {
1023 dev_err(&pdev
->dev
, "no ipg clock defined\n");
1024 return PTR_ERR(clk_ipg
);
1027 clk_per
= devm_clk_get(&pdev
->dev
, "per");
1028 if (IS_ERR(clk_per
)) {
1029 dev_err(&pdev
->dev
, "no per clock defined\n");
1030 return PTR_ERR(clk_per
);
1032 clock_freq
= clk_get_rate(clk_per
);
1035 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1036 irq
= platform_get_irq(pdev
, 0);
1040 base
= devm_ioremap_resource(&pdev
->dev
, mem
);
1042 return PTR_ERR(base
);
1044 of_id
= of_match_device(flexcan_of_match
, &pdev
->dev
);
1046 devtype_data
= of_id
->data
;
1047 } else if (pdev
->id_entry
->driver_data
) {
1048 devtype_data
= (struct flexcan_devtype_data
*)
1049 pdev
->id_entry
->driver_data
;
1054 dev
= alloc_candev(sizeof(struct flexcan_priv
), 1);
1058 dev
->netdev_ops
= &flexcan_netdev_ops
;
1060 dev
->flags
|= IFF_ECHO
;
1062 priv
= netdev_priv(dev
);
1063 priv
->can
.clock
.freq
= clock_freq
;
1064 priv
->can
.bittiming_const
= &flexcan_bittiming_const
;
1065 priv
->can
.do_set_mode
= flexcan_set_mode
;
1066 priv
->can
.do_get_berr_counter
= flexcan_get_berr_counter
;
1067 priv
->can
.ctrlmode_supported
= CAN_CTRLMODE_LOOPBACK
|
1068 CAN_CTRLMODE_LISTENONLY
| CAN_CTRLMODE_3_SAMPLES
|
1069 CAN_CTRLMODE_BERR_REPORTING
;
1072 priv
->clk_ipg
= clk_ipg
;
1073 priv
->clk_per
= clk_per
;
1074 priv
->pdata
= dev_get_platdata(&pdev
->dev
);
1075 priv
->devtype_data
= devtype_data
;
1077 priv
->reg_xceiver
= devm_regulator_get(&pdev
->dev
, "xceiver");
1078 if (IS_ERR(priv
->reg_xceiver
))
1079 priv
->reg_xceiver
= NULL
;
1081 netif_napi_add(dev
, &priv
->napi
, flexcan_poll
, FLEXCAN_NAPI_WEIGHT
);
1083 platform_set_drvdata(pdev
, dev
);
1084 SET_NETDEV_DEV(dev
, &pdev
->dev
);
1086 err
= register_flexcandev(dev
);
1088 dev_err(&pdev
->dev
, "registering netdev failed\n");
1089 goto failed_register
;
1092 devm_can_led_init(dev
);
1094 dev_info(&pdev
->dev
, "device registered (reg_base=%p, irq=%d)\n",
1095 priv
->base
, dev
->irq
);
1104 static int flexcan_remove(struct platform_device
*pdev
)
1106 struct net_device
*dev
= platform_get_drvdata(pdev
);
1108 unregister_flexcandev(dev
);
1115 #ifdef CONFIG_PM_SLEEP
1116 static int flexcan_suspend(struct device
*device
)
1118 struct net_device
*dev
= dev_get_drvdata(device
);
1119 struct flexcan_priv
*priv
= netdev_priv(dev
);
1121 flexcan_chip_disable(priv
);
1123 if (netif_running(dev
)) {
1124 netif_stop_queue(dev
);
1125 netif_device_detach(dev
);
1127 priv
->can
.state
= CAN_STATE_SLEEPING
;
1132 static int flexcan_resume(struct device
*device
)
1134 struct net_device
*dev
= dev_get_drvdata(device
);
1135 struct flexcan_priv
*priv
= netdev_priv(dev
);
1137 priv
->can
.state
= CAN_STATE_ERROR_ACTIVE
;
1138 if (netif_running(dev
)) {
1139 netif_device_attach(dev
);
1140 netif_start_queue(dev
);
1142 flexcan_chip_enable(priv
);
1146 #endif /* CONFIG_PM_SLEEP */
1148 static SIMPLE_DEV_PM_OPS(flexcan_pm_ops
, flexcan_suspend
, flexcan_resume
);
1150 static struct platform_driver flexcan_driver
= {
1153 .owner
= THIS_MODULE
,
1154 .pm
= &flexcan_pm_ops
,
1155 .of_match_table
= flexcan_of_match
,
1157 .probe
= flexcan_probe
,
1158 .remove
= flexcan_remove
,
1159 .id_table
= flexcan_id_table
,
1162 module_platform_driver(flexcan_driver
);
1164 MODULE_AUTHOR("Sascha Hauer <kernel@pengutronix.de>, "
1165 "Marc Kleine-Budde <kernel@pengutronix.de>");
1166 MODULE_LICENSE("GPL v2");
1167 MODULE_DESCRIPTION("CAN port driver for flexcan based chip");