of_mdio.c 7.1 KB

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