Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at v4.7-rc3 204 lines 4.7 kB view raw
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 19struct mdio_mux_child_bus; 20 21struct 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 32struct 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; 37}; 38 39/* 40 * The parent bus' lock is used to order access to the switch_fn. 41 */ 42static 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 48 mutex_lock_nested(&pb->mii_bus->mdio_lock, MDIO_MUTEX_MUX); 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); 56out: 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 */ 65static 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 73 mutex_lock_nested(&pb->mii_bus->mdio_lock, MDIO_MUTEX_MUX); 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); 81out: 82 mutex_unlock(&pb->mii_bus->mdio_lock); 83 84 return r; 85} 86 87static int parent_count; 88 89int mdio_mux_init(struct device *dev, 90 int (*switch_fn)(int cur, int desired, void *data), 91 void **mux_handle, 92 void *data) 93{ 94 struct device_node *parent_bus_node; 95 struct device_node *child_bus_node; 96 int r, ret_val; 97 struct mii_bus *parent_bus; 98 struct mdio_mux_parent_bus *pb; 99 struct mdio_mux_child_bus *cb; 100 101 if (!dev->of_node) 102 return -ENODEV; 103 104 parent_bus_node = of_parse_phandle(dev->of_node, "mdio-parent-bus", 0); 105 106 if (!parent_bus_node) 107 return -ENODEV; 108 109 pb = devm_kzalloc(dev, sizeof(*pb), GFP_KERNEL); 110 if (pb == NULL) { 111 ret_val = -ENOMEM; 112 goto err_parent_bus; 113 } 114 115 parent_bus = of_mdio_find_bus(parent_bus_node); 116 if (parent_bus == NULL) { 117 ret_val = -EPROBE_DEFER; 118 goto err_parent_bus; 119 } 120 121 pb->switch_data = data; 122 pb->switch_fn = switch_fn; 123 pb->current_child = -1; 124 pb->parent_id = parent_count++; 125 pb->mii_bus = parent_bus; 126 127 ret_val = -ENODEV; 128 for_each_available_child_of_node(dev->of_node, child_bus_node) { 129 u32 v; 130 131 r = of_property_read_u32(child_bus_node, "reg", &v); 132 if (r) 133 continue; 134 135 cb = devm_kzalloc(dev, sizeof(*cb), GFP_KERNEL); 136 if (cb == NULL) { 137 dev_err(dev, 138 "Error: Failed to allocate memory for child\n"); 139 ret_val = -ENOMEM; 140 of_node_put(child_bus_node); 141 break; 142 } 143 cb->bus_number = v; 144 cb->parent = pb; 145 146 cb->mii_bus = mdiobus_alloc(); 147 if (!cb->mii_bus) { 148 ret_val = -ENOMEM; 149 of_node_put(child_bus_node); 150 break; 151 } 152 cb->mii_bus->priv = cb; 153 154 cb->mii_bus->name = "mdio_mux"; 155 snprintf(cb->mii_bus->id, MII_BUS_ID_SIZE, "%x.%x", 156 pb->parent_id, v); 157 cb->mii_bus->parent = dev; 158 cb->mii_bus->read = mdio_mux_read; 159 cb->mii_bus->write = mdio_mux_write; 160 r = of_mdiobus_register(cb->mii_bus, child_bus_node); 161 if (r) { 162 mdiobus_free(cb->mii_bus); 163 devm_kfree(dev, cb); 164 } else { 165 of_node_get(child_bus_node); 166 cb->next = pb->children; 167 pb->children = cb; 168 } 169 } 170 if (pb->children) { 171 *mux_handle = pb; 172 dev_info(dev, "Version " DRV_VERSION "\n"); 173 return 0; 174 } 175 176 /* balance the reference of_mdio_find_bus() took */ 177 put_device(&pb->mii_bus->dev); 178 179err_parent_bus: 180 of_node_put(parent_bus_node); 181 return ret_val; 182} 183EXPORT_SYMBOL_GPL(mdio_mux_init); 184 185void mdio_mux_uninit(void *mux_handle) 186{ 187 struct mdio_mux_parent_bus *pb = mux_handle; 188 struct mdio_mux_child_bus *cb = pb->children; 189 190 while (cb) { 191 mdiobus_unregister(cb->mii_bus); 192 mdiobus_free(cb->mii_bus); 193 cb = cb->next; 194 } 195 196 /* balance the reference of_mdio_find_bus() in mdio_mux_init() took */ 197 put_device(&pb->mii_bus->dev); 198} 199EXPORT_SYMBOL_GPL(mdio_mux_uninit); 200 201MODULE_DESCRIPTION(DRV_DESCRIPTION); 202MODULE_VERSION(DRV_VERSION); 203MODULE_AUTHOR("David Daney"); 204MODULE_LICENSE("GPL");