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