186 lines · c
1// SPDX-License-Identifier: GPL-2.0-only2/*3 * fwnode helpers for the MDIO (Ethernet PHY) API4 *5 * This file provides helper functions for extracting PHY device information6 * out of the fwnode and using it to populate an mii_bus.7 */8 9#include <linux/acpi.h>10#include <linux/dev_printk.h>11#include <linux/fwnode_mdio.h>12#include <linux/of.h>13#include <linux/phy.h>14#include <linux/pse-pd/pse.h>15 16MODULE_AUTHOR("Calvin Johnson <calvin.johnson@oss.nxp.com>");17MODULE_LICENSE("GPL");18MODULE_DESCRIPTION("FWNODE MDIO bus (Ethernet PHY) accessors");19 20static struct pse_control *21fwnode_find_pse_control(struct fwnode_handle *fwnode)22{23 struct pse_control *psec;24 struct device_node *np;25 26 if (!IS_ENABLED(CONFIG_PSE_CONTROLLER))27 return NULL;28 29 np = to_of_node(fwnode);30 if (!np)31 return NULL;32 33 psec = of_pse_control_get(np);34 if (PTR_ERR(psec) == -ENOENT)35 return NULL;36 37 return psec;38}39 40static struct mii_timestamper *41fwnode_find_mii_timestamper(struct fwnode_handle *fwnode)42{43 struct of_phandle_args arg;44 int err;45 46 if (is_acpi_node(fwnode))47 return NULL;48 49 err = of_parse_phandle_with_fixed_args(to_of_node(fwnode),50 "timestamper", 1, 0, &arg);51 if (err == -ENOENT)52 return NULL;53 else if (err)54 return ERR_PTR(err);55 56 if (arg.args_count != 1)57 return ERR_PTR(-EINVAL);58 59 return register_mii_timestamper(arg.np, arg.args[0]);60}61 62int fwnode_mdiobus_phy_device_register(struct mii_bus *mdio,63 struct phy_device *phy,64 struct fwnode_handle *child, u32 addr)65{66 int rc;67 68 rc = fwnode_irq_get(child, 0);69 /* Don't wait forever if the IRQ provider doesn't become available,70 * just fall back to poll mode71 */72 if (rc == -EPROBE_DEFER)73 rc = driver_deferred_probe_check_state(&phy->mdio.dev);74 if (rc == -EPROBE_DEFER)75 return rc;76 77 if (rc > 0) {78 phy->irq = rc;79 mdio->irq[addr] = rc;80 } else {81 phy->irq = mdio->irq[addr];82 }83 84 if (fwnode_property_read_bool(child, "broken-turn-around"))85 mdio->phy_ignore_ta_mask |= 1 << addr;86 87 fwnode_property_read_u32(child, "reset-assert-us",88 &phy->mdio.reset_assert_delay);89 fwnode_property_read_u32(child, "reset-deassert-us",90 &phy->mdio.reset_deassert_delay);91 92 /* Associate the fwnode with the device structure so it93 * can be looked up later94 */95 fwnode_handle_get(child);96 device_set_node(&phy->mdio.dev, child);97 98 /* All data is now stored in the phy struct;99 * register it100 */101 rc = phy_device_register(phy);102 if (rc) {103 device_set_node(&phy->mdio.dev, NULL);104 fwnode_handle_put(child);105 return rc;106 }107 108 dev_dbg(&mdio->dev, "registered phy fwnode %pfw at address %i\n",109 child, addr);110 return 0;111}112EXPORT_SYMBOL(fwnode_mdiobus_phy_device_register);113 114int fwnode_mdiobus_register_phy(struct mii_bus *bus,115 struct fwnode_handle *child, u32 addr)116{117 struct mii_timestamper *mii_ts = NULL;118 struct pse_control *psec = NULL;119 struct phy_device *phy;120 bool is_c45;121 u32 phy_id;122 int rc;123 124 psec = fwnode_find_pse_control(child);125 if (IS_ERR(psec))126 return PTR_ERR(psec);127 128 mii_ts = fwnode_find_mii_timestamper(child);129 if (IS_ERR(mii_ts)) {130 rc = PTR_ERR(mii_ts);131 goto clean_pse;132 }133 134 is_c45 = fwnode_device_is_compatible(child, "ethernet-phy-ieee802.3-c45");135 if (is_c45 || fwnode_get_phy_id(child, &phy_id))136 phy = get_phy_device(bus, addr, is_c45);137 else138 phy = phy_device_create(bus, addr, phy_id, 0, NULL);139 if (IS_ERR(phy)) {140 rc = PTR_ERR(phy);141 goto clean_mii_ts;142 }143 144 if (is_acpi_node(child)) {145 phy->irq = bus->irq[addr];146 147 /* Associate the fwnode with the device structure so it148 * can be looked up later.149 */150 phy->mdio.dev.fwnode = fwnode_handle_get(child);151 152 /* All data is now stored in the phy struct, so register it */153 rc = phy_device_register(phy);154 if (rc) {155 phy->mdio.dev.fwnode = NULL;156 fwnode_handle_put(child);157 goto clean_phy;158 }159 } else if (is_of_node(child)) {160 rc = fwnode_mdiobus_phy_device_register(bus, phy, child, addr);161 if (rc)162 goto clean_phy;163 }164 165 phy->psec = psec;166 167 /* phy->mii_ts may already be defined by the PHY driver. A168 * mii_timestamper probed via the device tree will still have169 * precedence.170 */171 if (mii_ts)172 phy->mii_ts = mii_ts;173 174 return 0;175 176clean_phy:177 phy_device_free(phy);178clean_mii_ts:179 unregister_mii_timestamper(mii_ts);180clean_pse:181 pse_control_put(psec);182 183 return rc;184}185EXPORT_SYMBOL(fwnode_mdiobus_register_phy);186