Index: stable/4/sys/dev/mii/amphy.c =================================================================== --- stable/4/sys/dev/mii/amphy.c (revision 69923) +++ stable/4/sys/dev/mii/amphy.c (revision 69924) @@ -1,351 +1,352 @@ /* * Copyright (c) 1997, 1998, 1999 * Bill Paul . 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. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by Bill Paul. * 4. Neither the name of the author nor the names of any co-contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY Bill Paul 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 Bill Paul OR THE VOICES IN HIS HEAD * 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. * * $FreeBSD$ */ /* * driver for AMD AM79c873 PHYs * This driver also works for the Davicom DM9101 PHY, which appears to * be an AM79c873 workalike. */ #include #include #include #include #include #include #include #include #include #include #include #include #include "miibus_if.h" #if !defined(lint) static const char rcsid[] = "$FreeBSD$"; #endif static int amphy_probe __P((device_t)); static int amphy_attach __P((device_t)); static int amphy_detach __P((device_t)); static device_method_t amphy_methods[] = { /* device interface */ DEVMETHOD(device_probe, amphy_probe), DEVMETHOD(device_attach, amphy_attach), DEVMETHOD(device_detach, amphy_detach), DEVMETHOD(device_shutdown, bus_generic_shutdown), { 0, 0 } }; static devclass_t amphy_devclass; static driver_t amphy_driver = { "amphy", amphy_methods, sizeof(struct mii_softc) }; DRIVER_MODULE(amphy, miibus, amphy_driver, amphy_devclass, 0, 0); int amphy_service __P((struct mii_softc *, struct mii_data *, int)); void amphy_status __P((struct mii_softc *)); static int amphy_probe(dev) device_t dev; { struct mii_attach_args *ma; ma = device_get_ivars(dev); if ((MII_OUI(ma->mii_id1, ma->mii_id2) != MII_OUI_xxAMD || MII_MODEL(ma->mii_id2) != MII_MODEL_xxAMD_79C873) && (MII_OUI(ma->mii_id1, ma->mii_id2) != MII_OUI_xxDAVICOM || MII_MODEL(ma->mii_id2) != MII_MODEL_xxDAVICOM_DM9101)) return(ENXIO); if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_xxAMD) device_set_desc(dev, MII_STR_xxAMD_79C873); else if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_xxDAVICOM) device_set_desc(dev, MII_STR_xxDAVICOM_DM9101); return(0); } static int amphy_attach(dev) device_t dev; { struct mii_softc *sc; struct mii_attach_args *ma; struct mii_data *mii; sc = device_get_softc(dev); ma = device_get_ivars(dev); sc->mii_dev = device_get_parent(dev); mii = device_get_softc(sc->mii_dev); LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list); sc->mii_inst = mii->mii_instance; sc->mii_phy = ma->mii_phyno; sc->mii_service = amphy_service; sc->mii_pdata = mii; sc->mii_flags |= MIIF_NOISOLATE; mii->mii_instance++; #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL) ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst), BMCR_ISO); #if 0 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->mii_inst), BMCR_LOOP|BMCR_S100); #endif mii_phy_reset(sc); sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & ma->mii_capmask; device_printf(dev, " "); if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0) printf("no media present"); else mii_add_media(mii, sc->mii_capabilities, sc->mii_inst); printf("\n"); #undef ADD MIIBUS_MEDIAINIT(sc->mii_dev); return(0); } static int amphy_detach(dev) device_t dev; { struct mii_softc *sc; struct mii_data *mii; sc = device_get_softc(dev); mii = device_get_softc(device_get_parent(dev)); + mii_phy_auto_stop(sc); sc->mii_dev = NULL; LIST_REMOVE(sc, mii_list); return(0); } int amphy_service(sc, mii, cmd) struct mii_softc *sc; struct mii_data *mii; int cmd; { struct ifmedia_entry *ife = mii->mii_media.ifm_cur; int reg; 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) { reg = 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; switch (IFM_SUBTYPE(ife->ifm_media)) { case IFM_AUTO: /* * If we're already in auto mode, just return. */ if (PHY_READ(sc, MII_BMCR) & BMCR_AUTOEN) return (0); (void) mii_phy_auto(sc, 1); break; case IFM_100_T4: /* * XXX Not supported as a manual setting right now. */ return (EINVAL); default: /* * BMCR data is stored in the ifmedia entry. */ PHY_WRITE(sc, MII_ANAR, mii_anar(ife->ifm_media)); PHY_WRITE(sc, MII_BMCR, ife->ifm_data); } break; case MII_TICK: /* * If we're not currently selected, just return. */ if (IFM_INST(ife->ifm_media) != sc->mii_inst) return (0); /* * Only used for autonegotiation. */ if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) return (0); /* * Is the interface even up? */ if ((mii->mii_ifp->if_flags & IFF_UP) == 0) return (0); /* * Only retry autonegotiation every 5 seconds. */ if (++sc->mii_ticks != 5) return (0); sc->mii_ticks = 0; /* * Check to see if we have link. If we do, we don't * need to restart the autonegotiation process. Read * the BMSR twice in case it's latched. */ reg = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR); if (reg & BMSR_LINK) break; mii_phy_reset(sc); if (mii_phy_auto(sc, 0) == EJUSTRETURN) return(0); break; } /* Update the media status. */ amphy_status(sc); /* Callback if something changed. */ if (sc->mii_active != mii->mii_media_active || cmd == MII_MEDIACHG) { MIIBUS_STATCHG(sc->mii_dev); sc->mii_active = mii->mii_media_active; } return (0); } void amphy_status(sc) struct mii_softc *sc; { struct mii_data *mii = sc->mii_pdata; int bmsr, bmcr, par, anlpar; mii->mii_media_status = IFM_AVALID; mii->mii_media_active = IFM_ETHER; bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR); if (bmsr & BMSR_LINK) mii->mii_media_status |= IFM_ACTIVE; bmcr = PHY_READ(sc, MII_BMCR); if (bmcr & BMCR_ISO) { mii->mii_media_active |= IFM_NONE; mii->mii_media_status = 0; return; } if (bmcr & BMCR_LOOP) mii->mii_media_active |= IFM_LOOP; if (bmcr & BMCR_AUTOEN) { /* * The PAR 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; } if (PHY_READ(sc, MII_ANER) & ANER_LPAN) { anlpar = PHY_READ(sc, MII_ANAR) & PHY_READ(sc, MII_ANLPAR); if (anlpar & ANLPAR_T4) mii->mii_media_active |= IFM_100_T4; else if (anlpar & ANLPAR_TX_FD) mii->mii_media_active |= IFM_100_TX|IFM_FDX; else if (anlpar & ANLPAR_TX) mii->mii_media_active |= IFM_100_TX; else if (anlpar & ANLPAR_10_FD) mii->mii_media_active |= IFM_10_T|IFM_FDX; else if (anlpar & ANLPAR_10) mii->mii_media_active |= IFM_10_T; else mii->mii_media_active |= IFM_NONE; return; } /* * Link partner is not capable of autonegotiation. */ par = PHY_READ(sc, MII_AMPHY_DSCSR); if (par & DSCSR_100FDX) mii->mii_media_active |= IFM_100_TX|IFM_FDX; else if (par & DSCSR_100HDX) mii->mii_media_active |= IFM_100_TX; else if (par & DSCSR_10FDX) mii->mii_media_active |= IFM_10_T|IFM_HDX; else if (par & DSCSR_10HDX) mii->mii_media_active |= IFM_10_T; } else mii->mii_media_active = mii_media_from_bmcr(bmcr); } Index: stable/4/sys/dev/mii/brgphy.c =================================================================== --- stable/4/sys/dev/mii/brgphy.c (revision 69923) +++ stable/4/sys/dev/mii/brgphy.c (revision 69924) @@ -1,406 +1,408 @@ /* * Copyright (c) 2000 * Bill Paul . 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. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by Bill Paul. * 4. Neither the name of the author nor the names of any co-contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY Bill Paul 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 Bill Paul OR THE VOICES IN HIS HEAD * 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. * * $FreeBSD$ */ /* * Driver for the Broadcom BCR5400 1000baseTX PHY. Speed is always * 1000mbps; all we need to negotiate here is full or half duplex. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include "miibus_if.h" #if !defined(lint) static const char rcsid[] = "$FreeBSD$"; #endif static int brgphy_probe __P((device_t)); static int brgphy_attach __P((device_t)); static int brgphy_detach __P((device_t)); static device_method_t brgphy_methods[] = { /* device interface */ DEVMETHOD(device_probe, brgphy_probe), DEVMETHOD(device_attach, brgphy_attach), DEVMETHOD(device_detach, brgphy_detach), DEVMETHOD(device_shutdown, bus_generic_shutdown), { 0, 0 } }; static devclass_t brgphy_devclass; static driver_t brgphy_driver = { "brgphy", brgphy_methods, sizeof(struct mii_softc) }; DRIVER_MODULE(brgphy, miibus, brgphy_driver, brgphy_devclass, 0, 0); int brgphy_service __P((struct mii_softc *, struct mii_data *, int)); void brgphy_status __P((struct mii_softc *)); static int brgphy_mii_phy_auto __P((struct mii_softc *, int)); extern void mii_phy_auto_timeout __P((void *)); static int brgphy_probe(dev) device_t dev; { struct mii_attach_args *ma; ma = device_get_ivars(dev); if (MII_OUI(ma->mii_id1, ma->mii_id2) != MII_OUI_xxBROADCOM || MII_MODEL(ma->mii_id2) != MII_MODEL_xxBROADCOM_BCM5400) return(ENXIO); device_set_desc(dev, MII_STR_xxBROADCOM_BCM5400); return(0); } static int brgphy_attach(dev) device_t dev; { struct mii_softc *sc; struct mii_attach_args *ma; struct mii_data *mii; const char *sep = ""; sc = device_get_softc(dev); ma = device_get_ivars(dev); sc->mii_dev = device_get_parent(dev); mii = device_get_softc(sc->mii_dev); LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list); sc->mii_inst = mii->mii_instance; sc->mii_phy = ma->mii_phyno; sc->mii_service = brgphy_service; sc->mii_pdata = mii; sc->mii_flags |= MIIF_NOISOLATE; mii->mii_instance++; #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL) #define PRINT(s) printf("%s%s", sep, s); sep = ", " ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst), BMCR_ISO); #if 0 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->mii_inst), BMCR_LOOP|BMCR_S100); #endif mii_phy_reset(sc); device_printf(dev, " "); ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_TX, 0, sc->mii_inst), BRGPHY_BMCR_FDX); PRINT("1000baseTX"); ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_TX, IFM_FDX, sc->mii_inst), 0); PRINT("1000baseTX-FDX"); ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst), 0); PRINT("auto"); printf("\n"); #undef ADD #undef PRINT MIIBUS_MEDIAINIT(sc->mii_dev); return(0); } static int brgphy_detach(dev) device_t dev; { struct mii_softc *sc; struct mii_data *mii; sc = device_get_softc(dev); mii = device_get_softc(device_get_parent(dev)); + if (sc->mii_flags & MIIF_DOINGAUTO) + untimeout(mii_phy_auto_timeout, sc, sc->mii_auto_ch); sc->mii_dev = NULL; LIST_REMOVE(sc, mii_list); return(0); } int brgphy_service(sc, mii, cmd) struct mii_softc *sc; struct mii_data *mii; int cmd; { struct ifmedia_entry *ife = mii->mii_media.ifm_cur; int reg; 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) { reg = 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_WRITE(sc, BRGPHY_MII_PHY_EXTCTL, BRGPHY_PHY_EXTCTL_HIGH_LA|BRGPHY_PHY_EXTCTL_EN_LTR); PHY_WRITE(sc, BRGPHY_MII_AUXCTL, BRGPHY_AUXCTL_LONG_PKT|BRGPHY_AUXCTL_TX_TST); PHY_WRITE(sc, BRGPHY_MII_IMR, 0xFF00); switch (IFM_SUBTYPE(ife->ifm_media)) { case IFM_AUTO: #ifdef foo /* * If we're already in auto mode, just return. */ if (PHY_READ(sc, BRGPHY_MII_BMCR) & BRGPHY_BMCR_AUTOEN) return (0); #endif (void) brgphy_mii_phy_auto(sc, 1); break; case IFM_1000_TX: if ((ife->ifm_media & IFM_GMASK) == IFM_FDX) { PHY_WRITE(sc, BRGPHY_MII_BMCR, BRGPHY_BMCR_FDX|BRGPHY_BMCR_SPD1); } else { PHY_WRITE(sc, BRGPHY_MII_BMCR, BRGPHY_BMCR_SPD1); } PHY_WRITE(sc, BRGPHY_MII_ANAR, BRGPHY_SEL_TYPE); /* * When settning the link manually, one side must * be the master and the other the slave. However * ifmedia doesn't give us a good way to specify * this, so we fake it by using one of the LINK * flags. If LINK0 is set, we program the PHY to * be a master, otherwise it's a slave. */ if ((mii->mii_ifp->if_flags & IFF_LINK0)) { PHY_WRITE(sc, BRGPHY_MII_1000CTL, BRGPHY_1000CTL_MSE|BRGPHY_1000CTL_MSC); } else { PHY_WRITE(sc, BRGPHY_MII_1000CTL, BRGPHY_1000CTL_MSE); } break; case IFM_100_T4: case IFM_100_TX: case IFM_10_T: default: return (EINVAL); } break; case MII_TICK: /* * If we're not currently selected, just return. */ if (IFM_INST(ife->ifm_media) != sc->mii_inst) return (0); /* * Only used for autonegotiation. */ if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) return (0); /* * Is the interface even up? */ if ((mii->mii_ifp->if_flags & IFF_UP) == 0) return (0); /* * Only retry autonegotiation every 5 seconds. */ if (++sc->mii_ticks != 5) return (0); sc->mii_ticks = 0; /* * Check to see if we have link. If we do, we don't * need to restart the autonegotiation process. Read * the BMSR twice in case it's latched. */ reg = PHY_READ(sc, BRGPHY_MII_AUXSTS); if (reg & BRGPHY_AUXSTS_LINK) break; mii_phy_reset(sc); if (brgphy_mii_phy_auto(sc, 0) == EJUSTRETURN) return(0); break; } /* Update the media status. */ brgphy_status(sc); /* Callback if something changed. */ if (sc->mii_active != mii->mii_media_active || cmd == MII_MEDIACHG) { MIIBUS_STATCHG(sc->mii_dev); sc->mii_active = mii->mii_media_active; } return (0); } void brgphy_status(sc) struct mii_softc *sc; { struct mii_data *mii = sc->mii_pdata; int bmsr, bmcr, anlpar; mii->mii_media_status = IFM_AVALID; mii->mii_media_active = IFM_ETHER; bmsr = PHY_READ(sc, BRGPHY_MII_BMSR); if (PHY_READ(sc, BRGPHY_MII_AUXSTS) & BRGPHY_AUXSTS_LINK) mii->mii_media_status |= IFM_ACTIVE; bmcr = PHY_READ(sc, BRGPHY_MII_BMCR); if (bmcr & BRGPHY_BMCR_LOOP) mii->mii_media_active |= IFM_LOOP; if (bmcr & BRGPHY_BMCR_AUTOEN) { if ((bmsr & BRGPHY_BMSR_ACOMP) == 0) { /* Erg, still trying, I guess... */ mii->mii_media_active |= IFM_NONE; return; } mii->mii_media_active |= IFM_1000_TX; anlpar = PHY_READ(sc, BRGPHY_MII_AUXSTS); if ((anlpar & BRGPHY_AUXSTS_AN_RES) == BRGPHY_RES_1000FD) mii->mii_media_active |= IFM_FDX; if ((anlpar & BRGPHY_AUXSTS_AN_RES) == BRGPHY_RES_1000HD) mii->mii_media_active |= IFM_HDX; return; } mii->mii_media_active |= IFM_1000_TX; if (bmcr & BRGPHY_BMCR_FDX) mii->mii_media_active |= IFM_FDX; else mii->mii_media_active |= IFM_HDX; return; } static int brgphy_mii_phy_auto(mii, waitfor) struct mii_softc *mii; int waitfor; { int bmsr, i; if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) { PHY_WRITE(mii, BRGPHY_MII_1000CTL, BRGPHY_1000CTL_AFD|BRGPHY_1000CTL_AHD); PHY_WRITE(mii, BRGPHY_MII_ANAR, BRGPHY_SEL_TYPE); PHY_WRITE(mii, BRGPHY_MII_BMCR, BRGPHY_BMCR_AUTOEN | BRGPHY_BMCR_STARTNEG); PHY_WRITE(mii, BRGPHY_MII_IMR, 0xFF00); } if (waitfor) { /* Wait 500ms for it to complete. */ for (i = 0; i < 500; i++) { if ((bmsr = PHY_READ(mii, BRGPHY_MII_BMSR)) & BRGPHY_BMSR_ACOMP) return (0); DELAY(1000); #if 0 if ((bmsr & BMSR_ACOMP) == 0) printf("%s: autonegotiation failed to complete\n", mii->mii_dev.dv_xname); #endif } /* * Don't need to worry about clearing MIIF_DOINGAUTO. * If that's set, a timeout is pending, and it will * clear the flag. */ return (EIO); } /* * Just let it finish asynchronously. This is for the benefit of * the tick handler driving autonegotiation. Don't want 500ms * delays all the time while the system is running! */ if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) { mii->mii_flags |= MIIF_DOINGAUTO; - timeout(mii_phy_auto_timeout, mii, hz >> 1); + mii->mii_auto_ch = timeout(mii_phy_auto_timeout, mii, hz >> 1); } return (EJUSTRETURN); } Index: stable/4/sys/dev/mii/exphy.c =================================================================== --- stable/4/sys/dev/mii/exphy.c (revision 69923) +++ stable/4/sys/dev/mii/exphy.c (revision 69924) @@ -1,316 +1,317 @@ /* $NetBSD: exphy.c,v 1.16 1999/04/23 04:24:32 thorpej Exp $ */ /*- * Copyright (c) 1998, 1999 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, and by Frank van der Linden. * * 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. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the NetBSD * Foundation, Inc. and its contributors. * 4. Neither the name of The NetBSD Foundation nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * 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. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by Manuel Bouyer. * 4. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * 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 3Com internal PHYs */ #include #include #include #include #include #include #include #include #include #include #include #include #include "miibus_if.h" #if !defined(lint) static const char rcsid[] = "$FreeBSD$"; #endif static int exphy_probe __P((device_t)); static int exphy_attach __P((device_t)); static int exphy_detach __P((device_t)); static device_method_t exphy_methods[] = { /* device interface */ DEVMETHOD(device_probe, exphy_probe), DEVMETHOD(device_attach, exphy_attach), DEVMETHOD(device_detach, exphy_detach), DEVMETHOD(device_shutdown, bus_generic_shutdown), { 0, 0 } }; static devclass_t exphy_devclass; static driver_t exphy_driver = { "xlphy", exphy_methods, sizeof(struct mii_softc) }; DRIVER_MODULE(xlphy, miibus, exphy_driver, exphy_devclass, 0, 0); int exphy_service __P((struct mii_softc *, struct mii_data *, int)); void exphy_reset __P((struct mii_softc *)); static int exphy_probe(dev) device_t dev; { struct mii_attach_args *ma; device_t parent; ma = device_get_ivars(dev); parent = device_get_parent(device_get_parent(dev)); /* * Argh, 3Com PHY reports oui == 0 model == 0! */ if ((MII_OUI(ma->mii_id1, ma->mii_id2) != 0 || MII_MODEL(ma->mii_id2) != 0) && (MII_OUI(ma->mii_id1, ma->mii_id2) != MII_OUI_BROADCOM || MII_MODEL(ma->mii_id2) != MII_MODEL_BROADCOM_3c905Cphy)) return (ENXIO); /* * Make sure the parent is an `ex'. */ if (strcmp(device_get_name(parent), "xl") != 0) return (ENXIO); if (MII_OUI(ma->mii_id1, ma->mii_id2) == 0) device_set_desc(dev, "3Com internal media interface"); else device_set_desc(dev, MII_STR_BROADCOM_3c905Cphy); return (0); } static int exphy_attach(dev) device_t dev; { struct mii_softc *sc; struct mii_attach_args *ma; struct mii_data *mii; sc = device_get_softc(dev); ma = device_get_ivars(dev); sc->mii_dev = device_get_parent(dev); mii = device_get_softc(sc->mii_dev); /* * The 3Com PHY can never be isolated, so never allow non-zero * instances! */ if (mii->mii_instance != 0) { device_printf(dev, "ignoring this PHY, non-zero instance\n"); return(ENXIO); } LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list); sc->mii_inst = mii->mii_instance; sc->mii_phy = ma->mii_phyno; sc->mii_service = exphy_service; sc->mii_pdata = mii; mii->mii_instance++; sc->mii_flags |= MIIF_NOISOLATE; #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL) #if 0 /* See above. */ ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst), BMCR_ISO); #endif ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->mii_inst), BMCR_LOOP|BMCR_S100); exphy_reset(sc); sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & ma->mii_capmask; device_printf(dev, " "); if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0) printf("no media present"); else mii_add_media(mii, sc->mii_capabilities, sc->mii_inst); printf("\n"); #undef ADD MIIBUS_MEDIAINIT(sc->mii_dev); return(0); } static int exphy_detach(dev) device_t dev; { struct mii_softc *sc; struct mii_data *mii; sc = device_get_softc(dev); mii = device_get_softc(device_get_parent(dev)); + mii_phy_auto_stop(sc); sc->mii_dev = NULL; LIST_REMOVE(sc, mii_list); return(0); } int exphy_service(sc, mii, cmd) struct mii_softc *sc; struct mii_data *mii; int cmd; { struct ifmedia_entry *ife = mii->mii_media.ifm_cur; /* * We can't isolate the 3Com PHY, so it has to be the only one! */ if (IFM_INST(ife->ifm_media) != sc->mii_inst) panic("exphy_service: can't isolate 3Com PHY"); switch (cmd) { case MII_POLLSTAT: break; case MII_MEDIACHG: /* * If the interface is not up, don't do anything. */ if ((mii->mii_ifp->if_flags & IFF_UP) == 0) break; switch (IFM_SUBTYPE(ife->ifm_media)) { case IFM_AUTO: /* * If we're already in auto mode, just return. */ if (PHY_READ(sc, MII_BMCR) & BMCR_AUTOEN) return (0); (void) mii_phy_auto(sc, 1); break; case IFM_100_T4: /* * XXX Not supported as a manual setting right now. */ return (EINVAL); default: /* * BMCR data is stored in the ifmedia entry. */ PHY_WRITE(sc, MII_ANAR, mii_anar(ife->ifm_media)); PHY_WRITE(sc, MII_BMCR, ife->ifm_data); } break; case MII_TICK: /* * Only used for autonegotiation. */ if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) return (0); /* * Is the interface even up? */ if ((mii->mii_ifp->if_flags & IFF_UP) == 0) return (0); /* * The 3Com PHY's autonegotiation doesn't need to be * kicked; it continues in the background. */ break; } /* Update the media status. */ ukphy_status(sc); /* Callback if something changed. */ if (sc->mii_active != mii->mii_media_active || cmd == MII_MEDIACHG) { MIIBUS_STATCHG(sc->mii_dev); sc->mii_active = mii->mii_media_active; } return (0); } void exphy_reset(sc) struct mii_softc *sc; { mii_phy_reset(sc); /* * XXX 3Com PHY doesn't set the BMCR properly after * XXX reset, which breaks autonegotiation. */ PHY_WRITE(sc, MII_BMCR, BMCR_S100|BMCR_AUTOEN|BMCR_FDX); } Index: stable/4/sys/dev/mii/mii_physubr.c =================================================================== --- stable/4/sys/dev/mii/mii_physubr.c (revision 69923) +++ stable/4/sys/dev/mii/mii_physubr.c (revision 69924) @@ -1,264 +1,274 @@ /* $NetBSD: mii_physubr.c,v 1.5 1999/08/03 19:41:49 drochner Exp $ */ /*- * Copyright (c) 1998, 1999 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. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the NetBSD * Foundation, Inc. and its contributors. * 4. Neither the name of The NetBSD Foundation nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * 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. */ /* * Subroutines common to all PHYs. */ #include #include #include #include #include #include #include #include #include #include #include #include #include "miibus_if.h" #if !defined(lint) static const char rcsid[] = "$FreeBSD$"; #endif void mii_phy_auto_timeout __P((void *)); int mii_phy_auto(mii, waitfor) struct mii_softc *mii; int waitfor; { int bmsr, i; if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) { PHY_WRITE(mii, MII_ANAR, BMSR_MEDIA_TO_ANAR(mii->mii_capabilities) | ANAR_CSMA); PHY_WRITE(mii, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG); } if (waitfor) { /* Wait 500ms for it to complete. */ for (i = 0; i < 500; i++) { if ((bmsr = PHY_READ(mii, MII_BMSR)) & BMSR_ACOMP) return (0); DELAY(1000); #if 0 if ((bmsr & BMSR_ACOMP) == 0) printf("%s: autonegotiation failed to complete\n", mii->mii_dev.dv_xname); #endif } /* * Don't need to worry about clearing MIIF_DOINGAUTO. * If that's set, a timeout is pending, and it will * clear the flag. */ return (EIO); } /* * Just let it finish asynchronously. This is for the benefit of * the tick handler driving autonegotiation. Don't want 500ms * delays all the time while the system is running! */ if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) { mii->mii_flags |= MIIF_DOINGAUTO; - timeout(mii_phy_auto_timeout, mii, hz >> 1); + mii->mii_auto_ch = timeout(mii_phy_auto_timeout, mii, hz >> 1); } return (EJUSTRETURN); +} + +void +mii_phy_auto_stop(sc) + struct mii_softc *sc; +{ + if (sc->mii_flags & MIIF_DOINGAUTO) { + sc->mii_flags &= ~MIIF_DOINGAUTO; + untimeout(mii_phy_auto_timeout, sc, sc->mii_auto_ch); + } } void mii_phy_auto_timeout(arg) void *arg; { struct mii_softc *mii = arg; int s, bmsr; s = splnet(); mii->mii_flags &= ~MIIF_DOINGAUTO; bmsr = PHY_READ(mii, MII_BMSR); #if 0 if ((bmsr & BMSR_ACOMP) == 0) printf("%s: autonegotiation failed to complete\n", sc->sc_dev.dv_xname); #endif /* Update the media status. */ (void) (*mii->mii_service)(mii, mii->mii_pdata, MII_POLLSTAT); splx(s); } void mii_phy_reset(mii) struct mii_softc *mii; { int reg, i; if (mii->mii_flags & MIIF_NOISOLATE) reg = BMCR_RESET; else reg = BMCR_RESET | BMCR_ISO; PHY_WRITE(mii, MII_BMCR, reg); /* Wait 100ms for it to complete. */ for (i = 0; i < 100; i++) { reg = PHY_READ(mii, MII_BMCR); if ((reg & BMCR_RESET) == 0) break; DELAY(1000); } if (mii->mii_inst != 0 && ((mii->mii_flags & MIIF_NOISOLATE) == 0)) PHY_WRITE(mii, MII_BMCR, reg | BMCR_ISO); } /* * Given an ifmedia word, return the corresponding ANAR value. */ int mii_anar(media) int media; { int rv; switch (media & (IFM_TMASK|IFM_NMASK|IFM_FDX)) { case IFM_ETHER|IFM_10_T: rv = ANAR_10|ANAR_CSMA; break; case IFM_ETHER|IFM_10_T|IFM_FDX: rv = ANAR_10_FD|ANAR_CSMA; break; case IFM_ETHER|IFM_100_TX: rv = ANAR_TX|ANAR_CSMA; break; case IFM_ETHER|IFM_100_TX|IFM_FDX: rv = ANAR_TX_FD|ANAR_CSMA; break; case IFM_ETHER|IFM_100_T4: rv = ANAR_T4|ANAR_CSMA; break; default: rv = 0; break; } return (rv); } /* * Given a BMCR value, return the corresponding ifmedia word. */ int mii_media_from_bmcr(bmcr) int bmcr; { int rv = IFM_ETHER; if (bmcr & BMCR_S100) rv |= IFM_100_TX; else rv |= IFM_10_T; if (bmcr & BMCR_FDX) rv |= IFM_FDX; return (rv); } /* * Initialize generic PHY media based on BMSR, called when a PHY is * attached. We expect to be set up to print a comma-separated list * of media names. Does not print a newline. */ void mii_add_media(mii, bmsr, instance) struct mii_data *mii; int bmsr, instance; { const char *sep = ""; #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL) #define PRINT(s) printf("%s%s", sep, s); sep = ", " if (bmsr & BMSR_10THDX) { ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, instance), 0); PRINT("10baseT"); } if (bmsr & BMSR_10TFDX) { ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, instance), BMCR_FDX); PRINT("10baseT-FDX"); } if (bmsr & BMSR_100TXHDX) { ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, instance), BMCR_S100); PRINT("100baseTX"); } if (bmsr & BMSR_100TXFDX) { ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, instance), BMCR_S100|BMCR_FDX); PRINT("100baseTX-FDX"); } if (bmsr & BMSR_100T4) { /* * XXX How do you enable 100baseT4? I assume we set * XXX BMCR_S100 and then assume the PHYs will take * XXX watever action is necessary to switch themselves * XXX into T4 mode. */ ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_T4, 0, instance), BMCR_S100); PRINT("100baseT4"); } if (bmsr & BMSR_ANEG) { ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, instance), BMCR_AUTOEN); PRINT("auto"); } #undef ADD #undef PRINT } Index: stable/4/sys/dev/mii/miivar.h =================================================================== --- stable/4/sys/dev/mii/miivar.h (revision 69923) +++ stable/4/sys/dev/mii/miivar.h (revision 69924) @@ -1,178 +1,180 @@ /* $NetBSD: miivar.h,v 1.8 1999/04/23 04:24:32 thorpej Exp $ */ /*- * Copyright (c) 1998, 1999 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. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the NetBSD * Foundation, Inc. and its contributors. * 4. Neither the name of The NetBSD Foundation nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * 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. * * $FreeBSD$ */ #ifndef _DEV_MII_MIIVAR_H_ #define _DEV_MII_MIIVAR_H_ #include /* * Media Independent Interface autoconfiguration defintions. * * This file exports an interface which attempts to be compatible * with the BSD/OS 3.0 interface. */ struct mii_softc; /* * Callbacks from MII layer into network interface device driver. */ typedef int (*mii_readreg_t) __P((struct device *, int, int)); typedef void (*mii_writereg_t) __P((struct device *, int, int, int)); typedef void (*mii_statchg_t) __P((struct device *)); /* * A network interface driver has one of these structures in its softc. * It is the interface from the network interface driver to the MII * layer. */ struct mii_data { struct ifmedia mii_media; /* media information */ struct ifnet *mii_ifp; /* pointer back to network interface */ /* * For network interfaces with multiple PHYs, a list of all * PHYs is required so they can all be notified when a media * request is made. */ LIST_HEAD(mii_listhead, mii_softc) mii_phys; int mii_instance; /* * PHY driver fills this in with active media status. */ int mii_media_status; int mii_media_active; /* * Calls from MII layer into network interface driver. */ mii_readreg_t mii_readreg; mii_writereg_t mii_writereg; mii_statchg_t mii_statchg; }; typedef struct mii_data mii_data_t; /* * This call is used by the MII layer to call into the PHY driver * to perform a `service request'. */ typedef int (*mii_downcall_t) __P((struct mii_softc *, struct mii_data *, int)); /* * Requests that can be made to the downcall. */ #define MII_TICK 1 /* once-per-second tick */ #define MII_MEDIACHG 2 /* user changed media; perform the switch */ #define MII_POLLSTAT 3 /* user requested media status; fill it in */ /* * Each PHY driver's softc has one of these as the first member. * XXX This would be better named "phy_softc", but this is the name * XXX BSDI used, and we would like to have the same interface. */ struct mii_softc { device_t mii_dev; /* generic device glue */ LIST_ENTRY(mii_softc) mii_list; /* entry on parent's PHY list */ int mii_phy; /* our MII address */ int mii_inst; /* instance for ifmedia */ mii_downcall_t mii_service; /* our downcall */ struct mii_data *mii_pdata; /* pointer to parent's mii_data */ + struct callout_handle mii_auto_ch; /* callout handle for phy autoneg */ int mii_flags; /* misc. flags; see below */ int mii_capabilities; /* capabilities from BMSR */ int mii_ticks; /* MII_TICK counter */ int mii_active; /* last active media */ }; typedef struct mii_softc mii_softc_t; /* mii_flags */ #define MIIF_NOISOLATE 0x0001 /* do not isolate the PHY */ #define MIIF_DOINGAUTO 0x0002 /* doing autonegotiation */ /* * Used to attach a PHY to a parent. */ struct mii_attach_args { struct mii_data *mii_data; /* pointer to parent data */ int mii_phyno; /* MII address */ int mii_id1; /* PHY ID register 1 */ int mii_id2; /* PHY ID register 2 */ int mii_capmask; /* capability mask from BMSR */ }; typedef struct mii_attach_args mii_attach_args_t; #ifdef _KERNEL #define PHY_READ(p, r) \ MIIBUS_READREG((p)->mii_dev, (p)->mii_phy, (r)) #define PHY_WRITE(p, r, v) \ MIIBUS_WRITEREG((p)->mii_dev, (p)->mii_phy, (r), (v)) extern devclass_t miibus_devclass; extern driver_t miibus_driver; int miibus_probe __P((device_t)); int miibus_attach __P((device_t)); int miibus_detach __P((device_t)); int mii_anar __P((int)); int mii_mediachg __P((struct mii_data *)); void mii_tick __P((struct mii_data *)); void mii_pollstat __P((struct mii_data *)); int mii_phy_probe __P((device_t, device_t *, ifm_change_cb_t, ifm_stat_cb_t)); void mii_add_media __P((struct mii_data *, int, int)); int mii_media_from_bmcr __P((int)); int mii_phy_auto __P((struct mii_softc *, int)); +void mii_phy_auto_stop __P((struct mii_softc *)); void mii_phy_reset __P((struct mii_softc *)); void ukphy_status __P((struct mii_softc *)); #endif /* _KERNEL */ #endif /* _DEV_MII_MIIVAR_H_ */ Index: stable/4/sys/dev/mii/mlphy.c =================================================================== --- stable/4/sys/dev/mii/mlphy.c (revision 69923) +++ stable/4/sys/dev/mii/mlphy.c (revision 69924) @@ -1,456 +1,457 @@ /* * Copyright (c) 1997, 1998, 1999 * Bill Paul . 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. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by Bill Paul. * 4. Neither the name of the author nor the names of any co-contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY Bill Paul 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 Bill Paul OR THE VOICES IN HIS HEAD * 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. * * $FreeBSD$ */ /* * driver for Micro Linear 6692 PHYs * * The Micro Linear 6692 is a strange beast, and dealing with it using * this code framework is tricky. The 6692 is actually a 100Mbps-only * device, which means that a second PHY is required to support 10Mbps * modes. However, even though the 6692 does not support 10Mbps modes, * it can still advertise them when performing autonegotiation. If a * 10Mbps mode is negotiated, we must program the registers of the * companion PHY accordingly in addition to programming the registers * of the 6692. * * This device also does not have vendor/device ID registers. */ #include #include #include #include #include #include #include #include #include #include #include #include #include "miibus_if.h" #define ML_STATE_AUTO_SELF 1 #define ML_STATE_AUTO_OTHER 2 struct mlphy_softc { struct mii_softc ml_mii; int ml_state; int ml_linked; }; static int mlphy_probe __P((device_t)); static int mlphy_attach __P((device_t)); static int mlphy_detach __P((device_t)); static device_method_t mlphy_methods[] = { /* device interface */ DEVMETHOD(device_probe, mlphy_probe), DEVMETHOD(device_attach, mlphy_attach), DEVMETHOD(device_detach, mlphy_detach), DEVMETHOD(device_shutdown, bus_generic_shutdown), { 0, 0 } }; static devclass_t mlphy_devclass; static driver_t mlphy_driver = { "mlphy", mlphy_methods, sizeof(struct mlphy_softc) }; DRIVER_MODULE(mlphy, miibus, mlphy_driver, mlphy_devclass, 0, 0); static int mlphy_service __P((struct mii_softc *, struct mii_data *, int)); static void mlphy_reset __P((struct mii_softc *)); static void mlphy_status __P((struct mii_softc *)); static int mlphy_probe(dev) device_t dev; { struct mii_attach_args *ma; device_t parent; ma = device_get_ivars(dev); parent = device_get_parent(device_get_parent(dev)); /* * Micro Linear PHY reports oui == 0 model == 0 */ if (MII_OUI(ma->mii_id1, ma->mii_id2) != 0 || MII_MODEL(ma->mii_id2) != 0) return (ENXIO); /* * Make sure the parent is a `tl'. So far, I have only * encountered the 6692 on an Olicom card with a ThunderLAN * controller chip. */ if (strcmp(device_get_name(parent), "tl") != 0) return (ENXIO); device_set_desc(dev, "Micro Linear 6692 media interface"); return (0); } static int mlphy_attach(dev) device_t dev; { struct mlphy_softc *msc; struct mii_softc *sc; struct mii_attach_args *ma; struct mii_data *mii; msc = device_get_softc(dev); sc = &msc->ml_mii; ma = device_get_ivars(dev); sc->mii_dev = device_get_parent(dev); mii = device_get_softc(sc->mii_dev); LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list); sc->mii_inst = mii->mii_instance; sc->mii_phy = ma->mii_phyno; sc->mii_service = mlphy_service; sc->mii_pdata = mii; mii->mii_instance++; #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL) #if 0 /* See above. */ ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst), BMCR_ISO); #endif ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->mii_inst), BMCR_LOOP|BMCR_S100); sc->mii_flags &= ~MIIF_NOISOLATE; mii_phy_reset(sc); sc->mii_flags |= MIIF_NOISOLATE; sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & ma->mii_capmask; ma->mii_capmask = ~sc->mii_capabilities; device_printf(dev, " "); if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0) printf("no media present"); else mii_add_media(mii, sc->mii_capabilities, sc->mii_inst); printf("\n"); #undef ADD MIIBUS_MEDIAINIT(sc->mii_dev); return(0); } static int mlphy_detach(dev) device_t dev; { struct mlphy_softc *sc; struct mii_data *mii; sc = device_get_softc(dev); mii = device_get_softc(device_get_parent(dev)); + mii_phy_auto_stop(&sc->ml_mii); sc->ml_mii.mii_dev = NULL; LIST_REMOVE(&sc->ml_mii, mii_list); return(0); } static int mlphy_service(xsc, mii, cmd) struct mii_softc *xsc; struct mii_data *mii; int cmd; { struct ifmedia_entry *ife = mii->mii_media.ifm_cur; int reg; struct mii_softc *other = NULL; struct mlphy_softc *msc = (struct mlphy_softc *)xsc; struct mii_softc *sc = (struct mii_softc *)&msc->ml_mii; device_t *devlist; int devs, i; /* * See if there's another PHY on this bus with us. * If so, we may need it for 10Mbps modes. */ device_get_children(msc->ml_mii.mii_dev, &devlist, &devs); for (i = 0; i < devs; i++) { if (strcmp(device_get_name(devlist[i]), "mlphy")) { other = device_get_softc(devlist[i]); break; } } 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) { reg = 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; switch (IFM_SUBTYPE(ife->ifm_media)) { case IFM_AUTO: /* * For autonegotiation, reset and isolate the * companion PHY (if any) and then do NWAY * autonegotiation ourselves. */ msc->ml_state = ML_STATE_AUTO_SELF; if (other != NULL) { mii_phy_reset(other); PHY_WRITE(other, MII_BMCR, BMCR_ISO); } (void) mii_phy_auto(sc, 1); msc->ml_linked = 0; return(0); break; case IFM_10_T: /* * For 10baseT modes, reset and program the * companion PHY (of any), then program ourselves * to match. This will put us in pass-through * mode and let the companion PHY do all the * work. * * BMCR data is stored in the ifmedia entry. */ if (other != NULL) { mii_phy_reset(other); PHY_WRITE(other, MII_BMCR, ife->ifm_data); } PHY_WRITE(sc, MII_ANAR, mii_anar(ife->ifm_media)); PHY_WRITE(sc, MII_BMCR, ife->ifm_data); msc->ml_state = 0; break; case IFM_100_TX: /* * For 100baseTX modes, reset and isolate the * companion PHY (if any), then program ourselves * accordingly. * * BMCR data is stored in the ifmedia entry. */ if (other != NULL) { mii_phy_reset(other); PHY_WRITE(other, MII_BMCR, BMCR_ISO); } PHY_WRITE(sc, MII_ANAR, mii_anar(ife->ifm_media)); PHY_WRITE(sc, MII_BMCR, ife->ifm_data); msc->ml_state = 0; break; case IFM_100_T4: /* * XXX Not supported as a manual setting right now. */ return (EINVAL); default: break; } break; case MII_TICK: /* * If we're not currently selected, just return. */ if (IFM_INST(ife->ifm_media) != sc->mii_inst) return (0); /* * Only used for autonegotiation. */ if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) return (0); /* * Is the interface even up? */ if ((mii->mii_ifp->if_flags & IFF_UP) == 0) return (0); /* * Only retry autonegotiation every 5 seconds. */ if (++sc->mii_ticks != 5) return (0); sc->mii_ticks = 0; /* * Check to see if we have link. If we do, we don't * need to restart the autonegotiation process. Read * the BMSR twice in case it's latched. * If we're in a 10Mbps mode, check the link of the * 10Mbps PHY. Sometimes the Micro Linear PHY's * linkstat bit will clear while the linkstat bit of * the companion PHY will remain set. */ if (msc->ml_state == ML_STATE_AUTO_OTHER) { reg = PHY_READ(other, MII_BMSR) | PHY_READ(other, MII_BMSR); } else { reg = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR); } if (reg & BMSR_LINK) { if (!msc->ml_linked) { msc->ml_linked = 1; mlphy_status(sc); break; } return(0); } msc->ml_linked = 0; mii->mii_media_active = IFM_NONE; mii_phy_reset(sc); msc->ml_state = ML_STATE_AUTO_SELF; if (other != NULL) { mii_phy_reset(other); PHY_WRITE(other, MII_BMCR, BMCR_ISO); } if (mii_phy_auto(sc, 0) == EJUSTRETURN) return(0); break; } /* Update the media status. */ if (msc->ml_state == ML_STATE_AUTO_OTHER) { int other_inst; other_inst = other->mii_inst; other->mii_inst = sc->mii_inst; (void) (*other->mii_service)(other, mii, MII_POLLSTAT); other->mii_inst = other_inst; sc->mii_active = other->mii_active; } else ukphy_status(sc); /* Callback if something changed. */ if (sc->mii_active != mii->mii_media_active || cmd == MII_MEDIACHG) { MIIBUS_STATCHG(sc->mii_dev); sc->mii_active = mii->mii_media_active; } return (0); } /* * The Micro Linear PHY comes out of reset with the 'autoneg * enable' bit set, which we don't want. */ static void mlphy_reset(sc) struct mii_softc *sc; { int reg; mii_phy_reset(sc); reg = PHY_READ(sc, MII_BMCR); reg &= ~BMCR_AUTOEN; PHY_WRITE(sc, MII_BMCR, reg); return; } /* * If we negotiate a 10Mbps mode, we need to check for an alternate * PHY and make sure it's enabled and set correctly. */ static void mlphy_status(sc) struct mii_softc *sc; { struct mlphy_softc *msc = (struct mlphy_softc *)sc; struct mii_data *mii = msc->ml_mii.mii_pdata; struct mii_softc *other = NULL; device_t *devlist; int devs, i; /* See if there's another PHY on the bus with us. */ device_get_children(msc->ml_mii.mii_dev, &devlist, &devs); for (i = 0; i < devs; i++) { if (strcmp(device_get_name(devlist[i]), "mlphy")) { other = device_get_softc(devlist[i]); break; } } if (other == NULL) return; ukphy_status(sc); if (IFM_SUBTYPE(mii->mii_media_active) != IFM_10_T) { msc->ml_state = ML_STATE_AUTO_SELF; mii_phy_reset(other); PHY_WRITE(other, MII_BMCR, BMCR_ISO); } if (IFM_SUBTYPE(mii->mii_media_active) == IFM_10_T) { msc->ml_state = ML_STATE_AUTO_OTHER; mlphy_reset(&msc->ml_mii); PHY_WRITE(&msc->ml_mii, MII_BMCR, BMCR_ISO); mii_phy_reset(other); mii_phy_auto(other, 1); } return; } Index: stable/4/sys/dev/mii/nsphy.c =================================================================== --- stable/4/sys/dev/mii/nsphy.c (revision 69923) +++ stable/4/sys/dev/mii/nsphy.c (revision 69924) @@ -1,435 +1,436 @@ /* $NetBSD: nsphy.c,v 1.18 1999/07/14 23:57:36 thorpej Exp $ */ /*- * Copyright (c) 1998, 1999 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. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the NetBSD * Foundation, Inc. and its contributors. * 4. Neither the name of The NetBSD Foundation nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * 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. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by Manuel Bouyer. * 4. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * 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 National Semiconductor's DP83840A ethernet 10/100 PHY * Data Sheet available from www.national.com */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include "miibus_if.h" #if !defined(lint) static const char rcsid[] = "$FreeBSD$"; #endif static int nsphy_probe __P((device_t)); static int nsphy_attach __P((device_t)); static int nsphy_detach __P((device_t)); static device_method_t nsphy_methods[] = { /* device interface */ DEVMETHOD(device_probe, nsphy_probe), DEVMETHOD(device_attach, nsphy_attach), DEVMETHOD(device_detach, nsphy_detach), DEVMETHOD(device_shutdown, bus_generic_shutdown), { 0, 0 } }; static devclass_t nsphy_devclass; static driver_t nsphy_driver = { "nsphy", nsphy_methods, sizeof(struct mii_softc) }; DRIVER_MODULE(nsphy, miibus, nsphy_driver, nsphy_devclass, 0, 0); int nsphy_service __P((struct mii_softc *, struct mii_data *, int)); void nsphy_status __P((struct mii_softc *)); static int nsphy_probe(dev) device_t dev; { struct mii_attach_args *ma; ma = device_get_ivars(dev); if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_NATSEMI && MII_MODEL(ma->mii_id2) == MII_MODEL_NATSEMI_DP83840) { device_set_desc(dev, MII_STR_NATSEMI_DP83840); } else if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_QUALSEMI && MII_MODEL(ma->mii_id2) == MII_MODEL_QUALSEMI_QS6612) { device_set_desc(dev, MII_STR_QUALSEMI_QS6612); } else if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_xxALTIMA && MII_MODEL(ma->mii_id2) == MII_MODEL_xxALTIMA_AC101) { device_set_desc(dev, MII_STR_xxALTIMA_AC101); } else return (ENXIO); return (0); } static int nsphy_attach(dev) device_t dev; { struct mii_softc *sc; struct mii_attach_args *ma; struct mii_data *mii; sc = device_get_softc(dev); ma = device_get_ivars(dev); sc->mii_dev = device_get_parent(dev); mii = device_get_softc(sc->mii_dev); LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list); sc->mii_inst = mii->mii_instance; sc->mii_phy = ma->mii_phyno; sc->mii_service = nsphy_service; sc->mii_pdata = mii; #ifdef foo /* * i82557 wedges if all of its PHYs are isolated! */ if (strcmp(device_get_name(device_get_parent(sc->mii_dev)), "fxp") == 0 && mii->mii_instance == 0) #endif sc->mii_flags |= MIIF_NOISOLATE; mii->mii_instance++; #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL) #if 0 /* Can't do this on the i82557! */ ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst), BMCR_ISO); #endif ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->mii_inst), BMCR_LOOP|BMCR_S100); mii_phy_reset(sc); sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & ma->mii_capmask; device_printf(dev, " "); if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0) printf("no media present"); else mii_add_media(mii, sc->mii_capabilities, sc->mii_inst); printf("\n"); #undef ADD MIIBUS_MEDIAINIT(sc->mii_dev); return(0); } static int nsphy_detach(dev) device_t dev; { struct mii_softc *sc; struct mii_data *mii; sc = device_get_softc(dev); mii = device_get_softc(device_get_parent(dev)); + mii_phy_auto_stop(sc); sc->mii_dev = NULL; LIST_REMOVE(sc, mii_list); return(0); } int nsphy_service(sc, mii, cmd) struct mii_softc *sc; struct mii_data *mii; int cmd; { struct ifmedia_entry *ife = mii->mii_media.ifm_cur; int reg; 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) { reg = 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; reg = PHY_READ(sc, MII_NSPHY_PCR); /* * Set up the PCR to use LED4 to indicate full-duplex * in both 10baseT and 100baseTX modes. */ reg |= PCR_LED4MODE; /* * Make sure Carrier Intgrity Monitor function is * disabled (normal for Node operation, but sometimes * it's not set?!) */ reg |= PCR_CIMDIS; /* * Make sure "force link good" is set to normal mode. * It's only intended for debugging. */ reg |= PCR_FLINK100; #if 0 /* * Mystery bits which are supposedly `reserved', * but we seem to need to set them when the PHY * is connected to some interfaces! */ reg |= 0x0100 | 0x0400; #endif /* PHY_WRITE(sc, MII_NSPHY_PCR, reg); */ switch (IFM_SUBTYPE(ife->ifm_media)) { case IFM_AUTO: /* * If we're already in auto mode, just return. */ if (PHY_READ(sc, MII_BMCR) & BMCR_AUTOEN) return (0); (void) mii_phy_auto(sc, 1); break; case IFM_100_T4: /* * XXX Not supported as a manual setting right now. */ return (EINVAL); default: /* * BMCR data is stored in the ifmedia entry. */ PHY_WRITE(sc, MII_ANAR, mii_anar(ife->ifm_media)); PHY_WRITE(sc, MII_BMCR, ife->ifm_data); } break; case MII_TICK: /* * If we're not currently selected, just return. */ if (IFM_INST(ife->ifm_media) != sc->mii_inst) return (0); /* * Only used for autonegotiation. */ if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) return (0); /* * Is the interface even up? */ if ((mii->mii_ifp->if_flags & IFF_UP) == 0) return (0); /* * Check to see if we have link. If we do, we don't * need to restart the autonegotiation process. Read * the BMSR twice in case it's latched. */ reg = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR); if (reg & BMSR_LINK) return (0); /* * Only retry autonegotiation every 5 seconds. */ if (++sc->mii_ticks != 5) return (0); sc->mii_ticks = 0; mii_phy_reset(sc); if (mii_phy_auto(sc, 0) == EJUSTRETURN) return (0); break; } /* Update the media status. */ nsphy_status(sc); /* Callback if something changed. */ if (sc->mii_active != mii->mii_media_active || cmd == MII_MEDIACHG) { MIIBUS_STATCHG(sc->mii_dev); sc->mii_active = mii->mii_media_active; } return (0); } void nsphy_status(sc) struct mii_softc *sc; { struct mii_data *mii = sc->mii_pdata; int bmsr, bmcr, par, anlpar; mii->mii_media_status = IFM_AVALID; mii->mii_media_active = IFM_ETHER; bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR); if (bmsr & BMSR_LINK) mii->mii_media_status |= IFM_ACTIVE; bmcr = PHY_READ(sc, MII_BMCR); if (bmcr & BMCR_ISO) { mii->mii_media_active |= IFM_NONE; mii->mii_media_status = 0; return; } if (bmcr & BMCR_LOOP) mii->mii_media_active |= IFM_LOOP; if (bmcr & BMCR_AUTOEN) { /* * The PAR 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; } /* * Argh. The PAR doesn't seem to indicate duplex mode * properly! Determine media based on link partner's * advertised capabilities. */ if (PHY_READ(sc, MII_ANER) & ANER_LPAN) { anlpar = PHY_READ(sc, MII_ANAR) & PHY_READ(sc, MII_ANLPAR); if (anlpar & ANLPAR_T4) mii->mii_media_active |= IFM_100_T4; else if (anlpar & ANLPAR_TX_FD) mii->mii_media_active |= IFM_100_TX|IFM_FDX; else if (anlpar & ANLPAR_TX) mii->mii_media_active |= IFM_100_TX; else if (anlpar & ANLPAR_10_FD) mii->mii_media_active |= IFM_10_T|IFM_FDX; else if (anlpar & ANLPAR_10) mii->mii_media_active |= IFM_10_T; else mii->mii_media_active |= IFM_NONE; return; } /* * Link partner is not capable of autonegotiation. * We will never be in full-duplex mode if this is * the case, so reading the PAR is OK. */ par = PHY_READ(sc, MII_NSPHY_PAR); if (par & PAR_10) mii->mii_media_active |= IFM_10_T; else mii->mii_media_active |= IFM_100_TX; #if 0 if (par & PAR_FDX) mii->mii_media_active |= IFM_FDX; #endif } else mii->mii_media_active |= mii_media_from_bmcr(bmcr); } Index: stable/4/sys/dev/mii/pnaphy.c =================================================================== --- stable/4/sys/dev/mii/pnaphy.c (revision 69923) +++ stable/4/sys/dev/mii/pnaphy.c (revision 69924) @@ -1,282 +1,283 @@ /* * Copyright (c) 2000 Berkeley Software Design, Inc. * Copyright (c) 1997, 1998, 1999, 2000 * Bill Paul . 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. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by Bill Paul. * 4. Neither the name of the author nor the names of any co-contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY Bill Paul 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 Bill Paul OR THE VOICES IN HIS HEAD * 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. * * $FreeBSD$ */ /* * driver for homePNA PHYs * This is really just a stub that allows us to identify homePNA-based * transceicers and display the link status. MII-based homePNA PHYs * only support one media type and no autonegotiation. If we were * really clever, we could tweak some of the vendor-specific registers * to optimize the link. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "miibus_if.h" #if !defined(lint) static const char rcsid[] = "$FreeBSD$"; #endif static int pnaphy_probe __P((device_t)); static int pnaphy_attach __P((device_t)); static int pnaphy_detach __P((device_t)); static device_method_t pnaphy_methods[] = { /* device interface */ DEVMETHOD(device_probe, pnaphy_probe), DEVMETHOD(device_attach, pnaphy_attach), DEVMETHOD(device_detach, pnaphy_detach), DEVMETHOD(device_shutdown, bus_generic_shutdown), { 0, 0 } }; static devclass_t pnaphy_devclass; static driver_t pnaphy_driver = { "pnaphy", pnaphy_methods, sizeof(struct mii_softc) }; DRIVER_MODULE(pnaphy, miibus, pnaphy_driver, pnaphy_devclass, 0, 0); int pnaphy_service __P((struct mii_softc *, struct mii_data *, int)); static int pnaphy_probe(dev) device_t dev; { struct mii_attach_args *ma; ma = device_get_ivars(dev); if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_AMD && MII_MODEL(ma->mii_id2) == MII_MODEL_AMD_79c978) { device_set_desc(dev, MII_STR_AMD_79c978); return(0); } return(ENXIO); } static int pnaphy_attach(dev) device_t dev; { struct mii_softc *sc; struct mii_attach_args *ma; struct mii_data *mii; const char *sep = ""; sc = device_get_softc(dev); ma = device_get_ivars(dev); sc->mii_dev = device_get_parent(dev); mii = device_get_softc(sc->mii_dev); LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list); sc->mii_inst = mii->mii_instance; sc->mii_phy = ma->mii_phyno; sc->mii_service = pnaphy_service; sc->mii_pdata = mii; mii->mii_instance++; sc->mii_flags |= MIIF_NOISOLATE; #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL) #define PRINT(s) printf("%s%s", sep, s); sep = ", " mii_phy_reset(sc); sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & ma->mii_capmask; device_printf(dev, " "); if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0) printf("no media present"); else { ADD(IFM_MAKEWORD(IFM_ETHER, IFM_homePNA, 0, sc->mii_inst), 0); PRINT("HomePNA"); } ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst), BMCR_ISO); printf("\n"); #undef ADD #undef PRINT MIIBUS_MEDIAINIT(sc->mii_dev); return(0); } static int pnaphy_detach(dev) device_t dev; { struct mii_softc *sc; struct mii_data *mii; sc = device_get_softc(dev); mii = device_get_softc(device_get_parent(dev)); + mii_phy_auto_stop(sc); sc->mii_dev = NULL; LIST_REMOVE(sc, mii_list); return(0); } int pnaphy_service(sc, mii, cmd) struct mii_softc *sc; struct mii_data *mii; int cmd; { struct ifmedia_entry *ife = mii->mii_media.ifm_cur; int reg; 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) { reg = 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; switch (IFM_SUBTYPE(ife->ifm_media)) { case IFM_AUTO: case IFM_10_T: case IFM_100_TX: case IFM_100_T4: return (EINVAL); default: /* * BMCR data is stored in the ifmedia entry. */ PHY_WRITE(sc, MII_ANAR, mii_anar(ife->ifm_media)); PHY_WRITE(sc, MII_BMCR, ife->ifm_data); } break; case MII_TICK: /* * If we're not currently selected, just return. */ if (IFM_INST(ife->ifm_media) != sc->mii_inst) return (0); /* * Only used for autonegotiation. */ if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) return (0); /* * Is the interface even up? */ if ((mii->mii_ifp->if_flags & IFF_UP) == 0) return (0); /* * Check to see if we have link. If we do, we don't * need to restart the autonegotiation process. Read * the BMSR twice in case it's latched. */ reg = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR); if (reg & BMSR_LINK) return (0); /* * Only retry autonegotiation every 5 seconds. */ if (++sc->mii_ticks != 5) return (0); sc->mii_ticks = 0; mii_phy_reset(sc); if (mii_phy_auto(sc, 0) == EJUSTRETURN) return (0); break; } /* Update the media status. */ ukphy_status(sc); if (IFM_SUBTYPE(mii->mii_media_active) == IFM_10_T) mii->mii_media_active = IFM_ETHER|IFM_homePNA; /* Callback if something changed. */ if (sc->mii_active != mii->mii_media_active || cmd == MII_MEDIACHG) { MIIBUS_STATCHG(sc->mii_dev); sc->mii_active = mii->mii_media_active; } return (0); } Index: stable/4/sys/dev/mii/rlphy.c =================================================================== --- stable/4/sys/dev/mii/rlphy.c (revision 69923) +++ stable/4/sys/dev/mii/rlphy.c (revision 69924) @@ -1,284 +1,285 @@ /* * Copyright (c) 1997, 1998, 1999 * Bill Paul . 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. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by Bill Paul. * 4. Neither the name of the author nor the names of any co-contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY Bill Paul 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 Bill Paul OR THE VOICES IN HIS HEAD * 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. * * $FreeBSD$ */ /* * driver for RealTek 8139 internal PHYs */ #include #include #include #include #include #include #include #include #include #include #include "miibus_if.h" #if !defined(lint) static const char rcsid[] = "$FreeBSD$"; #endif static int rlphy_probe __P((device_t)); static int rlphy_attach __P((device_t)); static int rlphy_detach __P((device_t)); static device_method_t rlphy_methods[] = { /* device interface */ DEVMETHOD(device_probe, rlphy_probe), DEVMETHOD(device_attach, rlphy_attach), DEVMETHOD(device_detach, rlphy_detach), DEVMETHOD(device_shutdown, bus_generic_shutdown), { 0, 0 } }; static devclass_t rlphy_devclass; static driver_t rlphy_driver = { "rlphy", rlphy_methods, sizeof(struct mii_softc) }; DRIVER_MODULE(rlphy, miibus, rlphy_driver, rlphy_devclass, 0, 0); int rlphy_service __P((struct mii_softc *, struct mii_data *, int)); void rlphy_reset __P((struct mii_softc *)); static int rlphy_probe(dev) device_t dev; { struct mii_attach_args *ma; device_t parent; ma = device_get_ivars(dev); parent = device_get_parent(device_get_parent(dev)); /* * RealTek PHY doesn't have vendor/device ID registers: * the rl driver fakes up a return value of all zeros. */ if (MII_OUI(ma->mii_id1, ma->mii_id2) != 0 || MII_MODEL(ma->mii_id2) != 0) return (ENXIO); /* * Make sure the parent is an `rl'. */ if (strcmp(device_get_name(parent), "rl") != 0) return (ENXIO); device_set_desc(dev, "RealTek internal media interface"); return (0); } static int rlphy_attach(dev) device_t dev; { struct mii_softc *sc; struct mii_attach_args *ma; struct mii_data *mii; sc = device_get_softc(dev); ma = device_get_ivars(dev); sc->mii_dev = device_get_parent(dev); mii = device_get_softc(sc->mii_dev); /* * The RealTek PHY can never be isolated, so never allow non-zero * instances! */ if (mii->mii_instance != 0) { device_printf(dev, "ignoring this PHY, non-zero instance\n"); return(ENXIO); } LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list); sc->mii_inst = mii->mii_instance; sc->mii_phy = ma->mii_phyno; sc->mii_service = rlphy_service; sc->mii_pdata = mii; mii->mii_instance++; sc->mii_flags |= MIIF_NOISOLATE; #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL) #if 0 /* See above. */ ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst), BMCR_ISO); #endif ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->mii_inst), BMCR_LOOP|BMCR_S100); rlphy_reset(sc); sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & ma->mii_capmask; device_printf(dev, " "); if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0) printf("no media present"); else mii_add_media(mii, sc->mii_capabilities, sc->mii_inst); printf("\n"); #undef ADD MIIBUS_MEDIAINIT(sc->mii_dev); return(0); } static int rlphy_detach(dev) device_t dev; { struct mii_softc *sc; struct mii_data *mii; sc = device_get_softc(dev); mii = device_get_softc(device_get_softc(dev)); + mii_phy_auto_stop(sc); sc->mii_dev = NULL; LIST_REMOVE(sc, mii_list); return(0); } int rlphy_service(sc, mii, cmd) struct mii_softc *sc; struct mii_data *mii; int cmd; { struct ifmedia_entry *ife = mii->mii_media.ifm_cur; /* * We can't isolate the RealTek PHY, so it has to be the only one! */ if (IFM_INST(ife->ifm_media) != sc->mii_inst) panic("rlphy_service: can't isolate RealTek PHY"); switch (cmd) { case MII_POLLSTAT: break; case MII_MEDIACHG: /* * If the interface is not up, don't do anything. */ if ((mii->mii_ifp->if_flags & IFF_UP) == 0) break; switch (IFM_SUBTYPE(ife->ifm_media)) { case IFM_AUTO: /* * If we're already in auto mode, just return. */ if (PHY_READ(sc, MII_BMCR) & BMCR_AUTOEN) return (0); (void) mii_phy_auto(sc, 0); break; case IFM_100_T4: /* * XXX Not supported as a manual setting right now. */ return (EINVAL); default: /* * BMCR data is stored in the ifmedia entry. */ PHY_WRITE(sc, MII_ANAR, mii_anar(ife->ifm_media)); PHY_WRITE(sc, MII_BMCR, ife->ifm_data); } break; case MII_TICK: /* * Only used for autonegotiation. */ if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) return (0); /* * Is the interface even up? */ if ((mii->mii_ifp->if_flags & IFF_UP) == 0) return (0); /* * Only retry autonegotiation every 5 seconds. */ if (++sc->mii_ticks != 5) return (0); sc->mii_ticks = 0; /* * The RealTek PHY's autonegotiation doesn't need to be * kicked; it continues in the background. */ break; } /* Update the media status. */ ukphy_status(sc); /* Callback if something changed. */ if (sc->mii_active != mii->mii_media_active || cmd == MII_MEDIACHG) { MIIBUS_STATCHG(sc->mii_dev); sc->mii_active = mii->mii_media_active; } return (0); } void rlphy_reset(sc) struct mii_softc *sc; { mii_phy_reset(sc); /* * XXX RealTek PHY doesn't set the BMCR properly after * XXX reset, which breaks autonegotiation. */ PHY_WRITE(sc, MII_BMCR, BMCR_S100|BMCR_AUTOEN|BMCR_FDX); } Index: stable/4/sys/dev/mii/ukphy.c =================================================================== --- stable/4/sys/dev/mii/ukphy.c (revision 69923) +++ stable/4/sys/dev/mii/ukphy.c (revision 69924) @@ -1,313 +1,314 @@ /* $NetBSD: ukphy.c,v 1.2 1999/04/23 04:24:32 thorpej Exp $ */ /*- * Copyright (c) 1998, 1999 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, and by Frank van der Linden. * * 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. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the NetBSD * Foundation, Inc. and its contributors. * 4. Neither the name of The NetBSD Foundation nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * 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. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by Manuel Bouyer. * 4. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * 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 generic unknown PHYs */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include "miibus_if.h" #if !defined(lint) static const char rcsid[] = "$FreeBSD$"; #endif static int ukphy_probe __P((device_t)); static int ukphy_attach __P((device_t)); static int ukphy_detach __P((device_t)); static device_method_t ukphy_methods[] = { /* device interface */ DEVMETHOD(device_probe, ukphy_probe), DEVMETHOD(device_attach, ukphy_attach), DEVMETHOD(device_detach, ukphy_detach), DEVMETHOD(device_shutdown, bus_generic_shutdown), { 0, 0 } }; static devclass_t ukphy_devclass; static driver_t ukphy_driver = { "ukphy", ukphy_methods, sizeof(struct mii_softc) }; DRIVER_MODULE(ukphy, miibus, ukphy_driver, ukphy_devclass, 0, 0); int ukphy_service __P((struct mii_softc *, struct mii_data *, int)); static int ukphy_probe(dev) device_t dev; { /* * We know something is here, so always match at a low priority. */ device_set_desc(dev, "Generic IEEE 802.3u media interface"); return (-100); } static int ukphy_attach(dev) device_t dev; { struct mii_softc *sc; struct mii_attach_args *ma; struct mii_data *mii; sc = device_get_softc(dev); ma = device_get_ivars(dev); sc->mii_dev = device_get_parent(dev); mii = device_get_softc(sc->mii_dev); LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list); if (bootverbose) device_printf(dev, "OUI 0x%06x, model 0x%04x, rev. %d\n", MII_OUI(ma->mii_id1, ma->mii_id2), MII_MODEL(ma->mii_id2), MII_REV(ma->mii_id2)); sc->mii_inst = mii->mii_instance; sc->mii_phy = ma->mii_phyno; sc->mii_service = ukphy_service; sc->mii_pdata = mii; mii->mii_instance++; sc->mii_flags |= MIIF_NOISOLATE; #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL) ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst), BMCR_ISO); #if 0 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->mii_inst), BMCR_LOOP|BMCR_S100); #endif mii_phy_reset(sc); sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & ma->mii_capmask; device_printf(dev, " "); if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0) printf("no media present"); else mii_add_media(mii, sc->mii_capabilities, sc->mii_inst); printf("\n"); #undef ADD MIIBUS_MEDIAINIT(sc->mii_dev); return(0); } static int ukphy_detach(dev) device_t dev; { struct mii_softc *sc; struct mii_data *mii; sc = device_get_softc(dev); mii = device_get_softc(device_get_parent(dev)); + mii_phy_auto_stop(sc); sc->mii_dev = NULL; LIST_REMOVE(sc, mii_list); return(0); } int ukphy_service(sc, mii, cmd) struct mii_softc *sc; struct mii_data *mii; int cmd; { struct ifmedia_entry *ife = mii->mii_media.ifm_cur; int reg; 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) { reg = 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; switch (IFM_SUBTYPE(ife->ifm_media)) { case IFM_AUTO: /* * If we're already in auto mode, just return. */ if (PHY_READ(sc, MII_BMCR) & BMCR_AUTOEN) return (0); (void) mii_phy_auto(sc, 1); break; case IFM_100_T4: /* * XXX Not supported as a manual setting right now. */ return (EINVAL); default: /* * BMCR data is stored in the ifmedia entry. */ PHY_WRITE(sc, MII_ANAR, mii_anar(ife->ifm_media)); PHY_WRITE(sc, MII_BMCR, ife->ifm_data); } break; case MII_TICK: /* * If we're not currently selected, just return. */ if (IFM_INST(ife->ifm_media) != sc->mii_inst) return (0); /* * Only used for autonegotiation. */ if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) return (0); /* * Is the interface even up? */ if ((mii->mii_ifp->if_flags & IFF_UP) == 0) return (0); /* * Check to see if we have link. If we do, we don't * need to restart the autonegotiation process. Read * the BMSR twice in case it's latched. */ reg = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR); if (reg & BMSR_LINK) return (0); /* * Only retry autonegotiation every 5 seconds. */ if (++sc->mii_ticks != 5) return (0); sc->mii_ticks = 0; mii_phy_reset(sc); if (mii_phy_auto(sc, 0) == EJUSTRETURN) return (0); break; } /* Update the media status. */ ukphy_status(sc); /* Callback if something changed. */ if (sc->mii_active != mii->mii_media_active || cmd == MII_MEDIACHG) { MIIBUS_STATCHG(sc->mii_dev); sc->mii_active = mii->mii_media_active; } return (0); } Index: stable/4/sys/dev/mii/xmphy.c =================================================================== --- stable/4/sys/dev/mii/xmphy.c (revision 69923) +++ stable/4/sys/dev/mii/xmphy.c (revision 69924) @@ -1,392 +1,394 @@ /* * Copyright (c) 2000 * Bill Paul . 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. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by Bill Paul. * 4. Neither the name of the author nor the names of any co-contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY Bill Paul 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 Bill Paul OR THE VOICES IN HIS HEAD * 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. * * $FreeBSD$ */ /* * driver for the XaQti XMAC II's internal PHY. This is sort of * like a 10/100 PHY, except the only thing we're really autoselecting * here is full/half duplex. Speed is always 1000mbps. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include "miibus_if.h" #if !defined(lint) static const char rcsid[] = "$FreeBSD$"; #endif static int xmphy_probe __P((device_t)); static int xmphy_attach __P((device_t)); static int xmphy_detach __P((device_t)); static device_method_t xmphy_methods[] = { /* device interface */ DEVMETHOD(device_probe, xmphy_probe), DEVMETHOD(device_attach, xmphy_attach), DEVMETHOD(device_detach, xmphy_detach), DEVMETHOD(device_shutdown, bus_generic_shutdown), { 0, 0 } }; static devclass_t xmphy_devclass; static driver_t xmphy_driver = { "xmphy", xmphy_methods, sizeof(struct mii_softc) }; DRIVER_MODULE(xmphy, miibus, xmphy_driver, xmphy_devclass, 0, 0); int xmphy_service __P((struct mii_softc *, struct mii_data *, int)); void xmphy_status __P((struct mii_softc *)); static int xmphy_mii_phy_auto __P((struct mii_softc *, int)); extern void mii_phy_auto_timeout __P((void *)); static int xmphy_probe(dev) device_t dev; { struct mii_attach_args *ma; ma = device_get_ivars(dev); if (MII_OUI(ma->mii_id1, ma->mii_id2) != MII_OUI_xxXAQTI || MII_MODEL(ma->mii_id2) != MII_MODEL_XAQTI_XMACII) return(ENXIO); device_set_desc(dev, MII_STR_XAQTI_XMACII); return(0); } static int xmphy_attach(dev) device_t dev; { struct mii_softc *sc; struct mii_attach_args *ma; struct mii_data *mii; const char *sep = ""; sc = device_get_softc(dev); ma = device_get_ivars(dev); sc->mii_dev = device_get_parent(dev); mii = device_get_softc(sc->mii_dev); LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list); sc->mii_inst = mii->mii_instance; sc->mii_phy = ma->mii_phyno; sc->mii_service = xmphy_service; sc->mii_pdata = mii; sc->mii_flags |= MIIF_NOISOLATE; mii->mii_instance++; #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL) #define PRINT(s) printf("%s%s", sep, s); sep = ", " ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst), BMCR_ISO); #if 0 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->mii_inst), BMCR_LOOP|BMCR_S100); #endif mii_phy_reset(sc); device_printf(dev, " "); ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, 0, sc->mii_inst), XMPHY_BMCR_FDX); PRINT("1000baseSX"); ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, IFM_FDX, sc->mii_inst), 0); PRINT("1000baseSX-FDX"); ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst), 0); PRINT("auto"); printf("\n"); #undef ADD #undef PRINT MIIBUS_MEDIAINIT(sc->mii_dev); return(0); } static int xmphy_detach(dev) device_t dev; { struct mii_softc *sc; struct mii_data *mii; sc = device_get_softc(dev); mii = device_get_softc(device_get_parent(dev)); + if (sc->mii_flags & MIIF_DOINGAUTO) + untimeout(mii_phy_auto_timeout, sc, sc->mii_auto_ch); sc->mii_dev = NULL; LIST_REMOVE(sc, mii_list); return(0); } int xmphy_service(sc, mii, cmd) struct mii_softc *sc; struct mii_data *mii; int cmd; { struct ifmedia_entry *ife = mii->mii_media.ifm_cur; int reg; 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) { reg = 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; switch (IFM_SUBTYPE(ife->ifm_media)) { case IFM_AUTO: /* * If we're already in auto mode, just return. */ if (PHY_READ(sc, XMPHY_MII_BMCR) & XMPHY_BMCR_AUTOEN) return (0); (void) xmphy_mii_phy_auto(sc, 1); break; case IFM_1000_SX: mii_phy_reset(sc); if ((ife->ifm_media & IFM_GMASK) == IFM_FDX) { PHY_WRITE(sc, XMPHY_MII_ANAR, XMPHY_ANAR_FDX); PHY_WRITE(sc, XMPHY_MII_BMCR, XMPHY_BMCR_FDX); } else { PHY_WRITE(sc, XMPHY_MII_ANAR, XMPHY_ANAR_HDX); PHY_WRITE(sc, XMPHY_MII_BMCR, 0); } break; case IFM_100_T4: case IFM_100_TX: case IFM_10_T: default: return (EINVAL); } break; case MII_TICK: /* * If we're not currently selected, just return. */ if (IFM_INST(ife->ifm_media) != sc->mii_inst) return (0); /* * Only used for autonegotiation. */ if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) return (0); /* * Is the interface even up? */ if ((mii->mii_ifp->if_flags & IFF_UP) == 0) return (0); /* * Only retry autonegotiation every 5 seconds. */ if (++sc->mii_ticks != 5) return (0); sc->mii_ticks = 0; /* * Check to see if we have link. If we do, we don't * need to restart the autonegotiation process. Read * the BMSR twice in case it's latched. */ reg = PHY_READ(sc, XMPHY_MII_BMSR) | PHY_READ(sc, XMPHY_MII_BMSR); if (reg & XMPHY_BMSR_LINK) break; mii_phy_reset(sc); if (xmphy_mii_phy_auto(sc, 0) == EJUSTRETURN) return(0); break; } /* Update the media status. */ xmphy_status(sc); /* Callback if something changed. */ if (sc->mii_active != mii->mii_media_active || cmd == MII_MEDIACHG) { MIIBUS_STATCHG(sc->mii_dev); sc->mii_active = mii->mii_media_active; } return (0); } void xmphy_status(sc) struct mii_softc *sc; { struct mii_data *mii = sc->mii_pdata; int bmsr, bmcr, anlpar; mii->mii_media_status = IFM_AVALID; mii->mii_media_active = IFM_ETHER; bmsr = PHY_READ(sc, XMPHY_MII_BMSR) | PHY_READ(sc, XMPHY_MII_BMSR); if (bmsr & XMPHY_BMSR_LINK) mii->mii_media_status |= IFM_ACTIVE; /* Do dummy read of extended status register. */ bmcr = PHY_READ(sc, XMPHY_MII_EXTSTS); bmcr = PHY_READ(sc, XMPHY_MII_BMCR); if (bmcr & XMPHY_BMCR_LOOP) mii->mii_media_active |= IFM_LOOP; if (bmcr & XMPHY_BMCR_AUTOEN) { if ((bmsr & XMPHY_BMSR_ACOMP) == 0) { if (bmsr & XMPHY_BMSR_LINK) { mii->mii_media_active |= IFM_1000_SX|IFM_HDX; return; } /* Erg, still trying, I guess... */ mii->mii_media_active |= IFM_NONE; return; } mii->mii_media_active |= IFM_1000_SX; anlpar = PHY_READ(sc, XMPHY_MII_ANAR) & PHY_READ(sc, XMPHY_MII_ANLPAR); if (anlpar & XMPHY_ANLPAR_FDX) mii->mii_media_active |= IFM_FDX; else mii->mii_media_active |= IFM_HDX; return; } mii->mii_media_active |= IFM_1000_SX; if (bmcr & XMPHY_BMCR_FDX) mii->mii_media_active |= IFM_FDX; else mii->mii_media_active |= IFM_HDX; return; } static int xmphy_mii_phy_auto(mii, waitfor) struct mii_softc *mii; int waitfor; { int bmsr, i; if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) { PHY_WRITE(mii, XMPHY_MII_ANAR, XMPHY_ANAR_FDX|XMPHY_ANAR_HDX); PHY_WRITE(mii, XMPHY_MII_BMCR, XMPHY_BMCR_AUTOEN | XMPHY_BMCR_STARTNEG); } if (waitfor) { /* Wait 500ms for it to complete. */ for (i = 0; i < 500; i++) { if ((bmsr = PHY_READ(mii, XMPHY_MII_BMSR)) & XMPHY_BMSR_ACOMP) return (0); DELAY(1000); #if 0 if ((bmsr & BMSR_ACOMP) == 0) printf("%s: autonegotiation failed to complete\n", mii->mii_dev.dv_xname); #endif } /* * Don't need to worry about clearing MIIF_DOINGAUTO. * If that's set, a timeout is pending, and it will * clear the flag. */ return (EIO); } /* * Just let it finish asynchronously. This is for the benefit of * the tick handler driving autonegotiation. Don't want 500ms * delays all the time while the system is running! */ if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) { mii->mii_flags |= MIIF_DOINGAUTO; - timeout(mii_phy_auto_timeout, mii, hz >> 1); + mii->mii_auto_ch = timeout(mii_phy_auto_timeout, mii, hz >> 1); } return (EJUSTRETURN); }