mdio-mux-uclass.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 <miiphy.h>
  10. #include <dm/device-internal.h>
  11. #include <dm/uclass-internal.h>
  12. #include <dm/lists.h>
  13. #define MDIO_MUX_CHILD_DRV_NAME "mdio-mux-bus-drv"
  14. /**
  15. * struct mdio_mux_perdev_priv - Per-device class data for MDIO MUX DM
  16. *
  17. * @parent_mdio: Parent DM MDIO device, this is called for actual MDIO I/O after
  18. * setting up the mux. Typically this is a real MDIO device,
  19. * unless there are cascaded muxes.
  20. * @selected: Current child bus selection. Defaults to -1
  21. */
  22. struct mdio_mux_perdev_priv {
  23. struct udevice *mdio_parent;
  24. int selected;
  25. };
  26. /*
  27. * This source file uses three types of devices, as follows:
  28. * - mux is the hardware MDIO MUX which selects between the existing child MDIO
  29. * buses, this is the device relevant for MDIO MUX class of drivers.
  30. * - ch is a child MDIO bus, this is just a representation of a mux selection,
  31. * not a real piece of hardware.
  32. * - mdio_parent is the actual MDIO bus called to perform reads/writes after
  33. * the MUX is configured. Typically this is a real MDIO device, unless there
  34. * are cascaded muxes.
  35. */
  36. /**
  37. * struct mdio_mux_ch_data - Per-device data for child MDIOs
  38. *
  39. * @sel: Selection value used by the MDIO MUX to access this child MDIO bus
  40. */
  41. struct mdio_mux_ch_data {
  42. int sel;
  43. };
  44. static struct udevice *mmux_get_parent_mdio(struct udevice *mux)
  45. {
  46. struct mdio_mux_perdev_priv *pdata = dev_get_uclass_priv(mux);
  47. return pdata->mdio_parent;
  48. }
  49. /* call driver select function before performing MDIO r/w */
  50. static int mmux_change_sel(struct udevice *ch, bool sel)
  51. {
  52. struct udevice *mux = ch->parent;
  53. struct mdio_mux_perdev_priv *priv = dev_get_uclass_priv(mux);
  54. struct mdio_mux_ops *ops = mdio_mux_get_ops(mux);
  55. struct mdio_mux_ch_data *ch_data = dev_get_parent_plat(ch);
  56. int err = 0;
  57. if (sel) {
  58. err = ops->select(mux, priv->selected, ch_data->sel);
  59. if (err)
  60. return err;
  61. priv->selected = ch_data->sel;
  62. } else {
  63. if (ops->deselect) {
  64. ops->deselect(mux, ch_data->sel);
  65. priv->selected = MDIO_MUX_SELECT_NONE;
  66. }
  67. }
  68. return 0;
  69. }
  70. /* Read wrapper, sets up the mux before issuing a read on parent MDIO bus */
  71. static int mmux_read(struct udevice *ch, int addr, int devad,
  72. int reg)
  73. {
  74. struct udevice *mux = ch->parent;
  75. struct udevice *parent_mdio = mmux_get_parent_mdio(mux);
  76. int err;
  77. err = mmux_change_sel(ch, true);
  78. if (err)
  79. return err;
  80. err = dm_mdio_read(parent_mdio, addr, devad, reg);
  81. mmux_change_sel(ch, false);
  82. return err;
  83. }
  84. /* Write wrapper, sets up the mux before issuing a write on parent MDIO bus */
  85. static int mmux_write(struct udevice *ch, int addr, int devad,
  86. int reg, u16 val)
  87. {
  88. struct udevice *mux = ch->parent;
  89. struct udevice *parent_mdio = mmux_get_parent_mdio(mux);
  90. int err;
  91. err = mmux_change_sel(ch, true);
  92. if (err)
  93. return err;
  94. err = dm_mdio_write(parent_mdio, addr, devad, reg, val);
  95. mmux_change_sel(ch, false);
  96. return err;
  97. }
  98. /* Reset wrapper, sets up the mux before issuing a reset on parent MDIO bus */
  99. static int mmux_reset(struct udevice *ch)
  100. {
  101. struct udevice *mux = ch->parent;
  102. struct udevice *parent_mdio = mmux_get_parent_mdio(mux);
  103. int err;
  104. /* reset is optional, if it's not implemented just exit */
  105. if (!mdio_get_ops(parent_mdio)->reset)
  106. return 0;
  107. err = mmux_change_sel(ch, true);
  108. if (err)
  109. return err;
  110. err = dm_mdio_reset(parent_mdio);
  111. mmux_change_sel(ch, false);
  112. return err;
  113. }
  114. /* Picks up the mux selection value for each child */
  115. static int dm_mdio_mux_child_post_bind(struct udevice *ch)
  116. {
  117. struct mdio_mux_ch_data *ch_data = dev_get_parent_plat(ch);
  118. ch_data->sel = dev_read_u32_default(ch, "reg", MDIO_MUX_SELECT_NONE);
  119. if (ch_data->sel == MDIO_MUX_SELECT_NONE)
  120. return -EINVAL;
  121. return 0;
  122. }
  123. /* Explicitly bind child MDIOs after binding the mux */
  124. static int dm_mdio_mux_post_bind(struct udevice *mux)
  125. {
  126. ofnode ch_node;
  127. int err, first_err = 0;
  128. if (!dev_has_ofnode(mux)) {
  129. debug("%s: no mux node found, no child MDIO busses set up\n",
  130. __func__);
  131. return 0;
  132. }
  133. /*
  134. * we're going by Linux bindings so the child nodes do not have
  135. * compatible strings. We're going through them here and binding to
  136. * them.
  137. */
  138. dev_for_each_subnode(ch_node, mux) {
  139. struct udevice *ch_dev;
  140. const char *ch_name;
  141. ch_name = ofnode_get_name(ch_node);
  142. err = device_bind_driver_to_node(mux, MDIO_MUX_CHILD_DRV_NAME,
  143. ch_name, ch_node, &ch_dev);
  144. /* try to bind all, but keep 1st error */
  145. if (err && !first_err)
  146. first_err = err;
  147. }
  148. return first_err;
  149. }
  150. /* Get a reference to the parent MDIO bus, it should be bound by now */
  151. static int dm_mdio_mux_post_probe(struct udevice *mux)
  152. {
  153. struct mdio_mux_perdev_priv *priv = dev_get_uclass_priv(mux);
  154. int err;
  155. priv->selected = MDIO_MUX_SELECT_NONE;
  156. /* pick up mdio parent from device tree */
  157. err = uclass_get_device_by_phandle(UCLASS_MDIO, mux, "mdio-parent-bus",
  158. &priv->mdio_parent);
  159. if (err) {
  160. debug("%s: didn't find mdio-parent-bus\n", __func__);
  161. return err;
  162. }
  163. return 0;
  164. }
  165. const struct mdio_ops mmux_child_mdio_ops = {
  166. .read = mmux_read,
  167. .write = mmux_write,
  168. .reset = mmux_reset,
  169. };
  170. /* MDIO class driver used for MUX child MDIO buses */
  171. U_BOOT_DRIVER(mdio_mux_child) = {
  172. .name = MDIO_MUX_CHILD_DRV_NAME,
  173. .id = UCLASS_MDIO,
  174. .ops = &mmux_child_mdio_ops,
  175. };
  176. UCLASS_DRIVER(mdio_mux) = {
  177. .id = UCLASS_MDIO_MUX,
  178. .name = "mdio-mux",
  179. .child_post_bind = dm_mdio_mux_child_post_bind,
  180. .post_bind = dm_mdio_mux_post_bind,
  181. .post_probe = dm_mdio_mux_post_probe,
  182. .per_device_auto = sizeof(struct mdio_mux_perdev_priv),
  183. .per_child_plat_auto = sizeof(struct mdio_mux_ch_data),
  184. };