1 // SPDX-License-Identifier: GPL-2.0+
11 #include <dm/device-internal.h>
12 #include <dm/uclass-internal.h>
15 #define MDIO_MUX_CHILD_DRV_NAME "mdio-mux-bus-drv"
18 * struct mdio_mux_perdev_priv - Per-device class data for MDIO MUX DM
20 * @parent_mdio: Parent DM MDIO device, this is called for actual MDIO I/O after
21 * setting up the mux. Typically this is a real MDIO device,
22 * unless there are cascaded muxes.
23 * @selected: Current child bus selection. Defaults to -1
25 struct mdio_mux_perdev_priv {
26 struct udevice *mdio_parent;
31 * This source file uses three types of devices, as follows:
32 * - mux is the hardware MDIO MUX which selects between the existing child MDIO
33 * buses, this is the device relevant for MDIO MUX class of drivers.
34 * - ch is a child MDIO bus, this is just a representation of a mux selection,
35 * not a real piece of hardware.
36 * - mdio_parent is the actual MDIO bus called to perform reads/writes after
37 * the MUX is configured. Typically this is a real MDIO device, unless there
42 * struct mdio_mux_ch_data - Per-device data for child MDIOs
44 * @sel: Selection value used by the MDIO MUX to access this child MDIO bus
46 struct mdio_mux_ch_data {
50 static struct udevice *mmux_get_parent_mdio(struct udevice *mux)
52 struct mdio_mux_perdev_priv *pdata = dev_get_uclass_priv(mux);
54 return pdata->mdio_parent;
57 /* call driver select function before performing MDIO r/w */
58 static int mmux_change_sel(struct udevice *ch, bool sel)
60 struct udevice *mux = ch->parent;
61 struct mdio_mux_perdev_priv *priv = dev_get_uclass_priv(mux);
62 struct mdio_mux_ops *ops = mdio_mux_get_ops(mux);
63 struct mdio_mux_ch_data *ch_data = dev_get_parent_plat(ch);
67 err = ops->select(mux, priv->selected, ch_data->sel);
71 priv->selected = ch_data->sel;
74 ops->deselect(mux, ch_data->sel);
75 priv->selected = MDIO_MUX_SELECT_NONE;
82 /* Read wrapper, sets up the mux before issuing a read on parent MDIO bus */
83 static int mmux_read(struct udevice *ch, int addr, int devad,
86 struct udevice *mux = ch->parent;
87 struct udevice *parent_mdio = mmux_get_parent_mdio(mux);
90 err = mmux_change_sel(ch, true);
94 err = dm_mdio_read(parent_mdio, addr, devad, reg);
95 mmux_change_sel(ch, false);
100 /* Write wrapper, sets up the mux before issuing a write on parent MDIO bus */
101 static int mmux_write(struct udevice *ch, int addr, int devad,
104 struct udevice *mux = ch->parent;
105 struct udevice *parent_mdio = mmux_get_parent_mdio(mux);
108 err = mmux_change_sel(ch, true);
112 err = dm_mdio_write(parent_mdio, addr, devad, reg, val);
113 mmux_change_sel(ch, false);
118 /* Reset wrapper, sets up the mux before issuing a reset on parent MDIO bus */
119 static int mmux_reset(struct udevice *ch)
121 struct udevice *mux = ch->parent;
122 struct udevice *parent_mdio = mmux_get_parent_mdio(mux);
125 /* reset is optional, if it's not implemented just exit */
126 if (!mdio_get_ops(parent_mdio)->reset)
129 err = mmux_change_sel(ch, true);
133 err = dm_mdio_reset(parent_mdio);
134 mmux_change_sel(ch, false);
139 /* Picks up the mux selection value for each child */
140 static int dm_mdio_mux_child_post_bind(struct udevice *ch)
142 struct mdio_mux_ch_data *ch_data = dev_get_parent_plat(ch);
144 ch_data->sel = dev_read_u32_default(ch, "reg", MDIO_MUX_SELECT_NONE);
146 if (ch_data->sel == MDIO_MUX_SELECT_NONE)
152 /* Explicitly bind child MDIOs after binding the mux */
153 static int dm_mdio_mux_post_bind(struct udevice *mux)
156 int err, first_err = 0;
158 if (!dev_has_ofnode(mux)) {
159 debug("%s: no mux node found, no child MDIO busses set up\n",
165 * we're going by Linux bindings so the child nodes do not have
166 * compatible strings. We're going through them here and binding to
169 dev_for_each_subnode(ch_node, mux) {
170 struct udevice *ch_dev;
173 ch_name = ofnode_get_name(ch_node);
175 err = device_bind_driver_to_node(mux, MDIO_MUX_CHILD_DRV_NAME,
176 ch_name, ch_node, &ch_dev);
177 /* try to bind all, but keep 1st error */
178 if (err && !first_err)
185 /* Get a reference to the parent MDIO bus, it should be bound by now */
186 static int dm_mdio_mux_post_probe(struct udevice *mux)
188 struct mdio_mux_perdev_priv *priv = dev_get_uclass_priv(mux);
191 priv->selected = MDIO_MUX_SELECT_NONE;
193 /* pick up mdio parent from device tree */
194 err = uclass_get_device_by_phandle(UCLASS_MDIO, mux, "mdio-parent-bus",
197 debug("%s: didn't find mdio-parent-bus\n", __func__);
204 const struct mdio_ops mmux_child_mdio_ops = {
210 /* MDIO class driver used for MUX child MDIO buses */
211 U_BOOT_DRIVER(mdio_mux_child) = {
212 .name = MDIO_MUX_CHILD_DRV_NAME,
214 .ops = &mmux_child_mdio_ops,
217 UCLASS_DRIVER(mdio_mux) = {
218 .id = UCLASS_MDIO_MUX,
220 .child_post_bind = dm_mdio_mux_child_post_bind,
221 .post_bind = dm_mdio_mux_post_bind,
222 .post_probe = dm_mdio_mux_post_probe,
223 .per_device_auto = sizeof(struct mdio_mux_perdev_priv),
224 .per_child_plat_auto = sizeof(struct mdio_mux_ch_data),