xref: /openbmc/linux/drivers/net/can/m_can/m_can.c (revision 34fa67e7)
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 /* napi related */
81 #define M_CAN_NAPI_WEIGHT	64
82 
83 /* message ram configuration data length */
84 #define MRAM_CFG_LEN	8
85 
86 /* Core Release Register (CREL) */
87 #define CREL_REL_MASK		GENMASK(31, 28)
88 #define CREL_STEP_MASK		GENMASK(27, 24)
89 #define CREL_SUBSTEP_MASK	GENMASK(23, 20)
90 
91 /* Data Bit Timing & Prescaler Register (DBTP) */
92 #define DBTP_TDC		BIT(23)
93 #define DBTP_DBRP_MASK		GENMASK(20, 16)
94 #define DBTP_DTSEG1_MASK	GENMASK(12, 8)
95 #define DBTP_DTSEG2_MASK	GENMASK(7, 4)
96 #define DBTP_DSJW_MASK		GENMASK(3, 0)
97 
98 /* Transmitter Delay Compensation Register (TDCR) */
99 #define TDCR_TDCO_MASK		GENMASK(14, 8)
100 #define TDCR_TDCF_MASK		GENMASK(6, 0)
101 
102 /* Test Register (TEST) */
103 #define TEST_LBCK		BIT(4)
104 
105 /* CC Control Register (CCCR) */
106 #define CCCR_TXP		BIT(14)
107 #define CCCR_TEST		BIT(7)
108 #define CCCR_DAR		BIT(6)
109 #define CCCR_MON		BIT(5)
110 #define CCCR_CSR		BIT(4)
111 #define CCCR_CSA		BIT(3)
112 #define CCCR_ASM		BIT(2)
113 #define CCCR_CCE		BIT(1)
114 #define CCCR_INIT		BIT(0)
115 /* for version 3.0.x */
116 #define CCCR_CMR_MASK		GENMASK(11, 10)
117 #define CCCR_CMR_CANFD		0x1
118 #define CCCR_CMR_CANFD_BRS	0x2
119 #define CCCR_CMR_CAN		0x3
120 #define CCCR_CME_MASK		GENMASK(9, 8)
121 #define CCCR_CME_CAN		0
122 #define CCCR_CME_CANFD		0x1
123 #define CCCR_CME_CANFD_BRS	0x2
124 /* for version >=3.1.x */
125 #define CCCR_EFBI		BIT(13)
126 #define CCCR_PXHD		BIT(12)
127 #define CCCR_BRSE		BIT(9)
128 #define CCCR_FDOE		BIT(8)
129 /* for version >=3.2.x */
130 #define CCCR_NISO		BIT(15)
131 /* for version >=3.3.x */
132 #define CCCR_WMM		BIT(11)
133 #define CCCR_UTSU		BIT(10)
134 
135 /* Nominal Bit Timing & Prescaler Register (NBTP) */
136 #define NBTP_NSJW_MASK		GENMASK(31, 25)
137 #define NBTP_NBRP_MASK		GENMASK(24, 16)
138 #define NBTP_NTSEG1_MASK	GENMASK(15, 8)
139 #define NBTP_NTSEG2_MASK	GENMASK(6, 0)
140 
141 /* Timestamp Counter Configuration Register (TSCC) */
142 #define TSCC_TCP_MASK		GENMASK(19, 16)
143 #define TSCC_TSS_MASK		GENMASK(1, 0)
144 #define TSCC_TSS_DISABLE	0x0
145 #define TSCC_TSS_INTERNAL	0x1
146 #define TSCC_TSS_EXTERNAL	0x2
147 
148 /* Timestamp Counter Value Register (TSCV) */
149 #define TSCV_TSC_MASK		GENMASK(15, 0)
150 
151 /* Error Counter Register (ECR) */
152 #define ECR_RP			BIT(15)
153 #define ECR_REC_MASK		GENMASK(14, 8)
154 #define ECR_TEC_MASK		GENMASK(7, 0)
155 
156 /* Protocol Status Register (PSR) */
157 #define PSR_BO		BIT(7)
158 #define PSR_EW		BIT(6)
159 #define PSR_EP		BIT(5)
160 #define PSR_LEC_MASK	GENMASK(2, 0)
161 
162 /* Interrupt Register (IR) */
163 #define IR_ALL_INT	0xffffffff
164 
165 /* Renamed bits for versions > 3.1.x */
166 #define IR_ARA		BIT(29)
167 #define IR_PED		BIT(28)
168 #define IR_PEA		BIT(27)
169 
170 /* Bits for version 3.0.x */
171 #define IR_STE		BIT(31)
172 #define IR_FOE		BIT(30)
173 #define IR_ACKE		BIT(29)
174 #define IR_BE		BIT(28)
175 #define IR_CRCE		BIT(27)
176 #define IR_WDI		BIT(26)
177 #define IR_BO		BIT(25)
178 #define IR_EW		BIT(24)
179 #define IR_EP		BIT(23)
180 #define IR_ELO		BIT(22)
181 #define IR_BEU		BIT(21)
182 #define IR_BEC		BIT(20)
183 #define IR_DRX		BIT(19)
184 #define IR_TOO		BIT(18)
185 #define IR_MRAF		BIT(17)
186 #define IR_TSW		BIT(16)
187 #define IR_TEFL		BIT(15)
188 #define IR_TEFF		BIT(14)
189 #define IR_TEFW		BIT(13)
190 #define IR_TEFN		BIT(12)
191 #define IR_TFE		BIT(11)
192 #define IR_TCF		BIT(10)
193 #define IR_TC		BIT(9)
194 #define IR_HPM		BIT(8)
195 #define IR_RF1L		BIT(7)
196 #define IR_RF1F		BIT(6)
197 #define IR_RF1W		BIT(5)
198 #define IR_RF1N		BIT(4)
199 #define IR_RF0L		BIT(3)
200 #define IR_RF0F		BIT(2)
201 #define IR_RF0W		BIT(1)
202 #define IR_RF0N		BIT(0)
203 #define IR_ERR_STATE	(IR_BO | IR_EW | IR_EP)
204 
205 /* Interrupts for version 3.0.x */
206 #define IR_ERR_LEC_30X	(IR_STE	| IR_FOE | IR_ACKE | IR_BE | IR_CRCE)
207 #define IR_ERR_BUS_30X	(IR_ERR_LEC_30X | IR_WDI | IR_BEU | IR_BEC | \
208 			 IR_TOO | IR_MRAF | IR_TSW | IR_TEFL | IR_RF1L | \
209 			 IR_RF0L)
210 #define IR_ERR_ALL_30X	(IR_ERR_STATE | IR_ERR_BUS_30X)
211 
212 /* Interrupts for version >= 3.1.x */
213 #define IR_ERR_LEC_31X	(IR_PED | IR_PEA)
214 #define IR_ERR_BUS_31X      (IR_ERR_LEC_31X | IR_WDI | IR_BEU | IR_BEC | \
215 			 IR_TOO | IR_MRAF | IR_TSW | IR_TEFL | IR_RF1L | \
216 			 IR_RF0L)
217 #define IR_ERR_ALL_31X	(IR_ERR_STATE | IR_ERR_BUS_31X)
218 
219 /* Interrupt Line Select (ILS) */
220 #define ILS_ALL_INT0	0x0
221 #define ILS_ALL_INT1	0xFFFFFFFF
222 
223 /* Interrupt Line Enable (ILE) */
224 #define ILE_EINT1	BIT(1)
225 #define ILE_EINT0	BIT(0)
226 
227 /* Rx FIFO 0/1 Configuration (RXF0C/RXF1C) */
228 #define RXFC_FWM_MASK	GENMASK(30, 24)
229 #define RXFC_FS_MASK	GENMASK(22, 16)
230 
231 /* Rx FIFO 0/1 Status (RXF0S/RXF1S) */
232 #define RXFS_RFL	BIT(25)
233 #define RXFS_FF		BIT(24)
234 #define RXFS_FPI_MASK	GENMASK(21, 16)
235 #define RXFS_FGI_MASK	GENMASK(13, 8)
236 #define RXFS_FFL_MASK	GENMASK(6, 0)
237 
238 /* Rx Buffer / FIFO Element Size Configuration (RXESC) */
239 #define RXESC_RBDS_MASK		GENMASK(10, 8)
240 #define RXESC_F1DS_MASK		GENMASK(6, 4)
241 #define RXESC_F0DS_MASK		GENMASK(2, 0)
242 #define RXESC_64B		0x7
243 
244 /* Tx Buffer Configuration (TXBC) */
245 #define TXBC_TFQS_MASK		GENMASK(29, 24)
246 #define TXBC_NDTB_MASK		GENMASK(21, 16)
247 
248 /* Tx FIFO/Queue Status (TXFQS) */
249 #define TXFQS_TFQF		BIT(21)
250 #define TXFQS_TFQPI_MASK	GENMASK(20, 16)
251 #define TXFQS_TFGI_MASK		GENMASK(12, 8)
252 #define TXFQS_TFFL_MASK		GENMASK(5, 0)
253 
254 /* Tx Buffer Element Size Configuration (TXESC) */
255 #define TXESC_TBDS_MASK		GENMASK(2, 0)
256 #define TXESC_TBDS_64B		0x7
257 
258 /* Tx Event FIFO Configuration (TXEFC) */
259 #define TXEFC_EFS_MASK		GENMASK(21, 16)
260 
261 /* Tx Event FIFO Status (TXEFS) */
262 #define TXEFS_TEFL		BIT(25)
263 #define TXEFS_EFF		BIT(24)
264 #define TXEFS_EFGI_MASK		GENMASK(12, 8)
265 #define TXEFS_EFFL_MASK		GENMASK(5, 0)
266 
267 /* Tx Event FIFO Acknowledge (TXEFA) */
268 #define TXEFA_EFAI_MASK		GENMASK(4, 0)
269 
270 /* Message RAM Configuration (in bytes) */
271 #define SIDF_ELEMENT_SIZE	4
272 #define XIDF_ELEMENT_SIZE	8
273 #define RXF0_ELEMENT_SIZE	72
274 #define RXF1_ELEMENT_SIZE	72
275 #define RXB_ELEMENT_SIZE	72
276 #define TXE_ELEMENT_SIZE	8
277 #define TXB_ELEMENT_SIZE	72
278 
279 /* Message RAM Elements */
280 #define M_CAN_FIFO_ID		0x0
281 #define M_CAN_FIFO_DLC		0x4
282 #define M_CAN_FIFO_DATA		0x8
283 
284 /* Rx Buffer Element */
285 /* R0 */
286 #define RX_BUF_ESI		BIT(31)
287 #define RX_BUF_XTD		BIT(30)
288 #define RX_BUF_RTR		BIT(29)
289 /* R1 */
290 #define RX_BUF_ANMF		BIT(31)
291 #define RX_BUF_FDF		BIT(21)
292 #define RX_BUF_BRS		BIT(20)
293 #define RX_BUF_RXTS_MASK	GENMASK(15, 0)
294 
295 /* Tx Buffer Element */
296 /* T0 */
297 #define TX_BUF_ESI		BIT(31)
298 #define TX_BUF_XTD		BIT(30)
299 #define TX_BUF_RTR		BIT(29)
300 /* T1 */
301 #define TX_BUF_EFC		BIT(23)
302 #define TX_BUF_FDF		BIT(21)
303 #define TX_BUF_BRS		BIT(20)
304 #define TX_BUF_MM_MASK		GENMASK(31, 24)
305 #define TX_BUF_DLC_MASK		GENMASK(19, 16)
306 
307 /* Tx event FIFO Element */
308 /* E1 */
309 #define TX_EVENT_MM_MASK	GENMASK(31, 24)
310 #define TX_EVENT_TXTS_MASK	GENMASK(15, 0)
311 
312 /* The ID and DLC registers are adjacent in M_CAN FIFO memory,
313  * and we can save a (potentially slow) bus round trip by combining
314  * reads and writes to them.
315  */
316 struct id_and_dlc {
317 	u32 id;
318 	u32 dlc;
319 };
320 
321 static inline u32 m_can_read(struct m_can_classdev *cdev, enum m_can_reg reg)
322 {
323 	return cdev->ops->read_reg(cdev, reg);
324 }
325 
326 static inline void m_can_write(struct m_can_classdev *cdev, enum m_can_reg reg,
327 			       u32 val)
328 {
329 	cdev->ops->write_reg(cdev, reg, val);
330 }
331 
332 static int
333 m_can_fifo_read(struct m_can_classdev *cdev,
334 		u32 fgi, unsigned int offset, void *val, size_t val_count)
335 {
336 	u32 addr_offset = cdev->mcfg[MRAM_RXF0].off + fgi * RXF0_ELEMENT_SIZE +
337 		offset;
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 	return cdev->ops->write_fifo(cdev, addr_offset, val, val_count);
350 }
351 
352 static inline int m_can_fifo_write_no_off(struct m_can_classdev *cdev,
353 					  u32 fpi, u32 val)
354 {
355 	return cdev->ops->write_fifo(cdev, fpi, &val, 1);
356 }
357 
358 static int
359 m_can_txe_fifo_read(struct m_can_classdev *cdev, u32 fgi, u32 offset, u32 *val)
360 {
361 	u32 addr_offset = cdev->mcfg[MRAM_TXE].off + fgi * TXE_ELEMENT_SIZE +
362 		offset;
363 
364 	return cdev->ops->read_fifo(cdev, addr_offset, val, 1);
365 }
366 
367 static inline bool m_can_tx_fifo_full(struct m_can_classdev *cdev)
368 {
369 	return !!(m_can_read(cdev, M_CAN_TXFQS) & TXFQS_TFQF);
370 }
371 
372 static void m_can_config_endisable(struct m_can_classdev *cdev, bool enable)
373 {
374 	u32 cccr = m_can_read(cdev, M_CAN_CCCR);
375 	u32 timeout = 10;
376 	u32 val = 0;
377 
378 	/* Clear the Clock stop request if it was set */
379 	if (cccr & CCCR_CSR)
380 		cccr &= ~CCCR_CSR;
381 
382 	if (enable) {
383 		/* enable m_can configuration */
384 		m_can_write(cdev, M_CAN_CCCR, cccr | CCCR_INIT);
385 		udelay(5);
386 		/* CCCR.CCE can only be set/reset while CCCR.INIT = '1' */
387 		m_can_write(cdev, M_CAN_CCCR, cccr | CCCR_INIT | CCCR_CCE);
388 	} else {
389 		m_can_write(cdev, M_CAN_CCCR, cccr & ~(CCCR_INIT | CCCR_CCE));
390 	}
391 
392 	/* there's a delay for module initialization */
393 	if (enable)
394 		val = CCCR_INIT | CCCR_CCE;
395 
396 	while ((m_can_read(cdev, M_CAN_CCCR) & (CCCR_INIT | CCCR_CCE)) != val) {
397 		if (timeout == 0) {
398 			netdev_warn(cdev->net, "Failed to init module\n");
399 			return;
400 		}
401 		timeout--;
402 		udelay(1);
403 	}
404 }
405 
406 static inline void m_can_enable_all_interrupts(struct m_can_classdev *cdev)
407 {
408 	/* Only interrupt line 0 is used in this driver */
409 	m_can_write(cdev, M_CAN_ILE, ILE_EINT0);
410 }
411 
412 static inline void m_can_disable_all_interrupts(struct m_can_classdev *cdev)
413 {
414 	m_can_write(cdev, M_CAN_ILE, 0x0);
415 }
416 
417 /* Retrieve internal timestamp counter from TSCV.TSC, and shift it to 32-bit
418  * width.
419  */
420 static u32 m_can_get_timestamp(struct m_can_classdev *cdev)
421 {
422 	u32 tscv;
423 	u32 tsc;
424 
425 	tscv = m_can_read(cdev, M_CAN_TSCV);
426 	tsc = FIELD_GET(TSCV_TSC_MASK, tscv);
427 
428 	return (tsc << 16);
429 }
430 
431 static void m_can_clean(struct net_device *net)
432 {
433 	struct m_can_classdev *cdev = netdev_priv(net);
434 
435 	if (cdev->tx_skb) {
436 		int putidx = 0;
437 
438 		net->stats.tx_errors++;
439 		if (cdev->version > 30)
440 			putidx = FIELD_GET(TXFQS_TFQPI_MASK,
441 					   m_can_read(cdev, M_CAN_TXFQS));
442 
443 		can_free_echo_skb(cdev->net, putidx, NULL);
444 		cdev->tx_skb = NULL;
445 	}
446 }
447 
448 /* For peripherals, pass skb to rx-offload, which will push skb from
449  * napi. For non-peripherals, RX is done in napi already, so push
450  * directly. timestamp is used to ensure good skb ordering in
451  * rx-offload and is ignored for non-peripherals.
452  */
453 static void m_can_receive_skb(struct m_can_classdev *cdev,
454 			      struct sk_buff *skb,
455 			      u32 timestamp)
456 {
457 	if (cdev->is_peripheral) {
458 		struct net_device_stats *stats = &cdev->net->stats;
459 		int err;
460 
461 		err = can_rx_offload_queue_sorted(&cdev->offload, skb,
462 						  timestamp);
463 		if (err)
464 			stats->rx_fifo_errors++;
465 	} else {
466 		netif_receive_skb(skb);
467 	}
468 }
469 
470 static int m_can_read_fifo(struct net_device *dev, u32 rxfs)
471 {
472 	struct net_device_stats *stats = &dev->stats;
473 	struct m_can_classdev *cdev = netdev_priv(dev);
474 	struct canfd_frame *cf;
475 	struct sk_buff *skb;
476 	struct id_and_dlc fifo_header;
477 	u32 fgi;
478 	u32 timestamp = 0;
479 	int err;
480 
481 	/* calculate the fifo get index for where to read data */
482 	fgi = FIELD_GET(RXFS_FGI_MASK, rxfs);
483 	err = m_can_fifo_read(cdev, fgi, M_CAN_FIFO_ID, &fifo_header, 2);
484 	if (err)
485 		goto out_fail;
486 
487 	if (fifo_header.dlc & RX_BUF_FDF)
488 		skb = alloc_canfd_skb(dev, &cf);
489 	else
490 		skb = alloc_can_skb(dev, (struct can_frame **)&cf);
491 	if (!skb) {
492 		stats->rx_dropped++;
493 		return 0;
494 	}
495 
496 	if (fifo_header.dlc & RX_BUF_FDF)
497 		cf->len = can_fd_dlc2len((fifo_header.dlc >> 16) & 0x0F);
498 	else
499 		cf->len = can_cc_dlc2len((fifo_header.dlc >> 16) & 0x0F);
500 
501 	if (fifo_header.id & RX_BUF_XTD)
502 		cf->can_id = (fifo_header.id & CAN_EFF_MASK) | CAN_EFF_FLAG;
503 	else
504 		cf->can_id = (fifo_header.id >> 18) & CAN_SFF_MASK;
505 
506 	if (fifo_header.id & RX_BUF_ESI) {
507 		cf->flags |= CANFD_ESI;
508 		netdev_dbg(dev, "ESI Error\n");
509 	}
510 
511 	if (!(fifo_header.dlc & RX_BUF_FDF) && (fifo_header.id & RX_BUF_RTR)) {
512 		cf->can_id |= CAN_RTR_FLAG;
513 	} else {
514 		if (fifo_header.dlc & RX_BUF_BRS)
515 			cf->flags |= CANFD_BRS;
516 
517 		err = m_can_fifo_read(cdev, fgi, M_CAN_FIFO_DATA,
518 				      cf->data, DIV_ROUND_UP(cf->len, 4));
519 		if (err)
520 			goto out_free_skb;
521 
522 		stats->rx_bytes += cf->len;
523 	}
524 	stats->rx_packets++;
525 
526 	/* acknowledge rx fifo 0 */
527 	m_can_write(cdev, M_CAN_RXF0A, fgi);
528 
529 	timestamp = FIELD_GET(RX_BUF_RXTS_MASK, fifo_header.dlc);
530 
531 	m_can_receive_skb(cdev, skb, timestamp);
532 
533 	return 0;
534 
535 out_free_skb:
536 	kfree_skb(skb);
537 out_fail:
538 	netdev_err(dev, "FIFO read returned %d\n", err);
539 	return err;
540 }
541 
542 static int m_can_do_rx_poll(struct net_device *dev, int quota)
543 {
544 	struct m_can_classdev *cdev = netdev_priv(dev);
545 	u32 pkts = 0;
546 	u32 rxfs;
547 	int err;
548 
549 	rxfs = m_can_read(cdev, M_CAN_RXF0S);
550 	if (!(rxfs & RXFS_FFL_MASK)) {
551 		netdev_dbg(dev, "no messages in fifo0\n");
552 		return 0;
553 	}
554 
555 	while ((rxfs & RXFS_FFL_MASK) && (quota > 0)) {
556 		err = m_can_read_fifo(dev, rxfs);
557 		if (err)
558 			return err;
559 
560 		quota--;
561 		pkts++;
562 		rxfs = m_can_read(cdev, M_CAN_RXF0S);
563 	}
564 
565 	if (pkts)
566 		can_led_event(dev, CAN_LED_EVENT_RX);
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, M_CAN_NAPI_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 
1088 			can_led_event(dev, CAN_LED_EVENT_TX);
1089 			netif_wake_queue(dev);
1090 		}
1091 	} else  {
1092 		if (ir & IR_TEFN) {
1093 			/* New TX FIFO Element arrived */
1094 			if (m_can_echo_tx_event(dev) != 0)
1095 				goto out_fail;
1096 
1097 			can_led_event(dev, CAN_LED_EVENT_TX);
1098 			if (netif_queue_stopped(dev) &&
1099 			    !m_can_tx_fifo_full(cdev))
1100 				netif_wake_queue(dev);
1101 		}
1102 	}
1103 
1104 	if (cdev->is_peripheral)
1105 		can_rx_offload_threaded_irq_finish(&cdev->offload);
1106 
1107 	return IRQ_HANDLED;
1108 
1109 out_fail:
1110 	m_can_disable_all_interrupts(cdev);
1111 	return IRQ_HANDLED;
1112 }
1113 
1114 static const struct can_bittiming_const m_can_bittiming_const_30X = {
1115 	.name = KBUILD_MODNAME,
1116 	.tseg1_min = 2,		/* Time segment 1 = prop_seg + phase_seg1 */
1117 	.tseg1_max = 64,
1118 	.tseg2_min = 1,		/* Time segment 2 = phase_seg2 */
1119 	.tseg2_max = 16,
1120 	.sjw_max = 16,
1121 	.brp_min = 1,
1122 	.brp_max = 1024,
1123 	.brp_inc = 1,
1124 };
1125 
1126 static const struct can_bittiming_const m_can_data_bittiming_const_30X = {
1127 	.name = KBUILD_MODNAME,
1128 	.tseg1_min = 2,		/* Time segment 1 = prop_seg + phase_seg1 */
1129 	.tseg1_max = 16,
1130 	.tseg2_min = 1,		/* Time segment 2 = phase_seg2 */
1131 	.tseg2_max = 8,
1132 	.sjw_max = 4,
1133 	.brp_min = 1,
1134 	.brp_max = 32,
1135 	.brp_inc = 1,
1136 };
1137 
1138 static const struct can_bittiming_const m_can_bittiming_const_31X = {
1139 	.name = KBUILD_MODNAME,
1140 	.tseg1_min = 2,		/* Time segment 1 = prop_seg + phase_seg1 */
1141 	.tseg1_max = 256,
1142 	.tseg2_min = 2,		/* Time segment 2 = phase_seg2 */
1143 	.tseg2_max = 128,
1144 	.sjw_max = 128,
1145 	.brp_min = 1,
1146 	.brp_max = 512,
1147 	.brp_inc = 1,
1148 };
1149 
1150 static const struct can_bittiming_const m_can_data_bittiming_const_31X = {
1151 	.name = KBUILD_MODNAME,
1152 	.tseg1_min = 1,		/* Time segment 1 = prop_seg + phase_seg1 */
1153 	.tseg1_max = 32,
1154 	.tseg2_min = 1,		/* Time segment 2 = phase_seg2 */
1155 	.tseg2_max = 16,
1156 	.sjw_max = 16,
1157 	.brp_min = 1,
1158 	.brp_max = 32,
1159 	.brp_inc = 1,
1160 };
1161 
1162 static int m_can_set_bittiming(struct net_device *dev)
1163 {
1164 	struct m_can_classdev *cdev = netdev_priv(dev);
1165 	const struct can_bittiming *bt = &cdev->can.bittiming;
1166 	const struct can_bittiming *dbt = &cdev->can.data_bittiming;
1167 	u16 brp, sjw, tseg1, tseg2;
1168 	u32 reg_btp;
1169 
1170 	brp = bt->brp - 1;
1171 	sjw = bt->sjw - 1;
1172 	tseg1 = bt->prop_seg + bt->phase_seg1 - 1;
1173 	tseg2 = bt->phase_seg2 - 1;
1174 	reg_btp = FIELD_PREP(NBTP_NBRP_MASK, brp) |
1175 		  FIELD_PREP(NBTP_NSJW_MASK, sjw) |
1176 		  FIELD_PREP(NBTP_NTSEG1_MASK, tseg1) |
1177 		  FIELD_PREP(NBTP_NTSEG2_MASK, tseg2);
1178 	m_can_write(cdev, M_CAN_NBTP, reg_btp);
1179 
1180 	if (cdev->can.ctrlmode & CAN_CTRLMODE_FD) {
1181 		reg_btp = 0;
1182 		brp = dbt->brp - 1;
1183 		sjw = dbt->sjw - 1;
1184 		tseg1 = dbt->prop_seg + dbt->phase_seg1 - 1;
1185 		tseg2 = dbt->phase_seg2 - 1;
1186 
1187 		/* TDC is only needed for bitrates beyond 2.5 MBit/s.
1188 		 * This is mentioned in the "Bit Time Requirements for CAN FD"
1189 		 * paper presented at the International CAN Conference 2013
1190 		 */
1191 		if (dbt->bitrate > 2500000) {
1192 			u32 tdco, ssp;
1193 
1194 			/* Use the same value of secondary sampling point
1195 			 * as the data sampling point
1196 			 */
1197 			ssp = dbt->sample_point;
1198 
1199 			/* Equation based on Bosch's M_CAN User Manual's
1200 			 * Transmitter Delay Compensation Section
1201 			 */
1202 			tdco = (cdev->can.clock.freq / 1000) *
1203 				ssp / dbt->bitrate;
1204 
1205 			/* Max valid TDCO value is 127 */
1206 			if (tdco > 127) {
1207 				netdev_warn(dev, "TDCO value of %u is beyond maximum. Using maximum possible value\n",
1208 					    tdco);
1209 				tdco = 127;
1210 			}
1211 
1212 			reg_btp |= DBTP_TDC;
1213 			m_can_write(cdev, M_CAN_TDCR,
1214 				    FIELD_PREP(TDCR_TDCO_MASK, tdco));
1215 		}
1216 
1217 		reg_btp |= FIELD_PREP(DBTP_DBRP_MASK, brp) |
1218 			FIELD_PREP(DBTP_DSJW_MASK, sjw) |
1219 			FIELD_PREP(DBTP_DTSEG1_MASK, tseg1) |
1220 			FIELD_PREP(DBTP_DTSEG2_MASK, tseg2);
1221 
1222 		m_can_write(cdev, M_CAN_DBTP, reg_btp);
1223 	}
1224 
1225 	return 0;
1226 }
1227 
1228 /* Configure M_CAN chip:
1229  * - set rx buffer/fifo element size
1230  * - configure rx fifo
1231  * - accept non-matching frame into fifo 0
1232  * - configure tx buffer
1233  *		- >= v3.1.x: TX FIFO is used
1234  * - configure mode
1235  * - setup bittiming
1236  * - configure timestamp generation
1237  */
1238 static void m_can_chip_config(struct net_device *dev)
1239 {
1240 	struct m_can_classdev *cdev = netdev_priv(dev);
1241 	u32 cccr, test;
1242 
1243 	m_can_config_endisable(cdev, true);
1244 
1245 	/* RX Buffer/FIFO Element Size 64 bytes data field */
1246 	m_can_write(cdev, M_CAN_RXESC,
1247 		    FIELD_PREP(RXESC_RBDS_MASK, RXESC_64B) |
1248 		    FIELD_PREP(RXESC_F1DS_MASK, RXESC_64B) |
1249 		    FIELD_PREP(RXESC_F0DS_MASK, RXESC_64B));
1250 
1251 	/* Accept Non-matching Frames Into FIFO 0 */
1252 	m_can_write(cdev, M_CAN_GFC, 0x0);
1253 
1254 	if (cdev->version == 30) {
1255 		/* only support one Tx Buffer currently */
1256 		m_can_write(cdev, M_CAN_TXBC, FIELD_PREP(TXBC_NDTB_MASK, 1) |
1257 			    cdev->mcfg[MRAM_TXB].off);
1258 	} else {
1259 		/* TX FIFO is used for newer IP Core versions */
1260 		m_can_write(cdev, M_CAN_TXBC,
1261 			    FIELD_PREP(TXBC_TFQS_MASK,
1262 				       cdev->mcfg[MRAM_TXB].num) |
1263 			    cdev->mcfg[MRAM_TXB].off);
1264 	}
1265 
1266 	/* support 64 bytes payload */
1267 	m_can_write(cdev, M_CAN_TXESC,
1268 		    FIELD_PREP(TXESC_TBDS_MASK, TXESC_TBDS_64B));
1269 
1270 	/* TX Event FIFO */
1271 	if (cdev->version == 30) {
1272 		m_can_write(cdev, M_CAN_TXEFC,
1273 			    FIELD_PREP(TXEFC_EFS_MASK, 1) |
1274 			    cdev->mcfg[MRAM_TXE].off);
1275 	} else {
1276 		/* Full TX Event FIFO is used */
1277 		m_can_write(cdev, M_CAN_TXEFC,
1278 			    FIELD_PREP(TXEFC_EFS_MASK,
1279 				       cdev->mcfg[MRAM_TXE].num) |
1280 			    cdev->mcfg[MRAM_TXE].off);
1281 	}
1282 
1283 	/* rx fifo configuration, blocking mode, fifo size 1 */
1284 	m_can_write(cdev, M_CAN_RXF0C,
1285 		    FIELD_PREP(RXFC_FS_MASK, cdev->mcfg[MRAM_RXF0].num) |
1286 		    cdev->mcfg[MRAM_RXF0].off);
1287 
1288 	m_can_write(cdev, M_CAN_RXF1C,
1289 		    FIELD_PREP(RXFC_FS_MASK, cdev->mcfg[MRAM_RXF1].num) |
1290 		    cdev->mcfg[MRAM_RXF1].off);
1291 
1292 	cccr = m_can_read(cdev, M_CAN_CCCR);
1293 	test = m_can_read(cdev, M_CAN_TEST);
1294 	test &= ~TEST_LBCK;
1295 	if (cdev->version == 30) {
1296 		/* Version 3.0.x */
1297 
1298 		cccr &= ~(CCCR_TEST | CCCR_MON | CCCR_DAR |
1299 			  FIELD_PREP(CCCR_CMR_MASK, FIELD_MAX(CCCR_CMR_MASK)) |
1300 			  FIELD_PREP(CCCR_CME_MASK, FIELD_MAX(CCCR_CME_MASK)));
1301 
1302 		if (cdev->can.ctrlmode & CAN_CTRLMODE_FD)
1303 			cccr |= FIELD_PREP(CCCR_CME_MASK, CCCR_CME_CANFD_BRS);
1304 
1305 	} else {
1306 		/* Version 3.1.x or 3.2.x */
1307 		cccr &= ~(CCCR_TEST | CCCR_MON | CCCR_BRSE | CCCR_FDOE |
1308 			  CCCR_NISO | CCCR_DAR);
1309 
1310 		/* Only 3.2.x has NISO Bit implemented */
1311 		if (cdev->can.ctrlmode & CAN_CTRLMODE_FD_NON_ISO)
1312 			cccr |= CCCR_NISO;
1313 
1314 		if (cdev->can.ctrlmode & CAN_CTRLMODE_FD)
1315 			cccr |= (CCCR_BRSE | CCCR_FDOE);
1316 	}
1317 
1318 	/* Loopback Mode */
1319 	if (cdev->can.ctrlmode & CAN_CTRLMODE_LOOPBACK) {
1320 		cccr |= CCCR_TEST | CCCR_MON;
1321 		test |= TEST_LBCK;
1322 	}
1323 
1324 	/* Enable Monitoring (all versions) */
1325 	if (cdev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
1326 		cccr |= CCCR_MON;
1327 
1328 	/* Disable Auto Retransmission (all versions) */
1329 	if (cdev->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT)
1330 		cccr |= CCCR_DAR;
1331 
1332 	/* Write config */
1333 	m_can_write(cdev, M_CAN_CCCR, cccr);
1334 	m_can_write(cdev, M_CAN_TEST, test);
1335 
1336 	/* Enable interrupts */
1337 	m_can_write(cdev, M_CAN_IR, IR_ALL_INT);
1338 	if (!(cdev->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING))
1339 		if (cdev->version == 30)
1340 			m_can_write(cdev, M_CAN_IE, IR_ALL_INT &
1341 				    ~(IR_ERR_LEC_30X));
1342 		else
1343 			m_can_write(cdev, M_CAN_IE, IR_ALL_INT &
1344 				    ~(IR_ERR_LEC_31X));
1345 	else
1346 		m_can_write(cdev, M_CAN_IE, IR_ALL_INT);
1347 
1348 	/* route all interrupts to INT0 */
1349 	m_can_write(cdev, M_CAN_ILS, ILS_ALL_INT0);
1350 
1351 	/* set bittiming params */
1352 	m_can_set_bittiming(dev);
1353 
1354 	/* enable internal timestamp generation, with a prescalar of 16. The
1355 	 * prescalar is applied to the nominal bit timing
1356 	 */
1357 	m_can_write(cdev, M_CAN_TSCC, FIELD_PREP(TSCC_TCP_MASK, 0xf));
1358 
1359 	m_can_config_endisable(cdev, false);
1360 
1361 	if (cdev->ops->init)
1362 		cdev->ops->init(cdev);
1363 }
1364 
1365 static void m_can_start(struct net_device *dev)
1366 {
1367 	struct m_can_classdev *cdev = netdev_priv(dev);
1368 
1369 	/* basic m_can configuration */
1370 	m_can_chip_config(dev);
1371 
1372 	cdev->can.state = CAN_STATE_ERROR_ACTIVE;
1373 
1374 	m_can_enable_all_interrupts(cdev);
1375 }
1376 
1377 static int m_can_set_mode(struct net_device *dev, enum can_mode mode)
1378 {
1379 	switch (mode) {
1380 	case CAN_MODE_START:
1381 		m_can_clean(dev);
1382 		m_can_start(dev);
1383 		netif_wake_queue(dev);
1384 		break;
1385 	default:
1386 		return -EOPNOTSUPP;
1387 	}
1388 
1389 	return 0;
1390 }
1391 
1392 /* Checks core release number of M_CAN
1393  * returns 0 if an unsupported device is detected
1394  * else it returns the release and step coded as:
1395  * return value = 10 * <release> + 1 * <step>
1396  */
1397 static int m_can_check_core_release(struct m_can_classdev *cdev)
1398 {
1399 	u32 crel_reg;
1400 	u8 rel;
1401 	u8 step;
1402 	int res;
1403 
1404 	/* Read Core Release Version and split into version number
1405 	 * Example: Version 3.2.1 => rel = 3; step = 2; substep = 1;
1406 	 */
1407 	crel_reg = m_can_read(cdev, M_CAN_CREL);
1408 	rel = (u8)FIELD_GET(CREL_REL_MASK, crel_reg);
1409 	step = (u8)FIELD_GET(CREL_STEP_MASK, crel_reg);
1410 
1411 	if (rel == 3) {
1412 		/* M_CAN v3.x.y: create return value */
1413 		res = 30 + step;
1414 	} else {
1415 		/* Unsupported M_CAN version */
1416 		res = 0;
1417 	}
1418 
1419 	return res;
1420 }
1421 
1422 /* Selectable Non ISO support only in version 3.2.x
1423  * This function checks if the bit is writable.
1424  */
1425 static bool m_can_niso_supported(struct m_can_classdev *cdev)
1426 {
1427 	u32 cccr_reg, cccr_poll = 0;
1428 	int niso_timeout = -ETIMEDOUT;
1429 	int i;
1430 
1431 	m_can_config_endisable(cdev, true);
1432 	cccr_reg = m_can_read(cdev, M_CAN_CCCR);
1433 	cccr_reg |= CCCR_NISO;
1434 	m_can_write(cdev, M_CAN_CCCR, cccr_reg);
1435 
1436 	for (i = 0; i <= 10; i++) {
1437 		cccr_poll = m_can_read(cdev, M_CAN_CCCR);
1438 		if (cccr_poll == cccr_reg) {
1439 			niso_timeout = 0;
1440 			break;
1441 		}
1442 
1443 		usleep_range(1, 5);
1444 	}
1445 
1446 	/* Clear NISO */
1447 	cccr_reg &= ~(CCCR_NISO);
1448 	m_can_write(cdev, M_CAN_CCCR, cccr_reg);
1449 
1450 	m_can_config_endisable(cdev, false);
1451 
1452 	/* return false if time out (-ETIMEDOUT), else return true */
1453 	return !niso_timeout;
1454 }
1455 
1456 static int m_can_dev_setup(struct m_can_classdev *cdev)
1457 {
1458 	struct net_device *dev = cdev->net;
1459 	int m_can_version, err;
1460 
1461 	m_can_version = m_can_check_core_release(cdev);
1462 	/* return if unsupported version */
1463 	if (!m_can_version) {
1464 		dev_err(cdev->dev, "Unsupported version number: %2d",
1465 			m_can_version);
1466 		return -EINVAL;
1467 	}
1468 
1469 	if (!cdev->is_peripheral)
1470 		netif_napi_add(dev, &cdev->napi,
1471 			       m_can_poll, M_CAN_NAPI_WEIGHT);
1472 
1473 	/* Shared properties of all M_CAN versions */
1474 	cdev->version = m_can_version;
1475 	cdev->can.do_set_mode = m_can_set_mode;
1476 	cdev->can.do_get_berr_counter = m_can_get_berr_counter;
1477 
1478 	/* Set M_CAN supported operations */
1479 	cdev->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK |
1480 		CAN_CTRLMODE_LISTENONLY |
1481 		CAN_CTRLMODE_BERR_REPORTING |
1482 		CAN_CTRLMODE_FD |
1483 		CAN_CTRLMODE_ONE_SHOT;
1484 
1485 	/* Set properties depending on M_CAN version */
1486 	switch (cdev->version) {
1487 	case 30:
1488 		/* CAN_CTRLMODE_FD_NON_ISO is fixed with M_CAN IP v3.0.x */
1489 		err = can_set_static_ctrlmode(dev, CAN_CTRLMODE_FD_NON_ISO);
1490 		if (err)
1491 			return err;
1492 		cdev->can.bittiming_const = cdev->bit_timing ?
1493 			cdev->bit_timing : &m_can_bittiming_const_30X;
1494 
1495 		cdev->can.data_bittiming_const = cdev->data_timing ?
1496 			cdev->data_timing :
1497 			&m_can_data_bittiming_const_30X;
1498 		break;
1499 	case 31:
1500 		/* CAN_CTRLMODE_FD_NON_ISO is fixed with M_CAN IP v3.1.x */
1501 		err = can_set_static_ctrlmode(dev, CAN_CTRLMODE_FD_NON_ISO);
1502 		if (err)
1503 			return err;
1504 		cdev->can.bittiming_const = cdev->bit_timing ?
1505 			cdev->bit_timing : &m_can_bittiming_const_31X;
1506 
1507 		cdev->can.data_bittiming_const = cdev->data_timing ?
1508 			cdev->data_timing :
1509 			&m_can_data_bittiming_const_31X;
1510 		break;
1511 	case 32:
1512 	case 33:
1513 		/* Support both MCAN version v3.2.x and v3.3.0 */
1514 		cdev->can.bittiming_const = cdev->bit_timing ?
1515 			cdev->bit_timing : &m_can_bittiming_const_31X;
1516 
1517 		cdev->can.data_bittiming_const = cdev->data_timing ?
1518 			cdev->data_timing :
1519 			&m_can_data_bittiming_const_31X;
1520 
1521 		cdev->can.ctrlmode_supported |=
1522 			(m_can_niso_supported(cdev) ?
1523 			 CAN_CTRLMODE_FD_NON_ISO : 0);
1524 		break;
1525 	default:
1526 		dev_err(cdev->dev, "Unsupported version number: %2d",
1527 			cdev->version);
1528 		return -EINVAL;
1529 	}
1530 
1531 	if (cdev->ops->init)
1532 		cdev->ops->init(cdev);
1533 
1534 	return 0;
1535 }
1536 
1537 static void m_can_stop(struct net_device *dev)
1538 {
1539 	struct m_can_classdev *cdev = netdev_priv(dev);
1540 
1541 	/* disable all interrupts */
1542 	m_can_disable_all_interrupts(cdev);
1543 
1544 	/* Set init mode to disengage from the network */
1545 	m_can_config_endisable(cdev, true);
1546 
1547 	/* set the state as STOPPED */
1548 	cdev->can.state = CAN_STATE_STOPPED;
1549 }
1550 
1551 static int m_can_close(struct net_device *dev)
1552 {
1553 	struct m_can_classdev *cdev = netdev_priv(dev);
1554 
1555 	netif_stop_queue(dev);
1556 
1557 	if (!cdev->is_peripheral)
1558 		napi_disable(&cdev->napi);
1559 
1560 	m_can_stop(dev);
1561 	m_can_clk_stop(cdev);
1562 	free_irq(dev->irq, dev);
1563 
1564 	if (cdev->is_peripheral) {
1565 		cdev->tx_skb = NULL;
1566 		destroy_workqueue(cdev->tx_wq);
1567 		cdev->tx_wq = NULL;
1568 	}
1569 
1570 	if (cdev->is_peripheral)
1571 		can_rx_offload_disable(&cdev->offload);
1572 
1573 	close_candev(dev);
1574 	can_led_event(dev, CAN_LED_EVENT_STOP);
1575 
1576 	phy_power_off(cdev->transceiver);
1577 
1578 	return 0;
1579 }
1580 
1581 static int m_can_next_echo_skb_occupied(struct net_device *dev, int putidx)
1582 {
1583 	struct m_can_classdev *cdev = netdev_priv(dev);
1584 	/*get wrap around for loopback skb index */
1585 	unsigned int wrap = cdev->can.echo_skb_max;
1586 	int next_idx;
1587 
1588 	/* calculate next index */
1589 	next_idx = (++putidx >= wrap ? 0 : putidx);
1590 
1591 	/* check if occupied */
1592 	return !!cdev->can.echo_skb[next_idx];
1593 }
1594 
1595 static netdev_tx_t m_can_tx_handler(struct m_can_classdev *cdev)
1596 {
1597 	struct canfd_frame *cf = (struct canfd_frame *)cdev->tx_skb->data;
1598 	struct net_device *dev = cdev->net;
1599 	struct sk_buff *skb = cdev->tx_skb;
1600 	struct id_and_dlc fifo_header;
1601 	u32 cccr, fdflags;
1602 	int err;
1603 	int putidx;
1604 
1605 	cdev->tx_skb = NULL;
1606 
1607 	/* Generate ID field for TX buffer Element */
1608 	/* Common to all supported M_CAN versions */
1609 	if (cf->can_id & CAN_EFF_FLAG) {
1610 		fifo_header.id = cf->can_id & CAN_EFF_MASK;
1611 		fifo_header.id |= TX_BUF_XTD;
1612 	} else {
1613 		fifo_header.id = ((cf->can_id & CAN_SFF_MASK) << 18);
1614 	}
1615 
1616 	if (cf->can_id & CAN_RTR_FLAG)
1617 		fifo_header.id |= TX_BUF_RTR;
1618 
1619 	if (cdev->version == 30) {
1620 		netif_stop_queue(dev);
1621 
1622 		fifo_header.dlc = can_fd_len2dlc(cf->len) << 16;
1623 
1624 		/* Write the frame ID, DLC, and payload to the FIFO element. */
1625 		err = m_can_fifo_write(cdev, 0, M_CAN_FIFO_ID, &fifo_header, 2);
1626 		if (err)
1627 			goto out_fail;
1628 
1629 		err = m_can_fifo_write(cdev, 0, M_CAN_FIFO_DATA,
1630 				       cf->data, DIV_ROUND_UP(cf->len, 4));
1631 		if (err)
1632 			goto out_fail;
1633 
1634 		can_put_echo_skb(skb, dev, 0, 0);
1635 
1636 		if (cdev->can.ctrlmode & CAN_CTRLMODE_FD) {
1637 			cccr = m_can_read(cdev, M_CAN_CCCR);
1638 			cccr &= ~CCCR_CMR_MASK;
1639 			if (can_is_canfd_skb(skb)) {
1640 				if (cf->flags & CANFD_BRS)
1641 					cccr |= FIELD_PREP(CCCR_CMR_MASK,
1642 							   CCCR_CMR_CANFD_BRS);
1643 				else
1644 					cccr |= FIELD_PREP(CCCR_CMR_MASK,
1645 							   CCCR_CMR_CANFD);
1646 			} else {
1647 				cccr |= FIELD_PREP(CCCR_CMR_MASK, CCCR_CMR_CAN);
1648 			}
1649 			m_can_write(cdev, M_CAN_CCCR, cccr);
1650 		}
1651 		m_can_write(cdev, M_CAN_TXBTIE, 0x1);
1652 		m_can_write(cdev, M_CAN_TXBAR, 0x1);
1653 		/* End of xmit function for version 3.0.x */
1654 	} else {
1655 		/* Transmit routine for version >= v3.1.x */
1656 
1657 		/* Check if FIFO full */
1658 		if (m_can_tx_fifo_full(cdev)) {
1659 			/* This shouldn't happen */
1660 			netif_stop_queue(dev);
1661 			netdev_warn(dev,
1662 				    "TX queue active although FIFO is full.");
1663 
1664 			if (cdev->is_peripheral) {
1665 				kfree_skb(skb);
1666 				dev->stats.tx_dropped++;
1667 				return NETDEV_TX_OK;
1668 			} else {
1669 				return NETDEV_TX_BUSY;
1670 			}
1671 		}
1672 
1673 		/* get put index for frame */
1674 		putidx = FIELD_GET(TXFQS_TFQPI_MASK,
1675 				   m_can_read(cdev, M_CAN_TXFQS));
1676 
1677 		/* Construct DLC Field, with CAN-FD configuration.
1678 		 * Use the put index of the fifo as the message marker,
1679 		 * used in the TX interrupt for sending the correct echo frame.
1680 		 */
1681 
1682 		/* get CAN FD configuration of frame */
1683 		fdflags = 0;
1684 		if (can_is_canfd_skb(skb)) {
1685 			fdflags |= TX_BUF_FDF;
1686 			if (cf->flags & CANFD_BRS)
1687 				fdflags |= TX_BUF_BRS;
1688 		}
1689 
1690 		fifo_header.dlc = FIELD_PREP(TX_BUF_MM_MASK, putidx) |
1691 			FIELD_PREP(TX_BUF_DLC_MASK, can_fd_len2dlc(cf->len)) |
1692 			fdflags | TX_BUF_EFC;
1693 		err = m_can_fifo_write(cdev, putidx, M_CAN_FIFO_ID, &fifo_header, 2);
1694 		if (err)
1695 			goto out_fail;
1696 
1697 		err = m_can_fifo_write(cdev, putidx, M_CAN_FIFO_DATA,
1698 				       cf->data, DIV_ROUND_UP(cf->len, 4));
1699 		if (err)
1700 			goto out_fail;
1701 
1702 		/* Push loopback echo.
1703 		 * Will be looped back on TX interrupt based on message marker
1704 		 */
1705 		can_put_echo_skb(skb, dev, putidx, 0);
1706 
1707 		/* Enable TX FIFO element to start transfer  */
1708 		m_can_write(cdev, M_CAN_TXBAR, (1 << putidx));
1709 
1710 		/* stop network queue if fifo full */
1711 		if (m_can_tx_fifo_full(cdev) ||
1712 		    m_can_next_echo_skb_occupied(dev, putidx))
1713 			netif_stop_queue(dev);
1714 	}
1715 
1716 	return NETDEV_TX_OK;
1717 
1718 out_fail:
1719 	netdev_err(dev, "FIFO write returned %d\n", err);
1720 	m_can_disable_all_interrupts(cdev);
1721 	return NETDEV_TX_BUSY;
1722 }
1723 
1724 static void m_can_tx_work_queue(struct work_struct *ws)
1725 {
1726 	struct m_can_classdev *cdev = container_of(ws, struct m_can_classdev,
1727 						   tx_work);
1728 
1729 	m_can_tx_handler(cdev);
1730 }
1731 
1732 static netdev_tx_t m_can_start_xmit(struct sk_buff *skb,
1733 				    struct net_device *dev)
1734 {
1735 	struct m_can_classdev *cdev = netdev_priv(dev);
1736 
1737 	if (can_dropped_invalid_skb(dev, skb))
1738 		return NETDEV_TX_OK;
1739 
1740 	if (cdev->is_peripheral) {
1741 		if (cdev->tx_skb) {
1742 			netdev_err(dev, "hard_xmit called while tx busy\n");
1743 			return NETDEV_TX_BUSY;
1744 		}
1745 
1746 		if (cdev->can.state == CAN_STATE_BUS_OFF) {
1747 			m_can_clean(dev);
1748 		} else {
1749 			/* Need to stop the queue to avoid numerous requests
1750 			 * from being sent.  Suggested improvement is to create
1751 			 * a queueing mechanism that will queue the skbs and
1752 			 * process them in order.
1753 			 */
1754 			cdev->tx_skb = skb;
1755 			netif_stop_queue(cdev->net);
1756 			queue_work(cdev->tx_wq, &cdev->tx_work);
1757 		}
1758 	} else {
1759 		cdev->tx_skb = skb;
1760 		return m_can_tx_handler(cdev);
1761 	}
1762 
1763 	return NETDEV_TX_OK;
1764 }
1765 
1766 static int m_can_open(struct net_device *dev)
1767 {
1768 	struct m_can_classdev *cdev = netdev_priv(dev);
1769 	int err;
1770 
1771 	err = phy_power_on(cdev->transceiver);
1772 	if (err)
1773 		return err;
1774 
1775 	err = m_can_clk_start(cdev);
1776 	if (err)
1777 		goto out_phy_power_off;
1778 
1779 	/* open the can device */
1780 	err = open_candev(dev);
1781 	if (err) {
1782 		netdev_err(dev, "failed to open can device\n");
1783 		goto exit_disable_clks;
1784 	}
1785 
1786 	if (cdev->is_peripheral)
1787 		can_rx_offload_enable(&cdev->offload);
1788 
1789 	/* register interrupt handler */
1790 	if (cdev->is_peripheral) {
1791 		cdev->tx_skb = NULL;
1792 		cdev->tx_wq = alloc_workqueue("mcan_wq",
1793 					      WQ_FREEZABLE | WQ_MEM_RECLAIM, 0);
1794 		if (!cdev->tx_wq) {
1795 			err = -ENOMEM;
1796 			goto out_wq_fail;
1797 		}
1798 
1799 		INIT_WORK(&cdev->tx_work, m_can_tx_work_queue);
1800 
1801 		err = request_threaded_irq(dev->irq, NULL, m_can_isr,
1802 					   IRQF_ONESHOT,
1803 					   dev->name, dev);
1804 	} else {
1805 		err = request_irq(dev->irq, m_can_isr, IRQF_SHARED, dev->name,
1806 				  dev);
1807 	}
1808 
1809 	if (err < 0) {
1810 		netdev_err(dev, "failed to request interrupt\n");
1811 		goto exit_irq_fail;
1812 	}
1813 
1814 	/* start the m_can controller */
1815 	m_can_start(dev);
1816 
1817 	can_led_event(dev, CAN_LED_EVENT_OPEN);
1818 
1819 	if (!cdev->is_peripheral)
1820 		napi_enable(&cdev->napi);
1821 
1822 	netif_start_queue(dev);
1823 
1824 	return 0;
1825 
1826 exit_irq_fail:
1827 	if (cdev->is_peripheral)
1828 		destroy_workqueue(cdev->tx_wq);
1829 out_wq_fail:
1830 	if (cdev->is_peripheral)
1831 		can_rx_offload_disable(&cdev->offload);
1832 	close_candev(dev);
1833 exit_disable_clks:
1834 	m_can_clk_stop(cdev);
1835 out_phy_power_off:
1836 	phy_power_off(cdev->transceiver);
1837 	return err;
1838 }
1839 
1840 static const struct net_device_ops m_can_netdev_ops = {
1841 	.ndo_open = m_can_open,
1842 	.ndo_stop = m_can_close,
1843 	.ndo_start_xmit = m_can_start_xmit,
1844 	.ndo_change_mtu = can_change_mtu,
1845 };
1846 
1847 static int register_m_can_dev(struct net_device *dev)
1848 {
1849 	dev->flags |= IFF_ECHO;	/* we support local echo */
1850 	dev->netdev_ops = &m_can_netdev_ops;
1851 
1852 	return register_candev(dev);
1853 }
1854 
1855 static void m_can_of_parse_mram(struct m_can_classdev *cdev,
1856 				const u32 *mram_config_vals)
1857 {
1858 	cdev->mcfg[MRAM_SIDF].off = mram_config_vals[0];
1859 	cdev->mcfg[MRAM_SIDF].num = mram_config_vals[1];
1860 	cdev->mcfg[MRAM_XIDF].off = cdev->mcfg[MRAM_SIDF].off +
1861 		cdev->mcfg[MRAM_SIDF].num * SIDF_ELEMENT_SIZE;
1862 	cdev->mcfg[MRAM_XIDF].num = mram_config_vals[2];
1863 	cdev->mcfg[MRAM_RXF0].off = cdev->mcfg[MRAM_XIDF].off +
1864 		cdev->mcfg[MRAM_XIDF].num * XIDF_ELEMENT_SIZE;
1865 	cdev->mcfg[MRAM_RXF0].num = mram_config_vals[3] &
1866 		FIELD_MAX(RXFC_FS_MASK);
1867 	cdev->mcfg[MRAM_RXF1].off = cdev->mcfg[MRAM_RXF0].off +
1868 		cdev->mcfg[MRAM_RXF0].num * RXF0_ELEMENT_SIZE;
1869 	cdev->mcfg[MRAM_RXF1].num = mram_config_vals[4] &
1870 		FIELD_MAX(RXFC_FS_MASK);
1871 	cdev->mcfg[MRAM_RXB].off = cdev->mcfg[MRAM_RXF1].off +
1872 		cdev->mcfg[MRAM_RXF1].num * RXF1_ELEMENT_SIZE;
1873 	cdev->mcfg[MRAM_RXB].num = mram_config_vals[5];
1874 	cdev->mcfg[MRAM_TXE].off = cdev->mcfg[MRAM_RXB].off +
1875 		cdev->mcfg[MRAM_RXB].num * RXB_ELEMENT_SIZE;
1876 	cdev->mcfg[MRAM_TXE].num = mram_config_vals[6];
1877 	cdev->mcfg[MRAM_TXB].off = cdev->mcfg[MRAM_TXE].off +
1878 		cdev->mcfg[MRAM_TXE].num * TXE_ELEMENT_SIZE;
1879 	cdev->mcfg[MRAM_TXB].num = mram_config_vals[7] &
1880 		FIELD_MAX(TXBC_NDTB_MASK);
1881 
1882 	dev_dbg(cdev->dev,
1883 		"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",
1884 		cdev->mcfg[MRAM_SIDF].off, cdev->mcfg[MRAM_SIDF].num,
1885 		cdev->mcfg[MRAM_XIDF].off, cdev->mcfg[MRAM_XIDF].num,
1886 		cdev->mcfg[MRAM_RXF0].off, cdev->mcfg[MRAM_RXF0].num,
1887 		cdev->mcfg[MRAM_RXF1].off, cdev->mcfg[MRAM_RXF1].num,
1888 		cdev->mcfg[MRAM_RXB].off, cdev->mcfg[MRAM_RXB].num,
1889 		cdev->mcfg[MRAM_TXE].off, cdev->mcfg[MRAM_TXE].num,
1890 		cdev->mcfg[MRAM_TXB].off, cdev->mcfg[MRAM_TXB].num);
1891 }
1892 
1893 int m_can_init_ram(struct m_can_classdev *cdev)
1894 {
1895 	int end, i, start;
1896 	int err = 0;
1897 
1898 	/* initialize the entire Message RAM in use to avoid possible
1899 	 * ECC/parity checksum errors when reading an uninitialized buffer
1900 	 */
1901 	start = cdev->mcfg[MRAM_SIDF].off;
1902 	end = cdev->mcfg[MRAM_TXB].off +
1903 		cdev->mcfg[MRAM_TXB].num * TXB_ELEMENT_SIZE;
1904 
1905 	for (i = start; i < end; i += 4) {
1906 		err = m_can_fifo_write_no_off(cdev, i, 0x0);
1907 		if (err)
1908 			break;
1909 	}
1910 
1911 	return err;
1912 }
1913 EXPORT_SYMBOL_GPL(m_can_init_ram);
1914 
1915 int m_can_class_get_clocks(struct m_can_classdev *cdev)
1916 {
1917 	int ret = 0;
1918 
1919 	cdev->hclk = devm_clk_get(cdev->dev, "hclk");
1920 	cdev->cclk = devm_clk_get(cdev->dev, "cclk");
1921 
1922 	if (IS_ERR(cdev->cclk)) {
1923 		dev_err(cdev->dev, "no clock found\n");
1924 		ret = -ENODEV;
1925 	}
1926 
1927 	return ret;
1928 }
1929 EXPORT_SYMBOL_GPL(m_can_class_get_clocks);
1930 
1931 struct m_can_classdev *m_can_class_allocate_dev(struct device *dev,
1932 						int sizeof_priv)
1933 {
1934 	struct m_can_classdev *class_dev = NULL;
1935 	u32 mram_config_vals[MRAM_CFG_LEN];
1936 	struct net_device *net_dev;
1937 	u32 tx_fifo_size;
1938 	int ret;
1939 
1940 	ret = fwnode_property_read_u32_array(dev_fwnode(dev),
1941 					     "bosch,mram-cfg",
1942 					     mram_config_vals,
1943 					     sizeof(mram_config_vals) / 4);
1944 	if (ret) {
1945 		dev_err(dev, "Could not get Message RAM configuration.");
1946 		goto out;
1947 	}
1948 
1949 	/* Get TX FIFO size
1950 	 * Defines the total amount of echo buffers for loopback
1951 	 */
1952 	tx_fifo_size = mram_config_vals[7];
1953 
1954 	/* allocate the m_can device */
1955 	net_dev = alloc_candev(sizeof_priv, tx_fifo_size);
1956 	if (!net_dev) {
1957 		dev_err(dev, "Failed to allocate CAN device");
1958 		goto out;
1959 	}
1960 
1961 	class_dev = netdev_priv(net_dev);
1962 	class_dev->net = net_dev;
1963 	class_dev->dev = dev;
1964 	SET_NETDEV_DEV(net_dev, dev);
1965 
1966 	m_can_of_parse_mram(class_dev, mram_config_vals);
1967 out:
1968 	return class_dev;
1969 }
1970 EXPORT_SYMBOL_GPL(m_can_class_allocate_dev);
1971 
1972 void m_can_class_free_dev(struct net_device *net)
1973 {
1974 	free_candev(net);
1975 }
1976 EXPORT_SYMBOL_GPL(m_can_class_free_dev);
1977 
1978 int m_can_class_register(struct m_can_classdev *cdev)
1979 {
1980 	int ret;
1981 
1982 	if (cdev->pm_clock_support) {
1983 		ret = m_can_clk_start(cdev);
1984 		if (ret)
1985 			return ret;
1986 	}
1987 
1988 	if (cdev->is_peripheral) {
1989 		ret = can_rx_offload_add_manual(cdev->net, &cdev->offload,
1990 						M_CAN_NAPI_WEIGHT);
1991 		if (ret)
1992 			goto clk_disable;
1993 	}
1994 
1995 	ret = m_can_dev_setup(cdev);
1996 	if (ret)
1997 		goto rx_offload_del;
1998 
1999 	ret = register_m_can_dev(cdev->net);
2000 	if (ret) {
2001 		dev_err(cdev->dev, "registering %s failed (err=%d)\n",
2002 			cdev->net->name, ret);
2003 		goto rx_offload_del;
2004 	}
2005 
2006 	devm_can_led_init(cdev->net);
2007 
2008 	of_can_transceiver(cdev->net);
2009 
2010 	dev_info(cdev->dev, "%s device registered (irq=%d, version=%d)\n",
2011 		 KBUILD_MODNAME, cdev->net->irq, cdev->version);
2012 
2013 	/* Probe finished
2014 	 * Stop clocks. They will be reactivated once the M_CAN device is opened
2015 	 */
2016 	m_can_clk_stop(cdev);
2017 
2018 	return 0;
2019 
2020 rx_offload_del:
2021 	if (cdev->is_peripheral)
2022 		can_rx_offload_del(&cdev->offload);
2023 clk_disable:
2024 	m_can_clk_stop(cdev);
2025 
2026 	return ret;
2027 }
2028 EXPORT_SYMBOL_GPL(m_can_class_register);
2029 
2030 void m_can_class_unregister(struct m_can_classdev *cdev)
2031 {
2032 	if (cdev->is_peripheral)
2033 		can_rx_offload_del(&cdev->offload);
2034 	unregister_candev(cdev->net);
2035 }
2036 EXPORT_SYMBOL_GPL(m_can_class_unregister);
2037 
2038 int m_can_class_suspend(struct device *dev)
2039 {
2040 	struct m_can_classdev *cdev = dev_get_drvdata(dev);
2041 	struct net_device *ndev = cdev->net;
2042 
2043 	if (netif_running(ndev)) {
2044 		netif_stop_queue(ndev);
2045 		netif_device_detach(ndev);
2046 		m_can_stop(ndev);
2047 		m_can_clk_stop(cdev);
2048 	}
2049 
2050 	pinctrl_pm_select_sleep_state(dev);
2051 
2052 	cdev->can.state = CAN_STATE_SLEEPING;
2053 
2054 	return 0;
2055 }
2056 EXPORT_SYMBOL_GPL(m_can_class_suspend);
2057 
2058 int m_can_class_resume(struct device *dev)
2059 {
2060 	struct m_can_classdev *cdev = dev_get_drvdata(dev);
2061 	struct net_device *ndev = cdev->net;
2062 
2063 	pinctrl_pm_select_default_state(dev);
2064 
2065 	cdev->can.state = CAN_STATE_ERROR_ACTIVE;
2066 
2067 	if (netif_running(ndev)) {
2068 		int ret;
2069 
2070 		ret = m_can_clk_start(cdev);
2071 		if (ret)
2072 			return ret;
2073 
2074 		m_can_init_ram(cdev);
2075 		m_can_start(ndev);
2076 		netif_device_attach(ndev);
2077 		netif_start_queue(ndev);
2078 	}
2079 
2080 	return 0;
2081 }
2082 EXPORT_SYMBOL_GPL(m_can_class_resume);
2083 
2084 MODULE_AUTHOR("Dong Aisheng <b29396@freescale.com>");
2085 MODULE_AUTHOR("Dan Murphy <dmurphy@ti.com>");
2086 MODULE_LICENSE("GPL v2");
2087 MODULE_DESCRIPTION("CAN bus driver for Bosch M_CAN controller");
2088