d98a0d90b5a5822d17358dd862a6c0d7401fd8a2
[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 int 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 return 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 fp = kzalloc(sizeof(*fp), GFP_KERNEL);
164 if (!fp)
165 return -ENOMEM;
166
167 memset(fp->regs, 0xFF, sizeof(fp->regs[0]) * MII_REGS_NUM);
168
169 if (irq != PHY_POLL)
170 fmb->mii_bus->irq[phy_addr] = irq;
171
172 fp->addr = phy_addr;
173 fp->status = *status;
174 fp->link_gpio = link_gpio;
175
176 if (gpio_is_valid(fp->link_gpio)) {
177 ret = gpio_request_one(fp->link_gpio, GPIOF_DIR_IN,
178 "fixed-link-gpio-link");
179 if (ret)
180 goto err_regs;
181 }
182
183 ret = fixed_phy_update_regs(fp);
184 if (ret)
185 goto err_gpio;
186
187 list_add_tail(&fp->node, &fmb->phys);
188
189 return 0;
190
191 err_gpio:
192 if (gpio_is_valid(fp->link_gpio))
193 gpio_free(fp->link_gpio);
194 err_regs:
195 kfree(fp);
196 return ret;
197 }
198 EXPORT_SYMBOL_GPL(fixed_phy_add);
199
200 static void fixed_phy_del(int phy_addr)
201 {
202 struct fixed_mdio_bus *fmb = &platform_fmb;
203 struct fixed_phy *fp, *tmp;
204
205 list_for_each_entry_safe(fp, tmp, &fmb->phys, node) {
206 if (fp->addr == phy_addr) {
207 list_del(&fp->node);
208 if (gpio_is_valid(fp->link_gpio))
209 gpio_free(fp->link_gpio);
210 kfree(fp);
211 return;
212 }
213 }
214 }
215
216 static int phy_fixed_addr;
217 static DEFINE_SPINLOCK(phy_fixed_addr_lock);
218
219 struct phy_device *fixed_phy_register(unsigned int irq,
220 struct fixed_phy_status *status,
221 int link_gpio,
222 struct device_node *np)
223 {
224 struct fixed_mdio_bus *fmb = &platform_fmb;
225 struct phy_device *phy;
226 int phy_addr;
227 int ret;
228
229 if (!fmb->mii_bus || fmb->mii_bus->state != MDIOBUS_REGISTERED)
230 return ERR_PTR(-EPROBE_DEFER);
231
232 /* Get the next available PHY address, up to PHY_MAX_ADDR */
233 spin_lock(&phy_fixed_addr_lock);
234 if (phy_fixed_addr == PHY_MAX_ADDR) {
235 spin_unlock(&phy_fixed_addr_lock);
236 return ERR_PTR(-ENOSPC);
237 }
238 phy_addr = phy_fixed_addr++;
239 spin_unlock(&phy_fixed_addr_lock);
240
241 ret = fixed_phy_add(irq, phy_addr, status, link_gpio);
242 if (ret < 0)
243 return ERR_PTR(ret);
244
245 phy = get_phy_device(fmb->mii_bus, phy_addr, false);
246 if (IS_ERR(phy)) {
247 fixed_phy_del(phy_addr);
248 return ERR_PTR(-EINVAL);
249 }
250
251 /* propagate the fixed link values to struct phy_device */
252 phy->link = status->link;
253 if (status->link) {
254 phy->speed = status->speed;
255 phy->duplex = status->duplex;
256 phy->pause = status->pause;
257 phy->asym_pause = status->asym_pause;
258 }
259
260 of_node_get(np);
261 phy->mdio.dev.of_node = np;
262 phy->is_pseudo_fixed_link = true;
263
264 switch (status->speed) {
265 case SPEED_1000:
266 phy->supported = PHY_1000BT_FEATURES;
267 break;
268 case SPEED_100:
269 phy->supported = PHY_100BT_FEATURES;
270 break;
271 case SPEED_10:
272 default:
273 phy->supported = PHY_10BT_FEATURES;
274 }
275
276 ret = phy_device_register(phy);
277 if (ret) {
278 phy_device_free(phy);
279 of_node_put(np);
280 fixed_phy_del(phy_addr);
281 return ERR_PTR(ret);
282 }
283
284 return phy;
285 }
286 EXPORT_SYMBOL_GPL(fixed_phy_register);
287
288 void fixed_phy_unregister(struct phy_device *phy)
289 {
290 phy_device_remove(phy);
291
292 fixed_phy_del(phy->mdio.addr);
293 }
294 EXPORT_SYMBOL_GPL(fixed_phy_unregister);
295
296 static int __init fixed_mdio_bus_init(void)
297 {
298 struct fixed_mdio_bus *fmb = &platform_fmb;
299 int ret;
300
301 pdev = platform_device_register_simple("Fixed MDIO bus", 0, NULL, 0);
302 if (IS_ERR(pdev)) {
303 ret = PTR_ERR(pdev);
304 goto err_pdev;
305 }
306
307 fmb->mii_bus = mdiobus_alloc();
308 if (fmb->mii_bus == NULL) {
309 ret = -ENOMEM;
310 goto err_mdiobus_reg;
311 }
312
313 snprintf(fmb->mii_bus->id, MII_BUS_ID_SIZE, "fixed-0");
314 fmb->mii_bus->name = "Fixed MDIO Bus";
315 fmb->mii_bus->priv = fmb;
316 fmb->mii_bus->parent = &pdev->dev;
317 fmb->mii_bus->read = &fixed_mdio_read;
318 fmb->mii_bus->write = &fixed_mdio_write;
319
320 ret = mdiobus_register(fmb->mii_bus);
321 if (ret)
322 goto err_mdiobus_alloc;
323
324 return 0;
325
326 err_mdiobus_alloc:
327 mdiobus_free(fmb->mii_bus);
328 err_mdiobus_reg:
329 platform_device_unregister(pdev);
330 err_pdev:
331 return ret;
332 }
333 module_init(fixed_mdio_bus_init);
334
335 static void __exit fixed_mdio_bus_exit(void)
336 {
337 struct fixed_mdio_bus *fmb = &platform_fmb;
338 struct fixed_phy *fp, *tmp;
339
340 mdiobus_unregister(fmb->mii_bus);
341 mdiobus_free(fmb->mii_bus);
342 platform_device_unregister(pdev);
343
344 list_for_each_entry_safe(fp, tmp, &fmb->phys, node) {
345 list_del(&fp->node);
346 kfree(fp);
347 }
348 }
349 module_exit(fixed_mdio_bus_exit);
350
351 MODULE_DESCRIPTION("Fixed MDIO bus (MDIO bus emulation with fixed PHYs)");
352 MODULE_AUTHOR("Vitaly Bordug");
353 MODULE_LICENSE("GPL");
This page took 0.038605 seconds and 4 git commands to generate.