mdio-mux-uclass.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. static struct mdio_ops *mmux_get_mdio_parent_ops(struct udevice *mux)
  50. {
  51. return mdio_get_ops(mmux_get_parent_mdio(mux));
  52. }
  53. /* call driver select function before performing MDIO r/w */
  54. static int mmux_change_sel(struct udevice *ch, bool sel)
  55. {
  56. struct udevice *mux = ch->parent;
  57. struct mdio_mux_perdev_priv *priv = dev_get_uclass_priv(mux);
  58. struct mdio_mux_ops *ops = mdio_mux_get_ops(mux);
  59. struct mdio_mux_ch_data *ch_data = dev_get_parent_plat(ch);
  60. int err = 0;
  61. if (sel) {
  62. err = ops->select(mux, priv->selected, ch_data->sel);
  63. if (err)
  64. return err;
  65. priv->selected = ch_data->sel;
  66. } else {
  67. if (ops->deselect) {
  68. ops->deselect(mux, ch_data->sel);
  69. priv->selected = MDIO_MUX_SELECT_NONE;
  70. }
  71. }
  72. return 0;
  73. }
  74. /* Read wrapper, sets up the mux before issuing a read on parent MDIO bus */
  75. static int mmux_read(struct udevice *ch, int addr, int devad,
  76. int reg)
  77. {
  78. struct udevice *mux = ch->parent;
  79. struct udevice *parent_mdio = mmux_get_parent_mdio(mux);
  80. struct mdio_ops *parent_ops = mmux_get_mdio_parent_ops(mux);
  81. int err;
  82. err = mmux_change_sel(ch, true);
  83. if (err)
  84. return err;
  85. err = parent_ops->read(parent_mdio, addr, devad, reg);
  86. mmux_change_sel(ch, false);
  87. return err;
  88. }
  89. /* Write wrapper, sets up the mux before issuing a write on parent MDIO bus */
  90. static int mmux_write(struct udevice *ch, int addr, int devad,
  91. int reg, u16 val)
  92. {
  93. struct udevice *mux = ch->parent;
  94. struct udevice *parent_mdio = mmux_get_parent_mdio(mux);
  95. struct mdio_ops *parent_ops = mmux_get_mdio_parent_ops(mux);
  96. int err;
  97. err = mmux_change_sel(ch, true);
  98. if (err)
  99. return err;
  100. err = parent_ops->write(parent_mdio, addr, devad, reg, val);
  101. mmux_change_sel(ch, false);
  102. return err;
  103. }
  104. /* Reset wrapper, sets up the mux before issuing a reset on parent MDIO bus */
  105. static int mmux_reset(struct udevice *ch)
  106. {
  107. struct udevice *mux = ch->parent;
  108. struct udevice *parent_mdio = mmux_get_parent_mdio(mux);
  109. struct mdio_ops *parent_ops = mmux_get_mdio_parent_ops(mux);
  110. int err;
  111. /* reset is optional, if it's not implemented just exit */
  112. if (!parent_ops->reset)
  113. return 0;
  114. err = mmux_change_sel(ch, true);
  115. if (err)
  116. return err;
  117. err = parent_ops->reset(parent_mdio);
  118. mmux_change_sel(ch, false);
  119. return err;
  120. }
  121. /* Picks up the mux selection value for each child */
  122. static int dm_mdio_mux_child_post_bind(struct udevice *ch)
  123. {
  124. struct mdio_mux_ch_data *ch_data = dev_get_parent_plat(ch);
  125. ch_data->sel = dev_read_u32_default(ch, "reg", MDIO_MUX_SELECT_NONE);
  126. if (ch_data->sel == MDIO_MUX_SELECT_NONE)
  127. return -EINVAL;
  128. return 0;
  129. }
  130. /* Explicitly bind child MDIOs after binding the mux */
  131. static int dm_mdio_mux_post_bind(struct udevice *mux)
  132. {
  133. ofnode ch_node;
  134. int err, first_err = 0;
  135. if (!dev_has_ofnode(mux)) {
  136. debug("%s: no mux node found, no child MDIO busses set up\n",
  137. __func__);
  138. return 0;
  139. }
  140. /*
  141. * we're going by Linux bindings so the child nodes do not have
  142. * compatible strings. We're going through them here and binding to
  143. * them.
  144. */
  145. dev_for_each_subnode(ch_node, mux) {
  146. struct udevice *ch_dev;
  147. const char *ch_name;
  148. ch_name = ofnode_get_name(ch_node);
  149. err = device_bind_driver_to_node(mux, MDIO_MUX_CHILD_DRV_NAME,
  150. ch_name, ch_node, &ch_dev);
  151. /* try to bind all, but keep 1st error */
  152. if (err && !first_err)
  153. first_err = err;
  154. }
  155. return first_err;
  156. }
  157. /* Get a reference to the parent MDIO bus, it should be bound by now */
  158. static int dm_mdio_mux_post_probe(struct udevice *mux)
  159. {
  160. struct mdio_mux_perdev_priv *priv = dev_get_uclass_priv(mux);
  161. int err;
  162. priv->selected = MDIO_MUX_SELECT_NONE;
  163. /* pick up mdio parent from device tree */
  164. err = uclass_get_device_by_phandle(UCLASS_MDIO, mux, "mdio-parent-bus",
  165. &priv->mdio_parent);
  166. if (err) {
  167. debug("%s: didn't find mdio-parent-bus\n", __func__);
  168. return err;
  169. }
  170. return 0;
  171. }
  172. const struct mdio_ops mmux_child_mdio_ops = {
  173. .read = mmux_read,
  174. .write = mmux_write,
  175. .reset = mmux_reset,
  176. };
  177. /* MDIO class driver used for MUX child MDIO buses */
  178. U_BOOT_DRIVER(mdio_mux_child) = {
  179. .name = MDIO_MUX_CHILD_DRV_NAME,
  180. .id = UCLASS_MDIO,
  181. .ops = &mmux_child_mdio_ops,
  182. };
  183. UCLASS_DRIVER(mdio_mux) = {
  184. .id = UCLASS_MDIO_MUX,
  185. .name = "mdio-mux",
  186. .child_post_bind = dm_mdio_mux_child_post_bind,
  187. .post_bind = dm_mdio_mux_post_bind,
  188. .post_probe = dm_mdio_mux_post_probe,
  189. .per_device_auto = sizeof(struct mdio_mux_perdev_priv),
  190. .per_child_plat_auto = sizeof(struct mdio_mux_ch_data),
  191. };