Merge remote-tracking branches 'asoc/topic/tas571x', 'asoc/topic/tlv320aic31xx',...
[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>
69fc58a5 26#include <linux/idr.h>
11b0bacd 27
a79d8e93 28#define MII_REGS_NUM 29
11b0bacd 29
a79d8e93 30struct fixed_mdio_bus {
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
e5a03bfd 201 if (!phydev || !phydev->mdio.bus)
a79d8e93 202 return -EINVAL;
11b0bacd 203
a79d8e93 204 list_for_each_entry(fp, &fmb->phys, node) {
e5a03bfd 205 if (fp->addr == phydev->mdio.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
e5a03bfd 223 if (!phydev || phydev->mdio.bus != fmb->mii_bus)
a3bebdce
SS
224 return -EINVAL;
225
226 list_for_each_entry(fp, &fmb->phys, node) {
e5a03bfd 227 if (fp->addr == phydev->mdio.addr) {
a3bebdce
SS
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
185be5ae
RV
259 if (irq != PHY_POLL)
260 fmb->mii_bus->irq[phy_addr] = irq;
11b0bacd 261
9b744942 262 fp->addr = phy_addr;
a79d8e93 263 fp->status = *status;
a5597008
AL
264 fp->link_gpio = link_gpio;
265
266 if (gpio_is_valid(fp->link_gpio)) {
267 ret = gpio_request_one(fp->link_gpio, GPIOF_DIR_IN,
268 "fixed-link-gpio-link");
269 if (ret)
270 goto err_regs;
271 }
7c32f470 272
a79d8e93
VB
273 ret = fixed_phy_update_regs(fp);
274 if (ret)
a5597008 275 goto err_gpio;
11b0bacd 276
a79d8e93 277 list_add_tail(&fp->node, &fmb->phys);
7c32f470 278
a79d8e93 279 return 0;
11b0bacd 280
a5597008
AL
281err_gpio:
282 if (gpio_is_valid(fp->link_gpio))
283 gpio_free(fp->link_gpio);
a79d8e93
VB
284err_regs:
285 kfree(fp);
286 return ret;
287}
288EXPORT_SYMBOL_GPL(fixed_phy_add);
11b0bacd 289
69fc58a5
FF
290static DEFINE_IDA(phy_fixed_ida);
291
5bcbe0f3 292static void fixed_phy_del(int phy_addr)
a7595121
TP
293{
294 struct fixed_mdio_bus *fmb = &platform_fmb;
295 struct fixed_phy *fp, *tmp;
296
297 list_for_each_entry_safe(fp, tmp, &fmb->phys, node) {
298 if (fp->addr == phy_addr) {
299 list_del(&fp->node);
a5597008
AL
300 if (gpio_is_valid(fp->link_gpio))
301 gpio_free(fp->link_gpio);
a7595121 302 kfree(fp);
69fc58a5 303 ida_simple_remove(&phy_fixed_ida, phy_addr);
a7595121
TP
304 return;
305 }
306 }
307}
a7595121 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
185be5ae
RV
319 if (!fmb->mii_bus || fmb->mii_bus->state != MDIOBUS_REGISTERED)
320 return ERR_PTR(-EPROBE_DEFER);
321
a7595121 322 /* Get the next available PHY address, up to PHY_MAX_ADDR */
69fc58a5
FF
323 phy_addr = ida_simple_get(&phy_fixed_ida, 0, PHY_MAX_ADDR, GFP_KERNEL);
324 if (phy_addr < 0)
325 return ERR_PTR(phy_addr);
a7595121 326
bd1a05ee 327 ret = fixed_phy_add(irq, phy_addr, status, link_gpio);
69fc58a5
FF
328 if (ret < 0) {
329 ida_simple_remove(&phy_fixed_ida, phy_addr);
fd2ef0ba 330 return ERR_PTR(ret);
69fc58a5 331 }
a7595121
TP
332
333 phy = get_phy_device(fmb->mii_bus, phy_addr, false);
4914a584 334 if (IS_ERR(phy)) {
a7595121 335 fixed_phy_del(phy_addr);
fd2ef0ba 336 return ERR_PTR(-EINVAL);
a7595121
TP
337 }
338
4b195360
MB
339 /* propagate the fixed link values to struct phy_device */
340 phy->link = status->link;
341 if (status->link) {
342 phy->speed = status->speed;
343 phy->duplex = status->duplex;
344 phy->pause = status->pause;
345 phy->asym_pause = status->asym_pause;
346 }
347
a7595121 348 of_node_get(np);
e5a03bfd 349 phy->mdio.dev.of_node = np;
5a11dd7d 350 phy->is_pseudo_fixed_link = true;
a7595121 351
34b31da4
AL
352 switch (status->speed) {
353 case SPEED_1000:
354 phy->supported = PHY_1000BT_FEATURES;
355 break;
356 case SPEED_100:
357 phy->supported = PHY_100BT_FEATURES;
358 break;
359 case SPEED_10:
360 default:
361 phy->supported = PHY_10BT_FEATURES;
362 }
363
a7595121
TP
364 ret = phy_device_register(phy);
365 if (ret) {
366 phy_device_free(phy);
367 of_node_put(np);
368 fixed_phy_del(phy_addr);
fd2ef0ba 369 return ERR_PTR(ret);
a7595121
TP
370 }
371
fd2ef0ba 372 return phy;
a7595121 373}
37e9a690 374EXPORT_SYMBOL_GPL(fixed_phy_register);
a7595121 375
5bcbe0f3
AL
376void fixed_phy_unregister(struct phy_device *phy)
377{
378 phy_device_remove(phy);
379
380 fixed_phy_del(phy->mdio.addr);
381}
382EXPORT_SYMBOL_GPL(fixed_phy_unregister);
383
a79d8e93
VB
384static int __init fixed_mdio_bus_init(void)
385{
386 struct fixed_mdio_bus *fmb = &platform_fmb;
387 int ret;
11b0bacd 388
a79d8e93 389 pdev = platform_device_register_simple("Fixed MDIO bus", 0, NULL, 0);
57401d5e
DC
390 if (IS_ERR(pdev)) {
391 ret = PTR_ERR(pdev);
a79d8e93
VB
392 goto err_pdev;
393 }
11b0bacd 394
298cf9be
LB
395 fmb->mii_bus = mdiobus_alloc();
396 if (fmb->mii_bus == NULL) {
397 ret = -ENOMEM;
398 goto err_mdiobus_reg;
399 }
11b0bacd 400
9e6c643b 401 snprintf(fmb->mii_bus->id, MII_BUS_ID_SIZE, "fixed-0");
298cf9be 402 fmb->mii_bus->name = "Fixed MDIO Bus";
ec2a5652 403 fmb->mii_bus->priv = fmb;
298cf9be
LB
404 fmb->mii_bus->parent = &pdev->dev;
405 fmb->mii_bus->read = &fixed_mdio_read;
406 fmb->mii_bus->write = &fixed_mdio_write;
298cf9be
LB
407
408 ret = mdiobus_register(fmb->mii_bus);
a79d8e93 409 if (ret)
298cf9be 410 goto err_mdiobus_alloc;
11b0bacd 411
a79d8e93 412 return 0;
11b0bacd 413
298cf9be
LB
414err_mdiobus_alloc:
415 mdiobus_free(fmb->mii_bus);
a79d8e93
VB
416err_mdiobus_reg:
417 platform_device_unregister(pdev);
418err_pdev:
419 return ret;
420}
421module_init(fixed_mdio_bus_init);
11b0bacd 422
a79d8e93
VB
423static void __exit fixed_mdio_bus_exit(void)
424{
425 struct fixed_mdio_bus *fmb = &platform_fmb;
651be3a2 426 struct fixed_phy *fp, *tmp;
11b0bacd 427
298cf9be
LB
428 mdiobus_unregister(fmb->mii_bus);
429 mdiobus_free(fmb->mii_bus);
a79d8e93 430 platform_device_unregister(pdev);
11b0bacd 431
651be3a2 432 list_for_each_entry_safe(fp, tmp, &fmb->phys, node) {
a79d8e93
VB
433 list_del(&fp->node);
434 kfree(fp);
7c32f470 435 }
69fc58a5 436 ida_destroy(&phy_fixed_ida);
11b0bacd 437}
a79d8e93 438module_exit(fixed_mdio_bus_exit);
11b0bacd 439
a79d8e93 440MODULE_DESCRIPTION("Fixed MDIO bus (MDIO bus emulation with fixed PHYs)");
11b0bacd
VB
441MODULE_AUTHOR("Vitaly Bordug");
442MODULE_LICENSE("GPL");
This page took 0.821062 seconds and 5 git commands to generate.