of_mdio.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * OF helpers for the MDIO (Ethernet PHY) API
  4. *
  5. * Copyright (c) 2009 Secret Lab Technologies, Ltd.
  6. *
  7. * This file provides helper functions for extracting PHY device information
  8. * out of the OpenFirmware device tree and using it to populate an mii_bus.
  9. */
  10. #include <linux/device.h>
  11. #include <linux/err.h>
  12. #include <linux/fwnode_mdio.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/netdevice.h>
  16. #include <linux/of.h>
  17. #include <linux/of_irq.h>
  18. #include <linux/of_mdio.h>
  19. #include <linux/of_net.h>
  20. #include <linux/phy.h>
  21. #include <linux/phy_fixed.h>
  22. #define DEFAULT_GPIO_RESET_DELAY 10 /* in microseconds */
  23. MODULE_AUTHOR("Grant Likely <[email protected]>");
  24. MODULE_LICENSE("GPL");
  25. /* Extract the clause 22 phy ID from the compatible string of the form
  26. * ethernet-phy-idAAAA.BBBB */
  27. static int of_get_phy_id(struct device_node *device, u32 *phy_id)
  28. {
  29. return fwnode_get_phy_id(of_fwnode_handle(device), phy_id);
  30. }
  31. int of_mdiobus_phy_device_register(struct mii_bus *mdio, struct phy_device *phy,
  32. struct device_node *child, u32 addr)
  33. {
  34. return fwnode_mdiobus_phy_device_register(mdio, phy,
  35. of_fwnode_handle(child),
  36. addr);
  37. }
  38. EXPORT_SYMBOL(of_mdiobus_phy_device_register);
  39. static int of_mdiobus_register_phy(struct mii_bus *mdio,
  40. struct device_node *child, u32 addr)
  41. {
  42. return fwnode_mdiobus_register_phy(mdio, of_fwnode_handle(child), addr);
  43. }
  44. static int of_mdiobus_register_device(struct mii_bus *mdio,
  45. struct device_node *child, u32 addr)
  46. {
  47. struct fwnode_handle *fwnode = of_fwnode_handle(child);
  48. struct mdio_device *mdiodev;
  49. int rc;
  50. mdiodev = mdio_device_create(mdio, addr);
  51. if (IS_ERR(mdiodev))
  52. return PTR_ERR(mdiodev);
  53. /* Associate the OF node with the device structure so it
  54. * can be looked up later.
  55. */
  56. fwnode_handle_get(fwnode);
  57. device_set_node(&mdiodev->dev, fwnode);
  58. /* All data is now stored in the mdiodev struct; register it. */
  59. rc = mdio_device_register(mdiodev);
  60. if (rc) {
  61. device_set_node(&mdiodev->dev, NULL);
  62. fwnode_handle_put(fwnode);
  63. mdio_device_free(mdiodev);
  64. return rc;
  65. }
  66. dev_dbg(&mdio->dev, "registered mdio device %pOFn at address %i\n",
  67. child, addr);
  68. return 0;
  69. }
  70. /* The following is a list of PHY compatible strings which appear in
  71. * some DTBs. The compatible string is never matched against a PHY
  72. * driver, so is pointless. We only expect devices which are not PHYs
  73. * to have a compatible string, so they can be matched to an MDIO
  74. * driver. Encourage users to upgrade their DT blobs to remove these.
  75. */
  76. static const struct of_device_id whitelist_phys[] = {
  77. { .compatible = "brcm,40nm-ephy" },
  78. { .compatible = "broadcom,bcm5241" },
  79. { .compatible = "marvell,88E1111", },
  80. { .compatible = "marvell,88e1116", },
  81. { .compatible = "marvell,88e1118", },
  82. { .compatible = "marvell,88e1145", },
  83. { .compatible = "marvell,88e1149r", },
  84. { .compatible = "marvell,88e1310", },
  85. { .compatible = "marvell,88E1510", },
  86. { .compatible = "marvell,88E1514", },
  87. { .compatible = "moxa,moxart-rtl8201cp", },
  88. {}
  89. };
  90. /*
  91. * Return true if the child node is for a phy. It must either:
  92. * o Compatible string of "ethernet-phy-idX.X"
  93. * o Compatible string of "ethernet-phy-ieee802.3-c45"
  94. * o Compatible string of "ethernet-phy-ieee802.3-c22"
  95. * o In the white list above (and issue a warning)
  96. * o No compatibility string
  97. *
  98. * A device which is not a phy is expected to have a compatible string
  99. * indicating what sort of device it is.
  100. */
  101. bool of_mdiobus_child_is_phy(struct device_node *child)
  102. {
  103. u32 phy_id;
  104. if (of_get_phy_id(child, &phy_id) != -EINVAL)
  105. return true;
  106. if (of_device_is_compatible(child, "ethernet-phy-ieee802.3-c45"))
  107. return true;
  108. if (of_device_is_compatible(child, "ethernet-phy-ieee802.3-c22"))
  109. return true;
  110. if (of_match_node(whitelist_phys, child)) {
  111. pr_warn(FW_WARN
  112. "%pOF: Whitelisted compatible string. Please remove\n",
  113. child);
  114. return true;
  115. }
  116. if (!of_find_property(child, "compatible", NULL))
  117. return true;
  118. return false;
  119. }
  120. EXPORT_SYMBOL(of_mdiobus_child_is_phy);
  121. /**
  122. * __of_mdiobus_register - Register mii_bus and create PHYs from the device tree
  123. * @mdio: pointer to mii_bus structure
  124. * @np: pointer to device_node of MDIO bus.
  125. * @owner: module owning the @mdio object.
  126. *
  127. * This function registers the mii_bus structure and registers a phy_device
  128. * for each child node of @np.
  129. */
  130. int __of_mdiobus_register(struct mii_bus *mdio, struct device_node *np,
  131. struct module *owner)
  132. {
  133. struct device_node *child;
  134. bool scanphys = false;
  135. int addr, rc;
  136. if (!np)
  137. return __mdiobus_register(mdio, owner);
  138. /* Do not continue if the node is disabled */
  139. if (!of_device_is_available(np))
  140. return -ENODEV;
  141. /* Mask out all PHYs from auto probing. Instead the PHYs listed in
  142. * the device tree are populated after the bus has been registered */
  143. mdio->phy_mask = ~0;
  144. device_set_node(&mdio->dev, of_fwnode_handle(np));
  145. /* Get bus level PHY reset GPIO details */
  146. mdio->reset_delay_us = DEFAULT_GPIO_RESET_DELAY;
  147. of_property_read_u32(np, "reset-delay-us", &mdio->reset_delay_us);
  148. mdio->reset_post_delay_us = 0;
  149. of_property_read_u32(np, "reset-post-delay-us", &mdio->reset_post_delay_us);
  150. /* Register the MDIO bus */
  151. rc = __mdiobus_register(mdio, owner);
  152. if (rc)
  153. return rc;
  154. /* Loop over the child nodes and register a phy_device for each phy */
  155. for_each_available_child_of_node(np, child) {
  156. addr = of_mdio_parse_addr(&mdio->dev, child);
  157. if (addr < 0) {
  158. scanphys = true;
  159. continue;
  160. }
  161. if (of_mdiobus_child_is_phy(child))
  162. rc = of_mdiobus_register_phy(mdio, child, addr);
  163. else
  164. rc = of_mdiobus_register_device(mdio, child, addr);
  165. if (rc == -ENODEV)
  166. dev_err(&mdio->dev,
  167. "MDIO device at address %d is missing.\n",
  168. addr);
  169. else if (rc)
  170. goto unregister;
  171. }
  172. if (!scanphys)
  173. return 0;
  174. /* auto scan for PHYs with empty reg property */
  175. for_each_available_child_of_node(np, child) {
  176. /* Skip PHYs with reg property set */
  177. if (of_find_property(child, "reg", NULL))
  178. continue;
  179. for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
  180. /* skip already registered PHYs */
  181. if (mdiobus_is_registered_device(mdio, addr))
  182. continue;
  183. /* be noisy to encourage people to set reg property */
  184. dev_info(&mdio->dev, "scan phy %pOFn at address %i\n",
  185. child, addr);
  186. if (of_mdiobus_child_is_phy(child)) {
  187. /* -ENODEV is the return code that PHYLIB has
  188. * standardized on to indicate that bus
  189. * scanning should continue.
  190. */
  191. rc = of_mdiobus_register_phy(mdio, child, addr);
  192. if (!rc)
  193. break;
  194. if (rc != -ENODEV)
  195. goto unregister;
  196. }
  197. }
  198. }
  199. return 0;
  200. unregister:
  201. of_node_put(child);
  202. mdiobus_unregister(mdio);
  203. return rc;
  204. }
  205. EXPORT_SYMBOL(__of_mdiobus_register);
  206. /**
  207. * of_mdio_find_device - Given a device tree node, find the mdio_device
  208. * @np: pointer to the mdio_device's device tree node
  209. *
  210. * If successful, returns a pointer to the mdio_device with the embedded
  211. * struct device refcount incremented by one, or NULL on failure.
  212. * The caller should call put_device() on the mdio_device after its use
  213. */
  214. struct mdio_device *of_mdio_find_device(struct device_node *np)
  215. {
  216. return fwnode_mdio_find_device(of_fwnode_handle(np));
  217. }
  218. EXPORT_SYMBOL(of_mdio_find_device);
  219. /**
  220. * of_phy_find_device - Give a PHY node, find the phy_device
  221. * @phy_np: Pointer to the phy's device tree node
  222. *
  223. * If successful, returns a pointer to the phy_device with the embedded
  224. * struct device refcount incremented by one, or NULL on failure.
  225. */
  226. struct phy_device *of_phy_find_device(struct device_node *phy_np)
  227. {
  228. return fwnode_phy_find_device(of_fwnode_handle(phy_np));
  229. }
  230. EXPORT_SYMBOL(of_phy_find_device);
  231. /**
  232. * of_phy_connect - Connect to the phy described in the device tree
  233. * @dev: pointer to net_device claiming the phy
  234. * @phy_np: Pointer to device tree node for the PHY
  235. * @hndlr: Link state callback for the network device
  236. * @flags: flags to pass to the PHY
  237. * @iface: PHY data interface type
  238. *
  239. * If successful, returns a pointer to the phy_device with the embedded
  240. * struct device refcount incremented by one, or NULL on failure. The
  241. * refcount must be dropped by calling phy_disconnect() or phy_detach().
  242. */
  243. struct phy_device *of_phy_connect(struct net_device *dev,
  244. struct device_node *phy_np,
  245. void (*hndlr)(struct net_device *), u32 flags,
  246. phy_interface_t iface)
  247. {
  248. struct phy_device *phy = of_phy_find_device(phy_np);
  249. int ret;
  250. if (!phy)
  251. return NULL;
  252. phy->dev_flags |= flags;
  253. ret = phy_connect_direct(dev, phy, hndlr, iface);
  254. /* refcount is held by phy_connect_direct() on success */
  255. put_device(&phy->mdio.dev);
  256. return ret ? NULL : phy;
  257. }
  258. EXPORT_SYMBOL(of_phy_connect);
  259. /**
  260. * of_phy_get_and_connect
  261. * - Get phy node and connect to the phy described in the device tree
  262. * @dev: pointer to net_device claiming the phy
  263. * @np: Pointer to device tree node for the net_device claiming the phy
  264. * @hndlr: Link state callback for the network device
  265. *
  266. * If successful, returns a pointer to the phy_device with the embedded
  267. * struct device refcount incremented by one, or NULL on failure. The
  268. * refcount must be dropped by calling phy_disconnect() or phy_detach().
  269. */
  270. struct phy_device *of_phy_get_and_connect(struct net_device *dev,
  271. struct device_node *np,
  272. void (*hndlr)(struct net_device *))
  273. {
  274. phy_interface_t iface;
  275. struct device_node *phy_np;
  276. struct phy_device *phy;
  277. int ret;
  278. ret = of_get_phy_mode(np, &iface);
  279. if (ret)
  280. return NULL;
  281. if (of_phy_is_fixed_link(np)) {
  282. ret = of_phy_register_fixed_link(np);
  283. if (ret < 0) {
  284. netdev_err(dev, "broken fixed-link specification\n");
  285. return NULL;
  286. }
  287. phy_np = of_node_get(np);
  288. } else {
  289. phy_np = of_parse_phandle(np, "phy-handle", 0);
  290. if (!phy_np)
  291. return NULL;
  292. }
  293. phy = of_phy_connect(dev, phy_np, hndlr, 0, iface);
  294. of_node_put(phy_np);
  295. return phy;
  296. }
  297. EXPORT_SYMBOL(of_phy_get_and_connect);
  298. /*
  299. * of_phy_is_fixed_link() and of_phy_register_fixed_link() must
  300. * support two DT bindings:
  301. * - the old DT binding, where 'fixed-link' was a property with 5
  302. * cells encoding various information about the fixed PHY
  303. * - the new DT binding, where 'fixed-link' is a sub-node of the
  304. * Ethernet device.
  305. */
  306. bool of_phy_is_fixed_link(struct device_node *np)
  307. {
  308. struct device_node *dn;
  309. int len, err;
  310. const char *managed;
  311. /* New binding */
  312. dn = of_get_child_by_name(np, "fixed-link");
  313. if (dn) {
  314. of_node_put(dn);
  315. return true;
  316. }
  317. err = of_property_read_string(np, "managed", &managed);
  318. if (err == 0 && strcmp(managed, "auto") != 0)
  319. return true;
  320. /* Old binding */
  321. if (of_get_property(np, "fixed-link", &len) &&
  322. len == (5 * sizeof(__be32)))
  323. return true;
  324. return false;
  325. }
  326. EXPORT_SYMBOL(of_phy_is_fixed_link);
  327. int of_phy_register_fixed_link(struct device_node *np)
  328. {
  329. struct fixed_phy_status status = {};
  330. struct device_node *fixed_link_node;
  331. u32 fixed_link_prop[5];
  332. const char *managed;
  333. if (of_property_read_string(np, "managed", &managed) == 0 &&
  334. strcmp(managed, "in-band-status") == 0) {
  335. /* status is zeroed, namely its .link member */
  336. goto register_phy;
  337. }
  338. /* New binding */
  339. fixed_link_node = of_get_child_by_name(np, "fixed-link");
  340. if (fixed_link_node) {
  341. status.link = 1;
  342. status.duplex = of_property_read_bool(fixed_link_node,
  343. "full-duplex");
  344. if (of_property_read_u32(fixed_link_node, "speed",
  345. &status.speed)) {
  346. of_node_put(fixed_link_node);
  347. return -EINVAL;
  348. }
  349. status.pause = of_property_read_bool(fixed_link_node, "pause");
  350. status.asym_pause = of_property_read_bool(fixed_link_node,
  351. "asym-pause");
  352. of_node_put(fixed_link_node);
  353. goto register_phy;
  354. }
  355. /* Old binding */
  356. if (of_property_read_u32_array(np, "fixed-link", fixed_link_prop,
  357. ARRAY_SIZE(fixed_link_prop)) == 0) {
  358. status.link = 1;
  359. status.duplex = fixed_link_prop[1];
  360. status.speed = fixed_link_prop[2];
  361. status.pause = fixed_link_prop[3];
  362. status.asym_pause = fixed_link_prop[4];
  363. goto register_phy;
  364. }
  365. return -ENODEV;
  366. register_phy:
  367. return PTR_ERR_OR_ZERO(fixed_phy_register(PHY_POLL, &status, np));
  368. }
  369. EXPORT_SYMBOL(of_phy_register_fixed_link);
  370. void of_phy_deregister_fixed_link(struct device_node *np)
  371. {
  372. struct phy_device *phydev;
  373. phydev = of_phy_find_device(np);
  374. if (!phydev)
  375. return;
  376. fixed_phy_unregister(phydev);
  377. put_device(&phydev->mdio.dev); /* of_phy_find_device() */
  378. phy_device_free(phydev); /* fixed_phy_register() */
  379. }
  380. EXPORT_SYMBOL(of_phy_deregister_fixed_link);