mdio-mux-uclass.c 5.9 KB

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