blob: d53a45bf2a72eb9e0d3cd16fe9f876bccbe5ade5 [file] [log] [blame]
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001/*
2 * flexcan.c - FLEXCAN CAN controller driver
3 *
4 * Copyright (c) 2005-2006 Varma Electronics Oy
5 * Copyright (c) 2009 Sascha Hauer, Pengutronix
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +02006 * Copyright (c) 2010-2017 Pengutronix, Marc Kleine-Budde <kernel@pengutronix.de>
7 * Copyright (c) 2014 David Jander, Protonic Holland
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02008 *
9 * Based on code originally by Andrey Volkov <avolkov@varma-el.com>
10 *
11 * LICENCE:
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation version 2.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 */
22
23#include <linux/netdevice.h>
24#include <linux/can.h>
25#include <linux/can/dev.h>
26#include <linux/can/error.h>
Fabio Baltieriadccadb2012-12-18 18:50:58 +010027#include <linux/can/led.h>
Marc Kleine-Budde30164752015-05-10 15:26:58 +020028#include <linux/can/rx-offload.h>
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +020029#include <linux/clk.h>
30#include <linux/delay.h>
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +020031#include <linux/interrupt.h>
32#include <linux/io.h>
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +020033#include <linux/module.h>
holt@sgi.com97efe9a2011-08-16 17:32:23 +000034#include <linux/of.h>
Hui Wang30c1e672012-06-28 16:21:35 +080035#include <linux/of_device.h>
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +020036#include <linux/platform_device.h>
Fabio Estevamb7c41142013-06-10 23:12:57 -030037#include <linux/regulator/consumer.h>
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +020038
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +020039#define DRV_NAME "flexcan"
40
41/* 8 for RX fifo and 2 error handling */
42#define FLEXCAN_NAPI_WEIGHT (8 + 2)
43
44/* FLEXCAN module configuration register (CANMCR) bits */
45#define FLEXCAN_MCR_MDIS BIT(31)
46#define FLEXCAN_MCR_FRZ BIT(30)
47#define FLEXCAN_MCR_FEN BIT(29)
48#define FLEXCAN_MCR_HALT BIT(28)
49#define FLEXCAN_MCR_NOT_RDY BIT(27)
50#define FLEXCAN_MCR_WAK_MSK BIT(26)
51#define FLEXCAN_MCR_SOFTRST BIT(25)
52#define FLEXCAN_MCR_FRZ_ACK BIT(24)
53#define FLEXCAN_MCR_SUPV BIT(23)
54#define FLEXCAN_MCR_SLF_WAK BIT(22)
55#define FLEXCAN_MCR_WRN_EN BIT(21)
56#define FLEXCAN_MCR_LPM_ACK BIT(20)
57#define FLEXCAN_MCR_WAK_SRC BIT(19)
58#define FLEXCAN_MCR_DOZE BIT(18)
59#define FLEXCAN_MCR_SRX_DIS BIT(17)
Marc Kleine-Budde62d10862015-08-27 16:01:27 +020060#define FLEXCAN_MCR_IRMQ BIT(16)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +020061#define FLEXCAN_MCR_LPRIO_EN BIT(13)
62#define FLEXCAN_MCR_AEN BIT(12)
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +020063/* MCR_MAXMB: maximum used MBs is MAXMB + 1 */
Marc Kleine-Budde4c728d82014-09-02 16:54:17 +020064#define FLEXCAN_MCR_MAXMB(x) ((x) & 0x7f)
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +020065#define FLEXCAN_MCR_IDAM_A (0x0 << 8)
66#define FLEXCAN_MCR_IDAM_B (0x1 << 8)
67#define FLEXCAN_MCR_IDAM_C (0x2 << 8)
68#define FLEXCAN_MCR_IDAM_D (0x3 << 8)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +020069
70/* FLEXCAN control register (CANCTRL) bits */
71#define FLEXCAN_CTRL_PRESDIV(x) (((x) & 0xff) << 24)
72#define FLEXCAN_CTRL_RJW(x) (((x) & 0x03) << 22)
73#define FLEXCAN_CTRL_PSEG1(x) (((x) & 0x07) << 19)
74#define FLEXCAN_CTRL_PSEG2(x) (((x) & 0x07) << 16)
75#define FLEXCAN_CTRL_BOFF_MSK BIT(15)
76#define FLEXCAN_CTRL_ERR_MSK BIT(14)
77#define FLEXCAN_CTRL_CLK_SRC BIT(13)
78#define FLEXCAN_CTRL_LPB BIT(12)
79#define FLEXCAN_CTRL_TWRN_MSK BIT(11)
80#define FLEXCAN_CTRL_RWRN_MSK BIT(10)
81#define FLEXCAN_CTRL_SMP BIT(7)
82#define FLEXCAN_CTRL_BOFF_REC BIT(6)
83#define FLEXCAN_CTRL_TSYN BIT(5)
84#define FLEXCAN_CTRL_LBUF BIT(4)
85#define FLEXCAN_CTRL_LOM BIT(3)
86#define FLEXCAN_CTRL_PROPSEG(x) ((x) & 0x07)
87#define FLEXCAN_CTRL_ERR_BUS (FLEXCAN_CTRL_ERR_MSK)
88#define FLEXCAN_CTRL_ERR_STATE \
89 (FLEXCAN_CTRL_TWRN_MSK | FLEXCAN_CTRL_RWRN_MSK | \
90 FLEXCAN_CTRL_BOFF_MSK)
91#define FLEXCAN_CTRL_ERR_ALL \
92 (FLEXCAN_CTRL_ERR_BUS | FLEXCAN_CTRL_ERR_STATE)
93
Stefan Agnercdce8442014-07-15 14:56:21 +020094/* FLEXCAN control register 2 (CTRL2) bits */
Marc Kleine-Budde6f75fce2014-09-23 11:03:01 +020095#define FLEXCAN_CTRL2_ECRWRE BIT(29)
96#define FLEXCAN_CTRL2_WRMFRZ BIT(28)
97#define FLEXCAN_CTRL2_RFFN(x) (((x) & 0x0f) << 24)
98#define FLEXCAN_CTRL2_TASD(x) (((x) & 0x1f) << 19)
99#define FLEXCAN_CTRL2_MRP BIT(18)
100#define FLEXCAN_CTRL2_RRS BIT(17)
101#define FLEXCAN_CTRL2_EACEN BIT(16)
Stefan Agnercdce8442014-07-15 14:56:21 +0200102
103/* FLEXCAN memory error control register (MECR) bits */
104#define FLEXCAN_MECR_ECRWRDIS BIT(31)
105#define FLEXCAN_MECR_HANCEI_MSK BIT(19)
106#define FLEXCAN_MECR_FANCEI_MSK BIT(18)
107#define FLEXCAN_MECR_CEI_MSK BIT(16)
108#define FLEXCAN_MECR_HAERRIE BIT(15)
109#define FLEXCAN_MECR_FAERRIE BIT(14)
110#define FLEXCAN_MECR_EXTERRIE BIT(13)
111#define FLEXCAN_MECR_RERRDIS BIT(9)
112#define FLEXCAN_MECR_ECCDIS BIT(8)
113#define FLEXCAN_MECR_NCEFAFRZ BIT(7)
114
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200115/* FLEXCAN error and status register (ESR) bits */
116#define FLEXCAN_ESR_TWRN_INT BIT(17)
117#define FLEXCAN_ESR_RWRN_INT BIT(16)
118#define FLEXCAN_ESR_BIT1_ERR BIT(15)
119#define FLEXCAN_ESR_BIT0_ERR BIT(14)
120#define FLEXCAN_ESR_ACK_ERR BIT(13)
121#define FLEXCAN_ESR_CRC_ERR BIT(12)
122#define FLEXCAN_ESR_FRM_ERR BIT(11)
123#define FLEXCAN_ESR_STF_ERR BIT(10)
124#define FLEXCAN_ESR_TX_WRN BIT(9)
125#define FLEXCAN_ESR_RX_WRN BIT(8)
126#define FLEXCAN_ESR_IDLE BIT(7)
127#define FLEXCAN_ESR_TXRX BIT(6)
128#define FLEXCAN_EST_FLT_CONF_SHIFT (4)
129#define FLEXCAN_ESR_FLT_CONF_MASK (0x3 << FLEXCAN_EST_FLT_CONF_SHIFT)
130#define FLEXCAN_ESR_FLT_CONF_ACTIVE (0x0 << FLEXCAN_EST_FLT_CONF_SHIFT)
131#define FLEXCAN_ESR_FLT_CONF_PASSIVE (0x1 << FLEXCAN_EST_FLT_CONF_SHIFT)
132#define FLEXCAN_ESR_BOFF_INT BIT(2)
133#define FLEXCAN_ESR_ERR_INT BIT(1)
134#define FLEXCAN_ESR_WAK_INT BIT(0)
135#define FLEXCAN_ESR_ERR_BUS \
136 (FLEXCAN_ESR_BIT1_ERR | FLEXCAN_ESR_BIT0_ERR | \
137 FLEXCAN_ESR_ACK_ERR | FLEXCAN_ESR_CRC_ERR | \
138 FLEXCAN_ESR_FRM_ERR | FLEXCAN_ESR_STF_ERR)
139#define FLEXCAN_ESR_ERR_STATE \
140 (FLEXCAN_ESR_TWRN_INT | FLEXCAN_ESR_RWRN_INT | FLEXCAN_ESR_BOFF_INT)
141#define FLEXCAN_ESR_ERR_ALL \
142 (FLEXCAN_ESR_ERR_BUS | FLEXCAN_ESR_ERR_STATE)
Wolfgang Grandegger6e9d5542011-12-12 16:09:28 +0100143#define FLEXCAN_ESR_ALL_INT \
144 (FLEXCAN_ESR_TWRN_INT | FLEXCAN_ESR_RWRN_INT | \
145 FLEXCAN_ESR_BOFF_INT | FLEXCAN_ESR_ERR_INT)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200146
147/* FLEXCAN interrupt flag register (IFLAG) bits */
David Jander25e92442014-09-03 16:47:22 +0200148/* Errata ERR005829 step7: Reserve first valid MB */
Marc Kleine-Buddeb93917c2015-07-12 00:47:47 +0200149#define FLEXCAN_TX_MB_RESERVED_OFF_FIFO 8
150#define FLEXCAN_TX_MB_OFF_FIFO 9
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200151#define FLEXCAN_TX_MB_RESERVED_OFF_TIMESTAMP 0
152#define FLEXCAN_TX_MB_OFF_TIMESTAMP 1
153#define FLEXCAN_RX_MB_OFF_TIMESTAMP_FIRST (FLEXCAN_TX_MB_OFF_TIMESTAMP + 1)
154#define FLEXCAN_RX_MB_OFF_TIMESTAMP_LAST 63
Marc Kleine-Buddeb93917c2015-07-12 00:47:47 +0200155#define FLEXCAN_IFLAG_MB(x) BIT(x)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200156#define FLEXCAN_IFLAG_RX_FIFO_OVERFLOW BIT(7)
157#define FLEXCAN_IFLAG_RX_FIFO_WARN BIT(6)
158#define FLEXCAN_IFLAG_RX_FIFO_AVAILABLE BIT(5)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200159
160/* FLEXCAN message buffers */
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200161#define FLEXCAN_MB_CODE_MASK (0xf << 24)
162#define FLEXCAN_MB_CODE_RX_BUSY_BIT (0x1 << 24)
Marc Kleine-Buddec32fe4a2014-09-16 12:39:28 +0200163#define FLEXCAN_MB_CODE_RX_INACTIVE (0x0 << 24)
164#define FLEXCAN_MB_CODE_RX_EMPTY (0x4 << 24)
165#define FLEXCAN_MB_CODE_RX_FULL (0x2 << 24)
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200166#define FLEXCAN_MB_CODE_RX_OVERRUN (0x6 << 24)
Marc Kleine-Buddec32fe4a2014-09-16 12:39:28 +0200167#define FLEXCAN_MB_CODE_RX_RANSWER (0xa << 24)
168
169#define FLEXCAN_MB_CODE_TX_INACTIVE (0x8 << 24)
170#define FLEXCAN_MB_CODE_TX_ABORT (0x9 << 24)
171#define FLEXCAN_MB_CODE_TX_DATA (0xc << 24)
172#define FLEXCAN_MB_CODE_TX_TANSWER (0xe << 24)
173
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200174#define FLEXCAN_MB_CNT_SRR BIT(22)
175#define FLEXCAN_MB_CNT_IDE BIT(21)
176#define FLEXCAN_MB_CNT_RTR BIT(20)
177#define FLEXCAN_MB_CNT_LENGTH(x) (((x) & 0xf) << 16)
178#define FLEXCAN_MB_CNT_TIMESTAMP(x) ((x) & 0xffff)
179
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200180#define FLEXCAN_TIMEOUT_US (50)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200181
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200182/* FLEXCAN hardware feature flags
Wolfgang Grandeggerbb698ca2012-10-10 21:10:42 +0200183 *
184 * Below is some version info we got:
ZHU Yi (ST-FIR/ENG1-Zhu)da49a802017-09-15 07:03:58 +0000185 * SOC Version IP-Version Glitch- [TR]WRN_INT IRQ Err Memory err RTR re-
186 * Filter? connected? Passive detection ception in MB
Marc Kleine-Budde658f5342017-11-22 13:01:08 +0100187 * MX25 FlexCAN2 03.00.00.00 no no no no no
ZHU Yi (ST-FIR/ENG1-Zhu)da49a802017-09-15 07:03:58 +0000188 * MX28 FlexCAN2 03.00.04.00 yes yes no no no
Marc Kleine-Budde658f5342017-11-22 13:01:08 +0100189 * MX35 FlexCAN2 03.00.00.00 no no no no no
ZHU Yi (ST-FIR/ENG1-Zhu)da49a802017-09-15 07:03:58 +0000190 * MX53 FlexCAN2 03.00.00.00 yes no no no no
191 * MX6s FlexCAN3 10.00.12.00 yes yes no no yes
Marc Kleine-Budde29c64b12017-11-27 09:18:21 +0100192 * VF610 FlexCAN3 ? no yes no yes yes?
Pankaj Bansal99b76682017-11-24 18:52:09 +0530193 * LS1021A FlexCAN2 03.00.04.00 no yes no no yes
Wolfgang Grandeggerbb698ca2012-10-10 21:10:42 +0200194 *
195 * Some SOCs do not have the RX_WARN & TX_WARN interrupt line connected.
196 */
ZHU Yi (ST-FIR/ENG1-Zhu)2f8639b2017-09-15 07:01:23 +0000197#define FLEXCAN_QUIRK_BROKEN_WERR_STATE BIT(1) /* [TR]WRN_INT not connected */
Marc Kleine-Buddef377bff2015-05-08 15:22:36 +0200198#define FLEXCAN_QUIRK_DISABLE_RXFG BIT(2) /* Disable RX FIFO Global mask */
Marc Kleine-Budde9eb7aa82015-09-01 08:57:55 +0200199#define FLEXCAN_QUIRK_ENABLE_EACEN_RRS BIT(3) /* Enable EACEN and RRS bit in ctrl2 */
Marc Kleine-Budde66ddb822017-03-02 15:42:49 +0100200#define FLEXCAN_QUIRK_DISABLE_MECR BIT(4) /* Disable Memory error detection */
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200201#define FLEXCAN_QUIRK_USE_OFF_TIMESTAMP BIT(5) /* Use timestamp based offloading */
ZHU Yi (ST-FIR/ENG1-Zhu)da49a802017-09-15 07:03:58 +0000202#define FLEXCAN_QUIRK_BROKEN_PERR_STATE BIT(6) /* No interrupt for error passive */
Uwe Kleine-König0e030a32018-04-25 16:50:39 +0200203#define FLEXCAN_QUIRK_DEFAULT_BIG_ENDIAN BIT(7) /* default to BE register access */
Wolfgang Grandegger4f72e5f2012-09-28 03:17:15 +0000204
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200205/* Structure of the message buffer */
206struct flexcan_mb {
207 u32 can_ctrl;
208 u32 can_id;
209 u32 data[2];
210};
211
212/* Structure of the hardware registers */
213struct flexcan_regs {
214 u32 mcr; /* 0x00 */
215 u32 ctrl; /* 0x04 */
216 u32 timer; /* 0x08 */
217 u32 _reserved1; /* 0x0c */
218 u32 rxgmask; /* 0x10 */
219 u32 rx14mask; /* 0x14 */
220 u32 rx15mask; /* 0x18 */
221 u32 ecr; /* 0x1c */
222 u32 esr; /* 0x20 */
223 u32 imask2; /* 0x24 */
224 u32 imask1; /* 0x28 */
225 u32 iflag2; /* 0x2c */
226 u32 iflag1; /* 0x30 */
Marc Kleine-Budde62d10862015-08-27 16:01:27 +0200227 union { /* 0x34 */
228 u32 gfwr_mx28; /* MX28, MX53 */
229 u32 ctrl2; /* MX6, VF610 */
230 };
Hui Wang30c1e672012-06-28 16:21:35 +0800231 u32 esr2; /* 0x38 */
232 u32 imeur; /* 0x3c */
233 u32 lrfr; /* 0x40 */
234 u32 crcr; /* 0x44 */
235 u32 rxfgmask; /* 0x48 */
236 u32 rxfir; /* 0x4c */
Stefan Agnercdce8442014-07-15 14:56:21 +0200237 u32 _reserved3[12]; /* 0x50 */
Marc Kleine-Budde1ba763d2015-08-25 10:39:19 +0200238 struct flexcan_mb mb[64]; /* 0x80 */
Marc Kleine-Budde66a6ef02014-09-17 12:50:48 +0200239 /* FIFO-mode:
240 * MB
241 * 0x080...0x08f 0 RX message buffer
242 * 0x090...0x0df 1-5 reserverd
243 * 0x0e0...0x0ff 6-7 8 entry ID table
244 * (mx25, mx28, mx35, mx53)
245 * 0x0e0...0x2df 6-7..37 8..128 entry ID table
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200246 * size conf'ed via ctrl2::RFFN
Marc Kleine-Budde66a6ef02014-09-17 12:50:48 +0200247 * (mx6, vf610)
248 */
Marc Kleine-Budde62d10862015-08-27 16:01:27 +0200249 u32 _reserved4[256]; /* 0x480 */
250 u32 rximr[64]; /* 0x880 */
251 u32 _reserved5[24]; /* 0x980 */
252 u32 gfwr_mx6; /* 0x9e0 - MX6 */
253 u32 _reserved6[63]; /* 0x9e4 */
Stefan Agnercdce8442014-07-15 14:56:21 +0200254 u32 mecr; /* 0xae0 */
255 u32 erriar; /* 0xae4 */
256 u32 erridpr; /* 0xae8 */
257 u32 errippr; /* 0xaec */
258 u32 rerrar; /* 0xaf0 */
259 u32 rerrdr; /* 0xaf4 */
260 u32 rerrsynr; /* 0xaf8 */
261 u32 errsr; /* 0xafc */
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200262};
263
Hui Wang30c1e672012-06-28 16:21:35 +0800264struct flexcan_devtype_data {
Marc Kleine-Buddef377bff2015-05-08 15:22:36 +0200265 u32 quirks; /* quirks needed for different IP cores */
Hui Wang30c1e672012-06-28 16:21:35 +0800266};
267
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200268struct flexcan_priv {
269 struct can_priv can;
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200270 struct can_rx_offload offload;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200271
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200272 struct flexcan_regs __iomem *regs;
Marc Kleine-Buddeb93917c2015-07-12 00:47:47 +0200273 struct flexcan_mb __iomem *tx_mb;
274 struct flexcan_mb __iomem *tx_mb_reserved;
275 u8 tx_mb_idx;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200276 u32 reg_ctrl_default;
Marc Kleine-Budde28ac7dc2015-08-04 13:46:10 +0200277 u32 reg_imask1_default;
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200278 u32 reg_imask2_default;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200279
Steffen Trumtrar3d42a372012-07-17 16:14:34 +0200280 struct clk *clk_ipg;
281 struct clk *clk_per;
Marc Kleine-Buddedda0b3b2012-07-13 14:52:48 +0200282 const struct flexcan_devtype_data *devtype_data;
Fabio Estevamb7c41142013-06-10 23:12:57 -0300283 struct regulator *reg_xceiver;
Pankaj Bansal88462d22017-11-24 18:52:08 +0530284
285 /* Read and Write APIs */
286 u32 (*read)(void __iomem *addr);
287 void (*write)(u32 val, void __iomem *addr);
Hui Wang30c1e672012-06-28 16:21:35 +0800288};
289
Marc Kleine-Buddea3c11a72016-07-04 14:45:44 +0200290static const struct flexcan_devtype_data fsl_p1010_devtype_data = {
ZHU Yi (ST-FIR/ENG1-Zhu)fb5b91d62017-09-15 07:09:37 +0000291 .quirks = FLEXCAN_QUIRK_BROKEN_WERR_STATE |
Uwe Kleine-König0e030a32018-04-25 16:50:39 +0200292 FLEXCAN_QUIRK_BROKEN_PERR_STATE |
293 FLEXCAN_QUIRK_DEFAULT_BIG_ENDIAN,
294};
295
296static const struct flexcan_devtype_data fsl_imx25_devtype_data = {
297 .quirks = FLEXCAN_QUIRK_BROKEN_WERR_STATE |
ZHU Yi (ST-FIR/ENG1-Zhu)fb5b91d62017-09-15 07:09:37 +0000298 FLEXCAN_QUIRK_BROKEN_PERR_STATE,
Hui Wang30c1e672012-06-28 16:21:35 +0800299};
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200300
ZHU Yi (ST-FIR/ENG1-Zhu)083c5572017-09-15 07:08:23 +0000301static const struct flexcan_devtype_data fsl_imx28_devtype_data = {
302 .quirks = FLEXCAN_QUIRK_BROKEN_PERR_STATE,
303};
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200304
Marc Kleine-Buddea3c11a72016-07-04 14:45:44 +0200305static const struct flexcan_devtype_data fsl_imx6q_devtype_data = {
Marc Kleine-Budde096de072015-09-01 10:28:46 +0200306 .quirks = FLEXCAN_QUIRK_DISABLE_RXFG | FLEXCAN_QUIRK_ENABLE_EACEN_RRS |
ZHU Yi (ST-FIR/ENG1-Zhu)cf9c0462017-09-15 07:05:50 +0000307 FLEXCAN_QUIRK_USE_OFF_TIMESTAMP | FLEXCAN_QUIRK_BROKEN_PERR_STATE,
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200308};
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200309
Marc Kleine-Buddea3c11a72016-07-04 14:45:44 +0200310static const struct flexcan_devtype_data fsl_vf610_devtype_data = {
Marc Kleine-Budde9eb7aa82015-09-01 08:57:55 +0200311 .quirks = FLEXCAN_QUIRK_DISABLE_RXFG | FLEXCAN_QUIRK_ENABLE_EACEN_RRS |
Marc Kleine-Budde29c64b12017-11-27 09:18:21 +0100312 FLEXCAN_QUIRK_DISABLE_MECR | FLEXCAN_QUIRK_USE_OFF_TIMESTAMP |
313 FLEXCAN_QUIRK_BROKEN_PERR_STATE,
Stefan Agnercdce8442014-07-15 14:56:21 +0200314};
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200315
Pankaj Bansal99b76682017-11-24 18:52:09 +0530316static const struct flexcan_devtype_data fsl_ls1021a_r2_devtype_data = {
317 .quirks = FLEXCAN_QUIRK_DISABLE_RXFG | FLEXCAN_QUIRK_ENABLE_EACEN_RRS |
318 FLEXCAN_QUIRK_DISABLE_MECR | FLEXCAN_QUIRK_BROKEN_PERR_STATE |
319 FLEXCAN_QUIRK_USE_OFF_TIMESTAMP,
320};
321
Marc Kleine-Budde194b9a42012-07-16 12:58:31 +0200322static const struct can_bittiming_const flexcan_bittiming_const = {
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200323 .name = DRV_NAME,
324 .tseg1_min = 4,
325 .tseg1_max = 16,
326 .tseg2_min = 2,
327 .tseg2_max = 8,
328 .sjw_max = 4,
329 .brp_min = 1,
330 .brp_max = 256,
331 .brp_inc = 1,
332};
333
Pankaj Bansal88462d22017-11-24 18:52:08 +0530334/* FlexCAN module is essentially modelled as a little-endian IP in most
335 * SoCs, i.e the registers as well as the message buffer areas are
336 * implemented in a little-endian fashion.
337 *
338 * However there are some SoCs (e.g. LS1021A) which implement the FlexCAN
339 * module in a big-endian fashion (i.e the registers as well as the
340 * message buffer areas are implemented in a big-endian way).
341 *
342 * In addition, the FlexCAN module can be found on SoCs having ARM or
343 * PPC cores. So, we need to abstract off the register read/write
344 * functions, ensuring that these cater to all the combinations of module
345 * endianness and underlying CPU endianness.
holt@sgi.com61e271e2011-08-16 17:32:20 +0000346 */
Pankaj Bansal88462d22017-11-24 18:52:08 +0530347static inline u32 flexcan_read_be(void __iomem *addr)
holt@sgi.com61e271e2011-08-16 17:32:20 +0000348{
Pankaj Bansal88462d22017-11-24 18:52:08 +0530349 return ioread32be(addr);
holt@sgi.com61e271e2011-08-16 17:32:20 +0000350}
351
Pankaj Bansal88462d22017-11-24 18:52:08 +0530352static inline void flexcan_write_be(u32 val, void __iomem *addr)
holt@sgi.com61e271e2011-08-16 17:32:20 +0000353{
Pankaj Bansal88462d22017-11-24 18:52:08 +0530354 iowrite32be(val, addr);
holt@sgi.com61e271e2011-08-16 17:32:20 +0000355}
356
Pankaj Bansal88462d22017-11-24 18:52:08 +0530357static inline u32 flexcan_read_le(void __iomem *addr)
holt@sgi.com61e271e2011-08-16 17:32:20 +0000358{
Pankaj Bansal88462d22017-11-24 18:52:08 +0530359 return ioread32(addr);
holt@sgi.com61e271e2011-08-16 17:32:20 +0000360}
Pankaj Bansal88462d22017-11-24 18:52:08 +0530361
362static inline void flexcan_write_le(u32 val, void __iomem *addr)
363{
364 iowrite32(val, addr);
365}
holt@sgi.com61e271e2011-08-16 17:32:20 +0000366
ZHU Yi (ST-FIR/ENG1-Zhu)da49a802017-09-15 07:03:58 +0000367static inline void flexcan_error_irq_enable(const struct flexcan_priv *priv)
368{
369 struct flexcan_regs __iomem *regs = priv->regs;
370 u32 reg_ctrl = (priv->reg_ctrl_default | FLEXCAN_CTRL_ERR_MSK);
371
Pankaj Bansal88462d22017-11-24 18:52:08 +0530372 priv->write(reg_ctrl, &regs->ctrl);
ZHU Yi (ST-FIR/ENG1-Zhu)da49a802017-09-15 07:03:58 +0000373}
374
375static inline void flexcan_error_irq_disable(const struct flexcan_priv *priv)
376{
377 struct flexcan_regs __iomem *regs = priv->regs;
378 u32 reg_ctrl = (priv->reg_ctrl_default & ~FLEXCAN_CTRL_ERR_MSK);
379
Pankaj Bansal88462d22017-11-24 18:52:08 +0530380 priv->write(reg_ctrl, &regs->ctrl);
ZHU Yi (ST-FIR/ENG1-Zhu)da49a802017-09-15 07:03:58 +0000381}
382
Marc Kleine-Buddef0036982014-02-28 17:18:27 +0100383static inline int flexcan_transceiver_enable(const struct flexcan_priv *priv)
384{
385 if (!priv->reg_xceiver)
386 return 0;
387
388 return regulator_enable(priv->reg_xceiver);
389}
390
391static inline int flexcan_transceiver_disable(const struct flexcan_priv *priv)
392{
393 if (!priv->reg_xceiver)
394 return 0;
395
396 return regulator_disable(priv->reg_xceiver);
397}
398
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100399static int flexcan_chip_enable(struct flexcan_priv *priv)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200400{
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200401 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100402 unsigned int timeout = FLEXCAN_TIMEOUT_US / 10;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200403 u32 reg;
404
Pankaj Bansal88462d22017-11-24 18:52:08 +0530405 reg = priv->read(&regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200406 reg &= ~FLEXCAN_MCR_MDIS;
Pankaj Bansal88462d22017-11-24 18:52:08 +0530407 priv->write(reg, &regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200408
Pankaj Bansal88462d22017-11-24 18:52:08 +0530409 while (timeout-- && (priv->read(&regs->mcr) & FLEXCAN_MCR_LPM_ACK))
David Jander8badd652014-08-27 12:02:16 +0200410 udelay(10);
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100411
Pankaj Bansal88462d22017-11-24 18:52:08 +0530412 if (priv->read(&regs->mcr) & FLEXCAN_MCR_LPM_ACK)
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100413 return -ETIMEDOUT;
414
415 return 0;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200416}
417
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100418static int flexcan_chip_disable(struct flexcan_priv *priv)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200419{
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200420 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100421 unsigned int timeout = FLEXCAN_TIMEOUT_US / 10;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200422 u32 reg;
423
Pankaj Bansal88462d22017-11-24 18:52:08 +0530424 reg = priv->read(&regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200425 reg |= FLEXCAN_MCR_MDIS;
Pankaj Bansal88462d22017-11-24 18:52:08 +0530426 priv->write(reg, &regs->mcr);
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100427
Pankaj Bansal88462d22017-11-24 18:52:08 +0530428 while (timeout-- && !(priv->read(&regs->mcr) & FLEXCAN_MCR_LPM_ACK))
David Jander8badd652014-08-27 12:02:16 +0200429 udelay(10);
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100430
Pankaj Bansal88462d22017-11-24 18:52:08 +0530431 if (!(priv->read(&regs->mcr) & FLEXCAN_MCR_LPM_ACK))
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100432 return -ETIMEDOUT;
433
434 return 0;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200435}
436
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100437static int flexcan_chip_freeze(struct flexcan_priv *priv)
438{
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200439 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100440 unsigned int timeout = 1000 * 1000 * 10 / priv->can.bittiming.bitrate;
441 u32 reg;
442
Pankaj Bansal88462d22017-11-24 18:52:08 +0530443 reg = priv->read(&regs->mcr);
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100444 reg |= FLEXCAN_MCR_HALT;
Pankaj Bansal88462d22017-11-24 18:52:08 +0530445 priv->write(reg, &regs->mcr);
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100446
Pankaj Bansal88462d22017-11-24 18:52:08 +0530447 while (timeout-- && !(priv->read(&regs->mcr) & FLEXCAN_MCR_FRZ_ACK))
David Jander8badd652014-08-27 12:02:16 +0200448 udelay(100);
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100449
Pankaj Bansal88462d22017-11-24 18:52:08 +0530450 if (!(priv->read(&regs->mcr) & FLEXCAN_MCR_FRZ_ACK))
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100451 return -ETIMEDOUT;
452
453 return 0;
454}
455
456static int flexcan_chip_unfreeze(struct flexcan_priv *priv)
457{
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200458 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100459 unsigned int timeout = FLEXCAN_TIMEOUT_US / 10;
460 u32 reg;
461
Pankaj Bansal88462d22017-11-24 18:52:08 +0530462 reg = priv->read(&regs->mcr);
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100463 reg &= ~FLEXCAN_MCR_HALT;
Pankaj Bansal88462d22017-11-24 18:52:08 +0530464 priv->write(reg, &regs->mcr);
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100465
Pankaj Bansal88462d22017-11-24 18:52:08 +0530466 while (timeout-- && (priv->read(&regs->mcr) & FLEXCAN_MCR_FRZ_ACK))
David Jander8badd652014-08-27 12:02:16 +0200467 udelay(10);
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100468
Pankaj Bansal88462d22017-11-24 18:52:08 +0530469 if (priv->read(&regs->mcr) & FLEXCAN_MCR_FRZ_ACK)
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100470 return -ETIMEDOUT;
471
472 return 0;
473}
474
Marc Kleine-Budde4b5b8222014-02-28 15:16:59 +0100475static int flexcan_chip_softreset(struct flexcan_priv *priv)
476{
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200477 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Budde4b5b8222014-02-28 15:16:59 +0100478 unsigned int timeout = FLEXCAN_TIMEOUT_US / 10;
479
Pankaj Bansal88462d22017-11-24 18:52:08 +0530480 priv->write(FLEXCAN_MCR_SOFTRST, &regs->mcr);
481 while (timeout-- && (priv->read(&regs->mcr) & FLEXCAN_MCR_SOFTRST))
David Jander8badd652014-08-27 12:02:16 +0200482 udelay(10);
Marc Kleine-Budde4b5b8222014-02-28 15:16:59 +0100483
Pankaj Bansal88462d22017-11-24 18:52:08 +0530484 if (priv->read(&regs->mcr) & FLEXCAN_MCR_SOFTRST)
Marc Kleine-Budde4b5b8222014-02-28 15:16:59 +0100485 return -ETIMEDOUT;
486
487 return 0;
488}
489
Stefan Agnerec56acf2014-07-15 14:56:20 +0200490static int __flexcan_get_berr_counter(const struct net_device *dev,
491 struct can_berr_counter *bec)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200492{
493 const struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200494 struct flexcan_regs __iomem *regs = priv->regs;
Pankaj Bansal88462d22017-11-24 18:52:08 +0530495 u32 reg = priv->read(&regs->ecr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200496
497 bec->txerr = (reg >> 0) & 0xff;
498 bec->rxerr = (reg >> 8) & 0xff;
499
500 return 0;
501}
502
Stefan Agnerec56acf2014-07-15 14:56:20 +0200503static int flexcan_get_berr_counter(const struct net_device *dev,
504 struct can_berr_counter *bec)
505{
506 const struct flexcan_priv *priv = netdev_priv(dev);
507 int err;
508
509 err = clk_prepare_enable(priv->clk_ipg);
510 if (err)
511 return err;
512
513 err = clk_prepare_enable(priv->clk_per);
514 if (err)
515 goto out_disable_ipg;
516
517 err = __flexcan_get_berr_counter(dev, bec);
518
519 clk_disable_unprepare(priv->clk_per);
520 out_disable_ipg:
521 clk_disable_unprepare(priv->clk_ipg);
522
523 return err;
524}
525
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200526static int flexcan_start_xmit(struct sk_buff *skb, struct net_device *dev)
527{
528 const struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200529 struct can_frame *cf = (struct can_frame *)skb->data;
530 u32 can_id;
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200531 u32 data;
Marc Kleine-Budde10d089b2014-09-23 11:18:11 +0200532 u32 ctrl = FLEXCAN_MB_CODE_TX_DATA | (cf->can_dlc << 16);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200533
534 if (can_dropped_invalid_skb(dev, skb))
535 return NETDEV_TX_OK;
536
537 netif_stop_queue(dev);
538
539 if (cf->can_id & CAN_EFF_FLAG) {
540 can_id = cf->can_id & CAN_EFF_MASK;
541 ctrl |= FLEXCAN_MB_CNT_IDE | FLEXCAN_MB_CNT_SRR;
542 } else {
543 can_id = (cf->can_id & CAN_SFF_MASK) << 18;
544 }
545
546 if (cf->can_id & CAN_RTR_FLAG)
547 ctrl |= FLEXCAN_MB_CNT_RTR;
548
549 if (cf->can_dlc > 0) {
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200550 data = be32_to_cpup((__be32 *)&cf->data[0]);
Pankaj Bansal88462d22017-11-24 18:52:08 +0530551 priv->write(data, &priv->tx_mb->data[0]);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200552 }
Luu An Phu13454c12018-01-02 10:44:18 +0700553 if (cf->can_dlc > 4) {
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200554 data = be32_to_cpup((__be32 *)&cf->data[4]);
Pankaj Bansal88462d22017-11-24 18:52:08 +0530555 priv->write(data, &priv->tx_mb->data[1]);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200556 }
557
Reuben Dowle9a123492011-11-01 11:18:03 +1300558 can_put_echo_skb(skb, dev, 0);
559
Pankaj Bansal88462d22017-11-24 18:52:08 +0530560 priv->write(can_id, &priv->tx_mb->can_id);
561 priv->write(ctrl, &priv->tx_mb->can_ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200562
David Jander25e92442014-09-03 16:47:22 +0200563 /* Errata ERR005829 step8:
564 * Write twice INACTIVE(0x8) code to first MB.
565 */
Pankaj Bansal88462d22017-11-24 18:52:08 +0530566 priv->write(FLEXCAN_MB_CODE_TX_INACTIVE,
Marc Kleine-Buddeb93917c2015-07-12 00:47:47 +0200567 &priv->tx_mb_reserved->can_ctrl);
Pankaj Bansal88462d22017-11-24 18:52:08 +0530568 priv->write(FLEXCAN_MB_CODE_TX_INACTIVE,
Marc Kleine-Buddeb93917c2015-07-12 00:47:47 +0200569 &priv->tx_mb_reserved->can_ctrl);
David Jander25e92442014-09-03 16:47:22 +0200570
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200571 return NETDEV_TX_OK;
572}
573
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200574static void flexcan_irq_bus_err(struct net_device *dev, u32 reg_esr)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200575{
576 struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Buddea5c02f662017-01-18 11:38:26 +0100577 struct sk_buff *skb;
578 struct can_frame *cf;
Marc Kleine-Budded166f562017-01-17 17:33:46 +0100579 bool rx_errors = false, tx_errors = false;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200580
Marc Kleine-Buddea5c02f662017-01-18 11:38:26 +0100581 skb = alloc_can_err_skb(dev, &cf);
582 if (unlikely(!skb))
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200583 return;
Marc Kleine-Buddea5c02f662017-01-18 11:38:26 +0100584
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200585 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
586
587 if (reg_esr & FLEXCAN_ESR_BIT1_ERR) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100588 netdev_dbg(dev, "BIT1_ERR irq\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200589 cf->data[2] |= CAN_ERR_PROT_BIT1;
Marc Kleine-Budded166f562017-01-17 17:33:46 +0100590 tx_errors = true;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200591 }
592 if (reg_esr & FLEXCAN_ESR_BIT0_ERR) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100593 netdev_dbg(dev, "BIT0_ERR irq\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200594 cf->data[2] |= CAN_ERR_PROT_BIT0;
Marc Kleine-Budded166f562017-01-17 17:33:46 +0100595 tx_errors = true;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200596 }
597 if (reg_esr & FLEXCAN_ESR_ACK_ERR) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100598 netdev_dbg(dev, "ACK_ERR irq\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200599 cf->can_id |= CAN_ERR_ACK;
Oliver Hartkoppffd461f2015-11-21 18:41:20 +0100600 cf->data[3] = CAN_ERR_PROT_LOC_ACK;
Marc Kleine-Budded166f562017-01-17 17:33:46 +0100601 tx_errors = true;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200602 }
603 if (reg_esr & FLEXCAN_ESR_CRC_ERR) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100604 netdev_dbg(dev, "CRC_ERR irq\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200605 cf->data[2] |= CAN_ERR_PROT_BIT;
Oliver Hartkoppffd461f2015-11-21 18:41:20 +0100606 cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ;
Marc Kleine-Budded166f562017-01-17 17:33:46 +0100607 rx_errors = true;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200608 }
609 if (reg_esr & FLEXCAN_ESR_FRM_ERR) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100610 netdev_dbg(dev, "FRM_ERR irq\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200611 cf->data[2] |= CAN_ERR_PROT_FORM;
Marc Kleine-Budded166f562017-01-17 17:33:46 +0100612 rx_errors = true;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200613 }
614 if (reg_esr & FLEXCAN_ESR_STF_ERR) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100615 netdev_dbg(dev, "STF_ERR irq\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200616 cf->data[2] |= CAN_ERR_PROT_STUFF;
Marc Kleine-Budded166f562017-01-17 17:33:46 +0100617 rx_errors = true;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200618 }
619
620 priv->can.can_stats.bus_error++;
621 if (rx_errors)
622 dev->stats.rx_errors++;
623 if (tx_errors)
624 dev->stats.tx_errors++;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200625
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200626 can_rx_offload_irq_queue_err_skb(&priv->offload, skb);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200627}
628
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200629static void flexcan_irq_state(struct net_device *dev, u32 reg_esr)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200630{
631 struct flexcan_priv *priv = netdev_priv(dev);
632 struct sk_buff *skb;
633 struct can_frame *cf;
Marc Kleine-Budde238443d2017-01-18 11:25:41 +0100634 enum can_state new_state, rx_state, tx_state;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200635 int flt;
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000636 struct can_berr_counter bec;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200637
638 flt = reg_esr & FLEXCAN_ESR_FLT_CONF_MASK;
639 if (likely(flt == FLEXCAN_ESR_FLT_CONF_ACTIVE)) {
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000640 tx_state = unlikely(reg_esr & FLEXCAN_ESR_TX_WRN) ?
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200641 CAN_STATE_ERROR_WARNING : CAN_STATE_ERROR_ACTIVE;
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000642 rx_state = unlikely(reg_esr & FLEXCAN_ESR_RX_WRN) ?
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200643 CAN_STATE_ERROR_WARNING : CAN_STATE_ERROR_ACTIVE;
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000644 new_state = max(tx_state, rx_state);
Andri Yngvason258ce802015-03-17 13:03:09 +0000645 } else {
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000646 __flexcan_get_berr_counter(dev, &bec);
Andri Yngvason258ce802015-03-17 13:03:09 +0000647 new_state = flt == FLEXCAN_ESR_FLT_CONF_PASSIVE ?
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200648 CAN_STATE_ERROR_PASSIVE : CAN_STATE_BUS_OFF;
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000649 rx_state = bec.rxerr >= bec.txerr ? new_state : 0;
650 tx_state = bec.rxerr <= bec.txerr ? new_state : 0;
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000651 }
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200652
653 /* state hasn't changed */
654 if (likely(new_state == priv->can.state))
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200655 return;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200656
657 skb = alloc_can_err_skb(dev, &cf);
658 if (unlikely(!skb))
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200659 return;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200660
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000661 can_change_state(dev, cf, tx_state, rx_state);
662
663 if (unlikely(new_state == CAN_STATE_BUS_OFF))
664 can_bus_off(dev);
665
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200666 can_rx_offload_irq_queue_err_skb(&priv->offload, skb);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200667}
668
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200669static inline struct flexcan_priv *rx_offload_to_priv(struct can_rx_offload *offload)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200670{
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200671 return container_of(offload, struct flexcan_priv, offload);
672}
673
674static unsigned int flexcan_mailbox_read(struct can_rx_offload *offload,
675 struct can_frame *cf,
676 u32 *timestamp, unsigned int n)
677{
678 struct flexcan_priv *priv = rx_offload_to_priv(offload);
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200679 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200680 struct flexcan_mb __iomem *mb = &regs->mb[n];
681 u32 reg_ctrl, reg_id, reg_iflag1;
682
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200683 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP) {
684 u32 code;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200685
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200686 do {
Pankaj Bansal88462d22017-11-24 18:52:08 +0530687 reg_ctrl = priv->read(&mb->can_ctrl);
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200688 } while (reg_ctrl & FLEXCAN_MB_CODE_RX_BUSY_BIT);
689
690 /* is this MB empty? */
691 code = reg_ctrl & FLEXCAN_MB_CODE_MASK;
692 if ((code != FLEXCAN_MB_CODE_RX_FULL) &&
693 (code != FLEXCAN_MB_CODE_RX_OVERRUN))
694 return 0;
695
696 if (code == FLEXCAN_MB_CODE_RX_OVERRUN) {
697 /* This MB was overrun, we lost data */
698 offload->dev->stats.rx_over_errors++;
699 offload->dev->stats.rx_errors++;
700 }
701 } else {
Pankaj Bansal88462d22017-11-24 18:52:08 +0530702 reg_iflag1 = priv->read(&regs->iflag1);
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200703 if (!(reg_iflag1 & FLEXCAN_IFLAG_RX_FIFO_AVAILABLE))
704 return 0;
705
Pankaj Bansal88462d22017-11-24 18:52:08 +0530706 reg_ctrl = priv->read(&mb->can_ctrl);
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200707 }
708
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200709 /* increase timstamp to full 32 bit */
710 *timestamp = reg_ctrl << 16;
711
Pankaj Bansal88462d22017-11-24 18:52:08 +0530712 reg_id = priv->read(&mb->can_id);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200713 if (reg_ctrl & FLEXCAN_MB_CNT_IDE)
714 cf->can_id = ((reg_id >> 0) & CAN_EFF_MASK) | CAN_EFF_FLAG;
715 else
716 cf->can_id = (reg_id >> 18) & CAN_SFF_MASK;
717
718 if (reg_ctrl & FLEXCAN_MB_CNT_RTR)
719 cf->can_id |= CAN_RTR_FLAG;
720 cf->can_dlc = get_can_dlc((reg_ctrl >> 16) & 0xf);
721
Pankaj Bansal88462d22017-11-24 18:52:08 +0530722 *(__be32 *)(cf->data + 0) = cpu_to_be32(priv->read(&mb->data[0]));
723 *(__be32 *)(cf->data + 4) = cpu_to_be32(priv->read(&mb->data[1]));
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200724
725 /* mark as read */
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200726 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP) {
727 /* Clear IRQ */
728 if (n < 32)
Pankaj Bansal88462d22017-11-24 18:52:08 +0530729 priv->write(BIT(n), &regs->iflag1);
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200730 else
Pankaj Bansal88462d22017-11-24 18:52:08 +0530731 priv->write(BIT(n - 32), &regs->iflag2);
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200732 } else {
Pankaj Bansal88462d22017-11-24 18:52:08 +0530733 priv->write(FLEXCAN_IFLAG_RX_FIFO_AVAILABLE, &regs->iflag1);
734 priv->read(&regs->timer);
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200735 }
Fabio Baltieriadccadb2012-12-18 18:50:58 +0100736
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200737 return 1;
738}
739
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200740
741static inline u64 flexcan_read_reg_iflag_rx(struct flexcan_priv *priv)
742{
743 struct flexcan_regs __iomem *regs = priv->regs;
744 u32 iflag1, iflag2;
745
Pankaj Bansal88462d22017-11-24 18:52:08 +0530746 iflag2 = priv->read(&regs->iflag2) & priv->reg_imask2_default;
747 iflag1 = priv->read(&regs->iflag1) & priv->reg_imask1_default &
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200748 ~FLEXCAN_IFLAG_MB(priv->tx_mb_idx);
749
750 return (u64)iflag2 << 32 | iflag1;
751}
752
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200753static irqreturn_t flexcan_irq(int irq, void *dev_id)
754{
755 struct net_device *dev = dev_id;
756 struct net_device_stats *stats = &dev->stats;
757 struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200758 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddedd2f1222017-01-18 11:45:14 +0100759 irqreturn_t handled = IRQ_NONE;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200760 u32 reg_iflag1, reg_esr;
ZHU Yi (ST-FIR/ENG1-Zhu)da49a802017-09-15 07:03:58 +0000761 enum can_state last_state = priv->can.state;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200762
Pankaj Bansal88462d22017-11-24 18:52:08 +0530763 reg_iflag1 = priv->read(&regs->iflag1);
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200764
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200765 /* reception interrupt */
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200766 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP) {
767 u64 reg_iflag;
768 int ret;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200769
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200770 while ((reg_iflag = flexcan_read_reg_iflag_rx(priv))) {
771 handled = IRQ_HANDLED;
772 ret = can_rx_offload_irq_offload_timestamp(&priv->offload,
773 reg_iflag);
774 if (!ret)
775 break;
776 }
777 } else {
778 if (reg_iflag1 & FLEXCAN_IFLAG_RX_FIFO_AVAILABLE) {
779 handled = IRQ_HANDLED;
780 can_rx_offload_irq_offload_fifo(&priv->offload);
781 }
782
783 /* FIFO overflow interrupt */
784 if (reg_iflag1 & FLEXCAN_IFLAG_RX_FIFO_OVERFLOW) {
785 handled = IRQ_HANDLED;
Pankaj Bansal88462d22017-11-24 18:52:08 +0530786 priv->write(FLEXCAN_IFLAG_RX_FIFO_OVERFLOW,
787 &regs->iflag1);
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200788 dev->stats.rx_over_errors++;
789 dev->stats.rx_errors++;
790 }
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200791 }
792
793 /* transmission complete interrupt */
Marc Kleine-Buddeb93917c2015-07-12 00:47:47 +0200794 if (reg_iflag1 & FLEXCAN_IFLAG_MB(priv->tx_mb_idx)) {
Marc Kleine-Buddedd2f1222017-01-18 11:45:14 +0100795 handled = IRQ_HANDLED;
Reuben Dowle9a123492011-11-01 11:18:03 +1300796 stats->tx_bytes += can_get_echo_skb(dev, 0);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200797 stats->tx_packets++;
Fabio Baltieriadccadb2012-12-18 18:50:58 +0100798 can_led_event(dev, CAN_LED_EVENT_TX);
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200799
800 /* after sending a RTR frame MB is in RX mode */
Pankaj Bansal88462d22017-11-24 18:52:08 +0530801 priv->write(FLEXCAN_MB_CODE_TX_INACTIVE,
802 &priv->tx_mb->can_ctrl);
803 priv->write(FLEXCAN_IFLAG_MB(priv->tx_mb_idx), &regs->iflag1);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200804 netif_wake_queue(dev);
805 }
806
Pankaj Bansal88462d22017-11-24 18:52:08 +0530807 reg_esr = priv->read(&regs->esr);
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200808
Marc Kleine-Buddedd2f1222017-01-18 11:45:14 +0100809 /* ACK all bus error and state change IRQ sources */
810 if (reg_esr & FLEXCAN_ESR_ALL_INT) {
811 handled = IRQ_HANDLED;
Pankaj Bansal88462d22017-11-24 18:52:08 +0530812 priv->write(reg_esr & FLEXCAN_ESR_ALL_INT, &regs->esr);
Marc Kleine-Buddedd2f1222017-01-18 11:45:14 +0100813 }
814
ZHU Yi (ST-FIR/ENG1-Zhu)ad230232017-09-15 06:59:15 +0000815 /* state change interrupt or broken error state quirk fix is enabled */
816 if ((reg_esr & FLEXCAN_ESR_ERR_STATE) ||
ZHU Yi (ST-FIR/ENG1-Zhu)da49a802017-09-15 07:03:58 +0000817 (priv->devtype_data->quirks & (FLEXCAN_QUIRK_BROKEN_WERR_STATE |
818 FLEXCAN_QUIRK_BROKEN_PERR_STATE)))
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200819 flexcan_irq_state(dev, reg_esr);
820
821 /* bus error IRQ - handle if bus error reporting is activated */
822 if ((reg_esr & FLEXCAN_ESR_ERR_BUS) &&
823 (priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING))
824 flexcan_irq_bus_err(dev, reg_esr);
825
ZHU Yi (ST-FIR/ENG1-Zhu)da49a802017-09-15 07:03:58 +0000826 /* availability of error interrupt among state transitions in case
827 * bus error reporting is de-activated and
828 * FLEXCAN_QUIRK_BROKEN_PERR_STATE is enabled:
829 * +--------------------------------------------------------------+
830 * | +----------------------------------------------+ [stopped / |
831 * | | | sleeping] -+
832 * +-+-> active <-> warning <-> passive -> bus off -+
833 * ___________^^^^^^^^^^^^_______________________________
834 * disabled(1) enabled disabled
835 *
836 * (1): enabled if FLEXCAN_QUIRK_BROKEN_WERR_STATE is enabled
837 */
838 if ((last_state != priv->can.state) &&
839 (priv->devtype_data->quirks & FLEXCAN_QUIRK_BROKEN_PERR_STATE) &&
840 !(priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING)) {
841 switch (priv->can.state) {
842 case CAN_STATE_ERROR_ACTIVE:
843 if (priv->devtype_data->quirks &
844 FLEXCAN_QUIRK_BROKEN_WERR_STATE)
845 flexcan_error_irq_enable(priv);
846 else
847 flexcan_error_irq_disable(priv);
848 break;
849
850 case CAN_STATE_ERROR_WARNING:
851 flexcan_error_irq_enable(priv);
852 break;
853
854 case CAN_STATE_ERROR_PASSIVE:
855 case CAN_STATE_BUS_OFF:
856 flexcan_error_irq_disable(priv);
857 break;
858
859 default:
860 break;
861 }
862 }
863
Marc Kleine-Buddedd2f1222017-01-18 11:45:14 +0100864 return handled;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200865}
866
867static void flexcan_set_bittiming(struct net_device *dev)
868{
869 const struct flexcan_priv *priv = netdev_priv(dev);
870 const struct can_bittiming *bt = &priv->can.bittiming;
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200871 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200872 u32 reg;
873
Pankaj Bansal88462d22017-11-24 18:52:08 +0530874 reg = priv->read(&regs->ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200875 reg &= ~(FLEXCAN_CTRL_PRESDIV(0xff) |
876 FLEXCAN_CTRL_RJW(0x3) |
877 FLEXCAN_CTRL_PSEG1(0x7) |
878 FLEXCAN_CTRL_PSEG2(0x7) |
879 FLEXCAN_CTRL_PROPSEG(0x7) |
880 FLEXCAN_CTRL_LPB |
881 FLEXCAN_CTRL_SMP |
882 FLEXCAN_CTRL_LOM);
883
884 reg |= FLEXCAN_CTRL_PRESDIV(bt->brp - 1) |
885 FLEXCAN_CTRL_PSEG1(bt->phase_seg1 - 1) |
886 FLEXCAN_CTRL_PSEG2(bt->phase_seg2 - 1) |
887 FLEXCAN_CTRL_RJW(bt->sjw - 1) |
888 FLEXCAN_CTRL_PROPSEG(bt->prop_seg - 1);
889
890 if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)
891 reg |= FLEXCAN_CTRL_LPB;
892 if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
893 reg |= FLEXCAN_CTRL_LOM;
894 if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
895 reg |= FLEXCAN_CTRL_SMP;
896
Lucas Stach7a4b6c82015-08-07 17:16:03 +0200897 netdev_dbg(dev, "writing ctrl=0x%08x\n", reg);
Pankaj Bansal88462d22017-11-24 18:52:08 +0530898 priv->write(reg, &regs->ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200899
900 /* print chip status */
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100901 netdev_dbg(dev, "%s: mcr=0x%08x ctrl=0x%08x\n", __func__,
Pankaj Bansal88462d22017-11-24 18:52:08 +0530902 priv->read(&regs->mcr), priv->read(&regs->ctrl));
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200903}
904
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200905/* flexcan_chip_start
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200906 *
907 * this functions is entered with clocks enabled
908 *
909 */
910static int flexcan_chip_start(struct net_device *dev)
911{
912 struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200913 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Budde6f75fce2014-09-23 11:03:01 +0200914 u32 reg_mcr, reg_ctrl, reg_ctrl2, reg_mecr;
David S. Miller1f6d8032014-09-23 12:09:27 -0400915 int err, i;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200916
917 /* enable module */
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100918 err = flexcan_chip_enable(priv);
919 if (err)
920 return err;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200921
922 /* soft reset */
Marc Kleine-Budde4b5b8222014-02-28 15:16:59 +0100923 err = flexcan_chip_softreset(priv);
924 if (err)
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100925 goto out_chip_disable;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200926
927 flexcan_set_bittiming(dev);
928
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200929 /* MCR
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200930 *
931 * enable freeze
932 * enable fifo
933 * halt now
934 * only supervisor access
935 * enable warning int
Reuben Dowle9a123492011-11-01 11:18:03 +1300936 * disable local echo
Marc Kleine-Budde4bd888a2015-08-31 21:03:29 +0200937 * enable individual RX masking
Marc Kleine-Budde749de6f2015-08-31 21:32:34 +0200938 * choose format C
939 * set max mailbox number
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200940 */
Pankaj Bansal88462d22017-11-24 18:52:08 +0530941 reg_mcr = priv->read(&regs->mcr);
Marc Kleine-Budded5a7b402013-10-04 10:52:36 +0200942 reg_mcr &= ~FLEXCAN_MCR_MAXMB(0xff);
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200943 reg_mcr |= FLEXCAN_MCR_FRZ | FLEXCAN_MCR_HALT | FLEXCAN_MCR_SUPV |
944 FLEXCAN_MCR_WRN_EN | FLEXCAN_MCR_SRX_DIS | FLEXCAN_MCR_IRMQ |
945 FLEXCAN_MCR_IDAM_C;
946
947 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP) {
948 reg_mcr &= ~FLEXCAN_MCR_FEN;
949 reg_mcr |= FLEXCAN_MCR_MAXMB(priv->offload.mb_last);
950 } else {
951 reg_mcr |= FLEXCAN_MCR_FEN |
952 FLEXCAN_MCR_MAXMB(priv->tx_mb_idx);
953 }
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100954 netdev_dbg(dev, "%s: writing mcr=0x%08x", __func__, reg_mcr);
Pankaj Bansal88462d22017-11-24 18:52:08 +0530955 priv->write(reg_mcr, &regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200956
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200957 /* CTRL
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200958 *
959 * disable timer sync feature
960 *
961 * disable auto busoff recovery
962 * transmit lowest buffer first
963 *
964 * enable tx and rx warning interrupt
965 * enable bus off interrupt
966 * (== FLEXCAN_CTRL_ERR_STATE)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200967 */
Pankaj Bansal88462d22017-11-24 18:52:08 +0530968 reg_ctrl = priv->read(&regs->ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200969 reg_ctrl &= ~FLEXCAN_CTRL_TSYN;
970 reg_ctrl |= FLEXCAN_CTRL_BOFF_REC | FLEXCAN_CTRL_LBUF |
Wolfgang Grandegger4f72e5f2012-09-28 03:17:15 +0000971 FLEXCAN_CTRL_ERR_STATE;
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200972
973 /* enable the "error interrupt" (FLEXCAN_CTRL_ERR_MSK),
Wolfgang Grandegger4f72e5f2012-09-28 03:17:15 +0000974 * on most Flexcan cores, too. Otherwise we don't get
975 * any error warning or passive interrupts.
976 */
ZHU Yi (ST-FIR/ENG1-Zhu)2f8639b2017-09-15 07:01:23 +0000977 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_BROKEN_WERR_STATE ||
Wolfgang Grandegger4f72e5f2012-09-28 03:17:15 +0000978 priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING)
979 reg_ctrl |= FLEXCAN_CTRL_ERR_MSK;
Alexander Steinbc03a542014-08-12 10:47:21 +0200980 else
981 reg_ctrl &= ~FLEXCAN_CTRL_ERR_MSK;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200982
983 /* save for later use */
984 priv->reg_ctrl_default = reg_ctrl;
Marc Kleine-Budde6fa7da22015-08-27 14:24:48 +0200985 /* leave interrupts disabled for now */
986 reg_ctrl &= ~FLEXCAN_CTRL_ERR_ALL;
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100987 netdev_dbg(dev, "%s: writing ctrl=0x%08x", __func__, reg_ctrl);
Pankaj Bansal88462d22017-11-24 18:52:08 +0530988 priv->write(reg_ctrl, &regs->ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200989
Marc Kleine-Budde9eb7aa82015-09-01 08:57:55 +0200990 if ((priv->devtype_data->quirks & FLEXCAN_QUIRK_ENABLE_EACEN_RRS)) {
Pankaj Bansal88462d22017-11-24 18:52:08 +0530991 reg_ctrl2 = priv->read(&regs->ctrl2);
Marc Kleine-Budde9eb7aa82015-09-01 08:57:55 +0200992 reg_ctrl2 |= FLEXCAN_CTRL2_EACEN | FLEXCAN_CTRL2_RRS;
Pankaj Bansal88462d22017-11-24 18:52:08 +0530993 priv->write(reg_ctrl2, &regs->ctrl2);
Marc Kleine-Budde9eb7aa82015-09-01 08:57:55 +0200994 }
995
David Janderfc05b882014-08-27 11:58:05 +0200996 /* clear and invalidate all mailboxes first */
Marc Kleine-Buddeb93917c2015-07-12 00:47:47 +0200997 for (i = priv->tx_mb_idx; i < ARRAY_SIZE(regs->mb); i++) {
Pankaj Bansal88462d22017-11-24 18:52:08 +0530998 priv->write(FLEXCAN_MB_CODE_RX_INACTIVE,
999 &regs->mb[i].can_ctrl);
David Janderfc05b882014-08-27 11:58:05 +02001000 }
1001
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +02001002 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP) {
1003 for (i = priv->offload.mb_first; i <= priv->offload.mb_last; i++)
Pankaj Bansal88462d22017-11-24 18:52:08 +05301004 priv->write(FLEXCAN_MB_CODE_RX_EMPTY,
1005 &regs->mb[i].can_ctrl);
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +02001006 }
1007
David Jander25e92442014-09-03 16:47:22 +02001008 /* Errata ERR005829: mark first TX mailbox as INACTIVE */
Pankaj Bansal88462d22017-11-24 18:52:08 +05301009 priv->write(FLEXCAN_MB_CODE_TX_INACTIVE,
1010 &priv->tx_mb_reserved->can_ctrl);
David Jander25e92442014-09-03 16:47:22 +02001011
Marc Kleine-Buddec32fe4a2014-09-16 12:39:28 +02001012 /* mark TX mailbox as INACTIVE */
Pankaj Bansal88462d22017-11-24 18:52:08 +05301013 priv->write(FLEXCAN_MB_CODE_TX_INACTIVE,
1014 &priv->tx_mb->can_ctrl);
Marc Kleine-Budded5a7b402013-10-04 10:52:36 +02001015
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001016 /* acceptance mask/acceptance code (accept everything) */
Pankaj Bansal88462d22017-11-24 18:52:08 +05301017 priv->write(0x0, &regs->rxgmask);
1018 priv->write(0x0, &regs->rx14mask);
1019 priv->write(0x0, &regs->rx15mask);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001020
Marc Kleine-Buddef377bff2015-05-08 15:22:36 +02001021 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_DISABLE_RXFG)
Pankaj Bansal88462d22017-11-24 18:52:08 +05301022 priv->write(0x0, &regs->rxfgmask);
Hui Wang30c1e672012-06-28 16:21:35 +08001023
Marc Kleine-Budde4bd888a2015-08-31 21:03:29 +02001024 /* clear acceptance filters */
1025 for (i = 0; i < ARRAY_SIZE(regs->mb); i++)
Pankaj Bansal88462d22017-11-24 18:52:08 +05301026 priv->write(0, &regs->rximr[i]);
Marc Kleine-Budde4bd888a2015-08-31 21:03:29 +02001027
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +02001028 /* On Vybrid, disable memory error detection interrupts
Stefan Agnercdce8442014-07-15 14:56:21 +02001029 * and freeze mode.
1030 * This also works around errata e5295 which generates
1031 * false positive memory errors and put the device in
1032 * freeze mode.
1033 */
Marc Kleine-Buddef377bff2015-05-08 15:22:36 +02001034 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_DISABLE_MECR) {
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +02001035 /* Follow the protocol as described in "Detection
Stefan Agnercdce8442014-07-15 14:56:21 +02001036 * and Correction of Memory Errors" to write to
1037 * MECR register
1038 */
Pankaj Bansal88462d22017-11-24 18:52:08 +05301039 reg_ctrl2 = priv->read(&regs->ctrl2);
Marc Kleine-Budde6f75fce2014-09-23 11:03:01 +02001040 reg_ctrl2 |= FLEXCAN_CTRL2_ECRWRE;
Pankaj Bansal88462d22017-11-24 18:52:08 +05301041 priv->write(reg_ctrl2, &regs->ctrl2);
Stefan Agnercdce8442014-07-15 14:56:21 +02001042
Pankaj Bansal88462d22017-11-24 18:52:08 +05301043 reg_mecr = priv->read(&regs->mecr);
Stefan Agnercdce8442014-07-15 14:56:21 +02001044 reg_mecr &= ~FLEXCAN_MECR_ECRWRDIS;
Pankaj Bansal88462d22017-11-24 18:52:08 +05301045 priv->write(reg_mecr, &regs->mecr);
Stefan Agnercdce8442014-07-15 14:56:21 +02001046 reg_mecr &= ~(FLEXCAN_MECR_NCEFAFRZ | FLEXCAN_MECR_HANCEI_MSK |
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +02001047 FLEXCAN_MECR_FANCEI_MSK);
Pankaj Bansal88462d22017-11-24 18:52:08 +05301048 priv->write(reg_mecr, &regs->mecr);
Stefan Agnercdce8442014-07-15 14:56:21 +02001049 }
1050
Marc Kleine-Buddef0036982014-02-28 17:18:27 +01001051 err = flexcan_transceiver_enable(priv);
1052 if (err)
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +01001053 goto out_chip_disable;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001054
1055 /* synchronize with the can bus */
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +01001056 err = flexcan_chip_unfreeze(priv);
1057 if (err)
1058 goto out_transceiver_disable;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001059
1060 priv->can.state = CAN_STATE_ERROR_ACTIVE;
1061
Marc Kleine-Budde6fa7da22015-08-27 14:24:48 +02001062 /* enable interrupts atomically */
1063 disable_irq(dev->irq);
Pankaj Bansal88462d22017-11-24 18:52:08 +05301064 priv->write(priv->reg_ctrl_default, &regs->ctrl);
1065 priv->write(priv->reg_imask1_default, &regs->imask1);
1066 priv->write(priv->reg_imask2_default, &regs->imask2);
Marc Kleine-Budde6fa7da22015-08-27 14:24:48 +02001067 enable_irq(dev->irq);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001068
1069 /* print chip status */
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +01001070 netdev_dbg(dev, "%s: reading mcr=0x%08x ctrl=0x%08x\n", __func__,
Pankaj Bansal88462d22017-11-24 18:52:08 +05301071 priv->read(&regs->mcr), priv->read(&regs->ctrl));
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001072
1073 return 0;
1074
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +01001075 out_transceiver_disable:
1076 flexcan_transceiver_disable(priv);
1077 out_chip_disable:
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001078 flexcan_chip_disable(priv);
1079 return err;
1080}
1081
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +02001082/* flexcan_chip_stop
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001083 *
1084 * this functions is entered with clocks enabled
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001085 */
1086static void flexcan_chip_stop(struct net_device *dev)
1087{
1088 struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Budde89af8742015-05-08 09:32:58 +02001089 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001090
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +01001091 /* freeze + disable module */
1092 flexcan_chip_freeze(priv);
1093 flexcan_chip_disable(priv);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001094
Marc Kleine-Budde5be93bd2014-02-19 12:00:51 +01001095 /* Disable all interrupts */
Pankaj Bansal88462d22017-11-24 18:52:08 +05301096 priv->write(0, &regs->imask2);
1097 priv->write(0, &regs->imask1);
1098 priv->write(priv->reg_ctrl_default & ~FLEXCAN_CTRL_ERR_ALL,
1099 &regs->ctrl);
Marc Kleine-Budde5be93bd2014-02-19 12:00:51 +01001100
Marc Kleine-Buddef0036982014-02-28 17:18:27 +01001101 flexcan_transceiver_disable(priv);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001102 priv->can.state = CAN_STATE_STOPPED;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001103}
1104
1105static int flexcan_open(struct net_device *dev)
1106{
1107 struct flexcan_priv *priv = netdev_priv(dev);
1108 int err;
1109
Fabio Estevamaa101812013-07-22 12:41:40 -03001110 err = clk_prepare_enable(priv->clk_ipg);
1111 if (err)
1112 return err;
1113
1114 err = clk_prepare_enable(priv->clk_per);
1115 if (err)
1116 goto out_disable_ipg;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001117
1118 err = open_candev(dev);
1119 if (err)
Fabio Estevamaa101812013-07-22 12:41:40 -03001120 goto out_disable_per;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001121
1122 err = request_irq(dev->irq, flexcan_irq, IRQF_SHARED, dev->name, dev);
1123 if (err)
1124 goto out_close;
1125
1126 /* start chip and queuing */
1127 err = flexcan_chip_start(dev);
1128 if (err)
Marc Kleine-Budde7e9e1482014-02-28 14:52:01 +01001129 goto out_free_irq;
Fabio Baltieriadccadb2012-12-18 18:50:58 +01001130
1131 can_led_event(dev, CAN_LED_EVENT_OPEN);
1132
Marc Kleine-Budde30164752015-05-10 15:26:58 +02001133 can_rx_offload_enable(&priv->offload);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001134 netif_start_queue(dev);
1135
1136 return 0;
1137
Marc Kleine-Budde7e9e1482014-02-28 14:52:01 +01001138 out_free_irq:
1139 free_irq(dev->irq, dev);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001140 out_close:
1141 close_candev(dev);
Fabio Estevamaa101812013-07-22 12:41:40 -03001142 out_disable_per:
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001143 clk_disable_unprepare(priv->clk_per);
Fabio Estevamaa101812013-07-22 12:41:40 -03001144 out_disable_ipg:
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001145 clk_disable_unprepare(priv->clk_ipg);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001146
1147 return err;
1148}
1149
1150static int flexcan_close(struct net_device *dev)
1151{
1152 struct flexcan_priv *priv = netdev_priv(dev);
1153
1154 netif_stop_queue(dev);
Marc Kleine-Budde30164752015-05-10 15:26:58 +02001155 can_rx_offload_disable(&priv->offload);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001156 flexcan_chip_stop(dev);
1157
1158 free_irq(dev->irq, dev);
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001159 clk_disable_unprepare(priv->clk_per);
1160 clk_disable_unprepare(priv->clk_ipg);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001161
1162 close_candev(dev);
1163
Fabio Baltieriadccadb2012-12-18 18:50:58 +01001164 can_led_event(dev, CAN_LED_EVENT_STOP);
1165
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001166 return 0;
1167}
1168
1169static int flexcan_set_mode(struct net_device *dev, enum can_mode mode)
1170{
1171 int err;
1172
1173 switch (mode) {
1174 case CAN_MODE_START:
1175 err = flexcan_chip_start(dev);
1176 if (err)
1177 return err;
1178
1179 netif_wake_queue(dev);
1180 break;
1181
1182 default:
1183 return -EOPNOTSUPP;
1184 }
1185
1186 return 0;
1187}
1188
1189static const struct net_device_ops flexcan_netdev_ops = {
1190 .ndo_open = flexcan_open,
1191 .ndo_stop = flexcan_close,
1192 .ndo_start_xmit = flexcan_start_xmit,
Oliver Hartkoppc971fa22014-03-07 09:23:41 +01001193 .ndo_change_mtu = can_change_mtu,
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001194};
1195
Bill Pemberton3c8ac0f2012-12-03 09:22:44 -05001196static int register_flexcandev(struct net_device *dev)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001197{
1198 struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Budde89af8742015-05-08 09:32:58 +02001199 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001200 u32 reg, err;
1201
Fabio Estevamaa101812013-07-22 12:41:40 -03001202 err = clk_prepare_enable(priv->clk_ipg);
1203 if (err)
1204 return err;
1205
1206 err = clk_prepare_enable(priv->clk_per);
1207 if (err)
1208 goto out_disable_ipg;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001209
1210 /* select "bus clock", chip must be disabled */
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001211 err = flexcan_chip_disable(priv);
1212 if (err)
1213 goto out_disable_per;
Pankaj Bansal88462d22017-11-24 18:52:08 +05301214 reg = priv->read(&regs->ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001215 reg |= FLEXCAN_CTRL_CLK_SRC;
Pankaj Bansal88462d22017-11-24 18:52:08 +05301216 priv->write(reg, &regs->ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001217
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001218 err = flexcan_chip_enable(priv);
1219 if (err)
1220 goto out_chip_disable;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001221
1222 /* set freeze, halt and activate FIFO, restrict register access */
Pankaj Bansal88462d22017-11-24 18:52:08 +05301223 reg = priv->read(&regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001224 reg |= FLEXCAN_MCR_FRZ | FLEXCAN_MCR_HALT |
1225 FLEXCAN_MCR_FEN | FLEXCAN_MCR_SUPV;
Pankaj Bansal88462d22017-11-24 18:52:08 +05301226 priv->write(reg, &regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001227
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +02001228 /* Currently we only support newer versions of this core
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +02001229 * featuring a RX hardware FIFO (although this driver doesn't
1230 * make use of it on some cores). Older cores, found on some
1231 * Coldfire derivates are not tested.
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001232 */
Pankaj Bansal88462d22017-11-24 18:52:08 +05301233 reg = priv->read(&regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001234 if (!(reg & FLEXCAN_MCR_FEN)) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +01001235 netdev_err(dev, "Could not enable RX FIFO, unsupported core\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001236 err = -ENODEV;
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001237 goto out_chip_disable;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001238 }
1239
1240 err = register_candev(dev);
1241
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001242 /* disable core and turn off clocks */
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001243 out_chip_disable:
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001244 flexcan_chip_disable(priv);
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001245 out_disable_per:
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001246 clk_disable_unprepare(priv->clk_per);
Fabio Estevamaa101812013-07-22 12:41:40 -03001247 out_disable_ipg:
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001248 clk_disable_unprepare(priv->clk_ipg);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001249
1250 return err;
1251}
1252
Bill Pemberton3c8ac0f2012-12-03 09:22:44 -05001253static void unregister_flexcandev(struct net_device *dev)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001254{
1255 unregister_candev(dev);
1256}
1257
Hui Wang30c1e672012-06-28 16:21:35 +08001258static const struct of_device_id flexcan_of_match[] = {
Hui Wang30c1e672012-06-28 16:21:35 +08001259 { .compatible = "fsl,imx6q-flexcan", .data = &fsl_imx6q_devtype_data, },
Marc Kleine-Buddee3587842013-10-03 23:51:55 +02001260 { .compatible = "fsl,imx28-flexcan", .data = &fsl_imx28_devtype_data, },
Uwe Kleine-König0e030a32018-04-25 16:50:39 +02001261 { .compatible = "fsl,imx53-flexcan", .data = &fsl_imx25_devtype_data, },
1262 { .compatible = "fsl,imx35-flexcan", .data = &fsl_imx25_devtype_data, },
1263 { .compatible = "fsl,imx25-flexcan", .data = &fsl_imx25_devtype_data, },
Marc Kleine-Buddee3587842013-10-03 23:51:55 +02001264 { .compatible = "fsl,p1010-flexcan", .data = &fsl_p1010_devtype_data, },
Stefan Agnercdce8442014-07-15 14:56:21 +02001265 { .compatible = "fsl,vf610-flexcan", .data = &fsl_vf610_devtype_data, },
Pankaj Bansal99b76682017-11-24 18:52:09 +05301266 { .compatible = "fsl,ls1021ar2-flexcan", .data = &fsl_ls1021a_r2_devtype_data, },
Hui Wang30c1e672012-06-28 16:21:35 +08001267 { /* sentinel */ },
1268};
Marc Kleine-Budde4358a9d2012-10-04 10:55:35 +02001269MODULE_DEVICE_TABLE(of, flexcan_of_match);
Hui Wang30c1e672012-06-28 16:21:35 +08001270
1271static const struct platform_device_id flexcan_id_table[] = {
1272 { .name = "flexcan", .driver_data = (kernel_ulong_t)&fsl_p1010_devtype_data, },
1273 { /* sentinel */ },
1274};
Marc Kleine-Budde4358a9d2012-10-04 10:55:35 +02001275MODULE_DEVICE_TABLE(platform, flexcan_id_table);
Hui Wang30c1e672012-06-28 16:21:35 +08001276
Bill Pemberton3c8ac0f2012-12-03 09:22:44 -05001277static int flexcan_probe(struct platform_device *pdev)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001278{
Hui Wang30c1e672012-06-28 16:21:35 +08001279 const struct of_device_id *of_id;
Marc Kleine-Buddedda0b3b2012-07-13 14:52:48 +02001280 const struct flexcan_devtype_data *devtype_data;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001281 struct net_device *dev;
1282 struct flexcan_priv *priv;
Andreas Werner555828e2015-03-22 17:35:52 +01001283 struct regulator *reg_xceiver;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001284 struct resource *mem;
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001285 struct clk *clk_ipg = NULL, *clk_per = NULL;
Marc Kleine-Budde89af8742015-05-08 09:32:58 +02001286 struct flexcan_regs __iomem *regs;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001287 int err, irq;
holt@sgi.com97efe9a2011-08-16 17:32:23 +00001288 u32 clock_freq = 0;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001289
Andreas Werner555828e2015-03-22 17:35:52 +01001290 reg_xceiver = devm_regulator_get(&pdev->dev, "xceiver");
1291 if (PTR_ERR(reg_xceiver) == -EPROBE_DEFER)
1292 return -EPROBE_DEFER;
1293 else if (IS_ERR(reg_xceiver))
1294 reg_xceiver = NULL;
1295
Hui Wangafc016d2012-06-28 16:21:34 +08001296 if (pdev->dev.of_node)
1297 of_property_read_u32(pdev->dev.of_node,
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +02001298 "clock-frequency", &clock_freq);
holt@sgi.com97efe9a2011-08-16 17:32:23 +00001299
1300 if (!clock_freq) {
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001301 clk_ipg = devm_clk_get(&pdev->dev, "ipg");
1302 if (IS_ERR(clk_ipg)) {
1303 dev_err(&pdev->dev, "no ipg clock defined\n");
Fabio Estevam933e4af2013-07-22 12:41:39 -03001304 return PTR_ERR(clk_ipg);
holt@sgi.com97efe9a2011-08-16 17:32:23 +00001305 }
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001306
1307 clk_per = devm_clk_get(&pdev->dev, "per");
1308 if (IS_ERR(clk_per)) {
1309 dev_err(&pdev->dev, "no per clock defined\n");
Fabio Estevam933e4af2013-07-22 12:41:39 -03001310 return PTR_ERR(clk_per);
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001311 }
Marc Kleine-Budde1a3e5172013-11-25 22:15:20 +01001312 clock_freq = clk_get_rate(clk_per);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001313 }
1314
1315 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1316 irq = platform_get_irq(pdev, 0);
Fabio Estevam933e4af2013-07-22 12:41:39 -03001317 if (irq <= 0)
1318 return -ENODEV;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001319
Marc Kleine-Budde89af8742015-05-08 09:32:58 +02001320 regs = devm_ioremap_resource(&pdev->dev, mem);
1321 if (IS_ERR(regs))
1322 return PTR_ERR(regs);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001323
Hui Wang30c1e672012-06-28 16:21:35 +08001324 of_id = of_match_device(flexcan_of_match, &pdev->dev);
1325 if (of_id) {
1326 devtype_data = of_id->data;
Marc Kleine-Budded0873e62014-03-04 22:04:22 +01001327 } else if (platform_get_device_id(pdev)->driver_data) {
Hui Wang30c1e672012-06-28 16:21:35 +08001328 devtype_data = (struct flexcan_devtype_data *)
Marc Kleine-Budded0873e62014-03-04 22:04:22 +01001329 platform_get_device_id(pdev)->driver_data;
Hui Wang30c1e672012-06-28 16:21:35 +08001330 } else {
Fabio Estevam933e4af2013-07-22 12:41:39 -03001331 return -ENODEV;
Hui Wang30c1e672012-06-28 16:21:35 +08001332 }
1333
Fabio Estevam933e4af2013-07-22 12:41:39 -03001334 dev = alloc_candev(sizeof(struct flexcan_priv), 1);
1335 if (!dev)
1336 return -ENOMEM;
1337
Marc Kleine-Budde30164752015-05-10 15:26:58 +02001338 platform_set_drvdata(pdev, dev);
1339 SET_NETDEV_DEV(dev, &pdev->dev);
1340
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001341 dev->netdev_ops = &flexcan_netdev_ops;
1342 dev->irq = irq;
Reuben Dowle9a123492011-11-01 11:18:03 +13001343 dev->flags |= IFF_ECHO;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001344
1345 priv = netdev_priv(dev);
Pankaj Bansal88462d22017-11-24 18:52:08 +05301346
Uwe Kleine-König0e030a32018-04-25 16:50:39 +02001347 if (of_property_read_bool(pdev->dev.of_node, "big-endian") ||
1348 devtype_data->quirks & FLEXCAN_QUIRK_DEFAULT_BIG_ENDIAN) {
Pankaj Bansal88462d22017-11-24 18:52:08 +05301349 priv->read = flexcan_read_be;
1350 priv->write = flexcan_write_be;
1351 } else {
Uwe Kleine-König0e030a32018-04-25 16:50:39 +02001352 priv->read = flexcan_read_le;
1353 priv->write = flexcan_write_le;
Pankaj Bansal88462d22017-11-24 18:52:08 +05301354 }
1355
holt@sgi.com97efe9a2011-08-16 17:32:23 +00001356 priv->can.clock.freq = clock_freq;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001357 priv->can.bittiming_const = &flexcan_bittiming_const;
1358 priv->can.do_set_mode = flexcan_set_mode;
1359 priv->can.do_get_berr_counter = flexcan_get_berr_counter;
1360 priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK |
1361 CAN_CTRLMODE_LISTENONLY | CAN_CTRLMODE_3_SAMPLES |
1362 CAN_CTRLMODE_BERR_REPORTING;
Marc Kleine-Budde89af8742015-05-08 09:32:58 +02001363 priv->regs = regs;
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001364 priv->clk_ipg = clk_ipg;
1365 priv->clk_per = clk_per;
Hui Wang30c1e672012-06-28 16:21:35 +08001366 priv->devtype_data = devtype_data;
Andreas Werner555828e2015-03-22 17:35:52 +01001367 priv->reg_xceiver = reg_xceiver;
Fabio Estevamb7c41142013-06-10 23:12:57 -03001368
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +02001369 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP) {
1370 priv->tx_mb_idx = FLEXCAN_TX_MB_OFF_TIMESTAMP;
1371 priv->tx_mb_reserved = &regs->mb[FLEXCAN_TX_MB_RESERVED_OFF_TIMESTAMP];
1372 } else {
1373 priv->tx_mb_idx = FLEXCAN_TX_MB_OFF_FIFO;
1374 priv->tx_mb_reserved = &regs->mb[FLEXCAN_TX_MB_RESERVED_OFF_FIFO];
1375 }
Marc Kleine-Buddeb93917c2015-07-12 00:47:47 +02001376 priv->tx_mb = &regs->mb[priv->tx_mb_idx];
1377
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +02001378 priv->reg_imask1_default = FLEXCAN_IFLAG_MB(priv->tx_mb_idx);
1379 priv->reg_imask2_default = 0;
Marc Kleine-Budde28ac7dc2015-08-04 13:46:10 +02001380
Marc Kleine-Budde30164752015-05-10 15:26:58 +02001381 priv->offload.mailbox_read = flexcan_mailbox_read;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001382
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +02001383 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP) {
1384 u64 imask;
1385
1386 priv->offload.mb_first = FLEXCAN_RX_MB_OFF_TIMESTAMP_FIRST;
1387 priv->offload.mb_last = FLEXCAN_RX_MB_OFF_TIMESTAMP_LAST;
1388
1389 imask = GENMASK_ULL(priv->offload.mb_last, priv->offload.mb_first);
1390 priv->reg_imask1_default |= imask;
1391 priv->reg_imask2_default |= imask >> 32;
1392
1393 err = can_rx_offload_add_timestamp(dev, &priv->offload);
1394 } else {
1395 priv->reg_imask1_default |= FLEXCAN_IFLAG_RX_FIFO_OVERFLOW |
1396 FLEXCAN_IFLAG_RX_FIFO_AVAILABLE;
1397 err = can_rx_offload_add_fifo(dev, &priv->offload, FLEXCAN_NAPI_WEIGHT);
1398 }
Marc Kleine-Budde30164752015-05-10 15:26:58 +02001399 if (err)
1400 goto failed_offload;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001401
1402 err = register_flexcandev(dev);
1403 if (err) {
1404 dev_err(&pdev->dev, "registering netdev failed\n");
1405 goto failed_register;
1406 }
1407
Fabio Baltieriadccadb2012-12-18 18:50:58 +01001408 devm_can_led_init(dev);
1409
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001410 dev_info(&pdev->dev, "device registered (reg_base=%p, irq=%d)\n",
Marc Kleine-Budde89af8742015-05-08 09:32:58 +02001411 priv->regs, dev->irq);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001412
1413 return 0;
1414
Marc Kleine-Budde30164752015-05-10 15:26:58 +02001415 failed_offload:
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001416 failed_register:
1417 free_candev(dev);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001418 return err;
1419}
1420
Bill Pemberton3c8ac0f2012-12-03 09:22:44 -05001421static int flexcan_remove(struct platform_device *pdev)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001422{
1423 struct net_device *dev = platform_get_drvdata(pdev);
Marc Kleine-Budded96e43e2014-02-28 20:48:36 +01001424 struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001425
1426 unregister_flexcandev(dev);
Marc Kleine-Budde30164752015-05-10 15:26:58 +02001427 can_rx_offload_del(&priv->offload);
Marc Kleine-Budde9a275862010-10-21 05:07:58 +00001428 free_candev(dev);
1429
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001430 return 0;
1431}
1432
Marc Kleine-Budde08c6d352014-03-05 19:10:44 +01001433static int __maybe_unused flexcan_suspend(struct device *device)
Eric Bénard8b5e2182012-05-08 17:12:17 +02001434{
Fabio Estevam588e7a82013-05-20 15:43:43 -03001435 struct net_device *dev = dev_get_drvdata(device);
Eric Bénard8b5e2182012-05-08 17:12:17 +02001436 struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001437 int err;
Eric Bénard8b5e2182012-05-08 17:12:17 +02001438
Eric Bénard8b5e2182012-05-08 17:12:17 +02001439 if (netif_running(dev)) {
Fabio Estevam4de349e2016-08-17 12:41:08 -03001440 err = flexcan_chip_disable(priv);
1441 if (err)
1442 return err;
Eric Bénard8b5e2182012-05-08 17:12:17 +02001443 netif_stop_queue(dev);
1444 netif_device_detach(dev);
1445 }
1446 priv->can.state = CAN_STATE_SLEEPING;
1447
1448 return 0;
1449}
1450
Marc Kleine-Budde08c6d352014-03-05 19:10:44 +01001451static int __maybe_unused flexcan_resume(struct device *device)
Eric Bénard8b5e2182012-05-08 17:12:17 +02001452{
Fabio Estevam588e7a82013-05-20 15:43:43 -03001453 struct net_device *dev = dev_get_drvdata(device);
Eric Bénard8b5e2182012-05-08 17:12:17 +02001454 struct flexcan_priv *priv = netdev_priv(dev);
Fabio Estevam4de349e2016-08-17 12:41:08 -03001455 int err;
Eric Bénard8b5e2182012-05-08 17:12:17 +02001456
1457 priv->can.state = CAN_STATE_ERROR_ACTIVE;
1458 if (netif_running(dev)) {
1459 netif_device_attach(dev);
1460 netif_start_queue(dev);
Fabio Estevam4de349e2016-08-17 12:41:08 -03001461 err = flexcan_chip_enable(priv);
1462 if (err)
1463 return err;
Eric Bénard8b5e2182012-05-08 17:12:17 +02001464 }
Fabio Estevam4de349e2016-08-17 12:41:08 -03001465 return 0;
Eric Bénard8b5e2182012-05-08 17:12:17 +02001466}
Fabio Estevam588e7a82013-05-20 15:43:43 -03001467
1468static SIMPLE_DEV_PM_OPS(flexcan_pm_ops, flexcan_suspend, flexcan_resume);
Eric Bénard8b5e2182012-05-08 17:12:17 +02001469
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001470static struct platform_driver flexcan_driver = {
holt@sgi.comc8aef4c2011-08-16 17:32:22 +00001471 .driver = {
1472 .name = DRV_NAME,
Fabio Estevam588e7a82013-05-20 15:43:43 -03001473 .pm = &flexcan_pm_ops,
holt@sgi.comc8aef4c2011-08-16 17:32:22 +00001474 .of_match_table = flexcan_of_match,
1475 },
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001476 .probe = flexcan_probe,
Bill Pemberton3c8ac0f2012-12-03 09:22:44 -05001477 .remove = flexcan_remove,
Hui Wang30c1e672012-06-28 16:21:35 +08001478 .id_table = flexcan_id_table,
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001479};
1480
Axel Lin871d3372011-11-27 15:42:31 +00001481module_platform_driver(flexcan_driver);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001482
1483MODULE_AUTHOR("Sascha Hauer <kernel@pengutronix.de>, "
1484 "Marc Kleine-Budde <kernel@pengutronix.de>");
1485MODULE_LICENSE("GPL v2");
1486MODULE_DESCRIPTION("CAN port driver for flexcan based chip");