mdio-uclass.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2019
  4. * Alex Marginean, NXP
  5. */
  6. #include <common.h>
  7. #include <dm.h>
  8. #include <miiphy.h>
  9. #include <dm/device-internal.h>
  10. #include <dm/uclass-internal.h>
  11. /* DT node properties for MAC-PHY interface */
  12. #define PHY_MODE_STR_CNT 2
  13. static const char *phy_mode_str[PHY_MODE_STR_CNT] = { "phy-mode",
  14. "phy-connection-type" };
  15. /* DT node properties that reference a PHY node */
  16. #define PHY_HANDLE_STR_CNT 3
  17. const char *phy_handle_str[PHY_HANDLE_STR_CNT] = { "phy-handle",
  18. "phy",
  19. "phy-device" };
  20. void dm_mdio_probe_devices(void)
  21. {
  22. struct udevice *it;
  23. struct uclass *uc;
  24. uclass_get(UCLASS_MDIO, &uc);
  25. uclass_foreach_dev(it, uc) {
  26. device_probe(it);
  27. }
  28. }
  29. static int dm_mdio_post_bind(struct udevice *dev)
  30. {
  31. const char *dt_name;
  32. /* set a custom name for the MDIO device, if present in DT */
  33. if (ofnode_valid(dev->node)) {
  34. dt_name = ofnode_read_string(dev->node, "device-name");
  35. if (dt_name) {
  36. debug("renaming dev %s to %s\n", dev->name, dt_name);
  37. device_set_name(dev, dt_name);
  38. }
  39. }
  40. /*
  41. * MDIO command doesn't like spaces in names, don't allow them to keep
  42. * it happy
  43. */
  44. if (strchr(dev->name, ' ')) {
  45. debug("\nError: MDIO device name \"%s\" has a space!\n",
  46. dev->name);
  47. return -EINVAL;
  48. }
  49. return 0;
  50. }
  51. /*
  52. * Following read/write/reset functions are registered with legacy MII code.
  53. * These are called for PHY operations by upper layers and we further call the
  54. * DM MDIO driver functions.
  55. */
  56. static int mdio_read(struct mii_dev *mii_bus, int addr, int devad, int reg)
  57. {
  58. struct udevice *dev = mii_bus->priv;
  59. return mdio_get_ops(dev)->read(dev, addr, devad, reg);
  60. }
  61. static int mdio_write(struct mii_dev *mii_bus, int addr, int devad, int reg,
  62. u16 val)
  63. {
  64. struct udevice *dev = mii_bus->priv;
  65. return mdio_get_ops(dev)->write(dev, addr, devad, reg, val);
  66. }
  67. static int mdio_reset(struct mii_dev *mii_bus)
  68. {
  69. struct udevice *dev = mii_bus->priv;
  70. if (mdio_get_ops(dev)->reset)
  71. return mdio_get_ops(dev)->reset(dev);
  72. else
  73. return 0;
  74. }
  75. static int dm_mdio_post_probe(struct udevice *dev)
  76. {
  77. struct mdio_perdev_priv *pdata = dev_get_uclass_priv(dev);
  78. pdata->mii_bus = mdio_alloc();
  79. pdata->mii_bus->read = mdio_read;
  80. pdata->mii_bus->write = mdio_write;
  81. pdata->mii_bus->reset = mdio_reset;
  82. pdata->mii_bus->priv = dev;
  83. strncpy(pdata->mii_bus->name, dev->name, MDIO_NAME_LEN - 1);
  84. return mdio_register(pdata->mii_bus);
  85. }
  86. static int dm_mdio_pre_remove(struct udevice *dev)
  87. {
  88. struct mdio_perdev_priv *pdata = dev_get_uclass_priv(dev);
  89. struct mdio_ops *ops = mdio_get_ops(dev);
  90. if (ops->reset)
  91. ops->reset(dev);
  92. mdio_unregister(pdata->mii_bus);
  93. mdio_free(pdata->mii_bus);
  94. return 0;
  95. }
  96. struct phy_device *dm_mdio_phy_connect(struct udevice *mdiodev, int phyaddr,
  97. struct udevice *ethdev,
  98. phy_interface_t interface)
  99. {
  100. struct mdio_perdev_priv *pdata = dev_get_uclass_priv(mdiodev);
  101. if (device_probe(mdiodev))
  102. return NULL;
  103. return phy_connect(pdata->mii_bus, phyaddr, ethdev, interface);
  104. }
  105. static struct phy_device *dm_eth_connect_phy_handle(struct udevice *ethdev,
  106. phy_interface_t interface)
  107. {
  108. u32 phy_addr;
  109. struct udevice *mdiodev;
  110. struct phy_device *phy;
  111. struct ofnode_phandle_args phandle = {.node = ofnode_null()};
  112. int i;
  113. for (i = 0; i < PHY_HANDLE_STR_CNT; i++)
  114. if (!dev_read_phandle_with_args(ethdev, phy_handle_str[i], NULL,
  115. 0, 0, &phandle))
  116. break;
  117. if (!ofnode_valid(phandle.node)) {
  118. dev_dbg(dev, "can't find PHY node\n");
  119. return NULL;
  120. }
  121. /*
  122. * reading 'reg' directly should be fine. This is a PHY node, the
  123. * address is always size 1 and requires no translation
  124. */
  125. if (ofnode_read_u32(phandle.node, "reg", &phy_addr)) {
  126. dev_dbg(ethdev, "missing reg property in phy node\n");
  127. return NULL;
  128. }
  129. if (uclass_get_device_by_ofnode(UCLASS_MDIO,
  130. ofnode_get_parent(phandle.node),
  131. &mdiodev)) {
  132. dev_dbg(dev, "can't find MDIO bus for node %s\n",
  133. ofnode_get_name(ofnode_get_parent(phandle.node)));
  134. return NULL;
  135. }
  136. phy = dm_mdio_phy_connect(mdiodev, phy_addr, ethdev, interface);
  137. if (phy)
  138. phy->node = phandle.node;
  139. return phy;
  140. }
  141. /* Connect to a PHY linked in eth DT node */
  142. struct phy_device *dm_eth_phy_connect(struct udevice *ethdev)
  143. {
  144. const char *if_str;
  145. phy_interface_t interface;
  146. struct phy_device *phy;
  147. int i;
  148. if (!ofnode_valid(ethdev->node)) {
  149. debug("%s: supplied eth dev has no DT node!\n", ethdev->name);
  150. return NULL;
  151. }
  152. interface = PHY_INTERFACE_MODE_NONE;
  153. for (i = 0; i < PHY_MODE_STR_CNT; i++) {
  154. if_str = ofnode_read_string(ethdev->node, phy_mode_str[i]);
  155. if (if_str) {
  156. interface = phy_get_interface_by_name(if_str);
  157. break;
  158. }
  159. }
  160. if (interface < 0)
  161. interface = PHY_INTERFACE_MODE_NONE;
  162. if (interface == PHY_INTERFACE_MODE_NONE)
  163. dev_dbg(ethdev, "can't find interface mode, default to NONE\n");
  164. phy = dm_eth_connect_phy_handle(ethdev, interface);
  165. if (!phy)
  166. return NULL;
  167. phy->interface = interface;
  168. return phy;
  169. }
  170. UCLASS_DRIVER(mdio) = {
  171. .id = UCLASS_MDIO,
  172. .name = "mdio",
  173. .post_bind = dm_mdio_post_bind,
  174. .post_probe = dm_mdio_post_probe,
  175. .pre_remove = dm_mdio_pre_remove,
  176. .per_device_auto_alloc_size = sizeof(struct mdio_perdev_priv),
  177. };