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