of_mdio.c 13 KB

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