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>
12 #include <environment.h>
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 static int fec_get_clk_rate(void *udev, int idx)
128 #if IS_ENABLED(CONFIG_IMX8)
129 struct fec_priv *fec;
135 ret = uclass_get_device(UCLASS_ETH, idx, &dev);
137 debug("Can't get FEC udev: %d\n", ret);
142 fec = dev_get_priv(dev);
144 return fec->clk_rate;
148 return imx_get_fecclk();
152 static void fec_mii_setspeed(struct ethernet_regs *eth)
155 * Set MII_SPEED = (1/(mii_speed * 2)) * System Clock
156 * and do not drop the Preamble.
158 * The i.MX28 and i.MX6 types have another field in the MSCR (aka
159 * MII_SPEED) register that defines the MDIO output hold time. Earlier
160 * versions are RAZ there, so just ignore the difference and write the
162 * The minimal hold time according to IEE802.3 (clause 22) is 10 ns.
163 * HOLDTIME + 1 is the number of clk cycles the fec is holding the
165 * The HOLDTIME bitfield takes values between 0 and 7 (inclusive).
166 * Given that ceil(clkrate / 5000000) <= 64, the calculation for
167 * holdtime cannot result in a value greater than 3.
174 ret = fec_get_clk_rate(NULL, 0);
176 printf("Can't find FEC0 clk rate: %d\n", ret);
180 speed = DIV_ROUND_UP(pclk, 5000000);
181 hold = DIV_ROUND_UP(pclk, 100000000) - 1;
183 #ifdef FEC_QUIRK_ENET_MAC
186 writel(speed << 1 | hold << 8, ð->mii_speed);
187 debug("%s: mii_speed %08x\n", __func__, readl(ð->mii_speed));
190 static int fec_mdio_write(struct ethernet_regs *eth, uint8_t phyaddr,
191 uint8_t regaddr, uint16_t data)
193 uint32_t reg; /* convenient holder for the PHY register */
194 uint32_t phy; /* convenient holder for the PHY */
197 reg = regaddr << FEC_MII_DATA_RA_SHIFT;
198 phy = phyaddr << FEC_MII_DATA_PA_SHIFT;
200 writel(FEC_MII_DATA_ST | FEC_MII_DATA_OP_WR |
201 FEC_MII_DATA_TA | phy | reg | data, ð->mii_data);
203 /* wait for the MII interrupt */
204 start = get_timer(0);
205 while (!(readl(ð->ievent) & FEC_IEVENT_MII)) {
206 if (get_timer(start) > (CONFIG_SYS_HZ / 1000)) {
207 printf("Write MDIO failed...\n");
212 /* clear MII interrupt bit */
213 writel(FEC_IEVENT_MII, ð->ievent);
214 debug("%s: phy: %02x reg:%02x val:%#x\n", __func__, phyaddr,
220 static int fec_phy_read(struct mii_dev *bus, int phyaddr, int dev_addr,
223 return fec_mdio_read(bus->priv, phyaddr, regaddr);
226 static int fec_phy_write(struct mii_dev *bus, int phyaddr, int dev_addr,
227 int regaddr, u16 data)
229 return fec_mdio_write(bus->priv, phyaddr, regaddr, data);
232 #ifndef CONFIG_PHYLIB
233 static int miiphy_restart_aneg(struct eth_device *dev)
236 #if !defined(CONFIG_FEC_MXC_NO_ANEG)
237 struct fec_priv *fec = (struct fec_priv *)dev->priv;
238 struct ethernet_regs *eth = fec->bus->priv;
241 * Wake up from sleep if necessary
242 * Reset PHY, then delay 300ns
245 fec_mdio_write(eth, fec->phy_id, MII_DCOUNTER, 0x00FF);
247 fec_mdio_write(eth, fec->phy_id, MII_BMCR, BMCR_RESET);
250 /* Set the auto-negotiation advertisement register bits */
251 fec_mdio_write(eth, fec->phy_id, MII_ADVERTISE,
252 LPA_100FULL | LPA_100HALF | LPA_10FULL |
253 LPA_10HALF | PHY_ANLPAR_PSB_802_3);
254 fec_mdio_write(eth, fec->phy_id, MII_BMCR,
255 BMCR_ANENABLE | BMCR_ANRESTART);
257 if (fec->mii_postcall)
258 ret = fec->mii_postcall(fec->phy_id);
264 #ifndef CONFIG_FEC_FIXED_SPEED
265 static int miiphy_wait_aneg(struct eth_device *dev)
269 struct fec_priv *fec = (struct fec_priv *)dev->priv;
270 struct ethernet_regs *eth = fec->bus->priv;
272 /* Wait for AN completion */
273 start = get_timer(0);
275 if (get_timer(start) > (CONFIG_SYS_HZ * 5)) {
276 printf("%s: Autonegotiation timeout\n", dev->name);
280 status = fec_mdio_read(eth, fec->phy_id, MII_BMSR);
282 printf("%s: Autonegotiation failed. status: %d\n",
286 } while (!(status & BMSR_LSTATUS));
290 #endif /* CONFIG_FEC_FIXED_SPEED */
293 static int fec_rx_task_enable(struct fec_priv *fec)
295 writel(FEC_R_DES_ACTIVE_RDAR, &fec->eth->r_des_active);
299 static int fec_rx_task_disable(struct fec_priv *fec)
304 static int fec_tx_task_enable(struct fec_priv *fec)
306 writel(FEC_X_DES_ACTIVE_TDAR, &fec->eth->x_des_active);
310 static int fec_tx_task_disable(struct fec_priv *fec)
316 * Initialize receive task's buffer descriptors
317 * @param[in] fec all we know about the device yet
318 * @param[in] count receive buffer count to be allocated
319 * @param[in] dsize desired size of each receive buffer
320 * @return 0 on success
322 * Init all RX descriptors to default values.
324 static void fec_rbd_init(struct fec_priv *fec, int count, int dsize)
331 * Reload the RX descriptors with default values and wipe
334 size = roundup(dsize, ARCH_DMA_MINALIGN);
335 for (i = 0; i < count; i++) {
336 data = fec->rbd_base[i].data_pointer;
337 memset((void *)data, 0, dsize);
338 flush_dcache_range(data, data + size);
340 fec->rbd_base[i].status = FEC_RBD_EMPTY;
341 fec->rbd_base[i].data_length = 0;
344 /* Mark the last RBD to close the ring. */
345 fec->rbd_base[i - 1].status = FEC_RBD_WRAP | FEC_RBD_EMPTY;
348 flush_dcache_range((ulong)fec->rbd_base,
349 (ulong)fec->rbd_base + size);
353 * Initialize transmit task's buffer descriptors
354 * @param[in] fec all we know about the device yet
356 * Transmit buffers are created externally. We only have to init the BDs here.\n
357 * Note: There is a race condition in the hardware. When only one BD is in
358 * use it must be marked with the WRAP bit to use it for every transmitt.
359 * This bit in combination with the READY bit results into double transmit
360 * of each data buffer. It seems the state machine checks READY earlier then
361 * resetting it after the first transfer.
362 * Using two BDs solves this issue.
364 static void fec_tbd_init(struct fec_priv *fec)
366 ulong addr = (ulong)fec->tbd_base;
367 unsigned size = roundup(2 * sizeof(struct fec_bd),
370 memset(fec->tbd_base, 0, size);
371 fec->tbd_base[0].status = 0;
372 fec->tbd_base[1].status = FEC_TBD_WRAP;
374 flush_dcache_range(addr, addr + size);
378 * Mark the given read buffer descriptor as free
379 * @param[in] last 1 if this is the last buffer descriptor in the chain, else 0
380 * @param[in] prbd buffer descriptor to mark free again
382 static void fec_rbd_clean(int last, struct fec_bd *prbd)
384 unsigned short flags = FEC_RBD_EMPTY;
386 flags |= FEC_RBD_WRAP;
387 writew(flags, &prbd->status);
388 writew(0, &prbd->data_length);
391 static int fec_get_hwaddr(int dev_id, unsigned char *mac)
393 imx_get_mac_from_fuse(dev_id, mac);
394 return !is_valid_ethaddr(mac);
398 static int fecmxc_set_hwaddr(struct udevice *dev)
400 static int fec_set_hwaddr(struct eth_device *dev)
404 struct fec_priv *fec = dev_get_priv(dev);
405 struct eth_pdata *pdata = dev_get_platdata(dev);
406 uchar *mac = pdata->enetaddr;
408 uchar *mac = dev->enetaddr;
409 struct fec_priv *fec = (struct fec_priv *)dev->priv;
412 writel(0, &fec->eth->iaddr1);
413 writel(0, &fec->eth->iaddr2);
414 writel(0, &fec->eth->gaddr1);
415 writel(0, &fec->eth->gaddr2);
417 /* Set physical address */
418 writel((mac[0] << 24) + (mac[1] << 16) + (mac[2] << 8) + mac[3],
420 writel((mac[4] << 24) + (mac[5] << 16) + 0x8808, &fec->eth->paddr2);
425 /* Do initial configuration of the FEC registers */
426 static void fec_reg_setup(struct fec_priv *fec)
430 /* Set interrupt mask register */
431 writel(0x00000000, &fec->eth->imask);
433 /* Clear FEC-Lite interrupt event register(IEVENT) */
434 writel(0xffffffff, &fec->eth->ievent);
436 /* Set FEC-Lite receive control register(R_CNTRL): */
438 /* Start with frame length = 1518, common for all modes. */
439 rcntrl = PKTSIZE << FEC_RCNTRL_MAX_FL_SHIFT;
440 if (fec->xcv_type != SEVENWIRE) /* xMII modes */
441 rcntrl |= FEC_RCNTRL_FCE | FEC_RCNTRL_MII_MODE;
442 if (fec->xcv_type == RGMII)
443 rcntrl |= FEC_RCNTRL_RGMII;
444 else if (fec->xcv_type == RMII)
445 rcntrl |= FEC_RCNTRL_RMII;
447 writel(rcntrl, &fec->eth->r_cntrl);
451 * Start the FEC engine
452 * @param[in] dev Our device to handle
455 static int fec_open(struct udevice *dev)
457 static int fec_open(struct eth_device *edev)
461 struct fec_priv *fec = dev_get_priv(dev);
463 struct fec_priv *fec = (struct fec_priv *)edev->priv;
469 debug("fec_open: fec_open(dev)\n");
470 /* full-duplex, heartbeat disabled */
471 writel(1 << 2, &fec->eth->x_cntrl);
474 /* Invalidate all descriptors */
475 for (i = 0; i < FEC_RBD_NUM - 1; i++)
476 fec_rbd_clean(0, &fec->rbd_base[i]);
477 fec_rbd_clean(1, &fec->rbd_base[i]);
479 /* Flush the descriptors into RAM */
480 size = roundup(FEC_RBD_NUM * sizeof(struct fec_bd),
482 addr = (ulong)fec->rbd_base;
483 flush_dcache_range(addr, addr + size);
485 #ifdef FEC_QUIRK_ENET_MAC
486 /* Enable ENET HW endian SWAP */
487 writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_DBSWAP,
489 /* Enable ENET store and forward mode */
490 writel(readl(&fec->eth->x_wmrk) | FEC_X_WMRK_STRFWD,
493 /* Enable FEC-Lite controller */
494 writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_ETHER_EN,
497 #if defined(CONFIG_MX25) || defined(CONFIG_MX53) || defined(CONFIG_MX6SL)
500 /* setup the MII gasket for RMII mode */
501 /* disable the gasket */
502 writew(0, &fec->eth->miigsk_enr);
504 /* wait for the gasket to be disabled */
505 while (readw(&fec->eth->miigsk_enr) & MIIGSK_ENR_READY)
508 /* configure gasket for RMII, 50 MHz, no loopback, and no echo */
509 writew(MIIGSK_CFGR_IF_MODE_RMII, &fec->eth->miigsk_cfgr);
511 /* re-enable the gasket */
512 writew(MIIGSK_ENR_EN, &fec->eth->miigsk_enr);
514 /* wait until MII gasket is ready */
516 while ((readw(&fec->eth->miigsk_enr) & MIIGSK_ENR_READY) == 0) {
517 if (--max_loops <= 0) {
518 printf("WAIT for MII Gasket ready timed out\n");
526 /* Start up the PHY */
527 int ret = phy_startup(fec->phydev);
530 printf("Could not initialize PHY %s\n",
531 fec->phydev->dev->name);
534 speed = fec->phydev->speed;
536 #elif CONFIG_FEC_FIXED_SPEED
537 speed = CONFIG_FEC_FIXED_SPEED;
539 miiphy_wait_aneg(edev);
540 speed = miiphy_speed(edev->name, fec->phy_id);
541 miiphy_duplex(edev->name, fec->phy_id);
544 #ifdef FEC_QUIRK_ENET_MAC
546 u32 ecr = readl(&fec->eth->ecntrl) & ~FEC_ECNTRL_SPEED;
547 u32 rcr = readl(&fec->eth->r_cntrl) & ~FEC_RCNTRL_RMII_10T;
548 if (speed == _1000BASET)
549 ecr |= FEC_ECNTRL_SPEED;
550 else if (speed != _100BASET)
551 rcr |= FEC_RCNTRL_RMII_10T;
552 writel(ecr, &fec->eth->ecntrl);
553 writel(rcr, &fec->eth->r_cntrl);
556 debug("%s:Speed=%i\n", __func__, speed);
558 /* Enable SmartDMA receive task */
559 fec_rx_task_enable(fec);
566 static int fecmxc_init(struct udevice *dev)
568 static int fec_init(struct eth_device *dev, bd_t *bd)
572 struct fec_priv *fec = dev_get_priv(dev);
574 struct fec_priv *fec = (struct fec_priv *)dev->priv;
576 u8 *mib_ptr = (uint8_t *)&fec->eth->rmon_t_drop;
580 /* Initialize MAC address */
582 fecmxc_set_hwaddr(dev);
587 /* Setup transmit descriptors, there are two in total. */
590 /* Setup receive descriptors. */
591 fec_rbd_init(fec, FEC_RBD_NUM, FEC_MAX_PKT_SIZE);
595 if (fec->xcv_type != SEVENWIRE)
596 fec_mii_setspeed(fec->bus->priv);
598 /* Set Opcode/Pause Duration Register */
599 writel(0x00010020, &fec->eth->op_pause); /* FIXME 0xffff0020; */
600 writel(0x2, &fec->eth->x_wmrk);
602 /* Set multicast address filter */
603 writel(0x00000000, &fec->eth->gaddr1);
604 writel(0x00000000, &fec->eth->gaddr2);
606 /* Do not access reserved register */
607 if (!is_mx6ul() && !is_mx6ull() && !is_imx8m()) {
609 for (i = mib_ptr; i <= mib_ptr + 0xfc; i += 4)
612 /* FIFO receive start register */
613 writel(0x520, &fec->eth->r_fstart);
616 /* size and address of each buffer */
617 writel(FEC_MAX_PKT_SIZE, &fec->eth->emrbr);
619 addr = (ulong)fec->tbd_base;
620 writel((uint32_t)addr, &fec->eth->etdsr);
622 addr = (ulong)fec->rbd_base;
623 writel((uint32_t)addr, &fec->eth->erdsr);
625 #ifndef CONFIG_PHYLIB
626 if (fec->xcv_type != SEVENWIRE)
627 miiphy_restart_aneg(dev);
634 * Halt the FEC engine
635 * @param[in] dev Our device to handle
638 static void fecmxc_halt(struct udevice *dev)
640 static void fec_halt(struct eth_device *dev)
644 struct fec_priv *fec = dev_get_priv(dev);
646 struct fec_priv *fec = (struct fec_priv *)dev->priv;
648 int counter = 0xffff;
650 /* issue graceful stop command to the FEC transmitter if necessary */
651 writel(FEC_TCNTRL_GTS | readl(&fec->eth->x_cntrl),
654 debug("eth_halt: wait for stop regs\n");
655 /* wait for graceful stop to register */
656 while ((counter--) && (!(readl(&fec->eth->ievent) & FEC_IEVENT_GRA)))
659 /* Disable SmartDMA tasks */
660 fec_tx_task_disable(fec);
661 fec_rx_task_disable(fec);
664 * Disable the Ethernet Controller
665 * Note: this will also reset the BD index counter!
667 writel(readl(&fec->eth->ecntrl) & ~FEC_ECNTRL_ETHER_EN,
671 debug("eth_halt: done\n");
676 * @param[in] dev Our ethernet device to handle
677 * @param[in] packet Pointer to the data to be transmitted
678 * @param[in] length Data count in bytes
679 * @return 0 on success
682 static int fecmxc_send(struct udevice *dev, void *packet, int length)
684 static int fec_send(struct eth_device *dev, void *packet, int length)
690 int timeout = FEC_XFER_TIMEOUT;
694 * This routine transmits one frame. This routine only accepts
695 * 6-byte Ethernet addresses.
698 struct fec_priv *fec = dev_get_priv(dev);
700 struct fec_priv *fec = (struct fec_priv *)dev->priv;
704 * Check for valid length of data.
706 if ((length > 1500) || (length <= 0)) {
707 printf("Payload (%d) too large\n", length);
712 * Setup the transmit buffer. We are always using the first buffer for
713 * transmission, the second will be empty and only used to stop the DMA
714 * engine. We also flush the packet to RAM here to avoid cache trouble.
716 #ifdef CONFIG_FEC_MXC_SWAP_PACKET
717 swap_packet((uint32_t *)packet, length);
720 addr = (ulong)packet;
721 end = roundup(addr + length, ARCH_DMA_MINALIGN);
722 addr &= ~(ARCH_DMA_MINALIGN - 1);
723 flush_dcache_range(addr, end);
725 writew(length, &fec->tbd_base[fec->tbd_index].data_length);
726 writel((uint32_t)addr, &fec->tbd_base[fec->tbd_index].data_pointer);
729 * update BD's status now
731 * - is always the last in a chain (means no chain)
732 * - should transmitt the CRC
733 * - might be the last BD in the list, so the address counter should
734 * wrap (-> keep the WRAP flag)
736 status = readw(&fec->tbd_base[fec->tbd_index].status) & FEC_TBD_WRAP;
737 status |= FEC_TBD_LAST | FEC_TBD_TC | FEC_TBD_READY;
738 writew(status, &fec->tbd_base[fec->tbd_index].status);
741 * Flush data cache. This code flushes both TX descriptors to RAM.
742 * After this code, the descriptors will be safely in RAM and we
745 size = roundup(2 * sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
746 addr = (ulong)fec->tbd_base;
747 flush_dcache_range(addr, addr + size);
750 * Below we read the DMA descriptor's last four bytes back from the
751 * DRAM. This is important in order to make sure that all WRITE
752 * operations on the bus that were triggered by previous cache FLUSH
755 * Otherwise, on MX28, it is possible to observe a corruption of the
756 * DMA descriptors. Please refer to schematic "Figure 1-2" in MX28RM
757 * for the bus structure of MX28. The scenario is as follows:
759 * 1) ARM core triggers a series of WRITEs on the AHB_ARB2 bus going
760 * to DRAM due to flush_dcache_range()
761 * 2) ARM core writes the FEC registers via AHB_ARB2
762 * 3) FEC DMA starts reading/writing from/to DRAM via AHB_ARB3
764 * Note that 2) does sometimes finish before 1) due to reordering of
765 * WRITE accesses on the AHB bus, therefore triggering 3) before the
766 * DMA descriptor is fully written into DRAM. This results in occasional
767 * corruption of the DMA descriptor.
769 readl(addr + size - 4);
771 /* Enable SmartDMA transmit task */
772 fec_tx_task_enable(fec);
775 * Wait until frame is sent. On each turn of the wait cycle, we must
776 * invalidate data cache to see what's really in RAM. Also, we need
780 if (!(readl(&fec->eth->x_des_active) & FEC_X_DES_ACTIVE_TDAR))
790 * The TDAR bit is cleared when the descriptors are all out from TX
791 * but on mx6solox we noticed that the READY bit is still not cleared
793 * These are two distinct signals, and in IC simulation, we found that
794 * TDAR always gets cleared prior than the READY bit of last BD becomes
796 * In mx6solox, we use a later version of FEC IP. It looks like that
797 * this intrinsic behaviour of TDAR bit has changed in this newer FEC
800 * Fix this by polling the READY bit of BD after the TDAR polling,
801 * which covers the mx6solox case and does not harm the other SoCs.
803 timeout = FEC_XFER_TIMEOUT;
805 invalidate_dcache_range(addr, addr + size);
806 if (!(readw(&fec->tbd_base[fec->tbd_index].status) &
815 debug("fec_send: status 0x%x index %d ret %i\n",
816 readw(&fec->tbd_base[fec->tbd_index].status),
817 fec->tbd_index, ret);
818 /* for next transmission use the other buffer */
828 * Pull one frame from the card
829 * @param[in] dev Our ethernet device to handle
830 * @return Length of packet read
833 static int fecmxc_recv(struct udevice *dev, int flags, uchar **packetp)
835 static int fec_recv(struct eth_device *dev)
839 struct fec_priv *fec = dev_get_priv(dev);
841 struct fec_priv *fec = (struct fec_priv *)dev->priv;
843 struct fec_bd *rbd = &fec->rbd_base[fec->rbd_index];
844 unsigned long ievent;
845 int frame_length, len = 0;
847 ulong addr, size, end;
851 *packetp = memalign(ARCH_DMA_MINALIGN, FEC_MAX_PKT_SIZE);
853 printf("%s: error allocating packetp\n", __func__);
857 ALLOC_CACHE_ALIGN_BUFFER(uchar, buff, FEC_MAX_PKT_SIZE);
860 /* Check if any critical events have happened */
861 ievent = readl(&fec->eth->ievent);
862 writel(ievent, &fec->eth->ievent);
863 debug("fec_recv: ievent 0x%lx\n", ievent);
864 if (ievent & FEC_IEVENT_BABR) {
870 fec_init(dev, fec->bd);
872 printf("some error: 0x%08lx\n", ievent);
875 if (ievent & FEC_IEVENT_HBERR) {
876 /* Heartbeat error */
877 writel(0x00000001 | readl(&fec->eth->x_cntrl),
880 if (ievent & FEC_IEVENT_GRA) {
881 /* Graceful stop complete */
882 if (readl(&fec->eth->x_cntrl) & 0x00000001) {
888 writel(~0x00000001 & readl(&fec->eth->x_cntrl),
893 fec_init(dev, fec->bd);
899 * Read the buffer status. Before the status can be read, the data cache
900 * must be invalidated, because the data in RAM might have been changed
901 * by DMA. The descriptors are properly aligned to cachelines so there's
902 * no need to worry they'd overlap.
904 * WARNING: By invalidating the descriptor here, we also invalidate
905 * the descriptors surrounding this one. Therefore we can NOT change the
906 * contents of this descriptor nor the surrounding ones. The problem is
907 * that in order to mark the descriptor as processed, we need to change
908 * the descriptor. The solution is to mark the whole cache line when all
909 * descriptors in the cache line are processed.
912 addr &= ~(ARCH_DMA_MINALIGN - 1);
913 size = roundup(sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
914 invalidate_dcache_range(addr, addr + size);
916 bd_status = readw(&rbd->status);
917 debug("fec_recv: status 0x%x\n", bd_status);
919 if (!(bd_status & FEC_RBD_EMPTY)) {
920 if ((bd_status & FEC_RBD_LAST) && !(bd_status & FEC_RBD_ERR) &&
921 ((readw(&rbd->data_length) - 4) > 14)) {
922 /* Get buffer address and size */
923 addr = readl(&rbd->data_pointer);
924 frame_length = readw(&rbd->data_length) - 4;
925 /* Invalidate data cache over the buffer */
926 end = roundup(addr + frame_length, ARCH_DMA_MINALIGN);
927 addr &= ~(ARCH_DMA_MINALIGN - 1);
928 invalidate_dcache_range(addr, end);
930 /* Fill the buffer and pass it to upper layers */
931 #ifdef CONFIG_FEC_MXC_SWAP_PACKET
932 swap_packet((uint32_t *)addr, frame_length);
936 memcpy(*packetp, (char *)addr, frame_length);
938 memcpy(buff, (char *)addr, frame_length);
939 net_process_received_packet(buff, frame_length);
943 if (bd_status & FEC_RBD_ERR)
944 debug("error frame: 0x%08lx 0x%08x\n",
949 * Free the current buffer, restart the engine and move forward
950 * to the next buffer. Here we check if the whole cacheline of
951 * descriptors was already processed and if so, we mark it free
954 size = RXDESC_PER_CACHELINE - 1;
955 if ((fec->rbd_index & size) == size) {
956 i = fec->rbd_index - size;
957 addr = (ulong)&fec->rbd_base[i];
958 for (; i <= fec->rbd_index ; i++) {
959 fec_rbd_clean(i == (FEC_RBD_NUM - 1),
962 flush_dcache_range(addr,
963 addr + ARCH_DMA_MINALIGN);
966 fec_rx_task_enable(fec);
967 fec->rbd_index = (fec->rbd_index + 1) % FEC_RBD_NUM;
969 debug("fec_recv: stop\n");
974 static void fec_set_dev_name(char *dest, int dev_id)
976 sprintf(dest, (dev_id == -1) ? "FEC" : "FEC%i", dev_id);
979 static int fec_alloc_descs(struct fec_priv *fec)
986 /* Allocate TX descriptors. */
987 size = roundup(2 * sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
988 fec->tbd_base = memalign(ARCH_DMA_MINALIGN, size);
992 /* Allocate RX descriptors. */
993 size = roundup(FEC_RBD_NUM * sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
994 fec->rbd_base = memalign(ARCH_DMA_MINALIGN, size);
998 memset(fec->rbd_base, 0, size);
1000 /* Allocate RX buffers. */
1002 /* Maximum RX buffer size. */
1003 size = roundup(FEC_MAX_PKT_SIZE, FEC_DMA_RX_MINALIGN);
1004 for (i = 0; i < FEC_RBD_NUM; i++) {
1005 data = memalign(FEC_DMA_RX_MINALIGN, size);
1007 printf("%s: error allocating rxbuf %d\n", __func__, i);
1011 memset(data, 0, size);
1014 fec->rbd_base[i].data_pointer = (uint32_t)addr;
1015 fec->rbd_base[i].status = FEC_RBD_EMPTY;
1016 fec->rbd_base[i].data_length = 0;
1017 /* Flush the buffer to memory. */
1018 flush_dcache_range(addr, addr + size);
1021 /* Mark the last RBD to close the ring. */
1022 fec->rbd_base[i - 1].status = FEC_RBD_WRAP | FEC_RBD_EMPTY;
1030 for (; i >= 0; i--) {
1031 addr = fec->rbd_base[i].data_pointer;
1034 free(fec->rbd_base);
1036 free(fec->tbd_base);
1041 static void fec_free_descs(struct fec_priv *fec)
1046 for (i = 0; i < FEC_RBD_NUM; i++) {
1047 addr = fec->rbd_base[i].data_pointer;
1050 free(fec->rbd_base);
1051 free(fec->tbd_base);
1054 struct mii_dev *fec_get_miibus(ulong base_addr, int dev_id)
1056 struct ethernet_regs *eth = (struct ethernet_regs *)base_addr;
1057 struct mii_dev *bus;
1062 printf("mdio_alloc failed\n");
1065 bus->read = fec_phy_read;
1066 bus->write = fec_phy_write;
1068 fec_set_dev_name(bus->name, dev_id);
1070 ret = mdio_register(bus);
1072 printf("mdio_register failed\n");
1076 fec_mii_setspeed(eth);
1080 #ifndef CONFIG_DM_ETH
1081 #ifdef CONFIG_PHYLIB
1082 int fec_probe(bd_t *bd, int dev_id, uint32_t base_addr,
1083 struct mii_dev *bus, struct phy_device *phydev)
1085 static int fec_probe(bd_t *bd, int dev_id, uint32_t base_addr,
1086 struct mii_dev *bus, int phy_id)
1089 struct eth_device *edev;
1090 struct fec_priv *fec;
1091 unsigned char ethaddr[6];
1096 /* create and fill edev struct */
1097 edev = (struct eth_device *)malloc(sizeof(struct eth_device));
1099 puts("fec_mxc: not enough malloc memory for eth_device\n");
1104 fec = (struct fec_priv *)malloc(sizeof(struct fec_priv));
1106 puts("fec_mxc: not enough malloc memory for fec_priv\n");
1111 memset(edev, 0, sizeof(*edev));
1112 memset(fec, 0, sizeof(*fec));
1114 ret = fec_alloc_descs(fec);
1119 edev->init = fec_init;
1120 edev->send = fec_send;
1121 edev->recv = fec_recv;
1122 edev->halt = fec_halt;
1123 edev->write_hwaddr = fec_set_hwaddr;
1125 fec->eth = (struct ethernet_regs *)(ulong)base_addr;
1128 fec->xcv_type = CONFIG_FEC_XCV_TYPE;
1131 writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_RESET, &fec->eth->ecntrl);
1132 start = get_timer(0);
1133 while (readl(&fec->eth->ecntrl) & FEC_ECNTRL_RESET) {
1134 if (get_timer(start) > (CONFIG_SYS_HZ * 5)) {
1135 printf("FEC MXC: Timeout resetting chip\n");
1142 fec_set_dev_name(edev->name, dev_id);
1143 fec->dev_id = (dev_id == -1) ? 0 : dev_id;
1145 fec_mii_setspeed(bus->priv);
1146 #ifdef CONFIG_PHYLIB
1147 fec->phydev = phydev;
1148 phy_connect_dev(phydev, edev);
1152 fec->phy_id = phy_id;
1155 /* only support one eth device, the index number pointed by dev_id */
1156 edev->index = fec->dev_id;
1158 if (fec_get_hwaddr(fec->dev_id, ethaddr) == 0) {
1159 debug("got MAC%d address from fuse: %pM\n", fec->dev_id, ethaddr);
1160 memcpy(edev->enetaddr, ethaddr, 6);
1162 sprintf(mac, "eth%daddr", fec->dev_id);
1164 strcpy(mac, "ethaddr");
1166 eth_env_set_enetaddr(mac, ethaddr);
1170 fec_free_descs(fec);
1179 int fecmxc_initialize_multi(bd_t *bd, int dev_id, int phy_id, uint32_t addr)
1182 struct mii_dev *bus = NULL;
1183 #ifdef CONFIG_PHYLIB
1184 struct phy_device *phydev = NULL;
1188 #ifdef CONFIG_FEC_MXC_MDIO_BASE
1190 * The i.MX28 has two ethernet interfaces, but they are not equal.
1191 * Only the first one can access the MDIO bus.
1193 base_mii = CONFIG_FEC_MXC_MDIO_BASE;
1197 debug("eth_init: fec_probe(bd, %i, %i) @ %08x\n", dev_id, phy_id, addr);
1198 bus = fec_get_miibus(base_mii, dev_id);
1201 #ifdef CONFIG_PHYLIB
1202 phydev = phy_find_by_mask(bus, 1 << phy_id, PHY_INTERFACE_MODE_RGMII);
1204 mdio_unregister(bus);
1208 ret = fec_probe(bd, dev_id, addr, bus, phydev);
1210 ret = fec_probe(bd, dev_id, addr, bus, phy_id);
1213 #ifdef CONFIG_PHYLIB
1216 mdio_unregister(bus);
1222 #ifdef CONFIG_FEC_MXC_PHYADDR
1223 int fecmxc_initialize(bd_t *bd)
1225 return fecmxc_initialize_multi(bd, -1, CONFIG_FEC_MXC_PHYADDR,
1230 #ifndef CONFIG_PHYLIB
1231 int fecmxc_register_mii_postcall(struct eth_device *dev, int (*cb)(int))
1233 struct fec_priv *fec = (struct fec_priv *)dev->priv;
1234 fec->mii_postcall = cb;
1241 static int fecmxc_read_rom_hwaddr(struct udevice *dev)
1243 struct fec_priv *priv = dev_get_priv(dev);
1244 struct eth_pdata *pdata = dev_get_platdata(dev);
1246 return fec_get_hwaddr(priv->dev_id, pdata->enetaddr);
1249 static int fecmxc_free_pkt(struct udevice *dev, uchar *packet, int length)
1257 static const struct eth_ops fecmxc_ops = {
1258 .start = fecmxc_init,
1259 .send = fecmxc_send,
1260 .recv = fecmxc_recv,
1261 .free_pkt = fecmxc_free_pkt,
1262 .stop = fecmxc_halt,
1263 .write_hwaddr = fecmxc_set_hwaddr,
1264 .read_rom_hwaddr = fecmxc_read_rom_hwaddr,
1267 static int device_get_phy_addr(struct udevice *dev)
1269 struct ofnode_phandle_args phandle_args;
1272 if (dev_read_phandle_with_args(dev, "phy-handle", NULL, 0, 0,
1274 debug("Failed to find phy-handle");
1278 reg = ofnode_read_u32_default(phandle_args.node, "reg", 0);
1283 static int fec_phy_init(struct fec_priv *priv, struct udevice *dev)
1285 struct phy_device *phydev;
1288 addr = device_get_phy_addr(dev);
1289 #ifdef CONFIG_FEC_MXC_PHYADDR
1290 addr = CONFIG_FEC_MXC_PHYADDR;
1293 phydev = phy_connect(priv->bus, addr, dev, priv->interface);
1297 priv->phydev = phydev;
1303 #ifdef CONFIG_DM_GPIO
1304 /* FEC GPIO reset */
1305 static void fec_gpio_reset(struct fec_priv *priv)
1307 debug("fec_gpio_reset: fec_gpio_reset(dev)\n");
1308 if (dm_gpio_is_valid(&priv->phy_reset_gpio)) {
1309 dm_gpio_set_value(&priv->phy_reset_gpio, 1);
1310 mdelay(priv->reset_delay);
1311 dm_gpio_set_value(&priv->phy_reset_gpio, 0);
1316 static int fecmxc_probe(struct udevice *dev)
1318 struct eth_pdata *pdata = dev_get_platdata(dev);
1319 struct fec_priv *priv = dev_get_priv(dev);
1320 struct mii_dev *bus = NULL;
1324 if (IS_ENABLED(CONFIG_IMX8)) {
1325 ret = clk_get_by_name(dev, "ipg", &priv->ipg_clk);
1327 debug("Can't get FEC ipg clk: %d\n", ret);
1330 ret = clk_enable(&priv->ipg_clk);
1332 debug("Can't enable FEC ipg clk: %d\n", ret);
1336 priv->clk_rate = clk_get_rate(&priv->ipg_clk);
1339 ret = fec_alloc_descs(priv);
1343 #ifdef CONFIG_DM_REGULATOR
1344 if (priv->phy_supply) {
1345 ret = regulator_set_enable(priv->phy_supply, true);
1347 printf("%s: Error enabling phy supply\n", dev->name);
1353 #ifdef CONFIG_DM_GPIO
1354 fec_gpio_reset(priv);
1357 writel(readl(&priv->eth->ecntrl) | FEC_ECNTRL_RESET,
1358 &priv->eth->ecntrl);
1359 start = get_timer(0);
1360 while (readl(&priv->eth->ecntrl) & FEC_ECNTRL_RESET) {
1361 if (get_timer(start) > (CONFIG_SYS_HZ * 5)) {
1362 printf("FEC MXC: Timeout reseting chip\n");
1368 fec_reg_setup(priv);
1370 priv->dev_id = dev->seq;
1371 #ifdef CONFIG_FEC_MXC_MDIO_BASE
1372 bus = fec_get_miibus((ulong)CONFIG_FEC_MXC_MDIO_BASE, dev->seq);
1374 bus = fec_get_miibus((ulong)priv->eth, dev->seq);
1382 priv->interface = pdata->phy_interface;
1383 switch (priv->interface) {
1384 case PHY_INTERFACE_MODE_MII:
1385 priv->xcv_type = MII100;
1387 case PHY_INTERFACE_MODE_RMII:
1388 priv->xcv_type = RMII;
1390 case PHY_INTERFACE_MODE_RGMII:
1391 case PHY_INTERFACE_MODE_RGMII_ID:
1392 case PHY_INTERFACE_MODE_RGMII_RXID:
1393 case PHY_INTERFACE_MODE_RGMII_TXID:
1394 priv->xcv_type = RGMII;
1397 priv->xcv_type = CONFIG_FEC_XCV_TYPE;
1398 printf("Unsupported interface type %d defaulting to %d\n",
1399 priv->interface, priv->xcv_type);
1403 ret = fec_phy_init(priv, dev);
1410 mdio_unregister(bus);
1414 fec_free_descs(priv);
1418 static int fecmxc_remove(struct udevice *dev)
1420 struct fec_priv *priv = dev_get_priv(dev);
1423 fec_free_descs(priv);
1424 mdio_unregister(priv->bus);
1425 mdio_free(priv->bus);
1427 #ifdef CONFIG_DM_REGULATOR
1428 if (priv->phy_supply)
1429 regulator_set_enable(priv->phy_supply, false);
1435 static int fecmxc_ofdata_to_platdata(struct udevice *dev)
1438 struct eth_pdata *pdata = dev_get_platdata(dev);
1439 struct fec_priv *priv = dev_get_priv(dev);
1440 const char *phy_mode;
1442 pdata->iobase = (phys_addr_t)devfdt_get_addr(dev);
1443 priv->eth = (struct ethernet_regs *)pdata->iobase;
1445 pdata->phy_interface = -1;
1446 phy_mode = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "phy-mode",
1449 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
1450 if (pdata->phy_interface == -1) {
1451 debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
1455 #ifdef CONFIG_DM_REGULATOR
1456 device_get_supply_regulator(dev, "phy-supply", &priv->phy_supply);
1459 #ifdef CONFIG_DM_GPIO
1460 ret = gpio_request_by_name(dev, "phy-reset-gpios", 0,
1461 &priv->phy_reset_gpio, GPIOD_IS_OUT);
1463 return 0; /* property is optional, don't return error! */
1465 priv->reset_delay = dev_read_u32_default(dev, "phy-reset-duration", 1);
1466 if (priv->reset_delay > 1000) {
1467 printf("FEC MXC: phy reset duration should be <= 1000ms\n");
1468 /* property value wrong, use default value */
1469 priv->reset_delay = 1;
1476 static const struct udevice_id fecmxc_ids[] = {
1477 { .compatible = "fsl,imx6q-fec" },
1478 { .compatible = "fsl,imx6sl-fec" },
1479 { .compatible = "fsl,imx6sx-fec" },
1480 { .compatible = "fsl,imx6ul-fec" },
1481 { .compatible = "fsl,imx53-fec" },
1482 { .compatible = "fsl,imx7d-fec" },
1486 U_BOOT_DRIVER(fecmxc_gem) = {
1489 .of_match = fecmxc_ids,
1490 .ofdata_to_platdata = fecmxc_ofdata_to_platdata,
1491 .probe = fecmxc_probe,
1492 .remove = fecmxc_remove,
1494 .priv_auto_alloc_size = sizeof(struct fec_priv),
1495 .platdata_auto_alloc_size = sizeof(struct eth_pdata),