2 * Fast Ethernet Controller (FEC) driver for Motorola MPC8xx.
3 * Copyright (c) 1997 Dan Malek (dmalek@jlc.net)
5 * Right now, I am very wasteful with the buffers. I allocate memory
6 * pages and then divide them into 2K frame buffers. This way I know I
7 * have buffers large enough to hold one frame within one buffer descriptor.
8 * Once I get this working, I will use 64 or 128 byte CPM buffers, which
9 * will be much more memory efficient and will easily handle lots of
12 * Much better multiple PHY support by Magnus Damm.
13 * Copyright (c) 2000 Ericsson Radio Systems AB.
15 * Support for FEC controller of ColdFire processors.
16 * Copyright (c) 2001-2005 Greg Ungerer (gerg@snapgear.com)
18 * Bug fixes and cleanup by Philippe De Muyter (phdm@macqel.be)
19 * Copyright (c) 2004-2006 Macq Electronique SA.
21 * Copyright (C) 2010-2011 Freescale Semiconductor, Inc.
24 #include <linux/module.h>
25 #include <linux/kernel.h>
26 #include <linux/string.h>
27 #include <linux/pm_runtime.h>
28 #include <linux/ptrace.h>
29 #include <linux/errno.h>
30 #include <linux/ioport.h>
31 #include <linux/slab.h>
32 #include <linux/interrupt.h>
33 #include <linux/delay.h>
34 #include <linux/netdevice.h>
35 #include <linux/etherdevice.h>
36 #include <linux/skbuff.h>
41 #include <linux/tcp.h>
42 #include <linux/udp.h>
43 #include <linux/icmp.h>
44 #include <linux/spinlock.h>
45 #include <linux/workqueue.h>
46 #include <linux/bitops.h>
48 #include <linux/irq.h>
49 #include <linux/clk.h>
50 #include <linux/platform_device.h>
51 #include <linux/mdio.h>
52 #include <linux/phy.h>
53 #include <linux/fec.h>
55 #include <linux/of_device.h>
56 #include <linux/of_gpio.h>
57 #include <linux/of_mdio.h>
58 #include <linux/of_net.h>
59 #include <linux/regulator/consumer.h>
60 #include <linux/if_vlan.h>
61 #include <linux/pinctrl/consumer.h>
62 #include <linux/prefetch.h>
63 #include <soc/imx/cpuidle.h>
65 #include <asm/cacheflush.h>
69 static void set_multicast_list(struct net_device *ndev);
70 static void fec_enet_itr_coal_init(struct net_device *ndev);
72 #define DRIVER_NAME "fec"
74 #define FEC_ENET_GET_QUQUE(_x) ((_x == 0) ? 1 : ((_x == 1) ? 2 : 0))
76 /* Pause frame feild and FIFO threshold */
77 #define FEC_ENET_FCE (1 << 5)
78 #define FEC_ENET_RSEM_V 0x84
79 #define FEC_ENET_RSFL_V 16
80 #define FEC_ENET_RAEM_V 0x8
81 #define FEC_ENET_RAFL_V 0x8
82 #define FEC_ENET_OPD_V 0xFFF0
83 #define FEC_MDIO_PM_TIMEOUT 100 /* ms */
85 static struct platform_device_id fec_devtype[] = {
87 /* keep it for coldfire */
92 .driver_data = FEC_QUIRK_USE_GASKET | FEC_QUIRK_HAS_RACC,
95 .driver_data = FEC_QUIRK_HAS_RACC,
98 .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_SWAP_FRAME |
99 FEC_QUIRK_SINGLE_MDIO | FEC_QUIRK_HAS_RACC,
102 .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
103 FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
104 FEC_QUIRK_HAS_VLAN | FEC_QUIRK_ERR006358 |
107 .name = "mvf600-fec",
108 .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_RACC,
110 .name = "imx6sx-fec",
111 .driver_data = 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_HAS_AVB |
114 FEC_QUIRK_ERR007885 | FEC_QUIRK_BUG_CAPTURE |
120 MODULE_DEVICE_TABLE(platform, fec_devtype);
123 IMX25_FEC = 1, /* runs on i.mx25/50/53 */
124 IMX27_FEC, /* runs on i.mx27/35/51 */
131 static const struct of_device_id fec_dt_ids[] = {
132 { .compatible = "fsl,imx25-fec", .data = &fec_devtype[IMX25_FEC], },
133 { .compatible = "fsl,imx27-fec", .data = &fec_devtype[IMX27_FEC], },
134 { .compatible = "fsl,imx28-fec", .data = &fec_devtype[IMX28_FEC], },
135 { .compatible = "fsl,imx6q-fec", .data = &fec_devtype[IMX6Q_FEC], },
136 { .compatible = "fsl,mvf600-fec", .data = &fec_devtype[MVF600_FEC], },
137 { .compatible = "fsl,imx6sx-fec", .data = &fec_devtype[IMX6SX_FEC], },
140 MODULE_DEVICE_TABLE(of, fec_dt_ids);
142 static unsigned char macaddr[ETH_ALEN];
143 module_param_array(macaddr, byte, NULL, 0);
144 MODULE_PARM_DESC(macaddr, "FEC Ethernet MAC address");
146 #if defined(CONFIG_M5272)
148 * Some hardware gets it MAC address out of local flash memory.
149 * if this is non-zero then assume it is the address to get MAC from.
151 #if defined(CONFIG_NETtel)
152 #define FEC_FLASHMAC 0xf0006006
153 #elif defined(CONFIG_GILBARCONAP) || defined(CONFIG_SCALES)
154 #define FEC_FLASHMAC 0xf0006000
155 #elif defined(CONFIG_CANCam)
156 #define FEC_FLASHMAC 0xf0020000
157 #elif defined (CONFIG_M5272C3)
158 #define FEC_FLASHMAC (0xffe04000 + 4)
159 #elif defined(CONFIG_MOD5272)
160 #define FEC_FLASHMAC 0xffc0406b
162 #define FEC_FLASHMAC 0
164 #endif /* CONFIG_M5272 */
166 /* The FEC stores dest/src/type/vlan, data, and checksum for receive packets.
168 #define PKT_MAXBUF_SIZE 1522
169 #define PKT_MINBUF_SIZE 64
170 #define PKT_MAXBLR_SIZE 1536
172 /* FEC receive acceleration */
173 #define FEC_RACC_IPDIS (1 << 1)
174 #define FEC_RACC_PRODIS (1 << 2)
175 #define FEC_RACC_OPTIONS (FEC_RACC_IPDIS | FEC_RACC_PRODIS)
178 * The 5270/5271/5280/5282/532x RX control register also contains maximum frame
179 * size bits. Other FEC hardware does not, so we need to take that into
180 * account when setting it.
182 #if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
183 defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM)
184 #define OPT_FRAME_SIZE (PKT_MAXBUF_SIZE << 16)
186 #define OPT_FRAME_SIZE 0
189 /* FEC MII MMFR bits definition */
190 #define FEC_MMFR_ST (1 << 30)
191 #define FEC_MMFR_OP_READ (2 << 28)
192 #define FEC_MMFR_OP_WRITE (1 << 28)
193 #define FEC_MMFR_PA(v) ((v & 0x1f) << 23)
194 #define FEC_MMFR_RA(v) ((v & 0x1f) << 18)
195 #define FEC_MMFR_TA (2 << 16)
196 #define FEC_MMFR_DATA(v) (v & 0xffff)
197 /* FEC ECR bits definition */
198 #define FEC_ECR_MAGICEN (1 << 2)
199 #define FEC_ECR_SLEEP (1 << 3)
201 #define FEC_MII_TIMEOUT 30000 /* us */
203 /* Transmitter timeout */
204 #define TX_TIMEOUT (2 * HZ)
206 #define FEC_PAUSE_FLAG_AUTONEG 0x1
207 #define FEC_PAUSE_FLAG_ENABLE 0x2
208 #define FEC_WOL_HAS_MAGIC_PACKET (0x1 << 0)
209 #define FEC_WOL_FLAG_ENABLE (0x1 << 1)
210 #define FEC_WOL_FLAG_SLEEP_ON (0x1 << 2)
212 #define COPYBREAK_DEFAULT 256
214 #define TSO_HEADER_SIZE 128
215 /* Max number of allowed TCP segments for software TSO */
216 #define FEC_MAX_TSO_SEGS 100
217 #define FEC_MAX_SKB_DESCS (FEC_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS)
219 #define IS_TSO_HEADER(txq, addr) \
220 ((addr >= txq->tso_hdrs_dma) && \
221 (addr < txq->tso_hdrs_dma + txq->bd.ring_size * TSO_HEADER_SIZE))
225 static struct bufdesc *fec_enet_get_nextdesc(struct bufdesc *bdp,
226 struct bufdesc_prop *bd)
228 return (bdp >= bd->last) ? bd->base
229 : (struct bufdesc *)(((unsigned)bdp) + bd->dsize);
232 static struct bufdesc *fec_enet_get_prevdesc(struct bufdesc *bdp,
233 struct bufdesc_prop *bd)
235 return (bdp <= bd->base) ? bd->last
236 : (struct bufdesc *)(((unsigned)bdp) - bd->dsize);
239 static int fec_enet_get_bd_index(struct bufdesc *bdp,
240 struct bufdesc_prop *bd)
242 return ((const char *)bdp - (const char *)bd->base) >> bd->dsize_log2;
245 static int fec_enet_get_free_txdesc_num(struct fec_enet_priv_tx_q *txq)
249 entries = (((const char *)txq->dirty_tx -
250 (const char *)txq->bd.cur) >> txq->bd.dsize_log2) - 1;
252 return entries >= 0 ? entries : entries + txq->bd.ring_size;
255 static void swap_buffer(void *bufaddr, int len)
258 unsigned int *buf = bufaddr;
260 for (i = 0; i < len; i += 4, buf++)
264 static void swap_buffer2(void *dst_buf, void *src_buf, int len)
267 unsigned int *src = src_buf;
268 unsigned int *dst = dst_buf;
270 for (i = 0; i < len; i += 4, src++, dst++)
274 static void fec_dump(struct net_device *ndev)
276 struct fec_enet_private *fep = netdev_priv(ndev);
278 struct fec_enet_priv_tx_q *txq;
281 netdev_info(ndev, "TX ring dump\n");
282 pr_info("Nr SC addr len SKB\n");
284 txq = fep->tx_queue[0];
288 pr_info("%3u %c%c 0x%04x 0x%08x %4u %p\n",
290 bdp == txq->bd.cur ? 'S' : ' ',
291 bdp == txq->dirty_tx ? 'H' : ' ',
292 fec16_to_cpu(bdp->cbd_sc),
293 fec32_to_cpu(bdp->cbd_bufaddr),
294 fec16_to_cpu(bdp->cbd_datlen),
295 txq->tx_skbuff[index]);
296 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
298 } while (bdp != txq->bd.base);
301 static inline bool is_ipv4_pkt(struct sk_buff *skb)
303 return skb->protocol == htons(ETH_P_IP) && ip_hdr(skb)->version == 4;
307 fec_enet_clear_csum(struct sk_buff *skb, struct net_device *ndev)
309 /* Only run for packets requiring a checksum. */
310 if (skb->ip_summed != CHECKSUM_PARTIAL)
313 if (unlikely(skb_cow_head(skb, 0)))
316 if (is_ipv4_pkt(skb))
317 ip_hdr(skb)->check = 0;
318 *(__sum16 *)(skb->head + skb->csum_start + skb->csum_offset) = 0;
323 static struct bufdesc *
324 fec_enet_txq_submit_frag_skb(struct fec_enet_priv_tx_q *txq,
326 struct net_device *ndev)
328 struct fec_enet_private *fep = netdev_priv(ndev);
329 struct bufdesc *bdp = txq->bd.cur;
330 struct bufdesc_ex *ebdp;
331 int nr_frags = skb_shinfo(skb)->nr_frags;
333 unsigned short status;
334 unsigned int estatus = 0;
335 skb_frag_t *this_frag;
341 for (frag = 0; frag < nr_frags; frag++) {
342 this_frag = &skb_shinfo(skb)->frags[frag];
343 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
344 ebdp = (struct bufdesc_ex *)bdp;
346 status = fec16_to_cpu(bdp->cbd_sc);
347 status &= ~BD_ENET_TX_STATS;
348 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
349 frag_len = skb_shinfo(skb)->frags[frag].size;
351 /* Handle the last BD specially */
352 if (frag == nr_frags - 1) {
353 status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
354 if (fep->bufdesc_ex) {
355 estatus |= BD_ENET_TX_INT;
356 if (unlikely(skb_shinfo(skb)->tx_flags &
357 SKBTX_HW_TSTAMP && fep->hwts_tx_en))
358 estatus |= BD_ENET_TX_TS;
362 if (fep->bufdesc_ex) {
363 if (fep->quirks & FEC_QUIRK_HAS_AVB)
364 estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
365 if (skb->ip_summed == CHECKSUM_PARTIAL)
366 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
368 ebdp->cbd_esc = cpu_to_fec32(estatus);
371 bufaddr = page_address(this_frag->page.p) + this_frag->page_offset;
373 index = fec_enet_get_bd_index(bdp, &txq->bd);
374 if (((unsigned long) bufaddr) & fep->tx_align ||
375 fep->quirks & FEC_QUIRK_SWAP_FRAME) {
376 memcpy(txq->tx_bounce[index], bufaddr, frag_len);
377 bufaddr = txq->tx_bounce[index];
379 if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
380 swap_buffer(bufaddr, frag_len);
383 addr = dma_map_single(&fep->pdev->dev, bufaddr, frag_len,
385 if (dma_mapping_error(&fep->pdev->dev, addr)) {
387 netdev_err(ndev, "Tx DMA memory map failed\n");
388 goto dma_mapping_error;
391 bdp->cbd_bufaddr = cpu_to_fec32(addr);
392 bdp->cbd_datlen = cpu_to_fec16(frag_len);
393 /* Make sure the updates to rest of the descriptor are
394 * performed before transferring ownership.
397 bdp->cbd_sc = cpu_to_fec16(status);
403 for (i = 0; i < frag; i++) {
404 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
405 dma_unmap_single(&fep->pdev->dev, fec32_to_cpu(bdp->cbd_bufaddr),
406 fec16_to_cpu(bdp->cbd_datlen), DMA_TO_DEVICE);
408 return ERR_PTR(-ENOMEM);
411 static int fec_enet_txq_submit_skb(struct fec_enet_priv_tx_q *txq,
412 struct sk_buff *skb, struct net_device *ndev)
414 struct fec_enet_private *fep = netdev_priv(ndev);
415 int nr_frags = skb_shinfo(skb)->nr_frags;
416 struct bufdesc *bdp, *last_bdp;
419 unsigned short status;
420 unsigned short buflen;
421 unsigned int estatus = 0;
425 entries_free = fec_enet_get_free_txdesc_num(txq);
426 if (entries_free < MAX_SKB_FRAGS + 1) {
427 dev_kfree_skb_any(skb);
429 netdev_err(ndev, "NOT enough BD for SG!\n");
433 /* Protocol checksum off-load for TCP and UDP. */
434 if (fec_enet_clear_csum(skb, ndev)) {
435 dev_kfree_skb_any(skb);
439 /* Fill in a Tx ring entry */
442 status = fec16_to_cpu(bdp->cbd_sc);
443 status &= ~BD_ENET_TX_STATS;
445 /* Set buffer length and buffer pointer */
447 buflen = skb_headlen(skb);
449 index = fec_enet_get_bd_index(bdp, &txq->bd);
450 if (((unsigned long) bufaddr) & fep->tx_align ||
451 fep->quirks & FEC_QUIRK_SWAP_FRAME) {
452 memcpy(txq->tx_bounce[index], skb->data, buflen);
453 bufaddr = txq->tx_bounce[index];
455 if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
456 swap_buffer(bufaddr, buflen);
459 /* Push the data cache so the CPM does not get stale memory data. */
460 addr = dma_map_single(&fep->pdev->dev, bufaddr, buflen, DMA_TO_DEVICE);
461 if (dma_mapping_error(&fep->pdev->dev, addr)) {
462 dev_kfree_skb_any(skb);
464 netdev_err(ndev, "Tx DMA memory map failed\n");
469 last_bdp = fec_enet_txq_submit_frag_skb(txq, skb, ndev);
470 if (IS_ERR(last_bdp)) {
471 dma_unmap_single(&fep->pdev->dev, addr,
472 buflen, DMA_TO_DEVICE);
473 dev_kfree_skb_any(skb);
477 status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
478 if (fep->bufdesc_ex) {
479 estatus = BD_ENET_TX_INT;
480 if (unlikely(skb_shinfo(skb)->tx_flags &
481 SKBTX_HW_TSTAMP && fep->hwts_tx_en))
482 estatus |= BD_ENET_TX_TS;
485 bdp->cbd_bufaddr = cpu_to_fec32(addr);
486 bdp->cbd_datlen = cpu_to_fec16(buflen);
488 if (fep->bufdesc_ex) {
490 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
492 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
494 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
496 if (fep->quirks & FEC_QUIRK_HAS_AVB)
497 estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
499 if (skb->ip_summed == CHECKSUM_PARTIAL)
500 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
503 ebdp->cbd_esc = cpu_to_fec32(estatus);
506 index = fec_enet_get_bd_index(last_bdp, &txq->bd);
507 /* Save skb pointer */
508 txq->tx_skbuff[index] = skb;
510 /* Make sure the updates to rest of the descriptor are performed before
511 * transferring ownership.
515 /* Send it on its way. Tell FEC it's ready, interrupt when done,
516 * it's the last BD of the frame, and to put the CRC on the end.
518 status |= (BD_ENET_TX_READY | BD_ENET_TX_TC);
519 bdp->cbd_sc = cpu_to_fec16(status);
521 /* If this was the last BD in the ring, start at the beginning again. */
522 bdp = fec_enet_get_nextdesc(last_bdp, &txq->bd);
524 skb_tx_timestamp(skb);
526 /* Make sure the update to bdp and tx_skbuff are performed before
532 /* Trigger transmission start */
533 writel(0, txq->bd.reg_desc_active);
539 fec_enet_txq_put_data_tso(struct fec_enet_priv_tx_q *txq, struct sk_buff *skb,
540 struct net_device *ndev,
541 struct bufdesc *bdp, int index, char *data,
542 int size, bool last_tcp, bool is_last)
544 struct fec_enet_private *fep = netdev_priv(ndev);
545 struct bufdesc_ex *ebdp = container_of(bdp, struct bufdesc_ex, desc);
546 unsigned short status;
547 unsigned int estatus = 0;
550 status = fec16_to_cpu(bdp->cbd_sc);
551 status &= ~BD_ENET_TX_STATS;
553 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
555 if (((unsigned long) data) & fep->tx_align ||
556 fep->quirks & FEC_QUIRK_SWAP_FRAME) {
557 memcpy(txq->tx_bounce[index], data, size);
558 data = txq->tx_bounce[index];
560 if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
561 swap_buffer(data, size);
564 addr = dma_map_single(&fep->pdev->dev, data, size, DMA_TO_DEVICE);
565 if (dma_mapping_error(&fep->pdev->dev, addr)) {
566 dev_kfree_skb_any(skb);
568 netdev_err(ndev, "Tx DMA memory map failed\n");
569 return NETDEV_TX_BUSY;
572 bdp->cbd_datlen = cpu_to_fec16(size);
573 bdp->cbd_bufaddr = cpu_to_fec32(addr);
575 if (fep->bufdesc_ex) {
576 if (fep->quirks & FEC_QUIRK_HAS_AVB)
577 estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
578 if (skb->ip_summed == CHECKSUM_PARTIAL)
579 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
581 ebdp->cbd_esc = cpu_to_fec32(estatus);
584 /* Handle the last BD specially */
586 status |= (BD_ENET_TX_LAST | BD_ENET_TX_TC);
588 status |= BD_ENET_TX_INTR;
590 ebdp->cbd_esc |= cpu_to_fec32(BD_ENET_TX_INT);
593 bdp->cbd_sc = cpu_to_fec16(status);
599 fec_enet_txq_put_hdr_tso(struct fec_enet_priv_tx_q *txq,
600 struct sk_buff *skb, struct net_device *ndev,
601 struct bufdesc *bdp, int index)
603 struct fec_enet_private *fep = netdev_priv(ndev);
604 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
605 struct bufdesc_ex *ebdp = container_of(bdp, struct bufdesc_ex, desc);
607 unsigned long dmabuf;
608 unsigned short status;
609 unsigned int estatus = 0;
611 status = fec16_to_cpu(bdp->cbd_sc);
612 status &= ~BD_ENET_TX_STATS;
613 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
615 bufaddr = txq->tso_hdrs + index * TSO_HEADER_SIZE;
616 dmabuf = txq->tso_hdrs_dma + index * TSO_HEADER_SIZE;
617 if (((unsigned long)bufaddr) & fep->tx_align ||
618 fep->quirks & FEC_QUIRK_SWAP_FRAME) {
619 memcpy(txq->tx_bounce[index], skb->data, hdr_len);
620 bufaddr = txq->tx_bounce[index];
622 if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
623 swap_buffer(bufaddr, hdr_len);
625 dmabuf = dma_map_single(&fep->pdev->dev, bufaddr,
626 hdr_len, DMA_TO_DEVICE);
627 if (dma_mapping_error(&fep->pdev->dev, dmabuf)) {
628 dev_kfree_skb_any(skb);
630 netdev_err(ndev, "Tx DMA memory map failed\n");
631 return NETDEV_TX_BUSY;
635 bdp->cbd_bufaddr = cpu_to_fec32(dmabuf);
636 bdp->cbd_datlen = cpu_to_fec16(hdr_len);
638 if (fep->bufdesc_ex) {
639 if (fep->quirks & FEC_QUIRK_HAS_AVB)
640 estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
641 if (skb->ip_summed == CHECKSUM_PARTIAL)
642 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
644 ebdp->cbd_esc = cpu_to_fec32(estatus);
647 bdp->cbd_sc = cpu_to_fec16(status);
652 static int fec_enet_txq_submit_tso(struct fec_enet_priv_tx_q *txq,
654 struct net_device *ndev)
656 struct fec_enet_private *fep = netdev_priv(ndev);
657 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
658 int total_len, data_left;
659 struct bufdesc *bdp = txq->bd.cur;
661 unsigned int index = 0;
664 if (tso_count_descs(skb) >= fec_enet_get_free_txdesc_num(txq)) {
665 dev_kfree_skb_any(skb);
667 netdev_err(ndev, "NOT enough BD for TSO!\n");
671 /* Protocol checksum off-load for TCP and UDP. */
672 if (fec_enet_clear_csum(skb, ndev)) {
673 dev_kfree_skb_any(skb);
677 /* Initialize the TSO handler, and prepare the first payload */
678 tso_start(skb, &tso);
680 total_len = skb->len - hdr_len;
681 while (total_len > 0) {
684 index = fec_enet_get_bd_index(bdp, &txq->bd);
685 data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
686 total_len -= data_left;
688 /* prepare packet headers: MAC + IP + TCP */
689 hdr = txq->tso_hdrs + index * TSO_HEADER_SIZE;
690 tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
691 ret = fec_enet_txq_put_hdr_tso(txq, skb, ndev, bdp, index);
695 while (data_left > 0) {
698 size = min_t(int, tso.size, data_left);
699 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
700 index = fec_enet_get_bd_index(bdp, &txq->bd);
701 ret = fec_enet_txq_put_data_tso(txq, skb, ndev,
710 tso_build_data(skb, &tso, size);
713 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
716 /* Save skb pointer */
717 txq->tx_skbuff[index] = skb;
719 skb_tx_timestamp(skb);
722 /* Trigger transmission start */
723 if (!(fep->quirks & FEC_QUIRK_ERR007885) ||
724 !readl(txq->bd.reg_desc_active) ||
725 !readl(txq->bd.reg_desc_active) ||
726 !readl(txq->bd.reg_desc_active) ||
727 !readl(txq->bd.reg_desc_active))
728 writel(0, txq->bd.reg_desc_active);
733 /* TODO: Release all used data descriptors for TSO */
738 fec_enet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
740 struct fec_enet_private *fep = netdev_priv(ndev);
742 unsigned short queue;
743 struct fec_enet_priv_tx_q *txq;
744 struct netdev_queue *nq;
747 queue = skb_get_queue_mapping(skb);
748 txq = fep->tx_queue[queue];
749 nq = netdev_get_tx_queue(ndev, queue);
752 ret = fec_enet_txq_submit_tso(txq, skb, ndev);
754 ret = fec_enet_txq_submit_skb(txq, skb, ndev);
758 entries_free = fec_enet_get_free_txdesc_num(txq);
759 if (entries_free <= txq->tx_stop_threshold)
760 netif_tx_stop_queue(nq);
765 /* Init RX & TX buffer descriptors
767 static void fec_enet_bd_init(struct net_device *dev)
769 struct fec_enet_private *fep = netdev_priv(dev);
770 struct fec_enet_priv_tx_q *txq;
771 struct fec_enet_priv_rx_q *rxq;
776 for (q = 0; q < fep->num_rx_queues; q++) {
777 /* Initialize the receive buffer descriptors. */
778 rxq = fep->rx_queue[q];
781 for (i = 0; i < rxq->bd.ring_size; i++) {
783 /* Initialize the BD for every fragment in the page. */
784 if (bdp->cbd_bufaddr)
785 bdp->cbd_sc = cpu_to_fec16(BD_ENET_RX_EMPTY);
787 bdp->cbd_sc = cpu_to_fec16(0);
788 bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
791 /* Set the last buffer to wrap */
792 bdp = fec_enet_get_prevdesc(bdp, &rxq->bd);
793 bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP);
795 rxq->bd.cur = rxq->bd.base;
798 for (q = 0; q < fep->num_tx_queues; q++) {
799 /* ...and the same for transmit */
800 txq = fep->tx_queue[q];
804 for (i = 0; i < txq->bd.ring_size; i++) {
805 /* Initialize the BD for every fragment in the page. */
806 bdp->cbd_sc = cpu_to_fec16(0);
807 if (txq->tx_skbuff[i]) {
808 dev_kfree_skb_any(txq->tx_skbuff[i]);
809 txq->tx_skbuff[i] = NULL;
811 bdp->cbd_bufaddr = cpu_to_fec32(0);
812 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
815 /* Set the last buffer to wrap */
816 bdp = fec_enet_get_prevdesc(bdp, &txq->bd);
817 bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP);
822 static void fec_enet_active_rxring(struct net_device *ndev)
824 struct fec_enet_private *fep = netdev_priv(ndev);
827 for (i = 0; i < fep->num_rx_queues; i++)
828 writel(0, fep->rx_queue[i]->bd.reg_desc_active);
831 static void fec_enet_enable_ring(struct net_device *ndev)
833 struct fec_enet_private *fep = netdev_priv(ndev);
834 struct fec_enet_priv_tx_q *txq;
835 struct fec_enet_priv_rx_q *rxq;
838 for (i = 0; i < fep->num_rx_queues; i++) {
839 rxq = fep->rx_queue[i];
840 writel(rxq->bd.dma, fep->hwp + FEC_R_DES_START(i));
841 writel(PKT_MAXBLR_SIZE, fep->hwp + FEC_R_BUFF_SIZE(i));
845 writel(RCMR_MATCHEN | RCMR_CMP(i),
846 fep->hwp + FEC_RCMR(i));
849 for (i = 0; i < fep->num_tx_queues; i++) {
850 txq = fep->tx_queue[i];
851 writel(txq->bd.dma, fep->hwp + FEC_X_DES_START(i));
855 writel(DMA_CLASS_EN | IDLE_SLOPE(i),
856 fep->hwp + FEC_DMA_CFG(i));
860 static void fec_enet_reset_skb(struct net_device *ndev)
862 struct fec_enet_private *fep = netdev_priv(ndev);
863 struct fec_enet_priv_tx_q *txq;
866 for (i = 0; i < fep->num_tx_queues; i++) {
867 txq = fep->tx_queue[i];
869 for (j = 0; j < txq->bd.ring_size; j++) {
870 if (txq->tx_skbuff[j]) {
871 dev_kfree_skb_any(txq->tx_skbuff[j]);
872 txq->tx_skbuff[j] = NULL;
879 * This function is called to start or restart the FEC during a link
880 * change, transmit timeout, or to reconfigure the FEC. The network
881 * packet processing for this device must be stopped before this call.
884 fec_restart(struct net_device *ndev)
886 struct fec_enet_private *fep = netdev_priv(ndev);
889 u32 rcntl = OPT_FRAME_SIZE | 0x04;
890 u32 ecntl = 0x2; /* ETHEREN */
892 /* Whack a reset. We should wait for this.
893 * For i.MX6SX SOC, enet use AXI bus, we use disable MAC
894 * instead of reset MAC itself.
896 if (fep->quirks & FEC_QUIRK_HAS_AVB) {
897 writel(0, fep->hwp + FEC_ECNTRL);
899 writel(1, fep->hwp + FEC_ECNTRL);
904 * enet-mac reset will reset mac address registers too,
905 * so need to reconfigure it.
907 if (fep->quirks & FEC_QUIRK_ENET_MAC) {
908 memcpy(&temp_mac, ndev->dev_addr, ETH_ALEN);
909 writel((__force u32)cpu_to_be32(temp_mac[0]),
910 fep->hwp + FEC_ADDR_LOW);
911 writel((__force u32)cpu_to_be32(temp_mac[1]),
912 fep->hwp + FEC_ADDR_HIGH);
915 /* Clear any outstanding interrupt. */
916 writel(0xffffffff, fep->hwp + FEC_IEVENT);
918 fec_enet_bd_init(ndev);
920 fec_enet_enable_ring(ndev);
922 /* Reset tx SKB buffers. */
923 fec_enet_reset_skb(ndev);
925 /* Enable MII mode */
926 if (fep->full_duplex == DUPLEX_FULL) {
928 writel(0x04, fep->hwp + FEC_X_CNTRL);
932 writel(0x0, fep->hwp + FEC_X_CNTRL);
936 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
938 #if !defined(CONFIG_M5272)
939 if (fep->quirks & FEC_QUIRK_HAS_RACC) {
940 /* set RX checksum */
941 val = readl(fep->hwp + FEC_RACC);
942 if (fep->csum_flags & FLAG_RX_CSUM_ENABLED)
943 val |= FEC_RACC_OPTIONS;
945 val &= ~FEC_RACC_OPTIONS;
946 writel(val, fep->hwp + FEC_RACC);
947 writel(PKT_MAXBUF_SIZE, fep->hwp + FEC_FTRL);
952 * The phy interface and speed need to get configured
953 * differently on enet-mac.
955 if (fep->quirks & FEC_QUIRK_ENET_MAC) {
956 /* Enable flow control and length check */
957 rcntl |= 0x40000000 | 0x00000020;
959 /* RGMII, RMII or MII */
960 if (fep->phy_interface == PHY_INTERFACE_MODE_RGMII ||
961 fep->phy_interface == PHY_INTERFACE_MODE_RGMII_ID ||
962 fep->phy_interface == PHY_INTERFACE_MODE_RGMII_RXID ||
963 fep->phy_interface == PHY_INTERFACE_MODE_RGMII_TXID)
965 else if (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
970 /* 1G, 100M or 10M */
972 if (ndev->phydev->speed == SPEED_1000)
974 else if (ndev->phydev->speed == SPEED_100)
980 #ifdef FEC_MIIGSK_ENR
981 if (fep->quirks & FEC_QUIRK_USE_GASKET) {
983 /* disable the gasket and wait */
984 writel(0, fep->hwp + FEC_MIIGSK_ENR);
985 while (readl(fep->hwp + FEC_MIIGSK_ENR) & 4)
989 * configure the gasket:
990 * RMII, 50 MHz, no loopback, no echo
991 * MII, 25 MHz, no loopback, no echo
993 cfgr = (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
994 ? BM_MIIGSK_CFGR_RMII : BM_MIIGSK_CFGR_MII;
995 if (ndev->phydev && ndev->phydev->speed == SPEED_10)
996 cfgr |= BM_MIIGSK_CFGR_FRCONT_10M;
997 writel(cfgr, fep->hwp + FEC_MIIGSK_CFGR);
999 /* re-enable the gasket */
1000 writel(2, fep->hwp + FEC_MIIGSK_ENR);
1005 #if !defined(CONFIG_M5272)
1006 /* enable pause frame*/
1007 if ((fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) ||
1008 ((fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) &&
1009 ndev->phydev && ndev->phydev->pause)) {
1010 rcntl |= FEC_ENET_FCE;
1012 /* set FIFO threshold parameter to reduce overrun */
1013 writel(FEC_ENET_RSEM_V, fep->hwp + FEC_R_FIFO_RSEM);
1014 writel(FEC_ENET_RSFL_V, fep->hwp + FEC_R_FIFO_RSFL);
1015 writel(FEC_ENET_RAEM_V, fep->hwp + FEC_R_FIFO_RAEM);
1016 writel(FEC_ENET_RAFL_V, fep->hwp + FEC_R_FIFO_RAFL);
1019 writel(FEC_ENET_OPD_V, fep->hwp + FEC_OPD);
1021 rcntl &= ~FEC_ENET_FCE;
1023 #endif /* !defined(CONFIG_M5272) */
1025 writel(rcntl, fep->hwp + FEC_R_CNTRL);
1027 /* Setup multicast filter. */
1028 set_multicast_list(ndev);
1029 #ifndef CONFIG_M5272
1030 writel(0, fep->hwp + FEC_HASH_TABLE_HIGH);
1031 writel(0, fep->hwp + FEC_HASH_TABLE_LOW);
1034 if (fep->quirks & FEC_QUIRK_ENET_MAC) {
1035 /* enable ENET endian swap */
1037 /* enable ENET store and forward mode */
1038 writel(1 << 8, fep->hwp + FEC_X_WMRK);
1041 if (fep->bufdesc_ex)
1044 #ifndef CONFIG_M5272
1045 /* Enable the MIB statistic event counters */
1046 writel(0 << 31, fep->hwp + FEC_MIB_CTRLSTAT);
1049 /* And last, enable the transmit and receive processing */
1050 writel(ecntl, fep->hwp + FEC_ECNTRL);
1051 fec_enet_active_rxring(ndev);
1053 if (fep->bufdesc_ex)
1054 fec_ptp_start_cyclecounter(ndev);
1056 /* Enable interrupts we wish to service */
1058 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1060 writel(FEC_ENET_MII, fep->hwp + FEC_IMASK);
1062 /* Init the interrupt coalescing */
1063 fec_enet_itr_coal_init(ndev);
1068 fec_stop(struct net_device *ndev)
1070 struct fec_enet_private *fep = netdev_priv(ndev);
1071 struct fec_platform_data *pdata = fep->pdev->dev.platform_data;
1072 u32 rmii_mode = readl(fep->hwp + FEC_R_CNTRL) & (1 << 8);
1075 /* We cannot expect a graceful transmit stop without link !!! */
1077 writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */
1079 if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA))
1080 netdev_err(ndev, "Graceful transmit stop did not complete!\n");
1083 /* Whack a reset. We should wait for this.
1084 * For i.MX6SX SOC, enet use AXI bus, we use disable MAC
1085 * instead of reset MAC itself.
1087 if (!(fep->wol_flag & FEC_WOL_FLAG_SLEEP_ON)) {
1088 if (fep->quirks & FEC_QUIRK_HAS_AVB) {
1089 writel(0, fep->hwp + FEC_ECNTRL);
1091 writel(1, fep->hwp + FEC_ECNTRL);
1094 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1096 writel(FEC_DEFAULT_IMASK | FEC_ENET_WAKEUP, fep->hwp + FEC_IMASK);
1097 val = readl(fep->hwp + FEC_ECNTRL);
1098 val |= (FEC_ECR_MAGICEN | FEC_ECR_SLEEP);
1099 writel(val, fep->hwp + FEC_ECNTRL);
1101 if (pdata && pdata->sleep_mode_enable)
1102 pdata->sleep_mode_enable(true);
1104 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
1106 /* We have to keep ENET enabled to have MII interrupt stay working */
1107 if (fep->quirks & FEC_QUIRK_ENET_MAC &&
1108 !(fep->wol_flag & FEC_WOL_FLAG_SLEEP_ON)) {
1109 writel(2, fep->hwp + FEC_ECNTRL);
1110 writel(rmii_mode, fep->hwp + FEC_R_CNTRL);
1116 fec_timeout(struct net_device *ndev)
1118 struct fec_enet_private *fep = netdev_priv(ndev);
1122 ndev->stats.tx_errors++;
1124 schedule_work(&fep->tx_timeout_work);
1127 static void fec_enet_timeout_work(struct work_struct *work)
1129 struct fec_enet_private *fep =
1130 container_of(work, struct fec_enet_private, tx_timeout_work);
1131 struct net_device *ndev = fep->netdev;
1134 if (netif_device_present(ndev) || netif_running(ndev)) {
1135 napi_disable(&fep->napi);
1136 netif_tx_lock_bh(ndev);
1138 netif_wake_queue(ndev);
1139 netif_tx_unlock_bh(ndev);
1140 napi_enable(&fep->napi);
1146 fec_enet_hwtstamp(struct fec_enet_private *fep, unsigned ts,
1147 struct skb_shared_hwtstamps *hwtstamps)
1149 unsigned long flags;
1152 spin_lock_irqsave(&fep->tmreg_lock, flags);
1153 ns = timecounter_cyc2time(&fep->tc, ts);
1154 spin_unlock_irqrestore(&fep->tmreg_lock, flags);
1156 memset(hwtstamps, 0, sizeof(*hwtstamps));
1157 hwtstamps->hwtstamp = ns_to_ktime(ns);
1161 fec_enet_tx_queue(struct net_device *ndev, u16 queue_id)
1163 struct fec_enet_private *fep;
1164 struct bufdesc *bdp;
1165 unsigned short status;
1166 struct sk_buff *skb;
1167 struct fec_enet_priv_tx_q *txq;
1168 struct netdev_queue *nq;
1172 fep = netdev_priv(ndev);
1174 queue_id = FEC_ENET_GET_QUQUE(queue_id);
1176 txq = fep->tx_queue[queue_id];
1177 /* get next bdp of dirty_tx */
1178 nq = netdev_get_tx_queue(ndev, queue_id);
1179 bdp = txq->dirty_tx;
1181 /* get next bdp of dirty_tx */
1182 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
1184 while (bdp != READ_ONCE(txq->bd.cur)) {
1185 /* Order the load of bd.cur and cbd_sc */
1187 status = fec16_to_cpu(READ_ONCE(bdp->cbd_sc));
1188 if (status & BD_ENET_TX_READY)
1191 index = fec_enet_get_bd_index(bdp, &txq->bd);
1193 skb = txq->tx_skbuff[index];
1194 txq->tx_skbuff[index] = NULL;
1195 if (!IS_TSO_HEADER(txq, fec32_to_cpu(bdp->cbd_bufaddr)))
1196 dma_unmap_single(&fep->pdev->dev,
1197 fec32_to_cpu(bdp->cbd_bufaddr),
1198 fec16_to_cpu(bdp->cbd_datlen),
1200 bdp->cbd_bufaddr = cpu_to_fec32(0);
1202 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
1206 /* Check for errors. */
1207 if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC |
1208 BD_ENET_TX_RL | BD_ENET_TX_UN |
1210 ndev->stats.tx_errors++;
1211 if (status & BD_ENET_TX_HB) /* No heartbeat */
1212 ndev->stats.tx_heartbeat_errors++;
1213 if (status & BD_ENET_TX_LC) /* Late collision */
1214 ndev->stats.tx_window_errors++;
1215 if (status & BD_ENET_TX_RL) /* Retrans limit */
1216 ndev->stats.tx_aborted_errors++;
1217 if (status & BD_ENET_TX_UN) /* Underrun */
1218 ndev->stats.tx_fifo_errors++;
1219 if (status & BD_ENET_TX_CSL) /* Carrier lost */
1220 ndev->stats.tx_carrier_errors++;
1222 ndev->stats.tx_packets++;
1223 ndev->stats.tx_bytes += skb->len;
1226 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) &&
1228 struct skb_shared_hwtstamps shhwtstamps;
1229 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
1231 fec_enet_hwtstamp(fep, fec32_to_cpu(ebdp->ts), &shhwtstamps);
1232 skb_tstamp_tx(skb, &shhwtstamps);
1235 /* Deferred means some collisions occurred during transmit,
1236 * but we eventually sent the packet OK.
1238 if (status & BD_ENET_TX_DEF)
1239 ndev->stats.collisions++;
1241 /* Free the sk buffer associated with this last transmit */
1242 dev_kfree_skb_any(skb);
1244 /* Make sure the update to bdp and tx_skbuff are performed
1248 txq->dirty_tx = bdp;
1250 /* Update pointer to next buffer descriptor to be transmitted */
1251 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
1253 /* Since we have freed up a buffer, the ring is no longer full
1255 if (netif_queue_stopped(ndev)) {
1256 entries_free = fec_enet_get_free_txdesc_num(txq);
1257 if (entries_free >= txq->tx_wake_threshold)
1258 netif_tx_wake_queue(nq);
1262 /* ERR006538: Keep the transmitter going */
1263 if (bdp != txq->bd.cur &&
1264 readl(txq->bd.reg_desc_active) == 0)
1265 writel(0, txq->bd.reg_desc_active);
1269 fec_enet_tx(struct net_device *ndev)
1271 struct fec_enet_private *fep = netdev_priv(ndev);
1273 /* First process class A queue, then Class B and Best Effort queue */
1274 for_each_set_bit(queue_id, &fep->work_tx, FEC_ENET_MAX_TX_QS) {
1275 clear_bit(queue_id, &fep->work_tx);
1276 fec_enet_tx_queue(ndev, queue_id);
1282 fec_enet_new_rxbdp(struct net_device *ndev, struct bufdesc *bdp, struct sk_buff *skb)
1284 struct fec_enet_private *fep = netdev_priv(ndev);
1287 off = ((unsigned long)skb->data) & fep->rx_align;
1289 skb_reserve(skb, fep->rx_align + 1 - off);
1291 bdp->cbd_bufaddr = cpu_to_fec32(dma_map_single(&fep->pdev->dev, skb->data, FEC_ENET_RX_FRSIZE - fep->rx_align, DMA_FROM_DEVICE));
1292 if (dma_mapping_error(&fep->pdev->dev, fec32_to_cpu(bdp->cbd_bufaddr))) {
1293 if (net_ratelimit())
1294 netdev_err(ndev, "Rx DMA memory map failed\n");
1301 static bool fec_enet_copybreak(struct net_device *ndev, struct sk_buff **skb,
1302 struct bufdesc *bdp, u32 length, bool swap)
1304 struct fec_enet_private *fep = netdev_priv(ndev);
1305 struct sk_buff *new_skb;
1307 if (length > fep->rx_copybreak)
1310 new_skb = netdev_alloc_skb(ndev, length);
1314 dma_sync_single_for_cpu(&fep->pdev->dev,
1315 fec32_to_cpu(bdp->cbd_bufaddr),
1316 FEC_ENET_RX_FRSIZE - fep->rx_align,
1319 memcpy(new_skb->data, (*skb)->data, length);
1321 swap_buffer2(new_skb->data, (*skb)->data, length);
1327 /* During a receive, the bd_rx.cur points to the current incoming buffer.
1328 * When we update through the ring, if the next incoming buffer has
1329 * not been given to the system, we just set the empty indicator,
1330 * effectively tossing the packet.
1333 fec_enet_rx_queue(struct net_device *ndev, int budget, u16 queue_id)
1335 struct fec_enet_private *fep = netdev_priv(ndev);
1336 struct fec_enet_priv_rx_q *rxq;
1337 struct bufdesc *bdp;
1338 unsigned short status;
1339 struct sk_buff *skb_new = NULL;
1340 struct sk_buff *skb;
1343 int pkt_received = 0;
1344 struct bufdesc_ex *ebdp = NULL;
1345 bool vlan_packet_rcvd = false;
1349 bool need_swap = fep->quirks & FEC_QUIRK_SWAP_FRAME;
1354 queue_id = FEC_ENET_GET_QUQUE(queue_id);
1355 rxq = fep->rx_queue[queue_id];
1357 /* First, grab all of the stats for the incoming packet.
1358 * These get messed up if we get called due to a busy condition.
1362 while (!((status = fec16_to_cpu(bdp->cbd_sc)) & BD_ENET_RX_EMPTY)) {
1364 if (pkt_received >= budget)
1368 writel(FEC_ENET_RXF, fep->hwp + FEC_IEVENT);
1370 /* Check for errors. */
1371 status ^= BD_ENET_RX_LAST;
1372 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
1373 BD_ENET_RX_CR | BD_ENET_RX_OV | BD_ENET_RX_LAST |
1375 ndev->stats.rx_errors++;
1376 if (status & BD_ENET_RX_OV) {
1378 ndev->stats.rx_fifo_errors++;
1379 goto rx_processing_done;
1381 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH
1382 | BD_ENET_RX_LAST)) {
1383 /* Frame too long or too short. */
1384 ndev->stats.rx_length_errors++;
1385 if (status & BD_ENET_RX_LAST)
1386 netdev_err(ndev, "rcv is not +last\n");
1388 if (status & BD_ENET_RX_CR) /* CRC Error */
1389 ndev->stats.rx_crc_errors++;
1390 /* Report late collisions as a frame error. */
1391 if (status & (BD_ENET_RX_NO | BD_ENET_RX_CL))
1392 ndev->stats.rx_frame_errors++;
1393 goto rx_processing_done;
1396 /* Process the incoming frame. */
1397 ndev->stats.rx_packets++;
1398 pkt_len = fec16_to_cpu(bdp->cbd_datlen);
1399 ndev->stats.rx_bytes += pkt_len;
1401 index = fec_enet_get_bd_index(bdp, &rxq->bd);
1402 skb = rxq->rx_skbuff[index];
1404 /* The packet length includes FCS, but we don't want to
1405 * include that when passing upstream as it messes up
1406 * bridging applications.
1408 is_copybreak = fec_enet_copybreak(ndev, &skb, bdp, pkt_len - 4,
1410 if (!is_copybreak) {
1411 skb_new = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
1412 if (unlikely(!skb_new)) {
1413 ndev->stats.rx_dropped++;
1414 goto rx_processing_done;
1416 dma_unmap_single(&fep->pdev->dev,
1417 fec32_to_cpu(bdp->cbd_bufaddr),
1418 FEC_ENET_RX_FRSIZE - fep->rx_align,
1422 prefetch(skb->data - NET_IP_ALIGN);
1423 skb_put(skb, pkt_len - 4);
1425 if (!is_copybreak && need_swap)
1426 swap_buffer(data, pkt_len);
1428 /* Extract the enhanced buffer descriptor */
1430 if (fep->bufdesc_ex)
1431 ebdp = (struct bufdesc_ex *)bdp;
1433 /* If this is a VLAN packet remove the VLAN Tag */
1434 vlan_packet_rcvd = false;
1435 if ((ndev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
1437 (ebdp->cbd_esc & cpu_to_fec32(BD_ENET_RX_VLAN))) {
1438 /* Push and remove the vlan tag */
1439 struct vlan_hdr *vlan_header =
1440 (struct vlan_hdr *) (data + ETH_HLEN);
1441 vlan_tag = ntohs(vlan_header->h_vlan_TCI);
1443 vlan_packet_rcvd = true;
1445 memmove(skb->data + VLAN_HLEN, data, ETH_ALEN * 2);
1446 skb_pull(skb, VLAN_HLEN);
1449 skb->protocol = eth_type_trans(skb, ndev);
1451 /* Get receive timestamp from the skb */
1452 if (fep->hwts_rx_en && fep->bufdesc_ex)
1453 fec_enet_hwtstamp(fep, fec32_to_cpu(ebdp->ts),
1454 skb_hwtstamps(skb));
1456 if (fep->bufdesc_ex &&
1457 (fep->csum_flags & FLAG_RX_CSUM_ENABLED)) {
1458 if (!(ebdp->cbd_esc & cpu_to_fec32(FLAG_RX_CSUM_ERROR))) {
1459 /* don't check it */
1460 skb->ip_summed = CHECKSUM_UNNECESSARY;
1462 skb_checksum_none_assert(skb);
1466 /* Handle received VLAN packets */
1467 if (vlan_packet_rcvd)
1468 __vlan_hwaccel_put_tag(skb,
1472 napi_gro_receive(&fep->napi, skb);
1475 dma_sync_single_for_device(&fep->pdev->dev,
1476 fec32_to_cpu(bdp->cbd_bufaddr),
1477 FEC_ENET_RX_FRSIZE - fep->rx_align,
1480 rxq->rx_skbuff[index] = skb_new;
1481 fec_enet_new_rxbdp(ndev, bdp, skb_new);
1485 /* Clear the status flags for this buffer */
1486 status &= ~BD_ENET_RX_STATS;
1488 /* Mark the buffer empty */
1489 status |= BD_ENET_RX_EMPTY;
1491 if (fep->bufdesc_ex) {
1492 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
1494 ebdp->cbd_esc = cpu_to_fec32(BD_ENET_RX_INT);
1498 /* Make sure the updates to rest of the descriptor are
1499 * performed before transferring ownership.
1502 bdp->cbd_sc = cpu_to_fec16(status);
1504 /* Update BD pointer to next entry */
1505 bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
1507 /* Doing this here will keep the FEC running while we process
1508 * incoming frames. On a heavily loaded network, we should be
1509 * able to keep up at the expense of system resources.
1511 writel(0, rxq->bd.reg_desc_active);
1514 return pkt_received;
1518 fec_enet_rx(struct net_device *ndev, int budget)
1520 int pkt_received = 0;
1522 struct fec_enet_private *fep = netdev_priv(ndev);
1524 for_each_set_bit(queue_id, &fep->work_rx, FEC_ENET_MAX_RX_QS) {
1527 ret = fec_enet_rx_queue(ndev,
1528 budget - pkt_received, queue_id);
1530 if (ret < budget - pkt_received)
1531 clear_bit(queue_id, &fep->work_rx);
1533 pkt_received += ret;
1535 return pkt_received;
1539 fec_enet_collect_events(struct fec_enet_private *fep, uint int_events)
1541 if (int_events == 0)
1544 if (int_events & FEC_ENET_RXF)
1545 fep->work_rx |= (1 << 2);
1546 if (int_events & FEC_ENET_RXF_1)
1547 fep->work_rx |= (1 << 0);
1548 if (int_events & FEC_ENET_RXF_2)
1549 fep->work_rx |= (1 << 1);
1551 if (int_events & FEC_ENET_TXF)
1552 fep->work_tx |= (1 << 2);
1553 if (int_events & FEC_ENET_TXF_1)
1554 fep->work_tx |= (1 << 0);
1555 if (int_events & FEC_ENET_TXF_2)
1556 fep->work_tx |= (1 << 1);
1562 fec_enet_interrupt(int irq, void *dev_id)
1564 struct net_device *ndev = dev_id;
1565 struct fec_enet_private *fep = netdev_priv(ndev);
1567 irqreturn_t ret = IRQ_NONE;
1569 int_events = readl(fep->hwp + FEC_IEVENT);
1570 writel(int_events, fep->hwp + FEC_IEVENT);
1571 fec_enet_collect_events(fep, int_events);
1573 if ((fep->work_tx || fep->work_rx) && fep->link) {
1576 if (napi_schedule_prep(&fep->napi)) {
1577 /* Disable the NAPI interrupts */
1578 writel(FEC_NAPI_IMASK, fep->hwp + FEC_IMASK);
1579 __napi_schedule(&fep->napi);
1583 if (int_events & FEC_ENET_MII) {
1585 complete(&fep->mdio_done);
1589 fec_ptp_check_pps_event(fep);
1594 static int fec_enet_rx_napi(struct napi_struct *napi, int budget)
1596 struct net_device *ndev = napi->dev;
1597 struct fec_enet_private *fep = netdev_priv(ndev);
1600 pkts = fec_enet_rx(ndev, budget);
1604 if (pkts < budget) {
1605 napi_complete(napi);
1606 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1611 /* ------------------------------------------------------------------------- */
1612 static void fec_get_mac(struct net_device *ndev)
1614 struct fec_enet_private *fep = netdev_priv(ndev);
1615 struct fec_platform_data *pdata = dev_get_platdata(&fep->pdev->dev);
1616 unsigned char *iap, tmpaddr[ETH_ALEN];
1619 * try to get mac address in following order:
1621 * 1) module parameter via kernel command line in form
1622 * fec.macaddr=0x00,0x04,0x9f,0x01,0x30,0xe0
1627 * 2) from device tree data
1629 if (!is_valid_ether_addr(iap)) {
1630 struct device_node *np = fep->pdev->dev.of_node;
1632 const char *mac = of_get_mac_address(np);
1634 iap = (unsigned char *) mac;
1639 * 3) from flash or fuse (via platform data)
1641 if (!is_valid_ether_addr(iap)) {
1644 iap = (unsigned char *)FEC_FLASHMAC;
1647 iap = (unsigned char *)&pdata->mac;
1652 * 4) FEC mac registers set by bootloader
1654 if (!is_valid_ether_addr(iap)) {
1655 *((__be32 *) &tmpaddr[0]) =
1656 cpu_to_be32(readl(fep->hwp + FEC_ADDR_LOW));
1657 *((__be16 *) &tmpaddr[4]) =
1658 cpu_to_be16(readl(fep->hwp + FEC_ADDR_HIGH) >> 16);
1663 * 5) random mac address
1665 if (!is_valid_ether_addr(iap)) {
1666 /* Report it and use a random ethernet address instead */
1667 netdev_err(ndev, "Invalid MAC address: %pM\n", iap);
1668 eth_hw_addr_random(ndev);
1669 netdev_info(ndev, "Using random MAC address: %pM\n",
1674 memcpy(ndev->dev_addr, iap, ETH_ALEN);
1676 /* Adjust MAC if using macaddr */
1678 ndev->dev_addr[ETH_ALEN-1] = macaddr[ETH_ALEN-1] + fep->dev_id;
1681 /* ------------------------------------------------------------------------- */
1686 static void fec_enet_adjust_link(struct net_device *ndev)
1688 struct fec_enet_private *fep = netdev_priv(ndev);
1689 struct phy_device *phy_dev = ndev->phydev;
1690 int status_change = 0;
1692 /* Prevent a state halted on mii error */
1693 if (fep->mii_timeout && phy_dev->state == PHY_HALTED) {
1694 phy_dev->state = PHY_RESUMING;
1699 * If the netdev is down, or is going down, we're not interested
1700 * in link state events, so just mark our idea of the link as down
1701 * and ignore the event.
1703 if (!netif_running(ndev) || !netif_device_present(ndev)) {
1705 } else if (phy_dev->link) {
1707 fep->link = phy_dev->link;
1711 if (fep->full_duplex != phy_dev->duplex) {
1712 fep->full_duplex = phy_dev->duplex;
1716 if (phy_dev->speed != fep->speed) {
1717 fep->speed = phy_dev->speed;
1721 /* if any of the above changed restart the FEC */
1722 if (status_change) {
1723 napi_disable(&fep->napi);
1724 netif_tx_lock_bh(ndev);
1726 netif_wake_queue(ndev);
1727 netif_tx_unlock_bh(ndev);
1728 napi_enable(&fep->napi);
1732 napi_disable(&fep->napi);
1733 netif_tx_lock_bh(ndev);
1735 netif_tx_unlock_bh(ndev);
1736 napi_enable(&fep->napi);
1737 fep->link = phy_dev->link;
1743 phy_print_status(phy_dev);
1746 static int fec_enet_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
1748 struct fec_enet_private *fep = bus->priv;
1749 struct device *dev = &fep->pdev->dev;
1750 unsigned long time_left;
1753 ret = pm_runtime_get_sync(dev);
1757 fep->mii_timeout = 0;
1758 reinit_completion(&fep->mdio_done);
1760 /* start a read op */
1761 writel(FEC_MMFR_ST | FEC_MMFR_OP_READ |
1762 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1763 FEC_MMFR_TA, fep->hwp + FEC_MII_DATA);
1765 /* wait for end of transfer */
1766 time_left = wait_for_completion_timeout(&fep->mdio_done,
1767 usecs_to_jiffies(FEC_MII_TIMEOUT));
1768 if (time_left == 0) {
1769 fep->mii_timeout = 1;
1770 netdev_err(fep->netdev, "MDIO read timeout\n");
1775 ret = FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA));
1778 pm_runtime_mark_last_busy(dev);
1779 pm_runtime_put_autosuspend(dev);
1784 static int fec_enet_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
1787 struct fec_enet_private *fep = bus->priv;
1788 struct device *dev = &fep->pdev->dev;
1789 unsigned long time_left;
1792 ret = pm_runtime_get_sync(dev);
1798 fep->mii_timeout = 0;
1799 reinit_completion(&fep->mdio_done);
1801 /* start a write op */
1802 writel(FEC_MMFR_ST | FEC_MMFR_OP_WRITE |
1803 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1804 FEC_MMFR_TA | FEC_MMFR_DATA(value),
1805 fep->hwp + FEC_MII_DATA);
1807 /* wait for end of transfer */
1808 time_left = wait_for_completion_timeout(&fep->mdio_done,
1809 usecs_to_jiffies(FEC_MII_TIMEOUT));
1810 if (time_left == 0) {
1811 fep->mii_timeout = 1;
1812 netdev_err(fep->netdev, "MDIO write timeout\n");
1816 pm_runtime_mark_last_busy(dev);
1817 pm_runtime_put_autosuspend(dev);
1822 static int fec_enet_clk_enable(struct net_device *ndev, bool enable)
1824 struct fec_enet_private *fep = netdev_priv(ndev);
1828 ret = clk_prepare_enable(fep->clk_ahb);
1831 if (fep->clk_enet_out) {
1832 ret = clk_prepare_enable(fep->clk_enet_out);
1834 goto failed_clk_enet_out;
1837 mutex_lock(&fep->ptp_clk_mutex);
1838 ret = clk_prepare_enable(fep->clk_ptp);
1840 mutex_unlock(&fep->ptp_clk_mutex);
1841 goto failed_clk_ptp;
1843 fep->ptp_clk_on = true;
1845 mutex_unlock(&fep->ptp_clk_mutex);
1848 ret = clk_prepare_enable(fep->clk_ref);
1850 goto failed_clk_ref;
1853 clk_disable_unprepare(fep->clk_ahb);
1854 if (fep->clk_enet_out)
1855 clk_disable_unprepare(fep->clk_enet_out);
1857 mutex_lock(&fep->ptp_clk_mutex);
1858 clk_disable_unprepare(fep->clk_ptp);
1859 fep->ptp_clk_on = false;
1860 mutex_unlock(&fep->ptp_clk_mutex);
1863 clk_disable_unprepare(fep->clk_ref);
1870 clk_disable_unprepare(fep->clk_ref);
1872 if (fep->clk_enet_out)
1873 clk_disable_unprepare(fep->clk_enet_out);
1874 failed_clk_enet_out:
1875 clk_disable_unprepare(fep->clk_ahb);
1880 static int fec_enet_mii_probe(struct net_device *ndev)
1882 struct fec_enet_private *fep = netdev_priv(ndev);
1883 struct phy_device *phy_dev = NULL;
1884 char mdio_bus_id[MII_BUS_ID_SIZE];
1885 char phy_name[MII_BUS_ID_SIZE + 3];
1887 int dev_id = fep->dev_id;
1889 if (fep->phy_node) {
1890 phy_dev = of_phy_connect(ndev, fep->phy_node,
1891 &fec_enet_adjust_link, 0,
1892 fep->phy_interface);
1896 /* check for attached phy */
1897 for (phy_id = 0; (phy_id < PHY_MAX_ADDR); phy_id++) {
1898 if (!mdiobus_is_registered_device(fep->mii_bus, phy_id))
1902 strlcpy(mdio_bus_id, fep->mii_bus->id, MII_BUS_ID_SIZE);
1906 if (phy_id >= PHY_MAX_ADDR) {
1907 netdev_info(ndev, "no PHY, assuming direct connection to switch\n");
1908 strlcpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE);
1912 snprintf(phy_name, sizeof(phy_name),
1913 PHY_ID_FMT, mdio_bus_id, phy_id);
1914 phy_dev = phy_connect(ndev, phy_name, &fec_enet_adjust_link,
1915 fep->phy_interface);
1918 if (IS_ERR(phy_dev)) {
1919 netdev_err(ndev, "could not attach to PHY\n");
1920 return PTR_ERR(phy_dev);
1923 /* mask with MAC supported features */
1924 if (fep->quirks & FEC_QUIRK_HAS_GBIT) {
1925 phy_dev->supported &= PHY_GBIT_FEATURES;
1926 phy_dev->supported &= ~SUPPORTED_1000baseT_Half;
1927 #if !defined(CONFIG_M5272)
1928 phy_dev->supported |= SUPPORTED_Pause;
1932 phy_dev->supported &= PHY_BASIC_FEATURES;
1934 phy_dev->advertising = phy_dev->supported;
1937 fep->full_duplex = 0;
1939 phy_attached_info(phy_dev);
1944 static int fec_enet_mii_init(struct platform_device *pdev)
1946 static struct mii_bus *fec0_mii_bus;
1947 struct net_device *ndev = platform_get_drvdata(pdev);
1948 struct fec_enet_private *fep = netdev_priv(ndev);
1949 struct device_node *node;
1951 u32 mii_speed, holdtime;
1954 * The i.MX28 dual fec interfaces are not equal.
1955 * Here are the differences:
1957 * - fec0 supports MII & RMII modes while fec1 only supports RMII
1958 * - fec0 acts as the 1588 time master while fec1 is slave
1959 * - external phys can only be configured by fec0
1961 * That is to say fec1 can not work independently. It only works
1962 * when fec0 is working. The reason behind this design is that the
1963 * second interface is added primarily for Switch mode.
1965 * Because of the last point above, both phys are attached on fec0
1966 * mdio interface in board design, and need to be configured by
1969 if ((fep->quirks & FEC_QUIRK_SINGLE_MDIO) && fep->dev_id > 0) {
1970 /* fec1 uses fec0 mii_bus */
1971 if (mii_cnt && fec0_mii_bus) {
1972 fep->mii_bus = fec0_mii_bus;
1979 fep->mii_timeout = 0;
1982 * Set MII speed to 2.5 MHz (= clk_get_rate() / 2 * phy_speed)
1984 * The formula for FEC MDC is 'ref_freq / (MII_SPEED x 2)' while
1985 * for ENET-MAC is 'ref_freq / ((MII_SPEED + 1) x 2)'. The i.MX28
1986 * Reference Manual has an error on this, and gets fixed on i.MX6Q
1989 mii_speed = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), 5000000);
1990 if (fep->quirks & FEC_QUIRK_ENET_MAC)
1992 if (mii_speed > 63) {
1994 "fec clock (%lu) to fast to get right mii speed\n",
1995 clk_get_rate(fep->clk_ipg));
2001 * The i.MX28 and i.MX6 types have another filed in the MSCR (aka
2002 * MII_SPEED) register that defines the MDIO output hold time. Earlier
2003 * versions are RAZ there, so just ignore the difference and write the
2005 * The minimal hold time according to IEE802.3 (clause 22) is 10 ns.
2006 * HOLDTIME + 1 is the number of clk cycles the fec is holding the
2008 * The HOLDTIME bitfield takes values between 0 and 7 (inclusive).
2009 * Given that ceil(clkrate / 5000000) <= 64, the calculation for
2010 * holdtime cannot result in a value greater than 3.
2012 holdtime = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), 100000000) - 1;
2014 fep->phy_speed = mii_speed << 1 | holdtime << 8;
2016 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
2018 fep->mii_bus = mdiobus_alloc();
2019 if (fep->mii_bus == NULL) {
2024 fep->mii_bus->name = "fec_enet_mii_bus";
2025 fep->mii_bus->read = fec_enet_mdio_read;
2026 fep->mii_bus->write = fec_enet_mdio_write;
2027 snprintf(fep->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
2028 pdev->name, fep->dev_id + 1);
2029 fep->mii_bus->priv = fep;
2030 fep->mii_bus->parent = &pdev->dev;
2032 node = of_get_child_by_name(pdev->dev.of_node, "mdio");
2034 err = of_mdiobus_register(fep->mii_bus, node);
2037 err = mdiobus_register(fep->mii_bus);
2041 goto err_out_free_mdiobus;
2045 /* save fec0 mii_bus */
2046 if (fep->quirks & FEC_QUIRK_SINGLE_MDIO)
2047 fec0_mii_bus = fep->mii_bus;
2051 err_out_free_mdiobus:
2052 mdiobus_free(fep->mii_bus);
2057 static void fec_enet_mii_remove(struct fec_enet_private *fep)
2059 if (--mii_cnt == 0) {
2060 mdiobus_unregister(fep->mii_bus);
2061 mdiobus_free(fep->mii_bus);
2065 static void fec_enet_get_drvinfo(struct net_device *ndev,
2066 struct ethtool_drvinfo *info)
2068 struct fec_enet_private *fep = netdev_priv(ndev);
2070 strlcpy(info->driver, fep->pdev->dev.driver->name,
2071 sizeof(info->driver));
2072 strlcpy(info->version, "Revision: 1.0", sizeof(info->version));
2073 strlcpy(info->bus_info, dev_name(&ndev->dev), sizeof(info->bus_info));
2076 static int fec_enet_get_regs_len(struct net_device *ndev)
2078 struct fec_enet_private *fep = netdev_priv(ndev);
2082 r = platform_get_resource(fep->pdev, IORESOURCE_MEM, 0);
2084 s = resource_size(r);
2089 /* List of registers that can be safety be read to dump them with ethtool */
2090 #if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
2091 defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM)
2092 static u32 fec_enet_register_offset[] = {
2093 FEC_IEVENT, FEC_IMASK, FEC_R_DES_ACTIVE_0, FEC_X_DES_ACTIVE_0,
2094 FEC_ECNTRL, FEC_MII_DATA, FEC_MII_SPEED, FEC_MIB_CTRLSTAT, FEC_R_CNTRL,
2095 FEC_X_CNTRL, FEC_ADDR_LOW, FEC_ADDR_HIGH, FEC_OPD, FEC_TXIC0, FEC_TXIC1,
2096 FEC_TXIC2, FEC_RXIC0, FEC_RXIC1, FEC_RXIC2, FEC_HASH_TABLE_HIGH,
2097 FEC_HASH_TABLE_LOW, FEC_GRP_HASH_TABLE_HIGH, FEC_GRP_HASH_TABLE_LOW,
2098 FEC_X_WMRK, FEC_R_BOUND, FEC_R_FSTART, FEC_R_DES_START_1,
2099 FEC_X_DES_START_1, FEC_R_BUFF_SIZE_1, FEC_R_DES_START_2,
2100 FEC_X_DES_START_2, FEC_R_BUFF_SIZE_2, FEC_R_DES_START_0,
2101 FEC_X_DES_START_0, FEC_R_BUFF_SIZE_0, FEC_R_FIFO_RSFL, FEC_R_FIFO_RSEM,
2102 FEC_R_FIFO_RAEM, FEC_R_FIFO_RAFL, FEC_RACC, FEC_RCMR_1, FEC_RCMR_2,
2103 FEC_DMA_CFG_1, FEC_DMA_CFG_2, FEC_R_DES_ACTIVE_1, FEC_X_DES_ACTIVE_1,
2104 FEC_R_DES_ACTIVE_2, FEC_X_DES_ACTIVE_2, FEC_QOS_SCHEME,
2105 RMON_T_DROP, RMON_T_PACKETS, RMON_T_BC_PKT, RMON_T_MC_PKT,
2106 RMON_T_CRC_ALIGN, RMON_T_UNDERSIZE, RMON_T_OVERSIZE, RMON_T_FRAG,
2107 RMON_T_JAB, RMON_T_COL, RMON_T_P64, RMON_T_P65TO127, RMON_T_P128TO255,
2108 RMON_T_P256TO511, RMON_T_P512TO1023, RMON_T_P1024TO2047,
2109 RMON_T_P_GTE2048, RMON_T_OCTETS,
2110 IEEE_T_DROP, IEEE_T_FRAME_OK, IEEE_T_1COL, IEEE_T_MCOL, IEEE_T_DEF,
2111 IEEE_T_LCOL, IEEE_T_EXCOL, IEEE_T_MACERR, IEEE_T_CSERR, IEEE_T_SQE,
2112 IEEE_T_FDXFC, IEEE_T_OCTETS_OK,
2113 RMON_R_PACKETS, RMON_R_BC_PKT, RMON_R_MC_PKT, RMON_R_CRC_ALIGN,
2114 RMON_R_UNDERSIZE, RMON_R_OVERSIZE, RMON_R_FRAG, RMON_R_JAB,
2115 RMON_R_RESVD_O, RMON_R_P64, RMON_R_P65TO127, RMON_R_P128TO255,
2116 RMON_R_P256TO511, RMON_R_P512TO1023, RMON_R_P1024TO2047,
2117 RMON_R_P_GTE2048, RMON_R_OCTETS,
2118 IEEE_R_DROP, IEEE_R_FRAME_OK, IEEE_R_CRC, IEEE_R_ALIGN, IEEE_R_MACERR,
2119 IEEE_R_FDXFC, IEEE_R_OCTETS_OK
2122 static u32 fec_enet_register_offset[] = {
2123 FEC_ECNTRL, FEC_IEVENT, FEC_IMASK, FEC_IVEC, FEC_R_DES_ACTIVE_0,
2124 FEC_R_DES_ACTIVE_1, FEC_R_DES_ACTIVE_2, FEC_X_DES_ACTIVE_0,
2125 FEC_X_DES_ACTIVE_1, FEC_X_DES_ACTIVE_2, FEC_MII_DATA, FEC_MII_SPEED,
2126 FEC_R_BOUND, FEC_R_FSTART, FEC_X_WMRK, FEC_X_FSTART, FEC_R_CNTRL,
2127 FEC_MAX_FRM_LEN, FEC_X_CNTRL, FEC_ADDR_LOW, FEC_ADDR_HIGH,
2128 FEC_GRP_HASH_TABLE_HIGH, FEC_GRP_HASH_TABLE_LOW, FEC_R_DES_START_0,
2129 FEC_R_DES_START_1, FEC_R_DES_START_2, FEC_X_DES_START_0,
2130 FEC_X_DES_START_1, FEC_X_DES_START_2, FEC_R_BUFF_SIZE_0,
2131 FEC_R_BUFF_SIZE_1, FEC_R_BUFF_SIZE_2
2135 static void fec_enet_get_regs(struct net_device *ndev,
2136 struct ethtool_regs *regs, void *regbuf)
2138 struct fec_enet_private *fep = netdev_priv(ndev);
2139 u32 __iomem *theregs = (u32 __iomem *)fep->hwp;
2140 u32 *buf = (u32 *)regbuf;
2143 memset(buf, 0, regs->len);
2145 for (i = 0; i < ARRAY_SIZE(fec_enet_register_offset); i++) {
2146 off = fec_enet_register_offset[i] / 4;
2147 buf[off] = readl(&theregs[off]);
2151 static int fec_enet_get_ts_info(struct net_device *ndev,
2152 struct ethtool_ts_info *info)
2154 struct fec_enet_private *fep = netdev_priv(ndev);
2156 if (fep->bufdesc_ex) {
2158 info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
2159 SOF_TIMESTAMPING_RX_SOFTWARE |
2160 SOF_TIMESTAMPING_SOFTWARE |
2161 SOF_TIMESTAMPING_TX_HARDWARE |
2162 SOF_TIMESTAMPING_RX_HARDWARE |
2163 SOF_TIMESTAMPING_RAW_HARDWARE;
2165 info->phc_index = ptp_clock_index(fep->ptp_clock);
2167 info->phc_index = -1;
2169 info->tx_types = (1 << HWTSTAMP_TX_OFF) |
2170 (1 << HWTSTAMP_TX_ON);
2172 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
2173 (1 << HWTSTAMP_FILTER_ALL);
2176 return ethtool_op_get_ts_info(ndev, info);
2180 #if !defined(CONFIG_M5272)
2182 static void fec_enet_get_pauseparam(struct net_device *ndev,
2183 struct ethtool_pauseparam *pause)
2185 struct fec_enet_private *fep = netdev_priv(ndev);
2187 pause->autoneg = (fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) != 0;
2188 pause->tx_pause = (fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) != 0;
2189 pause->rx_pause = pause->tx_pause;
2192 static int fec_enet_set_pauseparam(struct net_device *ndev,
2193 struct ethtool_pauseparam *pause)
2195 struct fec_enet_private *fep = netdev_priv(ndev);
2200 if (pause->tx_pause != pause->rx_pause) {
2202 "hardware only support enable/disable both tx and rx");
2206 fep->pause_flag = 0;
2208 /* tx pause must be same as rx pause */
2209 fep->pause_flag |= pause->rx_pause ? FEC_PAUSE_FLAG_ENABLE : 0;
2210 fep->pause_flag |= pause->autoneg ? FEC_PAUSE_FLAG_AUTONEG : 0;
2212 if (pause->rx_pause || pause->autoneg) {
2213 ndev->phydev->supported |= ADVERTISED_Pause;
2214 ndev->phydev->advertising |= ADVERTISED_Pause;
2216 ndev->phydev->supported &= ~ADVERTISED_Pause;
2217 ndev->phydev->advertising &= ~ADVERTISED_Pause;
2220 if (pause->autoneg) {
2221 if (netif_running(ndev))
2223 phy_start_aneg(ndev->phydev);
2225 if (netif_running(ndev)) {
2226 napi_disable(&fep->napi);
2227 netif_tx_lock_bh(ndev);
2229 netif_wake_queue(ndev);
2230 netif_tx_unlock_bh(ndev);
2231 napi_enable(&fep->napi);
2237 static const struct fec_stat {
2238 char name[ETH_GSTRING_LEN];
2242 { "tx_dropped", RMON_T_DROP },
2243 { "tx_packets", RMON_T_PACKETS },
2244 { "tx_broadcast", RMON_T_BC_PKT },
2245 { "tx_multicast", RMON_T_MC_PKT },
2246 { "tx_crc_errors", RMON_T_CRC_ALIGN },
2247 { "tx_undersize", RMON_T_UNDERSIZE },
2248 { "tx_oversize", RMON_T_OVERSIZE },
2249 { "tx_fragment", RMON_T_FRAG },
2250 { "tx_jabber", RMON_T_JAB },
2251 { "tx_collision", RMON_T_COL },
2252 { "tx_64byte", RMON_T_P64 },
2253 { "tx_65to127byte", RMON_T_P65TO127 },
2254 { "tx_128to255byte", RMON_T_P128TO255 },
2255 { "tx_256to511byte", RMON_T_P256TO511 },
2256 { "tx_512to1023byte", RMON_T_P512TO1023 },
2257 { "tx_1024to2047byte", RMON_T_P1024TO2047 },
2258 { "tx_GTE2048byte", RMON_T_P_GTE2048 },
2259 { "tx_octets", RMON_T_OCTETS },
2262 { "IEEE_tx_drop", IEEE_T_DROP },
2263 { "IEEE_tx_frame_ok", IEEE_T_FRAME_OK },
2264 { "IEEE_tx_1col", IEEE_T_1COL },
2265 { "IEEE_tx_mcol", IEEE_T_MCOL },
2266 { "IEEE_tx_def", IEEE_T_DEF },
2267 { "IEEE_tx_lcol", IEEE_T_LCOL },
2268 { "IEEE_tx_excol", IEEE_T_EXCOL },
2269 { "IEEE_tx_macerr", IEEE_T_MACERR },
2270 { "IEEE_tx_cserr", IEEE_T_CSERR },
2271 { "IEEE_tx_sqe", IEEE_T_SQE },
2272 { "IEEE_tx_fdxfc", IEEE_T_FDXFC },
2273 { "IEEE_tx_octets_ok", IEEE_T_OCTETS_OK },
2276 { "rx_packets", RMON_R_PACKETS },
2277 { "rx_broadcast", RMON_R_BC_PKT },
2278 { "rx_multicast", RMON_R_MC_PKT },
2279 { "rx_crc_errors", RMON_R_CRC_ALIGN },
2280 { "rx_undersize", RMON_R_UNDERSIZE },
2281 { "rx_oversize", RMON_R_OVERSIZE },
2282 { "rx_fragment", RMON_R_FRAG },
2283 { "rx_jabber", RMON_R_JAB },
2284 { "rx_64byte", RMON_R_P64 },
2285 { "rx_65to127byte", RMON_R_P65TO127 },
2286 { "rx_128to255byte", RMON_R_P128TO255 },
2287 { "rx_256to511byte", RMON_R_P256TO511 },
2288 { "rx_512to1023byte", RMON_R_P512TO1023 },
2289 { "rx_1024to2047byte", RMON_R_P1024TO2047 },
2290 { "rx_GTE2048byte", RMON_R_P_GTE2048 },
2291 { "rx_octets", RMON_R_OCTETS },
2294 { "IEEE_rx_drop", IEEE_R_DROP },
2295 { "IEEE_rx_frame_ok", IEEE_R_FRAME_OK },
2296 { "IEEE_rx_crc", IEEE_R_CRC },
2297 { "IEEE_rx_align", IEEE_R_ALIGN },
2298 { "IEEE_rx_macerr", IEEE_R_MACERR },
2299 { "IEEE_rx_fdxfc", IEEE_R_FDXFC },
2300 { "IEEE_rx_octets_ok", IEEE_R_OCTETS_OK },
2303 static void fec_enet_get_ethtool_stats(struct net_device *dev,
2304 struct ethtool_stats *stats, u64 *data)
2306 struct fec_enet_private *fep = netdev_priv(dev);
2309 for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
2310 data[i] = readl(fep->hwp + fec_stats[i].offset);
2313 static void fec_enet_get_strings(struct net_device *netdev,
2314 u32 stringset, u8 *data)
2317 switch (stringset) {
2319 for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
2320 memcpy(data + i * ETH_GSTRING_LEN,
2321 fec_stats[i].name, ETH_GSTRING_LEN);
2326 static int fec_enet_get_sset_count(struct net_device *dev, int sset)
2330 return ARRAY_SIZE(fec_stats);
2335 #endif /* !defined(CONFIG_M5272) */
2337 static int fec_enet_nway_reset(struct net_device *dev)
2339 struct phy_device *phydev = dev->phydev;
2344 return genphy_restart_aneg(phydev);
2347 /* ITR clock source is enet system clock (clk_ahb).
2348 * TCTT unit is cycle_ns * 64 cycle
2349 * So, the ICTT value = X us / (cycle_ns * 64)
2351 static int fec_enet_us_to_itr_clock(struct net_device *ndev, int us)
2353 struct fec_enet_private *fep = netdev_priv(ndev);
2355 return us * (fep->itr_clk_rate / 64000) / 1000;
2358 /* Set threshold for interrupt coalescing */
2359 static void fec_enet_itr_coal_set(struct net_device *ndev)
2361 struct fec_enet_private *fep = netdev_priv(ndev);
2364 if (!(fep->quirks & FEC_QUIRK_HAS_AVB))
2367 /* Must be greater than zero to avoid unpredictable behavior */
2368 if (!fep->rx_time_itr || !fep->rx_pkts_itr ||
2369 !fep->tx_time_itr || !fep->tx_pkts_itr)
2372 /* Select enet system clock as Interrupt Coalescing
2373 * timer Clock Source
2375 rx_itr = FEC_ITR_CLK_SEL;
2376 tx_itr = FEC_ITR_CLK_SEL;
2378 /* set ICFT and ICTT */
2379 rx_itr |= FEC_ITR_ICFT(fep->rx_pkts_itr);
2380 rx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->rx_time_itr));
2381 tx_itr |= FEC_ITR_ICFT(fep->tx_pkts_itr);
2382 tx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->tx_time_itr));
2384 rx_itr |= FEC_ITR_EN;
2385 tx_itr |= FEC_ITR_EN;
2387 writel(tx_itr, fep->hwp + FEC_TXIC0);
2388 writel(rx_itr, fep->hwp + FEC_RXIC0);
2389 writel(tx_itr, fep->hwp + FEC_TXIC1);
2390 writel(rx_itr, fep->hwp + FEC_RXIC1);
2391 writel(tx_itr, fep->hwp + FEC_TXIC2);
2392 writel(rx_itr, fep->hwp + FEC_RXIC2);
2396 fec_enet_get_coalesce(struct net_device *ndev, struct ethtool_coalesce *ec)
2398 struct fec_enet_private *fep = netdev_priv(ndev);
2400 if (!(fep->quirks & FEC_QUIRK_HAS_AVB))
2403 ec->rx_coalesce_usecs = fep->rx_time_itr;
2404 ec->rx_max_coalesced_frames = fep->rx_pkts_itr;
2406 ec->tx_coalesce_usecs = fep->tx_time_itr;
2407 ec->tx_max_coalesced_frames = fep->tx_pkts_itr;
2413 fec_enet_set_coalesce(struct net_device *ndev, struct ethtool_coalesce *ec)
2415 struct fec_enet_private *fep = netdev_priv(ndev);
2418 if (!(fep->quirks & FEC_QUIRK_HAS_AVB))
2421 if (ec->rx_max_coalesced_frames > 255) {
2422 pr_err("Rx coalesced frames exceed hardware limiation");
2426 if (ec->tx_max_coalesced_frames > 255) {
2427 pr_err("Tx coalesced frame exceed hardware limiation");
2431 cycle = fec_enet_us_to_itr_clock(ndev, fep->rx_time_itr);
2432 if (cycle > 0xFFFF) {
2433 pr_err("Rx coalesed usec exceeed hardware limiation");
2437 cycle = fec_enet_us_to_itr_clock(ndev, fep->tx_time_itr);
2438 if (cycle > 0xFFFF) {
2439 pr_err("Rx coalesed usec exceeed hardware limiation");
2443 fep->rx_time_itr = ec->rx_coalesce_usecs;
2444 fep->rx_pkts_itr = ec->rx_max_coalesced_frames;
2446 fep->tx_time_itr = ec->tx_coalesce_usecs;
2447 fep->tx_pkts_itr = ec->tx_max_coalesced_frames;
2449 fec_enet_itr_coal_set(ndev);
2454 static void fec_enet_itr_coal_init(struct net_device *ndev)
2456 struct ethtool_coalesce ec;
2458 ec.rx_coalesce_usecs = FEC_ITR_ICTT_DEFAULT;
2459 ec.rx_max_coalesced_frames = FEC_ITR_ICFT_DEFAULT;
2461 ec.tx_coalesce_usecs = FEC_ITR_ICTT_DEFAULT;
2462 ec.tx_max_coalesced_frames = FEC_ITR_ICFT_DEFAULT;
2464 fec_enet_set_coalesce(ndev, &ec);
2467 static int fec_enet_get_tunable(struct net_device *netdev,
2468 const struct ethtool_tunable *tuna,
2471 struct fec_enet_private *fep = netdev_priv(netdev);
2475 case ETHTOOL_RX_COPYBREAK:
2476 *(u32 *)data = fep->rx_copybreak;
2486 static int fec_enet_set_tunable(struct net_device *netdev,
2487 const struct ethtool_tunable *tuna,
2490 struct fec_enet_private *fep = netdev_priv(netdev);
2494 case ETHTOOL_RX_COPYBREAK:
2495 fep->rx_copybreak = *(u32 *)data;
2506 fec_enet_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
2508 struct fec_enet_private *fep = netdev_priv(ndev);
2510 if (fep->wol_flag & FEC_WOL_HAS_MAGIC_PACKET) {
2511 wol->supported = WAKE_MAGIC;
2512 wol->wolopts = fep->wol_flag & FEC_WOL_FLAG_ENABLE ? WAKE_MAGIC : 0;
2514 wol->supported = wol->wolopts = 0;
2519 fec_enet_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
2521 struct fec_enet_private *fep = netdev_priv(ndev);
2523 if (!(fep->wol_flag & FEC_WOL_HAS_MAGIC_PACKET))
2526 if (wol->wolopts & ~WAKE_MAGIC)
2529 device_set_wakeup_enable(&ndev->dev, wol->wolopts & WAKE_MAGIC);
2530 if (device_may_wakeup(&ndev->dev)) {
2531 fep->wol_flag |= FEC_WOL_FLAG_ENABLE;
2532 if (fep->irq[0] > 0)
2533 enable_irq_wake(fep->irq[0]);
2535 fep->wol_flag &= (~FEC_WOL_FLAG_ENABLE);
2536 if (fep->irq[0] > 0)
2537 disable_irq_wake(fep->irq[0]);
2543 static const struct ethtool_ops fec_enet_ethtool_ops = {
2544 .get_drvinfo = fec_enet_get_drvinfo,
2545 .get_regs_len = fec_enet_get_regs_len,
2546 .get_regs = fec_enet_get_regs,
2547 .nway_reset = fec_enet_nway_reset,
2548 .get_link = ethtool_op_get_link,
2549 .get_coalesce = fec_enet_get_coalesce,
2550 .set_coalesce = fec_enet_set_coalesce,
2551 #ifndef CONFIG_M5272
2552 .get_pauseparam = fec_enet_get_pauseparam,
2553 .set_pauseparam = fec_enet_set_pauseparam,
2554 .get_strings = fec_enet_get_strings,
2555 .get_ethtool_stats = fec_enet_get_ethtool_stats,
2556 .get_sset_count = fec_enet_get_sset_count,
2558 .get_ts_info = fec_enet_get_ts_info,
2559 .get_tunable = fec_enet_get_tunable,
2560 .set_tunable = fec_enet_set_tunable,
2561 .get_wol = fec_enet_get_wol,
2562 .set_wol = fec_enet_set_wol,
2563 .get_link_ksettings = phy_ethtool_get_link_ksettings,
2564 .set_link_ksettings = phy_ethtool_set_link_ksettings,
2567 static int fec_enet_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
2569 struct fec_enet_private *fep = netdev_priv(ndev);
2570 struct phy_device *phydev = ndev->phydev;
2572 if (!netif_running(ndev))
2578 if (fep->bufdesc_ex) {
2579 if (cmd == SIOCSHWTSTAMP)
2580 return fec_ptp_set(ndev, rq);
2581 if (cmd == SIOCGHWTSTAMP)
2582 return fec_ptp_get(ndev, rq);
2585 return phy_mii_ioctl(phydev, rq, cmd);
2588 static void fec_enet_free_buffers(struct net_device *ndev)
2590 struct fec_enet_private *fep = netdev_priv(ndev);
2592 struct sk_buff *skb;
2593 struct bufdesc *bdp;
2594 struct fec_enet_priv_tx_q *txq;
2595 struct fec_enet_priv_rx_q *rxq;
2598 for (q = 0; q < fep->num_rx_queues; q++) {
2599 rxq = fep->rx_queue[q];
2601 for (i = 0; i < rxq->bd.ring_size; i++) {
2602 skb = rxq->rx_skbuff[i];
2603 rxq->rx_skbuff[i] = NULL;
2605 dma_unmap_single(&fep->pdev->dev,
2606 fec32_to_cpu(bdp->cbd_bufaddr),
2607 FEC_ENET_RX_FRSIZE - fep->rx_align,
2611 bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
2615 for (q = 0; q < fep->num_tx_queues; q++) {
2616 txq = fep->tx_queue[q];
2618 for (i = 0; i < txq->bd.ring_size; i++) {
2619 kfree(txq->tx_bounce[i]);
2620 txq->tx_bounce[i] = NULL;
2621 skb = txq->tx_skbuff[i];
2622 txq->tx_skbuff[i] = NULL;
2628 static void fec_enet_free_queue(struct net_device *ndev)
2630 struct fec_enet_private *fep = netdev_priv(ndev);
2632 struct fec_enet_priv_tx_q *txq;
2634 for (i = 0; i < fep->num_tx_queues; i++)
2635 if (fep->tx_queue[i] && fep->tx_queue[i]->tso_hdrs) {
2636 txq = fep->tx_queue[i];
2637 dma_free_coherent(NULL,
2638 txq->bd.ring_size * TSO_HEADER_SIZE,
2643 for (i = 0; i < fep->num_rx_queues; i++)
2644 kfree(fep->rx_queue[i]);
2645 for (i = 0; i < fep->num_tx_queues; i++)
2646 kfree(fep->tx_queue[i]);
2649 static int fec_enet_alloc_queue(struct net_device *ndev)
2651 struct fec_enet_private *fep = netdev_priv(ndev);
2654 struct fec_enet_priv_tx_q *txq;
2656 for (i = 0; i < fep->num_tx_queues; i++) {
2657 txq = kzalloc(sizeof(*txq), GFP_KERNEL);
2663 fep->tx_queue[i] = txq;
2664 txq->bd.ring_size = TX_RING_SIZE;
2665 fep->total_tx_ring_size += fep->tx_queue[i]->bd.ring_size;
2667 txq->tx_stop_threshold = FEC_MAX_SKB_DESCS;
2668 txq->tx_wake_threshold =
2669 (txq->bd.ring_size - txq->tx_stop_threshold) / 2;
2671 txq->tso_hdrs = dma_alloc_coherent(NULL,
2672 txq->bd.ring_size * TSO_HEADER_SIZE,
2675 if (!txq->tso_hdrs) {
2681 for (i = 0; i < fep->num_rx_queues; i++) {
2682 fep->rx_queue[i] = kzalloc(sizeof(*fep->rx_queue[i]),
2684 if (!fep->rx_queue[i]) {
2689 fep->rx_queue[i]->bd.ring_size = RX_RING_SIZE;
2690 fep->total_rx_ring_size += fep->rx_queue[i]->bd.ring_size;
2695 fec_enet_free_queue(ndev);
2700 fec_enet_alloc_rxq_buffers(struct net_device *ndev, unsigned int queue)
2702 struct fec_enet_private *fep = netdev_priv(ndev);
2704 struct sk_buff *skb;
2705 struct bufdesc *bdp;
2706 struct fec_enet_priv_rx_q *rxq;
2708 rxq = fep->rx_queue[queue];
2710 for (i = 0; i < rxq->bd.ring_size; i++) {
2711 skb = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
2715 if (fec_enet_new_rxbdp(ndev, bdp, skb)) {
2720 rxq->rx_skbuff[i] = skb;
2721 bdp->cbd_sc = cpu_to_fec16(BD_ENET_RX_EMPTY);
2723 if (fep->bufdesc_ex) {
2724 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
2725 ebdp->cbd_esc = cpu_to_fec32(BD_ENET_RX_INT);
2728 bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
2731 /* Set the last buffer to wrap. */
2732 bdp = fec_enet_get_prevdesc(bdp, &rxq->bd);
2733 bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP);
2737 fec_enet_free_buffers(ndev);
2742 fec_enet_alloc_txq_buffers(struct net_device *ndev, unsigned int queue)
2744 struct fec_enet_private *fep = netdev_priv(ndev);
2746 struct bufdesc *bdp;
2747 struct fec_enet_priv_tx_q *txq;
2749 txq = fep->tx_queue[queue];
2751 for (i = 0; i < txq->bd.ring_size; i++) {
2752 txq->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL);
2753 if (!txq->tx_bounce[i])
2756 bdp->cbd_sc = cpu_to_fec16(0);
2757 bdp->cbd_bufaddr = cpu_to_fec32(0);
2759 if (fep->bufdesc_ex) {
2760 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
2761 ebdp->cbd_esc = cpu_to_fec32(BD_ENET_TX_INT);
2764 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
2767 /* Set the last buffer to wrap. */
2768 bdp = fec_enet_get_prevdesc(bdp, &txq->bd);
2769 bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP);
2774 fec_enet_free_buffers(ndev);
2778 static int fec_enet_alloc_buffers(struct net_device *ndev)
2780 struct fec_enet_private *fep = netdev_priv(ndev);
2783 for (i = 0; i < fep->num_rx_queues; i++)
2784 if (fec_enet_alloc_rxq_buffers(ndev, i))
2787 for (i = 0; i < fep->num_tx_queues; i++)
2788 if (fec_enet_alloc_txq_buffers(ndev, i))
2794 fec_enet_open(struct net_device *ndev)
2796 struct fec_enet_private *fep = netdev_priv(ndev);
2799 ret = pm_runtime_get_sync(&fep->pdev->dev);
2803 pinctrl_pm_select_default_state(&fep->pdev->dev);
2804 ret = fec_enet_clk_enable(ndev, true);
2808 /* I should reset the ring buffers here, but I don't yet know
2809 * a simple way to do that.
2812 ret = fec_enet_alloc_buffers(ndev);
2814 goto err_enet_alloc;
2816 /* Init MAC prior to mii bus probe */
2819 /* Probe and connect to PHY when open the interface */
2820 ret = fec_enet_mii_probe(ndev);
2822 goto err_enet_mii_probe;
2824 if (fep->quirks & FEC_QUIRK_ERR006687)
2825 imx6q_cpuidle_fec_irqs_used();
2827 napi_enable(&fep->napi);
2828 phy_start(ndev->phydev);
2829 netif_tx_start_all_queues(ndev);
2831 device_set_wakeup_enable(&ndev->dev, fep->wol_flag &
2832 FEC_WOL_FLAG_ENABLE);
2837 fec_enet_free_buffers(ndev);
2839 fec_enet_clk_enable(ndev, false);
2841 pm_runtime_mark_last_busy(&fep->pdev->dev);
2842 pm_runtime_put_autosuspend(&fep->pdev->dev);
2843 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
2848 fec_enet_close(struct net_device *ndev)
2850 struct fec_enet_private *fep = netdev_priv(ndev);
2852 phy_stop(ndev->phydev);
2854 if (netif_device_present(ndev)) {
2855 napi_disable(&fep->napi);
2856 netif_tx_disable(ndev);
2860 phy_disconnect(ndev->phydev);
2862 if (fep->quirks & FEC_QUIRK_ERR006687)
2863 imx6q_cpuidle_fec_irqs_unused();
2865 fec_enet_clk_enable(ndev, false);
2866 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
2867 pm_runtime_mark_last_busy(&fep->pdev->dev);
2868 pm_runtime_put_autosuspend(&fep->pdev->dev);
2870 fec_enet_free_buffers(ndev);
2875 /* Set or clear the multicast filter for this adaptor.
2876 * Skeleton taken from sunlance driver.
2877 * The CPM Ethernet implementation allows Multicast as well as individual
2878 * MAC address filtering. Some of the drivers check to make sure it is
2879 * a group multicast address, and discard those that are not. I guess I
2880 * will do the same for now, but just remove the test if you want
2881 * individual filtering as well (do the upper net layers want or support
2882 * this kind of feature?).
2885 #define HASH_BITS 6 /* #bits in hash */
2886 #define CRC32_POLY 0xEDB88320
2888 static void set_multicast_list(struct net_device *ndev)
2890 struct fec_enet_private *fep = netdev_priv(ndev);
2891 struct netdev_hw_addr *ha;
2892 unsigned int i, bit, data, crc, tmp;
2895 if (ndev->flags & IFF_PROMISC) {
2896 tmp = readl(fep->hwp + FEC_R_CNTRL);
2898 writel(tmp, fep->hwp + FEC_R_CNTRL);
2902 tmp = readl(fep->hwp + FEC_R_CNTRL);
2904 writel(tmp, fep->hwp + FEC_R_CNTRL);
2906 if (ndev->flags & IFF_ALLMULTI) {
2907 /* Catch all multicast addresses, so set the
2910 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2911 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
2916 /* Clear filter and add the addresses in hash register
2918 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2919 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
2921 netdev_for_each_mc_addr(ha, ndev) {
2922 /* calculate crc32 value of mac address */
2925 for (i = 0; i < ndev->addr_len; i++) {
2927 for (bit = 0; bit < 8; bit++, data >>= 1) {
2929 (((crc ^ data) & 1) ? CRC32_POLY : 0);
2933 /* only upper 6 bits (HASH_BITS) are used
2934 * which point to specific bit in he hash registers
2936 hash = (crc >> (32 - HASH_BITS)) & 0x3f;
2939 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2940 tmp |= 1 << (hash - 32);
2941 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2943 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_LOW);
2945 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
2950 /* Set a MAC change in hardware. */
2952 fec_set_mac_address(struct net_device *ndev, void *p)
2954 struct fec_enet_private *fep = netdev_priv(ndev);
2955 struct sockaddr *addr = p;
2958 if (!is_valid_ether_addr(addr->sa_data))
2959 return -EADDRNOTAVAIL;
2960 memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len);
2963 /* Add netif status check here to avoid system hang in below case:
2964 * ifconfig ethx down; ifconfig ethx hw ether xx:xx:xx:xx:xx:xx;
2965 * After ethx down, fec all clocks are gated off and then register
2966 * access causes system hang.
2968 if (!netif_running(ndev))
2971 writel(ndev->dev_addr[3] | (ndev->dev_addr[2] << 8) |
2972 (ndev->dev_addr[1] << 16) | (ndev->dev_addr[0] << 24),
2973 fep->hwp + FEC_ADDR_LOW);
2974 writel((ndev->dev_addr[5] << 16) | (ndev->dev_addr[4] << 24),
2975 fep->hwp + FEC_ADDR_HIGH);
2979 #ifdef CONFIG_NET_POLL_CONTROLLER
2981 * fec_poll_controller - FEC Poll controller function
2982 * @dev: The FEC network adapter
2984 * Polled functionality used by netconsole and others in non interrupt mode
2987 static void fec_poll_controller(struct net_device *dev)
2990 struct fec_enet_private *fep = netdev_priv(dev);
2992 for (i = 0; i < FEC_IRQ_NUM; i++) {
2993 if (fep->irq[i] > 0) {
2994 disable_irq(fep->irq[i]);
2995 fec_enet_interrupt(fep->irq[i], dev);
2996 enable_irq(fep->irq[i]);
3002 static inline void fec_enet_set_netdev_features(struct net_device *netdev,
3003 netdev_features_t features)
3005 struct fec_enet_private *fep = netdev_priv(netdev);
3006 netdev_features_t changed = features ^ netdev->features;
3008 netdev->features = features;
3010 /* Receive checksum has been changed */
3011 if (changed & NETIF_F_RXCSUM) {
3012 if (features & NETIF_F_RXCSUM)
3013 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
3015 fep->csum_flags &= ~FLAG_RX_CSUM_ENABLED;
3019 static int fec_set_features(struct net_device *netdev,
3020 netdev_features_t features)
3022 struct fec_enet_private *fep = netdev_priv(netdev);
3023 netdev_features_t changed = features ^ netdev->features;
3025 if (netif_running(netdev) && changed & NETIF_F_RXCSUM) {
3026 napi_disable(&fep->napi);
3027 netif_tx_lock_bh(netdev);
3029 fec_enet_set_netdev_features(netdev, features);
3030 fec_restart(netdev);
3031 netif_tx_wake_all_queues(netdev);
3032 netif_tx_unlock_bh(netdev);
3033 napi_enable(&fep->napi);
3035 fec_enet_set_netdev_features(netdev, features);
3041 static const struct net_device_ops fec_netdev_ops = {
3042 .ndo_open = fec_enet_open,
3043 .ndo_stop = fec_enet_close,
3044 .ndo_start_xmit = fec_enet_start_xmit,
3045 .ndo_set_rx_mode = set_multicast_list,
3046 .ndo_change_mtu = eth_change_mtu,
3047 .ndo_validate_addr = eth_validate_addr,
3048 .ndo_tx_timeout = fec_timeout,
3049 .ndo_set_mac_address = fec_set_mac_address,
3050 .ndo_do_ioctl = fec_enet_ioctl,
3051 #ifdef CONFIG_NET_POLL_CONTROLLER
3052 .ndo_poll_controller = fec_poll_controller,
3054 .ndo_set_features = fec_set_features,
3057 static const unsigned short offset_des_active_rxq[] = {
3058 FEC_R_DES_ACTIVE_0, FEC_R_DES_ACTIVE_1, FEC_R_DES_ACTIVE_2
3061 static const unsigned short offset_des_active_txq[] = {
3062 FEC_X_DES_ACTIVE_0, FEC_X_DES_ACTIVE_1, FEC_X_DES_ACTIVE_2
3066 * XXX: We need to clean up on failure exits here.
3069 static int fec_enet_init(struct net_device *ndev)
3071 struct fec_enet_private *fep = netdev_priv(ndev);
3072 struct bufdesc *cbd_base;
3076 unsigned dsize = fep->bufdesc_ex ? sizeof(struct bufdesc_ex) :
3077 sizeof(struct bufdesc);
3078 unsigned dsize_log2 = __fls(dsize);
3080 WARN_ON(dsize != (1 << dsize_log2));
3081 #if defined(CONFIG_ARM)
3082 fep->rx_align = 0xf;
3083 fep->tx_align = 0xf;
3085 fep->rx_align = 0x3;
3086 fep->tx_align = 0x3;
3089 fec_enet_alloc_queue(ndev);
3091 bd_size = (fep->total_tx_ring_size + fep->total_rx_ring_size) * dsize;
3093 /* Allocate memory for buffer descriptors. */
3094 cbd_base = dmam_alloc_coherent(&fep->pdev->dev, bd_size, &bd_dma,
3100 memset(cbd_base, 0, bd_size);
3102 /* Get the Ethernet address */
3104 /* make sure MAC we just acquired is programmed into the hw */
3105 fec_set_mac_address(ndev, NULL);
3107 /* Set receive and transmit descriptor base. */
3108 for (i = 0; i < fep->num_rx_queues; i++) {
3109 struct fec_enet_priv_rx_q *rxq = fep->rx_queue[i];
3110 unsigned size = dsize * rxq->bd.ring_size;
3113 rxq->bd.base = cbd_base;
3114 rxq->bd.cur = cbd_base;
3115 rxq->bd.dma = bd_dma;
3116 rxq->bd.dsize = dsize;
3117 rxq->bd.dsize_log2 = dsize_log2;
3118 rxq->bd.reg_desc_active = fep->hwp + offset_des_active_rxq[i];
3120 cbd_base = (struct bufdesc *)(((void *)cbd_base) + size);
3121 rxq->bd.last = (struct bufdesc *)(((void *)cbd_base) - dsize);
3124 for (i = 0; i < fep->num_tx_queues; i++) {
3125 struct fec_enet_priv_tx_q *txq = fep->tx_queue[i];
3126 unsigned size = dsize * txq->bd.ring_size;
3129 txq->bd.base = cbd_base;
3130 txq->bd.cur = cbd_base;
3131 txq->bd.dma = bd_dma;
3132 txq->bd.dsize = dsize;
3133 txq->bd.dsize_log2 = dsize_log2;
3134 txq->bd.reg_desc_active = fep->hwp + offset_des_active_txq[i];
3136 cbd_base = (struct bufdesc *)(((void *)cbd_base) + size);
3137 txq->bd.last = (struct bufdesc *)(((void *)cbd_base) - dsize);
3141 /* The FEC Ethernet specific entries in the device structure */
3142 ndev->watchdog_timeo = TX_TIMEOUT;
3143 ndev->netdev_ops = &fec_netdev_ops;
3144 ndev->ethtool_ops = &fec_enet_ethtool_ops;
3146 writel(FEC_RX_DISABLED_IMASK, fep->hwp + FEC_IMASK);
3147 netif_napi_add(ndev, &fep->napi, fec_enet_rx_napi, NAPI_POLL_WEIGHT);
3149 if (fep->quirks & FEC_QUIRK_HAS_VLAN)
3150 /* enable hw VLAN support */
3151 ndev->features |= NETIF_F_HW_VLAN_CTAG_RX;
3153 if (fep->quirks & FEC_QUIRK_HAS_CSUM) {
3154 ndev->gso_max_segs = FEC_MAX_TSO_SEGS;
3156 /* enable hw accelerator */
3157 ndev->features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
3158 | NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_TSO);
3159 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
3162 if (fep->quirks & FEC_QUIRK_HAS_AVB) {
3164 fep->rx_align = 0x3f;
3167 ndev->hw_features = ndev->features;
3175 static void fec_reset_phy(struct platform_device *pdev)
3178 bool active_high = false;
3180 struct device_node *np = pdev->dev.of_node;
3185 of_property_read_u32(np, "phy-reset-duration", &msec);
3186 /* A sane reset duration should not be longer than 1s */
3190 phy_reset = of_get_named_gpio(np, "phy-reset-gpios", 0);
3191 if (!gpio_is_valid(phy_reset))
3194 active_high = of_property_read_bool(np, "phy-reset-active-high");
3196 err = devm_gpio_request_one(&pdev->dev, phy_reset,
3197 active_high ? GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW,
3200 dev_err(&pdev->dev, "failed to get phy-reset-gpios: %d\n", err);
3204 gpio_set_value_cansleep(phy_reset, !active_high);
3206 #else /* CONFIG_OF */
3207 static void fec_reset_phy(struct platform_device *pdev)
3210 * In case of platform probe, the reset has been done
3214 #endif /* CONFIG_OF */
3217 fec_enet_get_queue_num(struct platform_device *pdev, int *num_tx, int *num_rx)
3219 struct device_node *np = pdev->dev.of_node;
3221 *num_tx = *num_rx = 1;
3223 if (!np || !of_device_is_available(np))
3226 /* parse the num of tx and rx queues */
3227 of_property_read_u32(np, "fsl,num-tx-queues", num_tx);
3229 of_property_read_u32(np, "fsl,num-rx-queues", num_rx);
3231 if (*num_tx < 1 || *num_tx > FEC_ENET_MAX_TX_QS) {
3232 dev_warn(&pdev->dev, "Invalid num_tx(=%d), fall back to 1\n",
3238 if (*num_rx < 1 || *num_rx > FEC_ENET_MAX_RX_QS) {
3239 dev_warn(&pdev->dev, "Invalid num_rx(=%d), fall back to 1\n",
3248 fec_probe(struct platform_device *pdev)
3250 struct fec_enet_private *fep;
3251 struct fec_platform_data *pdata;
3252 struct net_device *ndev;
3253 int i, irq, ret = 0;
3255 const struct of_device_id *of_id;
3257 struct device_node *np = pdev->dev.of_node, *phy_node;
3261 fec_enet_get_queue_num(pdev, &num_tx_qs, &num_rx_qs);
3263 /* Init network device */
3264 ndev = alloc_etherdev_mqs(sizeof(struct fec_enet_private),
3265 num_tx_qs, num_rx_qs);
3269 SET_NETDEV_DEV(ndev, &pdev->dev);
3271 /* setup board info structure */
3272 fep = netdev_priv(ndev);
3274 of_id = of_match_device(fec_dt_ids, &pdev->dev);
3276 pdev->id_entry = of_id->data;
3277 fep->quirks = pdev->id_entry->driver_data;
3280 fep->num_rx_queues = num_rx_qs;
3281 fep->num_tx_queues = num_tx_qs;
3283 #if !defined(CONFIG_M5272)
3284 /* default enable pause frame auto negotiation */
3285 if (fep->quirks & FEC_QUIRK_HAS_GBIT)
3286 fep->pause_flag |= FEC_PAUSE_FLAG_AUTONEG;
3289 /* Select default pin state */
3290 pinctrl_pm_select_default_state(&pdev->dev);
3292 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3293 fep->hwp = devm_ioremap_resource(&pdev->dev, r);
3294 if (IS_ERR(fep->hwp)) {
3295 ret = PTR_ERR(fep->hwp);
3296 goto failed_ioremap;
3300 fep->dev_id = dev_id++;
3302 platform_set_drvdata(pdev, ndev);
3304 if ((of_machine_is_compatible("fsl,imx6q") ||
3305 of_machine_is_compatible("fsl,imx6dl")) &&
3306 !of_property_read_bool(np, "fsl,err006687-workaround-present"))
3307 fep->quirks |= FEC_QUIRK_ERR006687;
3309 if (of_get_property(np, "fsl,magic-packet", NULL))
3310 fep->wol_flag |= FEC_WOL_HAS_MAGIC_PACKET;
3312 phy_node = of_parse_phandle(np, "phy-handle", 0);
3313 if (!phy_node && of_phy_is_fixed_link(np)) {
3314 ret = of_phy_register_fixed_link(np);
3317 "broken fixed-link specification\n");
3320 phy_node = of_node_get(np);
3322 fep->phy_node = phy_node;
3324 ret = of_get_phy_mode(pdev->dev.of_node);
3326 pdata = dev_get_platdata(&pdev->dev);
3328 fep->phy_interface = pdata->phy;
3330 fep->phy_interface = PHY_INTERFACE_MODE_MII;
3332 fep->phy_interface = ret;
3335 fep->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
3336 if (IS_ERR(fep->clk_ipg)) {
3337 ret = PTR_ERR(fep->clk_ipg);
3341 fep->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
3342 if (IS_ERR(fep->clk_ahb)) {
3343 ret = PTR_ERR(fep->clk_ahb);
3347 fep->itr_clk_rate = clk_get_rate(fep->clk_ahb);
3349 /* enet_out is optional, depends on board */
3350 fep->clk_enet_out = devm_clk_get(&pdev->dev, "enet_out");
3351 if (IS_ERR(fep->clk_enet_out))
3352 fep->clk_enet_out = NULL;
3354 fep->ptp_clk_on = false;
3355 mutex_init(&fep->ptp_clk_mutex);
3357 /* clk_ref is optional, depends on board */
3358 fep->clk_ref = devm_clk_get(&pdev->dev, "enet_clk_ref");
3359 if (IS_ERR(fep->clk_ref))
3360 fep->clk_ref = NULL;
3362 fep->bufdesc_ex = fep->quirks & FEC_QUIRK_HAS_BUFDESC_EX;
3363 fep->clk_ptp = devm_clk_get(&pdev->dev, "ptp");
3364 if (IS_ERR(fep->clk_ptp)) {
3365 fep->clk_ptp = NULL;
3366 fep->bufdesc_ex = false;
3369 ret = fec_enet_clk_enable(ndev, true);
3373 ret = clk_prepare_enable(fep->clk_ipg);
3375 goto failed_clk_ipg;
3377 fep->reg_phy = devm_regulator_get(&pdev->dev, "phy");
3378 if (!IS_ERR(fep->reg_phy)) {
3379 ret = regulator_enable(fep->reg_phy);
3382 "Failed to enable phy regulator: %d\n", ret);
3383 goto failed_regulator;
3386 fep->reg_phy = NULL;
3389 pm_runtime_set_autosuspend_delay(&pdev->dev, FEC_MDIO_PM_TIMEOUT);
3390 pm_runtime_use_autosuspend(&pdev->dev);
3391 pm_runtime_get_noresume(&pdev->dev);
3392 pm_runtime_set_active(&pdev->dev);
3393 pm_runtime_enable(&pdev->dev);
3395 fec_reset_phy(pdev);
3397 if (fep->bufdesc_ex)
3400 ret = fec_enet_init(ndev);
3404 for (i = 0; i < FEC_IRQ_NUM; i++) {
3405 irq = platform_get_irq(pdev, i);
3412 ret = devm_request_irq(&pdev->dev, irq, fec_enet_interrupt,
3413 0, pdev->name, ndev);
3420 init_completion(&fep->mdio_done);
3421 ret = fec_enet_mii_init(pdev);
3423 goto failed_mii_init;
3425 /* Carrier starts down, phylib will bring it up */
3426 netif_carrier_off(ndev);
3427 fec_enet_clk_enable(ndev, false);
3428 pinctrl_pm_select_sleep_state(&pdev->dev);
3430 ret = register_netdev(ndev);
3432 goto failed_register;
3434 device_init_wakeup(&ndev->dev, fep->wol_flag &
3435 FEC_WOL_HAS_MAGIC_PACKET);
3437 if (fep->bufdesc_ex && fep->ptp_clock)
3438 netdev_info(ndev, "registered PHC device %d\n", fep->dev_id);
3440 fep->rx_copybreak = COPYBREAK_DEFAULT;
3441 INIT_WORK(&fep->tx_timeout_work, fec_enet_timeout_work);
3443 pm_runtime_mark_last_busy(&pdev->dev);
3444 pm_runtime_put_autosuspend(&pdev->dev);
3449 fec_enet_mii_remove(fep);
3455 regulator_disable(fep->reg_phy);
3457 clk_disable_unprepare(fep->clk_ipg);
3459 fec_enet_clk_enable(ndev, false);
3462 of_node_put(phy_node);
3470 fec_drv_remove(struct platform_device *pdev)
3472 struct net_device *ndev = platform_get_drvdata(pdev);
3473 struct fec_enet_private *fep = netdev_priv(ndev);
3475 cancel_work_sync(&fep->tx_timeout_work);
3477 unregister_netdev(ndev);
3478 fec_enet_mii_remove(fep);
3480 regulator_disable(fep->reg_phy);
3481 of_node_put(fep->phy_node);
3487 static int __maybe_unused fec_suspend(struct device *dev)
3489 struct net_device *ndev = dev_get_drvdata(dev);
3490 struct fec_enet_private *fep = netdev_priv(ndev);
3493 if (netif_running(ndev)) {
3494 if (fep->wol_flag & FEC_WOL_FLAG_ENABLE)
3495 fep->wol_flag |= FEC_WOL_FLAG_SLEEP_ON;
3496 phy_stop(ndev->phydev);
3497 napi_disable(&fep->napi);
3498 netif_tx_lock_bh(ndev);
3499 netif_device_detach(ndev);
3500 netif_tx_unlock_bh(ndev);
3502 fec_enet_clk_enable(ndev, false);
3503 if (!(fep->wol_flag & FEC_WOL_FLAG_ENABLE))
3504 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
3508 if (fep->reg_phy && !(fep->wol_flag & FEC_WOL_FLAG_ENABLE))
3509 regulator_disable(fep->reg_phy);
3511 /* SOC supply clock to phy, when clock is disabled, phy link down
3512 * SOC control phy regulator, when regulator is disabled, phy link down
3514 if (fep->clk_enet_out || fep->reg_phy)
3520 static int __maybe_unused fec_resume(struct device *dev)
3522 struct net_device *ndev = dev_get_drvdata(dev);
3523 struct fec_enet_private *fep = netdev_priv(ndev);
3524 struct fec_platform_data *pdata = fep->pdev->dev.platform_data;
3528 if (fep->reg_phy && !(fep->wol_flag & FEC_WOL_FLAG_ENABLE)) {
3529 ret = regulator_enable(fep->reg_phy);
3535 if (netif_running(ndev)) {
3536 ret = fec_enet_clk_enable(ndev, true);
3541 if (fep->wol_flag & FEC_WOL_FLAG_ENABLE) {
3542 if (pdata && pdata->sleep_mode_enable)
3543 pdata->sleep_mode_enable(false);
3544 val = readl(fep->hwp + FEC_ECNTRL);
3545 val &= ~(FEC_ECR_MAGICEN | FEC_ECR_SLEEP);
3546 writel(val, fep->hwp + FEC_ECNTRL);
3547 fep->wol_flag &= ~FEC_WOL_FLAG_SLEEP_ON;
3549 pinctrl_pm_select_default_state(&fep->pdev->dev);
3552 netif_tx_lock_bh(ndev);
3553 netif_device_attach(ndev);
3554 netif_tx_unlock_bh(ndev);
3555 napi_enable(&fep->napi);
3556 phy_start(ndev->phydev);
3564 regulator_disable(fep->reg_phy);
3568 static int __maybe_unused fec_runtime_suspend(struct device *dev)
3570 struct net_device *ndev = dev_get_drvdata(dev);
3571 struct fec_enet_private *fep = netdev_priv(ndev);
3573 clk_disable_unprepare(fep->clk_ipg);
3578 static int __maybe_unused fec_runtime_resume(struct device *dev)
3580 struct net_device *ndev = dev_get_drvdata(dev);
3581 struct fec_enet_private *fep = netdev_priv(ndev);
3583 return clk_prepare_enable(fep->clk_ipg);
3586 static const struct dev_pm_ops fec_pm_ops = {
3587 SET_SYSTEM_SLEEP_PM_OPS(fec_suspend, fec_resume)
3588 SET_RUNTIME_PM_OPS(fec_runtime_suspend, fec_runtime_resume, NULL)
3591 static struct platform_driver fec_driver = {
3593 .name = DRIVER_NAME,
3595 .of_match_table = fec_dt_ids,
3597 .id_table = fec_devtype,
3599 .remove = fec_drv_remove,
3602 module_platform_driver(fec_driver);
3604 MODULE_ALIAS("platform:"DRIVER_NAME);
3605 MODULE_LICENSE("GPL");