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>
26 #include <linux/seqlock.h>
27 #include <linux/idr.h>
31 struct fixed_mdio_bus
{
32 struct mii_bus
*mii_bus
;
33 struct list_head phys
;
38 struct phy_device
*phydev
;
40 struct fixed_phy_status status
;
41 int (*link_update
)(struct net_device
*, struct fixed_phy_status
*);
42 struct list_head node
;
46 static struct platform_device
*pdev
;
47 static struct fixed_mdio_bus platform_fmb
= {
48 .phys
= LIST_HEAD_INIT(platform_fmb
.phys
),
51 static void fixed_phy_update(struct fixed_phy
*fp
)
53 if (gpio_is_valid(fp
->link_gpio
))
54 fp
->status
.link
= !!gpio_get_value_cansleep(fp
->link_gpio
);
57 static int fixed_mdio_read(struct mii_bus
*bus
, int phy_addr
, int reg_num
)
59 struct fixed_mdio_bus
*fmb
= bus
->priv
;
62 list_for_each_entry(fp
, &fmb
->phys
, node
) {
63 if (fp
->addr
== phy_addr
) {
64 struct fixed_phy_status state
;
68 s
= read_seqcount_begin(&fp
->seqcount
);
69 /* Issue callback if user registered it. */
70 if (fp
->link_update
) {
71 fp
->link_update(fp
->phydev
->attached_dev
,
76 } while (read_seqcount_retry(&fp
->seqcount
, s
));
78 return swphy_read_reg(reg_num
, &state
);
85 static int fixed_mdio_write(struct mii_bus
*bus
, int phy_addr
, int reg_num
,
92 * If something weird is required to be done with link/speed,
93 * network driver is able to assign a function to implement this.
94 * May be useful for PHY's that need to be software-driven.
96 int fixed_phy_set_link_update(struct phy_device
*phydev
,
97 int (*link_update
)(struct net_device
*,
98 struct fixed_phy_status
*))
100 struct fixed_mdio_bus
*fmb
= &platform_fmb
;
101 struct fixed_phy
*fp
;
103 if (!phydev
|| !phydev
->mdio
.bus
)
106 list_for_each_entry(fp
, &fmb
->phys
, node
) {
107 if (fp
->addr
== phydev
->mdio
.addr
) {
108 fp
->link_update
= link_update
;
116 EXPORT_SYMBOL_GPL(fixed_phy_set_link_update
);
118 int fixed_phy_add(unsigned int irq
, int phy_addr
,
119 struct fixed_phy_status
*status
,
123 struct fixed_mdio_bus
*fmb
= &platform_fmb
;
124 struct fixed_phy
*fp
;
126 ret
= swphy_validate_state(status
);
130 fp
= kzalloc(sizeof(*fp
), GFP_KERNEL
);
134 seqcount_init(&fp
->seqcount
);
137 fmb
->mii_bus
->irq
[phy_addr
] = irq
;
140 fp
->status
= *status
;
141 fp
->link_gpio
= link_gpio
;
143 if (gpio_is_valid(fp
->link_gpio
)) {
144 ret
= gpio_request_one(fp
->link_gpio
, GPIOF_DIR_IN
,
145 "fixed-link-gpio-link");
150 fixed_phy_update(fp
);
152 list_add_tail(&fp
->node
, &fmb
->phys
);
160 EXPORT_SYMBOL_GPL(fixed_phy_add
);
162 static DEFINE_IDA(phy_fixed_ida
);
164 static void fixed_phy_del(int phy_addr
)
166 struct fixed_mdio_bus
*fmb
= &platform_fmb
;
167 struct fixed_phy
*fp
, *tmp
;
169 list_for_each_entry_safe(fp
, tmp
, &fmb
->phys
, node
) {
170 if (fp
->addr
== phy_addr
) {
172 if (gpio_is_valid(fp
->link_gpio
))
173 gpio_free(fp
->link_gpio
);
175 ida_simple_remove(&phy_fixed_ida
, phy_addr
);
181 struct phy_device
*fixed_phy_register(unsigned int irq
,
182 struct fixed_phy_status
*status
,
184 struct device_node
*np
)
186 struct fixed_mdio_bus
*fmb
= &platform_fmb
;
187 struct phy_device
*phy
;
191 if (!fmb
->mii_bus
|| fmb
->mii_bus
->state
!= MDIOBUS_REGISTERED
)
192 return ERR_PTR(-EPROBE_DEFER
);
194 /* Get the next available PHY address, up to PHY_MAX_ADDR */
195 phy_addr
= ida_simple_get(&phy_fixed_ida
, 0, PHY_MAX_ADDR
, GFP_KERNEL
);
197 return ERR_PTR(phy_addr
);
199 ret
= fixed_phy_add(irq
, phy_addr
, status
, link_gpio
);
201 ida_simple_remove(&phy_fixed_ida
, phy_addr
);
205 phy
= get_phy_device(fmb
->mii_bus
, phy_addr
, false);
207 fixed_phy_del(phy_addr
);
208 return ERR_PTR(-EINVAL
);
211 /* propagate the fixed link values to struct phy_device */
212 phy
->link
= status
->link
;
214 phy
->speed
= status
->speed
;
215 phy
->duplex
= status
->duplex
;
216 phy
->pause
= status
->pause
;
217 phy
->asym_pause
= status
->asym_pause
;
221 phy
->mdio
.dev
.of_node
= np
;
222 phy
->is_pseudo_fixed_link
= true;
224 switch (status
->speed
) {
226 phy
->supported
= PHY_1000BT_FEATURES
;
229 phy
->supported
= PHY_100BT_FEATURES
;
233 phy
->supported
= PHY_10BT_FEATURES
;
236 ret
= phy_device_register(phy
);
238 phy_device_free(phy
);
240 fixed_phy_del(phy_addr
);
246 EXPORT_SYMBOL_GPL(fixed_phy_register
);
248 void fixed_phy_unregister(struct phy_device
*phy
)
250 phy_device_remove(phy
);
251 of_node_put(phy
->mdio
.dev
.of_node
);
252 fixed_phy_del(phy
->mdio
.addr
);
254 EXPORT_SYMBOL_GPL(fixed_phy_unregister
);
256 static int __init
fixed_mdio_bus_init(void)
258 struct fixed_mdio_bus
*fmb
= &platform_fmb
;
261 pdev
= platform_device_register_simple("Fixed MDIO bus", 0, NULL
, 0);
267 fmb
->mii_bus
= mdiobus_alloc();
268 if (fmb
->mii_bus
== NULL
) {
270 goto err_mdiobus_reg
;
273 snprintf(fmb
->mii_bus
->id
, MII_BUS_ID_SIZE
, "fixed-0");
274 fmb
->mii_bus
->name
= "Fixed MDIO Bus";
275 fmb
->mii_bus
->priv
= fmb
;
276 fmb
->mii_bus
->parent
= &pdev
->dev
;
277 fmb
->mii_bus
->read
= &fixed_mdio_read
;
278 fmb
->mii_bus
->write
= &fixed_mdio_write
;
280 ret
= mdiobus_register(fmb
->mii_bus
);
282 goto err_mdiobus_alloc
;
287 mdiobus_free(fmb
->mii_bus
);
289 platform_device_unregister(pdev
);
293 module_init(fixed_mdio_bus_init
);
295 static void __exit
fixed_mdio_bus_exit(void)
297 struct fixed_mdio_bus
*fmb
= &platform_fmb
;
298 struct fixed_phy
*fp
, *tmp
;
300 mdiobus_unregister(fmb
->mii_bus
);
301 mdiobus_free(fmb
->mii_bus
);
302 platform_device_unregister(pdev
);
304 list_for_each_entry_safe(fp
, tmp
, &fmb
->phys
, node
) {
308 ida_destroy(&phy_fixed_ida
);
310 module_exit(fixed_mdio_bus_exit
);
312 MODULE_DESCRIPTION("Fixed MDIO bus (MDIO bus emulation with fixed PHYs)");
313 MODULE_AUTHOR("Vitaly Bordug");
314 MODULE_LICENSE("GPL");