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