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