1 // SPDX-License-Identifier: GPL-2.0+
3 * Fixed MDIO bus (MDIO bus emulation with fixed PHYs)
5 * Author: Vitaly Bordug <vbordug@ru.mvista.com>
6 * Anton Vorontsov <avorontsov@ru.mvista.com>
8 * Copyright (c) 2006-2007 MontaVista Software, Inc.
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/platform_device.h>
14 #include <linux/list.h>
15 #include <linux/mii.h>
16 #include <linux/phy.h>
17 #include <linux/phy_fixed.h>
18 #include <linux/err.h>
19 #include <linux/slab.h>
21 #include <linux/gpio.h>
22 #include <linux/seqlock.h>
23 #include <linux/idr.h>
24 #include <linux/netdevice.h>
28 struct fixed_mdio_bus {
29 struct mii_bus *mii_bus;
30 struct list_head phys;
35 struct phy_device *phydev;
37 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 int fixed_phy_change_carrier(struct net_device *dev, bool new_carrier)
51 struct fixed_mdio_bus *fmb = &platform_fmb;
52 struct phy_device *phydev = dev->phydev;
55 if (!phydev || !phydev->mdio.bus)
58 list_for_each_entry(fp, &fmb->phys, node) {
59 if (fp->addr == phydev->mdio.addr) {
60 fp->no_carrier = !new_carrier;
66 EXPORT_SYMBOL_GPL(fixed_phy_change_carrier);
68 static void fixed_phy_update(struct fixed_phy *fp)
70 if (!fp->no_carrier && gpio_is_valid(fp->link_gpio))
71 fp->status.link = !!gpio_get_value_cansleep(fp->link_gpio);
74 static int fixed_mdio_read(struct mii_bus *bus, int phy_addr, int reg_num)
76 struct fixed_mdio_bus *fmb = bus->priv;
79 list_for_each_entry(fp, &fmb->phys, node) {
80 if (fp->addr == phy_addr) {
81 struct fixed_phy_status state;
85 s = read_seqcount_begin(&fp->seqcount);
86 fp->status.link = !fp->no_carrier;
87 /* Issue callback if user registered it. */
88 if (fp->link_update) {
89 fp->link_update(fp->phydev->attached_dev,
94 } while (read_seqcount_retry(&fp->seqcount, s));
96 return swphy_read_reg(reg_num, &state);
103 static int fixed_mdio_write(struct mii_bus *bus, int phy_addr, int reg_num,
110 * If something weird is required to be done with link/speed,
111 * network driver is able to assign a function to implement this.
112 * May be useful for PHY's that need to be software-driven.
114 int fixed_phy_set_link_update(struct phy_device *phydev,
115 int (*link_update)(struct net_device *,
116 struct fixed_phy_status *))
118 struct fixed_mdio_bus *fmb = &platform_fmb;
119 struct fixed_phy *fp;
121 if (!phydev || !phydev->mdio.bus)
124 list_for_each_entry(fp, &fmb->phys, node) {
125 if (fp->addr == phydev->mdio.addr) {
126 fp->link_update = link_update;
134 EXPORT_SYMBOL_GPL(fixed_phy_set_link_update);
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);
152 seqcount_init(&fp->seqcount);
155 fmb->mii_bus->irq[phy_addr] = irq;
158 fp->status = *status;
159 fp->link_gpio = link_gpio;
161 if (gpio_is_valid(fp->link_gpio)) {
162 ret = gpio_request_one(fp->link_gpio, GPIOF_DIR_IN,
163 "fixed-link-gpio-link");
168 fixed_phy_update(fp);
170 list_add_tail(&fp->node, &fmb->phys);
178 EXPORT_SYMBOL_GPL(fixed_phy_add);
180 static DEFINE_IDA(phy_fixed_ida);
182 static void fixed_phy_del(int phy_addr)
184 struct fixed_mdio_bus *fmb = &platform_fmb;
185 struct fixed_phy *fp, *tmp;
187 list_for_each_entry_safe(fp, tmp, &fmb->phys, node) {
188 if (fp->addr == phy_addr) {
190 if (gpio_is_valid(fp->link_gpio))
191 gpio_free(fp->link_gpio);
193 ida_simple_remove(&phy_fixed_ida, phy_addr);
199 struct phy_device *fixed_phy_register(unsigned int irq,
200 struct fixed_phy_status *status,
202 struct device_node *np)
204 struct fixed_mdio_bus *fmb = &platform_fmb;
205 struct phy_device *phy;
209 if (!fmb->mii_bus || fmb->mii_bus->state != MDIOBUS_REGISTERED)
210 return ERR_PTR(-EPROBE_DEFER);
212 /* Get the next available PHY address, up to PHY_MAX_ADDR */
213 phy_addr = ida_simple_get(&phy_fixed_ida, 0, PHY_MAX_ADDR, GFP_KERNEL);
215 return ERR_PTR(phy_addr);
217 ret = fixed_phy_add(irq, phy_addr, status, link_gpio);
219 ida_simple_remove(&phy_fixed_ida, phy_addr);
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 linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
246 linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
250 linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT,
252 linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT,
257 linkmode_set_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT,
259 linkmode_set_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT,
263 ret = phy_device_register(phy);
265 phy_device_free(phy);
267 fixed_phy_del(phy_addr);
273 EXPORT_SYMBOL_GPL(fixed_phy_register);
275 void fixed_phy_unregister(struct phy_device *phy)
277 phy_device_remove(phy);
278 of_node_put(phy->mdio.dev.of_node);
279 fixed_phy_del(phy->mdio.addr);
281 EXPORT_SYMBOL_GPL(fixed_phy_unregister);
283 static int __init fixed_mdio_bus_init(void)
285 struct fixed_mdio_bus *fmb = &platform_fmb;
288 pdev = platform_device_register_simple("Fixed MDIO bus", 0, NULL, 0);
290 return PTR_ERR(pdev);
292 fmb->mii_bus = mdiobus_alloc();
293 if (fmb->mii_bus == NULL) {
295 goto err_mdiobus_reg;
298 snprintf(fmb->mii_bus->id, MII_BUS_ID_SIZE, "fixed-0");
299 fmb->mii_bus->name = "Fixed MDIO Bus";
300 fmb->mii_bus->priv = fmb;
301 fmb->mii_bus->parent = &pdev->dev;
302 fmb->mii_bus->read = &fixed_mdio_read;
303 fmb->mii_bus->write = &fixed_mdio_write;
305 ret = mdiobus_register(fmb->mii_bus);
307 goto err_mdiobus_alloc;
312 mdiobus_free(fmb->mii_bus);
314 platform_device_unregister(pdev);
317 module_init(fixed_mdio_bus_init);
319 static void __exit fixed_mdio_bus_exit(void)
321 struct fixed_mdio_bus *fmb = &platform_fmb;
322 struct fixed_phy *fp, *tmp;
324 mdiobus_unregister(fmb->mii_bus);
325 mdiobus_free(fmb->mii_bus);
326 platform_device_unregister(pdev);
328 list_for_each_entry_safe(fp, tmp, &fmb->phys, node) {
332 ida_destroy(&phy_fixed_ida);
334 module_exit(fixed_mdio_bus_exit);
336 MODULE_DESCRIPTION("Fixed MDIO bus (MDIO bus emulation with fixed PHYs)");
337 MODULE_AUTHOR("Vitaly Bordug");
338 MODULE_LICENSE("GPL");