Home | History | Annotate | Line # | Download | only in mii
nsphy.c revision 1.14
      1 /*	$NetBSD: nsphy.c,v 1.14 1998/11/04 23:44:09 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.
     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 National Semiconductor's DP83840A ethernet 10/100 PHY
     71  * Data Sheet available from www.national.com
     72  */
     73 
     74 #include <sys/param.h>
     75 #include <sys/systm.h>
     76 #include <sys/kernel.h>
     77 #include <sys/device.h>
     78 #include <sys/malloc.h>
     79 #include <sys/socket.h>
     80 
     81 #include <net/if.h>
     82 #include <net/if_media.h>
     83 
     84 #include <dev/mii/mii.h>
     85 #include <dev/mii/miivar.h>
     86 #include <dev/mii/miidevs.h>
     87 
     88 #include <dev/mii/nsphyreg.h>
     89 
     90 struct nsphy_softc {
     91 	struct mii_softc sc_mii;		/* generic PHY */
     92 	int sc_ticks;
     93 	int sc_active;
     94 };
     95 
     96 int	nsphymatch __P((struct device *, struct cfdata *, void *));
     97 void	nsphyattach __P((struct device *, struct device *, void *));
     98 
     99 struct cfattach nsphy_ca = {
    100 	sizeof(struct nsphy_softc), nsphymatch, nsphyattach
    101 };
    102 
    103 int	nsphy_service __P((struct mii_softc *, struct mii_data *, int));
    104 void	nsphy_status __P((struct nsphy_softc *));
    105 
    106 int
    107 nsphymatch(parent, match, aux)
    108 	struct device *parent;
    109 	struct cfdata *match;
    110 	void *aux;
    111 {
    112 	struct mii_attach_args *ma = aux;
    113 
    114 	if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_NATSEMI &&
    115 	    MII_MODEL(ma->mii_id2) == MII_MODEL_NATSEMI_DP83840)
    116 		return (10);
    117 
    118 	return (0);
    119 }
    120 
    121 void
    122 nsphyattach(parent, self, aux)
    123 	struct device *parent, *self;
    124 	void *aux;
    125 {
    126 	struct nsphy_softc *sc = (struct nsphy_softc *)self;
    127 	struct mii_attach_args *ma = aux;
    128 	struct mii_data *mii = ma->mii_data;
    129 
    130 	printf(": %s, rev. %d\n", MII_STR_NATSEMI_DP83840,
    131 	    MII_REV(ma->mii_id2));
    132 
    133 	sc->sc_mii.mii_inst = mii->mii_instance;
    134 	sc->sc_mii.mii_phy = ma->mii_phyno;
    135 	sc->sc_mii.mii_service = nsphy_service;
    136 	sc->sc_mii.mii_pdata = mii;
    137 
    138 	/*
    139 	 * i82557 wedges if all of its PHYs are isolated!
    140 	 */
    141 	if (strcmp(parent->dv_cfdata->cf_driver->cd_name, "fxp") == 0 &&
    142 	    mii->mii_instance == 0)
    143 		sc->sc_mii.mii_flags |= MIIF_NOISOLATE;
    144 
    145 #define	ADD(m, c)	ifmedia_add(&mii->mii_media, (m), (c), NULL)
    146 
    147 #if 0
    148 	/* Can't do this on the i82557! */
    149 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->sc_mii.mii_inst),
    150 	    BMCR_ISO);
    151 #endif
    152 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->sc_mii.mii_inst),
    153 	    BMCR_LOOP|BMCR_S100);
    154 
    155 	mii_phy_reset(&sc->sc_mii);
    156 
    157 	sc->sc_mii.mii_capabilities =
    158 	    PHY_READ(&sc->sc_mii, MII_BMSR) & ma->mii_capmask;
    159 	printf("%s: ", sc->sc_mii.mii_dev.dv_xname);
    160 	if ((sc->sc_mii.mii_capabilities & BMSR_MEDIAMASK) == 0)
    161 		printf("no media present");
    162 	else
    163 		mii_add_media(mii, sc->sc_mii.mii_capabilities,
    164 		    sc->sc_mii.mii_inst);
    165 	printf("\n");
    166 #undef ADD
    167 }
    168 
    169 int
    170 nsphy_service(self, mii, cmd)
    171 	struct mii_softc *self;
    172 	struct mii_data *mii;
    173 	int cmd;
    174 {
    175 	struct nsphy_softc *sc = (struct nsphy_softc *)self;
    176 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
    177 	int reg;
    178 
    179 	switch (cmd) {
    180 	case MII_POLLSTAT:
    181 		/*
    182 		 * If we're not polling our PHY instance, just return.
    183 		 */
    184 		if (IFM_INST(ife->ifm_media) != sc->sc_mii.mii_inst)
    185 			return (0);
    186 		break;
    187 
    188 	case MII_MEDIACHG:
    189 		/*
    190 		 * If the media indicates a different PHY instance,
    191 		 * isolate ourselves.
    192 		 */
    193 		if (IFM_INST(ife->ifm_media) != sc->sc_mii.mii_inst) {
    194 			reg = PHY_READ(&sc->sc_mii, MII_BMCR);
    195 			PHY_WRITE(&sc->sc_mii, MII_BMCR, reg | BMCR_ISO);
    196 			return (0);
    197 		}
    198 
    199 		/*
    200 		 * If the interface is not up, don't do anything.
    201 		 */
    202 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
    203 			break;
    204 
    205 		reg = PHY_READ(&sc->sc_mii, MII_NSPHY_PCR);
    206 
    207 		/*
    208 		 * Set up the PCR to use LED4 to indicate full-duplex
    209 		 * in both 10baseT and 100baseTX modes.
    210 		 */
    211 		reg |= PCR_LED4MODE;
    212 
    213 		/*
    214 		 * Make sure Carrier Intgrity Monitor function is
    215 		 * disabled (normal for Node operation, but sometimes
    216 		 * it's not set?!)
    217 		 */
    218 		reg |= PCR_CIMDIS;
    219 
    220 		/*
    221 		 * Make sure "force link good" is not set.  It's only
    222 		 * intended for debugging, but sometimes it's set
    223 		 * after a reset.
    224 		 */
    225 		reg &= ~PCR_FLINK100;
    226 
    227 #if 0
    228 		/*
    229 		 * Mystery bits which are supposedly `reserved',
    230 		 * but we seem to need to set them when the PHY
    231 		 * is connected to some interfaces!
    232 		 */
    233 		reg |= 0x0100 | 0x0400;
    234 #endif
    235 
    236 		PHY_WRITE(&sc->sc_mii, MII_NSPHY_PCR, reg);
    237 
    238 		switch (IFM_SUBTYPE(ife->ifm_media)) {
    239 		case IFM_AUTO:
    240 			/*
    241 			 * If we're already in auto mode, just return.
    242 			 */
    243 			if (PHY_READ(&sc->sc_mii, MII_BMCR) & BMCR_AUTOEN)
    244 				return (0);
    245 			(void) mii_phy_auto(&sc->sc_mii);
    246 			break;
    247 		case IFM_100_T4:
    248 			/*
    249 			 * XXX Not supported as a manual setting right now.
    250 			 */
    251 			return (EINVAL);
    252 		default:
    253 			/*
    254 			 * BMCR data is stored in the ifmedia entry.
    255 			 */
    256 			PHY_WRITE(&sc->sc_mii, MII_ANAR,
    257 			    mii_anar(ife->ifm_media));
    258 			PHY_WRITE(&sc->sc_mii, MII_BMCR, ife->ifm_data);
    259 		}
    260 		break;
    261 
    262 	case MII_TICK:
    263 		/*
    264 		 * If we're not currently selected, just return.
    265 		 */
    266 		if (IFM_INST(ife->ifm_media) != sc->sc_mii.mii_inst)
    267 			return (0);
    268 
    269 		/*
    270 		 * Only used for autonegotiation.
    271 		 */
    272 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
    273 			return (0);
    274 
    275 		/*
    276 		 * Is the interface even up?
    277 		 */
    278 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
    279 			return (0);
    280 
    281 		/*
    282 		 * Check to see if we have link.  If we do, we don't
    283 		 * need to restart the autonegotiation process.  Read
    284 		 * the BMSR twice in case it's latched.
    285 		 */
    286 		reg = PHY_READ(&sc->sc_mii, MII_BMSR) |
    287 		    PHY_READ(&sc->sc_mii, MII_BMSR);
    288 		if (reg & BMSR_LINK)
    289 			return (0);
    290 
    291 		/*
    292 		 * Only retry autonegotiation every 5 seconds.
    293 		 */
    294 		if (++sc->sc_ticks != 5)
    295 			return (0);
    296 
    297 		sc->sc_ticks = 0;
    298 		mii_phy_reset(&sc->sc_mii);
    299 		(void) mii_phy_auto(&sc->sc_mii);
    300 		break;
    301 	}
    302 
    303 	/* Update the media status. */
    304 	nsphy_status(sc);
    305 
    306 	/* Callback if something changed. */
    307 	if (sc->sc_active != mii->mii_media_active || cmd == MII_MEDIACHG) {
    308 		(*mii->mii_statchg)(sc->sc_mii.mii_dev.dv_parent);
    309 		sc->sc_active = mii->mii_media_active;
    310 	}
    311 	return (0);
    312 }
    313 
    314 void
    315 nsphy_status(sc)
    316 	struct nsphy_softc *sc;
    317 {
    318 	struct mii_data *mii = sc->sc_mii.mii_pdata;
    319 	int bmsr, bmcr, par, anlpar;
    320 
    321 	mii->mii_media_status = IFM_AVALID;
    322 	mii->mii_media_active = IFM_ETHER;
    323 
    324 	bmsr = PHY_READ(&sc->sc_mii, MII_BMSR) |
    325 	    PHY_READ(&sc->sc_mii, MII_BMSR);
    326 	if (bmsr & BMSR_LINK)
    327 		mii->mii_media_status |= IFM_ACTIVE;
    328 
    329 	bmcr = PHY_READ(&sc->sc_mii, MII_BMCR);
    330 	if (bmcr & BMCR_ISO) {
    331 		mii->mii_media_active |= IFM_NONE;
    332 		mii->mii_media_status = 0;
    333 		return;
    334 	}
    335 
    336 	if (bmcr & BMCR_LOOP)
    337 		mii->mii_media_active |= IFM_LOOP;
    338 
    339 	if (bmcr & BMCR_AUTOEN) {
    340 		/*
    341 		 * The PAR status bits are only valid of autonegotiation
    342 		 * has completed (or it's disabled).
    343 		 */
    344 		if ((bmsr & BMSR_ACOMP) == 0) {
    345 			/* Erg, still trying, I guess... */
    346 			mii->mii_media_active |= IFM_NONE;
    347 			return;
    348 		}
    349 
    350 		/*
    351 		 * Argh.  The PAR doesn't seem to indicate duplex mode
    352 		 * properly!  Determine media based on link partner's
    353 		 * advertised capabilities.
    354 		 */
    355 		if (PHY_READ(&sc->sc_mii, MII_ANER) & ANER_LPAN) {
    356 			anlpar = PHY_READ(&sc->sc_mii, MII_ANAR) &
    357 			    PHY_READ(&sc->sc_mii, MII_ANLPAR);
    358 			if (anlpar & ANLPAR_T4)
    359 				mii->mii_media_active |= IFM_100_T4;
    360 			else if (anlpar & ANLPAR_TX_FD)
    361 				mii->mii_media_active |= IFM_100_TX|IFM_FDX;
    362 			else if (anlpar & ANLPAR_TX)
    363 				mii->mii_media_active |= IFM_100_TX;
    364 			else if (anlpar & ANLPAR_10_FD)
    365 				mii->mii_media_active |= IFM_10_T|IFM_FDX;
    366 			else if (anlpar & ANLPAR_10)
    367 				mii->mii_media_active |= IFM_10_T;
    368 			else
    369 				mii->mii_media_active |= IFM_NONE;
    370 			return;
    371 		}
    372 
    373 		/*
    374 		 * Link partner is not capable of autonegotiation.
    375 		 * We will never be in full-duplex mode if this is
    376 		 * the case, so reading the PAR is OK.
    377 		 */
    378 		par = PHY_READ(&sc->sc_mii, MII_NSPHY_PAR);
    379 		if (par & PAR_10)
    380 			mii->mii_media_active |= IFM_10_T;
    381 		else
    382 			mii->mii_media_active |= IFM_100_TX;
    383 #if 0
    384 		if (par & PAR_FDX)
    385 			mii->mii_media_active |= IFM_FDX;
    386 #endif
    387 	} else {
    388 		if (bmcr & BMCR_S100)
    389 			mii->mii_media_active |= IFM_100_TX;
    390 		else
    391 			mii->mii_media_active |= IFM_10_T;
    392 		if (bmcr & BMCR_FDX)
    393 			mii->mii_media_active |= IFM_FDX;
    394 	}
    395 }
    396