xref: /OK3568_Linux_fs/kernel/drivers/net/can/grcan.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Socket CAN driver for Aeroflex Gaisler GRCAN and GRHCAN.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * 2012 (c) Aeroflex Gaisler AB
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * This driver supports GRCAN and GRHCAN CAN controllers available in the GRLIB
8*4882a593Smuzhiyun  * VHDL IP core library.
9*4882a593Smuzhiyun  *
10*4882a593Smuzhiyun  * Full documentation of the GRCAN core can be found here:
11*4882a593Smuzhiyun  * http://www.gaisler.com/products/grlib/grip.pdf
12*4882a593Smuzhiyun  *
13*4882a593Smuzhiyun  * See "Documentation/devicetree/bindings/net/can/grcan.txt" for information on
14*4882a593Smuzhiyun  * open firmware properties.
15*4882a593Smuzhiyun  *
16*4882a593Smuzhiyun  * See "Documentation/ABI/testing/sysfs-class-net-grcan" for information on the
17*4882a593Smuzhiyun  * sysfs interface.
18*4882a593Smuzhiyun  *
19*4882a593Smuzhiyun  * See "Documentation/admin-guide/kernel-parameters.rst" for information on the module
20*4882a593Smuzhiyun  * parameters.
21*4882a593Smuzhiyun  *
22*4882a593Smuzhiyun  * Contributors: Andreas Larsson <andreas@gaisler.com>
23*4882a593Smuzhiyun  */
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun #include <linux/kernel.h>
26*4882a593Smuzhiyun #include <linux/module.h>
27*4882a593Smuzhiyun #include <linux/interrupt.h>
28*4882a593Smuzhiyun #include <linux/netdevice.h>
29*4882a593Smuzhiyun #include <linux/delay.h>
30*4882a593Smuzhiyun #include <linux/io.h>
31*4882a593Smuzhiyun #include <linux/can/dev.h>
32*4882a593Smuzhiyun #include <linux/spinlock.h>
33*4882a593Smuzhiyun #include <linux/of_platform.h>
34*4882a593Smuzhiyun #include <linux/of_irq.h>
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun #include <linux/dma-mapping.h>
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun #define DRV_NAME	"grcan"
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun #define GRCAN_NAPI_WEIGHT	32
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun #define GRCAN_RESERVE_SIZE(slot1, slot2) (((slot2) - (slot1)) / 4 - 1)
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun struct grcan_registers {
45*4882a593Smuzhiyun 	u32 conf;	/* 0x00 */
46*4882a593Smuzhiyun 	u32 stat;	/* 0x04 */
47*4882a593Smuzhiyun 	u32 ctrl;	/* 0x08 */
48*4882a593Smuzhiyun 	u32 __reserved1[GRCAN_RESERVE_SIZE(0x08, 0x18)];
49*4882a593Smuzhiyun 	u32 smask;	/* 0x18 - CanMASK */
50*4882a593Smuzhiyun 	u32 scode;	/* 0x1c - CanCODE */
51*4882a593Smuzhiyun 	u32 __reserved2[GRCAN_RESERVE_SIZE(0x1c, 0x100)];
52*4882a593Smuzhiyun 	u32 pimsr;	/* 0x100 */
53*4882a593Smuzhiyun 	u32 pimr;	/* 0x104 */
54*4882a593Smuzhiyun 	u32 pisr;	/* 0x108 */
55*4882a593Smuzhiyun 	u32 pir;	/* 0x10C */
56*4882a593Smuzhiyun 	u32 imr;	/* 0x110 */
57*4882a593Smuzhiyun 	u32 picr;	/* 0x114 */
58*4882a593Smuzhiyun 	u32 __reserved3[GRCAN_RESERVE_SIZE(0x114, 0x200)];
59*4882a593Smuzhiyun 	u32 txctrl;	/* 0x200 */
60*4882a593Smuzhiyun 	u32 txaddr;	/* 0x204 */
61*4882a593Smuzhiyun 	u32 txsize;	/* 0x208 */
62*4882a593Smuzhiyun 	u32 txwr;	/* 0x20C */
63*4882a593Smuzhiyun 	u32 txrd;	/* 0x210 */
64*4882a593Smuzhiyun 	u32 txirq;	/* 0x214 */
65*4882a593Smuzhiyun 	u32 __reserved4[GRCAN_RESERVE_SIZE(0x214, 0x300)];
66*4882a593Smuzhiyun 	u32 rxctrl;	/* 0x300 */
67*4882a593Smuzhiyun 	u32 rxaddr;	/* 0x304 */
68*4882a593Smuzhiyun 	u32 rxsize;	/* 0x308 */
69*4882a593Smuzhiyun 	u32 rxwr;	/* 0x30C */
70*4882a593Smuzhiyun 	u32 rxrd;	/* 0x310 */
71*4882a593Smuzhiyun 	u32 rxirq;	/* 0x314 */
72*4882a593Smuzhiyun 	u32 rxmask;	/* 0x318 */
73*4882a593Smuzhiyun 	u32 rxcode;	/* 0x31C */
74*4882a593Smuzhiyun };
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun #define GRCAN_CONF_ABORT	0x00000001
77*4882a593Smuzhiyun #define GRCAN_CONF_ENABLE0	0x00000002
78*4882a593Smuzhiyun #define GRCAN_CONF_ENABLE1	0x00000004
79*4882a593Smuzhiyun #define GRCAN_CONF_SELECT	0x00000008
80*4882a593Smuzhiyun #define GRCAN_CONF_SILENT	0x00000010
81*4882a593Smuzhiyun #define GRCAN_CONF_SAM		0x00000020 /* Available in some hardware */
82*4882a593Smuzhiyun #define GRCAN_CONF_BPR		0x00000300 /* Note: not BRP */
83*4882a593Smuzhiyun #define GRCAN_CONF_RSJ		0x00007000
84*4882a593Smuzhiyun #define GRCAN_CONF_PS1		0x00f00000
85*4882a593Smuzhiyun #define GRCAN_CONF_PS2		0x000f0000
86*4882a593Smuzhiyun #define GRCAN_CONF_SCALER	0xff000000
87*4882a593Smuzhiyun #define GRCAN_CONF_OPERATION						\
88*4882a593Smuzhiyun 	(GRCAN_CONF_ABORT | GRCAN_CONF_ENABLE0 | GRCAN_CONF_ENABLE1	\
89*4882a593Smuzhiyun 	 | GRCAN_CONF_SELECT | GRCAN_CONF_SILENT | GRCAN_CONF_SAM)
90*4882a593Smuzhiyun #define GRCAN_CONF_TIMING						\
91*4882a593Smuzhiyun 	(GRCAN_CONF_BPR | GRCAN_CONF_RSJ | GRCAN_CONF_PS1		\
92*4882a593Smuzhiyun 	 | GRCAN_CONF_PS2 | GRCAN_CONF_SCALER)
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun #define GRCAN_CONF_RSJ_MIN	1
95*4882a593Smuzhiyun #define GRCAN_CONF_RSJ_MAX	4
96*4882a593Smuzhiyun #define GRCAN_CONF_PS1_MIN	1
97*4882a593Smuzhiyun #define GRCAN_CONF_PS1_MAX	15
98*4882a593Smuzhiyun #define GRCAN_CONF_PS2_MIN	2
99*4882a593Smuzhiyun #define GRCAN_CONF_PS2_MAX	8
100*4882a593Smuzhiyun #define GRCAN_CONF_SCALER_MIN	0
101*4882a593Smuzhiyun #define GRCAN_CONF_SCALER_MAX	255
102*4882a593Smuzhiyun #define GRCAN_CONF_SCALER_INC	1
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun #define GRCAN_CONF_BPR_BIT	8
105*4882a593Smuzhiyun #define GRCAN_CONF_RSJ_BIT	12
106*4882a593Smuzhiyun #define GRCAN_CONF_PS1_BIT	20
107*4882a593Smuzhiyun #define GRCAN_CONF_PS2_BIT	16
108*4882a593Smuzhiyun #define GRCAN_CONF_SCALER_BIT	24
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun #define GRCAN_STAT_PASS		0x000001
111*4882a593Smuzhiyun #define GRCAN_STAT_OFF		0x000002
112*4882a593Smuzhiyun #define GRCAN_STAT_OR		0x000004
113*4882a593Smuzhiyun #define GRCAN_STAT_AHBERR	0x000008
114*4882a593Smuzhiyun #define GRCAN_STAT_ACTIVE	0x000010
115*4882a593Smuzhiyun #define GRCAN_STAT_RXERRCNT	0x00ff00
116*4882a593Smuzhiyun #define GRCAN_STAT_TXERRCNT	0xff0000
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun #define GRCAN_STAT_ERRCTR_RELATED	(GRCAN_STAT_PASS | GRCAN_STAT_OFF)
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun #define GRCAN_STAT_RXERRCNT_BIT	8
121*4882a593Smuzhiyun #define GRCAN_STAT_TXERRCNT_BIT	16
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun #define GRCAN_STAT_ERRCNT_WARNING_LIMIT	96
124*4882a593Smuzhiyun #define GRCAN_STAT_ERRCNT_PASSIVE_LIMIT	127
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun #define GRCAN_CTRL_RESET	0x2
127*4882a593Smuzhiyun #define GRCAN_CTRL_ENABLE	0x1
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun #define GRCAN_TXCTRL_ENABLE	0x1
130*4882a593Smuzhiyun #define GRCAN_TXCTRL_ONGOING	0x2
131*4882a593Smuzhiyun #define GRCAN_TXCTRL_SINGLE	0x4
132*4882a593Smuzhiyun 
133*4882a593Smuzhiyun #define GRCAN_RXCTRL_ENABLE	0x1
134*4882a593Smuzhiyun #define GRCAN_RXCTRL_ONGOING	0x2
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun /* Relative offset of IRQ sources to AMBA Plug&Play */
137*4882a593Smuzhiyun #define GRCAN_IRQIX_IRQ		0
138*4882a593Smuzhiyun #define GRCAN_IRQIX_TXSYNC	1
139*4882a593Smuzhiyun #define GRCAN_IRQIX_RXSYNC	2
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun #define GRCAN_IRQ_PASS		0x00001
142*4882a593Smuzhiyun #define GRCAN_IRQ_OFF		0x00002
143*4882a593Smuzhiyun #define GRCAN_IRQ_OR		0x00004
144*4882a593Smuzhiyun #define GRCAN_IRQ_RXAHBERR	0x00008
145*4882a593Smuzhiyun #define GRCAN_IRQ_TXAHBERR	0x00010
146*4882a593Smuzhiyun #define GRCAN_IRQ_RXIRQ		0x00020
147*4882a593Smuzhiyun #define GRCAN_IRQ_TXIRQ		0x00040
148*4882a593Smuzhiyun #define GRCAN_IRQ_RXFULL	0x00080
149*4882a593Smuzhiyun #define GRCAN_IRQ_TXEMPTY	0x00100
150*4882a593Smuzhiyun #define GRCAN_IRQ_RX		0x00200
151*4882a593Smuzhiyun #define GRCAN_IRQ_TX		0x00400
152*4882a593Smuzhiyun #define GRCAN_IRQ_RXSYNC	0x00800
153*4882a593Smuzhiyun #define GRCAN_IRQ_TXSYNC	0x01000
154*4882a593Smuzhiyun #define GRCAN_IRQ_RXERRCTR	0x02000
155*4882a593Smuzhiyun #define GRCAN_IRQ_TXERRCTR	0x04000
156*4882a593Smuzhiyun #define GRCAN_IRQ_RXMISS	0x08000
157*4882a593Smuzhiyun #define GRCAN_IRQ_TXLOSS	0x10000
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun #define GRCAN_IRQ_NONE	0
160*4882a593Smuzhiyun #define GRCAN_IRQ_ALL							\
161*4882a593Smuzhiyun 	(GRCAN_IRQ_PASS | GRCAN_IRQ_OFF | GRCAN_IRQ_OR			\
162*4882a593Smuzhiyun 	 | GRCAN_IRQ_RXAHBERR | GRCAN_IRQ_TXAHBERR			\
163*4882a593Smuzhiyun 	 | GRCAN_IRQ_RXIRQ | GRCAN_IRQ_TXIRQ				\
164*4882a593Smuzhiyun 	 | GRCAN_IRQ_RXFULL | GRCAN_IRQ_TXEMPTY				\
165*4882a593Smuzhiyun 	 | GRCAN_IRQ_RX | GRCAN_IRQ_TX | GRCAN_IRQ_RXSYNC		\
166*4882a593Smuzhiyun 	 | GRCAN_IRQ_TXSYNC | GRCAN_IRQ_RXERRCTR			\
167*4882a593Smuzhiyun 	 | GRCAN_IRQ_TXERRCTR | GRCAN_IRQ_RXMISS			\
168*4882a593Smuzhiyun 	 | GRCAN_IRQ_TXLOSS)
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun #define GRCAN_IRQ_ERRCTR_RELATED (GRCAN_IRQ_RXERRCTR | GRCAN_IRQ_TXERRCTR \
171*4882a593Smuzhiyun 				  | GRCAN_IRQ_PASS | GRCAN_IRQ_OFF)
172*4882a593Smuzhiyun #define GRCAN_IRQ_ERRORS (GRCAN_IRQ_ERRCTR_RELATED | GRCAN_IRQ_OR	\
173*4882a593Smuzhiyun 			  | GRCAN_IRQ_TXAHBERR | GRCAN_IRQ_RXAHBERR	\
174*4882a593Smuzhiyun 			  | GRCAN_IRQ_TXLOSS)
175*4882a593Smuzhiyun #define GRCAN_IRQ_DEFAULT (GRCAN_IRQ_RX | GRCAN_IRQ_TX | GRCAN_IRQ_ERRORS)
176*4882a593Smuzhiyun 
177*4882a593Smuzhiyun #define GRCAN_MSG_SIZE		16
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun #define GRCAN_MSG_IDE		0x80000000
180*4882a593Smuzhiyun #define GRCAN_MSG_RTR		0x40000000
181*4882a593Smuzhiyun #define GRCAN_MSG_BID		0x1ffc0000
182*4882a593Smuzhiyun #define GRCAN_MSG_EID		0x1fffffff
183*4882a593Smuzhiyun #define GRCAN_MSG_IDE_BIT	31
184*4882a593Smuzhiyun #define GRCAN_MSG_RTR_BIT	30
185*4882a593Smuzhiyun #define GRCAN_MSG_BID_BIT	18
186*4882a593Smuzhiyun #define GRCAN_MSG_EID_BIT	0
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun #define GRCAN_MSG_DLC		0xf0000000
189*4882a593Smuzhiyun #define GRCAN_MSG_TXERRC	0x00ff0000
190*4882a593Smuzhiyun #define GRCAN_MSG_RXERRC	0x0000ff00
191*4882a593Smuzhiyun #define GRCAN_MSG_DLC_BIT	28
192*4882a593Smuzhiyun #define GRCAN_MSG_TXERRC_BIT	16
193*4882a593Smuzhiyun #define GRCAN_MSG_RXERRC_BIT	8
194*4882a593Smuzhiyun #define GRCAN_MSG_AHBERR	0x00000008
195*4882a593Smuzhiyun #define GRCAN_MSG_OR		0x00000004
196*4882a593Smuzhiyun #define GRCAN_MSG_OFF		0x00000002
197*4882a593Smuzhiyun #define GRCAN_MSG_PASS		0x00000001
198*4882a593Smuzhiyun 
199*4882a593Smuzhiyun #define GRCAN_MSG_DATA_SLOT_INDEX(i) (2 + (i) / 4)
200*4882a593Smuzhiyun #define GRCAN_MSG_DATA_SHIFT(i) ((3 - (i) % 4) * 8)
201*4882a593Smuzhiyun 
202*4882a593Smuzhiyun #define GRCAN_BUFFER_ALIGNMENT		1024
203*4882a593Smuzhiyun #define GRCAN_DEFAULT_BUFFER_SIZE	1024
204*4882a593Smuzhiyun #define GRCAN_VALID_TR_SIZE_MASK	0x001fffc0
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun #define GRCAN_INVALID_BUFFER_SIZE(s)			\
207*4882a593Smuzhiyun 	((s) == 0 || ((s) & ~GRCAN_VALID_TR_SIZE_MASK))
208*4882a593Smuzhiyun 
209*4882a593Smuzhiyun #if GRCAN_INVALID_BUFFER_SIZE(GRCAN_DEFAULT_BUFFER_SIZE)
210*4882a593Smuzhiyun #error "Invalid default buffer size"
211*4882a593Smuzhiyun #endif
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun struct grcan_dma_buffer {
214*4882a593Smuzhiyun 	size_t size;
215*4882a593Smuzhiyun 	void *buf;
216*4882a593Smuzhiyun 	dma_addr_t handle;
217*4882a593Smuzhiyun };
218*4882a593Smuzhiyun 
219*4882a593Smuzhiyun struct grcan_dma {
220*4882a593Smuzhiyun 	size_t base_size;
221*4882a593Smuzhiyun 	void *base_buf;
222*4882a593Smuzhiyun 	dma_addr_t base_handle;
223*4882a593Smuzhiyun 	struct grcan_dma_buffer tx;
224*4882a593Smuzhiyun 	struct grcan_dma_buffer rx;
225*4882a593Smuzhiyun };
226*4882a593Smuzhiyun 
227*4882a593Smuzhiyun /* GRCAN configuration parameters */
228*4882a593Smuzhiyun struct grcan_device_config {
229*4882a593Smuzhiyun 	unsigned short enable0;
230*4882a593Smuzhiyun 	unsigned short enable1;
231*4882a593Smuzhiyun 	unsigned short select;
232*4882a593Smuzhiyun 	unsigned int txsize;
233*4882a593Smuzhiyun 	unsigned int rxsize;
234*4882a593Smuzhiyun };
235*4882a593Smuzhiyun 
236*4882a593Smuzhiyun #define GRCAN_DEFAULT_DEVICE_CONFIG {				\
237*4882a593Smuzhiyun 		.enable0	= 0,				\
238*4882a593Smuzhiyun 		.enable1	= 0,				\
239*4882a593Smuzhiyun 		.select		= 0,				\
240*4882a593Smuzhiyun 		.txsize		= GRCAN_DEFAULT_BUFFER_SIZE,	\
241*4882a593Smuzhiyun 		.rxsize		= GRCAN_DEFAULT_BUFFER_SIZE,	\
242*4882a593Smuzhiyun 		}
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun #define GRCAN_TXBUG_SAFE_GRLIB_VERSION	4100
245*4882a593Smuzhiyun #define GRLIB_VERSION_MASK		0xffff
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun /* GRCAN private data structure */
248*4882a593Smuzhiyun struct grcan_priv {
249*4882a593Smuzhiyun 	struct can_priv can;	/* must be the first member */
250*4882a593Smuzhiyun 	struct net_device *dev;
251*4882a593Smuzhiyun 	struct device *ofdev_dev;
252*4882a593Smuzhiyun 	struct napi_struct napi;
253*4882a593Smuzhiyun 
254*4882a593Smuzhiyun 	struct grcan_registers __iomem *regs;	/* ioremap'ed registers */
255*4882a593Smuzhiyun 	struct grcan_device_config config;
256*4882a593Smuzhiyun 	struct grcan_dma dma;
257*4882a593Smuzhiyun 
258*4882a593Smuzhiyun 	struct sk_buff **echo_skb;	/* We allocate this on our own */
259*4882a593Smuzhiyun 	u8 *txdlc;			/* Length of queued frames */
260*4882a593Smuzhiyun 
261*4882a593Smuzhiyun 	/* The echo skb pointer, pointing into echo_skb and indicating which
262*4882a593Smuzhiyun 	 * frames can be echoed back. See the "Notes on the tx cyclic buffer
263*4882a593Smuzhiyun 	 * handling"-comment for grcan_start_xmit for more details.
264*4882a593Smuzhiyun 	 */
265*4882a593Smuzhiyun 	u32 eskbp;
266*4882a593Smuzhiyun 
267*4882a593Smuzhiyun 	/* Lock for controlling changes to the netif tx queue state, accesses to
268*4882a593Smuzhiyun 	 * the echo_skb pointer eskbp and for making sure that a running reset
269*4882a593Smuzhiyun 	 * and/or a close of the interface is done without interference from
270*4882a593Smuzhiyun 	 * other parts of the code.
271*4882a593Smuzhiyun 	 *
272*4882a593Smuzhiyun 	 * The echo_skb pointer, eskbp, should only be accessed under this lock
273*4882a593Smuzhiyun 	 * as it can be changed in several places and together with decisions on
274*4882a593Smuzhiyun 	 * whether to wake up the tx queue.
275*4882a593Smuzhiyun 	 *
276*4882a593Smuzhiyun 	 * The tx queue must never be woken up if there is a running reset or
277*4882a593Smuzhiyun 	 * close in progress.
278*4882a593Smuzhiyun 	 *
279*4882a593Smuzhiyun 	 * A running reset (see below on need_txbug_workaround) should never be
280*4882a593Smuzhiyun 	 * done if the interface is closing down and several running resets
281*4882a593Smuzhiyun 	 * should never be scheduled simultaneously.
282*4882a593Smuzhiyun 	 */
283*4882a593Smuzhiyun 	spinlock_t lock;
284*4882a593Smuzhiyun 
285*4882a593Smuzhiyun 	/* Whether a workaround is needed due to a bug in older hardware. In
286*4882a593Smuzhiyun 	 * this case, the driver both tries to prevent the bug from being
287*4882a593Smuzhiyun 	 * triggered and recovers, if the bug nevertheless happens, by doing a
288*4882a593Smuzhiyun 	 * running reset. A running reset, resets the device and continues from
289*4882a593Smuzhiyun 	 * where it were without being noticeable from outside the driver (apart
290*4882a593Smuzhiyun 	 * from slight delays).
291*4882a593Smuzhiyun 	 */
292*4882a593Smuzhiyun 	bool need_txbug_workaround;
293*4882a593Smuzhiyun 
294*4882a593Smuzhiyun 	/* To trigger initization of running reset and to trigger running reset
295*4882a593Smuzhiyun 	 * respectively in the case of a hanged device due to a txbug.
296*4882a593Smuzhiyun 	 */
297*4882a593Smuzhiyun 	struct timer_list hang_timer;
298*4882a593Smuzhiyun 	struct timer_list rr_timer;
299*4882a593Smuzhiyun 
300*4882a593Smuzhiyun 	/* To avoid waking up the netif queue and restarting timers
301*4882a593Smuzhiyun 	 * when a reset is scheduled or when closing of the device is
302*4882a593Smuzhiyun 	 * undergoing
303*4882a593Smuzhiyun 	 */
304*4882a593Smuzhiyun 	bool resetting;
305*4882a593Smuzhiyun 	bool closing;
306*4882a593Smuzhiyun };
307*4882a593Smuzhiyun 
308*4882a593Smuzhiyun /* Wait time for a short wait for ongoing to clear */
309*4882a593Smuzhiyun #define GRCAN_SHORTWAIT_USECS	10
310*4882a593Smuzhiyun 
311*4882a593Smuzhiyun /* Limit on the number of transmitted bits of an eff frame according to the CAN
312*4882a593Smuzhiyun  * specification: 1 bit start of frame, 32 bits arbitration field, 6 bits
313*4882a593Smuzhiyun  * control field, 8 bytes data field, 16 bits crc field, 2 bits ACK field and 7
314*4882a593Smuzhiyun  * bits end of frame
315*4882a593Smuzhiyun  */
316*4882a593Smuzhiyun #define GRCAN_EFF_FRAME_MAX_BITS	(1+32+6+8*8+16+2+7)
317*4882a593Smuzhiyun 
318*4882a593Smuzhiyun #if defined(__BIG_ENDIAN)
grcan_read_reg(u32 __iomem * reg)319*4882a593Smuzhiyun static inline u32 grcan_read_reg(u32 __iomem *reg)
320*4882a593Smuzhiyun {
321*4882a593Smuzhiyun 	return ioread32be(reg);
322*4882a593Smuzhiyun }
323*4882a593Smuzhiyun 
grcan_write_reg(u32 __iomem * reg,u32 val)324*4882a593Smuzhiyun static inline void grcan_write_reg(u32 __iomem *reg, u32 val)
325*4882a593Smuzhiyun {
326*4882a593Smuzhiyun 	iowrite32be(val, reg);
327*4882a593Smuzhiyun }
328*4882a593Smuzhiyun #else
grcan_read_reg(u32 __iomem * reg)329*4882a593Smuzhiyun static inline u32 grcan_read_reg(u32 __iomem *reg)
330*4882a593Smuzhiyun {
331*4882a593Smuzhiyun 	return ioread32(reg);
332*4882a593Smuzhiyun }
333*4882a593Smuzhiyun 
grcan_write_reg(u32 __iomem * reg,u32 val)334*4882a593Smuzhiyun static inline void grcan_write_reg(u32 __iomem *reg, u32 val)
335*4882a593Smuzhiyun {
336*4882a593Smuzhiyun 	iowrite32(val, reg);
337*4882a593Smuzhiyun }
338*4882a593Smuzhiyun #endif
339*4882a593Smuzhiyun 
grcan_clear_bits(u32 __iomem * reg,u32 mask)340*4882a593Smuzhiyun static inline void grcan_clear_bits(u32 __iomem *reg, u32 mask)
341*4882a593Smuzhiyun {
342*4882a593Smuzhiyun 	grcan_write_reg(reg, grcan_read_reg(reg) & ~mask);
343*4882a593Smuzhiyun }
344*4882a593Smuzhiyun 
grcan_set_bits(u32 __iomem * reg,u32 mask)345*4882a593Smuzhiyun static inline void grcan_set_bits(u32 __iomem *reg, u32 mask)
346*4882a593Smuzhiyun {
347*4882a593Smuzhiyun 	grcan_write_reg(reg, grcan_read_reg(reg) | mask);
348*4882a593Smuzhiyun }
349*4882a593Smuzhiyun 
grcan_read_bits(u32 __iomem * reg,u32 mask)350*4882a593Smuzhiyun static inline u32 grcan_read_bits(u32 __iomem *reg, u32 mask)
351*4882a593Smuzhiyun {
352*4882a593Smuzhiyun 	return grcan_read_reg(reg) & mask;
353*4882a593Smuzhiyun }
354*4882a593Smuzhiyun 
grcan_write_bits(u32 __iomem * reg,u32 value,u32 mask)355*4882a593Smuzhiyun static inline void grcan_write_bits(u32 __iomem *reg, u32 value, u32 mask)
356*4882a593Smuzhiyun {
357*4882a593Smuzhiyun 	u32 old = grcan_read_reg(reg);
358*4882a593Smuzhiyun 
359*4882a593Smuzhiyun 	grcan_write_reg(reg, (old & ~mask) | (value & mask));
360*4882a593Smuzhiyun }
361*4882a593Smuzhiyun 
362*4882a593Smuzhiyun /* a and b should both be in [0,size] and a == b == size should not hold */
grcan_ring_add(u32 a,u32 b,u32 size)363*4882a593Smuzhiyun static inline u32 grcan_ring_add(u32 a, u32 b, u32 size)
364*4882a593Smuzhiyun {
365*4882a593Smuzhiyun 	u32 sum = a + b;
366*4882a593Smuzhiyun 
367*4882a593Smuzhiyun 	if (sum < size)
368*4882a593Smuzhiyun 		return sum;
369*4882a593Smuzhiyun 	else
370*4882a593Smuzhiyun 		return sum - size;
371*4882a593Smuzhiyun }
372*4882a593Smuzhiyun 
373*4882a593Smuzhiyun /* a and b should both be in [0,size) */
grcan_ring_sub(u32 a,u32 b,u32 size)374*4882a593Smuzhiyun static inline u32 grcan_ring_sub(u32 a, u32 b, u32 size)
375*4882a593Smuzhiyun {
376*4882a593Smuzhiyun 	return grcan_ring_add(a, size - b, size);
377*4882a593Smuzhiyun }
378*4882a593Smuzhiyun 
379*4882a593Smuzhiyun /* Available slots for new transmissions */
grcan_txspace(size_t txsize,u32 txwr,u32 eskbp)380*4882a593Smuzhiyun static inline u32 grcan_txspace(size_t txsize, u32 txwr, u32 eskbp)
381*4882a593Smuzhiyun {
382*4882a593Smuzhiyun 	u32 slots = txsize / GRCAN_MSG_SIZE - 1;
383*4882a593Smuzhiyun 	u32 used = grcan_ring_sub(txwr, eskbp, txsize) / GRCAN_MSG_SIZE;
384*4882a593Smuzhiyun 
385*4882a593Smuzhiyun 	return slots - used;
386*4882a593Smuzhiyun }
387*4882a593Smuzhiyun 
388*4882a593Smuzhiyun /* Configuration parameters that can be set via module parameters */
389*4882a593Smuzhiyun static struct grcan_device_config grcan_module_config =
390*4882a593Smuzhiyun 	GRCAN_DEFAULT_DEVICE_CONFIG;
391*4882a593Smuzhiyun 
392*4882a593Smuzhiyun static const struct can_bittiming_const grcan_bittiming_const = {
393*4882a593Smuzhiyun 	.name		= DRV_NAME,
394*4882a593Smuzhiyun 	.tseg1_min	= GRCAN_CONF_PS1_MIN + 1,
395*4882a593Smuzhiyun 	.tseg1_max	= GRCAN_CONF_PS1_MAX + 1,
396*4882a593Smuzhiyun 	.tseg2_min	= GRCAN_CONF_PS2_MIN,
397*4882a593Smuzhiyun 	.tseg2_max	= GRCAN_CONF_PS2_MAX,
398*4882a593Smuzhiyun 	.sjw_max	= GRCAN_CONF_RSJ_MAX,
399*4882a593Smuzhiyun 	.brp_min	= GRCAN_CONF_SCALER_MIN + 1,
400*4882a593Smuzhiyun 	.brp_max	= GRCAN_CONF_SCALER_MAX + 1,
401*4882a593Smuzhiyun 	.brp_inc	= GRCAN_CONF_SCALER_INC,
402*4882a593Smuzhiyun };
403*4882a593Smuzhiyun 
grcan_set_bittiming(struct net_device * dev)404*4882a593Smuzhiyun static int grcan_set_bittiming(struct net_device *dev)
405*4882a593Smuzhiyun {
406*4882a593Smuzhiyun 	struct grcan_priv *priv = netdev_priv(dev);
407*4882a593Smuzhiyun 	struct grcan_registers __iomem *regs = priv->regs;
408*4882a593Smuzhiyun 	struct can_bittiming *bt = &priv->can.bittiming;
409*4882a593Smuzhiyun 	u32 timing = 0;
410*4882a593Smuzhiyun 	int bpr, rsj, ps1, ps2, scaler;
411*4882a593Smuzhiyun 
412*4882a593Smuzhiyun 	/* Should never happen - function will not be called when
413*4882a593Smuzhiyun 	 * device is up
414*4882a593Smuzhiyun 	 */
415*4882a593Smuzhiyun 	if (grcan_read_bits(&regs->ctrl, GRCAN_CTRL_ENABLE))
416*4882a593Smuzhiyun 		return -EBUSY;
417*4882a593Smuzhiyun 
418*4882a593Smuzhiyun 	bpr = 0; /* Note bpr and brp are different concepts */
419*4882a593Smuzhiyun 	rsj = bt->sjw;
420*4882a593Smuzhiyun 	ps1 = (bt->prop_seg + bt->phase_seg1) - 1; /* tseg1 - 1 */
421*4882a593Smuzhiyun 	ps2 = bt->phase_seg2;
422*4882a593Smuzhiyun 	scaler = (bt->brp - 1);
423*4882a593Smuzhiyun 	netdev_dbg(dev, "Request for BPR=%d, RSJ=%d, PS1=%d, PS2=%d, SCALER=%d",
424*4882a593Smuzhiyun 		   bpr, rsj, ps1, ps2, scaler);
425*4882a593Smuzhiyun 	if (!(ps1 > ps2)) {
426*4882a593Smuzhiyun 		netdev_err(dev, "PS1 > PS2 must hold: PS1=%d, PS2=%d\n",
427*4882a593Smuzhiyun 			   ps1, ps2);
428*4882a593Smuzhiyun 		return -EINVAL;
429*4882a593Smuzhiyun 	}
430*4882a593Smuzhiyun 	if (!(ps2 >= rsj)) {
431*4882a593Smuzhiyun 		netdev_err(dev, "PS2 >= RSJ must hold: PS2=%d, RSJ=%d\n",
432*4882a593Smuzhiyun 			   ps2, rsj);
433*4882a593Smuzhiyun 		return -EINVAL;
434*4882a593Smuzhiyun 	}
435*4882a593Smuzhiyun 
436*4882a593Smuzhiyun 	timing |= (bpr << GRCAN_CONF_BPR_BIT) & GRCAN_CONF_BPR;
437*4882a593Smuzhiyun 	timing |= (rsj << GRCAN_CONF_RSJ_BIT) & GRCAN_CONF_RSJ;
438*4882a593Smuzhiyun 	timing |= (ps1 << GRCAN_CONF_PS1_BIT) & GRCAN_CONF_PS1;
439*4882a593Smuzhiyun 	timing |= (ps2 << GRCAN_CONF_PS2_BIT) & GRCAN_CONF_PS2;
440*4882a593Smuzhiyun 	timing |= (scaler << GRCAN_CONF_SCALER_BIT) & GRCAN_CONF_SCALER;
441*4882a593Smuzhiyun 	netdev_info(dev, "setting timing=0x%x\n", timing);
442*4882a593Smuzhiyun 	grcan_write_bits(&regs->conf, timing, GRCAN_CONF_TIMING);
443*4882a593Smuzhiyun 
444*4882a593Smuzhiyun 	return 0;
445*4882a593Smuzhiyun }
446*4882a593Smuzhiyun 
grcan_get_berr_counter(const struct net_device * dev,struct can_berr_counter * bec)447*4882a593Smuzhiyun static int grcan_get_berr_counter(const struct net_device *dev,
448*4882a593Smuzhiyun 				  struct can_berr_counter *bec)
449*4882a593Smuzhiyun {
450*4882a593Smuzhiyun 	struct grcan_priv *priv = netdev_priv(dev);
451*4882a593Smuzhiyun 	struct grcan_registers __iomem *regs = priv->regs;
452*4882a593Smuzhiyun 	u32 status = grcan_read_reg(&regs->stat);
453*4882a593Smuzhiyun 
454*4882a593Smuzhiyun 	bec->txerr = (status & GRCAN_STAT_TXERRCNT) >> GRCAN_STAT_TXERRCNT_BIT;
455*4882a593Smuzhiyun 	bec->rxerr = (status & GRCAN_STAT_RXERRCNT) >> GRCAN_STAT_RXERRCNT_BIT;
456*4882a593Smuzhiyun 	return 0;
457*4882a593Smuzhiyun }
458*4882a593Smuzhiyun 
459*4882a593Smuzhiyun static int grcan_poll(struct napi_struct *napi, int budget);
460*4882a593Smuzhiyun 
461*4882a593Smuzhiyun /* Reset device, but keep configuration information */
grcan_reset(struct net_device * dev)462*4882a593Smuzhiyun static void grcan_reset(struct net_device *dev)
463*4882a593Smuzhiyun {
464*4882a593Smuzhiyun 	struct grcan_priv *priv = netdev_priv(dev);
465*4882a593Smuzhiyun 	struct grcan_registers __iomem *regs = priv->regs;
466*4882a593Smuzhiyun 	u32 config = grcan_read_reg(&regs->conf);
467*4882a593Smuzhiyun 
468*4882a593Smuzhiyun 	grcan_set_bits(&regs->ctrl, GRCAN_CTRL_RESET);
469*4882a593Smuzhiyun 	grcan_write_reg(&regs->conf, config);
470*4882a593Smuzhiyun 
471*4882a593Smuzhiyun 	priv->eskbp = grcan_read_reg(&regs->txrd);
472*4882a593Smuzhiyun 	priv->can.state = CAN_STATE_STOPPED;
473*4882a593Smuzhiyun 
474*4882a593Smuzhiyun 	/* Turn off hardware filtering - regs->rxcode set to 0 by reset */
475*4882a593Smuzhiyun 	grcan_write_reg(&regs->rxmask, 0);
476*4882a593Smuzhiyun }
477*4882a593Smuzhiyun 
478*4882a593Smuzhiyun /* stop device without changing any configurations */
grcan_stop_hardware(struct net_device * dev)479*4882a593Smuzhiyun static void grcan_stop_hardware(struct net_device *dev)
480*4882a593Smuzhiyun {
481*4882a593Smuzhiyun 	struct grcan_priv *priv = netdev_priv(dev);
482*4882a593Smuzhiyun 	struct grcan_registers __iomem *regs = priv->regs;
483*4882a593Smuzhiyun 
484*4882a593Smuzhiyun 	grcan_write_reg(&regs->imr, GRCAN_IRQ_NONE);
485*4882a593Smuzhiyun 	grcan_clear_bits(&regs->txctrl, GRCAN_TXCTRL_ENABLE);
486*4882a593Smuzhiyun 	grcan_clear_bits(&regs->rxctrl, GRCAN_RXCTRL_ENABLE);
487*4882a593Smuzhiyun 	grcan_clear_bits(&regs->ctrl, GRCAN_CTRL_ENABLE);
488*4882a593Smuzhiyun }
489*4882a593Smuzhiyun 
490*4882a593Smuzhiyun /* Let priv->eskbp catch up to regs->txrd and echo back the skbs if echo
491*4882a593Smuzhiyun  * is true and free them otherwise.
492*4882a593Smuzhiyun  *
493*4882a593Smuzhiyun  * If budget is >= 0, stop after handling at most budget skbs. Otherwise,
494*4882a593Smuzhiyun  * continue until priv->eskbp catches up to regs->txrd.
495*4882a593Smuzhiyun  *
496*4882a593Smuzhiyun  * priv->lock *must* be held when calling this function
497*4882a593Smuzhiyun  */
catch_up_echo_skb(struct net_device * dev,int budget,bool echo)498*4882a593Smuzhiyun static int catch_up_echo_skb(struct net_device *dev, int budget, bool echo)
499*4882a593Smuzhiyun {
500*4882a593Smuzhiyun 	struct grcan_priv *priv = netdev_priv(dev);
501*4882a593Smuzhiyun 	struct grcan_registers __iomem *regs = priv->regs;
502*4882a593Smuzhiyun 	struct grcan_dma *dma = &priv->dma;
503*4882a593Smuzhiyun 	struct net_device_stats *stats = &dev->stats;
504*4882a593Smuzhiyun 	int i, work_done;
505*4882a593Smuzhiyun 
506*4882a593Smuzhiyun 	/* Updates to priv->eskbp and wake-ups of the queue needs to
507*4882a593Smuzhiyun 	 * be atomic towards the reads of priv->eskbp and shut-downs
508*4882a593Smuzhiyun 	 * of the queue in grcan_start_xmit.
509*4882a593Smuzhiyun 	 */
510*4882a593Smuzhiyun 	u32 txrd = grcan_read_reg(&regs->txrd);
511*4882a593Smuzhiyun 
512*4882a593Smuzhiyun 	for (work_done = 0; work_done < budget || budget < 0; work_done++) {
513*4882a593Smuzhiyun 		if (priv->eskbp == txrd)
514*4882a593Smuzhiyun 			break;
515*4882a593Smuzhiyun 		i = priv->eskbp / GRCAN_MSG_SIZE;
516*4882a593Smuzhiyun 		if (echo) {
517*4882a593Smuzhiyun 			/* Normal echo of messages */
518*4882a593Smuzhiyun 			stats->tx_packets++;
519*4882a593Smuzhiyun 			stats->tx_bytes += priv->txdlc[i];
520*4882a593Smuzhiyun 			priv->txdlc[i] = 0;
521*4882a593Smuzhiyun 			can_get_echo_skb(dev, i);
522*4882a593Smuzhiyun 		} else {
523*4882a593Smuzhiyun 			/* For cleanup of untransmitted messages */
524*4882a593Smuzhiyun 			can_free_echo_skb(dev, i);
525*4882a593Smuzhiyun 		}
526*4882a593Smuzhiyun 
527*4882a593Smuzhiyun 		priv->eskbp = grcan_ring_add(priv->eskbp, GRCAN_MSG_SIZE,
528*4882a593Smuzhiyun 					     dma->tx.size);
529*4882a593Smuzhiyun 		txrd = grcan_read_reg(&regs->txrd);
530*4882a593Smuzhiyun 	}
531*4882a593Smuzhiyun 	return work_done;
532*4882a593Smuzhiyun }
533*4882a593Smuzhiyun 
grcan_lost_one_shot_frame(struct net_device * dev)534*4882a593Smuzhiyun static void grcan_lost_one_shot_frame(struct net_device *dev)
535*4882a593Smuzhiyun {
536*4882a593Smuzhiyun 	struct grcan_priv *priv = netdev_priv(dev);
537*4882a593Smuzhiyun 	struct grcan_registers __iomem *regs = priv->regs;
538*4882a593Smuzhiyun 	struct grcan_dma *dma = &priv->dma;
539*4882a593Smuzhiyun 	u32 txrd;
540*4882a593Smuzhiyun 	unsigned long flags;
541*4882a593Smuzhiyun 
542*4882a593Smuzhiyun 	spin_lock_irqsave(&priv->lock, flags);
543*4882a593Smuzhiyun 
544*4882a593Smuzhiyun 	catch_up_echo_skb(dev, -1, true);
545*4882a593Smuzhiyun 
546*4882a593Smuzhiyun 	if (unlikely(grcan_read_bits(&regs->txctrl, GRCAN_TXCTRL_ENABLE))) {
547*4882a593Smuzhiyun 		/* Should never happen */
548*4882a593Smuzhiyun 		netdev_err(dev, "TXCTRL enabled at TXLOSS in one shot mode\n");
549*4882a593Smuzhiyun 	} else {
550*4882a593Smuzhiyun 		/* By the time an GRCAN_IRQ_TXLOSS is generated in
551*4882a593Smuzhiyun 		 * one-shot mode there is no problem in writing
552*4882a593Smuzhiyun 		 * to TXRD even in versions of the hardware in
553*4882a593Smuzhiyun 		 * which GRCAN_TXCTRL_ONGOING is not cleared properly
554*4882a593Smuzhiyun 		 * in one-shot mode.
555*4882a593Smuzhiyun 		 */
556*4882a593Smuzhiyun 
557*4882a593Smuzhiyun 		/* Skip message and discard echo-skb */
558*4882a593Smuzhiyun 		txrd = grcan_read_reg(&regs->txrd);
559*4882a593Smuzhiyun 		txrd = grcan_ring_add(txrd, GRCAN_MSG_SIZE, dma->tx.size);
560*4882a593Smuzhiyun 		grcan_write_reg(&regs->txrd, txrd);
561*4882a593Smuzhiyun 		catch_up_echo_skb(dev, -1, false);
562*4882a593Smuzhiyun 
563*4882a593Smuzhiyun 		if (!priv->resetting && !priv->closing &&
564*4882a593Smuzhiyun 		    !(priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)) {
565*4882a593Smuzhiyun 			netif_wake_queue(dev);
566*4882a593Smuzhiyun 			grcan_set_bits(&regs->txctrl, GRCAN_TXCTRL_ENABLE);
567*4882a593Smuzhiyun 		}
568*4882a593Smuzhiyun 	}
569*4882a593Smuzhiyun 
570*4882a593Smuzhiyun 	spin_unlock_irqrestore(&priv->lock, flags);
571*4882a593Smuzhiyun }
572*4882a593Smuzhiyun 
grcan_err(struct net_device * dev,u32 sources,u32 status)573*4882a593Smuzhiyun static void grcan_err(struct net_device *dev, u32 sources, u32 status)
574*4882a593Smuzhiyun {
575*4882a593Smuzhiyun 	struct grcan_priv *priv = netdev_priv(dev);
576*4882a593Smuzhiyun 	struct grcan_registers __iomem *regs = priv->regs;
577*4882a593Smuzhiyun 	struct grcan_dma *dma = &priv->dma;
578*4882a593Smuzhiyun 	struct net_device_stats *stats = &dev->stats;
579*4882a593Smuzhiyun 	struct can_frame cf;
580*4882a593Smuzhiyun 
581*4882a593Smuzhiyun 	/* Zero potential error_frame */
582*4882a593Smuzhiyun 	memset(&cf, 0, sizeof(cf));
583*4882a593Smuzhiyun 
584*4882a593Smuzhiyun 	/* Message lost interrupt. This might be due to arbitration error, but
585*4882a593Smuzhiyun 	 * is also triggered when there is no one else on the can bus or when
586*4882a593Smuzhiyun 	 * there is a problem with the hardware interface or the bus itself. As
587*4882a593Smuzhiyun 	 * arbitration errors can not be singled out, no error frames are
588*4882a593Smuzhiyun 	 * generated reporting this event as an arbitration error.
589*4882a593Smuzhiyun 	 */
590*4882a593Smuzhiyun 	if (sources & GRCAN_IRQ_TXLOSS) {
591*4882a593Smuzhiyun 		/* Take care of failed one-shot transmit */
592*4882a593Smuzhiyun 		if (priv->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT)
593*4882a593Smuzhiyun 			grcan_lost_one_shot_frame(dev);
594*4882a593Smuzhiyun 
595*4882a593Smuzhiyun 		/* Stop printing as soon as error passive or bus off is in
596*4882a593Smuzhiyun 		 * effect to limit the amount of txloss debug printouts.
597*4882a593Smuzhiyun 		 */
598*4882a593Smuzhiyun 		if (!(status & GRCAN_STAT_ERRCTR_RELATED)) {
599*4882a593Smuzhiyun 			netdev_dbg(dev, "tx message lost\n");
600*4882a593Smuzhiyun 			stats->tx_errors++;
601*4882a593Smuzhiyun 		}
602*4882a593Smuzhiyun 	}
603*4882a593Smuzhiyun 
604*4882a593Smuzhiyun 	/* Conditions dealing with the error counters. There is no interrupt for
605*4882a593Smuzhiyun 	 * error warning, but there are interrupts for increases of the error
606*4882a593Smuzhiyun 	 * counters.
607*4882a593Smuzhiyun 	 */
608*4882a593Smuzhiyun 	if ((sources & GRCAN_IRQ_ERRCTR_RELATED) ||
609*4882a593Smuzhiyun 	    (status & GRCAN_STAT_ERRCTR_RELATED)) {
610*4882a593Smuzhiyun 		enum can_state state = priv->can.state;
611*4882a593Smuzhiyun 		enum can_state oldstate = state;
612*4882a593Smuzhiyun 		u32 txerr = (status & GRCAN_STAT_TXERRCNT)
613*4882a593Smuzhiyun 			>> GRCAN_STAT_TXERRCNT_BIT;
614*4882a593Smuzhiyun 		u32 rxerr = (status & GRCAN_STAT_RXERRCNT)
615*4882a593Smuzhiyun 			>> GRCAN_STAT_RXERRCNT_BIT;
616*4882a593Smuzhiyun 
617*4882a593Smuzhiyun 		/* Figure out current state */
618*4882a593Smuzhiyun 		if (status & GRCAN_STAT_OFF) {
619*4882a593Smuzhiyun 			state = CAN_STATE_BUS_OFF;
620*4882a593Smuzhiyun 		} else if (status & GRCAN_STAT_PASS) {
621*4882a593Smuzhiyun 			state = CAN_STATE_ERROR_PASSIVE;
622*4882a593Smuzhiyun 		} else if (txerr >= GRCAN_STAT_ERRCNT_WARNING_LIMIT ||
623*4882a593Smuzhiyun 			   rxerr >= GRCAN_STAT_ERRCNT_WARNING_LIMIT) {
624*4882a593Smuzhiyun 			state = CAN_STATE_ERROR_WARNING;
625*4882a593Smuzhiyun 		} else {
626*4882a593Smuzhiyun 			state = CAN_STATE_ERROR_ACTIVE;
627*4882a593Smuzhiyun 		}
628*4882a593Smuzhiyun 
629*4882a593Smuzhiyun 		/* Handle and report state changes */
630*4882a593Smuzhiyun 		if (state != oldstate) {
631*4882a593Smuzhiyun 			switch (state) {
632*4882a593Smuzhiyun 			case CAN_STATE_BUS_OFF:
633*4882a593Smuzhiyun 				netdev_dbg(dev, "bus-off\n");
634*4882a593Smuzhiyun 				netif_carrier_off(dev);
635*4882a593Smuzhiyun 				priv->can.can_stats.bus_off++;
636*4882a593Smuzhiyun 
637*4882a593Smuzhiyun 				/* Prevent the hardware from recovering from bus
638*4882a593Smuzhiyun 				 * off on its own if restart is disabled.
639*4882a593Smuzhiyun 				 */
640*4882a593Smuzhiyun 				if (!priv->can.restart_ms)
641*4882a593Smuzhiyun 					grcan_stop_hardware(dev);
642*4882a593Smuzhiyun 
643*4882a593Smuzhiyun 				cf.can_id |= CAN_ERR_BUSOFF;
644*4882a593Smuzhiyun 				break;
645*4882a593Smuzhiyun 
646*4882a593Smuzhiyun 			case CAN_STATE_ERROR_PASSIVE:
647*4882a593Smuzhiyun 				netdev_dbg(dev, "Error passive condition\n");
648*4882a593Smuzhiyun 				priv->can.can_stats.error_passive++;
649*4882a593Smuzhiyun 
650*4882a593Smuzhiyun 				cf.can_id |= CAN_ERR_CRTL;
651*4882a593Smuzhiyun 				if (txerr >= GRCAN_STAT_ERRCNT_PASSIVE_LIMIT)
652*4882a593Smuzhiyun 					cf.data[1] |= CAN_ERR_CRTL_TX_PASSIVE;
653*4882a593Smuzhiyun 				if (rxerr >= GRCAN_STAT_ERRCNT_PASSIVE_LIMIT)
654*4882a593Smuzhiyun 					cf.data[1] |= CAN_ERR_CRTL_RX_PASSIVE;
655*4882a593Smuzhiyun 				break;
656*4882a593Smuzhiyun 
657*4882a593Smuzhiyun 			case CAN_STATE_ERROR_WARNING:
658*4882a593Smuzhiyun 				netdev_dbg(dev, "Error warning condition\n");
659*4882a593Smuzhiyun 				priv->can.can_stats.error_warning++;
660*4882a593Smuzhiyun 
661*4882a593Smuzhiyun 				cf.can_id |= CAN_ERR_CRTL;
662*4882a593Smuzhiyun 				if (txerr >= GRCAN_STAT_ERRCNT_WARNING_LIMIT)
663*4882a593Smuzhiyun 					cf.data[1] |= CAN_ERR_CRTL_TX_WARNING;
664*4882a593Smuzhiyun 				if (rxerr >= GRCAN_STAT_ERRCNT_WARNING_LIMIT)
665*4882a593Smuzhiyun 					cf.data[1] |= CAN_ERR_CRTL_RX_WARNING;
666*4882a593Smuzhiyun 				break;
667*4882a593Smuzhiyun 
668*4882a593Smuzhiyun 			case CAN_STATE_ERROR_ACTIVE:
669*4882a593Smuzhiyun 				netdev_dbg(dev, "Error active condition\n");
670*4882a593Smuzhiyun 				cf.can_id |= CAN_ERR_CRTL;
671*4882a593Smuzhiyun 				break;
672*4882a593Smuzhiyun 
673*4882a593Smuzhiyun 			default:
674*4882a593Smuzhiyun 				/* There are no others at this point */
675*4882a593Smuzhiyun 				break;
676*4882a593Smuzhiyun 			}
677*4882a593Smuzhiyun 			cf.data[6] = txerr;
678*4882a593Smuzhiyun 			cf.data[7] = rxerr;
679*4882a593Smuzhiyun 			priv->can.state = state;
680*4882a593Smuzhiyun 		}
681*4882a593Smuzhiyun 
682*4882a593Smuzhiyun 		/* Report automatic restarts */
683*4882a593Smuzhiyun 		if (priv->can.restart_ms && oldstate == CAN_STATE_BUS_OFF) {
684*4882a593Smuzhiyun 			unsigned long flags;
685*4882a593Smuzhiyun 
686*4882a593Smuzhiyun 			cf.can_id |= CAN_ERR_RESTARTED;
687*4882a593Smuzhiyun 			netdev_dbg(dev, "restarted\n");
688*4882a593Smuzhiyun 			priv->can.can_stats.restarts++;
689*4882a593Smuzhiyun 			netif_carrier_on(dev);
690*4882a593Smuzhiyun 
691*4882a593Smuzhiyun 			spin_lock_irqsave(&priv->lock, flags);
692*4882a593Smuzhiyun 
693*4882a593Smuzhiyun 			if (!priv->resetting && !priv->closing) {
694*4882a593Smuzhiyun 				u32 txwr = grcan_read_reg(&regs->txwr);
695*4882a593Smuzhiyun 
696*4882a593Smuzhiyun 				if (grcan_txspace(dma->tx.size, txwr,
697*4882a593Smuzhiyun 						  priv->eskbp))
698*4882a593Smuzhiyun 					netif_wake_queue(dev);
699*4882a593Smuzhiyun 			}
700*4882a593Smuzhiyun 
701*4882a593Smuzhiyun 			spin_unlock_irqrestore(&priv->lock, flags);
702*4882a593Smuzhiyun 		}
703*4882a593Smuzhiyun 	}
704*4882a593Smuzhiyun 
705*4882a593Smuzhiyun 	/* Data overrun interrupt */
706*4882a593Smuzhiyun 	if ((sources & GRCAN_IRQ_OR) || (status & GRCAN_STAT_OR)) {
707*4882a593Smuzhiyun 		netdev_dbg(dev, "got data overrun interrupt\n");
708*4882a593Smuzhiyun 		stats->rx_over_errors++;
709*4882a593Smuzhiyun 		stats->rx_errors++;
710*4882a593Smuzhiyun 
711*4882a593Smuzhiyun 		cf.can_id |= CAN_ERR_CRTL;
712*4882a593Smuzhiyun 		cf.data[1] |= CAN_ERR_CRTL_RX_OVERFLOW;
713*4882a593Smuzhiyun 	}
714*4882a593Smuzhiyun 
715*4882a593Smuzhiyun 	/* AHB bus error interrupts (not CAN bus errors) - shut down the
716*4882a593Smuzhiyun 	 * device.
717*4882a593Smuzhiyun 	 */
718*4882a593Smuzhiyun 	if (sources & (GRCAN_IRQ_TXAHBERR | GRCAN_IRQ_RXAHBERR) ||
719*4882a593Smuzhiyun 	    (status & GRCAN_STAT_AHBERR)) {
720*4882a593Smuzhiyun 		char *txrx = "";
721*4882a593Smuzhiyun 		unsigned long flags;
722*4882a593Smuzhiyun 
723*4882a593Smuzhiyun 		if (sources & GRCAN_IRQ_TXAHBERR) {
724*4882a593Smuzhiyun 			txrx = "on tx ";
725*4882a593Smuzhiyun 			stats->tx_errors++;
726*4882a593Smuzhiyun 		} else if (sources & GRCAN_IRQ_RXAHBERR) {
727*4882a593Smuzhiyun 			txrx = "on rx ";
728*4882a593Smuzhiyun 			stats->rx_errors++;
729*4882a593Smuzhiyun 		}
730*4882a593Smuzhiyun 		netdev_err(dev, "Fatal AHB bus error %s- halting device\n",
731*4882a593Smuzhiyun 			   txrx);
732*4882a593Smuzhiyun 
733*4882a593Smuzhiyun 		spin_lock_irqsave(&priv->lock, flags);
734*4882a593Smuzhiyun 
735*4882a593Smuzhiyun 		/* Prevent anything to be enabled again and halt device */
736*4882a593Smuzhiyun 		priv->closing = true;
737*4882a593Smuzhiyun 		netif_stop_queue(dev);
738*4882a593Smuzhiyun 		grcan_stop_hardware(dev);
739*4882a593Smuzhiyun 		priv->can.state = CAN_STATE_STOPPED;
740*4882a593Smuzhiyun 
741*4882a593Smuzhiyun 		spin_unlock_irqrestore(&priv->lock, flags);
742*4882a593Smuzhiyun 	}
743*4882a593Smuzhiyun 
744*4882a593Smuzhiyun 	/* Pass on error frame if something to report,
745*4882a593Smuzhiyun 	 * i.e. id contains some information
746*4882a593Smuzhiyun 	 */
747*4882a593Smuzhiyun 	if (cf.can_id) {
748*4882a593Smuzhiyun 		struct can_frame *skb_cf;
749*4882a593Smuzhiyun 		struct sk_buff *skb = alloc_can_err_skb(dev, &skb_cf);
750*4882a593Smuzhiyun 
751*4882a593Smuzhiyun 		if (skb == NULL) {
752*4882a593Smuzhiyun 			netdev_dbg(dev, "could not allocate error frame\n");
753*4882a593Smuzhiyun 			return;
754*4882a593Smuzhiyun 		}
755*4882a593Smuzhiyun 		skb_cf->can_id |= cf.can_id;
756*4882a593Smuzhiyun 		memcpy(skb_cf->data, cf.data, sizeof(cf.data));
757*4882a593Smuzhiyun 
758*4882a593Smuzhiyun 		netif_rx(skb);
759*4882a593Smuzhiyun 	}
760*4882a593Smuzhiyun }
761*4882a593Smuzhiyun 
grcan_interrupt(int irq,void * dev_id)762*4882a593Smuzhiyun static irqreturn_t grcan_interrupt(int irq, void *dev_id)
763*4882a593Smuzhiyun {
764*4882a593Smuzhiyun 	struct net_device *dev = dev_id;
765*4882a593Smuzhiyun 	struct grcan_priv *priv = netdev_priv(dev);
766*4882a593Smuzhiyun 	struct grcan_registers __iomem *regs = priv->regs;
767*4882a593Smuzhiyun 	u32 sources, status;
768*4882a593Smuzhiyun 
769*4882a593Smuzhiyun 	/* Find out the source */
770*4882a593Smuzhiyun 	sources = grcan_read_reg(&regs->pimsr);
771*4882a593Smuzhiyun 	if (!sources)
772*4882a593Smuzhiyun 		return IRQ_NONE;
773*4882a593Smuzhiyun 	grcan_write_reg(&regs->picr, sources);
774*4882a593Smuzhiyun 	status = grcan_read_reg(&regs->stat);
775*4882a593Smuzhiyun 
776*4882a593Smuzhiyun 	/* If we got TX progress, the device has not hanged,
777*4882a593Smuzhiyun 	 * so disable the hang timer
778*4882a593Smuzhiyun 	 */
779*4882a593Smuzhiyun 	if (priv->need_txbug_workaround &&
780*4882a593Smuzhiyun 	    (sources & (GRCAN_IRQ_TX | GRCAN_IRQ_TXLOSS))) {
781*4882a593Smuzhiyun 		del_timer(&priv->hang_timer);
782*4882a593Smuzhiyun 	}
783*4882a593Smuzhiyun 
784*4882a593Smuzhiyun 	/* Frame(s) received or transmitted */
785*4882a593Smuzhiyun 	if (sources & (GRCAN_IRQ_TX | GRCAN_IRQ_RX)) {
786*4882a593Smuzhiyun 		/* Disable tx/rx interrupts and schedule poll(). No need for
787*4882a593Smuzhiyun 		 * locking as interference from a running reset at worst leads
788*4882a593Smuzhiyun 		 * to an extra interrupt.
789*4882a593Smuzhiyun 		 */
790*4882a593Smuzhiyun 		grcan_clear_bits(&regs->imr, GRCAN_IRQ_TX | GRCAN_IRQ_RX);
791*4882a593Smuzhiyun 		napi_schedule(&priv->napi);
792*4882a593Smuzhiyun 	}
793*4882a593Smuzhiyun 
794*4882a593Smuzhiyun 	/* (Potential) error conditions to take care of */
795*4882a593Smuzhiyun 	if (sources & GRCAN_IRQ_ERRORS)
796*4882a593Smuzhiyun 		grcan_err(dev, sources, status);
797*4882a593Smuzhiyun 
798*4882a593Smuzhiyun 	return IRQ_HANDLED;
799*4882a593Smuzhiyun }
800*4882a593Smuzhiyun 
801*4882a593Smuzhiyun /* Reset device and restart operations from where they were.
802*4882a593Smuzhiyun  *
803*4882a593Smuzhiyun  * This assumes that RXCTRL & RXCTRL is properly disabled and that RX
804*4882a593Smuzhiyun  * is not ONGOING (TX might be stuck in ONGOING due to a harwrware bug
805*4882a593Smuzhiyun  * for single shot)
806*4882a593Smuzhiyun  */
grcan_running_reset(struct timer_list * t)807*4882a593Smuzhiyun static void grcan_running_reset(struct timer_list *t)
808*4882a593Smuzhiyun {
809*4882a593Smuzhiyun 	struct grcan_priv *priv = from_timer(priv, t, rr_timer);
810*4882a593Smuzhiyun 	struct net_device *dev = priv->dev;
811*4882a593Smuzhiyun 	struct grcan_registers __iomem *regs = priv->regs;
812*4882a593Smuzhiyun 	unsigned long flags;
813*4882a593Smuzhiyun 
814*4882a593Smuzhiyun 	/* This temporarily messes with eskbp, so we need to lock
815*4882a593Smuzhiyun 	 * priv->lock
816*4882a593Smuzhiyun 	 */
817*4882a593Smuzhiyun 	spin_lock_irqsave(&priv->lock, flags);
818*4882a593Smuzhiyun 
819*4882a593Smuzhiyun 	priv->resetting = false;
820*4882a593Smuzhiyun 	del_timer(&priv->hang_timer);
821*4882a593Smuzhiyun 	del_timer(&priv->rr_timer);
822*4882a593Smuzhiyun 
823*4882a593Smuzhiyun 	if (!priv->closing) {
824*4882a593Smuzhiyun 		/* Save and reset - config register preserved by grcan_reset */
825*4882a593Smuzhiyun 		u32 imr = grcan_read_reg(&regs->imr);
826*4882a593Smuzhiyun 
827*4882a593Smuzhiyun 		u32 txaddr = grcan_read_reg(&regs->txaddr);
828*4882a593Smuzhiyun 		u32 txsize = grcan_read_reg(&regs->txsize);
829*4882a593Smuzhiyun 		u32 txwr = grcan_read_reg(&regs->txwr);
830*4882a593Smuzhiyun 		u32 txrd = grcan_read_reg(&regs->txrd);
831*4882a593Smuzhiyun 		u32 eskbp = priv->eskbp;
832*4882a593Smuzhiyun 
833*4882a593Smuzhiyun 		u32 rxaddr = grcan_read_reg(&regs->rxaddr);
834*4882a593Smuzhiyun 		u32 rxsize = grcan_read_reg(&regs->rxsize);
835*4882a593Smuzhiyun 		u32 rxwr = grcan_read_reg(&regs->rxwr);
836*4882a593Smuzhiyun 		u32 rxrd = grcan_read_reg(&regs->rxrd);
837*4882a593Smuzhiyun 
838*4882a593Smuzhiyun 		grcan_reset(dev);
839*4882a593Smuzhiyun 
840*4882a593Smuzhiyun 		/* Restore */
841*4882a593Smuzhiyun 		grcan_write_reg(&regs->txaddr, txaddr);
842*4882a593Smuzhiyun 		grcan_write_reg(&regs->txsize, txsize);
843*4882a593Smuzhiyun 		grcan_write_reg(&regs->txwr, txwr);
844*4882a593Smuzhiyun 		grcan_write_reg(&regs->txrd, txrd);
845*4882a593Smuzhiyun 		priv->eskbp = eskbp;
846*4882a593Smuzhiyun 
847*4882a593Smuzhiyun 		grcan_write_reg(&regs->rxaddr, rxaddr);
848*4882a593Smuzhiyun 		grcan_write_reg(&regs->rxsize, rxsize);
849*4882a593Smuzhiyun 		grcan_write_reg(&regs->rxwr, rxwr);
850*4882a593Smuzhiyun 		grcan_write_reg(&regs->rxrd, rxrd);
851*4882a593Smuzhiyun 
852*4882a593Smuzhiyun 		/* Turn on device again */
853*4882a593Smuzhiyun 		grcan_write_reg(&regs->imr, imr);
854*4882a593Smuzhiyun 		priv->can.state = CAN_STATE_ERROR_ACTIVE;
855*4882a593Smuzhiyun 		grcan_write_reg(&regs->txctrl, GRCAN_TXCTRL_ENABLE
856*4882a593Smuzhiyun 				| (priv->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT
857*4882a593Smuzhiyun 				   ? GRCAN_TXCTRL_SINGLE : 0));
858*4882a593Smuzhiyun 		grcan_write_reg(&regs->rxctrl, GRCAN_RXCTRL_ENABLE);
859*4882a593Smuzhiyun 		grcan_write_reg(&regs->ctrl, GRCAN_CTRL_ENABLE);
860*4882a593Smuzhiyun 
861*4882a593Smuzhiyun 		/* Start queue if there is size and listen-onle mode is not
862*4882a593Smuzhiyun 		 * enabled
863*4882a593Smuzhiyun 		 */
864*4882a593Smuzhiyun 		if (grcan_txspace(priv->dma.tx.size, txwr, priv->eskbp) &&
865*4882a593Smuzhiyun 		    !(priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY))
866*4882a593Smuzhiyun 			netif_wake_queue(dev);
867*4882a593Smuzhiyun 	}
868*4882a593Smuzhiyun 
869*4882a593Smuzhiyun 	spin_unlock_irqrestore(&priv->lock, flags);
870*4882a593Smuzhiyun 
871*4882a593Smuzhiyun 	netdev_err(dev, "Device reset and restored\n");
872*4882a593Smuzhiyun }
873*4882a593Smuzhiyun 
874*4882a593Smuzhiyun /* Waiting time in usecs corresponding to the transmission of three maximum
875*4882a593Smuzhiyun  * sized can frames in the given bitrate (in bits/sec). Waiting for this amount
876*4882a593Smuzhiyun  * of time makes sure that the can controller have time to finish sending or
877*4882a593Smuzhiyun  * receiving a frame with a good margin.
878*4882a593Smuzhiyun  *
879*4882a593Smuzhiyun  * usecs/sec * number of frames * bits/frame / bits/sec
880*4882a593Smuzhiyun  */
grcan_ongoing_wait_usecs(__u32 bitrate)881*4882a593Smuzhiyun static inline u32 grcan_ongoing_wait_usecs(__u32 bitrate)
882*4882a593Smuzhiyun {
883*4882a593Smuzhiyun 	return 1000000 * 3 * GRCAN_EFF_FRAME_MAX_BITS / bitrate;
884*4882a593Smuzhiyun }
885*4882a593Smuzhiyun 
886*4882a593Smuzhiyun /* Set timer so that it will not fire until after a period in which the can
887*4882a593Smuzhiyun  * controller have a good margin to finish transmitting a frame unless it has
888*4882a593Smuzhiyun  * hanged
889*4882a593Smuzhiyun  */
grcan_reset_timer(struct timer_list * timer,__u32 bitrate)890*4882a593Smuzhiyun static inline void grcan_reset_timer(struct timer_list *timer, __u32 bitrate)
891*4882a593Smuzhiyun {
892*4882a593Smuzhiyun 	u32 wait_jiffies = usecs_to_jiffies(grcan_ongoing_wait_usecs(bitrate));
893*4882a593Smuzhiyun 
894*4882a593Smuzhiyun 	mod_timer(timer, jiffies + wait_jiffies);
895*4882a593Smuzhiyun }
896*4882a593Smuzhiyun 
897*4882a593Smuzhiyun /* Disable channels and schedule a running reset */
grcan_initiate_running_reset(struct timer_list * t)898*4882a593Smuzhiyun static void grcan_initiate_running_reset(struct timer_list *t)
899*4882a593Smuzhiyun {
900*4882a593Smuzhiyun 	struct grcan_priv *priv = from_timer(priv, t, hang_timer);
901*4882a593Smuzhiyun 	struct net_device *dev = priv->dev;
902*4882a593Smuzhiyun 	struct grcan_registers __iomem *regs = priv->regs;
903*4882a593Smuzhiyun 	unsigned long flags;
904*4882a593Smuzhiyun 
905*4882a593Smuzhiyun 	netdev_err(dev, "Device seems hanged - reset scheduled\n");
906*4882a593Smuzhiyun 
907*4882a593Smuzhiyun 	spin_lock_irqsave(&priv->lock, flags);
908*4882a593Smuzhiyun 
909*4882a593Smuzhiyun 	/* The main body of this function must never be executed again
910*4882a593Smuzhiyun 	 * until after an execution of grcan_running_reset
911*4882a593Smuzhiyun 	 */
912*4882a593Smuzhiyun 	if (!priv->resetting && !priv->closing) {
913*4882a593Smuzhiyun 		priv->resetting = true;
914*4882a593Smuzhiyun 		netif_stop_queue(dev);
915*4882a593Smuzhiyun 		grcan_clear_bits(&regs->txctrl, GRCAN_TXCTRL_ENABLE);
916*4882a593Smuzhiyun 		grcan_clear_bits(&regs->rxctrl, GRCAN_RXCTRL_ENABLE);
917*4882a593Smuzhiyun 		grcan_reset_timer(&priv->rr_timer, priv->can.bittiming.bitrate);
918*4882a593Smuzhiyun 	}
919*4882a593Smuzhiyun 
920*4882a593Smuzhiyun 	spin_unlock_irqrestore(&priv->lock, flags);
921*4882a593Smuzhiyun }
922*4882a593Smuzhiyun 
grcan_free_dma_buffers(struct net_device * dev)923*4882a593Smuzhiyun static void grcan_free_dma_buffers(struct net_device *dev)
924*4882a593Smuzhiyun {
925*4882a593Smuzhiyun 	struct grcan_priv *priv = netdev_priv(dev);
926*4882a593Smuzhiyun 	struct grcan_dma *dma = &priv->dma;
927*4882a593Smuzhiyun 
928*4882a593Smuzhiyun 	dma_free_coherent(priv->ofdev_dev, dma->base_size, dma->base_buf,
929*4882a593Smuzhiyun 			  dma->base_handle);
930*4882a593Smuzhiyun 	memset(dma, 0, sizeof(*dma));
931*4882a593Smuzhiyun }
932*4882a593Smuzhiyun 
grcan_allocate_dma_buffers(struct net_device * dev,size_t tsize,size_t rsize)933*4882a593Smuzhiyun static int grcan_allocate_dma_buffers(struct net_device *dev,
934*4882a593Smuzhiyun 				      size_t tsize, size_t rsize)
935*4882a593Smuzhiyun {
936*4882a593Smuzhiyun 	struct grcan_priv *priv = netdev_priv(dev);
937*4882a593Smuzhiyun 	struct grcan_dma *dma = &priv->dma;
938*4882a593Smuzhiyun 	struct grcan_dma_buffer *large = rsize > tsize ? &dma->rx : &dma->tx;
939*4882a593Smuzhiyun 	struct grcan_dma_buffer *small = rsize > tsize ? &dma->tx : &dma->rx;
940*4882a593Smuzhiyun 	size_t shift;
941*4882a593Smuzhiyun 
942*4882a593Smuzhiyun 	/* Need a whole number of GRCAN_BUFFER_ALIGNMENT for the large,
943*4882a593Smuzhiyun 	 * i.e. first buffer
944*4882a593Smuzhiyun 	 */
945*4882a593Smuzhiyun 	size_t maxs = max(tsize, rsize);
946*4882a593Smuzhiyun 	size_t lsize = ALIGN(maxs, GRCAN_BUFFER_ALIGNMENT);
947*4882a593Smuzhiyun 
948*4882a593Smuzhiyun 	/* Put the small buffer after that */
949*4882a593Smuzhiyun 	size_t ssize = min(tsize, rsize);
950*4882a593Smuzhiyun 
951*4882a593Smuzhiyun 	/* Extra GRCAN_BUFFER_ALIGNMENT to allow for alignment */
952*4882a593Smuzhiyun 	dma->base_size = lsize + ssize + GRCAN_BUFFER_ALIGNMENT;
953*4882a593Smuzhiyun 	dma->base_buf = dma_alloc_coherent(priv->ofdev_dev,
954*4882a593Smuzhiyun 					   dma->base_size,
955*4882a593Smuzhiyun 					   &dma->base_handle,
956*4882a593Smuzhiyun 					   GFP_KERNEL);
957*4882a593Smuzhiyun 
958*4882a593Smuzhiyun 	if (!dma->base_buf)
959*4882a593Smuzhiyun 		return -ENOMEM;
960*4882a593Smuzhiyun 
961*4882a593Smuzhiyun 	dma->tx.size = tsize;
962*4882a593Smuzhiyun 	dma->rx.size = rsize;
963*4882a593Smuzhiyun 
964*4882a593Smuzhiyun 	large->handle = ALIGN(dma->base_handle, GRCAN_BUFFER_ALIGNMENT);
965*4882a593Smuzhiyun 	small->handle = large->handle + lsize;
966*4882a593Smuzhiyun 	shift = large->handle - dma->base_handle;
967*4882a593Smuzhiyun 
968*4882a593Smuzhiyun 	large->buf = dma->base_buf + shift;
969*4882a593Smuzhiyun 	small->buf = large->buf + lsize;
970*4882a593Smuzhiyun 
971*4882a593Smuzhiyun 	return 0;
972*4882a593Smuzhiyun }
973*4882a593Smuzhiyun 
974*4882a593Smuzhiyun /* priv->lock *must* be held when calling this function */
grcan_start(struct net_device * dev)975*4882a593Smuzhiyun static int grcan_start(struct net_device *dev)
976*4882a593Smuzhiyun {
977*4882a593Smuzhiyun 	struct grcan_priv *priv = netdev_priv(dev);
978*4882a593Smuzhiyun 	struct grcan_registers __iomem *regs = priv->regs;
979*4882a593Smuzhiyun 	u32 confop, txctrl;
980*4882a593Smuzhiyun 
981*4882a593Smuzhiyun 	grcan_reset(dev);
982*4882a593Smuzhiyun 
983*4882a593Smuzhiyun 	grcan_write_reg(&regs->txaddr, priv->dma.tx.handle);
984*4882a593Smuzhiyun 	grcan_write_reg(&regs->txsize, priv->dma.tx.size);
985*4882a593Smuzhiyun 	/* regs->txwr, regs->txrd and priv->eskbp already set to 0 by reset */
986*4882a593Smuzhiyun 
987*4882a593Smuzhiyun 	grcan_write_reg(&regs->rxaddr, priv->dma.rx.handle);
988*4882a593Smuzhiyun 	grcan_write_reg(&regs->rxsize, priv->dma.rx.size);
989*4882a593Smuzhiyun 	/* regs->rxwr and regs->rxrd already set to 0 by reset */
990*4882a593Smuzhiyun 
991*4882a593Smuzhiyun 	/* Enable interrupts */
992*4882a593Smuzhiyun 	grcan_read_reg(&regs->pir);
993*4882a593Smuzhiyun 	grcan_write_reg(&regs->imr, GRCAN_IRQ_DEFAULT);
994*4882a593Smuzhiyun 
995*4882a593Smuzhiyun 	/* Enable interfaces, channels and device */
996*4882a593Smuzhiyun 	confop = GRCAN_CONF_ABORT
997*4882a593Smuzhiyun 		| (priv->config.enable0 ? GRCAN_CONF_ENABLE0 : 0)
998*4882a593Smuzhiyun 		| (priv->config.enable1 ? GRCAN_CONF_ENABLE1 : 0)
999*4882a593Smuzhiyun 		| (priv->config.select ? GRCAN_CONF_SELECT : 0)
1000*4882a593Smuzhiyun 		| (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY ?
1001*4882a593Smuzhiyun 		   GRCAN_CONF_SILENT : 0)
1002*4882a593Smuzhiyun 		| (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES ?
1003*4882a593Smuzhiyun 		   GRCAN_CONF_SAM : 0);
1004*4882a593Smuzhiyun 	grcan_write_bits(&regs->conf, confop, GRCAN_CONF_OPERATION);
1005*4882a593Smuzhiyun 	txctrl = GRCAN_TXCTRL_ENABLE
1006*4882a593Smuzhiyun 		| (priv->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT
1007*4882a593Smuzhiyun 		   ? GRCAN_TXCTRL_SINGLE : 0);
1008*4882a593Smuzhiyun 	grcan_write_reg(&regs->txctrl, txctrl);
1009*4882a593Smuzhiyun 	grcan_write_reg(&regs->rxctrl, GRCAN_RXCTRL_ENABLE);
1010*4882a593Smuzhiyun 	grcan_write_reg(&regs->ctrl, GRCAN_CTRL_ENABLE);
1011*4882a593Smuzhiyun 
1012*4882a593Smuzhiyun 	priv->can.state = CAN_STATE_ERROR_ACTIVE;
1013*4882a593Smuzhiyun 
1014*4882a593Smuzhiyun 	return 0;
1015*4882a593Smuzhiyun }
1016*4882a593Smuzhiyun 
grcan_set_mode(struct net_device * dev,enum can_mode mode)1017*4882a593Smuzhiyun static int grcan_set_mode(struct net_device *dev, enum can_mode mode)
1018*4882a593Smuzhiyun {
1019*4882a593Smuzhiyun 	struct grcan_priv *priv = netdev_priv(dev);
1020*4882a593Smuzhiyun 	unsigned long flags;
1021*4882a593Smuzhiyun 	int err = 0;
1022*4882a593Smuzhiyun 
1023*4882a593Smuzhiyun 	if (mode == CAN_MODE_START) {
1024*4882a593Smuzhiyun 		/* This might be called to restart the device to recover from
1025*4882a593Smuzhiyun 		 * bus off errors
1026*4882a593Smuzhiyun 		 */
1027*4882a593Smuzhiyun 		spin_lock_irqsave(&priv->lock, flags);
1028*4882a593Smuzhiyun 		if (priv->closing || priv->resetting) {
1029*4882a593Smuzhiyun 			err = -EBUSY;
1030*4882a593Smuzhiyun 		} else {
1031*4882a593Smuzhiyun 			netdev_info(dev, "Restarting device\n");
1032*4882a593Smuzhiyun 			grcan_start(dev);
1033*4882a593Smuzhiyun 			if (!(priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY))
1034*4882a593Smuzhiyun 				netif_wake_queue(dev);
1035*4882a593Smuzhiyun 		}
1036*4882a593Smuzhiyun 		spin_unlock_irqrestore(&priv->lock, flags);
1037*4882a593Smuzhiyun 		return err;
1038*4882a593Smuzhiyun 	}
1039*4882a593Smuzhiyun 	return -EOPNOTSUPP;
1040*4882a593Smuzhiyun }
1041*4882a593Smuzhiyun 
grcan_open(struct net_device * dev)1042*4882a593Smuzhiyun static int grcan_open(struct net_device *dev)
1043*4882a593Smuzhiyun {
1044*4882a593Smuzhiyun 	struct grcan_priv *priv = netdev_priv(dev);
1045*4882a593Smuzhiyun 	struct grcan_dma *dma = &priv->dma;
1046*4882a593Smuzhiyun 	unsigned long flags;
1047*4882a593Smuzhiyun 	int err;
1048*4882a593Smuzhiyun 
1049*4882a593Smuzhiyun 	/* Allocate memory */
1050*4882a593Smuzhiyun 	err = grcan_allocate_dma_buffers(dev, priv->config.txsize,
1051*4882a593Smuzhiyun 					 priv->config.rxsize);
1052*4882a593Smuzhiyun 	if (err) {
1053*4882a593Smuzhiyun 		netdev_err(dev, "could not allocate DMA buffers\n");
1054*4882a593Smuzhiyun 		return err;
1055*4882a593Smuzhiyun 	}
1056*4882a593Smuzhiyun 
1057*4882a593Smuzhiyun 	priv->echo_skb = kcalloc(dma->tx.size, sizeof(*priv->echo_skb),
1058*4882a593Smuzhiyun 				 GFP_KERNEL);
1059*4882a593Smuzhiyun 	if (!priv->echo_skb) {
1060*4882a593Smuzhiyun 		err = -ENOMEM;
1061*4882a593Smuzhiyun 		goto exit_free_dma_buffers;
1062*4882a593Smuzhiyun 	}
1063*4882a593Smuzhiyun 	priv->can.echo_skb_max = dma->tx.size;
1064*4882a593Smuzhiyun 	priv->can.echo_skb = priv->echo_skb;
1065*4882a593Smuzhiyun 
1066*4882a593Smuzhiyun 	priv->txdlc = kcalloc(dma->tx.size, sizeof(*priv->txdlc), GFP_KERNEL);
1067*4882a593Smuzhiyun 	if (!priv->txdlc) {
1068*4882a593Smuzhiyun 		err = -ENOMEM;
1069*4882a593Smuzhiyun 		goto exit_free_echo_skb;
1070*4882a593Smuzhiyun 	}
1071*4882a593Smuzhiyun 
1072*4882a593Smuzhiyun 	/* Get can device up */
1073*4882a593Smuzhiyun 	err = open_candev(dev);
1074*4882a593Smuzhiyun 	if (err)
1075*4882a593Smuzhiyun 		goto exit_free_txdlc;
1076*4882a593Smuzhiyun 
1077*4882a593Smuzhiyun 	err = request_irq(dev->irq, grcan_interrupt, IRQF_SHARED,
1078*4882a593Smuzhiyun 			  dev->name, dev);
1079*4882a593Smuzhiyun 	if (err)
1080*4882a593Smuzhiyun 		goto exit_close_candev;
1081*4882a593Smuzhiyun 
1082*4882a593Smuzhiyun 	spin_lock_irqsave(&priv->lock, flags);
1083*4882a593Smuzhiyun 
1084*4882a593Smuzhiyun 	napi_enable(&priv->napi);
1085*4882a593Smuzhiyun 	grcan_start(dev);
1086*4882a593Smuzhiyun 	if (!(priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY))
1087*4882a593Smuzhiyun 		netif_start_queue(dev);
1088*4882a593Smuzhiyun 	priv->resetting = false;
1089*4882a593Smuzhiyun 	priv->closing = false;
1090*4882a593Smuzhiyun 
1091*4882a593Smuzhiyun 	spin_unlock_irqrestore(&priv->lock, flags);
1092*4882a593Smuzhiyun 
1093*4882a593Smuzhiyun 	return 0;
1094*4882a593Smuzhiyun 
1095*4882a593Smuzhiyun exit_close_candev:
1096*4882a593Smuzhiyun 	close_candev(dev);
1097*4882a593Smuzhiyun exit_free_txdlc:
1098*4882a593Smuzhiyun 	kfree(priv->txdlc);
1099*4882a593Smuzhiyun exit_free_echo_skb:
1100*4882a593Smuzhiyun 	kfree(priv->echo_skb);
1101*4882a593Smuzhiyun exit_free_dma_buffers:
1102*4882a593Smuzhiyun 	grcan_free_dma_buffers(dev);
1103*4882a593Smuzhiyun 	return err;
1104*4882a593Smuzhiyun }
1105*4882a593Smuzhiyun 
grcan_close(struct net_device * dev)1106*4882a593Smuzhiyun static int grcan_close(struct net_device *dev)
1107*4882a593Smuzhiyun {
1108*4882a593Smuzhiyun 	struct grcan_priv *priv = netdev_priv(dev);
1109*4882a593Smuzhiyun 	unsigned long flags;
1110*4882a593Smuzhiyun 
1111*4882a593Smuzhiyun 	napi_disable(&priv->napi);
1112*4882a593Smuzhiyun 
1113*4882a593Smuzhiyun 	spin_lock_irqsave(&priv->lock, flags);
1114*4882a593Smuzhiyun 
1115*4882a593Smuzhiyun 	priv->closing = true;
1116*4882a593Smuzhiyun 	if (priv->need_txbug_workaround) {
1117*4882a593Smuzhiyun 		spin_unlock_irqrestore(&priv->lock, flags);
1118*4882a593Smuzhiyun 		del_timer_sync(&priv->hang_timer);
1119*4882a593Smuzhiyun 		del_timer_sync(&priv->rr_timer);
1120*4882a593Smuzhiyun 		spin_lock_irqsave(&priv->lock, flags);
1121*4882a593Smuzhiyun 	}
1122*4882a593Smuzhiyun 	netif_stop_queue(dev);
1123*4882a593Smuzhiyun 	grcan_stop_hardware(dev);
1124*4882a593Smuzhiyun 	priv->can.state = CAN_STATE_STOPPED;
1125*4882a593Smuzhiyun 
1126*4882a593Smuzhiyun 	spin_unlock_irqrestore(&priv->lock, flags);
1127*4882a593Smuzhiyun 
1128*4882a593Smuzhiyun 	free_irq(dev->irq, dev);
1129*4882a593Smuzhiyun 	close_candev(dev);
1130*4882a593Smuzhiyun 
1131*4882a593Smuzhiyun 	grcan_free_dma_buffers(dev);
1132*4882a593Smuzhiyun 	priv->can.echo_skb_max = 0;
1133*4882a593Smuzhiyun 	priv->can.echo_skb = NULL;
1134*4882a593Smuzhiyun 	kfree(priv->echo_skb);
1135*4882a593Smuzhiyun 	kfree(priv->txdlc);
1136*4882a593Smuzhiyun 
1137*4882a593Smuzhiyun 	return 0;
1138*4882a593Smuzhiyun }
1139*4882a593Smuzhiyun 
grcan_transmit_catch_up(struct net_device * dev)1140*4882a593Smuzhiyun static void grcan_transmit_catch_up(struct net_device *dev)
1141*4882a593Smuzhiyun {
1142*4882a593Smuzhiyun 	struct grcan_priv *priv = netdev_priv(dev);
1143*4882a593Smuzhiyun 	unsigned long flags;
1144*4882a593Smuzhiyun 	int work_done;
1145*4882a593Smuzhiyun 
1146*4882a593Smuzhiyun 	spin_lock_irqsave(&priv->lock, flags);
1147*4882a593Smuzhiyun 
1148*4882a593Smuzhiyun 	work_done = catch_up_echo_skb(dev, -1, true);
1149*4882a593Smuzhiyun 	if (work_done) {
1150*4882a593Smuzhiyun 		if (!priv->resetting && !priv->closing &&
1151*4882a593Smuzhiyun 		    !(priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY))
1152*4882a593Smuzhiyun 			netif_wake_queue(dev);
1153*4882a593Smuzhiyun 
1154*4882a593Smuzhiyun 		/* With napi we don't get TX interrupts for a while,
1155*4882a593Smuzhiyun 		 * so prevent a running reset while catching up
1156*4882a593Smuzhiyun 		 */
1157*4882a593Smuzhiyun 		if (priv->need_txbug_workaround)
1158*4882a593Smuzhiyun 			del_timer(&priv->hang_timer);
1159*4882a593Smuzhiyun 	}
1160*4882a593Smuzhiyun 
1161*4882a593Smuzhiyun 	spin_unlock_irqrestore(&priv->lock, flags);
1162*4882a593Smuzhiyun }
1163*4882a593Smuzhiyun 
grcan_receive(struct net_device * dev,int budget)1164*4882a593Smuzhiyun static int grcan_receive(struct net_device *dev, int budget)
1165*4882a593Smuzhiyun {
1166*4882a593Smuzhiyun 	struct grcan_priv *priv = netdev_priv(dev);
1167*4882a593Smuzhiyun 	struct grcan_registers __iomem *regs = priv->regs;
1168*4882a593Smuzhiyun 	struct grcan_dma *dma = &priv->dma;
1169*4882a593Smuzhiyun 	struct net_device_stats *stats = &dev->stats;
1170*4882a593Smuzhiyun 	struct can_frame *cf;
1171*4882a593Smuzhiyun 	struct sk_buff *skb;
1172*4882a593Smuzhiyun 	u32 wr, rd, startrd;
1173*4882a593Smuzhiyun 	u32 *slot;
1174*4882a593Smuzhiyun 	u32 i, rtr, eff, j, shift;
1175*4882a593Smuzhiyun 	int work_done = 0;
1176*4882a593Smuzhiyun 
1177*4882a593Smuzhiyun 	rd = grcan_read_reg(&regs->rxrd);
1178*4882a593Smuzhiyun 	startrd = rd;
1179*4882a593Smuzhiyun 	for (work_done = 0; work_done < budget; work_done++) {
1180*4882a593Smuzhiyun 		/* Check for packet to receive */
1181*4882a593Smuzhiyun 		wr = grcan_read_reg(&regs->rxwr);
1182*4882a593Smuzhiyun 		if (rd == wr)
1183*4882a593Smuzhiyun 			break;
1184*4882a593Smuzhiyun 
1185*4882a593Smuzhiyun 		/* Take care of packet */
1186*4882a593Smuzhiyun 		skb = alloc_can_skb(dev, &cf);
1187*4882a593Smuzhiyun 		if (skb == NULL) {
1188*4882a593Smuzhiyun 			netdev_err(dev,
1189*4882a593Smuzhiyun 				   "dropping frame: skb allocation failed\n");
1190*4882a593Smuzhiyun 			stats->rx_dropped++;
1191*4882a593Smuzhiyun 			continue;
1192*4882a593Smuzhiyun 		}
1193*4882a593Smuzhiyun 
1194*4882a593Smuzhiyun 		slot = dma->rx.buf + rd;
1195*4882a593Smuzhiyun 		eff = slot[0] & GRCAN_MSG_IDE;
1196*4882a593Smuzhiyun 		rtr = slot[0] & GRCAN_MSG_RTR;
1197*4882a593Smuzhiyun 		if (eff) {
1198*4882a593Smuzhiyun 			cf->can_id = ((slot[0] & GRCAN_MSG_EID)
1199*4882a593Smuzhiyun 				      >> GRCAN_MSG_EID_BIT);
1200*4882a593Smuzhiyun 			cf->can_id |= CAN_EFF_FLAG;
1201*4882a593Smuzhiyun 		} else {
1202*4882a593Smuzhiyun 			cf->can_id = ((slot[0] & GRCAN_MSG_BID)
1203*4882a593Smuzhiyun 				      >> GRCAN_MSG_BID_BIT);
1204*4882a593Smuzhiyun 		}
1205*4882a593Smuzhiyun 		cf->can_dlc = get_can_dlc((slot[1] & GRCAN_MSG_DLC)
1206*4882a593Smuzhiyun 					  >> GRCAN_MSG_DLC_BIT);
1207*4882a593Smuzhiyun 		if (rtr) {
1208*4882a593Smuzhiyun 			cf->can_id |= CAN_RTR_FLAG;
1209*4882a593Smuzhiyun 		} else {
1210*4882a593Smuzhiyun 			for (i = 0; i < cf->can_dlc; i++) {
1211*4882a593Smuzhiyun 				j = GRCAN_MSG_DATA_SLOT_INDEX(i);
1212*4882a593Smuzhiyun 				shift = GRCAN_MSG_DATA_SHIFT(i);
1213*4882a593Smuzhiyun 				cf->data[i] = (u8)(slot[j] >> shift);
1214*4882a593Smuzhiyun 			}
1215*4882a593Smuzhiyun 		}
1216*4882a593Smuzhiyun 
1217*4882a593Smuzhiyun 		/* Update statistics and read pointer */
1218*4882a593Smuzhiyun 		stats->rx_packets++;
1219*4882a593Smuzhiyun 		stats->rx_bytes += cf->can_dlc;
1220*4882a593Smuzhiyun 		netif_receive_skb(skb);
1221*4882a593Smuzhiyun 
1222*4882a593Smuzhiyun 		rd = grcan_ring_add(rd, GRCAN_MSG_SIZE, dma->rx.size);
1223*4882a593Smuzhiyun 	}
1224*4882a593Smuzhiyun 
1225*4882a593Smuzhiyun 	/* Make sure everything is read before allowing hardware to
1226*4882a593Smuzhiyun 	 * use the memory
1227*4882a593Smuzhiyun 	 */
1228*4882a593Smuzhiyun 	mb();
1229*4882a593Smuzhiyun 
1230*4882a593Smuzhiyun 	/* Update read pointer - no need to check for ongoing */
1231*4882a593Smuzhiyun 	if (likely(rd != startrd))
1232*4882a593Smuzhiyun 		grcan_write_reg(&regs->rxrd, rd);
1233*4882a593Smuzhiyun 
1234*4882a593Smuzhiyun 	return work_done;
1235*4882a593Smuzhiyun }
1236*4882a593Smuzhiyun 
grcan_poll(struct napi_struct * napi,int budget)1237*4882a593Smuzhiyun static int grcan_poll(struct napi_struct *napi, int budget)
1238*4882a593Smuzhiyun {
1239*4882a593Smuzhiyun 	struct grcan_priv *priv = container_of(napi, struct grcan_priv, napi);
1240*4882a593Smuzhiyun 	struct net_device *dev = priv->dev;
1241*4882a593Smuzhiyun 	struct grcan_registers __iomem *regs = priv->regs;
1242*4882a593Smuzhiyun 	unsigned long flags;
1243*4882a593Smuzhiyun 	int work_done;
1244*4882a593Smuzhiyun 
1245*4882a593Smuzhiyun 	work_done = grcan_receive(dev, budget);
1246*4882a593Smuzhiyun 
1247*4882a593Smuzhiyun 	grcan_transmit_catch_up(dev);
1248*4882a593Smuzhiyun 
1249*4882a593Smuzhiyun 	if (work_done < budget) {
1250*4882a593Smuzhiyun 		napi_complete(napi);
1251*4882a593Smuzhiyun 
1252*4882a593Smuzhiyun 		/* Guarantee no interference with a running reset that otherwise
1253*4882a593Smuzhiyun 		 * could turn off interrupts.
1254*4882a593Smuzhiyun 		 */
1255*4882a593Smuzhiyun 		spin_lock_irqsave(&priv->lock, flags);
1256*4882a593Smuzhiyun 
1257*4882a593Smuzhiyun 		/* Enable tx and rx interrupts again. No need to check
1258*4882a593Smuzhiyun 		 * priv->closing as napi_disable in grcan_close is waiting for
1259*4882a593Smuzhiyun 		 * scheduled napi calls to finish.
1260*4882a593Smuzhiyun 		 */
1261*4882a593Smuzhiyun 		grcan_set_bits(&regs->imr, GRCAN_IRQ_TX | GRCAN_IRQ_RX);
1262*4882a593Smuzhiyun 
1263*4882a593Smuzhiyun 		spin_unlock_irqrestore(&priv->lock, flags);
1264*4882a593Smuzhiyun 	}
1265*4882a593Smuzhiyun 
1266*4882a593Smuzhiyun 	return work_done;
1267*4882a593Smuzhiyun }
1268*4882a593Smuzhiyun 
1269*4882a593Smuzhiyun /* Work tx bug by waiting while for the risky situation to clear. If that fails,
1270*4882a593Smuzhiyun  * drop a frame in one-shot mode or indicate a busy device otherwise.
1271*4882a593Smuzhiyun  *
1272*4882a593Smuzhiyun  * Returns 0 on successful wait. Otherwise it sets *netdev_tx_status to the
1273*4882a593Smuzhiyun  * value that should be returned by grcan_start_xmit when aborting the xmit.
1274*4882a593Smuzhiyun  */
grcan_txbug_workaround(struct net_device * dev,struct sk_buff * skb,u32 txwr,u32 oneshotmode,netdev_tx_t * netdev_tx_status)1275*4882a593Smuzhiyun static int grcan_txbug_workaround(struct net_device *dev, struct sk_buff *skb,
1276*4882a593Smuzhiyun 				  u32 txwr, u32 oneshotmode,
1277*4882a593Smuzhiyun 				  netdev_tx_t *netdev_tx_status)
1278*4882a593Smuzhiyun {
1279*4882a593Smuzhiyun 	struct grcan_priv *priv = netdev_priv(dev);
1280*4882a593Smuzhiyun 	struct grcan_registers __iomem *regs = priv->regs;
1281*4882a593Smuzhiyun 	struct grcan_dma *dma = &priv->dma;
1282*4882a593Smuzhiyun 	int i;
1283*4882a593Smuzhiyun 	unsigned long flags;
1284*4882a593Smuzhiyun 
1285*4882a593Smuzhiyun 	/* Wait a while for ongoing to be cleared or read pointer to catch up to
1286*4882a593Smuzhiyun 	 * write pointer. The latter is needed due to a bug in older versions of
1287*4882a593Smuzhiyun 	 * GRCAN in which ONGOING is not cleared properly one-shot mode when a
1288*4882a593Smuzhiyun 	 * transmission fails.
1289*4882a593Smuzhiyun 	 */
1290*4882a593Smuzhiyun 	for (i = 0; i < GRCAN_SHORTWAIT_USECS; i++) {
1291*4882a593Smuzhiyun 		udelay(1);
1292*4882a593Smuzhiyun 		if (!grcan_read_bits(&regs->txctrl, GRCAN_TXCTRL_ONGOING) ||
1293*4882a593Smuzhiyun 		    grcan_read_reg(&regs->txrd) == txwr) {
1294*4882a593Smuzhiyun 			return 0;
1295*4882a593Smuzhiyun 		}
1296*4882a593Smuzhiyun 	}
1297*4882a593Smuzhiyun 
1298*4882a593Smuzhiyun 	/* Clean up, in case the situation was not resolved */
1299*4882a593Smuzhiyun 	spin_lock_irqsave(&priv->lock, flags);
1300*4882a593Smuzhiyun 	if (!priv->resetting && !priv->closing) {
1301*4882a593Smuzhiyun 		/* Queue might have been stopped earlier in grcan_start_xmit */
1302*4882a593Smuzhiyun 		if (grcan_txspace(dma->tx.size, txwr, priv->eskbp))
1303*4882a593Smuzhiyun 			netif_wake_queue(dev);
1304*4882a593Smuzhiyun 		/* Set a timer to resolve a hanged tx controller */
1305*4882a593Smuzhiyun 		if (!timer_pending(&priv->hang_timer))
1306*4882a593Smuzhiyun 			grcan_reset_timer(&priv->hang_timer,
1307*4882a593Smuzhiyun 					  priv->can.bittiming.bitrate);
1308*4882a593Smuzhiyun 	}
1309*4882a593Smuzhiyun 	spin_unlock_irqrestore(&priv->lock, flags);
1310*4882a593Smuzhiyun 
1311*4882a593Smuzhiyun 	if (oneshotmode) {
1312*4882a593Smuzhiyun 		/* In one-shot mode we should never end up here because
1313*4882a593Smuzhiyun 		 * then the interrupt handler increases txrd on TXLOSS,
1314*4882a593Smuzhiyun 		 * but it is consistent with one-shot mode to drop the
1315*4882a593Smuzhiyun 		 * frame in this case.
1316*4882a593Smuzhiyun 		 */
1317*4882a593Smuzhiyun 		kfree_skb(skb);
1318*4882a593Smuzhiyun 		*netdev_tx_status = NETDEV_TX_OK;
1319*4882a593Smuzhiyun 	} else {
1320*4882a593Smuzhiyun 		/* In normal mode the socket-can transmission queue get
1321*4882a593Smuzhiyun 		 * to keep the frame so that it can be retransmitted
1322*4882a593Smuzhiyun 		 * later
1323*4882a593Smuzhiyun 		 */
1324*4882a593Smuzhiyun 		*netdev_tx_status = NETDEV_TX_BUSY;
1325*4882a593Smuzhiyun 	}
1326*4882a593Smuzhiyun 	return -EBUSY;
1327*4882a593Smuzhiyun }
1328*4882a593Smuzhiyun 
1329*4882a593Smuzhiyun /* Notes on the tx cyclic buffer handling:
1330*4882a593Smuzhiyun  *
1331*4882a593Smuzhiyun  * regs->txwr	- the next slot for the driver to put data to be sent
1332*4882a593Smuzhiyun  * regs->txrd	- the next slot for the device to read data
1333*4882a593Smuzhiyun  * priv->eskbp	- the next slot for the driver to call can_put_echo_skb for
1334*4882a593Smuzhiyun  *
1335*4882a593Smuzhiyun  * grcan_start_xmit can enter more messages as long as regs->txwr does
1336*4882a593Smuzhiyun  * not reach priv->eskbp (within 1 message gap)
1337*4882a593Smuzhiyun  *
1338*4882a593Smuzhiyun  * The device sends messages until regs->txrd reaches regs->txwr
1339*4882a593Smuzhiyun  *
1340*4882a593Smuzhiyun  * The interrupt calls handler calls can_put_echo_skb until
1341*4882a593Smuzhiyun  * priv->eskbp reaches regs->txrd
1342*4882a593Smuzhiyun  */
grcan_start_xmit(struct sk_buff * skb,struct net_device * dev)1343*4882a593Smuzhiyun static netdev_tx_t grcan_start_xmit(struct sk_buff *skb,
1344*4882a593Smuzhiyun 				    struct net_device *dev)
1345*4882a593Smuzhiyun {
1346*4882a593Smuzhiyun 	struct grcan_priv *priv = netdev_priv(dev);
1347*4882a593Smuzhiyun 	struct grcan_registers __iomem *regs = priv->regs;
1348*4882a593Smuzhiyun 	struct grcan_dma *dma = &priv->dma;
1349*4882a593Smuzhiyun 	struct can_frame *cf = (struct can_frame *)skb->data;
1350*4882a593Smuzhiyun 	u32 id, txwr, txrd, space, txctrl;
1351*4882a593Smuzhiyun 	int slotindex;
1352*4882a593Smuzhiyun 	u32 *slot;
1353*4882a593Smuzhiyun 	u32 i, rtr, eff, dlc, tmp, err;
1354*4882a593Smuzhiyun 	int j, shift;
1355*4882a593Smuzhiyun 	unsigned long flags;
1356*4882a593Smuzhiyun 	u32 oneshotmode = priv->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT;
1357*4882a593Smuzhiyun 
1358*4882a593Smuzhiyun 	if (can_dropped_invalid_skb(dev, skb))
1359*4882a593Smuzhiyun 		return NETDEV_TX_OK;
1360*4882a593Smuzhiyun 
1361*4882a593Smuzhiyun 	/* Trying to transmit in silent mode will generate error interrupts, but
1362*4882a593Smuzhiyun 	 * this should never happen - the queue should not have been started.
1363*4882a593Smuzhiyun 	 */
1364*4882a593Smuzhiyun 	if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
1365*4882a593Smuzhiyun 		return NETDEV_TX_BUSY;
1366*4882a593Smuzhiyun 
1367*4882a593Smuzhiyun 	/* Reads of priv->eskbp and shut-downs of the queue needs to
1368*4882a593Smuzhiyun 	 * be atomic towards the updates to priv->eskbp and wake-ups
1369*4882a593Smuzhiyun 	 * of the queue in the interrupt handler.
1370*4882a593Smuzhiyun 	 */
1371*4882a593Smuzhiyun 	spin_lock_irqsave(&priv->lock, flags);
1372*4882a593Smuzhiyun 
1373*4882a593Smuzhiyun 	txwr = grcan_read_reg(&regs->txwr);
1374*4882a593Smuzhiyun 	space = grcan_txspace(dma->tx.size, txwr, priv->eskbp);
1375*4882a593Smuzhiyun 
1376*4882a593Smuzhiyun 	slotindex = txwr / GRCAN_MSG_SIZE;
1377*4882a593Smuzhiyun 	slot = dma->tx.buf + txwr;
1378*4882a593Smuzhiyun 
1379*4882a593Smuzhiyun 	if (unlikely(space == 1))
1380*4882a593Smuzhiyun 		netif_stop_queue(dev);
1381*4882a593Smuzhiyun 
1382*4882a593Smuzhiyun 	spin_unlock_irqrestore(&priv->lock, flags);
1383*4882a593Smuzhiyun 	/* End of critical section*/
1384*4882a593Smuzhiyun 
1385*4882a593Smuzhiyun 	/* This should never happen. If circular buffer is full, the
1386*4882a593Smuzhiyun 	 * netif_stop_queue should have been stopped already.
1387*4882a593Smuzhiyun 	 */
1388*4882a593Smuzhiyun 	if (unlikely(!space)) {
1389*4882a593Smuzhiyun 		netdev_err(dev, "No buffer space, but queue is non-stopped.\n");
1390*4882a593Smuzhiyun 		return NETDEV_TX_BUSY;
1391*4882a593Smuzhiyun 	}
1392*4882a593Smuzhiyun 
1393*4882a593Smuzhiyun 	/* Convert and write CAN message to DMA buffer */
1394*4882a593Smuzhiyun 	eff = cf->can_id & CAN_EFF_FLAG;
1395*4882a593Smuzhiyun 	rtr = cf->can_id & CAN_RTR_FLAG;
1396*4882a593Smuzhiyun 	id = cf->can_id & (eff ? CAN_EFF_MASK : CAN_SFF_MASK);
1397*4882a593Smuzhiyun 	dlc = cf->can_dlc;
1398*4882a593Smuzhiyun 	if (eff)
1399*4882a593Smuzhiyun 		tmp = (id << GRCAN_MSG_EID_BIT) & GRCAN_MSG_EID;
1400*4882a593Smuzhiyun 	else
1401*4882a593Smuzhiyun 		tmp = (id << GRCAN_MSG_BID_BIT) & GRCAN_MSG_BID;
1402*4882a593Smuzhiyun 	slot[0] = (eff ? GRCAN_MSG_IDE : 0) | (rtr ? GRCAN_MSG_RTR : 0) | tmp;
1403*4882a593Smuzhiyun 
1404*4882a593Smuzhiyun 	slot[1] = ((dlc << GRCAN_MSG_DLC_BIT) & GRCAN_MSG_DLC);
1405*4882a593Smuzhiyun 	slot[2] = 0;
1406*4882a593Smuzhiyun 	slot[3] = 0;
1407*4882a593Smuzhiyun 	for (i = 0; i < dlc; i++) {
1408*4882a593Smuzhiyun 		j = GRCAN_MSG_DATA_SLOT_INDEX(i);
1409*4882a593Smuzhiyun 		shift = GRCAN_MSG_DATA_SHIFT(i);
1410*4882a593Smuzhiyun 		slot[j] |= cf->data[i] << shift;
1411*4882a593Smuzhiyun 	}
1412*4882a593Smuzhiyun 
1413*4882a593Smuzhiyun 	/* Checking that channel has not been disabled. These cases
1414*4882a593Smuzhiyun 	 * should never happen
1415*4882a593Smuzhiyun 	 */
1416*4882a593Smuzhiyun 	txctrl = grcan_read_reg(&regs->txctrl);
1417*4882a593Smuzhiyun 	if (!(txctrl & GRCAN_TXCTRL_ENABLE))
1418*4882a593Smuzhiyun 		netdev_err(dev, "tx channel spuriously disabled\n");
1419*4882a593Smuzhiyun 
1420*4882a593Smuzhiyun 	if (oneshotmode && !(txctrl & GRCAN_TXCTRL_SINGLE))
1421*4882a593Smuzhiyun 		netdev_err(dev, "one-shot mode spuriously disabled\n");
1422*4882a593Smuzhiyun 
1423*4882a593Smuzhiyun 	/* Bug workaround for old version of grcan where updating txwr
1424*4882a593Smuzhiyun 	 * in the same clock cycle as the controller updates txrd to
1425*4882a593Smuzhiyun 	 * the current txwr could hang the can controller
1426*4882a593Smuzhiyun 	 */
1427*4882a593Smuzhiyun 	if (priv->need_txbug_workaround) {
1428*4882a593Smuzhiyun 		txrd = grcan_read_reg(&regs->txrd);
1429*4882a593Smuzhiyun 		if (unlikely(grcan_ring_sub(txwr, txrd, dma->tx.size) == 1)) {
1430*4882a593Smuzhiyun 			netdev_tx_t txstatus;
1431*4882a593Smuzhiyun 
1432*4882a593Smuzhiyun 			err = grcan_txbug_workaround(dev, skb, txwr,
1433*4882a593Smuzhiyun 						     oneshotmode, &txstatus);
1434*4882a593Smuzhiyun 			if (err)
1435*4882a593Smuzhiyun 				return txstatus;
1436*4882a593Smuzhiyun 		}
1437*4882a593Smuzhiyun 	}
1438*4882a593Smuzhiyun 
1439*4882a593Smuzhiyun 	/* Prepare skb for echoing. This must be after the bug workaround above
1440*4882a593Smuzhiyun 	 * as ownership of the skb is passed on by calling can_put_echo_skb.
1441*4882a593Smuzhiyun 	 * Returning NETDEV_TX_BUSY or accessing skb or cf after a call to
1442*4882a593Smuzhiyun 	 * can_put_echo_skb would be an error unless other measures are
1443*4882a593Smuzhiyun 	 * taken.
1444*4882a593Smuzhiyun 	 */
1445*4882a593Smuzhiyun 	priv->txdlc[slotindex] = cf->can_dlc; /* Store dlc for statistics */
1446*4882a593Smuzhiyun 	can_put_echo_skb(skb, dev, slotindex);
1447*4882a593Smuzhiyun 
1448*4882a593Smuzhiyun 	/* Make sure everything is written before allowing hardware to
1449*4882a593Smuzhiyun 	 * read from the memory
1450*4882a593Smuzhiyun 	 */
1451*4882a593Smuzhiyun 	wmb();
1452*4882a593Smuzhiyun 
1453*4882a593Smuzhiyun 	/* Update write pointer to start transmission */
1454*4882a593Smuzhiyun 	grcan_write_reg(&regs->txwr,
1455*4882a593Smuzhiyun 			grcan_ring_add(txwr, GRCAN_MSG_SIZE, dma->tx.size));
1456*4882a593Smuzhiyun 
1457*4882a593Smuzhiyun 	return NETDEV_TX_OK;
1458*4882a593Smuzhiyun }
1459*4882a593Smuzhiyun 
1460*4882a593Smuzhiyun /* ========== Setting up sysfs interface and module parameters ========== */
1461*4882a593Smuzhiyun 
1462*4882a593Smuzhiyun #define GRCAN_NOT_BOOL(unsigned_val) ((unsigned_val) > 1)
1463*4882a593Smuzhiyun 
1464*4882a593Smuzhiyun #define GRCAN_MODULE_PARAM(name, mtype, valcheckf, desc)		\
1465*4882a593Smuzhiyun 	static void grcan_sanitize_##name(struct platform_device *pd)	\
1466*4882a593Smuzhiyun 	{								\
1467*4882a593Smuzhiyun 		struct grcan_device_config grcan_default_config		\
1468*4882a593Smuzhiyun 			= GRCAN_DEFAULT_DEVICE_CONFIG;			\
1469*4882a593Smuzhiyun 		if (valcheckf(grcan_module_config.name)) {		\
1470*4882a593Smuzhiyun 			dev_err(&pd->dev,				\
1471*4882a593Smuzhiyun 				"Invalid module parameter value for "	\
1472*4882a593Smuzhiyun 				#name " - setting default\n");		\
1473*4882a593Smuzhiyun 			grcan_module_config.name =			\
1474*4882a593Smuzhiyun 				grcan_default_config.name;		\
1475*4882a593Smuzhiyun 		}							\
1476*4882a593Smuzhiyun 	}								\
1477*4882a593Smuzhiyun 	module_param_named(name, grcan_module_config.name,		\
1478*4882a593Smuzhiyun 			   mtype, 0444);				\
1479*4882a593Smuzhiyun 	MODULE_PARM_DESC(name, desc)
1480*4882a593Smuzhiyun 
1481*4882a593Smuzhiyun #define GRCAN_CONFIG_ATTR(name, desc)					\
1482*4882a593Smuzhiyun 	static ssize_t grcan_store_##name(struct device *sdev,		\
1483*4882a593Smuzhiyun 					  struct device_attribute *att,	\
1484*4882a593Smuzhiyun 					  const char *buf,		\
1485*4882a593Smuzhiyun 					  size_t count)			\
1486*4882a593Smuzhiyun 	{								\
1487*4882a593Smuzhiyun 		struct net_device *dev = to_net_dev(sdev);		\
1488*4882a593Smuzhiyun 		struct grcan_priv *priv = netdev_priv(dev);		\
1489*4882a593Smuzhiyun 		u8 val;							\
1490*4882a593Smuzhiyun 		int ret;						\
1491*4882a593Smuzhiyun 		if (dev->flags & IFF_UP)				\
1492*4882a593Smuzhiyun 			return -EBUSY;					\
1493*4882a593Smuzhiyun 		ret = kstrtou8(buf, 0, &val);				\
1494*4882a593Smuzhiyun 		if (ret < 0 || val > 1)					\
1495*4882a593Smuzhiyun 			return -EINVAL;					\
1496*4882a593Smuzhiyun 		priv->config.name = val;				\
1497*4882a593Smuzhiyun 		return count;						\
1498*4882a593Smuzhiyun 	}								\
1499*4882a593Smuzhiyun 	static ssize_t grcan_show_##name(struct device *sdev,		\
1500*4882a593Smuzhiyun 					 struct device_attribute *att,	\
1501*4882a593Smuzhiyun 					 char *buf)			\
1502*4882a593Smuzhiyun 	{								\
1503*4882a593Smuzhiyun 		struct net_device *dev = to_net_dev(sdev);		\
1504*4882a593Smuzhiyun 		struct grcan_priv *priv = netdev_priv(dev);		\
1505*4882a593Smuzhiyun 		return sprintf(buf, "%d\n", priv->config.name);		\
1506*4882a593Smuzhiyun 	}								\
1507*4882a593Smuzhiyun 	static DEVICE_ATTR(name, 0644,					\
1508*4882a593Smuzhiyun 			   grcan_show_##name,				\
1509*4882a593Smuzhiyun 			   grcan_store_##name);				\
1510*4882a593Smuzhiyun 	GRCAN_MODULE_PARAM(name, ushort, GRCAN_NOT_BOOL, desc)
1511*4882a593Smuzhiyun 
1512*4882a593Smuzhiyun /* The following configuration options are made available both via module
1513*4882a593Smuzhiyun  * parameters and writable sysfs files. See the chapter about GRCAN in the
1514*4882a593Smuzhiyun  * documentation for the GRLIB VHDL library for further details.
1515*4882a593Smuzhiyun  */
1516*4882a593Smuzhiyun GRCAN_CONFIG_ATTR(enable0,
1517*4882a593Smuzhiyun 		  "Configuration of physical interface 0. Determines\n"	\
1518*4882a593Smuzhiyun 		  "the \"Enable 0\" bit of the configuration register.\n" \
1519*4882a593Smuzhiyun 		  "Format: 0 | 1\nDefault: 0\n");
1520*4882a593Smuzhiyun 
1521*4882a593Smuzhiyun GRCAN_CONFIG_ATTR(enable1,
1522*4882a593Smuzhiyun 		  "Configuration of physical interface 1. Determines\n"	\
1523*4882a593Smuzhiyun 		  "the \"Enable 1\" bit of the configuration register.\n" \
1524*4882a593Smuzhiyun 		  "Format: 0 | 1\nDefault: 0\n");
1525*4882a593Smuzhiyun 
1526*4882a593Smuzhiyun GRCAN_CONFIG_ATTR(select,
1527*4882a593Smuzhiyun 		  "Select which physical interface to use.\n"	\
1528*4882a593Smuzhiyun 		  "Format: 0 | 1\nDefault: 0\n");
1529*4882a593Smuzhiyun 
1530*4882a593Smuzhiyun /* The tx and rx buffer size configuration options are only available via module
1531*4882a593Smuzhiyun  * parameters.
1532*4882a593Smuzhiyun  */
1533*4882a593Smuzhiyun GRCAN_MODULE_PARAM(txsize, uint, GRCAN_INVALID_BUFFER_SIZE,
1534*4882a593Smuzhiyun 		   "Sets the size of the tx buffer.\n"			\
1535*4882a593Smuzhiyun 		   "Format: <unsigned int> where (txsize & ~0x1fffc0) == 0\n" \
1536*4882a593Smuzhiyun 		   "Default: 1024\n");
1537*4882a593Smuzhiyun GRCAN_MODULE_PARAM(rxsize, uint, GRCAN_INVALID_BUFFER_SIZE,
1538*4882a593Smuzhiyun 		   "Sets the size of the rx buffer.\n"			\
1539*4882a593Smuzhiyun 		   "Format: <unsigned int> where (size & ~0x1fffc0) == 0\n" \
1540*4882a593Smuzhiyun 		   "Default: 1024\n");
1541*4882a593Smuzhiyun 
1542*4882a593Smuzhiyun /* Function that makes sure that configuration done using
1543*4882a593Smuzhiyun  * module parameters are set to valid values
1544*4882a593Smuzhiyun  */
grcan_sanitize_module_config(struct platform_device * ofdev)1545*4882a593Smuzhiyun static void grcan_sanitize_module_config(struct platform_device *ofdev)
1546*4882a593Smuzhiyun {
1547*4882a593Smuzhiyun 	grcan_sanitize_enable0(ofdev);
1548*4882a593Smuzhiyun 	grcan_sanitize_enable1(ofdev);
1549*4882a593Smuzhiyun 	grcan_sanitize_select(ofdev);
1550*4882a593Smuzhiyun 	grcan_sanitize_txsize(ofdev);
1551*4882a593Smuzhiyun 	grcan_sanitize_rxsize(ofdev);
1552*4882a593Smuzhiyun }
1553*4882a593Smuzhiyun 
1554*4882a593Smuzhiyun static const struct attribute *const sysfs_grcan_attrs[] = {
1555*4882a593Smuzhiyun 	/* Config attrs */
1556*4882a593Smuzhiyun 	&dev_attr_enable0.attr,
1557*4882a593Smuzhiyun 	&dev_attr_enable1.attr,
1558*4882a593Smuzhiyun 	&dev_attr_select.attr,
1559*4882a593Smuzhiyun 	NULL,
1560*4882a593Smuzhiyun };
1561*4882a593Smuzhiyun 
1562*4882a593Smuzhiyun static const struct attribute_group sysfs_grcan_group = {
1563*4882a593Smuzhiyun 	.name	= "grcan",
1564*4882a593Smuzhiyun 	.attrs	= (struct attribute **)sysfs_grcan_attrs,
1565*4882a593Smuzhiyun };
1566*4882a593Smuzhiyun 
1567*4882a593Smuzhiyun /* ========== Setting up the driver ========== */
1568*4882a593Smuzhiyun 
1569*4882a593Smuzhiyun static const struct net_device_ops grcan_netdev_ops = {
1570*4882a593Smuzhiyun 	.ndo_open	= grcan_open,
1571*4882a593Smuzhiyun 	.ndo_stop	= grcan_close,
1572*4882a593Smuzhiyun 	.ndo_start_xmit	= grcan_start_xmit,
1573*4882a593Smuzhiyun 	.ndo_change_mtu = can_change_mtu,
1574*4882a593Smuzhiyun };
1575*4882a593Smuzhiyun 
grcan_setup_netdev(struct platform_device * ofdev,void __iomem * base,int irq,u32 ambafreq,bool txbug)1576*4882a593Smuzhiyun static int grcan_setup_netdev(struct platform_device *ofdev,
1577*4882a593Smuzhiyun 			      void __iomem *base,
1578*4882a593Smuzhiyun 			      int irq, u32 ambafreq, bool txbug)
1579*4882a593Smuzhiyun {
1580*4882a593Smuzhiyun 	struct net_device *dev;
1581*4882a593Smuzhiyun 	struct grcan_priv *priv;
1582*4882a593Smuzhiyun 	struct grcan_registers __iomem *regs;
1583*4882a593Smuzhiyun 	int err;
1584*4882a593Smuzhiyun 
1585*4882a593Smuzhiyun 	dev = alloc_candev(sizeof(struct grcan_priv), 0);
1586*4882a593Smuzhiyun 	if (!dev)
1587*4882a593Smuzhiyun 		return -ENOMEM;
1588*4882a593Smuzhiyun 
1589*4882a593Smuzhiyun 	dev->irq = irq;
1590*4882a593Smuzhiyun 	dev->flags |= IFF_ECHO;
1591*4882a593Smuzhiyun 	dev->netdev_ops = &grcan_netdev_ops;
1592*4882a593Smuzhiyun 	dev->sysfs_groups[0] = &sysfs_grcan_group;
1593*4882a593Smuzhiyun 
1594*4882a593Smuzhiyun 	priv = netdev_priv(dev);
1595*4882a593Smuzhiyun 	memcpy(&priv->config, &grcan_module_config,
1596*4882a593Smuzhiyun 	       sizeof(struct grcan_device_config));
1597*4882a593Smuzhiyun 	priv->dev = dev;
1598*4882a593Smuzhiyun 	priv->ofdev_dev = &ofdev->dev;
1599*4882a593Smuzhiyun 	priv->regs = base;
1600*4882a593Smuzhiyun 	priv->can.bittiming_const = &grcan_bittiming_const;
1601*4882a593Smuzhiyun 	priv->can.do_set_bittiming = grcan_set_bittiming;
1602*4882a593Smuzhiyun 	priv->can.do_set_mode = grcan_set_mode;
1603*4882a593Smuzhiyun 	priv->can.do_get_berr_counter = grcan_get_berr_counter;
1604*4882a593Smuzhiyun 	priv->can.clock.freq = ambafreq;
1605*4882a593Smuzhiyun 	priv->can.ctrlmode_supported =
1606*4882a593Smuzhiyun 		CAN_CTRLMODE_LISTENONLY | CAN_CTRLMODE_ONE_SHOT;
1607*4882a593Smuzhiyun 	priv->need_txbug_workaround = txbug;
1608*4882a593Smuzhiyun 
1609*4882a593Smuzhiyun 	/* Discover if triple sampling is supported by hardware */
1610*4882a593Smuzhiyun 	regs = priv->regs;
1611*4882a593Smuzhiyun 	grcan_set_bits(&regs->ctrl, GRCAN_CTRL_RESET);
1612*4882a593Smuzhiyun 	grcan_set_bits(&regs->conf, GRCAN_CONF_SAM);
1613*4882a593Smuzhiyun 	if (grcan_read_bits(&regs->conf, GRCAN_CONF_SAM)) {
1614*4882a593Smuzhiyun 		priv->can.ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES;
1615*4882a593Smuzhiyun 		dev_dbg(&ofdev->dev, "Hardware supports triple-sampling\n");
1616*4882a593Smuzhiyun 	}
1617*4882a593Smuzhiyun 
1618*4882a593Smuzhiyun 	spin_lock_init(&priv->lock);
1619*4882a593Smuzhiyun 
1620*4882a593Smuzhiyun 	if (priv->need_txbug_workaround) {
1621*4882a593Smuzhiyun 		timer_setup(&priv->rr_timer, grcan_running_reset, 0);
1622*4882a593Smuzhiyun 		timer_setup(&priv->hang_timer, grcan_initiate_running_reset, 0);
1623*4882a593Smuzhiyun 	}
1624*4882a593Smuzhiyun 
1625*4882a593Smuzhiyun 	netif_napi_add(dev, &priv->napi, grcan_poll, GRCAN_NAPI_WEIGHT);
1626*4882a593Smuzhiyun 
1627*4882a593Smuzhiyun 	SET_NETDEV_DEV(dev, &ofdev->dev);
1628*4882a593Smuzhiyun 	dev_info(&ofdev->dev, "regs=0x%p, irq=%d, clock=%d\n",
1629*4882a593Smuzhiyun 		 priv->regs, dev->irq, priv->can.clock.freq);
1630*4882a593Smuzhiyun 
1631*4882a593Smuzhiyun 	err = register_candev(dev);
1632*4882a593Smuzhiyun 	if (err)
1633*4882a593Smuzhiyun 		goto exit_free_candev;
1634*4882a593Smuzhiyun 
1635*4882a593Smuzhiyun 	platform_set_drvdata(ofdev, dev);
1636*4882a593Smuzhiyun 
1637*4882a593Smuzhiyun 	/* Reset device to allow bit-timing to be set. No need to call
1638*4882a593Smuzhiyun 	 * grcan_reset at this stage. That is done in grcan_open.
1639*4882a593Smuzhiyun 	 */
1640*4882a593Smuzhiyun 	grcan_write_reg(&regs->ctrl, GRCAN_CTRL_RESET);
1641*4882a593Smuzhiyun 
1642*4882a593Smuzhiyun 	return 0;
1643*4882a593Smuzhiyun exit_free_candev:
1644*4882a593Smuzhiyun 	free_candev(dev);
1645*4882a593Smuzhiyun 	return err;
1646*4882a593Smuzhiyun }
1647*4882a593Smuzhiyun 
grcan_probe(struct platform_device * ofdev)1648*4882a593Smuzhiyun static int grcan_probe(struct platform_device *ofdev)
1649*4882a593Smuzhiyun {
1650*4882a593Smuzhiyun 	struct device_node *np = ofdev->dev.of_node;
1651*4882a593Smuzhiyun 	struct device_node *sysid_parent;
1652*4882a593Smuzhiyun 	u32 sysid, ambafreq;
1653*4882a593Smuzhiyun 	int irq, err;
1654*4882a593Smuzhiyun 	void __iomem *base;
1655*4882a593Smuzhiyun 	bool txbug = true;
1656*4882a593Smuzhiyun 
1657*4882a593Smuzhiyun 	/* Compare GRLIB version number with the first that does not
1658*4882a593Smuzhiyun 	 * have the tx bug (see start_xmit)
1659*4882a593Smuzhiyun 	 */
1660*4882a593Smuzhiyun 	sysid_parent = of_find_node_by_path("/ambapp0");
1661*4882a593Smuzhiyun 	if (sysid_parent) {
1662*4882a593Smuzhiyun 		err = of_property_read_u32(sysid_parent, "systemid", &sysid);
1663*4882a593Smuzhiyun 		if (!err && ((sysid & GRLIB_VERSION_MASK) >=
1664*4882a593Smuzhiyun 			     GRCAN_TXBUG_SAFE_GRLIB_VERSION))
1665*4882a593Smuzhiyun 			txbug = false;
1666*4882a593Smuzhiyun 		of_node_put(sysid_parent);
1667*4882a593Smuzhiyun 	}
1668*4882a593Smuzhiyun 
1669*4882a593Smuzhiyun 	err = of_property_read_u32(np, "freq", &ambafreq);
1670*4882a593Smuzhiyun 	if (err) {
1671*4882a593Smuzhiyun 		dev_err(&ofdev->dev, "unable to fetch \"freq\" property\n");
1672*4882a593Smuzhiyun 		goto exit_error;
1673*4882a593Smuzhiyun 	}
1674*4882a593Smuzhiyun 
1675*4882a593Smuzhiyun 	base = devm_platform_ioremap_resource(ofdev, 0);
1676*4882a593Smuzhiyun 	if (IS_ERR(base)) {
1677*4882a593Smuzhiyun 		err = PTR_ERR(base);
1678*4882a593Smuzhiyun 		goto exit_error;
1679*4882a593Smuzhiyun 	}
1680*4882a593Smuzhiyun 
1681*4882a593Smuzhiyun 	irq = irq_of_parse_and_map(np, GRCAN_IRQIX_IRQ);
1682*4882a593Smuzhiyun 	if (!irq) {
1683*4882a593Smuzhiyun 		dev_err(&ofdev->dev, "no irq found\n");
1684*4882a593Smuzhiyun 		err = -ENODEV;
1685*4882a593Smuzhiyun 		goto exit_error;
1686*4882a593Smuzhiyun 	}
1687*4882a593Smuzhiyun 
1688*4882a593Smuzhiyun 	grcan_sanitize_module_config(ofdev);
1689*4882a593Smuzhiyun 
1690*4882a593Smuzhiyun 	err = grcan_setup_netdev(ofdev, base, irq, ambafreq, txbug);
1691*4882a593Smuzhiyun 	if (err)
1692*4882a593Smuzhiyun 		goto exit_dispose_irq;
1693*4882a593Smuzhiyun 
1694*4882a593Smuzhiyun 	return 0;
1695*4882a593Smuzhiyun 
1696*4882a593Smuzhiyun exit_dispose_irq:
1697*4882a593Smuzhiyun 	irq_dispose_mapping(irq);
1698*4882a593Smuzhiyun exit_error:
1699*4882a593Smuzhiyun 	dev_err(&ofdev->dev,
1700*4882a593Smuzhiyun 		"%s socket CAN driver initialization failed with error %d\n",
1701*4882a593Smuzhiyun 		DRV_NAME, err);
1702*4882a593Smuzhiyun 	return err;
1703*4882a593Smuzhiyun }
1704*4882a593Smuzhiyun 
grcan_remove(struct platform_device * ofdev)1705*4882a593Smuzhiyun static int grcan_remove(struct platform_device *ofdev)
1706*4882a593Smuzhiyun {
1707*4882a593Smuzhiyun 	struct net_device *dev = platform_get_drvdata(ofdev);
1708*4882a593Smuzhiyun 	struct grcan_priv *priv = netdev_priv(dev);
1709*4882a593Smuzhiyun 
1710*4882a593Smuzhiyun 	unregister_candev(dev); /* Will in turn call grcan_close */
1711*4882a593Smuzhiyun 
1712*4882a593Smuzhiyun 	irq_dispose_mapping(dev->irq);
1713*4882a593Smuzhiyun 	netif_napi_del(&priv->napi);
1714*4882a593Smuzhiyun 	free_candev(dev);
1715*4882a593Smuzhiyun 
1716*4882a593Smuzhiyun 	return 0;
1717*4882a593Smuzhiyun }
1718*4882a593Smuzhiyun 
1719*4882a593Smuzhiyun static const struct of_device_id grcan_match[] = {
1720*4882a593Smuzhiyun 	{.name = "GAISLER_GRCAN"},
1721*4882a593Smuzhiyun 	{.name = "01_03d"},
1722*4882a593Smuzhiyun 	{.name = "GAISLER_GRHCAN"},
1723*4882a593Smuzhiyun 	{.name = "01_034"},
1724*4882a593Smuzhiyun 	{},
1725*4882a593Smuzhiyun };
1726*4882a593Smuzhiyun 
1727*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, grcan_match);
1728*4882a593Smuzhiyun 
1729*4882a593Smuzhiyun static struct platform_driver grcan_driver = {
1730*4882a593Smuzhiyun 	.driver = {
1731*4882a593Smuzhiyun 		.name = DRV_NAME,
1732*4882a593Smuzhiyun 		.of_match_table = grcan_match,
1733*4882a593Smuzhiyun 	},
1734*4882a593Smuzhiyun 	.probe = grcan_probe,
1735*4882a593Smuzhiyun 	.remove = grcan_remove,
1736*4882a593Smuzhiyun };
1737*4882a593Smuzhiyun 
1738*4882a593Smuzhiyun module_platform_driver(grcan_driver);
1739*4882a593Smuzhiyun 
1740*4882a593Smuzhiyun MODULE_AUTHOR("Aeroflex Gaisler AB.");
1741*4882a593Smuzhiyun MODULE_DESCRIPTION("Socket CAN driver for Aeroflex Gaisler GRCAN");
1742*4882a593Smuzhiyun MODULE_LICENSE("GPL");
1743