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 #define MII_REGS_NUM 29
31 struct fixed_mdio_bus
{
32 struct mii_bus
*mii_bus
;
33 struct list_head phys
;
38 u16 regs
[MII_REGS_NUM
];
39 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_regs(struct fixed_phy
*fp
)
53 if (gpio_is_valid(fp
->link_gpio
))
54 fp
->status
.link
= !!gpio_get_value_cansleep(fp
->link_gpio
);
56 swphy_update_regs(fp
->regs
, &fp
->status
);
59 static int fixed_mdio_read(struct mii_bus
*bus
, int phy_addr
, int reg_num
)
61 struct fixed_mdio_bus
*fmb
= bus
->priv
;
64 if (reg_num
>= MII_REGS_NUM
)
67 /* We do not support emulating Clause 45 over Clause 22 register reads
68 * return an error instead of bogus data.
78 list_for_each_entry(fp
, &fmb
->phys
, node
) {
79 if (fp
->addr
== phy_addr
) {
80 /* Issue callback if user registered it. */
81 if (fp
->link_update
) {
82 fp
->link_update(fp
->phydev
->attached_dev
,
84 fixed_phy_update_regs(fp
);
86 return fp
->regs
[reg_num
];
93 static int fixed_mdio_write(struct mii_bus
*bus
, int phy_addr
, int reg_num
,
100 * If something weird is required to be done with link/speed,
101 * network driver is able to assign a function to implement this.
102 * May be useful for PHY's that need to be software-driven.
104 int fixed_phy_set_link_update(struct phy_device
*phydev
,
105 int (*link_update
)(struct net_device
*,
106 struct fixed_phy_status
*))
108 struct fixed_mdio_bus
*fmb
= &platform_fmb
;
109 struct fixed_phy
*fp
;
111 if (!phydev
|| !phydev
->mdio
.bus
)
114 list_for_each_entry(fp
, &fmb
->phys
, node
) {
115 if (fp
->addr
== phydev
->mdio
.addr
) {
116 fp
->link_update
= link_update
;
124 EXPORT_SYMBOL_GPL(fixed_phy_set_link_update
);
126 int fixed_phy_update_state(struct phy_device
*phydev
,
127 const struct fixed_phy_status
*status
,
128 const struct fixed_phy_status
*changed
)
130 struct fixed_mdio_bus
*fmb
= &platform_fmb
;
131 struct fixed_phy
*fp
;
133 if (!phydev
|| phydev
->mdio
.bus
!= fmb
->mii_bus
)
136 list_for_each_entry(fp
, &fmb
->phys
, node
) {
137 if (fp
->addr
== phydev
->mdio
.addr
) {
138 #define _UPD(x) if (changed->x) \
139 fp->status.x = status->x
146 fixed_phy_update_regs(fp
);
153 EXPORT_SYMBOL(fixed_phy_update_state
);
155 int fixed_phy_add(unsigned int irq
, int phy_addr
,
156 struct fixed_phy_status
*status
,
160 struct fixed_mdio_bus
*fmb
= &platform_fmb
;
161 struct fixed_phy
*fp
;
163 ret
= swphy_validate_state(status
);
167 fp
= kzalloc(sizeof(*fp
), GFP_KERNEL
);
171 memset(fp
->regs
, 0xFF, sizeof(fp
->regs
[0]) * MII_REGS_NUM
);
174 fmb
->mii_bus
->irq
[phy_addr
] = irq
;
177 fp
->status
= *status
;
178 fp
->link_gpio
= link_gpio
;
180 if (gpio_is_valid(fp
->link_gpio
)) {
181 ret
= gpio_request_one(fp
->link_gpio
, GPIOF_DIR_IN
,
182 "fixed-link-gpio-link");
187 fixed_phy_update_regs(fp
);
189 list_add_tail(&fp
->node
, &fmb
->phys
);
197 EXPORT_SYMBOL_GPL(fixed_phy_add
);
199 static void fixed_phy_del(int phy_addr
)
201 struct fixed_mdio_bus
*fmb
= &platform_fmb
;
202 struct fixed_phy
*fp
, *tmp
;
204 list_for_each_entry_safe(fp
, tmp
, &fmb
->phys
, node
) {
205 if (fp
->addr
== phy_addr
) {
207 if (gpio_is_valid(fp
->link_gpio
))
208 gpio_free(fp
->link_gpio
);
215 static int phy_fixed_addr
;
216 static DEFINE_SPINLOCK(phy_fixed_addr_lock
);
218 struct phy_device
*fixed_phy_register(unsigned int irq
,
219 struct fixed_phy_status
*status
,
221 struct device_node
*np
)
223 struct fixed_mdio_bus
*fmb
= &platform_fmb
;
224 struct phy_device
*phy
;
228 if (!fmb
->mii_bus
|| fmb
->mii_bus
->state
!= MDIOBUS_REGISTERED
)
229 return ERR_PTR(-EPROBE_DEFER
);
231 /* Get the next available PHY address, up to PHY_MAX_ADDR */
232 spin_lock(&phy_fixed_addr_lock
);
233 if (phy_fixed_addr
== PHY_MAX_ADDR
) {
234 spin_unlock(&phy_fixed_addr_lock
);
235 return ERR_PTR(-ENOSPC
);
237 phy_addr
= phy_fixed_addr
++;
238 spin_unlock(&phy_fixed_addr_lock
);
240 ret
= fixed_phy_add(irq
, phy_addr
, status
, link_gpio
);
244 phy
= get_phy_device(fmb
->mii_bus
, phy_addr
, false);
246 fixed_phy_del(phy_addr
);
247 return ERR_PTR(-EINVAL
);
250 /* propagate the fixed link values to struct phy_device */
251 phy
->link
= status
->link
;
253 phy
->speed
= status
->speed
;
254 phy
->duplex
= status
->duplex
;
255 phy
->pause
= status
->pause
;
256 phy
->asym_pause
= status
->asym_pause
;
260 phy
->mdio
.dev
.of_node
= np
;
261 phy
->is_pseudo_fixed_link
= true;
263 switch (status
->speed
) {
265 phy
->supported
= PHY_1000BT_FEATURES
;
268 phy
->supported
= PHY_100BT_FEATURES
;
272 phy
->supported
= PHY_10BT_FEATURES
;
275 ret
= phy_device_register(phy
);
277 phy_device_free(phy
);
279 fixed_phy_del(phy_addr
);
285 EXPORT_SYMBOL_GPL(fixed_phy_register
);
287 void fixed_phy_unregister(struct phy_device
*phy
)
289 phy_device_remove(phy
);
291 fixed_phy_del(phy
->mdio
.addr
);
293 EXPORT_SYMBOL_GPL(fixed_phy_unregister
);
295 static int __init
fixed_mdio_bus_init(void)
297 struct fixed_mdio_bus
*fmb
= &platform_fmb
;
300 pdev
= platform_device_register_simple("Fixed MDIO bus", 0, NULL
, 0);
306 fmb
->mii_bus
= mdiobus_alloc();
307 if (fmb
->mii_bus
== NULL
) {
309 goto err_mdiobus_reg
;
312 snprintf(fmb
->mii_bus
->id
, MII_BUS_ID_SIZE
, "fixed-0");
313 fmb
->mii_bus
->name
= "Fixed MDIO Bus";
314 fmb
->mii_bus
->priv
= fmb
;
315 fmb
->mii_bus
->parent
= &pdev
->dev
;
316 fmb
->mii_bus
->read
= &fixed_mdio_read
;
317 fmb
->mii_bus
->write
= &fixed_mdio_write
;
319 ret
= mdiobus_register(fmb
->mii_bus
);
321 goto err_mdiobus_alloc
;
326 mdiobus_free(fmb
->mii_bus
);
328 platform_device_unregister(pdev
);
332 module_init(fixed_mdio_bus_init
);
334 static void __exit
fixed_mdio_bus_exit(void)
336 struct fixed_mdio_bus
*fmb
= &platform_fmb
;
337 struct fixed_phy
*fp
, *tmp
;
339 mdiobus_unregister(fmb
->mii_bus
);
340 mdiobus_free(fmb
->mii_bus
);
341 platform_device_unregister(pdev
);
343 list_for_each_entry_safe(fp
, tmp
, &fmb
->phys
, node
) {
348 module_exit(fixed_mdio_bus_exit
);
350 MODULE_DESCRIPTION("Fixed MDIO bus (MDIO bus emulation with fixed PHYs)");
351 MODULE_AUTHOR("Vitaly Bordug");
352 MODULE_LICENSE("GPL");