blob: 3a370d8437b0a7699d4b3be80395725e4b56c3e5 [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
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +02006 * Copyright (c) 2010-2017 Pengutronix, Marc Kleine-Budde <kernel@pengutronix.de>
7 * Copyright (c) 2014 David Jander, Protonic Holland
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02008 *
9 * Based on code originally by Andrey Volkov <avolkov@varma-el.com>
10 *
11 * LICENCE:
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation version 2.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 */
22
23#include <linux/netdevice.h>
24#include <linux/can.h>
25#include <linux/can/dev.h>
26#include <linux/can/error.h>
Fabio Baltieriadccadb2012-12-18 18:50:58 +010027#include <linux/can/led.h>
Marc Kleine-Budde30164752015-05-10 15:26:58 +020028#include <linux/can/rx-offload.h>
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +020029#include <linux/clk.h>
30#include <linux/delay.h>
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +020031#include <linux/interrupt.h>
32#include <linux/io.h>
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +020033#include <linux/module.h>
holt@sgi.com97efe9a2011-08-16 17:32:23 +000034#include <linux/of.h>
Hui Wang30c1e672012-06-28 16:21:35 +080035#include <linux/of_device.h>
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +020036#include <linux/platform_device.h>
Fabio Estevamb7c41142013-06-10 23:12:57 -030037#include <linux/regulator/consumer.h>
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +020038
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +020039#define DRV_NAME "flexcan"
40
41/* 8 for RX fifo and 2 error handling */
42#define FLEXCAN_NAPI_WEIGHT (8 + 2)
43
44/* FLEXCAN module configuration register (CANMCR) bits */
45#define FLEXCAN_MCR_MDIS BIT(31)
46#define FLEXCAN_MCR_FRZ BIT(30)
47#define FLEXCAN_MCR_FEN BIT(29)
48#define FLEXCAN_MCR_HALT BIT(28)
49#define FLEXCAN_MCR_NOT_RDY BIT(27)
50#define FLEXCAN_MCR_WAK_MSK BIT(26)
51#define FLEXCAN_MCR_SOFTRST BIT(25)
52#define FLEXCAN_MCR_FRZ_ACK BIT(24)
53#define FLEXCAN_MCR_SUPV BIT(23)
54#define FLEXCAN_MCR_SLF_WAK BIT(22)
55#define FLEXCAN_MCR_WRN_EN BIT(21)
56#define FLEXCAN_MCR_LPM_ACK BIT(20)
57#define FLEXCAN_MCR_WAK_SRC BIT(19)
58#define FLEXCAN_MCR_DOZE BIT(18)
59#define FLEXCAN_MCR_SRX_DIS BIT(17)
Marc Kleine-Budde62d10862015-08-27 16:01:27 +020060#define FLEXCAN_MCR_IRMQ BIT(16)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +020061#define FLEXCAN_MCR_LPRIO_EN BIT(13)
62#define FLEXCAN_MCR_AEN BIT(12)
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +020063/* MCR_MAXMB: maximum used MBs is MAXMB + 1 */
Marc Kleine-Budde4c728d82014-09-02 16:54:17 +020064#define FLEXCAN_MCR_MAXMB(x) ((x) & 0x7f)
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +020065#define FLEXCAN_MCR_IDAM_A (0x0 << 8)
66#define FLEXCAN_MCR_IDAM_B (0x1 << 8)
67#define FLEXCAN_MCR_IDAM_C (0x2 << 8)
68#define FLEXCAN_MCR_IDAM_D (0x3 << 8)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +020069
70/* FLEXCAN control register (CANCTRL) bits */
71#define FLEXCAN_CTRL_PRESDIV(x) (((x) & 0xff) << 24)
72#define FLEXCAN_CTRL_RJW(x) (((x) & 0x03) << 22)
73#define FLEXCAN_CTRL_PSEG1(x) (((x) & 0x07) << 19)
74#define FLEXCAN_CTRL_PSEG2(x) (((x) & 0x07) << 16)
75#define FLEXCAN_CTRL_BOFF_MSK BIT(15)
76#define FLEXCAN_CTRL_ERR_MSK BIT(14)
77#define FLEXCAN_CTRL_CLK_SRC BIT(13)
78#define FLEXCAN_CTRL_LPB BIT(12)
79#define FLEXCAN_CTRL_TWRN_MSK BIT(11)
80#define FLEXCAN_CTRL_RWRN_MSK BIT(10)
81#define FLEXCAN_CTRL_SMP BIT(7)
82#define FLEXCAN_CTRL_BOFF_REC BIT(6)
83#define FLEXCAN_CTRL_TSYN BIT(5)
84#define FLEXCAN_CTRL_LBUF BIT(4)
85#define FLEXCAN_CTRL_LOM BIT(3)
86#define FLEXCAN_CTRL_PROPSEG(x) ((x) & 0x07)
87#define FLEXCAN_CTRL_ERR_BUS (FLEXCAN_CTRL_ERR_MSK)
88#define FLEXCAN_CTRL_ERR_STATE \
89 (FLEXCAN_CTRL_TWRN_MSK | FLEXCAN_CTRL_RWRN_MSK | \
90 FLEXCAN_CTRL_BOFF_MSK)
91#define FLEXCAN_CTRL_ERR_ALL \
92 (FLEXCAN_CTRL_ERR_BUS | FLEXCAN_CTRL_ERR_STATE)
93
Stefan Agnercdce8442014-07-15 14:56:21 +020094/* FLEXCAN control register 2 (CTRL2) bits */
Marc Kleine-Budde6f75fce2014-09-23 11:03:01 +020095#define FLEXCAN_CTRL2_ECRWRE BIT(29)
96#define FLEXCAN_CTRL2_WRMFRZ BIT(28)
97#define FLEXCAN_CTRL2_RFFN(x) (((x) & 0x0f) << 24)
98#define FLEXCAN_CTRL2_TASD(x) (((x) & 0x1f) << 19)
99#define FLEXCAN_CTRL2_MRP BIT(18)
100#define FLEXCAN_CTRL2_RRS BIT(17)
101#define FLEXCAN_CTRL2_EACEN BIT(16)
Stefan Agnercdce8442014-07-15 14:56:21 +0200102
103/* FLEXCAN memory error control register (MECR) bits */
104#define FLEXCAN_MECR_ECRWRDIS BIT(31)
105#define FLEXCAN_MECR_HANCEI_MSK BIT(19)
106#define FLEXCAN_MECR_FANCEI_MSK BIT(18)
107#define FLEXCAN_MECR_CEI_MSK BIT(16)
108#define FLEXCAN_MECR_HAERRIE BIT(15)
109#define FLEXCAN_MECR_FAERRIE BIT(14)
110#define FLEXCAN_MECR_EXTERRIE BIT(13)
111#define FLEXCAN_MECR_RERRDIS BIT(9)
112#define FLEXCAN_MECR_ECCDIS BIT(8)
113#define FLEXCAN_MECR_NCEFAFRZ BIT(7)
114
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200115/* FLEXCAN error and status register (ESR) bits */
116#define FLEXCAN_ESR_TWRN_INT BIT(17)
117#define FLEXCAN_ESR_RWRN_INT BIT(16)
118#define FLEXCAN_ESR_BIT1_ERR BIT(15)
119#define FLEXCAN_ESR_BIT0_ERR BIT(14)
120#define FLEXCAN_ESR_ACK_ERR BIT(13)
121#define FLEXCAN_ESR_CRC_ERR BIT(12)
122#define FLEXCAN_ESR_FRM_ERR BIT(11)
123#define FLEXCAN_ESR_STF_ERR BIT(10)
124#define FLEXCAN_ESR_TX_WRN BIT(9)
125#define FLEXCAN_ESR_RX_WRN BIT(8)
126#define FLEXCAN_ESR_IDLE BIT(7)
127#define FLEXCAN_ESR_TXRX BIT(6)
128#define FLEXCAN_EST_FLT_CONF_SHIFT (4)
129#define FLEXCAN_ESR_FLT_CONF_MASK (0x3 << FLEXCAN_EST_FLT_CONF_SHIFT)
130#define FLEXCAN_ESR_FLT_CONF_ACTIVE (0x0 << FLEXCAN_EST_FLT_CONF_SHIFT)
131#define FLEXCAN_ESR_FLT_CONF_PASSIVE (0x1 << FLEXCAN_EST_FLT_CONF_SHIFT)
132#define FLEXCAN_ESR_BOFF_INT BIT(2)
133#define FLEXCAN_ESR_ERR_INT BIT(1)
134#define FLEXCAN_ESR_WAK_INT BIT(0)
135#define FLEXCAN_ESR_ERR_BUS \
136 (FLEXCAN_ESR_BIT1_ERR | FLEXCAN_ESR_BIT0_ERR | \
137 FLEXCAN_ESR_ACK_ERR | FLEXCAN_ESR_CRC_ERR | \
138 FLEXCAN_ESR_FRM_ERR | FLEXCAN_ESR_STF_ERR)
139#define FLEXCAN_ESR_ERR_STATE \
140 (FLEXCAN_ESR_TWRN_INT | FLEXCAN_ESR_RWRN_INT | FLEXCAN_ESR_BOFF_INT)
141#define FLEXCAN_ESR_ERR_ALL \
142 (FLEXCAN_ESR_ERR_BUS | FLEXCAN_ESR_ERR_STATE)
Wolfgang Grandegger6e9d5542011-12-12 16:09:28 +0100143#define FLEXCAN_ESR_ALL_INT \
144 (FLEXCAN_ESR_TWRN_INT | FLEXCAN_ESR_RWRN_INT | \
145 FLEXCAN_ESR_BOFF_INT | FLEXCAN_ESR_ERR_INT)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200146
147/* FLEXCAN interrupt flag register (IFLAG) bits */
David Jander25e92442014-09-03 16:47:22 +0200148/* Errata ERR005829 step7: Reserve first valid MB */
Marc Kleine-Buddeb93917c2015-07-12 00:47:47 +0200149#define FLEXCAN_TX_MB_RESERVED_OFF_FIFO 8
150#define FLEXCAN_TX_MB_OFF_FIFO 9
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200151#define FLEXCAN_TX_MB_RESERVED_OFF_TIMESTAMP 0
152#define FLEXCAN_TX_MB_OFF_TIMESTAMP 1
153#define FLEXCAN_RX_MB_OFF_TIMESTAMP_FIRST (FLEXCAN_TX_MB_OFF_TIMESTAMP + 1)
154#define FLEXCAN_RX_MB_OFF_TIMESTAMP_LAST 63
Marc Kleine-Buddeb93917c2015-07-12 00:47:47 +0200155#define FLEXCAN_IFLAG_MB(x) BIT(x)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200156#define FLEXCAN_IFLAG_RX_FIFO_OVERFLOW BIT(7)
157#define FLEXCAN_IFLAG_RX_FIFO_WARN BIT(6)
158#define FLEXCAN_IFLAG_RX_FIFO_AVAILABLE BIT(5)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200159
160/* FLEXCAN message buffers */
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200161#define FLEXCAN_MB_CODE_MASK (0xf << 24)
162#define FLEXCAN_MB_CODE_RX_BUSY_BIT (0x1 << 24)
Marc Kleine-Buddec32fe4a2014-09-16 12:39:28 +0200163#define FLEXCAN_MB_CODE_RX_INACTIVE (0x0 << 24)
164#define FLEXCAN_MB_CODE_RX_EMPTY (0x4 << 24)
165#define FLEXCAN_MB_CODE_RX_FULL (0x2 << 24)
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200166#define FLEXCAN_MB_CODE_RX_OVERRUN (0x6 << 24)
Marc Kleine-Buddec32fe4a2014-09-16 12:39:28 +0200167#define FLEXCAN_MB_CODE_RX_RANSWER (0xa << 24)
168
169#define FLEXCAN_MB_CODE_TX_INACTIVE (0x8 << 24)
170#define FLEXCAN_MB_CODE_TX_ABORT (0x9 << 24)
171#define FLEXCAN_MB_CODE_TX_DATA (0xc << 24)
172#define FLEXCAN_MB_CODE_TX_TANSWER (0xe << 24)
173
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200174#define FLEXCAN_MB_CNT_SRR BIT(22)
175#define FLEXCAN_MB_CNT_IDE BIT(21)
176#define FLEXCAN_MB_CNT_RTR BIT(20)
177#define FLEXCAN_MB_CNT_LENGTH(x) (((x) & 0xf) << 16)
178#define FLEXCAN_MB_CNT_TIMESTAMP(x) ((x) & 0xffff)
179
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200180#define FLEXCAN_TIMEOUT_US (50)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200181
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200182/* FLEXCAN hardware feature flags
Wolfgang Grandeggerbb698ca2012-10-10 21:10:42 +0200183 *
184 * Below is some version info we got:
ZHU Yi (ST-FIR/ENG1-Zhu)da49a802017-09-15 07:03:58 +0000185 * SOC Version IP-Version Glitch- [TR]WRN_INT IRQ Err Memory err RTR re-
186 * Filter? connected? Passive detection ception in MB
187 * MX25 FlexCAN2 03.00.00.00 no no ? no no
188 * MX28 FlexCAN2 03.00.04.00 yes yes no no no
189 * MX35 FlexCAN2 03.00.00.00 no no ? no no
190 * MX53 FlexCAN2 03.00.00.00 yes no no no no
191 * MX6s FlexCAN3 10.00.12.00 yes yes no no yes
192 * VF610 FlexCAN3 ? no yes ? yes yes?
Pankaj Bansal99b76682017-11-24 18:52:09 +0530193 * LS1021A FlexCAN2 03.00.04.00 no yes no no yes
Wolfgang Grandeggerbb698ca2012-10-10 21:10:42 +0200194 *
195 * Some SOCs do not have the RX_WARN & TX_WARN interrupt line connected.
196 */
ZHU Yi (ST-FIR/ENG1-Zhu)2f8639b2017-09-15 07:01:23 +0000197#define FLEXCAN_QUIRK_BROKEN_WERR_STATE BIT(1) /* [TR]WRN_INT not connected */
Marc Kleine-Buddef377bff2015-05-08 15:22:36 +0200198#define FLEXCAN_QUIRK_DISABLE_RXFG BIT(2) /* Disable RX FIFO Global mask */
Marc Kleine-Budde9eb7aa82015-09-01 08:57:55 +0200199#define FLEXCAN_QUIRK_ENABLE_EACEN_RRS BIT(3) /* Enable EACEN and RRS bit in ctrl2 */
Marc Kleine-Budde66ddb822017-03-02 15:42:49 +0100200#define FLEXCAN_QUIRK_DISABLE_MECR BIT(4) /* Disable Memory error detection */
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200201#define FLEXCAN_QUIRK_USE_OFF_TIMESTAMP BIT(5) /* Use timestamp based offloading */
ZHU Yi (ST-FIR/ENG1-Zhu)da49a802017-09-15 07:03:58 +0000202#define FLEXCAN_QUIRK_BROKEN_PERR_STATE BIT(6) /* No interrupt for error passive */
Wolfgang Grandegger4f72e5f2012-09-28 03:17:15 +0000203
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200204/* Structure of the message buffer */
205struct flexcan_mb {
206 u32 can_ctrl;
207 u32 can_id;
208 u32 data[2];
209};
210
211/* Structure of the hardware registers */
212struct flexcan_regs {
213 u32 mcr; /* 0x00 */
214 u32 ctrl; /* 0x04 */
215 u32 timer; /* 0x08 */
216 u32 _reserved1; /* 0x0c */
217 u32 rxgmask; /* 0x10 */
218 u32 rx14mask; /* 0x14 */
219 u32 rx15mask; /* 0x18 */
220 u32 ecr; /* 0x1c */
221 u32 esr; /* 0x20 */
222 u32 imask2; /* 0x24 */
223 u32 imask1; /* 0x28 */
224 u32 iflag2; /* 0x2c */
225 u32 iflag1; /* 0x30 */
Marc Kleine-Budde62d10862015-08-27 16:01:27 +0200226 union { /* 0x34 */
227 u32 gfwr_mx28; /* MX28, MX53 */
228 u32 ctrl2; /* MX6, VF610 */
229 };
Hui Wang30c1e672012-06-28 16:21:35 +0800230 u32 esr2; /* 0x38 */
231 u32 imeur; /* 0x3c */
232 u32 lrfr; /* 0x40 */
233 u32 crcr; /* 0x44 */
234 u32 rxfgmask; /* 0x48 */
235 u32 rxfir; /* 0x4c */
Stefan Agnercdce8442014-07-15 14:56:21 +0200236 u32 _reserved3[12]; /* 0x50 */
Marc Kleine-Budde1ba763d2015-08-25 10:39:19 +0200237 struct flexcan_mb mb[64]; /* 0x80 */
Marc Kleine-Budde66a6ef02014-09-17 12:50:48 +0200238 /* FIFO-mode:
239 * MB
240 * 0x080...0x08f 0 RX message buffer
241 * 0x090...0x0df 1-5 reserverd
242 * 0x0e0...0x0ff 6-7 8 entry ID table
243 * (mx25, mx28, mx35, mx53)
244 * 0x0e0...0x2df 6-7..37 8..128 entry ID table
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200245 * size conf'ed via ctrl2::RFFN
Marc Kleine-Budde66a6ef02014-09-17 12:50:48 +0200246 * (mx6, vf610)
247 */
Marc Kleine-Budde62d10862015-08-27 16:01:27 +0200248 u32 _reserved4[256]; /* 0x480 */
249 u32 rximr[64]; /* 0x880 */
250 u32 _reserved5[24]; /* 0x980 */
251 u32 gfwr_mx6; /* 0x9e0 - MX6 */
252 u32 _reserved6[63]; /* 0x9e4 */
Stefan Agnercdce8442014-07-15 14:56:21 +0200253 u32 mecr; /* 0xae0 */
254 u32 erriar; /* 0xae4 */
255 u32 erridpr; /* 0xae8 */
256 u32 errippr; /* 0xaec */
257 u32 rerrar; /* 0xaf0 */
258 u32 rerrdr; /* 0xaf4 */
259 u32 rerrsynr; /* 0xaf8 */
260 u32 errsr; /* 0xafc */
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200261};
262
Hui Wang30c1e672012-06-28 16:21:35 +0800263struct flexcan_devtype_data {
Marc Kleine-Buddef377bff2015-05-08 15:22:36 +0200264 u32 quirks; /* quirks needed for different IP cores */
Hui Wang30c1e672012-06-28 16:21:35 +0800265};
266
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200267struct flexcan_priv {
268 struct can_priv can;
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200269 struct can_rx_offload offload;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200270
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200271 struct flexcan_regs __iomem *regs;
Marc Kleine-Buddeb93917c2015-07-12 00:47:47 +0200272 struct flexcan_mb __iomem *tx_mb;
273 struct flexcan_mb __iomem *tx_mb_reserved;
274 u8 tx_mb_idx;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200275 u32 reg_ctrl_default;
Marc Kleine-Budde28ac7dc2015-08-04 13:46:10 +0200276 u32 reg_imask1_default;
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200277 u32 reg_imask2_default;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200278
Steffen Trumtrar3d42a372012-07-17 16:14:34 +0200279 struct clk *clk_ipg;
280 struct clk *clk_per;
Marc Kleine-Buddedda0b3b2012-07-13 14:52:48 +0200281 const struct flexcan_devtype_data *devtype_data;
Fabio Estevamb7c41142013-06-10 23:12:57 -0300282 struct regulator *reg_xceiver;
Pankaj Bansal88462d22017-11-24 18:52:08 +0530283
284 /* Read and Write APIs */
285 u32 (*read)(void __iomem *addr);
286 void (*write)(u32 val, void __iomem *addr);
Hui Wang30c1e672012-06-28 16:21:35 +0800287};
288
Marc Kleine-Buddea3c11a72016-07-04 14:45:44 +0200289static const struct flexcan_devtype_data fsl_p1010_devtype_data = {
ZHU Yi (ST-FIR/ENG1-Zhu)fb5b91d62017-09-15 07:09:37 +0000290 .quirks = FLEXCAN_QUIRK_BROKEN_WERR_STATE |
291 FLEXCAN_QUIRK_BROKEN_PERR_STATE,
Hui Wang30c1e672012-06-28 16:21:35 +0800292};
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200293
ZHU Yi (ST-FIR/ENG1-Zhu)083c5572017-09-15 07:08:23 +0000294static const struct flexcan_devtype_data fsl_imx28_devtype_data = {
295 .quirks = FLEXCAN_QUIRK_BROKEN_PERR_STATE,
296};
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200297
Marc Kleine-Buddea3c11a72016-07-04 14:45:44 +0200298static const struct flexcan_devtype_data fsl_imx6q_devtype_data = {
Marc Kleine-Budde096de072015-09-01 10:28:46 +0200299 .quirks = FLEXCAN_QUIRK_DISABLE_RXFG | FLEXCAN_QUIRK_ENABLE_EACEN_RRS |
ZHU Yi (ST-FIR/ENG1-Zhu)cf9c0462017-09-15 07:05:50 +0000300 FLEXCAN_QUIRK_USE_OFF_TIMESTAMP | FLEXCAN_QUIRK_BROKEN_PERR_STATE,
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200301};
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200302
Marc Kleine-Buddea3c11a72016-07-04 14:45:44 +0200303static const struct flexcan_devtype_data fsl_vf610_devtype_data = {
Marc Kleine-Budde9eb7aa82015-09-01 08:57:55 +0200304 .quirks = FLEXCAN_QUIRK_DISABLE_RXFG | FLEXCAN_QUIRK_ENABLE_EACEN_RRS |
Marc Kleine-Budde096de072015-09-01 10:28:46 +0200305 FLEXCAN_QUIRK_DISABLE_MECR | FLEXCAN_QUIRK_USE_OFF_TIMESTAMP,
Stefan Agnercdce8442014-07-15 14:56:21 +0200306};
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200307
Pankaj Bansal99b76682017-11-24 18:52:09 +0530308static const struct flexcan_devtype_data fsl_ls1021a_r2_devtype_data = {
309 .quirks = FLEXCAN_QUIRK_DISABLE_RXFG | FLEXCAN_QUIRK_ENABLE_EACEN_RRS |
310 FLEXCAN_QUIRK_DISABLE_MECR | FLEXCAN_QUIRK_BROKEN_PERR_STATE |
311 FLEXCAN_QUIRK_USE_OFF_TIMESTAMP,
312};
313
Marc Kleine-Budde194b9a42012-07-16 12:58:31 +0200314static const struct can_bittiming_const flexcan_bittiming_const = {
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200315 .name = DRV_NAME,
316 .tseg1_min = 4,
317 .tseg1_max = 16,
318 .tseg2_min = 2,
319 .tseg2_max = 8,
320 .sjw_max = 4,
321 .brp_min = 1,
322 .brp_max = 256,
323 .brp_inc = 1,
324};
325
Pankaj Bansal88462d22017-11-24 18:52:08 +0530326/* FlexCAN module is essentially modelled as a little-endian IP in most
327 * SoCs, i.e the registers as well as the message buffer areas are
328 * implemented in a little-endian fashion.
329 *
330 * However there are some SoCs (e.g. LS1021A) which implement the FlexCAN
331 * module in a big-endian fashion (i.e the registers as well as the
332 * message buffer areas are implemented in a big-endian way).
333 *
334 * In addition, the FlexCAN module can be found on SoCs having ARM or
335 * PPC cores. So, we need to abstract off the register read/write
336 * functions, ensuring that these cater to all the combinations of module
337 * endianness and underlying CPU endianness.
holt@sgi.com61e271e2011-08-16 17:32:20 +0000338 */
Pankaj Bansal88462d22017-11-24 18:52:08 +0530339static inline u32 flexcan_read_be(void __iomem *addr)
holt@sgi.com61e271e2011-08-16 17:32:20 +0000340{
Pankaj Bansal88462d22017-11-24 18:52:08 +0530341 return ioread32be(addr);
holt@sgi.com61e271e2011-08-16 17:32:20 +0000342}
343
Pankaj Bansal88462d22017-11-24 18:52:08 +0530344static inline void flexcan_write_be(u32 val, void __iomem *addr)
holt@sgi.com61e271e2011-08-16 17:32:20 +0000345{
Pankaj Bansal88462d22017-11-24 18:52:08 +0530346 iowrite32be(val, addr);
holt@sgi.com61e271e2011-08-16 17:32:20 +0000347}
348
Pankaj Bansal88462d22017-11-24 18:52:08 +0530349static inline u32 flexcan_read_le(void __iomem *addr)
holt@sgi.com61e271e2011-08-16 17:32:20 +0000350{
Pankaj Bansal88462d22017-11-24 18:52:08 +0530351 return ioread32(addr);
holt@sgi.com61e271e2011-08-16 17:32:20 +0000352}
Pankaj Bansal88462d22017-11-24 18:52:08 +0530353
354static inline void flexcan_write_le(u32 val, void __iomem *addr)
355{
356 iowrite32(val, addr);
357}
holt@sgi.com61e271e2011-08-16 17:32:20 +0000358
ZHU Yi (ST-FIR/ENG1-Zhu)da49a802017-09-15 07:03:58 +0000359static inline void flexcan_error_irq_enable(const struct flexcan_priv *priv)
360{
361 struct flexcan_regs __iomem *regs = priv->regs;
362 u32 reg_ctrl = (priv->reg_ctrl_default | FLEXCAN_CTRL_ERR_MSK);
363
Pankaj Bansal88462d22017-11-24 18:52:08 +0530364 priv->write(reg_ctrl, &regs->ctrl);
ZHU Yi (ST-FIR/ENG1-Zhu)da49a802017-09-15 07:03:58 +0000365}
366
367static inline void flexcan_error_irq_disable(const struct flexcan_priv *priv)
368{
369 struct flexcan_regs __iomem *regs = priv->regs;
370 u32 reg_ctrl = (priv->reg_ctrl_default & ~FLEXCAN_CTRL_ERR_MSK);
371
Pankaj Bansal88462d22017-11-24 18:52:08 +0530372 priv->write(reg_ctrl, &regs->ctrl);
ZHU Yi (ST-FIR/ENG1-Zhu)da49a802017-09-15 07:03:58 +0000373}
374
Marc Kleine-Buddef0036982014-02-28 17:18:27 +0100375static inline int flexcan_transceiver_enable(const struct flexcan_priv *priv)
376{
377 if (!priv->reg_xceiver)
378 return 0;
379
380 return regulator_enable(priv->reg_xceiver);
381}
382
383static inline int flexcan_transceiver_disable(const struct flexcan_priv *priv)
384{
385 if (!priv->reg_xceiver)
386 return 0;
387
388 return regulator_disable(priv->reg_xceiver);
389}
390
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100391static int flexcan_chip_enable(struct flexcan_priv *priv)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200392{
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200393 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100394 unsigned int timeout = FLEXCAN_TIMEOUT_US / 10;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200395 u32 reg;
396
Pankaj Bansal88462d22017-11-24 18:52:08 +0530397 reg = priv->read(&regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200398 reg &= ~FLEXCAN_MCR_MDIS;
Pankaj Bansal88462d22017-11-24 18:52:08 +0530399 priv->write(reg, &regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200400
Pankaj Bansal88462d22017-11-24 18:52:08 +0530401 while (timeout-- && (priv->read(&regs->mcr) & FLEXCAN_MCR_LPM_ACK))
David Jander8badd652014-08-27 12:02:16 +0200402 udelay(10);
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100403
Pankaj Bansal88462d22017-11-24 18:52:08 +0530404 if (priv->read(&regs->mcr) & FLEXCAN_MCR_LPM_ACK)
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100405 return -ETIMEDOUT;
406
407 return 0;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200408}
409
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100410static int flexcan_chip_disable(struct flexcan_priv *priv)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200411{
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200412 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100413 unsigned int timeout = FLEXCAN_TIMEOUT_US / 10;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200414 u32 reg;
415
Pankaj Bansal88462d22017-11-24 18:52:08 +0530416 reg = priv->read(&regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200417 reg |= FLEXCAN_MCR_MDIS;
Pankaj Bansal88462d22017-11-24 18:52:08 +0530418 priv->write(reg, &regs->mcr);
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100419
Pankaj Bansal88462d22017-11-24 18:52:08 +0530420 while (timeout-- && !(priv->read(&regs->mcr) & FLEXCAN_MCR_LPM_ACK))
David Jander8badd652014-08-27 12:02:16 +0200421 udelay(10);
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100422
Pankaj Bansal88462d22017-11-24 18:52:08 +0530423 if (!(priv->read(&regs->mcr) & FLEXCAN_MCR_LPM_ACK))
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100424 return -ETIMEDOUT;
425
426 return 0;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200427}
428
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100429static int flexcan_chip_freeze(struct flexcan_priv *priv)
430{
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200431 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100432 unsigned int timeout = 1000 * 1000 * 10 / priv->can.bittiming.bitrate;
433 u32 reg;
434
Pankaj Bansal88462d22017-11-24 18:52:08 +0530435 reg = priv->read(&regs->mcr);
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100436 reg |= FLEXCAN_MCR_HALT;
Pankaj Bansal88462d22017-11-24 18:52:08 +0530437 priv->write(reg, &regs->mcr);
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100438
Pankaj Bansal88462d22017-11-24 18:52:08 +0530439 while (timeout-- && !(priv->read(&regs->mcr) & FLEXCAN_MCR_FRZ_ACK))
David Jander8badd652014-08-27 12:02:16 +0200440 udelay(100);
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100441
Pankaj Bansal88462d22017-11-24 18:52:08 +0530442 if (!(priv->read(&regs->mcr) & FLEXCAN_MCR_FRZ_ACK))
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100443 return -ETIMEDOUT;
444
445 return 0;
446}
447
448static int flexcan_chip_unfreeze(struct flexcan_priv *priv)
449{
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200450 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100451 unsigned int timeout = FLEXCAN_TIMEOUT_US / 10;
452 u32 reg;
453
Pankaj Bansal88462d22017-11-24 18:52:08 +0530454 reg = priv->read(&regs->mcr);
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100455 reg &= ~FLEXCAN_MCR_HALT;
Pankaj Bansal88462d22017-11-24 18:52:08 +0530456 priv->write(reg, &regs->mcr);
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100457
Pankaj Bansal88462d22017-11-24 18:52:08 +0530458 while (timeout-- && (priv->read(&regs->mcr) & FLEXCAN_MCR_FRZ_ACK))
David Jander8badd652014-08-27 12:02:16 +0200459 udelay(10);
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100460
Pankaj Bansal88462d22017-11-24 18:52:08 +0530461 if (priv->read(&regs->mcr) & FLEXCAN_MCR_FRZ_ACK)
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100462 return -ETIMEDOUT;
463
464 return 0;
465}
466
Marc Kleine-Budde4b5b8222014-02-28 15:16:59 +0100467static int flexcan_chip_softreset(struct flexcan_priv *priv)
468{
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200469 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Budde4b5b8222014-02-28 15:16:59 +0100470 unsigned int timeout = FLEXCAN_TIMEOUT_US / 10;
471
Pankaj Bansal88462d22017-11-24 18:52:08 +0530472 priv->write(FLEXCAN_MCR_SOFTRST, &regs->mcr);
473 while (timeout-- && (priv->read(&regs->mcr) & FLEXCAN_MCR_SOFTRST))
David Jander8badd652014-08-27 12:02:16 +0200474 udelay(10);
Marc Kleine-Budde4b5b8222014-02-28 15:16:59 +0100475
Pankaj Bansal88462d22017-11-24 18:52:08 +0530476 if (priv->read(&regs->mcr) & FLEXCAN_MCR_SOFTRST)
Marc Kleine-Budde4b5b8222014-02-28 15:16:59 +0100477 return -ETIMEDOUT;
478
479 return 0;
480}
481
Stefan Agnerec56acf2014-07-15 14:56:20 +0200482static int __flexcan_get_berr_counter(const struct net_device *dev,
483 struct can_berr_counter *bec)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200484{
485 const struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200486 struct flexcan_regs __iomem *regs = priv->regs;
Pankaj Bansal88462d22017-11-24 18:52:08 +0530487 u32 reg = priv->read(&regs->ecr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200488
489 bec->txerr = (reg >> 0) & 0xff;
490 bec->rxerr = (reg >> 8) & 0xff;
491
492 return 0;
493}
494
Stefan Agnerec56acf2014-07-15 14:56:20 +0200495static int flexcan_get_berr_counter(const struct net_device *dev,
496 struct can_berr_counter *bec)
497{
498 const struct flexcan_priv *priv = netdev_priv(dev);
499 int err;
500
501 err = clk_prepare_enable(priv->clk_ipg);
502 if (err)
503 return err;
504
505 err = clk_prepare_enable(priv->clk_per);
506 if (err)
507 goto out_disable_ipg;
508
509 err = __flexcan_get_berr_counter(dev, bec);
510
511 clk_disable_unprepare(priv->clk_per);
512 out_disable_ipg:
513 clk_disable_unprepare(priv->clk_ipg);
514
515 return err;
516}
517
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200518static int flexcan_start_xmit(struct sk_buff *skb, struct net_device *dev)
519{
520 const struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200521 struct can_frame *cf = (struct can_frame *)skb->data;
522 u32 can_id;
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200523 u32 data;
Marc Kleine-Budde10d089b2014-09-23 11:18:11 +0200524 u32 ctrl = FLEXCAN_MB_CODE_TX_DATA | (cf->can_dlc << 16);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200525
526 if (can_dropped_invalid_skb(dev, skb))
527 return NETDEV_TX_OK;
528
529 netif_stop_queue(dev);
530
531 if (cf->can_id & CAN_EFF_FLAG) {
532 can_id = cf->can_id & CAN_EFF_MASK;
533 ctrl |= FLEXCAN_MB_CNT_IDE | FLEXCAN_MB_CNT_SRR;
534 } else {
535 can_id = (cf->can_id & CAN_SFF_MASK) << 18;
536 }
537
538 if (cf->can_id & CAN_RTR_FLAG)
539 ctrl |= FLEXCAN_MB_CNT_RTR;
540
541 if (cf->can_dlc > 0) {
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200542 data = be32_to_cpup((__be32 *)&cf->data[0]);
Pankaj Bansal88462d22017-11-24 18:52:08 +0530543 priv->write(data, &priv->tx_mb->data[0]);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200544 }
545 if (cf->can_dlc > 3) {
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200546 data = be32_to_cpup((__be32 *)&cf->data[4]);
Pankaj Bansal88462d22017-11-24 18:52:08 +0530547 priv->write(data, &priv->tx_mb->data[1]);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200548 }
549
Reuben Dowle9a123492011-11-01 11:18:03 +1300550 can_put_echo_skb(skb, dev, 0);
551
Pankaj Bansal88462d22017-11-24 18:52:08 +0530552 priv->write(can_id, &priv->tx_mb->can_id);
553 priv->write(ctrl, &priv->tx_mb->can_ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200554
David Jander25e92442014-09-03 16:47:22 +0200555 /* Errata ERR005829 step8:
556 * Write twice INACTIVE(0x8) code to first MB.
557 */
Pankaj Bansal88462d22017-11-24 18:52:08 +0530558 priv->write(FLEXCAN_MB_CODE_TX_INACTIVE,
Marc Kleine-Buddeb93917c2015-07-12 00:47:47 +0200559 &priv->tx_mb_reserved->can_ctrl);
Pankaj Bansal88462d22017-11-24 18:52:08 +0530560 priv->write(FLEXCAN_MB_CODE_TX_INACTIVE,
Marc Kleine-Buddeb93917c2015-07-12 00:47:47 +0200561 &priv->tx_mb_reserved->can_ctrl);
David Jander25e92442014-09-03 16:47:22 +0200562
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200563 return NETDEV_TX_OK;
564}
565
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200566static void flexcan_irq_bus_err(struct net_device *dev, u32 reg_esr)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200567{
568 struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Buddea5c02f662017-01-18 11:38:26 +0100569 struct sk_buff *skb;
570 struct can_frame *cf;
Marc Kleine-Budded166f562017-01-17 17:33:46 +0100571 bool rx_errors = false, tx_errors = false;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200572
Marc Kleine-Buddea5c02f662017-01-18 11:38:26 +0100573 skb = alloc_can_err_skb(dev, &cf);
574 if (unlikely(!skb))
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200575 return;
Marc Kleine-Buddea5c02f662017-01-18 11:38:26 +0100576
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200577 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
578
579 if (reg_esr & FLEXCAN_ESR_BIT1_ERR) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100580 netdev_dbg(dev, "BIT1_ERR irq\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200581 cf->data[2] |= CAN_ERR_PROT_BIT1;
Marc Kleine-Budded166f562017-01-17 17:33:46 +0100582 tx_errors = true;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200583 }
584 if (reg_esr & FLEXCAN_ESR_BIT0_ERR) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100585 netdev_dbg(dev, "BIT0_ERR irq\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200586 cf->data[2] |= CAN_ERR_PROT_BIT0;
Marc Kleine-Budded166f562017-01-17 17:33:46 +0100587 tx_errors = true;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200588 }
589 if (reg_esr & FLEXCAN_ESR_ACK_ERR) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100590 netdev_dbg(dev, "ACK_ERR irq\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200591 cf->can_id |= CAN_ERR_ACK;
Oliver Hartkoppffd461f2015-11-21 18:41:20 +0100592 cf->data[3] = CAN_ERR_PROT_LOC_ACK;
Marc Kleine-Budded166f562017-01-17 17:33:46 +0100593 tx_errors = true;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200594 }
595 if (reg_esr & FLEXCAN_ESR_CRC_ERR) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100596 netdev_dbg(dev, "CRC_ERR irq\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200597 cf->data[2] |= CAN_ERR_PROT_BIT;
Oliver Hartkoppffd461f2015-11-21 18:41:20 +0100598 cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ;
Marc Kleine-Budded166f562017-01-17 17:33:46 +0100599 rx_errors = true;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200600 }
601 if (reg_esr & FLEXCAN_ESR_FRM_ERR) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100602 netdev_dbg(dev, "FRM_ERR irq\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200603 cf->data[2] |= CAN_ERR_PROT_FORM;
Marc Kleine-Budded166f562017-01-17 17:33:46 +0100604 rx_errors = true;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200605 }
606 if (reg_esr & FLEXCAN_ESR_STF_ERR) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100607 netdev_dbg(dev, "STF_ERR irq\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200608 cf->data[2] |= CAN_ERR_PROT_STUFF;
Marc Kleine-Budded166f562017-01-17 17:33:46 +0100609 rx_errors = true;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200610 }
611
612 priv->can.can_stats.bus_error++;
613 if (rx_errors)
614 dev->stats.rx_errors++;
615 if (tx_errors)
616 dev->stats.tx_errors++;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200617
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200618 can_rx_offload_irq_queue_err_skb(&priv->offload, skb);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200619}
620
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200621static void flexcan_irq_state(struct net_device *dev, u32 reg_esr)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200622{
623 struct flexcan_priv *priv = netdev_priv(dev);
624 struct sk_buff *skb;
625 struct can_frame *cf;
Marc Kleine-Budde238443d2017-01-18 11:25:41 +0100626 enum can_state new_state, rx_state, tx_state;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200627 int flt;
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000628 struct can_berr_counter bec;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200629
630 flt = reg_esr & FLEXCAN_ESR_FLT_CONF_MASK;
631 if (likely(flt == FLEXCAN_ESR_FLT_CONF_ACTIVE)) {
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000632 tx_state = unlikely(reg_esr & FLEXCAN_ESR_TX_WRN) ?
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200633 CAN_STATE_ERROR_WARNING : CAN_STATE_ERROR_ACTIVE;
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000634 rx_state = unlikely(reg_esr & FLEXCAN_ESR_RX_WRN) ?
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200635 CAN_STATE_ERROR_WARNING : CAN_STATE_ERROR_ACTIVE;
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000636 new_state = max(tx_state, rx_state);
Andri Yngvason258ce802015-03-17 13:03:09 +0000637 } else {
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000638 __flexcan_get_berr_counter(dev, &bec);
Andri Yngvason258ce802015-03-17 13:03:09 +0000639 new_state = flt == FLEXCAN_ESR_FLT_CONF_PASSIVE ?
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200640 CAN_STATE_ERROR_PASSIVE : CAN_STATE_BUS_OFF;
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000641 rx_state = bec.rxerr >= bec.txerr ? new_state : 0;
642 tx_state = bec.rxerr <= bec.txerr ? new_state : 0;
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000643 }
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200644
645 /* state hasn't changed */
646 if (likely(new_state == priv->can.state))
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200647 return;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200648
649 skb = alloc_can_err_skb(dev, &cf);
650 if (unlikely(!skb))
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200651 return;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200652
Andri Yngvason71a3aed2014-12-03 17:54:15 +0000653 can_change_state(dev, cf, tx_state, rx_state);
654
655 if (unlikely(new_state == CAN_STATE_BUS_OFF))
656 can_bus_off(dev);
657
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200658 can_rx_offload_irq_queue_err_skb(&priv->offload, skb);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200659}
660
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200661static inline struct flexcan_priv *rx_offload_to_priv(struct can_rx_offload *offload)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200662{
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200663 return container_of(offload, struct flexcan_priv, offload);
664}
665
666static unsigned int flexcan_mailbox_read(struct can_rx_offload *offload,
667 struct can_frame *cf,
668 u32 *timestamp, unsigned int n)
669{
670 struct flexcan_priv *priv = rx_offload_to_priv(offload);
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200671 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200672 struct flexcan_mb __iomem *mb = &regs->mb[n];
673 u32 reg_ctrl, reg_id, reg_iflag1;
674
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200675 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP) {
676 u32 code;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200677
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200678 do {
Pankaj Bansal88462d22017-11-24 18:52:08 +0530679 reg_ctrl = priv->read(&mb->can_ctrl);
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200680 } while (reg_ctrl & FLEXCAN_MB_CODE_RX_BUSY_BIT);
681
682 /* is this MB empty? */
683 code = reg_ctrl & FLEXCAN_MB_CODE_MASK;
684 if ((code != FLEXCAN_MB_CODE_RX_FULL) &&
685 (code != FLEXCAN_MB_CODE_RX_OVERRUN))
686 return 0;
687
688 if (code == FLEXCAN_MB_CODE_RX_OVERRUN) {
689 /* This MB was overrun, we lost data */
690 offload->dev->stats.rx_over_errors++;
691 offload->dev->stats.rx_errors++;
692 }
693 } else {
Pankaj Bansal88462d22017-11-24 18:52:08 +0530694 reg_iflag1 = priv->read(&regs->iflag1);
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200695 if (!(reg_iflag1 & FLEXCAN_IFLAG_RX_FIFO_AVAILABLE))
696 return 0;
697
Pankaj Bansal88462d22017-11-24 18:52:08 +0530698 reg_ctrl = priv->read(&mb->can_ctrl);
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200699 }
700
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200701 /* increase timstamp to full 32 bit */
702 *timestamp = reg_ctrl << 16;
703
Pankaj Bansal88462d22017-11-24 18:52:08 +0530704 reg_id = priv->read(&mb->can_id);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200705 if (reg_ctrl & FLEXCAN_MB_CNT_IDE)
706 cf->can_id = ((reg_id >> 0) & CAN_EFF_MASK) | CAN_EFF_FLAG;
707 else
708 cf->can_id = (reg_id >> 18) & CAN_SFF_MASK;
709
710 if (reg_ctrl & FLEXCAN_MB_CNT_RTR)
711 cf->can_id |= CAN_RTR_FLAG;
712 cf->can_dlc = get_can_dlc((reg_ctrl >> 16) & 0xf);
713
Pankaj Bansal88462d22017-11-24 18:52:08 +0530714 *(__be32 *)(cf->data + 0) = cpu_to_be32(priv->read(&mb->data[0]));
715 *(__be32 *)(cf->data + 4) = cpu_to_be32(priv->read(&mb->data[1]));
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200716
717 /* mark as read */
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200718 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP) {
719 /* Clear IRQ */
720 if (n < 32)
Pankaj Bansal88462d22017-11-24 18:52:08 +0530721 priv->write(BIT(n), &regs->iflag1);
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200722 else
Pankaj Bansal88462d22017-11-24 18:52:08 +0530723 priv->write(BIT(n - 32), &regs->iflag2);
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200724 } else {
Pankaj Bansal88462d22017-11-24 18:52:08 +0530725 priv->write(FLEXCAN_IFLAG_RX_FIFO_AVAILABLE, &regs->iflag1);
726 priv->read(&regs->timer);
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200727 }
Fabio Baltieriadccadb2012-12-18 18:50:58 +0100728
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200729 return 1;
730}
731
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200732
733static inline u64 flexcan_read_reg_iflag_rx(struct flexcan_priv *priv)
734{
735 struct flexcan_regs __iomem *regs = priv->regs;
736 u32 iflag1, iflag2;
737
Pankaj Bansal88462d22017-11-24 18:52:08 +0530738 iflag2 = priv->read(&regs->iflag2) & priv->reg_imask2_default;
739 iflag1 = priv->read(&regs->iflag1) & priv->reg_imask1_default &
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200740 ~FLEXCAN_IFLAG_MB(priv->tx_mb_idx);
741
742 return (u64)iflag2 << 32 | iflag1;
743}
744
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200745static irqreturn_t flexcan_irq(int irq, void *dev_id)
746{
747 struct net_device *dev = dev_id;
748 struct net_device_stats *stats = &dev->stats;
749 struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200750 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddedd2f1222017-01-18 11:45:14 +0100751 irqreturn_t handled = IRQ_NONE;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200752 u32 reg_iflag1, reg_esr;
ZHU Yi (ST-FIR/ENG1-Zhu)da49a802017-09-15 07:03:58 +0000753 enum can_state last_state = priv->can.state;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200754
Pankaj Bansal88462d22017-11-24 18:52:08 +0530755 reg_iflag1 = priv->read(&regs->iflag1);
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200756
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200757 /* reception interrupt */
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200758 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP) {
759 u64 reg_iflag;
760 int ret;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200761
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200762 while ((reg_iflag = flexcan_read_reg_iflag_rx(priv))) {
763 handled = IRQ_HANDLED;
764 ret = can_rx_offload_irq_offload_timestamp(&priv->offload,
765 reg_iflag);
766 if (!ret)
767 break;
768 }
769 } else {
770 if (reg_iflag1 & FLEXCAN_IFLAG_RX_FIFO_AVAILABLE) {
771 handled = IRQ_HANDLED;
772 can_rx_offload_irq_offload_fifo(&priv->offload);
773 }
774
775 /* FIFO overflow interrupt */
776 if (reg_iflag1 & FLEXCAN_IFLAG_RX_FIFO_OVERFLOW) {
777 handled = IRQ_HANDLED;
Pankaj Bansal88462d22017-11-24 18:52:08 +0530778 priv->write(FLEXCAN_IFLAG_RX_FIFO_OVERFLOW,
779 &regs->iflag1);
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200780 dev->stats.rx_over_errors++;
781 dev->stats.rx_errors++;
782 }
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200783 }
784
785 /* transmission complete interrupt */
Marc Kleine-Buddeb93917c2015-07-12 00:47:47 +0200786 if (reg_iflag1 & FLEXCAN_IFLAG_MB(priv->tx_mb_idx)) {
Marc Kleine-Buddedd2f1222017-01-18 11:45:14 +0100787 handled = IRQ_HANDLED;
Reuben Dowle9a123492011-11-01 11:18:03 +1300788 stats->tx_bytes += can_get_echo_skb(dev, 0);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200789 stats->tx_packets++;
Fabio Baltieriadccadb2012-12-18 18:50:58 +0100790 can_led_event(dev, CAN_LED_EVENT_TX);
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200791
792 /* after sending a RTR frame MB is in RX mode */
Pankaj Bansal88462d22017-11-24 18:52:08 +0530793 priv->write(FLEXCAN_MB_CODE_TX_INACTIVE,
794 &priv->tx_mb->can_ctrl);
795 priv->write(FLEXCAN_IFLAG_MB(priv->tx_mb_idx), &regs->iflag1);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200796 netif_wake_queue(dev);
797 }
798
Pankaj Bansal88462d22017-11-24 18:52:08 +0530799 reg_esr = priv->read(&regs->esr);
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200800
Marc Kleine-Buddedd2f1222017-01-18 11:45:14 +0100801 /* ACK all bus error and state change IRQ sources */
802 if (reg_esr & FLEXCAN_ESR_ALL_INT) {
803 handled = IRQ_HANDLED;
Pankaj Bansal88462d22017-11-24 18:52:08 +0530804 priv->write(reg_esr & FLEXCAN_ESR_ALL_INT, &regs->esr);
Marc Kleine-Buddedd2f1222017-01-18 11:45:14 +0100805 }
806
ZHU Yi (ST-FIR/ENG1-Zhu)ad230232017-09-15 06:59:15 +0000807 /* state change interrupt or broken error state quirk fix is enabled */
808 if ((reg_esr & FLEXCAN_ESR_ERR_STATE) ||
ZHU Yi (ST-FIR/ENG1-Zhu)da49a802017-09-15 07:03:58 +0000809 (priv->devtype_data->quirks & (FLEXCAN_QUIRK_BROKEN_WERR_STATE |
810 FLEXCAN_QUIRK_BROKEN_PERR_STATE)))
Marc Kleine-Budde30164752015-05-10 15:26:58 +0200811 flexcan_irq_state(dev, reg_esr);
812
813 /* bus error IRQ - handle if bus error reporting is activated */
814 if ((reg_esr & FLEXCAN_ESR_ERR_BUS) &&
815 (priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING))
816 flexcan_irq_bus_err(dev, reg_esr);
817
ZHU Yi (ST-FIR/ENG1-Zhu)da49a802017-09-15 07:03:58 +0000818 /* availability of error interrupt among state transitions in case
819 * bus error reporting is de-activated and
820 * FLEXCAN_QUIRK_BROKEN_PERR_STATE is enabled:
821 * +--------------------------------------------------------------+
822 * | +----------------------------------------------+ [stopped / |
823 * | | | sleeping] -+
824 * +-+-> active <-> warning <-> passive -> bus off -+
825 * ___________^^^^^^^^^^^^_______________________________
826 * disabled(1) enabled disabled
827 *
828 * (1): enabled if FLEXCAN_QUIRK_BROKEN_WERR_STATE is enabled
829 */
830 if ((last_state != priv->can.state) &&
831 (priv->devtype_data->quirks & FLEXCAN_QUIRK_BROKEN_PERR_STATE) &&
832 !(priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING)) {
833 switch (priv->can.state) {
834 case CAN_STATE_ERROR_ACTIVE:
835 if (priv->devtype_data->quirks &
836 FLEXCAN_QUIRK_BROKEN_WERR_STATE)
837 flexcan_error_irq_enable(priv);
838 else
839 flexcan_error_irq_disable(priv);
840 break;
841
842 case CAN_STATE_ERROR_WARNING:
843 flexcan_error_irq_enable(priv);
844 break;
845
846 case CAN_STATE_ERROR_PASSIVE:
847 case CAN_STATE_BUS_OFF:
848 flexcan_error_irq_disable(priv);
849 break;
850
851 default:
852 break;
853 }
854 }
855
Marc Kleine-Buddedd2f1222017-01-18 11:45:14 +0100856 return handled;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200857}
858
859static void flexcan_set_bittiming(struct net_device *dev)
860{
861 const struct flexcan_priv *priv = netdev_priv(dev);
862 const struct can_bittiming *bt = &priv->can.bittiming;
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200863 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200864 u32 reg;
865
Pankaj Bansal88462d22017-11-24 18:52:08 +0530866 reg = priv->read(&regs->ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200867 reg &= ~(FLEXCAN_CTRL_PRESDIV(0xff) |
868 FLEXCAN_CTRL_RJW(0x3) |
869 FLEXCAN_CTRL_PSEG1(0x7) |
870 FLEXCAN_CTRL_PSEG2(0x7) |
871 FLEXCAN_CTRL_PROPSEG(0x7) |
872 FLEXCAN_CTRL_LPB |
873 FLEXCAN_CTRL_SMP |
874 FLEXCAN_CTRL_LOM);
875
876 reg |= FLEXCAN_CTRL_PRESDIV(bt->brp - 1) |
877 FLEXCAN_CTRL_PSEG1(bt->phase_seg1 - 1) |
878 FLEXCAN_CTRL_PSEG2(bt->phase_seg2 - 1) |
879 FLEXCAN_CTRL_RJW(bt->sjw - 1) |
880 FLEXCAN_CTRL_PROPSEG(bt->prop_seg - 1);
881
882 if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)
883 reg |= FLEXCAN_CTRL_LPB;
884 if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
885 reg |= FLEXCAN_CTRL_LOM;
886 if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
887 reg |= FLEXCAN_CTRL_SMP;
888
Lucas Stach7a4b6c82015-08-07 17:16:03 +0200889 netdev_dbg(dev, "writing ctrl=0x%08x\n", reg);
Pankaj Bansal88462d22017-11-24 18:52:08 +0530890 priv->write(reg, &regs->ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200891
892 /* print chip status */
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100893 netdev_dbg(dev, "%s: mcr=0x%08x ctrl=0x%08x\n", __func__,
Pankaj Bansal88462d22017-11-24 18:52:08 +0530894 priv->read(&regs->mcr), priv->read(&regs->ctrl));
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200895}
896
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200897/* flexcan_chip_start
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200898 *
899 * this functions is entered with clocks enabled
900 *
901 */
902static int flexcan_chip_start(struct net_device *dev)
903{
904 struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Budde89af8742015-05-08 09:32:58 +0200905 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Budde6f75fce2014-09-23 11:03:01 +0200906 u32 reg_mcr, reg_ctrl, reg_ctrl2, reg_mecr;
David S. Miller1f6d8032014-09-23 12:09:27 -0400907 int err, i;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200908
909 /* enable module */
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +0100910 err = flexcan_chip_enable(priv);
911 if (err)
912 return err;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200913
914 /* soft reset */
Marc Kleine-Budde4b5b8222014-02-28 15:16:59 +0100915 err = flexcan_chip_softreset(priv);
916 if (err)
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +0100917 goto out_chip_disable;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200918
919 flexcan_set_bittiming(dev);
920
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200921 /* MCR
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200922 *
923 * enable freeze
924 * enable fifo
925 * halt now
926 * only supervisor access
927 * enable warning int
Reuben Dowle9a123492011-11-01 11:18:03 +1300928 * disable local echo
Marc Kleine-Budde4bd888a2015-08-31 21:03:29 +0200929 * enable individual RX masking
Marc Kleine-Budde749de6f2015-08-31 21:32:34 +0200930 * choose format C
931 * set max mailbox number
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200932 */
Pankaj Bansal88462d22017-11-24 18:52:08 +0530933 reg_mcr = priv->read(&regs->mcr);
Marc Kleine-Budded5a7b402013-10-04 10:52:36 +0200934 reg_mcr &= ~FLEXCAN_MCR_MAXMB(0xff);
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200935 reg_mcr |= FLEXCAN_MCR_FRZ | FLEXCAN_MCR_HALT | FLEXCAN_MCR_SUPV |
936 FLEXCAN_MCR_WRN_EN | FLEXCAN_MCR_SRX_DIS | FLEXCAN_MCR_IRMQ |
937 FLEXCAN_MCR_IDAM_C;
938
939 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP) {
940 reg_mcr &= ~FLEXCAN_MCR_FEN;
941 reg_mcr |= FLEXCAN_MCR_MAXMB(priv->offload.mb_last);
942 } else {
943 reg_mcr |= FLEXCAN_MCR_FEN |
944 FLEXCAN_MCR_MAXMB(priv->tx_mb_idx);
945 }
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100946 netdev_dbg(dev, "%s: writing mcr=0x%08x", __func__, reg_mcr);
Pankaj Bansal88462d22017-11-24 18:52:08 +0530947 priv->write(reg_mcr, &regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200948
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200949 /* CTRL
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200950 *
951 * disable timer sync feature
952 *
953 * disable auto busoff recovery
954 * transmit lowest buffer first
955 *
956 * enable tx and rx warning interrupt
957 * enable bus off interrupt
958 * (== FLEXCAN_CTRL_ERR_STATE)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200959 */
Pankaj Bansal88462d22017-11-24 18:52:08 +0530960 reg_ctrl = priv->read(&regs->ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200961 reg_ctrl &= ~FLEXCAN_CTRL_TSYN;
962 reg_ctrl |= FLEXCAN_CTRL_BOFF_REC | FLEXCAN_CTRL_LBUF |
Wolfgang Grandegger4f72e5f2012-09-28 03:17:15 +0000963 FLEXCAN_CTRL_ERR_STATE;
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +0200964
965 /* enable the "error interrupt" (FLEXCAN_CTRL_ERR_MSK),
Wolfgang Grandegger4f72e5f2012-09-28 03:17:15 +0000966 * on most Flexcan cores, too. Otherwise we don't get
967 * any error warning or passive interrupts.
968 */
ZHU Yi (ST-FIR/ENG1-Zhu)2f8639b2017-09-15 07:01:23 +0000969 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_BROKEN_WERR_STATE ||
Wolfgang Grandegger4f72e5f2012-09-28 03:17:15 +0000970 priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING)
971 reg_ctrl |= FLEXCAN_CTRL_ERR_MSK;
Alexander Steinbc03a542014-08-12 10:47:21 +0200972 else
973 reg_ctrl &= ~FLEXCAN_CTRL_ERR_MSK;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200974
975 /* save for later use */
976 priv->reg_ctrl_default = reg_ctrl;
Marc Kleine-Budde6fa7da22015-08-27 14:24:48 +0200977 /* leave interrupts disabled for now */
978 reg_ctrl &= ~FLEXCAN_CTRL_ERR_ALL;
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100979 netdev_dbg(dev, "%s: writing ctrl=0x%08x", __func__, reg_ctrl);
Pankaj Bansal88462d22017-11-24 18:52:08 +0530980 priv->write(reg_ctrl, &regs->ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +0200981
Marc Kleine-Budde9eb7aa82015-09-01 08:57:55 +0200982 if ((priv->devtype_data->quirks & FLEXCAN_QUIRK_ENABLE_EACEN_RRS)) {
Pankaj Bansal88462d22017-11-24 18:52:08 +0530983 reg_ctrl2 = priv->read(&regs->ctrl2);
Marc Kleine-Budde9eb7aa82015-09-01 08:57:55 +0200984 reg_ctrl2 |= FLEXCAN_CTRL2_EACEN | FLEXCAN_CTRL2_RRS;
Pankaj Bansal88462d22017-11-24 18:52:08 +0530985 priv->write(reg_ctrl2, &regs->ctrl2);
Marc Kleine-Budde9eb7aa82015-09-01 08:57:55 +0200986 }
987
David Janderfc05b882014-08-27 11:58:05 +0200988 /* clear and invalidate all mailboxes first */
Marc Kleine-Buddeb93917c2015-07-12 00:47:47 +0200989 for (i = priv->tx_mb_idx; i < ARRAY_SIZE(regs->mb); i++) {
Pankaj Bansal88462d22017-11-24 18:52:08 +0530990 priv->write(FLEXCAN_MB_CODE_RX_INACTIVE,
991 &regs->mb[i].can_ctrl);
David Janderfc05b882014-08-27 11:58:05 +0200992 }
993
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200994 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP) {
995 for (i = priv->offload.mb_first; i <= priv->offload.mb_last; i++)
Pankaj Bansal88462d22017-11-24 18:52:08 +0530996 priv->write(FLEXCAN_MB_CODE_RX_EMPTY,
997 &regs->mb[i].can_ctrl);
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +0200998 }
999
David Jander25e92442014-09-03 16:47:22 +02001000 /* Errata ERR005829: mark first TX mailbox as INACTIVE */
Pankaj Bansal88462d22017-11-24 18:52:08 +05301001 priv->write(FLEXCAN_MB_CODE_TX_INACTIVE,
1002 &priv->tx_mb_reserved->can_ctrl);
David Jander25e92442014-09-03 16:47:22 +02001003
Marc Kleine-Buddec32fe4a2014-09-16 12:39:28 +02001004 /* mark TX mailbox as INACTIVE */
Pankaj Bansal88462d22017-11-24 18:52:08 +05301005 priv->write(FLEXCAN_MB_CODE_TX_INACTIVE,
1006 &priv->tx_mb->can_ctrl);
Marc Kleine-Budded5a7b402013-10-04 10:52:36 +02001007
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001008 /* acceptance mask/acceptance code (accept everything) */
Pankaj Bansal88462d22017-11-24 18:52:08 +05301009 priv->write(0x0, &regs->rxgmask);
1010 priv->write(0x0, &regs->rx14mask);
1011 priv->write(0x0, &regs->rx15mask);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001012
Marc Kleine-Buddef377bff2015-05-08 15:22:36 +02001013 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_DISABLE_RXFG)
Pankaj Bansal88462d22017-11-24 18:52:08 +05301014 priv->write(0x0, &regs->rxfgmask);
Hui Wang30c1e672012-06-28 16:21:35 +08001015
Marc Kleine-Budde4bd888a2015-08-31 21:03:29 +02001016 /* clear acceptance filters */
1017 for (i = 0; i < ARRAY_SIZE(regs->mb); i++)
Pankaj Bansal88462d22017-11-24 18:52:08 +05301018 priv->write(0, &regs->rximr[i]);
Marc Kleine-Budde4bd888a2015-08-31 21:03:29 +02001019
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +02001020 /* On Vybrid, disable memory error detection interrupts
Stefan Agnercdce8442014-07-15 14:56:21 +02001021 * and freeze mode.
1022 * This also works around errata e5295 which generates
1023 * false positive memory errors and put the device in
1024 * freeze mode.
1025 */
Marc Kleine-Buddef377bff2015-05-08 15:22:36 +02001026 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_DISABLE_MECR) {
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +02001027 /* Follow the protocol as described in "Detection
Stefan Agnercdce8442014-07-15 14:56:21 +02001028 * and Correction of Memory Errors" to write to
1029 * MECR register
1030 */
Pankaj Bansal88462d22017-11-24 18:52:08 +05301031 reg_ctrl2 = priv->read(&regs->ctrl2);
Marc Kleine-Budde6f75fce2014-09-23 11:03:01 +02001032 reg_ctrl2 |= FLEXCAN_CTRL2_ECRWRE;
Pankaj Bansal88462d22017-11-24 18:52:08 +05301033 priv->write(reg_ctrl2, &regs->ctrl2);
Stefan Agnercdce8442014-07-15 14:56:21 +02001034
Pankaj Bansal88462d22017-11-24 18:52:08 +05301035 reg_mecr = priv->read(&regs->mecr);
Stefan Agnercdce8442014-07-15 14:56:21 +02001036 reg_mecr &= ~FLEXCAN_MECR_ECRWRDIS;
Pankaj Bansal88462d22017-11-24 18:52:08 +05301037 priv->write(reg_mecr, &regs->mecr);
Stefan Agnercdce8442014-07-15 14:56:21 +02001038 reg_mecr &= ~(FLEXCAN_MECR_NCEFAFRZ | FLEXCAN_MECR_HANCEI_MSK |
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +02001039 FLEXCAN_MECR_FANCEI_MSK);
Pankaj Bansal88462d22017-11-24 18:52:08 +05301040 priv->write(reg_mecr, &regs->mecr);
Stefan Agnercdce8442014-07-15 14:56:21 +02001041 }
1042
Marc Kleine-Buddef0036982014-02-28 17:18:27 +01001043 err = flexcan_transceiver_enable(priv);
1044 if (err)
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +01001045 goto out_chip_disable;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001046
1047 /* synchronize with the can bus */
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +01001048 err = flexcan_chip_unfreeze(priv);
1049 if (err)
1050 goto out_transceiver_disable;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001051
1052 priv->can.state = CAN_STATE_ERROR_ACTIVE;
1053
Marc Kleine-Budde6fa7da22015-08-27 14:24:48 +02001054 /* enable interrupts atomically */
1055 disable_irq(dev->irq);
Pankaj Bansal88462d22017-11-24 18:52:08 +05301056 priv->write(priv->reg_ctrl_default, &regs->ctrl);
1057 priv->write(priv->reg_imask1_default, &regs->imask1);
1058 priv->write(priv->reg_imask2_default, &regs->imask2);
Marc Kleine-Budde6fa7da22015-08-27 14:24:48 +02001059 enable_irq(dev->irq);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001060
1061 /* print chip status */
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +01001062 netdev_dbg(dev, "%s: reading mcr=0x%08x ctrl=0x%08x\n", __func__,
Pankaj Bansal88462d22017-11-24 18:52:08 +05301063 priv->read(&regs->mcr), priv->read(&regs->ctrl));
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001064
1065 return 0;
1066
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +01001067 out_transceiver_disable:
1068 flexcan_transceiver_disable(priv);
1069 out_chip_disable:
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001070 flexcan_chip_disable(priv);
1071 return err;
1072}
1073
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +02001074/* flexcan_chip_stop
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001075 *
1076 * this functions is entered with clocks enabled
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001077 */
1078static void flexcan_chip_stop(struct net_device *dev)
1079{
1080 struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Budde89af8742015-05-08 09:32:58 +02001081 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001082
Marc Kleine-Buddeb1aa1c72014-02-28 17:08:21 +01001083 /* freeze + disable module */
1084 flexcan_chip_freeze(priv);
1085 flexcan_chip_disable(priv);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001086
Marc Kleine-Budde5be93bd2014-02-19 12:00:51 +01001087 /* Disable all interrupts */
Pankaj Bansal88462d22017-11-24 18:52:08 +05301088 priv->write(0, &regs->imask2);
1089 priv->write(0, &regs->imask1);
1090 priv->write(priv->reg_ctrl_default & ~FLEXCAN_CTRL_ERR_ALL,
1091 &regs->ctrl);
Marc Kleine-Budde5be93bd2014-02-19 12:00:51 +01001092
Marc Kleine-Buddef0036982014-02-28 17:18:27 +01001093 flexcan_transceiver_disable(priv);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001094 priv->can.state = CAN_STATE_STOPPED;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001095}
1096
1097static int flexcan_open(struct net_device *dev)
1098{
1099 struct flexcan_priv *priv = netdev_priv(dev);
1100 int err;
1101
Fabio Estevamaa101812013-07-22 12:41:40 -03001102 err = clk_prepare_enable(priv->clk_ipg);
1103 if (err)
1104 return err;
1105
1106 err = clk_prepare_enable(priv->clk_per);
1107 if (err)
1108 goto out_disable_ipg;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001109
1110 err = open_candev(dev);
1111 if (err)
Fabio Estevamaa101812013-07-22 12:41:40 -03001112 goto out_disable_per;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001113
1114 err = request_irq(dev->irq, flexcan_irq, IRQF_SHARED, dev->name, dev);
1115 if (err)
1116 goto out_close;
1117
1118 /* start chip and queuing */
1119 err = flexcan_chip_start(dev);
1120 if (err)
Marc Kleine-Budde7e9e1482014-02-28 14:52:01 +01001121 goto out_free_irq;
Fabio Baltieriadccadb2012-12-18 18:50:58 +01001122
1123 can_led_event(dev, CAN_LED_EVENT_OPEN);
1124
Marc Kleine-Budde30164752015-05-10 15:26:58 +02001125 can_rx_offload_enable(&priv->offload);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001126 netif_start_queue(dev);
1127
1128 return 0;
1129
Marc Kleine-Budde7e9e1482014-02-28 14:52:01 +01001130 out_free_irq:
1131 free_irq(dev->irq, dev);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001132 out_close:
1133 close_candev(dev);
Fabio Estevamaa101812013-07-22 12:41:40 -03001134 out_disable_per:
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001135 clk_disable_unprepare(priv->clk_per);
Fabio Estevamaa101812013-07-22 12:41:40 -03001136 out_disable_ipg:
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001137 clk_disable_unprepare(priv->clk_ipg);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001138
1139 return err;
1140}
1141
1142static int flexcan_close(struct net_device *dev)
1143{
1144 struct flexcan_priv *priv = netdev_priv(dev);
1145
1146 netif_stop_queue(dev);
Marc Kleine-Budde30164752015-05-10 15:26:58 +02001147 can_rx_offload_disable(&priv->offload);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001148 flexcan_chip_stop(dev);
1149
1150 free_irq(dev->irq, dev);
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001151 clk_disable_unprepare(priv->clk_per);
1152 clk_disable_unprepare(priv->clk_ipg);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001153
1154 close_candev(dev);
1155
Fabio Baltieriadccadb2012-12-18 18:50:58 +01001156 can_led_event(dev, CAN_LED_EVENT_STOP);
1157
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001158 return 0;
1159}
1160
1161static int flexcan_set_mode(struct net_device *dev, enum can_mode mode)
1162{
1163 int err;
1164
1165 switch (mode) {
1166 case CAN_MODE_START:
1167 err = flexcan_chip_start(dev);
1168 if (err)
1169 return err;
1170
1171 netif_wake_queue(dev);
1172 break;
1173
1174 default:
1175 return -EOPNOTSUPP;
1176 }
1177
1178 return 0;
1179}
1180
1181static const struct net_device_ops flexcan_netdev_ops = {
1182 .ndo_open = flexcan_open,
1183 .ndo_stop = flexcan_close,
1184 .ndo_start_xmit = flexcan_start_xmit,
Oliver Hartkoppc971fa22014-03-07 09:23:41 +01001185 .ndo_change_mtu = can_change_mtu,
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001186};
1187
Bill Pemberton3c8ac0f2012-12-03 09:22:44 -05001188static int register_flexcandev(struct net_device *dev)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001189{
1190 struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Budde89af8742015-05-08 09:32:58 +02001191 struct flexcan_regs __iomem *regs = priv->regs;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001192 u32 reg, err;
1193
Fabio Estevamaa101812013-07-22 12:41:40 -03001194 err = clk_prepare_enable(priv->clk_ipg);
1195 if (err)
1196 return err;
1197
1198 err = clk_prepare_enable(priv->clk_per);
1199 if (err)
1200 goto out_disable_ipg;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001201
1202 /* select "bus clock", chip must be disabled */
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001203 err = flexcan_chip_disable(priv);
1204 if (err)
1205 goto out_disable_per;
Pankaj Bansal88462d22017-11-24 18:52:08 +05301206 reg = priv->read(&regs->ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001207 reg |= FLEXCAN_CTRL_CLK_SRC;
Pankaj Bansal88462d22017-11-24 18:52:08 +05301208 priv->write(reg, &regs->ctrl);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001209
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001210 err = flexcan_chip_enable(priv);
1211 if (err)
1212 goto out_chip_disable;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001213
1214 /* set freeze, halt and activate FIFO, restrict register access */
Pankaj Bansal88462d22017-11-24 18:52:08 +05301215 reg = priv->read(&regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001216 reg |= FLEXCAN_MCR_FRZ | FLEXCAN_MCR_HALT |
1217 FLEXCAN_MCR_FEN | FLEXCAN_MCR_SUPV;
Pankaj Bansal88462d22017-11-24 18:52:08 +05301218 priv->write(reg, &regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001219
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +02001220 /* Currently we only support newer versions of this core
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +02001221 * featuring a RX hardware FIFO (although this driver doesn't
1222 * make use of it on some cores). Older cores, found on some
1223 * Coldfire derivates are not tested.
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001224 */
Pankaj Bansal88462d22017-11-24 18:52:08 +05301225 reg = priv->read(&regs->mcr);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001226 if (!(reg & FLEXCAN_MCR_FEN)) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +01001227 netdev_err(dev, "Could not enable RX FIFO, unsupported core\n");
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001228 err = -ENODEV;
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001229 goto out_chip_disable;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001230 }
1231
1232 err = register_candev(dev);
1233
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001234 /* disable core and turn off clocks */
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001235 out_chip_disable:
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001236 flexcan_chip_disable(priv);
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001237 out_disable_per:
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001238 clk_disable_unprepare(priv->clk_per);
Fabio Estevamaa101812013-07-22 12:41:40 -03001239 out_disable_ipg:
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001240 clk_disable_unprepare(priv->clk_ipg);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001241
1242 return err;
1243}
1244
Bill Pemberton3c8ac0f2012-12-03 09:22:44 -05001245static void unregister_flexcandev(struct net_device *dev)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001246{
1247 unregister_candev(dev);
1248}
1249
Hui Wang30c1e672012-06-28 16:21:35 +08001250static const struct of_device_id flexcan_of_match[] = {
Hui Wang30c1e672012-06-28 16:21:35 +08001251 { .compatible = "fsl,imx6q-flexcan", .data = &fsl_imx6q_devtype_data, },
Marc Kleine-Buddee3587842013-10-03 23:51:55 +02001252 { .compatible = "fsl,imx28-flexcan", .data = &fsl_imx28_devtype_data, },
Pankaj Bansal88462d22017-11-24 18:52:08 +05301253 { .compatible = "fsl,imx53-flexcan", .data = &fsl_p1010_devtype_data, },
1254 { .compatible = "fsl,imx35-flexcan", .data = &fsl_p1010_devtype_data, },
1255 { .compatible = "fsl,imx25-flexcan", .data = &fsl_p1010_devtype_data, },
Marc Kleine-Buddee3587842013-10-03 23:51:55 +02001256 { .compatible = "fsl,p1010-flexcan", .data = &fsl_p1010_devtype_data, },
Stefan Agnercdce8442014-07-15 14:56:21 +02001257 { .compatible = "fsl,vf610-flexcan", .data = &fsl_vf610_devtype_data, },
Pankaj Bansal99b76682017-11-24 18:52:09 +05301258 { .compatible = "fsl,ls1021ar2-flexcan", .data = &fsl_ls1021a_r2_devtype_data, },
Hui Wang30c1e672012-06-28 16:21:35 +08001259 { /* sentinel */ },
1260};
Marc Kleine-Budde4358a9d2012-10-04 10:55:35 +02001261MODULE_DEVICE_TABLE(of, flexcan_of_match);
Hui Wang30c1e672012-06-28 16:21:35 +08001262
1263static const struct platform_device_id flexcan_id_table[] = {
1264 { .name = "flexcan", .driver_data = (kernel_ulong_t)&fsl_p1010_devtype_data, },
1265 { /* sentinel */ },
1266};
Marc Kleine-Budde4358a9d2012-10-04 10:55:35 +02001267MODULE_DEVICE_TABLE(platform, flexcan_id_table);
Hui Wang30c1e672012-06-28 16:21:35 +08001268
Bill Pemberton3c8ac0f2012-12-03 09:22:44 -05001269static int flexcan_probe(struct platform_device *pdev)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001270{
Hui Wang30c1e672012-06-28 16:21:35 +08001271 const struct of_device_id *of_id;
Marc Kleine-Buddedda0b3b2012-07-13 14:52:48 +02001272 const struct flexcan_devtype_data *devtype_data;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001273 struct net_device *dev;
1274 struct flexcan_priv *priv;
Andreas Werner555828e2015-03-22 17:35:52 +01001275 struct regulator *reg_xceiver;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001276 struct resource *mem;
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001277 struct clk *clk_ipg = NULL, *clk_per = NULL;
Marc Kleine-Budde89af8742015-05-08 09:32:58 +02001278 struct flexcan_regs __iomem *regs;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001279 int err, irq;
holt@sgi.com97efe9a2011-08-16 17:32:23 +00001280 u32 clock_freq = 0;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001281
Andreas Werner555828e2015-03-22 17:35:52 +01001282 reg_xceiver = devm_regulator_get(&pdev->dev, "xceiver");
1283 if (PTR_ERR(reg_xceiver) == -EPROBE_DEFER)
1284 return -EPROBE_DEFER;
1285 else if (IS_ERR(reg_xceiver))
1286 reg_xceiver = NULL;
1287
Hui Wangafc016d2012-06-28 16:21:34 +08001288 if (pdev->dev.of_node)
1289 of_property_read_u32(pdev->dev.of_node,
Marc Kleine-Budde0012e5c2015-08-06 14:53:57 +02001290 "clock-frequency", &clock_freq);
holt@sgi.com97efe9a2011-08-16 17:32:23 +00001291
1292 if (!clock_freq) {
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001293 clk_ipg = devm_clk_get(&pdev->dev, "ipg");
1294 if (IS_ERR(clk_ipg)) {
1295 dev_err(&pdev->dev, "no ipg clock defined\n");
Fabio Estevam933e4af2013-07-22 12:41:39 -03001296 return PTR_ERR(clk_ipg);
holt@sgi.com97efe9a2011-08-16 17:32:23 +00001297 }
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001298
1299 clk_per = devm_clk_get(&pdev->dev, "per");
1300 if (IS_ERR(clk_per)) {
1301 dev_err(&pdev->dev, "no per clock defined\n");
Fabio Estevam933e4af2013-07-22 12:41:39 -03001302 return PTR_ERR(clk_per);
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001303 }
Marc Kleine-Budde1a3e5172013-11-25 22:15:20 +01001304 clock_freq = clk_get_rate(clk_per);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001305 }
1306
1307 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1308 irq = platform_get_irq(pdev, 0);
Fabio Estevam933e4af2013-07-22 12:41:39 -03001309 if (irq <= 0)
1310 return -ENODEV;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001311
Marc Kleine-Budde89af8742015-05-08 09:32:58 +02001312 regs = devm_ioremap_resource(&pdev->dev, mem);
1313 if (IS_ERR(regs))
1314 return PTR_ERR(regs);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001315
Hui Wang30c1e672012-06-28 16:21:35 +08001316 of_id = of_match_device(flexcan_of_match, &pdev->dev);
1317 if (of_id) {
1318 devtype_data = of_id->data;
Marc Kleine-Budded0873e62014-03-04 22:04:22 +01001319 } else if (platform_get_device_id(pdev)->driver_data) {
Hui Wang30c1e672012-06-28 16:21:35 +08001320 devtype_data = (struct flexcan_devtype_data *)
Marc Kleine-Budded0873e62014-03-04 22:04:22 +01001321 platform_get_device_id(pdev)->driver_data;
Hui Wang30c1e672012-06-28 16:21:35 +08001322 } else {
Fabio Estevam933e4af2013-07-22 12:41:39 -03001323 return -ENODEV;
Hui Wang30c1e672012-06-28 16:21:35 +08001324 }
1325
Fabio Estevam933e4af2013-07-22 12:41:39 -03001326 dev = alloc_candev(sizeof(struct flexcan_priv), 1);
1327 if (!dev)
1328 return -ENOMEM;
1329
Marc Kleine-Budde30164752015-05-10 15:26:58 +02001330 platform_set_drvdata(pdev, dev);
1331 SET_NETDEV_DEV(dev, &pdev->dev);
1332
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001333 dev->netdev_ops = &flexcan_netdev_ops;
1334 dev->irq = irq;
Reuben Dowle9a123492011-11-01 11:18:03 +13001335 dev->flags |= IFF_ECHO;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001336
1337 priv = netdev_priv(dev);
Pankaj Bansal88462d22017-11-24 18:52:08 +05301338
1339 if (of_property_read_bool(pdev->dev.of_node, "big-endian")) {
1340 priv->read = flexcan_read_be;
1341 priv->write = flexcan_write_be;
1342 } else {
1343 if (of_device_is_compatible(pdev->dev.of_node,
1344 "fsl,p1010-flexcan")) {
1345 priv->read = flexcan_read_be;
1346 priv->write = flexcan_write_be;
1347 } else {
1348 priv->read = flexcan_read_le;
1349 priv->write = flexcan_write_le;
1350 }
1351 }
1352
holt@sgi.com97efe9a2011-08-16 17:32:23 +00001353 priv->can.clock.freq = clock_freq;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001354 priv->can.bittiming_const = &flexcan_bittiming_const;
1355 priv->can.do_set_mode = flexcan_set_mode;
1356 priv->can.do_get_berr_counter = flexcan_get_berr_counter;
1357 priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK |
1358 CAN_CTRLMODE_LISTENONLY | CAN_CTRLMODE_3_SAMPLES |
1359 CAN_CTRLMODE_BERR_REPORTING;
Marc Kleine-Budde89af8742015-05-08 09:32:58 +02001360 priv->regs = regs;
Steffen Trumtrar3d42a372012-07-17 16:14:34 +02001361 priv->clk_ipg = clk_ipg;
1362 priv->clk_per = clk_per;
Hui Wang30c1e672012-06-28 16:21:35 +08001363 priv->devtype_data = devtype_data;
Andreas Werner555828e2015-03-22 17:35:52 +01001364 priv->reg_xceiver = reg_xceiver;
Fabio Estevamb7c41142013-06-10 23:12:57 -03001365
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +02001366 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP) {
1367 priv->tx_mb_idx = FLEXCAN_TX_MB_OFF_TIMESTAMP;
1368 priv->tx_mb_reserved = &regs->mb[FLEXCAN_TX_MB_RESERVED_OFF_TIMESTAMP];
1369 } else {
1370 priv->tx_mb_idx = FLEXCAN_TX_MB_OFF_FIFO;
1371 priv->tx_mb_reserved = &regs->mb[FLEXCAN_TX_MB_RESERVED_OFF_FIFO];
1372 }
Marc Kleine-Buddeb93917c2015-07-12 00:47:47 +02001373 priv->tx_mb = &regs->mb[priv->tx_mb_idx];
1374
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +02001375 priv->reg_imask1_default = FLEXCAN_IFLAG_MB(priv->tx_mb_idx);
1376 priv->reg_imask2_default = 0;
Marc Kleine-Budde28ac7dc2015-08-04 13:46:10 +02001377
Marc Kleine-Budde30164752015-05-10 15:26:58 +02001378 priv->offload.mailbox_read = flexcan_mailbox_read;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001379
Marc Kleine-Buddeb3cf53e2015-09-01 09:00:13 +02001380 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP) {
1381 u64 imask;
1382
1383 priv->offload.mb_first = FLEXCAN_RX_MB_OFF_TIMESTAMP_FIRST;
1384 priv->offload.mb_last = FLEXCAN_RX_MB_OFF_TIMESTAMP_LAST;
1385
1386 imask = GENMASK_ULL(priv->offload.mb_last, priv->offload.mb_first);
1387 priv->reg_imask1_default |= imask;
1388 priv->reg_imask2_default |= imask >> 32;
1389
1390 err = can_rx_offload_add_timestamp(dev, &priv->offload);
1391 } else {
1392 priv->reg_imask1_default |= FLEXCAN_IFLAG_RX_FIFO_OVERFLOW |
1393 FLEXCAN_IFLAG_RX_FIFO_AVAILABLE;
1394 err = can_rx_offload_add_fifo(dev, &priv->offload, FLEXCAN_NAPI_WEIGHT);
1395 }
Marc Kleine-Budde30164752015-05-10 15:26:58 +02001396 if (err)
1397 goto failed_offload;
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001398
1399 err = register_flexcandev(dev);
1400 if (err) {
1401 dev_err(&pdev->dev, "registering netdev failed\n");
1402 goto failed_register;
1403 }
1404
Fabio Baltieriadccadb2012-12-18 18:50:58 +01001405 devm_can_led_init(dev);
1406
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001407 dev_info(&pdev->dev, "device registered (reg_base=%p, irq=%d)\n",
Marc Kleine-Budde89af8742015-05-08 09:32:58 +02001408 priv->regs, dev->irq);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001409
1410 return 0;
1411
Marc Kleine-Budde30164752015-05-10 15:26:58 +02001412 failed_offload:
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001413 failed_register:
1414 free_candev(dev);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001415 return err;
1416}
1417
Bill Pemberton3c8ac0f2012-12-03 09:22:44 -05001418static int flexcan_remove(struct platform_device *pdev)
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001419{
1420 struct net_device *dev = platform_get_drvdata(pdev);
Marc Kleine-Budded96e43e2014-02-28 20:48:36 +01001421 struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001422
1423 unregister_flexcandev(dev);
Marc Kleine-Budde30164752015-05-10 15:26:58 +02001424 can_rx_offload_del(&priv->offload);
Marc Kleine-Budde9a275862010-10-21 05:07:58 +00001425 free_candev(dev);
1426
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001427 return 0;
1428}
1429
Marc Kleine-Budde08c6d352014-03-05 19:10:44 +01001430static int __maybe_unused flexcan_suspend(struct device *device)
Eric Bénard8b5e2182012-05-08 17:12:17 +02001431{
Fabio Estevam588e7a82013-05-20 15:43:43 -03001432 struct net_device *dev = dev_get_drvdata(device);
Eric Bénard8b5e2182012-05-08 17:12:17 +02001433 struct flexcan_priv *priv = netdev_priv(dev);
Marc Kleine-Budde9b00b302014-02-28 15:30:18 +01001434 int err;
Eric Bénard8b5e2182012-05-08 17:12:17 +02001435
Eric Bénard8b5e2182012-05-08 17:12:17 +02001436 if (netif_running(dev)) {
Fabio Estevam4de349e2016-08-17 12:41:08 -03001437 err = flexcan_chip_disable(priv);
1438 if (err)
1439 return err;
Eric Bénard8b5e2182012-05-08 17:12:17 +02001440 netif_stop_queue(dev);
1441 netif_device_detach(dev);
1442 }
1443 priv->can.state = CAN_STATE_SLEEPING;
1444
1445 return 0;
1446}
1447
Marc Kleine-Budde08c6d352014-03-05 19:10:44 +01001448static int __maybe_unused flexcan_resume(struct device *device)
Eric Bénard8b5e2182012-05-08 17:12:17 +02001449{
Fabio Estevam588e7a82013-05-20 15:43:43 -03001450 struct net_device *dev = dev_get_drvdata(device);
Eric Bénard8b5e2182012-05-08 17:12:17 +02001451 struct flexcan_priv *priv = netdev_priv(dev);
Fabio Estevam4de349e2016-08-17 12:41:08 -03001452 int err;
Eric Bénard8b5e2182012-05-08 17:12:17 +02001453
1454 priv->can.state = CAN_STATE_ERROR_ACTIVE;
1455 if (netif_running(dev)) {
1456 netif_device_attach(dev);
1457 netif_start_queue(dev);
Fabio Estevam4de349e2016-08-17 12:41:08 -03001458 err = flexcan_chip_enable(priv);
1459 if (err)
1460 return err;
Eric Bénard8b5e2182012-05-08 17:12:17 +02001461 }
Fabio Estevam4de349e2016-08-17 12:41:08 -03001462 return 0;
Eric Bénard8b5e2182012-05-08 17:12:17 +02001463}
Fabio Estevam588e7a82013-05-20 15:43:43 -03001464
1465static SIMPLE_DEV_PM_OPS(flexcan_pm_ops, flexcan_suspend, flexcan_resume);
Eric Bénard8b5e2182012-05-08 17:12:17 +02001466
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001467static struct platform_driver flexcan_driver = {
holt@sgi.comc8aef4c2011-08-16 17:32:22 +00001468 .driver = {
1469 .name = DRV_NAME,
Fabio Estevam588e7a82013-05-20 15:43:43 -03001470 .pm = &flexcan_pm_ops,
holt@sgi.comc8aef4c2011-08-16 17:32:22 +00001471 .of_match_table = flexcan_of_match,
1472 },
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001473 .probe = flexcan_probe,
Bill Pemberton3c8ac0f2012-12-03 09:22:44 -05001474 .remove = flexcan_remove,
Hui Wang30c1e672012-06-28 16:21:35 +08001475 .id_table = flexcan_id_table,
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001476};
1477
Axel Lin871d3372011-11-27 15:42:31 +00001478module_platform_driver(flexcan_driver);
Marc Kleine-Buddee955cea2009-07-29 10:20:10 +02001479
1480MODULE_AUTHOR("Sascha Hauer <kernel@pengutronix.de>, "
1481 "Marc Kleine-Budde <kernel@pengutronix.de>");
1482MODULE_LICENSE("GPL v2");
1483MODULE_DESCRIPTION("CAN port driver for flexcan based chip");