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