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>
14 MODULE_AUTHOR("Calvin Johnson <calvin.johnson@oss.nxp.com>");
15 MODULE_LICENSE("GPL");
17 static struct mii_timestamper *
18 fwnode_find_mii_timestamper(struct fwnode_handle *fwnode)
20 struct of_phandle_args arg;
23 if (is_acpi_node(fwnode))
26 err = of_parse_phandle_with_fixed_args(to_of_node(fwnode),
27 "timestamper", 1, 0, &arg);
33 if (arg.args_count != 1)
34 return ERR_PTR(-EINVAL);
36 return register_mii_timestamper(arg.np, arg.args[0]);
39 int fwnode_mdiobus_phy_device_register(struct mii_bus *mdio,
40 struct phy_device *phy,
41 struct fwnode_handle *child, u32 addr)
45 rc = fwnode_irq_get(child, 0);
46 /* Don't wait forever if the IRQ provider doesn't become available,
47 * just fall back to poll mode
49 if (rc == -EPROBE_DEFER)
50 rc = driver_deferred_probe_check_state(&phy->mdio.dev);
51 if (rc == -EPROBE_DEFER)
58 phy->irq = mdio->irq[addr];
61 if (fwnode_property_read_bool(child, "broken-turn-around"))
62 mdio->phy_ignore_ta_mask |= 1 << addr;
64 fwnode_property_read_u32(child, "reset-assert-us",
65 &phy->mdio.reset_assert_delay);
66 fwnode_property_read_u32(child, "reset-deassert-us",
67 &phy->mdio.reset_deassert_delay);
69 /* Associate the fwnode with the device structure so it
70 * can be looked up later
72 fwnode_handle_get(child);
73 device_set_node(&phy->mdio.dev, child);
75 /* All data is now stored in the phy struct;
78 rc = phy_device_register(phy);
80 fwnode_handle_put(child);
84 dev_dbg(&mdio->dev, "registered phy %p fwnode at address %i\n",
88 EXPORT_SYMBOL(fwnode_mdiobus_phy_device_register);
90 int fwnode_mdiobus_register_phy(struct mii_bus *bus,
91 struct fwnode_handle *child, u32 addr)
93 struct mii_timestamper *mii_ts = NULL;
94 struct phy_device *phy;
99 mii_ts = fwnode_find_mii_timestamper(child);
101 return PTR_ERR(mii_ts);
103 rc = fwnode_property_match_string(child, "compatible",
104 "ethernet-phy-ieee802.3-c45");
108 if (is_c45 || fwnode_get_phy_id(child, &phy_id))
109 phy = get_phy_device(bus, addr, is_c45);
111 phy = phy_device_create(bus, addr, phy_id, 0, NULL);
113 unregister_mii_timestamper(mii_ts);
117 if (is_acpi_node(child)) {
118 phy->irq = bus->irq[addr];
120 /* Associate the fwnode with the device structure so it
121 * can be looked up later.
123 phy->mdio.dev.fwnode = child;
125 /* All data is now stored in the phy struct, so register it */
126 rc = phy_device_register(phy);
128 phy_device_free(phy);
129 fwnode_handle_put(phy->mdio.dev.fwnode);
132 } else if (is_of_node(child)) {
133 rc = fwnode_mdiobus_phy_device_register(bus, phy, child, addr);
135 unregister_mii_timestamper(mii_ts);
136 phy_device_free(phy);
141 /* phy->mii_ts may already be defined by the PHY driver. A
142 * mii_timestamper probed via the device tree will still have
146 phy->mii_ts = mii_ts;
149 EXPORT_SYMBOL(fwnode_mdiobus_register_phy);