Skip to contentSkip to conversation

net: phy: Handle PHY reset during initial PHY ID read

PATCH In review Chen-Yu Tsai 2 participants 5 messages Mar 2 – Mar 12

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: phy: Handle PHY reset during initial PHY ID read

The mdio device core handles reset GPIOs and controls for PHYs and MDIOs after the MDIO or PHY device is created. However this does not cover the initial PHY ID read _before_ the PHY device is created, since the PHY ID is needed for the PHY device. This causes PHY devices that have reset GPIOs or controls to not work after a reboot if the GPIO is left in the reset state; neither will it work if the reset GPIO is by default (for example, missing a pull-up) in the reset state.

One possible workaround is to place the reset GPIO or control property under the MDIO bus instead of under the PHY. However the common PHY device tree bindings already allow a reset for the PHY, so we should make some effort to support this.

Rework get_phy_device() to allow passing in a fwnode handle for the PHY device, and use the handle to acquire the reset GPIO, control and delay timings for it. Before reading the PHY ID, deassert the reset. This reworked version is renamed to fwnode_get_phy_device(), with get_phy_device() calling the new version.

Use this new version in fwnode_mdiobus_register_phy() so that PHY reset is handled. This allows the reset GPIO and reset control in the common Ethernet PHY device tree to be correctly handled without any gotchas.

---
This work was the result of Russell mentioning [1] that placing the reset GPIO under the PHY node in the device tree might result in it not working.

I also talked about this at Plumbers in Tokyo last year during the Device Tree MC. There a few people mentioned that MDIO reset handling has been a pain point.

[1] https://lore.kernel.org/linux-sunxi/[email protected]/

 drivers/net/mdio/fwnode_mdio.c |  2 +-
 drivers/net/phy/phy_device.c   | 47 +++++++++++++++++++++++++++++++---
 include/linux/phy.h            | 10 +++++++-
 3 files changed, 53 insertions(+), 6 deletions(-)
drivers/net/mdio/fwnode_mdio.c +1 -1
index ba7091518265..f62a48583404 100644
@@ -130,7 +130,7 @@ int fwnode_mdiobus_register_phy(struct mii_bus *bus,
 
 	is_c45 = fwnode_device_is_compatible(child, "ethernet-phy-ieee802.3-c45");
 	if (is_c45 || fwnode_get_phy_id(child, &phy_id))
-		phy = get_phy_device(bus, addr, is_c45);
+		phy = fwnode_get_phy_device(bus, addr, child, is_c45);
 	else
 		phy = phy_device_create(bus, addr, phy_id, 0, NULL);
 	if (IS_ERR(phy)) {
drivers/net/phy/phy_device.c +43 -4

97 lines not on this page · raw message

include/linux/phy.h +9 -1
index 6f9979a26892..3bd3bbc1b281 100644
@@ -2142,7 +2142,15 @@ int fwnode_get_phy_id(struct fwnode_handle *fwnode, u32 *phy_id);
 struct mdio_device *fwnode_mdio_find_device(struct fwnode_handle *fwnode);
 struct phy_device *fwnode_phy_find_device(struct fwnode_handle *phy_fwnode);
 struct fwnode_handle *fwnode_get_phy_node(const struct fwnode_handle *fwnode);
-struct phy_device *get_phy_device(struct mii_bus *bus, int addr, bool is_c45);
+struct phy_device *fwnode_get_phy_device(struct mii_bus *bus, int addr,
+					 struct fwnode_handle *fwnode, bool is_c45);
+
+static inline
+struct phy_device *get_phy_device(struct mii_bus *bus, int addr, bool is_c45)
+{
+	return fwnode_get_phy_device(bus, addr, NULL, is_c45);
+}
+
 int phy_device_register(struct phy_device *phy);
 void phy_device_free(struct phy_device *phydev);
 void phy_device_remove(struct phy_device *phydev);
Signature · 1 line
2.47.3
maintainer
#1 lore raw
Quoted 8 lines · Chen-Yu Tsai

The mdio device core handles reset GPIOs and controls for PHYs and MDIOs after the MDIO or PHY device is created. However this does not cover the initial PHY ID read _before_ the PHY device is created, since the PHY ID is needed for the PHY device. This causes PHY devices that have reset GPIOs or controls to not work after a reboot if the GPIO is left in the reset state; neither will it work if the reset GPIO is by default (for example, missing a pull-up) in the reset state.

Quoted 4 lines

One possible workaround is to place the reset GPIO or control property under the MDIO bus instead of under the PHY. However the common PHY device tree bindings already allow a reset for the PHY, so we should make some effort to support this.

Please either:

1) Add the ID to the DT

2) Work on driver/base, and add generic power on sequencing code which can handle reset GPIOs, reset controllers, regulators, clocks etc. A solution which works for PCIe, USB, SATA, and other enumerable devices. And then modify phylib to use this generic code.

Andrew

---
pw-bot: cr

maintainer
#2 lore raw
In reply to Chen-Yu Tsai
Quoted 7 lines

This work was the result of Russell mentioning [1] that placing the reset GPIO under the PHY node in the device tree might result in it not working.

I also talked about this at Plumbers in Tokyo last year during the Device Tree MC. There a few people mentioned that MDIO reset handling has been a pain point.

It is definitely a pain point. I wish we never added support for resets, just left it to the bootloader to get the hardware into a usable state before starting linux.

Anyway, i don't really see this being a MDIO/PHY problem. It is a generic device driver model problem. We need the core to have generic code used for any enumerable bus, PCIe, USB, SDIO, MDIO etc to ensure the clocks, resets, regulators, etc are in the correct state that the probe can work.

Once the core has this facility, we can modify MDIO to make use of it, if needed.

Andrew

---
pw-bot: cr

#3 lore raw
Quoted 13 lines · Andrew Lunn
Quoted 8 lines

This work was the result of Russell mentioning [1] that placing the reset GPIO under the PHY node in the device tree might result in it not working.

I also talked about this at Plumbers in Tokyo last year during the Device Tree MC. There a few people mentioned that MDIO reset handling has been a pain point.

It is definitely a pain point. I wish we never added support for resets, just left it to the bootloader to get the hardware into a usable state before starting linux.

Quoted 5 lines

Anyway, i don't really see this being a MDIO/PHY problem. It is a generic device driver model problem. We need the core to have generic code used for any enumerable bus, PCIe, USB, SDIO, MDIO etc to ensure the clocks, resets, regulators, etc are in the correct state that the probe can work.

I agree it's not just a MDIO/PHY problem. I am looking into it for all the common enumerable bus types. Currently SDIO, USB, and PCIe all have some solution, though the SDIO one is deprecated, and the USB one is incomplete.

That said, this doesn't fit into the driver model well: to create a struct device the device needs to be enumerated; to enumerate the device, its resources need to be provided and enabled, but at this point there's no device to tie the resources to.

All the existing solutions scan the DT and create extra platform devices to handle the power sequencing. Some bus specific code is needed for this. USB is simpler, since once the USB device powers up it will initiate a handshake with the host. All the other bus types rely on the host controller doing a scan to enumerate the devices, so the power sequencing code needs to be inserted into the bus scanning code. The PCI implementation is extra complicated: some controllers have dedicated PERST# functions that are toggled from the controller's registers instead of using GPIOs for the device reset.

Quoted 2 lines

Once the core has this facility, we can modify MDIO to make use of it, if needed.

We already have the power sequencing framework (drivers/power/sequencing). So instead of the open coded reset GPIO I did in this patch, it would just be calls to pwrseq_get() and pwrseq_power_on(). I would need to figure out how to actually work them into the MDIO subsystem, and also how to model a generic PHY power sequencing provider. But the recent M.2 slot work [1] provides some good examples.

Does that sound more acceptable?

Thanks
ChenYu

[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/

maintainer
#4 lore raw
Quoted 2 lines

I agree it's not just a MDIO/PHY problem. I am looking into it for all the common enumerable bus types.

Great.

Quoted 6 lines

We already have the power sequencing framework (drivers/power/sequencing). So instead of the open coded reset GPIO I did in this patch, it would just be calls to pwrseq_get() and pwrseq_power_on(). I would need to figure out how to actually work them into the MDIO subsystem, and also how to model a generic PHY power sequencing provider. But the recent M.2 slot work [1] provides some good examples.

You need to be careful with "generic PHY". That generally means drivers/phy and the config symbol GENERIC_PHY. Ethernet PHYs have been around a long time, so PHY generally means Ethernet PHY. But "recently", devices with SERDES interfaces, PCIe, SATA, USB, Ethernet PCS have a 'phy' which needs configuring, so we gained the Generic PHY subsystem. And we gained the confusion of PHY vs Generic PHY.

Quoted 1 line

Does that sound more acceptable?

Yes it does.

Thanks
Andrew