blob: c83f0f03482ba1d2d9f2b121d2c844af29432f3c [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
6 * Copyright (c) 2010 Marc Kleine-Budde, Pengutronix
7 *
8 * Based on code originally by Andrey Volkov <avolkov@varma-el.com>
9 *
10 * LICENCE:
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation version 2.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 */
21
22#include <linux/netdevice.h>
23#include <linux/can.h>
24#include <linux/can/dev.h>
25#include <linux/can/error.h>
Fabio Baltieriadccadb2012-12-18 18:50:58 +010026#include <linux/can/led.h>
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +020027#include <linux/clk.h>
28#include <linux/delay.h>
29#include <linux/if_arp.h>
30#include <linux/if_ether.h>
31#include <linux/interrupt.h>
32#include <linux/io.h>
33#include <linux/kernel.h>
34#include <linux/list.h>
35#include <linux/module.h>
holt@sgi.com97efe9a2011-08-16 17:32:23 +000036#include <linux/of.h>
Hui Wang30c1e672012-06-28 16:21:35 +080037#include <linux/of_device.h>
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +020038#include <linux/platform_device.h>
Fabio Estevamb7c41142013-06-10 23:12:57 -030039#include <linux/regulator/consumer.h>
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +020040
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +020041#define DRV_NAME "flexcan"
42
43/* 8 for RX fifo and 2 error handling */
44#define FLEXCAN_NAPI_WEIGHT (8 + 2)
45
46/* FLEXCAN module configuration register (CANMCR) bits */
47#define FLEXCAN_MCR_MDIS BIT(31)
48#define FLEXCAN_MCR_FRZ BIT(30)
49#define FLEXCAN_MCR_FEN BIT(29)
50#define FLEXCAN_MCR_HALT BIT(28)
51#define FLEXCAN_MCR_NOT_RDY BIT(27)
52#define FLEXCAN_MCR_WAK_MSK BIT(26)
53#define FLEXCAN_MCR_SOFTRST BIT(25)
54#define FLEXCAN_MCR_FRZ_ACK BIT(24)
55#define FLEXCAN_MCR_SUPV BIT(23)
56#define FLEXCAN_MCR_SLF_WAK BIT(22)
57#define FLEXCAN_MCR_WRN_EN BIT(21)
58#define FLEXCAN_MCR_LPM_ACK BIT(20)
59#define FLEXCAN_MCR_WAK_SRC BIT(19)
60#define FLEXCAN_MCR_DOZE BIT(18)
61#define FLEXCAN_MCR_SRX_DIS BIT(17)
62#define FLEXCAN_MCR_BCC BIT(16)
63#define FLEXCAN_MCR_LPRIO_EN BIT(13)
64#define FLEXCAN_MCR_AEN BIT(12)
Marc Kleine-Budde4c728d82014-09-02 16:54:17 +020065#define FLEXCAN_MCR_MAXMB(x) ((x) & 0x7f)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +020066#define FLEXCAN_MCR_IDAM_A (0 << 8)
67#define FLEXCAN_MCR_IDAM_B (1 << 8)
68#define FLEXCAN_MCR_IDAM_C (2 << 8)
69#define FLEXCAN_MCR_IDAM_D (3 << 8)
70
71/* FLEXCAN control register (CANCTRL) bits */
72#define FLEXCAN_CTRL_PRESDIV(x) (((x) & 0xff) << 24)
73#define FLEXCAN_CTRL_RJW(x) (((x) & 0x03) << 22)
74#define FLEXCAN_CTRL_PSEG1(x) (((x) & 0x07) << 19)
75#define FLEXCAN_CTRL_PSEG2(x) (((x) & 0x07) << 16)
76#define FLEXCAN_CTRL_BOFF_MSK BIT(15)
77#define FLEXCAN_CTRL_ERR_MSK BIT(14)
78#define FLEXCAN_CTRL_CLK_SRC BIT(13)
79#define FLEXCAN_CTRL_LPB BIT(12)
80#define FLEXCAN_CTRL_TWRN_MSK BIT(11)
81#define FLEXCAN_CTRL_RWRN_MSK BIT(10)
82#define FLEXCAN_CTRL_SMP BIT(7)
83#define FLEXCAN_CTRL_BOFF_REC BIT(6)
84#define FLEXCAN_CTRL_TSYN BIT(5)
85#define FLEXCAN_CTRL_LBUF BIT(4)
86#define FLEXCAN_CTRL_LOM BIT(3)
87#define FLEXCAN_CTRL_PROPSEG(x) ((x) & 0x07)
88#define FLEXCAN_CTRL_ERR_BUS (FLEXCAN_CTRL_ERR_MSK)
89#define FLEXCAN_CTRL_ERR_STATE \
90 (FLEXCAN_CTRL_TWRN_MSK | FLEXCAN_CTRL_RWRN_MSK | \
91 FLEXCAN_CTRL_BOFF_MSK)
92#define FLEXCAN_CTRL_ERR_ALL \
93 (FLEXCAN_CTRL_ERR_BUS | FLEXCAN_CTRL_ERR_STATE)
94
Stefan Agnercdce8442014-07-15 14:56:21 +020095/* FLEXCAN control register 2 (CTRL2) bits */
Marc Kleine-Budde6f75fce2014-09-23 11:03:01 +020096#define FLEXCAN_CTRL2_ECRWRE BIT(29)
97#define FLEXCAN_CTRL2_WRMFRZ BIT(28)
98#define FLEXCAN_CTRL2_RFFN(x) (((x) & 0x0f) << 24)
99#define FLEXCAN_CTRL2_TASD(x) (((x) & 0x1f) << 19)
100#define FLEXCAN_CTRL2_MRP BIT(18)
101#define FLEXCAN_CTRL2_RRS BIT(17)
102#define FLEXCAN_CTRL2_EACEN BIT(16)
Stefan Agnercdce8442014-07-15 14:56:21 +0200103
104/* FLEXCAN memory error control register (MECR) bits */
105#define FLEXCAN_MECR_ECRWRDIS BIT(31)
106#define FLEXCAN_MECR_HANCEI_MSK BIT(19)
107#define FLEXCAN_MECR_FANCEI_MSK BIT(18)
108#define FLEXCAN_MECR_CEI_MSK BIT(16)
109#define FLEXCAN_MECR_HAERRIE BIT(15)
110#define FLEXCAN_MECR_FAERRIE BIT(14)
111#define FLEXCAN_MECR_EXTERRIE BIT(13)
112#define FLEXCAN_MECR_RERRDIS BIT(9)
113#define FLEXCAN_MECR_ECCDIS BIT(8)
114#define FLEXCAN_MECR_NCEFAFRZ BIT(7)
115
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200116/* FLEXCAN error and status register (ESR) bits */
117#define FLEXCAN_ESR_TWRN_INT BIT(17)
118#define FLEXCAN_ESR_RWRN_INT BIT(16)
119#define FLEXCAN_ESR_BIT1_ERR BIT(15)
120#define FLEXCAN_ESR_BIT0_ERR BIT(14)
121#define FLEXCAN_ESR_ACK_ERR BIT(13)
122#define FLEXCAN_ESR_CRC_ERR BIT(12)
123#define FLEXCAN_ESR_FRM_ERR BIT(11)
124#define FLEXCAN_ESR_STF_ERR BIT(10)
125#define FLEXCAN_ESR_TX_WRN BIT(9)
126#define FLEXCAN_ESR_RX_WRN BIT(8)
127#define FLEXCAN_ESR_IDLE BIT(7)
128#define FLEXCAN_ESR_TXRX BIT(6)
129#define FLEXCAN_EST_FLT_CONF_SHIFT (4)
130#define FLEXCAN_ESR_FLT_CONF_MASK (0x3 << FLEXCAN_EST_FLT_CONF_SHIFT)
131#define FLEXCAN_ESR_FLT_CONF_ACTIVE (0x0 << FLEXCAN_EST_FLT_CONF_SHIFT)
132#define FLEXCAN_ESR_FLT_CONF_PASSIVE (0x1 << FLEXCAN_EST_FLT_CONF_SHIFT)
133#define FLEXCAN_ESR_BOFF_INT BIT(2)
134#define FLEXCAN_ESR_ERR_INT BIT(1)
135#define FLEXCAN_ESR_WAK_INT BIT(0)
136#define FLEXCAN_ESR_ERR_BUS \
137 (FLEXCAN_ESR_BIT1_ERR | FLEXCAN_ESR_BIT0_ERR | \
138 FLEXCAN_ESR_ACK_ERR | FLEXCAN_ESR_CRC_ERR | \
139 FLEXCAN_ESR_FRM_ERR | FLEXCAN_ESR_STF_ERR)
140#define FLEXCAN_ESR_ERR_STATE \
141 (FLEXCAN_ESR_TWRN_INT | FLEXCAN_ESR_RWRN_INT | FLEXCAN_ESR_BOFF_INT)
142#define FLEXCAN_ESR_ERR_ALL \
143 (FLEXCAN_ESR_ERR_BUS | FLEXCAN_ESR_ERR_STATE)
Wolfgang Grandegger6e9d5542011-12-12 16:09:28 +0100144#define FLEXCAN_ESR_ALL_INT \
145 (FLEXCAN_ESR_TWRN_INT | FLEXCAN_ESR_RWRN_INT | \
146 FLEXCAN_ESR_BOFF_INT | FLEXCAN_ESR_ERR_INT)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200147
148/* FLEXCAN interrupt flag register (IFLAG) bits */
David Jander25e92442014-09-03 16:47:22 +0200149/* Errata ERR005829 step7: Reserve first valid MB */
150#define FLEXCAN_TX_BUF_RESERVED 8
151#define FLEXCAN_TX_BUF_ID 9
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200152#define FLEXCAN_IFLAG_BUF(x) BIT(x)
153#define FLEXCAN_IFLAG_RX_FIFO_OVERFLOW BIT(7)
154#define FLEXCAN_IFLAG_RX_FIFO_WARN BIT(6)
155#define FLEXCAN_IFLAG_RX_FIFO_AVAILABLE BIT(5)
156#define FLEXCAN_IFLAG_DEFAULT \
157 (FLEXCAN_IFLAG_RX_FIFO_OVERFLOW | FLEXCAN_IFLAG_RX_FIFO_AVAILABLE | \
158 FLEXCAN_IFLAG_BUF(FLEXCAN_TX_BUF_ID))
159
160/* FLEXCAN message buffers */
Marc Kleine-Buddec32fe4a2014-09-16 12:39:28 +0200161#define FLEXCAN_MB_CODE_RX_INACTIVE (0x0 << 24)
162#define FLEXCAN_MB_CODE_RX_EMPTY (0x4 << 24)
163#define FLEXCAN_MB_CODE_RX_FULL (0x2 << 24)
164#define FLEXCAN_MB_CODE_RX_OVERRRUN (0x6 << 24)
165#define FLEXCAN_MB_CODE_RX_RANSWER (0xa << 24)
166
167#define FLEXCAN_MB_CODE_TX_INACTIVE (0x8 << 24)
168#define FLEXCAN_MB_CODE_TX_ABORT (0x9 << 24)
169#define FLEXCAN_MB_CODE_TX_DATA (0xc << 24)
170#define FLEXCAN_MB_CODE_TX_TANSWER (0xe << 24)
171
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200172#define FLEXCAN_MB_CNT_SRR BIT(22)
173#define FLEXCAN_MB_CNT_IDE BIT(21)
174#define FLEXCAN_MB_CNT_RTR BIT(20)
175#define FLEXCAN_MB_CNT_LENGTH(x) (((x) & 0xf) << 16)
176#define FLEXCAN_MB_CNT_TIMESTAMP(x) ((x) & 0xffff)
177
178#define FLEXCAN_MB_CODE_MASK (0xf0ffffff)
179
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100180#define FLEXCAN_TIMEOUT_US (50)
181
Wolfgang Grandeggerbb698ca2012-10-10 21:10:42 +0200182/*
183 * FLEXCAN hardware feature flags
184 *
185 * Below is some version info we got:
David Jander8a1ce7e2014-10-10 15:04:03 +0200186 * SOC Version IP-Version Glitch- [TR]WRN_INT Memory err RTR re-
187 * Filter? connected? detection ception in MB
188 * MX25 FlexCAN2 03.00.00.00 no no no no
189 * MX28 FlexCAN2 03.00.04.00 yes yes no no
190 * MX35 FlexCAN2 03.00.00.00 no no no no
191 * MX53 FlexCAN2 03.00.00.00 yes no no no
192 * MX6s FlexCAN3 10.00.12.00 yes yes no yes
193 * VF610 FlexCAN3 ? no yes yes yes?
Wolfgang Grandeggerbb698ca2012-10-10 21:10:42 +0200194 *
195 * Some SOCs do not have the RX_WARN & TX_WARN interrupt line connected.
196 */
Wolfgang Grandegger4f72e5f2012-09-28 03:17:15 +0000197#define FLEXCAN_HAS_V10_FEATURES BIT(1) /* For core version >= 10 */
Wolfgang Grandeggerbb698ca2012-10-10 21:10:42 +0200198#define FLEXCAN_HAS_BROKEN_ERR_STATE BIT(2) /* [TR]WRN_INT not connected */
Stefan Agnercdce8442014-07-15 14:56:21 +0200199#define FLEXCAN_HAS_MECR_FEATURES BIT(3) /* Memory error detection */
Wolfgang Grandegger4f72e5f2012-09-28 03:17:15 +0000200
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200201/* Structure of the message buffer */
202struct flexcan_mb {
203 u32 can_ctrl;
204 u32 can_id;
205 u32 data[2];
206};
207
208/* Structure of the hardware registers */
209struct flexcan_regs {
210 u32 mcr; /* 0x00 */
211 u32 ctrl; /* 0x04 */
212 u32 timer; /* 0x08 */
213 u32 _reserved1; /* 0x0c */
214 u32 rxgmask; /* 0x10 */
215 u32 rx14mask; /* 0x14 */
216 u32 rx15mask; /* 0x18 */
217 u32 ecr; /* 0x1c */
218 u32 esr; /* 0x20 */
219 u32 imask2; /* 0x24 */
220 u32 imask1; /* 0x28 */
221 u32 iflag2; /* 0x2c */
222 u32 iflag1; /* 0x30 */
Marc Kleine-Budde6f75fce2014-09-23 11:03:01 +0200223 u32 ctrl2; /* 0x34 */
Hui Wang30c1e672012-06-28 16:21:35 +0800224 u32 esr2; /* 0x38 */
225 u32 imeur; /* 0x3c */
226 u32 lrfr; /* 0x40 */
227 u32 crcr; /* 0x44 */
228 u32 rxfgmask; /* 0x48 */
229 u32 rxfir; /* 0x4c */
Stefan Agnercdce8442014-07-15 14:56:21 +0200230 u32 _reserved3[12]; /* 0x50 */
231 struct flexcan_mb cantxfg[64]; /* 0x80 */
Marc Kleine-Budde66a6ef02014-09-17 12:50:48 +0200232 /* FIFO-mode:
233 * MB
234 * 0x080...0x08f 0 RX message buffer
235 * 0x090...0x0df 1-5 reserverd
236 * 0x0e0...0x0ff 6-7 8 entry ID table
237 * (mx25, mx28, mx35, mx53)
238 * 0x0e0...0x2df 6-7..37 8..128 entry ID table
239 * size conf'ed via ctrl2::RFFN
240 * (mx6, vf610)
241 */
Stefan Agnercdce8442014-07-15 14:56:21 +0200242 u32 _reserved4[408];
243 u32 mecr; /* 0xae0 */
244 u32 erriar; /* 0xae4 */
245 u32 erridpr; /* 0xae8 */
246 u32 errippr; /* 0xaec */
247 u32 rerrar; /* 0xaf0 */
248 u32 rerrdr; /* 0xaf4 */
249 u32 rerrsynr; /* 0xaf8 */
250 u32 errsr; /* 0xafc */
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200251};
252
Hui Wang30c1e672012-06-28 16:21:35 +0800253struct flexcan_devtype_data {
Wolfgang Grandegger4f72e5f2012-09-28 03:17:15 +0000254 u32 features; /* hardware controller features */
Hui Wang30c1e672012-06-28 16:21:35 +0800255};
256
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200257struct flexcan_priv {
258 struct can_priv can;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200259 struct napi_struct napi;
260
261 void __iomem *base;
262 u32 reg_esr;
263 u32 reg_ctrl_default;
264
Steffen Trumtrar3d42a372012-07-17 16:14:34 +0200265 struct clk *clk_ipg;
266 struct clk *clk_per;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200267 struct flexcan_platform_data *pdata;
Marc Kleine-Buddedda0b3b2012-07-13 14:52:48 +0200268 const struct flexcan_devtype_data *devtype_data;
Fabio Estevamb7c41142013-06-10 23:12:57 -0300269 struct regulator *reg_xceiver;
Hui Wang30c1e672012-06-28 16:21:35 +0800270};
271
272static struct flexcan_devtype_data fsl_p1010_devtype_data = {
Wolfgang Grandegger4f72e5f2012-09-28 03:17:15 +0000273 .features = FLEXCAN_HAS_BROKEN_ERR_STATE,
Hui Wang30c1e672012-06-28 16:21:35 +0800274};
Wolfgang Grandegger4f72e5f2012-09-28 03:17:15 +0000275static struct flexcan_devtype_data fsl_imx28_devtype_data;
Hui Wang30c1e672012-06-28 16:21:35 +0800276static struct flexcan_devtype_data fsl_imx6q_devtype_data = {
Wolfgang Grandeggerbb698ca2012-10-10 21:10:42 +0200277 .features = FLEXCAN_HAS_V10_FEATURES,
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200278};
Stefan Agnercdce8442014-07-15 14:56:21 +0200279static struct flexcan_devtype_data fsl_vf610_devtype_data = {
280 .features = FLEXCAN_HAS_V10_FEATURES | FLEXCAN_HAS_MECR_FEATURES,
281};
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200282
Marc Kleine-Budde194b9a42012-07-16 12:58:31 +0200283static const struct can_bittiming_const flexcan_bittiming_const = {
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200284 .name = DRV_NAME,
285 .tseg1_min = 4,
286 .tseg1_max = 16,
287 .tseg2_min = 2,
288 .tseg2_max = 8,
289 .sjw_max = 4,
290 .brp_min = 1,
291 .brp_max = 256,
292 .brp_inc = 1,
293};
294
295/*
Arnd Bergmann0e4b9492014-01-14 11:44:09 +0100296 * Abstract off the read/write for arm versus ppc. This
297 * assumes that PPC uses big-endian registers and everything
298 * else uses little-endian registers, independent of CPU
299 * endianess.
holt@sgi.com61e271e2011-08-16 17:32:20 +0000300 */
Arnd Bergmann0e4b9492014-01-14 11:44:09 +0100301#if defined(CONFIG_PPC)
holt@sgi.com61e271e2011-08-16 17:32:20 +0000302static inline u32 flexcan_read(void __iomem *addr)
303{
304 return in_be32(addr);
305}
306
307static inline void flexcan_write(u32 val, void __iomem *addr)
308{
309 out_be32(addr, val);
310}
311#else
312static inline u32 flexcan_read(void __iomem *addr)
313{
314 return readl(addr);
315}
316
317static inline void flexcan_write(u32 val, void __iomem *addr)
318{
319 writel(val, addr);
320}
321#endif
322
Marc Kleine-Buddef0036982014-02-28 17:18:27 +0100323static inline int flexcan_transceiver_enable(const struct flexcan_priv *priv)
324{
325 if (!priv->reg_xceiver)
326 return 0;
327
328 return regulator_enable(priv->reg_xceiver);
329}
330
331static inline int flexcan_transceiver_disable(const struct flexcan_priv *priv)
332{
333 if (!priv->reg_xceiver)
334 return 0;
335
336 return regulator_disable(priv->reg_xceiver);
337}
338
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200339static inline int flexcan_has_and_handle_berr(const struct flexcan_priv *priv,
340 u32 reg_esr)
341{
342 return (priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING) &&
343 (reg_esr & FLEXCAN_ESR_ERR_BUS);
344}
345
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100346static int flexcan_chip_enable(struct flexcan_priv *priv)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200347{
348 struct flexcan_regs __iomem *regs = priv->base;
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100349 unsigned int timeout = FLEXCAN_TIMEOUT_US / 10;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200350 u32 reg;
351
holt@sgi.com61e271e2011-08-16 17:32:20 +0000352 reg = flexcan_read(&regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200353 reg &= ~FLEXCAN_MCR_MDIS;
holt@sgi.com61e271e2011-08-16 17:32:20 +0000354 flexcan_write(reg, &regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200355
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100356 while (timeout-- && (flexcan_read(&regs->mcr) & FLEXCAN_MCR_LPM_ACK))
David Jander8badd652014-08-27 12:02:16 +0200357 udelay(10);
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100358
359 if (flexcan_read(&regs->mcr) & FLEXCAN_MCR_LPM_ACK)
360 return -ETIMEDOUT;
361
362 return 0;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200363}
364
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100365static int flexcan_chip_disable(struct flexcan_priv *priv)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200366{
367 struct flexcan_regs __iomem *regs = priv->base;
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100368 unsigned int timeout = FLEXCAN_TIMEOUT_US / 10;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200369 u32 reg;
370
holt@sgi.com61e271e2011-08-16 17:32:20 +0000371 reg = flexcan_read(&regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200372 reg |= FLEXCAN_MCR_MDIS;
holt@sgi.com61e271e2011-08-16 17:32:20 +0000373 flexcan_write(reg, &regs->mcr);
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100374
375 while (timeout-- && !(flexcan_read(&regs->mcr) & FLEXCAN_MCR_LPM_ACK))
David Jander8badd652014-08-27 12:02:16 +0200376 udelay(10);
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100377
378 if (!(flexcan_read(&regs->mcr) & FLEXCAN_MCR_LPM_ACK))
379 return -ETIMEDOUT;
380
381 return 0;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200382}
383
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100384static int flexcan_chip_freeze(struct flexcan_priv *priv)
385{
386 struct flexcan_regs __iomem *regs = priv->base;
387 unsigned int timeout = 1000 * 1000 * 10 / priv->can.bittiming.bitrate;
388 u32 reg;
389
390 reg = flexcan_read(&regs->mcr);
391 reg |= FLEXCAN_MCR_HALT;
392 flexcan_write(reg, &regs->mcr);
393
394 while (timeout-- && !(flexcan_read(&regs->mcr) & FLEXCAN_MCR_FRZ_ACK))
David Jander8badd652014-08-27 12:02:16 +0200395 udelay(100);
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100396
397 if (!(flexcan_read(&regs->mcr) & FLEXCAN_MCR_FRZ_ACK))
398 return -ETIMEDOUT;
399
400 return 0;
401}
402
403static int flexcan_chip_unfreeze(struct flexcan_priv *priv)
404{
405 struct flexcan_regs __iomem *regs = priv->base;
406 unsigned int timeout = FLEXCAN_TIMEOUT_US / 10;
407 u32 reg;
408
409 reg = flexcan_read(&regs->mcr);
410 reg &= ~FLEXCAN_MCR_HALT;
411 flexcan_write(reg, &regs->mcr);
412
413 while (timeout-- && (flexcan_read(&regs->mcr) & FLEXCAN_MCR_FRZ_ACK))
David Jander8badd652014-08-27 12:02:16 +0200414 udelay(10);
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100415
416 if (flexcan_read(&regs->mcr) & FLEXCAN_MCR_FRZ_ACK)
417 return -ETIMEDOUT;
418
419 return 0;
420}
421
Marc Kleine-Budde4b5b8222014-02-28 15:16:59 +0100422static int flexcan_chip_softreset(struct flexcan_priv *priv)
423{
424 struct flexcan_regs __iomem *regs = priv->base;
425 unsigned int timeout = FLEXCAN_TIMEOUT_US / 10;
426
427 flexcan_write(FLEXCAN_MCR_SOFTRST, &regs->mcr);
428 while (timeout-- && (flexcan_read(&regs->mcr) & FLEXCAN_MCR_SOFTRST))
David Jander8badd652014-08-27 12:02:16 +0200429 udelay(10);
Marc Kleine-Budde4b5b8222014-02-28 15:16:59 +0100430
431 if (flexcan_read(&regs->mcr) & FLEXCAN_MCR_SOFTRST)
432 return -ETIMEDOUT;
433
434 return 0;
435}
436
Stefan Agnerec56acf2014-07-15 14:56:20 +0200437
438static int __flexcan_get_berr_counter(const struct net_device *dev,
439 struct can_berr_counter *bec)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200440{
441 const struct flexcan_priv *priv = netdev_priv(dev);
442 struct flexcan_regs __iomem *regs = priv->base;
holt@sgi.com61e271e2011-08-16 17:32:20 +0000443 u32 reg = flexcan_read(&regs->ecr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200444
445 bec->txerr = (reg >> 0) & 0xff;
446 bec->rxerr = (reg >> 8) & 0xff;
447
448 return 0;
449}
450
Stefan Agnerec56acf2014-07-15 14:56:20 +0200451static int flexcan_get_berr_counter(const struct net_device *dev,
452 struct can_berr_counter *bec)
453{
454 const struct flexcan_priv *priv = netdev_priv(dev);
455 int err;
456
457 err = clk_prepare_enable(priv->clk_ipg);
458 if (err)
459 return err;
460
461 err = clk_prepare_enable(priv->clk_per);
462 if (err)
463 goto out_disable_ipg;
464
465 err = __flexcan_get_berr_counter(dev, bec);
466
467 clk_disable_unprepare(priv->clk_per);
468 out_disable_ipg:
469 clk_disable_unprepare(priv->clk_ipg);
470
471 return err;
472}
473
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200474static int flexcan_start_xmit(struct sk_buff *skb, struct net_device *dev)
475{
476 const struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200477 struct flexcan_regs __iomem *regs = priv->base;
478 struct can_frame *cf = (struct can_frame *)skb->data;
479 u32 can_id;
Marc Kleine-Budde10d089b2014-09-23 11:18:11 +0200480 u32 ctrl = FLEXCAN_MB_CODE_TX_DATA | (cf->can_dlc << 16);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200481
482 if (can_dropped_invalid_skb(dev, skb))
483 return NETDEV_TX_OK;
484
485 netif_stop_queue(dev);
486
487 if (cf->can_id & CAN_EFF_FLAG) {
488 can_id = cf->can_id & CAN_EFF_MASK;
489 ctrl |= FLEXCAN_MB_CNT_IDE | FLEXCAN_MB_CNT_SRR;
490 } else {
491 can_id = (cf->can_id & CAN_SFF_MASK) << 18;
492 }
493
494 if (cf->can_id & CAN_RTR_FLAG)
495 ctrl |= FLEXCAN_MB_CNT_RTR;
496
497 if (cf->can_dlc > 0) {
498 u32 data = be32_to_cpup((__be32 *)&cf->data[0]);
holt@sgi.com61e271e2011-08-16 17:32:20 +0000499 flexcan_write(data, &regs->cantxfg[FLEXCAN_TX_BUF_ID].data[0]);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200500 }
501 if (cf->can_dlc > 3) {
502 u32 data = be32_to_cpup((__be32 *)&cf->data[4]);
holt@sgi.com61e271e2011-08-16 17:32:20 +0000503 flexcan_write(data, &regs->cantxfg[FLEXCAN_TX_BUF_ID].data[1]);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200504 }
505
Reuben Dowle9a123492011-11-01 11:18:03 +1300506 can_put_echo_skb(skb, dev, 0);
507
holt@sgi.com61e271e2011-08-16 17:32:20 +0000508 flexcan_write(can_id, &regs->cantxfg[FLEXCAN_TX_BUF_ID].can_id);
509 flexcan_write(ctrl, &regs->cantxfg[FLEXCAN_TX_BUF_ID].can_ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200510
David Jander25e92442014-09-03 16:47:22 +0200511 /* Errata ERR005829 step8:
512 * Write twice INACTIVE(0x8) code to first MB.
513 */
514 flexcan_write(FLEXCAN_MB_CODE_TX_INACTIVE,
515 &regs->cantxfg[FLEXCAN_TX_BUF_RESERVED].can_ctrl);
516 flexcan_write(FLEXCAN_MB_CODE_TX_INACTIVE,
517 &regs->cantxfg[FLEXCAN_TX_BUF_RESERVED].can_ctrl);
518
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200519 return NETDEV_TX_OK;
520}
521
522static void do_bus_err(struct net_device *dev,
523 struct can_frame *cf, u32 reg_esr)
524{
525 struct flexcan_priv *priv = netdev_priv(dev);
526 int rx_errors = 0, tx_errors = 0;
527
528 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
529
530 if (reg_esr & FLEXCAN_ESR_BIT1_ERR) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100531 netdev_dbg(dev, "BIT1_ERR irq\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200532 cf->data[2] |= CAN_ERR_PROT_BIT1;
533 tx_errors = 1;
534 }
535 if (reg_esr & FLEXCAN_ESR_BIT0_ERR) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100536 netdev_dbg(dev, "BIT0_ERR irq\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200537 cf->data[2] |= CAN_ERR_PROT_BIT0;
538 tx_errors = 1;
539 }
540 if (reg_esr & FLEXCAN_ESR_ACK_ERR) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100541 netdev_dbg(dev, "ACK_ERR irq\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200542 cf->can_id |= CAN_ERR_ACK;
543 cf->data[3] |= CAN_ERR_PROT_LOC_ACK;
544 tx_errors = 1;
545 }
546 if (reg_esr & FLEXCAN_ESR_CRC_ERR) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100547 netdev_dbg(dev, "CRC_ERR irq\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200548 cf->data[2] |= CAN_ERR_PROT_BIT;
549 cf->data[3] |= CAN_ERR_PROT_LOC_CRC_SEQ;
550 rx_errors = 1;
551 }
552 if (reg_esr & FLEXCAN_ESR_FRM_ERR) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100553 netdev_dbg(dev, "FRM_ERR irq\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200554 cf->data[2] |= CAN_ERR_PROT_FORM;
555 rx_errors = 1;
556 }
557 if (reg_esr & FLEXCAN_ESR_STF_ERR) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100558 netdev_dbg(dev, "STF_ERR irq\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200559 cf->data[2] |= CAN_ERR_PROT_STUFF;
560 rx_errors = 1;
561 }
562
563 priv->can.can_stats.bus_error++;
564 if (rx_errors)
565 dev->stats.rx_errors++;
566 if (tx_errors)
567 dev->stats.tx_errors++;
568}
569
570static int flexcan_poll_bus_err(struct net_device *dev, u32 reg_esr)
571{
572 struct sk_buff *skb;
573 struct can_frame *cf;
574
575 skb = alloc_can_err_skb(dev, &cf);
576 if (unlikely(!skb))
577 return 0;
578
579 do_bus_err(dev, cf, reg_esr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200580
581 dev->stats.rx_packets++;
582 dev->stats.rx_bytes += cf->can_dlc;
Marc Kleine-Buddea18ec1b2015-05-08 11:30:29 +0200583 netif_receive_skb(skb);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200584
585 return 1;
586}
587
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200588static int flexcan_poll_state(struct net_device *dev, u32 reg_esr)
589{
590 struct flexcan_priv *priv = netdev_priv(dev);
591 struct sk_buff *skb;
592 struct can_frame *cf;
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000593 enum can_state new_state = 0, rx_state = 0, tx_state = 0;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200594 int flt;
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000595 struct can_berr_counter bec;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200596
597 flt = reg_esr & FLEXCAN_ESR_FLT_CONF_MASK;
598 if (likely(flt == FLEXCAN_ESR_FLT_CONF_ACTIVE)) {
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000599 tx_state = unlikely(reg_esr & FLEXCAN_ESR_TX_WRN) ?
600 CAN_STATE_ERROR_WARNING : CAN_STATE_ERROR_ACTIVE;
601 rx_state = unlikely(reg_esr & FLEXCAN_ESR_RX_WRN) ?
602 CAN_STATE_ERROR_WARNING : CAN_STATE_ERROR_ACTIVE;
603 new_state = max(tx_state, rx_state);
Andri Yngvason258ce802015-03-17 13:03:09 +0000604 } else {
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000605 __flexcan_get_berr_counter(dev, &bec);
Andri Yngvason258ce802015-03-17 13:03:09 +0000606 new_state = flt == FLEXCAN_ESR_FLT_CONF_PASSIVE ?
607 CAN_STATE_ERROR_PASSIVE : CAN_STATE_BUS_OFF;
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000608 rx_state = bec.rxerr >= bec.txerr ? new_state : 0;
609 tx_state = bec.rxerr <= bec.txerr ? new_state : 0;
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000610 }
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200611
612 /* state hasn't changed */
613 if (likely(new_state == priv->can.state))
614 return 0;
615
616 skb = alloc_can_err_skb(dev, &cf);
617 if (unlikely(!skb))
618 return 0;
619
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000620 can_change_state(dev, cf, tx_state, rx_state);
621
622 if (unlikely(new_state == CAN_STATE_BUS_OFF))
623 can_bus_off(dev);
624
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200625 dev->stats.rx_packets++;
626 dev->stats.rx_bytes += cf->can_dlc;
Marc Kleine-Buddea18ec1b2015-05-08 11:30:29 +0200627 netif_receive_skb(skb);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200628
629 return 1;
630}
631
632static void flexcan_read_fifo(const struct net_device *dev,
633 struct can_frame *cf)
634{
635 const struct flexcan_priv *priv = netdev_priv(dev);
636 struct flexcan_regs __iomem *regs = priv->base;
637 struct flexcan_mb __iomem *mb = &regs->cantxfg[0];
638 u32 reg_ctrl, reg_id;
639
holt@sgi.com61e271e2011-08-16 17:32:20 +0000640 reg_ctrl = flexcan_read(&mb->can_ctrl);
641 reg_id = flexcan_read(&mb->can_id);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200642 if (reg_ctrl & FLEXCAN_MB_CNT_IDE)
643 cf->can_id = ((reg_id >> 0) & CAN_EFF_MASK) | CAN_EFF_FLAG;
644 else
645 cf->can_id = (reg_id >> 18) & CAN_SFF_MASK;
646
647 if (reg_ctrl & FLEXCAN_MB_CNT_RTR)
648 cf->can_id |= CAN_RTR_FLAG;
649 cf->can_dlc = get_can_dlc((reg_ctrl >> 16) & 0xf);
650
holt@sgi.com61e271e2011-08-16 17:32:20 +0000651 *(__be32 *)(cf->data + 0) = cpu_to_be32(flexcan_read(&mb->data[0]));
652 *(__be32 *)(cf->data + 4) = cpu_to_be32(flexcan_read(&mb->data[1]));
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200653
654 /* mark as read */
holt@sgi.com61e271e2011-08-16 17:32:20 +0000655 flexcan_write(FLEXCAN_IFLAG_RX_FIFO_AVAILABLE, &regs->iflag1);
656 flexcan_read(&regs->timer);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200657}
658
659static int flexcan_read_frame(struct net_device *dev)
660{
661 struct net_device_stats *stats = &dev->stats;
662 struct can_frame *cf;
663 struct sk_buff *skb;
664
665 skb = alloc_can_skb(dev, &cf);
666 if (unlikely(!skb)) {
667 stats->rx_dropped++;
668 return 0;
669 }
670
671 flexcan_read_fifo(dev, cf);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200672
673 stats->rx_packets++;
674 stats->rx_bytes += cf->can_dlc;
Marc Kleine-Buddea18ec1b2015-05-08 11:30:29 +0200675 netif_receive_skb(skb);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200676
Fabio Baltieriadccadb2012-12-18 18:50:58 +0100677 can_led_event(dev, CAN_LED_EVENT_RX);
678
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200679 return 1;
680}
681
682static int flexcan_poll(struct napi_struct *napi, int quota)
683{
684 struct net_device *dev = napi->dev;
685 const struct flexcan_priv *priv = netdev_priv(dev);
686 struct flexcan_regs __iomem *regs = priv->base;
687 u32 reg_iflag1, reg_esr;
688 int work_done = 0;
689
690 /*
691 * The error bits are cleared on read,
692 * use saved value from irq handler.
693 */
holt@sgi.com61e271e2011-08-16 17:32:20 +0000694 reg_esr = flexcan_read(&regs->esr) | priv->reg_esr;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200695
696 /* handle state changes */
697 work_done += flexcan_poll_state(dev, reg_esr);
698
699 /* handle RX-FIFO */
holt@sgi.com61e271e2011-08-16 17:32:20 +0000700 reg_iflag1 = flexcan_read(&regs->iflag1);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200701 while (reg_iflag1 & FLEXCAN_IFLAG_RX_FIFO_AVAILABLE &&
702 work_done < quota) {
703 work_done += flexcan_read_frame(dev);
holt@sgi.com61e271e2011-08-16 17:32:20 +0000704 reg_iflag1 = flexcan_read(&regs->iflag1);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200705 }
706
707 /* report bus errors */
708 if (flexcan_has_and_handle_berr(priv, reg_esr) && work_done < quota)
709 work_done += flexcan_poll_bus_err(dev, reg_esr);
710
711 if (work_done < quota) {
712 napi_complete(napi);
713 /* enable IRQs */
holt@sgi.com61e271e2011-08-16 17:32:20 +0000714 flexcan_write(FLEXCAN_IFLAG_DEFAULT, &regs->imask1);
715 flexcan_write(priv->reg_ctrl_default, &regs->ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200716 }
717
718 return work_done;
719}
720
721static irqreturn_t flexcan_irq(int irq, void *dev_id)
722{
723 struct net_device *dev = dev_id;
724 struct net_device_stats *stats = &dev->stats;
725 struct flexcan_priv *priv = netdev_priv(dev);
726 struct flexcan_regs __iomem *regs = priv->base;
727 u32 reg_iflag1, reg_esr;
728
holt@sgi.com61e271e2011-08-16 17:32:20 +0000729 reg_iflag1 = flexcan_read(&regs->iflag1);
730 reg_esr = flexcan_read(&regs->esr);
Wolfgang Grandegger6e9d5542011-12-12 16:09:28 +0100731 /* ACK all bus error and state change IRQ sources */
732 if (reg_esr & FLEXCAN_ESR_ALL_INT)
733 flexcan_write(reg_esr & FLEXCAN_ESR_ALL_INT, &regs->esr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200734
735 /*
736 * schedule NAPI in case of:
737 * - rx IRQ
738 * - state change IRQ
739 * - bus error IRQ and bus error reporting is activated
740 */
741 if ((reg_iflag1 & FLEXCAN_IFLAG_RX_FIFO_AVAILABLE) ||
742 (reg_esr & FLEXCAN_ESR_ERR_STATE) ||
743 flexcan_has_and_handle_berr(priv, reg_esr)) {
744 /*
745 * The error bits are cleared on read,
746 * save them for later use.
747 */
748 priv->reg_esr = reg_esr & FLEXCAN_ESR_ERR_BUS;
holt@sgi.com61e271e2011-08-16 17:32:20 +0000749 flexcan_write(FLEXCAN_IFLAG_DEFAULT &
750 ~FLEXCAN_IFLAG_RX_FIFO_AVAILABLE, &regs->imask1);
751 flexcan_write(priv->reg_ctrl_default & ~FLEXCAN_CTRL_ERR_ALL,
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200752 &regs->ctrl);
753 napi_schedule(&priv->napi);
754 }
755
756 /* FIFO overflow */
757 if (reg_iflag1 & FLEXCAN_IFLAG_RX_FIFO_OVERFLOW) {
holt@sgi.com61e271e2011-08-16 17:32:20 +0000758 flexcan_write(FLEXCAN_IFLAG_RX_FIFO_OVERFLOW, &regs->iflag1);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200759 dev->stats.rx_over_errors++;
760 dev->stats.rx_errors++;
761 }
762
763 /* transmission complete interrupt */
764 if (reg_iflag1 & (1 << FLEXCAN_TX_BUF_ID)) {
Reuben Dowle9a123492011-11-01 11:18:03 +1300765 stats->tx_bytes += can_get_echo_skb(dev, 0);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200766 stats->tx_packets++;
Fabio Baltieriadccadb2012-12-18 18:50:58 +0100767 can_led_event(dev, CAN_LED_EVENT_TX);
Marc Kleine-Buddede594482014-09-16 15:31:27 +0200768 /* after sending a RTR frame mailbox is in RX mode */
769 flexcan_write(FLEXCAN_MB_CODE_TX_INACTIVE,
770 &regs->cantxfg[FLEXCAN_TX_BUF_ID].can_ctrl);
holt@sgi.com61e271e2011-08-16 17:32:20 +0000771 flexcan_write((1 << FLEXCAN_TX_BUF_ID), &regs->iflag1);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200772 netif_wake_queue(dev);
773 }
774
775 return IRQ_HANDLED;
776}
777
778static void flexcan_set_bittiming(struct net_device *dev)
779{
780 const struct flexcan_priv *priv = netdev_priv(dev);
781 const struct can_bittiming *bt = &priv->can.bittiming;
782 struct flexcan_regs __iomem *regs = priv->base;
783 u32 reg;
784
holt@sgi.com61e271e2011-08-16 17:32:20 +0000785 reg = flexcan_read(&regs->ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200786 reg &= ~(FLEXCAN_CTRL_PRESDIV(0xff) |
787 FLEXCAN_CTRL_RJW(0x3) |
788 FLEXCAN_CTRL_PSEG1(0x7) |
789 FLEXCAN_CTRL_PSEG2(0x7) |
790 FLEXCAN_CTRL_PROPSEG(0x7) |
791 FLEXCAN_CTRL_LPB |
792 FLEXCAN_CTRL_SMP |
793 FLEXCAN_CTRL_LOM);
794
795 reg |= FLEXCAN_CTRL_PRESDIV(bt->brp - 1) |
796 FLEXCAN_CTRL_PSEG1(bt->phase_seg1 - 1) |
797 FLEXCAN_CTRL_PSEG2(bt->phase_seg2 - 1) |
798 FLEXCAN_CTRL_RJW(bt->sjw - 1) |
799 FLEXCAN_CTRL_PROPSEG(bt->prop_seg - 1);
800
801 if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)
802 reg |= FLEXCAN_CTRL_LPB;
803 if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
804 reg |= FLEXCAN_CTRL_LOM;
805 if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
806 reg |= FLEXCAN_CTRL_SMP;
807
Lucas Stach7a4b6c82015-08-07 17:16:03 +0200808 netdev_dbg(dev, "writing ctrl=0x%08x\n", reg);
holt@sgi.com61e271e2011-08-16 17:32:20 +0000809 flexcan_write(reg, &regs->ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200810
811 /* print chip status */
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100812 netdev_dbg(dev, "%s: mcr=0x%08x ctrl=0x%08x\n", __func__,
813 flexcan_read(&regs->mcr), flexcan_read(&regs->ctrl));
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200814}
815
816/*
817 * flexcan_chip_start
818 *
819 * this functions is entered with clocks enabled
820 *
821 */
822static int flexcan_chip_start(struct net_device *dev)
823{
824 struct flexcan_priv *priv = netdev_priv(dev);
825 struct flexcan_regs __iomem *regs = priv->base;
Marc Kleine-Budde6f75fce2014-09-23 11:03:01 +0200826 u32 reg_mcr, reg_ctrl, reg_ctrl2, reg_mecr;
David S. Miller1f6d8032014-09-23 12:09:27 -0400827 int err, i;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200828
829 /* enable module */
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100830 err = flexcan_chip_enable(priv);
831 if (err)
832 return err;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200833
834 /* soft reset */
Marc Kleine-Budde4b5b8222014-02-28 15:16:59 +0100835 err = flexcan_chip_softreset(priv);
836 if (err)
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100837 goto out_chip_disable;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200838
839 flexcan_set_bittiming(dev);
840
841 /*
842 * MCR
843 *
844 * enable freeze
845 * enable fifo
846 * halt now
847 * only supervisor access
848 * enable warning int
849 * choose format C
Reuben Dowle9a123492011-11-01 11:18:03 +1300850 * disable local echo
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200851 *
852 */
holt@sgi.com61e271e2011-08-16 17:32:20 +0000853 reg_mcr = flexcan_read(&regs->mcr);
Marc Kleine-Budded5a7b402013-10-04 10:52:36 +0200854 reg_mcr &= ~FLEXCAN_MCR_MAXMB(0xff);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200855 reg_mcr |= FLEXCAN_MCR_FRZ | FLEXCAN_MCR_FEN | FLEXCAN_MCR_HALT |
856 FLEXCAN_MCR_SUPV | FLEXCAN_MCR_WRN_EN |
Marc Kleine-Budded5a7b402013-10-04 10:52:36 +0200857 FLEXCAN_MCR_IDAM_C | FLEXCAN_MCR_SRX_DIS |
858 FLEXCAN_MCR_MAXMB(FLEXCAN_TX_BUF_ID);
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100859 netdev_dbg(dev, "%s: writing mcr=0x%08x", __func__, reg_mcr);
holt@sgi.com61e271e2011-08-16 17:32:20 +0000860 flexcan_write(reg_mcr, &regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200861
862 /*
863 * CTRL
864 *
865 * disable timer sync feature
866 *
867 * disable auto busoff recovery
868 * transmit lowest buffer first
869 *
870 * enable tx and rx warning interrupt
871 * enable bus off interrupt
872 * (== FLEXCAN_CTRL_ERR_STATE)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200873 */
holt@sgi.com61e271e2011-08-16 17:32:20 +0000874 reg_ctrl = flexcan_read(&regs->ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200875 reg_ctrl &= ~FLEXCAN_CTRL_TSYN;
876 reg_ctrl |= FLEXCAN_CTRL_BOFF_REC | FLEXCAN_CTRL_LBUF |
Wolfgang Grandegger4f72e5f2012-09-28 03:17:15 +0000877 FLEXCAN_CTRL_ERR_STATE;
878 /*
879 * enable the "error interrupt" (FLEXCAN_CTRL_ERR_MSK),
880 * on most Flexcan cores, too. Otherwise we don't get
881 * any error warning or passive interrupts.
882 */
883 if (priv->devtype_data->features & FLEXCAN_HAS_BROKEN_ERR_STATE ||
884 priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING)
885 reg_ctrl |= FLEXCAN_CTRL_ERR_MSK;
Alexander Steinbc03a542014-08-12 10:47:21 +0200886 else
887 reg_ctrl &= ~FLEXCAN_CTRL_ERR_MSK;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200888
889 /* save for later use */
890 priv->reg_ctrl_default = reg_ctrl;
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100891 netdev_dbg(dev, "%s: writing ctrl=0x%08x", __func__, reg_ctrl);
holt@sgi.com61e271e2011-08-16 17:32:20 +0000892 flexcan_write(reg_ctrl, &regs->ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200893
David Janderfc05b882014-08-27 11:58:05 +0200894 /* clear and invalidate all mailboxes first */
895 for (i = FLEXCAN_TX_BUF_ID; i < ARRAY_SIZE(regs->cantxfg); i++) {
896 flexcan_write(FLEXCAN_MB_CODE_RX_INACTIVE,
897 &regs->cantxfg[i].can_ctrl);
898 }
899
David Jander25e92442014-09-03 16:47:22 +0200900 /* Errata ERR005829: mark first TX mailbox as INACTIVE */
901 flexcan_write(FLEXCAN_MB_CODE_TX_INACTIVE,
902 &regs->cantxfg[FLEXCAN_TX_BUF_RESERVED].can_ctrl);
903
Marc Kleine-Buddec32fe4a2014-09-16 12:39:28 +0200904 /* mark TX mailbox as INACTIVE */
905 flexcan_write(FLEXCAN_MB_CODE_TX_INACTIVE,
Marc Kleine-Budded5a7b402013-10-04 10:52:36 +0200906 &regs->cantxfg[FLEXCAN_TX_BUF_ID].can_ctrl);
907
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200908 /* acceptance mask/acceptance code (accept everything) */
holt@sgi.com61e271e2011-08-16 17:32:20 +0000909 flexcan_write(0x0, &regs->rxgmask);
910 flexcan_write(0x0, &regs->rx14mask);
911 flexcan_write(0x0, &regs->rx15mask);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200912
Wolfgang Grandegger4f72e5f2012-09-28 03:17:15 +0000913 if (priv->devtype_data->features & FLEXCAN_HAS_V10_FEATURES)
Hui Wang30c1e672012-06-28 16:21:35 +0800914 flexcan_write(0x0, &regs->rxfgmask);
915
Stefan Agnercdce8442014-07-15 14:56:21 +0200916 /*
917 * On Vybrid, disable memory error detection interrupts
918 * and freeze mode.
919 * This also works around errata e5295 which generates
920 * false positive memory errors and put the device in
921 * freeze mode.
922 */
923 if (priv->devtype_data->features & FLEXCAN_HAS_MECR_FEATURES) {
924 /*
925 * Follow the protocol as described in "Detection
926 * and Correction of Memory Errors" to write to
927 * MECR register
928 */
Marc Kleine-Budde6f75fce2014-09-23 11:03:01 +0200929 reg_ctrl2 = flexcan_read(&regs->ctrl2);
930 reg_ctrl2 |= FLEXCAN_CTRL2_ECRWRE;
931 flexcan_write(reg_ctrl2, &regs->ctrl2);
Stefan Agnercdce8442014-07-15 14:56:21 +0200932
933 reg_mecr = flexcan_read(&regs->mecr);
934 reg_mecr &= ~FLEXCAN_MECR_ECRWRDIS;
935 flexcan_write(reg_mecr, &regs->mecr);
936 reg_mecr &= ~(FLEXCAN_MECR_NCEFAFRZ | FLEXCAN_MECR_HANCEI_MSK |
937 FLEXCAN_MECR_FANCEI_MSK);
938 flexcan_write(reg_mecr, &regs->mecr);
939 }
940
Marc Kleine-Buddef0036982014-02-28 17:18:27 +0100941 err = flexcan_transceiver_enable(priv);
942 if (err)
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100943 goto out_chip_disable;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200944
945 /* synchronize with the can bus */
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100946 err = flexcan_chip_unfreeze(priv);
947 if (err)
948 goto out_transceiver_disable;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200949
950 priv->can.state = CAN_STATE_ERROR_ACTIVE;
951
952 /* enable FIFO interrupts */
holt@sgi.com61e271e2011-08-16 17:32:20 +0000953 flexcan_write(FLEXCAN_IFLAG_DEFAULT, &regs->imask1);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200954
955 /* print chip status */
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100956 netdev_dbg(dev, "%s: reading mcr=0x%08x ctrl=0x%08x\n", __func__,
957 flexcan_read(&regs->mcr), flexcan_read(&regs->ctrl));
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200958
959 return 0;
960
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100961 out_transceiver_disable:
962 flexcan_transceiver_disable(priv);
963 out_chip_disable:
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200964 flexcan_chip_disable(priv);
965 return err;
966}
967
968/*
969 * flexcan_chip_stop
970 *
971 * this functions is entered with clocks enabled
972 *
973 */
974static void flexcan_chip_stop(struct net_device *dev)
975{
976 struct flexcan_priv *priv = netdev_priv(dev);
977 struct flexcan_regs __iomem *regs = priv->base;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200978
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100979 /* freeze + disable module */
980 flexcan_chip_freeze(priv);
981 flexcan_chip_disable(priv);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200982
Marc Kleine-Budde5be93bd2014-02-19 12:00:51 +0100983 /* Disable all interrupts */
984 flexcan_write(0, &regs->imask1);
985 flexcan_write(priv->reg_ctrl_default & ~FLEXCAN_CTRL_ERR_ALL,
986 &regs->ctrl);
987
Marc Kleine-Buddef0036982014-02-28 17:18:27 +0100988 flexcan_transceiver_disable(priv);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200989 priv->can.state = CAN_STATE_STOPPED;
990
991 return;
992}
993
994static int flexcan_open(struct net_device *dev)
995{
996 struct flexcan_priv *priv = netdev_priv(dev);
997 int err;
998
Fabio Estevamaa101812013-07-22 12:41:40 -0300999 err = clk_prepare_enable(priv->clk_ipg);
1000 if (err)
1001 return err;
1002
1003 err = clk_prepare_enable(priv->clk_per);
1004 if (err)
1005 goto out_disable_ipg;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001006
1007 err = open_candev(dev);
1008 if (err)
Fabio Estevamaa101812013-07-22 12:41:40 -03001009 goto out_disable_per;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001010
1011 err = request_irq(dev->irq, flexcan_irq, IRQF_SHARED, dev->name, dev);
1012 if (err)
1013 goto out_close;
1014
1015 /* start chip and queuing */
1016 err = flexcan_chip_start(dev);
1017 if (err)
Marc Kleine-Budde7e9e1482014-02-28 14:52:01 +01001018 goto out_free_irq;
Fabio Baltieriadccadb2012-12-18 18:50:58 +01001019
1020 can_led_event(dev, CAN_LED_EVENT_OPEN);
1021
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001022 napi_enable(&priv->napi);
1023 netif_start_queue(dev);
1024
1025 return 0;
1026
Marc Kleine-Budde7e9e1482014-02-28 14:52:01 +01001027 out_free_irq:
1028 free_irq(dev->irq, dev);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001029 out_close:
1030 close_candev(dev);
Fabio Estevamaa101812013-07-22 12:41:40 -03001031 out_disable_per:
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001032 clk_disable_unprepare(priv->clk_per);
Fabio Estevamaa101812013-07-22 12:41:40 -03001033 out_disable_ipg:
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001034 clk_disable_unprepare(priv->clk_ipg);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001035
1036 return err;
1037}
1038
1039static int flexcan_close(struct net_device *dev)
1040{
1041 struct flexcan_priv *priv = netdev_priv(dev);
1042
1043 netif_stop_queue(dev);
1044 napi_disable(&priv->napi);
1045 flexcan_chip_stop(dev);
1046
1047 free_irq(dev->irq, dev);
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001048 clk_disable_unprepare(priv->clk_per);
1049 clk_disable_unprepare(priv->clk_ipg);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001050
1051 close_candev(dev);
1052
Fabio Baltieriadccadb2012-12-18 18:50:58 +01001053 can_led_event(dev, CAN_LED_EVENT_STOP);
1054
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001055 return 0;
1056}
1057
1058static int flexcan_set_mode(struct net_device *dev, enum can_mode mode)
1059{
1060 int err;
1061
1062 switch (mode) {
1063 case CAN_MODE_START:
1064 err = flexcan_chip_start(dev);
1065 if (err)
1066 return err;
1067
1068 netif_wake_queue(dev);
1069 break;
1070
1071 default:
1072 return -EOPNOTSUPP;
1073 }
1074
1075 return 0;
1076}
1077
1078static const struct net_device_ops flexcan_netdev_ops = {
1079 .ndo_open = flexcan_open,
1080 .ndo_stop = flexcan_close,
1081 .ndo_start_xmit = flexcan_start_xmit,
Oliver Hartkoppc971fa22014-03-07 09:23:41 +01001082 .ndo_change_mtu = can_change_mtu,
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001083};
1084
Bill Pemberton3c8ac0f2012-12-03 09:22:44 -05001085static int register_flexcandev(struct net_device *dev)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001086{
1087 struct flexcan_priv *priv = netdev_priv(dev);
1088 struct flexcan_regs __iomem *regs = priv->base;
1089 u32 reg, err;
1090
Fabio Estevamaa101812013-07-22 12:41:40 -03001091 err = clk_prepare_enable(priv->clk_ipg);
1092 if (err)
1093 return err;
1094
1095 err = clk_prepare_enable(priv->clk_per);
1096 if (err)
1097 goto out_disable_ipg;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001098
1099 /* select "bus clock", chip must be disabled */
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001100 err = flexcan_chip_disable(priv);
1101 if (err)
1102 goto out_disable_per;
holt@sgi.com61e271e2011-08-16 17:32:20 +00001103 reg = flexcan_read(&regs->ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001104 reg |= FLEXCAN_CTRL_CLK_SRC;
holt@sgi.com61e271e2011-08-16 17:32:20 +00001105 flexcan_write(reg, &regs->ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001106
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001107 err = flexcan_chip_enable(priv);
1108 if (err)
1109 goto out_chip_disable;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001110
1111 /* set freeze, halt and activate FIFO, restrict register access */
holt@sgi.com61e271e2011-08-16 17:32:20 +00001112 reg = flexcan_read(&regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001113 reg |= FLEXCAN_MCR_FRZ | FLEXCAN_MCR_HALT |
1114 FLEXCAN_MCR_FEN | FLEXCAN_MCR_SUPV;
holt@sgi.com61e271e2011-08-16 17:32:20 +00001115 flexcan_write(reg, &regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001116
1117 /*
1118 * Currently we only support newer versions of this core
1119 * featuring a RX FIFO. Older cores found on some Coldfire
1120 * derivates are not yet supported.
1121 */
holt@sgi.com61e271e2011-08-16 17:32:20 +00001122 reg = flexcan_read(&regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001123 if (!(reg & FLEXCAN_MCR_FEN)) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +01001124 netdev_err(dev, "Could not enable RX FIFO, unsupported core\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001125 err = -ENODEV;
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001126 goto out_chip_disable;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001127 }
1128
1129 err = register_candev(dev);
1130
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001131 /* disable core and turn off clocks */
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001132 out_chip_disable:
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001133 flexcan_chip_disable(priv);
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001134 out_disable_per:
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001135 clk_disable_unprepare(priv->clk_per);
Fabio Estevamaa101812013-07-22 12:41:40 -03001136 out_disable_ipg:
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001137 clk_disable_unprepare(priv->clk_ipg);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001138
1139 return err;
1140}
1141
Bill Pemberton3c8ac0f2012-12-03 09:22:44 -05001142static void unregister_flexcandev(struct net_device *dev)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001143{
1144 unregister_candev(dev);
1145}
1146
Hui Wang30c1e672012-06-28 16:21:35 +08001147static const struct of_device_id flexcan_of_match[] = {
Hui Wang30c1e672012-06-28 16:21:35 +08001148 { .compatible = "fsl,imx6q-flexcan", .data = &fsl_imx6q_devtype_data, },
Marc Kleine-Buddee3587842013-10-03 23:51:55 +02001149 { .compatible = "fsl,imx28-flexcan", .data = &fsl_imx28_devtype_data, },
1150 { .compatible = "fsl,p1010-flexcan", .data = &fsl_p1010_devtype_data, },
Stefan Agnercdce8442014-07-15 14:56:21 +02001151 { .compatible = "fsl,vf610-flexcan", .data = &fsl_vf610_devtype_data, },
Hui Wang30c1e672012-06-28 16:21:35 +08001152 { /* sentinel */ },
1153};
Marc Kleine-Budde4358a9d2012-10-04 10:55:35 +02001154MODULE_DEVICE_TABLE(of, flexcan_of_match);
Hui Wang30c1e672012-06-28 16:21:35 +08001155
1156static const struct platform_device_id flexcan_id_table[] = {
1157 { .name = "flexcan", .driver_data = (kernel_ulong_t)&fsl_p1010_devtype_data, },
1158 { /* sentinel */ },
1159};
Marc Kleine-Budde4358a9d2012-10-04 10:55:35 +02001160MODULE_DEVICE_TABLE(platform, flexcan_id_table);
Hui Wang30c1e672012-06-28 16:21:35 +08001161
Bill Pemberton3c8ac0f2012-12-03 09:22:44 -05001162static int flexcan_probe(struct platform_device *pdev)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001163{
Hui Wang30c1e672012-06-28 16:21:35 +08001164 const struct of_device_id *of_id;
Marc Kleine-Buddedda0b3b2012-07-13 14:52:48 +02001165 const struct flexcan_devtype_data *devtype_data;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001166 struct net_device *dev;
1167 struct flexcan_priv *priv;
Andreas Werner555828e2015-03-22 17:35:52 +01001168 struct regulator *reg_xceiver;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001169 struct resource *mem;
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001170 struct clk *clk_ipg = NULL, *clk_per = NULL;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001171 void __iomem *base;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001172 int err, irq;
holt@sgi.com97efe9a2011-08-16 17:32:23 +00001173 u32 clock_freq = 0;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001174
Andreas Werner555828e2015-03-22 17:35:52 +01001175 reg_xceiver = devm_regulator_get(&pdev->dev, "xceiver");
1176 if (PTR_ERR(reg_xceiver) == -EPROBE_DEFER)
1177 return -EPROBE_DEFER;
1178 else if (IS_ERR(reg_xceiver))
1179 reg_xceiver = NULL;
1180
Hui Wangafc016d2012-06-28 16:21:34 +08001181 if (pdev->dev.of_node)
1182 of_property_read_u32(pdev->dev.of_node,
1183 "clock-frequency", &clock_freq);
holt@sgi.com97efe9a2011-08-16 17:32:23 +00001184
1185 if (!clock_freq) {
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001186 clk_ipg = devm_clk_get(&pdev->dev, "ipg");
1187 if (IS_ERR(clk_ipg)) {
1188 dev_err(&pdev->dev, "no ipg clock defined\n");
Fabio Estevam933e4af2013-07-22 12:41:39 -03001189 return PTR_ERR(clk_ipg);
holt@sgi.com97efe9a2011-08-16 17:32:23 +00001190 }
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001191
1192 clk_per = devm_clk_get(&pdev->dev, "per");
1193 if (IS_ERR(clk_per)) {
1194 dev_err(&pdev->dev, "no per clock defined\n");
Fabio Estevam933e4af2013-07-22 12:41:39 -03001195 return PTR_ERR(clk_per);
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001196 }
Marc Kleine-Budde1a3e5172013-11-25 22:15:20 +01001197 clock_freq = clk_get_rate(clk_per);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001198 }
1199
1200 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1201 irq = platform_get_irq(pdev, 0);
Fabio Estevam933e4af2013-07-22 12:41:39 -03001202 if (irq <= 0)
1203 return -ENODEV;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001204
Fabio Estevam933e4af2013-07-22 12:41:39 -03001205 base = devm_ioremap_resource(&pdev->dev, mem);
1206 if (IS_ERR(base))
1207 return PTR_ERR(base);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001208
Hui Wang30c1e672012-06-28 16:21:35 +08001209 of_id = of_match_device(flexcan_of_match, &pdev->dev);
1210 if (of_id) {
1211 devtype_data = of_id->data;
Marc Kleine-Budded0873e62014-03-04 22:04:22 +01001212 } else if (platform_get_device_id(pdev)->driver_data) {
Hui Wang30c1e672012-06-28 16:21:35 +08001213 devtype_data = (struct flexcan_devtype_data *)
Marc Kleine-Budded0873e62014-03-04 22:04:22 +01001214 platform_get_device_id(pdev)->driver_data;
Hui Wang30c1e672012-06-28 16:21:35 +08001215 } else {
Fabio Estevam933e4af2013-07-22 12:41:39 -03001216 return -ENODEV;
Hui Wang30c1e672012-06-28 16:21:35 +08001217 }
1218
Fabio Estevam933e4af2013-07-22 12:41:39 -03001219 dev = alloc_candev(sizeof(struct flexcan_priv), 1);
1220 if (!dev)
1221 return -ENOMEM;
1222
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001223 dev->netdev_ops = &flexcan_netdev_ops;
1224 dev->irq = irq;
Reuben Dowle9a123492011-11-01 11:18:03 +13001225 dev->flags |= IFF_ECHO;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001226
1227 priv = netdev_priv(dev);
holt@sgi.com97efe9a2011-08-16 17:32:23 +00001228 priv->can.clock.freq = clock_freq;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001229 priv->can.bittiming_const = &flexcan_bittiming_const;
1230 priv->can.do_set_mode = flexcan_set_mode;
1231 priv->can.do_get_berr_counter = flexcan_get_berr_counter;
1232 priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK |
1233 CAN_CTRLMODE_LISTENONLY | CAN_CTRLMODE_3_SAMPLES |
1234 CAN_CTRLMODE_BERR_REPORTING;
1235 priv->base = base;
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001236 priv->clk_ipg = clk_ipg;
1237 priv->clk_per = clk_per;
Jingoo Han84ae6642013-09-10 17:41:30 +09001238 priv->pdata = dev_get_platdata(&pdev->dev);
Hui Wang30c1e672012-06-28 16:21:35 +08001239 priv->devtype_data = devtype_data;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001240
Andreas Werner555828e2015-03-22 17:35:52 +01001241 priv->reg_xceiver = reg_xceiver;
Fabio Estevamb7c41142013-06-10 23:12:57 -03001242
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001243 netif_napi_add(dev, &priv->napi, flexcan_poll, FLEXCAN_NAPI_WEIGHT);
1244
Libo Chend75ea942013-08-21 18:15:08 +08001245 platform_set_drvdata(pdev, dev);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001246 SET_NETDEV_DEV(dev, &pdev->dev);
1247
1248 err = register_flexcandev(dev);
1249 if (err) {
1250 dev_err(&pdev->dev, "registering netdev failed\n");
1251 goto failed_register;
1252 }
1253
Fabio Baltieriadccadb2012-12-18 18:50:58 +01001254 devm_can_led_init(dev);
1255
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001256 dev_info(&pdev->dev, "device registered (reg_base=%p, irq=%d)\n",
1257 priv->base, dev->irq);
1258
1259 return 0;
1260
1261 failed_register:
1262 free_candev(dev);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001263 return err;
1264}
1265
Bill Pemberton3c8ac0f2012-12-03 09:22:44 -05001266static int flexcan_remove(struct platform_device *pdev)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001267{
1268 struct net_device *dev = platform_get_drvdata(pdev);
Marc Kleine-Budded96e43e2014-02-28 20:48:36 +01001269 struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001270
1271 unregister_flexcandev(dev);
Marc Kleine-Budded96e43e2014-02-28 20:48:36 +01001272 netif_napi_del(&priv->napi);
Marc Kleine-Budde9a275862010-10-21 05:07:58 +00001273 free_candev(dev);
1274
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001275 return 0;
1276}
1277
Marc Kleine-Budde08c6d352014-03-05 19:10:44 +01001278static int __maybe_unused flexcan_suspend(struct device *device)
Eric Bénard8b5e2182012-05-08 17:12:17 +02001279{
Fabio Estevam588e7a82013-05-20 15:43:43 -03001280 struct net_device *dev = dev_get_drvdata(device);
Eric Bénard8b5e2182012-05-08 17:12:17 +02001281 struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001282 int err;
Eric Bénard8b5e2182012-05-08 17:12:17 +02001283
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001284 err = flexcan_chip_disable(priv);
1285 if (err)
1286 return err;
Eric Bénard8b5e2182012-05-08 17:12:17 +02001287
1288 if (netif_running(dev)) {
1289 netif_stop_queue(dev);
1290 netif_device_detach(dev);
1291 }
1292 priv->can.state = CAN_STATE_SLEEPING;
1293
1294 return 0;
1295}
1296
Marc Kleine-Budde08c6d352014-03-05 19:10:44 +01001297static int __maybe_unused flexcan_resume(struct device *device)
Eric Bénard8b5e2182012-05-08 17:12:17 +02001298{
Fabio Estevam588e7a82013-05-20 15:43:43 -03001299 struct net_device *dev = dev_get_drvdata(device);
Eric Bénard8b5e2182012-05-08 17:12:17 +02001300 struct flexcan_priv *priv = netdev_priv(dev);
1301
1302 priv->can.state = CAN_STATE_ERROR_ACTIVE;
1303 if (netif_running(dev)) {
1304 netif_device_attach(dev);
1305 netif_start_queue(dev);
1306 }
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001307 return flexcan_chip_enable(priv);
Eric Bénard8b5e2182012-05-08 17:12:17 +02001308}
Fabio Estevam588e7a82013-05-20 15:43:43 -03001309
1310static SIMPLE_DEV_PM_OPS(flexcan_pm_ops, flexcan_suspend, flexcan_resume);
Eric Bénard8b5e2182012-05-08 17:12:17 +02001311
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001312static struct platform_driver flexcan_driver = {
holt@sgi.comc8aef4c2011-08-16 17:32:22 +00001313 .driver = {
1314 .name = DRV_NAME,
Fabio Estevam588e7a82013-05-20 15:43:43 -03001315 .pm = &flexcan_pm_ops,
holt@sgi.comc8aef4c2011-08-16 17:32:22 +00001316 .of_match_table = flexcan_of_match,
1317 },
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001318 .probe = flexcan_probe,
Bill Pemberton3c8ac0f2012-12-03 09:22:44 -05001319 .remove = flexcan_remove,
Hui Wang30c1e672012-06-28 16:21:35 +08001320 .id_table = flexcan_id_table,
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001321};
1322
Axel Lin871d3372011-11-27 15:42:31 +00001323module_platform_driver(flexcan_driver);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001324
1325MODULE_AUTHOR("Sascha Hauer <kernel@pengutronix.de>, "
1326 "Marc Kleine-Budde <kernel@pengutronix.de>");
1327MODULE_LICENSE("GPL v2");
1328MODULE_DESCRIPTION("CAN port driver for flexcan based chip");