[PATCH u-boot-net v4 07/14] net: introduce helpers to get PHY interface mode from a device/ofnode

Marek Behún kabel at kernel.org
Thu Apr 7 00:33:01 CEST 2022


From: Marek Behún <marek.behun at nic.cz>

Add helpers ofnode_read_phy_mode() and dev_read_phy_mode() to parse the
"phy-mode" / "phy-connection-type" property. Add corresponding UT test.

Use them treewide.

This allows us to inline the phy_get_interface_by_name() into
ofnode_read_phy_mode(), since the former is not used anymore.

Signed-off-by: Marek Behún <marek.behun at nic.cz>
Reviewed-by: Ramon Fried <rfried.dev at gmail.com>
Tested-by: Patrice Chotard <patrice.chotard at foss.st.com>
---
 arch/sandbox/dts/test.dts                  |  1 +
 board/st/stm32f746-disco/stm32f746-disco.c | 13 +++-----
 drivers/core/ofnode.c                      | 23 ++++++++++++++
 drivers/core/read.c                        |  5 +++
 drivers/net/ag7xxx.c                       |  9 ++----
 drivers/net/altera_tse.c                   | 13 ++------
 drivers/net/bcm6348-eth.c                  |  6 +---
 drivers/net/bcmgenet.c                     | 10 ++----
 drivers/net/designware.c                   | 10 ++----
 drivers/net/dwc_eth_qos.c                  | 36 +++-------------------
 drivers/net/fec_mxc.c                      | 11 ++-----
 drivers/net/fm/eth.c                       | 13 +-------
 drivers/net/fsl_enetc.c                    | 13 +++-----
 drivers/net/ftgmac100.c                    | 11 ++-----
 drivers/net/higmacv300.c                   |  9 ++----
 drivers/net/ldpaa_eth/ldpaa_eth.c          | 28 +++--------------
 drivers/net/macb.c                         | 10 ++----
 drivers/net/mt7620-eth.c                   | 35 +++++++--------------
 drivers/net/mtk_eth.c                      |  8 ++---
 drivers/net/mvgbe.c                        |  7 ++---
 drivers/net/mvneta.c                       | 11 ++-----
 drivers/net/mvpp2.c                        |  9 ++----
 drivers/net/phy/phy.c                      | 25 ++-------------
 drivers/net/pic32_eth.c                    | 11 ++-----
 drivers/net/qe/dm_qe_uec.c                 | 11 ++-----
 drivers/net/ravb.c                         | 15 +++------
 drivers/net/sh_eth.c                       | 15 +++------
 drivers/net/sni_ave.c                      | 12 ++------
 drivers/net/sni_netsec.c                   | 10 ++----
 drivers/net/sun8i_emac.c                   | 11 ++-----
 drivers/net/ti/am65-cpsw-nuss.c            | 15 +++------
 drivers/net/ti/cpsw.c                      | 10 ++----
 drivers/net/ti/keystone_net.c              | 24 +++++++--------
 drivers/net/tsec.c                         |  9 ++----
 drivers/net/xilinx_axi_emac.c              | 10 ++----
 drivers/net/zynq_gem.c                     |  9 ++----
 include/dm/ofnode.h                        | 13 ++++++++
 include/dm/read.h                          | 17 ++++++++++
 include/phy.h                              |  8 -----
 net/mdio-uclass.c                          | 18 +----------
 test/dm/ofnode.c                           |  4 +++
 41 files changed, 166 insertions(+), 372 deletions(-)

diff --git a/arch/sandbox/dts/test.dts b/arch/sandbox/dts/test.dts
index e536943503..5b38ee4a5f 100644
--- a/arch/sandbox/dts/test.dts
+++ b/arch/sandbox/dts/test.dts
@@ -535,6 +535,7 @@
 		reg = <0x10007000 0x1000>;
 		fake-host-hwaddr = [00 00 66 44 22 77];
 		phy-handle = <&ethphy1>;
+		phy-mode = "2500base-x";
 	};
 
 	dsa_eth0: dsa-test-eth {
diff --git a/board/st/stm32f746-disco/stm32f746-disco.c b/board/st/stm32f746-disco/stm32f746-disco.c
index 95d83e73ee..69f657c54b 100644
--- a/board/st/stm32f746-disco/stm32f746-disco.c
+++ b/board/st/stm32f746-disco/stm32f746-disco.c
@@ -117,16 +117,13 @@ int board_late_init(void)
 int board_init(void)
 {
 #ifdef CONFIG_ETH_DESIGNWARE
-	const char *phy_mode;
-	int node;
+	ofnode node;
 
-	node = fdt_node_offset_by_compatible(gd->fdt_blob, 0, "st,stm32-dwmac");
-	if (node < 0)
+	node = ofnode_by_compatible(ofnode_null(), "st,stm32-dwmac");
+	if (!ofnode_valid(node))
 		return -1;
 
-	phy_mode = fdt_getprop(gd->fdt_blob, node, "phy-mode", NULL);
-
-	switch (phy_get_interface_by_name(phy_mode)) {
+	switch (ofnode_read_phy_mode(node)) {
 	case PHY_INTERFACE_MODE_RMII:
 		STM32_SYSCFG->pmc |= SYSCFG_PMC_MII_RMII_SEL;
 		break;
@@ -134,7 +131,7 @@ int board_init(void)
 		STM32_SYSCFG->pmc &= ~SYSCFG_PMC_MII_RMII_SEL;
 		break;
 	default:
-		printf("PHY interface %s not supported !\n", phy_mode);
+		printf("Unsupported PHY interface!\n");
 	}
 #endif
 
diff --git a/drivers/core/ofnode.c b/drivers/core/ofnode.c
index 445b7ad5ad..f644a593d3 100644
--- a/drivers/core/ofnode.c
+++ b/drivers/core/ofnode.c
@@ -1219,3 +1219,26 @@ ofnode ofnode_get_phy_node(ofnode node)
 
 	return args.node;
 }
+
+phy_interface_t ofnode_read_phy_mode(ofnode node)
+{
+	const char *mode;
+	int i;
+
+	assert(ofnode_valid(node));
+
+	mode = ofnode_read_string(node, "phy-mode");
+	if (!mode)
+		mode = ofnode_read_string(node, "phy-connection-type");
+
+	if (!mode)
+		return PHY_INTERFACE_MODE_NONE;
+
+	for (i = 0; i < PHY_INTERFACE_MODE_COUNT; i++)
+		if (!strcmp(mode, phy_interface_strings[i]))
+			return i;
+
+	debug("%s: Invalid PHY interface '%s'\n", __func__, mode);
+
+	return PHY_INTERFACE_MODE_NONE;
+}
diff --git a/drivers/core/read.c b/drivers/core/read.c
index 7ff100218d..c73508d276 100644
--- a/drivers/core/read.c
+++ b/drivers/core/read.c
@@ -403,3 +403,8 @@ ofnode dev_get_phy_node(const struct udevice *dev)
 {
 	return ofnode_get_phy_node(dev_ofnode(dev));
 }
+
+phy_interface_t dev_read_phy_mode(const struct udevice *dev)
+{
+	return ofnode_read_phy_mode(dev_ofnode(dev));
+}
diff --git a/drivers/net/ag7xxx.c b/drivers/net/ag7xxx.c
index 632ab3c1e5..f24a917bd4 100644
--- a/drivers/net/ag7xxx.c
+++ b/drivers/net/ag7xxx.c
@@ -1254,7 +1254,6 @@ static const struct eth_ops ag7xxx_eth_ops = {
 static int ag7xxx_eth_of_to_plat(struct udevice *dev)
 {
 	struct eth_pdata *pdata = dev_get_plat(dev);
-	const char *phy_mode;
 	int ret;
 
 	pdata->iobase = dev_read_addr(dev);
@@ -1265,13 +1264,9 @@ static int ag7xxx_eth_of_to_plat(struct udevice *dev)
 	if (ret <= 0)
 		return ret;
 
-	phy_mode = fdt_getprop(gd->fdt_blob, ret, "phy-mode", NULL);
-	if (phy_mode)
-		pdata->phy_interface = phy_get_interface_by_name(phy_mode);
-	if (pdata->phy_interface == -1) {
-		debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
+	pdata->phy_interface = dev_read_phy_mode(dev);
+	if (pdata->phy_interface == PHY_INTERFACE_MODE_NONE)
 		return -EINVAL;
-	}
 
 	return 0;
 }
diff --git a/drivers/net/altera_tse.c b/drivers/net/altera_tse.c
index eb4cd96763..25247472b0 100644
--- a/drivers/net/altera_tse.c
+++ b/drivers/net/altera_tse.c
@@ -676,17 +676,10 @@ static int altera_tse_probe(struct udevice *dev)
 static int altera_tse_of_to_plat(struct udevice *dev)
 {
 	struct eth_pdata *pdata = dev_get_plat(dev);
-	const char *phy_mode;
-
-	pdata->phy_interface = -1;
-	phy_mode = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "phy-mode",
-			       NULL);
-	if (phy_mode)
-		pdata->phy_interface = phy_get_interface_by_name(phy_mode);
-	if (pdata->phy_interface == -1) {
-		debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
+
+	pdata->phy_interface = dev_read_phy_mode(dev);
+	if (pdata->phy_interface == PHY_INTERFACE_MODE_NONE)
 		return -EINVAL;
-	}
 
 	return 0;
 }
diff --git a/drivers/net/bcm6348-eth.c b/drivers/net/bcm6348-eth.c
index 06e0dd74a5..8f1864a334 100644
--- a/drivers/net/bcm6348-eth.c
+++ b/drivers/net/bcm6348-eth.c
@@ -415,7 +415,6 @@ static int bcm6348_eth_probe(struct udevice *dev)
 	struct eth_pdata *pdata = dev_get_plat(dev);
 	struct bcm6348_eth_priv *priv = dev_get_priv(dev);
 	struct ofnode_phandle_args phy;
-	const char *phy_mode;
 	int ret, i;
 
 	/* get base address */
@@ -425,10 +424,7 @@ static int bcm6348_eth_probe(struct udevice *dev)
 	pdata->iobase = (phys_addr_t) priv->base;
 
 	/* get phy mode */
-	pdata->phy_interface = PHY_INTERFACE_MODE_NONE;
-	phy_mode = dev_read_string(dev, "phy-mode");
-	if (phy_mode)
-		pdata->phy_interface = phy_get_interface_by_name(phy_mode);
+	pdata->phy_interface = dev_read_phy_mode(dev);
 	if (pdata->phy_interface == PHY_INTERFACE_MODE_NONE)
 		return -ENODEV;
 
diff --git a/drivers/net/bcmgenet.c b/drivers/net/bcmgenet.c
index 67839563db..c6acb4932b 100644
--- a/drivers/net/bcmgenet.c
+++ b/drivers/net/bcmgenet.c
@@ -690,20 +690,14 @@ static int bcmgenet_eth_of_to_plat(struct udevice *dev)
 	struct eth_pdata *pdata = dev_get_plat(dev);
 	struct bcmgenet_eth_priv *priv = dev_get_priv(dev);
 	struct ofnode_phandle_args phy_node;
-	const char *phy_mode;
 	int ret;
 
 	pdata->iobase = dev_read_addr(dev);
 
 	/* Get phy mode from DT */
-	pdata->phy_interface = -1;
-	phy_mode = dev_read_string(dev, "phy-mode");
-	if (phy_mode)
-		pdata->phy_interface = phy_get_interface_by_name(phy_mode);
-	if (pdata->phy_interface == -1) {
-		debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
+	pdata->phy_interface = dev_read_phy_mode(dev);
+	if (pdata->phy_interface == PHY_INTERFACE_MODE_NONE)
 		return -EINVAL;
-	}
 
 	ret = dev_read_phandle_with_args(dev, "phy-handle", NULL, 0, 0,
 					 &phy_node);
diff --git a/drivers/net/designware.c b/drivers/net/designware.c
index 5aaac603a0..7b7b0f6578 100644
--- a/drivers/net/designware.c
+++ b/drivers/net/designware.c
@@ -914,21 +914,15 @@ int designware_eth_of_to_plat(struct udevice *dev)
 	struct dw_eth_dev *priv = dev_get_priv(dev);
 #endif
 	struct eth_pdata *pdata = &dw_pdata->eth_pdata;
-	const char *phy_mode;
 #if CONFIG_IS_ENABLED(DM_GPIO)
 	int reset_flags = GPIOD_IS_OUT;
 #endif
 	int ret = 0;
 
 	pdata->iobase = dev_read_addr(dev);
-	pdata->phy_interface = -1;
-	phy_mode = dev_read_string(dev, "phy-mode");
-	if (phy_mode)
-		pdata->phy_interface = phy_get_interface_by_name(phy_mode);
-	if (pdata->phy_interface == -1) {
-		debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
+	pdata->phy_interface = dev_read_phy_mode(dev);
+	if (pdata->phy_interface == PHY_INTERFACE_MODE_NONE)
 		return -EINVAL;
-	}
 
 	pdata->max_speed = dev_read_u32_default(dev, "max-speed", 0);
 
diff --git a/drivers/net/dwc_eth_qos.c b/drivers/net/dwc_eth_qos.c
index 22dad5b203..9777f6cb9c 100644
--- a/drivers/net/dwc_eth_qos.c
+++ b/drivers/net/dwc_eth_qos.c
@@ -270,7 +270,7 @@ struct eqos_config {
 	int config_mac;
 	int config_mac_mdio;
 	unsigned int axi_bus_width;
-	phy_interface_t (*interface)(struct udevice *dev);
+	phy_interface_t (*interface)(const struct udevice *dev);
 	struct eqos_ops *ops;
 };
 
@@ -1729,21 +1729,7 @@ err_probe:
 	return ret;
 }
 
-static phy_interface_t eqos_get_interface_stm32(struct udevice *dev)
-{
-	const char *phy_mode;
-	phy_interface_t interface = PHY_INTERFACE_MODE_NONE;
-
-	debug("%s(dev=%p):\n", __func__, dev);
-
-	phy_mode = dev_read_prop(dev, "phy-mode", NULL);
-	if (phy_mode)
-		interface = phy_get_interface_by_name(phy_mode);
-
-	return interface;
-}
-
-static phy_interface_t eqos_get_interface_tegra186(struct udevice *dev)
+static phy_interface_t eqos_get_interface_tegra186(const struct udevice *dev)
 {
 	return PHY_INTERFACE_MODE_MII;
 }
@@ -1766,20 +1752,6 @@ static int eqos_probe_resources_imx(struct udevice *dev)
 	return 0;
 }
 
-static phy_interface_t eqos_get_interface_imx(struct udevice *dev)
-{
-	const char *phy_mode;
-	phy_interface_t interface = PHY_INTERFACE_MODE_NONE;
-
-	debug("%s(dev=%p):\n", __func__, dev);
-
-	phy_mode = dev_read_prop(dev, "phy-mode", NULL);
-	if (phy_mode)
-		interface = phy_get_interface_by_name(phy_mode);
-
-	return interface;
-}
-
 static int eqos_remove_resources_tegra186(struct udevice *dev)
 {
 	struct eqos_priv *eqos = dev_get_priv(dev);
@@ -1985,7 +1957,7 @@ static const struct eqos_config __maybe_unused eqos_stm32_config = {
 	.config_mac = EQOS_MAC_RXQ_CTRL0_RXQ0EN_ENABLED_AV,
 	.config_mac_mdio = EQOS_MAC_MDIO_ADDRESS_CR_250_300,
 	.axi_bus_width = EQOS_AXI_WIDTH_64,
-	.interface = eqos_get_interface_stm32,
+	.interface = dev_read_phy_mode,
 	.ops = &eqos_stm32_ops
 };
 
@@ -2013,7 +1985,7 @@ struct eqos_config __maybe_unused eqos_imx_config = {
 	.config_mac = EQOS_MAC_RXQ_CTRL0_RXQ0EN_ENABLED_DCB,
 	.config_mac_mdio = EQOS_MAC_MDIO_ADDRESS_CR_250_300,
 	.axi_bus_width = EQOS_AXI_WIDTH_64,
-	.interface = eqos_get_interface_imx,
+	.interface = dev_read_phy_mode,
 	.ops = &eqos_imx_ops
 };
 
diff --git a/drivers/net/fec_mxc.c b/drivers/net/fec_mxc.c
index e8ebef0903..22c2a3a30f 100644
--- a/drivers/net/fec_mxc.c
+++ b/drivers/net/fec_mxc.c
@@ -1310,20 +1310,13 @@ static int fecmxc_of_to_plat(struct udevice *dev)
 	int ret = 0;
 	struct eth_pdata *pdata = dev_get_plat(dev);
 	struct fec_priv *priv = dev_get_priv(dev);
-	const char *phy_mode;
 
 	pdata->iobase = dev_read_addr(dev);
 	priv->eth = (struct ethernet_regs *)pdata->iobase;
 
-	pdata->phy_interface = -1;
-	phy_mode = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "phy-mode",
-			       NULL);
-	if (phy_mode)
-		pdata->phy_interface = phy_get_interface_by_name(phy_mode);
-	if (pdata->phy_interface == -1) {
-		debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
+	pdata->phy_interface = dev_read_phy_mode(dev);
+	if (pdata->phy_interface == PHY_INTERFACE_MODE_NONE)
 		return -EINVAL;
-	}
 
 #ifdef CONFIG_DM_REGULATOR
 	device_get_supply_regulator(dev, "phy-supply", &priv->phy_supply);
diff --git a/drivers/net/fm/eth.c b/drivers/net/fm/eth.c
index 5e0d0bca9b..1ffe9e2b7a 100644
--- a/drivers/net/fm/eth.c
+++ b/drivers/net/fm/eth.c
@@ -954,17 +954,6 @@ int fm_eth_initialize(struct ccsr_fman *reg, struct fm_eth_info *info)
 	return 0;
 }
 #else /* CONFIG_DM_ETH */
-#ifdef CONFIG_PHYLIB
-phy_interface_t fman_read_sys_if(struct udevice *dev)
-{
-	const char *if_str;
-
-	if_str = ofnode_read_string(dev_ofnode(dev), "phy-connection-type");
-	debug("MAC system interface mode %s\n", if_str);
-
-	return phy_get_interface_by_name(if_str);
-}
-#endif
 
 static int fm_eth_bind(struct udevice *dev)
 {
@@ -1038,7 +1027,7 @@ static int fm_eth_probe(struct udevice *dev)
 	reg = (void *)(uintptr_t)dev_read_addr(dev);
 	fm_eth->mac_type = dev_get_driver_data(dev);
 #ifdef CONFIG_PHYLIB
-	fm_eth->enet_if = fman_read_sys_if(dev);
+	fm_eth->enet_if = dev_read_phy_mode(dev);
 #else
 	fm_eth->enet_if = PHY_INTERFACE_MODE_SGMII;
 	printf("%s: warning - unable to determine interface type\n", __func__);
diff --git a/drivers/net/fsl_enetc.c b/drivers/net/fsl_enetc.c
index 915c7c8025..8f5af1dbc0 100644
--- a/drivers/net/fsl_enetc.c
+++ b/drivers/net/fsl_enetc.c
@@ -260,9 +260,6 @@ static int enetc_init_sxgmii(struct udevice *dev)
 static void enetc_start_pcs(struct udevice *dev)
 {
 	struct enetc_priv *priv = dev_get_priv(dev);
-	const char *if_str;
-
-	priv->if_type = PHY_INTERFACE_MODE_NONE;
 
 	/* register internal MDIO for debug purposes */
 	if (enetc_read_port(priv, ENETC_PCAPR0) & ENETC_PCAPRO_MDIO) {
@@ -279,14 +276,12 @@ static void enetc_start_pcs(struct udevice *dev)
 		return;
 	}
 
-	if_str = ofnode_read_string(dev_ofnode(dev), "phy-mode");
-	if (if_str)
-		priv->if_type = phy_get_interface_by_name(if_str);
-	else
+	priv->if_type = dev_read_phy_mode(dev);
+	if (priv->if_type == PHY_INTERFACE_MODE_NONE) {
 		enetc_dbg(dev,
 			  "phy-mode property not found, defaulting to SGMII\n");
-	if (priv->if_type < 0)
-		priv->if_type = PHY_INTERFACE_MODE_NONE;
+		priv->if_type = PHY_INTERFACE_MODE_SGMII;
+	}
 
 	switch (priv->if_type) {
 	case PHY_INTERFACE_MODE_SGMII:
diff --git a/drivers/net/ftgmac100.c b/drivers/net/ftgmac100.c
index aa719d295f..626c27d7bf 100644
--- a/drivers/net/ftgmac100.c
+++ b/drivers/net/ftgmac100.c
@@ -549,17 +549,12 @@ static int ftgmac100_of_to_plat(struct udevice *dev)
 {
 	struct eth_pdata *pdata = dev_get_plat(dev);
 	struct ftgmac100_data *priv = dev_get_priv(dev);
-	const char *phy_mode;
 
 	pdata->iobase = dev_read_addr(dev);
-	pdata->phy_interface = -1;
-	phy_mode = dev_read_string(dev, "phy-mode");
-	if (phy_mode)
-		pdata->phy_interface = phy_get_interface_by_name(phy_mode);
-	if (pdata->phy_interface == -1) {
-		dev_err(dev, "Invalid PHY interface '%s'\n", phy_mode);
+
+	pdata->phy_interface = dev_read_phy_mode(dev);
+	if (pdata->phy_interface == PHY_INTERFACE_MODE_NONE)
 		return -EINVAL;
-	}
 
 	pdata->max_speed = dev_read_u32_default(dev, "max-speed", 0);
 
diff --git a/drivers/net/higmacv300.c b/drivers/net/higmacv300.c
index aa79d6eda8..ce8f2dfd09 100644
--- a/drivers/net/higmacv300.c
+++ b/drivers/net/higmacv300.c
@@ -561,19 +561,14 @@ static int higmac_remove(struct udevice *dev)
 static int higmac_of_to_plat(struct udevice *dev)
 {
 	struct higmac_priv *priv = dev_get_priv(dev);
-	int phyintf = PHY_INTERFACE_MODE_NONE;
-	const char *phy_mode;
 	ofnode phy_node;
 
 	priv->base = dev_remap_addr_index(dev, 0);
 	priv->macif_ctrl = dev_remap_addr_index(dev, 1);
 
-	phy_mode = dev_read_string(dev, "phy-mode");
-	if (phy_mode)
-		phyintf = phy_get_interface_by_name(phy_mode);
-	if (phyintf == PHY_INTERFACE_MODE_NONE)
+	priv->phyintf = dev_read_phy_mode(dev);
+	if (priv->phyintf == PHY_INTERFACE_MODE_NONE)
 		return -ENODEV;
-	priv->phyintf = phyintf;
 
 	phy_node = dev_read_subnode(dev, "phy");
 	if (!ofnode_valid(phy_node)) {
diff --git a/drivers/net/ldpaa_eth/ldpaa_eth.c b/drivers/net/ldpaa_eth/ldpaa_eth.c
index 725173f627..c775598b91 100644
--- a/drivers/net/ldpaa_eth/ldpaa_eth.c
+++ b/drivers/net/ldpaa_eth/ldpaa_eth.c
@@ -1120,31 +1120,14 @@ static uint32_t ldpaa_eth_get_dpmac_id(struct udevice *dev)
 	return fdtdec_get_uint(gd->fdt_blob, port_node, "reg", -1);
 }
 
-static const char *ldpaa_eth_get_phy_mode_str(struct udevice *dev)
-{
-	int port_node = dev_of_offset(dev);
-	const char *phy_mode_str;
-
-	phy_mode_str = fdt_getprop(gd->fdt_blob, port_node,
-				   "phy-connection-type", NULL);
-	if (phy_mode_str)
-		return phy_mode_str;
-
-	phy_mode_str = fdt_getprop(gd->fdt_blob, port_node, "phy-mode", NULL);
-	return phy_mode_str;
-}
-
 static int ldpaa_eth_bind(struct udevice *dev)
 {
-	const char *phy_mode_str = NULL;
 	uint32_t dpmac_id;
 	char eth_name[16];
 	int phy_mode = -1;
 
-	phy_mode_str = ldpaa_eth_get_phy_mode_str(dev);
-	if (phy_mode_str)
-		phy_mode = phy_get_interface_by_name(phy_mode_str);
-	if (phy_mode == -1) {
+	phy_mode = dev_read_phy_mode(dev);
+	if (phy_mode == PHY_INTERFACE_MODE_NONE) {
 		dev_err(dev, "incorrect phy mode\n");
 		return -EINVAL;
 	}
@@ -1155,7 +1138,8 @@ static int ldpaa_eth_bind(struct udevice *dev)
 		return -EINVAL;
 	}
 
-	sprintf(eth_name, "DPMAC%d@%s", dpmac_id, phy_mode_str);
+	sprintf(eth_name, "DPMAC%d@%s", dpmac_id,
+		phy_string_for_interface(phy_mode));
 	device_set_name(dev, eth_name);
 
 	return 0;
@@ -1164,11 +1148,9 @@ static int ldpaa_eth_bind(struct udevice *dev)
 static int ldpaa_eth_of_to_plat(struct udevice *dev)
 {
 	struct ldpaa_eth_priv *priv = dev_get_priv(dev);
-	const char *phy_mode_str;
 
 	priv->dpmac_id = ldpaa_eth_get_dpmac_id(dev);
-	phy_mode_str = ldpaa_eth_get_phy_mode_str(dev);
-	priv->phy_mode = phy_get_interface_by_name(phy_mode_str);
+	priv->phy_mode = dev_read_phy_mode(dev);
 
 	return 0;
 }
diff --git a/drivers/net/macb.c b/drivers/net/macb.c
index 37eed59a69..317b380e8f 100644
--- a/drivers/net/macb.c
+++ b/drivers/net/macb.c
@@ -1360,17 +1360,11 @@ static int macb_eth_probe(struct udevice *dev)
 	struct eth_pdata *pdata = dev_get_plat(dev);
 	struct macb_device *macb = dev_get_priv(dev);
 	struct ofnode_phandle_args phandle_args;
-	const char *phy_mode;
 	int ret;
 
-	phy_mode = dev_read_prop(dev, "phy-mode", NULL);
-
-	if (phy_mode)
-		macb->phy_interface = phy_get_interface_by_name(phy_mode);
-	if (macb->phy_interface == -1) {
-		debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
+	macb->phy_interface = dev_read_phy_mode(dev);
+	if (macb->phy_interface == PHY_INTERFACE_MODE_NONE)
 		return -EINVAL;
-	}
 
 	/* Read phyaddr from DT */
 	if (!dev_read_phandle_with_args(dev, "phy-handle", NULL, 0, 0,
diff --git a/drivers/net/mt7620-eth.c b/drivers/net/mt7620-eth.c
index 222250d52a..24fcb9373f 100644
--- a/drivers/net/mt7620-eth.c
+++ b/drivers/net/mt7620-eth.c
@@ -1048,33 +1048,20 @@ static int mt7620_eth_parse_gsw_port(struct mt7620_eth_priv *priv, u32 idx,
 				     ofnode node)
 {
 	ofnode subnode;
-	const char *str;
-	int mode, speed, ret;
+	int speed, ret;
 	u32 phy_addr;
 
-	str = ofnode_read_string(node, "phy-mode");
-	if (str) {
-		mode = phy_get_interface_by_name(str);
-		if (mode < 0) {
-			dev_err(priv->dev, "mt7620_eth: invalid phy-mode\n");
-			return -EINVAL;
-		}
-
-		switch (mode) {
-		case PHY_INTERFACE_MODE_MII:
-		case PHY_INTERFACE_MODE_RMII:
-		case PHY_INTERFACE_MODE_RGMII:
-		case PHY_INTERFACE_MODE_NONE:
-			break;
-		default:
-			dev_err(priv->dev,
-				"mt7620_eth: unsupported phy-mode\n");
-			return -ENOTSUPP;
-		}
+	priv->port_cfg[idx].mode = ofnode_read_phy_mode(node);
 
-		priv->port_cfg[idx].mode = mode;
-	} else {
-		priv->port_cfg[idx].mode = PHY_INTERFACE_MODE_NONE;
+	switch (priv->port_cfg[idx].mode) {
+	case PHY_INTERFACE_MODE_MII:
+	case PHY_INTERFACE_MODE_RMII:
+	case PHY_INTERFACE_MODE_RGMII:
+	case PHY_INTERFACE_MODE_NONE:
+		break;
+	default:
+		dev_err(priv->dev, "mt7620_eth: unsupported phy-mode\n");
+		return -ENOTSUPP;
 	}
 
 	subnode = ofnode_find_subnode(node, "fixed-link");
diff --git a/drivers/net/mtk_eth.c b/drivers/net/mtk_eth.c
index 26f02847a2..d6065db5fb 100644
--- a/drivers/net/mtk_eth.c
+++ b/drivers/net/mtk_eth.c
@@ -1447,11 +1447,9 @@ static int mtk_eth_of_to_plat(struct udevice *dev)
 	priv->gmac_id = dev_read_u32_default(dev, "mediatek,gmac-id", 0);
 
 	/* Interface mode is required */
-	str = dev_read_string(dev, "phy-mode");
-	if (str) {
-		pdata->phy_interface = phy_get_interface_by_name(str);
-		priv->phy_interface = pdata->phy_interface;
-	} else {
+	pdata->phy_interface = dev_read_phy_mode(dev);
+	priv->phy_interface = pdata->phy_interface;
+	if (pdata->phy_interface == PHY_INTERFACE_MODE_NONE) {
 		printf("error: phy-mode is not set\n");
 		return -EINVAL;
 	}
diff --git a/drivers/net/mvgbe.c b/drivers/net/mvgbe.c
index 954bf86121..32ec0b437d 100644
--- a/drivers/net/mvgbe.c
+++ b/drivers/net/mvgbe.c
@@ -993,7 +993,6 @@ static int mvgbe_of_to_plat(struct udevice *dev)
 	struct mvgbe_device *dmvgbe = dev_get_priv(dev);
 	void *blob = (void *)gd->fdt_blob;
 	int node = dev_of_offset(dev);
-	const char *phy_mode;
 	int fl_node;
 	int pnode;
 	unsigned long addr;
@@ -1005,10 +1004,8 @@ static int mvgbe_of_to_plat(struct udevice *dev)
 					      "marvell,kirkwood-eth-port");
 
 	/* Get phy-mode / phy_interface from DT */
-	phy_mode = fdt_getprop(gd->fdt_blob, pnode, "phy-mode", NULL);
-	if (phy_mode)
-		pdata->phy_interface = phy_get_interface_by_name(phy_mode);
-	else
+	pdata->phy_interface = dev_read_phy_mode(dev);
+	if (pdata->phy_interface == PHY_INTERFACE_MODE_NONE)
 		pdata->phy_interface = PHY_INTERFACE_MODE_GMII;
 
 	dmvgbe->phy_interface = pdata->phy_interface;
diff --git a/drivers/net/mvneta.c b/drivers/net/mvneta.c
index 4a4268c2b2..d31b96a9d8 100644
--- a/drivers/net/mvneta.c
+++ b/drivers/net/mvneta.c
@@ -1799,20 +1799,13 @@ static const struct eth_ops mvneta_ops = {
 static int mvneta_of_to_plat(struct udevice *dev)
 {
 	struct eth_pdata *pdata = dev_get_plat(dev);
-	const char *phy_mode;
 
 	pdata->iobase = dev_read_addr(dev);
 
 	/* Get phy-mode / phy_interface from DT */
-	pdata->phy_interface = -1;
-	phy_mode = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "phy-mode",
-			       NULL);
-	if (phy_mode)
-		pdata->phy_interface = phy_get_interface_by_name(phy_mode);
-	if (pdata->phy_interface == -1) {
-		debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
+	pdata->phy_interface = dev_read_phy_mode(dev);
+	if (pdata->phy_interface == PHY_INTERFACE_MODE_NONE)
 		return -EINVAL;
-	}
 
 	return 0;
 }
diff --git a/drivers/net/mvpp2.c b/drivers/net/mvpp2.c
index 4c0a7b0a9f..dfddac180f 100644
--- a/drivers/net/mvpp2.c
+++ b/drivers/net/mvpp2.c
@@ -4786,11 +4786,9 @@ static int mvpp2_port_init(struct udevice *dev, struct mvpp2_port *port)
 static int phy_info_parse(struct udevice *dev, struct mvpp2_port *port)
 {
 	int port_node = dev_of_offset(dev);
-	const char *phy_mode_str;
 	int phy_node;
 	u32 id;
 	u32 phyaddr = 0;
-	int phy_mode = -1;
 	int fixed_link = 0;
 	int ret;
 
@@ -4821,10 +4819,8 @@ static int phy_info_parse(struct udevice *dev, struct mvpp2_port *port)
 		phyaddr = PHY_MAX_ADDR;
 	}
 
-	phy_mode_str = fdt_getprop(gd->fdt_blob, port_node, "phy-mode", NULL);
-	if (phy_mode_str)
-		phy_mode = phy_get_interface_by_name(phy_mode_str);
-	if (phy_mode == -1) {
+	port->phy_interface = dev_read_phy_mode(dev);
+	if (port->phy_interface == PHY_INTERFACE_MODE_NONE) {
 		dev_err(dev, "incorrect phy mode\n");
 		return -EINVAL;
 	}
@@ -4847,7 +4843,6 @@ static int phy_info_parse(struct udevice *dev, struct mvpp2_port *port)
 		port->first_rxq = port->id * rxq_number;
 	else
 		port->first_rxq = port->id * port->priv->max_port_rxqs;
-	port->phy_interface = phy_mode;
 	port->phyaddr = phyaddr;
 
 	return 0;
diff --git a/drivers/net/phy/phy.c b/drivers/net/phy/phy.c
index c8289d3889..ba7b361c3d 100644
--- a/drivers/net/phy/phy.c
+++ b/drivers/net/phy/phy.c
@@ -982,25 +982,16 @@ static struct phy_device *phy_connect_gmii2rgmii(struct mii_dev *bus,
  */
 struct phy_device *fixed_phy_create(ofnode node)
 {
-	phy_interface_t interface = PHY_INTERFACE_MODE_NONE;
 	struct phy_device *phydev;
-	const char *if_str;
 	ofnode subnode;
 
-	if_str = ofnode_read_string(node, "phy-mode");
-	if (!if_str) {
-		if_str = ofnode_read_string(node, "phy-connection-type");
-	}
-	if (if_str) {
-		interface = phy_get_interface_by_name(if_str);
-	}
-
 	subnode = ofnode_find_subnode(node, "fixed-link");
 	if (!ofnode_valid(subnode)) {
 		return NULL;
 	}
 
-	phydev = phy_device_create(NULL, 0, PHY_FIXED_ID, false, interface);
+	phydev = phy_device_create(NULL, 0, PHY_FIXED_ID, false,
+				   ofnode_read_phy_mode(node));
 	if (phydev)
 		phydev->node = subnode;
 
@@ -1098,15 +1089,3 @@ int phy_shutdown(struct phy_device *phydev)
 
 	return 0;
 }
-
-int phy_get_interface_by_name(const char *str)
-{
-	int i;
-
-	for (i = 0; i < PHY_INTERFACE_MODE_COUNT; i++) {
-		if (!strcmp(str, phy_interface_strings[i]))
-			return i;
-	}
-
-	return -1;
-}
diff --git a/drivers/net/pic32_eth.c b/drivers/net/pic32_eth.c
index 5a678d1cf9..03eb51e51d 100644
--- a/drivers/net/pic32_eth.c
+++ b/drivers/net/pic32_eth.c
@@ -534,7 +534,6 @@ static int pic32_eth_probe(struct udevice *dev)
 {
 	struct eth_pdata *pdata = dev_get_plat(dev);
 	struct pic32eth_dev *priv = dev_get_priv(dev);
-	const char *phy_mode;
 	void __iomem *iobase;
 	fdt_addr_t addr;
 	fdt_size_t size;
@@ -550,15 +549,9 @@ static int pic32_eth_probe(struct udevice *dev)
 	pdata->iobase = (phys_addr_t)addr;
 
 	/* get phy mode */
-	pdata->phy_interface = -1;
-	phy_mode = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "phy-mode",
-			       NULL);
-	if (phy_mode)
-		pdata->phy_interface = phy_get_interface_by_name(phy_mode);
-	if (pdata->phy_interface == -1) {
-		debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
+	pdata->phy_interface = dev_read_phy_mode(dev);
+	if (pdata->phy_interface == PHY_INTERFACE_MODE_NONE)
 		return -EINVAL;
-	}
 
 	/* get phy addr */
 	offset = fdtdec_lookup_phandle(gd->fdt_blob, dev_of_offset(dev),
diff --git a/drivers/net/qe/dm_qe_uec.c b/drivers/net/qe/dm_qe_uec.c
index a12c8cd2ac..5a66d726cd 100644
--- a/drivers/net/qe/dm_qe_uec.c
+++ b/drivers/net/qe/dm_qe_uec.c
@@ -1133,19 +1133,12 @@ static int qe_uec_remove(struct udevice *dev)
 static int qe_uec_of_to_plat(struct udevice *dev)
 {
 	struct eth_pdata *pdata = dev_get_plat(dev);
-	const char *phy_mode;
 
 	pdata->iobase = (phys_addr_t)devfdt_get_addr(dev);
 
-	pdata->phy_interface = -1;
-	phy_mode = fdt_getprop(gd->fdt_blob, dev_of_offset(dev),
-			       "phy-connection-type", NULL);
-	if (phy_mode)
-		pdata->phy_interface = phy_get_interface_by_name(phy_mode);
-	if (pdata->phy_interface == -1) {
-		debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
+	pdata->phy_interface = dev_read_phy_mode(dev);
+	if (pdata->phy_interface == PHY_INTERFACE_MODE_NONE)
 		return -EINVAL;
-	}
 
 	return 0;
 }
diff --git a/drivers/net/ravb.c b/drivers/net/ravb.c
index 4078d33bb5..f6d386bd6b 100644
--- a/drivers/net/ravb.c
+++ b/drivers/net/ravb.c
@@ -674,20 +674,13 @@ static const struct eth_ops ravb_ops = {
 int ravb_of_to_plat(struct udevice *dev)
 {
 	struct eth_pdata *pdata = dev_get_plat(dev);
-	const char *phy_mode;
 	const fdt32_t *cell;
-	int ret = 0;
 
 	pdata->iobase = dev_read_addr(dev);
-	pdata->phy_interface = -1;
-	phy_mode = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "phy-mode",
-			       NULL);
-	if (phy_mode)
-		pdata->phy_interface = phy_get_interface_by_name(phy_mode);
-	if (pdata->phy_interface == -1) {
-		debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
+
+	pdata->phy_interface = dev_read_phy_mode(dev);
+	if (pdata->phy_interface == PHY_INTERFACE_MODE_NONE)
 		return -EINVAL;
-	}
 
 	pdata->max_speed = 1000;
 	cell = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "max-speed", NULL);
@@ -696,7 +689,7 @@ int ravb_of_to_plat(struct udevice *dev)
 
 	sprintf(bb_miiphy_buses[0].name, dev->name);
 
-	return ret;
+	return 0;
 }
 
 static const struct udevice_id ravb_ids[] = {
diff --git a/drivers/net/sh_eth.c b/drivers/net/sh_eth.c
index 4055f07b2f..04c9c2d968 100644
--- a/drivers/net/sh_eth.c
+++ b/drivers/net/sh_eth.c
@@ -915,20 +915,13 @@ static const struct eth_ops sh_ether_ops = {
 int sh_ether_of_to_plat(struct udevice *dev)
 {
 	struct eth_pdata *pdata = dev_get_plat(dev);
-	const char *phy_mode;
 	const fdt32_t *cell;
-	int ret = 0;
 
 	pdata->iobase = dev_read_addr(dev);
-	pdata->phy_interface = -1;
-	phy_mode = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "phy-mode",
-			       NULL);
-	if (phy_mode)
-		pdata->phy_interface = phy_get_interface_by_name(phy_mode);
-	if (pdata->phy_interface == -1) {
-		debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
+
+	pdata->phy_interface = dev_read_phy_mode(dev);
+	if (pdata->phy_interface == PHY_INTERFACE_MODE_NONE)
 		return -EINVAL;
-	}
 
 	pdata->max_speed = 1000;
 	cell = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "max-speed", NULL);
@@ -937,7 +930,7 @@ int sh_ether_of_to_plat(struct udevice *dev)
 
 	sprintf(bb_miiphy_buses[0].name, dev->name);
 
-	return ret;
+	return 0;
 }
 
 static const struct udevice_id sh_ether_ids[] = {
diff --git a/drivers/net/sni_ave.c b/drivers/net/sni_ave.c
index ab51552ed8..0a368c6d03 100644
--- a/drivers/net/sni_ave.c
+++ b/drivers/net/sni_ave.c
@@ -738,7 +738,6 @@ static int ave_of_to_plat(struct udevice *dev)
 	struct eth_pdata *pdata = dev_get_plat(dev);
 	struct ave_private *priv = dev_get_priv(dev);
 	struct ofnode_phandle_args args;
-	const char *phy_mode;
 	const u32 *valp;
 	int ret, nc, nr;
 	const char *name;
@@ -748,15 +747,10 @@ static int ave_of_to_plat(struct udevice *dev)
 		return -EINVAL;
 
 	pdata->iobase = dev_read_addr(dev);
-	pdata->phy_interface = -1;
-	phy_mode = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "phy-mode",
-			       NULL);
-	if (phy_mode)
-		pdata->phy_interface = phy_get_interface_by_name(phy_mode);
-	if (pdata->phy_interface == -1) {
-		dev_err(dev, "Invalid PHY interface '%s'\n", phy_mode);
+
+	pdata->phy_interface = dev_read_phy_mode(dev);
+	if (pdata->phy_interface == PHY_INTERFACE_MODE_NONE)
 		return -EINVAL;
-	}
 
 	pdata->max_speed = 0;
 	valp = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "max-speed",
diff --git a/drivers/net/sni_netsec.c b/drivers/net/sni_netsec.c
index 4901321d0c..693fd3a35d 100644
--- a/drivers/net/sni_netsec.c
+++ b/drivers/net/sni_netsec.c
@@ -1029,19 +1029,13 @@ static int netsec_of_to_plat(struct udevice *dev)
 	struct eth_pdata *pdata = dev_get_plat(dev);
 	struct netsec_priv *priv = dev_get_priv(dev);
 	struct ofnode_phandle_args phandle_args;
-	const char *phy_mode;
 
 	pdata->iobase = dev_read_addr_index(dev, 0);
 	priv->eeprom_base = dev_read_addr_index(dev, 1) - EERPROM_MAP_OFFSET;
 
-	pdata->phy_interface = -1;
-	phy_mode = dev_read_prop(dev, "phy-mode", NULL);
-	if (phy_mode)
-		pdata->phy_interface = phy_get_interface_by_name(phy_mode);
-	if (pdata->phy_interface == -1) {
-		pr_err("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
+	pdata->phy_interface = dev_read_phy_mode(dev);
+	if (pdata->phy_interface == PHY_INTERFACE_MODE_NONE)
 		return -EINVAL;
-	}
 
 	if (!dev_read_phandle_with_args(dev, "phy-handle", NULL, 0, 0,
 					&phandle_args))
diff --git a/drivers/net/sun8i_emac.c b/drivers/net/sun8i_emac.c
index 2e24d12214..5654a3430e 100644
--- a/drivers/net/sun8i_emac.c
+++ b/drivers/net/sun8i_emac.c
@@ -882,7 +882,6 @@ static int sun8i_emac_eth_of_to_plat(struct udevice *dev)
 	struct sun8i_eth_pdata *sun8i_pdata = dev_get_plat(dev);
 	struct eth_pdata *pdata = &sun8i_pdata->eth_pdata;
 	struct emac_eth_dev *priv = dev_get_priv(dev);
-	const char *phy_mode;
 	const fdt32_t *reg;
 	int node = dev_of_offset(dev);
 	int offset = 0;
@@ -946,16 +945,10 @@ static int sun8i_emac_eth_of_to_plat(struct udevice *dev)
 	}
 	priv->phyaddr = fdtdec_get_int(gd->fdt_blob, offset, "reg", -1);
 
-	phy_mode = fdt_getprop(gd->fdt_blob, node, "phy-mode", NULL);
-
-	if (phy_mode)
-		pdata->phy_interface = phy_get_interface_by_name(phy_mode);
+	pdata->phy_interface = dev_read_phy_mode(dev);
 	printf("phy interface%d\n", pdata->phy_interface);
-
-	if (pdata->phy_interface == -1) {
-		debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
+	if (pdata->phy_interface == PHY_INTERFACE_MODE_NONE)
 		return -EINVAL;
-	}
 
 	if (priv->variant == H3_EMAC) {
 		ret = sun8i_handle_internal_phy(dev, priv);
diff --git a/drivers/net/ti/am65-cpsw-nuss.c b/drivers/net/ti/am65-cpsw-nuss.c
index 87f51b3b99..c1da334739 100644
--- a/drivers/net/ti/am65-cpsw-nuss.c
+++ b/drivers/net/ti/am65-cpsw-nuss.c
@@ -602,21 +602,14 @@ static int am65_cpsw_ofdata_parse_phy(struct udevice *dev)
 	struct eth_pdata *pdata = dev_get_plat(dev);
 	struct am65_cpsw_priv *priv = dev_get_priv(dev);
 	struct ofnode_phandle_args out_args;
-	const char *phy_mode;
 	int ret = 0;
 
 	dev_read_u32(dev, "reg", &priv->port_id);
 
-	phy_mode = dev_read_string(dev, "phy-mode");
-	if (phy_mode) {
-		pdata->phy_interface =
-				phy_get_interface_by_name(phy_mode);
-		if (pdata->phy_interface == -1) {
-			dev_err(dev, "Invalid PHY mode '%s', port %u\n",
-				phy_mode, priv->port_id);
-			ret = -EINVAL;
-			goto out;
-		}
+	pdata->phy_interface = dev_read_phy_mode(dev);
+	if (pdata->phy_interface == PHY_INTERFACE_MODE_NONE) {
+		dev_err(dev, "Invalid PHY mode, port %u\n", priv->port_id);
+		return -EINVAL;
 	}
 
 	dev_read_u32(dev, "max-speed", (u32 *)&pdata->max_speed);
diff --git a/drivers/net/ti/cpsw.c b/drivers/net/ti/cpsw.c
index 68f4191fe9..5b7bab734e 100644
--- a/drivers/net/ti/cpsw.c
+++ b/drivers/net/ti/cpsw.c
@@ -1194,15 +1194,12 @@ static void cpsw_eth_of_parse_slave(struct cpsw_platform_data *data,
 {
 	struct ofnode_phandle_args out_args;
 	struct cpsw_slave_data *slave_data;
-	const char *phy_mode;
 	u32 phy_id[2];
 	int ret;
 
 	slave_data = &data->slave_data[slave_index];
 
-	phy_mode = ofnode_read_string(subnode, "phy-mode");
-	if (phy_mode)
-		slave_data->phy_if = phy_get_interface_by_name(phy_mode);
+	slave_data->phy_if = ofnode_read_phy_mode(subnode);
 
 	ret = ofnode_parse_phandle_with_args(subnode, "phy-handle",
 					     NULL, 0, 0, &out_args);
@@ -1348,11 +1345,8 @@ static int cpsw_eth_of_to_plat(struct udevice *dev)
 	}
 
 	pdata->phy_interface = data->slave_data[data->active_slave].phy_if;
-	if (pdata->phy_interface == -1) {
-		debug("%s: Invalid PHY interface '%s'\n", __func__,
-		      phy_string_for_interface(pdata->phy_interface));
+	if (pdata->phy_interface == PHY_INTERFACE_MODE_NONE)
 		return -EINVAL;
-	}
 
 	return 0;
 }
diff --git a/drivers/net/ti/keystone_net.c b/drivers/net/ti/keystone_net.c
index 5e8f683c29..b55e7da4c1 100644
--- a/drivers/net/ti/keystone_net.c
+++ b/drivers/net/ti/keystone_net.c
@@ -687,7 +687,6 @@ static int ks2_eth_parse_slave_interface(int netcp, int slave,
 	int phy;
 	int dma_count;
 	u32 dma_channel[8];
-	const char *phy_mode;
 
 	priv->slave_port = fdtdec_get_int(fdt, slave, "slave-port", -1);
 	priv->net_rx_buffs.rx_flow = priv->slave_port * 8;
@@ -728,20 +727,19 @@ static int ks2_eth_parse_slave_interface(int netcp, int slave,
 		priv->sgmii_link_type = SGMII_LINK_MAC_PHY;
 		priv->has_mdio = true;
 	} else if (priv->link_type == LINK_TYPE_RGMII_LINK_MAC_PHY) {
-		phy_mode = fdt_getprop(fdt, slave, "phy-mode", NULL);
-		if (phy_mode) {
-			priv->phy_if = phy_get_interface_by_name(phy_mode);
-			if (priv->phy_if != PHY_INTERFACE_MODE_RGMII &&
-			    priv->phy_if != PHY_INTERFACE_MODE_RGMII_ID &&
-			    priv->phy_if != PHY_INTERFACE_MODE_RGMII_RXID &&
-			    priv->phy_if != PHY_INTERFACE_MODE_RGMII_TXID) {
-				pr_err("invalid phy-mode\n");
-				return -EINVAL;
-			}
-		} else {
+		priv->phy_if = ofnode_read_phy_mode(offset_to_ofnode(slave));
+		if (priv->phy_if == PHY_INTERFACE_MODE_NONE)
 			priv->phy_if = PHY_INTERFACE_MODE_RGMII;
-		}
 		pdata->phy_interface = priv->phy_if;
+
+		if (priv->phy_if != PHY_INTERFACE_MODE_RGMII &&
+		    priv->phy_if != PHY_INTERFACE_MODE_RGMII_ID &&
+		    priv->phy_if != PHY_INTERFACE_MODE_RGMII_RXID &&
+		    priv->phy_if != PHY_INTERFACE_MODE_RGMII_TXID) {
+			pr_err("invalid phy-mode\n");
+			return -EINVAL;
+		}
+
 		priv->has_mdio = true;
 	}
 
diff --git a/drivers/net/tsec.c b/drivers/net/tsec.c
index beca886b25..fec051ebb7 100644
--- a/drivers/net/tsec.c
+++ b/drivers/net/tsec.c
@@ -834,7 +834,6 @@ int tsec_probe(struct udevice *dev)
 	struct ofnode_phandle_args phandle_args;
 	u32 tbiaddr = CONFIG_SYS_TBIPA_VALUE;
 	struct tsec_data *data;
-	const char *phy_mode;
 	ofnode parent, child;
 	fdt_addr_t reg;
 	u32 max_speed;
@@ -894,12 +893,8 @@ int tsec_probe(struct udevice *dev)
 
 	priv->tbiaddr = tbiaddr;
 
-	phy_mode = dev_read_prop(dev, "phy-connection-type", NULL);
-	if (!phy_mode)
-		phy_mode = dev_read_prop(dev, "phy-mode", NULL);
-	if (phy_mode)
-		pdata->phy_interface = phy_get_interface_by_name(phy_mode);
-	if (pdata->phy_interface == -1)
+	pdata->phy_interface = dev_read_phy_mode(dev);
+	if (pdata->phy_interface == PHY_INTERFACE_MODE_NONE)
 		pdata->phy_interface = tsec_get_interface(priv);
 
 	priv->interface = pdata->phy_interface;
diff --git a/drivers/net/xilinx_axi_emac.c b/drivers/net/xilinx_axi_emac.c
index f21addb4d0..02d13c3e0a 100644
--- a/drivers/net/xilinx_axi_emac.c
+++ b/drivers/net/xilinx_axi_emac.c
@@ -821,7 +821,6 @@ static int axi_emac_of_to_plat(struct udevice *dev)
 	struct eth_pdata *pdata = &plat->eth_pdata;
 	int node = dev_of_offset(dev);
 	int offset = 0;
-	const char *phy_mode;
 
 	pdata->iobase = dev_read_addr(dev);
 	plat->mactype = dev_get_driver_data(dev);
@@ -850,14 +849,9 @@ static int axi_emac_of_to_plat(struct udevice *dev)
 			plat->phy_of_handle = offset;
 		}
 
-		phy_mode = fdt_getprop(gd->fdt_blob, node, "phy-mode", NULL);
-		if (phy_mode)
-			pdata->phy_interface = phy_get_interface_by_name(phy_mode);
-		if (pdata->phy_interface == -1) {
-			printf("%s: Invalid PHY interface '%s'\n", __func__,
-			       phy_mode);
+		pdata->phy_interface = dev_read_phy_mode(dev);
+		if (pdata->phy_interface == PHY_INTERFACE_MODE_NONE)
 			return -EINVAL;
-		}
 
 		plat->eth_hasnobuf = fdtdec_get_bool(gd->fdt_blob, node,
 						     "xlnx,eth-hasnobuf");
diff --git a/drivers/net/zynq_gem.c b/drivers/net/zynq_gem.c
index 3118d14726..0062851134 100644
--- a/drivers/net/zynq_gem.c
+++ b/drivers/net/zynq_gem.c
@@ -827,7 +827,6 @@ static int zynq_gem_of_to_plat(struct udevice *dev)
 	struct eth_pdata *pdata = dev_get_plat(dev);
 	struct zynq_gem_priv *priv = dev_get_priv(dev);
 	struct ofnode_phandle_args phandle_args;
-	const char *phy_mode;
 
 	pdata->iobase = (phys_addr_t)dev_read_addr(dev);
 	priv->iobase = (struct zynq_gem_regs *)pdata->iobase;
@@ -859,13 +858,9 @@ static int zynq_gem_of_to_plat(struct udevice *dev)
 		}
 	}
 
-	phy_mode = dev_read_prop(dev, "phy-mode", NULL);
-	if (phy_mode)
-		pdata->phy_interface = phy_get_interface_by_name(phy_mode);
-	if (pdata->phy_interface == -1) {
-		debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
+	pdata->phy_interface = dev_read_phy_mode(dev);
+	if (pdata->phy_interface == PHY_INTERFACE_MODE_NONE)
 		return -EINVAL;
-	}
 	priv->interface = pdata->phy_interface;
 
 	priv->int_pcs = dev_read_bool(dev, "is-internal-pcspma");
diff --git a/include/dm/ofnode.h b/include/dm/ofnode.h
index 429aee2812..3bd3ba4925 100644
--- a/include/dm/ofnode.h
+++ b/include/dm/ofnode.h
@@ -12,6 +12,7 @@
 #include <dm/of.h>
 #include <dm/of_access.h>
 #include <log.h>
+#include <phy_interface.h>
 
 /* Enable checks to protect against invalid calls */
 #undef OF_CHECKS
@@ -1231,4 +1232,16 @@ const char *ofnode_conf_read_str(const char *prop_name);
  */
 ofnode ofnode_get_phy_node(ofnode eth_node);
 
+/**
+ * ofnode_read_phy_mode() - Read PHY connection type from a MAC node
+ *
+ * This function parses the "phy-mode" / "phy-connection-type" property and
+ * returns the corresponding PHY interface type.
+ *
+ * @mac_node:	ofnode containing the property
+ * Return: one of PHY_INTERFACE_MODE_* constants, PHY_INTERFACE_MODE_NONE on
+ *	   error
+ */
+phy_interface_t ofnode_read_phy_mode(ofnode mac_node);
+
 #endif
diff --git a/include/dm/read.h b/include/dm/read.h
index 899eb813fd..bfa2645967 100644
--- a/include/dm/read.h
+++ b/include/dm/read.h
@@ -757,6 +757,18 @@ int dev_decode_display_timing(const struct udevice *dev, int index,
  */
 ofnode dev_get_phy_node(const struct udevice *dev);
 
+/**
+ * dev_read_phy_mode() - Read PHY connection type from a MAC
+ *
+ * This function parses the "phy-mode" / "phy-connection-type" property and
+ * returns the corresponding PHY interface type.
+ *
+ * @dev: device representing the MAC
+ * Return: one of PHY_INTERFACE_MODE_* constants, PHY_INTERFACE_MODE_NONE on
+ *	   error
+ */
+phy_interface_t dev_read_phy_mode(const struct udevice *dev);
+
 #else /* CONFIG_DM_DEV_READ_INLINE is enabled */
 #include <asm/global_data.h>
 
@@ -1111,6 +1123,11 @@ static inline ofnode dev_get_phy_node(const struct udevice *dev)
 	return ofnode_get_phy_node(dev_ofnode(dev));
 }
 
+static inline phy_interface_t dev_read_phy_mode(const struct udevice *dev)
+{
+	return ofnode_read_phy_mode(dev_ofnode(dev));
+}
+
 #endif /* CONFIG_DM_DEV_READ_INLINE */
 
 /**
diff --git a/include/phy.h b/include/phy.h
index 9ea4bd42db..399e050aba 100644
--- a/include/phy.h
+++ b/include/phy.h
@@ -568,14 +568,6 @@ int phy_xilinx_gmii2rgmii_init(void);
 int board_phy_config(struct phy_device *phydev);
 int get_phy_id(struct mii_dev *bus, int addr, int devad, u32 *phy_id);
 
-/**
- * phy_get_interface_by_name() - Look up a PHY interface name
- *
- * @str:	PHY interface name, e.g. "mii"
- * @return: PHY_INTERFACE_MODE_... value, or -1 if not found
- */
-int phy_get_interface_by_name(const char *str);
-
 /**
  * phy_interface_is_rgmii - Convenience function for testing if a PHY interface
  * is RGMII (all variants)
diff --git a/net/mdio-uclass.c b/net/mdio-uclass.c
index bef8280e21..874f59413a 100644
--- a/net/mdio-uclass.c
+++ b/net/mdio-uclass.c
@@ -15,11 +15,6 @@
 #include <dm/uclass-internal.h>
 #include <linux/compat.h>
 
-/* DT node properties for MAC-PHY interface */
-static const char * const phy_mode_str[] = {
-	"phy-mode", "phy-connection-type"
-};
-
 void dm_mdio_probe_devices(void)
 {
 	struct udevice *it;
@@ -195,26 +190,15 @@ out:
 /* 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;
-	int i;
 
 	if (!dev_has_ofnode(ethdev)) {
 		debug("%s: supplied eth dev has no DT node!\n", ethdev->name);
 		return NULL;
 	}
 
-	interface = PHY_INTERFACE_MODE_NONE;
-	for (i = 0; i < ARRAY_SIZE(phy_mode_str); i++) {
-		if_str = dev_read_string(ethdev, phy_mode_str[i]);
-		if (if_str) {
-			interface = phy_get_interface_by_name(if_str);
-			break;
-		}
-	}
-	if (interface < 0)
-		interface = PHY_INTERFACE_MODE_NONE;
+	interface = dev_read_phy_mode(ethdev);
 	if (interface == PHY_INTERFACE_MODE_NONE)
 		dev_dbg(ethdev, "can't find interface mode, default to NONE\n");
 
diff --git a/test/dm/ofnode.c b/test/dm/ofnode.c
index 500c63aef5..61ae1db62d 100644
--- a/test/dm/ofnode.c
+++ b/test/dm/ofnode.c
@@ -451,11 +451,15 @@ DM_TEST(dm_test_ofnode_string_err, UT_TESTF_LIVE_TREE);
 static int dm_test_ofnode_get_phy(struct unit_test_state *uts)
 {
 	ofnode eth_node, phy_node;
+	phy_interface_t mode;
 	u32 reg;
 
 	eth_node = ofnode_path("/phy-test-eth");
 	ut_assert(ofnode_valid(eth_node));
 
+	mode = ofnode_read_phy_mode(eth_node);
+	ut_assert(mode == PHY_INTERFACE_MODE_2500BASEX);
+
 	phy_node = ofnode_get_phy_node(eth_node);
 	ut_assert(ofnode_valid(phy_node));
 
-- 
2.35.1



More information about the U-Boot mailing list