of_mdio.c 5.0 KB

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