of_mdio.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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/phy_fixed.h>
  17. #include <linux/of.h>
  18. #include <linux/of_irq.h>
  19. #include <linux/of_mdio.h>
  20. #include <linux/module.h>
  21. MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>");
  22. MODULE_LICENSE("GPL");
  23. /* Extract the clause 22 phy ID from the compatible string of the form
  24. * ethernet-phy-idAAAA.BBBB */
  25. static int of_get_phy_id(struct device_node *device, u32 *phy_id)
  26. {
  27. struct property *prop;
  28. const char *cp;
  29. unsigned int upper, lower;
  30. of_property_for_each_string(device, "compatible", prop, cp) {
  31. if (sscanf(cp, "ethernet-phy-id%4x.%4x", &upper, &lower) == 2) {
  32. *phy_id = ((upper & 0xFFFF) << 16) | (lower & 0xFFFF);
  33. return 0;
  34. }
  35. }
  36. return -EINVAL;
  37. }
  38. static int of_mdiobus_register_phy(struct mii_bus *mdio, struct device_node *child,
  39. u32 addr)
  40. {
  41. struct phy_device *phy;
  42. bool is_c45;
  43. int rc;
  44. u32 phy_id;
  45. is_c45 = of_device_is_compatible(child,
  46. "ethernet-phy-ieee802.3-c45");
  47. if (!is_c45 && !of_get_phy_id(child, &phy_id))
  48. phy = phy_device_create(mdio, addr, phy_id, 0, NULL);
  49. else
  50. phy = get_phy_device(mdio, addr, is_c45);
  51. if (!phy || IS_ERR(phy))
  52. return 1;
  53. rc = irq_of_parse_and_map(child, 0);
  54. if (rc > 0) {
  55. phy->irq = rc;
  56. if (mdio->irq)
  57. mdio->irq[addr] = rc;
  58. } else {
  59. if (mdio->irq)
  60. phy->irq = mdio->irq[addr];
  61. }
  62. /* Associate the OF node with the device structure so it
  63. * can be looked up later */
  64. of_node_get(child);
  65. phy->dev.of_node = child;
  66. /* All data is now stored in the phy struct;
  67. * register it */
  68. rc = phy_device_register(phy);
  69. if (rc) {
  70. phy_device_free(phy);
  71. of_node_put(child);
  72. return 1;
  73. }
  74. dev_dbg(&mdio->dev, "registered phy %s at address %i\n",
  75. child->name, addr);
  76. return 0;
  77. }
  78. static int of_mdio_parse_addr(struct device *dev, const struct device_node *np)
  79. {
  80. u32 addr;
  81. int ret;
  82. ret = of_property_read_u32(np, "reg", &addr);
  83. if (ret < 0) {
  84. dev_err(dev, "%s has invalid PHY address\n", np->full_name);
  85. return ret;
  86. }
  87. /* A PHY must have a reg property in the range [0-31] */
  88. if (addr >= PHY_MAX_ADDR) {
  89. dev_err(dev, "%s PHY address %i is too large\n",
  90. np->full_name, addr);
  91. return -EINVAL;
  92. }
  93. return addr;
  94. }
  95. /**
  96. * of_mdiobus_register - Register mii_bus and create PHYs from the device tree
  97. * @mdio: pointer to mii_bus structure
  98. * @np: pointer to device_node of MDIO bus.
  99. *
  100. * This function registers the mii_bus structure and registers a phy_device
  101. * for each child node of @np.
  102. */
  103. int of_mdiobus_register(struct mii_bus *mdio, struct device_node *np)
  104. {
  105. struct device_node *child;
  106. const __be32 *paddr;
  107. bool scanphys = false;
  108. int addr, rc, i;
  109. /* Mask out all PHYs from auto probing. Instead the PHYs listed in
  110. * the device tree are populated after the bus has been registered */
  111. mdio->phy_mask = ~0;
  112. /* Clear all the IRQ properties */
  113. if (mdio->irq)
  114. for (i=0; i<PHY_MAX_ADDR; i++)
  115. mdio->irq[i] = PHY_POLL;
  116. mdio->dev.of_node = np;
  117. /* Register the MDIO bus */
  118. rc = mdiobus_register(mdio);
  119. if (rc)
  120. return rc;
  121. /* Loop over the child nodes and register a phy_device for each one */
  122. for_each_available_child_of_node(np, child) {
  123. addr = of_mdio_parse_addr(&mdio->dev, child);
  124. if (addr < 0) {
  125. scanphys = true;
  126. continue;
  127. }
  128. rc = of_mdiobus_register_phy(mdio, child, addr);
  129. if (rc)
  130. continue;
  131. }
  132. if (!scanphys)
  133. return 0;
  134. /* auto scan for PHYs with empty reg property */
  135. for_each_available_child_of_node(np, child) {
  136. /* Skip PHYs with reg property set */
  137. paddr = of_get_property(child, "reg", NULL);
  138. if (paddr)
  139. continue;
  140. for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
  141. /* skip already registered PHYs */
  142. if (mdio->phy_map[addr])
  143. continue;
  144. /* be noisy to encourage people to set reg property */
  145. dev_info(&mdio->dev, "scan phy %s at address %i\n",
  146. child->name, addr);
  147. rc = of_mdiobus_register_phy(mdio, child, addr);
  148. if (rc)
  149. continue;
  150. }
  151. }
  152. return 0;
  153. }
  154. EXPORT_SYMBOL(of_mdiobus_register);
  155. /* Helper function for of_phy_find_device */
  156. static int of_phy_match(struct device *dev, void *phy_np)
  157. {
  158. return dev->of_node == phy_np;
  159. }
  160. /**
  161. * of_phy_find_device - Give a PHY node, find the phy_device
  162. * @phy_np: Pointer to the phy's device tree node
  163. *
  164. * Returns a pointer to the phy_device.
  165. */
  166. struct phy_device *of_phy_find_device(struct device_node *phy_np)
  167. {
  168. struct device *d;
  169. if (!phy_np)
  170. return NULL;
  171. d = bus_find_device(&mdio_bus_type, NULL, phy_np, of_phy_match);
  172. return d ? to_phy_device(d) : NULL;
  173. }
  174. EXPORT_SYMBOL(of_phy_find_device);
  175. /**
  176. * of_phy_connect - Connect to the phy described in the device tree
  177. * @dev: pointer to net_device claiming the phy
  178. * @phy_np: Pointer to device tree node for the PHY
  179. * @hndlr: Link state callback for the network device
  180. * @iface: PHY data interface type
  181. *
  182. * Returns a pointer to the phy_device if successful. NULL otherwise
  183. */
  184. struct phy_device *of_phy_connect(struct net_device *dev,
  185. struct device_node *phy_np,
  186. void (*hndlr)(struct net_device *), u32 flags,
  187. phy_interface_t iface)
  188. {
  189. struct phy_device *phy = of_phy_find_device(phy_np);
  190. if (!phy)
  191. return NULL;
  192. phy->dev_flags = flags;
  193. return phy_connect_direct(dev, phy, hndlr, iface) ? NULL : phy;
  194. }
  195. EXPORT_SYMBOL(of_phy_connect);
  196. /**
  197. * of_phy_attach - Attach to a PHY without starting the state machine
  198. * @dev: pointer to net_device claiming the phy
  199. * @phy_np: Node pointer for the PHY
  200. * @flags: flags to pass to the PHY
  201. * @iface: PHY data interface type
  202. */
  203. struct phy_device *of_phy_attach(struct net_device *dev,
  204. struct device_node *phy_np, u32 flags,
  205. phy_interface_t iface)
  206. {
  207. struct phy_device *phy = of_phy_find_device(phy_np);
  208. if (!phy)
  209. return NULL;
  210. return phy_attach_direct(dev, phy, flags, iface) ? NULL : phy;
  211. }
  212. EXPORT_SYMBOL(of_phy_attach);
  213. #if defined(CONFIG_FIXED_PHY)
  214. /*
  215. * of_phy_is_fixed_link() and of_phy_register_fixed_link() must
  216. * support two DT bindings:
  217. * - the old DT binding, where 'fixed-link' was a property with 5
  218. * cells encoding various informations about the fixed PHY
  219. * - the new DT binding, where 'fixed-link' is a sub-node of the
  220. * Ethernet device.
  221. */
  222. bool of_phy_is_fixed_link(struct device_node *np)
  223. {
  224. struct device_node *dn;
  225. int len;
  226. /* New binding */
  227. dn = of_get_child_by_name(np, "fixed-link");
  228. if (dn) {
  229. of_node_put(dn);
  230. return true;
  231. }
  232. /* Old binding */
  233. if (of_get_property(np, "fixed-link", &len) &&
  234. len == (5 * sizeof(__be32)))
  235. return true;
  236. return false;
  237. }
  238. EXPORT_SYMBOL(of_phy_is_fixed_link);
  239. int of_phy_register_fixed_link(struct device_node *np)
  240. {
  241. struct fixed_phy_status status = {};
  242. struct device_node *fixed_link_node;
  243. const __be32 *fixed_link_prop;
  244. int len;
  245. struct phy_device *phy;
  246. /* New binding */
  247. fixed_link_node = of_get_child_by_name(np, "fixed-link");
  248. if (fixed_link_node) {
  249. status.link = 1;
  250. status.duplex = of_property_read_bool(fixed_link_node,
  251. "full-duplex");
  252. if (of_property_read_u32(fixed_link_node, "speed", &status.speed))
  253. return -EINVAL;
  254. status.pause = of_property_read_bool(fixed_link_node, "pause");
  255. status.asym_pause = of_property_read_bool(fixed_link_node,
  256. "asym-pause");
  257. of_node_put(fixed_link_node);
  258. phy = fixed_phy_register(PHY_POLL, &status, np);
  259. return IS_ERR(phy) ? PTR_ERR(phy) : 0;
  260. }
  261. /* Old binding */
  262. fixed_link_prop = of_get_property(np, "fixed-link", &len);
  263. if (fixed_link_prop && len == (5 * sizeof(__be32))) {
  264. status.link = 1;
  265. status.duplex = be32_to_cpu(fixed_link_prop[1]);
  266. status.speed = be32_to_cpu(fixed_link_prop[2]);
  267. status.pause = be32_to_cpu(fixed_link_prop[3]);
  268. status.asym_pause = be32_to_cpu(fixed_link_prop[4]);
  269. phy = fixed_phy_register(PHY_POLL, &status, np);
  270. return IS_ERR(phy) ? PTR_ERR(phy) : 0;
  271. }
  272. return -ENODEV;
  273. }
  274. EXPORT_SYMBOL(of_phy_register_fixed_link);
  275. #endif