mdio-uclass.c 5.3 KB

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