1 // SPDX-License-Identifier: GPL-2.0-only
3 * fwnode helpers for the MDIO (Ethernet PHY) API
5 * This file provides helper functions for extracting PHY device information
6 * out of the fwnode and using it to populate an mii_bus.
9 #include <linux/acpi.h>
10 #include <linux/fwnode_mdio.h>
12 #include <linux/phy.h>
13 #include <linux/pse-pd/pse.h>
16 MODULE_LICENSE("GPL");
17 MODULE_DESCRIPTION("FWNODE MDIO bus (Ethernet PHY) accessors");
19 static struct pse_control *
20 fwnode_find_pse_control(struct fwnode_handle *fwnode)
22 struct pse_control *psec;
23 struct device_node *np;
25 if (!IS_ENABLED(CONFIG_PSE_CONTROLLER))
28 np = to_of_node(fwnode);
32 psec = of_pse_control_get(np);
33 if (PTR_ERR(psec) == -ENOENT)
39 static struct mii_timestamper *
40 fwnode_find_mii_timestamper(struct fwnode_handle *fwnode)
42 struct of_phandle_args arg;
45 if (is_acpi_node(fwnode))
48 err = of_parse_phandle_with_fixed_args(to_of_node(fwnode),
49 "timestamper", 1, 0, &arg);
55 if (arg.args_count != 1)
56 return ERR_PTR(-EINVAL);
58 return register_mii_timestamper(arg.np, arg.args[0]);
61 int fwnode_mdiobus_phy_device_register(struct mii_bus *mdio,
62 struct phy_device *phy,
63 struct fwnode_handle *child, u32 addr)
67 rc = fwnode_irq_get(child, 0);
68 /* Don't wait forever if the IRQ provider doesn't become available,
69 * just fall back to poll mode
71 if (rc == -EPROBE_DEFER)
72 rc = driver_deferred_probe_check_state(&phy->mdio.dev);
73 if (rc == -EPROBE_DEFER)
80 phy->irq = mdio->irq[addr];
83 if (fwnode_property_read_bool(child, "broken-turn-around"))
84 mdio->phy_ignore_ta_mask |= 1 << addr;
86 fwnode_property_read_u32(child, "reset-assert-us",
87 &phy->mdio.reset_assert_delay);
88 fwnode_property_read_u32(child, "reset-deassert-us",
89 &phy->mdio.reset_deassert_delay);
91 /* Associate the fwnode with the device structure so it
92 * can be looked up later
94 fwnode_handle_get(child);
95 device_set_node(&phy->mdio.dev, child);
97 /* All data is now stored in the phy struct;
100 rc = phy_device_register(phy);
102 device_set_node(&phy->mdio.dev, NULL);
103 fwnode_handle_put(child);
107 dev_dbg(&mdio->dev, "registered phy %p fwnode at address %i\n",
111 EXPORT_SYMBOL(fwnode_mdiobus_phy_device_register);
113 int fwnode_mdiobus_register_phy(struct mii_bus *bus,
114 struct fwnode_handle *child, u32 addr)
116 struct mii_timestamper *mii_ts = NULL;
117 struct pse_control *psec = NULL;
118 struct phy_device *phy;
123 psec = fwnode_find_pse_control(child);
125 return PTR_ERR(psec);
127 mii_ts = fwnode_find_mii_timestamper(child);
128 if (IS_ERR(mii_ts)) {
129 rc = PTR_ERR(mii_ts);
133 is_c45 = fwnode_device_is_compatible(child, "ethernet-phy-ieee802.3-c45");
134 if (is_c45 || fwnode_get_phy_id(child, &phy_id))
135 phy = get_phy_device(bus, addr, is_c45);
137 phy = phy_device_create(bus, addr, phy_id, 0, NULL);
143 if (is_acpi_node(child)) {
144 phy->irq = bus->irq[addr];
146 /* Associate the fwnode with the device structure so it
147 * can be looked up later.
149 phy->mdio.dev.fwnode = fwnode_handle_get(child);
151 /* All data is now stored in the phy struct, so register it */
152 rc = phy_device_register(phy);
154 phy->mdio.dev.fwnode = NULL;
155 fwnode_handle_put(child);
158 } else if (is_of_node(child)) {
159 rc = fwnode_mdiobus_phy_device_register(bus, phy, child, addr);
166 /* phy->mii_ts may already be defined by the PHY driver. A
167 * mii_timestamper probed via the device tree will still have
171 phy->mii_ts = mii_ts;
176 phy_device_free(phy);
178 unregister_mii_timestamper(mii_ts);
180 pse_control_put(psec);
184 EXPORT_SYMBOL(fwnode_mdiobus_register_phy);