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