blob: 1cd5179cb87611ff3e6c36af2d13b6e94a49b0bb [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-Budde22233f72018-11-28 15:31:37 +0100145#define FLEXCAN_IFLAG_MB(x) BIT((x) & 0x1f)
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
233 * 0x090...0x0df 1-5 reserverd
234 * 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-Buddee955cea2009-07-29 10:20:10 +0200280 u32 reg_ctrl_default;
Marc Kleine-Budde28ac7dc2015-08-04 13:46:10 +0200281 u32 reg_imask1_default;
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200282 u32 reg_imask2_default;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200283
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
Aisheng Dongde3578c2018-11-23 08:35:33 +0000392static void flexcan_enable_wakeup_irq(struct flexcan_priv *priv, bool enable)
393{
394 struct flexcan_regs __iomem *regs = priv->regs;
395 u32 reg_mcr;
396
397 reg_mcr = priv->read(&regs->mcr);
398
399 if (enable)
400 reg_mcr |= FLEXCAN_MCR_WAK_MSK;
401 else
402 reg_mcr &= ~FLEXCAN_MCR_WAK_MSK;
403
404 priv->write(reg_mcr, &regs->mcr);
405}
406
Joakim Zhang5f186c22019-07-02 01:45:41 +0000407static inline int flexcan_enter_stop_mode(struct flexcan_priv *priv)
Aisheng Dongde3578c2018-11-23 08:35:33 +0000408{
409 struct flexcan_regs __iomem *regs = priv->regs;
Joakim Zhang5f186c22019-07-02 01:45:41 +0000410 unsigned int ackval;
Aisheng Dongde3578c2018-11-23 08:35:33 +0000411 u32 reg_mcr;
412
413 reg_mcr = priv->read(&regs->mcr);
414 reg_mcr |= FLEXCAN_MCR_SLF_WAK;
415 priv->write(reg_mcr, &regs->mcr);
416
417 /* enable stop request */
418 regmap_update_bits(priv->stm.gpr, priv->stm.req_gpr,
419 1 << priv->stm.req_bit, 1 << priv->stm.req_bit);
Joakim Zhang5f186c22019-07-02 01:45:41 +0000420
421 /* get stop acknowledgment */
422 if (regmap_read_poll_timeout(priv->stm.gpr, priv->stm.ack_gpr,
423 ackval, ackval & (1 << priv->stm.ack_bit),
424 0, FLEXCAN_TIMEOUT_US))
425 return -ETIMEDOUT;
426
427 return 0;
Aisheng Dongde3578c2018-11-23 08:35:33 +0000428}
429
Joakim Zhang5f186c22019-07-02 01:45:41 +0000430static inline int flexcan_exit_stop_mode(struct flexcan_priv *priv)
Aisheng Dongde3578c2018-11-23 08:35:33 +0000431{
432 struct flexcan_regs __iomem *regs = priv->regs;
Joakim Zhang5f186c22019-07-02 01:45:41 +0000433 unsigned int ackval;
Aisheng Dongde3578c2018-11-23 08:35:33 +0000434 u32 reg_mcr;
435
436 /* remove stop request */
437 regmap_update_bits(priv->stm.gpr, priv->stm.req_gpr,
438 1 << priv->stm.req_bit, 0);
439
Joakim Zhang5f186c22019-07-02 01:45:41 +0000440 /* get stop acknowledgment */
441 if (regmap_read_poll_timeout(priv->stm.gpr, priv->stm.ack_gpr,
442 ackval, !(ackval & (1 << priv->stm.ack_bit)),
443 0, FLEXCAN_TIMEOUT_US))
444 return -ETIMEDOUT;
445
Aisheng Dongde3578c2018-11-23 08:35:33 +0000446 reg_mcr = priv->read(&regs->mcr);
447 reg_mcr &= ~FLEXCAN_MCR_SLF_WAK;
448 priv->write(reg_mcr, &regs->mcr);
Joakim Zhang5f186c22019-07-02 01:45:41 +0000449
450 return 0;
Aisheng Dongde3578c2018-11-23 08:35:33 +0000451}
452
ZHU Yi (ST-FIR/ENG1-Zhu)da49a802017-09-15 07:03:58 +0000453static inline void flexcan_error_irq_enable(const struct flexcan_priv *priv)
454{
455 struct flexcan_regs __iomem *regs = priv->regs;
456 u32 reg_ctrl = (priv->reg_ctrl_default | FLEXCAN_CTRL_ERR_MSK);
457
Pankaj Bansal88462d22017-11-24 18:52:08 +0530458 priv->write(reg_ctrl, &regs->ctrl);
ZHU Yi (ST-FIR/ENG1-Zhu)da49a802017-09-15 07:03:58 +0000459}
460
461static inline void flexcan_error_irq_disable(const struct flexcan_priv *priv)
462{
463 struct flexcan_regs __iomem *regs = priv->regs;
464 u32 reg_ctrl = (priv->reg_ctrl_default & ~FLEXCAN_CTRL_ERR_MSK);
465
Pankaj Bansal88462d22017-11-24 18:52:08 +0530466 priv->write(reg_ctrl, &regs->ctrl);
ZHU Yi (ST-FIR/ENG1-Zhu)da49a802017-09-15 07:03:58 +0000467}
468
Aisheng Dongca109892018-11-30 08:53:26 +0000469static int flexcan_clks_enable(const struct flexcan_priv *priv)
470{
471 int err;
472
473 err = clk_prepare_enable(priv->clk_ipg);
474 if (err)
475 return err;
476
477 err = clk_prepare_enable(priv->clk_per);
478 if (err)
479 clk_disable_unprepare(priv->clk_ipg);
480
481 return err;
482}
483
484static void flexcan_clks_disable(const struct flexcan_priv *priv)
485{
486 clk_disable_unprepare(priv->clk_per);
487 clk_disable_unprepare(priv->clk_ipg);
488}
489
Marc Kleine-Buddef0036982014-02-28 17:18:27 +0100490static inline int flexcan_transceiver_enable(const struct flexcan_priv *priv)
491{
492 if (!priv->reg_xceiver)
493 return 0;
494
495 return regulator_enable(priv->reg_xceiver);
496}
497
498static inline int flexcan_transceiver_disable(const struct flexcan_priv *priv)
499{
500 if (!priv->reg_xceiver)
501 return 0;
502
503 return regulator_disable(priv->reg_xceiver);
504}
505
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100506static int flexcan_chip_enable(struct flexcan_priv *priv)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200507{
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200508 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100509 unsigned int timeout = FLEXCAN_TIMEOUT_US / 10;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200510 u32 reg;
511
Pankaj Bansal88462d22017-11-24 18:52:08 +0530512 reg = priv->read(&regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200513 reg &= ~FLEXCAN_MCR_MDIS;
Pankaj Bansal88462d22017-11-24 18:52:08 +0530514 priv->write(reg, &regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200515
Pankaj Bansal88462d22017-11-24 18:52:08 +0530516 while (timeout-- && (priv->read(&regs->mcr) & FLEXCAN_MCR_LPM_ACK))
David Jander8badd652014-08-27 12:02:16 +0200517 udelay(10);
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100518
Pankaj Bansal88462d22017-11-24 18:52:08 +0530519 if (priv->read(&regs->mcr) & FLEXCAN_MCR_LPM_ACK)
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100520 return -ETIMEDOUT;
521
522 return 0;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200523}
524
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100525static int flexcan_chip_disable(struct flexcan_priv *priv)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200526{
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200527 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100528 unsigned int timeout = FLEXCAN_TIMEOUT_US / 10;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200529 u32 reg;
530
Pankaj Bansal88462d22017-11-24 18:52:08 +0530531 reg = priv->read(&regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200532 reg |= FLEXCAN_MCR_MDIS;
Pankaj Bansal88462d22017-11-24 18:52:08 +0530533 priv->write(reg, &regs->mcr);
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100534
Pankaj Bansal88462d22017-11-24 18:52:08 +0530535 while (timeout-- && !(priv->read(&regs->mcr) & FLEXCAN_MCR_LPM_ACK))
David Jander8badd652014-08-27 12:02:16 +0200536 udelay(10);
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100537
Pankaj Bansal88462d22017-11-24 18:52:08 +0530538 if (!(priv->read(&regs->mcr) & FLEXCAN_MCR_LPM_ACK))
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100539 return -ETIMEDOUT;
540
541 return 0;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200542}
543
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100544static int flexcan_chip_freeze(struct flexcan_priv *priv)
545{
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200546 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100547 unsigned int timeout = 1000 * 1000 * 10 / priv->can.bittiming.bitrate;
548 u32 reg;
549
Pankaj Bansal88462d22017-11-24 18:52:08 +0530550 reg = priv->read(&regs->mcr);
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100551 reg |= FLEXCAN_MCR_HALT;
Pankaj Bansal88462d22017-11-24 18:52:08 +0530552 priv->write(reg, &regs->mcr);
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100553
Pankaj Bansal88462d22017-11-24 18:52:08 +0530554 while (timeout-- && !(priv->read(&regs->mcr) & FLEXCAN_MCR_FRZ_ACK))
David Jander8badd652014-08-27 12:02:16 +0200555 udelay(100);
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100556
Pankaj Bansal88462d22017-11-24 18:52:08 +0530557 if (!(priv->read(&regs->mcr) & FLEXCAN_MCR_FRZ_ACK))
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100558 return -ETIMEDOUT;
559
560 return 0;
561}
562
563static int flexcan_chip_unfreeze(struct flexcan_priv *priv)
564{
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200565 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100566 unsigned int timeout = FLEXCAN_TIMEOUT_US / 10;
567 u32 reg;
568
Pankaj Bansal88462d22017-11-24 18:52:08 +0530569 reg = priv->read(&regs->mcr);
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100570 reg &= ~FLEXCAN_MCR_HALT;
Pankaj Bansal88462d22017-11-24 18:52:08 +0530571 priv->write(reg, &regs->mcr);
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100572
Pankaj Bansal88462d22017-11-24 18:52:08 +0530573 while (timeout-- && (priv->read(&regs->mcr) & FLEXCAN_MCR_FRZ_ACK))
David Jander8badd652014-08-27 12:02:16 +0200574 udelay(10);
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100575
Pankaj Bansal88462d22017-11-24 18:52:08 +0530576 if (priv->read(&regs->mcr) & FLEXCAN_MCR_FRZ_ACK)
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100577 return -ETIMEDOUT;
578
579 return 0;
580}
581
Marc Kleine-Budde4b5b8222014-02-28 15:16:59 +0100582static int flexcan_chip_softreset(struct flexcan_priv *priv)
583{
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200584 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Budde4b5b8222014-02-28 15:16:59 +0100585 unsigned int timeout = FLEXCAN_TIMEOUT_US / 10;
586
Pankaj Bansal88462d22017-11-24 18:52:08 +0530587 priv->write(FLEXCAN_MCR_SOFTRST, &regs->mcr);
588 while (timeout-- && (priv->read(&regs->mcr) & FLEXCAN_MCR_SOFTRST))
David Jander8badd652014-08-27 12:02:16 +0200589 udelay(10);
Marc Kleine-Budde4b5b8222014-02-28 15:16:59 +0100590
Pankaj Bansal88462d22017-11-24 18:52:08 +0530591 if (priv->read(&regs->mcr) & FLEXCAN_MCR_SOFTRST)
Marc Kleine-Budde4b5b8222014-02-28 15:16:59 +0100592 return -ETIMEDOUT;
593
594 return 0;
595}
596
Stefan Agnerec56acf2014-07-15 14:56:20 +0200597static int __flexcan_get_berr_counter(const struct net_device *dev,
598 struct can_berr_counter *bec)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200599{
600 const struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200601 struct flexcan_regs __iomem *regs = priv->regs;
Pankaj Bansal88462d22017-11-24 18:52:08 +0530602 u32 reg = priv->read(&regs->ecr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200603
604 bec->txerr = (reg >> 0) & 0xff;
605 bec->rxerr = (reg >> 8) & 0xff;
606
607 return 0;
608}
609
Stefan Agnerec56acf2014-07-15 14:56:20 +0200610static int flexcan_get_berr_counter(const struct net_device *dev,
611 struct can_berr_counter *bec)
612{
613 const struct flexcan_priv *priv = netdev_priv(dev);
614 int err;
615
Aisheng Dongca109892018-11-30 08:53:26 +0000616 err = pm_runtime_get_sync(priv->dev);
617 if (err < 0)
Stefan Agnerec56acf2014-07-15 14:56:20 +0200618 return err;
619
Stefan Agnerec56acf2014-07-15 14:56:20 +0200620 err = __flexcan_get_berr_counter(dev, bec);
621
Aisheng Dongca109892018-11-30 08:53:26 +0000622 pm_runtime_put(priv->dev);
Stefan Agnerec56acf2014-07-15 14:56:20 +0200623
624 return err;
625}
626
Marc Kleine-Buddefb1e13e62018-04-26 23:13:38 +0200627static netdev_tx_t flexcan_start_xmit(struct sk_buff *skb, struct net_device *dev)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200628{
629 const struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200630 struct can_frame *cf = (struct can_frame *)skb->data;
631 u32 can_id;
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200632 u32 data;
Marc Kleine-Budde10d089b2014-09-23 11:18:11 +0200633 u32 ctrl = FLEXCAN_MB_CODE_TX_DATA | (cf->can_dlc << 16);
Pankaj Bansal05179612018-11-23 22:18:44 +0100634 int i;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200635
636 if (can_dropped_invalid_skb(dev, skb))
637 return NETDEV_TX_OK;
638
639 netif_stop_queue(dev);
640
641 if (cf->can_id & CAN_EFF_FLAG) {
642 can_id = cf->can_id & CAN_EFF_MASK;
643 ctrl |= FLEXCAN_MB_CNT_IDE | FLEXCAN_MB_CNT_SRR;
644 } else {
645 can_id = (cf->can_id & CAN_SFF_MASK) << 18;
646 }
647
648 if (cf->can_id & CAN_RTR_FLAG)
649 ctrl |= FLEXCAN_MB_CNT_RTR;
650
Pankaj Bansal05179612018-11-23 22:18:44 +0100651 for (i = 0; i < cf->can_dlc; i += sizeof(u32)) {
652 data = be32_to_cpup((__be32 *)&cf->data[i]);
653 priv->write(data, &priv->tx_mb->data[i / sizeof(u32)]);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200654 }
655
Reuben Dowle9a123492011-11-01 11:18:03 +1300656 can_put_echo_skb(skb, dev, 0);
657
Pankaj Bansal05179612018-11-23 22:18:44 +0100658 priv->write(can_id, &priv->tx_mb->can_id);
659 priv->write(ctrl, &priv->tx_mb->can_ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200660
David Jander25e92442014-09-03 16:47:22 +0200661 /* Errata ERR005829 step8:
662 * Write twice INACTIVE(0x8) code to first MB.
663 */
Pankaj Bansal88462d22017-11-24 18:52:08 +0530664 priv->write(FLEXCAN_MB_CODE_TX_INACTIVE,
Marc Kleine-Budde9dc1ee12018-11-12 15:33:57 +0100665 &priv->tx_mb_reserved->can_ctrl);
Pankaj Bansal88462d22017-11-24 18:52:08 +0530666 priv->write(FLEXCAN_MB_CODE_TX_INACTIVE,
Marc Kleine-Budde9dc1ee12018-11-12 15:33:57 +0100667 &priv->tx_mb_reserved->can_ctrl);
David Jander25e92442014-09-03 16:47:22 +0200668
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200669 return NETDEV_TX_OK;
670}
671
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200672static void flexcan_irq_bus_err(struct net_device *dev, u32 reg_esr)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200673{
674 struct flexcan_priv *priv = netdev_priv(dev);
Oleksij Rempeld788905f2018-09-18 11:40:41 +0200675 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddea5c02f662017-01-18 11:38:26 +0100676 struct sk_buff *skb;
677 struct can_frame *cf;
Marc Kleine-Budded166f562017-01-17 17:33:46 +0100678 bool rx_errors = false, tx_errors = false;
Oleksij Rempeld788905f2018-09-18 11:40:41 +0200679 u32 timestamp;
680
681 timestamp = priv->read(&regs->timer) << 16;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200682
Marc Kleine-Buddea5c02f662017-01-18 11:38:26 +0100683 skb = alloc_can_err_skb(dev, &cf);
684 if (unlikely(!skb))
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200685 return;
Marc Kleine-Buddea5c02f662017-01-18 11:38:26 +0100686
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200687 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
688
689 if (reg_esr & FLEXCAN_ESR_BIT1_ERR) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100690 netdev_dbg(dev, "BIT1_ERR irq\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200691 cf->data[2] |= CAN_ERR_PROT_BIT1;
Marc Kleine-Budded166f562017-01-17 17:33:46 +0100692 tx_errors = true;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200693 }
694 if (reg_esr & FLEXCAN_ESR_BIT0_ERR) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100695 netdev_dbg(dev, "BIT0_ERR irq\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200696 cf->data[2] |= CAN_ERR_PROT_BIT0;
Marc Kleine-Budded166f562017-01-17 17:33:46 +0100697 tx_errors = true;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200698 }
699 if (reg_esr & FLEXCAN_ESR_ACK_ERR) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100700 netdev_dbg(dev, "ACK_ERR irq\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200701 cf->can_id |= CAN_ERR_ACK;
Oliver Hartkoppffd461f2015-11-21 18:41:20 +0100702 cf->data[3] = CAN_ERR_PROT_LOC_ACK;
Marc Kleine-Budded166f562017-01-17 17:33:46 +0100703 tx_errors = true;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200704 }
705 if (reg_esr & FLEXCAN_ESR_CRC_ERR) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100706 netdev_dbg(dev, "CRC_ERR irq\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200707 cf->data[2] |= CAN_ERR_PROT_BIT;
Oliver Hartkoppffd461f2015-11-21 18:41:20 +0100708 cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ;
Marc Kleine-Budded166f562017-01-17 17:33:46 +0100709 rx_errors = true;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200710 }
711 if (reg_esr & FLEXCAN_ESR_FRM_ERR) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100712 netdev_dbg(dev, "FRM_ERR irq\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200713 cf->data[2] |= CAN_ERR_PROT_FORM;
Marc Kleine-Budded166f562017-01-17 17:33:46 +0100714 rx_errors = true;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200715 }
716 if (reg_esr & FLEXCAN_ESR_STF_ERR) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100717 netdev_dbg(dev, "STF_ERR irq\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200718 cf->data[2] |= CAN_ERR_PROT_STUFF;
Marc Kleine-Budded166f562017-01-17 17:33:46 +0100719 rx_errors = true;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200720 }
721
722 priv->can.can_stats.bus_error++;
723 if (rx_errors)
724 dev->stats.rx_errors++;
725 if (tx_errors)
726 dev->stats.tx_errors++;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200727
Oleksij Rempeld788905f2018-09-18 11:40:41 +0200728 can_rx_offload_queue_sorted(&priv->offload, skb, timestamp);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200729}
730
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200731static void flexcan_irq_state(struct net_device *dev, u32 reg_esr)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200732{
733 struct flexcan_priv *priv = netdev_priv(dev);
Oleksij Rempeld788905f2018-09-18 11:40:41 +0200734 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200735 struct sk_buff *skb;
736 struct can_frame *cf;
Marc Kleine-Budde238443d2017-01-18 11:25:41 +0100737 enum can_state new_state, rx_state, tx_state;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200738 int flt;
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000739 struct can_berr_counter bec;
Oleksij Rempeld788905f2018-09-18 11:40:41 +0200740 u32 timestamp;
741
742 timestamp = priv->read(&regs->timer) << 16;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200743
744 flt = reg_esr & FLEXCAN_ESR_FLT_CONF_MASK;
745 if (likely(flt == FLEXCAN_ESR_FLT_CONF_ACTIVE)) {
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000746 tx_state = unlikely(reg_esr & FLEXCAN_ESR_TX_WRN) ?
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200747 CAN_STATE_ERROR_WARNING : CAN_STATE_ERROR_ACTIVE;
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000748 rx_state = unlikely(reg_esr & FLEXCAN_ESR_RX_WRN) ?
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200749 CAN_STATE_ERROR_WARNING : CAN_STATE_ERROR_ACTIVE;
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000750 new_state = max(tx_state, rx_state);
Andri Yngvason258ce802015-03-17 13:03:09 +0000751 } else {
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000752 __flexcan_get_berr_counter(dev, &bec);
Andri Yngvason258ce802015-03-17 13:03:09 +0000753 new_state = flt == FLEXCAN_ESR_FLT_CONF_PASSIVE ?
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200754 CAN_STATE_ERROR_PASSIVE : CAN_STATE_BUS_OFF;
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000755 rx_state = bec.rxerr >= bec.txerr ? new_state : 0;
756 tx_state = bec.rxerr <= bec.txerr ? new_state : 0;
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000757 }
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200758
759 /* state hasn't changed */
760 if (likely(new_state == priv->can.state))
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200761 return;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200762
763 skb = alloc_can_err_skb(dev, &cf);
764 if (unlikely(!skb))
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200765 return;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200766
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000767 can_change_state(dev, cf, tx_state, rx_state);
768
769 if (unlikely(new_state == CAN_STATE_BUS_OFF))
770 can_bus_off(dev);
771
Oleksij Rempeld788905f2018-09-18 11:40:41 +0200772 can_rx_offload_queue_sorted(&priv->offload, skb, timestamp);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200773}
774
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200775static inline struct flexcan_priv *rx_offload_to_priv(struct can_rx_offload *offload)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200776{
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200777 return container_of(offload, struct flexcan_priv, offload);
778}
779
780static unsigned int flexcan_mailbox_read(struct can_rx_offload *offload,
781 struct can_frame *cf,
782 u32 *timestamp, unsigned int n)
783{
784 struct flexcan_priv *priv = rx_offload_to_priv(offload);
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200785 struct flexcan_regs __iomem *regs = priv->regs;
Pankaj Bansal05179612018-11-23 22:18:44 +0100786 struct flexcan_mb __iomem *mb;
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200787 u32 reg_ctrl, reg_id, reg_iflag1;
Pankaj Bansal05179612018-11-23 22:18:44 +0100788 int i;
789
790 mb = flexcan_get_mb(priv, n);
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200791
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200792 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP) {
793 u32 code;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200794
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200795 do {
Pankaj Bansal88462d22017-11-24 18:52:08 +0530796 reg_ctrl = priv->read(&mb->can_ctrl);
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200797 } while (reg_ctrl & FLEXCAN_MB_CODE_RX_BUSY_BIT);
798
799 /* is this MB empty? */
800 code = reg_ctrl & FLEXCAN_MB_CODE_MASK;
801 if ((code != FLEXCAN_MB_CODE_RX_FULL) &&
802 (code != FLEXCAN_MB_CODE_RX_OVERRUN))
803 return 0;
804
805 if (code == FLEXCAN_MB_CODE_RX_OVERRUN) {
806 /* This MB was overrun, we lost data */
807 offload->dev->stats.rx_over_errors++;
808 offload->dev->stats.rx_errors++;
809 }
810 } else {
Pankaj Bansal88462d22017-11-24 18:52:08 +0530811 reg_iflag1 = priv->read(&regs->iflag1);
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200812 if (!(reg_iflag1 & FLEXCAN_IFLAG_RX_FIFO_AVAILABLE))
813 return 0;
814
Pankaj Bansal88462d22017-11-24 18:52:08 +0530815 reg_ctrl = priv->read(&mb->can_ctrl);
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200816 }
817
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200818 /* increase timstamp to full 32 bit */
819 *timestamp = reg_ctrl << 16;
820
Pankaj Bansal88462d22017-11-24 18:52:08 +0530821 reg_id = priv->read(&mb->can_id);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200822 if (reg_ctrl & FLEXCAN_MB_CNT_IDE)
823 cf->can_id = ((reg_id >> 0) & CAN_EFF_MASK) | CAN_EFF_FLAG;
824 else
825 cf->can_id = (reg_id >> 18) & CAN_SFF_MASK;
826
827 if (reg_ctrl & FLEXCAN_MB_CNT_RTR)
828 cf->can_id |= CAN_RTR_FLAG;
829 cf->can_dlc = get_can_dlc((reg_ctrl >> 16) & 0xf);
830
Pankaj Bansal05179612018-11-23 22:18:44 +0100831 for (i = 0; i < cf->can_dlc; i += sizeof(u32)) {
832 __be32 data = cpu_to_be32(priv->read(&mb->data[i / sizeof(u32)]));
833 *(__be32 *)(cf->data + i) = data;
834 }
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200835
836 /* mark as read */
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200837 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP) {
838 /* Clear IRQ */
839 if (n < 32)
Pankaj Bansal88462d22017-11-24 18:52:08 +0530840 priv->write(BIT(n), &regs->iflag1);
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200841 else
Pankaj Bansal88462d22017-11-24 18:52:08 +0530842 priv->write(BIT(n - 32), &regs->iflag2);
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200843 } else {
Pankaj Bansal88462d22017-11-24 18:52:08 +0530844 priv->write(FLEXCAN_IFLAG_RX_FIFO_AVAILABLE, &regs->iflag1);
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200845 }
Fabio Baltieriadccadb2012-12-18 18:50:58 +0100846
Pankaj Bansal5178b7c2018-08-01 19:36:46 +0530847 /* Read the Free Running Timer. It is optional but recommended
848 * to unlock Mailbox as soon as possible and make it available
849 * for reception.
850 */
851 priv->read(&regs->timer);
852
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200853 return 1;
854}
855
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200856
857static inline u64 flexcan_read_reg_iflag_rx(struct flexcan_priv *priv)
858{
859 struct flexcan_regs __iomem *regs = priv->regs;
860 u32 iflag1, iflag2;
861
Alexander Steincbffaf72018-10-11 17:01:25 +0200862 iflag2 = priv->read(&regs->iflag2) & priv->reg_imask2_default &
Pankaj Bansal05179612018-11-23 22:18:44 +0100863 ~FLEXCAN_IFLAG_MB(priv->tx_mb_idx);
Alexander Steincbffaf72018-10-11 17:01:25 +0200864 iflag1 = priv->read(&regs->iflag1) & priv->reg_imask1_default;
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200865
866 return (u64)iflag2 << 32 | iflag1;
867}
868
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200869static irqreturn_t flexcan_irq(int irq, void *dev_id)
870{
871 struct net_device *dev = dev_id;
872 struct net_device_stats *stats = &dev->stats;
873 struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200874 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddedd2f1222017-01-18 11:45:14 +0100875 irqreturn_t handled = IRQ_NONE;
Alexander Steincbffaf72018-10-11 17:01:25 +0200876 u32 reg_iflag2, reg_esr;
ZHU Yi (ST-FIR/ENG1-Zhu)da49a802017-09-15 07:03:58 +0000877 enum can_state last_state = priv->can.state;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200878
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200879 /* reception interrupt */
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200880 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP) {
881 u64 reg_iflag;
882 int ret;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200883
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200884 while ((reg_iflag = flexcan_read_reg_iflag_rx(priv))) {
885 handled = IRQ_HANDLED;
886 ret = can_rx_offload_irq_offload_timestamp(&priv->offload,
887 reg_iflag);
888 if (!ret)
889 break;
890 }
891 } else {
Alexander Steincbffaf72018-10-11 17:01:25 +0200892 u32 reg_iflag1;
893
894 reg_iflag1 = priv->read(&regs->iflag1);
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200895 if (reg_iflag1 & FLEXCAN_IFLAG_RX_FIFO_AVAILABLE) {
896 handled = IRQ_HANDLED;
897 can_rx_offload_irq_offload_fifo(&priv->offload);
898 }
899
900 /* FIFO overflow interrupt */
901 if (reg_iflag1 & FLEXCAN_IFLAG_RX_FIFO_OVERFLOW) {
902 handled = IRQ_HANDLED;
Pankaj Bansal88462d22017-11-24 18:52:08 +0530903 priv->write(FLEXCAN_IFLAG_RX_FIFO_OVERFLOW,
904 &regs->iflag1);
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200905 dev->stats.rx_over_errors++;
906 dev->stats.rx_errors++;
907 }
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200908 }
909
Alexander Steincbffaf72018-10-11 17:01:25 +0200910 reg_iflag2 = priv->read(&regs->iflag2);
911
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200912 /* transmission complete interrupt */
Pankaj Bansal05179612018-11-23 22:18:44 +0100913 if (reg_iflag2 & FLEXCAN_IFLAG_MB(priv->tx_mb_idx)) {
914 u32 reg_ctrl = priv->read(&priv->tx_mb->can_ctrl);
Oleksij Rempeled72bc82018-09-18 11:40:39 +0200915
Marc Kleine-Buddedd2f1222017-01-18 11:45:14 +0100916 handled = IRQ_HANDLED;
Oleksij Rempeled72bc82018-09-18 11:40:39 +0200917 stats->tx_bytes += can_rx_offload_get_echo_skb(&priv->offload,
918 0, reg_ctrl << 16);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200919 stats->tx_packets++;
Fabio Baltieriadccadb2012-12-18 18:50:58 +0100920 can_led_event(dev, CAN_LED_EVENT_TX);
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200921
922 /* after sending a RTR frame MB is in RX mode */
Pankaj Bansal88462d22017-11-24 18:52:08 +0530923 priv->write(FLEXCAN_MB_CODE_TX_INACTIVE,
Pankaj Bansal05179612018-11-23 22:18:44 +0100924 &priv->tx_mb->can_ctrl);
925 priv->write(FLEXCAN_IFLAG_MB(priv->tx_mb_idx), &regs->iflag2);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200926 netif_wake_queue(dev);
927 }
928
Pankaj Bansal88462d22017-11-24 18:52:08 +0530929 reg_esr = priv->read(&regs->esr);
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200930
Marc Kleine-Buddedd2f1222017-01-18 11:45:14 +0100931 /* ACK all bus error and state change IRQ sources */
932 if (reg_esr & FLEXCAN_ESR_ALL_INT) {
933 handled = IRQ_HANDLED;
Pankaj Bansal88462d22017-11-24 18:52:08 +0530934 priv->write(reg_esr & FLEXCAN_ESR_ALL_INT, &regs->esr);
Marc Kleine-Buddedd2f1222017-01-18 11:45:14 +0100935 }
936
ZHU Yi (ST-FIR/ENG1-Zhu)ad230232017-09-15 06:59:15 +0000937 /* state change interrupt or broken error state quirk fix is enabled */
938 if ((reg_esr & FLEXCAN_ESR_ERR_STATE) ||
ZHU Yi (ST-FIR/ENG1-Zhu)da49a802017-09-15 07:03:58 +0000939 (priv->devtype_data->quirks & (FLEXCAN_QUIRK_BROKEN_WERR_STATE |
Marc Kleine-Buddebc8ad652018-11-28 15:45:27 +0100940 FLEXCAN_QUIRK_BROKEN_PERR_STATE)))
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200941 flexcan_irq_state(dev, reg_esr);
942
943 /* bus error IRQ - handle if bus error reporting is activated */
944 if ((reg_esr & FLEXCAN_ESR_ERR_BUS) &&
945 (priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING))
946 flexcan_irq_bus_err(dev, reg_esr);
947
ZHU Yi (ST-FIR/ENG1-Zhu)da49a802017-09-15 07:03:58 +0000948 /* availability of error interrupt among state transitions in case
949 * bus error reporting is de-activated and
950 * FLEXCAN_QUIRK_BROKEN_PERR_STATE is enabled:
951 * +--------------------------------------------------------------+
952 * | +----------------------------------------------+ [stopped / |
953 * | | | sleeping] -+
954 * +-+-> active <-> warning <-> passive -> bus off -+
955 * ___________^^^^^^^^^^^^_______________________________
956 * disabled(1) enabled disabled
957 *
958 * (1): enabled if FLEXCAN_QUIRK_BROKEN_WERR_STATE is enabled
959 */
960 if ((last_state != priv->can.state) &&
961 (priv->devtype_data->quirks & FLEXCAN_QUIRK_BROKEN_PERR_STATE) &&
962 !(priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING)) {
963 switch (priv->can.state) {
964 case CAN_STATE_ERROR_ACTIVE:
965 if (priv->devtype_data->quirks &
966 FLEXCAN_QUIRK_BROKEN_WERR_STATE)
967 flexcan_error_irq_enable(priv);
968 else
969 flexcan_error_irq_disable(priv);
970 break;
971
972 case CAN_STATE_ERROR_WARNING:
973 flexcan_error_irq_enable(priv);
974 break;
975
976 case CAN_STATE_ERROR_PASSIVE:
977 case CAN_STATE_BUS_OFF:
978 flexcan_error_irq_disable(priv);
979 break;
980
981 default:
982 break;
983 }
984 }
985
Marc Kleine-Buddedd2f1222017-01-18 11:45:14 +0100986 return handled;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200987}
988
989static void flexcan_set_bittiming(struct net_device *dev)
990{
991 const struct flexcan_priv *priv = netdev_priv(dev);
992 const struct can_bittiming *bt = &priv->can.bittiming;
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200993 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200994 u32 reg;
995
Pankaj Bansal88462d22017-11-24 18:52:08 +0530996 reg = priv->read(&regs->ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200997 reg &= ~(FLEXCAN_CTRL_PRESDIV(0xff) |
998 FLEXCAN_CTRL_RJW(0x3) |
999 FLEXCAN_CTRL_PSEG1(0x7) |
1000 FLEXCAN_CTRL_PSEG2(0x7) |
1001 FLEXCAN_CTRL_PROPSEG(0x7) |
1002 FLEXCAN_CTRL_LPB |
1003 FLEXCAN_CTRL_SMP |
1004 FLEXCAN_CTRL_LOM);
1005
1006 reg |= FLEXCAN_CTRL_PRESDIV(bt->brp - 1) |
1007 FLEXCAN_CTRL_PSEG1(bt->phase_seg1 - 1) |
1008 FLEXCAN_CTRL_PSEG2(bt->phase_seg2 - 1) |
1009 FLEXCAN_CTRL_RJW(bt->sjw - 1) |
1010 FLEXCAN_CTRL_PROPSEG(bt->prop_seg - 1);
1011
1012 if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)
1013 reg |= FLEXCAN_CTRL_LPB;
1014 if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
1015 reg |= FLEXCAN_CTRL_LOM;
1016 if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
1017 reg |= FLEXCAN_CTRL_SMP;
1018
Lucas Stach7a4b6c82015-08-07 17:16:03 +02001019 netdev_dbg(dev, "writing ctrl=0x%08x\n", reg);
Pankaj Bansal88462d22017-11-24 18:52:08 +05301020 priv->write(reg, &regs->ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001021
1022 /* print chip status */
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +01001023 netdev_dbg(dev, "%s: mcr=0x%08x ctrl=0x%08x\n", __func__,
Pankaj Bansal88462d22017-11-24 18:52:08 +05301024 priv->read(&regs->mcr), priv->read(&regs->ctrl));
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001025}
1026
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +02001027/* flexcan_chip_start
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001028 *
1029 * this functions is entered with clocks enabled
1030 *
1031 */
1032static int flexcan_chip_start(struct net_device *dev)
1033{
1034 struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Budde89af8742015-05-08 09:32:58 +02001035 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Budde6f75fce2014-09-23 11:03:01 +02001036 u32 reg_mcr, reg_ctrl, reg_ctrl2, reg_mecr;
David S. Miller1f6d8032014-09-23 12:09:27 -04001037 int err, i;
Pankaj Bansal05179612018-11-23 22:18:44 +01001038 struct flexcan_mb __iomem *mb;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001039
1040 /* enable module */
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001041 err = flexcan_chip_enable(priv);
1042 if (err)
1043 return err;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001044
1045 /* soft reset */
Marc Kleine-Budde4b5b8222014-02-28 15:16:59 +01001046 err = flexcan_chip_softreset(priv);
1047 if (err)
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +01001048 goto out_chip_disable;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001049
1050 flexcan_set_bittiming(dev);
1051
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +02001052 /* MCR
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001053 *
1054 * enable freeze
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001055 * halt now
1056 * only supervisor access
1057 * enable warning int
Marc Kleine-Budde4bd888a2015-08-31 21:03:29 +02001058 * enable individual RX masking
Marc Kleine-Budde749de6f2015-08-31 21:32:34 +02001059 * choose format C
1060 * set max mailbox number
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001061 */
Pankaj Bansal88462d22017-11-24 18:52:08 +05301062 reg_mcr = priv->read(&regs->mcr);
Marc Kleine-Budded5a7b402013-10-04 10:52:36 +02001063 reg_mcr &= ~FLEXCAN_MCR_MAXMB(0xff);
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +02001064 reg_mcr |= FLEXCAN_MCR_FRZ | FLEXCAN_MCR_HALT | FLEXCAN_MCR_SUPV |
Pankaj Bansal7ad0f532018-08-13 23:50:48 +05301065 FLEXCAN_MCR_WRN_EN | FLEXCAN_MCR_IRMQ | FLEXCAN_MCR_IDAM_C |
Pankaj Bansal05179612018-11-23 22:18:44 +01001066 FLEXCAN_MCR_MAXMB(priv->tx_mb_idx);
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +02001067
Marc Kleine-Buddec982a3ca2018-08-17 14:52:58 +02001068 /* MCR
1069 *
1070 * FIFO:
1071 * - disable for timestamp mode
1072 * - enable for FIFO mode
1073 */
Alexander Steincbffaf72018-10-11 17:01:25 +02001074 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP)
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +02001075 reg_mcr &= ~FLEXCAN_MCR_FEN;
Alexander Steincbffaf72018-10-11 17:01:25 +02001076 else
1077 reg_mcr |= FLEXCAN_MCR_FEN;
1078
Pankaj Bansal7ad0f532018-08-13 23:50:48 +05301079 /* MCR
1080 *
1081 * NOTE: In loopback mode, the CAN_MCR[SRXDIS] cannot be
1082 * asserted because this will impede the self reception
1083 * of a transmitted message. This is not documented in
1084 * earlier versions of flexcan block guide.
1085 *
1086 * Self Reception:
1087 * - enable Self Reception for loopback mode
1088 * (by clearing "Self Reception Disable" bit)
1089 * - disable for normal operation
1090 */
1091 if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)
1092 reg_mcr &= ~FLEXCAN_MCR_SRX_DIS;
1093 else
1094 reg_mcr |= FLEXCAN_MCR_SRX_DIS;
1095
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +01001096 netdev_dbg(dev, "%s: writing mcr=0x%08x", __func__, reg_mcr);
Pankaj Bansal88462d22017-11-24 18:52:08 +05301097 priv->write(reg_mcr, &regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001098
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +02001099 /* CTRL
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001100 *
1101 * disable timer sync feature
1102 *
1103 * disable auto busoff recovery
1104 * transmit lowest buffer first
1105 *
1106 * enable tx and rx warning interrupt
1107 * enable bus off interrupt
1108 * (== FLEXCAN_CTRL_ERR_STATE)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001109 */
Pankaj Bansal88462d22017-11-24 18:52:08 +05301110 reg_ctrl = priv->read(&regs->ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001111 reg_ctrl &= ~FLEXCAN_CTRL_TSYN;
1112 reg_ctrl |= FLEXCAN_CTRL_BOFF_REC | FLEXCAN_CTRL_LBUF |
Wolfgang Grandegger4f72e5f2012-09-28 03:17:15 +00001113 FLEXCAN_CTRL_ERR_STATE;
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +02001114
1115 /* enable the "error interrupt" (FLEXCAN_CTRL_ERR_MSK),
Wolfgang Grandegger4f72e5f2012-09-28 03:17:15 +00001116 * on most Flexcan cores, too. Otherwise we don't get
1117 * any error warning or passive interrupts.
1118 */
ZHU Yi (ST-FIR/ENG1-Zhu)2f8639b2017-09-15 07:01:23 +00001119 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_BROKEN_WERR_STATE ||
Wolfgang Grandegger4f72e5f2012-09-28 03:17:15 +00001120 priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING)
1121 reg_ctrl |= FLEXCAN_CTRL_ERR_MSK;
Alexander Steinbc03a542014-08-12 10:47:21 +02001122 else
1123 reg_ctrl &= ~FLEXCAN_CTRL_ERR_MSK;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001124
1125 /* save for later use */
1126 priv->reg_ctrl_default = reg_ctrl;
Marc Kleine-Budde6fa7da22015-08-27 14:24:48 +02001127 /* leave interrupts disabled for now */
1128 reg_ctrl &= ~FLEXCAN_CTRL_ERR_ALL;
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +01001129 netdev_dbg(dev, "%s: writing ctrl=0x%08x", __func__, reg_ctrl);
Pankaj Bansal88462d22017-11-24 18:52:08 +05301130 priv->write(reg_ctrl, &regs->ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001131
Marc Kleine-Budde9eb7aa82015-09-01 08:57:55 +02001132 if ((priv->devtype_data->quirks & FLEXCAN_QUIRK_ENABLE_EACEN_RRS)) {
Pankaj Bansal88462d22017-11-24 18:52:08 +05301133 reg_ctrl2 = priv->read(&regs->ctrl2);
Marc Kleine-Budde9eb7aa82015-09-01 08:57:55 +02001134 reg_ctrl2 |= FLEXCAN_CTRL2_EACEN | FLEXCAN_CTRL2_RRS;
Pankaj Bansal88462d22017-11-24 18:52:08 +05301135 priv->write(reg_ctrl2, &regs->ctrl2);
Marc Kleine-Budde9eb7aa82015-09-01 08:57:55 +02001136 }
1137
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +02001138 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP) {
Alexander Steincbffaf72018-10-11 17:01:25 +02001139 for (i = priv->offload.mb_first; i <= priv->offload.mb_last; i++) {
Pankaj Bansal05179612018-11-23 22:18:44 +01001140 mb = flexcan_get_mb(priv, i);
Pankaj Bansal88462d22017-11-24 18:52:08 +05301141 priv->write(FLEXCAN_MB_CODE_RX_EMPTY,
Pankaj Bansal05179612018-11-23 22:18:44 +01001142 &mb->can_ctrl);
Alexander Steincbffaf72018-10-11 17:01:25 +02001143 }
1144 } else {
1145 /* clear and invalidate unused mailboxes first */
Uwe Kleine-Königa55234d2019-01-11 12:20:41 +01001146 for (i = FLEXCAN_TX_MB_RESERVED_OFF_FIFO; i < priv->mb_count; i++) {
Pankaj Bansal05179612018-11-23 22:18:44 +01001147 mb = flexcan_get_mb(priv, i);
Alexander Steincbffaf72018-10-11 17:01:25 +02001148 priv->write(FLEXCAN_MB_CODE_RX_INACTIVE,
Pankaj Bansal05179612018-11-23 22:18:44 +01001149 &mb->can_ctrl);
Alexander Steincbffaf72018-10-11 17:01:25 +02001150 }
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +02001151 }
1152
David Jander25e92442014-09-03 16:47:22 +02001153 /* Errata ERR005829: mark first TX mailbox as INACTIVE */
Pankaj Bansal88462d22017-11-24 18:52:08 +05301154 priv->write(FLEXCAN_MB_CODE_TX_INACTIVE,
1155 &priv->tx_mb_reserved->can_ctrl);
David Jander25e92442014-09-03 16:47:22 +02001156
Marc Kleine-Buddec32fe4a2014-09-16 12:39:28 +02001157 /* mark TX mailbox as INACTIVE */
Pankaj Bansal88462d22017-11-24 18:52:08 +05301158 priv->write(FLEXCAN_MB_CODE_TX_INACTIVE,
Pankaj Bansal05179612018-11-23 22:18:44 +01001159 &priv->tx_mb->can_ctrl);
Marc Kleine-Budded5a7b402013-10-04 10:52:36 +02001160
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001161 /* acceptance mask/acceptance code (accept everything) */
Pankaj Bansal88462d22017-11-24 18:52:08 +05301162 priv->write(0x0, &regs->rxgmask);
1163 priv->write(0x0, &regs->rx14mask);
1164 priv->write(0x0, &regs->rx15mask);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001165
Marc Kleine-Buddef377bff2015-05-08 15:22:36 +02001166 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_DISABLE_RXFG)
Pankaj Bansal88462d22017-11-24 18:52:08 +05301167 priv->write(0x0, &regs->rxfgmask);
Hui Wang30c1e672012-06-28 16:21:35 +08001168
Marc Kleine-Budde4bd888a2015-08-31 21:03:29 +02001169 /* clear acceptance filters */
Pankaj Bansal05179612018-11-23 22:18:44 +01001170 for (i = 0; i < priv->mb_count; i++)
Pankaj Bansal88462d22017-11-24 18:52:08 +05301171 priv->write(0, &regs->rximr[i]);
Marc Kleine-Budde4bd888a2015-08-31 21:03:29 +02001172
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +02001173 /* On Vybrid, disable memory error detection interrupts
Stefan Agnercdce8442014-07-15 14:56:21 +02001174 * and freeze mode.
1175 * This also works around errata e5295 which generates
1176 * false positive memory errors and put the device in
1177 * freeze mode.
1178 */
Marc Kleine-Buddef377bff2015-05-08 15:22:36 +02001179 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_DISABLE_MECR) {
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +02001180 /* Follow the protocol as described in "Detection
Stefan Agnercdce8442014-07-15 14:56:21 +02001181 * and Correction of Memory Errors" to write to
1182 * MECR register
1183 */
Pankaj Bansal88462d22017-11-24 18:52:08 +05301184 reg_ctrl2 = priv->read(&regs->ctrl2);
Marc Kleine-Budde6f75fce2014-09-23 11:03:01 +02001185 reg_ctrl2 |= FLEXCAN_CTRL2_ECRWRE;
Pankaj Bansal88462d22017-11-24 18:52:08 +05301186 priv->write(reg_ctrl2, &regs->ctrl2);
Stefan Agnercdce8442014-07-15 14:56:21 +02001187
Pankaj Bansal88462d22017-11-24 18:52:08 +05301188 reg_mecr = priv->read(&regs->mecr);
Stefan Agnercdce8442014-07-15 14:56:21 +02001189 reg_mecr &= ~FLEXCAN_MECR_ECRWRDIS;
Pankaj Bansal88462d22017-11-24 18:52:08 +05301190 priv->write(reg_mecr, &regs->mecr);
Joakim Zhang5e269322019-08-15 08:00:26 +00001191 reg_mecr |= FLEXCAN_MECR_ECCDIS;
Stefan Agnercdce8442014-07-15 14:56:21 +02001192 reg_mecr &= ~(FLEXCAN_MECR_NCEFAFRZ | FLEXCAN_MECR_HANCEI_MSK |
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +02001193 FLEXCAN_MECR_FANCEI_MSK);
Pankaj Bansal88462d22017-11-24 18:52:08 +05301194 priv->write(reg_mecr, &regs->mecr);
Stefan Agnercdce8442014-07-15 14:56:21 +02001195 }
1196
Marc Kleine-Buddef0036982014-02-28 17:18:27 +01001197 err = flexcan_transceiver_enable(priv);
1198 if (err)
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +01001199 goto out_chip_disable;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001200
1201 /* synchronize with the can bus */
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +01001202 err = flexcan_chip_unfreeze(priv);
1203 if (err)
1204 goto out_transceiver_disable;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001205
1206 priv->can.state = CAN_STATE_ERROR_ACTIVE;
1207
Marc Kleine-Budde6fa7da22015-08-27 14:24:48 +02001208 /* enable interrupts atomically */
1209 disable_irq(dev->irq);
Pankaj Bansal88462d22017-11-24 18:52:08 +05301210 priv->write(priv->reg_ctrl_default, &regs->ctrl);
1211 priv->write(priv->reg_imask1_default, &regs->imask1);
1212 priv->write(priv->reg_imask2_default, &regs->imask2);
Marc Kleine-Budde6fa7da22015-08-27 14:24:48 +02001213 enable_irq(dev->irq);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001214
1215 /* print chip status */
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +01001216 netdev_dbg(dev, "%s: reading mcr=0x%08x ctrl=0x%08x\n", __func__,
Pankaj Bansal88462d22017-11-24 18:52:08 +05301217 priv->read(&regs->mcr), priv->read(&regs->ctrl));
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001218
1219 return 0;
1220
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +01001221 out_transceiver_disable:
1222 flexcan_transceiver_disable(priv);
1223 out_chip_disable:
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001224 flexcan_chip_disable(priv);
1225 return err;
1226}
1227
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +02001228/* flexcan_chip_stop
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001229 *
1230 * this functions is entered with clocks enabled
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001231 */
1232static void flexcan_chip_stop(struct net_device *dev)
1233{
1234 struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Budde89af8742015-05-08 09:32:58 +02001235 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001236
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +01001237 /* freeze + disable module */
1238 flexcan_chip_freeze(priv);
1239 flexcan_chip_disable(priv);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001240
Marc Kleine-Budde5be93bd2014-02-19 12:00:51 +01001241 /* Disable all interrupts */
Pankaj Bansal88462d22017-11-24 18:52:08 +05301242 priv->write(0, &regs->imask2);
1243 priv->write(0, &regs->imask1);
1244 priv->write(priv->reg_ctrl_default & ~FLEXCAN_CTRL_ERR_ALL,
1245 &regs->ctrl);
Marc Kleine-Budde5be93bd2014-02-19 12:00:51 +01001246
Marc Kleine-Buddef0036982014-02-28 17:18:27 +01001247 flexcan_transceiver_disable(priv);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001248 priv->can.state = CAN_STATE_STOPPED;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001249}
1250
1251static int flexcan_open(struct net_device *dev)
1252{
1253 struct flexcan_priv *priv = netdev_priv(dev);
1254 int err;
1255
Aisheng Dongca109892018-11-30 08:53:26 +00001256 err = pm_runtime_get_sync(priv->dev);
1257 if (err < 0)
Fabio Estevamaa101812013-07-22 12:41:40 -03001258 return err;
1259
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001260 err = open_candev(dev);
1261 if (err)
Aisheng Dongca109892018-11-30 08:53:26 +00001262 goto out_runtime_put;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001263
1264 err = request_irq(dev->irq, flexcan_irq, IRQF_SHARED, dev->name, dev);
1265 if (err)
1266 goto out_close;
1267
Pankaj Bansal05179612018-11-23 22:18:44 +01001268 priv->mb_size = sizeof(struct flexcan_mb) + CAN_MAX_DLEN;
Pankaj Bansal6cbf7602018-08-28 23:19:12 +05301269 priv->mb_count = (sizeof(priv->regs->mb[0]) / priv->mb_size) +
1270 (sizeof(priv->regs->mb[1]) / priv->mb_size);
Pankaj Bansal05179612018-11-23 22:18:44 +01001271
Pankaj Bansal5156c7b2018-08-13 23:50:49 +05301272 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP)
Pankaj Bansal05179612018-11-23 22:18:44 +01001273 priv->tx_mb_reserved =
1274 flexcan_get_mb(priv, FLEXCAN_TX_MB_RESERVED_OFF_TIMESTAMP);
Pankaj Bansal5156c7b2018-08-13 23:50:49 +05301275 else
Pankaj Bansal05179612018-11-23 22:18:44 +01001276 priv->tx_mb_reserved =
1277 flexcan_get_mb(priv, FLEXCAN_TX_MB_RESERVED_OFF_FIFO);
1278 priv->tx_mb_idx = priv->mb_count - 1;
1279 priv->tx_mb = flexcan_get_mb(priv, priv->tx_mb_idx);
Pankaj Bansal5156c7b2018-08-13 23:50:49 +05301280
1281 priv->reg_imask1_default = 0;
Pankaj Bansal05179612018-11-23 22:18:44 +01001282 priv->reg_imask2_default = FLEXCAN_IFLAG_MB(priv->tx_mb_idx);
Pankaj Bansal5156c7b2018-08-13 23:50:49 +05301283
1284 priv->offload.mailbox_read = flexcan_mailbox_read;
1285
1286 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP) {
1287 u64 imask;
1288
1289 priv->offload.mb_first = FLEXCAN_RX_MB_OFF_TIMESTAMP_FIRST;
Pankaj Bansal05179612018-11-23 22:18:44 +01001290 priv->offload.mb_last = priv->mb_count - 2;
Pankaj Bansal5156c7b2018-08-13 23:50:49 +05301291
1292 imask = GENMASK_ULL(priv->offload.mb_last,
1293 priv->offload.mb_first);
1294 priv->reg_imask1_default |= imask;
1295 priv->reg_imask2_default |= imask >> 32;
1296
1297 err = can_rx_offload_add_timestamp(dev, &priv->offload);
1298 } else {
1299 priv->reg_imask1_default |= FLEXCAN_IFLAG_RX_FIFO_OVERFLOW |
1300 FLEXCAN_IFLAG_RX_FIFO_AVAILABLE;
1301 err = can_rx_offload_add_fifo(dev, &priv->offload,
1302 FLEXCAN_NAPI_WEIGHT);
1303 }
1304 if (err)
1305 goto out_free_irq;
1306
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001307 /* start chip and queuing */
1308 err = flexcan_chip_start(dev);
1309 if (err)
Pankaj Bansal5156c7b2018-08-13 23:50:49 +05301310 goto out_offload_del;
Fabio Baltieriadccadb2012-12-18 18:50:58 +01001311
1312 can_led_event(dev, CAN_LED_EVENT_OPEN);
1313
Marc Kleine-Budde30164752015-05-10 15:26:58 +02001314 can_rx_offload_enable(&priv->offload);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001315 netif_start_queue(dev);
1316
1317 return 0;
1318
Pankaj Bansal5156c7b2018-08-13 23:50:49 +05301319 out_offload_del:
1320 can_rx_offload_del(&priv->offload);
Marc Kleine-Budde7e9e1482014-02-28 14:52:01 +01001321 out_free_irq:
1322 free_irq(dev->irq, dev);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001323 out_close:
1324 close_candev(dev);
Aisheng Dongca109892018-11-30 08:53:26 +00001325 out_runtime_put:
1326 pm_runtime_put(priv->dev);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001327
1328 return err;
1329}
1330
1331static int flexcan_close(struct net_device *dev)
1332{
1333 struct flexcan_priv *priv = netdev_priv(dev);
1334
1335 netif_stop_queue(dev);
Marc Kleine-Budde30164752015-05-10 15:26:58 +02001336 can_rx_offload_disable(&priv->offload);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001337 flexcan_chip_stop(dev);
1338
Pankaj Bansal5156c7b2018-08-13 23:50:49 +05301339 can_rx_offload_del(&priv->offload);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001340 free_irq(dev->irq, dev);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001341
1342 close_candev(dev);
Aisheng Dongca109892018-11-30 08:53:26 +00001343 pm_runtime_put(priv->dev);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001344
Fabio Baltieriadccadb2012-12-18 18:50:58 +01001345 can_led_event(dev, CAN_LED_EVENT_STOP);
1346
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001347 return 0;
1348}
1349
1350static int flexcan_set_mode(struct net_device *dev, enum can_mode mode)
1351{
1352 int err;
1353
1354 switch (mode) {
1355 case CAN_MODE_START:
1356 err = flexcan_chip_start(dev);
1357 if (err)
1358 return err;
1359
1360 netif_wake_queue(dev);
1361 break;
1362
1363 default:
1364 return -EOPNOTSUPP;
1365 }
1366
1367 return 0;
1368}
1369
1370static const struct net_device_ops flexcan_netdev_ops = {
1371 .ndo_open = flexcan_open,
1372 .ndo_stop = flexcan_close,
1373 .ndo_start_xmit = flexcan_start_xmit,
Oliver Hartkoppc971fa22014-03-07 09:23:41 +01001374 .ndo_change_mtu = can_change_mtu,
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001375};
1376
Bill Pemberton3c8ac0f2012-12-03 09:22:44 -05001377static int register_flexcandev(struct net_device *dev)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001378{
1379 struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Budde89af8742015-05-08 09:32:58 +02001380 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001381 u32 reg, err;
1382
Aisheng Dongca109892018-11-30 08:53:26 +00001383 err = flexcan_clks_enable(priv);
Fabio Estevamaa101812013-07-22 12:41:40 -03001384 if (err)
1385 return err;
1386
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001387 /* select "bus clock", chip must be disabled */
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001388 err = flexcan_chip_disable(priv);
1389 if (err)
Aisheng Dongca109892018-11-30 08:53:26 +00001390 goto out_clks_disable;
1391
Pankaj Bansal88462d22017-11-24 18:52:08 +05301392 reg = priv->read(&regs->ctrl);
Dong Aisheng8c306be2018-12-13 07:08:00 +00001393 if (priv->clk_src)
1394 reg |= FLEXCAN_CTRL_CLK_SRC;
1395 else
1396 reg &= ~FLEXCAN_CTRL_CLK_SRC;
Pankaj Bansal88462d22017-11-24 18:52:08 +05301397 priv->write(reg, &regs->ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001398
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001399 err = flexcan_chip_enable(priv);
1400 if (err)
1401 goto out_chip_disable;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001402
1403 /* set freeze, halt and activate FIFO, restrict register access */
Pankaj Bansal88462d22017-11-24 18:52:08 +05301404 reg = priv->read(&regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001405 reg |= FLEXCAN_MCR_FRZ | FLEXCAN_MCR_HALT |
1406 FLEXCAN_MCR_FEN | FLEXCAN_MCR_SUPV;
Pankaj Bansal88462d22017-11-24 18:52:08 +05301407 priv->write(reg, &regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001408
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +02001409 /* Currently we only support newer versions of this core
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +02001410 * featuring a RX hardware FIFO (although this driver doesn't
1411 * make use of it on some cores). Older cores, found on some
1412 * Coldfire derivates are not tested.
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001413 */
Pankaj Bansal88462d22017-11-24 18:52:08 +05301414 reg = priv->read(&regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001415 if (!(reg & FLEXCAN_MCR_FEN)) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +01001416 netdev_err(dev, "Could not enable RX FIFO, unsupported core\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001417 err = -ENODEV;
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001418 goto out_chip_disable;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001419 }
1420
1421 err = register_candev(dev);
Aisheng Dongca109892018-11-30 08:53:26 +00001422 if (err)
1423 goto out_chip_disable;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001424
Aisheng Dongca109892018-11-30 08:53:26 +00001425 /* Disable core and let pm_runtime_put() disable the clocks.
1426 * If CONFIG_PM is not enabled, the clocks will stay powered.
1427 */
1428 flexcan_chip_disable(priv);
1429 pm_runtime_put(priv->dev);
1430
1431 return 0;
1432
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001433 out_chip_disable:
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001434 flexcan_chip_disable(priv);
Aisheng Dongca109892018-11-30 08:53:26 +00001435 out_clks_disable:
1436 flexcan_clks_disable(priv);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001437 return err;
1438}
1439
Bill Pemberton3c8ac0f2012-12-03 09:22:44 -05001440static void unregister_flexcandev(struct net_device *dev)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001441{
1442 unregister_candev(dev);
1443}
1444
Aisheng Dongde3578c2018-11-23 08:35:33 +00001445static int flexcan_setup_stop_mode(struct platform_device *pdev)
1446{
1447 struct net_device *dev = platform_get_drvdata(pdev);
1448 struct device_node *np = pdev->dev.of_node;
1449 struct device_node *gpr_np;
1450 struct flexcan_priv *priv;
1451 phandle phandle;
1452 u32 out_val[5];
1453 int ret;
1454
1455 if (!np)
1456 return -EINVAL;
1457
1458 /* stop mode property format is:
1459 * <&gpr req_gpr req_bit ack_gpr ack_bit>.
1460 */
1461 ret = of_property_read_u32_array(np, "fsl,stop-mode", out_val,
1462 ARRAY_SIZE(out_val));
1463 if (ret) {
1464 dev_dbg(&pdev->dev, "no stop-mode property\n");
1465 return ret;
1466 }
1467 phandle = *out_val;
1468
1469 gpr_np = of_find_node_by_phandle(phandle);
1470 if (!gpr_np) {
1471 dev_dbg(&pdev->dev, "could not find gpr node by phandle\n");
YueHaibing7873e982018-12-12 17:24:01 +08001472 return -ENODEV;
Aisheng Dongde3578c2018-11-23 08:35:33 +00001473 }
1474
1475 priv = netdev_priv(dev);
1476 priv->stm.gpr = syscon_node_to_regmap(gpr_np);
Aisheng Dongde3578c2018-11-23 08:35:33 +00001477 if (IS_ERR(priv->stm.gpr)) {
1478 dev_dbg(&pdev->dev, "could not find gpr regmap\n");
Wen Yange9f2a852019-07-06 11:37:20 +08001479 ret = PTR_ERR(priv->stm.gpr);
1480 goto out_put_node;
Aisheng Dongde3578c2018-11-23 08:35:33 +00001481 }
1482
1483 priv->stm.req_gpr = out_val[1];
1484 priv->stm.req_bit = out_val[2];
1485 priv->stm.ack_gpr = out_val[3];
1486 priv->stm.ack_bit = out_val[4];
1487
1488 dev_dbg(&pdev->dev,
1489 "gpr %s req_gpr=0x02%x req_bit=%u ack_gpr=0x02%x ack_bit=%u\n",
1490 gpr_np->full_name, priv->stm.req_gpr, priv->stm.req_bit,
1491 priv->stm.ack_gpr, priv->stm.ack_bit);
1492
1493 device_set_wakeup_capable(&pdev->dev, true);
1494
Sean Nyekjaer915f9662019-04-09 10:39:48 +02001495 if (of_property_read_bool(np, "wakeup-source"))
1496 device_set_wakeup_enable(&pdev->dev, true);
1497
Aisheng Dongde3578c2018-11-23 08:35:33 +00001498 return 0;
David S. Miller13dfb3f2019-08-06 18:44:57 -07001499
Wen Yange9f2a852019-07-06 11:37:20 +08001500out_put_node:
1501 of_node_put(gpr_np);
1502 return ret;
Aisheng Dongde3578c2018-11-23 08:35:33 +00001503}
1504
Hui Wang30c1e672012-06-28 16:21:35 +08001505static const struct of_device_id flexcan_of_match[] = {
Hui Wang30c1e672012-06-28 16:21:35 +08001506 { .compatible = "fsl,imx6q-flexcan", .data = &fsl_imx6q_devtype_data, },
Marc Kleine-Buddee3587842013-10-03 23:51:55 +02001507 { .compatible = "fsl,imx28-flexcan", .data = &fsl_imx28_devtype_data, },
Uwe Kleine-König0e030a32018-04-25 16:50:39 +02001508 { .compatible = "fsl,imx53-flexcan", .data = &fsl_imx25_devtype_data, },
1509 { .compatible = "fsl,imx35-flexcan", .data = &fsl_imx25_devtype_data, },
1510 { .compatible = "fsl,imx25-flexcan", .data = &fsl_imx25_devtype_data, },
Marc Kleine-Buddee3587842013-10-03 23:51:55 +02001511 { .compatible = "fsl,p1010-flexcan", .data = &fsl_p1010_devtype_data, },
Stefan Agnercdce8442014-07-15 14:56:21 +02001512 { .compatible = "fsl,vf610-flexcan", .data = &fsl_vf610_devtype_data, },
Pankaj Bansal99b76682017-11-24 18:52:09 +05301513 { .compatible = "fsl,ls1021ar2-flexcan", .data = &fsl_ls1021a_r2_devtype_data, },
Hui Wang30c1e672012-06-28 16:21:35 +08001514 { /* sentinel */ },
1515};
Marc Kleine-Budde4358a9d2012-10-04 10:55:35 +02001516MODULE_DEVICE_TABLE(of, flexcan_of_match);
Hui Wang30c1e672012-06-28 16:21:35 +08001517
1518static const struct platform_device_id flexcan_id_table[] = {
1519 { .name = "flexcan", .driver_data = (kernel_ulong_t)&fsl_p1010_devtype_data, },
1520 { /* sentinel */ },
1521};
Marc Kleine-Budde4358a9d2012-10-04 10:55:35 +02001522MODULE_DEVICE_TABLE(platform, flexcan_id_table);
Hui Wang30c1e672012-06-28 16:21:35 +08001523
Bill Pemberton3c8ac0f2012-12-03 09:22:44 -05001524static int flexcan_probe(struct platform_device *pdev)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001525{
Hui Wang30c1e672012-06-28 16:21:35 +08001526 const struct of_device_id *of_id;
Marc Kleine-Buddedda0b3b2012-07-13 14:52:48 +02001527 const struct flexcan_devtype_data *devtype_data;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001528 struct net_device *dev;
1529 struct flexcan_priv *priv;
Andreas Werner555828e2015-03-22 17:35:52 +01001530 struct regulator *reg_xceiver;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001531 struct resource *mem;
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001532 struct clk *clk_ipg = NULL, *clk_per = NULL;
Marc Kleine-Budde89af8742015-05-08 09:32:58 +02001533 struct flexcan_regs __iomem *regs;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001534 int err, irq;
Dong Aisheng8c306be2018-12-13 07:08:00 +00001535 u8 clk_src = 1;
holt@sgi.com97efe9a2011-08-16 17:32:23 +00001536 u32 clock_freq = 0;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001537
Andreas Werner555828e2015-03-22 17:35:52 +01001538 reg_xceiver = devm_regulator_get(&pdev->dev, "xceiver");
1539 if (PTR_ERR(reg_xceiver) == -EPROBE_DEFER)
1540 return -EPROBE_DEFER;
1541 else if (IS_ERR(reg_xceiver))
1542 reg_xceiver = NULL;
1543
Dong Aisheng8c306be2018-12-13 07:08:00 +00001544 if (pdev->dev.of_node) {
Hui Wangafc016d2012-06-28 16:21:34 +08001545 of_property_read_u32(pdev->dev.of_node,
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +02001546 "clock-frequency", &clock_freq);
Dong Aisheng8c306be2018-12-13 07:08:00 +00001547 of_property_read_u8(pdev->dev.of_node,
1548 "fsl,clk-source", &clk_src);
1549 }
holt@sgi.com97efe9a2011-08-16 17:32:23 +00001550
1551 if (!clock_freq) {
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001552 clk_ipg = devm_clk_get(&pdev->dev, "ipg");
1553 if (IS_ERR(clk_ipg)) {
1554 dev_err(&pdev->dev, "no ipg clock defined\n");
Fabio Estevam933e4af2013-07-22 12:41:39 -03001555 return PTR_ERR(clk_ipg);
holt@sgi.com97efe9a2011-08-16 17:32:23 +00001556 }
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001557
1558 clk_per = devm_clk_get(&pdev->dev, "per");
1559 if (IS_ERR(clk_per)) {
1560 dev_err(&pdev->dev, "no per clock defined\n");
Fabio Estevam933e4af2013-07-22 12:41:39 -03001561 return PTR_ERR(clk_per);
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001562 }
Marc Kleine-Budde1a3e5172013-11-25 22:15:20 +01001563 clock_freq = clk_get_rate(clk_per);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001564 }
1565
1566 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1567 irq = platform_get_irq(pdev, 0);
Fabio Estevam933e4af2013-07-22 12:41:39 -03001568 if (irq <= 0)
1569 return -ENODEV;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001570
Marc Kleine-Budde89af8742015-05-08 09:32:58 +02001571 regs = devm_ioremap_resource(&pdev->dev, mem);
1572 if (IS_ERR(regs))
1573 return PTR_ERR(regs);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001574
Hui Wang30c1e672012-06-28 16:21:35 +08001575 of_id = of_match_device(flexcan_of_match, &pdev->dev);
1576 if (of_id) {
1577 devtype_data = of_id->data;
Marc Kleine-Budded0873e62014-03-04 22:04:22 +01001578 } else if (platform_get_device_id(pdev)->driver_data) {
Hui Wang30c1e672012-06-28 16:21:35 +08001579 devtype_data = (struct flexcan_devtype_data *)
Marc Kleine-Budded0873e62014-03-04 22:04:22 +01001580 platform_get_device_id(pdev)->driver_data;
Hui Wang30c1e672012-06-28 16:21:35 +08001581 } else {
Fabio Estevam933e4af2013-07-22 12:41:39 -03001582 return -ENODEV;
Hui Wang30c1e672012-06-28 16:21:35 +08001583 }
1584
Fabio Estevam933e4af2013-07-22 12:41:39 -03001585 dev = alloc_candev(sizeof(struct flexcan_priv), 1);
1586 if (!dev)
1587 return -ENOMEM;
1588
Marc Kleine-Budde30164752015-05-10 15:26:58 +02001589 platform_set_drvdata(pdev, dev);
1590 SET_NETDEV_DEV(dev, &pdev->dev);
1591
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001592 dev->netdev_ops = &flexcan_netdev_ops;
1593 dev->irq = irq;
Reuben Dowle9a123492011-11-01 11:18:03 +13001594 dev->flags |= IFF_ECHO;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001595
1596 priv = netdev_priv(dev);
Pankaj Bansal88462d22017-11-24 18:52:08 +05301597
Uwe Kleine-König0e030a32018-04-25 16:50:39 +02001598 if (of_property_read_bool(pdev->dev.of_node, "big-endian") ||
1599 devtype_data->quirks & FLEXCAN_QUIRK_DEFAULT_BIG_ENDIAN) {
Pankaj Bansal88462d22017-11-24 18:52:08 +05301600 priv->read = flexcan_read_be;
1601 priv->write = flexcan_write_be;
1602 } else {
Uwe Kleine-König0e030a32018-04-25 16:50:39 +02001603 priv->read = flexcan_read_le;
1604 priv->write = flexcan_write_le;
Pankaj Bansal88462d22017-11-24 18:52:08 +05301605 }
1606
Aisheng Dongca109892018-11-30 08:53:26 +00001607 priv->dev = &pdev->dev;
holt@sgi.com97efe9a2011-08-16 17:32:23 +00001608 priv->can.clock.freq = clock_freq;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001609 priv->can.bittiming_const = &flexcan_bittiming_const;
1610 priv->can.do_set_mode = flexcan_set_mode;
1611 priv->can.do_get_berr_counter = flexcan_get_berr_counter;
1612 priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK |
1613 CAN_CTRLMODE_LISTENONLY | CAN_CTRLMODE_3_SAMPLES |
1614 CAN_CTRLMODE_BERR_REPORTING;
Marc Kleine-Budde89af8742015-05-08 09:32:58 +02001615 priv->regs = regs;
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001616 priv->clk_ipg = clk_ipg;
1617 priv->clk_per = clk_per;
Dong Aisheng8c306be2018-12-13 07:08:00 +00001618 priv->clk_src = clk_src;
Hui Wang30c1e672012-06-28 16:21:35 +08001619 priv->devtype_data = devtype_data;
Andreas Werner555828e2015-03-22 17:35:52 +01001620 priv->reg_xceiver = reg_xceiver;
Fabio Estevamb7c41142013-06-10 23:12:57 -03001621
Aisheng Dongca109892018-11-30 08:53:26 +00001622 pm_runtime_get_noresume(&pdev->dev);
1623 pm_runtime_set_active(&pdev->dev);
1624 pm_runtime_enable(&pdev->dev);
1625
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001626 err = register_flexcandev(dev);
1627 if (err) {
1628 dev_err(&pdev->dev, "registering netdev failed\n");
1629 goto failed_register;
1630 }
1631
Fabio Baltieriadccadb2012-12-18 18:50:58 +01001632 devm_can_led_init(dev);
1633
Aisheng Dongde3578c2018-11-23 08:35:33 +00001634 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_SETUP_STOP_MODE) {
1635 err = flexcan_setup_stop_mode(pdev);
1636 if (err)
1637 dev_dbg(&pdev->dev, "failed to setup stop-mode\n");
1638 }
1639
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001640 return 0;
1641
1642 failed_register:
1643 free_candev(dev);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001644 return err;
1645}
1646
Bill Pemberton3c8ac0f2012-12-03 09:22:44 -05001647static int flexcan_remove(struct platform_device *pdev)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001648{
1649 struct net_device *dev = platform_get_drvdata(pdev);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001650
1651 unregister_flexcandev(dev);
Aisheng Dongca109892018-11-30 08:53:26 +00001652 pm_runtime_disable(&pdev->dev);
Marc Kleine-Budde9a275862010-10-21 05:07:58 +00001653 free_candev(dev);
1654
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001655 return 0;
1656}
1657
Marc Kleine-Budde08c6d352014-03-05 19:10:44 +01001658static int __maybe_unused flexcan_suspend(struct device *device)
Eric Bénard8b5e2182012-05-08 17:12:17 +02001659{
Fabio Estevam588e7a82013-05-20 15:43:43 -03001660 struct net_device *dev = dev_get_drvdata(device);
Eric Bénard8b5e2182012-05-08 17:12:17 +02001661 struct flexcan_priv *priv = netdev_priv(dev);
Aisheng Dongca109892018-11-30 08:53:26 +00001662 int err = 0;
Eric Bénard8b5e2182012-05-08 17:12:17 +02001663
Eric Bénard8b5e2182012-05-08 17:12:17 +02001664 if (netif_running(dev)) {
Aisheng Dongde3578c2018-11-23 08:35:33 +00001665 /* if wakeup is enabled, enter stop mode
1666 * else enter disabled mode.
1667 */
1668 if (device_may_wakeup(device)) {
1669 enable_irq_wake(dev->irq);
Joakim Zhang5f186c22019-07-02 01:45:41 +00001670 err = flexcan_enter_stop_mode(priv);
1671 if (err)
1672 return err;
Aisheng Dongde3578c2018-11-23 08:35:33 +00001673 } else {
1674 err = flexcan_chip_disable(priv);
1675 if (err)
1676 return err;
Aisheng Dongca109892018-11-30 08:53:26 +00001677
1678 err = pm_runtime_force_suspend(device);
Aisheng Dongde3578c2018-11-23 08:35:33 +00001679 }
Eric Bénard8b5e2182012-05-08 17:12:17 +02001680 netif_stop_queue(dev);
1681 netif_device_detach(dev);
1682 }
1683 priv->can.state = CAN_STATE_SLEEPING;
1684
Aisheng Dongca109892018-11-30 08:53:26 +00001685 return err;
Eric Bénard8b5e2182012-05-08 17:12:17 +02001686}
1687
Marc Kleine-Budde08c6d352014-03-05 19:10:44 +01001688static int __maybe_unused flexcan_resume(struct device *device)
Eric Bénard8b5e2182012-05-08 17:12:17 +02001689{
Fabio Estevam588e7a82013-05-20 15:43:43 -03001690 struct net_device *dev = dev_get_drvdata(device);
Eric Bénard8b5e2182012-05-08 17:12:17 +02001691 struct flexcan_priv *priv = netdev_priv(dev);
Aisheng Dongca109892018-11-30 08:53:26 +00001692 int err = 0;
Eric Bénard8b5e2182012-05-08 17:12:17 +02001693
1694 priv->can.state = CAN_STATE_ERROR_ACTIVE;
1695 if (netif_running(dev)) {
1696 netif_device_attach(dev);
1697 netif_start_queue(dev);
Aisheng Dongde3578c2018-11-23 08:35:33 +00001698 if (device_may_wakeup(device)) {
1699 disable_irq_wake(dev->irq);
1700 } else {
Aisheng Dongca109892018-11-30 08:53:26 +00001701 err = pm_runtime_force_resume(device);
Aisheng Dongde3578c2018-11-23 08:35:33 +00001702 if (err)
1703 return err;
Aisheng Dongca109892018-11-30 08:53:26 +00001704
1705 err = flexcan_chip_enable(priv);
Aisheng Dongde3578c2018-11-23 08:35:33 +00001706 }
Eric Bénard8b5e2182012-05-08 17:12:17 +02001707 }
Aisheng Dongca109892018-11-30 08:53:26 +00001708
1709 return err;
1710}
1711
1712static int __maybe_unused flexcan_runtime_suspend(struct device *device)
1713{
1714 struct net_device *dev = dev_get_drvdata(device);
1715 struct flexcan_priv *priv = netdev_priv(dev);
1716
1717 flexcan_clks_disable(priv);
1718
Fabio Estevam4de349e2016-08-17 12:41:08 -03001719 return 0;
Eric Bénard8b5e2182012-05-08 17:12:17 +02001720}
Fabio Estevam588e7a82013-05-20 15:43:43 -03001721
Aisheng Dongca109892018-11-30 08:53:26 +00001722static int __maybe_unused flexcan_runtime_resume(struct device *device)
1723{
1724 struct net_device *dev = dev_get_drvdata(device);
1725 struct flexcan_priv *priv = netdev_priv(dev);
1726
1727 return flexcan_clks_enable(priv);
1728}
1729
Aisheng Dongde3578c2018-11-23 08:35:33 +00001730static int __maybe_unused flexcan_noirq_suspend(struct device *device)
1731{
1732 struct net_device *dev = dev_get_drvdata(device);
1733 struct flexcan_priv *priv = netdev_priv(dev);
1734
1735 if (netif_running(dev) && device_may_wakeup(device))
1736 flexcan_enable_wakeup_irq(priv, true);
1737
1738 return 0;
1739}
1740
1741static int __maybe_unused flexcan_noirq_resume(struct device *device)
1742{
1743 struct net_device *dev = dev_get_drvdata(device);
1744 struct flexcan_priv *priv = netdev_priv(dev);
Joakim Zhang5f186c22019-07-02 01:45:41 +00001745 int err;
Aisheng Dongde3578c2018-11-23 08:35:33 +00001746
1747 if (netif_running(dev) && device_may_wakeup(device)) {
1748 flexcan_enable_wakeup_irq(priv, false);
Joakim Zhang5f186c22019-07-02 01:45:41 +00001749 err = flexcan_exit_stop_mode(priv);
1750 if (err)
1751 return err;
Aisheng Dongde3578c2018-11-23 08:35:33 +00001752 }
1753
1754 return 0;
1755}
1756
1757static const struct dev_pm_ops flexcan_pm_ops = {
1758 SET_SYSTEM_SLEEP_PM_OPS(flexcan_suspend, flexcan_resume)
Aisheng Dongca109892018-11-30 08:53:26 +00001759 SET_RUNTIME_PM_OPS(flexcan_runtime_suspend, flexcan_runtime_resume, NULL)
Aisheng Dongde3578c2018-11-23 08:35:33 +00001760 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(flexcan_noirq_suspend, flexcan_noirq_resume)
1761};
Eric Bénard8b5e2182012-05-08 17:12:17 +02001762
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001763static struct platform_driver flexcan_driver = {
holt@sgi.comc8aef4c2011-08-16 17:32:22 +00001764 .driver = {
1765 .name = DRV_NAME,
Fabio Estevam588e7a82013-05-20 15:43:43 -03001766 .pm = &flexcan_pm_ops,
holt@sgi.comc8aef4c2011-08-16 17:32:22 +00001767 .of_match_table = flexcan_of_match,
1768 },
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001769 .probe = flexcan_probe,
Bill Pemberton3c8ac0f2012-12-03 09:22:44 -05001770 .remove = flexcan_remove,
Hui Wang30c1e672012-06-28 16:21:35 +08001771 .id_table = flexcan_id_table,
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001772};
1773
Axel Lin871d3372011-11-27 15:42:31 +00001774module_platform_driver(flexcan_driver);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001775
1776MODULE_AUTHOR("Sascha Hauer <kernel@pengutronix.de>, "
1777 "Marc Kleine-Budde <kernel@pengutronix.de>");
1778MODULE_LICENSE("GPL v2");
1779MODULE_DESCRIPTION("CAN port driver for flexcan based chip");