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