Home | History | Annotate | Line # | Download | only in mii
mii_physubr.c revision 1.8
      1 /*	$NetBSD: mii_physubr.c,v 1.8 1999/11/12 18:13:00 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998, 1999 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 
     54 #include <dev/mii/mii.h>
     55 #include <dev/mii/miivar.h>
     56 
     57 /*
     58  * Media to register setting conversion table.  Order matters.
     59  */
     60 const struct mii_media mii_media_table[] = {
     61 	{ BMCR_ISO,		ANAR_CSMA },		/* None */
     62 	{ 0,			ANAR_CSMA|ANAR_10 },	/* 10baseT */
     63 	{ BMCR_FDX,		ANAR_CSMA|ANAR_10_FD },	/* 10baseT-FDX */
     64 	{ BMCR_S100,		ANAR_CSMA|ANAR_T4 },	/* 100baseT4 */
     65 	{ BMCR_S100,		ANAR_CSMA|ANAR_TX },	/* 100baseTX */
     66 	{ BMCR_S100|BMCR_FDX,	ANAR_CSMA|ANAR_TX_FD },	/* 100baseTX-FDX */
     67 };
     68 
     69 void	mii_phy_auto_timeout __P((void *));
     70 
     71 void
     72 mii_phy_setmedia(sc)
     73 	struct mii_softc *sc;
     74 {
     75 	struct mii_data *mii = sc->mii_pdata;
     76 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
     77 	int bmcr, anar;
     78 
     79 	/*
     80 	 * Table index is stored in the media entry.
     81 	 */
     82 
     83 #ifdef DIAGNOSTIC
     84 	if (ife->ifm_data < 0 || ife->ifm_data >= MII_NMEDIA)
     85 		panic("mii_phy_setmedia");
     86 #endif
     87 
     88 	anar = mii_media_table[ife->ifm_data].mm_anar;
     89 	bmcr = mii_media_table[ife->ifm_data].mm_bmcr;
     90 
     91 	if (ife->ifm_media & IFM_LOOP)
     92 		bmcr |= BMCR_LOOP;
     93 
     94 	PHY_WRITE(sc, MII_ANAR, anar);
     95 	PHY_WRITE(sc, MII_BMCR, bmcr);
     96 }
     97 
     98 int
     99 mii_phy_auto(sc, waitfor)
    100 	struct mii_softc *sc;
    101 	int waitfor;
    102 {
    103 	int bmsr, i;
    104 
    105 	if ((sc->mii_flags & MIIF_DOINGAUTO) == 0) {
    106 		PHY_WRITE(sc, MII_ANAR,
    107 		    BMSR_MEDIA_TO_ANAR(sc->mii_capabilities) | ANAR_CSMA);
    108 		PHY_WRITE(sc, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG);
    109 	}
    110 
    111 	if (waitfor) {
    112 		/* Wait 500ms for it to complete. */
    113 		for (i = 0; i < 500; i++) {
    114 			if ((bmsr = PHY_READ(sc, MII_BMSR)) & BMSR_ACOMP)
    115 				return (0);
    116 			delay(1000);
    117 		}
    118 
    119 		/*
    120 		 * Don't need to worry about clearing MIIF_DOINGAUTO.
    121 		 * If that's set, a timeout is pending, and it will
    122 		 * clear the flag.
    123 		 */
    124 		return (EIO);
    125 	}
    126 
    127 	/*
    128 	 * Just let it finish asynchronously.  This is for the benefit of
    129 	 * the tick handler driving autonegotiation.  Don't want 500ms
    130 	 * delays all the time while the system is running!
    131 	 */
    132 	if ((sc->mii_flags & MIIF_DOINGAUTO) == 0) {
    133 		sc->mii_flags |= MIIF_DOINGAUTO;
    134 		timeout(mii_phy_auto_timeout, sc, hz >> 1);
    135 	}
    136 	return (EJUSTRETURN);
    137 }
    138 
    139 void
    140 mii_phy_auto_timeout(arg)
    141 	void *arg;
    142 {
    143 	struct mii_softc *sc = arg;
    144 	int s, bmsr;
    145 
    146 	s = splnet();
    147 	sc->mii_flags &= ~MIIF_DOINGAUTO;
    148 	bmsr = PHY_READ(sc, MII_BMSR);
    149 
    150 	/* Update the media status. */
    151 	(void) (*sc->mii_service)(sc, sc->mii_pdata, MII_POLLSTAT);
    152 	splx(s);
    153 }
    154 
    155 void
    156 mii_phy_reset(sc)
    157 	struct mii_softc *sc;
    158 {
    159 	int reg, i;
    160 
    161 	if (sc->mii_flags & MIIF_NOISOLATE)
    162 		reg = BMCR_RESET;
    163 	else
    164 		reg = BMCR_RESET | BMCR_ISO;
    165 	PHY_WRITE(sc, MII_BMCR, reg);
    166 
    167 	/* Wait 100ms for it to complete. */
    168 	for (i = 0; i < 100; i++) {
    169 		reg = PHY_READ(sc, MII_BMCR);
    170 		if ((reg & BMCR_RESET) == 0)
    171 			break;
    172 		delay(1000);
    173 	}
    174 
    175 	if (sc->mii_inst != 0 && ((sc->mii_flags & MIIF_NOISOLATE) == 0))
    176 		PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
    177 }
    178 
    179 void
    180 mii_phy_down(sc)
    181 	struct mii_softc *sc;
    182 {
    183 
    184 	if (sc->mii_flags & MIIF_DOINGAUTO) {
    185 		sc->mii_flags &= ~MIIF_DOINGAUTO;
    186 		untimeout(mii_phy_auto_timeout, sc);
    187 	}
    188 }
    189 
    190 /*
    191  * Initialize generic PHY media based on BMSR, called when a PHY is
    192  * attached.  We expect to be set up to print a comma-separated list
    193  * of media names.  Does not print a newline.
    194  */
    195 void
    196 mii_add_media(sc)
    197 	struct mii_softc *sc;
    198 {
    199 	struct mii_data *mii = sc->mii_pdata;
    200 	const char *sep = "";
    201 
    202 #define	ADD(m, c)	ifmedia_add(&mii->mii_media, (m), (c), NULL)
    203 #define	PRINT(s)	printf("%s%s", sep, s); sep = ", "
    204 
    205 	if ((sc->mii_flags & MIIF_NOISOLATE) == 0)
    206 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
    207 		    MII_MEDIA_NONE);
    208 
    209 	if (sc->mii_capabilities & BMSR_10THDX) {
    210 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, sc->mii_inst),
    211 		    MII_MEDIA_10_T);
    212 #if 0
    213 		if ((sc->mii_flags & MIIF_NOLOOP) == 0)
    214 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_LOOP,
    215 			    sc->mii_inst), MII_MEDIA_10_T);
    216 #endif
    217 		PRINT("10baseT");
    218 	}
    219 	if (sc->mii_capabilities & BMSR_10TFDX) {
    220 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, sc->mii_inst),
    221 		    MII_MEDIA_10_T_FDX);
    222 		PRINT("10baseT-FDX");
    223 	}
    224 	if (sc->mii_capabilities & BMSR_100TXHDX) {
    225 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, sc->mii_inst),
    226 		    MII_MEDIA_100_TX);
    227 #if 0
    228 		if ((sc->mii_flags & MIIF_NOLOOP) == 0)
    229 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP,
    230 			    sc->mii_inst), MII_MEDIA_100_TX);
    231 #endif
    232 		PRINT("100baseTX");
    233 	}
    234 	if (sc->mii_capabilities & BMSR_100TXFDX) {
    235 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, sc->mii_inst),
    236 		    MII_MEDIA_100_TX_FDX);
    237 		PRINT("100baseTX-FDX");
    238 	}
    239 	if (sc->mii_capabilities & BMSR_100T4) {
    240 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_T4, 0, sc->mii_inst),
    241 		    MII_MEDIA_100_T4);
    242 #if 0
    243 		if ((sc->mii_flags & MIIF_NOLOOP) == 0)
    244 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_T4, IFM_LOOP,
    245 			    sc->mii_inst), MII_MEDIA_100_T4);
    246 #endif
    247 		PRINT("100baseT4");
    248 	}
    249 	if (sc->mii_capabilities & BMSR_ANEG) {
    250 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst),
    251 		    MII_NMEDIA);	/* intentionally invalid index */
    252 		PRINT("auto");
    253 	}
    254 #undef ADD
    255 #undef PRINT
    256 }
    257