fwnode_mdio.c 4.2 KB

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