From cae7e5b7f92c31cb1b2b20256865ab60cb2c854c Mon Sep 17 00:00:00 2001 From: Wolfram Sang Date: Tue, 24 Jan 2012 14:53:50 +0100 Subject: [PATCH] net: miidev: factor out miidev_get_status() Currently, we can only print the phy_status. Factor out the routine to get the status, so we can query it from fec drivers and configure accordingly. Needed because mx28 needs a special bit set for 10Mbit. Signed-off-by: Wolfram Sang Signed-off-by: Sascha Hauer --- drivers/net/miidev.c | 67 ++++++++++++++++++++++++++++---------------- include/miidev.h | 5 ++++ 2 files changed, 48 insertions(+), 24 deletions(-) diff --git a/drivers/net/miidev.c b/drivers/net/miidev.c index 501a4f8e1..d721aac9d 100644 --- a/drivers/net/miidev.c +++ b/drivers/net/miidev.c @@ -100,44 +100,63 @@ int miidev_wait_aneg(struct mii_device *mdev) return 0; } +int miidev_get_status(struct mii_device *mdev) +{ + int ret, status; + + ret = mii_read(mdev, mdev->address, MII_BMSR); + if (ret < 0) + goto err_out; + + status = ret & BMSR_LSTATUS ? MIIDEV_STATUS_IS_UP : 0; + + ret = mii_read(mdev, mdev->address, MII_BMCR); + if (ret < 0) + goto err_out; + + if (ret & BMCR_ANENABLE) { + ret = mii_read(mdev, mdev->address, MII_LPA); + if (ret < 0) + goto err_out; + + status |= ret & LPA_DUPLEX ? MIIDEV_STATUS_IS_FULL_DUPLEX : 0; + status |= ret & LPA_100 ? MIIDEV_STATUS_IS_100MBIT : + MIIDEV_STATUS_IS_10MBIT; + } else { + status |= ret & BMCR_FULLDPLX ? MIIDEV_STATUS_IS_FULL_DUPLEX : 0; + status |= ret & BMCR_SPEED100 ? MIIDEV_STATUS_IS_100MBIT : + MIIDEV_STATUS_IS_10MBIT; + } + + return status; +err_out: + printf("%s: failed to read (%d)\n", mdev->cdev.name, ret); + return ret; +} + int miidev_print_status(struct mii_device *mdev) { - int bmsr, bmcr, lpa; char *duplex; - int speed; + int speed, status; if (mdev->flags & MIIDEV_FORCE_LINK) { printf("Forcing link present...\n"); return 0; } - bmsr = mii_read(mdev, mdev->address, MII_BMSR); - if (bmsr < 0) - goto err_out; - bmcr = mii_read(mdev, mdev->address, MII_BMCR); - if (bmcr < 0) - goto err_out; - lpa = mii_read(mdev, mdev->address, MII_LPA); - if (lpa < 0) - goto err_out; + status = miidev_get_status(mdev); + if (status < 0) + return status; + + duplex = status & MIIDEV_STATUS_IS_FULL_DUPLEX ? "Full" : "Half"; + speed = status & MIIDEV_STATUS_IS_100MBIT ? 100 : 10; + printf("%s: Link is %s", mdev->cdev.name, - bmsr & BMSR_LSTATUS ? "up" : "down"); - - if (bmcr & BMCR_ANENABLE) { - duplex = lpa & LPA_DUPLEX ? "Full" : "Half"; - speed = lpa & LPA_100 ? 100 : 10; - } else { - duplex = bmcr & BMCR_FULLDPLX ? "Full" : "Half"; - speed = bmcr & BMCR_SPEED100 ? 100 : 10; - } - + status & MIIDEV_STATUS_IS_UP ? "up" : "down"); printf(" - %d/%s\n", speed, duplex); return 0; -err_out: - printf("%s: failed to read\n", mdev->cdev.name); - return -1; } static ssize_t miidev_read(struct cdev *cdev, void *_buf, size_t count, ulong offset, ulong flags) diff --git a/include/miidev.h b/include/miidev.h index 21727effc..8cd4c4500 100644 --- a/include/miidev.h +++ b/include/miidev.h @@ -49,6 +49,11 @@ int mii_register(struct mii_device *dev); void mii_unregister(struct mii_device *mdev); int miidev_restart_aneg(struct mii_device *mdev); int miidev_wait_aneg(struct mii_device *mdev); +int miidev_get_status(struct mii_device *mdev); +#define MIIDEV_STATUS_IS_UP (1 << 0) +#define MIIDEV_STATUS_IS_FULL_DUPLEX (1 << 1) +#define MIIDEV_STATUS_IS_10MBIT (1 << 2) +#define MIIDEV_STATUS_IS_100MBIT (1 << 3) int miidev_print_status(struct mii_device *mdev); static int inline mii_write(struct mii_device *dev, int addr, int reg, int value)