mdio-mux.c 4.8 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2 3 4 5 6
/*
 * Copyright (C) 2011, 2012 Cavium, Inc.
 */

#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>
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43

#define DRV_DESCRIPTION "MDIO bus multiplexer driver"

struct mdio_mux_child_bus;

struct mdio_mux_parent_bus {
	struct mii_bus *mii_bus;
	int current_child;
	int parent_id;
	void *switch_data;
	int (*switch_fn)(int current_child, int desired_child, void *data);

	/* List of our children linked through their next fields. */
	struct mdio_mux_child_bus *children;
};

struct mdio_mux_child_bus {
	struct mii_bus *mii_bus;
	struct mdio_mux_parent_bus *parent;
	struct mdio_mux_child_bus *next;
	int bus_number;
};

/*
 * The parent bus' lock is used to order access to the switch_fn.
 */
static int mdio_mux_read(struct mii_bus *bus, int phy_id, int regnum)
{
	struct mdio_mux_child_bus *cb = bus->priv;
	struct mdio_mux_parent_bus *pb = cb->parent;
	int r;

44
	mutex_lock_nested(&pb->mii_bus->mdio_lock, MDIO_MUTEX_MUX);
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
	r = pb->switch_fn(pb->current_child, cb->bus_number, pb->switch_data);
	if (r)
		goto out;

	pb->current_child = cb->bus_number;

	r = pb->mii_bus->read(pb->mii_bus, phy_id, regnum);
out:
	mutex_unlock(&pb->mii_bus->mdio_lock);

	return r;
}

/*
 * The parent bus' lock is used to order access to the switch_fn.
 */
static int mdio_mux_write(struct mii_bus *bus, int phy_id,
			  int regnum, u16 val)
{
	struct mdio_mux_child_bus *cb = bus->priv;
	struct mdio_mux_parent_bus *pb = cb->parent;

	int r;

69
	mutex_lock_nested(&pb->mii_bus->mdio_lock, MDIO_MUTEX_MUX);
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
	r = pb->switch_fn(pb->current_child, cb->bus_number, pb->switch_data);
	if (r)
		goto out;

	pb->current_child = cb->bus_number;

	r = pb->mii_bus->write(pb->mii_bus, phy_id, regnum, val);
out:
	mutex_unlock(&pb->mii_bus->mdio_lock);

	return r;
}

static int parent_count;

85 86 87 88 89 90 91 92 93 94 95
static void mdio_mux_uninit_children(struct mdio_mux_parent_bus *pb)
{
	struct mdio_mux_child_bus *cb = pb->children;

	while (cb) {
		mdiobus_unregister(cb->mii_bus);
		mdiobus_free(cb->mii_bus);
		cb = cb->next;
	}
}

96
int mdio_mux_init(struct device *dev,
97
		  struct device_node *mux_node,
98 99
		  int (*switch_fn)(int cur, int desired, void *data),
		  void **mux_handle,
100 101
		  void *data,
		  struct mii_bus *mux_bus)
102 103 104 105 106 107 108 109
{
	struct device_node *parent_bus_node;
	struct device_node *child_bus_node;
	int r, ret_val;
	struct mii_bus *parent_bus;
	struct mdio_mux_parent_bus *pb;
	struct mdio_mux_child_bus *cb;

110
	if (!mux_node)
111 112
		return -ENODEV;

113
	if (!mux_bus) {
114
		parent_bus_node = of_parse_phandle(mux_node,
115
						   "mdio-parent-bus", 0);
116

117 118 119 120 121 122 123 124 125
		if (!parent_bus_node)
			return -ENODEV;

		parent_bus = of_mdio_find_bus(parent_bus_node);
		if (!parent_bus) {
			ret_val = -EPROBE_DEFER;
			goto err_parent_bus;
		}
	} else {
126
		parent_bus_node = NULL;
127
		parent_bus = mux_bus;
128
		get_device(&parent_bus->dev);
129
	}
130 131

	pb = devm_kzalloc(dev, sizeof(*pb), GFP_KERNEL);
132
	if (!pb) {
133
		ret_val = -ENOMEM;
134
		goto err_pb_kz;
135 136 137 138 139 140 141 142 143
	}

	pb->switch_data = data;
	pb->switch_fn = switch_fn;
	pb->current_child = -1;
	pb->parent_id = parent_count++;
	pb->mii_bus = parent_bus;

	ret_val = -ENODEV;
144
	for_each_available_child_of_node(mux_node, child_bus_node) {
145
		int v;
146

147 148
		r = of_property_read_u32(child_bus_node, "reg", &v);
		if (r) {
149
			dev_err(dev,
150 151
				"Error: Failed to find reg for child %pOF\n",
				child_bus_node);
152
			continue;
153
		}
154 155

		cb = devm_kzalloc(dev, sizeof(*cb), GFP_KERNEL);
156
		if (!cb) {
157
			ret_val = -ENOMEM;
158
			goto err_loop;
159 160 161
		}
		cb->bus_number = v;
		cb->parent = pb;
162

163
		cb->mii_bus = mdiobus_alloc();
164 165
		if (!cb->mii_bus) {
			ret_val = -ENOMEM;
166
			goto err_loop;
167
		}
168
		cb->mii_bus->priv = cb;
169

170 171 172 173 174 175 176 177
		cb->mii_bus->name = "mdio_mux";
		snprintf(cb->mii_bus->id, MII_BUS_ID_SIZE, "%x.%x",
			 pb->parent_id, v);
		cb->mii_bus->parent = dev;
		cb->mii_bus->read = mdio_mux_read;
		cb->mii_bus->write = mdio_mux_write;
		r = of_mdiobus_register(cb->mii_bus, child_bus_node);
		if (r) {
178 179 180 181 182 183
			mdiobus_free(cb->mii_bus);
			if (r == -EPROBE_DEFER) {
				ret_val = r;
				goto err_loop;
			}
			devm_kfree(dev, cb);
184
			dev_err(dev,
185 186
				"Error: Failed to register MDIO bus for child %pOF\n",
				child_bus_node);
187 188 189 190 191 192 193 194 195
		} else {
			cb->next = pb->children;
			pb->children = cb;
		}
	}
	if (pb->children) {
		*mux_handle = pb;
		return 0;
	}
196

197
	dev_err(dev, "Error: No acceptable child buses found\n");
198 199 200 201

err_loop:
	mdio_mux_uninit_children(pb);
	of_node_put(child_bus_node);
202
err_pb_kz:
203
	put_device(&parent_bus->dev);
204
err_parent_bus:
205
	of_node_put(parent_bus_node);
206 207 208 209 210 211 212
	return ret_val;
}
EXPORT_SYMBOL_GPL(mdio_mux_init);

void mdio_mux_uninit(void *mux_handle)
{
	struct mdio_mux_parent_bus *pb = mux_handle;
213

214
	mdio_mux_uninit_children(pb);
215
	put_device(&pb->mii_bus->dev);
216 217 218 219 220
}
EXPORT_SYMBOL_GPL(mdio_mux_uninit);

MODULE_DESCRIPTION(DRV_DESCRIPTION);
MODULE_AUTHOR("David Daney");
221
MODULE_LICENSE("GPL v2");