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