4c6eb640d4986463905476c61853e4797f3b641f
[oweals/u-boot.git] / drivers / net / fec_mxc.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2009 Ilya Yanok, Emcraft Systems Ltd <yanok@emcraft.com>
4  * (C) Copyright 2008,2009 Eric Jarrige <eric.jarrige@armadeus.org>
5  * (C) Copyright 2008 Armadeus Systems nc
6  * (C) Copyright 2007 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de>
7  * (C) Copyright 2007 Pengutronix, Juergen Beisert <j.beisert@pengutronix.de>
8  */
9
10 #include <common.h>
11 #include <cpu_func.h>
12 #include <dm.h>
13 #include <env.h>
14 #include <log.h>
15 #include <malloc.h>
16 #include <memalign.h>
17 #include <miiphy.h>
18 #include <net.h>
19 #include <netdev.h>
20 #include <asm/cache.h>
21 #include <power/regulator.h>
22
23 #include <asm/io.h>
24 #include <linux/errno.h>
25 #include <linux/compiler.h>
26
27 #include <asm/arch/clock.h>
28 #include <asm/arch/imx-regs.h>
29 #include <asm/mach-imx/sys_proto.h>
30 #include <asm-generic/gpio.h>
31
32 #include "fec_mxc.h"
33 #include <eth_phy.h>
34
35 DECLARE_GLOBAL_DATA_PTR;
36
37 /*
38  * Timeout the transfer after 5 mS. This is usually a bit more, since
39  * the code in the tightloops this timeout is used in adds some overhead.
40  */
41 #define FEC_XFER_TIMEOUT        5000
42
43 /*
44  * The standard 32-byte DMA alignment does not work on mx6solox, which requires
45  * 64-byte alignment in the DMA RX FEC buffer.
46  * Introduce the FEC_DMA_RX_MINALIGN which can cover mx6solox needs and also
47  * satisfies the alignment on other SoCs (32-bytes)
48  */
49 #define FEC_DMA_RX_MINALIGN     64
50
51 #ifndef CONFIG_MII
52 #error "CONFIG_MII has to be defined!"
53 #endif
54
55 #ifndef CONFIG_FEC_XCV_TYPE
56 #define CONFIG_FEC_XCV_TYPE MII100
57 #endif
58
59 /*
60  * The i.MX28 operates with packets in big endian. We need to swap them before
61  * sending and after receiving.
62  */
63 #ifdef CONFIG_MX28
64 #define CONFIG_FEC_MXC_SWAP_PACKET
65 #endif
66
67 #define RXDESC_PER_CACHELINE (ARCH_DMA_MINALIGN/sizeof(struct fec_bd))
68
69 /* Check various alignment issues at compile time */
70 #if ((ARCH_DMA_MINALIGN < 16) || (ARCH_DMA_MINALIGN % 16 != 0))
71 #error "ARCH_DMA_MINALIGN must be multiple of 16!"
72 #endif
73
74 #if ((PKTALIGN < ARCH_DMA_MINALIGN) || \
75         (PKTALIGN % ARCH_DMA_MINALIGN != 0))
76 #error "PKTALIGN must be multiple of ARCH_DMA_MINALIGN!"
77 #endif
78
79 #undef DEBUG
80
81 #ifdef CONFIG_FEC_MXC_SWAP_PACKET
82 static void swap_packet(uint32_t *packet, int length)
83 {
84         int i;
85
86         for (i = 0; i < DIV_ROUND_UP(length, 4); i++)
87                 packet[i] = __swab32(packet[i]);
88 }
89 #endif
90
91 /* MII-interface related functions */
92 static int fec_mdio_read(struct ethernet_regs *eth, uint8_t phyaddr,
93                 uint8_t regaddr)
94 {
95         uint32_t reg;           /* convenient holder for the PHY register */
96         uint32_t phy;           /* convenient holder for the PHY */
97         uint32_t start;
98         int val;
99
100         /*
101          * reading from any PHY's register is done by properly
102          * programming the FEC's MII data register.
103          */
104         writel(FEC_IEVENT_MII, &eth->ievent);
105         reg = regaddr << FEC_MII_DATA_RA_SHIFT;
106         phy = phyaddr << FEC_MII_DATA_PA_SHIFT;
107
108         writel(FEC_MII_DATA_ST | FEC_MII_DATA_OP_RD | FEC_MII_DATA_TA |
109                         phy | reg, &eth->mii_data);
110
111         /* wait for the related interrupt */
112         start = get_timer(0);
113         while (!(readl(&eth->ievent) & FEC_IEVENT_MII)) {
114                 if (get_timer(start) > (CONFIG_SYS_HZ / 1000)) {
115                         printf("Read MDIO failed...\n");
116                         return -1;
117                 }
118         }
119
120         /* clear mii interrupt bit */
121         writel(FEC_IEVENT_MII, &eth->ievent);
122
123         /* it's now safe to read the PHY's register */
124         val = (unsigned short)readl(&eth->mii_data);
125         debug("%s: phy: %02x reg:%02x val:%#x\n", __func__, phyaddr,
126               regaddr, val);
127         return val;
128 }
129
130 #ifndef imx_get_fecclk
131 u32 __weak imx_get_fecclk(void)
132 {
133         return 0;
134 }
135 #endif
136
137 static int fec_get_clk_rate(void *udev, int idx)
138 {
139         struct fec_priv *fec;
140         struct udevice *dev;
141         int ret;
142
143         if (IS_ENABLED(CONFIG_IMX8) ||
144             CONFIG_IS_ENABLED(CLK_CCF)) {
145                 dev = udev;
146                 if (!dev) {
147                         ret = uclass_get_device(UCLASS_ETH, idx, &dev);
148                         if (ret < 0) {
149                                 debug("Can't get FEC udev: %d\n", ret);
150                                 return ret;
151                         }
152                 }
153
154                 fec = dev_get_priv(dev);
155                 if (fec)
156                         return fec->clk_rate;
157
158                 return -EINVAL;
159         } else {
160                 return imx_get_fecclk();
161         }
162 }
163
164 static void fec_mii_setspeed(struct ethernet_regs *eth)
165 {
166         /*
167          * Set MII_SPEED = (1/(mii_speed * 2)) * System Clock
168          * and do not drop the Preamble.
169          *
170          * The i.MX28 and i.MX6 types have another field in the MSCR (aka
171          * MII_SPEED) register that defines the MDIO output hold time. Earlier
172          * versions are RAZ there, so just ignore the difference and write the
173          * register always.
174          * The minimal hold time according to IEE802.3 (clause 22) is 10 ns.
175          * HOLDTIME + 1 is the number of clk cycles the fec is holding the
176          * output.
177          * The HOLDTIME bitfield takes values between 0 and 7 (inclusive).
178          * Given that ceil(clkrate / 5000000) <= 64, the calculation for
179          * holdtime cannot result in a value greater than 3.
180          */
181         u32 pclk;
182         u32 speed;
183         u32 hold;
184         int ret;
185
186         ret = fec_get_clk_rate(NULL, 0);
187         if (ret < 0) {
188                 printf("Can't find FEC0 clk rate: %d\n", ret);
189                 return;
190         }
191         pclk = ret;
192         speed = DIV_ROUND_UP(pclk, 5000000);
193         hold = DIV_ROUND_UP(pclk, 100000000) - 1;
194
195 #ifdef FEC_QUIRK_ENET_MAC
196         speed--;
197 #endif
198         writel(speed << 1 | hold << 8, &eth->mii_speed);
199         debug("%s: mii_speed %08x\n", __func__, readl(&eth->mii_speed));
200 }
201
202 static int fec_mdio_write(struct ethernet_regs *eth, uint8_t phyaddr,
203                 uint8_t regaddr, uint16_t data)
204 {
205         uint32_t reg;           /* convenient holder for the PHY register */
206         uint32_t phy;           /* convenient holder for the PHY */
207         uint32_t start;
208
209         reg = regaddr << FEC_MII_DATA_RA_SHIFT;
210         phy = phyaddr << FEC_MII_DATA_PA_SHIFT;
211
212         writel(FEC_MII_DATA_ST | FEC_MII_DATA_OP_WR |
213                 FEC_MII_DATA_TA | phy | reg | data, &eth->mii_data);
214
215         /* wait for the MII interrupt */
216         start = get_timer(0);
217         while (!(readl(&eth->ievent) & FEC_IEVENT_MII)) {
218                 if (get_timer(start) > (CONFIG_SYS_HZ / 1000)) {
219                         printf("Write MDIO failed...\n");
220                         return -1;
221                 }
222         }
223
224         /* clear MII interrupt bit */
225         writel(FEC_IEVENT_MII, &eth->ievent);
226         debug("%s: phy: %02x reg:%02x val:%#x\n", __func__, phyaddr,
227               regaddr, data);
228
229         return 0;
230 }
231
232 static int fec_phy_read(struct mii_dev *bus, int phyaddr, int dev_addr,
233                         int regaddr)
234 {
235         return fec_mdio_read(bus->priv, phyaddr, regaddr);
236 }
237
238 static int fec_phy_write(struct mii_dev *bus, int phyaddr, int dev_addr,
239                          int regaddr, u16 data)
240 {
241         return fec_mdio_write(bus->priv, phyaddr, regaddr, data);
242 }
243
244 #ifndef CONFIG_PHYLIB
245 static int miiphy_restart_aneg(struct eth_device *dev)
246 {
247         int ret = 0;
248 #if !defined(CONFIG_FEC_MXC_NO_ANEG)
249         struct fec_priv *fec = (struct fec_priv *)dev->priv;
250         struct ethernet_regs *eth = fec->bus->priv;
251
252         /*
253          * Wake up from sleep if necessary
254          * Reset PHY, then delay 300ns
255          */
256 #ifdef CONFIG_MX27
257         fec_mdio_write(eth, fec->phy_id, MII_DCOUNTER, 0x00FF);
258 #endif
259         fec_mdio_write(eth, fec->phy_id, MII_BMCR, BMCR_RESET);
260         udelay(1000);
261
262         /* Set the auto-negotiation advertisement register bits */
263         fec_mdio_write(eth, fec->phy_id, MII_ADVERTISE,
264                        LPA_100FULL | LPA_100HALF | LPA_10FULL |
265                        LPA_10HALF | PHY_ANLPAR_PSB_802_3);
266         fec_mdio_write(eth, fec->phy_id, MII_BMCR,
267                        BMCR_ANENABLE | BMCR_ANRESTART);
268
269         if (fec->mii_postcall)
270                 ret = fec->mii_postcall(fec->phy_id);
271
272 #endif
273         return ret;
274 }
275
276 #ifndef CONFIG_FEC_FIXED_SPEED
277 static int miiphy_wait_aneg(struct eth_device *dev)
278 {
279         uint32_t start;
280         int status;
281         struct fec_priv *fec = (struct fec_priv *)dev->priv;
282         struct ethernet_regs *eth = fec->bus->priv;
283
284         /* Wait for AN completion */
285         start = get_timer(0);
286         do {
287                 if (get_timer(start) > (CONFIG_SYS_HZ * 5)) {
288                         printf("%s: Autonegotiation timeout\n", dev->name);
289                         return -1;
290                 }
291
292                 status = fec_mdio_read(eth, fec->phy_id, MII_BMSR);
293                 if (status < 0) {
294                         printf("%s: Autonegotiation failed. status: %d\n",
295                                dev->name, status);
296                         return -1;
297                 }
298         } while (!(status & BMSR_LSTATUS));
299
300         return 0;
301 }
302 #endif /* CONFIG_FEC_FIXED_SPEED */
303 #endif
304
305 static int fec_rx_task_enable(struct fec_priv *fec)
306 {
307         writel(FEC_R_DES_ACTIVE_RDAR, &fec->eth->r_des_active);
308         return 0;
309 }
310
311 static int fec_rx_task_disable(struct fec_priv *fec)
312 {
313         return 0;
314 }
315
316 static int fec_tx_task_enable(struct fec_priv *fec)
317 {
318         writel(FEC_X_DES_ACTIVE_TDAR, &fec->eth->x_des_active);
319         return 0;
320 }
321
322 static int fec_tx_task_disable(struct fec_priv *fec)
323 {
324         return 0;
325 }
326
327 /**
328  * Initialize receive task's buffer descriptors
329  * @param[in] fec all we know about the device yet
330  * @param[in] count receive buffer count to be allocated
331  * @param[in] dsize desired size of each receive buffer
332  * @return 0 on success
333  *
334  * Init all RX descriptors to default values.
335  */
336 static void fec_rbd_init(struct fec_priv *fec, int count, int dsize)
337 {
338         uint32_t size;
339         ulong data;
340         int i;
341
342         /*
343          * Reload the RX descriptors with default values and wipe
344          * the RX buffers.
345          */
346         size = roundup(dsize, ARCH_DMA_MINALIGN);
347         for (i = 0; i < count; i++) {
348                 data = fec->rbd_base[i].data_pointer;
349                 memset((void *)data, 0, dsize);
350                 flush_dcache_range(data, data + size);
351
352                 fec->rbd_base[i].status = FEC_RBD_EMPTY;
353                 fec->rbd_base[i].data_length = 0;
354         }
355
356         /* Mark the last RBD to close the ring. */
357         fec->rbd_base[i - 1].status = FEC_RBD_WRAP | FEC_RBD_EMPTY;
358         fec->rbd_index = 0;
359
360         flush_dcache_range((ulong)fec->rbd_base,
361                            (ulong)fec->rbd_base + size);
362 }
363
364 /**
365  * Initialize transmit task's buffer descriptors
366  * @param[in] fec all we know about the device yet
367  *
368  * Transmit buffers are created externally. We only have to init the BDs here.\n
369  * Note: There is a race condition in the hardware. When only one BD is in
370  * use it must be marked with the WRAP bit to use it for every transmitt.
371  * This bit in combination with the READY bit results into double transmit
372  * of each data buffer. It seems the state machine checks READY earlier then
373  * resetting it after the first transfer.
374  * Using two BDs solves this issue.
375  */
376 static void fec_tbd_init(struct fec_priv *fec)
377 {
378         ulong addr = (ulong)fec->tbd_base;
379         unsigned size = roundup(2 * sizeof(struct fec_bd),
380                                 ARCH_DMA_MINALIGN);
381
382         memset(fec->tbd_base, 0, size);
383         fec->tbd_base[0].status = 0;
384         fec->tbd_base[1].status = FEC_TBD_WRAP;
385         fec->tbd_index = 0;
386         flush_dcache_range(addr, addr + size);
387 }
388
389 /**
390  * Mark the given read buffer descriptor as free
391  * @param[in] last 1 if this is the last buffer descriptor in the chain, else 0
392  * @param[in] prbd buffer descriptor to mark free again
393  */
394 static void fec_rbd_clean(int last, struct fec_bd *prbd)
395 {
396         unsigned short flags = FEC_RBD_EMPTY;
397         if (last)
398                 flags |= FEC_RBD_WRAP;
399         writew(flags, &prbd->status);
400         writew(0, &prbd->data_length);
401 }
402
403 static int fec_get_hwaddr(int dev_id, unsigned char *mac)
404 {
405         imx_get_mac_from_fuse(dev_id, mac);
406         return !is_valid_ethaddr(mac);
407 }
408
409 #ifdef CONFIG_DM_ETH
410 static int fecmxc_set_hwaddr(struct udevice *dev)
411 #else
412 static int fec_set_hwaddr(struct eth_device *dev)
413 #endif
414 {
415 #ifdef CONFIG_DM_ETH
416         struct fec_priv *fec = dev_get_priv(dev);
417         struct eth_pdata *pdata = dev_get_platdata(dev);
418         uchar *mac = pdata->enetaddr;
419 #else
420         uchar *mac = dev->enetaddr;
421         struct fec_priv *fec = (struct fec_priv *)dev->priv;
422 #endif
423
424         writel(0, &fec->eth->iaddr1);
425         writel(0, &fec->eth->iaddr2);
426         writel(0, &fec->eth->gaddr1);
427         writel(0, &fec->eth->gaddr2);
428
429         /* Set physical address */
430         writel((mac[0] << 24) + (mac[1] << 16) + (mac[2] << 8) + mac[3],
431                &fec->eth->paddr1);
432         writel((mac[4] << 24) + (mac[5] << 16) + 0x8808, &fec->eth->paddr2);
433
434         return 0;
435 }
436
437 /* Do initial configuration of the FEC registers */
438 static void fec_reg_setup(struct fec_priv *fec)
439 {
440         uint32_t rcntrl;
441
442         /* Set interrupt mask register */
443         writel(0x00000000, &fec->eth->imask);
444
445         /* Clear FEC-Lite interrupt event register(IEVENT) */
446         writel(0xffffffff, &fec->eth->ievent);
447
448         /* Set FEC-Lite receive control register(R_CNTRL): */
449
450         /* Start with frame length = 1518, common for all modes. */
451         rcntrl = PKTSIZE << FEC_RCNTRL_MAX_FL_SHIFT;
452         if (fec->xcv_type != SEVENWIRE)         /* xMII modes */
453                 rcntrl |= FEC_RCNTRL_FCE | FEC_RCNTRL_MII_MODE;
454         if (fec->xcv_type == RGMII)
455                 rcntrl |= FEC_RCNTRL_RGMII;
456         else if (fec->xcv_type == RMII)
457                 rcntrl |= FEC_RCNTRL_RMII;
458
459         writel(rcntrl, &fec->eth->r_cntrl);
460 }
461
462 /**
463  * Start the FEC engine
464  * @param[in] dev Our device to handle
465  */
466 #ifdef CONFIG_DM_ETH
467 static int fec_open(struct udevice *dev)
468 #else
469 static int fec_open(struct eth_device *edev)
470 #endif
471 {
472 #ifdef CONFIG_DM_ETH
473         struct fec_priv *fec = dev_get_priv(dev);
474 #else
475         struct fec_priv *fec = (struct fec_priv *)edev->priv;
476 #endif
477         int speed;
478         ulong addr, size;
479         int i;
480
481         debug("fec_open: fec_open(dev)\n");
482         /* full-duplex, heartbeat disabled */
483         writel(1 << 2, &fec->eth->x_cntrl);
484         fec->rbd_index = 0;
485
486         /* Invalidate all descriptors */
487         for (i = 0; i < FEC_RBD_NUM - 1; i++)
488                 fec_rbd_clean(0, &fec->rbd_base[i]);
489         fec_rbd_clean(1, &fec->rbd_base[i]);
490
491         /* Flush the descriptors into RAM */
492         size = roundup(FEC_RBD_NUM * sizeof(struct fec_bd),
493                         ARCH_DMA_MINALIGN);
494         addr = (ulong)fec->rbd_base;
495         flush_dcache_range(addr, addr + size);
496
497 #ifdef FEC_QUIRK_ENET_MAC
498         /* Enable ENET HW endian SWAP */
499         writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_DBSWAP,
500                &fec->eth->ecntrl);
501         /* Enable ENET store and forward mode */
502         writel(readl(&fec->eth->x_wmrk) | FEC_X_WMRK_STRFWD,
503                &fec->eth->x_wmrk);
504 #endif
505         /* Enable FEC-Lite controller */
506         writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_ETHER_EN,
507                &fec->eth->ecntrl);
508
509 #ifdef FEC_ENET_ENABLE_TXC_DELAY
510         writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_TXC_DLY,
511                &fec->eth->ecntrl);
512 #endif
513
514 #ifdef FEC_ENET_ENABLE_RXC_DELAY
515         writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_RXC_DLY,
516                &fec->eth->ecntrl);
517 #endif
518
519 #if defined(CONFIG_MX25) || defined(CONFIG_MX53) || defined(CONFIG_MX6SL)
520         udelay(100);
521
522         /* setup the MII gasket for RMII mode */
523         /* disable the gasket */
524         writew(0, &fec->eth->miigsk_enr);
525
526         /* wait for the gasket to be disabled */
527         while (readw(&fec->eth->miigsk_enr) & MIIGSK_ENR_READY)
528                 udelay(2);
529
530         /* configure gasket for RMII, 50 MHz, no loopback, and no echo */
531         writew(MIIGSK_CFGR_IF_MODE_RMII, &fec->eth->miigsk_cfgr);
532
533         /* re-enable the gasket */
534         writew(MIIGSK_ENR_EN, &fec->eth->miigsk_enr);
535
536         /* wait until MII gasket is ready */
537         int max_loops = 10;
538         while ((readw(&fec->eth->miigsk_enr) & MIIGSK_ENR_READY) == 0) {
539                 if (--max_loops <= 0) {
540                         printf("WAIT for MII Gasket ready timed out\n");
541                         break;
542                 }
543         }
544 #endif
545
546 #ifdef CONFIG_PHYLIB
547         {
548                 /* Start up the PHY */
549                 int ret = phy_startup(fec->phydev);
550
551                 if (ret) {
552                         printf("Could not initialize PHY %s\n",
553                                fec->phydev->dev->name);
554                         return ret;
555                 }
556                 speed = fec->phydev->speed;
557         }
558 #elif CONFIG_FEC_FIXED_SPEED
559         speed = CONFIG_FEC_FIXED_SPEED;
560 #else
561         miiphy_wait_aneg(edev);
562         speed = miiphy_speed(edev->name, fec->phy_id);
563         miiphy_duplex(edev->name, fec->phy_id);
564 #endif
565
566 #ifdef FEC_QUIRK_ENET_MAC
567         {
568                 u32 ecr = readl(&fec->eth->ecntrl) & ~FEC_ECNTRL_SPEED;
569                 u32 rcr = readl(&fec->eth->r_cntrl) & ~FEC_RCNTRL_RMII_10T;
570                 if (speed == _1000BASET)
571                         ecr |= FEC_ECNTRL_SPEED;
572                 else if (speed != _100BASET)
573                         rcr |= FEC_RCNTRL_RMII_10T;
574                 writel(ecr, &fec->eth->ecntrl);
575                 writel(rcr, &fec->eth->r_cntrl);
576         }
577 #endif
578         debug("%s:Speed=%i\n", __func__, speed);
579
580         /* Enable SmartDMA receive task */
581         fec_rx_task_enable(fec);
582
583         udelay(100000);
584         return 0;
585 }
586
587 #ifdef CONFIG_DM_ETH
588 static int fecmxc_init(struct udevice *dev)
589 #else
590 static int fec_init(struct eth_device *dev, bd_t *bd)
591 #endif
592 {
593 #ifdef CONFIG_DM_ETH
594         struct fec_priv *fec = dev_get_priv(dev);
595 #else
596         struct fec_priv *fec = (struct fec_priv *)dev->priv;
597 #endif
598         u8 *mib_ptr = (uint8_t *)&fec->eth->rmon_t_drop;
599         u8 *i;
600         ulong addr;
601
602         /* Initialize MAC address */
603 #ifdef CONFIG_DM_ETH
604         fecmxc_set_hwaddr(dev);
605 #else
606         fec_set_hwaddr(dev);
607 #endif
608
609         /* Setup transmit descriptors, there are two in total. */
610         fec_tbd_init(fec);
611
612         /* Setup receive descriptors. */
613         fec_rbd_init(fec, FEC_RBD_NUM, FEC_MAX_PKT_SIZE);
614
615         fec_reg_setup(fec);
616
617         if (fec->xcv_type != SEVENWIRE)
618                 fec_mii_setspeed(fec->bus->priv);
619
620         /* Set Opcode/Pause Duration Register */
621         writel(0x00010020, &fec->eth->op_pause);        /* FIXME 0xffff0020; */
622         writel(0x2, &fec->eth->x_wmrk);
623
624         /* Set multicast address filter */
625         writel(0x00000000, &fec->eth->gaddr1);
626         writel(0x00000000, &fec->eth->gaddr2);
627
628         /* Do not access reserved register */
629         if (!is_mx6ul() && !is_mx6ull() && !is_imx8() && !is_imx8m()) {
630                 /* clear MIB RAM */
631                 for (i = mib_ptr; i <= mib_ptr + 0xfc; i += 4)
632                         writel(0, i);
633
634                 /* FIFO receive start register */
635                 writel(0x520, &fec->eth->r_fstart);
636         }
637
638         /* size and address of each buffer */
639         writel(FEC_MAX_PKT_SIZE, &fec->eth->emrbr);
640
641         addr = (ulong)fec->tbd_base;
642         writel((uint32_t)addr, &fec->eth->etdsr);
643
644         addr = (ulong)fec->rbd_base;
645         writel((uint32_t)addr, &fec->eth->erdsr);
646
647 #ifndef CONFIG_PHYLIB
648         if (fec->xcv_type != SEVENWIRE)
649                 miiphy_restart_aneg(dev);
650 #endif
651         fec_open(dev);
652         return 0;
653 }
654
655 /**
656  * Halt the FEC engine
657  * @param[in] dev Our device to handle
658  */
659 #ifdef CONFIG_DM_ETH
660 static void fecmxc_halt(struct udevice *dev)
661 #else
662 static void fec_halt(struct eth_device *dev)
663 #endif
664 {
665 #ifdef CONFIG_DM_ETH
666         struct fec_priv *fec = dev_get_priv(dev);
667 #else
668         struct fec_priv *fec = (struct fec_priv *)dev->priv;
669 #endif
670         int counter = 0xffff;
671
672         /* issue graceful stop command to the FEC transmitter if necessary */
673         writel(FEC_TCNTRL_GTS | readl(&fec->eth->x_cntrl),
674                &fec->eth->x_cntrl);
675
676         debug("eth_halt: wait for stop regs\n");
677         /* wait for graceful stop to register */
678         while ((counter--) && (!(readl(&fec->eth->ievent) & FEC_IEVENT_GRA)))
679                 udelay(1);
680
681         /* Disable SmartDMA tasks */
682         fec_tx_task_disable(fec);
683         fec_rx_task_disable(fec);
684
685         /*
686          * Disable the Ethernet Controller
687          * Note: this will also reset the BD index counter!
688          */
689         writel(readl(&fec->eth->ecntrl) & ~FEC_ECNTRL_ETHER_EN,
690                &fec->eth->ecntrl);
691         fec->rbd_index = 0;
692         fec->tbd_index = 0;
693         debug("eth_halt: done\n");
694 }
695
696 /**
697  * Transmit one frame
698  * @param[in] dev Our ethernet device to handle
699  * @param[in] packet Pointer to the data to be transmitted
700  * @param[in] length Data count in bytes
701  * @return 0 on success
702  */
703 #ifdef CONFIG_DM_ETH
704 static int fecmxc_send(struct udevice *dev, void *packet, int length)
705 #else
706 static int fec_send(struct eth_device *dev, void *packet, int length)
707 #endif
708 {
709         unsigned int status;
710         u32 size;
711         ulong addr, end;
712         int timeout = FEC_XFER_TIMEOUT;
713         int ret = 0;
714
715         /*
716          * This routine transmits one frame.  This routine only accepts
717          * 6-byte Ethernet addresses.
718          */
719 #ifdef CONFIG_DM_ETH
720         struct fec_priv *fec = dev_get_priv(dev);
721 #else
722         struct fec_priv *fec = (struct fec_priv *)dev->priv;
723 #endif
724
725         /*
726          * Check for valid length of data.
727          */
728         if ((length > 1500) || (length <= 0)) {
729                 printf("Payload (%d) too large\n", length);
730                 return -1;
731         }
732
733         /*
734          * Setup the transmit buffer. We are always using the first buffer for
735          * transmission, the second will be empty and only used to stop the DMA
736          * engine. We also flush the packet to RAM here to avoid cache trouble.
737          */
738 #ifdef CONFIG_FEC_MXC_SWAP_PACKET
739         swap_packet((uint32_t *)packet, length);
740 #endif
741
742         addr = (ulong)packet;
743         end = roundup(addr + length, ARCH_DMA_MINALIGN);
744         addr &= ~(ARCH_DMA_MINALIGN - 1);
745         flush_dcache_range(addr, end);
746
747         writew(length, &fec->tbd_base[fec->tbd_index].data_length);
748         writel((uint32_t)addr, &fec->tbd_base[fec->tbd_index].data_pointer);
749
750         /*
751          * update BD's status now
752          * This block:
753          * - is always the last in a chain (means no chain)
754          * - should transmitt the CRC
755          * - might be the last BD in the list, so the address counter should
756          *   wrap (-> keep the WRAP flag)
757          */
758         status = readw(&fec->tbd_base[fec->tbd_index].status) & FEC_TBD_WRAP;
759         status |= FEC_TBD_LAST | FEC_TBD_TC | FEC_TBD_READY;
760         writew(status, &fec->tbd_base[fec->tbd_index].status);
761
762         /*
763          * Flush data cache. This code flushes both TX descriptors to RAM.
764          * After this code, the descriptors will be safely in RAM and we
765          * can start DMA.
766          */
767         size = roundup(2 * sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
768         addr = (ulong)fec->tbd_base;
769         flush_dcache_range(addr, addr + size);
770
771         /*
772          * Below we read the DMA descriptor's last four bytes back from the
773          * DRAM. This is important in order to make sure that all WRITE
774          * operations on the bus that were triggered by previous cache FLUSH
775          * have completed.
776          *
777          * Otherwise, on MX28, it is possible to observe a corruption of the
778          * DMA descriptors. Please refer to schematic "Figure 1-2" in MX28RM
779          * for the bus structure of MX28. The scenario is as follows:
780          *
781          * 1) ARM core triggers a series of WRITEs on the AHB_ARB2 bus going
782          *    to DRAM due to flush_dcache_range()
783          * 2) ARM core writes the FEC registers via AHB_ARB2
784          * 3) FEC DMA starts reading/writing from/to DRAM via AHB_ARB3
785          *
786          * Note that 2) does sometimes finish before 1) due to reordering of
787          * WRITE accesses on the AHB bus, therefore triggering 3) before the
788          * DMA descriptor is fully written into DRAM. This results in occasional
789          * corruption of the DMA descriptor.
790          */
791         readl(addr + size - 4);
792
793         /* Enable SmartDMA transmit task */
794         fec_tx_task_enable(fec);
795
796         /*
797          * Wait until frame is sent. On each turn of the wait cycle, we must
798          * invalidate data cache to see what's really in RAM. Also, we need
799          * barrier here.
800          */
801         while (--timeout) {
802                 if (!(readl(&fec->eth->x_des_active) & FEC_X_DES_ACTIVE_TDAR))
803                         break;
804         }
805
806         if (!timeout) {
807                 ret = -EINVAL;
808                 goto out;
809         }
810
811         /*
812          * The TDAR bit is cleared when the descriptors are all out from TX
813          * but on mx6solox we noticed that the READY bit is still not cleared
814          * right after TDAR.
815          * These are two distinct signals, and in IC simulation, we found that
816          * TDAR always gets cleared prior than the READY bit of last BD becomes
817          * cleared.
818          * In mx6solox, we use a later version of FEC IP. It looks like that
819          * this intrinsic behaviour of TDAR bit has changed in this newer FEC
820          * version.
821          *
822          * Fix this by polling the READY bit of BD after the TDAR polling,
823          * which covers the mx6solox case and does not harm the other SoCs.
824          */
825         timeout = FEC_XFER_TIMEOUT;
826         while (--timeout) {
827                 invalidate_dcache_range(addr, addr + size);
828                 if (!(readw(&fec->tbd_base[fec->tbd_index].status) &
829                     FEC_TBD_READY))
830                         break;
831         }
832
833         if (!timeout)
834                 ret = -EINVAL;
835
836 out:
837         debug("fec_send: status 0x%x index %d ret %i\n",
838               readw(&fec->tbd_base[fec->tbd_index].status),
839               fec->tbd_index, ret);
840         /* for next transmission use the other buffer */
841         if (fec->tbd_index)
842                 fec->tbd_index = 0;
843         else
844                 fec->tbd_index = 1;
845
846         return ret;
847 }
848
849 /**
850  * Pull one frame from the card
851  * @param[in] dev Our ethernet device to handle
852  * @return Length of packet read
853  */
854 #ifdef CONFIG_DM_ETH
855 static int fecmxc_recv(struct udevice *dev, int flags, uchar **packetp)
856 #else
857 static int fec_recv(struct eth_device *dev)
858 #endif
859 {
860 #ifdef CONFIG_DM_ETH
861         struct fec_priv *fec = dev_get_priv(dev);
862 #else
863         struct fec_priv *fec = (struct fec_priv *)dev->priv;
864 #endif
865         struct fec_bd *rbd = &fec->rbd_base[fec->rbd_index];
866         unsigned long ievent;
867         int frame_length, len = 0;
868         uint16_t bd_status;
869         ulong addr, size, end;
870         int i;
871
872 #ifdef CONFIG_DM_ETH
873         *packetp = memalign(ARCH_DMA_MINALIGN, FEC_MAX_PKT_SIZE);
874         if (*packetp == 0) {
875                 printf("%s: error allocating packetp\n", __func__);
876                 return -ENOMEM;
877         }
878 #else
879         ALLOC_CACHE_ALIGN_BUFFER(uchar, buff, FEC_MAX_PKT_SIZE);
880 #endif
881
882         /* Check if any critical events have happened */
883         ievent = readl(&fec->eth->ievent);
884         writel(ievent, &fec->eth->ievent);
885         debug("fec_recv: ievent 0x%lx\n", ievent);
886         if (ievent & FEC_IEVENT_BABR) {
887 #ifdef CONFIG_DM_ETH
888                 fecmxc_halt(dev);
889                 fecmxc_init(dev);
890 #else
891                 fec_halt(dev);
892                 fec_init(dev, fec->bd);
893 #endif
894                 printf("some error: 0x%08lx\n", ievent);
895                 return 0;
896         }
897         if (ievent & FEC_IEVENT_HBERR) {
898                 /* Heartbeat error */
899                 writel(0x00000001 | readl(&fec->eth->x_cntrl),
900                        &fec->eth->x_cntrl);
901         }
902         if (ievent & FEC_IEVENT_GRA) {
903                 /* Graceful stop complete */
904                 if (readl(&fec->eth->x_cntrl) & 0x00000001) {
905 #ifdef CONFIG_DM_ETH
906                         fecmxc_halt(dev);
907 #else
908                         fec_halt(dev);
909 #endif
910                         writel(~0x00000001 & readl(&fec->eth->x_cntrl),
911                                &fec->eth->x_cntrl);
912 #ifdef CONFIG_DM_ETH
913                         fecmxc_init(dev);
914 #else
915                         fec_init(dev, fec->bd);
916 #endif
917                 }
918         }
919
920         /*
921          * Read the buffer status. Before the status can be read, the data cache
922          * must be invalidated, because the data in RAM might have been changed
923          * by DMA. The descriptors are properly aligned to cachelines so there's
924          * no need to worry they'd overlap.
925          *
926          * WARNING: By invalidating the descriptor here, we also invalidate
927          * the descriptors surrounding this one. Therefore we can NOT change the
928          * contents of this descriptor nor the surrounding ones. The problem is
929          * that in order to mark the descriptor as processed, we need to change
930          * the descriptor. The solution is to mark the whole cache line when all
931          * descriptors in the cache line are processed.
932          */
933         addr = (ulong)rbd;
934         addr &= ~(ARCH_DMA_MINALIGN - 1);
935         size = roundup(sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
936         invalidate_dcache_range(addr, addr + size);
937
938         bd_status = readw(&rbd->status);
939         debug("fec_recv: status 0x%x\n", bd_status);
940
941         if (!(bd_status & FEC_RBD_EMPTY)) {
942                 if ((bd_status & FEC_RBD_LAST) && !(bd_status & FEC_RBD_ERR) &&
943                     ((readw(&rbd->data_length) - 4) > 14)) {
944                         /* Get buffer address and size */
945                         addr = readl(&rbd->data_pointer);
946                         frame_length = readw(&rbd->data_length) - 4;
947                         /* Invalidate data cache over the buffer */
948                         end = roundup(addr + frame_length, ARCH_DMA_MINALIGN);
949                         addr &= ~(ARCH_DMA_MINALIGN - 1);
950                         invalidate_dcache_range(addr, end);
951
952                         /* Fill the buffer and pass it to upper layers */
953 #ifdef CONFIG_FEC_MXC_SWAP_PACKET
954                         swap_packet((uint32_t *)addr, frame_length);
955 #endif
956
957 #ifdef CONFIG_DM_ETH
958                         memcpy(*packetp, (char *)addr, frame_length);
959 #else
960                         memcpy(buff, (char *)addr, frame_length);
961                         net_process_received_packet(buff, frame_length);
962 #endif
963                         len = frame_length;
964                 } else {
965                         if (bd_status & FEC_RBD_ERR)
966                                 debug("error frame: 0x%08lx 0x%08x\n",
967                                       addr, bd_status);
968                 }
969
970                 /*
971                  * Free the current buffer, restart the engine and move forward
972                  * to the next buffer. Here we check if the whole cacheline of
973                  * descriptors was already processed and if so, we mark it free
974                  * as whole.
975                  */
976                 size = RXDESC_PER_CACHELINE - 1;
977                 if ((fec->rbd_index & size) == size) {
978                         i = fec->rbd_index - size;
979                         addr = (ulong)&fec->rbd_base[i];
980                         for (; i <= fec->rbd_index ; i++) {
981                                 fec_rbd_clean(i == (FEC_RBD_NUM - 1),
982                                               &fec->rbd_base[i]);
983                         }
984                         flush_dcache_range(addr,
985                                            addr + ARCH_DMA_MINALIGN);
986                 }
987
988                 fec_rx_task_enable(fec);
989                 fec->rbd_index = (fec->rbd_index + 1) % FEC_RBD_NUM;
990         }
991         debug("fec_recv: stop\n");
992
993         return len;
994 }
995
996 static void fec_set_dev_name(char *dest, int dev_id)
997 {
998         sprintf(dest, (dev_id == -1) ? "FEC" : "FEC%i", dev_id);
999 }
1000
1001 static int fec_alloc_descs(struct fec_priv *fec)
1002 {
1003         unsigned int size;
1004         int i;
1005         uint8_t *data;
1006         ulong addr;
1007
1008         /* Allocate TX descriptors. */
1009         size = roundup(2 * sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
1010         fec->tbd_base = memalign(ARCH_DMA_MINALIGN, size);
1011         if (!fec->tbd_base)
1012                 goto err_tx;
1013
1014         /* Allocate RX descriptors. */
1015         size = roundup(FEC_RBD_NUM * sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
1016         fec->rbd_base = memalign(ARCH_DMA_MINALIGN, size);
1017         if (!fec->rbd_base)
1018                 goto err_rx;
1019
1020         memset(fec->rbd_base, 0, size);
1021
1022         /* Allocate RX buffers. */
1023
1024         /* Maximum RX buffer size. */
1025         size = roundup(FEC_MAX_PKT_SIZE, FEC_DMA_RX_MINALIGN);
1026         for (i = 0; i < FEC_RBD_NUM; i++) {
1027                 data = memalign(FEC_DMA_RX_MINALIGN, size);
1028                 if (!data) {
1029                         printf("%s: error allocating rxbuf %d\n", __func__, i);
1030                         goto err_ring;
1031                 }
1032
1033                 memset(data, 0, size);
1034
1035                 addr = (ulong)data;
1036                 fec->rbd_base[i].data_pointer = (uint32_t)addr;
1037                 fec->rbd_base[i].status = FEC_RBD_EMPTY;
1038                 fec->rbd_base[i].data_length = 0;
1039                 /* Flush the buffer to memory. */
1040                 flush_dcache_range(addr, addr + size);
1041         }
1042
1043         /* Mark the last RBD to close the ring. */
1044         fec->rbd_base[i - 1].status = FEC_RBD_WRAP | FEC_RBD_EMPTY;
1045
1046         fec->rbd_index = 0;
1047         fec->tbd_index = 0;
1048
1049         return 0;
1050
1051 err_ring:
1052         for (; i >= 0; i--) {
1053                 addr = fec->rbd_base[i].data_pointer;
1054                 free((void *)addr);
1055         }
1056         free(fec->rbd_base);
1057 err_rx:
1058         free(fec->tbd_base);
1059 err_tx:
1060         return -ENOMEM;
1061 }
1062
1063 static void fec_free_descs(struct fec_priv *fec)
1064 {
1065         int i;
1066         ulong addr;
1067
1068         for (i = 0; i < FEC_RBD_NUM; i++) {
1069                 addr = fec->rbd_base[i].data_pointer;
1070                 free((void *)addr);
1071         }
1072         free(fec->rbd_base);
1073         free(fec->tbd_base);
1074 }
1075
1076 struct mii_dev *fec_get_miibus(ulong base_addr, int dev_id)
1077 {
1078         struct ethernet_regs *eth = (struct ethernet_regs *)base_addr;
1079         struct mii_dev *bus;
1080         int ret;
1081
1082         bus = mdio_alloc();
1083         if (!bus) {
1084                 printf("mdio_alloc failed\n");
1085                 return NULL;
1086         }
1087         bus->read = fec_phy_read;
1088         bus->write = fec_phy_write;
1089         bus->priv = eth;
1090         fec_set_dev_name(bus->name, dev_id);
1091
1092         ret = mdio_register(bus);
1093         if (ret) {
1094                 printf("mdio_register failed\n");
1095                 free(bus);
1096                 return NULL;
1097         }
1098         fec_mii_setspeed(eth);
1099         return bus;
1100 }
1101
1102 #ifndef CONFIG_DM_ETH
1103 #ifdef CONFIG_PHYLIB
1104 int fec_probe(bd_t *bd, int dev_id, uint32_t base_addr,
1105                 struct mii_dev *bus, struct phy_device *phydev)
1106 #else
1107 static int fec_probe(bd_t *bd, int dev_id, uint32_t base_addr,
1108                 struct mii_dev *bus, int phy_id)
1109 #endif
1110 {
1111         struct eth_device *edev;
1112         struct fec_priv *fec;
1113         unsigned char ethaddr[6];
1114         char mac[16];
1115         uint32_t start;
1116         int ret = 0;
1117
1118         /* create and fill edev struct */
1119         edev = (struct eth_device *)malloc(sizeof(struct eth_device));
1120         if (!edev) {
1121                 puts("fec_mxc: not enough malloc memory for eth_device\n");
1122                 ret = -ENOMEM;
1123                 goto err1;
1124         }
1125
1126         fec = (struct fec_priv *)malloc(sizeof(struct fec_priv));
1127         if (!fec) {
1128                 puts("fec_mxc: not enough malloc memory for fec_priv\n");
1129                 ret = -ENOMEM;
1130                 goto err2;
1131         }
1132
1133         memset(edev, 0, sizeof(*edev));
1134         memset(fec, 0, sizeof(*fec));
1135
1136         ret = fec_alloc_descs(fec);
1137         if (ret)
1138                 goto err3;
1139
1140         edev->priv = fec;
1141         edev->init = fec_init;
1142         edev->send = fec_send;
1143         edev->recv = fec_recv;
1144         edev->halt = fec_halt;
1145         edev->write_hwaddr = fec_set_hwaddr;
1146
1147         fec->eth = (struct ethernet_regs *)(ulong)base_addr;
1148         fec->bd = bd;
1149
1150         fec->xcv_type = CONFIG_FEC_XCV_TYPE;
1151
1152         /* Reset chip. */
1153         writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_RESET, &fec->eth->ecntrl);
1154         start = get_timer(0);
1155         while (readl(&fec->eth->ecntrl) & FEC_ECNTRL_RESET) {
1156                 if (get_timer(start) > (CONFIG_SYS_HZ * 5)) {
1157                         printf("FEC MXC: Timeout resetting chip\n");
1158                         goto err4;
1159                 }
1160                 udelay(10);
1161         }
1162
1163         fec_reg_setup(fec);
1164         fec_set_dev_name(edev->name, dev_id);
1165         fec->dev_id = (dev_id == -1) ? 0 : dev_id;
1166         fec->bus = bus;
1167         fec_mii_setspeed(bus->priv);
1168 #ifdef CONFIG_PHYLIB
1169         fec->phydev = phydev;
1170         phy_connect_dev(phydev, edev);
1171         /* Configure phy */
1172         phy_config(phydev);
1173 #else
1174         fec->phy_id = phy_id;
1175 #endif
1176         eth_register(edev);
1177         /* only support one eth device, the index number pointed by dev_id */
1178         edev->index = fec->dev_id;
1179
1180         if (fec_get_hwaddr(fec->dev_id, ethaddr) == 0) {
1181                 debug("got MAC%d address from fuse: %pM\n", fec->dev_id, ethaddr);
1182                 memcpy(edev->enetaddr, ethaddr, 6);
1183                 if (fec->dev_id)
1184                         sprintf(mac, "eth%daddr", fec->dev_id);
1185                 else
1186                         strcpy(mac, "ethaddr");
1187                 if (!env_get(mac))
1188                         eth_env_set_enetaddr(mac, ethaddr);
1189         }
1190         return ret;
1191 err4:
1192         fec_free_descs(fec);
1193 err3:
1194         free(fec);
1195 err2:
1196         free(edev);
1197 err1:
1198         return ret;
1199 }
1200
1201 int fecmxc_initialize_multi(bd_t *bd, int dev_id, int phy_id, uint32_t addr)
1202 {
1203         uint32_t base_mii;
1204         struct mii_dev *bus = NULL;
1205 #ifdef CONFIG_PHYLIB
1206         struct phy_device *phydev = NULL;
1207 #endif
1208         int ret;
1209
1210         if (CONFIG_IS_ENABLED(IMX_MODULE_FUSE)) {
1211                 if (enet_fused((ulong)addr)) {
1212                         printf("SoC fuse indicates Ethernet@0x%x is unavailable.\n", addr);
1213                         return -ENODEV;
1214                 }
1215         }
1216
1217 #ifdef CONFIG_FEC_MXC_MDIO_BASE
1218         /*
1219          * The i.MX28 has two ethernet interfaces, but they are not equal.
1220          * Only the first one can access the MDIO bus.
1221          */
1222         base_mii = CONFIG_FEC_MXC_MDIO_BASE;
1223 #else
1224         base_mii = addr;
1225 #endif
1226         debug("eth_init: fec_probe(bd, %i, %i) @ %08x\n", dev_id, phy_id, addr);
1227         bus = fec_get_miibus(base_mii, dev_id);
1228         if (!bus)
1229                 return -ENOMEM;
1230 #ifdef CONFIG_PHYLIB
1231         phydev = phy_find_by_mask(bus, 1 << phy_id, PHY_INTERFACE_MODE_RGMII);
1232         if (!phydev) {
1233                 mdio_unregister(bus);
1234                 free(bus);
1235                 return -ENOMEM;
1236         }
1237         ret = fec_probe(bd, dev_id, addr, bus, phydev);
1238 #else
1239         ret = fec_probe(bd, dev_id, addr, bus, phy_id);
1240 #endif
1241         if (ret) {
1242 #ifdef CONFIG_PHYLIB
1243                 free(phydev);
1244 #endif
1245                 mdio_unregister(bus);
1246                 free(bus);
1247         }
1248         return ret;
1249 }
1250
1251 #ifdef CONFIG_FEC_MXC_PHYADDR
1252 int fecmxc_initialize(bd_t *bd)
1253 {
1254         return fecmxc_initialize_multi(bd, -1, CONFIG_FEC_MXC_PHYADDR,
1255                         IMX_FEC_BASE);
1256 }
1257 #endif
1258
1259 #ifndef CONFIG_PHYLIB
1260 int fecmxc_register_mii_postcall(struct eth_device *dev, int (*cb)(int))
1261 {
1262         struct fec_priv *fec = (struct fec_priv *)dev->priv;
1263         fec->mii_postcall = cb;
1264         return 0;
1265 }
1266 #endif
1267
1268 #else
1269
1270 static int fecmxc_read_rom_hwaddr(struct udevice *dev)
1271 {
1272         struct fec_priv *priv = dev_get_priv(dev);
1273         struct eth_pdata *pdata = dev_get_platdata(dev);
1274
1275         return fec_get_hwaddr(priv->dev_id, pdata->enetaddr);
1276 }
1277
1278 static int fecmxc_free_pkt(struct udevice *dev, uchar *packet, int length)
1279 {
1280         if (packet)
1281                 free(packet);
1282
1283         return 0;
1284 }
1285
1286 static const struct eth_ops fecmxc_ops = {
1287         .start                  = fecmxc_init,
1288         .send                   = fecmxc_send,
1289         .recv                   = fecmxc_recv,
1290         .free_pkt               = fecmxc_free_pkt,
1291         .stop                   = fecmxc_halt,
1292         .write_hwaddr           = fecmxc_set_hwaddr,
1293         .read_rom_hwaddr        = fecmxc_read_rom_hwaddr,
1294 };
1295
1296 static int device_get_phy_addr(struct udevice *dev)
1297 {
1298         struct ofnode_phandle_args phandle_args;
1299         int reg;
1300
1301         if (dev_read_phandle_with_args(dev, "phy-handle", NULL, 0, 0,
1302                                        &phandle_args)) {
1303                 debug("Failed to find phy-handle");
1304                 return -ENODEV;
1305         }
1306
1307         reg = ofnode_read_u32_default(phandle_args.node, "reg", 0);
1308
1309         return reg;
1310 }
1311
1312 static int fec_phy_init(struct fec_priv *priv, struct udevice *dev)
1313 {
1314         struct phy_device *phydev;
1315         int addr;
1316
1317         addr = device_get_phy_addr(dev);
1318 #ifdef CONFIG_FEC_MXC_PHYADDR
1319         addr = CONFIG_FEC_MXC_PHYADDR;
1320 #endif
1321
1322         phydev = phy_connect(priv->bus, addr, dev, priv->interface);
1323         if (!phydev)
1324                 return -ENODEV;
1325
1326         priv->phydev = phydev;
1327         phy_config(phydev);
1328
1329         return 0;
1330 }
1331
1332 #if CONFIG_IS_ENABLED(DM_GPIO)
1333 /* FEC GPIO reset */
1334 static void fec_gpio_reset(struct fec_priv *priv)
1335 {
1336         debug("fec_gpio_reset: fec_gpio_reset(dev)\n");
1337         if (dm_gpio_is_valid(&priv->phy_reset_gpio)) {
1338                 dm_gpio_set_value(&priv->phy_reset_gpio, 1);
1339                 mdelay(priv->reset_delay);
1340                 dm_gpio_set_value(&priv->phy_reset_gpio, 0);
1341                 if (priv->reset_post_delay)
1342                         mdelay(priv->reset_post_delay);
1343         }
1344 }
1345 #endif
1346
1347 static int fecmxc_probe(struct udevice *dev)
1348 {
1349         struct eth_pdata *pdata = dev_get_platdata(dev);
1350         struct fec_priv *priv = dev_get_priv(dev);
1351         struct mii_dev *bus = NULL;
1352         uint32_t start;
1353         int ret;
1354
1355         if (CONFIG_IS_ENABLED(IMX_MODULE_FUSE)) {
1356                 if (enet_fused((ulong)priv->eth)) {
1357                         printf("SoC fuse indicates Ethernet@0x%lx is unavailable.\n", (ulong)priv->eth);
1358                         return -ENODEV;
1359                 }
1360         }
1361
1362         if (IS_ENABLED(CONFIG_IMX8)) {
1363                 ret = clk_get_by_name(dev, "ipg", &priv->ipg_clk);
1364                 if (ret < 0) {
1365                         debug("Can't get FEC ipg clk: %d\n", ret);
1366                         return ret;
1367                 }
1368                 ret = clk_enable(&priv->ipg_clk);
1369                 if (ret < 0) {
1370                         debug("Can't enable FEC ipg clk: %d\n", ret);
1371                         return ret;
1372                 }
1373
1374                 priv->clk_rate = clk_get_rate(&priv->ipg_clk);
1375         } else if (CONFIG_IS_ENABLED(CLK_CCF)) {
1376                 ret = clk_get_by_name(dev, "ipg", &priv->ipg_clk);
1377                 if (ret < 0) {
1378                         debug("Can't get FEC ipg clk: %d\n", ret);
1379                         return ret;
1380                 }
1381                 ret = clk_enable(&priv->ipg_clk);
1382                 if(ret)
1383                         return ret;
1384
1385                 ret = clk_get_by_name(dev, "ahb", &priv->ahb_clk);
1386                 if (ret < 0) {
1387                         debug("Can't get FEC ahb clk: %d\n", ret);
1388                         return ret;
1389                 }
1390                 ret = clk_enable(&priv->ahb_clk);
1391                 if (ret)
1392                         return ret;
1393
1394                 ret = clk_get_by_name(dev, "enet_out", &priv->clk_enet_out);
1395                 if (!ret) {
1396                         ret = clk_enable(&priv->clk_enet_out);
1397                         if (ret)
1398                                 return ret;
1399                 }
1400
1401                 ret = clk_get_by_name(dev, "enet_clk_ref", &priv->clk_ref);
1402                 if (!ret) {
1403                         ret = clk_enable(&priv->clk_ref);
1404                         if (ret)
1405                                 return ret;
1406                 }
1407
1408                 ret = clk_get_by_name(dev, "ptp", &priv->clk_ptp);
1409                 if (!ret) {
1410                         ret = clk_enable(&priv->clk_ptp);
1411                         if (ret)
1412                                 return ret;
1413                 }
1414
1415                 priv->clk_rate = clk_get_rate(&priv->ipg_clk);
1416         }
1417
1418         ret = fec_alloc_descs(priv);
1419         if (ret)
1420                 return ret;
1421
1422 #ifdef CONFIG_DM_REGULATOR
1423         if (priv->phy_supply) {
1424                 ret = regulator_set_enable(priv->phy_supply, true);
1425                 if (ret) {
1426                         printf("%s: Error enabling phy supply\n", dev->name);
1427                         return ret;
1428                 }
1429         }
1430 #endif
1431
1432 #if CONFIG_IS_ENABLED(DM_GPIO)
1433         fec_gpio_reset(priv);
1434 #endif
1435         /* Reset chip. */
1436         writel(readl(&priv->eth->ecntrl) | FEC_ECNTRL_RESET,
1437                &priv->eth->ecntrl);
1438         start = get_timer(0);
1439         while (readl(&priv->eth->ecntrl) & FEC_ECNTRL_RESET) {
1440                 if (get_timer(start) > (CONFIG_SYS_HZ * 5)) {
1441                         printf("FEC MXC: Timeout reseting chip\n");
1442                         goto err_timeout;
1443                 }
1444                 udelay(10);
1445         }
1446
1447         fec_reg_setup(priv);
1448
1449         priv->dev_id = dev->seq;
1450
1451 #ifdef CONFIG_DM_ETH_PHY
1452         bus = eth_phy_get_mdio_bus(dev);
1453 #endif
1454
1455         if (!bus) {
1456 #ifdef CONFIG_FEC_MXC_MDIO_BASE
1457                 bus = fec_get_miibus((ulong)CONFIG_FEC_MXC_MDIO_BASE, dev->seq);
1458 #else
1459                 bus = fec_get_miibus((ulong)priv->eth, dev->seq);
1460 #endif
1461         }
1462         if (!bus) {
1463                 ret = -ENOMEM;
1464                 goto err_mii;
1465         }
1466
1467 #ifdef CONFIG_DM_ETH_PHY
1468         eth_phy_set_mdio_bus(dev, bus);
1469 #endif
1470
1471         priv->bus = bus;
1472         priv->interface = pdata->phy_interface;
1473         switch (priv->interface) {
1474         case PHY_INTERFACE_MODE_MII:
1475                 priv->xcv_type = MII100;
1476                 break;
1477         case PHY_INTERFACE_MODE_RMII:
1478                 priv->xcv_type = RMII;
1479                 break;
1480         case PHY_INTERFACE_MODE_RGMII:
1481         case PHY_INTERFACE_MODE_RGMII_ID:
1482         case PHY_INTERFACE_MODE_RGMII_RXID:
1483         case PHY_INTERFACE_MODE_RGMII_TXID:
1484                 priv->xcv_type = RGMII;
1485                 break;
1486         default:
1487                 priv->xcv_type = CONFIG_FEC_XCV_TYPE;
1488                 printf("Unsupported interface type %d defaulting to %d\n",
1489                        priv->interface, priv->xcv_type);
1490                 break;
1491         }
1492
1493         ret = fec_phy_init(priv, dev);
1494         if (ret)
1495                 goto err_phy;
1496
1497         return 0;
1498
1499 err_phy:
1500         mdio_unregister(bus);
1501         free(bus);
1502 err_mii:
1503 err_timeout:
1504         fec_free_descs(priv);
1505         return ret;
1506 }
1507
1508 static int fecmxc_remove(struct udevice *dev)
1509 {
1510         struct fec_priv *priv = dev_get_priv(dev);
1511
1512         free(priv->phydev);
1513         fec_free_descs(priv);
1514         mdio_unregister(priv->bus);
1515         mdio_free(priv->bus);
1516
1517 #ifdef CONFIG_DM_REGULATOR
1518         if (priv->phy_supply)
1519                 regulator_set_enable(priv->phy_supply, false);
1520 #endif
1521
1522         return 0;
1523 }
1524
1525 static int fecmxc_ofdata_to_platdata(struct udevice *dev)
1526 {
1527         int ret = 0;
1528         struct eth_pdata *pdata = dev_get_platdata(dev);
1529         struct fec_priv *priv = dev_get_priv(dev);
1530         const char *phy_mode;
1531
1532         pdata->iobase = (phys_addr_t)devfdt_get_addr(dev);
1533         priv->eth = (struct ethernet_regs *)pdata->iobase;
1534
1535         pdata->phy_interface = -1;
1536         phy_mode = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "phy-mode",
1537                                NULL);
1538         if (phy_mode)
1539                 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
1540         if (pdata->phy_interface == -1) {
1541                 debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
1542                 return -EINVAL;
1543         }
1544
1545 #ifdef CONFIG_DM_REGULATOR
1546         device_get_supply_regulator(dev, "phy-supply", &priv->phy_supply);
1547 #endif
1548
1549 #if CONFIG_IS_ENABLED(DM_GPIO)
1550         ret = gpio_request_by_name(dev, "phy-reset-gpios", 0,
1551                                    &priv->phy_reset_gpio, GPIOD_IS_OUT);
1552         if (ret < 0)
1553                 return 0; /* property is optional, don't return error! */
1554
1555         priv->reset_delay = dev_read_u32_default(dev, "phy-reset-duration", 1);
1556         if (priv->reset_delay > 1000) {
1557                 printf("FEC MXC: phy reset duration should be <= 1000ms\n");
1558                 /* property value wrong, use default value */
1559                 priv->reset_delay = 1;
1560         }
1561
1562         priv->reset_post_delay = dev_read_u32_default(dev,
1563                                                       "phy-reset-post-delay",
1564                                                       0);
1565         if (priv->reset_post_delay > 1000) {
1566                 printf("FEC MXC: phy reset post delay should be <= 1000ms\n");
1567                 /* property value wrong, use default value */
1568                 priv->reset_post_delay = 0;
1569         }
1570 #endif
1571
1572         return 0;
1573 }
1574
1575 static const struct udevice_id fecmxc_ids[] = {
1576         { .compatible = "fsl,imx28-fec" },
1577         { .compatible = "fsl,imx6q-fec" },
1578         { .compatible = "fsl,imx6sl-fec" },
1579         { .compatible = "fsl,imx6sx-fec" },
1580         { .compatible = "fsl,imx6ul-fec" },
1581         { .compatible = "fsl,imx53-fec" },
1582         { .compatible = "fsl,imx7d-fec" },
1583         { .compatible = "fsl,mvf600-fec" },
1584         { }
1585 };
1586
1587 U_BOOT_DRIVER(fecmxc_gem) = {
1588         .name   = "fecmxc",
1589         .id     = UCLASS_ETH,
1590         .of_match = fecmxc_ids,
1591         .ofdata_to_platdata = fecmxc_ofdata_to_platdata,
1592         .probe  = fecmxc_probe,
1593         .remove = fecmxc_remove,
1594         .ops    = &fecmxc_ops,
1595         .priv_auto_alloc_size = sizeof(struct fec_priv),
1596         .platdata_auto_alloc_size = sizeof(struct eth_pdata),
1597 };
1598 #endif