1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2021 Waymo LLC
4 * Copyright (C) 2011 Michal Simek <monstr@monstr.eu>
5 * Copyright (C) 2011 PetaLogix
6 * Copyright (C) 2010 Xilinx, Inc. All rights reserved.
12 #include <display_options.h>
14 #include <dm/device_compat.h>
18 #include <asm/global_data.h>
23 #include <linux/delay.h>
26 DECLARE_GLOBAL_DATA_PTR;
29 #define XAE_EMMC_LINKSPEED_MASK 0xC0000000 /* Link speed */
30 #define XAE_EMMC_LINKSPD_10 0x00000000 /* Link Speed mask for 10 Mbit */
31 #define XAE_EMMC_LINKSPD_100 0x40000000 /* Link Speed mask for 100 Mbit */
32 #define XAE_EMMC_LINKSPD_1000 0x80000000 /* Link Speed mask for 1000 Mbit */
34 /* Interrupt Status/Enable/Mask Registers bit definitions */
35 #define XAE_INT_RXRJECT_MASK 0x00000008 /* Rx frame rejected */
36 #define XAE_INT_MGTRDY_MASK 0x00000080 /* MGT clock Lock */
38 /* Receive Configuration Word 1 (RCW1) Register bit definitions */
39 #define XAE_RCW1_RX_MASK 0x10000000 /* Receiver enable */
41 /* Transmitter Configuration (TC) Register bit definitions */
42 #define XAE_TC_TX_MASK 0x10000000 /* Transmitter enable */
44 #define XAE_UAW1_UNICASTADDR_MASK 0x0000FFFF
46 /* MDIO Management Configuration (MC) Register bit definitions */
47 #define XAE_MDIO_MC_MDIOEN_MASK 0x00000040 /* MII management enable*/
49 /* MDIO Management Control Register (MCR) Register bit definitions */
50 #define XAE_MDIO_MCR_PHYAD_MASK 0x1F000000 /* Phy Address Mask */
51 #define XAE_MDIO_MCR_PHYAD_SHIFT 24 /* Phy Address Shift */
52 #define XAE_MDIO_MCR_REGAD_MASK 0x001F0000 /* Reg Address Mask */
53 #define XAE_MDIO_MCR_REGAD_SHIFT 16 /* Reg Address Shift */
54 #define XAE_MDIO_MCR_OP_READ_MASK 0x00008000 /* Op Code Read Mask */
55 #define XAE_MDIO_MCR_OP_WRITE_MASK 0x00004000 /* Op Code Write Mask */
56 #define XAE_MDIO_MCR_INITIATE_MASK 0x00000800 /* Ready Mask */
57 #define XAE_MDIO_MCR_READY_MASK 0x00000080 /* Ready Mask */
59 #define XAE_MDIO_DIV_DFT 29 /* Default MDIO clock divisor */
61 #define XAXIDMA_BD_STS_ACTUAL_LEN_MASK 0x007FFFFF /* Actual len */
64 /* Bitmasks of XAXIDMA_CR_OFFSET register */
65 #define XAXIDMA_CR_RUNSTOP_MASK 0x00000001 /* Start/stop DMA channel */
66 #define XAXIDMA_CR_RESET_MASK 0x00000004 /* Reset DMA engine */
68 /* Bitmasks of XAXIDMA_SR_OFFSET register */
69 #define XAXIDMA_HALTED_MASK 0x00000001 /* DMA channel halted */
71 /* Bitmask for interrupts */
72 #define XAXIDMA_IRQ_IOC_MASK 0x00001000 /* Completion intr */
73 #define XAXIDMA_IRQ_DELAY_MASK 0x00002000 /* Delay interrupt */
74 #define XAXIDMA_IRQ_ALL_MASK 0x00007000 /* All interrupts */
76 /* Bitmasks of XAXIDMA_BD_CTRL_OFFSET register */
77 #define XAXIDMA_BD_CTRL_TXSOF_MASK 0x08000000 /* First tx packet */
78 #define XAXIDMA_BD_CTRL_TXEOF_MASK 0x04000000 /* Last tx packet */
80 /* Bitmasks for XXV Ethernet MAC */
81 #define XXV_TC_TX_MASK 0x00000001
82 #define XXV_TC_FCS_MASK 0x00000002
83 #define XXV_RCW1_RX_MASK 0x00000001
84 #define XXV_RCW1_FCS_MASK 0x00000002
87 #define XXV_MIN_PKT_SIZE 60
89 static u8 rxframe[PKTSIZE_ALIGN] __attribute((aligned(DMAALIGN)));
90 static u8 txminframe[XXV_MIN_PKT_SIZE] __attribute((aligned(DMAALIGN)));
97 /* Reflect dma offsets */
99 u32 control; /* DMACR */
100 u32 status; /* DMASR */
101 u32 current; /* CURDESC low 32 bit */
102 u32 current_hi; /* CURDESC high 32 bit */
103 u32 tail; /* TAILDESC low 32 bit */
104 u32 tail_hi; /* TAILDESC high 32 bit */
107 /* Platform data structures */
109 struct eth_pdata eth_pdata;
110 struct axidma_reg *dmatx;
111 struct axidma_reg *dmarx;
116 enum emac_variant mactype;
119 /* Private driver structures */
121 struct axidma_reg *dmatx;
122 struct axidma_reg *dmarx;
125 struct axi_regs *iobase;
126 phy_interface_t interface;
127 struct phy_device *phydev;
131 enum emac_variant mactype;
136 u32 next_desc; /* Next descriptor pointer */
138 u32 buf_addr; /* Buffer address */
142 u32 cntrl; /* Control */
143 u32 status; /* Status */
145 u32 app1; /* TX start << 16 | insert */
146 u32 app2; /* TX csum seed */
154 /* Static BDs - driver uses only one BD */
155 static struct axidma_bd tx_bd __attribute((aligned(DMAALIGN)));
156 static struct axidma_bd rx_bd __attribute((aligned(DMAALIGN)));
160 u32 is; /* 0xC: Interrupt status */
162 u32 ie; /* 0x14: Interrupt enable */
164 u32 rcw1; /* 0x404: Rx Configuration Word 1 */
165 u32 tc; /* 0x408: Tx Configuration */
167 u32 emmc; /* 0x410: EMAC mode configuration */
169 u32 mdio_mc; /* 0x500: MII Management Config */
170 u32 mdio_mcr; /* 0x504: MII Management Control */
171 u32 mdio_mwd; /* 0x508: MII Management Write Data */
172 u32 mdio_mrd; /* 0x50C: MII Management Read Data */
174 u32 uaw0; /* 0x700: Unicast address word 0 */
175 u32 uaw1; /* 0x704: Unicast address word 1 */
178 struct xxv_axi_regs {
179 u32 gt_reset; /* 0x0 */
181 u32 tc; /* 0xC: Tx Configuration */
183 u32 rcw1; /* 0x14: Rx Configuration Word 1 */
186 /* Use MII register 1 (MII status register) to detect PHY */
187 #define PHY_DETECT_REG 1
190 * Mask used to verify certain PHY features (or register contents)
191 * in the register above:
192 * 0x1000: 10Mbps full duplex support
193 * 0x0800: 10Mbps half duplex support
194 * 0x0008: Auto-negotiation support
196 #define PHY_DETECT_MASK 0x1808
198 static inline int mdio_wait(struct axi_regs *regs)
202 /* Wait till MDIO interface is ready to accept a new transaction. */
203 while (timeout && (!(readl(®s->mdio_mcr)
204 & XAE_MDIO_MCR_READY_MASK))) {
209 printf("%s: Timeout\n", __func__);
216 * axienet_dma_write - Memory mapped Axi DMA register Buffer Descriptor write.
217 * @bd: pointer to BD descriptor structure
218 * @desc: Address offset of DMA descriptors
220 * This function writes the value into the corresponding Axi DMA register.
222 static inline void axienet_dma_write(struct axidma_bd *bd, u32 *desc)
224 #if defined(CONFIG_PHYS_64BIT)
225 writeq((unsigned long)bd, desc);
227 writel((u32)bd, desc);
231 static u32 phyread(struct axidma_priv *priv, u32 phyaddress, u32 registernum,
234 struct axi_regs *regs = priv->iobase;
240 mdioctrlreg = ((phyaddress << XAE_MDIO_MCR_PHYAD_SHIFT) &
241 XAE_MDIO_MCR_PHYAD_MASK) |
242 ((registernum << XAE_MDIO_MCR_REGAD_SHIFT)
243 & XAE_MDIO_MCR_REGAD_MASK) |
244 XAE_MDIO_MCR_INITIATE_MASK |
245 XAE_MDIO_MCR_OP_READ_MASK;
247 writel(mdioctrlreg, ®s->mdio_mcr);
253 *val = readl(®s->mdio_mrd);
257 static u32 phywrite(struct axidma_priv *priv, u32 phyaddress, u32 registernum,
260 struct axi_regs *regs = priv->iobase;
266 mdioctrlreg = ((phyaddress << XAE_MDIO_MCR_PHYAD_SHIFT) &
267 XAE_MDIO_MCR_PHYAD_MASK) |
268 ((registernum << XAE_MDIO_MCR_REGAD_SHIFT)
269 & XAE_MDIO_MCR_REGAD_MASK) |
270 XAE_MDIO_MCR_INITIATE_MASK |
271 XAE_MDIO_MCR_OP_WRITE_MASK;
274 writel(data, ®s->mdio_mwd);
276 writel(mdioctrlreg, ®s->mdio_mcr);
284 static int axiemac_phy_init(struct udevice *dev)
289 struct axidma_priv *priv = dev_get_priv(dev);
290 struct axi_regs *regs = priv->iobase;
291 struct phy_device *phydev;
293 u32 supported = SUPPORTED_10baseT_Half |
294 SUPPORTED_10baseT_Full |
295 SUPPORTED_100baseT_Half |
296 SUPPORTED_100baseT_Full |
297 SUPPORTED_1000baseT_Half |
298 SUPPORTED_1000baseT_Full;
300 /* Set default MDIO divisor */
301 writel(XAE_MDIO_DIV_DFT | XAE_MDIO_MC_MDIOEN_MASK, ®s->mdio_mc);
303 if (IS_ENABLED(CONFIG_DM_ETH_PHY))
304 priv->phyaddr = eth_phy_get_addr(dev);
307 * Set address of PCS/PMA PHY to the one pointed by phy-handle for
308 * backward compatibility.
310 if (priv->phyaddr != -1 && priv->pcsaddr == 0)
311 priv->pcsaddr = priv->phyaddr;
313 if (priv->phyaddr == -1) {
314 /* Detect the PHY address */
315 for (i = 31; i >= 0; i--) {
316 ret = phyread(priv, i, PHY_DETECT_REG, &phyreg);
317 if (!ret && (phyreg != 0xFFFF) &&
318 ((phyreg & PHY_DETECT_MASK) == PHY_DETECT_MASK)) {
319 /* Found a valid PHY address */
321 debug("axiemac: Found valid phy address, %x\n",
328 /* Interface - look at tsec */
329 phydev = phy_connect(priv->bus, priv->phyaddr, dev, priv->interface);
330 if (IS_ERR_OR_NULL(phydev)) {
331 dev_err(dev, "phy_connect() failed\n");
335 phydev->supported &= supported;
336 phydev->advertising = phydev->supported;
337 priv->phydev = phydev;
338 if (priv->phy_of_handle)
339 priv->phydev->node = offset_to_ofnode(priv->phy_of_handle);
345 static int pcs_pma_startup(struct axidma_priv *priv)
347 u32 rc, retry_cnt = 0;
350 rc = phyread(priv, priv->pcsaddr, MII_BMCR, &mii_reg);
354 if (!(mii_reg & BMCR_ANENABLE)) {
355 mii_reg |= BMCR_ANENABLE;
356 if (phywrite(priv, priv->pcsaddr, MII_BMCR, mii_reg))
361 * Check the internal PHY status and warn user if the link between it
362 * and the external PHY is not obtained.
364 debug("axiemac: waiting for link status of the PCS/PMA PHY");
365 while (retry_cnt * 10 < PHY_ANEG_TIMEOUT) {
366 rc = phyread(priv, priv->pcsaddr, MII_BMSR, &mii_reg);
367 if ((mii_reg & BMSR_LSTATUS) && mii_reg != 0xffff && !rc) {
371 if ((retry_cnt++ % 10) == 0)
376 printf("axiemac: Warning, PCS/PMA PHY@%d is not ready, link is down\n",
380 printf("axiemac: MDIO to the PCS/PMA PHY has failed\n");
384 /* Setting axi emac and phy to proper setting */
385 static int setup_phy(struct udevice *dev)
388 u32 speed, emmc_reg, ret;
389 struct axidma_priv *priv = dev_get_priv(dev);
390 struct axi_regs *regs = priv->iobase;
391 struct phy_device *phydev = priv->phydev;
393 if (priv->interface == PHY_INTERFACE_MODE_SGMII) {
395 * In SGMII cases the isolate bit might set
396 * after DMA and ethernet resets and hence
397 * check and clear if set.
399 ret = phyread(priv, priv->pcsaddr, MII_BMCR, &temp);
402 if (temp & BMCR_ISOLATE) {
403 temp &= ~BMCR_ISOLATE;
404 ret = phywrite(priv, priv->pcsaddr, MII_BMCR, temp);
410 if (phy_startup(phydev)) {
411 printf("axiemac: could not initialize PHY %s\n",
415 if (priv->interface == PHY_INTERFACE_MODE_SGMII ||
416 priv->interface == PHY_INTERFACE_MODE_1000BASEX) {
417 if (pcs_pma_startup(priv))
421 printf("%s: No link.\n", phydev->dev->name);
425 switch (phydev->speed) {
427 speed = XAE_EMMC_LINKSPD_1000;
430 speed = XAE_EMMC_LINKSPD_100;
433 speed = XAE_EMMC_LINKSPD_10;
439 /* Setup the emac for the phy speed */
440 emmc_reg = readl(®s->emmc);
441 emmc_reg &= ~XAE_EMMC_LINKSPEED_MASK;
444 /* Write new speed setting out to Axi Ethernet */
445 writel(emmc_reg, ®s->emmc);
448 * Setting the operating speed of the MAC needs a delay. There
449 * doesn't seem to be register to poll, so please consider this
450 * during your application design.
457 /* STOP DMA transfers */
458 static void axiemac_stop(struct udevice *dev)
460 struct axidma_priv *priv = dev_get_priv(dev);
463 /* Stop the hardware */
464 temp = readl(&priv->dmatx->control);
465 temp &= ~XAXIDMA_CR_RUNSTOP_MASK;
466 writel(temp, &priv->dmatx->control);
468 temp = readl(&priv->dmarx->control);
469 temp &= ~XAXIDMA_CR_RUNSTOP_MASK;
470 writel(temp, &priv->dmarx->control);
472 debug("axiemac: Halted\n");
475 static int xxv_axi_ethernet_init(struct axidma_priv *priv)
477 struct xxv_axi_regs *regs = (struct xxv_axi_regs *)priv->iobase;
479 writel(readl(®s->rcw1) | XXV_RCW1_FCS_MASK, ®s->rcw1);
480 writel(readl(®s->tc) | XXV_TC_FCS_MASK, ®s->tc);
481 writel(readl(®s->tc) | XXV_TC_TX_MASK, ®s->tc);
482 writel(readl(®s->rcw1) | XXV_RCW1_RX_MASK, ®s->rcw1);
487 static int axi_ethernet_init(struct axidma_priv *priv)
489 struct axi_regs *regs = priv->iobase;
493 * Check the status of the MgtRdy bit in the interrupt status
494 * registers. This must be done to allow the MGT clock to become stable
495 * for the Sgmii and 1000BaseX PHY interfaces. No other register reads
496 * will be valid until this bit is valid.
497 * The bit is always a 1 for all other PHY interfaces.
498 * Interrupt status and enable registers are not available in non
499 * processor mode and hence bypass in this mode
501 if (!priv->eth_hasnobuf) {
502 err = wait_for_bit_le32(®s->is, XAE_INT_MGTRDY_MASK,
505 printf("%s: Timeout\n", __func__);
510 * Stop the device and reset HW
513 writel(0, ®s->ie);
516 /* Disable the receiver */
517 writel(readl(®s->rcw1) & ~XAE_RCW1_RX_MASK, ®s->rcw1);
520 * Stopping the receiver in mid-packet causes a dropped packet
521 * indication from HW. Clear it.
523 if (!priv->eth_hasnobuf) {
524 /* Set the interrupt status register to clear the interrupt */
525 writel(XAE_INT_RXRJECT_MASK, ®s->is);
529 /* Set default MDIO divisor */
530 writel(XAE_MDIO_DIV_DFT | XAE_MDIO_MC_MDIOEN_MASK, ®s->mdio_mc);
532 debug("axiemac: InitHw done\n");
536 static int axiemac_write_hwaddr(struct udevice *dev)
538 struct eth_pdata *pdata = dev_get_plat(dev);
539 struct axidma_priv *priv = dev_get_priv(dev);
540 struct axi_regs *regs = priv->iobase;
542 if (priv->mactype != EMAC_1G)
545 /* Set the MAC address */
546 int val = ((pdata->enetaddr[3] << 24) | (pdata->enetaddr[2] << 16) |
547 (pdata->enetaddr[1] << 8) | (pdata->enetaddr[0]));
548 writel(val, ®s->uaw0);
550 val = (pdata->enetaddr[5] << 8) | pdata->enetaddr[4];
551 val |= readl(®s->uaw1) & ~XAE_UAW1_UNICASTADDR_MASK;
552 writel(val, ®s->uaw1);
556 /* Reset DMA engine */
557 static void axi_dma_init(struct axidma_priv *priv)
561 /* Reset the engine so the hardware starts from a known state */
562 writel(XAXIDMA_CR_RESET_MASK, &priv->dmatx->control);
563 writel(XAXIDMA_CR_RESET_MASK, &priv->dmarx->control);
565 /* At the initialization time, hardware should finish reset quickly */
567 /* Check transmit/receive channel */
568 /* Reset is done when the reset bit is low */
569 if (!((readl(&priv->dmatx->control) |
570 readl(&priv->dmarx->control))
571 & XAXIDMA_CR_RESET_MASK)) {
576 printf("%s: Timeout\n", __func__);
579 static int axiemac_start(struct udevice *dev)
581 struct axidma_priv *priv = dev_get_priv(dev);
584 debug("axiemac: Init started\n");
586 * Initialize AXIDMA engine. AXIDMA engine must be initialized before
587 * AxiEthernet. During AXIDMA engine initialization, AXIDMA hardware is
588 * reset, and since AXIDMA reset line is connected to AxiEthernet, this
589 * would ensure a reset of AxiEthernet.
593 /* Initialize AxiEthernet hardware. */
594 if (priv->mactype == EMAC_1G) {
595 if (axi_ethernet_init(priv))
598 if (xxv_axi_ethernet_init(priv))
602 /* Disable all RX interrupts before RxBD space setup */
603 temp = readl(&priv->dmarx->control);
604 temp &= ~XAXIDMA_IRQ_ALL_MASK;
605 writel(temp, &priv->dmarx->control);
607 /* Start DMA RX channel. Now it's ready to receive data.*/
608 axienet_dma_write(&rx_bd, &priv->dmarx->current);
611 memset(&rx_bd, 0, sizeof(rx_bd));
612 rx_bd.next_desc = lower_32_bits((unsigned long)&rx_bd);
613 rx_bd.buf_addr = lower_32_bits((unsigned long)&rxframe);
614 #if defined(CONFIG_PHYS_64BIT)
615 rx_bd.next_desc_msb = upper_32_bits((unsigned long)&rx_bd);
616 rx_bd.buf_addr_msb = upper_32_bits((unsigned long)&rxframe);
618 rx_bd.cntrl = sizeof(rxframe);
619 /* Flush the last BD so DMA core could see the updates */
620 flush_cache((phys_addr_t)&rx_bd, sizeof(rx_bd));
622 /* It is necessary to flush rxframe because if you don't do it
623 * then cache can contain uninitialized data */
624 flush_cache((phys_addr_t)&rxframe, sizeof(rxframe));
626 /* Start the hardware */
627 temp = readl(&priv->dmarx->control);
628 temp |= XAXIDMA_CR_RUNSTOP_MASK;
629 writel(temp, &priv->dmarx->control);
631 /* Rx BD is ready - start */
632 axienet_dma_write(&rx_bd, &priv->dmarx->tail);
634 if (priv->mactype == EMAC_1G) {
635 struct axi_regs *regs = priv->iobase;
637 writel(XAE_TC_TX_MASK, ®s->tc);
639 writel(XAE_RCW1_RX_MASK, ®s->rcw1);
642 if (!setup_phy(dev)) {
647 struct xxv_axi_regs *regs = (struct xxv_axi_regs *)priv->iobase;
649 writel(readl(®s->tc) | XXV_TC_TX_MASK, ®s->tc);
652 writel(readl(®s->rcw1) | XXV_RCW1_RX_MASK, ®s->rcw1);
655 debug("axiemac: Init complete\n");
659 static int axiemac_send(struct udevice *dev, void *ptr, int len)
661 struct axidma_priv *priv = dev_get_priv(dev);
664 if (len > PKTSIZE_ALIGN)
667 /* If size is less than min packet size, pad to min size */
668 if (priv->mactype == EMAC_10G_25G && len < XXV_MIN_PKT_SIZE) {
669 memset(txminframe, 0, XXV_MIN_PKT_SIZE);
670 memcpy(txminframe, ptr, len);
671 len = XXV_MIN_PKT_SIZE;
675 /* Flush packet to main memory to be trasfered by DMA */
676 flush_cache((phys_addr_t)ptr, len);
679 memset(&tx_bd, 0, sizeof(tx_bd));
680 /* At the end of the ring, link the last BD back to the top */
681 tx_bd.next_desc = lower_32_bits((unsigned long)&tx_bd);
682 tx_bd.buf_addr = lower_32_bits((unsigned long)ptr);
683 #if defined(CONFIG_PHYS_64BIT)
684 tx_bd.next_desc_msb = upper_32_bits((unsigned long)&tx_bd);
685 tx_bd.buf_addr_msb = upper_32_bits((unsigned long)ptr);
688 tx_bd.cntrl = len | XAXIDMA_BD_CTRL_TXSOF_MASK |
689 XAXIDMA_BD_CTRL_TXEOF_MASK;
691 /* Flush the last BD so DMA core could see the updates */
692 flush_cache((phys_addr_t)&tx_bd, sizeof(tx_bd));
694 if (readl(&priv->dmatx->status) & XAXIDMA_HALTED_MASK) {
696 axienet_dma_write(&tx_bd, &priv->dmatx->current);
697 /* Start the hardware */
698 temp = readl(&priv->dmatx->control);
699 temp |= XAXIDMA_CR_RUNSTOP_MASK;
700 writel(temp, &priv->dmatx->control);
704 axienet_dma_write(&tx_bd, &priv->dmatx->tail);
706 /* Wait for transmission to complete */
707 debug("axiemac: Waiting for tx to be done\n");
709 while (timeout && (!(readl(&priv->dmatx->status) &
710 (XAXIDMA_IRQ_DELAY_MASK | XAXIDMA_IRQ_IOC_MASK)))) {
715 printf("%s: Timeout\n", __func__);
719 debug("axiemac: Sending complete\n");
723 static int isrxready(struct axidma_priv *priv)
727 /* Read pending interrupts */
728 status = readl(&priv->dmarx->status);
730 /* Acknowledge pending interrupts */
731 writel(status & XAXIDMA_IRQ_ALL_MASK, &priv->dmarx->status);
734 * If Reception done interrupt is asserted, call RX call back function
735 * to handle the processed BDs and then raise the according flag.
737 if ((status & (XAXIDMA_IRQ_DELAY_MASK | XAXIDMA_IRQ_IOC_MASK)))
743 static int axiemac_recv(struct udevice *dev, int flags, uchar **packetp)
746 struct axidma_priv *priv = dev_get_priv(dev);
749 /* Wait for an incoming packet */
750 if (!isrxready(priv))
753 debug("axiemac: RX data ready\n");
755 /* Disable IRQ for a moment till packet is handled */
756 temp = readl(&priv->dmarx->control);
757 temp &= ~XAXIDMA_IRQ_ALL_MASK;
758 writel(temp, &priv->dmarx->control);
759 if (!priv->eth_hasnobuf && priv->mactype == EMAC_1G)
760 length = rx_bd.app4 & 0xFFFF; /* max length mask */
762 length = rx_bd.status & XAXIDMA_BD_STS_ACTUAL_LEN_MASK;
765 print_buffer(&rxframe, &rxframe[0], 1, length, 16);
772 static int axiemac_free_pkt(struct udevice *dev, uchar *packet, int length)
774 struct axidma_priv *priv = dev_get_priv(dev);
777 /* It is useful to clear buffer to be sure that it is consistent */
778 memset(rxframe, 0, sizeof(rxframe));
781 /* Clear the whole buffer and setup it again - all flags are cleared */
782 memset(&rx_bd, 0, sizeof(rx_bd));
783 rx_bd.next_desc = lower_32_bits((unsigned long)&rx_bd);
784 rx_bd.buf_addr = lower_32_bits((unsigned long)&rxframe);
785 #if defined(CONFIG_PHYS_64BIT)
786 rx_bd.next_desc_msb = upper_32_bits((unsigned long)&rx_bd);
787 rx_bd.buf_addr_msb = upper_32_bits((unsigned long)&rxframe);
789 rx_bd.cntrl = sizeof(rxframe);
792 flush_cache((phys_addr_t)&rx_bd, sizeof(rx_bd));
794 /* It is necessary to flush rxframe because if you don't do it
795 * then cache will contain previous packet */
796 flush_cache((phys_addr_t)&rxframe, sizeof(rxframe));
798 /* Rx BD is ready - start again */
799 axienet_dma_write(&rx_bd, &priv->dmarx->tail);
801 debug("axiemac: RX completed, framelength = %d\n", length);
806 static int axiemac_miiphy_read(struct mii_dev *bus, int addr,
812 ret = phyread(bus->priv, addr, reg, &value);
813 debug("axiemac: Read MII 0x%x, 0x%x, 0x%x, %d\n", addr, reg,
818 static int axiemac_miiphy_write(struct mii_dev *bus, int addr, int devad,
821 debug("axiemac: Write MII 0x%x, 0x%x, 0x%x\n", addr, reg, value);
822 return phywrite(bus->priv, addr, reg, value);
825 static int axi_emac_probe(struct udevice *dev)
827 struct axidma_plat *plat = dev_get_plat(dev);
828 struct eth_pdata *pdata = &plat->eth_pdata;
829 struct axidma_priv *priv = dev_get_priv(dev);
832 priv->iobase = (struct axi_regs *)pdata->iobase;
833 priv->dmatx = plat->dmatx;
834 /* RX channel offset is 0x30 */
835 priv->dmarx = (struct axidma_reg *)((phys_addr_t)priv->dmatx + 0x30);
836 priv->mactype = plat->mactype;
838 if (priv->mactype == EMAC_1G) {
839 priv->eth_hasnobuf = plat->eth_hasnobuf;
840 priv->pcsaddr = plat->pcsaddr;
841 priv->phyaddr = plat->phyaddr;
842 priv->phy_of_handle = plat->phy_of_handle;
843 priv->interface = pdata->phy_interface;
845 if (IS_ENABLED(CONFIG_DM_ETH_PHY))
846 priv->bus = eth_phy_get_mdio_bus(dev);
849 priv->bus = mdio_alloc();
850 priv->bus->read = axiemac_miiphy_read;
851 priv->bus->write = axiemac_miiphy_write;
852 priv->bus->priv = priv;
854 ret = mdio_register_seq(priv->bus, dev_seq(dev));
859 if (IS_ENABLED(CONFIG_DM_ETH_PHY))
860 eth_phy_set_mdio_bus(dev, priv->bus);
862 axiemac_phy_init(dev);
865 printf("AXI EMAC: %lx, phyaddr %d, interface %s\n", (ulong)pdata->iobase,
866 priv->phyaddr, phy_string_for_interface(pdata->phy_interface));
871 static int axi_emac_remove(struct udevice *dev)
873 struct axidma_priv *priv = dev_get_priv(dev);
875 if (priv->mactype == EMAC_1G) {
877 mdio_unregister(priv->bus);
878 mdio_free(priv->bus);
884 static const struct eth_ops axi_emac_ops = {
885 .start = axiemac_start,
886 .send = axiemac_send,
887 .recv = axiemac_recv,
888 .free_pkt = axiemac_free_pkt,
889 .stop = axiemac_stop,
890 .write_hwaddr = axiemac_write_hwaddr,
893 static int axi_emac_of_to_plat(struct udevice *dev)
895 struct axidma_plat *plat = dev_get_plat(dev);
896 struct eth_pdata *pdata = &plat->eth_pdata;
897 int node = dev_of_offset(dev);
900 pdata->iobase = dev_read_addr(dev);
901 plat->mactype = dev_get_driver_data(dev);
903 offset = fdtdec_lookup_phandle(gd->fdt_blob, node,
904 "axistream-connected");
906 printf("%s: axistream is not found\n", __func__);
909 plat->dmatx = (struct axidma_reg *)fdtdec_get_addr_size_auto_parent
910 (gd->fdt_blob, 0, offset, "reg", 0, NULL, false);
912 printf("%s: axi_dma register space not found\n", __func__);
916 if (plat->mactype == EMAC_1G) {
918 /* PHYAD 0 always redirects to the PCS/PMA PHY */
921 offset = fdtdec_lookup_phandle(gd->fdt_blob, node,
924 if (!(IS_ENABLED(CONFIG_DM_ETH_PHY)))
925 plat->phyaddr = fdtdec_get_int(gd->fdt_blob,
928 plat->phy_of_handle = offset;
931 pdata->phy_interface = dev_read_phy_mode(dev);
932 if (pdata->phy_interface == PHY_INTERFACE_MODE_NA)
935 plat->eth_hasnobuf = fdtdec_get_bool(gd->fdt_blob, node,
936 "xlnx,eth-hasnobuf");
938 if (pdata->phy_interface == PHY_INTERFACE_MODE_SGMII ||
939 pdata->phy_interface == PHY_INTERFACE_MODE_1000BASEX) {
940 offset = fdtdec_lookup_phandle(gd->fdt_blob, node,
943 plat->pcsaddr = fdtdec_get_int(gd->fdt_blob,
952 static const struct udevice_id axi_emac_ids[] = {
953 { .compatible = "xlnx,axi-ethernet-1.00.a", .data = (uintptr_t)EMAC_1G },
954 { .compatible = "xlnx,xxv-ethernet-1.0", .data = (uintptr_t)EMAC_10G_25G },
958 U_BOOT_DRIVER(axi_emac) = {
961 .of_match = axi_emac_ids,
962 .of_to_plat = axi_emac_of_to_plat,
963 .probe = axi_emac_probe,
964 .remove = axi_emac_remove,
965 .ops = &axi_emac_ops,
966 .priv_auto = sizeof(struct axidma_priv),
967 .plat_auto = sizeof(struct axidma_plat),