blob: 5838ea0685ef81e20102e37b119193f5e1170fa6 [file] [log] [blame]
/*
* Copyright 2014 Google Inc.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* 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
*/
#include <stdint.h>
#include <stdio.h>
#include "drivers/net/mii.h"
#include "drivers/net/net.h"
static int mii_restart_autoneg(MdioOps *mdio)
{
uint16_t bmsr;
if (mdio_read(mdio, MiiBmsr, &bmsr))
return 1;
if (!(bmsr & BmsrAnegCapable)) {
printf("No AutoNeg, falling back to 100baseTX\n");
return mdio_write(mdio, MiiBmcr, BmcrSpeedSel | BmcrDuplexMode);
} else {
return mdio_write(mdio, MiiBmcr,
BmcrAutoNegEnable | BmcrRestartAutoNeg);
}
}
int mii_phy_initialize(MdioOps *mdio)
{
if (mdio_write(mdio, MiiBmcr, BmcrReset))
return 1;
if (mdio_write(mdio, MiiAnar, AdvertiseAll | AdvertiseCsma))
return 1;
if (mii_restart_autoneg(mdio))
return 1;
return 0;
}
int mii_ready(MdioOps *mdio, int *ready)
{
uint16_t link_status = 0;
if (mdio_read(mdio, MiiBmsr, &link_status)) {
printf("Failed to read BMSR.\n");
return 1;
}
*ready = (link_status & BmsrLstatus);
return 0;
}