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