David Daney | 0ca2997 | 2012-05-02 15:16:38 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is subject to the terms and conditions of the GNU General Public |
| 3 | * License. See the file "COPYING" in the main directory of this archive |
| 4 | * for more details. |
| 5 | * |
| 6 | * Copyright (C) 2011, 2012 Cavium, Inc. |
| 7 | */ |
| 8 | |
| 9 | #include <linux/platform_device.h> |
| 10 | #include <linux/mdio-mux.h> |
| 11 | #include <linux/of_mdio.h> |
| 12 | #include <linux/device.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/phy.h> |
| 15 | |
| 16 | #define DRV_VERSION "1.0" |
| 17 | #define DRV_DESCRIPTION "MDIO bus multiplexer driver" |
| 18 | |
| 19 | struct mdio_mux_child_bus; |
| 20 | |
| 21 | struct mdio_mux_parent_bus { |
| 22 | struct mii_bus *mii_bus; |
| 23 | int current_child; |
| 24 | int parent_id; |
| 25 | void *switch_data; |
| 26 | int (*switch_fn)(int current_child, int desired_child, void *data); |
| 27 | |
| 28 | /* List of our children linked through their next fields. */ |
| 29 | struct mdio_mux_child_bus *children; |
| 30 | }; |
| 31 | |
| 32 | struct mdio_mux_child_bus { |
| 33 | struct mii_bus *mii_bus; |
| 34 | struct mdio_mux_parent_bus *parent; |
| 35 | struct mdio_mux_child_bus *next; |
| 36 | int bus_number; |
David Daney | 0ca2997 | 2012-05-02 15:16:38 +0000 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | /* |
| 40 | * The parent bus' lock is used to order access to the switch_fn. |
| 41 | */ |
| 42 | static int mdio_mux_read(struct mii_bus *bus, int phy_id, int regnum) |
| 43 | { |
| 44 | struct mdio_mux_child_bus *cb = bus->priv; |
| 45 | struct mdio_mux_parent_bus *pb = cb->parent; |
| 46 | int r; |
| 47 | |
Andrew Lunn | 9a6f2b0 | 2016-04-11 21:40:05 +0200 | [diff] [blame] | 48 | mutex_lock_nested(&pb->mii_bus->mdio_lock, MDIO_MUTEX_MUX); |
David Daney | 0ca2997 | 2012-05-02 15:16:38 +0000 | [diff] [blame] | 49 | r = pb->switch_fn(pb->current_child, cb->bus_number, pb->switch_data); |
| 50 | if (r) |
| 51 | goto out; |
| 52 | |
| 53 | pb->current_child = cb->bus_number; |
| 54 | |
| 55 | r = pb->mii_bus->read(pb->mii_bus, phy_id, regnum); |
| 56 | out: |
| 57 | mutex_unlock(&pb->mii_bus->mdio_lock); |
| 58 | |
| 59 | return r; |
| 60 | } |
| 61 | |
| 62 | /* |
| 63 | * The parent bus' lock is used to order access to the switch_fn. |
| 64 | */ |
| 65 | static int mdio_mux_write(struct mii_bus *bus, int phy_id, |
| 66 | int regnum, u16 val) |
| 67 | { |
| 68 | struct mdio_mux_child_bus *cb = bus->priv; |
| 69 | struct mdio_mux_parent_bus *pb = cb->parent; |
| 70 | |
| 71 | int r; |
| 72 | |
Andrew Lunn | 9a6f2b0 | 2016-04-11 21:40:05 +0200 | [diff] [blame] | 73 | mutex_lock_nested(&pb->mii_bus->mdio_lock, MDIO_MUTEX_MUX); |
David Daney | 0ca2997 | 2012-05-02 15:16:38 +0000 | [diff] [blame] | 74 | r = pb->switch_fn(pb->current_child, cb->bus_number, pb->switch_data); |
| 75 | if (r) |
| 76 | goto out; |
| 77 | |
| 78 | pb->current_child = cb->bus_number; |
| 79 | |
| 80 | r = pb->mii_bus->write(pb->mii_bus, phy_id, regnum, val); |
| 81 | out: |
| 82 | mutex_unlock(&pb->mii_bus->mdio_lock); |
| 83 | |
| 84 | return r; |
| 85 | } |
| 86 | |
| 87 | static int parent_count; |
| 88 | |
| 89 | int mdio_mux_init(struct device *dev, |
| 90 | int (*switch_fn)(int cur, int desired, void *data), |
| 91 | void **mux_handle, |
Pramod Kumar | f20e665 | 2016-06-10 11:03:45 +0530 | [diff] [blame] | 92 | void *data, |
| 93 | struct mii_bus *mux_bus) |
David Daney | 0ca2997 | 2012-05-02 15:16:38 +0000 | [diff] [blame] | 94 | { |
| 95 | struct device_node *parent_bus_node; |
| 96 | struct device_node *child_bus_node; |
| 97 | int r, ret_val; |
| 98 | struct mii_bus *parent_bus; |
| 99 | struct mdio_mux_parent_bus *pb; |
| 100 | struct mdio_mux_child_bus *cb; |
| 101 | |
| 102 | if (!dev->of_node) |
| 103 | return -ENODEV; |
| 104 | |
Pramod Kumar | f20e665 | 2016-06-10 11:03:45 +0530 | [diff] [blame] | 105 | if (!mux_bus) { |
| 106 | parent_bus_node = of_parse_phandle(dev->of_node, |
| 107 | "mdio-parent-bus", 0); |
David Daney | 0ca2997 | 2012-05-02 15:16:38 +0000 | [diff] [blame] | 108 | |
Pramod Kumar | f20e665 | 2016-06-10 11:03:45 +0530 | [diff] [blame] | 109 | if (!parent_bus_node) |
| 110 | return -ENODEV; |
| 111 | |
| 112 | parent_bus = of_mdio_find_bus(parent_bus_node); |
| 113 | if (!parent_bus) { |
| 114 | ret_val = -EPROBE_DEFER; |
| 115 | goto err_parent_bus; |
| 116 | } |
| 117 | } else { |
Arnd Bergmann | a78c16e | 2016-06-14 12:03:17 +0200 | [diff] [blame] | 118 | parent_bus_node = NULL; |
Pramod Kumar | f20e665 | 2016-06-10 11:03:45 +0530 | [diff] [blame] | 119 | parent_bus = mux_bus; |
| 120 | } |
David Daney | 0ca2997 | 2012-05-02 15:16:38 +0000 | [diff] [blame] | 121 | |
David Daney | 0ca2997 | 2012-05-02 15:16:38 +0000 | [diff] [blame] | 122 | pb = devm_kzalloc(dev, sizeof(*pb), GFP_KERNEL); |
| 123 | if (pb == NULL) { |
| 124 | ret_val = -ENOMEM; |
Jon Mason | b601616 | 2017-05-10 11:20:27 -0400 | [diff] [blame] | 125 | goto err_pb_kz; |
David Daney | 0ca2997 | 2012-05-02 15:16:38 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | pb->switch_data = data; |
| 129 | pb->switch_fn = switch_fn; |
| 130 | pb->current_child = -1; |
| 131 | pb->parent_id = parent_count++; |
| 132 | pb->mii_bus = parent_bus; |
| 133 | |
| 134 | ret_val = -ENODEV; |
Timur Tabi | 61abcb7 | 2012-08-14 13:20:24 +0000 | [diff] [blame] | 135 | for_each_available_child_of_node(dev->of_node, child_bus_node) { |
Colin Ian King | 9d15e5c | 2017-06-05 19:08:04 +0100 | [diff] [blame] | 136 | int v; |
David Daney | 0ca2997 | 2012-05-02 15:16:38 +0000 | [diff] [blame] | 137 | |
Jon Mason | 342fa19 | 2017-05-31 15:44:50 -0400 | [diff] [blame] | 138 | v = of_mdio_parse_addr(dev, child_bus_node); |
| 139 | if (v < 0) { |
| 140 | dev_err(dev, |
| 141 | "Error: Failed to find reg for child %s\n", |
| 142 | of_node_full_name(child_bus_node)); |
David Daney | 0ca2997 | 2012-05-02 15:16:38 +0000 | [diff] [blame] | 143 | continue; |
Jon Mason | 342fa19 | 2017-05-31 15:44:50 -0400 | [diff] [blame] | 144 | } |
David Daney | 0ca2997 | 2012-05-02 15:16:38 +0000 | [diff] [blame] | 145 | |
| 146 | cb = devm_kzalloc(dev, sizeof(*cb), GFP_KERNEL); |
| 147 | if (cb == NULL) { |
| 148 | dev_err(dev, |
Jon Mason | 342fa19 | 2017-05-31 15:44:50 -0400 | [diff] [blame] | 149 | "Error: Failed to allocate memory for child %s\n", |
| 150 | of_node_full_name(child_bus_node)); |
David Daney | 0ca2997 | 2012-05-02 15:16:38 +0000 | [diff] [blame] | 151 | ret_val = -ENOMEM; |
Jon Mason | 342fa19 | 2017-05-31 15:44:50 -0400 | [diff] [blame] | 152 | continue; |
David Daney | 0ca2997 | 2012-05-02 15:16:38 +0000 | [diff] [blame] | 153 | } |
| 154 | cb->bus_number = v; |
| 155 | cb->parent = pb; |
David Daney | 0ca2997 | 2012-05-02 15:16:38 +0000 | [diff] [blame] | 156 | |
Tobias Klauser | 20b08e1 | 2015-12-14 13:51:51 +0100 | [diff] [blame] | 157 | cb->mii_bus = mdiobus_alloc(); |
| 158 | if (!cb->mii_bus) { |
Jon Mason | 342fa19 | 2017-05-31 15:44:50 -0400 | [diff] [blame] | 159 | dev_err(dev, |
| 160 | "Error: Failed to allocate MDIO bus for child %s\n", |
| 161 | of_node_full_name(child_bus_node)); |
Tobias Klauser | 20b08e1 | 2015-12-14 13:51:51 +0100 | [diff] [blame] | 162 | ret_val = -ENOMEM; |
Jon Mason | b601616 | 2017-05-10 11:20:27 -0400 | [diff] [blame] | 163 | devm_kfree(dev, cb); |
Jon Mason | 342fa19 | 2017-05-31 15:44:50 -0400 | [diff] [blame] | 164 | continue; |
Tobias Klauser | 20b08e1 | 2015-12-14 13:51:51 +0100 | [diff] [blame] | 165 | } |
| 166 | cb->mii_bus->priv = cb; |
Andrew Lunn | e7f4dc3 | 2016-01-06 20:11:15 +0100 | [diff] [blame] | 167 | |
David Daney | 0ca2997 | 2012-05-02 15:16:38 +0000 | [diff] [blame] | 168 | cb->mii_bus->name = "mdio_mux"; |
| 169 | snprintf(cb->mii_bus->id, MII_BUS_ID_SIZE, "%x.%x", |
| 170 | pb->parent_id, v); |
| 171 | cb->mii_bus->parent = dev; |
| 172 | cb->mii_bus->read = mdio_mux_read; |
| 173 | cb->mii_bus->write = mdio_mux_write; |
| 174 | r = of_mdiobus_register(cb->mii_bus, child_bus_node); |
| 175 | if (r) { |
Jon Mason | 342fa19 | 2017-05-31 15:44:50 -0400 | [diff] [blame] | 176 | dev_err(dev, |
| 177 | "Error: Failed to register MDIO bus for child %s\n", |
| 178 | of_node_full_name(child_bus_node)); |
David Daney | 0ca2997 | 2012-05-02 15:16:38 +0000 | [diff] [blame] | 179 | mdiobus_free(cb->mii_bus); |
| 180 | devm_kfree(dev, cb); |
| 181 | } else { |
David Daney | 0ca2997 | 2012-05-02 15:16:38 +0000 | [diff] [blame] | 182 | cb->next = pb->children; |
| 183 | pb->children = cb; |
| 184 | } |
| 185 | } |
| 186 | if (pb->children) { |
| 187 | *mux_handle = pb; |
| 188 | dev_info(dev, "Version " DRV_VERSION "\n"); |
| 189 | return 0; |
| 190 | } |
Russell King | a136442 | 2015-09-24 20:35:52 +0100 | [diff] [blame] | 191 | |
Jon Mason | 342fa19 | 2017-05-31 15:44:50 -0400 | [diff] [blame] | 192 | dev_err(dev, "Error: No acceptable child buses found\n"); |
Jon Mason | b601616 | 2017-05-10 11:20:27 -0400 | [diff] [blame] | 193 | devm_kfree(dev, pb); |
| 194 | err_pb_kz: |
Russell King | a136442 | 2015-09-24 20:35:52 +0100 | [diff] [blame] | 195 | /* balance the reference of_mdio_find_bus() took */ |
Jon Mason | b601616 | 2017-05-10 11:20:27 -0400 | [diff] [blame] | 196 | if (!mux_bus) |
| 197 | put_device(&parent_bus->dev); |
David Daney | 0ca2997 | 2012-05-02 15:16:38 +0000 | [diff] [blame] | 198 | err_parent_bus: |
Arnd Bergmann | a78c16e | 2016-06-14 12:03:17 +0200 | [diff] [blame] | 199 | of_node_put(parent_bus_node); |
David Daney | 0ca2997 | 2012-05-02 15:16:38 +0000 | [diff] [blame] | 200 | return ret_val; |
| 201 | } |
| 202 | EXPORT_SYMBOL_GPL(mdio_mux_init); |
| 203 | |
| 204 | void mdio_mux_uninit(void *mux_handle) |
| 205 | { |
| 206 | struct mdio_mux_parent_bus *pb = mux_handle; |
| 207 | struct mdio_mux_child_bus *cb = pb->children; |
| 208 | |
| 209 | while (cb) { |
| 210 | mdiobus_unregister(cb->mii_bus); |
| 211 | mdiobus_free(cb->mii_bus); |
| 212 | cb = cb->next; |
| 213 | } |
Russell King | a136442 | 2015-09-24 20:35:52 +0100 | [diff] [blame] | 214 | |
| 215 | /* balance the reference of_mdio_find_bus() in mdio_mux_init() took */ |
| 216 | put_device(&pb->mii_bus->dev); |
David Daney | 0ca2997 | 2012-05-02 15:16:38 +0000 | [diff] [blame] | 217 | } |
| 218 | EXPORT_SYMBOL_GPL(mdio_mux_uninit); |
| 219 | |
| 220 | MODULE_DESCRIPTION(DRV_DESCRIPTION); |
| 221 | MODULE_VERSION(DRV_VERSION); |
| 222 | MODULE_AUTHOR("David Daney"); |
| 223 | MODULE_LICENSE("GPL"); |