• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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>
26 #include <linux/can/led.h>
27 #include <linux/clk.h>
28 #include <linux/delay.h>
29 #include <linux/interrupt.h>
30 #include <linux/io.h>
31 #include <linux/module.h>
32 #include <linux/of.h>
33 #include <linux/of_device.h>
34 #include <linux/platform_device.h>
35 #include <linux/regulator/consumer.h>
36 
37 #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)
61 #define FLEXCAN_MCR_MAXMB(x)		((x) & 0x7f)
62 #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)
66 
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 
91 /* FLEXCAN control register 2 (CTRL2) bits */
92 #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)
99 
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 
112 /* 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)
140 #define FLEXCAN_ESR_ALL_INT \
141 	(FLEXCAN_ESR_TWRN_INT | FLEXCAN_ESR_RWRN_INT | \
142 	 FLEXCAN_ESR_BOFF_INT | FLEXCAN_ESR_ERR_INT)
143 
144 /* FLEXCAN interrupt flag register (IFLAG) bits */
145 /* Errata ERR005829 step7: Reserve first valid MB */
146 #define FLEXCAN_TX_BUF_RESERVED		8
147 #define FLEXCAN_TX_BUF_ID		9
148 #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 */
157 #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)
160 #define FLEXCAN_MB_CODE_RX_OVERRUN	(0x6 << 24)
161 #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 
168 #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 
174 #define FLEXCAN_TIMEOUT_US		(250)
175 
176 /* FLEXCAN hardware feature flags
177  *
178  * Below is some version info we got:
179  *    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?
187  *
188  * Some SOCs do not have the RX_WARN & TX_WARN interrupt line connected.
189  */
190 #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 */
193 
194 /* Structure of the message buffer */
195 struct flexcan_mb {
196 	u32 can_ctrl;
197 	u32 can_id;
198 	u32 data[2];
199 };
200 
201 /* Structure of the hardware registers */
202 struct 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 */
216 	u32 ctrl2;		/* 0x34 */
217 	u32 esr2;		/* 0x38 */
218 	u32 imeur;		/* 0x3c */
219 	u32 lrfr;		/* 0x40 */
220 	u32 crcr;		/* 0x44 */
221 	u32 rxfgmask;		/* 0x48 */
222 	u32 rxfir;		/* 0x4c */
223 	u32 _reserved3[12];	/* 0x50 */
224 	struct flexcan_mb mb[64];	/* 0x80 */
225 	/* 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
232 	 *				size conf'ed via ctrl2::RFFN
233 	 *				(mx6, vf610)
234 	 */
235 	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 */
244 };
245 
246 struct flexcan_devtype_data {
247 	u32 quirks;		/* quirks needed for different IP cores */
248 };
249 
250 struct flexcan_priv {
251 	struct can_priv can;
252 	struct napi_struct napi;
253 
254 	struct flexcan_regs __iomem *regs;
255 	u32 reg_esr;
256 	u32 reg_ctrl_default;
257 
258 	struct clk *clk_ipg;
259 	struct clk *clk_per;
260 	struct flexcan_platform_data *pdata;
261 	const struct flexcan_devtype_data *devtype_data;
262 	struct regulator *reg_xceiver;
263 };
264 
265 static struct flexcan_devtype_data fsl_p1010_devtype_data = {
266 	.quirks = FLEXCAN_QUIRK_BROKEN_ERR_STATE,
267 };
268 
269 static struct flexcan_devtype_data fsl_imx28_devtype_data;
270 
271 static struct flexcan_devtype_data fsl_imx6q_devtype_data = {
272 	.quirks = FLEXCAN_QUIRK_DISABLE_RXFG,
273 };
274 
275 static struct flexcan_devtype_data fsl_vf610_devtype_data = {
276 	.quirks = FLEXCAN_QUIRK_DISABLE_RXFG | FLEXCAN_QUIRK_DISABLE_MECR,
277 };
278 
279 static const struct can_bittiming_const flexcan_bittiming_const = {
280 	.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 
291 /* Abstract off the read/write for arm versus ppc. This
292  * assumes that PPC uses big-endian registers and everything
293  * else uses little-endian registers, independent of CPU
294  * endianness.
295  */
296 #if defined(CONFIG_PPC)
flexcan_read(void __iomem * addr)297 static inline u32 flexcan_read(void __iomem *addr)
298 {
299 	return in_be32(addr);
300 }
301 
flexcan_write(u32 val,void __iomem * addr)302 static inline void flexcan_write(u32 val, void __iomem *addr)
303 {
304 	out_be32(addr, val);
305 }
306 #else
flexcan_read(void __iomem * addr)307 static inline u32 flexcan_read(void __iomem *addr)
308 {
309 	return readl(addr);
310 }
311 
flexcan_write(u32 val,void __iomem * addr)312 static inline void flexcan_write(u32 val, void __iomem *addr)
313 {
314 	writel(val, addr);
315 }
316 #endif
317 
flexcan_transceiver_enable(const struct flexcan_priv * priv)318 static 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 
flexcan_transceiver_disable(const struct flexcan_priv * priv)326 static 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 
flexcan_has_and_handle_berr(const struct flexcan_priv * priv,u32 reg_esr)334 static 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 
flexcan_chip_enable(struct flexcan_priv * priv)341 static int flexcan_chip_enable(struct flexcan_priv *priv)
342 {
343 	struct flexcan_regs __iomem *regs = priv->regs;
344 	unsigned int timeout = FLEXCAN_TIMEOUT_US / 10;
345 	u32 reg;
346 
347 	reg = flexcan_read(&regs->mcr);
348 	reg &= ~FLEXCAN_MCR_MDIS;
349 	flexcan_write(reg, &regs->mcr);
350 
351 	while (timeout-- && (flexcan_read(&regs->mcr) & FLEXCAN_MCR_LPM_ACK))
352 		udelay(10);
353 
354 	if (flexcan_read(&regs->mcr) & FLEXCAN_MCR_LPM_ACK)
355 		return -ETIMEDOUT;
356 
357 	return 0;
358 }
359 
flexcan_chip_disable(struct flexcan_priv * priv)360 static int flexcan_chip_disable(struct flexcan_priv *priv)
361 {
362 	struct flexcan_regs __iomem *regs = priv->regs;
363 	unsigned int timeout = FLEXCAN_TIMEOUT_US / 10;
364 	u32 reg;
365 
366 	reg = flexcan_read(&regs->mcr);
367 	reg |= FLEXCAN_MCR_MDIS;
368 	flexcan_write(reg, &regs->mcr);
369 
370 	while (timeout-- && !(flexcan_read(&regs->mcr) & FLEXCAN_MCR_LPM_ACK))
371 		udelay(10);
372 
373 	if (!(flexcan_read(&regs->mcr) & FLEXCAN_MCR_LPM_ACK))
374 		return -ETIMEDOUT;
375 
376 	return 0;
377 }
378 
flexcan_chip_freeze(struct flexcan_priv * priv)379 static int flexcan_chip_freeze(struct flexcan_priv *priv)
380 {
381 	struct flexcan_regs __iomem *regs = priv->regs;
382 	unsigned int timeout;
383 	u32 bitrate = priv->can.bittiming.bitrate;
384 	u32 reg;
385 
386 	if (bitrate)
387 		timeout = 1000 * 1000 * 10 / bitrate;
388 	else
389 		timeout = FLEXCAN_TIMEOUT_US / 10;
390 
391 	reg = flexcan_read(&regs->mcr);
392 	reg |= FLEXCAN_MCR_FRZ | FLEXCAN_MCR_HALT;
393 	flexcan_write(reg, &regs->mcr);
394 
395 	while (timeout-- && !(flexcan_read(&regs->mcr) & FLEXCAN_MCR_FRZ_ACK))
396 		udelay(100);
397 
398 	if (!(flexcan_read(&regs->mcr) & FLEXCAN_MCR_FRZ_ACK))
399 		return -ETIMEDOUT;
400 
401 	return 0;
402 }
403 
flexcan_chip_unfreeze(struct flexcan_priv * priv)404 static int flexcan_chip_unfreeze(struct flexcan_priv *priv)
405 {
406 	struct flexcan_regs __iomem *regs = priv->regs;
407 	unsigned int timeout = FLEXCAN_TIMEOUT_US / 10;
408 	u32 reg;
409 
410 	reg = flexcan_read(&regs->mcr);
411 	reg &= ~FLEXCAN_MCR_HALT;
412 	flexcan_write(reg, &regs->mcr);
413 
414 	while (timeout-- && (flexcan_read(&regs->mcr) & FLEXCAN_MCR_FRZ_ACK))
415 		udelay(10);
416 
417 	if (flexcan_read(&regs->mcr) & FLEXCAN_MCR_FRZ_ACK)
418 		return -ETIMEDOUT;
419 
420 	return 0;
421 }
422 
flexcan_chip_softreset(struct flexcan_priv * priv)423 static int flexcan_chip_softreset(struct flexcan_priv *priv)
424 {
425 	struct flexcan_regs __iomem *regs = priv->regs;
426 	unsigned int timeout = FLEXCAN_TIMEOUT_US / 10;
427 
428 	flexcan_write(FLEXCAN_MCR_SOFTRST, &regs->mcr);
429 	while (timeout-- && (flexcan_read(&regs->mcr) & FLEXCAN_MCR_SOFTRST))
430 		udelay(10);
431 
432 	if (flexcan_read(&regs->mcr) & FLEXCAN_MCR_SOFTRST)
433 		return -ETIMEDOUT;
434 
435 	return 0;
436 }
437 
__flexcan_get_berr_counter(const struct net_device * dev,struct can_berr_counter * bec)438 static int __flexcan_get_berr_counter(const struct net_device *dev,
439 				      struct can_berr_counter *bec)
440 {
441 	const struct flexcan_priv *priv = netdev_priv(dev);
442 	struct flexcan_regs __iomem *regs = priv->regs;
443 	u32 reg = flexcan_read(&regs->ecr);
444 
445 	bec->txerr = (reg >> 0) & 0xff;
446 	bec->rxerr = (reg >> 8) & 0xff;
447 
448 	return 0;
449 }
450 
flexcan_get_berr_counter(const struct net_device * dev,struct can_berr_counter * bec)451 static int flexcan_get_berr_counter(const struct net_device *dev,
452 				    struct can_berr_counter *bec)
453 {
454 	const struct flexcan_priv *priv = netdev_priv(dev);
455 	int err;
456 
457 	err = clk_prepare_enable(priv->clk_ipg);
458 	if (err)
459 		return err;
460 
461 	err = clk_prepare_enable(priv->clk_per);
462 	if (err)
463 		goto out_disable_ipg;
464 
465 	err = __flexcan_get_berr_counter(dev, bec);
466 
467 	clk_disable_unprepare(priv->clk_per);
468  out_disable_ipg:
469 	clk_disable_unprepare(priv->clk_ipg);
470 
471 	return err;
472 }
473 
flexcan_start_xmit(struct sk_buff * skb,struct net_device * dev)474 static int flexcan_start_xmit(struct sk_buff *skb, struct net_device *dev)
475 {
476 	const struct flexcan_priv *priv = netdev_priv(dev);
477 	struct flexcan_regs __iomem *regs = priv->regs;
478 	struct can_frame *cf = (struct can_frame *)skb->data;
479 	u32 can_id;
480 	u32 data;
481 	u32 ctrl = FLEXCAN_MB_CODE_TX_DATA | (cf->can_dlc << 16);
482 
483 	if (can_dropped_invalid_skb(dev, skb))
484 		return NETDEV_TX_OK;
485 
486 	netif_stop_queue(dev);
487 
488 	if (cf->can_id & CAN_EFF_FLAG) {
489 		can_id = cf->can_id & CAN_EFF_MASK;
490 		ctrl |= FLEXCAN_MB_CNT_IDE | FLEXCAN_MB_CNT_SRR;
491 	} else {
492 		can_id = (cf->can_id & CAN_SFF_MASK) << 18;
493 	}
494 
495 	if (cf->can_id & CAN_RTR_FLAG)
496 		ctrl |= FLEXCAN_MB_CNT_RTR;
497 
498 	if (cf->can_dlc > 0) {
499 		data = be32_to_cpup((__be32 *)&cf->data[0]);
500 		flexcan_write(data, &regs->mb[FLEXCAN_TX_BUF_ID].data[0]);
501 	}
502 	if (cf->can_dlc > 4) {
503 		data = be32_to_cpup((__be32 *)&cf->data[4]);
504 		flexcan_write(data, &regs->mb[FLEXCAN_TX_BUF_ID].data[1]);
505 	}
506 
507 	can_put_echo_skb(skb, dev, 0);
508 
509 	flexcan_write(can_id, &regs->mb[FLEXCAN_TX_BUF_ID].can_id);
510 	flexcan_write(ctrl, &regs->mb[FLEXCAN_TX_BUF_ID].can_ctrl);
511 
512 	/* Errata ERR005829 step8:
513 	 * Write twice INACTIVE(0x8) code to first MB.
514 	 */
515 	flexcan_write(FLEXCAN_MB_CODE_TX_INACTIVE,
516 		      &regs->mb[FLEXCAN_TX_BUF_RESERVED].can_ctrl);
517 	flexcan_write(FLEXCAN_MB_CODE_TX_INACTIVE,
518 		      &regs->mb[FLEXCAN_TX_BUF_RESERVED].can_ctrl);
519 
520 	return NETDEV_TX_OK;
521 }
522 
do_bus_err(struct net_device * dev,struct can_frame * cf,u32 reg_esr)523 static void do_bus_err(struct net_device *dev,
524 		       struct can_frame *cf, u32 reg_esr)
525 {
526 	struct flexcan_priv *priv = netdev_priv(dev);
527 	int rx_errors = 0, tx_errors = 0;
528 
529 	cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
530 
531 	if (reg_esr & FLEXCAN_ESR_BIT1_ERR) {
532 		netdev_dbg(dev, "BIT1_ERR irq\n");
533 		cf->data[2] |= CAN_ERR_PROT_BIT1;
534 		tx_errors = 1;
535 	}
536 	if (reg_esr & FLEXCAN_ESR_BIT0_ERR) {
537 		netdev_dbg(dev, "BIT0_ERR irq\n");
538 		cf->data[2] |= CAN_ERR_PROT_BIT0;
539 		tx_errors = 1;
540 	}
541 	if (reg_esr & FLEXCAN_ESR_ACK_ERR) {
542 		netdev_dbg(dev, "ACK_ERR irq\n");
543 		cf->can_id |= CAN_ERR_ACK;
544 		cf->data[3] = CAN_ERR_PROT_LOC_ACK;
545 		tx_errors = 1;
546 	}
547 	if (reg_esr & FLEXCAN_ESR_CRC_ERR) {
548 		netdev_dbg(dev, "CRC_ERR irq\n");
549 		cf->data[2] |= CAN_ERR_PROT_BIT;
550 		cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ;
551 		rx_errors = 1;
552 	}
553 	if (reg_esr & FLEXCAN_ESR_FRM_ERR) {
554 		netdev_dbg(dev, "FRM_ERR irq\n");
555 		cf->data[2] |= CAN_ERR_PROT_FORM;
556 		rx_errors = 1;
557 	}
558 	if (reg_esr & FLEXCAN_ESR_STF_ERR) {
559 		netdev_dbg(dev, "STF_ERR irq\n");
560 		cf->data[2] |= CAN_ERR_PROT_STUFF;
561 		rx_errors = 1;
562 	}
563 
564 	priv->can.can_stats.bus_error++;
565 	if (rx_errors)
566 		dev->stats.rx_errors++;
567 	if (tx_errors)
568 		dev->stats.tx_errors++;
569 }
570 
flexcan_poll_bus_err(struct net_device * dev,u32 reg_esr)571 static int flexcan_poll_bus_err(struct net_device *dev, u32 reg_esr)
572 {
573 	struct sk_buff *skb;
574 	struct can_frame *cf;
575 
576 	skb = alloc_can_err_skb(dev, &cf);
577 	if (unlikely(!skb))
578 		return 0;
579 
580 	do_bus_err(dev, cf, reg_esr);
581 
582 	dev->stats.rx_packets++;
583 	dev->stats.rx_bytes += cf->can_dlc;
584 	netif_receive_skb(skb);
585 
586 	return 1;
587 }
588 
flexcan_poll_state(struct net_device * dev,u32 reg_esr)589 static int flexcan_poll_state(struct net_device *dev, u32 reg_esr)
590 {
591 	struct flexcan_priv *priv = netdev_priv(dev);
592 	struct sk_buff *skb;
593 	struct can_frame *cf;
594 	enum can_state new_state = 0, rx_state = 0, tx_state = 0;
595 	int flt;
596 	struct can_berr_counter bec;
597 
598 	flt = reg_esr & FLEXCAN_ESR_FLT_CONF_MASK;
599 	if (likely(flt == FLEXCAN_ESR_FLT_CONF_ACTIVE)) {
600 		tx_state = unlikely(reg_esr & FLEXCAN_ESR_TX_WRN) ?
601 			CAN_STATE_ERROR_WARNING : CAN_STATE_ERROR_ACTIVE;
602 		rx_state = unlikely(reg_esr & FLEXCAN_ESR_RX_WRN) ?
603 			CAN_STATE_ERROR_WARNING : CAN_STATE_ERROR_ACTIVE;
604 		new_state = max(tx_state, rx_state);
605 	} else {
606 		__flexcan_get_berr_counter(dev, &bec);
607 		new_state = flt == FLEXCAN_ESR_FLT_CONF_PASSIVE ?
608 			CAN_STATE_ERROR_PASSIVE : CAN_STATE_BUS_OFF;
609 		rx_state = bec.rxerr >= bec.txerr ? new_state : 0;
610 		tx_state = bec.rxerr <= bec.txerr ? new_state : 0;
611 	}
612 
613 	/* state hasn't changed */
614 	if (likely(new_state == priv->can.state))
615 		return 0;
616 
617 	skb = alloc_can_err_skb(dev, &cf);
618 	if (unlikely(!skb))
619 		return 0;
620 
621 	can_change_state(dev, cf, tx_state, rx_state);
622 
623 	if (unlikely(new_state == CAN_STATE_BUS_OFF))
624 		can_bus_off(dev);
625 
626 	dev->stats.rx_packets++;
627 	dev->stats.rx_bytes += cf->can_dlc;
628 	netif_receive_skb(skb);
629 
630 	return 1;
631 }
632 
flexcan_read_fifo(const struct net_device * dev,struct can_frame * cf)633 static void flexcan_read_fifo(const struct net_device *dev,
634 			      struct can_frame *cf)
635 {
636 	const struct flexcan_priv *priv = netdev_priv(dev);
637 	struct flexcan_regs __iomem *regs = priv->regs;
638 	struct flexcan_mb __iomem *mb = &regs->mb[0];
639 	u32 reg_ctrl, reg_id;
640 
641 	reg_ctrl = flexcan_read(&mb->can_ctrl);
642 	reg_id = flexcan_read(&mb->can_id);
643 	if (reg_ctrl & FLEXCAN_MB_CNT_IDE)
644 		cf->can_id = ((reg_id >> 0) & CAN_EFF_MASK) | CAN_EFF_FLAG;
645 	else
646 		cf->can_id = (reg_id >> 18) & CAN_SFF_MASK;
647 
648 	if (reg_ctrl & FLEXCAN_MB_CNT_RTR)
649 		cf->can_id |= CAN_RTR_FLAG;
650 	cf->can_dlc = get_can_dlc((reg_ctrl >> 16) & 0xf);
651 
652 	*(__be32 *)(cf->data + 0) = cpu_to_be32(flexcan_read(&mb->data[0]));
653 	*(__be32 *)(cf->data + 4) = cpu_to_be32(flexcan_read(&mb->data[1]));
654 
655 	/* mark as read */
656 	flexcan_write(FLEXCAN_IFLAG_RX_FIFO_AVAILABLE, &regs->iflag1);
657 	flexcan_read(&regs->timer);
658 }
659 
flexcan_read_frame(struct net_device * dev)660 static int flexcan_read_frame(struct net_device *dev)
661 {
662 	struct net_device_stats *stats = &dev->stats;
663 	struct can_frame *cf;
664 	struct sk_buff *skb;
665 
666 	skb = alloc_can_skb(dev, &cf);
667 	if (unlikely(!skb)) {
668 		stats->rx_dropped++;
669 		return 0;
670 	}
671 
672 	flexcan_read_fifo(dev, cf);
673 
674 	stats->rx_packets++;
675 	stats->rx_bytes += cf->can_dlc;
676 	netif_receive_skb(skb);
677 
678 	can_led_event(dev, CAN_LED_EVENT_RX);
679 
680 	return 1;
681 }
682 
flexcan_poll(struct napi_struct * napi,int quota)683 static int flexcan_poll(struct napi_struct *napi, int quota)
684 {
685 	struct net_device *dev = napi->dev;
686 	const struct flexcan_priv *priv = netdev_priv(dev);
687 	struct flexcan_regs __iomem *regs = priv->regs;
688 	u32 reg_iflag1, reg_esr;
689 	int work_done = 0;
690 
691 	/* The error bits are cleared on read,
692 	 * use saved value from irq handler.
693 	 */
694 	reg_esr = flexcan_read(&regs->esr) | priv->reg_esr;
695 
696 	/* handle state changes */
697 	work_done += flexcan_poll_state(dev, reg_esr);
698 
699 	/* handle RX-FIFO */
700 	reg_iflag1 = flexcan_read(&regs->iflag1);
701 	while (reg_iflag1 & FLEXCAN_IFLAG_RX_FIFO_AVAILABLE &&
702 	       work_done < quota) {
703 		work_done += flexcan_read_frame(dev);
704 		reg_iflag1 = flexcan_read(&regs->iflag1);
705 	}
706 
707 	/* report bus errors */
708 	if (flexcan_has_and_handle_berr(priv, reg_esr) && work_done < quota)
709 		work_done += flexcan_poll_bus_err(dev, reg_esr);
710 
711 	if (work_done < quota) {
712 		napi_complete(napi);
713 		/* enable IRQs */
714 		flexcan_write(FLEXCAN_IFLAG_DEFAULT, &regs->imask1);
715 		flexcan_write(priv->reg_ctrl_default, &regs->ctrl);
716 	}
717 
718 	return work_done;
719 }
720 
flexcan_irq(int irq,void * dev_id)721 static irqreturn_t flexcan_irq(int irq, void *dev_id)
722 {
723 	struct net_device *dev = dev_id;
724 	struct net_device_stats *stats = &dev->stats;
725 	struct flexcan_priv *priv = netdev_priv(dev);
726 	struct flexcan_regs __iomem *regs = priv->regs;
727 	u32 reg_iflag1, reg_esr;
728 
729 	reg_iflag1 = flexcan_read(&regs->iflag1);
730 	reg_esr = flexcan_read(&regs->esr);
731 
732 	/* ACK all bus error and state change IRQ sources */
733 	if (reg_esr & FLEXCAN_ESR_ALL_INT)
734 		flexcan_write(reg_esr & FLEXCAN_ESR_ALL_INT, &regs->esr);
735 
736 	/* schedule NAPI in case of:
737 	 * - rx IRQ
738 	 * - state change IRQ
739 	 * - bus error IRQ and bus error reporting is activated
740 	 */
741 	if ((reg_iflag1 & FLEXCAN_IFLAG_RX_FIFO_AVAILABLE) ||
742 	    (reg_esr & FLEXCAN_ESR_ERR_STATE) ||
743 	    flexcan_has_and_handle_berr(priv, reg_esr)) {
744 		/* The error bits are cleared on read,
745 		 * save them for later use.
746 		 */
747 		priv->reg_esr = reg_esr & FLEXCAN_ESR_ERR_BUS;
748 		flexcan_write(FLEXCAN_IFLAG_DEFAULT &
749 			      ~FLEXCAN_IFLAG_RX_FIFO_AVAILABLE, &regs->imask1);
750 		flexcan_write(priv->reg_ctrl_default & ~FLEXCAN_CTRL_ERR_ALL,
751 			      &regs->ctrl);
752 		napi_schedule(&priv->napi);
753 	}
754 
755 	/* FIFO overflow */
756 	if (reg_iflag1 & FLEXCAN_IFLAG_RX_FIFO_OVERFLOW) {
757 		flexcan_write(FLEXCAN_IFLAG_RX_FIFO_OVERFLOW, &regs->iflag1);
758 		dev->stats.rx_over_errors++;
759 		dev->stats.rx_errors++;
760 	}
761 
762 	/* transmission complete interrupt */
763 	if (reg_iflag1 & (1 << FLEXCAN_TX_BUF_ID)) {
764 		stats->tx_bytes += can_get_echo_skb(dev, 0);
765 		stats->tx_packets++;
766 		can_led_event(dev, CAN_LED_EVENT_TX);
767 
768 		/* after sending a RTR frame MB is in RX mode */
769 		flexcan_write(FLEXCAN_MB_CODE_TX_INACTIVE,
770 			      &regs->mb[FLEXCAN_TX_BUF_ID].can_ctrl);
771 		flexcan_write((1 << FLEXCAN_TX_BUF_ID), &regs->iflag1);
772 		netif_wake_queue(dev);
773 	}
774 
775 	return IRQ_HANDLED;
776 }
777 
flexcan_set_bittiming(struct net_device * dev)778 static void flexcan_set_bittiming(struct net_device *dev)
779 {
780 	const struct flexcan_priv *priv = netdev_priv(dev);
781 	const struct can_bittiming *bt = &priv->can.bittiming;
782 	struct flexcan_regs __iomem *regs = priv->regs;
783 	u32 reg;
784 
785 	reg = flexcan_read(&regs->ctrl);
786 	reg &= ~(FLEXCAN_CTRL_PRESDIV(0xff) |
787 		 FLEXCAN_CTRL_RJW(0x3) |
788 		 FLEXCAN_CTRL_PSEG1(0x7) |
789 		 FLEXCAN_CTRL_PSEG2(0x7) |
790 		 FLEXCAN_CTRL_PROPSEG(0x7) |
791 		 FLEXCAN_CTRL_LPB |
792 		 FLEXCAN_CTRL_SMP |
793 		 FLEXCAN_CTRL_LOM);
794 
795 	reg |= FLEXCAN_CTRL_PRESDIV(bt->brp - 1) |
796 		FLEXCAN_CTRL_PSEG1(bt->phase_seg1 - 1) |
797 		FLEXCAN_CTRL_PSEG2(bt->phase_seg2 - 1) |
798 		FLEXCAN_CTRL_RJW(bt->sjw - 1) |
799 		FLEXCAN_CTRL_PROPSEG(bt->prop_seg - 1);
800 
801 	if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)
802 		reg |= FLEXCAN_CTRL_LPB;
803 	if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
804 		reg |= FLEXCAN_CTRL_LOM;
805 	if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
806 		reg |= FLEXCAN_CTRL_SMP;
807 
808 	netdev_dbg(dev, "writing ctrl=0x%08x\n", reg);
809 	flexcan_write(reg, &regs->ctrl);
810 
811 	/* print chip status */
812 	netdev_dbg(dev, "%s: mcr=0x%08x ctrl=0x%08x\n", __func__,
813 		   flexcan_read(&regs->mcr), flexcan_read(&regs->ctrl));
814 }
815 
816 /* flexcan_chip_start
817  *
818  * this functions is entered with clocks enabled
819  *
820  */
flexcan_chip_start(struct net_device * dev)821 static int flexcan_chip_start(struct net_device *dev)
822 {
823 	struct flexcan_priv *priv = netdev_priv(dev);
824 	struct flexcan_regs __iomem *regs = priv->regs;
825 	u32 reg_mcr, reg_ctrl, reg_ctrl2, reg_mecr;
826 	int err, i;
827 
828 	/* enable module */
829 	err = flexcan_chip_enable(priv);
830 	if (err)
831 		return err;
832 
833 	/* soft reset */
834 	err = flexcan_chip_softreset(priv);
835 	if (err)
836 		goto out_chip_disable;
837 
838 	flexcan_set_bittiming(dev);
839 
840 	/* MCR
841 	 *
842 	 * enable freeze
843 	 * enable fifo
844 	 * halt now
845 	 * only supervisor access
846 	 * enable warning int
847 	 * disable local echo
848 	 * choose format C
849 	 * set max mailbox number
850 	 */
851 	reg_mcr = flexcan_read(&regs->mcr);
852 	reg_mcr &= ~FLEXCAN_MCR_MAXMB(0xff);
853 	reg_mcr |= FLEXCAN_MCR_FRZ | FLEXCAN_MCR_FEN | FLEXCAN_MCR_HALT |
854 		FLEXCAN_MCR_SUPV | FLEXCAN_MCR_WRN_EN | FLEXCAN_MCR_SRX_DIS |
855 		FLEXCAN_MCR_IDAM_C | FLEXCAN_MCR_MAXMB(FLEXCAN_TX_BUF_ID);
856 	netdev_dbg(dev, "%s: writing mcr=0x%08x", __func__, reg_mcr);
857 	flexcan_write(reg_mcr, &regs->mcr);
858 
859 	/* CTRL
860 	 *
861 	 * disable timer sync feature
862 	 *
863 	 * disable auto busoff recovery
864 	 * transmit lowest buffer first
865 	 *
866 	 * enable tx and rx warning interrupt
867 	 * enable bus off interrupt
868 	 * (== FLEXCAN_CTRL_ERR_STATE)
869 	 */
870 	reg_ctrl = flexcan_read(&regs->ctrl);
871 	reg_ctrl &= ~FLEXCAN_CTRL_TSYN;
872 	reg_ctrl |= FLEXCAN_CTRL_BOFF_REC | FLEXCAN_CTRL_LBUF |
873 		FLEXCAN_CTRL_ERR_STATE;
874 
875 	/* enable the "error interrupt" (FLEXCAN_CTRL_ERR_MSK),
876 	 * on most Flexcan cores, too. Otherwise we don't get
877 	 * any error warning or passive interrupts.
878 	 */
879 	if (priv->devtype_data->quirks & FLEXCAN_QUIRK_BROKEN_ERR_STATE ||
880 	    priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING)
881 		reg_ctrl |= FLEXCAN_CTRL_ERR_MSK;
882 	else
883 		reg_ctrl &= ~FLEXCAN_CTRL_ERR_MSK;
884 
885 	/* save for later use */
886 	priv->reg_ctrl_default = reg_ctrl;
887 	/* leave interrupts disabled for now */
888 	reg_ctrl &= ~FLEXCAN_CTRL_ERR_ALL;
889 	netdev_dbg(dev, "%s: writing ctrl=0x%08x", __func__, reg_ctrl);
890 	flexcan_write(reg_ctrl, &regs->ctrl);
891 
892 	/* clear and invalidate all mailboxes first */
893 	for (i = FLEXCAN_TX_BUF_ID; i < ARRAY_SIZE(regs->mb); i++) {
894 		flexcan_write(FLEXCAN_MB_CODE_RX_INACTIVE,
895 			      &regs->mb[i].can_ctrl);
896 	}
897 
898 	/* Errata ERR005829: mark first TX mailbox as INACTIVE */
899 	flexcan_write(FLEXCAN_MB_CODE_TX_INACTIVE,
900 		      &regs->mb[FLEXCAN_TX_BUF_RESERVED].can_ctrl);
901 
902 	/* mark TX mailbox as INACTIVE */
903 	flexcan_write(FLEXCAN_MB_CODE_TX_INACTIVE,
904 		      &regs->mb[FLEXCAN_TX_BUF_ID].can_ctrl);
905 
906 	/* acceptance mask/acceptance code (accept everything) */
907 	flexcan_write(0x0, &regs->rxgmask);
908 	flexcan_write(0x0, &regs->rx14mask);
909 	flexcan_write(0x0, &regs->rx15mask);
910 
911 	if (priv->devtype_data->quirks & FLEXCAN_QUIRK_DISABLE_RXFG)
912 		flexcan_write(0x0, &regs->rxfgmask);
913 
914 	/* On Vybrid, disable memory error detection interrupts
915 	 * and freeze mode.
916 	 * This also works around errata e5295 which generates
917 	 * false positive memory errors and put the device in
918 	 * freeze mode.
919 	 */
920 	if (priv->devtype_data->quirks & FLEXCAN_QUIRK_DISABLE_MECR) {
921 		/* Follow the protocol as described in "Detection
922 		 * and Correction of Memory Errors" to write to
923 		 * MECR register
924 		 */
925 		reg_ctrl2 = flexcan_read(&regs->ctrl2);
926 		reg_ctrl2 |= FLEXCAN_CTRL2_ECRWRE;
927 		flexcan_write(reg_ctrl2, &regs->ctrl2);
928 
929 		reg_mecr = flexcan_read(&regs->mecr);
930 		reg_mecr &= ~FLEXCAN_MECR_ECRWRDIS;
931 		flexcan_write(reg_mecr, &regs->mecr);
932 		reg_mecr |= FLEXCAN_MECR_ECCDIS;
933 		reg_mecr &= ~(FLEXCAN_MECR_NCEFAFRZ | FLEXCAN_MECR_HANCEI_MSK |
934 			      FLEXCAN_MECR_FANCEI_MSK);
935 		flexcan_write(reg_mecr, &regs->mecr);
936 	}
937 
938 	err = flexcan_transceiver_enable(priv);
939 	if (err)
940 		goto out_chip_disable;
941 
942 	/* synchronize with the can bus */
943 	err = flexcan_chip_unfreeze(priv);
944 	if (err)
945 		goto out_transceiver_disable;
946 
947 	priv->can.state = CAN_STATE_ERROR_ACTIVE;
948 
949 	/* enable interrupts atomically */
950 	disable_irq(dev->irq);
951 	flexcan_write(priv->reg_ctrl_default, &regs->ctrl);
952 	flexcan_write(FLEXCAN_IFLAG_DEFAULT, &regs->imask1);
953 	enable_irq(dev->irq);
954 
955 	/* print chip status */
956 	netdev_dbg(dev, "%s: reading mcr=0x%08x ctrl=0x%08x\n", __func__,
957 		   flexcan_read(&regs->mcr), flexcan_read(&regs->ctrl));
958 
959 	return 0;
960 
961  out_transceiver_disable:
962 	flexcan_transceiver_disable(priv);
963  out_chip_disable:
964 	flexcan_chip_disable(priv);
965 	return err;
966 }
967 
968 /* flexcan_chip_stop
969  *
970  * this functions is entered with clocks enabled
971  */
flexcan_chip_stop(struct net_device * dev)972 static void flexcan_chip_stop(struct net_device *dev)
973 {
974 	struct flexcan_priv *priv = netdev_priv(dev);
975 	struct flexcan_regs __iomem *regs = priv->regs;
976 
977 	/* freeze + disable module */
978 	flexcan_chip_freeze(priv);
979 	flexcan_chip_disable(priv);
980 
981 	/* Disable all interrupts */
982 	flexcan_write(0, &regs->imask1);
983 	flexcan_write(priv->reg_ctrl_default & ~FLEXCAN_CTRL_ERR_ALL,
984 		      &regs->ctrl);
985 
986 	flexcan_transceiver_disable(priv);
987 	priv->can.state = CAN_STATE_STOPPED;
988 }
989 
flexcan_open(struct net_device * dev)990 static int flexcan_open(struct net_device *dev)
991 {
992 	struct flexcan_priv *priv = netdev_priv(dev);
993 	int err;
994 
995 	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;
1002 
1003 	err = open_candev(dev);
1004 	if (err)
1005 		goto out_disable_per;
1006 
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)
1014 		goto out_free_irq;
1015 
1016 	can_led_event(dev, CAN_LED_EVENT_OPEN);
1017 
1018 	napi_enable(&priv->napi);
1019 	netif_start_queue(dev);
1020 
1021 	return 0;
1022 
1023  out_free_irq:
1024 	free_irq(dev->irq, dev);
1025  out_close:
1026 	close_candev(dev);
1027  out_disable_per:
1028 	clk_disable_unprepare(priv->clk_per);
1029  out_disable_ipg:
1030 	clk_disable_unprepare(priv->clk_ipg);
1031 
1032 	return err;
1033 }
1034 
flexcan_close(struct net_device * dev)1035 static 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);
1044 	clk_disable_unprepare(priv->clk_per);
1045 	clk_disable_unprepare(priv->clk_ipg);
1046 
1047 	close_candev(dev);
1048 
1049 	can_led_event(dev, CAN_LED_EVENT_STOP);
1050 
1051 	return 0;
1052 }
1053 
flexcan_set_mode(struct net_device * dev,enum can_mode mode)1054 static 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 
1074 static 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,
1078 	.ndo_change_mtu = can_change_mtu,
1079 };
1080 
register_flexcandev(struct net_device * dev)1081 static int register_flexcandev(struct net_device *dev)
1082 {
1083 	struct flexcan_priv *priv = netdev_priv(dev);
1084 	struct flexcan_regs __iomem *regs = priv->regs;
1085 	u32 reg, err;
1086 
1087 	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;
1094 
1095 	/* select "bus clock", chip must be disabled */
1096 	err = flexcan_chip_disable(priv);
1097 	if (err)
1098 		goto out_disable_per;
1099 	reg = flexcan_read(&regs->ctrl);
1100 	reg |= FLEXCAN_CTRL_CLK_SRC;
1101 	flexcan_write(reg, &regs->ctrl);
1102 
1103 	err = flexcan_chip_enable(priv);
1104 	if (err)
1105 		goto out_chip_disable;
1106 
1107 	/* set freeze, halt */
1108 	err = flexcan_chip_freeze(priv);
1109 	if (err)
1110 		goto out_chip_disable;
1111 
1112 	/* activate FIFO, restrict register access */
1113 	reg = flexcan_read(&regs->mcr);
1114 	reg |=  FLEXCAN_MCR_FEN | FLEXCAN_MCR_SUPV;
1115 	flexcan_write(reg, &regs->mcr);
1116 
1117 	/* Currently we only support newer versions of this core
1118 	 * featuring a RX FIFO. Older cores found on some Coldfire
1119 	 * derivates are not yet supported.
1120 	 */
1121 	reg = flexcan_read(&regs->mcr);
1122 	if (!(reg & FLEXCAN_MCR_FEN)) {
1123 		netdev_err(dev, "Could not enable RX FIFO, unsupported core\n");
1124 		err = -ENODEV;
1125 		goto out_chip_disable;
1126 	}
1127 
1128 	err = register_candev(dev);
1129 
1130 	/* disable core and turn off clocks */
1131  out_chip_disable:
1132 	flexcan_chip_disable(priv);
1133  out_disable_per:
1134 	clk_disable_unprepare(priv->clk_per);
1135  out_disable_ipg:
1136 	clk_disable_unprepare(priv->clk_ipg);
1137 
1138 	return err;
1139 }
1140 
unregister_flexcandev(struct net_device * dev)1141 static void unregister_flexcandev(struct net_device *dev)
1142 {
1143 	unregister_candev(dev);
1144 }
1145 
1146 static const struct of_device_id flexcan_of_match[] = {
1147 	{ .compatible = "fsl,imx6q-flexcan", .data = &fsl_imx6q_devtype_data, },
1148 	{ .compatible = "fsl,imx28-flexcan", .data = &fsl_imx28_devtype_data, },
1149 	{ .compatible = "fsl,p1010-flexcan", .data = &fsl_p1010_devtype_data, },
1150 	{ .compatible = "fsl,vf610-flexcan", .data = &fsl_vf610_devtype_data, },
1151 	{ /* sentinel */ },
1152 };
1153 MODULE_DEVICE_TABLE(of, flexcan_of_match);
1154 
1155 static const struct platform_device_id flexcan_id_table[] = {
1156 	{ .name = "flexcan", .driver_data = (kernel_ulong_t)&fsl_p1010_devtype_data, },
1157 	{ /* sentinel */ },
1158 };
1159 MODULE_DEVICE_TABLE(platform, flexcan_id_table);
1160 
flexcan_probe(struct platform_device * pdev)1161 static int flexcan_probe(struct platform_device *pdev)
1162 {
1163 	const struct of_device_id *of_id;
1164 	const struct flexcan_devtype_data *devtype_data;
1165 	struct net_device *dev;
1166 	struct flexcan_priv *priv;
1167 	struct regulator *reg_xceiver;
1168 	struct resource *mem;
1169 	struct clk *clk_ipg = NULL, *clk_per = NULL;
1170 	struct flexcan_regs __iomem *regs;
1171 	int err, irq;
1172 	u32 clock_freq = 0;
1173 
1174 	reg_xceiver = devm_regulator_get(&pdev->dev, "xceiver");
1175 	if (PTR_ERR(reg_xceiver) == -EPROBE_DEFER)
1176 		return -EPROBE_DEFER;
1177 	else if (IS_ERR(reg_xceiver))
1178 		reg_xceiver = NULL;
1179 
1180 	if (pdev->dev.of_node)
1181 		of_property_read_u32(pdev->dev.of_node,
1182 				     "clock-frequency", &clock_freq);
1183 
1184 	if (!clock_freq) {
1185 		clk_ipg = devm_clk_get(&pdev->dev, "ipg");
1186 		if (IS_ERR(clk_ipg)) {
1187 			dev_err(&pdev->dev, "no ipg clock defined\n");
1188 			return PTR_ERR(clk_ipg);
1189 		}
1190 
1191 		clk_per = devm_clk_get(&pdev->dev, "per");
1192 		if (IS_ERR(clk_per)) {
1193 			dev_err(&pdev->dev, "no per clock defined\n");
1194 			return PTR_ERR(clk_per);
1195 		}
1196 		clock_freq = clk_get_rate(clk_per);
1197 	}
1198 
1199 	mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1200 	irq = platform_get_irq(pdev, 0);
1201 	if (irq <= 0)
1202 		return -ENODEV;
1203 
1204 	regs = devm_ioremap_resource(&pdev->dev, mem);
1205 	if (IS_ERR(regs))
1206 		return PTR_ERR(regs);
1207 
1208 	of_id = of_match_device(flexcan_of_match, &pdev->dev);
1209 	if (of_id) {
1210 		devtype_data = of_id->data;
1211 	} else if (platform_get_device_id(pdev)->driver_data) {
1212 		devtype_data = (struct flexcan_devtype_data *)
1213 			platform_get_device_id(pdev)->driver_data;
1214 	} else {
1215 		return -ENODEV;
1216 	}
1217 
1218 	dev = alloc_candev(sizeof(struct flexcan_priv), 1);
1219 	if (!dev)
1220 		return -ENOMEM;
1221 
1222 	dev->netdev_ops = &flexcan_netdev_ops;
1223 	dev->irq = irq;
1224 	dev->flags |= IFF_ECHO;
1225 
1226 	priv = netdev_priv(dev);
1227 	priv->can.clock.freq = clock_freq;
1228 	priv->can.bittiming_const = &flexcan_bittiming_const;
1229 	priv->can.do_set_mode = flexcan_set_mode;
1230 	priv->can.do_get_berr_counter = flexcan_get_berr_counter;
1231 	priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK |
1232 		CAN_CTRLMODE_LISTENONLY	| CAN_CTRLMODE_3_SAMPLES |
1233 		CAN_CTRLMODE_BERR_REPORTING;
1234 	priv->regs = regs;
1235 	priv->clk_ipg = clk_ipg;
1236 	priv->clk_per = clk_per;
1237 	priv->pdata = dev_get_platdata(&pdev->dev);
1238 	priv->devtype_data = devtype_data;
1239 	priv->reg_xceiver = reg_xceiver;
1240 
1241 	netif_napi_add(dev, &priv->napi, flexcan_poll, FLEXCAN_NAPI_WEIGHT);
1242 
1243 	platform_set_drvdata(pdev, dev);
1244 	SET_NETDEV_DEV(dev, &pdev->dev);
1245 
1246 	err = register_flexcandev(dev);
1247 	if (err) {
1248 		dev_err(&pdev->dev, "registering netdev failed\n");
1249 		goto failed_register;
1250 	}
1251 
1252 	devm_can_led_init(dev);
1253 
1254 	dev_info(&pdev->dev, "device registered (reg_base=%p, irq=%d)\n",
1255 		 priv->regs, dev->irq);
1256 
1257 	return 0;
1258 
1259  failed_register:
1260 	free_candev(dev);
1261 	return err;
1262 }
1263 
flexcan_remove(struct platform_device * pdev)1264 static int flexcan_remove(struct platform_device *pdev)
1265 {
1266 	struct net_device *dev = platform_get_drvdata(pdev);
1267 	struct flexcan_priv *priv = netdev_priv(dev);
1268 
1269 	unregister_flexcandev(dev);
1270 	netif_napi_del(&priv->napi);
1271 	free_candev(dev);
1272 
1273 	return 0;
1274 }
1275 
flexcan_suspend(struct device * device)1276 static int __maybe_unused flexcan_suspend(struct device *device)
1277 {
1278 	struct net_device *dev = dev_get_drvdata(device);
1279 	struct flexcan_priv *priv = netdev_priv(dev);
1280 	int err;
1281 
1282 	if (netif_running(dev)) {
1283 		err = flexcan_chip_disable(priv);
1284 		if (err)
1285 			return err;
1286 		netif_stop_queue(dev);
1287 		netif_device_detach(dev);
1288 	}
1289 	priv->can.state = CAN_STATE_SLEEPING;
1290 
1291 	return 0;
1292 }
1293 
flexcan_resume(struct device * device)1294 static int __maybe_unused flexcan_resume(struct device *device)
1295 {
1296 	struct net_device *dev = dev_get_drvdata(device);
1297 	struct flexcan_priv *priv = netdev_priv(dev);
1298 	int err;
1299 
1300 	priv->can.state = CAN_STATE_ERROR_ACTIVE;
1301 	if (netif_running(dev)) {
1302 		netif_device_attach(dev);
1303 		netif_start_queue(dev);
1304 		err = flexcan_chip_enable(priv);
1305 		if (err)
1306 			return err;
1307 	}
1308 	return 0;
1309 }
1310 
1311 static SIMPLE_DEV_PM_OPS(flexcan_pm_ops, flexcan_suspend, flexcan_resume);
1312 
1313 static struct platform_driver flexcan_driver = {
1314 	.driver = {
1315 		.name = DRV_NAME,
1316 		.pm = &flexcan_pm_ops,
1317 		.of_match_table = flexcan_of_match,
1318 	},
1319 	.probe = flexcan_probe,
1320 	.remove = flexcan_remove,
1321 	.id_table = flexcan_id_table,
1322 };
1323 
1324 module_platform_driver(flexcan_driver);
1325 
1326 MODULE_AUTHOR("Sascha Hauer <kernel@pengutronix.de>, "
1327 	      "Marc Kleine-Budde <kernel@pengutronix.de>");
1328 MODULE_LICENSE("GPL v2");
1329 MODULE_DESCRIPTION("CAN port driver for flexcan based chip");
1330