net: tsec: fsl_mdio: add DM MDIO support

Allow the MDIO devices to be probed based on the device tree.

Signed-off-by: Madalin Bucur <madalin.bucur@oss.nxp.com>
Signed-off-by: Priyanka Jain <priyanka.jain@nxp.com>
diff --git a/drivers/net/fsl_mdio.c b/drivers/net/fsl_mdio.c
index 894b52e..43040d4 100644
--- a/drivers/net/fsl_mdio.c
+++ b/drivers/net/fsl_mdio.c
@@ -12,6 +12,12 @@
 #include <asm/io.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 __iomem *phyregs, int port_addr,
 		int dev_addr, int regnum, int value)
 {
@@ -56,10 +62,21 @@
 	return value;
 }
 
+#if defined(CONFIG_PHYLIB)
 static int fsl_pq_mdio_reset(struct mii_dev *bus)
 {
-	struct tsec_mii_mng __iomem *regs =
-		(struct tsec_mii_mng __iomem *)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);
@@ -71,11 +88,22 @@
 
 	return 0;
 }
+#endif
 
 int tsec_phy_read(struct mii_dev *bus, int addr, int dev_addr, int regnum)
 {
-	struct tsec_mii_mng __iomem *phyregs =
-		(struct tsec_mii_mng __iomem *)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);
 }
@@ -83,14 +111,25 @@
 int tsec_phy_write(struct mii_dev *bus, int addr, int dev_addr, int regnum,
 			u16 value)
 {
-	struct tsec_mii_mng __iomem *phyregs =
-		(struct tsec_mii_mng __iomem *)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();
@@ -109,3 +148,92 @@
 
 	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 */