1 /* $NetBSD: mii_physubr.c,v 1.5 1999/08/03 19:41:49 drochner Exp $ */
4 * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
41 * Subroutines common to all PHYs.
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/socket.h>
48 #include <sys/errno.h>
49 #include <sys/module.h>
52 #include <machine/clock.h>
55 #include <net/if_media.h>
57 #include <dev/mii/mii.h>
58 #include <dev/mii/miivar.h>
60 #include "miibus_if.h"
63 static const char rcsid[] =
64 "$FreeBSD: src/sys/dev/mii/mii_physubr.c,v 1.2.2.1 2000/12/12 19:29:14 wpaul Exp $";
67 void mii_phy_auto_timeout __P((void *));
70 mii_phy_auto(mii, waitfor)
71 struct mii_softc *mii;
76 if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) {
77 PHY_WRITE(mii, MII_ANAR,
78 BMSR_MEDIA_TO_ANAR(mii->mii_capabilities) | ANAR_CSMA);
79 PHY_WRITE(mii, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG);
83 /* Wait 500ms for it to complete. */
84 for (i = 0; i < 500; i++) {
85 if ((bmsr = PHY_READ(mii, MII_BMSR)) & BMSR_ACOMP)
89 if ((bmsr & BMSR_ACOMP) == 0)
90 printf("%s: autonegotiation failed to complete\n",
91 mii->mii_dev.dv_xname);
96 * Don't need to worry about clearing MIIF_DOINGAUTO.
97 * If that's set, a timeout is pending, and it will
104 * Just let it finish asynchronously. This is for the benefit of
105 * the tick handler driving autonegotiation. Don't want 500ms
106 * delays all the time while the system is running!
108 if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) {
109 mii->mii_flags |= MIIF_DOINGAUTO;
110 mii->mii_auto_ch = timeout(mii_phy_auto_timeout, mii, hz >> 1);
112 return (EJUSTRETURN);
116 mii_phy_auto_stop(sc)
117 struct mii_softc *sc;
119 if (sc->mii_flags & MIIF_DOINGAUTO) {
120 sc->mii_flags &= ~MIIF_DOINGAUTO;
121 untimeout(mii_phy_auto_timeout, sc, sc->mii_auto_ch);
126 mii_phy_auto_timeout(arg)
129 struct mii_softc *mii = arg;
133 mii->mii_flags &= ~MIIF_DOINGAUTO;
134 bmsr = PHY_READ(mii, MII_BMSR);
136 if ((bmsr & BMSR_ACOMP) == 0)
137 printf("%s: autonegotiation failed to complete\n",
138 sc->sc_dev.dv_xname);
141 /* Update the media status. */
142 (void) (*mii->mii_service)(mii, mii->mii_pdata, MII_POLLSTAT);
148 struct mii_softc *mii;
152 if (mii->mii_flags & MIIF_NOISOLATE)
155 reg = BMCR_RESET | BMCR_ISO;
156 PHY_WRITE(mii, MII_BMCR, reg);
158 /* Wait 100ms for it to complete. */
159 for (i = 0; i < 100; i++) {
160 reg = PHY_READ(mii, MII_BMCR);
161 if ((reg & BMCR_RESET) == 0)
166 if (mii->mii_inst != 0 && ((mii->mii_flags & MIIF_NOISOLATE) == 0))
167 PHY_WRITE(mii, MII_BMCR, reg | BMCR_ISO);
171 * Given an ifmedia word, return the corresponding ANAR value.
179 switch (media & (IFM_TMASK|IFM_NMASK|IFM_FDX)) {
180 case IFM_ETHER|IFM_10_T:
181 rv = ANAR_10|ANAR_CSMA;
183 case IFM_ETHER|IFM_10_T|IFM_FDX:
184 rv = ANAR_10_FD|ANAR_CSMA;
186 case IFM_ETHER|IFM_100_TX:
187 rv = ANAR_TX|ANAR_CSMA;
189 case IFM_ETHER|IFM_100_TX|IFM_FDX:
190 rv = ANAR_TX_FD|ANAR_CSMA;
192 case IFM_ETHER|IFM_100_T4:
193 rv = ANAR_T4|ANAR_CSMA;
204 * Given a BMCR value, return the corresponding ifmedia word.
207 mii_media_from_bmcr(bmcr)
212 if (bmcr & BMCR_S100)
223 * Initialize generic PHY media based on BMSR, called when a PHY is
224 * attached. We expect to be set up to print a comma-separated list
225 * of media names. Does not print a newline.
228 mii_add_media(mii, bmsr, instance)
229 struct mii_data *mii;
232 const char *sep = "";
234 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL)
235 #define PRINT(s) printf("%s%s", sep, s); sep = ", "
237 if (bmsr & BMSR_10THDX) {
238 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, instance), 0);
241 if (bmsr & BMSR_10TFDX) {
242 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, instance),
244 PRINT("10baseT-FDX");
246 if (bmsr & BMSR_100TXHDX) {
247 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, instance),
251 if (bmsr & BMSR_100TXFDX) {
252 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, instance),
254 PRINT("100baseTX-FDX");
256 if (bmsr & BMSR_100T4) {
258 * XXX How do you enable 100baseT4? I assume we set
259 * XXX BMCR_S100 and then assume the PHYs will take
260 * XXX watever action is necessary to switch themselves
263 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_T4, 0, instance),
267 if (bmsr & BMSR_ANEG) {
268 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, instance),