of_mdio.c 14 KB

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