1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Fast Ethernet Controller (FEC) driver for Motorola MPC8xx.
4  * Copyright (c) 1997 Dan Malek (dmalek@jlc.net)
5  *
6  * Right now, I am very wasteful with the buffers.  I allocate memory
7  * pages and then divide them into 2K frame buffers.  This way I know I
8  * have buffers large enough to hold one frame within one buffer descriptor.
9  * Once I get this working, I will use 64 or 128 byte CPM buffers, which
10  * will be much more memory efficient and will easily handle lots of
11  * small packets.
12  *
13  * Much better multiple PHY support by Magnus Damm.
14  * Copyright (c) 2000 Ericsson Radio Systems AB.
15  *
16  * Support for FEC controller of ColdFire processors.
17  * Copyright (c) 2001-2005 Greg Ungerer (gerg@snapgear.com)
18  *
19  * Bug fixes and cleanup by Philippe De Muyter (phdm@macqel.be)
20  * Copyright (c) 2004-2006 Macq Electronique SA.
21  *
22  * Copyright (C) 2010-2011 Freescale Semiconductor, Inc.
23  */
24 
25 #include <linux/module.h>
26 #include <linux/kernel.h>
27 #include <linux/string.h>
28 #include <linux/pm_runtime.h>
29 #include <linux/ptrace.h>
30 #include <linux/errno.h>
31 #include <linux/ioport.h>
32 #include <linux/slab.h>
33 #include <linux/interrupt.h>
34 #include <linux/delay.h>
35 #include <linux/netdevice.h>
36 #include <linux/etherdevice.h>
37 #include <linux/skbuff.h>
38 #include <linux/in.h>
39 #include <linux/ip.h>
40 #include <net/ip.h>
41 #include <net/selftests.h>
42 #include <net/tso.h>
43 #include <linux/tcp.h>
44 #include <linux/udp.h>
45 #include <linux/icmp.h>
46 #include <linux/spinlock.h>
47 #include <linux/workqueue.h>
48 #include <linux/bitops.h>
49 #include <linux/io.h>
50 #include <linux/irq.h>
51 #include <linux/clk.h>
52 #include <linux/crc32.h>
53 #include <linux/platform_device.h>
54 #include <linux/mdio.h>
55 #include <linux/phy.h>
56 #include <linux/fec.h>
57 #include <linux/of.h>
58 #include <linux/of_device.h>
59 #include <linux/of_gpio.h>
60 #include <linux/of_mdio.h>
61 #include <linux/of_net.h>
62 #include <linux/regulator/consumer.h>
63 #include <linux/if_vlan.h>
64 #include <linux/pinctrl/consumer.h>
65 #include <linux/prefetch.h>
66 #include <linux/mfd/syscon.h>
67 #include <linux/regmap.h>
68 #include <soc/imx/cpuidle.h>
69 #include <linux/filter.h>
70 #include <linux/bpf.h>
71 
72 #include <asm/cacheflush.h>
73 
74 #include "fec.h"
75 
76 static void set_multicast_list(struct net_device *ndev);
77 static void fec_enet_itr_coal_init(struct net_device *ndev);
78 
79 #define DRIVER_NAME	"fec"
80 
81 static const u16 fec_enet_vlan_pri_to_queue[8] = {0, 0, 1, 1, 1, 2, 2, 2};
82 
83 /* Pause frame feild and FIFO threshold */
84 #define FEC_ENET_FCE	(1 << 5)
85 #define FEC_ENET_RSEM_V	0x84
86 #define FEC_ENET_RSFL_V	16
87 #define FEC_ENET_RAEM_V	0x8
88 #define FEC_ENET_RAFL_V	0x8
89 #define FEC_ENET_OPD_V	0xFFF0
90 #define FEC_MDIO_PM_TIMEOUT  100 /* ms */
91 
92 struct fec_devinfo {
93 	u32 quirks;
94 };
95 
96 static const struct fec_devinfo fec_imx25_info = {
97 	.quirks = FEC_QUIRK_USE_GASKET | FEC_QUIRK_MIB_CLEAR |
98 		  FEC_QUIRK_HAS_FRREG,
99 };
100 
101 static const struct fec_devinfo fec_imx27_info = {
102 	.quirks = FEC_QUIRK_MIB_CLEAR | FEC_QUIRK_HAS_FRREG,
103 };
104 
105 static const struct fec_devinfo fec_imx28_info = {
106 	.quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_SWAP_FRAME |
107 		  FEC_QUIRK_SINGLE_MDIO | FEC_QUIRK_HAS_RACC |
108 		  FEC_QUIRK_HAS_FRREG | FEC_QUIRK_CLEAR_SETUP_MII |
109 		  FEC_QUIRK_NO_HARD_RESET,
110 };
111 
112 static const struct fec_devinfo fec_imx6q_info = {
113 	.quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
114 		  FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
115 		  FEC_QUIRK_HAS_VLAN | FEC_QUIRK_ERR006358 |
116 		  FEC_QUIRK_HAS_RACC | FEC_QUIRK_CLEAR_SETUP_MII |
117 		  FEC_QUIRK_HAS_PMQOS,
118 };
119 
120 static const struct fec_devinfo fec_mvf600_info = {
121 	.quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_RACC,
122 };
123 
124 static const struct fec_devinfo fec_imx6x_info = {
125 	.quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
126 		  FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
127 		  FEC_QUIRK_HAS_VLAN | FEC_QUIRK_HAS_AVB |
128 		  FEC_QUIRK_ERR007885 | FEC_QUIRK_BUG_CAPTURE |
129 		  FEC_QUIRK_HAS_RACC | FEC_QUIRK_HAS_COALESCE |
130 		  FEC_QUIRK_CLEAR_SETUP_MII | FEC_QUIRK_HAS_MULTI_QUEUES,
131 };
132 
133 static const struct fec_devinfo fec_imx6ul_info = {
134 	.quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
135 		  FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
136 		  FEC_QUIRK_HAS_VLAN | FEC_QUIRK_ERR007885 |
137 		  FEC_QUIRK_BUG_CAPTURE | FEC_QUIRK_HAS_RACC |
138 		  FEC_QUIRK_HAS_COALESCE | FEC_QUIRK_CLEAR_SETUP_MII,
139 };
140 
141 static const struct fec_devinfo fec_imx8mq_info = {
142 	.quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
143 		  FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
144 		  FEC_QUIRK_HAS_VLAN | FEC_QUIRK_HAS_AVB |
145 		  FEC_QUIRK_ERR007885 | FEC_QUIRK_BUG_CAPTURE |
146 		  FEC_QUIRK_HAS_RACC | FEC_QUIRK_HAS_COALESCE |
147 		  FEC_QUIRK_CLEAR_SETUP_MII | FEC_QUIRK_HAS_MULTI_QUEUES |
148 		  FEC_QUIRK_HAS_EEE | FEC_QUIRK_WAKEUP_FROM_INT2,
149 };
150 
151 static const struct fec_devinfo fec_imx8qm_info = {
152 	.quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
153 		  FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
154 		  FEC_QUIRK_HAS_VLAN | FEC_QUIRK_HAS_AVB |
155 		  FEC_QUIRK_ERR007885 | FEC_QUIRK_BUG_CAPTURE |
156 		  FEC_QUIRK_HAS_RACC | FEC_QUIRK_HAS_COALESCE |
157 		  FEC_QUIRK_CLEAR_SETUP_MII | FEC_QUIRK_HAS_MULTI_QUEUES |
158 		  FEC_QUIRK_DELAYED_CLKS_SUPPORT,
159 };
160 
161 static const struct fec_devinfo fec_s32v234_info = {
162 	.quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
163 		  FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
164 		  FEC_QUIRK_HAS_VLAN | FEC_QUIRK_HAS_AVB |
165 		  FEC_QUIRK_ERR007885 | FEC_QUIRK_BUG_CAPTURE,
166 };
167 
168 static struct platform_device_id fec_devtype[] = {
169 	{
170 		/* keep it for coldfire */
171 		.name = DRIVER_NAME,
172 		.driver_data = 0,
173 	}, {
174 		.name = "imx25-fec",
175 		.driver_data = (kernel_ulong_t)&fec_imx25_info,
176 	}, {
177 		.name = "imx27-fec",
178 		.driver_data = (kernel_ulong_t)&fec_imx27_info,
179 	}, {
180 		.name = "imx28-fec",
181 		.driver_data = (kernel_ulong_t)&fec_imx28_info,
182 	}, {
183 		.name = "imx6q-fec",
184 		.driver_data = (kernel_ulong_t)&fec_imx6q_info,
185 	}, {
186 		.name = "mvf600-fec",
187 		.driver_data = (kernel_ulong_t)&fec_mvf600_info,
188 	}, {
189 		.name = "imx6sx-fec",
190 		.driver_data = (kernel_ulong_t)&fec_imx6x_info,
191 	}, {
192 		.name = "imx6ul-fec",
193 		.driver_data = (kernel_ulong_t)&fec_imx6ul_info,
194 	}, {
195 		.name = "imx8mq-fec",
196 		.driver_data = (kernel_ulong_t)&fec_imx8mq_info,
197 	}, {
198 		.name = "imx8qm-fec",
199 		.driver_data = (kernel_ulong_t)&fec_imx8qm_info,
200 	}, {
201 		.name = "s32v234-fec",
202 		.driver_data = (kernel_ulong_t)&fec_s32v234_info,
203 	}, {
204 		/* sentinel */
205 	}
206 };
207 MODULE_DEVICE_TABLE(platform, fec_devtype);
208 
209 enum imx_fec_type {
210 	IMX25_FEC = 1,	/* runs on i.mx25/50/53 */
211 	IMX27_FEC,	/* runs on i.mx27/35/51 */
212 	IMX28_FEC,
213 	IMX6Q_FEC,
214 	MVF600_FEC,
215 	IMX6SX_FEC,
216 	IMX6UL_FEC,
217 	IMX8MQ_FEC,
218 	IMX8QM_FEC,
219 	S32V234_FEC,
220 };
221 
222 static const struct of_device_id fec_dt_ids[] = {
223 	{ .compatible = "fsl,imx25-fec", .data = &fec_devtype[IMX25_FEC], },
224 	{ .compatible = "fsl,imx27-fec", .data = &fec_devtype[IMX27_FEC], },
225 	{ .compatible = "fsl,imx28-fec", .data = &fec_devtype[IMX28_FEC], },
226 	{ .compatible = "fsl,imx6q-fec", .data = &fec_devtype[IMX6Q_FEC], },
227 	{ .compatible = "fsl,mvf600-fec", .data = &fec_devtype[MVF600_FEC], },
228 	{ .compatible = "fsl,imx6sx-fec", .data = &fec_devtype[IMX6SX_FEC], },
229 	{ .compatible = "fsl,imx6ul-fec", .data = &fec_devtype[IMX6UL_FEC], },
230 	{ .compatible = "fsl,imx8mq-fec", .data = &fec_devtype[IMX8MQ_FEC], },
231 	{ .compatible = "fsl,imx8qm-fec", .data = &fec_devtype[IMX8QM_FEC], },
232 	{ .compatible = "fsl,s32v234-fec", .data = &fec_devtype[S32V234_FEC], },
233 	{ /* sentinel */ }
234 };
235 MODULE_DEVICE_TABLE(of, fec_dt_ids);
236 
237 static unsigned char macaddr[ETH_ALEN];
238 module_param_array(macaddr, byte, NULL, 0);
239 MODULE_PARM_DESC(macaddr, "FEC Ethernet MAC address");
240 
241 #if defined(CONFIG_M5272)
242 /*
243  * Some hardware gets it MAC address out of local flash memory.
244  * if this is non-zero then assume it is the address to get MAC from.
245  */
246 #if defined(CONFIG_NETtel)
247 #define	FEC_FLASHMAC	0xf0006006
248 #elif defined(CONFIG_GILBARCONAP) || defined(CONFIG_SCALES)
249 #define	FEC_FLASHMAC	0xf0006000
250 #elif defined(CONFIG_CANCam)
251 #define	FEC_FLASHMAC	0xf0020000
252 #elif defined (CONFIG_M5272C3)
253 #define	FEC_FLASHMAC	(0xffe04000 + 4)
254 #elif defined(CONFIG_MOD5272)
255 #define FEC_FLASHMAC	0xffc0406b
256 #else
257 #define	FEC_FLASHMAC	0
258 #endif
259 #endif /* CONFIG_M5272 */
260 
261 /* The FEC stores dest/src/type/vlan, data, and checksum for receive packets.
262  *
263  * 2048 byte skbufs are allocated. However, alignment requirements
264  * varies between FEC variants. Worst case is 64, so round down by 64.
265  */
266 #define PKT_MAXBUF_SIZE		(round_down(2048 - 64, 64))
267 #define PKT_MINBUF_SIZE		64
268 
269 /* FEC receive acceleration */
270 #define FEC_RACC_IPDIS		(1 << 1)
271 #define FEC_RACC_PRODIS		(1 << 2)
272 #define FEC_RACC_SHIFT16	BIT(7)
273 #define FEC_RACC_OPTIONS	(FEC_RACC_IPDIS | FEC_RACC_PRODIS)
274 
275 /* MIB Control Register */
276 #define FEC_MIB_CTRLSTAT_DISABLE	BIT(31)
277 
278 /*
279  * The 5270/5271/5280/5282/532x RX control register also contains maximum frame
280  * size bits. Other FEC hardware does not, so we need to take that into
281  * account when setting it.
282  */
283 #if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
284     defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM) || \
285     defined(CONFIG_ARM64)
286 #define	OPT_FRAME_SIZE	(PKT_MAXBUF_SIZE << 16)
287 #else
288 #define	OPT_FRAME_SIZE	0
289 #endif
290 
291 /* FEC MII MMFR bits definition */
292 #define FEC_MMFR_ST		(1 << 30)
293 #define FEC_MMFR_ST_C45		(0)
294 #define FEC_MMFR_OP_READ	(2 << 28)
295 #define FEC_MMFR_OP_READ_C45	(3 << 28)
296 #define FEC_MMFR_OP_WRITE	(1 << 28)
297 #define FEC_MMFR_OP_ADDR_WRITE	(0)
298 #define FEC_MMFR_PA(v)		((v & 0x1f) << 23)
299 #define FEC_MMFR_RA(v)		((v & 0x1f) << 18)
300 #define FEC_MMFR_TA		(2 << 16)
301 #define FEC_MMFR_DATA(v)	(v & 0xffff)
302 /* FEC ECR bits definition */
303 #define FEC_ECR_MAGICEN		(1 << 2)
304 #define FEC_ECR_SLEEP		(1 << 3)
305 
306 #define FEC_MII_TIMEOUT		30000 /* us */
307 
308 /* Transmitter timeout */
309 #define TX_TIMEOUT (2 * HZ)
310 
311 #define FEC_PAUSE_FLAG_AUTONEG	0x1
312 #define FEC_PAUSE_FLAG_ENABLE	0x2
313 #define FEC_WOL_HAS_MAGIC_PACKET	(0x1 << 0)
314 #define FEC_WOL_FLAG_ENABLE		(0x1 << 1)
315 #define FEC_WOL_FLAG_SLEEP_ON		(0x1 << 2)
316 
317 #define COPYBREAK_DEFAULT	256
318 
319 /* Max number of allowed TCP segments for software TSO */
320 #define FEC_MAX_TSO_SEGS	100
321 #define FEC_MAX_SKB_DESCS	(FEC_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS)
322 
323 #define IS_TSO_HEADER(txq, addr) \
324 	((addr >= txq->tso_hdrs_dma) && \
325 	(addr < txq->tso_hdrs_dma + txq->bd.ring_size * TSO_HEADER_SIZE))
326 
327 static int mii_cnt;
328 
329 static struct bufdesc *fec_enet_get_nextdesc(struct bufdesc *bdp,
330 					     struct bufdesc_prop *bd)
331 {
332 	return (bdp >= bd->last) ? bd->base
333 			: (struct bufdesc *)(((void *)bdp) + bd->dsize);
334 }
335 
336 static struct bufdesc *fec_enet_get_prevdesc(struct bufdesc *bdp,
337 					     struct bufdesc_prop *bd)
338 {
339 	return (bdp <= bd->base) ? bd->last
340 			: (struct bufdesc *)(((void *)bdp) - bd->dsize);
341 }
342 
343 static int fec_enet_get_bd_index(struct bufdesc *bdp,
344 				 struct bufdesc_prop *bd)
345 {
346 	return ((const char *)bdp - (const char *)bd->base) >> bd->dsize_log2;
347 }
348 
349 static int fec_enet_get_free_txdesc_num(struct fec_enet_priv_tx_q *txq)
350 {
351 	int entries;
352 
353 	entries = (((const char *)txq->dirty_tx -
354 			(const char *)txq->bd.cur) >> txq->bd.dsize_log2) - 1;
355 
356 	return entries >= 0 ? entries : entries + txq->bd.ring_size;
357 }
358 
359 static void swap_buffer(void *bufaddr, int len)
360 {
361 	int i;
362 	unsigned int *buf = bufaddr;
363 
364 	for (i = 0; i < len; i += 4, buf++)
365 		swab32s(buf);
366 }
367 
368 static void fec_dump(struct net_device *ndev)
369 {
370 	struct fec_enet_private *fep = netdev_priv(ndev);
371 	struct bufdesc *bdp;
372 	struct fec_enet_priv_tx_q *txq;
373 	int index = 0;
374 
375 	netdev_info(ndev, "TX ring dump\n");
376 	pr_info("Nr     SC     addr       len  SKB\n");
377 
378 	txq = fep->tx_queue[0];
379 	bdp = txq->bd.base;
380 
381 	do {
382 		pr_info("%3u %c%c 0x%04x 0x%08x %4u %p\n",
383 			index,
384 			bdp == txq->bd.cur ? 'S' : ' ',
385 			bdp == txq->dirty_tx ? 'H' : ' ',
386 			fec16_to_cpu(bdp->cbd_sc),
387 			fec32_to_cpu(bdp->cbd_bufaddr),
388 			fec16_to_cpu(bdp->cbd_datlen),
389 			txq->tx_skbuff[index]);
390 		bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
391 		index++;
392 	} while (bdp != txq->bd.base);
393 }
394 
395 static inline bool is_ipv4_pkt(struct sk_buff *skb)
396 {
397 	return skb->protocol == htons(ETH_P_IP) && ip_hdr(skb)->version == 4;
398 }
399 
400 static int
401 fec_enet_clear_csum(struct sk_buff *skb, struct net_device *ndev)
402 {
403 	/* Only run for packets requiring a checksum. */
404 	if (skb->ip_summed != CHECKSUM_PARTIAL)
405 		return 0;
406 
407 	if (unlikely(skb_cow_head(skb, 0)))
408 		return -1;
409 
410 	if (is_ipv4_pkt(skb))
411 		ip_hdr(skb)->check = 0;
412 	*(__sum16 *)(skb->head + skb->csum_start + skb->csum_offset) = 0;
413 
414 	return 0;
415 }
416 
417 static int
418 fec_enet_create_page_pool(struct fec_enet_private *fep,
419 			  struct fec_enet_priv_rx_q *rxq, int size)
420 {
421 	struct page_pool_params pp_params = {
422 		.order = 0,
423 		.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV,
424 		.pool_size = size,
425 		.nid = dev_to_node(&fep->pdev->dev),
426 		.dev = &fep->pdev->dev,
427 		.dma_dir = DMA_FROM_DEVICE,
428 		.offset = FEC_ENET_XDP_HEADROOM,
429 		.max_len = FEC_ENET_RX_FRSIZE,
430 	};
431 	int err;
432 
433 	rxq->page_pool = page_pool_create(&pp_params);
434 	if (IS_ERR(rxq->page_pool)) {
435 		err = PTR_ERR(rxq->page_pool);
436 		rxq->page_pool = NULL;
437 		return err;
438 	}
439 
440 	err = xdp_rxq_info_reg(&rxq->xdp_rxq, fep->netdev, rxq->id, 0);
441 	if (err < 0)
442 		goto err_free_pp;
443 
444 	err = xdp_rxq_info_reg_mem_model(&rxq->xdp_rxq, MEM_TYPE_PAGE_POOL,
445 					 rxq->page_pool);
446 	if (err)
447 		goto err_unregister_rxq;
448 
449 	return 0;
450 
451 err_unregister_rxq:
452 	xdp_rxq_info_unreg(&rxq->xdp_rxq);
453 err_free_pp:
454 	page_pool_destroy(rxq->page_pool);
455 	rxq->page_pool = NULL;
456 	return err;
457 }
458 
459 static struct bufdesc *
460 fec_enet_txq_submit_frag_skb(struct fec_enet_priv_tx_q *txq,
461 			     struct sk_buff *skb,
462 			     struct net_device *ndev)
463 {
464 	struct fec_enet_private *fep = netdev_priv(ndev);
465 	struct bufdesc *bdp = txq->bd.cur;
466 	struct bufdesc_ex *ebdp;
467 	int nr_frags = skb_shinfo(skb)->nr_frags;
468 	int frag, frag_len;
469 	unsigned short status;
470 	unsigned int estatus = 0;
471 	skb_frag_t *this_frag;
472 	unsigned int index;
473 	void *bufaddr;
474 	dma_addr_t addr;
475 	int i;
476 
477 	for (frag = 0; frag < nr_frags; frag++) {
478 		this_frag = &skb_shinfo(skb)->frags[frag];
479 		bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
480 		ebdp = (struct bufdesc_ex *)bdp;
481 
482 		status = fec16_to_cpu(bdp->cbd_sc);
483 		status &= ~BD_ENET_TX_STATS;
484 		status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
485 		frag_len = skb_frag_size(&skb_shinfo(skb)->frags[frag]);
486 
487 		/* Handle the last BD specially */
488 		if (frag == nr_frags - 1) {
489 			status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
490 			if (fep->bufdesc_ex) {
491 				estatus |= BD_ENET_TX_INT;
492 				if (unlikely(skb_shinfo(skb)->tx_flags &
493 					SKBTX_HW_TSTAMP && fep->hwts_tx_en))
494 					estatus |= BD_ENET_TX_TS;
495 			}
496 		}
497 
498 		if (fep->bufdesc_ex) {
499 			if (fep->quirks & FEC_QUIRK_HAS_AVB)
500 				estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
501 			if (skb->ip_summed == CHECKSUM_PARTIAL)
502 				estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
503 
504 			ebdp->cbd_bdu = 0;
505 			ebdp->cbd_esc = cpu_to_fec32(estatus);
506 		}
507 
508 		bufaddr = skb_frag_address(this_frag);
509 
510 		index = fec_enet_get_bd_index(bdp, &txq->bd);
511 		if (((unsigned long) bufaddr) & fep->tx_align ||
512 			fep->quirks & FEC_QUIRK_SWAP_FRAME) {
513 			memcpy(txq->tx_bounce[index], bufaddr, frag_len);
514 			bufaddr = txq->tx_bounce[index];
515 
516 			if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
517 				swap_buffer(bufaddr, frag_len);
518 		}
519 
520 		addr = dma_map_single(&fep->pdev->dev, bufaddr, frag_len,
521 				      DMA_TO_DEVICE);
522 		if (dma_mapping_error(&fep->pdev->dev, addr)) {
523 			if (net_ratelimit())
524 				netdev_err(ndev, "Tx DMA memory map failed\n");
525 			goto dma_mapping_error;
526 		}
527 
528 		bdp->cbd_bufaddr = cpu_to_fec32(addr);
529 		bdp->cbd_datlen = cpu_to_fec16(frag_len);
530 		/* Make sure the updates to rest of the descriptor are
531 		 * performed before transferring ownership.
532 		 */
533 		wmb();
534 		bdp->cbd_sc = cpu_to_fec16(status);
535 	}
536 
537 	return bdp;
538 dma_mapping_error:
539 	bdp = txq->bd.cur;
540 	for (i = 0; i < frag; i++) {
541 		bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
542 		dma_unmap_single(&fep->pdev->dev, fec32_to_cpu(bdp->cbd_bufaddr),
543 				 fec16_to_cpu(bdp->cbd_datlen), DMA_TO_DEVICE);
544 	}
545 	return ERR_PTR(-ENOMEM);
546 }
547 
548 static int fec_enet_txq_submit_skb(struct fec_enet_priv_tx_q *txq,
549 				   struct sk_buff *skb, struct net_device *ndev)
550 {
551 	struct fec_enet_private *fep = netdev_priv(ndev);
552 	int nr_frags = skb_shinfo(skb)->nr_frags;
553 	struct bufdesc *bdp, *last_bdp;
554 	void *bufaddr;
555 	dma_addr_t addr;
556 	unsigned short status;
557 	unsigned short buflen;
558 	unsigned int estatus = 0;
559 	unsigned int index;
560 	int entries_free;
561 
562 	entries_free = fec_enet_get_free_txdesc_num(txq);
563 	if (entries_free < MAX_SKB_FRAGS + 1) {
564 		dev_kfree_skb_any(skb);
565 		if (net_ratelimit())
566 			netdev_err(ndev, "NOT enough BD for SG!\n");
567 		return NETDEV_TX_OK;
568 	}
569 
570 	/* Protocol checksum off-load for TCP and UDP. */
571 	if (fec_enet_clear_csum(skb, ndev)) {
572 		dev_kfree_skb_any(skb);
573 		return NETDEV_TX_OK;
574 	}
575 
576 	/* Fill in a Tx ring entry */
577 	bdp = txq->bd.cur;
578 	last_bdp = bdp;
579 	status = fec16_to_cpu(bdp->cbd_sc);
580 	status &= ~BD_ENET_TX_STATS;
581 
582 	/* Set buffer length and buffer pointer */
583 	bufaddr = skb->data;
584 	buflen = skb_headlen(skb);
585 
586 	index = fec_enet_get_bd_index(bdp, &txq->bd);
587 	if (((unsigned long) bufaddr) & fep->tx_align ||
588 		fep->quirks & FEC_QUIRK_SWAP_FRAME) {
589 		memcpy(txq->tx_bounce[index], skb->data, buflen);
590 		bufaddr = txq->tx_bounce[index];
591 
592 		if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
593 			swap_buffer(bufaddr, buflen);
594 	}
595 
596 	/* Push the data cache so the CPM does not get stale memory data. */
597 	addr = dma_map_single(&fep->pdev->dev, bufaddr, buflen, DMA_TO_DEVICE);
598 	if (dma_mapping_error(&fep->pdev->dev, addr)) {
599 		dev_kfree_skb_any(skb);
600 		if (net_ratelimit())
601 			netdev_err(ndev, "Tx DMA memory map failed\n");
602 		return NETDEV_TX_OK;
603 	}
604 
605 	if (nr_frags) {
606 		last_bdp = fec_enet_txq_submit_frag_skb(txq, skb, ndev);
607 		if (IS_ERR(last_bdp)) {
608 			dma_unmap_single(&fep->pdev->dev, addr,
609 					 buflen, DMA_TO_DEVICE);
610 			dev_kfree_skb_any(skb);
611 			return NETDEV_TX_OK;
612 		}
613 	} else {
614 		status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
615 		if (fep->bufdesc_ex) {
616 			estatus = BD_ENET_TX_INT;
617 			if (unlikely(skb_shinfo(skb)->tx_flags &
618 				SKBTX_HW_TSTAMP && fep->hwts_tx_en))
619 				estatus |= BD_ENET_TX_TS;
620 		}
621 	}
622 	bdp->cbd_bufaddr = cpu_to_fec32(addr);
623 	bdp->cbd_datlen = cpu_to_fec16(buflen);
624 
625 	if (fep->bufdesc_ex) {
626 
627 		struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
628 
629 		if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
630 			fep->hwts_tx_en))
631 			skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
632 
633 		if (fep->quirks & FEC_QUIRK_HAS_AVB)
634 			estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
635 
636 		if (skb->ip_summed == CHECKSUM_PARTIAL)
637 			estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
638 
639 		ebdp->cbd_bdu = 0;
640 		ebdp->cbd_esc = cpu_to_fec32(estatus);
641 	}
642 
643 	index = fec_enet_get_bd_index(last_bdp, &txq->bd);
644 	/* Save skb pointer */
645 	txq->tx_skbuff[index] = skb;
646 
647 	/* Make sure the updates to rest of the descriptor are performed before
648 	 * transferring ownership.
649 	 */
650 	wmb();
651 
652 	/* Send it on its way.  Tell FEC it's ready, interrupt when done,
653 	 * it's the last BD of the frame, and to put the CRC on the end.
654 	 */
655 	status |= (BD_ENET_TX_READY | BD_ENET_TX_TC);
656 	bdp->cbd_sc = cpu_to_fec16(status);
657 
658 	/* If this was the last BD in the ring, start at the beginning again. */
659 	bdp = fec_enet_get_nextdesc(last_bdp, &txq->bd);
660 
661 	skb_tx_timestamp(skb);
662 
663 	/* Make sure the update to bdp and tx_skbuff are performed before
664 	 * txq->bd.cur.
665 	 */
666 	wmb();
667 	txq->bd.cur = bdp;
668 
669 	/* Trigger transmission start */
670 	writel(0, txq->bd.reg_desc_active);
671 
672 	return 0;
673 }
674 
675 static int
676 fec_enet_txq_put_data_tso(struct fec_enet_priv_tx_q *txq, struct sk_buff *skb,
677 			  struct net_device *ndev,
678 			  struct bufdesc *bdp, int index, char *data,
679 			  int size, bool last_tcp, bool is_last)
680 {
681 	struct fec_enet_private *fep = netdev_priv(ndev);
682 	struct bufdesc_ex *ebdp = container_of(bdp, struct bufdesc_ex, desc);
683 	unsigned short status;
684 	unsigned int estatus = 0;
685 	dma_addr_t addr;
686 
687 	status = fec16_to_cpu(bdp->cbd_sc);
688 	status &= ~BD_ENET_TX_STATS;
689 
690 	status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
691 
692 	if (((unsigned long) data) & fep->tx_align ||
693 		fep->quirks & FEC_QUIRK_SWAP_FRAME) {
694 		memcpy(txq->tx_bounce[index], data, size);
695 		data = txq->tx_bounce[index];
696 
697 		if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
698 			swap_buffer(data, size);
699 	}
700 
701 	addr = dma_map_single(&fep->pdev->dev, data, size, DMA_TO_DEVICE);
702 	if (dma_mapping_error(&fep->pdev->dev, addr)) {
703 		dev_kfree_skb_any(skb);
704 		if (net_ratelimit())
705 			netdev_err(ndev, "Tx DMA memory map failed\n");
706 		return NETDEV_TX_BUSY;
707 	}
708 
709 	bdp->cbd_datlen = cpu_to_fec16(size);
710 	bdp->cbd_bufaddr = cpu_to_fec32(addr);
711 
712 	if (fep->bufdesc_ex) {
713 		if (fep->quirks & FEC_QUIRK_HAS_AVB)
714 			estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
715 		if (skb->ip_summed == CHECKSUM_PARTIAL)
716 			estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
717 		ebdp->cbd_bdu = 0;
718 		ebdp->cbd_esc = cpu_to_fec32(estatus);
719 	}
720 
721 	/* Handle the last BD specially */
722 	if (last_tcp)
723 		status |= (BD_ENET_TX_LAST | BD_ENET_TX_TC);
724 	if (is_last) {
725 		status |= BD_ENET_TX_INTR;
726 		if (fep->bufdesc_ex)
727 			ebdp->cbd_esc |= cpu_to_fec32(BD_ENET_TX_INT);
728 	}
729 
730 	bdp->cbd_sc = cpu_to_fec16(status);
731 
732 	return 0;
733 }
734 
735 static int
736 fec_enet_txq_put_hdr_tso(struct fec_enet_priv_tx_q *txq,
737 			 struct sk_buff *skb, struct net_device *ndev,
738 			 struct bufdesc *bdp, int index)
739 {
740 	struct fec_enet_private *fep = netdev_priv(ndev);
741 	int hdr_len = skb_tcp_all_headers(skb);
742 	struct bufdesc_ex *ebdp = container_of(bdp, struct bufdesc_ex, desc);
743 	void *bufaddr;
744 	unsigned long dmabuf;
745 	unsigned short status;
746 	unsigned int estatus = 0;
747 
748 	status = fec16_to_cpu(bdp->cbd_sc);
749 	status &= ~BD_ENET_TX_STATS;
750 	status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
751 
752 	bufaddr = txq->tso_hdrs + index * TSO_HEADER_SIZE;
753 	dmabuf = txq->tso_hdrs_dma + index * TSO_HEADER_SIZE;
754 	if (((unsigned long)bufaddr) & fep->tx_align ||
755 		fep->quirks & FEC_QUIRK_SWAP_FRAME) {
756 		memcpy(txq->tx_bounce[index], skb->data, hdr_len);
757 		bufaddr = txq->tx_bounce[index];
758 
759 		if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
760 			swap_buffer(bufaddr, hdr_len);
761 
762 		dmabuf = dma_map_single(&fep->pdev->dev, bufaddr,
763 					hdr_len, DMA_TO_DEVICE);
764 		if (dma_mapping_error(&fep->pdev->dev, dmabuf)) {
765 			dev_kfree_skb_any(skb);
766 			if (net_ratelimit())
767 				netdev_err(ndev, "Tx DMA memory map failed\n");
768 			return NETDEV_TX_BUSY;
769 		}
770 	}
771 
772 	bdp->cbd_bufaddr = cpu_to_fec32(dmabuf);
773 	bdp->cbd_datlen = cpu_to_fec16(hdr_len);
774 
775 	if (fep->bufdesc_ex) {
776 		if (fep->quirks & FEC_QUIRK_HAS_AVB)
777 			estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
778 		if (skb->ip_summed == CHECKSUM_PARTIAL)
779 			estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
780 		ebdp->cbd_bdu = 0;
781 		ebdp->cbd_esc = cpu_to_fec32(estatus);
782 	}
783 
784 	bdp->cbd_sc = cpu_to_fec16(status);
785 
786 	return 0;
787 }
788 
789 static int fec_enet_txq_submit_tso(struct fec_enet_priv_tx_q *txq,
790 				   struct sk_buff *skb,
791 				   struct net_device *ndev)
792 {
793 	struct fec_enet_private *fep = netdev_priv(ndev);
794 	int hdr_len, total_len, data_left;
795 	struct bufdesc *bdp = txq->bd.cur;
796 	struct tso_t tso;
797 	unsigned int index = 0;
798 	int ret;
799 
800 	if (tso_count_descs(skb) >= fec_enet_get_free_txdesc_num(txq)) {
801 		dev_kfree_skb_any(skb);
802 		if (net_ratelimit())
803 			netdev_err(ndev, "NOT enough BD for TSO!\n");
804 		return NETDEV_TX_OK;
805 	}
806 
807 	/* Protocol checksum off-load for TCP and UDP. */
808 	if (fec_enet_clear_csum(skb, ndev)) {
809 		dev_kfree_skb_any(skb);
810 		return NETDEV_TX_OK;
811 	}
812 
813 	/* Initialize the TSO handler, and prepare the first payload */
814 	hdr_len = tso_start(skb, &tso);
815 
816 	total_len = skb->len - hdr_len;
817 	while (total_len > 0) {
818 		char *hdr;
819 
820 		index = fec_enet_get_bd_index(bdp, &txq->bd);
821 		data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
822 		total_len -= data_left;
823 
824 		/* prepare packet headers: MAC + IP + TCP */
825 		hdr = txq->tso_hdrs + index * TSO_HEADER_SIZE;
826 		tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
827 		ret = fec_enet_txq_put_hdr_tso(txq, skb, ndev, bdp, index);
828 		if (ret)
829 			goto err_release;
830 
831 		while (data_left > 0) {
832 			int size;
833 
834 			size = min_t(int, tso.size, data_left);
835 			bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
836 			index = fec_enet_get_bd_index(bdp, &txq->bd);
837 			ret = fec_enet_txq_put_data_tso(txq, skb, ndev,
838 							bdp, index,
839 							tso.data, size,
840 							size == data_left,
841 							total_len == 0);
842 			if (ret)
843 				goto err_release;
844 
845 			data_left -= size;
846 			tso_build_data(skb, &tso, size);
847 		}
848 
849 		bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
850 	}
851 
852 	/* Save skb pointer */
853 	txq->tx_skbuff[index] = skb;
854 
855 	skb_tx_timestamp(skb);
856 	txq->bd.cur = bdp;
857 
858 	/* Trigger transmission start */
859 	if (!(fep->quirks & FEC_QUIRK_ERR007885) ||
860 	    !readl(txq->bd.reg_desc_active) ||
861 	    !readl(txq->bd.reg_desc_active) ||
862 	    !readl(txq->bd.reg_desc_active) ||
863 	    !readl(txq->bd.reg_desc_active))
864 		writel(0, txq->bd.reg_desc_active);
865 
866 	return 0;
867 
868 err_release:
869 	/* TODO: Release all used data descriptors for TSO */
870 	return ret;
871 }
872 
873 static netdev_tx_t
874 fec_enet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
875 {
876 	struct fec_enet_private *fep = netdev_priv(ndev);
877 	int entries_free;
878 	unsigned short queue;
879 	struct fec_enet_priv_tx_q *txq;
880 	struct netdev_queue *nq;
881 	int ret;
882 
883 	queue = skb_get_queue_mapping(skb);
884 	txq = fep->tx_queue[queue];
885 	nq = netdev_get_tx_queue(ndev, queue);
886 
887 	if (skb_is_gso(skb))
888 		ret = fec_enet_txq_submit_tso(txq, skb, ndev);
889 	else
890 		ret = fec_enet_txq_submit_skb(txq, skb, ndev);
891 	if (ret)
892 		return ret;
893 
894 	entries_free = fec_enet_get_free_txdesc_num(txq);
895 	if (entries_free <= txq->tx_stop_threshold)
896 		netif_tx_stop_queue(nq);
897 
898 	return NETDEV_TX_OK;
899 }
900 
901 /* Init RX & TX buffer descriptors
902  */
903 static void fec_enet_bd_init(struct net_device *dev)
904 {
905 	struct fec_enet_private *fep = netdev_priv(dev);
906 	struct fec_enet_priv_tx_q *txq;
907 	struct fec_enet_priv_rx_q *rxq;
908 	struct bufdesc *bdp;
909 	unsigned int i;
910 	unsigned int q;
911 
912 	for (q = 0; q < fep->num_rx_queues; q++) {
913 		/* Initialize the receive buffer descriptors. */
914 		rxq = fep->rx_queue[q];
915 		bdp = rxq->bd.base;
916 
917 		for (i = 0; i < rxq->bd.ring_size; i++) {
918 
919 			/* Initialize the BD for every fragment in the page. */
920 			if (bdp->cbd_bufaddr)
921 				bdp->cbd_sc = cpu_to_fec16(BD_ENET_RX_EMPTY);
922 			else
923 				bdp->cbd_sc = cpu_to_fec16(0);
924 			bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
925 		}
926 
927 		/* Set the last buffer to wrap */
928 		bdp = fec_enet_get_prevdesc(bdp, &rxq->bd);
929 		bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP);
930 
931 		rxq->bd.cur = rxq->bd.base;
932 	}
933 
934 	for (q = 0; q < fep->num_tx_queues; q++) {
935 		/* ...and the same for transmit */
936 		txq = fep->tx_queue[q];
937 		bdp = txq->bd.base;
938 		txq->bd.cur = bdp;
939 
940 		for (i = 0; i < txq->bd.ring_size; i++) {
941 			/* Initialize the BD for every fragment in the page. */
942 			bdp->cbd_sc = cpu_to_fec16(0);
943 			if (bdp->cbd_bufaddr &&
944 			    !IS_TSO_HEADER(txq, fec32_to_cpu(bdp->cbd_bufaddr)))
945 				dma_unmap_single(&fep->pdev->dev,
946 						 fec32_to_cpu(bdp->cbd_bufaddr),
947 						 fec16_to_cpu(bdp->cbd_datlen),
948 						 DMA_TO_DEVICE);
949 			if (txq->tx_skbuff[i]) {
950 				dev_kfree_skb_any(txq->tx_skbuff[i]);
951 				txq->tx_skbuff[i] = NULL;
952 			}
953 			bdp->cbd_bufaddr = cpu_to_fec32(0);
954 			bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
955 		}
956 
957 		/* Set the last buffer to wrap */
958 		bdp = fec_enet_get_prevdesc(bdp, &txq->bd);
959 		bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP);
960 		txq->dirty_tx = bdp;
961 	}
962 }
963 
964 static void fec_enet_active_rxring(struct net_device *ndev)
965 {
966 	struct fec_enet_private *fep = netdev_priv(ndev);
967 	int i;
968 
969 	for (i = 0; i < fep->num_rx_queues; i++)
970 		writel(0, fep->rx_queue[i]->bd.reg_desc_active);
971 }
972 
973 static void fec_enet_enable_ring(struct net_device *ndev)
974 {
975 	struct fec_enet_private *fep = netdev_priv(ndev);
976 	struct fec_enet_priv_tx_q *txq;
977 	struct fec_enet_priv_rx_q *rxq;
978 	int i;
979 
980 	for (i = 0; i < fep->num_rx_queues; i++) {
981 		rxq = fep->rx_queue[i];
982 		writel(rxq->bd.dma, fep->hwp + FEC_R_DES_START(i));
983 		writel(PKT_MAXBUF_SIZE, fep->hwp + FEC_R_BUFF_SIZE(i));
984 
985 		/* enable DMA1/2 */
986 		if (i)
987 			writel(RCMR_MATCHEN | RCMR_CMP(i),
988 			       fep->hwp + FEC_RCMR(i));
989 	}
990 
991 	for (i = 0; i < fep->num_tx_queues; i++) {
992 		txq = fep->tx_queue[i];
993 		writel(txq->bd.dma, fep->hwp + FEC_X_DES_START(i));
994 
995 		/* enable DMA1/2 */
996 		if (i)
997 			writel(DMA_CLASS_EN | IDLE_SLOPE(i),
998 			       fep->hwp + FEC_DMA_CFG(i));
999 	}
1000 }
1001 
1002 static void fec_enet_reset_skb(struct net_device *ndev)
1003 {
1004 	struct fec_enet_private *fep = netdev_priv(ndev);
1005 	struct fec_enet_priv_tx_q *txq;
1006 	int i, j;
1007 
1008 	for (i = 0; i < fep->num_tx_queues; i++) {
1009 		txq = fep->tx_queue[i];
1010 
1011 		for (j = 0; j < txq->bd.ring_size; j++) {
1012 			if (txq->tx_skbuff[j]) {
1013 				dev_kfree_skb_any(txq->tx_skbuff[j]);
1014 				txq->tx_skbuff[j] = NULL;
1015 			}
1016 		}
1017 	}
1018 }
1019 
1020 /*
1021  * This function is called to start or restart the FEC during a link
1022  * change, transmit timeout, or to reconfigure the FEC.  The network
1023  * packet processing for this device must be stopped before this call.
1024  */
1025 static void
1026 fec_restart(struct net_device *ndev)
1027 {
1028 	struct fec_enet_private *fep = netdev_priv(ndev);
1029 	u32 temp_mac[2];
1030 	u32 rcntl = OPT_FRAME_SIZE | 0x04;
1031 	u32 ecntl = 0x2; /* ETHEREN */
1032 
1033 	/* Whack a reset.  We should wait for this.
1034 	 * For i.MX6SX SOC, enet use AXI bus, we use disable MAC
1035 	 * instead of reset MAC itself.
1036 	 */
1037 	if (fep->quirks & FEC_QUIRK_HAS_MULTI_QUEUES ||
1038 	    ((fep->quirks & FEC_QUIRK_NO_HARD_RESET) && fep->link)) {
1039 		writel(0, fep->hwp + FEC_ECNTRL);
1040 	} else {
1041 		writel(1, fep->hwp + FEC_ECNTRL);
1042 		udelay(10);
1043 	}
1044 
1045 	/*
1046 	 * enet-mac reset will reset mac address registers too,
1047 	 * so need to reconfigure it.
1048 	 */
1049 	memcpy(&temp_mac, ndev->dev_addr, ETH_ALEN);
1050 	writel((__force u32)cpu_to_be32(temp_mac[0]),
1051 	       fep->hwp + FEC_ADDR_LOW);
1052 	writel((__force u32)cpu_to_be32(temp_mac[1]),
1053 	       fep->hwp + FEC_ADDR_HIGH);
1054 
1055 	/* Clear any outstanding interrupt, except MDIO. */
1056 	writel((0xffffffff & ~FEC_ENET_MII), fep->hwp + FEC_IEVENT);
1057 
1058 	fec_enet_bd_init(ndev);
1059 
1060 	fec_enet_enable_ring(ndev);
1061 
1062 	/* Reset tx SKB buffers. */
1063 	fec_enet_reset_skb(ndev);
1064 
1065 	/* Enable MII mode */
1066 	if (fep->full_duplex == DUPLEX_FULL) {
1067 		/* FD enable */
1068 		writel(0x04, fep->hwp + FEC_X_CNTRL);
1069 	} else {
1070 		/* No Rcv on Xmit */
1071 		rcntl |= 0x02;
1072 		writel(0x0, fep->hwp + FEC_X_CNTRL);
1073 	}
1074 
1075 	/* Set MII speed */
1076 	writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
1077 
1078 #if !defined(CONFIG_M5272)
1079 	if (fep->quirks & FEC_QUIRK_HAS_RACC) {
1080 		u32 val = readl(fep->hwp + FEC_RACC);
1081 
1082 		/* align IP header */
1083 		val |= FEC_RACC_SHIFT16;
1084 		if (fep->csum_flags & FLAG_RX_CSUM_ENABLED)
1085 			/* set RX checksum */
1086 			val |= FEC_RACC_OPTIONS;
1087 		else
1088 			val &= ~FEC_RACC_OPTIONS;
1089 		writel(val, fep->hwp + FEC_RACC);
1090 		writel(PKT_MAXBUF_SIZE, fep->hwp + FEC_FTRL);
1091 	}
1092 #endif
1093 
1094 	/*
1095 	 * The phy interface and speed need to get configured
1096 	 * differently on enet-mac.
1097 	 */
1098 	if (fep->quirks & FEC_QUIRK_ENET_MAC) {
1099 		/* Enable flow control and length check */
1100 		rcntl |= 0x40000000 | 0x00000020;
1101 
1102 		/* RGMII, RMII or MII */
1103 		if (fep->phy_interface == PHY_INTERFACE_MODE_RGMII ||
1104 		    fep->phy_interface == PHY_INTERFACE_MODE_RGMII_ID ||
1105 		    fep->phy_interface == PHY_INTERFACE_MODE_RGMII_RXID ||
1106 		    fep->phy_interface == PHY_INTERFACE_MODE_RGMII_TXID)
1107 			rcntl |= (1 << 6);
1108 		else if (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
1109 			rcntl |= (1 << 8);
1110 		else
1111 			rcntl &= ~(1 << 8);
1112 
1113 		/* 1G, 100M or 10M */
1114 		if (ndev->phydev) {
1115 			if (ndev->phydev->speed == SPEED_1000)
1116 				ecntl |= (1 << 5);
1117 			else if (ndev->phydev->speed == SPEED_100)
1118 				rcntl &= ~(1 << 9);
1119 			else
1120 				rcntl |= (1 << 9);
1121 		}
1122 	} else {
1123 #ifdef FEC_MIIGSK_ENR
1124 		if (fep->quirks & FEC_QUIRK_USE_GASKET) {
1125 			u32 cfgr;
1126 			/* disable the gasket and wait */
1127 			writel(0, fep->hwp + FEC_MIIGSK_ENR);
1128 			while (readl(fep->hwp + FEC_MIIGSK_ENR) & 4)
1129 				udelay(1);
1130 
1131 			/*
1132 			 * configure the gasket:
1133 			 *   RMII, 50 MHz, no loopback, no echo
1134 			 *   MII, 25 MHz, no loopback, no echo
1135 			 */
1136 			cfgr = (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
1137 				? BM_MIIGSK_CFGR_RMII : BM_MIIGSK_CFGR_MII;
1138 			if (ndev->phydev && ndev->phydev->speed == SPEED_10)
1139 				cfgr |= BM_MIIGSK_CFGR_FRCONT_10M;
1140 			writel(cfgr, fep->hwp + FEC_MIIGSK_CFGR);
1141 
1142 			/* re-enable the gasket */
1143 			writel(2, fep->hwp + FEC_MIIGSK_ENR);
1144 		}
1145 #endif
1146 	}
1147 
1148 #if !defined(CONFIG_M5272)
1149 	/* enable pause frame*/
1150 	if ((fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) ||
1151 	    ((fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) &&
1152 	     ndev->phydev && ndev->phydev->pause)) {
1153 		rcntl |= FEC_ENET_FCE;
1154 
1155 		/* set FIFO threshold parameter to reduce overrun */
1156 		writel(FEC_ENET_RSEM_V, fep->hwp + FEC_R_FIFO_RSEM);
1157 		writel(FEC_ENET_RSFL_V, fep->hwp + FEC_R_FIFO_RSFL);
1158 		writel(FEC_ENET_RAEM_V, fep->hwp + FEC_R_FIFO_RAEM);
1159 		writel(FEC_ENET_RAFL_V, fep->hwp + FEC_R_FIFO_RAFL);
1160 
1161 		/* OPD */
1162 		writel(FEC_ENET_OPD_V, fep->hwp + FEC_OPD);
1163 	} else {
1164 		rcntl &= ~FEC_ENET_FCE;
1165 	}
1166 #endif /* !defined(CONFIG_M5272) */
1167 
1168 	writel(rcntl, fep->hwp + FEC_R_CNTRL);
1169 
1170 	/* Setup multicast filter. */
1171 	set_multicast_list(ndev);
1172 #ifndef CONFIG_M5272
1173 	writel(0, fep->hwp + FEC_HASH_TABLE_HIGH);
1174 	writel(0, fep->hwp + FEC_HASH_TABLE_LOW);
1175 #endif
1176 
1177 	if (fep->quirks & FEC_QUIRK_ENET_MAC) {
1178 		/* enable ENET endian swap */
1179 		ecntl |= (1 << 8);
1180 		/* enable ENET store and forward mode */
1181 		writel(1 << 8, fep->hwp + FEC_X_WMRK);
1182 	}
1183 
1184 	if (fep->bufdesc_ex)
1185 		ecntl |= (1 << 4);
1186 
1187 	if (fep->quirks & FEC_QUIRK_DELAYED_CLKS_SUPPORT &&
1188 	    fep->rgmii_txc_dly)
1189 		ecntl |= FEC_ENET_TXC_DLY;
1190 	if (fep->quirks & FEC_QUIRK_DELAYED_CLKS_SUPPORT &&
1191 	    fep->rgmii_rxc_dly)
1192 		ecntl |= FEC_ENET_RXC_DLY;
1193 
1194 #ifndef CONFIG_M5272
1195 	/* Enable the MIB statistic event counters */
1196 	writel(0 << 31, fep->hwp + FEC_MIB_CTRLSTAT);
1197 #endif
1198 
1199 	/* And last, enable the transmit and receive processing */
1200 	writel(ecntl, fep->hwp + FEC_ECNTRL);
1201 	fec_enet_active_rxring(ndev);
1202 
1203 	if (fep->bufdesc_ex)
1204 		fec_ptp_start_cyclecounter(ndev);
1205 
1206 	/* Enable interrupts we wish to service */
1207 	if (fep->link)
1208 		writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1209 	else
1210 		writel(0, fep->hwp + FEC_IMASK);
1211 
1212 	/* Init the interrupt coalescing */
1213 	fec_enet_itr_coal_init(ndev);
1214 
1215 }
1216 
1217 static int fec_enet_ipc_handle_init(struct fec_enet_private *fep)
1218 {
1219 	if (!(of_machine_is_compatible("fsl,imx8qm") ||
1220 	      of_machine_is_compatible("fsl,imx8qxp") ||
1221 	      of_machine_is_compatible("fsl,imx8dxl")))
1222 		return 0;
1223 
1224 	return imx_scu_get_handle(&fep->ipc_handle);
1225 }
1226 
1227 static void fec_enet_ipg_stop_set(struct fec_enet_private *fep, bool enabled)
1228 {
1229 	struct device_node *np = fep->pdev->dev.of_node;
1230 	u32 rsrc_id, val;
1231 	int idx;
1232 
1233 	if (!np || !fep->ipc_handle)
1234 		return;
1235 
1236 	idx = of_alias_get_id(np, "ethernet");
1237 	if (idx < 0)
1238 		idx = 0;
1239 	rsrc_id = idx ? IMX_SC_R_ENET_1 : IMX_SC_R_ENET_0;
1240 
1241 	val = enabled ? 1 : 0;
1242 	imx_sc_misc_set_control(fep->ipc_handle, rsrc_id, IMX_SC_C_IPG_STOP, val);
1243 }
1244 
1245 static void fec_enet_stop_mode(struct fec_enet_private *fep, bool enabled)
1246 {
1247 	struct fec_platform_data *pdata = fep->pdev->dev.platform_data;
1248 	struct fec_stop_mode_gpr *stop_gpr = &fep->stop_gpr;
1249 
1250 	if (stop_gpr->gpr) {
1251 		if (enabled)
1252 			regmap_update_bits(stop_gpr->gpr, stop_gpr->reg,
1253 					   BIT(stop_gpr->bit),
1254 					   BIT(stop_gpr->bit));
1255 		else
1256 			regmap_update_bits(stop_gpr->gpr, stop_gpr->reg,
1257 					   BIT(stop_gpr->bit), 0);
1258 	} else if (pdata && pdata->sleep_mode_enable) {
1259 		pdata->sleep_mode_enable(enabled);
1260 	} else {
1261 		fec_enet_ipg_stop_set(fep, enabled);
1262 	}
1263 }
1264 
1265 static void fec_irqs_disable(struct net_device *ndev)
1266 {
1267 	struct fec_enet_private *fep = netdev_priv(ndev);
1268 
1269 	writel(0, fep->hwp + FEC_IMASK);
1270 }
1271 
1272 static void fec_irqs_disable_except_wakeup(struct net_device *ndev)
1273 {
1274 	struct fec_enet_private *fep = netdev_priv(ndev);
1275 
1276 	writel(0, fep->hwp + FEC_IMASK);
1277 	writel(FEC_ENET_WAKEUP, fep->hwp + FEC_IMASK);
1278 }
1279 
1280 static void
1281 fec_stop(struct net_device *ndev)
1282 {
1283 	struct fec_enet_private *fep = netdev_priv(ndev);
1284 	u32 rmii_mode = readl(fep->hwp + FEC_R_CNTRL) & (1 << 8);
1285 	u32 val;
1286 
1287 	/* We cannot expect a graceful transmit stop without link !!! */
1288 	if (fep->link) {
1289 		writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */
1290 		udelay(10);
1291 		if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA))
1292 			netdev_err(ndev, "Graceful transmit stop did not complete!\n");
1293 	}
1294 
1295 	/* Whack a reset.  We should wait for this.
1296 	 * For i.MX6SX SOC, enet use AXI bus, we use disable MAC
1297 	 * instead of reset MAC itself.
1298 	 */
1299 	if (!(fep->wol_flag & FEC_WOL_FLAG_SLEEP_ON)) {
1300 		if (fep->quirks & FEC_QUIRK_HAS_MULTI_QUEUES) {
1301 			writel(0, fep->hwp + FEC_ECNTRL);
1302 		} else {
1303 			writel(1, fep->hwp + FEC_ECNTRL);
1304 			udelay(10);
1305 		}
1306 	} else {
1307 		val = readl(fep->hwp + FEC_ECNTRL);
1308 		val |= (FEC_ECR_MAGICEN | FEC_ECR_SLEEP);
1309 		writel(val, fep->hwp + FEC_ECNTRL);
1310 	}
1311 	writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
1312 	writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1313 
1314 	/* We have to keep ENET enabled to have MII interrupt stay working */
1315 	if (fep->quirks & FEC_QUIRK_ENET_MAC &&
1316 		!(fep->wol_flag & FEC_WOL_FLAG_SLEEP_ON)) {
1317 		writel(2, fep->hwp + FEC_ECNTRL);
1318 		writel(rmii_mode, fep->hwp + FEC_R_CNTRL);
1319 	}
1320 }
1321 
1322 
1323 static void
1324 fec_timeout(struct net_device *ndev, unsigned int txqueue)
1325 {
1326 	struct fec_enet_private *fep = netdev_priv(ndev);
1327 
1328 	fec_dump(ndev);
1329 
1330 	ndev->stats.tx_errors++;
1331 
1332 	schedule_work(&fep->tx_timeout_work);
1333 }
1334 
1335 static void fec_enet_timeout_work(struct work_struct *work)
1336 {
1337 	struct fec_enet_private *fep =
1338 		container_of(work, struct fec_enet_private, tx_timeout_work);
1339 	struct net_device *ndev = fep->netdev;
1340 
1341 	rtnl_lock();
1342 	if (netif_device_present(ndev) || netif_running(ndev)) {
1343 		napi_disable(&fep->napi);
1344 		netif_tx_lock_bh(ndev);
1345 		fec_restart(ndev);
1346 		netif_tx_wake_all_queues(ndev);
1347 		netif_tx_unlock_bh(ndev);
1348 		napi_enable(&fep->napi);
1349 	}
1350 	rtnl_unlock();
1351 }
1352 
1353 static void
1354 fec_enet_hwtstamp(struct fec_enet_private *fep, unsigned ts,
1355 	struct skb_shared_hwtstamps *hwtstamps)
1356 {
1357 	unsigned long flags;
1358 	u64 ns;
1359 
1360 	spin_lock_irqsave(&fep->tmreg_lock, flags);
1361 	ns = timecounter_cyc2time(&fep->tc, ts);
1362 	spin_unlock_irqrestore(&fep->tmreg_lock, flags);
1363 
1364 	memset(hwtstamps, 0, sizeof(*hwtstamps));
1365 	hwtstamps->hwtstamp = ns_to_ktime(ns);
1366 }
1367 
1368 static void
1369 fec_enet_tx_queue(struct net_device *ndev, u16 queue_id)
1370 {
1371 	struct	fec_enet_private *fep;
1372 	struct bufdesc *bdp;
1373 	unsigned short status;
1374 	struct	sk_buff	*skb;
1375 	struct fec_enet_priv_tx_q *txq;
1376 	struct netdev_queue *nq;
1377 	int	index = 0;
1378 	int	entries_free;
1379 
1380 	fep = netdev_priv(ndev);
1381 
1382 	txq = fep->tx_queue[queue_id];
1383 	/* get next bdp of dirty_tx */
1384 	nq = netdev_get_tx_queue(ndev, queue_id);
1385 	bdp = txq->dirty_tx;
1386 
1387 	/* get next bdp of dirty_tx */
1388 	bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
1389 
1390 	while (bdp != READ_ONCE(txq->bd.cur)) {
1391 		/* Order the load of bd.cur and cbd_sc */
1392 		rmb();
1393 		status = fec16_to_cpu(READ_ONCE(bdp->cbd_sc));
1394 		if (status & BD_ENET_TX_READY)
1395 			break;
1396 
1397 		index = fec_enet_get_bd_index(bdp, &txq->bd);
1398 
1399 		skb = txq->tx_skbuff[index];
1400 		txq->tx_skbuff[index] = NULL;
1401 		if (!IS_TSO_HEADER(txq, fec32_to_cpu(bdp->cbd_bufaddr)))
1402 			dma_unmap_single(&fep->pdev->dev,
1403 					 fec32_to_cpu(bdp->cbd_bufaddr),
1404 					 fec16_to_cpu(bdp->cbd_datlen),
1405 					 DMA_TO_DEVICE);
1406 		bdp->cbd_bufaddr = cpu_to_fec32(0);
1407 		if (!skb)
1408 			goto skb_done;
1409 
1410 		/* Check for errors. */
1411 		if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC |
1412 				   BD_ENET_TX_RL | BD_ENET_TX_UN |
1413 				   BD_ENET_TX_CSL)) {
1414 			ndev->stats.tx_errors++;
1415 			if (status & BD_ENET_TX_HB)  /* No heartbeat */
1416 				ndev->stats.tx_heartbeat_errors++;
1417 			if (status & BD_ENET_TX_LC)  /* Late collision */
1418 				ndev->stats.tx_window_errors++;
1419 			if (status & BD_ENET_TX_RL)  /* Retrans limit */
1420 				ndev->stats.tx_aborted_errors++;
1421 			if (status & BD_ENET_TX_UN)  /* Underrun */
1422 				ndev->stats.tx_fifo_errors++;
1423 			if (status & BD_ENET_TX_CSL) /* Carrier lost */
1424 				ndev->stats.tx_carrier_errors++;
1425 		} else {
1426 			ndev->stats.tx_packets++;
1427 			ndev->stats.tx_bytes += skb->len;
1428 		}
1429 
1430 		/* NOTE: SKBTX_IN_PROGRESS being set does not imply it's we who
1431 		 * are to time stamp the packet, so we still need to check time
1432 		 * stamping enabled flag.
1433 		 */
1434 		if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS &&
1435 			     fep->hwts_tx_en) &&
1436 		    fep->bufdesc_ex) {
1437 			struct skb_shared_hwtstamps shhwtstamps;
1438 			struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
1439 
1440 			fec_enet_hwtstamp(fep, fec32_to_cpu(ebdp->ts), &shhwtstamps);
1441 			skb_tstamp_tx(skb, &shhwtstamps);
1442 		}
1443 
1444 		/* Deferred means some collisions occurred during transmit,
1445 		 * but we eventually sent the packet OK.
1446 		 */
1447 		if (status & BD_ENET_TX_DEF)
1448 			ndev->stats.collisions++;
1449 
1450 		/* Free the sk buffer associated with this last transmit */
1451 		dev_kfree_skb_any(skb);
1452 skb_done:
1453 		/* Make sure the update to bdp and tx_skbuff are performed
1454 		 * before dirty_tx
1455 		 */
1456 		wmb();
1457 		txq->dirty_tx = bdp;
1458 
1459 		/* Update pointer to next buffer descriptor to be transmitted */
1460 		bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
1461 
1462 		/* Since we have freed up a buffer, the ring is no longer full
1463 		 */
1464 		if (netif_tx_queue_stopped(nq)) {
1465 			entries_free = fec_enet_get_free_txdesc_num(txq);
1466 			if (entries_free >= txq->tx_wake_threshold)
1467 				netif_tx_wake_queue(nq);
1468 		}
1469 	}
1470 
1471 	/* ERR006358: Keep the transmitter going */
1472 	if (bdp != txq->bd.cur &&
1473 	    readl(txq->bd.reg_desc_active) == 0)
1474 		writel(0, txq->bd.reg_desc_active);
1475 }
1476 
1477 static void fec_enet_tx(struct net_device *ndev)
1478 {
1479 	struct fec_enet_private *fep = netdev_priv(ndev);
1480 	int i;
1481 
1482 	/* Make sure that AVB queues are processed first. */
1483 	for (i = fep->num_tx_queues - 1; i >= 0; i--)
1484 		fec_enet_tx_queue(ndev, i);
1485 }
1486 
1487 static void fec_enet_update_cbd(struct fec_enet_priv_rx_q *rxq,
1488 				struct bufdesc *bdp, int index)
1489 {
1490 	struct page *new_page;
1491 	dma_addr_t phys_addr;
1492 
1493 	new_page = page_pool_dev_alloc_pages(rxq->page_pool);
1494 	WARN_ON(!new_page);
1495 	rxq->rx_skb_info[index].page = new_page;
1496 
1497 	rxq->rx_skb_info[index].offset = FEC_ENET_XDP_HEADROOM;
1498 	phys_addr = page_pool_get_dma_addr(new_page) + FEC_ENET_XDP_HEADROOM;
1499 	bdp->cbd_bufaddr = cpu_to_fec32(phys_addr);
1500 }
1501 
1502 /* During a receive, the bd_rx.cur points to the current incoming buffer.
1503  * When we update through the ring, if the next incoming buffer has
1504  * not been given to the system, we just set the empty indicator,
1505  * effectively tossing the packet.
1506  */
1507 static int
1508 fec_enet_rx_queue(struct net_device *ndev, int budget, u16 queue_id)
1509 {
1510 	struct fec_enet_private *fep = netdev_priv(ndev);
1511 	struct fec_enet_priv_rx_q *rxq;
1512 	struct bufdesc *bdp;
1513 	unsigned short status;
1514 	struct  sk_buff *skb;
1515 	ushort	pkt_len;
1516 	__u8 *data;
1517 	int	pkt_received = 0;
1518 	struct	bufdesc_ex *ebdp = NULL;
1519 	bool	vlan_packet_rcvd = false;
1520 	u16	vlan_tag;
1521 	int	index = 0;
1522 	bool	need_swap = fep->quirks & FEC_QUIRK_SWAP_FRAME;
1523 	struct page *page;
1524 
1525 #ifdef CONFIG_M532x
1526 	flush_cache_all();
1527 #endif
1528 	rxq = fep->rx_queue[queue_id];
1529 
1530 	/* First, grab all of the stats for the incoming packet.
1531 	 * These get messed up if we get called due to a busy condition.
1532 	 */
1533 	bdp = rxq->bd.cur;
1534 
1535 	while (!((status = fec16_to_cpu(bdp->cbd_sc)) & BD_ENET_RX_EMPTY)) {
1536 
1537 		if (pkt_received >= budget)
1538 			break;
1539 		pkt_received++;
1540 
1541 		writel(FEC_ENET_RXF_GET(queue_id), fep->hwp + FEC_IEVENT);
1542 
1543 		/* Check for errors. */
1544 		status ^= BD_ENET_RX_LAST;
1545 		if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
1546 			   BD_ENET_RX_CR | BD_ENET_RX_OV | BD_ENET_RX_LAST |
1547 			   BD_ENET_RX_CL)) {
1548 			ndev->stats.rx_errors++;
1549 			if (status & BD_ENET_RX_OV) {
1550 				/* FIFO overrun */
1551 				ndev->stats.rx_fifo_errors++;
1552 				goto rx_processing_done;
1553 			}
1554 			if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH
1555 						| BD_ENET_RX_LAST)) {
1556 				/* Frame too long or too short. */
1557 				ndev->stats.rx_length_errors++;
1558 				if (status & BD_ENET_RX_LAST)
1559 					netdev_err(ndev, "rcv is not +last\n");
1560 			}
1561 			if (status & BD_ENET_RX_CR)	/* CRC Error */
1562 				ndev->stats.rx_crc_errors++;
1563 			/* Report late collisions as a frame error. */
1564 			if (status & (BD_ENET_RX_NO | BD_ENET_RX_CL))
1565 				ndev->stats.rx_frame_errors++;
1566 			goto rx_processing_done;
1567 		}
1568 
1569 		/* Process the incoming frame. */
1570 		ndev->stats.rx_packets++;
1571 		pkt_len = fec16_to_cpu(bdp->cbd_datlen);
1572 		ndev->stats.rx_bytes += pkt_len;
1573 
1574 		index = fec_enet_get_bd_index(bdp, &rxq->bd);
1575 		page = rxq->rx_skb_info[index].page;
1576 		dma_sync_single_for_cpu(&fep->pdev->dev,
1577 					fec32_to_cpu(bdp->cbd_bufaddr),
1578 					pkt_len,
1579 					DMA_FROM_DEVICE);
1580 		prefetch(page_address(page));
1581 		fec_enet_update_cbd(rxq, bdp, index);
1582 
1583 		/* The packet length includes FCS, but we don't want to
1584 		 * include that when passing upstream as it messes up
1585 		 * bridging applications.
1586 		 */
1587 		skb = build_skb(page_address(page), PAGE_SIZE);
1588 		skb_reserve(skb, FEC_ENET_XDP_HEADROOM);
1589 		skb_put(skb, pkt_len - 4);
1590 		skb_mark_for_recycle(skb);
1591 		data = skb->data;
1592 
1593 		if (need_swap)
1594 			swap_buffer(data, pkt_len);
1595 
1596 #if !defined(CONFIG_M5272)
1597 		if (fep->quirks & FEC_QUIRK_HAS_RACC)
1598 			data = skb_pull_inline(skb, 2);
1599 #endif
1600 
1601 		/* Extract the enhanced buffer descriptor */
1602 		ebdp = NULL;
1603 		if (fep->bufdesc_ex)
1604 			ebdp = (struct bufdesc_ex *)bdp;
1605 
1606 		/* If this is a VLAN packet remove the VLAN Tag */
1607 		vlan_packet_rcvd = false;
1608 		if ((ndev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
1609 		    fep->bufdesc_ex &&
1610 		    (ebdp->cbd_esc & cpu_to_fec32(BD_ENET_RX_VLAN))) {
1611 			/* Push and remove the vlan tag */
1612 			struct vlan_hdr *vlan_header =
1613 					(struct vlan_hdr *) (data + ETH_HLEN);
1614 			vlan_tag = ntohs(vlan_header->h_vlan_TCI);
1615 
1616 			vlan_packet_rcvd = true;
1617 
1618 			memmove(skb->data + VLAN_HLEN, data, ETH_ALEN * 2);
1619 			skb_pull(skb, VLAN_HLEN);
1620 		}
1621 
1622 		skb->protocol = eth_type_trans(skb, ndev);
1623 
1624 		/* Get receive timestamp from the skb */
1625 		if (fep->hwts_rx_en && fep->bufdesc_ex)
1626 			fec_enet_hwtstamp(fep, fec32_to_cpu(ebdp->ts),
1627 					  skb_hwtstamps(skb));
1628 
1629 		if (fep->bufdesc_ex &&
1630 		    (fep->csum_flags & FLAG_RX_CSUM_ENABLED)) {
1631 			if (!(ebdp->cbd_esc & cpu_to_fec32(FLAG_RX_CSUM_ERROR))) {
1632 				/* don't check it */
1633 				skb->ip_summed = CHECKSUM_UNNECESSARY;
1634 			} else {
1635 				skb_checksum_none_assert(skb);
1636 			}
1637 		}
1638 
1639 		/* Handle received VLAN packets */
1640 		if (vlan_packet_rcvd)
1641 			__vlan_hwaccel_put_tag(skb,
1642 					       htons(ETH_P_8021Q),
1643 					       vlan_tag);
1644 
1645 		skb_record_rx_queue(skb, queue_id);
1646 		napi_gro_receive(&fep->napi, skb);
1647 
1648 rx_processing_done:
1649 		/* Clear the status flags for this buffer */
1650 		status &= ~BD_ENET_RX_STATS;
1651 
1652 		/* Mark the buffer empty */
1653 		status |= BD_ENET_RX_EMPTY;
1654 
1655 		if (fep->bufdesc_ex) {
1656 			struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
1657 
1658 			ebdp->cbd_esc = cpu_to_fec32(BD_ENET_RX_INT);
1659 			ebdp->cbd_prot = 0;
1660 			ebdp->cbd_bdu = 0;
1661 		}
1662 		/* Make sure the updates to rest of the descriptor are
1663 		 * performed before transferring ownership.
1664 		 */
1665 		wmb();
1666 		bdp->cbd_sc = cpu_to_fec16(status);
1667 
1668 		/* Update BD pointer to next entry */
1669 		bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
1670 
1671 		/* Doing this here will keep the FEC running while we process
1672 		 * incoming frames.  On a heavily loaded network, we should be
1673 		 * able to keep up at the expense of system resources.
1674 		 */
1675 		writel(0, rxq->bd.reg_desc_active);
1676 	}
1677 	rxq->bd.cur = bdp;
1678 	return pkt_received;
1679 }
1680 
1681 static int fec_enet_rx(struct net_device *ndev, int budget)
1682 {
1683 	struct fec_enet_private *fep = netdev_priv(ndev);
1684 	int i, done = 0;
1685 
1686 	/* Make sure that AVB queues are processed first. */
1687 	for (i = fep->num_rx_queues - 1; i >= 0; i--)
1688 		done += fec_enet_rx_queue(ndev, budget - done, i);
1689 
1690 	return done;
1691 }
1692 
1693 static bool fec_enet_collect_events(struct fec_enet_private *fep)
1694 {
1695 	uint int_events;
1696 
1697 	int_events = readl(fep->hwp + FEC_IEVENT);
1698 
1699 	/* Don't clear MDIO events, we poll for those */
1700 	int_events &= ~FEC_ENET_MII;
1701 
1702 	writel(int_events, fep->hwp + FEC_IEVENT);
1703 
1704 	return int_events != 0;
1705 }
1706 
1707 static irqreturn_t
1708 fec_enet_interrupt(int irq, void *dev_id)
1709 {
1710 	struct net_device *ndev = dev_id;
1711 	struct fec_enet_private *fep = netdev_priv(ndev);
1712 	irqreturn_t ret = IRQ_NONE;
1713 
1714 	if (fec_enet_collect_events(fep) && fep->link) {
1715 		ret = IRQ_HANDLED;
1716 
1717 		if (napi_schedule_prep(&fep->napi)) {
1718 			/* Disable interrupts */
1719 			writel(0, fep->hwp + FEC_IMASK);
1720 			__napi_schedule(&fep->napi);
1721 		}
1722 	}
1723 
1724 	return ret;
1725 }
1726 
1727 static int fec_enet_rx_napi(struct napi_struct *napi, int budget)
1728 {
1729 	struct net_device *ndev = napi->dev;
1730 	struct fec_enet_private *fep = netdev_priv(ndev);
1731 	int done = 0;
1732 
1733 	do {
1734 		done += fec_enet_rx(ndev, budget - done);
1735 		fec_enet_tx(ndev);
1736 	} while ((done < budget) && fec_enet_collect_events(fep));
1737 
1738 	if (done < budget) {
1739 		napi_complete_done(napi, done);
1740 		writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1741 	}
1742 
1743 	return done;
1744 }
1745 
1746 /* ------------------------------------------------------------------------- */
1747 static int fec_get_mac(struct net_device *ndev)
1748 {
1749 	struct fec_enet_private *fep = netdev_priv(ndev);
1750 	unsigned char *iap, tmpaddr[ETH_ALEN];
1751 	int ret;
1752 
1753 	/*
1754 	 * try to get mac address in following order:
1755 	 *
1756 	 * 1) module parameter via kernel command line in form
1757 	 *    fec.macaddr=0x00,0x04,0x9f,0x01,0x30,0xe0
1758 	 */
1759 	iap = macaddr;
1760 
1761 	/*
1762 	 * 2) from device tree data
1763 	 */
1764 	if (!is_valid_ether_addr(iap)) {
1765 		struct device_node *np = fep->pdev->dev.of_node;
1766 		if (np) {
1767 			ret = of_get_mac_address(np, tmpaddr);
1768 			if (!ret)
1769 				iap = tmpaddr;
1770 			else if (ret == -EPROBE_DEFER)
1771 				return ret;
1772 		}
1773 	}
1774 
1775 	/*
1776 	 * 3) from flash or fuse (via platform data)
1777 	 */
1778 	if (!is_valid_ether_addr(iap)) {
1779 #ifdef CONFIG_M5272
1780 		if (FEC_FLASHMAC)
1781 			iap = (unsigned char *)FEC_FLASHMAC;
1782 #else
1783 		struct fec_platform_data *pdata = dev_get_platdata(&fep->pdev->dev);
1784 
1785 		if (pdata)
1786 			iap = (unsigned char *)&pdata->mac;
1787 #endif
1788 	}
1789 
1790 	/*
1791 	 * 4) FEC mac registers set by bootloader
1792 	 */
1793 	if (!is_valid_ether_addr(iap)) {
1794 		*((__be32 *) &tmpaddr[0]) =
1795 			cpu_to_be32(readl(fep->hwp + FEC_ADDR_LOW));
1796 		*((__be16 *) &tmpaddr[4]) =
1797 			cpu_to_be16(readl(fep->hwp + FEC_ADDR_HIGH) >> 16);
1798 		iap = &tmpaddr[0];
1799 	}
1800 
1801 	/*
1802 	 * 5) random mac address
1803 	 */
1804 	if (!is_valid_ether_addr(iap)) {
1805 		/* Report it and use a random ethernet address instead */
1806 		dev_err(&fep->pdev->dev, "Invalid MAC address: %pM\n", iap);
1807 		eth_hw_addr_random(ndev);
1808 		dev_info(&fep->pdev->dev, "Using random MAC address: %pM\n",
1809 			 ndev->dev_addr);
1810 		return 0;
1811 	}
1812 
1813 	/* Adjust MAC if using macaddr */
1814 	eth_hw_addr_gen(ndev, iap, iap == macaddr ? fep->dev_id : 0);
1815 
1816 	return 0;
1817 }
1818 
1819 /* ------------------------------------------------------------------------- */
1820 
1821 /*
1822  * Phy section
1823  */
1824 static void fec_enet_adjust_link(struct net_device *ndev)
1825 {
1826 	struct fec_enet_private *fep = netdev_priv(ndev);
1827 	struct phy_device *phy_dev = ndev->phydev;
1828 	int status_change = 0;
1829 
1830 	/*
1831 	 * If the netdev is down, or is going down, we're not interested
1832 	 * in link state events, so just mark our idea of the link as down
1833 	 * and ignore the event.
1834 	 */
1835 	if (!netif_running(ndev) || !netif_device_present(ndev)) {
1836 		fep->link = 0;
1837 	} else if (phy_dev->link) {
1838 		if (!fep->link) {
1839 			fep->link = phy_dev->link;
1840 			status_change = 1;
1841 		}
1842 
1843 		if (fep->full_duplex != phy_dev->duplex) {
1844 			fep->full_duplex = phy_dev->duplex;
1845 			status_change = 1;
1846 		}
1847 
1848 		if (phy_dev->speed != fep->speed) {
1849 			fep->speed = phy_dev->speed;
1850 			status_change = 1;
1851 		}
1852 
1853 		/* if any of the above changed restart the FEC */
1854 		if (status_change) {
1855 			napi_disable(&fep->napi);
1856 			netif_tx_lock_bh(ndev);
1857 			fec_restart(ndev);
1858 			netif_tx_wake_all_queues(ndev);
1859 			netif_tx_unlock_bh(ndev);
1860 			napi_enable(&fep->napi);
1861 		}
1862 	} else {
1863 		if (fep->link) {
1864 			napi_disable(&fep->napi);
1865 			netif_tx_lock_bh(ndev);
1866 			fec_stop(ndev);
1867 			netif_tx_unlock_bh(ndev);
1868 			napi_enable(&fep->napi);
1869 			fep->link = phy_dev->link;
1870 			status_change = 1;
1871 		}
1872 	}
1873 
1874 	if (status_change)
1875 		phy_print_status(phy_dev);
1876 }
1877 
1878 static int fec_enet_mdio_wait(struct fec_enet_private *fep)
1879 {
1880 	uint ievent;
1881 	int ret;
1882 
1883 	ret = readl_poll_timeout_atomic(fep->hwp + FEC_IEVENT, ievent,
1884 					ievent & FEC_ENET_MII, 2, 30000);
1885 
1886 	if (!ret)
1887 		writel(FEC_ENET_MII, fep->hwp + FEC_IEVENT);
1888 
1889 	return ret;
1890 }
1891 
1892 static int fec_enet_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
1893 {
1894 	struct fec_enet_private *fep = bus->priv;
1895 	struct device *dev = &fep->pdev->dev;
1896 	int ret = 0, frame_start, frame_addr, frame_op;
1897 	bool is_c45 = !!(regnum & MII_ADDR_C45);
1898 
1899 	ret = pm_runtime_resume_and_get(dev);
1900 	if (ret < 0)
1901 		return ret;
1902 
1903 	if (is_c45) {
1904 		frame_start = FEC_MMFR_ST_C45;
1905 
1906 		/* write address */
1907 		frame_addr = (regnum >> 16);
1908 		writel(frame_start | FEC_MMFR_OP_ADDR_WRITE |
1909 		       FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(frame_addr) |
1910 		       FEC_MMFR_TA | (regnum & 0xFFFF),
1911 		       fep->hwp + FEC_MII_DATA);
1912 
1913 		/* wait for end of transfer */
1914 		ret = fec_enet_mdio_wait(fep);
1915 		if (ret) {
1916 			netdev_err(fep->netdev, "MDIO address write timeout\n");
1917 			goto out;
1918 		}
1919 
1920 		frame_op = FEC_MMFR_OP_READ_C45;
1921 
1922 	} else {
1923 		/* C22 read */
1924 		frame_op = FEC_MMFR_OP_READ;
1925 		frame_start = FEC_MMFR_ST;
1926 		frame_addr = regnum;
1927 	}
1928 
1929 	/* start a read op */
1930 	writel(frame_start | frame_op |
1931 		FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(frame_addr) |
1932 		FEC_MMFR_TA, fep->hwp + FEC_MII_DATA);
1933 
1934 	/* wait for end of transfer */
1935 	ret = fec_enet_mdio_wait(fep);
1936 	if (ret) {
1937 		netdev_err(fep->netdev, "MDIO read timeout\n");
1938 		goto out;
1939 	}
1940 
1941 	ret = FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA));
1942 
1943 out:
1944 	pm_runtime_mark_last_busy(dev);
1945 	pm_runtime_put_autosuspend(dev);
1946 
1947 	return ret;
1948 }
1949 
1950 static int fec_enet_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
1951 			   u16 value)
1952 {
1953 	struct fec_enet_private *fep = bus->priv;
1954 	struct device *dev = &fep->pdev->dev;
1955 	int ret, frame_start, frame_addr;
1956 	bool is_c45 = !!(regnum & MII_ADDR_C45);
1957 
1958 	ret = pm_runtime_resume_and_get(dev);
1959 	if (ret < 0)
1960 		return ret;
1961 
1962 	if (is_c45) {
1963 		frame_start = FEC_MMFR_ST_C45;
1964 
1965 		/* write address */
1966 		frame_addr = (regnum >> 16);
1967 		writel(frame_start | FEC_MMFR_OP_ADDR_WRITE |
1968 		       FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(frame_addr) |
1969 		       FEC_MMFR_TA | (regnum & 0xFFFF),
1970 		       fep->hwp + FEC_MII_DATA);
1971 
1972 		/* wait for end of transfer */
1973 		ret = fec_enet_mdio_wait(fep);
1974 		if (ret) {
1975 			netdev_err(fep->netdev, "MDIO address write timeout\n");
1976 			goto out;
1977 		}
1978 	} else {
1979 		/* C22 write */
1980 		frame_start = FEC_MMFR_ST;
1981 		frame_addr = regnum;
1982 	}
1983 
1984 	/* start a write op */
1985 	writel(frame_start | FEC_MMFR_OP_WRITE |
1986 		FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(frame_addr) |
1987 		FEC_MMFR_TA | FEC_MMFR_DATA(value),
1988 		fep->hwp + FEC_MII_DATA);
1989 
1990 	/* wait for end of transfer */
1991 	ret = fec_enet_mdio_wait(fep);
1992 	if (ret)
1993 		netdev_err(fep->netdev, "MDIO write timeout\n");
1994 
1995 out:
1996 	pm_runtime_mark_last_busy(dev);
1997 	pm_runtime_put_autosuspend(dev);
1998 
1999 	return ret;
2000 }
2001 
2002 static void fec_enet_phy_reset_after_clk_enable(struct net_device *ndev)
2003 {
2004 	struct fec_enet_private *fep = netdev_priv(ndev);
2005 	struct phy_device *phy_dev = ndev->phydev;
2006 
2007 	if (phy_dev) {
2008 		phy_reset_after_clk_enable(phy_dev);
2009 	} else if (fep->phy_node) {
2010 		/*
2011 		 * If the PHY still is not bound to the MAC, but there is
2012 		 * OF PHY node and a matching PHY device instance already,
2013 		 * use the OF PHY node to obtain the PHY device instance,
2014 		 * and then use that PHY device instance when triggering
2015 		 * the PHY reset.
2016 		 */
2017 		phy_dev = of_phy_find_device(fep->phy_node);
2018 		phy_reset_after_clk_enable(phy_dev);
2019 		put_device(&phy_dev->mdio.dev);
2020 	}
2021 }
2022 
2023 static int fec_enet_clk_enable(struct net_device *ndev, bool enable)
2024 {
2025 	struct fec_enet_private *fep = netdev_priv(ndev);
2026 	int ret;
2027 
2028 	if (enable) {
2029 		ret = clk_prepare_enable(fep->clk_enet_out);
2030 		if (ret)
2031 			return ret;
2032 
2033 		if (fep->clk_ptp) {
2034 			mutex_lock(&fep->ptp_clk_mutex);
2035 			ret = clk_prepare_enable(fep->clk_ptp);
2036 			if (ret) {
2037 				mutex_unlock(&fep->ptp_clk_mutex);
2038 				goto failed_clk_ptp;
2039 			} else {
2040 				fep->ptp_clk_on = true;
2041 			}
2042 			mutex_unlock(&fep->ptp_clk_mutex);
2043 		}
2044 
2045 		ret = clk_prepare_enable(fep->clk_ref);
2046 		if (ret)
2047 			goto failed_clk_ref;
2048 
2049 		ret = clk_prepare_enable(fep->clk_2x_txclk);
2050 		if (ret)
2051 			goto failed_clk_2x_txclk;
2052 
2053 		fec_enet_phy_reset_after_clk_enable(ndev);
2054 	} else {
2055 		clk_disable_unprepare(fep->clk_enet_out);
2056 		if (fep->clk_ptp) {
2057 			mutex_lock(&fep->ptp_clk_mutex);
2058 			clk_disable_unprepare(fep->clk_ptp);
2059 			fep->ptp_clk_on = false;
2060 			mutex_unlock(&fep->ptp_clk_mutex);
2061 		}
2062 		clk_disable_unprepare(fep->clk_ref);
2063 		clk_disable_unprepare(fep->clk_2x_txclk);
2064 	}
2065 
2066 	return 0;
2067 
2068 failed_clk_2x_txclk:
2069 	if (fep->clk_ref)
2070 		clk_disable_unprepare(fep->clk_ref);
2071 failed_clk_ref:
2072 	if (fep->clk_ptp) {
2073 		mutex_lock(&fep->ptp_clk_mutex);
2074 		clk_disable_unprepare(fep->clk_ptp);
2075 		fep->ptp_clk_on = false;
2076 		mutex_unlock(&fep->ptp_clk_mutex);
2077 	}
2078 failed_clk_ptp:
2079 	clk_disable_unprepare(fep->clk_enet_out);
2080 
2081 	return ret;
2082 }
2083 
2084 static int fec_enet_parse_rgmii_delay(struct fec_enet_private *fep,
2085 				      struct device_node *np)
2086 {
2087 	u32 rgmii_tx_delay, rgmii_rx_delay;
2088 
2089 	/* For rgmii tx internal delay, valid values are 0ps and 2000ps */
2090 	if (!of_property_read_u32(np, "tx-internal-delay-ps", &rgmii_tx_delay)) {
2091 		if (rgmii_tx_delay != 0 && rgmii_tx_delay != 2000) {
2092 			dev_err(&fep->pdev->dev, "The only allowed RGMII TX delay values are: 0ps, 2000ps");
2093 			return -EINVAL;
2094 		} else if (rgmii_tx_delay == 2000) {
2095 			fep->rgmii_txc_dly = true;
2096 		}
2097 	}
2098 
2099 	/* For rgmii rx internal delay, valid values are 0ps and 2000ps */
2100 	if (!of_property_read_u32(np, "rx-internal-delay-ps", &rgmii_rx_delay)) {
2101 		if (rgmii_rx_delay != 0 && rgmii_rx_delay != 2000) {
2102 			dev_err(&fep->pdev->dev, "The only allowed RGMII RX delay values are: 0ps, 2000ps");
2103 			return -EINVAL;
2104 		} else if (rgmii_rx_delay == 2000) {
2105 			fep->rgmii_rxc_dly = true;
2106 		}
2107 	}
2108 
2109 	return 0;
2110 }
2111 
2112 static int fec_enet_mii_probe(struct net_device *ndev)
2113 {
2114 	struct fec_enet_private *fep = netdev_priv(ndev);
2115 	struct phy_device *phy_dev = NULL;
2116 	char mdio_bus_id[MII_BUS_ID_SIZE];
2117 	char phy_name[MII_BUS_ID_SIZE + 3];
2118 	int phy_id;
2119 	int dev_id = fep->dev_id;
2120 
2121 	if (fep->phy_node) {
2122 		phy_dev = of_phy_connect(ndev, fep->phy_node,
2123 					 &fec_enet_adjust_link, 0,
2124 					 fep->phy_interface);
2125 		if (!phy_dev) {
2126 			netdev_err(ndev, "Unable to connect to phy\n");
2127 			return -ENODEV;
2128 		}
2129 	} else {
2130 		/* check for attached phy */
2131 		for (phy_id = 0; (phy_id < PHY_MAX_ADDR); phy_id++) {
2132 			if (!mdiobus_is_registered_device(fep->mii_bus, phy_id))
2133 				continue;
2134 			if (dev_id--)
2135 				continue;
2136 			strscpy(mdio_bus_id, fep->mii_bus->id, MII_BUS_ID_SIZE);
2137 			break;
2138 		}
2139 
2140 		if (phy_id >= PHY_MAX_ADDR) {
2141 			netdev_info(ndev, "no PHY, assuming direct connection to switch\n");
2142 			strscpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE);
2143 			phy_id = 0;
2144 		}
2145 
2146 		snprintf(phy_name, sizeof(phy_name),
2147 			 PHY_ID_FMT, mdio_bus_id, phy_id);
2148 		phy_dev = phy_connect(ndev, phy_name, &fec_enet_adjust_link,
2149 				      fep->phy_interface);
2150 	}
2151 
2152 	if (IS_ERR(phy_dev)) {
2153 		netdev_err(ndev, "could not attach to PHY\n");
2154 		return PTR_ERR(phy_dev);
2155 	}
2156 
2157 	/* mask with MAC supported features */
2158 	if (fep->quirks & FEC_QUIRK_HAS_GBIT) {
2159 		phy_set_max_speed(phy_dev, 1000);
2160 		phy_remove_link_mode(phy_dev,
2161 				     ETHTOOL_LINK_MODE_1000baseT_Half_BIT);
2162 #if !defined(CONFIG_M5272)
2163 		phy_support_sym_pause(phy_dev);
2164 #endif
2165 	}
2166 	else
2167 		phy_set_max_speed(phy_dev, 100);
2168 
2169 	fep->link = 0;
2170 	fep->full_duplex = 0;
2171 
2172 	phy_dev->mac_managed_pm = 1;
2173 
2174 	phy_attached_info(phy_dev);
2175 
2176 	return 0;
2177 }
2178 
2179 static int fec_enet_mii_init(struct platform_device *pdev)
2180 {
2181 	static struct mii_bus *fec0_mii_bus;
2182 	struct net_device *ndev = platform_get_drvdata(pdev);
2183 	struct fec_enet_private *fep = netdev_priv(ndev);
2184 	bool suppress_preamble = false;
2185 	struct device_node *node;
2186 	int err = -ENXIO;
2187 	u32 mii_speed, holdtime;
2188 	u32 bus_freq;
2189 
2190 	/*
2191 	 * The i.MX28 dual fec interfaces are not equal.
2192 	 * Here are the differences:
2193 	 *
2194 	 *  - fec0 supports MII & RMII modes while fec1 only supports RMII
2195 	 *  - fec0 acts as the 1588 time master while fec1 is slave
2196 	 *  - external phys can only be configured by fec0
2197 	 *
2198 	 * That is to say fec1 can not work independently. It only works
2199 	 * when fec0 is working. The reason behind this design is that the
2200 	 * second interface is added primarily for Switch mode.
2201 	 *
2202 	 * Because of the last point above, both phys are attached on fec0
2203 	 * mdio interface in board design, and need to be configured by
2204 	 * fec0 mii_bus.
2205 	 */
2206 	if ((fep->quirks & FEC_QUIRK_SINGLE_MDIO) && fep->dev_id > 0) {
2207 		/* fec1 uses fec0 mii_bus */
2208 		if (mii_cnt && fec0_mii_bus) {
2209 			fep->mii_bus = fec0_mii_bus;
2210 			mii_cnt++;
2211 			return 0;
2212 		}
2213 		return -ENOENT;
2214 	}
2215 
2216 	bus_freq = 2500000; /* 2.5MHz by default */
2217 	node = of_get_child_by_name(pdev->dev.of_node, "mdio");
2218 	if (node) {
2219 		of_property_read_u32(node, "clock-frequency", &bus_freq);
2220 		suppress_preamble = of_property_read_bool(node,
2221 							  "suppress-preamble");
2222 	}
2223 
2224 	/*
2225 	 * Set MII speed (= clk_get_rate() / 2 * phy_speed)
2226 	 *
2227 	 * The formula for FEC MDC is 'ref_freq / (MII_SPEED x 2)' while
2228 	 * for ENET-MAC is 'ref_freq / ((MII_SPEED + 1) x 2)'.  The i.MX28
2229 	 * Reference Manual has an error on this, and gets fixed on i.MX6Q
2230 	 * document.
2231 	 */
2232 	mii_speed = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), bus_freq * 2);
2233 	if (fep->quirks & FEC_QUIRK_ENET_MAC)
2234 		mii_speed--;
2235 	if (mii_speed > 63) {
2236 		dev_err(&pdev->dev,
2237 			"fec clock (%lu) too fast to get right mii speed\n",
2238 			clk_get_rate(fep->clk_ipg));
2239 		err = -EINVAL;
2240 		goto err_out;
2241 	}
2242 
2243 	/*
2244 	 * The i.MX28 and i.MX6 types have another filed in the MSCR (aka
2245 	 * MII_SPEED) register that defines the MDIO output hold time. Earlier
2246 	 * versions are RAZ there, so just ignore the difference and write the
2247 	 * register always.
2248 	 * The minimal hold time according to IEE802.3 (clause 22) is 10 ns.
2249 	 * HOLDTIME + 1 is the number of clk cycles the fec is holding the
2250 	 * output.
2251 	 * The HOLDTIME bitfield takes values between 0 and 7 (inclusive).
2252 	 * Given that ceil(clkrate / 5000000) <= 64, the calculation for
2253 	 * holdtime cannot result in a value greater than 3.
2254 	 */
2255 	holdtime = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), 100000000) - 1;
2256 
2257 	fep->phy_speed = mii_speed << 1 | holdtime << 8;
2258 
2259 	if (suppress_preamble)
2260 		fep->phy_speed |= BIT(7);
2261 
2262 	if (fep->quirks & FEC_QUIRK_CLEAR_SETUP_MII) {
2263 		/* Clear MMFR to avoid to generate MII event by writing MSCR.
2264 		 * MII event generation condition:
2265 		 * - writing MSCR:
2266 		 *	- mmfr[31:0]_not_zero & mscr[7:0]_is_zero &
2267 		 *	  mscr_reg_data_in[7:0] != 0
2268 		 * - writing MMFR:
2269 		 *	- mscr[7:0]_not_zero
2270 		 */
2271 		writel(0, fep->hwp + FEC_MII_DATA);
2272 	}
2273 
2274 	writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
2275 
2276 	/* Clear any pending transaction complete indication */
2277 	writel(FEC_ENET_MII, fep->hwp + FEC_IEVENT);
2278 
2279 	fep->mii_bus = mdiobus_alloc();
2280 	if (fep->mii_bus == NULL) {
2281 		err = -ENOMEM;
2282 		goto err_out;
2283 	}
2284 
2285 	fep->mii_bus->name = "fec_enet_mii_bus";
2286 	fep->mii_bus->read = fec_enet_mdio_read;
2287 	fep->mii_bus->write = fec_enet_mdio_write;
2288 	snprintf(fep->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
2289 		pdev->name, fep->dev_id + 1);
2290 	fep->mii_bus->priv = fep;
2291 	fep->mii_bus->parent = &pdev->dev;
2292 
2293 	err = of_mdiobus_register(fep->mii_bus, node);
2294 	if (err)
2295 		goto err_out_free_mdiobus;
2296 	of_node_put(node);
2297 
2298 	mii_cnt++;
2299 
2300 	/* save fec0 mii_bus */
2301 	if (fep->quirks & FEC_QUIRK_SINGLE_MDIO)
2302 		fec0_mii_bus = fep->mii_bus;
2303 
2304 	return 0;
2305 
2306 err_out_free_mdiobus:
2307 	mdiobus_free(fep->mii_bus);
2308 err_out:
2309 	of_node_put(node);
2310 	return err;
2311 }
2312 
2313 static void fec_enet_mii_remove(struct fec_enet_private *fep)
2314 {
2315 	if (--mii_cnt == 0) {
2316 		mdiobus_unregister(fep->mii_bus);
2317 		mdiobus_free(fep->mii_bus);
2318 	}
2319 }
2320 
2321 static void fec_enet_get_drvinfo(struct net_device *ndev,
2322 				 struct ethtool_drvinfo *info)
2323 {
2324 	struct fec_enet_private *fep = netdev_priv(ndev);
2325 
2326 	strscpy(info->driver, fep->pdev->dev.driver->name,
2327 		sizeof(info->driver));
2328 	strscpy(info->bus_info, dev_name(&ndev->dev), sizeof(info->bus_info));
2329 }
2330 
2331 static int fec_enet_get_regs_len(struct net_device *ndev)
2332 {
2333 	struct fec_enet_private *fep = netdev_priv(ndev);
2334 	struct resource *r;
2335 	int s = 0;
2336 
2337 	r = platform_get_resource(fep->pdev, IORESOURCE_MEM, 0);
2338 	if (r)
2339 		s = resource_size(r);
2340 
2341 	return s;
2342 }
2343 
2344 /* List of registers that can be safety be read to dump them with ethtool */
2345 #if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
2346 	defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM) || \
2347 	defined(CONFIG_ARM64) || defined(CONFIG_COMPILE_TEST)
2348 static __u32 fec_enet_register_version = 2;
2349 static u32 fec_enet_register_offset[] = {
2350 	FEC_IEVENT, FEC_IMASK, FEC_R_DES_ACTIVE_0, FEC_X_DES_ACTIVE_0,
2351 	FEC_ECNTRL, FEC_MII_DATA, FEC_MII_SPEED, FEC_MIB_CTRLSTAT, FEC_R_CNTRL,
2352 	FEC_X_CNTRL, FEC_ADDR_LOW, FEC_ADDR_HIGH, FEC_OPD, FEC_TXIC0, FEC_TXIC1,
2353 	FEC_TXIC2, FEC_RXIC0, FEC_RXIC1, FEC_RXIC2, FEC_HASH_TABLE_HIGH,
2354 	FEC_HASH_TABLE_LOW, FEC_GRP_HASH_TABLE_HIGH, FEC_GRP_HASH_TABLE_LOW,
2355 	FEC_X_WMRK, FEC_R_BOUND, FEC_R_FSTART, FEC_R_DES_START_1,
2356 	FEC_X_DES_START_1, FEC_R_BUFF_SIZE_1, FEC_R_DES_START_2,
2357 	FEC_X_DES_START_2, FEC_R_BUFF_SIZE_2, FEC_R_DES_START_0,
2358 	FEC_X_DES_START_0, FEC_R_BUFF_SIZE_0, FEC_R_FIFO_RSFL, FEC_R_FIFO_RSEM,
2359 	FEC_R_FIFO_RAEM, FEC_R_FIFO_RAFL, FEC_RACC, FEC_RCMR_1, FEC_RCMR_2,
2360 	FEC_DMA_CFG_1, FEC_DMA_CFG_2, FEC_R_DES_ACTIVE_1, FEC_X_DES_ACTIVE_1,
2361 	FEC_R_DES_ACTIVE_2, FEC_X_DES_ACTIVE_2, FEC_QOS_SCHEME,
2362 	RMON_T_DROP, RMON_T_PACKETS, RMON_T_BC_PKT, RMON_T_MC_PKT,
2363 	RMON_T_CRC_ALIGN, RMON_T_UNDERSIZE, RMON_T_OVERSIZE, RMON_T_FRAG,
2364 	RMON_T_JAB, RMON_T_COL, RMON_T_P64, RMON_T_P65TO127, RMON_T_P128TO255,
2365 	RMON_T_P256TO511, RMON_T_P512TO1023, RMON_T_P1024TO2047,
2366 	RMON_T_P_GTE2048, RMON_T_OCTETS,
2367 	IEEE_T_DROP, IEEE_T_FRAME_OK, IEEE_T_1COL, IEEE_T_MCOL, IEEE_T_DEF,
2368 	IEEE_T_LCOL, IEEE_T_EXCOL, IEEE_T_MACERR, IEEE_T_CSERR, IEEE_T_SQE,
2369 	IEEE_T_FDXFC, IEEE_T_OCTETS_OK,
2370 	RMON_R_PACKETS, RMON_R_BC_PKT, RMON_R_MC_PKT, RMON_R_CRC_ALIGN,
2371 	RMON_R_UNDERSIZE, RMON_R_OVERSIZE, RMON_R_FRAG, RMON_R_JAB,
2372 	RMON_R_RESVD_O, RMON_R_P64, RMON_R_P65TO127, RMON_R_P128TO255,
2373 	RMON_R_P256TO511, RMON_R_P512TO1023, RMON_R_P1024TO2047,
2374 	RMON_R_P_GTE2048, RMON_R_OCTETS,
2375 	IEEE_R_DROP, IEEE_R_FRAME_OK, IEEE_R_CRC, IEEE_R_ALIGN, IEEE_R_MACERR,
2376 	IEEE_R_FDXFC, IEEE_R_OCTETS_OK
2377 };
2378 #else
2379 static __u32 fec_enet_register_version = 1;
2380 static u32 fec_enet_register_offset[] = {
2381 	FEC_ECNTRL, FEC_IEVENT, FEC_IMASK, FEC_IVEC, FEC_R_DES_ACTIVE_0,
2382 	FEC_R_DES_ACTIVE_1, FEC_R_DES_ACTIVE_2, FEC_X_DES_ACTIVE_0,
2383 	FEC_X_DES_ACTIVE_1, FEC_X_DES_ACTIVE_2, FEC_MII_DATA, FEC_MII_SPEED,
2384 	FEC_R_BOUND, FEC_R_FSTART, FEC_X_WMRK, FEC_X_FSTART, FEC_R_CNTRL,
2385 	FEC_MAX_FRM_LEN, FEC_X_CNTRL, FEC_ADDR_LOW, FEC_ADDR_HIGH,
2386 	FEC_GRP_HASH_TABLE_HIGH, FEC_GRP_HASH_TABLE_LOW, FEC_R_DES_START_0,
2387 	FEC_R_DES_START_1, FEC_R_DES_START_2, FEC_X_DES_START_0,
2388 	FEC_X_DES_START_1, FEC_X_DES_START_2, FEC_R_BUFF_SIZE_0,
2389 	FEC_R_BUFF_SIZE_1, FEC_R_BUFF_SIZE_2
2390 };
2391 #endif
2392 
2393 static void fec_enet_get_regs(struct net_device *ndev,
2394 			      struct ethtool_regs *regs, void *regbuf)
2395 {
2396 	struct fec_enet_private *fep = netdev_priv(ndev);
2397 	u32 __iomem *theregs = (u32 __iomem *)fep->hwp;
2398 	struct device *dev = &fep->pdev->dev;
2399 	u32 *buf = (u32 *)regbuf;
2400 	u32 i, off;
2401 	int ret;
2402 
2403 	ret = pm_runtime_resume_and_get(dev);
2404 	if (ret < 0)
2405 		return;
2406 
2407 	regs->version = fec_enet_register_version;
2408 
2409 	memset(buf, 0, regs->len);
2410 
2411 	for (i = 0; i < ARRAY_SIZE(fec_enet_register_offset); i++) {
2412 		off = fec_enet_register_offset[i];
2413 
2414 		if ((off == FEC_R_BOUND || off == FEC_R_FSTART) &&
2415 		    !(fep->quirks & FEC_QUIRK_HAS_FRREG))
2416 			continue;
2417 
2418 		off >>= 2;
2419 		buf[off] = readl(&theregs[off]);
2420 	}
2421 
2422 	pm_runtime_mark_last_busy(dev);
2423 	pm_runtime_put_autosuspend(dev);
2424 }
2425 
2426 static int fec_enet_get_ts_info(struct net_device *ndev,
2427 				struct ethtool_ts_info *info)
2428 {
2429 	struct fec_enet_private *fep = netdev_priv(ndev);
2430 
2431 	if (fep->bufdesc_ex) {
2432 
2433 		info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
2434 					SOF_TIMESTAMPING_RX_SOFTWARE |
2435 					SOF_TIMESTAMPING_SOFTWARE |
2436 					SOF_TIMESTAMPING_TX_HARDWARE |
2437 					SOF_TIMESTAMPING_RX_HARDWARE |
2438 					SOF_TIMESTAMPING_RAW_HARDWARE;
2439 		if (fep->ptp_clock)
2440 			info->phc_index = ptp_clock_index(fep->ptp_clock);
2441 		else
2442 			info->phc_index = -1;
2443 
2444 		info->tx_types = (1 << HWTSTAMP_TX_OFF) |
2445 				 (1 << HWTSTAMP_TX_ON);
2446 
2447 		info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
2448 				   (1 << HWTSTAMP_FILTER_ALL);
2449 		return 0;
2450 	} else {
2451 		return ethtool_op_get_ts_info(ndev, info);
2452 	}
2453 }
2454 
2455 #if !defined(CONFIG_M5272)
2456 
2457 static void fec_enet_get_pauseparam(struct net_device *ndev,
2458 				    struct ethtool_pauseparam *pause)
2459 {
2460 	struct fec_enet_private *fep = netdev_priv(ndev);
2461 
2462 	pause->autoneg = (fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) != 0;
2463 	pause->tx_pause = (fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) != 0;
2464 	pause->rx_pause = pause->tx_pause;
2465 }
2466 
2467 static int fec_enet_set_pauseparam(struct net_device *ndev,
2468 				   struct ethtool_pauseparam *pause)
2469 {
2470 	struct fec_enet_private *fep = netdev_priv(ndev);
2471 
2472 	if (!ndev->phydev)
2473 		return -ENODEV;
2474 
2475 	if (pause->tx_pause != pause->rx_pause) {
2476 		netdev_info(ndev,
2477 			"hardware only support enable/disable both tx and rx");
2478 		return -EINVAL;
2479 	}
2480 
2481 	fep->pause_flag = 0;
2482 
2483 	/* tx pause must be same as rx pause */
2484 	fep->pause_flag |= pause->rx_pause ? FEC_PAUSE_FLAG_ENABLE : 0;
2485 	fep->pause_flag |= pause->autoneg ? FEC_PAUSE_FLAG_AUTONEG : 0;
2486 
2487 	phy_set_sym_pause(ndev->phydev, pause->rx_pause, pause->tx_pause,
2488 			  pause->autoneg);
2489 
2490 	if (pause->autoneg) {
2491 		if (netif_running(ndev))
2492 			fec_stop(ndev);
2493 		phy_start_aneg(ndev->phydev);
2494 	}
2495 	if (netif_running(ndev)) {
2496 		napi_disable(&fep->napi);
2497 		netif_tx_lock_bh(ndev);
2498 		fec_restart(ndev);
2499 		netif_tx_wake_all_queues(ndev);
2500 		netif_tx_unlock_bh(ndev);
2501 		napi_enable(&fep->napi);
2502 	}
2503 
2504 	return 0;
2505 }
2506 
2507 static const struct fec_stat {
2508 	char name[ETH_GSTRING_LEN];
2509 	u16 offset;
2510 } fec_stats[] = {
2511 	/* RMON TX */
2512 	{ "tx_dropped", RMON_T_DROP },
2513 	{ "tx_packets", RMON_T_PACKETS },
2514 	{ "tx_broadcast", RMON_T_BC_PKT },
2515 	{ "tx_multicast", RMON_T_MC_PKT },
2516 	{ "tx_crc_errors", RMON_T_CRC_ALIGN },
2517 	{ "tx_undersize", RMON_T_UNDERSIZE },
2518 	{ "tx_oversize", RMON_T_OVERSIZE },
2519 	{ "tx_fragment", RMON_T_FRAG },
2520 	{ "tx_jabber", RMON_T_JAB },
2521 	{ "tx_collision", RMON_T_COL },
2522 	{ "tx_64byte", RMON_T_P64 },
2523 	{ "tx_65to127byte", RMON_T_P65TO127 },
2524 	{ "tx_128to255byte", RMON_T_P128TO255 },
2525 	{ "tx_256to511byte", RMON_T_P256TO511 },
2526 	{ "tx_512to1023byte", RMON_T_P512TO1023 },
2527 	{ "tx_1024to2047byte", RMON_T_P1024TO2047 },
2528 	{ "tx_GTE2048byte", RMON_T_P_GTE2048 },
2529 	{ "tx_octets", RMON_T_OCTETS },
2530 
2531 	/* IEEE TX */
2532 	{ "IEEE_tx_drop", IEEE_T_DROP },
2533 	{ "IEEE_tx_frame_ok", IEEE_T_FRAME_OK },
2534 	{ "IEEE_tx_1col", IEEE_T_1COL },
2535 	{ "IEEE_tx_mcol", IEEE_T_MCOL },
2536 	{ "IEEE_tx_def", IEEE_T_DEF },
2537 	{ "IEEE_tx_lcol", IEEE_T_LCOL },
2538 	{ "IEEE_tx_excol", IEEE_T_EXCOL },
2539 	{ "IEEE_tx_macerr", IEEE_T_MACERR },
2540 	{ "IEEE_tx_cserr", IEEE_T_CSERR },
2541 	{ "IEEE_tx_sqe", IEEE_T_SQE },
2542 	{ "IEEE_tx_fdxfc", IEEE_T_FDXFC },
2543 	{ "IEEE_tx_octets_ok", IEEE_T_OCTETS_OK },
2544 
2545 	/* RMON RX */
2546 	{ "rx_packets", RMON_R_PACKETS },
2547 	{ "rx_broadcast", RMON_R_BC_PKT },
2548 	{ "rx_multicast", RMON_R_MC_PKT },
2549 	{ "rx_crc_errors", RMON_R_CRC_ALIGN },
2550 	{ "rx_undersize", RMON_R_UNDERSIZE },
2551 	{ "rx_oversize", RMON_R_OVERSIZE },
2552 	{ "rx_fragment", RMON_R_FRAG },
2553 	{ "rx_jabber", RMON_R_JAB },
2554 	{ "rx_64byte", RMON_R_P64 },
2555 	{ "rx_65to127byte", RMON_R_P65TO127 },
2556 	{ "rx_128to255byte", RMON_R_P128TO255 },
2557 	{ "rx_256to511byte", RMON_R_P256TO511 },
2558 	{ "rx_512to1023byte", RMON_R_P512TO1023 },
2559 	{ "rx_1024to2047byte", RMON_R_P1024TO2047 },
2560 	{ "rx_GTE2048byte", RMON_R_P_GTE2048 },
2561 	{ "rx_octets", RMON_R_OCTETS },
2562 
2563 	/* IEEE RX */
2564 	{ "IEEE_rx_drop", IEEE_R_DROP },
2565 	{ "IEEE_rx_frame_ok", IEEE_R_FRAME_OK },
2566 	{ "IEEE_rx_crc", IEEE_R_CRC },
2567 	{ "IEEE_rx_align", IEEE_R_ALIGN },
2568 	{ "IEEE_rx_macerr", IEEE_R_MACERR },
2569 	{ "IEEE_rx_fdxfc", IEEE_R_FDXFC },
2570 	{ "IEEE_rx_octets_ok", IEEE_R_OCTETS_OK },
2571 };
2572 
2573 #define FEC_STATS_SIZE		(ARRAY_SIZE(fec_stats) * sizeof(u64))
2574 
2575 static void fec_enet_update_ethtool_stats(struct net_device *dev)
2576 {
2577 	struct fec_enet_private *fep = netdev_priv(dev);
2578 	int i;
2579 
2580 	for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
2581 		fep->ethtool_stats[i] = readl(fep->hwp + fec_stats[i].offset);
2582 }
2583 
2584 static void fec_enet_get_ethtool_stats(struct net_device *dev,
2585 				       struct ethtool_stats *stats, u64 *data)
2586 {
2587 	struct fec_enet_private *fep = netdev_priv(dev);
2588 
2589 	if (netif_running(dev))
2590 		fec_enet_update_ethtool_stats(dev);
2591 
2592 	memcpy(data, fep->ethtool_stats, FEC_STATS_SIZE);
2593 }
2594 
2595 static void fec_enet_get_strings(struct net_device *netdev,
2596 	u32 stringset, u8 *data)
2597 {
2598 	int i;
2599 	switch (stringset) {
2600 	case ETH_SS_STATS:
2601 		for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
2602 			memcpy(data + i * ETH_GSTRING_LEN,
2603 				fec_stats[i].name, ETH_GSTRING_LEN);
2604 		break;
2605 	case ETH_SS_TEST:
2606 		net_selftest_get_strings(data);
2607 		break;
2608 	}
2609 }
2610 
2611 static int fec_enet_get_sset_count(struct net_device *dev, int sset)
2612 {
2613 	switch (sset) {
2614 	case ETH_SS_STATS:
2615 		return ARRAY_SIZE(fec_stats);
2616 	case ETH_SS_TEST:
2617 		return net_selftest_get_count();
2618 	default:
2619 		return -EOPNOTSUPP;
2620 	}
2621 }
2622 
2623 static void fec_enet_clear_ethtool_stats(struct net_device *dev)
2624 {
2625 	struct fec_enet_private *fep = netdev_priv(dev);
2626 	int i;
2627 
2628 	/* Disable MIB statistics counters */
2629 	writel(FEC_MIB_CTRLSTAT_DISABLE, fep->hwp + FEC_MIB_CTRLSTAT);
2630 
2631 	for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
2632 		writel(0, fep->hwp + fec_stats[i].offset);
2633 
2634 	/* Don't disable MIB statistics counters */
2635 	writel(0, fep->hwp + FEC_MIB_CTRLSTAT);
2636 }
2637 
2638 #else	/* !defined(CONFIG_M5272) */
2639 #define FEC_STATS_SIZE	0
2640 static inline void fec_enet_update_ethtool_stats(struct net_device *dev)
2641 {
2642 }
2643 
2644 static inline void fec_enet_clear_ethtool_stats(struct net_device *dev)
2645 {
2646 }
2647 #endif /* !defined(CONFIG_M5272) */
2648 
2649 /* ITR clock source is enet system clock (clk_ahb).
2650  * TCTT unit is cycle_ns * 64 cycle
2651  * So, the ICTT value = X us / (cycle_ns * 64)
2652  */
2653 static int fec_enet_us_to_itr_clock(struct net_device *ndev, int us)
2654 {
2655 	struct fec_enet_private *fep = netdev_priv(ndev);
2656 
2657 	return us * (fep->itr_clk_rate / 64000) / 1000;
2658 }
2659 
2660 /* Set threshold for interrupt coalescing */
2661 static void fec_enet_itr_coal_set(struct net_device *ndev)
2662 {
2663 	struct fec_enet_private *fep = netdev_priv(ndev);
2664 	int rx_itr, tx_itr;
2665 
2666 	/* Must be greater than zero to avoid unpredictable behavior */
2667 	if (!fep->rx_time_itr || !fep->rx_pkts_itr ||
2668 	    !fep->tx_time_itr || !fep->tx_pkts_itr)
2669 		return;
2670 
2671 	/* Select enet system clock as Interrupt Coalescing
2672 	 * timer Clock Source
2673 	 */
2674 	rx_itr = FEC_ITR_CLK_SEL;
2675 	tx_itr = FEC_ITR_CLK_SEL;
2676 
2677 	/* set ICFT and ICTT */
2678 	rx_itr |= FEC_ITR_ICFT(fep->rx_pkts_itr);
2679 	rx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->rx_time_itr));
2680 	tx_itr |= FEC_ITR_ICFT(fep->tx_pkts_itr);
2681 	tx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->tx_time_itr));
2682 
2683 	rx_itr |= FEC_ITR_EN;
2684 	tx_itr |= FEC_ITR_EN;
2685 
2686 	writel(tx_itr, fep->hwp + FEC_TXIC0);
2687 	writel(rx_itr, fep->hwp + FEC_RXIC0);
2688 	if (fep->quirks & FEC_QUIRK_HAS_MULTI_QUEUES) {
2689 		writel(tx_itr, fep->hwp + FEC_TXIC1);
2690 		writel(rx_itr, fep->hwp + FEC_RXIC1);
2691 		writel(tx_itr, fep->hwp + FEC_TXIC2);
2692 		writel(rx_itr, fep->hwp + FEC_RXIC2);
2693 	}
2694 }
2695 
2696 static int fec_enet_get_coalesce(struct net_device *ndev,
2697 				 struct ethtool_coalesce *ec,
2698 				 struct kernel_ethtool_coalesce *kernel_coal,
2699 				 struct netlink_ext_ack *extack)
2700 {
2701 	struct fec_enet_private *fep = netdev_priv(ndev);
2702 
2703 	if (!(fep->quirks & FEC_QUIRK_HAS_COALESCE))
2704 		return -EOPNOTSUPP;
2705 
2706 	ec->rx_coalesce_usecs = fep->rx_time_itr;
2707 	ec->rx_max_coalesced_frames = fep->rx_pkts_itr;
2708 
2709 	ec->tx_coalesce_usecs = fep->tx_time_itr;
2710 	ec->tx_max_coalesced_frames = fep->tx_pkts_itr;
2711 
2712 	return 0;
2713 }
2714 
2715 static int fec_enet_set_coalesce(struct net_device *ndev,
2716 				 struct ethtool_coalesce *ec,
2717 				 struct kernel_ethtool_coalesce *kernel_coal,
2718 				 struct netlink_ext_ack *extack)
2719 {
2720 	struct fec_enet_private *fep = netdev_priv(ndev);
2721 	struct device *dev = &fep->pdev->dev;
2722 	unsigned int cycle;
2723 
2724 	if (!(fep->quirks & FEC_QUIRK_HAS_COALESCE))
2725 		return -EOPNOTSUPP;
2726 
2727 	if (ec->rx_max_coalesced_frames > 255) {
2728 		dev_err(dev, "Rx coalesced frames exceed hardware limitation\n");
2729 		return -EINVAL;
2730 	}
2731 
2732 	if (ec->tx_max_coalesced_frames > 255) {
2733 		dev_err(dev, "Tx coalesced frame exceed hardware limitation\n");
2734 		return -EINVAL;
2735 	}
2736 
2737 	cycle = fec_enet_us_to_itr_clock(ndev, ec->rx_coalesce_usecs);
2738 	if (cycle > 0xFFFF) {
2739 		dev_err(dev, "Rx coalesced usec exceed hardware limitation\n");
2740 		return -EINVAL;
2741 	}
2742 
2743 	cycle = fec_enet_us_to_itr_clock(ndev, ec->tx_coalesce_usecs);
2744 	if (cycle > 0xFFFF) {
2745 		dev_err(dev, "Tx coalesced usec exceed hardware limitation\n");
2746 		return -EINVAL;
2747 	}
2748 
2749 	fep->rx_time_itr = ec->rx_coalesce_usecs;
2750 	fep->rx_pkts_itr = ec->rx_max_coalesced_frames;
2751 
2752 	fep->tx_time_itr = ec->tx_coalesce_usecs;
2753 	fep->tx_pkts_itr = ec->tx_max_coalesced_frames;
2754 
2755 	fec_enet_itr_coal_set(ndev);
2756 
2757 	return 0;
2758 }
2759 
2760 static void fec_enet_itr_coal_init(struct net_device *ndev)
2761 {
2762 	struct ethtool_coalesce ec;
2763 
2764 	ec.rx_coalesce_usecs = FEC_ITR_ICTT_DEFAULT;
2765 	ec.rx_max_coalesced_frames = FEC_ITR_ICFT_DEFAULT;
2766 
2767 	ec.tx_coalesce_usecs = FEC_ITR_ICTT_DEFAULT;
2768 	ec.tx_max_coalesced_frames = FEC_ITR_ICFT_DEFAULT;
2769 
2770 	fec_enet_set_coalesce(ndev, &ec, NULL, NULL);
2771 }
2772 
2773 static int fec_enet_get_tunable(struct net_device *netdev,
2774 				const struct ethtool_tunable *tuna,
2775 				void *data)
2776 {
2777 	struct fec_enet_private *fep = netdev_priv(netdev);
2778 	int ret = 0;
2779 
2780 	switch (tuna->id) {
2781 	case ETHTOOL_RX_COPYBREAK:
2782 		*(u32 *)data = fep->rx_copybreak;
2783 		break;
2784 	default:
2785 		ret = -EINVAL;
2786 		break;
2787 	}
2788 
2789 	return ret;
2790 }
2791 
2792 static int fec_enet_set_tunable(struct net_device *netdev,
2793 				const struct ethtool_tunable *tuna,
2794 				const void *data)
2795 {
2796 	struct fec_enet_private *fep = netdev_priv(netdev);
2797 	int ret = 0;
2798 
2799 	switch (tuna->id) {
2800 	case ETHTOOL_RX_COPYBREAK:
2801 		fep->rx_copybreak = *(u32 *)data;
2802 		break;
2803 	default:
2804 		ret = -EINVAL;
2805 		break;
2806 	}
2807 
2808 	return ret;
2809 }
2810 
2811 /* LPI Sleep Ts count base on tx clk (clk_ref).
2812  * The lpi sleep cnt value = X us / (cycle_ns).
2813  */
2814 static int fec_enet_us_to_tx_cycle(struct net_device *ndev, int us)
2815 {
2816 	struct fec_enet_private *fep = netdev_priv(ndev);
2817 
2818 	return us * (fep->clk_ref_rate / 1000) / 1000;
2819 }
2820 
2821 static int fec_enet_eee_mode_set(struct net_device *ndev, bool enable)
2822 {
2823 	struct fec_enet_private *fep = netdev_priv(ndev);
2824 	struct ethtool_eee *p = &fep->eee;
2825 	unsigned int sleep_cycle, wake_cycle;
2826 	int ret = 0;
2827 
2828 	if (enable) {
2829 		ret = phy_init_eee(ndev->phydev, false);
2830 		if (ret)
2831 			return ret;
2832 
2833 		sleep_cycle = fec_enet_us_to_tx_cycle(ndev, p->tx_lpi_timer);
2834 		wake_cycle = sleep_cycle;
2835 	} else {
2836 		sleep_cycle = 0;
2837 		wake_cycle = 0;
2838 	}
2839 
2840 	p->tx_lpi_enabled = enable;
2841 	p->eee_enabled = enable;
2842 	p->eee_active = enable;
2843 
2844 	writel(sleep_cycle, fep->hwp + FEC_LPI_SLEEP);
2845 	writel(wake_cycle, fep->hwp + FEC_LPI_WAKE);
2846 
2847 	return 0;
2848 }
2849 
2850 static int
2851 fec_enet_get_eee(struct net_device *ndev, struct ethtool_eee *edata)
2852 {
2853 	struct fec_enet_private *fep = netdev_priv(ndev);
2854 	struct ethtool_eee *p = &fep->eee;
2855 
2856 	if (!(fep->quirks & FEC_QUIRK_HAS_EEE))
2857 		return -EOPNOTSUPP;
2858 
2859 	if (!netif_running(ndev))
2860 		return -ENETDOWN;
2861 
2862 	edata->eee_enabled = p->eee_enabled;
2863 	edata->eee_active = p->eee_active;
2864 	edata->tx_lpi_timer = p->tx_lpi_timer;
2865 	edata->tx_lpi_enabled = p->tx_lpi_enabled;
2866 
2867 	return phy_ethtool_get_eee(ndev->phydev, edata);
2868 }
2869 
2870 static int
2871 fec_enet_set_eee(struct net_device *ndev, struct ethtool_eee *edata)
2872 {
2873 	struct fec_enet_private *fep = netdev_priv(ndev);
2874 	struct ethtool_eee *p = &fep->eee;
2875 	int ret = 0;
2876 
2877 	if (!(fep->quirks & FEC_QUIRK_HAS_EEE))
2878 		return -EOPNOTSUPP;
2879 
2880 	if (!netif_running(ndev))
2881 		return -ENETDOWN;
2882 
2883 	p->tx_lpi_timer = edata->tx_lpi_timer;
2884 
2885 	if (!edata->eee_enabled || !edata->tx_lpi_enabled ||
2886 	    !edata->tx_lpi_timer)
2887 		ret = fec_enet_eee_mode_set(ndev, false);
2888 	else
2889 		ret = fec_enet_eee_mode_set(ndev, true);
2890 
2891 	if (ret)
2892 		return ret;
2893 
2894 	return phy_ethtool_set_eee(ndev->phydev, edata);
2895 }
2896 
2897 static void
2898 fec_enet_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
2899 {
2900 	struct fec_enet_private *fep = netdev_priv(ndev);
2901 
2902 	if (fep->wol_flag & FEC_WOL_HAS_MAGIC_PACKET) {
2903 		wol->supported = WAKE_MAGIC;
2904 		wol->wolopts = fep->wol_flag & FEC_WOL_FLAG_ENABLE ? WAKE_MAGIC : 0;
2905 	} else {
2906 		wol->supported = wol->wolopts = 0;
2907 	}
2908 }
2909 
2910 static int
2911 fec_enet_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
2912 {
2913 	struct fec_enet_private *fep = netdev_priv(ndev);
2914 
2915 	if (!(fep->wol_flag & FEC_WOL_HAS_MAGIC_PACKET))
2916 		return -EINVAL;
2917 
2918 	if (wol->wolopts & ~WAKE_MAGIC)
2919 		return -EINVAL;
2920 
2921 	device_set_wakeup_enable(&ndev->dev, wol->wolopts & WAKE_MAGIC);
2922 	if (device_may_wakeup(&ndev->dev))
2923 		fep->wol_flag |= FEC_WOL_FLAG_ENABLE;
2924 	else
2925 		fep->wol_flag &= (~FEC_WOL_FLAG_ENABLE);
2926 
2927 	return 0;
2928 }
2929 
2930 static const struct ethtool_ops fec_enet_ethtool_ops = {
2931 	.supported_coalesce_params = ETHTOOL_COALESCE_USECS |
2932 				     ETHTOOL_COALESCE_MAX_FRAMES,
2933 	.get_drvinfo		= fec_enet_get_drvinfo,
2934 	.get_regs_len		= fec_enet_get_regs_len,
2935 	.get_regs		= fec_enet_get_regs,
2936 	.nway_reset		= phy_ethtool_nway_reset,
2937 	.get_link		= ethtool_op_get_link,
2938 	.get_coalesce		= fec_enet_get_coalesce,
2939 	.set_coalesce		= fec_enet_set_coalesce,
2940 #ifndef CONFIG_M5272
2941 	.get_pauseparam		= fec_enet_get_pauseparam,
2942 	.set_pauseparam		= fec_enet_set_pauseparam,
2943 	.get_strings		= fec_enet_get_strings,
2944 	.get_ethtool_stats	= fec_enet_get_ethtool_stats,
2945 	.get_sset_count		= fec_enet_get_sset_count,
2946 #endif
2947 	.get_ts_info		= fec_enet_get_ts_info,
2948 	.get_tunable		= fec_enet_get_tunable,
2949 	.set_tunable		= fec_enet_set_tunable,
2950 	.get_wol		= fec_enet_get_wol,
2951 	.set_wol		= fec_enet_set_wol,
2952 	.get_eee		= fec_enet_get_eee,
2953 	.set_eee		= fec_enet_set_eee,
2954 	.get_link_ksettings	= phy_ethtool_get_link_ksettings,
2955 	.set_link_ksettings	= phy_ethtool_set_link_ksettings,
2956 	.self_test		= net_selftest,
2957 };
2958 
2959 static int fec_enet_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
2960 {
2961 	struct fec_enet_private *fep = netdev_priv(ndev);
2962 	struct phy_device *phydev = ndev->phydev;
2963 
2964 	if (!netif_running(ndev))
2965 		return -EINVAL;
2966 
2967 	if (!phydev)
2968 		return -ENODEV;
2969 
2970 	if (fep->bufdesc_ex) {
2971 		bool use_fec_hwts = !phy_has_hwtstamp(phydev);
2972 
2973 		if (cmd == SIOCSHWTSTAMP) {
2974 			if (use_fec_hwts)
2975 				return fec_ptp_set(ndev, rq);
2976 			fec_ptp_disable_hwts(ndev);
2977 		} else if (cmd == SIOCGHWTSTAMP) {
2978 			if (use_fec_hwts)
2979 				return fec_ptp_get(ndev, rq);
2980 		}
2981 	}
2982 
2983 	return phy_mii_ioctl(phydev, rq, cmd);
2984 }
2985 
2986 static void fec_enet_free_buffers(struct net_device *ndev)
2987 {
2988 	struct fec_enet_private *fep = netdev_priv(ndev);
2989 	unsigned int i;
2990 	struct sk_buff *skb;
2991 	struct fec_enet_priv_tx_q *txq;
2992 	struct fec_enet_priv_rx_q *rxq;
2993 	unsigned int q;
2994 
2995 	for (q = 0; q < fep->num_rx_queues; q++) {
2996 		rxq = fep->rx_queue[q];
2997 		for (i = 0; i < rxq->bd.ring_size; i++)
2998 			page_pool_release_page(rxq->page_pool, rxq->rx_skb_info[i].page);
2999 
3000 		if (xdp_rxq_info_is_reg(&rxq->xdp_rxq))
3001 			xdp_rxq_info_unreg(&rxq->xdp_rxq);
3002 		page_pool_destroy(rxq->page_pool);
3003 		rxq->page_pool = NULL;
3004 	}
3005 
3006 	for (q = 0; q < fep->num_tx_queues; q++) {
3007 		txq = fep->tx_queue[q];
3008 		for (i = 0; i < txq->bd.ring_size; i++) {
3009 			kfree(txq->tx_bounce[i]);
3010 			txq->tx_bounce[i] = NULL;
3011 			skb = txq->tx_skbuff[i];
3012 			txq->tx_skbuff[i] = NULL;
3013 			dev_kfree_skb(skb);
3014 		}
3015 	}
3016 }
3017 
3018 static void fec_enet_free_queue(struct net_device *ndev)
3019 {
3020 	struct fec_enet_private *fep = netdev_priv(ndev);
3021 	int i;
3022 	struct fec_enet_priv_tx_q *txq;
3023 
3024 	for (i = 0; i < fep->num_tx_queues; i++)
3025 		if (fep->tx_queue[i] && fep->tx_queue[i]->tso_hdrs) {
3026 			txq = fep->tx_queue[i];
3027 			dma_free_coherent(&fep->pdev->dev,
3028 					  txq->bd.ring_size * TSO_HEADER_SIZE,
3029 					  txq->tso_hdrs,
3030 					  txq->tso_hdrs_dma);
3031 		}
3032 
3033 	for (i = 0; i < fep->num_rx_queues; i++)
3034 		kfree(fep->rx_queue[i]);
3035 	for (i = 0; i < fep->num_tx_queues; i++)
3036 		kfree(fep->tx_queue[i]);
3037 }
3038 
3039 static int fec_enet_alloc_queue(struct net_device *ndev)
3040 {
3041 	struct fec_enet_private *fep = netdev_priv(ndev);
3042 	int i;
3043 	int ret = 0;
3044 	struct fec_enet_priv_tx_q *txq;
3045 
3046 	for (i = 0; i < fep->num_tx_queues; i++) {
3047 		txq = kzalloc(sizeof(*txq), GFP_KERNEL);
3048 		if (!txq) {
3049 			ret = -ENOMEM;
3050 			goto alloc_failed;
3051 		}
3052 
3053 		fep->tx_queue[i] = txq;
3054 		txq->bd.ring_size = TX_RING_SIZE;
3055 		fep->total_tx_ring_size += fep->tx_queue[i]->bd.ring_size;
3056 
3057 		txq->tx_stop_threshold = FEC_MAX_SKB_DESCS;
3058 		txq->tx_wake_threshold =
3059 			(txq->bd.ring_size - txq->tx_stop_threshold) / 2;
3060 
3061 		txq->tso_hdrs = dma_alloc_coherent(&fep->pdev->dev,
3062 					txq->bd.ring_size * TSO_HEADER_SIZE,
3063 					&txq->tso_hdrs_dma,
3064 					GFP_KERNEL);
3065 		if (!txq->tso_hdrs) {
3066 			ret = -ENOMEM;
3067 			goto alloc_failed;
3068 		}
3069 	}
3070 
3071 	for (i = 0; i < fep->num_rx_queues; i++) {
3072 		fep->rx_queue[i] = kzalloc(sizeof(*fep->rx_queue[i]),
3073 					   GFP_KERNEL);
3074 		if (!fep->rx_queue[i]) {
3075 			ret = -ENOMEM;
3076 			goto alloc_failed;
3077 		}
3078 
3079 		fep->rx_queue[i]->bd.ring_size = RX_RING_SIZE;
3080 		fep->total_rx_ring_size += fep->rx_queue[i]->bd.ring_size;
3081 	}
3082 	return ret;
3083 
3084 alloc_failed:
3085 	fec_enet_free_queue(ndev);
3086 	return ret;
3087 }
3088 
3089 static int
3090 fec_enet_alloc_rxq_buffers(struct net_device *ndev, unsigned int queue)
3091 {
3092 	struct fec_enet_private *fep = netdev_priv(ndev);
3093 	struct fec_enet_priv_rx_q *rxq;
3094 	dma_addr_t phys_addr;
3095 	struct bufdesc	*bdp;
3096 	struct page *page;
3097 	int i, err;
3098 
3099 	rxq = fep->rx_queue[queue];
3100 	bdp = rxq->bd.base;
3101 
3102 	err = fec_enet_create_page_pool(fep, rxq, rxq->bd.ring_size);
3103 	if (err < 0) {
3104 		netdev_err(ndev, "%s failed queue %d (%d)\n", __func__, queue, err);
3105 		return err;
3106 	}
3107 
3108 	for (i = 0; i < rxq->bd.ring_size; i++) {
3109 		page = page_pool_dev_alloc_pages(rxq->page_pool);
3110 		if (!page)
3111 			goto err_alloc;
3112 
3113 		phys_addr = page_pool_get_dma_addr(page) + FEC_ENET_XDP_HEADROOM;
3114 		bdp->cbd_bufaddr = cpu_to_fec32(phys_addr);
3115 
3116 		rxq->rx_skb_info[i].page = page;
3117 		rxq->rx_skb_info[i].offset = FEC_ENET_XDP_HEADROOM;
3118 		bdp->cbd_sc = cpu_to_fec16(BD_ENET_RX_EMPTY);
3119 
3120 		if (fep->bufdesc_ex) {
3121 			struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
3122 			ebdp->cbd_esc = cpu_to_fec32(BD_ENET_RX_INT);
3123 		}
3124 
3125 		bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
3126 	}
3127 
3128 	/* Set the last buffer to wrap. */
3129 	bdp = fec_enet_get_prevdesc(bdp, &rxq->bd);
3130 	bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP);
3131 	return 0;
3132 
3133  err_alloc:
3134 	fec_enet_free_buffers(ndev);
3135 	return -ENOMEM;
3136 }
3137 
3138 static int
3139 fec_enet_alloc_txq_buffers(struct net_device *ndev, unsigned int queue)
3140 {
3141 	struct fec_enet_private *fep = netdev_priv(ndev);
3142 	unsigned int i;
3143 	struct bufdesc  *bdp;
3144 	struct fec_enet_priv_tx_q *txq;
3145 
3146 	txq = fep->tx_queue[queue];
3147 	bdp = txq->bd.base;
3148 	for (i = 0; i < txq->bd.ring_size; i++) {
3149 		txq->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL);
3150 		if (!txq->tx_bounce[i])
3151 			goto err_alloc;
3152 
3153 		bdp->cbd_sc = cpu_to_fec16(0);
3154 		bdp->cbd_bufaddr = cpu_to_fec32(0);
3155 
3156 		if (fep->bufdesc_ex) {
3157 			struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
3158 			ebdp->cbd_esc = cpu_to_fec32(BD_ENET_TX_INT);
3159 		}
3160 
3161 		bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
3162 	}
3163 
3164 	/* Set the last buffer to wrap. */
3165 	bdp = fec_enet_get_prevdesc(bdp, &txq->bd);
3166 	bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP);
3167 
3168 	return 0;
3169 
3170  err_alloc:
3171 	fec_enet_free_buffers(ndev);
3172 	return -ENOMEM;
3173 }
3174 
3175 static int fec_enet_alloc_buffers(struct net_device *ndev)
3176 {
3177 	struct fec_enet_private *fep = netdev_priv(ndev);
3178 	unsigned int i;
3179 
3180 	for (i = 0; i < fep->num_rx_queues; i++)
3181 		if (fec_enet_alloc_rxq_buffers(ndev, i))
3182 			return -ENOMEM;
3183 
3184 	for (i = 0; i < fep->num_tx_queues; i++)
3185 		if (fec_enet_alloc_txq_buffers(ndev, i))
3186 			return -ENOMEM;
3187 	return 0;
3188 }
3189 
3190 static int
3191 fec_enet_open(struct net_device *ndev)
3192 {
3193 	struct fec_enet_private *fep = netdev_priv(ndev);
3194 	int ret;
3195 	bool reset_again;
3196 
3197 	ret = pm_runtime_resume_and_get(&fep->pdev->dev);
3198 	if (ret < 0)
3199 		return ret;
3200 
3201 	pinctrl_pm_select_default_state(&fep->pdev->dev);
3202 	ret = fec_enet_clk_enable(ndev, true);
3203 	if (ret)
3204 		goto clk_enable;
3205 
3206 	/* During the first fec_enet_open call the PHY isn't probed at this
3207 	 * point. Therefore the phy_reset_after_clk_enable() call within
3208 	 * fec_enet_clk_enable() fails. As we need this reset in order to be
3209 	 * sure the PHY is working correctly we check if we need to reset again
3210 	 * later when the PHY is probed
3211 	 */
3212 	if (ndev->phydev && ndev->phydev->drv)
3213 		reset_again = false;
3214 	else
3215 		reset_again = true;
3216 
3217 	/* I should reset the ring buffers here, but I don't yet know
3218 	 * a simple way to do that.
3219 	 */
3220 
3221 	ret = fec_enet_alloc_buffers(ndev);
3222 	if (ret)
3223 		goto err_enet_alloc;
3224 
3225 	/* Init MAC prior to mii bus probe */
3226 	fec_restart(ndev);
3227 
3228 	/* Call phy_reset_after_clk_enable() again if it failed during
3229 	 * phy_reset_after_clk_enable() before because the PHY wasn't probed.
3230 	 */
3231 	if (reset_again)
3232 		fec_enet_phy_reset_after_clk_enable(ndev);
3233 
3234 	/* Probe and connect to PHY when open the interface */
3235 	ret = fec_enet_mii_probe(ndev);
3236 	if (ret)
3237 		goto err_enet_mii_probe;
3238 
3239 	if (fep->quirks & FEC_QUIRK_ERR006687)
3240 		imx6q_cpuidle_fec_irqs_used();
3241 
3242 	if (fep->quirks & FEC_QUIRK_HAS_PMQOS)
3243 		cpu_latency_qos_add_request(&fep->pm_qos_req, 0);
3244 
3245 	napi_enable(&fep->napi);
3246 	phy_start(ndev->phydev);
3247 	netif_tx_start_all_queues(ndev);
3248 
3249 	device_set_wakeup_enable(&ndev->dev, fep->wol_flag &
3250 				 FEC_WOL_FLAG_ENABLE);
3251 
3252 	return 0;
3253 
3254 err_enet_mii_probe:
3255 	fec_enet_free_buffers(ndev);
3256 err_enet_alloc:
3257 	fec_enet_clk_enable(ndev, false);
3258 clk_enable:
3259 	pm_runtime_mark_last_busy(&fep->pdev->dev);
3260 	pm_runtime_put_autosuspend(&fep->pdev->dev);
3261 	pinctrl_pm_select_sleep_state(&fep->pdev->dev);
3262 	return ret;
3263 }
3264 
3265 static int
3266 fec_enet_close(struct net_device *ndev)
3267 {
3268 	struct fec_enet_private *fep = netdev_priv(ndev);
3269 
3270 	phy_stop(ndev->phydev);
3271 
3272 	if (netif_device_present(ndev)) {
3273 		napi_disable(&fep->napi);
3274 		netif_tx_disable(ndev);
3275 		fec_stop(ndev);
3276 	}
3277 
3278 	phy_disconnect(ndev->phydev);
3279 
3280 	if (fep->quirks & FEC_QUIRK_ERR006687)
3281 		imx6q_cpuidle_fec_irqs_unused();
3282 
3283 	fec_enet_update_ethtool_stats(ndev);
3284 
3285 	fec_enet_clk_enable(ndev, false);
3286 	if (fep->quirks & FEC_QUIRK_HAS_PMQOS)
3287 		cpu_latency_qos_remove_request(&fep->pm_qos_req);
3288 
3289 	pinctrl_pm_select_sleep_state(&fep->pdev->dev);
3290 	pm_runtime_mark_last_busy(&fep->pdev->dev);
3291 	pm_runtime_put_autosuspend(&fep->pdev->dev);
3292 
3293 	fec_enet_free_buffers(ndev);
3294 
3295 	return 0;
3296 }
3297 
3298 /* Set or clear the multicast filter for this adaptor.
3299  * Skeleton taken from sunlance driver.
3300  * The CPM Ethernet implementation allows Multicast as well as individual
3301  * MAC address filtering.  Some of the drivers check to make sure it is
3302  * a group multicast address, and discard those that are not.  I guess I
3303  * will do the same for now, but just remove the test if you want
3304  * individual filtering as well (do the upper net layers want or support
3305  * this kind of feature?).
3306  */
3307 
3308 #define FEC_HASH_BITS	6		/* #bits in hash */
3309 
3310 static void set_multicast_list(struct net_device *ndev)
3311 {
3312 	struct fec_enet_private *fep = netdev_priv(ndev);
3313 	struct netdev_hw_addr *ha;
3314 	unsigned int crc, tmp;
3315 	unsigned char hash;
3316 	unsigned int hash_high = 0, hash_low = 0;
3317 
3318 	if (ndev->flags & IFF_PROMISC) {
3319 		tmp = readl(fep->hwp + FEC_R_CNTRL);
3320 		tmp |= 0x8;
3321 		writel(tmp, fep->hwp + FEC_R_CNTRL);
3322 		return;
3323 	}
3324 
3325 	tmp = readl(fep->hwp + FEC_R_CNTRL);
3326 	tmp &= ~0x8;
3327 	writel(tmp, fep->hwp + FEC_R_CNTRL);
3328 
3329 	if (ndev->flags & IFF_ALLMULTI) {
3330 		/* Catch all multicast addresses, so set the
3331 		 * filter to all 1's
3332 		 */
3333 		writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
3334 		writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
3335 
3336 		return;
3337 	}
3338 
3339 	/* Add the addresses in hash register */
3340 	netdev_for_each_mc_addr(ha, ndev) {
3341 		/* calculate crc32 value of mac address */
3342 		crc = ether_crc_le(ndev->addr_len, ha->addr);
3343 
3344 		/* only upper 6 bits (FEC_HASH_BITS) are used
3345 		 * which point to specific bit in the hash registers
3346 		 */
3347 		hash = (crc >> (32 - FEC_HASH_BITS)) & 0x3f;
3348 
3349 		if (hash > 31)
3350 			hash_high |= 1 << (hash - 32);
3351 		else
3352 			hash_low |= 1 << hash;
3353 	}
3354 
3355 	writel(hash_high, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
3356 	writel(hash_low, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
3357 }
3358 
3359 /* Set a MAC change in hardware. */
3360 static int
3361 fec_set_mac_address(struct net_device *ndev, void *p)
3362 {
3363 	struct fec_enet_private *fep = netdev_priv(ndev);
3364 	struct sockaddr *addr = p;
3365 
3366 	if (addr) {
3367 		if (!is_valid_ether_addr(addr->sa_data))
3368 			return -EADDRNOTAVAIL;
3369 		eth_hw_addr_set(ndev, addr->sa_data);
3370 	}
3371 
3372 	/* Add netif status check here to avoid system hang in below case:
3373 	 * ifconfig ethx down; ifconfig ethx hw ether xx:xx:xx:xx:xx:xx;
3374 	 * After ethx down, fec all clocks are gated off and then register
3375 	 * access causes system hang.
3376 	 */
3377 	if (!netif_running(ndev))
3378 		return 0;
3379 
3380 	writel(ndev->dev_addr[3] | (ndev->dev_addr[2] << 8) |
3381 		(ndev->dev_addr[1] << 16) | (ndev->dev_addr[0] << 24),
3382 		fep->hwp + FEC_ADDR_LOW);
3383 	writel((ndev->dev_addr[5] << 16) | (ndev->dev_addr[4] << 24),
3384 		fep->hwp + FEC_ADDR_HIGH);
3385 	return 0;
3386 }
3387 
3388 #ifdef CONFIG_NET_POLL_CONTROLLER
3389 /**
3390  * fec_poll_controller - FEC Poll controller function
3391  * @dev: The FEC network adapter
3392  *
3393  * Polled functionality used by netconsole and others in non interrupt mode
3394  *
3395  */
3396 static void fec_poll_controller(struct net_device *dev)
3397 {
3398 	int i;
3399 	struct fec_enet_private *fep = netdev_priv(dev);
3400 
3401 	for (i = 0; i < FEC_IRQ_NUM; i++) {
3402 		if (fep->irq[i] > 0) {
3403 			disable_irq(fep->irq[i]);
3404 			fec_enet_interrupt(fep->irq[i], dev);
3405 			enable_irq(fep->irq[i]);
3406 		}
3407 	}
3408 }
3409 #endif
3410 
3411 static inline void fec_enet_set_netdev_features(struct net_device *netdev,
3412 	netdev_features_t features)
3413 {
3414 	struct fec_enet_private *fep = netdev_priv(netdev);
3415 	netdev_features_t changed = features ^ netdev->features;
3416 
3417 	netdev->features = features;
3418 
3419 	/* Receive checksum has been changed */
3420 	if (changed & NETIF_F_RXCSUM) {
3421 		if (features & NETIF_F_RXCSUM)
3422 			fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
3423 		else
3424 			fep->csum_flags &= ~FLAG_RX_CSUM_ENABLED;
3425 	}
3426 }
3427 
3428 static int fec_set_features(struct net_device *netdev,
3429 	netdev_features_t features)
3430 {
3431 	struct fec_enet_private *fep = netdev_priv(netdev);
3432 	netdev_features_t changed = features ^ netdev->features;
3433 
3434 	if (netif_running(netdev) && changed & NETIF_F_RXCSUM) {
3435 		napi_disable(&fep->napi);
3436 		netif_tx_lock_bh(netdev);
3437 		fec_stop(netdev);
3438 		fec_enet_set_netdev_features(netdev, features);
3439 		fec_restart(netdev);
3440 		netif_tx_wake_all_queues(netdev);
3441 		netif_tx_unlock_bh(netdev);
3442 		napi_enable(&fep->napi);
3443 	} else {
3444 		fec_enet_set_netdev_features(netdev, features);
3445 	}
3446 
3447 	return 0;
3448 }
3449 
3450 static u16 fec_enet_get_raw_vlan_tci(struct sk_buff *skb)
3451 {
3452 	struct vlan_ethhdr *vhdr;
3453 	unsigned short vlan_TCI = 0;
3454 
3455 	if (skb->protocol == htons(ETH_P_ALL)) {
3456 		vhdr = (struct vlan_ethhdr *)(skb->data);
3457 		vlan_TCI = ntohs(vhdr->h_vlan_TCI);
3458 	}
3459 
3460 	return vlan_TCI;
3461 }
3462 
3463 static u16 fec_enet_select_queue(struct net_device *ndev, struct sk_buff *skb,
3464 				 struct net_device *sb_dev)
3465 {
3466 	struct fec_enet_private *fep = netdev_priv(ndev);
3467 	u16 vlan_tag;
3468 
3469 	if (!(fep->quirks & FEC_QUIRK_HAS_AVB))
3470 		return netdev_pick_tx(ndev, skb, NULL);
3471 
3472 	vlan_tag = fec_enet_get_raw_vlan_tci(skb);
3473 	if (!vlan_tag)
3474 		return vlan_tag;
3475 
3476 	return fec_enet_vlan_pri_to_queue[vlan_tag >> 13];
3477 }
3478 
3479 static const struct net_device_ops fec_netdev_ops = {
3480 	.ndo_open		= fec_enet_open,
3481 	.ndo_stop		= fec_enet_close,
3482 	.ndo_start_xmit		= fec_enet_start_xmit,
3483 	.ndo_select_queue       = fec_enet_select_queue,
3484 	.ndo_set_rx_mode	= set_multicast_list,
3485 	.ndo_validate_addr	= eth_validate_addr,
3486 	.ndo_tx_timeout		= fec_timeout,
3487 	.ndo_set_mac_address	= fec_set_mac_address,
3488 	.ndo_eth_ioctl		= fec_enet_ioctl,
3489 #ifdef CONFIG_NET_POLL_CONTROLLER
3490 	.ndo_poll_controller	= fec_poll_controller,
3491 #endif
3492 	.ndo_set_features	= fec_set_features,
3493 };
3494 
3495 static const unsigned short offset_des_active_rxq[] = {
3496 	FEC_R_DES_ACTIVE_0, FEC_R_DES_ACTIVE_1, FEC_R_DES_ACTIVE_2
3497 };
3498 
3499 static const unsigned short offset_des_active_txq[] = {
3500 	FEC_X_DES_ACTIVE_0, FEC_X_DES_ACTIVE_1, FEC_X_DES_ACTIVE_2
3501 };
3502 
3503  /*
3504   * XXX:  We need to clean up on failure exits here.
3505   *
3506   */
3507 static int fec_enet_init(struct net_device *ndev)
3508 {
3509 	struct fec_enet_private *fep = netdev_priv(ndev);
3510 	struct bufdesc *cbd_base;
3511 	dma_addr_t bd_dma;
3512 	int bd_size;
3513 	unsigned int i;
3514 	unsigned dsize = fep->bufdesc_ex ? sizeof(struct bufdesc_ex) :
3515 			sizeof(struct bufdesc);
3516 	unsigned dsize_log2 = __fls(dsize);
3517 	int ret;
3518 
3519 	WARN_ON(dsize != (1 << dsize_log2));
3520 #if defined(CONFIG_ARM) || defined(CONFIG_ARM64)
3521 	fep->rx_align = 0xf;
3522 	fep->tx_align = 0xf;
3523 #else
3524 	fep->rx_align = 0x3;
3525 	fep->tx_align = 0x3;
3526 #endif
3527 
3528 	/* Check mask of the streaming and coherent API */
3529 	ret = dma_set_mask_and_coherent(&fep->pdev->dev, DMA_BIT_MASK(32));
3530 	if (ret < 0) {
3531 		dev_warn(&fep->pdev->dev, "No suitable DMA available\n");
3532 		return ret;
3533 	}
3534 
3535 	ret = fec_enet_alloc_queue(ndev);
3536 	if (ret)
3537 		return ret;
3538 
3539 	bd_size = (fep->total_tx_ring_size + fep->total_rx_ring_size) * dsize;
3540 
3541 	/* Allocate memory for buffer descriptors. */
3542 	cbd_base = dmam_alloc_coherent(&fep->pdev->dev, bd_size, &bd_dma,
3543 				       GFP_KERNEL);
3544 	if (!cbd_base) {
3545 		ret = -ENOMEM;
3546 		goto free_queue_mem;
3547 	}
3548 
3549 	/* Get the Ethernet address */
3550 	ret = fec_get_mac(ndev);
3551 	if (ret)
3552 		goto free_queue_mem;
3553 
3554 	/* make sure MAC we just acquired is programmed into the hw */
3555 	fec_set_mac_address(ndev, NULL);
3556 
3557 	/* Set receive and transmit descriptor base. */
3558 	for (i = 0; i < fep->num_rx_queues; i++) {
3559 		struct fec_enet_priv_rx_q *rxq = fep->rx_queue[i];
3560 		unsigned size = dsize * rxq->bd.ring_size;
3561 
3562 		rxq->bd.qid = i;
3563 		rxq->bd.base = cbd_base;
3564 		rxq->bd.cur = cbd_base;
3565 		rxq->bd.dma = bd_dma;
3566 		rxq->bd.dsize = dsize;
3567 		rxq->bd.dsize_log2 = dsize_log2;
3568 		rxq->bd.reg_desc_active = fep->hwp + offset_des_active_rxq[i];
3569 		bd_dma += size;
3570 		cbd_base = (struct bufdesc *)(((void *)cbd_base) + size);
3571 		rxq->bd.last = (struct bufdesc *)(((void *)cbd_base) - dsize);
3572 	}
3573 
3574 	for (i = 0; i < fep->num_tx_queues; i++) {
3575 		struct fec_enet_priv_tx_q *txq = fep->tx_queue[i];
3576 		unsigned size = dsize * txq->bd.ring_size;
3577 
3578 		txq->bd.qid = i;
3579 		txq->bd.base = cbd_base;
3580 		txq->bd.cur = cbd_base;
3581 		txq->bd.dma = bd_dma;
3582 		txq->bd.dsize = dsize;
3583 		txq->bd.dsize_log2 = dsize_log2;
3584 		txq->bd.reg_desc_active = fep->hwp + offset_des_active_txq[i];
3585 		bd_dma += size;
3586 		cbd_base = (struct bufdesc *)(((void *)cbd_base) + size);
3587 		txq->bd.last = (struct bufdesc *)(((void *)cbd_base) - dsize);
3588 	}
3589 
3590 
3591 	/* The FEC Ethernet specific entries in the device structure */
3592 	ndev->watchdog_timeo = TX_TIMEOUT;
3593 	ndev->netdev_ops = &fec_netdev_ops;
3594 	ndev->ethtool_ops = &fec_enet_ethtool_ops;
3595 
3596 	writel(FEC_RX_DISABLED_IMASK, fep->hwp + FEC_IMASK);
3597 	netif_napi_add(ndev, &fep->napi, fec_enet_rx_napi);
3598 
3599 	if (fep->quirks & FEC_QUIRK_HAS_VLAN)
3600 		/* enable hw VLAN support */
3601 		ndev->features |= NETIF_F_HW_VLAN_CTAG_RX;
3602 
3603 	if (fep->quirks & FEC_QUIRK_HAS_CSUM) {
3604 		netif_set_tso_max_segs(ndev, FEC_MAX_TSO_SEGS);
3605 
3606 		/* enable hw accelerator */
3607 		ndev->features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
3608 				| NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_TSO);
3609 		fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
3610 	}
3611 
3612 	if (fep->quirks & FEC_QUIRK_HAS_MULTI_QUEUES) {
3613 		fep->tx_align = 0;
3614 		fep->rx_align = 0x3f;
3615 	}
3616 
3617 	ndev->hw_features = ndev->features;
3618 
3619 	fec_restart(ndev);
3620 
3621 	if (fep->quirks & FEC_QUIRK_MIB_CLEAR)
3622 		fec_enet_clear_ethtool_stats(ndev);
3623 	else
3624 		fec_enet_update_ethtool_stats(ndev);
3625 
3626 	return 0;
3627 
3628 free_queue_mem:
3629 	fec_enet_free_queue(ndev);
3630 	return ret;
3631 }
3632 
3633 #ifdef CONFIG_OF
3634 static int fec_reset_phy(struct platform_device *pdev)
3635 {
3636 	int err, phy_reset;
3637 	bool active_high = false;
3638 	int msec = 1, phy_post_delay = 0;
3639 	struct device_node *np = pdev->dev.of_node;
3640 
3641 	if (!np)
3642 		return 0;
3643 
3644 	err = of_property_read_u32(np, "phy-reset-duration", &msec);
3645 	/* A sane reset duration should not be longer than 1s */
3646 	if (!err && msec > 1000)
3647 		msec = 1;
3648 
3649 	phy_reset = of_get_named_gpio(np, "phy-reset-gpios", 0);
3650 	if (phy_reset == -EPROBE_DEFER)
3651 		return phy_reset;
3652 	else if (!gpio_is_valid(phy_reset))
3653 		return 0;
3654 
3655 	err = of_property_read_u32(np, "phy-reset-post-delay", &phy_post_delay);
3656 	/* valid reset duration should be less than 1s */
3657 	if (!err && phy_post_delay > 1000)
3658 		return -EINVAL;
3659 
3660 	active_high = of_property_read_bool(np, "phy-reset-active-high");
3661 
3662 	err = devm_gpio_request_one(&pdev->dev, phy_reset,
3663 			active_high ? GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW,
3664 			"phy-reset");
3665 	if (err) {
3666 		dev_err(&pdev->dev, "failed to get phy-reset-gpios: %d\n", err);
3667 		return err;
3668 	}
3669 
3670 	if (msec > 20)
3671 		msleep(msec);
3672 	else
3673 		usleep_range(msec * 1000, msec * 1000 + 1000);
3674 
3675 	gpio_set_value_cansleep(phy_reset, !active_high);
3676 
3677 	if (!phy_post_delay)
3678 		return 0;
3679 
3680 	if (phy_post_delay > 20)
3681 		msleep(phy_post_delay);
3682 	else
3683 		usleep_range(phy_post_delay * 1000,
3684 			     phy_post_delay * 1000 + 1000);
3685 
3686 	return 0;
3687 }
3688 #else /* CONFIG_OF */
3689 static int fec_reset_phy(struct platform_device *pdev)
3690 {
3691 	/*
3692 	 * In case of platform probe, the reset has been done
3693 	 * by machine code.
3694 	 */
3695 	return 0;
3696 }
3697 #endif /* CONFIG_OF */
3698 
3699 static void
3700 fec_enet_get_queue_num(struct platform_device *pdev, int *num_tx, int *num_rx)
3701 {
3702 	struct device_node *np = pdev->dev.of_node;
3703 
3704 	*num_tx = *num_rx = 1;
3705 
3706 	if (!np || !of_device_is_available(np))
3707 		return;
3708 
3709 	/* parse the num of tx and rx queues */
3710 	of_property_read_u32(np, "fsl,num-tx-queues", num_tx);
3711 
3712 	of_property_read_u32(np, "fsl,num-rx-queues", num_rx);
3713 
3714 	if (*num_tx < 1 || *num_tx > FEC_ENET_MAX_TX_QS) {
3715 		dev_warn(&pdev->dev, "Invalid num_tx(=%d), fall back to 1\n",
3716 			 *num_tx);
3717 		*num_tx = 1;
3718 		return;
3719 	}
3720 
3721 	if (*num_rx < 1 || *num_rx > FEC_ENET_MAX_RX_QS) {
3722 		dev_warn(&pdev->dev, "Invalid num_rx(=%d), fall back to 1\n",
3723 			 *num_rx);
3724 		*num_rx = 1;
3725 		return;
3726 	}
3727 
3728 }
3729 
3730 static int fec_enet_get_irq_cnt(struct platform_device *pdev)
3731 {
3732 	int irq_cnt = platform_irq_count(pdev);
3733 
3734 	if (irq_cnt > FEC_IRQ_NUM)
3735 		irq_cnt = FEC_IRQ_NUM;	/* last for pps */
3736 	else if (irq_cnt == 2)
3737 		irq_cnt = 1;	/* last for pps */
3738 	else if (irq_cnt <= 0)
3739 		irq_cnt = 1;	/* At least 1 irq is needed */
3740 	return irq_cnt;
3741 }
3742 
3743 static void fec_enet_get_wakeup_irq(struct platform_device *pdev)
3744 {
3745 	struct net_device *ndev = platform_get_drvdata(pdev);
3746 	struct fec_enet_private *fep = netdev_priv(ndev);
3747 
3748 	if (fep->quirks & FEC_QUIRK_WAKEUP_FROM_INT2)
3749 		fep->wake_irq = fep->irq[2];
3750 	else
3751 		fep->wake_irq = fep->irq[0];
3752 }
3753 
3754 static int fec_enet_init_stop_mode(struct fec_enet_private *fep,
3755 				   struct device_node *np)
3756 {
3757 	struct device_node *gpr_np;
3758 	u32 out_val[3];
3759 	int ret = 0;
3760 
3761 	gpr_np = of_parse_phandle(np, "fsl,stop-mode", 0);
3762 	if (!gpr_np)
3763 		return 0;
3764 
3765 	ret = of_property_read_u32_array(np, "fsl,stop-mode", out_val,
3766 					 ARRAY_SIZE(out_val));
3767 	if (ret) {
3768 		dev_dbg(&fep->pdev->dev, "no stop mode property\n");
3769 		goto out;
3770 	}
3771 
3772 	fep->stop_gpr.gpr = syscon_node_to_regmap(gpr_np);
3773 	if (IS_ERR(fep->stop_gpr.gpr)) {
3774 		dev_err(&fep->pdev->dev, "could not find gpr regmap\n");
3775 		ret = PTR_ERR(fep->stop_gpr.gpr);
3776 		fep->stop_gpr.gpr = NULL;
3777 		goto out;
3778 	}
3779 
3780 	fep->stop_gpr.reg = out_val[1];
3781 	fep->stop_gpr.bit = out_val[2];
3782 
3783 out:
3784 	of_node_put(gpr_np);
3785 
3786 	return ret;
3787 }
3788 
3789 static int
3790 fec_probe(struct platform_device *pdev)
3791 {
3792 	struct fec_enet_private *fep;
3793 	struct fec_platform_data *pdata;
3794 	phy_interface_t interface;
3795 	struct net_device *ndev;
3796 	int i, irq, ret = 0;
3797 	const struct of_device_id *of_id;
3798 	static int dev_id;
3799 	struct device_node *np = pdev->dev.of_node, *phy_node;
3800 	int num_tx_qs;
3801 	int num_rx_qs;
3802 	char irq_name[8];
3803 	int irq_cnt;
3804 	struct fec_devinfo *dev_info;
3805 
3806 	fec_enet_get_queue_num(pdev, &num_tx_qs, &num_rx_qs);
3807 
3808 	/* Init network device */
3809 	ndev = alloc_etherdev_mqs(sizeof(struct fec_enet_private) +
3810 				  FEC_STATS_SIZE, num_tx_qs, num_rx_qs);
3811 	if (!ndev)
3812 		return -ENOMEM;
3813 
3814 	SET_NETDEV_DEV(ndev, &pdev->dev);
3815 
3816 	/* setup board info structure */
3817 	fep = netdev_priv(ndev);
3818 
3819 	of_id = of_match_device(fec_dt_ids, &pdev->dev);
3820 	if (of_id)
3821 		pdev->id_entry = of_id->data;
3822 	dev_info = (struct fec_devinfo *)pdev->id_entry->driver_data;
3823 	if (dev_info)
3824 		fep->quirks = dev_info->quirks;
3825 
3826 	fep->netdev = ndev;
3827 	fep->num_rx_queues = num_rx_qs;
3828 	fep->num_tx_queues = num_tx_qs;
3829 
3830 #if !defined(CONFIG_M5272)
3831 	/* default enable pause frame auto negotiation */
3832 	if (fep->quirks & FEC_QUIRK_HAS_GBIT)
3833 		fep->pause_flag |= FEC_PAUSE_FLAG_AUTONEG;
3834 #endif
3835 
3836 	/* Select default pin state */
3837 	pinctrl_pm_select_default_state(&pdev->dev);
3838 
3839 	fep->hwp = devm_platform_ioremap_resource(pdev, 0);
3840 	if (IS_ERR(fep->hwp)) {
3841 		ret = PTR_ERR(fep->hwp);
3842 		goto failed_ioremap;
3843 	}
3844 
3845 	fep->pdev = pdev;
3846 	fep->dev_id = dev_id++;
3847 
3848 	platform_set_drvdata(pdev, ndev);
3849 
3850 	if ((of_machine_is_compatible("fsl,imx6q") ||
3851 	     of_machine_is_compatible("fsl,imx6dl")) &&
3852 	    !of_property_read_bool(np, "fsl,err006687-workaround-present"))
3853 		fep->quirks |= FEC_QUIRK_ERR006687;
3854 
3855 	ret = fec_enet_ipc_handle_init(fep);
3856 	if (ret)
3857 		goto failed_ipc_init;
3858 
3859 	if (of_get_property(np, "fsl,magic-packet", NULL))
3860 		fep->wol_flag |= FEC_WOL_HAS_MAGIC_PACKET;
3861 
3862 	ret = fec_enet_init_stop_mode(fep, np);
3863 	if (ret)
3864 		goto failed_stop_mode;
3865 
3866 	phy_node = of_parse_phandle(np, "phy-handle", 0);
3867 	if (!phy_node && of_phy_is_fixed_link(np)) {
3868 		ret = of_phy_register_fixed_link(np);
3869 		if (ret < 0) {
3870 			dev_err(&pdev->dev,
3871 				"broken fixed-link specification\n");
3872 			goto failed_phy;
3873 		}
3874 		phy_node = of_node_get(np);
3875 	}
3876 	fep->phy_node = phy_node;
3877 
3878 	ret = of_get_phy_mode(pdev->dev.of_node, &interface);
3879 	if (ret) {
3880 		pdata = dev_get_platdata(&pdev->dev);
3881 		if (pdata)
3882 			fep->phy_interface = pdata->phy;
3883 		else
3884 			fep->phy_interface = PHY_INTERFACE_MODE_MII;
3885 	} else {
3886 		fep->phy_interface = interface;
3887 	}
3888 
3889 	ret = fec_enet_parse_rgmii_delay(fep, np);
3890 	if (ret)
3891 		goto failed_rgmii_delay;
3892 
3893 	fep->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
3894 	if (IS_ERR(fep->clk_ipg)) {
3895 		ret = PTR_ERR(fep->clk_ipg);
3896 		goto failed_clk;
3897 	}
3898 
3899 	fep->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
3900 	if (IS_ERR(fep->clk_ahb)) {
3901 		ret = PTR_ERR(fep->clk_ahb);
3902 		goto failed_clk;
3903 	}
3904 
3905 	fep->itr_clk_rate = clk_get_rate(fep->clk_ahb);
3906 
3907 	/* enet_out is optional, depends on board */
3908 	fep->clk_enet_out = devm_clk_get_optional(&pdev->dev, "enet_out");
3909 	if (IS_ERR(fep->clk_enet_out)) {
3910 		ret = PTR_ERR(fep->clk_enet_out);
3911 		goto failed_clk;
3912 	}
3913 
3914 	fep->ptp_clk_on = false;
3915 	mutex_init(&fep->ptp_clk_mutex);
3916 
3917 	/* clk_ref is optional, depends on board */
3918 	fep->clk_ref = devm_clk_get_optional(&pdev->dev, "enet_clk_ref");
3919 	if (IS_ERR(fep->clk_ref)) {
3920 		ret = PTR_ERR(fep->clk_ref);
3921 		goto failed_clk;
3922 	}
3923 	fep->clk_ref_rate = clk_get_rate(fep->clk_ref);
3924 
3925 	/* clk_2x_txclk is optional, depends on board */
3926 	if (fep->rgmii_txc_dly || fep->rgmii_rxc_dly) {
3927 		fep->clk_2x_txclk = devm_clk_get(&pdev->dev, "enet_2x_txclk");
3928 		if (IS_ERR(fep->clk_2x_txclk))
3929 			fep->clk_2x_txclk = NULL;
3930 	}
3931 
3932 	fep->bufdesc_ex = fep->quirks & FEC_QUIRK_HAS_BUFDESC_EX;
3933 	fep->clk_ptp = devm_clk_get(&pdev->dev, "ptp");
3934 	if (IS_ERR(fep->clk_ptp)) {
3935 		fep->clk_ptp = NULL;
3936 		fep->bufdesc_ex = false;
3937 	}
3938 
3939 	ret = fec_enet_clk_enable(ndev, true);
3940 	if (ret)
3941 		goto failed_clk;
3942 
3943 	ret = clk_prepare_enable(fep->clk_ipg);
3944 	if (ret)
3945 		goto failed_clk_ipg;
3946 	ret = clk_prepare_enable(fep->clk_ahb);
3947 	if (ret)
3948 		goto failed_clk_ahb;
3949 
3950 	fep->reg_phy = devm_regulator_get_optional(&pdev->dev, "phy");
3951 	if (!IS_ERR(fep->reg_phy)) {
3952 		ret = regulator_enable(fep->reg_phy);
3953 		if (ret) {
3954 			dev_err(&pdev->dev,
3955 				"Failed to enable phy regulator: %d\n", ret);
3956 			goto failed_regulator;
3957 		}
3958 	} else {
3959 		if (PTR_ERR(fep->reg_phy) == -EPROBE_DEFER) {
3960 			ret = -EPROBE_DEFER;
3961 			goto failed_regulator;
3962 		}
3963 		fep->reg_phy = NULL;
3964 	}
3965 
3966 	pm_runtime_set_autosuspend_delay(&pdev->dev, FEC_MDIO_PM_TIMEOUT);
3967 	pm_runtime_use_autosuspend(&pdev->dev);
3968 	pm_runtime_get_noresume(&pdev->dev);
3969 	pm_runtime_set_active(&pdev->dev);
3970 	pm_runtime_enable(&pdev->dev);
3971 
3972 	ret = fec_reset_phy(pdev);
3973 	if (ret)
3974 		goto failed_reset;
3975 
3976 	irq_cnt = fec_enet_get_irq_cnt(pdev);
3977 	if (fep->bufdesc_ex)
3978 		fec_ptp_init(pdev, irq_cnt);
3979 
3980 	ret = fec_enet_init(ndev);
3981 	if (ret)
3982 		goto failed_init;
3983 
3984 	for (i = 0; i < irq_cnt; i++) {
3985 		snprintf(irq_name, sizeof(irq_name), "int%d", i);
3986 		irq = platform_get_irq_byname_optional(pdev, irq_name);
3987 		if (irq < 0)
3988 			irq = platform_get_irq(pdev, i);
3989 		if (irq < 0) {
3990 			ret = irq;
3991 			goto failed_irq;
3992 		}
3993 		ret = devm_request_irq(&pdev->dev, irq, fec_enet_interrupt,
3994 				       0, pdev->name, ndev);
3995 		if (ret)
3996 			goto failed_irq;
3997 
3998 		fep->irq[i] = irq;
3999 	}
4000 
4001 	/* Decide which interrupt line is wakeup capable */
4002 	fec_enet_get_wakeup_irq(pdev);
4003 
4004 	ret = fec_enet_mii_init(pdev);
4005 	if (ret)
4006 		goto failed_mii_init;
4007 
4008 	/* Carrier starts down, phylib will bring it up */
4009 	netif_carrier_off(ndev);
4010 	fec_enet_clk_enable(ndev, false);
4011 	pinctrl_pm_select_sleep_state(&pdev->dev);
4012 
4013 	ndev->max_mtu = PKT_MAXBUF_SIZE - ETH_HLEN - ETH_FCS_LEN;
4014 
4015 	ret = register_netdev(ndev);
4016 	if (ret)
4017 		goto failed_register;
4018 
4019 	device_init_wakeup(&ndev->dev, fep->wol_flag &
4020 			   FEC_WOL_HAS_MAGIC_PACKET);
4021 
4022 	if (fep->bufdesc_ex && fep->ptp_clock)
4023 		netdev_info(ndev, "registered PHC device %d\n", fep->dev_id);
4024 
4025 	fep->rx_copybreak = COPYBREAK_DEFAULT;
4026 	INIT_WORK(&fep->tx_timeout_work, fec_enet_timeout_work);
4027 
4028 	pm_runtime_mark_last_busy(&pdev->dev);
4029 	pm_runtime_put_autosuspend(&pdev->dev);
4030 
4031 	return 0;
4032 
4033 failed_register:
4034 	fec_enet_mii_remove(fep);
4035 failed_mii_init:
4036 failed_irq:
4037 failed_init:
4038 	fec_ptp_stop(pdev);
4039 failed_reset:
4040 	pm_runtime_put_noidle(&pdev->dev);
4041 	pm_runtime_disable(&pdev->dev);
4042 	if (fep->reg_phy)
4043 		regulator_disable(fep->reg_phy);
4044 failed_regulator:
4045 	clk_disable_unprepare(fep->clk_ahb);
4046 failed_clk_ahb:
4047 	clk_disable_unprepare(fep->clk_ipg);
4048 failed_clk_ipg:
4049 	fec_enet_clk_enable(ndev, false);
4050 failed_clk:
4051 failed_rgmii_delay:
4052 	if (of_phy_is_fixed_link(np))
4053 		of_phy_deregister_fixed_link(np);
4054 	of_node_put(phy_node);
4055 failed_stop_mode:
4056 failed_ipc_init:
4057 failed_phy:
4058 	dev_id--;
4059 failed_ioremap:
4060 	free_netdev(ndev);
4061 
4062 	return ret;
4063 }
4064 
4065 static int
4066 fec_drv_remove(struct platform_device *pdev)
4067 {
4068 	struct net_device *ndev = platform_get_drvdata(pdev);
4069 	struct fec_enet_private *fep = netdev_priv(ndev);
4070 	struct device_node *np = pdev->dev.of_node;
4071 	int ret;
4072 
4073 	ret = pm_runtime_resume_and_get(&pdev->dev);
4074 	if (ret < 0)
4075 		return ret;
4076 
4077 	cancel_work_sync(&fep->tx_timeout_work);
4078 	fec_ptp_stop(pdev);
4079 	unregister_netdev(ndev);
4080 	fec_enet_mii_remove(fep);
4081 	if (fep->reg_phy)
4082 		regulator_disable(fep->reg_phy);
4083 
4084 	if (of_phy_is_fixed_link(np))
4085 		of_phy_deregister_fixed_link(np);
4086 	of_node_put(fep->phy_node);
4087 
4088 	clk_disable_unprepare(fep->clk_ahb);
4089 	clk_disable_unprepare(fep->clk_ipg);
4090 	pm_runtime_put_noidle(&pdev->dev);
4091 	pm_runtime_disable(&pdev->dev);
4092 
4093 	free_netdev(ndev);
4094 	return 0;
4095 }
4096 
4097 static int __maybe_unused fec_suspend(struct device *dev)
4098 {
4099 	struct net_device *ndev = dev_get_drvdata(dev);
4100 	struct fec_enet_private *fep = netdev_priv(ndev);
4101 	int ret;
4102 
4103 	rtnl_lock();
4104 	if (netif_running(ndev)) {
4105 		if (fep->wol_flag & FEC_WOL_FLAG_ENABLE)
4106 			fep->wol_flag |= FEC_WOL_FLAG_SLEEP_ON;
4107 		phy_stop(ndev->phydev);
4108 		napi_disable(&fep->napi);
4109 		netif_tx_lock_bh(ndev);
4110 		netif_device_detach(ndev);
4111 		netif_tx_unlock_bh(ndev);
4112 		fec_stop(ndev);
4113 		if (!(fep->wol_flag & FEC_WOL_FLAG_ENABLE)) {
4114 			fec_irqs_disable(ndev);
4115 			pinctrl_pm_select_sleep_state(&fep->pdev->dev);
4116 		} else {
4117 			fec_irqs_disable_except_wakeup(ndev);
4118 			if (fep->wake_irq > 0) {
4119 				disable_irq(fep->wake_irq);
4120 				enable_irq_wake(fep->wake_irq);
4121 			}
4122 			fec_enet_stop_mode(fep, true);
4123 		}
4124 		/* It's safe to disable clocks since interrupts are masked */
4125 		fec_enet_clk_enable(ndev, false);
4126 
4127 		fep->rpm_active = !pm_runtime_status_suspended(dev);
4128 		if (fep->rpm_active) {
4129 			ret = pm_runtime_force_suspend(dev);
4130 			if (ret < 0) {
4131 				rtnl_unlock();
4132 				return ret;
4133 			}
4134 		}
4135 	}
4136 	rtnl_unlock();
4137 
4138 	if (fep->reg_phy && !(fep->wol_flag & FEC_WOL_FLAG_ENABLE))
4139 		regulator_disable(fep->reg_phy);
4140 
4141 	/* SOC supply clock to phy, when clock is disabled, phy link down
4142 	 * SOC control phy regulator, when regulator is disabled, phy link down
4143 	 */
4144 	if (fep->clk_enet_out || fep->reg_phy)
4145 		fep->link = 0;
4146 
4147 	return 0;
4148 }
4149 
4150 static int __maybe_unused fec_resume(struct device *dev)
4151 {
4152 	struct net_device *ndev = dev_get_drvdata(dev);
4153 	struct fec_enet_private *fep = netdev_priv(ndev);
4154 	int ret;
4155 	int val;
4156 
4157 	if (fep->reg_phy && !(fep->wol_flag & FEC_WOL_FLAG_ENABLE)) {
4158 		ret = regulator_enable(fep->reg_phy);
4159 		if (ret)
4160 			return ret;
4161 	}
4162 
4163 	rtnl_lock();
4164 	if (netif_running(ndev)) {
4165 		if (fep->rpm_active)
4166 			pm_runtime_force_resume(dev);
4167 
4168 		ret = fec_enet_clk_enable(ndev, true);
4169 		if (ret) {
4170 			rtnl_unlock();
4171 			goto failed_clk;
4172 		}
4173 		if (fep->wol_flag & FEC_WOL_FLAG_ENABLE) {
4174 			fec_enet_stop_mode(fep, false);
4175 			if (fep->wake_irq) {
4176 				disable_irq_wake(fep->wake_irq);
4177 				enable_irq(fep->wake_irq);
4178 			}
4179 
4180 			val = readl(fep->hwp + FEC_ECNTRL);
4181 			val &= ~(FEC_ECR_MAGICEN | FEC_ECR_SLEEP);
4182 			writel(val, fep->hwp + FEC_ECNTRL);
4183 			fep->wol_flag &= ~FEC_WOL_FLAG_SLEEP_ON;
4184 		} else {
4185 			pinctrl_pm_select_default_state(&fep->pdev->dev);
4186 		}
4187 		fec_restart(ndev);
4188 		netif_tx_lock_bh(ndev);
4189 		netif_device_attach(ndev);
4190 		netif_tx_unlock_bh(ndev);
4191 		napi_enable(&fep->napi);
4192 		phy_init_hw(ndev->phydev);
4193 		phy_start(ndev->phydev);
4194 	}
4195 	rtnl_unlock();
4196 
4197 	return 0;
4198 
4199 failed_clk:
4200 	if (fep->reg_phy)
4201 		regulator_disable(fep->reg_phy);
4202 	return ret;
4203 }
4204 
4205 static int __maybe_unused fec_runtime_suspend(struct device *dev)
4206 {
4207 	struct net_device *ndev = dev_get_drvdata(dev);
4208 	struct fec_enet_private *fep = netdev_priv(ndev);
4209 
4210 	clk_disable_unprepare(fep->clk_ahb);
4211 	clk_disable_unprepare(fep->clk_ipg);
4212 
4213 	return 0;
4214 }
4215 
4216 static int __maybe_unused fec_runtime_resume(struct device *dev)
4217 {
4218 	struct net_device *ndev = dev_get_drvdata(dev);
4219 	struct fec_enet_private *fep = netdev_priv(ndev);
4220 	int ret;
4221 
4222 	ret = clk_prepare_enable(fep->clk_ahb);
4223 	if (ret)
4224 		return ret;
4225 	ret = clk_prepare_enable(fep->clk_ipg);
4226 	if (ret)
4227 		goto failed_clk_ipg;
4228 
4229 	return 0;
4230 
4231 failed_clk_ipg:
4232 	clk_disable_unprepare(fep->clk_ahb);
4233 	return ret;
4234 }
4235 
4236 static const struct dev_pm_ops fec_pm_ops = {
4237 	SET_SYSTEM_SLEEP_PM_OPS(fec_suspend, fec_resume)
4238 	SET_RUNTIME_PM_OPS(fec_runtime_suspend, fec_runtime_resume, NULL)
4239 };
4240 
4241 static struct platform_driver fec_driver = {
4242 	.driver	= {
4243 		.name	= DRIVER_NAME,
4244 		.pm	= &fec_pm_ops,
4245 		.of_match_table = fec_dt_ids,
4246 		.suppress_bind_attrs = true,
4247 	},
4248 	.id_table = fec_devtype,
4249 	.probe	= fec_probe,
4250 	.remove	= fec_drv_remove,
4251 };
4252 
4253 module_platform_driver(fec_driver);
4254 
4255 MODULE_LICENSE("GPL");
4256