net: tsec: fsl_mdio: add DM MDIO support
[oweals/u-boot.git] / drivers / net / fsl_mdio.c
index 1aab307897eae96e2d05618108f5ad69f0d2d2e2..43040d4c3f89d207b060967a67e337f3d6835c5b 100644 (file)
@@ -1,61 +1,55 @@
+// SPDX-License-Identifier: GPL-2.0+
 /*
- * Copyright 2009-2010 Freescale Semiconductor, Inc.
+ * Copyright 2009-2010, 2013 Freescale Semiconductor, Inc.
  *     Jun-jie Zhang <b18070@freescale.com>
  *     Mingkai Hu <Mingkai.hu@freescale.com>
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of
- * the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
- * MA 02111-1307 USA
  */
+
 #include <common.h>
 #include <miiphy.h>
 #include <phy.h>
 #include <fsl_mdio.h>
 #include <asm/io.h>
-#include <asm/errno.h>
-#include <asm/fsl_enet.h>
+#include <linux/errno.h>
+
+#ifdef CONFIG_DM_MDIO
+struct tsec_mdio_priv {
+       struct tsec_mii_mng __iomem *regs;
+};
+#endif
 
-void tsec_local_mdio_write(struct tsec_mii_mng *phyregs, int port_addr,
+void tsec_local_mdio_write(struct tsec_mii_mng __iomem *phyregs, int port_addr,
                int dev_addr, int regnum, int value)
 {
        int timeout = 1000000;
 
        out_be32(&phyregs->miimadd, (port_addr << 8) | (regnum & 0x1f));
        out_be32(&phyregs->miimcon, value);
-       asm("sync");
+       /* Memory barrier */
+       mb();
 
        while ((in_be32(&phyregs->miimind) & MIIMIND_BUSY) && timeout--)
                ;
 }
 
-int tsec_local_mdio_read(struct tsec_mii_mng *phyregs, int port_addr,
+int tsec_local_mdio_read(struct tsec_mii_mng __iomem *phyregs, int port_addr,
                int dev_addr, int regnum)
 {
        int value;
        int timeout = 1000000;
 
-       /* Put the address of the phy, and the register
-        * number into MIIMADD */
+       /* Put the address of the phy, and the register number into MIIMADD */
        out_be32(&phyregs->miimadd, (port_addr << 8) | (regnum & 0x1f));
 
        /* Clear the command register, and wait */
        out_be32(&phyregs->miimcom, 0);
-       asm("sync");
+       /* Memory barrier */
+       mb();
 
        /* Initiate a read command, and wait */
        out_be32(&phyregs->miimcom, MIIMCOM_READ_CYCLE);
-       asm("sync");
+       /* Memory barrier */
+       mb();
 
        /* Wait for the the indication that the read is done */
        while ((in_be32(&phyregs->miimind) & (MIIMIND_NOTVALID | MIIMIND_BUSY))
@@ -68,9 +62,21 @@ int tsec_local_mdio_read(struct tsec_mii_mng *phyregs, int port_addr,
        return value;
 }
 
+#if defined(CONFIG_PHYLIB)
 static int fsl_pq_mdio_reset(struct mii_dev *bus)
 {
-       struct tsec_mii_mng *regs = bus->priv;
+       struct tsec_mii_mng __iomem *regs;
+#ifndef CONFIG_DM_MDIO
+       regs = (struct tsec_mii_mng __iomem *)bus->priv;
+#else
+       struct tsec_mdio_priv *priv;
+
+       if (!bus->priv)
+               return -EINVAL;
+
+       priv = dev_get_priv(bus->priv);
+       regs = priv->regs;
+#endif
 
        /* Reset MII (due to new addresses) */
        out_be32(&regs->miimcfg, MIIMCFG_RESET_MGMT);
@@ -82,10 +88,22 @@ static int fsl_pq_mdio_reset(struct mii_dev *bus)
 
        return 0;
 }
+#endif
 
 int tsec_phy_read(struct mii_dev *bus, int addr, int dev_addr, int regnum)
 {
-       struct tsec_mii_mng *phyregs = bus->priv;
+       struct tsec_mii_mng __iomem *phyregs;
+#ifndef CONFIG_DM_MDIO
+       phyregs = (struct tsec_mii_mng __iomem *)bus->priv;
+#else
+       struct tsec_mdio_priv *priv;
+
+       if (!bus->priv)
+               return -EINVAL;
+
+       priv = dev_get_priv(bus->priv);
+       phyregs = priv->regs;
+#endif
 
        return tsec_local_mdio_read(phyregs, addr, dev_addr, regnum);
 }
@@ -93,13 +111,25 @@ int tsec_phy_read(struct mii_dev *bus, int addr, int dev_addr, int regnum)
 int tsec_phy_write(struct mii_dev *bus, int addr, int dev_addr, int regnum,
                        u16 value)
 {
-       struct tsec_mii_mng *phyregs = bus->priv;
+       struct tsec_mii_mng __iomem *phyregs;
+#ifndef CONFIG_DM_MDIO
+       phyregs = (struct tsec_mii_mng __iomem *)bus->priv;
+#else
+       struct tsec_mdio_priv *priv;
+
+       if (!bus->priv)
+               return -EINVAL;
+
+       priv = dev_get_priv(bus->priv);
+       phyregs = priv->regs;
+#endif
 
        tsec_local_mdio_write(phyregs, addr, dev_addr, regnum, value);
 
        return 0;
 }
 
+#ifndef CONFIG_DM_MDIO
 int fsl_pq_mdio_init(bd_t *bis, struct fsl_pq_mdio_info *info)
 {
        struct mii_dev *bus = mdio_alloc();
@@ -112,9 +142,98 @@ int fsl_pq_mdio_init(bd_t *bis, struct fsl_pq_mdio_info *info)
        bus->read = tsec_phy_read;
        bus->write = tsec_phy_write;
        bus->reset = fsl_pq_mdio_reset;
-       sprintf(bus->name, info->name);
+       strcpy(bus->name, info->name);
 
-       bus->priv = info->regs;
+       bus->priv = (void *)info->regs;
 
        return mdio_register(bus);
 }
+#else /* CONFIG_DM_MDIO */
+#if defined(CONFIG_PHYLIB)
+static int tsec_mdio_read(struct udevice *dev, int addr, int devad, int reg)
+{
+       struct mdio_perdev_priv *pdata = (dev) ? dev_get_uclass_priv(dev) :
+                                                NULL;
+
+       if (pdata && pdata->mii_bus)
+               return tsec_phy_read(pdata->mii_bus, addr, devad, reg);
+
+       return -1;
+}
+
+static int tsec_mdio_write(struct udevice *dev, int addr, int devad, int reg,
+                          u16 val)
+{
+       struct mdio_perdev_priv *pdata = (dev) ? dev_get_uclass_priv(dev) :
+                                                NULL;
+
+       if (pdata && pdata->mii_bus)
+               return tsec_phy_write(pdata->mii_bus, addr, devad, reg, val);
+
+       return -1;
+}
+
+static int tsec_mdio_reset(struct udevice *dev)
+{
+       struct mdio_perdev_priv *pdata = (dev) ? dev_get_uclass_priv(dev) :
+                                                NULL;
+
+       if (pdata && pdata->mii_bus)
+               return fsl_pq_mdio_reset(pdata->mii_bus);
+
+       return -1;
+}
+
+static const struct mdio_ops tsec_mdio_ops = {
+       .read = tsec_mdio_read,
+       .write = tsec_mdio_write,
+       .reset = tsec_mdio_reset,
+};
+
+static const struct udevice_id tsec_mdio_ids[] = {
+       { .compatible = "fsl,gianfar-tbi" },
+       { .compatible = "fsl,gianfar-mdio" },
+       { .compatible = "fsl,etsec2-tbi" },
+       { .compatible = "fsl,etsec2-mdio" },
+       { .compatible = "fsl,fman-mdio" },
+       {}
+};
+
+static int tsec_mdio_probe(struct udevice *dev)
+{
+       struct tsec_mdio_priv *priv = (dev) ? dev_get_priv(dev) : NULL;
+       struct mdio_perdev_priv *pdata = (dev) ? dev_get_uclass_priv(dev) :
+                                                NULL;
+
+       if (!dev) {
+               printf("%s dev = NULL\n", __func__);
+               return -1;
+       }
+       if (!priv) {
+               printf("dev_get_priv(dev %p) = NULL\n", dev);
+               return -1;
+       }
+       priv->regs = (void *)(uintptr_t)dev_read_addr(dev);
+       debug("%s priv %p @ regs %p, pdata %p\n", __func__,
+             priv, priv->regs, pdata);
+
+       return 0;
+}
+
+static int tsec_mdio_remove(struct udevice *dev)
+{
+       return 0;
+}
+
+U_BOOT_DRIVER(tsec_mdio) = {
+       .name = "tsec_mdio",
+       .id = UCLASS_MDIO,
+       .of_match = tsec_mdio_ids,
+       .probe = tsec_mdio_probe,
+       .remove = tsec_mdio_remove,
+       .ops = &tsec_mdio_ops,
+       .priv_auto_alloc_size = sizeof(struct tsec_mdio_priv),
+       .platdata_auto_alloc_size = sizeof(struct mdio_perdev_priv),
+};
+#endif /* CONFIG_PHYLIB */
+#endif /* CONFIG_DM_MDIO */