9
0
Fork 0
barebox/drivers/net/miidev.c

230 lines
4.9 KiB
C
Raw Normal View History

2007-07-05 16:02:19 +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>
#include <miidev.h>
#include <clock.h>
#include <net.h>
#include <malloc.h>
2007-07-05 16:02:06 +00:00
int miidev_restart_aneg(struct mii_device *mdev)
2007-07-05 16:02:06 +00:00
{
uint16_t status;
int timeout;
/*
* Reset PHY, then delay 300ns
*/
mii_write(mdev, mdev->address, MII_BMCR, BMCR_RESET);
if (mdev->flags & MIIDEV_FORCE_LINK)
return 0;
2007-07-05 16:02:06 +00:00
udelay(1000);
if (mdev->flags & MIIDEV_FORCE_10) {
2007-07-05 16:02:06 +00:00
printf("Forcing 10 Mbps ethernet link... ");
status = mii_read(mdev, mdev->address, MII_BMSR);
mii_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;
}
status = mii_read(mdev, mdev->address, MII_BMSR);
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
*/
status = mii_read(mdev, mdev->address, MII_ADVERTISE);
status |= ADVERTISE_ALL;
mii_write(mdev, mdev->address, MII_ADVERTISE, status);
2007-07-05 16:02:06 +00:00
mii_write(mdev, mdev->address, MII_BMCR, BMCR_ANENABLE | BMCR_ANRESTART);
2007-07-05 16:02:06 +00:00
}
return 0;
}
int miidev_wait_aneg(struct mii_device *mdev)
2007-07-05 16:02:06 +00:00
{
uint64_t start;
int status;
2007-07-05 16:02:06 +00:00
if (mdev->flags & MIIDEV_FORCE_LINK)
return 0;
2007-07-05 16:02:06 +00:00
/*
* Wait for AN completion
*/
start = get_time_ns();
2007-07-05 16:02:06 +00:00
do {
if (is_timeout(start, 5 * SECOND)) {
printf("%s: Autonegotiation timeout\n", mdev->cdev.name);
2007-07-05 16:02:06 +00:00
return -1;
}
status = mii_read(mdev, mdev->address, MII_BMSR);
if (status < 0) {
printf("%s: Autonegotiation failed. status: 0x%04x\n", mdev->cdev.name, status);
2007-07-05 16:02:06 +00:00
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 miidev_print_status(struct mii_device *mdev)
2007-07-05 16:02:06 +00:00
{
int bmsr, bmcr, lpa;
2007-07-05 16:02:06 +00:00
char *duplex;
int speed;
if (mdev->flags & MIIDEV_FORCE_LINK) {
printf("Forcing link present...\n");
return 0;
}
bmsr = mii_read(mdev, mdev->address, MII_BMSR);
if (bmsr < 0)
2007-07-05 16:02:06 +00:00
goto err_out;
bmcr = mii_read(mdev, mdev->address, MII_BMCR);
if (bmcr < 0)
2007-07-05 16:02:06 +00:00
goto err_out;
lpa = mii_read(mdev, mdev->address, MII_LPA);
if (lpa < 0)
2007-07-05 16:02:06 +00:00
goto err_out;
printf("%s: Link is %s", mdev->cdev.name,
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->cdev.name);
2007-07-05 16:02:06 +00:00
return -1;
}
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;
struct mii_device *mdev = cdev->priv;
2007-07-05 16:02:06 +00:00
while (i > 1) {
*buf = mii_read(mdev, mdev->address, offset);
2007-07-05 16:02:06 +00:00
buf++;
i -= 2;
offset++;
}
return count;
}
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;
struct mii_device *mdev = cdev->priv;
2007-07-05 16:02:06 +00:00
while (i > 1) {
mii_write(mdev, mdev->address, offset, *buf);
2007-07-05 16:02:06 +00:00
buf++;
i -= 2;
offset++;
}
return count;
}
static struct file_operations miidev_ops = {
.read = miidev_read,
.write = miidev_write,
.lseek = dev_lseek_default,
};
static int miidev_probe(struct device_d *dev)
2007-07-05 16:02:06 +00:00
{
struct mii_device *mdev = dev->priv;
mdev->cdev.name = asprintf("phy%d", dev->id);
mdev->cdev.size = 32;
mdev->cdev.ops = &miidev_ops;
mdev->cdev.priv = mdev;
mdev->cdev.dev = dev;
devfs_create(&mdev->cdev);
2007-07-05 16:02:06 +00:00
return 0;
}
static void miidev_remove(struct device_d *dev)
2007-07-05 16:02:06 +00:00
{
struct mii_device *mdev = dev->priv;
2007-07-05 16:02:06 +00:00
free(mdev->cdev.name);
devfs_remove(&mdev->cdev);
2007-07-05 16:02:06 +00:00
}
static struct driver_d miidev_drv = {
.name = "miidev",
.probe = miidev_probe,
.remove = miidev_remove,
2007-07-05 16:02:06 +00:00
};
int mii_register(struct mii_device *mdev)
{
mdev->dev.priv = mdev;
mdev->dev.id = -1;
strcpy(mdev->dev.name, "miidev");
return register_device(&mdev->dev);
}
void mii_unregister(struct mii_device *mdev)
{
unregister_device(&mdev->dev);
}
static int miidev_init(void)
2007-07-05 16:02:06 +00:00
{
register_driver(&miidev_drv);
2007-07-05 16:02:06 +00:00
return 0;
}
device_initcall(miidev_init);
2007-07-05 16:02:06 +00:00