of_mdio.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. /* Helper function for of_phy_find_device */
  86. static int of_phy_match(struct device *dev, void *phy_np)
  87. {
  88. return dev_archdata_get_node(&dev->archdata) == phy_np;
  89. }
  90. /**
  91. * of_phy_find_device - Give a PHY node, find the phy_device
  92. * @phy_np: Pointer to the phy's device tree node
  93. *
  94. * Returns a pointer to the phy_device.
  95. */
  96. struct phy_device *of_phy_find_device(struct device_node *phy_np)
  97. {
  98. struct device *d;
  99. if (!phy_np)
  100. return NULL;
  101. d = bus_find_device(&mdio_bus_type, NULL, phy_np, of_phy_match);
  102. return d ? to_phy_device(d) : NULL;
  103. }
  104. EXPORT_SYMBOL(of_phy_find_device);
  105. /**
  106. * of_phy_connect - Connect to the phy described in the device tree
  107. * @dev: pointer to net_device claiming the phy
  108. * @phy_np: Pointer to device tree node for the PHY
  109. * @hndlr: Link state callback for the network device
  110. * @iface: PHY data interface type
  111. *
  112. * Returns a pointer to the phy_device if successfull. NULL otherwise
  113. */
  114. struct phy_device *of_phy_connect(struct net_device *dev,
  115. struct device_node *phy_np,
  116. void (*hndlr)(struct net_device *), u32 flags,
  117. phy_interface_t iface)
  118. {
  119. struct phy_device *phy = of_phy_find_device(phy_np);
  120. if (!phy)
  121. return NULL;
  122. return phy_connect_direct(dev, phy, hndlr, flags, iface) ? NULL : phy;
  123. }
  124. EXPORT_SYMBOL(of_phy_connect);
  125. /**
  126. * of_phy_connect_fixed_link - Parse fixed-link property and return a dummy phy
  127. * @dev: pointer to net_device claiming the phy
  128. * @hndlr: Link state callback for the network device
  129. * @iface: PHY data interface type
  130. *
  131. * This function is a temporary stop-gap and will be removed soon. It is
  132. * only to support the fs_enet, ucc_geth and gianfar Ethernet drivers. Do
  133. * not call this function from new drivers.
  134. */
  135. struct phy_device *of_phy_connect_fixed_link(struct net_device *dev,
  136. void (*hndlr)(struct net_device *),
  137. phy_interface_t iface)
  138. {
  139. struct device_node *net_np;
  140. char bus_id[MII_BUS_ID_SIZE + 3];
  141. struct phy_device *phy;
  142. const u32 *phy_id;
  143. int sz;
  144. if (!dev->dev.parent)
  145. return NULL;
  146. net_np = dev_archdata_get_node(&dev->dev.parent->archdata);
  147. if (!net_np)
  148. return NULL;
  149. phy_id = of_get_property(net_np, "fixed-link", &sz);
  150. if (!phy_id || sz < sizeof(*phy_id))
  151. return NULL;
  152. sprintf(bus_id, PHY_ID_FMT, "0", phy_id[0]);
  153. phy = phy_connect(dev, bus_id, hndlr, 0, iface);
  154. return IS_ERR(phy) ? NULL : phy;
  155. }
  156. EXPORT_SYMBOL(of_phy_connect_fixed_link);