Home | History | Annotate | Line # | Download | only in mii
mii_physubr.c revision 1.16
      1 /*	$NetBSD: mii_physubr.c,v 1.16 2000/03/15 20:34:43 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998, 1999, 2000 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      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.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     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.
     26  *
     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.
     38  */
     39 
     40 /*
     41  * Subroutines common to all PHYs.
     42  */
     43 
     44 #include <sys/param.h>
     45 #include <sys/device.h>
     46 #include <sys/systm.h>
     47 #include <sys/kernel.h>
     48 #include <sys/socket.h>
     49 #include <sys/errno.h>
     50 
     51 #include <net/if.h>
     52 #include <net/if_media.h>
     53 #include <net/route.h>
     54 
     55 #include <dev/mii/mii.h>
     56 #include <dev/mii/miivar.h>
     57 
     58 /*
     59  * Media to register setting conversion table.  Order matters.
     60  */
     61 const struct mii_media mii_media_table[] = {
     62 	{ BMCR_ISO,		ANAR_CSMA },		/* None */
     63 	{ 0,			ANAR_CSMA|ANAR_10 },	/* 10baseT */
     64 	{ BMCR_FDX,		ANAR_CSMA|ANAR_10_FD },	/* 10baseT-FDX */
     65 	{ BMCR_S100,		ANAR_CSMA|ANAR_T4 },	/* 100baseT4 */
     66 	{ BMCR_S100,		ANAR_CSMA|ANAR_TX },	/* 100baseTX */
     67 	{ BMCR_S100|BMCR_FDX,	ANAR_CSMA|ANAR_TX_FD },	/* 100baseTX-FDX */
     68 };
     69 
     70 void	mii_phy_auto_timeout __P((void *));
     71 
     72 void
     73 mii_phy_setmedia(sc)
     74 	struct mii_softc *sc;
     75 {
     76 	struct mii_data *mii = sc->mii_pdata;
     77 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
     78 	int bmcr, anar;
     79 
     80 	if (IFM_SUBTYPE(ife->ifm_media) == IFM_AUTO) {
     81 		if ((PHY_READ(sc, MII_BMCR) & BMCR_AUTOEN) == 0)
     82 			(void) mii_phy_auto(sc, 1);
     83 		return;
     84 	}
     85 
     86 	/*
     87 	 * Table index is stored in the media entry.
     88 	 */
     89 
     90 #ifdef DIAGNOSTIC
     91 	if (ife->ifm_data < 0 || ife->ifm_data >= MII_NMEDIA)
     92 		panic("mii_phy_setmedia");
     93 #endif
     94 
     95 	anar = mii_media_table[ife->ifm_data].mm_anar;
     96 	bmcr = mii_media_table[ife->ifm_data].mm_bmcr;
     97 
     98 	if (ife->ifm_media & IFM_LOOP)
     99 		bmcr |= BMCR_LOOP;
    100 
    101 	PHY_WRITE(sc, MII_ANAR, anar);
    102 	PHY_WRITE(sc, MII_BMCR, bmcr);
    103 }
    104 
    105 int
    106 mii_phy_auto(sc, waitfor)
    107 	struct mii_softc *sc;
    108 	int waitfor;
    109 {
    110 	int bmsr, i;
    111 
    112 	if ((sc->mii_flags & MIIF_DOINGAUTO) == 0) {
    113 		PHY_WRITE(sc, MII_ANAR,
    114 		    BMSR_MEDIA_TO_ANAR(sc->mii_capabilities) | ANAR_CSMA);
    115 		PHY_WRITE(sc, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG);
    116 	}
    117 
    118 	if (waitfor) {
    119 		/* Wait 500ms for it to complete. */
    120 		for (i = 0; i < 500; i++) {
    121 			if ((bmsr = PHY_READ(sc, MII_BMSR)) & BMSR_ACOMP)
    122 				return (0);
    123 			delay(1000);
    124 		}
    125 
    126 		/*
    127 		 * Don't need to worry about clearing MIIF_DOINGAUTO.
    128 		 * If that's set, a timeout is pending, and it will
    129 		 * clear the flag.
    130 		 */
    131 		return (EIO);
    132 	}
    133 
    134 	/*
    135 	 * Just let it finish asynchronously.  This is for the benefit of
    136 	 * the tick handler driving autonegotiation.  Don't want 500ms
    137 	 * delays all the time while the system is running!
    138 	 */
    139 	if ((sc->mii_flags & MIIF_DOINGAUTO) == 0) {
    140 		sc->mii_flags |= MIIF_DOINGAUTO;
    141 		timeout(mii_phy_auto_timeout, sc, hz >> 1);
    142 	}
    143 	return (EJUSTRETURN);
    144 }
    145 
    146 void
    147 mii_phy_auto_timeout(arg)
    148 	void *arg;
    149 {
    150 	struct mii_softc *sc = arg;
    151 	int s, bmsr;
    152 
    153 	if ((sc->mii_dev.dv_flags & DVF_ACTIVE) == 0)
    154 		return;
    155 
    156 	s = splnet();
    157 	sc->mii_flags &= ~MIIF_DOINGAUTO;
    158 	bmsr = PHY_READ(sc, MII_BMSR);
    159 
    160 	/* Update the media status. */
    161 	(void) (*sc->mii_service)(sc, sc->mii_pdata, MII_POLLSTAT);
    162 	splx(s);
    163 }
    164 
    165 int
    166 mii_phy_tick(sc)
    167 	struct mii_softc *sc;
    168 {
    169 	struct mii_data *mii = sc->mii_pdata;
    170 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
    171 	int reg;
    172 
    173 	/* Just bail now if the interface is down. */
    174 	if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
    175 		return (EJUSTRETURN);
    176 
    177 	/*
    178 	 * If we're not doing autonegotiation, we don't need to do
    179 	 * any extra work here.  However, we need to check the link
    180 	 * status so we can generate an announcement if the status
    181 	 * changes.
    182 	 */
    183 	if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
    184 		return (0);
    185 
    186 	/* Read the status register twice; BMSR_LINK is latch-low. */
    187 	reg = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
    188 	if (reg & BMSR_LINK) {
    189 		/*
    190 		 * See above.
    191 		 */
    192 		return (0);
    193 	}
    194 
    195 	/*
    196 	 * Only retry autonegotiation every 5 seconds.
    197 	 */
    198 	if (++sc->mii_ticks != 5)
    199 		return (EJUSTRETURN);
    200 
    201 	sc->mii_ticks = 0;
    202 	mii_phy_reset(sc);
    203 
    204 	if (mii_phy_auto(sc, 0) == EJUSTRETURN)
    205 		return (EJUSTRETURN);
    206 
    207 	/*
    208 	 * Might need to generate a status message if autonegotiation
    209 	 * failed.
    210 	 */
    211 	return (0);
    212 }
    213 
    214 void
    215 mii_phy_reset(sc)
    216 	struct mii_softc *sc;
    217 {
    218 	int reg, i;
    219 
    220 	if (sc->mii_flags & MIIF_NOISOLATE)
    221 		reg = BMCR_RESET;
    222 	else
    223 		reg = BMCR_RESET | BMCR_ISO;
    224 	PHY_WRITE(sc, MII_BMCR, reg);
    225 
    226 	/* Wait 100ms for it to complete. */
    227 	for (i = 0; i < 100; i++) {
    228 		reg = PHY_READ(sc, MII_BMCR);
    229 		if ((reg & BMCR_RESET) == 0)
    230 			break;
    231 		delay(1000);
    232 	}
    233 
    234 	if (sc->mii_inst != 0 && ((sc->mii_flags & MIIF_NOISOLATE) == 0))
    235 		PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
    236 }
    237 
    238 void
    239 mii_phy_down(sc)
    240 	struct mii_softc *sc;
    241 {
    242 
    243 	if (sc->mii_flags & MIIF_DOINGAUTO) {
    244 		sc->mii_flags &= ~MIIF_DOINGAUTO;
    245 		untimeout(mii_phy_auto_timeout, sc);
    246 	}
    247 }
    248 
    249 void
    250 mii_phy_status(sc)
    251 	struct mii_softc *sc;
    252 {
    253 
    254 	(*sc->mii_status)(sc);
    255 }
    256 
    257 void
    258 mii_phy_update(sc, cmd)
    259 	struct mii_softc *sc;
    260 	int cmd;
    261 {
    262 	struct mii_data *mii = sc->mii_pdata;
    263 
    264 	if (sc->mii_media_active != mii->mii_media_active ||
    265 	    sc->mii_media_status != mii->mii_media_status ||
    266 	    cmd == MII_MEDIACHG) {
    267 		(*mii->mii_statchg)(sc->mii_dev.dv_parent);
    268 		mii_phy_statusmsg(sc);
    269 		sc->mii_media_active = mii->mii_media_active;
    270 		sc->mii_media_status = mii->mii_media_status;
    271 	}
    272 }
    273 
    274 void
    275 mii_phy_statusmsg(sc)
    276 	struct mii_softc *sc;
    277 {
    278 	struct mii_data *mii = sc->mii_pdata;
    279 	struct ifnet *ifp = mii->mii_ifp;
    280 	int s, baudrate, link_state, announce = 0;
    281 
    282 	if (mii->mii_media_status & IFM_AVALID) {
    283 		if (mii->mii_media_status & IFM_ACTIVE)
    284 			link_state = LINK_STATE_UP;
    285 		else
    286 			link_state = LINK_STATE_DOWN;
    287 	} else
    288 		link_state = LINK_STATE_UNKNOWN;
    289 
    290 	baudrate = ifmedia_baudrate(mii->mii_media_active);
    291 
    292 	if (link_state != ifp->if_link_state) {
    293 		ifp->if_link_state = link_state;
    294 		/*
    295 		 * XXX Right here we'd like to notify protocols
    296 		 * XXX that the link status has changed, so that
    297 		 * XXX e.g. Duplicate Address Detection can restart.
    298 		 */
    299 		announce = 1;
    300 	}
    301 
    302 	if (baudrate != ifp->if_baudrate) {
    303 		ifp->if_baudrate = baudrate;
    304 		announce = 1;
    305 	}
    306 
    307 	if (announce) {
    308 		s = splimp();	/* XXX Should be splnet() */
    309 		rt_ifmsg(ifp);
    310 		splx(s);
    311 	}
    312 }
    313 
    314 /*
    315  * Initialize generic PHY media based on BMSR, called when a PHY is
    316  * attached.  We expect to be set up to print a comma-separated list
    317  * of media names.  Does not print a newline.
    318  */
    319 void
    320 mii_phy_add_media(sc)
    321 	struct mii_softc *sc;
    322 {
    323 	struct mii_data *mii = sc->mii_pdata;
    324 	const char *sep = "";
    325 
    326 #define	ADD(m, c)	ifmedia_add(&mii->mii_media, (m), (c), NULL)
    327 #define	PRINT(s)	printf("%s%s", sep, s); sep = ", "
    328 
    329 	if ((sc->mii_flags & MIIF_NOISOLATE) == 0)
    330 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
    331 		    MII_MEDIA_NONE);
    332 
    333 	if (sc->mii_capabilities & BMSR_10THDX) {
    334 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, sc->mii_inst),
    335 		    MII_MEDIA_10_T);
    336 #if 0
    337 		if ((sc->mii_flags & MIIF_NOLOOP) == 0)
    338 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_LOOP,
    339 			    sc->mii_inst), MII_MEDIA_10_T);
    340 #endif
    341 		PRINT("10baseT");
    342 	}
    343 	if (sc->mii_capabilities & BMSR_10TFDX) {
    344 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, sc->mii_inst),
    345 		    MII_MEDIA_10_T_FDX);
    346 		PRINT("10baseT-FDX");
    347 	}
    348 	if (sc->mii_capabilities & BMSR_100TXHDX) {
    349 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, sc->mii_inst),
    350 		    MII_MEDIA_100_TX);
    351 #if 0
    352 		if ((sc->mii_flags & MIIF_NOLOOP) == 0)
    353 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP,
    354 			    sc->mii_inst), MII_MEDIA_100_TX);
    355 #endif
    356 		PRINT("100baseTX");
    357 	}
    358 	if (sc->mii_capabilities & BMSR_100TXFDX) {
    359 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, sc->mii_inst),
    360 		    MII_MEDIA_100_TX_FDX);
    361 		PRINT("100baseTX-FDX");
    362 	}
    363 	if (sc->mii_capabilities & BMSR_100T4) {
    364 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_T4, 0, sc->mii_inst),
    365 		    MII_MEDIA_100_T4);
    366 #if 0
    367 		if ((sc->mii_flags & MIIF_NOLOOP) == 0)
    368 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_T4, IFM_LOOP,
    369 			    sc->mii_inst), MII_MEDIA_100_T4);
    370 #endif
    371 		PRINT("100baseT4");
    372 	}
    373 	if (sc->mii_capabilities & BMSR_ANEG) {
    374 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst),
    375 		    MII_NMEDIA);	/* intentionally invalid index */
    376 		PRINT("auto");
    377 	}
    378 #undef ADD
    379 #undef PRINT
    380 }
    381 
    382 void
    383 mii_phy_delete_media(sc)
    384 	struct mii_softc *sc;
    385 {
    386 	struct mii_data *mii = sc->mii_pdata;
    387 
    388 	ifmedia_delete_instance(&mii->mii_media, sc->mii_inst);
    389 }
    390 
    391 int
    392 mii_phy_activate(self, act)
    393 	struct device *self;
    394 	enum devact act;
    395 {
    396 	int rv = 0;
    397 
    398 	switch (act) {
    399 	case DVACT_ACTIVATE:
    400 		rv = EOPNOTSUPP;
    401 		break;
    402 
    403 	case DVACT_DEACTIVATE:
    404 		/* Nothing special to do. */
    405 		break;
    406 	}
    407 
    408 	return (rv);
    409 }
    410 
    411 int
    412 mii_phy_detach(self, flags)
    413 	struct device *self;
    414 	int flags;
    415 {
    416 	struct mii_softc *sc = (void *) self;
    417 
    418 	if (sc->mii_flags & MIIF_DOINGAUTO)
    419 		untimeout(mii_phy_auto_timeout, sc);
    420 
    421 	mii_phy_delete_media(sc);
    422 
    423 	return (0);
    424 }
    425