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