]> git.sur5r.net Git - u-boot/blobdiff - drivers/net/designware.c
net: designware: Add reset ctrl to driver
[u-boot] / drivers / net / designware.c
index 8413d57767a753e22616c432276d0c7ad6725eac..19db0a8114ae8bc0c37766f294486ea64980ad0b 100644 (file)
+// SPDX-License-Identifier: GPL-2.0+
 /*
  * (C) Copyright 2010
  * Vipin Kumar, ST Micoelectronics, vipin.kumar@st.com.
- *
- * SPDX-License-Identifier:    GPL-2.0+
  */
 
 /*
- * Designware ethernet IP driver for u-boot
+ * Designware ethernet IP driver for U-Boot
  */
 
 #include <common.h>
+#include <clk.h>
+#include <dm.h>
+#include <errno.h>
 #include <miiphy.h>
 #include <malloc.h>
+#include <pci.h>
+#include <reset.h>
 #include <linux/compiler.h>
 #include <linux/err.h>
+#include <linux/kernel.h>
 #include <asm/io.h>
+#include <power/regulator.h>
 #include "designware.h"
 
-static int configure_phy(struct eth_device *dev);
+static int dw_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
+{
+#ifdef CONFIG_DM_ETH
+       struct dw_eth_dev *priv = dev_get_priv((struct udevice *)bus->priv);
+       struct eth_mac_regs *mac_p = priv->mac_regs_p;
+#else
+       struct eth_mac_regs *mac_p = bus->priv;
+#endif
+       ulong start;
+       u16 miiaddr;
+       int timeout = CONFIG_MDIO_TIMEOUT;
+
+       miiaddr = ((addr << MIIADDRSHIFT) & MII_ADDRMSK) |
+                 ((reg << MIIREGSHIFT) & MII_REGMSK);
+
+       writel(miiaddr | MII_CLKRANGE_150_250M | MII_BUSY, &mac_p->miiaddr);
+
+       start = get_timer(0);
+       while (get_timer(start) < timeout) {
+               if (!(readl(&mac_p->miiaddr) & MII_BUSY))
+                       return readl(&mac_p->miidata);
+               udelay(10);
+       };
+
+       return -ETIMEDOUT;
+}
+
+static int dw_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
+                       u16 val)
+{
+#ifdef CONFIG_DM_ETH
+       struct dw_eth_dev *priv = dev_get_priv((struct udevice *)bus->priv);
+       struct eth_mac_regs *mac_p = priv->mac_regs_p;
+#else
+       struct eth_mac_regs *mac_p = bus->priv;
+#endif
+       ulong start;
+       u16 miiaddr;
+       int ret = -ETIMEDOUT, timeout = CONFIG_MDIO_TIMEOUT;
+
+       writel(val, &mac_p->miidata);
+       miiaddr = ((addr << MIIADDRSHIFT) & MII_ADDRMSK) |
+                 ((reg << MIIREGSHIFT) & MII_REGMSK) | MII_WRITE;
 
-static void tx_descs_init(struct eth_device *dev)
+       writel(miiaddr | MII_CLKRANGE_150_250M | MII_BUSY, &mac_p->miiaddr);
+
+       start = get_timer(0);
+       while (get_timer(start) < timeout) {
+               if (!(readl(&mac_p->miiaddr) & MII_BUSY)) {
+                       ret = 0;
+                       break;
+               }
+               udelay(10);
+       };
+
+       return ret;
+}
+
+#if defined(CONFIG_DM_ETH) && defined(CONFIG_DM_GPIO)
+static int dw_mdio_reset(struct mii_dev *bus)
+{
+       struct udevice *dev = bus->priv;
+       struct dw_eth_dev *priv = dev_get_priv(dev);
+       struct dw_eth_pdata *pdata = dev_get_platdata(dev);
+       int ret;
+
+       if (!dm_gpio_is_valid(&priv->reset_gpio))
+               return 0;
+
+       /* reset the phy */
+       ret = dm_gpio_set_value(&priv->reset_gpio, 0);
+       if (ret)
+               return ret;
+
+       udelay(pdata->reset_delays[0]);
+
+       ret = dm_gpio_set_value(&priv->reset_gpio, 1);
+       if (ret)
+               return ret;
+
+       udelay(pdata->reset_delays[1]);
+
+       ret = dm_gpio_set_value(&priv->reset_gpio, 0);
+       if (ret)
+               return ret;
+
+       udelay(pdata->reset_delays[2]);
+
+       return 0;
+}
+#endif
+
+static int dw_mdio_init(const char *name, void *priv)
+{
+       struct mii_dev *bus = mdio_alloc();
+
+       if (!bus) {
+               printf("Failed to allocate MDIO bus\n");
+               return -ENOMEM;
+       }
+
+       bus->read = dw_mdio_read;
+       bus->write = dw_mdio_write;
+       snprintf(bus->name, sizeof(bus->name), "%s", name);
+#if defined(CONFIG_DM_ETH) && defined(CONFIG_DM_GPIO)
+       bus->reset = dw_mdio_reset;
+#endif
+
+       bus->priv = priv;
+
+       return mdio_register(bus);
+}
+
+static void tx_descs_init(struct dw_eth_dev *priv)
 {
-       struct dw_eth_dev *priv = dev->priv;
        struct eth_dma_regs *dma_p = priv->dma_regs_p;
        struct dmamacdescr *desc_table_p = &priv->tx_mac_descrtable[0];
        char *txbuffs = &priv->txbuffs[0];
@@ -30,13 +146,13 @@ static void tx_descs_init(struct eth_device *dev)
 
        for (idx = 0; idx < CONFIG_TX_DESCR_NUM; idx++) {
                desc_p = &desc_table_p[idx];
-               desc_p->dmamac_addr = &txbuffs[idx * CONFIG_ETH_BUFSIZE];
-               desc_p->dmamac_next = &desc_table_p[idx + 1];
+               desc_p->dmamac_addr = (ulong)&txbuffs[idx * CONFIG_ETH_BUFSIZE];
+               desc_p->dmamac_next = (ulong)&desc_table_p[idx + 1];
 
 #if defined(CONFIG_DW_ALTDESCRIPTOR)
                desc_p->txrx_status &= ~(DESC_TXSTS_TXINT | DESC_TXSTS_TXLAST |
-                               DESC_TXSTS_TXFIRST | DESC_TXSTS_TXCRCDIS | \
-                               DESC_TXSTS_TXCHECKINSCTRL | \
+                               DESC_TXSTS_TXFIRST | DESC_TXSTS_TXCRCDIS |
+                               DESC_TXSTS_TXCHECKINSCTRL |
                                DESC_TXSTS_TXRINGEND | DESC_TXSTS_TXPADDIS);
 
                desc_p->txrx_status |= DESC_TXSTS_TXCHAIN;
@@ -49,177 +165,246 @@ static void tx_descs_init(struct eth_device *dev)
        }
 
        /* Correcting the last pointer of the chain */
-       desc_p->dmamac_next = &desc_table_p[0];
+       desc_p->dmamac_next = (ulong)&desc_table_p[0];
+
+       /* Flush all Tx buffer descriptors at once */
+       flush_dcache_range((ulong)priv->tx_mac_descrtable,
+                          (ulong)priv->tx_mac_descrtable +
+                          sizeof(priv->tx_mac_descrtable));
 
        writel((ulong)&desc_table_p[0], &dma_p->txdesclistaddr);
+       priv->tx_currdescnum = 0;
 }
 
-static void rx_descs_init(struct eth_device *dev)
+static void rx_descs_init(struct dw_eth_dev *priv)
 {
-       struct dw_eth_dev *priv = dev->priv;
        struct eth_dma_regs *dma_p = priv->dma_regs_p;
        struct dmamacdescr *desc_table_p = &priv->rx_mac_descrtable[0];
        char *rxbuffs = &priv->rxbuffs[0];
        struct dmamacdescr *desc_p;
        u32 idx;
 
+       /* Before passing buffers to GMAC we need to make sure zeros
+        * written there right after "priv" structure allocation were
+        * flushed into RAM.
+        * Otherwise there's a chance to get some of them flushed in RAM when
+        * GMAC is already pushing data to RAM via DMA. This way incoming from
+        * GMAC data will be corrupted. */
+       flush_dcache_range((ulong)rxbuffs, (ulong)rxbuffs + RX_TOTAL_BUFSIZE);
+
        for (idx = 0; idx < CONFIG_RX_DESCR_NUM; idx++) {
                desc_p = &desc_table_p[idx];
-               desc_p->dmamac_addr = &rxbuffs[idx * CONFIG_ETH_BUFSIZE];
-               desc_p->dmamac_next = &desc_table_p[idx + 1];
+               desc_p->dmamac_addr = (ulong)&rxbuffs[idx * CONFIG_ETH_BUFSIZE];
+               desc_p->dmamac_next = (ulong)&desc_table_p[idx + 1];
 
                desc_p->dmamac_cntl =
-                       (MAC_MAX_FRAME_SZ & DESC_RXCTRL_SIZE1MASK) | \
+                       (MAC_MAX_FRAME_SZ & DESC_RXCTRL_SIZE1MASK) |
                                      DESC_RXCTRL_RXCHAIN;
 
                desc_p->txrx_status = DESC_RXSTS_OWNBYDMA;
        }
 
        /* Correcting the last pointer of the chain */
-       desc_p->dmamac_next = &desc_table_p[0];
+       desc_p->dmamac_next = (ulong)&desc_table_p[0];
+
+       /* Flush all Rx buffer descriptors at once */
+       flush_dcache_range((ulong)priv->rx_mac_descrtable,
+                          (ulong)priv->rx_mac_descrtable +
+                          sizeof(priv->rx_mac_descrtable));
 
        writel((ulong)&desc_table_p[0], &dma_p->rxdesclistaddr);
+       priv->rx_currdescnum = 0;
 }
 
-static void descs_init(struct eth_device *dev)
+static int _dw_write_hwaddr(struct dw_eth_dev *priv, u8 *mac_id)
 {
-       tx_descs_init(dev);
-       rx_descs_init(dev);
+       struct eth_mac_regs *mac_p = priv->mac_regs_p;
+       u32 macid_lo, macid_hi;
+
+       macid_lo = mac_id[0] + (mac_id[1] << 8) + (mac_id[2] << 16) +
+                  (mac_id[3] << 24);
+       macid_hi = mac_id[4] + (mac_id[5] << 8);
+
+       writel(macid_hi, &mac_p->macaddr0hi);
+       writel(macid_lo, &mac_p->macaddr0lo);
+
+       return 0;
 }
 
-static int mac_reset(struct eth_device *dev)
+static int dw_adjust_link(struct dw_eth_dev *priv, struct eth_mac_regs *mac_p,
+                         struct phy_device *phydev)
 {
-       struct dw_eth_dev *priv = dev->priv;
-       struct eth_mac_regs *mac_p = priv->mac_regs_p;
-       struct eth_dma_regs *dma_p = priv->dma_regs_p;
+       u32 conf = readl(&mac_p->conf) | FRAMEBURSTENABLE | DISABLERXOWN;
 
-       ulong start;
-       int timeout = CONFIG_MACRESET_TIMEOUT;
+       if (!phydev->link) {
+               printf("%s: No link.\n", phydev->dev->name);
+               return 0;
+       }
 
-       writel(DMAMAC_SRST, &dma_p->busmode);
+       if (phydev->speed != 1000)
+               conf |= MII_PORTSELECT;
+       else
+               conf &= ~MII_PORTSELECT;
 
-       if (priv->interface != PHY_INTERFACE_MODE_RGMII)
-               writel(MII_PORTSELECT, &mac_p->conf);
+       if (phydev->speed == 100)
+               conf |= FES_100;
 
-       start = get_timer(0);
-       while (get_timer(start) < timeout) {
-               if (!(readl(&dma_p->busmode) & DMAMAC_SRST))
-                       return 0;
+       if (phydev->duplex)
+               conf |= FULLDPLXMODE;
 
-               /* Try again after 10usec */
-               udelay(10);
-       };
+       writel(conf, &mac_p->conf);
+
+       printf("Speed: %d, %s duplex%s\n", phydev->speed,
+              (phydev->duplex) ? "full" : "half",
+              (phydev->port == PORT_FIBRE) ? ", fiber mode" : "");
 
-       return -1;
+       return 0;
 }
 
-static int dw_write_hwaddr(struct eth_device *dev)
+static void _dw_eth_halt(struct dw_eth_dev *priv)
 {
-       struct dw_eth_dev *priv = dev->priv;
        struct eth_mac_regs *mac_p = priv->mac_regs_p;
-       u32 macid_lo, macid_hi;
-       u8 *mac_id = &dev->enetaddr[0];
+       struct eth_dma_regs *dma_p = priv->dma_regs_p;
 
-       macid_lo = mac_id[0] + (mac_id[1] << 8) + \
-                  (mac_id[2] << 16) + (mac_id[3] << 24);
-       macid_hi = mac_id[4] + (mac_id[5] << 8);
+       writel(readl(&mac_p->conf) & ~(RXENABLE | TXENABLE), &mac_p->conf);
+       writel(readl(&dma_p->opmode) & ~(RXSTART | TXSTART), &dma_p->opmode);
 
-       writel(macid_hi, &mac_p->macaddr0hi);
-       writel(macid_lo, &mac_p->macaddr0lo);
-
-       return 0;
+       phy_shutdown(priv->phydev);
 }
 
-static int dw_eth_init(struct eth_device *dev, bd_t *bis)
+int designware_eth_init(struct dw_eth_dev *priv, u8 *enetaddr)
 {
-       struct dw_eth_dev *priv = dev->priv;
        struct eth_mac_regs *mac_p = priv->mac_regs_p;
        struct eth_dma_regs *dma_p = priv->dma_regs_p;
-       u32 conf;
+       unsigned int start;
+       int ret;
 
-       if (priv->phy_configured != 1)
-               configure_phy(dev);
+       writel(readl(&dma_p->busmode) | DMAMAC_SRST, &dma_p->busmode);
 
-       /* Print link status only once */
-       if (!priv->link_printed) {
-               printf("ENET Speed is %d Mbps - %s duplex connection\n",
-                      priv->speed, (priv->duplex == HALF) ? "HALF" : "FULL");
-               priv->link_printed = 1;
-       }
+       /*
+        * When a MII PHY is used, we must set the PS bit for the DMA
+        * reset to succeed.
+        */
+       if (priv->phydev->interface == PHY_INTERFACE_MODE_MII)
+               writel(readl(&mac_p->conf) | MII_PORTSELECT, &mac_p->conf);
+       else
+               writel(readl(&mac_p->conf) & ~MII_PORTSELECT, &mac_p->conf);
 
-       /* Reset ethernet hardware */
-       if (mac_reset(dev) < 0)
-               return -1;
+       start = get_timer(0);
+       while (readl(&dma_p->busmode) & DMAMAC_SRST) {
+               if (get_timer(start) >= CONFIG_MACRESET_TIMEOUT) {
+                       printf("DMA reset timeout\n");
+                       return -ETIMEDOUT;
+               }
 
-       /* Resore the HW MAC address as it has been lost during MAC reset */
-       dw_write_hwaddr(dev);
+               mdelay(100);
+       };
 
-       writel(FIXEDBURST | PRIORXTX_41 | BURST_16,
-                       &dma_p->busmode);
+       /*
+        * Soft reset above clears HW address registers.
+        * So we have to set it here once again.
+        */
+       _dw_write_hwaddr(priv, enetaddr);
 
-       writel(readl(&dma_p->opmode) | FLUSHTXFIFO | STOREFORWARD |
-               TXSECONDFRAME, &dma_p->opmode);
+       rx_descs_init(priv);
+       tx_descs_init(priv);
 
-       conf = FRAMEBURSTENABLE | DISABLERXOWN;
+       writel(FIXEDBURST | PRIORXTX_41 | DMA_PBL, &dma_p->busmode);
 
-       if (priv->speed != 1000)
-               conf |= MII_PORTSELECT;
+#ifndef CONFIG_DW_MAC_FORCE_THRESHOLD_MODE
+       writel(readl(&dma_p->opmode) | FLUSHTXFIFO | STOREFORWARD,
+              &dma_p->opmode);
+#else
+       writel(readl(&dma_p->opmode) | FLUSHTXFIFO,
+              &dma_p->opmode);
+#endif
+
+       writel(readl(&dma_p->opmode) | RXSTART | TXSTART, &dma_p->opmode);
 
-       if ((priv->interface != PHY_INTERFACE_MODE_MII) &&
-               (priv->interface != PHY_INTERFACE_MODE_GMII)) {
+#ifdef CONFIG_DW_AXI_BURST_LEN
+       writel((CONFIG_DW_AXI_BURST_LEN & 0x1FF >> 1), &dma_p->axibus);
+#endif
 
-               if (priv->speed == 100)
-                       conf |= FES_100;
+       /* Start up the PHY */
+       ret = phy_startup(priv->phydev);
+       if (ret) {
+               printf("Could not initialize PHY %s\n",
+                      priv->phydev->dev->name);
+               return ret;
        }
 
-       if (priv->duplex == FULL)
-               conf |= FULLDPLXMODE;
+       ret = dw_adjust_link(priv, mac_p, priv->phydev);
+       if (ret)
+               return ret;
 
-       writel(conf, &mac_p->conf);
+       return 0;
+}
 
-       descs_init(dev);
+int designware_eth_enable(struct dw_eth_dev *priv)
+{
+       struct eth_mac_regs *mac_p = priv->mac_regs_p;
 
-       /*
-        * Start/Enable xfer at dma as well as mac level
-        */
-       writel(readl(&dma_p->opmode) | RXSTART, &dma_p->opmode);
-       writel(readl(&dma_p->opmode) | TXSTART, &dma_p->opmode);
+       if (!priv->phydev->link)
+               return -EIO;
 
        writel(readl(&mac_p->conf) | RXENABLE | TXENABLE, &mac_p->conf);
 
        return 0;
 }
 
-static int dw_eth_send(struct eth_device *dev, void *packet, int length)
+#define ETH_ZLEN       60
+
+static int _dw_eth_send(struct dw_eth_dev *priv, void *packet, int length)
 {
-       struct dw_eth_dev *priv = dev->priv;
        struct eth_dma_regs *dma_p = priv->dma_regs_p;
        u32 desc_num = priv->tx_currdescnum;
        struct dmamacdescr *desc_p = &priv->tx_mac_descrtable[desc_num];
+       ulong desc_start = (ulong)desc_p;
+       ulong desc_end = desc_start +
+               roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
+       ulong data_start = desc_p->dmamac_addr;
+       ulong data_end = data_start + roundup(length, ARCH_DMA_MINALIGN);
+       /*
+        * Strictly we only need to invalidate the "txrx_status" field
+        * for the following check, but on some platforms we cannot
+        * invalidate only 4 bytes, so we flush the entire descriptor,
+        * which is 16 bytes in total. This is safe because the
+        * individual descriptors in the array are each aligned to
+        * ARCH_DMA_MINALIGN and padded appropriately.
+        */
+       invalidate_dcache_range(desc_start, desc_end);
 
        /* Check if the descriptor is owned by CPU */
        if (desc_p->txrx_status & DESC_TXSTS_OWNBYDMA) {
                printf("CPU not owner of tx frame\n");
-               return -1;
+               return -EPERM;
        }
 
-       memcpy((void *)desc_p->dmamac_addr, packet, length);
+       length = max(length, ETH_ZLEN);
+
+       memcpy((void *)data_start, packet, length);
+
+       /* Flush data to be sent */
+       flush_dcache_range(data_start, data_end);
 
 #if defined(CONFIG_DW_ALTDESCRIPTOR)
        desc_p->txrx_status |= DESC_TXSTS_TXFIRST | DESC_TXSTS_TXLAST;
-       desc_p->dmamac_cntl |= (length << DESC_TXCTRL_SIZE1SHFT) & \
+       desc_p->dmamac_cntl |= (length << DESC_TXCTRL_SIZE1SHFT) &
                               DESC_TXCTRL_SIZE1MASK;
 
        desc_p->txrx_status &= ~(DESC_TXSTS_MSK);
        desc_p->txrx_status |= DESC_TXSTS_OWNBYDMA;
 #else
-       desc_p->dmamac_cntl |= ((length << DESC_TXCTRL_SIZE1SHFT) & \
-                              DESC_TXCTRL_SIZE1MASK) | DESC_TXCTRL_TXLAST | \
+       desc_p->dmamac_cntl |= ((length << DESC_TXCTRL_SIZE1SHFT) &
+                              DESC_TXCTRL_SIZE1MASK) | DESC_TXCTRL_TXLAST |
                               DESC_TXCTRL_TXFIRST;
 
        desc_p->txrx_status = DESC_TXSTS_OWNBYDMA;
 #endif
 
+       /* Flush modified buffer descriptor */
+       flush_dcache_range(desc_start, desc_end);
+
        /* Test the wrap-around condition. */
        if (++desc_num >= CONFIG_TX_DESCR_NUM)
                desc_num = 0;
@@ -232,329 +417,449 @@ static int dw_eth_send(struct eth_device *dev, void *packet, int length)
        return 0;
 }
 
-static int dw_eth_recv(struct eth_device *dev)
+static int _dw_eth_recv(struct dw_eth_dev *priv, uchar **packetp)
 {
-       struct dw_eth_dev *priv = dev->priv;
-       u32 desc_num = priv->rx_currdescnum;
+       u32 status, desc_num = priv->rx_currdescnum;
        struct dmamacdescr *desc_p = &priv->rx_mac_descrtable[desc_num];
+       int length = -EAGAIN;
+       ulong desc_start = (ulong)desc_p;
+       ulong desc_end = desc_start +
+               roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
+       ulong data_start = desc_p->dmamac_addr;
+       ulong data_end;
+
+       /* Invalidate entire buffer descriptor */
+       invalidate_dcache_range(desc_start, desc_end);
 
-       u32 status = desc_p->txrx_status;
-       int length = 0;
+       status = desc_p->txrx_status;
 
        /* Check  if the owner is the CPU */
        if (!(status & DESC_RXSTS_OWNBYDMA)) {
 
-               length = (status & DESC_RXSTS_FRMLENMSK) >> \
+               length = (status & DESC_RXSTS_FRMLENMSK) >>
                         DESC_RXSTS_FRMLENSHFT;
 
-               NetReceive(desc_p->dmamac_addr, length);
-
-               /*
-                * Make the current descriptor valid again and go to
-                * the next one
-                */
-               desc_p->txrx_status |= DESC_RXSTS_OWNBYDMA;
-
-               /* Test the wrap-around condition. */
-               if (++desc_num >= CONFIG_RX_DESCR_NUM)
-                       desc_num = 0;
+               /* Invalidate received data */
+               data_end = data_start + roundup(length, ARCH_DMA_MINALIGN);
+               invalidate_dcache_range(data_start, data_end);
+               *packetp = (uchar *)(ulong)desc_p->dmamac_addr;
        }
 
-       priv->rx_currdescnum = desc_num;
-
        return length;
 }
 
-static void dw_eth_halt(struct eth_device *dev)
+static int _dw_free_pkt(struct dw_eth_dev *priv)
 {
-       struct dw_eth_dev *priv = dev->priv;
-
-       mac_reset(dev);
-       priv->tx_currdescnum = priv->rx_currdescnum = 0;
-}
-
-static int eth_mdio_read(struct eth_device *dev, u8 addr, u8 reg, u16 *val)
-{
-       struct dw_eth_dev *priv = dev->priv;
-       struct eth_mac_regs *mac_p = priv->mac_regs_p;
-       ulong start;
-       u32 miiaddr;
-       int timeout = CONFIG_MDIO_TIMEOUT;
-
-       miiaddr = ((addr << MIIADDRSHIFT) & MII_ADDRMSK) | \
-                 ((reg << MIIREGSHIFT) & MII_REGMSK);
+       u32 desc_num = priv->rx_currdescnum;
+       struct dmamacdescr *desc_p = &priv->rx_mac_descrtable[desc_num];
+       ulong desc_start = (ulong)desc_p;
+       ulong desc_end = desc_start +
+               roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
 
-       writel(miiaddr | MII_CLKRANGE_150_250M | MII_BUSY, &mac_p->miiaddr);
+       /*
+        * Make the current descriptor valid again and go to
+        * the next one
+        */
+       desc_p->txrx_status |= DESC_RXSTS_OWNBYDMA;
 
-       start = get_timer(0);
-       while (get_timer(start) < timeout) {
-               if (!(readl(&mac_p->miiaddr) & MII_BUSY)) {
-                       *val = readl(&mac_p->miidata);
-                       return 0;
-               }
+       /* Flush only status field - others weren't changed */
+       flush_dcache_range(desc_start, desc_end);
 
-               /* Try again after 10usec */
-               udelay(10);
-       };
+       /* Test the wrap-around condition. */
+       if (++desc_num >= CONFIG_RX_DESCR_NUM)
+               desc_num = 0;
+       priv->rx_currdescnum = desc_num;
 
-       return -1;
+       return 0;
 }
 
-static int eth_mdio_write(struct eth_device *dev, u8 addr, u8 reg, u16 val)
+static int dw_phy_init(struct dw_eth_dev *priv, void *dev)
 {
-       struct dw_eth_dev *priv = dev->priv;
-       struct eth_mac_regs *mac_p = priv->mac_regs_p;
-       ulong start;
-       u32 miiaddr;
-       int ret = -1, timeout = CONFIG_MDIO_TIMEOUT;
-       u16 value;
+       struct phy_device *phydev;
+       int mask = 0xffffffff, ret;
 
-       writel(val, &mac_p->miidata);
-       miiaddr = ((addr << MIIADDRSHIFT) & MII_ADDRMSK) | \
-                 ((reg << MIIREGSHIFT) & MII_REGMSK) | MII_WRITE;
+#ifdef CONFIG_PHY_ADDR
+       mask = 1 << CONFIG_PHY_ADDR;
+#endif
 
-       writel(miiaddr | MII_CLKRANGE_150_250M | MII_BUSY, &mac_p->miiaddr);
+       phydev = phy_find_by_mask(priv->bus, mask, priv->interface);
+       if (!phydev)
+               return -ENODEV;
 
-       start = get_timer(0);
-       while (get_timer(start) < timeout) {
-               if (!(readl(&mac_p->miiaddr) & MII_BUSY)) {
-                       ret = 0;
-                       break;
-               }
+       phy_connect_dev(phydev, dev);
 
-               /* Try again after 10usec */
-               udelay(10);
-       };
+       phydev->supported &= PHY_GBIT_FEATURES;
+       if (priv->max_speed) {
+               ret = phy_set_supported(phydev, priv->max_speed);
+               if (ret)
+                       return ret;
+       }
+       phydev->advertising = phydev->supported;
 
-       /* Needed as a fix for ST-Phy */
-       eth_mdio_read(dev, addr, reg, &value);
+       priv->phydev = phydev;
+       phy_config(phydev);
 
-       return ret;
+       return 0;
 }
 
-#if defined(CONFIG_DW_SEARCH_PHY)
-static int find_phy(struct eth_device *dev)
+#ifndef CONFIG_DM_ETH
+static int dw_eth_init(struct eth_device *dev, bd_t *bis)
 {
-       int phy_addr = 0;
-       u16 ctrl, oldctrl;
-
-       do {
-               eth_mdio_read(dev, phy_addr, MII_BMCR, &ctrl);
-               oldctrl = ctrl & BMCR_ANENABLE;
-
-               ctrl ^= BMCR_ANENABLE;
-               eth_mdio_write(dev, phy_addr, MII_BMCR, ctrl);
-               eth_mdio_read(dev, phy_addr, MII_BMCR, &ctrl);
-               ctrl &= BMCR_ANENABLE;
+       int ret;
 
-               if (ctrl == oldctrl) {
-                       phy_addr++;
-               } else {
-                       ctrl ^= BMCR_ANENABLE;
-                       eth_mdio_write(dev, phy_addr, MII_BMCR, ctrl);
-
-                       return phy_addr;
-               }
-       } while (phy_addr < 32);
+       ret = designware_eth_init(dev->priv, dev->enetaddr);
+       if (!ret)
+               ret = designware_eth_enable(dev->priv);
 
-       return -1;
+       return ret;
 }
-#endif
 
-static int dw_reset_phy(struct eth_device *dev)
+static int dw_eth_send(struct eth_device *dev, void *packet, int length)
 {
-       struct dw_eth_dev *priv = dev->priv;
-       u16 ctrl;
-       ulong start;
-       int timeout = CONFIG_PHYRESET_TIMEOUT;
-       u32 phy_addr = priv->address;
-
-       eth_mdio_write(dev, phy_addr, MII_BMCR, BMCR_RESET);
+       return _dw_eth_send(dev->priv, packet, length);
+}
 
-       start = get_timer(0);
-       while (get_timer(start) < timeout) {
-               eth_mdio_read(dev, phy_addr, MII_BMCR, &ctrl);
-               if (!(ctrl & BMCR_RESET))
-                       break;
+static int dw_eth_recv(struct eth_device *dev)
+{
+       uchar *packet;
+       int length;
 
-               /* Try again after 10usec */
-               udelay(10);
-       };
+       length = _dw_eth_recv(dev->priv, &packet);
+       if (length == -EAGAIN)
+               return 0;
+       net_process_received_packet(packet, length);
 
-       if (get_timer(start) >= CONFIG_PHYRESET_TIMEOUT)
-               return -1;
+       _dw_free_pkt(dev->priv);
 
-#ifdef CONFIG_PHY_RESET_DELAY
-       udelay(CONFIG_PHY_RESET_DELAY);
-#endif
        return 0;
 }
 
-/*
- * Add weak default function for board specific PHY configuration
- */
-int __weak designware_board_phy_init(struct eth_device *dev, int phy_addr,
-               int (*mii_write)(struct eth_device *, u8, u8, u16),
-               int dw_reset_phy(struct eth_device *))
+static void dw_eth_halt(struct eth_device *dev)
 {
-       return 0;
+       return _dw_eth_halt(dev->priv);
 }
 
-static int configure_phy(struct eth_device *dev)
+static int dw_write_hwaddr(struct eth_device *dev)
 {
-       struct dw_eth_dev *priv = dev->priv;
-       int phy_addr;
-       u16 bmcr;
-#if defined(CONFIG_DW_AUTONEG)
-       u16 bmsr;
-       u32 timeout;
-       ulong start;
-#endif
+       return _dw_write_hwaddr(dev->priv, dev->enetaddr);
+}
 
-#if defined(CONFIG_DW_SEARCH_PHY)
-       phy_addr = find_phy(dev);
-       if (phy_addr >= 0)
-               priv->address = phy_addr;
-       else
-               return -1;
-#else
-       phy_addr = priv->address;
-#endif
+int designware_initialize(ulong base_addr, u32 interface)
+{
+       struct eth_device *dev;
+       struct dw_eth_dev *priv;
+
+       dev = (struct eth_device *) malloc(sizeof(struct eth_device));
+       if (!dev)
+               return -ENOMEM;
 
        /*
-        * Some boards need board specific PHY initialization. This is
-        * after the main driver init code but before the auto negotiation
-        * is run.
+        * Since the priv structure contains the descriptors which need a strict
+        * buswidth alignment, memalign is used to allocate memory
         */
-       if (designware_board_phy_init(dev, phy_addr,
-                                     eth_mdio_write, dw_reset_phy) < 0)
-               return -1;
+       priv = (struct dw_eth_dev *) memalign(ARCH_DMA_MINALIGN,
+                                             sizeof(struct dw_eth_dev));
+       if (!priv) {
+               free(dev);
+               return -ENOMEM;
+       }
 
-       if (dw_reset_phy(dev) < 0)
-               return -1;
+       if ((phys_addr_t)priv + sizeof(*priv) > (1ULL << 32)) {
+               printf("designware: buffers are outside DMA memory\n");
+               return -EINVAL;
+       }
 
-#if defined(CONFIG_DW_AUTONEG)
-       /* Set Auto-Neg Advertisement capabilities to 10/100 half/full */
-       eth_mdio_write(dev, phy_addr, MII_ADVERTISE, 0x1E1);
+       memset(dev, 0, sizeof(struct eth_device));
+       memset(priv, 0, sizeof(struct dw_eth_dev));
 
-       bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
-#else
-       bmcr = BMCR_SPEED100 | BMCR_FULLDPLX;
+       sprintf(dev->name, "dwmac.%lx", base_addr);
+       dev->iobase = (int)base_addr;
+       dev->priv = priv;
 
-#if defined(CONFIG_DW_SPEED10M)
-       bmcr &= ~BMCR_SPEED100;
-#endif
-#if defined(CONFIG_DW_DUPLEXHALF)
-       bmcr &= ~BMCR_FULLDPLX;
-#endif
-#endif
-       if (eth_mdio_write(dev, phy_addr, MII_BMCR, bmcr) < 0)
-               return -1;
+       priv->dev = dev;
+       priv->mac_regs_p = (struct eth_mac_regs *)base_addr;
+       priv->dma_regs_p = (struct eth_dma_regs *)(base_addr +
+                       DW_DMA_BASE_OFFSET);
 
-       /* Read the phy status register and populate priv structure */
-#if defined(CONFIG_DW_AUTONEG)
-       timeout = CONFIG_AUTONEG_TIMEOUT;
-       start = get_timer(0);
-       puts("Waiting for PHY auto negotiation to complete");
-       while (get_timer(start) < timeout) {
-               eth_mdio_read(dev, phy_addr, MII_BMSR, &bmsr);
-               if (bmsr & BMSR_ANEGCOMPLETE) {
-                       priv->phy_configured = 1;
-                       break;
-               }
+       dev->init = dw_eth_init;
+       dev->send = dw_eth_send;
+       dev->recv = dw_eth_recv;
+       dev->halt = dw_eth_halt;
+       dev->write_hwaddr = dw_write_hwaddr;
+
+       eth_register(dev);
 
-               /* Print dot all 1s to show progress */
-               if ((get_timer(start) % 1000) == 0)
-                       putc('.');
+       priv->interface = interface;
 
-               /* Try again after 1msec */
-               udelay(1000);
-       };
+       dw_mdio_init(dev->name, priv->mac_regs_p);
+       priv->bus = miiphy_get_dev_by_name(dev->name);
 
-       if (!(bmsr & BMSR_ANEGCOMPLETE))
-               puts(" TIMEOUT!\n");
-       else
-               puts(" done\n");
-#else
-       priv->phy_configured = 1;
+       return dw_phy_init(priv, dev);
+}
 #endif
 
-       priv->speed = miiphy_speed(dev->name, phy_addr);
-       priv->duplex = miiphy_duplex(dev->name, phy_addr);
+#ifdef CONFIG_DM_ETH
+static int designware_eth_start(struct udevice *dev)
+{
+       struct eth_pdata *pdata = dev_get_platdata(dev);
+       struct dw_eth_dev *priv = dev_get_priv(dev);
+       int ret;
+
+       ret = designware_eth_init(priv, pdata->enetaddr);
+       if (ret)
+               return ret;
+       ret = designware_eth_enable(priv);
+       if (ret)
+               return ret;
 
        return 0;
 }
 
-#if defined(CONFIG_MII)
-static int dw_mii_read(const char *devname, u8 addr, u8 reg, u16 *val)
+int designware_eth_send(struct udevice *dev, void *packet, int length)
 {
-       struct eth_device *dev;
+       struct dw_eth_dev *priv = dev_get_priv(dev);
 
-       dev = eth_get_dev_by_name(devname);
-       if (dev)
-               eth_mdio_read(dev, addr, reg, val);
+       return _dw_eth_send(priv, packet, length);
+}
 
-       return 0;
+int designware_eth_recv(struct udevice *dev, int flags, uchar **packetp)
+{
+       struct dw_eth_dev *priv = dev_get_priv(dev);
+
+       return _dw_eth_recv(priv, packetp);
 }
 
-static int dw_mii_write(const char *devname, u8 addr, u8 reg, u16 val)
+int designware_eth_free_pkt(struct udevice *dev, uchar *packet, int length)
 {
-       struct eth_device *dev;
+       struct dw_eth_dev *priv = dev_get_priv(dev);
 
-       dev = eth_get_dev_by_name(devname);
-       if (dev)
-               eth_mdio_write(dev, addr, reg, val);
+       return _dw_free_pkt(priv);
+}
 
-       return 0;
+void designware_eth_stop(struct udevice *dev)
+{
+       struct dw_eth_dev *priv = dev_get_priv(dev);
+
+       return _dw_eth_halt(priv);
+}
+
+int designware_eth_write_hwaddr(struct udevice *dev)
+{
+       struct eth_pdata *pdata = dev_get_platdata(dev);
+       struct dw_eth_dev *priv = dev_get_priv(dev);
+
+       return _dw_write_hwaddr(priv, pdata->enetaddr);
 }
+
+static int designware_eth_bind(struct udevice *dev)
+{
+#ifdef CONFIG_DM_PCI
+       static int num_cards;
+       char name[20];
+
+       /* Create a unique device name for PCI type devices */
+       if (device_is_on_pci_bus(dev)) {
+               sprintf(name, "eth_designware#%u", num_cards++);
+               device_set_name(dev, name);
+       }
 #endif
 
-int designware_initialize(u32 id, ulong base_addr, u32 phy_addr, u32 interface)
+       return 0;
+}
+
+int designware_eth_probe(struct udevice *dev)
 {
-       struct eth_device *dev;
-       struct dw_eth_dev *priv;
+       struct eth_pdata *pdata = dev_get_platdata(dev);
+       struct dw_eth_dev *priv = dev_get_priv(dev);
+       u32 iobase = pdata->iobase;
+       ulong ioaddr;
+       int ret;
+       struct reset_ctl_bulk reset_bulk;
+#ifdef CONFIG_CLK
+       int i, err, clock_nb;
+
+       priv->clock_count = 0;
+       clock_nb = dev_count_phandle_with_args(dev, "clocks", "#clock-cells");
+       if (clock_nb > 0) {
+               priv->clocks = devm_kcalloc(dev, clock_nb, sizeof(struct clk),
+                                           GFP_KERNEL);
+               if (!priv->clocks)
+                       return -ENOMEM;
+
+               for (i = 0; i < clock_nb; i++) {
+                       err = clk_get_by_index(dev, i, &priv->clocks[i]);
+                       if (err < 0)
+                               break;
+
+                       err = clk_enable(&priv->clocks[i]);
+                       if (err && err != -ENOSYS && err != -ENOTSUPP) {
+                               pr_err("failed to enable clock %d\n", i);
+                               clk_free(&priv->clocks[i]);
+                               goto clk_err;
+                       }
+                       priv->clock_count++;
+               }
+       } else if (clock_nb != -ENOENT) {
+               pr_err("failed to get clock phandle(%d)\n", clock_nb);
+               return clock_nb;
+       }
+#endif
 
-       dev = (struct eth_device *) malloc(sizeof(struct eth_device));
-       if (!dev)
-               return -ENOMEM;
+#if defined(CONFIG_DM_REGULATOR)
+       struct udevice *phy_supply;
+
+       ret = device_get_supply_regulator(dev, "phy-supply",
+                                         &phy_supply);
+       if (ret) {
+               debug("%s: No phy supply\n", dev->name);
+       } else {
+               ret = regulator_set_enable(phy_supply, true);
+               if (ret) {
+                       puts("Error enabling phy supply\n");
+                       return ret;
+               }
+       }
+#endif
+
+       ret = reset_get_bulk(dev, &reset_bulk);
+       if (ret)
+               dev_warn(dev, "Can't get reset: %d\n", ret);
+       else
+               reset_deassert_bulk(&reset_bulk);
 
+#ifdef CONFIG_DM_PCI
        /*
-        * Since the priv structure contains the descriptors which need a strict
-        * buswidth alignment, memalign is used to allocate memory
+        * If we are on PCI bus, either directly attached to a PCI root port,
+        * or via a PCI bridge, fill in platdata before we probe the hardware.
         */
-       priv = (struct dw_eth_dev *) memalign(16, sizeof(struct dw_eth_dev));
-       if (!priv) {
-               free(dev);
-               return -ENOMEM;
+       if (device_is_on_pci_bus(dev)) {
+               dm_pci_read_config32(dev, PCI_BASE_ADDRESS_0, &iobase);
+               iobase &= PCI_BASE_ADDRESS_MEM_MASK;
+               iobase = dm_pci_mem_to_phys(dev, iobase);
+
+               pdata->iobase = iobase;
+               pdata->phy_interface = PHY_INTERFACE_MODE_RMII;
        }
+#endif
 
-       memset(dev, 0, sizeof(struct eth_device));
-       memset(priv, 0, sizeof(struct dw_eth_dev));
+       debug("%s, iobase=%x, priv=%p\n", __func__, iobase, priv);
+       ioaddr = iobase;
+       priv->mac_regs_p = (struct eth_mac_regs *)ioaddr;
+       priv->dma_regs_p = (struct eth_dma_regs *)(ioaddr + DW_DMA_BASE_OFFSET);
+       priv->interface = pdata->phy_interface;
+       priv->max_speed = pdata->max_speed;
 
-       sprintf(dev->name, "mii%d", id);
-       dev->iobase = (int)base_addr;
-       dev->priv = priv;
+       dw_mdio_init(dev->name, dev);
+       priv->bus = miiphy_get_dev_by_name(dev->name);
 
-       eth_getenv_enetaddr_by_index("eth", id, &dev->enetaddr[0]);
+       ret = dw_phy_init(priv, dev);
+       debug("%s, ret=%d\n", __func__, ret);
 
-       priv->dev = dev;
-       priv->mac_regs_p = (struct eth_mac_regs *)base_addr;
-       priv->dma_regs_p = (struct eth_dma_regs *)(base_addr +
-                       DW_DMA_BASE_OFFSET);
-       priv->address = phy_addr;
-       priv->phy_configured = 0;
-       priv->interface = interface;
+       return ret;
 
-       dev->init = dw_eth_init;
-       dev->send = dw_eth_send;
-       dev->recv = dw_eth_recv;
-       dev->halt = dw_eth_halt;
-       dev->write_hwaddr = dw_write_hwaddr;
+#ifdef CONFIG_CLK
+clk_err:
+       ret = clk_release_all(priv->clocks, priv->clock_count);
+       if (ret)
+               pr_err("failed to disable all clocks\n");
 
-       eth_register(dev);
+       return err;
+#endif
+}
 
-#if defined(CONFIG_MII)
-       miiphy_register(dev->name, dw_mii_read, dw_mii_write);
+static int designware_eth_remove(struct udevice *dev)
+{
+       struct dw_eth_dev *priv = dev_get_priv(dev);
+
+       free(priv->phydev);
+       mdio_unregister(priv->bus);
+       mdio_free(priv->bus);
+
+#ifdef CONFIG_CLK
+       return clk_release_all(priv->clocks, priv->clock_count);
+#else
+       return 0;
+#endif
+}
+
+const struct eth_ops designware_eth_ops = {
+       .start                  = designware_eth_start,
+       .send                   = designware_eth_send,
+       .recv                   = designware_eth_recv,
+       .free_pkt               = designware_eth_free_pkt,
+       .stop                   = designware_eth_stop,
+       .write_hwaddr           = designware_eth_write_hwaddr,
+};
+
+int designware_eth_ofdata_to_platdata(struct udevice *dev)
+{
+       struct dw_eth_pdata *dw_pdata = dev_get_platdata(dev);
+#ifdef CONFIG_DM_GPIO
+       struct dw_eth_dev *priv = dev_get_priv(dev);
+#endif
+       struct eth_pdata *pdata = &dw_pdata->eth_pdata;
+       const char *phy_mode;
+#ifdef CONFIG_DM_GPIO
+       int reset_flags = GPIOD_IS_OUT;
 #endif
-       return 1;
+       int ret = 0;
+
+       pdata->iobase = dev_read_addr(dev);
+       pdata->phy_interface = -1;
+       phy_mode = dev_read_string(dev, "phy-mode");
+       if (phy_mode)
+               pdata->phy_interface = phy_get_interface_by_name(phy_mode);
+       if (pdata->phy_interface == -1) {
+               debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
+               return -EINVAL;
+       }
+
+       pdata->max_speed = dev_read_u32_default(dev, "max-speed", 0);
+
+#ifdef CONFIG_DM_GPIO
+       if (dev_read_bool(dev, "snps,reset-active-low"))
+               reset_flags |= GPIOD_ACTIVE_LOW;
+
+       ret = gpio_request_by_name(dev, "snps,reset-gpio", 0,
+               &priv->reset_gpio, reset_flags);
+       if (ret == 0) {
+               ret = dev_read_u32_array(dev, "snps,reset-delays-us",
+                                        dw_pdata->reset_delays, 3);
+       } else if (ret == -ENOENT) {
+               ret = 0;
+       }
+#endif
+
+       return ret;
 }
+
+static const struct udevice_id designware_eth_ids[] = {
+       { .compatible = "allwinner,sun7i-a20-gmac" },
+       { .compatible = "altr,socfpga-stmmac" },
+       { .compatible = "amlogic,meson6-dwmac" },
+       { .compatible = "amlogic,meson-gx-dwmac" },
+       { .compatible = "st,stm32-dwmac" },
+       { }
+};
+
+U_BOOT_DRIVER(eth_designware) = {
+       .name   = "eth_designware",
+       .id     = UCLASS_ETH,
+       .of_match = designware_eth_ids,
+       .ofdata_to_platdata = designware_eth_ofdata_to_platdata,
+       .bind   = designware_eth_bind,
+       .probe  = designware_eth_probe,
+       .remove = designware_eth_remove,
+       .ops    = &designware_eth_ops,
+       .priv_auto_alloc_size = sizeof(struct dw_eth_dev),
+       .platdata_auto_alloc_size = sizeof(struct dw_eth_pdata),
+       .flags = DM_FLAG_ALLOC_PRIV_DMA,
+};
+
+static struct pci_device_id supported[] = {
+       { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_QRK_EMAC) },
+       { }
+};
+
+U_BOOT_PCI_DEVICE(eth_designware, supported);
+#endif