1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2011, 2012 Cavium, Inc.
6 #include <linux/device.h>
7 #include <linux/mdio-mux.h>
8 #include <linux/module.h>
9 #include <linux/of_mdio.h>
10 #include <linux/phy.h>
11 #include <linux/platform_device.h>
13 #define DRV_DESCRIPTION "MDIO bus multiplexer driver"
15 struct mdio_mux_child_bus;
17 struct mdio_mux_parent_bus {
18 struct mii_bus *mii_bus;
22 int (*switch_fn)(int current_child, int desired_child, void *data);
24 /* List of our children linked through their next fields. */
25 struct mdio_mux_child_bus *children;
28 struct mdio_mux_child_bus {
29 struct mii_bus *mii_bus;
30 struct mdio_mux_parent_bus *parent;
31 struct mdio_mux_child_bus *next;
36 * The parent bus' lock is used to order access to the switch_fn.
38 static int mdio_mux_read(struct mii_bus *bus, int phy_id, int regnum)
40 struct mdio_mux_child_bus *cb = bus->priv;
41 struct mdio_mux_parent_bus *pb = cb->parent;
44 mutex_lock_nested(&pb->mii_bus->mdio_lock, MDIO_MUTEX_MUX);
45 r = pb->switch_fn(pb->current_child, cb->bus_number, pb->switch_data);
49 pb->current_child = cb->bus_number;
51 r = pb->mii_bus->read(pb->mii_bus, phy_id, regnum);
53 mutex_unlock(&pb->mii_bus->mdio_lock);
58 static int mdio_mux_read_c45(struct mii_bus *bus, int phy_id, int dev_addr,
61 struct mdio_mux_child_bus *cb = bus->priv;
62 struct mdio_mux_parent_bus *pb = cb->parent;
65 mutex_lock_nested(&pb->mii_bus->mdio_lock, MDIO_MUTEX_MUX);
66 r = pb->switch_fn(pb->current_child, cb->bus_number, pb->switch_data);
70 pb->current_child = cb->bus_number;
72 r = pb->mii_bus->read_c45(pb->mii_bus, phy_id, dev_addr, regnum);
74 mutex_unlock(&pb->mii_bus->mdio_lock);
80 * The parent bus' lock is used to order access to the switch_fn.
82 static int mdio_mux_write(struct mii_bus *bus, int phy_id,
85 struct mdio_mux_child_bus *cb = bus->priv;
86 struct mdio_mux_parent_bus *pb = cb->parent;
90 mutex_lock_nested(&pb->mii_bus->mdio_lock, MDIO_MUTEX_MUX);
91 r = pb->switch_fn(pb->current_child, cb->bus_number, pb->switch_data);
95 pb->current_child = cb->bus_number;
97 r = pb->mii_bus->write(pb->mii_bus, phy_id, regnum, val);
99 mutex_unlock(&pb->mii_bus->mdio_lock);
104 static int mdio_mux_write_c45(struct mii_bus *bus, int phy_id, int dev_addr,
107 struct mdio_mux_child_bus *cb = bus->priv;
108 struct mdio_mux_parent_bus *pb = cb->parent;
112 mutex_lock_nested(&pb->mii_bus->mdio_lock, MDIO_MUTEX_MUX);
113 r = pb->switch_fn(pb->current_child, cb->bus_number, pb->switch_data);
117 pb->current_child = cb->bus_number;
119 r = pb->mii_bus->write_c45(pb->mii_bus, phy_id, dev_addr, regnum, val);
121 mutex_unlock(&pb->mii_bus->mdio_lock);
126 static int parent_count;
128 static void mdio_mux_uninit_children(struct mdio_mux_parent_bus *pb)
130 struct mdio_mux_child_bus *cb = pb->children;
133 mdiobus_unregister(cb->mii_bus);
134 mdiobus_free(cb->mii_bus);
139 int mdio_mux_init(struct device *dev,
140 struct device_node *mux_node,
141 int (*switch_fn)(int cur, int desired, void *data),
144 struct mii_bus *mux_bus)
146 struct device_node *parent_bus_node;
147 struct device_node *child_bus_node;
149 struct mii_bus *parent_bus;
150 struct mdio_mux_parent_bus *pb;
151 struct mdio_mux_child_bus *cb;
157 parent_bus_node = of_parse_phandle(mux_node,
158 "mdio-parent-bus", 0);
160 if (!parent_bus_node)
163 parent_bus = of_mdio_find_bus(parent_bus_node);
165 ret_val = -EPROBE_DEFER;
169 parent_bus_node = NULL;
170 parent_bus = mux_bus;
171 get_device(&parent_bus->dev);
174 pb = devm_kzalloc(dev, sizeof(*pb), GFP_KERNEL);
180 pb->switch_data = data;
181 pb->switch_fn = switch_fn;
182 pb->current_child = -1;
183 pb->parent_id = parent_count++;
184 pb->mii_bus = parent_bus;
187 for_each_available_child_of_node(mux_node, child_bus_node) {
190 r = of_property_read_u32(child_bus_node, "reg", &v);
193 "Error: Failed to find reg for child %pOF\n",
198 cb = devm_kzalloc(dev, sizeof(*cb), GFP_KERNEL);
206 cb->mii_bus = mdiobus_alloc();
211 cb->mii_bus->priv = cb;
213 cb->mii_bus->name = "mdio_mux";
214 snprintf(cb->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x.%x",
215 cb->mii_bus->name, pb->parent_id, v);
216 cb->mii_bus->parent = dev;
217 cb->mii_bus->read = mdio_mux_read;
218 cb->mii_bus->write = mdio_mux_write;
219 if (parent_bus->read_c45)
220 cb->mii_bus->read_c45 = mdio_mux_read_c45;
221 if (parent_bus->write_c45)
222 cb->mii_bus->write_c45 = mdio_mux_write_c45;
223 r = of_mdiobus_register(cb->mii_bus, child_bus_node);
225 mdiobus_free(cb->mii_bus);
226 if (r == -EPROBE_DEFER) {
232 "Error: Failed to register MDIO bus for child %pOF\n",
235 cb->next = pb->children;
244 dev_err(dev, "Error: No acceptable child buses found\n");
247 mdio_mux_uninit_children(pb);
248 of_node_put(child_bus_node);
250 put_device(&parent_bus->dev);
252 of_node_put(parent_bus_node);
255 EXPORT_SYMBOL_GPL(mdio_mux_init);
257 void mdio_mux_uninit(void *mux_handle)
259 struct mdio_mux_parent_bus *pb = mux_handle;
261 mdio_mux_uninit_children(pb);
262 put_device(&pb->mii_bus->dev);
264 EXPORT_SYMBOL_GPL(mdio_mux_uninit);
266 MODULE_DESCRIPTION(DRV_DESCRIPTION);
267 MODULE_AUTHOR("David Daney");
268 MODULE_LICENSE("GPL v2");