xref: /openbmc/linux/drivers/net/can/m_can/m_can.c (revision 2fa5ebe3)
1 // SPDX-License-Identifier: GPL-2.0
2 // CAN bus driver for Bosch M_CAN controller
3 // Copyright (C) 2014 Freescale Semiconductor, Inc.
4 //      Dong Aisheng <b29396@freescale.com>
5 // Copyright (C) 2018-19 Texas Instruments Incorporated - http://www.ti.com/
6 
7 /* Bosch M_CAN user manual can be obtained from:
8  * https://github.com/linux-can/can-doc/tree/master/m_can
9  */
10 
11 #include <linux/bitfield.h>
12 #include <linux/can/dev.h>
13 #include <linux/ethtool.h>
14 #include <linux/interrupt.h>
15 #include <linux/io.h>
16 #include <linux/iopoll.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/phy/phy.h>
23 #include <linux/pinctrl/consumer.h>
24 #include <linux/platform_device.h>
25 #include <linux/pm_runtime.h>
26 
27 #include "m_can.h"
28 
29 /* registers definition */
30 enum m_can_reg {
31 	M_CAN_CREL	= 0x0,
32 	M_CAN_ENDN	= 0x4,
33 	M_CAN_CUST	= 0x8,
34 	M_CAN_DBTP	= 0xc,
35 	M_CAN_TEST	= 0x10,
36 	M_CAN_RWD	= 0x14,
37 	M_CAN_CCCR	= 0x18,
38 	M_CAN_NBTP	= 0x1c,
39 	M_CAN_TSCC	= 0x20,
40 	M_CAN_TSCV	= 0x24,
41 	M_CAN_TOCC	= 0x28,
42 	M_CAN_TOCV	= 0x2c,
43 	M_CAN_ECR	= 0x40,
44 	M_CAN_PSR	= 0x44,
45 	/* TDCR Register only available for version >=3.1.x */
46 	M_CAN_TDCR	= 0x48,
47 	M_CAN_IR	= 0x50,
48 	M_CAN_IE	= 0x54,
49 	M_CAN_ILS	= 0x58,
50 	M_CAN_ILE	= 0x5c,
51 	M_CAN_GFC	= 0x80,
52 	M_CAN_SIDFC	= 0x84,
53 	M_CAN_XIDFC	= 0x88,
54 	M_CAN_XIDAM	= 0x90,
55 	M_CAN_HPMS	= 0x94,
56 	M_CAN_NDAT1	= 0x98,
57 	M_CAN_NDAT2	= 0x9c,
58 	M_CAN_RXF0C	= 0xa0,
59 	M_CAN_RXF0S	= 0xa4,
60 	M_CAN_RXF0A	= 0xa8,
61 	M_CAN_RXBC	= 0xac,
62 	M_CAN_RXF1C	= 0xb0,
63 	M_CAN_RXF1S	= 0xb4,
64 	M_CAN_RXF1A	= 0xb8,
65 	M_CAN_RXESC	= 0xbc,
66 	M_CAN_TXBC	= 0xc0,
67 	M_CAN_TXFQS	= 0xc4,
68 	M_CAN_TXESC	= 0xc8,
69 	M_CAN_TXBRP	= 0xcc,
70 	M_CAN_TXBAR	= 0xd0,
71 	M_CAN_TXBCR	= 0xd4,
72 	M_CAN_TXBTO	= 0xd8,
73 	M_CAN_TXBCF	= 0xdc,
74 	M_CAN_TXBTIE	= 0xe0,
75 	M_CAN_TXBCIE	= 0xe4,
76 	M_CAN_TXEFC	= 0xf0,
77 	M_CAN_TXEFS	= 0xf4,
78 	M_CAN_TXEFA	= 0xf8,
79 };
80 
81 /* message ram configuration data length */
82 #define MRAM_CFG_LEN	8
83 
84 /* Core Release Register (CREL) */
85 #define CREL_REL_MASK		GENMASK(31, 28)
86 #define CREL_STEP_MASK		GENMASK(27, 24)
87 #define CREL_SUBSTEP_MASK	GENMASK(23, 20)
88 
89 /* Data Bit Timing & Prescaler Register (DBTP) */
90 #define DBTP_TDC		BIT(23)
91 #define DBTP_DBRP_MASK		GENMASK(20, 16)
92 #define DBTP_DTSEG1_MASK	GENMASK(12, 8)
93 #define DBTP_DTSEG2_MASK	GENMASK(7, 4)
94 #define DBTP_DSJW_MASK		GENMASK(3, 0)
95 
96 /* Transmitter Delay Compensation Register (TDCR) */
97 #define TDCR_TDCO_MASK		GENMASK(14, 8)
98 #define TDCR_TDCF_MASK		GENMASK(6, 0)
99 
100 /* Test Register (TEST) */
101 #define TEST_LBCK		BIT(4)
102 
103 /* CC Control Register (CCCR) */
104 #define CCCR_TXP		BIT(14)
105 #define CCCR_TEST		BIT(7)
106 #define CCCR_DAR		BIT(6)
107 #define CCCR_MON		BIT(5)
108 #define CCCR_CSR		BIT(4)
109 #define CCCR_CSA		BIT(3)
110 #define CCCR_ASM		BIT(2)
111 #define CCCR_CCE		BIT(1)
112 #define CCCR_INIT		BIT(0)
113 /* for version 3.0.x */
114 #define CCCR_CMR_MASK		GENMASK(11, 10)
115 #define CCCR_CMR_CANFD		0x1
116 #define CCCR_CMR_CANFD_BRS	0x2
117 #define CCCR_CMR_CAN		0x3
118 #define CCCR_CME_MASK		GENMASK(9, 8)
119 #define CCCR_CME_CAN		0
120 #define CCCR_CME_CANFD		0x1
121 #define CCCR_CME_CANFD_BRS	0x2
122 /* for version >=3.1.x */
123 #define CCCR_EFBI		BIT(13)
124 #define CCCR_PXHD		BIT(12)
125 #define CCCR_BRSE		BIT(9)
126 #define CCCR_FDOE		BIT(8)
127 /* for version >=3.2.x */
128 #define CCCR_NISO		BIT(15)
129 /* for version >=3.3.x */
130 #define CCCR_WMM		BIT(11)
131 #define CCCR_UTSU		BIT(10)
132 
133 /* Nominal Bit Timing & Prescaler Register (NBTP) */
134 #define NBTP_NSJW_MASK		GENMASK(31, 25)
135 #define NBTP_NBRP_MASK		GENMASK(24, 16)
136 #define NBTP_NTSEG1_MASK	GENMASK(15, 8)
137 #define NBTP_NTSEG2_MASK	GENMASK(6, 0)
138 
139 /* Timestamp Counter Configuration Register (TSCC) */
140 #define TSCC_TCP_MASK		GENMASK(19, 16)
141 #define TSCC_TSS_MASK		GENMASK(1, 0)
142 #define TSCC_TSS_DISABLE	0x0
143 #define TSCC_TSS_INTERNAL	0x1
144 #define TSCC_TSS_EXTERNAL	0x2
145 
146 /* Timestamp Counter Value Register (TSCV) */
147 #define TSCV_TSC_MASK		GENMASK(15, 0)
148 
149 /* Error Counter Register (ECR) */
150 #define ECR_RP			BIT(15)
151 #define ECR_REC_MASK		GENMASK(14, 8)
152 #define ECR_TEC_MASK		GENMASK(7, 0)
153 
154 /* Protocol Status Register (PSR) */
155 #define PSR_BO		BIT(7)
156 #define PSR_EW		BIT(6)
157 #define PSR_EP		BIT(5)
158 #define PSR_LEC_MASK	GENMASK(2, 0)
159 #define PSR_DLEC_MASK	GENMASK(10, 8)
160 
161 /* Interrupt Register (IR) */
162 #define IR_ALL_INT	0xffffffff
163 
164 /* Renamed bits for versions > 3.1.x */
165 #define IR_ARA		BIT(29)
166 #define IR_PED		BIT(28)
167 #define IR_PEA		BIT(27)
168 
169 /* Bits for version 3.0.x */
170 #define IR_STE		BIT(31)
171 #define IR_FOE		BIT(30)
172 #define IR_ACKE		BIT(29)
173 #define IR_BE		BIT(28)
174 #define IR_CRCE		BIT(27)
175 #define IR_WDI		BIT(26)
176 #define IR_BO		BIT(25)
177 #define IR_EW		BIT(24)
178 #define IR_EP		BIT(23)
179 #define IR_ELO		BIT(22)
180 #define IR_BEU		BIT(21)
181 #define IR_BEC		BIT(20)
182 #define IR_DRX		BIT(19)
183 #define IR_TOO		BIT(18)
184 #define IR_MRAF		BIT(17)
185 #define IR_TSW		BIT(16)
186 #define IR_TEFL		BIT(15)
187 #define IR_TEFF		BIT(14)
188 #define IR_TEFW		BIT(13)
189 #define IR_TEFN		BIT(12)
190 #define IR_TFE		BIT(11)
191 #define IR_TCF		BIT(10)
192 #define IR_TC		BIT(9)
193 #define IR_HPM		BIT(8)
194 #define IR_RF1L		BIT(7)
195 #define IR_RF1F		BIT(6)
196 #define IR_RF1W		BIT(5)
197 #define IR_RF1N		BIT(4)
198 #define IR_RF0L		BIT(3)
199 #define IR_RF0F		BIT(2)
200 #define IR_RF0W		BIT(1)
201 #define IR_RF0N		BIT(0)
202 #define IR_ERR_STATE	(IR_BO | IR_EW | IR_EP)
203 
204 /* Interrupts for version 3.0.x */
205 #define IR_ERR_LEC_30X	(IR_STE	| IR_FOE | IR_ACKE | IR_BE | IR_CRCE)
206 #define IR_ERR_BUS_30X	(IR_ERR_LEC_30X | IR_WDI | IR_BEU | IR_BEC | \
207 			 IR_TOO | IR_MRAF | IR_TSW | IR_TEFL | IR_RF1L | \
208 			 IR_RF0L)
209 #define IR_ERR_ALL_30X	(IR_ERR_STATE | IR_ERR_BUS_30X)
210 
211 /* Interrupts for version >= 3.1.x */
212 #define IR_ERR_LEC_31X	(IR_PED | IR_PEA)
213 #define IR_ERR_BUS_31X	(IR_ERR_LEC_31X | IR_WDI | IR_BEU | IR_BEC | \
214 			 IR_TOO | IR_MRAF | IR_TSW | IR_TEFL | IR_RF1L | \
215 			 IR_RF0L)
216 #define IR_ERR_ALL_31X	(IR_ERR_STATE | IR_ERR_BUS_31X)
217 
218 /* Interrupt Line Select (ILS) */
219 #define ILS_ALL_INT0	0x0
220 #define ILS_ALL_INT1	0xFFFFFFFF
221 
222 /* Interrupt Line Enable (ILE) */
223 #define ILE_EINT1	BIT(1)
224 #define ILE_EINT0	BIT(0)
225 
226 /* Rx FIFO 0/1 Configuration (RXF0C/RXF1C) */
227 #define RXFC_FWM_MASK	GENMASK(30, 24)
228 #define RXFC_FS_MASK	GENMASK(22, 16)
229 
230 /* Rx FIFO 0/1 Status (RXF0S/RXF1S) */
231 #define RXFS_RFL	BIT(25)
232 #define RXFS_FF		BIT(24)
233 #define RXFS_FPI_MASK	GENMASK(21, 16)
234 #define RXFS_FGI_MASK	GENMASK(13, 8)
235 #define RXFS_FFL_MASK	GENMASK(6, 0)
236 
237 /* Rx Buffer / FIFO Element Size Configuration (RXESC) */
238 #define RXESC_RBDS_MASK		GENMASK(10, 8)
239 #define RXESC_F1DS_MASK		GENMASK(6, 4)
240 #define RXESC_F0DS_MASK		GENMASK(2, 0)
241 #define RXESC_64B		0x7
242 
243 /* Tx Buffer Configuration (TXBC) */
244 #define TXBC_TFQS_MASK		GENMASK(29, 24)
245 #define TXBC_NDTB_MASK		GENMASK(21, 16)
246 
247 /* Tx FIFO/Queue Status (TXFQS) */
248 #define TXFQS_TFQF		BIT(21)
249 #define TXFQS_TFQPI_MASK	GENMASK(20, 16)
250 #define TXFQS_TFGI_MASK		GENMASK(12, 8)
251 #define TXFQS_TFFL_MASK		GENMASK(5, 0)
252 
253 /* Tx Buffer Element Size Configuration (TXESC) */
254 #define TXESC_TBDS_MASK		GENMASK(2, 0)
255 #define TXESC_TBDS_64B		0x7
256 
257 /* Tx Event FIFO Configuration (TXEFC) */
258 #define TXEFC_EFS_MASK		GENMASK(21, 16)
259 
260 /* Tx Event FIFO Status (TXEFS) */
261 #define TXEFS_TEFL		BIT(25)
262 #define TXEFS_EFF		BIT(24)
263 #define TXEFS_EFGI_MASK		GENMASK(12, 8)
264 #define TXEFS_EFFL_MASK		GENMASK(5, 0)
265 
266 /* Tx Event FIFO Acknowledge (TXEFA) */
267 #define TXEFA_EFAI_MASK		GENMASK(4, 0)
268 
269 /* Message RAM Configuration (in bytes) */
270 #define SIDF_ELEMENT_SIZE	4
271 #define XIDF_ELEMENT_SIZE	8
272 #define RXF0_ELEMENT_SIZE	72
273 #define RXF1_ELEMENT_SIZE	72
274 #define RXB_ELEMENT_SIZE	72
275 #define TXE_ELEMENT_SIZE	8
276 #define TXB_ELEMENT_SIZE	72
277 
278 /* Message RAM Elements */
279 #define M_CAN_FIFO_ID		0x0
280 #define M_CAN_FIFO_DLC		0x4
281 #define M_CAN_FIFO_DATA		0x8
282 
283 /* Rx Buffer Element */
284 /* R0 */
285 #define RX_BUF_ESI		BIT(31)
286 #define RX_BUF_XTD		BIT(30)
287 #define RX_BUF_RTR		BIT(29)
288 /* R1 */
289 #define RX_BUF_ANMF		BIT(31)
290 #define RX_BUF_FDF		BIT(21)
291 #define RX_BUF_BRS		BIT(20)
292 #define RX_BUF_RXTS_MASK	GENMASK(15, 0)
293 
294 /* Tx Buffer Element */
295 /* T0 */
296 #define TX_BUF_ESI		BIT(31)
297 #define TX_BUF_XTD		BIT(30)
298 #define TX_BUF_RTR		BIT(29)
299 /* T1 */
300 #define TX_BUF_EFC		BIT(23)
301 #define TX_BUF_FDF		BIT(21)
302 #define TX_BUF_BRS		BIT(20)
303 #define TX_BUF_MM_MASK		GENMASK(31, 24)
304 #define TX_BUF_DLC_MASK		GENMASK(19, 16)
305 
306 /* Tx event FIFO Element */
307 /* E1 */
308 #define TX_EVENT_MM_MASK	GENMASK(31, 24)
309 #define TX_EVENT_TXTS_MASK	GENMASK(15, 0)
310 
311 /* The ID and DLC registers are adjacent in M_CAN FIFO memory,
312  * and we can save a (potentially slow) bus round trip by combining
313  * reads and writes to them.
314  */
315 struct id_and_dlc {
316 	u32 id;
317 	u32 dlc;
318 };
319 
320 static inline u32 m_can_read(struct m_can_classdev *cdev, enum m_can_reg reg)
321 {
322 	return cdev->ops->read_reg(cdev, reg);
323 }
324 
325 static inline void m_can_write(struct m_can_classdev *cdev, enum m_can_reg reg,
326 			       u32 val)
327 {
328 	cdev->ops->write_reg(cdev, reg, val);
329 }
330 
331 static int
332 m_can_fifo_read(struct m_can_classdev *cdev,
333 		u32 fgi, unsigned int offset, void *val, size_t val_count)
334 {
335 	u32 addr_offset = cdev->mcfg[MRAM_RXF0].off + fgi * RXF0_ELEMENT_SIZE +
336 		offset;
337 
338 	if (val_count == 0)
339 		return 0;
340 
341 	return cdev->ops->read_fifo(cdev, addr_offset, val, val_count);
342 }
343 
344 static int
345 m_can_fifo_write(struct m_can_classdev *cdev,
346 		 u32 fpi, unsigned int offset, const void *val, size_t val_count)
347 {
348 	u32 addr_offset = cdev->mcfg[MRAM_TXB].off + fpi * TXB_ELEMENT_SIZE +
349 		offset;
350 
351 	if (val_count == 0)
352 		return 0;
353 
354 	return cdev->ops->write_fifo(cdev, addr_offset, val, val_count);
355 }
356 
357 static inline int m_can_fifo_write_no_off(struct m_can_classdev *cdev,
358 					  u32 fpi, u32 val)
359 {
360 	return cdev->ops->write_fifo(cdev, fpi, &val, 1);
361 }
362 
363 static int
364 m_can_txe_fifo_read(struct m_can_classdev *cdev, u32 fgi, u32 offset, u32 *val)
365 {
366 	u32 addr_offset = cdev->mcfg[MRAM_TXE].off + fgi * TXE_ELEMENT_SIZE +
367 		offset;
368 
369 	return cdev->ops->read_fifo(cdev, addr_offset, val, 1);
370 }
371 
372 static inline bool _m_can_tx_fifo_full(u32 txfqs)
373 {
374 	return !!(txfqs & TXFQS_TFQF);
375 }
376 
377 static inline bool m_can_tx_fifo_full(struct m_can_classdev *cdev)
378 {
379 	return _m_can_tx_fifo_full(m_can_read(cdev, M_CAN_TXFQS));
380 }
381 
382 static void m_can_config_endisable(struct m_can_classdev *cdev, bool enable)
383 {
384 	u32 cccr = m_can_read(cdev, M_CAN_CCCR);
385 	u32 timeout = 10;
386 	u32 val = 0;
387 
388 	/* Clear the Clock stop request if it was set */
389 	if (cccr & CCCR_CSR)
390 		cccr &= ~CCCR_CSR;
391 
392 	if (enable) {
393 		/* enable m_can configuration */
394 		m_can_write(cdev, M_CAN_CCCR, cccr | CCCR_INIT);
395 		udelay(5);
396 		/* CCCR.CCE can only be set/reset while CCCR.INIT = '1' */
397 		m_can_write(cdev, M_CAN_CCCR, cccr | CCCR_INIT | CCCR_CCE);
398 	} else {
399 		m_can_write(cdev, M_CAN_CCCR, cccr & ~(CCCR_INIT | CCCR_CCE));
400 	}
401 
402 	/* there's a delay for module initialization */
403 	if (enable)
404 		val = CCCR_INIT | CCCR_CCE;
405 
406 	while ((m_can_read(cdev, M_CAN_CCCR) & (CCCR_INIT | CCCR_CCE)) != val) {
407 		if (timeout == 0) {
408 			netdev_warn(cdev->net, "Failed to init module\n");
409 			return;
410 		}
411 		timeout--;
412 		udelay(1);
413 	}
414 }
415 
416 static inline void m_can_enable_all_interrupts(struct m_can_classdev *cdev)
417 {
418 	/* Only interrupt line 0 is used in this driver */
419 	m_can_write(cdev, M_CAN_ILE, ILE_EINT0);
420 }
421 
422 static inline void m_can_disable_all_interrupts(struct m_can_classdev *cdev)
423 {
424 	m_can_write(cdev, M_CAN_ILE, 0x0);
425 }
426 
427 /* Retrieve internal timestamp counter from TSCV.TSC, and shift it to 32-bit
428  * width.
429  */
430 static u32 m_can_get_timestamp(struct m_can_classdev *cdev)
431 {
432 	u32 tscv;
433 	u32 tsc;
434 
435 	tscv = m_can_read(cdev, M_CAN_TSCV);
436 	tsc = FIELD_GET(TSCV_TSC_MASK, tscv);
437 
438 	return (tsc << 16);
439 }
440 
441 static void m_can_clean(struct net_device *net)
442 {
443 	struct m_can_classdev *cdev = netdev_priv(net);
444 
445 	if (cdev->tx_skb) {
446 		int putidx = 0;
447 
448 		net->stats.tx_errors++;
449 		if (cdev->version > 30)
450 			putidx = FIELD_GET(TXFQS_TFQPI_MASK,
451 					   m_can_read(cdev, M_CAN_TXFQS));
452 
453 		can_free_echo_skb(cdev->net, putidx, NULL);
454 		cdev->tx_skb = NULL;
455 	}
456 }
457 
458 /* For peripherals, pass skb to rx-offload, which will push skb from
459  * napi. For non-peripherals, RX is done in napi already, so push
460  * directly. timestamp is used to ensure good skb ordering in
461  * rx-offload and is ignored for non-peripherals.
462  */
463 static void m_can_receive_skb(struct m_can_classdev *cdev,
464 			      struct sk_buff *skb,
465 			      u32 timestamp)
466 {
467 	if (cdev->is_peripheral) {
468 		struct net_device_stats *stats = &cdev->net->stats;
469 		int err;
470 
471 		err = can_rx_offload_queue_timestamp(&cdev->offload, skb,
472 						  timestamp);
473 		if (err)
474 			stats->rx_fifo_errors++;
475 	} else {
476 		netif_receive_skb(skb);
477 	}
478 }
479 
480 static int m_can_read_fifo(struct net_device *dev, u32 fgi)
481 {
482 	struct net_device_stats *stats = &dev->stats;
483 	struct m_can_classdev *cdev = netdev_priv(dev);
484 	struct canfd_frame *cf;
485 	struct sk_buff *skb;
486 	struct id_and_dlc fifo_header;
487 	u32 timestamp = 0;
488 	int err;
489 
490 	err = m_can_fifo_read(cdev, fgi, M_CAN_FIFO_ID, &fifo_header, 2);
491 	if (err)
492 		goto out_fail;
493 
494 	if (fifo_header.dlc & RX_BUF_FDF)
495 		skb = alloc_canfd_skb(dev, &cf);
496 	else
497 		skb = alloc_can_skb(dev, (struct can_frame **)&cf);
498 	if (!skb) {
499 		stats->rx_dropped++;
500 		return 0;
501 	}
502 
503 	if (fifo_header.dlc & RX_BUF_FDF)
504 		cf->len = can_fd_dlc2len((fifo_header.dlc >> 16) & 0x0F);
505 	else
506 		cf->len = can_cc_dlc2len((fifo_header.dlc >> 16) & 0x0F);
507 
508 	if (fifo_header.id & RX_BUF_XTD)
509 		cf->can_id = (fifo_header.id & CAN_EFF_MASK) | CAN_EFF_FLAG;
510 	else
511 		cf->can_id = (fifo_header.id >> 18) & CAN_SFF_MASK;
512 
513 	if (fifo_header.id & RX_BUF_ESI) {
514 		cf->flags |= CANFD_ESI;
515 		netdev_dbg(dev, "ESI Error\n");
516 	}
517 
518 	if (!(fifo_header.dlc & RX_BUF_FDF) && (fifo_header.id & RX_BUF_RTR)) {
519 		cf->can_id |= CAN_RTR_FLAG;
520 	} else {
521 		if (fifo_header.dlc & RX_BUF_BRS)
522 			cf->flags |= CANFD_BRS;
523 
524 		err = m_can_fifo_read(cdev, fgi, M_CAN_FIFO_DATA,
525 				      cf->data, DIV_ROUND_UP(cf->len, 4));
526 		if (err)
527 			goto out_free_skb;
528 
529 		stats->rx_bytes += cf->len;
530 	}
531 	stats->rx_packets++;
532 
533 	timestamp = FIELD_GET(RX_BUF_RXTS_MASK, fifo_header.dlc) << 16;
534 
535 	m_can_receive_skb(cdev, skb, timestamp);
536 
537 	return 0;
538 
539 out_free_skb:
540 	kfree_skb(skb);
541 out_fail:
542 	netdev_err(dev, "FIFO read returned %d\n", err);
543 	return err;
544 }
545 
546 static int m_can_do_rx_poll(struct net_device *dev, int quota)
547 {
548 	struct m_can_classdev *cdev = netdev_priv(dev);
549 	u32 pkts = 0;
550 	u32 rxfs;
551 	u32 rx_count;
552 	u32 fgi;
553 	int ack_fgi = -1;
554 	int i;
555 	int err = 0;
556 
557 	rxfs = m_can_read(cdev, M_CAN_RXF0S);
558 	if (!(rxfs & RXFS_FFL_MASK)) {
559 		netdev_dbg(dev, "no messages in fifo0\n");
560 		return 0;
561 	}
562 
563 	rx_count = FIELD_GET(RXFS_FFL_MASK, rxfs);
564 	fgi = FIELD_GET(RXFS_FGI_MASK, rxfs);
565 
566 	for (i = 0; i < rx_count && quota > 0; ++i) {
567 		err = m_can_read_fifo(dev, fgi);
568 		if (err)
569 			break;
570 
571 		quota--;
572 		pkts++;
573 		ack_fgi = fgi;
574 		fgi = (++fgi >= cdev->mcfg[MRAM_RXF0].num ? 0 : fgi);
575 	}
576 
577 	if (ack_fgi != -1)
578 		m_can_write(cdev, M_CAN_RXF0A, ack_fgi);
579 
580 	if (err)
581 		return err;
582 
583 	return pkts;
584 }
585 
586 static int m_can_handle_lost_msg(struct net_device *dev)
587 {
588 	struct m_can_classdev *cdev = netdev_priv(dev);
589 	struct net_device_stats *stats = &dev->stats;
590 	struct sk_buff *skb;
591 	struct can_frame *frame;
592 	u32 timestamp = 0;
593 
594 	netdev_err(dev, "msg lost in rxf0\n");
595 
596 	stats->rx_errors++;
597 	stats->rx_over_errors++;
598 
599 	skb = alloc_can_err_skb(dev, &frame);
600 	if (unlikely(!skb))
601 		return 0;
602 
603 	frame->can_id |= CAN_ERR_CRTL;
604 	frame->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
605 
606 	if (cdev->is_peripheral)
607 		timestamp = m_can_get_timestamp(cdev);
608 
609 	m_can_receive_skb(cdev, skb, timestamp);
610 
611 	return 1;
612 }
613 
614 static int m_can_handle_lec_err(struct net_device *dev,
615 				enum m_can_lec_type lec_type)
616 {
617 	struct m_can_classdev *cdev = netdev_priv(dev);
618 	struct net_device_stats *stats = &dev->stats;
619 	struct can_frame *cf;
620 	struct sk_buff *skb;
621 	u32 timestamp = 0;
622 
623 	cdev->can.can_stats.bus_error++;
624 	stats->rx_errors++;
625 
626 	/* propagate the error condition to the CAN stack */
627 	skb = alloc_can_err_skb(dev, &cf);
628 	if (unlikely(!skb))
629 		return 0;
630 
631 	/* check for 'last error code' which tells us the
632 	 * type of the last error to occur on the CAN bus
633 	 */
634 	cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
635 
636 	switch (lec_type) {
637 	case LEC_STUFF_ERROR:
638 		netdev_dbg(dev, "stuff error\n");
639 		cf->data[2] |= CAN_ERR_PROT_STUFF;
640 		break;
641 	case LEC_FORM_ERROR:
642 		netdev_dbg(dev, "form error\n");
643 		cf->data[2] |= CAN_ERR_PROT_FORM;
644 		break;
645 	case LEC_ACK_ERROR:
646 		netdev_dbg(dev, "ack error\n");
647 		cf->data[3] = CAN_ERR_PROT_LOC_ACK;
648 		break;
649 	case LEC_BIT1_ERROR:
650 		netdev_dbg(dev, "bit1 error\n");
651 		cf->data[2] |= CAN_ERR_PROT_BIT1;
652 		break;
653 	case LEC_BIT0_ERROR:
654 		netdev_dbg(dev, "bit0 error\n");
655 		cf->data[2] |= CAN_ERR_PROT_BIT0;
656 		break;
657 	case LEC_CRC_ERROR:
658 		netdev_dbg(dev, "CRC error\n");
659 		cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ;
660 		break;
661 	default:
662 		break;
663 	}
664 
665 	if (cdev->is_peripheral)
666 		timestamp = m_can_get_timestamp(cdev);
667 
668 	m_can_receive_skb(cdev, skb, timestamp);
669 
670 	return 1;
671 }
672 
673 static int __m_can_get_berr_counter(const struct net_device *dev,
674 				    struct can_berr_counter *bec)
675 {
676 	struct m_can_classdev *cdev = netdev_priv(dev);
677 	unsigned int ecr;
678 
679 	ecr = m_can_read(cdev, M_CAN_ECR);
680 	bec->rxerr = FIELD_GET(ECR_REC_MASK, ecr);
681 	bec->txerr = FIELD_GET(ECR_TEC_MASK, ecr);
682 
683 	return 0;
684 }
685 
686 static int m_can_clk_start(struct m_can_classdev *cdev)
687 {
688 	if (cdev->pm_clock_support == 0)
689 		return 0;
690 
691 	return pm_runtime_resume_and_get(cdev->dev);
692 }
693 
694 static void m_can_clk_stop(struct m_can_classdev *cdev)
695 {
696 	if (cdev->pm_clock_support)
697 		pm_runtime_put_sync(cdev->dev);
698 }
699 
700 static int m_can_get_berr_counter(const struct net_device *dev,
701 				  struct can_berr_counter *bec)
702 {
703 	struct m_can_classdev *cdev = netdev_priv(dev);
704 	int err;
705 
706 	err = m_can_clk_start(cdev);
707 	if (err)
708 		return err;
709 
710 	__m_can_get_berr_counter(dev, bec);
711 
712 	m_can_clk_stop(cdev);
713 
714 	return 0;
715 }
716 
717 static int m_can_handle_state_change(struct net_device *dev,
718 				     enum can_state new_state)
719 {
720 	struct m_can_classdev *cdev = netdev_priv(dev);
721 	struct can_frame *cf;
722 	struct sk_buff *skb;
723 	struct can_berr_counter bec;
724 	unsigned int ecr;
725 	u32 timestamp = 0;
726 
727 	switch (new_state) {
728 	case CAN_STATE_ERROR_WARNING:
729 		/* error warning state */
730 		cdev->can.can_stats.error_warning++;
731 		cdev->can.state = CAN_STATE_ERROR_WARNING;
732 		break;
733 	case CAN_STATE_ERROR_PASSIVE:
734 		/* error passive state */
735 		cdev->can.can_stats.error_passive++;
736 		cdev->can.state = CAN_STATE_ERROR_PASSIVE;
737 		break;
738 	case CAN_STATE_BUS_OFF:
739 		/* bus-off state */
740 		cdev->can.state = CAN_STATE_BUS_OFF;
741 		m_can_disable_all_interrupts(cdev);
742 		cdev->can.can_stats.bus_off++;
743 		can_bus_off(dev);
744 		break;
745 	default:
746 		break;
747 	}
748 
749 	/* propagate the error condition to the CAN stack */
750 	skb = alloc_can_err_skb(dev, &cf);
751 	if (unlikely(!skb))
752 		return 0;
753 
754 	__m_can_get_berr_counter(dev, &bec);
755 
756 	switch (new_state) {
757 	case CAN_STATE_ERROR_WARNING:
758 		/* error warning state */
759 		cf->can_id |= CAN_ERR_CRTL | CAN_ERR_CNT;
760 		cf->data[1] = (bec.txerr > bec.rxerr) ?
761 			CAN_ERR_CRTL_TX_WARNING :
762 			CAN_ERR_CRTL_RX_WARNING;
763 		cf->data[6] = bec.txerr;
764 		cf->data[7] = bec.rxerr;
765 		break;
766 	case CAN_STATE_ERROR_PASSIVE:
767 		/* error passive state */
768 		cf->can_id |= CAN_ERR_CRTL | CAN_ERR_CNT;
769 		ecr = m_can_read(cdev, M_CAN_ECR);
770 		if (ecr & ECR_RP)
771 			cf->data[1] |= CAN_ERR_CRTL_RX_PASSIVE;
772 		if (bec.txerr > 127)
773 			cf->data[1] |= CAN_ERR_CRTL_TX_PASSIVE;
774 		cf->data[6] = bec.txerr;
775 		cf->data[7] = bec.rxerr;
776 		break;
777 	case CAN_STATE_BUS_OFF:
778 		/* bus-off state */
779 		cf->can_id |= CAN_ERR_BUSOFF;
780 		break;
781 	default:
782 		break;
783 	}
784 
785 	if (cdev->is_peripheral)
786 		timestamp = m_can_get_timestamp(cdev);
787 
788 	m_can_receive_skb(cdev, skb, timestamp);
789 
790 	return 1;
791 }
792 
793 static int m_can_handle_state_errors(struct net_device *dev, u32 psr)
794 {
795 	struct m_can_classdev *cdev = netdev_priv(dev);
796 	int work_done = 0;
797 
798 	if (psr & PSR_EW && cdev->can.state != CAN_STATE_ERROR_WARNING) {
799 		netdev_dbg(dev, "entered error warning state\n");
800 		work_done += m_can_handle_state_change(dev,
801 						       CAN_STATE_ERROR_WARNING);
802 	}
803 
804 	if (psr & PSR_EP && cdev->can.state != CAN_STATE_ERROR_PASSIVE) {
805 		netdev_dbg(dev, "entered error passive state\n");
806 		work_done += m_can_handle_state_change(dev,
807 						       CAN_STATE_ERROR_PASSIVE);
808 	}
809 
810 	if (psr & PSR_BO && cdev->can.state != CAN_STATE_BUS_OFF) {
811 		netdev_dbg(dev, "entered error bus off state\n");
812 		work_done += m_can_handle_state_change(dev,
813 						       CAN_STATE_BUS_OFF);
814 	}
815 
816 	return work_done;
817 }
818 
819 static void m_can_handle_other_err(struct net_device *dev, u32 irqstatus)
820 {
821 	if (irqstatus & IR_WDI)
822 		netdev_err(dev, "Message RAM Watchdog event due to missing READY\n");
823 	if (irqstatus & IR_BEU)
824 		netdev_err(dev, "Bit Error Uncorrected\n");
825 	if (irqstatus & IR_BEC)
826 		netdev_err(dev, "Bit Error Corrected\n");
827 	if (irqstatus & IR_TOO)
828 		netdev_err(dev, "Timeout reached\n");
829 	if (irqstatus & IR_MRAF)
830 		netdev_err(dev, "Message RAM access failure occurred\n");
831 }
832 
833 static inline bool is_lec_err(u8 lec)
834 {
835 	return lec != LEC_NO_ERROR && lec != LEC_NO_CHANGE;
836 }
837 
838 static inline bool m_can_is_protocol_err(u32 irqstatus)
839 {
840 	return irqstatus & IR_ERR_LEC_31X;
841 }
842 
843 static int m_can_handle_protocol_error(struct net_device *dev, u32 irqstatus)
844 {
845 	struct net_device_stats *stats = &dev->stats;
846 	struct m_can_classdev *cdev = netdev_priv(dev);
847 	struct can_frame *cf;
848 	struct sk_buff *skb;
849 	u32 timestamp = 0;
850 
851 	/* propagate the error condition to the CAN stack */
852 	skb = alloc_can_err_skb(dev, &cf);
853 
854 	/* update tx error stats since there is protocol error */
855 	stats->tx_errors++;
856 
857 	/* update arbitration lost status */
858 	if (cdev->version >= 31 && (irqstatus & IR_PEA)) {
859 		netdev_dbg(dev, "Protocol error in Arbitration fail\n");
860 		cdev->can.can_stats.arbitration_lost++;
861 		if (skb) {
862 			cf->can_id |= CAN_ERR_LOSTARB;
863 			cf->data[0] |= CAN_ERR_LOSTARB_UNSPEC;
864 		}
865 	}
866 
867 	if (unlikely(!skb)) {
868 		netdev_dbg(dev, "allocation of skb failed\n");
869 		return 0;
870 	}
871 
872 	if (cdev->is_peripheral)
873 		timestamp = m_can_get_timestamp(cdev);
874 
875 	m_can_receive_skb(cdev, skb, timestamp);
876 
877 	return 1;
878 }
879 
880 static int m_can_handle_bus_errors(struct net_device *dev, u32 irqstatus,
881 				   u32 psr)
882 {
883 	struct m_can_classdev *cdev = netdev_priv(dev);
884 	int work_done = 0;
885 
886 	if (irqstatus & IR_RF0L)
887 		work_done += m_can_handle_lost_msg(dev);
888 
889 	/* handle lec errors on the bus */
890 	if (cdev->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING) {
891 		u8 lec = FIELD_GET(PSR_LEC_MASK, psr);
892 		u8 dlec = FIELD_GET(PSR_DLEC_MASK, psr);
893 
894 		if (is_lec_err(lec)) {
895 			netdev_dbg(dev, "Arbitration phase error detected\n");
896 			work_done += m_can_handle_lec_err(dev, lec);
897 		}
898 
899 		if (is_lec_err(dlec)) {
900 			netdev_dbg(dev, "Data phase error detected\n");
901 			work_done += m_can_handle_lec_err(dev, dlec);
902 		}
903 	}
904 
905 	/* handle protocol errors in arbitration phase */
906 	if ((cdev->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING) &&
907 	    m_can_is_protocol_err(irqstatus))
908 		work_done += m_can_handle_protocol_error(dev, irqstatus);
909 
910 	/* other unproccessed error interrupts */
911 	m_can_handle_other_err(dev, irqstatus);
912 
913 	return work_done;
914 }
915 
916 static int m_can_rx_handler(struct net_device *dev, int quota, u32 irqstatus)
917 {
918 	struct m_can_classdev *cdev = netdev_priv(dev);
919 	int rx_work_or_err;
920 	int work_done = 0;
921 
922 	if (!irqstatus)
923 		goto end;
924 
925 	/* Errata workaround for issue "Needless activation of MRAF irq"
926 	 * During frame reception while the MCAN is in Error Passive state
927 	 * and the Receive Error Counter has the value MCAN_ECR.REC = 127,
928 	 * it may happen that MCAN_IR.MRAF is set although there was no
929 	 * Message RAM access failure.
930 	 * If MCAN_IR.MRAF is enabled, an interrupt to the Host CPU is generated
931 	 * The Message RAM Access Failure interrupt routine needs to check
932 	 * whether MCAN_ECR.RP = ’1’ and MCAN_ECR.REC = 127.
933 	 * In this case, reset MCAN_IR.MRAF. No further action is required.
934 	 */
935 	if (cdev->version <= 31 && irqstatus & IR_MRAF &&
936 	    m_can_read(cdev, M_CAN_ECR) & ECR_RP) {
937 		struct can_berr_counter bec;
938 
939 		__m_can_get_berr_counter(dev, &bec);
940 		if (bec.rxerr == 127) {
941 			m_can_write(cdev, M_CAN_IR, IR_MRAF);
942 			irqstatus &= ~IR_MRAF;
943 		}
944 	}
945 
946 	if (irqstatus & IR_ERR_STATE)
947 		work_done += m_can_handle_state_errors(dev,
948 						       m_can_read(cdev, M_CAN_PSR));
949 
950 	if (irqstatus & IR_ERR_BUS_30X)
951 		work_done += m_can_handle_bus_errors(dev, irqstatus,
952 						     m_can_read(cdev, M_CAN_PSR));
953 
954 	if (irqstatus & IR_RF0N) {
955 		rx_work_or_err = m_can_do_rx_poll(dev, (quota - work_done));
956 		if (rx_work_or_err < 0)
957 			return rx_work_or_err;
958 
959 		work_done += rx_work_or_err;
960 	}
961 end:
962 	return work_done;
963 }
964 
965 static int m_can_rx_peripheral(struct net_device *dev, u32 irqstatus)
966 {
967 	struct m_can_classdev *cdev = netdev_priv(dev);
968 	int work_done;
969 
970 	work_done = m_can_rx_handler(dev, NAPI_POLL_WEIGHT, irqstatus);
971 
972 	/* Don't re-enable interrupts if the driver had a fatal error
973 	 * (e.g., FIFO read failure).
974 	 */
975 	if (work_done >= 0)
976 		m_can_enable_all_interrupts(cdev);
977 
978 	return work_done;
979 }
980 
981 static int m_can_poll(struct napi_struct *napi, int quota)
982 {
983 	struct net_device *dev = napi->dev;
984 	struct m_can_classdev *cdev = netdev_priv(dev);
985 	int work_done;
986 	u32 irqstatus;
987 
988 	irqstatus = cdev->irqstatus | m_can_read(cdev, M_CAN_IR);
989 
990 	work_done = m_can_rx_handler(dev, quota, irqstatus);
991 
992 	/* Don't re-enable interrupts if the driver had a fatal error
993 	 * (e.g., FIFO read failure).
994 	 */
995 	if (work_done >= 0 && work_done < quota) {
996 		napi_complete_done(napi, work_done);
997 		m_can_enable_all_interrupts(cdev);
998 	}
999 
1000 	return work_done;
1001 }
1002 
1003 /* Echo tx skb and update net stats. Peripherals use rx-offload for
1004  * echo. timestamp is used for peripherals to ensure correct ordering
1005  * by rx-offload, and is ignored for non-peripherals.
1006  */
1007 static void m_can_tx_update_stats(struct m_can_classdev *cdev,
1008 				  unsigned int msg_mark,
1009 				  u32 timestamp)
1010 {
1011 	struct net_device *dev = cdev->net;
1012 	struct net_device_stats *stats = &dev->stats;
1013 
1014 	if (cdev->is_peripheral)
1015 		stats->tx_bytes +=
1016 			can_rx_offload_get_echo_skb(&cdev->offload,
1017 						    msg_mark,
1018 						    timestamp,
1019 						    NULL);
1020 	else
1021 		stats->tx_bytes += can_get_echo_skb(dev, msg_mark, NULL);
1022 
1023 	stats->tx_packets++;
1024 }
1025 
1026 static int m_can_echo_tx_event(struct net_device *dev)
1027 {
1028 	u32 txe_count = 0;
1029 	u32 m_can_txefs;
1030 	u32 fgi = 0;
1031 	int ack_fgi = -1;
1032 	int i = 0;
1033 	int err = 0;
1034 	unsigned int msg_mark;
1035 
1036 	struct m_can_classdev *cdev = netdev_priv(dev);
1037 
1038 	/* read tx event fifo status */
1039 	m_can_txefs = m_can_read(cdev, M_CAN_TXEFS);
1040 
1041 	/* Get Tx Event fifo element count */
1042 	txe_count = FIELD_GET(TXEFS_EFFL_MASK, m_can_txefs);
1043 	fgi = FIELD_GET(TXEFS_EFGI_MASK, m_can_txefs);
1044 
1045 	/* Get and process all sent elements */
1046 	for (i = 0; i < txe_count; i++) {
1047 		u32 txe, timestamp = 0;
1048 
1049 		/* get message marker, timestamp */
1050 		err = m_can_txe_fifo_read(cdev, fgi, 4, &txe);
1051 		if (err) {
1052 			netdev_err(dev, "TXE FIFO read returned %d\n", err);
1053 			break;
1054 		}
1055 
1056 		msg_mark = FIELD_GET(TX_EVENT_MM_MASK, txe);
1057 		timestamp = FIELD_GET(TX_EVENT_TXTS_MASK, txe) << 16;
1058 
1059 		ack_fgi = fgi;
1060 		fgi = (++fgi >= cdev->mcfg[MRAM_TXE].num ? 0 : fgi);
1061 
1062 		/* update stats */
1063 		m_can_tx_update_stats(cdev, msg_mark, timestamp);
1064 	}
1065 
1066 	if (ack_fgi != -1)
1067 		m_can_write(cdev, M_CAN_TXEFA, FIELD_PREP(TXEFA_EFAI_MASK,
1068 							  ack_fgi));
1069 
1070 	return err;
1071 }
1072 
1073 static irqreturn_t m_can_isr(int irq, void *dev_id)
1074 {
1075 	struct net_device *dev = (struct net_device *)dev_id;
1076 	struct m_can_classdev *cdev = netdev_priv(dev);
1077 	u32 ir;
1078 
1079 	if (pm_runtime_suspended(cdev->dev))
1080 		return IRQ_NONE;
1081 	ir = m_can_read(cdev, M_CAN_IR);
1082 	if (!ir)
1083 		return IRQ_NONE;
1084 
1085 	/* ACK all irqs */
1086 	if (ir & IR_ALL_INT)
1087 		m_can_write(cdev, M_CAN_IR, ir);
1088 
1089 	if (cdev->ops->clear_interrupts)
1090 		cdev->ops->clear_interrupts(cdev);
1091 
1092 	/* schedule NAPI in case of
1093 	 * - rx IRQ
1094 	 * - state change IRQ
1095 	 * - bus error IRQ and bus error reporting
1096 	 */
1097 	if ((ir & IR_RF0N) || (ir & IR_ERR_ALL_30X)) {
1098 		cdev->irqstatus = ir;
1099 		m_can_disable_all_interrupts(cdev);
1100 		if (!cdev->is_peripheral)
1101 			napi_schedule(&cdev->napi);
1102 		else if (m_can_rx_peripheral(dev, ir) < 0)
1103 			goto out_fail;
1104 	}
1105 
1106 	if (cdev->version == 30) {
1107 		if (ir & IR_TC) {
1108 			/* Transmission Complete Interrupt*/
1109 			u32 timestamp = 0;
1110 
1111 			if (cdev->is_peripheral)
1112 				timestamp = m_can_get_timestamp(cdev);
1113 			m_can_tx_update_stats(cdev, 0, timestamp);
1114 			netif_wake_queue(dev);
1115 		}
1116 	} else  {
1117 		if (ir & IR_TEFN) {
1118 			/* New TX FIFO Element arrived */
1119 			if (m_can_echo_tx_event(dev) != 0)
1120 				goto out_fail;
1121 
1122 			if (netif_queue_stopped(dev) &&
1123 			    !m_can_tx_fifo_full(cdev))
1124 				netif_wake_queue(dev);
1125 		}
1126 	}
1127 
1128 	if (cdev->is_peripheral)
1129 		can_rx_offload_threaded_irq_finish(&cdev->offload);
1130 
1131 	return IRQ_HANDLED;
1132 
1133 out_fail:
1134 	m_can_disable_all_interrupts(cdev);
1135 	return IRQ_HANDLED;
1136 }
1137 
1138 static const struct can_bittiming_const m_can_bittiming_const_30X = {
1139 	.name = KBUILD_MODNAME,
1140 	.tseg1_min = 2,		/* Time segment 1 = prop_seg + phase_seg1 */
1141 	.tseg1_max = 64,
1142 	.tseg2_min = 1,		/* Time segment 2 = phase_seg2 */
1143 	.tseg2_max = 16,
1144 	.sjw_max = 16,
1145 	.brp_min = 1,
1146 	.brp_max = 1024,
1147 	.brp_inc = 1,
1148 };
1149 
1150 static const struct can_bittiming_const m_can_data_bittiming_const_30X = {
1151 	.name = KBUILD_MODNAME,
1152 	.tseg1_min = 2,		/* Time segment 1 = prop_seg + phase_seg1 */
1153 	.tseg1_max = 16,
1154 	.tseg2_min = 1,		/* Time segment 2 = phase_seg2 */
1155 	.tseg2_max = 8,
1156 	.sjw_max = 4,
1157 	.brp_min = 1,
1158 	.brp_max = 32,
1159 	.brp_inc = 1,
1160 };
1161 
1162 static const struct can_bittiming_const m_can_bittiming_const_31X = {
1163 	.name = KBUILD_MODNAME,
1164 	.tseg1_min = 2,		/* Time segment 1 = prop_seg + phase_seg1 */
1165 	.tseg1_max = 256,
1166 	.tseg2_min = 2,		/* Time segment 2 = phase_seg2 */
1167 	.tseg2_max = 128,
1168 	.sjw_max = 128,
1169 	.brp_min = 1,
1170 	.brp_max = 512,
1171 	.brp_inc = 1,
1172 };
1173 
1174 static const struct can_bittiming_const m_can_data_bittiming_const_31X = {
1175 	.name = KBUILD_MODNAME,
1176 	.tseg1_min = 1,		/* Time segment 1 = prop_seg + phase_seg1 */
1177 	.tseg1_max = 32,
1178 	.tseg2_min = 1,		/* Time segment 2 = phase_seg2 */
1179 	.tseg2_max = 16,
1180 	.sjw_max = 16,
1181 	.brp_min = 1,
1182 	.brp_max = 32,
1183 	.brp_inc = 1,
1184 };
1185 
1186 static int m_can_set_bittiming(struct net_device *dev)
1187 {
1188 	struct m_can_classdev *cdev = netdev_priv(dev);
1189 	const struct can_bittiming *bt = &cdev->can.bittiming;
1190 	const struct can_bittiming *dbt = &cdev->can.data_bittiming;
1191 	u16 brp, sjw, tseg1, tseg2;
1192 	u32 reg_btp;
1193 
1194 	brp = bt->brp - 1;
1195 	sjw = bt->sjw - 1;
1196 	tseg1 = bt->prop_seg + bt->phase_seg1 - 1;
1197 	tseg2 = bt->phase_seg2 - 1;
1198 	reg_btp = FIELD_PREP(NBTP_NBRP_MASK, brp) |
1199 		  FIELD_PREP(NBTP_NSJW_MASK, sjw) |
1200 		  FIELD_PREP(NBTP_NTSEG1_MASK, tseg1) |
1201 		  FIELD_PREP(NBTP_NTSEG2_MASK, tseg2);
1202 	m_can_write(cdev, M_CAN_NBTP, reg_btp);
1203 
1204 	if (cdev->can.ctrlmode & CAN_CTRLMODE_FD) {
1205 		reg_btp = 0;
1206 		brp = dbt->brp - 1;
1207 		sjw = dbt->sjw - 1;
1208 		tseg1 = dbt->prop_seg + dbt->phase_seg1 - 1;
1209 		tseg2 = dbt->phase_seg2 - 1;
1210 
1211 		/* TDC is only needed for bitrates beyond 2.5 MBit/s.
1212 		 * This is mentioned in the "Bit Time Requirements for CAN FD"
1213 		 * paper presented at the International CAN Conference 2013
1214 		 */
1215 		if (dbt->bitrate > 2500000) {
1216 			u32 tdco, ssp;
1217 
1218 			/* Use the same value of secondary sampling point
1219 			 * as the data sampling point
1220 			 */
1221 			ssp = dbt->sample_point;
1222 
1223 			/* Equation based on Bosch's M_CAN User Manual's
1224 			 * Transmitter Delay Compensation Section
1225 			 */
1226 			tdco = (cdev->can.clock.freq / 1000) *
1227 				ssp / dbt->bitrate;
1228 
1229 			/* Max valid TDCO value is 127 */
1230 			if (tdco > 127) {
1231 				netdev_warn(dev, "TDCO value of %u is beyond maximum. Using maximum possible value\n",
1232 					    tdco);
1233 				tdco = 127;
1234 			}
1235 
1236 			reg_btp |= DBTP_TDC;
1237 			m_can_write(cdev, M_CAN_TDCR,
1238 				    FIELD_PREP(TDCR_TDCO_MASK, tdco));
1239 		}
1240 
1241 		reg_btp |= FIELD_PREP(DBTP_DBRP_MASK, brp) |
1242 			FIELD_PREP(DBTP_DSJW_MASK, sjw) |
1243 			FIELD_PREP(DBTP_DTSEG1_MASK, tseg1) |
1244 			FIELD_PREP(DBTP_DTSEG2_MASK, tseg2);
1245 
1246 		m_can_write(cdev, M_CAN_DBTP, reg_btp);
1247 	}
1248 
1249 	return 0;
1250 }
1251 
1252 /* Configure M_CAN chip:
1253  * - set rx buffer/fifo element size
1254  * - configure rx fifo
1255  * - accept non-matching frame into fifo 0
1256  * - configure tx buffer
1257  *		- >= v3.1.x: TX FIFO is used
1258  * - configure mode
1259  * - setup bittiming
1260  * - configure timestamp generation
1261  */
1262 static int m_can_chip_config(struct net_device *dev)
1263 {
1264 	struct m_can_classdev *cdev = netdev_priv(dev);
1265 	u32 cccr, test;
1266 	int err;
1267 
1268 	err = m_can_init_ram(cdev);
1269 	if (err) {
1270 		dev_err(cdev->dev, "Message RAM configuration failed\n");
1271 		return err;
1272 	}
1273 
1274 	m_can_config_endisable(cdev, true);
1275 
1276 	/* RX Buffer/FIFO Element Size 64 bytes data field */
1277 	m_can_write(cdev, M_CAN_RXESC,
1278 		    FIELD_PREP(RXESC_RBDS_MASK, RXESC_64B) |
1279 		    FIELD_PREP(RXESC_F1DS_MASK, RXESC_64B) |
1280 		    FIELD_PREP(RXESC_F0DS_MASK, RXESC_64B));
1281 
1282 	/* Accept Non-matching Frames Into FIFO 0 */
1283 	m_can_write(cdev, M_CAN_GFC, 0x0);
1284 
1285 	if (cdev->version == 30) {
1286 		/* only support one Tx Buffer currently */
1287 		m_can_write(cdev, M_CAN_TXBC, FIELD_PREP(TXBC_NDTB_MASK, 1) |
1288 			    cdev->mcfg[MRAM_TXB].off);
1289 	} else {
1290 		/* TX FIFO is used for newer IP Core versions */
1291 		m_can_write(cdev, M_CAN_TXBC,
1292 			    FIELD_PREP(TXBC_TFQS_MASK,
1293 				       cdev->mcfg[MRAM_TXB].num) |
1294 			    cdev->mcfg[MRAM_TXB].off);
1295 	}
1296 
1297 	/* support 64 bytes payload */
1298 	m_can_write(cdev, M_CAN_TXESC,
1299 		    FIELD_PREP(TXESC_TBDS_MASK, TXESC_TBDS_64B));
1300 
1301 	/* TX Event FIFO */
1302 	if (cdev->version == 30) {
1303 		m_can_write(cdev, M_CAN_TXEFC,
1304 			    FIELD_PREP(TXEFC_EFS_MASK, 1) |
1305 			    cdev->mcfg[MRAM_TXE].off);
1306 	} else {
1307 		/* Full TX Event FIFO is used */
1308 		m_can_write(cdev, M_CAN_TXEFC,
1309 			    FIELD_PREP(TXEFC_EFS_MASK,
1310 				       cdev->mcfg[MRAM_TXE].num) |
1311 			    cdev->mcfg[MRAM_TXE].off);
1312 	}
1313 
1314 	/* rx fifo configuration, blocking mode, fifo size 1 */
1315 	m_can_write(cdev, M_CAN_RXF0C,
1316 		    FIELD_PREP(RXFC_FS_MASK, cdev->mcfg[MRAM_RXF0].num) |
1317 		    cdev->mcfg[MRAM_RXF0].off);
1318 
1319 	m_can_write(cdev, M_CAN_RXF1C,
1320 		    FIELD_PREP(RXFC_FS_MASK, cdev->mcfg[MRAM_RXF1].num) |
1321 		    cdev->mcfg[MRAM_RXF1].off);
1322 
1323 	cccr = m_can_read(cdev, M_CAN_CCCR);
1324 	test = m_can_read(cdev, M_CAN_TEST);
1325 	test &= ~TEST_LBCK;
1326 	if (cdev->version == 30) {
1327 		/* Version 3.0.x */
1328 
1329 		cccr &= ~(CCCR_TEST | CCCR_MON | CCCR_DAR |
1330 			  FIELD_PREP(CCCR_CMR_MASK, FIELD_MAX(CCCR_CMR_MASK)) |
1331 			  FIELD_PREP(CCCR_CME_MASK, FIELD_MAX(CCCR_CME_MASK)));
1332 
1333 		if (cdev->can.ctrlmode & CAN_CTRLMODE_FD)
1334 			cccr |= FIELD_PREP(CCCR_CME_MASK, CCCR_CME_CANFD_BRS);
1335 
1336 	} else {
1337 		/* Version 3.1.x or 3.2.x */
1338 		cccr &= ~(CCCR_TEST | CCCR_MON | CCCR_BRSE | CCCR_FDOE |
1339 			  CCCR_NISO | CCCR_DAR);
1340 
1341 		/* Only 3.2.x has NISO Bit implemented */
1342 		if (cdev->can.ctrlmode & CAN_CTRLMODE_FD_NON_ISO)
1343 			cccr |= CCCR_NISO;
1344 
1345 		if (cdev->can.ctrlmode & CAN_CTRLMODE_FD)
1346 			cccr |= (CCCR_BRSE | CCCR_FDOE);
1347 	}
1348 
1349 	/* Loopback Mode */
1350 	if (cdev->can.ctrlmode & CAN_CTRLMODE_LOOPBACK) {
1351 		cccr |= CCCR_TEST | CCCR_MON;
1352 		test |= TEST_LBCK;
1353 	}
1354 
1355 	/* Enable Monitoring (all versions) */
1356 	if (cdev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
1357 		cccr |= CCCR_MON;
1358 
1359 	/* Disable Auto Retransmission (all versions) */
1360 	if (cdev->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT)
1361 		cccr |= CCCR_DAR;
1362 
1363 	/* Write config */
1364 	m_can_write(cdev, M_CAN_CCCR, cccr);
1365 	m_can_write(cdev, M_CAN_TEST, test);
1366 
1367 	/* Enable interrupts */
1368 	m_can_write(cdev, M_CAN_IR, IR_ALL_INT);
1369 	if (!(cdev->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING))
1370 		if (cdev->version == 30)
1371 			m_can_write(cdev, M_CAN_IE, IR_ALL_INT &
1372 				    ~(IR_ERR_LEC_30X));
1373 		else
1374 			m_can_write(cdev, M_CAN_IE, IR_ALL_INT &
1375 				    ~(IR_ERR_LEC_31X));
1376 	else
1377 		m_can_write(cdev, M_CAN_IE, IR_ALL_INT);
1378 
1379 	/* route all interrupts to INT0 */
1380 	m_can_write(cdev, M_CAN_ILS, ILS_ALL_INT0);
1381 
1382 	/* set bittiming params */
1383 	m_can_set_bittiming(dev);
1384 
1385 	/* enable internal timestamp generation, with a prescaler of 16. The
1386 	 * prescaler is applied to the nominal bit timing
1387 	 */
1388 	m_can_write(cdev, M_CAN_TSCC,
1389 		    FIELD_PREP(TSCC_TCP_MASK, 0xf) |
1390 		    FIELD_PREP(TSCC_TSS_MASK, TSCC_TSS_INTERNAL));
1391 
1392 	m_can_config_endisable(cdev, false);
1393 
1394 	if (cdev->ops->init)
1395 		cdev->ops->init(cdev);
1396 
1397 	return 0;
1398 }
1399 
1400 static int m_can_start(struct net_device *dev)
1401 {
1402 	struct m_can_classdev *cdev = netdev_priv(dev);
1403 	int ret;
1404 
1405 	/* basic m_can configuration */
1406 	ret = m_can_chip_config(dev);
1407 	if (ret)
1408 		return ret;
1409 
1410 	cdev->can.state = CAN_STATE_ERROR_ACTIVE;
1411 
1412 	m_can_enable_all_interrupts(cdev);
1413 
1414 	return 0;
1415 }
1416 
1417 static int m_can_set_mode(struct net_device *dev, enum can_mode mode)
1418 {
1419 	switch (mode) {
1420 	case CAN_MODE_START:
1421 		m_can_clean(dev);
1422 		m_can_start(dev);
1423 		netif_wake_queue(dev);
1424 		break;
1425 	default:
1426 		return -EOPNOTSUPP;
1427 	}
1428 
1429 	return 0;
1430 }
1431 
1432 /* Checks core release number of M_CAN
1433  * returns 0 if an unsupported device is detected
1434  * else it returns the release and step coded as:
1435  * return value = 10 * <release> + 1 * <step>
1436  */
1437 static int m_can_check_core_release(struct m_can_classdev *cdev)
1438 {
1439 	u32 crel_reg;
1440 	u8 rel;
1441 	u8 step;
1442 	int res;
1443 
1444 	/* Read Core Release Version and split into version number
1445 	 * Example: Version 3.2.1 => rel = 3; step = 2; substep = 1;
1446 	 */
1447 	crel_reg = m_can_read(cdev, M_CAN_CREL);
1448 	rel = (u8)FIELD_GET(CREL_REL_MASK, crel_reg);
1449 	step = (u8)FIELD_GET(CREL_STEP_MASK, crel_reg);
1450 
1451 	if (rel == 3) {
1452 		/* M_CAN v3.x.y: create return value */
1453 		res = 30 + step;
1454 	} else {
1455 		/* Unsupported M_CAN version */
1456 		res = 0;
1457 	}
1458 
1459 	return res;
1460 }
1461 
1462 /* Selectable Non ISO support only in version 3.2.x
1463  * This function checks if the bit is writable.
1464  */
1465 static bool m_can_niso_supported(struct m_can_classdev *cdev)
1466 {
1467 	u32 cccr_reg, cccr_poll = 0;
1468 	int niso_timeout = -ETIMEDOUT;
1469 	int i;
1470 
1471 	m_can_config_endisable(cdev, true);
1472 	cccr_reg = m_can_read(cdev, M_CAN_CCCR);
1473 	cccr_reg |= CCCR_NISO;
1474 	m_can_write(cdev, M_CAN_CCCR, cccr_reg);
1475 
1476 	for (i = 0; i <= 10; i++) {
1477 		cccr_poll = m_can_read(cdev, M_CAN_CCCR);
1478 		if (cccr_poll == cccr_reg) {
1479 			niso_timeout = 0;
1480 			break;
1481 		}
1482 
1483 		usleep_range(1, 5);
1484 	}
1485 
1486 	/* Clear NISO */
1487 	cccr_reg &= ~(CCCR_NISO);
1488 	m_can_write(cdev, M_CAN_CCCR, cccr_reg);
1489 
1490 	m_can_config_endisable(cdev, false);
1491 
1492 	/* return false if time out (-ETIMEDOUT), else return true */
1493 	return !niso_timeout;
1494 }
1495 
1496 static int m_can_dev_setup(struct m_can_classdev *cdev)
1497 {
1498 	struct net_device *dev = cdev->net;
1499 	int m_can_version, err;
1500 
1501 	m_can_version = m_can_check_core_release(cdev);
1502 	/* return if unsupported version */
1503 	if (!m_can_version) {
1504 		dev_err(cdev->dev, "Unsupported version number: %2d",
1505 			m_can_version);
1506 		return -EINVAL;
1507 	}
1508 
1509 	if (!cdev->is_peripheral)
1510 		netif_napi_add(dev, &cdev->napi, m_can_poll);
1511 
1512 	/* Shared properties of all M_CAN versions */
1513 	cdev->version = m_can_version;
1514 	cdev->can.do_set_mode = m_can_set_mode;
1515 	cdev->can.do_get_berr_counter = m_can_get_berr_counter;
1516 
1517 	/* Set M_CAN supported operations */
1518 	cdev->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK |
1519 		CAN_CTRLMODE_LISTENONLY |
1520 		CAN_CTRLMODE_BERR_REPORTING |
1521 		CAN_CTRLMODE_FD |
1522 		CAN_CTRLMODE_ONE_SHOT;
1523 
1524 	/* Set properties depending on M_CAN version */
1525 	switch (cdev->version) {
1526 	case 30:
1527 		/* CAN_CTRLMODE_FD_NON_ISO is fixed with M_CAN IP v3.0.x */
1528 		err = can_set_static_ctrlmode(dev, CAN_CTRLMODE_FD_NON_ISO);
1529 		if (err)
1530 			return err;
1531 		cdev->can.bittiming_const = &m_can_bittiming_const_30X;
1532 		cdev->can.data_bittiming_const = &m_can_data_bittiming_const_30X;
1533 		break;
1534 	case 31:
1535 		/* CAN_CTRLMODE_FD_NON_ISO is fixed with M_CAN IP v3.1.x */
1536 		err = can_set_static_ctrlmode(dev, CAN_CTRLMODE_FD_NON_ISO);
1537 		if (err)
1538 			return err;
1539 		cdev->can.bittiming_const = &m_can_bittiming_const_31X;
1540 		cdev->can.data_bittiming_const = &m_can_data_bittiming_const_31X;
1541 		break;
1542 	case 32:
1543 	case 33:
1544 		/* Support both MCAN version v3.2.x and v3.3.0 */
1545 		cdev->can.bittiming_const = &m_can_bittiming_const_31X;
1546 		cdev->can.data_bittiming_const = &m_can_data_bittiming_const_31X;
1547 
1548 		cdev->can.ctrlmode_supported |=
1549 			(m_can_niso_supported(cdev) ?
1550 			 CAN_CTRLMODE_FD_NON_ISO : 0);
1551 		break;
1552 	default:
1553 		dev_err(cdev->dev, "Unsupported version number: %2d",
1554 			cdev->version);
1555 		return -EINVAL;
1556 	}
1557 
1558 	if (cdev->ops->init)
1559 		cdev->ops->init(cdev);
1560 
1561 	return 0;
1562 }
1563 
1564 static void m_can_stop(struct net_device *dev)
1565 {
1566 	struct m_can_classdev *cdev = netdev_priv(dev);
1567 
1568 	/* disable all interrupts */
1569 	m_can_disable_all_interrupts(cdev);
1570 
1571 	/* Set init mode to disengage from the network */
1572 	m_can_config_endisable(cdev, true);
1573 
1574 	/* set the state as STOPPED */
1575 	cdev->can.state = CAN_STATE_STOPPED;
1576 }
1577 
1578 static int m_can_close(struct net_device *dev)
1579 {
1580 	struct m_can_classdev *cdev = netdev_priv(dev);
1581 
1582 	netif_stop_queue(dev);
1583 
1584 	if (!cdev->is_peripheral)
1585 		napi_disable(&cdev->napi);
1586 
1587 	m_can_stop(dev);
1588 	m_can_clk_stop(cdev);
1589 	free_irq(dev->irq, dev);
1590 
1591 	if (cdev->is_peripheral) {
1592 		cdev->tx_skb = NULL;
1593 		destroy_workqueue(cdev->tx_wq);
1594 		cdev->tx_wq = NULL;
1595 	}
1596 
1597 	if (cdev->is_peripheral)
1598 		can_rx_offload_disable(&cdev->offload);
1599 
1600 	close_candev(dev);
1601 
1602 	phy_power_off(cdev->transceiver);
1603 
1604 	return 0;
1605 }
1606 
1607 static int m_can_next_echo_skb_occupied(struct net_device *dev, int putidx)
1608 {
1609 	struct m_can_classdev *cdev = netdev_priv(dev);
1610 	/*get wrap around for loopback skb index */
1611 	unsigned int wrap = cdev->can.echo_skb_max;
1612 	int next_idx;
1613 
1614 	/* calculate next index */
1615 	next_idx = (++putidx >= wrap ? 0 : putidx);
1616 
1617 	/* check if occupied */
1618 	return !!cdev->can.echo_skb[next_idx];
1619 }
1620 
1621 static netdev_tx_t m_can_tx_handler(struct m_can_classdev *cdev)
1622 {
1623 	struct canfd_frame *cf = (struct canfd_frame *)cdev->tx_skb->data;
1624 	struct net_device *dev = cdev->net;
1625 	struct sk_buff *skb = cdev->tx_skb;
1626 	struct id_and_dlc fifo_header;
1627 	u32 cccr, fdflags;
1628 	u32 txfqs;
1629 	int err;
1630 	int putidx;
1631 
1632 	cdev->tx_skb = NULL;
1633 
1634 	/* Generate ID field for TX buffer Element */
1635 	/* Common to all supported M_CAN versions */
1636 	if (cf->can_id & CAN_EFF_FLAG) {
1637 		fifo_header.id = cf->can_id & CAN_EFF_MASK;
1638 		fifo_header.id |= TX_BUF_XTD;
1639 	} else {
1640 		fifo_header.id = ((cf->can_id & CAN_SFF_MASK) << 18);
1641 	}
1642 
1643 	if (cf->can_id & CAN_RTR_FLAG)
1644 		fifo_header.id |= TX_BUF_RTR;
1645 
1646 	if (cdev->version == 30) {
1647 		netif_stop_queue(dev);
1648 
1649 		fifo_header.dlc = can_fd_len2dlc(cf->len) << 16;
1650 
1651 		/* Write the frame ID, DLC, and payload to the FIFO element. */
1652 		err = m_can_fifo_write(cdev, 0, M_CAN_FIFO_ID, &fifo_header, 2);
1653 		if (err)
1654 			goto out_fail;
1655 
1656 		err = m_can_fifo_write(cdev, 0, M_CAN_FIFO_DATA,
1657 				       cf->data, DIV_ROUND_UP(cf->len, 4));
1658 		if (err)
1659 			goto out_fail;
1660 
1661 		if (cdev->can.ctrlmode & CAN_CTRLMODE_FD) {
1662 			cccr = m_can_read(cdev, M_CAN_CCCR);
1663 			cccr &= ~CCCR_CMR_MASK;
1664 			if (can_is_canfd_skb(skb)) {
1665 				if (cf->flags & CANFD_BRS)
1666 					cccr |= FIELD_PREP(CCCR_CMR_MASK,
1667 							   CCCR_CMR_CANFD_BRS);
1668 				else
1669 					cccr |= FIELD_PREP(CCCR_CMR_MASK,
1670 							   CCCR_CMR_CANFD);
1671 			} else {
1672 				cccr |= FIELD_PREP(CCCR_CMR_MASK, CCCR_CMR_CAN);
1673 			}
1674 			m_can_write(cdev, M_CAN_CCCR, cccr);
1675 		}
1676 		m_can_write(cdev, M_CAN_TXBTIE, 0x1);
1677 
1678 		can_put_echo_skb(skb, dev, 0, 0);
1679 
1680 		m_can_write(cdev, M_CAN_TXBAR, 0x1);
1681 		/* End of xmit function for version 3.0.x */
1682 	} else {
1683 		/* Transmit routine for version >= v3.1.x */
1684 
1685 		txfqs = m_can_read(cdev, M_CAN_TXFQS);
1686 
1687 		/* Check if FIFO full */
1688 		if (_m_can_tx_fifo_full(txfqs)) {
1689 			/* This shouldn't happen */
1690 			netif_stop_queue(dev);
1691 			netdev_warn(dev,
1692 				    "TX queue active although FIFO is full.");
1693 
1694 			if (cdev->is_peripheral) {
1695 				kfree_skb(skb);
1696 				dev->stats.tx_dropped++;
1697 				return NETDEV_TX_OK;
1698 			} else {
1699 				return NETDEV_TX_BUSY;
1700 			}
1701 		}
1702 
1703 		/* get put index for frame */
1704 		putidx = FIELD_GET(TXFQS_TFQPI_MASK, txfqs);
1705 
1706 		/* Construct DLC Field, with CAN-FD configuration.
1707 		 * Use the put index of the fifo as the message marker,
1708 		 * used in the TX interrupt for sending the correct echo frame.
1709 		 */
1710 
1711 		/* get CAN FD configuration of frame */
1712 		fdflags = 0;
1713 		if (can_is_canfd_skb(skb)) {
1714 			fdflags |= TX_BUF_FDF;
1715 			if (cf->flags & CANFD_BRS)
1716 				fdflags |= TX_BUF_BRS;
1717 		}
1718 
1719 		fifo_header.dlc = FIELD_PREP(TX_BUF_MM_MASK, putidx) |
1720 			FIELD_PREP(TX_BUF_DLC_MASK, can_fd_len2dlc(cf->len)) |
1721 			fdflags | TX_BUF_EFC;
1722 		err = m_can_fifo_write(cdev, putidx, M_CAN_FIFO_ID, &fifo_header, 2);
1723 		if (err)
1724 			goto out_fail;
1725 
1726 		err = m_can_fifo_write(cdev, putidx, M_CAN_FIFO_DATA,
1727 				       cf->data, DIV_ROUND_UP(cf->len, 4));
1728 		if (err)
1729 			goto out_fail;
1730 
1731 		/* Push loopback echo.
1732 		 * Will be looped back on TX interrupt based on message marker
1733 		 */
1734 		can_put_echo_skb(skb, dev, putidx, 0);
1735 
1736 		/* Enable TX FIFO element to start transfer  */
1737 		m_can_write(cdev, M_CAN_TXBAR, (1 << putidx));
1738 
1739 		/* stop network queue if fifo full */
1740 		if (m_can_tx_fifo_full(cdev) ||
1741 		    m_can_next_echo_skb_occupied(dev, putidx))
1742 			netif_stop_queue(dev);
1743 	}
1744 
1745 	return NETDEV_TX_OK;
1746 
1747 out_fail:
1748 	netdev_err(dev, "FIFO write returned %d\n", err);
1749 	m_can_disable_all_interrupts(cdev);
1750 	return NETDEV_TX_BUSY;
1751 }
1752 
1753 static void m_can_tx_work_queue(struct work_struct *ws)
1754 {
1755 	struct m_can_classdev *cdev = container_of(ws, struct m_can_classdev,
1756 						   tx_work);
1757 
1758 	m_can_tx_handler(cdev);
1759 }
1760 
1761 static netdev_tx_t m_can_start_xmit(struct sk_buff *skb,
1762 				    struct net_device *dev)
1763 {
1764 	struct m_can_classdev *cdev = netdev_priv(dev);
1765 
1766 	if (can_dev_dropped_skb(dev, skb))
1767 		return NETDEV_TX_OK;
1768 
1769 	if (cdev->is_peripheral) {
1770 		if (cdev->tx_skb) {
1771 			netdev_err(dev, "hard_xmit called while tx busy\n");
1772 			return NETDEV_TX_BUSY;
1773 		}
1774 
1775 		if (cdev->can.state == CAN_STATE_BUS_OFF) {
1776 			m_can_clean(dev);
1777 		} else {
1778 			/* Need to stop the queue to avoid numerous requests
1779 			 * from being sent.  Suggested improvement is to create
1780 			 * a queueing mechanism that will queue the skbs and
1781 			 * process them in order.
1782 			 */
1783 			cdev->tx_skb = skb;
1784 			netif_stop_queue(cdev->net);
1785 			queue_work(cdev->tx_wq, &cdev->tx_work);
1786 		}
1787 	} else {
1788 		cdev->tx_skb = skb;
1789 		return m_can_tx_handler(cdev);
1790 	}
1791 
1792 	return NETDEV_TX_OK;
1793 }
1794 
1795 static int m_can_open(struct net_device *dev)
1796 {
1797 	struct m_can_classdev *cdev = netdev_priv(dev);
1798 	int err;
1799 
1800 	err = phy_power_on(cdev->transceiver);
1801 	if (err)
1802 		return err;
1803 
1804 	err = m_can_clk_start(cdev);
1805 	if (err)
1806 		goto out_phy_power_off;
1807 
1808 	/* open the can device */
1809 	err = open_candev(dev);
1810 	if (err) {
1811 		netdev_err(dev, "failed to open can device\n");
1812 		goto exit_disable_clks;
1813 	}
1814 
1815 	if (cdev->is_peripheral)
1816 		can_rx_offload_enable(&cdev->offload);
1817 
1818 	/* register interrupt handler */
1819 	if (cdev->is_peripheral) {
1820 		cdev->tx_skb = NULL;
1821 		cdev->tx_wq = alloc_workqueue("mcan_wq",
1822 					      WQ_FREEZABLE | WQ_MEM_RECLAIM, 0);
1823 		if (!cdev->tx_wq) {
1824 			err = -ENOMEM;
1825 			goto out_wq_fail;
1826 		}
1827 
1828 		INIT_WORK(&cdev->tx_work, m_can_tx_work_queue);
1829 
1830 		err = request_threaded_irq(dev->irq, NULL, m_can_isr,
1831 					   IRQF_ONESHOT,
1832 					   dev->name, dev);
1833 	} else {
1834 		err = request_irq(dev->irq, m_can_isr, IRQF_SHARED, dev->name,
1835 				  dev);
1836 	}
1837 
1838 	if (err < 0) {
1839 		netdev_err(dev, "failed to request interrupt\n");
1840 		goto exit_irq_fail;
1841 	}
1842 
1843 	/* start the m_can controller */
1844 	err = m_can_start(dev);
1845 	if (err)
1846 		goto exit_irq_fail;
1847 
1848 	if (!cdev->is_peripheral)
1849 		napi_enable(&cdev->napi);
1850 
1851 	netif_start_queue(dev);
1852 
1853 	return 0;
1854 
1855 exit_irq_fail:
1856 	if (cdev->is_peripheral)
1857 		destroy_workqueue(cdev->tx_wq);
1858 out_wq_fail:
1859 	if (cdev->is_peripheral)
1860 		can_rx_offload_disable(&cdev->offload);
1861 	close_candev(dev);
1862 exit_disable_clks:
1863 	m_can_clk_stop(cdev);
1864 out_phy_power_off:
1865 	phy_power_off(cdev->transceiver);
1866 	return err;
1867 }
1868 
1869 static const struct net_device_ops m_can_netdev_ops = {
1870 	.ndo_open = m_can_open,
1871 	.ndo_stop = m_can_close,
1872 	.ndo_start_xmit = m_can_start_xmit,
1873 	.ndo_change_mtu = can_change_mtu,
1874 };
1875 
1876 static const struct ethtool_ops m_can_ethtool_ops = {
1877 	.get_ts_info = ethtool_op_get_ts_info,
1878 };
1879 
1880 static int register_m_can_dev(struct net_device *dev)
1881 {
1882 	dev->flags |= IFF_ECHO;	/* we support local echo */
1883 	dev->netdev_ops = &m_can_netdev_ops;
1884 	dev->ethtool_ops = &m_can_ethtool_ops;
1885 
1886 	return register_candev(dev);
1887 }
1888 
1889 static void m_can_of_parse_mram(struct m_can_classdev *cdev,
1890 				const u32 *mram_config_vals)
1891 {
1892 	cdev->mcfg[MRAM_SIDF].off = mram_config_vals[0];
1893 	cdev->mcfg[MRAM_SIDF].num = mram_config_vals[1];
1894 	cdev->mcfg[MRAM_XIDF].off = cdev->mcfg[MRAM_SIDF].off +
1895 		cdev->mcfg[MRAM_SIDF].num * SIDF_ELEMENT_SIZE;
1896 	cdev->mcfg[MRAM_XIDF].num = mram_config_vals[2];
1897 	cdev->mcfg[MRAM_RXF0].off = cdev->mcfg[MRAM_XIDF].off +
1898 		cdev->mcfg[MRAM_XIDF].num * XIDF_ELEMENT_SIZE;
1899 	cdev->mcfg[MRAM_RXF0].num = mram_config_vals[3] &
1900 		FIELD_MAX(RXFC_FS_MASK);
1901 	cdev->mcfg[MRAM_RXF1].off = cdev->mcfg[MRAM_RXF0].off +
1902 		cdev->mcfg[MRAM_RXF0].num * RXF0_ELEMENT_SIZE;
1903 	cdev->mcfg[MRAM_RXF1].num = mram_config_vals[4] &
1904 		FIELD_MAX(RXFC_FS_MASK);
1905 	cdev->mcfg[MRAM_RXB].off = cdev->mcfg[MRAM_RXF1].off +
1906 		cdev->mcfg[MRAM_RXF1].num * RXF1_ELEMENT_SIZE;
1907 	cdev->mcfg[MRAM_RXB].num = mram_config_vals[5];
1908 	cdev->mcfg[MRAM_TXE].off = cdev->mcfg[MRAM_RXB].off +
1909 		cdev->mcfg[MRAM_RXB].num * RXB_ELEMENT_SIZE;
1910 	cdev->mcfg[MRAM_TXE].num = mram_config_vals[6];
1911 	cdev->mcfg[MRAM_TXB].off = cdev->mcfg[MRAM_TXE].off +
1912 		cdev->mcfg[MRAM_TXE].num * TXE_ELEMENT_SIZE;
1913 	cdev->mcfg[MRAM_TXB].num = mram_config_vals[7] &
1914 		FIELD_MAX(TXBC_NDTB_MASK);
1915 
1916 	dev_dbg(cdev->dev,
1917 		"sidf 0x%x %d xidf 0x%x %d rxf0 0x%x %d rxf1 0x%x %d rxb 0x%x %d txe 0x%x %d txb 0x%x %d\n",
1918 		cdev->mcfg[MRAM_SIDF].off, cdev->mcfg[MRAM_SIDF].num,
1919 		cdev->mcfg[MRAM_XIDF].off, cdev->mcfg[MRAM_XIDF].num,
1920 		cdev->mcfg[MRAM_RXF0].off, cdev->mcfg[MRAM_RXF0].num,
1921 		cdev->mcfg[MRAM_RXF1].off, cdev->mcfg[MRAM_RXF1].num,
1922 		cdev->mcfg[MRAM_RXB].off, cdev->mcfg[MRAM_RXB].num,
1923 		cdev->mcfg[MRAM_TXE].off, cdev->mcfg[MRAM_TXE].num,
1924 		cdev->mcfg[MRAM_TXB].off, cdev->mcfg[MRAM_TXB].num);
1925 }
1926 
1927 int m_can_init_ram(struct m_can_classdev *cdev)
1928 {
1929 	int end, i, start;
1930 	int err = 0;
1931 
1932 	/* initialize the entire Message RAM in use to avoid possible
1933 	 * ECC/parity checksum errors when reading an uninitialized buffer
1934 	 */
1935 	start = cdev->mcfg[MRAM_SIDF].off;
1936 	end = cdev->mcfg[MRAM_TXB].off +
1937 		cdev->mcfg[MRAM_TXB].num * TXB_ELEMENT_SIZE;
1938 
1939 	for (i = start; i < end; i += 4) {
1940 		err = m_can_fifo_write_no_off(cdev, i, 0x0);
1941 		if (err)
1942 			break;
1943 	}
1944 
1945 	return err;
1946 }
1947 EXPORT_SYMBOL_GPL(m_can_init_ram);
1948 
1949 int m_can_class_get_clocks(struct m_can_classdev *cdev)
1950 {
1951 	int ret = 0;
1952 
1953 	cdev->hclk = devm_clk_get(cdev->dev, "hclk");
1954 	cdev->cclk = devm_clk_get(cdev->dev, "cclk");
1955 
1956 	if (IS_ERR(cdev->hclk) || IS_ERR(cdev->cclk)) {
1957 		dev_err(cdev->dev, "no clock found\n");
1958 		ret = -ENODEV;
1959 	}
1960 
1961 	return ret;
1962 }
1963 EXPORT_SYMBOL_GPL(m_can_class_get_clocks);
1964 
1965 struct m_can_classdev *m_can_class_allocate_dev(struct device *dev,
1966 						int sizeof_priv)
1967 {
1968 	struct m_can_classdev *class_dev = NULL;
1969 	u32 mram_config_vals[MRAM_CFG_LEN];
1970 	struct net_device *net_dev;
1971 	u32 tx_fifo_size;
1972 	int ret;
1973 
1974 	ret = fwnode_property_read_u32_array(dev_fwnode(dev),
1975 					     "bosch,mram-cfg",
1976 					     mram_config_vals,
1977 					     sizeof(mram_config_vals) / 4);
1978 	if (ret) {
1979 		dev_err(dev, "Could not get Message RAM configuration.");
1980 		goto out;
1981 	}
1982 
1983 	/* Get TX FIFO size
1984 	 * Defines the total amount of echo buffers for loopback
1985 	 */
1986 	tx_fifo_size = mram_config_vals[7];
1987 
1988 	/* allocate the m_can device */
1989 	net_dev = alloc_candev(sizeof_priv, tx_fifo_size);
1990 	if (!net_dev) {
1991 		dev_err(dev, "Failed to allocate CAN device");
1992 		goto out;
1993 	}
1994 
1995 	class_dev = netdev_priv(net_dev);
1996 	class_dev->net = net_dev;
1997 	class_dev->dev = dev;
1998 	SET_NETDEV_DEV(net_dev, dev);
1999 
2000 	m_can_of_parse_mram(class_dev, mram_config_vals);
2001 out:
2002 	return class_dev;
2003 }
2004 EXPORT_SYMBOL_GPL(m_can_class_allocate_dev);
2005 
2006 void m_can_class_free_dev(struct net_device *net)
2007 {
2008 	free_candev(net);
2009 }
2010 EXPORT_SYMBOL_GPL(m_can_class_free_dev);
2011 
2012 int m_can_class_register(struct m_can_classdev *cdev)
2013 {
2014 	int ret;
2015 
2016 	if (cdev->pm_clock_support) {
2017 		ret = m_can_clk_start(cdev);
2018 		if (ret)
2019 			return ret;
2020 	}
2021 
2022 	if (cdev->is_peripheral) {
2023 		ret = can_rx_offload_add_manual(cdev->net, &cdev->offload,
2024 						NAPI_POLL_WEIGHT);
2025 		if (ret)
2026 			goto clk_disable;
2027 	}
2028 
2029 	ret = m_can_dev_setup(cdev);
2030 	if (ret)
2031 		goto rx_offload_del;
2032 
2033 	ret = register_m_can_dev(cdev->net);
2034 	if (ret) {
2035 		dev_err(cdev->dev, "registering %s failed (err=%d)\n",
2036 			cdev->net->name, ret);
2037 		goto rx_offload_del;
2038 	}
2039 
2040 	of_can_transceiver(cdev->net);
2041 
2042 	dev_info(cdev->dev, "%s device registered (irq=%d, version=%d)\n",
2043 		 KBUILD_MODNAME, cdev->net->irq, cdev->version);
2044 
2045 	/* Probe finished
2046 	 * Stop clocks. They will be reactivated once the M_CAN device is opened
2047 	 */
2048 	m_can_clk_stop(cdev);
2049 
2050 	return 0;
2051 
2052 rx_offload_del:
2053 	if (cdev->is_peripheral)
2054 		can_rx_offload_del(&cdev->offload);
2055 clk_disable:
2056 	m_can_clk_stop(cdev);
2057 
2058 	return ret;
2059 }
2060 EXPORT_SYMBOL_GPL(m_can_class_register);
2061 
2062 void m_can_class_unregister(struct m_can_classdev *cdev)
2063 {
2064 	if (cdev->is_peripheral)
2065 		can_rx_offload_del(&cdev->offload);
2066 	unregister_candev(cdev->net);
2067 }
2068 EXPORT_SYMBOL_GPL(m_can_class_unregister);
2069 
2070 int m_can_class_suspend(struct device *dev)
2071 {
2072 	struct m_can_classdev *cdev = dev_get_drvdata(dev);
2073 	struct net_device *ndev = cdev->net;
2074 
2075 	if (netif_running(ndev)) {
2076 		netif_stop_queue(ndev);
2077 		netif_device_detach(ndev);
2078 		m_can_stop(ndev);
2079 		m_can_clk_stop(cdev);
2080 	}
2081 
2082 	pinctrl_pm_select_sleep_state(dev);
2083 
2084 	cdev->can.state = CAN_STATE_SLEEPING;
2085 
2086 	return 0;
2087 }
2088 EXPORT_SYMBOL_GPL(m_can_class_suspend);
2089 
2090 int m_can_class_resume(struct device *dev)
2091 {
2092 	struct m_can_classdev *cdev = dev_get_drvdata(dev);
2093 	struct net_device *ndev = cdev->net;
2094 
2095 	pinctrl_pm_select_default_state(dev);
2096 
2097 	cdev->can.state = CAN_STATE_ERROR_ACTIVE;
2098 
2099 	if (netif_running(ndev)) {
2100 		int ret;
2101 
2102 		ret = m_can_clk_start(cdev);
2103 		if (ret)
2104 			return ret;
2105 		ret  = m_can_start(ndev);
2106 		if (ret) {
2107 			m_can_clk_stop(cdev);
2108 
2109 			return ret;
2110 		}
2111 
2112 		netif_device_attach(ndev);
2113 		netif_start_queue(ndev);
2114 	}
2115 
2116 	return 0;
2117 }
2118 EXPORT_SYMBOL_GPL(m_can_class_resume);
2119 
2120 MODULE_AUTHOR("Dong Aisheng <b29396@freescale.com>");
2121 MODULE_AUTHOR("Dan Murphy <dmurphy@ti.com>");
2122 MODULE_LICENSE("GPL v2");
2123 MODULE_DESCRIPTION("CAN bus driver for Bosch M_CAN controller");
2124