2007-07-05 16:02:19 +00:00
|
|
|
/*
|
|
|
|
* miiphy.c - generic phy abstraction
|
|
|
|
*
|
|
|
|
* 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>
|
2007-07-05 16:02:06 +00:00
|
|
|
#include <miiphy.h>
|
2007-09-11 08:22:00 +00:00
|
|
|
#include <clock.h>
|
2007-07-05 16:02:06 +00:00
|
|
|
|
|
|
|
int miiphy_restart_aneg(struct miiphy_device *mdev)
|
|
|
|
{
|
|
|
|
uint16_t status;
|
|
|
|
int timeout;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Reset PHY, then delay 300ns
|
|
|
|
*/
|
2007-07-05 16:02:06 +00:00
|
|
|
mdev->write(mdev, mdev->address, MII_BMCR, BMCR_RESET);
|
2009-05-18 13:48:44 +00:00
|
|
|
|
|
|
|
if (mdev->flags & MIIPHY_FORCE_LINK)
|
|
|
|
return 0;
|
|
|
|
|
2007-07-05 16:02:06 +00:00
|
|
|
udelay(1000);
|
|
|
|
|
|
|
|
if (mdev->flags & MIIPHY_FORCE_10) {
|
|
|
|
printf("Forcing 10 Mbps ethernet link... ");
|
2007-07-05 16:02:06 +00:00
|
|
|
mdev->read(mdev, mdev->address, MII_BMSR, &status);
|
|
|
|
mdev->write(mdev, mdev->address, MII_BMCR, BMCR_FULLDPLX | BMCR_CTST);
|
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;
|
|
|
|
}
|
2007-07-05 16:02:06 +00:00
|
|
|
mdev->read(mdev, mdev->address, MII_BMSR, &status);
|
|
|
|
} while (status & BMSR_LSTATUS);
|
2007-07-05 16:02:06 +00:00
|
|
|
|
|
|
|
} else { /* MII100 */
|
|
|
|
/*
|
|
|
|
* Set the auto-negotiation advertisement register bits
|
|
|
|
*/
|
2008-01-23 11:12:01 +00:00
|
|
|
mdev->read(mdev, mdev->address, MII_ADVERTISE, &status);
|
|
|
|
status |= ADVERTISE_ALL;
|
|
|
|
mdev->write(mdev, mdev->address, MII_ADVERTISE, status);
|
2007-07-05 16:02:06 +00:00
|
|
|
|
2007-07-05 16:02:06 +00:00
|
|
|
mdev->write(mdev, mdev->address, MII_BMCR, BMCR_ANENABLE | BMCR_ANRESTART);
|
2007-07-05 16:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int miiphy_wait_aneg(struct miiphy_device *mdev)
|
|
|
|
{
|
2007-09-11 08:22:00 +00:00
|
|
|
uint64_t start;
|
2007-07-05 16:02:06 +00:00
|
|
|
uint16_t status;
|
|
|
|
|
2009-05-18 13:48:44 +00:00
|
|
|
if (mdev->flags & MIIPHY_FORCE_LINK)
|
|
|
|
return 0;
|
|
|
|
|
2007-07-05 16:02:06 +00:00
|
|
|
/*
|
|
|
|
* Wait for AN completion
|
|
|
|
*/
|
2007-09-11 08:22:00 +00:00
|
|
|
start = get_time_ns();
|
2007-07-05 16:02:06 +00:00
|
|
|
do {
|
2007-09-11 08:22:00 +00:00
|
|
|
if (is_timeout(start, 5 * SECOND)) {
|
2007-07-05 16:02:06 +00:00
|
|
|
printf("%s: Autonegotiation timeout\n", mdev->dev.id);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-07-05 16:02:06 +00:00
|
|
|
if (mdev->read(mdev, mdev->address, MII_BMSR, &status)) {
|
2007-07-05 16:02:06 +00:00
|
|
|
printf("%s: Autonegotiation failed. status: 0x%04x\n", mdev->dev.id, status);
|
|
|
|
return -1;
|
|
|
|
}
|
2007-07-05 16:02:06 +00:00
|
|
|
} while (!(status & BMSR_LSTATUS));
|
2007-07-05 16:02:06 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int miiphy_print_status(struct miiphy_device *mdev)
|
|
|
|
{
|
2007-07-05 16:02:06 +00:00
|
|
|
uint16_t bmsr, bmcr, lpa;
|
2007-07-05 16:02:06 +00:00
|
|
|
char *duplex;
|
|
|
|
int speed;
|
|
|
|
|
2009-05-18 13:48:44 +00:00
|
|
|
if (mdev->flags & MIIPHY_FORCE_LINK) {
|
|
|
|
printf("Forcing link present...\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-07-05 16:02:06 +00:00
|
|
|
if (mdev->read(mdev, mdev->address, MII_BMSR, &bmsr) != 0)
|
2007-07-05 16:02:06 +00:00
|
|
|
goto err_out;
|
2007-07-05 16:02:06 +00:00
|
|
|
if (mdev->read(mdev, mdev->address, MII_BMCR, &bmcr) != 0)
|
2007-07-05 16:02:06 +00:00
|
|
|
goto err_out;
|
2007-07-05 16:02:06 +00:00
|
|
|
if (mdev->read(mdev, mdev->address, MII_LPA, &lpa) != 0)
|
2007-07-05 16:02:06 +00:00
|
|
|
goto err_out;
|
|
|
|
|
|
|
|
printf("%s: Link is %s", mdev->dev.id,
|
2007-07-05 16:02:06 +00:00
|
|
|
bmsr & BMSR_LSTATUS ? "up" : "down");
|
2007-07-05 16:02:06 +00:00
|
|
|
|
2007-07-05 16:02:06 +00:00
|
|
|
if (bmcr & BMCR_ANENABLE) {
|
|
|
|
duplex = lpa & LPA_DUPLEX ? "Full" : "Half";
|
|
|
|
speed = lpa & LPA_100 ? 100 : 10;
|
2007-07-05 16:02:06 +00:00
|
|
|
} else {
|
2007-07-05 16:02:06 +00:00
|
|
|
duplex = bmcr & BMCR_FULLDPLX ? "Full" : "Half";
|
|
|
|
speed = bmcr & BMCR_SPEED100 ? 100 : 10;
|
2007-07-05 16:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
printf(" - %d/%s\n", speed, duplex);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
err_out:
|
|
|
|
printf("%s: failed to read\n", mdev->dev.id);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-10-27 11:06:01 +00:00
|
|
|
static ssize_t miiphy_read(struct device_d *dev, void *_buf, size_t count, ulong offset, ulong flags)
|
2007-07-05 16:02:06 +00:00
|
|
|
{
|
|
|
|
int i = count;
|
|
|
|
uint16_t *buf = _buf;
|
|
|
|
struct miiphy_device *mdev = dev->priv;
|
|
|
|
|
|
|
|
while (i > 1) {
|
|
|
|
mdev->read(mdev, mdev->address, offset, buf);
|
|
|
|
buf++;
|
|
|
|
i -= 2;
|
|
|
|
offset++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2008-10-27 11:06:01 +00:00
|
|
|
static ssize_t miiphy_write(struct device_d *dev, 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;
|
|
|
|
struct miiphy_device *mdev = dev->priv;
|
|
|
|
|
|
|
|
while (i > 1) {
|
|
|
|
mdev->write(mdev, mdev->address, offset, *buf);
|
|
|
|
buf++;
|
|
|
|
i -= 2;
|
|
|
|
offset++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int miiphy_probe(struct device_d *dev)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-04-06 16:56:21 +00:00
|
|
|
static void miiphy_remove(struct device_d *dev)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2007-07-05 16:02:06 +00:00
|
|
|
int miiphy_register(struct miiphy_device *mdev)
|
|
|
|
{
|
|
|
|
strcpy(mdev->dev.name, "miiphy");
|
|
|
|
get_free_deviceid(mdev->dev.id, "phy");
|
|
|
|
mdev->dev.type = DEVICE_TYPE_MIIPHY;
|
|
|
|
mdev->dev.priv = mdev;
|
|
|
|
mdev->dev.size = 32;
|
|
|
|
|
|
|
|
return register_device(&mdev->dev);
|
|
|
|
}
|
|
|
|
|
2009-04-06 16:56:21 +00:00
|
|
|
void miiphy_unregister(struct miiphy_device *mdev)
|
|
|
|
{
|
|
|
|
unregister_device(&mdev->dev);
|
|
|
|
}
|
|
|
|
|
2007-07-05 16:02:06 +00:00
|
|
|
static struct driver_d miiphy_drv = {
|
|
|
|
.name = "miiphy",
|
|
|
|
.probe = miiphy_probe,
|
2009-04-06 16:56:21 +00:00
|
|
|
.remove = miiphy_remove,
|
2008-08-01 06:58:06 +00:00
|
|
|
.open = dev_open_default,
|
|
|
|
.close = dev_close_default,
|
2007-07-05 16:02:06 +00:00
|
|
|
.read = miiphy_read,
|
|
|
|
.write = miiphy_write,
|
2009-05-18 14:34:24 +00:00
|
|
|
.lseek = dev_lseek_default,
|
2007-07-05 16:02:06 +00:00
|
|
|
.type = DEVICE_TYPE_MIIPHY,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int miiphy_init(void)
|
|
|
|
{
|
|
|
|
register_driver(&miiphy_drv);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
device_initcall(miiphy_init);
|
|
|
|
|