mdio-uclass.c 5.4 KB

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