blob: 12b60ad95b02ae24b8109c47ba5a34577ea66525 [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
Joakim Zhang812f0112020-11-06 18:56:27 +080012#include <dt-bindings/firmware/imx/rsrc.h>
Joakim Zhangeadf6ca2019-07-12 08:02:44 +000013#include <linux/bitfield.h>
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +020014#include <linux/can.h>
15#include <linux/can/dev.h>
16#include <linux/can/error.h>
Fabio Baltieriadccadb2012-12-18 18:50:58 +010017#include <linux/can/led.h>
Marc Kleine-Budde30164752015-05-10 15:26:58 +020018#include <linux/can/rx-offload.h>
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +020019#include <linux/clk.h>
20#include <linux/delay.h>
Joakim Zhang812f0112020-11-06 18:56:27 +080021#include <linux/firmware/imx/sci.h>
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +020022#include <linux/interrupt.h>
23#include <linux/io.h>
Aisheng Dongde3578c2018-11-23 08:35:33 +000024#include <linux/mfd/syscon.h>
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +020025#include <linux/module.h>
Marc Kleine-Budde555f6e52020-09-22 16:44:10 +020026#include <linux/netdevice.h>
holt@sgi.com97efe9a2011-08-16 17:32:23 +000027#include <linux/of.h>
Hui Wang30c1e672012-06-28 16:21:35 +080028#include <linux/of_device.h>
Joakim Zhang1434d042019-12-04 11:36:19 +000029#include <linux/pinctrl/consumer.h>
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +020030#include <linux/platform_device.h>
Angelo Dureghellod9cead72021-07-02 11:48:41 +020031#include <linux/can/platform/flexcan.h>
Aisheng Dongca109892018-11-30 08:53:26 +000032#include <linux/pm_runtime.h>
Aisheng Dongde3578c2018-11-23 08:35:33 +000033#include <linux/regmap.h>
Marc Kleine-Budde555f6e52020-09-22 16:44:10 +020034#include <linux/regulator/consumer.h>
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +020035
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +020036#define DRV_NAME "flexcan"
37
38/* 8 for RX fifo and 2 error handling */
39#define FLEXCAN_NAPI_WEIGHT (8 + 2)
40
41/* FLEXCAN module configuration register (CANMCR) bits */
42#define FLEXCAN_MCR_MDIS BIT(31)
43#define FLEXCAN_MCR_FRZ BIT(30)
44#define FLEXCAN_MCR_FEN BIT(29)
45#define FLEXCAN_MCR_HALT BIT(28)
46#define FLEXCAN_MCR_NOT_RDY BIT(27)
47#define FLEXCAN_MCR_WAK_MSK BIT(26)
48#define FLEXCAN_MCR_SOFTRST BIT(25)
49#define FLEXCAN_MCR_FRZ_ACK BIT(24)
50#define FLEXCAN_MCR_SUPV BIT(23)
51#define FLEXCAN_MCR_SLF_WAK BIT(22)
52#define FLEXCAN_MCR_WRN_EN BIT(21)
53#define FLEXCAN_MCR_LPM_ACK BIT(20)
54#define FLEXCAN_MCR_WAK_SRC BIT(19)
55#define FLEXCAN_MCR_DOZE BIT(18)
56#define FLEXCAN_MCR_SRX_DIS BIT(17)
Marc Kleine-Budde62d10862015-08-27 16:01:27 +020057#define FLEXCAN_MCR_IRMQ BIT(16)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +020058#define FLEXCAN_MCR_LPRIO_EN BIT(13)
59#define FLEXCAN_MCR_AEN BIT(12)
Joakim Zhangeadf6ca2019-07-12 08:02:44 +000060#define FLEXCAN_MCR_FDEN BIT(11)
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +020061/* MCR_MAXMB: maximum used MBs is MAXMB + 1 */
Marc Kleine-Budde4c728d82014-09-02 16:54:17 +020062#define FLEXCAN_MCR_MAXMB(x) ((x) & 0x7f)
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +020063#define FLEXCAN_MCR_IDAM_A (0x0 << 8)
64#define FLEXCAN_MCR_IDAM_B (0x1 << 8)
65#define FLEXCAN_MCR_IDAM_C (0x2 << 8)
66#define FLEXCAN_MCR_IDAM_D (0x3 << 8)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +020067
68/* FLEXCAN control register (CANCTRL) bits */
69#define FLEXCAN_CTRL_PRESDIV(x) (((x) & 0xff) << 24)
70#define FLEXCAN_CTRL_RJW(x) (((x) & 0x03) << 22)
71#define FLEXCAN_CTRL_PSEG1(x) (((x) & 0x07) << 19)
72#define FLEXCAN_CTRL_PSEG2(x) (((x) & 0x07) << 16)
73#define FLEXCAN_CTRL_BOFF_MSK BIT(15)
74#define FLEXCAN_CTRL_ERR_MSK BIT(14)
75#define FLEXCAN_CTRL_CLK_SRC BIT(13)
76#define FLEXCAN_CTRL_LPB BIT(12)
77#define FLEXCAN_CTRL_TWRN_MSK BIT(11)
78#define FLEXCAN_CTRL_RWRN_MSK BIT(10)
79#define FLEXCAN_CTRL_SMP BIT(7)
80#define FLEXCAN_CTRL_BOFF_REC BIT(6)
81#define FLEXCAN_CTRL_TSYN BIT(5)
82#define FLEXCAN_CTRL_LBUF BIT(4)
83#define FLEXCAN_CTRL_LOM BIT(3)
84#define FLEXCAN_CTRL_PROPSEG(x) ((x) & 0x07)
85#define FLEXCAN_CTRL_ERR_BUS (FLEXCAN_CTRL_ERR_MSK)
86#define FLEXCAN_CTRL_ERR_STATE \
87 (FLEXCAN_CTRL_TWRN_MSK | FLEXCAN_CTRL_RWRN_MSK | \
88 FLEXCAN_CTRL_BOFF_MSK)
89#define FLEXCAN_CTRL_ERR_ALL \
90 (FLEXCAN_CTRL_ERR_BUS | FLEXCAN_CTRL_ERR_STATE)
91
Stefan Agnercdce8442014-07-15 14:56:21 +020092/* FLEXCAN control register 2 (CTRL2) bits */
Marc Kleine-Budde6f75fce2014-09-23 11:03:01 +020093#define FLEXCAN_CTRL2_ECRWRE BIT(29)
94#define FLEXCAN_CTRL2_WRMFRZ BIT(28)
95#define FLEXCAN_CTRL2_RFFN(x) (((x) & 0x0f) << 24)
96#define FLEXCAN_CTRL2_TASD(x) (((x) & 0x1f) << 19)
97#define FLEXCAN_CTRL2_MRP BIT(18)
98#define FLEXCAN_CTRL2_RRS BIT(17)
99#define FLEXCAN_CTRL2_EACEN BIT(16)
Joakim Zhangce885a12019-07-12 08:02:51 +0000100#define FLEXCAN_CTRL2_ISOCANFDEN BIT(12)
Stefan Agnercdce8442014-07-15 14:56:21 +0200101
102/* FLEXCAN memory error control register (MECR) bits */
103#define FLEXCAN_MECR_ECRWRDIS BIT(31)
104#define FLEXCAN_MECR_HANCEI_MSK BIT(19)
105#define FLEXCAN_MECR_FANCEI_MSK BIT(18)
106#define FLEXCAN_MECR_CEI_MSK BIT(16)
107#define FLEXCAN_MECR_HAERRIE BIT(15)
108#define FLEXCAN_MECR_FAERRIE BIT(14)
109#define FLEXCAN_MECR_EXTERRIE BIT(13)
110#define FLEXCAN_MECR_RERRDIS BIT(9)
111#define FLEXCAN_MECR_ECCDIS BIT(8)
112#define FLEXCAN_MECR_NCEFAFRZ BIT(7)
113
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200114/* FLEXCAN error and status register (ESR) bits */
115#define FLEXCAN_ESR_TWRN_INT BIT(17)
116#define FLEXCAN_ESR_RWRN_INT BIT(16)
117#define FLEXCAN_ESR_BIT1_ERR BIT(15)
118#define FLEXCAN_ESR_BIT0_ERR BIT(14)
119#define FLEXCAN_ESR_ACK_ERR BIT(13)
120#define FLEXCAN_ESR_CRC_ERR BIT(12)
121#define FLEXCAN_ESR_FRM_ERR BIT(11)
122#define FLEXCAN_ESR_STF_ERR BIT(10)
123#define FLEXCAN_ESR_TX_WRN BIT(9)
124#define FLEXCAN_ESR_RX_WRN BIT(8)
125#define FLEXCAN_ESR_IDLE BIT(7)
126#define FLEXCAN_ESR_TXRX BIT(6)
127#define FLEXCAN_EST_FLT_CONF_SHIFT (4)
128#define FLEXCAN_ESR_FLT_CONF_MASK (0x3 << FLEXCAN_EST_FLT_CONF_SHIFT)
129#define FLEXCAN_ESR_FLT_CONF_ACTIVE (0x0 << FLEXCAN_EST_FLT_CONF_SHIFT)
130#define FLEXCAN_ESR_FLT_CONF_PASSIVE (0x1 << FLEXCAN_EST_FLT_CONF_SHIFT)
131#define FLEXCAN_ESR_BOFF_INT BIT(2)
132#define FLEXCAN_ESR_ERR_INT BIT(1)
133#define FLEXCAN_ESR_WAK_INT BIT(0)
134#define FLEXCAN_ESR_ERR_BUS \
135 (FLEXCAN_ESR_BIT1_ERR | FLEXCAN_ESR_BIT0_ERR | \
136 FLEXCAN_ESR_ACK_ERR | FLEXCAN_ESR_CRC_ERR | \
137 FLEXCAN_ESR_FRM_ERR | FLEXCAN_ESR_STF_ERR)
138#define FLEXCAN_ESR_ERR_STATE \
139 (FLEXCAN_ESR_TWRN_INT | FLEXCAN_ESR_RWRN_INT | FLEXCAN_ESR_BOFF_INT)
140#define FLEXCAN_ESR_ERR_ALL \
141 (FLEXCAN_ESR_ERR_BUS | FLEXCAN_ESR_ERR_STATE)
Wolfgang Grandegger6e9d5542011-12-12 16:09:28 +0100142#define FLEXCAN_ESR_ALL_INT \
143 (FLEXCAN_ESR_TWRN_INT | FLEXCAN_ESR_RWRN_INT | \
Joakim Zhangab605232019-12-04 11:36:08 +0000144 FLEXCAN_ESR_BOFF_INT | FLEXCAN_ESR_ERR_INT)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200145
Joakim Zhangeadf6ca2019-07-12 08:02:44 +0000146/* FLEXCAN Bit Timing register (CBT) bits */
147#define FLEXCAN_CBT_BTF BIT(31)
148#define FLEXCAN_CBT_EPRESDIV_MASK GENMASK(30, 21)
149#define FLEXCAN_CBT_ERJW_MASK GENMASK(20, 16)
150#define FLEXCAN_CBT_EPROPSEG_MASK GENMASK(15, 10)
151#define FLEXCAN_CBT_EPSEG1_MASK GENMASK(9, 5)
152#define FLEXCAN_CBT_EPSEG2_MASK GENMASK(4, 0)
153
154/* FLEXCAN FD control register (FDCTRL) bits */
155#define FLEXCAN_FDCTRL_FDRATE BIT(31)
156#define FLEXCAN_FDCTRL_MBDSR1 GENMASK(20, 19)
157#define FLEXCAN_FDCTRL_MBDSR0 GENMASK(17, 16)
158#define FLEXCAN_FDCTRL_MBDSR_8 0x0
159#define FLEXCAN_FDCTRL_MBDSR_12 0x1
160#define FLEXCAN_FDCTRL_MBDSR_32 0x2
161#define FLEXCAN_FDCTRL_MBDSR_64 0x3
Joakim Zhangef5f6312019-07-12 08:02:56 +0000162#define FLEXCAN_FDCTRL_TDCEN BIT(15)
163#define FLEXCAN_FDCTRL_TDCFAIL BIT(14)
164#define FLEXCAN_FDCTRL_TDCOFF GENMASK(12, 8)
165#define FLEXCAN_FDCTRL_TDCVAL GENMASK(5, 0)
Joakim Zhangeadf6ca2019-07-12 08:02:44 +0000166
167/* FLEXCAN FD Bit Timing register (FDCBT) bits */
168#define FLEXCAN_FDCBT_FPRESDIV_MASK GENMASK(29, 20)
169#define FLEXCAN_FDCBT_FRJW_MASK GENMASK(18, 16)
170#define FLEXCAN_FDCBT_FPROPSEG_MASK GENMASK(14, 10)
171#define FLEXCAN_FDCBT_FPSEG1_MASK GENMASK(7, 5)
172#define FLEXCAN_FDCBT_FPSEG2_MASK GENMASK(2, 0)
173
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200174/* FLEXCAN interrupt flag register (IFLAG) bits */
David Jander25e92442014-09-03 16:47:22 +0200175/* Errata ERR005829 step7: Reserve first valid MB */
Alexander Steincbffaf72018-10-11 17:01:25 +0200176#define FLEXCAN_TX_MB_RESERVED_OFF_FIFO 8
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200177#define FLEXCAN_TX_MB_RESERVED_OFF_TIMESTAMP 0
Alexander Steincbffaf72018-10-11 17:01:25 +0200178#define FLEXCAN_RX_MB_OFF_TIMESTAMP_FIRST (FLEXCAN_TX_MB_RESERVED_OFF_TIMESTAMP + 1)
Marc Kleine-Budde8ce51392019-03-01 12:17:30 +0100179#define FLEXCAN_IFLAG_MB(x) BIT_ULL(x)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200180#define FLEXCAN_IFLAG_RX_FIFO_OVERFLOW BIT(7)
181#define FLEXCAN_IFLAG_RX_FIFO_WARN BIT(6)
182#define FLEXCAN_IFLAG_RX_FIFO_AVAILABLE BIT(5)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200183
184/* FLEXCAN message buffers */
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200185#define FLEXCAN_MB_CODE_MASK (0xf << 24)
186#define FLEXCAN_MB_CODE_RX_BUSY_BIT (0x1 << 24)
Marc Kleine-Buddec32fe4a2014-09-16 12:39:28 +0200187#define FLEXCAN_MB_CODE_RX_INACTIVE (0x0 << 24)
188#define FLEXCAN_MB_CODE_RX_EMPTY (0x4 << 24)
189#define FLEXCAN_MB_CODE_RX_FULL (0x2 << 24)
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200190#define FLEXCAN_MB_CODE_RX_OVERRUN (0x6 << 24)
Marc Kleine-Buddec32fe4a2014-09-16 12:39:28 +0200191#define FLEXCAN_MB_CODE_RX_RANSWER (0xa << 24)
192
193#define FLEXCAN_MB_CODE_TX_INACTIVE (0x8 << 24)
194#define FLEXCAN_MB_CODE_TX_ABORT (0x9 << 24)
195#define FLEXCAN_MB_CODE_TX_DATA (0xc << 24)
196#define FLEXCAN_MB_CODE_TX_TANSWER (0xe << 24)
197
Joakim Zhangeadf6ca2019-07-12 08:02:44 +0000198#define FLEXCAN_MB_CNT_EDL BIT(31)
199#define FLEXCAN_MB_CNT_BRS BIT(30)
200#define FLEXCAN_MB_CNT_ESI BIT(29)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200201#define FLEXCAN_MB_CNT_SRR BIT(22)
202#define FLEXCAN_MB_CNT_IDE BIT(21)
203#define FLEXCAN_MB_CNT_RTR BIT(20)
204#define FLEXCAN_MB_CNT_LENGTH(x) (((x) & 0xf) << 16)
205#define FLEXCAN_MB_CNT_TIMESTAMP(x) ((x) & 0xffff)
206
Joakim Zhang247e5352019-01-31 09:37:22 +0000207#define FLEXCAN_TIMEOUT_US (250)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200208
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200209/* FLEXCAN hardware feature flags
Wolfgang Grandeggerbb698ca2012-10-10 21:10:42 +0200210 *
211 * Below is some version info we got:
Angelo Dureghellod9cead72021-07-02 11:48:41 +0200212 * SOC Version IP-Version Glitch- [TR]WRN_INT IRQ Err Memory err RTR rece- FD Mode MB
Joakim Zhang2a1993e2019-07-12 08:02:59 +0000213 * Filter? connected? Passive detection ption in MB Supported?
Angelo Dureghellod9cead72021-07-02 11:48:41 +0200214 * MCF5441X FlexCAN2 ? no yes no no yes no 16
215 * MX25 FlexCAN2 03.00.00.00 no no no no no no 64
216 * MX28 FlexCAN2 03.00.04.00 yes yes no no no no 64
217 * MX35 FlexCAN2 03.00.00.00 no no no no no no 64
218 * MX53 FlexCAN2 03.00.00.00 yes no no no no no 64
219 * MX6s FlexCAN3 10.00.12.00 yes yes no no yes no 64
220 * MX8QM FlexCAN3 03.00.23.00 yes yes no no yes yes 64
221 * MX8MP FlexCAN3 03.00.17.01 yes yes no yes yes yes 64
222 * VF610 FlexCAN3 ? no yes no yes yes? no 64
223 * LS1021A FlexCAN2 03.00.04.00 no yes no no yes no 64
224 * LX2160A FlexCAN3 03.00.23.00 no yes no yes yes yes 64
Wolfgang Grandeggerbb698ca2012-10-10 21:10:42 +0200225 *
226 * Some SOCs do not have the RX_WARN & TX_WARN interrupt line connected.
227 */
Marc Kleine-Buddeef4b6232020-09-22 16:44:14 +0200228
229/* [TR]WRN_INT not connected */
230#define FLEXCAN_QUIRK_BROKEN_WERR_STATE BIT(1)
231 /* Disable RX FIFO Global mask */
232#define FLEXCAN_QUIRK_DISABLE_RXFG BIT(2)
233/* Enable EACEN and RRS bit in ctrl2 */
234#define FLEXCAN_QUIRK_ENABLE_EACEN_RRS BIT(3)
Joakim Zhang15ef2072020-04-16 17:31:25 +0800235/* Disable non-correctable errors interrupt and freeze mode */
Marc Kleine-Buddeef4b6232020-09-22 16:44:14 +0200236#define FLEXCAN_QUIRK_DISABLE_MECR BIT(4)
237/* Use timestamp based offloading */
238#define FLEXCAN_QUIRK_USE_OFF_TIMESTAMP BIT(5)
239/* No interrupt for error passive */
240#define FLEXCAN_QUIRK_BROKEN_PERR_STATE BIT(6)
241/* default to BE register access */
242#define FLEXCAN_QUIRK_DEFAULT_BIG_ENDIAN BIT(7)
Joakim Zhang14579982020-11-06 18:56:25 +0800243/* Setup stop mode with GPR to support wakeup */
244#define FLEXCAN_QUIRK_SETUP_STOP_MODE_GPR BIT(8)
Joakim Zhangeadf6ca2019-07-12 08:02:44 +0000245/* Support CAN-FD mode */
246#define FLEXCAN_QUIRK_SUPPORT_FD BIT(9)
Joakim Zhanga6597122020-09-30 05:15:55 +0800247/* support memory detection and correction */
248#define FLEXCAN_QUIRK_SUPPORT_ECC BIT(10)
Joakim Zhang812f0112020-11-06 18:56:27 +0800249/* Setup stop mode with SCU firmware to support wakeup */
250#define FLEXCAN_QUIRK_SETUP_STOP_MODE_SCFW BIT(11)
Angelo Dureghellod9cead72021-07-02 11:48:41 +0200251/* Setup 3 separate interrupts, main, boff and err */
252#define FLEXCAN_QUIRK_NR_IRQ_3 BIT(12)
253/* Setup 16 mailboxes */
254#define FLEXCAN_QUIRK_NR_MB_16 BIT(13)
Wolfgang Grandegger4f72e5f2012-09-28 03:17:15 +0000255
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200256/* Structure of the message buffer */
257struct flexcan_mb {
258 u32 can_ctrl;
259 u32 can_id;
Pankaj Bansal05179612018-11-23 22:18:44 +0100260 u32 data[];
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200261};
262
263/* Structure of the hardware registers */
264struct flexcan_regs {
265 u32 mcr; /* 0x00 */
Marc Kleine-Buddefe63a062020-09-22 16:44:13 +0200266 u32 ctrl; /* 0x04 - Not affected by Soft Reset */
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200267 u32 timer; /* 0x08 */
Marc Kleine-Budde4b702872020-09-22 16:44:12 +0200268 u32 tcr; /* 0x0c */
Marc Kleine-Buddefe63a062020-09-22 16:44:13 +0200269 u32 rxgmask; /* 0x10 - Not affected by Soft Reset */
270 u32 rx14mask; /* 0x14 - Not affected by Soft Reset */
271 u32 rx15mask; /* 0x18 - Not affected by Soft Reset */
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200272 u32 ecr; /* 0x1c */
273 u32 esr; /* 0x20 */
274 u32 imask2; /* 0x24 */
275 u32 imask1; /* 0x28 */
276 u32 iflag2; /* 0x2c */
277 u32 iflag1; /* 0x30 */
Marc Kleine-Budde62d10862015-08-27 16:01:27 +0200278 union { /* 0x34 */
279 u32 gfwr_mx28; /* MX28, MX53 */
Marc Kleine-Buddefe63a062020-09-22 16:44:13 +0200280 u32 ctrl2; /* MX6, VF610 - Not affected by Soft Reset */
Marc Kleine-Budde62d10862015-08-27 16:01:27 +0200281 };
Hui Wang30c1e672012-06-28 16:21:35 +0800282 u32 esr2; /* 0x38 */
283 u32 imeur; /* 0x3c */
284 u32 lrfr; /* 0x40 */
285 u32 crcr; /* 0x44 */
286 u32 rxfgmask; /* 0x48 */
Marc Kleine-Buddefe63a062020-09-22 16:44:13 +0200287 u32 rxfir; /* 0x4c - Not affected by Soft Reset */
288 u32 cbt; /* 0x50 - Not affected by Soft Reset */
Marc Kleine-Budde4b702872020-09-22 16:44:12 +0200289 u32 _reserved2; /* 0x54 */
290 u32 dbg1; /* 0x58 */
291 u32 dbg2; /* 0x5c */
292 u32 _reserved3[8]; /* 0x60 */
Kees Cookc92a08c2021-07-31 17:50:58 -0700293 struct_group(init,
294 u8 mb[2][512]; /* 0x80 - Not affected by Soft Reset */
295 /* FIFO-mode:
296 * MB
297 * 0x080...0x08f 0 RX message buffer
298 * 0x090...0x0df 1-5 reserved
299 * 0x0e0...0x0ff 6-7 8 entry ID table
300 * (mx25, mx28, mx35, mx53)
301 * 0x0e0...0x2df 6-7..37 8..128 entry ID table
302 * size conf'ed via ctrl2::RFFN
303 * (mx6, vf610)
304 */
305 u32 _reserved4[256]; /* 0x480 */
306 u32 rximr[64]; /* 0x880 - Not affected by Soft Reset */
307 u32 _reserved5[24]; /* 0x980 */
308 u32 gfwr_mx6; /* 0x9e0 - MX6 */
309 u32 _reserved6[39]; /* 0x9e4 */
310 u32 _rxfir[6]; /* 0xa80 */
311 u32 _reserved8[2]; /* 0xa98 */
312 u32 _rxmgmask; /* 0xaa0 */
313 u32 _rxfgmask; /* 0xaa4 */
314 u32 _rx14mask; /* 0xaa8 */
315 u32 _rx15mask; /* 0xaac */
316 u32 tx_smb[4]; /* 0xab0 */
317 u32 rx_smb0[4]; /* 0xac0 */
318 u32 rx_smb1[4]; /* 0xad0 */
319 );
Stefan Agnercdce8442014-07-15 14:56:21 +0200320 u32 mecr; /* 0xae0 */
321 u32 erriar; /* 0xae4 */
322 u32 erridpr; /* 0xae8 */
323 u32 errippr; /* 0xaec */
324 u32 rerrar; /* 0xaf0 */
325 u32 rerrdr; /* 0xaf4 */
326 u32 rerrsynr; /* 0xaf8 */
327 u32 errsr; /* 0xafc */
Marc Kleine-Budde4b702872020-09-22 16:44:12 +0200328 u32 _reserved7[64]; /* 0xb00 */
Marc Kleine-Buddefe63a062020-09-22 16:44:13 +0200329 u32 fdctrl; /* 0xc00 - Not affected by Soft Reset */
330 u32 fdcbt; /* 0xc04 - Not affected by Soft Reset */
Marc Kleine-Budde4b702872020-09-22 16:44:12 +0200331 u32 fdcrc; /* 0xc08 */
Joakim Zhanga6597122020-09-30 05:15:55 +0800332 u32 _reserved9[199]; /* 0xc0c */
Kees Cookc92a08c2021-07-31 17:50:58 -0700333 struct_group(init_fd,
334 u32 tx_smb_fd[18]; /* 0xf28 */
335 u32 rx_smb0_fd[18]; /* 0xf70 */
336 u32 rx_smb1_fd[18]; /* 0xfb8 */
337 );
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200338};
339
Joakim Zhanga6597122020-09-30 05:15:55 +0800340static_assert(sizeof(struct flexcan_regs) == 0x4 * 18 + 0xfb8);
Marc Kleine-Budde4b702872020-09-22 16:44:12 +0200341
Hui Wang30c1e672012-06-28 16:21:35 +0800342struct flexcan_devtype_data {
Marc Kleine-Buddef377bff2015-05-08 15:22:36 +0200343 u32 quirks; /* quirks needed for different IP cores */
Hui Wang30c1e672012-06-28 16:21:35 +0800344};
345
Aisheng Dongde3578c2018-11-23 08:35:33 +0000346struct flexcan_stop_mode {
347 struct regmap *gpr;
348 u8 req_gpr;
349 u8 req_bit;
Aisheng Dongde3578c2018-11-23 08:35:33 +0000350};
351
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200352struct flexcan_priv {
353 struct can_priv can;
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200354 struct can_rx_offload offload;
Aisheng Dongca109892018-11-30 08:53:26 +0000355 struct device *dev;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200356
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200357 struct flexcan_regs __iomem *regs;
Pankaj Bansal05179612018-11-23 22:18:44 +0100358 struct flexcan_mb __iomem *tx_mb;
Marc Kleine-Buddeb93917c2015-07-12 00:47:47 +0200359 struct flexcan_mb __iomem *tx_mb_reserved;
Pankaj Bansal05179612018-11-23 22:18:44 +0100360 u8 tx_mb_idx;
361 u8 mb_count;
362 u8 mb_size;
Dong Aisheng8c306be2018-12-13 07:08:00 +0000363 u8 clk_src; /* clock source of CAN Protocol Engine */
Joakim Zhang812f0112020-11-06 18:56:27 +0800364 u8 scu_idx;
Dong Aisheng8c306be2018-12-13 07:08:00 +0000365
Marc Kleine-Budde8ce51392019-03-01 12:17:30 +0100366 u64 rx_mask;
Marc Kleine-Budde0ca64f022019-03-01 13:54:19 +0100367 u64 tx_mask;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200368 u32 reg_ctrl_default;
369
Steffen Trumtrar3d42a372012-07-17 16:14:34 +0200370 struct clk *clk_ipg;
371 struct clk *clk_per;
Marc Kleine-Buddedda0b3b2012-07-13 14:52:48 +0200372 const struct flexcan_devtype_data *devtype_data;
Fabio Estevamb7c41142013-06-10 23:12:57 -0300373 struct regulator *reg_xceiver;
Aisheng Dongde3578c2018-11-23 08:35:33 +0000374 struct flexcan_stop_mode stm;
Pankaj Bansal88462d22017-11-24 18:52:08 +0530375
Angelo Dureghellod9cead72021-07-02 11:48:41 +0200376 int irq_boff;
377 int irq_err;
378
Joakim Zhang812f0112020-11-06 18:56:27 +0800379 /* IPC handle when setup stop mode by System Controller firmware(scfw) */
380 struct imx_sc_ipc *sc_ipc_handle;
381
Pankaj Bansal88462d22017-11-24 18:52:08 +0530382 /* Read and Write APIs */
383 u32 (*read)(void __iomem *addr);
384 void (*write)(u32 val, void __iomem *addr);
Hui Wang30c1e672012-06-28 16:21:35 +0800385};
386
Angelo Dureghellod9cead72021-07-02 11:48:41 +0200387static const struct flexcan_devtype_data fsl_mcf5441x_devtype_data = {
388 .quirks = FLEXCAN_QUIRK_BROKEN_PERR_STATE |
389 FLEXCAN_QUIRK_NR_IRQ_3 | FLEXCAN_QUIRK_NR_MB_16,
390};
391
Marc Kleine-Buddea3c11a72016-07-04 14:45:44 +0200392static const struct flexcan_devtype_data fsl_p1010_devtype_data = {
ZHU Yi (ST-FIR/ENG1-Zhu)fb5b91d62017-09-15 07:09:37 +0000393 .quirks = FLEXCAN_QUIRK_BROKEN_WERR_STATE |
Uwe Kleine-König0e030a32018-04-25 16:50:39 +0200394 FLEXCAN_QUIRK_BROKEN_PERR_STATE |
395 FLEXCAN_QUIRK_DEFAULT_BIG_ENDIAN,
396};
397
398static const struct flexcan_devtype_data fsl_imx25_devtype_data = {
399 .quirks = FLEXCAN_QUIRK_BROKEN_WERR_STATE |
ZHU Yi (ST-FIR/ENG1-Zhu)fb5b91d62017-09-15 07:09:37 +0000400 FLEXCAN_QUIRK_BROKEN_PERR_STATE,
Hui Wang30c1e672012-06-28 16:21:35 +0800401};
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200402
ZHU Yi (ST-FIR/ENG1-Zhu)083c5572017-09-15 07:08:23 +0000403static const struct flexcan_devtype_data fsl_imx28_devtype_data = {
404 .quirks = FLEXCAN_QUIRK_BROKEN_PERR_STATE,
405};
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200406
Marc Kleine-Buddea3c11a72016-07-04 14:45:44 +0200407static const struct flexcan_devtype_data fsl_imx6q_devtype_data = {
Marc Kleine-Budde096de072015-09-01 10:28:46 +0200408 .quirks = FLEXCAN_QUIRK_DISABLE_RXFG | FLEXCAN_QUIRK_ENABLE_EACEN_RRS |
Aisheng Dongde3578c2018-11-23 08:35:33 +0000409 FLEXCAN_QUIRK_USE_OFF_TIMESTAMP | FLEXCAN_QUIRK_BROKEN_PERR_STATE |
Joakim Zhang14579982020-11-06 18:56:25 +0800410 FLEXCAN_QUIRK_SETUP_STOP_MODE_GPR,
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200411};
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200412
Joakim Zhang2a1993e2019-07-12 08:02:59 +0000413static const struct flexcan_devtype_data fsl_imx8qm_devtype_data = {
414 .quirks = FLEXCAN_QUIRK_DISABLE_RXFG | FLEXCAN_QUIRK_ENABLE_EACEN_RRS |
415 FLEXCAN_QUIRK_USE_OFF_TIMESTAMP | FLEXCAN_QUIRK_BROKEN_PERR_STATE |
Joakim Zhang812f0112020-11-06 18:56:27 +0800416 FLEXCAN_QUIRK_SUPPORT_FD | FLEXCAN_QUIRK_SETUP_STOP_MODE_SCFW,
Joakim Zhang2a1993e2019-07-12 08:02:59 +0000417};
418
Joakim Zhang3aa25392020-09-30 05:15:56 +0800419static struct flexcan_devtype_data fsl_imx8mp_devtype_data = {
420 .quirks = FLEXCAN_QUIRK_DISABLE_RXFG | FLEXCAN_QUIRK_ENABLE_EACEN_RRS |
421 FLEXCAN_QUIRK_DISABLE_MECR | FLEXCAN_QUIRK_USE_OFF_TIMESTAMP |
Joakim Zhang14579982020-11-06 18:56:25 +0800422 FLEXCAN_QUIRK_BROKEN_PERR_STATE | FLEXCAN_QUIRK_SETUP_STOP_MODE_GPR |
Joakim Zhang3aa25392020-09-30 05:15:56 +0800423 FLEXCAN_QUIRK_SUPPORT_FD | FLEXCAN_QUIRK_SUPPORT_ECC,
424};
425
Marc Kleine-Buddea3c11a72016-07-04 14:45:44 +0200426static const struct flexcan_devtype_data fsl_vf610_devtype_data = {
Marc Kleine-Budde9eb7aa82015-09-01 08:57:55 +0200427 .quirks = FLEXCAN_QUIRK_DISABLE_RXFG | FLEXCAN_QUIRK_ENABLE_EACEN_RRS |
Marc Kleine-Budde29c64b12017-11-27 09:18:21 +0100428 FLEXCAN_QUIRK_DISABLE_MECR | FLEXCAN_QUIRK_USE_OFF_TIMESTAMP |
Joakim Zhang5fdf04a2020-10-20 23:53:57 +0800429 FLEXCAN_QUIRK_BROKEN_PERR_STATE | FLEXCAN_QUIRK_SUPPORT_ECC,
Stefan Agnercdce8442014-07-15 14:56:21 +0200430};
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200431
Pankaj Bansal99b76682017-11-24 18:52:09 +0530432static const struct flexcan_devtype_data fsl_ls1021a_r2_devtype_data = {
433 .quirks = FLEXCAN_QUIRK_DISABLE_RXFG | FLEXCAN_QUIRK_ENABLE_EACEN_RRS |
Joakim Zhang01879962020-10-20 23:53:55 +0800434 FLEXCAN_QUIRK_BROKEN_PERR_STATE | FLEXCAN_QUIRK_USE_OFF_TIMESTAMP,
Pankaj Bansal99b76682017-11-24 18:52:09 +0530435};
436
Joakim Zhang2c19bb42019-07-12 08:03:01 +0000437static const struct flexcan_devtype_data fsl_lx2160a_r1_devtype_data = {
438 .quirks = FLEXCAN_QUIRK_DISABLE_RXFG | FLEXCAN_QUIRK_ENABLE_EACEN_RRS |
439 FLEXCAN_QUIRK_DISABLE_MECR | FLEXCAN_QUIRK_BROKEN_PERR_STATE |
Joakim Zhang91a22bf2020-10-20 23:53:56 +0800440 FLEXCAN_QUIRK_USE_OFF_TIMESTAMP | FLEXCAN_QUIRK_SUPPORT_FD |
441 FLEXCAN_QUIRK_SUPPORT_ECC,
Joakim Zhang2c19bb42019-07-12 08:03:01 +0000442};
443
Marc Kleine-Budde194b9a42012-07-16 12:58:31 +0200444static const struct can_bittiming_const flexcan_bittiming_const = {
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200445 .name = DRV_NAME,
446 .tseg1_min = 4,
447 .tseg1_max = 16,
448 .tseg2_min = 2,
449 .tseg2_max = 8,
450 .sjw_max = 4,
451 .brp_min = 1,
452 .brp_max = 256,
453 .brp_inc = 1,
454};
455
Joakim Zhangeadf6ca2019-07-12 08:02:44 +0000456static const struct can_bittiming_const flexcan_fd_bittiming_const = {
457 .name = DRV_NAME,
458 .tseg1_min = 2,
459 .tseg1_max = 96,
460 .tseg2_min = 2,
461 .tseg2_max = 32,
462 .sjw_max = 16,
463 .brp_min = 1,
464 .brp_max = 1024,
465 .brp_inc = 1,
466};
467
468static const struct can_bittiming_const flexcan_fd_data_bittiming_const = {
469 .name = DRV_NAME,
470 .tseg1_min = 2,
471 .tseg1_max = 39,
472 .tseg2_min = 2,
473 .tseg2_max = 8,
474 .sjw_max = 4,
475 .brp_min = 1,
476 .brp_max = 1024,
477 .brp_inc = 1,
478};
479
Pankaj Bansal88462d22017-11-24 18:52:08 +0530480/* FlexCAN module is essentially modelled as a little-endian IP in most
481 * SoCs, i.e the registers as well as the message buffer areas are
482 * implemented in a little-endian fashion.
483 *
484 * However there are some SoCs (e.g. LS1021A) which implement the FlexCAN
485 * module in a big-endian fashion (i.e the registers as well as the
486 * message buffer areas are implemented in a big-endian way).
487 *
488 * In addition, the FlexCAN module can be found on SoCs having ARM or
489 * PPC cores. So, we need to abstract off the register read/write
490 * functions, ensuring that these cater to all the combinations of module
491 * endianness and underlying CPU endianness.
holt@sgi.com61e271e2011-08-16 17:32:20 +0000492 */
Pankaj Bansal88462d22017-11-24 18:52:08 +0530493static inline u32 flexcan_read_be(void __iomem *addr)
holt@sgi.com61e271e2011-08-16 17:32:20 +0000494{
Pankaj Bansal88462d22017-11-24 18:52:08 +0530495 return ioread32be(addr);
holt@sgi.com61e271e2011-08-16 17:32:20 +0000496}
497
Pankaj Bansal88462d22017-11-24 18:52:08 +0530498static inline void flexcan_write_be(u32 val, void __iomem *addr)
holt@sgi.com61e271e2011-08-16 17:32:20 +0000499{
Pankaj Bansal88462d22017-11-24 18:52:08 +0530500 iowrite32be(val, addr);
holt@sgi.com61e271e2011-08-16 17:32:20 +0000501}
502
Pankaj Bansal88462d22017-11-24 18:52:08 +0530503static inline u32 flexcan_read_le(void __iomem *addr)
holt@sgi.com61e271e2011-08-16 17:32:20 +0000504{
Pankaj Bansal88462d22017-11-24 18:52:08 +0530505 return ioread32(addr);
holt@sgi.com61e271e2011-08-16 17:32:20 +0000506}
Pankaj Bansal88462d22017-11-24 18:52:08 +0530507
508static inline void flexcan_write_le(u32 val, void __iomem *addr)
509{
510 iowrite32(val, addr);
511}
holt@sgi.com61e271e2011-08-16 17:32:20 +0000512
Pankaj Bansal05179612018-11-23 22:18:44 +0100513static struct flexcan_mb __iomem *flexcan_get_mb(const struct flexcan_priv *priv,
514 u8 mb_index)
515{
Pankaj Bansal6cbf7602018-08-28 23:19:12 +0530516 u8 bank_size;
517 bool bank;
518
Pankaj Bansal05179612018-11-23 22:18:44 +0100519 if (WARN_ON(mb_index >= priv->mb_count))
520 return NULL;
521
Pankaj Bansal6cbf7602018-08-28 23:19:12 +0530522 bank_size = sizeof(priv->regs->mb[0]) / priv->mb_size;
523
524 bank = mb_index >= bank_size;
525 if (bank)
526 mb_index -= bank_size;
527
Pankaj Bansal05179612018-11-23 22:18:44 +0100528 return (struct flexcan_mb __iomem *)
Pankaj Bansal6cbf7602018-08-28 23:19:12 +0530529 (&priv->regs->mb[bank][priv->mb_size * mb_index]);
Pankaj Bansal05179612018-11-23 22:18:44 +0100530}
531
Joakim Zhangb7603d02019-12-04 11:36:11 +0000532static int flexcan_low_power_enter_ack(struct flexcan_priv *priv)
533{
534 struct flexcan_regs __iomem *regs = priv->regs;
535 unsigned int timeout = FLEXCAN_TIMEOUT_US / 10;
536
537 while (timeout-- && !(priv->read(&regs->mcr) & FLEXCAN_MCR_LPM_ACK))
538 udelay(10);
539
540 if (!(priv->read(&regs->mcr) & FLEXCAN_MCR_LPM_ACK))
541 return -ETIMEDOUT;
542
543 return 0;
544}
545
546static int flexcan_low_power_exit_ack(struct flexcan_priv *priv)
547{
548 struct flexcan_regs __iomem *regs = priv->regs;
549 unsigned int timeout = FLEXCAN_TIMEOUT_US / 10;
550
551 while (timeout-- && (priv->read(&regs->mcr) & FLEXCAN_MCR_LPM_ACK))
552 udelay(10);
553
554 if (priv->read(&regs->mcr) & FLEXCAN_MCR_LPM_ACK)
555 return -ETIMEDOUT;
556
557 return 0;
558}
559
Aisheng Dongde3578c2018-11-23 08:35:33 +0000560static void flexcan_enable_wakeup_irq(struct flexcan_priv *priv, bool enable)
561{
562 struct flexcan_regs __iomem *regs = priv->regs;
563 u32 reg_mcr;
564
565 reg_mcr = priv->read(&regs->mcr);
566
567 if (enable)
568 reg_mcr |= FLEXCAN_MCR_WAK_MSK;
569 else
570 reg_mcr &= ~FLEXCAN_MCR_WAK_MSK;
571
572 priv->write(reg_mcr, &regs->mcr);
573}
574
Joakim Zhang812f0112020-11-06 18:56:27 +0800575static int flexcan_stop_mode_enable_scfw(struct flexcan_priv *priv, bool enabled)
576{
577 u8 idx = priv->scu_idx;
578 u32 rsrc_id, val;
579
580 rsrc_id = IMX_SC_R_CAN(idx);
581
582 if (enabled)
583 val = 1;
584 else
585 val = 0;
586
587 /* stop mode request via scu firmware */
588 return imx_sc_misc_set_control(priv->sc_ipc_handle, rsrc_id,
589 IMX_SC_C_IPG_STOP, val);
590}
591
Joakim Zhang5f186c22019-07-02 01:45:41 +0000592static inline int flexcan_enter_stop_mode(struct flexcan_priv *priv)
Aisheng Dongde3578c2018-11-23 08:35:33 +0000593{
594 struct flexcan_regs __iomem *regs = priv->regs;
595 u32 reg_mcr;
Joakim Zhang812f0112020-11-06 18:56:27 +0800596 int ret;
Aisheng Dongde3578c2018-11-23 08:35:33 +0000597
598 reg_mcr = priv->read(&regs->mcr);
599 reg_mcr |= FLEXCAN_MCR_SLF_WAK;
600 priv->write(reg_mcr, &regs->mcr);
601
602 /* enable stop request */
Joakim Zhang812f0112020-11-06 18:56:27 +0800603 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_SETUP_STOP_MODE_SCFW) {
604 ret = flexcan_stop_mode_enable_scfw(priv, true);
605 if (ret < 0)
606 return ret;
607 } else {
608 regmap_update_bits(priv->stm.gpr, priv->stm.req_gpr,
609 1 << priv->stm.req_bit, 1 << priv->stm.req_bit);
610 }
Joakim Zhang5f186c22019-07-02 01:45:41 +0000611
Joakim Zhang048e3a342019-12-04 11:36:14 +0000612 return flexcan_low_power_enter_ack(priv);
Aisheng Dongde3578c2018-11-23 08:35:33 +0000613}
614
Joakim Zhang5f186c22019-07-02 01:45:41 +0000615static inline int flexcan_exit_stop_mode(struct flexcan_priv *priv)
Aisheng Dongde3578c2018-11-23 08:35:33 +0000616{
617 struct flexcan_regs __iomem *regs = priv->regs;
618 u32 reg_mcr;
Joakim Zhang812f0112020-11-06 18:56:27 +0800619 int ret;
Aisheng Dongde3578c2018-11-23 08:35:33 +0000620
621 /* remove stop request */
Joakim Zhang812f0112020-11-06 18:56:27 +0800622 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_SETUP_STOP_MODE_SCFW) {
623 ret = flexcan_stop_mode_enable_scfw(priv, false);
624 if (ret < 0)
625 return ret;
626 } else {
627 regmap_update_bits(priv->stm.gpr, priv->stm.req_gpr,
628 1 << priv->stm.req_bit, 0);
629 }
Aisheng Dongde3578c2018-11-23 08:35:33 +0000630
631 reg_mcr = priv->read(&regs->mcr);
632 reg_mcr &= ~FLEXCAN_MCR_SLF_WAK;
633 priv->write(reg_mcr, &regs->mcr);
Joakim Zhang5f186c22019-07-02 01:45:41 +0000634
Joakim Zhang048e3a342019-12-04 11:36:14 +0000635 return flexcan_low_power_exit_ack(priv);
Aisheng Dongde3578c2018-11-23 08:35:33 +0000636}
637
ZHU Yi (ST-FIR/ENG1-Zhu)da49a802017-09-15 07:03:58 +0000638static inline void flexcan_error_irq_enable(const struct flexcan_priv *priv)
639{
640 struct flexcan_regs __iomem *regs = priv->regs;
641 u32 reg_ctrl = (priv->reg_ctrl_default | FLEXCAN_CTRL_ERR_MSK);
642
Pankaj Bansal88462d22017-11-24 18:52:08 +0530643 priv->write(reg_ctrl, &regs->ctrl);
ZHU Yi (ST-FIR/ENG1-Zhu)da49a802017-09-15 07:03:58 +0000644}
645
646static inline void flexcan_error_irq_disable(const struct flexcan_priv *priv)
647{
648 struct flexcan_regs __iomem *regs = priv->regs;
649 u32 reg_ctrl = (priv->reg_ctrl_default & ~FLEXCAN_CTRL_ERR_MSK);
650
Pankaj Bansal88462d22017-11-24 18:52:08 +0530651 priv->write(reg_ctrl, &regs->ctrl);
ZHU Yi (ST-FIR/ENG1-Zhu)da49a802017-09-15 07:03:58 +0000652}
653
Aisheng Dongca109892018-11-30 08:53:26 +0000654static int flexcan_clks_enable(const struct flexcan_priv *priv)
655{
Marc Kleine-Budde33626662021-07-28 09:51:42 +0200656 int err = 0;
Aisheng Dongca109892018-11-30 08:53:26 +0000657
Angelo Dureghellod9cead72021-07-02 11:48:41 +0200658 if (priv->clk_ipg) {
659 err = clk_prepare_enable(priv->clk_ipg);
660 if (err)
661 return err;
662 }
Aisheng Dongca109892018-11-30 08:53:26 +0000663
Angelo Dureghellod9cead72021-07-02 11:48:41 +0200664 if (priv->clk_per) {
665 err = clk_prepare_enable(priv->clk_per);
666 if (err)
667 clk_disable_unprepare(priv->clk_ipg);
668 }
Aisheng Dongca109892018-11-30 08:53:26 +0000669
670 return err;
671}
672
673static void flexcan_clks_disable(const struct flexcan_priv *priv)
674{
675 clk_disable_unprepare(priv->clk_per);
676 clk_disable_unprepare(priv->clk_ipg);
677}
678
Marc Kleine-Buddef0036982014-02-28 17:18:27 +0100679static inline int flexcan_transceiver_enable(const struct flexcan_priv *priv)
680{
681 if (!priv->reg_xceiver)
682 return 0;
683
684 return regulator_enable(priv->reg_xceiver);
685}
686
687static inline int flexcan_transceiver_disable(const struct flexcan_priv *priv)
688{
689 if (!priv->reg_xceiver)
690 return 0;
691
692 return regulator_disable(priv->reg_xceiver);
693}
694
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100695static int flexcan_chip_enable(struct flexcan_priv *priv)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200696{
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200697 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200698 u32 reg;
699
Pankaj Bansal88462d22017-11-24 18:52:08 +0530700 reg = priv->read(&regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200701 reg &= ~FLEXCAN_MCR_MDIS;
Pankaj Bansal88462d22017-11-24 18:52:08 +0530702 priv->write(reg, &regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200703
Joakim Zhangb7603d02019-12-04 11:36:11 +0000704 return flexcan_low_power_exit_ack(priv);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200705}
706
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100707static int flexcan_chip_disable(struct flexcan_priv *priv)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200708{
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200709 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200710 u32 reg;
711
Pankaj Bansal88462d22017-11-24 18:52:08 +0530712 reg = priv->read(&regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200713 reg |= FLEXCAN_MCR_MDIS;
Pankaj Bansal88462d22017-11-24 18:52:08 +0530714 priv->write(reg, &regs->mcr);
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100715
Joakim Zhangb7603d02019-12-04 11:36:11 +0000716 return flexcan_low_power_enter_ack(priv);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200717}
718
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100719static int flexcan_chip_freeze(struct flexcan_priv *priv)
720{
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200721 struct flexcan_regs __iomem *regs = priv->regs;
Angelo Dureghello47c5e472021-03-16 00:15:10 +0100722 unsigned int timeout;
723 u32 bitrate = priv->can.bittiming.bitrate;
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100724 u32 reg;
725
Angelo Dureghello47c5e472021-03-16 00:15:10 +0100726 if (bitrate)
727 timeout = 1000 * 1000 * 10 / bitrate;
728 else
729 timeout = FLEXCAN_TIMEOUT_US / 10;
730
Pankaj Bansal88462d22017-11-24 18:52:08 +0530731 reg = priv->read(&regs->mcr);
Joakim Zhang449052c2021-02-18 19:00:35 +0800732 reg |= FLEXCAN_MCR_FRZ | FLEXCAN_MCR_HALT;
Pankaj Bansal88462d22017-11-24 18:52:08 +0530733 priv->write(reg, &regs->mcr);
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100734
Pankaj Bansal88462d22017-11-24 18:52:08 +0530735 while (timeout-- && !(priv->read(&regs->mcr) & FLEXCAN_MCR_FRZ_ACK))
David Jander8badd652014-08-27 12:02:16 +0200736 udelay(100);
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100737
Pankaj Bansal88462d22017-11-24 18:52:08 +0530738 if (!(priv->read(&regs->mcr) & FLEXCAN_MCR_FRZ_ACK))
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100739 return -ETIMEDOUT;
740
741 return 0;
742}
743
744static int flexcan_chip_unfreeze(struct flexcan_priv *priv)
745{
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200746 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100747 unsigned int timeout = FLEXCAN_TIMEOUT_US / 10;
748 u32 reg;
749
Pankaj Bansal88462d22017-11-24 18:52:08 +0530750 reg = priv->read(&regs->mcr);
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100751 reg &= ~FLEXCAN_MCR_HALT;
Pankaj Bansal88462d22017-11-24 18:52:08 +0530752 priv->write(reg, &regs->mcr);
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100753
Pankaj Bansal88462d22017-11-24 18:52:08 +0530754 while (timeout-- && (priv->read(&regs->mcr) & FLEXCAN_MCR_FRZ_ACK))
David Jander8badd652014-08-27 12:02:16 +0200755 udelay(10);
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100756
Pankaj Bansal88462d22017-11-24 18:52:08 +0530757 if (priv->read(&regs->mcr) & FLEXCAN_MCR_FRZ_ACK)
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100758 return -ETIMEDOUT;
759
760 return 0;
761}
762
Marc Kleine-Budde4b5b8222014-02-28 15:16:59 +0100763static int flexcan_chip_softreset(struct flexcan_priv *priv)
764{
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200765 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Budde4b5b8222014-02-28 15:16:59 +0100766 unsigned int timeout = FLEXCAN_TIMEOUT_US / 10;
767
Pankaj Bansal88462d22017-11-24 18:52:08 +0530768 priv->write(FLEXCAN_MCR_SOFTRST, &regs->mcr);
769 while (timeout-- && (priv->read(&regs->mcr) & FLEXCAN_MCR_SOFTRST))
David Jander8badd652014-08-27 12:02:16 +0200770 udelay(10);
Marc Kleine-Budde4b5b8222014-02-28 15:16:59 +0100771
Pankaj Bansal88462d22017-11-24 18:52:08 +0530772 if (priv->read(&regs->mcr) & FLEXCAN_MCR_SOFTRST)
Marc Kleine-Budde4b5b8222014-02-28 15:16:59 +0100773 return -ETIMEDOUT;
774
775 return 0;
776}
777
Stefan Agnerec56acf2014-07-15 14:56:20 +0200778static int __flexcan_get_berr_counter(const struct net_device *dev,
779 struct can_berr_counter *bec)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200780{
781 const struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200782 struct flexcan_regs __iomem *regs = priv->regs;
Pankaj Bansal88462d22017-11-24 18:52:08 +0530783 u32 reg = priv->read(&regs->ecr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200784
785 bec->txerr = (reg >> 0) & 0xff;
786 bec->rxerr = (reg >> 8) & 0xff;
787
788 return 0;
789}
790
Stefan Agnerec56acf2014-07-15 14:56:20 +0200791static int flexcan_get_berr_counter(const struct net_device *dev,
792 struct can_berr_counter *bec)
793{
794 const struct flexcan_priv *priv = netdev_priv(dev);
795 int err;
796
Aisheng Dongca109892018-11-30 08:53:26 +0000797 err = pm_runtime_get_sync(priv->dev);
Zhang Qilongb7ee5bc2020-11-08 16:30:00 +0800798 if (err < 0) {
799 pm_runtime_put_noidle(priv->dev);
Stefan Agnerec56acf2014-07-15 14:56:20 +0200800 return err;
Zhang Qilongb7ee5bc2020-11-08 16:30:00 +0800801 }
Stefan Agnerec56acf2014-07-15 14:56:20 +0200802
Stefan Agnerec56acf2014-07-15 14:56:20 +0200803 err = __flexcan_get_berr_counter(dev, bec);
804
Aisheng Dongca109892018-11-30 08:53:26 +0000805 pm_runtime_put(priv->dev);
Stefan Agnerec56acf2014-07-15 14:56:20 +0200806
807 return err;
808}
809
Marc Kleine-Buddefb1e13e62018-04-26 23:13:38 +0200810static netdev_tx_t flexcan_start_xmit(struct sk_buff *skb, struct net_device *dev)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200811{
812 const struct flexcan_priv *priv = netdev_priv(dev);
Joakim Zhangd9b90b02019-07-12 08:02:41 +0000813 struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200814 u32 can_id;
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200815 u32 data;
Oliver Hartkopp3ab4ce02020-11-10 11:18:49 +0100816 u32 ctrl = FLEXCAN_MB_CODE_TX_DATA | ((can_fd_len2dlc(cfd->len)) << 16);
Pankaj Bansal05179612018-11-23 22:18:44 +0100817 int i;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200818
819 if (can_dropped_invalid_skb(dev, skb))
820 return NETDEV_TX_OK;
821
822 netif_stop_queue(dev);
823
Joakim Zhangd9b90b02019-07-12 08:02:41 +0000824 if (cfd->can_id & CAN_EFF_FLAG) {
825 can_id = cfd->can_id & CAN_EFF_MASK;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200826 ctrl |= FLEXCAN_MB_CNT_IDE | FLEXCAN_MB_CNT_SRR;
827 } else {
Joakim Zhangd9b90b02019-07-12 08:02:41 +0000828 can_id = (cfd->can_id & CAN_SFF_MASK) << 18;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200829 }
830
Joakim Zhangd9b90b02019-07-12 08:02:41 +0000831 if (cfd->can_id & CAN_RTR_FLAG)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200832 ctrl |= FLEXCAN_MB_CNT_RTR;
833
Joakim Zhang05429202019-07-12 08:02:47 +0000834 if (can_is_canfd_skb(skb)) {
Joakim Zhangeadf6ca2019-07-12 08:02:44 +0000835 ctrl |= FLEXCAN_MB_CNT_EDL;
836
Joakim Zhang05429202019-07-12 08:02:47 +0000837 if (cfd->flags & CANFD_BRS)
838 ctrl |= FLEXCAN_MB_CNT_BRS;
839 }
840
Joakim Zhangd9b90b02019-07-12 08:02:41 +0000841 for (i = 0; i < cfd->len; i += sizeof(u32)) {
842 data = be32_to_cpup((__be32 *)&cfd->data[i]);
Pankaj Bansal05179612018-11-23 22:18:44 +0100843 priv->write(data, &priv->tx_mb->data[i / sizeof(u32)]);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200844 }
845
Vincent Mailhol1dcb6e52021-01-11 15:19:27 +0100846 can_put_echo_skb(skb, dev, 0, 0);
Reuben Dowle9a123492011-11-01 11:18:03 +1300847
Pankaj Bansal05179612018-11-23 22:18:44 +0100848 priv->write(can_id, &priv->tx_mb->can_id);
849 priv->write(ctrl, &priv->tx_mb->can_ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200850
David Jander25e92442014-09-03 16:47:22 +0200851 /* Errata ERR005829 step8:
852 * Write twice INACTIVE(0x8) code to first MB.
853 */
Pankaj Bansal88462d22017-11-24 18:52:08 +0530854 priv->write(FLEXCAN_MB_CODE_TX_INACTIVE,
Marc Kleine-Budde9dc1ee12018-11-12 15:33:57 +0100855 &priv->tx_mb_reserved->can_ctrl);
Pankaj Bansal88462d22017-11-24 18:52:08 +0530856 priv->write(FLEXCAN_MB_CODE_TX_INACTIVE,
Marc Kleine-Budde9dc1ee12018-11-12 15:33:57 +0100857 &priv->tx_mb_reserved->can_ctrl);
David Jander25e92442014-09-03 16:47:22 +0200858
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200859 return NETDEV_TX_OK;
860}
861
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200862static void flexcan_irq_bus_err(struct net_device *dev, u32 reg_esr)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200863{
864 struct flexcan_priv *priv = netdev_priv(dev);
Oleksij Rempeld788905f2018-09-18 11:40:41 +0200865 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddea5c02f662017-01-18 11:38:26 +0100866 struct sk_buff *skb;
867 struct can_frame *cf;
Marc Kleine-Budded166f562017-01-17 17:33:46 +0100868 bool rx_errors = false, tx_errors = false;
Oleksij Rempeld788905f2018-09-18 11:40:41 +0200869 u32 timestamp;
Marc Kleine-Budde75812432019-07-15 20:53:08 +0200870 int err;
Oleksij Rempeld788905f2018-09-18 11:40:41 +0200871
872 timestamp = priv->read(&regs->timer) << 16;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200873
Marc Kleine-Buddea5c02f662017-01-18 11:38:26 +0100874 skb = alloc_can_err_skb(dev, &cf);
875 if (unlikely(!skb))
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200876 return;
Marc Kleine-Buddea5c02f662017-01-18 11:38:26 +0100877
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200878 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
879
880 if (reg_esr & FLEXCAN_ESR_BIT1_ERR) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100881 netdev_dbg(dev, "BIT1_ERR irq\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200882 cf->data[2] |= CAN_ERR_PROT_BIT1;
Marc Kleine-Budded166f562017-01-17 17:33:46 +0100883 tx_errors = true;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200884 }
885 if (reg_esr & FLEXCAN_ESR_BIT0_ERR) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100886 netdev_dbg(dev, "BIT0_ERR irq\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200887 cf->data[2] |= CAN_ERR_PROT_BIT0;
Marc Kleine-Budded166f562017-01-17 17:33:46 +0100888 tx_errors = true;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200889 }
890 if (reg_esr & FLEXCAN_ESR_ACK_ERR) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100891 netdev_dbg(dev, "ACK_ERR irq\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200892 cf->can_id |= CAN_ERR_ACK;
Oliver Hartkoppffd461f2015-11-21 18:41:20 +0100893 cf->data[3] = CAN_ERR_PROT_LOC_ACK;
Marc Kleine-Budded166f562017-01-17 17:33:46 +0100894 tx_errors = true;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200895 }
896 if (reg_esr & FLEXCAN_ESR_CRC_ERR) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100897 netdev_dbg(dev, "CRC_ERR irq\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200898 cf->data[2] |= CAN_ERR_PROT_BIT;
Oliver Hartkoppffd461f2015-11-21 18:41:20 +0100899 cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ;
Marc Kleine-Budded166f562017-01-17 17:33:46 +0100900 rx_errors = true;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200901 }
902 if (reg_esr & FLEXCAN_ESR_FRM_ERR) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100903 netdev_dbg(dev, "FRM_ERR irq\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200904 cf->data[2] |= CAN_ERR_PROT_FORM;
Marc Kleine-Budded166f562017-01-17 17:33:46 +0100905 rx_errors = true;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200906 }
907 if (reg_esr & FLEXCAN_ESR_STF_ERR) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100908 netdev_dbg(dev, "STF_ERR irq\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200909 cf->data[2] |= CAN_ERR_PROT_STUFF;
Marc Kleine-Budded166f562017-01-17 17:33:46 +0100910 rx_errors = true;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200911 }
912
913 priv->can.can_stats.bus_error++;
914 if (rx_errors)
915 dev->stats.rx_errors++;
916 if (tx_errors)
917 dev->stats.tx_errors++;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200918
Marc Kleine-Budde75812432019-07-15 20:53:08 +0200919 err = can_rx_offload_queue_sorted(&priv->offload, skb, timestamp);
920 if (err)
921 dev->stats.rx_fifo_errors++;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200922}
923
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200924static void flexcan_irq_state(struct net_device *dev, u32 reg_esr)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200925{
926 struct flexcan_priv *priv = netdev_priv(dev);
Oleksij Rempeld788905f2018-09-18 11:40:41 +0200927 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200928 struct sk_buff *skb;
929 struct can_frame *cf;
Marc Kleine-Budde238443d2017-01-18 11:25:41 +0100930 enum can_state new_state, rx_state, tx_state;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200931 int flt;
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000932 struct can_berr_counter bec;
Oleksij Rempeld788905f2018-09-18 11:40:41 +0200933 u32 timestamp;
Marc Kleine-Budde75812432019-07-15 20:53:08 +0200934 int err;
Oleksij Rempeld788905f2018-09-18 11:40:41 +0200935
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200936 flt = reg_esr & FLEXCAN_ESR_FLT_CONF_MASK;
937 if (likely(flt == FLEXCAN_ESR_FLT_CONF_ACTIVE)) {
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000938 tx_state = unlikely(reg_esr & FLEXCAN_ESR_TX_WRN) ?
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200939 CAN_STATE_ERROR_WARNING : CAN_STATE_ERROR_ACTIVE;
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000940 rx_state = unlikely(reg_esr & FLEXCAN_ESR_RX_WRN) ?
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200941 CAN_STATE_ERROR_WARNING : CAN_STATE_ERROR_ACTIVE;
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000942 new_state = max(tx_state, rx_state);
Andri Yngvason258ce802015-03-17 13:03:09 +0000943 } else {
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000944 __flexcan_get_berr_counter(dev, &bec);
Andri Yngvason258ce802015-03-17 13:03:09 +0000945 new_state = flt == FLEXCAN_ESR_FLT_CONF_PASSIVE ?
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200946 CAN_STATE_ERROR_PASSIVE : CAN_STATE_BUS_OFF;
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000947 rx_state = bec.rxerr >= bec.txerr ? new_state : 0;
948 tx_state = bec.rxerr <= bec.txerr ? new_state : 0;
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000949 }
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200950
951 /* state hasn't changed */
952 if (likely(new_state == priv->can.state))
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200953 return;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200954
Marc Kleine-Budde58ed8e72019-10-09 15:15:37 +0200955 timestamp = priv->read(&regs->timer) << 16;
956
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200957 skb = alloc_can_err_skb(dev, &cf);
958 if (unlikely(!skb))
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200959 return;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200960
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000961 can_change_state(dev, cf, tx_state, rx_state);
962
963 if (unlikely(new_state == CAN_STATE_BUS_OFF))
964 can_bus_off(dev);
965
Marc Kleine-Budde75812432019-07-15 20:53:08 +0200966 err = can_rx_offload_queue_sorted(&priv->offload, skb, timestamp);
967 if (err)
968 dev->stats.rx_fifo_errors++;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200969}
970
Marc Kleine-Budded3a51502019-03-01 15:38:05 +0100971static inline u64 flexcan_read64_mask(struct flexcan_priv *priv, void __iomem *addr, u64 mask)
972{
973 u64 reg = 0;
974
975 if (upper_32_bits(mask))
976 reg = (u64)priv->read(addr - 4) << 32;
977 if (lower_32_bits(mask))
978 reg |= priv->read(addr);
979
980 return reg & mask;
981}
982
Marc Kleine-Buddeb87c28b72019-03-01 15:38:05 +0100983static inline void flexcan_write64(struct flexcan_priv *priv, u64 val, void __iomem *addr)
984{
985 if (upper_32_bits(val))
986 priv->write(upper_32_bits(val), addr - 4);
987 if (lower_32_bits(val))
988 priv->write(lower_32_bits(val), addr);
989}
990
Marc Kleine-Budded3a51502019-03-01 15:38:05 +0100991static inline u64 flexcan_read_reg_iflag_rx(struct flexcan_priv *priv)
992{
993 return flexcan_read64_mask(priv, &priv->regs->iflag1, priv->rx_mask);
994}
995
Marc Kleine-Buddeb87c28b72019-03-01 15:38:05 +0100996static inline u64 flexcan_read_reg_iflag_tx(struct flexcan_priv *priv)
997{
998 return flexcan_read64_mask(priv, &priv->regs->iflag1, priv->tx_mask);
999}
1000
Marc Kleine-Budde30164752015-05-10 15:26:58 +02001001static inline struct flexcan_priv *rx_offload_to_priv(struct can_rx_offload *offload)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001002{
Marc Kleine-Budde30164752015-05-10 15:26:58 +02001003 return container_of(offload, struct flexcan_priv, offload);
1004}
1005
Joakim Zhang4e9c9482019-07-12 08:02:38 +00001006static struct sk_buff *flexcan_mailbox_read(struct can_rx_offload *offload,
1007 unsigned int n, u32 *timestamp,
1008 bool drop)
Marc Kleine-Budde30164752015-05-10 15:26:58 +02001009{
1010 struct flexcan_priv *priv = rx_offload_to_priv(offload);
Marc Kleine-Budde89af8742015-05-08 09:32:58 +02001011 struct flexcan_regs __iomem *regs = priv->regs;
Pankaj Bansal05179612018-11-23 22:18:44 +01001012 struct flexcan_mb __iomem *mb;
Joakim Zhang4e9c9482019-07-12 08:02:38 +00001013 struct sk_buff *skb;
Joakim Zhangd9b90b02019-07-12 08:02:41 +00001014 struct canfd_frame *cfd;
Marc Kleine-Budde30164752015-05-10 15:26:58 +02001015 u32 reg_ctrl, reg_id, reg_iflag1;
Pankaj Bansal05179612018-11-23 22:18:44 +01001016 int i;
1017
Joakim Zhang4e9c9482019-07-12 08:02:38 +00001018 if (unlikely(drop)) {
1019 skb = ERR_PTR(-ENOBUFS);
1020 goto mark_as_read;
1021 }
1022
Pankaj Bansal05179612018-11-23 22:18:44 +01001023 mb = flexcan_get_mb(priv, n);
Marc Kleine-Budde30164752015-05-10 15:26:58 +02001024
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +02001025 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP) {
1026 u32 code;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001027
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +02001028 do {
Pankaj Bansal88462d22017-11-24 18:52:08 +05301029 reg_ctrl = priv->read(&mb->can_ctrl);
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +02001030 } while (reg_ctrl & FLEXCAN_MB_CODE_RX_BUSY_BIT);
1031
1032 /* is this MB empty? */
1033 code = reg_ctrl & FLEXCAN_MB_CODE_MASK;
1034 if ((code != FLEXCAN_MB_CODE_RX_FULL) &&
1035 (code != FLEXCAN_MB_CODE_RX_OVERRUN))
Joakim Zhang4e9c9482019-07-12 08:02:38 +00001036 return NULL;
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +02001037
1038 if (code == FLEXCAN_MB_CODE_RX_OVERRUN) {
1039 /* This MB was overrun, we lost data */
1040 offload->dev->stats.rx_over_errors++;
1041 offload->dev->stats.rx_errors++;
1042 }
1043 } else {
Pankaj Bansal88462d22017-11-24 18:52:08 +05301044 reg_iflag1 = priv->read(&regs->iflag1);
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +02001045 if (!(reg_iflag1 & FLEXCAN_IFLAG_RX_FIFO_AVAILABLE))
Joakim Zhang4e9c9482019-07-12 08:02:38 +00001046 return NULL;
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +02001047
Pankaj Bansal88462d22017-11-24 18:52:08 +05301048 reg_ctrl = priv->read(&mb->can_ctrl);
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +02001049 }
1050
Joakim Zhangeadf6ca2019-07-12 08:02:44 +00001051 if (reg_ctrl & FLEXCAN_MB_CNT_EDL)
1052 skb = alloc_canfd_skb(offload->dev, &cfd);
1053 else
1054 skb = alloc_can_skb(offload->dev, (struct can_frame **)&cfd);
Joakim Zhangd9b90b02019-07-12 08:02:41 +00001055 if (unlikely(!skb)) {
Joakim Zhang4e9c9482019-07-12 08:02:38 +00001056 skb = ERR_PTR(-ENOMEM);
1057 goto mark_as_read;
1058 }
1059
Marc Kleine-Budde30164752015-05-10 15:26:58 +02001060 /* increase timstamp to full 32 bit */
1061 *timestamp = reg_ctrl << 16;
1062
Pankaj Bansal88462d22017-11-24 18:52:08 +05301063 reg_id = priv->read(&mb->can_id);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001064 if (reg_ctrl & FLEXCAN_MB_CNT_IDE)
Joakim Zhangd9b90b02019-07-12 08:02:41 +00001065 cfd->can_id = ((reg_id >> 0) & CAN_EFF_MASK) | CAN_EFF_FLAG;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001066 else
Joakim Zhangd9b90b02019-07-12 08:02:41 +00001067 cfd->can_id = (reg_id >> 18) & CAN_SFF_MASK;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001068
Joakim Zhangeadf6ca2019-07-12 08:02:44 +00001069 if (reg_ctrl & FLEXCAN_MB_CNT_EDL) {
Oliver Hartkopp3ab4ce02020-11-10 11:18:49 +01001070 cfd->len = can_fd_dlc2len((reg_ctrl >> 16) & 0xf);
Joakim Zhang05429202019-07-12 08:02:47 +00001071
1072 if (reg_ctrl & FLEXCAN_MB_CNT_BRS)
1073 cfd->flags |= CANFD_BRS;
Joakim Zhangeadf6ca2019-07-12 08:02:44 +00001074 } else {
Oliver Hartkopp69d989692020-11-10 11:18:46 +01001075 cfd->len = can_cc_dlc2len((reg_ctrl >> 16) & 0xf);
Joakim Zhangeadf6ca2019-07-12 08:02:44 +00001076
1077 if (reg_ctrl & FLEXCAN_MB_CNT_RTR)
1078 cfd->can_id |= CAN_RTR_FLAG;
1079 }
1080
1081 if (reg_ctrl & FLEXCAN_MB_CNT_ESI)
1082 cfd->flags |= CANFD_ESI;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001083
Joakim Zhangd9b90b02019-07-12 08:02:41 +00001084 for (i = 0; i < cfd->len; i += sizeof(u32)) {
Pankaj Bansal05179612018-11-23 22:18:44 +01001085 __be32 data = cpu_to_be32(priv->read(&mb->data[i / sizeof(u32)]));
Joakim Zhangd9b90b02019-07-12 08:02:41 +00001086 *(__be32 *)(cfd->data + i) = data;
Pankaj Bansal05179612018-11-23 22:18:44 +01001087 }
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001088
Joakim Zhang4e9c9482019-07-12 08:02:38 +00001089 mark_as_read:
Marc Kleine-Buddeb9468ad2019-03-01 16:27:59 +01001090 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP)
1091 flexcan_write64(priv, FLEXCAN_IFLAG_MB(n), &regs->iflag1);
1092 else
Pankaj Bansal88462d22017-11-24 18:52:08 +05301093 priv->write(FLEXCAN_IFLAG_RX_FIFO_AVAILABLE, &regs->iflag1);
Fabio Baltieriadccadb2012-12-18 18:50:58 +01001094
Pankaj Bansal5178b7c2018-08-01 19:36:46 +05301095 /* Read the Free Running Timer. It is optional but recommended
1096 * to unlock Mailbox as soon as possible and make it available
1097 * for reception.
1098 */
1099 priv->read(&regs->timer);
1100
Joakim Zhang4e9c9482019-07-12 08:02:38 +00001101 return skb;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001102}
1103
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001104static irqreturn_t flexcan_irq(int irq, void *dev_id)
1105{
1106 struct net_device *dev = dev_id;
1107 struct net_device_stats *stats = &dev->stats;
1108 struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Budde89af8742015-05-08 09:32:58 +02001109 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddedd2f1222017-01-18 11:45:14 +01001110 irqreturn_t handled = IRQ_NONE;
Marc Kleine-Budde0ca64f022019-03-01 13:54:19 +01001111 u64 reg_iflag_tx;
1112 u32 reg_esr;
ZHU Yi (ST-FIR/ENG1-Zhu)da49a802017-09-15 07:03:58 +00001113 enum can_state last_state = priv->can.state;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001114
Marc Kleine-Budde30164752015-05-10 15:26:58 +02001115 /* reception interrupt */
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +02001116 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP) {
Marc Kleine-Budde4e265982019-03-01 16:29:47 +01001117 u64 reg_iflag_rx;
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +02001118 int ret;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001119
Marc Kleine-Budde4e265982019-03-01 16:29:47 +01001120 while ((reg_iflag_rx = flexcan_read_reg_iflag_rx(priv))) {
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +02001121 handled = IRQ_HANDLED;
1122 ret = can_rx_offload_irq_offload_timestamp(&priv->offload,
Marc Kleine-Budde4e265982019-03-01 16:29:47 +01001123 reg_iflag_rx);
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +02001124 if (!ret)
1125 break;
1126 }
1127 } else {
Alexander Steincbffaf72018-10-11 17:01:25 +02001128 u32 reg_iflag1;
1129
1130 reg_iflag1 = priv->read(&regs->iflag1);
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +02001131 if (reg_iflag1 & FLEXCAN_IFLAG_RX_FIFO_AVAILABLE) {
1132 handled = IRQ_HANDLED;
1133 can_rx_offload_irq_offload_fifo(&priv->offload);
1134 }
1135
1136 /* FIFO overflow interrupt */
1137 if (reg_iflag1 & FLEXCAN_IFLAG_RX_FIFO_OVERFLOW) {
1138 handled = IRQ_HANDLED;
Pankaj Bansal88462d22017-11-24 18:52:08 +05301139 priv->write(FLEXCAN_IFLAG_RX_FIFO_OVERFLOW,
1140 &regs->iflag1);
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +02001141 dev->stats.rx_over_errors++;
1142 dev->stats.rx_errors++;
1143 }
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001144 }
1145
Marc Kleine-Buddeb87c28b72019-03-01 15:38:05 +01001146 reg_iflag_tx = flexcan_read_reg_iflag_tx(priv);
Alexander Steincbffaf72018-10-11 17:01:25 +02001147
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001148 /* transmission complete interrupt */
Marc Kleine-Budde0ca64f022019-03-01 13:54:19 +01001149 if (reg_iflag_tx & priv->tx_mask) {
Pankaj Bansal05179612018-11-23 22:18:44 +01001150 u32 reg_ctrl = priv->read(&priv->tx_mb->can_ctrl);
Oleksij Rempeled72bc82018-09-18 11:40:39 +02001151
Marc Kleine-Buddedd2f1222017-01-18 11:45:14 +01001152 handled = IRQ_HANDLED;
Marc Kleine-Budde99842c92021-01-11 15:19:29 +01001153 stats->tx_bytes +=
1154 can_rx_offload_get_echo_skb(&priv->offload, 0,
1155 reg_ctrl << 16, NULL);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001156 stats->tx_packets++;
Fabio Baltieriadccadb2012-12-18 18:50:58 +01001157 can_led_event(dev, CAN_LED_EVENT_TX);
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +02001158
1159 /* after sending a RTR frame MB is in RX mode */
Pankaj Bansal88462d22017-11-24 18:52:08 +05301160 priv->write(FLEXCAN_MB_CODE_TX_INACTIVE,
Pankaj Bansal05179612018-11-23 22:18:44 +01001161 &priv->tx_mb->can_ctrl);
Marc Kleine-Buddeb87c28b72019-03-01 15:38:05 +01001162 flexcan_write64(priv, priv->tx_mask, &regs->iflag1);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001163 netif_wake_queue(dev);
1164 }
1165
Pankaj Bansal88462d22017-11-24 18:52:08 +05301166 reg_esr = priv->read(&regs->esr);
Marc Kleine-Budde30164752015-05-10 15:26:58 +02001167
Joakim Zhangab605232019-12-04 11:36:08 +00001168 /* ACK all bus error, state change and wake IRQ sources */
1169 if (reg_esr & (FLEXCAN_ESR_ALL_INT | FLEXCAN_ESR_WAK_INT)) {
Marc Kleine-Buddedd2f1222017-01-18 11:45:14 +01001170 handled = IRQ_HANDLED;
Joakim Zhangab605232019-12-04 11:36:08 +00001171 priv->write(reg_esr & (FLEXCAN_ESR_ALL_INT | FLEXCAN_ESR_WAK_INT), &regs->esr);
Marc Kleine-Buddedd2f1222017-01-18 11:45:14 +01001172 }
1173
ZHU Yi (ST-FIR/ENG1-Zhu)ad230232017-09-15 06:59:15 +00001174 /* state change interrupt or broken error state quirk fix is enabled */
1175 if ((reg_esr & FLEXCAN_ESR_ERR_STATE) ||
ZHU Yi (ST-FIR/ENG1-Zhu)da49a802017-09-15 07:03:58 +00001176 (priv->devtype_data->quirks & (FLEXCAN_QUIRK_BROKEN_WERR_STATE |
Marc Kleine-Buddebc8ad652018-11-28 15:45:27 +01001177 FLEXCAN_QUIRK_BROKEN_PERR_STATE)))
Marc Kleine-Budde30164752015-05-10 15:26:58 +02001178 flexcan_irq_state(dev, reg_esr);
1179
1180 /* bus error IRQ - handle if bus error reporting is activated */
1181 if ((reg_esr & FLEXCAN_ESR_ERR_BUS) &&
1182 (priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING))
1183 flexcan_irq_bus_err(dev, reg_esr);
1184
ZHU Yi (ST-FIR/ENG1-Zhu)da49a802017-09-15 07:03:58 +00001185 /* availability of error interrupt among state transitions in case
1186 * bus error reporting is de-activated and
1187 * FLEXCAN_QUIRK_BROKEN_PERR_STATE is enabled:
1188 * +--------------------------------------------------------------+
1189 * | +----------------------------------------------+ [stopped / |
1190 * | | | sleeping] -+
1191 * +-+-> active <-> warning <-> passive -> bus off -+
1192 * ___________^^^^^^^^^^^^_______________________________
1193 * disabled(1) enabled disabled
1194 *
1195 * (1): enabled if FLEXCAN_QUIRK_BROKEN_WERR_STATE is enabled
1196 */
1197 if ((last_state != priv->can.state) &&
1198 (priv->devtype_data->quirks & FLEXCAN_QUIRK_BROKEN_PERR_STATE) &&
1199 !(priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING)) {
1200 switch (priv->can.state) {
1201 case CAN_STATE_ERROR_ACTIVE:
1202 if (priv->devtype_data->quirks &
1203 FLEXCAN_QUIRK_BROKEN_WERR_STATE)
1204 flexcan_error_irq_enable(priv);
1205 else
1206 flexcan_error_irq_disable(priv);
1207 break;
1208
1209 case CAN_STATE_ERROR_WARNING:
1210 flexcan_error_irq_enable(priv);
1211 break;
1212
1213 case CAN_STATE_ERROR_PASSIVE:
1214 case CAN_STATE_BUS_OFF:
1215 flexcan_error_irq_disable(priv);
1216 break;
1217
1218 default:
1219 break;
1220 }
1221 }
1222
Marc Kleine-Buddec7570962019-10-09 06:41:08 +02001223 if (handled)
1224 can_rx_offload_irq_finish(&priv->offload);
1225
Marc Kleine-Buddedd2f1222017-01-18 11:45:14 +01001226 return handled;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001227}
1228
Marc Kleine-Budde890599b2020-09-22 16:44:22 +02001229static void flexcan_set_bittiming_ctrl(const struct net_device *dev)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001230{
1231 const struct flexcan_priv *priv = netdev_priv(dev);
1232 const struct can_bittiming *bt = &priv->can.bittiming;
Marc Kleine-Budde89af8742015-05-08 09:32:58 +02001233 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001234 u32 reg;
1235
Pankaj Bansal88462d22017-11-24 18:52:08 +05301236 reg = priv->read(&regs->ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001237 reg &= ~(FLEXCAN_CTRL_PRESDIV(0xff) |
1238 FLEXCAN_CTRL_RJW(0x3) |
1239 FLEXCAN_CTRL_PSEG1(0x7) |
1240 FLEXCAN_CTRL_PSEG2(0x7) |
Marc Kleine-Budde890599b2020-09-22 16:44:22 +02001241 FLEXCAN_CTRL_PROPSEG(0x7));
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001242
1243 reg |= FLEXCAN_CTRL_PRESDIV(bt->brp - 1) |
1244 FLEXCAN_CTRL_PSEG1(bt->phase_seg1 - 1) |
1245 FLEXCAN_CTRL_PSEG2(bt->phase_seg2 - 1) |
1246 FLEXCAN_CTRL_RJW(bt->sjw - 1) |
1247 FLEXCAN_CTRL_PROPSEG(bt->prop_seg - 1);
1248
Marc Kleine-Budde890599b2020-09-22 16:44:22 +02001249 netdev_dbg(dev, "writing ctrl=0x%08x\n", reg);
1250 priv->write(reg, &regs->ctrl);
1251
1252 /* print chip status */
1253 netdev_dbg(dev, "%s: mcr=0x%08x ctrl=0x%08x\n", __func__,
1254 priv->read(&regs->mcr), priv->read(&regs->ctrl));
1255}
1256
Joakim Zhangeadf6ca2019-07-12 08:02:44 +00001257static void flexcan_set_bittiming_cbt(const struct net_device *dev)
1258{
1259 struct flexcan_priv *priv = netdev_priv(dev);
1260 struct can_bittiming *bt = &priv->can.bittiming;
1261 struct can_bittiming *dbt = &priv->can.data_bittiming;
1262 struct flexcan_regs __iomem *regs = priv->regs;
1263 u32 reg_cbt, reg_fdctrl;
1264
1265 /* CBT */
1266 /* CBT[EPSEG1] is 5 bit long and CBT[EPROPSEG] is 6 bit
1267 * long. The can_calc_bittiming() tries to divide the tseg1
1268 * equally between phase_seg1 and prop_seg, which may not fit
1269 * in CBT register. Therefore, if phase_seg1 is more than
1270 * possible value, increase prop_seg and decrease phase_seg1.
1271 */
1272 if (bt->phase_seg1 > 0x20) {
1273 bt->prop_seg += (bt->phase_seg1 - 0x20);
1274 bt->phase_seg1 = 0x20;
1275 }
1276
1277 reg_cbt = FLEXCAN_CBT_BTF |
1278 FIELD_PREP(FLEXCAN_CBT_EPRESDIV_MASK, bt->brp - 1) |
1279 FIELD_PREP(FLEXCAN_CBT_ERJW_MASK, bt->sjw - 1) |
1280 FIELD_PREP(FLEXCAN_CBT_EPROPSEG_MASK, bt->prop_seg - 1) |
1281 FIELD_PREP(FLEXCAN_CBT_EPSEG1_MASK, bt->phase_seg1 - 1) |
1282 FIELD_PREP(FLEXCAN_CBT_EPSEG2_MASK, bt->phase_seg2 - 1);
1283
1284 netdev_dbg(dev, "writing cbt=0x%08x\n", reg_cbt);
1285 priv->write(reg_cbt, &regs->cbt);
1286
1287 if (priv->can.ctrlmode & CAN_CTRLMODE_FD) {
Joakim Zhangce885a12019-07-12 08:02:51 +00001288 u32 reg_fdcbt, reg_ctrl2;
Joakim Zhangeadf6ca2019-07-12 08:02:44 +00001289
1290 if (bt->brp != dbt->brp)
1291 netdev_warn(dev, "Data brp=%d and brp=%d don't match, this may result in a phase error. Consider using different bitrate and/or data bitrate.\n",
1292 dbt->brp, bt->brp);
1293
1294 /* FDCBT */
1295 /* FDCBT[FPSEG1] is 3 bit long and FDCBT[FPROPSEG] is
1296 * 5 bit long. The can_calc_bittiming tries to divide
1297 * the tseg1 equally between phase_seg1 and prop_seg,
1298 * which may not fit in FDCBT register. Therefore, if
1299 * phase_seg1 is more than possible value, increase
1300 * prop_seg and decrease phase_seg1
1301 */
1302 if (dbt->phase_seg1 > 0x8) {
1303 dbt->prop_seg += (dbt->phase_seg1 - 0x8);
1304 dbt->phase_seg1 = 0x8;
1305 }
1306
Joakim Zhangce885a12019-07-12 08:02:51 +00001307 reg_fdcbt = priv->read(&regs->fdcbt);
1308 reg_fdcbt &= ~(FIELD_PREP(FLEXCAN_FDCBT_FPRESDIV_MASK, 0x3ff) |
1309 FIELD_PREP(FLEXCAN_FDCBT_FRJW_MASK, 0x7) |
1310 FIELD_PREP(FLEXCAN_FDCBT_FPROPSEG_MASK, 0x1f) |
1311 FIELD_PREP(FLEXCAN_FDCBT_FPSEG1_MASK, 0x7) |
1312 FIELD_PREP(FLEXCAN_FDCBT_FPSEG2_MASK, 0x7));
1313
1314 reg_fdcbt |= FIELD_PREP(FLEXCAN_FDCBT_FPRESDIV_MASK, dbt->brp - 1) |
Joakim Zhangeadf6ca2019-07-12 08:02:44 +00001315 FIELD_PREP(FLEXCAN_FDCBT_FRJW_MASK, dbt->sjw - 1) |
1316 FIELD_PREP(FLEXCAN_FDCBT_FPROPSEG_MASK, dbt->prop_seg) |
1317 FIELD_PREP(FLEXCAN_FDCBT_FPSEG1_MASK, dbt->phase_seg1 - 1) |
1318 FIELD_PREP(FLEXCAN_FDCBT_FPSEG2_MASK, dbt->phase_seg2 - 1);
1319
1320 netdev_dbg(dev, "writing fdcbt=0x%08x\n", reg_fdcbt);
1321 priv->write(reg_fdcbt, &regs->fdcbt);
Joakim Zhangce885a12019-07-12 08:02:51 +00001322
1323 /* CTRL2 */
1324 reg_ctrl2 = priv->read(&regs->ctrl2);
1325 reg_ctrl2 &= ~FLEXCAN_CTRL2_ISOCANFDEN;
1326 if (!(priv->can.ctrlmode & CAN_CTRLMODE_FD_NON_ISO))
1327 reg_ctrl2 |= FLEXCAN_CTRL2_ISOCANFDEN;
1328
1329 netdev_dbg(dev, "writing ctrl2=0x%08x\n", reg_ctrl2);
1330 priv->write(reg_ctrl2, &regs->ctrl2);
Joakim Zhangeadf6ca2019-07-12 08:02:44 +00001331 }
1332
1333 /* FDCTRL */
1334 reg_fdctrl = priv->read(&regs->fdctrl);
Joakim Zhangef5f6312019-07-12 08:02:56 +00001335 reg_fdctrl &= ~(FLEXCAN_FDCTRL_FDRATE |
1336 FIELD_PREP(FLEXCAN_FDCTRL_TDCOFF, 0x1f));
Joakim Zhangeadf6ca2019-07-12 08:02:44 +00001337
Joakim Zhangef5f6312019-07-12 08:02:56 +00001338 if (priv->can.ctrlmode & CAN_CTRLMODE_FD) {
Joakim Zhangeadf6ca2019-07-12 08:02:44 +00001339 reg_fdctrl |= FLEXCAN_FDCTRL_FDRATE;
1340
Joakim Zhangef5f6312019-07-12 08:02:56 +00001341 if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK) {
1342 /* TDC must be disabled for Loop Back mode */
1343 reg_fdctrl &= ~FLEXCAN_FDCTRL_TDCEN;
1344 } else {
1345 reg_fdctrl |= FLEXCAN_FDCTRL_TDCEN |
1346 FIELD_PREP(FLEXCAN_FDCTRL_TDCOFF,
1347 ((dbt->phase_seg1 - 1) +
1348 dbt->prop_seg + 2) *
1349 ((dbt->brp - 1 ) + 1));
1350 }
1351 }
1352
Joakim Zhangeadf6ca2019-07-12 08:02:44 +00001353 netdev_dbg(dev, "writing fdctrl=0x%08x\n", reg_fdctrl);
1354 priv->write(reg_fdctrl, &regs->fdctrl);
1355
Joakim Zhangce885a12019-07-12 08:02:51 +00001356 netdev_dbg(dev, "%s: mcr=0x%08x ctrl=0x%08x ctrl2=0x%08x fdctrl=0x%08x cbt=0x%08x fdcbt=0x%08x\n",
Joakim Zhangeadf6ca2019-07-12 08:02:44 +00001357 __func__,
1358 priv->read(&regs->mcr), priv->read(&regs->ctrl),
Joakim Zhangce885a12019-07-12 08:02:51 +00001359 priv->read(&regs->ctrl2), priv->read(&regs->fdctrl),
1360 priv->read(&regs->cbt), priv->read(&regs->fdcbt));
Joakim Zhangeadf6ca2019-07-12 08:02:44 +00001361}
1362
Marc Kleine-Budde890599b2020-09-22 16:44:22 +02001363static void flexcan_set_bittiming(struct net_device *dev)
1364{
1365 const struct flexcan_priv *priv = netdev_priv(dev);
1366 struct flexcan_regs __iomem *regs = priv->regs;
1367 u32 reg;
1368
1369 reg = priv->read(&regs->ctrl);
1370 reg &= ~(FLEXCAN_CTRL_LPB | FLEXCAN_CTRL_SMP |
1371 FLEXCAN_CTRL_LOM);
1372
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001373 if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)
1374 reg |= FLEXCAN_CTRL_LPB;
1375 if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
1376 reg |= FLEXCAN_CTRL_LOM;
1377 if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
1378 reg |= FLEXCAN_CTRL_SMP;
1379
Lucas Stach7a4b6c82015-08-07 17:16:03 +02001380 netdev_dbg(dev, "writing ctrl=0x%08x\n", reg);
Pankaj Bansal88462d22017-11-24 18:52:08 +05301381 priv->write(reg, &regs->ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001382
Joakim Zhangeadf6ca2019-07-12 08:02:44 +00001383 if (priv->can.ctrlmode_supported & CAN_CTRLMODE_FD)
1384 return flexcan_set_bittiming_cbt(dev);
1385 else
1386 return flexcan_set_bittiming_ctrl(dev);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001387}
1388
Joakim Zhanga6597122020-09-30 05:15:55 +08001389static void flexcan_ram_init(struct net_device *dev)
1390{
1391 struct flexcan_priv *priv = netdev_priv(dev);
1392 struct flexcan_regs __iomem *regs = priv->regs;
1393 u32 reg_ctrl2;
1394
1395 /* 11.8.3.13 Detection and correction of memory errors:
1396 * CTRL2[WRMFRZ] grants write access to all memory positions
1397 * that require initialization, ranging from 0x080 to 0xADF
1398 * and from 0xF28 to 0xFFF when the CAN FD feature is enabled.
1399 * The RXMGMASK, RX14MASK, RX15MASK, and RXFGMASK registers
1400 * need to be initialized as well. MCR[RFEN] must not be set
1401 * during memory initialization.
1402 */
1403 reg_ctrl2 = priv->read(&regs->ctrl2);
1404 reg_ctrl2 |= FLEXCAN_CTRL2_WRMFRZ;
1405 priv->write(reg_ctrl2, &regs->ctrl2);
1406
Kees Cookc92a08c2021-07-31 17:50:58 -07001407 memset_io(&regs->init, 0, sizeof(regs->init));
Joakim Zhanga6597122020-09-30 05:15:55 +08001408
1409 if (priv->can.ctrlmode & CAN_CTRLMODE_FD)
Kees Cookc92a08c2021-07-31 17:50:58 -07001410 memset_io(&regs->init_fd, 0, sizeof(regs->init_fd));
Joakim Zhanga6597122020-09-30 05:15:55 +08001411
1412 reg_ctrl2 &= ~FLEXCAN_CTRL2_WRMFRZ;
1413 priv->write(reg_ctrl2, &regs->ctrl2);
1414}
1415
Marc Kleine-Buddef3f2a542020-11-19 11:09:15 +01001416static int flexcan_rx_offload_setup(struct net_device *dev)
1417{
1418 struct flexcan_priv *priv = netdev_priv(dev);
1419 int err;
1420
1421 if (priv->can.ctrlmode & CAN_CTRLMODE_FD)
1422 priv->mb_size = sizeof(struct flexcan_mb) + CANFD_MAX_DLEN;
1423 else
1424 priv->mb_size = sizeof(struct flexcan_mb) + CAN_MAX_DLEN;
Angelo Dureghellod9cead72021-07-02 11:48:41 +02001425
1426 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_NR_MB_16)
1427 priv->mb_count = 16;
1428 else
1429 priv->mb_count = (sizeof(priv->regs->mb[0]) / priv->mb_size) +
1430 (sizeof(priv->regs->mb[1]) / priv->mb_size);
Marc Kleine-Buddef3f2a542020-11-19 11:09:15 +01001431
1432 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP)
1433 priv->tx_mb_reserved =
1434 flexcan_get_mb(priv, FLEXCAN_TX_MB_RESERVED_OFF_TIMESTAMP);
1435 else
1436 priv->tx_mb_reserved =
1437 flexcan_get_mb(priv, FLEXCAN_TX_MB_RESERVED_OFF_FIFO);
1438 priv->tx_mb_idx = priv->mb_count - 1;
1439 priv->tx_mb = flexcan_get_mb(priv, priv->tx_mb_idx);
1440 priv->tx_mask = FLEXCAN_IFLAG_MB(priv->tx_mb_idx);
1441
1442 priv->offload.mailbox_read = flexcan_mailbox_read;
1443
1444 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP) {
1445 priv->offload.mb_first = FLEXCAN_RX_MB_OFF_TIMESTAMP_FIRST;
1446 priv->offload.mb_last = priv->mb_count - 2;
1447
1448 priv->rx_mask = GENMASK_ULL(priv->offload.mb_last,
1449 priv->offload.mb_first);
1450 err = can_rx_offload_add_timestamp(dev, &priv->offload);
1451 } else {
1452 priv->rx_mask = FLEXCAN_IFLAG_RX_FIFO_OVERFLOW |
1453 FLEXCAN_IFLAG_RX_FIFO_AVAILABLE;
1454 err = can_rx_offload_add_fifo(dev, &priv->offload,
1455 FLEXCAN_NAPI_WEIGHT);
1456 }
1457
1458 return err;
1459}
1460
Marc Kleine-Budde1c5e6db2020-11-19 11:09:13 +01001461static void flexcan_chip_interrupts_enable(const struct net_device *dev)
1462{
1463 const struct flexcan_priv *priv = netdev_priv(dev);
1464 struct flexcan_regs __iomem *regs = priv->regs;
1465 u64 reg_imask;
1466
1467 disable_irq(dev->irq);
1468 priv->write(priv->reg_ctrl_default, &regs->ctrl);
1469 reg_imask = priv->rx_mask | priv->tx_mask;
1470 priv->write(upper_32_bits(reg_imask), &regs->imask2);
1471 priv->write(lower_32_bits(reg_imask), &regs->imask1);
1472 enable_irq(dev->irq);
1473}
1474
1475static void flexcan_chip_interrupts_disable(const struct net_device *dev)
1476{
1477 const struct flexcan_priv *priv = netdev_priv(dev);
1478 struct flexcan_regs __iomem *regs = priv->regs;
1479
1480 priv->write(0, &regs->imask2);
1481 priv->write(0, &regs->imask1);
1482 priv->write(priv->reg_ctrl_default & ~FLEXCAN_CTRL_ERR_ALL,
1483 &regs->ctrl);
1484}
1485
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +02001486/* flexcan_chip_start
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001487 *
1488 * this functions is entered with clocks enabled
1489 *
1490 */
1491static int flexcan_chip_start(struct net_device *dev)
1492{
1493 struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Budde89af8742015-05-08 09:32:58 +02001494 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Budde6f75fce2014-09-23 11:03:01 +02001495 u32 reg_mcr, reg_ctrl, reg_ctrl2, reg_mecr;
David S. Miller1f6d8032014-09-23 12:09:27 -04001496 int err, i;
Pankaj Bansal05179612018-11-23 22:18:44 +01001497 struct flexcan_mb __iomem *mb;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001498
1499 /* enable module */
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001500 err = flexcan_chip_enable(priv);
1501 if (err)
1502 return err;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001503
1504 /* soft reset */
Marc Kleine-Budde4b5b8222014-02-28 15:16:59 +01001505 err = flexcan_chip_softreset(priv);
1506 if (err)
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +01001507 goto out_chip_disable;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001508
Joakim Zhanga6597122020-09-30 05:15:55 +08001509 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_SUPPORT_ECC)
1510 flexcan_ram_init(dev);
1511
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001512 flexcan_set_bittiming(dev);
1513
Joakim Zhangc6382002021-02-18 19:00:37 +08001514 /* set freeze, halt */
1515 err = flexcan_chip_freeze(priv);
1516 if (err)
1517 goto out_chip_disable;
1518
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +02001519 /* MCR
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001520 *
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001521 * only supervisor access
1522 * enable warning int
Marc Kleine-Budde4bd888a2015-08-31 21:03:29 +02001523 * enable individual RX masking
Marc Kleine-Budde749de6f2015-08-31 21:32:34 +02001524 * choose format C
1525 * set max mailbox number
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001526 */
Pankaj Bansal88462d22017-11-24 18:52:08 +05301527 reg_mcr = priv->read(&regs->mcr);
Marc Kleine-Budded5a7b402013-10-04 10:52:36 +02001528 reg_mcr &= ~FLEXCAN_MCR_MAXMB(0xff);
Joakim Zhangc6382002021-02-18 19:00:37 +08001529 reg_mcr |= FLEXCAN_MCR_SUPV | FLEXCAN_MCR_WRN_EN | FLEXCAN_MCR_IRMQ |
1530 FLEXCAN_MCR_IDAM_C | FLEXCAN_MCR_MAXMB(priv->tx_mb_idx);
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +02001531
Marc Kleine-Buddec982a3ca2018-08-17 14:52:58 +02001532 /* MCR
1533 *
1534 * FIFO:
1535 * - disable for timestamp mode
1536 * - enable for FIFO mode
1537 */
Alexander Steincbffaf72018-10-11 17:01:25 +02001538 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP)
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +02001539 reg_mcr &= ~FLEXCAN_MCR_FEN;
Alexander Steincbffaf72018-10-11 17:01:25 +02001540 else
1541 reg_mcr |= FLEXCAN_MCR_FEN;
1542
Pankaj Bansal7ad0f532018-08-13 23:50:48 +05301543 /* MCR
1544 *
1545 * NOTE: In loopback mode, the CAN_MCR[SRXDIS] cannot be
1546 * asserted because this will impede the self reception
1547 * of a transmitted message. This is not documented in
1548 * earlier versions of flexcan block guide.
1549 *
1550 * Self Reception:
1551 * - enable Self Reception for loopback mode
1552 * (by clearing "Self Reception Disable" bit)
1553 * - disable for normal operation
1554 */
1555 if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)
1556 reg_mcr &= ~FLEXCAN_MCR_SRX_DIS;
1557 else
1558 reg_mcr |= FLEXCAN_MCR_SRX_DIS;
1559
Joakim Zhangeadf6ca2019-07-12 08:02:44 +00001560 /* MCR - CAN-FD */
1561 if (priv->can.ctrlmode & CAN_CTRLMODE_FD)
1562 reg_mcr |= FLEXCAN_MCR_FDEN;
1563 else
1564 reg_mcr &= ~FLEXCAN_MCR_FDEN;
1565
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +01001566 netdev_dbg(dev, "%s: writing mcr=0x%08x", __func__, reg_mcr);
Pankaj Bansal88462d22017-11-24 18:52:08 +05301567 priv->write(reg_mcr, &regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001568
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +02001569 /* CTRL
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001570 *
1571 * disable timer sync feature
1572 *
1573 * disable auto busoff recovery
1574 * transmit lowest buffer first
1575 *
1576 * enable tx and rx warning interrupt
1577 * enable bus off interrupt
1578 * (== FLEXCAN_CTRL_ERR_STATE)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001579 */
Pankaj Bansal88462d22017-11-24 18:52:08 +05301580 reg_ctrl = priv->read(&regs->ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001581 reg_ctrl &= ~FLEXCAN_CTRL_TSYN;
1582 reg_ctrl |= FLEXCAN_CTRL_BOFF_REC | FLEXCAN_CTRL_LBUF |
Wolfgang Grandegger4f72e5f2012-09-28 03:17:15 +00001583 FLEXCAN_CTRL_ERR_STATE;
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +02001584
1585 /* enable the "error interrupt" (FLEXCAN_CTRL_ERR_MSK),
Wolfgang Grandegger4f72e5f2012-09-28 03:17:15 +00001586 * on most Flexcan cores, too. Otherwise we don't get
1587 * any error warning or passive interrupts.
1588 */
ZHU Yi (ST-FIR/ENG1-Zhu)2f8639b2017-09-15 07:01:23 +00001589 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_BROKEN_WERR_STATE ||
Wolfgang Grandegger4f72e5f2012-09-28 03:17:15 +00001590 priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING)
1591 reg_ctrl |= FLEXCAN_CTRL_ERR_MSK;
Alexander Steinbc03a542014-08-12 10:47:21 +02001592 else
1593 reg_ctrl &= ~FLEXCAN_CTRL_ERR_MSK;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001594
1595 /* save for later use */
1596 priv->reg_ctrl_default = reg_ctrl;
Marc Kleine-Budde6fa7da22015-08-27 14:24:48 +02001597 /* leave interrupts disabled for now */
1598 reg_ctrl &= ~FLEXCAN_CTRL_ERR_ALL;
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +01001599 netdev_dbg(dev, "%s: writing ctrl=0x%08x", __func__, reg_ctrl);
Pankaj Bansal88462d22017-11-24 18:52:08 +05301600 priv->write(reg_ctrl, &regs->ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001601
Marc Kleine-Budde9eb7aa82015-09-01 08:57:55 +02001602 if ((priv->devtype_data->quirks & FLEXCAN_QUIRK_ENABLE_EACEN_RRS)) {
Pankaj Bansal88462d22017-11-24 18:52:08 +05301603 reg_ctrl2 = priv->read(&regs->ctrl2);
Marc Kleine-Budde9eb7aa82015-09-01 08:57:55 +02001604 reg_ctrl2 |= FLEXCAN_CTRL2_EACEN | FLEXCAN_CTRL2_RRS;
Pankaj Bansal88462d22017-11-24 18:52:08 +05301605 priv->write(reg_ctrl2, &regs->ctrl2);
Marc Kleine-Budde9eb7aa82015-09-01 08:57:55 +02001606 }
1607
Joakim Zhangeadf6ca2019-07-12 08:02:44 +00001608 if (priv->can.ctrlmode_supported & CAN_CTRLMODE_FD) {
1609 u32 reg_fdctrl;
1610
1611 reg_fdctrl = priv->read(&regs->fdctrl);
1612 reg_fdctrl &= ~(FIELD_PREP(FLEXCAN_FDCTRL_MBDSR1, 0x3) |
1613 FIELD_PREP(FLEXCAN_FDCTRL_MBDSR0, 0x3));
1614
1615 if (priv->can.ctrlmode & CAN_CTRLMODE_FD) {
1616 reg_fdctrl |=
1617 FIELD_PREP(FLEXCAN_FDCTRL_MBDSR1,
1618 FLEXCAN_FDCTRL_MBDSR_64) |
1619 FIELD_PREP(FLEXCAN_FDCTRL_MBDSR0,
1620 FLEXCAN_FDCTRL_MBDSR_64);
1621 } else {
1622 reg_fdctrl |=
1623 FIELD_PREP(FLEXCAN_FDCTRL_MBDSR1,
1624 FLEXCAN_FDCTRL_MBDSR_8) |
1625 FIELD_PREP(FLEXCAN_FDCTRL_MBDSR0,
1626 FLEXCAN_FDCTRL_MBDSR_8);
1627 }
1628
1629 netdev_dbg(dev, "%s: writing fdctrl=0x%08x",
1630 __func__, reg_fdctrl);
1631 priv->write(reg_fdctrl, &regs->fdctrl);
1632 }
1633
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +02001634 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP) {
Alexander Steincbffaf72018-10-11 17:01:25 +02001635 for (i = priv->offload.mb_first; i <= priv->offload.mb_last; i++) {
Pankaj Bansal05179612018-11-23 22:18:44 +01001636 mb = flexcan_get_mb(priv, i);
Pankaj Bansal88462d22017-11-24 18:52:08 +05301637 priv->write(FLEXCAN_MB_CODE_RX_EMPTY,
Pankaj Bansal05179612018-11-23 22:18:44 +01001638 &mb->can_ctrl);
Alexander Steincbffaf72018-10-11 17:01:25 +02001639 }
1640 } else {
1641 /* clear and invalidate unused mailboxes first */
Uwe Kleine-Königa55234d2019-01-11 12:20:41 +01001642 for (i = FLEXCAN_TX_MB_RESERVED_OFF_FIFO; i < priv->mb_count; i++) {
Pankaj Bansal05179612018-11-23 22:18:44 +01001643 mb = flexcan_get_mb(priv, i);
Alexander Steincbffaf72018-10-11 17:01:25 +02001644 priv->write(FLEXCAN_MB_CODE_RX_INACTIVE,
Pankaj Bansal05179612018-11-23 22:18:44 +01001645 &mb->can_ctrl);
Alexander Steincbffaf72018-10-11 17:01:25 +02001646 }
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +02001647 }
1648
David Jander25e92442014-09-03 16:47:22 +02001649 /* Errata ERR005829: mark first TX mailbox as INACTIVE */
Pankaj Bansal88462d22017-11-24 18:52:08 +05301650 priv->write(FLEXCAN_MB_CODE_TX_INACTIVE,
1651 &priv->tx_mb_reserved->can_ctrl);
David Jander25e92442014-09-03 16:47:22 +02001652
Marc Kleine-Buddec32fe4a2014-09-16 12:39:28 +02001653 /* mark TX mailbox as INACTIVE */
Pankaj Bansal88462d22017-11-24 18:52:08 +05301654 priv->write(FLEXCAN_MB_CODE_TX_INACTIVE,
Pankaj Bansal05179612018-11-23 22:18:44 +01001655 &priv->tx_mb->can_ctrl);
Marc Kleine-Budded5a7b402013-10-04 10:52:36 +02001656
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001657 /* acceptance mask/acceptance code (accept everything) */
Pankaj Bansal88462d22017-11-24 18:52:08 +05301658 priv->write(0x0, &regs->rxgmask);
1659 priv->write(0x0, &regs->rx14mask);
1660 priv->write(0x0, &regs->rx15mask);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001661
Marc Kleine-Buddef377bff2015-05-08 15:22:36 +02001662 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_DISABLE_RXFG)
Pankaj Bansal88462d22017-11-24 18:52:08 +05301663 priv->write(0x0, &regs->rxfgmask);
Hui Wang30c1e672012-06-28 16:21:35 +08001664
Marc Kleine-Budde4bd888a2015-08-31 21:03:29 +02001665 /* clear acceptance filters */
Pankaj Bansal05179612018-11-23 22:18:44 +01001666 for (i = 0; i < priv->mb_count; i++)
Pankaj Bansal88462d22017-11-24 18:52:08 +05301667 priv->write(0, &regs->rximr[i]);
Marc Kleine-Budde4bd888a2015-08-31 21:03:29 +02001668
Joakim Zhang15ef2072020-04-16 17:31:25 +08001669 /* On Vybrid, disable non-correctable errors interrupt and
1670 * freeze mode. It still can correct the correctable errors
1671 * when HW supports ECC.
1672 *
1673 * This also works around errata e5295 which generates false
1674 * positive memory errors and put the device in freeze mode.
Stefan Agnercdce8442014-07-15 14:56:21 +02001675 */
Marc Kleine-Buddef377bff2015-05-08 15:22:36 +02001676 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_DISABLE_MECR) {
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +02001677 /* Follow the protocol as described in "Detection
Stefan Agnercdce8442014-07-15 14:56:21 +02001678 * and Correction of Memory Errors" to write to
Joakim Zhang15ef2072020-04-16 17:31:25 +08001679 * MECR register (step 1 - 5)
1680 *
1681 * 1. By default, CTRL2[ECRWRE] = 0, MECR[ECRWRDIS] = 1
1682 * 2. set CTRL2[ECRWRE]
Stefan Agnercdce8442014-07-15 14:56:21 +02001683 */
Pankaj Bansal88462d22017-11-24 18:52:08 +05301684 reg_ctrl2 = priv->read(&regs->ctrl2);
Marc Kleine-Budde6f75fce2014-09-23 11:03:01 +02001685 reg_ctrl2 |= FLEXCAN_CTRL2_ECRWRE;
Pankaj Bansal88462d22017-11-24 18:52:08 +05301686 priv->write(reg_ctrl2, &regs->ctrl2);
Stefan Agnercdce8442014-07-15 14:56:21 +02001687
Joakim Zhang15ef2072020-04-16 17:31:25 +08001688 /* 3. clear MECR[ECRWRDIS] */
Pankaj Bansal88462d22017-11-24 18:52:08 +05301689 reg_mecr = priv->read(&regs->mecr);
Stefan Agnercdce8442014-07-15 14:56:21 +02001690 reg_mecr &= ~FLEXCAN_MECR_ECRWRDIS;
Pankaj Bansal88462d22017-11-24 18:52:08 +05301691 priv->write(reg_mecr, &regs->mecr);
Joakim Zhang15ef2072020-04-16 17:31:25 +08001692
1693 /* 4. all writes to MECR must keep MECR[ECRWRDIS] cleared */
Stefan Agnercdce8442014-07-15 14:56:21 +02001694 reg_mecr &= ~(FLEXCAN_MECR_NCEFAFRZ | FLEXCAN_MECR_HANCEI_MSK |
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +02001695 FLEXCAN_MECR_FANCEI_MSK);
Pankaj Bansal88462d22017-11-24 18:52:08 +05301696 priv->write(reg_mecr, &regs->mecr);
Joakim Zhang15ef2072020-04-16 17:31:25 +08001697
1698 /* 5. after configuration done, lock MECR by either
1699 * setting MECR[ECRWRDIS] or clearing CTRL2[ECRWRE]
1700 */
1701 reg_mecr |= FLEXCAN_MECR_ECRWRDIS;
1702 priv->write(reg_mecr, &regs->mecr);
1703
1704 reg_ctrl2 &= ~FLEXCAN_CTRL2_ECRWRE;
1705 priv->write(reg_ctrl2, &regs->ctrl2);
Stefan Agnercdce8442014-07-15 14:56:21 +02001706 }
1707
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001708 /* synchronize with the can bus */
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +01001709 err = flexcan_chip_unfreeze(priv);
1710 if (err)
Marc Kleine-Buddecd9f13c2020-11-18 16:01:48 +01001711 goto out_chip_disable;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001712
1713 priv->can.state = CAN_STATE_ERROR_ACTIVE;
1714
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001715 /* print chip status */
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +01001716 netdev_dbg(dev, "%s: reading mcr=0x%08x ctrl=0x%08x\n", __func__,
Pankaj Bansal88462d22017-11-24 18:52:08 +05301717 priv->read(&regs->mcr), priv->read(&regs->ctrl));
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001718
1719 return 0;
1720
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +01001721 out_chip_disable:
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001722 flexcan_chip_disable(priv);
1723 return err;
1724}
1725
Joakim Zhang9ad02c72020-09-22 16:44:19 +02001726/* __flexcan_chip_stop
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001727 *
Joakim Zhang9ad02c72020-09-22 16:44:19 +02001728 * this function is entered with clocks enabled
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001729 */
Joakim Zhang9ad02c72020-09-22 16:44:19 +02001730static int __flexcan_chip_stop(struct net_device *dev, bool disable_on_error)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001731{
1732 struct flexcan_priv *priv = netdev_priv(dev);
Joakim Zhang9ad02c72020-09-22 16:44:19 +02001733 int err;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001734
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +01001735 /* freeze + disable module */
Joakim Zhang9ad02c72020-09-22 16:44:19 +02001736 err = flexcan_chip_freeze(priv);
1737 if (err && !disable_on_error)
1738 return err;
1739 err = flexcan_chip_disable(priv);
1740 if (err && !disable_on_error)
1741 goto out_chip_unfreeze;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001742
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001743 priv->can.state = CAN_STATE_STOPPED;
Joakim Zhang9ad02c72020-09-22 16:44:19 +02001744
1745 return 0;
1746
1747 out_chip_unfreeze:
1748 flexcan_chip_unfreeze(priv);
1749
1750 return err;
1751}
1752
1753static inline int flexcan_chip_stop_disable_on_error(struct net_device *dev)
1754{
1755 return __flexcan_chip_stop(dev, true);
1756}
1757
1758static inline int flexcan_chip_stop(struct net_device *dev)
1759{
1760 return __flexcan_chip_stop(dev, false);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001761}
1762
1763static int flexcan_open(struct net_device *dev)
1764{
1765 struct flexcan_priv *priv = netdev_priv(dev);
1766 int err;
1767
Joakim Zhangeadf6ca2019-07-12 08:02:44 +00001768 if ((priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES) &&
1769 (priv->can.ctrlmode & CAN_CTRLMODE_FD)) {
1770 netdev_err(dev, "Three Samples mode and CAN-FD mode can't be used together\n");
1771 return -EINVAL;
1772 }
1773
Aisheng Dongca109892018-11-30 08:53:26 +00001774 err = pm_runtime_get_sync(priv->dev);
Zhang Qilongb7ee5bc2020-11-08 16:30:00 +08001775 if (err < 0) {
1776 pm_runtime_put_noidle(priv->dev);
Fabio Estevamaa101812013-07-22 12:41:40 -03001777 return err;
Zhang Qilongb7ee5bc2020-11-08 16:30:00 +08001778 }
Fabio Estevamaa101812013-07-22 12:41:40 -03001779
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001780 err = open_candev(dev);
1781 if (err)
Aisheng Dongca109892018-11-30 08:53:26 +00001782 goto out_runtime_put;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001783
Marc Kleine-Buddecd9f13c2020-11-18 16:01:48 +01001784 err = flexcan_transceiver_enable(priv);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001785 if (err)
1786 goto out_close;
1787
Marc Kleine-Budde648a34b2020-11-19 11:09:16 +01001788 err = flexcan_rx_offload_setup(dev);
Marc Kleine-Buddecd9f13c2020-11-18 16:01:48 +01001789 if (err)
1790 goto out_transceiver_disable;
1791
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001792 err = flexcan_chip_start(dev);
1793 if (err)
Marc Kleine-Budde648a34b2020-11-19 11:09:16 +01001794 goto out_can_rx_offload_del;
1795
1796 can_rx_offload_enable(&priv->offload);
1797
1798 err = request_irq(dev->irq, flexcan_irq, IRQF_SHARED, dev->name, dev);
1799 if (err)
1800 goto out_can_rx_offload_disable;
Fabio Baltieriadccadb2012-12-18 18:50:58 +01001801
Angelo Dureghellod9cead72021-07-02 11:48:41 +02001802 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_NR_IRQ_3) {
1803 err = request_irq(priv->irq_boff,
1804 flexcan_irq, IRQF_SHARED, dev->name, dev);
1805 if (err)
1806 goto out_free_irq;
1807
1808 err = request_irq(priv->irq_err,
1809 flexcan_irq, IRQF_SHARED, dev->name, dev);
1810 if (err)
1811 goto out_free_irq_boff;
1812 }
1813
Marc Kleine-Budde49dea042020-11-19 11:09:14 +01001814 flexcan_chip_interrupts_enable(dev);
1815
Fabio Baltieriadccadb2012-12-18 18:50:58 +01001816 can_led_event(dev, CAN_LED_EVENT_OPEN);
1817
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001818 netif_start_queue(dev);
1819
1820 return 0;
1821
Angelo Dureghellod9cead72021-07-02 11:48:41 +02001822 out_free_irq_boff:
1823 free_irq(priv->irq_boff, dev);
1824 out_free_irq:
1825 free_irq(dev->irq, dev);
Marc Kleine-Budde648a34b2020-11-19 11:09:16 +01001826 out_can_rx_offload_disable:
1827 can_rx_offload_disable(&priv->offload);
1828 flexcan_chip_stop(dev);
1829 out_can_rx_offload_del:
Pankaj Bansal5156c7b2018-08-13 23:50:49 +05301830 can_rx_offload_del(&priv->offload);
Marc Kleine-Buddecd9f13c2020-11-18 16:01:48 +01001831 out_transceiver_disable:
1832 flexcan_transceiver_disable(priv);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001833 out_close:
1834 close_candev(dev);
Aisheng Dongca109892018-11-30 08:53:26 +00001835 out_runtime_put:
1836 pm_runtime_put(priv->dev);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001837
1838 return err;
1839}
1840
1841static int flexcan_close(struct net_device *dev)
1842{
1843 struct flexcan_priv *priv = netdev_priv(dev);
1844
1845 netif_stop_queue(dev);
Marc Kleine-Budde6b6e9862020-11-19 11:09:17 +01001846 flexcan_chip_interrupts_disable(dev);
Angelo Dureghellod9cead72021-07-02 11:48:41 +02001847
1848 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_NR_IRQ_3) {
1849 free_irq(priv->irq_err, dev);
1850 free_irq(priv->irq_boff, dev);
1851 }
1852
Marc Kleine-Budde6b6e9862020-11-19 11:09:17 +01001853 free_irq(dev->irq, dev);
Marc Kleine-Budde30164752015-05-10 15:26:58 +02001854 can_rx_offload_disable(&priv->offload);
Joakim Zhang9ad02c72020-09-22 16:44:19 +02001855 flexcan_chip_stop_disable_on_error(dev);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001856
Pankaj Bansal5156c7b2018-08-13 23:50:49 +05301857 can_rx_offload_del(&priv->offload);
Marc Kleine-Buddecd9f13c2020-11-18 16:01:48 +01001858 flexcan_transceiver_disable(priv);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001859 close_candev(dev);
Marc Kleine-Budde6b6e9862020-11-19 11:09:17 +01001860
Aisheng Dongca109892018-11-30 08:53:26 +00001861 pm_runtime_put(priv->dev);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001862
Fabio Baltieriadccadb2012-12-18 18:50:58 +01001863 can_led_event(dev, CAN_LED_EVENT_STOP);
1864
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001865 return 0;
1866}
1867
1868static int flexcan_set_mode(struct net_device *dev, enum can_mode mode)
1869{
1870 int err;
1871
1872 switch (mode) {
1873 case CAN_MODE_START:
1874 err = flexcan_chip_start(dev);
1875 if (err)
1876 return err;
1877
Marc Kleine-Budde49dea042020-11-19 11:09:14 +01001878 flexcan_chip_interrupts_enable(dev);
1879
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001880 netif_wake_queue(dev);
1881 break;
1882
1883 default:
1884 return -EOPNOTSUPP;
1885 }
1886
1887 return 0;
1888}
1889
1890static const struct net_device_ops flexcan_netdev_ops = {
1891 .ndo_open = flexcan_open,
1892 .ndo_stop = flexcan_close,
1893 .ndo_start_xmit = flexcan_start_xmit,
Oliver Hartkoppc971fa22014-03-07 09:23:41 +01001894 .ndo_change_mtu = can_change_mtu,
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001895};
1896
Bill Pemberton3c8ac0f2012-12-03 09:22:44 -05001897static int register_flexcandev(struct net_device *dev)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001898{
1899 struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Budde89af8742015-05-08 09:32:58 +02001900 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001901 u32 reg, err;
1902
Aisheng Dongca109892018-11-30 08:53:26 +00001903 err = flexcan_clks_enable(priv);
Fabio Estevamaa101812013-07-22 12:41:40 -03001904 if (err)
1905 return err;
1906
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001907 /* select "bus clock", chip must be disabled */
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001908 err = flexcan_chip_disable(priv);
1909 if (err)
Aisheng Dongca109892018-11-30 08:53:26 +00001910 goto out_clks_disable;
1911
Pankaj Bansal88462d22017-11-24 18:52:08 +05301912 reg = priv->read(&regs->ctrl);
Dong Aisheng8c306be2018-12-13 07:08:00 +00001913 if (priv->clk_src)
1914 reg |= FLEXCAN_CTRL_CLK_SRC;
1915 else
1916 reg &= ~FLEXCAN_CTRL_CLK_SRC;
Pankaj Bansal88462d22017-11-24 18:52:08 +05301917 priv->write(reg, &regs->ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001918
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001919 err = flexcan_chip_enable(priv);
1920 if (err)
1921 goto out_chip_disable;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001922
Joakim Zhangec15e272021-02-18 19:00:36 +08001923 /* set freeze, halt */
1924 err = flexcan_chip_freeze(priv);
1925 if (err)
1926 goto out_chip_disable;
1927
1928 /* activate FIFO, restrict register access */
Pankaj Bansal88462d22017-11-24 18:52:08 +05301929 reg = priv->read(&regs->mcr);
Joakim Zhangec15e272021-02-18 19:00:36 +08001930 reg |= FLEXCAN_MCR_FEN | FLEXCAN_MCR_SUPV;
Pankaj Bansal88462d22017-11-24 18:52:08 +05301931 priv->write(reg, &regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001932
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +02001933 /* Currently we only support newer versions of this core
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +02001934 * featuring a RX hardware FIFO (although this driver doesn't
1935 * make use of it on some cores). Older cores, found on some
1936 * Coldfire derivates are not tested.
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001937 */
Pankaj Bansal88462d22017-11-24 18:52:08 +05301938 reg = priv->read(&regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001939 if (!(reg & FLEXCAN_MCR_FEN)) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +01001940 netdev_err(dev, "Could not enable RX FIFO, unsupported core\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001941 err = -ENODEV;
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001942 goto out_chip_disable;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001943 }
1944
1945 err = register_candev(dev);
Aisheng Dongca109892018-11-30 08:53:26 +00001946 if (err)
1947 goto out_chip_disable;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001948
Aisheng Dongca109892018-11-30 08:53:26 +00001949 /* Disable core and let pm_runtime_put() disable the clocks.
1950 * If CONFIG_PM is not enabled, the clocks will stay powered.
1951 */
1952 flexcan_chip_disable(priv);
1953 pm_runtime_put(priv->dev);
1954
1955 return 0;
1956
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001957 out_chip_disable:
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001958 flexcan_chip_disable(priv);
Aisheng Dongca109892018-11-30 08:53:26 +00001959 out_clks_disable:
1960 flexcan_clks_disable(priv);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001961 return err;
1962}
1963
Bill Pemberton3c8ac0f2012-12-03 09:22:44 -05001964static void unregister_flexcandev(struct net_device *dev)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001965{
1966 unregister_candev(dev);
1967}
1968
Joakim Zhang812f0112020-11-06 18:56:27 +08001969static int flexcan_setup_stop_mode_gpr(struct platform_device *pdev)
Aisheng Dongde3578c2018-11-23 08:35:33 +00001970{
1971 struct net_device *dev = platform_get_drvdata(pdev);
1972 struct device_node *np = pdev->dev.of_node;
1973 struct device_node *gpr_np;
1974 struct flexcan_priv *priv;
1975 phandle phandle;
Marc Kleine-Budded9b081e2020-06-14 21:09:20 +02001976 u32 out_val[3];
Aisheng Dongde3578c2018-11-23 08:35:33 +00001977 int ret;
1978
1979 if (!np)
1980 return -EINVAL;
1981
1982 /* stop mode property format is:
Marc Kleine-Budde499aa922020-10-14 13:41:36 +02001983 * <&gpr req_gpr req_bit>.
Aisheng Dongde3578c2018-11-23 08:35:33 +00001984 */
1985 ret = of_property_read_u32_array(np, "fsl,stop-mode", out_val,
1986 ARRAY_SIZE(out_val));
1987 if (ret) {
1988 dev_dbg(&pdev->dev, "no stop-mode property\n");
1989 return ret;
1990 }
1991 phandle = *out_val;
1992
1993 gpr_np = of_find_node_by_phandle(phandle);
1994 if (!gpr_np) {
1995 dev_dbg(&pdev->dev, "could not find gpr node by phandle\n");
YueHaibing7873e982018-12-12 17:24:01 +08001996 return -ENODEV;
Aisheng Dongde3578c2018-11-23 08:35:33 +00001997 }
1998
1999 priv = netdev_priv(dev);
2000 priv->stm.gpr = syscon_node_to_regmap(gpr_np);
Aisheng Dongde3578c2018-11-23 08:35:33 +00002001 if (IS_ERR(priv->stm.gpr)) {
2002 dev_dbg(&pdev->dev, "could not find gpr regmap\n");
Wen Yange9f2a852019-07-06 11:37:20 +08002003 ret = PTR_ERR(priv->stm.gpr);
2004 goto out_put_node;
Aisheng Dongde3578c2018-11-23 08:35:33 +00002005 }
2006
2007 priv->stm.req_gpr = out_val[1];
2008 priv->stm.req_bit = out_val[2];
Aisheng Dongde3578c2018-11-23 08:35:33 +00002009
2010 dev_dbg(&pdev->dev,
Marc Kleine-Budded9b081e2020-06-14 21:09:20 +02002011 "gpr %s req_gpr=0x02%x req_bit=%u\n",
2012 gpr_np->full_name, priv->stm.req_gpr, priv->stm.req_bit);
Aisheng Dongde3578c2018-11-23 08:35:33 +00002013
Aisheng Dongde3578c2018-11-23 08:35:33 +00002014 return 0;
David S. Miller13dfb3f2019-08-06 18:44:57 -07002015
Wen Yange9f2a852019-07-06 11:37:20 +08002016out_put_node:
2017 of_node_put(gpr_np);
2018 return ret;
Aisheng Dongde3578c2018-11-23 08:35:33 +00002019}
2020
Joakim Zhang812f0112020-11-06 18:56:27 +08002021static int flexcan_setup_stop_mode_scfw(struct platform_device *pdev)
2022{
2023 struct net_device *dev = platform_get_drvdata(pdev);
2024 struct flexcan_priv *priv;
2025 u8 scu_idx;
2026 int ret;
2027
2028 ret = of_property_read_u8(pdev->dev.of_node, "fsl,scu-index", &scu_idx);
2029 if (ret < 0) {
2030 dev_dbg(&pdev->dev, "failed to get scu index\n");
2031 return ret;
2032 }
2033
2034 priv = netdev_priv(dev);
2035 priv->scu_idx = scu_idx;
2036
Marc Kleine-Budde02ee6802021-01-27 09:13:03 +01002037 /* this function could be deferred probe, return -EPROBE_DEFER */
Joakim Zhang812f0112020-11-06 18:56:27 +08002038 return imx_scu_get_handle(&priv->sc_ipc_handle);
2039}
2040
2041/* flexcan_setup_stop_mode - Setup stop mode for wakeup
2042 *
2043 * Return: = 0 setup stop mode successfully or doesn't support this feature
Marc Kleine-Budde02ee6802021-01-27 09:13:03 +01002044 * < 0 fail to setup stop mode (could be deferred probe)
Joakim Zhang812f0112020-11-06 18:56:27 +08002045 */
2046static int flexcan_setup_stop_mode(struct platform_device *pdev)
2047{
2048 struct net_device *dev = platform_get_drvdata(pdev);
2049 struct flexcan_priv *priv;
2050 int ret;
2051
2052 priv = netdev_priv(dev);
2053
2054 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_SETUP_STOP_MODE_SCFW)
2055 ret = flexcan_setup_stop_mode_scfw(pdev);
2056 else if (priv->devtype_data->quirks & FLEXCAN_QUIRK_SETUP_STOP_MODE_GPR)
2057 ret = flexcan_setup_stop_mode_gpr(pdev);
2058 else
2059 /* return 0 directly if doesn't support stop mode feature */
2060 return 0;
2061
2062 if (ret)
2063 return ret;
2064
2065 device_set_wakeup_capable(&pdev->dev, true);
2066
2067 if (of_property_read_bool(pdev->dev.of_node, "wakeup-source"))
2068 device_set_wakeup_enable(&pdev->dev, true);
2069
2070 return 0;
2071}
2072
Hui Wang30c1e672012-06-28 16:21:35 +08002073static const struct of_device_id flexcan_of_match[] = {
Joakim Zhang2a1993e2019-07-12 08:02:59 +00002074 { .compatible = "fsl,imx8qm-flexcan", .data = &fsl_imx8qm_devtype_data, },
Joakim Zhang3aa25392020-09-30 05:15:56 +08002075 { .compatible = "fsl,imx8mp-flexcan", .data = &fsl_imx8mp_devtype_data, },
Hui Wang30c1e672012-06-28 16:21:35 +08002076 { .compatible = "fsl,imx6q-flexcan", .data = &fsl_imx6q_devtype_data, },
Marc Kleine-Buddee3587842013-10-03 23:51:55 +02002077 { .compatible = "fsl,imx28-flexcan", .data = &fsl_imx28_devtype_data, },
Uwe Kleine-König0e030a32018-04-25 16:50:39 +02002078 { .compatible = "fsl,imx53-flexcan", .data = &fsl_imx25_devtype_data, },
2079 { .compatible = "fsl,imx35-flexcan", .data = &fsl_imx25_devtype_data, },
2080 { .compatible = "fsl,imx25-flexcan", .data = &fsl_imx25_devtype_data, },
Marc Kleine-Buddee3587842013-10-03 23:51:55 +02002081 { .compatible = "fsl,p1010-flexcan", .data = &fsl_p1010_devtype_data, },
Stefan Agnercdce8442014-07-15 14:56:21 +02002082 { .compatible = "fsl,vf610-flexcan", .data = &fsl_vf610_devtype_data, },
Pankaj Bansal99b76682017-11-24 18:52:09 +05302083 { .compatible = "fsl,ls1021ar2-flexcan", .data = &fsl_ls1021a_r2_devtype_data, },
Joakim Zhang2c19bb42019-07-12 08:03:01 +00002084 { .compatible = "fsl,lx2160ar1-flexcan", .data = &fsl_lx2160a_r1_devtype_data, },
Hui Wang30c1e672012-06-28 16:21:35 +08002085 { /* sentinel */ },
2086};
Marc Kleine-Budde4358a9d2012-10-04 10:55:35 +02002087MODULE_DEVICE_TABLE(of, flexcan_of_match);
Hui Wang30c1e672012-06-28 16:21:35 +08002088
Angelo Dureghellod9cead72021-07-02 11:48:41 +02002089static const struct platform_device_id flexcan_id_table[] = {
2090 {
2091 .name = "flexcan-mcf5441x",
2092 .driver_data = (kernel_ulong_t)&fsl_mcf5441x_devtype_data,
2093 }, {
2094 /* sentinel */
2095 },
2096};
2097MODULE_DEVICE_TABLE(platform, flexcan_id_table);
2098
Bill Pemberton3c8ac0f2012-12-03 09:22:44 -05002099static int flexcan_probe(struct platform_device *pdev)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02002100{
Angelo Dureghellod9cead72021-07-02 11:48:41 +02002101 const struct of_device_id *of_id;
Marc Kleine-Buddedda0b3b2012-07-13 14:52:48 +02002102 const struct flexcan_devtype_data *devtype_data;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02002103 struct net_device *dev;
2104 struct flexcan_priv *priv;
Andreas Werner555828e2015-03-22 17:35:52 +01002105 struct regulator *reg_xceiver;
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02002106 struct clk *clk_ipg = NULL, *clk_per = NULL;
Marc Kleine-Budde89af8742015-05-08 09:32:58 +02002107 struct flexcan_regs __iomem *regs;
Angelo Dureghellod9cead72021-07-02 11:48:41 +02002108 struct flexcan_platform_data *pdata;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02002109 int err, irq;
Dong Aisheng8c306be2018-12-13 07:08:00 +00002110 u8 clk_src = 1;
holt@sgi.com97efe9a2011-08-16 17:32:23 +00002111 u32 clock_freq = 0;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02002112
Marc Kleine-Budde3d60f332020-09-22 16:44:16 +02002113 reg_xceiver = devm_regulator_get_optional(&pdev->dev, "xceiver");
Andreas Werner555828e2015-03-22 17:35:52 +01002114 if (PTR_ERR(reg_xceiver) == -EPROBE_DEFER)
2115 return -EPROBE_DEFER;
Marc Kleine-Budde3d60f332020-09-22 16:44:16 +02002116 else if (PTR_ERR(reg_xceiver) == -ENODEV)
Andreas Werner555828e2015-03-22 17:35:52 +01002117 reg_xceiver = NULL;
Marc Kleine-Budde3d60f332020-09-22 16:44:16 +02002118 else if (IS_ERR(reg_xceiver))
2119 return PTR_ERR(reg_xceiver);
Andreas Werner555828e2015-03-22 17:35:52 +01002120
Dong Aisheng8c306be2018-12-13 07:08:00 +00002121 if (pdev->dev.of_node) {
Hui Wangafc016d2012-06-28 16:21:34 +08002122 of_property_read_u32(pdev->dev.of_node,
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +02002123 "clock-frequency", &clock_freq);
Dong Aisheng8c306be2018-12-13 07:08:00 +00002124 of_property_read_u8(pdev->dev.of_node,
2125 "fsl,clk-source", &clk_src);
Angelo Dureghellod9cead72021-07-02 11:48:41 +02002126 } else {
2127 pdata = dev_get_platdata(&pdev->dev);
2128 if (pdata) {
2129 clock_freq = pdata->clock_frequency;
2130 clk_src = pdata->clk_src;
2131 }
Dong Aisheng8c306be2018-12-13 07:08:00 +00002132 }
holt@sgi.com97efe9a2011-08-16 17:32:23 +00002133
2134 if (!clock_freq) {
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02002135 clk_ipg = devm_clk_get(&pdev->dev, "ipg");
2136 if (IS_ERR(clk_ipg)) {
2137 dev_err(&pdev->dev, "no ipg clock defined\n");
Fabio Estevam933e4af2013-07-22 12:41:39 -03002138 return PTR_ERR(clk_ipg);
holt@sgi.com97efe9a2011-08-16 17:32:23 +00002139 }
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02002140
2141 clk_per = devm_clk_get(&pdev->dev, "per");
2142 if (IS_ERR(clk_per)) {
2143 dev_err(&pdev->dev, "no per clock defined\n");
Fabio Estevam933e4af2013-07-22 12:41:39 -03002144 return PTR_ERR(clk_per);
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02002145 }
Marc Kleine-Budde1a3e5172013-11-25 22:15:20 +01002146 clock_freq = clk_get_rate(clk_per);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02002147 }
2148
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02002149 irq = platform_get_irq(pdev, 0);
Fabio Estevam933e4af2013-07-22 12:41:39 -03002150 if (irq <= 0)
2151 return -ENODEV;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02002152
Joakim Zhanga4721f22019-09-29 08:32:09 +00002153 regs = devm_platform_ioremap_resource(pdev, 0);
Marc Kleine-Budde89af8742015-05-08 09:32:58 +02002154 if (IS_ERR(regs))
2155 return PTR_ERR(regs);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02002156
Angelo Dureghellod9cead72021-07-02 11:48:41 +02002157 of_id = of_match_device(flexcan_of_match, &pdev->dev);
2158 if (of_id)
2159 devtype_data = of_id->data;
2160 else if (platform_get_device_id(pdev)->driver_data)
2161 devtype_data = (struct flexcan_devtype_data *)
2162 platform_get_device_id(pdev)->driver_data;
2163 else
2164 return -ENODEV;
Hui Wang30c1e672012-06-28 16:21:35 +08002165
Joakim Zhangeadf6ca2019-07-12 08:02:44 +00002166 if ((devtype_data->quirks & FLEXCAN_QUIRK_SUPPORT_FD) &&
2167 !(devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP)) {
2168 dev_err(&pdev->dev, "CAN-FD mode doesn't work with FIFO mode!\n");
2169 return -EINVAL;
2170 }
2171
Fabio Estevam933e4af2013-07-22 12:41:39 -03002172 dev = alloc_candev(sizeof(struct flexcan_priv), 1);
2173 if (!dev)
2174 return -ENOMEM;
2175
Marc Kleine-Budde30164752015-05-10 15:26:58 +02002176 platform_set_drvdata(pdev, dev);
2177 SET_NETDEV_DEV(dev, &pdev->dev);
2178
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02002179 dev->netdev_ops = &flexcan_netdev_ops;
2180 dev->irq = irq;
Reuben Dowle9a123492011-11-01 11:18:03 +13002181 dev->flags |= IFF_ECHO;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02002182
2183 priv = netdev_priv(dev);
Pankaj Bansal88462d22017-11-24 18:52:08 +05302184
Uwe Kleine-König0e030a32018-04-25 16:50:39 +02002185 if (of_property_read_bool(pdev->dev.of_node, "big-endian") ||
2186 devtype_data->quirks & FLEXCAN_QUIRK_DEFAULT_BIG_ENDIAN) {
Pankaj Bansal88462d22017-11-24 18:52:08 +05302187 priv->read = flexcan_read_be;
2188 priv->write = flexcan_write_be;
2189 } else {
Uwe Kleine-König0e030a32018-04-25 16:50:39 +02002190 priv->read = flexcan_read_le;
2191 priv->write = flexcan_write_le;
Pankaj Bansal88462d22017-11-24 18:52:08 +05302192 }
2193
Aisheng Dongca109892018-11-30 08:53:26 +00002194 priv->dev = &pdev->dev;
holt@sgi.com97efe9a2011-08-16 17:32:23 +00002195 priv->can.clock.freq = clock_freq;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02002196 priv->can.do_set_mode = flexcan_set_mode;
2197 priv->can.do_get_berr_counter = flexcan_get_berr_counter;
2198 priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK |
2199 CAN_CTRLMODE_LISTENONLY | CAN_CTRLMODE_3_SAMPLES |
2200 CAN_CTRLMODE_BERR_REPORTING;
Marc Kleine-Budde89af8742015-05-08 09:32:58 +02002201 priv->regs = regs;
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02002202 priv->clk_ipg = clk_ipg;
2203 priv->clk_per = clk_per;
Dong Aisheng8c306be2018-12-13 07:08:00 +00002204 priv->clk_src = clk_src;
Hui Wang30c1e672012-06-28 16:21:35 +08002205 priv->devtype_data = devtype_data;
Andreas Werner555828e2015-03-22 17:35:52 +01002206 priv->reg_xceiver = reg_xceiver;
Fabio Estevamb7c41142013-06-10 23:12:57 -03002207
Angelo Dureghellod9cead72021-07-02 11:48:41 +02002208 if (devtype_data->quirks & FLEXCAN_QUIRK_NR_IRQ_3) {
2209 priv->irq_boff = platform_get_irq(pdev, 1);
2210 if (priv->irq_boff <= 0) {
2211 err = -ENODEV;
2212 goto failed_platform_get_irq;
2213 }
2214 priv->irq_err = platform_get_irq(pdev, 2);
2215 if (priv->irq_err <= 0) {
2216 err = -ENODEV;
2217 goto failed_platform_get_irq;
2218 }
2219 }
2220
Joakim Zhangeadf6ca2019-07-12 08:02:44 +00002221 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_SUPPORT_FD) {
Joakim Zhangce885a12019-07-12 08:02:51 +00002222 priv->can.ctrlmode_supported |= CAN_CTRLMODE_FD |
2223 CAN_CTRLMODE_FD_NON_ISO;
Joakim Zhangeadf6ca2019-07-12 08:02:44 +00002224 priv->can.bittiming_const = &flexcan_fd_bittiming_const;
2225 priv->can.data_bittiming_const =
2226 &flexcan_fd_data_bittiming_const;
2227 } else {
2228 priv->can.bittiming_const = &flexcan_bittiming_const;
2229 }
2230
Aisheng Dongca109892018-11-30 08:53:26 +00002231 pm_runtime_get_noresume(&pdev->dev);
2232 pm_runtime_set_active(&pdev->dev);
2233 pm_runtime_enable(&pdev->dev);
2234
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02002235 err = register_flexcandev(dev);
2236 if (err) {
2237 dev_err(&pdev->dev, "registering netdev failed\n");
2238 goto failed_register;
2239 }
2240
Joakim Zhang812f0112020-11-06 18:56:27 +08002241 err = flexcan_setup_stop_mode(pdev);
2242 if (err < 0) {
2243 if (err != -EPROBE_DEFER)
2244 dev_err(&pdev->dev, "setup stop mode failed\n");
2245 goto failed_setup_stop_mode;
2246 }
2247
Joakim Zhangee973022019-10-30 06:45:57 +00002248 of_can_transceiver(dev);
Fabio Baltieriadccadb2012-12-18 18:50:58 +01002249 devm_can_led_init(dev);
2250
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02002251 return 0;
2252
Joakim Zhang812f0112020-11-06 18:56:27 +08002253 failed_setup_stop_mode:
2254 unregister_flexcandev(dev);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02002255 failed_register:
Joakim Zhang5a9323f2020-09-30 05:15:57 +08002256 pm_runtime_put_noidle(&pdev->dev);
2257 pm_runtime_disable(&pdev->dev);
Angelo Dureghellod9cead72021-07-02 11:48:41 +02002258 failed_platform_get_irq:
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02002259 free_candev(dev);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02002260 return err;
2261}
2262
Bill Pemberton3c8ac0f2012-12-03 09:22:44 -05002263static int flexcan_remove(struct platform_device *pdev)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02002264{
2265 struct net_device *dev = platform_get_drvdata(pdev);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02002266
Joakim Zhangab07ff12020-10-21 02:45:27 +08002267 device_set_wakeup_enable(&pdev->dev, false);
2268 device_set_wakeup_capable(&pdev->dev, false);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02002269 unregister_flexcandev(dev);
Aisheng Dongca109892018-11-30 08:53:26 +00002270 pm_runtime_disable(&pdev->dev);
Marc Kleine-Budde9a275862010-10-21 05:07:58 +00002271 free_candev(dev);
2272
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02002273 return 0;
2274}
2275
Marc Kleine-Budde08c6d352014-03-05 19:10:44 +01002276static int __maybe_unused flexcan_suspend(struct device *device)
Eric Bénard8b5e2182012-05-08 17:12:17 +02002277{
Fabio Estevam588e7a82013-05-20 15:43:43 -03002278 struct net_device *dev = dev_get_drvdata(device);
Eric Bénard8b5e2182012-05-08 17:12:17 +02002279 struct flexcan_priv *priv = netdev_priv(dev);
Joakim Zhang1434d042019-12-04 11:36:19 +00002280 int err;
Eric Bénard8b5e2182012-05-08 17:12:17 +02002281
Eric Bénard8b5e2182012-05-08 17:12:17 +02002282 if (netif_running(dev)) {
Aisheng Dongde3578c2018-11-23 08:35:33 +00002283 /* if wakeup is enabled, enter stop mode
2284 * else enter disabled mode.
2285 */
2286 if (device_may_wakeup(device)) {
2287 enable_irq_wake(dev->irq);
Joakim Zhang5f186c22019-07-02 01:45:41 +00002288 err = flexcan_enter_stop_mode(priv);
2289 if (err)
2290 return err;
Aisheng Dongde3578c2018-11-23 08:35:33 +00002291 } else {
Joakim Zhang1434d042019-12-04 11:36:19 +00002292 err = flexcan_chip_stop(dev);
2293 if (err)
2294 return err;
2295
Marc Kleine-Budde49dea042020-11-19 11:09:14 +01002296 flexcan_chip_interrupts_disable(dev);
2297
Joakim Zhang1434d042019-12-04 11:36:19 +00002298 err = pinctrl_pm_select_sleep_state(device);
Aisheng Dongde3578c2018-11-23 08:35:33 +00002299 if (err)
2300 return err;
2301 }
Eric Bénard8b5e2182012-05-08 17:12:17 +02002302 netif_stop_queue(dev);
2303 netif_device_detach(dev);
2304 }
2305 priv->can.state = CAN_STATE_SLEEPING;
2306
Joakim Zhang1434d042019-12-04 11:36:19 +00002307 return 0;
Eric Bénard8b5e2182012-05-08 17:12:17 +02002308}
2309
Marc Kleine-Budde08c6d352014-03-05 19:10:44 +01002310static int __maybe_unused flexcan_resume(struct device *device)
Eric Bénard8b5e2182012-05-08 17:12:17 +02002311{
Fabio Estevam588e7a82013-05-20 15:43:43 -03002312 struct net_device *dev = dev_get_drvdata(device);
Eric Bénard8b5e2182012-05-08 17:12:17 +02002313 struct flexcan_priv *priv = netdev_priv(dev);
Joakim Zhang1434d042019-12-04 11:36:19 +00002314 int err;
Eric Bénard8b5e2182012-05-08 17:12:17 +02002315
2316 priv->can.state = CAN_STATE_ERROR_ACTIVE;
2317 if (netif_running(dev)) {
2318 netif_device_attach(dev);
2319 netif_start_queue(dev);
Aisheng Dongde3578c2018-11-23 08:35:33 +00002320 if (device_may_wakeup(device)) {
2321 disable_irq_wake(dev->irq);
Sean Nyekjaere7071802019-12-04 11:36:06 +00002322 err = flexcan_exit_stop_mode(priv);
2323 if (err)
2324 return err;
Aisheng Dongde3578c2018-11-23 08:35:33 +00002325 } else {
Joakim Zhang1434d042019-12-04 11:36:19 +00002326 err = pinctrl_pm_select_default_state(device);
2327 if (err)
2328 return err;
2329
2330 err = flexcan_chip_start(dev);
2331 if (err)
2332 return err;
Marc Kleine-Budde49dea042020-11-19 11:09:14 +01002333
2334 flexcan_chip_interrupts_enable(dev);
Aisheng Dongde3578c2018-11-23 08:35:33 +00002335 }
Eric Bénard8b5e2182012-05-08 17:12:17 +02002336 }
Aisheng Dongca109892018-11-30 08:53:26 +00002337
Joakim Zhang1434d042019-12-04 11:36:19 +00002338 return 0;
Aisheng Dongca109892018-11-30 08:53:26 +00002339}
2340
2341static int __maybe_unused flexcan_runtime_suspend(struct device *device)
2342{
2343 struct net_device *dev = dev_get_drvdata(device);
2344 struct flexcan_priv *priv = netdev_priv(dev);
2345
2346 flexcan_clks_disable(priv);
2347
Fabio Estevam4de349e2016-08-17 12:41:08 -03002348 return 0;
Eric Bénard8b5e2182012-05-08 17:12:17 +02002349}
Fabio Estevam588e7a82013-05-20 15:43:43 -03002350
Aisheng Dongca109892018-11-30 08:53:26 +00002351static int __maybe_unused flexcan_runtime_resume(struct device *device)
2352{
2353 struct net_device *dev = dev_get_drvdata(device);
2354 struct flexcan_priv *priv = netdev_priv(dev);
2355
2356 return flexcan_clks_enable(priv);
2357}
2358
Aisheng Dongde3578c2018-11-23 08:35:33 +00002359static int __maybe_unused flexcan_noirq_suspend(struct device *device)
2360{
2361 struct net_device *dev = dev_get_drvdata(device);
2362 struct flexcan_priv *priv = netdev_priv(dev);
2363
Joakim Zhang02f71c62019-12-10 09:00:13 +00002364 if (netif_running(dev)) {
2365 int err;
2366
2367 if (device_may_wakeup(device))
2368 flexcan_enable_wakeup_irq(priv, true);
2369
2370 err = pm_runtime_force_suspend(device);
2371 if (err)
2372 return err;
2373 }
Aisheng Dongde3578c2018-11-23 08:35:33 +00002374
2375 return 0;
2376}
2377
2378static int __maybe_unused flexcan_noirq_resume(struct device *device)
2379{
2380 struct net_device *dev = dev_get_drvdata(device);
2381 struct flexcan_priv *priv = netdev_priv(dev);
2382
Joakim Zhang02f71c62019-12-10 09:00:13 +00002383 if (netif_running(dev)) {
2384 int err;
2385
2386 err = pm_runtime_force_resume(device);
2387 if (err)
2388 return err;
2389
2390 if (device_may_wakeup(device))
2391 flexcan_enable_wakeup_irq(priv, false);
2392 }
Aisheng Dongde3578c2018-11-23 08:35:33 +00002393
2394 return 0;
2395}
2396
2397static const struct dev_pm_ops flexcan_pm_ops = {
2398 SET_SYSTEM_SLEEP_PM_OPS(flexcan_suspend, flexcan_resume)
Aisheng Dongca109892018-11-30 08:53:26 +00002399 SET_RUNTIME_PM_OPS(flexcan_runtime_suspend, flexcan_runtime_resume, NULL)
Aisheng Dongde3578c2018-11-23 08:35:33 +00002400 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(flexcan_noirq_suspend, flexcan_noirq_resume)
2401};
Eric Bénard8b5e2182012-05-08 17:12:17 +02002402
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02002403static struct platform_driver flexcan_driver = {
holt@sgi.comc8aef4c2011-08-16 17:32:22 +00002404 .driver = {
2405 .name = DRV_NAME,
Fabio Estevam588e7a82013-05-20 15:43:43 -03002406 .pm = &flexcan_pm_ops,
holt@sgi.comc8aef4c2011-08-16 17:32:22 +00002407 .of_match_table = flexcan_of_match,
2408 },
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02002409 .probe = flexcan_probe,
Bill Pemberton3c8ac0f2012-12-03 09:22:44 -05002410 .remove = flexcan_remove,
Angelo Dureghellod9cead72021-07-02 11:48:41 +02002411 .id_table = flexcan_id_table,
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02002412};
2413
Axel Lin871d3372011-11-27 15:42:31 +00002414module_platform_driver(flexcan_driver);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02002415
2416MODULE_AUTHOR("Sascha Hauer <kernel@pengutronix.de>, "
2417 "Marc Kleine-Budde <kernel@pengutronix.de>");
2418MODULE_LICENSE("GPL v2");
2419MODULE_DESCRIPTION("CAN port driver for flexcan based chip");