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