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