[PATCH v5] net: mdiobus: Add a function to deassert reset

From: Yan Wang
Date: Fri May 12 2023 - 03:12:08 EST


It is possible to mount multiple sub-devices on the mido bus.
The hardware power-on does not necessarily reset these devices.
The device may be in an uncertain state, causing the device's ID
to not be scanned.

So,before adding a reset to the scan, make sure the device is in
normal working mode.

Reported-by: kernel test robot <lkp@xxxxxxxxx>
Link: https://lore.kernel.org/oe-kbuild-all/202305101702.4xW6vT72-lkp@xxxxxxxxx/
Signed-off-by: Yan Wang <rk.code@xxxxxxxxxxx>
---
v5:
- fixed code style.
- Add fwnode_property_read_u32()'s return value processing.
v4: https://lore.kernel.org/all/KL1PR01MB54480925428513803DF3D03AE6749@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/
- Get pulse width and settling time from the device tree
- Add logic for processing (PTR_ERR(reset) == -EPROBE_DEFER)
- included <linux/goio/consumer.h>
- fixed commit message
v3: https://lore.kernel.org/all/KL1PR01MB5448A33A549CDAD7D68945B9E6779@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/
- fixed commit message
v2: https://lore.kernel.org/all/KL1PR01MB54482416A8BE0D80EA27223CE6779@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/
- fixed commit message
- Using gpiod_ replace gpio_
v1: https://lore.kernel.org/all/KL1PR01MB5448631F2D6F71021602117FE6769@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/
- Incorrect description of commit message.
- The gpio-api too old
---
drivers/net/mdio/fwnode_mdio.c | 48 ++++++++++++++++++++++++++++++++++
1 file changed, 48 insertions(+)

diff --git a/drivers/net/mdio/fwnode_mdio.c b/drivers/net/mdio/fwnode_mdio.c
index 1183ef5e203e..02e89e25c23d 100644
--- a/drivers/net/mdio/fwnode_mdio.c
+++ b/drivers/net/mdio/fwnode_mdio.c
@@ -11,6 +11,7 @@
#include <linux/of.h>
#include <linux/phy.h>
#include <linux/pse-pd/pse.h>
+#include <linux/gpio/consumer.h>

MODULE_AUTHOR("Calvin Johnson <calvin.johnson@xxxxxxxxxxx>");
MODULE_LICENSE("GPL");
@@ -57,6 +58,51 @@ fwnode_find_mii_timestamper(struct fwnode_handle *fwnode)
return register_mii_timestamper(arg.np, arg.args[0]);
}

+static void fwnode_mdiobus_pre_enable_phy(struct fwnode_handle *fwnode)
+{
+ unsigned int reset_deassert_delay;
+ unsigned int reset_assert_delay;
+ struct gpio_desc *reset;
+ int ret;
+
+ ret = fwnode_property_read_u32(fwnode, "reset-assert-us",
+ &reset_assert_delay);
+ if (ret) {
+ pr_err("%pOFn: %s : The reset-assert-us property missing\n",
+ to_of_node(fwnode), __func__);
+ return;
+ }
+
+ ret = fwnode_property_read_u32(fwnode, "reset-deassert-us",
+ &reset_deassert_delay);
+ if (ret) {
+ pr_err("%pOFn: %s : The reset-deassert-us property missing\n",
+ to_of_node(fwnode), __func__);
+ return;
+ }
+
+ reset = fwnode_gpiod_get_index(fwnode, "reset", 0, GPIOD_OUT_LOW, NULL);
+ if (IS_ERR(reset)) {
+ if (PTR_ERR(reset) == -EPROBE_DEFER)
+ pr_debug("%pOFn: %s: GPIOs not yet available, retry later\n",
+ to_of_node(fwnode), __func__);
+ else
+ pr_err("%pOFn: %s: Can't get reset line property\n",
+ to_of_node(fwnode), __func__);
+
+ return;
+ }
+
+ gpiod_set_value_cansleep(reset, gpiod_is_active_low(reset));
+ fsleep(reset_assert_delay);
+ gpiod_set_value_cansleep(reset, !gpiod_is_active_low(reset));
+ fsleep(reset_deassert_delay);
+ /*Release phy's reset line, mdiobus_register_gpiod() need to
+ *request it
+ */
+ gpiod_put(reset);
+}
+
int fwnode_mdiobus_phy_device_register(struct mii_bus *mdio,
struct phy_device *phy,
struct fwnode_handle *child, u32 addr)
@@ -119,6 +165,8 @@ int fwnode_mdiobus_register_phy(struct mii_bus *bus,
u32 phy_id;
int rc;

+ fwnode_mdiobus_pre_enable_phy(child);
+
psec = fwnode_find_pse_control(child);
if (IS_ERR(psec))
return PTR_ERR(psec);
--
2.17.1