of_mdio.c 6.4 KB

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