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