]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/net/ethernet/freescale/fec_main.c
976940e303f6062a28e70816cac76e4d40549351
[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/pm_runtime.h>
28 #include <linux/ptrace.h>
29 #include <linux/errno.h>
30 #include <linux/ioport.h>
31 #include <linux/slab.h>
32 #include <linux/interrupt.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 <net/tso.h>
41 #include <linux/tcp.h>
42 #include <linux/udp.h>
43 #include <linux/icmp.h>
44 #include <linux/spinlock.h>
45 #include <linux/workqueue.h>
46 #include <linux/bitops.h>
47 #include <linux/io.h>
48 #include <linux/irq.h>
49 #include <linux/clk.h>
50 #include <linux/platform_device.h>
51 #include <linux/phy.h>
52 #include <linux/fec.h>
53 #include <linux/of.h>
54 #include <linux/of_device.h>
55 #include <linux/of_gpio.h>
56 #include <linux/of_mdio.h>
57 #include <linux/of_net.h>
58 #include <linux/regulator/consumer.h>
59 #include <linux/if_vlan.h>
60 #include <linux/pinctrl/consumer.h>
61 #include <linux/prefetch.h>
62
63 #include <asm/cacheflush.h>
64
65 #include "fec.h"
66
67 static void set_multicast_list(struct net_device *ndev);
68 static void fec_enet_itr_coal_init(struct net_device *ndev);
69 static void fec_reset_phy(struct platform_device *pdev);
70
71 #define DRIVER_NAME     "fec"
72
73 #define FEC_ENET_GET_QUQUE(_x) ((_x == 0) ? 1 : ((_x == 1) ? 2 : 0))
74
75 /* Pause frame feild and FIFO threshold */
76 #define FEC_ENET_FCE    (1 << 5)
77 #define FEC_ENET_RSEM_V 0x84
78 #define FEC_ENET_RSFL_V 16
79 #define FEC_ENET_RAEM_V 0x8
80 #define FEC_ENET_RAFL_V 0x8
81 #define FEC_ENET_OPD_V  0xFFF0
82 #define FEC_MDIO_PM_TIMEOUT  100 /* ms */
83
84 static struct platform_device_id fec_devtype[] = {
85         {
86                 /* keep it for coldfire */
87                 .name = DRIVER_NAME,
88                 .driver_data = 0,
89         }, {
90                 .name = "imx25-fec",
91                 .driver_data = FEC_QUIRK_USE_GASKET | FEC_QUIRK_HAS_RACC,
92         }, {
93                 .name = "imx27-fec",
94                 .driver_data = FEC_QUIRK_HAS_RACC,
95         }, {
96                 .name = "imx28-fec",
97                 .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_SWAP_FRAME |
98                                 FEC_QUIRK_SINGLE_MDIO | FEC_QUIRK_HAS_RACC,
99         }, {
100                 .name = "imx6q-fec",
101                 .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
102                                 FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
103                                 FEC_QUIRK_HAS_VLAN | FEC_QUIRK_ERR006358 |
104                                 FEC_QUIRK_HAS_RACC,
105         }, {
106                 .name = "mvf600-fec",
107                 .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_RACC,
108         }, {
109                 .name = "imx6sx-fec",
110                 .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
111                                 FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
112                                 FEC_QUIRK_HAS_VLAN | FEC_QUIRK_HAS_AVB |
113                                 FEC_QUIRK_ERR007885 | FEC_QUIRK_BUG_CAPTURE |
114                                 FEC_QUIRK_HAS_RACC,
115         }, {
116                 /* sentinel */
117         }
118 };
119 MODULE_DEVICE_TABLE(platform, fec_devtype);
120
121 enum imx_fec_type {
122         IMX25_FEC = 1,  /* runs on i.mx25/50/53 */
123         IMX27_FEC,      /* runs on i.mx27/35/51 */
124         IMX28_FEC,
125         IMX6Q_FEC,
126         MVF600_FEC,
127         IMX6SX_FEC,
128 };
129
130 static const struct of_device_id fec_dt_ids[] = {
131         { .compatible = "fsl,imx25-fec", .data = &fec_devtype[IMX25_FEC], },
132         { .compatible = "fsl,imx27-fec", .data = &fec_devtype[IMX27_FEC], },
133         { .compatible = "fsl,imx28-fec", .data = &fec_devtype[IMX28_FEC], },
134         { .compatible = "fsl,imx6q-fec", .data = &fec_devtype[IMX6Q_FEC], },
135         { .compatible = "fsl,mvf600-fec", .data = &fec_devtype[MVF600_FEC], },
136         { .compatible = "fsl,imx6sx-fec", .data = &fec_devtype[IMX6SX_FEC], },
137         { /* sentinel */ }
138 };
139 MODULE_DEVICE_TABLE(of, fec_dt_ids);
140
141 static unsigned char macaddr[ETH_ALEN];
142 module_param_array(macaddr, byte, NULL, 0);
143 MODULE_PARM_DESC(macaddr, "FEC Ethernet MAC address");
144
145 #if defined(CONFIG_M5272)
146 /*
147  * Some hardware gets it MAC address out of local flash memory.
148  * if this is non-zero then assume it is the address to get MAC from.
149  */
150 #if defined(CONFIG_NETtel)
151 #define FEC_FLASHMAC    0xf0006006
152 #elif defined(CONFIG_GILBARCONAP) || defined(CONFIG_SCALES)
153 #define FEC_FLASHMAC    0xf0006000
154 #elif defined(CONFIG_CANCam)
155 #define FEC_FLASHMAC    0xf0020000
156 #elif defined (CONFIG_M5272C3)
157 #define FEC_FLASHMAC    (0xffe04000 + 4)
158 #elif defined(CONFIG_MOD5272)
159 #define FEC_FLASHMAC    0xffc0406b
160 #else
161 #define FEC_FLASHMAC    0
162 #endif
163 #endif /* CONFIG_M5272 */
164
165 /* The FEC stores dest/src/type/vlan, data, and checksum for receive packets.
166  */
167 #define PKT_MAXBUF_SIZE         1522
168 #define PKT_MINBUF_SIZE         64
169 #define PKT_MAXBLR_SIZE         1536
170
171 /* FEC receive acceleration */
172 #define FEC_RACC_IPDIS          (1 << 1)
173 #define FEC_RACC_PRODIS         (1 << 2)
174 #define FEC_RACC_OPTIONS        (FEC_RACC_IPDIS | FEC_RACC_PRODIS)
175
176 /*
177  * The 5270/5271/5280/5282/532x RX control register also contains maximum frame
178  * size bits. Other FEC hardware does not, so we need to take that into
179  * account when setting it.
180  */
181 #if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
182     defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM)
183 #define OPT_FRAME_SIZE  (PKT_MAXBUF_SIZE << 16)
184 #else
185 #define OPT_FRAME_SIZE  0
186 #endif
187
188 /* FEC MII MMFR bits definition */
189 #define FEC_MMFR_ST             (1 << 30)
190 #define FEC_MMFR_OP_READ        (2 << 28)
191 #define FEC_MMFR_OP_WRITE       (1 << 28)
192 #define FEC_MMFR_PA(v)          ((v & 0x1f) << 23)
193 #define FEC_MMFR_RA(v)          ((v & 0x1f) << 18)
194 #define FEC_MMFR_TA             (2 << 16)
195 #define FEC_MMFR_DATA(v)        (v & 0xffff)
196 /* FEC ECR bits definition */
197 #define FEC_ECR_MAGICEN         (1 << 2)
198 #define FEC_ECR_SLEEP           (1 << 3)
199
200 #define FEC_MII_TIMEOUT         30000 /* us */
201
202 /* Transmitter timeout */
203 #define TX_TIMEOUT (2 * HZ)
204
205 #define FEC_PAUSE_FLAG_AUTONEG  0x1
206 #define FEC_PAUSE_FLAG_ENABLE   0x2
207 #define FEC_WOL_HAS_MAGIC_PACKET        (0x1 << 0)
208 #define FEC_WOL_FLAG_ENABLE             (0x1 << 1)
209 #define FEC_WOL_FLAG_SLEEP_ON           (0x1 << 2)
210
211 #define COPYBREAK_DEFAULT       256
212
213 #define TSO_HEADER_SIZE         128
214 /* Max number of allowed TCP segments for software TSO */
215 #define FEC_MAX_TSO_SEGS        100
216 #define FEC_MAX_SKB_DESCS       (FEC_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS)
217
218 #define IS_TSO_HEADER(txq, addr) \
219         ((addr >= txq->tso_hdrs_dma) && \
220         (addr < txq->tso_hdrs_dma + txq->tx_ring_size * TSO_HEADER_SIZE))
221
222 static int mii_cnt;
223
224 static inline
225 struct bufdesc *fec_enet_get_nextdesc(struct bufdesc *bdp,
226                                       struct fec_enet_private *fep,
227                                       int queue_id)
228 {
229         struct bufdesc *new_bd = bdp + 1;
230         struct bufdesc_ex *ex_new_bd = (struct bufdesc_ex *)bdp + 1;
231         struct fec_enet_priv_tx_q *txq = fep->tx_queue[queue_id];
232         struct fec_enet_priv_rx_q *rxq = fep->rx_queue[queue_id];
233         struct bufdesc_ex *ex_base;
234         struct bufdesc *base;
235         int ring_size;
236
237         if (bdp >= txq->tx_bd_base) {
238                 base = txq->tx_bd_base;
239                 ring_size = txq->tx_ring_size;
240                 ex_base = (struct bufdesc_ex *)txq->tx_bd_base;
241         } else {
242                 base = rxq->rx_bd_base;
243                 ring_size = rxq->rx_ring_size;
244                 ex_base = (struct bufdesc_ex *)rxq->rx_bd_base;
245         }
246
247         if (fep->bufdesc_ex)
248                 return (struct bufdesc *)((ex_new_bd >= (ex_base + ring_size)) ?
249                         ex_base : ex_new_bd);
250         else
251                 return (new_bd >= (base + ring_size)) ?
252                         base : new_bd;
253 }
254
255 static inline
256 struct bufdesc *fec_enet_get_prevdesc(struct bufdesc *bdp,
257                                       struct fec_enet_private *fep,
258                                       int queue_id)
259 {
260         struct bufdesc *new_bd = bdp - 1;
261         struct bufdesc_ex *ex_new_bd = (struct bufdesc_ex *)bdp - 1;
262         struct fec_enet_priv_tx_q *txq = fep->tx_queue[queue_id];
263         struct fec_enet_priv_rx_q *rxq = fep->rx_queue[queue_id];
264         struct bufdesc_ex *ex_base;
265         struct bufdesc *base;
266         int ring_size;
267
268         if (bdp >= txq->tx_bd_base) {
269                 base = txq->tx_bd_base;
270                 ring_size = txq->tx_ring_size;
271                 ex_base = (struct bufdesc_ex *)txq->tx_bd_base;
272         } else {
273                 base = rxq->rx_bd_base;
274                 ring_size = rxq->rx_ring_size;
275                 ex_base = (struct bufdesc_ex *)rxq->rx_bd_base;
276         }
277
278         if (fep->bufdesc_ex)
279                 return (struct bufdesc *)((ex_new_bd < ex_base) ?
280                         (ex_new_bd + ring_size) : ex_new_bd);
281         else
282                 return (new_bd < base) ? (new_bd + ring_size) : new_bd;
283 }
284
285 static int fec_enet_get_bd_index(struct bufdesc *base, struct bufdesc *bdp,
286                                 struct fec_enet_private *fep)
287 {
288         return ((const char *)bdp - (const char *)base) / fep->bufdesc_size;
289 }
290
291 static int fec_enet_get_free_txdesc_num(struct fec_enet_private *fep,
292                                         struct fec_enet_priv_tx_q *txq)
293 {
294         int entries;
295
296         entries = ((const char *)txq->dirty_tx -
297                         (const char *)txq->cur_tx) / fep->bufdesc_size - 1;
298
299         return entries > 0 ? entries : entries + txq->tx_ring_size;
300 }
301
302 static void swap_buffer(void *bufaddr, int len)
303 {
304         int i;
305         unsigned int *buf = bufaddr;
306
307         for (i = 0; i < len; i += 4, buf++)
308                 swab32s(buf);
309 }
310
311 static void swap_buffer2(void *dst_buf, void *src_buf, int len)
312 {
313         int i;
314         unsigned int *src = src_buf;
315         unsigned int *dst = dst_buf;
316
317         for (i = 0; i < len; i += 4, src++, dst++)
318                 *dst = swab32p(src);
319 }
320
321 static void fec_dump(struct net_device *ndev)
322 {
323         struct fec_enet_private *fep = netdev_priv(ndev);
324         struct bufdesc *bdp;
325         struct fec_enet_priv_tx_q *txq;
326         int index = 0;
327
328         netdev_info(ndev, "TX ring dump\n");
329         pr_info("Nr     SC     addr       len  SKB\n");
330
331         txq = fep->tx_queue[0];
332         bdp = txq->tx_bd_base;
333
334         do {
335                 pr_info("%3u %c%c 0x%04x 0x%08lx %4u %p\n",
336                         index,
337                         bdp == txq->cur_tx ? 'S' : ' ',
338                         bdp == txq->dirty_tx ? 'H' : ' ',
339                         bdp->cbd_sc, bdp->cbd_bufaddr, bdp->cbd_datlen,
340                         txq->tx_skbuff[index]);
341                 bdp = fec_enet_get_nextdesc(bdp, fep, 0);
342                 index++;
343         } while (bdp != txq->tx_bd_base);
344 }
345
346 static inline bool is_ipv4_pkt(struct sk_buff *skb)
347 {
348         return skb->protocol == htons(ETH_P_IP) && ip_hdr(skb)->version == 4;
349 }
350
351 static int
352 fec_enet_clear_csum(struct sk_buff *skb, struct net_device *ndev)
353 {
354         /* Only run for packets requiring a checksum. */
355         if (skb->ip_summed != CHECKSUM_PARTIAL)
356                 return 0;
357
358         if (unlikely(skb_cow_head(skb, 0)))
359                 return -1;
360
361         if (is_ipv4_pkt(skb))
362                 ip_hdr(skb)->check = 0;
363         *(__sum16 *)(skb->head + skb->csum_start + skb->csum_offset) = 0;
364
365         return 0;
366 }
367
368 static struct bufdesc *
369 fec_enet_txq_submit_frag_skb(struct fec_enet_priv_tx_q *txq,
370                              struct sk_buff *skb,
371                              struct net_device *ndev)
372 {
373         struct fec_enet_private *fep = netdev_priv(ndev);
374         struct bufdesc *bdp = txq->cur_tx;
375         struct bufdesc_ex *ebdp;
376         int nr_frags = skb_shinfo(skb)->nr_frags;
377         unsigned short queue = skb_get_queue_mapping(skb);
378         int frag, frag_len;
379         unsigned short status;
380         unsigned int estatus = 0;
381         skb_frag_t *this_frag;
382         unsigned int index;
383         void *bufaddr;
384         dma_addr_t addr;
385         int i;
386
387         for (frag = 0; frag < nr_frags; frag++) {
388                 this_frag = &skb_shinfo(skb)->frags[frag];
389                 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
390                 ebdp = (struct bufdesc_ex *)bdp;
391
392                 status = bdp->cbd_sc;
393                 status &= ~BD_ENET_TX_STATS;
394                 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
395                 frag_len = skb_shinfo(skb)->frags[frag].size;
396
397                 /* Handle the last BD specially */
398                 if (frag == nr_frags - 1) {
399                         status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
400                         if (fep->bufdesc_ex) {
401                                 estatus |= BD_ENET_TX_INT;
402                                 if (unlikely(skb_shinfo(skb)->tx_flags &
403                                         SKBTX_HW_TSTAMP && fep->hwts_tx_en))
404                                         estatus |= BD_ENET_TX_TS;
405                         }
406                 }
407
408                 if (fep->bufdesc_ex) {
409                         if (fep->quirks & FEC_QUIRK_HAS_AVB)
410                                 estatus |= FEC_TX_BD_FTYPE(queue);
411                         if (skb->ip_summed == CHECKSUM_PARTIAL)
412                                 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
413                         ebdp->cbd_bdu = 0;
414                         ebdp->cbd_esc = estatus;
415                 }
416
417                 bufaddr = page_address(this_frag->page.p) + this_frag->page_offset;
418
419                 index = fec_enet_get_bd_index(txq->tx_bd_base, bdp, fep);
420                 if (((unsigned long) bufaddr) & fep->tx_align ||
421                         fep->quirks & FEC_QUIRK_SWAP_FRAME) {
422                         memcpy(txq->tx_bounce[index], bufaddr, frag_len);
423                         bufaddr = txq->tx_bounce[index];
424
425                         if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
426                                 swap_buffer(bufaddr, frag_len);
427                 }
428
429                 addr = dma_map_single(&fep->pdev->dev, bufaddr, frag_len,
430                                       DMA_TO_DEVICE);
431                 if (dma_mapping_error(&fep->pdev->dev, addr)) {
432                         dev_kfree_skb_any(skb);
433                         if (net_ratelimit())
434                                 netdev_err(ndev, "Tx DMA memory map failed\n");
435                         goto dma_mapping_error;
436                 }
437
438                 bdp->cbd_bufaddr = addr;
439                 bdp->cbd_datlen = frag_len;
440                 bdp->cbd_sc = status;
441         }
442
443         return bdp;
444 dma_mapping_error:
445         bdp = txq->cur_tx;
446         for (i = 0; i < frag; i++) {
447                 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
448                 dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
449                                 bdp->cbd_datlen, DMA_TO_DEVICE);
450         }
451         return ERR_PTR(-ENOMEM);
452 }
453
454 static int fec_enet_txq_submit_skb(struct fec_enet_priv_tx_q *txq,
455                                    struct sk_buff *skb, struct net_device *ndev)
456 {
457         struct fec_enet_private *fep = netdev_priv(ndev);
458         int nr_frags = skb_shinfo(skb)->nr_frags;
459         struct bufdesc *bdp, *last_bdp;
460         void *bufaddr;
461         dma_addr_t addr;
462         unsigned short status;
463         unsigned short buflen;
464         unsigned short queue;
465         unsigned int estatus = 0;
466         unsigned int index;
467         int entries_free;
468
469         entries_free = fec_enet_get_free_txdesc_num(fep, txq);
470         if (entries_free < MAX_SKB_FRAGS + 1) {
471                 dev_kfree_skb_any(skb);
472                 if (net_ratelimit())
473                         netdev_err(ndev, "NOT enough BD for SG!\n");
474                 return NETDEV_TX_OK;
475         }
476
477         /* Protocol checksum off-load for TCP and UDP. */
478         if (fec_enet_clear_csum(skb, ndev)) {
479                 dev_kfree_skb_any(skb);
480                 return NETDEV_TX_OK;
481         }
482
483         /* Fill in a Tx ring entry */
484         bdp = txq->cur_tx;
485         last_bdp = bdp;
486         status = bdp->cbd_sc;
487         status &= ~BD_ENET_TX_STATS;
488
489         /* Set buffer length and buffer pointer */
490         bufaddr = skb->data;
491         buflen = skb_headlen(skb);
492
493         queue = skb_get_queue_mapping(skb);
494         index = fec_enet_get_bd_index(txq->tx_bd_base, bdp, fep);
495         if (((unsigned long) bufaddr) & fep->tx_align ||
496                 fep->quirks & FEC_QUIRK_SWAP_FRAME) {
497                 memcpy(txq->tx_bounce[index], skb->data, buflen);
498                 bufaddr = txq->tx_bounce[index];
499
500                 if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
501                         swap_buffer(bufaddr, buflen);
502         }
503
504         /* Push the data cache so the CPM does not get stale memory data. */
505         addr = dma_map_single(&fep->pdev->dev, bufaddr, buflen, DMA_TO_DEVICE);
506         if (dma_mapping_error(&fep->pdev->dev, addr)) {
507                 dev_kfree_skb_any(skb);
508                 if (net_ratelimit())
509                         netdev_err(ndev, "Tx DMA memory map failed\n");
510                 return NETDEV_TX_OK;
511         }
512
513         if (nr_frags) {
514                 last_bdp = fec_enet_txq_submit_frag_skb(txq, skb, ndev);
515                 if (IS_ERR(last_bdp))
516                         return NETDEV_TX_OK;
517         } else {
518                 status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
519                 if (fep->bufdesc_ex) {
520                         estatus = BD_ENET_TX_INT;
521                         if (unlikely(skb_shinfo(skb)->tx_flags &
522                                 SKBTX_HW_TSTAMP && fep->hwts_tx_en))
523                                 estatus |= BD_ENET_TX_TS;
524                 }
525         }
526
527         if (fep->bufdesc_ex) {
528
529                 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
530
531                 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
532                         fep->hwts_tx_en))
533                         skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
534
535                 if (fep->quirks & FEC_QUIRK_HAS_AVB)
536                         estatus |= FEC_TX_BD_FTYPE(queue);
537
538                 if (skb->ip_summed == CHECKSUM_PARTIAL)
539                         estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
540
541                 ebdp->cbd_bdu = 0;
542                 ebdp->cbd_esc = estatus;
543         }
544
545         index = fec_enet_get_bd_index(txq->tx_bd_base, last_bdp, fep);
546         /* Save skb pointer */
547         txq->tx_skbuff[index] = skb;
548
549         bdp->cbd_datlen = buflen;
550         bdp->cbd_bufaddr = addr;
551
552         /* Send it on its way.  Tell FEC it's ready, interrupt when done,
553          * it's the last BD of the frame, and to put the CRC on the end.
554          */
555         status |= (BD_ENET_TX_READY | BD_ENET_TX_TC);
556         bdp->cbd_sc = status;
557
558         /* If this was the last BD in the ring, start at the beginning again. */
559         bdp = fec_enet_get_nextdesc(last_bdp, fep, queue);
560
561         skb_tx_timestamp(skb);
562
563         /* Make sure the update to bdp and tx_skbuff are performed before
564          * cur_tx.
565          */
566         wmb();
567         txq->cur_tx = bdp;
568
569         /* Trigger transmission start */
570         writel(0, fep->hwp + FEC_X_DES_ACTIVE(queue));
571
572         return 0;
573 }
574
575 static int
576 fec_enet_txq_put_data_tso(struct fec_enet_priv_tx_q *txq, struct sk_buff *skb,
577                           struct net_device *ndev,
578                           struct bufdesc *bdp, int index, char *data,
579                           int size, bool last_tcp, bool is_last)
580 {
581         struct fec_enet_private *fep = netdev_priv(ndev);
582         struct bufdesc_ex *ebdp = container_of(bdp, struct bufdesc_ex, desc);
583         unsigned short queue = skb_get_queue_mapping(skb);
584         unsigned short status;
585         unsigned int estatus = 0;
586         dma_addr_t addr;
587
588         status = bdp->cbd_sc;
589         status &= ~BD_ENET_TX_STATS;
590
591         status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
592
593         if (((unsigned long) data) & fep->tx_align ||
594                 fep->quirks & FEC_QUIRK_SWAP_FRAME) {
595                 memcpy(txq->tx_bounce[index], data, size);
596                 data = txq->tx_bounce[index];
597
598                 if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
599                         swap_buffer(data, size);
600         }
601
602         addr = dma_map_single(&fep->pdev->dev, data, size, DMA_TO_DEVICE);
603         if (dma_mapping_error(&fep->pdev->dev, addr)) {
604                 dev_kfree_skb_any(skb);
605                 if (net_ratelimit())
606                         netdev_err(ndev, "Tx DMA memory map failed\n");
607                 return NETDEV_TX_BUSY;
608         }
609
610         bdp->cbd_datlen = size;
611         bdp->cbd_bufaddr = addr;
612
613         if (fep->bufdesc_ex) {
614                 if (fep->quirks & FEC_QUIRK_HAS_AVB)
615                         estatus |= FEC_TX_BD_FTYPE(queue);
616                 if (skb->ip_summed == CHECKSUM_PARTIAL)
617                         estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
618                 ebdp->cbd_bdu = 0;
619                 ebdp->cbd_esc = estatus;
620         }
621
622         /* Handle the last BD specially */
623         if (last_tcp)
624                 status |= (BD_ENET_TX_LAST | BD_ENET_TX_TC);
625         if (is_last) {
626                 status |= BD_ENET_TX_INTR;
627                 if (fep->bufdesc_ex)
628                         ebdp->cbd_esc |= BD_ENET_TX_INT;
629         }
630
631         bdp->cbd_sc = status;
632
633         return 0;
634 }
635
636 static int
637 fec_enet_txq_put_hdr_tso(struct fec_enet_priv_tx_q *txq,
638                          struct sk_buff *skb, struct net_device *ndev,
639                          struct bufdesc *bdp, int index)
640 {
641         struct fec_enet_private *fep = netdev_priv(ndev);
642         int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
643         struct bufdesc_ex *ebdp = container_of(bdp, struct bufdesc_ex, desc);
644         unsigned short queue = skb_get_queue_mapping(skb);
645         void *bufaddr;
646         unsigned long dmabuf;
647         unsigned short status;
648         unsigned int estatus = 0;
649
650         status = bdp->cbd_sc;
651         status &= ~BD_ENET_TX_STATS;
652         status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
653
654         bufaddr = txq->tso_hdrs + index * TSO_HEADER_SIZE;
655         dmabuf = txq->tso_hdrs_dma + index * TSO_HEADER_SIZE;
656         if (((unsigned long)bufaddr) & fep->tx_align ||
657                 fep->quirks & FEC_QUIRK_SWAP_FRAME) {
658                 memcpy(txq->tx_bounce[index], skb->data, hdr_len);
659                 bufaddr = txq->tx_bounce[index];
660
661                 if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
662                         swap_buffer(bufaddr, hdr_len);
663
664                 dmabuf = dma_map_single(&fep->pdev->dev, bufaddr,
665                                         hdr_len, DMA_TO_DEVICE);
666                 if (dma_mapping_error(&fep->pdev->dev, dmabuf)) {
667                         dev_kfree_skb_any(skb);
668                         if (net_ratelimit())
669                                 netdev_err(ndev, "Tx DMA memory map failed\n");
670                         return NETDEV_TX_BUSY;
671                 }
672         }
673
674         bdp->cbd_bufaddr = dmabuf;
675         bdp->cbd_datlen = hdr_len;
676
677         if (fep->bufdesc_ex) {
678                 if (fep->quirks & FEC_QUIRK_HAS_AVB)
679                         estatus |= FEC_TX_BD_FTYPE(queue);
680                 if (skb->ip_summed == CHECKSUM_PARTIAL)
681                         estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
682                 ebdp->cbd_bdu = 0;
683                 ebdp->cbd_esc = estatus;
684         }
685
686         bdp->cbd_sc = status;
687
688         return 0;
689 }
690
691 static int fec_enet_txq_submit_tso(struct fec_enet_priv_tx_q *txq,
692                                    struct sk_buff *skb,
693                                    struct net_device *ndev)
694 {
695         struct fec_enet_private *fep = netdev_priv(ndev);
696         int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
697         int total_len, data_left;
698         struct bufdesc *bdp = txq->cur_tx;
699         unsigned short queue = skb_get_queue_mapping(skb);
700         struct tso_t tso;
701         unsigned int index = 0;
702         int ret;
703
704         if (tso_count_descs(skb) >= fec_enet_get_free_txdesc_num(fep, txq)) {
705                 dev_kfree_skb_any(skb);
706                 if (net_ratelimit())
707                         netdev_err(ndev, "NOT enough BD for TSO!\n");
708                 return NETDEV_TX_OK;
709         }
710
711         /* Protocol checksum off-load for TCP and UDP. */
712         if (fec_enet_clear_csum(skb, ndev)) {
713                 dev_kfree_skb_any(skb);
714                 return NETDEV_TX_OK;
715         }
716
717         /* Initialize the TSO handler, and prepare the first payload */
718         tso_start(skb, &tso);
719
720         total_len = skb->len - hdr_len;
721         while (total_len > 0) {
722                 char *hdr;
723
724                 index = fec_enet_get_bd_index(txq->tx_bd_base, bdp, fep);
725                 data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
726                 total_len -= data_left;
727
728                 /* prepare packet headers: MAC + IP + TCP */
729                 hdr = txq->tso_hdrs + index * TSO_HEADER_SIZE;
730                 tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
731                 ret = fec_enet_txq_put_hdr_tso(txq, skb, ndev, bdp, index);
732                 if (ret)
733                         goto err_release;
734
735                 while (data_left > 0) {
736                         int size;
737
738                         size = min_t(int, tso.size, data_left);
739                         bdp = fec_enet_get_nextdesc(bdp, fep, queue);
740                         index = fec_enet_get_bd_index(txq->tx_bd_base,
741                                                       bdp, fep);
742                         ret = fec_enet_txq_put_data_tso(txq, skb, ndev,
743                                                         bdp, index,
744                                                         tso.data, size,
745                                                         size == data_left,
746                                                         total_len == 0);
747                         if (ret)
748                                 goto err_release;
749
750                         data_left -= size;
751                         tso_build_data(skb, &tso, size);
752                 }
753
754                 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
755         }
756
757         /* Save skb pointer */
758         txq->tx_skbuff[index] = skb;
759
760         skb_tx_timestamp(skb);
761         txq->cur_tx = bdp;
762
763         /* Trigger transmission start */
764         if (!(fep->quirks & FEC_QUIRK_ERR007885) ||
765             !readl(fep->hwp + FEC_X_DES_ACTIVE(queue)) ||
766             !readl(fep->hwp + FEC_X_DES_ACTIVE(queue)) ||
767             !readl(fep->hwp + FEC_X_DES_ACTIVE(queue)) ||
768             !readl(fep->hwp + FEC_X_DES_ACTIVE(queue)))
769                 writel(0, fep->hwp + FEC_X_DES_ACTIVE(queue));
770
771         return 0;
772
773 err_release:
774         /* TODO: Release all used data descriptors for TSO */
775         return ret;
776 }
777
778 static netdev_tx_t
779 fec_enet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
780 {
781         struct fec_enet_private *fep = netdev_priv(ndev);
782         int entries_free;
783         unsigned short queue;
784         struct fec_enet_priv_tx_q *txq;
785         struct netdev_queue *nq;
786         int ret;
787
788         queue = skb_get_queue_mapping(skb);
789         txq = fep->tx_queue[queue];
790         nq = netdev_get_tx_queue(ndev, queue);
791
792         if (skb_is_gso(skb))
793                 ret = fec_enet_txq_submit_tso(txq, skb, ndev);
794         else
795                 ret = fec_enet_txq_submit_skb(txq, skb, ndev);
796         if (ret)
797                 return ret;
798
799         entries_free = fec_enet_get_free_txdesc_num(fep, txq);
800         if (entries_free <= txq->tx_stop_threshold)
801                 netif_tx_stop_queue(nq);
802
803         return NETDEV_TX_OK;
804 }
805
806 /* Init RX & TX buffer descriptors
807  */
808 static void fec_enet_bd_init(struct net_device *dev)
809 {
810         struct fec_enet_private *fep = netdev_priv(dev);
811         struct fec_enet_priv_tx_q *txq;
812         struct fec_enet_priv_rx_q *rxq;
813         struct bufdesc *bdp;
814         unsigned int i;
815         unsigned int q;
816
817         for (q = 0; q < fep->num_rx_queues; q++) {
818                 /* Initialize the receive buffer descriptors. */
819                 rxq = fep->rx_queue[q];
820                 bdp = rxq->rx_bd_base;
821
822                 for (i = 0; i < rxq->rx_ring_size; i++) {
823
824                         /* Initialize the BD for every fragment in the page. */
825                         if (bdp->cbd_bufaddr)
826                                 bdp->cbd_sc = BD_ENET_RX_EMPTY;
827                         else
828                                 bdp->cbd_sc = 0;
829                         bdp = fec_enet_get_nextdesc(bdp, fep, q);
830                 }
831
832                 /* Set the last buffer to wrap */
833                 bdp = fec_enet_get_prevdesc(bdp, fep, q);
834                 bdp->cbd_sc |= BD_SC_WRAP;
835
836                 rxq->cur_rx = rxq->rx_bd_base;
837         }
838
839         for (q = 0; q < fep->num_tx_queues; q++) {
840                 /* ...and the same for transmit */
841                 txq = fep->tx_queue[q];
842                 bdp = txq->tx_bd_base;
843                 txq->cur_tx = bdp;
844
845                 for (i = 0; i < txq->tx_ring_size; i++) {
846                         /* Initialize the BD for every fragment in the page. */
847                         bdp->cbd_sc = 0;
848                         if (txq->tx_skbuff[i]) {
849                                 dev_kfree_skb_any(txq->tx_skbuff[i]);
850                                 txq->tx_skbuff[i] = NULL;
851                         }
852                         bdp->cbd_bufaddr = 0;
853                         bdp = fec_enet_get_nextdesc(bdp, fep, q);
854                 }
855
856                 /* Set the last buffer to wrap */
857                 bdp = fec_enet_get_prevdesc(bdp, fep, q);
858                 bdp->cbd_sc |= BD_SC_WRAP;
859                 txq->dirty_tx = bdp;
860         }
861 }
862
863 static void fec_enet_active_rxring(struct net_device *ndev)
864 {
865         struct fec_enet_private *fep = netdev_priv(ndev);
866         int i;
867
868         for (i = 0; i < fep->num_rx_queues; i++)
869                 writel(0, fep->hwp + FEC_R_DES_ACTIVE(i));
870 }
871
872 static void fec_enet_enable_ring(struct net_device *ndev)
873 {
874         struct fec_enet_private *fep = netdev_priv(ndev);
875         struct fec_enet_priv_tx_q *txq;
876         struct fec_enet_priv_rx_q *rxq;
877         int i;
878
879         for (i = 0; i < fep->num_rx_queues; i++) {
880                 rxq = fep->rx_queue[i];
881                 writel(rxq->bd_dma, fep->hwp + FEC_R_DES_START(i));
882                 writel(PKT_MAXBLR_SIZE, fep->hwp + FEC_R_BUFF_SIZE(i));
883
884                 /* enable DMA1/2 */
885                 if (i)
886                         writel(RCMR_MATCHEN | RCMR_CMP(i),
887                                fep->hwp + FEC_RCMR(i));
888         }
889
890         for (i = 0; i < fep->num_tx_queues; i++) {
891                 txq = fep->tx_queue[i];
892                 writel(txq->bd_dma, fep->hwp + FEC_X_DES_START(i));
893
894                 /* enable DMA1/2 */
895                 if (i)
896                         writel(DMA_CLASS_EN | IDLE_SLOPE(i),
897                                fep->hwp + FEC_DMA_CFG(i));
898         }
899 }
900
901 static void fec_enet_reset_skb(struct net_device *ndev)
902 {
903         struct fec_enet_private *fep = netdev_priv(ndev);
904         struct fec_enet_priv_tx_q *txq;
905         int i, j;
906
907         for (i = 0; i < fep->num_tx_queues; i++) {
908                 txq = fep->tx_queue[i];
909
910                 for (j = 0; j < txq->tx_ring_size; j++) {
911                         if (txq->tx_skbuff[j]) {
912                                 dev_kfree_skb_any(txq->tx_skbuff[j]);
913                                 txq->tx_skbuff[j] = NULL;
914                         }
915                 }
916         }
917 }
918
919 /*
920  * This function is called to start or restart the FEC during a link
921  * change, transmit timeout, or to reconfigure the FEC.  The network
922  * packet processing for this device must be stopped before this call.
923  */
924 static void
925 fec_restart(struct net_device *ndev)
926 {
927         struct fec_enet_private *fep = netdev_priv(ndev);
928         u32 val;
929         u32 temp_mac[2];
930         u32 rcntl = OPT_FRAME_SIZE | 0x04;
931         u32 ecntl = 0x2; /* ETHEREN */
932
933         /* Whack a reset.  We should wait for this.
934          * For i.MX6SX SOC, enet use AXI bus, we use disable MAC
935          * instead of reset MAC itself.
936          */
937         if (fep->quirks & FEC_QUIRK_HAS_AVB) {
938                 writel(0, fep->hwp + FEC_ECNTRL);
939         } else {
940                 writel(1, fep->hwp + FEC_ECNTRL);
941                 udelay(10);
942         }
943
944         /*
945          * enet-mac reset will reset mac address registers too,
946          * so need to reconfigure it.
947          */
948         if (fep->quirks & FEC_QUIRK_ENET_MAC) {
949                 memcpy(&temp_mac, ndev->dev_addr, ETH_ALEN);
950                 writel(cpu_to_be32(temp_mac[0]), fep->hwp + FEC_ADDR_LOW);
951                 writel(cpu_to_be32(temp_mac[1]), fep->hwp + FEC_ADDR_HIGH);
952         }
953
954         /* Clear any outstanding interrupt. */
955         writel(0xffffffff, fep->hwp + FEC_IEVENT);
956
957         fec_enet_bd_init(ndev);
958
959         fec_enet_enable_ring(ndev);
960
961         /* Reset tx SKB buffers. */
962         fec_enet_reset_skb(ndev);
963
964         /* Enable MII mode */
965         if (fep->full_duplex == DUPLEX_FULL) {
966                 /* FD enable */
967                 writel(0x04, fep->hwp + FEC_X_CNTRL);
968         } else {
969                 /* No Rcv on Xmit */
970                 rcntl |= 0x02;
971                 writel(0x0, fep->hwp + FEC_X_CNTRL);
972         }
973
974         /* Set MII speed */
975         writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
976
977 #if !defined(CONFIG_M5272)
978         if (fep->quirks & FEC_QUIRK_HAS_RACC) {
979                 /* set RX checksum */
980                 val = readl(fep->hwp + FEC_RACC);
981                 if (fep->csum_flags & FLAG_RX_CSUM_ENABLED)
982                         val |= FEC_RACC_OPTIONS;
983                 else
984                         val &= ~FEC_RACC_OPTIONS;
985                 writel(val, fep->hwp + FEC_RACC);
986         }
987 #endif
988
989         /*
990          * The phy interface and speed need to get configured
991          * differently on enet-mac.
992          */
993         if (fep->quirks & FEC_QUIRK_ENET_MAC) {
994                 /* Enable flow control and length check */
995                 rcntl |= 0x40000000 | 0x00000020;
996
997                 /* RGMII, RMII or MII */
998                 if (fep->phy_interface == PHY_INTERFACE_MODE_RGMII ||
999                     fep->phy_interface == PHY_INTERFACE_MODE_RGMII_ID ||
1000                     fep->phy_interface == PHY_INTERFACE_MODE_RGMII_RXID ||
1001                     fep->phy_interface == PHY_INTERFACE_MODE_RGMII_TXID)
1002                         rcntl |= (1 << 6);
1003                 else if (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
1004                         rcntl |= (1 << 8);
1005                 else
1006                         rcntl &= ~(1 << 8);
1007
1008                 /* 1G, 100M or 10M */
1009                 if (fep->phy_dev) {
1010                         if (fep->phy_dev->speed == SPEED_1000)
1011                                 ecntl |= (1 << 5);
1012                         else if (fep->phy_dev->speed == SPEED_100)
1013                                 rcntl &= ~(1 << 9);
1014                         else
1015                                 rcntl |= (1 << 9);
1016                 }
1017         } else {
1018 #ifdef FEC_MIIGSK_ENR
1019                 if (fep->quirks & FEC_QUIRK_USE_GASKET) {
1020                         u32 cfgr;
1021                         /* disable the gasket and wait */
1022                         writel(0, fep->hwp + FEC_MIIGSK_ENR);
1023                         while (readl(fep->hwp + FEC_MIIGSK_ENR) & 4)
1024                                 udelay(1);
1025
1026                         /*
1027                          * configure the gasket:
1028                          *   RMII, 50 MHz, no loopback, no echo
1029                          *   MII, 25 MHz, no loopback, no echo
1030                          */
1031                         cfgr = (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
1032                                 ? BM_MIIGSK_CFGR_RMII : BM_MIIGSK_CFGR_MII;
1033                         if (fep->phy_dev && fep->phy_dev->speed == SPEED_10)
1034                                 cfgr |= BM_MIIGSK_CFGR_FRCONT_10M;
1035                         writel(cfgr, fep->hwp + FEC_MIIGSK_CFGR);
1036
1037                         /* re-enable the gasket */
1038                         writel(2, fep->hwp + FEC_MIIGSK_ENR);
1039                 }
1040 #endif
1041         }
1042
1043 #if !defined(CONFIG_M5272)
1044         /* enable pause frame*/
1045         if ((fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) ||
1046             ((fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) &&
1047              fep->phy_dev && fep->phy_dev->pause)) {
1048                 rcntl |= FEC_ENET_FCE;
1049
1050                 /* set FIFO threshold parameter to reduce overrun */
1051                 writel(FEC_ENET_RSEM_V, fep->hwp + FEC_R_FIFO_RSEM);
1052                 writel(FEC_ENET_RSFL_V, fep->hwp + FEC_R_FIFO_RSFL);
1053                 writel(FEC_ENET_RAEM_V, fep->hwp + FEC_R_FIFO_RAEM);
1054                 writel(FEC_ENET_RAFL_V, fep->hwp + FEC_R_FIFO_RAFL);
1055
1056                 /* OPD */
1057                 writel(FEC_ENET_OPD_V, fep->hwp + FEC_OPD);
1058         } else {
1059                 rcntl &= ~FEC_ENET_FCE;
1060         }
1061 #endif /* !defined(CONFIG_M5272) */
1062
1063         writel(rcntl, fep->hwp + FEC_R_CNTRL);
1064
1065         /* Setup multicast filter. */
1066         set_multicast_list(ndev);
1067 #ifndef CONFIG_M5272
1068         writel(0, fep->hwp + FEC_HASH_TABLE_HIGH);
1069         writel(0, fep->hwp + FEC_HASH_TABLE_LOW);
1070 #endif
1071
1072         if (fep->quirks & FEC_QUIRK_ENET_MAC) {
1073                 /* enable ENET endian swap */
1074                 ecntl |= (1 << 8);
1075                 /* enable ENET store and forward mode */
1076                 writel(1 << 8, fep->hwp + FEC_X_WMRK);
1077         }
1078
1079         if (fep->bufdesc_ex)
1080                 ecntl |= (1 << 4);
1081
1082 #ifndef CONFIG_M5272
1083         /* Enable the MIB statistic event counters */
1084         writel(0 << 31, fep->hwp + FEC_MIB_CTRLSTAT);
1085 #endif
1086
1087         /* And last, enable the transmit and receive processing */
1088         writel(ecntl, fep->hwp + FEC_ECNTRL);
1089         fec_enet_active_rxring(ndev);
1090
1091         if (fep->bufdesc_ex)
1092                 fec_ptp_start_cyclecounter(ndev);
1093
1094         /* Enable interrupts we wish to service */
1095         if (fep->link)
1096                 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1097         else
1098                 writel(FEC_ENET_MII, fep->hwp + FEC_IMASK);
1099
1100         /* Init the interrupt coalescing */
1101         fec_enet_itr_coal_init(ndev);
1102
1103 }
1104
1105 static void
1106 fec_stop(struct net_device *ndev)
1107 {
1108         struct fec_enet_private *fep = netdev_priv(ndev);
1109         struct fec_platform_data *pdata = fep->pdev->dev.platform_data;
1110         u32 rmii_mode = readl(fep->hwp + FEC_R_CNTRL) & (1 << 8);
1111         u32 val;
1112
1113         /* We cannot expect a graceful transmit stop without link !!! */
1114         if (fep->link) {
1115                 writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */
1116                 udelay(10);
1117                 if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA))
1118                         netdev_err(ndev, "Graceful transmit stop did not complete!\n");
1119         }
1120
1121         /* Whack a reset.  We should wait for this.
1122          * For i.MX6SX SOC, enet use AXI bus, we use disable MAC
1123          * instead of reset MAC itself.
1124          */
1125         if (!(fep->wol_flag & FEC_WOL_FLAG_SLEEP_ON)) {
1126                 if (fep->quirks & FEC_QUIRK_HAS_AVB) {
1127                         writel(0, fep->hwp + FEC_ECNTRL);
1128                 } else {
1129                         writel(1, fep->hwp + FEC_ECNTRL);
1130                         udelay(10);
1131                 }
1132                 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1133         } else {
1134                 writel(FEC_DEFAULT_IMASK | FEC_ENET_WAKEUP, fep->hwp + FEC_IMASK);
1135                 val = readl(fep->hwp + FEC_ECNTRL);
1136                 val |= (FEC_ECR_MAGICEN | FEC_ECR_SLEEP);
1137                 writel(val, fep->hwp + FEC_ECNTRL);
1138
1139                 if (pdata && pdata->sleep_mode_enable)
1140                         pdata->sleep_mode_enable(true);
1141         }
1142         writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
1143
1144         /* We have to keep ENET enabled to have MII interrupt stay working */
1145         if (fep->quirks & FEC_QUIRK_ENET_MAC &&
1146                 !(fep->wol_flag & FEC_WOL_FLAG_SLEEP_ON)) {
1147                 writel(2, fep->hwp + FEC_ECNTRL);
1148                 writel(rmii_mode, fep->hwp + FEC_R_CNTRL);
1149         }
1150 }
1151
1152
1153 static void
1154 fec_timeout(struct net_device *ndev)
1155 {
1156         struct fec_enet_private *fep = netdev_priv(ndev);
1157
1158         fec_dump(ndev);
1159
1160         ndev->stats.tx_errors++;
1161
1162         schedule_work(&fep->tx_timeout_work);
1163 }
1164
1165 static void fec_enet_timeout_work(struct work_struct *work)
1166 {
1167         struct fec_enet_private *fep =
1168                 container_of(work, struct fec_enet_private, tx_timeout_work);
1169         struct net_device *ndev = fep->netdev;
1170
1171         rtnl_lock();
1172         if (netif_device_present(ndev) || netif_running(ndev)) {
1173                 napi_disable(&fep->napi);
1174                 netif_tx_lock_bh(ndev);
1175                 fec_restart(ndev);
1176                 netif_wake_queue(ndev);
1177                 netif_tx_unlock_bh(ndev);
1178                 napi_enable(&fep->napi);
1179         }
1180         rtnl_unlock();
1181 }
1182
1183 static void
1184 fec_enet_hwtstamp(struct fec_enet_private *fep, unsigned ts,
1185         struct skb_shared_hwtstamps *hwtstamps)
1186 {
1187         unsigned long flags;
1188         u64 ns;
1189
1190         spin_lock_irqsave(&fep->tmreg_lock, flags);
1191         ns = timecounter_cyc2time(&fep->tc, ts);
1192         spin_unlock_irqrestore(&fep->tmreg_lock, flags);
1193
1194         memset(hwtstamps, 0, sizeof(*hwtstamps));
1195         hwtstamps->hwtstamp = ns_to_ktime(ns);
1196 }
1197
1198 static void
1199 fec_enet_tx_queue(struct net_device *ndev, u16 queue_id)
1200 {
1201         struct  fec_enet_private *fep;
1202         struct bufdesc *bdp;
1203         unsigned short status;
1204         struct  sk_buff *skb;
1205         struct fec_enet_priv_tx_q *txq;
1206         struct netdev_queue *nq;
1207         int     index = 0;
1208         int     entries_free;
1209
1210         fep = netdev_priv(ndev);
1211
1212         queue_id = FEC_ENET_GET_QUQUE(queue_id);
1213
1214         txq = fep->tx_queue[queue_id];
1215         /* get next bdp of dirty_tx */
1216         nq = netdev_get_tx_queue(ndev, queue_id);
1217         bdp = txq->dirty_tx;
1218
1219         /* get next bdp of dirty_tx */
1220         bdp = fec_enet_get_nextdesc(bdp, fep, queue_id);
1221
1222         while (bdp != READ_ONCE(txq->cur_tx)) {
1223                 /* Order the load of cur_tx and cbd_sc */
1224                 rmb();
1225                 status = READ_ONCE(bdp->cbd_sc);
1226                 if (status & BD_ENET_TX_READY)
1227                         break;
1228
1229                 index = fec_enet_get_bd_index(txq->tx_bd_base, bdp, fep);
1230
1231                 skb = txq->tx_skbuff[index];
1232                 txq->tx_skbuff[index] = NULL;
1233                 if (!IS_TSO_HEADER(txq, bdp->cbd_bufaddr))
1234                         dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
1235                                         bdp->cbd_datlen, DMA_TO_DEVICE);
1236                 bdp->cbd_bufaddr = 0;
1237                 if (!skb) {
1238                         bdp = fec_enet_get_nextdesc(bdp, fep, queue_id);
1239                         continue;
1240                 }
1241
1242                 /* Check for errors. */
1243                 if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC |
1244                                    BD_ENET_TX_RL | BD_ENET_TX_UN |
1245                                    BD_ENET_TX_CSL)) {
1246                         ndev->stats.tx_errors++;
1247                         if (status & BD_ENET_TX_HB)  /* No heartbeat */
1248                                 ndev->stats.tx_heartbeat_errors++;
1249                         if (status & BD_ENET_TX_LC)  /* Late collision */
1250                                 ndev->stats.tx_window_errors++;
1251                         if (status & BD_ENET_TX_RL)  /* Retrans limit */
1252                                 ndev->stats.tx_aborted_errors++;
1253                         if (status & BD_ENET_TX_UN)  /* Underrun */
1254                                 ndev->stats.tx_fifo_errors++;
1255                         if (status & BD_ENET_TX_CSL) /* Carrier lost */
1256                                 ndev->stats.tx_carrier_errors++;
1257                 } else {
1258                         ndev->stats.tx_packets++;
1259                         ndev->stats.tx_bytes += skb->len;
1260                 }
1261
1262                 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) &&
1263                         fep->bufdesc_ex) {
1264                         struct skb_shared_hwtstamps shhwtstamps;
1265                         struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
1266
1267                         fec_enet_hwtstamp(fep, ebdp->ts, &shhwtstamps);
1268                         skb_tstamp_tx(skb, &shhwtstamps);
1269                 }
1270
1271                 /* Deferred means some collisions occurred during transmit,
1272                  * but we eventually sent the packet OK.
1273                  */
1274                 if (status & BD_ENET_TX_DEF)
1275                         ndev->stats.collisions++;
1276
1277                 /* Free the sk buffer associated with this last transmit */
1278                 dev_kfree_skb_any(skb);
1279
1280                 /* Make sure the update to bdp and tx_skbuff are performed
1281                  * before dirty_tx
1282                  */
1283                 wmb();
1284                 txq->dirty_tx = bdp;
1285
1286                 /* Update pointer to next buffer descriptor to be transmitted */
1287                 bdp = fec_enet_get_nextdesc(bdp, fep, queue_id);
1288
1289                 /* Since we have freed up a buffer, the ring is no longer full
1290                  */
1291                 if (netif_queue_stopped(ndev)) {
1292                         entries_free = fec_enet_get_free_txdesc_num(fep, txq);
1293                         if (entries_free >= txq->tx_wake_threshold)
1294                                 netif_tx_wake_queue(nq);
1295                 }
1296         }
1297
1298         /* ERR006538: Keep the transmitter going */
1299         if (bdp != txq->cur_tx &&
1300             readl(fep->hwp + FEC_X_DES_ACTIVE(queue_id)) == 0)
1301                 writel(0, fep->hwp + FEC_X_DES_ACTIVE(queue_id));
1302 }
1303
1304 static void
1305 fec_enet_tx(struct net_device *ndev)
1306 {
1307         struct fec_enet_private *fep = netdev_priv(ndev);
1308         u16 queue_id;
1309         /* First process class A queue, then Class B and Best Effort queue */
1310         for_each_set_bit(queue_id, &fep->work_tx, FEC_ENET_MAX_TX_QS) {
1311                 clear_bit(queue_id, &fep->work_tx);
1312                 fec_enet_tx_queue(ndev, queue_id);
1313         }
1314         return;
1315 }
1316
1317 static int
1318 fec_enet_new_rxbdp(struct net_device *ndev, struct bufdesc *bdp, struct sk_buff *skb)
1319 {
1320         struct  fec_enet_private *fep = netdev_priv(ndev);
1321         int off;
1322
1323         off = ((unsigned long)skb->data) & fep->rx_align;
1324         if (off)
1325                 skb_reserve(skb, fep->rx_align + 1 - off);
1326
1327         bdp->cbd_bufaddr = dma_map_single(&fep->pdev->dev, skb->data,
1328                                           FEC_ENET_RX_FRSIZE - fep->rx_align,
1329                                           DMA_FROM_DEVICE);
1330         if (dma_mapping_error(&fep->pdev->dev, bdp->cbd_bufaddr)) {
1331                 if (net_ratelimit())
1332                         netdev_err(ndev, "Rx DMA memory map failed\n");
1333                 return -ENOMEM;
1334         }
1335
1336         return 0;
1337 }
1338
1339 static bool fec_enet_copybreak(struct net_device *ndev, struct sk_buff **skb,
1340                                struct bufdesc *bdp, u32 length, bool swap)
1341 {
1342         struct  fec_enet_private *fep = netdev_priv(ndev);
1343         struct sk_buff *new_skb;
1344
1345         if (length > fep->rx_copybreak)
1346                 return false;
1347
1348         new_skb = netdev_alloc_skb(ndev, length);
1349         if (!new_skb)
1350                 return false;
1351
1352         dma_sync_single_for_cpu(&fep->pdev->dev, bdp->cbd_bufaddr,
1353                                 FEC_ENET_RX_FRSIZE - fep->rx_align,
1354                                 DMA_FROM_DEVICE);
1355         if (!swap)
1356                 memcpy(new_skb->data, (*skb)->data, length);
1357         else
1358                 swap_buffer2(new_skb->data, (*skb)->data, length);
1359         *skb = new_skb;
1360
1361         return true;
1362 }
1363
1364 /* During a receive, the cur_rx points to the current incoming buffer.
1365  * When we update through the ring, if the next incoming buffer has
1366  * not been given to the system, we just set the empty indicator,
1367  * effectively tossing the packet.
1368  */
1369 static int
1370 fec_enet_rx_queue(struct net_device *ndev, int budget, u16 queue_id)
1371 {
1372         struct fec_enet_private *fep = netdev_priv(ndev);
1373         struct fec_enet_priv_rx_q *rxq;
1374         struct bufdesc *bdp;
1375         unsigned short status;
1376         struct  sk_buff *skb_new = NULL;
1377         struct  sk_buff *skb;
1378         ushort  pkt_len;
1379         __u8 *data;
1380         int     pkt_received = 0;
1381         struct  bufdesc_ex *ebdp = NULL;
1382         bool    vlan_packet_rcvd = false;
1383         u16     vlan_tag;
1384         int     index = 0;
1385         bool    is_copybreak;
1386         bool    need_swap = fep->quirks & FEC_QUIRK_SWAP_FRAME;
1387
1388 #ifdef CONFIG_M532x
1389         flush_cache_all();
1390 #endif
1391         queue_id = FEC_ENET_GET_QUQUE(queue_id);
1392         rxq = fep->rx_queue[queue_id];
1393
1394         /* First, grab all of the stats for the incoming packet.
1395          * These get messed up if we get called due to a busy condition.
1396          */
1397         bdp = rxq->cur_rx;
1398
1399         while (!((status = bdp->cbd_sc) & BD_ENET_RX_EMPTY)) {
1400
1401                 if (pkt_received >= budget)
1402                         break;
1403                 pkt_received++;
1404
1405                 /* Since we have allocated space to hold a complete frame,
1406                  * the last indicator should be set.
1407                  */
1408                 if ((status & BD_ENET_RX_LAST) == 0)
1409                         netdev_err(ndev, "rcv is not +last\n");
1410
1411                 writel(FEC_ENET_RXF, fep->hwp + FEC_IEVENT);
1412
1413                 /* Check for errors. */
1414                 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
1415                            BD_ENET_RX_CR | BD_ENET_RX_OV)) {
1416                         ndev->stats.rx_errors++;
1417                         if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH)) {
1418                                 /* Frame too long or too short. */
1419                                 ndev->stats.rx_length_errors++;
1420                         }
1421                         if (status & BD_ENET_RX_NO)     /* Frame alignment */
1422                                 ndev->stats.rx_frame_errors++;
1423                         if (status & BD_ENET_RX_CR)     /* CRC Error */
1424                                 ndev->stats.rx_crc_errors++;
1425                         if (status & BD_ENET_RX_OV)     /* FIFO overrun */
1426                                 ndev->stats.rx_fifo_errors++;
1427                 }
1428
1429                 /* Report late collisions as a frame error.
1430                  * On this error, the BD is closed, but we don't know what we
1431                  * have in the buffer.  So, just drop this frame on the floor.
1432                  */
1433                 if (status & BD_ENET_RX_CL) {
1434                         ndev->stats.rx_errors++;
1435                         ndev->stats.rx_frame_errors++;
1436                         goto rx_processing_done;
1437                 }
1438
1439                 /* Process the incoming frame. */
1440                 ndev->stats.rx_packets++;
1441                 pkt_len = bdp->cbd_datlen;
1442                 ndev->stats.rx_bytes += pkt_len;
1443
1444                 index = fec_enet_get_bd_index(rxq->rx_bd_base, bdp, fep);
1445                 skb = rxq->rx_skbuff[index];
1446
1447                 /* The packet length includes FCS, but we don't want to
1448                  * include that when passing upstream as it messes up
1449                  * bridging applications.
1450                  */
1451                 is_copybreak = fec_enet_copybreak(ndev, &skb, bdp, pkt_len - 4,
1452                                                   need_swap);
1453                 if (!is_copybreak) {
1454                         skb_new = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
1455                         if (unlikely(!skb_new)) {
1456                                 ndev->stats.rx_dropped++;
1457                                 goto rx_processing_done;
1458                         }
1459                         dma_unmap_single(&fep->pdev->dev, bdp->cbd_bufaddr,
1460                                          FEC_ENET_RX_FRSIZE - fep->rx_align,
1461                                          DMA_FROM_DEVICE);
1462                 }
1463
1464                 prefetch(skb->data - NET_IP_ALIGN);
1465                 skb_put(skb, pkt_len - 4);
1466                 data = skb->data;
1467                 if (!is_copybreak && need_swap)
1468                         swap_buffer(data, pkt_len);
1469
1470                 /* Extract the enhanced buffer descriptor */
1471                 ebdp = NULL;
1472                 if (fep->bufdesc_ex)
1473                         ebdp = (struct bufdesc_ex *)bdp;
1474
1475                 /* If this is a VLAN packet remove the VLAN Tag */
1476                 vlan_packet_rcvd = false;
1477                 if ((ndev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
1478                         fep->bufdesc_ex && (ebdp->cbd_esc & BD_ENET_RX_VLAN)) {
1479                         /* Push and remove the vlan tag */
1480                         struct vlan_hdr *vlan_header =
1481                                         (struct vlan_hdr *) (data + ETH_HLEN);
1482                         vlan_tag = ntohs(vlan_header->h_vlan_TCI);
1483
1484                         vlan_packet_rcvd = true;
1485
1486                         memmove(skb->data + VLAN_HLEN, data, ETH_ALEN * 2);
1487                         skb_pull(skb, VLAN_HLEN);
1488                 }
1489
1490                 skb->protocol = eth_type_trans(skb, ndev);
1491
1492                 /* Get receive timestamp from the skb */
1493                 if (fep->hwts_rx_en && fep->bufdesc_ex)
1494                         fec_enet_hwtstamp(fep, ebdp->ts,
1495                                           skb_hwtstamps(skb));
1496
1497                 if (fep->bufdesc_ex &&
1498                     (fep->csum_flags & FLAG_RX_CSUM_ENABLED)) {
1499                         if (!(ebdp->cbd_esc & FLAG_RX_CSUM_ERROR)) {
1500                                 /* don't check it */
1501                                 skb->ip_summed = CHECKSUM_UNNECESSARY;
1502                         } else {
1503                                 skb_checksum_none_assert(skb);
1504                         }
1505                 }
1506
1507                 /* Handle received VLAN packets */
1508                 if (vlan_packet_rcvd)
1509                         __vlan_hwaccel_put_tag(skb,
1510                                                htons(ETH_P_8021Q),
1511                                                vlan_tag);
1512
1513                 napi_gro_receive(&fep->napi, skb);
1514
1515                 if (is_copybreak) {
1516                         dma_sync_single_for_device(&fep->pdev->dev, bdp->cbd_bufaddr,
1517                                                    FEC_ENET_RX_FRSIZE - fep->rx_align,
1518                                                    DMA_FROM_DEVICE);
1519                 } else {
1520                         rxq->rx_skbuff[index] = skb_new;
1521                         fec_enet_new_rxbdp(ndev, bdp, skb_new);
1522                 }
1523
1524 rx_processing_done:
1525                 /* Clear the status flags for this buffer */
1526                 status &= ~BD_ENET_RX_STATS;
1527
1528                 /* Mark the buffer empty */
1529                 status |= BD_ENET_RX_EMPTY;
1530                 bdp->cbd_sc = status;
1531
1532                 if (fep->bufdesc_ex) {
1533                         struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
1534
1535                         ebdp->cbd_esc = BD_ENET_RX_INT;
1536                         ebdp->cbd_prot = 0;
1537                         ebdp->cbd_bdu = 0;
1538                 }
1539
1540                 /* Update BD pointer to next entry */
1541                 bdp = fec_enet_get_nextdesc(bdp, fep, queue_id);
1542
1543                 /* Doing this here will keep the FEC running while we process
1544                  * incoming frames.  On a heavily loaded network, we should be
1545                  * able to keep up at the expense of system resources.
1546                  */
1547                 writel(0, fep->hwp + FEC_R_DES_ACTIVE(queue_id));
1548         }
1549         rxq->cur_rx = bdp;
1550         return pkt_received;
1551 }
1552
1553 static int
1554 fec_enet_rx(struct net_device *ndev, int budget)
1555 {
1556         int     pkt_received = 0;
1557         u16     queue_id;
1558         struct fec_enet_private *fep = netdev_priv(ndev);
1559
1560         for_each_set_bit(queue_id, &fep->work_rx, FEC_ENET_MAX_RX_QS) {
1561                 clear_bit(queue_id, &fep->work_rx);
1562                 pkt_received += fec_enet_rx_queue(ndev,
1563                                         budget - pkt_received, queue_id);
1564         }
1565         return pkt_received;
1566 }
1567
1568 static bool
1569 fec_enet_collect_events(struct fec_enet_private *fep, uint int_events)
1570 {
1571         if (int_events == 0)
1572                 return false;
1573
1574         if (int_events & FEC_ENET_RXF)
1575                 fep->work_rx |= (1 << 2);
1576         if (int_events & FEC_ENET_RXF_1)
1577                 fep->work_rx |= (1 << 0);
1578         if (int_events & FEC_ENET_RXF_2)
1579                 fep->work_rx |= (1 << 1);
1580
1581         if (int_events & FEC_ENET_TXF)
1582                 fep->work_tx |= (1 << 2);
1583         if (int_events & FEC_ENET_TXF_1)
1584                 fep->work_tx |= (1 << 0);
1585         if (int_events & FEC_ENET_TXF_2)
1586                 fep->work_tx |= (1 << 1);
1587
1588         return true;
1589 }
1590
1591 static irqreturn_t
1592 fec_enet_interrupt(int irq, void *dev_id)
1593 {
1594         struct net_device *ndev = dev_id;
1595         struct fec_enet_private *fep = netdev_priv(ndev);
1596         uint int_events;
1597         irqreturn_t ret = IRQ_NONE;
1598
1599         int_events = readl(fep->hwp + FEC_IEVENT);
1600         writel(int_events, fep->hwp + FEC_IEVENT);
1601         fec_enet_collect_events(fep, int_events);
1602
1603         if ((fep->work_tx || fep->work_rx) && fep->link) {
1604                 ret = IRQ_HANDLED;
1605
1606                 if (napi_schedule_prep(&fep->napi)) {
1607                         /* Disable the NAPI interrupts */
1608                         writel(FEC_ENET_MII, fep->hwp + FEC_IMASK);
1609                         __napi_schedule(&fep->napi);
1610                 }
1611         }
1612
1613         if (int_events & FEC_ENET_MII) {
1614                 ret = IRQ_HANDLED;
1615                 complete(&fep->mdio_done);
1616         }
1617
1618         if (fep->ptp_clock)
1619                 fec_ptp_check_pps_event(fep);
1620
1621         return ret;
1622 }
1623
1624 static int fec_enet_rx_napi(struct napi_struct *napi, int budget)
1625 {
1626         struct net_device *ndev = napi->dev;
1627         struct fec_enet_private *fep = netdev_priv(ndev);
1628         int pkts;
1629
1630         pkts = fec_enet_rx(ndev, budget);
1631
1632         fec_enet_tx(ndev);
1633
1634         if (pkts < budget) {
1635                 napi_complete(napi);
1636                 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1637         }
1638         return pkts;
1639 }
1640
1641 /* ------------------------------------------------------------------------- */
1642 static void fec_get_mac(struct net_device *ndev)
1643 {
1644         struct fec_enet_private *fep = netdev_priv(ndev);
1645         struct fec_platform_data *pdata = dev_get_platdata(&fep->pdev->dev);
1646         unsigned char *iap, tmpaddr[ETH_ALEN];
1647
1648         /*
1649          * try to get mac address in following order:
1650          *
1651          * 1) module parameter via kernel command line in form
1652          *    fec.macaddr=0x00,0x04,0x9f,0x01,0x30,0xe0
1653          */
1654         iap = macaddr;
1655
1656         /*
1657          * 2) from device tree data
1658          */
1659         if (!is_valid_ether_addr(iap)) {
1660                 struct device_node *np = fep->pdev->dev.of_node;
1661                 if (np) {
1662                         const char *mac = of_get_mac_address(np);
1663                         if (mac)
1664                                 iap = (unsigned char *) mac;
1665                 }
1666         }
1667
1668         /*
1669          * 3) from flash or fuse (via platform data)
1670          */
1671         if (!is_valid_ether_addr(iap)) {
1672 #ifdef CONFIG_M5272
1673                 if (FEC_FLASHMAC)
1674                         iap = (unsigned char *)FEC_FLASHMAC;
1675 #else
1676                 if (pdata)
1677                         iap = (unsigned char *)&pdata->mac;
1678 #endif
1679         }
1680
1681         /*
1682          * 4) FEC mac registers set by bootloader
1683          */
1684         if (!is_valid_ether_addr(iap)) {
1685                 *((__be32 *) &tmpaddr[0]) =
1686                         cpu_to_be32(readl(fep->hwp + FEC_ADDR_LOW));
1687                 *((__be16 *) &tmpaddr[4]) =
1688                         cpu_to_be16(readl(fep->hwp + FEC_ADDR_HIGH) >> 16);
1689                 iap = &tmpaddr[0];
1690         }
1691
1692         /*
1693          * 5) random mac address
1694          */
1695         if (!is_valid_ether_addr(iap)) {
1696                 /* Report it and use a random ethernet address instead */
1697                 netdev_err(ndev, "Invalid MAC address: %pM\n", iap);
1698                 eth_hw_addr_random(ndev);
1699                 netdev_info(ndev, "Using random MAC address: %pM\n",
1700                             ndev->dev_addr);
1701                 return;
1702         }
1703
1704         memcpy(ndev->dev_addr, iap, ETH_ALEN);
1705
1706         /* Adjust MAC if using macaddr */
1707         if (iap == macaddr)
1708                  ndev->dev_addr[ETH_ALEN-1] = macaddr[ETH_ALEN-1] + fep->dev_id;
1709 }
1710
1711 /* ------------------------------------------------------------------------- */
1712
1713 /*
1714  * Phy section
1715  */
1716 static void fec_enet_adjust_link(struct net_device *ndev)
1717 {
1718         struct fec_enet_private *fep = netdev_priv(ndev);
1719         struct phy_device *phy_dev = fep->phy_dev;
1720         int status_change = 0;
1721
1722         /* Prevent a state halted on mii error */
1723         if (fep->mii_timeout && phy_dev->state == PHY_HALTED) {
1724                 phy_dev->state = PHY_RESUMING;
1725                 return;
1726         }
1727
1728         /*
1729          * If the netdev is down, or is going down, we're not interested
1730          * in link state events, so just mark our idea of the link as down
1731          * and ignore the event.
1732          */
1733         if (!netif_running(ndev) || !netif_device_present(ndev)) {
1734                 fep->link = 0;
1735         } else if (phy_dev->link) {
1736                 if (!fep->link) {
1737                         fep->link = phy_dev->link;
1738                         status_change = 1;
1739                 }
1740
1741                 if (fep->full_duplex != phy_dev->duplex) {
1742                         fep->full_duplex = phy_dev->duplex;
1743                         status_change = 1;
1744                 }
1745
1746                 if (phy_dev->speed != fep->speed) {
1747                         fep->speed = phy_dev->speed;
1748                         status_change = 1;
1749                 }
1750
1751                 /* if any of the above changed restart the FEC */
1752                 if (status_change) {
1753                         napi_disable(&fep->napi);
1754                         netif_tx_lock_bh(ndev);
1755                         fec_restart(ndev);
1756                         netif_wake_queue(ndev);
1757                         netif_tx_unlock_bh(ndev);
1758                         napi_enable(&fep->napi);
1759                 }
1760         } else {
1761                 if (fep->link) {
1762                         napi_disable(&fep->napi);
1763                         netif_tx_lock_bh(ndev);
1764                         fec_stop(ndev);
1765                         netif_tx_unlock_bh(ndev);
1766                         napi_enable(&fep->napi);
1767                         fep->link = phy_dev->link;
1768                         status_change = 1;
1769                 }
1770         }
1771
1772         if (status_change)
1773                 phy_print_status(phy_dev);
1774 }
1775
1776 static int fec_enet_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
1777 {
1778         struct fec_enet_private *fep = bus->priv;
1779         struct device *dev = &fep->pdev->dev;
1780         unsigned long time_left;
1781         int ret = 0;
1782
1783         ret = pm_runtime_get_sync(dev);
1784         if (ret < 0)
1785                 return ret;
1786
1787         fep->mii_timeout = 0;
1788         reinit_completion(&fep->mdio_done);
1789
1790         /* start a read op */
1791         writel(FEC_MMFR_ST | FEC_MMFR_OP_READ |
1792                 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1793                 FEC_MMFR_TA, fep->hwp + FEC_MII_DATA);
1794
1795         /* wait for end of transfer */
1796         time_left = wait_for_completion_timeout(&fep->mdio_done,
1797                         usecs_to_jiffies(FEC_MII_TIMEOUT));
1798         if (time_left == 0) {
1799                 fep->mii_timeout = 1;
1800                 netdev_err(fep->netdev, "MDIO read timeout\n");
1801                 ret = -ETIMEDOUT;
1802                 goto out;
1803         }
1804
1805         ret = FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA));
1806
1807 out:
1808         pm_runtime_mark_last_busy(dev);
1809         pm_runtime_put_autosuspend(dev);
1810
1811         return ret;
1812 }
1813
1814 static int fec_enet_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
1815                            u16 value)
1816 {
1817         struct fec_enet_private *fep = bus->priv;
1818         struct device *dev = &fep->pdev->dev;
1819         unsigned long time_left;
1820         int ret;
1821
1822         ret = pm_runtime_get_sync(dev);
1823         if (ret < 0)
1824                 return ret;
1825         else
1826                 ret = 0;
1827
1828         fep->mii_timeout = 0;
1829         reinit_completion(&fep->mdio_done);
1830
1831         /* start a write op */
1832         writel(FEC_MMFR_ST | FEC_MMFR_OP_WRITE |
1833                 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
1834                 FEC_MMFR_TA | FEC_MMFR_DATA(value),
1835                 fep->hwp + FEC_MII_DATA);
1836
1837         /* wait for end of transfer */
1838         time_left = wait_for_completion_timeout(&fep->mdio_done,
1839                         usecs_to_jiffies(FEC_MII_TIMEOUT));
1840         if (time_left == 0) {
1841                 fep->mii_timeout = 1;
1842                 netdev_err(fep->netdev, "MDIO write timeout\n");
1843                 ret  = -ETIMEDOUT;
1844         }
1845
1846         pm_runtime_mark_last_busy(dev);
1847         pm_runtime_put_autosuspend(dev);
1848
1849         return ret;
1850 }
1851
1852 static int fec_enet_clk_enable(struct net_device *ndev, bool enable)
1853 {
1854         struct fec_enet_private *fep = netdev_priv(ndev);
1855         int ret;
1856
1857         if (enable) {
1858                 ret = clk_prepare_enable(fep->clk_ahb);
1859                 if (ret)
1860                         return ret;
1861                 if (fep->clk_enet_out) {
1862                         ret = clk_prepare_enable(fep->clk_enet_out);
1863                         if (ret)
1864                                 goto failed_clk_enet_out;
1865
1866                         fec_reset_phy(fep->pdev);
1867                 }
1868                 if (fep->clk_ptp) {
1869                         mutex_lock(&fep->ptp_clk_mutex);
1870                         ret = clk_prepare_enable(fep->clk_ptp);
1871                         if (ret) {
1872                                 mutex_unlock(&fep->ptp_clk_mutex);
1873                                 goto failed_clk_ptp;
1874                         } else {
1875                                 fep->ptp_clk_on = true;
1876                         }
1877                         mutex_unlock(&fep->ptp_clk_mutex);
1878                 }
1879
1880                 ret = clk_prepare_enable(fep->clk_ref);
1881                 if (ret)
1882                         goto failed_clk_ref;
1883         } else {
1884                 clk_disable_unprepare(fep->clk_ahb);
1885                 clk_disable_unprepare(fep->clk_enet_out);
1886                 if (fep->clk_ptp) {
1887                         mutex_lock(&fep->ptp_clk_mutex);
1888                         clk_disable_unprepare(fep->clk_ptp);
1889                         fep->ptp_clk_on = false;
1890                         mutex_unlock(&fep->ptp_clk_mutex);
1891                 }
1892                 clk_disable_unprepare(fep->clk_ref);
1893         }
1894
1895         return 0;
1896
1897 failed_clk_ref:
1898         clk_disable_unprepare(fep->clk_ref);
1899 failed_clk_ptp:
1900         clk_disable_unprepare(fep->clk_enet_out);
1901 failed_clk_enet_out:
1902         clk_disable_unprepare(fep->clk_ahb);
1903
1904         return ret;
1905 }
1906
1907 static int fec_enet_mii_probe(struct net_device *ndev)
1908 {
1909         struct fec_enet_private *fep = netdev_priv(ndev);
1910         struct phy_device *phy_dev = NULL;
1911         char mdio_bus_id[MII_BUS_ID_SIZE];
1912         char phy_name[MII_BUS_ID_SIZE + 3];
1913         int phy_id;
1914         int dev_id = fep->dev_id;
1915
1916         fep->phy_dev = NULL;
1917
1918         if (fep->phy_node) {
1919                 phy_dev = of_phy_connect(ndev, fep->phy_node,
1920                                          &fec_enet_adjust_link, 0,
1921                                          fep->phy_interface);
1922                 if (!phy_dev)
1923                         return -ENODEV;
1924         } else {
1925                 /* check for attached phy */
1926                 for (phy_id = 0; (phy_id < PHY_MAX_ADDR); phy_id++) {
1927                         if ((fep->mii_bus->phy_mask & (1 << phy_id)))
1928                                 continue;
1929                         if (fep->mii_bus->phy_map[phy_id] == NULL)
1930                                 continue;
1931                         if (fep->mii_bus->phy_map[phy_id]->phy_id == 0)
1932                                 continue;
1933                         if (dev_id--)
1934                                 continue;
1935                         strlcpy(mdio_bus_id, fep->mii_bus->id, MII_BUS_ID_SIZE);
1936                         break;
1937                 }
1938
1939                 if (phy_id >= PHY_MAX_ADDR) {
1940                         netdev_info(ndev, "no PHY, assuming direct connection to switch\n");
1941                         strlcpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE);
1942                         phy_id = 0;
1943                 }
1944
1945                 snprintf(phy_name, sizeof(phy_name),
1946                          PHY_ID_FMT, mdio_bus_id, phy_id);
1947                 phy_dev = phy_connect(ndev, phy_name, &fec_enet_adjust_link,
1948                                       fep->phy_interface);
1949         }
1950
1951         if (IS_ERR(phy_dev)) {
1952                 netdev_err(ndev, "could not attach to PHY\n");
1953                 return PTR_ERR(phy_dev);
1954         }
1955
1956         /* mask with MAC supported features */
1957         if (fep->quirks & FEC_QUIRK_HAS_GBIT) {
1958                 phy_dev->supported &= PHY_GBIT_FEATURES;
1959                 phy_dev->supported &= ~SUPPORTED_1000baseT_Half;
1960 #if !defined(CONFIG_M5272)
1961                 phy_dev->supported |= SUPPORTED_Pause;
1962 #endif
1963         }
1964         else
1965                 phy_dev->supported &= PHY_BASIC_FEATURES;
1966
1967         phy_dev->advertising = phy_dev->supported;
1968
1969         fep->phy_dev = phy_dev;
1970         fep->link = 0;
1971         fep->full_duplex = 0;
1972
1973         netdev_info(ndev, "Freescale FEC PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
1974                     fep->phy_dev->drv->name, dev_name(&fep->phy_dev->dev),
1975                     fep->phy_dev->irq);
1976
1977         return 0;
1978 }
1979
1980 static int fec_enet_mii_init(struct platform_device *pdev)
1981 {
1982         static struct mii_bus *fec0_mii_bus;
1983         struct net_device *ndev = platform_get_drvdata(pdev);
1984         struct fec_enet_private *fep = netdev_priv(ndev);
1985         struct device_node *node;
1986         int err = -ENXIO, i;
1987         u32 mii_speed, holdtime;
1988
1989         /*
1990          * The i.MX28 dual fec interfaces are not equal.
1991          * Here are the differences:
1992          *
1993          *  - fec0 supports MII & RMII modes while fec1 only supports RMII
1994          *  - fec0 acts as the 1588 time master while fec1 is slave
1995          *  - external phys can only be configured by fec0
1996          *
1997          * That is to say fec1 can not work independently. It only works
1998          * when fec0 is working. The reason behind this design is that the
1999          * second interface is added primarily for Switch mode.
2000          *
2001          * Because of the last point above, both phys are attached on fec0
2002          * mdio interface in board design, and need to be configured by
2003          * fec0 mii_bus.
2004          */
2005         if ((fep->quirks & FEC_QUIRK_SINGLE_MDIO) && fep->dev_id > 0) {
2006                 /* fec1 uses fec0 mii_bus */
2007                 if (mii_cnt && fec0_mii_bus) {
2008                         fep->mii_bus = fec0_mii_bus;
2009                         mii_cnt++;
2010                         return 0;
2011                 }
2012                 return -ENOENT;
2013         }
2014
2015         fep->mii_timeout = 0;
2016
2017         /*
2018          * Set MII speed to 2.5 MHz (= clk_get_rate() / 2 * phy_speed)
2019          *
2020          * The formula for FEC MDC is 'ref_freq / (MII_SPEED x 2)' while
2021          * for ENET-MAC is 'ref_freq / ((MII_SPEED + 1) x 2)'.  The i.MX28
2022          * Reference Manual has an error on this, and gets fixed on i.MX6Q
2023          * document.
2024          */
2025         mii_speed = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), 5000000);
2026         if (fep->quirks & FEC_QUIRK_ENET_MAC)
2027                 mii_speed--;
2028         if (mii_speed > 63) {
2029                 dev_err(&pdev->dev,
2030                         "fec clock (%lu) to fast to get right mii speed\n",
2031                         clk_get_rate(fep->clk_ipg));
2032                 err = -EINVAL;
2033                 goto err_out;
2034         }
2035
2036         /*
2037          * The i.MX28 and i.MX6 types have another filed in the MSCR (aka
2038          * MII_SPEED) register that defines the MDIO output hold time. Earlier
2039          * versions are RAZ there, so just ignore the difference and write the
2040          * register always.
2041          * The minimal hold time according to IEE802.3 (clause 22) is 10 ns.
2042          * HOLDTIME + 1 is the number of clk cycles the fec is holding the
2043          * output.
2044          * The HOLDTIME bitfield takes values between 0 and 7 (inclusive).
2045          * Given that ceil(clkrate / 5000000) <= 64, the calculation for
2046          * holdtime cannot result in a value greater than 3.
2047          */
2048         holdtime = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), 100000000) - 1;
2049
2050         fep->phy_speed = mii_speed << 1 | holdtime << 8;
2051
2052         writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
2053
2054         fep->mii_bus = mdiobus_alloc();
2055         if (fep->mii_bus == NULL) {
2056                 err = -ENOMEM;
2057                 goto err_out;
2058         }
2059
2060         fep->mii_bus->name = "fec_enet_mii_bus";
2061         fep->mii_bus->read = fec_enet_mdio_read;
2062         fep->mii_bus->write = fec_enet_mdio_write;
2063         snprintf(fep->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
2064                 pdev->name, fep->dev_id + 1);
2065         fep->mii_bus->priv = fep;
2066         fep->mii_bus->parent = &pdev->dev;
2067
2068         fep->mii_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
2069         if (!fep->mii_bus->irq) {
2070                 err = -ENOMEM;
2071                 goto err_out_free_mdiobus;
2072         }
2073
2074         for (i = 0; i < PHY_MAX_ADDR; i++)
2075                 fep->mii_bus->irq[i] = PHY_POLL;
2076
2077         node = of_get_child_by_name(pdev->dev.of_node, "mdio");
2078         if (node) {
2079                 err = of_mdiobus_register(fep->mii_bus, node);
2080                 of_node_put(node);
2081         } else {
2082                 err = mdiobus_register(fep->mii_bus);
2083         }
2084
2085         if (err)
2086                 goto err_out_free_mdio_irq;
2087
2088         mii_cnt++;
2089
2090         /* save fec0 mii_bus */
2091         if (fep->quirks & FEC_QUIRK_SINGLE_MDIO)
2092                 fec0_mii_bus = fep->mii_bus;
2093
2094         return 0;
2095
2096 err_out_free_mdio_irq:
2097         kfree(fep->mii_bus->irq);
2098 err_out_free_mdiobus:
2099         mdiobus_free(fep->mii_bus);
2100 err_out:
2101         return err;
2102 }
2103
2104 static void fec_enet_mii_remove(struct fec_enet_private *fep)
2105 {
2106         if (--mii_cnt == 0) {
2107                 mdiobus_unregister(fep->mii_bus);
2108                 kfree(fep->mii_bus->irq);
2109                 mdiobus_free(fep->mii_bus);
2110         }
2111 }
2112
2113 static int fec_enet_get_settings(struct net_device *ndev,
2114                                   struct ethtool_cmd *cmd)
2115 {
2116         struct fec_enet_private *fep = netdev_priv(ndev);
2117         struct phy_device *phydev = fep->phy_dev;
2118
2119         if (!phydev)
2120                 return -ENODEV;
2121
2122         return phy_ethtool_gset(phydev, cmd);
2123 }
2124
2125 static int fec_enet_set_settings(struct net_device *ndev,
2126                                  struct ethtool_cmd *cmd)
2127 {
2128         struct fec_enet_private *fep = netdev_priv(ndev);
2129         struct phy_device *phydev = fep->phy_dev;
2130
2131         if (!phydev)
2132                 return -ENODEV;
2133
2134         return phy_ethtool_sset(phydev, cmd);
2135 }
2136
2137 static void fec_enet_get_drvinfo(struct net_device *ndev,
2138                                  struct ethtool_drvinfo *info)
2139 {
2140         struct fec_enet_private *fep = netdev_priv(ndev);
2141
2142         strlcpy(info->driver, fep->pdev->dev.driver->name,
2143                 sizeof(info->driver));
2144         strlcpy(info->version, "Revision: 1.0", sizeof(info->version));
2145         strlcpy(info->bus_info, dev_name(&ndev->dev), sizeof(info->bus_info));
2146 }
2147
2148 static int fec_enet_get_regs_len(struct net_device *ndev)
2149 {
2150         struct fec_enet_private *fep = netdev_priv(ndev);
2151         struct resource *r;
2152         int s = 0;
2153
2154         r = platform_get_resource(fep->pdev, IORESOURCE_MEM, 0);
2155         if (r)
2156                 s = resource_size(r);
2157
2158         return s;
2159 }
2160
2161 /* List of registers that can be safety be read to dump them with ethtool */
2162 #if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
2163         defined(CONFIG_M520x) || defined(CONFIG_M532x) ||               \
2164         defined(CONFIG_ARCH_MXC) || defined(CONFIG_SOC_IMX28)
2165 static u32 fec_enet_register_offset[] = {
2166         FEC_IEVENT, FEC_IMASK, FEC_R_DES_ACTIVE_0, FEC_X_DES_ACTIVE_0,
2167         FEC_ECNTRL, FEC_MII_DATA, FEC_MII_SPEED, FEC_MIB_CTRLSTAT, FEC_R_CNTRL,
2168         FEC_X_CNTRL, FEC_ADDR_LOW, FEC_ADDR_HIGH, FEC_OPD, FEC_TXIC0, FEC_TXIC1,
2169         FEC_TXIC2, FEC_RXIC0, FEC_RXIC1, FEC_RXIC2, FEC_HASH_TABLE_HIGH,
2170         FEC_HASH_TABLE_LOW, FEC_GRP_HASH_TABLE_HIGH, FEC_GRP_HASH_TABLE_LOW,
2171         FEC_X_WMRK, FEC_R_BOUND, FEC_R_FSTART, FEC_R_DES_START_1,
2172         FEC_X_DES_START_1, FEC_R_BUFF_SIZE_1, FEC_R_DES_START_2,
2173         FEC_X_DES_START_2, FEC_R_BUFF_SIZE_2, FEC_R_DES_START_0,
2174         FEC_X_DES_START_0, FEC_R_BUFF_SIZE_0, FEC_R_FIFO_RSFL, FEC_R_FIFO_RSEM,
2175         FEC_R_FIFO_RAEM, FEC_R_FIFO_RAFL, FEC_RACC, FEC_RCMR_1, FEC_RCMR_2,
2176         FEC_DMA_CFG_1, FEC_DMA_CFG_2, FEC_R_DES_ACTIVE_1, FEC_X_DES_ACTIVE_1,
2177         FEC_R_DES_ACTIVE_2, FEC_X_DES_ACTIVE_2, FEC_QOS_SCHEME,
2178         RMON_T_DROP, RMON_T_PACKETS, RMON_T_BC_PKT, RMON_T_MC_PKT,
2179         RMON_T_CRC_ALIGN, RMON_T_UNDERSIZE, RMON_T_OVERSIZE, RMON_T_FRAG,
2180         RMON_T_JAB, RMON_T_COL, RMON_T_P64, RMON_T_P65TO127, RMON_T_P128TO255,
2181         RMON_T_P256TO511, RMON_T_P512TO1023, RMON_T_P1024TO2047,
2182         RMON_T_P_GTE2048, RMON_T_OCTETS,
2183         IEEE_T_DROP, IEEE_T_FRAME_OK, IEEE_T_1COL, IEEE_T_MCOL, IEEE_T_DEF,
2184         IEEE_T_LCOL, IEEE_T_EXCOL, IEEE_T_MACERR, IEEE_T_CSERR, IEEE_T_SQE,
2185         IEEE_T_FDXFC, IEEE_T_OCTETS_OK,
2186         RMON_R_PACKETS, RMON_R_BC_PKT, RMON_R_MC_PKT, RMON_R_CRC_ALIGN,
2187         RMON_R_UNDERSIZE, RMON_R_OVERSIZE, RMON_R_FRAG, RMON_R_JAB,
2188         RMON_R_RESVD_O, RMON_R_P64, RMON_R_P65TO127, RMON_R_P128TO255,
2189         RMON_R_P256TO511, RMON_R_P512TO1023, RMON_R_P1024TO2047,
2190         RMON_R_P_GTE2048, RMON_R_OCTETS,
2191         IEEE_R_DROP, IEEE_R_FRAME_OK, IEEE_R_CRC, IEEE_R_ALIGN, IEEE_R_MACERR,
2192         IEEE_R_FDXFC, IEEE_R_OCTETS_OK
2193 };
2194 #else
2195 static u32 fec_enet_register_offset[] = {
2196         FEC_ECNTRL, FEC_IEVENT, FEC_IMASK, FEC_IVEC, FEC_R_DES_ACTIVE_0,
2197         FEC_R_DES_ACTIVE_1, FEC_R_DES_ACTIVE_2, FEC_X_DES_ACTIVE_0,
2198         FEC_X_DES_ACTIVE_1, FEC_X_DES_ACTIVE_2, FEC_MII_DATA, FEC_MII_SPEED,
2199         FEC_R_BOUND, FEC_R_FSTART, FEC_X_WMRK, FEC_X_FSTART, FEC_R_CNTRL,
2200         FEC_MAX_FRM_LEN, FEC_X_CNTRL, FEC_ADDR_LOW, FEC_ADDR_HIGH,
2201         FEC_GRP_HASH_TABLE_HIGH, FEC_GRP_HASH_TABLE_LOW, FEC_R_DES_START_0,
2202         FEC_R_DES_START_1, FEC_R_DES_START_2, FEC_X_DES_START_0,
2203         FEC_X_DES_START_1, FEC_X_DES_START_2, FEC_R_BUFF_SIZE_0,
2204         FEC_R_BUFF_SIZE_1, FEC_R_BUFF_SIZE_2
2205 };
2206 #endif
2207
2208 static void fec_enet_get_regs(struct net_device *ndev,
2209                               struct ethtool_regs *regs, void *regbuf)
2210 {
2211         struct fec_enet_private *fep = netdev_priv(ndev);
2212         u32 __iomem *theregs = (u32 __iomem *)fep->hwp;
2213         u32 *buf = (u32 *)regbuf;
2214         u32 i, off;
2215
2216         memset(buf, 0, regs->len);
2217
2218         for (i = 0; i < ARRAY_SIZE(fec_enet_register_offset); i++) {
2219                 off = fec_enet_register_offset[i] / 4;
2220                 buf[off] = readl(&theregs[off]);
2221         }
2222 }
2223
2224 static int fec_enet_get_ts_info(struct net_device *ndev,
2225                                 struct ethtool_ts_info *info)
2226 {
2227         struct fec_enet_private *fep = netdev_priv(ndev);
2228
2229         if (fep->bufdesc_ex) {
2230
2231                 info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
2232                                         SOF_TIMESTAMPING_RX_SOFTWARE |
2233                                         SOF_TIMESTAMPING_SOFTWARE |
2234                                         SOF_TIMESTAMPING_TX_HARDWARE |
2235                                         SOF_TIMESTAMPING_RX_HARDWARE |
2236                                         SOF_TIMESTAMPING_RAW_HARDWARE;
2237                 if (fep->ptp_clock)
2238                         info->phc_index = ptp_clock_index(fep->ptp_clock);
2239                 else
2240                         info->phc_index = -1;
2241
2242                 info->tx_types = (1 << HWTSTAMP_TX_OFF) |
2243                                  (1 << HWTSTAMP_TX_ON);
2244
2245                 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
2246                                    (1 << HWTSTAMP_FILTER_ALL);
2247                 return 0;
2248         } else {
2249                 return ethtool_op_get_ts_info(ndev, info);
2250         }
2251 }
2252
2253 #if !defined(CONFIG_M5272)
2254
2255 static void fec_enet_get_pauseparam(struct net_device *ndev,
2256                                     struct ethtool_pauseparam *pause)
2257 {
2258         struct fec_enet_private *fep = netdev_priv(ndev);
2259
2260         pause->autoneg = (fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) != 0;
2261         pause->tx_pause = (fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) != 0;
2262         pause->rx_pause = pause->tx_pause;
2263 }
2264
2265 static int fec_enet_set_pauseparam(struct net_device *ndev,
2266                                    struct ethtool_pauseparam *pause)
2267 {
2268         struct fec_enet_private *fep = netdev_priv(ndev);
2269
2270         if (!fep->phy_dev)
2271                 return -ENODEV;
2272
2273         if (pause->tx_pause != pause->rx_pause) {
2274                 netdev_info(ndev,
2275                         "hardware only support enable/disable both tx and rx");
2276                 return -EINVAL;
2277         }
2278
2279         fep->pause_flag = 0;
2280
2281         /* tx pause must be same as rx pause */
2282         fep->pause_flag |= pause->rx_pause ? FEC_PAUSE_FLAG_ENABLE : 0;
2283         fep->pause_flag |= pause->autoneg ? FEC_PAUSE_FLAG_AUTONEG : 0;
2284
2285         if (pause->rx_pause || pause->autoneg) {
2286                 fep->phy_dev->supported |= ADVERTISED_Pause;
2287                 fep->phy_dev->advertising |= ADVERTISED_Pause;
2288         } else {
2289                 fep->phy_dev->supported &= ~ADVERTISED_Pause;
2290                 fep->phy_dev->advertising &= ~ADVERTISED_Pause;
2291         }
2292
2293         if (pause->autoneg) {
2294                 if (netif_running(ndev))
2295                         fec_stop(ndev);
2296                 phy_start_aneg(fep->phy_dev);
2297         }
2298         if (netif_running(ndev)) {
2299                 napi_disable(&fep->napi);
2300                 netif_tx_lock_bh(ndev);
2301                 fec_restart(ndev);
2302                 netif_wake_queue(ndev);
2303                 netif_tx_unlock_bh(ndev);
2304                 napi_enable(&fep->napi);
2305         }
2306
2307         return 0;
2308 }
2309
2310 static const struct fec_stat {
2311         char name[ETH_GSTRING_LEN];
2312         u16 offset;
2313 } fec_stats[] = {
2314         /* RMON TX */
2315         { "tx_dropped", RMON_T_DROP },
2316         { "tx_packets", RMON_T_PACKETS },
2317         { "tx_broadcast", RMON_T_BC_PKT },
2318         { "tx_multicast", RMON_T_MC_PKT },
2319         { "tx_crc_errors", RMON_T_CRC_ALIGN },
2320         { "tx_undersize", RMON_T_UNDERSIZE },
2321         { "tx_oversize", RMON_T_OVERSIZE },
2322         { "tx_fragment", RMON_T_FRAG },
2323         { "tx_jabber", RMON_T_JAB },
2324         { "tx_collision", RMON_T_COL },
2325         { "tx_64byte", RMON_T_P64 },
2326         { "tx_65to127byte", RMON_T_P65TO127 },
2327         { "tx_128to255byte", RMON_T_P128TO255 },
2328         { "tx_256to511byte", RMON_T_P256TO511 },
2329         { "tx_512to1023byte", RMON_T_P512TO1023 },
2330         { "tx_1024to2047byte", RMON_T_P1024TO2047 },
2331         { "tx_GTE2048byte", RMON_T_P_GTE2048 },
2332         { "tx_octets", RMON_T_OCTETS },
2333
2334         /* IEEE TX */
2335         { "IEEE_tx_drop", IEEE_T_DROP },
2336         { "IEEE_tx_frame_ok", IEEE_T_FRAME_OK },
2337         { "IEEE_tx_1col", IEEE_T_1COL },
2338         { "IEEE_tx_mcol", IEEE_T_MCOL },
2339         { "IEEE_tx_def", IEEE_T_DEF },
2340         { "IEEE_tx_lcol", IEEE_T_LCOL },
2341         { "IEEE_tx_excol", IEEE_T_EXCOL },
2342         { "IEEE_tx_macerr", IEEE_T_MACERR },
2343         { "IEEE_tx_cserr", IEEE_T_CSERR },
2344         { "IEEE_tx_sqe", IEEE_T_SQE },
2345         { "IEEE_tx_fdxfc", IEEE_T_FDXFC },
2346         { "IEEE_tx_octets_ok", IEEE_T_OCTETS_OK },
2347
2348         /* RMON RX */
2349         { "rx_packets", RMON_R_PACKETS },
2350         { "rx_broadcast", RMON_R_BC_PKT },
2351         { "rx_multicast", RMON_R_MC_PKT },
2352         { "rx_crc_errors", RMON_R_CRC_ALIGN },
2353         { "rx_undersize", RMON_R_UNDERSIZE },
2354         { "rx_oversize", RMON_R_OVERSIZE },
2355         { "rx_fragment", RMON_R_FRAG },
2356         { "rx_jabber", RMON_R_JAB },
2357         { "rx_64byte", RMON_R_P64 },
2358         { "rx_65to127byte", RMON_R_P65TO127 },
2359         { "rx_128to255byte", RMON_R_P128TO255 },
2360         { "rx_256to511byte", RMON_R_P256TO511 },
2361         { "rx_512to1023byte", RMON_R_P512TO1023 },
2362         { "rx_1024to2047byte", RMON_R_P1024TO2047 },
2363         { "rx_GTE2048byte", RMON_R_P_GTE2048 },
2364         { "rx_octets", RMON_R_OCTETS },
2365
2366         /* IEEE RX */
2367         { "IEEE_rx_drop", IEEE_R_DROP },
2368         { "IEEE_rx_frame_ok", IEEE_R_FRAME_OK },
2369         { "IEEE_rx_crc", IEEE_R_CRC },
2370         { "IEEE_rx_align", IEEE_R_ALIGN },
2371         { "IEEE_rx_macerr", IEEE_R_MACERR },
2372         { "IEEE_rx_fdxfc", IEEE_R_FDXFC },
2373         { "IEEE_rx_octets_ok", IEEE_R_OCTETS_OK },
2374 };
2375
2376 static void fec_enet_get_ethtool_stats(struct net_device *dev,
2377         struct ethtool_stats *stats, u64 *data)
2378 {
2379         struct fec_enet_private *fep = netdev_priv(dev);
2380         int i;
2381
2382         for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
2383                 data[i] = readl(fep->hwp + fec_stats[i].offset);
2384 }
2385
2386 static void fec_enet_get_strings(struct net_device *netdev,
2387         u32 stringset, u8 *data)
2388 {
2389         int i;
2390         switch (stringset) {
2391         case ETH_SS_STATS:
2392                 for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
2393                         memcpy(data + i * ETH_GSTRING_LEN,
2394                                 fec_stats[i].name, ETH_GSTRING_LEN);
2395                 break;
2396         }
2397 }
2398
2399 static int fec_enet_get_sset_count(struct net_device *dev, int sset)
2400 {
2401         switch (sset) {
2402         case ETH_SS_STATS:
2403                 return ARRAY_SIZE(fec_stats);
2404         default:
2405                 return -EOPNOTSUPP;
2406         }
2407 }
2408 #endif /* !defined(CONFIG_M5272) */
2409
2410 static int fec_enet_nway_reset(struct net_device *dev)
2411 {
2412         struct fec_enet_private *fep = netdev_priv(dev);
2413         struct phy_device *phydev = fep->phy_dev;
2414
2415         if (!phydev)
2416                 return -ENODEV;
2417
2418         return genphy_restart_aneg(phydev);
2419 }
2420
2421 /* ITR clock source is enet system clock (clk_ahb).
2422  * TCTT unit is cycle_ns * 64 cycle
2423  * So, the ICTT value = X us / (cycle_ns * 64)
2424  */
2425 static int fec_enet_us_to_itr_clock(struct net_device *ndev, int us)
2426 {
2427         struct fec_enet_private *fep = netdev_priv(ndev);
2428
2429         return us * (fep->itr_clk_rate / 64000) / 1000;
2430 }
2431
2432 /* Set threshold for interrupt coalescing */
2433 static void fec_enet_itr_coal_set(struct net_device *ndev)
2434 {
2435         struct fec_enet_private *fep = netdev_priv(ndev);
2436         int rx_itr, tx_itr;
2437
2438         if (!(fep->quirks & FEC_QUIRK_HAS_AVB))
2439                 return;
2440
2441         /* Must be greater than zero to avoid unpredictable behavior */
2442         if (!fep->rx_time_itr || !fep->rx_pkts_itr ||
2443             !fep->tx_time_itr || !fep->tx_pkts_itr)
2444                 return;
2445
2446         /* Select enet system clock as Interrupt Coalescing
2447          * timer Clock Source
2448          */
2449         rx_itr = FEC_ITR_CLK_SEL;
2450         tx_itr = FEC_ITR_CLK_SEL;
2451
2452         /* set ICFT and ICTT */
2453         rx_itr |= FEC_ITR_ICFT(fep->rx_pkts_itr);
2454         rx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->rx_time_itr));
2455         tx_itr |= FEC_ITR_ICFT(fep->tx_pkts_itr);
2456         tx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->tx_time_itr));
2457
2458         rx_itr |= FEC_ITR_EN;
2459         tx_itr |= FEC_ITR_EN;
2460
2461         writel(tx_itr, fep->hwp + FEC_TXIC0);
2462         writel(rx_itr, fep->hwp + FEC_RXIC0);
2463         writel(tx_itr, fep->hwp + FEC_TXIC1);
2464         writel(rx_itr, fep->hwp + FEC_RXIC1);
2465         writel(tx_itr, fep->hwp + FEC_TXIC2);
2466         writel(rx_itr, fep->hwp + FEC_RXIC2);
2467 }
2468
2469 static int
2470 fec_enet_get_coalesce(struct net_device *ndev, struct ethtool_coalesce *ec)
2471 {
2472         struct fec_enet_private *fep = netdev_priv(ndev);
2473
2474         if (!(fep->quirks & FEC_QUIRK_HAS_AVB))
2475                 return -EOPNOTSUPP;
2476
2477         ec->rx_coalesce_usecs = fep->rx_time_itr;
2478         ec->rx_max_coalesced_frames = fep->rx_pkts_itr;
2479
2480         ec->tx_coalesce_usecs = fep->tx_time_itr;
2481         ec->tx_max_coalesced_frames = fep->tx_pkts_itr;
2482
2483         return 0;
2484 }
2485
2486 static int
2487 fec_enet_set_coalesce(struct net_device *ndev, struct ethtool_coalesce *ec)
2488 {
2489         struct fec_enet_private *fep = netdev_priv(ndev);
2490         unsigned int cycle;
2491
2492         if (!(fep->quirks & FEC_QUIRK_HAS_AVB))
2493                 return -EOPNOTSUPP;
2494
2495         if (ec->rx_max_coalesced_frames > 255) {
2496                 pr_err("Rx coalesced frames exceed hardware limiation");
2497                 return -EINVAL;
2498         }
2499
2500         if (ec->tx_max_coalesced_frames > 255) {
2501                 pr_err("Tx coalesced frame exceed hardware limiation");
2502                 return -EINVAL;
2503         }
2504
2505         cycle = fec_enet_us_to_itr_clock(ndev, fep->rx_time_itr);
2506         if (cycle > 0xFFFF) {
2507                 pr_err("Rx coalesed usec exceeed hardware limiation");
2508                 return -EINVAL;
2509         }
2510
2511         cycle = fec_enet_us_to_itr_clock(ndev, fep->tx_time_itr);
2512         if (cycle > 0xFFFF) {
2513                 pr_err("Rx coalesed usec exceeed hardware limiation");
2514                 return -EINVAL;
2515         }
2516
2517         fep->rx_time_itr = ec->rx_coalesce_usecs;
2518         fep->rx_pkts_itr = ec->rx_max_coalesced_frames;
2519
2520         fep->tx_time_itr = ec->tx_coalesce_usecs;
2521         fep->tx_pkts_itr = ec->tx_max_coalesced_frames;
2522
2523         fec_enet_itr_coal_set(ndev);
2524
2525         return 0;
2526 }
2527
2528 static void fec_enet_itr_coal_init(struct net_device *ndev)
2529 {
2530         struct ethtool_coalesce ec;
2531
2532         ec.rx_coalesce_usecs = FEC_ITR_ICTT_DEFAULT;
2533         ec.rx_max_coalesced_frames = FEC_ITR_ICFT_DEFAULT;
2534
2535         ec.tx_coalesce_usecs = FEC_ITR_ICTT_DEFAULT;
2536         ec.tx_max_coalesced_frames = FEC_ITR_ICFT_DEFAULT;
2537
2538         fec_enet_set_coalesce(ndev, &ec);
2539 }
2540
2541 static int fec_enet_get_tunable(struct net_device *netdev,
2542                                 const struct ethtool_tunable *tuna,
2543                                 void *data)
2544 {
2545         struct fec_enet_private *fep = netdev_priv(netdev);
2546         int ret = 0;
2547
2548         switch (tuna->id) {
2549         case ETHTOOL_RX_COPYBREAK:
2550                 *(u32 *)data = fep->rx_copybreak;
2551                 break;
2552         default:
2553                 ret = -EINVAL;
2554                 break;
2555         }
2556
2557         return ret;
2558 }
2559
2560 static int fec_enet_set_tunable(struct net_device *netdev,
2561                                 const struct ethtool_tunable *tuna,
2562                                 const void *data)
2563 {
2564         struct fec_enet_private *fep = netdev_priv(netdev);
2565         int ret = 0;
2566
2567         switch (tuna->id) {
2568         case ETHTOOL_RX_COPYBREAK:
2569                 fep->rx_copybreak = *(u32 *)data;
2570                 break;
2571         default:
2572                 ret = -EINVAL;
2573                 break;
2574         }
2575
2576         return ret;
2577 }
2578
2579 static void
2580 fec_enet_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
2581 {
2582         struct fec_enet_private *fep = netdev_priv(ndev);
2583
2584         if (fep->wol_flag & FEC_WOL_HAS_MAGIC_PACKET) {
2585                 wol->supported = WAKE_MAGIC;
2586                 wol->wolopts = fep->wol_flag & FEC_WOL_FLAG_ENABLE ? WAKE_MAGIC : 0;
2587         } else {
2588                 wol->supported = wol->wolopts = 0;
2589         }
2590 }
2591
2592 static int
2593 fec_enet_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
2594 {
2595         struct fec_enet_private *fep = netdev_priv(ndev);
2596
2597         if (!(fep->wol_flag & FEC_WOL_HAS_MAGIC_PACKET))
2598                 return -EINVAL;
2599
2600         if (wol->wolopts & ~WAKE_MAGIC)
2601                 return -EINVAL;
2602
2603         device_set_wakeup_enable(&ndev->dev, wol->wolopts & WAKE_MAGIC);
2604         if (device_may_wakeup(&ndev->dev)) {
2605                 fep->wol_flag |= FEC_WOL_FLAG_ENABLE;
2606                 if (fep->irq[0] > 0)
2607                         enable_irq_wake(fep->irq[0]);
2608         } else {
2609                 fep->wol_flag &= (~FEC_WOL_FLAG_ENABLE);
2610                 if (fep->irq[0] > 0)
2611                         disable_irq_wake(fep->irq[0]);
2612         }
2613
2614         return 0;
2615 }
2616
2617 static const struct ethtool_ops fec_enet_ethtool_ops = {
2618         .get_settings           = fec_enet_get_settings,
2619         .set_settings           = fec_enet_set_settings,
2620         .get_drvinfo            = fec_enet_get_drvinfo,
2621         .get_regs_len           = fec_enet_get_regs_len,
2622         .get_regs               = fec_enet_get_regs,
2623         .nway_reset             = fec_enet_nway_reset,
2624         .get_link               = ethtool_op_get_link,
2625         .get_coalesce           = fec_enet_get_coalesce,
2626         .set_coalesce           = fec_enet_set_coalesce,
2627 #ifndef CONFIG_M5272
2628         .get_pauseparam         = fec_enet_get_pauseparam,
2629         .set_pauseparam         = fec_enet_set_pauseparam,
2630         .get_strings            = fec_enet_get_strings,
2631         .get_ethtool_stats      = fec_enet_get_ethtool_stats,
2632         .get_sset_count         = fec_enet_get_sset_count,
2633 #endif
2634         .get_ts_info            = fec_enet_get_ts_info,
2635         .get_tunable            = fec_enet_get_tunable,
2636         .set_tunable            = fec_enet_set_tunable,
2637         .get_wol                = fec_enet_get_wol,
2638         .set_wol                = fec_enet_set_wol,
2639 };
2640
2641 static int fec_enet_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
2642 {
2643         struct fec_enet_private *fep = netdev_priv(ndev);
2644         struct phy_device *phydev = fep->phy_dev;
2645
2646         if (!netif_running(ndev))
2647                 return -EINVAL;
2648
2649         if (!phydev)
2650                 return -ENODEV;
2651
2652         if (fep->bufdesc_ex) {
2653                 if (cmd == SIOCSHWTSTAMP)
2654                         return fec_ptp_set(ndev, rq);
2655                 if (cmd == SIOCGHWTSTAMP)
2656                         return fec_ptp_get(ndev, rq);
2657         }
2658
2659         return phy_mii_ioctl(phydev, rq, cmd);
2660 }
2661
2662 static void fec_enet_free_buffers(struct net_device *ndev)
2663 {
2664         struct fec_enet_private *fep = netdev_priv(ndev);
2665         unsigned int i;
2666         struct sk_buff *skb;
2667         struct bufdesc  *bdp;
2668         struct fec_enet_priv_tx_q *txq;
2669         struct fec_enet_priv_rx_q *rxq;
2670         unsigned int q;
2671
2672         for (q = 0; q < fep->num_rx_queues; q++) {
2673                 rxq = fep->rx_queue[q];
2674                 bdp = rxq->rx_bd_base;
2675                 for (i = 0; i < rxq->rx_ring_size; i++) {
2676                         skb = rxq->rx_skbuff[i];
2677                         rxq->rx_skbuff[i] = NULL;
2678                         if (skb) {
2679                                 dma_unmap_single(&fep->pdev->dev,
2680                                                  bdp->cbd_bufaddr,
2681                                                  FEC_ENET_RX_FRSIZE - fep->rx_align,
2682                                                  DMA_FROM_DEVICE);
2683                                 dev_kfree_skb(skb);
2684                         }
2685                         bdp = fec_enet_get_nextdesc(bdp, fep, q);
2686                 }
2687         }
2688
2689         for (q = 0; q < fep->num_tx_queues; q++) {
2690                 txq = fep->tx_queue[q];
2691                 bdp = txq->tx_bd_base;
2692                 for (i = 0; i < txq->tx_ring_size; i++) {
2693                         kfree(txq->tx_bounce[i]);
2694                         txq->tx_bounce[i] = NULL;
2695                         skb = txq->tx_skbuff[i];
2696                         txq->tx_skbuff[i] = NULL;
2697                         dev_kfree_skb(skb);
2698                 }
2699         }
2700 }
2701
2702 static void fec_enet_free_queue(struct net_device *ndev)
2703 {
2704         struct fec_enet_private *fep = netdev_priv(ndev);
2705         int i;
2706         struct fec_enet_priv_tx_q *txq;
2707
2708         for (i = 0; i < fep->num_tx_queues; i++)
2709                 if (fep->tx_queue[i] && fep->tx_queue[i]->tso_hdrs) {
2710                         txq = fep->tx_queue[i];
2711                         dma_free_coherent(NULL,
2712                                           txq->tx_ring_size * TSO_HEADER_SIZE,
2713                                           txq->tso_hdrs,
2714                                           txq->tso_hdrs_dma);
2715                 }
2716
2717         for (i = 0; i < fep->num_rx_queues; i++)
2718                 kfree(fep->rx_queue[i]);
2719         for (i = 0; i < fep->num_tx_queues; i++)
2720                 kfree(fep->tx_queue[i]);
2721 }
2722
2723 static int fec_enet_alloc_queue(struct net_device *ndev)
2724 {
2725         struct fec_enet_private *fep = netdev_priv(ndev);
2726         int i;
2727         int ret = 0;
2728         struct fec_enet_priv_tx_q *txq;
2729
2730         for (i = 0; i < fep->num_tx_queues; i++) {
2731                 txq = kzalloc(sizeof(*txq), GFP_KERNEL);
2732                 if (!txq) {
2733                         ret = -ENOMEM;
2734                         goto alloc_failed;
2735                 }
2736
2737                 fep->tx_queue[i] = txq;
2738                 txq->tx_ring_size = TX_RING_SIZE;
2739                 fep->total_tx_ring_size += fep->tx_queue[i]->tx_ring_size;
2740
2741                 txq->tx_stop_threshold = FEC_MAX_SKB_DESCS;
2742                 txq->tx_wake_threshold =
2743                                 (txq->tx_ring_size - txq->tx_stop_threshold) / 2;
2744
2745                 txq->tso_hdrs = dma_alloc_coherent(NULL,
2746                                         txq->tx_ring_size * TSO_HEADER_SIZE,
2747                                         &txq->tso_hdrs_dma,
2748                                         GFP_KERNEL);
2749                 if (!txq->tso_hdrs) {
2750                         ret = -ENOMEM;
2751                         goto alloc_failed;
2752                 }
2753         }
2754
2755         for (i = 0; i < fep->num_rx_queues; i++) {
2756                 fep->rx_queue[i] = kzalloc(sizeof(*fep->rx_queue[i]),
2757                                            GFP_KERNEL);
2758                 if (!fep->rx_queue[i]) {
2759                         ret = -ENOMEM;
2760                         goto alloc_failed;
2761                 }
2762
2763                 fep->rx_queue[i]->rx_ring_size = RX_RING_SIZE;
2764                 fep->total_rx_ring_size += fep->rx_queue[i]->rx_ring_size;
2765         }
2766         return ret;
2767
2768 alloc_failed:
2769         fec_enet_free_queue(ndev);
2770         return ret;
2771 }
2772
2773 static int
2774 fec_enet_alloc_rxq_buffers(struct net_device *ndev, unsigned int queue)
2775 {
2776         struct fec_enet_private *fep = netdev_priv(ndev);
2777         unsigned int i;
2778         struct sk_buff *skb;
2779         struct bufdesc  *bdp;
2780         struct fec_enet_priv_rx_q *rxq;
2781
2782         rxq = fep->rx_queue[queue];
2783         bdp = rxq->rx_bd_base;
2784         for (i = 0; i < rxq->rx_ring_size; i++) {
2785                 skb = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
2786                 if (!skb)
2787                         goto err_alloc;
2788
2789                 if (fec_enet_new_rxbdp(ndev, bdp, skb)) {
2790                         dev_kfree_skb(skb);
2791                         goto err_alloc;
2792                 }
2793
2794                 rxq->rx_skbuff[i] = skb;
2795                 bdp->cbd_sc = BD_ENET_RX_EMPTY;
2796
2797                 if (fep->bufdesc_ex) {
2798                         struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
2799                         ebdp->cbd_esc = BD_ENET_RX_INT;
2800                 }
2801
2802                 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
2803         }
2804
2805         /* Set the last buffer to wrap. */
2806         bdp = fec_enet_get_prevdesc(bdp, fep, queue);
2807         bdp->cbd_sc |= BD_SC_WRAP;
2808         return 0;
2809
2810  err_alloc:
2811         fec_enet_free_buffers(ndev);
2812         return -ENOMEM;
2813 }
2814
2815 static int
2816 fec_enet_alloc_txq_buffers(struct net_device *ndev, unsigned int queue)
2817 {
2818         struct fec_enet_private *fep = netdev_priv(ndev);
2819         unsigned int i;
2820         struct bufdesc  *bdp;
2821         struct fec_enet_priv_tx_q *txq;
2822
2823         txq = fep->tx_queue[queue];
2824         bdp = txq->tx_bd_base;
2825         for (i = 0; i < txq->tx_ring_size; i++) {
2826                 txq->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL);
2827                 if (!txq->tx_bounce[i])
2828                         goto err_alloc;
2829
2830                 bdp->cbd_sc = 0;
2831                 bdp->cbd_bufaddr = 0;
2832
2833                 if (fep->bufdesc_ex) {
2834                         struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
2835                         ebdp->cbd_esc = BD_ENET_TX_INT;
2836                 }
2837
2838                 bdp = fec_enet_get_nextdesc(bdp, fep, queue);
2839         }
2840
2841         /* Set the last buffer to wrap. */
2842         bdp = fec_enet_get_prevdesc(bdp, fep, queue);
2843         bdp->cbd_sc |= BD_SC_WRAP;
2844
2845         return 0;
2846
2847  err_alloc:
2848         fec_enet_free_buffers(ndev);
2849         return -ENOMEM;
2850 }
2851
2852 static int fec_enet_alloc_buffers(struct net_device *ndev)
2853 {
2854         struct fec_enet_private *fep = netdev_priv(ndev);
2855         unsigned int i;
2856
2857         for (i = 0; i < fep->num_rx_queues; i++)
2858                 if (fec_enet_alloc_rxq_buffers(ndev, i))
2859                         return -ENOMEM;
2860
2861         for (i = 0; i < fep->num_tx_queues; i++)
2862                 if (fec_enet_alloc_txq_buffers(ndev, i))
2863                         return -ENOMEM;
2864         return 0;
2865 }
2866
2867 static int
2868 fec_enet_open(struct net_device *ndev)
2869 {
2870         struct fec_enet_private *fep = netdev_priv(ndev);
2871         int ret;
2872
2873         ret = pm_runtime_get_sync(&fep->pdev->dev);
2874         if (ret < 0)
2875                 return ret;
2876
2877         pinctrl_pm_select_default_state(&fep->pdev->dev);
2878         ret = fec_enet_clk_enable(ndev, true);
2879         if (ret)
2880                 goto clk_enable;
2881
2882         /* I should reset the ring buffers here, but I don't yet know
2883          * a simple way to do that.
2884          */
2885
2886         ret = fec_enet_alloc_buffers(ndev);
2887         if (ret)
2888                 goto err_enet_alloc;
2889
2890         /* Init MAC prior to mii bus probe */
2891         fec_restart(ndev);
2892
2893         /* Probe and connect to PHY when open the interface */
2894         ret = fec_enet_mii_probe(ndev);
2895         if (ret)
2896                 goto err_enet_mii_probe;
2897
2898         napi_enable(&fep->napi);
2899         phy_start(fep->phy_dev);
2900         netif_tx_start_all_queues(ndev);
2901
2902         device_set_wakeup_enable(&ndev->dev, fep->wol_flag &
2903                                  FEC_WOL_FLAG_ENABLE);
2904
2905         return 0;
2906
2907 err_enet_mii_probe:
2908         fec_enet_free_buffers(ndev);
2909 err_enet_alloc:
2910         fec_enet_clk_enable(ndev, false);
2911 clk_enable:
2912         pm_runtime_mark_last_busy(&fep->pdev->dev);
2913         pm_runtime_put_autosuspend(&fep->pdev->dev);
2914         pinctrl_pm_select_sleep_state(&fep->pdev->dev);
2915         return ret;
2916 }
2917
2918 static int
2919 fec_enet_close(struct net_device *ndev)
2920 {
2921         struct fec_enet_private *fep = netdev_priv(ndev);
2922
2923         phy_stop(fep->phy_dev);
2924
2925         if (netif_device_present(ndev)) {
2926                 napi_disable(&fep->napi);
2927                 netif_tx_disable(ndev);
2928                 fec_stop(ndev);
2929         }
2930
2931         phy_disconnect(fep->phy_dev);
2932         fep->phy_dev = NULL;
2933
2934         fec_enet_clk_enable(ndev, false);
2935         pinctrl_pm_select_sleep_state(&fep->pdev->dev);
2936         pm_runtime_mark_last_busy(&fep->pdev->dev);
2937         pm_runtime_put_autosuspend(&fep->pdev->dev);
2938
2939         fec_enet_free_buffers(ndev);
2940
2941         return 0;
2942 }
2943
2944 /* Set or clear the multicast filter for this adaptor.
2945  * Skeleton taken from sunlance driver.
2946  * The CPM Ethernet implementation allows Multicast as well as individual
2947  * MAC address filtering.  Some of the drivers check to make sure it is
2948  * a group multicast address, and discard those that are not.  I guess I
2949  * will do the same for now, but just remove the test if you want
2950  * individual filtering as well (do the upper net layers want or support
2951  * this kind of feature?).
2952  */
2953
2954 #define HASH_BITS       6               /* #bits in hash */
2955 #define CRC32_POLY      0xEDB88320
2956
2957 static void set_multicast_list(struct net_device *ndev)
2958 {
2959         struct fec_enet_private *fep = netdev_priv(ndev);
2960         struct netdev_hw_addr *ha;
2961         unsigned int i, bit, data, crc, tmp;
2962         unsigned char hash;
2963
2964         if (ndev->flags & IFF_PROMISC) {
2965                 tmp = readl(fep->hwp + FEC_R_CNTRL);
2966                 tmp |= 0x8;
2967                 writel(tmp, fep->hwp + FEC_R_CNTRL);
2968                 return;
2969         }
2970
2971         tmp = readl(fep->hwp + FEC_R_CNTRL);
2972         tmp &= ~0x8;
2973         writel(tmp, fep->hwp + FEC_R_CNTRL);
2974
2975         if (ndev->flags & IFF_ALLMULTI) {
2976                 /* Catch all multicast addresses, so set the
2977                  * filter to all 1's
2978                  */
2979                 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2980                 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
2981
2982                 return;
2983         }
2984
2985         /* Clear filter and add the addresses in hash register
2986          */
2987         writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
2988         writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
2989
2990         netdev_for_each_mc_addr(ha, ndev) {
2991                 /* calculate crc32 value of mac address */
2992                 crc = 0xffffffff;
2993
2994                 for (i = 0; i < ndev->addr_len; i++) {
2995                         data = ha->addr[i];
2996                         for (bit = 0; bit < 8; bit++, data >>= 1) {
2997                                 crc = (crc >> 1) ^
2998                                 (((crc ^ data) & 1) ? CRC32_POLY : 0);
2999                         }
3000                 }
3001
3002                 /* only upper 6 bits (HASH_BITS) are used
3003                  * which point to specific bit in he hash registers
3004                  */
3005                 hash = (crc >> (32 - HASH_BITS)) & 0x3f;
3006
3007                 if (hash > 31) {
3008                         tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
3009                         tmp |= 1 << (hash - 32);
3010                         writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
3011                 } else {
3012                         tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_LOW);
3013                         tmp |= 1 << hash;
3014                         writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
3015                 }
3016         }
3017 }
3018
3019 /* Set a MAC change in hardware. */
3020 static int
3021 fec_set_mac_address(struct net_device *ndev, void *p)
3022 {
3023         struct fec_enet_private *fep = netdev_priv(ndev);
3024         struct sockaddr *addr = p;
3025
3026         if (addr) {
3027                 if (!is_valid_ether_addr(addr->sa_data))
3028                         return -EADDRNOTAVAIL;
3029                 memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len);
3030         }
3031
3032         /* Add netif status check here to avoid system hang in below case:
3033          * ifconfig ethx down; ifconfig ethx hw ether xx:xx:xx:xx:xx:xx;
3034          * After ethx down, fec all clocks are gated off and then register
3035          * access causes system hang.
3036          */
3037         if (!netif_running(ndev))
3038                 return 0;
3039
3040         writel(ndev->dev_addr[3] | (ndev->dev_addr[2] << 8) |
3041                 (ndev->dev_addr[1] << 16) | (ndev->dev_addr[0] << 24),
3042                 fep->hwp + FEC_ADDR_LOW);
3043         writel((ndev->dev_addr[5] << 16) | (ndev->dev_addr[4] << 24),
3044                 fep->hwp + FEC_ADDR_HIGH);
3045         return 0;
3046 }
3047
3048 #ifdef CONFIG_NET_POLL_CONTROLLER
3049 /**
3050  * fec_poll_controller - FEC Poll controller function
3051  * @dev: The FEC network adapter
3052  *
3053  * Polled functionality used by netconsole and others in non interrupt mode
3054  *
3055  */
3056 static void fec_poll_controller(struct net_device *dev)
3057 {
3058         int i;
3059         struct fec_enet_private *fep = netdev_priv(dev);
3060
3061         for (i = 0; i < FEC_IRQ_NUM; i++) {
3062                 if (fep->irq[i] > 0) {
3063                         disable_irq(fep->irq[i]);
3064                         fec_enet_interrupt(fep->irq[i], dev);
3065                         enable_irq(fep->irq[i]);
3066                 }
3067         }
3068 }
3069 #endif
3070
3071 static inline void fec_enet_set_netdev_features(struct net_device *netdev,
3072         netdev_features_t features)
3073 {
3074         struct fec_enet_private *fep = netdev_priv(netdev);
3075         netdev_features_t changed = features ^ netdev->features;
3076
3077         netdev->features = features;
3078
3079         /* Receive checksum has been changed */
3080         if (changed & NETIF_F_RXCSUM) {
3081                 if (features & NETIF_F_RXCSUM)
3082                         fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
3083                 else
3084                         fep->csum_flags &= ~FLAG_RX_CSUM_ENABLED;
3085         }
3086 }
3087
3088 static int fec_set_features(struct net_device *netdev,
3089         netdev_features_t features)
3090 {
3091         struct fec_enet_private *fep = netdev_priv(netdev);
3092         netdev_features_t changed = features ^ netdev->features;
3093
3094         if (netif_running(netdev) && changed & NETIF_F_RXCSUM) {
3095                 napi_disable(&fep->napi);
3096                 netif_tx_lock_bh(netdev);
3097                 fec_stop(netdev);
3098                 fec_enet_set_netdev_features(netdev, features);
3099                 fec_restart(netdev);
3100                 netif_tx_wake_all_queues(netdev);
3101                 netif_tx_unlock_bh(netdev);
3102                 napi_enable(&fep->napi);
3103         } else {
3104                 fec_enet_set_netdev_features(netdev, features);
3105         }
3106
3107         return 0;
3108 }
3109
3110 static const struct net_device_ops fec_netdev_ops = {
3111         .ndo_open               = fec_enet_open,
3112         .ndo_stop               = fec_enet_close,
3113         .ndo_start_xmit         = fec_enet_start_xmit,
3114         .ndo_set_rx_mode        = set_multicast_list,
3115         .ndo_change_mtu         = eth_change_mtu,
3116         .ndo_validate_addr      = eth_validate_addr,
3117         .ndo_tx_timeout         = fec_timeout,
3118         .ndo_set_mac_address    = fec_set_mac_address,
3119         .ndo_do_ioctl           = fec_enet_ioctl,
3120 #ifdef CONFIG_NET_POLL_CONTROLLER
3121         .ndo_poll_controller    = fec_poll_controller,
3122 #endif
3123         .ndo_set_features       = fec_set_features,
3124 };
3125
3126  /*
3127   * XXX:  We need to clean up on failure exits here.
3128   *
3129   */
3130 static int fec_enet_init(struct net_device *ndev)
3131 {
3132         struct fec_enet_private *fep = netdev_priv(ndev);
3133         struct fec_enet_priv_tx_q *txq;
3134         struct fec_enet_priv_rx_q *rxq;
3135         struct bufdesc *cbd_base;
3136         dma_addr_t bd_dma;
3137         int bd_size;
3138         unsigned int i;
3139
3140 #if defined(CONFIG_ARM)
3141         fep->rx_align = 0xf;
3142         fep->tx_align = 0xf;
3143 #else
3144         fep->rx_align = 0x3;
3145         fep->tx_align = 0x3;
3146 #endif
3147
3148         fec_enet_alloc_queue(ndev);
3149
3150         if (fep->bufdesc_ex)
3151                 fep->bufdesc_size = sizeof(struct bufdesc_ex);
3152         else
3153                 fep->bufdesc_size = sizeof(struct bufdesc);
3154         bd_size = (fep->total_tx_ring_size + fep->total_rx_ring_size) *
3155                         fep->bufdesc_size;
3156
3157         /* Allocate memory for buffer descriptors. */
3158         cbd_base = dmam_alloc_coherent(&fep->pdev->dev, bd_size, &bd_dma,
3159                                        GFP_KERNEL);
3160         if (!cbd_base) {
3161                 return -ENOMEM;
3162         }
3163
3164         memset(cbd_base, 0, bd_size);
3165
3166         /* Get the Ethernet address */
3167         fec_get_mac(ndev);
3168         /* make sure MAC we just acquired is programmed into the hw */
3169         fec_set_mac_address(ndev, NULL);
3170
3171         /* Set receive and transmit descriptor base. */
3172         for (i = 0; i < fep->num_rx_queues; i++) {
3173                 rxq = fep->rx_queue[i];
3174                 rxq->index = i;
3175                 rxq->rx_bd_base = (struct bufdesc *)cbd_base;
3176                 rxq->bd_dma = bd_dma;
3177                 if (fep->bufdesc_ex) {
3178                         bd_dma += sizeof(struct bufdesc_ex) * rxq->rx_ring_size;
3179                         cbd_base = (struct bufdesc *)
3180                                 (((struct bufdesc_ex *)cbd_base) + rxq->rx_ring_size);
3181                 } else {
3182                         bd_dma += sizeof(struct bufdesc) * rxq->rx_ring_size;
3183                         cbd_base += rxq->rx_ring_size;
3184                 }
3185         }
3186
3187         for (i = 0; i < fep->num_tx_queues; i++) {
3188                 txq = fep->tx_queue[i];
3189                 txq->index = i;
3190                 txq->tx_bd_base = (struct bufdesc *)cbd_base;
3191                 txq->bd_dma = bd_dma;
3192                 if (fep->bufdesc_ex) {
3193                         bd_dma += sizeof(struct bufdesc_ex) * txq->tx_ring_size;
3194                         cbd_base = (struct bufdesc *)
3195                          (((struct bufdesc_ex *)cbd_base) + txq->tx_ring_size);
3196                 } else {
3197                         bd_dma += sizeof(struct bufdesc) * txq->tx_ring_size;
3198                         cbd_base += txq->tx_ring_size;
3199                 }
3200         }
3201
3202
3203         /* The FEC Ethernet specific entries in the device structure */
3204         ndev->watchdog_timeo = TX_TIMEOUT;
3205         ndev->netdev_ops = &fec_netdev_ops;
3206         ndev->ethtool_ops = &fec_enet_ethtool_ops;
3207
3208         writel(FEC_RX_DISABLED_IMASK, fep->hwp + FEC_IMASK);
3209         netif_napi_add(ndev, &fep->napi, fec_enet_rx_napi, NAPI_POLL_WEIGHT);
3210
3211         if (fep->quirks & FEC_QUIRK_HAS_VLAN)
3212                 /* enable hw VLAN support */
3213                 ndev->features |= NETIF_F_HW_VLAN_CTAG_RX;
3214
3215         if (fep->quirks & FEC_QUIRK_HAS_CSUM) {
3216                 ndev->gso_max_segs = FEC_MAX_TSO_SEGS;
3217
3218                 /* enable hw accelerator */
3219                 ndev->features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
3220                                 | NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_TSO);
3221                 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
3222         }
3223
3224         if (fep->quirks & FEC_QUIRK_HAS_AVB) {
3225                 fep->tx_align = 0;
3226                 fep->rx_align = 0x3f;
3227         }
3228
3229         ndev->hw_features = ndev->features;
3230
3231         fec_restart(ndev);
3232
3233         return 0;
3234 }
3235
3236 #ifdef CONFIG_OF
3237 static void fec_reset_phy(struct platform_device *pdev)
3238 {
3239         struct net_device *ndev = platform_get_drvdata(pdev);
3240         struct fec_enet_private *fep = netdev_priv(ndev);
3241
3242         if (!gpio_is_valid(fep->phy_reset_gpio))
3243                 return;
3244
3245         gpio_set_value_cansleep(fep->phy_reset_gpio, 0);
3246         msleep(fep->phy_reset_duration);
3247         gpio_set_value_cansleep(fep->phy_reset_gpio, 1);
3248 }
3249
3250 static int fec_get_reset_gpio(struct platform_device *pdev)
3251 {
3252         int err, phy_reset;
3253         int msec = 1;
3254         struct device_node *np = pdev->dev.of_node;
3255         struct net_device *ndev = platform_get_drvdata(pdev);
3256         struct fec_enet_private *fep = netdev_priv(ndev);
3257
3258         phy_reset = of_get_named_gpio(np, "phy-reset-gpios", 0);
3259         if (!gpio_is_valid(phy_reset))
3260                 return phy_reset;
3261
3262         err = devm_gpio_request_one(&pdev->dev, phy_reset,
3263                                     GPIOF_OUT_INIT_LOW, "phy-reset");
3264         if (err) {
3265                 dev_err(&pdev->dev, "failed to get phy-reset-gpios: %d\n", err);
3266                 return err;
3267         }
3268
3269         of_property_read_u32(np, "phy-reset-duration", &msec);
3270         /* A sane reset duration should not be longer than 1s */
3271         if (msec > 1000)
3272                 msec = 1;
3273         fep->phy_reset_duration = msec;
3274
3275         return phy_reset;
3276 }
3277 #else /* CONFIG_OF */
3278 static void fec_reset_phy(struct platform_device *pdev)
3279 {
3280         /*
3281          * In case of platform probe, the reset has been done
3282          * by machine code.
3283          */
3284 }
3285
3286 static inline int fec_get_reset_gpio(struct platform_device *pdev)
3287 {
3288         return -EINVAL;
3289 }
3290 #endif /* CONFIG_OF */
3291
3292 static void
3293 fec_enet_get_queue_num(struct platform_device *pdev, int *num_tx, int *num_rx)
3294 {
3295         struct device_node *np = pdev->dev.of_node;
3296         int err;
3297
3298         *num_tx = *num_rx = 1;
3299
3300         if (!np || !of_device_is_available(np))
3301                 return;
3302
3303         /* parse the num of tx and rx queues */
3304         err = of_property_read_u32(np, "fsl,num-tx-queues", num_tx);
3305         if (err)
3306                 *num_tx = 1;
3307
3308         err = of_property_read_u32(np, "fsl,num-rx-queues", num_rx);
3309         if (err)
3310                 *num_rx = 1;
3311
3312         if (*num_tx < 1 || *num_tx > FEC_ENET_MAX_TX_QS) {
3313                 dev_warn(&pdev->dev, "Invalid num_tx(=%d), fall back to 1\n",
3314                          *num_tx);
3315                 *num_tx = 1;
3316                 return;
3317         }
3318
3319         if (*num_rx < 1 || *num_rx > FEC_ENET_MAX_RX_QS) {
3320                 dev_warn(&pdev->dev, "Invalid num_rx(=%d), fall back to 1\n",
3321                          *num_rx);
3322                 *num_rx = 1;
3323                 return;
3324         }
3325
3326 }
3327
3328 static int
3329 fec_probe(struct platform_device *pdev)
3330 {
3331         struct fec_enet_private *fep;
3332         struct fec_platform_data *pdata;
3333         struct net_device *ndev;
3334         int i, irq, ret = 0;
3335         struct resource *r;
3336         const struct of_device_id *of_id;
3337         static int dev_id;
3338         struct device_node *np = pdev->dev.of_node, *phy_node;
3339         int num_tx_qs;
3340         int num_rx_qs;
3341
3342         fec_enet_get_queue_num(pdev, &num_tx_qs, &num_rx_qs);
3343
3344         /* Init network device */
3345         ndev = alloc_etherdev_mqs(sizeof(struct fec_enet_private),
3346                                   num_tx_qs, num_rx_qs);
3347         if (!ndev)
3348                 return -ENOMEM;
3349
3350         SET_NETDEV_DEV(ndev, &pdev->dev);
3351
3352         /* setup board info structure */
3353         fep = netdev_priv(ndev);
3354
3355         of_id = of_match_device(fec_dt_ids, &pdev->dev);
3356         if (of_id)
3357                 pdev->id_entry = of_id->data;
3358         fep->quirks = pdev->id_entry->driver_data;
3359
3360         fep->netdev = ndev;
3361         fep->num_rx_queues = num_rx_qs;
3362         fep->num_tx_queues = num_tx_qs;
3363
3364 #if !defined(CONFIG_M5272)
3365         /* default enable pause frame auto negotiation */
3366         if (fep->quirks & FEC_QUIRK_HAS_GBIT)
3367                 fep->pause_flag |= FEC_PAUSE_FLAG_AUTONEG;
3368 #endif
3369
3370         /* Select default pin state */
3371         pinctrl_pm_select_default_state(&pdev->dev);
3372
3373         r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3374         fep->hwp = devm_ioremap_resource(&pdev->dev, r);
3375         if (IS_ERR(fep->hwp)) {
3376                 ret = PTR_ERR(fep->hwp);
3377                 goto failed_ioremap;
3378         }
3379
3380         fep->pdev = pdev;
3381         fep->dev_id = dev_id++;
3382
3383         platform_set_drvdata(pdev, ndev);
3384
3385         ret = fec_get_reset_gpio(pdev);
3386         if (ret == -EPROBE_DEFER)
3387                 goto gpio_defer;
3388         fep->phy_reset_gpio = ret;
3389
3390         if (of_get_property(np, "fsl,magic-packet", NULL))
3391                 fep->wol_flag |= FEC_WOL_HAS_MAGIC_PACKET;
3392
3393         phy_node = of_parse_phandle(np, "phy-handle", 0);
3394         if (!phy_node && of_phy_is_fixed_link(np)) {
3395                 ret = of_phy_register_fixed_link(np);
3396                 if (ret < 0) {
3397                         dev_err(&pdev->dev,
3398                                 "broken fixed-link specification\n");
3399                         goto failed_phy;
3400                 }
3401                 phy_node = of_node_get(np);
3402         }
3403         fep->phy_node = phy_node;
3404
3405         ret = of_get_phy_mode(pdev->dev.of_node);
3406         if (ret < 0) {
3407                 pdata = dev_get_platdata(&pdev->dev);
3408                 if (pdata)
3409                         fep->phy_interface = pdata->phy;
3410                 else
3411                         fep->phy_interface = PHY_INTERFACE_MODE_MII;
3412         } else {
3413                 fep->phy_interface = ret;
3414         }
3415
3416         fep->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
3417         if (IS_ERR(fep->clk_ipg)) {
3418                 ret = PTR_ERR(fep->clk_ipg);
3419                 goto failed_clk;
3420         }
3421
3422         fep->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
3423         if (IS_ERR(fep->clk_ahb)) {
3424                 ret = PTR_ERR(fep->clk_ahb);
3425                 goto failed_clk;
3426         }
3427
3428         fep->itr_clk_rate = clk_get_rate(fep->clk_ahb);
3429
3430         /* enet_out is optional, depends on board */
3431         fep->clk_enet_out = devm_clk_get(&pdev->dev, "enet_out");
3432         if (IS_ERR(fep->clk_enet_out))
3433                 fep->clk_enet_out = NULL;
3434
3435         fep->ptp_clk_on = false;
3436         mutex_init(&fep->ptp_clk_mutex);
3437
3438         /* clk_ref is optional, depends on board */
3439         fep->clk_ref = devm_clk_get(&pdev->dev, "enet_clk_ref");
3440         if (IS_ERR(fep->clk_ref))
3441                 fep->clk_ref = NULL;
3442
3443         fep->bufdesc_ex = fep->quirks & FEC_QUIRK_HAS_BUFDESC_EX;
3444         fep->clk_ptp = devm_clk_get(&pdev->dev, "ptp");
3445         if (IS_ERR(fep->clk_ptp)) {
3446                 fep->clk_ptp = NULL;
3447                 fep->bufdesc_ex = false;
3448         }
3449
3450         ret = fec_enet_clk_enable(ndev, true);
3451         if (ret)
3452                 goto failed_clk;
3453
3454         ret = clk_prepare_enable(fep->clk_ipg);
3455         if (ret)
3456                 goto failed_clk_ipg;
3457
3458         fep->reg_phy = devm_regulator_get(&pdev->dev, "phy");
3459         if (!IS_ERR(fep->reg_phy)) {
3460                 ret = regulator_enable(fep->reg_phy);
3461                 if (ret) {
3462                         dev_err(&pdev->dev,
3463                                 "Failed to enable phy regulator: %d\n", ret);
3464                         goto failed_regulator;
3465                 }
3466         } else {
3467                 fep->reg_phy = NULL;
3468         }
3469
3470         pm_runtime_set_autosuspend_delay(&pdev->dev, FEC_MDIO_PM_TIMEOUT);
3471         pm_runtime_use_autosuspend(&pdev->dev);
3472         pm_runtime_get_noresume(&pdev->dev);
3473         pm_runtime_set_active(&pdev->dev);
3474         pm_runtime_enable(&pdev->dev);
3475
3476         fec_reset_phy(pdev);
3477
3478         if (fep->bufdesc_ex)
3479                 fec_ptp_init(pdev);
3480
3481         ret = fec_enet_init(ndev);
3482         if (ret)
3483                 goto failed_init;
3484
3485         for (i = 0; i < FEC_IRQ_NUM; i++) {
3486                 irq = platform_get_irq(pdev, i);
3487                 if (irq < 0) {
3488                         if (i)
3489                                 break;
3490                         ret = irq;
3491                         goto failed_irq;
3492                 }
3493                 ret = devm_request_irq(&pdev->dev, irq, fec_enet_interrupt,
3494                                        0, pdev->name, ndev);
3495                 if (ret)
3496                         goto failed_irq;
3497
3498                 fep->irq[i] = irq;
3499         }
3500
3501         init_completion(&fep->mdio_done);
3502         ret = fec_enet_mii_init(pdev);
3503         if (ret)
3504                 goto failed_mii_init;
3505
3506         /* Carrier starts down, phylib will bring it up */
3507         netif_carrier_off(ndev);
3508         fec_enet_clk_enable(ndev, false);
3509         pinctrl_pm_select_sleep_state(&pdev->dev);
3510
3511         ret = register_netdev(ndev);
3512         if (ret)
3513                 goto failed_register;
3514
3515         device_init_wakeup(&ndev->dev, fep->wol_flag &
3516                            FEC_WOL_HAS_MAGIC_PACKET);
3517
3518         if (fep->bufdesc_ex && fep->ptp_clock)
3519                 netdev_info(ndev, "registered PHC device %d\n", fep->dev_id);
3520
3521         fep->rx_copybreak = COPYBREAK_DEFAULT;
3522         INIT_WORK(&fep->tx_timeout_work, fec_enet_timeout_work);
3523
3524         pm_runtime_mark_last_busy(&pdev->dev);
3525         pm_runtime_put_autosuspend(&pdev->dev);
3526
3527         return 0;
3528
3529 failed_register:
3530         fec_enet_mii_remove(fep);
3531 failed_mii_init:
3532 failed_irq:
3533 failed_init:
3534         fec_ptp_stop(pdev);
3535         if (fep->reg_phy)
3536                 regulator_disable(fep->reg_phy);
3537 failed_regulator:
3538         clk_disable_unprepare(fep->clk_ipg);
3539 failed_clk_ipg:
3540         fec_enet_clk_enable(ndev, false);
3541 failed_clk:
3542 failed_phy:
3543         of_node_put(phy_node);
3544 gpio_defer:
3545 failed_ioremap:
3546         free_netdev(ndev);
3547
3548         return ret;
3549 }
3550
3551 static int
3552 fec_drv_remove(struct platform_device *pdev)
3553 {
3554         struct net_device *ndev = platform_get_drvdata(pdev);
3555         struct fec_enet_private *fep = netdev_priv(ndev);
3556
3557         cancel_work_sync(&fep->tx_timeout_work);
3558         fec_ptp_stop(pdev);
3559         unregister_netdev(ndev);
3560         fec_enet_mii_remove(fep);
3561         if (fep->reg_phy)
3562                 regulator_disable(fep->reg_phy);
3563         of_node_put(fep->phy_node);
3564         free_netdev(ndev);
3565
3566         return 0;
3567 }
3568
3569 static int __maybe_unused fec_suspend(struct device *dev)
3570 {
3571         struct net_device *ndev = dev_get_drvdata(dev);
3572         struct fec_enet_private *fep = netdev_priv(ndev);
3573
3574         rtnl_lock();
3575         if (netif_running(ndev)) {
3576                 if (fep->wol_flag & FEC_WOL_FLAG_ENABLE)
3577                         fep->wol_flag |= FEC_WOL_FLAG_SLEEP_ON;
3578                 phy_stop(fep->phy_dev);
3579                 napi_disable(&fep->napi);
3580                 netif_tx_lock_bh(ndev);
3581                 netif_device_detach(ndev);
3582                 netif_tx_unlock_bh(ndev);
3583                 fec_stop(ndev);
3584                 fec_enet_clk_enable(ndev, false);
3585                 if (!(fep->wol_flag & FEC_WOL_FLAG_ENABLE))
3586                         pinctrl_pm_select_sleep_state(&fep->pdev->dev);
3587         }
3588         rtnl_unlock();
3589
3590         if (fep->reg_phy && !(fep->wol_flag & FEC_WOL_FLAG_ENABLE))
3591                 regulator_disable(fep->reg_phy);
3592
3593         /* SOC supply clock to phy, when clock is disabled, phy link down
3594          * SOC control phy regulator, when regulator is disabled, phy link down
3595          */
3596         if (fep->clk_enet_out || fep->reg_phy)
3597                 fep->link = 0;
3598
3599         return 0;
3600 }
3601
3602 static int __maybe_unused fec_resume(struct device *dev)
3603 {
3604         struct net_device *ndev = dev_get_drvdata(dev);
3605         struct fec_enet_private *fep = netdev_priv(ndev);
3606         struct fec_platform_data *pdata = fep->pdev->dev.platform_data;
3607         int ret;
3608         int val;
3609
3610         if (fep->reg_phy && !(fep->wol_flag & FEC_WOL_FLAG_ENABLE)) {
3611                 ret = regulator_enable(fep->reg_phy);
3612                 if (ret)
3613                         return ret;
3614         }
3615
3616         rtnl_lock();
3617         if (netif_running(ndev)) {
3618                 ret = fec_enet_clk_enable(ndev, true);
3619                 if (ret) {
3620                         rtnl_unlock();
3621                         goto failed_clk;
3622                 }
3623                 if (fep->wol_flag & FEC_WOL_FLAG_ENABLE) {
3624                         if (pdata && pdata->sleep_mode_enable)
3625                                 pdata->sleep_mode_enable(false);
3626                         val = readl(fep->hwp + FEC_ECNTRL);
3627                         val &= ~(FEC_ECR_MAGICEN | FEC_ECR_SLEEP);
3628                         writel(val, fep->hwp + FEC_ECNTRL);
3629                         fep->wol_flag &= ~FEC_WOL_FLAG_SLEEP_ON;
3630                 } else {
3631                         pinctrl_pm_select_default_state(&fep->pdev->dev);
3632                 }
3633                 fec_restart(ndev);
3634                 netif_tx_lock_bh(ndev);
3635                 netif_device_attach(ndev);
3636                 netif_tx_unlock_bh(ndev);
3637                 napi_enable(&fep->napi);
3638                 phy_start(fep->phy_dev);
3639         }
3640         rtnl_unlock();
3641
3642         return 0;
3643
3644 failed_clk:
3645         if (fep->reg_phy)
3646                 regulator_disable(fep->reg_phy);
3647         return ret;
3648 }
3649
3650 static int __maybe_unused fec_runtime_suspend(struct device *dev)
3651 {
3652         struct net_device *ndev = dev_get_drvdata(dev);
3653         struct fec_enet_private *fep = netdev_priv(ndev);
3654
3655         clk_disable_unprepare(fep->clk_ipg);
3656
3657         return 0;
3658 }
3659
3660 static int __maybe_unused fec_runtime_resume(struct device *dev)
3661 {
3662         struct net_device *ndev = dev_get_drvdata(dev);
3663         struct fec_enet_private *fep = netdev_priv(ndev);
3664
3665         return clk_prepare_enable(fep->clk_ipg);
3666 }
3667
3668 static const struct dev_pm_ops fec_pm_ops = {
3669         SET_SYSTEM_SLEEP_PM_OPS(fec_suspend, fec_resume)
3670         SET_RUNTIME_PM_OPS(fec_runtime_suspend, fec_runtime_resume, NULL)
3671 };
3672
3673 static struct platform_driver fec_driver = {
3674         .driver = {
3675                 .name   = DRIVER_NAME,
3676                 .pm     = &fec_pm_ops,
3677                 .of_match_table = fec_dt_ids,
3678         },
3679         .id_table = fec_devtype,
3680         .probe  = fec_probe,
3681         .remove = fec_drv_remove,
3682 };
3683
3684 module_platform_driver(fec_driver);
3685
3686 MODULE_ALIAS("platform:"DRIVER_NAME);
3687 MODULE_LICENSE("GPL");