blob: 5c6903e23c01d3539c6a70aa566ce74706f3325c [file] [log] [blame]
Fabio Estevam5b749be2018-07-06 14:35:12 -03001// SPDX-License-Identifier: GPL-2.0
2//
3// flexcan.c - FLEXCAN CAN controller driver
4//
5// Copyright (c) 2005-2006 Varma Electronics Oy
6// Copyright (c) 2009 Sascha Hauer, Pengutronix
7// Copyright (c) 2010-2017 Pengutronix, Marc Kleine-Budde <kernel@pengutronix.de>
8// Copyright (c) 2014 David Jander, Protonic Holland
9//
10// Based on code originally by Andrey Volkov <avolkov@varma-el.com>
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +020011
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +020012#include <linux/can.h>
13#include <linux/can/dev.h>
14#include <linux/can/error.h>
Fabio Baltieriadccadb2012-12-18 18:50:58 +010015#include <linux/can/led.h>
Marc Kleine-Budde30164752015-05-10 15:26:58 +020016#include <linux/can/rx-offload.h>
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +020017#include <linux/clk.h>
18#include <linux/delay.h>
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +020019#include <linux/interrupt.h>
20#include <linux/io.h>
Aisheng Dongde3578c2018-11-23 08:35:33 +000021#include <linux/mfd/syscon.h>
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +020022#include <linux/module.h>
Marc Kleine-Budde555f6e52020-09-22 16:44:10 +020023#include <linux/netdevice.h>
holt@sgi.com97efe9a2011-08-16 17:32:23 +000024#include <linux/of.h>
Hui Wang30c1e672012-06-28 16:21:35 +080025#include <linux/of_device.h>
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +020026#include <linux/platform_device.h>
Aisheng Dongca109892018-11-30 08:53:26 +000027#include <linux/pm_runtime.h>
Aisheng Dongde3578c2018-11-23 08:35:33 +000028#include <linux/regmap.h>
Marc Kleine-Budde555f6e52020-09-22 16:44:10 +020029#include <linux/regulator/consumer.h>
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +020030
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +020031#define DRV_NAME "flexcan"
32
33/* 8 for RX fifo and 2 error handling */
34#define FLEXCAN_NAPI_WEIGHT (8 + 2)
35
36/* FLEXCAN module configuration register (CANMCR) bits */
37#define FLEXCAN_MCR_MDIS BIT(31)
38#define FLEXCAN_MCR_FRZ BIT(30)
39#define FLEXCAN_MCR_FEN BIT(29)
40#define FLEXCAN_MCR_HALT BIT(28)
41#define FLEXCAN_MCR_NOT_RDY BIT(27)
42#define FLEXCAN_MCR_WAK_MSK BIT(26)
43#define FLEXCAN_MCR_SOFTRST BIT(25)
44#define FLEXCAN_MCR_FRZ_ACK BIT(24)
45#define FLEXCAN_MCR_SUPV BIT(23)
46#define FLEXCAN_MCR_SLF_WAK BIT(22)
47#define FLEXCAN_MCR_WRN_EN BIT(21)
48#define FLEXCAN_MCR_LPM_ACK BIT(20)
49#define FLEXCAN_MCR_WAK_SRC BIT(19)
50#define FLEXCAN_MCR_DOZE BIT(18)
51#define FLEXCAN_MCR_SRX_DIS BIT(17)
Marc Kleine-Budde62d10862015-08-27 16:01:27 +020052#define FLEXCAN_MCR_IRMQ BIT(16)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +020053#define FLEXCAN_MCR_LPRIO_EN BIT(13)
54#define FLEXCAN_MCR_AEN BIT(12)
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +020055/* MCR_MAXMB: maximum used MBs is MAXMB + 1 */
Marc Kleine-Budde4c728d82014-09-02 16:54:17 +020056#define FLEXCAN_MCR_MAXMB(x) ((x) & 0x7f)
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +020057#define FLEXCAN_MCR_IDAM_A (0x0 << 8)
58#define FLEXCAN_MCR_IDAM_B (0x1 << 8)
59#define FLEXCAN_MCR_IDAM_C (0x2 << 8)
60#define FLEXCAN_MCR_IDAM_D (0x3 << 8)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +020061
62/* FLEXCAN control register (CANCTRL) bits */
63#define FLEXCAN_CTRL_PRESDIV(x) (((x) & 0xff) << 24)
64#define FLEXCAN_CTRL_RJW(x) (((x) & 0x03) << 22)
65#define FLEXCAN_CTRL_PSEG1(x) (((x) & 0x07) << 19)
66#define FLEXCAN_CTRL_PSEG2(x) (((x) & 0x07) << 16)
67#define FLEXCAN_CTRL_BOFF_MSK BIT(15)
68#define FLEXCAN_CTRL_ERR_MSK BIT(14)
69#define FLEXCAN_CTRL_CLK_SRC BIT(13)
70#define FLEXCAN_CTRL_LPB BIT(12)
71#define FLEXCAN_CTRL_TWRN_MSK BIT(11)
72#define FLEXCAN_CTRL_RWRN_MSK BIT(10)
73#define FLEXCAN_CTRL_SMP BIT(7)
74#define FLEXCAN_CTRL_BOFF_REC BIT(6)
75#define FLEXCAN_CTRL_TSYN BIT(5)
76#define FLEXCAN_CTRL_LBUF BIT(4)
77#define FLEXCAN_CTRL_LOM BIT(3)
78#define FLEXCAN_CTRL_PROPSEG(x) ((x) & 0x07)
79#define FLEXCAN_CTRL_ERR_BUS (FLEXCAN_CTRL_ERR_MSK)
80#define FLEXCAN_CTRL_ERR_STATE \
81 (FLEXCAN_CTRL_TWRN_MSK | FLEXCAN_CTRL_RWRN_MSK | \
82 FLEXCAN_CTRL_BOFF_MSK)
83#define FLEXCAN_CTRL_ERR_ALL \
84 (FLEXCAN_CTRL_ERR_BUS | FLEXCAN_CTRL_ERR_STATE)
85
Stefan Agnercdce8442014-07-15 14:56:21 +020086/* FLEXCAN control register 2 (CTRL2) bits */
Marc Kleine-Budde6f75fce2014-09-23 11:03:01 +020087#define FLEXCAN_CTRL2_ECRWRE BIT(29)
88#define FLEXCAN_CTRL2_WRMFRZ BIT(28)
89#define FLEXCAN_CTRL2_RFFN(x) (((x) & 0x0f) << 24)
90#define FLEXCAN_CTRL2_TASD(x) (((x) & 0x1f) << 19)
91#define FLEXCAN_CTRL2_MRP BIT(18)
92#define FLEXCAN_CTRL2_RRS BIT(17)
93#define FLEXCAN_CTRL2_EACEN BIT(16)
Stefan Agnercdce8442014-07-15 14:56:21 +020094
95/* FLEXCAN memory error control register (MECR) bits */
96#define FLEXCAN_MECR_ECRWRDIS BIT(31)
97#define FLEXCAN_MECR_HANCEI_MSK BIT(19)
98#define FLEXCAN_MECR_FANCEI_MSK BIT(18)
99#define FLEXCAN_MECR_CEI_MSK BIT(16)
100#define FLEXCAN_MECR_HAERRIE BIT(15)
101#define FLEXCAN_MECR_FAERRIE BIT(14)
102#define FLEXCAN_MECR_EXTERRIE BIT(13)
103#define FLEXCAN_MECR_RERRDIS BIT(9)
104#define FLEXCAN_MECR_ECCDIS BIT(8)
105#define FLEXCAN_MECR_NCEFAFRZ BIT(7)
106
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200107/* FLEXCAN error and status register (ESR) bits */
108#define FLEXCAN_ESR_TWRN_INT BIT(17)
109#define FLEXCAN_ESR_RWRN_INT BIT(16)
110#define FLEXCAN_ESR_BIT1_ERR BIT(15)
111#define FLEXCAN_ESR_BIT0_ERR BIT(14)
112#define FLEXCAN_ESR_ACK_ERR BIT(13)
113#define FLEXCAN_ESR_CRC_ERR BIT(12)
114#define FLEXCAN_ESR_FRM_ERR BIT(11)
115#define FLEXCAN_ESR_STF_ERR BIT(10)
116#define FLEXCAN_ESR_TX_WRN BIT(9)
117#define FLEXCAN_ESR_RX_WRN BIT(8)
118#define FLEXCAN_ESR_IDLE BIT(7)
119#define FLEXCAN_ESR_TXRX BIT(6)
120#define FLEXCAN_EST_FLT_CONF_SHIFT (4)
121#define FLEXCAN_ESR_FLT_CONF_MASK (0x3 << FLEXCAN_EST_FLT_CONF_SHIFT)
122#define FLEXCAN_ESR_FLT_CONF_ACTIVE (0x0 << FLEXCAN_EST_FLT_CONF_SHIFT)
123#define FLEXCAN_ESR_FLT_CONF_PASSIVE (0x1 << FLEXCAN_EST_FLT_CONF_SHIFT)
124#define FLEXCAN_ESR_BOFF_INT BIT(2)
125#define FLEXCAN_ESR_ERR_INT BIT(1)
126#define FLEXCAN_ESR_WAK_INT BIT(0)
127#define FLEXCAN_ESR_ERR_BUS \
128 (FLEXCAN_ESR_BIT1_ERR | FLEXCAN_ESR_BIT0_ERR | \
129 FLEXCAN_ESR_ACK_ERR | FLEXCAN_ESR_CRC_ERR | \
130 FLEXCAN_ESR_FRM_ERR | FLEXCAN_ESR_STF_ERR)
131#define FLEXCAN_ESR_ERR_STATE \
132 (FLEXCAN_ESR_TWRN_INT | FLEXCAN_ESR_RWRN_INT | FLEXCAN_ESR_BOFF_INT)
133#define FLEXCAN_ESR_ERR_ALL \
134 (FLEXCAN_ESR_ERR_BUS | FLEXCAN_ESR_ERR_STATE)
Wolfgang Grandegger6e9d5542011-12-12 16:09:28 +0100135#define FLEXCAN_ESR_ALL_INT \
136 (FLEXCAN_ESR_TWRN_INT | FLEXCAN_ESR_RWRN_INT | \
Joakim Zhangab605232019-12-04 11:36:08 +0000137 FLEXCAN_ESR_BOFF_INT | FLEXCAN_ESR_ERR_INT)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200138
139/* FLEXCAN interrupt flag register (IFLAG) bits */
David Jander25e92442014-09-03 16:47:22 +0200140/* Errata ERR005829 step7: Reserve first valid MB */
Alexander Steincbffaf72018-10-11 17:01:25 +0200141#define FLEXCAN_TX_MB_RESERVED_OFF_FIFO 8
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200142#define FLEXCAN_TX_MB_RESERVED_OFF_TIMESTAMP 0
Alexander Steincbffaf72018-10-11 17:01:25 +0200143#define FLEXCAN_RX_MB_OFF_TIMESTAMP_FIRST (FLEXCAN_TX_MB_RESERVED_OFF_TIMESTAMP + 1)
Marc Kleine-Budde8ce51392019-03-01 12:17:30 +0100144#define FLEXCAN_IFLAG_MB(x) BIT_ULL(x)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200145#define FLEXCAN_IFLAG_RX_FIFO_OVERFLOW BIT(7)
146#define FLEXCAN_IFLAG_RX_FIFO_WARN BIT(6)
147#define FLEXCAN_IFLAG_RX_FIFO_AVAILABLE BIT(5)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200148
149/* FLEXCAN message buffers */
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200150#define FLEXCAN_MB_CODE_MASK (0xf << 24)
151#define FLEXCAN_MB_CODE_RX_BUSY_BIT (0x1 << 24)
Marc Kleine-Buddec32fe4a2014-09-16 12:39:28 +0200152#define FLEXCAN_MB_CODE_RX_INACTIVE (0x0 << 24)
153#define FLEXCAN_MB_CODE_RX_EMPTY (0x4 << 24)
154#define FLEXCAN_MB_CODE_RX_FULL (0x2 << 24)
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200155#define FLEXCAN_MB_CODE_RX_OVERRUN (0x6 << 24)
Marc Kleine-Buddec32fe4a2014-09-16 12:39:28 +0200156#define FLEXCAN_MB_CODE_RX_RANSWER (0xa << 24)
157
158#define FLEXCAN_MB_CODE_TX_INACTIVE (0x8 << 24)
159#define FLEXCAN_MB_CODE_TX_ABORT (0x9 << 24)
160#define FLEXCAN_MB_CODE_TX_DATA (0xc << 24)
161#define FLEXCAN_MB_CODE_TX_TANSWER (0xe << 24)
162
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200163#define FLEXCAN_MB_CNT_SRR BIT(22)
164#define FLEXCAN_MB_CNT_IDE BIT(21)
165#define FLEXCAN_MB_CNT_RTR BIT(20)
166#define FLEXCAN_MB_CNT_LENGTH(x) (((x) & 0xf) << 16)
167#define FLEXCAN_MB_CNT_TIMESTAMP(x) ((x) & 0xffff)
168
Joakim Zhang247e5352019-01-31 09:37:22 +0000169#define FLEXCAN_TIMEOUT_US (250)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200170
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200171/* FLEXCAN hardware feature flags
Wolfgang Grandeggerbb698ca2012-10-10 21:10:42 +0200172 *
173 * Below is some version info we got:
ZHU Yi (ST-FIR/ENG1-Zhu)da49a802017-09-15 07:03:58 +0000174 * SOC Version IP-Version Glitch- [TR]WRN_INT IRQ Err Memory err RTR re-
175 * Filter? connected? Passive detection ception in MB
Marc Kleine-Budde658f5342017-11-22 13:01:08 +0100176 * MX25 FlexCAN2 03.00.00.00 no no no no no
ZHU Yi (ST-FIR/ENG1-Zhu)da49a802017-09-15 07:03:58 +0000177 * MX28 FlexCAN2 03.00.04.00 yes yes no no no
Marc Kleine-Budde658f5342017-11-22 13:01:08 +0100178 * MX35 FlexCAN2 03.00.00.00 no no no no no
ZHU Yi (ST-FIR/ENG1-Zhu)da49a802017-09-15 07:03:58 +0000179 * MX53 FlexCAN2 03.00.00.00 yes no no no no
180 * MX6s FlexCAN3 10.00.12.00 yes yes no no yes
Marc Kleine-Budde29c64b12017-11-27 09:18:21 +0100181 * VF610 FlexCAN3 ? no yes no yes yes?
Pankaj Bansal99b76682017-11-24 18:52:09 +0530182 * LS1021A FlexCAN2 03.00.04.00 no yes no no yes
Wolfgang Grandeggerbb698ca2012-10-10 21:10:42 +0200183 *
184 * Some SOCs do not have the RX_WARN & TX_WARN interrupt line connected.
185 */
Marc Kleine-Buddeef4b6232020-09-22 16:44:14 +0200186
187/* [TR]WRN_INT not connected */
188#define FLEXCAN_QUIRK_BROKEN_WERR_STATE BIT(1)
189 /* Disable RX FIFO Global mask */
190#define FLEXCAN_QUIRK_DISABLE_RXFG BIT(2)
191/* Enable EACEN and RRS bit in ctrl2 */
192#define FLEXCAN_QUIRK_ENABLE_EACEN_RRS BIT(3)
193/* Disable Memory error detection */
194#define FLEXCAN_QUIRK_DISABLE_MECR BIT(4)
195/* Use timestamp based offloading */
196#define FLEXCAN_QUIRK_USE_OFF_TIMESTAMP BIT(5)
197/* No interrupt for error passive */
198#define FLEXCAN_QUIRK_BROKEN_PERR_STATE BIT(6)
199/* default to BE register access */
200#define FLEXCAN_QUIRK_DEFAULT_BIG_ENDIAN BIT(7)
201/* Setup stop mode to support wakeup */
202#define FLEXCAN_QUIRK_SETUP_STOP_MODE BIT(8)
Wolfgang Grandegger4f72e5f2012-09-28 03:17:15 +0000203
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200204/* Structure of the message buffer */
205struct flexcan_mb {
206 u32 can_ctrl;
207 u32 can_id;
Pankaj Bansal05179612018-11-23 22:18:44 +0100208 u32 data[];
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200209};
210
211/* Structure of the hardware registers */
212struct flexcan_regs {
213 u32 mcr; /* 0x00 */
Marc Kleine-Buddefe63a062020-09-22 16:44:13 +0200214 u32 ctrl; /* 0x04 - Not affected by Soft Reset */
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200215 u32 timer; /* 0x08 */
Marc Kleine-Budde4b702872020-09-22 16:44:12 +0200216 u32 tcr; /* 0x0c */
Marc Kleine-Buddefe63a062020-09-22 16:44:13 +0200217 u32 rxgmask; /* 0x10 - Not affected by Soft Reset */
218 u32 rx14mask; /* 0x14 - Not affected by Soft Reset */
219 u32 rx15mask; /* 0x18 - Not affected by Soft Reset */
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200220 u32 ecr; /* 0x1c */
221 u32 esr; /* 0x20 */
222 u32 imask2; /* 0x24 */
223 u32 imask1; /* 0x28 */
224 u32 iflag2; /* 0x2c */
225 u32 iflag1; /* 0x30 */
Marc Kleine-Budde62d10862015-08-27 16:01:27 +0200226 union { /* 0x34 */
227 u32 gfwr_mx28; /* MX28, MX53 */
Marc Kleine-Buddefe63a062020-09-22 16:44:13 +0200228 u32 ctrl2; /* MX6, VF610 - Not affected by Soft Reset */
Marc Kleine-Budde62d10862015-08-27 16:01:27 +0200229 };
Hui Wang30c1e672012-06-28 16:21:35 +0800230 u32 esr2; /* 0x38 */
231 u32 imeur; /* 0x3c */
232 u32 lrfr; /* 0x40 */
233 u32 crcr; /* 0x44 */
234 u32 rxfgmask; /* 0x48 */
Marc Kleine-Buddefe63a062020-09-22 16:44:13 +0200235 u32 rxfir; /* 0x4c - Not affected by Soft Reset */
236 u32 cbt; /* 0x50 - Not affected by Soft Reset */
Marc Kleine-Budde4b702872020-09-22 16:44:12 +0200237 u32 _reserved2; /* 0x54 */
238 u32 dbg1; /* 0x58 */
239 u32 dbg2; /* 0x5c */
240 u32 _reserved3[8]; /* 0x60 */
Marc Kleine-Buddefe63a062020-09-22 16:44:13 +0200241 u8 mb[2][512]; /* 0x80 - Not affected by Soft Reset */
Marc Kleine-Budde66a6ef02014-09-17 12:50:48 +0200242 /* FIFO-mode:
243 * MB
244 * 0x080...0x08f 0 RX message buffer
Alexandre Belloni68508632020-02-14 15:17:51 +0100245 * 0x090...0x0df 1-5 reserved
Marc Kleine-Budde66a6ef02014-09-17 12:50:48 +0200246 * 0x0e0...0x0ff 6-7 8 entry ID table
247 * (mx25, mx28, mx35, mx53)
248 * 0x0e0...0x2df 6-7..37 8..128 entry ID table
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200249 * size conf'ed via ctrl2::RFFN
Marc Kleine-Budde66a6ef02014-09-17 12:50:48 +0200250 * (mx6, vf610)
251 */
Marc Kleine-Budde62d10862015-08-27 16:01:27 +0200252 u32 _reserved4[256]; /* 0x480 */
Marc Kleine-Buddefe63a062020-09-22 16:44:13 +0200253 u32 rximr[64]; /* 0x880 - Not affected by Soft Reset */
Marc Kleine-Budde62d10862015-08-27 16:01:27 +0200254 u32 _reserved5[24]; /* 0x980 */
255 u32 gfwr_mx6; /* 0x9e0 - MX6 */
256 u32 _reserved6[63]; /* 0x9e4 */
Stefan Agnercdce8442014-07-15 14:56:21 +0200257 u32 mecr; /* 0xae0 */
258 u32 erriar; /* 0xae4 */
259 u32 erridpr; /* 0xae8 */
260 u32 errippr; /* 0xaec */
261 u32 rerrar; /* 0xaf0 */
262 u32 rerrdr; /* 0xaf4 */
263 u32 rerrsynr; /* 0xaf8 */
264 u32 errsr; /* 0xafc */
Marc Kleine-Budde4b702872020-09-22 16:44:12 +0200265 u32 _reserved7[64]; /* 0xb00 */
Marc Kleine-Buddefe63a062020-09-22 16:44:13 +0200266 u32 fdctrl; /* 0xc00 - Not affected by Soft Reset */
267 u32 fdcbt; /* 0xc04 - Not affected by Soft Reset */
Marc Kleine-Budde4b702872020-09-22 16:44:12 +0200268 u32 fdcrc; /* 0xc08 */
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200269};
270
Marc Kleine-Budde4b702872020-09-22 16:44:12 +0200271static_assert(sizeof(struct flexcan_regs) == 0x4 + 0xc08);
272
Hui Wang30c1e672012-06-28 16:21:35 +0800273struct flexcan_devtype_data {
Marc Kleine-Buddef377bff2015-05-08 15:22:36 +0200274 u32 quirks; /* quirks needed for different IP cores */
Hui Wang30c1e672012-06-28 16:21:35 +0800275};
276
Aisheng Dongde3578c2018-11-23 08:35:33 +0000277struct flexcan_stop_mode {
278 struct regmap *gpr;
279 u8 req_gpr;
280 u8 req_bit;
281 u8 ack_gpr;
282 u8 ack_bit;
283};
284
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200285struct flexcan_priv {
286 struct can_priv can;
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200287 struct can_rx_offload offload;
Aisheng Dongca109892018-11-30 08:53:26 +0000288 struct device *dev;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200289
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200290 struct flexcan_regs __iomem *regs;
Pankaj Bansal05179612018-11-23 22:18:44 +0100291 struct flexcan_mb __iomem *tx_mb;
Marc Kleine-Buddeb93917c2015-07-12 00:47:47 +0200292 struct flexcan_mb __iomem *tx_mb_reserved;
Pankaj Bansal05179612018-11-23 22:18:44 +0100293 u8 tx_mb_idx;
294 u8 mb_count;
295 u8 mb_size;
Dong Aisheng8c306be2018-12-13 07:08:00 +0000296 u8 clk_src; /* clock source of CAN Protocol Engine */
297
Marc Kleine-Budde8ce51392019-03-01 12:17:30 +0100298 u64 rx_mask;
Marc Kleine-Budde0ca64f022019-03-01 13:54:19 +0100299 u64 tx_mask;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200300 u32 reg_ctrl_default;
301
Steffen Trumtrar3d42a372012-07-17 16:14:34 +0200302 struct clk *clk_ipg;
303 struct clk *clk_per;
Marc Kleine-Buddedda0b3b2012-07-13 14:52:48 +0200304 const struct flexcan_devtype_data *devtype_data;
Fabio Estevamb7c41142013-06-10 23:12:57 -0300305 struct regulator *reg_xceiver;
Aisheng Dongde3578c2018-11-23 08:35:33 +0000306 struct flexcan_stop_mode stm;
Pankaj Bansal88462d22017-11-24 18:52:08 +0530307
308 /* Read and Write APIs */
309 u32 (*read)(void __iomem *addr);
310 void (*write)(u32 val, void __iomem *addr);
Hui Wang30c1e672012-06-28 16:21:35 +0800311};
312
Marc Kleine-Buddea3c11a72016-07-04 14:45:44 +0200313static const struct flexcan_devtype_data fsl_p1010_devtype_data = {
ZHU Yi (ST-FIR/ENG1-Zhu)fb5b91d62017-09-15 07:09:37 +0000314 .quirks = FLEXCAN_QUIRK_BROKEN_WERR_STATE |
Uwe Kleine-König0e030a32018-04-25 16:50:39 +0200315 FLEXCAN_QUIRK_BROKEN_PERR_STATE |
316 FLEXCAN_QUIRK_DEFAULT_BIG_ENDIAN,
317};
318
319static const struct flexcan_devtype_data fsl_imx25_devtype_data = {
320 .quirks = FLEXCAN_QUIRK_BROKEN_WERR_STATE |
ZHU Yi (ST-FIR/ENG1-Zhu)fb5b91d62017-09-15 07:09:37 +0000321 FLEXCAN_QUIRK_BROKEN_PERR_STATE,
Hui Wang30c1e672012-06-28 16:21:35 +0800322};
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200323
ZHU Yi (ST-FIR/ENG1-Zhu)083c5572017-09-15 07:08:23 +0000324static const struct flexcan_devtype_data fsl_imx28_devtype_data = {
325 .quirks = FLEXCAN_QUIRK_BROKEN_PERR_STATE,
326};
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200327
Marc Kleine-Buddea3c11a72016-07-04 14:45:44 +0200328static const struct flexcan_devtype_data fsl_imx6q_devtype_data = {
Marc Kleine-Budde096de072015-09-01 10:28:46 +0200329 .quirks = FLEXCAN_QUIRK_DISABLE_RXFG | FLEXCAN_QUIRK_ENABLE_EACEN_RRS |
Aisheng Dongde3578c2018-11-23 08:35:33 +0000330 FLEXCAN_QUIRK_USE_OFF_TIMESTAMP | FLEXCAN_QUIRK_BROKEN_PERR_STATE |
331 FLEXCAN_QUIRK_SETUP_STOP_MODE,
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200332};
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200333
Marc Kleine-Buddea3c11a72016-07-04 14:45:44 +0200334static const struct flexcan_devtype_data fsl_vf610_devtype_data = {
Marc Kleine-Budde9eb7aa82015-09-01 08:57:55 +0200335 .quirks = FLEXCAN_QUIRK_DISABLE_RXFG | FLEXCAN_QUIRK_ENABLE_EACEN_RRS |
Marc Kleine-Budde29c64b12017-11-27 09:18:21 +0100336 FLEXCAN_QUIRK_DISABLE_MECR | FLEXCAN_QUIRK_USE_OFF_TIMESTAMP |
337 FLEXCAN_QUIRK_BROKEN_PERR_STATE,
Stefan Agnercdce8442014-07-15 14:56:21 +0200338};
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200339
Pankaj Bansal99b76682017-11-24 18:52:09 +0530340static const struct flexcan_devtype_data fsl_ls1021a_r2_devtype_data = {
341 .quirks = FLEXCAN_QUIRK_DISABLE_RXFG | FLEXCAN_QUIRK_ENABLE_EACEN_RRS |
342 FLEXCAN_QUIRK_DISABLE_MECR | FLEXCAN_QUIRK_BROKEN_PERR_STATE |
343 FLEXCAN_QUIRK_USE_OFF_TIMESTAMP,
344};
345
Marc Kleine-Budde194b9a42012-07-16 12:58:31 +0200346static const struct can_bittiming_const flexcan_bittiming_const = {
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200347 .name = DRV_NAME,
348 .tseg1_min = 4,
349 .tseg1_max = 16,
350 .tseg2_min = 2,
351 .tseg2_max = 8,
352 .sjw_max = 4,
353 .brp_min = 1,
354 .brp_max = 256,
355 .brp_inc = 1,
356};
357
Pankaj Bansal88462d22017-11-24 18:52:08 +0530358/* FlexCAN module is essentially modelled as a little-endian IP in most
359 * SoCs, i.e the registers as well as the message buffer areas are
360 * implemented in a little-endian fashion.
361 *
362 * However there are some SoCs (e.g. LS1021A) which implement the FlexCAN
363 * module in a big-endian fashion (i.e the registers as well as the
364 * message buffer areas are implemented in a big-endian way).
365 *
366 * In addition, the FlexCAN module can be found on SoCs having ARM or
367 * PPC cores. So, we need to abstract off the register read/write
368 * functions, ensuring that these cater to all the combinations of module
369 * endianness and underlying CPU endianness.
holt@sgi.com61e271e2011-08-16 17:32:20 +0000370 */
Pankaj Bansal88462d22017-11-24 18:52:08 +0530371static inline u32 flexcan_read_be(void __iomem *addr)
holt@sgi.com61e271e2011-08-16 17:32:20 +0000372{
Pankaj Bansal88462d22017-11-24 18:52:08 +0530373 return ioread32be(addr);
holt@sgi.com61e271e2011-08-16 17:32:20 +0000374}
375
Pankaj Bansal88462d22017-11-24 18:52:08 +0530376static inline void flexcan_write_be(u32 val, void __iomem *addr)
holt@sgi.com61e271e2011-08-16 17:32:20 +0000377{
Pankaj Bansal88462d22017-11-24 18:52:08 +0530378 iowrite32be(val, addr);
holt@sgi.com61e271e2011-08-16 17:32:20 +0000379}
380
Pankaj Bansal88462d22017-11-24 18:52:08 +0530381static inline u32 flexcan_read_le(void __iomem *addr)
holt@sgi.com61e271e2011-08-16 17:32:20 +0000382{
Pankaj Bansal88462d22017-11-24 18:52:08 +0530383 return ioread32(addr);
holt@sgi.com61e271e2011-08-16 17:32:20 +0000384}
Pankaj Bansal88462d22017-11-24 18:52:08 +0530385
386static inline void flexcan_write_le(u32 val, void __iomem *addr)
387{
388 iowrite32(val, addr);
389}
holt@sgi.com61e271e2011-08-16 17:32:20 +0000390
Pankaj Bansal05179612018-11-23 22:18:44 +0100391static struct flexcan_mb __iomem *flexcan_get_mb(const struct flexcan_priv *priv,
392 u8 mb_index)
393{
Pankaj Bansal6cbf7602018-08-28 23:19:12 +0530394 u8 bank_size;
395 bool bank;
396
Pankaj Bansal05179612018-11-23 22:18:44 +0100397 if (WARN_ON(mb_index >= priv->mb_count))
398 return NULL;
399
Pankaj Bansal6cbf7602018-08-28 23:19:12 +0530400 bank_size = sizeof(priv->regs->mb[0]) / priv->mb_size;
401
402 bank = mb_index >= bank_size;
403 if (bank)
404 mb_index -= bank_size;
405
Pankaj Bansal05179612018-11-23 22:18:44 +0100406 return (struct flexcan_mb __iomem *)
Pankaj Bansal6cbf7602018-08-28 23:19:12 +0530407 (&priv->regs->mb[bank][priv->mb_size * mb_index]);
Pankaj Bansal05179612018-11-23 22:18:44 +0100408}
409
Joakim Zhangb7603d02019-12-04 11:36:11 +0000410static int flexcan_low_power_enter_ack(struct flexcan_priv *priv)
411{
412 struct flexcan_regs __iomem *regs = priv->regs;
413 unsigned int timeout = FLEXCAN_TIMEOUT_US / 10;
414
415 while (timeout-- && !(priv->read(&regs->mcr) & FLEXCAN_MCR_LPM_ACK))
416 udelay(10);
417
418 if (!(priv->read(&regs->mcr) & FLEXCAN_MCR_LPM_ACK))
419 return -ETIMEDOUT;
420
421 return 0;
422}
423
424static int flexcan_low_power_exit_ack(struct flexcan_priv *priv)
425{
426 struct flexcan_regs __iomem *regs = priv->regs;
427 unsigned int timeout = FLEXCAN_TIMEOUT_US / 10;
428
429 while (timeout-- && (priv->read(&regs->mcr) & FLEXCAN_MCR_LPM_ACK))
430 udelay(10);
431
432 if (priv->read(&regs->mcr) & FLEXCAN_MCR_LPM_ACK)
433 return -ETIMEDOUT;
434
435 return 0;
436}
437
Aisheng Dongde3578c2018-11-23 08:35:33 +0000438static void flexcan_enable_wakeup_irq(struct flexcan_priv *priv, bool enable)
439{
440 struct flexcan_regs __iomem *regs = priv->regs;
441 u32 reg_mcr;
442
443 reg_mcr = priv->read(&regs->mcr);
444
445 if (enable)
446 reg_mcr |= FLEXCAN_MCR_WAK_MSK;
447 else
448 reg_mcr &= ~FLEXCAN_MCR_WAK_MSK;
449
450 priv->write(reg_mcr, &regs->mcr);
451}
452
Joakim Zhang5f186c22019-07-02 01:45:41 +0000453static inline int flexcan_enter_stop_mode(struct flexcan_priv *priv)
Aisheng Dongde3578c2018-11-23 08:35:33 +0000454{
455 struct flexcan_regs __iomem *regs = priv->regs;
456 u32 reg_mcr;
457
458 reg_mcr = priv->read(&regs->mcr);
459 reg_mcr |= FLEXCAN_MCR_SLF_WAK;
460 priv->write(reg_mcr, &regs->mcr);
461
462 /* enable stop request */
463 regmap_update_bits(priv->stm.gpr, priv->stm.req_gpr,
464 1 << priv->stm.req_bit, 1 << priv->stm.req_bit);
Joakim Zhang5f186c22019-07-02 01:45:41 +0000465
Joakim Zhang048e3a342019-12-04 11:36:14 +0000466 return flexcan_low_power_enter_ack(priv);
Aisheng Dongde3578c2018-11-23 08:35:33 +0000467}
468
Joakim Zhang5f186c22019-07-02 01:45:41 +0000469static inline int flexcan_exit_stop_mode(struct flexcan_priv *priv)
Aisheng Dongde3578c2018-11-23 08:35:33 +0000470{
471 struct flexcan_regs __iomem *regs = priv->regs;
472 u32 reg_mcr;
473
474 /* remove stop request */
475 regmap_update_bits(priv->stm.gpr, priv->stm.req_gpr,
476 1 << priv->stm.req_bit, 0);
477
478 reg_mcr = priv->read(&regs->mcr);
479 reg_mcr &= ~FLEXCAN_MCR_SLF_WAK;
480 priv->write(reg_mcr, &regs->mcr);
Joakim Zhang5f186c22019-07-02 01:45:41 +0000481
Joakim Zhang048e3a342019-12-04 11:36:14 +0000482 return flexcan_low_power_exit_ack(priv);
Aisheng Dongde3578c2018-11-23 08:35:33 +0000483}
484
ZHU Yi (ST-FIR/ENG1-Zhu)da49a802017-09-15 07:03:58 +0000485static inline void flexcan_error_irq_enable(const struct flexcan_priv *priv)
486{
487 struct flexcan_regs __iomem *regs = priv->regs;
488 u32 reg_ctrl = (priv->reg_ctrl_default | FLEXCAN_CTRL_ERR_MSK);
489
Pankaj Bansal88462d22017-11-24 18:52:08 +0530490 priv->write(reg_ctrl, &regs->ctrl);
ZHU Yi (ST-FIR/ENG1-Zhu)da49a802017-09-15 07:03:58 +0000491}
492
493static inline void flexcan_error_irq_disable(const struct flexcan_priv *priv)
494{
495 struct flexcan_regs __iomem *regs = priv->regs;
496 u32 reg_ctrl = (priv->reg_ctrl_default & ~FLEXCAN_CTRL_ERR_MSK);
497
Pankaj Bansal88462d22017-11-24 18:52:08 +0530498 priv->write(reg_ctrl, &regs->ctrl);
ZHU Yi (ST-FIR/ENG1-Zhu)da49a802017-09-15 07:03:58 +0000499}
500
Aisheng Dongca109892018-11-30 08:53:26 +0000501static int flexcan_clks_enable(const struct flexcan_priv *priv)
502{
503 int err;
504
505 err = clk_prepare_enable(priv->clk_ipg);
506 if (err)
507 return err;
508
509 err = clk_prepare_enable(priv->clk_per);
510 if (err)
511 clk_disable_unprepare(priv->clk_ipg);
512
513 return err;
514}
515
516static void flexcan_clks_disable(const struct flexcan_priv *priv)
517{
518 clk_disable_unprepare(priv->clk_per);
519 clk_disable_unprepare(priv->clk_ipg);
520}
521
Marc Kleine-Buddef0036982014-02-28 17:18:27 +0100522static inline int flexcan_transceiver_enable(const struct flexcan_priv *priv)
523{
524 if (!priv->reg_xceiver)
525 return 0;
526
527 return regulator_enable(priv->reg_xceiver);
528}
529
530static inline int flexcan_transceiver_disable(const struct flexcan_priv *priv)
531{
532 if (!priv->reg_xceiver)
533 return 0;
534
535 return regulator_disable(priv->reg_xceiver);
536}
537
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100538static int flexcan_chip_enable(struct flexcan_priv *priv)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200539{
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200540 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200541 u32 reg;
542
Pankaj Bansal88462d22017-11-24 18:52:08 +0530543 reg = priv->read(&regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200544 reg &= ~FLEXCAN_MCR_MDIS;
Pankaj Bansal88462d22017-11-24 18:52:08 +0530545 priv->write(reg, &regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200546
Joakim Zhangb7603d02019-12-04 11:36:11 +0000547 return flexcan_low_power_exit_ack(priv);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200548}
549
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100550static int flexcan_chip_disable(struct flexcan_priv *priv)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200551{
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200552 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200553 u32 reg;
554
Pankaj Bansal88462d22017-11-24 18:52:08 +0530555 reg = priv->read(&regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200556 reg |= FLEXCAN_MCR_MDIS;
Pankaj Bansal88462d22017-11-24 18:52:08 +0530557 priv->write(reg, &regs->mcr);
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100558
Joakim Zhangb7603d02019-12-04 11:36:11 +0000559 return flexcan_low_power_enter_ack(priv);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200560}
561
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100562static int flexcan_chip_freeze(struct flexcan_priv *priv)
563{
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200564 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100565 unsigned int timeout = 1000 * 1000 * 10 / priv->can.bittiming.bitrate;
566 u32 reg;
567
Pankaj Bansal88462d22017-11-24 18:52:08 +0530568 reg = priv->read(&regs->mcr);
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100569 reg |= FLEXCAN_MCR_HALT;
Pankaj Bansal88462d22017-11-24 18:52:08 +0530570 priv->write(reg, &regs->mcr);
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100571
Pankaj Bansal88462d22017-11-24 18:52:08 +0530572 while (timeout-- && !(priv->read(&regs->mcr) & FLEXCAN_MCR_FRZ_ACK))
David Jander8badd652014-08-27 12:02:16 +0200573 udelay(100);
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100574
Pankaj Bansal88462d22017-11-24 18:52:08 +0530575 if (!(priv->read(&regs->mcr) & FLEXCAN_MCR_FRZ_ACK))
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100576 return -ETIMEDOUT;
577
578 return 0;
579}
580
581static int flexcan_chip_unfreeze(struct flexcan_priv *priv)
582{
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200583 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100584 unsigned int timeout = FLEXCAN_TIMEOUT_US / 10;
585 u32 reg;
586
Pankaj Bansal88462d22017-11-24 18:52:08 +0530587 reg = priv->read(&regs->mcr);
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100588 reg &= ~FLEXCAN_MCR_HALT;
Pankaj Bansal88462d22017-11-24 18:52:08 +0530589 priv->write(reg, &regs->mcr);
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100590
Pankaj Bansal88462d22017-11-24 18:52:08 +0530591 while (timeout-- && (priv->read(&regs->mcr) & FLEXCAN_MCR_FRZ_ACK))
David Jander8badd652014-08-27 12:02:16 +0200592 udelay(10);
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100593
Pankaj Bansal88462d22017-11-24 18:52:08 +0530594 if (priv->read(&regs->mcr) & FLEXCAN_MCR_FRZ_ACK)
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100595 return -ETIMEDOUT;
596
597 return 0;
598}
599
Marc Kleine-Budde4b5b8222014-02-28 15:16:59 +0100600static int flexcan_chip_softreset(struct flexcan_priv *priv)
601{
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200602 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Budde4b5b8222014-02-28 15:16:59 +0100603 unsigned int timeout = FLEXCAN_TIMEOUT_US / 10;
604
Pankaj Bansal88462d22017-11-24 18:52:08 +0530605 priv->write(FLEXCAN_MCR_SOFTRST, &regs->mcr);
606 while (timeout-- && (priv->read(&regs->mcr) & FLEXCAN_MCR_SOFTRST))
David Jander8badd652014-08-27 12:02:16 +0200607 udelay(10);
Marc Kleine-Budde4b5b8222014-02-28 15:16:59 +0100608
Pankaj Bansal88462d22017-11-24 18:52:08 +0530609 if (priv->read(&regs->mcr) & FLEXCAN_MCR_SOFTRST)
Marc Kleine-Budde4b5b8222014-02-28 15:16:59 +0100610 return -ETIMEDOUT;
611
612 return 0;
613}
614
Stefan Agnerec56acf2014-07-15 14:56:20 +0200615static int __flexcan_get_berr_counter(const struct net_device *dev,
616 struct can_berr_counter *bec)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200617{
618 const struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200619 struct flexcan_regs __iomem *regs = priv->regs;
Pankaj Bansal88462d22017-11-24 18:52:08 +0530620 u32 reg = priv->read(&regs->ecr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200621
622 bec->txerr = (reg >> 0) & 0xff;
623 bec->rxerr = (reg >> 8) & 0xff;
624
625 return 0;
626}
627
Stefan Agnerec56acf2014-07-15 14:56:20 +0200628static int flexcan_get_berr_counter(const struct net_device *dev,
629 struct can_berr_counter *bec)
630{
631 const struct flexcan_priv *priv = netdev_priv(dev);
632 int err;
633
Aisheng Dongca109892018-11-30 08:53:26 +0000634 err = pm_runtime_get_sync(priv->dev);
635 if (err < 0)
Stefan Agnerec56acf2014-07-15 14:56:20 +0200636 return err;
637
Stefan Agnerec56acf2014-07-15 14:56:20 +0200638 err = __flexcan_get_berr_counter(dev, bec);
639
Aisheng Dongca109892018-11-30 08:53:26 +0000640 pm_runtime_put(priv->dev);
Stefan Agnerec56acf2014-07-15 14:56:20 +0200641
642 return err;
643}
644
Marc Kleine-Buddefb1e13e62018-04-26 23:13:38 +0200645static netdev_tx_t flexcan_start_xmit(struct sk_buff *skb, struct net_device *dev)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200646{
647 const struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200648 struct can_frame *cf = (struct can_frame *)skb->data;
649 u32 can_id;
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200650 u32 data;
Marc Kleine-Budde10d089b2014-09-23 11:18:11 +0200651 u32 ctrl = FLEXCAN_MB_CODE_TX_DATA | (cf->can_dlc << 16);
Pankaj Bansal05179612018-11-23 22:18:44 +0100652 int i;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200653
654 if (can_dropped_invalid_skb(dev, skb))
655 return NETDEV_TX_OK;
656
657 netif_stop_queue(dev);
658
659 if (cf->can_id & CAN_EFF_FLAG) {
660 can_id = cf->can_id & CAN_EFF_MASK;
661 ctrl |= FLEXCAN_MB_CNT_IDE | FLEXCAN_MB_CNT_SRR;
662 } else {
663 can_id = (cf->can_id & CAN_SFF_MASK) << 18;
664 }
665
666 if (cf->can_id & CAN_RTR_FLAG)
667 ctrl |= FLEXCAN_MB_CNT_RTR;
668
Pankaj Bansal05179612018-11-23 22:18:44 +0100669 for (i = 0; i < cf->can_dlc; i += sizeof(u32)) {
670 data = be32_to_cpup((__be32 *)&cf->data[i]);
671 priv->write(data, &priv->tx_mb->data[i / sizeof(u32)]);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200672 }
673
Reuben Dowle9a123492011-11-01 11:18:03 +1300674 can_put_echo_skb(skb, dev, 0);
675
Pankaj Bansal05179612018-11-23 22:18:44 +0100676 priv->write(can_id, &priv->tx_mb->can_id);
677 priv->write(ctrl, &priv->tx_mb->can_ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200678
David Jander25e92442014-09-03 16:47:22 +0200679 /* Errata ERR005829 step8:
680 * Write twice INACTIVE(0x8) code to first MB.
681 */
Pankaj Bansal88462d22017-11-24 18:52:08 +0530682 priv->write(FLEXCAN_MB_CODE_TX_INACTIVE,
Marc Kleine-Budde9dc1ee12018-11-12 15:33:57 +0100683 &priv->tx_mb_reserved->can_ctrl);
Pankaj Bansal88462d22017-11-24 18:52:08 +0530684 priv->write(FLEXCAN_MB_CODE_TX_INACTIVE,
Marc Kleine-Budde9dc1ee12018-11-12 15:33:57 +0100685 &priv->tx_mb_reserved->can_ctrl);
David Jander25e92442014-09-03 16:47:22 +0200686
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200687 return NETDEV_TX_OK;
688}
689
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200690static void flexcan_irq_bus_err(struct net_device *dev, u32 reg_esr)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200691{
692 struct flexcan_priv *priv = netdev_priv(dev);
Oleksij Rempeld788905f2018-09-18 11:40:41 +0200693 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddea5c02f662017-01-18 11:38:26 +0100694 struct sk_buff *skb;
695 struct can_frame *cf;
Marc Kleine-Budded166f562017-01-17 17:33:46 +0100696 bool rx_errors = false, tx_errors = false;
Oleksij Rempeld788905f2018-09-18 11:40:41 +0200697 u32 timestamp;
Marc Kleine-Budde75812432019-07-15 20:53:08 +0200698 int err;
Oleksij Rempeld788905f2018-09-18 11:40:41 +0200699
700 timestamp = priv->read(&regs->timer) << 16;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200701
Marc Kleine-Buddea5c02f662017-01-18 11:38:26 +0100702 skb = alloc_can_err_skb(dev, &cf);
703 if (unlikely(!skb))
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200704 return;
Marc Kleine-Buddea5c02f662017-01-18 11:38:26 +0100705
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200706 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
707
708 if (reg_esr & FLEXCAN_ESR_BIT1_ERR) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100709 netdev_dbg(dev, "BIT1_ERR irq\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200710 cf->data[2] |= CAN_ERR_PROT_BIT1;
Marc Kleine-Budded166f562017-01-17 17:33:46 +0100711 tx_errors = true;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200712 }
713 if (reg_esr & FLEXCAN_ESR_BIT0_ERR) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100714 netdev_dbg(dev, "BIT0_ERR irq\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200715 cf->data[2] |= CAN_ERR_PROT_BIT0;
Marc Kleine-Budded166f562017-01-17 17:33:46 +0100716 tx_errors = true;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200717 }
718 if (reg_esr & FLEXCAN_ESR_ACK_ERR) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100719 netdev_dbg(dev, "ACK_ERR irq\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200720 cf->can_id |= CAN_ERR_ACK;
Oliver Hartkoppffd461f2015-11-21 18:41:20 +0100721 cf->data[3] = CAN_ERR_PROT_LOC_ACK;
Marc Kleine-Budded166f562017-01-17 17:33:46 +0100722 tx_errors = true;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200723 }
724 if (reg_esr & FLEXCAN_ESR_CRC_ERR) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100725 netdev_dbg(dev, "CRC_ERR irq\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200726 cf->data[2] |= CAN_ERR_PROT_BIT;
Oliver Hartkoppffd461f2015-11-21 18:41:20 +0100727 cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ;
Marc Kleine-Budded166f562017-01-17 17:33:46 +0100728 rx_errors = true;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200729 }
730 if (reg_esr & FLEXCAN_ESR_FRM_ERR) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100731 netdev_dbg(dev, "FRM_ERR irq\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200732 cf->data[2] |= CAN_ERR_PROT_FORM;
Marc Kleine-Budded166f562017-01-17 17:33:46 +0100733 rx_errors = true;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200734 }
735 if (reg_esr & FLEXCAN_ESR_STF_ERR) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100736 netdev_dbg(dev, "STF_ERR irq\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200737 cf->data[2] |= CAN_ERR_PROT_STUFF;
Marc Kleine-Budded166f562017-01-17 17:33:46 +0100738 rx_errors = true;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200739 }
740
741 priv->can.can_stats.bus_error++;
742 if (rx_errors)
743 dev->stats.rx_errors++;
744 if (tx_errors)
745 dev->stats.tx_errors++;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200746
Marc Kleine-Budde75812432019-07-15 20:53:08 +0200747 err = can_rx_offload_queue_sorted(&priv->offload, skb, timestamp);
748 if (err)
749 dev->stats.rx_fifo_errors++;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200750}
751
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200752static void flexcan_irq_state(struct net_device *dev, u32 reg_esr)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200753{
754 struct flexcan_priv *priv = netdev_priv(dev);
Oleksij Rempeld788905f2018-09-18 11:40:41 +0200755 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200756 struct sk_buff *skb;
757 struct can_frame *cf;
Marc Kleine-Budde238443d2017-01-18 11:25:41 +0100758 enum can_state new_state, rx_state, tx_state;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200759 int flt;
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000760 struct can_berr_counter bec;
Oleksij Rempeld788905f2018-09-18 11:40:41 +0200761 u32 timestamp;
Marc Kleine-Budde75812432019-07-15 20:53:08 +0200762 int err;
Oleksij Rempeld788905f2018-09-18 11:40:41 +0200763
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200764 flt = reg_esr & FLEXCAN_ESR_FLT_CONF_MASK;
765 if (likely(flt == FLEXCAN_ESR_FLT_CONF_ACTIVE)) {
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000766 tx_state = unlikely(reg_esr & FLEXCAN_ESR_TX_WRN) ?
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200767 CAN_STATE_ERROR_WARNING : CAN_STATE_ERROR_ACTIVE;
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000768 rx_state = unlikely(reg_esr & FLEXCAN_ESR_RX_WRN) ?
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200769 CAN_STATE_ERROR_WARNING : CAN_STATE_ERROR_ACTIVE;
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000770 new_state = max(tx_state, rx_state);
Andri Yngvason258ce802015-03-17 13:03:09 +0000771 } else {
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000772 __flexcan_get_berr_counter(dev, &bec);
Andri Yngvason258ce802015-03-17 13:03:09 +0000773 new_state = flt == FLEXCAN_ESR_FLT_CONF_PASSIVE ?
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200774 CAN_STATE_ERROR_PASSIVE : CAN_STATE_BUS_OFF;
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000775 rx_state = bec.rxerr >= bec.txerr ? new_state : 0;
776 tx_state = bec.rxerr <= bec.txerr ? new_state : 0;
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000777 }
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200778
779 /* state hasn't changed */
780 if (likely(new_state == priv->can.state))
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200781 return;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200782
Marc Kleine-Budde58ed8e72019-10-09 15:15:37 +0200783 timestamp = priv->read(&regs->timer) << 16;
784
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200785 skb = alloc_can_err_skb(dev, &cf);
786 if (unlikely(!skb))
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200787 return;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200788
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000789 can_change_state(dev, cf, tx_state, rx_state);
790
791 if (unlikely(new_state == CAN_STATE_BUS_OFF))
792 can_bus_off(dev);
793
Marc Kleine-Budde75812432019-07-15 20:53:08 +0200794 err = can_rx_offload_queue_sorted(&priv->offload, skb, timestamp);
795 if (err)
796 dev->stats.rx_fifo_errors++;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200797}
798
Marc Kleine-Budded3a51502019-03-01 15:38:05 +0100799static inline u64 flexcan_read64_mask(struct flexcan_priv *priv, void __iomem *addr, u64 mask)
800{
801 u64 reg = 0;
802
803 if (upper_32_bits(mask))
804 reg = (u64)priv->read(addr - 4) << 32;
805 if (lower_32_bits(mask))
806 reg |= priv->read(addr);
807
808 return reg & mask;
809}
810
Marc Kleine-Buddeb87c28b72019-03-01 15:38:05 +0100811static inline void flexcan_write64(struct flexcan_priv *priv, u64 val, void __iomem *addr)
812{
813 if (upper_32_bits(val))
814 priv->write(upper_32_bits(val), addr - 4);
815 if (lower_32_bits(val))
816 priv->write(lower_32_bits(val), addr);
817}
818
Marc Kleine-Budded3a51502019-03-01 15:38:05 +0100819static inline u64 flexcan_read_reg_iflag_rx(struct flexcan_priv *priv)
820{
821 return flexcan_read64_mask(priv, &priv->regs->iflag1, priv->rx_mask);
822}
823
Marc Kleine-Buddeb87c28b72019-03-01 15:38:05 +0100824static inline u64 flexcan_read_reg_iflag_tx(struct flexcan_priv *priv)
825{
826 return flexcan_read64_mask(priv, &priv->regs->iflag1, priv->tx_mask);
827}
828
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200829static inline struct flexcan_priv *rx_offload_to_priv(struct can_rx_offload *offload)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200830{
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200831 return container_of(offload, struct flexcan_priv, offload);
832}
833
Joakim Zhang4e9c9482019-07-12 08:02:38 +0000834static struct sk_buff *flexcan_mailbox_read(struct can_rx_offload *offload,
835 unsigned int n, u32 *timestamp,
836 bool drop)
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200837{
838 struct flexcan_priv *priv = rx_offload_to_priv(offload);
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200839 struct flexcan_regs __iomem *regs = priv->regs;
Pankaj Bansal05179612018-11-23 22:18:44 +0100840 struct flexcan_mb __iomem *mb;
Joakim Zhang4e9c9482019-07-12 08:02:38 +0000841 struct sk_buff *skb;
842 struct can_frame *cf;
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200843 u32 reg_ctrl, reg_id, reg_iflag1;
Pankaj Bansal05179612018-11-23 22:18:44 +0100844 int i;
845
Joakim Zhang4e9c9482019-07-12 08:02:38 +0000846 if (unlikely(drop)) {
847 skb = ERR_PTR(-ENOBUFS);
848 goto mark_as_read;
849 }
850
Pankaj Bansal05179612018-11-23 22:18:44 +0100851 mb = flexcan_get_mb(priv, n);
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200852
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200853 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP) {
854 u32 code;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200855
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200856 do {
Pankaj Bansal88462d22017-11-24 18:52:08 +0530857 reg_ctrl = priv->read(&mb->can_ctrl);
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200858 } while (reg_ctrl & FLEXCAN_MB_CODE_RX_BUSY_BIT);
859
860 /* is this MB empty? */
861 code = reg_ctrl & FLEXCAN_MB_CODE_MASK;
862 if ((code != FLEXCAN_MB_CODE_RX_FULL) &&
863 (code != FLEXCAN_MB_CODE_RX_OVERRUN))
Joakim Zhang4e9c9482019-07-12 08:02:38 +0000864 return NULL;
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200865
866 if (code == FLEXCAN_MB_CODE_RX_OVERRUN) {
867 /* This MB was overrun, we lost data */
868 offload->dev->stats.rx_over_errors++;
869 offload->dev->stats.rx_errors++;
870 }
871 } else {
Pankaj Bansal88462d22017-11-24 18:52:08 +0530872 reg_iflag1 = priv->read(&regs->iflag1);
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200873 if (!(reg_iflag1 & FLEXCAN_IFLAG_RX_FIFO_AVAILABLE))
Joakim Zhang4e9c9482019-07-12 08:02:38 +0000874 return NULL;
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200875
Pankaj Bansal88462d22017-11-24 18:52:08 +0530876 reg_ctrl = priv->read(&mb->can_ctrl);
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200877 }
878
Joakim Zhang4e9c9482019-07-12 08:02:38 +0000879 skb = alloc_can_skb(offload->dev, &cf);
880 if (!skb) {
881 skb = ERR_PTR(-ENOMEM);
882 goto mark_as_read;
883 }
884
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200885 /* increase timstamp to full 32 bit */
886 *timestamp = reg_ctrl << 16;
887
Pankaj Bansal88462d22017-11-24 18:52:08 +0530888 reg_id = priv->read(&mb->can_id);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200889 if (reg_ctrl & FLEXCAN_MB_CNT_IDE)
890 cf->can_id = ((reg_id >> 0) & CAN_EFF_MASK) | CAN_EFF_FLAG;
891 else
892 cf->can_id = (reg_id >> 18) & CAN_SFF_MASK;
893
894 if (reg_ctrl & FLEXCAN_MB_CNT_RTR)
895 cf->can_id |= CAN_RTR_FLAG;
896 cf->can_dlc = get_can_dlc((reg_ctrl >> 16) & 0xf);
897
Pankaj Bansal05179612018-11-23 22:18:44 +0100898 for (i = 0; i < cf->can_dlc; i += sizeof(u32)) {
899 __be32 data = cpu_to_be32(priv->read(&mb->data[i / sizeof(u32)]));
900 *(__be32 *)(cf->data + i) = data;
901 }
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200902
Joakim Zhang4e9c9482019-07-12 08:02:38 +0000903 mark_as_read:
Marc Kleine-Buddeb9468ad2019-03-01 16:27:59 +0100904 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP)
905 flexcan_write64(priv, FLEXCAN_IFLAG_MB(n), &regs->iflag1);
906 else
Pankaj Bansal88462d22017-11-24 18:52:08 +0530907 priv->write(FLEXCAN_IFLAG_RX_FIFO_AVAILABLE, &regs->iflag1);
Fabio Baltieriadccadb2012-12-18 18:50:58 +0100908
Pankaj Bansal5178b7c2018-08-01 19:36:46 +0530909 /* Read the Free Running Timer. It is optional but recommended
910 * to unlock Mailbox as soon as possible and make it available
911 * for reception.
912 */
913 priv->read(&regs->timer);
914
Joakim Zhang4e9c9482019-07-12 08:02:38 +0000915 return skb;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200916}
917
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200918static irqreturn_t flexcan_irq(int irq, void *dev_id)
919{
920 struct net_device *dev = dev_id;
921 struct net_device_stats *stats = &dev->stats;
922 struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200923 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddedd2f1222017-01-18 11:45:14 +0100924 irqreturn_t handled = IRQ_NONE;
Marc Kleine-Budde0ca64f022019-03-01 13:54:19 +0100925 u64 reg_iflag_tx;
926 u32 reg_esr;
ZHU Yi (ST-FIR/ENG1-Zhu)da49a802017-09-15 07:03:58 +0000927 enum can_state last_state = priv->can.state;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200928
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200929 /* reception interrupt */
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200930 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP) {
Marc Kleine-Budde4e265982019-03-01 16:29:47 +0100931 u64 reg_iflag_rx;
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200932 int ret;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200933
Marc Kleine-Budde4e265982019-03-01 16:29:47 +0100934 while ((reg_iflag_rx = flexcan_read_reg_iflag_rx(priv))) {
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200935 handled = IRQ_HANDLED;
936 ret = can_rx_offload_irq_offload_timestamp(&priv->offload,
Marc Kleine-Budde4e265982019-03-01 16:29:47 +0100937 reg_iflag_rx);
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200938 if (!ret)
939 break;
940 }
941 } else {
Alexander Steincbffaf72018-10-11 17:01:25 +0200942 u32 reg_iflag1;
943
944 reg_iflag1 = priv->read(&regs->iflag1);
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200945 if (reg_iflag1 & FLEXCAN_IFLAG_RX_FIFO_AVAILABLE) {
946 handled = IRQ_HANDLED;
947 can_rx_offload_irq_offload_fifo(&priv->offload);
948 }
949
950 /* FIFO overflow interrupt */
951 if (reg_iflag1 & FLEXCAN_IFLAG_RX_FIFO_OVERFLOW) {
952 handled = IRQ_HANDLED;
Pankaj Bansal88462d22017-11-24 18:52:08 +0530953 priv->write(FLEXCAN_IFLAG_RX_FIFO_OVERFLOW,
954 &regs->iflag1);
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200955 dev->stats.rx_over_errors++;
956 dev->stats.rx_errors++;
957 }
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200958 }
959
Marc Kleine-Buddeb87c28b72019-03-01 15:38:05 +0100960 reg_iflag_tx = flexcan_read_reg_iflag_tx(priv);
Alexander Steincbffaf72018-10-11 17:01:25 +0200961
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200962 /* transmission complete interrupt */
Marc Kleine-Budde0ca64f022019-03-01 13:54:19 +0100963 if (reg_iflag_tx & priv->tx_mask) {
Pankaj Bansal05179612018-11-23 22:18:44 +0100964 u32 reg_ctrl = priv->read(&priv->tx_mb->can_ctrl);
Oleksij Rempeled72bc82018-09-18 11:40:39 +0200965
Marc Kleine-Buddedd2f1222017-01-18 11:45:14 +0100966 handled = IRQ_HANDLED;
Oleksij Rempeled72bc82018-09-18 11:40:39 +0200967 stats->tx_bytes += can_rx_offload_get_echo_skb(&priv->offload,
968 0, reg_ctrl << 16);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200969 stats->tx_packets++;
Fabio Baltieriadccadb2012-12-18 18:50:58 +0100970 can_led_event(dev, CAN_LED_EVENT_TX);
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200971
972 /* after sending a RTR frame MB is in RX mode */
Pankaj Bansal88462d22017-11-24 18:52:08 +0530973 priv->write(FLEXCAN_MB_CODE_TX_INACTIVE,
Pankaj Bansal05179612018-11-23 22:18:44 +0100974 &priv->tx_mb->can_ctrl);
Marc Kleine-Buddeb87c28b72019-03-01 15:38:05 +0100975 flexcan_write64(priv, priv->tx_mask, &regs->iflag1);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200976 netif_wake_queue(dev);
977 }
978
Pankaj Bansal88462d22017-11-24 18:52:08 +0530979 reg_esr = priv->read(&regs->esr);
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200980
Joakim Zhangab605232019-12-04 11:36:08 +0000981 /* ACK all bus error, state change and wake IRQ sources */
982 if (reg_esr & (FLEXCAN_ESR_ALL_INT | FLEXCAN_ESR_WAK_INT)) {
Marc Kleine-Buddedd2f1222017-01-18 11:45:14 +0100983 handled = IRQ_HANDLED;
Joakim Zhangab605232019-12-04 11:36:08 +0000984 priv->write(reg_esr & (FLEXCAN_ESR_ALL_INT | FLEXCAN_ESR_WAK_INT), &regs->esr);
Marc Kleine-Buddedd2f1222017-01-18 11:45:14 +0100985 }
986
ZHU Yi (ST-FIR/ENG1-Zhu)ad230232017-09-15 06:59:15 +0000987 /* state change interrupt or broken error state quirk fix is enabled */
988 if ((reg_esr & FLEXCAN_ESR_ERR_STATE) ||
ZHU Yi (ST-FIR/ENG1-Zhu)da49a802017-09-15 07:03:58 +0000989 (priv->devtype_data->quirks & (FLEXCAN_QUIRK_BROKEN_WERR_STATE |
Marc Kleine-Buddebc8ad652018-11-28 15:45:27 +0100990 FLEXCAN_QUIRK_BROKEN_PERR_STATE)))
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200991 flexcan_irq_state(dev, reg_esr);
992
993 /* bus error IRQ - handle if bus error reporting is activated */
994 if ((reg_esr & FLEXCAN_ESR_ERR_BUS) &&
995 (priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING))
996 flexcan_irq_bus_err(dev, reg_esr);
997
ZHU Yi (ST-FIR/ENG1-Zhu)da49a802017-09-15 07:03:58 +0000998 /* availability of error interrupt among state transitions in case
999 * bus error reporting is de-activated and
1000 * FLEXCAN_QUIRK_BROKEN_PERR_STATE is enabled:
1001 * +--------------------------------------------------------------+
1002 * | +----------------------------------------------+ [stopped / |
1003 * | | | sleeping] -+
1004 * +-+-> active <-> warning <-> passive -> bus off -+
1005 * ___________^^^^^^^^^^^^_______________________________
1006 * disabled(1) enabled disabled
1007 *
1008 * (1): enabled if FLEXCAN_QUIRK_BROKEN_WERR_STATE is enabled
1009 */
1010 if ((last_state != priv->can.state) &&
1011 (priv->devtype_data->quirks & FLEXCAN_QUIRK_BROKEN_PERR_STATE) &&
1012 !(priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING)) {
1013 switch (priv->can.state) {
1014 case CAN_STATE_ERROR_ACTIVE:
1015 if (priv->devtype_data->quirks &
1016 FLEXCAN_QUIRK_BROKEN_WERR_STATE)
1017 flexcan_error_irq_enable(priv);
1018 else
1019 flexcan_error_irq_disable(priv);
1020 break;
1021
1022 case CAN_STATE_ERROR_WARNING:
1023 flexcan_error_irq_enable(priv);
1024 break;
1025
1026 case CAN_STATE_ERROR_PASSIVE:
1027 case CAN_STATE_BUS_OFF:
1028 flexcan_error_irq_disable(priv);
1029 break;
1030
1031 default:
1032 break;
1033 }
1034 }
1035
Marc Kleine-Buddedd2f1222017-01-18 11:45:14 +01001036 return handled;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001037}
1038
1039static void flexcan_set_bittiming(struct net_device *dev)
1040{
1041 const struct flexcan_priv *priv = netdev_priv(dev);
1042 const struct can_bittiming *bt = &priv->can.bittiming;
Marc Kleine-Budde89af8742015-05-08 09:32:58 +02001043 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001044 u32 reg;
1045
Pankaj Bansal88462d22017-11-24 18:52:08 +05301046 reg = priv->read(&regs->ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001047 reg &= ~(FLEXCAN_CTRL_PRESDIV(0xff) |
1048 FLEXCAN_CTRL_RJW(0x3) |
1049 FLEXCAN_CTRL_PSEG1(0x7) |
1050 FLEXCAN_CTRL_PSEG2(0x7) |
1051 FLEXCAN_CTRL_PROPSEG(0x7) |
1052 FLEXCAN_CTRL_LPB |
1053 FLEXCAN_CTRL_SMP |
1054 FLEXCAN_CTRL_LOM);
1055
1056 reg |= FLEXCAN_CTRL_PRESDIV(bt->brp - 1) |
1057 FLEXCAN_CTRL_PSEG1(bt->phase_seg1 - 1) |
1058 FLEXCAN_CTRL_PSEG2(bt->phase_seg2 - 1) |
1059 FLEXCAN_CTRL_RJW(bt->sjw - 1) |
1060 FLEXCAN_CTRL_PROPSEG(bt->prop_seg - 1);
1061
1062 if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)
1063 reg |= FLEXCAN_CTRL_LPB;
1064 if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
1065 reg |= FLEXCAN_CTRL_LOM;
1066 if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
1067 reg |= FLEXCAN_CTRL_SMP;
1068
Lucas Stach7a4b6c82015-08-07 17:16:03 +02001069 netdev_dbg(dev, "writing ctrl=0x%08x\n", reg);
Pankaj Bansal88462d22017-11-24 18:52:08 +05301070 priv->write(reg, &regs->ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001071
1072 /* print chip status */
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +01001073 netdev_dbg(dev, "%s: mcr=0x%08x ctrl=0x%08x\n", __func__,
Pankaj Bansal88462d22017-11-24 18:52:08 +05301074 priv->read(&regs->mcr), priv->read(&regs->ctrl));
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001075}
1076
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +02001077/* flexcan_chip_start
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001078 *
1079 * this functions is entered with clocks enabled
1080 *
1081 */
1082static int flexcan_chip_start(struct net_device *dev)
1083{
1084 struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Budde89af8742015-05-08 09:32:58 +02001085 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Budde6f75fce2014-09-23 11:03:01 +02001086 u32 reg_mcr, reg_ctrl, reg_ctrl2, reg_mecr;
Marc Kleine-Budde8ce51392019-03-01 12:17:30 +01001087 u64 reg_imask;
David S. Miller1f6d8032014-09-23 12:09:27 -04001088 int err, i;
Pankaj Bansal05179612018-11-23 22:18:44 +01001089 struct flexcan_mb __iomem *mb;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001090
1091 /* enable module */
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001092 err = flexcan_chip_enable(priv);
1093 if (err)
1094 return err;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001095
1096 /* soft reset */
Marc Kleine-Budde4b5b8222014-02-28 15:16:59 +01001097 err = flexcan_chip_softreset(priv);
1098 if (err)
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +01001099 goto out_chip_disable;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001100
1101 flexcan_set_bittiming(dev);
1102
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +02001103 /* MCR
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001104 *
1105 * enable freeze
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001106 * halt now
1107 * only supervisor access
1108 * enable warning int
Marc Kleine-Budde4bd888a2015-08-31 21:03:29 +02001109 * enable individual RX masking
Marc Kleine-Budde749de6f2015-08-31 21:32:34 +02001110 * choose format C
1111 * set max mailbox number
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001112 */
Pankaj Bansal88462d22017-11-24 18:52:08 +05301113 reg_mcr = priv->read(&regs->mcr);
Marc Kleine-Budded5a7b402013-10-04 10:52:36 +02001114 reg_mcr &= ~FLEXCAN_MCR_MAXMB(0xff);
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +02001115 reg_mcr |= FLEXCAN_MCR_FRZ | FLEXCAN_MCR_HALT | FLEXCAN_MCR_SUPV |
Pankaj Bansal7ad0f532018-08-13 23:50:48 +05301116 FLEXCAN_MCR_WRN_EN | FLEXCAN_MCR_IRMQ | FLEXCAN_MCR_IDAM_C |
Pankaj Bansal05179612018-11-23 22:18:44 +01001117 FLEXCAN_MCR_MAXMB(priv->tx_mb_idx);
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +02001118
Marc Kleine-Buddec982a3ca2018-08-17 14:52:58 +02001119 /* MCR
1120 *
1121 * FIFO:
1122 * - disable for timestamp mode
1123 * - enable for FIFO mode
1124 */
Alexander Steincbffaf72018-10-11 17:01:25 +02001125 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP)
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +02001126 reg_mcr &= ~FLEXCAN_MCR_FEN;
Alexander Steincbffaf72018-10-11 17:01:25 +02001127 else
1128 reg_mcr |= FLEXCAN_MCR_FEN;
1129
Pankaj Bansal7ad0f532018-08-13 23:50:48 +05301130 /* MCR
1131 *
1132 * NOTE: In loopback mode, the CAN_MCR[SRXDIS] cannot be
1133 * asserted because this will impede the self reception
1134 * of a transmitted message. This is not documented in
1135 * earlier versions of flexcan block guide.
1136 *
1137 * Self Reception:
1138 * - enable Self Reception for loopback mode
1139 * (by clearing "Self Reception Disable" bit)
1140 * - disable for normal operation
1141 */
1142 if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)
1143 reg_mcr &= ~FLEXCAN_MCR_SRX_DIS;
1144 else
1145 reg_mcr |= FLEXCAN_MCR_SRX_DIS;
1146
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +01001147 netdev_dbg(dev, "%s: writing mcr=0x%08x", __func__, reg_mcr);
Pankaj Bansal88462d22017-11-24 18:52:08 +05301148 priv->write(reg_mcr, &regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001149
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +02001150 /* CTRL
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001151 *
1152 * disable timer sync feature
1153 *
1154 * disable auto busoff recovery
1155 * transmit lowest buffer first
1156 *
1157 * enable tx and rx warning interrupt
1158 * enable bus off interrupt
1159 * (== FLEXCAN_CTRL_ERR_STATE)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001160 */
Pankaj Bansal88462d22017-11-24 18:52:08 +05301161 reg_ctrl = priv->read(&regs->ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001162 reg_ctrl &= ~FLEXCAN_CTRL_TSYN;
1163 reg_ctrl |= FLEXCAN_CTRL_BOFF_REC | FLEXCAN_CTRL_LBUF |
Wolfgang Grandegger4f72e5f2012-09-28 03:17:15 +00001164 FLEXCAN_CTRL_ERR_STATE;
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +02001165
1166 /* enable the "error interrupt" (FLEXCAN_CTRL_ERR_MSK),
Wolfgang Grandegger4f72e5f2012-09-28 03:17:15 +00001167 * on most Flexcan cores, too. Otherwise we don't get
1168 * any error warning or passive interrupts.
1169 */
ZHU Yi (ST-FIR/ENG1-Zhu)2f8639b2017-09-15 07:01:23 +00001170 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_BROKEN_WERR_STATE ||
Wolfgang Grandegger4f72e5f2012-09-28 03:17:15 +00001171 priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING)
1172 reg_ctrl |= FLEXCAN_CTRL_ERR_MSK;
Alexander Steinbc03a542014-08-12 10:47:21 +02001173 else
1174 reg_ctrl &= ~FLEXCAN_CTRL_ERR_MSK;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001175
1176 /* save for later use */
1177 priv->reg_ctrl_default = reg_ctrl;
Marc Kleine-Budde6fa7da22015-08-27 14:24:48 +02001178 /* leave interrupts disabled for now */
1179 reg_ctrl &= ~FLEXCAN_CTRL_ERR_ALL;
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +01001180 netdev_dbg(dev, "%s: writing ctrl=0x%08x", __func__, reg_ctrl);
Pankaj Bansal88462d22017-11-24 18:52:08 +05301181 priv->write(reg_ctrl, &regs->ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001182
Marc Kleine-Budde9eb7aa82015-09-01 08:57:55 +02001183 if ((priv->devtype_data->quirks & FLEXCAN_QUIRK_ENABLE_EACEN_RRS)) {
Pankaj Bansal88462d22017-11-24 18:52:08 +05301184 reg_ctrl2 = priv->read(&regs->ctrl2);
Marc Kleine-Budde9eb7aa82015-09-01 08:57:55 +02001185 reg_ctrl2 |= FLEXCAN_CTRL2_EACEN | FLEXCAN_CTRL2_RRS;
Pankaj Bansal88462d22017-11-24 18:52:08 +05301186 priv->write(reg_ctrl2, &regs->ctrl2);
Marc Kleine-Budde9eb7aa82015-09-01 08:57:55 +02001187 }
1188
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +02001189 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP) {
Alexander Steincbffaf72018-10-11 17:01:25 +02001190 for (i = priv->offload.mb_first; i <= priv->offload.mb_last; i++) {
Pankaj Bansal05179612018-11-23 22:18:44 +01001191 mb = flexcan_get_mb(priv, i);
Pankaj Bansal88462d22017-11-24 18:52:08 +05301192 priv->write(FLEXCAN_MB_CODE_RX_EMPTY,
Pankaj Bansal05179612018-11-23 22:18:44 +01001193 &mb->can_ctrl);
Alexander Steincbffaf72018-10-11 17:01:25 +02001194 }
1195 } else {
1196 /* clear and invalidate unused mailboxes first */
Uwe Kleine-Königa55234d2019-01-11 12:20:41 +01001197 for (i = FLEXCAN_TX_MB_RESERVED_OFF_FIFO; i < priv->mb_count; i++) {
Pankaj Bansal05179612018-11-23 22:18:44 +01001198 mb = flexcan_get_mb(priv, i);
Alexander Steincbffaf72018-10-11 17:01:25 +02001199 priv->write(FLEXCAN_MB_CODE_RX_INACTIVE,
Pankaj Bansal05179612018-11-23 22:18:44 +01001200 &mb->can_ctrl);
Alexander Steincbffaf72018-10-11 17:01:25 +02001201 }
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +02001202 }
1203
David Jander25e92442014-09-03 16:47:22 +02001204 /* Errata ERR005829: mark first TX mailbox as INACTIVE */
Pankaj Bansal88462d22017-11-24 18:52:08 +05301205 priv->write(FLEXCAN_MB_CODE_TX_INACTIVE,
1206 &priv->tx_mb_reserved->can_ctrl);
David Jander25e92442014-09-03 16:47:22 +02001207
Marc Kleine-Buddec32fe4a2014-09-16 12:39:28 +02001208 /* mark TX mailbox as INACTIVE */
Pankaj Bansal88462d22017-11-24 18:52:08 +05301209 priv->write(FLEXCAN_MB_CODE_TX_INACTIVE,
Pankaj Bansal05179612018-11-23 22:18:44 +01001210 &priv->tx_mb->can_ctrl);
Marc Kleine-Budded5a7b402013-10-04 10:52:36 +02001211
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001212 /* acceptance mask/acceptance code (accept everything) */
Pankaj Bansal88462d22017-11-24 18:52:08 +05301213 priv->write(0x0, &regs->rxgmask);
1214 priv->write(0x0, &regs->rx14mask);
1215 priv->write(0x0, &regs->rx15mask);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001216
Marc Kleine-Buddef377bff2015-05-08 15:22:36 +02001217 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_DISABLE_RXFG)
Pankaj Bansal88462d22017-11-24 18:52:08 +05301218 priv->write(0x0, &regs->rxfgmask);
Hui Wang30c1e672012-06-28 16:21:35 +08001219
Marc Kleine-Budde4bd888a2015-08-31 21:03:29 +02001220 /* clear acceptance filters */
Pankaj Bansal05179612018-11-23 22:18:44 +01001221 for (i = 0; i < priv->mb_count; i++)
Pankaj Bansal88462d22017-11-24 18:52:08 +05301222 priv->write(0, &regs->rximr[i]);
Marc Kleine-Budde4bd888a2015-08-31 21:03:29 +02001223
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +02001224 /* On Vybrid, disable memory error detection interrupts
Stefan Agnercdce8442014-07-15 14:56:21 +02001225 * and freeze mode.
1226 * This also works around errata e5295 which generates
1227 * false positive memory errors and put the device in
1228 * freeze mode.
1229 */
Marc Kleine-Buddef377bff2015-05-08 15:22:36 +02001230 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_DISABLE_MECR) {
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +02001231 /* Follow the protocol as described in "Detection
Stefan Agnercdce8442014-07-15 14:56:21 +02001232 * and Correction of Memory Errors" to write to
1233 * MECR register
1234 */
Pankaj Bansal88462d22017-11-24 18:52:08 +05301235 reg_ctrl2 = priv->read(&regs->ctrl2);
Marc Kleine-Budde6f75fce2014-09-23 11:03:01 +02001236 reg_ctrl2 |= FLEXCAN_CTRL2_ECRWRE;
Pankaj Bansal88462d22017-11-24 18:52:08 +05301237 priv->write(reg_ctrl2, &regs->ctrl2);
Stefan Agnercdce8442014-07-15 14:56:21 +02001238
Pankaj Bansal88462d22017-11-24 18:52:08 +05301239 reg_mecr = priv->read(&regs->mecr);
Stefan Agnercdce8442014-07-15 14:56:21 +02001240 reg_mecr &= ~FLEXCAN_MECR_ECRWRDIS;
Pankaj Bansal88462d22017-11-24 18:52:08 +05301241 priv->write(reg_mecr, &regs->mecr);
Joakim Zhang5e269322019-08-15 08:00:26 +00001242 reg_mecr |= FLEXCAN_MECR_ECCDIS;
Stefan Agnercdce8442014-07-15 14:56:21 +02001243 reg_mecr &= ~(FLEXCAN_MECR_NCEFAFRZ | FLEXCAN_MECR_HANCEI_MSK |
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +02001244 FLEXCAN_MECR_FANCEI_MSK);
Pankaj Bansal88462d22017-11-24 18:52:08 +05301245 priv->write(reg_mecr, &regs->mecr);
Stefan Agnercdce8442014-07-15 14:56:21 +02001246 }
1247
Marc Kleine-Buddef0036982014-02-28 17:18:27 +01001248 err = flexcan_transceiver_enable(priv);
1249 if (err)
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +01001250 goto out_chip_disable;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001251
1252 /* synchronize with the can bus */
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +01001253 err = flexcan_chip_unfreeze(priv);
1254 if (err)
1255 goto out_transceiver_disable;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001256
1257 priv->can.state = CAN_STATE_ERROR_ACTIVE;
1258
Marc Kleine-Budde6fa7da22015-08-27 14:24:48 +02001259 /* enable interrupts atomically */
1260 disable_irq(dev->irq);
Pankaj Bansal88462d22017-11-24 18:52:08 +05301261 priv->write(priv->reg_ctrl_default, &regs->ctrl);
Marc Kleine-Budde0ca64f022019-03-01 13:54:19 +01001262 reg_imask = priv->rx_mask | priv->tx_mask;
Marc Kleine-Budde8ce51392019-03-01 12:17:30 +01001263 priv->write(upper_32_bits(reg_imask), &regs->imask2);
1264 priv->write(lower_32_bits(reg_imask), &regs->imask1);
Marc Kleine-Budde6fa7da22015-08-27 14:24:48 +02001265 enable_irq(dev->irq);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001266
1267 /* print chip status */
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +01001268 netdev_dbg(dev, "%s: reading mcr=0x%08x ctrl=0x%08x\n", __func__,
Pankaj Bansal88462d22017-11-24 18:52:08 +05301269 priv->read(&regs->mcr), priv->read(&regs->ctrl));
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001270
1271 return 0;
1272
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +01001273 out_transceiver_disable:
1274 flexcan_transceiver_disable(priv);
1275 out_chip_disable:
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001276 flexcan_chip_disable(priv);
1277 return err;
1278}
1279
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +02001280/* flexcan_chip_stop
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001281 *
1282 * this functions is entered with clocks enabled
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001283 */
1284static void flexcan_chip_stop(struct net_device *dev)
1285{
1286 struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Budde89af8742015-05-08 09:32:58 +02001287 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001288
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +01001289 /* freeze + disable module */
1290 flexcan_chip_freeze(priv);
1291 flexcan_chip_disable(priv);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001292
Marc Kleine-Budde5be93bd2014-02-19 12:00:51 +01001293 /* Disable all interrupts */
Pankaj Bansal88462d22017-11-24 18:52:08 +05301294 priv->write(0, &regs->imask2);
1295 priv->write(0, &regs->imask1);
1296 priv->write(priv->reg_ctrl_default & ~FLEXCAN_CTRL_ERR_ALL,
1297 &regs->ctrl);
Marc Kleine-Budde5be93bd2014-02-19 12:00:51 +01001298
Marc Kleine-Buddef0036982014-02-28 17:18:27 +01001299 flexcan_transceiver_disable(priv);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001300 priv->can.state = CAN_STATE_STOPPED;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001301}
1302
1303static int flexcan_open(struct net_device *dev)
1304{
1305 struct flexcan_priv *priv = netdev_priv(dev);
1306 int err;
1307
Aisheng Dongca109892018-11-30 08:53:26 +00001308 err = pm_runtime_get_sync(priv->dev);
1309 if (err < 0)
Fabio Estevamaa101812013-07-22 12:41:40 -03001310 return err;
1311
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001312 err = open_candev(dev);
1313 if (err)
Aisheng Dongca109892018-11-30 08:53:26 +00001314 goto out_runtime_put;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001315
1316 err = request_irq(dev->irq, flexcan_irq, IRQF_SHARED, dev->name, dev);
1317 if (err)
1318 goto out_close;
1319
Pankaj Bansal05179612018-11-23 22:18:44 +01001320 priv->mb_size = sizeof(struct flexcan_mb) + CAN_MAX_DLEN;
Pankaj Bansal6cbf7602018-08-28 23:19:12 +05301321 priv->mb_count = (sizeof(priv->regs->mb[0]) / priv->mb_size) +
1322 (sizeof(priv->regs->mb[1]) / priv->mb_size);
Pankaj Bansal05179612018-11-23 22:18:44 +01001323
Pankaj Bansal5156c7b2018-08-13 23:50:49 +05301324 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP)
Pankaj Bansal05179612018-11-23 22:18:44 +01001325 priv->tx_mb_reserved =
1326 flexcan_get_mb(priv, FLEXCAN_TX_MB_RESERVED_OFF_TIMESTAMP);
Pankaj Bansal5156c7b2018-08-13 23:50:49 +05301327 else
Pankaj Bansal05179612018-11-23 22:18:44 +01001328 priv->tx_mb_reserved =
1329 flexcan_get_mb(priv, FLEXCAN_TX_MB_RESERVED_OFF_FIFO);
1330 priv->tx_mb_idx = priv->mb_count - 1;
1331 priv->tx_mb = flexcan_get_mb(priv, priv->tx_mb_idx);
Marc Kleine-Budde0ca64f022019-03-01 13:54:19 +01001332 priv->tx_mask = FLEXCAN_IFLAG_MB(priv->tx_mb_idx);
Pankaj Bansal5156c7b2018-08-13 23:50:49 +05301333
Pankaj Bansal5156c7b2018-08-13 23:50:49 +05301334 priv->offload.mailbox_read = flexcan_mailbox_read;
1335
1336 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP) {
Pankaj Bansal5156c7b2018-08-13 23:50:49 +05301337 priv->offload.mb_first = FLEXCAN_RX_MB_OFF_TIMESTAMP_FIRST;
Pankaj Bansal05179612018-11-23 22:18:44 +01001338 priv->offload.mb_last = priv->mb_count - 2;
Pankaj Bansal5156c7b2018-08-13 23:50:49 +05301339
Marc Kleine-Budde8ce51392019-03-01 12:17:30 +01001340 priv->rx_mask = GENMASK_ULL(priv->offload.mb_last,
1341 priv->offload.mb_first);
Pankaj Bansal5156c7b2018-08-13 23:50:49 +05301342 err = can_rx_offload_add_timestamp(dev, &priv->offload);
1343 } else {
Marc Kleine-Budde8ce51392019-03-01 12:17:30 +01001344 priv->rx_mask = FLEXCAN_IFLAG_RX_FIFO_OVERFLOW |
Pankaj Bansal5156c7b2018-08-13 23:50:49 +05301345 FLEXCAN_IFLAG_RX_FIFO_AVAILABLE;
1346 err = can_rx_offload_add_fifo(dev, &priv->offload,
1347 FLEXCAN_NAPI_WEIGHT);
1348 }
1349 if (err)
1350 goto out_free_irq;
1351
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001352 /* start chip and queuing */
1353 err = flexcan_chip_start(dev);
1354 if (err)
Pankaj Bansal5156c7b2018-08-13 23:50:49 +05301355 goto out_offload_del;
Fabio Baltieriadccadb2012-12-18 18:50:58 +01001356
1357 can_led_event(dev, CAN_LED_EVENT_OPEN);
1358
Marc Kleine-Budde30164752015-05-10 15:26:58 +02001359 can_rx_offload_enable(&priv->offload);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001360 netif_start_queue(dev);
1361
1362 return 0;
1363
Pankaj Bansal5156c7b2018-08-13 23:50:49 +05301364 out_offload_del:
1365 can_rx_offload_del(&priv->offload);
Marc Kleine-Budde7e9e1482014-02-28 14:52:01 +01001366 out_free_irq:
1367 free_irq(dev->irq, dev);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001368 out_close:
1369 close_candev(dev);
Aisheng Dongca109892018-11-30 08:53:26 +00001370 out_runtime_put:
1371 pm_runtime_put(priv->dev);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001372
1373 return err;
1374}
1375
1376static int flexcan_close(struct net_device *dev)
1377{
1378 struct flexcan_priv *priv = netdev_priv(dev);
1379
1380 netif_stop_queue(dev);
Marc Kleine-Budde30164752015-05-10 15:26:58 +02001381 can_rx_offload_disable(&priv->offload);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001382 flexcan_chip_stop(dev);
1383
Pankaj Bansal5156c7b2018-08-13 23:50:49 +05301384 can_rx_offload_del(&priv->offload);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001385 free_irq(dev->irq, dev);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001386
1387 close_candev(dev);
Aisheng Dongca109892018-11-30 08:53:26 +00001388 pm_runtime_put(priv->dev);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001389
Fabio Baltieriadccadb2012-12-18 18:50:58 +01001390 can_led_event(dev, CAN_LED_EVENT_STOP);
1391
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001392 return 0;
1393}
1394
1395static int flexcan_set_mode(struct net_device *dev, enum can_mode mode)
1396{
1397 int err;
1398
1399 switch (mode) {
1400 case CAN_MODE_START:
1401 err = flexcan_chip_start(dev);
1402 if (err)
1403 return err;
1404
1405 netif_wake_queue(dev);
1406 break;
1407
1408 default:
1409 return -EOPNOTSUPP;
1410 }
1411
1412 return 0;
1413}
1414
1415static const struct net_device_ops flexcan_netdev_ops = {
1416 .ndo_open = flexcan_open,
1417 .ndo_stop = flexcan_close,
1418 .ndo_start_xmit = flexcan_start_xmit,
Oliver Hartkoppc971fa22014-03-07 09:23:41 +01001419 .ndo_change_mtu = can_change_mtu,
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001420};
1421
Bill Pemberton3c8ac0f2012-12-03 09:22:44 -05001422static int register_flexcandev(struct net_device *dev)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001423{
1424 struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Budde89af8742015-05-08 09:32:58 +02001425 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001426 u32 reg, err;
1427
Aisheng Dongca109892018-11-30 08:53:26 +00001428 err = flexcan_clks_enable(priv);
Fabio Estevamaa101812013-07-22 12:41:40 -03001429 if (err)
1430 return err;
1431
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001432 /* select "bus clock", chip must be disabled */
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001433 err = flexcan_chip_disable(priv);
1434 if (err)
Aisheng Dongca109892018-11-30 08:53:26 +00001435 goto out_clks_disable;
1436
Pankaj Bansal88462d22017-11-24 18:52:08 +05301437 reg = priv->read(&regs->ctrl);
Dong Aisheng8c306be2018-12-13 07:08:00 +00001438 if (priv->clk_src)
1439 reg |= FLEXCAN_CTRL_CLK_SRC;
1440 else
1441 reg &= ~FLEXCAN_CTRL_CLK_SRC;
Pankaj Bansal88462d22017-11-24 18:52:08 +05301442 priv->write(reg, &regs->ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001443
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001444 err = flexcan_chip_enable(priv);
1445 if (err)
1446 goto out_chip_disable;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001447
1448 /* set freeze, halt and activate FIFO, restrict register access */
Pankaj Bansal88462d22017-11-24 18:52:08 +05301449 reg = priv->read(&regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001450 reg |= FLEXCAN_MCR_FRZ | FLEXCAN_MCR_HALT |
1451 FLEXCAN_MCR_FEN | FLEXCAN_MCR_SUPV;
Pankaj Bansal88462d22017-11-24 18:52:08 +05301452 priv->write(reg, &regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001453
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +02001454 /* Currently we only support newer versions of this core
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +02001455 * featuring a RX hardware FIFO (although this driver doesn't
1456 * make use of it on some cores). Older cores, found on some
1457 * Coldfire derivates are not tested.
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001458 */
Pankaj Bansal88462d22017-11-24 18:52:08 +05301459 reg = priv->read(&regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001460 if (!(reg & FLEXCAN_MCR_FEN)) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +01001461 netdev_err(dev, "Could not enable RX FIFO, unsupported core\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001462 err = -ENODEV;
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001463 goto out_chip_disable;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001464 }
1465
1466 err = register_candev(dev);
Aisheng Dongca109892018-11-30 08:53:26 +00001467 if (err)
1468 goto out_chip_disable;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001469
Aisheng Dongca109892018-11-30 08:53:26 +00001470 /* Disable core and let pm_runtime_put() disable the clocks.
1471 * If CONFIG_PM is not enabled, the clocks will stay powered.
1472 */
1473 flexcan_chip_disable(priv);
1474 pm_runtime_put(priv->dev);
1475
1476 return 0;
1477
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001478 out_chip_disable:
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001479 flexcan_chip_disable(priv);
Aisheng Dongca109892018-11-30 08:53:26 +00001480 out_clks_disable:
1481 flexcan_clks_disable(priv);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001482 return err;
1483}
1484
Bill Pemberton3c8ac0f2012-12-03 09:22:44 -05001485static void unregister_flexcandev(struct net_device *dev)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001486{
1487 unregister_candev(dev);
1488}
1489
Aisheng Dongde3578c2018-11-23 08:35:33 +00001490static int flexcan_setup_stop_mode(struct platform_device *pdev)
1491{
1492 struct net_device *dev = platform_get_drvdata(pdev);
1493 struct device_node *np = pdev->dev.of_node;
1494 struct device_node *gpr_np;
1495 struct flexcan_priv *priv;
1496 phandle phandle;
1497 u32 out_val[5];
1498 int ret;
1499
1500 if (!np)
1501 return -EINVAL;
1502
1503 /* stop mode property format is:
1504 * <&gpr req_gpr req_bit ack_gpr ack_bit>.
1505 */
1506 ret = of_property_read_u32_array(np, "fsl,stop-mode", out_val,
1507 ARRAY_SIZE(out_val));
1508 if (ret) {
1509 dev_dbg(&pdev->dev, "no stop-mode property\n");
1510 return ret;
1511 }
1512 phandle = *out_val;
1513
1514 gpr_np = of_find_node_by_phandle(phandle);
1515 if (!gpr_np) {
1516 dev_dbg(&pdev->dev, "could not find gpr node by phandle\n");
YueHaibing7873e982018-12-12 17:24:01 +08001517 return -ENODEV;
Aisheng Dongde3578c2018-11-23 08:35:33 +00001518 }
1519
1520 priv = netdev_priv(dev);
1521 priv->stm.gpr = syscon_node_to_regmap(gpr_np);
Aisheng Dongde3578c2018-11-23 08:35:33 +00001522 if (IS_ERR(priv->stm.gpr)) {
1523 dev_dbg(&pdev->dev, "could not find gpr regmap\n");
Wen Yange9f2a852019-07-06 11:37:20 +08001524 ret = PTR_ERR(priv->stm.gpr);
1525 goto out_put_node;
Aisheng Dongde3578c2018-11-23 08:35:33 +00001526 }
1527
1528 priv->stm.req_gpr = out_val[1];
1529 priv->stm.req_bit = out_val[2];
1530 priv->stm.ack_gpr = out_val[3];
1531 priv->stm.ack_bit = out_val[4];
1532
1533 dev_dbg(&pdev->dev,
1534 "gpr %s req_gpr=0x02%x req_bit=%u ack_gpr=0x02%x ack_bit=%u\n",
1535 gpr_np->full_name, priv->stm.req_gpr, priv->stm.req_bit,
1536 priv->stm.ack_gpr, priv->stm.ack_bit);
1537
1538 device_set_wakeup_capable(&pdev->dev, true);
1539
Sean Nyekjaer915f9662019-04-09 10:39:48 +02001540 if (of_property_read_bool(np, "wakeup-source"))
1541 device_set_wakeup_enable(&pdev->dev, true);
1542
Aisheng Dongde3578c2018-11-23 08:35:33 +00001543 return 0;
David S. Miller13dfb3f2019-08-06 18:44:57 -07001544
Wen Yange9f2a852019-07-06 11:37:20 +08001545out_put_node:
1546 of_node_put(gpr_np);
1547 return ret;
Aisheng Dongde3578c2018-11-23 08:35:33 +00001548}
1549
Hui Wang30c1e672012-06-28 16:21:35 +08001550static const struct of_device_id flexcan_of_match[] = {
Hui Wang30c1e672012-06-28 16:21:35 +08001551 { .compatible = "fsl,imx6q-flexcan", .data = &fsl_imx6q_devtype_data, },
Marc Kleine-Buddee3587842013-10-03 23:51:55 +02001552 { .compatible = "fsl,imx28-flexcan", .data = &fsl_imx28_devtype_data, },
Uwe Kleine-König0e030a32018-04-25 16:50:39 +02001553 { .compatible = "fsl,imx53-flexcan", .data = &fsl_imx25_devtype_data, },
1554 { .compatible = "fsl,imx35-flexcan", .data = &fsl_imx25_devtype_data, },
1555 { .compatible = "fsl,imx25-flexcan", .data = &fsl_imx25_devtype_data, },
Marc Kleine-Buddee3587842013-10-03 23:51:55 +02001556 { .compatible = "fsl,p1010-flexcan", .data = &fsl_p1010_devtype_data, },
Stefan Agnercdce8442014-07-15 14:56:21 +02001557 { .compatible = "fsl,vf610-flexcan", .data = &fsl_vf610_devtype_data, },
Pankaj Bansal99b76682017-11-24 18:52:09 +05301558 { .compatible = "fsl,ls1021ar2-flexcan", .data = &fsl_ls1021a_r2_devtype_data, },
Hui Wang30c1e672012-06-28 16:21:35 +08001559 { /* sentinel */ },
1560};
Marc Kleine-Budde4358a9d2012-10-04 10:55:35 +02001561MODULE_DEVICE_TABLE(of, flexcan_of_match);
Hui Wang30c1e672012-06-28 16:21:35 +08001562
1563static const struct platform_device_id flexcan_id_table[] = {
1564 { .name = "flexcan", .driver_data = (kernel_ulong_t)&fsl_p1010_devtype_data, },
1565 { /* sentinel */ },
1566};
Marc Kleine-Budde4358a9d2012-10-04 10:55:35 +02001567MODULE_DEVICE_TABLE(platform, flexcan_id_table);
Hui Wang30c1e672012-06-28 16:21:35 +08001568
Bill Pemberton3c8ac0f2012-12-03 09:22:44 -05001569static int flexcan_probe(struct platform_device *pdev)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001570{
Hui Wang30c1e672012-06-28 16:21:35 +08001571 const struct of_device_id *of_id;
Marc Kleine-Buddedda0b3b2012-07-13 14:52:48 +02001572 const struct flexcan_devtype_data *devtype_data;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001573 struct net_device *dev;
1574 struct flexcan_priv *priv;
Andreas Werner555828e2015-03-22 17:35:52 +01001575 struct regulator *reg_xceiver;
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001576 struct clk *clk_ipg = NULL, *clk_per = NULL;
Marc Kleine-Budde89af8742015-05-08 09:32:58 +02001577 struct flexcan_regs __iomem *regs;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001578 int err, irq;
Dong Aisheng8c306be2018-12-13 07:08:00 +00001579 u8 clk_src = 1;
holt@sgi.com97efe9a2011-08-16 17:32:23 +00001580 u32 clock_freq = 0;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001581
Marc Kleine-Budde3d60f332020-09-22 16:44:16 +02001582 reg_xceiver = devm_regulator_get_optional(&pdev->dev, "xceiver");
Andreas Werner555828e2015-03-22 17:35:52 +01001583 if (PTR_ERR(reg_xceiver) == -EPROBE_DEFER)
1584 return -EPROBE_DEFER;
Marc Kleine-Budde3d60f332020-09-22 16:44:16 +02001585 else if (PTR_ERR(reg_xceiver) == -ENODEV)
Andreas Werner555828e2015-03-22 17:35:52 +01001586 reg_xceiver = NULL;
Marc Kleine-Budde3d60f332020-09-22 16:44:16 +02001587 else if (IS_ERR(reg_xceiver))
1588 return PTR_ERR(reg_xceiver);
Andreas Werner555828e2015-03-22 17:35:52 +01001589
Dong Aisheng8c306be2018-12-13 07:08:00 +00001590 if (pdev->dev.of_node) {
Hui Wangafc016d2012-06-28 16:21:34 +08001591 of_property_read_u32(pdev->dev.of_node,
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +02001592 "clock-frequency", &clock_freq);
Dong Aisheng8c306be2018-12-13 07:08:00 +00001593 of_property_read_u8(pdev->dev.of_node,
1594 "fsl,clk-source", &clk_src);
1595 }
holt@sgi.com97efe9a2011-08-16 17:32:23 +00001596
1597 if (!clock_freq) {
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001598 clk_ipg = devm_clk_get(&pdev->dev, "ipg");
1599 if (IS_ERR(clk_ipg)) {
1600 dev_err(&pdev->dev, "no ipg clock defined\n");
Fabio Estevam933e4af2013-07-22 12:41:39 -03001601 return PTR_ERR(clk_ipg);
holt@sgi.com97efe9a2011-08-16 17:32:23 +00001602 }
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001603
1604 clk_per = devm_clk_get(&pdev->dev, "per");
1605 if (IS_ERR(clk_per)) {
1606 dev_err(&pdev->dev, "no per clock defined\n");
Fabio Estevam933e4af2013-07-22 12:41:39 -03001607 return PTR_ERR(clk_per);
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001608 }
Marc Kleine-Budde1a3e5172013-11-25 22:15:20 +01001609 clock_freq = clk_get_rate(clk_per);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001610 }
1611
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001612 irq = platform_get_irq(pdev, 0);
Fabio Estevam933e4af2013-07-22 12:41:39 -03001613 if (irq <= 0)
1614 return -ENODEV;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001615
Joakim Zhanga4721f22019-09-29 08:32:09 +00001616 regs = devm_platform_ioremap_resource(pdev, 0);
Marc Kleine-Budde89af8742015-05-08 09:32:58 +02001617 if (IS_ERR(regs))
1618 return PTR_ERR(regs);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001619
Hui Wang30c1e672012-06-28 16:21:35 +08001620 of_id = of_match_device(flexcan_of_match, &pdev->dev);
1621 if (of_id) {
1622 devtype_data = of_id->data;
Marc Kleine-Budded0873e62014-03-04 22:04:22 +01001623 } else if (platform_get_device_id(pdev)->driver_data) {
Hui Wang30c1e672012-06-28 16:21:35 +08001624 devtype_data = (struct flexcan_devtype_data *)
Marc Kleine-Budded0873e62014-03-04 22:04:22 +01001625 platform_get_device_id(pdev)->driver_data;
Hui Wang30c1e672012-06-28 16:21:35 +08001626 } else {
Fabio Estevam933e4af2013-07-22 12:41:39 -03001627 return -ENODEV;
Hui Wang30c1e672012-06-28 16:21:35 +08001628 }
1629
Fabio Estevam933e4af2013-07-22 12:41:39 -03001630 dev = alloc_candev(sizeof(struct flexcan_priv), 1);
1631 if (!dev)
1632 return -ENOMEM;
1633
Marc Kleine-Budde30164752015-05-10 15:26:58 +02001634 platform_set_drvdata(pdev, dev);
1635 SET_NETDEV_DEV(dev, &pdev->dev);
1636
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001637 dev->netdev_ops = &flexcan_netdev_ops;
1638 dev->irq = irq;
Reuben Dowle9a123492011-11-01 11:18:03 +13001639 dev->flags |= IFF_ECHO;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001640
1641 priv = netdev_priv(dev);
Pankaj Bansal88462d22017-11-24 18:52:08 +05301642
Uwe Kleine-König0e030a32018-04-25 16:50:39 +02001643 if (of_property_read_bool(pdev->dev.of_node, "big-endian") ||
1644 devtype_data->quirks & FLEXCAN_QUIRK_DEFAULT_BIG_ENDIAN) {
Pankaj Bansal88462d22017-11-24 18:52:08 +05301645 priv->read = flexcan_read_be;
1646 priv->write = flexcan_write_be;
1647 } else {
Uwe Kleine-König0e030a32018-04-25 16:50:39 +02001648 priv->read = flexcan_read_le;
1649 priv->write = flexcan_write_le;
Pankaj Bansal88462d22017-11-24 18:52:08 +05301650 }
1651
Aisheng Dongca109892018-11-30 08:53:26 +00001652 priv->dev = &pdev->dev;
holt@sgi.com97efe9a2011-08-16 17:32:23 +00001653 priv->can.clock.freq = clock_freq;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001654 priv->can.bittiming_const = &flexcan_bittiming_const;
1655 priv->can.do_set_mode = flexcan_set_mode;
1656 priv->can.do_get_berr_counter = flexcan_get_berr_counter;
1657 priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK |
1658 CAN_CTRLMODE_LISTENONLY | CAN_CTRLMODE_3_SAMPLES |
1659 CAN_CTRLMODE_BERR_REPORTING;
Marc Kleine-Budde89af8742015-05-08 09:32:58 +02001660 priv->regs = regs;
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001661 priv->clk_ipg = clk_ipg;
1662 priv->clk_per = clk_per;
Dong Aisheng8c306be2018-12-13 07:08:00 +00001663 priv->clk_src = clk_src;
Hui Wang30c1e672012-06-28 16:21:35 +08001664 priv->devtype_data = devtype_data;
Andreas Werner555828e2015-03-22 17:35:52 +01001665 priv->reg_xceiver = reg_xceiver;
Fabio Estevamb7c41142013-06-10 23:12:57 -03001666
Aisheng Dongca109892018-11-30 08:53:26 +00001667 pm_runtime_get_noresume(&pdev->dev);
1668 pm_runtime_set_active(&pdev->dev);
1669 pm_runtime_enable(&pdev->dev);
1670
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001671 err = register_flexcandev(dev);
1672 if (err) {
1673 dev_err(&pdev->dev, "registering netdev failed\n");
1674 goto failed_register;
1675 }
1676
Joakim Zhangee973022019-10-30 06:45:57 +00001677 of_can_transceiver(dev);
Fabio Baltieriadccadb2012-12-18 18:50:58 +01001678 devm_can_led_init(dev);
1679
Aisheng Dongde3578c2018-11-23 08:35:33 +00001680 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_SETUP_STOP_MODE) {
1681 err = flexcan_setup_stop_mode(pdev);
1682 if (err)
1683 dev_dbg(&pdev->dev, "failed to setup stop-mode\n");
1684 }
1685
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001686 return 0;
1687
1688 failed_register:
1689 free_candev(dev);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001690 return err;
1691}
1692
Bill Pemberton3c8ac0f2012-12-03 09:22:44 -05001693static int flexcan_remove(struct platform_device *pdev)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001694{
1695 struct net_device *dev = platform_get_drvdata(pdev);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001696
1697 unregister_flexcandev(dev);
Aisheng Dongca109892018-11-30 08:53:26 +00001698 pm_runtime_disable(&pdev->dev);
Marc Kleine-Budde9a275862010-10-21 05:07:58 +00001699 free_candev(dev);
1700
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001701 return 0;
1702}
1703
Marc Kleine-Budde08c6d352014-03-05 19:10:44 +01001704static int __maybe_unused flexcan_suspend(struct device *device)
Eric Bénard8b5e2182012-05-08 17:12:17 +02001705{
Fabio Estevam588e7a82013-05-20 15:43:43 -03001706 struct net_device *dev = dev_get_drvdata(device);
Eric Bénard8b5e2182012-05-08 17:12:17 +02001707 struct flexcan_priv *priv = netdev_priv(dev);
Aisheng Dongca109892018-11-30 08:53:26 +00001708 int err = 0;
Eric Bénard8b5e2182012-05-08 17:12:17 +02001709
Eric Bénard8b5e2182012-05-08 17:12:17 +02001710 if (netif_running(dev)) {
Aisheng Dongde3578c2018-11-23 08:35:33 +00001711 /* if wakeup is enabled, enter stop mode
1712 * else enter disabled mode.
1713 */
1714 if (device_may_wakeup(device)) {
1715 enable_irq_wake(dev->irq);
Joakim Zhang5f186c22019-07-02 01:45:41 +00001716 err = flexcan_enter_stop_mode(priv);
1717 if (err)
1718 return err;
Aisheng Dongde3578c2018-11-23 08:35:33 +00001719 } else {
1720 err = flexcan_chip_disable(priv);
1721 if (err)
1722 return err;
Aisheng Dongca109892018-11-30 08:53:26 +00001723
1724 err = pm_runtime_force_suspend(device);
Aisheng Dongde3578c2018-11-23 08:35:33 +00001725 }
Eric Bénard8b5e2182012-05-08 17:12:17 +02001726 netif_stop_queue(dev);
1727 netif_device_detach(dev);
1728 }
1729 priv->can.state = CAN_STATE_SLEEPING;
1730
Aisheng Dongca109892018-11-30 08:53:26 +00001731 return err;
Eric Bénard8b5e2182012-05-08 17:12:17 +02001732}
1733
Marc Kleine-Budde08c6d352014-03-05 19:10:44 +01001734static int __maybe_unused flexcan_resume(struct device *device)
Eric Bénard8b5e2182012-05-08 17:12:17 +02001735{
Fabio Estevam588e7a82013-05-20 15:43:43 -03001736 struct net_device *dev = dev_get_drvdata(device);
Eric Bénard8b5e2182012-05-08 17:12:17 +02001737 struct flexcan_priv *priv = netdev_priv(dev);
Aisheng Dongca109892018-11-30 08:53:26 +00001738 int err = 0;
Eric Bénard8b5e2182012-05-08 17:12:17 +02001739
1740 priv->can.state = CAN_STATE_ERROR_ACTIVE;
1741 if (netif_running(dev)) {
1742 netif_device_attach(dev);
1743 netif_start_queue(dev);
Aisheng Dongde3578c2018-11-23 08:35:33 +00001744 if (device_may_wakeup(device)) {
1745 disable_irq_wake(dev->irq);
Sean Nyekjaere7071802019-12-04 11:36:06 +00001746 err = flexcan_exit_stop_mode(priv);
1747 if (err)
1748 return err;
Aisheng Dongde3578c2018-11-23 08:35:33 +00001749 } else {
Aisheng Dongca109892018-11-30 08:53:26 +00001750 err = pm_runtime_force_resume(device);
Aisheng Dongde3578c2018-11-23 08:35:33 +00001751 if (err)
1752 return err;
Aisheng Dongca109892018-11-30 08:53:26 +00001753
1754 err = flexcan_chip_enable(priv);
Aisheng Dongde3578c2018-11-23 08:35:33 +00001755 }
Eric Bénard8b5e2182012-05-08 17:12:17 +02001756 }
Aisheng Dongca109892018-11-30 08:53:26 +00001757
1758 return err;
1759}
1760
1761static int __maybe_unused flexcan_runtime_suspend(struct device *device)
1762{
1763 struct net_device *dev = dev_get_drvdata(device);
1764 struct flexcan_priv *priv = netdev_priv(dev);
1765
1766 flexcan_clks_disable(priv);
1767
Fabio Estevam4de349e2016-08-17 12:41:08 -03001768 return 0;
Eric Bénard8b5e2182012-05-08 17:12:17 +02001769}
Fabio Estevam588e7a82013-05-20 15:43:43 -03001770
Aisheng Dongca109892018-11-30 08:53:26 +00001771static int __maybe_unused flexcan_runtime_resume(struct device *device)
1772{
1773 struct net_device *dev = dev_get_drvdata(device);
1774 struct flexcan_priv *priv = netdev_priv(dev);
1775
1776 return flexcan_clks_enable(priv);
1777}
1778
Aisheng Dongde3578c2018-11-23 08:35:33 +00001779static int __maybe_unused flexcan_noirq_suspend(struct device *device)
1780{
1781 struct net_device *dev = dev_get_drvdata(device);
1782 struct flexcan_priv *priv = netdev_priv(dev);
1783
1784 if (netif_running(dev) && device_may_wakeup(device))
1785 flexcan_enable_wakeup_irq(priv, true);
1786
1787 return 0;
1788}
1789
1790static int __maybe_unused flexcan_noirq_resume(struct device *device)
1791{
1792 struct net_device *dev = dev_get_drvdata(device);
1793 struct flexcan_priv *priv = netdev_priv(dev);
1794
Sean Nyekjaere7071802019-12-04 11:36:06 +00001795 if (netif_running(dev) && device_may_wakeup(device))
Aisheng Dongde3578c2018-11-23 08:35:33 +00001796 flexcan_enable_wakeup_irq(priv, false);
Aisheng Dongde3578c2018-11-23 08:35:33 +00001797
1798 return 0;
1799}
1800
1801static const struct dev_pm_ops flexcan_pm_ops = {
1802 SET_SYSTEM_SLEEP_PM_OPS(flexcan_suspend, flexcan_resume)
Aisheng Dongca109892018-11-30 08:53:26 +00001803 SET_RUNTIME_PM_OPS(flexcan_runtime_suspend, flexcan_runtime_resume, NULL)
Aisheng Dongde3578c2018-11-23 08:35:33 +00001804 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(flexcan_noirq_suspend, flexcan_noirq_resume)
1805};
Eric Bénard8b5e2182012-05-08 17:12:17 +02001806
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001807static struct platform_driver flexcan_driver = {
holt@sgi.comc8aef4c2011-08-16 17:32:22 +00001808 .driver = {
1809 .name = DRV_NAME,
Fabio Estevam588e7a82013-05-20 15:43:43 -03001810 .pm = &flexcan_pm_ops,
holt@sgi.comc8aef4c2011-08-16 17:32:22 +00001811 .of_match_table = flexcan_of_match,
1812 },
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001813 .probe = flexcan_probe,
Bill Pemberton3c8ac0f2012-12-03 09:22:44 -05001814 .remove = flexcan_remove,
Hui Wang30c1e672012-06-28 16:21:35 +08001815 .id_table = flexcan_id_table,
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001816};
1817
Axel Lin871d3372011-11-27 15:42:31 +00001818module_platform_driver(flexcan_driver);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001819
1820MODULE_AUTHOR("Sascha Hauer <kernel@pengutronix.de>, "
1821 "Marc Kleine-Budde <kernel@pengutronix.de>");
1822MODULE_LICENSE("GPL v2");
1823MODULE_DESCRIPTION("CAN port driver for flexcan based chip");