1 // SPDX-License-Identifier: GPL-2.0+
3 * Fast Ethernet Controller (FEC) driver for Motorola MPC8xx.
4 * Copyright (c) 1997 Dan Malek (dmalek@jlc.net)
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
13 * Much better multiple PHY support by Magnus Damm.
14 * Copyright (c) 2000 Ericsson Radio Systems AB.
16 * Support for FEC controller of ColdFire processors.
17 * Copyright (c) 2001-2005 Greg Ungerer (gerg@snapgear.com)
19 * Bug fixes and cleanup by Philippe De Muyter (phdm@macqel.be)
20 * Copyright (c) 2004-2006 Macq Electronique SA.
22 * Copyright (C) 2010-2011 Freescale Semiconductor, Inc.
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>
41 #include <net/selftests.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>
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>
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>
70 #include <asm/cacheflush.h>
74 static void set_multicast_list(struct net_device *ndev);
75 static void fec_enet_itr_coal_init(struct net_device *ndev);
77 #define DRIVER_NAME "fec"
79 static const u16 fec_enet_vlan_pri_to_queue[8] = {0, 0, 1, 1, 1, 2, 2, 2};
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 */
94 static const struct fec_devinfo fec_imx25_info = {
95 .quirks = FEC_QUIRK_USE_GASKET | FEC_QUIRK_MIB_CLEAR |
99 static const struct fec_devinfo fec_imx27_info = {
100 .quirks = FEC_QUIRK_MIB_CLEAR | FEC_QUIRK_HAS_FRREG,
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,
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,
117 static const struct fec_devinfo fec_mvf600_info = {
118 .quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_RACC,
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,
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,
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,
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,
158 static struct platform_device_id fec_devtype[] = {
160 /* keep it for coldfire */
165 .driver_data = (kernel_ulong_t)&fec_imx25_info,
168 .driver_data = (kernel_ulong_t)&fec_imx27_info,
171 .driver_data = (kernel_ulong_t)&fec_imx28_info,
174 .driver_data = (kernel_ulong_t)&fec_imx6q_info,
176 .name = "mvf600-fec",
177 .driver_data = (kernel_ulong_t)&fec_mvf600_info,
179 .name = "imx6sx-fec",
180 .driver_data = (kernel_ulong_t)&fec_imx6x_info,
182 .name = "imx6ul-fec",
183 .driver_data = (kernel_ulong_t)&fec_imx6ul_info,
185 .name = "imx8mq-fec",
186 .driver_data = (kernel_ulong_t)&fec_imx8mq_info,
188 .name = "imx8qm-fec",
189 .driver_data = (kernel_ulong_t)&fec_imx8qm_info,
194 MODULE_DEVICE_TABLE(platform, fec_devtype);
197 IMX25_FEC = 1, /* runs on i.mx25/50/53 */
198 IMX27_FEC, /* runs on i.mx27/35/51 */
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], },
220 MODULE_DEVICE_TABLE(of, fec_dt_ids);
222 static unsigned char macaddr[ETH_ALEN];
223 module_param_array(macaddr, byte, NULL, 0);
224 MODULE_PARM_DESC(macaddr, "FEC Ethernet MAC address");
226 #if defined(CONFIG_M5272)
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.
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
242 #define FEC_FLASHMAC 0
244 #endif /* CONFIG_M5272 */
246 /* The FEC stores dest/src/type/vlan, data, and checksum for receive packets.
248 * 2048 byte skbufs are allocated. However, alignment requirements
249 * varies between FEC variants. Worst case is 64, so round down by 64.
251 #define PKT_MAXBUF_SIZE (round_down(2048 - 64, 64))
252 #define PKT_MINBUF_SIZE 64
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)
260 /* MIB Control Register */
261 #define FEC_MIB_CTRLSTAT_DISABLE BIT(31)
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.
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)
273 #define OPT_FRAME_SIZE 0
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_MAGICEN (1 << 2)
289 #define FEC_ECR_SLEEP (1 << 3)
291 #define FEC_MII_TIMEOUT 30000 /* us */
293 /* Transmitter timeout */
294 #define TX_TIMEOUT (2 * HZ)
296 #define FEC_PAUSE_FLAG_AUTONEG 0x1
297 #define FEC_PAUSE_FLAG_ENABLE 0x2
298 #define FEC_WOL_HAS_MAGIC_PACKET (0x1 << 0)
299 #define FEC_WOL_FLAG_ENABLE (0x1 << 1)
300 #define FEC_WOL_FLAG_SLEEP_ON (0x1 << 2)
302 #define COPYBREAK_DEFAULT 256
304 /* Max number of allowed TCP segments for software TSO */
305 #define FEC_MAX_TSO_SEGS 100
306 #define FEC_MAX_SKB_DESCS (FEC_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS)
308 #define IS_TSO_HEADER(txq, addr) \
309 ((addr >= txq->tso_hdrs_dma) && \
310 (addr < txq->tso_hdrs_dma + txq->bd.ring_size * TSO_HEADER_SIZE))
314 static struct bufdesc *fec_enet_get_nextdesc(struct bufdesc *bdp,
315 struct bufdesc_prop *bd)
317 return (bdp >= bd->last) ? bd->base
318 : (struct bufdesc *)(((void *)bdp) + bd->dsize);
321 static struct bufdesc *fec_enet_get_prevdesc(struct bufdesc *bdp,
322 struct bufdesc_prop *bd)
324 return (bdp <= bd->base) ? bd->last
325 : (struct bufdesc *)(((void *)bdp) - bd->dsize);
328 static int fec_enet_get_bd_index(struct bufdesc *bdp,
329 struct bufdesc_prop *bd)
331 return ((const char *)bdp - (const char *)bd->base) >> bd->dsize_log2;
334 static int fec_enet_get_free_txdesc_num(struct fec_enet_priv_tx_q *txq)
338 entries = (((const char *)txq->dirty_tx -
339 (const char *)txq->bd.cur) >> txq->bd.dsize_log2) - 1;
341 return entries >= 0 ? entries : entries + txq->bd.ring_size;
344 static void swap_buffer(void *bufaddr, int len)
347 unsigned int *buf = bufaddr;
349 for (i = 0; i < len; i += 4, buf++)
353 static void swap_buffer2(void *dst_buf, void *src_buf, int len)
356 unsigned int *src = src_buf;
357 unsigned int *dst = dst_buf;
359 for (i = 0; i < len; i += 4, src++, dst++)
363 static void fec_dump(struct net_device *ndev)
365 struct fec_enet_private *fep = netdev_priv(ndev);
367 struct fec_enet_priv_tx_q *txq;
370 netdev_info(ndev, "TX ring dump\n");
371 pr_info("Nr SC addr len SKB\n");
373 txq = fep->tx_queue[0];
377 pr_info("%3u %c%c 0x%04x 0x%08x %4u %p\n",
379 bdp == txq->bd.cur ? 'S' : ' ',
380 bdp == txq->dirty_tx ? 'H' : ' ',
381 fec16_to_cpu(bdp->cbd_sc),
382 fec32_to_cpu(bdp->cbd_bufaddr),
383 fec16_to_cpu(bdp->cbd_datlen),
384 txq->tx_skbuff[index]);
385 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
387 } while (bdp != txq->bd.base);
390 static inline bool is_ipv4_pkt(struct sk_buff *skb)
392 return skb->protocol == htons(ETH_P_IP) && ip_hdr(skb)->version == 4;
396 fec_enet_clear_csum(struct sk_buff *skb, struct net_device *ndev)
398 /* Only run for packets requiring a checksum. */
399 if (skb->ip_summed != CHECKSUM_PARTIAL)
402 if (unlikely(skb_cow_head(skb, 0)))
405 if (is_ipv4_pkt(skb))
406 ip_hdr(skb)->check = 0;
407 *(__sum16 *)(skb->head + skb->csum_start + skb->csum_offset) = 0;
412 static struct bufdesc *
413 fec_enet_txq_submit_frag_skb(struct fec_enet_priv_tx_q *txq,
415 struct net_device *ndev)
417 struct fec_enet_private *fep = netdev_priv(ndev);
418 struct bufdesc *bdp = txq->bd.cur;
419 struct bufdesc_ex *ebdp;
420 int nr_frags = skb_shinfo(skb)->nr_frags;
422 unsigned short status;
423 unsigned int estatus = 0;
424 skb_frag_t *this_frag;
430 for (frag = 0; frag < nr_frags; frag++) {
431 this_frag = &skb_shinfo(skb)->frags[frag];
432 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
433 ebdp = (struct bufdesc_ex *)bdp;
435 status = fec16_to_cpu(bdp->cbd_sc);
436 status &= ~BD_ENET_TX_STATS;
437 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
438 frag_len = skb_frag_size(&skb_shinfo(skb)->frags[frag]);
440 /* Handle the last BD specially */
441 if (frag == nr_frags - 1) {
442 status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
443 if (fep->bufdesc_ex) {
444 estatus |= BD_ENET_TX_INT;
445 if (unlikely(skb_shinfo(skb)->tx_flags &
446 SKBTX_HW_TSTAMP && fep->hwts_tx_en))
447 estatus |= BD_ENET_TX_TS;
451 if (fep->bufdesc_ex) {
452 if (fep->quirks & FEC_QUIRK_HAS_AVB)
453 estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
454 if (skb->ip_summed == CHECKSUM_PARTIAL)
455 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
458 ebdp->cbd_esc = cpu_to_fec32(estatus);
461 bufaddr = skb_frag_address(this_frag);
463 index = fec_enet_get_bd_index(bdp, &txq->bd);
464 if (((unsigned long) bufaddr) & fep->tx_align ||
465 fep->quirks & FEC_QUIRK_SWAP_FRAME) {
466 memcpy(txq->tx_bounce[index], bufaddr, frag_len);
467 bufaddr = txq->tx_bounce[index];
469 if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
470 swap_buffer(bufaddr, frag_len);
473 addr = dma_map_single(&fep->pdev->dev, bufaddr, frag_len,
475 if (dma_mapping_error(&fep->pdev->dev, addr)) {
477 netdev_err(ndev, "Tx DMA memory map failed\n");
478 goto dma_mapping_error;
481 bdp->cbd_bufaddr = cpu_to_fec32(addr);
482 bdp->cbd_datlen = cpu_to_fec16(frag_len);
483 /* Make sure the updates to rest of the descriptor are
484 * performed before transferring ownership.
487 bdp->cbd_sc = cpu_to_fec16(status);
493 for (i = 0; i < frag; i++) {
494 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
495 dma_unmap_single(&fep->pdev->dev, fec32_to_cpu(bdp->cbd_bufaddr),
496 fec16_to_cpu(bdp->cbd_datlen), DMA_TO_DEVICE);
498 return ERR_PTR(-ENOMEM);
501 static int fec_enet_txq_submit_skb(struct fec_enet_priv_tx_q *txq,
502 struct sk_buff *skb, struct net_device *ndev)
504 struct fec_enet_private *fep = netdev_priv(ndev);
505 int nr_frags = skb_shinfo(skb)->nr_frags;
506 struct bufdesc *bdp, *last_bdp;
509 unsigned short status;
510 unsigned short buflen;
511 unsigned int estatus = 0;
515 entries_free = fec_enet_get_free_txdesc_num(txq);
516 if (entries_free < MAX_SKB_FRAGS + 1) {
517 dev_kfree_skb_any(skb);
519 netdev_err(ndev, "NOT enough BD for SG!\n");
523 /* Protocol checksum off-load for TCP and UDP. */
524 if (fec_enet_clear_csum(skb, ndev)) {
525 dev_kfree_skb_any(skb);
529 /* Fill in a Tx ring entry */
532 status = fec16_to_cpu(bdp->cbd_sc);
533 status &= ~BD_ENET_TX_STATS;
535 /* Set buffer length and buffer pointer */
537 buflen = skb_headlen(skb);
539 index = fec_enet_get_bd_index(bdp, &txq->bd);
540 if (((unsigned long) bufaddr) & fep->tx_align ||
541 fep->quirks & FEC_QUIRK_SWAP_FRAME) {
542 memcpy(txq->tx_bounce[index], skb->data, buflen);
543 bufaddr = txq->tx_bounce[index];
545 if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
546 swap_buffer(bufaddr, buflen);
549 /* Push the data cache so the CPM does not get stale memory data. */
550 addr = dma_map_single(&fep->pdev->dev, bufaddr, buflen, DMA_TO_DEVICE);
551 if (dma_mapping_error(&fep->pdev->dev, addr)) {
552 dev_kfree_skb_any(skb);
554 netdev_err(ndev, "Tx DMA memory map failed\n");
559 last_bdp = fec_enet_txq_submit_frag_skb(txq, skb, ndev);
560 if (IS_ERR(last_bdp)) {
561 dma_unmap_single(&fep->pdev->dev, addr,
562 buflen, DMA_TO_DEVICE);
563 dev_kfree_skb_any(skb);
567 status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
568 if (fep->bufdesc_ex) {
569 estatus = BD_ENET_TX_INT;
570 if (unlikely(skb_shinfo(skb)->tx_flags &
571 SKBTX_HW_TSTAMP && fep->hwts_tx_en))
572 estatus |= BD_ENET_TX_TS;
575 bdp->cbd_bufaddr = cpu_to_fec32(addr);
576 bdp->cbd_datlen = cpu_to_fec16(buflen);
578 if (fep->bufdesc_ex) {
580 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
582 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
584 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
586 if (fep->quirks & FEC_QUIRK_HAS_AVB)
587 estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
589 if (skb->ip_summed == CHECKSUM_PARTIAL)
590 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
593 ebdp->cbd_esc = cpu_to_fec32(estatus);
596 index = fec_enet_get_bd_index(last_bdp, &txq->bd);
597 /* Save skb pointer */
598 txq->tx_skbuff[index] = skb;
600 /* Make sure the updates to rest of the descriptor are performed before
601 * transferring ownership.
605 /* Send it on its way. Tell FEC it's ready, interrupt when done,
606 * it's the last BD of the frame, and to put the CRC on the end.
608 status |= (BD_ENET_TX_READY | BD_ENET_TX_TC);
609 bdp->cbd_sc = cpu_to_fec16(status);
611 /* If this was the last BD in the ring, start at the beginning again. */
612 bdp = fec_enet_get_nextdesc(last_bdp, &txq->bd);
614 skb_tx_timestamp(skb);
616 /* Make sure the update to bdp and tx_skbuff are performed before
622 /* Trigger transmission start */
623 writel(0, txq->bd.reg_desc_active);
629 fec_enet_txq_put_data_tso(struct fec_enet_priv_tx_q *txq, struct sk_buff *skb,
630 struct net_device *ndev,
631 struct bufdesc *bdp, int index, char *data,
632 int size, bool last_tcp, bool is_last)
634 struct fec_enet_private *fep = netdev_priv(ndev);
635 struct bufdesc_ex *ebdp = container_of(bdp, struct bufdesc_ex, desc);
636 unsigned short status;
637 unsigned int estatus = 0;
640 status = fec16_to_cpu(bdp->cbd_sc);
641 status &= ~BD_ENET_TX_STATS;
643 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
645 if (((unsigned long) data) & fep->tx_align ||
646 fep->quirks & FEC_QUIRK_SWAP_FRAME) {
647 memcpy(txq->tx_bounce[index], data, size);
648 data = txq->tx_bounce[index];
650 if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
651 swap_buffer(data, size);
654 addr = dma_map_single(&fep->pdev->dev, data, size, DMA_TO_DEVICE);
655 if (dma_mapping_error(&fep->pdev->dev, addr)) {
656 dev_kfree_skb_any(skb);
658 netdev_err(ndev, "Tx DMA memory map failed\n");
659 return NETDEV_TX_BUSY;
662 bdp->cbd_datlen = cpu_to_fec16(size);
663 bdp->cbd_bufaddr = cpu_to_fec32(addr);
665 if (fep->bufdesc_ex) {
666 if (fep->quirks & FEC_QUIRK_HAS_AVB)
667 estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
668 if (skb->ip_summed == CHECKSUM_PARTIAL)
669 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
671 ebdp->cbd_esc = cpu_to_fec32(estatus);
674 /* Handle the last BD specially */
676 status |= (BD_ENET_TX_LAST | BD_ENET_TX_TC);
678 status |= BD_ENET_TX_INTR;
680 ebdp->cbd_esc |= cpu_to_fec32(BD_ENET_TX_INT);
683 bdp->cbd_sc = cpu_to_fec16(status);
689 fec_enet_txq_put_hdr_tso(struct fec_enet_priv_tx_q *txq,
690 struct sk_buff *skb, struct net_device *ndev,
691 struct bufdesc *bdp, int index)
693 struct fec_enet_private *fep = netdev_priv(ndev);
694 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
695 struct bufdesc_ex *ebdp = container_of(bdp, struct bufdesc_ex, desc);
697 unsigned long dmabuf;
698 unsigned short status;
699 unsigned int estatus = 0;
701 status = fec16_to_cpu(bdp->cbd_sc);
702 status &= ~BD_ENET_TX_STATS;
703 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
705 bufaddr = txq->tso_hdrs + index * TSO_HEADER_SIZE;
706 dmabuf = txq->tso_hdrs_dma + index * TSO_HEADER_SIZE;
707 if (((unsigned long)bufaddr) & fep->tx_align ||
708 fep->quirks & FEC_QUIRK_SWAP_FRAME) {
709 memcpy(txq->tx_bounce[index], skb->data, hdr_len);
710 bufaddr = txq->tx_bounce[index];
712 if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
713 swap_buffer(bufaddr, hdr_len);
715 dmabuf = dma_map_single(&fep->pdev->dev, bufaddr,
716 hdr_len, DMA_TO_DEVICE);
717 if (dma_mapping_error(&fep->pdev->dev, dmabuf)) {
718 dev_kfree_skb_any(skb);
720 netdev_err(ndev, "Tx DMA memory map failed\n");
721 return NETDEV_TX_BUSY;
725 bdp->cbd_bufaddr = cpu_to_fec32(dmabuf);
726 bdp->cbd_datlen = cpu_to_fec16(hdr_len);
728 if (fep->bufdesc_ex) {
729 if (fep->quirks & FEC_QUIRK_HAS_AVB)
730 estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
731 if (skb->ip_summed == CHECKSUM_PARTIAL)
732 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
734 ebdp->cbd_esc = cpu_to_fec32(estatus);
737 bdp->cbd_sc = cpu_to_fec16(status);
742 static int fec_enet_txq_submit_tso(struct fec_enet_priv_tx_q *txq,
744 struct net_device *ndev)
746 struct fec_enet_private *fep = netdev_priv(ndev);
747 int hdr_len, total_len, data_left;
748 struct bufdesc *bdp = txq->bd.cur;
750 unsigned int index = 0;
753 if (tso_count_descs(skb) >= fec_enet_get_free_txdesc_num(txq)) {
754 dev_kfree_skb_any(skb);
756 netdev_err(ndev, "NOT enough BD for TSO!\n");
760 /* Protocol checksum off-load for TCP and UDP. */
761 if (fec_enet_clear_csum(skb, ndev)) {
762 dev_kfree_skb_any(skb);
766 /* Initialize the TSO handler, and prepare the first payload */
767 hdr_len = tso_start(skb, &tso);
769 total_len = skb->len - hdr_len;
770 while (total_len > 0) {
773 index = fec_enet_get_bd_index(bdp, &txq->bd);
774 data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
775 total_len -= data_left;
777 /* prepare packet headers: MAC + IP + TCP */
778 hdr = txq->tso_hdrs + index * TSO_HEADER_SIZE;
779 tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
780 ret = fec_enet_txq_put_hdr_tso(txq, skb, ndev, bdp, index);
784 while (data_left > 0) {
787 size = min_t(int, tso.size, data_left);
788 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
789 index = fec_enet_get_bd_index(bdp, &txq->bd);
790 ret = fec_enet_txq_put_data_tso(txq, skb, ndev,
799 tso_build_data(skb, &tso, size);
802 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
805 /* Save skb pointer */
806 txq->tx_skbuff[index] = skb;
808 skb_tx_timestamp(skb);
811 /* Trigger transmission start */
812 if (!(fep->quirks & FEC_QUIRK_ERR007885) ||
813 !readl(txq->bd.reg_desc_active) ||
814 !readl(txq->bd.reg_desc_active) ||
815 !readl(txq->bd.reg_desc_active) ||
816 !readl(txq->bd.reg_desc_active))
817 writel(0, txq->bd.reg_desc_active);
822 /* TODO: Release all used data descriptors for TSO */
827 fec_enet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
829 struct fec_enet_private *fep = netdev_priv(ndev);
831 unsigned short queue;
832 struct fec_enet_priv_tx_q *txq;
833 struct netdev_queue *nq;
836 queue = skb_get_queue_mapping(skb);
837 txq = fep->tx_queue[queue];
838 nq = netdev_get_tx_queue(ndev, queue);
841 ret = fec_enet_txq_submit_tso(txq, skb, ndev);
843 ret = fec_enet_txq_submit_skb(txq, skb, ndev);
847 entries_free = fec_enet_get_free_txdesc_num(txq);
848 if (entries_free <= txq->tx_stop_threshold)
849 netif_tx_stop_queue(nq);
854 /* Init RX & TX buffer descriptors
856 static void fec_enet_bd_init(struct net_device *dev)
858 struct fec_enet_private *fep = netdev_priv(dev);
859 struct fec_enet_priv_tx_q *txq;
860 struct fec_enet_priv_rx_q *rxq;
865 for (q = 0; q < fep->num_rx_queues; q++) {
866 /* Initialize the receive buffer descriptors. */
867 rxq = fep->rx_queue[q];
870 for (i = 0; i < rxq->bd.ring_size; i++) {
872 /* Initialize the BD for every fragment in the page. */
873 if (bdp->cbd_bufaddr)
874 bdp->cbd_sc = cpu_to_fec16(BD_ENET_RX_EMPTY);
876 bdp->cbd_sc = cpu_to_fec16(0);
877 bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
880 /* Set the last buffer to wrap */
881 bdp = fec_enet_get_prevdesc(bdp, &rxq->bd);
882 bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP);
884 rxq->bd.cur = rxq->bd.base;
887 for (q = 0; q < fep->num_tx_queues; q++) {
888 /* ...and the same for transmit */
889 txq = fep->tx_queue[q];
893 for (i = 0; i < txq->bd.ring_size; i++) {
894 /* Initialize the BD for every fragment in the page. */
895 bdp->cbd_sc = cpu_to_fec16(0);
896 if (bdp->cbd_bufaddr &&
897 !IS_TSO_HEADER(txq, fec32_to_cpu(bdp->cbd_bufaddr)))
898 dma_unmap_single(&fep->pdev->dev,
899 fec32_to_cpu(bdp->cbd_bufaddr),
900 fec16_to_cpu(bdp->cbd_datlen),
902 if (txq->tx_skbuff[i]) {
903 dev_kfree_skb_any(txq->tx_skbuff[i]);
904 txq->tx_skbuff[i] = NULL;
906 bdp->cbd_bufaddr = cpu_to_fec32(0);
907 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
910 /* Set the last buffer to wrap */
911 bdp = fec_enet_get_prevdesc(bdp, &txq->bd);
912 bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP);
917 static void fec_enet_active_rxring(struct net_device *ndev)
919 struct fec_enet_private *fep = netdev_priv(ndev);
922 for (i = 0; i < fep->num_rx_queues; i++)
923 writel(0, fep->rx_queue[i]->bd.reg_desc_active);
926 static void fec_enet_enable_ring(struct net_device *ndev)
928 struct fec_enet_private *fep = netdev_priv(ndev);
929 struct fec_enet_priv_tx_q *txq;
930 struct fec_enet_priv_rx_q *rxq;
933 for (i = 0; i < fep->num_rx_queues; i++) {
934 rxq = fep->rx_queue[i];
935 writel(rxq->bd.dma, fep->hwp + FEC_R_DES_START(i));
936 writel(PKT_MAXBUF_SIZE, fep->hwp + FEC_R_BUFF_SIZE(i));
940 writel(RCMR_MATCHEN | RCMR_CMP(i),
941 fep->hwp + FEC_RCMR(i));
944 for (i = 0; i < fep->num_tx_queues; i++) {
945 txq = fep->tx_queue[i];
946 writel(txq->bd.dma, fep->hwp + FEC_X_DES_START(i));
950 writel(DMA_CLASS_EN | IDLE_SLOPE(i),
951 fep->hwp + FEC_DMA_CFG(i));
955 static void fec_enet_reset_skb(struct net_device *ndev)
957 struct fec_enet_private *fep = netdev_priv(ndev);
958 struct fec_enet_priv_tx_q *txq;
961 for (i = 0; i < fep->num_tx_queues; i++) {
962 txq = fep->tx_queue[i];
964 for (j = 0; j < txq->bd.ring_size; j++) {
965 if (txq->tx_skbuff[j]) {
966 dev_kfree_skb_any(txq->tx_skbuff[j]);
967 txq->tx_skbuff[j] = NULL;
974 * This function is called to start or restart the FEC during a link
975 * change, transmit timeout, or to reconfigure the FEC. The network
976 * packet processing for this device must be stopped before this call.
979 fec_restart(struct net_device *ndev)
981 struct fec_enet_private *fep = netdev_priv(ndev);
983 u32 rcntl = OPT_FRAME_SIZE | 0x04;
984 u32 ecntl = 0x2; /* ETHEREN */
986 /* Whack a reset. We should wait for this.
987 * For i.MX6SX SOC, enet use AXI bus, we use disable MAC
988 * instead of reset MAC itself.
990 if (fep->quirks & FEC_QUIRK_HAS_MULTI_QUEUES ||
991 ((fep->quirks & FEC_QUIRK_NO_HARD_RESET) && fep->link)) {
992 writel(0, fep->hwp + FEC_ECNTRL);
994 writel(1, fep->hwp + FEC_ECNTRL);
999 * enet-mac reset will reset mac address registers too,
1000 * so need to reconfigure it.
1002 memcpy(&temp_mac, ndev->dev_addr, ETH_ALEN);
1003 writel((__force u32)cpu_to_be32(temp_mac[0]),
1004 fep->hwp + FEC_ADDR_LOW);
1005 writel((__force u32)cpu_to_be32(temp_mac[1]),
1006 fep->hwp + FEC_ADDR_HIGH);
1008 /* Clear any outstanding interrupt, except MDIO. */
1009 writel((0xffffffff & ~FEC_ENET_MII), fep->hwp + FEC_IEVENT);
1011 fec_enet_bd_init(ndev);
1013 fec_enet_enable_ring(ndev);
1015 /* Reset tx SKB buffers. */
1016 fec_enet_reset_skb(ndev);
1018 /* Enable MII mode */
1019 if (fep->full_duplex == DUPLEX_FULL) {
1021 writel(0x04, fep->hwp + FEC_X_CNTRL);
1023 /* No Rcv on Xmit */
1025 writel(0x0, fep->hwp + FEC_X_CNTRL);
1029 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
1031 #if !defined(CONFIG_M5272)
1032 if (fep->quirks & FEC_QUIRK_HAS_RACC) {
1033 u32 val = readl(fep->hwp + FEC_RACC);
1035 /* align IP header */
1036 val |= FEC_RACC_SHIFT16;
1037 if (fep->csum_flags & FLAG_RX_CSUM_ENABLED)
1038 /* set RX checksum */
1039 val |= FEC_RACC_OPTIONS;
1041 val &= ~FEC_RACC_OPTIONS;
1042 writel(val, fep->hwp + FEC_RACC);
1043 writel(PKT_MAXBUF_SIZE, fep->hwp + FEC_FTRL);
1048 * The phy interface and speed need to get configured
1049 * differently on enet-mac.
1051 if (fep->quirks & FEC_QUIRK_ENET_MAC) {
1052 /* Enable flow control and length check */
1053 rcntl |= 0x40000000 | 0x00000020;
1055 /* RGMII, RMII or MII */
1056 if (fep->phy_interface == PHY_INTERFACE_MODE_RGMII ||
1057 fep->phy_interface == PHY_INTERFACE_MODE_RGMII_ID ||
1058 fep->phy_interface == PHY_INTERFACE_MODE_RGMII_RXID ||
1059 fep->phy_interface == PHY_INTERFACE_MODE_RGMII_TXID)
1061 else if (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
1066 /* 1G, 100M or 10M */
1068 if (ndev->phydev->speed == SPEED_1000)
1070 else if (ndev->phydev->speed == SPEED_100)
1076 #ifdef FEC_MIIGSK_ENR
1077 if (fep->quirks & FEC_QUIRK_USE_GASKET) {
1079 /* disable the gasket and wait */
1080 writel(0, fep->hwp + FEC_MIIGSK_ENR);
1081 while (readl(fep->hwp + FEC_MIIGSK_ENR) & 4)
1085 * configure the gasket:
1086 * RMII, 50 MHz, no loopback, no echo
1087 * MII, 25 MHz, no loopback, no echo
1089 cfgr = (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
1090 ? BM_MIIGSK_CFGR_RMII : BM_MIIGSK_CFGR_MII;
1091 if (ndev->phydev && ndev->phydev->speed == SPEED_10)
1092 cfgr |= BM_MIIGSK_CFGR_FRCONT_10M;
1093 writel(cfgr, fep->hwp + FEC_MIIGSK_CFGR);
1095 /* re-enable the gasket */
1096 writel(2, fep->hwp + FEC_MIIGSK_ENR);
1101 #if !defined(CONFIG_M5272)
1102 /* enable pause frame*/
1103 if ((fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) ||
1104 ((fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) &&
1105 ndev->phydev && ndev->phydev->pause)) {
1106 rcntl |= FEC_ENET_FCE;
1108 /* set FIFO threshold parameter to reduce overrun */
1109 writel(FEC_ENET_RSEM_V, fep->hwp + FEC_R_FIFO_RSEM);
1110 writel(FEC_ENET_RSFL_V, fep->hwp + FEC_R_FIFO_RSFL);
1111 writel(FEC_ENET_RAEM_V, fep->hwp + FEC_R_FIFO_RAEM);
1112 writel(FEC_ENET_RAFL_V, fep->hwp + FEC_R_FIFO_RAFL);
1115 writel(FEC_ENET_OPD_V, fep->hwp + FEC_OPD);
1117 rcntl &= ~FEC_ENET_FCE;
1119 #endif /* !defined(CONFIG_M5272) */
1121 writel(rcntl, fep->hwp + FEC_R_CNTRL);
1123 /* Setup multicast filter. */
1124 set_multicast_list(ndev);
1125 #ifndef CONFIG_M5272
1126 writel(0, fep->hwp + FEC_HASH_TABLE_HIGH);
1127 writel(0, fep->hwp + FEC_HASH_TABLE_LOW);
1130 if (fep->quirks & FEC_QUIRK_ENET_MAC) {
1131 /* enable ENET endian swap */
1133 /* enable ENET store and forward mode */
1134 writel(1 << 8, fep->hwp + FEC_X_WMRK);
1137 if (fep->bufdesc_ex)
1140 if (fep->quirks & FEC_QUIRK_DELAYED_CLKS_SUPPORT &&
1142 ecntl |= FEC_ENET_TXC_DLY;
1143 if (fep->quirks & FEC_QUIRK_DELAYED_CLKS_SUPPORT &&
1145 ecntl |= FEC_ENET_RXC_DLY;
1147 #ifndef CONFIG_M5272
1148 /* Enable the MIB statistic event counters */
1149 writel(0 << 31, fep->hwp + FEC_MIB_CTRLSTAT);
1152 /* And last, enable the transmit and receive processing */
1153 writel(ecntl, fep->hwp + FEC_ECNTRL);
1154 fec_enet_active_rxring(ndev);
1156 if (fep->bufdesc_ex)
1157 fec_ptp_start_cyclecounter(ndev);
1159 /* Enable interrupts we wish to service */
1161 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1163 writel(0, fep->hwp + FEC_IMASK);
1165 /* Init the interrupt coalescing */
1166 fec_enet_itr_coal_init(ndev);
1170 static void fec_enet_stop_mode(struct fec_enet_private *fep, bool enabled)
1172 struct fec_platform_data *pdata = fep->pdev->dev.platform_data;
1173 struct fec_stop_mode_gpr *stop_gpr = &fep->stop_gpr;
1175 if (stop_gpr->gpr) {
1177 regmap_update_bits(stop_gpr->gpr, stop_gpr->reg,
1179 BIT(stop_gpr->bit));
1181 regmap_update_bits(stop_gpr->gpr, stop_gpr->reg,
1182 BIT(stop_gpr->bit), 0);
1183 } else if (pdata && pdata->sleep_mode_enable) {
1184 pdata->sleep_mode_enable(enabled);
1189 fec_stop(struct net_device *ndev)
1191 struct fec_enet_private *fep = netdev_priv(ndev);
1192 u32 rmii_mode = readl(fep->hwp + FEC_R_CNTRL) & (1 << 8);
1195 /* We cannot expect a graceful transmit stop without link !!! */
1197 writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */
1199 if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA))
1200 netdev_err(ndev, "Graceful transmit stop did not complete!\n");
1203 /* Whack a reset. We should wait for this.
1204 * For i.MX6SX SOC, enet use AXI bus, we use disable MAC
1205 * instead of reset MAC itself.
1207 if (!(fep->wol_flag & FEC_WOL_FLAG_SLEEP_ON)) {
1208 if (fep->quirks & FEC_QUIRK_HAS_MULTI_QUEUES) {
1209 writel(0, fep->hwp + FEC_ECNTRL);
1211 writel(1, fep->hwp + FEC_ECNTRL);
1214 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1216 writel(FEC_DEFAULT_IMASK | FEC_ENET_WAKEUP, fep->hwp + FEC_IMASK);
1217 val = readl(fep->hwp + FEC_ECNTRL);
1218 val |= (FEC_ECR_MAGICEN | FEC_ECR_SLEEP);
1219 writel(val, fep->hwp + FEC_ECNTRL);
1220 fec_enet_stop_mode(fep, true);
1222 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
1224 /* We have to keep ENET enabled to have MII interrupt stay working */
1225 if (fep->quirks & FEC_QUIRK_ENET_MAC &&
1226 !(fep->wol_flag & FEC_WOL_FLAG_SLEEP_ON)) {
1227 writel(2, fep->hwp + FEC_ECNTRL);
1228 writel(rmii_mode, fep->hwp + FEC_R_CNTRL);
1234 fec_timeout(struct net_device *ndev, unsigned int txqueue)
1236 struct fec_enet_private *fep = netdev_priv(ndev);
1240 ndev->stats.tx_errors++;
1242 schedule_work(&fep->tx_timeout_work);
1245 static void fec_enet_timeout_work(struct work_struct *work)
1247 struct fec_enet_private *fep =
1248 container_of(work, struct fec_enet_private, tx_timeout_work);
1249 struct net_device *ndev = fep->netdev;
1252 if (netif_device_present(ndev) || netif_running(ndev)) {
1253 napi_disable(&fep->napi);
1254 netif_tx_lock_bh(ndev);
1256 netif_tx_wake_all_queues(ndev);
1257 netif_tx_unlock_bh(ndev);
1258 napi_enable(&fep->napi);
1264 fec_enet_hwtstamp(struct fec_enet_private *fep, unsigned ts,
1265 struct skb_shared_hwtstamps *hwtstamps)
1267 unsigned long flags;
1270 spin_lock_irqsave(&fep->tmreg_lock, flags);
1271 ns = timecounter_cyc2time(&fep->tc, ts);
1272 spin_unlock_irqrestore(&fep->tmreg_lock, flags);
1274 memset(hwtstamps, 0, sizeof(*hwtstamps));
1275 hwtstamps->hwtstamp = ns_to_ktime(ns);
1279 fec_enet_tx_queue(struct net_device *ndev, u16 queue_id)
1281 struct fec_enet_private *fep;
1282 struct bufdesc *bdp;
1283 unsigned short status;
1284 struct sk_buff *skb;
1285 struct fec_enet_priv_tx_q *txq;
1286 struct netdev_queue *nq;
1290 fep = netdev_priv(ndev);
1292 txq = fep->tx_queue[queue_id];
1293 /* get next bdp of dirty_tx */
1294 nq = netdev_get_tx_queue(ndev, queue_id);
1295 bdp = txq->dirty_tx;
1297 /* get next bdp of dirty_tx */
1298 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
1300 while (bdp != READ_ONCE(txq->bd.cur)) {
1301 /* Order the load of bd.cur and cbd_sc */
1303 status = fec16_to_cpu(READ_ONCE(bdp->cbd_sc));
1304 if (status & BD_ENET_TX_READY)
1307 index = fec_enet_get_bd_index(bdp, &txq->bd);
1309 skb = txq->tx_skbuff[index];
1310 txq->tx_skbuff[index] = NULL;
1311 if (!IS_TSO_HEADER(txq, fec32_to_cpu(bdp->cbd_bufaddr)))
1312 dma_unmap_single(&fep->pdev->dev,
1313 fec32_to_cpu(bdp->cbd_bufaddr),
1314 fec16_to_cpu(bdp->cbd_datlen),
1316 bdp->cbd_bufaddr = cpu_to_fec32(0);
1320 /* Check for errors. */
1321 if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC |
1322 BD_ENET_TX_RL | BD_ENET_TX_UN |
1324 ndev->stats.tx_errors++;
1325 if (status & BD_ENET_TX_HB) /* No heartbeat */
1326 ndev->stats.tx_heartbeat_errors++;
1327 if (status & BD_ENET_TX_LC) /* Late collision */
1328 ndev->stats.tx_window_errors++;
1329 if (status & BD_ENET_TX_RL) /* Retrans limit */
1330 ndev->stats.tx_aborted_errors++;
1331 if (status & BD_ENET_TX_UN) /* Underrun */
1332 ndev->stats.tx_fifo_errors++;
1333 if (status & BD_ENET_TX_CSL) /* Carrier lost */
1334 ndev->stats.tx_carrier_errors++;
1336 ndev->stats.tx_packets++;
1337 ndev->stats.tx_bytes += skb->len;
1340 /* NOTE: SKBTX_IN_PROGRESS being set does not imply it's we who
1341 * are to time stamp the packet, so we still need to check time
1342 * stamping enabled flag.
1344 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS &&
1347 struct skb_shared_hwtstamps shhwtstamps;
1348 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
1350 fec_enet_hwtstamp(fep, fec32_to_cpu(ebdp->ts), &shhwtstamps);
1351 skb_tstamp_tx(skb, &shhwtstamps);
1354 /* Deferred means some collisions occurred during transmit,
1355 * but we eventually sent the packet OK.
1357 if (status & BD_ENET_TX_DEF)
1358 ndev->stats.collisions++;
1360 /* Free the sk buffer associated with this last transmit */
1361 dev_kfree_skb_any(skb);
1363 /* Make sure the update to bdp and tx_skbuff are performed
1367 txq->dirty_tx = bdp;
1369 /* Update pointer to next buffer descriptor to be transmitted */
1370 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
1372 /* Since we have freed up a buffer, the ring is no longer full
1374 if (netif_tx_queue_stopped(nq)) {
1375 entries_free = fec_enet_get_free_txdesc_num(txq);
1376 if (entries_free >= txq->tx_wake_threshold)
1377 netif_tx_wake_queue(nq);
1381 /* ERR006358: Keep the transmitter going */
1382 if (bdp != txq->bd.cur &&
1383 readl(txq->bd.reg_desc_active) == 0)
1384 writel(0, txq->bd.reg_desc_active);
1387 static void fec_enet_tx(struct net_device *ndev)
1389 struct fec_enet_private *fep = netdev_priv(ndev);
1392 /* Make sure that AVB queues are processed first. */
1393 for (i = fep->num_tx_queues - 1; i >= 0; i--)
1394 fec_enet_tx_queue(ndev, i);
1398 fec_enet_new_rxbdp(struct net_device *ndev, struct bufdesc *bdp, struct sk_buff *skb)
1400 struct fec_enet_private *fep = netdev_priv(ndev);
1403 off = ((unsigned long)skb->data) & fep->rx_align;
1405 skb_reserve(skb, fep->rx_align + 1 - off);
1407 bdp->cbd_bufaddr = cpu_to_fec32(dma_map_single(&fep->pdev->dev, skb->data, FEC_ENET_RX_FRSIZE - fep->rx_align, DMA_FROM_DEVICE));
1408 if (dma_mapping_error(&fep->pdev->dev, fec32_to_cpu(bdp->cbd_bufaddr))) {
1409 if (net_ratelimit())
1410 netdev_err(ndev, "Rx DMA memory map failed\n");
1417 static bool fec_enet_copybreak(struct net_device *ndev, struct sk_buff **skb,
1418 struct bufdesc *bdp, u32 length, bool swap)
1420 struct fec_enet_private *fep = netdev_priv(ndev);
1421 struct sk_buff *new_skb;
1423 if (length > fep->rx_copybreak)
1426 new_skb = netdev_alloc_skb(ndev, length);
1430 dma_sync_single_for_cpu(&fep->pdev->dev,
1431 fec32_to_cpu(bdp->cbd_bufaddr),
1432 FEC_ENET_RX_FRSIZE - fep->rx_align,
1435 memcpy(new_skb->data, (*skb)->data, length);
1437 swap_buffer2(new_skb->data, (*skb)->data, length);
1443 /* During a receive, the bd_rx.cur points to the current incoming buffer.
1444 * When we update through the ring, if the next incoming buffer has
1445 * not been given to the system, we just set the empty indicator,
1446 * effectively tossing the packet.
1449 fec_enet_rx_queue(struct net_device *ndev, int budget, u16 queue_id)
1451 struct fec_enet_private *fep = netdev_priv(ndev);
1452 struct fec_enet_priv_rx_q *rxq;
1453 struct bufdesc *bdp;
1454 unsigned short status;
1455 struct sk_buff *skb_new = NULL;
1456 struct sk_buff *skb;
1459 int pkt_received = 0;
1460 struct bufdesc_ex *ebdp = NULL;
1461 bool vlan_packet_rcvd = false;
1465 bool need_swap = fep->quirks & FEC_QUIRK_SWAP_FRAME;
1470 rxq = fep->rx_queue[queue_id];
1472 /* First, grab all of the stats for the incoming packet.
1473 * These get messed up if we get called due to a busy condition.
1477 while (!((status = fec16_to_cpu(bdp->cbd_sc)) & BD_ENET_RX_EMPTY)) {
1479 if (pkt_received >= budget)
1483 writel(FEC_ENET_RXF_GET(queue_id), fep->hwp + FEC_IEVENT);
1485 /* Check for errors. */
1486 status ^= BD_ENET_RX_LAST;
1487 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
1488 BD_ENET_RX_CR | BD_ENET_RX_OV | BD_ENET_RX_LAST |
1490 ndev->stats.rx_errors++;
1491 if (status & BD_ENET_RX_OV) {
1493 ndev->stats.rx_fifo_errors++;
1494 goto rx_processing_done;
1496 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH
1497 | BD_ENET_RX_LAST)) {
1498 /* Frame too long or too short. */
1499 ndev->stats.rx_length_errors++;
1500 if (status & BD_ENET_RX_LAST)
1501 netdev_err(ndev, "rcv is not +last\n");
1503 if (status & BD_ENET_RX_CR) /* CRC Error */
1504 ndev->stats.rx_crc_errors++;
1505 /* Report late collisions as a frame error. */
1506 if (status & (BD_ENET_RX_NO | BD_ENET_RX_CL))
1507 ndev->stats.rx_frame_errors++;
1508 goto rx_processing_done;
1511 /* Process the incoming frame. */
1512 ndev->stats.rx_packets++;
1513 pkt_len = fec16_to_cpu(bdp->cbd_datlen);
1514 ndev->stats.rx_bytes += pkt_len;
1516 index = fec_enet_get_bd_index(bdp, &rxq->bd);
1517 skb = rxq->rx_skbuff[index];
1519 /* The packet length includes FCS, but we don't want to
1520 * include that when passing upstream as it messes up
1521 * bridging applications.
1523 is_copybreak = fec_enet_copybreak(ndev, &skb, bdp, pkt_len - 4,
1525 if (!is_copybreak) {
1526 skb_new = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
1527 if (unlikely(!skb_new)) {
1528 ndev->stats.rx_dropped++;
1529 goto rx_processing_done;
1531 dma_unmap_single(&fep->pdev->dev,
1532 fec32_to_cpu(bdp->cbd_bufaddr),
1533 FEC_ENET_RX_FRSIZE - fep->rx_align,
1537 prefetch(skb->data - NET_IP_ALIGN);
1538 skb_put(skb, pkt_len - 4);
1541 if (!is_copybreak && need_swap)
1542 swap_buffer(data, pkt_len);
1544 #if !defined(CONFIG_M5272)
1545 if (fep->quirks & FEC_QUIRK_HAS_RACC)
1546 data = skb_pull_inline(skb, 2);
1549 /* Extract the enhanced buffer descriptor */
1551 if (fep->bufdesc_ex)
1552 ebdp = (struct bufdesc_ex *)bdp;
1554 /* If this is a VLAN packet remove the VLAN Tag */
1555 vlan_packet_rcvd = false;
1556 if ((ndev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
1558 (ebdp->cbd_esc & cpu_to_fec32(BD_ENET_RX_VLAN))) {
1559 /* Push and remove the vlan tag */
1560 struct vlan_hdr *vlan_header =
1561 (struct vlan_hdr *) (data + ETH_HLEN);
1562 vlan_tag = ntohs(vlan_header->h_vlan_TCI);
1564 vlan_packet_rcvd = true;
1566 memmove(skb->data + VLAN_HLEN, data, ETH_ALEN * 2);
1567 skb_pull(skb, VLAN_HLEN);
1570 skb->protocol = eth_type_trans(skb, ndev);
1572 /* Get receive timestamp from the skb */
1573 if (fep->hwts_rx_en && fep->bufdesc_ex)
1574 fec_enet_hwtstamp(fep, fec32_to_cpu(ebdp->ts),
1575 skb_hwtstamps(skb));
1577 if (fep->bufdesc_ex &&
1578 (fep->csum_flags & FLAG_RX_CSUM_ENABLED)) {
1579 if (!(ebdp->cbd_esc & cpu_to_fec32(FLAG_RX_CSUM_ERROR))) {
1580 /* don't check it */
1581 skb->ip_summed = CHECKSUM_UNNECESSARY;
1583 skb_checksum_none_assert(skb);
1587 /* Handle received VLAN packets */
1588 if (vlan_packet_rcvd)
1589 __vlan_hwaccel_put_tag(skb,
1593 skb_record_rx_queue(skb, queue_id);
1594 napi_gro_receive(&fep->napi, skb);
1597 dma_sync_single_for_device(&fep->pdev->dev,
1598 fec32_to_cpu(bdp->cbd_bufaddr),
1599 FEC_ENET_RX_FRSIZE - fep->rx_align,
1602 rxq->rx_skbuff[index] = skb_new;
1603 fec_enet_new_rxbdp(ndev, bdp, skb_new);
1607 /* Clear the status flags for this buffer */
1608 status &= ~BD_ENET_RX_STATS;
1610 /* Mark the buffer empty */
1611 status |= BD_ENET_RX_EMPTY;
1613 if (fep->bufdesc_ex) {
1614 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
1616 ebdp->cbd_esc = cpu_to_fec32(BD_ENET_RX_INT);
1620 /* Make sure the updates to rest of the descriptor are
1621 * performed before transferring ownership.
1624 bdp->cbd_sc = cpu_to_fec16(status);
1626 /* Update BD pointer to next entry */
1627 bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
1629 /* Doing this here will keep the FEC running while we process
1630 * incoming frames. On a heavily loaded network, we should be
1631 * able to keep up at the expense of system resources.
1633 writel(0, rxq->bd.reg_desc_active);
1636 return pkt_received;
1639 static int fec_enet_rx(struct net_device *ndev, int budget)
1641 struct fec_enet_private *fep = netdev_priv(ndev);
1644 /* Make sure that AVB queues are processed first. */
1645 for (i = fep->num_rx_queues - 1; i >= 0; i--)
1646 done += fec_enet_rx_queue(ndev, budget - done, i);
1651 static bool fec_enet_collect_events(struct fec_enet_private *fep)
1655 int_events = readl(fep->hwp + FEC_IEVENT);
1657 /* Don't clear MDIO events, we poll for those */
1658 int_events &= ~FEC_ENET_MII;
1660 writel(int_events, fep->hwp + FEC_IEVENT);
1662 return int_events != 0;
1666 fec_enet_interrupt(int irq, void *dev_id)
1668 struct net_device *ndev = dev_id;
1669 struct fec_enet_private *fep = netdev_priv(ndev);
1670 irqreturn_t ret = IRQ_NONE;
1672 if (fec_enet_collect_events(fep) && fep->link) {
1675 if (napi_schedule_prep(&fep->napi)) {
1676 /* Disable interrupts */
1677 writel(0, fep->hwp + FEC_IMASK);
1678 __napi_schedule(&fep->napi);
1685 static int fec_enet_rx_napi(struct napi_struct *napi, int budget)
1687 struct net_device *ndev = napi->dev;
1688 struct fec_enet_private *fep = netdev_priv(ndev);
1692 done += fec_enet_rx(ndev, budget - done);
1694 } while ((done < budget) && fec_enet_collect_events(fep));
1696 if (done < budget) {
1697 napi_complete_done(napi, done);
1698 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1704 /* ------------------------------------------------------------------------- */
1705 static int fec_get_mac(struct net_device *ndev)
1707 struct fec_enet_private *fep = netdev_priv(ndev);
1708 unsigned char *iap, tmpaddr[ETH_ALEN];
1712 * try to get mac address in following order:
1714 * 1) module parameter via kernel command line in form
1715 * fec.macaddr=0x00,0x04,0x9f,0x01,0x30,0xe0
1720 * 2) from device tree data
1722 if (!is_valid_ether_addr(iap)) {
1723 struct device_node *np = fep->pdev->dev.of_node;
1725 ret = of_get_mac_address(np, tmpaddr);
1728 else if (ret == -EPROBE_DEFER)
1734 * 3) from flash or fuse (via platform data)
1736 if (!is_valid_ether_addr(iap)) {
1739 iap = (unsigned char *)FEC_FLASHMAC;
1741 struct fec_platform_data *pdata = dev_get_platdata(&fep->pdev->dev);
1744 iap = (unsigned char *)&pdata->mac;
1749 * 4) FEC mac registers set by bootloader
1751 if (!is_valid_ether_addr(iap)) {
1752 *((__be32 *) &tmpaddr[0]) =
1753 cpu_to_be32(readl(fep->hwp + FEC_ADDR_LOW));
1754 *((__be16 *) &tmpaddr[4]) =
1755 cpu_to_be16(readl(fep->hwp + FEC_ADDR_HIGH) >> 16);
1760 * 5) random mac address
1762 if (!is_valid_ether_addr(iap)) {
1763 /* Report it and use a random ethernet address instead */
1764 dev_err(&fep->pdev->dev, "Invalid MAC address: %pM\n", iap);
1765 eth_hw_addr_random(ndev);
1766 dev_info(&fep->pdev->dev, "Using random MAC address: %pM\n",
1771 memcpy(ndev->dev_addr, iap, ETH_ALEN);
1773 /* Adjust MAC if using macaddr */
1775 ndev->dev_addr[ETH_ALEN-1] = macaddr[ETH_ALEN-1] + fep->dev_id;
1780 /* ------------------------------------------------------------------------- */
1785 static void fec_enet_adjust_link(struct net_device *ndev)
1787 struct fec_enet_private *fep = netdev_priv(ndev);
1788 struct phy_device *phy_dev = ndev->phydev;
1789 int status_change = 0;
1792 * If the netdev is down, or is going down, we're not interested
1793 * in link state events, so just mark our idea of the link as down
1794 * and ignore the event.
1796 if (!netif_running(ndev) || !netif_device_present(ndev)) {
1798 } else if (phy_dev->link) {
1800 fep->link = phy_dev->link;
1804 if (fep->full_duplex != phy_dev->duplex) {
1805 fep->full_duplex = phy_dev->duplex;
1809 if (phy_dev->speed != fep->speed) {
1810 fep->speed = phy_dev->speed;
1814 /* if any of the above changed restart the FEC */
1815 if (status_change) {
1816 napi_disable(&fep->napi);
1817 netif_tx_lock_bh(ndev);
1819 netif_tx_wake_all_queues(ndev);
1820 netif_tx_unlock_bh(ndev);
1821 napi_enable(&fep->napi);
1825 napi_disable(&fep->napi);
1826 netif_tx_lock_bh(ndev);
1828 netif_tx_unlock_bh(ndev);
1829 napi_enable(&fep->napi);
1830 fep->link = phy_dev->link;
1836 phy_print_status(phy_dev);
1839 static int fec_enet_mdio_wait(struct fec_enet_private *fep)
1844 ret = readl_poll_timeout_atomic(fep->hwp + FEC_IEVENT, ievent,
1845 ievent & FEC_ENET_MII, 2, 30000);
1848 writel(FEC_ENET_MII, fep->hwp + FEC_IEVENT);
1853 static int fec_enet_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
1855 struct fec_enet_private *fep = bus->priv;
1856 struct device *dev = &fep->pdev->dev;
1857 int ret = 0, frame_start, frame_addr, frame_op;
1858 bool is_c45 = !!(regnum & MII_ADDR_C45);
1860 ret = pm_runtime_resume_and_get(dev);
1865 frame_start = FEC_MMFR_ST_C45;
1868 frame_addr = (regnum >> 16);
1869 writel(frame_start | FEC_MMFR_OP_ADDR_WRITE |
1870 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(frame_addr) |
1871 FEC_MMFR_TA | (regnum & 0xFFFF),
1872 fep->hwp + FEC_MII_DATA);
1874 /* wait for end of transfer */
1875 ret = fec_enet_mdio_wait(fep);
1877 netdev_err(fep->netdev, "MDIO address write timeout\n");
1881 frame_op = FEC_MMFR_OP_READ_C45;
1885 frame_op = FEC_MMFR_OP_READ;
1886 frame_start = FEC_MMFR_ST;
1887 frame_addr = regnum;
1890 /* start a read op */
1891 writel(frame_start | frame_op |
1892 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(frame_addr) |
1893 FEC_MMFR_TA, fep->hwp + FEC_MII_DATA);
1895 /* wait for end of transfer */
1896 ret = fec_enet_mdio_wait(fep);
1898 netdev_err(fep->netdev, "MDIO read timeout\n");
1902 ret = FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA));
1905 pm_runtime_mark_last_busy(dev);
1906 pm_runtime_put_autosuspend(dev);
1911 static int fec_enet_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
1914 struct fec_enet_private *fep = bus->priv;
1915 struct device *dev = &fep->pdev->dev;
1916 int ret, frame_start, frame_addr;
1917 bool is_c45 = !!(regnum & MII_ADDR_C45);
1919 ret = pm_runtime_resume_and_get(dev);
1924 frame_start = FEC_MMFR_ST_C45;
1927 frame_addr = (regnum >> 16);
1928 writel(frame_start | FEC_MMFR_OP_ADDR_WRITE |
1929 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(frame_addr) |
1930 FEC_MMFR_TA | (regnum & 0xFFFF),
1931 fep->hwp + FEC_MII_DATA);
1933 /* wait for end of transfer */
1934 ret = fec_enet_mdio_wait(fep);
1936 netdev_err(fep->netdev, "MDIO address write timeout\n");
1941 frame_start = FEC_MMFR_ST;
1942 frame_addr = regnum;
1945 /* start a write op */
1946 writel(frame_start | FEC_MMFR_OP_WRITE |
1947 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(frame_addr) |
1948 FEC_MMFR_TA | FEC_MMFR_DATA(value),
1949 fep->hwp + FEC_MII_DATA);
1951 /* wait for end of transfer */
1952 ret = fec_enet_mdio_wait(fep);
1954 netdev_err(fep->netdev, "MDIO write timeout\n");
1957 pm_runtime_mark_last_busy(dev);
1958 pm_runtime_put_autosuspend(dev);
1963 static void fec_enet_phy_reset_after_clk_enable(struct net_device *ndev)
1965 struct fec_enet_private *fep = netdev_priv(ndev);
1966 struct phy_device *phy_dev = ndev->phydev;
1969 phy_reset_after_clk_enable(phy_dev);
1970 } else if (fep->phy_node) {
1972 * If the PHY still is not bound to the MAC, but there is
1973 * OF PHY node and a matching PHY device instance already,
1974 * use the OF PHY node to obtain the PHY device instance,
1975 * and then use that PHY device instance when triggering
1978 phy_dev = of_phy_find_device(fep->phy_node);
1979 phy_reset_after_clk_enable(phy_dev);
1980 put_device(&phy_dev->mdio.dev);
1984 static int fec_enet_clk_enable(struct net_device *ndev, bool enable)
1986 struct fec_enet_private *fep = netdev_priv(ndev);
1990 ret = clk_prepare_enable(fep->clk_enet_out);
1995 mutex_lock(&fep->ptp_clk_mutex);
1996 ret = clk_prepare_enable(fep->clk_ptp);
1998 mutex_unlock(&fep->ptp_clk_mutex);
1999 goto failed_clk_ptp;
2001 fep->ptp_clk_on = true;
2003 mutex_unlock(&fep->ptp_clk_mutex);
2006 ret = clk_prepare_enable(fep->clk_ref);
2008 goto failed_clk_ref;
2010 ret = clk_prepare_enable(fep->clk_2x_txclk);
2012 goto failed_clk_2x_txclk;
2014 fec_enet_phy_reset_after_clk_enable(ndev);
2016 clk_disable_unprepare(fep->clk_enet_out);
2018 mutex_lock(&fep->ptp_clk_mutex);
2019 clk_disable_unprepare(fep->clk_ptp);
2020 fep->ptp_clk_on = false;
2021 mutex_unlock(&fep->ptp_clk_mutex);
2023 clk_disable_unprepare(fep->clk_ref);
2024 clk_disable_unprepare(fep->clk_2x_txclk);
2029 failed_clk_2x_txclk:
2031 clk_disable_unprepare(fep->clk_ref);
2034 mutex_lock(&fep->ptp_clk_mutex);
2035 clk_disable_unprepare(fep->clk_ptp);
2036 fep->ptp_clk_on = false;
2037 mutex_unlock(&fep->ptp_clk_mutex);
2040 clk_disable_unprepare(fep->clk_enet_out);
2045 static int fec_enet_parse_rgmii_delay(struct fec_enet_private *fep,
2046 struct device_node *np)
2048 u32 rgmii_tx_delay, rgmii_rx_delay;
2050 /* For rgmii tx internal delay, valid values are 0ps and 2000ps */
2051 if (!of_property_read_u32(np, "tx-internal-delay-ps", &rgmii_tx_delay)) {
2052 if (rgmii_tx_delay != 0 && rgmii_tx_delay != 2000) {
2053 dev_err(&fep->pdev->dev, "The only allowed RGMII TX delay values are: 0ps, 2000ps");
2055 } else if (rgmii_tx_delay == 2000) {
2056 fep->rgmii_txc_dly = true;
2060 /* For rgmii rx internal delay, valid values are 0ps and 2000ps */
2061 if (!of_property_read_u32(np, "rx-internal-delay-ps", &rgmii_rx_delay)) {
2062 if (rgmii_rx_delay != 0 && rgmii_rx_delay != 2000) {
2063 dev_err(&fep->pdev->dev, "The only allowed RGMII RX delay values are: 0ps, 2000ps");
2065 } else if (rgmii_rx_delay == 2000) {
2066 fep->rgmii_rxc_dly = true;
2073 static int fec_enet_mii_probe(struct net_device *ndev)
2075 struct fec_enet_private *fep = netdev_priv(ndev);
2076 struct phy_device *phy_dev = NULL;
2077 char mdio_bus_id[MII_BUS_ID_SIZE];
2078 char phy_name[MII_BUS_ID_SIZE + 3];
2080 int dev_id = fep->dev_id;
2082 if (fep->phy_node) {
2083 phy_dev = of_phy_connect(ndev, fep->phy_node,
2084 &fec_enet_adjust_link, 0,
2085 fep->phy_interface);
2087 netdev_err(ndev, "Unable to connect to phy\n");
2091 /* check for attached phy */
2092 for (phy_id = 0; (phy_id < PHY_MAX_ADDR); phy_id++) {
2093 if (!mdiobus_is_registered_device(fep->mii_bus, phy_id))
2097 strlcpy(mdio_bus_id, fep->mii_bus->id, MII_BUS_ID_SIZE);
2101 if (phy_id >= PHY_MAX_ADDR) {
2102 netdev_info(ndev, "no PHY, assuming direct connection to switch\n");
2103 strlcpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE);
2107 snprintf(phy_name, sizeof(phy_name),
2108 PHY_ID_FMT, mdio_bus_id, phy_id);
2109 phy_dev = phy_connect(ndev, phy_name, &fec_enet_adjust_link,
2110 fep->phy_interface);
2113 if (IS_ERR(phy_dev)) {
2114 netdev_err(ndev, "could not attach to PHY\n");
2115 return PTR_ERR(phy_dev);
2118 /* mask with MAC supported features */
2119 if (fep->quirks & FEC_QUIRK_HAS_GBIT) {
2120 phy_set_max_speed(phy_dev, 1000);
2121 phy_remove_link_mode(phy_dev,
2122 ETHTOOL_LINK_MODE_1000baseT_Half_BIT);
2123 #if !defined(CONFIG_M5272)
2124 phy_support_sym_pause(phy_dev);
2128 phy_set_max_speed(phy_dev, 100);
2131 fep->full_duplex = 0;
2133 phy_dev->mac_managed_pm = 1;
2135 phy_attached_info(phy_dev);
2140 static int fec_enet_mii_init(struct platform_device *pdev)
2142 static struct mii_bus *fec0_mii_bus;
2143 struct net_device *ndev = platform_get_drvdata(pdev);
2144 struct fec_enet_private *fep = netdev_priv(ndev);
2145 bool suppress_preamble = false;
2146 struct device_node *node;
2148 u32 mii_speed, holdtime;
2152 * The i.MX28 dual fec interfaces are not equal.
2153 * Here are the differences:
2155 * - fec0 supports MII & RMII modes while fec1 only supports RMII
2156 * - fec0 acts as the 1588 time master while fec1 is slave
2157 * - external phys can only be configured by fec0
2159 * That is to say fec1 can not work independently. It only works
2160 * when fec0 is working. The reason behind this design is that the
2161 * second interface is added primarily for Switch mode.
2163 * Because of the last point above, both phys are attached on fec0
2164 * mdio interface in board design, and need to be configured by
2167 if ((fep->quirks & FEC_QUIRK_SINGLE_MDIO) && fep->dev_id > 0) {
2168 /* fec1 uses fec0 mii_bus */
2169 if (mii_cnt && fec0_mii_bus) {
2170 fep->mii_bus = fec0_mii_bus;
2177 bus_freq = 2500000; /* 2.5MHz by default */
2178 node = of_get_child_by_name(pdev->dev.of_node, "mdio");
2180 of_property_read_u32(node, "clock-frequency", &bus_freq);
2181 suppress_preamble = of_property_read_bool(node,
2182 "suppress-preamble");
2186 * Set MII speed (= clk_get_rate() / 2 * phy_speed)
2188 * The formula for FEC MDC is 'ref_freq / (MII_SPEED x 2)' while
2189 * for ENET-MAC is 'ref_freq / ((MII_SPEED + 1) x 2)'. The i.MX28
2190 * Reference Manual has an error on this, and gets fixed on i.MX6Q
2193 mii_speed = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), bus_freq * 2);
2194 if (fep->quirks & FEC_QUIRK_ENET_MAC)
2196 if (mii_speed > 63) {
2198 "fec clock (%lu) too fast to get right mii speed\n",
2199 clk_get_rate(fep->clk_ipg));
2205 * The i.MX28 and i.MX6 types have another filed in the MSCR (aka
2206 * MII_SPEED) register that defines the MDIO output hold time. Earlier
2207 * versions are RAZ there, so just ignore the difference and write the
2209 * The minimal hold time according to IEE802.3 (clause 22) is 10 ns.
2210 * HOLDTIME + 1 is the number of clk cycles the fec is holding the
2212 * The HOLDTIME bitfield takes values between 0 and 7 (inclusive).
2213 * Given that ceil(clkrate / 5000000) <= 64, the calculation for
2214 * holdtime cannot result in a value greater than 3.
2216 holdtime = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), 100000000) - 1;
2218 fep->phy_speed = mii_speed << 1 | holdtime << 8;
2220 if (suppress_preamble)
2221 fep->phy_speed |= BIT(7);
2223 if (fep->quirks & FEC_QUIRK_CLEAR_SETUP_MII) {
2224 /* Clear MMFR to avoid to generate MII event by writing MSCR.
2225 * MII event generation condition:
2227 * - mmfr[31:0]_not_zero & mscr[7:0]_is_zero &
2228 * mscr_reg_data_in[7:0] != 0
2230 * - mscr[7:0]_not_zero
2232 writel(0, fep->hwp + FEC_MII_DATA);
2235 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
2237 /* Clear any pending transaction complete indication */
2238 writel(FEC_ENET_MII, fep->hwp + FEC_IEVENT);
2240 fep->mii_bus = mdiobus_alloc();
2241 if (fep->mii_bus == NULL) {
2246 fep->mii_bus->name = "fec_enet_mii_bus";
2247 fep->mii_bus->read = fec_enet_mdio_read;
2248 fep->mii_bus->write = fec_enet_mdio_write;
2249 snprintf(fep->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
2250 pdev->name, fep->dev_id + 1);
2251 fep->mii_bus->priv = fep;
2252 fep->mii_bus->parent = &pdev->dev;
2254 err = of_mdiobus_register(fep->mii_bus, node);
2256 goto err_out_free_mdiobus;
2261 /* save fec0 mii_bus */
2262 if (fep->quirks & FEC_QUIRK_SINGLE_MDIO)
2263 fec0_mii_bus = fep->mii_bus;
2267 err_out_free_mdiobus:
2268 mdiobus_free(fep->mii_bus);
2274 static void fec_enet_mii_remove(struct fec_enet_private *fep)
2276 if (--mii_cnt == 0) {
2277 mdiobus_unregister(fep->mii_bus);
2278 mdiobus_free(fep->mii_bus);
2282 static void fec_enet_get_drvinfo(struct net_device *ndev,
2283 struct ethtool_drvinfo *info)
2285 struct fec_enet_private *fep = netdev_priv(ndev);
2287 strlcpy(info->driver, fep->pdev->dev.driver->name,
2288 sizeof(info->driver));
2289 strlcpy(info->bus_info, dev_name(&ndev->dev), sizeof(info->bus_info));
2292 static int fec_enet_get_regs_len(struct net_device *ndev)
2294 struct fec_enet_private *fep = netdev_priv(ndev);
2298 r = platform_get_resource(fep->pdev, IORESOURCE_MEM, 0);
2300 s = resource_size(r);
2305 /* List of registers that can be safety be read to dump them with ethtool */
2306 #if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
2307 defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM) || \
2308 defined(CONFIG_ARM64) || defined(CONFIG_COMPILE_TEST)
2309 static __u32 fec_enet_register_version = 2;
2310 static u32 fec_enet_register_offset[] = {
2311 FEC_IEVENT, FEC_IMASK, FEC_R_DES_ACTIVE_0, FEC_X_DES_ACTIVE_0,
2312 FEC_ECNTRL, FEC_MII_DATA, FEC_MII_SPEED, FEC_MIB_CTRLSTAT, FEC_R_CNTRL,
2313 FEC_X_CNTRL, FEC_ADDR_LOW, FEC_ADDR_HIGH, FEC_OPD, FEC_TXIC0, FEC_TXIC1,
2314 FEC_TXIC2, FEC_RXIC0, FEC_RXIC1, FEC_RXIC2, FEC_HASH_TABLE_HIGH,
2315 FEC_HASH_TABLE_LOW, FEC_GRP_HASH_TABLE_HIGH, FEC_GRP_HASH_TABLE_LOW,
2316 FEC_X_WMRK, FEC_R_BOUND, FEC_R_FSTART, FEC_R_DES_START_1,
2317 FEC_X_DES_START_1, FEC_R_BUFF_SIZE_1, FEC_R_DES_START_2,
2318 FEC_X_DES_START_2, FEC_R_BUFF_SIZE_2, FEC_R_DES_START_0,
2319 FEC_X_DES_START_0, FEC_R_BUFF_SIZE_0, FEC_R_FIFO_RSFL, FEC_R_FIFO_RSEM,
2320 FEC_R_FIFO_RAEM, FEC_R_FIFO_RAFL, FEC_RACC, FEC_RCMR_1, FEC_RCMR_2,
2321 FEC_DMA_CFG_1, FEC_DMA_CFG_2, FEC_R_DES_ACTIVE_1, FEC_X_DES_ACTIVE_1,
2322 FEC_R_DES_ACTIVE_2, FEC_X_DES_ACTIVE_2, FEC_QOS_SCHEME,
2323 RMON_T_DROP, RMON_T_PACKETS, RMON_T_BC_PKT, RMON_T_MC_PKT,
2324 RMON_T_CRC_ALIGN, RMON_T_UNDERSIZE, RMON_T_OVERSIZE, RMON_T_FRAG,
2325 RMON_T_JAB, RMON_T_COL, RMON_T_P64, RMON_T_P65TO127, RMON_T_P128TO255,
2326 RMON_T_P256TO511, RMON_T_P512TO1023, RMON_T_P1024TO2047,
2327 RMON_T_P_GTE2048, RMON_T_OCTETS,
2328 IEEE_T_DROP, IEEE_T_FRAME_OK, IEEE_T_1COL, IEEE_T_MCOL, IEEE_T_DEF,
2329 IEEE_T_LCOL, IEEE_T_EXCOL, IEEE_T_MACERR, IEEE_T_CSERR, IEEE_T_SQE,
2330 IEEE_T_FDXFC, IEEE_T_OCTETS_OK,
2331 RMON_R_PACKETS, RMON_R_BC_PKT, RMON_R_MC_PKT, RMON_R_CRC_ALIGN,
2332 RMON_R_UNDERSIZE, RMON_R_OVERSIZE, RMON_R_FRAG, RMON_R_JAB,
2333 RMON_R_RESVD_O, RMON_R_P64, RMON_R_P65TO127, RMON_R_P128TO255,
2334 RMON_R_P256TO511, RMON_R_P512TO1023, RMON_R_P1024TO2047,
2335 RMON_R_P_GTE2048, RMON_R_OCTETS,
2336 IEEE_R_DROP, IEEE_R_FRAME_OK, IEEE_R_CRC, IEEE_R_ALIGN, IEEE_R_MACERR,
2337 IEEE_R_FDXFC, IEEE_R_OCTETS_OK
2340 static u32 fec_enet_register_offset_6ul[] = {
2341 FEC_IEVENT, FEC_IMASK, FEC_R_DES_ACTIVE_0, FEC_X_DES_ACTIVE_0,
2342 FEC_ECNTRL, FEC_MII_DATA, FEC_MII_SPEED, FEC_MIB_CTRLSTAT, FEC_R_CNTRL,
2343 FEC_X_CNTRL, FEC_ADDR_LOW, FEC_ADDR_HIGH, FEC_OPD, FEC_TXIC0, FEC_RXIC0,
2344 FEC_HASH_TABLE_HIGH, FEC_HASH_TABLE_LOW, FEC_GRP_HASH_TABLE_HIGH,
2345 FEC_GRP_HASH_TABLE_LOW, FEC_X_WMRK, FEC_R_DES_START_0,
2346 FEC_X_DES_START_0, FEC_R_BUFF_SIZE_0, FEC_R_FIFO_RSFL, FEC_R_FIFO_RSEM,
2347 FEC_R_FIFO_RAEM, FEC_R_FIFO_RAFL, FEC_RACC,
2348 RMON_T_DROP, RMON_T_PACKETS, RMON_T_BC_PKT, RMON_T_MC_PKT,
2349 RMON_T_CRC_ALIGN, RMON_T_UNDERSIZE, RMON_T_OVERSIZE, RMON_T_FRAG,
2350 RMON_T_JAB, RMON_T_COL, RMON_T_P64, RMON_T_P65TO127, RMON_T_P128TO255,
2351 RMON_T_P256TO511, RMON_T_P512TO1023, RMON_T_P1024TO2047,
2352 RMON_T_P_GTE2048, RMON_T_OCTETS,
2353 IEEE_T_DROP, IEEE_T_FRAME_OK, IEEE_T_1COL, IEEE_T_MCOL, IEEE_T_DEF,
2354 IEEE_T_LCOL, IEEE_T_EXCOL, IEEE_T_MACERR, IEEE_T_CSERR, IEEE_T_SQE,
2355 IEEE_T_FDXFC, IEEE_T_OCTETS_OK,
2356 RMON_R_PACKETS, RMON_R_BC_PKT, RMON_R_MC_PKT, RMON_R_CRC_ALIGN,
2357 RMON_R_UNDERSIZE, RMON_R_OVERSIZE, RMON_R_FRAG, RMON_R_JAB,
2358 RMON_R_RESVD_O, RMON_R_P64, RMON_R_P65TO127, RMON_R_P128TO255,
2359 RMON_R_P256TO511, RMON_R_P512TO1023, RMON_R_P1024TO2047,
2360 RMON_R_P_GTE2048, RMON_R_OCTETS,
2361 IEEE_R_DROP, IEEE_R_FRAME_OK, IEEE_R_CRC, IEEE_R_ALIGN, IEEE_R_MACERR,
2362 IEEE_R_FDXFC, IEEE_R_OCTETS_OK
2365 static __u32 fec_enet_register_version = 1;
2366 static u32 fec_enet_register_offset[] = {
2367 FEC_ECNTRL, FEC_IEVENT, FEC_IMASK, FEC_IVEC, FEC_R_DES_ACTIVE_0,
2368 FEC_R_DES_ACTIVE_1, FEC_R_DES_ACTIVE_2, FEC_X_DES_ACTIVE_0,
2369 FEC_X_DES_ACTIVE_1, FEC_X_DES_ACTIVE_2, FEC_MII_DATA, FEC_MII_SPEED,
2370 FEC_R_BOUND, FEC_R_FSTART, FEC_X_WMRK, FEC_X_FSTART, FEC_R_CNTRL,
2371 FEC_MAX_FRM_LEN, FEC_X_CNTRL, FEC_ADDR_LOW, FEC_ADDR_HIGH,
2372 FEC_GRP_HASH_TABLE_HIGH, FEC_GRP_HASH_TABLE_LOW, FEC_R_DES_START_0,
2373 FEC_R_DES_START_1, FEC_R_DES_START_2, FEC_X_DES_START_0,
2374 FEC_X_DES_START_1, FEC_X_DES_START_2, FEC_R_BUFF_SIZE_0,
2375 FEC_R_BUFF_SIZE_1, FEC_R_BUFF_SIZE_2
2379 static void fec_enet_get_regs(struct net_device *ndev,
2380 struct ethtool_regs *regs, void *regbuf)
2382 struct fec_enet_private *fep = netdev_priv(ndev);
2383 u32 __iomem *theregs = (u32 __iomem *)fep->hwp;
2384 struct device *dev = &fep->pdev->dev;
2385 u32 *buf = (u32 *)regbuf;
2388 #if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
2389 defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM) || \
2390 defined(CONFIG_ARM64) || defined(CONFIG_COMPILE_TEST)
2394 if (!of_machine_is_compatible("fsl,imx6ul")) {
2395 reg_list = fec_enet_register_offset;
2396 reg_cnt = ARRAY_SIZE(fec_enet_register_offset);
2398 reg_list = fec_enet_register_offset_6ul;
2399 reg_cnt = ARRAY_SIZE(fec_enet_register_offset_6ul);
2403 static u32 *reg_list = fec_enet_register_offset;
2404 static const u32 reg_cnt = ARRAY_SIZE(fec_enet_register_offset);
2406 ret = pm_runtime_resume_and_get(dev);
2410 regs->version = fec_enet_register_version;
2412 memset(buf, 0, regs->len);
2414 for (i = 0; i < reg_cnt; i++) {
2417 if ((off == FEC_R_BOUND || off == FEC_R_FSTART) &&
2418 !(fep->quirks & FEC_QUIRK_HAS_FRREG))
2422 buf[off] = readl(&theregs[off]);
2425 pm_runtime_mark_last_busy(dev);
2426 pm_runtime_put_autosuspend(dev);
2429 static int fec_enet_get_ts_info(struct net_device *ndev,
2430 struct ethtool_ts_info *info)
2432 struct fec_enet_private *fep = netdev_priv(ndev);
2434 if (fep->bufdesc_ex) {
2436 info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
2437 SOF_TIMESTAMPING_RX_SOFTWARE |
2438 SOF_TIMESTAMPING_SOFTWARE |
2439 SOF_TIMESTAMPING_TX_HARDWARE |
2440 SOF_TIMESTAMPING_RX_HARDWARE |
2441 SOF_TIMESTAMPING_RAW_HARDWARE;
2443 info->phc_index = ptp_clock_index(fep->ptp_clock);
2445 info->phc_index = -1;
2447 info->tx_types = (1 << HWTSTAMP_TX_OFF) |
2448 (1 << HWTSTAMP_TX_ON);
2450 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
2451 (1 << HWTSTAMP_FILTER_ALL);
2454 return ethtool_op_get_ts_info(ndev, info);
2458 #if !defined(CONFIG_M5272)
2460 static void fec_enet_get_pauseparam(struct net_device *ndev,
2461 struct ethtool_pauseparam *pause)
2463 struct fec_enet_private *fep = netdev_priv(ndev);
2465 pause->autoneg = (fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) != 0;
2466 pause->tx_pause = (fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) != 0;
2467 pause->rx_pause = pause->tx_pause;
2470 static int fec_enet_set_pauseparam(struct net_device *ndev,
2471 struct ethtool_pauseparam *pause)
2473 struct fec_enet_private *fep = netdev_priv(ndev);
2478 if (pause->tx_pause != pause->rx_pause) {
2480 "hardware only support enable/disable both tx and rx");
2484 fep->pause_flag = 0;
2486 /* tx pause must be same as rx pause */
2487 fep->pause_flag |= pause->rx_pause ? FEC_PAUSE_FLAG_ENABLE : 0;
2488 fep->pause_flag |= pause->autoneg ? FEC_PAUSE_FLAG_AUTONEG : 0;
2490 phy_set_sym_pause(ndev->phydev, pause->rx_pause, pause->tx_pause,
2493 if (pause->autoneg) {
2494 if (netif_running(ndev))
2496 phy_start_aneg(ndev->phydev);
2498 if (netif_running(ndev)) {
2499 napi_disable(&fep->napi);
2500 netif_tx_lock_bh(ndev);
2502 netif_tx_wake_all_queues(ndev);
2503 netif_tx_unlock_bh(ndev);
2504 napi_enable(&fep->napi);
2510 static const struct fec_stat {
2511 char name[ETH_GSTRING_LEN];
2515 { "tx_dropped", RMON_T_DROP },
2516 { "tx_packets", RMON_T_PACKETS },
2517 { "tx_broadcast", RMON_T_BC_PKT },
2518 { "tx_multicast", RMON_T_MC_PKT },
2519 { "tx_crc_errors", RMON_T_CRC_ALIGN },
2520 { "tx_undersize", RMON_T_UNDERSIZE },
2521 { "tx_oversize", RMON_T_OVERSIZE },
2522 { "tx_fragment", RMON_T_FRAG },
2523 { "tx_jabber", RMON_T_JAB },
2524 { "tx_collision", RMON_T_COL },
2525 { "tx_64byte", RMON_T_P64 },
2526 { "tx_65to127byte", RMON_T_P65TO127 },
2527 { "tx_128to255byte", RMON_T_P128TO255 },
2528 { "tx_256to511byte", RMON_T_P256TO511 },
2529 { "tx_512to1023byte", RMON_T_P512TO1023 },
2530 { "tx_1024to2047byte", RMON_T_P1024TO2047 },
2531 { "tx_GTE2048byte", RMON_T_P_GTE2048 },
2532 { "tx_octets", RMON_T_OCTETS },
2535 { "IEEE_tx_drop", IEEE_T_DROP },
2536 { "IEEE_tx_frame_ok", IEEE_T_FRAME_OK },
2537 { "IEEE_tx_1col", IEEE_T_1COL },
2538 { "IEEE_tx_mcol", IEEE_T_MCOL },
2539 { "IEEE_tx_def", IEEE_T_DEF },
2540 { "IEEE_tx_lcol", IEEE_T_LCOL },
2541 { "IEEE_tx_excol", IEEE_T_EXCOL },
2542 { "IEEE_tx_macerr", IEEE_T_MACERR },
2543 { "IEEE_tx_cserr", IEEE_T_CSERR },
2544 { "IEEE_tx_sqe", IEEE_T_SQE },
2545 { "IEEE_tx_fdxfc", IEEE_T_FDXFC },
2546 { "IEEE_tx_octets_ok", IEEE_T_OCTETS_OK },
2549 { "rx_packets", RMON_R_PACKETS },
2550 { "rx_broadcast", RMON_R_BC_PKT },
2551 { "rx_multicast", RMON_R_MC_PKT },
2552 { "rx_crc_errors", RMON_R_CRC_ALIGN },
2553 { "rx_undersize", RMON_R_UNDERSIZE },
2554 { "rx_oversize", RMON_R_OVERSIZE },
2555 { "rx_fragment", RMON_R_FRAG },
2556 { "rx_jabber", RMON_R_JAB },
2557 { "rx_64byte", RMON_R_P64 },
2558 { "rx_65to127byte", RMON_R_P65TO127 },
2559 { "rx_128to255byte", RMON_R_P128TO255 },
2560 { "rx_256to511byte", RMON_R_P256TO511 },
2561 { "rx_512to1023byte", RMON_R_P512TO1023 },
2562 { "rx_1024to2047byte", RMON_R_P1024TO2047 },
2563 { "rx_GTE2048byte", RMON_R_P_GTE2048 },
2564 { "rx_octets", RMON_R_OCTETS },
2567 { "IEEE_rx_drop", IEEE_R_DROP },
2568 { "IEEE_rx_frame_ok", IEEE_R_FRAME_OK },
2569 { "IEEE_rx_crc", IEEE_R_CRC },
2570 { "IEEE_rx_align", IEEE_R_ALIGN },
2571 { "IEEE_rx_macerr", IEEE_R_MACERR },
2572 { "IEEE_rx_fdxfc", IEEE_R_FDXFC },
2573 { "IEEE_rx_octets_ok", IEEE_R_OCTETS_OK },
2576 #define FEC_STATS_SIZE (ARRAY_SIZE(fec_stats) * sizeof(u64))
2578 static void fec_enet_update_ethtool_stats(struct net_device *dev)
2580 struct fec_enet_private *fep = netdev_priv(dev);
2583 for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
2584 fep->ethtool_stats[i] = readl(fep->hwp + fec_stats[i].offset);
2587 static void fec_enet_get_ethtool_stats(struct net_device *dev,
2588 struct ethtool_stats *stats, u64 *data)
2590 struct fec_enet_private *fep = netdev_priv(dev);
2592 if (netif_running(dev))
2593 fec_enet_update_ethtool_stats(dev);
2595 memcpy(data, fep->ethtool_stats, FEC_STATS_SIZE);
2598 static void fec_enet_get_strings(struct net_device *netdev,
2599 u32 stringset, u8 *data)
2602 switch (stringset) {
2604 for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
2605 memcpy(data + i * ETH_GSTRING_LEN,
2606 fec_stats[i].name, ETH_GSTRING_LEN);
2609 net_selftest_get_strings(data);
2614 static int fec_enet_get_sset_count(struct net_device *dev, int sset)
2618 return ARRAY_SIZE(fec_stats);
2620 return net_selftest_get_count();
2626 static void fec_enet_clear_ethtool_stats(struct net_device *dev)
2628 struct fec_enet_private *fep = netdev_priv(dev);
2631 /* Disable MIB statistics counters */
2632 writel(FEC_MIB_CTRLSTAT_DISABLE, fep->hwp + FEC_MIB_CTRLSTAT);
2634 for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
2635 writel(0, fep->hwp + fec_stats[i].offset);
2637 /* Don't disable MIB statistics counters */
2638 writel(0, fep->hwp + FEC_MIB_CTRLSTAT);
2641 #else /* !defined(CONFIG_M5272) */
2642 #define FEC_STATS_SIZE 0
2643 static inline void fec_enet_update_ethtool_stats(struct net_device *dev)
2647 static inline void fec_enet_clear_ethtool_stats(struct net_device *dev)
2650 #endif /* !defined(CONFIG_M5272) */
2652 /* ITR clock source is enet system clock (clk_ahb).
2653 * TCTT unit is cycle_ns * 64 cycle
2654 * So, the ICTT value = X us / (cycle_ns * 64)
2656 static int fec_enet_us_to_itr_clock(struct net_device *ndev, int us)
2658 struct fec_enet_private *fep = netdev_priv(ndev);
2660 return us * (fep->itr_clk_rate / 64000) / 1000;
2663 /* Set threshold for interrupt coalescing */
2664 static void fec_enet_itr_coal_set(struct net_device *ndev)
2666 struct fec_enet_private *fep = netdev_priv(ndev);
2669 /* Must be greater than zero to avoid unpredictable behavior */
2670 if (!fep->rx_time_itr || !fep->rx_pkts_itr ||
2671 !fep->tx_time_itr || !fep->tx_pkts_itr)
2674 /* Select enet system clock as Interrupt Coalescing
2675 * timer Clock Source
2677 rx_itr = FEC_ITR_CLK_SEL;
2678 tx_itr = FEC_ITR_CLK_SEL;
2680 /* set ICFT and ICTT */
2681 rx_itr |= FEC_ITR_ICFT(fep->rx_pkts_itr);
2682 rx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->rx_time_itr));
2683 tx_itr |= FEC_ITR_ICFT(fep->tx_pkts_itr);
2684 tx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->tx_time_itr));
2686 rx_itr |= FEC_ITR_EN;
2687 tx_itr |= FEC_ITR_EN;
2689 writel(tx_itr, fep->hwp + FEC_TXIC0);
2690 writel(rx_itr, fep->hwp + FEC_RXIC0);
2691 if (fep->quirks & FEC_QUIRK_HAS_MULTI_QUEUES) {
2692 writel(tx_itr, fep->hwp + FEC_TXIC1);
2693 writel(rx_itr, fep->hwp + FEC_RXIC1);
2694 writel(tx_itr, fep->hwp + FEC_TXIC2);
2695 writel(rx_itr, fep->hwp + FEC_RXIC2);
2699 static int fec_enet_get_coalesce(struct net_device *ndev,
2700 struct ethtool_coalesce *ec,
2701 struct kernel_ethtool_coalesce *kernel_coal,
2702 struct netlink_ext_ack *extack)
2704 struct fec_enet_private *fep = netdev_priv(ndev);
2706 if (!(fep->quirks & FEC_QUIRK_HAS_COALESCE))
2709 ec->rx_coalesce_usecs = fep->rx_time_itr;
2710 ec->rx_max_coalesced_frames = fep->rx_pkts_itr;
2712 ec->tx_coalesce_usecs = fep->tx_time_itr;
2713 ec->tx_max_coalesced_frames = fep->tx_pkts_itr;
2718 static int fec_enet_set_coalesce(struct net_device *ndev,
2719 struct ethtool_coalesce *ec,
2720 struct kernel_ethtool_coalesce *kernel_coal,
2721 struct netlink_ext_ack *extack)
2723 struct fec_enet_private *fep = netdev_priv(ndev);
2724 struct device *dev = &fep->pdev->dev;
2727 if (!(fep->quirks & FEC_QUIRK_HAS_COALESCE))
2730 if (ec->rx_max_coalesced_frames > 255) {
2731 dev_err(dev, "Rx coalesced frames exceed hardware limitation\n");
2735 if (ec->tx_max_coalesced_frames > 255) {
2736 dev_err(dev, "Tx coalesced frame exceed hardware limitation\n");
2740 cycle = fec_enet_us_to_itr_clock(ndev, ec->rx_coalesce_usecs);
2741 if (cycle > 0xFFFF) {
2742 dev_err(dev, "Rx coalesced usec exceed hardware limitation\n");
2746 cycle = fec_enet_us_to_itr_clock(ndev, ec->tx_coalesce_usecs);
2747 if (cycle > 0xFFFF) {
2748 dev_err(dev, "Tx coalesced usec exceed hardware limitation\n");
2752 fep->rx_time_itr = ec->rx_coalesce_usecs;
2753 fep->rx_pkts_itr = ec->rx_max_coalesced_frames;
2755 fep->tx_time_itr = ec->tx_coalesce_usecs;
2756 fep->tx_pkts_itr = ec->tx_max_coalesced_frames;
2758 fec_enet_itr_coal_set(ndev);
2763 static void fec_enet_itr_coal_init(struct net_device *ndev)
2765 struct ethtool_coalesce ec;
2767 ec.rx_coalesce_usecs = FEC_ITR_ICTT_DEFAULT;
2768 ec.rx_max_coalesced_frames = FEC_ITR_ICFT_DEFAULT;
2770 ec.tx_coalesce_usecs = FEC_ITR_ICTT_DEFAULT;
2771 ec.tx_max_coalesced_frames = FEC_ITR_ICFT_DEFAULT;
2773 fec_enet_set_coalesce(ndev, &ec, NULL, NULL);
2776 static int fec_enet_get_tunable(struct net_device *netdev,
2777 const struct ethtool_tunable *tuna,
2780 struct fec_enet_private *fep = netdev_priv(netdev);
2784 case ETHTOOL_RX_COPYBREAK:
2785 *(u32 *)data = fep->rx_copybreak;
2795 static int fec_enet_set_tunable(struct net_device *netdev,
2796 const struct ethtool_tunable *tuna,
2799 struct fec_enet_private *fep = netdev_priv(netdev);
2803 case ETHTOOL_RX_COPYBREAK:
2804 fep->rx_copybreak = *(u32 *)data;
2814 /* LPI Sleep Ts count base on tx clk (clk_ref).
2815 * The lpi sleep cnt value = X us / (cycle_ns).
2817 static int fec_enet_us_to_tx_cycle(struct net_device *ndev, int us)
2819 struct fec_enet_private *fep = netdev_priv(ndev);
2821 return us * (fep->clk_ref_rate / 1000) / 1000;
2824 static int fec_enet_eee_mode_set(struct net_device *ndev, bool enable)
2826 struct fec_enet_private *fep = netdev_priv(ndev);
2827 struct ethtool_eee *p = &fep->eee;
2828 unsigned int sleep_cycle, wake_cycle;
2832 ret = phy_init_eee(ndev->phydev, 0);
2836 sleep_cycle = fec_enet_us_to_tx_cycle(ndev, p->tx_lpi_timer);
2837 wake_cycle = sleep_cycle;
2843 p->tx_lpi_enabled = enable;
2844 p->eee_enabled = enable;
2845 p->eee_active = enable;
2847 writel(sleep_cycle, fep->hwp + FEC_LPI_SLEEP);
2848 writel(wake_cycle, fep->hwp + FEC_LPI_WAKE);
2854 fec_enet_get_eee(struct net_device *ndev, struct ethtool_eee *edata)
2856 struct fec_enet_private *fep = netdev_priv(ndev);
2857 struct ethtool_eee *p = &fep->eee;
2859 if (!(fep->quirks & FEC_QUIRK_HAS_EEE))
2862 if (!netif_running(ndev))
2865 edata->eee_enabled = p->eee_enabled;
2866 edata->eee_active = p->eee_active;
2867 edata->tx_lpi_timer = p->tx_lpi_timer;
2868 edata->tx_lpi_enabled = p->tx_lpi_enabled;
2870 return phy_ethtool_get_eee(ndev->phydev, edata);
2874 fec_enet_set_eee(struct net_device *ndev, struct ethtool_eee *edata)
2876 struct fec_enet_private *fep = netdev_priv(ndev);
2877 struct ethtool_eee *p = &fep->eee;
2880 if (!(fep->quirks & FEC_QUIRK_HAS_EEE))
2883 if (!netif_running(ndev))
2886 p->tx_lpi_timer = edata->tx_lpi_timer;
2888 if (!edata->eee_enabled || !edata->tx_lpi_enabled ||
2889 !edata->tx_lpi_timer)
2890 ret = fec_enet_eee_mode_set(ndev, false);
2892 ret = fec_enet_eee_mode_set(ndev, true);
2897 return phy_ethtool_set_eee(ndev->phydev, edata);
2901 fec_enet_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
2903 struct fec_enet_private *fep = netdev_priv(ndev);
2905 if (fep->wol_flag & FEC_WOL_HAS_MAGIC_PACKET) {
2906 wol->supported = WAKE_MAGIC;
2907 wol->wolopts = fep->wol_flag & FEC_WOL_FLAG_ENABLE ? WAKE_MAGIC : 0;
2909 wol->supported = wol->wolopts = 0;
2914 fec_enet_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
2916 struct fec_enet_private *fep = netdev_priv(ndev);
2918 if (!(fep->wol_flag & FEC_WOL_HAS_MAGIC_PACKET))
2921 if (wol->wolopts & ~WAKE_MAGIC)
2924 device_set_wakeup_enable(&ndev->dev, wol->wolopts & WAKE_MAGIC);
2925 if (device_may_wakeup(&ndev->dev)) {
2926 fep->wol_flag |= FEC_WOL_FLAG_ENABLE;
2927 if (fep->wake_irq > 0)
2928 enable_irq_wake(fep->wake_irq);
2930 fep->wol_flag &= (~FEC_WOL_FLAG_ENABLE);
2931 if (fep->wake_irq > 0)
2932 disable_irq_wake(fep->wake_irq);
2938 static const struct ethtool_ops fec_enet_ethtool_ops = {
2939 .supported_coalesce_params = ETHTOOL_COALESCE_USECS |
2940 ETHTOOL_COALESCE_MAX_FRAMES,
2941 .get_drvinfo = fec_enet_get_drvinfo,
2942 .get_regs_len = fec_enet_get_regs_len,
2943 .get_regs = fec_enet_get_regs,
2944 .nway_reset = phy_ethtool_nway_reset,
2945 .get_link = ethtool_op_get_link,
2946 .get_coalesce = fec_enet_get_coalesce,
2947 .set_coalesce = fec_enet_set_coalesce,
2948 #ifndef CONFIG_M5272
2949 .get_pauseparam = fec_enet_get_pauseparam,
2950 .set_pauseparam = fec_enet_set_pauseparam,
2951 .get_strings = fec_enet_get_strings,
2952 .get_ethtool_stats = fec_enet_get_ethtool_stats,
2953 .get_sset_count = fec_enet_get_sset_count,
2955 .get_ts_info = fec_enet_get_ts_info,
2956 .get_tunable = fec_enet_get_tunable,
2957 .set_tunable = fec_enet_set_tunable,
2958 .get_wol = fec_enet_get_wol,
2959 .set_wol = fec_enet_set_wol,
2960 .get_eee = fec_enet_get_eee,
2961 .set_eee = fec_enet_set_eee,
2962 .get_link_ksettings = phy_ethtool_get_link_ksettings,
2963 .set_link_ksettings = phy_ethtool_set_link_ksettings,
2964 .self_test = net_selftest,
2967 static int fec_enet_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
2969 struct fec_enet_private *fep = netdev_priv(ndev);
2970 struct phy_device *phydev = ndev->phydev;
2972 if (!netif_running(ndev))
2978 if (fep->bufdesc_ex) {
2979 bool use_fec_hwts = !phy_has_hwtstamp(phydev);
2981 if (cmd == SIOCSHWTSTAMP) {
2983 return fec_ptp_set(ndev, rq);
2984 fec_ptp_disable_hwts(ndev);
2985 } else if (cmd == SIOCGHWTSTAMP) {
2987 return fec_ptp_get(ndev, rq);
2991 return phy_mii_ioctl(phydev, rq, cmd);
2994 static void fec_enet_free_buffers(struct net_device *ndev)
2996 struct fec_enet_private *fep = netdev_priv(ndev);
2998 struct sk_buff *skb;
2999 struct bufdesc *bdp;
3000 struct fec_enet_priv_tx_q *txq;
3001 struct fec_enet_priv_rx_q *rxq;
3004 for (q = 0; q < fep->num_rx_queues; q++) {
3005 rxq = fep->rx_queue[q];
3007 for (i = 0; i < rxq->bd.ring_size; i++) {
3008 skb = rxq->rx_skbuff[i];
3009 rxq->rx_skbuff[i] = NULL;
3011 dma_unmap_single(&fep->pdev->dev,
3012 fec32_to_cpu(bdp->cbd_bufaddr),
3013 FEC_ENET_RX_FRSIZE - fep->rx_align,
3017 bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
3021 for (q = 0; q < fep->num_tx_queues; q++) {
3022 txq = fep->tx_queue[q];
3023 for (i = 0; i < txq->bd.ring_size; i++) {
3024 kfree(txq->tx_bounce[i]);
3025 txq->tx_bounce[i] = NULL;
3026 skb = txq->tx_skbuff[i];
3027 txq->tx_skbuff[i] = NULL;
3033 static void fec_enet_free_queue(struct net_device *ndev)
3035 struct fec_enet_private *fep = netdev_priv(ndev);
3037 struct fec_enet_priv_tx_q *txq;
3039 for (i = 0; i < fep->num_tx_queues; i++)
3040 if (fep->tx_queue[i] && fep->tx_queue[i]->tso_hdrs) {
3041 txq = fep->tx_queue[i];
3042 dma_free_coherent(&fep->pdev->dev,
3043 txq->bd.ring_size * TSO_HEADER_SIZE,
3048 for (i = 0; i < fep->num_rx_queues; i++)
3049 kfree(fep->rx_queue[i]);
3050 for (i = 0; i < fep->num_tx_queues; i++)
3051 kfree(fep->tx_queue[i]);
3054 static int fec_enet_alloc_queue(struct net_device *ndev)
3056 struct fec_enet_private *fep = netdev_priv(ndev);
3059 struct fec_enet_priv_tx_q *txq;
3061 for (i = 0; i < fep->num_tx_queues; i++) {
3062 txq = kzalloc(sizeof(*txq), GFP_KERNEL);
3068 fep->tx_queue[i] = txq;
3069 txq->bd.ring_size = TX_RING_SIZE;
3070 fep->total_tx_ring_size += fep->tx_queue[i]->bd.ring_size;
3072 txq->tx_stop_threshold = FEC_MAX_SKB_DESCS;
3073 txq->tx_wake_threshold =
3074 (txq->bd.ring_size - txq->tx_stop_threshold) / 2;
3076 txq->tso_hdrs = dma_alloc_coherent(&fep->pdev->dev,
3077 txq->bd.ring_size * TSO_HEADER_SIZE,
3080 if (!txq->tso_hdrs) {
3086 for (i = 0; i < fep->num_rx_queues; i++) {
3087 fep->rx_queue[i] = kzalloc(sizeof(*fep->rx_queue[i]),
3089 if (!fep->rx_queue[i]) {
3094 fep->rx_queue[i]->bd.ring_size = RX_RING_SIZE;
3095 fep->total_rx_ring_size += fep->rx_queue[i]->bd.ring_size;
3100 fec_enet_free_queue(ndev);
3105 fec_enet_alloc_rxq_buffers(struct net_device *ndev, unsigned int queue)
3107 struct fec_enet_private *fep = netdev_priv(ndev);
3109 struct sk_buff *skb;
3110 struct bufdesc *bdp;
3111 struct fec_enet_priv_rx_q *rxq;
3113 rxq = fep->rx_queue[queue];
3115 for (i = 0; i < rxq->bd.ring_size; i++) {
3116 skb = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
3120 if (fec_enet_new_rxbdp(ndev, bdp, skb)) {
3125 rxq->rx_skbuff[i] = skb;
3126 bdp->cbd_sc = cpu_to_fec16(BD_ENET_RX_EMPTY);
3128 if (fep->bufdesc_ex) {
3129 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
3130 ebdp->cbd_esc = cpu_to_fec32(BD_ENET_RX_INT);
3133 bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
3136 /* Set the last buffer to wrap. */
3137 bdp = fec_enet_get_prevdesc(bdp, &rxq->bd);
3138 bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP);
3142 fec_enet_free_buffers(ndev);
3147 fec_enet_alloc_txq_buffers(struct net_device *ndev, unsigned int queue)
3149 struct fec_enet_private *fep = netdev_priv(ndev);
3151 struct bufdesc *bdp;
3152 struct fec_enet_priv_tx_q *txq;
3154 txq = fep->tx_queue[queue];
3156 for (i = 0; i < txq->bd.ring_size; i++) {
3157 txq->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL);
3158 if (!txq->tx_bounce[i])
3161 bdp->cbd_sc = cpu_to_fec16(0);
3162 bdp->cbd_bufaddr = cpu_to_fec32(0);
3164 if (fep->bufdesc_ex) {
3165 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
3166 ebdp->cbd_esc = cpu_to_fec32(BD_ENET_TX_INT);
3169 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
3172 /* Set the last buffer to wrap. */
3173 bdp = fec_enet_get_prevdesc(bdp, &txq->bd);
3174 bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP);
3179 fec_enet_free_buffers(ndev);
3183 static int fec_enet_alloc_buffers(struct net_device *ndev)
3185 struct fec_enet_private *fep = netdev_priv(ndev);
3188 for (i = 0; i < fep->num_rx_queues; i++)
3189 if (fec_enet_alloc_rxq_buffers(ndev, i))
3192 for (i = 0; i < fep->num_tx_queues; i++)
3193 if (fec_enet_alloc_txq_buffers(ndev, i))
3199 fec_enet_open(struct net_device *ndev)
3201 struct fec_enet_private *fep = netdev_priv(ndev);
3205 ret = pm_runtime_resume_and_get(&fep->pdev->dev);
3209 pinctrl_pm_select_default_state(&fep->pdev->dev);
3210 ret = fec_enet_clk_enable(ndev, true);
3214 /* During the first fec_enet_open call the PHY isn't probed at this
3215 * point. Therefore the phy_reset_after_clk_enable() call within
3216 * fec_enet_clk_enable() fails. As we need this reset in order to be
3217 * sure the PHY is working correctly we check if we need to reset again
3218 * later when the PHY is probed
3220 if (ndev->phydev && ndev->phydev->drv)
3221 reset_again = false;
3225 /* I should reset the ring buffers here, but I don't yet know
3226 * a simple way to do that.
3229 ret = fec_enet_alloc_buffers(ndev);
3231 goto err_enet_alloc;
3233 /* Init MAC prior to mii bus probe */
3236 /* Call phy_reset_after_clk_enable() again if it failed during
3237 * phy_reset_after_clk_enable() before because the PHY wasn't probed.
3240 fec_enet_phy_reset_after_clk_enable(ndev);
3242 /* Probe and connect to PHY when open the interface */
3243 ret = fec_enet_mii_probe(ndev);
3245 goto err_enet_mii_probe;
3247 if (fep->quirks & FEC_QUIRK_ERR006687)
3248 imx6q_cpuidle_fec_irqs_used();
3250 napi_enable(&fep->napi);
3251 phy_start(ndev->phydev);
3252 netif_tx_start_all_queues(ndev);
3254 device_set_wakeup_enable(&ndev->dev, fep->wol_flag &
3255 FEC_WOL_FLAG_ENABLE);
3260 fec_enet_free_buffers(ndev);
3262 fec_enet_clk_enable(ndev, false);
3264 pm_runtime_mark_last_busy(&fep->pdev->dev);
3265 pm_runtime_put_autosuspend(&fep->pdev->dev);
3266 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
3271 fec_enet_close(struct net_device *ndev)
3273 struct fec_enet_private *fep = netdev_priv(ndev);
3275 phy_stop(ndev->phydev);
3277 if (netif_device_present(ndev)) {
3278 napi_disable(&fep->napi);
3279 netif_tx_disable(ndev);
3283 phy_disconnect(ndev->phydev);
3285 if (fep->quirks & FEC_QUIRK_ERR006687)
3286 imx6q_cpuidle_fec_irqs_unused();
3288 fec_enet_update_ethtool_stats(ndev);
3290 fec_enet_clk_enable(ndev, false);
3291 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
3292 pm_runtime_mark_last_busy(&fep->pdev->dev);
3293 pm_runtime_put_autosuspend(&fep->pdev->dev);
3295 fec_enet_free_buffers(ndev);
3300 /* Set or clear the multicast filter for this adaptor.
3301 * Skeleton taken from sunlance driver.
3302 * The CPM Ethernet implementation allows Multicast as well as individual
3303 * MAC address filtering. Some of the drivers check to make sure it is
3304 * a group multicast address, and discard those that are not. I guess I
3305 * will do the same for now, but just remove the test if you want
3306 * individual filtering as well (do the upper net layers want or support
3307 * this kind of feature?).
3310 #define FEC_HASH_BITS 6 /* #bits in hash */
3312 static void set_multicast_list(struct net_device *ndev)
3314 struct fec_enet_private *fep = netdev_priv(ndev);
3315 struct netdev_hw_addr *ha;
3316 unsigned int crc, tmp;
3318 unsigned int hash_high = 0, hash_low = 0;
3320 if (ndev->flags & IFF_PROMISC) {
3321 tmp = readl(fep->hwp + FEC_R_CNTRL);
3323 writel(tmp, fep->hwp + FEC_R_CNTRL);
3327 tmp = readl(fep->hwp + FEC_R_CNTRL);
3329 writel(tmp, fep->hwp + FEC_R_CNTRL);
3331 if (ndev->flags & IFF_ALLMULTI) {
3332 /* Catch all multicast addresses, so set the
3335 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
3336 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
3341 /* Add the addresses in hash register */
3342 netdev_for_each_mc_addr(ha, ndev) {
3343 /* calculate crc32 value of mac address */
3344 crc = ether_crc_le(ndev->addr_len, ha->addr);
3346 /* only upper 6 bits (FEC_HASH_BITS) are used
3347 * which point to specific bit in the hash registers
3349 hash = (crc >> (32 - FEC_HASH_BITS)) & 0x3f;
3352 hash_high |= 1 << (hash - 32);
3354 hash_low |= 1 << hash;
3357 writel(hash_high, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
3358 writel(hash_low, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
3361 /* Set a MAC change in hardware. */
3363 fec_set_mac_address(struct net_device *ndev, void *p)
3365 struct fec_enet_private *fep = netdev_priv(ndev);
3366 struct sockaddr *addr = p;
3369 if (!is_valid_ether_addr(addr->sa_data))
3370 return -EADDRNOTAVAIL;
3371 memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len);
3374 /* Add netif status check here to avoid system hang in below case:
3375 * ifconfig ethx down; ifconfig ethx hw ether xx:xx:xx:xx:xx:xx;
3376 * After ethx down, fec all clocks are gated off and then register
3377 * access causes system hang.
3379 if (!netif_running(ndev))
3382 writel(ndev->dev_addr[3] | (ndev->dev_addr[2] << 8) |
3383 (ndev->dev_addr[1] << 16) | (ndev->dev_addr[0] << 24),
3384 fep->hwp + FEC_ADDR_LOW);
3385 writel((ndev->dev_addr[5] << 16) | (ndev->dev_addr[4] << 24),
3386 fep->hwp + FEC_ADDR_HIGH);
3390 #ifdef CONFIG_NET_POLL_CONTROLLER
3392 * fec_poll_controller - FEC Poll controller function
3393 * @dev: The FEC network adapter
3395 * Polled functionality used by netconsole and others in non interrupt mode
3398 static void fec_poll_controller(struct net_device *dev)
3401 struct fec_enet_private *fep = netdev_priv(dev);
3403 for (i = 0; i < FEC_IRQ_NUM; i++) {
3404 if (fep->irq[i] > 0) {
3405 disable_irq(fep->irq[i]);
3406 fec_enet_interrupt(fep->irq[i], dev);
3407 enable_irq(fep->irq[i]);
3413 static inline void fec_enet_set_netdev_features(struct net_device *netdev,
3414 netdev_features_t features)
3416 struct fec_enet_private *fep = netdev_priv(netdev);
3417 netdev_features_t changed = features ^ netdev->features;
3419 netdev->features = features;
3421 /* Receive checksum has been changed */
3422 if (changed & NETIF_F_RXCSUM) {
3423 if (features & NETIF_F_RXCSUM)
3424 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
3426 fep->csum_flags &= ~FLAG_RX_CSUM_ENABLED;
3430 static int fec_set_features(struct net_device *netdev,
3431 netdev_features_t features)
3433 struct fec_enet_private *fep = netdev_priv(netdev);
3434 netdev_features_t changed = features ^ netdev->features;
3436 if (netif_running(netdev) && changed & NETIF_F_RXCSUM) {
3437 napi_disable(&fep->napi);
3438 netif_tx_lock_bh(netdev);
3440 fec_enet_set_netdev_features(netdev, features);
3441 fec_restart(netdev);
3442 netif_tx_wake_all_queues(netdev);
3443 netif_tx_unlock_bh(netdev);
3444 napi_enable(&fep->napi);
3446 fec_enet_set_netdev_features(netdev, features);
3452 static u16 fec_enet_get_raw_vlan_tci(struct sk_buff *skb)
3454 struct vlan_ethhdr *vhdr;
3455 unsigned short vlan_TCI = 0;
3457 if (skb->protocol == htons(ETH_P_ALL)) {
3458 vhdr = (struct vlan_ethhdr *)(skb->data);
3459 vlan_TCI = ntohs(vhdr->h_vlan_TCI);
3465 static u16 fec_enet_select_queue(struct net_device *ndev, struct sk_buff *skb,
3466 struct net_device *sb_dev)
3468 struct fec_enet_private *fep = netdev_priv(ndev);
3471 if (!(fep->quirks & FEC_QUIRK_HAS_AVB))
3472 return netdev_pick_tx(ndev, skb, NULL);
3474 vlan_tag = fec_enet_get_raw_vlan_tci(skb);
3478 return fec_enet_vlan_pri_to_queue[vlan_tag >> 13];
3481 static const struct net_device_ops fec_netdev_ops = {
3482 .ndo_open = fec_enet_open,
3483 .ndo_stop = fec_enet_close,
3484 .ndo_start_xmit = fec_enet_start_xmit,
3485 .ndo_select_queue = fec_enet_select_queue,
3486 .ndo_set_rx_mode = set_multicast_list,
3487 .ndo_validate_addr = eth_validate_addr,
3488 .ndo_tx_timeout = fec_timeout,
3489 .ndo_set_mac_address = fec_set_mac_address,
3490 .ndo_eth_ioctl = fec_enet_ioctl,
3491 #ifdef CONFIG_NET_POLL_CONTROLLER
3492 .ndo_poll_controller = fec_poll_controller,
3494 .ndo_set_features = fec_set_features,
3497 static const unsigned short offset_des_active_rxq[] = {
3498 FEC_R_DES_ACTIVE_0, FEC_R_DES_ACTIVE_1, FEC_R_DES_ACTIVE_2
3501 static const unsigned short offset_des_active_txq[] = {
3502 FEC_X_DES_ACTIVE_0, FEC_X_DES_ACTIVE_1, FEC_X_DES_ACTIVE_2
3506 * XXX: We need to clean up on failure exits here.
3509 static int fec_enet_init(struct net_device *ndev)
3511 struct fec_enet_private *fep = netdev_priv(ndev);
3512 struct bufdesc *cbd_base;
3516 unsigned dsize = fep->bufdesc_ex ? sizeof(struct bufdesc_ex) :
3517 sizeof(struct bufdesc);
3518 unsigned dsize_log2 = __fls(dsize);
3521 WARN_ON(dsize != (1 << dsize_log2));
3522 #if defined(CONFIG_ARM) || defined(CONFIG_ARM64)
3523 fep->rx_align = 0xf;
3524 fep->tx_align = 0xf;
3526 fep->rx_align = 0x3;
3527 fep->tx_align = 0x3;
3530 /* Check mask of the streaming and coherent API */
3531 ret = dma_set_mask_and_coherent(&fep->pdev->dev, DMA_BIT_MASK(32));
3533 dev_warn(&fep->pdev->dev, "No suitable DMA available\n");
3537 ret = fec_enet_alloc_queue(ndev);
3541 bd_size = (fep->total_tx_ring_size + fep->total_rx_ring_size) * dsize;
3543 /* Allocate memory for buffer descriptors. */
3544 cbd_base = dmam_alloc_coherent(&fep->pdev->dev, bd_size, &bd_dma,
3548 goto free_queue_mem;
3551 /* Get the Ethernet address */
3552 ret = fec_get_mac(ndev);
3554 goto free_queue_mem;
3556 /* make sure MAC we just acquired is programmed into the hw */
3557 fec_set_mac_address(ndev, NULL);
3559 /* Set receive and transmit descriptor base. */
3560 for (i = 0; i < fep->num_rx_queues; i++) {
3561 struct fec_enet_priv_rx_q *rxq = fep->rx_queue[i];
3562 unsigned size = dsize * rxq->bd.ring_size;
3565 rxq->bd.base = cbd_base;
3566 rxq->bd.cur = cbd_base;
3567 rxq->bd.dma = bd_dma;
3568 rxq->bd.dsize = dsize;
3569 rxq->bd.dsize_log2 = dsize_log2;
3570 rxq->bd.reg_desc_active = fep->hwp + offset_des_active_rxq[i];
3572 cbd_base = (struct bufdesc *)(((void *)cbd_base) + size);
3573 rxq->bd.last = (struct bufdesc *)(((void *)cbd_base) - dsize);
3576 for (i = 0; i < fep->num_tx_queues; i++) {
3577 struct fec_enet_priv_tx_q *txq = fep->tx_queue[i];
3578 unsigned size = dsize * txq->bd.ring_size;
3581 txq->bd.base = cbd_base;
3582 txq->bd.cur = cbd_base;
3583 txq->bd.dma = bd_dma;
3584 txq->bd.dsize = dsize;
3585 txq->bd.dsize_log2 = dsize_log2;
3586 txq->bd.reg_desc_active = fep->hwp + offset_des_active_txq[i];
3588 cbd_base = (struct bufdesc *)(((void *)cbd_base) + size);
3589 txq->bd.last = (struct bufdesc *)(((void *)cbd_base) - dsize);
3593 /* The FEC Ethernet specific entries in the device structure */
3594 ndev->watchdog_timeo = TX_TIMEOUT;
3595 ndev->netdev_ops = &fec_netdev_ops;
3596 ndev->ethtool_ops = &fec_enet_ethtool_ops;
3598 writel(FEC_RX_DISABLED_IMASK, fep->hwp + FEC_IMASK);
3599 netif_napi_add(ndev, &fep->napi, fec_enet_rx_napi, NAPI_POLL_WEIGHT);
3601 if (fep->quirks & FEC_QUIRK_HAS_VLAN)
3602 /* enable hw VLAN support */
3603 ndev->features |= NETIF_F_HW_VLAN_CTAG_RX;
3605 if (fep->quirks & FEC_QUIRK_HAS_CSUM) {
3606 ndev->gso_max_segs = FEC_MAX_TSO_SEGS;
3608 /* enable hw accelerator */
3609 ndev->features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
3610 | NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_TSO);
3611 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
3614 if (fep->quirks & FEC_QUIRK_HAS_MULTI_QUEUES) {
3616 fep->rx_align = 0x3f;
3619 ndev->hw_features = ndev->features;
3623 if (fep->quirks & FEC_QUIRK_MIB_CLEAR)
3624 fec_enet_clear_ethtool_stats(ndev);
3626 fec_enet_update_ethtool_stats(ndev);
3631 fec_enet_free_queue(ndev);
3636 static int fec_reset_phy(struct platform_device *pdev)
3639 bool active_high = false;
3640 int msec = 1, phy_post_delay = 0;
3641 struct device_node *np = pdev->dev.of_node;
3646 err = of_property_read_u32(np, "phy-reset-duration", &msec);
3647 /* A sane reset duration should not be longer than 1s */
3648 if (!err && msec > 1000)
3651 phy_reset = of_get_named_gpio(np, "phy-reset-gpios", 0);
3652 if (phy_reset == -EPROBE_DEFER)
3654 else if (!gpio_is_valid(phy_reset))
3657 err = of_property_read_u32(np, "phy-reset-post-delay", &phy_post_delay);
3658 /* valid reset duration should be less than 1s */
3659 if (!err && phy_post_delay > 1000)
3662 active_high = of_property_read_bool(np, "phy-reset-active-high");
3664 err = devm_gpio_request_one(&pdev->dev, phy_reset,
3665 active_high ? GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW,
3668 dev_err(&pdev->dev, "failed to get phy-reset-gpios: %d\n", err);
3675 usleep_range(msec * 1000, msec * 1000 + 1000);
3677 gpio_set_value_cansleep(phy_reset, !active_high);
3679 if (!phy_post_delay)
3682 if (phy_post_delay > 20)
3683 msleep(phy_post_delay);
3685 usleep_range(phy_post_delay * 1000,
3686 phy_post_delay * 1000 + 1000);
3690 #else /* CONFIG_OF */
3691 static int fec_reset_phy(struct platform_device *pdev)
3694 * In case of platform probe, the reset has been done
3699 #endif /* CONFIG_OF */
3702 fec_enet_get_queue_num(struct platform_device *pdev, int *num_tx, int *num_rx)
3704 struct device_node *np = pdev->dev.of_node;
3706 *num_tx = *num_rx = 1;
3708 if (!np || !of_device_is_available(np))
3711 /* parse the num of tx and rx queues */
3712 of_property_read_u32(np, "fsl,num-tx-queues", num_tx);
3714 of_property_read_u32(np, "fsl,num-rx-queues", num_rx);
3716 if (*num_tx < 1 || *num_tx > FEC_ENET_MAX_TX_QS) {
3717 dev_warn(&pdev->dev, "Invalid num_tx(=%d), fall back to 1\n",
3723 if (*num_rx < 1 || *num_rx > FEC_ENET_MAX_RX_QS) {
3724 dev_warn(&pdev->dev, "Invalid num_rx(=%d), fall back to 1\n",
3732 static int fec_enet_get_irq_cnt(struct platform_device *pdev)
3734 int irq_cnt = platform_irq_count(pdev);
3736 if (irq_cnt > FEC_IRQ_NUM)
3737 irq_cnt = FEC_IRQ_NUM; /* last for pps */
3738 else if (irq_cnt == 2)
3739 irq_cnt = 1; /* last for pps */
3740 else if (irq_cnt <= 0)
3741 irq_cnt = 1; /* At least 1 irq is needed */
3745 static void fec_enet_get_wakeup_irq(struct platform_device *pdev)
3747 struct net_device *ndev = platform_get_drvdata(pdev);
3748 struct fec_enet_private *fep = netdev_priv(ndev);
3750 if (fep->quirks & FEC_QUIRK_WAKEUP_FROM_INT2)
3751 fep->wake_irq = fep->irq[2];
3753 fep->wake_irq = fep->irq[0];
3756 static int fec_enet_init_stop_mode(struct fec_enet_private *fep,
3757 struct device_node *np)
3759 struct device_node *gpr_np;
3763 gpr_np = of_parse_phandle(np, "fsl,stop-mode", 0);
3767 ret = of_property_read_u32_array(np, "fsl,stop-mode", out_val,
3768 ARRAY_SIZE(out_val));
3770 dev_dbg(&fep->pdev->dev, "no stop mode property\n");
3774 fep->stop_gpr.gpr = syscon_node_to_regmap(gpr_np);
3775 if (IS_ERR(fep->stop_gpr.gpr)) {
3776 dev_err(&fep->pdev->dev, "could not find gpr regmap\n");
3777 ret = PTR_ERR(fep->stop_gpr.gpr);
3778 fep->stop_gpr.gpr = NULL;
3782 fep->stop_gpr.reg = out_val[1];
3783 fep->stop_gpr.bit = out_val[2];
3786 of_node_put(gpr_np);
3792 fec_probe(struct platform_device *pdev)
3794 struct fec_enet_private *fep;
3795 struct fec_platform_data *pdata;
3796 phy_interface_t interface;
3797 struct net_device *ndev;
3798 int i, irq, ret = 0;
3799 const struct of_device_id *of_id;
3801 struct device_node *np = pdev->dev.of_node, *phy_node;
3806 struct fec_devinfo *dev_info;
3808 fec_enet_get_queue_num(pdev, &num_tx_qs, &num_rx_qs);
3810 /* Init network device */
3811 ndev = alloc_etherdev_mqs(sizeof(struct fec_enet_private) +
3812 FEC_STATS_SIZE, num_tx_qs, num_rx_qs);
3816 SET_NETDEV_DEV(ndev, &pdev->dev);
3818 /* setup board info structure */
3819 fep = netdev_priv(ndev);
3821 of_id = of_match_device(fec_dt_ids, &pdev->dev);
3823 pdev->id_entry = of_id->data;
3824 dev_info = (struct fec_devinfo *)pdev->id_entry->driver_data;
3826 fep->quirks = dev_info->quirks;
3829 fep->num_rx_queues = num_rx_qs;
3830 fep->num_tx_queues = num_tx_qs;
3832 #if !defined(CONFIG_M5272)
3833 /* default enable pause frame auto negotiation */
3834 if (fep->quirks & FEC_QUIRK_HAS_GBIT)
3835 fep->pause_flag |= FEC_PAUSE_FLAG_AUTONEG;
3838 /* Select default pin state */
3839 pinctrl_pm_select_default_state(&pdev->dev);
3841 fep->hwp = devm_platform_ioremap_resource(pdev, 0);
3842 if (IS_ERR(fep->hwp)) {
3843 ret = PTR_ERR(fep->hwp);
3844 goto failed_ioremap;
3848 fep->dev_id = dev_id++;
3850 platform_set_drvdata(pdev, ndev);
3852 if ((of_machine_is_compatible("fsl,imx6q") ||
3853 of_machine_is_compatible("fsl,imx6dl")) &&
3854 !of_property_read_bool(np, "fsl,err006687-workaround-present"))
3855 fep->quirks |= FEC_QUIRK_ERR006687;
3857 if (of_get_property(np, "fsl,magic-packet", NULL))
3858 fep->wol_flag |= FEC_WOL_HAS_MAGIC_PACKET;
3860 ret = fec_enet_init_stop_mode(fep, np);
3862 goto failed_stop_mode;
3864 phy_node = of_parse_phandle(np, "phy-handle", 0);
3865 if (!phy_node && of_phy_is_fixed_link(np)) {
3866 ret = of_phy_register_fixed_link(np);
3869 "broken fixed-link specification\n");
3872 phy_node = of_node_get(np);
3874 fep->phy_node = phy_node;
3876 ret = of_get_phy_mode(pdev->dev.of_node, &interface);
3878 pdata = dev_get_platdata(&pdev->dev);
3880 fep->phy_interface = pdata->phy;
3882 fep->phy_interface = PHY_INTERFACE_MODE_MII;
3884 fep->phy_interface = interface;
3887 ret = fec_enet_parse_rgmii_delay(fep, np);
3889 goto failed_rgmii_delay;
3891 fep->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
3892 if (IS_ERR(fep->clk_ipg)) {
3893 ret = PTR_ERR(fep->clk_ipg);
3897 fep->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
3898 if (IS_ERR(fep->clk_ahb)) {
3899 ret = PTR_ERR(fep->clk_ahb);
3903 fep->itr_clk_rate = clk_get_rate(fep->clk_ahb);
3905 /* enet_out is optional, depends on board */
3906 fep->clk_enet_out = devm_clk_get(&pdev->dev, "enet_out");
3907 if (IS_ERR(fep->clk_enet_out))
3908 fep->clk_enet_out = NULL;
3910 fep->ptp_clk_on = false;
3911 mutex_init(&fep->ptp_clk_mutex);
3913 /* clk_ref is optional, depends on board */
3914 fep->clk_ref = devm_clk_get(&pdev->dev, "enet_clk_ref");
3915 if (IS_ERR(fep->clk_ref))
3916 fep->clk_ref = NULL;
3917 fep->clk_ref_rate = clk_get_rate(fep->clk_ref);
3919 /* clk_2x_txclk is optional, depends on board */
3920 if (fep->rgmii_txc_dly || fep->rgmii_rxc_dly) {
3921 fep->clk_2x_txclk = devm_clk_get(&pdev->dev, "enet_2x_txclk");
3922 if (IS_ERR(fep->clk_2x_txclk))
3923 fep->clk_2x_txclk = NULL;
3926 fep->bufdesc_ex = fep->quirks & FEC_QUIRK_HAS_BUFDESC_EX;
3927 fep->clk_ptp = devm_clk_get(&pdev->dev, "ptp");
3928 if (IS_ERR(fep->clk_ptp)) {
3929 fep->clk_ptp = NULL;
3930 fep->bufdesc_ex = false;
3933 ret = fec_enet_clk_enable(ndev, true);
3937 ret = clk_prepare_enable(fep->clk_ipg);
3939 goto failed_clk_ipg;
3940 ret = clk_prepare_enable(fep->clk_ahb);
3942 goto failed_clk_ahb;
3944 fep->reg_phy = devm_regulator_get_optional(&pdev->dev, "phy");
3945 if (!IS_ERR(fep->reg_phy)) {
3946 ret = regulator_enable(fep->reg_phy);
3949 "Failed to enable phy regulator: %d\n", ret);
3950 goto failed_regulator;
3953 if (PTR_ERR(fep->reg_phy) == -EPROBE_DEFER) {
3954 ret = -EPROBE_DEFER;
3955 goto failed_regulator;
3957 fep->reg_phy = NULL;
3960 pm_runtime_set_autosuspend_delay(&pdev->dev, FEC_MDIO_PM_TIMEOUT);
3961 pm_runtime_use_autosuspend(&pdev->dev);
3962 pm_runtime_get_noresume(&pdev->dev);
3963 pm_runtime_set_active(&pdev->dev);
3964 pm_runtime_enable(&pdev->dev);
3966 ret = fec_reset_phy(pdev);
3970 irq_cnt = fec_enet_get_irq_cnt(pdev);
3971 if (fep->bufdesc_ex)
3972 fec_ptp_init(pdev, irq_cnt);
3974 ret = fec_enet_init(ndev);
3978 for (i = 0; i < irq_cnt; i++) {
3979 snprintf(irq_name, sizeof(irq_name), "int%d", i);
3980 irq = platform_get_irq_byname_optional(pdev, irq_name);
3982 irq = platform_get_irq(pdev, i);
3987 ret = devm_request_irq(&pdev->dev, irq, fec_enet_interrupt,
3988 0, pdev->name, ndev);
3995 /* Decide which interrupt line is wakeup capable */
3996 fec_enet_get_wakeup_irq(pdev);
3998 ret = fec_enet_mii_init(pdev);
4000 goto failed_mii_init;
4002 /* Carrier starts down, phylib will bring it up */
4003 netif_carrier_off(ndev);
4004 fec_enet_clk_enable(ndev, false);
4005 pinctrl_pm_select_sleep_state(&pdev->dev);
4007 ndev->max_mtu = PKT_MAXBUF_SIZE - ETH_HLEN - ETH_FCS_LEN;
4009 ret = register_netdev(ndev);
4011 goto failed_register;
4013 device_init_wakeup(&ndev->dev, fep->wol_flag &
4014 FEC_WOL_HAS_MAGIC_PACKET);
4016 if (fep->bufdesc_ex && fep->ptp_clock)
4017 netdev_info(ndev, "registered PHC device %d\n", fep->dev_id);
4019 fep->rx_copybreak = COPYBREAK_DEFAULT;
4020 INIT_WORK(&fep->tx_timeout_work, fec_enet_timeout_work);
4022 pm_runtime_mark_last_busy(&pdev->dev);
4023 pm_runtime_put_autosuspend(&pdev->dev);
4028 fec_enet_mii_remove(fep);
4034 pm_runtime_put_noidle(&pdev->dev);
4035 pm_runtime_disable(&pdev->dev);
4037 regulator_disable(fep->reg_phy);
4039 clk_disable_unprepare(fep->clk_ahb);
4041 clk_disable_unprepare(fep->clk_ipg);
4043 fec_enet_clk_enable(ndev, false);
4046 if (of_phy_is_fixed_link(np))
4047 of_phy_deregister_fixed_link(np);
4048 of_node_put(phy_node);
4059 fec_drv_remove(struct platform_device *pdev)
4061 struct net_device *ndev = platform_get_drvdata(pdev);
4062 struct fec_enet_private *fep = netdev_priv(ndev);
4063 struct device_node *np = pdev->dev.of_node;
4066 ret = pm_runtime_resume_and_get(&pdev->dev);
4070 cancel_work_sync(&fep->tx_timeout_work);
4072 unregister_netdev(ndev);
4073 fec_enet_mii_remove(fep);
4075 regulator_disable(fep->reg_phy);
4077 if (of_phy_is_fixed_link(np))
4078 of_phy_deregister_fixed_link(np);
4079 of_node_put(fep->phy_node);
4081 clk_disable_unprepare(fep->clk_ahb);
4082 clk_disable_unprepare(fep->clk_ipg);
4083 pm_runtime_put_noidle(&pdev->dev);
4084 pm_runtime_disable(&pdev->dev);
4090 static int __maybe_unused fec_suspend(struct device *dev)
4092 struct net_device *ndev = dev_get_drvdata(dev);
4093 struct fec_enet_private *fep = netdev_priv(ndev);
4096 if (netif_running(ndev)) {
4097 if (fep->wol_flag & FEC_WOL_FLAG_ENABLE)
4098 fep->wol_flag |= FEC_WOL_FLAG_SLEEP_ON;
4099 phy_stop(ndev->phydev);
4100 napi_disable(&fep->napi);
4101 netif_tx_lock_bh(ndev);
4102 netif_device_detach(ndev);
4103 netif_tx_unlock_bh(ndev);
4105 fec_enet_clk_enable(ndev, false);
4106 if (!(fep->wol_flag & FEC_WOL_FLAG_ENABLE))
4107 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
4111 if (fep->reg_phy && !(fep->wol_flag & FEC_WOL_FLAG_ENABLE))
4112 regulator_disable(fep->reg_phy);
4114 /* SOC supply clock to phy, when clock is disabled, phy link down
4115 * SOC control phy regulator, when regulator is disabled, phy link down
4117 if (fep->clk_enet_out || fep->reg_phy)
4123 static int __maybe_unused fec_resume(struct device *dev)
4125 struct net_device *ndev = dev_get_drvdata(dev);
4126 struct fec_enet_private *fep = netdev_priv(ndev);
4130 if (fep->reg_phy && !(fep->wol_flag & FEC_WOL_FLAG_ENABLE)) {
4131 ret = regulator_enable(fep->reg_phy);
4137 if (netif_running(ndev)) {
4138 ret = fec_enet_clk_enable(ndev, true);
4143 if (fep->wol_flag & FEC_WOL_FLAG_ENABLE) {
4144 fec_enet_stop_mode(fep, false);
4146 val = readl(fep->hwp + FEC_ECNTRL);
4147 val &= ~(FEC_ECR_MAGICEN | FEC_ECR_SLEEP);
4148 writel(val, fep->hwp + FEC_ECNTRL);
4149 fep->wol_flag &= ~FEC_WOL_FLAG_SLEEP_ON;
4151 pinctrl_pm_select_default_state(&fep->pdev->dev);
4154 netif_tx_lock_bh(ndev);
4155 netif_device_attach(ndev);
4156 netif_tx_unlock_bh(ndev);
4157 napi_enable(&fep->napi);
4158 phy_init_hw(ndev->phydev);
4159 phy_start(ndev->phydev);
4167 regulator_disable(fep->reg_phy);
4171 static int __maybe_unused fec_runtime_suspend(struct device *dev)
4173 struct net_device *ndev = dev_get_drvdata(dev);
4174 struct fec_enet_private *fep = netdev_priv(ndev);
4176 clk_disable_unprepare(fep->clk_ahb);
4177 clk_disable_unprepare(fep->clk_ipg);
4182 static int __maybe_unused fec_runtime_resume(struct device *dev)
4184 struct net_device *ndev = dev_get_drvdata(dev);
4185 struct fec_enet_private *fep = netdev_priv(ndev);
4188 ret = clk_prepare_enable(fep->clk_ahb);
4191 ret = clk_prepare_enable(fep->clk_ipg);
4193 goto failed_clk_ipg;
4198 clk_disable_unprepare(fep->clk_ahb);
4202 static const struct dev_pm_ops fec_pm_ops = {
4203 SET_SYSTEM_SLEEP_PM_OPS(fec_suspend, fec_resume)
4204 SET_RUNTIME_PM_OPS(fec_runtime_suspend, fec_runtime_resume, NULL)
4207 static struct platform_driver fec_driver = {
4209 .name = DRIVER_NAME,
4211 .of_match_table = fec_dt_ids,
4212 .suppress_bind_attrs = true,
4214 .id_table = fec_devtype,
4216 .remove = fec_drv_remove,
4219 module_platform_driver(fec_driver);
4221 MODULE_LICENSE("GPL");