blob: 52d73115c7fd26274a998e2ed33b0c8a6efc11c8 [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)
Joakim Zhang15ef2072020-04-16 17:31:25 +0800193/* Disable non-correctable errors interrupt and freeze mode */
Marc Kleine-Buddeef4b6232020-09-22 16:44:14 +0200194#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
Joakim Zhang15ef2072020-04-16 17:31:25 +08001224 /* On Vybrid, disable non-correctable errors interrupt and
1225 * freeze mode. It still can correct the correctable errors
1226 * when HW supports ECC.
1227 *
1228 * This also works around errata e5295 which generates false
1229 * positive memory errors and put the device in freeze mode.
Stefan Agnercdce8442014-07-15 14:56:21 +02001230 */
Marc Kleine-Buddef377bff2015-05-08 15:22:36 +02001231 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_DISABLE_MECR) {
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +02001232 /* Follow the protocol as described in "Detection
Stefan Agnercdce8442014-07-15 14:56:21 +02001233 * and Correction of Memory Errors" to write to
Joakim Zhang15ef2072020-04-16 17:31:25 +08001234 * MECR register (step 1 - 5)
1235 *
1236 * 1. By default, CTRL2[ECRWRE] = 0, MECR[ECRWRDIS] = 1
1237 * 2. set CTRL2[ECRWRE]
Stefan Agnercdce8442014-07-15 14:56:21 +02001238 */
Pankaj Bansal88462d22017-11-24 18:52:08 +05301239 reg_ctrl2 = priv->read(&regs->ctrl2);
Marc Kleine-Budde6f75fce2014-09-23 11:03:01 +02001240 reg_ctrl2 |= FLEXCAN_CTRL2_ECRWRE;
Pankaj Bansal88462d22017-11-24 18:52:08 +05301241 priv->write(reg_ctrl2, &regs->ctrl2);
Stefan Agnercdce8442014-07-15 14:56:21 +02001242
Joakim Zhang15ef2072020-04-16 17:31:25 +08001243 /* 3. clear MECR[ECRWRDIS] */
Pankaj Bansal88462d22017-11-24 18:52:08 +05301244 reg_mecr = priv->read(&regs->mecr);
Stefan Agnercdce8442014-07-15 14:56:21 +02001245 reg_mecr &= ~FLEXCAN_MECR_ECRWRDIS;
Pankaj Bansal88462d22017-11-24 18:52:08 +05301246 priv->write(reg_mecr, &regs->mecr);
Joakim Zhang15ef2072020-04-16 17:31:25 +08001247
1248 /* 4. all writes to MECR must keep MECR[ECRWRDIS] cleared */
Stefan Agnercdce8442014-07-15 14:56:21 +02001249 reg_mecr &= ~(FLEXCAN_MECR_NCEFAFRZ | FLEXCAN_MECR_HANCEI_MSK |
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +02001250 FLEXCAN_MECR_FANCEI_MSK);
Pankaj Bansal88462d22017-11-24 18:52:08 +05301251 priv->write(reg_mecr, &regs->mecr);
Joakim Zhang15ef2072020-04-16 17:31:25 +08001252
1253 /* 5. after configuration done, lock MECR by either
1254 * setting MECR[ECRWRDIS] or clearing CTRL2[ECRWRE]
1255 */
1256 reg_mecr |= FLEXCAN_MECR_ECRWRDIS;
1257 priv->write(reg_mecr, &regs->mecr);
1258
1259 reg_ctrl2 &= ~FLEXCAN_CTRL2_ECRWRE;
1260 priv->write(reg_ctrl2, &regs->ctrl2);
Stefan Agnercdce8442014-07-15 14:56:21 +02001261 }
1262
Marc Kleine-Buddef0036982014-02-28 17:18:27 +01001263 err = flexcan_transceiver_enable(priv);
1264 if (err)
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +01001265 goto out_chip_disable;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001266
1267 /* synchronize with the can bus */
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +01001268 err = flexcan_chip_unfreeze(priv);
1269 if (err)
1270 goto out_transceiver_disable;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001271
1272 priv->can.state = CAN_STATE_ERROR_ACTIVE;
1273
Marc Kleine-Budde6fa7da22015-08-27 14:24:48 +02001274 /* enable interrupts atomically */
1275 disable_irq(dev->irq);
Pankaj Bansal88462d22017-11-24 18:52:08 +05301276 priv->write(priv->reg_ctrl_default, &regs->ctrl);
Marc Kleine-Budde0ca64f022019-03-01 13:54:19 +01001277 reg_imask = priv->rx_mask | priv->tx_mask;
Marc Kleine-Budde8ce51392019-03-01 12:17:30 +01001278 priv->write(upper_32_bits(reg_imask), &regs->imask2);
1279 priv->write(lower_32_bits(reg_imask), &regs->imask1);
Marc Kleine-Budde6fa7da22015-08-27 14:24:48 +02001280 enable_irq(dev->irq);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001281
1282 /* print chip status */
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +01001283 netdev_dbg(dev, "%s: reading mcr=0x%08x ctrl=0x%08x\n", __func__,
Pankaj Bansal88462d22017-11-24 18:52:08 +05301284 priv->read(&regs->mcr), priv->read(&regs->ctrl));
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001285
1286 return 0;
1287
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +01001288 out_transceiver_disable:
1289 flexcan_transceiver_disable(priv);
1290 out_chip_disable:
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001291 flexcan_chip_disable(priv);
1292 return err;
1293}
1294
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +02001295/* flexcan_chip_stop
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001296 *
1297 * this functions is entered with clocks enabled
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001298 */
1299static void flexcan_chip_stop(struct net_device *dev)
1300{
1301 struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Budde89af8742015-05-08 09:32:58 +02001302 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001303
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +01001304 /* freeze + disable module */
1305 flexcan_chip_freeze(priv);
1306 flexcan_chip_disable(priv);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001307
Marc Kleine-Budde5be93bd2014-02-19 12:00:51 +01001308 /* Disable all interrupts */
Pankaj Bansal88462d22017-11-24 18:52:08 +05301309 priv->write(0, &regs->imask2);
1310 priv->write(0, &regs->imask1);
1311 priv->write(priv->reg_ctrl_default & ~FLEXCAN_CTRL_ERR_ALL,
1312 &regs->ctrl);
Marc Kleine-Budde5be93bd2014-02-19 12:00:51 +01001313
Marc Kleine-Buddef0036982014-02-28 17:18:27 +01001314 flexcan_transceiver_disable(priv);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001315 priv->can.state = CAN_STATE_STOPPED;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001316}
1317
1318static int flexcan_open(struct net_device *dev)
1319{
1320 struct flexcan_priv *priv = netdev_priv(dev);
1321 int err;
1322
Aisheng Dongca109892018-11-30 08:53:26 +00001323 err = pm_runtime_get_sync(priv->dev);
1324 if (err < 0)
Fabio Estevamaa101812013-07-22 12:41:40 -03001325 return err;
1326
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001327 err = open_candev(dev);
1328 if (err)
Aisheng Dongca109892018-11-30 08:53:26 +00001329 goto out_runtime_put;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001330
1331 err = request_irq(dev->irq, flexcan_irq, IRQF_SHARED, dev->name, dev);
1332 if (err)
1333 goto out_close;
1334
Pankaj Bansal05179612018-11-23 22:18:44 +01001335 priv->mb_size = sizeof(struct flexcan_mb) + CAN_MAX_DLEN;
Pankaj Bansal6cbf7602018-08-28 23:19:12 +05301336 priv->mb_count = (sizeof(priv->regs->mb[0]) / priv->mb_size) +
1337 (sizeof(priv->regs->mb[1]) / priv->mb_size);
Pankaj Bansal05179612018-11-23 22:18:44 +01001338
Pankaj Bansal5156c7b2018-08-13 23:50:49 +05301339 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP)
Pankaj Bansal05179612018-11-23 22:18:44 +01001340 priv->tx_mb_reserved =
1341 flexcan_get_mb(priv, FLEXCAN_TX_MB_RESERVED_OFF_TIMESTAMP);
Pankaj Bansal5156c7b2018-08-13 23:50:49 +05301342 else
Pankaj Bansal05179612018-11-23 22:18:44 +01001343 priv->tx_mb_reserved =
1344 flexcan_get_mb(priv, FLEXCAN_TX_MB_RESERVED_OFF_FIFO);
1345 priv->tx_mb_idx = priv->mb_count - 1;
1346 priv->tx_mb = flexcan_get_mb(priv, priv->tx_mb_idx);
Marc Kleine-Budde0ca64f022019-03-01 13:54:19 +01001347 priv->tx_mask = FLEXCAN_IFLAG_MB(priv->tx_mb_idx);
Pankaj Bansal5156c7b2018-08-13 23:50:49 +05301348
Pankaj Bansal5156c7b2018-08-13 23:50:49 +05301349 priv->offload.mailbox_read = flexcan_mailbox_read;
1350
1351 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP) {
Pankaj Bansal5156c7b2018-08-13 23:50:49 +05301352 priv->offload.mb_first = FLEXCAN_RX_MB_OFF_TIMESTAMP_FIRST;
Pankaj Bansal05179612018-11-23 22:18:44 +01001353 priv->offload.mb_last = priv->mb_count - 2;
Pankaj Bansal5156c7b2018-08-13 23:50:49 +05301354
Marc Kleine-Budde8ce51392019-03-01 12:17:30 +01001355 priv->rx_mask = GENMASK_ULL(priv->offload.mb_last,
1356 priv->offload.mb_first);
Pankaj Bansal5156c7b2018-08-13 23:50:49 +05301357 err = can_rx_offload_add_timestamp(dev, &priv->offload);
1358 } else {
Marc Kleine-Budde8ce51392019-03-01 12:17:30 +01001359 priv->rx_mask = FLEXCAN_IFLAG_RX_FIFO_OVERFLOW |
Pankaj Bansal5156c7b2018-08-13 23:50:49 +05301360 FLEXCAN_IFLAG_RX_FIFO_AVAILABLE;
1361 err = can_rx_offload_add_fifo(dev, &priv->offload,
1362 FLEXCAN_NAPI_WEIGHT);
1363 }
1364 if (err)
1365 goto out_free_irq;
1366
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001367 /* start chip and queuing */
1368 err = flexcan_chip_start(dev);
1369 if (err)
Pankaj Bansal5156c7b2018-08-13 23:50:49 +05301370 goto out_offload_del;
Fabio Baltieriadccadb2012-12-18 18:50:58 +01001371
1372 can_led_event(dev, CAN_LED_EVENT_OPEN);
1373
Marc Kleine-Budde30164752015-05-10 15:26:58 +02001374 can_rx_offload_enable(&priv->offload);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001375 netif_start_queue(dev);
1376
1377 return 0;
1378
Pankaj Bansal5156c7b2018-08-13 23:50:49 +05301379 out_offload_del:
1380 can_rx_offload_del(&priv->offload);
Marc Kleine-Budde7e9e1482014-02-28 14:52:01 +01001381 out_free_irq:
1382 free_irq(dev->irq, dev);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001383 out_close:
1384 close_candev(dev);
Aisheng Dongca109892018-11-30 08:53:26 +00001385 out_runtime_put:
1386 pm_runtime_put(priv->dev);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001387
1388 return err;
1389}
1390
1391static int flexcan_close(struct net_device *dev)
1392{
1393 struct flexcan_priv *priv = netdev_priv(dev);
1394
1395 netif_stop_queue(dev);
Marc Kleine-Budde30164752015-05-10 15:26:58 +02001396 can_rx_offload_disable(&priv->offload);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001397 flexcan_chip_stop(dev);
1398
Pankaj Bansal5156c7b2018-08-13 23:50:49 +05301399 can_rx_offload_del(&priv->offload);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001400 free_irq(dev->irq, dev);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001401
1402 close_candev(dev);
Aisheng Dongca109892018-11-30 08:53:26 +00001403 pm_runtime_put(priv->dev);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001404
Fabio Baltieriadccadb2012-12-18 18:50:58 +01001405 can_led_event(dev, CAN_LED_EVENT_STOP);
1406
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001407 return 0;
1408}
1409
1410static int flexcan_set_mode(struct net_device *dev, enum can_mode mode)
1411{
1412 int err;
1413
1414 switch (mode) {
1415 case CAN_MODE_START:
1416 err = flexcan_chip_start(dev);
1417 if (err)
1418 return err;
1419
1420 netif_wake_queue(dev);
1421 break;
1422
1423 default:
1424 return -EOPNOTSUPP;
1425 }
1426
1427 return 0;
1428}
1429
1430static const struct net_device_ops flexcan_netdev_ops = {
1431 .ndo_open = flexcan_open,
1432 .ndo_stop = flexcan_close,
1433 .ndo_start_xmit = flexcan_start_xmit,
Oliver Hartkoppc971fa22014-03-07 09:23:41 +01001434 .ndo_change_mtu = can_change_mtu,
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001435};
1436
Bill Pemberton3c8ac0f2012-12-03 09:22:44 -05001437static int register_flexcandev(struct net_device *dev)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001438{
1439 struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Budde89af8742015-05-08 09:32:58 +02001440 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001441 u32 reg, err;
1442
Aisheng Dongca109892018-11-30 08:53:26 +00001443 err = flexcan_clks_enable(priv);
Fabio Estevamaa101812013-07-22 12:41:40 -03001444 if (err)
1445 return err;
1446
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001447 /* select "bus clock", chip must be disabled */
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001448 err = flexcan_chip_disable(priv);
1449 if (err)
Aisheng Dongca109892018-11-30 08:53:26 +00001450 goto out_clks_disable;
1451
Pankaj Bansal88462d22017-11-24 18:52:08 +05301452 reg = priv->read(&regs->ctrl);
Dong Aisheng8c306be2018-12-13 07:08:00 +00001453 if (priv->clk_src)
1454 reg |= FLEXCAN_CTRL_CLK_SRC;
1455 else
1456 reg &= ~FLEXCAN_CTRL_CLK_SRC;
Pankaj Bansal88462d22017-11-24 18:52:08 +05301457 priv->write(reg, &regs->ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001458
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001459 err = flexcan_chip_enable(priv);
1460 if (err)
1461 goto out_chip_disable;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001462
1463 /* set freeze, halt and activate FIFO, restrict register access */
Pankaj Bansal88462d22017-11-24 18:52:08 +05301464 reg = priv->read(&regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001465 reg |= FLEXCAN_MCR_FRZ | FLEXCAN_MCR_HALT |
1466 FLEXCAN_MCR_FEN | FLEXCAN_MCR_SUPV;
Pankaj Bansal88462d22017-11-24 18:52:08 +05301467 priv->write(reg, &regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001468
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +02001469 /* Currently we only support newer versions of this core
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +02001470 * featuring a RX hardware FIFO (although this driver doesn't
1471 * make use of it on some cores). Older cores, found on some
1472 * Coldfire derivates are not tested.
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001473 */
Pankaj Bansal88462d22017-11-24 18:52:08 +05301474 reg = priv->read(&regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001475 if (!(reg & FLEXCAN_MCR_FEN)) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +01001476 netdev_err(dev, "Could not enable RX FIFO, unsupported core\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001477 err = -ENODEV;
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001478 goto out_chip_disable;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001479 }
1480
1481 err = register_candev(dev);
Aisheng Dongca109892018-11-30 08:53:26 +00001482 if (err)
1483 goto out_chip_disable;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001484
Aisheng Dongca109892018-11-30 08:53:26 +00001485 /* Disable core and let pm_runtime_put() disable the clocks.
1486 * If CONFIG_PM is not enabled, the clocks will stay powered.
1487 */
1488 flexcan_chip_disable(priv);
1489 pm_runtime_put(priv->dev);
1490
1491 return 0;
1492
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001493 out_chip_disable:
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001494 flexcan_chip_disable(priv);
Aisheng Dongca109892018-11-30 08:53:26 +00001495 out_clks_disable:
1496 flexcan_clks_disable(priv);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001497 return err;
1498}
1499
Bill Pemberton3c8ac0f2012-12-03 09:22:44 -05001500static void unregister_flexcandev(struct net_device *dev)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001501{
1502 unregister_candev(dev);
1503}
1504
Aisheng Dongde3578c2018-11-23 08:35:33 +00001505static int flexcan_setup_stop_mode(struct platform_device *pdev)
1506{
1507 struct net_device *dev = platform_get_drvdata(pdev);
1508 struct device_node *np = pdev->dev.of_node;
1509 struct device_node *gpr_np;
1510 struct flexcan_priv *priv;
1511 phandle phandle;
1512 u32 out_val[5];
1513 int ret;
1514
1515 if (!np)
1516 return -EINVAL;
1517
1518 /* stop mode property format is:
1519 * <&gpr req_gpr req_bit ack_gpr ack_bit>.
1520 */
1521 ret = of_property_read_u32_array(np, "fsl,stop-mode", out_val,
1522 ARRAY_SIZE(out_val));
1523 if (ret) {
1524 dev_dbg(&pdev->dev, "no stop-mode property\n");
1525 return ret;
1526 }
1527 phandle = *out_val;
1528
1529 gpr_np = of_find_node_by_phandle(phandle);
1530 if (!gpr_np) {
1531 dev_dbg(&pdev->dev, "could not find gpr node by phandle\n");
YueHaibing7873e982018-12-12 17:24:01 +08001532 return -ENODEV;
Aisheng Dongde3578c2018-11-23 08:35:33 +00001533 }
1534
1535 priv = netdev_priv(dev);
1536 priv->stm.gpr = syscon_node_to_regmap(gpr_np);
Aisheng Dongde3578c2018-11-23 08:35:33 +00001537 if (IS_ERR(priv->stm.gpr)) {
1538 dev_dbg(&pdev->dev, "could not find gpr regmap\n");
Wen Yange9f2a852019-07-06 11:37:20 +08001539 ret = PTR_ERR(priv->stm.gpr);
1540 goto out_put_node;
Aisheng Dongde3578c2018-11-23 08:35:33 +00001541 }
1542
1543 priv->stm.req_gpr = out_val[1];
1544 priv->stm.req_bit = out_val[2];
1545 priv->stm.ack_gpr = out_val[3];
1546 priv->stm.ack_bit = out_val[4];
1547
1548 dev_dbg(&pdev->dev,
1549 "gpr %s req_gpr=0x02%x req_bit=%u ack_gpr=0x02%x ack_bit=%u\n",
1550 gpr_np->full_name, priv->stm.req_gpr, priv->stm.req_bit,
1551 priv->stm.ack_gpr, priv->stm.ack_bit);
1552
1553 device_set_wakeup_capable(&pdev->dev, true);
1554
Sean Nyekjaer915f9662019-04-09 10:39:48 +02001555 if (of_property_read_bool(np, "wakeup-source"))
1556 device_set_wakeup_enable(&pdev->dev, true);
1557
Aisheng Dongde3578c2018-11-23 08:35:33 +00001558 return 0;
David S. Miller13dfb3f2019-08-06 18:44:57 -07001559
Wen Yange9f2a852019-07-06 11:37:20 +08001560out_put_node:
1561 of_node_put(gpr_np);
1562 return ret;
Aisheng Dongde3578c2018-11-23 08:35:33 +00001563}
1564
Hui Wang30c1e672012-06-28 16:21:35 +08001565static const struct of_device_id flexcan_of_match[] = {
Hui Wang30c1e672012-06-28 16:21:35 +08001566 { .compatible = "fsl,imx6q-flexcan", .data = &fsl_imx6q_devtype_data, },
Marc Kleine-Buddee3587842013-10-03 23:51:55 +02001567 { .compatible = "fsl,imx28-flexcan", .data = &fsl_imx28_devtype_data, },
Uwe Kleine-König0e030a32018-04-25 16:50:39 +02001568 { .compatible = "fsl,imx53-flexcan", .data = &fsl_imx25_devtype_data, },
1569 { .compatible = "fsl,imx35-flexcan", .data = &fsl_imx25_devtype_data, },
1570 { .compatible = "fsl,imx25-flexcan", .data = &fsl_imx25_devtype_data, },
Marc Kleine-Buddee3587842013-10-03 23:51:55 +02001571 { .compatible = "fsl,p1010-flexcan", .data = &fsl_p1010_devtype_data, },
Stefan Agnercdce8442014-07-15 14:56:21 +02001572 { .compatible = "fsl,vf610-flexcan", .data = &fsl_vf610_devtype_data, },
Pankaj Bansal99b76682017-11-24 18:52:09 +05301573 { .compatible = "fsl,ls1021ar2-flexcan", .data = &fsl_ls1021a_r2_devtype_data, },
Hui Wang30c1e672012-06-28 16:21:35 +08001574 { /* sentinel */ },
1575};
Marc Kleine-Budde4358a9d2012-10-04 10:55:35 +02001576MODULE_DEVICE_TABLE(of, flexcan_of_match);
Hui Wang30c1e672012-06-28 16:21:35 +08001577
1578static const struct platform_device_id flexcan_id_table[] = {
1579 { .name = "flexcan", .driver_data = (kernel_ulong_t)&fsl_p1010_devtype_data, },
1580 { /* sentinel */ },
1581};
Marc Kleine-Budde4358a9d2012-10-04 10:55:35 +02001582MODULE_DEVICE_TABLE(platform, flexcan_id_table);
Hui Wang30c1e672012-06-28 16:21:35 +08001583
Bill Pemberton3c8ac0f2012-12-03 09:22:44 -05001584static int flexcan_probe(struct platform_device *pdev)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001585{
Hui Wang30c1e672012-06-28 16:21:35 +08001586 const struct of_device_id *of_id;
Marc Kleine-Buddedda0b3b2012-07-13 14:52:48 +02001587 const struct flexcan_devtype_data *devtype_data;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001588 struct net_device *dev;
1589 struct flexcan_priv *priv;
Andreas Werner555828e2015-03-22 17:35:52 +01001590 struct regulator *reg_xceiver;
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001591 struct clk *clk_ipg = NULL, *clk_per = NULL;
Marc Kleine-Budde89af8742015-05-08 09:32:58 +02001592 struct flexcan_regs __iomem *regs;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001593 int err, irq;
Dong Aisheng8c306be2018-12-13 07:08:00 +00001594 u8 clk_src = 1;
holt@sgi.com97efe9a2011-08-16 17:32:23 +00001595 u32 clock_freq = 0;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001596
Marc Kleine-Budde3d60f332020-09-22 16:44:16 +02001597 reg_xceiver = devm_regulator_get_optional(&pdev->dev, "xceiver");
Andreas Werner555828e2015-03-22 17:35:52 +01001598 if (PTR_ERR(reg_xceiver) == -EPROBE_DEFER)
1599 return -EPROBE_DEFER;
Marc Kleine-Budde3d60f332020-09-22 16:44:16 +02001600 else if (PTR_ERR(reg_xceiver) == -ENODEV)
Andreas Werner555828e2015-03-22 17:35:52 +01001601 reg_xceiver = NULL;
Marc Kleine-Budde3d60f332020-09-22 16:44:16 +02001602 else if (IS_ERR(reg_xceiver))
1603 return PTR_ERR(reg_xceiver);
Andreas Werner555828e2015-03-22 17:35:52 +01001604
Dong Aisheng8c306be2018-12-13 07:08:00 +00001605 if (pdev->dev.of_node) {
Hui Wangafc016d2012-06-28 16:21:34 +08001606 of_property_read_u32(pdev->dev.of_node,
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +02001607 "clock-frequency", &clock_freq);
Dong Aisheng8c306be2018-12-13 07:08:00 +00001608 of_property_read_u8(pdev->dev.of_node,
1609 "fsl,clk-source", &clk_src);
1610 }
holt@sgi.com97efe9a2011-08-16 17:32:23 +00001611
1612 if (!clock_freq) {
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001613 clk_ipg = devm_clk_get(&pdev->dev, "ipg");
1614 if (IS_ERR(clk_ipg)) {
1615 dev_err(&pdev->dev, "no ipg clock defined\n");
Fabio Estevam933e4af2013-07-22 12:41:39 -03001616 return PTR_ERR(clk_ipg);
holt@sgi.com97efe9a2011-08-16 17:32:23 +00001617 }
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001618
1619 clk_per = devm_clk_get(&pdev->dev, "per");
1620 if (IS_ERR(clk_per)) {
1621 dev_err(&pdev->dev, "no per clock defined\n");
Fabio Estevam933e4af2013-07-22 12:41:39 -03001622 return PTR_ERR(clk_per);
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001623 }
Marc Kleine-Budde1a3e5172013-11-25 22:15:20 +01001624 clock_freq = clk_get_rate(clk_per);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001625 }
1626
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001627 irq = platform_get_irq(pdev, 0);
Fabio Estevam933e4af2013-07-22 12:41:39 -03001628 if (irq <= 0)
1629 return -ENODEV;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001630
Joakim Zhanga4721f22019-09-29 08:32:09 +00001631 regs = devm_platform_ioremap_resource(pdev, 0);
Marc Kleine-Budde89af8742015-05-08 09:32:58 +02001632 if (IS_ERR(regs))
1633 return PTR_ERR(regs);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001634
Hui Wang30c1e672012-06-28 16:21:35 +08001635 of_id = of_match_device(flexcan_of_match, &pdev->dev);
1636 if (of_id) {
1637 devtype_data = of_id->data;
Marc Kleine-Budded0873e62014-03-04 22:04:22 +01001638 } else if (platform_get_device_id(pdev)->driver_data) {
Hui Wang30c1e672012-06-28 16:21:35 +08001639 devtype_data = (struct flexcan_devtype_data *)
Marc Kleine-Budded0873e62014-03-04 22:04:22 +01001640 platform_get_device_id(pdev)->driver_data;
Hui Wang30c1e672012-06-28 16:21:35 +08001641 } else {
Fabio Estevam933e4af2013-07-22 12:41:39 -03001642 return -ENODEV;
Hui Wang30c1e672012-06-28 16:21:35 +08001643 }
1644
Fabio Estevam933e4af2013-07-22 12:41:39 -03001645 dev = alloc_candev(sizeof(struct flexcan_priv), 1);
1646 if (!dev)
1647 return -ENOMEM;
1648
Marc Kleine-Budde30164752015-05-10 15:26:58 +02001649 platform_set_drvdata(pdev, dev);
1650 SET_NETDEV_DEV(dev, &pdev->dev);
1651
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001652 dev->netdev_ops = &flexcan_netdev_ops;
1653 dev->irq = irq;
Reuben Dowle9a123492011-11-01 11:18:03 +13001654 dev->flags |= IFF_ECHO;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001655
1656 priv = netdev_priv(dev);
Pankaj Bansal88462d22017-11-24 18:52:08 +05301657
Uwe Kleine-König0e030a32018-04-25 16:50:39 +02001658 if (of_property_read_bool(pdev->dev.of_node, "big-endian") ||
1659 devtype_data->quirks & FLEXCAN_QUIRK_DEFAULT_BIG_ENDIAN) {
Pankaj Bansal88462d22017-11-24 18:52:08 +05301660 priv->read = flexcan_read_be;
1661 priv->write = flexcan_write_be;
1662 } else {
Uwe Kleine-König0e030a32018-04-25 16:50:39 +02001663 priv->read = flexcan_read_le;
1664 priv->write = flexcan_write_le;
Pankaj Bansal88462d22017-11-24 18:52:08 +05301665 }
1666
Aisheng Dongca109892018-11-30 08:53:26 +00001667 priv->dev = &pdev->dev;
holt@sgi.com97efe9a2011-08-16 17:32:23 +00001668 priv->can.clock.freq = clock_freq;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001669 priv->can.bittiming_const = &flexcan_bittiming_const;
1670 priv->can.do_set_mode = flexcan_set_mode;
1671 priv->can.do_get_berr_counter = flexcan_get_berr_counter;
1672 priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK |
1673 CAN_CTRLMODE_LISTENONLY | CAN_CTRLMODE_3_SAMPLES |
1674 CAN_CTRLMODE_BERR_REPORTING;
Marc Kleine-Budde89af8742015-05-08 09:32:58 +02001675 priv->regs = regs;
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001676 priv->clk_ipg = clk_ipg;
1677 priv->clk_per = clk_per;
Dong Aisheng8c306be2018-12-13 07:08:00 +00001678 priv->clk_src = clk_src;
Hui Wang30c1e672012-06-28 16:21:35 +08001679 priv->devtype_data = devtype_data;
Andreas Werner555828e2015-03-22 17:35:52 +01001680 priv->reg_xceiver = reg_xceiver;
Fabio Estevamb7c41142013-06-10 23:12:57 -03001681
Aisheng Dongca109892018-11-30 08:53:26 +00001682 pm_runtime_get_noresume(&pdev->dev);
1683 pm_runtime_set_active(&pdev->dev);
1684 pm_runtime_enable(&pdev->dev);
1685
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001686 err = register_flexcandev(dev);
1687 if (err) {
1688 dev_err(&pdev->dev, "registering netdev failed\n");
1689 goto failed_register;
1690 }
1691
Joakim Zhangee973022019-10-30 06:45:57 +00001692 of_can_transceiver(dev);
Fabio Baltieriadccadb2012-12-18 18:50:58 +01001693 devm_can_led_init(dev);
1694
Aisheng Dongde3578c2018-11-23 08:35:33 +00001695 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_SETUP_STOP_MODE) {
1696 err = flexcan_setup_stop_mode(pdev);
1697 if (err)
1698 dev_dbg(&pdev->dev, "failed to setup stop-mode\n");
1699 }
1700
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001701 return 0;
1702
1703 failed_register:
1704 free_candev(dev);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001705 return err;
1706}
1707
Bill Pemberton3c8ac0f2012-12-03 09:22:44 -05001708static int flexcan_remove(struct platform_device *pdev)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001709{
1710 struct net_device *dev = platform_get_drvdata(pdev);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001711
1712 unregister_flexcandev(dev);
Aisheng Dongca109892018-11-30 08:53:26 +00001713 pm_runtime_disable(&pdev->dev);
Marc Kleine-Budde9a275862010-10-21 05:07:58 +00001714 free_candev(dev);
1715
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001716 return 0;
1717}
1718
Marc Kleine-Budde08c6d352014-03-05 19:10:44 +01001719static int __maybe_unused flexcan_suspend(struct device *device)
Eric Bénard8b5e2182012-05-08 17:12:17 +02001720{
Fabio Estevam588e7a82013-05-20 15:43:43 -03001721 struct net_device *dev = dev_get_drvdata(device);
Eric Bénard8b5e2182012-05-08 17:12:17 +02001722 struct flexcan_priv *priv = netdev_priv(dev);
Aisheng Dongca109892018-11-30 08:53:26 +00001723 int err = 0;
Eric Bénard8b5e2182012-05-08 17:12:17 +02001724
Eric Bénard8b5e2182012-05-08 17:12:17 +02001725 if (netif_running(dev)) {
Aisheng Dongde3578c2018-11-23 08:35:33 +00001726 /* if wakeup is enabled, enter stop mode
1727 * else enter disabled mode.
1728 */
1729 if (device_may_wakeup(device)) {
1730 enable_irq_wake(dev->irq);
Joakim Zhang5f186c22019-07-02 01:45:41 +00001731 err = flexcan_enter_stop_mode(priv);
1732 if (err)
1733 return err;
Aisheng Dongde3578c2018-11-23 08:35:33 +00001734 } else {
1735 err = flexcan_chip_disable(priv);
1736 if (err)
1737 return err;
Aisheng Dongca109892018-11-30 08:53:26 +00001738
1739 err = pm_runtime_force_suspend(device);
Aisheng Dongde3578c2018-11-23 08:35:33 +00001740 }
Eric Bénard8b5e2182012-05-08 17:12:17 +02001741 netif_stop_queue(dev);
1742 netif_device_detach(dev);
1743 }
1744 priv->can.state = CAN_STATE_SLEEPING;
1745
Aisheng Dongca109892018-11-30 08:53:26 +00001746 return err;
Eric Bénard8b5e2182012-05-08 17:12:17 +02001747}
1748
Marc Kleine-Budde08c6d352014-03-05 19:10:44 +01001749static int __maybe_unused flexcan_resume(struct device *device)
Eric Bénard8b5e2182012-05-08 17:12:17 +02001750{
Fabio Estevam588e7a82013-05-20 15:43:43 -03001751 struct net_device *dev = dev_get_drvdata(device);
Eric Bénard8b5e2182012-05-08 17:12:17 +02001752 struct flexcan_priv *priv = netdev_priv(dev);
Aisheng Dongca109892018-11-30 08:53:26 +00001753 int err = 0;
Eric Bénard8b5e2182012-05-08 17:12:17 +02001754
1755 priv->can.state = CAN_STATE_ERROR_ACTIVE;
1756 if (netif_running(dev)) {
1757 netif_device_attach(dev);
1758 netif_start_queue(dev);
Aisheng Dongde3578c2018-11-23 08:35:33 +00001759 if (device_may_wakeup(device)) {
1760 disable_irq_wake(dev->irq);
Sean Nyekjaere7071802019-12-04 11:36:06 +00001761 err = flexcan_exit_stop_mode(priv);
1762 if (err)
1763 return err;
Aisheng Dongde3578c2018-11-23 08:35:33 +00001764 } else {
Aisheng Dongca109892018-11-30 08:53:26 +00001765 err = pm_runtime_force_resume(device);
Aisheng Dongde3578c2018-11-23 08:35:33 +00001766 if (err)
1767 return err;
Aisheng Dongca109892018-11-30 08:53:26 +00001768
1769 err = flexcan_chip_enable(priv);
Aisheng Dongde3578c2018-11-23 08:35:33 +00001770 }
Eric Bénard8b5e2182012-05-08 17:12:17 +02001771 }
Aisheng Dongca109892018-11-30 08:53:26 +00001772
1773 return err;
1774}
1775
1776static int __maybe_unused flexcan_runtime_suspend(struct device *device)
1777{
1778 struct net_device *dev = dev_get_drvdata(device);
1779 struct flexcan_priv *priv = netdev_priv(dev);
1780
1781 flexcan_clks_disable(priv);
1782
Fabio Estevam4de349e2016-08-17 12:41:08 -03001783 return 0;
Eric Bénard8b5e2182012-05-08 17:12:17 +02001784}
Fabio Estevam588e7a82013-05-20 15:43:43 -03001785
Aisheng Dongca109892018-11-30 08:53:26 +00001786static int __maybe_unused flexcan_runtime_resume(struct device *device)
1787{
1788 struct net_device *dev = dev_get_drvdata(device);
1789 struct flexcan_priv *priv = netdev_priv(dev);
1790
1791 return flexcan_clks_enable(priv);
1792}
1793
Aisheng Dongde3578c2018-11-23 08:35:33 +00001794static int __maybe_unused flexcan_noirq_suspend(struct device *device)
1795{
1796 struct net_device *dev = dev_get_drvdata(device);
1797 struct flexcan_priv *priv = netdev_priv(dev);
1798
1799 if (netif_running(dev) && device_may_wakeup(device))
1800 flexcan_enable_wakeup_irq(priv, true);
1801
1802 return 0;
1803}
1804
1805static int __maybe_unused flexcan_noirq_resume(struct device *device)
1806{
1807 struct net_device *dev = dev_get_drvdata(device);
1808 struct flexcan_priv *priv = netdev_priv(dev);
1809
Sean Nyekjaere7071802019-12-04 11:36:06 +00001810 if (netif_running(dev) && device_may_wakeup(device))
Aisheng Dongde3578c2018-11-23 08:35:33 +00001811 flexcan_enable_wakeup_irq(priv, false);
Aisheng Dongde3578c2018-11-23 08:35:33 +00001812
1813 return 0;
1814}
1815
1816static const struct dev_pm_ops flexcan_pm_ops = {
1817 SET_SYSTEM_SLEEP_PM_OPS(flexcan_suspend, flexcan_resume)
Aisheng Dongca109892018-11-30 08:53:26 +00001818 SET_RUNTIME_PM_OPS(flexcan_runtime_suspend, flexcan_runtime_resume, NULL)
Aisheng Dongde3578c2018-11-23 08:35:33 +00001819 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(flexcan_noirq_suspend, flexcan_noirq_resume)
1820};
Eric Bénard8b5e2182012-05-08 17:12:17 +02001821
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001822static struct platform_driver flexcan_driver = {
holt@sgi.comc8aef4c2011-08-16 17:32:22 +00001823 .driver = {
1824 .name = DRV_NAME,
Fabio Estevam588e7a82013-05-20 15:43:43 -03001825 .pm = &flexcan_pm_ops,
holt@sgi.comc8aef4c2011-08-16 17:32:22 +00001826 .of_match_table = flexcan_of_match,
1827 },
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001828 .probe = flexcan_probe,
Bill Pemberton3c8ac0f2012-12-03 09:22:44 -05001829 .remove = flexcan_remove,
Hui Wang30c1e672012-06-28 16:21:35 +08001830 .id_table = flexcan_id_table,
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001831};
1832
Axel Lin871d3372011-11-27 15:42:31 +00001833module_platform_driver(flexcan_driver);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001834
1835MODULE_AUTHOR("Sascha Hauer <kernel@pengutronix.de>, "
1836 "Marc Kleine-Budde <kernel@pengutronix.de>");
1837MODULE_LICENSE("GPL v2");
1838MODULE_DESCRIPTION("CAN port driver for flexcan based chip");