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