eth-phy-uclass.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2020 NXP
  4. */
  5. #define LOG_CATEGORY UCLASS_ETH_PHY
  6. #include <common.h>
  7. #include <dm.h>
  8. #include <log.h>
  9. #include <net.h>
  10. #include <asm-generic/gpio.h>
  11. #include <dm/device_compat.h>
  12. #include <dm/device-internal.h>
  13. #include <dm/uclass-internal.h>
  14. #include <dm/lists.h>
  15. #include <linux/delay.h>
  16. struct eth_phy_device_priv {
  17. struct mii_dev *mdio_bus;
  18. struct gpio_desc reset_gpio;
  19. u32 reset_assert_delay;
  20. u32 reset_deassert_delay;
  21. };
  22. int eth_phy_binds_nodes(struct udevice *eth_dev)
  23. {
  24. ofnode mdio_node, phy_node;
  25. const char *node_name;
  26. int ret;
  27. /* search a subnode named "mdio.*" */
  28. dev_for_each_subnode(mdio_node, eth_dev) {
  29. node_name = ofnode_get_name(mdio_node);
  30. if (!strncmp(node_name, "mdio", 4))
  31. break;
  32. }
  33. if (!ofnode_valid(mdio_node)) {
  34. dev_dbg(eth_dev, "%s: %s mdio subnode not found!\n", __func__,
  35. eth_dev->name);
  36. return -ENXIO;
  37. }
  38. dev_dbg(eth_dev, "%s: %s subnode found!\n", __func__, node_name);
  39. ofnode_for_each_subnode(phy_node, mdio_node) {
  40. node_name = ofnode_get_name(phy_node);
  41. dev_dbg(eth_dev, "* Found child node: '%s'\n", node_name);
  42. ret = device_bind_driver_to_node(eth_dev,
  43. "eth_phy_generic_drv",
  44. node_name, phy_node, NULL);
  45. if (ret) {
  46. dev_dbg(eth_dev, " - Eth phy binding error: %d\n", ret);
  47. continue;
  48. }
  49. dev_dbg(eth_dev, " - bound phy device: '%s'\n", node_name);
  50. }
  51. return 0;
  52. }
  53. int eth_phy_set_mdio_bus(struct udevice *eth_dev, struct mii_dev *mdio_bus)
  54. {
  55. struct udevice *dev;
  56. struct eth_phy_device_priv *uc_priv;
  57. for (uclass_first_device(UCLASS_ETH_PHY, &dev); dev;
  58. uclass_next_device(&dev)) {
  59. if (dev->parent == eth_dev) {
  60. uc_priv = (struct eth_phy_device_priv *)(dev_get_uclass_priv(dev));
  61. if (!uc_priv->mdio_bus)
  62. uc_priv->mdio_bus = mdio_bus;
  63. }
  64. }
  65. return 0;
  66. }
  67. struct mii_dev *eth_phy_get_mdio_bus(struct udevice *eth_dev)
  68. {
  69. int ret;
  70. struct udevice *phy_dev;
  71. struct eth_phy_device_priv *uc_priv;
  72. /* Will probe the parent of phy device, then phy device */
  73. ret = uclass_get_device_by_phandle(UCLASS_ETH_PHY, eth_dev,
  74. "phy-handle", &phy_dev);
  75. if (!ret) {
  76. if (eth_dev != phy_dev->parent) {
  77. /*
  78. * phy_dev is shared and controlled by
  79. * other eth controller
  80. */
  81. uc_priv = (struct eth_phy_device_priv *)(dev_get_uclass_priv(phy_dev));
  82. if (uc_priv->mdio_bus)
  83. log_notice("Get shared mii bus on %s\n", eth_dev->name);
  84. else
  85. log_notice("Can't get shared mii bus on %s\n", eth_dev->name);
  86. return uc_priv->mdio_bus;
  87. }
  88. } else {
  89. log_notice("FEC: can't find phy-handle\n");
  90. }
  91. return NULL;
  92. }
  93. int eth_phy_get_addr(struct udevice *dev)
  94. {
  95. struct ofnode_phandle_args phandle_args;
  96. int reg;
  97. if (dev_read_phandle_with_args(dev, "phy-handle", NULL, 0, 0,
  98. &phandle_args)) {
  99. dev_dbg(dev, "Failed to find phy-handle");
  100. return -ENODEV;
  101. }
  102. reg = ofnode_read_u32_default(phandle_args.node, "reg", 0);
  103. return reg;
  104. }
  105. /* parsing generic properties of devicetree/bindings/net/ethernet-phy.yaml */
  106. static int eth_phy_of_to_plat(struct udevice *dev)
  107. {
  108. struct eth_phy_device_priv *uc_priv = dev_get_uclass_priv(dev);
  109. int ret;
  110. if (!CONFIG_IS_ENABLED(DM_GPIO))
  111. return 0;
  112. /* search "reset-gpios" in phy node */
  113. ret = gpio_request_by_name(dev, "reset-gpios", 0,
  114. &uc_priv->reset_gpio,
  115. GPIOD_IS_OUT);
  116. if (ret != -ENOENT)
  117. return ret;
  118. uc_priv->reset_assert_delay = dev_read_u32_default(dev, "reset-assert-us", 0);
  119. uc_priv->reset_deassert_delay = dev_read_u32_default(dev, "reset-deassert-us", 0);
  120. return 0;
  121. }
  122. void eth_phy_reset(struct udevice *dev, int value)
  123. {
  124. struct eth_phy_device_priv *uc_priv = dev_get_uclass_priv(dev);
  125. u32 delay;
  126. if (!CONFIG_IS_ENABLED(DM_GPIO))
  127. return;
  128. if (!dm_gpio_is_valid(&uc_priv->reset_gpio))
  129. return;
  130. dm_gpio_set_value(&uc_priv->reset_gpio, value);
  131. delay = value ? uc_priv->reset_assert_delay : uc_priv->reset_deassert_delay;
  132. if (delay)
  133. udelay(delay);
  134. }
  135. static int eth_phy_pre_probe(struct udevice *dev)
  136. {
  137. /* Assert and deassert the reset signal */
  138. eth_phy_reset(dev, 1);
  139. eth_phy_reset(dev, 0);
  140. return 0;
  141. }
  142. UCLASS_DRIVER(eth_phy_generic) = {
  143. .id = UCLASS_ETH_PHY,
  144. .name = "eth_phy_generic",
  145. .per_device_auto = sizeof(struct eth_phy_device_priv),
  146. .pre_probe = eth_phy_pre_probe,
  147. };
  148. U_BOOT_DRIVER(eth_phy_generic_drv) = {
  149. .name = "eth_phy_generic_drv",
  150. .id = UCLASS_ETH_PHY,
  151. .of_to_plat = eth_phy_of_to_plat,
  152. };