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/ptrace.h>
28 #include <linux/errno.h>
29 #include <linux/ioport.h>
30 #include <linux/slab.h>
31 #include <linux/interrupt.h>
32 #include <linux/delay.h>
33 #include <linux/netdevice.h>
34 #include <linux/etherdevice.h>
35 #include <linux/skbuff.h>
40 #include <linux/tcp.h>
41 #include <linux/udp.h>
42 #include <linux/icmp.h>
43 #include <linux/spinlock.h>
44 #include <linux/workqueue.h>
45 #include <linux/bitops.h>
47 #include <linux/irq.h>
48 #include <linux/clk.h>
49 #include <linux/platform_device.h>
50 #include <linux/phy.h>
51 #include <linux/fec.h>
53 #include <linux/of_device.h>
54 #include <linux/of_gpio.h>
55 #include <linux/of_mdio.h>
56 #include <linux/of_net.h>
57 #include <linux/regulator/consumer.h>
58 #include <linux/if_vlan.h>
59 #include <linux/pinctrl/consumer.h>
60 #include <linux/prefetch.h>
62 #include <asm/cacheflush.h>
66 static void set_multicast_list(struct net_device *ndev);
67 static void fec_enet_itr_coal_init(struct net_device *ndev);
69 #define DRIVER_NAME "fec"
71 #define FEC_ENET_GET_QUQUE(_x) ((_x == 0) ? 1 : ((_x == 1) ? 2 : 0))
73 /* Pause frame feild and FIFO threshold */
74 #define FEC_ENET_FCE (1 << 5)
75 #define FEC_ENET_RSEM_V 0x84
76 #define FEC_ENET_RSFL_V 16
77 #define FEC_ENET_RAEM_V 0x8
78 #define FEC_ENET_RAFL_V 0x8
79 #define FEC_ENET_OPD_V 0xFFF0
81 static struct platform_device_id fec_devtype[] = {
83 /* keep it for coldfire */
88 .driver_data = FEC_QUIRK_USE_GASKET,
94 .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_SWAP_FRAME,
97 .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
98 FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
99 FEC_QUIRK_HAS_VLAN | FEC_QUIRK_ERR006358,
101 .name = "mvf600-fec",
102 .driver_data = FEC_QUIRK_ENET_MAC,
104 .name = "imx6sx-fec",
105 .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
106 FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
107 FEC_QUIRK_HAS_VLAN | FEC_QUIRK_HAS_AVB |
108 FEC_QUIRK_ERR007885 | FEC_QUIRK_BUG_CAPTURE,
113 MODULE_DEVICE_TABLE(platform, fec_devtype);
116 IMX25_FEC = 1, /* runs on i.mx25/50/53 */
117 IMX27_FEC, /* runs on i.mx27/35/51 */
124 static const struct of_device_id fec_dt_ids[] = {
125 { .compatible = "fsl,imx25-fec", .data = &fec_devtype[IMX25_FEC], },
126 { .compatible = "fsl,imx27-fec", .data = &fec_devtype[IMX27_FEC], },
127 { .compatible = "fsl,imx28-fec", .data = &fec_devtype[IMX28_FEC], },
128 { .compatible = "fsl,imx6q-fec", .data = &fec_devtype[IMX6Q_FEC], },
129 { .compatible = "fsl,mvf600-fec", .data = &fec_devtype[MVF600_FEC], },
130 { .compatible = "fsl,imx6sx-fec", .data = &fec_devtype[IMX6SX_FEC], },
133 MODULE_DEVICE_TABLE(of, fec_dt_ids);
135 static unsigned char macaddr[ETH_ALEN];
136 module_param_array(macaddr, byte, NULL, 0);
137 MODULE_PARM_DESC(macaddr, "FEC Ethernet MAC address");
139 #if defined(CONFIG_M5272)
141 * Some hardware gets it MAC address out of local flash memory.
142 * if this is non-zero then assume it is the address to get MAC from.
144 #if defined(CONFIG_NETtel)
145 #define FEC_FLASHMAC 0xf0006006
146 #elif defined(CONFIG_GILBARCONAP) || defined(CONFIG_SCALES)
147 #define FEC_FLASHMAC 0xf0006000
148 #elif defined(CONFIG_CANCam)
149 #define FEC_FLASHMAC 0xf0020000
150 #elif defined (CONFIG_M5272C3)
151 #define FEC_FLASHMAC (0xffe04000 + 4)
152 #elif defined(CONFIG_MOD5272)
153 #define FEC_FLASHMAC 0xffc0406b
155 #define FEC_FLASHMAC 0
157 #endif /* CONFIG_M5272 */
159 /* The FEC stores dest/src/type/vlan, data, and checksum for receive packets.
161 #define PKT_MAXBUF_SIZE 1522
162 #define PKT_MINBUF_SIZE 64
163 #define PKT_MAXBLR_SIZE 1536
165 /* FEC receive acceleration */
166 #define FEC_RACC_IPDIS (1 << 1)
167 #define FEC_RACC_PRODIS (1 << 2)
168 #define FEC_RACC_OPTIONS (FEC_RACC_IPDIS | FEC_RACC_PRODIS)
171 * The 5270/5271/5280/5282/532x RX control register also contains maximum frame
172 * size bits. Other FEC hardware does not, so we need to take that into
173 * account when setting it.
175 #if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
176 defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM)
177 #define OPT_FRAME_SIZE (PKT_MAXBUF_SIZE << 16)
179 #define OPT_FRAME_SIZE 0
182 /* FEC MII MMFR bits definition */
183 #define FEC_MMFR_ST (1 << 30)
184 #define FEC_MMFR_OP_READ (2 << 28)
185 #define FEC_MMFR_OP_WRITE (1 << 28)
186 #define FEC_MMFR_PA(v) ((v & 0x1f) << 23)
187 #define FEC_MMFR_RA(v) ((v & 0x1f) << 18)
188 #define FEC_MMFR_TA (2 << 16)
189 #define FEC_MMFR_DATA(v) (v & 0xffff)
191 #define FEC_MII_TIMEOUT 30000 /* us */
193 /* Transmitter timeout */
194 #define TX_TIMEOUT (2 * HZ)
196 #define FEC_PAUSE_FLAG_AUTONEG 0x1
197 #define FEC_PAUSE_FLAG_ENABLE 0x2
199 #define COPYBREAK_DEFAULT 256
201 #define TSO_HEADER_SIZE 128
202 /* Max number of allowed TCP segments for software TSO */
203 #define FEC_MAX_TSO_SEGS 100
204 #define FEC_MAX_SKB_DESCS (FEC_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS)
206 #define IS_TSO_HEADER(txq, addr) \
207 ((addr >= txq->tso_hdrs_dma) && \
208 (addr < txq->tso_hdrs_dma + txq->tx_ring_size * TSO_HEADER_SIZE))
213 struct bufdesc *fec_enet_get_nextdesc(struct bufdesc *bdp,
214 struct fec_enet_private *fep,
217 struct bufdesc *new_bd = bdp + 1;
218 struct bufdesc_ex *ex_new_bd = (struct bufdesc_ex *)bdp + 1;
219 struct fec_enet_priv_tx_q *txq = fep->tx_queue[queue_id];
220 struct fec_enet_priv_rx_q *rxq = fep->rx_queue[queue_id];
221 struct bufdesc_ex *ex_base;
222 struct bufdesc *base;
225 if (bdp >= txq->tx_bd_base) {
226 base = txq->tx_bd_base;
227 ring_size = txq->tx_ring_size;
228 ex_base = (struct bufdesc_ex *)txq->tx_bd_base;
230 base = rxq->rx_bd_base;
231 ring_size = rxq->rx_ring_size;
232 ex_base = (struct bufdesc_ex *)rxq->rx_bd_base;
236 return (struct bufdesc *)((ex_new_bd >= (ex_base + ring_size)) ?
237 ex_base : ex_new_bd);
239 return (new_bd >= (base + ring_size)) ?
244 struct bufdesc *fec_enet_get_prevdesc(struct bufdesc *bdp,
245 struct fec_enet_private *fep,
248 struct bufdesc *new_bd = bdp - 1;
249 struct bufdesc_ex *ex_new_bd = (struct bufdesc_ex *)bdp - 1;
250 struct fec_enet_priv_tx_q *txq = fep->tx_queue[queue_id];
251 struct fec_enet_priv_rx_q *rxq = fep->rx_queue[queue_id];
252 struct bufdesc_ex *ex_base;
253 struct bufdesc *base;
256 if (bdp >= txq->tx_bd_base) {
257 base = txq->tx_bd_base;
258 ring_size = txq->tx_ring_size;
259 ex_base = (struct bufdesc_ex *)txq->tx_bd_base;
261 base = rxq->rx_bd_base;
262 ring_size = rxq->rx_ring_size;
263 ex_base = (struct bufdesc_ex *)rxq->rx_bd_base;
267 return (struct bufdesc *)((ex_new_bd < ex_base) ?
268 (ex_new_bd + ring_size) : ex_new_bd);
270 return (new_bd < base) ? (new_bd + ring_size) : new_bd;
273 static int fec_enet_get_bd_index(struct bufdesc *base, struct bufdesc *bdp,
274 struct fec_enet_private *fep)
276 return ((const char *)bdp - (const char *)base) / fep->bufdesc_size;
279 static int fec_enet_get_free_txdesc_num(struct fec_enet_private *fep,
280 struct fec_enet_priv_tx_q *txq)
284 entries = ((const char *)txq->dirty_tx -
285 (const char *)txq->cur_tx) / fep->bufdesc_size - 1;
287 return entries > 0 ? entries : entries + txq->tx_ring_size;
290 static void *swap_buffer(void *bufaddr, int len)
293 unsigned int *buf = bufaddr;
295 for (i = 0; i < DIV_ROUND_UP(len, 4); i++, buf++)
296 *buf = cpu_to_be32(*buf);
301 static void fec_dump(struct net_device *ndev)
303 struct fec_enet_private *fep = netdev_priv(ndev);
305 struct fec_enet_priv_tx_q *txq;
308 netdev_info(ndev, "TX ring dump\n");
309 pr_info("Nr SC addr len SKB\n");
311 txq = fep->tx_queue[0];
312 bdp = txq->tx_bd_base;
315 pr_info("%3u %c%c 0x%04x 0x%08lx %4u %p\n",
317 bdp == txq->cur_tx ? 'S' : ' ',
318 bdp == txq->dirty_tx ? 'H' : ' ',
319 bdp->cbd_sc, bdp->cbd_bufaddr, bdp->cbd_datlen,
320 txq->tx_skbuff[index]);
321 bdp = fec_enet_get_nextdesc(bdp, fep, 0);
323 } while (bdp != txq->tx_bd_base);
326 static inline bool is_ipv4_pkt(struct sk_buff *skb)
328 return skb->protocol == htons(ETH_P_IP) && ip_hdr(skb)->version == 4;
332 fec_enet_clear_csum(struct sk_buff *skb, struct net_device *ndev)
334 /* Only run for packets requiring a checksum. */
335 if (skb->ip_summed != CHECKSUM_PARTIAL)
338 if (unlikely(skb_cow_head(skb, 0)))
341 if (is_ipv4_pkt(skb))
342 ip_hdr(skb)->check = 0;
343 *(__sum16 *)(skb->head + skb->csum_start + skb->csum_offset) = 0;
349 fec_enet_txq_submit_frag_skb(struct fec_enet_priv_tx_q *txq,
351 struct net_device *ndev)
353 struct fec_enet_private *fep = netdev_priv(ndev);
354 const struct platform_device_id *id_entry =
355 platform_get_device_id(fep->pdev);
356 struct bufdesc *bdp = txq->cur_tx;
357 struct bufdesc_ex *ebdp;
358 int nr_frags = skb_shinfo(skb)->nr_frags;
359 unsigned short queue = skb_get_queue_mapping(skb);
361 unsigned short status;
362 unsigned int estatus = 0;
363 skb_frag_t *this_frag;
369 for (frag = 0; frag < nr_frags; frag++) {
370 this_frag = &skb_shinfo(skb)->frags[frag];
371 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
372 ebdp = (struct bufdesc_ex *)bdp;
374 status = bdp->cbd_sc;
375 status &= ~BD_ENET_TX_STATS;
376 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
377 frag_len = skb_shinfo(skb)->frags[frag].size;
379 /* Handle the last BD specially */
380 if (frag == nr_frags - 1) {
381 status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
382 if (fep->bufdesc_ex) {
383 estatus |= BD_ENET_TX_INT;
384 if (unlikely(skb_shinfo(skb)->tx_flags &
385 SKBTX_HW_TSTAMP && fep->hwts_tx_en))
386 estatus |= BD_ENET_TX_TS;
390 if (fep->bufdesc_ex) {
391 if (id_entry->driver_data & FEC_QUIRK_HAS_AVB)
392 estatus |= FEC_TX_BD_FTYPE(queue);
393 if (skb->ip_summed == CHECKSUM_PARTIAL)
394 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
396 ebdp->cbd_esc = estatus;
399 bufaddr = page_address(this_frag->page.p) + this_frag->page_offset;
401 index = fec_enet_get_bd_index(txq->tx_bd_base, bdp, fep);
402 if (((unsigned long) bufaddr) & fep->tx_align ||
403 id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) {
404 memcpy(txq->tx_bounce[index], bufaddr, frag_len);
405 bufaddr = txq->tx_bounce[index];
407 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
408 swap_buffer(bufaddr, frag_len);
411 addr = dma_map_single(&fep->pdev->dev, bufaddr, frag_len,
413 if (dma_mapping_error(&fep->pdev->dev, addr)) {
414 dev_kfree_skb_any(skb);
416 netdev_err(ndev, "Tx DMA memory map failed\n");
417 goto dma_mapping_error;
420 bdp->cbd_bufaddr = addr;
421 bdp->cbd_datlen = frag_len;
422 bdp->cbd_sc = status;
431 for (i = 0; i < frag; i++) {
432 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
433 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
434 bdp->cbd_datlen, DMA_TO_DEVICE);
439 static int fec_enet_txq_submit_skb(struct fec_enet_priv_tx_q *txq,
440 struct sk_buff *skb, struct net_device *ndev)
442 struct fec_enet_private *fep = netdev_priv(ndev);
443 const struct platform_device_id *id_entry =
444 platform_get_device_id(fep->pdev);
445 int nr_frags = skb_shinfo(skb)->nr_frags;
446 struct bufdesc *bdp, *last_bdp;
449 unsigned short status;
450 unsigned short buflen;
451 unsigned short queue;
452 unsigned int estatus = 0;
457 entries_free = fec_enet_get_free_txdesc_num(fep, txq);
458 if (entries_free < MAX_SKB_FRAGS + 1) {
459 dev_kfree_skb_any(skb);
461 netdev_err(ndev, "NOT enough BD for SG!\n");
465 /* Protocol checksum off-load for TCP and UDP. */
466 if (fec_enet_clear_csum(skb, ndev)) {
467 dev_kfree_skb_any(skb);
471 /* Fill in a Tx ring entry */
473 status = bdp->cbd_sc;
474 status &= ~BD_ENET_TX_STATS;
476 /* Set buffer length and buffer pointer */
478 buflen = skb_headlen(skb);
480 queue = skb_get_queue_mapping(skb);
481 index = fec_enet_get_bd_index(txq->tx_bd_base, bdp, fep);
482 if (((unsigned long) bufaddr) & fep->tx_align ||
483 id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) {
484 memcpy(txq->tx_bounce[index], skb->data, buflen);
485 bufaddr = txq->tx_bounce[index];
487 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
488 swap_buffer(bufaddr, buflen);
491 /* Push the data cache so the CPM does not get stale memory data. */
492 addr = dma_map_single(&fep->pdev->dev, bufaddr, buflen, DMA_TO_DEVICE);
493 if (dma_mapping_error(&fep->pdev->dev, addr)) {
494 dev_kfree_skb_any(skb);
496 netdev_err(ndev, "Tx DMA memory map failed\n");
501 ret = fec_enet_txq_submit_frag_skb(txq, skb, ndev);
505 status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
506 if (fep->bufdesc_ex) {
507 estatus = BD_ENET_TX_INT;
508 if (unlikely(skb_shinfo(skb)->tx_flags &
509 SKBTX_HW_TSTAMP && fep->hwts_tx_en))
510 estatus |= BD_ENET_TX_TS;
514 if (fep->bufdesc_ex) {
516 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
518 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
520 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
522 if (id_entry->driver_data & FEC_QUIRK_HAS_AVB)
523 estatus |= FEC_TX_BD_FTYPE(queue);
525 if (skb->ip_summed == CHECKSUM_PARTIAL)
526 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
529 ebdp->cbd_esc = estatus;
532 last_bdp = txq->cur_tx;
533 index = fec_enet_get_bd_index(txq->tx_bd_base, last_bdp, fep);
534 /* Save skb pointer */
535 txq->tx_skbuff[index] = skb;
537 bdp->cbd_datlen = buflen;
538 bdp->cbd_bufaddr = addr;
540 /* Send it on its way. Tell FEC it's ready, interrupt when done,
541 * it's the last BD of the frame, and to put the CRC on the end.
543 status |= (BD_ENET_TX_READY | BD_ENET_TX_TC);
544 bdp->cbd_sc = status;
546 /* If this was the last BD in the ring, start at the beginning again. */
547 bdp = fec_enet_get_nextdesc(last_bdp, fep, queue);
549 skb_tx_timestamp(skb);
553 /* Trigger transmission start */
554 writel(0, fep->hwp + FEC_X_DES_ACTIVE(queue));
560 fec_enet_txq_put_data_tso(struct fec_enet_priv_tx_q *txq, struct sk_buff *skb,
561 struct net_device *ndev,
562 struct bufdesc *bdp, int index, char *data,
563 int size, bool last_tcp, bool is_last)
565 struct fec_enet_private *fep = netdev_priv(ndev);
566 const struct platform_device_id *id_entry =
567 platform_get_device_id(fep->pdev);
568 struct bufdesc_ex *ebdp = container_of(bdp, struct bufdesc_ex, desc);
569 unsigned short queue = skb_get_queue_mapping(skb);
570 unsigned short status;
571 unsigned int estatus = 0;
574 status = bdp->cbd_sc;
575 status &= ~BD_ENET_TX_STATS;
577 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
579 if (((unsigned long) data) & fep->tx_align ||
580 id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) {
581 memcpy(txq->tx_bounce[index], data, size);
582 data = txq->tx_bounce[index];
584 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
585 swap_buffer(data, size);
588 addr = dma_map_single(&fep->pdev->dev, data, size, DMA_TO_DEVICE);
589 if (dma_mapping_error(&fep->pdev->dev, addr)) {
590 dev_kfree_skb_any(skb);
592 netdev_err(ndev, "Tx DMA memory map failed\n");
593 return NETDEV_TX_BUSY;
596 bdp->cbd_datlen = size;
597 bdp->cbd_bufaddr = addr;
599 if (fep->bufdesc_ex) {
600 if (id_entry->driver_data & FEC_QUIRK_HAS_AVB)
601 estatus |= FEC_TX_BD_FTYPE(queue);
602 if (skb->ip_summed == CHECKSUM_PARTIAL)
603 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
605 ebdp->cbd_esc = estatus;
608 /* Handle the last BD specially */
610 status |= (BD_ENET_TX_LAST | BD_ENET_TX_TC);
612 status |= BD_ENET_TX_INTR;
614 ebdp->cbd_esc |= BD_ENET_TX_INT;
617 bdp->cbd_sc = status;
623 fec_enet_txq_put_hdr_tso(struct fec_enet_priv_tx_q *txq,
624 struct sk_buff *skb, struct net_device *ndev,
625 struct bufdesc *bdp, int index)
627 struct fec_enet_private *fep = netdev_priv(ndev);
628 const struct platform_device_id *id_entry =
629 platform_get_device_id(fep->pdev);
630 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
631 struct bufdesc_ex *ebdp = container_of(bdp, struct bufdesc_ex, desc);
632 unsigned short queue = skb_get_queue_mapping(skb);
634 unsigned long dmabuf;
635 unsigned short status;
636 unsigned int estatus = 0;
638 status = bdp->cbd_sc;
639 status &= ~BD_ENET_TX_STATS;
640 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
642 bufaddr = txq->tso_hdrs + index * TSO_HEADER_SIZE;
643 dmabuf = txq->tso_hdrs_dma + index * TSO_HEADER_SIZE;
644 if (((unsigned long)bufaddr) & fep->tx_align ||
645 id_entry->driver_data & FEC_QUIRK_SWAP_FRAME) {
646 memcpy(txq->tx_bounce[index], skb->data, hdr_len);
647 bufaddr = txq->tx_bounce[index];
649 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
650 swap_buffer(bufaddr, hdr_len);
652 dmabuf = dma_map_single(&fep->pdev->dev, bufaddr,
653 hdr_len, DMA_TO_DEVICE);
654 if (dma_mapping_error(&fep->pdev->dev, dmabuf)) {
655 dev_kfree_skb_any(skb);
657 netdev_err(ndev, "Tx DMA memory map failed\n");
658 return NETDEV_TX_BUSY;
662 bdp->cbd_bufaddr = dmabuf;
663 bdp->cbd_datlen = hdr_len;
665 if (fep->bufdesc_ex) {
666 if (id_entry->driver_data & FEC_QUIRK_HAS_AVB)
667 estatus |= FEC_TX_BD_FTYPE(queue);
668 if (skb->ip_summed == CHECKSUM_PARTIAL)
669 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
671 ebdp->cbd_esc = estatus;
674 bdp->cbd_sc = status;
679 static int fec_enet_txq_submit_tso(struct fec_enet_priv_tx_q *txq,
681 struct net_device *ndev)
683 struct fec_enet_private *fep = netdev_priv(ndev);
684 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
685 int total_len, data_left;
686 struct bufdesc *bdp = txq->cur_tx;
687 unsigned short queue = skb_get_queue_mapping(skb);
689 unsigned int index = 0;
691 const struct platform_device_id *id_entry =
692 platform_get_device_id(fep->pdev);
694 if (tso_count_descs(skb) >= fec_enet_get_free_txdesc_num(fep, txq)) {
695 dev_kfree_skb_any(skb);
697 netdev_err(ndev, "NOT enough BD for TSO!\n");
701 /* Protocol checksum off-load for TCP and UDP. */
702 if (fec_enet_clear_csum(skb, ndev)) {
703 dev_kfree_skb_any(skb);
707 /* Initialize the TSO handler, and prepare the first payload */
708 tso_start(skb, &tso);
710 total_len = skb->len - hdr_len;
711 while (total_len > 0) {
714 index = fec_enet_get_bd_index(txq->tx_bd_base, bdp, fep);
715 data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
716 total_len -= data_left;
718 /* prepare packet headers: MAC + IP + TCP */
719 hdr = txq->tso_hdrs + index * TSO_HEADER_SIZE;
720 tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
721 ret = fec_enet_txq_put_hdr_tso(txq, skb, ndev, bdp, index);
725 while (data_left > 0) {
728 size = min_t(int, tso.size, data_left);
729 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
730 index = fec_enet_get_bd_index(txq->tx_bd_base,
732 ret = fec_enet_txq_put_data_tso(txq, skb, ndev,
741 tso_build_data(skb, &tso, size);
744 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
747 /* Save skb pointer */
748 txq->tx_skbuff[index] = skb;
750 skb_tx_timestamp(skb);
753 /* Trigger transmission start */
754 if (!(id_entry->driver_data & FEC_QUIRK_ERR007885) ||
755 !readl(fep->hwp + FEC_X_DES_ACTIVE(queue)) ||
756 !readl(fep->hwp + FEC_X_DES_ACTIVE(queue)) ||
757 !readl(fep->hwp + FEC_X_DES_ACTIVE(queue)) ||
758 !readl(fep->hwp + FEC_X_DES_ACTIVE(queue)))
759 writel(0, fep->hwp + FEC_X_DES_ACTIVE(queue));
764 /* TODO: Release all used data descriptors for TSO */
769 fec_enet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
771 struct fec_enet_private *fep = netdev_priv(ndev);
773 unsigned short queue;
774 struct fec_enet_priv_tx_q *txq;
775 struct netdev_queue *nq;
778 queue = skb_get_queue_mapping(skb);
779 txq = fep->tx_queue[queue];
780 nq = netdev_get_tx_queue(ndev, queue);
783 ret = fec_enet_txq_submit_tso(txq, skb, ndev);
785 ret = fec_enet_txq_submit_skb(txq, skb, ndev);
789 entries_free = fec_enet_get_free_txdesc_num(fep, txq);
790 if (entries_free <= txq->tx_stop_threshold)
791 netif_tx_stop_queue(nq);
796 /* Init RX & TX buffer descriptors
798 static void fec_enet_bd_init(struct net_device *dev)
800 struct fec_enet_private *fep = netdev_priv(dev);
801 struct fec_enet_priv_tx_q *txq;
802 struct fec_enet_priv_rx_q *rxq;
807 for (q = 0; q < fep->num_rx_queues; q++) {
808 /* Initialize the receive buffer descriptors. */
809 rxq = fep->rx_queue[q];
810 bdp = rxq->rx_bd_base;
812 for (i = 0; i < rxq->rx_ring_size; i++) {
814 /* Initialize the BD for every fragment in the page. */
815 if (bdp->cbd_bufaddr)
816 bdp->cbd_sc = BD_ENET_RX_EMPTY;
819 bdp = fec_enet_get_nextdesc(bdp, fep, q);
822 /* Set the last buffer to wrap */
823 bdp = fec_enet_get_prevdesc(bdp, fep, q);
824 bdp->cbd_sc |= BD_SC_WRAP;
826 rxq->cur_rx = rxq->rx_bd_base;
829 for (q = 0; q < fep->num_tx_queues; q++) {
830 /* ...and the same for transmit */
831 txq = fep->tx_queue[q];
832 bdp = txq->tx_bd_base;
835 for (i = 0; i < txq->tx_ring_size; i++) {
836 /* Initialize the BD for every fragment in the page. */
838 if (txq->tx_skbuff[i]) {
839 dev_kfree_skb_any(txq->tx_skbuff[i]);
840 txq->tx_skbuff[i] = NULL;
842 bdp->cbd_bufaddr = 0;
843 bdp = fec_enet_get_nextdesc(bdp, fep, q);
846 /* Set the last buffer to wrap */
847 bdp = fec_enet_get_prevdesc(bdp, fep, q);
848 bdp->cbd_sc |= BD_SC_WRAP;
853 static void fec_enet_active_rxring(struct net_device *ndev)
855 struct fec_enet_private *fep = netdev_priv(ndev);
858 for (i = 0; i < fep->num_rx_queues; i++)
859 writel(0, fep->hwp + FEC_R_DES_ACTIVE(i));
862 static void fec_enet_enable_ring(struct net_device *ndev)
864 struct fec_enet_private *fep = netdev_priv(ndev);
865 struct fec_enet_priv_tx_q *txq;
866 struct fec_enet_priv_rx_q *rxq;
869 for (i = 0; i < fep->num_rx_queues; i++) {
870 rxq = fep->rx_queue[i];
871 writel(rxq->bd_dma, fep->hwp + FEC_R_DES_START(i));
875 writel(RCMR_MATCHEN | RCMR_CMP(i),
876 fep->hwp + FEC_RCMR(i));
879 for (i = 0; i < fep->num_tx_queues; i++) {
880 txq = fep->tx_queue[i];
881 writel(txq->bd_dma, fep->hwp + FEC_X_DES_START(i));
885 writel(DMA_CLASS_EN | IDLE_SLOPE(i),
886 fep->hwp + FEC_DMA_CFG(i));
890 static void fec_enet_reset_skb(struct net_device *ndev)
892 struct fec_enet_private *fep = netdev_priv(ndev);
893 struct fec_enet_priv_tx_q *txq;
896 for (i = 0; i < fep->num_tx_queues; i++) {
897 txq = fep->tx_queue[i];
899 for (j = 0; j < txq->tx_ring_size; j++) {
900 if (txq->tx_skbuff[j]) {
901 dev_kfree_skb_any(txq->tx_skbuff[j]);
902 txq->tx_skbuff[j] = NULL;
909 * This function is called to start or restart the FEC during a link
910 * change, transmit timeout, or to reconfigure the FEC. The network
911 * packet processing for this device must be stopped before this call.
914 fec_restart(struct net_device *ndev)
916 struct fec_enet_private *fep = netdev_priv(ndev);
917 const struct platform_device_id *id_entry =
918 platform_get_device_id(fep->pdev);
921 u32 rcntl = OPT_FRAME_SIZE | 0x04;
922 u32 ecntl = 0x2; /* ETHEREN */
924 /* Whack a reset. We should wait for this.
925 * For i.MX6SX SOC, enet use AXI bus, we use disable MAC
926 * instead of reset MAC itself.
928 if (id_entry && id_entry->driver_data & FEC_QUIRK_HAS_AVB) {
929 writel(0, fep->hwp + FEC_ECNTRL);
931 writel(1, fep->hwp + FEC_ECNTRL);
936 * enet-mac reset will reset mac address registers too,
937 * so need to reconfigure it.
939 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
940 memcpy(&temp_mac, ndev->dev_addr, ETH_ALEN);
941 writel(cpu_to_be32(temp_mac[0]), fep->hwp + FEC_ADDR_LOW);
942 writel(cpu_to_be32(temp_mac[1]), fep->hwp + FEC_ADDR_HIGH);
945 /* Clear any outstanding interrupt. */
946 writel(0xffc00000, fep->hwp + FEC_IEVENT);
948 /* Set maximum receive buffer size. */
949 writel(PKT_MAXBLR_SIZE, fep->hwp + FEC_R_BUFF_SIZE);
951 fec_enet_bd_init(ndev);
953 fec_enet_enable_ring(ndev);
955 /* Reset tx SKB buffers. */
956 fec_enet_reset_skb(ndev);
958 /* Enable MII mode */
959 if (fep->full_duplex == DUPLEX_FULL) {
961 writel(0x04, fep->hwp + FEC_X_CNTRL);
965 writel(0x0, fep->hwp + FEC_X_CNTRL);
969 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
971 #if !defined(CONFIG_M5272)
972 /* set RX checksum */
973 val = readl(fep->hwp + FEC_RACC);
974 if (fep->csum_flags & FLAG_RX_CSUM_ENABLED)
975 val |= FEC_RACC_OPTIONS;
977 val &= ~FEC_RACC_OPTIONS;
978 writel(val, fep->hwp + FEC_RACC);
982 * The phy interface and speed need to get configured
983 * differently on enet-mac.
985 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
986 /* Enable flow control and length check */
987 rcntl |= 0x40000000 | 0x00000020;
989 /* RGMII, RMII or MII */
990 if (fep->phy_interface == PHY_INTERFACE_MODE_RGMII)
992 else if (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
997 /* 1G, 100M or 10M */
999 if (fep->phy_dev->speed == SPEED_1000)
1001 else if (fep->phy_dev->speed == SPEED_100)
1007 #ifdef FEC_MIIGSK_ENR
1008 if (id_entry->driver_data & FEC_QUIRK_USE_GASKET) {
1010 /* disable the gasket and wait */
1011 writel(0, fep->hwp + FEC_MIIGSK_ENR);
1012 while (readl(fep->hwp + FEC_MIIGSK_ENR) & 4)
1016 * configure the gasket:
1017 * RMII, 50 MHz, no loopback, no echo
1018 * MII, 25 MHz, no loopback, no echo
1020 cfgr = (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
1021 ? BM_MIIGSK_CFGR_RMII : BM_MIIGSK_CFGR_MII;
1022 if (fep->phy_dev && fep->phy_dev->speed == SPEED_10)
1023 cfgr |= BM_MIIGSK_CFGR_FRCONT_10M;
1024 writel(cfgr, fep->hwp + FEC_MIIGSK_CFGR);
1026 /* re-enable the gasket */
1027 writel(2, fep->hwp + FEC_MIIGSK_ENR);
1032 #if !defined(CONFIG_M5272)
1033 /* enable pause frame*/
1034 if ((fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) ||
1035 ((fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) &&
1036 fep->phy_dev && fep->phy_dev->pause)) {
1037 rcntl |= FEC_ENET_FCE;
1039 /* set FIFO threshold parameter to reduce overrun */
1040 writel(FEC_ENET_RSEM_V, fep->hwp + FEC_R_FIFO_RSEM);
1041 writel(FEC_ENET_RSFL_V, fep->hwp + FEC_R_FIFO_RSFL);
1042 writel(FEC_ENET_RAEM_V, fep->hwp + FEC_R_FIFO_RAEM);
1043 writel(FEC_ENET_RAFL_V, fep->hwp + FEC_R_FIFO_RAFL);
1046 writel(FEC_ENET_OPD_V, fep->hwp + FEC_OPD);
1048 rcntl &= ~FEC_ENET_FCE;
1050 #endif /* !defined(CONFIG_M5272) */
1052 writel(rcntl, fep->hwp + FEC_R_CNTRL);
1054 /* Setup multicast filter. */
1055 set_multicast_list(ndev);
1056 #ifndef CONFIG_M5272
1057 writel(0, fep->hwp + FEC_HASH_TABLE_HIGH);
1058 writel(0, fep->hwp + FEC_HASH_TABLE_LOW);
1061 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
1062 /* enable ENET endian swap */
1064 /* enable ENET store and forward mode */
1065 writel(1 << 8, fep->hwp + FEC_X_WMRK);
1068 if (fep->bufdesc_ex)
1071 #ifndef CONFIG_M5272
1072 /* Enable the MIB statistic event counters */
1073 writel(0 << 31, fep->hwp + FEC_MIB_CTRLSTAT);
1076 /* And last, enable the transmit and receive processing */
1077 writel(ecntl, fep->hwp + FEC_ECNTRL);
1078 fec_enet_active_rxring(ndev);
1080 if (fep->bufdesc_ex)
1081 fec_ptp_start_cyclecounter(ndev);
1083 /* Enable interrupts we wish to service */
1084 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1086 /* Init the interrupt coalescing */
1087 fec_enet_itr_coal_init(ndev);
1092 fec_stop(struct net_device *ndev)
1094 struct fec_enet_private *fep = netdev_priv(ndev);
1095 const struct platform_device_id *id_entry =
1096 platform_get_device_id(fep->pdev);
1097 u32 rmii_mode = readl(fep->hwp + FEC_R_CNTRL) & (1 << 8);
1099 /* We cannot expect a graceful transmit stop without link !!! */
1101 writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */
1103 if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA))
1104 netdev_err(ndev, "Graceful transmit stop did not complete!\n");
1107 /* Whack a reset. We should wait for this.
1108 * For i.MX6SX SOC, enet use AXI bus, we use disable MAC
1109 * instead of reset MAC itself.
1111 if (id_entry && id_entry->driver_data & FEC_QUIRK_HAS_AVB) {
1112 writel(0, fep->hwp + FEC_ECNTRL);
1114 writel(1, fep->hwp + FEC_ECNTRL);
1117 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
1118 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1120 /* We have to keep ENET enabled to have MII interrupt stay working */
1121 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
1122 writel(2, fep->hwp + FEC_ECNTRL);
1123 writel(rmii_mode, fep->hwp + FEC_R_CNTRL);
1129 fec_timeout(struct net_device *ndev)
1131 struct fec_enet_private *fep = netdev_priv(ndev);
1135 ndev->stats.tx_errors++;
1137 schedule_work(&fep->tx_timeout_work);
1140 static void fec_enet_timeout_work(struct work_struct *work)
1142 struct fec_enet_private *fep =
1143 container_of(work, struct fec_enet_private, tx_timeout_work);
1144 struct net_device *ndev = fep->netdev;
1147 if (netif_device_present(ndev) || netif_running(ndev)) {
1148 napi_disable(&fep->napi);
1149 netif_tx_lock_bh(ndev);
1151 netif_wake_queue(ndev);
1152 netif_tx_unlock_bh(ndev);
1153 napi_enable(&fep->napi);
1159 fec_enet_hwtstamp(struct fec_enet_private *fep, unsigned ts,
1160 struct skb_shared_hwtstamps *hwtstamps)
1162 unsigned long flags;
1165 spin_lock_irqsave(&fep->tmreg_lock, flags);
1166 ns = timecounter_cyc2time(&fep->tc, ts);
1167 spin_unlock_irqrestore(&fep->tmreg_lock, flags);
1169 memset(hwtstamps, 0, sizeof(*hwtstamps));
1170 hwtstamps->hwtstamp = ns_to_ktime(ns);
1174 fec_enet_tx_queue(struct net_device *ndev, u16 queue_id)
1176 struct fec_enet_private *fep;
1177 struct bufdesc *bdp;
1178 unsigned short status;
1179 struct sk_buff *skb;
1180 struct fec_enet_priv_tx_q *txq;
1181 struct netdev_queue *nq;
1185 fep = netdev_priv(ndev);
1187 queue_id = FEC_ENET_GET_QUQUE(queue_id);
1189 txq = fep->tx_queue[queue_id];
1190 /* get next bdp of dirty_tx */
1191 nq = netdev_get_tx_queue(ndev, queue_id);
1192 bdp = txq->dirty_tx;
1194 /* get next bdp of dirty_tx */
1195 bdp = fec_enet_get_nextdesc(bdp, fep, queue_id);
1197 while (((status = bdp->cbd_sc) & BD_ENET_TX_READY) == 0) {
1199 /* current queue is empty */
1200 if (bdp == txq->cur_tx)
1203 index = fec_enet_get_bd_index(txq->tx_bd_base, bdp, fep);
1205 skb = txq->tx_skbuff[index];
1206 txq->tx_skbuff[index] = NULL;
1207 if (!IS_TSO_HEADER(txq, bdp->cbd_bufaddr))
1208 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
1209 bdp->cbd_datlen, DMA_TO_DEVICE);
1210 bdp->cbd_bufaddr = 0;
1212 bdp = fec_enet_get_nextdesc(bdp, fep, queue_id);
1216 /* Check for errors. */
1217 if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC |
1218 BD_ENET_TX_RL | BD_ENET_TX_UN |
1220 ndev->stats.tx_errors++;
1221 if (status & BD_ENET_TX_HB) /* No heartbeat */
1222 ndev->stats.tx_heartbeat_errors++;
1223 if (status & BD_ENET_TX_LC) /* Late collision */
1224 ndev->stats.tx_window_errors++;
1225 if (status & BD_ENET_TX_RL) /* Retrans limit */
1226 ndev->stats.tx_aborted_errors++;
1227 if (status & BD_ENET_TX_UN) /* Underrun */
1228 ndev->stats.tx_fifo_errors++;
1229 if (status & BD_ENET_TX_CSL) /* Carrier lost */
1230 ndev->stats.tx_carrier_errors++;
1232 ndev->stats.tx_packets++;
1233 ndev->stats.tx_bytes += skb->len;
1236 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) &&
1238 struct skb_shared_hwtstamps shhwtstamps;
1239 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
1241 fec_enet_hwtstamp(fep, ebdp->ts, &shhwtstamps);
1242 skb_tstamp_tx(skb, &shhwtstamps);
1245 /* Deferred means some collisions occurred during transmit,
1246 * but we eventually sent the packet OK.
1248 if (status & BD_ENET_TX_DEF)
1249 ndev->stats.collisions++;
1251 /* Free the sk buffer associated with this last transmit */
1252 dev_kfree_skb_any(skb);
1254 txq->dirty_tx = bdp;
1256 /* Update pointer to next buffer descriptor to be transmitted */
1257 bdp = fec_enet_get_nextdesc(bdp, fep, queue_id);
1259 /* Since we have freed up a buffer, the ring is no longer full
1261 if (netif_queue_stopped(ndev)) {
1262 entries_free = fec_enet_get_free_txdesc_num(fep, txq);
1263 if (entries_free >= txq->tx_wake_threshold)
1264 netif_tx_wake_queue(nq);
1268 /* ERR006538: Keep the transmitter going */
1269 if (bdp != txq->cur_tx &&
1270 readl(fep->hwp + FEC_X_DES_ACTIVE(queue_id)) == 0)
1271 writel(0, fep->hwp + FEC_X_DES_ACTIVE(queue_id));
1275 fec_enet_tx(struct net_device *ndev)
1277 struct fec_enet_private *fep = netdev_priv(ndev);
1279 /* First process class A queue, then Class B and Best Effort queue */
1280 for_each_set_bit(queue_id, &fep->work_tx, FEC_ENET_MAX_TX_QS) {
1281 clear_bit(queue_id, &fep->work_tx);
1282 fec_enet_tx_queue(ndev, queue_id);
1288 fec_enet_new_rxbdp(struct net_device *ndev, struct bufdesc *bdp, struct sk_buff *skb)
1290 struct fec_enet_private *fep = netdev_priv(ndev);
1293 off = ((unsigned long)skb->data) & fep->rx_align;
1295 skb_reserve(skb, fep->rx_align + 1 - off);
1297 bdp->cbd_bufaddr = dma_map_single(&fep->pdev->dev, skb->data,
1298 FEC_ENET_RX_FRSIZE - fep->rx_align,
1300 if (dma_mapping_error(&fep->pdev->dev, bdp->cbd_bufaddr)) {
1301 if (net_ratelimit())
1302 netdev_err(ndev, "Rx DMA memory map failed\n");
1309 static bool fec_enet_copybreak(struct net_device *ndev, struct sk_buff **skb,
1310 struct bufdesc *bdp, u32 length)
1312 struct fec_enet_private *fep = netdev_priv(ndev);
1313 struct sk_buff *new_skb;
1315 if (length > fep->rx_copybreak)
1318 new_skb = netdev_alloc_skb(ndev, length);
1322 dma_sync_single_for_cpu(&fep->pdev->dev, bdp->cbd_bufaddr,
1323 FEC_ENET_RX_FRSIZE - fep->rx_align,
1325 memcpy(new_skb->data, (*skb)->data, length);
1331 /* During a receive, the cur_rx points to the current incoming buffer.
1332 * When we update through the ring, if the next incoming buffer has
1333 * not been given to the system, we just set the empty indicator,
1334 * effectively tossing the packet.
1337 fec_enet_rx_queue(struct net_device *ndev, int budget, u16 queue_id)
1339 struct fec_enet_private *fep = netdev_priv(ndev);
1340 const struct platform_device_id *id_entry =
1341 platform_get_device_id(fep->pdev);
1342 struct fec_enet_priv_rx_q *rxq;
1343 struct bufdesc *bdp;
1344 unsigned short status;
1345 struct sk_buff *skb_new = NULL;
1346 struct sk_buff *skb;
1349 int pkt_received = 0;
1350 struct bufdesc_ex *ebdp = NULL;
1351 bool vlan_packet_rcvd = false;
1359 queue_id = FEC_ENET_GET_QUQUE(queue_id);
1360 rxq = fep->rx_queue[queue_id];
1362 /* First, grab all of the stats for the incoming packet.
1363 * These get messed up if we get called due to a busy condition.
1367 while (!((status = bdp->cbd_sc) & BD_ENET_RX_EMPTY)) {
1369 if (pkt_received >= budget)
1373 /* Since we have allocated space to hold a complete frame,
1374 * the last indicator should be set.
1376 if ((status & BD_ENET_RX_LAST) == 0)
1377 netdev_err(ndev, "rcv is not +last\n");
1380 /* Check for errors. */
1381 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
1382 BD_ENET_RX_CR | BD_ENET_RX_OV)) {
1383 ndev->stats.rx_errors++;
1384 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH)) {
1385 /* Frame too long or too short. */
1386 ndev->stats.rx_length_errors++;
1388 if (status & BD_ENET_RX_NO) /* Frame alignment */
1389 ndev->stats.rx_frame_errors++;
1390 if (status & BD_ENET_RX_CR) /* CRC Error */
1391 ndev->stats.rx_crc_errors++;
1392 if (status & BD_ENET_RX_OV) /* FIFO overrun */
1393 ndev->stats.rx_fifo_errors++;
1396 /* Report late collisions as a frame error.
1397 * On this error, the BD is closed, but we don't know what we
1398 * have in the buffer. So, just drop this frame on the floor.
1400 if (status & BD_ENET_RX_CL) {
1401 ndev->stats.rx_errors++;
1402 ndev->stats.rx_frame_errors++;
1403 goto rx_processing_done;
1406 /* Process the incoming frame. */
1407 ndev->stats.rx_packets++;
1408 pkt_len = bdp->cbd_datlen;
1409 ndev->stats.rx_bytes += pkt_len;
1411 index = fec_enet_get_bd_index(rxq->rx_bd_base, bdp, fep);
1412 skb = rxq->rx_skbuff[index];
1414 /* The packet length includes FCS, but we don't want to
1415 * include that when passing upstream as it messes up
1416 * bridging applications.
1418 is_copybreak = fec_enet_copybreak(ndev, &skb, bdp, pkt_len - 4);
1419 if (!is_copybreak) {
1420 skb_new = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
1421 if (unlikely(!skb_new)) {
1422 ndev->stats.rx_dropped++;
1423 goto rx_processing_done;
1425 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
1426 FEC_ENET_RX_FRSIZE - fep->rx_align,
1430 prefetch(skb->data - NET_IP_ALIGN);
1431 skb_put(skb, pkt_len - 4);
1433 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
1434 swap_buffer(data, pkt_len);
1436 /* Extract the enhanced buffer descriptor */
1438 if (fep->bufdesc_ex)
1439 ebdp = (struct bufdesc_ex *)bdp;
1441 /* If this is a VLAN packet remove the VLAN Tag */
1442 vlan_packet_rcvd = false;
1443 if ((ndev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
1444 fep->bufdesc_ex && (ebdp->cbd_esc & BD_ENET_RX_VLAN)) {
1445 /* Push and remove the vlan tag */
1446 struct vlan_hdr *vlan_header =
1447 (struct vlan_hdr *) (data + ETH_HLEN);
1448 vlan_tag = ntohs(vlan_header->h_vlan_TCI);
1450 vlan_packet_rcvd = true;
1452 skb_copy_to_linear_data_offset(skb, VLAN_HLEN,
1453 data, (2 * ETH_ALEN));
1454 skb_pull(skb, VLAN_HLEN);
1457 skb->protocol = eth_type_trans(skb, ndev);
1459 /* Get receive timestamp from the skb */
1460 if (fep->hwts_rx_en && fep->bufdesc_ex)
1461 fec_enet_hwtstamp(fep, ebdp->ts,
1462 skb_hwtstamps(skb));
1464 if (fep->bufdesc_ex &&
1465 (fep->csum_flags & FLAG_RX_CSUM_ENABLED)) {
1466 if (!(ebdp->cbd_esc & FLAG_RX_CSUM_ERROR)) {
1467 /* don't check it */
1468 skb->ip_summed = CHECKSUM_UNNECESSARY;
1470 skb_checksum_none_assert(skb);
1474 /* Handle received VLAN packets */
1475 if (vlan_packet_rcvd)
1476 __vlan_hwaccel_put_tag(skb,
1480 napi_gro_receive(&fep->napi, skb);
1483 dma_sync_single_for_device(&fep->pdev->dev, bdp->cbd_bufaddr,
1484 FEC_ENET_RX_FRSIZE - fep->rx_align,
1487 rxq->rx_skbuff[index] = skb_new;
1488 fec_enet_new_rxbdp(ndev, bdp, skb_new);
1492 /* Clear the status flags for this buffer */
1493 status &= ~BD_ENET_RX_STATS;
1495 /* Mark the buffer empty */
1496 status |= BD_ENET_RX_EMPTY;
1497 bdp->cbd_sc = status;
1499 if (fep->bufdesc_ex) {
1500 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
1502 ebdp->cbd_esc = BD_ENET_RX_INT;
1507 /* Update BD pointer to next entry */
1508 bdp = fec_enet_get_nextdesc(bdp, fep, queue_id);
1510 /* Doing this here will keep the FEC running while we process
1511 * incoming frames. On a heavily loaded network, we should be
1512 * able to keep up at the expense of system resources.
1514 writel(0, fep->hwp + FEC_R_DES_ACTIVE(queue_id));
1517 return pkt_received;
1521 fec_enet_rx(struct net_device *ndev, int budget)
1523 int pkt_received = 0;
1525 struct fec_enet_private *fep = netdev_priv(ndev);
1527 for_each_set_bit(queue_id, &fep->work_rx, FEC_ENET_MAX_RX_QS) {
1528 clear_bit(queue_id, &fep->work_rx);
1529 pkt_received += fec_enet_rx_queue(ndev,
1530 budget - pkt_received, queue_id);
1532 return pkt_received;
1536 fec_enet_collect_events(struct fec_enet_private *fep, uint int_events)
1538 if (int_events == 0)
1541 if (int_events & FEC_ENET_RXF)
1542 fep->work_rx |= (1 << 2);
1543 if (int_events & FEC_ENET_RXF_1)
1544 fep->work_rx |= (1 << 0);
1545 if (int_events & FEC_ENET_RXF_2)
1546 fep->work_rx |= (1 << 1);
1548 if (int_events & FEC_ENET_TXF)
1549 fep->work_tx |= (1 << 2);
1550 if (int_events & FEC_ENET_TXF_1)
1551 fep->work_tx |= (1 << 0);
1552 if (int_events & FEC_ENET_TXF_2)
1553 fep->work_tx |= (1 << 1);
1559 fec_enet_interrupt(int irq, void *dev_id)
1561 struct net_device *ndev = dev_id;
1562 struct fec_enet_private *fep = netdev_priv(ndev);
1563 const unsigned napi_mask = FEC_ENET_RXF | FEC_ENET_TXF;
1565 irqreturn_t ret = IRQ_NONE;
1567 int_events = readl(fep->hwp + FEC_IEVENT);
1568 writel(int_events & ~napi_mask, fep->hwp + FEC_IEVENT);
1569 fec_enet_collect_events(fep, int_events);
1571 if (int_events & napi_mask) {
1574 /* Disable the NAPI interrupts */
1575 writel(FEC_ENET_MII, fep->hwp + FEC_IMASK);
1576 napi_schedule(&fep->napi);
1579 if (int_events & FEC_ENET_MII) {
1581 complete(&fep->mdio_done);
1584 fec_ptp_check_pps_event(fep);
1589 static int fec_enet_rx_napi(struct napi_struct *napi, int budget)
1591 struct net_device *ndev = napi->dev;
1592 struct fec_enet_private *fep = netdev_priv(ndev);
1596 * Clear any pending transmit or receive interrupts before
1597 * processing the rings to avoid racing with the hardware.
1599 writel(FEC_ENET_RXF | FEC_ENET_TXF, fep->hwp + FEC_IEVENT);
1601 pkts = fec_enet_rx(ndev, budget);
1605 if (pkts < budget) {
1606 napi_complete(napi);
1607 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1612 /* ------------------------------------------------------------------------- */
1613 static void fec_get_mac(struct net_device *ndev)
1615 struct fec_enet_private *fep = netdev_priv(ndev);
1616 struct fec_platform_data *pdata = dev_get_platdata(&fep->pdev->dev);
1617 unsigned char *iap, tmpaddr[ETH_ALEN];
1620 * try to get mac address in following order:
1622 * 1) module parameter via kernel command line in form
1623 * fec.macaddr=0x00,0x04,0x9f,0x01,0x30,0xe0
1628 * 2) from device tree data
1630 if (!is_valid_ether_addr(iap)) {
1631 struct device_node *np = fep->pdev->dev.of_node;
1633 const char *mac = of_get_mac_address(np);
1635 iap = (unsigned char *) mac;
1640 * 3) from flash or fuse (via platform data)
1642 if (!is_valid_ether_addr(iap)) {
1645 iap = (unsigned char *)FEC_FLASHMAC;
1648 iap = (unsigned char *)&pdata->mac;
1653 * 4) FEC mac registers set by bootloader
1655 if (!is_valid_ether_addr(iap)) {
1656 *((__be32 *) &tmpaddr[0]) =
1657 cpu_to_be32(readl(fep->hwp + FEC_ADDR_LOW));
1658 *((__be16 *) &tmpaddr[4]) =
1659 cpu_to_be16(readl(fep->hwp + FEC_ADDR_HIGH) >> 16);
1664 * 5) random mac address
1666 if (!is_valid_ether_addr(iap)) {
1667 /* Report it and use a random ethernet address instead */
1668 netdev_err(ndev, "Invalid MAC address: %pM\n", iap);
1669 eth_hw_addr_random(ndev);
1670 netdev_info(ndev, "Using random MAC address: %pM\n",
1675 memcpy(ndev->dev_addr, iap, ETH_ALEN);
1677 /* Adjust MAC if using macaddr */
1679 ndev->dev_addr[ETH_ALEN-1] = macaddr[ETH_ALEN-1] + fep->dev_id;
1682 /* ------------------------------------------------------------------------- */
1687 static void fec_enet_adjust_link(struct net_device *ndev)
1689 struct fec_enet_private *fep = netdev_priv(ndev);
1690 struct phy_device *phy_dev = fep->phy_dev;
1691 int status_change = 0;
1693 /* Prevent a state halted on mii error */
1694 if (fep->mii_timeout && phy_dev->state == PHY_HALTED) {
1695 phy_dev->state = PHY_RESUMING;
1700 * If the netdev is down, or is going down, we're not interested
1701 * in link state events, so just mark our idea of the link as down
1702 * and ignore the event.
1704 if (!netif_running(ndev) || !netif_device_present(ndev)) {
1706 } else if (phy_dev->link) {
1708 fep->link = phy_dev->link;
1712 if (fep->full_duplex != phy_dev->duplex) {
1713 fep->full_duplex = phy_dev->duplex;
1717 if (phy_dev->speed != fep->speed) {
1718 fep->speed = phy_dev->speed;
1722 /* if any of the above changed restart the FEC */
1723 if (status_change) {
1724 napi_disable(&fep->napi);
1725 netif_tx_lock_bh(ndev);
1727 netif_wake_queue(ndev);
1728 netif_tx_unlock_bh(ndev);
1729 napi_enable(&fep->napi);
1733 napi_disable(&fep->napi);
1734 netif_tx_lock_bh(ndev);
1736 netif_tx_unlock_bh(ndev);
1737 napi_enable(&fep->napi);
1738 fep->link = phy_dev->link;
1744 phy_print_status(phy_dev);
1747 static int fec_enet_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
1749 struct fec_enet_private *fep = bus->priv;
1750 unsigned long time_left;
1752 fep->mii_timeout = 0;
1753 init_completion(&fep->mdio_done);
1755 /* start a read op */
1756 writel(FEC_MMFR_ST | FEC_MMFR_OP_READ |
1757 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1758 FEC_MMFR_TA, fep->hwp + FEC_MII_DATA);
1760 /* wait for end of transfer */
1761 time_left = wait_for_completion_timeout(&fep->mdio_done,
1762 usecs_to_jiffies(FEC_MII_TIMEOUT));
1763 if (time_left == 0) {
1764 fep->mii_timeout = 1;
1765 netdev_err(fep->netdev, "MDIO read timeout\n");
1770 return FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA));
1773 static int fec_enet_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
1776 struct fec_enet_private *fep = bus->priv;
1777 unsigned long time_left;
1779 fep->mii_timeout = 0;
1780 init_completion(&fep->mdio_done);
1782 /* start a write op */
1783 writel(FEC_MMFR_ST | FEC_MMFR_OP_WRITE |
1784 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1785 FEC_MMFR_TA | FEC_MMFR_DATA(value),
1786 fep->hwp + FEC_MII_DATA);
1788 /* wait for end of transfer */
1789 time_left = wait_for_completion_timeout(&fep->mdio_done,
1790 usecs_to_jiffies(FEC_MII_TIMEOUT));
1791 if (time_left == 0) {
1792 fep->mii_timeout = 1;
1793 netdev_err(fep->netdev, "MDIO write timeout\n");
1800 static int fec_enet_clk_enable(struct net_device *ndev, bool enable)
1802 struct fec_enet_private *fep = netdev_priv(ndev);
1806 ret = clk_prepare_enable(fep->clk_ahb);
1809 ret = clk_prepare_enable(fep->clk_ipg);
1811 goto failed_clk_ipg;
1812 if (fep->clk_enet_out) {
1813 ret = clk_prepare_enable(fep->clk_enet_out);
1815 goto failed_clk_enet_out;
1818 mutex_lock(&fep->ptp_clk_mutex);
1819 ret = clk_prepare_enable(fep->clk_ptp);
1821 mutex_unlock(&fep->ptp_clk_mutex);
1822 goto failed_clk_ptp;
1824 fep->ptp_clk_on = true;
1826 mutex_unlock(&fep->ptp_clk_mutex);
1829 ret = clk_prepare_enable(fep->clk_ref);
1831 goto failed_clk_ref;
1834 clk_disable_unprepare(fep->clk_ahb);
1835 clk_disable_unprepare(fep->clk_ipg);
1836 if (fep->clk_enet_out)
1837 clk_disable_unprepare(fep->clk_enet_out);
1839 mutex_lock(&fep->ptp_clk_mutex);
1840 clk_disable_unprepare(fep->clk_ptp);
1841 fep->ptp_clk_on = false;
1842 mutex_unlock(&fep->ptp_clk_mutex);
1845 clk_disable_unprepare(fep->clk_ref);
1852 clk_disable_unprepare(fep->clk_ref);
1854 if (fep->clk_enet_out)
1855 clk_disable_unprepare(fep->clk_enet_out);
1856 failed_clk_enet_out:
1857 clk_disable_unprepare(fep->clk_ipg);
1859 clk_disable_unprepare(fep->clk_ahb);
1864 static int fec_enet_mii_probe(struct net_device *ndev)
1866 struct fec_enet_private *fep = netdev_priv(ndev);
1867 const struct platform_device_id *id_entry =
1868 platform_get_device_id(fep->pdev);
1869 struct phy_device *phy_dev = NULL;
1870 char mdio_bus_id[MII_BUS_ID_SIZE];
1871 char phy_name[MII_BUS_ID_SIZE + 3];
1873 int dev_id = fep->dev_id;
1875 fep->phy_dev = NULL;
1877 if (fep->phy_node) {
1878 phy_dev = of_phy_connect(ndev, fep->phy_node,
1879 &fec_enet_adjust_link, 0,
1880 fep->phy_interface);
1882 /* check for attached phy */
1883 for (phy_id = 0; (phy_id < PHY_MAX_ADDR); phy_id++) {
1884 if ((fep->mii_bus->phy_mask & (1 << phy_id)))
1886 if (fep->mii_bus->phy_map[phy_id] == NULL)
1888 if (fep->mii_bus->phy_map[phy_id]->phy_id == 0)
1892 strlcpy(mdio_bus_id, fep->mii_bus->id, MII_BUS_ID_SIZE);
1896 if (phy_id >= PHY_MAX_ADDR) {
1897 netdev_info(ndev, "no PHY, assuming direct connection to switch\n");
1898 strlcpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE);
1902 snprintf(phy_name, sizeof(phy_name),
1903 PHY_ID_FMT, mdio_bus_id, phy_id);
1904 phy_dev = phy_connect(ndev, phy_name, &fec_enet_adjust_link,
1905 fep->phy_interface);
1908 if (IS_ERR(phy_dev)) {
1909 netdev_err(ndev, "could not attach to PHY\n");
1910 return PTR_ERR(phy_dev);
1913 /* mask with MAC supported features */
1914 if (id_entry->driver_data & FEC_QUIRK_HAS_GBIT) {
1915 phy_dev->supported &= PHY_GBIT_FEATURES;
1916 phy_dev->supported &= ~SUPPORTED_1000baseT_Half;
1917 #if !defined(CONFIG_M5272)
1918 phy_dev->supported |= SUPPORTED_Pause;
1922 phy_dev->supported &= PHY_BASIC_FEATURES;
1924 phy_dev->advertising = phy_dev->supported;
1926 fep->phy_dev = phy_dev;
1928 fep->full_duplex = 0;
1930 netdev_info(ndev, "Freescale FEC PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
1931 fep->phy_dev->drv->name, dev_name(&fep->phy_dev->dev),
1937 static int fec_enet_mii_init(struct platform_device *pdev)
1939 static struct mii_bus *fec0_mii_bus;
1940 struct net_device *ndev = platform_get_drvdata(pdev);
1941 struct fec_enet_private *fep = netdev_priv(ndev);
1942 const struct platform_device_id *id_entry =
1943 platform_get_device_id(fep->pdev);
1944 struct device_node *node;
1945 int err = -ENXIO, i;
1948 * The dual fec interfaces are not equivalent with enet-mac.
1949 * Here are the differences:
1951 * - fec0 supports MII & RMII modes while fec1 only supports RMII
1952 * - fec0 acts as the 1588 time master while fec1 is slave
1953 * - external phys can only be configured by fec0
1955 * That is to say fec1 can not work independently. It only works
1956 * when fec0 is working. The reason behind this design is that the
1957 * second interface is added primarily for Switch mode.
1959 * Because of the last point above, both phys are attached on fec0
1960 * mdio interface in board design, and need to be configured by
1963 if ((id_entry->driver_data & FEC_QUIRK_ENET_MAC) && fep->dev_id > 0) {
1964 /* fec1 uses fec0 mii_bus */
1965 if (mii_cnt && fec0_mii_bus) {
1966 fep->mii_bus = fec0_mii_bus;
1973 fep->mii_timeout = 0;
1976 * Set MII speed to 2.5 MHz (= clk_get_rate() / 2 * phy_speed)
1978 * The formula for FEC MDC is 'ref_freq / (MII_SPEED x 2)' while
1979 * for ENET-MAC is 'ref_freq / ((MII_SPEED + 1) x 2)'. The i.MX28
1980 * Reference Manual has an error on this, and gets fixed on i.MX6Q
1983 fep->phy_speed = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), 5000000);
1984 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
1986 fep->phy_speed <<= 1;
1987 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
1989 fep->mii_bus = mdiobus_alloc();
1990 if (fep->mii_bus == NULL) {
1995 fep->mii_bus->name = "fec_enet_mii_bus";
1996 fep->mii_bus->read = fec_enet_mdio_read;
1997 fep->mii_bus->write = fec_enet_mdio_write;
1998 snprintf(fep->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
1999 pdev->name, fep->dev_id + 1);
2000 fep->mii_bus->priv = fep;
2001 fep->mii_bus->parent = &pdev->dev;
2003 fep->mii_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
2004 if (!fep->mii_bus->irq) {
2006 goto err_out_free_mdiobus;
2009 for (i = 0; i < PHY_MAX_ADDR; i++)
2010 fep->mii_bus->irq[i] = PHY_POLL;
2012 node = of_get_child_by_name(pdev->dev.of_node, "mdio");
2014 err = of_mdiobus_register(fep->mii_bus, node);
2017 err = mdiobus_register(fep->mii_bus);
2021 goto err_out_free_mdio_irq;
2025 /* save fec0 mii_bus */
2026 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
2027 fec0_mii_bus = fep->mii_bus;
2031 err_out_free_mdio_irq:
2032 kfree(fep->mii_bus->irq);
2033 err_out_free_mdiobus:
2034 mdiobus_free(fep->mii_bus);
2039 static void fec_enet_mii_remove(struct fec_enet_private *fep)
2041 if (--mii_cnt == 0) {
2042 mdiobus_unregister(fep->mii_bus);
2043 kfree(fep->mii_bus->irq);
2044 mdiobus_free(fep->mii_bus);
2048 static int fec_enet_get_settings(struct net_device *ndev,
2049 struct ethtool_cmd *cmd)
2051 struct fec_enet_private *fep = netdev_priv(ndev);
2052 struct phy_device *phydev = fep->phy_dev;
2057 return phy_ethtool_gset(phydev, cmd);
2060 static int fec_enet_set_settings(struct net_device *ndev,
2061 struct ethtool_cmd *cmd)
2063 struct fec_enet_private *fep = netdev_priv(ndev);
2064 struct phy_device *phydev = fep->phy_dev;
2069 return phy_ethtool_sset(phydev, cmd);
2072 static void fec_enet_get_drvinfo(struct net_device *ndev,
2073 struct ethtool_drvinfo *info)
2075 struct fec_enet_private *fep = netdev_priv(ndev);
2077 strlcpy(info->driver, fep->pdev->dev.driver->name,
2078 sizeof(info->driver));
2079 strlcpy(info->version, "Revision: 1.0", sizeof(info->version));
2080 strlcpy(info->bus_info, dev_name(&ndev->dev), sizeof(info->bus_info));
2083 static int fec_enet_get_ts_info(struct net_device *ndev,
2084 struct ethtool_ts_info *info)
2086 struct fec_enet_private *fep = netdev_priv(ndev);
2088 if (fep->bufdesc_ex) {
2090 info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
2091 SOF_TIMESTAMPING_RX_SOFTWARE |
2092 SOF_TIMESTAMPING_SOFTWARE |
2093 SOF_TIMESTAMPING_TX_HARDWARE |
2094 SOF_TIMESTAMPING_RX_HARDWARE |
2095 SOF_TIMESTAMPING_RAW_HARDWARE;
2097 info->phc_index = ptp_clock_index(fep->ptp_clock);
2099 info->phc_index = -1;
2101 info->tx_types = (1 << HWTSTAMP_TX_OFF) |
2102 (1 << HWTSTAMP_TX_ON);
2104 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
2105 (1 << HWTSTAMP_FILTER_ALL);
2108 return ethtool_op_get_ts_info(ndev, info);
2112 #if !defined(CONFIG_M5272)
2114 static void fec_enet_get_pauseparam(struct net_device *ndev,
2115 struct ethtool_pauseparam *pause)
2117 struct fec_enet_private *fep = netdev_priv(ndev);
2119 pause->autoneg = (fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) != 0;
2120 pause->tx_pause = (fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) != 0;
2121 pause->rx_pause = pause->tx_pause;
2124 static int fec_enet_set_pauseparam(struct net_device *ndev,
2125 struct ethtool_pauseparam *pause)
2127 struct fec_enet_private *fep = netdev_priv(ndev);
2132 if (pause->tx_pause != pause->rx_pause) {
2134 "hardware only support enable/disable both tx and rx");
2138 fep->pause_flag = 0;
2140 /* tx pause must be same as rx pause */
2141 fep->pause_flag |= pause->rx_pause ? FEC_PAUSE_FLAG_ENABLE : 0;
2142 fep->pause_flag |= pause->autoneg ? FEC_PAUSE_FLAG_AUTONEG : 0;
2144 if (pause->rx_pause || pause->autoneg) {
2145 fep->phy_dev->supported |= ADVERTISED_Pause;
2146 fep->phy_dev->advertising |= ADVERTISED_Pause;
2148 fep->phy_dev->supported &= ~ADVERTISED_Pause;
2149 fep->phy_dev->advertising &= ~ADVERTISED_Pause;
2152 if (pause->autoneg) {
2153 if (netif_running(ndev))
2155 phy_start_aneg(fep->phy_dev);
2157 if (netif_running(ndev)) {
2158 napi_disable(&fep->napi);
2159 netif_tx_lock_bh(ndev);
2161 netif_wake_queue(ndev);
2162 netif_tx_unlock_bh(ndev);
2163 napi_enable(&fep->napi);
2169 static const struct fec_stat {
2170 char name[ETH_GSTRING_LEN];
2174 { "tx_dropped", RMON_T_DROP },
2175 { "tx_packets", RMON_T_PACKETS },
2176 { "tx_broadcast", RMON_T_BC_PKT },
2177 { "tx_multicast", RMON_T_MC_PKT },
2178 { "tx_crc_errors", RMON_T_CRC_ALIGN },
2179 { "tx_undersize", RMON_T_UNDERSIZE },
2180 { "tx_oversize", RMON_T_OVERSIZE },
2181 { "tx_fragment", RMON_T_FRAG },
2182 { "tx_jabber", RMON_T_JAB },
2183 { "tx_collision", RMON_T_COL },
2184 { "tx_64byte", RMON_T_P64 },
2185 { "tx_65to127byte", RMON_T_P65TO127 },
2186 { "tx_128to255byte", RMON_T_P128TO255 },
2187 { "tx_256to511byte", RMON_T_P256TO511 },
2188 { "tx_512to1023byte", RMON_T_P512TO1023 },
2189 { "tx_1024to2047byte", RMON_T_P1024TO2047 },
2190 { "tx_GTE2048byte", RMON_T_P_GTE2048 },
2191 { "tx_octets", RMON_T_OCTETS },
2194 { "IEEE_tx_drop", IEEE_T_DROP },
2195 { "IEEE_tx_frame_ok", IEEE_T_FRAME_OK },
2196 { "IEEE_tx_1col", IEEE_T_1COL },
2197 { "IEEE_tx_mcol", IEEE_T_MCOL },
2198 { "IEEE_tx_def", IEEE_T_DEF },
2199 { "IEEE_tx_lcol", IEEE_T_LCOL },
2200 { "IEEE_tx_excol", IEEE_T_EXCOL },
2201 { "IEEE_tx_macerr", IEEE_T_MACERR },
2202 { "IEEE_tx_cserr", IEEE_T_CSERR },
2203 { "IEEE_tx_sqe", IEEE_T_SQE },
2204 { "IEEE_tx_fdxfc", IEEE_T_FDXFC },
2205 { "IEEE_tx_octets_ok", IEEE_T_OCTETS_OK },
2208 { "rx_packets", RMON_R_PACKETS },
2209 { "rx_broadcast", RMON_R_BC_PKT },
2210 { "rx_multicast", RMON_R_MC_PKT },
2211 { "rx_crc_errors", RMON_R_CRC_ALIGN },
2212 { "rx_undersize", RMON_R_UNDERSIZE },
2213 { "rx_oversize", RMON_R_OVERSIZE },
2214 { "rx_fragment", RMON_R_FRAG },
2215 { "rx_jabber", RMON_R_JAB },
2216 { "rx_64byte", RMON_R_P64 },
2217 { "rx_65to127byte", RMON_R_P65TO127 },
2218 { "rx_128to255byte", RMON_R_P128TO255 },
2219 { "rx_256to511byte", RMON_R_P256TO511 },
2220 { "rx_512to1023byte", RMON_R_P512TO1023 },
2221 { "rx_1024to2047byte", RMON_R_P1024TO2047 },
2222 { "rx_GTE2048byte", RMON_R_P_GTE2048 },
2223 { "rx_octets", RMON_R_OCTETS },
2226 { "IEEE_rx_drop", IEEE_R_DROP },
2227 { "IEEE_rx_frame_ok", IEEE_R_FRAME_OK },
2228 { "IEEE_rx_crc", IEEE_R_CRC },
2229 { "IEEE_rx_align", IEEE_R_ALIGN },
2230 { "IEEE_rx_macerr", IEEE_R_MACERR },
2231 { "IEEE_rx_fdxfc", IEEE_R_FDXFC },
2232 { "IEEE_rx_octets_ok", IEEE_R_OCTETS_OK },
2235 static void fec_enet_get_ethtool_stats(struct net_device *dev,
2236 struct ethtool_stats *stats, u64 *data)
2238 struct fec_enet_private *fep = netdev_priv(dev);
2241 for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
2242 data[i] = readl(fep->hwp + fec_stats[i].offset);
2245 static void fec_enet_get_strings(struct net_device *netdev,
2246 u32 stringset, u8 *data)
2249 switch (stringset) {
2251 for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
2252 memcpy(data + i * ETH_GSTRING_LEN,
2253 fec_stats[i].name, ETH_GSTRING_LEN);
2258 static int fec_enet_get_sset_count(struct net_device *dev, int sset)
2262 return ARRAY_SIZE(fec_stats);
2267 #endif /* !defined(CONFIG_M5272) */
2269 static int fec_enet_nway_reset(struct net_device *dev)
2271 struct fec_enet_private *fep = netdev_priv(dev);
2272 struct phy_device *phydev = fep->phy_dev;
2277 return genphy_restart_aneg(phydev);
2280 /* ITR clock source is enet system clock (clk_ahb).
2281 * TCTT unit is cycle_ns * 64 cycle
2282 * So, the ICTT value = X us / (cycle_ns * 64)
2284 static int fec_enet_us_to_itr_clock(struct net_device *ndev, int us)
2286 struct fec_enet_private *fep = netdev_priv(ndev);
2288 return us * (fep->itr_clk_rate / 64000) / 1000;
2291 /* Set threshold for interrupt coalescing */
2292 static void fec_enet_itr_coal_set(struct net_device *ndev)
2294 struct fec_enet_private *fep = netdev_priv(ndev);
2295 const struct platform_device_id *id_entry =
2296 platform_get_device_id(fep->pdev);
2299 if (!(id_entry->driver_data & FEC_QUIRK_HAS_AVB))
2302 /* Must be greater than zero to avoid unpredictable behavior */
2303 if (!fep->rx_time_itr || !fep->rx_pkts_itr ||
2304 !fep->tx_time_itr || !fep->tx_pkts_itr)
2307 /* Select enet system clock as Interrupt Coalescing
2308 * timer Clock Source
2310 rx_itr = FEC_ITR_CLK_SEL;
2311 tx_itr = FEC_ITR_CLK_SEL;
2313 /* set ICFT and ICTT */
2314 rx_itr |= FEC_ITR_ICFT(fep->rx_pkts_itr);
2315 rx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->rx_time_itr));
2316 tx_itr |= FEC_ITR_ICFT(fep->tx_pkts_itr);
2317 tx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->tx_time_itr));
2319 rx_itr |= FEC_ITR_EN;
2320 tx_itr |= FEC_ITR_EN;
2322 writel(tx_itr, fep->hwp + FEC_TXIC0);
2323 writel(rx_itr, fep->hwp + FEC_RXIC0);
2324 writel(tx_itr, fep->hwp + FEC_TXIC1);
2325 writel(rx_itr, fep->hwp + FEC_RXIC1);
2326 writel(tx_itr, fep->hwp + FEC_TXIC2);
2327 writel(rx_itr, fep->hwp + FEC_RXIC2);
2331 fec_enet_get_coalesce(struct net_device *ndev, struct ethtool_coalesce *ec)
2333 struct fec_enet_private *fep = netdev_priv(ndev);
2334 const struct platform_device_id *id_entry =
2335 platform_get_device_id(fep->pdev);
2337 if (!(id_entry->driver_data & FEC_QUIRK_HAS_AVB))
2340 ec->rx_coalesce_usecs = fep->rx_time_itr;
2341 ec->rx_max_coalesced_frames = fep->rx_pkts_itr;
2343 ec->tx_coalesce_usecs = fep->tx_time_itr;
2344 ec->tx_max_coalesced_frames = fep->tx_pkts_itr;
2350 fec_enet_set_coalesce(struct net_device *ndev, struct ethtool_coalesce *ec)
2352 struct fec_enet_private *fep = netdev_priv(ndev);
2353 const struct platform_device_id *id_entry =
2354 platform_get_device_id(fep->pdev);
2358 if (!(id_entry->driver_data & FEC_QUIRK_HAS_AVB))
2361 if (ec->rx_max_coalesced_frames > 255) {
2362 pr_err("Rx coalesced frames exceed hardware limiation");
2366 if (ec->tx_max_coalesced_frames > 255) {
2367 pr_err("Tx coalesced frame exceed hardware limiation");
2371 cycle = fec_enet_us_to_itr_clock(ndev, fep->rx_time_itr);
2372 if (cycle > 0xFFFF) {
2373 pr_err("Rx coalesed usec exceeed hardware limiation");
2377 cycle = fec_enet_us_to_itr_clock(ndev, fep->tx_time_itr);
2378 if (cycle > 0xFFFF) {
2379 pr_err("Rx coalesed usec exceeed hardware limiation");
2383 fep->rx_time_itr = ec->rx_coalesce_usecs;
2384 fep->rx_pkts_itr = ec->rx_max_coalesced_frames;
2386 fep->tx_time_itr = ec->tx_coalesce_usecs;
2387 fep->tx_pkts_itr = ec->tx_max_coalesced_frames;
2389 fec_enet_itr_coal_set(ndev);
2394 static void fec_enet_itr_coal_init(struct net_device *ndev)
2396 struct ethtool_coalesce ec;
2398 ec.rx_coalesce_usecs = FEC_ITR_ICTT_DEFAULT;
2399 ec.rx_max_coalesced_frames = FEC_ITR_ICFT_DEFAULT;
2401 ec.tx_coalesce_usecs = FEC_ITR_ICTT_DEFAULT;
2402 ec.tx_max_coalesced_frames = FEC_ITR_ICFT_DEFAULT;
2404 fec_enet_set_coalesce(ndev, &ec);
2407 static int fec_enet_get_tunable(struct net_device *netdev,
2408 const struct ethtool_tunable *tuna,
2411 struct fec_enet_private *fep = netdev_priv(netdev);
2415 case ETHTOOL_RX_COPYBREAK:
2416 *(u32 *)data = fep->rx_copybreak;
2426 static int fec_enet_set_tunable(struct net_device *netdev,
2427 const struct ethtool_tunable *tuna,
2430 struct fec_enet_private *fep = netdev_priv(netdev);
2434 case ETHTOOL_RX_COPYBREAK:
2435 fep->rx_copybreak = *(u32 *)data;
2445 static const struct ethtool_ops fec_enet_ethtool_ops = {
2446 .get_settings = fec_enet_get_settings,
2447 .set_settings = fec_enet_set_settings,
2448 .get_drvinfo = fec_enet_get_drvinfo,
2449 .nway_reset = fec_enet_nway_reset,
2450 .get_link = ethtool_op_get_link,
2451 .get_coalesce = fec_enet_get_coalesce,
2452 .set_coalesce = fec_enet_set_coalesce,
2453 #ifndef CONFIG_M5272
2454 .get_pauseparam = fec_enet_get_pauseparam,
2455 .set_pauseparam = fec_enet_set_pauseparam,
2456 .get_strings = fec_enet_get_strings,
2457 .get_ethtool_stats = fec_enet_get_ethtool_stats,
2458 .get_sset_count = fec_enet_get_sset_count,
2460 .get_ts_info = fec_enet_get_ts_info,
2461 .get_tunable = fec_enet_get_tunable,
2462 .set_tunable = fec_enet_set_tunable,
2465 static int fec_enet_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
2467 struct fec_enet_private *fep = netdev_priv(ndev);
2468 struct phy_device *phydev = fep->phy_dev;
2470 if (!netif_running(ndev))
2476 if (fep->bufdesc_ex) {
2477 if (cmd == SIOCSHWTSTAMP)
2478 return fec_ptp_set(ndev, rq);
2479 if (cmd == SIOCGHWTSTAMP)
2480 return fec_ptp_get(ndev, rq);
2483 return phy_mii_ioctl(phydev, rq, cmd);
2486 static void fec_enet_free_buffers(struct net_device *ndev)
2488 struct fec_enet_private *fep = netdev_priv(ndev);
2490 struct sk_buff *skb;
2491 struct bufdesc *bdp;
2492 struct fec_enet_priv_tx_q *txq;
2493 struct fec_enet_priv_rx_q *rxq;
2496 for (q = 0; q < fep->num_rx_queues; q++) {
2497 rxq = fep->rx_queue[q];
2498 bdp = rxq->rx_bd_base;
2499 for (i = 0; i < rxq->rx_ring_size; i++) {
2500 skb = rxq->rx_skbuff[i];
2501 rxq->rx_skbuff[i] = NULL;
2503 dma_unmap_single(&fep->pdev->dev,
2505 FEC_ENET_RX_FRSIZE - fep->rx_align,
2509 bdp = fec_enet_get_nextdesc(bdp, fep, q);
2513 for (q = 0; q < fep->num_tx_queues; q++) {
2514 txq = fep->tx_queue[q];
2515 bdp = txq->tx_bd_base;
2516 for (i = 0; i < txq->tx_ring_size; i++) {
2517 kfree(txq->tx_bounce[i]);
2518 txq->tx_bounce[i] = NULL;
2519 skb = txq->tx_skbuff[i];
2520 txq->tx_skbuff[i] = NULL;
2526 static void fec_enet_free_queue(struct net_device *ndev)
2528 struct fec_enet_private *fep = netdev_priv(ndev);
2530 struct fec_enet_priv_tx_q *txq;
2532 for (i = 0; i < fep->num_tx_queues; i++)
2533 if (fep->tx_queue[i] && fep->tx_queue[i]->tso_hdrs) {
2534 txq = fep->tx_queue[i];
2535 dma_free_coherent(NULL,
2536 txq->tx_ring_size * TSO_HEADER_SIZE,
2541 for (i = 0; i < fep->num_rx_queues; i++)
2542 if (fep->rx_queue[i])
2543 kfree(fep->rx_queue[i]);
2545 for (i = 0; i < fep->num_tx_queues; i++)
2546 if (fep->tx_queue[i])
2547 kfree(fep->tx_queue[i]);
2550 static int fec_enet_alloc_queue(struct net_device *ndev)
2552 struct fec_enet_private *fep = netdev_priv(ndev);
2555 struct fec_enet_priv_tx_q *txq;
2557 for (i = 0; i < fep->num_tx_queues; i++) {
2558 txq = kzalloc(sizeof(*txq), GFP_KERNEL);
2564 fep->tx_queue[i] = txq;
2565 txq->tx_ring_size = TX_RING_SIZE;
2566 fep->total_tx_ring_size += fep->tx_queue[i]->tx_ring_size;
2568 txq->tx_stop_threshold = FEC_MAX_SKB_DESCS;
2569 txq->tx_wake_threshold =
2570 (txq->tx_ring_size - txq->tx_stop_threshold) / 2;
2572 txq->tso_hdrs = dma_alloc_coherent(NULL,
2573 txq->tx_ring_size * TSO_HEADER_SIZE,
2576 if (!txq->tso_hdrs) {
2582 for (i = 0; i < fep->num_rx_queues; i++) {
2583 fep->rx_queue[i] = kzalloc(sizeof(*fep->rx_queue[i]),
2585 if (!fep->rx_queue[i]) {
2590 fep->rx_queue[i]->rx_ring_size = RX_RING_SIZE;
2591 fep->total_rx_ring_size += fep->rx_queue[i]->rx_ring_size;
2596 fec_enet_free_queue(ndev);
2601 fec_enet_alloc_rxq_buffers(struct net_device *ndev, unsigned int queue)
2603 struct fec_enet_private *fep = netdev_priv(ndev);
2605 struct sk_buff *skb;
2606 struct bufdesc *bdp;
2607 struct fec_enet_priv_rx_q *rxq;
2609 rxq = fep->rx_queue[queue];
2610 bdp = rxq->rx_bd_base;
2611 for (i = 0; i < rxq->rx_ring_size; i++) {
2612 skb = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
2616 if (fec_enet_new_rxbdp(ndev, bdp, skb)) {
2621 rxq->rx_skbuff[i] = skb;
2622 bdp->cbd_sc = BD_ENET_RX_EMPTY;
2624 if (fep->bufdesc_ex) {
2625 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
2626 ebdp->cbd_esc = BD_ENET_RX_INT;
2629 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
2632 /* Set the last buffer to wrap. */
2633 bdp = fec_enet_get_prevdesc(bdp, fep, queue);
2634 bdp->cbd_sc |= BD_SC_WRAP;
2638 fec_enet_free_buffers(ndev);
2643 fec_enet_alloc_txq_buffers(struct net_device *ndev, unsigned int queue)
2645 struct fec_enet_private *fep = netdev_priv(ndev);
2647 struct bufdesc *bdp;
2648 struct fec_enet_priv_tx_q *txq;
2650 txq = fep->tx_queue[queue];
2651 bdp = txq->tx_bd_base;
2652 for (i = 0; i < txq->tx_ring_size; i++) {
2653 txq->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL);
2654 if (!txq->tx_bounce[i])
2658 bdp->cbd_bufaddr = 0;
2660 if (fep->bufdesc_ex) {
2661 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
2662 ebdp->cbd_esc = BD_ENET_TX_INT;
2665 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
2668 /* Set the last buffer to wrap. */
2669 bdp = fec_enet_get_prevdesc(bdp, fep, queue);
2670 bdp->cbd_sc |= BD_SC_WRAP;
2675 fec_enet_free_buffers(ndev);
2679 static int fec_enet_alloc_buffers(struct net_device *ndev)
2681 struct fec_enet_private *fep = netdev_priv(ndev);
2684 for (i = 0; i < fep->num_rx_queues; i++)
2685 if (fec_enet_alloc_rxq_buffers(ndev, i))
2688 for (i = 0; i < fep->num_tx_queues; i++)
2689 if (fec_enet_alloc_txq_buffers(ndev, i))
2695 fec_enet_open(struct net_device *ndev)
2697 struct fec_enet_private *fep = netdev_priv(ndev);
2700 pinctrl_pm_select_default_state(&fep->pdev->dev);
2701 ret = fec_enet_clk_enable(ndev, true);
2705 /* I should reset the ring buffers here, but I don't yet know
2706 * a simple way to do that.
2709 ret = fec_enet_alloc_buffers(ndev);
2711 goto err_enet_alloc;
2713 /* Probe and connect to PHY when open the interface */
2714 ret = fec_enet_mii_probe(ndev);
2716 goto err_enet_mii_probe;
2719 napi_enable(&fep->napi);
2720 phy_start(fep->phy_dev);
2721 netif_tx_start_all_queues(ndev);
2726 fec_enet_free_buffers(ndev);
2728 fec_enet_clk_enable(ndev, false);
2729 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
2734 fec_enet_close(struct net_device *ndev)
2736 struct fec_enet_private *fep = netdev_priv(ndev);
2738 phy_stop(fep->phy_dev);
2740 if (netif_device_present(ndev)) {
2741 napi_disable(&fep->napi);
2742 netif_tx_disable(ndev);
2746 phy_disconnect(fep->phy_dev);
2747 fep->phy_dev = NULL;
2749 fec_enet_clk_enable(ndev, false);
2750 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
2751 fec_enet_free_buffers(ndev);
2756 /* Set or clear the multicast filter for this adaptor.
2757 * Skeleton taken from sunlance driver.
2758 * The CPM Ethernet implementation allows Multicast as well as individual
2759 * MAC address filtering. Some of the drivers check to make sure it is
2760 * a group multicast address, and discard those that are not. I guess I
2761 * will do the same for now, but just remove the test if you want
2762 * individual filtering as well (do the upper net layers want or support
2763 * this kind of feature?).
2766 #define HASH_BITS 6 /* #bits in hash */
2767 #define CRC32_POLY 0xEDB88320
2769 static void set_multicast_list(struct net_device *ndev)
2771 struct fec_enet_private *fep = netdev_priv(ndev);
2772 struct netdev_hw_addr *ha;
2773 unsigned int i, bit, data, crc, tmp;
2776 if (ndev->flags & IFF_PROMISC) {
2777 tmp = readl(fep->hwp + FEC_R_CNTRL);
2779 writel(tmp, fep->hwp + FEC_R_CNTRL);
2783 tmp = readl(fep->hwp + FEC_R_CNTRL);
2785 writel(tmp, fep->hwp + FEC_R_CNTRL);
2787 if (ndev->flags & IFF_ALLMULTI) {
2788 /* Catch all multicast addresses, so set the
2791 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2792 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
2797 /* Clear filter and add the addresses in hash register
2799 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2800 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
2802 netdev_for_each_mc_addr(ha, ndev) {
2803 /* calculate crc32 value of mac address */
2806 for (i = 0; i < ndev->addr_len; i++) {
2808 for (bit = 0; bit < 8; bit++, data >>= 1) {
2810 (((crc ^ data) & 1) ? CRC32_POLY : 0);
2814 /* only upper 6 bits (HASH_BITS) are used
2815 * which point to specific bit in he hash registers
2817 hash = (crc >> (32 - HASH_BITS)) & 0x3f;
2820 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2821 tmp |= 1 << (hash - 32);
2822 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2824 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_LOW);
2826 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
2831 /* Set a MAC change in hardware. */
2833 fec_set_mac_address(struct net_device *ndev, void *p)
2835 struct fec_enet_private *fep = netdev_priv(ndev);
2836 struct sockaddr *addr = p;
2839 if (!is_valid_ether_addr(addr->sa_data))
2840 return -EADDRNOTAVAIL;
2841 memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len);
2844 writel(ndev->dev_addr[3] | (ndev->dev_addr[2] << 8) |
2845 (ndev->dev_addr[1] << 16) | (ndev->dev_addr[0] << 24),
2846 fep->hwp + FEC_ADDR_LOW);
2847 writel((ndev->dev_addr[5] << 16) | (ndev->dev_addr[4] << 24),
2848 fep->hwp + FEC_ADDR_HIGH);
2852 #ifdef CONFIG_NET_POLL_CONTROLLER
2854 * fec_poll_controller - FEC Poll controller function
2855 * @dev: The FEC network adapter
2857 * Polled functionality used by netconsole and others in non interrupt mode
2860 static void fec_poll_controller(struct net_device *dev)
2863 struct fec_enet_private *fep = netdev_priv(dev);
2865 for (i = 0; i < FEC_IRQ_NUM; i++) {
2866 if (fep->irq[i] > 0) {
2867 disable_irq(fep->irq[i]);
2868 fec_enet_interrupt(fep->irq[i], dev);
2869 enable_irq(fep->irq[i]);
2875 #define FEATURES_NEED_QUIESCE NETIF_F_RXCSUM
2876 static inline void fec_enet_set_netdev_features(struct net_device *netdev,
2877 netdev_features_t features)
2879 struct fec_enet_private *fep = netdev_priv(netdev);
2880 netdev_features_t changed = features ^ netdev->features;
2882 netdev->features = features;
2884 /* Receive checksum has been changed */
2885 if (changed & NETIF_F_RXCSUM) {
2886 if (features & NETIF_F_RXCSUM)
2887 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
2889 fep->csum_flags &= ~FLAG_RX_CSUM_ENABLED;
2893 static int fec_set_features(struct net_device *netdev,
2894 netdev_features_t features)
2896 struct fec_enet_private *fep = netdev_priv(netdev);
2897 netdev_features_t changed = features ^ netdev->features;
2899 if (netif_running(netdev) && changed & FEATURES_NEED_QUIESCE) {
2900 napi_disable(&fep->napi);
2901 netif_tx_lock_bh(netdev);
2903 fec_enet_set_netdev_features(netdev, features);
2904 fec_restart(netdev);
2905 netif_tx_wake_all_queues(netdev);
2906 netif_tx_unlock_bh(netdev);
2907 napi_enable(&fep->napi);
2909 fec_enet_set_netdev_features(netdev, features);
2915 static const struct net_device_ops fec_netdev_ops = {
2916 .ndo_open = fec_enet_open,
2917 .ndo_stop = fec_enet_close,
2918 .ndo_start_xmit = fec_enet_start_xmit,
2919 .ndo_set_rx_mode = set_multicast_list,
2920 .ndo_change_mtu = eth_change_mtu,
2921 .ndo_validate_addr = eth_validate_addr,
2922 .ndo_tx_timeout = fec_timeout,
2923 .ndo_set_mac_address = fec_set_mac_address,
2924 .ndo_do_ioctl = fec_enet_ioctl,
2925 #ifdef CONFIG_NET_POLL_CONTROLLER
2926 .ndo_poll_controller = fec_poll_controller,
2928 .ndo_set_features = fec_set_features,
2932 * XXX: We need to clean up on failure exits here.
2935 static int fec_enet_init(struct net_device *ndev)
2937 struct fec_enet_private *fep = netdev_priv(ndev);
2938 const struct platform_device_id *id_entry =
2939 platform_get_device_id(fep->pdev);
2940 struct fec_enet_priv_tx_q *txq;
2941 struct fec_enet_priv_rx_q *rxq;
2942 struct bufdesc *cbd_base;
2947 #if defined(CONFIG_ARM)
2948 fep->rx_align = 0xf;
2949 fep->tx_align = 0xf;
2951 fep->rx_align = 0x3;
2952 fep->tx_align = 0x3;
2955 fec_enet_alloc_queue(ndev);
2957 if (fep->bufdesc_ex)
2958 fep->bufdesc_size = sizeof(struct bufdesc_ex);
2960 fep->bufdesc_size = sizeof(struct bufdesc);
2961 bd_size = (fep->total_tx_ring_size + fep->total_rx_ring_size) *
2964 /* Allocate memory for buffer descriptors. */
2965 cbd_base = dma_alloc_coherent(NULL, bd_size, &bd_dma,
2971 memset(cbd_base, 0, bd_size);
2973 /* Get the Ethernet address */
2975 /* make sure MAC we just acquired is programmed into the hw */
2976 fec_set_mac_address(ndev, NULL);
2978 /* Set receive and transmit descriptor base. */
2979 for (i = 0; i < fep->num_rx_queues; i++) {
2980 rxq = fep->rx_queue[i];
2982 rxq->rx_bd_base = (struct bufdesc *)cbd_base;
2983 rxq->bd_dma = bd_dma;
2984 if (fep->bufdesc_ex) {
2985 bd_dma += sizeof(struct bufdesc_ex) * rxq->rx_ring_size;
2986 cbd_base = (struct bufdesc *)
2987 (((struct bufdesc_ex *)cbd_base) + rxq->rx_ring_size);
2989 bd_dma += sizeof(struct bufdesc) * rxq->rx_ring_size;
2990 cbd_base += rxq->rx_ring_size;
2994 for (i = 0; i < fep->num_tx_queues; i++) {
2995 txq = fep->tx_queue[i];
2997 txq->tx_bd_base = (struct bufdesc *)cbd_base;
2998 txq->bd_dma = bd_dma;
2999 if (fep->bufdesc_ex) {
3000 bd_dma += sizeof(struct bufdesc_ex) * txq->tx_ring_size;
3001 cbd_base = (struct bufdesc *)
3002 (((struct bufdesc_ex *)cbd_base) + txq->tx_ring_size);
3004 bd_dma += sizeof(struct bufdesc) * txq->tx_ring_size;
3005 cbd_base += txq->tx_ring_size;
3010 /* The FEC Ethernet specific entries in the device structure */
3011 ndev->watchdog_timeo = TX_TIMEOUT;
3012 ndev->netdev_ops = &fec_netdev_ops;
3013 ndev->ethtool_ops = &fec_enet_ethtool_ops;
3015 writel(FEC_RX_DISABLED_IMASK, fep->hwp + FEC_IMASK);
3016 netif_napi_add(ndev, &fep->napi, fec_enet_rx_napi, NAPI_POLL_WEIGHT);
3018 if (id_entry->driver_data & FEC_QUIRK_HAS_VLAN)
3019 /* enable hw VLAN support */
3020 ndev->features |= NETIF_F_HW_VLAN_CTAG_RX;
3022 if (id_entry->driver_data & FEC_QUIRK_HAS_CSUM) {
3023 ndev->gso_max_segs = FEC_MAX_TSO_SEGS;
3025 /* enable hw accelerator */
3026 ndev->features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
3027 | NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_TSO);
3028 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
3031 if (id_entry->driver_data & FEC_QUIRK_HAS_AVB) {
3033 fep->rx_align = 0x3f;
3036 ndev->hw_features = ndev->features;
3044 static void fec_reset_phy(struct platform_device *pdev)
3048 struct device_node *np = pdev->dev.of_node;
3053 of_property_read_u32(np, "phy-reset-duration", &msec);
3054 /* A sane reset duration should not be longer than 1s */
3058 phy_reset = of_get_named_gpio(np, "phy-reset-gpios", 0);
3059 if (!gpio_is_valid(phy_reset))
3062 err = devm_gpio_request_one(&pdev->dev, phy_reset,
3063 GPIOF_OUT_INIT_LOW, "phy-reset");
3065 dev_err(&pdev->dev, "failed to get phy-reset-gpios: %d\n", err);
3069 gpio_set_value(phy_reset, 1);
3071 #else /* CONFIG_OF */
3072 static void fec_reset_phy(struct platform_device *pdev)
3075 * In case of platform probe, the reset has been done
3079 #endif /* CONFIG_OF */
3082 fec_enet_get_queue_num(struct platform_device *pdev, int *num_tx, int *num_rx)
3084 struct device_node *np = pdev->dev.of_node;
3087 *num_tx = *num_rx = 1;
3089 if (!np || !of_device_is_available(np))
3092 /* parse the num of tx and rx queues */
3093 err = of_property_read_u32(np, "fsl,num-tx-queues", num_tx);
3097 err = of_property_read_u32(np, "fsl,num-rx-queues", num_rx);
3101 if (*num_tx < 1 || *num_tx > FEC_ENET_MAX_TX_QS) {
3102 dev_warn(&pdev->dev, "Invalid num_tx(=%d), fall back to 1\n",
3108 if (*num_rx < 1 || *num_rx > FEC_ENET_MAX_RX_QS) {
3109 dev_warn(&pdev->dev, "Invalid num_rx(=%d), fall back to 1\n",
3118 fec_probe(struct platform_device *pdev)
3120 struct fec_enet_private *fep;
3121 struct fec_platform_data *pdata;
3122 struct net_device *ndev;
3123 int i, irq, ret = 0;
3125 const struct of_device_id *of_id;
3127 struct device_node *np = pdev->dev.of_node, *phy_node;
3131 of_id = of_match_device(fec_dt_ids, &pdev->dev);
3133 pdev->id_entry = of_id->data;
3135 fec_enet_get_queue_num(pdev, &num_tx_qs, &num_rx_qs);
3137 /* Init network device */
3138 ndev = alloc_etherdev_mqs(sizeof(struct fec_enet_private),
3139 num_tx_qs, num_rx_qs);
3143 SET_NETDEV_DEV(ndev, &pdev->dev);
3145 /* setup board info structure */
3146 fep = netdev_priv(ndev);
3148 fep->num_rx_queues = num_rx_qs;
3149 fep->num_tx_queues = num_tx_qs;
3151 #if !defined(CONFIG_M5272)
3152 /* default enable pause frame auto negotiation */
3153 if (pdev->id_entry &&
3154 (pdev->id_entry->driver_data & FEC_QUIRK_HAS_GBIT))
3155 fep->pause_flag |= FEC_PAUSE_FLAG_AUTONEG;
3158 /* Select default pin state */
3159 pinctrl_pm_select_default_state(&pdev->dev);
3161 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3162 fep->hwp = devm_ioremap_resource(&pdev->dev, r);
3163 if (IS_ERR(fep->hwp)) {
3164 ret = PTR_ERR(fep->hwp);
3165 goto failed_ioremap;
3169 fep->dev_id = dev_id++;
3171 fep->bufdesc_ex = 0;
3173 platform_set_drvdata(pdev, ndev);
3175 phy_node = of_parse_phandle(np, "phy-handle", 0);
3176 if (!phy_node && of_phy_is_fixed_link(np)) {
3177 ret = of_phy_register_fixed_link(np);
3180 "broken fixed-link specification\n");
3183 phy_node = of_node_get(np);
3185 fep->phy_node = phy_node;
3187 ret = of_get_phy_mode(pdev->dev.of_node);
3189 pdata = dev_get_platdata(&pdev->dev);
3191 fep->phy_interface = pdata->phy;
3193 fep->phy_interface = PHY_INTERFACE_MODE_MII;
3195 fep->phy_interface = ret;
3198 fep->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
3199 if (IS_ERR(fep->clk_ipg)) {
3200 ret = PTR_ERR(fep->clk_ipg);
3204 fep->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
3205 if (IS_ERR(fep->clk_ahb)) {
3206 ret = PTR_ERR(fep->clk_ahb);
3210 fep->itr_clk_rate = clk_get_rate(fep->clk_ahb);
3212 /* enet_out is optional, depends on board */
3213 fep->clk_enet_out = devm_clk_get(&pdev->dev, "enet_out");
3214 if (IS_ERR(fep->clk_enet_out))
3215 fep->clk_enet_out = NULL;
3217 fep->ptp_clk_on = false;
3218 mutex_init(&fep->ptp_clk_mutex);
3220 /* clk_ref is optional, depends on board */
3221 fep->clk_ref = devm_clk_get(&pdev->dev, "enet_clk_ref");
3222 if (IS_ERR(fep->clk_ref))
3223 fep->clk_ref = NULL;
3225 fep->clk_ptp = devm_clk_get(&pdev->dev, "ptp");
3227 pdev->id_entry->driver_data & FEC_QUIRK_HAS_BUFDESC_EX;
3228 if (IS_ERR(fep->clk_ptp)) {
3229 fep->clk_ptp = NULL;
3230 fep->bufdesc_ex = 0;
3233 ret = fec_enet_clk_enable(ndev, true);
3237 fep->reg_phy = devm_regulator_get(&pdev->dev, "phy");
3238 if (!IS_ERR(fep->reg_phy)) {
3239 ret = regulator_enable(fep->reg_phy);
3242 "Failed to enable phy regulator: %d\n", ret);
3243 goto failed_regulator;
3246 fep->reg_phy = NULL;
3249 fec_reset_phy(pdev);
3251 if (fep->bufdesc_ex)
3254 ret = fec_enet_init(ndev);
3258 for (i = 0; i < FEC_IRQ_NUM; i++) {
3259 irq = platform_get_irq(pdev, i);
3266 ret = devm_request_irq(&pdev->dev, irq, fec_enet_interrupt,
3267 0, pdev->name, ndev);
3272 init_completion(&fep->mdio_done);
3273 ret = fec_enet_mii_init(pdev);
3275 goto failed_mii_init;
3277 /* Carrier starts down, phylib will bring it up */
3278 netif_carrier_off(ndev);
3279 fec_enet_clk_enable(ndev, false);
3280 pinctrl_pm_select_sleep_state(&pdev->dev);
3282 ret = register_netdev(ndev);
3284 goto failed_register;
3286 if (fep->bufdesc_ex && fep->ptp_clock)
3287 netdev_info(ndev, "registered PHC device %d\n", fep->dev_id);
3289 fep->rx_copybreak = COPYBREAK_DEFAULT;
3290 INIT_WORK(&fep->tx_timeout_work, fec_enet_timeout_work);
3294 fec_enet_mii_remove(fep);
3299 regulator_disable(fep->reg_phy);
3301 fec_enet_clk_enable(ndev, false);
3304 of_node_put(phy_node);
3312 fec_drv_remove(struct platform_device *pdev)
3314 struct net_device *ndev = platform_get_drvdata(pdev);
3315 struct fec_enet_private *fep = netdev_priv(ndev);
3317 cancel_delayed_work_sync(&fep->time_keep);
3318 cancel_work_sync(&fep->tx_timeout_work);
3319 unregister_netdev(ndev);
3320 fec_enet_mii_remove(fep);
3322 regulator_disable(fep->reg_phy);
3324 ptp_clock_unregister(fep->ptp_clock);
3325 fec_enet_clk_enable(ndev, false);
3326 of_node_put(fep->phy_node);
3332 static int __maybe_unused fec_suspend(struct device *dev)
3334 struct net_device *ndev = dev_get_drvdata(dev);
3335 struct fec_enet_private *fep = netdev_priv(ndev);
3338 if (netif_running(ndev)) {
3339 phy_stop(fep->phy_dev);
3340 napi_disable(&fep->napi);
3341 netif_tx_lock_bh(ndev);
3342 netif_device_detach(ndev);
3343 netif_tx_unlock_bh(ndev);
3348 fec_enet_clk_enable(ndev, false);
3349 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
3352 regulator_disable(fep->reg_phy);
3357 static int __maybe_unused fec_resume(struct device *dev)
3359 struct net_device *ndev = dev_get_drvdata(dev);
3360 struct fec_enet_private *fep = netdev_priv(ndev);
3364 ret = regulator_enable(fep->reg_phy);
3369 pinctrl_pm_select_default_state(&fep->pdev->dev);
3370 ret = fec_enet_clk_enable(ndev, true);
3375 if (netif_running(ndev)) {
3377 netif_tx_lock_bh(ndev);
3378 netif_device_attach(ndev);
3379 netif_tx_unlock_bh(ndev);
3380 napi_enable(&fep->napi);
3381 phy_start(fep->phy_dev);
3389 regulator_disable(fep->reg_phy);
3393 static SIMPLE_DEV_PM_OPS(fec_pm_ops, fec_suspend, fec_resume);
3395 static struct platform_driver fec_driver = {
3397 .name = DRIVER_NAME,
3398 .owner = THIS_MODULE,
3400 .of_match_table = fec_dt_ids,
3402 .id_table = fec_devtype,
3404 .remove = fec_drv_remove,
3407 module_platform_driver(fec_driver);
3409 MODULE_ALIAS("platform:"DRIVER_NAME);
3410 MODULE_LICENSE("GPL");