xref: /openbmc/linux/drivers/net/can/xilinx_can.c (revision 1372a51b)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Xilinx CAN device driver
3  *
4  * Copyright (C) 2012 - 2014 Xilinx, Inc.
5  * Copyright (C) 2009 PetaLogix. All rights reserved.
6  * Copyright (C) 2017 - 2018 Sandvik Mining and Construction Oy
7  *
8  * Description:
9  * This driver is developed for Axi CAN IP and for Zynq CANPS Controller.
10  */
11 
12 #include <linux/clk.h>
13 #include <linux/errno.h>
14 #include <linux/init.h>
15 #include <linux/interrupt.h>
16 #include <linux/io.h>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/netdevice.h>
20 #include <linux/of.h>
21 #include <linux/of_device.h>
22 #include <linux/platform_device.h>
23 #include <linux/skbuff.h>
24 #include <linux/spinlock.h>
25 #include <linux/string.h>
26 #include <linux/types.h>
27 #include <linux/can/dev.h>
28 #include <linux/can/error.h>
29 #include <linux/can/led.h>
30 #include <linux/pm_runtime.h>
31 
32 #define DRIVER_NAME	"xilinx_can"
33 
34 /* CAN registers set */
35 enum xcan_reg {
36 	XCAN_SRR_OFFSET		= 0x00, /* Software reset */
37 	XCAN_MSR_OFFSET		= 0x04, /* Mode select */
38 	XCAN_BRPR_OFFSET	= 0x08, /* Baud rate prescaler */
39 	XCAN_BTR_OFFSET		= 0x0C, /* Bit timing */
40 	XCAN_ECR_OFFSET		= 0x10, /* Error counter */
41 	XCAN_ESR_OFFSET		= 0x14, /* Error status */
42 	XCAN_SR_OFFSET		= 0x18, /* Status */
43 	XCAN_ISR_OFFSET		= 0x1C, /* Interrupt status */
44 	XCAN_IER_OFFSET		= 0x20, /* Interrupt enable */
45 	XCAN_ICR_OFFSET		= 0x24, /* Interrupt clear */
46 
47 	/* not on CAN FD cores */
48 	XCAN_TXFIFO_OFFSET	= 0x30, /* TX FIFO base */
49 	XCAN_RXFIFO_OFFSET	= 0x50, /* RX FIFO base */
50 	XCAN_AFR_OFFSET		= 0x60, /* Acceptance Filter */
51 
52 	/* only on CAN FD cores */
53 	XCAN_F_BRPR_OFFSET	= 0x088, /* Data Phase Baud Rate
54 					  * Prescalar
55 					  */
56 	XCAN_F_BTR_OFFSET	= 0x08C, /* Data Phase Bit Timing */
57 	XCAN_TRR_OFFSET		= 0x0090, /* TX Buffer Ready Request */
58 	XCAN_AFR_EXT_OFFSET	= 0x00E0, /* Acceptance Filter */
59 	XCAN_FSR_OFFSET		= 0x00E8, /* RX FIFO Status */
60 	XCAN_TXMSG_BASE_OFFSET	= 0x0100, /* TX Message Space */
61 	XCAN_RXMSG_BASE_OFFSET	= 0x1100, /* RX Message Space */
62 	XCAN_RXMSG_2_BASE_OFFSET	= 0x2100, /* RX Message Space */
63 };
64 
65 #define XCAN_FRAME_ID_OFFSET(frame_base)	((frame_base) + 0x00)
66 #define XCAN_FRAME_DLC_OFFSET(frame_base)	((frame_base) + 0x04)
67 #define XCAN_FRAME_DW1_OFFSET(frame_base)	((frame_base) + 0x08)
68 #define XCAN_FRAME_DW2_OFFSET(frame_base)	((frame_base) + 0x0C)
69 #define XCANFD_FRAME_DW_OFFSET(frame_base)	((frame_base) + 0x08)
70 
71 #define XCAN_CANFD_FRAME_SIZE		0x48
72 #define XCAN_TXMSG_FRAME_OFFSET(n)	(XCAN_TXMSG_BASE_OFFSET + \
73 					 XCAN_CANFD_FRAME_SIZE * (n))
74 #define XCAN_RXMSG_FRAME_OFFSET(n)	(XCAN_RXMSG_BASE_OFFSET + \
75 					 XCAN_CANFD_FRAME_SIZE * (n))
76 #define XCAN_RXMSG_2_FRAME_OFFSET(n)	(XCAN_RXMSG_2_BASE_OFFSET + \
77 					 XCAN_CANFD_FRAME_SIZE * (n))
78 
79 /* the single TX mailbox used by this driver on CAN FD HW */
80 #define XCAN_TX_MAILBOX_IDX		0
81 
82 /* CAN register bit masks - XCAN_<REG>_<BIT>_MASK */
83 #define XCAN_SRR_CEN_MASK		0x00000002 /* CAN enable */
84 #define XCAN_SRR_RESET_MASK		0x00000001 /* Soft Reset the CAN core */
85 #define XCAN_MSR_LBACK_MASK		0x00000002 /* Loop back mode select */
86 #define XCAN_MSR_SLEEP_MASK		0x00000001 /* Sleep mode select */
87 #define XCAN_BRPR_BRP_MASK		0x000000FF /* Baud rate prescaler */
88 #define XCAN_BTR_SJW_MASK		0x00000180 /* Synchronous jump width */
89 #define XCAN_BTR_TS2_MASK		0x00000070 /* Time segment 2 */
90 #define XCAN_BTR_TS1_MASK		0x0000000F /* Time segment 1 */
91 #define XCAN_BTR_SJW_MASK_CANFD		0x000F0000 /* Synchronous jump width */
92 #define XCAN_BTR_TS2_MASK_CANFD		0x00000F00 /* Time segment 2 */
93 #define XCAN_BTR_TS1_MASK_CANFD		0x0000003F /* Time segment 1 */
94 #define XCAN_ECR_REC_MASK		0x0000FF00 /* Receive error counter */
95 #define XCAN_ECR_TEC_MASK		0x000000FF /* Transmit error counter */
96 #define XCAN_ESR_ACKER_MASK		0x00000010 /* ACK error */
97 #define XCAN_ESR_BERR_MASK		0x00000008 /* Bit error */
98 #define XCAN_ESR_STER_MASK		0x00000004 /* Stuff error */
99 #define XCAN_ESR_FMER_MASK		0x00000002 /* Form error */
100 #define XCAN_ESR_CRCER_MASK		0x00000001 /* CRC error */
101 #define XCAN_SR_TXFLL_MASK		0x00000400 /* TX FIFO is full */
102 #define XCAN_SR_ESTAT_MASK		0x00000180 /* Error status */
103 #define XCAN_SR_ERRWRN_MASK		0x00000040 /* Error warning */
104 #define XCAN_SR_NORMAL_MASK		0x00000008 /* Normal mode */
105 #define XCAN_SR_LBACK_MASK		0x00000002 /* Loop back mode */
106 #define XCAN_SR_CONFIG_MASK		0x00000001 /* Configuration mode */
107 #define XCAN_IXR_RXMNF_MASK		0x00020000 /* RX match not finished */
108 #define XCAN_IXR_TXFEMP_MASK		0x00004000 /* TX FIFO Empty */
109 #define XCAN_IXR_WKUP_MASK		0x00000800 /* Wake up interrupt */
110 #define XCAN_IXR_SLP_MASK		0x00000400 /* Sleep interrupt */
111 #define XCAN_IXR_BSOFF_MASK		0x00000200 /* Bus off interrupt */
112 #define XCAN_IXR_ERROR_MASK		0x00000100 /* Error interrupt */
113 #define XCAN_IXR_RXNEMP_MASK		0x00000080 /* RX FIFO NotEmpty intr */
114 #define XCAN_IXR_RXOFLW_MASK		0x00000040 /* RX FIFO Overflow intr */
115 #define XCAN_IXR_RXOK_MASK		0x00000010 /* Message received intr */
116 #define XCAN_IXR_TXFLL_MASK		0x00000004 /* Tx FIFO Full intr */
117 #define XCAN_IXR_TXOK_MASK		0x00000002 /* TX successful intr */
118 #define XCAN_IXR_ARBLST_MASK		0x00000001 /* Arbitration lost intr */
119 #define XCAN_IDR_ID1_MASK		0xFFE00000 /* Standard msg identifier */
120 #define XCAN_IDR_SRR_MASK		0x00100000 /* Substitute remote TXreq */
121 #define XCAN_IDR_IDE_MASK		0x00080000 /* Identifier extension */
122 #define XCAN_IDR_ID2_MASK		0x0007FFFE /* Extended message ident */
123 #define XCAN_IDR_RTR_MASK		0x00000001 /* Remote TX request */
124 #define XCAN_DLCR_DLC_MASK		0xF0000000 /* Data length code */
125 #define XCAN_FSR_FL_MASK		0x00003F00 /* RX Fill Level */
126 #define XCAN_2_FSR_FL_MASK		0x00007F00 /* RX Fill Level */
127 #define XCAN_FSR_IRI_MASK		0x00000080 /* RX Increment Read Index */
128 #define XCAN_FSR_RI_MASK		0x0000001F /* RX Read Index */
129 #define XCAN_2_FSR_RI_MASK		0x0000003F /* RX Read Index */
130 #define XCAN_DLCR_EDL_MASK		0x08000000 /* EDL Mask in DLC */
131 #define XCAN_DLCR_BRS_MASK		0x04000000 /* BRS Mask in DLC */
132 
133 /* CAN register bit shift - XCAN_<REG>_<BIT>_SHIFT */
134 #define XCAN_BTR_SJW_SHIFT		7  /* Synchronous jump width */
135 #define XCAN_BTR_TS2_SHIFT		4  /* Time segment 2 */
136 #define XCAN_BTR_SJW_SHIFT_CANFD	16 /* Synchronous jump width */
137 #define XCAN_BTR_TS2_SHIFT_CANFD	8  /* Time segment 2 */
138 #define XCAN_IDR_ID1_SHIFT		21 /* Standard Messg Identifier */
139 #define XCAN_IDR_ID2_SHIFT		1  /* Extended Message Identifier */
140 #define XCAN_DLCR_DLC_SHIFT		28 /* Data length code */
141 #define XCAN_ESR_REC_SHIFT		8  /* Rx Error Count */
142 
143 /* CAN frame length constants */
144 #define XCAN_FRAME_MAX_DATA_LEN		8
145 #define XCANFD_DW_BYTES			4
146 #define XCAN_TIMEOUT			(1 * HZ)
147 
148 /* TX-FIFO-empty interrupt available */
149 #define XCAN_FLAG_TXFEMP	0x0001
150 /* RX Match Not Finished interrupt available */
151 #define XCAN_FLAG_RXMNF		0x0002
152 /* Extended acceptance filters with control at 0xE0 */
153 #define XCAN_FLAG_EXT_FILTERS	0x0004
154 /* TX mailboxes instead of TX FIFO */
155 #define XCAN_FLAG_TX_MAILBOXES	0x0008
156 /* RX FIFO with each buffer in separate registers at 0x1100
157  * instead of the regular FIFO at 0x50
158  */
159 #define XCAN_FLAG_RX_FIFO_MULTI	0x0010
160 #define XCAN_FLAG_CANFD_2	0x0020
161 
162 enum xcan_ip_type {
163 	XAXI_CAN = 0,
164 	XZYNQ_CANPS,
165 	XAXI_CANFD,
166 	XAXI_CANFD_2_0,
167 };
168 
169 struct xcan_devtype_data {
170 	enum xcan_ip_type cantype;
171 	unsigned int flags;
172 	const struct can_bittiming_const *bittiming_const;
173 	const char *bus_clk_name;
174 	unsigned int btr_ts2_shift;
175 	unsigned int btr_sjw_shift;
176 };
177 
178 /**
179  * struct xcan_priv - This definition define CAN driver instance
180  * @can:			CAN private data structure.
181  * @tx_lock:			Lock for synchronizing TX interrupt handling
182  * @tx_head:			Tx CAN packets ready to send on the queue
183  * @tx_tail:			Tx CAN packets successfully sended on the queue
184  * @tx_max:			Maximum number packets the driver can send
185  * @napi:			NAPI structure
186  * @read_reg:			For reading data from CAN registers
187  * @write_reg:			For writing data to CAN registers
188  * @dev:			Network device data structure
189  * @reg_base:			Ioremapped address to registers
190  * @irq_flags:			For request_irq()
191  * @bus_clk:			Pointer to struct clk
192  * @can_clk:			Pointer to struct clk
193  * @devtype:			Device type specific constants
194  */
195 struct xcan_priv {
196 	struct can_priv can;
197 	spinlock_t tx_lock; /* Lock for synchronizing TX interrupt handling */
198 	unsigned int tx_head;
199 	unsigned int tx_tail;
200 	unsigned int tx_max;
201 	struct napi_struct napi;
202 	u32 (*read_reg)(const struct xcan_priv *priv, enum xcan_reg reg);
203 	void (*write_reg)(const struct xcan_priv *priv, enum xcan_reg reg,
204 			  u32 val);
205 	struct device *dev;
206 	void __iomem *reg_base;
207 	unsigned long irq_flags;
208 	struct clk *bus_clk;
209 	struct clk *can_clk;
210 	struct xcan_devtype_data devtype;
211 };
212 
213 /* CAN Bittiming constants as per Xilinx CAN specs */
214 static const struct can_bittiming_const xcan_bittiming_const = {
215 	.name = DRIVER_NAME,
216 	.tseg1_min = 1,
217 	.tseg1_max = 16,
218 	.tseg2_min = 1,
219 	.tseg2_max = 8,
220 	.sjw_max = 4,
221 	.brp_min = 1,
222 	.brp_max = 256,
223 	.brp_inc = 1,
224 };
225 
226 /* AXI CANFD Arbitration Bittiming constants as per AXI CANFD 1.0 spec */
227 static const struct can_bittiming_const xcan_bittiming_const_canfd = {
228 	.name = DRIVER_NAME,
229 	.tseg1_min = 1,
230 	.tseg1_max = 64,
231 	.tseg2_min = 1,
232 	.tseg2_max = 16,
233 	.sjw_max = 16,
234 	.brp_min = 1,
235 	.brp_max = 256,
236 	.brp_inc = 1,
237 };
238 
239 /* AXI CANFD Data Bittiming constants as per AXI CANFD 1.0 specs */
240 static struct can_bittiming_const xcan_data_bittiming_const_canfd = {
241 	.name = DRIVER_NAME,
242 	.tseg1_min = 1,
243 	.tseg1_max = 16,
244 	.tseg2_min = 1,
245 	.tseg2_max = 8,
246 	.sjw_max = 8,
247 	.brp_min = 1,
248 	.brp_max = 256,
249 	.brp_inc = 1,
250 };
251 
252 /* AXI CANFD 2.0 Arbitration Bittiming constants as per AXI CANFD 2.0 spec */
253 static const struct can_bittiming_const xcan_bittiming_const_canfd2 = {
254 	.name = DRIVER_NAME,
255 	.tseg1_min = 1,
256 	.tseg1_max = 256,
257 	.tseg2_min = 1,
258 	.tseg2_max = 128,
259 	.sjw_max = 128,
260 	.brp_min = 1,
261 	.brp_max = 256,
262 	.brp_inc = 1,
263 };
264 
265 /* AXI CANFD 2.0 Data Bittiming constants as per AXI CANFD 2.0 spec */
266 static struct can_bittiming_const xcan_data_bittiming_const_canfd2 = {
267 	.name = DRIVER_NAME,
268 	.tseg1_min = 1,
269 	.tseg1_max = 32,
270 	.tseg2_min = 1,
271 	.tseg2_max = 16,
272 	.sjw_max = 16,
273 	.brp_min = 1,
274 	.brp_max = 256,
275 	.brp_inc = 1,
276 };
277 
278 /**
279  * xcan_write_reg_le - Write a value to the device register little endian
280  * @priv:	Driver private data structure
281  * @reg:	Register offset
282  * @val:	Value to write at the Register offset
283  *
284  * Write data to the paricular CAN register
285  */
286 static void xcan_write_reg_le(const struct xcan_priv *priv, enum xcan_reg reg,
287 			      u32 val)
288 {
289 	iowrite32(val, priv->reg_base + reg);
290 }
291 
292 /**
293  * xcan_read_reg_le - Read a value from the device register little endian
294  * @priv:	Driver private data structure
295  * @reg:	Register offset
296  *
297  * Read data from the particular CAN register
298  * Return: value read from the CAN register
299  */
300 static u32 xcan_read_reg_le(const struct xcan_priv *priv, enum xcan_reg reg)
301 {
302 	return ioread32(priv->reg_base + reg);
303 }
304 
305 /**
306  * xcan_write_reg_be - Write a value to the device register big endian
307  * @priv:	Driver private data structure
308  * @reg:	Register offset
309  * @val:	Value to write at the Register offset
310  *
311  * Write data to the paricular CAN register
312  */
313 static void xcan_write_reg_be(const struct xcan_priv *priv, enum xcan_reg reg,
314 			      u32 val)
315 {
316 	iowrite32be(val, priv->reg_base + reg);
317 }
318 
319 /**
320  * xcan_read_reg_be - Read a value from the device register big endian
321  * @priv:	Driver private data structure
322  * @reg:	Register offset
323  *
324  * Read data from the particular CAN register
325  * Return: value read from the CAN register
326  */
327 static u32 xcan_read_reg_be(const struct xcan_priv *priv, enum xcan_reg reg)
328 {
329 	return ioread32be(priv->reg_base + reg);
330 }
331 
332 /**
333  * xcan_rx_int_mask - Get the mask for the receive interrupt
334  * @priv:	Driver private data structure
335  *
336  * Return: The receive interrupt mask used by the driver on this HW
337  */
338 static u32 xcan_rx_int_mask(const struct xcan_priv *priv)
339 {
340 	/* RXNEMP is better suited for our use case as it cannot be cleared
341 	 * while the FIFO is non-empty, but CAN FD HW does not have it
342 	 */
343 	if (priv->devtype.flags & XCAN_FLAG_RX_FIFO_MULTI)
344 		return XCAN_IXR_RXOK_MASK;
345 	else
346 		return XCAN_IXR_RXNEMP_MASK;
347 }
348 
349 /**
350  * set_reset_mode - Resets the CAN device mode
351  * @ndev:	Pointer to net_device structure
352  *
353  * This is the driver reset mode routine.The driver
354  * enters into configuration mode.
355  *
356  * Return: 0 on success and failure value on error
357  */
358 static int set_reset_mode(struct net_device *ndev)
359 {
360 	struct xcan_priv *priv = netdev_priv(ndev);
361 	unsigned long timeout;
362 
363 	priv->write_reg(priv, XCAN_SRR_OFFSET, XCAN_SRR_RESET_MASK);
364 
365 	timeout = jiffies + XCAN_TIMEOUT;
366 	while (!(priv->read_reg(priv, XCAN_SR_OFFSET) & XCAN_SR_CONFIG_MASK)) {
367 		if (time_after(jiffies, timeout)) {
368 			netdev_warn(ndev, "timed out for config mode\n");
369 			return -ETIMEDOUT;
370 		}
371 		usleep_range(500, 10000);
372 	}
373 
374 	/* reset clears FIFOs */
375 	priv->tx_head = 0;
376 	priv->tx_tail = 0;
377 
378 	return 0;
379 }
380 
381 /**
382  * xcan_set_bittiming - CAN set bit timing routine
383  * @ndev:	Pointer to net_device structure
384  *
385  * This is the driver set bittiming  routine.
386  * Return: 0 on success and failure value on error
387  */
388 static int xcan_set_bittiming(struct net_device *ndev)
389 {
390 	struct xcan_priv *priv = netdev_priv(ndev);
391 	struct can_bittiming *bt = &priv->can.bittiming;
392 	struct can_bittiming *dbt = &priv->can.data_bittiming;
393 	u32 btr0, btr1;
394 	u32 is_config_mode;
395 
396 	/* Check whether Xilinx CAN is in configuration mode.
397 	 * It cannot set bit timing if Xilinx CAN is not in configuration mode.
398 	 */
399 	is_config_mode = priv->read_reg(priv, XCAN_SR_OFFSET) &
400 				XCAN_SR_CONFIG_MASK;
401 	if (!is_config_mode) {
402 		netdev_alert(ndev,
403 			     "BUG! Cannot set bittiming - CAN is not in config mode\n");
404 		return -EPERM;
405 	}
406 
407 	/* Setting Baud Rate prescalar value in BRPR Register */
408 	btr0 = (bt->brp - 1);
409 
410 	/* Setting Time Segment 1 in BTR Register */
411 	btr1 = (bt->prop_seg + bt->phase_seg1 - 1);
412 
413 	/* Setting Time Segment 2 in BTR Register */
414 	btr1 |= (bt->phase_seg2 - 1) << priv->devtype.btr_ts2_shift;
415 
416 	/* Setting Synchronous jump width in BTR Register */
417 	btr1 |= (bt->sjw - 1) << priv->devtype.btr_sjw_shift;
418 
419 	priv->write_reg(priv, XCAN_BRPR_OFFSET, btr0);
420 	priv->write_reg(priv, XCAN_BTR_OFFSET, btr1);
421 
422 	if (priv->devtype.cantype == XAXI_CANFD ||
423 	    priv->devtype.cantype == XAXI_CANFD_2_0) {
424 		/* Setting Baud Rate prescalar value in F_BRPR Register */
425 		btr0 = dbt->brp - 1;
426 
427 		/* Setting Time Segment 1 in BTR Register */
428 		btr1 = dbt->prop_seg + dbt->phase_seg1 - 1;
429 
430 		/* Setting Time Segment 2 in BTR Register */
431 		btr1 |= (dbt->phase_seg2 - 1) << priv->devtype.btr_ts2_shift;
432 
433 		/* Setting Synchronous jump width in BTR Register */
434 		btr1 |= (dbt->sjw - 1) << priv->devtype.btr_sjw_shift;
435 
436 		priv->write_reg(priv, XCAN_F_BRPR_OFFSET, btr0);
437 		priv->write_reg(priv, XCAN_F_BTR_OFFSET, btr1);
438 	}
439 
440 	netdev_dbg(ndev, "BRPR=0x%08x, BTR=0x%08x\n",
441 		   priv->read_reg(priv, XCAN_BRPR_OFFSET),
442 		   priv->read_reg(priv, XCAN_BTR_OFFSET));
443 
444 	return 0;
445 }
446 
447 /**
448  * xcan_chip_start - This the drivers start routine
449  * @ndev:	Pointer to net_device structure
450  *
451  * This is the drivers start routine.
452  * Based on the State of the CAN device it puts
453  * the CAN device into a proper mode.
454  *
455  * Return: 0 on success and failure value on error
456  */
457 static int xcan_chip_start(struct net_device *ndev)
458 {
459 	struct xcan_priv *priv = netdev_priv(ndev);
460 	u32 reg_msr;
461 	int err;
462 	u32 ier;
463 
464 	/* Check if it is in reset mode */
465 	err = set_reset_mode(ndev);
466 	if (err < 0)
467 		return err;
468 
469 	err = xcan_set_bittiming(ndev);
470 	if (err < 0)
471 		return err;
472 
473 	/* Enable interrupts
474 	 *
475 	 * We enable the ERROR interrupt even with
476 	 * CAN_CTRLMODE_BERR_REPORTING disabled as there is no
477 	 * dedicated interrupt for a state change to
478 	 * ERROR_WARNING/ERROR_PASSIVE.
479 	 */
480 	ier = XCAN_IXR_TXOK_MASK | XCAN_IXR_BSOFF_MASK |
481 		XCAN_IXR_WKUP_MASK | XCAN_IXR_SLP_MASK |
482 		XCAN_IXR_ERROR_MASK | XCAN_IXR_RXOFLW_MASK |
483 		XCAN_IXR_ARBLST_MASK | xcan_rx_int_mask(priv);
484 
485 	if (priv->devtype.flags & XCAN_FLAG_RXMNF)
486 		ier |= XCAN_IXR_RXMNF_MASK;
487 
488 	priv->write_reg(priv, XCAN_IER_OFFSET, ier);
489 
490 	/* Check whether it is loopback mode or normal mode  */
491 	if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)
492 		reg_msr = XCAN_MSR_LBACK_MASK;
493 	else
494 		reg_msr = 0x0;
495 
496 	/* enable the first extended filter, if any, as cores with extended
497 	 * filtering default to non-receipt if all filters are disabled
498 	 */
499 	if (priv->devtype.flags & XCAN_FLAG_EXT_FILTERS)
500 		priv->write_reg(priv, XCAN_AFR_EXT_OFFSET, 0x00000001);
501 
502 	priv->write_reg(priv, XCAN_MSR_OFFSET, reg_msr);
503 	priv->write_reg(priv, XCAN_SRR_OFFSET, XCAN_SRR_CEN_MASK);
504 
505 	netdev_dbg(ndev, "status:#x%08x\n",
506 		   priv->read_reg(priv, XCAN_SR_OFFSET));
507 
508 	priv->can.state = CAN_STATE_ERROR_ACTIVE;
509 	return 0;
510 }
511 
512 /**
513  * xcan_do_set_mode - This sets the mode of the driver
514  * @ndev:	Pointer to net_device structure
515  * @mode:	Tells the mode of the driver
516  *
517  * This check the drivers state and calls the
518  * the corresponding modes to set.
519  *
520  * Return: 0 on success and failure value on error
521  */
522 static int xcan_do_set_mode(struct net_device *ndev, enum can_mode mode)
523 {
524 	int ret;
525 
526 	switch (mode) {
527 	case CAN_MODE_START:
528 		ret = xcan_chip_start(ndev);
529 		if (ret < 0) {
530 			netdev_err(ndev, "xcan_chip_start failed!\n");
531 			return ret;
532 		}
533 		netif_wake_queue(ndev);
534 		break;
535 	default:
536 		ret = -EOPNOTSUPP;
537 		break;
538 	}
539 
540 	return ret;
541 }
542 
543 /**
544  * xcan_write_frame - Write a frame to HW
545  * @ndev:		Pointer to net_device structure
546  * @skb:		sk_buff pointer that contains data to be Txed
547  * @frame_offset:	Register offset to write the frame to
548  */
549 static void xcan_write_frame(struct net_device *ndev, struct sk_buff *skb,
550 			     int frame_offset)
551 {
552 	u32 id, dlc, data[2] = {0, 0};
553 	struct canfd_frame *cf = (struct canfd_frame *)skb->data;
554 	u32 ramoff, dwindex = 0, i;
555 	struct xcan_priv *priv = netdev_priv(ndev);
556 
557 	/* Watch carefully on the bit sequence */
558 	if (cf->can_id & CAN_EFF_FLAG) {
559 		/* Extended CAN ID format */
560 		id = ((cf->can_id & CAN_EFF_MASK) << XCAN_IDR_ID2_SHIFT) &
561 			XCAN_IDR_ID2_MASK;
562 		id |= (((cf->can_id & CAN_EFF_MASK) >>
563 			(CAN_EFF_ID_BITS - CAN_SFF_ID_BITS)) <<
564 			XCAN_IDR_ID1_SHIFT) & XCAN_IDR_ID1_MASK;
565 
566 		/* The substibute remote TX request bit should be "1"
567 		 * for extended frames as in the Xilinx CAN datasheet
568 		 */
569 		id |= XCAN_IDR_IDE_MASK | XCAN_IDR_SRR_MASK;
570 
571 		if (cf->can_id & CAN_RTR_FLAG)
572 			/* Extended frames remote TX request */
573 			id |= XCAN_IDR_RTR_MASK;
574 	} else {
575 		/* Standard CAN ID format */
576 		id = ((cf->can_id & CAN_SFF_MASK) << XCAN_IDR_ID1_SHIFT) &
577 			XCAN_IDR_ID1_MASK;
578 
579 		if (cf->can_id & CAN_RTR_FLAG)
580 			/* Standard frames remote TX request */
581 			id |= XCAN_IDR_SRR_MASK;
582 	}
583 
584 	dlc = can_len2dlc(cf->len) << XCAN_DLCR_DLC_SHIFT;
585 	if (can_is_canfd_skb(skb)) {
586 		if (cf->flags & CANFD_BRS)
587 			dlc |= XCAN_DLCR_BRS_MASK;
588 		dlc |= XCAN_DLCR_EDL_MASK;
589 	}
590 
591 	if (!(priv->devtype.flags & XCAN_FLAG_TX_MAILBOXES) &&
592 	    (priv->devtype.flags & XCAN_FLAG_TXFEMP))
593 		can_put_echo_skb(skb, ndev, priv->tx_head % priv->tx_max);
594 	else
595 		can_put_echo_skb(skb, ndev, 0);
596 
597 	priv->tx_head++;
598 
599 	priv->write_reg(priv, XCAN_FRAME_ID_OFFSET(frame_offset), id);
600 	/* If the CAN frame is RTR frame this write triggers transmission
601 	 * (not on CAN FD)
602 	 */
603 	priv->write_reg(priv, XCAN_FRAME_DLC_OFFSET(frame_offset), dlc);
604 	if (priv->devtype.cantype == XAXI_CANFD ||
605 	    priv->devtype.cantype == XAXI_CANFD_2_0) {
606 		for (i = 0; i < cf->len; i += 4) {
607 			ramoff = XCANFD_FRAME_DW_OFFSET(frame_offset) +
608 					(dwindex * XCANFD_DW_BYTES);
609 			priv->write_reg(priv, ramoff,
610 					be32_to_cpup((__be32 *)(cf->data + i)));
611 			dwindex++;
612 		}
613 	} else {
614 		if (cf->len > 0)
615 			data[0] = be32_to_cpup((__be32 *)(cf->data + 0));
616 		if (cf->len > 4)
617 			data[1] = be32_to_cpup((__be32 *)(cf->data + 4));
618 
619 		if (!(cf->can_id & CAN_RTR_FLAG)) {
620 			priv->write_reg(priv,
621 					XCAN_FRAME_DW1_OFFSET(frame_offset),
622 					data[0]);
623 			/* If the CAN frame is Standard/Extended frame this
624 			 * write triggers transmission (not on CAN FD)
625 			 */
626 			priv->write_reg(priv,
627 					XCAN_FRAME_DW2_OFFSET(frame_offset),
628 					data[1]);
629 		}
630 	}
631 }
632 
633 /**
634  * xcan_start_xmit_fifo - Starts the transmission (FIFO mode)
635  * @skb:	sk_buff pointer that contains data to be Txed
636  * @ndev:	Pointer to net_device structure
637  *
638  * Return: 0 on success, -ENOSPC if FIFO is full.
639  */
640 static int xcan_start_xmit_fifo(struct sk_buff *skb, struct net_device *ndev)
641 {
642 	struct xcan_priv *priv = netdev_priv(ndev);
643 	unsigned long flags;
644 
645 	/* Check if the TX buffer is full */
646 	if (unlikely(priv->read_reg(priv, XCAN_SR_OFFSET) &
647 			XCAN_SR_TXFLL_MASK))
648 		return -ENOSPC;
649 
650 	spin_lock_irqsave(&priv->tx_lock, flags);
651 
652 	xcan_write_frame(ndev, skb, XCAN_TXFIFO_OFFSET);
653 
654 	/* Clear TX-FIFO-empty interrupt for xcan_tx_interrupt() */
655 	if (priv->tx_max > 1)
656 		priv->write_reg(priv, XCAN_ICR_OFFSET, XCAN_IXR_TXFEMP_MASK);
657 
658 	/* Check if the TX buffer is full */
659 	if ((priv->tx_head - priv->tx_tail) == priv->tx_max)
660 		netif_stop_queue(ndev);
661 
662 	spin_unlock_irqrestore(&priv->tx_lock, flags);
663 
664 	return 0;
665 }
666 
667 /**
668  * xcan_start_xmit_mailbox - Starts the transmission (mailbox mode)
669  * @skb:	sk_buff pointer that contains data to be Txed
670  * @ndev:	Pointer to net_device structure
671  *
672  * Return: 0 on success, -ENOSPC if there is no space
673  */
674 static int xcan_start_xmit_mailbox(struct sk_buff *skb, struct net_device *ndev)
675 {
676 	struct xcan_priv *priv = netdev_priv(ndev);
677 	unsigned long flags;
678 
679 	if (unlikely(priv->read_reg(priv, XCAN_TRR_OFFSET) &
680 		     BIT(XCAN_TX_MAILBOX_IDX)))
681 		return -ENOSPC;
682 
683 	spin_lock_irqsave(&priv->tx_lock, flags);
684 
685 	xcan_write_frame(ndev, skb,
686 			 XCAN_TXMSG_FRAME_OFFSET(XCAN_TX_MAILBOX_IDX));
687 
688 	/* Mark buffer as ready for transmit */
689 	priv->write_reg(priv, XCAN_TRR_OFFSET, BIT(XCAN_TX_MAILBOX_IDX));
690 
691 	netif_stop_queue(ndev);
692 
693 	spin_unlock_irqrestore(&priv->tx_lock, flags);
694 
695 	return 0;
696 }
697 
698 /**
699  * xcan_start_xmit - Starts the transmission
700  * @skb:	sk_buff pointer that contains data to be Txed
701  * @ndev:	Pointer to net_device structure
702  *
703  * This function is invoked from upper layers to initiate transmission.
704  *
705  * Return: NETDEV_TX_OK on success and NETDEV_TX_BUSY when the tx queue is full
706  */
707 static netdev_tx_t xcan_start_xmit(struct sk_buff *skb, struct net_device *ndev)
708 {
709 	struct xcan_priv *priv = netdev_priv(ndev);
710 	int ret;
711 
712 	if (can_dropped_invalid_skb(ndev, skb))
713 		return NETDEV_TX_OK;
714 
715 	if (priv->devtype.flags & XCAN_FLAG_TX_MAILBOXES)
716 		ret = xcan_start_xmit_mailbox(skb, ndev);
717 	else
718 		ret = xcan_start_xmit_fifo(skb, ndev);
719 
720 	if (ret < 0) {
721 		netdev_err(ndev, "BUG!, TX full when queue awake!\n");
722 		netif_stop_queue(ndev);
723 		return NETDEV_TX_BUSY;
724 	}
725 
726 	return NETDEV_TX_OK;
727 }
728 
729 /**
730  * xcan_rx -  Is called from CAN isr to complete the received
731  *		frame  processing
732  * @ndev:	Pointer to net_device structure
733  * @frame_base:	Register offset to the frame to be read
734  *
735  * This function is invoked from the CAN isr(poll) to process the Rx frames. It
736  * does minimal processing and invokes "netif_receive_skb" to complete further
737  * processing.
738  * Return: 1 on success and 0 on failure.
739  */
740 static int xcan_rx(struct net_device *ndev, int frame_base)
741 {
742 	struct xcan_priv *priv = netdev_priv(ndev);
743 	struct net_device_stats *stats = &ndev->stats;
744 	struct can_frame *cf;
745 	struct sk_buff *skb;
746 	u32 id_xcan, dlc, data[2] = {0, 0};
747 
748 	skb = alloc_can_skb(ndev, &cf);
749 	if (unlikely(!skb)) {
750 		stats->rx_dropped++;
751 		return 0;
752 	}
753 
754 	/* Read a frame from Xilinx zynq CANPS */
755 	id_xcan = priv->read_reg(priv, XCAN_FRAME_ID_OFFSET(frame_base));
756 	dlc = priv->read_reg(priv, XCAN_FRAME_DLC_OFFSET(frame_base)) >>
757 				   XCAN_DLCR_DLC_SHIFT;
758 
759 	/* Change Xilinx CAN data length format to socketCAN data format */
760 	cf->can_dlc = get_can_dlc(dlc);
761 
762 	/* Change Xilinx CAN ID format to socketCAN ID format */
763 	if (id_xcan & XCAN_IDR_IDE_MASK) {
764 		/* The received frame is an Extended format frame */
765 		cf->can_id = (id_xcan & XCAN_IDR_ID1_MASK) >> 3;
766 		cf->can_id |= (id_xcan & XCAN_IDR_ID2_MASK) >>
767 				XCAN_IDR_ID2_SHIFT;
768 		cf->can_id |= CAN_EFF_FLAG;
769 		if (id_xcan & XCAN_IDR_RTR_MASK)
770 			cf->can_id |= CAN_RTR_FLAG;
771 	} else {
772 		/* The received frame is a standard format frame */
773 		cf->can_id = (id_xcan & XCAN_IDR_ID1_MASK) >>
774 				XCAN_IDR_ID1_SHIFT;
775 		if (id_xcan & XCAN_IDR_SRR_MASK)
776 			cf->can_id |= CAN_RTR_FLAG;
777 	}
778 
779 	/* DW1/DW2 must always be read to remove message from RXFIFO */
780 	data[0] = priv->read_reg(priv, XCAN_FRAME_DW1_OFFSET(frame_base));
781 	data[1] = priv->read_reg(priv, XCAN_FRAME_DW2_OFFSET(frame_base));
782 
783 	if (!(cf->can_id & CAN_RTR_FLAG)) {
784 		/* Change Xilinx CAN data format to socketCAN data format */
785 		if (cf->can_dlc > 0)
786 			*(__be32 *)(cf->data) = cpu_to_be32(data[0]);
787 		if (cf->can_dlc > 4)
788 			*(__be32 *)(cf->data + 4) = cpu_to_be32(data[1]);
789 	}
790 
791 	stats->rx_bytes += cf->can_dlc;
792 	stats->rx_packets++;
793 	netif_receive_skb(skb);
794 
795 	return 1;
796 }
797 
798 /**
799  * xcanfd_rx -  Is called from CAN isr to complete the received
800  *		frame  processing
801  * @ndev:	Pointer to net_device structure
802  * @frame_base:	Register offset to the frame to be read
803  *
804  * This function is invoked from the CAN isr(poll) to process the Rx frames. It
805  * does minimal processing and invokes "netif_receive_skb" to complete further
806  * processing.
807  * Return: 1 on success and 0 on failure.
808  */
809 static int xcanfd_rx(struct net_device *ndev, int frame_base)
810 {
811 	struct xcan_priv *priv = netdev_priv(ndev);
812 	struct net_device_stats *stats = &ndev->stats;
813 	struct canfd_frame *cf;
814 	struct sk_buff *skb;
815 	u32 id_xcan, dlc, data[2] = {0, 0}, dwindex = 0, i, dw_offset;
816 
817 	id_xcan = priv->read_reg(priv, XCAN_FRAME_ID_OFFSET(frame_base));
818 	dlc = priv->read_reg(priv, XCAN_FRAME_DLC_OFFSET(frame_base));
819 	if (dlc & XCAN_DLCR_EDL_MASK)
820 		skb = alloc_canfd_skb(ndev, &cf);
821 	else
822 		skb = alloc_can_skb(ndev, (struct can_frame **)&cf);
823 
824 	if (unlikely(!skb)) {
825 		stats->rx_dropped++;
826 		return 0;
827 	}
828 
829 	/* Change Xilinx CANFD data length format to socketCAN data
830 	 * format
831 	 */
832 	if (dlc & XCAN_DLCR_EDL_MASK)
833 		cf->len = can_dlc2len((dlc & XCAN_DLCR_DLC_MASK) >>
834 				  XCAN_DLCR_DLC_SHIFT);
835 	else
836 		cf->len = get_can_dlc((dlc & XCAN_DLCR_DLC_MASK) >>
837 					  XCAN_DLCR_DLC_SHIFT);
838 
839 	/* Change Xilinx CAN ID format to socketCAN ID format */
840 	if (id_xcan & XCAN_IDR_IDE_MASK) {
841 		/* The received frame is an Extended format frame */
842 		cf->can_id = (id_xcan & XCAN_IDR_ID1_MASK) >> 3;
843 		cf->can_id |= (id_xcan & XCAN_IDR_ID2_MASK) >>
844 				XCAN_IDR_ID2_SHIFT;
845 		cf->can_id |= CAN_EFF_FLAG;
846 		if (id_xcan & XCAN_IDR_RTR_MASK)
847 			cf->can_id |= CAN_RTR_FLAG;
848 	} else {
849 		/* The received frame is a standard format frame */
850 		cf->can_id = (id_xcan & XCAN_IDR_ID1_MASK) >>
851 				XCAN_IDR_ID1_SHIFT;
852 		if (!(dlc & XCAN_DLCR_EDL_MASK) && (id_xcan &
853 					XCAN_IDR_SRR_MASK))
854 			cf->can_id |= CAN_RTR_FLAG;
855 	}
856 
857 	/* Check the frame received is FD or not*/
858 	if (dlc & XCAN_DLCR_EDL_MASK) {
859 		for (i = 0; i < cf->len; i += 4) {
860 			dw_offset = XCANFD_FRAME_DW_OFFSET(frame_base) +
861 					(dwindex * XCANFD_DW_BYTES);
862 			data[0] = priv->read_reg(priv, dw_offset);
863 			*(__be32 *)(cf->data + i) = cpu_to_be32(data[0]);
864 			dwindex++;
865 		}
866 	} else {
867 		for (i = 0; i < cf->len; i += 4) {
868 			dw_offset = XCANFD_FRAME_DW_OFFSET(frame_base);
869 			data[0] = priv->read_reg(priv, dw_offset + i);
870 			*(__be32 *)(cf->data + i) = cpu_to_be32(data[0]);
871 		}
872 	}
873 	stats->rx_bytes += cf->len;
874 	stats->rx_packets++;
875 	netif_receive_skb(skb);
876 
877 	return 1;
878 }
879 
880 /**
881  * xcan_current_error_state - Get current error state from HW
882  * @ndev:	Pointer to net_device structure
883  *
884  * Checks the current CAN error state from the HW. Note that this
885  * only checks for ERROR_PASSIVE and ERROR_WARNING.
886  *
887  * Return:
888  * ERROR_PASSIVE or ERROR_WARNING if either is active, ERROR_ACTIVE
889  * otherwise.
890  */
891 static enum can_state xcan_current_error_state(struct net_device *ndev)
892 {
893 	struct xcan_priv *priv = netdev_priv(ndev);
894 	u32 status = priv->read_reg(priv, XCAN_SR_OFFSET);
895 
896 	if ((status & XCAN_SR_ESTAT_MASK) == XCAN_SR_ESTAT_MASK)
897 		return CAN_STATE_ERROR_PASSIVE;
898 	else if (status & XCAN_SR_ERRWRN_MASK)
899 		return CAN_STATE_ERROR_WARNING;
900 	else
901 		return CAN_STATE_ERROR_ACTIVE;
902 }
903 
904 /**
905  * xcan_set_error_state - Set new CAN error state
906  * @ndev:	Pointer to net_device structure
907  * @new_state:	The new CAN state to be set
908  * @cf:		Error frame to be populated or NULL
909  *
910  * Set new CAN error state for the device, updating statistics and
911  * populating the error frame if given.
912  */
913 static void xcan_set_error_state(struct net_device *ndev,
914 				 enum can_state new_state,
915 				 struct can_frame *cf)
916 {
917 	struct xcan_priv *priv = netdev_priv(ndev);
918 	u32 ecr = priv->read_reg(priv, XCAN_ECR_OFFSET);
919 	u32 txerr = ecr & XCAN_ECR_TEC_MASK;
920 	u32 rxerr = (ecr & XCAN_ECR_REC_MASK) >> XCAN_ESR_REC_SHIFT;
921 	enum can_state tx_state = txerr >= rxerr ? new_state : 0;
922 	enum can_state rx_state = txerr <= rxerr ? new_state : 0;
923 
924 	/* non-ERROR states are handled elsewhere */
925 	if (WARN_ON(new_state > CAN_STATE_ERROR_PASSIVE))
926 		return;
927 
928 	can_change_state(ndev, cf, tx_state, rx_state);
929 
930 	if (cf) {
931 		cf->data[6] = txerr;
932 		cf->data[7] = rxerr;
933 	}
934 }
935 
936 /**
937  * xcan_update_error_state_after_rxtx - Update CAN error state after RX/TX
938  * @ndev:	Pointer to net_device structure
939  *
940  * If the device is in a ERROR-WARNING or ERROR-PASSIVE state, check if
941  * the performed RX/TX has caused it to drop to a lesser state and set
942  * the interface state accordingly.
943  */
944 static void xcan_update_error_state_after_rxtx(struct net_device *ndev)
945 {
946 	struct xcan_priv *priv = netdev_priv(ndev);
947 	enum can_state old_state = priv->can.state;
948 	enum can_state new_state;
949 
950 	/* changing error state due to successful frame RX/TX can only
951 	 * occur from these states
952 	 */
953 	if (old_state != CAN_STATE_ERROR_WARNING &&
954 	    old_state != CAN_STATE_ERROR_PASSIVE)
955 		return;
956 
957 	new_state = xcan_current_error_state(ndev);
958 
959 	if (new_state != old_state) {
960 		struct sk_buff *skb;
961 		struct can_frame *cf;
962 
963 		skb = alloc_can_err_skb(ndev, &cf);
964 
965 		xcan_set_error_state(ndev, new_state, skb ? cf : NULL);
966 
967 		if (skb) {
968 			struct net_device_stats *stats = &ndev->stats;
969 
970 			stats->rx_packets++;
971 			stats->rx_bytes += cf->can_dlc;
972 			netif_rx(skb);
973 		}
974 	}
975 }
976 
977 /**
978  * xcan_err_interrupt - error frame Isr
979  * @ndev:	net_device pointer
980  * @isr:	interrupt status register value
981  *
982  * This is the CAN error interrupt and it will
983  * check the the type of error and forward the error
984  * frame to upper layers.
985  */
986 static void xcan_err_interrupt(struct net_device *ndev, u32 isr)
987 {
988 	struct xcan_priv *priv = netdev_priv(ndev);
989 	struct net_device_stats *stats = &ndev->stats;
990 	struct can_frame cf = { };
991 	u32 err_status;
992 
993 	err_status = priv->read_reg(priv, XCAN_ESR_OFFSET);
994 	priv->write_reg(priv, XCAN_ESR_OFFSET, err_status);
995 
996 	if (isr & XCAN_IXR_BSOFF_MASK) {
997 		priv->can.state = CAN_STATE_BUS_OFF;
998 		priv->can.can_stats.bus_off++;
999 		/* Leave device in Config Mode in bus-off state */
1000 		priv->write_reg(priv, XCAN_SRR_OFFSET, XCAN_SRR_RESET_MASK);
1001 		can_bus_off(ndev);
1002 		cf.can_id |= CAN_ERR_BUSOFF;
1003 	} else {
1004 		enum can_state new_state = xcan_current_error_state(ndev);
1005 
1006 		if (new_state != priv->can.state)
1007 			xcan_set_error_state(ndev, new_state, &cf);
1008 	}
1009 
1010 	/* Check for Arbitration lost interrupt */
1011 	if (isr & XCAN_IXR_ARBLST_MASK) {
1012 		priv->can.can_stats.arbitration_lost++;
1013 		cf.can_id |= CAN_ERR_LOSTARB;
1014 		cf.data[0] = CAN_ERR_LOSTARB_UNSPEC;
1015 	}
1016 
1017 	/* Check for RX FIFO Overflow interrupt */
1018 	if (isr & XCAN_IXR_RXOFLW_MASK) {
1019 		stats->rx_over_errors++;
1020 		stats->rx_errors++;
1021 		cf.can_id |= CAN_ERR_CRTL;
1022 		cf.data[1] |= CAN_ERR_CRTL_RX_OVERFLOW;
1023 	}
1024 
1025 	/* Check for RX Match Not Finished interrupt */
1026 	if (isr & XCAN_IXR_RXMNF_MASK) {
1027 		stats->rx_dropped++;
1028 		stats->rx_errors++;
1029 		netdev_err(ndev, "RX match not finished, frame discarded\n");
1030 		cf.can_id |= CAN_ERR_CRTL;
1031 		cf.data[1] |= CAN_ERR_CRTL_UNSPEC;
1032 	}
1033 
1034 	/* Check for error interrupt */
1035 	if (isr & XCAN_IXR_ERROR_MASK) {
1036 		bool berr_reporting = false;
1037 
1038 		if (priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING) {
1039 			berr_reporting = true;
1040 			cf.can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
1041 		}
1042 
1043 		/* Check for Ack error interrupt */
1044 		if (err_status & XCAN_ESR_ACKER_MASK) {
1045 			stats->tx_errors++;
1046 			if (berr_reporting) {
1047 				cf.can_id |= CAN_ERR_ACK;
1048 				cf.data[3] = CAN_ERR_PROT_LOC_ACK;
1049 			}
1050 		}
1051 
1052 		/* Check for Bit error interrupt */
1053 		if (err_status & XCAN_ESR_BERR_MASK) {
1054 			stats->tx_errors++;
1055 			if (berr_reporting) {
1056 				cf.can_id |= CAN_ERR_PROT;
1057 				cf.data[2] = CAN_ERR_PROT_BIT;
1058 			}
1059 		}
1060 
1061 		/* Check for Stuff error interrupt */
1062 		if (err_status & XCAN_ESR_STER_MASK) {
1063 			stats->rx_errors++;
1064 			if (berr_reporting) {
1065 				cf.can_id |= CAN_ERR_PROT;
1066 				cf.data[2] = CAN_ERR_PROT_STUFF;
1067 			}
1068 		}
1069 
1070 		/* Check for Form error interrupt */
1071 		if (err_status & XCAN_ESR_FMER_MASK) {
1072 			stats->rx_errors++;
1073 			if (berr_reporting) {
1074 				cf.can_id |= CAN_ERR_PROT;
1075 				cf.data[2] = CAN_ERR_PROT_FORM;
1076 			}
1077 		}
1078 
1079 		/* Check for CRC error interrupt */
1080 		if (err_status & XCAN_ESR_CRCER_MASK) {
1081 			stats->rx_errors++;
1082 			if (berr_reporting) {
1083 				cf.can_id |= CAN_ERR_PROT;
1084 				cf.data[3] = CAN_ERR_PROT_LOC_CRC_SEQ;
1085 			}
1086 		}
1087 		priv->can.can_stats.bus_error++;
1088 	}
1089 
1090 	if (cf.can_id) {
1091 		struct can_frame *skb_cf;
1092 		struct sk_buff *skb = alloc_can_err_skb(ndev, &skb_cf);
1093 
1094 		if (skb) {
1095 			skb_cf->can_id |= cf.can_id;
1096 			memcpy(skb_cf->data, cf.data, CAN_ERR_DLC);
1097 			stats->rx_packets++;
1098 			stats->rx_bytes += CAN_ERR_DLC;
1099 			netif_rx(skb);
1100 		}
1101 	}
1102 
1103 	netdev_dbg(ndev, "%s: error status register:0x%x\n",
1104 		   __func__, priv->read_reg(priv, XCAN_ESR_OFFSET));
1105 }
1106 
1107 /**
1108  * xcan_state_interrupt - It will check the state of the CAN device
1109  * @ndev:	net_device pointer
1110  * @isr:	interrupt status register value
1111  *
1112  * This will checks the state of the CAN device
1113  * and puts the device into appropriate state.
1114  */
1115 static void xcan_state_interrupt(struct net_device *ndev, u32 isr)
1116 {
1117 	struct xcan_priv *priv = netdev_priv(ndev);
1118 
1119 	/* Check for Sleep interrupt if set put CAN device in sleep state */
1120 	if (isr & XCAN_IXR_SLP_MASK)
1121 		priv->can.state = CAN_STATE_SLEEPING;
1122 
1123 	/* Check for Wake up interrupt if set put CAN device in Active state */
1124 	if (isr & XCAN_IXR_WKUP_MASK)
1125 		priv->can.state = CAN_STATE_ERROR_ACTIVE;
1126 }
1127 
1128 /**
1129  * xcan_rx_fifo_get_next_frame - Get register offset of next RX frame
1130  * @priv:	Driver private data structure
1131  *
1132  * Return: Register offset of the next frame in RX FIFO.
1133  */
1134 static int xcan_rx_fifo_get_next_frame(struct xcan_priv *priv)
1135 {
1136 	int offset;
1137 
1138 	if (priv->devtype.flags & XCAN_FLAG_RX_FIFO_MULTI) {
1139 		u32 fsr, mask;
1140 
1141 		/* clear RXOK before the is-empty check so that any newly
1142 		 * received frame will reassert it without a race
1143 		 */
1144 		priv->write_reg(priv, XCAN_ICR_OFFSET, XCAN_IXR_RXOK_MASK);
1145 
1146 		fsr = priv->read_reg(priv, XCAN_FSR_OFFSET);
1147 
1148 		/* check if RX FIFO is empty */
1149 		if (priv->devtype.flags & XCAN_FLAG_CANFD_2)
1150 			mask = XCAN_2_FSR_FL_MASK;
1151 		else
1152 			mask = XCAN_FSR_FL_MASK;
1153 
1154 		if (!(fsr & mask))
1155 			return -ENOENT;
1156 
1157 		if (priv->devtype.flags & XCAN_FLAG_CANFD_2)
1158 			offset =
1159 			  XCAN_RXMSG_2_FRAME_OFFSET(fsr & XCAN_2_FSR_RI_MASK);
1160 		else
1161 			offset =
1162 			  XCAN_RXMSG_FRAME_OFFSET(fsr & XCAN_FSR_RI_MASK);
1163 
1164 	} else {
1165 		/* check if RX FIFO is empty */
1166 		if (!(priv->read_reg(priv, XCAN_ISR_OFFSET) &
1167 		      XCAN_IXR_RXNEMP_MASK))
1168 			return -ENOENT;
1169 
1170 		/* frames are read from a static offset */
1171 		offset = XCAN_RXFIFO_OFFSET;
1172 	}
1173 
1174 	return offset;
1175 }
1176 
1177 /**
1178  * xcan_rx_poll - Poll routine for rx packets (NAPI)
1179  * @napi:	napi structure pointer
1180  * @quota:	Max number of rx packets to be processed.
1181  *
1182  * This is the poll routine for rx part.
1183  * It will process the packets maximux quota value.
1184  *
1185  * Return: number of packets received
1186  */
1187 static int xcan_rx_poll(struct napi_struct *napi, int quota)
1188 {
1189 	struct net_device *ndev = napi->dev;
1190 	struct xcan_priv *priv = netdev_priv(ndev);
1191 	u32 ier;
1192 	int work_done = 0;
1193 	int frame_offset;
1194 
1195 	while ((frame_offset = xcan_rx_fifo_get_next_frame(priv)) >= 0 &&
1196 	       (work_done < quota)) {
1197 		if (xcan_rx_int_mask(priv) & XCAN_IXR_RXOK_MASK)
1198 			work_done += xcanfd_rx(ndev, frame_offset);
1199 		else
1200 			work_done += xcan_rx(ndev, frame_offset);
1201 
1202 		if (priv->devtype.flags & XCAN_FLAG_RX_FIFO_MULTI)
1203 			/* increment read index */
1204 			priv->write_reg(priv, XCAN_FSR_OFFSET,
1205 					XCAN_FSR_IRI_MASK);
1206 		else
1207 			/* clear rx-not-empty (will actually clear only if
1208 			 * empty)
1209 			 */
1210 			priv->write_reg(priv, XCAN_ICR_OFFSET,
1211 					XCAN_IXR_RXNEMP_MASK);
1212 	}
1213 
1214 	if (work_done) {
1215 		can_led_event(ndev, CAN_LED_EVENT_RX);
1216 		xcan_update_error_state_after_rxtx(ndev);
1217 	}
1218 
1219 	if (work_done < quota) {
1220 		napi_complete_done(napi, work_done);
1221 		ier = priv->read_reg(priv, XCAN_IER_OFFSET);
1222 		ier |= xcan_rx_int_mask(priv);
1223 		priv->write_reg(priv, XCAN_IER_OFFSET, ier);
1224 	}
1225 	return work_done;
1226 }
1227 
1228 /**
1229  * xcan_tx_interrupt - Tx Done Isr
1230  * @ndev:	net_device pointer
1231  * @isr:	Interrupt status register value
1232  */
1233 static void xcan_tx_interrupt(struct net_device *ndev, u32 isr)
1234 {
1235 	struct xcan_priv *priv = netdev_priv(ndev);
1236 	struct net_device_stats *stats = &ndev->stats;
1237 	unsigned int frames_in_fifo;
1238 	int frames_sent = 1; /* TXOK => at least 1 frame was sent */
1239 	unsigned long flags;
1240 	int retries = 0;
1241 
1242 	/* Synchronize with xmit as we need to know the exact number
1243 	 * of frames in the FIFO to stay in sync due to the TXFEMP
1244 	 * handling.
1245 	 * This also prevents a race between netif_wake_queue() and
1246 	 * netif_stop_queue().
1247 	 */
1248 	spin_lock_irqsave(&priv->tx_lock, flags);
1249 
1250 	frames_in_fifo = priv->tx_head - priv->tx_tail;
1251 
1252 	if (WARN_ON_ONCE(frames_in_fifo == 0)) {
1253 		/* clear TXOK anyway to avoid getting back here */
1254 		priv->write_reg(priv, XCAN_ICR_OFFSET, XCAN_IXR_TXOK_MASK);
1255 		spin_unlock_irqrestore(&priv->tx_lock, flags);
1256 		return;
1257 	}
1258 
1259 	/* Check if 2 frames were sent (TXOK only means that at least 1
1260 	 * frame was sent).
1261 	 */
1262 	if (frames_in_fifo > 1) {
1263 		WARN_ON(frames_in_fifo > priv->tx_max);
1264 
1265 		/* Synchronize TXOK and isr so that after the loop:
1266 		 * (1) isr variable is up-to-date at least up to TXOK clear
1267 		 *     time. This avoids us clearing a TXOK of a second frame
1268 		 *     but not noticing that the FIFO is now empty and thus
1269 		 *     marking only a single frame as sent.
1270 		 * (2) No TXOK is left. Having one could mean leaving a
1271 		 *     stray TXOK as we might process the associated frame
1272 		 *     via TXFEMP handling as we read TXFEMP *after* TXOK
1273 		 *     clear to satisfy (1).
1274 		 */
1275 		while ((isr & XCAN_IXR_TXOK_MASK) &&
1276 		       !WARN_ON(++retries == 100)) {
1277 			priv->write_reg(priv, XCAN_ICR_OFFSET,
1278 					XCAN_IXR_TXOK_MASK);
1279 			isr = priv->read_reg(priv, XCAN_ISR_OFFSET);
1280 		}
1281 
1282 		if (isr & XCAN_IXR_TXFEMP_MASK) {
1283 			/* nothing in FIFO anymore */
1284 			frames_sent = frames_in_fifo;
1285 		}
1286 	} else {
1287 		/* single frame in fifo, just clear TXOK */
1288 		priv->write_reg(priv, XCAN_ICR_OFFSET, XCAN_IXR_TXOK_MASK);
1289 	}
1290 
1291 	while (frames_sent--) {
1292 		stats->tx_bytes += can_get_echo_skb(ndev, priv->tx_tail %
1293 						    priv->tx_max);
1294 		priv->tx_tail++;
1295 		stats->tx_packets++;
1296 	}
1297 
1298 	netif_wake_queue(ndev);
1299 
1300 	spin_unlock_irqrestore(&priv->tx_lock, flags);
1301 
1302 	can_led_event(ndev, CAN_LED_EVENT_TX);
1303 	xcan_update_error_state_after_rxtx(ndev);
1304 }
1305 
1306 /**
1307  * xcan_interrupt - CAN Isr
1308  * @irq:	irq number
1309  * @dev_id:	device id poniter
1310  *
1311  * This is the xilinx CAN Isr. It checks for the type of interrupt
1312  * and invokes the corresponding ISR.
1313  *
1314  * Return:
1315  * IRQ_NONE - If CAN device is in sleep mode, IRQ_HANDLED otherwise
1316  */
1317 static irqreturn_t xcan_interrupt(int irq, void *dev_id)
1318 {
1319 	struct net_device *ndev = (struct net_device *)dev_id;
1320 	struct xcan_priv *priv = netdev_priv(ndev);
1321 	u32 isr, ier;
1322 	u32 isr_errors;
1323 	u32 rx_int_mask = xcan_rx_int_mask(priv);
1324 
1325 	/* Get the interrupt status from Xilinx CAN */
1326 	isr = priv->read_reg(priv, XCAN_ISR_OFFSET);
1327 	if (!isr)
1328 		return IRQ_NONE;
1329 
1330 	/* Check for the type of interrupt and Processing it */
1331 	if (isr & (XCAN_IXR_SLP_MASK | XCAN_IXR_WKUP_MASK)) {
1332 		priv->write_reg(priv, XCAN_ICR_OFFSET, (XCAN_IXR_SLP_MASK |
1333 				XCAN_IXR_WKUP_MASK));
1334 		xcan_state_interrupt(ndev, isr);
1335 	}
1336 
1337 	/* Check for Tx interrupt and Processing it */
1338 	if (isr & XCAN_IXR_TXOK_MASK)
1339 		xcan_tx_interrupt(ndev, isr);
1340 
1341 	/* Check for the type of error interrupt and Processing it */
1342 	isr_errors = isr & (XCAN_IXR_ERROR_MASK | XCAN_IXR_RXOFLW_MASK |
1343 			    XCAN_IXR_BSOFF_MASK | XCAN_IXR_ARBLST_MASK |
1344 			    XCAN_IXR_RXMNF_MASK);
1345 	if (isr_errors) {
1346 		priv->write_reg(priv, XCAN_ICR_OFFSET, isr_errors);
1347 		xcan_err_interrupt(ndev, isr);
1348 	}
1349 
1350 	/* Check for the type of receive interrupt and Processing it */
1351 	if (isr & rx_int_mask) {
1352 		ier = priv->read_reg(priv, XCAN_IER_OFFSET);
1353 		ier &= ~rx_int_mask;
1354 		priv->write_reg(priv, XCAN_IER_OFFSET, ier);
1355 		napi_schedule(&priv->napi);
1356 	}
1357 	return IRQ_HANDLED;
1358 }
1359 
1360 /**
1361  * xcan_chip_stop - Driver stop routine
1362  * @ndev:	Pointer to net_device structure
1363  *
1364  * This is the drivers stop routine. It will disable the
1365  * interrupts and put the device into configuration mode.
1366  */
1367 static void xcan_chip_stop(struct net_device *ndev)
1368 {
1369 	struct xcan_priv *priv = netdev_priv(ndev);
1370 
1371 	/* Disable interrupts and leave the can in configuration mode */
1372 	set_reset_mode(ndev);
1373 	priv->can.state = CAN_STATE_STOPPED;
1374 }
1375 
1376 /**
1377  * xcan_open - Driver open routine
1378  * @ndev:	Pointer to net_device structure
1379  *
1380  * This is the driver open routine.
1381  * Return: 0 on success and failure value on error
1382  */
1383 static int xcan_open(struct net_device *ndev)
1384 {
1385 	struct xcan_priv *priv = netdev_priv(ndev);
1386 	int ret;
1387 
1388 	ret = pm_runtime_get_sync(priv->dev);
1389 	if (ret < 0) {
1390 		netdev_err(ndev, "%s: pm_runtime_get failed(%d)\n",
1391 			   __func__, ret);
1392 		return ret;
1393 	}
1394 
1395 	ret = request_irq(ndev->irq, xcan_interrupt, priv->irq_flags,
1396 			  ndev->name, ndev);
1397 	if (ret < 0) {
1398 		netdev_err(ndev, "irq allocation for CAN failed\n");
1399 		goto err;
1400 	}
1401 
1402 	/* Set chip into reset mode */
1403 	ret = set_reset_mode(ndev);
1404 	if (ret < 0) {
1405 		netdev_err(ndev, "mode resetting failed!\n");
1406 		goto err_irq;
1407 	}
1408 
1409 	/* Common open */
1410 	ret = open_candev(ndev);
1411 	if (ret)
1412 		goto err_irq;
1413 
1414 	ret = xcan_chip_start(ndev);
1415 	if (ret < 0) {
1416 		netdev_err(ndev, "xcan_chip_start failed!\n");
1417 		goto err_candev;
1418 	}
1419 
1420 	can_led_event(ndev, CAN_LED_EVENT_OPEN);
1421 	napi_enable(&priv->napi);
1422 	netif_start_queue(ndev);
1423 
1424 	return 0;
1425 
1426 err_candev:
1427 	close_candev(ndev);
1428 err_irq:
1429 	free_irq(ndev->irq, ndev);
1430 err:
1431 	pm_runtime_put(priv->dev);
1432 
1433 	return ret;
1434 }
1435 
1436 /**
1437  * xcan_close - Driver close routine
1438  * @ndev:	Pointer to net_device structure
1439  *
1440  * Return: 0 always
1441  */
1442 static int xcan_close(struct net_device *ndev)
1443 {
1444 	struct xcan_priv *priv = netdev_priv(ndev);
1445 
1446 	netif_stop_queue(ndev);
1447 	napi_disable(&priv->napi);
1448 	xcan_chip_stop(ndev);
1449 	free_irq(ndev->irq, ndev);
1450 	close_candev(ndev);
1451 
1452 	can_led_event(ndev, CAN_LED_EVENT_STOP);
1453 	pm_runtime_put(priv->dev);
1454 
1455 	return 0;
1456 }
1457 
1458 /**
1459  * xcan_get_berr_counter - error counter routine
1460  * @ndev:	Pointer to net_device structure
1461  * @bec:	Pointer to can_berr_counter structure
1462  *
1463  * This is the driver error counter routine.
1464  * Return: 0 on success and failure value on error
1465  */
1466 static int xcan_get_berr_counter(const struct net_device *ndev,
1467 				 struct can_berr_counter *bec)
1468 {
1469 	struct xcan_priv *priv = netdev_priv(ndev);
1470 	int ret;
1471 
1472 	ret = pm_runtime_get_sync(priv->dev);
1473 	if (ret < 0) {
1474 		netdev_err(ndev, "%s: pm_runtime_get failed(%d)\n",
1475 			   __func__, ret);
1476 		return ret;
1477 	}
1478 
1479 	bec->txerr = priv->read_reg(priv, XCAN_ECR_OFFSET) & XCAN_ECR_TEC_MASK;
1480 	bec->rxerr = ((priv->read_reg(priv, XCAN_ECR_OFFSET) &
1481 			XCAN_ECR_REC_MASK) >> XCAN_ESR_REC_SHIFT);
1482 
1483 	pm_runtime_put(priv->dev);
1484 
1485 	return 0;
1486 }
1487 
1488 static const struct net_device_ops xcan_netdev_ops = {
1489 	.ndo_open	= xcan_open,
1490 	.ndo_stop	= xcan_close,
1491 	.ndo_start_xmit	= xcan_start_xmit,
1492 	.ndo_change_mtu	= can_change_mtu,
1493 };
1494 
1495 /**
1496  * xcan_suspend - Suspend method for the driver
1497  * @dev:	Address of the device structure
1498  *
1499  * Put the driver into low power mode.
1500  * Return: 0 on success and failure value on error
1501  */
1502 static int __maybe_unused xcan_suspend(struct device *dev)
1503 {
1504 	struct net_device *ndev = dev_get_drvdata(dev);
1505 
1506 	if (netif_running(ndev)) {
1507 		netif_stop_queue(ndev);
1508 		netif_device_detach(ndev);
1509 		xcan_chip_stop(ndev);
1510 	}
1511 
1512 	return pm_runtime_force_suspend(dev);
1513 }
1514 
1515 /**
1516  * xcan_resume - Resume from suspend
1517  * @dev:	Address of the device structure
1518  *
1519  * Resume operation after suspend.
1520  * Return: 0 on success and failure value on error
1521  */
1522 static int __maybe_unused xcan_resume(struct device *dev)
1523 {
1524 	struct net_device *ndev = dev_get_drvdata(dev);
1525 	int ret;
1526 
1527 	ret = pm_runtime_force_resume(dev);
1528 	if (ret) {
1529 		dev_err(dev, "pm_runtime_force_resume failed on resume\n");
1530 		return ret;
1531 	}
1532 
1533 	if (netif_running(ndev)) {
1534 		ret = xcan_chip_start(ndev);
1535 		if (ret) {
1536 			dev_err(dev, "xcan_chip_start failed on resume\n");
1537 			return ret;
1538 		}
1539 
1540 		netif_device_attach(ndev);
1541 		netif_start_queue(ndev);
1542 	}
1543 
1544 	return 0;
1545 }
1546 
1547 /**
1548  * xcan_runtime_suspend - Runtime suspend method for the driver
1549  * @dev:	Address of the device structure
1550  *
1551  * Put the driver into low power mode.
1552  * Return: 0 always
1553  */
1554 static int __maybe_unused xcan_runtime_suspend(struct device *dev)
1555 {
1556 	struct net_device *ndev = dev_get_drvdata(dev);
1557 	struct xcan_priv *priv = netdev_priv(ndev);
1558 
1559 	clk_disable_unprepare(priv->bus_clk);
1560 	clk_disable_unprepare(priv->can_clk);
1561 
1562 	return 0;
1563 }
1564 
1565 /**
1566  * xcan_runtime_resume - Runtime resume from suspend
1567  * @dev:	Address of the device structure
1568  *
1569  * Resume operation after suspend.
1570  * Return: 0 on success and failure value on error
1571  */
1572 static int __maybe_unused xcan_runtime_resume(struct device *dev)
1573 {
1574 	struct net_device *ndev = dev_get_drvdata(dev);
1575 	struct xcan_priv *priv = netdev_priv(ndev);
1576 	int ret;
1577 
1578 	ret = clk_prepare_enable(priv->bus_clk);
1579 	if (ret) {
1580 		dev_err(dev, "Cannot enable clock.\n");
1581 		return ret;
1582 	}
1583 	ret = clk_prepare_enable(priv->can_clk);
1584 	if (ret) {
1585 		dev_err(dev, "Cannot enable clock.\n");
1586 		clk_disable_unprepare(priv->bus_clk);
1587 		return ret;
1588 	}
1589 
1590 	return 0;
1591 }
1592 
1593 static const struct dev_pm_ops xcan_dev_pm_ops = {
1594 	SET_SYSTEM_SLEEP_PM_OPS(xcan_suspend, xcan_resume)
1595 	SET_RUNTIME_PM_OPS(xcan_runtime_suspend, xcan_runtime_resume, NULL)
1596 };
1597 
1598 static const struct xcan_devtype_data xcan_zynq_data = {
1599 	.cantype = XZYNQ_CANPS,
1600 	.flags = XCAN_FLAG_TXFEMP,
1601 	.bittiming_const = &xcan_bittiming_const,
1602 	.btr_ts2_shift = XCAN_BTR_TS2_SHIFT,
1603 	.btr_sjw_shift = XCAN_BTR_SJW_SHIFT,
1604 	.bus_clk_name = "pclk",
1605 };
1606 
1607 static const struct xcan_devtype_data xcan_axi_data = {
1608 	.cantype = XAXI_CAN,
1609 	.bittiming_const = &xcan_bittiming_const,
1610 	.btr_ts2_shift = XCAN_BTR_TS2_SHIFT,
1611 	.btr_sjw_shift = XCAN_BTR_SJW_SHIFT,
1612 	.bus_clk_name = "s_axi_aclk",
1613 };
1614 
1615 static const struct xcan_devtype_data xcan_canfd_data = {
1616 	.cantype = XAXI_CANFD,
1617 	.flags = XCAN_FLAG_EXT_FILTERS |
1618 		 XCAN_FLAG_RXMNF |
1619 		 XCAN_FLAG_TX_MAILBOXES |
1620 		 XCAN_FLAG_RX_FIFO_MULTI,
1621 	.bittiming_const = &xcan_bittiming_const_canfd,
1622 	.btr_ts2_shift = XCAN_BTR_TS2_SHIFT_CANFD,
1623 	.btr_sjw_shift = XCAN_BTR_SJW_SHIFT_CANFD,
1624 	.bus_clk_name = "s_axi_aclk",
1625 };
1626 
1627 static const struct xcan_devtype_data xcan_canfd2_data = {
1628 	.cantype = XAXI_CANFD_2_0,
1629 	.flags = XCAN_FLAG_EXT_FILTERS |
1630 		 XCAN_FLAG_RXMNF |
1631 		 XCAN_FLAG_TX_MAILBOXES |
1632 		 XCAN_FLAG_CANFD_2 |
1633 		 XCAN_FLAG_RX_FIFO_MULTI,
1634 	.bittiming_const = &xcan_bittiming_const_canfd2,
1635 	.btr_ts2_shift = XCAN_BTR_TS2_SHIFT_CANFD,
1636 	.btr_sjw_shift = XCAN_BTR_SJW_SHIFT_CANFD,
1637 	.bus_clk_name = "s_axi_aclk",
1638 };
1639 
1640 /* Match table for OF platform binding */
1641 static const struct of_device_id xcan_of_match[] = {
1642 	{ .compatible = "xlnx,zynq-can-1.0", .data = &xcan_zynq_data },
1643 	{ .compatible = "xlnx,axi-can-1.00.a", .data = &xcan_axi_data },
1644 	{ .compatible = "xlnx,canfd-1.0", .data = &xcan_canfd_data },
1645 	{ .compatible = "xlnx,canfd-2.0", .data = &xcan_canfd2_data },
1646 	{ /* end of list */ },
1647 };
1648 MODULE_DEVICE_TABLE(of, xcan_of_match);
1649 
1650 /**
1651  * xcan_probe - Platform registration call
1652  * @pdev:	Handle to the platform device structure
1653  *
1654  * This function does all the memory allocation and registration for the CAN
1655  * device.
1656  *
1657  * Return: 0 on success and failure value on error
1658  */
1659 static int xcan_probe(struct platform_device *pdev)
1660 {
1661 	struct net_device *ndev;
1662 	struct xcan_priv *priv;
1663 	const struct of_device_id *of_id;
1664 	const struct xcan_devtype_data *devtype = &xcan_axi_data;
1665 	void __iomem *addr;
1666 	int ret;
1667 	int rx_max, tx_max;
1668 	int hw_tx_max, hw_rx_max;
1669 	const char *hw_tx_max_property;
1670 
1671 	/* Get the virtual base address for the device */
1672 	addr = devm_platform_ioremap_resource(pdev, 0);
1673 	if (IS_ERR(addr)) {
1674 		ret = PTR_ERR(addr);
1675 		goto err;
1676 	}
1677 
1678 	of_id = of_match_device(xcan_of_match, &pdev->dev);
1679 	if (of_id && of_id->data)
1680 		devtype = of_id->data;
1681 
1682 	hw_tx_max_property = devtype->flags & XCAN_FLAG_TX_MAILBOXES ?
1683 			     "tx-mailbox-count" : "tx-fifo-depth";
1684 
1685 	ret = of_property_read_u32(pdev->dev.of_node, hw_tx_max_property,
1686 				   &hw_tx_max);
1687 	if (ret < 0) {
1688 		dev_err(&pdev->dev, "missing %s property\n",
1689 			hw_tx_max_property);
1690 		goto err;
1691 	}
1692 
1693 	ret = of_property_read_u32(pdev->dev.of_node, "rx-fifo-depth",
1694 				   &hw_rx_max);
1695 	if (ret < 0) {
1696 		dev_err(&pdev->dev,
1697 			"missing rx-fifo-depth property (mailbox mode is not supported)\n");
1698 		goto err;
1699 	}
1700 
1701 	/* With TX FIFO:
1702 	 *
1703 	 * There is no way to directly figure out how many frames have been
1704 	 * sent when the TXOK interrupt is processed. If TXFEMP
1705 	 * is supported, we can have 2 frames in the FIFO and use TXFEMP
1706 	 * to determine if 1 or 2 frames have been sent.
1707 	 * Theoretically we should be able to use TXFWMEMP to determine up
1708 	 * to 3 frames, but it seems that after putting a second frame in the
1709 	 * FIFO, with watermark at 2 frames, it can happen that TXFWMEMP (less
1710 	 * than 2 frames in FIFO) is set anyway with no TXOK (a frame was
1711 	 * sent), which is not a sensible state - possibly TXFWMEMP is not
1712 	 * completely synchronized with the rest of the bits?
1713 	 *
1714 	 * With TX mailboxes:
1715 	 *
1716 	 * HW sends frames in CAN ID priority order. To preserve FIFO ordering
1717 	 * we submit frames one at a time.
1718 	 */
1719 	if (!(devtype->flags & XCAN_FLAG_TX_MAILBOXES) &&
1720 	    (devtype->flags & XCAN_FLAG_TXFEMP))
1721 		tx_max = min(hw_tx_max, 2);
1722 	else
1723 		tx_max = 1;
1724 
1725 	rx_max = hw_rx_max;
1726 
1727 	/* Create a CAN device instance */
1728 	ndev = alloc_candev(sizeof(struct xcan_priv), tx_max);
1729 	if (!ndev)
1730 		return -ENOMEM;
1731 
1732 	priv = netdev_priv(ndev);
1733 	priv->dev = &pdev->dev;
1734 	priv->can.bittiming_const = devtype->bittiming_const;
1735 	priv->can.do_set_mode = xcan_do_set_mode;
1736 	priv->can.do_get_berr_counter = xcan_get_berr_counter;
1737 	priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK |
1738 					CAN_CTRLMODE_BERR_REPORTING;
1739 
1740 	if (devtype->cantype == XAXI_CANFD)
1741 		priv->can.data_bittiming_const =
1742 			&xcan_data_bittiming_const_canfd;
1743 
1744 	if (devtype->cantype == XAXI_CANFD_2_0)
1745 		priv->can.data_bittiming_const =
1746 			&xcan_data_bittiming_const_canfd2;
1747 
1748 	if (devtype->cantype == XAXI_CANFD ||
1749 	    devtype->cantype == XAXI_CANFD_2_0)
1750 		priv->can.ctrlmode_supported |= CAN_CTRLMODE_FD;
1751 
1752 	priv->reg_base = addr;
1753 	priv->tx_max = tx_max;
1754 	priv->devtype = *devtype;
1755 	spin_lock_init(&priv->tx_lock);
1756 
1757 	/* Get IRQ for the device */
1758 	ndev->irq = platform_get_irq(pdev, 0);
1759 	ndev->flags |= IFF_ECHO;	/* We support local echo */
1760 
1761 	platform_set_drvdata(pdev, ndev);
1762 	SET_NETDEV_DEV(ndev, &pdev->dev);
1763 	ndev->netdev_ops = &xcan_netdev_ops;
1764 
1765 	/* Getting the CAN can_clk info */
1766 	priv->can_clk = devm_clk_get(&pdev->dev, "can_clk");
1767 	if (IS_ERR(priv->can_clk)) {
1768 		if (PTR_ERR(priv->can_clk) != -EPROBE_DEFER)
1769 			dev_err(&pdev->dev, "Device clock not found.\n");
1770 		ret = PTR_ERR(priv->can_clk);
1771 		goto err_free;
1772 	}
1773 
1774 	priv->bus_clk = devm_clk_get(&pdev->dev, devtype->bus_clk_name);
1775 	if (IS_ERR(priv->bus_clk)) {
1776 		if (PTR_ERR(priv->bus_clk) != -EPROBE_DEFER)
1777 			dev_err(&pdev->dev, "bus clock not found\n");
1778 		ret = PTR_ERR(priv->bus_clk);
1779 		goto err_free;
1780 	}
1781 
1782 	priv->write_reg = xcan_write_reg_le;
1783 	priv->read_reg = xcan_read_reg_le;
1784 
1785 	pm_runtime_enable(&pdev->dev);
1786 	ret = pm_runtime_get_sync(&pdev->dev);
1787 	if (ret < 0) {
1788 		netdev_err(ndev, "%s: pm_runtime_get failed(%d)\n",
1789 			   __func__, ret);
1790 		goto err_pmdisable;
1791 	}
1792 
1793 	if (priv->read_reg(priv, XCAN_SR_OFFSET) != XCAN_SR_CONFIG_MASK) {
1794 		priv->write_reg = xcan_write_reg_be;
1795 		priv->read_reg = xcan_read_reg_be;
1796 	}
1797 
1798 	priv->can.clock.freq = clk_get_rate(priv->can_clk);
1799 
1800 	netif_napi_add(ndev, &priv->napi, xcan_rx_poll, rx_max);
1801 
1802 	ret = register_candev(ndev);
1803 	if (ret) {
1804 		dev_err(&pdev->dev, "fail to register failed (err=%d)\n", ret);
1805 		goto err_disableclks;
1806 	}
1807 
1808 	devm_can_led_init(ndev);
1809 
1810 	pm_runtime_put(&pdev->dev);
1811 
1812 	netdev_dbg(ndev, "reg_base=0x%p irq=%d clock=%d, tx buffers: actual %d, using %d\n",
1813 		   priv->reg_base, ndev->irq, priv->can.clock.freq,
1814 		   hw_tx_max, priv->tx_max);
1815 
1816 	return 0;
1817 
1818 err_disableclks:
1819 	pm_runtime_put(priv->dev);
1820 err_pmdisable:
1821 	pm_runtime_disable(&pdev->dev);
1822 err_free:
1823 	free_candev(ndev);
1824 err:
1825 	return ret;
1826 }
1827 
1828 /**
1829  * xcan_remove - Unregister the device after releasing the resources
1830  * @pdev:	Handle to the platform device structure
1831  *
1832  * This function frees all the resources allocated to the device.
1833  * Return: 0 always
1834  */
1835 static int xcan_remove(struct platform_device *pdev)
1836 {
1837 	struct net_device *ndev = platform_get_drvdata(pdev);
1838 	struct xcan_priv *priv = netdev_priv(ndev);
1839 
1840 	unregister_candev(ndev);
1841 	pm_runtime_disable(&pdev->dev);
1842 	netif_napi_del(&priv->napi);
1843 	free_candev(ndev);
1844 
1845 	return 0;
1846 }
1847 
1848 static struct platform_driver xcan_driver = {
1849 	.probe = xcan_probe,
1850 	.remove	= xcan_remove,
1851 	.driver	= {
1852 		.name = DRIVER_NAME,
1853 		.pm = &xcan_dev_pm_ops,
1854 		.of_match_table	= xcan_of_match,
1855 	},
1856 };
1857 
1858 module_platform_driver(xcan_driver);
1859 
1860 MODULE_LICENSE("GPL");
1861 MODULE_AUTHOR("Xilinx Inc");
1862 MODULE_DESCRIPTION("Xilinx CAN interface");
1863