2 * Fixed MDIO bus (MDIO bus emulation with fixed PHYs)
7 * Copyright (c) 2006-2007 MontaVista Software, Inc.
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.
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/platform_device.h>
18 #include <linux/list.h>
19 #include <linux/mii.h>
20 #include <linux/phy.h>
21 #include <linux/phy_fixed.h>
22 #include <linux/err.h>
23 #include <linux/slab.h>
25 #include <linux/gpio.h>
27 #define MII_REGS_NUM 29
29 struct fixed_mdio_bus {
30 struct mii_bus *mii_bus;
31 struct list_head phys;
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;
44 static struct platform_device *pdev;
45 static struct fixed_mdio_bus platform_fmb = {
46 .phys = LIST_HEAD_INIT(platform_fmb.phys),
49 static int fixed_phy_update_regs(struct fixed_phy *fp)
51 u16 bmsr = BMSR_ANEGCAPABLE;
56 if (gpio_is_valid(fp->link_gpio))
57 fp->status.link = !!gpio_get_value_cansleep(fp->link_gpio);
59 if (fp->status.duplex) {
60 switch (fp->status.speed) {
74 switch (fp->status.speed) {
89 if (fp->status.link) {
90 bmsr |= BMSR_LSTATUS | BMSR_ANEGCOMPLETE;
92 if (fp->status.duplex) {
93 bmcr |= BMCR_FULLDPLX;
95 switch (fp->status.speed) {
97 bmcr |= BMCR_SPEED1000;
98 lpagb |= LPA_1000FULL;
101 bmcr |= BMCR_SPEED100;
108 pr_warn("fixed phy: unknown speed\n");
112 switch (fp->status.speed) {
114 bmcr |= BMCR_SPEED1000;
115 lpagb |= LPA_1000HALF;
118 bmcr |= BMCR_SPEED100;
125 pr_warn("fixed phy: unknown speed\n");
130 if (fp->status.pause)
131 lpa |= LPA_PAUSE_CAP;
133 if (fp->status.asym_pause)
134 lpa |= LPA_PAUSE_ASYM;
137 fp->regs[MII_PHYSID1] = 0;
138 fp->regs[MII_PHYSID2] = 0;
140 fp->regs[MII_BMSR] = bmsr;
141 fp->regs[MII_BMCR] = bmcr;
142 fp->regs[MII_LPA] = lpa;
143 fp->regs[MII_STAT1000] = lpagb;
148 static int fixed_mdio_read(struct mii_bus *bus, int phy_addr, int reg_num)
150 struct fixed_mdio_bus *fmb = bus->priv;
151 struct fixed_phy *fp;
153 if (reg_num >= MII_REGS_NUM)
156 /* We do not support emulating Clause 45 over Clause 22 register reads
157 * return an error instead of bogus data.
167 list_for_each_entry(fp, &fmb->phys, node) {
168 if (fp->addr == phy_addr) {
169 /* Issue callback if user registered it. */
170 if (fp->link_update) {
171 fp->link_update(fp->phydev->attached_dev,
173 fixed_phy_update_regs(fp);
175 return fp->regs[reg_num];
182 static int fixed_mdio_write(struct mii_bus *bus, int phy_addr, int reg_num,
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.
193 int fixed_phy_set_link_update(struct phy_device *phydev,
194 int (*link_update)(struct net_device *,
195 struct fixed_phy_status *))
197 struct fixed_mdio_bus *fmb = &platform_fmb;
198 struct fixed_phy *fp;
200 if (!phydev || !phydev->mdio.bus)
203 list_for_each_entry(fp, &fmb->phys, node) {
204 if (fp->addr == phydev->mdio.addr) {
205 fp->link_update = link_update;
213 EXPORT_SYMBOL_GPL(fixed_phy_set_link_update);
215 int fixed_phy_update_state(struct phy_device *phydev,
216 const struct fixed_phy_status *status,
217 const struct fixed_phy_status *changed)
219 struct fixed_mdio_bus *fmb = &platform_fmb;
220 struct fixed_phy *fp;
222 if (!phydev || phydev->mdio.bus != fmb->mii_bus)
225 list_for_each_entry(fp, &fmb->phys, node) {
226 if (fp->addr == phydev->mdio.addr) {
227 #define _UPD(x) if (changed->x) \
228 fp->status.x = status->x
235 fixed_phy_update_regs(fp);
242 EXPORT_SYMBOL(fixed_phy_update_state);
244 int fixed_phy_add(unsigned int irq, int phy_addr,
245 struct fixed_phy_status *status,
249 struct fixed_mdio_bus *fmb = &platform_fmb;
250 struct fixed_phy *fp;
252 fp = kzalloc(sizeof(*fp), GFP_KERNEL);
256 memset(fp->regs, 0xFF, sizeof(fp->regs[0]) * MII_REGS_NUM);
259 fmb->mii_bus->irq[phy_addr] = irq;
262 fp->status = *status;
263 fp->link_gpio = link_gpio;
265 if (gpio_is_valid(fp->link_gpio)) {
266 ret = gpio_request_one(fp->link_gpio, GPIOF_DIR_IN,
267 "fixed-link-gpio-link");
272 ret = fixed_phy_update_regs(fp);
276 list_add_tail(&fp->node, &fmb->phys);
281 if (gpio_is_valid(fp->link_gpio))
282 gpio_free(fp->link_gpio);
287 EXPORT_SYMBOL_GPL(fixed_phy_add);
289 static void fixed_phy_del(int phy_addr)
291 struct fixed_mdio_bus *fmb = &platform_fmb;
292 struct fixed_phy *fp, *tmp;
294 list_for_each_entry_safe(fp, tmp, &fmb->phys, node) {
295 if (fp->addr == phy_addr) {
297 if (gpio_is_valid(fp->link_gpio))
298 gpio_free(fp->link_gpio);
305 static int phy_fixed_addr;
306 static DEFINE_SPINLOCK(phy_fixed_addr_lock);
308 struct phy_device *fixed_phy_register(unsigned int irq,
309 struct fixed_phy_status *status,
311 struct device_node *np)
313 struct fixed_mdio_bus *fmb = &platform_fmb;
314 struct phy_device *phy;
318 if (!fmb->mii_bus || fmb->mii_bus->state != MDIOBUS_REGISTERED)
319 return ERR_PTR(-EPROBE_DEFER);
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);
325 return ERR_PTR(-ENOSPC);
327 phy_addr = phy_fixed_addr++;
328 spin_unlock(&phy_fixed_addr_lock);
330 ret = fixed_phy_add(irq, phy_addr, status, link_gpio);
334 phy = get_phy_device(fmb->mii_bus, phy_addr, false);
336 fixed_phy_del(phy_addr);
337 return ERR_PTR(-EINVAL);
340 /* propagate the fixed link values to struct phy_device */
341 phy->link = status->link;
343 phy->speed = status->speed;
344 phy->duplex = status->duplex;
345 phy->pause = status->pause;
346 phy->asym_pause = status->asym_pause;
350 phy->mdio.dev.of_node = np;
351 phy->is_pseudo_fixed_link = true;
353 switch (status->speed) {
355 phy->supported = PHY_1000BT_FEATURES;
358 phy->supported = PHY_100BT_FEATURES;
362 phy->supported = PHY_10BT_FEATURES;
365 ret = phy_device_register(phy);
367 phy_device_free(phy);
369 fixed_phy_del(phy_addr);
375 EXPORT_SYMBOL_GPL(fixed_phy_register);
377 void fixed_phy_unregister(struct phy_device *phy)
379 phy_device_remove(phy);
381 fixed_phy_del(phy->mdio.addr);
383 EXPORT_SYMBOL_GPL(fixed_phy_unregister);
385 static int __init fixed_mdio_bus_init(void)
387 struct fixed_mdio_bus *fmb = &platform_fmb;
390 pdev = platform_device_register_simple("Fixed MDIO bus", 0, NULL, 0);
396 fmb->mii_bus = mdiobus_alloc();
397 if (fmb->mii_bus == NULL) {
399 goto err_mdiobus_reg;
402 snprintf(fmb->mii_bus->id, MII_BUS_ID_SIZE, "fixed-0");
403 fmb->mii_bus->name = "Fixed MDIO Bus";
404 fmb->mii_bus->priv = fmb;
405 fmb->mii_bus->parent = &pdev->dev;
406 fmb->mii_bus->read = &fixed_mdio_read;
407 fmb->mii_bus->write = &fixed_mdio_write;
409 ret = mdiobus_register(fmb->mii_bus);
411 goto err_mdiobus_alloc;
416 mdiobus_free(fmb->mii_bus);
418 platform_device_unregister(pdev);
422 module_init(fixed_mdio_bus_init);
424 static void __exit fixed_mdio_bus_exit(void)
426 struct fixed_mdio_bus *fmb = &platform_fmb;
427 struct fixed_phy *fp, *tmp;
429 mdiobus_unregister(fmb->mii_bus);
430 mdiobus_free(fmb->mii_bus);
431 platform_device_unregister(pdev);
433 list_for_each_entry_safe(fp, tmp, &fmb->phys, node) {
438 module_exit(fixed_mdio_bus_exit);
440 MODULE_DESCRIPTION("Fixed MDIO bus (MDIO bus emulation with fixed PHYs)");
441 MODULE_AUTHOR("Vitaly Bordug");
442 MODULE_LICENSE("GPL");