[U-Boot] [PATCH v4 2/5] net: mdio-uclass: add dm_eth_phy_connect helper function

Alex Marginean alexandru.marginean at nxp.com
Thu Nov 14 15:04:44 UTC 2019


The function connects an ethernet device to a PHY using DT information.
This API is only available for eth devices with an associated device tree
node.

Signed-off-by: Alex Marginean <alexandru.marginean at nxp.com>
---
 include/miiphy.h  | 12 ++++++++
 net/mdio-uclass.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 84 insertions(+)

diff --git a/include/miiphy.h b/include/miiphy.h
index 94bf0da24a..e591d93416 100644
--- a/include/miiphy.h
+++ b/include/miiphy.h
@@ -165,6 +165,18 @@ struct phy_device *dm_mdio_phy_connect(struct udevice *mdiodev, int phyaddr,
 				       struct udevice *ethdev,
 				       phy_interface_t interface);
 
+/**
+ * dm_eth_phy_connect - Connect an Eth device to a PHY based on device tree
+ *
+ * Picks up the DT phy-handle/mdio-handle and phy-mode from ethernet device node
+ * and connects the ethernet device to the linked PHY.
+ *
+ * @ethdev: ethernet device
+ *
+ * @return pointer to phy_device, or 0 on error
+ */
+struct phy_device *dm_eth_phy_connect(struct udevice *ethdev);
+
 #endif
 
 #ifdef CONFIG_DM_MDIO_MUX
diff --git a/net/mdio-uclass.c b/net/mdio-uclass.c
index 7a5f1d6dcc..cf736a3ae5 100644
--- a/net/mdio-uclass.c
+++ b/net/mdio-uclass.c
@@ -116,6 +116,78 @@ struct phy_device *dm_mdio_phy_connect(struct udevice *mdiodev, int phyaddr,
 	return phy_connect(pdata->mii_bus, phyaddr, ethdev, interface);
 }
 
+static struct phy_device *dm_eth_connect_phy_handle(struct udevice *ethdev,
+						    phy_interface_t interface)
+{
+	u32 phy_phandle, phy_addr;
+	struct udevice *mdiodev;
+	struct phy_device *phy;
+	ofnode phy_node;
+
+	if (ofnode_read_u32(ethdev->node, "phy-handle", &phy_phandle)) {
+		dev_dbg(dev, "phy-handle missing in ethernet node\n");
+		return NULL;
+	}
+
+	phy_node = ofnode_get_by_phandle(phy_phandle);
+	if (!ofnode_valid(phy_node)) {
+		dev_dbg(dev, "invalid phy node\n");
+		return NULL;
+	}
+
+	if (ofnode_read_u32(phy_node, "reg", &phy_addr)) {
+		dev_dbg(ethdev, "missing reg property in phy node\n");
+		return NULL;
+	}
+
+	if (uclass_get_device_by_ofnode(UCLASS_MDIO,
+					ofnode_get_parent(phy_node),
+					&mdiodev)) {
+		dev_dbg(dev, "can't find MDIO bus for node %s\n",
+			ofnode_get_name(ofnode_get_parent(phy_node)));
+		return NULL;
+	}
+
+	phy = dm_mdio_phy_connect(mdiodev, phy_addr, ethdev, interface);
+
+	if (phy)
+		phy->node = phy_node;
+
+	return phy;
+}
+
+/* Connect to a PHY linked in eth DT node */
+struct phy_device *dm_eth_phy_connect(struct udevice *ethdev)
+{
+	const char *if_str;
+	phy_interface_t interface;
+	struct phy_device *phy;
+
+	if (!ofnode_valid(ethdev->node)) {
+		debug("%s: supplied eth dev has no DT node!\n", ethdev->name);
+		return NULL;
+	}
+
+	interface = PHY_INTERFACE_MODE_NONE;
+	if_str = ofnode_read_string(ethdev->node, "phy-mode");
+	if (if_str)
+		interface = phy_get_interface_by_name(if_str);
+	if (interface < 0)
+		interface = PHY_INTERFACE_MODE_NONE;
+
+	if (interface == PHY_INTERFACE_MODE_NONE)
+		dev_dbg(ethdev, "can't find interface mode, default to NONE\n");
+
+	phy = dm_eth_connect_phy_handle(ethdev, interface);
+
+	if (!phy)
+		return NULL;
+
+	phy->interface = interface;
+
+	return phy;
+}
+
 UCLASS_DRIVER(mdio) = {
 	.id = UCLASS_MDIO,
 	.name = "mdio",
-- 
2.17.1



More information about the U-Boot mailing list