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/init.h>
33 #include <linux/delay.h>
34 #include <linux/netdevice.h>
35 #include <linux/etherdevice.h>
36 #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_net.h>
56 #include <linux/regulator/consumer.h>
57 #include <linux/if_vlan.h>
59 #include <asm/cacheflush.h>
63 static void set_multicast_list(struct net_device *ndev);
65 #if defined(CONFIG_ARM)
66 #define FEC_ALIGNMENT 0xf
68 #define FEC_ALIGNMENT 0x3
71 #define DRIVER_NAME "fec"
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 /* Controller is ENET-MAC */
82 #define FEC_QUIRK_ENET_MAC (1 << 0)
83 /* Controller needs driver to swap frame */
84 #define FEC_QUIRK_SWAP_FRAME (1 << 1)
85 /* Controller uses gasket */
86 #define FEC_QUIRK_USE_GASKET (1 << 2)
87 /* Controller has GBIT support */
88 #define FEC_QUIRK_HAS_GBIT (1 << 3)
89 /* Controller has extend desc buffer */
90 #define FEC_QUIRK_HAS_BUFDESC_EX (1 << 4)
91 /* Controller has hardware checksum support */
92 #define FEC_QUIRK_HAS_CSUM (1 << 5)
93 /* Controller has hardware vlan support */
94 #define FEC_QUIRK_HAS_VLAN (1 << 6)
95 /* ENET IP errata ERR006358
97 * If the ready bit in the transmit buffer descriptor (TxBD[R]) is previously
98 * detected as not set during a prior frame transmission, then the
99 * ENET_TDAR[TDAR] bit is cleared at a later time, even if additional TxBDs
100 * were added to the ring and the ENET_TDAR[TDAR] bit is set. This results in
101 * If the ready bit in the transmit buffer descriptor (TxBD[R]) is previously
102 * detected as not set during a prior frame transmission, then the
103 * ENET_TDAR[TDAR] bit is cleared at a later time, even if additional TxBDs
104 * were added to the ring and the ENET_TDAR[TDAR] bit is set. This results in
105 * frames not being transmitted until there is a 0-to-1 transition on
108 #define FEC_QUIRK_ERR006358 (1 << 7)
110 static struct platform_device_id fec_devtype[] = {
112 /* keep it for coldfire */
117 .driver_data = FEC_QUIRK_USE_GASKET,
123 .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_SWAP_FRAME,
126 .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
127 FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
128 FEC_QUIRK_HAS_VLAN | FEC_QUIRK_ERR006358,
130 .name = "mvf600-fec",
131 .driver_data = FEC_QUIRK_ENET_MAC,
136 MODULE_DEVICE_TABLE(platform, fec_devtype);
139 IMX25_FEC = 1, /* runs on i.mx25/50/53 */
140 IMX27_FEC, /* runs on i.mx27/35/51 */
146 static const struct of_device_id fec_dt_ids[] = {
147 { .compatible = "fsl,imx25-fec", .data = &fec_devtype[IMX25_FEC], },
148 { .compatible = "fsl,imx27-fec", .data = &fec_devtype[IMX27_FEC], },
149 { .compatible = "fsl,imx28-fec", .data = &fec_devtype[IMX28_FEC], },
150 { .compatible = "fsl,imx6q-fec", .data = &fec_devtype[IMX6Q_FEC], },
151 { .compatible = "fsl,mvf600-fec", .data = &fec_devtype[MVF600_FEC], },
154 MODULE_DEVICE_TABLE(of, fec_dt_ids);
156 static unsigned char macaddr[ETH_ALEN];
157 module_param_array(macaddr, byte, NULL, 0);
158 MODULE_PARM_DESC(macaddr, "FEC Ethernet MAC address");
160 #if defined(CONFIG_M5272)
162 * Some hardware gets it MAC address out of local flash memory.
163 * if this is non-zero then assume it is the address to get MAC from.
165 #if defined(CONFIG_NETtel)
166 #define FEC_FLASHMAC 0xf0006006
167 #elif defined(CONFIG_GILBARCONAP) || defined(CONFIG_SCALES)
168 #define FEC_FLASHMAC 0xf0006000
169 #elif defined(CONFIG_CANCam)
170 #define FEC_FLASHMAC 0xf0020000
171 #elif defined (CONFIG_M5272C3)
172 #define FEC_FLASHMAC (0xffe04000 + 4)
173 #elif defined(CONFIG_MOD5272)
174 #define FEC_FLASHMAC 0xffc0406b
176 #define FEC_FLASHMAC 0
178 #endif /* CONFIG_M5272 */
180 #if (((RX_RING_SIZE + TX_RING_SIZE) * 32) > PAGE_SIZE)
181 #error "FEC: descriptor ring size constants too large"
184 /* Interrupt events/masks. */
185 #define FEC_ENET_HBERR ((uint)0x80000000) /* Heartbeat error */
186 #define FEC_ENET_BABR ((uint)0x40000000) /* Babbling receiver */
187 #define FEC_ENET_BABT ((uint)0x20000000) /* Babbling transmitter */
188 #define FEC_ENET_GRA ((uint)0x10000000) /* Graceful stop complete */
189 #define FEC_ENET_TXF ((uint)0x08000000) /* Full frame transmitted */
190 #define FEC_ENET_TXB ((uint)0x04000000) /* A buffer was transmitted */
191 #define FEC_ENET_RXF ((uint)0x02000000) /* Full frame received */
192 #define FEC_ENET_RXB ((uint)0x01000000) /* A buffer was received */
193 #define FEC_ENET_MII ((uint)0x00800000) /* MII interrupt */
194 #define FEC_ENET_EBERR ((uint)0x00400000) /* SDMA bus error */
196 #define FEC_DEFAULT_IMASK (FEC_ENET_TXF | FEC_ENET_RXF | FEC_ENET_MII)
197 #define FEC_RX_DISABLED_IMASK (FEC_DEFAULT_IMASK & (~FEC_ENET_RXF))
199 /* The FEC stores dest/src/type/vlan, data, and checksum for receive packets.
201 #define PKT_MAXBUF_SIZE 1522
202 #define PKT_MINBUF_SIZE 64
203 #define PKT_MAXBLR_SIZE 1536
205 /* FEC receive acceleration */
206 #define FEC_RACC_IPDIS (1 << 1)
207 #define FEC_RACC_PRODIS (1 << 2)
208 #define FEC_RACC_OPTIONS (FEC_RACC_IPDIS | FEC_RACC_PRODIS)
211 * The 5270/5271/5280/5282/532x RX control register also contains maximum frame
212 * size bits. Other FEC hardware does not, so we need to take that into
213 * account when setting it.
215 #if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
216 defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM)
217 #define OPT_FRAME_SIZE (PKT_MAXBUF_SIZE << 16)
219 #define OPT_FRAME_SIZE 0
222 /* FEC MII MMFR bits definition */
223 #define FEC_MMFR_ST (1 << 30)
224 #define FEC_MMFR_OP_READ (2 << 28)
225 #define FEC_MMFR_OP_WRITE (1 << 28)
226 #define FEC_MMFR_PA(v) ((v & 0x1f) << 23)
227 #define FEC_MMFR_RA(v) ((v & 0x1f) << 18)
228 #define FEC_MMFR_TA (2 << 16)
229 #define FEC_MMFR_DATA(v) (v & 0xffff)
231 #define FEC_MII_TIMEOUT 30000 /* us */
233 /* Transmitter timeout */
234 #define TX_TIMEOUT (2 * HZ)
236 #define FEC_PAUSE_FLAG_AUTONEG 0x1
237 #define FEC_PAUSE_FLAG_ENABLE 0x2
242 struct bufdesc *fec_enet_get_nextdesc(struct bufdesc *bdp, struct fec_enet_private *fep)
244 struct bufdesc *new_bd = bdp + 1;
245 struct bufdesc_ex *ex_new_bd = (struct bufdesc_ex *)bdp + 1;
246 struct bufdesc_ex *ex_base;
247 struct bufdesc *base;
250 if (bdp >= fep->tx_bd_base) {
251 base = fep->tx_bd_base;
252 ring_size = fep->tx_ring_size;
253 ex_base = (struct bufdesc_ex *)fep->tx_bd_base;
255 base = fep->rx_bd_base;
256 ring_size = fep->rx_ring_size;
257 ex_base = (struct bufdesc_ex *)fep->rx_bd_base;
261 return (struct bufdesc *)((ex_new_bd >= (ex_base + ring_size)) ?
262 ex_base : ex_new_bd);
264 return (new_bd >= (base + ring_size)) ?
269 struct bufdesc *fec_enet_get_prevdesc(struct bufdesc *bdp, struct fec_enet_private *fep)
271 struct bufdesc *new_bd = bdp - 1;
272 struct bufdesc_ex *ex_new_bd = (struct bufdesc_ex *)bdp - 1;
273 struct bufdesc_ex *ex_base;
274 struct bufdesc *base;
277 if (bdp >= fep->tx_bd_base) {
278 base = fep->tx_bd_base;
279 ring_size = fep->tx_ring_size;
280 ex_base = (struct bufdesc_ex *)fep->tx_bd_base;
282 base = fep->rx_bd_base;
283 ring_size = fep->rx_ring_size;
284 ex_base = (struct bufdesc_ex *)fep->rx_bd_base;
288 return (struct bufdesc *)((ex_new_bd < ex_base) ?
289 (ex_new_bd + ring_size) : ex_new_bd);
291 return (new_bd < base) ? (new_bd + ring_size) : new_bd;
294 static void *swap_buffer(void *bufaddr, int len)
297 unsigned int *buf = bufaddr;
299 for (i = 0; i < DIV_ROUND_UP(len, 4); i++, buf++)
300 *buf = cpu_to_be32(*buf);
306 fec_enet_clear_csum(struct sk_buff *skb, struct net_device *ndev)
308 /* Only run for packets requiring a checksum. */
309 if (skb->ip_summed != CHECKSUM_PARTIAL)
312 if (unlikely(skb_cow_head(skb, 0)))
315 *(__sum16 *)(skb->head + skb->csum_start + skb->csum_offset) = 0;
321 fec_enet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
323 struct fec_enet_private *fep = netdev_priv(ndev);
324 const struct platform_device_id *id_entry =
325 platform_get_device_id(fep->pdev);
326 struct bufdesc *bdp, *bdp_pre;
328 unsigned short status;
331 /* Fill in a Tx ring entry */
334 status = bdp->cbd_sc;
336 if (status & BD_ENET_TX_READY) {
337 /* Ooops. All transmit buffers are full. Bail out.
338 * This should not happen, since ndev->tbusy should be set.
340 netdev_err(ndev, "tx queue full!\n");
341 return NETDEV_TX_BUSY;
344 /* Protocol checksum off-load for TCP and UDP. */
345 if (fec_enet_clear_csum(skb, ndev)) {
350 /* Clear all of the status flags */
351 status &= ~BD_ENET_TX_STATS;
353 /* Set buffer length and buffer pointer */
355 bdp->cbd_datlen = skb->len;
358 * On some FEC implementations data must be aligned on
359 * 4-byte boundaries. Use bounce buffers to copy data
360 * and get it aligned. Ugh.
363 index = (struct bufdesc_ex *)bdp -
364 (struct bufdesc_ex *)fep->tx_bd_base;
366 index = bdp - fep->tx_bd_base;
368 if (((unsigned long) bufaddr) & FEC_ALIGNMENT) {
369 memcpy(fep->tx_bounce[index], skb->data, skb->len);
370 bufaddr = fep->tx_bounce[index];
374 * Some design made an incorrect assumption on endian mode of
375 * the system that it's running on. As the result, driver has to
376 * swap every frame going to and coming from the controller.
378 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
379 swap_buffer(bufaddr, skb->len);
381 /* Save skb pointer */
382 fep->tx_skbuff[index] = skb;
384 /* Push the data cache so the CPM does not get stale memory
387 bdp->cbd_bufaddr = dma_map_single(&fep->pdev->dev, bufaddr,
388 FEC_ENET_TX_FRSIZE, DMA_TO_DEVICE);
390 /* Send it on its way. Tell FEC it's ready, interrupt when done,
391 * it's the last BD of the frame, and to put the CRC on the end.
393 status |= (BD_ENET_TX_READY | BD_ENET_TX_INTR
394 | BD_ENET_TX_LAST | BD_ENET_TX_TC);
395 bdp->cbd_sc = status;
397 if (fep->bufdesc_ex) {
399 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
401 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
403 ebdp->cbd_esc = (BD_ENET_TX_TS | BD_ENET_TX_INT);
404 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
406 ebdp->cbd_esc = BD_ENET_TX_INT;
408 /* Enable protocol checksum flags
409 * We do not bother with the IP Checksum bits as they
410 * are done by the kernel
412 if (skb->ip_summed == CHECKSUM_PARTIAL)
413 ebdp->cbd_esc |= BD_ENET_TX_PINS;
417 bdp_pre = fec_enet_get_prevdesc(bdp, fep);
418 if ((id_entry->driver_data & FEC_QUIRK_ERR006358) &&
419 !(bdp_pre->cbd_sc & BD_ENET_TX_READY)) {
420 fep->delay_work.trig_tx = true;
421 schedule_delayed_work(&(fep->delay_work.delay_work),
422 msecs_to_jiffies(1));
425 /* If this was the last BD in the ring, start at the beginning again. */
426 bdp = fec_enet_get_nextdesc(bdp, fep);
430 if (fep->cur_tx == fep->dirty_tx)
431 netif_stop_queue(ndev);
433 /* Trigger transmission start */
434 writel(0, fep->hwp + FEC_X_DES_ACTIVE);
436 skb_tx_timestamp(skb);
441 /* Init RX & TX buffer descriptors
443 static void fec_enet_bd_init(struct net_device *dev)
445 struct fec_enet_private *fep = netdev_priv(dev);
449 /* Initialize the receive buffer descriptors. */
450 bdp = fep->rx_bd_base;
451 for (i = 0; i < fep->rx_ring_size; i++) {
453 /* Initialize the BD for every fragment in the page. */
454 if (bdp->cbd_bufaddr)
455 bdp->cbd_sc = BD_ENET_RX_EMPTY;
458 bdp = fec_enet_get_nextdesc(bdp, fep);
461 /* Set the last buffer to wrap */
462 bdp = fec_enet_get_prevdesc(bdp, fep);
463 bdp->cbd_sc |= BD_SC_WRAP;
465 fep->cur_rx = fep->rx_bd_base;
467 /* ...and the same for transmit */
468 bdp = fep->tx_bd_base;
470 for (i = 0; i < fep->tx_ring_size; i++) {
472 /* Initialize the BD for every fragment in the page. */
474 if (bdp->cbd_bufaddr && fep->tx_skbuff[i]) {
475 dev_kfree_skb_any(fep->tx_skbuff[i]);
476 fep->tx_skbuff[i] = NULL;
478 bdp->cbd_bufaddr = 0;
479 bdp = fec_enet_get_nextdesc(bdp, fep);
482 /* Set the last buffer to wrap */
483 bdp = fec_enet_get_prevdesc(bdp, fep);
484 bdp->cbd_sc |= BD_SC_WRAP;
488 /* This function is called to start or restart the FEC during a link
489 * change. This only happens when switching between half and full
493 fec_restart(struct net_device *ndev, int duplex)
495 struct fec_enet_private *fep = netdev_priv(ndev);
496 const struct platform_device_id *id_entry =
497 platform_get_device_id(fep->pdev);
501 u32 rcntl = OPT_FRAME_SIZE | 0x04;
502 u32 ecntl = 0x2; /* ETHEREN */
504 if (netif_running(ndev)) {
505 netif_device_detach(ndev);
506 napi_disable(&fep->napi);
507 netif_stop_queue(ndev);
508 netif_tx_lock_bh(ndev);
511 /* Whack a reset. We should wait for this. */
512 writel(1, fep->hwp + FEC_ECNTRL);
516 * enet-mac reset will reset mac address registers too,
517 * so need to reconfigure it.
519 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
520 memcpy(&temp_mac, ndev->dev_addr, ETH_ALEN);
521 writel(cpu_to_be32(temp_mac[0]), fep->hwp + FEC_ADDR_LOW);
522 writel(cpu_to_be32(temp_mac[1]), fep->hwp + FEC_ADDR_HIGH);
525 /* Clear any outstanding interrupt. */
526 writel(0xffc00000, fep->hwp + FEC_IEVENT);
528 /* Setup multicast filter. */
529 set_multicast_list(ndev);
531 writel(0, fep->hwp + FEC_HASH_TABLE_HIGH);
532 writel(0, fep->hwp + FEC_HASH_TABLE_LOW);
535 /* Set maximum receive buffer size. */
536 writel(PKT_MAXBLR_SIZE, fep->hwp + FEC_R_BUFF_SIZE);
538 fec_enet_bd_init(ndev);
540 /* Set receive and transmit descriptor base. */
541 writel(fep->bd_dma, fep->hwp + FEC_R_DES_START);
543 writel((unsigned long)fep->bd_dma + sizeof(struct bufdesc_ex)
544 * fep->rx_ring_size, fep->hwp + FEC_X_DES_START);
546 writel((unsigned long)fep->bd_dma + sizeof(struct bufdesc)
547 * fep->rx_ring_size, fep->hwp + FEC_X_DES_START);
550 for (i = 0; i <= TX_RING_MOD_MASK; i++) {
551 if (fep->tx_skbuff[i]) {
552 dev_kfree_skb_any(fep->tx_skbuff[i]);
553 fep->tx_skbuff[i] = NULL;
557 /* Enable MII mode */
560 writel(0x04, fep->hwp + FEC_X_CNTRL);
564 writel(0x0, fep->hwp + FEC_X_CNTRL);
567 fep->full_duplex = duplex;
570 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
572 #if !defined(CONFIG_M5272)
573 /* set RX checksum */
574 val = readl(fep->hwp + FEC_RACC);
575 if (fep->csum_flags & FLAG_RX_CSUM_ENABLED)
576 val |= FEC_RACC_OPTIONS;
578 val &= ~FEC_RACC_OPTIONS;
579 writel(val, fep->hwp + FEC_RACC);
583 * The phy interface and speed need to get configured
584 * differently on enet-mac.
586 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
587 /* Enable flow control and length check */
588 rcntl |= 0x40000000 | 0x00000020;
590 /* RGMII, RMII or MII */
591 if (fep->phy_interface == PHY_INTERFACE_MODE_RGMII)
593 else if (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
598 /* 1G, 100M or 10M */
600 if (fep->phy_dev->speed == SPEED_1000)
602 else if (fep->phy_dev->speed == SPEED_100)
608 #ifdef FEC_MIIGSK_ENR
609 if (id_entry->driver_data & FEC_QUIRK_USE_GASKET) {
611 /* disable the gasket and wait */
612 writel(0, fep->hwp + FEC_MIIGSK_ENR);
613 while (readl(fep->hwp + FEC_MIIGSK_ENR) & 4)
617 * configure the gasket:
618 * RMII, 50 MHz, no loopback, no echo
619 * MII, 25 MHz, no loopback, no echo
621 cfgr = (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
622 ? BM_MIIGSK_CFGR_RMII : BM_MIIGSK_CFGR_MII;
623 if (fep->phy_dev && fep->phy_dev->speed == SPEED_10)
624 cfgr |= BM_MIIGSK_CFGR_FRCONT_10M;
625 writel(cfgr, fep->hwp + FEC_MIIGSK_CFGR);
627 /* re-enable the gasket */
628 writel(2, fep->hwp + FEC_MIIGSK_ENR);
633 #if !defined(CONFIG_M5272)
634 /* enable pause frame*/
635 if ((fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) ||
636 ((fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) &&
637 fep->phy_dev && fep->phy_dev->pause)) {
638 rcntl |= FEC_ENET_FCE;
640 /* set FIFO threshold parameter to reduce overrun */
641 writel(FEC_ENET_RSEM_V, fep->hwp + FEC_R_FIFO_RSEM);
642 writel(FEC_ENET_RSFL_V, fep->hwp + FEC_R_FIFO_RSFL);
643 writel(FEC_ENET_RAEM_V, fep->hwp + FEC_R_FIFO_RAEM);
644 writel(FEC_ENET_RAFL_V, fep->hwp + FEC_R_FIFO_RAFL);
647 writel(FEC_ENET_OPD_V, fep->hwp + FEC_OPD);
649 rcntl &= ~FEC_ENET_FCE;
651 #endif /* !defined(CONFIG_M5272) */
653 writel(rcntl, fep->hwp + FEC_R_CNTRL);
655 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
656 /* enable ENET endian swap */
658 /* enable ENET store and forward mode */
659 writel(1 << 8, fep->hwp + FEC_X_WMRK);
666 /* Enable the MIB statistic event counters */
667 writel(0 << 31, fep->hwp + FEC_MIB_CTRLSTAT);
670 /* And last, enable the transmit and receive processing */
671 writel(ecntl, fep->hwp + FEC_ECNTRL);
672 writel(0, fep->hwp + FEC_R_DES_ACTIVE);
675 fec_ptp_start_cyclecounter(ndev);
677 /* Enable interrupts we wish to service */
678 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
680 if (netif_running(ndev)) {
681 netif_tx_unlock_bh(ndev);
682 netif_wake_queue(ndev);
683 napi_enable(&fep->napi);
684 netif_device_attach(ndev);
689 fec_stop(struct net_device *ndev)
691 struct fec_enet_private *fep = netdev_priv(ndev);
692 const struct platform_device_id *id_entry =
693 platform_get_device_id(fep->pdev);
694 u32 rmii_mode = readl(fep->hwp + FEC_R_CNTRL) & (1 << 8);
696 /* We cannot expect a graceful transmit stop without link !!! */
698 writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */
700 if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA))
701 netdev_err(ndev, "Graceful transmit stop did not complete!\n");
704 /* Whack a reset. We should wait for this. */
705 writel(1, fep->hwp + FEC_ECNTRL);
707 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
708 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
710 /* We have to keep ENET enabled to have MII interrupt stay working */
711 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
712 writel(2, fep->hwp + FEC_ECNTRL);
713 writel(rmii_mode, fep->hwp + FEC_R_CNTRL);
719 fec_timeout(struct net_device *ndev)
721 struct fec_enet_private *fep = netdev_priv(ndev);
723 ndev->stats.tx_errors++;
725 fep->delay_work.timeout = true;
726 schedule_delayed_work(&(fep->delay_work.delay_work), 0);
729 static void fec_enet_work(struct work_struct *work)
731 struct fec_enet_private *fep =
733 struct fec_enet_private,
734 delay_work.delay_work.work);
736 if (fep->delay_work.timeout) {
737 fep->delay_work.timeout = false;
738 fec_restart(fep->netdev, fep->full_duplex);
739 netif_wake_queue(fep->netdev);
742 if (fep->delay_work.trig_tx) {
743 fep->delay_work.trig_tx = false;
744 writel(0, fep->hwp + FEC_X_DES_ACTIVE);
749 fec_enet_tx(struct net_device *ndev)
751 struct fec_enet_private *fep;
753 unsigned short status;
757 fep = netdev_priv(ndev);
760 /* get next bdp of dirty_tx */
761 bdp = fec_enet_get_nextdesc(bdp, fep);
763 while (((status = bdp->cbd_sc) & BD_ENET_TX_READY) == 0) {
765 /* current queue is empty */
766 if (bdp == fep->cur_tx)
770 index = (struct bufdesc_ex *)bdp -
771 (struct bufdesc_ex *)fep->tx_bd_base;
773 index = bdp - fep->tx_bd_base;
775 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
776 FEC_ENET_TX_FRSIZE, DMA_TO_DEVICE);
777 bdp->cbd_bufaddr = 0;
779 skb = fep->tx_skbuff[index];
781 /* Check for errors. */
782 if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC |
783 BD_ENET_TX_RL | BD_ENET_TX_UN |
785 ndev->stats.tx_errors++;
786 if (status & BD_ENET_TX_HB) /* No heartbeat */
787 ndev->stats.tx_heartbeat_errors++;
788 if (status & BD_ENET_TX_LC) /* Late collision */
789 ndev->stats.tx_window_errors++;
790 if (status & BD_ENET_TX_RL) /* Retrans limit */
791 ndev->stats.tx_aborted_errors++;
792 if (status & BD_ENET_TX_UN) /* Underrun */
793 ndev->stats.tx_fifo_errors++;
794 if (status & BD_ENET_TX_CSL) /* Carrier lost */
795 ndev->stats.tx_carrier_errors++;
797 ndev->stats.tx_packets++;
798 ndev->stats.tx_bytes += bdp->cbd_datlen;
801 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) &&
803 struct skb_shared_hwtstamps shhwtstamps;
805 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
807 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
808 spin_lock_irqsave(&fep->tmreg_lock, flags);
809 shhwtstamps.hwtstamp = ns_to_ktime(
810 timecounter_cyc2time(&fep->tc, ebdp->ts));
811 spin_unlock_irqrestore(&fep->tmreg_lock, flags);
812 skb_tstamp_tx(skb, &shhwtstamps);
815 if (status & BD_ENET_TX_READY)
816 netdev_err(ndev, "HEY! Enet xmit interrupt and TX_READY\n");
818 /* Deferred means some collisions occurred during transmit,
819 * but we eventually sent the packet OK.
821 if (status & BD_ENET_TX_DEF)
822 ndev->stats.collisions++;
824 /* Free the sk buffer associated with this last transmit */
825 dev_kfree_skb_any(skb);
826 fep->tx_skbuff[index] = NULL;
830 /* Update pointer to next buffer descriptor to be transmitted */
831 bdp = fec_enet_get_nextdesc(bdp, fep);
833 /* Since we have freed up a buffer, the ring is no longer full
835 if (fep->dirty_tx != fep->cur_tx) {
836 if (netif_queue_stopped(ndev))
837 netif_wake_queue(ndev);
844 /* During a receive, the cur_rx points to the current incoming buffer.
845 * When we update through the ring, if the next incoming buffer has
846 * not been given to the system, we just set the empty indicator,
847 * effectively tossing the packet.
850 fec_enet_rx(struct net_device *ndev, int budget)
852 struct fec_enet_private *fep = netdev_priv(ndev);
853 const struct platform_device_id *id_entry =
854 platform_get_device_id(fep->pdev);
856 unsigned short status;
860 int pkt_received = 0;
861 struct bufdesc_ex *ebdp = NULL;
862 bool vlan_packet_rcvd = false;
869 /* First, grab all of the stats for the incoming packet.
870 * These get messed up if we get called due to a busy condition.
874 while (!((status = bdp->cbd_sc) & BD_ENET_RX_EMPTY)) {
876 if (pkt_received >= budget)
880 /* Since we have allocated space to hold a complete frame,
881 * the last indicator should be set.
883 if ((status & BD_ENET_RX_LAST) == 0)
884 netdev_err(ndev, "rcv is not +last\n");
887 goto rx_processing_done;
889 /* Check for errors. */
890 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
891 BD_ENET_RX_CR | BD_ENET_RX_OV)) {
892 ndev->stats.rx_errors++;
893 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH)) {
894 /* Frame too long or too short. */
895 ndev->stats.rx_length_errors++;
897 if (status & BD_ENET_RX_NO) /* Frame alignment */
898 ndev->stats.rx_frame_errors++;
899 if (status & BD_ENET_RX_CR) /* CRC Error */
900 ndev->stats.rx_crc_errors++;
901 if (status & BD_ENET_RX_OV) /* FIFO overrun */
902 ndev->stats.rx_fifo_errors++;
905 /* Report late collisions as a frame error.
906 * On this error, the BD is closed, but we don't know what we
907 * have in the buffer. So, just drop this frame on the floor.
909 if (status & BD_ENET_RX_CL) {
910 ndev->stats.rx_errors++;
911 ndev->stats.rx_frame_errors++;
912 goto rx_processing_done;
915 /* Process the incoming frame. */
916 ndev->stats.rx_packets++;
917 pkt_len = bdp->cbd_datlen;
918 ndev->stats.rx_bytes += pkt_len;
919 data = (__u8*)__va(bdp->cbd_bufaddr);
921 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
922 FEC_ENET_TX_FRSIZE, DMA_FROM_DEVICE);
924 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
925 swap_buffer(data, pkt_len);
927 /* Extract the enhanced buffer descriptor */
930 ebdp = (struct bufdesc_ex *)bdp;
932 /* If this is a VLAN packet remove the VLAN Tag */
933 vlan_packet_rcvd = false;
934 if ((ndev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
935 fep->bufdesc_ex && (ebdp->cbd_esc & BD_ENET_RX_VLAN)) {
936 /* Push and remove the vlan tag */
937 struct vlan_hdr *vlan_header =
938 (struct vlan_hdr *) (data + ETH_HLEN);
939 vlan_tag = ntohs(vlan_header->h_vlan_TCI);
940 pkt_len -= VLAN_HLEN;
942 vlan_packet_rcvd = true;
945 /* This does 16 byte alignment, exactly what we need.
946 * The packet length includes FCS, but we don't want to
947 * include that when passing upstream as it messes up
948 * bridging applications.
950 skb = netdev_alloc_skb(ndev, pkt_len - 4 + NET_IP_ALIGN);
952 if (unlikely(!skb)) {
953 ndev->stats.rx_dropped++;
955 int payload_offset = (2 * ETH_ALEN);
956 skb_reserve(skb, NET_IP_ALIGN);
957 skb_put(skb, pkt_len - 4); /* Make room */
959 /* Extract the frame data without the VLAN header. */
960 skb_copy_to_linear_data(skb, data, (2 * ETH_ALEN));
961 if (vlan_packet_rcvd)
962 payload_offset = (2 * ETH_ALEN) + VLAN_HLEN;
963 skb_copy_to_linear_data_offset(skb, (2 * ETH_ALEN),
964 data + payload_offset,
965 pkt_len - 4 - (2 * ETH_ALEN));
967 skb->protocol = eth_type_trans(skb, ndev);
969 /* Get receive timestamp from the skb */
970 if (fep->hwts_rx_en && fep->bufdesc_ex) {
971 struct skb_shared_hwtstamps *shhwtstamps =
975 memset(shhwtstamps, 0, sizeof(*shhwtstamps));
977 spin_lock_irqsave(&fep->tmreg_lock, flags);
978 shhwtstamps->hwtstamp = ns_to_ktime(
979 timecounter_cyc2time(&fep->tc, ebdp->ts));
980 spin_unlock_irqrestore(&fep->tmreg_lock, flags);
983 if (fep->bufdesc_ex &&
984 (fep->csum_flags & FLAG_RX_CSUM_ENABLED)) {
985 if (!(ebdp->cbd_esc & FLAG_RX_CSUM_ERROR)) {
987 skb->ip_summed = CHECKSUM_UNNECESSARY;
989 skb_checksum_none_assert(skb);
993 /* Handle received VLAN packets */
994 if (vlan_packet_rcvd)
995 __vlan_hwaccel_put_tag(skb,
999 napi_gro_receive(&fep->napi, skb);
1002 bdp->cbd_bufaddr = dma_map_single(&fep->pdev->dev, data,
1003 FEC_ENET_TX_FRSIZE, DMA_FROM_DEVICE);
1005 /* Clear the status flags for this buffer */
1006 status &= ~BD_ENET_RX_STATS;
1008 /* Mark the buffer empty */
1009 status |= BD_ENET_RX_EMPTY;
1010 bdp->cbd_sc = status;
1012 if (fep->bufdesc_ex) {
1013 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
1015 ebdp->cbd_esc = BD_ENET_RX_INT;
1020 /* Update BD pointer to next entry */
1021 bdp = fec_enet_get_nextdesc(bdp, fep);
1023 /* Doing this here will keep the FEC running while we process
1024 * incoming frames. On a heavily loaded network, we should be
1025 * able to keep up at the expense of system resources.
1027 writel(0, fep->hwp + FEC_R_DES_ACTIVE);
1031 return pkt_received;
1035 fec_enet_interrupt(int irq, void *dev_id)
1037 struct net_device *ndev = dev_id;
1038 struct fec_enet_private *fep = netdev_priv(ndev);
1040 irqreturn_t ret = IRQ_NONE;
1043 int_events = readl(fep->hwp + FEC_IEVENT);
1044 writel(int_events, fep->hwp + FEC_IEVENT);
1046 if (int_events & (FEC_ENET_RXF | FEC_ENET_TXF)) {
1049 /* Disable the RX interrupt */
1050 if (napi_schedule_prep(&fep->napi)) {
1051 writel(FEC_RX_DISABLED_IMASK,
1052 fep->hwp + FEC_IMASK);
1053 __napi_schedule(&fep->napi);
1057 if (int_events & FEC_ENET_MII) {
1059 complete(&fep->mdio_done);
1061 } while (int_events);
1066 static int fec_enet_rx_napi(struct napi_struct *napi, int budget)
1068 struct net_device *ndev = napi->dev;
1069 int pkts = fec_enet_rx(ndev, budget);
1070 struct fec_enet_private *fep = netdev_priv(ndev);
1074 if (pkts < budget) {
1075 napi_complete(napi);
1076 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1081 /* ------------------------------------------------------------------------- */
1082 static void fec_get_mac(struct net_device *ndev)
1084 struct fec_enet_private *fep = netdev_priv(ndev);
1085 struct fec_platform_data *pdata = dev_get_platdata(&fep->pdev->dev);
1086 unsigned char *iap, tmpaddr[ETH_ALEN];
1089 * try to get mac address in following order:
1091 * 1) module parameter via kernel command line in form
1092 * fec.macaddr=0x00,0x04,0x9f,0x01,0x30,0xe0
1097 * 2) from device tree data
1099 if (!is_valid_ether_addr(iap)) {
1100 struct device_node *np = fep->pdev->dev.of_node;
1102 const char *mac = of_get_mac_address(np);
1104 iap = (unsigned char *) mac;
1109 * 3) from flash or fuse (via platform data)
1111 if (!is_valid_ether_addr(iap)) {
1114 iap = (unsigned char *)FEC_FLASHMAC;
1117 iap = (unsigned char *)&pdata->mac;
1122 * 4) FEC mac registers set by bootloader
1124 if (!is_valid_ether_addr(iap)) {
1125 *((__be32 *) &tmpaddr[0]) =
1126 cpu_to_be32(readl(fep->hwp + FEC_ADDR_LOW));
1127 *((__be16 *) &tmpaddr[4]) =
1128 cpu_to_be16(readl(fep->hwp + FEC_ADDR_HIGH) >> 16);
1133 * 5) random mac address
1135 if (!is_valid_ether_addr(iap)) {
1136 /* Report it and use a random ethernet address instead */
1137 netdev_err(ndev, "Invalid MAC address: %pM\n", iap);
1138 eth_hw_addr_random(ndev);
1139 netdev_info(ndev, "Using random MAC address: %pM\n",
1144 memcpy(ndev->dev_addr, iap, ETH_ALEN);
1146 /* Adjust MAC if using macaddr */
1148 ndev->dev_addr[ETH_ALEN-1] = macaddr[ETH_ALEN-1] + fep->dev_id;
1151 /* ------------------------------------------------------------------------- */
1156 static void fec_enet_adjust_link(struct net_device *ndev)
1158 struct fec_enet_private *fep = netdev_priv(ndev);
1159 struct phy_device *phy_dev = fep->phy_dev;
1160 int status_change = 0;
1162 /* Prevent a state halted on mii error */
1163 if (fep->mii_timeout && phy_dev->state == PHY_HALTED) {
1164 phy_dev->state = PHY_RESUMING;
1168 if (phy_dev->link) {
1170 fep->link = phy_dev->link;
1174 if (fep->full_duplex != phy_dev->duplex)
1177 if (phy_dev->speed != fep->speed) {
1178 fep->speed = phy_dev->speed;
1182 /* if any of the above changed restart the FEC */
1184 fec_restart(ndev, phy_dev->duplex);
1188 fep->link = phy_dev->link;
1194 phy_print_status(phy_dev);
1197 static int fec_enet_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
1199 struct fec_enet_private *fep = bus->priv;
1200 unsigned long time_left;
1202 fep->mii_timeout = 0;
1203 init_completion(&fep->mdio_done);
1205 /* start a read op */
1206 writel(FEC_MMFR_ST | FEC_MMFR_OP_READ |
1207 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1208 FEC_MMFR_TA, fep->hwp + FEC_MII_DATA);
1210 /* wait for end of transfer */
1211 time_left = wait_for_completion_timeout(&fep->mdio_done,
1212 usecs_to_jiffies(FEC_MII_TIMEOUT));
1213 if (time_left == 0) {
1214 fep->mii_timeout = 1;
1215 netdev_err(fep->netdev, "MDIO read timeout\n");
1220 return FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA));
1223 static int fec_enet_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
1226 struct fec_enet_private *fep = bus->priv;
1227 unsigned long time_left;
1229 fep->mii_timeout = 0;
1230 init_completion(&fep->mdio_done);
1232 /* start a write op */
1233 writel(FEC_MMFR_ST | FEC_MMFR_OP_WRITE |
1234 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1235 FEC_MMFR_TA | FEC_MMFR_DATA(value),
1236 fep->hwp + FEC_MII_DATA);
1238 /* wait for end of transfer */
1239 time_left = wait_for_completion_timeout(&fep->mdio_done,
1240 usecs_to_jiffies(FEC_MII_TIMEOUT));
1241 if (time_left == 0) {
1242 fep->mii_timeout = 1;
1243 netdev_err(fep->netdev, "MDIO write timeout\n");
1250 static int fec_enet_mdio_reset(struct mii_bus *bus)
1255 static int fec_enet_mii_probe(struct net_device *ndev)
1257 struct fec_enet_private *fep = netdev_priv(ndev);
1258 const struct platform_device_id *id_entry =
1259 platform_get_device_id(fep->pdev);
1260 struct phy_device *phy_dev = NULL;
1261 char mdio_bus_id[MII_BUS_ID_SIZE];
1262 char phy_name[MII_BUS_ID_SIZE + 3];
1264 int dev_id = fep->dev_id;
1266 fep->phy_dev = NULL;
1268 /* check for attached phy */
1269 for (phy_id = 0; (phy_id < PHY_MAX_ADDR); phy_id++) {
1270 if ((fep->mii_bus->phy_mask & (1 << phy_id)))
1272 if (fep->mii_bus->phy_map[phy_id] == NULL)
1274 if (fep->mii_bus->phy_map[phy_id]->phy_id == 0)
1278 strncpy(mdio_bus_id, fep->mii_bus->id, MII_BUS_ID_SIZE);
1282 if (phy_id >= PHY_MAX_ADDR) {
1283 netdev_info(ndev, "no PHY, assuming direct connection to switch\n");
1284 strncpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE);
1288 snprintf(phy_name, sizeof(phy_name), PHY_ID_FMT, mdio_bus_id, phy_id);
1289 phy_dev = phy_connect(ndev, phy_name, &fec_enet_adjust_link,
1290 fep->phy_interface);
1291 if (IS_ERR(phy_dev)) {
1292 netdev_err(ndev, "could not attach to PHY\n");
1293 return PTR_ERR(phy_dev);
1296 /* mask with MAC supported features */
1297 if (id_entry->driver_data & FEC_QUIRK_HAS_GBIT) {
1298 phy_dev->supported &= PHY_GBIT_FEATURES;
1299 #if !defined(CONFIG_M5272)
1300 phy_dev->supported |= SUPPORTED_Pause;
1304 phy_dev->supported &= PHY_BASIC_FEATURES;
1306 phy_dev->advertising = phy_dev->supported;
1308 fep->phy_dev = phy_dev;
1310 fep->full_duplex = 0;
1312 netdev_info(ndev, "Freescale FEC PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
1313 fep->phy_dev->drv->name, dev_name(&fep->phy_dev->dev),
1319 static int fec_enet_mii_init(struct platform_device *pdev)
1321 static struct mii_bus *fec0_mii_bus;
1322 struct net_device *ndev = platform_get_drvdata(pdev);
1323 struct fec_enet_private *fep = netdev_priv(ndev);
1324 const struct platform_device_id *id_entry =
1325 platform_get_device_id(fep->pdev);
1326 int err = -ENXIO, i;
1329 * The dual fec interfaces are not equivalent with enet-mac.
1330 * Here are the differences:
1332 * - fec0 supports MII & RMII modes while fec1 only supports RMII
1333 * - fec0 acts as the 1588 time master while fec1 is slave
1334 * - external phys can only be configured by fec0
1336 * That is to say fec1 can not work independently. It only works
1337 * when fec0 is working. The reason behind this design is that the
1338 * second interface is added primarily for Switch mode.
1340 * Because of the last point above, both phys are attached on fec0
1341 * mdio interface in board design, and need to be configured by
1344 if ((id_entry->driver_data & FEC_QUIRK_ENET_MAC) && fep->dev_id > 0) {
1345 /* fec1 uses fec0 mii_bus */
1346 if (mii_cnt && fec0_mii_bus) {
1347 fep->mii_bus = fec0_mii_bus;
1354 fep->mii_timeout = 0;
1357 * Set MII speed to 2.5 MHz (= clk_get_rate() / 2 * phy_speed)
1359 * The formula for FEC MDC is 'ref_freq / (MII_SPEED x 2)' while
1360 * for ENET-MAC is 'ref_freq / ((MII_SPEED + 1) x 2)'. The i.MX28
1361 * Reference Manual has an error on this, and gets fixed on i.MX6Q
1364 fep->phy_speed = DIV_ROUND_UP(clk_get_rate(fep->clk_ahb), 5000000);
1365 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
1367 fep->phy_speed <<= 1;
1368 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
1370 fep->mii_bus = mdiobus_alloc();
1371 if (fep->mii_bus == NULL) {
1376 fep->mii_bus->name = "fec_enet_mii_bus";
1377 fep->mii_bus->read = fec_enet_mdio_read;
1378 fep->mii_bus->write = fec_enet_mdio_write;
1379 fep->mii_bus->reset = fec_enet_mdio_reset;
1380 snprintf(fep->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
1381 pdev->name, fep->dev_id + 1);
1382 fep->mii_bus->priv = fep;
1383 fep->mii_bus->parent = &pdev->dev;
1385 fep->mii_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
1386 if (!fep->mii_bus->irq) {
1388 goto err_out_free_mdiobus;
1391 for (i = 0; i < PHY_MAX_ADDR; i++)
1392 fep->mii_bus->irq[i] = PHY_POLL;
1394 if (mdiobus_register(fep->mii_bus))
1395 goto err_out_free_mdio_irq;
1399 /* save fec0 mii_bus */
1400 if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
1401 fec0_mii_bus = fep->mii_bus;
1405 err_out_free_mdio_irq:
1406 kfree(fep->mii_bus->irq);
1407 err_out_free_mdiobus:
1408 mdiobus_free(fep->mii_bus);
1413 static void fec_enet_mii_remove(struct fec_enet_private *fep)
1415 if (--mii_cnt == 0) {
1416 mdiobus_unregister(fep->mii_bus);
1417 kfree(fep->mii_bus->irq);
1418 mdiobus_free(fep->mii_bus);
1422 static int fec_enet_get_settings(struct net_device *ndev,
1423 struct ethtool_cmd *cmd)
1425 struct fec_enet_private *fep = netdev_priv(ndev);
1426 struct phy_device *phydev = fep->phy_dev;
1431 return phy_ethtool_gset(phydev, cmd);
1434 static int fec_enet_set_settings(struct net_device *ndev,
1435 struct ethtool_cmd *cmd)
1437 struct fec_enet_private *fep = netdev_priv(ndev);
1438 struct phy_device *phydev = fep->phy_dev;
1443 return phy_ethtool_sset(phydev, cmd);
1446 static void fec_enet_get_drvinfo(struct net_device *ndev,
1447 struct ethtool_drvinfo *info)
1449 struct fec_enet_private *fep = netdev_priv(ndev);
1451 strlcpy(info->driver, fep->pdev->dev.driver->name,
1452 sizeof(info->driver));
1453 strlcpy(info->version, "Revision: 1.0", sizeof(info->version));
1454 strlcpy(info->bus_info, dev_name(&ndev->dev), sizeof(info->bus_info));
1457 static int fec_enet_get_ts_info(struct net_device *ndev,
1458 struct ethtool_ts_info *info)
1460 struct fec_enet_private *fep = netdev_priv(ndev);
1462 if (fep->bufdesc_ex) {
1464 info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
1465 SOF_TIMESTAMPING_RX_SOFTWARE |
1466 SOF_TIMESTAMPING_SOFTWARE |
1467 SOF_TIMESTAMPING_TX_HARDWARE |
1468 SOF_TIMESTAMPING_RX_HARDWARE |
1469 SOF_TIMESTAMPING_RAW_HARDWARE;
1471 info->phc_index = ptp_clock_index(fep->ptp_clock);
1473 info->phc_index = -1;
1475 info->tx_types = (1 << HWTSTAMP_TX_OFF) |
1476 (1 << HWTSTAMP_TX_ON);
1478 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
1479 (1 << HWTSTAMP_FILTER_ALL);
1482 return ethtool_op_get_ts_info(ndev, info);
1486 #if !defined(CONFIG_M5272)
1488 static void fec_enet_get_pauseparam(struct net_device *ndev,
1489 struct ethtool_pauseparam *pause)
1491 struct fec_enet_private *fep = netdev_priv(ndev);
1493 pause->autoneg = (fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) != 0;
1494 pause->tx_pause = (fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) != 0;
1495 pause->rx_pause = pause->tx_pause;
1498 static int fec_enet_set_pauseparam(struct net_device *ndev,
1499 struct ethtool_pauseparam *pause)
1501 struct fec_enet_private *fep = netdev_priv(ndev);
1503 if (pause->tx_pause != pause->rx_pause) {
1505 "hardware only support enable/disable both tx and rx");
1509 fep->pause_flag = 0;
1511 /* tx pause must be same as rx pause */
1512 fep->pause_flag |= pause->rx_pause ? FEC_PAUSE_FLAG_ENABLE : 0;
1513 fep->pause_flag |= pause->autoneg ? FEC_PAUSE_FLAG_AUTONEG : 0;
1515 if (pause->rx_pause || pause->autoneg) {
1516 fep->phy_dev->supported |= ADVERTISED_Pause;
1517 fep->phy_dev->advertising |= ADVERTISED_Pause;
1519 fep->phy_dev->supported &= ~ADVERTISED_Pause;
1520 fep->phy_dev->advertising &= ~ADVERTISED_Pause;
1523 if (pause->autoneg) {
1524 if (netif_running(ndev))
1526 phy_start_aneg(fep->phy_dev);
1528 if (netif_running(ndev))
1529 fec_restart(ndev, 0);
1534 static const struct fec_stat {
1535 char name[ETH_GSTRING_LEN];
1539 { "tx_dropped", RMON_T_DROP },
1540 { "tx_packets", RMON_T_PACKETS },
1541 { "tx_broadcast", RMON_T_BC_PKT },
1542 { "tx_multicast", RMON_T_MC_PKT },
1543 { "tx_crc_errors", RMON_T_CRC_ALIGN },
1544 { "tx_undersize", RMON_T_UNDERSIZE },
1545 { "tx_oversize", RMON_T_OVERSIZE },
1546 { "tx_fragment", RMON_T_FRAG },
1547 { "tx_jabber", RMON_T_JAB },
1548 { "tx_collision", RMON_T_COL },
1549 { "tx_64byte", RMON_T_P64 },
1550 { "tx_65to127byte", RMON_T_P65TO127 },
1551 { "tx_128to255byte", RMON_T_P128TO255 },
1552 { "tx_256to511byte", RMON_T_P256TO511 },
1553 { "tx_512to1023byte", RMON_T_P512TO1023 },
1554 { "tx_1024to2047byte", RMON_T_P1024TO2047 },
1555 { "tx_GTE2048byte", RMON_T_P_GTE2048 },
1556 { "tx_octets", RMON_T_OCTETS },
1559 { "IEEE_tx_drop", IEEE_T_DROP },
1560 { "IEEE_tx_frame_ok", IEEE_T_FRAME_OK },
1561 { "IEEE_tx_1col", IEEE_T_1COL },
1562 { "IEEE_tx_mcol", IEEE_T_MCOL },
1563 { "IEEE_tx_def", IEEE_T_DEF },
1564 { "IEEE_tx_lcol", IEEE_T_LCOL },
1565 { "IEEE_tx_excol", IEEE_T_EXCOL },
1566 { "IEEE_tx_macerr", IEEE_T_MACERR },
1567 { "IEEE_tx_cserr", IEEE_T_CSERR },
1568 { "IEEE_tx_sqe", IEEE_T_SQE },
1569 { "IEEE_tx_fdxfc", IEEE_T_FDXFC },
1570 { "IEEE_tx_octets_ok", IEEE_T_OCTETS_OK },
1573 { "rx_packets", RMON_R_PACKETS },
1574 { "rx_broadcast", RMON_R_BC_PKT },
1575 { "rx_multicast", RMON_R_MC_PKT },
1576 { "rx_crc_errors", RMON_R_CRC_ALIGN },
1577 { "rx_undersize", RMON_R_UNDERSIZE },
1578 { "rx_oversize", RMON_R_OVERSIZE },
1579 { "rx_fragment", RMON_R_FRAG },
1580 { "rx_jabber", RMON_R_JAB },
1581 { "rx_64byte", RMON_R_P64 },
1582 { "rx_65to127byte", RMON_R_P65TO127 },
1583 { "rx_128to255byte", RMON_R_P128TO255 },
1584 { "rx_256to511byte", RMON_R_P256TO511 },
1585 { "rx_512to1023byte", RMON_R_P512TO1023 },
1586 { "rx_1024to2047byte", RMON_R_P1024TO2047 },
1587 { "rx_GTE2048byte", RMON_R_P_GTE2048 },
1588 { "rx_octets", RMON_R_OCTETS },
1591 { "IEEE_rx_drop", IEEE_R_DROP },
1592 { "IEEE_rx_frame_ok", IEEE_R_FRAME_OK },
1593 { "IEEE_rx_crc", IEEE_R_CRC },
1594 { "IEEE_rx_align", IEEE_R_ALIGN },
1595 { "IEEE_rx_macerr", IEEE_R_MACERR },
1596 { "IEEE_rx_fdxfc", IEEE_R_FDXFC },
1597 { "IEEE_rx_octets_ok", IEEE_R_OCTETS_OK },
1600 static void fec_enet_get_ethtool_stats(struct net_device *dev,
1601 struct ethtool_stats *stats, u64 *data)
1603 struct fec_enet_private *fep = netdev_priv(dev);
1606 for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
1607 data[i] = readl(fep->hwp + fec_stats[i].offset);
1610 static void fec_enet_get_strings(struct net_device *netdev,
1611 u32 stringset, u8 *data)
1614 switch (stringset) {
1616 for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
1617 memcpy(data + i * ETH_GSTRING_LEN,
1618 fec_stats[i].name, ETH_GSTRING_LEN);
1623 static int fec_enet_get_sset_count(struct net_device *dev, int sset)
1627 return ARRAY_SIZE(fec_stats);
1632 #endif /* !defined(CONFIG_M5272) */
1634 static int fec_enet_nway_reset(struct net_device *dev)
1636 struct fec_enet_private *fep = netdev_priv(dev);
1637 struct phy_device *phydev = fep->phy_dev;
1642 return genphy_restart_aneg(phydev);
1645 static const struct ethtool_ops fec_enet_ethtool_ops = {
1646 #if !defined(CONFIG_M5272)
1647 .get_pauseparam = fec_enet_get_pauseparam,
1648 .set_pauseparam = fec_enet_set_pauseparam,
1650 .get_settings = fec_enet_get_settings,
1651 .set_settings = fec_enet_set_settings,
1652 .get_drvinfo = fec_enet_get_drvinfo,
1653 .get_link = ethtool_op_get_link,
1654 .get_ts_info = fec_enet_get_ts_info,
1655 .nway_reset = fec_enet_nway_reset,
1656 #ifndef CONFIG_M5272
1657 .get_ethtool_stats = fec_enet_get_ethtool_stats,
1658 .get_strings = fec_enet_get_strings,
1659 .get_sset_count = fec_enet_get_sset_count,
1663 static int fec_enet_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
1665 struct fec_enet_private *fep = netdev_priv(ndev);
1666 struct phy_device *phydev = fep->phy_dev;
1668 if (!netif_running(ndev))
1674 if (cmd == SIOCSHWTSTAMP && fep->bufdesc_ex)
1675 return fec_ptp_ioctl(ndev, rq, cmd);
1677 return phy_mii_ioctl(phydev, rq, cmd);
1680 static void fec_enet_free_buffers(struct net_device *ndev)
1682 struct fec_enet_private *fep = netdev_priv(ndev);
1684 struct sk_buff *skb;
1685 struct bufdesc *bdp;
1687 bdp = fep->rx_bd_base;
1688 for (i = 0; i < fep->rx_ring_size; i++) {
1689 skb = fep->rx_skbuff[i];
1691 if (bdp->cbd_bufaddr)
1692 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
1693 FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
1696 bdp = fec_enet_get_nextdesc(bdp, fep);
1699 bdp = fep->tx_bd_base;
1700 for (i = 0; i < fep->tx_ring_size; i++)
1701 kfree(fep->tx_bounce[i]);
1704 static int fec_enet_alloc_buffers(struct net_device *ndev)
1706 struct fec_enet_private *fep = netdev_priv(ndev);
1708 struct sk_buff *skb;
1709 struct bufdesc *bdp;
1711 bdp = fep->rx_bd_base;
1712 for (i = 0; i < fep->rx_ring_size; i++) {
1713 skb = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
1715 fec_enet_free_buffers(ndev);
1718 fep->rx_skbuff[i] = skb;
1720 bdp->cbd_bufaddr = dma_map_single(&fep->pdev->dev, skb->data,
1721 FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
1722 bdp->cbd_sc = BD_ENET_RX_EMPTY;
1724 if (fep->bufdesc_ex) {
1725 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
1726 ebdp->cbd_esc = BD_ENET_RX_INT;
1729 bdp = fec_enet_get_nextdesc(bdp, fep);
1732 /* Set the last buffer to wrap. */
1733 bdp = fec_enet_get_prevdesc(bdp, fep);
1734 bdp->cbd_sc |= BD_SC_WRAP;
1736 bdp = fep->tx_bd_base;
1737 for (i = 0; i < fep->tx_ring_size; i++) {
1738 fep->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL);
1741 bdp->cbd_bufaddr = 0;
1743 if (fep->bufdesc_ex) {
1744 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
1745 ebdp->cbd_esc = BD_ENET_TX_INT;
1748 bdp = fec_enet_get_nextdesc(bdp, fep);
1751 /* Set the last buffer to wrap. */
1752 bdp = fec_enet_get_prevdesc(bdp, fep);
1753 bdp->cbd_sc |= BD_SC_WRAP;
1759 fec_enet_open(struct net_device *ndev)
1761 struct fec_enet_private *fep = netdev_priv(ndev);
1764 napi_enable(&fep->napi);
1766 /* I should reset the ring buffers here, but I don't yet know
1767 * a simple way to do that.
1770 ret = fec_enet_alloc_buffers(ndev);
1774 /* Probe and connect to PHY when open the interface */
1775 ret = fec_enet_mii_probe(ndev);
1777 fec_enet_free_buffers(ndev);
1780 phy_start(fep->phy_dev);
1781 netif_start_queue(ndev);
1787 fec_enet_close(struct net_device *ndev)
1789 struct fec_enet_private *fep = netdev_priv(ndev);
1791 /* Don't know what to do yet. */
1792 napi_disable(&fep->napi);
1794 netif_stop_queue(ndev);
1798 phy_stop(fep->phy_dev);
1799 phy_disconnect(fep->phy_dev);
1802 fec_enet_free_buffers(ndev);
1807 /* Set or clear the multicast filter for this adaptor.
1808 * Skeleton taken from sunlance driver.
1809 * The CPM Ethernet implementation allows Multicast as well as individual
1810 * MAC address filtering. Some of the drivers check to make sure it is
1811 * a group multicast address, and discard those that are not. I guess I
1812 * will do the same for now, but just remove the test if you want
1813 * individual filtering as well (do the upper net layers want or support
1814 * this kind of feature?).
1817 #define HASH_BITS 6 /* #bits in hash */
1818 #define CRC32_POLY 0xEDB88320
1820 static void set_multicast_list(struct net_device *ndev)
1822 struct fec_enet_private *fep = netdev_priv(ndev);
1823 struct netdev_hw_addr *ha;
1824 unsigned int i, bit, data, crc, tmp;
1827 if (ndev->flags & IFF_PROMISC) {
1828 tmp = readl(fep->hwp + FEC_R_CNTRL);
1830 writel(tmp, fep->hwp + FEC_R_CNTRL);
1834 tmp = readl(fep->hwp + FEC_R_CNTRL);
1836 writel(tmp, fep->hwp + FEC_R_CNTRL);
1838 if (ndev->flags & IFF_ALLMULTI) {
1839 /* Catch all multicast addresses, so set the
1842 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1843 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
1848 /* Clear filter and add the addresses in hash register
1850 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1851 writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
1853 netdev_for_each_mc_addr(ha, ndev) {
1854 /* calculate crc32 value of mac address */
1857 for (i = 0; i < ndev->addr_len; i++) {
1859 for (bit = 0; bit < 8; bit++, data >>= 1) {
1861 (((crc ^ data) & 1) ? CRC32_POLY : 0);
1865 /* only upper 6 bits (HASH_BITS) are used
1866 * which point to specific bit in he hash registers
1868 hash = (crc >> (32 - HASH_BITS)) & 0x3f;
1871 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1872 tmp |= 1 << (hash - 32);
1873 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1875 tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_LOW);
1877 writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
1882 /* Set a MAC change in hardware. */
1884 fec_set_mac_address(struct net_device *ndev, void *p)
1886 struct fec_enet_private *fep = netdev_priv(ndev);
1887 struct sockaddr *addr = p;
1889 if (!is_valid_ether_addr(addr->sa_data))
1890 return -EADDRNOTAVAIL;
1892 memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len);
1894 writel(ndev->dev_addr[3] | (ndev->dev_addr[2] << 8) |
1895 (ndev->dev_addr[1] << 16) | (ndev->dev_addr[0] << 24),
1896 fep->hwp + FEC_ADDR_LOW);
1897 writel((ndev->dev_addr[5] << 16) | (ndev->dev_addr[4] << 24),
1898 fep->hwp + FEC_ADDR_HIGH);
1902 #ifdef CONFIG_NET_POLL_CONTROLLER
1904 * fec_poll_controller - FEC Poll controller function
1905 * @dev: The FEC network adapter
1907 * Polled functionality used by netconsole and others in non interrupt mode
1910 static void fec_poll_controller(struct net_device *dev)
1913 struct fec_enet_private *fep = netdev_priv(dev);
1915 for (i = 0; i < FEC_IRQ_NUM; i++) {
1916 if (fep->irq[i] > 0) {
1917 disable_irq(fep->irq[i]);
1918 fec_enet_interrupt(fep->irq[i], dev);
1919 enable_irq(fep->irq[i]);
1925 static int fec_set_features(struct net_device *netdev,
1926 netdev_features_t features)
1928 struct fec_enet_private *fep = netdev_priv(netdev);
1929 netdev_features_t changed = features ^ netdev->features;
1931 netdev->features = features;
1933 /* Receive checksum has been changed */
1934 if (changed & NETIF_F_RXCSUM) {
1935 if (features & NETIF_F_RXCSUM)
1936 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
1938 fep->csum_flags &= ~FLAG_RX_CSUM_ENABLED;
1940 if (netif_running(netdev)) {
1942 fec_restart(netdev, fep->phy_dev->duplex);
1943 netif_wake_queue(netdev);
1945 fec_restart(netdev, fep->phy_dev->duplex);
1952 static const struct net_device_ops fec_netdev_ops = {
1953 .ndo_open = fec_enet_open,
1954 .ndo_stop = fec_enet_close,
1955 .ndo_start_xmit = fec_enet_start_xmit,
1956 .ndo_set_rx_mode = set_multicast_list,
1957 .ndo_change_mtu = eth_change_mtu,
1958 .ndo_validate_addr = eth_validate_addr,
1959 .ndo_tx_timeout = fec_timeout,
1960 .ndo_set_mac_address = fec_set_mac_address,
1961 .ndo_do_ioctl = fec_enet_ioctl,
1962 #ifdef CONFIG_NET_POLL_CONTROLLER
1963 .ndo_poll_controller = fec_poll_controller,
1965 .ndo_set_features = fec_set_features,
1969 * XXX: We need to clean up on failure exits here.
1972 static int fec_enet_init(struct net_device *ndev)
1974 struct fec_enet_private *fep = netdev_priv(ndev);
1975 const struct platform_device_id *id_entry =
1976 platform_get_device_id(fep->pdev);
1977 struct bufdesc *cbd_base;
1979 /* Allocate memory for buffer descriptors. */
1980 cbd_base = dma_alloc_coherent(NULL, PAGE_SIZE, &fep->bd_dma,
1985 memset(cbd_base, 0, PAGE_SIZE);
1989 /* Get the Ethernet address */
1992 /* init the tx & rx ring size */
1993 fep->tx_ring_size = TX_RING_SIZE;
1994 fep->rx_ring_size = RX_RING_SIZE;
1996 /* Set receive and transmit descriptor base. */
1997 fep->rx_bd_base = cbd_base;
1998 if (fep->bufdesc_ex)
1999 fep->tx_bd_base = (struct bufdesc *)
2000 (((struct bufdesc_ex *)cbd_base) + fep->rx_ring_size);
2002 fep->tx_bd_base = cbd_base + fep->rx_ring_size;
2004 /* The FEC Ethernet specific entries in the device structure */
2005 ndev->watchdog_timeo = TX_TIMEOUT;
2006 ndev->netdev_ops = &fec_netdev_ops;
2007 ndev->ethtool_ops = &fec_enet_ethtool_ops;
2009 writel(FEC_RX_DISABLED_IMASK, fep->hwp + FEC_IMASK);
2010 netif_napi_add(ndev, &fep->napi, fec_enet_rx_napi, NAPI_POLL_WEIGHT);
2012 if (id_entry->driver_data & FEC_QUIRK_HAS_VLAN) {
2013 /* enable hw VLAN support */
2014 ndev->features |= NETIF_F_HW_VLAN_CTAG_RX;
2015 ndev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
2018 if (id_entry->driver_data & FEC_QUIRK_HAS_CSUM) {
2019 /* enable hw accelerator */
2020 ndev->features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
2022 ndev->hw_features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
2024 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
2027 fec_restart(ndev, 0);
2033 static void fec_reset_phy(struct platform_device *pdev)
2037 struct device_node *np = pdev->dev.of_node;
2042 of_property_read_u32(np, "phy-reset-duration", &msec);
2043 /* A sane reset duration should not be longer than 1s */
2047 phy_reset = of_get_named_gpio(np, "phy-reset-gpios", 0);
2048 if (!gpio_is_valid(phy_reset))
2051 err = devm_gpio_request_one(&pdev->dev, phy_reset,
2052 GPIOF_OUT_INIT_LOW, "phy-reset");
2054 dev_err(&pdev->dev, "failed to get phy-reset-gpios: %d\n", err);
2058 gpio_set_value(phy_reset, 1);
2060 #else /* CONFIG_OF */
2061 static void fec_reset_phy(struct platform_device *pdev)
2064 * In case of platform probe, the reset has been done
2068 #endif /* CONFIG_OF */
2071 fec_probe(struct platform_device *pdev)
2073 struct fec_enet_private *fep;
2074 struct fec_platform_data *pdata;
2075 struct net_device *ndev;
2076 int i, irq, ret = 0;
2078 const struct of_device_id *of_id;
2081 of_id = of_match_device(fec_dt_ids, &pdev->dev);
2083 pdev->id_entry = of_id->data;
2085 /* Init network device */
2086 ndev = alloc_etherdev(sizeof(struct fec_enet_private));
2090 SET_NETDEV_DEV(ndev, &pdev->dev);
2092 /* setup board info structure */
2093 fep = netdev_priv(ndev);
2095 #if !defined(CONFIG_M5272)
2096 /* default enable pause frame auto negotiation */
2097 if (pdev->id_entry &&
2098 (pdev->id_entry->driver_data & FEC_QUIRK_HAS_GBIT))
2099 fep->pause_flag |= FEC_PAUSE_FLAG_AUTONEG;
2102 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2103 fep->hwp = devm_ioremap_resource(&pdev->dev, r);
2104 if (IS_ERR(fep->hwp)) {
2105 ret = PTR_ERR(fep->hwp);
2106 goto failed_ioremap;
2110 fep->dev_id = dev_id++;
2112 fep->bufdesc_ex = 0;
2114 platform_set_drvdata(pdev, ndev);
2116 ret = of_get_phy_mode(pdev->dev.of_node);
2118 pdata = dev_get_platdata(&pdev->dev);
2120 fep->phy_interface = pdata->phy;
2122 fep->phy_interface = PHY_INTERFACE_MODE_MII;
2124 fep->phy_interface = ret;
2127 fep->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
2128 if (IS_ERR(fep->clk_ipg)) {
2129 ret = PTR_ERR(fep->clk_ipg);
2133 fep->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
2134 if (IS_ERR(fep->clk_ahb)) {
2135 ret = PTR_ERR(fep->clk_ahb);
2139 /* enet_out is optional, depends on board */
2140 fep->clk_enet_out = devm_clk_get(&pdev->dev, "enet_out");
2141 if (IS_ERR(fep->clk_enet_out))
2142 fep->clk_enet_out = NULL;
2144 fep->clk_ptp = devm_clk_get(&pdev->dev, "ptp");
2146 pdev->id_entry->driver_data & FEC_QUIRK_HAS_BUFDESC_EX;
2147 if (IS_ERR(fep->clk_ptp)) {
2148 fep->clk_ptp = NULL;
2149 fep->bufdesc_ex = 0;
2152 ret = clk_prepare_enable(fep->clk_ahb);
2156 ret = clk_prepare_enable(fep->clk_ipg);
2158 goto failed_clk_ipg;
2160 if (fep->clk_enet_out) {
2161 ret = clk_prepare_enable(fep->clk_enet_out);
2163 goto failed_clk_enet_out;
2167 ret = clk_prepare_enable(fep->clk_ptp);
2169 goto failed_clk_ptp;
2172 fep->reg_phy = devm_regulator_get(&pdev->dev, "phy");
2173 if (!IS_ERR(fep->reg_phy)) {
2174 ret = regulator_enable(fep->reg_phy);
2177 "Failed to enable phy regulator: %d\n", ret);
2178 goto failed_regulator;
2181 fep->reg_phy = NULL;
2184 fec_reset_phy(pdev);
2186 if (fep->bufdesc_ex)
2189 ret = fec_enet_init(ndev);
2193 for (i = 0; i < FEC_IRQ_NUM; i++) {
2194 irq = platform_get_irq(pdev, i);
2201 ret = devm_request_irq(&pdev->dev, irq, fec_enet_interrupt,
2202 0, pdev->name, ndev);
2207 ret = fec_enet_mii_init(pdev);
2209 goto failed_mii_init;
2211 /* Carrier starts down, phylib will bring it up */
2212 netif_carrier_off(ndev);
2214 ret = register_netdev(ndev);
2216 goto failed_register;
2218 if (fep->bufdesc_ex && fep->ptp_clock)
2219 netdev_info(ndev, "registered PHC device %d\n", fep->dev_id);
2221 INIT_DELAYED_WORK(&(fep->delay_work.delay_work), fec_enet_work);
2225 fec_enet_mii_remove(fep);
2230 regulator_disable(fep->reg_phy);
2233 clk_disable_unprepare(fep->clk_ptp);
2235 if (fep->clk_enet_out)
2236 clk_disable_unprepare(fep->clk_enet_out);
2237 failed_clk_enet_out:
2238 clk_disable_unprepare(fep->clk_ipg);
2240 clk_disable_unprepare(fep->clk_ahb);
2249 fec_drv_remove(struct platform_device *pdev)
2251 struct net_device *ndev = platform_get_drvdata(pdev);
2252 struct fec_enet_private *fep = netdev_priv(ndev);
2254 cancel_delayed_work_sync(&(fep->delay_work.delay_work));
2255 unregister_netdev(ndev);
2256 fec_enet_mii_remove(fep);
2257 del_timer_sync(&fep->time_keep);
2259 regulator_disable(fep->reg_phy);
2261 clk_disable_unprepare(fep->clk_ptp);
2263 ptp_clock_unregister(fep->ptp_clock);
2264 if (fep->clk_enet_out)
2265 clk_disable_unprepare(fep->clk_enet_out);
2266 clk_disable_unprepare(fep->clk_ipg);
2267 clk_disable_unprepare(fep->clk_ahb);
2273 #ifdef CONFIG_PM_SLEEP
2275 fec_suspend(struct device *dev)
2277 struct net_device *ndev = dev_get_drvdata(dev);
2278 struct fec_enet_private *fep = netdev_priv(ndev);
2280 if (netif_running(ndev)) {
2282 netif_device_detach(ndev);
2285 clk_disable_unprepare(fep->clk_ptp);
2286 if (fep->clk_enet_out)
2287 clk_disable_unprepare(fep->clk_enet_out);
2288 clk_disable_unprepare(fep->clk_ipg);
2289 clk_disable_unprepare(fep->clk_ahb);
2292 regulator_disable(fep->reg_phy);
2298 fec_resume(struct device *dev)
2300 struct net_device *ndev = dev_get_drvdata(dev);
2301 struct fec_enet_private *fep = netdev_priv(ndev);
2305 ret = regulator_enable(fep->reg_phy);
2310 ret = clk_prepare_enable(fep->clk_ahb);
2312 goto failed_clk_ahb;
2314 ret = clk_prepare_enable(fep->clk_ipg);
2316 goto failed_clk_ipg;
2318 if (fep->clk_enet_out) {
2319 ret = clk_prepare_enable(fep->clk_enet_out);
2321 goto failed_clk_enet_out;
2325 ret = clk_prepare_enable(fep->clk_ptp);
2327 goto failed_clk_ptp;
2330 if (netif_running(ndev)) {
2331 fec_restart(ndev, fep->full_duplex);
2332 netif_device_attach(ndev);
2338 if (fep->clk_enet_out)
2339 clk_disable_unprepare(fep->clk_enet_out);
2340 failed_clk_enet_out:
2341 clk_disable_unprepare(fep->clk_ipg);
2343 clk_disable_unprepare(fep->clk_ahb);
2346 regulator_disable(fep->reg_phy);
2349 #endif /* CONFIG_PM_SLEEP */
2351 static SIMPLE_DEV_PM_OPS(fec_pm_ops, fec_suspend, fec_resume);
2353 static struct platform_driver fec_driver = {
2355 .name = DRIVER_NAME,
2356 .owner = THIS_MODULE,
2358 .of_match_table = fec_dt_ids,
2360 .id_table = fec_devtype,
2362 .remove = fec_drv_remove,
2365 module_platform_driver(fec_driver);
2367 MODULE_ALIAS("platform:"DRIVER_NAME);
2368 MODULE_LICENSE("GPL");