bf70cee
From d55a02f460ffd64a5ba7f331489af87edeebf8da Mon Sep 17 00:00:00 2001
bf70cee
From: Giuseppe CAVALLARO <peppe.cavallaro@st.com>
bf70cee
Date: Wed, 16 Mar 2016 10:38:49 +0100
bf70cee
Subject: [PATCH 2/2] stmmac: fix MDIO settings
bf70cee
MIME-Version: 1.0
bf70cee
Content-Type: text/plain; charset=UTF-8
bf70cee
Content-Transfer-Encoding: 8bit
bf70cee
bf70cee
Initially the phy_bus_name was added to manipulate the
bf70cee
driver name but it was recently just used to manage the
bf70cee
fixed-link and then to take some decision at run-time.
bf70cee
So the patch uses the is_pseudo_fixed_link and removes
bf70cee
the phy_bus_name variable not necessary anymore.
bf70cee
bf70cee
The driver can manage the mdio registration by using phy-handle,
bf70cee
dwmac-mdio and own parameter e.g. snps,phy-addr.
bf70cee
This patch takes care about all these possible configurations
bf70cee
and fixes the mdio registration in case of there is a real
bf70cee
transceiver or a switch (that needs to be managed by using
bf70cee
fixed-link).
bf70cee
bf70cee
Signed-off-by: Giuseppe Cavallaro <peppe.cavallaro@st.com>
bf70cee
Reviewed-by: Andreas Färber <afaerber@suse.de>
bf70cee
Tested-by: Frank Schäfer <fschaefer.oss@googlemail.com>
bf70cee
Cc: Gabriel Fernandez <gabriel.fernandez@linaro.org>
bf70cee
Cc: Dinh Nguyen <dinh.linux@gmail.com>
bf70cee
Cc: David S. Miller <davem@davemloft.net>
bf70cee
Cc: Phil Reid <preid@electromag.com.au>
bf70cee
---
bf70cee
 drivers/net/ethernet/stmicro/stmmac/stmmac_main.c  | 11 +--
bf70cee
 drivers/net/ethernet/stmicro/stmmac/stmmac_mdio.c  | 19 +----
bf70cee
 .../net/ethernet/stmicro/stmmac/stmmac_platform.c  | 84 +++++++++++++++++-----
bf70cee
 include/linux/stmmac.h                             |  2 +-
bf70cee
 4 files changed, 71 insertions(+), 45 deletions(-)
bf70cee
bf70cee
diff --git a/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c b/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c
bf70cee
index c21015b..389d7d0 100644
bf70cee
--- a/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c
bf70cee
+++ b/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c
bf70cee
@@ -271,7 +271,6 @@ static void stmmac_eee_ctrl_timer(unsigned long arg)
bf70cee
  */
bf70cee
 bool stmmac_eee_init(struct stmmac_priv *priv)
bf70cee
 {
bf70cee
-	char *phy_bus_name = priv->plat->phy_bus_name;
bf70cee
 	unsigned long flags;
bf70cee
 	bool ret = false;
bf70cee
 
bf70cee
@@ -283,7 +282,7 @@ bool stmmac_eee_init(struct stmmac_priv *priv)
bf70cee
 		goto out;
bf70cee
 
bf70cee
 	/* Never init EEE in case of a switch is attached */
bf70cee
-	if (phy_bus_name && (!strcmp(phy_bus_name, "fixed")))
bf70cee
+	if (priv->phydev->is_pseudo_fixed_link)
bf70cee
 		goto out;
bf70cee
 
bf70cee
 	/* MAC core supports the EEE feature. */
bf70cee
@@ -820,12 +819,8 @@ static int stmmac_init_phy(struct net_device *dev)
bf70cee
 		phydev = of_phy_connect(dev, priv->plat->phy_node,
bf70cee
 					&stmmac_adjust_link, 0, interface);
bf70cee
 	} else {
bf70cee
-		if (priv->plat->phy_bus_name)
bf70cee
-			snprintf(bus_id, MII_BUS_ID_SIZE, "%s-%x",
bf70cee
-				 priv->plat->phy_bus_name, priv->plat->bus_id);
bf70cee
-		else
bf70cee
-			snprintf(bus_id, MII_BUS_ID_SIZE, "stmmac-%x",
bf70cee
-				 priv->plat->bus_id);
bf70cee
+		snprintf(bus_id, MII_BUS_ID_SIZE, "stmmac-%x",
bf70cee
+			 priv->plat->bus_id);
bf70cee
 
bf70cee
 		snprintf(phy_id_fmt, MII_BUS_ID_SIZE + 3, PHY_ID_FMT, bus_id,
bf70cee
 			 priv->plat->phy_addr);
bf70cee
diff --git a/drivers/net/ethernet/stmicro/stmmac/stmmac_mdio.c b/drivers/net/ethernet/stmicro/stmmac/stmmac_mdio.c
bf70cee
index 0faf163..3f5512f 100644
bf70cee
--- a/drivers/net/ethernet/stmicro/stmmac/stmmac_mdio.c
bf70cee
+++ b/drivers/net/ethernet/stmicro/stmmac/stmmac_mdio.c
bf70cee
@@ -198,29 +198,12 @@ int stmmac_mdio_register(struct net_device *ndev)
bf70cee
 	struct mii_bus *new_bus;
bf70cee
 	struct stmmac_priv *priv = netdev_priv(ndev);
bf70cee
 	struct stmmac_mdio_bus_data *mdio_bus_data = priv->plat->mdio_bus_data;
bf70cee
+	struct device_node *mdio_node = priv->plat->mdio_node;
bf70cee
 	int addr, found;
bf70cee
-	struct device_node *mdio_node = NULL;
bf70cee
-	struct device_node *child_node = NULL;
bf70cee
 
bf70cee
 	if (!mdio_bus_data)
bf70cee
 		return 0;
bf70cee
 
bf70cee
-	if (IS_ENABLED(CONFIG_OF)) {
bf70cee
-		for_each_child_of_node(priv->device->of_node, child_node) {
bf70cee
-			if (of_device_is_compatible(child_node,
bf70cee
-						    "snps,dwmac-mdio")) {
bf70cee
-				mdio_node = child_node;
bf70cee
-				break;
bf70cee
-			}
bf70cee
-		}
bf70cee
-
bf70cee
-		if (mdio_node) {
bf70cee
-			netdev_dbg(ndev, "FOUND MDIO subnode\n");
bf70cee
-		} else {
bf70cee
-			netdev_warn(ndev, "No MDIO subnode found\n");
bf70cee
-		}
bf70cee
-	}
bf70cee
-
bf70cee
 	new_bus = mdiobus_alloc();
bf70cee
 	if (new_bus == NULL)
bf70cee
 		return -ENOMEM;
bf70cee
diff --git a/drivers/net/ethernet/stmicro/stmmac/stmmac_platform.c b/drivers/net/ethernet/stmicro/stmmac/stmmac_platform.c
bf70cee
index 6a52fa1..190fb6d 100644
bf70cee
--- a/drivers/net/ethernet/stmicro/stmmac/stmmac_platform.c
bf70cee
+++ b/drivers/net/ethernet/stmicro/stmmac/stmmac_platform.c
bf70cee
@@ -96,6 +96,69 @@ static int dwmac1000_validate_ucast_entries(int ucast_entries)
bf70cee
 }
bf70cee
 
bf70cee
 /**
bf70cee
+ * stmmac_dt_phy - parse device-tree driver parameters to allocate PHY resources
bf70cee
+ * @plat: driver data platform structure
bf70cee
+ * @np: device tree node
bf70cee
+ * @dev: device pointer
bf70cee
+ * Description:
bf70cee
+ * The mdio bus will be allocated in case of a phy transceiver is on board;
bf70cee
+ * it will be NULL if the fixed-link is configured.
bf70cee
+ * If there is the "snps,dwmac-mdio" sub-node the mdio will be allocated
bf70cee
+ * in any case (for DSA, mdio must be registered even if fixed-link).
bf70cee
+ * The table below sums the supported configurations:
bf70cee
+ *	-------------------------------
bf70cee
+ *	snps,phy-addr	|     Y
bf70cee
+ *	-------------------------------
bf70cee
+ *	phy-handle	|     Y
bf70cee
+ *	-------------------------------
bf70cee
+ *	fixed-link	|     N
bf70cee
+ *	-------------------------------
bf70cee
+ *	snps,dwmac-mdio	|
bf70cee
+ *	  even if	|     Y
bf70cee
+ *	fixed-link	|
bf70cee
+ *	-------------------------------
bf70cee
+ *
bf70cee
+ * It returns 0 in case of success otherwise -ENODEV.
bf70cee
+ */
bf70cee
+static int stmmac_dt_phy(struct plat_stmmacenet_data *plat,
bf70cee
+			 struct device_node *np, struct device *dev)
bf70cee
+{
bf70cee
+	bool mdio = true;
bf70cee
+
bf70cee
+	/* If phy-handle property is passed from DT, use it as the PHY */
bf70cee
+	plat->phy_node = of_parse_phandle(np, "phy-handle", 0);
bf70cee
+	if (plat->phy_node)
bf70cee
+		dev_dbg(dev, "Found phy-handle subnode\n");
bf70cee
+
bf70cee
+	/* If phy-handle is not specified, check if we have a fixed-phy */
bf70cee
+	if (!plat->phy_node && of_phy_is_fixed_link(np)) {
bf70cee
+		if ((of_phy_register_fixed_link(np) < 0))
bf70cee
+			return -ENODEV;
bf70cee
+
bf70cee
+		dev_dbg(dev, "Found fixed-link subnode\n");
bf70cee
+		plat->phy_node = of_node_get(np);
bf70cee
+		mdio = false;
bf70cee
+	}
bf70cee
+
bf70cee
+	/* If snps,dwmac-mdio is passed from DT, always register the MDIO */
bf70cee
+	for_each_child_of_node(np, plat->mdio_node) {
bf70cee
+		if (of_device_is_compatible(plat->mdio_node, "snps,dwmac-mdio"))
bf70cee
+			break;
bf70cee
+	}
bf70cee
+
bf70cee
+	if (plat->mdio_node) {
bf70cee
+		dev_dbg(dev, "Found MDIO subnode\n");
bf70cee
+		mdio = true;
bf70cee
+	}
bf70cee
+
bf70cee
+	if (mdio)
bf70cee
+		plat->mdio_bus_data =
bf70cee
+			devm_kzalloc(dev, sizeof(struct stmmac_mdio_bus_data),
bf70cee
+				     GFP_KERNEL);
bf70cee
+	return 0;
bf70cee
+}
bf70cee
+
bf70cee
+/**
bf70cee
  * stmmac_probe_config_dt - parse device-tree driver parameters
bf70cee
  * @pdev: platform_device structure
bf70cee
  * @plat: driver data platform structure
bf70cee
@@ -129,30 +192,15 @@ stmmac_probe_config_dt(struct platform_device *pdev, const char **mac)
bf70cee
 	/* Default to phy auto-detection */
bf70cee
 	plat->phy_addr = -1;
bf70cee
 
bf70cee
-	/* If we find a phy-handle property, use it as the PHY */
bf70cee
-	plat->phy_node = of_parse_phandle(np, "phy-handle", 0);
bf70cee
-
bf70cee
-	/* If phy-handle is not specified, check if we have a fixed-phy */
bf70cee
-	if (!plat->phy_node && of_phy_is_fixed_link(np)) {
bf70cee
-		if ((of_phy_register_fixed_link(np) < 0))
bf70cee
-			return ERR_PTR(-ENODEV);
bf70cee
-
bf70cee
-		plat->phy_node = of_node_get(np);
bf70cee
-	}
bf70cee
-
bf70cee
 	/* "snps,phy-addr" is not a standard property. Mark it as deprecated
bf70cee
 	 * and warn of its use. Remove this when phy node support is added.
bf70cee
 	 */
bf70cee
 	if (of_property_read_u32(np, "snps,phy-addr", &plat->phy_addr) == 0)
bf70cee
 		dev_warn(&pdev->dev, "snps,phy-addr property is deprecated\n");
bf70cee
 
bf70cee
-	if ((plat->phy_node && !of_phy_is_fixed_link(np)) || plat->phy_bus_name)
bf70cee
-		plat->mdio_bus_data = NULL;
bf70cee
-	else
bf70cee
-		plat->mdio_bus_data =
bf70cee
-			devm_kzalloc(&pdev->dev,
bf70cee
-				     sizeof(struct stmmac_mdio_bus_data),
bf70cee
-				     GFP_KERNEL);
bf70cee
+	/* To Configure PHY by using all device-tree supported properties */
bf70cee
+	if (stmmac_dt_phy(plat, np, &pdev->dev))
bf70cee
+		return ERR_PTR(-ENODEV);
bf70cee
 
bf70cee
 	of_property_read_u32(np, "tx-fifo-depth", &plat->tx_fifo_size);
bf70cee
 
bf70cee
diff --git a/include/linux/stmmac.h b/include/linux/stmmac.h
bf70cee
index eead8ab..8b1ff2b 100644
bf70cee
--- a/include/linux/stmmac.h
bf70cee
+++ b/include/linux/stmmac.h
bf70cee
@@ -94,12 +94,12 @@ struct stmmac_dma_cfg {
bf70cee
 };
bf70cee
 
bf70cee
 struct plat_stmmacenet_data {
bf70cee
-	char *phy_bus_name;
bf70cee
 	int bus_id;
bf70cee
 	int phy_addr;
bf70cee
 	int interface;
bf70cee
 	struct stmmac_mdio_bus_data *mdio_bus_data;
bf70cee
 	struct device_node *phy_node;
bf70cee
+	struct device_node *mdio_node;
bf70cee
 	struct stmmac_dma_cfg *dma_cfg;
bf70cee
 	int clk_csr;
bf70cee
 	int has_gmac;
bf70cee
-- 
bf70cee
2.5.0
bf70cee