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/consumer.h>
22 #include <linux/idr.h>
23 #include <linux/netdevice.h>
24 #include <linux/linkmode.h>
28 struct fixed_mdio_bus
{
29 struct mii_bus
*mii_bus
;
30 struct list_head phys
;
35 struct phy_device
*phydev
;
36 struct fixed_phy_status status
;
38 int (*link_update
)(struct net_device
*, struct fixed_phy_status
*);
39 struct list_head node
;
40 struct gpio_desc
*link_gpiod
;
43 static struct platform_device
*pdev
;
44 static struct fixed_mdio_bus platform_fmb
= {
45 .phys
= LIST_HEAD_INIT(platform_fmb
.phys
),
48 int fixed_phy_change_carrier(struct net_device
*dev
, bool new_carrier
)
50 struct fixed_mdio_bus
*fmb
= &platform_fmb
;
51 struct phy_device
*phydev
= dev
->phydev
;
54 if (!phydev
|| !phydev
->mdio
.bus
)
57 list_for_each_entry(fp
, &fmb
->phys
, node
) {
58 if (fp
->addr
== phydev
->mdio
.addr
) {
59 fp
->no_carrier
= !new_carrier
;
65 EXPORT_SYMBOL_GPL(fixed_phy_change_carrier
);
67 static void fixed_phy_update(struct fixed_phy
*fp
)
69 if (!fp
->no_carrier
&& fp
->link_gpiod
)
70 fp
->status
.link
= !!gpiod_get_value_cansleep(fp
->link_gpiod
);
73 static int fixed_mdio_read(struct mii_bus
*bus
, int phy_addr
, int reg_num
)
75 struct fixed_mdio_bus
*fmb
= bus
->priv
;
78 list_for_each_entry(fp
, &fmb
->phys
, node
) {
79 if (fp
->addr
== phy_addr
) {
80 struct fixed_phy_status state
;
82 fp
->status
.link
= !fp
->no_carrier
;
84 /* Issue callback if user registered it. */
86 fp
->link_update(fp
->phydev
->attached_dev
,
89 /* Check the GPIO for change in status */
93 return swphy_read_reg(reg_num
, &state
);
100 static int fixed_mdio_write(struct mii_bus
*bus
, int phy_addr
, int reg_num
,
107 * If something weird is required to be done with link/speed,
108 * network driver is able to assign a function to implement this.
109 * May be useful for PHY's that need to be software-driven.
111 int fixed_phy_set_link_update(struct phy_device
*phydev
,
112 int (*link_update
)(struct net_device
*,
113 struct fixed_phy_status
*))
115 struct fixed_mdio_bus
*fmb
= &platform_fmb
;
116 struct fixed_phy
*fp
;
118 if (!phydev
|| !phydev
->mdio
.bus
)
121 list_for_each_entry(fp
, &fmb
->phys
, node
) {
122 if (fp
->addr
== phydev
->mdio
.addr
) {
123 fp
->link_update
= link_update
;
131 EXPORT_SYMBOL_GPL(fixed_phy_set_link_update
);
133 static int fixed_phy_add_gpiod(unsigned int irq
, int phy_addr
,
134 struct fixed_phy_status
*status
,
135 struct gpio_desc
*gpiod
)
138 struct fixed_mdio_bus
*fmb
= &platform_fmb
;
139 struct fixed_phy
*fp
;
141 ret
= swphy_validate_state(status
);
145 fp
= kzalloc(sizeof(*fp
), GFP_KERNEL
);
150 fmb
->mii_bus
->irq
[phy_addr
] = irq
;
153 fp
->status
= *status
;
154 fp
->link_gpiod
= gpiod
;
156 fixed_phy_update(fp
);
158 list_add_tail(&fp
->node
, &fmb
->phys
);
163 int fixed_phy_add(unsigned int irq
, int phy_addr
,
164 struct fixed_phy_status
*status
)
166 return fixed_phy_add_gpiod(irq
, phy_addr
, status
, NULL
);
168 EXPORT_SYMBOL_GPL(fixed_phy_add
);
170 static DEFINE_IDA(phy_fixed_ida
);
172 static void fixed_phy_del(int phy_addr
)
174 struct fixed_mdio_bus
*fmb
= &platform_fmb
;
175 struct fixed_phy
*fp
, *tmp
;
177 list_for_each_entry_safe(fp
, tmp
, &fmb
->phys
, node
) {
178 if (fp
->addr
== phy_addr
) {
181 gpiod_put(fp
->link_gpiod
);
183 ida_free(&phy_fixed_ida
, phy_addr
);
189 #ifdef CONFIG_OF_GPIO
190 static struct gpio_desc
*fixed_phy_get_gpiod(struct device_node
*np
)
192 struct device_node
*fixed_link_node
;
193 struct gpio_desc
*gpiod
;
198 fixed_link_node
= of_get_child_by_name(np
, "fixed-link");
199 if (!fixed_link_node
)
203 * As the fixed link is just a device tree node without any
204 * Linux device associated with it, we simply have obtain
205 * the GPIO descriptor from the device tree like this.
207 gpiod
= fwnode_gpiod_get_index(of_fwnode_handle(fixed_link_node
),
208 "link", 0, GPIOD_IN
, "mdio");
209 if (IS_ERR(gpiod
) && PTR_ERR(gpiod
) != -EPROBE_DEFER
) {
210 if (PTR_ERR(gpiod
) != -ENOENT
)
211 pr_err("error getting GPIO for fixed link %pOF, proceed without\n",
215 of_node_put(fixed_link_node
);
220 static struct gpio_desc
*fixed_phy_get_gpiod(struct device_node
*np
)
226 static struct phy_device
*__fixed_phy_register(unsigned int irq
,
227 struct fixed_phy_status
*status
,
228 struct device_node
*np
,
229 struct gpio_desc
*gpiod
)
231 struct fixed_mdio_bus
*fmb
= &platform_fmb
;
232 struct phy_device
*phy
;
236 if (!fmb
->mii_bus
|| fmb
->mii_bus
->state
!= MDIOBUS_REGISTERED
)
237 return ERR_PTR(-EPROBE_DEFER
);
239 /* Check if we have a GPIO associated with this fixed phy */
241 gpiod
= fixed_phy_get_gpiod(np
);
243 return ERR_CAST(gpiod
);
246 /* Get the next available PHY address, up to PHY_MAX_ADDR */
247 phy_addr
= ida_alloc_max(&phy_fixed_ida
, PHY_MAX_ADDR
- 1, GFP_KERNEL
);
249 return ERR_PTR(phy_addr
);
251 ret
= fixed_phy_add_gpiod(irq
, phy_addr
, status
, gpiod
);
253 ida_free(&phy_fixed_ida
, phy_addr
);
257 phy
= get_phy_device(fmb
->mii_bus
, phy_addr
, false);
259 fixed_phy_del(phy_addr
);
260 return ERR_PTR(-EINVAL
);
263 /* propagate the fixed link values to struct phy_device */
264 phy
->link
= status
->link
;
266 phy
->speed
= status
->speed
;
267 phy
->duplex
= status
->duplex
;
268 phy
->pause
= status
->pause
;
269 phy
->asym_pause
= status
->asym_pause
;
273 phy
->mdio
.dev
.of_node
= np
;
274 phy
->is_pseudo_fixed_link
= true;
276 switch (status
->speed
) {
278 linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT
,
280 linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT
,
284 linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT
,
286 linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT
,
291 linkmode_set_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT
,
293 linkmode_set_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT
,
297 phy_advertise_supported(phy
);
299 ret
= phy_device_register(phy
);
301 phy_device_free(phy
);
303 fixed_phy_del(phy_addr
);
310 struct phy_device
*fixed_phy_register(unsigned int irq
,
311 struct fixed_phy_status
*status
,
312 struct device_node
*np
)
314 return __fixed_phy_register(irq
, status
, np
, NULL
);
316 EXPORT_SYMBOL_GPL(fixed_phy_register
);
319 fixed_phy_register_with_gpiod(unsigned int irq
,
320 struct fixed_phy_status
*status
,
321 struct gpio_desc
*gpiod
)
323 return __fixed_phy_register(irq
, status
, NULL
, gpiod
);
325 EXPORT_SYMBOL_GPL(fixed_phy_register_with_gpiod
);
327 void fixed_phy_unregister(struct phy_device
*phy
)
329 phy_device_remove(phy
);
330 of_node_put(phy
->mdio
.dev
.of_node
);
331 fixed_phy_del(phy
->mdio
.addr
);
333 EXPORT_SYMBOL_GPL(fixed_phy_unregister
);
335 static int __init
fixed_mdio_bus_init(void)
337 struct fixed_mdio_bus
*fmb
= &platform_fmb
;
340 pdev
= platform_device_register_simple("Fixed MDIO bus", 0, NULL
, 0);
342 return PTR_ERR(pdev
);
344 fmb
->mii_bus
= mdiobus_alloc();
345 if (fmb
->mii_bus
== NULL
) {
347 goto err_mdiobus_reg
;
350 snprintf(fmb
->mii_bus
->id
, MII_BUS_ID_SIZE
, "fixed-0");
351 fmb
->mii_bus
->name
= "Fixed MDIO Bus";
352 fmb
->mii_bus
->priv
= fmb
;
353 fmb
->mii_bus
->parent
= &pdev
->dev
;
354 fmb
->mii_bus
->read
= &fixed_mdio_read
;
355 fmb
->mii_bus
->write
= &fixed_mdio_write
;
356 fmb
->mii_bus
->phy_mask
= ~0;
358 ret
= mdiobus_register(fmb
->mii_bus
);
360 goto err_mdiobus_alloc
;
365 mdiobus_free(fmb
->mii_bus
);
367 platform_device_unregister(pdev
);
370 module_init(fixed_mdio_bus_init
);
372 static void __exit
fixed_mdio_bus_exit(void)
374 struct fixed_mdio_bus
*fmb
= &platform_fmb
;
375 struct fixed_phy
*fp
, *tmp
;
377 mdiobus_unregister(fmb
->mii_bus
);
378 mdiobus_free(fmb
->mii_bus
);
379 platform_device_unregister(pdev
);
381 list_for_each_entry_safe(fp
, tmp
, &fmb
->phys
, node
) {
385 ida_destroy(&phy_fixed_ida
);
387 module_exit(fixed_mdio_bus_exit
);
389 MODULE_DESCRIPTION("Fixed MDIO bus (MDIO bus emulation with fixed PHYs)");
390 MODULE_AUTHOR("Vitaly Bordug");
391 MODULE_LICENSE("GPL");