22 #include <linux/netdevice.h>
29 #include <linux/if_arp.h>
30 #include <linux/if_ether.h>
33 #include <linux/kernel.h>
34 #include <linux/list.h>
35 #include <linux/module.h>
41 #define DRV_NAME "flexcan"
44 #define FLEXCAN_NAPI_WEIGHT (8 + 2)
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) & 0xf)
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)
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)
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)
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))
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)
161 #define FLEXCAN_HAS_V10_FEATURES BIT(1)
162 #define FLEXCAN_HAS_BROKEN_ERR_STATE BIT(2)
239 #if defined(__BIG_ENDIAN)
264 static void flexcan_transceiver_switch(
const struct flexcan_priv *
priv,
int on)
266 if (priv->
pdata && priv->
pdata->transceiver_switch)
267 priv->
pdata->transceiver_switch(on);
270 static inline int flexcan_has_and_handle_berr(
const struct flexcan_priv *priv,
277 static inline void flexcan_chip_enable(
struct flexcan_priv *priv)
282 reg = flexcan_read(®s->
mcr);
284 flexcan_write(reg, ®s->
mcr);
289 static inline void flexcan_chip_disable(
struct flexcan_priv *priv)
294 reg = flexcan_read(®s->
mcr);
296 flexcan_write(reg, ®s->
mcr);
299 static int flexcan_get_berr_counter(
const struct net_device *
dev,
306 bec->
txerr = (reg >> 0) & 0xff;
307 bec->
rxerr = (reg >> 8) & 0xff;
320 if (can_dropped_invalid_skb(dev, skb))
323 netif_stop_queue(dev);
352 static void do_bus_err(
struct net_device *dev,
393 priv->
can.can_stats.bus_error++;
395 dev->
stats.rx_errors++;
397 dev->
stats.tx_errors++;
400 static int flexcan_poll_bus_err(
struct net_device *dev,
u32 reg_esr)
409 do_bus_err(dev, cf, reg_esr);
412 dev->
stats.rx_packets++;
424 flexcan_get_berr_counter(dev, &bec);
426 switch (priv->
can.state) {
436 priv->
can.can_stats.error_warning++;
439 cf->data[1] = (bec.txerr > bec.rxerr) ?
452 priv->
can.can_stats.error_passive++;
455 cf->data[1] = (bec.txerr > bec.rxerr) ?
461 netdev_err(dev,
"BUG! "
462 "hardware recovered automatically from BUS_OFF\n");
484 static int flexcan_poll_state(
struct net_device *dev,
u32 reg_esr)
505 if (
likely(new_state == priv->
can.state))
512 do_state(dev, cf, new_state);
516 dev->
stats.rx_packets++;
522 static void flexcan_read_fifo(
const struct net_device *dev,
528 u32 reg_ctrl, reg_id;
530 reg_ctrl = flexcan_read(&mb->
can_ctrl);
531 reg_id = flexcan_read(&mb->
can_id);
546 flexcan_read(®s->
timer);
549 static int flexcan_read_frame(
struct net_device *dev)
561 flexcan_read_fifo(dev, cf);
575 u32 reg_iflag1, reg_esr;
582 reg_esr = flexcan_read(®s->
esr) | priv->
reg_esr;
585 work_done += flexcan_poll_state(dev, reg_esr);
588 reg_iflag1 = flexcan_read(®s->
iflag1);
591 work_done += flexcan_read_frame(dev);
592 reg_iflag1 = flexcan_read(®s->
iflag1);
596 if (flexcan_has_and_handle_berr(priv, reg_esr) && work_done < quota)
597 work_done += flexcan_poll_bus_err(dev, reg_esr);
599 if (work_done < quota) {
615 u32 reg_iflag1, reg_esr;
617 reg_iflag1 = flexcan_read(®s->
iflag1);
618 reg_esr = flexcan_read(®s->
esr);
621 flexcan_write(reg_esr & FLEXCAN_ESR_ALL_INT, ®s->
esr);
631 flexcan_has_and_handle_berr(priv, reg_esr)) {
638 ~FLEXCAN_IFLAG_RX_FIFO_AVAILABLE, ®s->
imask1);
641 napi_schedule(&priv->
napi);
646 flexcan_write(FLEXCAN_IFLAG_RX_FIFO_OVERFLOW, ®s->
iflag1);
647 dev->
stats.rx_over_errors++;
648 dev->
stats.rx_errors++;
656 netif_wake_queue(dev);
662 static void flexcan_set_bittiming(
struct net_device *dev)
669 reg = flexcan_read(®s->
ctrl);
692 netdev_info(dev,
"writing ctrl=0x%08x\n", reg);
693 flexcan_write(reg, ®s->
ctrl);
696 netdev_dbg(dev,
"%s: mcr=0x%08x ctrl=0x%08x\n", __func__,
697 flexcan_read(®s->
mcr), flexcan_read(®s->
ctrl));
706 static int flexcan_chip_start(
struct net_device *dev)
712 u32 reg_mcr, reg_ctrl;
715 flexcan_chip_enable(priv);
721 reg_mcr = flexcan_read(®s->
mcr);
723 netdev_err(dev,
"Failed to softreset can module (mcr=0x%08x)\n",
729 flexcan_set_bittiming(dev);
743 reg_mcr = flexcan_read(®s->
mcr);
747 netdev_dbg(dev,
"%s: writing mcr=0x%08x", __func__, reg_mcr);
748 flexcan_write(reg_mcr, ®s->
mcr);
762 reg_ctrl = flexcan_read(®s->
ctrl);
777 netdev_dbg(dev,
"%s: writing ctrl=0x%08x", __func__, reg_ctrl);
778 flexcan_write(reg_ctrl, ®s->
ctrl);
781 flexcan_write(0, ®s->
cantxfg[i].can_ctrl);
782 flexcan_write(0, ®s->
cantxfg[i].can_id);
783 flexcan_write(0, ®s->
cantxfg[i].data[0]);
784 flexcan_write(0, ®s->
cantxfg[i].data[1]);
792 flexcan_write(0x0, ®s->
rxgmask);
793 flexcan_write(0x0, ®s->
rx14mask);
794 flexcan_write(0x0, ®s->
rx15mask);
797 flexcan_write(0x0, ®s->
rxfgmask);
799 flexcan_transceiver_switch(priv, 1);
802 reg_mcr = flexcan_read(®s->
mcr);
804 flexcan_write(reg_mcr, ®s->
mcr);
812 netdev_dbg(dev,
"%s: reading mcr=0x%08x ctrl=0x%08x\n", __func__,
813 flexcan_read(®s->
mcr), flexcan_read(®s->
ctrl));
818 flexcan_chip_disable(priv);
828 static void flexcan_chip_stop(
struct net_device *dev)
835 flexcan_write(0, ®s->
imask1);
838 reg = flexcan_read(®s->
mcr);
840 flexcan_write(reg, ®s->
mcr);
842 flexcan_transceiver_switch(priv, 0);
848 static int flexcan_open(
struct net_device *dev)
853 clk_prepare_enable(priv->
clk_ipg);
854 clk_prepare_enable(priv->
clk_per);
865 err = flexcan_chip_start(dev);
868 napi_enable(&priv->
napi);
869 netif_start_queue(dev);
876 clk_disable_unprepare(priv->
clk_per);
877 clk_disable_unprepare(priv->
clk_ipg);
882 static int flexcan_close(
struct net_device *dev)
886 netif_stop_queue(dev);
887 napi_disable(&priv->
napi);
888 flexcan_chip_stop(dev);
891 clk_disable_unprepare(priv->
clk_per);
892 clk_disable_unprepare(priv->
clk_ipg);
905 err = flexcan_chip_start(dev);
909 netif_wake_queue(dev);
920 .ndo_open = flexcan_open,
921 .ndo_stop = flexcan_close,
922 .ndo_start_xmit = flexcan_start_xmit,
931 clk_prepare_enable(priv->
clk_ipg);
932 clk_prepare_enable(priv->
clk_per);
935 flexcan_chip_disable(priv);
936 reg = flexcan_read(®s->
ctrl);
938 flexcan_write(reg, ®s->
ctrl);
940 flexcan_chip_enable(priv);
943 reg = flexcan_read(®s->
mcr);
946 flexcan_write(reg, ®s->
mcr);
953 reg = flexcan_read(®s->
mcr);
955 netdev_err(dev,
"Could not enable RX FIFO, unsupported core\n");
964 flexcan_chip_disable(priv);
965 clk_disable_unprepare(priv->
clk_per);
966 clk_disable_unprepare(priv->
clk_ipg);
977 { .compatible =
"fsl,p1010-flexcan", .data = &fsl_p1010_devtype_data, },
978 { .compatible =
"fsl,imx28-flexcan", .data = &fsl_imx28_devtype_data, },
979 { .compatible =
"fsl,imx6q-flexcan", .data = &fsl_imx6q_devtype_data, },
984 { .name =
"flexcan", .driver_data = (
kernel_ulong_t)&fsl_p1010_devtype_data, },
1002 pinctrl = devm_pinctrl_get_select_default(&pdev->
dev);
1003 if (IS_ERR(pinctrl))
1004 return PTR_ERR(pinctrl);
1006 if (pdev->
dev.of_node)
1007 of_property_read_u32(pdev->
dev.of_node,
1008 "clock-frequency", &clock_freq);
1012 if (IS_ERR(clk_ipg)) {
1013 dev_err(&pdev->
dev,
"no ipg clock defined\n");
1014 err = PTR_ERR(clk_ipg);
1020 if (IS_ERR(clk_per)) {
1021 dev_err(&pdev->
dev,
"no per clock defined\n");
1022 err = PTR_ERR(clk_per);
1029 if (!mem || irq <= 0) {
1034 mem_size = resource_size(mem);
1054 devtype_data = of_id->
data;
1055 }
else if (pdev->
id_entry->driver_data) {
1060 goto failed_devtype;
1067 priv = netdev_priv(dev);
1068 priv->
can.clock.freq = clock_freq;
1069 priv->
can.bittiming_const = &flexcan_bittiming_const;
1070 priv->
can.do_set_mode = flexcan_set_mode;
1071 priv->
can.do_get_berr_counter = flexcan_get_berr_counter;
1079 priv->
pdata = pdev->
dev.platform_data;
1087 err = register_flexcandev(dev);
1089 dev_err(&pdev->
dev,
"registering netdev failed\n");
1090 goto failed_register;
1093 dev_info(&pdev->
dev,
"device registered (reg_base=%p, irq=%d)\n",
1112 struct net_device *dev = platform_get_drvdata(pdev);
1116 unregister_flexcandev(dev);
1117 platform_set_drvdata(pdev,
NULL);
1131 struct net_device *dev = platform_get_drvdata(pdev);
1134 flexcan_chip_disable(priv);
1136 if (netif_running(dev)) {
1137 netif_stop_queue(dev);
1147 struct net_device *dev = platform_get_drvdata(pdev);
1151 if (netif_running(dev)) {
1153 netif_start_queue(dev);
1155 flexcan_chip_enable(priv);
1160 #define flexcan_suspend NULL
1161 #define flexcan_resume NULL
1168 .of_match_table = flexcan_of_match,
1170 .probe = flexcan_probe,
1174 .id_table = flexcan_id_table,