Home | History | Annotate | Line # | Download | only in mii
exphy.c revision 1.7
      1 /*	$NetBSD: exphy.c,v 1.7 1998/10/23 01:52:50 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998 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, and by Frank van der Linden.
     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 3Com internal PHYs
     71  */
     72 
     73 #include <sys/param.h>
     74 #include <sys/systm.h>
     75 #include <sys/kernel.h>
     76 #include <sys/device.h>
     77 #include <sys/malloc.h>
     78 #include <sys/socket.h>
     79 
     80 #include <net/if.h>
     81 #include <net/if_media.h>
     82 
     83 #include <dev/mii/mii.h>
     84 #include <dev/mii/miivar.h>
     85 #include <dev/mii/miidevs.h>
     86 
     87 struct exphy_softc {
     88 	struct mii_softc sc_mii;		/* generic PHY */
     89 	int sc_capabilities;
     90 	int sc_active;
     91 };
     92 
     93 int	exphymatch __P((struct device *, struct cfdata *, void *));
     94 void	exphyattach __P((struct device *, struct device *, void *));
     95 
     96 struct cfattach exphy_ca = {
     97 	sizeof(struct exphy_softc), exphymatch, exphyattach
     98 };
     99 
    100 #define	EXPHY_READ(sc, reg) \
    101     (*(sc)->sc_mii.mii_pdata->mii_readreg)((sc)->sc_mii.mii_dev.dv_parent, \
    102 	(sc)->sc_mii.mii_phy, (reg))
    103 
    104 #define	EXPHY_WRITE(sc, reg, val) \
    105     (*(sc)->sc_mii.mii_pdata->mii_writereg)((sc)->sc_mii.mii_dev.dv_parent, \
    106 	(sc)->sc_mii.mii_phy, (reg), (val))
    107 
    108 int	exphy_service __P((struct mii_softc *, struct mii_data *, int));
    109 void	exphy_reset __P((struct exphy_softc *));
    110 void	exphy_auto __P((struct exphy_softc *));
    111 void	exphy_status __P((struct exphy_softc *));
    112 
    113 int
    114 exphymatch(parent, match, aux)
    115 	struct device *parent;
    116 	struct cfdata *match;
    117 	void *aux;
    118 {
    119 	struct mii_attach_args *ma = aux;
    120 
    121 	/*
    122 	 * Argh, 3Com PHY reports oui == 0 model == 0!
    123 	 */
    124 	if (MII_OUI(ma->mii_id1, ma->mii_id2) != 0 &&
    125 	    MII_MODEL(ma->mii_id2) != 0)
    126 		return (0);
    127 
    128 	/*
    129 	 * Make sure the parent is an `ex'.
    130 	 */
    131 	if (strcmp(parent->dv_cfdata->cf_driver->cd_name, "ex") != 0)
    132 		return (0);
    133 
    134 	return (1);
    135 }
    136 
    137 void
    138 exphyattach(parent, self, aux)
    139 	struct device *parent, *self;
    140 	void *aux;
    141 {
    142 	struct exphy_softc *sc = (struct exphy_softc *)self;
    143 	struct mii_attach_args *ma = aux;
    144 	struct mii_data *mii = ma->mii_data;
    145 
    146 	printf(": 3Com internal media interface\n");
    147 
    148 	sc->sc_mii.mii_inst = mii->mii_instance;
    149 	sc->sc_mii.mii_phy = ma->mii_phyno;
    150 	sc->sc_mii.mii_service = exphy_service;
    151 	sc->sc_mii.mii_pdata = mii;
    152 
    153 	/*
    154 	 * The 3Com PHY can never be isolated, so never allow non-zero
    155 	 * instances!
    156 	 */
    157 	if (mii->mii_instance != 0) {
    158 		printf("%s: ignoring this PHY, non-zero instance\n",
    159 		    sc->sc_mii.mii_dev.dv_xname);
    160 		return;
    161 	}
    162 
    163 #define	ADD(m, c)	ifmedia_add(&mii->mii_media, (m), (c), NULL)
    164 
    165 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->sc_mii.mii_inst),
    166 	    BMCR_ISO);
    167 
    168 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->sc_mii.mii_inst),
    169 	    BMCR_LOOP|BMCR_S100);
    170 
    171 	exphy_reset(sc);
    172 
    173 	sc->sc_capabilities = EXPHY_READ(sc, MII_BMSR) & ma->mii_capmask;
    174 	printf("%s: ", sc->sc_mii.mii_dev.dv_xname);
    175 	if ((sc->sc_capabilities & BMSR_MEDIAMASK) == 0)
    176 		printf("no media present");
    177 	else
    178 		mii_add_media(mii, sc->sc_capabilities, sc->sc_mii.mii_inst);
    179 	printf("\n");
    180 #undef ADD
    181 }
    182 
    183 int
    184 exphy_service(self, mii, cmd)
    185 	struct mii_softc *self;
    186 	struct mii_data *mii;
    187 	int cmd;
    188 {
    189 	struct exphy_softc *sc = (struct exphy_softc *)self;
    190 
    191 	/*
    192 	 * We can't isolate the 3Com PHY, so it has to be the only one!
    193 	 */
    194 	if (IFM_INST(mii->mii_media.ifm_media) != sc->sc_mii.mii_inst)
    195 		panic("exphy_service: can't isolate 3Com PHY");
    196 
    197 
    198 	switch (cmd) {
    199 	case MII_POLLSTAT:
    200 		break;
    201 
    202 	case MII_MEDIACHG:
    203 		/*
    204 		 * If the interface is not up, don't do anything.
    205 		 */
    206 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
    207 			break;
    208 
    209 		switch (IFM_SUBTYPE(mii->mii_media.ifm_media)) {
    210 		case IFM_AUTO:
    211 			/*
    212 			 * If we're already in auto mode, just return.
    213 			 */
    214 			if (EXPHY_READ(sc, MII_BMCR) & BMCR_AUTOEN)
    215 				return (0);
    216 			exphy_auto(sc);
    217 			break;
    218 		case IFM_100_T4:
    219 			/*
    220 			 * XXX Not supported as a manual setting right now.
    221 			 */
    222 			return (EINVAL);
    223 		default:
    224 			/*
    225 			 * BMCR data is stored in the ifmedia entry.
    226 			 */
    227 			EXPHY_WRITE(sc, MII_ANAR,
    228 			    mii_anar(mii->mii_media.ifm_media));
    229 			EXPHY_WRITE(sc, MII_BMCR,
    230 			    mii->mii_media.ifm_cur->ifm_data);
    231 		}
    232 		break;
    233 
    234 	case MII_TICK:
    235 		/*
    236 		 * Only used for autonegotiation.
    237 		 */
    238 		if (IFM_SUBTYPE(mii->mii_media.ifm_media) != IFM_AUTO)
    239 			return (0);
    240 
    241 		/*
    242 		 * Is the interface even up?
    243 		 */
    244 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
    245 			return (0);
    246 
    247 		/*
    248 		 * The 3Com PHY's autonegotiation doesn't need to be
    249 		 * kicked; it continues in the background.
    250 		 */
    251 		break;
    252 	}
    253 
    254 	/* Update the media status. */
    255 	exphy_status(sc);
    256 
    257 	/* Callback if something changed. */
    258 	if (sc->sc_active != mii->mii_media_active || cmd == MII_MEDIACHG) {
    259 		(*mii->mii_statchg)(sc->sc_mii.mii_dev.dv_parent);
    260 		sc->sc_active = mii->mii_media_active;
    261 	}
    262 	return (0);
    263 }
    264 
    265 void
    266 exphy_status(sc)
    267 	struct exphy_softc *sc;
    268 {
    269 	struct mii_data *mii = sc->sc_mii.mii_pdata;
    270 	int bmsr, bmcr, anlpar;
    271 
    272 	mii->mii_media_status = IFM_AVALID;
    273 	mii->mii_media_active = IFM_ETHER;
    274 
    275 	bmsr = EXPHY_READ(sc, MII_BMSR) | EXPHY_READ(sc, MII_BMSR);
    276 	if (bmsr & BMSR_LINK)
    277 		mii->mii_media_status |= IFM_ACTIVE;
    278 
    279 	bmcr = EXPHY_READ(sc, MII_BMCR);
    280 	if (bmcr & BMCR_ISO) {
    281 		mii->mii_media_active |= IFM_NONE;
    282 		mii->mii_media_status = 0;
    283 		return;
    284 	}
    285 
    286 	if (bmcr & BMCR_LOOP)
    287 		mii->mii_media_active |= IFM_LOOP;
    288 
    289 	if (bmcr & BMCR_AUTOEN) {
    290 		/*
    291 		 * The 3Com PHY uses the highest-order common bit of
    292 		 * the ANAR and ANLPAR (i.e. best media advertised
    293 		 * both by us and our link partner).
    294 		 */
    295 		if ((bmsr & BMSR_ACOMP) == 0) {
    296 			/* Erg, still trying, I guess... */
    297 			mii->mii_media_active |= IFM_NONE;
    298 			return;
    299 		}
    300 
    301 		anlpar = EXPHY_READ(sc, MII_ANAR) & EXPHY_READ(sc, MII_ANLPAR);
    302 		if (anlpar & ANLPAR_T4)
    303 			mii->mii_media_active |= IFM_100_T4;
    304 		else if (anlpar & ANLPAR_TX_FD)
    305 			mii->mii_media_active |= IFM_100_TX|IFM_FDX;
    306 		else if (anlpar & ANLPAR_TX)
    307 			mii->mii_media_active |= IFM_100_TX;
    308 		else if (anlpar & ANLPAR_10_FD)
    309 			mii->mii_media_active |= IFM_10_T|IFM_FDX;
    310 		else if (anlpar & ANLPAR_10)
    311 			mii->mii_media_active |= IFM_10_T;
    312 		else
    313 			mii->mii_media_active |= IFM_NONE;
    314 	} else {
    315 		if (bmcr & BMCR_S100)
    316 			mii->mii_media_active |= IFM_100_TX;
    317 		else
    318 			mii->mii_media_active |= IFM_10_T;
    319 		if (bmcr & BMCR_FDX)
    320 			mii->mii_media_active |= IFM_FDX;
    321 	}
    322 }
    323 
    324 void
    325 exphy_auto(sc)
    326 	struct exphy_softc *sc;
    327 {
    328 	int bmsr, i;
    329 
    330 	EXPHY_WRITE(sc, MII_ANAR,
    331 	    BMSR_MEDIA_TO_ANAR(sc->sc_capabilities) | ANAR_CSMA);
    332 	EXPHY_WRITE(sc, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG);
    333 
    334 	/* Wait 500ms for it to complete. */
    335 	for (i = 0; i < 500; i++) {
    336 		if ((bmsr = EXPHY_READ(sc, MII_BMSR)) & BMSR_ACOMP)
    337 			return;
    338 		delay(1000);
    339 	}
    340 #if 0
    341 	if ((bmsr & BMSR_ACOMP) == 0)
    342 		printf("%s: autonegotiation failed to complete\n",
    343 		    sc->sc_mii.mii_dev.dv_xname);
    344 #endif
    345 }
    346 
    347 void
    348 exphy_reset(sc)
    349 	struct exphy_softc *sc;
    350 {
    351 	int reg, i;
    352 
    353 	EXPHY_WRITE(sc, MII_BMCR, BMCR_RESET);
    354 
    355 	/* Wait 100ms for it to complete. */
    356 	for (i = 0; i < 100; i++) {
    357 		reg = EXPHY_READ(sc, MII_BMCR);
    358 		if ((reg & BMCR_RESET) == 0)
    359 			break;
    360 		delay(1000);
    361 	}
    362 
    363 	/*
    364 	 * XXX 3Com PHY doesn't set the BMCR properly after
    365 	 * XXX reset, which breaks autonegotiation.
    366 	 */
    367 	EXPHY_WRITE(sc, MII_BMCR, BMCR_S100|BMCR_AUTOEN|BMCR_FDX);
    368 }
    369