2 * Fixed MDIO bus (MDIO bus emulation with fixed PHYs)
4 * Author: Vitaly Bordug <vbordug@ru.mvista.com>
5 * Anton Vorontsov <avorontsov@ru.mvista.com>
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>
29 struct fixed_mdio_bus {
30 struct mii_bus *mii_bus;
31 struct list_head phys;
36 struct phy_device *phydev;
37 struct fixed_phy_status status;
38 int (*link_update)(struct net_device *, struct fixed_phy_status *);
39 struct list_head node;
43 static struct platform_device *pdev;
44 static struct fixed_mdio_bus platform_fmb = {
45 .phys = LIST_HEAD_INIT(platform_fmb.phys),
48 static void fixed_phy_update(struct fixed_phy *fp)
50 if (gpio_is_valid(fp->link_gpio))
51 fp->status.link = !!gpio_get_value_cansleep(fp->link_gpio);
54 static int fixed_mdio_read(struct mii_bus *bus, int phy_addr, int reg_num)
56 struct fixed_mdio_bus *fmb = bus->priv;
59 list_for_each_entry(fp, &fmb->phys, node) {
60 if (fp->addr == phy_addr) {
61 /* Issue callback if user registered it. */
62 if (fp->link_update) {
63 fp->link_update(fp->phydev->attached_dev,
67 return swphy_read_reg(reg_num, &fp->status);
74 static int fixed_mdio_write(struct mii_bus *bus, int phy_addr, int reg_num,
81 * If something weird is required to be done with link/speed,
82 * network driver is able to assign a function to implement this.
83 * May be useful for PHY's that need to be software-driven.
85 int fixed_phy_set_link_update(struct phy_device *phydev,
86 int (*link_update)(struct net_device *,
87 struct fixed_phy_status *))
89 struct fixed_mdio_bus *fmb = &platform_fmb;
92 if (!phydev || !phydev->mdio.bus)
95 list_for_each_entry(fp, &fmb->phys, node) {
96 if (fp->addr == phydev->mdio.addr) {
97 fp->link_update = link_update;
105 EXPORT_SYMBOL_GPL(fixed_phy_set_link_update);
107 int fixed_phy_update_state(struct phy_device *phydev,
108 const struct fixed_phy_status *status,
109 const struct fixed_phy_status *changed)
111 struct fixed_mdio_bus *fmb = &platform_fmb;
112 struct fixed_phy *fp;
114 if (!phydev || phydev->mdio.bus != fmb->mii_bus)
117 list_for_each_entry(fp, &fmb->phys, node) {
118 if (fp->addr == phydev->mdio.addr) {
119 #define _UPD(x) if (changed->x) \
120 fp->status.x = status->x
127 fixed_phy_update(fp);
134 EXPORT_SYMBOL(fixed_phy_update_state);
136 int fixed_phy_add(unsigned int irq, int phy_addr,
137 struct fixed_phy_status *status,
141 struct fixed_mdio_bus *fmb = &platform_fmb;
142 struct fixed_phy *fp;
144 ret = swphy_validate_state(status);
148 fp = kzalloc(sizeof(*fp), GFP_KERNEL);
153 fmb->mii_bus->irq[phy_addr] = irq;
156 fp->status = *status;
157 fp->link_gpio = link_gpio;
159 if (gpio_is_valid(fp->link_gpio)) {
160 ret = gpio_request_one(fp->link_gpio, GPIOF_DIR_IN,
161 "fixed-link-gpio-link");
166 fixed_phy_update(fp);
168 list_add_tail(&fp->node, &fmb->phys);
176 EXPORT_SYMBOL_GPL(fixed_phy_add);
178 static void fixed_phy_del(int phy_addr)
180 struct fixed_mdio_bus *fmb = &platform_fmb;
181 struct fixed_phy *fp, *tmp;
183 list_for_each_entry_safe(fp, tmp, &fmb->phys, node) {
184 if (fp->addr == phy_addr) {
186 if (gpio_is_valid(fp->link_gpio))
187 gpio_free(fp->link_gpio);
194 static int phy_fixed_addr;
195 static DEFINE_SPINLOCK(phy_fixed_addr_lock);
197 struct phy_device *fixed_phy_register(unsigned int irq,
198 struct fixed_phy_status *status,
200 struct device_node *np)
202 struct fixed_mdio_bus *fmb = &platform_fmb;
203 struct phy_device *phy;
207 if (!fmb->mii_bus || fmb->mii_bus->state != MDIOBUS_REGISTERED)
208 return ERR_PTR(-EPROBE_DEFER);
210 /* Get the next available PHY address, up to PHY_MAX_ADDR */
211 spin_lock(&phy_fixed_addr_lock);
212 if (phy_fixed_addr == PHY_MAX_ADDR) {
213 spin_unlock(&phy_fixed_addr_lock);
214 return ERR_PTR(-ENOSPC);
216 phy_addr = phy_fixed_addr++;
217 spin_unlock(&phy_fixed_addr_lock);
219 ret = fixed_phy_add(irq, phy_addr, status, link_gpio);
223 phy = get_phy_device(fmb->mii_bus, phy_addr, false);
225 fixed_phy_del(phy_addr);
226 return ERR_PTR(-EINVAL);
229 /* propagate the fixed link values to struct phy_device */
230 phy->link = status->link;
232 phy->speed = status->speed;
233 phy->duplex = status->duplex;
234 phy->pause = status->pause;
235 phy->asym_pause = status->asym_pause;
239 phy->mdio.dev.of_node = np;
240 phy->is_pseudo_fixed_link = true;
242 switch (status->speed) {
244 phy->supported = PHY_1000BT_FEATURES;
247 phy->supported = PHY_100BT_FEATURES;
251 phy->supported = PHY_10BT_FEATURES;
254 ret = phy_device_register(phy);
256 phy_device_free(phy);
258 fixed_phy_del(phy_addr);
264 EXPORT_SYMBOL_GPL(fixed_phy_register);
266 void fixed_phy_unregister(struct phy_device *phy)
268 phy_device_remove(phy);
270 fixed_phy_del(phy->mdio.addr);
272 EXPORT_SYMBOL_GPL(fixed_phy_unregister);
274 static int __init fixed_mdio_bus_init(void)
276 struct fixed_mdio_bus *fmb = &platform_fmb;
279 pdev = platform_device_register_simple("Fixed MDIO bus", 0, NULL, 0);
285 fmb->mii_bus = mdiobus_alloc();
286 if (fmb->mii_bus == NULL) {
288 goto err_mdiobus_reg;
291 snprintf(fmb->mii_bus->id, MII_BUS_ID_SIZE, "fixed-0");
292 fmb->mii_bus->name = "Fixed MDIO Bus";
293 fmb->mii_bus->priv = fmb;
294 fmb->mii_bus->parent = &pdev->dev;
295 fmb->mii_bus->read = &fixed_mdio_read;
296 fmb->mii_bus->write = &fixed_mdio_write;
298 ret = mdiobus_register(fmb->mii_bus);
300 goto err_mdiobus_alloc;
305 mdiobus_free(fmb->mii_bus);
307 platform_device_unregister(pdev);
311 module_init(fixed_mdio_bus_init);
313 static void __exit fixed_mdio_bus_exit(void)
315 struct fixed_mdio_bus *fmb = &platform_fmb;
316 struct fixed_phy *fp, *tmp;
318 mdiobus_unregister(fmb->mii_bus);
319 mdiobus_free(fmb->mii_bus);
320 platform_device_unregister(pdev);
322 list_for_each_entry_safe(fp, tmp, &fmb->phys, node) {
327 module_exit(fixed_mdio_bus_exit);
329 MODULE_DESCRIPTION("Fixed MDIO bus (MDIO bus emulation with fixed PHYs)");
330 MODULE_AUTHOR("Vitaly Bordug");
331 MODULE_LICENSE("GPL");