of_mdio.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * OF helpers for the MDIO (Ethernet PHY) API
  3. *
  4. * Copyright (c) 2009 Secret Lab Technologies, Ltd.
  5. *
  6. * This file is released under the GPLv2
  7. *
  8. * This file provides helper functions for extracting PHY device information
  9. * out of the OpenFirmware device tree and using it to populate an mii_bus.
  10. */
  11. #include <linux/phy.h>
  12. #include <linux/of.h>
  13. #include <linux/of_mdio.h>
  14. #include <linux/module.h>
  15. MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>");
  16. MODULE_LICENSE("GPL");
  17. /**
  18. * of_mdiobus_register - Register mii_bus and create PHYs from the device tree
  19. * @mdio: pointer to mii_bus structure
  20. * @np: pointer to device_node of MDIO bus.
  21. *
  22. * This function registers the mii_bus structure and registers a phy_device
  23. * for each child node of @np.
  24. */
  25. int of_mdiobus_register(struct mii_bus *mdio, struct device_node *np)
  26. {
  27. struct phy_device *phy;
  28. struct device_node *child;
  29. int rc, i;
  30. /* Mask out all PHYs from auto probing. Instead the PHYs listed in
  31. * the device tree are populated after the bus has been registered */
  32. mdio->phy_mask = ~0;
  33. /* Clear all the IRQ properties */
  34. if (mdio->irq)
  35. for (i=0; i<PHY_MAX_ADDR; i++)
  36. mdio->irq[i] = PHY_POLL;
  37. /* Register the MDIO bus */
  38. rc = mdiobus_register(mdio);
  39. if (rc)
  40. return rc;
  41. /* Loop over the child nodes and register a phy_device for each one */
  42. for_each_child_of_node(np, child) {
  43. const u32 *addr;
  44. int len;
  45. /* A PHY must have a reg property in the range [0-31] */
  46. addr = of_get_property(child, "reg", &len);
  47. if (!addr || len < sizeof(*addr) || *addr >= 32 || *addr < 0) {
  48. dev_err(&mdio->dev, "%s has invalid PHY address\n",
  49. child->full_name);
  50. continue;
  51. }
  52. if (mdio->irq) {
  53. mdio->irq[*addr] = irq_of_parse_and_map(child, 0);
  54. if (!mdio->irq[*addr])
  55. mdio->irq[*addr] = PHY_POLL;
  56. }
  57. phy = get_phy_device(mdio, *addr);
  58. if (!phy) {
  59. dev_err(&mdio->dev, "error probing PHY at address %i\n",
  60. *addr);
  61. continue;
  62. }
  63. phy_scan_fixups(phy);
  64. /* Associate the OF node with the device structure so it
  65. * can be looked up later */
  66. of_node_get(child);
  67. dev_archdata_set_node(&phy->dev.archdata, child);
  68. /* All data is now stored in the phy struct; register it */
  69. rc = phy_device_register(phy);
  70. if (rc) {
  71. phy_device_free(phy);
  72. of_node_put(child);
  73. continue;
  74. }
  75. dev_dbg(&mdio->dev, "registered phy %s at address %i\n",
  76. child->name, *addr);
  77. }
  78. return 0;
  79. }
  80. EXPORT_SYMBOL(of_mdiobus_register);
  81. /**
  82. * of_phy_find_device - Give a PHY node, find the phy_device
  83. * @phy_np: Pointer to the phy's device tree node
  84. *
  85. * Returns a pointer to the phy_device.
  86. */
  87. struct phy_device *of_phy_find_device(struct device_node *phy_np)
  88. {
  89. struct device *d;
  90. int match(struct device *dev, void *phy_np)
  91. {
  92. return dev_archdata_get_node(&dev->archdata) == phy_np;
  93. }
  94. if (!phy_np)
  95. return NULL;
  96. d = bus_find_device(&mdio_bus_type, NULL, phy_np, match);
  97. return d ? to_phy_device(d) : NULL;
  98. }
  99. EXPORT_SYMBOL(of_phy_find_device);
  100. /**
  101. * of_phy_connect - Connect to the phy described in the device tree
  102. * @dev: pointer to net_device claiming the phy
  103. * @phy_np: Pointer to device tree node for the PHY
  104. * @hndlr: Link state callback for the network device
  105. * @iface: PHY data interface type
  106. *
  107. * Returns a pointer to the phy_device if successfull. NULL otherwise
  108. */
  109. struct phy_device *of_phy_connect(struct net_device *dev,
  110. struct device_node *phy_np,
  111. void (*hndlr)(struct net_device *), u32 flags,
  112. phy_interface_t iface)
  113. {
  114. struct phy_device *phy = of_phy_find_device(phy_np);
  115. if (!phy)
  116. return NULL;
  117. return phy_connect_direct(dev, phy, hndlr, flags, iface) ? NULL : phy;
  118. }
  119. EXPORT_SYMBOL(of_phy_connect);