1 // SPDX-License-Identifier: GPL-2.0+
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>
18 #include <power/regulator.h>
21 #include <linux/errno.h>
22 #include <linux/compiler.h>
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>
31 DECLARE_GLOBAL_DATA_PTR;
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.
37 #define FEC_XFER_TIMEOUT 5000
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)
45 #define FEC_DMA_RX_MINALIGN 64
48 #error "CONFIG_MII has to be defined!"
51 #ifndef CONFIG_FEC_XCV_TYPE
52 #define CONFIG_FEC_XCV_TYPE MII100
56 * The i.MX28 operates with packets in big endian. We need to swap them before
57 * sending and after receiving.
60 #define CONFIG_FEC_MXC_SWAP_PACKET
63 #define RXDESC_PER_CACHELINE (ARCH_DMA_MINALIGN/sizeof(struct fec_bd))
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!"
70 #if ((PKTALIGN < ARCH_DMA_MINALIGN) || \
71 (PKTALIGN % ARCH_DMA_MINALIGN != 0))
72 #error "PKTALIGN must be multiple of ARCH_DMA_MINALIGN!"
77 #ifdef CONFIG_FEC_MXC_SWAP_PACKET
78 static void swap_packet(uint32_t *packet, int length)
82 for (i = 0; i < DIV_ROUND_UP(length, 4); i++)
83 packet[i] = __swab32(packet[i]);
87 /* MII-interface related functions */
88 static int fec_mdio_read(struct ethernet_regs *eth, uint8_t phyaddr,
91 uint32_t reg; /* convenient holder for the PHY register */
92 uint32_t phy; /* convenient holder for the PHY */
97 * reading from any PHY's register is done by properly
98 * programming the FEC's MII data register.
100 writel(FEC_IEVENT_MII, ð->ievent);
101 reg = regaddr << FEC_MII_DATA_RA_SHIFT;
102 phy = phyaddr << FEC_MII_DATA_PA_SHIFT;
104 writel(FEC_MII_DATA_ST | FEC_MII_DATA_OP_RD | FEC_MII_DATA_TA |
105 phy | reg, ð->mii_data);
107 /* wait for the related interrupt */
108 start = get_timer(0);
109 while (!(readl(ð->ievent) & FEC_IEVENT_MII)) {
110 if (get_timer(start) > (CONFIG_SYS_HZ / 1000)) {
111 printf("Read MDIO failed...\n");
116 /* clear mii interrupt bit */
117 writel(FEC_IEVENT_MII, ð->ievent);
119 /* it's now safe to read the PHY's register */
120 val = (unsigned short)readl(ð->mii_data);
121 debug("%s: phy: %02x reg:%02x val:%#x\n", __func__, phyaddr,
126 #ifndef imx_get_fecclk
127 u32 __weak imx_get_fecclk(void)
133 static int fec_get_clk_rate(void *udev, int idx)
135 struct fec_priv *fec;
139 if (IS_ENABLED(CONFIG_IMX8) ||
140 CONFIG_IS_ENABLED(CLK_CCF)) {
143 ret = uclass_get_device(UCLASS_ETH, idx, &dev);
145 debug("Can't get FEC udev: %d\n", ret);
150 fec = dev_get_priv(dev);
152 return fec->clk_rate;
156 return imx_get_fecclk();
160 static void fec_mii_setspeed(struct ethernet_regs *eth)
163 * Set MII_SPEED = (1/(mii_speed * 2)) * System Clock
164 * and do not drop the Preamble.
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
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
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.
182 ret = fec_get_clk_rate(NULL, 0);
184 printf("Can't find FEC0 clk rate: %d\n", ret);
188 speed = DIV_ROUND_UP(pclk, 5000000);
189 hold = DIV_ROUND_UP(pclk, 100000000) - 1;
191 #ifdef FEC_QUIRK_ENET_MAC
194 writel(speed << 1 | hold << 8, ð->mii_speed);
195 debug("%s: mii_speed %08x\n", __func__, readl(ð->mii_speed));
198 static int fec_mdio_write(struct ethernet_regs *eth, uint8_t phyaddr,
199 uint8_t regaddr, uint16_t data)
201 uint32_t reg; /* convenient holder for the PHY register */
202 uint32_t phy; /* convenient holder for the PHY */
205 reg = regaddr << FEC_MII_DATA_RA_SHIFT;
206 phy = phyaddr << FEC_MII_DATA_PA_SHIFT;
208 writel(FEC_MII_DATA_ST | FEC_MII_DATA_OP_WR |
209 FEC_MII_DATA_TA | phy | reg | data, ð->mii_data);
211 /* wait for the MII interrupt */
212 start = get_timer(0);
213 while (!(readl(ð->ievent) & FEC_IEVENT_MII)) {
214 if (get_timer(start) > (CONFIG_SYS_HZ / 1000)) {
215 printf("Write MDIO failed...\n");
220 /* clear MII interrupt bit */
221 writel(FEC_IEVENT_MII, ð->ievent);
222 debug("%s: phy: %02x reg:%02x val:%#x\n", __func__, phyaddr,
228 static int fec_phy_read(struct mii_dev *bus, int phyaddr, int dev_addr,
231 return fec_mdio_read(bus->priv, phyaddr, regaddr);
234 static int fec_phy_write(struct mii_dev *bus, int phyaddr, int dev_addr,
235 int regaddr, u16 data)
237 return fec_mdio_write(bus->priv, phyaddr, regaddr, data);
240 #ifndef CONFIG_PHYLIB
241 static int miiphy_restart_aneg(struct eth_device *dev)
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;
249 * Wake up from sleep if necessary
250 * Reset PHY, then delay 300ns
253 fec_mdio_write(eth, fec->phy_id, MII_DCOUNTER, 0x00FF);
255 fec_mdio_write(eth, fec->phy_id, MII_BMCR, BMCR_RESET);
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);
265 if (fec->mii_postcall)
266 ret = fec->mii_postcall(fec->phy_id);
272 #ifndef CONFIG_FEC_FIXED_SPEED
273 static int miiphy_wait_aneg(struct eth_device *dev)
277 struct fec_priv *fec = (struct fec_priv *)dev->priv;
278 struct ethernet_regs *eth = fec->bus->priv;
280 /* Wait for AN completion */
281 start = get_timer(0);
283 if (get_timer(start) > (CONFIG_SYS_HZ * 5)) {
284 printf("%s: Autonegotiation timeout\n", dev->name);
288 status = fec_mdio_read(eth, fec->phy_id, MII_BMSR);
290 printf("%s: Autonegotiation failed. status: %d\n",
294 } while (!(status & BMSR_LSTATUS));
298 #endif /* CONFIG_FEC_FIXED_SPEED */
301 static int fec_rx_task_enable(struct fec_priv *fec)
303 writel(FEC_R_DES_ACTIVE_RDAR, &fec->eth->r_des_active);
307 static int fec_rx_task_disable(struct fec_priv *fec)
312 static int fec_tx_task_enable(struct fec_priv *fec)
314 writel(FEC_X_DES_ACTIVE_TDAR, &fec->eth->x_des_active);
318 static int fec_tx_task_disable(struct fec_priv *fec)
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
330 * Init all RX descriptors to default values.
332 static void fec_rbd_init(struct fec_priv *fec, int count, int dsize)
339 * Reload the RX descriptors with default values and wipe
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);
348 fec->rbd_base[i].status = FEC_RBD_EMPTY;
349 fec->rbd_base[i].data_length = 0;
352 /* Mark the last RBD to close the ring. */
353 fec->rbd_base[i - 1].status = FEC_RBD_WRAP | FEC_RBD_EMPTY;
356 flush_dcache_range((ulong)fec->rbd_base,
357 (ulong)fec->rbd_base + size);
361 * Initialize transmit task's buffer descriptors
362 * @param[in] fec all we know about the device yet
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.
372 static void fec_tbd_init(struct fec_priv *fec)
374 ulong addr = (ulong)fec->tbd_base;
375 unsigned size = roundup(2 * sizeof(struct fec_bd),
378 memset(fec->tbd_base, 0, size);
379 fec->tbd_base[0].status = 0;
380 fec->tbd_base[1].status = FEC_TBD_WRAP;
382 flush_dcache_range(addr, addr + size);
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
390 static void fec_rbd_clean(int last, struct fec_bd *prbd)
392 unsigned short flags = FEC_RBD_EMPTY;
394 flags |= FEC_RBD_WRAP;
395 writew(flags, &prbd->status);
396 writew(0, &prbd->data_length);
399 static int fec_get_hwaddr(int dev_id, unsigned char *mac)
401 imx_get_mac_from_fuse(dev_id, mac);
402 return !is_valid_ethaddr(mac);
406 static int fecmxc_set_hwaddr(struct udevice *dev)
408 static int fec_set_hwaddr(struct eth_device *dev)
412 struct fec_priv *fec = dev_get_priv(dev);
413 struct eth_pdata *pdata = dev_get_platdata(dev);
414 uchar *mac = pdata->enetaddr;
416 uchar *mac = dev->enetaddr;
417 struct fec_priv *fec = (struct fec_priv *)dev->priv;
420 writel(0, &fec->eth->iaddr1);
421 writel(0, &fec->eth->iaddr2);
422 writel(0, &fec->eth->gaddr1);
423 writel(0, &fec->eth->gaddr2);
425 /* Set physical address */
426 writel((mac[0] << 24) + (mac[1] << 16) + (mac[2] << 8) + mac[3],
428 writel((mac[4] << 24) + (mac[5] << 16) + 0x8808, &fec->eth->paddr2);
433 /* Do initial configuration of the FEC registers */
434 static void fec_reg_setup(struct fec_priv *fec)
438 /* Set interrupt mask register */
439 writel(0x00000000, &fec->eth->imask);
441 /* Clear FEC-Lite interrupt event register(IEVENT) */
442 writel(0xffffffff, &fec->eth->ievent);
444 /* Set FEC-Lite receive control register(R_CNTRL): */
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;
455 writel(rcntrl, &fec->eth->r_cntrl);
459 * Start the FEC engine
460 * @param[in] dev Our device to handle
463 static int fec_open(struct udevice *dev)
465 static int fec_open(struct eth_device *edev)
469 struct fec_priv *fec = dev_get_priv(dev);
471 struct fec_priv *fec = (struct fec_priv *)edev->priv;
477 debug("fec_open: fec_open(dev)\n");
478 /* full-duplex, heartbeat disabled */
479 writel(1 << 2, &fec->eth->x_cntrl);
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]);
487 /* Flush the descriptors into RAM */
488 size = roundup(FEC_RBD_NUM * sizeof(struct fec_bd),
490 addr = (ulong)fec->rbd_base;
491 flush_dcache_range(addr, addr + size);
493 #ifdef FEC_QUIRK_ENET_MAC
494 /* Enable ENET HW endian SWAP */
495 writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_DBSWAP,
497 /* Enable ENET store and forward mode */
498 writel(readl(&fec->eth->x_wmrk) | FEC_X_WMRK_STRFWD,
501 /* Enable FEC-Lite controller */
502 writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_ETHER_EN,
505 #if defined(CONFIG_MX25) || defined(CONFIG_MX53) || defined(CONFIG_MX6SL)
508 /* setup the MII gasket for RMII mode */
509 /* disable the gasket */
510 writew(0, &fec->eth->miigsk_enr);
512 /* wait for the gasket to be disabled */
513 while (readw(&fec->eth->miigsk_enr) & MIIGSK_ENR_READY)
516 /* configure gasket for RMII, 50 MHz, no loopback, and no echo */
517 writew(MIIGSK_CFGR_IF_MODE_RMII, &fec->eth->miigsk_cfgr);
519 /* re-enable the gasket */
520 writew(MIIGSK_ENR_EN, &fec->eth->miigsk_enr);
522 /* wait until MII gasket is ready */
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");
534 /* Start up the PHY */
535 int ret = phy_startup(fec->phydev);
538 printf("Could not initialize PHY %s\n",
539 fec->phydev->dev->name);
542 speed = fec->phydev->speed;
544 #elif CONFIG_FEC_FIXED_SPEED
545 speed = CONFIG_FEC_FIXED_SPEED;
547 miiphy_wait_aneg(edev);
548 speed = miiphy_speed(edev->name, fec->phy_id);
549 miiphy_duplex(edev->name, fec->phy_id);
552 #ifdef FEC_QUIRK_ENET_MAC
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);
564 debug("%s:Speed=%i\n", __func__, speed);
566 /* Enable SmartDMA receive task */
567 fec_rx_task_enable(fec);
574 static int fecmxc_init(struct udevice *dev)
576 static int fec_init(struct eth_device *dev, bd_t *bd)
580 struct fec_priv *fec = dev_get_priv(dev);
582 struct fec_priv *fec = (struct fec_priv *)dev->priv;
584 u8 *mib_ptr = (uint8_t *)&fec->eth->rmon_t_drop;
588 /* Initialize MAC address */
590 fecmxc_set_hwaddr(dev);
595 /* Setup transmit descriptors, there are two in total. */
598 /* Setup receive descriptors. */
599 fec_rbd_init(fec, FEC_RBD_NUM, FEC_MAX_PKT_SIZE);
603 if (fec->xcv_type != SEVENWIRE)
604 fec_mii_setspeed(fec->bus->priv);
606 /* Set Opcode/Pause Duration Register */
607 writel(0x00010020, &fec->eth->op_pause); /* FIXME 0xffff0020; */
608 writel(0x2, &fec->eth->x_wmrk);
610 /* Set multicast address filter */
611 writel(0x00000000, &fec->eth->gaddr1);
612 writel(0x00000000, &fec->eth->gaddr2);
614 /* Do not access reserved register */
615 if (!is_mx6ul() && !is_mx6ull() && !is_imx8() && !is_imx8m()) {
617 for (i = mib_ptr; i <= mib_ptr + 0xfc; i += 4)
620 /* FIFO receive start register */
621 writel(0x520, &fec->eth->r_fstart);
624 /* size and address of each buffer */
625 writel(FEC_MAX_PKT_SIZE, &fec->eth->emrbr);
627 addr = (ulong)fec->tbd_base;
628 writel((uint32_t)addr, &fec->eth->etdsr);
630 addr = (ulong)fec->rbd_base;
631 writel((uint32_t)addr, &fec->eth->erdsr);
633 #ifndef CONFIG_PHYLIB
634 if (fec->xcv_type != SEVENWIRE)
635 miiphy_restart_aneg(dev);
642 * Halt the FEC engine
643 * @param[in] dev Our device to handle
646 static void fecmxc_halt(struct udevice *dev)
648 static void fec_halt(struct eth_device *dev)
652 struct fec_priv *fec = dev_get_priv(dev);
654 struct fec_priv *fec = (struct fec_priv *)dev->priv;
656 int counter = 0xffff;
658 /* issue graceful stop command to the FEC transmitter if necessary */
659 writel(FEC_TCNTRL_GTS | readl(&fec->eth->x_cntrl),
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)))
667 /* Disable SmartDMA tasks */
668 fec_tx_task_disable(fec);
669 fec_rx_task_disable(fec);
672 * Disable the Ethernet Controller
673 * Note: this will also reset the BD index counter!
675 writel(readl(&fec->eth->ecntrl) & ~FEC_ECNTRL_ETHER_EN,
679 debug("eth_halt: done\n");
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
690 static int fecmxc_send(struct udevice *dev, void *packet, int length)
692 static int fec_send(struct eth_device *dev, void *packet, int length)
698 int timeout = FEC_XFER_TIMEOUT;
702 * This routine transmits one frame. This routine only accepts
703 * 6-byte Ethernet addresses.
706 struct fec_priv *fec = dev_get_priv(dev);
708 struct fec_priv *fec = (struct fec_priv *)dev->priv;
712 * Check for valid length of data.
714 if ((length > 1500) || (length <= 0)) {
715 printf("Payload (%d) too large\n", length);
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.
724 #ifdef CONFIG_FEC_MXC_SWAP_PACKET
725 swap_packet((uint32_t *)packet, length);
728 addr = (ulong)packet;
729 end = roundup(addr + length, ARCH_DMA_MINALIGN);
730 addr &= ~(ARCH_DMA_MINALIGN - 1);
731 flush_dcache_range(addr, end);
733 writew(length, &fec->tbd_base[fec->tbd_index].data_length);
734 writel((uint32_t)addr, &fec->tbd_base[fec->tbd_index].data_pointer);
737 * update BD's status now
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)
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);
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
753 size = roundup(2 * sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
754 addr = (ulong)fec->tbd_base;
755 flush_dcache_range(addr, addr + size);
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
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:
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
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.
777 readl(addr + size - 4);
779 /* Enable SmartDMA transmit task */
780 fec_tx_task_enable(fec);
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
788 if (!(readl(&fec->eth->x_des_active) & FEC_X_DES_ACTIVE_TDAR))
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
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
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
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.
811 timeout = FEC_XFER_TIMEOUT;
813 invalidate_dcache_range(addr, addr + size);
814 if (!(readw(&fec->tbd_base[fec->tbd_index].status) &
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 */
836 * Pull one frame from the card
837 * @param[in] dev Our ethernet device to handle
838 * @return Length of packet read
841 static int fecmxc_recv(struct udevice *dev, int flags, uchar **packetp)
843 static int fec_recv(struct eth_device *dev)
847 struct fec_priv *fec = dev_get_priv(dev);
849 struct fec_priv *fec = (struct fec_priv *)dev->priv;
851 struct fec_bd *rbd = &fec->rbd_base[fec->rbd_index];
852 unsigned long ievent;
853 int frame_length, len = 0;
855 ulong addr, size, end;
859 *packetp = memalign(ARCH_DMA_MINALIGN, FEC_MAX_PKT_SIZE);
861 printf("%s: error allocating packetp\n", __func__);
865 ALLOC_CACHE_ALIGN_BUFFER(uchar, buff, FEC_MAX_PKT_SIZE);
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) {
878 fec_init(dev, fec->bd);
880 printf("some error: 0x%08lx\n", ievent);
883 if (ievent & FEC_IEVENT_HBERR) {
884 /* Heartbeat error */
885 writel(0x00000001 | readl(&fec->eth->x_cntrl),
888 if (ievent & FEC_IEVENT_GRA) {
889 /* Graceful stop complete */
890 if (readl(&fec->eth->x_cntrl) & 0x00000001) {
896 writel(~0x00000001 & readl(&fec->eth->x_cntrl),
901 fec_init(dev, fec->bd);
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.
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.
920 addr &= ~(ARCH_DMA_MINALIGN - 1);
921 size = roundup(sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
922 invalidate_dcache_range(addr, addr + size);
924 bd_status = readw(&rbd->status);
925 debug("fec_recv: status 0x%x\n", bd_status);
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);
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);
944 memcpy(*packetp, (char *)addr, frame_length);
946 memcpy(buff, (char *)addr, frame_length);
947 net_process_received_packet(buff, frame_length);
951 if (bd_status & FEC_RBD_ERR)
952 debug("error frame: 0x%08lx 0x%08x\n",
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
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),
970 flush_dcache_range(addr,
971 addr + ARCH_DMA_MINALIGN);
974 fec_rx_task_enable(fec);
975 fec->rbd_index = (fec->rbd_index + 1) % FEC_RBD_NUM;
977 debug("fec_recv: stop\n");
982 static void fec_set_dev_name(char *dest, int dev_id)
984 sprintf(dest, (dev_id == -1) ? "FEC" : "FEC%i", dev_id);
987 static int fec_alloc_descs(struct fec_priv *fec)
994 /* Allocate TX descriptors. */
995 size = roundup(2 * sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
996 fec->tbd_base = memalign(ARCH_DMA_MINALIGN, size);
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);
1006 memset(fec->rbd_base, 0, size);
1008 /* Allocate RX buffers. */
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);
1015 printf("%s: error allocating rxbuf %d\n", __func__, i);
1019 memset(data, 0, size);
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);
1029 /* Mark the last RBD to close the ring. */
1030 fec->rbd_base[i - 1].status = FEC_RBD_WRAP | FEC_RBD_EMPTY;
1038 for (; i >= 0; i--) {
1039 addr = fec->rbd_base[i].data_pointer;
1042 free(fec->rbd_base);
1044 free(fec->tbd_base);
1049 static void fec_free_descs(struct fec_priv *fec)
1054 for (i = 0; i < FEC_RBD_NUM; i++) {
1055 addr = fec->rbd_base[i].data_pointer;
1058 free(fec->rbd_base);
1059 free(fec->tbd_base);
1062 struct mii_dev *fec_get_miibus(ulong base_addr, int dev_id)
1064 struct ethernet_regs *eth = (struct ethernet_regs *)base_addr;
1065 struct mii_dev *bus;
1070 printf("mdio_alloc failed\n");
1073 bus->read = fec_phy_read;
1074 bus->write = fec_phy_write;
1076 fec_set_dev_name(bus->name, dev_id);
1078 ret = mdio_register(bus);
1080 printf("mdio_register failed\n");
1084 fec_mii_setspeed(eth);
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)
1093 static int fec_probe(bd_t *bd, int dev_id, uint32_t base_addr,
1094 struct mii_dev *bus, int phy_id)
1097 struct eth_device *edev;
1098 struct fec_priv *fec;
1099 unsigned char ethaddr[6];
1104 /* create and fill edev struct */
1105 edev = (struct eth_device *)malloc(sizeof(struct eth_device));
1107 puts("fec_mxc: not enough malloc memory for eth_device\n");
1112 fec = (struct fec_priv *)malloc(sizeof(struct fec_priv));
1114 puts("fec_mxc: not enough malloc memory for fec_priv\n");
1119 memset(edev, 0, sizeof(*edev));
1120 memset(fec, 0, sizeof(*fec));
1122 ret = fec_alloc_descs(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;
1133 fec->eth = (struct ethernet_regs *)(ulong)base_addr;
1136 fec->xcv_type = CONFIG_FEC_XCV_TYPE;
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");
1150 fec_set_dev_name(edev->name, dev_id);
1151 fec->dev_id = (dev_id == -1) ? 0 : dev_id;
1153 fec_mii_setspeed(bus->priv);
1154 #ifdef CONFIG_PHYLIB
1155 fec->phydev = phydev;
1156 phy_connect_dev(phydev, edev);
1160 fec->phy_id = phy_id;
1163 /* only support one eth device, the index number pointed by dev_id */
1164 edev->index = fec->dev_id;
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);
1170 sprintf(mac, "eth%daddr", fec->dev_id);
1172 strcpy(mac, "ethaddr");
1174 eth_env_set_enetaddr(mac, ethaddr);
1178 fec_free_descs(fec);
1187 int fecmxc_initialize_multi(bd_t *bd, int dev_id, int phy_id, uint32_t addr)
1190 struct mii_dev *bus = NULL;
1191 #ifdef CONFIG_PHYLIB
1192 struct phy_device *phydev = NULL;
1196 #ifdef CONFIG_FEC_MXC_MDIO_BASE
1198 * The i.MX28 has two ethernet interfaces, but they are not equal.
1199 * Only the first one can access the MDIO bus.
1201 base_mii = CONFIG_FEC_MXC_MDIO_BASE;
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);
1209 #ifdef CONFIG_PHYLIB
1210 phydev = phy_find_by_mask(bus, 1 << phy_id, PHY_INTERFACE_MODE_RGMII);
1212 mdio_unregister(bus);
1216 ret = fec_probe(bd, dev_id, addr, bus, phydev);
1218 ret = fec_probe(bd, dev_id, addr, bus, phy_id);
1221 #ifdef CONFIG_PHYLIB
1224 mdio_unregister(bus);
1230 #ifdef CONFIG_FEC_MXC_PHYADDR
1231 int fecmxc_initialize(bd_t *bd)
1233 return fecmxc_initialize_multi(bd, -1, CONFIG_FEC_MXC_PHYADDR,
1238 #ifndef CONFIG_PHYLIB
1239 int fecmxc_register_mii_postcall(struct eth_device *dev, int (*cb)(int))
1241 struct fec_priv *fec = (struct fec_priv *)dev->priv;
1242 fec->mii_postcall = cb;
1249 static int fecmxc_read_rom_hwaddr(struct udevice *dev)
1251 struct fec_priv *priv = dev_get_priv(dev);
1252 struct eth_pdata *pdata = dev_get_platdata(dev);
1254 return fec_get_hwaddr(priv->dev_id, pdata->enetaddr);
1257 static int fecmxc_free_pkt(struct udevice *dev, uchar *packet, int length)
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,
1275 static int device_get_phy_addr(struct udevice *dev)
1277 struct ofnode_phandle_args phandle_args;
1280 if (dev_read_phandle_with_args(dev, "phy-handle", NULL, 0, 0,
1282 debug("Failed to find phy-handle");
1286 reg = ofnode_read_u32_default(phandle_args.node, "reg", 0);
1291 static int fec_phy_init(struct fec_priv *priv, struct udevice *dev)
1293 struct phy_device *phydev;
1296 addr = device_get_phy_addr(dev);
1297 #ifdef CONFIG_FEC_MXC_PHYADDR
1298 addr = CONFIG_FEC_MXC_PHYADDR;
1301 phydev = phy_connect(priv->bus, addr, dev, priv->interface);
1305 priv->phydev = phydev;
1311 #ifdef CONFIG_DM_GPIO
1312 /* FEC GPIO reset */
1313 static void fec_gpio_reset(struct fec_priv *priv)
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);
1326 static int fecmxc_probe(struct udevice *dev)
1328 struct eth_pdata *pdata = dev_get_platdata(dev);
1329 struct fec_priv *priv = dev_get_priv(dev);
1330 struct mii_dev *bus = NULL;
1334 if (IS_ENABLED(CONFIG_IMX8)) {
1335 ret = clk_get_by_name(dev, "ipg", &priv->ipg_clk);
1337 debug("Can't get FEC ipg clk: %d\n", ret);
1340 ret = clk_enable(&priv->ipg_clk);
1342 debug("Can't enable FEC ipg clk: %d\n", ret);
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);
1350 debug("Can't get FEC ipg clk: %d\n", ret);
1353 ret = clk_enable(&priv->ipg_clk);
1357 ret = clk_get_by_name(dev, "ahb", &priv->ahb_clk);
1359 debug("Can't get FEC ahb clk: %d\n", ret);
1362 ret = clk_enable(&priv->ahb_clk);
1366 ret = clk_get_by_name(dev, "enet_out", &priv->clk_enet_out);
1368 ret = clk_enable(&priv->clk_enet_out);
1373 ret = clk_get_by_name(dev, "enet_clk_ref", &priv->clk_ref);
1375 ret = clk_enable(&priv->clk_ref);
1380 ret = clk_get_by_name(dev, "ptp", &priv->clk_ptp);
1382 ret = clk_enable(&priv->clk_ptp);
1387 priv->clk_rate = clk_get_rate(&priv->ipg_clk);
1390 ret = fec_alloc_descs(priv);
1394 #ifdef CONFIG_DM_REGULATOR
1395 if (priv->phy_supply) {
1396 ret = regulator_set_enable(priv->phy_supply, true);
1398 printf("%s: Error enabling phy supply\n", dev->name);
1404 #ifdef CONFIG_DM_GPIO
1405 fec_gpio_reset(priv);
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");
1419 fec_reg_setup(priv);
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);
1425 bus = fec_get_miibus((ulong)priv->eth, dev->seq);
1433 priv->interface = pdata->phy_interface;
1434 switch (priv->interface) {
1435 case PHY_INTERFACE_MODE_MII:
1436 priv->xcv_type = MII100;
1438 case PHY_INTERFACE_MODE_RMII:
1439 priv->xcv_type = RMII;
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;
1448 priv->xcv_type = CONFIG_FEC_XCV_TYPE;
1449 printf("Unsupported interface type %d defaulting to %d\n",
1450 priv->interface, priv->xcv_type);
1454 ret = fec_phy_init(priv, dev);
1461 mdio_unregister(bus);
1465 fec_free_descs(priv);
1469 static int fecmxc_remove(struct udevice *dev)
1471 struct fec_priv *priv = dev_get_priv(dev);
1474 fec_free_descs(priv);
1475 mdio_unregister(priv->bus);
1476 mdio_free(priv->bus);
1478 #ifdef CONFIG_DM_REGULATOR
1479 if (priv->phy_supply)
1480 regulator_set_enable(priv->phy_supply, false);
1486 static int fecmxc_ofdata_to_platdata(struct udevice *dev)
1489 struct eth_pdata *pdata = dev_get_platdata(dev);
1490 struct fec_priv *priv = dev_get_priv(dev);
1491 const char *phy_mode;
1493 pdata->iobase = (phys_addr_t)devfdt_get_addr(dev);
1494 priv->eth = (struct ethernet_regs *)pdata->iobase;
1496 pdata->phy_interface = -1;
1497 phy_mode = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "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);
1506 #ifdef CONFIG_DM_REGULATOR
1507 device_get_supply_regulator(dev, "phy-supply", &priv->phy_supply);
1510 #ifdef CONFIG_DM_GPIO
1511 ret = gpio_request_by_name(dev, "phy-reset-gpios", 0,
1512 &priv->phy_reset_gpio, GPIOD_IS_OUT);
1514 return 0; /* property is optional, don't return error! */
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;
1523 priv->reset_post_delay = dev_read_u32_default(dev,
1524 "phy-reset-post-delay",
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;
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" },
1548 U_BOOT_DRIVER(fecmxc_gem) = {
1551 .of_match = fecmxc_ids,
1552 .ofdata_to_platdata = fecmxc_ofdata_to_platdata,
1553 .probe = fecmxc_probe,
1554 .remove = fecmxc_remove,
1556 .priv_auto_alloc_size = sizeof(struct fec_priv),
1557 .platdata_auto_alloc_size = sizeof(struct eth_pdata),