Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[firefly-linux-kernel-4.4.55.git] / drivers / net / ethernet / freescale / fec_main.c
1 /*
2  * Fast Ethernet Controller (FEC) driver for Motorola MPC8xx.
3  * Copyright (c) 1997 Dan Malek (dmalek@jlc.net)
4  *
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
10  * small packets.
11  *
12  * Much better multiple PHY support by Magnus Damm.
13  * Copyright (c) 2000 Ericsson Radio Systems AB.
14  *
15  * Support for FEC controller of ColdFire processors.
16  * Copyright (c) 2001-2005 Greg Ungerer (gerg@snapgear.com)
17  *
18  * Bug fixes and cleanup by Philippe De Muyter (phdm@macqel.be)
19  * Copyright (c) 2004-2006 Macq Electronique SA.
20  *
21  * Copyright (C) 2010-2011 Freescale Semiconductor, Inc.
22  */
23
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>
37 #include <linux/spinlock.h>
38 #include <linux/workqueue.h>
39 #include <linux/bitops.h>
40 #include <linux/io.h>
41 #include <linux/irq.h>
42 #include <linux/clk.h>
43 #include <linux/platform_device.h>
44 #include <linux/phy.h>
45 #include <linux/fec.h>
46 #include <linux/of.h>
47 #include <linux/of_device.h>
48 #include <linux/of_gpio.h>
49 #include <linux/of_net.h>
50 #include <linux/pinctrl/consumer.h>
51 #include <linux/regulator/consumer.h>
52
53 #include <asm/cacheflush.h>
54
55 #ifndef CONFIG_ARM
56 #include <asm/coldfire.h>
57 #include <asm/mcfsim.h>
58 #endif
59
60 #include "fec.h"
61
62 #if defined(CONFIG_ARM)
63 #define FEC_ALIGNMENT   0xf
64 #else
65 #define FEC_ALIGNMENT   0x3
66 #endif
67
68 #define DRIVER_NAME     "fec"
69 #define FEC_NAPI_WEIGHT 64
70
71 /* Pause frame feild and FIFO threshold */
72 #define FEC_ENET_FCE    (1 << 5)
73 #define FEC_ENET_RSEM_V 0x84
74 #define FEC_ENET_RSFL_V 16
75 #define FEC_ENET_RAEM_V 0x8
76 #define FEC_ENET_RAFL_V 0x8
77 #define FEC_ENET_OPD_V  0xFFF0
78
79 /* Controller is ENET-MAC */
80 #define FEC_QUIRK_ENET_MAC              (1 << 0)
81 /* Controller needs driver to swap frame */
82 #define FEC_QUIRK_SWAP_FRAME            (1 << 1)
83 /* Controller uses gasket */
84 #define FEC_QUIRK_USE_GASKET            (1 << 2)
85 /* Controller has GBIT support */
86 #define FEC_QUIRK_HAS_GBIT              (1 << 3)
87 /* Controller has extend desc buffer */
88 #define FEC_QUIRK_HAS_BUFDESC_EX        (1 << 4)
89
90 static struct platform_device_id fec_devtype[] = {
91         {
92                 /* keep it for coldfire */
93                 .name = DRIVER_NAME,
94                 .driver_data = 0,
95         }, {
96                 .name = "imx25-fec",
97                 .driver_data = FEC_QUIRK_USE_GASKET,
98         }, {
99                 .name = "imx27-fec",
100                 .driver_data = 0,
101         }, {
102                 .name = "imx28-fec",
103                 .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_SWAP_FRAME,
104         }, {
105                 .name = "imx6q-fec",
106                 .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
107                                 FEC_QUIRK_HAS_BUFDESC_EX,
108         }, {
109                 /* sentinel */
110         }
111 };
112 MODULE_DEVICE_TABLE(platform, fec_devtype);
113
114 enum imx_fec_type {
115         IMX25_FEC = 1,  /* runs on i.mx25/50/53 */
116         IMX27_FEC,      /* runs on i.mx27/35/51 */
117         IMX28_FEC,
118         IMX6Q_FEC,
119 };
120
121 static const struct of_device_id fec_dt_ids[] = {
122         { .compatible = "fsl,imx25-fec", .data = &fec_devtype[IMX25_FEC], },
123         { .compatible = "fsl,imx27-fec", .data = &fec_devtype[IMX27_FEC], },
124         { .compatible = "fsl,imx28-fec", .data = &fec_devtype[IMX28_FEC], },
125         { .compatible = "fsl,imx6q-fec", .data = &fec_devtype[IMX6Q_FEC], },
126         { /* sentinel */ }
127 };
128 MODULE_DEVICE_TABLE(of, fec_dt_ids);
129
130 static unsigned char macaddr[ETH_ALEN];
131 module_param_array(macaddr, byte, NULL, 0);
132 MODULE_PARM_DESC(macaddr, "FEC Ethernet MAC address");
133
134 #if defined(CONFIG_M5272)
135 /*
136  * Some hardware gets it MAC address out of local flash memory.
137  * if this is non-zero then assume it is the address to get MAC from.
138  */
139 #if defined(CONFIG_NETtel)
140 #define FEC_FLASHMAC    0xf0006006
141 #elif defined(CONFIG_GILBARCONAP) || defined(CONFIG_SCALES)
142 #define FEC_FLASHMAC    0xf0006000
143 #elif defined(CONFIG_CANCam)
144 #define FEC_FLASHMAC    0xf0020000
145 #elif defined (CONFIG_M5272C3)
146 #define FEC_FLASHMAC    (0xffe04000 + 4)
147 #elif defined(CONFIG_MOD5272)
148 #define FEC_FLASHMAC    0xffc0406b
149 #else
150 #define FEC_FLASHMAC    0
151 #endif
152 #endif /* CONFIG_M5272 */
153
154 #if (((RX_RING_SIZE + TX_RING_SIZE) * 32) > PAGE_SIZE)
155 #error "FEC: descriptor ring size constants too large"
156 #endif
157
158 /* Interrupt events/masks. */
159 #define FEC_ENET_HBERR  ((uint)0x80000000)      /* Heartbeat error */
160 #define FEC_ENET_BABR   ((uint)0x40000000)      /* Babbling receiver */
161 #define FEC_ENET_BABT   ((uint)0x20000000)      /* Babbling transmitter */
162 #define FEC_ENET_GRA    ((uint)0x10000000)      /* Graceful stop complete */
163 #define FEC_ENET_TXF    ((uint)0x08000000)      /* Full frame transmitted */
164 #define FEC_ENET_TXB    ((uint)0x04000000)      /* A buffer was transmitted */
165 #define FEC_ENET_RXF    ((uint)0x02000000)      /* Full frame received */
166 #define FEC_ENET_RXB    ((uint)0x01000000)      /* A buffer was received */
167 #define FEC_ENET_MII    ((uint)0x00800000)      /* MII interrupt */
168 #define FEC_ENET_EBERR  ((uint)0x00400000)      /* SDMA bus error */
169
170 #define FEC_DEFAULT_IMASK (FEC_ENET_TXF | FEC_ENET_RXF | FEC_ENET_MII)
171 #define FEC_RX_DISABLED_IMASK (FEC_DEFAULT_IMASK & (~FEC_ENET_RXF))
172
173 /* The FEC stores dest/src/type, data, and checksum for receive packets.
174  */
175 #define PKT_MAXBUF_SIZE         1518
176 #define PKT_MINBUF_SIZE         64
177 #define PKT_MAXBLR_SIZE         1520
178
179 /*
180  * The 5270/5271/5280/5282/532x RX control register also contains maximum frame
181  * size bits. Other FEC hardware does not, so we need to take that into
182  * account when setting it.
183  */
184 #if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
185     defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM)
186 #define OPT_FRAME_SIZE  (PKT_MAXBUF_SIZE << 16)
187 #else
188 #define OPT_FRAME_SIZE  0
189 #endif
190
191 /* FEC MII MMFR bits definition */
192 #define FEC_MMFR_ST             (1 << 30)
193 #define FEC_MMFR_OP_READ        (2 << 28)
194 #define FEC_MMFR_OP_WRITE       (1 << 28)
195 #define FEC_MMFR_PA(v)          ((v & 0x1f) << 23)
196 #define FEC_MMFR_RA(v)          ((v & 0x1f) << 18)
197 #define FEC_MMFR_TA             (2 << 16)
198 #define FEC_MMFR_DATA(v)        (v & 0xffff)
199
200 #define FEC_MII_TIMEOUT         30000 /* us */
201
202 /* Transmitter timeout */
203 #define TX_TIMEOUT (2 * HZ)
204
205 #define FEC_PAUSE_FLAG_AUTONEG  0x1
206 #define FEC_PAUSE_FLAG_ENABLE   0x2
207
208 static int mii_cnt;
209
210 static struct bufdesc *fec_enet_get_nextdesc(struct bufdesc *bdp, int is_ex)
211 {
212         struct bufdesc_ex *ex = (struct bufdesc_ex *)bdp;
213         if (is_ex)
214                 return (struct bufdesc *)(ex + 1);
215         else
216                 return bdp + 1;
217 }
218
219 static struct bufdesc *fec_enet_get_prevdesc(struct bufdesc *bdp, int is_ex)
220 {
221         struct bufdesc_ex *ex = (struct bufdesc_ex *)bdp;
222         if (is_ex)
223                 return (struct bufdesc *)(ex - 1);
224         else
225                 return bdp - 1;
226 }
227
228 static void *swap_buffer(void *bufaddr, int len)
229 {
230         int i;
231         unsigned int *buf = bufaddr;
232
233         for (i = 0; i < (len + 3) / 4; i++, buf++)
234                 *buf = cpu_to_be32(*buf);
235
236         return bufaddr;
237 }
238
239 static netdev_tx_t
240 fec_enet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
241 {
242         struct fec_enet_private *fep = netdev_priv(ndev);
243         const struct platform_device_id *id_entry =
244                                 platform_get_device_id(fep->pdev);
245         struct bufdesc *bdp;
246         void *bufaddr;
247         unsigned short  status;
248         unsigned int index;
249
250         if (!fep->link) {
251                 /* Link is down or autonegotiation is in progress. */
252                 return NETDEV_TX_BUSY;
253         }
254
255         /* Fill in a Tx ring entry */
256         bdp = fep->cur_tx;
257
258         status = bdp->cbd_sc;
259
260         if (status & BD_ENET_TX_READY) {
261                 /* Ooops.  All transmit buffers are full.  Bail out.
262                  * This should not happen, since ndev->tbusy should be set.
263                  */
264                 printk("%s: tx queue full!.\n", ndev->name);
265                 return NETDEV_TX_BUSY;
266         }
267
268         /* Clear all of the status flags */
269         status &= ~BD_ENET_TX_STATS;
270
271         /* Set buffer length and buffer pointer */
272         bufaddr = skb->data;
273         bdp->cbd_datlen = skb->len;
274
275         /*
276          * On some FEC implementations data must be aligned on
277          * 4-byte boundaries. Use bounce buffers to copy data
278          * and get it aligned. Ugh.
279          */
280         if (fep->bufdesc_ex)
281                 index = (struct bufdesc_ex *)bdp -
282                         (struct bufdesc_ex *)fep->tx_bd_base;
283         else
284                 index = bdp - fep->tx_bd_base;
285
286         if (((unsigned long) bufaddr) & FEC_ALIGNMENT) {
287                 memcpy(fep->tx_bounce[index], skb->data, skb->len);
288                 bufaddr = fep->tx_bounce[index];
289         }
290
291         /*
292          * Some design made an incorrect assumption on endian mode of
293          * the system that it's running on. As the result, driver has to
294          * swap every frame going to and coming from the controller.
295          */
296         if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
297                 swap_buffer(bufaddr, skb->len);
298
299         /* Save skb pointer */
300         fep->tx_skbuff[index] = skb;
301
302         /* Push the data cache so the CPM does not get stale memory
303          * data.
304          */
305         bdp->cbd_bufaddr = dma_map_single(&fep->pdev->dev, bufaddr,
306                         FEC_ENET_TX_FRSIZE, DMA_TO_DEVICE);
307
308         /* Send it on its way.  Tell FEC it's ready, interrupt when done,
309          * it's the last BD of the frame, and to put the CRC on the end.
310          */
311         status |= (BD_ENET_TX_READY | BD_ENET_TX_INTR
312                         | BD_ENET_TX_LAST | BD_ENET_TX_TC);
313         bdp->cbd_sc = status;
314
315         if (fep->bufdesc_ex) {
316
317                 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
318                 ebdp->cbd_bdu = 0;
319                 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
320                         fep->hwts_tx_en)) {
321                         ebdp->cbd_esc = (BD_ENET_TX_TS | BD_ENET_TX_INT);
322                         skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
323                 } else {
324
325                         ebdp->cbd_esc = BD_ENET_TX_INT;
326                 }
327         }
328         /* If this was the last BD in the ring, start at the beginning again. */
329         if (status & BD_ENET_TX_WRAP)
330                 bdp = fep->tx_bd_base;
331         else
332                 bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
333
334         fep->cur_tx = bdp;
335
336         if (fep->cur_tx == fep->dirty_tx)
337                 netif_stop_queue(ndev);
338
339         /* Trigger transmission start */
340         writel(0, fep->hwp + FEC_X_DES_ACTIVE);
341
342         skb_tx_timestamp(skb);
343
344         return NETDEV_TX_OK;
345 }
346
347 /* Init RX & TX buffer descriptors
348  */
349 static void fec_enet_bd_init(struct net_device *dev)
350 {
351         struct fec_enet_private *fep = netdev_priv(dev);
352         struct bufdesc *bdp;
353         unsigned int i;
354
355         /* Initialize the receive buffer descriptors. */
356         bdp = fep->rx_bd_base;
357         for (i = 0; i < RX_RING_SIZE; i++) {
358
359                 /* Initialize the BD for every fragment in the page. */
360                 if (bdp->cbd_bufaddr)
361                         bdp->cbd_sc = BD_ENET_RX_EMPTY;
362                 else
363                         bdp->cbd_sc = 0;
364                 bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
365         }
366
367         /* Set the last buffer to wrap */
368         bdp = fec_enet_get_prevdesc(bdp, fep->bufdesc_ex);
369         bdp->cbd_sc |= BD_SC_WRAP;
370
371         fep->cur_rx = fep->rx_bd_base;
372
373         /* ...and the same for transmit */
374         bdp = fep->tx_bd_base;
375         fep->cur_tx = bdp;
376         for (i = 0; i < TX_RING_SIZE; i++) {
377
378                 /* Initialize the BD for every fragment in the page. */
379                 bdp->cbd_sc = 0;
380                 if (bdp->cbd_bufaddr && fep->tx_skbuff[i]) {
381                         dev_kfree_skb_any(fep->tx_skbuff[i]);
382                         fep->tx_skbuff[i] = NULL;
383                 }
384                 bdp->cbd_bufaddr = 0;
385                 bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
386         }
387
388         /* Set the last buffer to wrap */
389         bdp = fec_enet_get_prevdesc(bdp, fep->bufdesc_ex);
390         bdp->cbd_sc |= BD_SC_WRAP;
391         fep->dirty_tx = bdp;
392 }
393
394 /* This function is called to start or restart the FEC during a link
395  * change.  This only happens when switching between half and full
396  * duplex.
397  */
398 static void
399 fec_restart(struct net_device *ndev, int duplex)
400 {
401         struct fec_enet_private *fep = netdev_priv(ndev);
402         const struct platform_device_id *id_entry =
403                                 platform_get_device_id(fep->pdev);
404         int i;
405         u32 temp_mac[2];
406         u32 rcntl = OPT_FRAME_SIZE | 0x04;
407         u32 ecntl = 0x2; /* ETHEREN */
408
409         /* Whack a reset.  We should wait for this. */
410         writel(1, fep->hwp + FEC_ECNTRL);
411         udelay(10);
412
413         /*
414          * enet-mac reset will reset mac address registers too,
415          * so need to reconfigure it.
416          */
417         if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
418                 memcpy(&temp_mac, ndev->dev_addr, ETH_ALEN);
419                 writel(cpu_to_be32(temp_mac[0]), fep->hwp + FEC_ADDR_LOW);
420                 writel(cpu_to_be32(temp_mac[1]), fep->hwp + FEC_ADDR_HIGH);
421         }
422
423         /* Clear any outstanding interrupt. */
424         writel(0xffc00000, fep->hwp + FEC_IEVENT);
425
426         /* Reset all multicast. */
427         writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
428         writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
429 #ifndef CONFIG_M5272
430         writel(0, fep->hwp + FEC_HASH_TABLE_HIGH);
431         writel(0, fep->hwp + FEC_HASH_TABLE_LOW);
432 #endif
433
434         /* Set maximum receive buffer size. */
435         writel(PKT_MAXBLR_SIZE, fep->hwp + FEC_R_BUFF_SIZE);
436
437         fec_enet_bd_init(ndev);
438
439         /* Set receive and transmit descriptor base. */
440         writel(fep->bd_dma, fep->hwp + FEC_R_DES_START);
441         if (fep->bufdesc_ex)
442                 writel((unsigned long)fep->bd_dma + sizeof(struct bufdesc_ex)
443                         * RX_RING_SIZE, fep->hwp + FEC_X_DES_START);
444         else
445                 writel((unsigned long)fep->bd_dma + sizeof(struct bufdesc)
446                         * RX_RING_SIZE, fep->hwp + FEC_X_DES_START);
447
448
449         for (i = 0; i <= TX_RING_MOD_MASK; i++) {
450                 if (fep->tx_skbuff[i]) {
451                         dev_kfree_skb_any(fep->tx_skbuff[i]);
452                         fep->tx_skbuff[i] = NULL;
453                 }
454         }
455
456         /* Enable MII mode */
457         if (duplex) {
458                 /* FD enable */
459                 writel(0x04, fep->hwp + FEC_X_CNTRL);
460         } else {
461                 /* No Rcv on Xmit */
462                 rcntl |= 0x02;
463                 writel(0x0, fep->hwp + FEC_X_CNTRL);
464         }
465
466         fep->full_duplex = duplex;
467
468         /* Set MII speed */
469         writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
470
471         /*
472          * The phy interface and speed need to get configured
473          * differently on enet-mac.
474          */
475         if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
476                 /* Enable flow control and length check */
477                 rcntl |= 0x40000000 | 0x00000020;
478
479                 /* RGMII, RMII or MII */
480                 if (fep->phy_interface == PHY_INTERFACE_MODE_RGMII)
481                         rcntl |= (1 << 6);
482                 else if (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
483                         rcntl |= (1 << 8);
484                 else
485                         rcntl &= ~(1 << 8);
486
487                 /* 1G, 100M or 10M */
488                 if (fep->phy_dev) {
489                         if (fep->phy_dev->speed == SPEED_1000)
490                                 ecntl |= (1 << 5);
491                         else if (fep->phy_dev->speed == SPEED_100)
492                                 rcntl &= ~(1 << 9);
493                         else
494                                 rcntl |= (1 << 9);
495                 }
496         } else {
497 #ifdef FEC_MIIGSK_ENR
498                 if (id_entry->driver_data & FEC_QUIRK_USE_GASKET) {
499                         u32 cfgr;
500                         /* disable the gasket and wait */
501                         writel(0, fep->hwp + FEC_MIIGSK_ENR);
502                         while (readl(fep->hwp + FEC_MIIGSK_ENR) & 4)
503                                 udelay(1);
504
505                         /*
506                          * configure the gasket:
507                          *   RMII, 50 MHz, no loopback, no echo
508                          *   MII, 25 MHz, no loopback, no echo
509                          */
510                         cfgr = (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
511                                 ? BM_MIIGSK_CFGR_RMII : BM_MIIGSK_CFGR_MII;
512                         if (fep->phy_dev && fep->phy_dev->speed == SPEED_10)
513                                 cfgr |= BM_MIIGSK_CFGR_FRCONT_10M;
514                         writel(cfgr, fep->hwp + FEC_MIIGSK_CFGR);
515
516                         /* re-enable the gasket */
517                         writel(2, fep->hwp + FEC_MIIGSK_ENR);
518                 }
519 #endif
520         }
521
522         /* enable pause frame*/
523         if ((fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) ||
524             ((fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) &&
525              fep->phy_dev && fep->phy_dev->pause)) {
526                 rcntl |= FEC_ENET_FCE;
527
528                 /* set FIFO thresh hold parameter to reduce overrun */
529                 writel(FEC_ENET_RSEM_V, fep->hwp + FEC_R_FIFO_RSEM);
530                 writel(FEC_ENET_RSFL_V, fep->hwp + FEC_R_FIFO_RSFL);
531                 writel(FEC_ENET_RAEM_V, fep->hwp + FEC_R_FIFO_RAEM);
532                 writel(FEC_ENET_RAFL_V, fep->hwp + FEC_R_FIFO_RAFL);
533
534                 /* OPD */
535                 writel(FEC_ENET_OPD_V, fep->hwp + FEC_OPD);
536         } else {
537                 rcntl &= ~FEC_ENET_FCE;
538         }
539
540         writel(rcntl, fep->hwp + FEC_R_CNTRL);
541
542         if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
543                 /* enable ENET endian swap */
544                 ecntl |= (1 << 8);
545                 /* enable ENET store and forward mode */
546                 writel(1 << 8, fep->hwp + FEC_X_WMRK);
547         }
548
549         if (fep->bufdesc_ex)
550                 ecntl |= (1 << 4);
551
552         /* And last, enable the transmit and receive processing */
553         writel(ecntl, fep->hwp + FEC_ECNTRL);
554         writel(0, fep->hwp + FEC_R_DES_ACTIVE);
555
556         if (fep->bufdesc_ex)
557                 fec_ptp_start_cyclecounter(ndev);
558
559         /* Enable interrupts we wish to service */
560         writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
561 }
562
563 static void
564 fec_stop(struct net_device *ndev)
565 {
566         struct fec_enet_private *fep = netdev_priv(ndev);
567         const struct platform_device_id *id_entry =
568                                 platform_get_device_id(fep->pdev);
569         u32 rmii_mode = readl(fep->hwp + FEC_R_CNTRL) & (1 << 8);
570
571         /* We cannot expect a graceful transmit stop without link !!! */
572         if (fep->link) {
573                 writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */
574                 udelay(10);
575                 if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA))
576                         printk("fec_stop : Graceful transmit stop did not complete !\n");
577         }
578
579         /* Whack a reset.  We should wait for this. */
580         writel(1, fep->hwp + FEC_ECNTRL);
581         udelay(10);
582         writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
583         writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
584
585         /* We have to keep ENET enabled to have MII interrupt stay working */
586         if (id_entry->driver_data & FEC_QUIRK_ENET_MAC) {
587                 writel(2, fep->hwp + FEC_ECNTRL);
588                 writel(rmii_mode, fep->hwp + FEC_R_CNTRL);
589         }
590 }
591
592
593 static void
594 fec_timeout(struct net_device *ndev)
595 {
596         struct fec_enet_private *fep = netdev_priv(ndev);
597
598         ndev->stats.tx_errors++;
599
600         fec_restart(ndev, fep->full_duplex);
601         netif_wake_queue(ndev);
602 }
603
604 static void
605 fec_enet_tx(struct net_device *ndev)
606 {
607         struct  fec_enet_private *fep;
608         struct bufdesc *bdp;
609         unsigned short status;
610         struct  sk_buff *skb;
611         int     index = 0;
612
613         fep = netdev_priv(ndev);
614         bdp = fep->dirty_tx;
615
616         /* get next bdp of dirty_tx */
617         if (bdp->cbd_sc & BD_ENET_TX_WRAP)
618                 bdp = fep->tx_bd_base;
619         else
620                 bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
621
622         while (((status = bdp->cbd_sc) & BD_ENET_TX_READY) == 0) {
623
624                 /* current queue is empty */
625                 if (bdp == fep->cur_tx)
626                         break;
627
628                 if (fep->bufdesc_ex)
629                         index = (struct bufdesc_ex *)bdp -
630                                 (struct bufdesc_ex *)fep->tx_bd_base;
631                 else
632                         index = bdp - fep->tx_bd_base;
633
634                 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
635                                 FEC_ENET_TX_FRSIZE, DMA_TO_DEVICE);
636                 bdp->cbd_bufaddr = 0;
637
638                 skb = fep->tx_skbuff[index];
639
640                 /* Check for errors. */
641                 if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC |
642                                    BD_ENET_TX_RL | BD_ENET_TX_UN |
643                                    BD_ENET_TX_CSL)) {
644                         ndev->stats.tx_errors++;
645                         if (status & BD_ENET_TX_HB)  /* No heartbeat */
646                                 ndev->stats.tx_heartbeat_errors++;
647                         if (status & BD_ENET_TX_LC)  /* Late collision */
648                                 ndev->stats.tx_window_errors++;
649                         if (status & BD_ENET_TX_RL)  /* Retrans limit */
650                                 ndev->stats.tx_aborted_errors++;
651                         if (status & BD_ENET_TX_UN)  /* Underrun */
652                                 ndev->stats.tx_fifo_errors++;
653                         if (status & BD_ENET_TX_CSL) /* Carrier lost */
654                                 ndev->stats.tx_carrier_errors++;
655                 } else {
656                         ndev->stats.tx_packets++;
657                 }
658
659                 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) &&
660                         fep->bufdesc_ex) {
661                         struct skb_shared_hwtstamps shhwtstamps;
662                         unsigned long flags;
663                         struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
664
665                         memset(&shhwtstamps, 0, sizeof(shhwtstamps));
666                         spin_lock_irqsave(&fep->tmreg_lock, flags);
667                         shhwtstamps.hwtstamp = ns_to_ktime(
668                                 timecounter_cyc2time(&fep->tc, ebdp->ts));
669                         spin_unlock_irqrestore(&fep->tmreg_lock, flags);
670                         skb_tstamp_tx(skb, &shhwtstamps);
671                 }
672
673                 if (status & BD_ENET_TX_READY)
674                         printk("HEY! Enet xmit interrupt and TX_READY.\n");
675
676                 /* Deferred means some collisions occurred during transmit,
677                  * but we eventually sent the packet OK.
678                  */
679                 if (status & BD_ENET_TX_DEF)
680                         ndev->stats.collisions++;
681
682                 /* Free the sk buffer associated with this last transmit */
683                 dev_kfree_skb_any(skb);
684                 fep->tx_skbuff[index] = NULL;
685
686                 fep->dirty_tx = bdp;
687
688                 /* Update pointer to next buffer descriptor to be transmitted */
689                 if (status & BD_ENET_TX_WRAP)
690                         bdp = fep->tx_bd_base;
691                 else
692                         bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
693
694                 /* Since we have freed up a buffer, the ring is no longer full
695                  */
696                 if (fep->dirty_tx != fep->cur_tx) {
697                         if (netif_queue_stopped(ndev))
698                                 netif_wake_queue(ndev);
699                 }
700         }
701         return;
702 }
703
704
705 /* During a receive, the cur_rx points to the current incoming buffer.
706  * When we update through the ring, if the next incoming buffer has
707  * not been given to the system, we just set the empty indicator,
708  * effectively tossing the packet.
709  */
710 static int
711 fec_enet_rx(struct net_device *ndev, int budget)
712 {
713         struct fec_enet_private *fep = netdev_priv(ndev);
714         const struct platform_device_id *id_entry =
715                                 platform_get_device_id(fep->pdev);
716         struct bufdesc *bdp;
717         unsigned short status;
718         struct  sk_buff *skb;
719         ushort  pkt_len;
720         __u8 *data;
721         int     pkt_received = 0;
722
723 #ifdef CONFIG_M532x
724         flush_cache_all();
725 #endif
726
727         /* First, grab all of the stats for the incoming packet.
728          * These get messed up if we get called due to a busy condition.
729          */
730         bdp = fep->cur_rx;
731
732         while (!((status = bdp->cbd_sc) & BD_ENET_RX_EMPTY)) {
733
734                 if (pkt_received >= budget)
735                         break;
736                 pkt_received++;
737
738                 /* Since we have allocated space to hold a complete frame,
739                  * the last indicator should be set.
740                  */
741                 if ((status & BD_ENET_RX_LAST) == 0)
742                         printk("FEC ENET: rcv is not +last\n");
743
744                 if (!fep->opened)
745                         goto rx_processing_done;
746
747                 /* Check for errors. */
748                 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
749                            BD_ENET_RX_CR | BD_ENET_RX_OV)) {
750                         ndev->stats.rx_errors++;
751                         if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH)) {
752                                 /* Frame too long or too short. */
753                                 ndev->stats.rx_length_errors++;
754                         }
755                         if (status & BD_ENET_RX_NO)     /* Frame alignment */
756                                 ndev->stats.rx_frame_errors++;
757                         if (status & BD_ENET_RX_CR)     /* CRC Error */
758                                 ndev->stats.rx_crc_errors++;
759                         if (status & BD_ENET_RX_OV)     /* FIFO overrun */
760                                 ndev->stats.rx_fifo_errors++;
761                 }
762
763                 /* Report late collisions as a frame error.
764                  * On this error, the BD is closed, but we don't know what we
765                  * have in the buffer.  So, just drop this frame on the floor.
766                  */
767                 if (status & BD_ENET_RX_CL) {
768                         ndev->stats.rx_errors++;
769                         ndev->stats.rx_frame_errors++;
770                         goto rx_processing_done;
771                 }
772
773                 /* Process the incoming frame. */
774                 ndev->stats.rx_packets++;
775                 pkt_len = bdp->cbd_datlen;
776                 ndev->stats.rx_bytes += pkt_len;
777                 data = (__u8*)__va(bdp->cbd_bufaddr);
778
779                 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
780                                 FEC_ENET_TX_FRSIZE, DMA_FROM_DEVICE);
781
782                 if (id_entry->driver_data & FEC_QUIRK_SWAP_FRAME)
783                         swap_buffer(data, pkt_len);
784
785                 /* This does 16 byte alignment, exactly what we need.
786                  * The packet length includes FCS, but we don't want to
787                  * include that when passing upstream as it messes up
788                  * bridging applications.
789                  */
790                 skb = netdev_alloc_skb(ndev, pkt_len - 4 + NET_IP_ALIGN);
791
792                 if (unlikely(!skb)) {
793                         ndev->stats.rx_dropped++;
794                 } else {
795                         skb_reserve(skb, NET_IP_ALIGN);
796                         skb_put(skb, pkt_len - 4);      /* Make room */
797                         skb_copy_to_linear_data(skb, data, pkt_len - 4);
798                         skb->protocol = eth_type_trans(skb, ndev);
799
800                         /* Get receive timestamp from the skb */
801                         if (fep->hwts_rx_en && fep->bufdesc_ex) {
802                                 struct skb_shared_hwtstamps *shhwtstamps =
803                                                             skb_hwtstamps(skb);
804                                 unsigned long flags;
805                                 struct bufdesc_ex *ebdp =
806                                         (struct bufdesc_ex *)bdp;
807
808                                 memset(shhwtstamps, 0, sizeof(*shhwtstamps));
809
810                                 spin_lock_irqsave(&fep->tmreg_lock, flags);
811                                 shhwtstamps->hwtstamp = ns_to_ktime(
812                                     timecounter_cyc2time(&fep->tc, ebdp->ts));
813                                 spin_unlock_irqrestore(&fep->tmreg_lock, flags);
814                         }
815
816                         if (!skb_defer_rx_timestamp(skb))
817                                 napi_gro_receive(&fep->napi, skb);
818                 }
819
820                 bdp->cbd_bufaddr = dma_map_single(&fep->pdev->dev, data,
821                                 FEC_ENET_TX_FRSIZE, DMA_FROM_DEVICE);
822 rx_processing_done:
823                 /* Clear the status flags for this buffer */
824                 status &= ~BD_ENET_RX_STATS;
825
826                 /* Mark the buffer empty */
827                 status |= BD_ENET_RX_EMPTY;
828                 bdp->cbd_sc = status;
829
830                 if (fep->bufdesc_ex) {
831                         struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
832
833                         ebdp->cbd_esc = BD_ENET_RX_INT;
834                         ebdp->cbd_prot = 0;
835                         ebdp->cbd_bdu = 0;
836                 }
837
838                 /* Update BD pointer to next entry */
839                 if (status & BD_ENET_RX_WRAP)
840                         bdp = fep->rx_bd_base;
841                 else
842                         bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
843                 /* Doing this here will keep the FEC running while we process
844                  * incoming frames.  On a heavily loaded network, we should be
845                  * able to keep up at the expense of system resources.
846                  */
847                 writel(0, fep->hwp + FEC_R_DES_ACTIVE);
848         }
849         fep->cur_rx = bdp;
850
851         return pkt_received;
852 }
853
854 static irqreturn_t
855 fec_enet_interrupt(int irq, void *dev_id)
856 {
857         struct net_device *ndev = dev_id;
858         struct fec_enet_private *fep = netdev_priv(ndev);
859         uint int_events;
860         irqreturn_t ret = IRQ_NONE;
861
862         do {
863                 int_events = readl(fep->hwp + FEC_IEVENT);
864                 writel(int_events, fep->hwp + FEC_IEVENT);
865
866                 if (int_events & (FEC_ENET_RXF | FEC_ENET_TXF)) {
867                         ret = IRQ_HANDLED;
868
869                         /* Disable the RX interrupt */
870                         if (napi_schedule_prep(&fep->napi)) {
871                                 writel(FEC_RX_DISABLED_IMASK,
872                                         fep->hwp + FEC_IMASK);
873                                 __napi_schedule(&fep->napi);
874                         }
875                 }
876
877                 if (int_events & FEC_ENET_MII) {
878                         ret = IRQ_HANDLED;
879                         complete(&fep->mdio_done);
880                 }
881         } while (int_events);
882
883         return ret;
884 }
885
886 static int fec_enet_rx_napi(struct napi_struct *napi, int budget)
887 {
888         struct net_device *ndev = napi->dev;
889         int pkts = fec_enet_rx(ndev, budget);
890         struct fec_enet_private *fep = netdev_priv(ndev);
891
892         fec_enet_tx(ndev);
893
894         if (pkts < budget) {
895                 napi_complete(napi);
896                 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
897         }
898         return pkts;
899 }
900
901 /* ------------------------------------------------------------------------- */
902 static void fec_get_mac(struct net_device *ndev)
903 {
904         struct fec_enet_private *fep = netdev_priv(ndev);
905         struct fec_platform_data *pdata = fep->pdev->dev.platform_data;
906         unsigned char *iap, tmpaddr[ETH_ALEN];
907
908         /*
909          * try to get mac address in following order:
910          *
911          * 1) module parameter via kernel command line in form
912          *    fec.macaddr=0x00,0x04,0x9f,0x01,0x30,0xe0
913          */
914         iap = macaddr;
915
916 #ifdef CONFIG_OF
917         /*
918          * 2) from device tree data
919          */
920         if (!is_valid_ether_addr(iap)) {
921                 struct device_node *np = fep->pdev->dev.of_node;
922                 if (np) {
923                         const char *mac = of_get_mac_address(np);
924                         if (mac)
925                                 iap = (unsigned char *) mac;
926                 }
927         }
928 #endif
929
930         /*
931          * 3) from flash or fuse (via platform data)
932          */
933         if (!is_valid_ether_addr(iap)) {
934 #ifdef CONFIG_M5272
935                 if (FEC_FLASHMAC)
936                         iap = (unsigned char *)FEC_FLASHMAC;
937 #else
938                 if (pdata)
939                         iap = (unsigned char *)&pdata->mac;
940 #endif
941         }
942
943         /*
944          * 4) FEC mac registers set by bootloader
945          */
946         if (!is_valid_ether_addr(iap)) {
947                 *((unsigned long *) &tmpaddr[0]) =
948                         be32_to_cpu(readl(fep->hwp + FEC_ADDR_LOW));
949                 *((unsigned short *) &tmpaddr[4]) =
950                         be16_to_cpu(readl(fep->hwp + FEC_ADDR_HIGH) >> 16);
951                 iap = &tmpaddr[0];
952         }
953
954         memcpy(ndev->dev_addr, iap, ETH_ALEN);
955
956         /* Adjust MAC if using macaddr */
957         if (iap == macaddr)
958                  ndev->dev_addr[ETH_ALEN-1] = macaddr[ETH_ALEN-1] + fep->dev_id;
959 }
960
961 /* ------------------------------------------------------------------------- */
962
963 /*
964  * Phy section
965  */
966 static void fec_enet_adjust_link(struct net_device *ndev)
967 {
968         struct fec_enet_private *fep = netdev_priv(ndev);
969         struct phy_device *phy_dev = fep->phy_dev;
970         unsigned long flags;
971
972         int status_change = 0;
973
974         spin_lock_irqsave(&fep->hw_lock, flags);
975
976         /* Prevent a state halted on mii error */
977         if (fep->mii_timeout && phy_dev->state == PHY_HALTED) {
978                 phy_dev->state = PHY_RESUMING;
979                 goto spin_unlock;
980         }
981
982         if (phy_dev->link) {
983                 if (!fep->link) {
984                         fep->link = phy_dev->link;
985                         status_change = 1;
986                 }
987
988                 if (fep->full_duplex != phy_dev->duplex)
989                         status_change = 1;
990
991                 if (phy_dev->speed != fep->speed) {
992                         fep->speed = phy_dev->speed;
993                         status_change = 1;
994                 }
995
996                 /* if any of the above changed restart the FEC */
997                 if (status_change)
998                         fec_restart(ndev, phy_dev->duplex);
999         } else {
1000                 if (fep->link) {
1001                         fec_stop(ndev);
1002                         status_change = 1;
1003                 }
1004         }
1005
1006 spin_unlock:
1007         spin_unlock_irqrestore(&fep->hw_lock, flags);
1008
1009         if (status_change)
1010                 phy_print_status(phy_dev);
1011 }
1012
1013 static int fec_enet_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
1014 {
1015         struct fec_enet_private *fep = bus->priv;
1016         unsigned long time_left;
1017
1018         fep->mii_timeout = 0;
1019         init_completion(&fep->mdio_done);
1020
1021         /* start a read op */
1022         writel(FEC_MMFR_ST | FEC_MMFR_OP_READ |
1023                 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1024                 FEC_MMFR_TA, fep->hwp + FEC_MII_DATA);
1025
1026         /* wait for end of transfer */
1027         time_left = wait_for_completion_timeout(&fep->mdio_done,
1028                         usecs_to_jiffies(FEC_MII_TIMEOUT));
1029         if (time_left == 0) {
1030                 fep->mii_timeout = 1;
1031                 printk(KERN_ERR "FEC: MDIO read timeout\n");
1032                 return -ETIMEDOUT;
1033         }
1034
1035         /* return value */
1036         return FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA));
1037 }
1038
1039 static int fec_enet_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
1040                            u16 value)
1041 {
1042         struct fec_enet_private *fep = bus->priv;
1043         unsigned long time_left;
1044
1045         fep->mii_timeout = 0;
1046         init_completion(&fep->mdio_done);
1047
1048         /* start a write op */
1049         writel(FEC_MMFR_ST | FEC_MMFR_OP_WRITE |
1050                 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1051                 FEC_MMFR_TA | FEC_MMFR_DATA(value),
1052                 fep->hwp + FEC_MII_DATA);
1053
1054         /* wait for end of transfer */
1055         time_left = wait_for_completion_timeout(&fep->mdio_done,
1056                         usecs_to_jiffies(FEC_MII_TIMEOUT));
1057         if (time_left == 0) {
1058                 fep->mii_timeout = 1;
1059                 printk(KERN_ERR "FEC: MDIO write timeout\n");
1060                 return -ETIMEDOUT;
1061         }
1062
1063         return 0;
1064 }
1065
1066 static int fec_enet_mdio_reset(struct mii_bus *bus)
1067 {
1068         return 0;
1069 }
1070
1071 static int fec_enet_mii_probe(struct net_device *ndev)
1072 {
1073         struct fec_enet_private *fep = netdev_priv(ndev);
1074         const struct platform_device_id *id_entry =
1075                                 platform_get_device_id(fep->pdev);
1076         struct phy_device *phy_dev = NULL;
1077         char mdio_bus_id[MII_BUS_ID_SIZE];
1078         char phy_name[MII_BUS_ID_SIZE + 3];
1079         int phy_id;
1080         int dev_id = fep->dev_id;
1081
1082         fep->phy_dev = NULL;
1083
1084         /* check for attached phy */
1085         for (phy_id = 0; (phy_id < PHY_MAX_ADDR); phy_id++) {
1086                 if ((fep->mii_bus->phy_mask & (1 << phy_id)))
1087                         continue;
1088                 if (fep->mii_bus->phy_map[phy_id] == NULL)
1089                         continue;
1090                 if (fep->mii_bus->phy_map[phy_id]->phy_id == 0)
1091                         continue;
1092                 if (dev_id--)
1093                         continue;
1094                 strncpy(mdio_bus_id, fep->mii_bus->id, MII_BUS_ID_SIZE);
1095                 break;
1096         }
1097
1098         if (phy_id >= PHY_MAX_ADDR) {
1099                 printk(KERN_INFO
1100                         "%s: no PHY, assuming direct connection to switch\n",
1101                         ndev->name);
1102                 strncpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE);
1103                 phy_id = 0;
1104         }
1105
1106         snprintf(phy_name, sizeof(phy_name), PHY_ID_FMT, mdio_bus_id, phy_id);
1107         phy_dev = phy_connect(ndev, phy_name, &fec_enet_adjust_link,
1108                               fep->phy_interface);
1109         if (IS_ERR(phy_dev)) {
1110                 printk(KERN_ERR "%s: could not attach to PHY\n", ndev->name);
1111                 return PTR_ERR(phy_dev);
1112         }
1113
1114         /* mask with MAC supported features */
1115         if (id_entry->driver_data & FEC_QUIRK_HAS_GBIT) {
1116                 phy_dev->supported &= PHY_GBIT_FEATURES;
1117                 phy_dev->supported |= SUPPORTED_Pause;
1118         }
1119         else
1120                 phy_dev->supported &= PHY_BASIC_FEATURES;
1121
1122         phy_dev->advertising = phy_dev->supported;
1123
1124         fep->phy_dev = phy_dev;
1125         fep->link = 0;
1126         fep->full_duplex = 0;
1127
1128         printk(KERN_INFO
1129                 "%s: Freescale FEC PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
1130                 ndev->name,
1131                 fep->phy_dev->drv->name, dev_name(&fep->phy_dev->dev),
1132                 fep->phy_dev->irq);
1133
1134         return 0;
1135 }
1136
1137 static int fec_enet_mii_init(struct platform_device *pdev)
1138 {
1139         static struct mii_bus *fec0_mii_bus;
1140         struct net_device *ndev = platform_get_drvdata(pdev);
1141         struct fec_enet_private *fep = netdev_priv(ndev);
1142         const struct platform_device_id *id_entry =
1143                                 platform_get_device_id(fep->pdev);
1144         int err = -ENXIO, i;
1145
1146         /*
1147          * The dual fec interfaces are not equivalent with enet-mac.
1148          * Here are the differences:
1149          *
1150          *  - fec0 supports MII & RMII modes while fec1 only supports RMII
1151          *  - fec0 acts as the 1588 time master while fec1 is slave
1152          *  - external phys can only be configured by fec0
1153          *
1154          * That is to say fec1 can not work independently. It only works
1155          * when fec0 is working. The reason behind this design is that the
1156          * second interface is added primarily for Switch mode.
1157          *
1158          * Because of the last point above, both phys are attached on fec0
1159          * mdio interface in board design, and need to be configured by
1160          * fec0 mii_bus.
1161          */
1162         if ((id_entry->driver_data & FEC_QUIRK_ENET_MAC) && fep->dev_id > 0) {
1163                 /* fec1 uses fec0 mii_bus */
1164                 if (mii_cnt && fec0_mii_bus) {
1165                         fep->mii_bus = fec0_mii_bus;
1166                         mii_cnt++;
1167                         return 0;
1168                 }
1169                 return -ENOENT;
1170         }
1171
1172         fep->mii_timeout = 0;
1173
1174         /*
1175          * Set MII speed to 2.5 MHz (= clk_get_rate() / 2 * phy_speed)
1176          *
1177          * The formula for FEC MDC is 'ref_freq / (MII_SPEED x 2)' while
1178          * for ENET-MAC is 'ref_freq / ((MII_SPEED + 1) x 2)'.  The i.MX28
1179          * Reference Manual has an error on this, and gets fixed on i.MX6Q
1180          * document.
1181          */
1182         fep->phy_speed = DIV_ROUND_UP(clk_get_rate(fep->clk_ahb), 5000000);
1183         if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
1184                 fep->phy_speed--;
1185         fep->phy_speed <<= 1;
1186         writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
1187
1188         fep->mii_bus = mdiobus_alloc();
1189         if (fep->mii_bus == NULL) {
1190                 err = -ENOMEM;
1191                 goto err_out;
1192         }
1193
1194         fep->mii_bus->name = "fec_enet_mii_bus";
1195         fep->mii_bus->read = fec_enet_mdio_read;
1196         fep->mii_bus->write = fec_enet_mdio_write;
1197         fep->mii_bus->reset = fec_enet_mdio_reset;
1198         snprintf(fep->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
1199                 pdev->name, fep->dev_id + 1);
1200         fep->mii_bus->priv = fep;
1201         fep->mii_bus->parent = &pdev->dev;
1202
1203         fep->mii_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
1204         if (!fep->mii_bus->irq) {
1205                 err = -ENOMEM;
1206                 goto err_out_free_mdiobus;
1207         }
1208
1209         for (i = 0; i < PHY_MAX_ADDR; i++)
1210                 fep->mii_bus->irq[i] = PHY_POLL;
1211
1212         if (mdiobus_register(fep->mii_bus))
1213                 goto err_out_free_mdio_irq;
1214
1215         mii_cnt++;
1216
1217         /* save fec0 mii_bus */
1218         if (id_entry->driver_data & FEC_QUIRK_ENET_MAC)
1219                 fec0_mii_bus = fep->mii_bus;
1220
1221         return 0;
1222
1223 err_out_free_mdio_irq:
1224         kfree(fep->mii_bus->irq);
1225 err_out_free_mdiobus:
1226         mdiobus_free(fep->mii_bus);
1227 err_out:
1228         return err;
1229 }
1230
1231 static void fec_enet_mii_remove(struct fec_enet_private *fep)
1232 {
1233         if (--mii_cnt == 0) {
1234                 mdiobus_unregister(fep->mii_bus);
1235                 kfree(fep->mii_bus->irq);
1236                 mdiobus_free(fep->mii_bus);
1237         }
1238 }
1239
1240 static int fec_enet_get_settings(struct net_device *ndev,
1241                                   struct ethtool_cmd *cmd)
1242 {
1243         struct fec_enet_private *fep = netdev_priv(ndev);
1244         struct phy_device *phydev = fep->phy_dev;
1245
1246         if (!phydev)
1247                 return -ENODEV;
1248
1249         return phy_ethtool_gset(phydev, cmd);
1250 }
1251
1252 static int fec_enet_set_settings(struct net_device *ndev,
1253                                  struct ethtool_cmd *cmd)
1254 {
1255         struct fec_enet_private *fep = netdev_priv(ndev);
1256         struct phy_device *phydev = fep->phy_dev;
1257
1258         if (!phydev)
1259                 return -ENODEV;
1260
1261         return phy_ethtool_sset(phydev, cmd);
1262 }
1263
1264 static void fec_enet_get_drvinfo(struct net_device *ndev,
1265                                  struct ethtool_drvinfo *info)
1266 {
1267         struct fec_enet_private *fep = netdev_priv(ndev);
1268
1269         strlcpy(info->driver, fep->pdev->dev.driver->name,
1270                 sizeof(info->driver));
1271         strlcpy(info->version, "Revision: 1.0", sizeof(info->version));
1272         strlcpy(info->bus_info, dev_name(&ndev->dev), sizeof(info->bus_info));
1273 }
1274
1275 static int fec_enet_get_ts_info(struct net_device *ndev,
1276                                 struct ethtool_ts_info *info)
1277 {
1278         struct fec_enet_private *fep = netdev_priv(ndev);
1279
1280         if (fep->bufdesc_ex) {
1281
1282                 info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
1283                                         SOF_TIMESTAMPING_RX_SOFTWARE |
1284                                         SOF_TIMESTAMPING_SOFTWARE |
1285                                         SOF_TIMESTAMPING_TX_HARDWARE |
1286                                         SOF_TIMESTAMPING_RX_HARDWARE |
1287                                         SOF_TIMESTAMPING_RAW_HARDWARE;
1288                 if (fep->ptp_clock)
1289                         info->phc_index = ptp_clock_index(fep->ptp_clock);
1290                 else
1291                         info->phc_index = -1;
1292
1293                 info->tx_types = (1 << HWTSTAMP_TX_OFF) |
1294                                  (1 << HWTSTAMP_TX_ON);
1295
1296                 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
1297                                    (1 << HWTSTAMP_FILTER_ALL);
1298                 return 0;
1299         } else {
1300                 return ethtool_op_get_ts_info(ndev, info);
1301         }
1302 }
1303
1304 static void fec_enet_get_pauseparam(struct net_device *ndev,
1305                                     struct ethtool_pauseparam *pause)
1306 {
1307         struct fec_enet_private *fep = netdev_priv(ndev);
1308
1309         pause->autoneg = (fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) != 0;
1310         pause->tx_pause = (fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) != 0;
1311         pause->rx_pause = pause->tx_pause;
1312 }
1313
1314 static int fec_enet_set_pauseparam(struct net_device *ndev,
1315                                    struct ethtool_pauseparam *pause)
1316 {
1317         struct fec_enet_private *fep = netdev_priv(ndev);
1318
1319         if (pause->tx_pause != pause->rx_pause) {
1320                 netdev_info(ndev,
1321                         "hardware only support enable/disable both tx and rx");
1322                 return -EINVAL;
1323         }
1324
1325         fep->pause_flag = 0;
1326
1327         /* tx pause must be same as rx pause */
1328         fep->pause_flag |= pause->rx_pause ? FEC_PAUSE_FLAG_ENABLE : 0;
1329         fep->pause_flag |= pause->autoneg ? FEC_PAUSE_FLAG_AUTONEG : 0;
1330
1331         if (pause->rx_pause || pause->autoneg) {
1332                 fep->phy_dev->supported |= ADVERTISED_Pause;
1333                 fep->phy_dev->advertising |= ADVERTISED_Pause;
1334         } else {
1335                 fep->phy_dev->supported &= ~ADVERTISED_Pause;
1336                 fep->phy_dev->advertising &= ~ADVERTISED_Pause;
1337         }
1338
1339         if (pause->autoneg) {
1340                 if (netif_running(ndev))
1341                         fec_stop(ndev);
1342                 phy_start_aneg(fep->phy_dev);
1343         }
1344         if (netif_running(ndev))
1345                 fec_restart(ndev, 0);
1346
1347         return 0;
1348 }
1349
1350 static const struct ethtool_ops fec_enet_ethtool_ops = {
1351         .get_pauseparam         = fec_enet_get_pauseparam,
1352         .set_pauseparam         = fec_enet_set_pauseparam,
1353         .get_settings           = fec_enet_get_settings,
1354         .set_settings           = fec_enet_set_settings,
1355         .get_drvinfo            = fec_enet_get_drvinfo,
1356         .get_link               = ethtool_op_get_link,
1357         .get_ts_info            = fec_enet_get_ts_info,
1358 };
1359
1360 static int fec_enet_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
1361 {
1362         struct fec_enet_private *fep = netdev_priv(ndev);
1363         struct phy_device *phydev = fep->phy_dev;
1364
1365         if (!netif_running(ndev))
1366                 return -EINVAL;
1367
1368         if (!phydev)
1369                 return -ENODEV;
1370
1371         if (cmd == SIOCSHWTSTAMP && fep->bufdesc_ex)
1372                 return fec_ptp_ioctl(ndev, rq, cmd);
1373
1374         return phy_mii_ioctl(phydev, rq, cmd);
1375 }
1376
1377 static void fec_enet_free_buffers(struct net_device *ndev)
1378 {
1379         struct fec_enet_private *fep = netdev_priv(ndev);
1380         unsigned int i;
1381         struct sk_buff *skb;
1382         struct bufdesc  *bdp;
1383
1384         bdp = fep->rx_bd_base;
1385         for (i = 0; i < RX_RING_SIZE; i++) {
1386                 skb = fep->rx_skbuff[i];
1387
1388                 if (bdp->cbd_bufaddr)
1389                         dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
1390                                         FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
1391                 if (skb)
1392                         dev_kfree_skb(skb);
1393                 bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
1394         }
1395
1396         bdp = fep->tx_bd_base;
1397         for (i = 0; i < TX_RING_SIZE; i++)
1398                 kfree(fep->tx_bounce[i]);
1399 }
1400
1401 static int fec_enet_alloc_buffers(struct net_device *ndev)
1402 {
1403         struct fec_enet_private *fep = netdev_priv(ndev);
1404         unsigned int i;
1405         struct sk_buff *skb;
1406         struct bufdesc  *bdp;
1407
1408         bdp = fep->rx_bd_base;
1409         for (i = 0; i < RX_RING_SIZE; i++) {
1410                 skb = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
1411                 if (!skb) {
1412                         fec_enet_free_buffers(ndev);
1413                         return -ENOMEM;
1414                 }
1415                 fep->rx_skbuff[i] = skb;
1416
1417                 bdp->cbd_bufaddr = dma_map_single(&fep->pdev->dev, skb->data,
1418                                 FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
1419                 bdp->cbd_sc = BD_ENET_RX_EMPTY;
1420
1421                 if (fep->bufdesc_ex) {
1422                         struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
1423                         ebdp->cbd_esc = BD_ENET_RX_INT;
1424                 }
1425
1426                 bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
1427         }
1428
1429         /* Set the last buffer to wrap. */
1430         bdp = fec_enet_get_prevdesc(bdp, fep->bufdesc_ex);
1431         bdp->cbd_sc |= BD_SC_WRAP;
1432
1433         bdp = fep->tx_bd_base;
1434         for (i = 0; i < TX_RING_SIZE; i++) {
1435                 fep->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL);
1436
1437                 bdp->cbd_sc = 0;
1438                 bdp->cbd_bufaddr = 0;
1439
1440                 if (fep->bufdesc_ex) {
1441                         struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
1442                         ebdp->cbd_esc = BD_ENET_TX_INT;
1443                 }
1444
1445                 bdp = fec_enet_get_nextdesc(bdp, fep->bufdesc_ex);
1446         }
1447
1448         /* Set the last buffer to wrap. */
1449         bdp = fec_enet_get_prevdesc(bdp, fep->bufdesc_ex);
1450         bdp->cbd_sc |= BD_SC_WRAP;
1451
1452         return 0;
1453 }
1454
1455 static int
1456 fec_enet_open(struct net_device *ndev)
1457 {
1458         struct fec_enet_private *fep = netdev_priv(ndev);
1459         int ret;
1460
1461         napi_enable(&fep->napi);
1462
1463         /* I should reset the ring buffers here, but I don't yet know
1464          * a simple way to do that.
1465          */
1466
1467         ret = fec_enet_alloc_buffers(ndev);
1468         if (ret)
1469                 return ret;
1470
1471         /* Probe and connect to PHY when open the interface */
1472         ret = fec_enet_mii_probe(ndev);
1473         if (ret) {
1474                 fec_enet_free_buffers(ndev);
1475                 return ret;
1476         }
1477         phy_start(fep->phy_dev);
1478         netif_start_queue(ndev);
1479         fep->opened = 1;
1480         return 0;
1481 }
1482
1483 static int
1484 fec_enet_close(struct net_device *ndev)
1485 {
1486         struct fec_enet_private *fep = netdev_priv(ndev);
1487
1488         /* Don't know what to do yet. */
1489         napi_disable(&fep->napi);
1490         fep->opened = 0;
1491         netif_stop_queue(ndev);
1492         fec_stop(ndev);
1493
1494         if (fep->phy_dev) {
1495                 phy_stop(fep->phy_dev);
1496                 phy_disconnect(fep->phy_dev);
1497         }
1498
1499         fec_enet_free_buffers(ndev);
1500
1501         return 0;
1502 }
1503
1504 /* Set or clear the multicast filter for this adaptor.
1505  * Skeleton taken from sunlance driver.
1506  * The CPM Ethernet implementation allows Multicast as well as individual
1507  * MAC address filtering.  Some of the drivers check to make sure it is
1508  * a group multicast address, and discard those that are not.  I guess I
1509  * will do the same for now, but just remove the test if you want
1510  * individual filtering as well (do the upper net layers want or support
1511  * this kind of feature?).
1512  */
1513
1514 #define HASH_BITS       6               /* #bits in hash */
1515 #define CRC32_POLY      0xEDB88320
1516
1517 static void set_multicast_list(struct net_device *ndev)
1518 {
1519         struct fec_enet_private *fep = netdev_priv(ndev);
1520         struct netdev_hw_addr *ha;
1521         unsigned int i, bit, data, crc, tmp;
1522         unsigned char hash;
1523
1524         if (ndev->flags & IFF_PROMISC) {
1525                 tmp = readl(fep->hwp + FEC_R_CNTRL);
1526                 tmp |= 0x8;
1527                 writel(tmp, fep->hwp + FEC_R_CNTRL);
1528                 return;
1529         }
1530
1531         tmp = readl(fep->hwp + FEC_R_CNTRL);
1532         tmp &= ~0x8;
1533         writel(tmp, fep->hwp + FEC_R_CNTRL);
1534
1535         if (ndev->flags & IFF_ALLMULTI) {
1536                 /* Catch all multicast addresses, so set the
1537                  * filter to all 1's
1538                  */
1539                 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1540                 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
1541
1542                 return;
1543         }
1544
1545         /* Clear filter and add the addresses in hash register
1546          */
1547         writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1548         writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
1549
1550         netdev_for_each_mc_addr(ha, ndev) {
1551                 /* calculate crc32 value of mac address */
1552                 crc = 0xffffffff;
1553
1554                 for (i = 0; i < ndev->addr_len; i++) {
1555                         data = ha->addr[i];
1556                         for (bit = 0; bit < 8; bit++, data >>= 1) {
1557                                 crc = (crc >> 1) ^
1558                                 (((crc ^ data) & 1) ? CRC32_POLY : 0);
1559                         }
1560                 }
1561
1562                 /* only upper 6 bits (HASH_BITS) are used
1563                  * which point to specific bit in he hash registers
1564                  */
1565                 hash = (crc >> (32 - HASH_BITS)) & 0x3f;
1566
1567                 if (hash > 31) {
1568                         tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1569                         tmp |= 1 << (hash - 32);
1570                         writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
1571                 } else {
1572                         tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_LOW);
1573                         tmp |= 1 << hash;
1574                         writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
1575                 }
1576         }
1577 }
1578
1579 /* Set a MAC change in hardware. */
1580 static int
1581 fec_set_mac_address(struct net_device *ndev, void *p)
1582 {
1583         struct fec_enet_private *fep = netdev_priv(ndev);
1584         struct sockaddr *addr = p;
1585
1586         if (!is_valid_ether_addr(addr->sa_data))
1587                 return -EADDRNOTAVAIL;
1588
1589         memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len);
1590
1591         writel(ndev->dev_addr[3] | (ndev->dev_addr[2] << 8) |
1592                 (ndev->dev_addr[1] << 16) | (ndev->dev_addr[0] << 24),
1593                 fep->hwp + FEC_ADDR_LOW);
1594         writel((ndev->dev_addr[5] << 16) | (ndev->dev_addr[4] << 24),
1595                 fep->hwp + FEC_ADDR_HIGH);
1596         return 0;
1597 }
1598
1599 #ifdef CONFIG_NET_POLL_CONTROLLER
1600 /**
1601  * fec_poll_controller - FEC Poll controller function
1602  * @dev: The FEC network adapter
1603  *
1604  * Polled functionality used by netconsole and others in non interrupt mode
1605  *
1606  */
1607 static void fec_poll_controller(struct net_device *dev)
1608 {
1609         int i;
1610         struct fec_enet_private *fep = netdev_priv(dev);
1611
1612         for (i = 0; i < FEC_IRQ_NUM; i++) {
1613                 if (fep->irq[i] > 0) {
1614                         disable_irq(fep->irq[i]);
1615                         fec_enet_interrupt(fep->irq[i], dev);
1616                         enable_irq(fep->irq[i]);
1617                 }
1618         }
1619 }
1620 #endif
1621
1622 static const struct net_device_ops fec_netdev_ops = {
1623         .ndo_open               = fec_enet_open,
1624         .ndo_stop               = fec_enet_close,
1625         .ndo_start_xmit         = fec_enet_start_xmit,
1626         .ndo_set_rx_mode        = set_multicast_list,
1627         .ndo_change_mtu         = eth_change_mtu,
1628         .ndo_validate_addr      = eth_validate_addr,
1629         .ndo_tx_timeout         = fec_timeout,
1630         .ndo_set_mac_address    = fec_set_mac_address,
1631         .ndo_do_ioctl           = fec_enet_ioctl,
1632 #ifdef CONFIG_NET_POLL_CONTROLLER
1633         .ndo_poll_controller    = fec_poll_controller,
1634 #endif
1635 };
1636
1637  /*
1638   * XXX:  We need to clean up on failure exits here.
1639   *
1640   */
1641 static int fec_enet_init(struct net_device *ndev)
1642 {
1643         struct fec_enet_private *fep = netdev_priv(ndev);
1644         struct bufdesc *cbd_base;
1645
1646         /* Allocate memory for buffer descriptors. */
1647         cbd_base = dma_alloc_coherent(NULL, PAGE_SIZE, &fep->bd_dma,
1648                                       GFP_KERNEL);
1649         if (!cbd_base)
1650                 return -ENOMEM;
1651
1652         memset(cbd_base, 0, PAGE_SIZE);
1653         spin_lock_init(&fep->hw_lock);
1654
1655         fep->netdev = ndev;
1656
1657         /* Get the Ethernet address */
1658         fec_get_mac(ndev);
1659
1660         /* Set receive and transmit descriptor base. */
1661         fep->rx_bd_base = cbd_base;
1662         if (fep->bufdesc_ex)
1663                 fep->tx_bd_base = (struct bufdesc *)
1664                         (((struct bufdesc_ex *)cbd_base) + RX_RING_SIZE);
1665         else
1666                 fep->tx_bd_base = cbd_base + RX_RING_SIZE;
1667
1668         /* The FEC Ethernet specific entries in the device structure */
1669         ndev->watchdog_timeo = TX_TIMEOUT;
1670         ndev->netdev_ops = &fec_netdev_ops;
1671         ndev->ethtool_ops = &fec_enet_ethtool_ops;
1672
1673         writel(FEC_RX_DISABLED_IMASK, fep->hwp + FEC_IMASK);
1674         netif_napi_add(ndev, &fep->napi, fec_enet_rx_napi, FEC_NAPI_WEIGHT);
1675
1676         fec_restart(ndev, 0);
1677
1678         return 0;
1679 }
1680
1681 #ifdef CONFIG_OF
1682 static int fec_get_phy_mode_dt(struct platform_device *pdev)
1683 {
1684         struct device_node *np = pdev->dev.of_node;
1685
1686         if (np)
1687                 return of_get_phy_mode(np);
1688
1689         return -ENODEV;
1690 }
1691
1692 static void fec_reset_phy(struct platform_device *pdev)
1693 {
1694         int err, phy_reset;
1695         int msec = 1;
1696         struct device_node *np = pdev->dev.of_node;
1697
1698         if (!np)
1699                 return;
1700
1701         of_property_read_u32(np, "phy-reset-duration", &msec);
1702         /* A sane reset duration should not be longer than 1s */
1703         if (msec > 1000)
1704                 msec = 1;
1705
1706         phy_reset = of_get_named_gpio(np, "phy-reset-gpios", 0);
1707         if (!gpio_is_valid(phy_reset))
1708                 return;
1709
1710         err = devm_gpio_request_one(&pdev->dev, phy_reset,
1711                                     GPIOF_OUT_INIT_LOW, "phy-reset");
1712         if (err) {
1713                 dev_err(&pdev->dev, "failed to get phy-reset-gpios: %d\n", err);
1714                 return;
1715         }
1716         msleep(msec);
1717         gpio_set_value(phy_reset, 1);
1718 }
1719 #else /* CONFIG_OF */
1720 static int fec_get_phy_mode_dt(struct platform_device *pdev)
1721 {
1722         return -ENODEV;
1723 }
1724
1725 static void fec_reset_phy(struct platform_device *pdev)
1726 {
1727         /*
1728          * In case of platform probe, the reset has been done
1729          * by machine code.
1730          */
1731 }
1732 #endif /* CONFIG_OF */
1733
1734 static int
1735 fec_probe(struct platform_device *pdev)
1736 {
1737         struct fec_enet_private *fep;
1738         struct fec_platform_data *pdata;
1739         struct net_device *ndev;
1740         int i, irq, ret = 0;
1741         struct resource *r;
1742         const struct of_device_id *of_id;
1743         static int dev_id;
1744         struct pinctrl *pinctrl;
1745         struct regulator *reg_phy;
1746
1747         of_id = of_match_device(fec_dt_ids, &pdev->dev);
1748         if (of_id)
1749                 pdev->id_entry = of_id->data;
1750
1751         r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1752         if (!r)
1753                 return -ENXIO;
1754
1755         /* Init network device */
1756         ndev = alloc_etherdev(sizeof(struct fec_enet_private));
1757         if (!ndev)
1758                 return -ENOMEM;
1759
1760         SET_NETDEV_DEV(ndev, &pdev->dev);
1761
1762         /* setup board info structure */
1763         fep = netdev_priv(ndev);
1764
1765         /* default enable pause frame auto negotiation */
1766         if (pdev->id_entry &&
1767             (pdev->id_entry->driver_data & FEC_QUIRK_HAS_GBIT))
1768                 fep->pause_flag |= FEC_PAUSE_FLAG_AUTONEG;
1769
1770         fep->hwp = devm_request_and_ioremap(&pdev->dev, r);
1771         fep->pdev = pdev;
1772         fep->dev_id = dev_id++;
1773
1774         fep->bufdesc_ex = 0;
1775
1776         if (!fep->hwp) {
1777                 ret = -ENOMEM;
1778                 goto failed_ioremap;
1779         }
1780
1781         platform_set_drvdata(pdev, ndev);
1782
1783         ret = fec_get_phy_mode_dt(pdev);
1784         if (ret < 0) {
1785                 pdata = pdev->dev.platform_data;
1786                 if (pdata)
1787                         fep->phy_interface = pdata->phy;
1788                 else
1789                         fep->phy_interface = PHY_INTERFACE_MODE_MII;
1790         } else {
1791                 fep->phy_interface = ret;
1792         }
1793
1794         pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
1795         if (IS_ERR(pinctrl)) {
1796                 ret = PTR_ERR(pinctrl);
1797                 goto failed_pin;
1798         }
1799
1800         fep->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
1801         if (IS_ERR(fep->clk_ipg)) {
1802                 ret = PTR_ERR(fep->clk_ipg);
1803                 goto failed_clk;
1804         }
1805
1806         fep->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
1807         if (IS_ERR(fep->clk_ahb)) {
1808                 ret = PTR_ERR(fep->clk_ahb);
1809                 goto failed_clk;
1810         }
1811
1812         fep->clk_ptp = devm_clk_get(&pdev->dev, "ptp");
1813         fep->bufdesc_ex =
1814                 pdev->id_entry->driver_data & FEC_QUIRK_HAS_BUFDESC_EX;
1815         if (IS_ERR(fep->clk_ptp)) {
1816                 ret = PTR_ERR(fep->clk_ptp);
1817                 fep->bufdesc_ex = 0;
1818         }
1819
1820         clk_prepare_enable(fep->clk_ahb);
1821         clk_prepare_enable(fep->clk_ipg);
1822         if (!IS_ERR(fep->clk_ptp))
1823                 clk_prepare_enable(fep->clk_ptp);
1824
1825         reg_phy = devm_regulator_get(&pdev->dev, "phy");
1826         if (!IS_ERR(reg_phy)) {
1827                 ret = regulator_enable(reg_phy);
1828                 if (ret) {
1829                         dev_err(&pdev->dev,
1830                                 "Failed to enable phy regulator: %d\n", ret);
1831                         goto failed_regulator;
1832                 }
1833         }
1834
1835         fec_reset_phy(pdev);
1836
1837         if (fep->bufdesc_ex)
1838                 fec_ptp_init(ndev, pdev);
1839
1840         ret = fec_enet_init(ndev);
1841         if (ret)
1842                 goto failed_init;
1843
1844         for (i = 0; i < FEC_IRQ_NUM; i++) {
1845                 irq = platform_get_irq(pdev, i);
1846                 if (irq < 0) {
1847                         if (i)
1848                                 break;
1849                         ret = irq;
1850                         goto failed_irq;
1851                 }
1852                 ret = request_irq(irq, fec_enet_interrupt, IRQF_DISABLED, pdev->name, ndev);
1853                 if (ret) {
1854                         while (--i >= 0) {
1855                                 irq = platform_get_irq(pdev, i);
1856                                 free_irq(irq, ndev);
1857                         }
1858                         goto failed_irq;
1859                 }
1860         }
1861
1862         ret = fec_enet_mii_init(pdev);
1863         if (ret)
1864                 goto failed_mii_init;
1865
1866         /* Carrier starts down, phylib will bring it up */
1867         netif_carrier_off(ndev);
1868
1869         ret = register_netdev(ndev);
1870         if (ret)
1871                 goto failed_register;
1872
1873         return 0;
1874
1875 failed_register:
1876         fec_enet_mii_remove(fep);
1877 failed_mii_init:
1878 failed_init:
1879         for (i = 0; i < FEC_IRQ_NUM; i++) {
1880                 irq = platform_get_irq(pdev, i);
1881                 if (irq > 0)
1882                         free_irq(irq, ndev);
1883         }
1884 failed_irq:
1885 failed_regulator:
1886         clk_disable_unprepare(fep->clk_ahb);
1887         clk_disable_unprepare(fep->clk_ipg);
1888         if (!IS_ERR(fep->clk_ptp))
1889                 clk_disable_unprepare(fep->clk_ptp);
1890 failed_pin:
1891 failed_clk:
1892 failed_ioremap:
1893         free_netdev(ndev);
1894
1895         return ret;
1896 }
1897
1898 static int
1899 fec_drv_remove(struct platform_device *pdev)
1900 {
1901         struct net_device *ndev = platform_get_drvdata(pdev);
1902         struct fec_enet_private *fep = netdev_priv(ndev);
1903         int i;
1904
1905         unregister_netdev(ndev);
1906         fec_enet_mii_remove(fep);
1907         del_timer_sync(&fep->time_keep);
1908         clk_disable_unprepare(fep->clk_ptp);
1909         if (fep->ptp_clock)
1910                 ptp_clock_unregister(fep->ptp_clock);
1911         clk_disable_unprepare(fep->clk_ahb);
1912         clk_disable_unprepare(fep->clk_ipg);
1913         for (i = 0; i < FEC_IRQ_NUM; i++) {
1914                 int irq = platform_get_irq(pdev, i);
1915                 if (irq > 0)
1916                         free_irq(irq, ndev);
1917         }
1918         free_netdev(ndev);
1919
1920         platform_set_drvdata(pdev, NULL);
1921
1922         return 0;
1923 }
1924
1925 #ifdef CONFIG_PM
1926 static int
1927 fec_suspend(struct device *dev)
1928 {
1929         struct net_device *ndev = dev_get_drvdata(dev);
1930         struct fec_enet_private *fep = netdev_priv(ndev);
1931
1932         if (netif_running(ndev)) {
1933                 fec_stop(ndev);
1934                 netif_device_detach(ndev);
1935         }
1936         clk_disable_unprepare(fep->clk_ahb);
1937         clk_disable_unprepare(fep->clk_ipg);
1938
1939         return 0;
1940 }
1941
1942 static int
1943 fec_resume(struct device *dev)
1944 {
1945         struct net_device *ndev = dev_get_drvdata(dev);
1946         struct fec_enet_private *fep = netdev_priv(ndev);
1947
1948         clk_prepare_enable(fep->clk_ahb);
1949         clk_prepare_enable(fep->clk_ipg);
1950         if (netif_running(ndev)) {
1951                 fec_restart(ndev, fep->full_duplex);
1952                 netif_device_attach(ndev);
1953         }
1954
1955         return 0;
1956 }
1957
1958 static const struct dev_pm_ops fec_pm_ops = {
1959         .suspend        = fec_suspend,
1960         .resume         = fec_resume,
1961         .freeze         = fec_suspend,
1962         .thaw           = fec_resume,
1963         .poweroff       = fec_suspend,
1964         .restore        = fec_resume,
1965 };
1966 #endif
1967
1968 static struct platform_driver fec_driver = {
1969         .driver = {
1970                 .name   = DRIVER_NAME,
1971                 .owner  = THIS_MODULE,
1972 #ifdef CONFIG_PM
1973                 .pm     = &fec_pm_ops,
1974 #endif
1975                 .of_match_table = fec_dt_ids,
1976         },
1977         .id_table = fec_devtype,
1978         .probe  = fec_probe,
1979         .remove = fec_drv_remove,
1980 };
1981
1982 module_platform_driver(fec_driver);
1983
1984 MODULE_LICENSE("GPL");