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