of_mdio.c 7.7 KB

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