mdio-uclass.c 5.2 KB

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