fwnode_mdio.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * fwnode helpers for the MDIO (Ethernet PHY) API
  4. *
  5. * This file provides helper functions for extracting PHY device information
  6. * out of the fwnode and using it to populate an mii_bus.
  7. */
  8. #include <linux/acpi.h>
  9. #include <linux/dev_printk.h>
  10. #include <linux/fwnode_mdio.h>
  11. #include <linux/of.h>
  12. #include <linux/phy.h>
  13. #include <linux/pse-pd/pse.h>
  14. MODULE_AUTHOR("Calvin Johnson <calvin.johnson@oss.nxp.com>");
  15. MODULE_LICENSE("GPL");
  16. MODULE_DESCRIPTION("FWNODE MDIO bus (Ethernet PHY) accessors");
  17. static struct pse_control *
  18. fwnode_find_pse_control(struct fwnode_handle *fwnode)
  19. {
  20. struct pse_control *psec;
  21. struct device_node *np;
  22. if (!IS_ENABLED(CONFIG_PSE_CONTROLLER))
  23. return NULL;
  24. np = to_of_node(fwnode);
  25. if (!np)
  26. return NULL;
  27. psec = of_pse_control_get(np);
  28. if (PTR_ERR(psec) == -ENOENT)
  29. return NULL;
  30. return psec;
  31. }
  32. static struct mii_timestamper *
  33. fwnode_find_mii_timestamper(struct fwnode_handle *fwnode)
  34. {
  35. struct mii_timestamper *mii_ts;
  36. struct of_phandle_args arg;
  37. int err;
  38. if (is_acpi_node(fwnode))
  39. return NULL;
  40. err = of_parse_phandle_with_fixed_args(to_of_node(fwnode),
  41. "timestamper", 1, 0, &arg);
  42. if (err == -ENOENT)
  43. return NULL;
  44. else if (err)
  45. return ERR_PTR(err);
  46. if (arg.args_count != 1) {
  47. mii_ts = ERR_PTR(-EINVAL);
  48. goto put_node;
  49. }
  50. mii_ts = register_mii_timestamper(arg.np, arg.args[0]);
  51. put_node:
  52. of_node_put(arg.np);
  53. return mii_ts;
  54. }
  55. int fwnode_mdiobus_phy_device_register(struct mii_bus *mdio,
  56. struct phy_device *phy,
  57. struct fwnode_handle *child, u32 addr)
  58. {
  59. int rc;
  60. rc = fwnode_irq_get(child, 0);
  61. /* Don't wait forever if the IRQ provider doesn't become available,
  62. * just fall back to poll mode
  63. */
  64. if (rc == -EPROBE_DEFER)
  65. rc = driver_deferred_probe_check_state(&phy->mdio.dev);
  66. if (rc == -EPROBE_DEFER)
  67. return rc;
  68. if (rc > 0) {
  69. phy->irq = rc;
  70. mdio->irq[addr] = rc;
  71. } else {
  72. phy->irq = mdio->irq[addr];
  73. }
  74. if (fwnode_property_read_bool(child, "broken-turn-around"))
  75. mdio->phy_ignore_ta_mask |= 1 << addr;
  76. fwnode_property_read_u32(child, "reset-assert-us",
  77. &phy->mdio.reset_assert_delay);
  78. fwnode_property_read_u32(child, "reset-deassert-us",
  79. &phy->mdio.reset_deassert_delay);
  80. /* Associate the fwnode with the device structure so it
  81. * can be looked up later
  82. */
  83. fwnode_handle_get(child);
  84. device_set_node(&phy->mdio.dev, child);
  85. /* All data is now stored in the phy struct;
  86. * register it
  87. */
  88. rc = phy_device_register(phy);
  89. if (rc) {
  90. device_set_node(&phy->mdio.dev, NULL);
  91. fwnode_handle_put(child);
  92. return rc;
  93. }
  94. dev_dbg(&mdio->dev, "registered phy fwnode %pfw at address %i\n",
  95. child, addr);
  96. return 0;
  97. }
  98. EXPORT_SYMBOL(fwnode_mdiobus_phy_device_register);
  99. int fwnode_mdiobus_register_phy(struct mii_bus *bus,
  100. struct fwnode_handle *child, u32 addr)
  101. {
  102. struct mii_timestamper *mii_ts = NULL;
  103. struct pse_control *psec = NULL;
  104. struct phy_device *phy;
  105. bool is_c45;
  106. u32 phy_id;
  107. int rc;
  108. psec = fwnode_find_pse_control(child);
  109. if (IS_ERR(psec))
  110. return PTR_ERR(psec);
  111. mii_ts = fwnode_find_mii_timestamper(child);
  112. if (IS_ERR(mii_ts)) {
  113. rc = PTR_ERR(mii_ts);
  114. goto clean_pse;
  115. }
  116. is_c45 = fwnode_device_is_compatible(child, "ethernet-phy-ieee802.3-c45");
  117. if (is_c45 || fwnode_get_phy_id(child, &phy_id))
  118. phy = get_phy_device(bus, addr, is_c45);
  119. else
  120. phy = phy_device_create(bus, addr, phy_id, 0, NULL);
  121. if (IS_ERR(phy)) {
  122. rc = PTR_ERR(phy);
  123. goto clean_mii_ts;
  124. }
  125. if (is_acpi_node(child)) {
  126. phy->irq = bus->irq[addr];
  127. /* Associate the fwnode with the device structure so it
  128. * can be looked up later.
  129. */
  130. phy->mdio.dev.fwnode = fwnode_handle_get(child);
  131. /* All data is now stored in the phy struct, so register it */
  132. rc = phy_device_register(phy);
  133. if (rc) {
  134. phy->mdio.dev.fwnode = NULL;
  135. fwnode_handle_put(child);
  136. goto clean_phy;
  137. }
  138. } else if (is_of_node(child)) {
  139. rc = fwnode_mdiobus_phy_device_register(bus, phy, child, addr);
  140. if (rc)
  141. goto clean_phy;
  142. }
  143. phy->psec = psec;
  144. /* phy->mii_ts may already be defined by the PHY driver. A
  145. * mii_timestamper probed via the device tree will still have
  146. * precedence.
  147. */
  148. if (mii_ts)
  149. phy->mii_ts = mii_ts;
  150. return 0;
  151. clean_phy:
  152. phy_device_free(phy);
  153. clean_mii_ts:
  154. unregister_mii_timestamper(mii_ts);
  155. clean_pse:
  156. pse_control_put(psec);
  157. return rc;
  158. }
  159. EXPORT_SYMBOL(fwnode_mdiobus_register_phy);