of_mdio.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. const __be32 *paddr;
  35. u32 addr;
  36. bool is_c45, scanphys = false;
  37. int rc, i, len;
  38. /* Mask out all PHYs from auto probing. Instead the PHYs listed in
  39. * the device tree are populated after the bus has been registered */
  40. mdio->phy_mask = ~0;
  41. /* Clear all the IRQ properties */
  42. if (mdio->irq)
  43. for (i=0; i<PHY_MAX_ADDR; i++)
  44. mdio->irq[i] = PHY_POLL;
  45. mdio->dev.of_node = np;
  46. /* Register the MDIO bus */
  47. rc = mdiobus_register(mdio);
  48. if (rc)
  49. return rc;
  50. /* Loop over the child nodes and register a phy_device for each one */
  51. for_each_available_child_of_node(np, child) {
  52. /* A PHY must have a reg property in the range [0-31] */
  53. paddr = of_get_property(child, "reg", &len);
  54. if (!paddr || len < sizeof(*paddr)) {
  55. scanphys = true;
  56. dev_err(&mdio->dev, "%s has invalid PHY address\n",
  57. child->full_name);
  58. continue;
  59. }
  60. addr = be32_to_cpup(paddr);
  61. if (addr >= 32) {
  62. dev_err(&mdio->dev, "%s PHY address %i is too large\n",
  63. child->full_name, addr);
  64. continue;
  65. }
  66. if (mdio->irq) {
  67. mdio->irq[addr] = irq_of_parse_and_map(child, 0);
  68. if (!mdio->irq[addr])
  69. mdio->irq[addr] = PHY_POLL;
  70. }
  71. is_c45 = of_device_is_compatible(child,
  72. "ethernet-phy-ieee802.3-c45");
  73. phy = get_phy_device(mdio, addr, is_c45);
  74. if (!phy || IS_ERR(phy)) {
  75. dev_err(&mdio->dev,
  76. "cannot get PHY at address %i\n",
  77. addr);
  78. continue;
  79. }
  80. /* Associate the OF node with the device structure so it
  81. * can be looked up later */
  82. of_node_get(child);
  83. phy->dev.of_node = child;
  84. /* All data is now stored in the phy struct; register it */
  85. rc = phy_device_register(phy);
  86. if (rc) {
  87. phy_device_free(phy);
  88. of_node_put(child);
  89. continue;
  90. }
  91. dev_dbg(&mdio->dev, "registered phy %s at address %i\n",
  92. child->name, addr);
  93. }
  94. if (!scanphys)
  95. return 0;
  96. /* auto scan for PHYs with empty reg property */
  97. for_each_available_child_of_node(np, child) {
  98. /* Skip PHYs with reg property set */
  99. paddr = of_get_property(child, "reg", &len);
  100. if (paddr)
  101. continue;
  102. is_c45 = of_device_is_compatible(child,
  103. "ethernet-phy-ieee802.3-c45");
  104. for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
  105. /* skip already registered PHYs */
  106. if (mdio->phy_map[addr])
  107. continue;
  108. /* be noisy to encourage people to set reg property */
  109. dev_info(&mdio->dev, "scan phy %s at address %i\n",
  110. child->name, addr);
  111. phy = get_phy_device(mdio, addr, is_c45);
  112. if (!phy || IS_ERR(phy))
  113. continue;
  114. if (mdio->irq) {
  115. mdio->irq[addr] =
  116. irq_of_parse_and_map(child, 0);
  117. if (!mdio->irq[addr])
  118. mdio->irq[addr] = PHY_POLL;
  119. }
  120. /* Associate the OF node with the device structure so it
  121. * can be looked up later */
  122. of_node_get(child);
  123. phy->dev.of_node = child;
  124. /* All data is now stored in the phy struct;
  125. * register it */
  126. rc = phy_device_register(phy);
  127. if (rc) {
  128. phy_device_free(phy);
  129. of_node_put(child);
  130. continue;
  131. }
  132. dev_info(&mdio->dev, "registered phy %s at address %i\n",
  133. child->name, addr);
  134. break;
  135. }
  136. }
  137. return 0;
  138. }
  139. EXPORT_SYMBOL(of_mdiobus_register);
  140. /* Helper function for of_phy_find_device */
  141. static int of_phy_match(struct device *dev, void *phy_np)
  142. {
  143. return dev->of_node == phy_np;
  144. }
  145. /**
  146. * of_phy_find_device - Give a PHY node, find the phy_device
  147. * @phy_np: Pointer to the phy's device tree node
  148. *
  149. * Returns a pointer to the phy_device.
  150. */
  151. struct phy_device *of_phy_find_device(struct device_node *phy_np)
  152. {
  153. struct device *d;
  154. if (!phy_np)
  155. return NULL;
  156. d = bus_find_device(&mdio_bus_type, NULL, phy_np, of_phy_match);
  157. return d ? to_phy_device(d) : NULL;
  158. }
  159. EXPORT_SYMBOL(of_phy_find_device);
  160. /**
  161. * of_phy_connect - Connect to the phy described in the device tree
  162. * @dev: pointer to net_device claiming the phy
  163. * @phy_np: Pointer to device tree node for the PHY
  164. * @hndlr: Link state callback for the network device
  165. * @iface: PHY data interface type
  166. *
  167. * Returns a pointer to the phy_device if successful. NULL otherwise
  168. */
  169. struct phy_device *of_phy_connect(struct net_device *dev,
  170. struct device_node *phy_np,
  171. void (*hndlr)(struct net_device *), u32 flags,
  172. phy_interface_t iface)
  173. {
  174. struct phy_device *phy = of_phy_find_device(phy_np);
  175. if (!phy)
  176. return NULL;
  177. return phy_connect_direct(dev, phy, hndlr, iface) ? NULL : phy;
  178. }
  179. EXPORT_SYMBOL(of_phy_connect);
  180. /**
  181. * of_phy_connect_fixed_link - Parse fixed-link property and return a dummy phy
  182. * @dev: pointer to net_device claiming the phy
  183. * @hndlr: Link state callback for the network device
  184. * @iface: PHY data interface type
  185. *
  186. * This function is a temporary stop-gap and will be removed soon. It is
  187. * only to support the fs_enet, ucc_geth and gianfar Ethernet drivers. Do
  188. * not call this function from new drivers.
  189. */
  190. struct phy_device *of_phy_connect_fixed_link(struct net_device *dev,
  191. void (*hndlr)(struct net_device *),
  192. phy_interface_t iface)
  193. {
  194. struct device_node *net_np;
  195. char bus_id[MII_BUS_ID_SIZE + 3];
  196. struct phy_device *phy;
  197. const __be32 *phy_id;
  198. int sz;
  199. if (!dev->dev.parent)
  200. return NULL;
  201. net_np = dev->dev.parent->of_node;
  202. if (!net_np)
  203. return NULL;
  204. phy_id = of_get_property(net_np, "fixed-link", &sz);
  205. if (!phy_id || sz < sizeof(*phy_id))
  206. return NULL;
  207. sprintf(bus_id, PHY_ID_FMT, "fixed-0", be32_to_cpu(phy_id[0]));
  208. phy = phy_connect(dev, bus_id, hndlr, iface);
  209. return IS_ERR(phy) ? NULL : phy;
  210. }
  211. EXPORT_SYMBOL(of_phy_connect_fixed_link);