^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2011, 2012 Cavium, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/mdio-mux.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/of_mdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/phy.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #define DRV_DESCRIPTION "MDIO bus multiplexer driver"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) struct mdio_mux_child_bus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) struct mdio_mux_parent_bus {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) struct mii_bus *mii_bus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) int current_child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) int parent_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) void *switch_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) int (*switch_fn)(int current_child, int desired_child, void *data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) /* List of our children linked through their next fields. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) struct mdio_mux_child_bus *children;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) struct mdio_mux_child_bus {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) struct mii_bus *mii_bus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) struct mdio_mux_parent_bus *parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) struct mdio_mux_child_bus *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) int bus_number;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * The parent bus' lock is used to order access to the switch_fn.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) static int mdio_mux_read(struct mii_bus *bus, int phy_id, int regnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) struct mdio_mux_child_bus *cb = bus->priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) struct mdio_mux_parent_bus *pb = cb->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) mutex_lock_nested(&pb->mii_bus->mdio_lock, MDIO_MUTEX_MUX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) r = pb->switch_fn(pb->current_child, cb->bus_number, pb->switch_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) pb->current_child = cb->bus_number;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) r = pb->mii_bus->read(pb->mii_bus, phy_id, regnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) mutex_unlock(&pb->mii_bus->mdio_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) * The parent bus' lock is used to order access to the switch_fn.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) static int mdio_mux_write(struct mii_bus *bus, int phy_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) int regnum, u16 val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) struct mdio_mux_child_bus *cb = bus->priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) struct mdio_mux_parent_bus *pb = cb->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) mutex_lock_nested(&pb->mii_bus->mdio_lock, MDIO_MUTEX_MUX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) r = pb->switch_fn(pb->current_child, cb->bus_number, pb->switch_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) pb->current_child = cb->bus_number;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) r = pb->mii_bus->write(pb->mii_bus, phy_id, regnum, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) mutex_unlock(&pb->mii_bus->mdio_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) static int parent_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) static void mdio_mux_uninit_children(struct mdio_mux_parent_bus *pb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) struct mdio_mux_child_bus *cb = pb->children;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) while (cb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) mdiobus_unregister(cb->mii_bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) mdiobus_free(cb->mii_bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) cb = cb->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) int mdio_mux_init(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) struct device_node *mux_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) int (*switch_fn)(int cur, int desired, void *data),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) void **mux_handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) void *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) struct mii_bus *mux_bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) struct device_node *parent_bus_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) struct device_node *child_bus_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) int r, ret_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) struct mii_bus *parent_bus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) struct mdio_mux_parent_bus *pb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) struct mdio_mux_child_bus *cb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) if (!mux_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) if (!mux_bus) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) parent_bus_node = of_parse_phandle(mux_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) "mdio-parent-bus", 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) if (!parent_bus_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) parent_bus = of_mdio_find_bus(parent_bus_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) if (!parent_bus) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) ret_val = -EPROBE_DEFER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) goto err_parent_bus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) parent_bus_node = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) parent_bus = mux_bus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) get_device(&parent_bus->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) pb = devm_kzalloc(dev, sizeof(*pb), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) if (!pb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) ret_val = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) goto err_pb_kz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) pb->switch_data = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) pb->switch_fn = switch_fn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) pb->current_child = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) pb->parent_id = parent_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) pb->mii_bus = parent_bus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) ret_val = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) for_each_available_child_of_node(mux_node, child_bus_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) int v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) r = of_property_read_u32(child_bus_node, "reg", &v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) dev_err(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) "Error: Failed to find reg for child %pOF\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) child_bus_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) cb = devm_kzalloc(dev, sizeof(*cb), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) if (!cb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) ret_val = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) goto err_loop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) cb->bus_number = v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) cb->parent = pb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) cb->mii_bus = mdiobus_alloc();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) if (!cb->mii_bus) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) ret_val = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) goto err_loop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) cb->mii_bus->priv = cb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) cb->mii_bus->name = "mdio_mux";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) snprintf(cb->mii_bus->id, MII_BUS_ID_SIZE, "%x.%x",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) pb->parent_id, v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) cb->mii_bus->parent = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) cb->mii_bus->read = mdio_mux_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) cb->mii_bus->write = mdio_mux_write;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) r = of_mdiobus_register(cb->mii_bus, child_bus_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) mdiobus_free(cb->mii_bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) if (r == -EPROBE_DEFER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) ret_val = r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) goto err_loop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) devm_kfree(dev, cb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) dev_err(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) "Error: Failed to register MDIO bus for child %pOF\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) child_bus_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) cb->next = pb->children;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) pb->children = cb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) if (pb->children) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) *mux_handle = pb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) dev_err(dev, "Error: No acceptable child buses found\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) devm_kfree(dev, pb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) err_loop:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) mdio_mux_uninit_children(pb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) of_node_put(child_bus_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) err_pb_kz:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) put_device(&parent_bus->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) err_parent_bus:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) of_node_put(parent_bus_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) return ret_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) EXPORT_SYMBOL_GPL(mdio_mux_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) void mdio_mux_uninit(void *mux_handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) struct mdio_mux_parent_bus *pb = mux_handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) mdio_mux_uninit_children(pb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) put_device(&pb->mii_bus->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) EXPORT_SYMBOL_GPL(mdio_mux_uninit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) MODULE_DESCRIPTION(DRV_DESCRIPTION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) MODULE_AUTHOR("David Daney");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) MODULE_LICENSE("GPL v2");