fwnode_mdio.c 4.2 KB

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