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