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