of_mdio.c 13 KB

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