blob: 74909e599f31af451da6fc5f60ad9ff010cd7f5e [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)
Marc Kleine-Budde62d10862015-08-27 16:01:27 +020058#define FLEXCAN_MCR_IRMQ BIT(16)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +020059#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-Budde62d10862015-08-27 16:01:27 +0200216 union { /* 0x34 */
217 u32 gfwr_mx28; /* MX28, MX53 */
218 u32 ctrl2; /* MX6, VF610 */
219 };
Hui Wang30c1e672012-06-28 16:21:35 +0800220 u32 esr2; /* 0x38 */
221 u32 imeur; /* 0x3c */
222 u32 lrfr; /* 0x40 */
223 u32 crcr; /* 0x44 */
224 u32 rxfgmask; /* 0x48 */
225 u32 rxfir; /* 0x4c */
Stefan Agnercdce8442014-07-15 14:56:21 +0200226 u32 _reserved3[12]; /* 0x50 */
Marc Kleine-Budde1ba763d2015-08-25 10:39:19 +0200227 struct flexcan_mb mb[64]; /* 0x80 */
Marc Kleine-Budde66a6ef02014-09-17 12:50:48 +0200228 /* FIFO-mode:
229 * MB
230 * 0x080...0x08f 0 RX message buffer
231 * 0x090...0x0df 1-5 reserverd
232 * 0x0e0...0x0ff 6-7 8 entry ID table
233 * (mx25, mx28, mx35, mx53)
234 * 0x0e0...0x2df 6-7..37 8..128 entry ID table
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200235 * size conf'ed via ctrl2::RFFN
Marc Kleine-Budde66a6ef02014-09-17 12:50:48 +0200236 * (mx6, vf610)
237 */
Marc Kleine-Budde62d10862015-08-27 16:01:27 +0200238 u32 _reserved4[256]; /* 0x480 */
239 u32 rximr[64]; /* 0x880 */
240 u32 _reserved5[24]; /* 0x980 */
241 u32 gfwr_mx6; /* 0x9e0 - MX6 */
242 u32 _reserved6[63]; /* 0x9e4 */
Stefan Agnercdce8442014-07-15 14:56:21 +0200243 u32 mecr; /* 0xae0 */
244 u32 erriar; /* 0xae4 */
245 u32 erridpr; /* 0xae8 */
246 u32 errippr; /* 0xaec */
247 u32 rerrar; /* 0xaf0 */
248 u32 rerrdr; /* 0xaf4 */
249 u32 rerrsynr; /* 0xaf8 */
250 u32 errsr; /* 0xafc */
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200251};
252
Hui Wang30c1e672012-06-28 16:21:35 +0800253struct flexcan_devtype_data {
Marc Kleine-Buddef377bff2015-05-08 15:22:36 +0200254 u32 quirks; /* quirks needed for different IP cores */
Hui Wang30c1e672012-06-28 16:21:35 +0800255};
256
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200257struct flexcan_priv {
258 struct can_priv can;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200259 struct napi_struct napi;
260
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200261 struct flexcan_regs __iomem *regs;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200262 u32 reg_esr;
263 u32 reg_ctrl_default;
264
Steffen Trumtrar3d42a372012-07-17 16:14:34 +0200265 struct clk *clk_ipg;
266 struct clk *clk_per;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200267 struct flexcan_platform_data *pdata;
Marc Kleine-Buddedda0b3b2012-07-13 14:52:48 +0200268 const struct flexcan_devtype_data *devtype_data;
Fabio Estevamb7c41142013-06-10 23:12:57 -0300269 struct regulator *reg_xceiver;
Hui Wang30c1e672012-06-28 16:21:35 +0800270};
271
272static struct flexcan_devtype_data fsl_p1010_devtype_data = {
Marc Kleine-Buddef377bff2015-05-08 15:22:36 +0200273 .quirks = FLEXCAN_QUIRK_BROKEN_ERR_STATE,
Hui Wang30c1e672012-06-28 16:21:35 +0800274};
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200275
Wolfgang Grandegger4f72e5f2012-09-28 03:17:15 +0000276static struct flexcan_devtype_data fsl_imx28_devtype_data;
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200277
Hui Wang30c1e672012-06-28 16:21:35 +0800278static struct flexcan_devtype_data fsl_imx6q_devtype_data = {
Marc Kleine-Buddef377bff2015-05-08 15:22:36 +0200279 .quirks = FLEXCAN_QUIRK_DISABLE_RXFG,
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200280};
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200281
Stefan Agnercdce8442014-07-15 14:56:21 +0200282static struct flexcan_devtype_data fsl_vf610_devtype_data = {
Marc Kleine-Buddef377bff2015-05-08 15:22:36 +0200283 .quirks = FLEXCAN_QUIRK_DISABLE_RXFG | FLEXCAN_QUIRK_DISABLE_MECR,
Stefan Agnercdce8442014-07-15 14:56:21 +0200284};
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200285
Marc Kleine-Budde194b9a42012-07-16 12:58:31 +0200286static const struct can_bittiming_const flexcan_bittiming_const = {
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200287 .name = DRV_NAME,
288 .tseg1_min = 4,
289 .tseg1_max = 16,
290 .tseg2_min = 2,
291 .tseg2_max = 8,
292 .sjw_max = 4,
293 .brp_min = 1,
294 .brp_max = 256,
295 .brp_inc = 1,
296};
297
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200298/* Abstract off the read/write for arm versus ppc. This
Arnd Bergmann0e4b9492014-01-14 11:44:09 +0100299 * assumes that PPC uses big-endian registers and everything
300 * else uses little-endian registers, independent of CPU
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200301 * endianness.
holt@sgi.com61e271e2011-08-16 17:32:20 +0000302 */
Arnd Bergmann0e4b9492014-01-14 11:44:09 +0100303#if defined(CONFIG_PPC)
holt@sgi.com61e271e2011-08-16 17:32:20 +0000304static inline u32 flexcan_read(void __iomem *addr)
305{
306 return in_be32(addr);
307}
308
309static inline void flexcan_write(u32 val, void __iomem *addr)
310{
311 out_be32(addr, val);
312}
313#else
314static inline u32 flexcan_read(void __iomem *addr)
315{
316 return readl(addr);
317}
318
319static inline void flexcan_write(u32 val, void __iomem *addr)
320{
321 writel(val, addr);
322}
323#endif
324
Marc Kleine-Buddef0036982014-02-28 17:18:27 +0100325static inline int flexcan_transceiver_enable(const struct flexcan_priv *priv)
326{
327 if (!priv->reg_xceiver)
328 return 0;
329
330 return regulator_enable(priv->reg_xceiver);
331}
332
333static inline int flexcan_transceiver_disable(const struct flexcan_priv *priv)
334{
335 if (!priv->reg_xceiver)
336 return 0;
337
338 return regulator_disable(priv->reg_xceiver);
339}
340
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200341static inline int flexcan_has_and_handle_berr(const struct flexcan_priv *priv,
342 u32 reg_esr)
343{
344 return (priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING) &&
345 (reg_esr & FLEXCAN_ESR_ERR_BUS);
346}
347
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100348static int flexcan_chip_enable(struct flexcan_priv *priv)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200349{
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200350 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100351 unsigned int timeout = FLEXCAN_TIMEOUT_US / 10;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200352 u32 reg;
353
holt@sgi.com61e271e2011-08-16 17:32:20 +0000354 reg = flexcan_read(&regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200355 reg &= ~FLEXCAN_MCR_MDIS;
holt@sgi.com61e271e2011-08-16 17:32:20 +0000356 flexcan_write(reg, &regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200357
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100358 while (timeout-- && (flexcan_read(&regs->mcr) & FLEXCAN_MCR_LPM_ACK))
David Jander8badd652014-08-27 12:02:16 +0200359 udelay(10);
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100360
361 if (flexcan_read(&regs->mcr) & FLEXCAN_MCR_LPM_ACK)
362 return -ETIMEDOUT;
363
364 return 0;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200365}
366
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100367static int flexcan_chip_disable(struct flexcan_priv *priv)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200368{
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200369 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100370 unsigned int timeout = FLEXCAN_TIMEOUT_US / 10;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200371 u32 reg;
372
holt@sgi.com61e271e2011-08-16 17:32:20 +0000373 reg = flexcan_read(&regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200374 reg |= FLEXCAN_MCR_MDIS;
holt@sgi.com61e271e2011-08-16 17:32:20 +0000375 flexcan_write(reg, &regs->mcr);
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100376
377 while (timeout-- && !(flexcan_read(&regs->mcr) & FLEXCAN_MCR_LPM_ACK))
David Jander8badd652014-08-27 12:02:16 +0200378 udelay(10);
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100379
380 if (!(flexcan_read(&regs->mcr) & FLEXCAN_MCR_LPM_ACK))
381 return -ETIMEDOUT;
382
383 return 0;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200384}
385
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100386static int flexcan_chip_freeze(struct flexcan_priv *priv)
387{
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200388 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100389 unsigned int timeout = 1000 * 1000 * 10 / priv->can.bittiming.bitrate;
390 u32 reg;
391
392 reg = flexcan_read(&regs->mcr);
393 reg |= FLEXCAN_MCR_HALT;
394 flexcan_write(reg, &regs->mcr);
395
396 while (timeout-- && !(flexcan_read(&regs->mcr) & FLEXCAN_MCR_FRZ_ACK))
David Jander8badd652014-08-27 12:02:16 +0200397 udelay(100);
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100398
399 if (!(flexcan_read(&regs->mcr) & FLEXCAN_MCR_FRZ_ACK))
400 return -ETIMEDOUT;
401
402 return 0;
403}
404
405static int flexcan_chip_unfreeze(struct flexcan_priv *priv)
406{
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200407 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100408 unsigned int timeout = FLEXCAN_TIMEOUT_US / 10;
409 u32 reg;
410
411 reg = flexcan_read(&regs->mcr);
412 reg &= ~FLEXCAN_MCR_HALT;
413 flexcan_write(reg, &regs->mcr);
414
415 while (timeout-- && (flexcan_read(&regs->mcr) & FLEXCAN_MCR_FRZ_ACK))
David Jander8badd652014-08-27 12:02:16 +0200416 udelay(10);
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100417
418 if (flexcan_read(&regs->mcr) & FLEXCAN_MCR_FRZ_ACK)
419 return -ETIMEDOUT;
420
421 return 0;
422}
423
Marc Kleine-Budde4b5b8222014-02-28 15:16:59 +0100424static int flexcan_chip_softreset(struct flexcan_priv *priv)
425{
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200426 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Budde4b5b8222014-02-28 15:16:59 +0100427 unsigned int timeout = FLEXCAN_TIMEOUT_US / 10;
428
429 flexcan_write(FLEXCAN_MCR_SOFTRST, &regs->mcr);
430 while (timeout-- && (flexcan_read(&regs->mcr) & FLEXCAN_MCR_SOFTRST))
David Jander8badd652014-08-27 12:02:16 +0200431 udelay(10);
Marc Kleine-Budde4b5b8222014-02-28 15:16:59 +0100432
433 if (flexcan_read(&regs->mcr) & FLEXCAN_MCR_SOFTRST)
434 return -ETIMEDOUT;
435
436 return 0;
437}
438
Stefan Agnerec56acf2014-07-15 14:56:20 +0200439static int __flexcan_get_berr_counter(const struct net_device *dev,
440 struct can_berr_counter *bec)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200441{
442 const struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200443 struct flexcan_regs __iomem *regs = priv->regs;
holt@sgi.com61e271e2011-08-16 17:32:20 +0000444 u32 reg = flexcan_read(&regs->ecr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200445
446 bec->txerr = (reg >> 0) & 0xff;
447 bec->rxerr = (reg >> 8) & 0xff;
448
449 return 0;
450}
451
Stefan Agnerec56acf2014-07-15 14:56:20 +0200452static int flexcan_get_berr_counter(const struct net_device *dev,
453 struct can_berr_counter *bec)
454{
455 const struct flexcan_priv *priv = netdev_priv(dev);
456 int err;
457
458 err = clk_prepare_enable(priv->clk_ipg);
459 if (err)
460 return err;
461
462 err = clk_prepare_enable(priv->clk_per);
463 if (err)
464 goto out_disable_ipg;
465
466 err = __flexcan_get_berr_counter(dev, bec);
467
468 clk_disable_unprepare(priv->clk_per);
469 out_disable_ipg:
470 clk_disable_unprepare(priv->clk_ipg);
471
472 return err;
473}
474
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200475static int flexcan_start_xmit(struct sk_buff *skb, struct net_device *dev)
476{
477 const struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200478 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200479 struct can_frame *cf = (struct can_frame *)skb->data;
480 u32 can_id;
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200481 u32 data;
Marc Kleine-Budde10d089b2014-09-23 11:18:11 +0200482 u32 ctrl = FLEXCAN_MB_CODE_TX_DATA | (cf->can_dlc << 16);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200483
484 if (can_dropped_invalid_skb(dev, skb))
485 return NETDEV_TX_OK;
486
487 netif_stop_queue(dev);
488
489 if (cf->can_id & CAN_EFF_FLAG) {
490 can_id = cf->can_id & CAN_EFF_MASK;
491 ctrl |= FLEXCAN_MB_CNT_IDE | FLEXCAN_MB_CNT_SRR;
492 } else {
493 can_id = (cf->can_id & CAN_SFF_MASK) << 18;
494 }
495
496 if (cf->can_id & CAN_RTR_FLAG)
497 ctrl |= FLEXCAN_MB_CNT_RTR;
498
499 if (cf->can_dlc > 0) {
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200500 data = be32_to_cpup((__be32 *)&cf->data[0]);
Marc Kleine-Budde1ba763d2015-08-25 10:39:19 +0200501 flexcan_write(data, &regs->mb[FLEXCAN_TX_BUF_ID].data[0]);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200502 }
503 if (cf->can_dlc > 3) {
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200504 data = be32_to_cpup((__be32 *)&cf->data[4]);
Marc Kleine-Budde1ba763d2015-08-25 10:39:19 +0200505 flexcan_write(data, &regs->mb[FLEXCAN_TX_BUF_ID].data[1]);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200506 }
507
Reuben Dowle9a123492011-11-01 11:18:03 +1300508 can_put_echo_skb(skb, dev, 0);
509
Marc Kleine-Budde1ba763d2015-08-25 10:39:19 +0200510 flexcan_write(can_id, &regs->mb[FLEXCAN_TX_BUF_ID].can_id);
511 flexcan_write(ctrl, &regs->mb[FLEXCAN_TX_BUF_ID].can_ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200512
David Jander25e92442014-09-03 16:47:22 +0200513 /* Errata ERR005829 step8:
514 * Write twice INACTIVE(0x8) code to first MB.
515 */
516 flexcan_write(FLEXCAN_MB_CODE_TX_INACTIVE,
Marc Kleine-Budde1ba763d2015-08-25 10:39:19 +0200517 &regs->mb[FLEXCAN_TX_BUF_RESERVED].can_ctrl);
David Jander25e92442014-09-03 16:47:22 +0200518 flexcan_write(FLEXCAN_MB_CODE_TX_INACTIVE,
Marc Kleine-Budde1ba763d2015-08-25 10:39:19 +0200519 &regs->mb[FLEXCAN_TX_BUF_RESERVED].can_ctrl);
David Jander25e92442014-09-03 16:47:22 +0200520
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200521 return NETDEV_TX_OK;
522}
523
524static void do_bus_err(struct net_device *dev,
525 struct can_frame *cf, u32 reg_esr)
526{
527 struct flexcan_priv *priv = netdev_priv(dev);
528 int rx_errors = 0, tx_errors = 0;
529
530 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
531
532 if (reg_esr & FLEXCAN_ESR_BIT1_ERR) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100533 netdev_dbg(dev, "BIT1_ERR irq\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200534 cf->data[2] |= CAN_ERR_PROT_BIT1;
535 tx_errors = 1;
536 }
537 if (reg_esr & FLEXCAN_ESR_BIT0_ERR) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100538 netdev_dbg(dev, "BIT0_ERR irq\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200539 cf->data[2] |= CAN_ERR_PROT_BIT0;
540 tx_errors = 1;
541 }
542 if (reg_esr & FLEXCAN_ESR_ACK_ERR) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100543 netdev_dbg(dev, "ACK_ERR irq\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200544 cf->can_id |= CAN_ERR_ACK;
Oliver Hartkoppffd461f2015-11-21 18:41:20 +0100545 cf->data[3] = CAN_ERR_PROT_LOC_ACK;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200546 tx_errors = 1;
547 }
548 if (reg_esr & FLEXCAN_ESR_CRC_ERR) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100549 netdev_dbg(dev, "CRC_ERR irq\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200550 cf->data[2] |= CAN_ERR_PROT_BIT;
Oliver Hartkoppffd461f2015-11-21 18:41:20 +0100551 cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200552 rx_errors = 1;
553 }
554 if (reg_esr & FLEXCAN_ESR_FRM_ERR) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100555 netdev_dbg(dev, "FRM_ERR irq\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200556 cf->data[2] |= CAN_ERR_PROT_FORM;
557 rx_errors = 1;
558 }
559 if (reg_esr & FLEXCAN_ESR_STF_ERR) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100560 netdev_dbg(dev, "STF_ERR irq\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200561 cf->data[2] |= CAN_ERR_PROT_STUFF;
562 rx_errors = 1;
563 }
564
565 priv->can.can_stats.bus_error++;
566 if (rx_errors)
567 dev->stats.rx_errors++;
568 if (tx_errors)
569 dev->stats.tx_errors++;
570}
571
572static int flexcan_poll_bus_err(struct net_device *dev, u32 reg_esr)
573{
574 struct sk_buff *skb;
575 struct can_frame *cf;
576
577 skb = alloc_can_err_skb(dev, &cf);
578 if (unlikely(!skb))
579 return 0;
580
581 do_bus_err(dev, cf, reg_esr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200582
583 dev->stats.rx_packets++;
584 dev->stats.rx_bytes += cf->can_dlc;
Marc Kleine-Buddea18ec1b2015-05-08 11:30:29 +0200585 netif_receive_skb(skb);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200586
587 return 1;
588}
589
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200590static int flexcan_poll_state(struct net_device *dev, u32 reg_esr)
591{
592 struct flexcan_priv *priv = netdev_priv(dev);
593 struct sk_buff *skb;
594 struct can_frame *cf;
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000595 enum can_state new_state = 0, rx_state = 0, tx_state = 0;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200596 int flt;
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000597 struct can_berr_counter bec;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200598
599 flt = reg_esr & FLEXCAN_ESR_FLT_CONF_MASK;
600 if (likely(flt == FLEXCAN_ESR_FLT_CONF_ACTIVE)) {
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000601 tx_state = unlikely(reg_esr & FLEXCAN_ESR_TX_WRN) ?
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200602 CAN_STATE_ERROR_WARNING : CAN_STATE_ERROR_ACTIVE;
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000603 rx_state = unlikely(reg_esr & FLEXCAN_ESR_RX_WRN) ?
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200604 CAN_STATE_ERROR_WARNING : CAN_STATE_ERROR_ACTIVE;
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000605 new_state = max(tx_state, rx_state);
Andri Yngvason258ce802015-03-17 13:03:09 +0000606 } else {
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000607 __flexcan_get_berr_counter(dev, &bec);
Andri Yngvason258ce802015-03-17 13:03:09 +0000608 new_state = flt == FLEXCAN_ESR_FLT_CONF_PASSIVE ?
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200609 CAN_STATE_ERROR_PASSIVE : CAN_STATE_BUS_OFF;
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000610 rx_state = bec.rxerr >= bec.txerr ? new_state : 0;
611 tx_state = bec.rxerr <= bec.txerr ? new_state : 0;
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000612 }
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200613
614 /* state hasn't changed */
615 if (likely(new_state == priv->can.state))
616 return 0;
617
618 skb = alloc_can_err_skb(dev, &cf);
619 if (unlikely(!skb))
620 return 0;
621
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000622 can_change_state(dev, cf, tx_state, rx_state);
623
624 if (unlikely(new_state == CAN_STATE_BUS_OFF))
625 can_bus_off(dev);
626
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200627 dev->stats.rx_packets++;
628 dev->stats.rx_bytes += cf->can_dlc;
Marc Kleine-Buddea18ec1b2015-05-08 11:30:29 +0200629 netif_receive_skb(skb);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200630
631 return 1;
632}
633
634static void flexcan_read_fifo(const struct net_device *dev,
635 struct can_frame *cf)
636{
637 const struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200638 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Budde1ba763d2015-08-25 10:39:19 +0200639 struct flexcan_mb __iomem *mb = &regs->mb[0];
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200640 u32 reg_ctrl, reg_id;
641
holt@sgi.com61e271e2011-08-16 17:32:20 +0000642 reg_ctrl = flexcan_read(&mb->can_ctrl);
643 reg_id = flexcan_read(&mb->can_id);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200644 if (reg_ctrl & FLEXCAN_MB_CNT_IDE)
645 cf->can_id = ((reg_id >> 0) & CAN_EFF_MASK) | CAN_EFF_FLAG;
646 else
647 cf->can_id = (reg_id >> 18) & CAN_SFF_MASK;
648
649 if (reg_ctrl & FLEXCAN_MB_CNT_RTR)
650 cf->can_id |= CAN_RTR_FLAG;
651 cf->can_dlc = get_can_dlc((reg_ctrl >> 16) & 0xf);
652
holt@sgi.com61e271e2011-08-16 17:32:20 +0000653 *(__be32 *)(cf->data + 0) = cpu_to_be32(flexcan_read(&mb->data[0]));
654 *(__be32 *)(cf->data + 4) = cpu_to_be32(flexcan_read(&mb->data[1]));
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200655
656 /* mark as read */
holt@sgi.com61e271e2011-08-16 17:32:20 +0000657 flexcan_write(FLEXCAN_IFLAG_RX_FIFO_AVAILABLE, &regs->iflag1);
658 flexcan_read(&regs->timer);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200659}
660
661static int flexcan_read_frame(struct net_device *dev)
662{
663 struct net_device_stats *stats = &dev->stats;
664 struct can_frame *cf;
665 struct sk_buff *skb;
666
667 skb = alloc_can_skb(dev, &cf);
668 if (unlikely(!skb)) {
669 stats->rx_dropped++;
670 return 0;
671 }
672
673 flexcan_read_fifo(dev, cf);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200674
675 stats->rx_packets++;
676 stats->rx_bytes += cf->can_dlc;
Marc Kleine-Buddea18ec1b2015-05-08 11:30:29 +0200677 netif_receive_skb(skb);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200678
Fabio Baltieriadccadb2012-12-18 18:50:58 +0100679 can_led_event(dev, CAN_LED_EVENT_RX);
680
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200681 return 1;
682}
683
684static int flexcan_poll(struct napi_struct *napi, int quota)
685{
686 struct net_device *dev = napi->dev;
687 const struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200688 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200689 u32 reg_iflag1, reg_esr;
690 int work_done = 0;
691
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200692 /* The error bits are cleared on read,
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200693 * use saved value from irq handler.
694 */
holt@sgi.com61e271e2011-08-16 17:32:20 +0000695 reg_esr = flexcan_read(&regs->esr) | priv->reg_esr;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200696
697 /* handle state changes */
698 work_done += flexcan_poll_state(dev, reg_esr);
699
700 /* handle RX-FIFO */
holt@sgi.com61e271e2011-08-16 17:32:20 +0000701 reg_iflag1 = flexcan_read(&regs->iflag1);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200702 while (reg_iflag1 & FLEXCAN_IFLAG_RX_FIFO_AVAILABLE &&
703 work_done < quota) {
704 work_done += flexcan_read_frame(dev);
holt@sgi.com61e271e2011-08-16 17:32:20 +0000705 reg_iflag1 = flexcan_read(&regs->iflag1);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200706 }
707
708 /* report bus errors */
709 if (flexcan_has_and_handle_berr(priv, reg_esr) && work_done < quota)
710 work_done += flexcan_poll_bus_err(dev, reg_esr);
711
712 if (work_done < quota) {
Eric Dumazet6ad20162017-01-30 08:22:01 -0800713 napi_complete_done(napi, work_done);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200714 /* enable IRQs */
holt@sgi.com61e271e2011-08-16 17:32:20 +0000715 flexcan_write(FLEXCAN_IFLAG_DEFAULT, &regs->imask1);
716 flexcan_write(priv->reg_ctrl_default, &regs->ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200717 }
718
719 return work_done;
720}
721
722static irqreturn_t flexcan_irq(int irq, void *dev_id)
723{
724 struct net_device *dev = dev_id;
725 struct net_device_stats *stats = &dev->stats;
726 struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200727 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200728 u32 reg_iflag1, reg_esr;
729
holt@sgi.com61e271e2011-08-16 17:32:20 +0000730 reg_iflag1 = flexcan_read(&regs->iflag1);
731 reg_esr = flexcan_read(&regs->esr);
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200732
Wolfgang Grandegger6e9d5542011-12-12 16:09:28 +0100733 /* ACK all bus error and state change IRQ sources */
734 if (reg_esr & FLEXCAN_ESR_ALL_INT)
735 flexcan_write(reg_esr & FLEXCAN_ESR_ALL_INT, &regs->esr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200736
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200737 /* schedule NAPI in case of:
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200738 * - rx IRQ
739 * - state change IRQ
740 * - bus error IRQ and bus error reporting is activated
741 */
742 if ((reg_iflag1 & FLEXCAN_IFLAG_RX_FIFO_AVAILABLE) ||
743 (reg_esr & FLEXCAN_ESR_ERR_STATE) ||
744 flexcan_has_and_handle_berr(priv, reg_esr)) {
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200745 /* The error bits are cleared on read,
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200746 * save them for later use.
747 */
748 priv->reg_esr = reg_esr & FLEXCAN_ESR_ERR_BUS;
holt@sgi.com61e271e2011-08-16 17:32:20 +0000749 flexcan_write(FLEXCAN_IFLAG_DEFAULT &
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200750 ~FLEXCAN_IFLAG_RX_FIFO_AVAILABLE, &regs->imask1);
holt@sgi.com61e271e2011-08-16 17:32:20 +0000751 flexcan_write(priv->reg_ctrl_default & ~FLEXCAN_CTRL_ERR_ALL,
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200752 &regs->ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200753 napi_schedule(&priv->napi);
754 }
755
756 /* FIFO overflow */
757 if (reg_iflag1 & FLEXCAN_IFLAG_RX_FIFO_OVERFLOW) {
holt@sgi.com61e271e2011-08-16 17:32:20 +0000758 flexcan_write(FLEXCAN_IFLAG_RX_FIFO_OVERFLOW, &regs->iflag1);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200759 dev->stats.rx_over_errors++;
760 dev->stats.rx_errors++;
761 }
762
763 /* transmission complete interrupt */
764 if (reg_iflag1 & (1 << FLEXCAN_TX_BUF_ID)) {
Reuben Dowle9a123492011-11-01 11:18:03 +1300765 stats->tx_bytes += can_get_echo_skb(dev, 0);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200766 stats->tx_packets++;
Fabio Baltieriadccadb2012-12-18 18:50:58 +0100767 can_led_event(dev, CAN_LED_EVENT_TX);
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200768
769 /* after sending a RTR frame MB is in RX mode */
Marc Kleine-Buddede594482014-09-16 15:31:27 +0200770 flexcan_write(FLEXCAN_MB_CODE_TX_INACTIVE,
Marc Kleine-Budde1ba763d2015-08-25 10:39:19 +0200771 &regs->mb[FLEXCAN_TX_BUF_ID].can_ctrl);
holt@sgi.com61e271e2011-08-16 17:32:20 +0000772 flexcan_write((1 << FLEXCAN_TX_BUF_ID), &regs->iflag1);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200773 netif_wake_queue(dev);
774 }
775
776 return IRQ_HANDLED;
777}
778
779static void flexcan_set_bittiming(struct net_device *dev)
780{
781 const struct flexcan_priv *priv = netdev_priv(dev);
782 const struct can_bittiming *bt = &priv->can.bittiming;
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200783 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200784 u32 reg;
785
holt@sgi.com61e271e2011-08-16 17:32:20 +0000786 reg = flexcan_read(&regs->ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200787 reg &= ~(FLEXCAN_CTRL_PRESDIV(0xff) |
788 FLEXCAN_CTRL_RJW(0x3) |
789 FLEXCAN_CTRL_PSEG1(0x7) |
790 FLEXCAN_CTRL_PSEG2(0x7) |
791 FLEXCAN_CTRL_PROPSEG(0x7) |
792 FLEXCAN_CTRL_LPB |
793 FLEXCAN_CTRL_SMP |
794 FLEXCAN_CTRL_LOM);
795
796 reg |= FLEXCAN_CTRL_PRESDIV(bt->brp - 1) |
797 FLEXCAN_CTRL_PSEG1(bt->phase_seg1 - 1) |
798 FLEXCAN_CTRL_PSEG2(bt->phase_seg2 - 1) |
799 FLEXCAN_CTRL_RJW(bt->sjw - 1) |
800 FLEXCAN_CTRL_PROPSEG(bt->prop_seg - 1);
801
802 if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)
803 reg |= FLEXCAN_CTRL_LPB;
804 if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
805 reg |= FLEXCAN_CTRL_LOM;
806 if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
807 reg |= FLEXCAN_CTRL_SMP;
808
Lucas Stach7a4b6c82015-08-07 17:16:03 +0200809 netdev_dbg(dev, "writing ctrl=0x%08x\n", reg);
holt@sgi.com61e271e2011-08-16 17:32:20 +0000810 flexcan_write(reg, &regs->ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200811
812 /* print chip status */
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100813 netdev_dbg(dev, "%s: mcr=0x%08x ctrl=0x%08x\n", __func__,
814 flexcan_read(&regs->mcr), flexcan_read(&regs->ctrl));
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200815}
816
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200817/* flexcan_chip_start
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200818 *
819 * this functions is entered with clocks enabled
820 *
821 */
822static int flexcan_chip_start(struct net_device *dev)
823{
824 struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200825 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Budde6f75fce2014-09-23 11:03:01 +0200826 u32 reg_mcr, reg_ctrl, reg_ctrl2, reg_mecr;
David S. Miller1f6d8032014-09-23 12:09:27 -0400827 int err, i;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200828
829 /* enable module */
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100830 err = flexcan_chip_enable(priv);
831 if (err)
832 return err;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200833
834 /* soft reset */
Marc Kleine-Budde4b5b8222014-02-28 15:16:59 +0100835 err = flexcan_chip_softreset(priv);
836 if (err)
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100837 goto out_chip_disable;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200838
839 flexcan_set_bittiming(dev);
840
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200841 /* MCR
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200842 *
843 * enable freeze
844 * enable fifo
845 * halt now
846 * only supervisor access
847 * enable warning int
Reuben Dowle9a123492011-11-01 11:18:03 +1300848 * disable local echo
Marc Kleine-Budde749de6f2015-08-31 21:32:34 +0200849 * choose format C
850 * set max mailbox number
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200851 */
holt@sgi.com61e271e2011-08-16 17:32:20 +0000852 reg_mcr = flexcan_read(&regs->mcr);
Marc Kleine-Budded5a7b402013-10-04 10:52:36 +0200853 reg_mcr &= ~FLEXCAN_MCR_MAXMB(0xff);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200854 reg_mcr |= FLEXCAN_MCR_FRZ | FLEXCAN_MCR_FEN | FLEXCAN_MCR_HALT |
Marc Kleine-Budde749de6f2015-08-31 21:32:34 +0200855 FLEXCAN_MCR_SUPV | FLEXCAN_MCR_WRN_EN | FLEXCAN_MCR_SRX_DIS |
856 FLEXCAN_MCR_IDAM_C | FLEXCAN_MCR_MAXMB(FLEXCAN_TX_BUF_ID);
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100857 netdev_dbg(dev, "%s: writing mcr=0x%08x", __func__, reg_mcr);
holt@sgi.com61e271e2011-08-16 17:32:20 +0000858 flexcan_write(reg_mcr, &regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200859
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200860 /* CTRL
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200861 *
862 * disable timer sync feature
863 *
864 * disable auto busoff recovery
865 * transmit lowest buffer first
866 *
867 * enable tx and rx warning interrupt
868 * enable bus off interrupt
869 * (== FLEXCAN_CTRL_ERR_STATE)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200870 */
holt@sgi.com61e271e2011-08-16 17:32:20 +0000871 reg_ctrl = flexcan_read(&regs->ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200872 reg_ctrl &= ~FLEXCAN_CTRL_TSYN;
873 reg_ctrl |= FLEXCAN_CTRL_BOFF_REC | FLEXCAN_CTRL_LBUF |
Wolfgang Grandegger4f72e5f2012-09-28 03:17:15 +0000874 FLEXCAN_CTRL_ERR_STATE;
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200875
876 /* enable the "error interrupt" (FLEXCAN_CTRL_ERR_MSK),
Wolfgang Grandegger4f72e5f2012-09-28 03:17:15 +0000877 * on most Flexcan cores, too. Otherwise we don't get
878 * any error warning or passive interrupts.
879 */
Marc Kleine-Buddef377bff2015-05-08 15:22:36 +0200880 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_BROKEN_ERR_STATE ||
Wolfgang Grandegger4f72e5f2012-09-28 03:17:15 +0000881 priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING)
882 reg_ctrl |= FLEXCAN_CTRL_ERR_MSK;
Alexander Steinbc03a542014-08-12 10:47:21 +0200883 else
884 reg_ctrl &= ~FLEXCAN_CTRL_ERR_MSK;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200885
886 /* save for later use */
887 priv->reg_ctrl_default = reg_ctrl;
Marc Kleine-Budde6fa7da22015-08-27 14:24:48 +0200888 /* leave interrupts disabled for now */
889 reg_ctrl &= ~FLEXCAN_CTRL_ERR_ALL;
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100890 netdev_dbg(dev, "%s: writing ctrl=0x%08x", __func__, reg_ctrl);
holt@sgi.com61e271e2011-08-16 17:32:20 +0000891 flexcan_write(reg_ctrl, &regs->ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200892
David Janderfc05b882014-08-27 11:58:05 +0200893 /* clear and invalidate all mailboxes first */
Marc Kleine-Budde1ba763d2015-08-25 10:39:19 +0200894 for (i = FLEXCAN_TX_BUF_ID; i < ARRAY_SIZE(regs->mb); i++) {
David Janderfc05b882014-08-27 11:58:05 +0200895 flexcan_write(FLEXCAN_MB_CODE_RX_INACTIVE,
Marc Kleine-Budde1ba763d2015-08-25 10:39:19 +0200896 &regs->mb[i].can_ctrl);
David Janderfc05b882014-08-27 11:58:05 +0200897 }
898
David Jander25e92442014-09-03 16:47:22 +0200899 /* Errata ERR005829: mark first TX mailbox as INACTIVE */
900 flexcan_write(FLEXCAN_MB_CODE_TX_INACTIVE,
Marc Kleine-Budde1ba763d2015-08-25 10:39:19 +0200901 &regs->mb[FLEXCAN_TX_BUF_RESERVED].can_ctrl);
David Jander25e92442014-09-03 16:47:22 +0200902
Marc Kleine-Buddec32fe4a2014-09-16 12:39:28 +0200903 /* mark TX mailbox as INACTIVE */
904 flexcan_write(FLEXCAN_MB_CODE_TX_INACTIVE,
Marc Kleine-Budde1ba763d2015-08-25 10:39:19 +0200905 &regs->mb[FLEXCAN_TX_BUF_ID].can_ctrl);
Marc Kleine-Budded5a7b402013-10-04 10:52:36 +0200906
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200907 /* acceptance mask/acceptance code (accept everything) */
holt@sgi.com61e271e2011-08-16 17:32:20 +0000908 flexcan_write(0x0, &regs->rxgmask);
909 flexcan_write(0x0, &regs->rx14mask);
910 flexcan_write(0x0, &regs->rx15mask);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200911
Marc Kleine-Buddef377bff2015-05-08 15:22:36 +0200912 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_DISABLE_RXFG)
Hui Wang30c1e672012-06-28 16:21:35 +0800913 flexcan_write(0x0, &regs->rxfgmask);
914
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200915 /* On Vybrid, disable memory error detection interrupts
Stefan Agnercdce8442014-07-15 14:56:21 +0200916 * and freeze mode.
917 * This also works around errata e5295 which generates
918 * false positive memory errors and put the device in
919 * freeze mode.
920 */
Marc Kleine-Buddef377bff2015-05-08 15:22:36 +0200921 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_DISABLE_MECR) {
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200922 /* Follow the protocol as described in "Detection
Stefan Agnercdce8442014-07-15 14:56:21 +0200923 * and Correction of Memory Errors" to write to
924 * MECR register
925 */
Marc Kleine-Budde6f75fce2014-09-23 11:03:01 +0200926 reg_ctrl2 = flexcan_read(&regs->ctrl2);
927 reg_ctrl2 |= FLEXCAN_CTRL2_ECRWRE;
928 flexcan_write(reg_ctrl2, &regs->ctrl2);
Stefan Agnercdce8442014-07-15 14:56:21 +0200929
930 reg_mecr = flexcan_read(&regs->mecr);
931 reg_mecr &= ~FLEXCAN_MECR_ECRWRDIS;
932 flexcan_write(reg_mecr, &regs->mecr);
933 reg_mecr &= ~(FLEXCAN_MECR_NCEFAFRZ | FLEXCAN_MECR_HANCEI_MSK |
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200934 FLEXCAN_MECR_FANCEI_MSK);
Stefan Agnercdce8442014-07-15 14:56:21 +0200935 flexcan_write(reg_mecr, &regs->mecr);
936 }
937
Marc Kleine-Buddef0036982014-02-28 17:18:27 +0100938 err = flexcan_transceiver_enable(priv);
939 if (err)
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100940 goto out_chip_disable;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200941
942 /* synchronize with the can bus */
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100943 err = flexcan_chip_unfreeze(priv);
944 if (err)
945 goto out_transceiver_disable;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200946
947 priv->can.state = CAN_STATE_ERROR_ACTIVE;
948
Marc Kleine-Budde6fa7da22015-08-27 14:24:48 +0200949 /* enable interrupts atomically */
950 disable_irq(dev->irq);
951 flexcan_write(priv->reg_ctrl_default, &regs->ctrl);
holt@sgi.com61e271e2011-08-16 17:32:20 +0000952 flexcan_write(FLEXCAN_IFLAG_DEFAULT, &regs->imask1);
Marc Kleine-Budde6fa7da22015-08-27 14:24:48 +0200953 enable_irq(dev->irq);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200954
955 /* print chip status */
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100956 netdev_dbg(dev, "%s: reading mcr=0x%08x ctrl=0x%08x\n", __func__,
957 flexcan_read(&regs->mcr), flexcan_read(&regs->ctrl));
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200958
959 return 0;
960
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100961 out_transceiver_disable:
962 flexcan_transceiver_disable(priv);
963 out_chip_disable:
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200964 flexcan_chip_disable(priv);
965 return err;
966}
967
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200968/* flexcan_chip_stop
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200969 *
970 * this functions is entered with clocks enabled
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200971 */
972static void flexcan_chip_stop(struct net_device *dev)
973{
974 struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200975 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200976
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100977 /* freeze + disable module */
978 flexcan_chip_freeze(priv);
979 flexcan_chip_disable(priv);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200980
Marc Kleine-Budde5be93bd2014-02-19 12:00:51 +0100981 /* Disable all interrupts */
982 flexcan_write(0, &regs->imask1);
983 flexcan_write(priv->reg_ctrl_default & ~FLEXCAN_CTRL_ERR_ALL,
984 &regs->ctrl);
985
Marc Kleine-Buddef0036982014-02-28 17:18:27 +0100986 flexcan_transceiver_disable(priv);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200987 priv->can.state = CAN_STATE_STOPPED;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200988}
989
990static int flexcan_open(struct net_device *dev)
991{
992 struct flexcan_priv *priv = netdev_priv(dev);
993 int err;
994
Fabio Estevamaa101812013-07-22 12:41:40 -0300995 err = clk_prepare_enable(priv->clk_ipg);
996 if (err)
997 return err;
998
999 err = clk_prepare_enable(priv->clk_per);
1000 if (err)
1001 goto out_disable_ipg;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001002
1003 err = open_candev(dev);
1004 if (err)
Fabio Estevamaa101812013-07-22 12:41:40 -03001005 goto out_disable_per;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001006
1007 err = request_irq(dev->irq, flexcan_irq, IRQF_SHARED, dev->name, dev);
1008 if (err)
1009 goto out_close;
1010
1011 /* start chip and queuing */
1012 err = flexcan_chip_start(dev);
1013 if (err)
Marc Kleine-Budde7e9e1482014-02-28 14:52:01 +01001014 goto out_free_irq;
Fabio Baltieriadccadb2012-12-18 18:50:58 +01001015
1016 can_led_event(dev, CAN_LED_EVENT_OPEN);
1017
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001018 napi_enable(&priv->napi);
1019 netif_start_queue(dev);
1020
1021 return 0;
1022
Marc Kleine-Budde7e9e1482014-02-28 14:52:01 +01001023 out_free_irq:
1024 free_irq(dev->irq, dev);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001025 out_close:
1026 close_candev(dev);
Fabio Estevamaa101812013-07-22 12:41:40 -03001027 out_disable_per:
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001028 clk_disable_unprepare(priv->clk_per);
Fabio Estevamaa101812013-07-22 12:41:40 -03001029 out_disable_ipg:
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001030 clk_disable_unprepare(priv->clk_ipg);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001031
1032 return err;
1033}
1034
1035static int flexcan_close(struct net_device *dev)
1036{
1037 struct flexcan_priv *priv = netdev_priv(dev);
1038
1039 netif_stop_queue(dev);
1040 napi_disable(&priv->napi);
1041 flexcan_chip_stop(dev);
1042
1043 free_irq(dev->irq, dev);
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001044 clk_disable_unprepare(priv->clk_per);
1045 clk_disable_unprepare(priv->clk_ipg);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001046
1047 close_candev(dev);
1048
Fabio Baltieriadccadb2012-12-18 18:50:58 +01001049 can_led_event(dev, CAN_LED_EVENT_STOP);
1050
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001051 return 0;
1052}
1053
1054static int flexcan_set_mode(struct net_device *dev, enum can_mode mode)
1055{
1056 int err;
1057
1058 switch (mode) {
1059 case CAN_MODE_START:
1060 err = flexcan_chip_start(dev);
1061 if (err)
1062 return err;
1063
1064 netif_wake_queue(dev);
1065 break;
1066
1067 default:
1068 return -EOPNOTSUPP;
1069 }
1070
1071 return 0;
1072}
1073
1074static const struct net_device_ops flexcan_netdev_ops = {
1075 .ndo_open = flexcan_open,
1076 .ndo_stop = flexcan_close,
1077 .ndo_start_xmit = flexcan_start_xmit,
Oliver Hartkoppc971fa22014-03-07 09:23:41 +01001078 .ndo_change_mtu = can_change_mtu,
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001079};
1080
Bill Pemberton3c8ac0f2012-12-03 09:22:44 -05001081static int register_flexcandev(struct net_device *dev)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001082{
1083 struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Budde89af8742015-05-08 09:32:58 +02001084 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001085 u32 reg, err;
1086
Fabio Estevamaa101812013-07-22 12:41:40 -03001087 err = clk_prepare_enable(priv->clk_ipg);
1088 if (err)
1089 return err;
1090
1091 err = clk_prepare_enable(priv->clk_per);
1092 if (err)
1093 goto out_disable_ipg;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001094
1095 /* select "bus clock", chip must be disabled */
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001096 err = flexcan_chip_disable(priv);
1097 if (err)
1098 goto out_disable_per;
holt@sgi.com61e271e2011-08-16 17:32:20 +00001099 reg = flexcan_read(&regs->ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001100 reg |= FLEXCAN_CTRL_CLK_SRC;
holt@sgi.com61e271e2011-08-16 17:32:20 +00001101 flexcan_write(reg, &regs->ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001102
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001103 err = flexcan_chip_enable(priv);
1104 if (err)
1105 goto out_chip_disable;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001106
1107 /* set freeze, halt and activate FIFO, restrict register access */
holt@sgi.com61e271e2011-08-16 17:32:20 +00001108 reg = flexcan_read(&regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001109 reg |= FLEXCAN_MCR_FRZ | FLEXCAN_MCR_HALT |
1110 FLEXCAN_MCR_FEN | FLEXCAN_MCR_SUPV;
holt@sgi.com61e271e2011-08-16 17:32:20 +00001111 flexcan_write(reg, &regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001112
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +02001113 /* Currently we only support newer versions of this core
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001114 * featuring a RX FIFO. Older cores found on some Coldfire
1115 * derivates are not yet supported.
1116 */
holt@sgi.com61e271e2011-08-16 17:32:20 +00001117 reg = flexcan_read(&regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001118 if (!(reg & FLEXCAN_MCR_FEN)) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +01001119 netdev_err(dev, "Could not enable RX FIFO, unsupported core\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001120 err = -ENODEV;
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001121 goto out_chip_disable;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001122 }
1123
1124 err = register_candev(dev);
1125
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001126 /* disable core and turn off clocks */
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001127 out_chip_disable:
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001128 flexcan_chip_disable(priv);
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001129 out_disable_per:
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001130 clk_disable_unprepare(priv->clk_per);
Fabio Estevamaa101812013-07-22 12:41:40 -03001131 out_disable_ipg:
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001132 clk_disable_unprepare(priv->clk_ipg);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001133
1134 return err;
1135}
1136
Bill Pemberton3c8ac0f2012-12-03 09:22:44 -05001137static void unregister_flexcandev(struct net_device *dev)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001138{
1139 unregister_candev(dev);
1140}
1141
Hui Wang30c1e672012-06-28 16:21:35 +08001142static const struct of_device_id flexcan_of_match[] = {
Hui Wang30c1e672012-06-28 16:21:35 +08001143 { .compatible = "fsl,imx6q-flexcan", .data = &fsl_imx6q_devtype_data, },
Marc Kleine-Buddee3587842013-10-03 23:51:55 +02001144 { .compatible = "fsl,imx28-flexcan", .data = &fsl_imx28_devtype_data, },
1145 { .compatible = "fsl,p1010-flexcan", .data = &fsl_p1010_devtype_data, },
Stefan Agnercdce8442014-07-15 14:56:21 +02001146 { .compatible = "fsl,vf610-flexcan", .data = &fsl_vf610_devtype_data, },
Hui Wang30c1e672012-06-28 16:21:35 +08001147 { /* sentinel */ },
1148};
Marc Kleine-Budde4358a9d2012-10-04 10:55:35 +02001149MODULE_DEVICE_TABLE(of, flexcan_of_match);
Hui Wang30c1e672012-06-28 16:21:35 +08001150
1151static const struct platform_device_id flexcan_id_table[] = {
1152 { .name = "flexcan", .driver_data = (kernel_ulong_t)&fsl_p1010_devtype_data, },
1153 { /* sentinel */ },
1154};
Marc Kleine-Budde4358a9d2012-10-04 10:55:35 +02001155MODULE_DEVICE_TABLE(platform, flexcan_id_table);
Hui Wang30c1e672012-06-28 16:21:35 +08001156
Bill Pemberton3c8ac0f2012-12-03 09:22:44 -05001157static int flexcan_probe(struct platform_device *pdev)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001158{
Hui Wang30c1e672012-06-28 16:21:35 +08001159 const struct of_device_id *of_id;
Marc Kleine-Buddedda0b3b2012-07-13 14:52:48 +02001160 const struct flexcan_devtype_data *devtype_data;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001161 struct net_device *dev;
1162 struct flexcan_priv *priv;
Andreas Werner555828e2015-03-22 17:35:52 +01001163 struct regulator *reg_xceiver;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001164 struct resource *mem;
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001165 struct clk *clk_ipg = NULL, *clk_per = NULL;
Marc Kleine-Budde89af8742015-05-08 09:32:58 +02001166 struct flexcan_regs __iomem *regs;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001167 int err, irq;
holt@sgi.com97efe9a2011-08-16 17:32:23 +00001168 u32 clock_freq = 0;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001169
Andreas Werner555828e2015-03-22 17:35:52 +01001170 reg_xceiver = devm_regulator_get(&pdev->dev, "xceiver");
1171 if (PTR_ERR(reg_xceiver) == -EPROBE_DEFER)
1172 return -EPROBE_DEFER;
1173 else if (IS_ERR(reg_xceiver))
1174 reg_xceiver = NULL;
1175
Hui Wangafc016d2012-06-28 16:21:34 +08001176 if (pdev->dev.of_node)
1177 of_property_read_u32(pdev->dev.of_node,
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +02001178 "clock-frequency", &clock_freq);
holt@sgi.com97efe9a2011-08-16 17:32:23 +00001179
1180 if (!clock_freq) {
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001181 clk_ipg = devm_clk_get(&pdev->dev, "ipg");
1182 if (IS_ERR(clk_ipg)) {
1183 dev_err(&pdev->dev, "no ipg clock defined\n");
Fabio Estevam933e4af2013-07-22 12:41:39 -03001184 return PTR_ERR(clk_ipg);
holt@sgi.com97efe9a2011-08-16 17:32:23 +00001185 }
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001186
1187 clk_per = devm_clk_get(&pdev->dev, "per");
1188 if (IS_ERR(clk_per)) {
1189 dev_err(&pdev->dev, "no per clock defined\n");
Fabio Estevam933e4af2013-07-22 12:41:39 -03001190 return PTR_ERR(clk_per);
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001191 }
Marc Kleine-Budde1a3e5172013-11-25 22:15:20 +01001192 clock_freq = clk_get_rate(clk_per);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001193 }
1194
1195 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1196 irq = platform_get_irq(pdev, 0);
Fabio Estevam933e4af2013-07-22 12:41:39 -03001197 if (irq <= 0)
1198 return -ENODEV;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001199
Marc Kleine-Budde89af8742015-05-08 09:32:58 +02001200 regs = devm_ioremap_resource(&pdev->dev, mem);
1201 if (IS_ERR(regs))
1202 return PTR_ERR(regs);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001203
Hui Wang30c1e672012-06-28 16:21:35 +08001204 of_id = of_match_device(flexcan_of_match, &pdev->dev);
1205 if (of_id) {
1206 devtype_data = of_id->data;
Marc Kleine-Budded0873e62014-03-04 22:04:22 +01001207 } else if (platform_get_device_id(pdev)->driver_data) {
Hui Wang30c1e672012-06-28 16:21:35 +08001208 devtype_data = (struct flexcan_devtype_data *)
Marc Kleine-Budded0873e62014-03-04 22:04:22 +01001209 platform_get_device_id(pdev)->driver_data;
Hui Wang30c1e672012-06-28 16:21:35 +08001210 } else {
Fabio Estevam933e4af2013-07-22 12:41:39 -03001211 return -ENODEV;
Hui Wang30c1e672012-06-28 16:21:35 +08001212 }
1213
Fabio Estevam933e4af2013-07-22 12:41:39 -03001214 dev = alloc_candev(sizeof(struct flexcan_priv), 1);
1215 if (!dev)
1216 return -ENOMEM;
1217
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001218 dev->netdev_ops = &flexcan_netdev_ops;
1219 dev->irq = irq;
Reuben Dowle9a123492011-11-01 11:18:03 +13001220 dev->flags |= IFF_ECHO;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001221
1222 priv = netdev_priv(dev);
holt@sgi.com97efe9a2011-08-16 17:32:23 +00001223 priv->can.clock.freq = clock_freq;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001224 priv->can.bittiming_const = &flexcan_bittiming_const;
1225 priv->can.do_set_mode = flexcan_set_mode;
1226 priv->can.do_get_berr_counter = flexcan_get_berr_counter;
1227 priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK |
1228 CAN_CTRLMODE_LISTENONLY | CAN_CTRLMODE_3_SAMPLES |
1229 CAN_CTRLMODE_BERR_REPORTING;
Marc Kleine-Budde89af8742015-05-08 09:32:58 +02001230 priv->regs = regs;
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001231 priv->clk_ipg = clk_ipg;
1232 priv->clk_per = clk_per;
Jingoo Han84ae6642013-09-10 17:41:30 +09001233 priv->pdata = dev_get_platdata(&pdev->dev);
Hui Wang30c1e672012-06-28 16:21:35 +08001234 priv->devtype_data = devtype_data;
Andreas Werner555828e2015-03-22 17:35:52 +01001235 priv->reg_xceiver = reg_xceiver;
Fabio Estevamb7c41142013-06-10 23:12:57 -03001236
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001237 netif_napi_add(dev, &priv->napi, flexcan_poll, FLEXCAN_NAPI_WEIGHT);
1238
Libo Chend75ea942013-08-21 18:15:08 +08001239 platform_set_drvdata(pdev, dev);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001240 SET_NETDEV_DEV(dev, &pdev->dev);
1241
1242 err = register_flexcandev(dev);
1243 if (err) {
1244 dev_err(&pdev->dev, "registering netdev failed\n");
1245 goto failed_register;
1246 }
1247
Fabio Baltieriadccadb2012-12-18 18:50:58 +01001248 devm_can_led_init(dev);
1249
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001250 dev_info(&pdev->dev, "device registered (reg_base=%p, irq=%d)\n",
Marc Kleine-Budde89af8742015-05-08 09:32:58 +02001251 priv->regs, dev->irq);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001252
1253 return 0;
1254
1255 failed_register:
1256 free_candev(dev);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001257 return err;
1258}
1259
Bill Pemberton3c8ac0f2012-12-03 09:22:44 -05001260static int flexcan_remove(struct platform_device *pdev)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001261{
1262 struct net_device *dev = platform_get_drvdata(pdev);
Marc Kleine-Budded96e43e2014-02-28 20:48:36 +01001263 struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001264
1265 unregister_flexcandev(dev);
Marc Kleine-Budded96e43e2014-02-28 20:48:36 +01001266 netif_napi_del(&priv->napi);
Marc Kleine-Budde9a275862010-10-21 05:07:58 +00001267 free_candev(dev);
1268
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001269 return 0;
1270}
1271
Marc Kleine-Budde08c6d352014-03-05 19:10:44 +01001272static int __maybe_unused flexcan_suspend(struct device *device)
Eric Bénard8b5e2182012-05-08 17:12:17 +02001273{
Fabio Estevam588e7a82013-05-20 15:43:43 -03001274 struct net_device *dev = dev_get_drvdata(device);
Eric Bénard8b5e2182012-05-08 17:12:17 +02001275 struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001276 int err;
Eric Bénard8b5e2182012-05-08 17:12:17 +02001277
Eric Bénard8b5e2182012-05-08 17:12:17 +02001278 if (netif_running(dev)) {
Fabio Estevam4de349e2016-08-17 12:41:08 -03001279 err = flexcan_chip_disable(priv);
1280 if (err)
1281 return err;
Eric Bénard8b5e2182012-05-08 17:12:17 +02001282 netif_stop_queue(dev);
1283 netif_device_detach(dev);
1284 }
1285 priv->can.state = CAN_STATE_SLEEPING;
1286
1287 return 0;
1288}
1289
Marc Kleine-Budde08c6d352014-03-05 19:10:44 +01001290static int __maybe_unused flexcan_resume(struct device *device)
Eric Bénard8b5e2182012-05-08 17:12:17 +02001291{
Fabio Estevam588e7a82013-05-20 15:43:43 -03001292 struct net_device *dev = dev_get_drvdata(device);
Eric Bénard8b5e2182012-05-08 17:12:17 +02001293 struct flexcan_priv *priv = netdev_priv(dev);
Fabio Estevam4de349e2016-08-17 12:41:08 -03001294 int err;
Eric Bénard8b5e2182012-05-08 17:12:17 +02001295
1296 priv->can.state = CAN_STATE_ERROR_ACTIVE;
1297 if (netif_running(dev)) {
1298 netif_device_attach(dev);
1299 netif_start_queue(dev);
Fabio Estevam4de349e2016-08-17 12:41:08 -03001300 err = flexcan_chip_enable(priv);
1301 if (err)
1302 return err;
Eric Bénard8b5e2182012-05-08 17:12:17 +02001303 }
Fabio Estevam4de349e2016-08-17 12:41:08 -03001304 return 0;
Eric Bénard8b5e2182012-05-08 17:12:17 +02001305}
Fabio Estevam588e7a82013-05-20 15:43:43 -03001306
1307static SIMPLE_DEV_PM_OPS(flexcan_pm_ops, flexcan_suspend, flexcan_resume);
Eric Bénard8b5e2182012-05-08 17:12:17 +02001308
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001309static struct platform_driver flexcan_driver = {
holt@sgi.comc8aef4c2011-08-16 17:32:22 +00001310 .driver = {
1311 .name = DRV_NAME,
Fabio Estevam588e7a82013-05-20 15:43:43 -03001312 .pm = &flexcan_pm_ops,
holt@sgi.comc8aef4c2011-08-16 17:32:22 +00001313 .of_match_table = flexcan_of_match,
1314 },
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001315 .probe = flexcan_probe,
Bill Pemberton3c8ac0f2012-12-03 09:22:44 -05001316 .remove = flexcan_remove,
Hui Wang30c1e672012-06-28 16:21:35 +08001317 .id_table = flexcan_id_table,
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001318};
1319
Axel Lin871d3372011-11-27 15:42:31 +00001320module_platform_driver(flexcan_driver);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001321
1322MODULE_AUTHOR("Sascha Hauer <kernel@pengutronix.de>, "
1323 "Marc Kleine-Budde <kernel@pengutronix.de>");
1324MODULE_LICENSE("GPL v2");
1325MODULE_DESCRIPTION("CAN port driver for flexcan based chip");