d84e30c4682461f3214ed5e04f5c68b25a897289
[deliverable/linux.git] / drivers / net / phy / fixed_phy.c
1 /*
2 * Fixed MDIO bus (MDIO bus emulation with fixed PHYs)
3 *
4 * Author: Vitaly Bordug <vbordug@ru.mvista.com>
5 * Anton Vorontsov <avorontsov@ru.mvista.com>
6 *
7 * Copyright (c) 2006-2007 MontaVista Software, Inc.
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
13 */
14
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/platform_device.h>
18 #include <linux/list.h>
19 #include <linux/mii.h>
20 #include <linux/phy.h>
21 #include <linux/phy_fixed.h>
22 #include <linux/err.h>
23 #include <linux/slab.h>
24 #include <linux/of.h>
25 #include <linux/gpio.h>
26
27 #include "swphy.h"
28
29 #define MII_REGS_NUM 29
30
31 struct fixed_mdio_bus {
32 struct mii_bus *mii_bus;
33 struct list_head phys;
34 };
35
36 struct fixed_phy {
37 int addr;
38 u16 regs[MII_REGS_NUM];
39 struct phy_device *phydev;
40 struct fixed_phy_status status;
41 int (*link_update)(struct net_device *, struct fixed_phy_status *);
42 struct list_head node;
43 int link_gpio;
44 };
45
46 static struct platform_device *pdev;
47 static struct fixed_mdio_bus platform_fmb = {
48 .phys = LIST_HEAD_INIT(platform_fmb.phys),
49 };
50
51 static void fixed_phy_update_regs(struct fixed_phy *fp)
52 {
53 if (gpio_is_valid(fp->link_gpio))
54 fp->status.link = !!gpio_get_value_cansleep(fp->link_gpio);
55
56 swphy_update_regs(fp->regs, &fp->status);
57 }
58
59 static int fixed_mdio_read(struct mii_bus *bus, int phy_addr, int reg_num)
60 {
61 struct fixed_mdio_bus *fmb = bus->priv;
62 struct fixed_phy *fp;
63
64 if (reg_num >= MII_REGS_NUM)
65 return -1;
66
67 /* We do not support emulating Clause 45 over Clause 22 register reads
68 * return an error instead of bogus data.
69 */
70 switch (reg_num) {
71 case MII_MMD_CTRL:
72 case MII_MMD_DATA:
73 return -1;
74 default:
75 break;
76 }
77
78 list_for_each_entry(fp, &fmb->phys, node) {
79 if (fp->addr == phy_addr) {
80 /* Issue callback if user registered it. */
81 if (fp->link_update) {
82 fp->link_update(fp->phydev->attached_dev,
83 &fp->status);
84 fixed_phy_update_regs(fp);
85 }
86 return fp->regs[reg_num];
87 }
88 }
89
90 return 0xFFFF;
91 }
92
93 static int fixed_mdio_write(struct mii_bus *bus, int phy_addr, int reg_num,
94 u16 val)
95 {
96 return 0;
97 }
98
99 /*
100 * If something weird is required to be done with link/speed,
101 * network driver is able to assign a function to implement this.
102 * May be useful for PHY's that need to be software-driven.
103 */
104 int fixed_phy_set_link_update(struct phy_device *phydev,
105 int (*link_update)(struct net_device *,
106 struct fixed_phy_status *))
107 {
108 struct fixed_mdio_bus *fmb = &platform_fmb;
109 struct fixed_phy *fp;
110
111 if (!phydev || !phydev->mdio.bus)
112 return -EINVAL;
113
114 list_for_each_entry(fp, &fmb->phys, node) {
115 if (fp->addr == phydev->mdio.addr) {
116 fp->link_update = link_update;
117 fp->phydev = phydev;
118 return 0;
119 }
120 }
121
122 return -ENOENT;
123 }
124 EXPORT_SYMBOL_GPL(fixed_phy_set_link_update);
125
126 int fixed_phy_update_state(struct phy_device *phydev,
127 const struct fixed_phy_status *status,
128 const struct fixed_phy_status *changed)
129 {
130 struct fixed_mdio_bus *fmb = &platform_fmb;
131 struct fixed_phy *fp;
132
133 if (!phydev || phydev->mdio.bus != fmb->mii_bus)
134 return -EINVAL;
135
136 list_for_each_entry(fp, &fmb->phys, node) {
137 if (fp->addr == phydev->mdio.addr) {
138 #define _UPD(x) if (changed->x) \
139 fp->status.x = status->x
140 _UPD(link);
141 _UPD(speed);
142 _UPD(duplex);
143 _UPD(pause);
144 _UPD(asym_pause);
145 #undef _UPD
146 fixed_phy_update_regs(fp);
147 return 0;
148 }
149 }
150
151 return -ENOENT;
152 }
153 EXPORT_SYMBOL(fixed_phy_update_state);
154
155 int fixed_phy_add(unsigned int irq, int phy_addr,
156 struct fixed_phy_status *status,
157 int link_gpio)
158 {
159 int ret;
160 struct fixed_mdio_bus *fmb = &platform_fmb;
161 struct fixed_phy *fp;
162
163 ret = swphy_validate_state(status);
164 if (ret < 0)
165 return ret;
166
167 fp = kzalloc(sizeof(*fp), GFP_KERNEL);
168 if (!fp)
169 return -ENOMEM;
170
171 memset(fp->regs, 0xFF, sizeof(fp->regs[0]) * MII_REGS_NUM);
172
173 if (irq != PHY_POLL)
174 fmb->mii_bus->irq[phy_addr] = irq;
175
176 fp->addr = phy_addr;
177 fp->status = *status;
178 fp->link_gpio = link_gpio;
179
180 if (gpio_is_valid(fp->link_gpio)) {
181 ret = gpio_request_one(fp->link_gpio, GPIOF_DIR_IN,
182 "fixed-link-gpio-link");
183 if (ret)
184 goto err_regs;
185 }
186
187 fixed_phy_update_regs(fp);
188
189 list_add_tail(&fp->node, &fmb->phys);
190
191 return 0;
192
193 err_regs:
194 kfree(fp);
195 return ret;
196 }
197 EXPORT_SYMBOL_GPL(fixed_phy_add);
198
199 static void fixed_phy_del(int phy_addr)
200 {
201 struct fixed_mdio_bus *fmb = &platform_fmb;
202 struct fixed_phy *fp, *tmp;
203
204 list_for_each_entry_safe(fp, tmp, &fmb->phys, node) {
205 if (fp->addr == phy_addr) {
206 list_del(&fp->node);
207 if (gpio_is_valid(fp->link_gpio))
208 gpio_free(fp->link_gpio);
209 kfree(fp);
210 return;
211 }
212 }
213 }
214
215 static int phy_fixed_addr;
216 static DEFINE_SPINLOCK(phy_fixed_addr_lock);
217
218 struct phy_device *fixed_phy_register(unsigned int irq,
219 struct fixed_phy_status *status,
220 int link_gpio,
221 struct device_node *np)
222 {
223 struct fixed_mdio_bus *fmb = &platform_fmb;
224 struct phy_device *phy;
225 int phy_addr;
226 int ret;
227
228 if (!fmb->mii_bus || fmb->mii_bus->state != MDIOBUS_REGISTERED)
229 return ERR_PTR(-EPROBE_DEFER);
230
231 /* Get the next available PHY address, up to PHY_MAX_ADDR */
232 spin_lock(&phy_fixed_addr_lock);
233 if (phy_fixed_addr == PHY_MAX_ADDR) {
234 spin_unlock(&phy_fixed_addr_lock);
235 return ERR_PTR(-ENOSPC);
236 }
237 phy_addr = phy_fixed_addr++;
238 spin_unlock(&phy_fixed_addr_lock);
239
240 ret = fixed_phy_add(irq, phy_addr, status, link_gpio);
241 if (ret < 0)
242 return ERR_PTR(ret);
243
244 phy = get_phy_device(fmb->mii_bus, phy_addr, false);
245 if (IS_ERR(phy)) {
246 fixed_phy_del(phy_addr);
247 return ERR_PTR(-EINVAL);
248 }
249
250 /* propagate the fixed link values to struct phy_device */
251 phy->link = status->link;
252 if (status->link) {
253 phy->speed = status->speed;
254 phy->duplex = status->duplex;
255 phy->pause = status->pause;
256 phy->asym_pause = status->asym_pause;
257 }
258
259 of_node_get(np);
260 phy->mdio.dev.of_node = np;
261 phy->is_pseudo_fixed_link = true;
262
263 switch (status->speed) {
264 case SPEED_1000:
265 phy->supported = PHY_1000BT_FEATURES;
266 break;
267 case SPEED_100:
268 phy->supported = PHY_100BT_FEATURES;
269 break;
270 case SPEED_10:
271 default:
272 phy->supported = PHY_10BT_FEATURES;
273 }
274
275 ret = phy_device_register(phy);
276 if (ret) {
277 phy_device_free(phy);
278 of_node_put(np);
279 fixed_phy_del(phy_addr);
280 return ERR_PTR(ret);
281 }
282
283 return phy;
284 }
285 EXPORT_SYMBOL_GPL(fixed_phy_register);
286
287 void fixed_phy_unregister(struct phy_device *phy)
288 {
289 phy_device_remove(phy);
290
291 fixed_phy_del(phy->mdio.addr);
292 }
293 EXPORT_SYMBOL_GPL(fixed_phy_unregister);
294
295 static int __init fixed_mdio_bus_init(void)
296 {
297 struct fixed_mdio_bus *fmb = &platform_fmb;
298 int ret;
299
300 pdev = platform_device_register_simple("Fixed MDIO bus", 0, NULL, 0);
301 if (IS_ERR(pdev)) {
302 ret = PTR_ERR(pdev);
303 goto err_pdev;
304 }
305
306 fmb->mii_bus = mdiobus_alloc();
307 if (fmb->mii_bus == NULL) {
308 ret = -ENOMEM;
309 goto err_mdiobus_reg;
310 }
311
312 snprintf(fmb->mii_bus->id, MII_BUS_ID_SIZE, "fixed-0");
313 fmb->mii_bus->name = "Fixed MDIO Bus";
314 fmb->mii_bus->priv = fmb;
315 fmb->mii_bus->parent = &pdev->dev;
316 fmb->mii_bus->read = &fixed_mdio_read;
317 fmb->mii_bus->write = &fixed_mdio_write;
318
319 ret = mdiobus_register(fmb->mii_bus);
320 if (ret)
321 goto err_mdiobus_alloc;
322
323 return 0;
324
325 err_mdiobus_alloc:
326 mdiobus_free(fmb->mii_bus);
327 err_mdiobus_reg:
328 platform_device_unregister(pdev);
329 err_pdev:
330 return ret;
331 }
332 module_init(fixed_mdio_bus_init);
333
334 static void __exit fixed_mdio_bus_exit(void)
335 {
336 struct fixed_mdio_bus *fmb = &platform_fmb;
337 struct fixed_phy *fp, *tmp;
338
339 mdiobus_unregister(fmb->mii_bus);
340 mdiobus_free(fmb->mii_bus);
341 platform_device_unregister(pdev);
342
343 list_for_each_entry_safe(fp, tmp, &fmb->phys, node) {
344 list_del(&fp->node);
345 kfree(fp);
346 }
347 }
348 module_exit(fixed_mdio_bus_exit);
349
350 MODULE_DESCRIPTION("Fixed MDIO bus (MDIO bus emulation with fixed PHYs)");
351 MODULE_AUTHOR("Vitaly Bordug");
352 MODULE_LICENSE("GPL");
This page took 0.039409 seconds and 4 git commands to generate.