Home | History | Annotate | Line # | Download | only in mii
gentbi.c revision 1.4
      1 /*	$NetBSD: gentbi.c,v 1.4 2001/11/13 07:41:36 lukem Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998, 1999, 2000, 2001 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  * Copyright (c) 1997 Manuel Bouyer.  All rights reserved.
     42  *
     43  * Redistribution and use in source and binary forms, with or without
     44  * modification, are permitted provided that the following conditions
     45  * are met:
     46  * 1. Redistributions of source code must retain the above copyright
     47  *    notice, this list of conditions and the following disclaimer.
     48  * 2. Redistributions in binary form must reproduce the above copyright
     49  *    notice, this list of conditions and the following disclaimer in the
     50  *    documentation and/or other materials provided with the distribution.
     51  * 3. All advertising materials mentioning features or use of this software
     52  *    must display the following acknowledgement:
     53  *	This product includes software developed by Manuel Bouyer.
     54  * 4. The name of the author may not be used to endorse or promote products
     55  *    derived from this software without specific prior written permission.
     56  *
     57  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     58  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     59  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     60  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     61  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     62  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     63  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     64  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     65  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     66  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     67  */
     68 
     69 /*
     70  * Driver for generic ten-bit (1000BASE-SX) interfaces, built in to
     71  * many Gigabit Ethernet chips.
     72  *
     73  * All we have to do here is correctly report speed and duplex.
     74  */
     75 
     76 #include <sys/cdefs.h>
     77 __KERNEL_RCSID(0, "$NetBSD: gentbi.c,v 1.4 2001/11/13 07:41:36 lukem Exp $");
     78 
     79 #include <sys/param.h>
     80 #include <sys/systm.h>
     81 #include <sys/kernel.h>
     82 #include <sys/device.h>
     83 #include <sys/malloc.h>
     84 #include <sys/socket.h>
     85 #include <sys/errno.h>
     86 
     87 #include <net/if.h>
     88 #include <net/if_media.h>
     89 
     90 #include <dev/mii/mii.h>
     91 #include <dev/mii/miivar.h>
     92 #include <dev/mii/miidevs.h>
     93 
     94 int	gentbimatch(struct device *, struct cfdata *, void *);
     95 void	gentbiattach(struct device *, struct device *, void *);
     96 
     97 struct cfattach gentbi_ca = {
     98 	sizeof(struct mii_softc), gentbimatch, gentbiattach,
     99 	    mii_phy_detach, mii_phy_activate
    100 };
    101 
    102 int	gentbi_service(struct mii_softc *, struct mii_data *, int);
    103 void	gentbi_status(struct mii_softc *);
    104 
    105 const struct mii_phy_funcs gentbi_funcs = {
    106 	gentbi_service, gentbi_status, mii_phy_reset,
    107 };
    108 
    109 int
    110 gentbimatch(struct device *parent, struct cfdata *match, void *aux)
    111 {
    112 	struct mii_attach_args *ma = aux;
    113 	struct mii_data *mii = ma->mii_data;
    114 	int bmsr, extsr;
    115 
    116 	/*
    117 	 * We match as a generic TBI if:
    118 	 *
    119 	 *	- There is no media in the BMSR.
    120 	 *	- EXTSR has only 1000X.
    121 	 */
    122 	bmsr = (*mii->mii_readreg)(parent, ma->mii_phyno, MII_BMSR);
    123 	if ((bmsr & BMSR_EXTSTAT) == 0 || (bmsr & BMSR_MEDIAMASK) != 0)
    124 		return (0);
    125 
    126 	extsr = (*mii->mii_readreg)(parent, ma->mii_phyno, MII_EXTSR);
    127 	if (extsr & (EXTSR_1000TFDX|EXTSR_1000THDX))
    128 		return (0);
    129 
    130 	if (extsr & (EXTSR_1000XFDX|EXTSR_1000XHDX)) {
    131 		/*
    132 		 * We think this is a generic TBI.  Return a match
    133 		 * priority higher than ukphy, but lower than what
    134 		 * specific drivers will return.
    135 		 */
    136 		return (2);
    137 	}
    138 
    139 	return (0);
    140 }
    141 
    142 void
    143 gentbiattach(struct device *parent, struct device *self, void *aux)
    144 {
    145 	struct mii_softc *sc = (struct mii_softc *)self;
    146 	struct mii_attach_args *ma = aux;
    147 	struct mii_data *mii = ma->mii_data;
    148 
    149 	printf(": Generic ten-bit interface, rev. %d\n",
    150 	    MII_REV(ma->mii_id2));
    151 
    152 	sc->mii_inst = mii->mii_instance;
    153 	sc->mii_phy = ma->mii_phyno;
    154 	sc->mii_funcs = &gentbi_funcs;
    155 	sc->mii_pdata = mii;
    156 	sc->mii_flags = mii->mii_flags;
    157 	sc->mii_anegticks = 5;
    158 
    159 	PHY_RESET(sc);
    160 
    161 	/*
    162 	 * Mask out all media in the BMSR.  We only are really interested
    163 	 * in "auto".
    164 	 */
    165 	sc->mii_capabilities =
    166 	    PHY_READ(sc, MII_BMSR) & ma->mii_capmask & ~BMSR_MEDIAMASK;
    167 	if (sc->mii_capabilities & BMSR_EXTSTAT)
    168 		sc->mii_extcapabilities = PHY_READ(sc, MII_EXTSR);
    169 
    170 	printf("%s: ", sc->mii_dev.dv_xname);
    171 	if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0 &&
    172 	    (sc->mii_extcapabilities & EXTSR_MEDIAMASK) == 0)
    173 		printf("no media present");
    174 	else
    175 		mii_phy_add_media(sc);
    176 	printf("\n");
    177 }
    178 
    179 int
    180 gentbi_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
    181 {
    182 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
    183 	int reg;
    184 
    185 	switch (cmd) {
    186 	case MII_POLLSTAT:
    187 		/*
    188 		 * If we're not polling our PHY instance, just return.
    189 		 */
    190 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
    191 			return (0);
    192 		break;
    193 
    194 	case MII_MEDIACHG:
    195 		/*
    196 		 * If the media indicates a different PHY instance,
    197 		 * isolate ourselves.
    198 		 */
    199 		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
    200 			reg = PHY_READ(sc, MII_BMCR);
    201 			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
    202 			return (0);
    203 		}
    204 
    205 		/*
    206 		 * If the interface is not up, don't do anything.
    207 		 */
    208 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
    209 			break;
    210 
    211 		mii_phy_setmedia(sc);
    212 		break;
    213 
    214 	case MII_TICK:
    215 		/*
    216 		 * If we're not currently selected, just return.
    217 		 */
    218 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
    219 			return (0);
    220 
    221 		if (mii_phy_tick(sc) == EJUSTRETURN)
    222 			return (0);
    223 		break;
    224 
    225 	case MII_DOWN:
    226 		mii_phy_down(sc);
    227 		return (0);
    228 	}
    229 
    230 	/* Update the media status. */
    231 	mii_phy_status(sc);
    232 
    233 	/* Callback if something changed. */
    234 	mii_phy_update(sc, cmd);
    235 	return (0);
    236 }
    237 
    238 void
    239 gentbi_status(struct mii_softc *sc)
    240 {
    241 	struct mii_data *mii = sc->mii_pdata;
    242 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
    243 	int bmsr, bmcr, anlpar;
    244 
    245 	mii->mii_media_status = IFM_AVALID;
    246 	mii->mii_media_active = IFM_ETHER;
    247 
    248 	bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
    249 
    250 	if (bmsr & BMSR_LINK)
    251 		mii->mii_media_status |= IFM_ACTIVE;
    252 
    253 	bmcr = PHY_READ(sc, MII_BMCR);
    254 	if (bmcr & BMCR_ISO) {
    255 		mii->mii_media_active |= IFM_NONE;
    256 		mii->mii_media_status = 0;
    257 		return;
    258 	}
    259 
    260 	if (bmcr & BMCR_LOOP)
    261 		mii->mii_media_active |= IFM_LOOP;
    262 
    263 	if (bmcr & BMCR_AUTOEN) {
    264 		/*
    265 		 * The media status bits are only valid of autonegotiation
    266 		 * has completed (or it's disabled).
    267 		 */
    268 		if ((bmsr & BMSR_ACOMP) == 0) {
    269 			/* Erg, still trying, I guess... */
    270 			mii->mii_media_active |= IFM_NONE;
    271 			return;
    272 		}
    273 
    274 		/*
    275 		 * The media is always 1000baseSX.  Check the ANLPAR to
    276 		 * see if we're doing full-duplex.
    277 		 */
    278 		mii->mii_media_active |= IFM_1000_SX;
    279 
    280 		anlpar = PHY_READ(sc, MII_ANLPAR);
    281 		if ((sc->mii_extcapabilities & EXTSR_1000XFDX) != 0 &&
    282 		    (anlpar & ANLPAR_X_FD) != 0)
    283 			mii->mii_media_active |= IFM_FDX;
    284 	} else
    285 		mii->mii_media_active = ife->ifm_media;
    286 }
    287