2007-07-05 16:02:19 +00:00
|
|
|
/*
|
2010-08-26 16:33:28 +00:00
|
|
|
* miidev.c - generic phy abstraction
|
2007-07-05 16:02:19 +00:00
|
|
|
*
|
|
|
|
* Copyright (c) 2007 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
|
|
|
|
*
|
|
|
|
* See file CREDITS for list of people who contributed to this
|
|
|
|
* project.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
2007-07-05 16:02:06 +00:00
|
|
|
#include <common.h>
|
|
|
|
#include <driver.h>
|
|
|
|
#include <init.h>
|
2010-08-26 16:33:28 +00:00
|
|
|
#include <miidev.h>
|
2007-09-11 08:22:00 +00:00
|
|
|
#include <clock.h>
|
2009-04-22 21:39:27 +00:00
|
|
|
#include <net.h>
|
2009-06-11 13:32:06 +00:00
|
|
|
#include <malloc.h>
|
2007-07-05 16:02:06 +00:00
|
|
|
|
2010-08-26 16:33:28 +00:00
|
|
|
int miidev_restart_aneg(struct mii_device *mdev)
|
2007-07-05 16:02:06 +00:00
|
|
|
{
|
2012-03-21 11:18:43 +00:00
|
|
|
int status, timeout;
|
2012-03-21 11:18:45 +00:00
|
|
|
uint64_t start;
|
2007-07-05 16:02:06 +00:00
|
|
|
|
2012-03-21 11:18:43 +00:00
|
|
|
status = mii_write(mdev, mdev->address, MII_BMCR, BMCR_RESET);
|
|
|
|
if (status)
|
|
|
|
return status;
|
2009-05-18 13:48:44 +00:00
|
|
|
|
2012-03-21 11:18:45 +00:00
|
|
|
start = get_time_ns();
|
|
|
|
do {
|
|
|
|
status = mii_read(mdev, mdev->address, MII_BMCR);
|
|
|
|
if (status < 0)
|
|
|
|
return status;
|
|
|
|
|
|
|
|
if (is_timeout(start, SECOND))
|
|
|
|
return -ETIMEDOUT;
|
|
|
|
|
|
|
|
} while (status & BMCR_RESET);
|
|
|
|
|
2010-08-26 16:33:28 +00:00
|
|
|
if (mdev->flags & MIIDEV_FORCE_LINK)
|
2009-05-18 13:48:44 +00:00
|
|
|
return 0;
|
|
|
|
|
2010-08-26 16:33:28 +00:00
|
|
|
if (mdev->flags & MIIDEV_FORCE_10) {
|
2007-07-05 16:02:06 +00:00
|
|
|
printf("Forcing 10 Mbps ethernet link... ");
|
2012-03-21 11:18:43 +00:00
|
|
|
|
2010-08-26 16:33:28 +00:00
|
|
|
status = mii_read(mdev, mdev->address, MII_BMSR);
|
2012-03-21 11:18:43 +00:00
|
|
|
if (status < 0)
|
|
|
|
return status;
|
|
|
|
|
|
|
|
status = mii_write(mdev, mdev->address, MII_BMCR, BMCR_FULLDPLX | BMCR_CTST);
|
|
|
|
if (status)
|
|
|
|
return status;
|
2007-07-05 16:02:06 +00:00
|
|
|
|
|
|
|
timeout = 20;
|
|
|
|
do { /* wait for link status to go down */
|
|
|
|
udelay(10000);
|
|
|
|
if ((timeout--) == 0) {
|
2008-04-07 10:23:54 +00:00
|
|
|
debug("hmmm, should not have waited...");
|
2007-07-05 16:02:06 +00:00
|
|
|
break;
|
|
|
|
}
|
2010-08-26 16:33:28 +00:00
|
|
|
status = mii_read(mdev, mdev->address, MII_BMSR);
|
2012-03-21 11:18:43 +00:00
|
|
|
if (status < 0)
|
|
|
|
return status;
|
2007-07-05 16:02:06 +00:00
|
|
|
} while (status & BMSR_LSTATUS);
|
2007-07-05 16:02:06 +00:00
|
|
|
|
|
|
|
} else { /* MII100 */
|
|
|
|
/*
|
|
|
|
* Set the auto-negotiation advertisement register bits
|
|
|
|
*/
|
2010-08-26 16:33:28 +00:00
|
|
|
status = mii_read(mdev, mdev->address, MII_ADVERTISE);
|
2012-03-21 11:18:43 +00:00
|
|
|
if (status < 0)
|
|
|
|
return status;
|
|
|
|
|
2008-01-23 11:12:01 +00:00
|
|
|
status |= ADVERTISE_ALL;
|
2007-07-05 16:02:06 +00:00
|
|
|
|
2012-03-21 11:18:43 +00:00
|
|
|
status = mii_write(mdev, mdev->address, MII_ADVERTISE, status);
|
|
|
|
if (status)
|
|
|
|
return status;
|
|
|
|
|
|
|
|
status = mii_write(mdev, mdev->address, MII_BMCR, BMCR_ANENABLE | BMCR_ANRESTART);
|
|
|
|
if (status)
|
|
|
|
return status;
|
2007-07-05 16:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-08-26 16:33:28 +00:00
|
|
|
int miidev_wait_aneg(struct mii_device *mdev)
|
2007-07-05 16:02:06 +00:00
|
|
|
{
|
2010-08-26 16:33:28 +00:00
|
|
|
int status;
|
2012-03-21 11:18:44 +00:00
|
|
|
uint64_t start = get_time_ns();
|
2007-07-05 16:02:06 +00:00
|
|
|
|
2010-08-26 16:33:28 +00:00
|
|
|
if (mdev->flags & MIIDEV_FORCE_LINK)
|
2009-05-18 13:48:44 +00:00
|
|
|
return 0;
|
|
|
|
|
2007-07-05 16:02:06 +00:00
|
|
|
do {
|
2012-03-21 11:18:44 +00:00
|
|
|
status = mii_read(mdev, mdev->address, MII_BMSR);
|
|
|
|
if (status < 0)
|
|
|
|
return status;
|
|
|
|
|
2007-09-11 08:22:00 +00:00
|
|
|
if (is_timeout(start, 5 * SECOND)) {
|
2009-04-22 21:39:27 +00:00
|
|
|
printf("%s: Autonegotiation timeout\n", mdev->cdev.name);
|
2012-03-21 11:18:44 +00:00
|
|
|
return -ETIMEDOUT;
|
2007-07-05 16:02:06 +00:00
|
|
|
}
|
|
|
|
|
2007-07-05 16:02:06 +00:00
|
|
|
} while (!(status & BMSR_LSTATUS));
|
2007-07-05 16:02:06 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-24 13:53:50 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-08-26 16:33:28 +00:00
|
|
|
int miidev_print_status(struct mii_device *mdev)
|
2007-07-05 16:02:06 +00:00
|
|
|
{
|
|
|
|
char *duplex;
|
2012-01-24 13:53:50 +00:00
|
|
|
int speed, status;
|
2007-07-05 16:02:06 +00:00
|
|
|
|
2010-08-26 16:33:28 +00:00
|
|
|
if (mdev->flags & MIIDEV_FORCE_LINK) {
|
2009-05-18 13:48:44 +00:00
|
|
|
printf("Forcing link present...\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-24 13:53:50 +00:00
|
|
|
status = miidev_get_status(mdev);
|
|
|
|
if (status < 0)
|
|
|
|
return status;
|
2007-07-05 16:02:06 +00:00
|
|
|
|
2012-01-24 13:53:50 +00:00
|
|
|
duplex = status & MIIDEV_STATUS_IS_FULL_DUPLEX ? "Full" : "Half";
|
|
|
|
speed = status & MIIDEV_STATUS_IS_100MBIT ? 100 : 10;
|
2007-07-05 16:02:06 +00:00
|
|
|
|
|
|
|
|
2012-01-24 13:53:50 +00:00
|
|
|
printf("%s: Link is %s", mdev->cdev.name,
|
|
|
|
status & MIIDEV_STATUS_IS_UP ? "up" : "down");
|
2007-07-05 16:02:06 +00:00
|
|
|
printf(" - %d/%s\n", speed, duplex);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-08-26 16:33:28 +00:00
|
|
|
static ssize_t miidev_read(struct cdev *cdev, void *_buf, size_t count, ulong offset, ulong flags)
|
2007-07-05 16:02:06 +00:00
|
|
|
{
|
|
|
|
int i = count;
|
|
|
|
uint16_t *buf = _buf;
|
2010-08-26 16:33:28 +00:00
|
|
|
struct mii_device *mdev = cdev->priv;
|
2007-07-05 16:02:06 +00:00
|
|
|
|
|
|
|
while (i > 1) {
|
2010-08-26 16:33:28 +00:00
|
|
|
*buf = mii_read(mdev, mdev->address, offset);
|
2007-07-05 16:02:06 +00:00
|
|
|
buf++;
|
|
|
|
i -= 2;
|
|
|
|
offset++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2010-08-26 16:33:28 +00:00
|
|
|
static ssize_t miidev_write(struct cdev *cdev, const void *_buf, size_t count, ulong offset, ulong flags)
|
2007-07-05 16:02:06 +00:00
|
|
|
{
|
|
|
|
int i = count;
|
|
|
|
const uint16_t *buf = _buf;
|
2010-08-26 16:33:28 +00:00
|
|
|
struct mii_device *mdev = cdev->priv;
|
2007-07-05 16:02:06 +00:00
|
|
|
|
|
|
|
while (i > 1) {
|
2010-08-26 16:33:28 +00:00
|
|
|
mii_write(mdev, mdev->address, offset, *buf);
|
2007-07-05 16:02:06 +00:00
|
|
|
buf++;
|
|
|
|
i -= 2;
|
|
|
|
offset++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2010-08-26 16:33:28 +00:00
|
|
|
static struct file_operations miidev_ops = {
|
|
|
|
.read = miidev_read,
|
|
|
|
.write = miidev_write,
|
2009-10-19 13:58:53 +00:00
|
|
|
.lseek = dev_lseek_default,
|
2009-04-22 21:39:27 +00:00
|
|
|
};
|
|
|
|
|
2010-08-26 16:33:28 +00:00
|
|
|
static int miidev_probe(struct device_d *dev)
|
2007-07-05 16:02:06 +00:00
|
|
|
{
|
2010-08-26 16:33:28 +00:00
|
|
|
struct mii_device *mdev = dev->priv;
|
2009-04-22 21:39:27 +00:00
|
|
|
|
2009-06-11 13:32:06 +00:00
|
|
|
mdev->cdev.name = asprintf("phy%d", dev->id);
|
2011-01-18 14:59:12 +00:00
|
|
|
mdev->cdev.size = 64;
|
2010-08-26 16:33:28 +00:00
|
|
|
mdev->cdev.ops = &miidev_ops;
|
2009-04-22 21:39:27 +00:00
|
|
|
mdev->cdev.priv = mdev;
|
2009-07-21 16:37:14 +00:00
|
|
|
mdev->cdev.dev = dev;
|
2009-04-22 21:39:27 +00:00
|
|
|
devfs_create(&mdev->cdev);
|
2007-07-05 16:02:06 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-08-26 16:33:28 +00:00
|
|
|
static void miidev_remove(struct device_d *dev)
|
2007-07-05 16:02:06 +00:00
|
|
|
{
|
2010-08-26 16:33:28 +00:00
|
|
|
struct mii_device *mdev = dev->priv;
|
2007-07-05 16:02:06 +00:00
|
|
|
|
2009-06-11 13:32:06 +00:00
|
|
|
free(mdev->cdev.name);
|
|
|
|
devfs_remove(&mdev->cdev);
|
2007-07-05 16:02:06 +00:00
|
|
|
}
|
|
|
|
|
2010-08-26 16:33:28 +00:00
|
|
|
static struct driver_d miidev_drv = {
|
|
|
|
.name = "miidev",
|
|
|
|
.probe = miidev_probe,
|
|
|
|
.remove = miidev_remove,
|
2007-07-05 16:02:06 +00:00
|
|
|
};
|
|
|
|
|
2010-08-26 16:33:28 +00:00
|
|
|
int mii_register(struct mii_device *mdev)
|
2009-06-11 13:32:06 +00:00
|
|
|
{
|
|
|
|
mdev->dev.priv = mdev;
|
2010-11-26 19:56:39 +00:00
|
|
|
mdev->dev.id = -1;
|
2010-08-26 16:33:28 +00:00
|
|
|
strcpy(mdev->dev.name, "miidev");
|
2011-08-15 07:30:46 +00:00
|
|
|
if (mdev->parent)
|
|
|
|
dev_add_child(mdev->parent, &mdev->dev);
|
2009-06-11 13:32:06 +00:00
|
|
|
|
|
|
|
return register_device(&mdev->dev);
|
|
|
|
}
|
|
|
|
|
2010-08-26 16:33:28 +00:00
|
|
|
void mii_unregister(struct mii_device *mdev)
|
2009-06-11 13:32:06 +00:00
|
|
|
{
|
|
|
|
unregister_device(&mdev->dev);
|
|
|
|
}
|
|
|
|
|
2010-08-26 16:33:28 +00:00
|
|
|
static int miidev_init(void)
|
2007-07-05 16:02:06 +00:00
|
|
|
{
|
2010-08-26 16:33:28 +00:00
|
|
|
register_driver(&miidev_drv);
|
2007-07-05 16:02:06 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-08-26 16:33:28 +00:00
|
|
|
device_initcall(miidev_init);
|
2007-07-05 16:02:06 +00:00
|
|
|
|