blob: 70cdbfcebb46c2d3556e67a941828dea5a5fdf6b [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>
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +020029#include <linux/interrupt.h>
30#include <linux/io.h>
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +020031#include <linux/module.h>
holt@sgi.com97efe9a2011-08-16 17:32:23 +000032#include <linux/of.h>
Hui Wang30c1e672012-06-28 16:21:35 +080033#include <linux/of_device.h>
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +020034#include <linux/platform_device.h>
Fabio Estevamb7c41142013-06-10 23:12:57 -030035#include <linux/regulator/consumer.h>
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +020036
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +020037#define DRV_NAME "flexcan"
38
39/* 8 for RX fifo and 2 error handling */
40#define FLEXCAN_NAPI_WEIGHT (8 + 2)
41
42/* FLEXCAN module configuration register (CANMCR) bits */
43#define FLEXCAN_MCR_MDIS BIT(31)
44#define FLEXCAN_MCR_FRZ BIT(30)
45#define FLEXCAN_MCR_FEN BIT(29)
46#define FLEXCAN_MCR_HALT BIT(28)
47#define FLEXCAN_MCR_NOT_RDY BIT(27)
48#define FLEXCAN_MCR_WAK_MSK BIT(26)
49#define FLEXCAN_MCR_SOFTRST BIT(25)
50#define FLEXCAN_MCR_FRZ_ACK BIT(24)
51#define FLEXCAN_MCR_SUPV BIT(23)
52#define FLEXCAN_MCR_SLF_WAK BIT(22)
53#define FLEXCAN_MCR_WRN_EN BIT(21)
54#define FLEXCAN_MCR_LPM_ACK BIT(20)
55#define FLEXCAN_MCR_WAK_SRC BIT(19)
56#define FLEXCAN_MCR_DOZE BIT(18)
57#define FLEXCAN_MCR_SRX_DIS BIT(17)
58#define FLEXCAN_MCR_BCC BIT(16)
59#define FLEXCAN_MCR_LPRIO_EN BIT(13)
60#define FLEXCAN_MCR_AEN BIT(12)
Marc Kleine-Budde4c728d82014-09-02 16:54:17 +020061#define FLEXCAN_MCR_MAXMB(x) ((x) & 0x7f)
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +020062#define FLEXCAN_MCR_IDAM_A (0x0 << 8)
63#define FLEXCAN_MCR_IDAM_B (0x1 << 8)
64#define FLEXCAN_MCR_IDAM_C (0x2 << 8)
65#define FLEXCAN_MCR_IDAM_D (0x3 << 8)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +020066
67/* FLEXCAN control register (CANCTRL) bits */
68#define FLEXCAN_CTRL_PRESDIV(x) (((x) & 0xff) << 24)
69#define FLEXCAN_CTRL_RJW(x) (((x) & 0x03) << 22)
70#define FLEXCAN_CTRL_PSEG1(x) (((x) & 0x07) << 19)
71#define FLEXCAN_CTRL_PSEG2(x) (((x) & 0x07) << 16)
72#define FLEXCAN_CTRL_BOFF_MSK BIT(15)
73#define FLEXCAN_CTRL_ERR_MSK BIT(14)
74#define FLEXCAN_CTRL_CLK_SRC BIT(13)
75#define FLEXCAN_CTRL_LPB BIT(12)
76#define FLEXCAN_CTRL_TWRN_MSK BIT(11)
77#define FLEXCAN_CTRL_RWRN_MSK BIT(10)
78#define FLEXCAN_CTRL_SMP BIT(7)
79#define FLEXCAN_CTRL_BOFF_REC BIT(6)
80#define FLEXCAN_CTRL_TSYN BIT(5)
81#define FLEXCAN_CTRL_LBUF BIT(4)
82#define FLEXCAN_CTRL_LOM BIT(3)
83#define FLEXCAN_CTRL_PROPSEG(x) ((x) & 0x07)
84#define FLEXCAN_CTRL_ERR_BUS (FLEXCAN_CTRL_ERR_MSK)
85#define FLEXCAN_CTRL_ERR_STATE \
86 (FLEXCAN_CTRL_TWRN_MSK | FLEXCAN_CTRL_RWRN_MSK | \
87 FLEXCAN_CTRL_BOFF_MSK)
88#define FLEXCAN_CTRL_ERR_ALL \
89 (FLEXCAN_CTRL_ERR_BUS | FLEXCAN_CTRL_ERR_STATE)
90
Stefan Agnercdce8442014-07-15 14:56:21 +020091/* FLEXCAN control register 2 (CTRL2) bits */
Marc Kleine-Budde6f75fce2014-09-23 11:03:01 +020092#define FLEXCAN_CTRL2_ECRWRE BIT(29)
93#define FLEXCAN_CTRL2_WRMFRZ BIT(28)
94#define FLEXCAN_CTRL2_RFFN(x) (((x) & 0x0f) << 24)
95#define FLEXCAN_CTRL2_TASD(x) (((x) & 0x1f) << 19)
96#define FLEXCAN_CTRL2_MRP BIT(18)
97#define FLEXCAN_CTRL2_RRS BIT(17)
98#define FLEXCAN_CTRL2_EACEN BIT(16)
Stefan Agnercdce8442014-07-15 14:56:21 +020099
100/* FLEXCAN memory error control register (MECR) bits */
101#define FLEXCAN_MECR_ECRWRDIS BIT(31)
102#define FLEXCAN_MECR_HANCEI_MSK BIT(19)
103#define FLEXCAN_MECR_FANCEI_MSK BIT(18)
104#define FLEXCAN_MECR_CEI_MSK BIT(16)
105#define FLEXCAN_MECR_HAERRIE BIT(15)
106#define FLEXCAN_MECR_FAERRIE BIT(14)
107#define FLEXCAN_MECR_EXTERRIE BIT(13)
108#define FLEXCAN_MECR_RERRDIS BIT(9)
109#define FLEXCAN_MECR_ECCDIS BIT(8)
110#define FLEXCAN_MECR_NCEFAFRZ BIT(7)
111
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200112/* FLEXCAN error and status register (ESR) bits */
113#define FLEXCAN_ESR_TWRN_INT BIT(17)
114#define FLEXCAN_ESR_RWRN_INT BIT(16)
115#define FLEXCAN_ESR_BIT1_ERR BIT(15)
116#define FLEXCAN_ESR_BIT0_ERR BIT(14)
117#define FLEXCAN_ESR_ACK_ERR BIT(13)
118#define FLEXCAN_ESR_CRC_ERR BIT(12)
119#define FLEXCAN_ESR_FRM_ERR BIT(11)
120#define FLEXCAN_ESR_STF_ERR BIT(10)
121#define FLEXCAN_ESR_TX_WRN BIT(9)
122#define FLEXCAN_ESR_RX_WRN BIT(8)
123#define FLEXCAN_ESR_IDLE BIT(7)
124#define FLEXCAN_ESR_TXRX BIT(6)
125#define FLEXCAN_EST_FLT_CONF_SHIFT (4)
126#define FLEXCAN_ESR_FLT_CONF_MASK (0x3 << FLEXCAN_EST_FLT_CONF_SHIFT)
127#define FLEXCAN_ESR_FLT_CONF_ACTIVE (0x0 << FLEXCAN_EST_FLT_CONF_SHIFT)
128#define FLEXCAN_ESR_FLT_CONF_PASSIVE (0x1 << FLEXCAN_EST_FLT_CONF_SHIFT)
129#define FLEXCAN_ESR_BOFF_INT BIT(2)
130#define FLEXCAN_ESR_ERR_INT BIT(1)
131#define FLEXCAN_ESR_WAK_INT BIT(0)
132#define FLEXCAN_ESR_ERR_BUS \
133 (FLEXCAN_ESR_BIT1_ERR | FLEXCAN_ESR_BIT0_ERR | \
134 FLEXCAN_ESR_ACK_ERR | FLEXCAN_ESR_CRC_ERR | \
135 FLEXCAN_ESR_FRM_ERR | FLEXCAN_ESR_STF_ERR)
136#define FLEXCAN_ESR_ERR_STATE \
137 (FLEXCAN_ESR_TWRN_INT | FLEXCAN_ESR_RWRN_INT | FLEXCAN_ESR_BOFF_INT)
138#define FLEXCAN_ESR_ERR_ALL \
139 (FLEXCAN_ESR_ERR_BUS | FLEXCAN_ESR_ERR_STATE)
Wolfgang Grandegger6e9d5542011-12-12 16:09:28 +0100140#define FLEXCAN_ESR_ALL_INT \
141 (FLEXCAN_ESR_TWRN_INT | FLEXCAN_ESR_RWRN_INT | \
142 FLEXCAN_ESR_BOFF_INT | FLEXCAN_ESR_ERR_INT)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200143
144/* FLEXCAN interrupt flag register (IFLAG) bits */
David Jander25e92442014-09-03 16:47:22 +0200145/* Errata ERR005829 step7: Reserve first valid MB */
146#define FLEXCAN_TX_BUF_RESERVED 8
147#define FLEXCAN_TX_BUF_ID 9
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200148#define FLEXCAN_IFLAG_BUF(x) BIT(x)
149#define FLEXCAN_IFLAG_RX_FIFO_OVERFLOW BIT(7)
150#define FLEXCAN_IFLAG_RX_FIFO_WARN BIT(6)
151#define FLEXCAN_IFLAG_RX_FIFO_AVAILABLE BIT(5)
152#define FLEXCAN_IFLAG_DEFAULT \
153 (FLEXCAN_IFLAG_RX_FIFO_OVERFLOW | FLEXCAN_IFLAG_RX_FIFO_AVAILABLE | \
154 FLEXCAN_IFLAG_BUF(FLEXCAN_TX_BUF_ID))
155
156/* FLEXCAN message buffers */
Marc Kleine-Buddec32fe4a2014-09-16 12:39:28 +0200157#define FLEXCAN_MB_CODE_RX_INACTIVE (0x0 << 24)
158#define FLEXCAN_MB_CODE_RX_EMPTY (0x4 << 24)
159#define FLEXCAN_MB_CODE_RX_FULL (0x2 << 24)
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200160#define FLEXCAN_MB_CODE_RX_OVERRUN (0x6 << 24)
Marc Kleine-Buddec32fe4a2014-09-16 12:39:28 +0200161#define FLEXCAN_MB_CODE_RX_RANSWER (0xa << 24)
162
163#define FLEXCAN_MB_CODE_TX_INACTIVE (0x8 << 24)
164#define FLEXCAN_MB_CODE_TX_ABORT (0x9 << 24)
165#define FLEXCAN_MB_CODE_TX_DATA (0xc << 24)
166#define FLEXCAN_MB_CODE_TX_TANSWER (0xe << 24)
167
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200168#define FLEXCAN_MB_CNT_SRR BIT(22)
169#define FLEXCAN_MB_CNT_IDE BIT(21)
170#define FLEXCAN_MB_CNT_RTR BIT(20)
171#define FLEXCAN_MB_CNT_LENGTH(x) (((x) & 0xf) << 16)
172#define FLEXCAN_MB_CNT_TIMESTAMP(x) ((x) & 0xffff)
173
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200174#define FLEXCAN_TIMEOUT_US (50)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200175
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200176/* FLEXCAN hardware feature flags
Wolfgang Grandeggerbb698ca2012-10-10 21:10:42 +0200177 *
178 * Below is some version info we got:
David Jander8a1ce7e2014-10-10 15:04:03 +0200179 * SOC Version IP-Version Glitch- [TR]WRN_INT Memory err RTR re-
180 * Filter? connected? detection ception in MB
181 * MX25 FlexCAN2 03.00.00.00 no no no no
182 * MX28 FlexCAN2 03.00.04.00 yes yes no no
183 * MX35 FlexCAN2 03.00.00.00 no no no no
184 * MX53 FlexCAN2 03.00.00.00 yes no no no
185 * MX6s FlexCAN3 10.00.12.00 yes yes no yes
186 * VF610 FlexCAN3 ? no yes yes yes?
Wolfgang Grandeggerbb698ca2012-10-10 21:10:42 +0200187 *
188 * Some SOCs do not have the RX_WARN & TX_WARN interrupt line connected.
189 */
Wolfgang Grandegger4f72e5f2012-09-28 03:17:15 +0000190#define FLEXCAN_HAS_V10_FEATURES BIT(1) /* For core version >= 10 */
Wolfgang Grandeggerbb698ca2012-10-10 21:10:42 +0200191#define FLEXCAN_HAS_BROKEN_ERR_STATE BIT(2) /* [TR]WRN_INT not connected */
Stefan Agnercdce8442014-07-15 14:56:21 +0200192#define FLEXCAN_HAS_MECR_FEATURES BIT(3) /* Memory error detection */
Wolfgang Grandegger4f72e5f2012-09-28 03:17:15 +0000193
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200194/* Structure of the message buffer */
195struct flexcan_mb {
196 u32 can_ctrl;
197 u32 can_id;
198 u32 data[2];
199};
200
201/* Structure of the hardware registers */
202struct flexcan_regs {
203 u32 mcr; /* 0x00 */
204 u32 ctrl; /* 0x04 */
205 u32 timer; /* 0x08 */
206 u32 _reserved1; /* 0x0c */
207 u32 rxgmask; /* 0x10 */
208 u32 rx14mask; /* 0x14 */
209 u32 rx15mask; /* 0x18 */
210 u32 ecr; /* 0x1c */
211 u32 esr; /* 0x20 */
212 u32 imask2; /* 0x24 */
213 u32 imask1; /* 0x28 */
214 u32 iflag2; /* 0x2c */
215 u32 iflag1; /* 0x30 */
Marc Kleine-Budde6f75fce2014-09-23 11:03:01 +0200216 u32 ctrl2; /* 0x34 */
Hui Wang30c1e672012-06-28 16:21:35 +0800217 u32 esr2; /* 0x38 */
218 u32 imeur; /* 0x3c */
219 u32 lrfr; /* 0x40 */
220 u32 crcr; /* 0x44 */
221 u32 rxfgmask; /* 0x48 */
222 u32 rxfir; /* 0x4c */
Stefan Agnercdce8442014-07-15 14:56:21 +0200223 u32 _reserved3[12]; /* 0x50 */
224 struct flexcan_mb cantxfg[64]; /* 0x80 */
Marc Kleine-Budde66a6ef02014-09-17 12:50:48 +0200225 /* FIFO-mode:
226 * MB
227 * 0x080...0x08f 0 RX message buffer
228 * 0x090...0x0df 1-5 reserverd
229 * 0x0e0...0x0ff 6-7 8 entry ID table
230 * (mx25, mx28, mx35, mx53)
231 * 0x0e0...0x2df 6-7..37 8..128 entry ID table
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200232 * size conf'ed via ctrl2::RFFN
Marc Kleine-Budde66a6ef02014-09-17 12:50:48 +0200233 * (mx6, vf610)
234 */
Stefan Agnercdce8442014-07-15 14:56:21 +0200235 u32 _reserved4[408];
236 u32 mecr; /* 0xae0 */
237 u32 erriar; /* 0xae4 */
238 u32 erridpr; /* 0xae8 */
239 u32 errippr; /* 0xaec */
240 u32 rerrar; /* 0xaf0 */
241 u32 rerrdr; /* 0xaf4 */
242 u32 rerrsynr; /* 0xaf8 */
243 u32 errsr; /* 0xafc */
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200244};
245
Hui Wang30c1e672012-06-28 16:21:35 +0800246struct flexcan_devtype_data {
Wolfgang Grandegger4f72e5f2012-09-28 03:17:15 +0000247 u32 features; /* hardware controller features */
Hui Wang30c1e672012-06-28 16:21:35 +0800248};
249
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200250struct flexcan_priv {
251 struct can_priv can;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200252 struct napi_struct napi;
253
254 void __iomem *base;
255 u32 reg_esr;
256 u32 reg_ctrl_default;
257
Steffen Trumtrar3d42a372012-07-17 16:14:34 +0200258 struct clk *clk_ipg;
259 struct clk *clk_per;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200260 struct flexcan_platform_data *pdata;
Marc Kleine-Buddedda0b3b2012-07-13 14:52:48 +0200261 const struct flexcan_devtype_data *devtype_data;
Fabio Estevamb7c41142013-06-10 23:12:57 -0300262 struct regulator *reg_xceiver;
Hui Wang30c1e672012-06-28 16:21:35 +0800263};
264
265static struct flexcan_devtype_data fsl_p1010_devtype_data = {
Wolfgang Grandegger4f72e5f2012-09-28 03:17:15 +0000266 .features = FLEXCAN_HAS_BROKEN_ERR_STATE,
Hui Wang30c1e672012-06-28 16:21:35 +0800267};
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200268
Wolfgang Grandegger4f72e5f2012-09-28 03:17:15 +0000269static struct flexcan_devtype_data fsl_imx28_devtype_data;
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200270
Hui Wang30c1e672012-06-28 16:21:35 +0800271static struct flexcan_devtype_data fsl_imx6q_devtype_data = {
Wolfgang Grandeggerbb698ca2012-10-10 21:10:42 +0200272 .features = FLEXCAN_HAS_V10_FEATURES,
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200273};
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200274
Stefan Agnercdce8442014-07-15 14:56:21 +0200275static struct flexcan_devtype_data fsl_vf610_devtype_data = {
276 .features = FLEXCAN_HAS_V10_FEATURES | FLEXCAN_HAS_MECR_FEATURES,
277};
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200278
Marc Kleine-Budde194b9a42012-07-16 12:58:31 +0200279static const struct can_bittiming_const flexcan_bittiming_const = {
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200280 .name = DRV_NAME,
281 .tseg1_min = 4,
282 .tseg1_max = 16,
283 .tseg2_min = 2,
284 .tseg2_max = 8,
285 .sjw_max = 4,
286 .brp_min = 1,
287 .brp_max = 256,
288 .brp_inc = 1,
289};
290
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200291/* Abstract off the read/write for arm versus ppc. This
Arnd Bergmann0e4b9492014-01-14 11:44:09 +0100292 * assumes that PPC uses big-endian registers and everything
293 * else uses little-endian registers, independent of CPU
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200294 * endianness.
holt@sgi.com61e271e2011-08-16 17:32:20 +0000295 */
Arnd Bergmann0e4b9492014-01-14 11:44:09 +0100296#if defined(CONFIG_PPC)
holt@sgi.com61e271e2011-08-16 17:32:20 +0000297static inline u32 flexcan_read(void __iomem *addr)
298{
299 return in_be32(addr);
300}
301
302static inline void flexcan_write(u32 val, void __iomem *addr)
303{
304 out_be32(addr, val);
305}
306#else
307static inline u32 flexcan_read(void __iomem *addr)
308{
309 return readl(addr);
310}
311
312static inline void flexcan_write(u32 val, void __iomem *addr)
313{
314 writel(val, addr);
315}
316#endif
317
Marc Kleine-Buddef0036982014-02-28 17:18:27 +0100318static inline int flexcan_transceiver_enable(const struct flexcan_priv *priv)
319{
320 if (!priv->reg_xceiver)
321 return 0;
322
323 return regulator_enable(priv->reg_xceiver);
324}
325
326static inline int flexcan_transceiver_disable(const struct flexcan_priv *priv)
327{
328 if (!priv->reg_xceiver)
329 return 0;
330
331 return regulator_disable(priv->reg_xceiver);
332}
333
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200334static inline int flexcan_has_and_handle_berr(const struct flexcan_priv *priv,
335 u32 reg_esr)
336{
337 return (priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING) &&
338 (reg_esr & FLEXCAN_ESR_ERR_BUS);
339}
340
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100341static int flexcan_chip_enable(struct flexcan_priv *priv)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200342{
343 struct flexcan_regs __iomem *regs = priv->base;
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100344 unsigned int timeout = FLEXCAN_TIMEOUT_US / 10;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200345 u32 reg;
346
holt@sgi.com61e271e2011-08-16 17:32:20 +0000347 reg = flexcan_read(&regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200348 reg &= ~FLEXCAN_MCR_MDIS;
holt@sgi.com61e271e2011-08-16 17:32:20 +0000349 flexcan_write(reg, &regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200350
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100351 while (timeout-- && (flexcan_read(&regs->mcr) & FLEXCAN_MCR_LPM_ACK))
David Jander8badd652014-08-27 12:02:16 +0200352 udelay(10);
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100353
354 if (flexcan_read(&regs->mcr) & FLEXCAN_MCR_LPM_ACK)
355 return -ETIMEDOUT;
356
357 return 0;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200358}
359
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100360static int flexcan_chip_disable(struct flexcan_priv *priv)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200361{
362 struct flexcan_regs __iomem *regs = priv->base;
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100363 unsigned int timeout = FLEXCAN_TIMEOUT_US / 10;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200364 u32 reg;
365
holt@sgi.com61e271e2011-08-16 17:32:20 +0000366 reg = flexcan_read(&regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200367 reg |= FLEXCAN_MCR_MDIS;
holt@sgi.com61e271e2011-08-16 17:32:20 +0000368 flexcan_write(reg, &regs->mcr);
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100369
370 while (timeout-- && !(flexcan_read(&regs->mcr) & FLEXCAN_MCR_LPM_ACK))
David Jander8badd652014-08-27 12:02:16 +0200371 udelay(10);
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100372
373 if (!(flexcan_read(&regs->mcr) & FLEXCAN_MCR_LPM_ACK))
374 return -ETIMEDOUT;
375
376 return 0;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200377}
378
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100379static int flexcan_chip_freeze(struct flexcan_priv *priv)
380{
381 struct flexcan_regs __iomem *regs = priv->base;
382 unsigned int timeout = 1000 * 1000 * 10 / priv->can.bittiming.bitrate;
383 u32 reg;
384
385 reg = flexcan_read(&regs->mcr);
386 reg |= FLEXCAN_MCR_HALT;
387 flexcan_write(reg, &regs->mcr);
388
389 while (timeout-- && !(flexcan_read(&regs->mcr) & FLEXCAN_MCR_FRZ_ACK))
David Jander8badd652014-08-27 12:02:16 +0200390 udelay(100);
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100391
392 if (!(flexcan_read(&regs->mcr) & FLEXCAN_MCR_FRZ_ACK))
393 return -ETIMEDOUT;
394
395 return 0;
396}
397
398static int flexcan_chip_unfreeze(struct flexcan_priv *priv)
399{
400 struct flexcan_regs __iomem *regs = priv->base;
401 unsigned int timeout = FLEXCAN_TIMEOUT_US / 10;
402 u32 reg;
403
404 reg = flexcan_read(&regs->mcr);
405 reg &= ~FLEXCAN_MCR_HALT;
406 flexcan_write(reg, &regs->mcr);
407
408 while (timeout-- && (flexcan_read(&regs->mcr) & FLEXCAN_MCR_FRZ_ACK))
David Jander8badd652014-08-27 12:02:16 +0200409 udelay(10);
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100410
411 if (flexcan_read(&regs->mcr) & FLEXCAN_MCR_FRZ_ACK)
412 return -ETIMEDOUT;
413
414 return 0;
415}
416
Marc Kleine-Budde4b5b8222014-02-28 15:16:59 +0100417static int flexcan_chip_softreset(struct flexcan_priv *priv)
418{
419 struct flexcan_regs __iomem *regs = priv->base;
420 unsigned int timeout = FLEXCAN_TIMEOUT_US / 10;
421
422 flexcan_write(FLEXCAN_MCR_SOFTRST, &regs->mcr);
423 while (timeout-- && (flexcan_read(&regs->mcr) & FLEXCAN_MCR_SOFTRST))
David Jander8badd652014-08-27 12:02:16 +0200424 udelay(10);
Marc Kleine-Budde4b5b8222014-02-28 15:16:59 +0100425
426 if (flexcan_read(&regs->mcr) & FLEXCAN_MCR_SOFTRST)
427 return -ETIMEDOUT;
428
429 return 0;
430}
431
Stefan Agnerec56acf2014-07-15 14:56:20 +0200432static int __flexcan_get_berr_counter(const struct net_device *dev,
433 struct can_berr_counter *bec)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200434{
435 const struct flexcan_priv *priv = netdev_priv(dev);
436 struct flexcan_regs __iomem *regs = priv->base;
holt@sgi.com61e271e2011-08-16 17:32:20 +0000437 u32 reg = flexcan_read(&regs->ecr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200438
439 bec->txerr = (reg >> 0) & 0xff;
440 bec->rxerr = (reg >> 8) & 0xff;
441
442 return 0;
443}
444
Stefan Agnerec56acf2014-07-15 14:56:20 +0200445static int flexcan_get_berr_counter(const struct net_device *dev,
446 struct can_berr_counter *bec)
447{
448 const struct flexcan_priv *priv = netdev_priv(dev);
449 int err;
450
451 err = clk_prepare_enable(priv->clk_ipg);
452 if (err)
453 return err;
454
455 err = clk_prepare_enable(priv->clk_per);
456 if (err)
457 goto out_disable_ipg;
458
459 err = __flexcan_get_berr_counter(dev, bec);
460
461 clk_disable_unprepare(priv->clk_per);
462 out_disable_ipg:
463 clk_disable_unprepare(priv->clk_ipg);
464
465 return err;
466}
467
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200468static int flexcan_start_xmit(struct sk_buff *skb, struct net_device *dev)
469{
470 const struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200471 struct flexcan_regs __iomem *regs = priv->base;
472 struct can_frame *cf = (struct can_frame *)skb->data;
473 u32 can_id;
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200474 u32 data;
Marc Kleine-Budde10d089b2014-09-23 11:18:11 +0200475 u32 ctrl = FLEXCAN_MB_CODE_TX_DATA | (cf->can_dlc << 16);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200476
477 if (can_dropped_invalid_skb(dev, skb))
478 return NETDEV_TX_OK;
479
480 netif_stop_queue(dev);
481
482 if (cf->can_id & CAN_EFF_FLAG) {
483 can_id = cf->can_id & CAN_EFF_MASK;
484 ctrl |= FLEXCAN_MB_CNT_IDE | FLEXCAN_MB_CNT_SRR;
485 } else {
486 can_id = (cf->can_id & CAN_SFF_MASK) << 18;
487 }
488
489 if (cf->can_id & CAN_RTR_FLAG)
490 ctrl |= FLEXCAN_MB_CNT_RTR;
491
492 if (cf->can_dlc > 0) {
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200493 data = be32_to_cpup((__be32 *)&cf->data[0]);
holt@sgi.com61e271e2011-08-16 17:32:20 +0000494 flexcan_write(data, &regs->cantxfg[FLEXCAN_TX_BUF_ID].data[0]);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200495 }
496 if (cf->can_dlc > 3) {
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200497 data = be32_to_cpup((__be32 *)&cf->data[4]);
holt@sgi.com61e271e2011-08-16 17:32:20 +0000498 flexcan_write(data, &regs->cantxfg[FLEXCAN_TX_BUF_ID].data[1]);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200499 }
500
Reuben Dowle9a123492011-11-01 11:18:03 +1300501 can_put_echo_skb(skb, dev, 0);
502
holt@sgi.com61e271e2011-08-16 17:32:20 +0000503 flexcan_write(can_id, &regs->cantxfg[FLEXCAN_TX_BUF_ID].can_id);
504 flexcan_write(ctrl, &regs->cantxfg[FLEXCAN_TX_BUF_ID].can_ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200505
David Jander25e92442014-09-03 16:47:22 +0200506 /* Errata ERR005829 step8:
507 * Write twice INACTIVE(0x8) code to first MB.
508 */
509 flexcan_write(FLEXCAN_MB_CODE_TX_INACTIVE,
510 &regs->cantxfg[FLEXCAN_TX_BUF_RESERVED].can_ctrl);
511 flexcan_write(FLEXCAN_MB_CODE_TX_INACTIVE,
512 &regs->cantxfg[FLEXCAN_TX_BUF_RESERVED].can_ctrl);
513
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200514 return NETDEV_TX_OK;
515}
516
517static void do_bus_err(struct net_device *dev,
518 struct can_frame *cf, u32 reg_esr)
519{
520 struct flexcan_priv *priv = netdev_priv(dev);
521 int rx_errors = 0, tx_errors = 0;
522
523 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
524
525 if (reg_esr & FLEXCAN_ESR_BIT1_ERR) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100526 netdev_dbg(dev, "BIT1_ERR irq\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200527 cf->data[2] |= CAN_ERR_PROT_BIT1;
528 tx_errors = 1;
529 }
530 if (reg_esr & FLEXCAN_ESR_BIT0_ERR) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100531 netdev_dbg(dev, "BIT0_ERR irq\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200532 cf->data[2] |= CAN_ERR_PROT_BIT0;
533 tx_errors = 1;
534 }
535 if (reg_esr & FLEXCAN_ESR_ACK_ERR) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100536 netdev_dbg(dev, "ACK_ERR irq\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200537 cf->can_id |= CAN_ERR_ACK;
538 cf->data[3] |= CAN_ERR_PROT_LOC_ACK;
539 tx_errors = 1;
540 }
541 if (reg_esr & FLEXCAN_ESR_CRC_ERR) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100542 netdev_dbg(dev, "CRC_ERR irq\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200543 cf->data[2] |= CAN_ERR_PROT_BIT;
544 cf->data[3] |= CAN_ERR_PROT_LOC_CRC_SEQ;
545 rx_errors = 1;
546 }
547 if (reg_esr & FLEXCAN_ESR_FRM_ERR) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100548 netdev_dbg(dev, "FRM_ERR irq\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200549 cf->data[2] |= CAN_ERR_PROT_FORM;
550 rx_errors = 1;
551 }
552 if (reg_esr & FLEXCAN_ESR_STF_ERR) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100553 netdev_dbg(dev, "STF_ERR irq\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200554 cf->data[2] |= CAN_ERR_PROT_STUFF;
555 rx_errors = 1;
556 }
557
558 priv->can.can_stats.bus_error++;
559 if (rx_errors)
560 dev->stats.rx_errors++;
561 if (tx_errors)
562 dev->stats.tx_errors++;
563}
564
565static int flexcan_poll_bus_err(struct net_device *dev, u32 reg_esr)
566{
567 struct sk_buff *skb;
568 struct can_frame *cf;
569
570 skb = alloc_can_err_skb(dev, &cf);
571 if (unlikely(!skb))
572 return 0;
573
574 do_bus_err(dev, cf, reg_esr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200575
576 dev->stats.rx_packets++;
577 dev->stats.rx_bytes += cf->can_dlc;
Marc Kleine-Buddea18ec1b2015-05-08 11:30:29 +0200578 netif_receive_skb(skb);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200579
580 return 1;
581}
582
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200583static int flexcan_poll_state(struct net_device *dev, u32 reg_esr)
584{
585 struct flexcan_priv *priv = netdev_priv(dev);
586 struct sk_buff *skb;
587 struct can_frame *cf;
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000588 enum can_state new_state = 0, rx_state = 0, tx_state = 0;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200589 int flt;
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000590 struct can_berr_counter bec;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200591
592 flt = reg_esr & FLEXCAN_ESR_FLT_CONF_MASK;
593 if (likely(flt == FLEXCAN_ESR_FLT_CONF_ACTIVE)) {
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000594 tx_state = unlikely(reg_esr & FLEXCAN_ESR_TX_WRN) ?
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200595 CAN_STATE_ERROR_WARNING : CAN_STATE_ERROR_ACTIVE;
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000596 rx_state = unlikely(reg_esr & FLEXCAN_ESR_RX_WRN) ?
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200597 CAN_STATE_ERROR_WARNING : CAN_STATE_ERROR_ACTIVE;
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000598 new_state = max(tx_state, rx_state);
Andri Yngvason258ce802015-03-17 13:03:09 +0000599 } else {
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000600 __flexcan_get_berr_counter(dev, &bec);
Andri Yngvason258ce802015-03-17 13:03:09 +0000601 new_state = flt == FLEXCAN_ESR_FLT_CONF_PASSIVE ?
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200602 CAN_STATE_ERROR_PASSIVE : CAN_STATE_BUS_OFF;
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000603 rx_state = bec.rxerr >= bec.txerr ? new_state : 0;
604 tx_state = bec.rxerr <= bec.txerr ? new_state : 0;
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000605 }
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200606
607 /* state hasn't changed */
608 if (likely(new_state == priv->can.state))
609 return 0;
610
611 skb = alloc_can_err_skb(dev, &cf);
612 if (unlikely(!skb))
613 return 0;
614
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000615 can_change_state(dev, cf, tx_state, rx_state);
616
617 if (unlikely(new_state == CAN_STATE_BUS_OFF))
618 can_bus_off(dev);
619
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200620 dev->stats.rx_packets++;
621 dev->stats.rx_bytes += cf->can_dlc;
Marc Kleine-Buddea18ec1b2015-05-08 11:30:29 +0200622 netif_receive_skb(skb);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200623
624 return 1;
625}
626
627static void flexcan_read_fifo(const struct net_device *dev,
628 struct can_frame *cf)
629{
630 const struct flexcan_priv *priv = netdev_priv(dev);
631 struct flexcan_regs __iomem *regs = priv->base;
632 struct flexcan_mb __iomem *mb = &regs->cantxfg[0];
633 u32 reg_ctrl, reg_id;
634
holt@sgi.com61e271e2011-08-16 17:32:20 +0000635 reg_ctrl = flexcan_read(&mb->can_ctrl);
636 reg_id = flexcan_read(&mb->can_id);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200637 if (reg_ctrl & FLEXCAN_MB_CNT_IDE)
638 cf->can_id = ((reg_id >> 0) & CAN_EFF_MASK) | CAN_EFF_FLAG;
639 else
640 cf->can_id = (reg_id >> 18) & CAN_SFF_MASK;
641
642 if (reg_ctrl & FLEXCAN_MB_CNT_RTR)
643 cf->can_id |= CAN_RTR_FLAG;
644 cf->can_dlc = get_can_dlc((reg_ctrl >> 16) & 0xf);
645
holt@sgi.com61e271e2011-08-16 17:32:20 +0000646 *(__be32 *)(cf->data + 0) = cpu_to_be32(flexcan_read(&mb->data[0]));
647 *(__be32 *)(cf->data + 4) = cpu_to_be32(flexcan_read(&mb->data[1]));
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200648
649 /* mark as read */
holt@sgi.com61e271e2011-08-16 17:32:20 +0000650 flexcan_write(FLEXCAN_IFLAG_RX_FIFO_AVAILABLE, &regs->iflag1);
651 flexcan_read(&regs->timer);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200652}
653
654static int flexcan_read_frame(struct net_device *dev)
655{
656 struct net_device_stats *stats = &dev->stats;
657 struct can_frame *cf;
658 struct sk_buff *skb;
659
660 skb = alloc_can_skb(dev, &cf);
661 if (unlikely(!skb)) {
662 stats->rx_dropped++;
663 return 0;
664 }
665
666 flexcan_read_fifo(dev, cf);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200667
668 stats->rx_packets++;
669 stats->rx_bytes += cf->can_dlc;
Marc Kleine-Buddea18ec1b2015-05-08 11:30:29 +0200670 netif_receive_skb(skb);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200671
Fabio Baltieriadccadb2012-12-18 18:50:58 +0100672 can_led_event(dev, CAN_LED_EVENT_RX);
673
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200674 return 1;
675}
676
677static int flexcan_poll(struct napi_struct *napi, int quota)
678{
679 struct net_device *dev = napi->dev;
680 const struct flexcan_priv *priv = netdev_priv(dev);
681 struct flexcan_regs __iomem *regs = priv->base;
682 u32 reg_iflag1, reg_esr;
683 int work_done = 0;
684
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200685 /* The error bits are cleared on read,
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200686 * use saved value from irq handler.
687 */
holt@sgi.com61e271e2011-08-16 17:32:20 +0000688 reg_esr = flexcan_read(&regs->esr) | priv->reg_esr;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200689
690 /* handle state changes */
691 work_done += flexcan_poll_state(dev, reg_esr);
692
693 /* handle RX-FIFO */
holt@sgi.com61e271e2011-08-16 17:32:20 +0000694 reg_iflag1 = flexcan_read(&regs->iflag1);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200695 while (reg_iflag1 & FLEXCAN_IFLAG_RX_FIFO_AVAILABLE &&
696 work_done < quota) {
697 work_done += flexcan_read_frame(dev);
holt@sgi.com61e271e2011-08-16 17:32:20 +0000698 reg_iflag1 = flexcan_read(&regs->iflag1);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200699 }
700
701 /* report bus errors */
702 if (flexcan_has_and_handle_berr(priv, reg_esr) && work_done < quota)
703 work_done += flexcan_poll_bus_err(dev, reg_esr);
704
705 if (work_done < quota) {
706 napi_complete(napi);
707 /* enable IRQs */
holt@sgi.com61e271e2011-08-16 17:32:20 +0000708 flexcan_write(FLEXCAN_IFLAG_DEFAULT, &regs->imask1);
709 flexcan_write(priv->reg_ctrl_default, &regs->ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200710 }
711
712 return work_done;
713}
714
715static irqreturn_t flexcan_irq(int irq, void *dev_id)
716{
717 struct net_device *dev = dev_id;
718 struct net_device_stats *stats = &dev->stats;
719 struct flexcan_priv *priv = netdev_priv(dev);
720 struct flexcan_regs __iomem *regs = priv->base;
721 u32 reg_iflag1, reg_esr;
722
holt@sgi.com61e271e2011-08-16 17:32:20 +0000723 reg_iflag1 = flexcan_read(&regs->iflag1);
724 reg_esr = flexcan_read(&regs->esr);
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200725
Wolfgang Grandegger6e9d5542011-12-12 16:09:28 +0100726 /* ACK all bus error and state change IRQ sources */
727 if (reg_esr & FLEXCAN_ESR_ALL_INT)
728 flexcan_write(reg_esr & FLEXCAN_ESR_ALL_INT, &regs->esr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200729
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200730 /* schedule NAPI in case of:
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200731 * - rx IRQ
732 * - state change IRQ
733 * - bus error IRQ and bus error reporting is activated
734 */
735 if ((reg_iflag1 & FLEXCAN_IFLAG_RX_FIFO_AVAILABLE) ||
736 (reg_esr & FLEXCAN_ESR_ERR_STATE) ||
737 flexcan_has_and_handle_berr(priv, reg_esr)) {
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200738 /* The error bits are cleared on read,
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200739 * save them for later use.
740 */
741 priv->reg_esr = reg_esr & FLEXCAN_ESR_ERR_BUS;
holt@sgi.com61e271e2011-08-16 17:32:20 +0000742 flexcan_write(FLEXCAN_IFLAG_DEFAULT &
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200743 ~FLEXCAN_IFLAG_RX_FIFO_AVAILABLE, &regs->imask1);
holt@sgi.com61e271e2011-08-16 17:32:20 +0000744 flexcan_write(priv->reg_ctrl_default & ~FLEXCAN_CTRL_ERR_ALL,
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200745 &regs->ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200746 napi_schedule(&priv->napi);
747 }
748
749 /* FIFO overflow */
750 if (reg_iflag1 & FLEXCAN_IFLAG_RX_FIFO_OVERFLOW) {
holt@sgi.com61e271e2011-08-16 17:32:20 +0000751 flexcan_write(FLEXCAN_IFLAG_RX_FIFO_OVERFLOW, &regs->iflag1);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200752 dev->stats.rx_over_errors++;
753 dev->stats.rx_errors++;
754 }
755
756 /* transmission complete interrupt */
757 if (reg_iflag1 & (1 << FLEXCAN_TX_BUF_ID)) {
Reuben Dowle9a123492011-11-01 11:18:03 +1300758 stats->tx_bytes += can_get_echo_skb(dev, 0);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200759 stats->tx_packets++;
Fabio Baltieriadccadb2012-12-18 18:50:58 +0100760 can_led_event(dev, CAN_LED_EVENT_TX);
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200761
762 /* after sending a RTR frame MB is in RX mode */
Marc Kleine-Buddede594482014-09-16 15:31:27 +0200763 flexcan_write(FLEXCAN_MB_CODE_TX_INACTIVE,
764 &regs->cantxfg[FLEXCAN_TX_BUF_ID].can_ctrl);
holt@sgi.com61e271e2011-08-16 17:32:20 +0000765 flexcan_write((1 << FLEXCAN_TX_BUF_ID), &regs->iflag1);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200766 netif_wake_queue(dev);
767 }
768
769 return IRQ_HANDLED;
770}
771
772static void flexcan_set_bittiming(struct net_device *dev)
773{
774 const struct flexcan_priv *priv = netdev_priv(dev);
775 const struct can_bittiming *bt = &priv->can.bittiming;
776 struct flexcan_regs __iomem *regs = priv->base;
777 u32 reg;
778
holt@sgi.com61e271e2011-08-16 17:32:20 +0000779 reg = flexcan_read(&regs->ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200780 reg &= ~(FLEXCAN_CTRL_PRESDIV(0xff) |
781 FLEXCAN_CTRL_RJW(0x3) |
782 FLEXCAN_CTRL_PSEG1(0x7) |
783 FLEXCAN_CTRL_PSEG2(0x7) |
784 FLEXCAN_CTRL_PROPSEG(0x7) |
785 FLEXCAN_CTRL_LPB |
786 FLEXCAN_CTRL_SMP |
787 FLEXCAN_CTRL_LOM);
788
789 reg |= FLEXCAN_CTRL_PRESDIV(bt->brp - 1) |
790 FLEXCAN_CTRL_PSEG1(bt->phase_seg1 - 1) |
791 FLEXCAN_CTRL_PSEG2(bt->phase_seg2 - 1) |
792 FLEXCAN_CTRL_RJW(bt->sjw - 1) |
793 FLEXCAN_CTRL_PROPSEG(bt->prop_seg - 1);
794
795 if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)
796 reg |= FLEXCAN_CTRL_LPB;
797 if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
798 reg |= FLEXCAN_CTRL_LOM;
799 if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
800 reg |= FLEXCAN_CTRL_SMP;
801
Lucas Stach7a4b6c82015-08-07 17:16:03 +0200802 netdev_dbg(dev, "writing ctrl=0x%08x\n", reg);
holt@sgi.com61e271e2011-08-16 17:32:20 +0000803 flexcan_write(reg, &regs->ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200804
805 /* print chip status */
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100806 netdev_dbg(dev, "%s: mcr=0x%08x ctrl=0x%08x\n", __func__,
807 flexcan_read(&regs->mcr), flexcan_read(&regs->ctrl));
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200808}
809
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200810/* flexcan_chip_start
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200811 *
812 * this functions is entered with clocks enabled
813 *
814 */
815static int flexcan_chip_start(struct net_device *dev)
816{
817 struct flexcan_priv *priv = netdev_priv(dev);
818 struct flexcan_regs __iomem *regs = priv->base;
Marc Kleine-Budde6f75fce2014-09-23 11:03:01 +0200819 u32 reg_mcr, reg_ctrl, reg_ctrl2, reg_mecr;
David S. Miller1f6d8032014-09-23 12:09:27 -0400820 int err, i;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200821
822 /* enable module */
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100823 err = flexcan_chip_enable(priv);
824 if (err)
825 return err;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200826
827 /* soft reset */
Marc Kleine-Budde4b5b8222014-02-28 15:16:59 +0100828 err = flexcan_chip_softreset(priv);
829 if (err)
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100830 goto out_chip_disable;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200831
832 flexcan_set_bittiming(dev);
833
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200834 /* MCR
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200835 *
836 * enable freeze
837 * enable fifo
838 * halt now
839 * only supervisor access
840 * enable warning int
Reuben Dowle9a123492011-11-01 11:18:03 +1300841 * disable local echo
Marc Kleine-Budde749de6f2015-08-31 21:32:34 +0200842 * choose format C
843 * set max mailbox number
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200844 */
holt@sgi.com61e271e2011-08-16 17:32:20 +0000845 reg_mcr = flexcan_read(&regs->mcr);
Marc Kleine-Budded5a7b402013-10-04 10:52:36 +0200846 reg_mcr &= ~FLEXCAN_MCR_MAXMB(0xff);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200847 reg_mcr |= FLEXCAN_MCR_FRZ | FLEXCAN_MCR_FEN | FLEXCAN_MCR_HALT |
Marc Kleine-Budde749de6f2015-08-31 21:32:34 +0200848 FLEXCAN_MCR_SUPV | FLEXCAN_MCR_WRN_EN | FLEXCAN_MCR_SRX_DIS |
849 FLEXCAN_MCR_IDAM_C | FLEXCAN_MCR_MAXMB(FLEXCAN_TX_BUF_ID);
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100850 netdev_dbg(dev, "%s: writing mcr=0x%08x", __func__, reg_mcr);
holt@sgi.com61e271e2011-08-16 17:32:20 +0000851 flexcan_write(reg_mcr, &regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200852
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200853 /* CTRL
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200854 *
855 * disable timer sync feature
856 *
857 * disable auto busoff recovery
858 * transmit lowest buffer first
859 *
860 * enable tx and rx warning interrupt
861 * enable bus off interrupt
862 * (== FLEXCAN_CTRL_ERR_STATE)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200863 */
holt@sgi.com61e271e2011-08-16 17:32:20 +0000864 reg_ctrl = flexcan_read(&regs->ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200865 reg_ctrl &= ~FLEXCAN_CTRL_TSYN;
866 reg_ctrl |= FLEXCAN_CTRL_BOFF_REC | FLEXCAN_CTRL_LBUF |
Wolfgang Grandegger4f72e5f2012-09-28 03:17:15 +0000867 FLEXCAN_CTRL_ERR_STATE;
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200868
869 /* enable the "error interrupt" (FLEXCAN_CTRL_ERR_MSK),
Wolfgang Grandegger4f72e5f2012-09-28 03:17:15 +0000870 * on most Flexcan cores, too. Otherwise we don't get
871 * any error warning or passive interrupts.
872 */
873 if (priv->devtype_data->features & FLEXCAN_HAS_BROKEN_ERR_STATE ||
874 priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING)
875 reg_ctrl |= FLEXCAN_CTRL_ERR_MSK;
Alexander Steinbc03a542014-08-12 10:47:21 +0200876 else
877 reg_ctrl &= ~FLEXCAN_CTRL_ERR_MSK;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200878
879 /* save for later use */
880 priv->reg_ctrl_default = reg_ctrl;
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100881 netdev_dbg(dev, "%s: writing ctrl=0x%08x", __func__, reg_ctrl);
holt@sgi.com61e271e2011-08-16 17:32:20 +0000882 flexcan_write(reg_ctrl, &regs->ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200883
David Janderfc05b882014-08-27 11:58:05 +0200884 /* clear and invalidate all mailboxes first */
885 for (i = FLEXCAN_TX_BUF_ID; i < ARRAY_SIZE(regs->cantxfg); i++) {
886 flexcan_write(FLEXCAN_MB_CODE_RX_INACTIVE,
887 &regs->cantxfg[i].can_ctrl);
888 }
889
David Jander25e92442014-09-03 16:47:22 +0200890 /* Errata ERR005829: mark first TX mailbox as INACTIVE */
891 flexcan_write(FLEXCAN_MB_CODE_TX_INACTIVE,
892 &regs->cantxfg[FLEXCAN_TX_BUF_RESERVED].can_ctrl);
893
Marc Kleine-Buddec32fe4a2014-09-16 12:39:28 +0200894 /* mark TX mailbox as INACTIVE */
895 flexcan_write(FLEXCAN_MB_CODE_TX_INACTIVE,
Marc Kleine-Budded5a7b402013-10-04 10:52:36 +0200896 &regs->cantxfg[FLEXCAN_TX_BUF_ID].can_ctrl);
897
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200898 /* acceptance mask/acceptance code (accept everything) */
holt@sgi.com61e271e2011-08-16 17:32:20 +0000899 flexcan_write(0x0, &regs->rxgmask);
900 flexcan_write(0x0, &regs->rx14mask);
901 flexcan_write(0x0, &regs->rx15mask);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200902
Wolfgang Grandegger4f72e5f2012-09-28 03:17:15 +0000903 if (priv->devtype_data->features & FLEXCAN_HAS_V10_FEATURES)
Hui Wang30c1e672012-06-28 16:21:35 +0800904 flexcan_write(0x0, &regs->rxfgmask);
905
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200906 /* On Vybrid, disable memory error detection interrupts
Stefan Agnercdce8442014-07-15 14:56:21 +0200907 * and freeze mode.
908 * This also works around errata e5295 which generates
909 * false positive memory errors and put the device in
910 * freeze mode.
911 */
912 if (priv->devtype_data->features & FLEXCAN_HAS_MECR_FEATURES) {
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200913 /* Follow the protocol as described in "Detection
Stefan Agnercdce8442014-07-15 14:56:21 +0200914 * and Correction of Memory Errors" to write to
915 * MECR register
916 */
Marc Kleine-Budde6f75fce2014-09-23 11:03:01 +0200917 reg_ctrl2 = flexcan_read(&regs->ctrl2);
918 reg_ctrl2 |= FLEXCAN_CTRL2_ECRWRE;
919 flexcan_write(reg_ctrl2, &regs->ctrl2);
Stefan Agnercdce8442014-07-15 14:56:21 +0200920
921 reg_mecr = flexcan_read(&regs->mecr);
922 reg_mecr &= ~FLEXCAN_MECR_ECRWRDIS;
923 flexcan_write(reg_mecr, &regs->mecr);
924 reg_mecr &= ~(FLEXCAN_MECR_NCEFAFRZ | FLEXCAN_MECR_HANCEI_MSK |
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200925 FLEXCAN_MECR_FANCEI_MSK);
Stefan Agnercdce8442014-07-15 14:56:21 +0200926 flexcan_write(reg_mecr, &regs->mecr);
927 }
928
Marc Kleine-Buddef0036982014-02-28 17:18:27 +0100929 err = flexcan_transceiver_enable(priv);
930 if (err)
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100931 goto out_chip_disable;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200932
933 /* synchronize with the can bus */
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100934 err = flexcan_chip_unfreeze(priv);
935 if (err)
936 goto out_transceiver_disable;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200937
938 priv->can.state = CAN_STATE_ERROR_ACTIVE;
939
940 /* enable FIFO interrupts */
holt@sgi.com61e271e2011-08-16 17:32:20 +0000941 flexcan_write(FLEXCAN_IFLAG_DEFAULT, &regs->imask1);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200942
943 /* print chip status */
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100944 netdev_dbg(dev, "%s: reading mcr=0x%08x ctrl=0x%08x\n", __func__,
945 flexcan_read(&regs->mcr), flexcan_read(&regs->ctrl));
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200946
947 return 0;
948
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100949 out_transceiver_disable:
950 flexcan_transceiver_disable(priv);
951 out_chip_disable:
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200952 flexcan_chip_disable(priv);
953 return err;
954}
955
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200956/* flexcan_chip_stop
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200957 *
958 * this functions is entered with clocks enabled
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200959 */
960static void flexcan_chip_stop(struct net_device *dev)
961{
962 struct flexcan_priv *priv = netdev_priv(dev);
963 struct flexcan_regs __iomem *regs = priv->base;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200964
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100965 /* freeze + disable module */
966 flexcan_chip_freeze(priv);
967 flexcan_chip_disable(priv);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200968
Marc Kleine-Budde5be93bd2014-02-19 12:00:51 +0100969 /* Disable all interrupts */
970 flexcan_write(0, &regs->imask1);
971 flexcan_write(priv->reg_ctrl_default & ~FLEXCAN_CTRL_ERR_ALL,
972 &regs->ctrl);
973
Marc Kleine-Buddef0036982014-02-28 17:18:27 +0100974 flexcan_transceiver_disable(priv);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200975 priv->can.state = CAN_STATE_STOPPED;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200976}
977
978static int flexcan_open(struct net_device *dev)
979{
980 struct flexcan_priv *priv = netdev_priv(dev);
981 int err;
982
Fabio Estevamaa101812013-07-22 12:41:40 -0300983 err = clk_prepare_enable(priv->clk_ipg);
984 if (err)
985 return err;
986
987 err = clk_prepare_enable(priv->clk_per);
988 if (err)
989 goto out_disable_ipg;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200990
991 err = open_candev(dev);
992 if (err)
Fabio Estevamaa101812013-07-22 12:41:40 -0300993 goto out_disable_per;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200994
995 err = request_irq(dev->irq, flexcan_irq, IRQF_SHARED, dev->name, dev);
996 if (err)
997 goto out_close;
998
999 /* start chip and queuing */
1000 err = flexcan_chip_start(dev);
1001 if (err)
Marc Kleine-Budde7e9e1482014-02-28 14:52:01 +01001002 goto out_free_irq;
Fabio Baltieriadccadb2012-12-18 18:50:58 +01001003
1004 can_led_event(dev, CAN_LED_EVENT_OPEN);
1005
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001006 napi_enable(&priv->napi);
1007 netif_start_queue(dev);
1008
1009 return 0;
1010
Marc Kleine-Budde7e9e1482014-02-28 14:52:01 +01001011 out_free_irq:
1012 free_irq(dev->irq, dev);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001013 out_close:
1014 close_candev(dev);
Fabio Estevamaa101812013-07-22 12:41:40 -03001015 out_disable_per:
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001016 clk_disable_unprepare(priv->clk_per);
Fabio Estevamaa101812013-07-22 12:41:40 -03001017 out_disable_ipg:
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001018 clk_disable_unprepare(priv->clk_ipg);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001019
1020 return err;
1021}
1022
1023static int flexcan_close(struct net_device *dev)
1024{
1025 struct flexcan_priv *priv = netdev_priv(dev);
1026
1027 netif_stop_queue(dev);
1028 napi_disable(&priv->napi);
1029 flexcan_chip_stop(dev);
1030
1031 free_irq(dev->irq, dev);
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001032 clk_disable_unprepare(priv->clk_per);
1033 clk_disable_unprepare(priv->clk_ipg);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001034
1035 close_candev(dev);
1036
Fabio Baltieriadccadb2012-12-18 18:50:58 +01001037 can_led_event(dev, CAN_LED_EVENT_STOP);
1038
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001039 return 0;
1040}
1041
1042static int flexcan_set_mode(struct net_device *dev, enum can_mode mode)
1043{
1044 int err;
1045
1046 switch (mode) {
1047 case CAN_MODE_START:
1048 err = flexcan_chip_start(dev);
1049 if (err)
1050 return err;
1051
1052 netif_wake_queue(dev);
1053 break;
1054
1055 default:
1056 return -EOPNOTSUPP;
1057 }
1058
1059 return 0;
1060}
1061
1062static const struct net_device_ops flexcan_netdev_ops = {
1063 .ndo_open = flexcan_open,
1064 .ndo_stop = flexcan_close,
1065 .ndo_start_xmit = flexcan_start_xmit,
Oliver Hartkoppc971fa22014-03-07 09:23:41 +01001066 .ndo_change_mtu = can_change_mtu,
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001067};
1068
Bill Pemberton3c8ac0f2012-12-03 09:22:44 -05001069static int register_flexcandev(struct net_device *dev)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001070{
1071 struct flexcan_priv *priv = netdev_priv(dev);
1072 struct flexcan_regs __iomem *regs = priv->base;
1073 u32 reg, err;
1074
Fabio Estevamaa101812013-07-22 12:41:40 -03001075 err = clk_prepare_enable(priv->clk_ipg);
1076 if (err)
1077 return err;
1078
1079 err = clk_prepare_enable(priv->clk_per);
1080 if (err)
1081 goto out_disable_ipg;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001082
1083 /* select "bus clock", chip must be disabled */
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001084 err = flexcan_chip_disable(priv);
1085 if (err)
1086 goto out_disable_per;
holt@sgi.com61e271e2011-08-16 17:32:20 +00001087 reg = flexcan_read(&regs->ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001088 reg |= FLEXCAN_CTRL_CLK_SRC;
holt@sgi.com61e271e2011-08-16 17:32:20 +00001089 flexcan_write(reg, &regs->ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001090
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001091 err = flexcan_chip_enable(priv);
1092 if (err)
1093 goto out_chip_disable;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001094
1095 /* set freeze, halt and activate FIFO, restrict register access */
holt@sgi.com61e271e2011-08-16 17:32:20 +00001096 reg = flexcan_read(&regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001097 reg |= FLEXCAN_MCR_FRZ | FLEXCAN_MCR_HALT |
1098 FLEXCAN_MCR_FEN | FLEXCAN_MCR_SUPV;
holt@sgi.com61e271e2011-08-16 17:32:20 +00001099 flexcan_write(reg, &regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001100
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +02001101 /* Currently we only support newer versions of this core
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001102 * featuring a RX FIFO. Older cores found on some Coldfire
1103 * derivates are not yet supported.
1104 */
holt@sgi.com61e271e2011-08-16 17:32:20 +00001105 reg = flexcan_read(&regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001106 if (!(reg & FLEXCAN_MCR_FEN)) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +01001107 netdev_err(dev, "Could not enable RX FIFO, unsupported core\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001108 err = -ENODEV;
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001109 goto out_chip_disable;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001110 }
1111
1112 err = register_candev(dev);
1113
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001114 /* disable core and turn off clocks */
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001115 out_chip_disable:
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001116 flexcan_chip_disable(priv);
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001117 out_disable_per:
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001118 clk_disable_unprepare(priv->clk_per);
Fabio Estevamaa101812013-07-22 12:41:40 -03001119 out_disable_ipg:
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001120 clk_disable_unprepare(priv->clk_ipg);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001121
1122 return err;
1123}
1124
Bill Pemberton3c8ac0f2012-12-03 09:22:44 -05001125static void unregister_flexcandev(struct net_device *dev)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001126{
1127 unregister_candev(dev);
1128}
1129
Hui Wang30c1e672012-06-28 16:21:35 +08001130static const struct of_device_id flexcan_of_match[] = {
Hui Wang30c1e672012-06-28 16:21:35 +08001131 { .compatible = "fsl,imx6q-flexcan", .data = &fsl_imx6q_devtype_data, },
Marc Kleine-Buddee3587842013-10-03 23:51:55 +02001132 { .compatible = "fsl,imx28-flexcan", .data = &fsl_imx28_devtype_data, },
1133 { .compatible = "fsl,p1010-flexcan", .data = &fsl_p1010_devtype_data, },
Stefan Agnercdce8442014-07-15 14:56:21 +02001134 { .compatible = "fsl,vf610-flexcan", .data = &fsl_vf610_devtype_data, },
Hui Wang30c1e672012-06-28 16:21:35 +08001135 { /* sentinel */ },
1136};
Marc Kleine-Budde4358a9d2012-10-04 10:55:35 +02001137MODULE_DEVICE_TABLE(of, flexcan_of_match);
Hui Wang30c1e672012-06-28 16:21:35 +08001138
1139static const struct platform_device_id flexcan_id_table[] = {
1140 { .name = "flexcan", .driver_data = (kernel_ulong_t)&fsl_p1010_devtype_data, },
1141 { /* sentinel */ },
1142};
Marc Kleine-Budde4358a9d2012-10-04 10:55:35 +02001143MODULE_DEVICE_TABLE(platform, flexcan_id_table);
Hui Wang30c1e672012-06-28 16:21:35 +08001144
Bill Pemberton3c8ac0f2012-12-03 09:22:44 -05001145static int flexcan_probe(struct platform_device *pdev)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001146{
Hui Wang30c1e672012-06-28 16:21:35 +08001147 const struct of_device_id *of_id;
Marc Kleine-Buddedda0b3b2012-07-13 14:52:48 +02001148 const struct flexcan_devtype_data *devtype_data;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001149 struct net_device *dev;
1150 struct flexcan_priv *priv;
Andreas Werner555828e2015-03-22 17:35:52 +01001151 struct regulator *reg_xceiver;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001152 struct resource *mem;
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001153 struct clk *clk_ipg = NULL, *clk_per = NULL;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001154 void __iomem *base;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001155 int err, irq;
holt@sgi.com97efe9a2011-08-16 17:32:23 +00001156 u32 clock_freq = 0;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001157
Andreas Werner555828e2015-03-22 17:35:52 +01001158 reg_xceiver = devm_regulator_get(&pdev->dev, "xceiver");
1159 if (PTR_ERR(reg_xceiver) == -EPROBE_DEFER)
1160 return -EPROBE_DEFER;
1161 else if (IS_ERR(reg_xceiver))
1162 reg_xceiver = NULL;
1163
Hui Wangafc016d2012-06-28 16:21:34 +08001164 if (pdev->dev.of_node)
1165 of_property_read_u32(pdev->dev.of_node,
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +02001166 "clock-frequency", &clock_freq);
holt@sgi.com97efe9a2011-08-16 17:32:23 +00001167
1168 if (!clock_freq) {
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001169 clk_ipg = devm_clk_get(&pdev->dev, "ipg");
1170 if (IS_ERR(clk_ipg)) {
1171 dev_err(&pdev->dev, "no ipg clock defined\n");
Fabio Estevam933e4af2013-07-22 12:41:39 -03001172 return PTR_ERR(clk_ipg);
holt@sgi.com97efe9a2011-08-16 17:32:23 +00001173 }
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001174
1175 clk_per = devm_clk_get(&pdev->dev, "per");
1176 if (IS_ERR(clk_per)) {
1177 dev_err(&pdev->dev, "no per clock defined\n");
Fabio Estevam933e4af2013-07-22 12:41:39 -03001178 return PTR_ERR(clk_per);
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001179 }
Marc Kleine-Budde1a3e5172013-11-25 22:15:20 +01001180 clock_freq = clk_get_rate(clk_per);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001181 }
1182
1183 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1184 irq = platform_get_irq(pdev, 0);
Fabio Estevam933e4af2013-07-22 12:41:39 -03001185 if (irq <= 0)
1186 return -ENODEV;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001187
Fabio Estevam933e4af2013-07-22 12:41:39 -03001188 base = devm_ioremap_resource(&pdev->dev, mem);
1189 if (IS_ERR(base))
1190 return PTR_ERR(base);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001191
Hui Wang30c1e672012-06-28 16:21:35 +08001192 of_id = of_match_device(flexcan_of_match, &pdev->dev);
1193 if (of_id) {
1194 devtype_data = of_id->data;
Marc Kleine-Budded0873e62014-03-04 22:04:22 +01001195 } else if (platform_get_device_id(pdev)->driver_data) {
Hui Wang30c1e672012-06-28 16:21:35 +08001196 devtype_data = (struct flexcan_devtype_data *)
Marc Kleine-Budded0873e62014-03-04 22:04:22 +01001197 platform_get_device_id(pdev)->driver_data;
Hui Wang30c1e672012-06-28 16:21:35 +08001198 } else {
Fabio Estevam933e4af2013-07-22 12:41:39 -03001199 return -ENODEV;
Hui Wang30c1e672012-06-28 16:21:35 +08001200 }
1201
Fabio Estevam933e4af2013-07-22 12:41:39 -03001202 dev = alloc_candev(sizeof(struct flexcan_priv), 1);
1203 if (!dev)
1204 return -ENOMEM;
1205
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001206 dev->netdev_ops = &flexcan_netdev_ops;
1207 dev->irq = irq;
Reuben Dowle9a123492011-11-01 11:18:03 +13001208 dev->flags |= IFF_ECHO;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001209
1210 priv = netdev_priv(dev);
holt@sgi.com97efe9a2011-08-16 17:32:23 +00001211 priv->can.clock.freq = clock_freq;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001212 priv->can.bittiming_const = &flexcan_bittiming_const;
1213 priv->can.do_set_mode = flexcan_set_mode;
1214 priv->can.do_get_berr_counter = flexcan_get_berr_counter;
1215 priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK |
1216 CAN_CTRLMODE_LISTENONLY | CAN_CTRLMODE_3_SAMPLES |
1217 CAN_CTRLMODE_BERR_REPORTING;
1218 priv->base = base;
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001219 priv->clk_ipg = clk_ipg;
1220 priv->clk_per = clk_per;
Jingoo Han84ae6642013-09-10 17:41:30 +09001221 priv->pdata = dev_get_platdata(&pdev->dev);
Hui Wang30c1e672012-06-28 16:21:35 +08001222 priv->devtype_data = devtype_data;
Andreas Werner555828e2015-03-22 17:35:52 +01001223 priv->reg_xceiver = reg_xceiver;
Fabio Estevamb7c41142013-06-10 23:12:57 -03001224
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001225 netif_napi_add(dev, &priv->napi, flexcan_poll, FLEXCAN_NAPI_WEIGHT);
1226
Libo Chend75ea942013-08-21 18:15:08 +08001227 platform_set_drvdata(pdev, dev);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001228 SET_NETDEV_DEV(dev, &pdev->dev);
1229
1230 err = register_flexcandev(dev);
1231 if (err) {
1232 dev_err(&pdev->dev, "registering netdev failed\n");
1233 goto failed_register;
1234 }
1235
Fabio Baltieriadccadb2012-12-18 18:50:58 +01001236 devm_can_led_init(dev);
1237
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001238 dev_info(&pdev->dev, "device registered (reg_base=%p, irq=%d)\n",
1239 priv->base, dev->irq);
1240
1241 return 0;
1242
1243 failed_register:
1244 free_candev(dev);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001245 return err;
1246}
1247
Bill Pemberton3c8ac0f2012-12-03 09:22:44 -05001248static int flexcan_remove(struct platform_device *pdev)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001249{
1250 struct net_device *dev = platform_get_drvdata(pdev);
Marc Kleine-Budded96e43e2014-02-28 20:48:36 +01001251 struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001252
1253 unregister_flexcandev(dev);
Marc Kleine-Budded96e43e2014-02-28 20:48:36 +01001254 netif_napi_del(&priv->napi);
Marc Kleine-Budde9a275862010-10-21 05:07:58 +00001255 free_candev(dev);
1256
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001257 return 0;
1258}
1259
Marc Kleine-Budde08c6d352014-03-05 19:10:44 +01001260static int __maybe_unused flexcan_suspend(struct device *device)
Eric Bénard8b5e2182012-05-08 17:12:17 +02001261{
Fabio Estevam588e7a82013-05-20 15:43:43 -03001262 struct net_device *dev = dev_get_drvdata(device);
Eric Bénard8b5e2182012-05-08 17:12:17 +02001263 struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001264 int err;
Eric Bénard8b5e2182012-05-08 17:12:17 +02001265
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001266 err = flexcan_chip_disable(priv);
1267 if (err)
1268 return err;
Eric Bénard8b5e2182012-05-08 17:12:17 +02001269
1270 if (netif_running(dev)) {
1271 netif_stop_queue(dev);
1272 netif_device_detach(dev);
1273 }
1274 priv->can.state = CAN_STATE_SLEEPING;
1275
1276 return 0;
1277}
1278
Marc Kleine-Budde08c6d352014-03-05 19:10:44 +01001279static int __maybe_unused flexcan_resume(struct device *device)
Eric Bénard8b5e2182012-05-08 17:12:17 +02001280{
Fabio Estevam588e7a82013-05-20 15:43:43 -03001281 struct net_device *dev = dev_get_drvdata(device);
Eric Bénard8b5e2182012-05-08 17:12:17 +02001282 struct flexcan_priv *priv = netdev_priv(dev);
1283
1284 priv->can.state = CAN_STATE_ERROR_ACTIVE;
1285 if (netif_running(dev)) {
1286 netif_device_attach(dev);
1287 netif_start_queue(dev);
1288 }
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001289 return flexcan_chip_enable(priv);
Eric Bénard8b5e2182012-05-08 17:12:17 +02001290}
Fabio Estevam588e7a82013-05-20 15:43:43 -03001291
1292static SIMPLE_DEV_PM_OPS(flexcan_pm_ops, flexcan_suspend, flexcan_resume);
Eric Bénard8b5e2182012-05-08 17:12:17 +02001293
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001294static struct platform_driver flexcan_driver = {
holt@sgi.comc8aef4c2011-08-16 17:32:22 +00001295 .driver = {
1296 .name = DRV_NAME,
Fabio Estevam588e7a82013-05-20 15:43:43 -03001297 .pm = &flexcan_pm_ops,
holt@sgi.comc8aef4c2011-08-16 17:32:22 +00001298 .of_match_table = flexcan_of_match,
1299 },
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001300 .probe = flexcan_probe,
Bill Pemberton3c8ac0f2012-12-03 09:22:44 -05001301 .remove = flexcan_remove,
Hui Wang30c1e672012-06-28 16:21:35 +08001302 .id_table = flexcan_id_table,
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001303};
1304
Axel Lin871d3372011-11-27 15:42:31 +00001305module_platform_driver(flexcan_driver);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001306
1307MODULE_AUTHOR("Sascha Hauer <kernel@pengutronix.de>, "
1308 "Marc Kleine-Budde <kernel@pengutronix.de>");
1309MODULE_LICENSE("GPL v2");
1310MODULE_DESCRIPTION("CAN port driver for flexcan based chip");