]> git.ipfire.org Git - people/ms/u-boot.git/blobdiff - drivers/net/ethoc.c
net/ethoc: implement MDIO bus and support phylib
[people/ms/u-boot.git] / drivers / net / ethoc.c
index a5c4b4621d9b58b82ca52535e1a619cd81c90b6e..ad8c462a6099b62a4f6b2485564919b73499ea9c 100644 (file)
@@ -18,6 +18,7 @@
 #include <net.h>
 #include <miiphy.h>
 #include <asm/cache.h>
+#include <wait_bit.h>
 
 /* register offsets */
 #define        MODER           0x00
@@ -179,6 +180,13 @@ struct ethoc {
        u32 num_rx;
        u32 cur_rx;
        void __iomem *iobase;
+       void __iomem *packet;
+       phys_addr_t packet_phys;
+
+#ifdef CONFIG_PHYLIB
+       struct mii_dev *bus;
+       struct phy_device *phydev;
+#endif
 };
 
 /**
@@ -191,14 +199,19 @@ struct ethoc_bd {
        u32 addr;
 };
 
+static inline u32 *ethoc_reg(struct ethoc *priv, size_t offset)
+{
+       return priv->iobase + offset;
+}
+
 static inline u32 ethoc_read(struct ethoc *priv, size_t offset)
 {
-       return readl(priv->iobase + offset);
+       return readl(ethoc_reg(priv, offset));
 }
 
 static inline void ethoc_write(struct ethoc *priv, size_t offset, u32 data)
 {
-       writel(data, priv->iobase + offset);
+       writel(data, ethoc_reg(priv, offset));
 }
 
 static inline void ethoc_read_bd(struct ethoc *priv, int index,
@@ -247,6 +260,7 @@ static inline void ethoc_disable_rx_and_tx(struct ethoc *priv)
 static int ethoc_init_ring(struct ethoc *priv)
 {
        struct ethoc_bd bd;
+       phys_addr_t addr = priv->packet_phys;
        int i;
 
        priv->cur_tx = 0;
@@ -255,8 +269,13 @@ static int ethoc_init_ring(struct ethoc *priv)
 
        /* setup transmission buffers */
        bd.stat = TX_BD_IRQ | TX_BD_CRC;
+       bd.addr = 0;
 
        for (i = 0; i < priv->num_tx; i++) {
+               if (addr) {
+                       bd.addr = addr;
+                       addr += PKTSIZE_ALIGN;
+               }
                if (i == priv->num_tx - 1)
                        bd.stat |= TX_BD_WRAP;
 
@@ -266,11 +285,17 @@ static int ethoc_init_ring(struct ethoc *priv)
        bd.stat = RX_BD_EMPTY | RX_BD_IRQ;
 
        for (i = 0; i < priv->num_rx; i++) {
-               bd.addr = (u32)net_rx_packets[i];
+               if (addr) {
+                       bd.addr = addr;
+                       addr += PKTSIZE_ALIGN;
+               } else {
+                       bd.addr = virt_to_phys(net_rx_packets[i]);
+               }
                if (i == priv->num_rx - 1)
                        bd.stat |= RX_BD_WRAP;
 
-               flush_dcache_range(bd.addr, bd.addr + PKTSIZE_ALIGN);
+               flush_dcache_range((ulong)net_rx_packets[i],
+                                  (ulong)net_rx_packets[i] + PKTSIZE_ALIGN);
                ethoc_write_bd(priv, priv->num_tx + i, &bd);
        }
 
@@ -305,13 +330,31 @@ static int ethoc_reset(struct ethoc *priv)
 
 static int ethoc_init_common(struct ethoc *priv)
 {
+       int ret = 0;
+
        priv->num_tx = 1;
        priv->num_rx = PKTBUFSRX;
        ethoc_write(priv, TX_BD_NUM, priv->num_tx);
        ethoc_init_ring(priv);
        ethoc_reset(priv);
 
-       return 0;
+#ifdef CONFIG_PHYLIB
+       ret = phy_startup(priv->phydev);
+       if (ret) {
+               printf("Could not initialize PHY %s\n",
+                      priv->phydev->dev->name);
+               return ret;
+       }
+#endif
+       return ret;
+}
+
+static void ethoc_stop_common(struct ethoc *priv)
+{
+       ethoc_disable_rx_and_tx(priv);
+#ifdef CONFIG_PHYLIB
+       phy_shutdown(priv->phydev);
+#endif
 }
 
 static int ethoc_update_rx_stats(struct ethoc_bd *bd)
@@ -351,10 +394,10 @@ static int ethoc_update_rx_stats(struct ethoc_bd *bd)
 
 static int ethoc_rx_common(struct ethoc *priv, uchar **packetp)
 {
-       u32 entry;
        struct ethoc_bd bd;
+       u32 i = priv->cur_rx % priv->num_rx;
+       u32 entry = priv->num_tx + i;
 
-       entry = priv->num_tx + (priv->cur_rx % priv->num_rx);
        ethoc_read_bd(priv, entry, &bd);
        if (bd.stat & RX_BD_EMPTY)
                return -EAGAIN;
@@ -365,7 +408,10 @@ static int ethoc_rx_common(struct ethoc *priv, uchar **packetp)
                int size = bd.stat >> 16;
 
                size -= 4;      /* strip the CRC */
-               *packetp = (void *)bd.addr;
+               if (priv->packet)
+                       *packetp = priv->packet + entry * PKTSIZE_ALIGN;
+               else
+                       *packetp = net_rx_packets[i];
                return size;
        } else {
                return 0;
@@ -428,9 +474,16 @@ static int ethoc_send_common(struct ethoc *priv, void *packet, int length)
                bd.stat |= TX_BD_PAD;
        else
                bd.stat &= ~TX_BD_PAD;
-       bd.addr = (u32)packet;
 
-       flush_dcache_range(bd.addr, bd.addr + length);
+       if (priv->packet) {
+               void *p = priv->packet + entry * PKTSIZE_ALIGN;
+
+               memcpy(p, packet, length);
+               packet = p;
+       } else {
+               bd.addr = virt_to_phys(packet);
+       }
+       flush_dcache_range((ulong)packet, (ulong)packet + length);
        bd.stat &= ~(TX_BD_STATS | TX_BD_LEN_MASK);
        bd.stat |= TX_BD_LEN(length);
        ethoc_write_bd(priv, entry, &bd);
@@ -463,14 +516,20 @@ static int ethoc_send_common(struct ethoc *priv, void *packet, int length)
 
 static int ethoc_free_pkt_common(struct ethoc *priv)
 {
-       u32 entry;
        struct ethoc_bd bd;
+       u32 i = priv->cur_rx % priv->num_rx;
+       u32 entry = priv->num_tx + i;
+       void *src;
 
-       entry = priv->num_tx + (priv->cur_rx % priv->num_rx);
        ethoc_read_bd(priv, entry, &bd);
 
+       if (priv->packet)
+               src = priv->packet + entry * PKTSIZE_ALIGN;
+       else
+               src = net_rx_packets[i];
        /* clear the buffer descriptor so it can be reused */
-       flush_dcache_range(bd.addr, bd.addr + PKTSIZE_ALIGN);
+       flush_dcache_range((ulong)src,
+                          (ulong)src + PKTSIZE_ALIGN);
        bd.stat &= ~RX_BD_STATS;
        bd.stat |= RX_BD_EMPTY;
        ethoc_write_bd(priv, entry, &bd);
@@ -479,6 +538,110 @@ static int ethoc_free_pkt_common(struct ethoc *priv)
        return 0;
 }
 
+#ifdef CONFIG_PHYLIB
+
+static int ethoc_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
+{
+       struct ethoc *priv = bus->priv;
+       int rc;
+
+       ethoc_write(priv, MIIADDRESS, MIIADDRESS_ADDR(addr, reg));
+       ethoc_write(priv, MIICOMMAND, MIICOMMAND_READ);
+
+       rc = wait_for_bit(__func__, ethoc_reg(priv, MIISTATUS),
+                         MIISTATUS_BUSY, false, CONFIG_SYS_HZ, false);
+
+       if (rc == 0) {
+               u32 data = ethoc_read(priv, MIIRX_DATA);
+
+               /* reset MII command register */
+               ethoc_write(priv, MIICOMMAND, 0);
+               return data;
+       }
+       return rc;
+}
+
+static int ethoc_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
+                           u16 val)
+{
+       struct ethoc *priv = bus->priv;
+       int rc;
+
+       ethoc_write(priv, MIIADDRESS, MIIADDRESS_ADDR(addr, reg));
+       ethoc_write(priv, MIITX_DATA, val);
+       ethoc_write(priv, MIICOMMAND, MIICOMMAND_WRITE);
+
+       rc = wait_for_bit(__func__, ethoc_reg(priv, MIISTATUS),
+                         MIISTATUS_BUSY, false, CONFIG_SYS_HZ, false);
+
+       if (rc == 0) {
+               /* reset MII command register */
+               ethoc_write(priv, MIICOMMAND, 0);
+       }
+       return rc;
+}
+
+static int ethoc_mdio_init(const char *name, struct ethoc *priv)
+{
+       struct mii_dev *bus = mdio_alloc();
+       int ret;
+
+       if (!bus) {
+               printf("Failed to allocate MDIO bus\n");
+               return -ENOMEM;
+       }
+
+       bus->read = ethoc_mdio_read;
+       bus->write = ethoc_mdio_write;
+       snprintf(bus->name, sizeof(bus->name), "%s", name);
+       bus->priv = priv;
+
+       ret = mdio_register(bus);
+       if (ret < 0)
+               return ret;
+
+       priv->bus = miiphy_get_dev_by_name(name);
+       return 0;
+}
+
+static int ethoc_phy_init(struct ethoc *priv, void *dev)
+{
+       struct phy_device *phydev;
+       int mask = 0xffffffff;
+
+#ifdef CONFIG_PHY_ADDR
+       mask = 1 << CONFIG_PHY_ADDR;
+#endif
+
+       phydev = phy_find_by_mask(priv->bus, mask, PHY_INTERFACE_MODE_MII);
+       if (!phydev)
+               return -ENODEV;
+
+       phy_connect_dev(phydev, dev);
+
+       phydev->supported &= PHY_BASIC_FEATURES;
+       phydev->advertising = phydev->supported;
+
+       priv->phydev = phydev;
+       phy_config(phydev);
+
+       return 0;
+}
+
+#else
+
+static inline int ethoc_mdio_init(const char *name, struct ethoc *priv)
+{
+       return 0;
+}
+
+static inline int ethoc_phy_init(struct ethoc *priv, void *dev)
+{
+       return 0;
+}
+
+#endif
+
 #ifdef CONFIG_DM_ETH
 
 static int ethoc_write_hwaddr(struct udevice *dev)
@@ -518,16 +681,18 @@ static int ethoc_start(struct udevice *dev)
 
 static void ethoc_stop(struct udevice *dev)
 {
-       struct ethoc *priv = dev_get_priv(dev);
-
-       ethoc_disable_rx_and_tx(priv);
+       ethoc_stop_common(dev_get_priv(dev));
 }
 
 static int ethoc_ofdata_to_platdata(struct udevice *dev)
 {
        struct ethoc_eth_pdata *pdata = dev_get_platdata(dev);
+       fdt_addr_t addr;
 
        pdata->eth_pdata.iobase = dev_get_addr(dev);
+       addr = dev_get_addr_index(dev, 1);
+       if (addr != FDT_ADDR_T_NONE)
+               pdata->packet_base = addr;
        return 0;
 }
 
@@ -537,6 +702,15 @@ static int ethoc_probe(struct udevice *dev)
        struct ethoc *priv = dev_get_priv(dev);
 
        priv->iobase = ioremap(pdata->eth_pdata.iobase, ETHOC_IOSIZE);
+       if (pdata->packet_base) {
+               priv->packet_phys = pdata->packet_base;
+               priv->packet = ioremap(pdata->packet_base,
+                                      (1 + PKTBUFSRX) * PKTSIZE_ALIGN);
+       }
+
+       ethoc_mdio_init(dev->name, priv);
+       ethoc_phy_init(priv, dev);
+
        return 0;
 }
 
@@ -544,6 +718,11 @@ static int ethoc_remove(struct udevice *dev)
 {
        struct ethoc *priv = dev_get_priv(dev);
 
+#ifdef CONFIG_PHYLIB
+       free(priv->phydev);
+       mdio_unregister(priv->bus);
+       mdio_free(priv->bus);
+#endif
        iounmap(priv->iobase);
        return 0;
 }
@@ -648,6 +827,10 @@ int ethoc_initialize(u8 dev_num, int base_addr)
        priv->iobase = ioremap(dev->iobase, ETHOC_IOSIZE);
 
        eth_register(dev);
+
+       ethoc_mdio_init(dev->name, priv);
+       ethoc_phy_init(priv, dev);
+
        return 1;
 }