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