blob: 19403e88daa36203bfce28fba618fe5e32dff345 [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
12#include <linux/netdevice.h>
13#include <linux/can.h>
14#include <linux/can/dev.h>
15#include <linux/can/error.h>
Fabio Baltieriadccadb2012-12-18 18:50:58 +010016#include <linux/can/led.h>
Marc Kleine-Budde30164752015-05-10 15:26:58 +020017#include <linux/can/rx-offload.h>
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +020018#include <linux/clk.h>
19#include <linux/delay.h>
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +020020#include <linux/interrupt.h>
21#include <linux/io.h>
Aisheng Dongde3578c2018-11-23 08:35:33 +000022#include <linux/mfd/syscon.h>
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +020023#include <linux/module.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>
Fabio Estevamb7c41142013-06-10 23:12:57 -030028#include <linux/regulator/consumer.h>
Aisheng Dongde3578c2018-11-23 08:35:33 +000029#include <linux/regmap.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 | \
Aisheng Dongde3578c2018-11-23 08:35:33 +0000137 FLEXCAN_ESR_BOFF_INT | FLEXCAN_ESR_ERR_INT | \
138 FLEXCAN_ESR_WAK_INT)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200139
140/* FLEXCAN interrupt flag register (IFLAG) bits */
David Jander25e92442014-09-03 16:47:22 +0200141/* Errata ERR005829 step7: Reserve first valid MB */
Alexander Steincbffaf72018-10-11 17:01:25 +0200142#define FLEXCAN_TX_MB_RESERVED_OFF_FIFO 8
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200143#define FLEXCAN_TX_MB_RESERVED_OFF_TIMESTAMP 0
Alexander Steincbffaf72018-10-11 17:01:25 +0200144#define FLEXCAN_RX_MB_OFF_TIMESTAMP_FIRST (FLEXCAN_TX_MB_RESERVED_OFF_TIMESTAMP + 1)
Marc Kleine-Budde8ce51392019-03-01 12:17:30 +0100145#define FLEXCAN_IFLAG_MB(x) BIT_ULL(x)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200146#define FLEXCAN_IFLAG_RX_FIFO_OVERFLOW BIT(7)
147#define FLEXCAN_IFLAG_RX_FIFO_WARN BIT(6)
148#define FLEXCAN_IFLAG_RX_FIFO_AVAILABLE BIT(5)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200149
150/* FLEXCAN message buffers */
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200151#define FLEXCAN_MB_CODE_MASK (0xf << 24)
152#define FLEXCAN_MB_CODE_RX_BUSY_BIT (0x1 << 24)
Marc Kleine-Buddec32fe4a2014-09-16 12:39:28 +0200153#define FLEXCAN_MB_CODE_RX_INACTIVE (0x0 << 24)
154#define FLEXCAN_MB_CODE_RX_EMPTY (0x4 << 24)
155#define FLEXCAN_MB_CODE_RX_FULL (0x2 << 24)
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200156#define FLEXCAN_MB_CODE_RX_OVERRUN (0x6 << 24)
Marc Kleine-Buddec32fe4a2014-09-16 12:39:28 +0200157#define FLEXCAN_MB_CODE_RX_RANSWER (0xa << 24)
158
159#define FLEXCAN_MB_CODE_TX_INACTIVE (0x8 << 24)
160#define FLEXCAN_MB_CODE_TX_ABORT (0x9 << 24)
161#define FLEXCAN_MB_CODE_TX_DATA (0xc << 24)
162#define FLEXCAN_MB_CODE_TX_TANSWER (0xe << 24)
163
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200164#define FLEXCAN_MB_CNT_SRR BIT(22)
165#define FLEXCAN_MB_CNT_IDE BIT(21)
166#define FLEXCAN_MB_CNT_RTR BIT(20)
167#define FLEXCAN_MB_CNT_LENGTH(x) (((x) & 0xf) << 16)
168#define FLEXCAN_MB_CNT_TIMESTAMP(x) ((x) & 0xffff)
169
Joakim Zhang247e5352019-01-31 09:37:22 +0000170#define FLEXCAN_TIMEOUT_US (250)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200171
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200172/* FLEXCAN hardware feature flags
Wolfgang Grandeggerbb698ca2012-10-10 21:10:42 +0200173 *
174 * Below is some version info we got:
ZHU Yi (ST-FIR/ENG1-Zhu)da49a802017-09-15 07:03:58 +0000175 * SOC Version IP-Version Glitch- [TR]WRN_INT IRQ Err Memory err RTR re-
176 * Filter? connected? Passive detection ception in MB
Marc Kleine-Budde658f5342017-11-22 13:01:08 +0100177 * MX25 FlexCAN2 03.00.00.00 no no no no no
ZHU Yi (ST-FIR/ENG1-Zhu)da49a802017-09-15 07:03:58 +0000178 * MX28 FlexCAN2 03.00.04.00 yes yes no no no
Marc Kleine-Budde658f5342017-11-22 13:01:08 +0100179 * MX35 FlexCAN2 03.00.00.00 no no no no no
ZHU Yi (ST-FIR/ENG1-Zhu)da49a802017-09-15 07:03:58 +0000180 * MX53 FlexCAN2 03.00.00.00 yes no no no no
181 * MX6s FlexCAN3 10.00.12.00 yes yes no no yes
Marc Kleine-Budde29c64b12017-11-27 09:18:21 +0100182 * VF610 FlexCAN3 ? no yes no yes yes?
Pankaj Bansal99b76682017-11-24 18:52:09 +0530183 * LS1021A FlexCAN2 03.00.04.00 no yes no no yes
Wolfgang Grandeggerbb698ca2012-10-10 21:10:42 +0200184 *
185 * Some SOCs do not have the RX_WARN & TX_WARN interrupt line connected.
186 */
ZHU Yi (ST-FIR/ENG1-Zhu)2f8639b2017-09-15 07:01:23 +0000187#define FLEXCAN_QUIRK_BROKEN_WERR_STATE BIT(1) /* [TR]WRN_INT not connected */
Marc Kleine-Buddef377bff2015-05-08 15:22:36 +0200188#define FLEXCAN_QUIRK_DISABLE_RXFG BIT(2) /* Disable RX FIFO Global mask */
Marc Kleine-Budde9eb7aa82015-09-01 08:57:55 +0200189#define FLEXCAN_QUIRK_ENABLE_EACEN_RRS BIT(3) /* Enable EACEN and RRS bit in ctrl2 */
Marc Kleine-Budde66ddb822017-03-02 15:42:49 +0100190#define FLEXCAN_QUIRK_DISABLE_MECR BIT(4) /* Disable Memory error detection */
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200191#define FLEXCAN_QUIRK_USE_OFF_TIMESTAMP BIT(5) /* Use timestamp based offloading */
ZHU Yi (ST-FIR/ENG1-Zhu)da49a802017-09-15 07:03:58 +0000192#define FLEXCAN_QUIRK_BROKEN_PERR_STATE BIT(6) /* No interrupt for error passive */
Uwe Kleine-König0e030a32018-04-25 16:50:39 +0200193#define FLEXCAN_QUIRK_DEFAULT_BIG_ENDIAN BIT(7) /* default to BE register access */
Aisheng Dongde3578c2018-11-23 08:35:33 +0000194#define FLEXCAN_QUIRK_SETUP_STOP_MODE BIT(8) /* Setup stop mode to support wakeup */
Wolfgang Grandegger4f72e5f2012-09-28 03:17:15 +0000195
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200196/* Structure of the message buffer */
197struct flexcan_mb {
198 u32 can_ctrl;
199 u32 can_id;
Pankaj Bansal05179612018-11-23 22:18:44 +0100200 u32 data[];
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200201};
202
203/* Structure of the hardware registers */
204struct flexcan_regs {
205 u32 mcr; /* 0x00 */
206 u32 ctrl; /* 0x04 */
207 u32 timer; /* 0x08 */
208 u32 _reserved1; /* 0x0c */
209 u32 rxgmask; /* 0x10 */
210 u32 rx14mask; /* 0x14 */
211 u32 rx15mask; /* 0x18 */
212 u32 ecr; /* 0x1c */
213 u32 esr; /* 0x20 */
214 u32 imask2; /* 0x24 */
215 u32 imask1; /* 0x28 */
216 u32 iflag2; /* 0x2c */
217 u32 iflag1; /* 0x30 */
Marc Kleine-Budde62d10862015-08-27 16:01:27 +0200218 union { /* 0x34 */
219 u32 gfwr_mx28; /* MX28, MX53 */
220 u32 ctrl2; /* MX6, VF610 */
221 };
Hui Wang30c1e672012-06-28 16:21:35 +0800222 u32 esr2; /* 0x38 */
223 u32 imeur; /* 0x3c */
224 u32 lrfr; /* 0x40 */
225 u32 crcr; /* 0x44 */
226 u32 rxfgmask; /* 0x48 */
227 u32 rxfir; /* 0x4c */
Stefan Agnercdce8442014-07-15 14:56:21 +0200228 u32 _reserved3[12]; /* 0x50 */
Pankaj Bansal6cbf7602018-08-28 23:19:12 +0530229 u8 mb[2][512]; /* 0x80 */
Marc Kleine-Budde66a6ef02014-09-17 12:50:48 +0200230 /* FIFO-mode:
231 * MB
232 * 0x080...0x08f 0 RX message buffer
Alexandre Belloni68508632020-02-14 15:17:51 +0100233 * 0x090...0x0df 1-5 reserved
Marc Kleine-Budde66a6ef02014-09-17 12:50:48 +0200234 * 0x0e0...0x0ff 6-7 8 entry ID table
235 * (mx25, mx28, mx35, mx53)
236 * 0x0e0...0x2df 6-7..37 8..128 entry ID table
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200237 * size conf'ed via ctrl2::RFFN
Marc Kleine-Budde66a6ef02014-09-17 12:50:48 +0200238 * (mx6, vf610)
239 */
Marc Kleine-Budde62d10862015-08-27 16:01:27 +0200240 u32 _reserved4[256]; /* 0x480 */
241 u32 rximr[64]; /* 0x880 */
242 u32 _reserved5[24]; /* 0x980 */
243 u32 gfwr_mx6; /* 0x9e0 - MX6 */
244 u32 _reserved6[63]; /* 0x9e4 */
Stefan Agnercdce8442014-07-15 14:56:21 +0200245 u32 mecr; /* 0xae0 */
246 u32 erriar; /* 0xae4 */
247 u32 erridpr; /* 0xae8 */
248 u32 errippr; /* 0xaec */
249 u32 rerrar; /* 0xaf0 */
250 u32 rerrdr; /* 0xaf4 */
251 u32 rerrsynr; /* 0xaf8 */
252 u32 errsr; /* 0xafc */
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200253};
254
Hui Wang30c1e672012-06-28 16:21:35 +0800255struct flexcan_devtype_data {
Marc Kleine-Buddef377bff2015-05-08 15:22:36 +0200256 u32 quirks; /* quirks needed for different IP cores */
Hui Wang30c1e672012-06-28 16:21:35 +0800257};
258
Aisheng Dongde3578c2018-11-23 08:35:33 +0000259struct flexcan_stop_mode {
260 struct regmap *gpr;
261 u8 req_gpr;
262 u8 req_bit;
263 u8 ack_gpr;
264 u8 ack_bit;
265};
266
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200267struct flexcan_priv {
268 struct can_priv can;
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200269 struct can_rx_offload offload;
Aisheng Dongca109892018-11-30 08:53:26 +0000270 struct device *dev;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200271
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200272 struct flexcan_regs __iomem *regs;
Pankaj Bansal05179612018-11-23 22:18:44 +0100273 struct flexcan_mb __iomem *tx_mb;
Marc Kleine-Buddeb93917c2015-07-12 00:47:47 +0200274 struct flexcan_mb __iomem *tx_mb_reserved;
Pankaj Bansal05179612018-11-23 22:18:44 +0100275 u8 tx_mb_idx;
276 u8 mb_count;
277 u8 mb_size;
Dong Aisheng8c306be2018-12-13 07:08:00 +0000278 u8 clk_src; /* clock source of CAN Protocol Engine */
279
Marc Kleine-Budde8ce51392019-03-01 12:17:30 +0100280 u64 rx_mask;
Marc Kleine-Budde0ca64f022019-03-01 13:54:19 +0100281 u64 tx_mask;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200282 u32 reg_ctrl_default;
283
Steffen Trumtrar3d42a372012-07-17 16:14:34 +0200284 struct clk *clk_ipg;
285 struct clk *clk_per;
Marc Kleine-Buddedda0b3b2012-07-13 14:52:48 +0200286 const struct flexcan_devtype_data *devtype_data;
Fabio Estevamb7c41142013-06-10 23:12:57 -0300287 struct regulator *reg_xceiver;
Aisheng Dongde3578c2018-11-23 08:35:33 +0000288 struct flexcan_stop_mode stm;
Pankaj Bansal88462d22017-11-24 18:52:08 +0530289
290 /* Read and Write APIs */
291 u32 (*read)(void __iomem *addr);
292 void (*write)(u32 val, void __iomem *addr);
Hui Wang30c1e672012-06-28 16:21:35 +0800293};
294
Marc Kleine-Buddea3c11a72016-07-04 14:45:44 +0200295static const struct flexcan_devtype_data fsl_p1010_devtype_data = {
ZHU Yi (ST-FIR/ENG1-Zhu)fb5b91d62017-09-15 07:09:37 +0000296 .quirks = FLEXCAN_QUIRK_BROKEN_WERR_STATE |
Uwe Kleine-König0e030a32018-04-25 16:50:39 +0200297 FLEXCAN_QUIRK_BROKEN_PERR_STATE |
298 FLEXCAN_QUIRK_DEFAULT_BIG_ENDIAN,
299};
300
301static const struct flexcan_devtype_data fsl_imx25_devtype_data = {
302 .quirks = FLEXCAN_QUIRK_BROKEN_WERR_STATE |
ZHU Yi (ST-FIR/ENG1-Zhu)fb5b91d62017-09-15 07:09:37 +0000303 FLEXCAN_QUIRK_BROKEN_PERR_STATE,
Hui Wang30c1e672012-06-28 16:21:35 +0800304};
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200305
ZHU Yi (ST-FIR/ENG1-Zhu)083c5572017-09-15 07:08:23 +0000306static const struct flexcan_devtype_data fsl_imx28_devtype_data = {
307 .quirks = FLEXCAN_QUIRK_BROKEN_PERR_STATE,
308};
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200309
Marc Kleine-Buddea3c11a72016-07-04 14:45:44 +0200310static const struct flexcan_devtype_data fsl_imx6q_devtype_data = {
Marc Kleine-Budde096de072015-09-01 10:28:46 +0200311 .quirks = FLEXCAN_QUIRK_DISABLE_RXFG | FLEXCAN_QUIRK_ENABLE_EACEN_RRS |
Aisheng Dongde3578c2018-11-23 08:35:33 +0000312 FLEXCAN_QUIRK_USE_OFF_TIMESTAMP | FLEXCAN_QUIRK_BROKEN_PERR_STATE |
313 FLEXCAN_QUIRK_SETUP_STOP_MODE,
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200314};
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200315
Marc Kleine-Buddea3c11a72016-07-04 14:45:44 +0200316static const struct flexcan_devtype_data fsl_vf610_devtype_data = {
Marc Kleine-Budde9eb7aa82015-09-01 08:57:55 +0200317 .quirks = FLEXCAN_QUIRK_DISABLE_RXFG | FLEXCAN_QUIRK_ENABLE_EACEN_RRS |
Marc Kleine-Budde29c64b12017-11-27 09:18:21 +0100318 FLEXCAN_QUIRK_DISABLE_MECR | FLEXCAN_QUIRK_USE_OFF_TIMESTAMP |
319 FLEXCAN_QUIRK_BROKEN_PERR_STATE,
Stefan Agnercdce8442014-07-15 14:56:21 +0200320};
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200321
Pankaj Bansal99b76682017-11-24 18:52:09 +0530322static const struct flexcan_devtype_data fsl_ls1021a_r2_devtype_data = {
323 .quirks = FLEXCAN_QUIRK_DISABLE_RXFG | FLEXCAN_QUIRK_ENABLE_EACEN_RRS |
324 FLEXCAN_QUIRK_DISABLE_MECR | FLEXCAN_QUIRK_BROKEN_PERR_STATE |
325 FLEXCAN_QUIRK_USE_OFF_TIMESTAMP,
326};
327
Marc Kleine-Budde194b9a42012-07-16 12:58:31 +0200328static const struct can_bittiming_const flexcan_bittiming_const = {
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200329 .name = DRV_NAME,
330 .tseg1_min = 4,
331 .tseg1_max = 16,
332 .tseg2_min = 2,
333 .tseg2_max = 8,
334 .sjw_max = 4,
335 .brp_min = 1,
336 .brp_max = 256,
337 .brp_inc = 1,
338};
339
Pankaj Bansal88462d22017-11-24 18:52:08 +0530340/* FlexCAN module is essentially modelled as a little-endian IP in most
341 * SoCs, i.e the registers as well as the message buffer areas are
342 * implemented in a little-endian fashion.
343 *
344 * However there are some SoCs (e.g. LS1021A) which implement the FlexCAN
345 * module in a big-endian fashion (i.e the registers as well as the
346 * message buffer areas are implemented in a big-endian way).
347 *
348 * In addition, the FlexCAN module can be found on SoCs having ARM or
349 * PPC cores. So, we need to abstract off the register read/write
350 * functions, ensuring that these cater to all the combinations of module
351 * endianness and underlying CPU endianness.
holt@sgi.com61e271e2011-08-16 17:32:20 +0000352 */
Pankaj Bansal88462d22017-11-24 18:52:08 +0530353static inline u32 flexcan_read_be(void __iomem *addr)
holt@sgi.com61e271e2011-08-16 17:32:20 +0000354{
Pankaj Bansal88462d22017-11-24 18:52:08 +0530355 return ioread32be(addr);
holt@sgi.com61e271e2011-08-16 17:32:20 +0000356}
357
Pankaj Bansal88462d22017-11-24 18:52:08 +0530358static inline void flexcan_write_be(u32 val, void __iomem *addr)
holt@sgi.com61e271e2011-08-16 17:32:20 +0000359{
Pankaj Bansal88462d22017-11-24 18:52:08 +0530360 iowrite32be(val, addr);
holt@sgi.com61e271e2011-08-16 17:32:20 +0000361}
362
Pankaj Bansal88462d22017-11-24 18:52:08 +0530363static inline u32 flexcan_read_le(void __iomem *addr)
holt@sgi.com61e271e2011-08-16 17:32:20 +0000364{
Pankaj Bansal88462d22017-11-24 18:52:08 +0530365 return ioread32(addr);
holt@sgi.com61e271e2011-08-16 17:32:20 +0000366}
Pankaj Bansal88462d22017-11-24 18:52:08 +0530367
368static inline void flexcan_write_le(u32 val, void __iomem *addr)
369{
370 iowrite32(val, addr);
371}
holt@sgi.com61e271e2011-08-16 17:32:20 +0000372
Pankaj Bansal05179612018-11-23 22:18:44 +0100373static struct flexcan_mb __iomem *flexcan_get_mb(const struct flexcan_priv *priv,
374 u8 mb_index)
375{
Pankaj Bansal6cbf7602018-08-28 23:19:12 +0530376 u8 bank_size;
377 bool bank;
378
Pankaj Bansal05179612018-11-23 22:18:44 +0100379 if (WARN_ON(mb_index >= priv->mb_count))
380 return NULL;
381
Pankaj Bansal6cbf7602018-08-28 23:19:12 +0530382 bank_size = sizeof(priv->regs->mb[0]) / priv->mb_size;
383
384 bank = mb_index >= bank_size;
385 if (bank)
386 mb_index -= bank_size;
387
Pankaj Bansal05179612018-11-23 22:18:44 +0100388 return (struct flexcan_mb __iomem *)
Pankaj Bansal6cbf7602018-08-28 23:19:12 +0530389 (&priv->regs->mb[bank][priv->mb_size * mb_index]);
Pankaj Bansal05179612018-11-23 22:18:44 +0100390}
391
Joakim Zhangb7603d02019-12-04 11:36:11 +0000392static int flexcan_low_power_enter_ack(struct flexcan_priv *priv)
393{
394 struct flexcan_regs __iomem *regs = priv->regs;
395 unsigned int timeout = FLEXCAN_TIMEOUT_US / 10;
396
397 while (timeout-- && !(priv->read(&regs->mcr) & FLEXCAN_MCR_LPM_ACK))
398 udelay(10);
399
400 if (!(priv->read(&regs->mcr) & FLEXCAN_MCR_LPM_ACK))
401 return -ETIMEDOUT;
402
403 return 0;
404}
405
406static int flexcan_low_power_exit_ack(struct flexcan_priv *priv)
407{
408 struct flexcan_regs __iomem *regs = priv->regs;
409 unsigned int timeout = FLEXCAN_TIMEOUT_US / 10;
410
411 while (timeout-- && (priv->read(&regs->mcr) & FLEXCAN_MCR_LPM_ACK))
412 udelay(10);
413
414 if (priv->read(&regs->mcr) & FLEXCAN_MCR_LPM_ACK)
415 return -ETIMEDOUT;
416
417 return 0;
418}
419
Aisheng Dongde3578c2018-11-23 08:35:33 +0000420static void flexcan_enable_wakeup_irq(struct flexcan_priv *priv, bool enable)
421{
422 struct flexcan_regs __iomem *regs = priv->regs;
423 u32 reg_mcr;
424
425 reg_mcr = priv->read(&regs->mcr);
426
427 if (enable)
428 reg_mcr |= FLEXCAN_MCR_WAK_MSK;
429 else
430 reg_mcr &= ~FLEXCAN_MCR_WAK_MSK;
431
432 priv->write(reg_mcr, &regs->mcr);
433}
434
Joakim Zhang5f186c22019-07-02 01:45:41 +0000435static inline int flexcan_enter_stop_mode(struct flexcan_priv *priv)
Aisheng Dongde3578c2018-11-23 08:35:33 +0000436{
437 struct flexcan_regs __iomem *regs = priv->regs;
438 u32 reg_mcr;
439
440 reg_mcr = priv->read(&regs->mcr);
441 reg_mcr |= FLEXCAN_MCR_SLF_WAK;
442 priv->write(reg_mcr, &regs->mcr);
443
444 /* enable stop request */
445 regmap_update_bits(priv->stm.gpr, priv->stm.req_gpr,
446 1 << priv->stm.req_bit, 1 << priv->stm.req_bit);
Joakim Zhang5f186c22019-07-02 01:45:41 +0000447
Joakim Zhang048e3a342019-12-04 11:36:14 +0000448 return flexcan_low_power_enter_ack(priv);
Aisheng Dongde3578c2018-11-23 08:35:33 +0000449}
450
Joakim Zhang5f186c22019-07-02 01:45:41 +0000451static inline int flexcan_exit_stop_mode(struct flexcan_priv *priv)
Aisheng Dongde3578c2018-11-23 08:35:33 +0000452{
453 struct flexcan_regs __iomem *regs = priv->regs;
454 u32 reg_mcr;
455
456 /* remove stop request */
457 regmap_update_bits(priv->stm.gpr, priv->stm.req_gpr,
458 1 << priv->stm.req_bit, 0);
459
Joakim Zhang5f186c22019-07-02 01:45:41 +0000460
Aisheng Dongde3578c2018-11-23 08:35:33 +0000461 reg_mcr = priv->read(&regs->mcr);
462 reg_mcr &= ~FLEXCAN_MCR_SLF_WAK;
463 priv->write(reg_mcr, &regs->mcr);
Joakim Zhang5f186c22019-07-02 01:45:41 +0000464
Joakim Zhang048e3a342019-12-04 11:36:14 +0000465 return flexcan_low_power_exit_ack(priv);
Aisheng Dongde3578c2018-11-23 08:35:33 +0000466}
467
ZHU Yi (ST-FIR/ENG1-Zhu)da49a802017-09-15 07:03:58 +0000468static inline void flexcan_error_irq_enable(const struct flexcan_priv *priv)
469{
470 struct flexcan_regs __iomem *regs = priv->regs;
471 u32 reg_ctrl = (priv->reg_ctrl_default | FLEXCAN_CTRL_ERR_MSK);
472
Pankaj Bansal88462d22017-11-24 18:52:08 +0530473 priv->write(reg_ctrl, &regs->ctrl);
ZHU Yi (ST-FIR/ENG1-Zhu)da49a802017-09-15 07:03:58 +0000474}
475
476static inline void flexcan_error_irq_disable(const struct flexcan_priv *priv)
477{
478 struct flexcan_regs __iomem *regs = priv->regs;
479 u32 reg_ctrl = (priv->reg_ctrl_default & ~FLEXCAN_CTRL_ERR_MSK);
480
Pankaj Bansal88462d22017-11-24 18:52:08 +0530481 priv->write(reg_ctrl, &regs->ctrl);
ZHU Yi (ST-FIR/ENG1-Zhu)da49a802017-09-15 07:03:58 +0000482}
483
Aisheng Dongca109892018-11-30 08:53:26 +0000484static int flexcan_clks_enable(const struct flexcan_priv *priv)
485{
486 int err;
487
488 err = clk_prepare_enable(priv->clk_ipg);
489 if (err)
490 return err;
491
492 err = clk_prepare_enable(priv->clk_per);
493 if (err)
494 clk_disable_unprepare(priv->clk_ipg);
495
496 return err;
497}
498
499static void flexcan_clks_disable(const struct flexcan_priv *priv)
500{
501 clk_disable_unprepare(priv->clk_per);
502 clk_disable_unprepare(priv->clk_ipg);
503}
504
Marc Kleine-Buddef0036982014-02-28 17:18:27 +0100505static inline int flexcan_transceiver_enable(const struct flexcan_priv *priv)
506{
507 if (!priv->reg_xceiver)
508 return 0;
509
510 return regulator_enable(priv->reg_xceiver);
511}
512
513static inline int flexcan_transceiver_disable(const struct flexcan_priv *priv)
514{
515 if (!priv->reg_xceiver)
516 return 0;
517
518 return regulator_disable(priv->reg_xceiver);
519}
520
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100521static int flexcan_chip_enable(struct flexcan_priv *priv)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200522{
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200523 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200524 u32 reg;
525
Pankaj Bansal88462d22017-11-24 18:52:08 +0530526 reg = priv->read(&regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200527 reg &= ~FLEXCAN_MCR_MDIS;
Pankaj Bansal88462d22017-11-24 18:52:08 +0530528 priv->write(reg, &regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200529
Joakim Zhangb7603d02019-12-04 11:36:11 +0000530 return flexcan_low_power_exit_ack(priv);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200531}
532
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100533static int flexcan_chip_disable(struct flexcan_priv *priv)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200534{
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200535 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200536 u32 reg;
537
Pankaj Bansal88462d22017-11-24 18:52:08 +0530538 reg = priv->read(&regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200539 reg |= FLEXCAN_MCR_MDIS;
Pankaj Bansal88462d22017-11-24 18:52:08 +0530540 priv->write(reg, &regs->mcr);
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100541
Joakim Zhangb7603d02019-12-04 11:36:11 +0000542 return flexcan_low_power_enter_ack(priv);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200543}
544
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100545static int flexcan_chip_freeze(struct flexcan_priv *priv)
546{
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200547 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100548 unsigned int timeout = 1000 * 1000 * 10 / priv->can.bittiming.bitrate;
549 u32 reg;
550
Pankaj Bansal88462d22017-11-24 18:52:08 +0530551 reg = priv->read(&regs->mcr);
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100552 reg |= FLEXCAN_MCR_HALT;
Pankaj Bansal88462d22017-11-24 18:52:08 +0530553 priv->write(reg, &regs->mcr);
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100554
Pankaj Bansal88462d22017-11-24 18:52:08 +0530555 while (timeout-- && !(priv->read(&regs->mcr) & FLEXCAN_MCR_FRZ_ACK))
David Jander8badd652014-08-27 12:02:16 +0200556 udelay(100);
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100557
Pankaj Bansal88462d22017-11-24 18:52:08 +0530558 if (!(priv->read(&regs->mcr) & FLEXCAN_MCR_FRZ_ACK))
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100559 return -ETIMEDOUT;
560
561 return 0;
562}
563
564static int flexcan_chip_unfreeze(struct flexcan_priv *priv)
565{
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200566 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100567 unsigned int timeout = FLEXCAN_TIMEOUT_US / 10;
568 u32 reg;
569
Pankaj Bansal88462d22017-11-24 18:52:08 +0530570 reg = priv->read(&regs->mcr);
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100571 reg &= ~FLEXCAN_MCR_HALT;
Pankaj Bansal88462d22017-11-24 18:52:08 +0530572 priv->write(reg, &regs->mcr);
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100573
Pankaj Bansal88462d22017-11-24 18:52:08 +0530574 while (timeout-- && (priv->read(&regs->mcr) & FLEXCAN_MCR_FRZ_ACK))
David Jander8badd652014-08-27 12:02:16 +0200575 udelay(10);
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100576
Pankaj Bansal88462d22017-11-24 18:52:08 +0530577 if (priv->read(&regs->mcr) & FLEXCAN_MCR_FRZ_ACK)
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100578 return -ETIMEDOUT;
579
580 return 0;
581}
582
Marc Kleine-Budde4b5b8222014-02-28 15:16:59 +0100583static int flexcan_chip_softreset(struct flexcan_priv *priv)
584{
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200585 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Budde4b5b8222014-02-28 15:16:59 +0100586 unsigned int timeout = FLEXCAN_TIMEOUT_US / 10;
587
Pankaj Bansal88462d22017-11-24 18:52:08 +0530588 priv->write(FLEXCAN_MCR_SOFTRST, &regs->mcr);
589 while (timeout-- && (priv->read(&regs->mcr) & FLEXCAN_MCR_SOFTRST))
David Jander8badd652014-08-27 12:02:16 +0200590 udelay(10);
Marc Kleine-Budde4b5b8222014-02-28 15:16:59 +0100591
Pankaj Bansal88462d22017-11-24 18:52:08 +0530592 if (priv->read(&regs->mcr) & FLEXCAN_MCR_SOFTRST)
Marc Kleine-Budde4b5b8222014-02-28 15:16:59 +0100593 return -ETIMEDOUT;
594
595 return 0;
596}
597
Stefan Agnerec56acf2014-07-15 14:56:20 +0200598static int __flexcan_get_berr_counter(const struct net_device *dev,
599 struct can_berr_counter *bec)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200600{
601 const struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200602 struct flexcan_regs __iomem *regs = priv->regs;
Pankaj Bansal88462d22017-11-24 18:52:08 +0530603 u32 reg = priv->read(&regs->ecr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200604
605 bec->txerr = (reg >> 0) & 0xff;
606 bec->rxerr = (reg >> 8) & 0xff;
607
608 return 0;
609}
610
Stefan Agnerec56acf2014-07-15 14:56:20 +0200611static int flexcan_get_berr_counter(const struct net_device *dev,
612 struct can_berr_counter *bec)
613{
614 const struct flexcan_priv *priv = netdev_priv(dev);
615 int err;
616
Aisheng Dongca109892018-11-30 08:53:26 +0000617 err = pm_runtime_get_sync(priv->dev);
618 if (err < 0)
Stefan Agnerec56acf2014-07-15 14:56:20 +0200619 return err;
620
Stefan Agnerec56acf2014-07-15 14:56:20 +0200621 err = __flexcan_get_berr_counter(dev, bec);
622
Aisheng Dongca109892018-11-30 08:53:26 +0000623 pm_runtime_put(priv->dev);
Stefan Agnerec56acf2014-07-15 14:56:20 +0200624
625 return err;
626}
627
Marc Kleine-Buddefb1e13e62018-04-26 23:13:38 +0200628static netdev_tx_t flexcan_start_xmit(struct sk_buff *skb, struct net_device *dev)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200629{
630 const struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200631 struct can_frame *cf = (struct can_frame *)skb->data;
632 u32 can_id;
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200633 u32 data;
Marc Kleine-Budde10d089b2014-09-23 11:18:11 +0200634 u32 ctrl = FLEXCAN_MB_CODE_TX_DATA | (cf->can_dlc << 16);
Pankaj Bansal05179612018-11-23 22:18:44 +0100635 int i;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200636
637 if (can_dropped_invalid_skb(dev, skb))
638 return NETDEV_TX_OK;
639
640 netif_stop_queue(dev);
641
642 if (cf->can_id & CAN_EFF_FLAG) {
643 can_id = cf->can_id & CAN_EFF_MASK;
644 ctrl |= FLEXCAN_MB_CNT_IDE | FLEXCAN_MB_CNT_SRR;
645 } else {
646 can_id = (cf->can_id & CAN_SFF_MASK) << 18;
647 }
648
649 if (cf->can_id & CAN_RTR_FLAG)
650 ctrl |= FLEXCAN_MB_CNT_RTR;
651
Pankaj Bansal05179612018-11-23 22:18:44 +0100652 for (i = 0; i < cf->can_dlc; i += sizeof(u32)) {
653 data = be32_to_cpup((__be32 *)&cf->data[i]);
654 priv->write(data, &priv->tx_mb->data[i / sizeof(u32)]);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200655 }
656
Reuben Dowle9a123492011-11-01 11:18:03 +1300657 can_put_echo_skb(skb, dev, 0);
658
Pankaj Bansal05179612018-11-23 22:18:44 +0100659 priv->write(can_id, &priv->tx_mb->can_id);
660 priv->write(ctrl, &priv->tx_mb->can_ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200661
David Jander25e92442014-09-03 16:47:22 +0200662 /* Errata ERR005829 step8:
663 * Write twice INACTIVE(0x8) code to first MB.
664 */
Pankaj Bansal88462d22017-11-24 18:52:08 +0530665 priv->write(FLEXCAN_MB_CODE_TX_INACTIVE,
Marc Kleine-Budde9dc1ee12018-11-12 15:33:57 +0100666 &priv->tx_mb_reserved->can_ctrl);
Pankaj Bansal88462d22017-11-24 18:52:08 +0530667 priv->write(FLEXCAN_MB_CODE_TX_INACTIVE,
Marc Kleine-Budde9dc1ee12018-11-12 15:33:57 +0100668 &priv->tx_mb_reserved->can_ctrl);
David Jander25e92442014-09-03 16:47:22 +0200669
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200670 return NETDEV_TX_OK;
671}
672
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200673static void flexcan_irq_bus_err(struct net_device *dev, u32 reg_esr)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200674{
675 struct flexcan_priv *priv = netdev_priv(dev);
Oleksij Rempeld788905f2018-09-18 11:40:41 +0200676 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddea5c02f662017-01-18 11:38:26 +0100677 struct sk_buff *skb;
678 struct can_frame *cf;
Marc Kleine-Budded166f562017-01-17 17:33:46 +0100679 bool rx_errors = false, tx_errors = false;
Oleksij Rempeld788905f2018-09-18 11:40:41 +0200680 u32 timestamp;
Marc Kleine-Budde75812432019-07-15 20:53:08 +0200681 int err;
Oleksij Rempeld788905f2018-09-18 11:40:41 +0200682
683 timestamp = priv->read(&regs->timer) << 16;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200684
Marc Kleine-Buddea5c02f662017-01-18 11:38:26 +0100685 skb = alloc_can_err_skb(dev, &cf);
686 if (unlikely(!skb))
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200687 return;
Marc Kleine-Buddea5c02f662017-01-18 11:38:26 +0100688
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200689 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
690
691 if (reg_esr & FLEXCAN_ESR_BIT1_ERR) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100692 netdev_dbg(dev, "BIT1_ERR irq\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200693 cf->data[2] |= CAN_ERR_PROT_BIT1;
Marc Kleine-Budded166f562017-01-17 17:33:46 +0100694 tx_errors = true;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200695 }
696 if (reg_esr & FLEXCAN_ESR_BIT0_ERR) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100697 netdev_dbg(dev, "BIT0_ERR irq\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200698 cf->data[2] |= CAN_ERR_PROT_BIT0;
Marc Kleine-Budded166f562017-01-17 17:33:46 +0100699 tx_errors = true;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200700 }
701 if (reg_esr & FLEXCAN_ESR_ACK_ERR) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100702 netdev_dbg(dev, "ACK_ERR irq\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200703 cf->can_id |= CAN_ERR_ACK;
Oliver Hartkoppffd461f2015-11-21 18:41:20 +0100704 cf->data[3] = CAN_ERR_PROT_LOC_ACK;
Marc Kleine-Budded166f562017-01-17 17:33:46 +0100705 tx_errors = true;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200706 }
707 if (reg_esr & FLEXCAN_ESR_CRC_ERR) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100708 netdev_dbg(dev, "CRC_ERR irq\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200709 cf->data[2] |= CAN_ERR_PROT_BIT;
Oliver Hartkoppffd461f2015-11-21 18:41:20 +0100710 cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ;
Marc Kleine-Budded166f562017-01-17 17:33:46 +0100711 rx_errors = true;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200712 }
713 if (reg_esr & FLEXCAN_ESR_FRM_ERR) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100714 netdev_dbg(dev, "FRM_ERR irq\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200715 cf->data[2] |= CAN_ERR_PROT_FORM;
Marc Kleine-Budded166f562017-01-17 17:33:46 +0100716 rx_errors = true;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200717 }
718 if (reg_esr & FLEXCAN_ESR_STF_ERR) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100719 netdev_dbg(dev, "STF_ERR irq\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200720 cf->data[2] |= CAN_ERR_PROT_STUFF;
Marc Kleine-Budded166f562017-01-17 17:33:46 +0100721 rx_errors = true;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200722 }
723
724 priv->can.can_stats.bus_error++;
725 if (rx_errors)
726 dev->stats.rx_errors++;
727 if (tx_errors)
728 dev->stats.tx_errors++;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200729
Marc Kleine-Budde75812432019-07-15 20:53:08 +0200730 err = can_rx_offload_queue_sorted(&priv->offload, skb, timestamp);
731 if (err)
732 dev->stats.rx_fifo_errors++;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200733}
734
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200735static void flexcan_irq_state(struct net_device *dev, u32 reg_esr)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200736{
737 struct flexcan_priv *priv = netdev_priv(dev);
Oleksij Rempeld788905f2018-09-18 11:40:41 +0200738 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200739 struct sk_buff *skb;
740 struct can_frame *cf;
Marc Kleine-Budde238443d2017-01-18 11:25:41 +0100741 enum can_state new_state, rx_state, tx_state;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200742 int flt;
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000743 struct can_berr_counter bec;
Oleksij Rempeld788905f2018-09-18 11:40:41 +0200744 u32 timestamp;
Marc Kleine-Budde75812432019-07-15 20:53:08 +0200745 int err;
Oleksij Rempeld788905f2018-09-18 11:40:41 +0200746
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200747 flt = reg_esr & FLEXCAN_ESR_FLT_CONF_MASK;
748 if (likely(flt == FLEXCAN_ESR_FLT_CONF_ACTIVE)) {
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000749 tx_state = unlikely(reg_esr & FLEXCAN_ESR_TX_WRN) ?
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200750 CAN_STATE_ERROR_WARNING : CAN_STATE_ERROR_ACTIVE;
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000751 rx_state = unlikely(reg_esr & FLEXCAN_ESR_RX_WRN) ?
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200752 CAN_STATE_ERROR_WARNING : CAN_STATE_ERROR_ACTIVE;
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000753 new_state = max(tx_state, rx_state);
Andri Yngvason258ce802015-03-17 13:03:09 +0000754 } else {
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000755 __flexcan_get_berr_counter(dev, &bec);
Andri Yngvason258ce802015-03-17 13:03:09 +0000756 new_state = flt == FLEXCAN_ESR_FLT_CONF_PASSIVE ?
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200757 CAN_STATE_ERROR_PASSIVE : CAN_STATE_BUS_OFF;
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000758 rx_state = bec.rxerr >= bec.txerr ? new_state : 0;
759 tx_state = bec.rxerr <= bec.txerr ? new_state : 0;
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000760 }
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200761
762 /* state hasn't changed */
763 if (likely(new_state == priv->can.state))
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200764 return;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200765
Marc Kleine-Budde58ed8e72019-10-09 15:15:37 +0200766 timestamp = priv->read(&regs->timer) << 16;
767
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200768 skb = alloc_can_err_skb(dev, &cf);
769 if (unlikely(!skb))
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200770 return;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200771
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000772 can_change_state(dev, cf, tx_state, rx_state);
773
774 if (unlikely(new_state == CAN_STATE_BUS_OFF))
775 can_bus_off(dev);
776
Marc Kleine-Budde75812432019-07-15 20:53:08 +0200777 err = can_rx_offload_queue_sorted(&priv->offload, skb, timestamp);
778 if (err)
779 dev->stats.rx_fifo_errors++;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200780}
781
Marc Kleine-Budded3a51502019-03-01 15:38:05 +0100782static inline u64 flexcan_read64_mask(struct flexcan_priv *priv, void __iomem *addr, u64 mask)
783{
784 u64 reg = 0;
785
786 if (upper_32_bits(mask))
787 reg = (u64)priv->read(addr - 4) << 32;
788 if (lower_32_bits(mask))
789 reg |= priv->read(addr);
790
791 return reg & mask;
792}
793
Marc Kleine-Buddeb87c28b72019-03-01 15:38:05 +0100794static inline void flexcan_write64(struct flexcan_priv *priv, u64 val, void __iomem *addr)
795{
796 if (upper_32_bits(val))
797 priv->write(upper_32_bits(val), addr - 4);
798 if (lower_32_bits(val))
799 priv->write(lower_32_bits(val), addr);
800}
801
Marc Kleine-Budded3a51502019-03-01 15:38:05 +0100802static inline u64 flexcan_read_reg_iflag_rx(struct flexcan_priv *priv)
803{
804 return flexcan_read64_mask(priv, &priv->regs->iflag1, priv->rx_mask);
805}
806
Marc Kleine-Buddeb87c28b72019-03-01 15:38:05 +0100807static inline u64 flexcan_read_reg_iflag_tx(struct flexcan_priv *priv)
808{
809 return flexcan_read64_mask(priv, &priv->regs->iflag1, priv->tx_mask);
810}
811
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200812static inline struct flexcan_priv *rx_offload_to_priv(struct can_rx_offload *offload)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200813{
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200814 return container_of(offload, struct flexcan_priv, offload);
815}
816
Joakim Zhang4e9c9482019-07-12 08:02:38 +0000817static struct sk_buff *flexcan_mailbox_read(struct can_rx_offload *offload,
818 unsigned int n, u32 *timestamp,
819 bool drop)
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200820{
821 struct flexcan_priv *priv = rx_offload_to_priv(offload);
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200822 struct flexcan_regs __iomem *regs = priv->regs;
Pankaj Bansal05179612018-11-23 22:18:44 +0100823 struct flexcan_mb __iomem *mb;
Joakim Zhang4e9c9482019-07-12 08:02:38 +0000824 struct sk_buff *skb;
825 struct can_frame *cf;
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200826 u32 reg_ctrl, reg_id, reg_iflag1;
Pankaj Bansal05179612018-11-23 22:18:44 +0100827 int i;
828
Joakim Zhang4e9c9482019-07-12 08:02:38 +0000829 if (unlikely(drop)) {
830 skb = ERR_PTR(-ENOBUFS);
831 goto mark_as_read;
832 }
833
Pankaj Bansal05179612018-11-23 22:18:44 +0100834 mb = flexcan_get_mb(priv, n);
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200835
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200836 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP) {
837 u32 code;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200838
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200839 do {
Pankaj Bansal88462d22017-11-24 18:52:08 +0530840 reg_ctrl = priv->read(&mb->can_ctrl);
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200841 } while (reg_ctrl & FLEXCAN_MB_CODE_RX_BUSY_BIT);
842
843 /* is this MB empty? */
844 code = reg_ctrl & FLEXCAN_MB_CODE_MASK;
845 if ((code != FLEXCAN_MB_CODE_RX_FULL) &&
846 (code != FLEXCAN_MB_CODE_RX_OVERRUN))
Joakim Zhang4e9c9482019-07-12 08:02:38 +0000847 return NULL;
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200848
849 if (code == FLEXCAN_MB_CODE_RX_OVERRUN) {
850 /* This MB was overrun, we lost data */
851 offload->dev->stats.rx_over_errors++;
852 offload->dev->stats.rx_errors++;
853 }
854 } else {
Pankaj Bansal88462d22017-11-24 18:52:08 +0530855 reg_iflag1 = priv->read(&regs->iflag1);
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200856 if (!(reg_iflag1 & FLEXCAN_IFLAG_RX_FIFO_AVAILABLE))
Joakim Zhang4e9c9482019-07-12 08:02:38 +0000857 return NULL;
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200858
Pankaj Bansal88462d22017-11-24 18:52:08 +0530859 reg_ctrl = priv->read(&mb->can_ctrl);
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200860 }
861
Joakim Zhang4e9c9482019-07-12 08:02:38 +0000862 skb = alloc_can_skb(offload->dev, &cf);
863 if (!skb) {
864 skb = ERR_PTR(-ENOMEM);
865 goto mark_as_read;
866 }
867
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200868 /* increase timstamp to full 32 bit */
869 *timestamp = reg_ctrl << 16;
870
Pankaj Bansal88462d22017-11-24 18:52:08 +0530871 reg_id = priv->read(&mb->can_id);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200872 if (reg_ctrl & FLEXCAN_MB_CNT_IDE)
873 cf->can_id = ((reg_id >> 0) & CAN_EFF_MASK) | CAN_EFF_FLAG;
874 else
875 cf->can_id = (reg_id >> 18) & CAN_SFF_MASK;
876
877 if (reg_ctrl & FLEXCAN_MB_CNT_RTR)
878 cf->can_id |= CAN_RTR_FLAG;
879 cf->can_dlc = get_can_dlc((reg_ctrl >> 16) & 0xf);
880
Pankaj Bansal05179612018-11-23 22:18:44 +0100881 for (i = 0; i < cf->can_dlc; i += sizeof(u32)) {
882 __be32 data = cpu_to_be32(priv->read(&mb->data[i / sizeof(u32)]));
883 *(__be32 *)(cf->data + i) = data;
884 }
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200885
Joakim Zhang4e9c9482019-07-12 08:02:38 +0000886 mark_as_read:
Marc Kleine-Buddeb9468ad2019-03-01 16:27:59 +0100887 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP)
888 flexcan_write64(priv, FLEXCAN_IFLAG_MB(n), &regs->iflag1);
889 else
Pankaj Bansal88462d22017-11-24 18:52:08 +0530890 priv->write(FLEXCAN_IFLAG_RX_FIFO_AVAILABLE, &regs->iflag1);
Fabio Baltieriadccadb2012-12-18 18:50:58 +0100891
Pankaj Bansal5178b7c2018-08-01 19:36:46 +0530892 /* Read the Free Running Timer. It is optional but recommended
893 * to unlock Mailbox as soon as possible and make it available
894 * for reception.
895 */
896 priv->read(&regs->timer);
897
Joakim Zhang4e9c9482019-07-12 08:02:38 +0000898 return skb;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200899}
900
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200901static irqreturn_t flexcan_irq(int irq, void *dev_id)
902{
903 struct net_device *dev = dev_id;
904 struct net_device_stats *stats = &dev->stats;
905 struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200906 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddedd2f1222017-01-18 11:45:14 +0100907 irqreturn_t handled = IRQ_NONE;
Marc Kleine-Budde0ca64f022019-03-01 13:54:19 +0100908 u64 reg_iflag_tx;
909 u32 reg_esr;
ZHU Yi (ST-FIR/ENG1-Zhu)da49a802017-09-15 07:03:58 +0000910 enum can_state last_state = priv->can.state;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200911
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200912 /* reception interrupt */
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200913 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP) {
Marc Kleine-Budde4e265982019-03-01 16:29:47 +0100914 u64 reg_iflag_rx;
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200915 int ret;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200916
Marc Kleine-Budde4e265982019-03-01 16:29:47 +0100917 while ((reg_iflag_rx = flexcan_read_reg_iflag_rx(priv))) {
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200918 handled = IRQ_HANDLED;
919 ret = can_rx_offload_irq_offload_timestamp(&priv->offload,
Marc Kleine-Budde4e265982019-03-01 16:29:47 +0100920 reg_iflag_rx);
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200921 if (!ret)
922 break;
923 }
924 } else {
Alexander Steincbffaf72018-10-11 17:01:25 +0200925 u32 reg_iflag1;
926
927 reg_iflag1 = priv->read(&regs->iflag1);
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200928 if (reg_iflag1 & FLEXCAN_IFLAG_RX_FIFO_AVAILABLE) {
929 handled = IRQ_HANDLED;
930 can_rx_offload_irq_offload_fifo(&priv->offload);
931 }
932
933 /* FIFO overflow interrupt */
934 if (reg_iflag1 & FLEXCAN_IFLAG_RX_FIFO_OVERFLOW) {
935 handled = IRQ_HANDLED;
Pankaj Bansal88462d22017-11-24 18:52:08 +0530936 priv->write(FLEXCAN_IFLAG_RX_FIFO_OVERFLOW,
937 &regs->iflag1);
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200938 dev->stats.rx_over_errors++;
939 dev->stats.rx_errors++;
940 }
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200941 }
942
Marc Kleine-Buddeb87c28b72019-03-01 15:38:05 +0100943 reg_iflag_tx = flexcan_read_reg_iflag_tx(priv);
Alexander Steincbffaf72018-10-11 17:01:25 +0200944
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200945 /* transmission complete interrupt */
Marc Kleine-Budde0ca64f022019-03-01 13:54:19 +0100946 if (reg_iflag_tx & priv->tx_mask) {
Pankaj Bansal05179612018-11-23 22:18:44 +0100947 u32 reg_ctrl = priv->read(&priv->tx_mb->can_ctrl);
Oleksij Rempeled72bc82018-09-18 11:40:39 +0200948
Marc Kleine-Buddedd2f1222017-01-18 11:45:14 +0100949 handled = IRQ_HANDLED;
Oleksij Rempeled72bc82018-09-18 11:40:39 +0200950 stats->tx_bytes += can_rx_offload_get_echo_skb(&priv->offload,
951 0, reg_ctrl << 16);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200952 stats->tx_packets++;
Fabio Baltieriadccadb2012-12-18 18:50:58 +0100953 can_led_event(dev, CAN_LED_EVENT_TX);
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200954
955 /* after sending a RTR frame MB is in RX mode */
Pankaj Bansal88462d22017-11-24 18:52:08 +0530956 priv->write(FLEXCAN_MB_CODE_TX_INACTIVE,
Pankaj Bansal05179612018-11-23 22:18:44 +0100957 &priv->tx_mb->can_ctrl);
Marc Kleine-Buddeb87c28b72019-03-01 15:38:05 +0100958 flexcan_write64(priv, priv->tx_mask, &regs->iflag1);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200959 netif_wake_queue(dev);
960 }
961
Pankaj Bansal88462d22017-11-24 18:52:08 +0530962 reg_esr = priv->read(&regs->esr);
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200963
Marc Kleine-Buddedd2f1222017-01-18 11:45:14 +0100964 /* ACK all bus error and state change IRQ sources */
965 if (reg_esr & FLEXCAN_ESR_ALL_INT) {
966 handled = IRQ_HANDLED;
Pankaj Bansal88462d22017-11-24 18:52:08 +0530967 priv->write(reg_esr & FLEXCAN_ESR_ALL_INT, &regs->esr);
Marc Kleine-Buddedd2f1222017-01-18 11:45:14 +0100968 }
969
ZHU Yi (ST-FIR/ENG1-Zhu)ad230232017-09-15 06:59:15 +0000970 /* state change interrupt or broken error state quirk fix is enabled */
971 if ((reg_esr & FLEXCAN_ESR_ERR_STATE) ||
ZHU Yi (ST-FIR/ENG1-Zhu)da49a802017-09-15 07:03:58 +0000972 (priv->devtype_data->quirks & (FLEXCAN_QUIRK_BROKEN_WERR_STATE |
Marc Kleine-Buddebc8ad652018-11-28 15:45:27 +0100973 FLEXCAN_QUIRK_BROKEN_PERR_STATE)))
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200974 flexcan_irq_state(dev, reg_esr);
975
976 /* bus error IRQ - handle if bus error reporting is activated */
977 if ((reg_esr & FLEXCAN_ESR_ERR_BUS) &&
978 (priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING))
979 flexcan_irq_bus_err(dev, reg_esr);
980
ZHU Yi (ST-FIR/ENG1-Zhu)da49a802017-09-15 07:03:58 +0000981 /* availability of error interrupt among state transitions in case
982 * bus error reporting is de-activated and
983 * FLEXCAN_QUIRK_BROKEN_PERR_STATE is enabled:
984 * +--------------------------------------------------------------+
985 * | +----------------------------------------------+ [stopped / |
986 * | | | sleeping] -+
987 * +-+-> active <-> warning <-> passive -> bus off -+
988 * ___________^^^^^^^^^^^^_______________________________
989 * disabled(1) enabled disabled
990 *
991 * (1): enabled if FLEXCAN_QUIRK_BROKEN_WERR_STATE is enabled
992 */
993 if ((last_state != priv->can.state) &&
994 (priv->devtype_data->quirks & FLEXCAN_QUIRK_BROKEN_PERR_STATE) &&
995 !(priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING)) {
996 switch (priv->can.state) {
997 case CAN_STATE_ERROR_ACTIVE:
998 if (priv->devtype_data->quirks &
999 FLEXCAN_QUIRK_BROKEN_WERR_STATE)
1000 flexcan_error_irq_enable(priv);
1001 else
1002 flexcan_error_irq_disable(priv);
1003 break;
1004
1005 case CAN_STATE_ERROR_WARNING:
1006 flexcan_error_irq_enable(priv);
1007 break;
1008
1009 case CAN_STATE_ERROR_PASSIVE:
1010 case CAN_STATE_BUS_OFF:
1011 flexcan_error_irq_disable(priv);
1012 break;
1013
1014 default:
1015 break;
1016 }
1017 }
1018
Marc Kleine-Buddedd2f1222017-01-18 11:45:14 +01001019 return handled;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001020}
1021
1022static void flexcan_set_bittiming(struct net_device *dev)
1023{
1024 const struct flexcan_priv *priv = netdev_priv(dev);
1025 const struct can_bittiming *bt = &priv->can.bittiming;
Marc Kleine-Budde89af8742015-05-08 09:32:58 +02001026 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001027 u32 reg;
1028
Pankaj Bansal88462d22017-11-24 18:52:08 +05301029 reg = priv->read(&regs->ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001030 reg &= ~(FLEXCAN_CTRL_PRESDIV(0xff) |
1031 FLEXCAN_CTRL_RJW(0x3) |
1032 FLEXCAN_CTRL_PSEG1(0x7) |
1033 FLEXCAN_CTRL_PSEG2(0x7) |
1034 FLEXCAN_CTRL_PROPSEG(0x7) |
1035 FLEXCAN_CTRL_LPB |
1036 FLEXCAN_CTRL_SMP |
1037 FLEXCAN_CTRL_LOM);
1038
1039 reg |= FLEXCAN_CTRL_PRESDIV(bt->brp - 1) |
1040 FLEXCAN_CTRL_PSEG1(bt->phase_seg1 - 1) |
1041 FLEXCAN_CTRL_PSEG2(bt->phase_seg2 - 1) |
1042 FLEXCAN_CTRL_RJW(bt->sjw - 1) |
1043 FLEXCAN_CTRL_PROPSEG(bt->prop_seg - 1);
1044
1045 if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)
1046 reg |= FLEXCAN_CTRL_LPB;
1047 if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
1048 reg |= FLEXCAN_CTRL_LOM;
1049 if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
1050 reg |= FLEXCAN_CTRL_SMP;
1051
Lucas Stach7a4b6c82015-08-07 17:16:03 +02001052 netdev_dbg(dev, "writing ctrl=0x%08x\n", reg);
Pankaj Bansal88462d22017-11-24 18:52:08 +05301053 priv->write(reg, &regs->ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001054
1055 /* print chip status */
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +01001056 netdev_dbg(dev, "%s: mcr=0x%08x ctrl=0x%08x\n", __func__,
Pankaj Bansal88462d22017-11-24 18:52:08 +05301057 priv->read(&regs->mcr), priv->read(&regs->ctrl));
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001058}
1059
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +02001060/* flexcan_chip_start
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001061 *
1062 * this functions is entered with clocks enabled
1063 *
1064 */
1065static int flexcan_chip_start(struct net_device *dev)
1066{
1067 struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Budde89af8742015-05-08 09:32:58 +02001068 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Budde6f75fce2014-09-23 11:03:01 +02001069 u32 reg_mcr, reg_ctrl, reg_ctrl2, reg_mecr;
Marc Kleine-Budde8ce51392019-03-01 12:17:30 +01001070 u64 reg_imask;
David S. Miller1f6d8032014-09-23 12:09:27 -04001071 int err, i;
Pankaj Bansal05179612018-11-23 22:18:44 +01001072 struct flexcan_mb __iomem *mb;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001073
1074 /* enable module */
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001075 err = flexcan_chip_enable(priv);
1076 if (err)
1077 return err;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001078
1079 /* soft reset */
Marc Kleine-Budde4b5b8222014-02-28 15:16:59 +01001080 err = flexcan_chip_softreset(priv);
1081 if (err)
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +01001082 goto out_chip_disable;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001083
1084 flexcan_set_bittiming(dev);
1085
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +02001086 /* MCR
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001087 *
1088 * enable freeze
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001089 * halt now
1090 * only supervisor access
1091 * enable warning int
Marc Kleine-Budde4bd888a2015-08-31 21:03:29 +02001092 * enable individual RX masking
Marc Kleine-Budde749de6f2015-08-31 21:32:34 +02001093 * choose format C
1094 * set max mailbox number
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001095 */
Pankaj Bansal88462d22017-11-24 18:52:08 +05301096 reg_mcr = priv->read(&regs->mcr);
Marc Kleine-Budded5a7b402013-10-04 10:52:36 +02001097 reg_mcr &= ~FLEXCAN_MCR_MAXMB(0xff);
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +02001098 reg_mcr |= FLEXCAN_MCR_FRZ | FLEXCAN_MCR_HALT | FLEXCAN_MCR_SUPV |
Pankaj Bansal7ad0f532018-08-13 23:50:48 +05301099 FLEXCAN_MCR_WRN_EN | FLEXCAN_MCR_IRMQ | FLEXCAN_MCR_IDAM_C |
Pankaj Bansal05179612018-11-23 22:18:44 +01001100 FLEXCAN_MCR_MAXMB(priv->tx_mb_idx);
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +02001101
Marc Kleine-Buddec982a3ca2018-08-17 14:52:58 +02001102 /* MCR
1103 *
1104 * FIFO:
1105 * - disable for timestamp mode
1106 * - enable for FIFO mode
1107 */
Alexander Steincbffaf72018-10-11 17:01:25 +02001108 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP)
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +02001109 reg_mcr &= ~FLEXCAN_MCR_FEN;
Alexander Steincbffaf72018-10-11 17:01:25 +02001110 else
1111 reg_mcr |= FLEXCAN_MCR_FEN;
1112
Pankaj Bansal7ad0f532018-08-13 23:50:48 +05301113 /* MCR
1114 *
1115 * NOTE: In loopback mode, the CAN_MCR[SRXDIS] cannot be
1116 * asserted because this will impede the self reception
1117 * of a transmitted message. This is not documented in
1118 * earlier versions of flexcan block guide.
1119 *
1120 * Self Reception:
1121 * - enable Self Reception for loopback mode
1122 * (by clearing "Self Reception Disable" bit)
1123 * - disable for normal operation
1124 */
1125 if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)
1126 reg_mcr &= ~FLEXCAN_MCR_SRX_DIS;
1127 else
1128 reg_mcr |= FLEXCAN_MCR_SRX_DIS;
1129
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +01001130 netdev_dbg(dev, "%s: writing mcr=0x%08x", __func__, reg_mcr);
Pankaj Bansal88462d22017-11-24 18:52:08 +05301131 priv->write(reg_mcr, &regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001132
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +02001133 /* CTRL
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001134 *
1135 * disable timer sync feature
1136 *
1137 * disable auto busoff recovery
1138 * transmit lowest buffer first
1139 *
1140 * enable tx and rx warning interrupt
1141 * enable bus off interrupt
1142 * (== FLEXCAN_CTRL_ERR_STATE)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001143 */
Pankaj Bansal88462d22017-11-24 18:52:08 +05301144 reg_ctrl = priv->read(&regs->ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001145 reg_ctrl &= ~FLEXCAN_CTRL_TSYN;
1146 reg_ctrl |= FLEXCAN_CTRL_BOFF_REC | FLEXCAN_CTRL_LBUF |
Wolfgang Grandegger4f72e5f2012-09-28 03:17:15 +00001147 FLEXCAN_CTRL_ERR_STATE;
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +02001148
1149 /* enable the "error interrupt" (FLEXCAN_CTRL_ERR_MSK),
Wolfgang Grandegger4f72e5f2012-09-28 03:17:15 +00001150 * on most Flexcan cores, too. Otherwise we don't get
1151 * any error warning or passive interrupts.
1152 */
ZHU Yi (ST-FIR/ENG1-Zhu)2f8639b2017-09-15 07:01:23 +00001153 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_BROKEN_WERR_STATE ||
Wolfgang Grandegger4f72e5f2012-09-28 03:17:15 +00001154 priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING)
1155 reg_ctrl |= FLEXCAN_CTRL_ERR_MSK;
Alexander Steinbc03a542014-08-12 10:47:21 +02001156 else
1157 reg_ctrl &= ~FLEXCAN_CTRL_ERR_MSK;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001158
1159 /* save for later use */
1160 priv->reg_ctrl_default = reg_ctrl;
Marc Kleine-Budde6fa7da22015-08-27 14:24:48 +02001161 /* leave interrupts disabled for now */
1162 reg_ctrl &= ~FLEXCAN_CTRL_ERR_ALL;
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +01001163 netdev_dbg(dev, "%s: writing ctrl=0x%08x", __func__, reg_ctrl);
Pankaj Bansal88462d22017-11-24 18:52:08 +05301164 priv->write(reg_ctrl, &regs->ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001165
Marc Kleine-Budde9eb7aa82015-09-01 08:57:55 +02001166 if ((priv->devtype_data->quirks & FLEXCAN_QUIRK_ENABLE_EACEN_RRS)) {
Pankaj Bansal88462d22017-11-24 18:52:08 +05301167 reg_ctrl2 = priv->read(&regs->ctrl2);
Marc Kleine-Budde9eb7aa82015-09-01 08:57:55 +02001168 reg_ctrl2 |= FLEXCAN_CTRL2_EACEN | FLEXCAN_CTRL2_RRS;
Pankaj Bansal88462d22017-11-24 18:52:08 +05301169 priv->write(reg_ctrl2, &regs->ctrl2);
Marc Kleine-Budde9eb7aa82015-09-01 08:57:55 +02001170 }
1171
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +02001172 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP) {
Alexander Steincbffaf72018-10-11 17:01:25 +02001173 for (i = priv->offload.mb_first; i <= priv->offload.mb_last; i++) {
Pankaj Bansal05179612018-11-23 22:18:44 +01001174 mb = flexcan_get_mb(priv, i);
Pankaj Bansal88462d22017-11-24 18:52:08 +05301175 priv->write(FLEXCAN_MB_CODE_RX_EMPTY,
Pankaj Bansal05179612018-11-23 22:18:44 +01001176 &mb->can_ctrl);
Alexander Steincbffaf72018-10-11 17:01:25 +02001177 }
1178 } else {
1179 /* clear and invalidate unused mailboxes first */
Uwe Kleine-Königa55234d2019-01-11 12:20:41 +01001180 for (i = FLEXCAN_TX_MB_RESERVED_OFF_FIFO; i < priv->mb_count; i++) {
Pankaj Bansal05179612018-11-23 22:18:44 +01001181 mb = flexcan_get_mb(priv, i);
Alexander Steincbffaf72018-10-11 17:01:25 +02001182 priv->write(FLEXCAN_MB_CODE_RX_INACTIVE,
Pankaj Bansal05179612018-11-23 22:18:44 +01001183 &mb->can_ctrl);
Alexander Steincbffaf72018-10-11 17:01:25 +02001184 }
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +02001185 }
1186
David Jander25e92442014-09-03 16:47:22 +02001187 /* Errata ERR005829: mark first TX mailbox as INACTIVE */
Pankaj Bansal88462d22017-11-24 18:52:08 +05301188 priv->write(FLEXCAN_MB_CODE_TX_INACTIVE,
1189 &priv->tx_mb_reserved->can_ctrl);
David Jander25e92442014-09-03 16:47:22 +02001190
Marc Kleine-Buddec32fe4a2014-09-16 12:39:28 +02001191 /* mark TX mailbox as INACTIVE */
Pankaj Bansal88462d22017-11-24 18:52:08 +05301192 priv->write(FLEXCAN_MB_CODE_TX_INACTIVE,
Pankaj Bansal05179612018-11-23 22:18:44 +01001193 &priv->tx_mb->can_ctrl);
Marc Kleine-Budded5a7b402013-10-04 10:52:36 +02001194
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001195 /* acceptance mask/acceptance code (accept everything) */
Pankaj Bansal88462d22017-11-24 18:52:08 +05301196 priv->write(0x0, &regs->rxgmask);
1197 priv->write(0x0, &regs->rx14mask);
1198 priv->write(0x0, &regs->rx15mask);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001199
Marc Kleine-Buddef377bff2015-05-08 15:22:36 +02001200 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_DISABLE_RXFG)
Pankaj Bansal88462d22017-11-24 18:52:08 +05301201 priv->write(0x0, &regs->rxfgmask);
Hui Wang30c1e672012-06-28 16:21:35 +08001202
Marc Kleine-Budde4bd888a2015-08-31 21:03:29 +02001203 /* clear acceptance filters */
Pankaj Bansal05179612018-11-23 22:18:44 +01001204 for (i = 0; i < priv->mb_count; i++)
Pankaj Bansal88462d22017-11-24 18:52:08 +05301205 priv->write(0, &regs->rximr[i]);
Marc Kleine-Budde4bd888a2015-08-31 21:03:29 +02001206
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +02001207 /* On Vybrid, disable memory error detection interrupts
Stefan Agnercdce8442014-07-15 14:56:21 +02001208 * and freeze mode.
1209 * This also works around errata e5295 which generates
1210 * false positive memory errors and put the device in
1211 * freeze mode.
1212 */
Marc Kleine-Buddef377bff2015-05-08 15:22:36 +02001213 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_DISABLE_MECR) {
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +02001214 /* Follow the protocol as described in "Detection
Stefan Agnercdce8442014-07-15 14:56:21 +02001215 * and Correction of Memory Errors" to write to
1216 * MECR register
1217 */
Pankaj Bansal88462d22017-11-24 18:52:08 +05301218 reg_ctrl2 = priv->read(&regs->ctrl2);
Marc Kleine-Budde6f75fce2014-09-23 11:03:01 +02001219 reg_ctrl2 |= FLEXCAN_CTRL2_ECRWRE;
Pankaj Bansal88462d22017-11-24 18:52:08 +05301220 priv->write(reg_ctrl2, &regs->ctrl2);
Stefan Agnercdce8442014-07-15 14:56:21 +02001221
Pankaj Bansal88462d22017-11-24 18:52:08 +05301222 reg_mecr = priv->read(&regs->mecr);
Stefan Agnercdce8442014-07-15 14:56:21 +02001223 reg_mecr &= ~FLEXCAN_MECR_ECRWRDIS;
Pankaj Bansal88462d22017-11-24 18:52:08 +05301224 priv->write(reg_mecr, &regs->mecr);
Joakim Zhang5e269322019-08-15 08:00:26 +00001225 reg_mecr |= FLEXCAN_MECR_ECCDIS;
Stefan Agnercdce8442014-07-15 14:56:21 +02001226 reg_mecr &= ~(FLEXCAN_MECR_NCEFAFRZ | FLEXCAN_MECR_HANCEI_MSK |
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +02001227 FLEXCAN_MECR_FANCEI_MSK);
Pankaj Bansal88462d22017-11-24 18:52:08 +05301228 priv->write(reg_mecr, &regs->mecr);
Stefan Agnercdce8442014-07-15 14:56:21 +02001229 }
1230
Marc Kleine-Buddef0036982014-02-28 17:18:27 +01001231 err = flexcan_transceiver_enable(priv);
1232 if (err)
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +01001233 goto out_chip_disable;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001234
1235 /* synchronize with the can bus */
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +01001236 err = flexcan_chip_unfreeze(priv);
1237 if (err)
1238 goto out_transceiver_disable;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001239
1240 priv->can.state = CAN_STATE_ERROR_ACTIVE;
1241
Marc Kleine-Budde6fa7da22015-08-27 14:24:48 +02001242 /* enable interrupts atomically */
1243 disable_irq(dev->irq);
Pankaj Bansal88462d22017-11-24 18:52:08 +05301244 priv->write(priv->reg_ctrl_default, &regs->ctrl);
Marc Kleine-Budde0ca64f022019-03-01 13:54:19 +01001245 reg_imask = priv->rx_mask | priv->tx_mask;
Marc Kleine-Budde8ce51392019-03-01 12:17:30 +01001246 priv->write(upper_32_bits(reg_imask), &regs->imask2);
1247 priv->write(lower_32_bits(reg_imask), &regs->imask1);
Marc Kleine-Budde6fa7da22015-08-27 14:24:48 +02001248 enable_irq(dev->irq);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001249
1250 /* print chip status */
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +01001251 netdev_dbg(dev, "%s: reading mcr=0x%08x ctrl=0x%08x\n", __func__,
Pankaj Bansal88462d22017-11-24 18:52:08 +05301252 priv->read(&regs->mcr), priv->read(&regs->ctrl));
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001253
1254 return 0;
1255
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +01001256 out_transceiver_disable:
1257 flexcan_transceiver_disable(priv);
1258 out_chip_disable:
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001259 flexcan_chip_disable(priv);
1260 return err;
1261}
1262
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +02001263/* flexcan_chip_stop
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001264 *
1265 * this functions is entered with clocks enabled
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001266 */
1267static void flexcan_chip_stop(struct net_device *dev)
1268{
1269 struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Budde89af8742015-05-08 09:32:58 +02001270 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001271
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +01001272 /* freeze + disable module */
1273 flexcan_chip_freeze(priv);
1274 flexcan_chip_disable(priv);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001275
Marc Kleine-Budde5be93bd2014-02-19 12:00:51 +01001276 /* Disable all interrupts */
Pankaj Bansal88462d22017-11-24 18:52:08 +05301277 priv->write(0, &regs->imask2);
1278 priv->write(0, &regs->imask1);
1279 priv->write(priv->reg_ctrl_default & ~FLEXCAN_CTRL_ERR_ALL,
1280 &regs->ctrl);
Marc Kleine-Budde5be93bd2014-02-19 12:00:51 +01001281
Marc Kleine-Buddef0036982014-02-28 17:18:27 +01001282 flexcan_transceiver_disable(priv);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001283 priv->can.state = CAN_STATE_STOPPED;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001284}
1285
1286static int flexcan_open(struct net_device *dev)
1287{
1288 struct flexcan_priv *priv = netdev_priv(dev);
1289 int err;
1290
Aisheng Dongca109892018-11-30 08:53:26 +00001291 err = pm_runtime_get_sync(priv->dev);
1292 if (err < 0)
Fabio Estevamaa101812013-07-22 12:41:40 -03001293 return err;
1294
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001295 err = open_candev(dev);
1296 if (err)
Aisheng Dongca109892018-11-30 08:53:26 +00001297 goto out_runtime_put;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001298
1299 err = request_irq(dev->irq, flexcan_irq, IRQF_SHARED, dev->name, dev);
1300 if (err)
1301 goto out_close;
1302
Pankaj Bansal05179612018-11-23 22:18:44 +01001303 priv->mb_size = sizeof(struct flexcan_mb) + CAN_MAX_DLEN;
Pankaj Bansal6cbf7602018-08-28 23:19:12 +05301304 priv->mb_count = (sizeof(priv->regs->mb[0]) / priv->mb_size) +
1305 (sizeof(priv->regs->mb[1]) / priv->mb_size);
Pankaj Bansal05179612018-11-23 22:18:44 +01001306
Pankaj Bansal5156c7b2018-08-13 23:50:49 +05301307 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP)
Pankaj Bansal05179612018-11-23 22:18:44 +01001308 priv->tx_mb_reserved =
1309 flexcan_get_mb(priv, FLEXCAN_TX_MB_RESERVED_OFF_TIMESTAMP);
Pankaj Bansal5156c7b2018-08-13 23:50:49 +05301310 else
Pankaj Bansal05179612018-11-23 22:18:44 +01001311 priv->tx_mb_reserved =
1312 flexcan_get_mb(priv, FLEXCAN_TX_MB_RESERVED_OFF_FIFO);
1313 priv->tx_mb_idx = priv->mb_count - 1;
1314 priv->tx_mb = flexcan_get_mb(priv, priv->tx_mb_idx);
Marc Kleine-Budde0ca64f022019-03-01 13:54:19 +01001315 priv->tx_mask = FLEXCAN_IFLAG_MB(priv->tx_mb_idx);
Pankaj Bansal5156c7b2018-08-13 23:50:49 +05301316
Pankaj Bansal5156c7b2018-08-13 23:50:49 +05301317 priv->offload.mailbox_read = flexcan_mailbox_read;
1318
1319 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP) {
Pankaj Bansal5156c7b2018-08-13 23:50:49 +05301320 priv->offload.mb_first = FLEXCAN_RX_MB_OFF_TIMESTAMP_FIRST;
Pankaj Bansal05179612018-11-23 22:18:44 +01001321 priv->offload.mb_last = priv->mb_count - 2;
Pankaj Bansal5156c7b2018-08-13 23:50:49 +05301322
Marc Kleine-Budde8ce51392019-03-01 12:17:30 +01001323 priv->rx_mask = GENMASK_ULL(priv->offload.mb_last,
1324 priv->offload.mb_first);
Pankaj Bansal5156c7b2018-08-13 23:50:49 +05301325 err = can_rx_offload_add_timestamp(dev, &priv->offload);
1326 } else {
Marc Kleine-Budde8ce51392019-03-01 12:17:30 +01001327 priv->rx_mask = FLEXCAN_IFLAG_RX_FIFO_OVERFLOW |
Pankaj Bansal5156c7b2018-08-13 23:50:49 +05301328 FLEXCAN_IFLAG_RX_FIFO_AVAILABLE;
1329 err = can_rx_offload_add_fifo(dev, &priv->offload,
1330 FLEXCAN_NAPI_WEIGHT);
1331 }
1332 if (err)
1333 goto out_free_irq;
1334
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001335 /* start chip and queuing */
1336 err = flexcan_chip_start(dev);
1337 if (err)
Pankaj Bansal5156c7b2018-08-13 23:50:49 +05301338 goto out_offload_del;
Fabio Baltieriadccadb2012-12-18 18:50:58 +01001339
1340 can_led_event(dev, CAN_LED_EVENT_OPEN);
1341
Marc Kleine-Budde30164752015-05-10 15:26:58 +02001342 can_rx_offload_enable(&priv->offload);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001343 netif_start_queue(dev);
1344
1345 return 0;
1346
Pankaj Bansal5156c7b2018-08-13 23:50:49 +05301347 out_offload_del:
1348 can_rx_offload_del(&priv->offload);
Marc Kleine-Budde7e9e1482014-02-28 14:52:01 +01001349 out_free_irq:
1350 free_irq(dev->irq, dev);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001351 out_close:
1352 close_candev(dev);
Aisheng Dongca109892018-11-30 08:53:26 +00001353 out_runtime_put:
1354 pm_runtime_put(priv->dev);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001355
1356 return err;
1357}
1358
1359static int flexcan_close(struct net_device *dev)
1360{
1361 struct flexcan_priv *priv = netdev_priv(dev);
1362
1363 netif_stop_queue(dev);
Marc Kleine-Budde30164752015-05-10 15:26:58 +02001364 can_rx_offload_disable(&priv->offload);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001365 flexcan_chip_stop(dev);
1366
Pankaj Bansal5156c7b2018-08-13 23:50:49 +05301367 can_rx_offload_del(&priv->offload);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001368 free_irq(dev->irq, dev);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001369
1370 close_candev(dev);
Aisheng Dongca109892018-11-30 08:53:26 +00001371 pm_runtime_put(priv->dev);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001372
Fabio Baltieriadccadb2012-12-18 18:50:58 +01001373 can_led_event(dev, CAN_LED_EVENT_STOP);
1374
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001375 return 0;
1376}
1377
1378static int flexcan_set_mode(struct net_device *dev, enum can_mode mode)
1379{
1380 int err;
1381
1382 switch (mode) {
1383 case CAN_MODE_START:
1384 err = flexcan_chip_start(dev);
1385 if (err)
1386 return err;
1387
1388 netif_wake_queue(dev);
1389 break;
1390
1391 default:
1392 return -EOPNOTSUPP;
1393 }
1394
1395 return 0;
1396}
1397
1398static const struct net_device_ops flexcan_netdev_ops = {
1399 .ndo_open = flexcan_open,
1400 .ndo_stop = flexcan_close,
1401 .ndo_start_xmit = flexcan_start_xmit,
Oliver Hartkoppc971fa22014-03-07 09:23:41 +01001402 .ndo_change_mtu = can_change_mtu,
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001403};
1404
Bill Pemberton3c8ac0f2012-12-03 09:22:44 -05001405static int register_flexcandev(struct net_device *dev)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001406{
1407 struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Budde89af8742015-05-08 09:32:58 +02001408 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001409 u32 reg, err;
1410
Aisheng Dongca109892018-11-30 08:53:26 +00001411 err = flexcan_clks_enable(priv);
Fabio Estevamaa101812013-07-22 12:41:40 -03001412 if (err)
1413 return err;
1414
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001415 /* select "bus clock", chip must be disabled */
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001416 err = flexcan_chip_disable(priv);
1417 if (err)
Aisheng Dongca109892018-11-30 08:53:26 +00001418 goto out_clks_disable;
1419
Pankaj Bansal88462d22017-11-24 18:52:08 +05301420 reg = priv->read(&regs->ctrl);
Dong Aisheng8c306be2018-12-13 07:08:00 +00001421 if (priv->clk_src)
1422 reg |= FLEXCAN_CTRL_CLK_SRC;
1423 else
1424 reg &= ~FLEXCAN_CTRL_CLK_SRC;
Pankaj Bansal88462d22017-11-24 18:52:08 +05301425 priv->write(reg, &regs->ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001426
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001427 err = flexcan_chip_enable(priv);
1428 if (err)
1429 goto out_chip_disable;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001430
1431 /* set freeze, halt and activate FIFO, restrict register access */
Pankaj Bansal88462d22017-11-24 18:52:08 +05301432 reg = priv->read(&regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001433 reg |= FLEXCAN_MCR_FRZ | FLEXCAN_MCR_HALT |
1434 FLEXCAN_MCR_FEN | FLEXCAN_MCR_SUPV;
Pankaj Bansal88462d22017-11-24 18:52:08 +05301435 priv->write(reg, &regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001436
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +02001437 /* Currently we only support newer versions of this core
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +02001438 * featuring a RX hardware FIFO (although this driver doesn't
1439 * make use of it on some cores). Older cores, found on some
1440 * Coldfire derivates are not tested.
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001441 */
Pankaj Bansal88462d22017-11-24 18:52:08 +05301442 reg = priv->read(&regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001443 if (!(reg & FLEXCAN_MCR_FEN)) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +01001444 netdev_err(dev, "Could not enable RX FIFO, unsupported core\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001445 err = -ENODEV;
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001446 goto out_chip_disable;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001447 }
1448
1449 err = register_candev(dev);
Aisheng Dongca109892018-11-30 08:53:26 +00001450 if (err)
1451 goto out_chip_disable;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001452
Aisheng Dongca109892018-11-30 08:53:26 +00001453 /* Disable core and let pm_runtime_put() disable the clocks.
1454 * If CONFIG_PM is not enabled, the clocks will stay powered.
1455 */
1456 flexcan_chip_disable(priv);
1457 pm_runtime_put(priv->dev);
1458
1459 return 0;
1460
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001461 out_chip_disable:
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001462 flexcan_chip_disable(priv);
Aisheng Dongca109892018-11-30 08:53:26 +00001463 out_clks_disable:
1464 flexcan_clks_disable(priv);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001465 return err;
1466}
1467
Bill Pemberton3c8ac0f2012-12-03 09:22:44 -05001468static void unregister_flexcandev(struct net_device *dev)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001469{
1470 unregister_candev(dev);
1471}
1472
Aisheng Dongde3578c2018-11-23 08:35:33 +00001473static int flexcan_setup_stop_mode(struct platform_device *pdev)
1474{
1475 struct net_device *dev = platform_get_drvdata(pdev);
1476 struct device_node *np = pdev->dev.of_node;
1477 struct device_node *gpr_np;
1478 struct flexcan_priv *priv;
1479 phandle phandle;
1480 u32 out_val[5];
1481 int ret;
1482
1483 if (!np)
1484 return -EINVAL;
1485
1486 /* stop mode property format is:
1487 * <&gpr req_gpr req_bit ack_gpr ack_bit>.
1488 */
1489 ret = of_property_read_u32_array(np, "fsl,stop-mode", out_val,
1490 ARRAY_SIZE(out_val));
1491 if (ret) {
1492 dev_dbg(&pdev->dev, "no stop-mode property\n");
1493 return ret;
1494 }
1495 phandle = *out_val;
1496
1497 gpr_np = of_find_node_by_phandle(phandle);
1498 if (!gpr_np) {
1499 dev_dbg(&pdev->dev, "could not find gpr node by phandle\n");
YueHaibing7873e982018-12-12 17:24:01 +08001500 return -ENODEV;
Aisheng Dongde3578c2018-11-23 08:35:33 +00001501 }
1502
1503 priv = netdev_priv(dev);
1504 priv->stm.gpr = syscon_node_to_regmap(gpr_np);
Aisheng Dongde3578c2018-11-23 08:35:33 +00001505 if (IS_ERR(priv->stm.gpr)) {
1506 dev_dbg(&pdev->dev, "could not find gpr regmap\n");
Wen Yange9f2a852019-07-06 11:37:20 +08001507 ret = PTR_ERR(priv->stm.gpr);
1508 goto out_put_node;
Aisheng Dongde3578c2018-11-23 08:35:33 +00001509 }
1510
1511 priv->stm.req_gpr = out_val[1];
1512 priv->stm.req_bit = out_val[2];
1513 priv->stm.ack_gpr = out_val[3];
1514 priv->stm.ack_bit = out_val[4];
1515
1516 dev_dbg(&pdev->dev,
1517 "gpr %s req_gpr=0x02%x req_bit=%u ack_gpr=0x02%x ack_bit=%u\n",
1518 gpr_np->full_name, priv->stm.req_gpr, priv->stm.req_bit,
1519 priv->stm.ack_gpr, priv->stm.ack_bit);
1520
1521 device_set_wakeup_capable(&pdev->dev, true);
1522
Sean Nyekjaer915f9662019-04-09 10:39:48 +02001523 if (of_property_read_bool(np, "wakeup-source"))
1524 device_set_wakeup_enable(&pdev->dev, true);
1525
Aisheng Dongde3578c2018-11-23 08:35:33 +00001526 return 0;
David S. Miller13dfb3f2019-08-06 18:44:57 -07001527
Wen Yange9f2a852019-07-06 11:37:20 +08001528out_put_node:
1529 of_node_put(gpr_np);
1530 return ret;
Aisheng Dongde3578c2018-11-23 08:35:33 +00001531}
1532
Hui Wang30c1e672012-06-28 16:21:35 +08001533static const struct of_device_id flexcan_of_match[] = {
Hui Wang30c1e672012-06-28 16:21:35 +08001534 { .compatible = "fsl,imx6q-flexcan", .data = &fsl_imx6q_devtype_data, },
Marc Kleine-Buddee3587842013-10-03 23:51:55 +02001535 { .compatible = "fsl,imx28-flexcan", .data = &fsl_imx28_devtype_data, },
Uwe Kleine-König0e030a32018-04-25 16:50:39 +02001536 { .compatible = "fsl,imx53-flexcan", .data = &fsl_imx25_devtype_data, },
1537 { .compatible = "fsl,imx35-flexcan", .data = &fsl_imx25_devtype_data, },
1538 { .compatible = "fsl,imx25-flexcan", .data = &fsl_imx25_devtype_data, },
Marc Kleine-Buddee3587842013-10-03 23:51:55 +02001539 { .compatible = "fsl,p1010-flexcan", .data = &fsl_p1010_devtype_data, },
Stefan Agnercdce8442014-07-15 14:56:21 +02001540 { .compatible = "fsl,vf610-flexcan", .data = &fsl_vf610_devtype_data, },
Pankaj Bansal99b76682017-11-24 18:52:09 +05301541 { .compatible = "fsl,ls1021ar2-flexcan", .data = &fsl_ls1021a_r2_devtype_data, },
Hui Wang30c1e672012-06-28 16:21:35 +08001542 { /* sentinel */ },
1543};
Marc Kleine-Budde4358a9d2012-10-04 10:55:35 +02001544MODULE_DEVICE_TABLE(of, flexcan_of_match);
Hui Wang30c1e672012-06-28 16:21:35 +08001545
1546static const struct platform_device_id flexcan_id_table[] = {
1547 { .name = "flexcan", .driver_data = (kernel_ulong_t)&fsl_p1010_devtype_data, },
1548 { /* sentinel */ },
1549};
Marc Kleine-Budde4358a9d2012-10-04 10:55:35 +02001550MODULE_DEVICE_TABLE(platform, flexcan_id_table);
Hui Wang30c1e672012-06-28 16:21:35 +08001551
Bill Pemberton3c8ac0f2012-12-03 09:22:44 -05001552static int flexcan_probe(struct platform_device *pdev)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001553{
Hui Wang30c1e672012-06-28 16:21:35 +08001554 const struct of_device_id *of_id;
Marc Kleine-Buddedda0b3b2012-07-13 14:52:48 +02001555 const struct flexcan_devtype_data *devtype_data;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001556 struct net_device *dev;
1557 struct flexcan_priv *priv;
Andreas Werner555828e2015-03-22 17:35:52 +01001558 struct regulator *reg_xceiver;
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001559 struct clk *clk_ipg = NULL, *clk_per = NULL;
Marc Kleine-Budde89af8742015-05-08 09:32:58 +02001560 struct flexcan_regs __iomem *regs;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001561 int err, irq;
Dong Aisheng8c306be2018-12-13 07:08:00 +00001562 u8 clk_src = 1;
holt@sgi.com97efe9a2011-08-16 17:32:23 +00001563 u32 clock_freq = 0;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001564
Andreas Werner555828e2015-03-22 17:35:52 +01001565 reg_xceiver = devm_regulator_get(&pdev->dev, "xceiver");
1566 if (PTR_ERR(reg_xceiver) == -EPROBE_DEFER)
1567 return -EPROBE_DEFER;
1568 else if (IS_ERR(reg_xceiver))
1569 reg_xceiver = NULL;
1570
Dong Aisheng8c306be2018-12-13 07:08:00 +00001571 if (pdev->dev.of_node) {
Hui Wangafc016d2012-06-28 16:21:34 +08001572 of_property_read_u32(pdev->dev.of_node,
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +02001573 "clock-frequency", &clock_freq);
Dong Aisheng8c306be2018-12-13 07:08:00 +00001574 of_property_read_u8(pdev->dev.of_node,
1575 "fsl,clk-source", &clk_src);
1576 }
holt@sgi.com97efe9a2011-08-16 17:32:23 +00001577
1578 if (!clock_freq) {
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001579 clk_ipg = devm_clk_get(&pdev->dev, "ipg");
1580 if (IS_ERR(clk_ipg)) {
1581 dev_err(&pdev->dev, "no ipg clock defined\n");
Fabio Estevam933e4af2013-07-22 12:41:39 -03001582 return PTR_ERR(clk_ipg);
holt@sgi.com97efe9a2011-08-16 17:32:23 +00001583 }
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001584
1585 clk_per = devm_clk_get(&pdev->dev, "per");
1586 if (IS_ERR(clk_per)) {
1587 dev_err(&pdev->dev, "no per clock defined\n");
Fabio Estevam933e4af2013-07-22 12:41:39 -03001588 return PTR_ERR(clk_per);
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001589 }
Marc Kleine-Budde1a3e5172013-11-25 22:15:20 +01001590 clock_freq = clk_get_rate(clk_per);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001591 }
1592
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001593 irq = platform_get_irq(pdev, 0);
Fabio Estevam933e4af2013-07-22 12:41:39 -03001594 if (irq <= 0)
1595 return -ENODEV;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001596
Joakim Zhanga4721f22019-09-29 08:32:09 +00001597 regs = devm_platform_ioremap_resource(pdev, 0);
Marc Kleine-Budde89af8742015-05-08 09:32:58 +02001598 if (IS_ERR(regs))
1599 return PTR_ERR(regs);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001600
Hui Wang30c1e672012-06-28 16:21:35 +08001601 of_id = of_match_device(flexcan_of_match, &pdev->dev);
1602 if (of_id) {
1603 devtype_data = of_id->data;
Marc Kleine-Budded0873e62014-03-04 22:04:22 +01001604 } else if (platform_get_device_id(pdev)->driver_data) {
Hui Wang30c1e672012-06-28 16:21:35 +08001605 devtype_data = (struct flexcan_devtype_data *)
Marc Kleine-Budded0873e62014-03-04 22:04:22 +01001606 platform_get_device_id(pdev)->driver_data;
Hui Wang30c1e672012-06-28 16:21:35 +08001607 } else {
Fabio Estevam933e4af2013-07-22 12:41:39 -03001608 return -ENODEV;
Hui Wang30c1e672012-06-28 16:21:35 +08001609 }
1610
Fabio Estevam933e4af2013-07-22 12:41:39 -03001611 dev = alloc_candev(sizeof(struct flexcan_priv), 1);
1612 if (!dev)
1613 return -ENOMEM;
1614
Marc Kleine-Budde30164752015-05-10 15:26:58 +02001615 platform_set_drvdata(pdev, dev);
1616 SET_NETDEV_DEV(dev, &pdev->dev);
1617
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001618 dev->netdev_ops = &flexcan_netdev_ops;
1619 dev->irq = irq;
Reuben Dowle9a123492011-11-01 11:18:03 +13001620 dev->flags |= IFF_ECHO;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001621
1622 priv = netdev_priv(dev);
Pankaj Bansal88462d22017-11-24 18:52:08 +05301623
Uwe Kleine-König0e030a32018-04-25 16:50:39 +02001624 if (of_property_read_bool(pdev->dev.of_node, "big-endian") ||
1625 devtype_data->quirks & FLEXCAN_QUIRK_DEFAULT_BIG_ENDIAN) {
Pankaj Bansal88462d22017-11-24 18:52:08 +05301626 priv->read = flexcan_read_be;
1627 priv->write = flexcan_write_be;
1628 } else {
Uwe Kleine-König0e030a32018-04-25 16:50:39 +02001629 priv->read = flexcan_read_le;
1630 priv->write = flexcan_write_le;
Pankaj Bansal88462d22017-11-24 18:52:08 +05301631 }
1632
Aisheng Dongca109892018-11-30 08:53:26 +00001633 priv->dev = &pdev->dev;
holt@sgi.com97efe9a2011-08-16 17:32:23 +00001634 priv->can.clock.freq = clock_freq;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001635 priv->can.bittiming_const = &flexcan_bittiming_const;
1636 priv->can.do_set_mode = flexcan_set_mode;
1637 priv->can.do_get_berr_counter = flexcan_get_berr_counter;
1638 priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK |
1639 CAN_CTRLMODE_LISTENONLY | CAN_CTRLMODE_3_SAMPLES |
1640 CAN_CTRLMODE_BERR_REPORTING;
Marc Kleine-Budde89af8742015-05-08 09:32:58 +02001641 priv->regs = regs;
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001642 priv->clk_ipg = clk_ipg;
1643 priv->clk_per = clk_per;
Dong Aisheng8c306be2018-12-13 07:08:00 +00001644 priv->clk_src = clk_src;
Hui Wang30c1e672012-06-28 16:21:35 +08001645 priv->devtype_data = devtype_data;
Andreas Werner555828e2015-03-22 17:35:52 +01001646 priv->reg_xceiver = reg_xceiver;
Fabio Estevamb7c41142013-06-10 23:12:57 -03001647
Aisheng Dongca109892018-11-30 08:53:26 +00001648 pm_runtime_get_noresume(&pdev->dev);
1649 pm_runtime_set_active(&pdev->dev);
1650 pm_runtime_enable(&pdev->dev);
1651
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001652 err = register_flexcandev(dev);
1653 if (err) {
1654 dev_err(&pdev->dev, "registering netdev failed\n");
1655 goto failed_register;
1656 }
1657
Fabio Baltieriadccadb2012-12-18 18:50:58 +01001658 devm_can_led_init(dev);
1659
Aisheng Dongde3578c2018-11-23 08:35:33 +00001660 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_SETUP_STOP_MODE) {
1661 err = flexcan_setup_stop_mode(pdev);
1662 if (err)
1663 dev_dbg(&pdev->dev, "failed to setup stop-mode\n");
1664 }
1665
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001666 return 0;
1667
1668 failed_register:
1669 free_candev(dev);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001670 return err;
1671}
1672
Bill Pemberton3c8ac0f2012-12-03 09:22:44 -05001673static int flexcan_remove(struct platform_device *pdev)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001674{
1675 struct net_device *dev = platform_get_drvdata(pdev);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001676
1677 unregister_flexcandev(dev);
Aisheng Dongca109892018-11-30 08:53:26 +00001678 pm_runtime_disable(&pdev->dev);
Marc Kleine-Budde9a275862010-10-21 05:07:58 +00001679 free_candev(dev);
1680
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001681 return 0;
1682}
1683
Marc Kleine-Budde08c6d352014-03-05 19:10:44 +01001684static int __maybe_unused flexcan_suspend(struct device *device)
Eric Bénard8b5e2182012-05-08 17:12:17 +02001685{
Fabio Estevam588e7a82013-05-20 15:43:43 -03001686 struct net_device *dev = dev_get_drvdata(device);
Eric Bénard8b5e2182012-05-08 17:12:17 +02001687 struct flexcan_priv *priv = netdev_priv(dev);
Aisheng Dongca109892018-11-30 08:53:26 +00001688 int err = 0;
Eric Bénard8b5e2182012-05-08 17:12:17 +02001689
Eric Bénard8b5e2182012-05-08 17:12:17 +02001690 if (netif_running(dev)) {
Aisheng Dongde3578c2018-11-23 08:35:33 +00001691 /* if wakeup is enabled, enter stop mode
1692 * else enter disabled mode.
1693 */
1694 if (device_may_wakeup(device)) {
1695 enable_irq_wake(dev->irq);
Joakim Zhang5f186c22019-07-02 01:45:41 +00001696 err = flexcan_enter_stop_mode(priv);
1697 if (err)
1698 return err;
Aisheng Dongde3578c2018-11-23 08:35:33 +00001699 } else {
1700 err = flexcan_chip_disable(priv);
1701 if (err)
1702 return err;
Aisheng Dongca109892018-11-30 08:53:26 +00001703
1704 err = pm_runtime_force_suspend(device);
Aisheng Dongde3578c2018-11-23 08:35:33 +00001705 }
Eric Bénard8b5e2182012-05-08 17:12:17 +02001706 netif_stop_queue(dev);
1707 netif_device_detach(dev);
1708 }
1709 priv->can.state = CAN_STATE_SLEEPING;
1710
Aisheng Dongca109892018-11-30 08:53:26 +00001711 return err;
Eric Bénard8b5e2182012-05-08 17:12:17 +02001712}
1713
Marc Kleine-Budde08c6d352014-03-05 19:10:44 +01001714static int __maybe_unused flexcan_resume(struct device *device)
Eric Bénard8b5e2182012-05-08 17:12:17 +02001715{
Fabio Estevam588e7a82013-05-20 15:43:43 -03001716 struct net_device *dev = dev_get_drvdata(device);
Eric Bénard8b5e2182012-05-08 17:12:17 +02001717 struct flexcan_priv *priv = netdev_priv(dev);
Aisheng Dongca109892018-11-30 08:53:26 +00001718 int err = 0;
Eric Bénard8b5e2182012-05-08 17:12:17 +02001719
1720 priv->can.state = CAN_STATE_ERROR_ACTIVE;
1721 if (netif_running(dev)) {
1722 netif_device_attach(dev);
1723 netif_start_queue(dev);
Aisheng Dongde3578c2018-11-23 08:35:33 +00001724 if (device_may_wakeup(device)) {
1725 disable_irq_wake(dev->irq);
Sean Nyekjaere7071802019-12-04 11:36:06 +00001726 err = flexcan_exit_stop_mode(priv);
1727 if (err)
1728 return err;
Aisheng Dongde3578c2018-11-23 08:35:33 +00001729 } else {
Aisheng Dongca109892018-11-30 08:53:26 +00001730 err = pm_runtime_force_resume(device);
Aisheng Dongde3578c2018-11-23 08:35:33 +00001731 if (err)
1732 return err;
Aisheng Dongca109892018-11-30 08:53:26 +00001733
1734 err = flexcan_chip_enable(priv);
Aisheng Dongde3578c2018-11-23 08:35:33 +00001735 }
Eric Bénard8b5e2182012-05-08 17:12:17 +02001736 }
Aisheng Dongca109892018-11-30 08:53:26 +00001737
1738 return err;
1739}
1740
1741static int __maybe_unused flexcan_runtime_suspend(struct device *device)
1742{
1743 struct net_device *dev = dev_get_drvdata(device);
1744 struct flexcan_priv *priv = netdev_priv(dev);
1745
1746 flexcan_clks_disable(priv);
1747
Fabio Estevam4de349e2016-08-17 12:41:08 -03001748 return 0;
Eric Bénard8b5e2182012-05-08 17:12:17 +02001749}
Fabio Estevam588e7a82013-05-20 15:43:43 -03001750
Aisheng Dongca109892018-11-30 08:53:26 +00001751static int __maybe_unused flexcan_runtime_resume(struct device *device)
1752{
1753 struct net_device *dev = dev_get_drvdata(device);
1754 struct flexcan_priv *priv = netdev_priv(dev);
1755
1756 return flexcan_clks_enable(priv);
1757}
1758
Aisheng Dongde3578c2018-11-23 08:35:33 +00001759static int __maybe_unused flexcan_noirq_suspend(struct device *device)
1760{
1761 struct net_device *dev = dev_get_drvdata(device);
1762 struct flexcan_priv *priv = netdev_priv(dev);
1763
1764 if (netif_running(dev) && device_may_wakeup(device))
1765 flexcan_enable_wakeup_irq(priv, true);
1766
1767 return 0;
1768}
1769
1770static int __maybe_unused flexcan_noirq_resume(struct device *device)
1771{
1772 struct net_device *dev = dev_get_drvdata(device);
1773 struct flexcan_priv *priv = netdev_priv(dev);
1774
Sean Nyekjaere7071802019-12-04 11:36:06 +00001775 if (netif_running(dev) && device_may_wakeup(device))
Aisheng Dongde3578c2018-11-23 08:35:33 +00001776 flexcan_enable_wakeup_irq(priv, false);
Aisheng Dongde3578c2018-11-23 08:35:33 +00001777
1778 return 0;
1779}
1780
1781static const struct dev_pm_ops flexcan_pm_ops = {
1782 SET_SYSTEM_SLEEP_PM_OPS(flexcan_suspend, flexcan_resume)
Aisheng Dongca109892018-11-30 08:53:26 +00001783 SET_RUNTIME_PM_OPS(flexcan_runtime_suspend, flexcan_runtime_resume, NULL)
Aisheng Dongde3578c2018-11-23 08:35:33 +00001784 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(flexcan_noirq_suspend, flexcan_noirq_resume)
1785};
Eric Bénard8b5e2182012-05-08 17:12:17 +02001786
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001787static struct platform_driver flexcan_driver = {
holt@sgi.comc8aef4c2011-08-16 17:32:22 +00001788 .driver = {
1789 .name = DRV_NAME,
Fabio Estevam588e7a82013-05-20 15:43:43 -03001790 .pm = &flexcan_pm_ops,
holt@sgi.comc8aef4c2011-08-16 17:32:22 +00001791 .of_match_table = flexcan_of_match,
1792 },
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001793 .probe = flexcan_probe,
Bill Pemberton3c8ac0f2012-12-03 09:22:44 -05001794 .remove = flexcan_remove,
Hui Wang30c1e672012-06-28 16:21:35 +08001795 .id_table = flexcan_id_table,
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001796};
1797
Axel Lin871d3372011-11-27 15:42:31 +00001798module_platform_driver(flexcan_driver);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001799
1800MODULE_AUTHOR("Sascha Hauer <kernel@pengutronix.de>, "
1801 "Marc Kleine-Budde <kernel@pengutronix.de>");
1802MODULE_LICENSE("GPL v2");
1803MODULE_DESCRIPTION("CAN port driver for flexcan based chip");