Commit e1393456 authored by Andy Fleming's avatar Andy Fleming Committed by Jeff Garzik

[PATCH] PHY Layer fixup

This patch adds back the code that was taken out, thus re-enabling:

* The PHY Layer to initialize without crashing
* Drivers to actually connect to PHYs
* The entire PHY Control Layer

This patch is used by the gianfar driver, and other drivers which are in
development.
Signed-off-by: default avatarAndy Fleming <afleming@freescale.com>
Signed-off-by: default avatarJeff Garzik <jgarzik@pobox.com>
parent 86f0cd50
...@@ -12,6 +12,14 @@ config PHYLIB ...@@ -12,6 +12,14 @@ config PHYLIB
devices. This option provides infrastructure for devices. This option provides infrastructure for
managing PHY devices. managing PHY devices.
config PHYCONTROL
bool " Support for automatically handling PHY state changes"
depends on PHYLIB
help
Adds code to perform all the work for keeping PHY link
state (speed/duplex/etc) up-to-date. Also handles
interrupts.
comment "MII PHY device drivers" comment "MII PHY device drivers"
depends on PHYLIB depends on PHYLIB
......
...@@ -2,8 +2,9 @@ ...@@ -2,8 +2,9 @@
libphy-objs := phy.o phy_device.o mdio_bus.o libphy-objs := phy.o phy_device.o mdio_bus.o
obj-$(CONFIG_MARVELL_PHY) += libphy.o marvell.o obj-$(CONFIG_PHYLIB) += libphy.o
obj-$(CONFIG_DAVICOM_PHY) += libphy.o davicom.o obj-$(CONFIG_MARVELL_PHY) += marvell.o
obj-$(CONFIG_CICADA_PHY) += libphy.o cicada.o obj-$(CONFIG_DAVICOM_PHY) += davicom.o
obj-$(CONFIG_LXT_PHY) += libphy.o lxt.o obj-$(CONFIG_CICADA_PHY) += cicada.o
obj-$(CONFIG_QSEMI_PHY) += libphy.o qsemi.o obj-$(CONFIG_LXT_PHY) += lxt.o
obj-$(CONFIG_QSEMI_PHY) += qsemi.o
...@@ -38,6 +38,80 @@ ...@@ -38,6 +38,80 @@
#include <asm/irq.h> #include <asm/irq.h>
#include <asm/uaccess.h> #include <asm/uaccess.h>
/* mdiobus_register
*
* description: Called by a bus driver to bring up all the PHYs
* on a given bus, and attach them to the bus
*/
int mdiobus_register(struct mii_bus *bus)
{
int i;
int err = 0;
spin_lock_init(&bus->mdio_lock);
if (NULL == bus || NULL == bus->name ||
NULL == bus->read ||
NULL == bus->write)
return -EINVAL;
if (bus->reset)
bus->reset(bus);
for (i = 0; i < PHY_MAX_ADDR; i++) {
struct phy_device *phydev;
phydev = get_phy_device(bus, i);
if (IS_ERR(phydev))
return PTR_ERR(phydev);
/* There's a PHY at this address
* We need to set:
* 1) IRQ
* 2) bus_id
* 3) parent
* 4) bus
* 5) mii_bus
* And, we need to register it */
if (phydev) {
phydev->irq = bus->irq[i];
phydev->dev.parent = bus->dev;
phydev->dev.bus = &mdio_bus_type;
sprintf(phydev->dev.bus_id, "phy%d:%d", bus->id, i);
phydev->bus = bus;
err = device_register(&phydev->dev);
if (err)
printk(KERN_ERR "phy %d failed to register\n",
i);
}
bus->phy_map[i] = phydev;
}
pr_info("%s: probed\n", bus->name);
return err;
}
EXPORT_SYMBOL(mdiobus_register);
void mdiobus_unregister(struct mii_bus *bus)
{
int i;
for (i = 0; i < PHY_MAX_ADDR; i++) {
if (bus->phy_map[i]) {
device_unregister(&bus->phy_map[i]->dev);
kfree(bus->phy_map[i]);
}
}
}
EXPORT_SYMBOL(mdiobus_unregister);
/* mdio_bus_match /* mdio_bus_match
* *
* description: Given a PHY device, and a PHY driver, return 1 if * description: Given a PHY device, and a PHY driver, return 1 if
...@@ -96,4 +170,7 @@ int __init mdio_bus_init(void) ...@@ -96,4 +170,7 @@ int __init mdio_bus_init(void)
return bus_register(&mdio_bus_type); return bus_register(&mdio_bus_type);
} }
void __exit mdio_bus_exit(void)
{
bus_unregister(&mdio_bus_type);
}
This diff is collapsed.
...@@ -39,18 +39,9 @@ ...@@ -39,18 +39,9 @@
#include <asm/irq.h> #include <asm/irq.h>
#include <asm/uaccess.h> #include <asm/uaccess.h>
static int genphy_config_init(struct phy_device *phydev); static struct phy_driver genphy_driver;
extern int mdio_bus_init(void);
static struct phy_driver genphy_driver = { extern void mdio_bus_exit(void);
.phy_id = 0xffffffff,
.phy_id_mask = 0xffffffff,
.name = "Generic PHY",
.config_init = genphy_config_init,
.features = 0,
.config_aneg = genphy_config_aneg,
.read_status = genphy_read_status,
.driver = {.owner = THIS_MODULE, },
};
/* get_phy_device /* get_phy_device
* *
...@@ -110,6 +101,7 @@ struct phy_device * get_phy_device(struct mii_bus *bus, int addr) ...@@ -110,6 +101,7 @@ struct phy_device * get_phy_device(struct mii_bus *bus, int addr)
return dev; return dev;
} }
#ifdef CONFIG_PHYCONTROL
/* phy_prepare_link: /* phy_prepare_link:
* *
* description: Tells the PHY infrastructure to handle the * description: Tells the PHY infrastructure to handle the
...@@ -124,6 +116,132 @@ void phy_prepare_link(struct phy_device *phydev, ...@@ -124,6 +116,132 @@ void phy_prepare_link(struct phy_device *phydev,
phydev->adjust_link = handler; phydev->adjust_link = handler;
} }
/* phy_connect:
*
* description: Convenience function for connecting ethernet
* devices to PHY devices. The default behavior is for
* the PHY infrastructure to handle everything, and only notify
* the connected driver when the link status changes. If you
* don't want, or can't use the provided functionality, you may
* choose to call only the subset of functions which provide
* the desired functionality.
*/
struct phy_device * phy_connect(struct net_device *dev, const char *phy_id,
void (*handler)(struct net_device *), u32 flags)
{
struct phy_device *phydev;
phydev = phy_attach(dev, phy_id, flags);
if (IS_ERR(phydev))
return phydev;
phy_prepare_link(phydev, handler);
phy_start_machine(phydev, NULL);
if (phydev->irq > 0)
phy_start_interrupts(phydev);
return phydev;
}
EXPORT_SYMBOL(phy_connect);
void phy_disconnect(struct phy_device *phydev)
{
if (phydev->irq > 0)
phy_stop_interrupts(phydev);
phy_stop_machine(phydev);
phydev->adjust_link = NULL;
phy_detach(phydev);
}
EXPORT_SYMBOL(phy_disconnect);
#endif /* CONFIG_PHYCONTROL */
/* phy_attach:
*
* description: Called by drivers to attach to a particular PHY
* device. The phy_device is found, and properly hooked up
* to the phy_driver. If no driver is attached, then the
* genphy_driver is used. The phy_device is given a ptr to
* the attaching device, and given a callback for link status
* change. The phy_device is returned to the attaching
* driver.
*/
static int phy_compare_id(struct device *dev, void *data)
{
return strcmp((char *)data, dev->bus_id) ? 0 : 1;
}
struct phy_device *phy_attach(struct net_device *dev,
const char *phy_id, u32 flags)
{
struct bus_type *bus = &mdio_bus_type;
struct phy_device *phydev;
struct device *d;
/* Search the list of PHY devices on the mdio bus for the
* PHY with the requested name */
d = bus_find_device(bus, NULL, (void *)phy_id, phy_compare_id);
if (d) {
phydev = to_phy_device(d);
} else {
printk(KERN_ERR "%s not found\n", phy_id);
return ERR_PTR(-ENODEV);
}
/* Assume that if there is no driver, that it doesn't
* exist, and we should use the genphy driver. */
if (NULL == d->driver) {
int err;
down_write(&d->bus->subsys.rwsem);
d->driver = &genphy_driver.driver;
err = d->driver->probe(d);
if (err < 0)
return ERR_PTR(err);
device_bind_driver(d);
up_write(&d->bus->subsys.rwsem);
}
if (phydev->attached_dev) {
printk(KERN_ERR "%s: %s already attached\n",
dev->name, phy_id);
return ERR_PTR(-EBUSY);
}
phydev->attached_dev = dev;
phydev->dev_flags = flags;
return phydev;
}
EXPORT_SYMBOL(phy_attach);
void phy_detach(struct phy_device *phydev)
{
phydev->attached_dev = NULL;
/* If the device had no specific driver before (i.e. - it
* was using the generic driver), we unbind the device
* from the generic driver so that there's a chance a
* real driver could be loaded */
if (phydev->dev.driver == &genphy_driver.driver) {
down_write(&phydev->dev.bus->subsys.rwsem);
device_release_driver(&phydev->dev);
up_write(&phydev->dev.bus->subsys.rwsem);
}
}
EXPORT_SYMBOL(phy_detach);
/* Generic PHY support and helper functions */ /* Generic PHY support and helper functions */
/* genphy_config_advert /* genphy_config_advert
...@@ -132,7 +250,7 @@ void phy_prepare_link(struct phy_device *phydev, ...@@ -132,7 +250,7 @@ void phy_prepare_link(struct phy_device *phydev,
* after sanitizing the values to make sure we only advertise * after sanitizing the values to make sure we only advertise
* what is supported * what is supported
*/ */
static int genphy_config_advert(struct phy_device *phydev) int genphy_config_advert(struct phy_device *phydev)
{ {
u32 advertise; u32 advertise;
int adv; int adv;
...@@ -190,6 +308,7 @@ static int genphy_config_advert(struct phy_device *phydev) ...@@ -190,6 +308,7 @@ static int genphy_config_advert(struct phy_device *phydev)
return adv; return adv;
} }
EXPORT_SYMBOL(genphy_config_advert);
/* genphy_setup_forced /* genphy_setup_forced
* *
...@@ -541,32 +660,37 @@ void phy_driver_unregister(struct phy_driver *drv) ...@@ -541,32 +660,37 @@ void phy_driver_unregister(struct phy_driver *drv)
} }
EXPORT_SYMBOL(phy_driver_unregister); EXPORT_SYMBOL(phy_driver_unregister);
static struct phy_driver genphy_driver = {
.phy_id = 0xffffffff,
.phy_id_mask = 0xffffffff,
.name = "Generic PHY",
.config_init = genphy_config_init,
.features = 0,
.config_aneg = genphy_config_aneg,
.read_status = genphy_read_status,
.driver = {.owner= THIS_MODULE, },
};
static int __init phy_init(void) static int __init phy_init(void)
{ {
int rc; int rc;
extern int mdio_bus_init(void);
rc = phy_driver_register(&genphy_driver);
if (rc)
goto out;
rc = mdio_bus_init(); rc = mdio_bus_init();
if (rc) if (rc)
goto out_unreg; return rc;
return 0; rc = phy_driver_register(&genphy_driver);
if (rc)
mdio_bus_exit();
out_unreg:
phy_driver_unregister(&genphy_driver);
out:
return rc; return rc;
} }
static void __exit phy_exit(void) static void __exit phy_exit(void)
{ {
phy_driver_unregister(&genphy_driver); phy_driver_unregister(&genphy_driver);
mdio_bus_exit();
} }
module_init(phy_init); subsys_initcall(phy_init);
module_exit(phy_exit); module_exit(phy_exit);
...@@ -334,11 +334,26 @@ int phy_write(struct phy_device *phydev, u16 regnum, u16 val); ...@@ -334,11 +334,26 @@ int phy_write(struct phy_device *phydev, u16 regnum, u16 val);
struct phy_device* get_phy_device(struct mii_bus *bus, int addr); struct phy_device* get_phy_device(struct mii_bus *bus, int addr);
int phy_clear_interrupt(struct phy_device *phydev); int phy_clear_interrupt(struct phy_device *phydev);
int phy_config_interrupt(struct phy_device *phydev, u32 interrupts); int phy_config_interrupt(struct phy_device *phydev, u32 interrupts);
struct phy_device * phy_attach(struct net_device *dev,
const char *phy_id, u32 flags);
struct phy_device * phy_connect(struct net_device *dev, const char *phy_id,
void (*handler)(struct net_device *), u32 flags);
void phy_disconnect(struct phy_device *phydev);
void phy_detach(struct phy_device *phydev);
void phy_start(struct phy_device *phydev);
void phy_stop(struct phy_device *phydev);
int phy_start_aneg(struct phy_device *phydev);
int mdiobus_register(struct mii_bus *bus);
void mdiobus_unregister(struct mii_bus *bus);
void phy_sanitize_settings(struct phy_device *phydev);
int phy_stop_interrupts(struct phy_device *phydev);
static inline int phy_read_status(struct phy_device *phydev) { static inline int phy_read_status(struct phy_device *phydev) {
return phydev->drv->read_status(phydev); return phydev->drv->read_status(phydev);
} }
int genphy_config_advert(struct phy_device *phydev);
int genphy_setup_forced(struct phy_device *phydev); int genphy_setup_forced(struct phy_device *phydev);
int genphy_restart_aneg(struct phy_device *phydev); int genphy_restart_aneg(struct phy_device *phydev);
int genphy_config_aneg(struct phy_device *phydev); int genphy_config_aneg(struct phy_device *phydev);
...@@ -355,6 +370,8 @@ int phy_ethtool_sset(struct phy_device *phydev, struct ethtool_cmd *cmd); ...@@ -355,6 +370,8 @@ int phy_ethtool_sset(struct phy_device *phydev, struct ethtool_cmd *cmd);
int phy_ethtool_gset(struct phy_device *phydev, struct ethtool_cmd *cmd); int phy_ethtool_gset(struct phy_device *phydev, struct ethtool_cmd *cmd);
int phy_mii_ioctl(struct phy_device *phydev, int phy_mii_ioctl(struct phy_device *phydev,
struct mii_ioctl_data *mii_data, int cmd); struct mii_ioctl_data *mii_data, int cmd);
int phy_start_interrupts(struct phy_device *phydev);
void phy_print_status(struct phy_device *phydev);
extern struct bus_type mdio_bus_type; extern struct bus_type mdio_bus_type;
#endif /* __PHY_H */ #endif /* __PHY_H */
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment