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>
54 #include <net/if_media.h>
56 #include <dev/mii/mii.h>
57 #include <dev/mii/miivar.h>
59 #include "miibus_if.h"
62 static const char rcsid[] =
66 void mii_phy_auto_timeout(void *);
69 mii_phy_auto(mii, waitfor)
70 struct mii_softc *mii;
75 if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) {
76 PHY_WRITE(mii, MII_ANAR,
77 BMSR_MEDIA_TO_ANAR(mii->mii_capabilities) | ANAR_CSMA);
78 PHY_WRITE(mii, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG);
82 /* Wait 500ms for it to complete. */
83 for (i = 0; i < 500; i++) {
84 if ((bmsr = PHY_READ(mii, MII_BMSR)) & BMSR_ACOMP)
88 if ((bmsr & BMSR_ACOMP) == 0)
89 printf("%s: autonegotiation failed to complete\n",
90 mii->mii_dev.dv_xname);
95 * Don't need to worry about clearing MIIF_DOINGAUTO.
96 * If that's set, a timeout is pending, and it will
103 * Just let it finish asynchronously. This is for the benefit of
104 * the tick handler driving autonegotiation. Don't want 500ms
105 * delays all the time while the system is running!
107 if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) {
108 mii->mii_flags |= MIIF_DOINGAUTO;
109 mii->mii_auto_ch = timeout(mii_phy_auto_timeout, mii, hz >> 1);
111 return (EJUSTRETURN);
115 mii_phy_auto_stop(sc)
116 struct mii_softc *sc;
118 if (sc->mii_flags & MIIF_DOINGAUTO) {
119 sc->mii_flags &= ~MIIF_DOINGAUTO;
120 untimeout(mii_phy_auto_timeout, sc, sc->mii_auto_ch);
125 mii_phy_auto_timeout(arg)
128 struct mii_softc *mii = arg;
132 mii->mii_flags &= ~MIIF_DOINGAUTO;
133 bmsr = PHY_READ(mii, MII_BMSR);
135 if ((bmsr & BMSR_ACOMP) == 0)
136 printf("%s: autonegotiation failed to complete\n",
137 sc->sc_dev.dv_xname);
140 /* Update the media status. */
141 (void) (*mii->mii_service)(mii, mii->mii_pdata, MII_POLLSTAT);
147 struct mii_softc *sc;
149 struct ifmedia_entry *ife = sc->mii_pdata->mii_media.ifm_cur;
150 struct ifnet *ifp = sc->mii_pdata->mii_ifp;
154 * Is the interface even up?
156 if ((ifp->if_flags & IFF_UP) == 0)
157 return (EJUSTRETURN);
160 * If we're not doing autonegotiation, we don't need to do
161 * any extra work here. However, we need to check the link
162 * status so we can generate an announcement if the status
165 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
170 * Read the status register twice; BMSR_LINK is latch-low.
172 reg = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
177 * Only retry autonegotiation every 5 seconds.
179 if (++sc->mii_ticks != 5)
180 return (EJUSTRETURN);
184 if (mii_phy_auto(sc, 0) == EJUSTRETURN)
185 return (EJUSTRETURN);
188 * Might need to generate a status message if autonegotiation
196 struct mii_softc *mii;
200 if (mii->mii_flags & MIIF_NOISOLATE)
203 reg = BMCR_RESET | BMCR_ISO;
204 PHY_WRITE(mii, MII_BMCR, reg);
206 /* Wait 100ms for it to complete. */
207 for (i = 0; i < 100; i++) {
208 reg = PHY_READ(mii, MII_BMCR);
209 if ((reg & BMCR_RESET) == 0)
214 if (mii->mii_inst != 0 && ((mii->mii_flags & MIIF_NOISOLATE) == 0))
215 PHY_WRITE(mii, MII_BMCR, reg | BMCR_ISO);
219 mii_phy_update(sc, cmd)
220 struct mii_softc *sc;
223 struct mii_data *mii = sc->mii_pdata;
225 if (sc->mii_media_active != mii->mii_media_active || cmd == MII_MEDIACHG) {
226 MIIBUS_STATCHG(sc->mii_dev);
227 sc->mii_media_active = mii->mii_media_active;
229 if (sc->mii_media_status != mii->mii_media_status) {
230 MIIBUS_LINKCHG(sc->mii_dev);
231 sc->mii_media_status = mii->mii_media_status;
236 * Given an ifmedia word, return the corresponding ANAR value.
244 switch (media & (IFM_TMASK|IFM_NMASK|IFM_FDX)) {
245 case IFM_ETHER|IFM_10_T:
246 rv = ANAR_10|ANAR_CSMA;
248 case IFM_ETHER|IFM_10_T|IFM_FDX:
249 rv = ANAR_10_FD|ANAR_CSMA;
251 case IFM_ETHER|IFM_100_TX:
252 rv = ANAR_TX|ANAR_CSMA;
254 case IFM_ETHER|IFM_100_TX|IFM_FDX:
255 rv = ANAR_TX_FD|ANAR_CSMA;
257 case IFM_ETHER|IFM_100_T4:
258 rv = ANAR_T4|ANAR_CSMA;
269 * Given a BMCR value, return the corresponding ifmedia word.
272 mii_media_from_bmcr(bmcr)
277 if (bmcr & BMCR_S100)
288 * Initialize generic PHY media based on BMSR, called when a PHY is
289 * attached. We expect to be set up to print a comma-separated list
290 * of media names. Does not print a newline.
293 mii_add_media(struct mii_softc *sc)
295 const char *sep = "";
297 struct mii_data *mii;
299 bmsr = sc->mii_capabilities;
300 instance = sc->mii_inst;
301 mii = device_get_softc(sc->mii_dev);
302 if ((bmsr & BMSR_MEDIAMASK) == 0) {
303 printf("no media present");
307 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL)
308 #define PRINT(s) printf("%s%s", sep, s); sep = ", "
310 if (bmsr & BMSR_10THDX) {
311 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, instance), 0);
314 if (bmsr & BMSR_10TFDX) {
315 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, instance),
317 PRINT("10baseT-FDX");
319 if (bmsr & BMSR_100TXHDX) {
320 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, instance),
324 if (bmsr & BMSR_100TXFDX) {
325 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, instance),
327 PRINT("100baseTX-FDX");
329 if (bmsr & BMSR_100T4) {
331 * XXX How do you enable 100baseT4? I assume we set
332 * XXX BMCR_S100 and then assume the PHYs will take
333 * XXX watever action is necessary to switch themselves
336 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_T4, 0, instance),
340 if (bmsr & BMSR_ANEG) {
341 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, instance),