of_mdio.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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/kernel.h>
  12. #include <linux/device.h>
  13. #include <linux/netdevice.h>
  14. #include <linux/err.h>
  15. #include <linux/phy.h>
  16. #include <linux/of.h>
  17. #include <linux/of_mdio.h>
  18. #include <linux/module.h>
  19. MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>");
  20. MODULE_LICENSE("GPL");
  21. /**
  22. * of_mdiobus_register - Register mii_bus and create PHYs from the device tree
  23. * @mdio: pointer to mii_bus structure
  24. * @np: pointer to device_node of MDIO bus.
  25. *
  26. * This function registers the mii_bus structure and registers a phy_device
  27. * for each child node of @np.
  28. */
  29. int of_mdiobus_register(struct mii_bus *mdio, struct device_node *np)
  30. {
  31. struct phy_device *phy;
  32. struct device_node *child;
  33. int rc, i;
  34. /* Mask out all PHYs from auto probing. Instead the PHYs listed in
  35. * the device tree are populated after the bus has been registered */
  36. mdio->phy_mask = ~0;
  37. /* Clear all the IRQ properties */
  38. if (mdio->irq)
  39. for (i=0; i<PHY_MAX_ADDR; i++)
  40. mdio->irq[i] = PHY_POLL;
  41. /* Register the MDIO bus */
  42. rc = mdiobus_register(mdio);
  43. if (rc)
  44. return rc;
  45. /* Loop over the child nodes and register a phy_device for each one */
  46. for_each_child_of_node(np, child) {
  47. const u32 *addr;
  48. int len;
  49. /* A PHY must have a reg property in the range [0-31] */
  50. addr = of_get_property(child, "reg", &len);
  51. if (!addr || len < sizeof(*addr) || *addr >= 32 || *addr < 0) {
  52. dev_err(&mdio->dev, "%s has invalid PHY address\n",
  53. child->full_name);
  54. continue;
  55. }
  56. if (mdio->irq) {
  57. mdio->irq[*addr] = irq_of_parse_and_map(child, 0);
  58. if (!mdio->irq[*addr])
  59. mdio->irq[*addr] = PHY_POLL;
  60. }
  61. phy = get_phy_device(mdio, *addr);
  62. if (!phy) {
  63. dev_err(&mdio->dev, "error probing PHY at address %i\n",
  64. *addr);
  65. continue;
  66. }
  67. phy_scan_fixups(phy);
  68. /* Associate the OF node with the device structure so it
  69. * can be looked up later */
  70. of_node_get(child);
  71. dev_archdata_set_node(&phy->dev.archdata, child);
  72. /* All data is now stored in the phy struct; register it */
  73. rc = phy_device_register(phy);
  74. if (rc) {
  75. phy_device_free(phy);
  76. of_node_put(child);
  77. continue;
  78. }
  79. dev_dbg(&mdio->dev, "registered phy %s at address %i\n",
  80. child->name, *addr);
  81. }
  82. return 0;
  83. }
  84. EXPORT_SYMBOL(of_mdiobus_register);
  85. /**
  86. * of_phy_find_device - Give a PHY node, find the phy_device
  87. * @phy_np: Pointer to the phy's device tree node
  88. *
  89. * Returns a pointer to the phy_device.
  90. */
  91. struct phy_device *of_phy_find_device(struct device_node *phy_np)
  92. {
  93. struct device *d;
  94. int match(struct device *dev, void *phy_np)
  95. {
  96. return dev_archdata_get_node(&dev->archdata) == phy_np;
  97. }
  98. if (!phy_np)
  99. return NULL;
  100. d = bus_find_device(&mdio_bus_type, NULL, phy_np, match);
  101. return d ? to_phy_device(d) : NULL;
  102. }
  103. EXPORT_SYMBOL(of_phy_find_device);
  104. /**
  105. * of_phy_connect - Connect to the phy described in the device tree
  106. * @dev: pointer to net_device claiming the phy
  107. * @phy_np: Pointer to device tree node for the PHY
  108. * @hndlr: Link state callback for the network device
  109. * @iface: PHY data interface type
  110. *
  111. * Returns a pointer to the phy_device if successfull. NULL otherwise
  112. */
  113. struct phy_device *of_phy_connect(struct net_device *dev,
  114. struct device_node *phy_np,
  115. void (*hndlr)(struct net_device *), u32 flags,
  116. phy_interface_t iface)
  117. {
  118. struct phy_device *phy = of_phy_find_device(phy_np);
  119. if (!phy)
  120. return NULL;
  121. return phy_connect_direct(dev, phy, hndlr, flags, iface) ? NULL : phy;
  122. }
  123. EXPORT_SYMBOL(of_phy_connect);
  124. /**
  125. * of_phy_connect_fixed_link - Parse fixed-link property and return a dummy phy
  126. * @dev: pointer to net_device claiming the phy
  127. * @hndlr: Link state callback for the network device
  128. * @iface: PHY data interface type
  129. *
  130. * This function is a temporary stop-gap and will be removed soon. It is
  131. * only to support the fs_enet, ucc_geth and gianfar Ethernet drivers. Do
  132. * not call this function from new drivers.
  133. */
  134. struct phy_device *of_phy_connect_fixed_link(struct net_device *dev,
  135. void (*hndlr)(struct net_device *),
  136. phy_interface_t iface)
  137. {
  138. struct device_node *net_np;
  139. char bus_id[MII_BUS_ID_SIZE + 3];
  140. struct phy_device *phy;
  141. const u32 *phy_id;
  142. int sz;
  143. if (!dev->dev.parent)
  144. return NULL;
  145. net_np = dev_archdata_get_node(&dev->dev.parent->archdata);
  146. if (!net_np)
  147. return NULL;
  148. phy_id = of_get_property(net_np, "fixed-link", &sz);
  149. if (!phy_id || sz < sizeof(*phy_id))
  150. return NULL;
  151. sprintf(bus_id, PHY_ID_FMT, "0", phy_id[0]);
  152. phy = phy_connect(dev, bus_id, hndlr, 0, iface);
  153. return IS_ERR(phy) ? NULL : phy;
  154. }
  155. EXPORT_SYMBOL(of_phy_connect_fixed_link);