mdio-uclass.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 (ofnode_valid(dev->node)) {
  38. dt_name = ofnode_read_string(dev->node, "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. for (i = 0; i < PHY_HANDLE_STR_CNT; i++)
  118. if (!dev_read_phandle_with_args(ethdev, phy_handle_str[i], NULL,
  119. 0, 0, &phandle))
  120. break;
  121. if (!ofnode_valid(phandle.node)) {
  122. dev_dbg(ethdev, "can't find PHY node\n");
  123. return NULL;
  124. }
  125. /*
  126. * reading 'reg' directly should be fine. This is a PHY node, the
  127. * address is always size 1 and requires no translation
  128. */
  129. if (ofnode_read_u32(phandle.node, "reg", &phy_addr)) {
  130. dev_dbg(ethdev, "missing reg property in phy node\n");
  131. return NULL;
  132. }
  133. if (uclass_get_device_by_ofnode(UCLASS_MDIO,
  134. ofnode_get_parent(phandle.node),
  135. &mdiodev)) {
  136. dev_dbg(ethdev, "can't find MDIO bus for node %s\n",
  137. ofnode_get_name(ofnode_get_parent(phandle.node)));
  138. return NULL;
  139. }
  140. phy = dm_mdio_phy_connect(mdiodev, phy_addr, ethdev, interface);
  141. if (phy)
  142. phy->node = phandle.node;
  143. return phy;
  144. }
  145. /* Connect to a PHY linked in eth DT node */
  146. struct phy_device *dm_eth_phy_connect(struct udevice *ethdev)
  147. {
  148. const char *if_str;
  149. phy_interface_t interface;
  150. struct phy_device *phy;
  151. int i;
  152. if (!ofnode_valid(ethdev->node)) {
  153. debug("%s: supplied eth dev has no DT node!\n", ethdev->name);
  154. return NULL;
  155. }
  156. interface = PHY_INTERFACE_MODE_NONE;
  157. for (i = 0; i < PHY_MODE_STR_CNT; i++) {
  158. if_str = ofnode_read_string(ethdev->node, phy_mode_str[i]);
  159. if (if_str) {
  160. interface = phy_get_interface_by_name(if_str);
  161. break;
  162. }
  163. }
  164. if (interface < 0)
  165. interface = PHY_INTERFACE_MODE_NONE;
  166. if (interface == PHY_INTERFACE_MODE_NONE)
  167. dev_dbg(ethdev, "can't find interface mode, default to NONE\n");
  168. phy = dm_eth_connect_phy_handle(ethdev, interface);
  169. if (!phy)
  170. return NULL;
  171. phy->interface = interface;
  172. return phy;
  173. }
  174. UCLASS_DRIVER(mdio) = {
  175. .id = UCLASS_MDIO,
  176. .name = "mdio",
  177. .post_bind = dm_mdio_post_bind,
  178. .post_probe = dm_mdio_post_probe,
  179. .pre_remove = dm_mdio_pre_remove,
  180. .per_device_auto_alloc_size = sizeof(struct mdio_perdev_priv),
  181. };