of_mdio.c 9.4 KB

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