KVM: x86: zero apic_arb_prio on reset
[deliverable/linux.git] / drivers / net / phy / fixed_phy.c
CommitLineData
11b0bacd 1/*
a79d8e93 2 * Fixed MDIO bus (MDIO bus emulation with fixed PHYs)
11b0bacd 3 *
a79d8e93
VB
4 * Author: Vitaly Bordug <vbordug@ru.mvista.com>
5 * Anton Vorontsov <avorontsov@ru.mvista.com>
11b0bacd 6 *
a79d8e93 7 * Copyright (c) 2006-2007 MontaVista Software, Inc.
11b0bacd
VB
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.
11b0bacd 13 */
a79d8e93 14
11b0bacd 15#include <linux/kernel.h>
11b0bacd 16#include <linux/module.h>
a79d8e93
VB
17#include <linux/platform_device.h>
18#include <linux/list.h>
11b0bacd 19#include <linux/mii.h>
11b0bacd 20#include <linux/phy.h>
7c32f470 21#include <linux/phy_fixed.h>
57401d5e 22#include <linux/err.h>
5a0e3ad6 23#include <linux/slab.h>
a7595121 24#include <linux/of.h>
a5597008 25#include <linux/gpio.h>
11b0bacd 26
a79d8e93 27#define MII_REGS_NUM 29
11b0bacd 28
a79d8e93
VB
29struct fixed_mdio_bus {
30 int irqs[PHY_MAX_ADDR];
298cf9be 31 struct mii_bus *mii_bus;
a79d8e93
VB
32 struct list_head phys;
33};
11b0bacd 34
a79d8e93 35struct fixed_phy {
9b744942 36 int addr;
a79d8e93
VB
37 u16 regs[MII_REGS_NUM];
38 struct phy_device *phydev;
39 struct fixed_phy_status status;
40 int (*link_update)(struct net_device *, struct fixed_phy_status *);
41 struct list_head node;
a5597008 42 int link_gpio;
a79d8e93 43};
7c32f470 44
a79d8e93
VB
45static struct platform_device *pdev;
46static struct fixed_mdio_bus platform_fmb = {
47 .phys = LIST_HEAD_INIT(platform_fmb.phys),
48};
7c32f470 49
a79d8e93 50static int fixed_phy_update_regs(struct fixed_phy *fp)
11b0bacd 51{
a79d8e93 52 u16 bmsr = BMSR_ANEGCAPABLE;
11b0bacd 53 u16 bmcr = 0;
a79d8e93
VB
54 u16 lpagb = 0;
55 u16 lpa = 0;
11b0bacd 56
a5597008
AL
57 if (gpio_is_valid(fp->link_gpio))
58 fp->status.link = !!gpio_get_value_cansleep(fp->link_gpio);
59
a79d8e93 60 if (fp->status.duplex) {
a79d8e93
VB
61 switch (fp->status.speed) {
62 case 1000:
63 bmsr |= BMSR_ESTATEN;
a79d8e93 64 break;
11b0bacd
VB
65 case 100:
66 bmsr |= BMSR_100FULL;
7c32f470 67 break;
11b0bacd
VB
68 case 10:
69 bmsr |= BMSR_10FULL;
7c32f470 70 break;
a79d8e93 71 default:
bc0f4a87 72 break;
11b0bacd
VB
73 }
74 } else {
a79d8e93
VB
75 switch (fp->status.speed) {
76 case 1000:
77 bmsr |= BMSR_ESTATEN;
a79d8e93 78 break;
11b0bacd
VB
79 case 100:
80 bmsr |= BMSR_100HALF;
7c32f470 81 break;
11b0bacd 82 case 10:
a79d8e93 83 bmsr |= BMSR_10HALF;
7c32f470 84 break;
a79d8e93 85 default:
bc0f4a87 86 break;
11b0bacd
VB
87 }
88 }
89
bc0f4a87
AL
90 if (fp->status.link) {
91 bmsr |= BMSR_LSTATUS | BMSR_ANEGCOMPLETE;
92
93 if (fp->status.duplex) {
94 bmcr |= BMCR_FULLDPLX;
95
96 switch (fp->status.speed) {
97 case 1000:
98 bmcr |= BMCR_SPEED1000;
99 lpagb |= LPA_1000FULL;
100 break;
101 case 100:
102 bmcr |= BMCR_SPEED100;
103 lpa |= LPA_100FULL;
104 break;
105 case 10:
106 lpa |= LPA_10FULL;
107 break;
108 default:
109 pr_warn("fixed phy: unknown speed\n");
110 return -EINVAL;
111 }
112 } else {
113 switch (fp->status.speed) {
114 case 1000:
115 bmcr |= BMCR_SPEED1000;
116 lpagb |= LPA_1000HALF;
117 break;
118 case 100:
119 bmcr |= BMCR_SPEED100;
120 lpa |= LPA_100HALF;
121 break;
122 case 10:
123 lpa |= LPA_10HALF;
124 break;
125 default:
126 pr_warn("fixed phy: unknown speed\n");
127 return -EINVAL;
128 }
129 }
a79d8e93 130
bc0f4a87
AL
131 if (fp->status.pause)
132 lpa |= LPA_PAUSE_CAP;
133
134 if (fp->status.asym_pause)
135 lpa |= LPA_PAUSE_ASYM;
136 }
a79d8e93 137
9b744942
TP
138 fp->regs[MII_PHYSID1] = 0;
139 fp->regs[MII_PHYSID2] = 0;
a79d8e93
VB
140
141 fp->regs[MII_BMSR] = bmsr;
142 fp->regs[MII_BMCR] = bmcr;
143 fp->regs[MII_LPA] = lpa;
144 fp->regs[MII_STAT1000] = lpagb;
11b0bacd
VB
145
146 return 0;
147}
148
9b744942 149static int fixed_mdio_read(struct mii_bus *bus, int phy_addr, int reg_num)
11b0bacd 150{
ec2a5652 151 struct fixed_mdio_bus *fmb = bus->priv;
a79d8e93
VB
152 struct fixed_phy *fp;
153
154 if (reg_num >= MII_REGS_NUM)
155 return -1;
156
a2dbba76
FF
157 /* We do not support emulating Clause 45 over Clause 22 register reads
158 * return an error instead of bogus data.
159 */
160 switch (reg_num) {
161 case MII_MMD_CTRL:
162 case MII_MMD_DATA:
163 return -1;
164 default:
165 break;
166 }
167
a79d8e93 168 list_for_each_entry(fp, &fmb->phys, node) {
9b744942 169 if (fp->addr == phy_addr) {
a79d8e93
VB
170 /* Issue callback if user registered it. */
171 if (fp->link_update) {
172 fp->link_update(fp->phydev->attached_dev,
173 &fp->status);
174 fixed_phy_update_regs(fp);
11b0bacd 175 }
a79d8e93 176 return fp->regs[reg_num];
7c32f470 177 }
a79d8e93 178 }
11b0bacd 179
a79d8e93 180 return 0xFFFF;
11b0bacd
VB
181}
182
9b744942 183static int fixed_mdio_write(struct mii_bus *bus, int phy_addr, int reg_num,
a79d8e93 184 u16 val)
11b0bacd 185{
11b0bacd
VB
186 return 0;
187}
188
a79d8e93
VB
189/*
190 * If something weird is required to be done with link/speed,
191 * network driver is able to assign a function to implement this.
192 * May be useful for PHY's that need to be software-driven.
193 */
194int fixed_phy_set_link_update(struct phy_device *phydev,
195 int (*link_update)(struct net_device *,
196 struct fixed_phy_status *))
11b0bacd 197{
a79d8e93
VB
198 struct fixed_mdio_bus *fmb = &platform_fmb;
199 struct fixed_phy *fp;
11b0bacd 200
799d4444 201 if (!phydev || !phydev->bus)
a79d8e93 202 return -EINVAL;
11b0bacd 203
a79d8e93 204 list_for_each_entry(fp, &fmb->phys, node) {
9b744942 205 if (fp->addr == phydev->addr) {
a79d8e93
VB
206 fp->link_update = link_update;
207 fp->phydev = phydev;
208 return 0;
209 }
210 }
11b0bacd 211
a79d8e93 212 return -ENOENT;
7c32f470 213}
a79d8e93 214EXPORT_SYMBOL_GPL(fixed_phy_set_link_update);
7c32f470 215
a3bebdce
SS
216int fixed_phy_update_state(struct phy_device *phydev,
217 const struct fixed_phy_status *status,
218 const struct fixed_phy_status *changed)
219{
220 struct fixed_mdio_bus *fmb = &platform_fmb;
221 struct fixed_phy *fp;
222
d618bf2b 223 if (!phydev || phydev->bus != fmb->mii_bus)
a3bebdce
SS
224 return -EINVAL;
225
226 list_for_each_entry(fp, &fmb->phys, node) {
227 if (fp->addr == phydev->addr) {
228#define _UPD(x) if (changed->x) \
229 fp->status.x = status->x
230 _UPD(link);
231 _UPD(speed);
232 _UPD(duplex);
233 _UPD(pause);
234 _UPD(asym_pause);
235#undef _UPD
236 fixed_phy_update_regs(fp);
237 return 0;
238 }
239 }
240
241 return -ENOENT;
242}
243EXPORT_SYMBOL(fixed_phy_update_state);
244
9b744942 245int fixed_phy_add(unsigned int irq, int phy_addr,
a5597008
AL
246 struct fixed_phy_status *status,
247 int link_gpio)
11b0bacd 248{
a79d8e93
VB
249 int ret;
250 struct fixed_mdio_bus *fmb = &platform_fmb;
251 struct fixed_phy *fp;
11b0bacd 252
a79d8e93
VB
253 fp = kzalloc(sizeof(*fp), GFP_KERNEL);
254 if (!fp)
255 return -ENOMEM;
11b0bacd 256
a79d8e93 257 memset(fp->regs, 0xFF, sizeof(fp->regs[0]) * MII_REGS_NUM);
11b0bacd 258
9b744942 259 fmb->irqs[phy_addr] = irq;
11b0bacd 260
9b744942 261 fp->addr = phy_addr;
a79d8e93 262 fp->status = *status;
a5597008
AL
263 fp->link_gpio = link_gpio;
264
265 if (gpio_is_valid(fp->link_gpio)) {
266 ret = gpio_request_one(fp->link_gpio, GPIOF_DIR_IN,
267 "fixed-link-gpio-link");
268 if (ret)
269 goto err_regs;
270 }
7c32f470 271
a79d8e93
VB
272 ret = fixed_phy_update_regs(fp);
273 if (ret)
a5597008 274 goto err_gpio;
11b0bacd 275
a79d8e93 276 list_add_tail(&fp->node, &fmb->phys);
7c32f470 277
a79d8e93 278 return 0;
11b0bacd 279
a5597008
AL
280err_gpio:
281 if (gpio_is_valid(fp->link_gpio))
282 gpio_free(fp->link_gpio);
a79d8e93
VB
283err_regs:
284 kfree(fp);
285 return ret;
286}
287EXPORT_SYMBOL_GPL(fixed_phy_add);
11b0bacd 288
a7595121
TP
289void fixed_phy_del(int phy_addr)
290{
291 struct fixed_mdio_bus *fmb = &platform_fmb;
292 struct fixed_phy *fp, *tmp;
293
294 list_for_each_entry_safe(fp, tmp, &fmb->phys, node) {
295 if (fp->addr == phy_addr) {
296 list_del(&fp->node);
a5597008
AL
297 if (gpio_is_valid(fp->link_gpio))
298 gpio_free(fp->link_gpio);
a7595121
TP
299 kfree(fp);
300 return;
301 }
302 }
303}
304EXPORT_SYMBOL_GPL(fixed_phy_del);
305
306static int phy_fixed_addr;
307static DEFINE_SPINLOCK(phy_fixed_addr_lock);
308
fd2ef0ba
PG
309struct phy_device *fixed_phy_register(unsigned int irq,
310 struct fixed_phy_status *status,
a5597008 311 int link_gpio,
fd2ef0ba 312 struct device_node *np)
a7595121
TP
313{
314 struct fixed_mdio_bus *fmb = &platform_fmb;
315 struct phy_device *phy;
316 int phy_addr;
317 int ret;
318
319 /* Get the next available PHY address, up to PHY_MAX_ADDR */
320 spin_lock(&phy_fixed_addr_lock);
321 if (phy_fixed_addr == PHY_MAX_ADDR) {
322 spin_unlock(&phy_fixed_addr_lock);
fd2ef0ba 323 return ERR_PTR(-ENOSPC);
a7595121
TP
324 }
325 phy_addr = phy_fixed_addr++;
326 spin_unlock(&phy_fixed_addr_lock);
327
bd1a05ee 328 ret = fixed_phy_add(irq, phy_addr, status, link_gpio);
a7595121 329 if (ret < 0)
fd2ef0ba 330 return ERR_PTR(ret);
a7595121
TP
331
332 phy = get_phy_device(fmb->mii_bus, phy_addr, false);
333 if (!phy || IS_ERR(phy)) {
334 fixed_phy_del(phy_addr);
fd2ef0ba 335 return ERR_PTR(-EINVAL);
a7595121
TP
336 }
337
4b195360
MB
338 /* propagate the fixed link values to struct phy_device */
339 phy->link = status->link;
340 if (status->link) {
341 phy->speed = status->speed;
342 phy->duplex = status->duplex;
343 phy->pause = status->pause;
344 phy->asym_pause = status->asym_pause;
345 }
346
a7595121
TP
347 of_node_get(np);
348 phy->dev.of_node = np;
5a11dd7d 349 phy->is_pseudo_fixed_link = true;
a7595121 350
34b31da4
AL
351 switch (status->speed) {
352 case SPEED_1000:
353 phy->supported = PHY_1000BT_FEATURES;
354 break;
355 case SPEED_100:
356 phy->supported = PHY_100BT_FEATURES;
357 break;
358 case SPEED_10:
359 default:
360 phy->supported = PHY_10BT_FEATURES;
361 }
362
a7595121
TP
363 ret = phy_device_register(phy);
364 if (ret) {
365 phy_device_free(phy);
366 of_node_put(np);
367 fixed_phy_del(phy_addr);
fd2ef0ba 368 return ERR_PTR(ret);
a7595121
TP
369 }
370
fd2ef0ba 371 return phy;
a7595121 372}
37e9a690 373EXPORT_SYMBOL_GPL(fixed_phy_register);
a7595121 374
a79d8e93
VB
375static int __init fixed_mdio_bus_init(void)
376{
377 struct fixed_mdio_bus *fmb = &platform_fmb;
378 int ret;
11b0bacd 379
a79d8e93 380 pdev = platform_device_register_simple("Fixed MDIO bus", 0, NULL, 0);
57401d5e
DC
381 if (IS_ERR(pdev)) {
382 ret = PTR_ERR(pdev);
a79d8e93
VB
383 goto err_pdev;
384 }
11b0bacd 385
298cf9be
LB
386 fmb->mii_bus = mdiobus_alloc();
387 if (fmb->mii_bus == NULL) {
388 ret = -ENOMEM;
389 goto err_mdiobus_reg;
390 }
11b0bacd 391
9e6c643b 392 snprintf(fmb->mii_bus->id, MII_BUS_ID_SIZE, "fixed-0");
298cf9be 393 fmb->mii_bus->name = "Fixed MDIO Bus";
ec2a5652 394 fmb->mii_bus->priv = fmb;
298cf9be
LB
395 fmb->mii_bus->parent = &pdev->dev;
396 fmb->mii_bus->read = &fixed_mdio_read;
397 fmb->mii_bus->write = &fixed_mdio_write;
398 fmb->mii_bus->irq = fmb->irqs;
399
400 ret = mdiobus_register(fmb->mii_bus);
a79d8e93 401 if (ret)
298cf9be 402 goto err_mdiobus_alloc;
11b0bacd 403
a79d8e93 404 return 0;
11b0bacd 405
298cf9be
LB
406err_mdiobus_alloc:
407 mdiobus_free(fmb->mii_bus);
a79d8e93
VB
408err_mdiobus_reg:
409 platform_device_unregister(pdev);
410err_pdev:
411 return ret;
412}
413module_init(fixed_mdio_bus_init);
11b0bacd 414
a79d8e93
VB
415static void __exit fixed_mdio_bus_exit(void)
416{
417 struct fixed_mdio_bus *fmb = &platform_fmb;
651be3a2 418 struct fixed_phy *fp, *tmp;
11b0bacd 419
298cf9be
LB
420 mdiobus_unregister(fmb->mii_bus);
421 mdiobus_free(fmb->mii_bus);
a79d8e93 422 platform_device_unregister(pdev);
11b0bacd 423
651be3a2 424 list_for_each_entry_safe(fp, tmp, &fmb->phys, node) {
a79d8e93
VB
425 list_del(&fp->node);
426 kfree(fp);
7c32f470 427 }
11b0bacd 428}
a79d8e93 429module_exit(fixed_mdio_bus_exit);
11b0bacd 430
a79d8e93 431MODULE_DESCRIPTION("Fixed MDIO bus (MDIO bus emulation with fixed PHYs)");
11b0bacd
VB
432MODULE_AUTHOR("Vitaly Bordug");
433MODULE_LICENSE("GPL");
This page took 0.863754 seconds and 5 git commands to generate.