blob: 41c0fc9f3b1465d9dbbde6b5c7798b1e40cf43ef [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 */
Marc Kleine-Buddef377bff2015-05-08 15:22:36 +0200190#define FLEXCAN_QUIRK_BROKEN_ERR_STATE BIT(1) /* [TR]WRN_INT not connected */
191#define FLEXCAN_QUIRK_DISABLE_RXFG BIT(2) /* Disable RX FIFO Global mask */
192#define FLEXCAN_QUIRK_DISABLE_MECR BIT(3) /* Disble 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 */
Marc Kleine-Budde1ba763d2015-08-25 10:39:19 +0200224 struct flexcan_mb mb[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 {
Marc Kleine-Buddef377bff2015-05-08 15:22:36 +0200247 u32 quirks; /* quirks needed for different IP cores */
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
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200254 struct flexcan_regs __iomem *regs;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200255 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 = {
Marc Kleine-Buddef377bff2015-05-08 15:22:36 +0200266 .quirks = FLEXCAN_QUIRK_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 = {
Marc Kleine-Buddef377bff2015-05-08 15:22:36 +0200272 .quirks = FLEXCAN_QUIRK_DISABLE_RXFG,
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 = {
Marc Kleine-Buddef377bff2015-05-08 15:22:36 +0200276 .quirks = FLEXCAN_QUIRK_DISABLE_RXFG | FLEXCAN_QUIRK_DISABLE_MECR,
Stefan Agnercdce8442014-07-15 14:56:21 +0200277};
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{
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200343 struct flexcan_regs __iomem *regs = priv->regs;
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{
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200362 struct flexcan_regs __iomem *regs = priv->regs;
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{
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200381 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100382 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{
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200400 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100401 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{
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200419 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Budde4b5b8222014-02-28 15:16:59 +0100420 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);
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200436 struct flexcan_regs __iomem *regs = priv->regs;
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-Budde89af8742015-05-08 09:32:58 +0200471 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200472 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]);
Marc Kleine-Budde1ba763d2015-08-25 10:39:19 +0200494 flexcan_write(data, &regs->mb[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]);
Marc Kleine-Budde1ba763d2015-08-25 10:39:19 +0200498 flexcan_write(data, &regs->mb[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
Marc Kleine-Budde1ba763d2015-08-25 10:39:19 +0200503 flexcan_write(can_id, &regs->mb[FLEXCAN_TX_BUF_ID].can_id);
504 flexcan_write(ctrl, &regs->mb[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,
Marc Kleine-Budde1ba763d2015-08-25 10:39:19 +0200510 &regs->mb[FLEXCAN_TX_BUF_RESERVED].can_ctrl);
David Jander25e92442014-09-03 16:47:22 +0200511 flexcan_write(FLEXCAN_MB_CODE_TX_INACTIVE,
Marc Kleine-Budde1ba763d2015-08-25 10:39:19 +0200512 &regs->mb[FLEXCAN_TX_BUF_RESERVED].can_ctrl);
David Jander25e92442014-09-03 16:47:22 +0200513
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;
Oliver Hartkoppffd461f2015-11-21 18:41:20 +0100538 cf->data[3] = CAN_ERR_PROT_LOC_ACK;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200539 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;
Oliver Hartkoppffd461f2015-11-21 18:41:20 +0100544 cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200545 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);
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200631 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Budde1ba763d2015-08-25 10:39:19 +0200632 struct flexcan_mb __iomem *mb = &regs->mb[0];
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200633 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);
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200681 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200682 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);
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200720 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200721 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,
Marc Kleine-Budde1ba763d2015-08-25 10:39:19 +0200764 &regs->mb[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;
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200776 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200777 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);
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200818 struct flexcan_regs __iomem *regs = priv->regs;
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 */
Marc Kleine-Buddef377bff2015-05-08 15:22:36 +0200873 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_BROKEN_ERR_STATE ||
Wolfgang Grandegger4f72e5f2012-09-28 03:17:15 +0000874 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;
Marc Kleine-Budde6fa7da22015-08-27 14:24:48 +0200881 /* leave interrupts disabled for now */
882 reg_ctrl &= ~FLEXCAN_CTRL_ERR_ALL;
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100883 netdev_dbg(dev, "%s: writing ctrl=0x%08x", __func__, reg_ctrl);
holt@sgi.com61e271e2011-08-16 17:32:20 +0000884 flexcan_write(reg_ctrl, &regs->ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200885
David Janderfc05b882014-08-27 11:58:05 +0200886 /* clear and invalidate all mailboxes first */
Marc Kleine-Budde1ba763d2015-08-25 10:39:19 +0200887 for (i = FLEXCAN_TX_BUF_ID; i < ARRAY_SIZE(regs->mb); i++) {
David Janderfc05b882014-08-27 11:58:05 +0200888 flexcan_write(FLEXCAN_MB_CODE_RX_INACTIVE,
Marc Kleine-Budde1ba763d2015-08-25 10:39:19 +0200889 &regs->mb[i].can_ctrl);
David Janderfc05b882014-08-27 11:58:05 +0200890 }
891
David Jander25e92442014-09-03 16:47:22 +0200892 /* Errata ERR005829: mark first TX mailbox as INACTIVE */
893 flexcan_write(FLEXCAN_MB_CODE_TX_INACTIVE,
Marc Kleine-Budde1ba763d2015-08-25 10:39:19 +0200894 &regs->mb[FLEXCAN_TX_BUF_RESERVED].can_ctrl);
David Jander25e92442014-09-03 16:47:22 +0200895
Marc Kleine-Buddec32fe4a2014-09-16 12:39:28 +0200896 /* mark TX mailbox as INACTIVE */
897 flexcan_write(FLEXCAN_MB_CODE_TX_INACTIVE,
Marc Kleine-Budde1ba763d2015-08-25 10:39:19 +0200898 &regs->mb[FLEXCAN_TX_BUF_ID].can_ctrl);
Marc Kleine-Budded5a7b402013-10-04 10:52:36 +0200899
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200900 /* acceptance mask/acceptance code (accept everything) */
holt@sgi.com61e271e2011-08-16 17:32:20 +0000901 flexcan_write(0x0, &regs->rxgmask);
902 flexcan_write(0x0, &regs->rx14mask);
903 flexcan_write(0x0, &regs->rx15mask);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200904
Marc Kleine-Buddef377bff2015-05-08 15:22:36 +0200905 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_DISABLE_RXFG)
Hui Wang30c1e672012-06-28 16:21:35 +0800906 flexcan_write(0x0, &regs->rxfgmask);
907
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200908 /* On Vybrid, disable memory error detection interrupts
Stefan Agnercdce8442014-07-15 14:56:21 +0200909 * and freeze mode.
910 * This also works around errata e5295 which generates
911 * false positive memory errors and put the device in
912 * freeze mode.
913 */
Marc Kleine-Buddef377bff2015-05-08 15:22:36 +0200914 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_DISABLE_MECR) {
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200915 /* Follow the protocol as described in "Detection
Stefan Agnercdce8442014-07-15 14:56:21 +0200916 * and Correction of Memory Errors" to write to
917 * MECR register
918 */
Marc Kleine-Budde6f75fce2014-09-23 11:03:01 +0200919 reg_ctrl2 = flexcan_read(&regs->ctrl2);
920 reg_ctrl2 |= FLEXCAN_CTRL2_ECRWRE;
921 flexcan_write(reg_ctrl2, &regs->ctrl2);
Stefan Agnercdce8442014-07-15 14:56:21 +0200922
923 reg_mecr = flexcan_read(&regs->mecr);
924 reg_mecr &= ~FLEXCAN_MECR_ECRWRDIS;
925 flexcan_write(reg_mecr, &regs->mecr);
926 reg_mecr &= ~(FLEXCAN_MECR_NCEFAFRZ | FLEXCAN_MECR_HANCEI_MSK |
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200927 FLEXCAN_MECR_FANCEI_MSK);
Stefan Agnercdce8442014-07-15 14:56:21 +0200928 flexcan_write(reg_mecr, &regs->mecr);
929 }
930
Marc Kleine-Buddef0036982014-02-28 17:18:27 +0100931 err = flexcan_transceiver_enable(priv);
932 if (err)
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100933 goto out_chip_disable;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200934
935 /* synchronize with the can bus */
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100936 err = flexcan_chip_unfreeze(priv);
937 if (err)
938 goto out_transceiver_disable;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200939
940 priv->can.state = CAN_STATE_ERROR_ACTIVE;
941
Marc Kleine-Budde6fa7da22015-08-27 14:24:48 +0200942 /* enable interrupts atomically */
943 disable_irq(dev->irq);
944 flexcan_write(priv->reg_ctrl_default, &regs->ctrl);
holt@sgi.com61e271e2011-08-16 17:32:20 +0000945 flexcan_write(FLEXCAN_IFLAG_DEFAULT, &regs->imask1);
Marc Kleine-Budde6fa7da22015-08-27 14:24:48 +0200946 enable_irq(dev->irq);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200947
948 /* print chip status */
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100949 netdev_dbg(dev, "%s: reading mcr=0x%08x ctrl=0x%08x\n", __func__,
950 flexcan_read(&regs->mcr), flexcan_read(&regs->ctrl));
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200951
952 return 0;
953
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100954 out_transceiver_disable:
955 flexcan_transceiver_disable(priv);
956 out_chip_disable:
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200957 flexcan_chip_disable(priv);
958 return err;
959}
960
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200961/* flexcan_chip_stop
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200962 *
963 * this functions is entered with clocks enabled
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200964 */
965static void flexcan_chip_stop(struct net_device *dev)
966{
967 struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200968 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200969
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100970 /* freeze + disable module */
971 flexcan_chip_freeze(priv);
972 flexcan_chip_disable(priv);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200973
Marc Kleine-Budde5be93bd2014-02-19 12:00:51 +0100974 /* Disable all interrupts */
975 flexcan_write(0, &regs->imask1);
976 flexcan_write(priv->reg_ctrl_default & ~FLEXCAN_CTRL_ERR_ALL,
977 &regs->ctrl);
978
Marc Kleine-Buddef0036982014-02-28 17:18:27 +0100979 flexcan_transceiver_disable(priv);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200980 priv->can.state = CAN_STATE_STOPPED;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200981}
982
983static int flexcan_open(struct net_device *dev)
984{
985 struct flexcan_priv *priv = netdev_priv(dev);
986 int err;
987
Fabio Estevamaa101812013-07-22 12:41:40 -0300988 err = clk_prepare_enable(priv->clk_ipg);
989 if (err)
990 return err;
991
992 err = clk_prepare_enable(priv->clk_per);
993 if (err)
994 goto out_disable_ipg;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200995
996 err = open_candev(dev);
997 if (err)
Fabio Estevamaa101812013-07-22 12:41:40 -0300998 goto out_disable_per;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200999
1000 err = request_irq(dev->irq, flexcan_irq, IRQF_SHARED, dev->name, dev);
1001 if (err)
1002 goto out_close;
1003
1004 /* start chip and queuing */
1005 err = flexcan_chip_start(dev);
1006 if (err)
Marc Kleine-Budde7e9e1482014-02-28 14:52:01 +01001007 goto out_free_irq;
Fabio Baltieriadccadb2012-12-18 18:50:58 +01001008
1009 can_led_event(dev, CAN_LED_EVENT_OPEN);
1010
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001011 napi_enable(&priv->napi);
1012 netif_start_queue(dev);
1013
1014 return 0;
1015
Marc Kleine-Budde7e9e1482014-02-28 14:52:01 +01001016 out_free_irq:
1017 free_irq(dev->irq, dev);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001018 out_close:
1019 close_candev(dev);
Fabio Estevamaa101812013-07-22 12:41:40 -03001020 out_disable_per:
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001021 clk_disable_unprepare(priv->clk_per);
Fabio Estevamaa101812013-07-22 12:41:40 -03001022 out_disable_ipg:
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001023 clk_disable_unprepare(priv->clk_ipg);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001024
1025 return err;
1026}
1027
1028static int flexcan_close(struct net_device *dev)
1029{
1030 struct flexcan_priv *priv = netdev_priv(dev);
1031
1032 netif_stop_queue(dev);
1033 napi_disable(&priv->napi);
1034 flexcan_chip_stop(dev);
1035
1036 free_irq(dev->irq, dev);
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001037 clk_disable_unprepare(priv->clk_per);
1038 clk_disable_unprepare(priv->clk_ipg);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001039
1040 close_candev(dev);
1041
Fabio Baltieriadccadb2012-12-18 18:50:58 +01001042 can_led_event(dev, CAN_LED_EVENT_STOP);
1043
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001044 return 0;
1045}
1046
1047static int flexcan_set_mode(struct net_device *dev, enum can_mode mode)
1048{
1049 int err;
1050
1051 switch (mode) {
1052 case CAN_MODE_START:
1053 err = flexcan_chip_start(dev);
1054 if (err)
1055 return err;
1056
1057 netif_wake_queue(dev);
1058 break;
1059
1060 default:
1061 return -EOPNOTSUPP;
1062 }
1063
1064 return 0;
1065}
1066
1067static const struct net_device_ops flexcan_netdev_ops = {
1068 .ndo_open = flexcan_open,
1069 .ndo_stop = flexcan_close,
1070 .ndo_start_xmit = flexcan_start_xmit,
Oliver Hartkoppc971fa22014-03-07 09:23:41 +01001071 .ndo_change_mtu = can_change_mtu,
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001072};
1073
Bill Pemberton3c8ac0f2012-12-03 09:22:44 -05001074static int register_flexcandev(struct net_device *dev)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001075{
1076 struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Budde89af8742015-05-08 09:32:58 +02001077 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001078 u32 reg, err;
1079
Fabio Estevamaa101812013-07-22 12:41:40 -03001080 err = clk_prepare_enable(priv->clk_ipg);
1081 if (err)
1082 return err;
1083
1084 err = clk_prepare_enable(priv->clk_per);
1085 if (err)
1086 goto out_disable_ipg;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001087
1088 /* select "bus clock", chip must be disabled */
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001089 err = flexcan_chip_disable(priv);
1090 if (err)
1091 goto out_disable_per;
holt@sgi.com61e271e2011-08-16 17:32:20 +00001092 reg = flexcan_read(&regs->ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001093 reg |= FLEXCAN_CTRL_CLK_SRC;
holt@sgi.com61e271e2011-08-16 17:32:20 +00001094 flexcan_write(reg, &regs->ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001095
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001096 err = flexcan_chip_enable(priv);
1097 if (err)
1098 goto out_chip_disable;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001099
1100 /* set freeze, halt and activate FIFO, restrict register access */
holt@sgi.com61e271e2011-08-16 17:32:20 +00001101 reg = flexcan_read(&regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001102 reg |= FLEXCAN_MCR_FRZ | FLEXCAN_MCR_HALT |
1103 FLEXCAN_MCR_FEN | FLEXCAN_MCR_SUPV;
holt@sgi.com61e271e2011-08-16 17:32:20 +00001104 flexcan_write(reg, &regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001105
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +02001106 /* Currently we only support newer versions of this core
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001107 * featuring a RX FIFO. Older cores found on some Coldfire
1108 * derivates are not yet supported.
1109 */
holt@sgi.com61e271e2011-08-16 17:32:20 +00001110 reg = flexcan_read(&regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001111 if (!(reg & FLEXCAN_MCR_FEN)) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +01001112 netdev_err(dev, "Could not enable RX FIFO, unsupported core\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001113 err = -ENODEV;
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001114 goto out_chip_disable;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001115 }
1116
1117 err = register_candev(dev);
1118
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001119 /* disable core and turn off clocks */
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001120 out_chip_disable:
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001121 flexcan_chip_disable(priv);
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001122 out_disable_per:
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001123 clk_disable_unprepare(priv->clk_per);
Fabio Estevamaa101812013-07-22 12:41:40 -03001124 out_disable_ipg:
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001125 clk_disable_unprepare(priv->clk_ipg);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001126
1127 return err;
1128}
1129
Bill Pemberton3c8ac0f2012-12-03 09:22:44 -05001130static void unregister_flexcandev(struct net_device *dev)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001131{
1132 unregister_candev(dev);
1133}
1134
Hui Wang30c1e672012-06-28 16:21:35 +08001135static const struct of_device_id flexcan_of_match[] = {
Hui Wang30c1e672012-06-28 16:21:35 +08001136 { .compatible = "fsl,imx6q-flexcan", .data = &fsl_imx6q_devtype_data, },
Marc Kleine-Buddee3587842013-10-03 23:51:55 +02001137 { .compatible = "fsl,imx28-flexcan", .data = &fsl_imx28_devtype_data, },
1138 { .compatible = "fsl,p1010-flexcan", .data = &fsl_p1010_devtype_data, },
Stefan Agnercdce8442014-07-15 14:56:21 +02001139 { .compatible = "fsl,vf610-flexcan", .data = &fsl_vf610_devtype_data, },
Hui Wang30c1e672012-06-28 16:21:35 +08001140 { /* sentinel */ },
1141};
Marc Kleine-Budde4358a9d2012-10-04 10:55:35 +02001142MODULE_DEVICE_TABLE(of, flexcan_of_match);
Hui Wang30c1e672012-06-28 16:21:35 +08001143
1144static const struct platform_device_id flexcan_id_table[] = {
1145 { .name = "flexcan", .driver_data = (kernel_ulong_t)&fsl_p1010_devtype_data, },
1146 { /* sentinel */ },
1147};
Marc Kleine-Budde4358a9d2012-10-04 10:55:35 +02001148MODULE_DEVICE_TABLE(platform, flexcan_id_table);
Hui Wang30c1e672012-06-28 16:21:35 +08001149
Bill Pemberton3c8ac0f2012-12-03 09:22:44 -05001150static int flexcan_probe(struct platform_device *pdev)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001151{
Hui Wang30c1e672012-06-28 16:21:35 +08001152 const struct of_device_id *of_id;
Marc Kleine-Buddedda0b3b2012-07-13 14:52:48 +02001153 const struct flexcan_devtype_data *devtype_data;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001154 struct net_device *dev;
1155 struct flexcan_priv *priv;
Andreas Werner555828e2015-03-22 17:35:52 +01001156 struct regulator *reg_xceiver;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001157 struct resource *mem;
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001158 struct clk *clk_ipg = NULL, *clk_per = NULL;
Marc Kleine-Budde89af8742015-05-08 09:32:58 +02001159 struct flexcan_regs __iomem *regs;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001160 int err, irq;
holt@sgi.com97efe9a2011-08-16 17:32:23 +00001161 u32 clock_freq = 0;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001162
Andreas Werner555828e2015-03-22 17:35:52 +01001163 reg_xceiver = devm_regulator_get(&pdev->dev, "xceiver");
1164 if (PTR_ERR(reg_xceiver) == -EPROBE_DEFER)
1165 return -EPROBE_DEFER;
1166 else if (IS_ERR(reg_xceiver))
1167 reg_xceiver = NULL;
1168
Hui Wangafc016d2012-06-28 16:21:34 +08001169 if (pdev->dev.of_node)
1170 of_property_read_u32(pdev->dev.of_node,
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +02001171 "clock-frequency", &clock_freq);
holt@sgi.com97efe9a2011-08-16 17:32:23 +00001172
1173 if (!clock_freq) {
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001174 clk_ipg = devm_clk_get(&pdev->dev, "ipg");
1175 if (IS_ERR(clk_ipg)) {
1176 dev_err(&pdev->dev, "no ipg clock defined\n");
Fabio Estevam933e4af2013-07-22 12:41:39 -03001177 return PTR_ERR(clk_ipg);
holt@sgi.com97efe9a2011-08-16 17:32:23 +00001178 }
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001179
1180 clk_per = devm_clk_get(&pdev->dev, "per");
1181 if (IS_ERR(clk_per)) {
1182 dev_err(&pdev->dev, "no per clock defined\n");
Fabio Estevam933e4af2013-07-22 12:41:39 -03001183 return PTR_ERR(clk_per);
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001184 }
Marc Kleine-Budde1a3e5172013-11-25 22:15:20 +01001185 clock_freq = clk_get_rate(clk_per);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001186 }
1187
1188 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1189 irq = platform_get_irq(pdev, 0);
Fabio Estevam933e4af2013-07-22 12:41:39 -03001190 if (irq <= 0)
1191 return -ENODEV;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001192
Marc Kleine-Budde89af8742015-05-08 09:32:58 +02001193 regs = devm_ioremap_resource(&pdev->dev, mem);
1194 if (IS_ERR(regs))
1195 return PTR_ERR(regs);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001196
Hui Wang30c1e672012-06-28 16:21:35 +08001197 of_id = of_match_device(flexcan_of_match, &pdev->dev);
1198 if (of_id) {
1199 devtype_data = of_id->data;
Marc Kleine-Budded0873e62014-03-04 22:04:22 +01001200 } else if (platform_get_device_id(pdev)->driver_data) {
Hui Wang30c1e672012-06-28 16:21:35 +08001201 devtype_data = (struct flexcan_devtype_data *)
Marc Kleine-Budded0873e62014-03-04 22:04:22 +01001202 platform_get_device_id(pdev)->driver_data;
Hui Wang30c1e672012-06-28 16:21:35 +08001203 } else {
Fabio Estevam933e4af2013-07-22 12:41:39 -03001204 return -ENODEV;
Hui Wang30c1e672012-06-28 16:21:35 +08001205 }
1206
Fabio Estevam933e4af2013-07-22 12:41:39 -03001207 dev = alloc_candev(sizeof(struct flexcan_priv), 1);
1208 if (!dev)
1209 return -ENOMEM;
1210
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001211 dev->netdev_ops = &flexcan_netdev_ops;
1212 dev->irq = irq;
Reuben Dowle9a123492011-11-01 11:18:03 +13001213 dev->flags |= IFF_ECHO;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001214
1215 priv = netdev_priv(dev);
holt@sgi.com97efe9a2011-08-16 17:32:23 +00001216 priv->can.clock.freq = clock_freq;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001217 priv->can.bittiming_const = &flexcan_bittiming_const;
1218 priv->can.do_set_mode = flexcan_set_mode;
1219 priv->can.do_get_berr_counter = flexcan_get_berr_counter;
1220 priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK |
1221 CAN_CTRLMODE_LISTENONLY | CAN_CTRLMODE_3_SAMPLES |
1222 CAN_CTRLMODE_BERR_REPORTING;
Marc Kleine-Budde89af8742015-05-08 09:32:58 +02001223 priv->regs = regs;
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001224 priv->clk_ipg = clk_ipg;
1225 priv->clk_per = clk_per;
Jingoo Han84ae6642013-09-10 17:41:30 +09001226 priv->pdata = dev_get_platdata(&pdev->dev);
Hui Wang30c1e672012-06-28 16:21:35 +08001227 priv->devtype_data = devtype_data;
Andreas Werner555828e2015-03-22 17:35:52 +01001228 priv->reg_xceiver = reg_xceiver;
Fabio Estevamb7c41142013-06-10 23:12:57 -03001229
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001230 netif_napi_add(dev, &priv->napi, flexcan_poll, FLEXCAN_NAPI_WEIGHT);
1231
Libo Chend75ea942013-08-21 18:15:08 +08001232 platform_set_drvdata(pdev, dev);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001233 SET_NETDEV_DEV(dev, &pdev->dev);
1234
1235 err = register_flexcandev(dev);
1236 if (err) {
1237 dev_err(&pdev->dev, "registering netdev failed\n");
1238 goto failed_register;
1239 }
1240
Fabio Baltieriadccadb2012-12-18 18:50:58 +01001241 devm_can_led_init(dev);
1242
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001243 dev_info(&pdev->dev, "device registered (reg_base=%p, irq=%d)\n",
Marc Kleine-Budde89af8742015-05-08 09:32:58 +02001244 priv->regs, dev->irq);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001245
1246 return 0;
1247
1248 failed_register:
1249 free_candev(dev);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001250 return err;
1251}
1252
Bill Pemberton3c8ac0f2012-12-03 09:22:44 -05001253static int flexcan_remove(struct platform_device *pdev)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001254{
1255 struct net_device *dev = platform_get_drvdata(pdev);
Marc Kleine-Budded96e43e2014-02-28 20:48:36 +01001256 struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001257
1258 unregister_flexcandev(dev);
Marc Kleine-Budded96e43e2014-02-28 20:48:36 +01001259 netif_napi_del(&priv->napi);
Marc Kleine-Budde9a275862010-10-21 05:07:58 +00001260 free_candev(dev);
1261
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001262 return 0;
1263}
1264
Marc Kleine-Budde08c6d352014-03-05 19:10:44 +01001265static int __maybe_unused flexcan_suspend(struct device *device)
Eric Bénard8b5e2182012-05-08 17:12:17 +02001266{
Fabio Estevam588e7a82013-05-20 15:43:43 -03001267 struct net_device *dev = dev_get_drvdata(device);
Eric Bénard8b5e2182012-05-08 17:12:17 +02001268 struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001269 int err;
Eric Bénard8b5e2182012-05-08 17:12:17 +02001270
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001271 err = flexcan_chip_disable(priv);
1272 if (err)
1273 return err;
Eric Bénard8b5e2182012-05-08 17:12:17 +02001274
1275 if (netif_running(dev)) {
1276 netif_stop_queue(dev);
1277 netif_device_detach(dev);
1278 }
1279 priv->can.state = CAN_STATE_SLEEPING;
1280
1281 return 0;
1282}
1283
Marc Kleine-Budde08c6d352014-03-05 19:10:44 +01001284static int __maybe_unused flexcan_resume(struct device *device)
Eric Bénard8b5e2182012-05-08 17:12:17 +02001285{
Fabio Estevam588e7a82013-05-20 15:43:43 -03001286 struct net_device *dev = dev_get_drvdata(device);
Eric Bénard8b5e2182012-05-08 17:12:17 +02001287 struct flexcan_priv *priv = netdev_priv(dev);
1288
1289 priv->can.state = CAN_STATE_ERROR_ACTIVE;
1290 if (netif_running(dev)) {
1291 netif_device_attach(dev);
1292 netif_start_queue(dev);
1293 }
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001294 return flexcan_chip_enable(priv);
Eric Bénard8b5e2182012-05-08 17:12:17 +02001295}
Fabio Estevam588e7a82013-05-20 15:43:43 -03001296
1297static SIMPLE_DEV_PM_OPS(flexcan_pm_ops, flexcan_suspend, flexcan_resume);
Eric Bénard8b5e2182012-05-08 17:12:17 +02001298
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001299static struct platform_driver flexcan_driver = {
holt@sgi.comc8aef4c2011-08-16 17:32:22 +00001300 .driver = {
1301 .name = DRV_NAME,
Fabio Estevam588e7a82013-05-20 15:43:43 -03001302 .pm = &flexcan_pm_ops,
holt@sgi.comc8aef4c2011-08-16 17:32:22 +00001303 .of_match_table = flexcan_of_match,
1304 },
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001305 .probe = flexcan_probe,
Bill Pemberton3c8ac0f2012-12-03 09:22:44 -05001306 .remove = flexcan_remove,
Hui Wang30c1e672012-06-28 16:21:35 +08001307 .id_table = flexcan_id_table,
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001308};
1309
Axel Lin871d3372011-11-27 15:42:31 +00001310module_platform_driver(flexcan_driver);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001311
1312MODULE_AUTHOR("Sascha Hauer <kernel@pengutronix.de>, "
1313 "Marc Kleine-Budde <kernel@pengutronix.de>");
1314MODULE_LICENSE("GPL v2");
1315MODULE_DESCRIPTION("CAN port driver for flexcan based chip");