blob: d6ad12744ff10462efeb9f01050aa939f2becc42 [file] [log] [blame]
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001/*
2 * flexcan.c - FLEXCAN CAN controller driver
3 *
4 * Copyright (c) 2005-2006 Varma Electronics Oy
5 * Copyright (c) 2009 Sascha Hauer, Pengutronix
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +02006 * Copyright (c) 2010-2017 Pengutronix, Marc Kleine-Budde <kernel@pengutronix.de>
7 * Copyright (c) 2014 David Jander, Protonic Holland
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02008 *
9 * Based on code originally by Andrey Volkov <avolkov@varma-el.com>
10 *
11 * LICENCE:
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation version 2.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 */
22
23#include <linux/netdevice.h>
24#include <linux/can.h>
25#include <linux/can/dev.h>
26#include <linux/can/error.h>
Fabio Baltieriadccadb2012-12-18 18:50:58 +010027#include <linux/can/led.h>
Marc Kleine-Budde30164752015-05-10 15:26:58 +020028#include <linux/can/rx-offload.h>
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +020029#include <linux/clk.h>
30#include <linux/delay.h>
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +020031#include <linux/interrupt.h>
32#include <linux/io.h>
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +020033#include <linux/module.h>
holt@sgi.com97efe9a2011-08-16 17:32:23 +000034#include <linux/of.h>
Hui Wang30c1e672012-06-28 16:21:35 +080035#include <linux/of_device.h>
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +020036#include <linux/platform_device.h>
Fabio Estevamb7c41142013-06-10 23:12:57 -030037#include <linux/regulator/consumer.h>
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +020038
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +020039#define DRV_NAME "flexcan"
40
41/* 8 for RX fifo and 2 error handling */
42#define FLEXCAN_NAPI_WEIGHT (8 + 2)
43
44/* FLEXCAN module configuration register (CANMCR) bits */
45#define FLEXCAN_MCR_MDIS BIT(31)
46#define FLEXCAN_MCR_FRZ BIT(30)
47#define FLEXCAN_MCR_FEN BIT(29)
48#define FLEXCAN_MCR_HALT BIT(28)
49#define FLEXCAN_MCR_NOT_RDY BIT(27)
50#define FLEXCAN_MCR_WAK_MSK BIT(26)
51#define FLEXCAN_MCR_SOFTRST BIT(25)
52#define FLEXCAN_MCR_FRZ_ACK BIT(24)
53#define FLEXCAN_MCR_SUPV BIT(23)
54#define FLEXCAN_MCR_SLF_WAK BIT(22)
55#define FLEXCAN_MCR_WRN_EN BIT(21)
56#define FLEXCAN_MCR_LPM_ACK BIT(20)
57#define FLEXCAN_MCR_WAK_SRC BIT(19)
58#define FLEXCAN_MCR_DOZE BIT(18)
59#define FLEXCAN_MCR_SRX_DIS BIT(17)
Marc Kleine-Budde62d10862015-08-27 16:01:27 +020060#define FLEXCAN_MCR_IRMQ BIT(16)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +020061#define FLEXCAN_MCR_LPRIO_EN BIT(13)
62#define FLEXCAN_MCR_AEN BIT(12)
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +020063/* MCR_MAXMB: maximum used MBs is MAXMB + 1 */
Marc Kleine-Budde4c728d82014-09-02 16:54:17 +020064#define FLEXCAN_MCR_MAXMB(x) ((x) & 0x7f)
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +020065#define FLEXCAN_MCR_IDAM_A (0x0 << 8)
66#define FLEXCAN_MCR_IDAM_B (0x1 << 8)
67#define FLEXCAN_MCR_IDAM_C (0x2 << 8)
68#define FLEXCAN_MCR_IDAM_D (0x3 << 8)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +020069
70/* FLEXCAN control register (CANCTRL) bits */
71#define FLEXCAN_CTRL_PRESDIV(x) (((x) & 0xff) << 24)
72#define FLEXCAN_CTRL_RJW(x) (((x) & 0x03) << 22)
73#define FLEXCAN_CTRL_PSEG1(x) (((x) & 0x07) << 19)
74#define FLEXCAN_CTRL_PSEG2(x) (((x) & 0x07) << 16)
75#define FLEXCAN_CTRL_BOFF_MSK BIT(15)
76#define FLEXCAN_CTRL_ERR_MSK BIT(14)
77#define FLEXCAN_CTRL_CLK_SRC BIT(13)
78#define FLEXCAN_CTRL_LPB BIT(12)
79#define FLEXCAN_CTRL_TWRN_MSK BIT(11)
80#define FLEXCAN_CTRL_RWRN_MSK BIT(10)
81#define FLEXCAN_CTRL_SMP BIT(7)
82#define FLEXCAN_CTRL_BOFF_REC BIT(6)
83#define FLEXCAN_CTRL_TSYN BIT(5)
84#define FLEXCAN_CTRL_LBUF BIT(4)
85#define FLEXCAN_CTRL_LOM BIT(3)
86#define FLEXCAN_CTRL_PROPSEG(x) ((x) & 0x07)
87#define FLEXCAN_CTRL_ERR_BUS (FLEXCAN_CTRL_ERR_MSK)
88#define FLEXCAN_CTRL_ERR_STATE \
89 (FLEXCAN_CTRL_TWRN_MSK | FLEXCAN_CTRL_RWRN_MSK | \
90 FLEXCAN_CTRL_BOFF_MSK)
91#define FLEXCAN_CTRL_ERR_ALL \
92 (FLEXCAN_CTRL_ERR_BUS | FLEXCAN_CTRL_ERR_STATE)
93
Stefan Agnercdce8442014-07-15 14:56:21 +020094/* FLEXCAN control register 2 (CTRL2) bits */
Marc Kleine-Budde6f75fce2014-09-23 11:03:01 +020095#define FLEXCAN_CTRL2_ECRWRE BIT(29)
96#define FLEXCAN_CTRL2_WRMFRZ BIT(28)
97#define FLEXCAN_CTRL2_RFFN(x) (((x) & 0x0f) << 24)
98#define FLEXCAN_CTRL2_TASD(x) (((x) & 0x1f) << 19)
99#define FLEXCAN_CTRL2_MRP BIT(18)
100#define FLEXCAN_CTRL2_RRS BIT(17)
101#define FLEXCAN_CTRL2_EACEN BIT(16)
Stefan Agnercdce8442014-07-15 14:56:21 +0200102
103/* FLEXCAN memory error control register (MECR) bits */
104#define FLEXCAN_MECR_ECRWRDIS BIT(31)
105#define FLEXCAN_MECR_HANCEI_MSK BIT(19)
106#define FLEXCAN_MECR_FANCEI_MSK BIT(18)
107#define FLEXCAN_MECR_CEI_MSK BIT(16)
108#define FLEXCAN_MECR_HAERRIE BIT(15)
109#define FLEXCAN_MECR_FAERRIE BIT(14)
110#define FLEXCAN_MECR_EXTERRIE BIT(13)
111#define FLEXCAN_MECR_RERRDIS BIT(9)
112#define FLEXCAN_MECR_ECCDIS BIT(8)
113#define FLEXCAN_MECR_NCEFAFRZ BIT(7)
114
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200115/* FLEXCAN error and status register (ESR) bits */
116#define FLEXCAN_ESR_TWRN_INT BIT(17)
117#define FLEXCAN_ESR_RWRN_INT BIT(16)
118#define FLEXCAN_ESR_BIT1_ERR BIT(15)
119#define FLEXCAN_ESR_BIT0_ERR BIT(14)
120#define FLEXCAN_ESR_ACK_ERR BIT(13)
121#define FLEXCAN_ESR_CRC_ERR BIT(12)
122#define FLEXCAN_ESR_FRM_ERR BIT(11)
123#define FLEXCAN_ESR_STF_ERR BIT(10)
124#define FLEXCAN_ESR_TX_WRN BIT(9)
125#define FLEXCAN_ESR_RX_WRN BIT(8)
126#define FLEXCAN_ESR_IDLE BIT(7)
127#define FLEXCAN_ESR_TXRX BIT(6)
128#define FLEXCAN_EST_FLT_CONF_SHIFT (4)
129#define FLEXCAN_ESR_FLT_CONF_MASK (0x3 << FLEXCAN_EST_FLT_CONF_SHIFT)
130#define FLEXCAN_ESR_FLT_CONF_ACTIVE (0x0 << FLEXCAN_EST_FLT_CONF_SHIFT)
131#define FLEXCAN_ESR_FLT_CONF_PASSIVE (0x1 << FLEXCAN_EST_FLT_CONF_SHIFT)
132#define FLEXCAN_ESR_BOFF_INT BIT(2)
133#define FLEXCAN_ESR_ERR_INT BIT(1)
134#define FLEXCAN_ESR_WAK_INT BIT(0)
135#define FLEXCAN_ESR_ERR_BUS \
136 (FLEXCAN_ESR_BIT1_ERR | FLEXCAN_ESR_BIT0_ERR | \
137 FLEXCAN_ESR_ACK_ERR | FLEXCAN_ESR_CRC_ERR | \
138 FLEXCAN_ESR_FRM_ERR | FLEXCAN_ESR_STF_ERR)
139#define FLEXCAN_ESR_ERR_STATE \
140 (FLEXCAN_ESR_TWRN_INT | FLEXCAN_ESR_RWRN_INT | FLEXCAN_ESR_BOFF_INT)
141#define FLEXCAN_ESR_ERR_ALL \
142 (FLEXCAN_ESR_ERR_BUS | FLEXCAN_ESR_ERR_STATE)
Wolfgang Grandegger6e9d5542011-12-12 16:09:28 +0100143#define FLEXCAN_ESR_ALL_INT \
144 (FLEXCAN_ESR_TWRN_INT | FLEXCAN_ESR_RWRN_INT | \
145 FLEXCAN_ESR_BOFF_INT | FLEXCAN_ESR_ERR_INT)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200146
147/* FLEXCAN interrupt flag register (IFLAG) bits */
David Jander25e92442014-09-03 16:47:22 +0200148/* Errata ERR005829 step7: Reserve first valid MB */
Marc Kleine-Buddeb93917c2015-07-12 00:47:47 +0200149#define FLEXCAN_TX_MB_RESERVED_OFF_FIFO 8
150#define FLEXCAN_TX_MB_OFF_FIFO 9
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200151#define FLEXCAN_TX_MB_RESERVED_OFF_TIMESTAMP 0
152#define FLEXCAN_TX_MB_OFF_TIMESTAMP 1
153#define FLEXCAN_RX_MB_OFF_TIMESTAMP_FIRST (FLEXCAN_TX_MB_OFF_TIMESTAMP + 1)
154#define FLEXCAN_RX_MB_OFF_TIMESTAMP_LAST 63
Marc Kleine-Buddeb93917c2015-07-12 00:47:47 +0200155#define FLEXCAN_IFLAG_MB(x) BIT(x)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200156#define FLEXCAN_IFLAG_RX_FIFO_OVERFLOW BIT(7)
157#define FLEXCAN_IFLAG_RX_FIFO_WARN BIT(6)
158#define FLEXCAN_IFLAG_RX_FIFO_AVAILABLE BIT(5)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200159
160/* FLEXCAN message buffers */
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200161#define FLEXCAN_MB_CODE_MASK (0xf << 24)
162#define FLEXCAN_MB_CODE_RX_BUSY_BIT (0x1 << 24)
Marc Kleine-Buddec32fe4a2014-09-16 12:39:28 +0200163#define FLEXCAN_MB_CODE_RX_INACTIVE (0x0 << 24)
164#define FLEXCAN_MB_CODE_RX_EMPTY (0x4 << 24)
165#define FLEXCAN_MB_CODE_RX_FULL (0x2 << 24)
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200166#define FLEXCAN_MB_CODE_RX_OVERRUN (0x6 << 24)
Marc Kleine-Buddec32fe4a2014-09-16 12:39:28 +0200167#define FLEXCAN_MB_CODE_RX_RANSWER (0xa << 24)
168
169#define FLEXCAN_MB_CODE_TX_INACTIVE (0x8 << 24)
170#define FLEXCAN_MB_CODE_TX_ABORT (0x9 << 24)
171#define FLEXCAN_MB_CODE_TX_DATA (0xc << 24)
172#define FLEXCAN_MB_CODE_TX_TANSWER (0xe << 24)
173
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200174#define FLEXCAN_MB_CNT_SRR BIT(22)
175#define FLEXCAN_MB_CNT_IDE BIT(21)
176#define FLEXCAN_MB_CNT_RTR BIT(20)
177#define FLEXCAN_MB_CNT_LENGTH(x) (((x) & 0xf) << 16)
178#define FLEXCAN_MB_CNT_TIMESTAMP(x) ((x) & 0xffff)
179
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200180#define FLEXCAN_TIMEOUT_US (50)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200181
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200182/* FLEXCAN hardware feature flags
Wolfgang Grandeggerbb698ca2012-10-10 21:10:42 +0200183 *
184 * Below is some version info we got:
ZHU Yi (ST-FIR/ENG1-Zhu)da49a802017-09-15 07:03:58 +0000185 * SOC Version IP-Version Glitch- [TR]WRN_INT IRQ Err Memory err RTR re-
186 * Filter? connected? Passive detection ception in MB
187 * MX25 FlexCAN2 03.00.00.00 no no ? no no
188 * MX28 FlexCAN2 03.00.04.00 yes yes no no no
189 * MX35 FlexCAN2 03.00.00.00 no no ? no no
190 * MX53 FlexCAN2 03.00.00.00 yes no no no no
191 * MX6s FlexCAN3 10.00.12.00 yes yes no no yes
192 * VF610 FlexCAN3 ? no yes ? yes yes?
Wolfgang Grandeggerbb698ca2012-10-10 21:10:42 +0200193 *
194 * Some SOCs do not have the RX_WARN & TX_WARN interrupt line connected.
195 */
ZHU Yi (ST-FIR/ENG1-Zhu)2f8639b2017-09-15 07:01:23 +0000196#define FLEXCAN_QUIRK_BROKEN_WERR_STATE BIT(1) /* [TR]WRN_INT not connected */
Marc Kleine-Buddef377bff2015-05-08 15:22:36 +0200197#define FLEXCAN_QUIRK_DISABLE_RXFG BIT(2) /* Disable RX FIFO Global mask */
Marc Kleine-Budde9eb7aa82015-09-01 08:57:55 +0200198#define FLEXCAN_QUIRK_ENABLE_EACEN_RRS BIT(3) /* Enable EACEN and RRS bit in ctrl2 */
Marc Kleine-Budde66ddb822017-03-02 15:42:49 +0100199#define FLEXCAN_QUIRK_DISABLE_MECR BIT(4) /* Disable Memory error detection */
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200200#define FLEXCAN_QUIRK_USE_OFF_TIMESTAMP BIT(5) /* Use timestamp based offloading */
ZHU Yi (ST-FIR/ENG1-Zhu)da49a802017-09-15 07:03:58 +0000201#define FLEXCAN_QUIRK_BROKEN_PERR_STATE BIT(6) /* No interrupt for error passive */
Wolfgang Grandegger4f72e5f2012-09-28 03:17:15 +0000202
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200203/* Structure of the message buffer */
204struct flexcan_mb {
205 u32 can_ctrl;
206 u32 can_id;
207 u32 data[2];
208};
209
210/* Structure of the hardware registers */
211struct flexcan_regs {
212 u32 mcr; /* 0x00 */
213 u32 ctrl; /* 0x04 */
214 u32 timer; /* 0x08 */
215 u32 _reserved1; /* 0x0c */
216 u32 rxgmask; /* 0x10 */
217 u32 rx14mask; /* 0x14 */
218 u32 rx15mask; /* 0x18 */
219 u32 ecr; /* 0x1c */
220 u32 esr; /* 0x20 */
221 u32 imask2; /* 0x24 */
222 u32 imask1; /* 0x28 */
223 u32 iflag2; /* 0x2c */
224 u32 iflag1; /* 0x30 */
Marc Kleine-Budde62d10862015-08-27 16:01:27 +0200225 union { /* 0x34 */
226 u32 gfwr_mx28; /* MX28, MX53 */
227 u32 ctrl2; /* MX6, VF610 */
228 };
Hui Wang30c1e672012-06-28 16:21:35 +0800229 u32 esr2; /* 0x38 */
230 u32 imeur; /* 0x3c */
231 u32 lrfr; /* 0x40 */
232 u32 crcr; /* 0x44 */
233 u32 rxfgmask; /* 0x48 */
234 u32 rxfir; /* 0x4c */
Stefan Agnercdce8442014-07-15 14:56:21 +0200235 u32 _reserved3[12]; /* 0x50 */
Marc Kleine-Budde1ba763d2015-08-25 10:39:19 +0200236 struct flexcan_mb mb[64]; /* 0x80 */
Marc Kleine-Budde66a6ef02014-09-17 12:50:48 +0200237 /* FIFO-mode:
238 * MB
239 * 0x080...0x08f 0 RX message buffer
240 * 0x090...0x0df 1-5 reserverd
241 * 0x0e0...0x0ff 6-7 8 entry ID table
242 * (mx25, mx28, mx35, mx53)
243 * 0x0e0...0x2df 6-7..37 8..128 entry ID table
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200244 * size conf'ed via ctrl2::RFFN
Marc Kleine-Budde66a6ef02014-09-17 12:50:48 +0200245 * (mx6, vf610)
246 */
Marc Kleine-Budde62d10862015-08-27 16:01:27 +0200247 u32 _reserved4[256]; /* 0x480 */
248 u32 rximr[64]; /* 0x880 */
249 u32 _reserved5[24]; /* 0x980 */
250 u32 gfwr_mx6; /* 0x9e0 - MX6 */
251 u32 _reserved6[63]; /* 0x9e4 */
Stefan Agnercdce8442014-07-15 14:56:21 +0200252 u32 mecr; /* 0xae0 */
253 u32 erriar; /* 0xae4 */
254 u32 erridpr; /* 0xae8 */
255 u32 errippr; /* 0xaec */
256 u32 rerrar; /* 0xaf0 */
257 u32 rerrdr; /* 0xaf4 */
258 u32 rerrsynr; /* 0xaf8 */
259 u32 errsr; /* 0xafc */
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200260};
261
Hui Wang30c1e672012-06-28 16:21:35 +0800262struct flexcan_devtype_data {
Marc Kleine-Buddef377bff2015-05-08 15:22:36 +0200263 u32 quirks; /* quirks needed for different IP cores */
Hui Wang30c1e672012-06-28 16:21:35 +0800264};
265
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200266struct flexcan_priv {
267 struct can_priv can;
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200268 struct can_rx_offload offload;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200269
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200270 struct flexcan_regs __iomem *regs;
Marc Kleine-Buddeb93917c2015-07-12 00:47:47 +0200271 struct flexcan_mb __iomem *tx_mb;
272 struct flexcan_mb __iomem *tx_mb_reserved;
273 u8 tx_mb_idx;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200274 u32 reg_ctrl_default;
Marc Kleine-Budde28ac7dc2015-08-04 13:46:10 +0200275 u32 reg_imask1_default;
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200276 u32 reg_imask2_default;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200277
Steffen Trumtrar3d42a372012-07-17 16:14:34 +0200278 struct clk *clk_ipg;
279 struct clk *clk_per;
Marc Kleine-Buddedda0b3b2012-07-13 14:52:48 +0200280 const struct flexcan_devtype_data *devtype_data;
Fabio Estevamb7c41142013-06-10 23:12:57 -0300281 struct regulator *reg_xceiver;
Hui Wang30c1e672012-06-28 16:21:35 +0800282};
283
Marc Kleine-Buddea3c11a72016-07-04 14:45:44 +0200284static const struct flexcan_devtype_data fsl_p1010_devtype_data = {
ZHU Yi (ST-FIR/ENG1-Zhu)2f8639b2017-09-15 07:01:23 +0000285 .quirks = FLEXCAN_QUIRK_BROKEN_WERR_STATE,
Hui Wang30c1e672012-06-28 16:21:35 +0800286};
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200287
Marc Kleine-Buddea3c11a72016-07-04 14:45:44 +0200288static const struct flexcan_devtype_data fsl_imx28_devtype_data;
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200289
Marc Kleine-Buddea3c11a72016-07-04 14:45:44 +0200290static const struct flexcan_devtype_data fsl_imx6q_devtype_data = {
Marc Kleine-Budde096de072015-09-01 10:28:46 +0200291 .quirks = FLEXCAN_QUIRK_DISABLE_RXFG | FLEXCAN_QUIRK_ENABLE_EACEN_RRS |
ZHU Yi (ST-FIR/ENG1-Zhu)cf9c0462017-09-15 07:05:50 +0000292 FLEXCAN_QUIRK_USE_OFF_TIMESTAMP | FLEXCAN_QUIRK_BROKEN_PERR_STATE,
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200293};
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200294
Marc Kleine-Buddea3c11a72016-07-04 14:45:44 +0200295static const struct flexcan_devtype_data fsl_vf610_devtype_data = {
Marc Kleine-Budde9eb7aa82015-09-01 08:57:55 +0200296 .quirks = FLEXCAN_QUIRK_DISABLE_RXFG | FLEXCAN_QUIRK_ENABLE_EACEN_RRS |
Marc Kleine-Budde096de072015-09-01 10:28:46 +0200297 FLEXCAN_QUIRK_DISABLE_MECR | FLEXCAN_QUIRK_USE_OFF_TIMESTAMP,
Stefan Agnercdce8442014-07-15 14:56:21 +0200298};
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200299
Marc Kleine-Budde194b9a42012-07-16 12:58:31 +0200300static const struct can_bittiming_const flexcan_bittiming_const = {
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200301 .name = DRV_NAME,
302 .tseg1_min = 4,
303 .tseg1_max = 16,
304 .tseg2_min = 2,
305 .tseg2_max = 8,
306 .sjw_max = 4,
307 .brp_min = 1,
308 .brp_max = 256,
309 .brp_inc = 1,
310};
311
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200312/* Abstract off the read/write for arm versus ppc. This
Arnd Bergmann0e4b9492014-01-14 11:44:09 +0100313 * assumes that PPC uses big-endian registers and everything
314 * else uses little-endian registers, independent of CPU
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200315 * endianness.
holt@sgi.com61e271e2011-08-16 17:32:20 +0000316 */
Arnd Bergmann0e4b9492014-01-14 11:44:09 +0100317#if defined(CONFIG_PPC)
holt@sgi.com61e271e2011-08-16 17:32:20 +0000318static inline u32 flexcan_read(void __iomem *addr)
319{
320 return in_be32(addr);
321}
322
323static inline void flexcan_write(u32 val, void __iomem *addr)
324{
325 out_be32(addr, val);
326}
327#else
328static inline u32 flexcan_read(void __iomem *addr)
329{
330 return readl(addr);
331}
332
333static inline void flexcan_write(u32 val, void __iomem *addr)
334{
335 writel(val, addr);
336}
337#endif
338
ZHU Yi (ST-FIR/ENG1-Zhu)da49a802017-09-15 07:03:58 +0000339static inline void flexcan_error_irq_enable(const struct flexcan_priv *priv)
340{
341 struct flexcan_regs __iomem *regs = priv->regs;
342 u32 reg_ctrl = (priv->reg_ctrl_default | FLEXCAN_CTRL_ERR_MSK);
343
344 flexcan_write(reg_ctrl, &regs->ctrl);
345}
346
347static inline void flexcan_error_irq_disable(const struct flexcan_priv *priv)
348{
349 struct flexcan_regs __iomem *regs = priv->regs;
350 u32 reg_ctrl = (priv->reg_ctrl_default & ~FLEXCAN_CTRL_ERR_MSK);
351
352 flexcan_write(reg_ctrl, &regs->ctrl);
353}
354
Marc Kleine-Buddef0036982014-02-28 17:18:27 +0100355static inline int flexcan_transceiver_enable(const struct flexcan_priv *priv)
356{
357 if (!priv->reg_xceiver)
358 return 0;
359
360 return regulator_enable(priv->reg_xceiver);
361}
362
363static inline int flexcan_transceiver_disable(const struct flexcan_priv *priv)
364{
365 if (!priv->reg_xceiver)
366 return 0;
367
368 return regulator_disable(priv->reg_xceiver);
369}
370
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100371static int flexcan_chip_enable(struct flexcan_priv *priv)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200372{
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200373 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100374 unsigned int timeout = FLEXCAN_TIMEOUT_US / 10;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200375 u32 reg;
376
holt@sgi.com61e271e2011-08-16 17:32:20 +0000377 reg = flexcan_read(&regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200378 reg &= ~FLEXCAN_MCR_MDIS;
holt@sgi.com61e271e2011-08-16 17:32:20 +0000379 flexcan_write(reg, &regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200380
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100381 while (timeout-- && (flexcan_read(&regs->mcr) & FLEXCAN_MCR_LPM_ACK))
David Jander8badd652014-08-27 12:02:16 +0200382 udelay(10);
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100383
384 if (flexcan_read(&regs->mcr) & FLEXCAN_MCR_LPM_ACK)
385 return -ETIMEDOUT;
386
387 return 0;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200388}
389
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100390static int flexcan_chip_disable(struct flexcan_priv *priv)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200391{
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200392 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100393 unsigned int timeout = FLEXCAN_TIMEOUT_US / 10;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200394 u32 reg;
395
holt@sgi.com61e271e2011-08-16 17:32:20 +0000396 reg = flexcan_read(&regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200397 reg |= FLEXCAN_MCR_MDIS;
holt@sgi.com61e271e2011-08-16 17:32:20 +0000398 flexcan_write(reg, &regs->mcr);
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100399
400 while (timeout-- && !(flexcan_read(&regs->mcr) & FLEXCAN_MCR_LPM_ACK))
David Jander8badd652014-08-27 12:02:16 +0200401 udelay(10);
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100402
403 if (!(flexcan_read(&regs->mcr) & FLEXCAN_MCR_LPM_ACK))
404 return -ETIMEDOUT;
405
406 return 0;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200407}
408
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100409static int flexcan_chip_freeze(struct flexcan_priv *priv)
410{
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200411 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100412 unsigned int timeout = 1000 * 1000 * 10 / priv->can.bittiming.bitrate;
413 u32 reg;
414
415 reg = flexcan_read(&regs->mcr);
416 reg |= FLEXCAN_MCR_HALT;
417 flexcan_write(reg, &regs->mcr);
418
419 while (timeout-- && !(flexcan_read(&regs->mcr) & FLEXCAN_MCR_FRZ_ACK))
David Jander8badd652014-08-27 12:02:16 +0200420 udelay(100);
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100421
422 if (!(flexcan_read(&regs->mcr) & FLEXCAN_MCR_FRZ_ACK))
423 return -ETIMEDOUT;
424
425 return 0;
426}
427
428static int flexcan_chip_unfreeze(struct flexcan_priv *priv)
429{
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200430 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100431 unsigned int timeout = FLEXCAN_TIMEOUT_US / 10;
432 u32 reg;
433
434 reg = flexcan_read(&regs->mcr);
435 reg &= ~FLEXCAN_MCR_HALT;
436 flexcan_write(reg, &regs->mcr);
437
438 while (timeout-- && (flexcan_read(&regs->mcr) & FLEXCAN_MCR_FRZ_ACK))
David Jander8badd652014-08-27 12:02:16 +0200439 udelay(10);
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100440
441 if (flexcan_read(&regs->mcr) & FLEXCAN_MCR_FRZ_ACK)
442 return -ETIMEDOUT;
443
444 return 0;
445}
446
Marc Kleine-Budde4b5b8222014-02-28 15:16:59 +0100447static int flexcan_chip_softreset(struct flexcan_priv *priv)
448{
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200449 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Budde4b5b8222014-02-28 15:16:59 +0100450 unsigned int timeout = FLEXCAN_TIMEOUT_US / 10;
451
452 flexcan_write(FLEXCAN_MCR_SOFTRST, &regs->mcr);
453 while (timeout-- && (flexcan_read(&regs->mcr) & FLEXCAN_MCR_SOFTRST))
David Jander8badd652014-08-27 12:02:16 +0200454 udelay(10);
Marc Kleine-Budde4b5b8222014-02-28 15:16:59 +0100455
456 if (flexcan_read(&regs->mcr) & FLEXCAN_MCR_SOFTRST)
457 return -ETIMEDOUT;
458
459 return 0;
460}
461
Stefan Agnerec56acf2014-07-15 14:56:20 +0200462static int __flexcan_get_berr_counter(const struct net_device *dev,
463 struct can_berr_counter *bec)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200464{
465 const struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200466 struct flexcan_regs __iomem *regs = priv->regs;
holt@sgi.com61e271e2011-08-16 17:32:20 +0000467 u32 reg = flexcan_read(&regs->ecr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200468
469 bec->txerr = (reg >> 0) & 0xff;
470 bec->rxerr = (reg >> 8) & 0xff;
471
472 return 0;
473}
474
Stefan Agnerec56acf2014-07-15 14:56:20 +0200475static int flexcan_get_berr_counter(const struct net_device *dev,
476 struct can_berr_counter *bec)
477{
478 const struct flexcan_priv *priv = netdev_priv(dev);
479 int err;
480
481 err = clk_prepare_enable(priv->clk_ipg);
482 if (err)
483 return err;
484
485 err = clk_prepare_enable(priv->clk_per);
486 if (err)
487 goto out_disable_ipg;
488
489 err = __flexcan_get_berr_counter(dev, bec);
490
491 clk_disable_unprepare(priv->clk_per);
492 out_disable_ipg:
493 clk_disable_unprepare(priv->clk_ipg);
494
495 return err;
496}
497
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200498static int flexcan_start_xmit(struct sk_buff *skb, struct net_device *dev)
499{
500 const struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200501 struct can_frame *cf = (struct can_frame *)skb->data;
502 u32 can_id;
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200503 u32 data;
Marc Kleine-Budde10d089b2014-09-23 11:18:11 +0200504 u32 ctrl = FLEXCAN_MB_CODE_TX_DATA | (cf->can_dlc << 16);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200505
506 if (can_dropped_invalid_skb(dev, skb))
507 return NETDEV_TX_OK;
508
509 netif_stop_queue(dev);
510
511 if (cf->can_id & CAN_EFF_FLAG) {
512 can_id = cf->can_id & CAN_EFF_MASK;
513 ctrl |= FLEXCAN_MB_CNT_IDE | FLEXCAN_MB_CNT_SRR;
514 } else {
515 can_id = (cf->can_id & CAN_SFF_MASK) << 18;
516 }
517
518 if (cf->can_id & CAN_RTR_FLAG)
519 ctrl |= FLEXCAN_MB_CNT_RTR;
520
521 if (cf->can_dlc > 0) {
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200522 data = be32_to_cpup((__be32 *)&cf->data[0]);
Marc Kleine-Buddeb93917c2015-07-12 00:47:47 +0200523 flexcan_write(data, &priv->tx_mb->data[0]);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200524 }
525 if (cf->can_dlc > 3) {
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200526 data = be32_to_cpup((__be32 *)&cf->data[4]);
Marc Kleine-Buddeb93917c2015-07-12 00:47:47 +0200527 flexcan_write(data, &priv->tx_mb->data[1]);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200528 }
529
Reuben Dowle9a123492011-11-01 11:18:03 +1300530 can_put_echo_skb(skb, dev, 0);
531
Marc Kleine-Buddeb93917c2015-07-12 00:47:47 +0200532 flexcan_write(can_id, &priv->tx_mb->can_id);
533 flexcan_write(ctrl, &priv->tx_mb->can_ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200534
David Jander25e92442014-09-03 16:47:22 +0200535 /* Errata ERR005829 step8:
536 * Write twice INACTIVE(0x8) code to first MB.
537 */
538 flexcan_write(FLEXCAN_MB_CODE_TX_INACTIVE,
Marc Kleine-Buddeb93917c2015-07-12 00:47:47 +0200539 &priv->tx_mb_reserved->can_ctrl);
David Jander25e92442014-09-03 16:47:22 +0200540 flexcan_write(FLEXCAN_MB_CODE_TX_INACTIVE,
Marc Kleine-Buddeb93917c2015-07-12 00:47:47 +0200541 &priv->tx_mb_reserved->can_ctrl);
David Jander25e92442014-09-03 16:47:22 +0200542
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200543 return NETDEV_TX_OK;
544}
545
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200546static void flexcan_irq_bus_err(struct net_device *dev, u32 reg_esr)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200547{
548 struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Buddea5c02f662017-01-18 11:38:26 +0100549 struct sk_buff *skb;
550 struct can_frame *cf;
Marc Kleine-Budded166f562017-01-17 17:33:46 +0100551 bool rx_errors = false, tx_errors = false;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200552
Marc Kleine-Buddea5c02f662017-01-18 11:38:26 +0100553 skb = alloc_can_err_skb(dev, &cf);
554 if (unlikely(!skb))
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200555 return;
Marc Kleine-Buddea5c02f662017-01-18 11:38:26 +0100556
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200557 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
558
559 if (reg_esr & FLEXCAN_ESR_BIT1_ERR) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100560 netdev_dbg(dev, "BIT1_ERR irq\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200561 cf->data[2] |= CAN_ERR_PROT_BIT1;
Marc Kleine-Budded166f562017-01-17 17:33:46 +0100562 tx_errors = true;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200563 }
564 if (reg_esr & FLEXCAN_ESR_BIT0_ERR) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100565 netdev_dbg(dev, "BIT0_ERR irq\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200566 cf->data[2] |= CAN_ERR_PROT_BIT0;
Marc Kleine-Budded166f562017-01-17 17:33:46 +0100567 tx_errors = true;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200568 }
569 if (reg_esr & FLEXCAN_ESR_ACK_ERR) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100570 netdev_dbg(dev, "ACK_ERR irq\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200571 cf->can_id |= CAN_ERR_ACK;
Oliver Hartkoppffd461f2015-11-21 18:41:20 +0100572 cf->data[3] = CAN_ERR_PROT_LOC_ACK;
Marc Kleine-Budded166f562017-01-17 17:33:46 +0100573 tx_errors = true;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200574 }
575 if (reg_esr & FLEXCAN_ESR_CRC_ERR) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100576 netdev_dbg(dev, "CRC_ERR irq\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200577 cf->data[2] |= CAN_ERR_PROT_BIT;
Oliver Hartkoppffd461f2015-11-21 18:41:20 +0100578 cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ;
Marc Kleine-Budded166f562017-01-17 17:33:46 +0100579 rx_errors = true;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200580 }
581 if (reg_esr & FLEXCAN_ESR_FRM_ERR) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100582 netdev_dbg(dev, "FRM_ERR irq\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200583 cf->data[2] |= CAN_ERR_PROT_FORM;
Marc Kleine-Budded166f562017-01-17 17:33:46 +0100584 rx_errors = true;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200585 }
586 if (reg_esr & FLEXCAN_ESR_STF_ERR) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100587 netdev_dbg(dev, "STF_ERR irq\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200588 cf->data[2] |= CAN_ERR_PROT_STUFF;
Marc Kleine-Budded166f562017-01-17 17:33:46 +0100589 rx_errors = true;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200590 }
591
592 priv->can.can_stats.bus_error++;
593 if (rx_errors)
594 dev->stats.rx_errors++;
595 if (tx_errors)
596 dev->stats.tx_errors++;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200597
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200598 can_rx_offload_irq_queue_err_skb(&priv->offload, skb);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200599}
600
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200601static void flexcan_irq_state(struct net_device *dev, u32 reg_esr)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200602{
603 struct flexcan_priv *priv = netdev_priv(dev);
604 struct sk_buff *skb;
605 struct can_frame *cf;
Marc Kleine-Budde238443d2017-01-18 11:25:41 +0100606 enum can_state new_state, rx_state, tx_state;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200607 int flt;
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000608 struct can_berr_counter bec;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200609
610 flt = reg_esr & FLEXCAN_ESR_FLT_CONF_MASK;
611 if (likely(flt == FLEXCAN_ESR_FLT_CONF_ACTIVE)) {
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000612 tx_state = unlikely(reg_esr & FLEXCAN_ESR_TX_WRN) ?
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200613 CAN_STATE_ERROR_WARNING : CAN_STATE_ERROR_ACTIVE;
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000614 rx_state = unlikely(reg_esr & FLEXCAN_ESR_RX_WRN) ?
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200615 CAN_STATE_ERROR_WARNING : CAN_STATE_ERROR_ACTIVE;
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000616 new_state = max(tx_state, rx_state);
Andri Yngvason258ce802015-03-17 13:03:09 +0000617 } else {
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000618 __flexcan_get_berr_counter(dev, &bec);
Andri Yngvason258ce802015-03-17 13:03:09 +0000619 new_state = flt == FLEXCAN_ESR_FLT_CONF_PASSIVE ?
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200620 CAN_STATE_ERROR_PASSIVE : CAN_STATE_BUS_OFF;
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000621 rx_state = bec.rxerr >= bec.txerr ? new_state : 0;
622 tx_state = bec.rxerr <= bec.txerr ? new_state : 0;
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000623 }
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200624
625 /* state hasn't changed */
626 if (likely(new_state == priv->can.state))
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200627 return;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200628
629 skb = alloc_can_err_skb(dev, &cf);
630 if (unlikely(!skb))
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200631 return;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200632
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000633 can_change_state(dev, cf, tx_state, rx_state);
634
635 if (unlikely(new_state == CAN_STATE_BUS_OFF))
636 can_bus_off(dev);
637
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200638 can_rx_offload_irq_queue_err_skb(&priv->offload, skb);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200639}
640
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200641static inline struct flexcan_priv *rx_offload_to_priv(struct can_rx_offload *offload)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200642{
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200643 return container_of(offload, struct flexcan_priv, offload);
644}
645
646static unsigned int flexcan_mailbox_read(struct can_rx_offload *offload,
647 struct can_frame *cf,
648 u32 *timestamp, unsigned int n)
649{
650 struct flexcan_priv *priv = rx_offload_to_priv(offload);
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200651 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200652 struct flexcan_mb __iomem *mb = &regs->mb[n];
653 u32 reg_ctrl, reg_id, reg_iflag1;
654
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200655 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP) {
656 u32 code;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200657
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200658 do {
659 reg_ctrl = flexcan_read(&mb->can_ctrl);
660 } while (reg_ctrl & FLEXCAN_MB_CODE_RX_BUSY_BIT);
661
662 /* is this MB empty? */
663 code = reg_ctrl & FLEXCAN_MB_CODE_MASK;
664 if ((code != FLEXCAN_MB_CODE_RX_FULL) &&
665 (code != FLEXCAN_MB_CODE_RX_OVERRUN))
666 return 0;
667
668 if (code == FLEXCAN_MB_CODE_RX_OVERRUN) {
669 /* This MB was overrun, we lost data */
670 offload->dev->stats.rx_over_errors++;
671 offload->dev->stats.rx_errors++;
672 }
673 } else {
674 reg_iflag1 = flexcan_read(&regs->iflag1);
675 if (!(reg_iflag1 & FLEXCAN_IFLAG_RX_FIFO_AVAILABLE))
676 return 0;
677
678 reg_ctrl = flexcan_read(&mb->can_ctrl);
679 }
680
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200681 /* increase timstamp to full 32 bit */
682 *timestamp = reg_ctrl << 16;
683
holt@sgi.com61e271e2011-08-16 17:32:20 +0000684 reg_id = flexcan_read(&mb->can_id);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200685 if (reg_ctrl & FLEXCAN_MB_CNT_IDE)
686 cf->can_id = ((reg_id >> 0) & CAN_EFF_MASK) | CAN_EFF_FLAG;
687 else
688 cf->can_id = (reg_id >> 18) & CAN_SFF_MASK;
689
690 if (reg_ctrl & FLEXCAN_MB_CNT_RTR)
691 cf->can_id |= CAN_RTR_FLAG;
692 cf->can_dlc = get_can_dlc((reg_ctrl >> 16) & 0xf);
693
holt@sgi.com61e271e2011-08-16 17:32:20 +0000694 *(__be32 *)(cf->data + 0) = cpu_to_be32(flexcan_read(&mb->data[0]));
695 *(__be32 *)(cf->data + 4) = cpu_to_be32(flexcan_read(&mb->data[1]));
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200696
697 /* mark as read */
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200698 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP) {
699 /* Clear IRQ */
700 if (n < 32)
701 flexcan_write(BIT(n), &regs->iflag1);
702 else
703 flexcan_write(BIT(n - 32), &regs->iflag2);
704 } else {
705 flexcan_write(FLEXCAN_IFLAG_RX_FIFO_AVAILABLE, &regs->iflag1);
706 flexcan_read(&regs->timer);
707 }
Fabio Baltieriadccadb2012-12-18 18:50:58 +0100708
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200709 return 1;
710}
711
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200712
713static inline u64 flexcan_read_reg_iflag_rx(struct flexcan_priv *priv)
714{
715 struct flexcan_regs __iomem *regs = priv->regs;
716 u32 iflag1, iflag2;
717
718 iflag2 = flexcan_read(&regs->iflag2) & priv->reg_imask2_default;
719 iflag1 = flexcan_read(&regs->iflag1) & priv->reg_imask1_default &
720 ~FLEXCAN_IFLAG_MB(priv->tx_mb_idx);
721
722 return (u64)iflag2 << 32 | iflag1;
723}
724
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200725static irqreturn_t flexcan_irq(int irq, void *dev_id)
726{
727 struct net_device *dev = dev_id;
728 struct net_device_stats *stats = &dev->stats;
729 struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200730 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddedd2f1222017-01-18 11:45:14 +0100731 irqreturn_t handled = IRQ_NONE;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200732 u32 reg_iflag1, reg_esr;
ZHU Yi (ST-FIR/ENG1-Zhu)da49a802017-09-15 07:03:58 +0000733 enum can_state last_state = priv->can.state;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200734
holt@sgi.com61e271e2011-08-16 17:32:20 +0000735 reg_iflag1 = flexcan_read(&regs->iflag1);
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200736
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200737 /* reception interrupt */
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200738 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP) {
739 u64 reg_iflag;
740 int ret;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200741
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200742 while ((reg_iflag = flexcan_read_reg_iflag_rx(priv))) {
743 handled = IRQ_HANDLED;
744 ret = can_rx_offload_irq_offload_timestamp(&priv->offload,
745 reg_iflag);
746 if (!ret)
747 break;
748 }
749 } else {
750 if (reg_iflag1 & FLEXCAN_IFLAG_RX_FIFO_AVAILABLE) {
751 handled = IRQ_HANDLED;
752 can_rx_offload_irq_offload_fifo(&priv->offload);
753 }
754
755 /* FIFO overflow interrupt */
756 if (reg_iflag1 & FLEXCAN_IFLAG_RX_FIFO_OVERFLOW) {
757 handled = IRQ_HANDLED;
758 flexcan_write(FLEXCAN_IFLAG_RX_FIFO_OVERFLOW, &regs->iflag1);
759 dev->stats.rx_over_errors++;
760 dev->stats.rx_errors++;
761 }
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200762 }
763
764 /* transmission complete interrupt */
Marc Kleine-Buddeb93917c2015-07-12 00:47:47 +0200765 if (reg_iflag1 & FLEXCAN_IFLAG_MB(priv->tx_mb_idx)) {
Marc Kleine-Buddedd2f1222017-01-18 11:45:14 +0100766 handled = IRQ_HANDLED;
Reuben Dowle9a123492011-11-01 11:18:03 +1300767 stats->tx_bytes += can_get_echo_skb(dev, 0);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200768 stats->tx_packets++;
Fabio Baltieriadccadb2012-12-18 18:50:58 +0100769 can_led_event(dev, CAN_LED_EVENT_TX);
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200770
771 /* after sending a RTR frame MB is in RX mode */
Marc Kleine-Buddede594482014-09-16 15:31:27 +0200772 flexcan_write(FLEXCAN_MB_CODE_TX_INACTIVE,
Marc Kleine-Buddeb93917c2015-07-12 00:47:47 +0200773 &priv->tx_mb->can_ctrl);
774 flexcan_write(FLEXCAN_IFLAG_MB(priv->tx_mb_idx), &regs->iflag1);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200775 netif_wake_queue(dev);
776 }
777
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200778 reg_esr = flexcan_read(&regs->esr);
779
Marc Kleine-Buddedd2f1222017-01-18 11:45:14 +0100780 /* ACK all bus error and state change IRQ sources */
781 if (reg_esr & FLEXCAN_ESR_ALL_INT) {
782 handled = IRQ_HANDLED;
783 flexcan_write(reg_esr & FLEXCAN_ESR_ALL_INT, &regs->esr);
784 }
785
ZHU Yi (ST-FIR/ENG1-Zhu)ad230232017-09-15 06:59:15 +0000786 /* state change interrupt or broken error state quirk fix is enabled */
787 if ((reg_esr & FLEXCAN_ESR_ERR_STATE) ||
ZHU Yi (ST-FIR/ENG1-Zhu)da49a802017-09-15 07:03:58 +0000788 (priv->devtype_data->quirks & (FLEXCAN_QUIRK_BROKEN_WERR_STATE |
789 FLEXCAN_QUIRK_BROKEN_PERR_STATE)))
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200790 flexcan_irq_state(dev, reg_esr);
791
792 /* bus error IRQ - handle if bus error reporting is activated */
793 if ((reg_esr & FLEXCAN_ESR_ERR_BUS) &&
794 (priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING))
795 flexcan_irq_bus_err(dev, reg_esr);
796
ZHU Yi (ST-FIR/ENG1-Zhu)da49a802017-09-15 07:03:58 +0000797 /* availability of error interrupt among state transitions in case
798 * bus error reporting is de-activated and
799 * FLEXCAN_QUIRK_BROKEN_PERR_STATE is enabled:
800 * +--------------------------------------------------------------+
801 * | +----------------------------------------------+ [stopped / |
802 * | | | sleeping] -+
803 * +-+-> active <-> warning <-> passive -> bus off -+
804 * ___________^^^^^^^^^^^^_______________________________
805 * disabled(1) enabled disabled
806 *
807 * (1): enabled if FLEXCAN_QUIRK_BROKEN_WERR_STATE is enabled
808 */
809 if ((last_state != priv->can.state) &&
810 (priv->devtype_data->quirks & FLEXCAN_QUIRK_BROKEN_PERR_STATE) &&
811 !(priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING)) {
812 switch (priv->can.state) {
813 case CAN_STATE_ERROR_ACTIVE:
814 if (priv->devtype_data->quirks &
815 FLEXCAN_QUIRK_BROKEN_WERR_STATE)
816 flexcan_error_irq_enable(priv);
817 else
818 flexcan_error_irq_disable(priv);
819 break;
820
821 case CAN_STATE_ERROR_WARNING:
822 flexcan_error_irq_enable(priv);
823 break;
824
825 case CAN_STATE_ERROR_PASSIVE:
826 case CAN_STATE_BUS_OFF:
827 flexcan_error_irq_disable(priv);
828 break;
829
830 default:
831 break;
832 }
833 }
834
Marc Kleine-Buddedd2f1222017-01-18 11:45:14 +0100835 return handled;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200836}
837
838static void flexcan_set_bittiming(struct net_device *dev)
839{
840 const struct flexcan_priv *priv = netdev_priv(dev);
841 const struct can_bittiming *bt = &priv->can.bittiming;
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200842 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200843 u32 reg;
844
holt@sgi.com61e271e2011-08-16 17:32:20 +0000845 reg = flexcan_read(&regs->ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200846 reg &= ~(FLEXCAN_CTRL_PRESDIV(0xff) |
847 FLEXCAN_CTRL_RJW(0x3) |
848 FLEXCAN_CTRL_PSEG1(0x7) |
849 FLEXCAN_CTRL_PSEG2(0x7) |
850 FLEXCAN_CTRL_PROPSEG(0x7) |
851 FLEXCAN_CTRL_LPB |
852 FLEXCAN_CTRL_SMP |
853 FLEXCAN_CTRL_LOM);
854
855 reg |= FLEXCAN_CTRL_PRESDIV(bt->brp - 1) |
856 FLEXCAN_CTRL_PSEG1(bt->phase_seg1 - 1) |
857 FLEXCAN_CTRL_PSEG2(bt->phase_seg2 - 1) |
858 FLEXCAN_CTRL_RJW(bt->sjw - 1) |
859 FLEXCAN_CTRL_PROPSEG(bt->prop_seg - 1);
860
861 if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)
862 reg |= FLEXCAN_CTRL_LPB;
863 if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
864 reg |= FLEXCAN_CTRL_LOM;
865 if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
866 reg |= FLEXCAN_CTRL_SMP;
867
Lucas Stach7a4b6c82015-08-07 17:16:03 +0200868 netdev_dbg(dev, "writing ctrl=0x%08x\n", reg);
holt@sgi.com61e271e2011-08-16 17:32:20 +0000869 flexcan_write(reg, &regs->ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200870
871 /* print chip status */
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100872 netdev_dbg(dev, "%s: mcr=0x%08x ctrl=0x%08x\n", __func__,
873 flexcan_read(&regs->mcr), flexcan_read(&regs->ctrl));
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200874}
875
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200876/* flexcan_chip_start
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200877 *
878 * this functions is entered with clocks enabled
879 *
880 */
881static int flexcan_chip_start(struct net_device *dev)
882{
883 struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200884 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Budde6f75fce2014-09-23 11:03:01 +0200885 u32 reg_mcr, reg_ctrl, reg_ctrl2, reg_mecr;
David S. Miller1f6d8032014-09-23 12:09:27 -0400886 int err, i;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200887
888 /* enable module */
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100889 err = flexcan_chip_enable(priv);
890 if (err)
891 return err;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200892
893 /* soft reset */
Marc Kleine-Budde4b5b8222014-02-28 15:16:59 +0100894 err = flexcan_chip_softreset(priv);
895 if (err)
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100896 goto out_chip_disable;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200897
898 flexcan_set_bittiming(dev);
899
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200900 /* MCR
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200901 *
902 * enable freeze
903 * enable fifo
904 * halt now
905 * only supervisor access
906 * enable warning int
Reuben Dowle9a123492011-11-01 11:18:03 +1300907 * disable local echo
Marc Kleine-Budde4bd888a2015-08-31 21:03:29 +0200908 * enable individual RX masking
Marc Kleine-Budde749de6f2015-08-31 21:32:34 +0200909 * choose format C
910 * set max mailbox number
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200911 */
holt@sgi.com61e271e2011-08-16 17:32:20 +0000912 reg_mcr = flexcan_read(&regs->mcr);
Marc Kleine-Budded5a7b402013-10-04 10:52:36 +0200913 reg_mcr &= ~FLEXCAN_MCR_MAXMB(0xff);
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200914 reg_mcr |= FLEXCAN_MCR_FRZ | FLEXCAN_MCR_HALT | FLEXCAN_MCR_SUPV |
915 FLEXCAN_MCR_WRN_EN | FLEXCAN_MCR_SRX_DIS | FLEXCAN_MCR_IRMQ |
916 FLEXCAN_MCR_IDAM_C;
917
918 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP) {
919 reg_mcr &= ~FLEXCAN_MCR_FEN;
920 reg_mcr |= FLEXCAN_MCR_MAXMB(priv->offload.mb_last);
921 } else {
922 reg_mcr |= FLEXCAN_MCR_FEN |
923 FLEXCAN_MCR_MAXMB(priv->tx_mb_idx);
924 }
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100925 netdev_dbg(dev, "%s: writing mcr=0x%08x", __func__, reg_mcr);
holt@sgi.com61e271e2011-08-16 17:32:20 +0000926 flexcan_write(reg_mcr, &regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200927
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200928 /* CTRL
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200929 *
930 * disable timer sync feature
931 *
932 * disable auto busoff recovery
933 * transmit lowest buffer first
934 *
935 * enable tx and rx warning interrupt
936 * enable bus off interrupt
937 * (== FLEXCAN_CTRL_ERR_STATE)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200938 */
holt@sgi.com61e271e2011-08-16 17:32:20 +0000939 reg_ctrl = flexcan_read(&regs->ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200940 reg_ctrl &= ~FLEXCAN_CTRL_TSYN;
941 reg_ctrl |= FLEXCAN_CTRL_BOFF_REC | FLEXCAN_CTRL_LBUF |
Wolfgang Grandegger4f72e5f2012-09-28 03:17:15 +0000942 FLEXCAN_CTRL_ERR_STATE;
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200943
944 /* enable the "error interrupt" (FLEXCAN_CTRL_ERR_MSK),
Wolfgang Grandegger4f72e5f2012-09-28 03:17:15 +0000945 * on most Flexcan cores, too. Otherwise we don't get
946 * any error warning or passive interrupts.
947 */
ZHU Yi (ST-FIR/ENG1-Zhu)2f8639b2017-09-15 07:01:23 +0000948 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_BROKEN_WERR_STATE ||
Wolfgang Grandegger4f72e5f2012-09-28 03:17:15 +0000949 priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING)
950 reg_ctrl |= FLEXCAN_CTRL_ERR_MSK;
Alexander Steinbc03a542014-08-12 10:47:21 +0200951 else
952 reg_ctrl &= ~FLEXCAN_CTRL_ERR_MSK;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200953
954 /* save for later use */
955 priv->reg_ctrl_default = reg_ctrl;
Marc Kleine-Budde6fa7da22015-08-27 14:24:48 +0200956 /* leave interrupts disabled for now */
957 reg_ctrl &= ~FLEXCAN_CTRL_ERR_ALL;
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100958 netdev_dbg(dev, "%s: writing ctrl=0x%08x", __func__, reg_ctrl);
holt@sgi.com61e271e2011-08-16 17:32:20 +0000959 flexcan_write(reg_ctrl, &regs->ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200960
Marc Kleine-Budde9eb7aa82015-09-01 08:57:55 +0200961 if ((priv->devtype_data->quirks & FLEXCAN_QUIRK_ENABLE_EACEN_RRS)) {
962 reg_ctrl2 = flexcan_read(&regs->ctrl2);
963 reg_ctrl2 |= FLEXCAN_CTRL2_EACEN | FLEXCAN_CTRL2_RRS;
964 flexcan_write(reg_ctrl2, &regs->ctrl2);
965 }
966
David Janderfc05b882014-08-27 11:58:05 +0200967 /* clear and invalidate all mailboxes first */
Marc Kleine-Buddeb93917c2015-07-12 00:47:47 +0200968 for (i = priv->tx_mb_idx; i < ARRAY_SIZE(regs->mb); i++) {
David Janderfc05b882014-08-27 11:58:05 +0200969 flexcan_write(FLEXCAN_MB_CODE_RX_INACTIVE,
Marc Kleine-Budde1ba763d2015-08-25 10:39:19 +0200970 &regs->mb[i].can_ctrl);
David Janderfc05b882014-08-27 11:58:05 +0200971 }
972
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200973 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP) {
974 for (i = priv->offload.mb_first; i <= priv->offload.mb_last; i++)
975 flexcan_write(FLEXCAN_MB_CODE_RX_EMPTY,
976 &regs->mb[i].can_ctrl);
977 }
978
David Jander25e92442014-09-03 16:47:22 +0200979 /* Errata ERR005829: mark first TX mailbox as INACTIVE */
980 flexcan_write(FLEXCAN_MB_CODE_TX_INACTIVE,
Marc Kleine-Buddeb93917c2015-07-12 00:47:47 +0200981 &priv->tx_mb_reserved->can_ctrl);
David Jander25e92442014-09-03 16:47:22 +0200982
Marc Kleine-Buddec32fe4a2014-09-16 12:39:28 +0200983 /* mark TX mailbox as INACTIVE */
984 flexcan_write(FLEXCAN_MB_CODE_TX_INACTIVE,
Marc Kleine-Buddeb93917c2015-07-12 00:47:47 +0200985 &priv->tx_mb->can_ctrl);
Marc Kleine-Budded5a7b402013-10-04 10:52:36 +0200986
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200987 /* acceptance mask/acceptance code (accept everything) */
holt@sgi.com61e271e2011-08-16 17:32:20 +0000988 flexcan_write(0x0, &regs->rxgmask);
989 flexcan_write(0x0, &regs->rx14mask);
990 flexcan_write(0x0, &regs->rx15mask);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200991
Marc Kleine-Buddef377bff2015-05-08 15:22:36 +0200992 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_DISABLE_RXFG)
Hui Wang30c1e672012-06-28 16:21:35 +0800993 flexcan_write(0x0, &regs->rxfgmask);
994
Marc Kleine-Budde4bd888a2015-08-31 21:03:29 +0200995 /* clear acceptance filters */
996 for (i = 0; i < ARRAY_SIZE(regs->mb); i++)
997 flexcan_write(0, &regs->rximr[i]);
998
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200999 /* On Vybrid, disable memory error detection interrupts
Stefan Agnercdce8442014-07-15 14:56:21 +02001000 * and freeze mode.
1001 * This also works around errata e5295 which generates
1002 * false positive memory errors and put the device in
1003 * freeze mode.
1004 */
Marc Kleine-Buddef377bff2015-05-08 15:22:36 +02001005 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_DISABLE_MECR) {
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +02001006 /* Follow the protocol as described in "Detection
Stefan Agnercdce8442014-07-15 14:56:21 +02001007 * and Correction of Memory Errors" to write to
1008 * MECR register
1009 */
Marc Kleine-Budde6f75fce2014-09-23 11:03:01 +02001010 reg_ctrl2 = flexcan_read(&regs->ctrl2);
1011 reg_ctrl2 |= FLEXCAN_CTRL2_ECRWRE;
1012 flexcan_write(reg_ctrl2, &regs->ctrl2);
Stefan Agnercdce8442014-07-15 14:56:21 +02001013
1014 reg_mecr = flexcan_read(&regs->mecr);
1015 reg_mecr &= ~FLEXCAN_MECR_ECRWRDIS;
1016 flexcan_write(reg_mecr, &regs->mecr);
1017 reg_mecr &= ~(FLEXCAN_MECR_NCEFAFRZ | FLEXCAN_MECR_HANCEI_MSK |
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +02001018 FLEXCAN_MECR_FANCEI_MSK);
Stefan Agnercdce8442014-07-15 14:56:21 +02001019 flexcan_write(reg_mecr, &regs->mecr);
1020 }
1021
Marc Kleine-Buddef0036982014-02-28 17:18:27 +01001022 err = flexcan_transceiver_enable(priv);
1023 if (err)
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +01001024 goto out_chip_disable;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001025
1026 /* synchronize with the can bus */
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +01001027 err = flexcan_chip_unfreeze(priv);
1028 if (err)
1029 goto out_transceiver_disable;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001030
1031 priv->can.state = CAN_STATE_ERROR_ACTIVE;
1032
Marc Kleine-Budde6fa7da22015-08-27 14:24:48 +02001033 /* enable interrupts atomically */
1034 disable_irq(dev->irq);
1035 flexcan_write(priv->reg_ctrl_default, &regs->ctrl);
Marc Kleine-Budde28ac7dc2015-08-04 13:46:10 +02001036 flexcan_write(priv->reg_imask1_default, &regs->imask1);
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +02001037 flexcan_write(priv->reg_imask2_default, &regs->imask2);
Marc Kleine-Budde6fa7da22015-08-27 14:24:48 +02001038 enable_irq(dev->irq);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001039
1040 /* print chip status */
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +01001041 netdev_dbg(dev, "%s: reading mcr=0x%08x ctrl=0x%08x\n", __func__,
1042 flexcan_read(&regs->mcr), flexcan_read(&regs->ctrl));
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001043
1044 return 0;
1045
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +01001046 out_transceiver_disable:
1047 flexcan_transceiver_disable(priv);
1048 out_chip_disable:
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001049 flexcan_chip_disable(priv);
1050 return err;
1051}
1052
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +02001053/* flexcan_chip_stop
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001054 *
1055 * this functions is entered with clocks enabled
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001056 */
1057static void flexcan_chip_stop(struct net_device *dev)
1058{
1059 struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Budde89af8742015-05-08 09:32:58 +02001060 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001061
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +01001062 /* freeze + disable module */
1063 flexcan_chip_freeze(priv);
1064 flexcan_chip_disable(priv);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001065
Marc Kleine-Budde5be93bd2014-02-19 12:00:51 +01001066 /* Disable all interrupts */
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +02001067 flexcan_write(0, &regs->imask2);
Marc Kleine-Budde5be93bd2014-02-19 12:00:51 +01001068 flexcan_write(0, &regs->imask1);
1069 flexcan_write(priv->reg_ctrl_default & ~FLEXCAN_CTRL_ERR_ALL,
1070 &regs->ctrl);
1071
Marc Kleine-Buddef0036982014-02-28 17:18:27 +01001072 flexcan_transceiver_disable(priv);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001073 priv->can.state = CAN_STATE_STOPPED;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001074}
1075
1076static int flexcan_open(struct net_device *dev)
1077{
1078 struct flexcan_priv *priv = netdev_priv(dev);
1079 int err;
1080
Fabio Estevamaa101812013-07-22 12:41:40 -03001081 err = clk_prepare_enable(priv->clk_ipg);
1082 if (err)
1083 return err;
1084
1085 err = clk_prepare_enable(priv->clk_per);
1086 if (err)
1087 goto out_disable_ipg;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001088
1089 err = open_candev(dev);
1090 if (err)
Fabio Estevamaa101812013-07-22 12:41:40 -03001091 goto out_disable_per;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001092
1093 err = request_irq(dev->irq, flexcan_irq, IRQF_SHARED, dev->name, dev);
1094 if (err)
1095 goto out_close;
1096
1097 /* start chip and queuing */
1098 err = flexcan_chip_start(dev);
1099 if (err)
Marc Kleine-Budde7e9e1482014-02-28 14:52:01 +01001100 goto out_free_irq;
Fabio Baltieriadccadb2012-12-18 18:50:58 +01001101
1102 can_led_event(dev, CAN_LED_EVENT_OPEN);
1103
Marc Kleine-Budde30164752015-05-10 15:26:58 +02001104 can_rx_offload_enable(&priv->offload);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001105 netif_start_queue(dev);
1106
1107 return 0;
1108
Marc Kleine-Budde7e9e1482014-02-28 14:52:01 +01001109 out_free_irq:
1110 free_irq(dev->irq, dev);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001111 out_close:
1112 close_candev(dev);
Fabio Estevamaa101812013-07-22 12:41:40 -03001113 out_disable_per:
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001114 clk_disable_unprepare(priv->clk_per);
Fabio Estevamaa101812013-07-22 12:41:40 -03001115 out_disable_ipg:
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001116 clk_disable_unprepare(priv->clk_ipg);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001117
1118 return err;
1119}
1120
1121static int flexcan_close(struct net_device *dev)
1122{
1123 struct flexcan_priv *priv = netdev_priv(dev);
1124
1125 netif_stop_queue(dev);
Marc Kleine-Budde30164752015-05-10 15:26:58 +02001126 can_rx_offload_disable(&priv->offload);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001127 flexcan_chip_stop(dev);
1128
1129 free_irq(dev->irq, dev);
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001130 clk_disable_unprepare(priv->clk_per);
1131 clk_disable_unprepare(priv->clk_ipg);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001132
1133 close_candev(dev);
1134
Fabio Baltieriadccadb2012-12-18 18:50:58 +01001135 can_led_event(dev, CAN_LED_EVENT_STOP);
1136
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001137 return 0;
1138}
1139
1140static int flexcan_set_mode(struct net_device *dev, enum can_mode mode)
1141{
1142 int err;
1143
1144 switch (mode) {
1145 case CAN_MODE_START:
1146 err = flexcan_chip_start(dev);
1147 if (err)
1148 return err;
1149
1150 netif_wake_queue(dev);
1151 break;
1152
1153 default:
1154 return -EOPNOTSUPP;
1155 }
1156
1157 return 0;
1158}
1159
1160static const struct net_device_ops flexcan_netdev_ops = {
1161 .ndo_open = flexcan_open,
1162 .ndo_stop = flexcan_close,
1163 .ndo_start_xmit = flexcan_start_xmit,
Oliver Hartkoppc971fa22014-03-07 09:23:41 +01001164 .ndo_change_mtu = can_change_mtu,
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001165};
1166
Bill Pemberton3c8ac0f2012-12-03 09:22:44 -05001167static int register_flexcandev(struct net_device *dev)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001168{
1169 struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Budde89af8742015-05-08 09:32:58 +02001170 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001171 u32 reg, err;
1172
Fabio Estevamaa101812013-07-22 12:41:40 -03001173 err = clk_prepare_enable(priv->clk_ipg);
1174 if (err)
1175 return err;
1176
1177 err = clk_prepare_enable(priv->clk_per);
1178 if (err)
1179 goto out_disable_ipg;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001180
1181 /* select "bus clock", chip must be disabled */
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001182 err = flexcan_chip_disable(priv);
1183 if (err)
1184 goto out_disable_per;
holt@sgi.com61e271e2011-08-16 17:32:20 +00001185 reg = flexcan_read(&regs->ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001186 reg |= FLEXCAN_CTRL_CLK_SRC;
holt@sgi.com61e271e2011-08-16 17:32:20 +00001187 flexcan_write(reg, &regs->ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001188
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001189 err = flexcan_chip_enable(priv);
1190 if (err)
1191 goto out_chip_disable;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001192
1193 /* set freeze, halt and activate FIFO, restrict register access */
holt@sgi.com61e271e2011-08-16 17:32:20 +00001194 reg = flexcan_read(&regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001195 reg |= FLEXCAN_MCR_FRZ | FLEXCAN_MCR_HALT |
1196 FLEXCAN_MCR_FEN | FLEXCAN_MCR_SUPV;
holt@sgi.com61e271e2011-08-16 17:32:20 +00001197 flexcan_write(reg, &regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001198
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +02001199 /* Currently we only support newer versions of this core
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +02001200 * featuring a RX hardware FIFO (although this driver doesn't
1201 * make use of it on some cores). Older cores, found on some
1202 * Coldfire derivates are not tested.
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001203 */
holt@sgi.com61e271e2011-08-16 17:32:20 +00001204 reg = flexcan_read(&regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001205 if (!(reg & FLEXCAN_MCR_FEN)) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +01001206 netdev_err(dev, "Could not enable RX FIFO, unsupported core\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001207 err = -ENODEV;
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001208 goto out_chip_disable;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001209 }
1210
1211 err = register_candev(dev);
1212
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001213 /* disable core and turn off clocks */
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001214 out_chip_disable:
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001215 flexcan_chip_disable(priv);
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001216 out_disable_per:
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001217 clk_disable_unprepare(priv->clk_per);
Fabio Estevamaa101812013-07-22 12:41:40 -03001218 out_disable_ipg:
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001219 clk_disable_unprepare(priv->clk_ipg);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001220
1221 return err;
1222}
1223
Bill Pemberton3c8ac0f2012-12-03 09:22:44 -05001224static void unregister_flexcandev(struct net_device *dev)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001225{
1226 unregister_candev(dev);
1227}
1228
Hui Wang30c1e672012-06-28 16:21:35 +08001229static const struct of_device_id flexcan_of_match[] = {
Hui Wang30c1e672012-06-28 16:21:35 +08001230 { .compatible = "fsl,imx6q-flexcan", .data = &fsl_imx6q_devtype_data, },
Marc Kleine-Buddee3587842013-10-03 23:51:55 +02001231 { .compatible = "fsl,imx28-flexcan", .data = &fsl_imx28_devtype_data, },
1232 { .compatible = "fsl,p1010-flexcan", .data = &fsl_p1010_devtype_data, },
Stefan Agnercdce8442014-07-15 14:56:21 +02001233 { .compatible = "fsl,vf610-flexcan", .data = &fsl_vf610_devtype_data, },
Hui Wang30c1e672012-06-28 16:21:35 +08001234 { /* sentinel */ },
1235};
Marc Kleine-Budde4358a9d2012-10-04 10:55:35 +02001236MODULE_DEVICE_TABLE(of, flexcan_of_match);
Hui Wang30c1e672012-06-28 16:21:35 +08001237
1238static const struct platform_device_id flexcan_id_table[] = {
1239 { .name = "flexcan", .driver_data = (kernel_ulong_t)&fsl_p1010_devtype_data, },
1240 { /* sentinel */ },
1241};
Marc Kleine-Budde4358a9d2012-10-04 10:55:35 +02001242MODULE_DEVICE_TABLE(platform, flexcan_id_table);
Hui Wang30c1e672012-06-28 16:21:35 +08001243
Bill Pemberton3c8ac0f2012-12-03 09:22:44 -05001244static int flexcan_probe(struct platform_device *pdev)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001245{
Hui Wang30c1e672012-06-28 16:21:35 +08001246 const struct of_device_id *of_id;
Marc Kleine-Buddedda0b3b2012-07-13 14:52:48 +02001247 const struct flexcan_devtype_data *devtype_data;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001248 struct net_device *dev;
1249 struct flexcan_priv *priv;
Andreas Werner555828e2015-03-22 17:35:52 +01001250 struct regulator *reg_xceiver;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001251 struct resource *mem;
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001252 struct clk *clk_ipg = NULL, *clk_per = NULL;
Marc Kleine-Budde89af8742015-05-08 09:32:58 +02001253 struct flexcan_regs __iomem *regs;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001254 int err, irq;
holt@sgi.com97efe9a2011-08-16 17:32:23 +00001255 u32 clock_freq = 0;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001256
Andreas Werner555828e2015-03-22 17:35:52 +01001257 reg_xceiver = devm_regulator_get(&pdev->dev, "xceiver");
1258 if (PTR_ERR(reg_xceiver) == -EPROBE_DEFER)
1259 return -EPROBE_DEFER;
1260 else if (IS_ERR(reg_xceiver))
1261 reg_xceiver = NULL;
1262
Hui Wangafc016d2012-06-28 16:21:34 +08001263 if (pdev->dev.of_node)
1264 of_property_read_u32(pdev->dev.of_node,
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +02001265 "clock-frequency", &clock_freq);
holt@sgi.com97efe9a2011-08-16 17:32:23 +00001266
1267 if (!clock_freq) {
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001268 clk_ipg = devm_clk_get(&pdev->dev, "ipg");
1269 if (IS_ERR(clk_ipg)) {
1270 dev_err(&pdev->dev, "no ipg clock defined\n");
Fabio Estevam933e4af2013-07-22 12:41:39 -03001271 return PTR_ERR(clk_ipg);
holt@sgi.com97efe9a2011-08-16 17:32:23 +00001272 }
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001273
1274 clk_per = devm_clk_get(&pdev->dev, "per");
1275 if (IS_ERR(clk_per)) {
1276 dev_err(&pdev->dev, "no per clock defined\n");
Fabio Estevam933e4af2013-07-22 12:41:39 -03001277 return PTR_ERR(clk_per);
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001278 }
Marc Kleine-Budde1a3e5172013-11-25 22:15:20 +01001279 clock_freq = clk_get_rate(clk_per);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001280 }
1281
1282 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1283 irq = platform_get_irq(pdev, 0);
Fabio Estevam933e4af2013-07-22 12:41:39 -03001284 if (irq <= 0)
1285 return -ENODEV;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001286
Marc Kleine-Budde89af8742015-05-08 09:32:58 +02001287 regs = devm_ioremap_resource(&pdev->dev, mem);
1288 if (IS_ERR(regs))
1289 return PTR_ERR(regs);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001290
Hui Wang30c1e672012-06-28 16:21:35 +08001291 of_id = of_match_device(flexcan_of_match, &pdev->dev);
1292 if (of_id) {
1293 devtype_data = of_id->data;
Marc Kleine-Budded0873e62014-03-04 22:04:22 +01001294 } else if (platform_get_device_id(pdev)->driver_data) {
Hui Wang30c1e672012-06-28 16:21:35 +08001295 devtype_data = (struct flexcan_devtype_data *)
Marc Kleine-Budded0873e62014-03-04 22:04:22 +01001296 platform_get_device_id(pdev)->driver_data;
Hui Wang30c1e672012-06-28 16:21:35 +08001297 } else {
Fabio Estevam933e4af2013-07-22 12:41:39 -03001298 return -ENODEV;
Hui Wang30c1e672012-06-28 16:21:35 +08001299 }
1300
Fabio Estevam933e4af2013-07-22 12:41:39 -03001301 dev = alloc_candev(sizeof(struct flexcan_priv), 1);
1302 if (!dev)
1303 return -ENOMEM;
1304
Marc Kleine-Budde30164752015-05-10 15:26:58 +02001305 platform_set_drvdata(pdev, dev);
1306 SET_NETDEV_DEV(dev, &pdev->dev);
1307
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001308 dev->netdev_ops = &flexcan_netdev_ops;
1309 dev->irq = irq;
Reuben Dowle9a123492011-11-01 11:18:03 +13001310 dev->flags |= IFF_ECHO;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001311
1312 priv = netdev_priv(dev);
holt@sgi.com97efe9a2011-08-16 17:32:23 +00001313 priv->can.clock.freq = clock_freq;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001314 priv->can.bittiming_const = &flexcan_bittiming_const;
1315 priv->can.do_set_mode = flexcan_set_mode;
1316 priv->can.do_get_berr_counter = flexcan_get_berr_counter;
1317 priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK |
1318 CAN_CTRLMODE_LISTENONLY | CAN_CTRLMODE_3_SAMPLES |
1319 CAN_CTRLMODE_BERR_REPORTING;
Marc Kleine-Budde89af8742015-05-08 09:32:58 +02001320 priv->regs = regs;
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001321 priv->clk_ipg = clk_ipg;
1322 priv->clk_per = clk_per;
Hui Wang30c1e672012-06-28 16:21:35 +08001323 priv->devtype_data = devtype_data;
Andreas Werner555828e2015-03-22 17:35:52 +01001324 priv->reg_xceiver = reg_xceiver;
Fabio Estevamb7c41142013-06-10 23:12:57 -03001325
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +02001326 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP) {
1327 priv->tx_mb_idx = FLEXCAN_TX_MB_OFF_TIMESTAMP;
1328 priv->tx_mb_reserved = &regs->mb[FLEXCAN_TX_MB_RESERVED_OFF_TIMESTAMP];
1329 } else {
1330 priv->tx_mb_idx = FLEXCAN_TX_MB_OFF_FIFO;
1331 priv->tx_mb_reserved = &regs->mb[FLEXCAN_TX_MB_RESERVED_OFF_FIFO];
1332 }
Marc Kleine-Buddeb93917c2015-07-12 00:47:47 +02001333 priv->tx_mb = &regs->mb[priv->tx_mb_idx];
1334
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +02001335 priv->reg_imask1_default = FLEXCAN_IFLAG_MB(priv->tx_mb_idx);
1336 priv->reg_imask2_default = 0;
Marc Kleine-Budde28ac7dc2015-08-04 13:46:10 +02001337
Marc Kleine-Budde30164752015-05-10 15:26:58 +02001338 priv->offload.mailbox_read = flexcan_mailbox_read;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001339
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +02001340 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP) {
1341 u64 imask;
1342
1343 priv->offload.mb_first = FLEXCAN_RX_MB_OFF_TIMESTAMP_FIRST;
1344 priv->offload.mb_last = FLEXCAN_RX_MB_OFF_TIMESTAMP_LAST;
1345
1346 imask = GENMASK_ULL(priv->offload.mb_last, priv->offload.mb_first);
1347 priv->reg_imask1_default |= imask;
1348 priv->reg_imask2_default |= imask >> 32;
1349
1350 err = can_rx_offload_add_timestamp(dev, &priv->offload);
1351 } else {
1352 priv->reg_imask1_default |= FLEXCAN_IFLAG_RX_FIFO_OVERFLOW |
1353 FLEXCAN_IFLAG_RX_FIFO_AVAILABLE;
1354 err = can_rx_offload_add_fifo(dev, &priv->offload, FLEXCAN_NAPI_WEIGHT);
1355 }
Marc Kleine-Budde30164752015-05-10 15:26:58 +02001356 if (err)
1357 goto failed_offload;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001358
1359 err = register_flexcandev(dev);
1360 if (err) {
1361 dev_err(&pdev->dev, "registering netdev failed\n");
1362 goto failed_register;
1363 }
1364
Fabio Baltieriadccadb2012-12-18 18:50:58 +01001365 devm_can_led_init(dev);
1366
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001367 dev_info(&pdev->dev, "device registered (reg_base=%p, irq=%d)\n",
Marc Kleine-Budde89af8742015-05-08 09:32:58 +02001368 priv->regs, dev->irq);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001369
1370 return 0;
1371
Marc Kleine-Budde30164752015-05-10 15:26:58 +02001372 failed_offload:
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001373 failed_register:
1374 free_candev(dev);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001375 return err;
1376}
1377
Bill Pemberton3c8ac0f2012-12-03 09:22:44 -05001378static int flexcan_remove(struct platform_device *pdev)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001379{
1380 struct net_device *dev = platform_get_drvdata(pdev);
Marc Kleine-Budded96e43e2014-02-28 20:48:36 +01001381 struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001382
1383 unregister_flexcandev(dev);
Marc Kleine-Budde30164752015-05-10 15:26:58 +02001384 can_rx_offload_del(&priv->offload);
Marc Kleine-Budde9a275862010-10-21 05:07:58 +00001385 free_candev(dev);
1386
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001387 return 0;
1388}
1389
Marc Kleine-Budde08c6d352014-03-05 19:10:44 +01001390static int __maybe_unused flexcan_suspend(struct device *device)
Eric Bénard8b5e2182012-05-08 17:12:17 +02001391{
Fabio Estevam588e7a82013-05-20 15:43:43 -03001392 struct net_device *dev = dev_get_drvdata(device);
Eric Bénard8b5e2182012-05-08 17:12:17 +02001393 struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001394 int err;
Eric Bénard8b5e2182012-05-08 17:12:17 +02001395
Eric Bénard8b5e2182012-05-08 17:12:17 +02001396 if (netif_running(dev)) {
Fabio Estevam4de349e2016-08-17 12:41:08 -03001397 err = flexcan_chip_disable(priv);
1398 if (err)
1399 return err;
Eric Bénard8b5e2182012-05-08 17:12:17 +02001400 netif_stop_queue(dev);
1401 netif_device_detach(dev);
1402 }
1403 priv->can.state = CAN_STATE_SLEEPING;
1404
1405 return 0;
1406}
1407
Marc Kleine-Budde08c6d352014-03-05 19:10:44 +01001408static int __maybe_unused flexcan_resume(struct device *device)
Eric Bénard8b5e2182012-05-08 17:12:17 +02001409{
Fabio Estevam588e7a82013-05-20 15:43:43 -03001410 struct net_device *dev = dev_get_drvdata(device);
Eric Bénard8b5e2182012-05-08 17:12:17 +02001411 struct flexcan_priv *priv = netdev_priv(dev);
Fabio Estevam4de349e2016-08-17 12:41:08 -03001412 int err;
Eric Bénard8b5e2182012-05-08 17:12:17 +02001413
1414 priv->can.state = CAN_STATE_ERROR_ACTIVE;
1415 if (netif_running(dev)) {
1416 netif_device_attach(dev);
1417 netif_start_queue(dev);
Fabio Estevam4de349e2016-08-17 12:41:08 -03001418 err = flexcan_chip_enable(priv);
1419 if (err)
1420 return err;
Eric Bénard8b5e2182012-05-08 17:12:17 +02001421 }
Fabio Estevam4de349e2016-08-17 12:41:08 -03001422 return 0;
Eric Bénard8b5e2182012-05-08 17:12:17 +02001423}
Fabio Estevam588e7a82013-05-20 15:43:43 -03001424
1425static SIMPLE_DEV_PM_OPS(flexcan_pm_ops, flexcan_suspend, flexcan_resume);
Eric Bénard8b5e2182012-05-08 17:12:17 +02001426
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001427static struct platform_driver flexcan_driver = {
holt@sgi.comc8aef4c2011-08-16 17:32:22 +00001428 .driver = {
1429 .name = DRV_NAME,
Fabio Estevam588e7a82013-05-20 15:43:43 -03001430 .pm = &flexcan_pm_ops,
holt@sgi.comc8aef4c2011-08-16 17:32:22 +00001431 .of_match_table = flexcan_of_match,
1432 },
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001433 .probe = flexcan_probe,
Bill Pemberton3c8ac0f2012-12-03 09:22:44 -05001434 .remove = flexcan_remove,
Hui Wang30c1e672012-06-28 16:21:35 +08001435 .id_table = flexcan_id_table,
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001436};
1437
Axel Lin871d3372011-11-27 15:42:31 +00001438module_platform_driver(flexcan_driver);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001439
1440MODULE_AUTHOR("Sascha Hauer <kernel@pengutronix.de>, "
1441 "Marc Kleine-Budde <kernel@pengutronix.de>");
1442MODULE_LICENSE("GPL v2");
1443MODULE_DESCRIPTION("CAN port driver for flexcan based chip");