net: phy: fix duplicate eth_phy binding
Subsystems (2)
PATCH v4 1/1 net: phy: fix duplicate eth_phy binding
When both CONFIG_PHY_ETHERNET_ID and CONFIG_DM_ETH_PHY are enabled, eth_phy_binds_nodes() called from eth_post_bind() already binds the ethernet PHY node to eth_phy_generic_drv. However, phy_connect_phy_id() called via phy_connect() also binds the same PHY node, resulting in duplicate entries in the DM tree.
Fix this by checking at the beginning of phy_connect() whether the PHY is already bound via uclass_find_device_by_phandle(). If so, skip all generic binding methods and fall through to phy_find_by_mask(). Also assign phydev->node from the already bound DM device.
---
Changes in v4:
- Removed IS_ENABLED(CONFIG_DM_ETH_PHY), use runtime check via uclass_find_device_by_phandle()
- Fix indentation of binding methods block
- Assign phydev->node from the bound DM device's ofnode
- Use phy_bound flag for clarity
Changes in v3:
- Moved duplicate binding check to beginning of phy_connect()
Changes in v2:
- Move duplicate binding check to caller phy_connect()
- Update commit description
drivers/net/phy/phy.c | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-)
drivers/net/phy/phy.c +20 -8
@@ -24,6 +24,7 @@#include <linux/delay.h> #include <linux/err.h> #include <linux/compiler.h> +#include <dm/uclass-internal.h> /* Generic PHY support and helper functions */
@@ -927,29 +928,40 @@ struct phy_device *phy_connect(struct mii_dev *bus, int addr, {struct phy_device *phydev =NULL ;uint mask = (addr >=0 ) ? (1 << addr) :0xffffffff ; - +struct udevice *phy_dev; +bool phy_bound; + +/* Skip binding if PHY already bound by eth_phy_binds_nodes(). */ + phy_bound = !uclass_find_device_by_phandle (UCLASS_ETH_PHY , dev, +"phy-handle" , + &phy_dev); +if (!phy_bound) {#ifdef CONFIG_PHY_FIXED - phydev =phy_connect_fixed (bus, dev); + phydev =phy_connect_fixed (bus, dev);#endif #ifdef CONFIG_PHY_NCSI -if (!phydev && interface ==PHY_INTERFACE_MODE_NCSI ) - phydev =phy_device_create (bus,0 ,PHY_NCSI_ID , false); +if (!phydev && interface ==PHY_INTERFACE_MODE_NCSI ) + phydev =phy_device_create (bus,0 ,PHY_NCSI_ID , false);#endif #ifdef CONFIG_PHY_ETHERNET_ID -if (!phydev) - phydev =phy_connect_phy_id (bus, dev, addr); +if (!phydev) + phydev =phy_connect_phy_id (bus, dev, addr);#endif #ifdef CONFIG_PHY_XILINX_GMII2RGMII -if (!phydev) - phydev =phy_connect_gmii2rgmii (bus, dev); +if (!phydev) + phydev =phy_connect_gmii2rgmii (bus, dev);#endif + }if (!phydev) phydev =phy_find_by_mask (bus, mask); +if (phydev && phy_bound && !ofnode_valid (phydev->node)) + phydev->node =dev_ofnode (phy_dev); +if (phydev)phy_connect_dev (phydev, dev, interface);else
Signature · 1 line
2.34.1
On 5/12/26 2:23 PM, Pranav Tilak wrote:
[...]
drivers/net/phy/phy.c struct phy_device *phy_connect(struct mii_dev *bus, int addr, · line 938
Quoted 13 lines
drivers/net/phy/phy.c +8
-1@@ -927,29 +928,40 @@ struct phy_device *phy_connect(struct mii_dev *bus, int addr, {struct phy_device *phydev =NULL ;uint mask = (addr >=0 ) ? (1 << addr) :0xffffffff ; - +struct udevice *phy_dev; +bool phy_bound; + +/* Skip binding if PHY already bound by eth_phy_binds_nodes(). */ + phy_bound = !uclass_find_device_by_phandle (UCLASS_ETH_PHY , dev, +"phy-handle" , + &phy_dev); +if (!phy_bound) {
The double negation is not needed, simply use int data type .
Also, try "goto exit" ...
Quoted 16 lines
drivers/net/phy/phy.c +4
-5#ifdef CONFIG_PHY_FIXED - phydev =phy_connect_fixed (bus, dev); + phydev =phy_connect_fixed (bus, dev);#endif #ifdef CONFIG_PHY_NCSI -if (!phydev && interface ==PHY_INTERFACE_MODE_NCSI ) - phydev =phy_device_create (bus,0 ,PHY_NCSI_ID , false); +if (!phydev && interface ==PHY_INTERFACE_MODE_NCSI ) + phydev =phy_device_create (bus,0 ,PHY_NCSI_ID , false);#endif #ifdef CONFIG_PHY_ETHERNET_ID -if (!phydev) - phydev =phy_connect_phy_id (bus, dev, addr); +if (!phydev)
drivers/net/phy/phy.c struct phy_device *phy_connect(struct mii_dev *bus, int addr, · line 957
Quoted 10 lines
drivers/net/phy/phy.c +4
-2+ phydev =phy_connect_phy_id (bus, dev, addr);#endif #ifdef CONFIG_PHY_XILINX_GMII2RGMII -if (!phydev) - phydev =phy_connect_gmii2rgmii (bus, dev); +if (!phydev) + phydev =phy_connect_gmii2rgmii (bus, dev);#endif + }
exit:
This may simplify the patch .
Quoted 9 lines
drivers/net/phy/phy.c +3
-0if (!phydev) phydev =phy_find_by_mask (bus, mask); +if (phydev && phy_bound && !ofnode_valid (phydev->node)) + phydev->node =dev_ofnode (phy_dev); +if (phydev)phy_connect_dev (phydev, dev, interface);else
[...]