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