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