/*-
* Copyright (c) 1998, 1999, 2000, 2001 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
* NASA Ames Research Center.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Copyright (c) 1997 Manuel Bouyer. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* driver for the Broadcom BCM5400 and BCM5700 Gig-E PHYs.
*
* Programming information for this PHY was gleaned from FreeBSD
* (they were apparently able to get a datasheet from Broadcom).
*/
if (device_is_a(parent, "bge"))
bsc->sc_isbge = true;
else if (device_is_a(parent, "bnx"))
bsc->sc_isbnx = true;
dict = device_properties(parent);
if (bsc->sc_isbge || bsc->sc_isbnx) {
if (!prop_dictionary_get_uint32(dict, "phyflags",
&bsc->sc_phyflags))
aprint_error_dev(self, "failed to get phyflags\n");
if (!prop_dictionary_get_uint32(dict, "chipid",
&bsc->sc_chipid))
aprint_error_dev(self, "failed to get chipid\n");
}
if (bsc->sc_isbnx) {
/* Currently, only bnx use sc_shared_hwcfg and sc_port_hwcfg */
if (!prop_dictionary_get_uint32(dict, "shared_hwcfg",
&bsc->sc_shared_hwcfg))
aprint_error_dev(self, "failed to get shared_hwcfg\n");
if (!prop_dictionary_get_uint32(dict, "port_hwcfg",
&bsc->sc_port_hwcfg))
aprint_error_dev(self, "failed to get port_hwcfg\n");
}
if (sc->mii_flags & MIIF_HAVEFIBER) {
if ((sc->mii_mpd_oui == MII_OUI_BROADCOM2)
&& sc->mii_mpd_model == MII_MODEL_BROADCOM2_BCM5708S)
sc->mii_funcs = &brgphy_5708s_funcs;
else if ((sc->mii_mpd_oui == MII_OUI_BROADCOM2)
&& (sc->mii_mpd_model == MII_MODEL_BROADCOM2_BCM5709S)) {
if (bsc->sc_isbnx)
sc->mii_funcs = &brgphy_5709s_funcs;
else {
/*
* XXX
* 5720S and 5709S shares the same PHY id.
* Assume 5720S PHY if parent device is bge(4).
*/
sc->mii_funcs = &brgphy_5708s_funcs;
}
} else
sc->mii_funcs = &brgphy_fiber_funcs;
} else
sc->mii_funcs = &brgphy_copper_funcs;
/*
* Set the proper bits for capabilities so that the
* correct media get selected by mii_phy_add_media()
*/
sc->mii_capabilities |= BMSR_ANEG;
sc->mii_capabilities &= ~BMSR_100T4;
sc->mii_extcapabilities |= EXTSR_1000XFDX;
mii_unlock(mii);
if (bsc->sc_isbnx) {
/*
* 2.5Gb support is a software enabled feature
* on the BCM5708S and BCM5709S controllers.
*/
#define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL)
if (bsc->sc_phyflags
& BNX_PHY_2_5G_CAPABLE_FLAG) {
ADD(IFM_MAKEWORD(IFM_ETHER, IFM_2500_SX,
IFM_FDX, sc->mii_inst), 0);
aprint_normal_dev(self, "2500baseSX-FDX\n");
#undef ADD
}
}
}
mii_phy_add_media(sc);
}
static int
brgphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
{
struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
uint16_t reg, speed, gig;
KASSERT(mii_locked(mii));
switch (cmd) {
case MII_POLLSTAT:
/* If we're not polling our PHY instance, just return. */
if (IFM_INST(ife->ifm_media) != sc->mii_inst)
return 0;
break;
case MII_MEDIACHG:
/*
* If the media indicates a different PHY instance,
* isolate ourselves.
*/
if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
PHY_READ(sc, MII_BMCR, ®);
PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
return 0;
}
/* If the interface is not up, don't do anything. */
if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
break;
PHY_RESET(sc); /* XXX hardware bug work-around */
switch (IFM_SUBTYPE(ife->ifm_media)) {
case IFM_AUTO:
(void) brgphy_mii_phy_auto(sc);
break;
case IFM_2500_SX:
speed = BRGPHY_5708S_BMCR_2500;
goto setit;
case IFM_1000_SX:
case IFM_1000_T:
speed = BMCR_S1000;
goto setit;
case IFM_100_TX:
speed = BMCR_S100;
goto setit;
case IFM_10_T:
speed = BMCR_S10;
setit:
brgphy_loop(sc);
if ((ife->ifm_media & IFM_FDX) != 0) {
speed |= BMCR_FDX;
gig = GTCR_ADV_1000TFDX;
} else
gig = GTCR_ADV_1000THDX;
case MII_TICK:
/* If we're not currently selected, just return. */
if (IFM_INST(ife->ifm_media) != sc->mii_inst)
return 0;
/* Is the interface even up? */
if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
return 0;
/* Only used for autonegotiation. */
if ((IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) &&
(IFM_SUBTYPE(ife->ifm_media) != IFM_1000_T)) {
sc->mii_ticks = 0;
break;
}
/*
* Check for link.
* Read the status register twice; BMSR_LINK is latch-low.
*/
PHY_READ(sc, MII_BMSR, ®);
PHY_READ(sc, MII_BMSR, ®);
if (reg & BMSR_LINK) {
sc->mii_ticks = 0;
break;
}
/*
* mii_ticks == 0 means it's the first tick after changing the
* media or the link became down since the last tick
* (see above), so break to update the status.
*/
if (sc->mii_ticks++ == 0)
break;
/* Only retry autonegotiation every mii_anegticks seconds. */
KASSERT(sc->mii_anegticks != 0);
if (sc->mii_ticks < sc->mii_anegticks)
break;
brgphy_mii_phy_auto(sc);
break;
case MII_DOWN:
mii_phy_down(sc);
return 0;
}
/* Update the media status. */
mii_phy_status(sc);
/*
* Callback if something changed. Note that we need to poke the DSP on
* the Broadcom PHYs if the media changes.
*/
if (sc->mii_media_active != mii->mii_media_active ||
sc->mii_media_status != mii->mii_media_status ||
cmd == MII_MEDIACHG) {
switch (sc->mii_mpd_oui) {
case MII_OUI_BROADCOM:
switch (sc->mii_mpd_model) {
case MII_MODEL_BROADCOM_BCM5400:
brgphy_bcm5401_dspcode(sc);
break;
case MII_MODEL_BROADCOM_BCM5401:
if (sc->mii_mpd_rev == 1 || sc->mii_mpd_rev == 3)
brgphy_bcm5401_dspcode(sc);
break;
case MII_MODEL_BROADCOM_BCM5411:
brgphy_bcm5411_dspcode(sc);
break;
}
break;
case MII_OUI_BROADCOM4:
switch (sc->mii_mpd_model) {
case MII_MODEL_BROADCOM4_BCM54213PE:
brgphy_bcm54xx_clock_delay(sc);
break;
}
}
}
if (bmcr & BMCR_LOOP)
mii->mii_media_active |= IFM_LOOP;
if (bmcr & BMCR_AUTOEN) {
/*
* The media status bits are only valid if autonegotiation
* has completed (or it's disabled).
*/
if ((bmsr & BMSR_ACOMP) == 0) {
/* Erg, still trying, I guess... */
mii->mii_media_active |= IFM_NONE;
return;
}
if (bmcr & BMCR_LOOP)
mii->mii_media_active |= IFM_LOOP;
if (bmcr & BMCR_AUTOEN) {
/*
* The media status bits are only valid of autonegotiation
* has completed (or it's disabled).
*/
if ((bmsr & BMSR_ACOMP) == 0) {
/* Erg, still trying, I guess... */
mii->mii_media_active |= IFM_NONE;
return;
}
/* 5709S has its own general purpose status registers */
PHY_WRITE(sc, BRGPHY_BLOCK_ADDR, BRGPHY_BLOCK_ADDR_GP_STATUS);
PHY_READ(sc, BRGPHY_GP_STATUS_TOP_ANEG_STATUS, &auxsts);
mii_phy_reset(sc);
switch (sc->mii_mpd_oui) {
case MII_OUI_BROADCOM:
switch (sc->mii_mpd_model) {
case MII_MODEL_BROADCOM_BCM5400:
brgphy_bcm5401_dspcode(sc);
break;
case MII_MODEL_BROADCOM_BCM5401:
if (sc->mii_mpd_rev == 1 || sc->mii_mpd_rev == 3)
brgphy_bcm5401_dspcode(sc);
break;
case MII_MODEL_BROADCOM_BCM5411:
brgphy_bcm5411_dspcode(sc);
break;
case MII_MODEL_BROADCOM_BCM5421:
brgphy_bcm5421_dspcode(sc);
break;
case MII_MODEL_BROADCOM_BCM54K2:
brgphy_bcm54k2_dspcode(sc);
break;
}
break;
case MII_OUI_BROADCOM3:
switch (sc->mii_mpd_model) {
case MII_MODEL_BROADCOM3_BCM5717C:
case MII_MODEL_BROADCOM3_BCM5719C:
case MII_MODEL_BROADCOM3_BCM5720C:
case MII_MODEL_BROADCOM3_BCM57765:
return;
}
break;
default:
break;
}
/* Handle any bge (NetXtreme/NetLink) workarounds. */
if (bsc->sc_isbge) {
if (!(sc->mii_flags & MIIF_HAVEFIBER)) {
if (bsc->sc_phyflags & BGEPHYF_ADC_BUG)
brgphy_adc_bug(sc);
if (bsc->sc_phyflags & BGEPHYF_5704_A0_BUG)
brgphy_5704_a0_bug(sc);
if (bsc->sc_phyflags & BGEPHYF_BER_BUG)
brgphy_ber_bug(sc);
else if (bsc->sc_phyflags & BGEPHYF_JITTER_BUG) {
PHY_WRITE(sc, BRGPHY_MII_AUXCTL, 0x0c00);
PHY_WRITE(sc, BRGPHY_MII_DSP_ADDR_REG, 0x000a);
if (bsc->sc_phyflags & BNX_PHY_2_5G_CAPABLE_FLAG) {
/* Select the Over 1G block of the AN MMD. */
PHY_WRITE(sc, BRGPHY_BLOCK_ADDR,
BRGPHY_BLOCK_ADDR_OVER_1G);
/* Set Class A mode */
PHY_WRITE(sc, BRGPHY_MII_AUXCTL, 0x1007);
PHY_READ(sc, BRGPHY_MII_AUXCTL, &data);
PHY_WRITE(sc, BRGPHY_MII_AUXCTL, data | 0x0400);
/* Set FFE gamma override to -0.125 */
PHY_WRITE(sc, BRGPHY_MII_AUXCTL, 0x0007);
PHY_READ(sc, BRGPHY_MII_AUXCTL, &data);
PHY_WRITE(sc, BRGPHY_MII_AUXCTL, data | 0x0800);
PHY_WRITE(sc, BRGPHY_MII_DSP_ADDR_REG, 0x000a);
PHY_READ(sc, BRGPHY_MII_DSP_RW_PORT, &data);
PHY_WRITE(sc, BRGPHY_MII_DSP_RW_PORT, data | 0x0200);
}