of_mdio.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. 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. EXPORT_SYMBOL(of_mdio_parse_addr);
  96. /**
  97. * of_mdiobus_register - Register mii_bus and create PHYs from the device tree
  98. * @mdio: pointer to mii_bus structure
  99. * @np: pointer to device_node of MDIO bus.
  100. *
  101. * This function registers the mii_bus structure and registers a phy_device
  102. * for each child node of @np.
  103. */
  104. int of_mdiobus_register(struct mii_bus *mdio, struct device_node *np)
  105. {
  106. struct device_node *child;
  107. const __be32 *paddr;
  108. bool scanphys = false;
  109. int addr, rc, i;
  110. /* Mask out all PHYs from auto probing. Instead the PHYs listed in
  111. * the device tree are populated after the bus has been registered */
  112. mdio->phy_mask = ~0;
  113. /* Clear all the IRQ properties */
  114. if (mdio->irq)
  115. for (i=0; i<PHY_MAX_ADDR; i++)
  116. mdio->irq[i] = PHY_POLL;
  117. mdio->dev.of_node = np;
  118. /* Register the MDIO bus */
  119. rc = mdiobus_register(mdio);
  120. if (rc)
  121. return rc;
  122. /* Loop over the child nodes and register a phy_device for each one */
  123. for_each_available_child_of_node(np, child) {
  124. addr = of_mdio_parse_addr(&mdio->dev, child);
  125. if (addr < 0) {
  126. scanphys = true;
  127. continue;
  128. }
  129. rc = of_mdiobus_register_phy(mdio, child, addr);
  130. if (rc)
  131. continue;
  132. }
  133. if (!scanphys)
  134. return 0;
  135. /* auto scan for PHYs with empty reg property */
  136. for_each_available_child_of_node(np, child) {
  137. /* Skip PHYs with reg property set */
  138. paddr = of_get_property(child, "reg", NULL);
  139. if (paddr)
  140. continue;
  141. for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
  142. /* skip already registered PHYs */
  143. if (mdio->phy_map[addr])
  144. continue;
  145. /* be noisy to encourage people to set reg property */
  146. dev_info(&mdio->dev, "scan phy %s at address %i\n",
  147. child->name, addr);
  148. rc = of_mdiobus_register_phy(mdio, child, addr);
  149. if (rc)
  150. continue;
  151. }
  152. }
  153. return 0;
  154. }
  155. EXPORT_SYMBOL(of_mdiobus_register);
  156. /* Helper function for of_phy_find_device */
  157. static int of_phy_match(struct device *dev, void *phy_np)
  158. {
  159. return dev->of_node == phy_np;
  160. }
  161. /**
  162. * of_phy_find_device - Give a PHY node, find the phy_device
  163. * @phy_np: Pointer to the phy's device tree node
  164. *
  165. * Returns a pointer to the phy_device.
  166. */
  167. struct phy_device *of_phy_find_device(struct device_node *phy_np)
  168. {
  169. struct device *d;
  170. if (!phy_np)
  171. return NULL;
  172. d = bus_find_device(&mdio_bus_type, NULL, phy_np, of_phy_match);
  173. return d ? to_phy_device(d) : NULL;
  174. }
  175. EXPORT_SYMBOL(of_phy_find_device);
  176. /**
  177. * of_phy_connect - Connect to the phy described in the device tree
  178. * @dev: pointer to net_device claiming the phy
  179. * @phy_np: Pointer to device tree node for the PHY
  180. * @hndlr: Link state callback for the network device
  181. * @iface: PHY data interface type
  182. *
  183. * Returns a pointer to the phy_device if successful. NULL otherwise
  184. */
  185. struct phy_device *of_phy_connect(struct net_device *dev,
  186. struct device_node *phy_np,
  187. void (*hndlr)(struct net_device *), u32 flags,
  188. phy_interface_t iface)
  189. {
  190. struct phy_device *phy = of_phy_find_device(phy_np);
  191. if (!phy)
  192. return NULL;
  193. phy->dev_flags = flags;
  194. return phy_connect_direct(dev, phy, hndlr, iface) ? NULL : phy;
  195. }
  196. EXPORT_SYMBOL(of_phy_connect);
  197. /**
  198. * of_phy_attach - Attach to a PHY without starting the state machine
  199. * @dev: pointer to net_device claiming the phy
  200. * @phy_np: Node pointer for the PHY
  201. * @flags: flags to pass to the PHY
  202. * @iface: PHY data interface type
  203. */
  204. struct phy_device *of_phy_attach(struct net_device *dev,
  205. struct device_node *phy_np, u32 flags,
  206. phy_interface_t iface)
  207. {
  208. struct phy_device *phy = of_phy_find_device(phy_np);
  209. if (!phy)
  210. return NULL;
  211. return phy_attach_direct(dev, phy, flags, iface) ? NULL : phy;
  212. }
  213. EXPORT_SYMBOL(of_phy_attach);
  214. #if defined(CONFIG_FIXED_PHY)
  215. /*
  216. * of_phy_is_fixed_link() and of_phy_register_fixed_link() must
  217. * support two DT bindings:
  218. * - the old DT binding, where 'fixed-link' was a property with 5
  219. * cells encoding various informations about the fixed PHY
  220. * - the new DT binding, where 'fixed-link' is a sub-node of the
  221. * Ethernet device.
  222. */
  223. bool of_phy_is_fixed_link(struct device_node *np)
  224. {
  225. struct device_node *dn;
  226. int len;
  227. /* New binding */
  228. dn = of_get_child_by_name(np, "fixed-link");
  229. if (dn) {
  230. of_node_put(dn);
  231. return true;
  232. }
  233. /* Old binding */
  234. if (of_get_property(np, "fixed-link", &len) &&
  235. len == (5 * sizeof(__be32)))
  236. return true;
  237. return false;
  238. }
  239. EXPORT_SYMBOL(of_phy_is_fixed_link);
  240. int of_phy_register_fixed_link(struct device_node *np)
  241. {
  242. struct fixed_phy_status status = {};
  243. struct device_node *fixed_link_node;
  244. const __be32 *fixed_link_prop;
  245. int len;
  246. struct phy_device *phy;
  247. /* New binding */
  248. fixed_link_node = of_get_child_by_name(np, "fixed-link");
  249. if (fixed_link_node) {
  250. status.link = 1;
  251. status.duplex = of_property_read_bool(fixed_link_node,
  252. "full-duplex");
  253. if (of_property_read_u32(fixed_link_node, "speed", &status.speed))
  254. return -EINVAL;
  255. status.pause = of_property_read_bool(fixed_link_node, "pause");
  256. status.asym_pause = of_property_read_bool(fixed_link_node,
  257. "asym-pause");
  258. of_node_put(fixed_link_node);
  259. phy = fixed_phy_register(PHY_POLL, &status, np);
  260. return IS_ERR(phy) ? PTR_ERR(phy) : 0;
  261. }
  262. /* Old binding */
  263. fixed_link_prop = of_get_property(np, "fixed-link", &len);
  264. if (fixed_link_prop && len == (5 * sizeof(__be32))) {
  265. status.link = 1;
  266. status.duplex = be32_to_cpu(fixed_link_prop[1]);
  267. status.speed = be32_to_cpu(fixed_link_prop[2]);
  268. status.pause = be32_to_cpu(fixed_link_prop[3]);
  269. status.asym_pause = be32_to_cpu(fixed_link_prop[4]);
  270. phy = fixed_phy_register(PHY_POLL, &status, np);
  271. return IS_ERR(phy) ? PTR_ERR(phy) : 0;
  272. }
  273. return -ENODEV;
  274. }
  275. EXPORT_SYMBOL(of_phy_register_fixed_link);
  276. #endif