of_mdio.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. phy = phy_device_create(mdio, addr, 0, false, NULL);
  76. if (!phy || IS_ERR(phy)) {
  77. dev_err(&mdio->dev,
  78. "error creating PHY at address %i\n",
  79. addr);
  80. continue;
  81. }
  82. }
  83. /* Associate the OF node with the device structure so it
  84. * can be looked up later */
  85. of_node_get(child);
  86. phy->dev.of_node = child;
  87. /* All data is now stored in the phy struct; register it */
  88. rc = phy_device_register(phy);
  89. if (rc) {
  90. phy_device_free(phy);
  91. of_node_put(child);
  92. continue;
  93. }
  94. dev_dbg(&mdio->dev, "registered phy %s at address %i\n",
  95. child->name, addr);
  96. }
  97. if (!scanphys)
  98. return 0;
  99. /* auto scan for PHYs with empty reg property */
  100. for_each_available_child_of_node(np, child) {
  101. /* Skip PHYs with reg property set */
  102. paddr = of_get_property(child, "reg", &len);
  103. if (paddr)
  104. continue;
  105. is_c45 = of_device_is_compatible(child,
  106. "ethernet-phy-ieee802.3-c45");
  107. for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
  108. /* skip already registered PHYs */
  109. if (mdio->phy_map[addr])
  110. continue;
  111. /* be noisy to encourage people to set reg property */
  112. dev_info(&mdio->dev, "scan phy %s at address %i\n",
  113. child->name, addr);
  114. phy = get_phy_device(mdio, addr, is_c45);
  115. if (!phy || IS_ERR(phy))
  116. continue;
  117. if (mdio->irq) {
  118. mdio->irq[addr] =
  119. irq_of_parse_and_map(child, 0);
  120. if (!mdio->irq[addr])
  121. mdio->irq[addr] = PHY_POLL;
  122. }
  123. /* Associate the OF node with the device structure so it
  124. * can be looked up later */
  125. of_node_get(child);
  126. phy->dev.of_node = child;
  127. /* All data is now stored in the phy struct;
  128. * register it */
  129. rc = phy_device_register(phy);
  130. if (rc) {
  131. phy_device_free(phy);
  132. of_node_put(child);
  133. continue;
  134. }
  135. dev_info(&mdio->dev, "registered phy %s at address %i\n",
  136. child->name, addr);
  137. break;
  138. }
  139. }
  140. return 0;
  141. }
  142. EXPORT_SYMBOL(of_mdiobus_register);
  143. /* Helper function for of_phy_find_device */
  144. static int of_phy_match(struct device *dev, void *phy_np)
  145. {
  146. return dev->of_node == phy_np;
  147. }
  148. /**
  149. * of_phy_find_device - Give a PHY node, find the phy_device
  150. * @phy_np: Pointer to the phy's device tree node
  151. *
  152. * Returns a pointer to the phy_device.
  153. */
  154. struct phy_device *of_phy_find_device(struct device_node *phy_np)
  155. {
  156. struct device *d;
  157. if (!phy_np)
  158. return NULL;
  159. d = bus_find_device(&mdio_bus_type, NULL, phy_np, of_phy_match);
  160. return d ? to_phy_device(d) : NULL;
  161. }
  162. EXPORT_SYMBOL(of_phy_find_device);
  163. /**
  164. * of_phy_connect - Connect to the phy described in the device tree
  165. * @dev: pointer to net_device claiming the phy
  166. * @phy_np: Pointer to device tree node for the PHY
  167. * @hndlr: Link state callback for the network device
  168. * @iface: PHY data interface type
  169. *
  170. * Returns a pointer to the phy_device if successful. NULL otherwise
  171. */
  172. struct phy_device *of_phy_connect(struct net_device *dev,
  173. struct device_node *phy_np,
  174. void (*hndlr)(struct net_device *), u32 flags,
  175. phy_interface_t iface)
  176. {
  177. struct phy_device *phy = of_phy_find_device(phy_np);
  178. if (!phy)
  179. return NULL;
  180. return phy_connect_direct(dev, phy, hndlr, iface) ? NULL : phy;
  181. }
  182. EXPORT_SYMBOL(of_phy_connect);
  183. /**
  184. * of_phy_connect_fixed_link - Parse fixed-link property and return a dummy phy
  185. * @dev: pointer to net_device claiming the phy
  186. * @hndlr: Link state callback for the network device
  187. * @iface: PHY data interface type
  188. *
  189. * This function is a temporary stop-gap and will be removed soon. It is
  190. * only to support the fs_enet, ucc_geth and gianfar Ethernet drivers. Do
  191. * not call this function from new drivers.
  192. */
  193. struct phy_device *of_phy_connect_fixed_link(struct net_device *dev,
  194. void (*hndlr)(struct net_device *),
  195. phy_interface_t iface)
  196. {
  197. struct device_node *net_np;
  198. char bus_id[MII_BUS_ID_SIZE + 3];
  199. struct phy_device *phy;
  200. const __be32 *phy_id;
  201. int sz;
  202. if (!dev->dev.parent)
  203. return NULL;
  204. net_np = dev->dev.parent->of_node;
  205. if (!net_np)
  206. return NULL;
  207. phy_id = of_get_property(net_np, "fixed-link", &sz);
  208. if (!phy_id || sz < sizeof(*phy_id))
  209. return NULL;
  210. sprintf(bus_id, PHY_ID_FMT, "fixed-0", be32_to_cpu(phy_id[0]));
  211. phy = phy_connect(dev, bus_id, hndlr, iface);
  212. return IS_ERR(phy) ? NULL : phy;
  213. }
  214. EXPORT_SYMBOL(of_phy_connect_fixed_link);