mdio-uclass.c 5.4 KB

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