1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * OF helpers for the MDIO (Ethernet PHY) API 4 * 5 * Copyright (c) 2009 Secret Lab Technologies, Ltd. 6 * 7 * This file provides helper functions for extracting PHY device information 8 * out of the OpenFirmware device tree and using it to populate an mii_bus. 9 */ 10 11 #include <linux/device.h> 12 #include <linux/err.h> 13 #include <linux/fwnode_mdio.h> 14 #include <linux/kernel.h> 15 #include <linux/module.h> 16 #include <linux/netdevice.h> 17 #include <linux/of.h> 18 #include <linux/of_irq.h> 19 #include <linux/of_mdio.h> 20 #include <linux/of_net.h> 21 #include <linux/phy.h> 22 #include <linux/phy_fixed.h> 23 24 #define DEFAULT_GPIO_RESET_DELAY 10 /* in microseconds */ 25 26 MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>"); 27 MODULE_LICENSE("GPL"); 28 MODULE_DESCRIPTION("OpenFirmware MDIO bus (Ethernet PHY) accessors"); 29 30 /* Extract the clause 22 phy ID from the compatible string of the form 31 * ethernet-phy-idAAAA.BBBB */ 32 static int of_get_phy_id(struct device_node *device, u32 *phy_id) 33 { 34 return fwnode_get_phy_id(of_fwnode_handle(device), phy_id); 35 } 36 37 int of_mdiobus_phy_device_register(struct mii_bus *mdio, struct phy_device *phy, 38 struct device_node *child, u32 addr) 39 { 40 return fwnode_mdiobus_phy_device_register(mdio, phy, 41 of_fwnode_handle(child), 42 addr); 43 } 44 EXPORT_SYMBOL(of_mdiobus_phy_device_register); 45 46 static int of_mdiobus_register_phy(struct mii_bus *mdio, 47 struct device_node *child, u32 addr) 48 { 49 return fwnode_mdiobus_register_phy(mdio, of_fwnode_handle(child), addr); 50 } 51 52 static int of_mdiobus_register_device(struct mii_bus *mdio, 53 struct device_node *child, u32 addr) 54 { 55 struct fwnode_handle *fwnode = of_fwnode_handle(child); 56 struct mdio_device *mdiodev; 57 int rc; 58 59 mdiodev = mdio_device_create(mdio, addr); 60 if (IS_ERR(mdiodev)) 61 return PTR_ERR(mdiodev); 62 63 /* Associate the OF node with the device structure so it 64 * can be looked up later. 65 */ 66 fwnode_handle_get(fwnode); 67 device_set_node(&mdiodev->dev, fwnode); 68 69 /* All data is now stored in the mdiodev struct; register it. */ 70 rc = mdio_device_register(mdiodev); 71 if (rc) { 72 device_set_node(&mdiodev->dev, NULL); 73 fwnode_handle_put(fwnode); 74 mdio_device_free(mdiodev); 75 return rc; 76 } 77 78 dev_dbg(&mdio->dev, "registered mdio device %pOFn at address %i\n", 79 child, addr); 80 return 0; 81 } 82 83 /* The following is a list of PHY compatible strings which appear in 84 * some DTBs. The compatible string is never matched against a PHY 85 * driver, so is pointless. We only expect devices which are not PHYs 86 * to have a compatible string, so they can be matched to an MDIO 87 * driver. Encourage users to upgrade their DT blobs to remove these. 88 */ 89 static const struct of_device_id whitelist_phys[] = { 90 { .compatible = "brcm,40nm-ephy" }, 91 { .compatible = "broadcom,bcm5241" }, 92 { .compatible = "marvell,88E1111", }, 93 { .compatible = "marvell,88e1116", }, 94 { .compatible = "marvell,88e1118", }, 95 { .compatible = "marvell,88e1145", }, 96 { .compatible = "marvell,88e1149r", }, 97 { .compatible = "marvell,88e1310", }, 98 { .compatible = "marvell,88E1510", }, 99 { .compatible = "marvell,88E1514", }, 100 { .compatible = "moxa,moxart-rtl8201cp", }, 101 {} 102 }; 103 104 /* 105 * Return true if the child node is for a phy. It must either: 106 * o Compatible string of "ethernet-phy-idX.X" 107 * o Compatible string of "ethernet-phy-ieee802.3-c45" 108 * o Compatible string of "ethernet-phy-ieee802.3-c22" 109 * o In the white list above (and issue a warning) 110 * o No compatibility string 111 * 112 * A device which is not a phy is expected to have a compatible string 113 * indicating what sort of device it is. 114 */ 115 bool of_mdiobus_child_is_phy(struct device_node *child) 116 { 117 u32 phy_id; 118 119 if (of_get_phy_id(child, &phy_id) != -EINVAL) 120 return true; 121 122 if (of_device_is_compatible(child, "ethernet-phy-ieee802.3-c45")) 123 return true; 124 125 if (of_device_is_compatible(child, "ethernet-phy-ieee802.3-c22")) 126 return true; 127 128 if (of_match_node(whitelist_phys, child)) { 129 pr_warn(FW_WARN 130 "%pOF: Whitelisted compatible string. Please remove\n", 131 child); 132 return true; 133 } 134 135 if (!of_property_present(child, "compatible")) 136 return true; 137 138 return false; 139 } 140 EXPORT_SYMBOL(of_mdiobus_child_is_phy); 141 142 static void __of_mdiobus_unregister_callback(struct mii_bus *mdio) 143 { 144 of_node_put(mdio->dev.of_node); 145 } 146 147 /** 148 * __of_mdiobus_register - Register mii_bus and create PHYs from the device tree 149 * @mdio: pointer to mii_bus structure 150 * @np: pointer to device_node of MDIO bus. 151 * @owner: module owning the @mdio object. 152 * 153 * This function registers the mii_bus structure and registers a phy_device 154 * for each child node of @np. 155 */ 156 int __of_mdiobus_register(struct mii_bus *mdio, struct device_node *np, 157 struct module *owner) 158 { 159 struct device_node *child; 160 bool scanphys = false; 161 int addr, rc; 162 163 if (!np) 164 return __mdiobus_register(mdio, owner); 165 166 /* Do not continue if the node is disabled */ 167 if (!of_device_is_available(np)) 168 return -ENODEV; 169 170 /* Mask out all PHYs from auto probing. Instead the PHYs listed in 171 * the device tree are populated after the bus has been registered */ 172 mdio->phy_mask = ~0; 173 174 mdio->__unregister_callback = __of_mdiobus_unregister_callback; 175 of_node_get(np); 176 device_set_node(&mdio->dev, of_fwnode_handle(np)); 177 178 /* Get bus level PHY reset GPIO details */ 179 mdio->reset_delay_us = DEFAULT_GPIO_RESET_DELAY; 180 of_property_read_u32(np, "reset-delay-us", &mdio->reset_delay_us); 181 mdio->reset_post_delay_us = 0; 182 of_property_read_u32(np, "reset-post-delay-us", &mdio->reset_post_delay_us); 183 184 /* Register the MDIO bus */ 185 rc = __mdiobus_register(mdio, owner); 186 if (rc) 187 goto put_node; 188 189 /* Loop over the child nodes and register a phy_device for each phy */ 190 for_each_available_child_of_node(np, child) { 191 addr = of_mdio_parse_addr(&mdio->dev, child); 192 if (addr < 0) { 193 scanphys = true; 194 continue; 195 } 196 197 if (of_mdiobus_child_is_phy(child)) 198 rc = of_mdiobus_register_phy(mdio, child, addr); 199 else 200 rc = of_mdiobus_register_device(mdio, child, addr); 201 202 if (rc == -ENODEV) 203 dev_err(&mdio->dev, 204 "MDIO device at address %d is missing.\n", 205 addr); 206 else if (rc) 207 goto unregister; 208 } 209 210 if (!scanphys) 211 return 0; 212 213 /* auto scan for PHYs with empty reg property */ 214 for_each_available_child_of_node(np, child) { 215 /* Skip PHYs with reg property set */ 216 if (of_property_present(child, "reg")) 217 continue; 218 219 for (addr = 0; addr < PHY_MAX_ADDR; addr++) { 220 /* skip already registered PHYs */ 221 if (mdiobus_is_registered_device(mdio, addr)) 222 continue; 223 224 /* be noisy to encourage people to set reg property */ 225 dev_info(&mdio->dev, "scan phy %pOFn at address %i\n", 226 child, addr); 227 228 if (of_mdiobus_child_is_phy(child)) { 229 /* -ENODEV is the return code that PHYLIB has 230 * standardized on to indicate that bus 231 * scanning should continue. 232 */ 233 rc = of_mdiobus_register_phy(mdio, child, addr); 234 if (!rc) 235 break; 236 if (rc != -ENODEV) 237 goto unregister; 238 } 239 } 240 } 241 242 return 0; 243 244 unregister: 245 of_node_put(child); 246 mdiobus_unregister(mdio); 247 248 put_node: 249 of_node_put(np); 250 return rc; 251 } 252 EXPORT_SYMBOL(__of_mdiobus_register); 253 254 /** 255 * of_mdio_find_device - Given a device tree node, find the mdio_device 256 * @np: pointer to the mdio_device's device tree node 257 * 258 * If successful, returns a pointer to the mdio_device with the embedded 259 * struct device refcount incremented by one, or NULL on failure. 260 * The caller should call put_device() on the mdio_device after its use 261 */ 262 struct mdio_device *of_mdio_find_device(struct device_node *np) 263 { 264 return fwnode_mdio_find_device(of_fwnode_handle(np)); 265 } 266 EXPORT_SYMBOL(of_mdio_find_device); 267 268 /** 269 * of_phy_find_device - Give a PHY node, find the phy_device 270 * @phy_np: Pointer to the phy's device tree node 271 * 272 * If successful, returns a pointer to the phy_device with the embedded 273 * struct device refcount incremented by one, or NULL on failure. 274 */ 275 struct phy_device *of_phy_find_device(struct device_node *phy_np) 276 { 277 return fwnode_phy_find_device(of_fwnode_handle(phy_np)); 278 } 279 EXPORT_SYMBOL(of_phy_find_device); 280 281 /** 282 * of_phy_connect - Connect to the phy described in the device tree 283 * @dev: pointer to net_device claiming the phy 284 * @phy_np: Pointer to device tree node for the PHY 285 * @hndlr: Link state callback for the network device 286 * @flags: flags to pass to the PHY 287 * @iface: PHY data interface type 288 * 289 * If successful, returns a pointer to the phy_device with the embedded 290 * struct device refcount incremented by one, or NULL on failure. The 291 * refcount must be dropped by calling phy_disconnect() or phy_detach(). 292 */ 293 struct phy_device *of_phy_connect(struct net_device *dev, 294 struct device_node *phy_np, 295 void (*hndlr)(struct net_device *), u32 flags, 296 phy_interface_t iface) 297 { 298 struct phy_device *phy = of_phy_find_device(phy_np); 299 int ret; 300 301 if (!phy) 302 return NULL; 303 304 phy->dev_flags |= flags; 305 306 ret = phy_connect_direct(dev, phy, hndlr, iface); 307 308 /* refcount is held by phy_connect_direct() on success */ 309 put_device(&phy->mdio.dev); 310 311 return ret ? NULL : phy; 312 } 313 EXPORT_SYMBOL(of_phy_connect); 314 315 /** 316 * of_phy_get_and_connect 317 * - Get phy node and connect to the phy described in the device tree 318 * @dev: pointer to net_device claiming the phy 319 * @np: Pointer to device tree node for the net_device claiming the phy 320 * @hndlr: Link state callback for the network device 321 * 322 * If successful, returns a pointer to the phy_device with the embedded 323 * struct device refcount incremented by one, or NULL on failure. The 324 * refcount must be dropped by calling phy_disconnect() or phy_detach(). 325 */ 326 struct phy_device *of_phy_get_and_connect(struct net_device *dev, 327 struct device_node *np, 328 void (*hndlr)(struct net_device *)) 329 { 330 phy_interface_t iface; 331 struct device_node *phy_np; 332 struct phy_device *phy; 333 int ret; 334 335 ret = of_get_phy_mode(np, &iface); 336 if (ret) 337 return NULL; 338 if (of_phy_is_fixed_link(np)) { 339 ret = of_phy_register_fixed_link(np); 340 if (ret < 0) { 341 netdev_err(dev, "broken fixed-link specification\n"); 342 return NULL; 343 } 344 phy_np = of_node_get(np); 345 } else { 346 phy_np = of_parse_phandle(np, "phy-handle", 0); 347 if (!phy_np) 348 return NULL; 349 } 350 351 phy = of_phy_connect(dev, phy_np, hndlr, 0, iface); 352 353 of_node_put(phy_np); 354 355 return phy; 356 } 357 EXPORT_SYMBOL(of_phy_get_and_connect); 358 359 /* 360 * of_phy_is_fixed_link() and of_phy_register_fixed_link() must 361 * support two DT bindings: 362 * - the old DT binding, where 'fixed-link' was a property with 5 363 * cells encoding various information about the fixed PHY 364 * - the new DT binding, where 'fixed-link' is a sub-node of the 365 * Ethernet device. 366 */ 367 bool of_phy_is_fixed_link(struct device_node *np) 368 { 369 struct device_node *dn; 370 int len, err; 371 const char *managed; 372 373 /* New binding */ 374 dn = of_get_child_by_name(np, "fixed-link"); 375 if (dn) { 376 of_node_put(dn); 377 return true; 378 } 379 380 err = of_property_read_string(np, "managed", &managed); 381 if (err == 0 && strcmp(managed, "auto") != 0) 382 return true; 383 384 /* Old binding */ 385 if (of_get_property(np, "fixed-link", &len) && 386 len == (5 * sizeof(__be32))) 387 return true; 388 389 return false; 390 } 391 EXPORT_SYMBOL(of_phy_is_fixed_link); 392 393 int of_phy_register_fixed_link(struct device_node *np) 394 { 395 struct fixed_phy_status status = {}; 396 struct device_node *fixed_link_node; 397 u32 fixed_link_prop[5]; 398 const char *managed; 399 400 if (of_property_read_string(np, "managed", &managed) == 0 && 401 strcmp(managed, "in-band-status") == 0) { 402 /* status is zeroed, namely its .link member */ 403 goto register_phy; 404 } 405 406 /* New binding */ 407 fixed_link_node = of_get_child_by_name(np, "fixed-link"); 408 if (fixed_link_node) { 409 status.link = 1; 410 status.duplex = of_property_read_bool(fixed_link_node, 411 "full-duplex"); 412 if (of_property_read_u32(fixed_link_node, "speed", 413 &status.speed)) { 414 of_node_put(fixed_link_node); 415 return -EINVAL; 416 } 417 status.pause = of_property_read_bool(fixed_link_node, "pause"); 418 status.asym_pause = of_property_read_bool(fixed_link_node, 419 "asym-pause"); 420 of_node_put(fixed_link_node); 421 422 goto register_phy; 423 } 424 425 /* Old binding */ 426 if (of_property_read_u32_array(np, "fixed-link", fixed_link_prop, 427 ARRAY_SIZE(fixed_link_prop)) == 0) { 428 status.link = 1; 429 status.duplex = fixed_link_prop[1]; 430 status.speed = fixed_link_prop[2]; 431 status.pause = fixed_link_prop[3]; 432 status.asym_pause = fixed_link_prop[4]; 433 goto register_phy; 434 } 435 436 return -ENODEV; 437 438 register_phy: 439 return PTR_ERR_OR_ZERO(fixed_phy_register(PHY_POLL, &status, np)); 440 } 441 EXPORT_SYMBOL(of_phy_register_fixed_link); 442 443 void of_phy_deregister_fixed_link(struct device_node *np) 444 { 445 struct phy_device *phydev; 446 447 phydev = of_phy_find_device(np); 448 if (!phydev) 449 return; 450 451 fixed_phy_unregister(phydev); 452 453 put_device(&phydev->mdio.dev); /* of_phy_find_device() */ 454 phy_device_free(phydev); /* fixed_phy_register() */ 455 } 456 EXPORT_SYMBOL(of_phy_deregister_fixed_link); 457