Home | History | Annotate | Line # | Download | only in mii
nsphy.c revision 1.9
      1  1.9  thorpej /*	$NetBSD: nsphy.c,v 1.9 1998/08/14 00:23:26 thorpej Exp $	*/
      2  1.5  thorpej 
      3  1.5  thorpej /*-
      4  1.5  thorpej  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  1.5  thorpej  * All rights reserved.
      6  1.5  thorpej  *
      7  1.5  thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8  1.5  thorpej  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  1.5  thorpej  * NASA Ames Research Center.
     10  1.5  thorpej  *
     11  1.5  thorpej  * Redistribution and use in source and binary forms, with or without
     12  1.5  thorpej  * modification, are permitted provided that the following conditions
     13  1.5  thorpej  * are met:
     14  1.5  thorpej  * 1. Redistributions of source code must retain the above copyright
     15  1.5  thorpej  *    notice, this list of conditions and the following disclaimer.
     16  1.5  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.5  thorpej  *    notice, this list of conditions and the following disclaimer in the
     18  1.5  thorpej  *    documentation and/or other materials provided with the distribution.
     19  1.5  thorpej  * 3. All advertising materials mentioning features or use of this software
     20  1.5  thorpej  *    must display the following acknowledgement:
     21  1.5  thorpej  *	This product includes software developed by the NetBSD
     22  1.5  thorpej  *	Foundation, Inc. and its contributors.
     23  1.5  thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  1.5  thorpej  *    contributors may be used to endorse or promote products derived
     25  1.5  thorpej  *    from this software without specific prior written permission.
     26  1.5  thorpej  *
     27  1.5  thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  1.5  thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  1.5  thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  1.5  thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  1.5  thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  1.5  thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  1.5  thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  1.5  thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  1.5  thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  1.5  thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  1.5  thorpej  * POSSIBILITY OF SUCH DAMAGE.
     38  1.5  thorpej  */
     39  1.5  thorpej 
     40  1.1   bouyer /*
     41  1.1   bouyer  * Copyright (c) 1997 Manuel Bouyer.  All rights reserved.
     42  1.1   bouyer  *
     43  1.1   bouyer  * Redistribution and use in source and binary forms, with or without
     44  1.1   bouyer  * modification, are permitted provided that the following conditions
     45  1.1   bouyer  * are met:
     46  1.1   bouyer  * 1. Redistributions of source code must retain the above copyright
     47  1.1   bouyer  *    notice, this list of conditions and the following disclaimer.
     48  1.1   bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     49  1.1   bouyer  *    notice, this list of conditions and the following disclaimer in the
     50  1.1   bouyer  *    documentation and/or other materials provided with the distribution.
     51  1.1   bouyer  * 3. All advertising materials mentioning features or use of this software
     52  1.1   bouyer  *    must display the following acknowledgement:
     53  1.2  thorpej  *	This product includes software developed by Manuel Bouyer.
     54  1.1   bouyer  * 4. The name of the author may not be used to endorse or promote products
     55  1.1   bouyer  *    derived from this software without specific prior written permission.
     56  1.1   bouyer  *
     57  1.1   bouyer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     58  1.1   bouyer  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     59  1.1   bouyer  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     60  1.1   bouyer  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     61  1.1   bouyer  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     62  1.1   bouyer  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     63  1.1   bouyer  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     64  1.1   bouyer  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     65  1.1   bouyer  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     66  1.1   bouyer  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     67  1.1   bouyer  */
     68  1.1   bouyer 
     69  1.5  thorpej /*
     70  1.5  thorpej  * driver for National Semiconductor's DP83840A ethernet 10/100 PHY
     71  1.5  thorpej  * Data Sheet available from www.national.com
     72  1.5  thorpej  */
     73  1.1   bouyer 
     74  1.1   bouyer #include <sys/param.h>
     75  1.1   bouyer #include <sys/systm.h>
     76  1.1   bouyer #include <sys/kernel.h>
     77  1.1   bouyer #include <sys/device.h>
     78  1.1   bouyer #include <sys/malloc.h>
     79  1.1   bouyer #include <sys/socket.h>
     80  1.5  thorpej 
     81  1.1   bouyer #include <net/if.h>
     82  1.1   bouyer #include <net/if_media.h>
     83  1.1   bouyer 
     84  1.5  thorpej #include <dev/mii/mii.h>
     85  1.5  thorpej #include <dev/mii/miivar.h>
     86  1.5  thorpej #include <dev/mii/miidevs.h>
     87  1.5  thorpej 
     88  1.5  thorpej #include <dev/mii/nsphyreg.h>
     89  1.5  thorpej 
     90  1.5  thorpej struct nsphy_softc {
     91  1.5  thorpej 	struct mii_softc sc_mii;		/* generic PHY */
     92  1.5  thorpej 	int sc_capabilities;
     93  1.5  thorpej 	int sc_ticks;
     94  1.5  thorpej 	int sc_active;
     95  1.5  thorpej };
     96  1.1   bouyer 
     97  1.2  thorpej int	nsphymatch __P((struct device *, struct cfdata *, void *));
     98  1.2  thorpej void	nsphyattach __P((struct device *, struct device *, void *));
     99  1.1   bouyer 
    100  1.1   bouyer struct cfattach nsphy_ca = {
    101  1.5  thorpej 	sizeof(struct nsphy_softc), nsphymatch, nsphyattach
    102  1.1   bouyer };
    103  1.1   bouyer 
    104  1.5  thorpej #define	NSPHY_READ(sc, reg) \
    105  1.5  thorpej     (*(sc)->sc_mii.mii_pdata->mii_readreg)((sc)->sc_mii.mii_dev.dv_parent, \
    106  1.5  thorpej 	(sc)->sc_mii.mii_phy, (reg))
    107  1.5  thorpej 
    108  1.5  thorpej #define	NSPHY_WRITE(sc, reg, val) \
    109  1.5  thorpej     (*(sc)->sc_mii.mii_pdata->mii_writereg)((sc)->sc_mii.mii_dev.dv_parent, \
    110  1.5  thorpej 	(sc)->sc_mii.mii_phy, (reg), (val))
    111  1.5  thorpej 
    112  1.5  thorpej int	nsphy_service __P((struct mii_softc *, struct mii_data *, int));
    113  1.5  thorpej void	nsphy_reset __P((struct nsphy_softc *));
    114  1.5  thorpej void	nsphy_auto __P((struct nsphy_softc *));
    115  1.5  thorpej void	nsphy_status __P((struct nsphy_softc *));
    116  1.5  thorpej 
    117  1.1   bouyer int
    118  1.1   bouyer nsphymatch(parent, match, aux)
    119  1.2  thorpej 	struct device *parent;
    120  1.1   bouyer 	struct cfdata *match;
    121  1.1   bouyer 	void *aux;
    122  1.1   bouyer {
    123  1.5  thorpej 	struct mii_attach_args *ma = aux;
    124  1.5  thorpej 
    125  1.5  thorpej 	if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_NATSEMI &&
    126  1.5  thorpej 	    MII_MODEL(ma->mii_id2) == MII_MODEL_NATSEMI_DP83840)
    127  1.5  thorpej 		return (1);
    128  1.1   bouyer 
    129  1.5  thorpej 	return (0);
    130  1.1   bouyer }
    131  1.1   bouyer 
    132  1.1   bouyer void
    133  1.1   bouyer nsphyattach(parent, self, aux)
    134  1.2  thorpej 	struct device *parent, *self;
    135  1.1   bouyer 	void *aux;
    136  1.1   bouyer {
    137  1.5  thorpej 	struct nsphy_softc *sc = (struct nsphy_softc *)self;
    138  1.5  thorpej 	struct mii_attach_args *ma = aux;
    139  1.5  thorpej 	struct mii_data *mii = ma->mii_data;
    140  1.5  thorpej 
    141  1.5  thorpej 	printf(": %s, rev. %d\n", MII_STR_NATSEMI_DP83840,
    142  1.5  thorpej 	    MII_REV(ma->mii_id2));
    143  1.5  thorpej 
    144  1.5  thorpej 	sc->sc_mii.mii_inst = mii->mii_instance;
    145  1.5  thorpej 	sc->sc_mii.mii_phy = ma->mii_phyno;
    146  1.5  thorpej 	sc->sc_mii.mii_service = nsphy_service;
    147  1.5  thorpej 	sc->sc_mii.mii_pdata = mii;
    148  1.5  thorpej 
    149  1.5  thorpej #define	ADD(m, c)	ifmedia_add(&mii->mii_media, (m), (c), NULL)
    150  1.5  thorpej 
    151  1.5  thorpej #if 0
    152  1.5  thorpej 	/* Can't do this on the i82557! */
    153  1.5  thorpej 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->sc_mii.mii_inst),
    154  1.5  thorpej 	    BMCR_ISO);
    155  1.5  thorpej #endif
    156  1.5  thorpej 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->sc_mii.mii_inst),
    157  1.5  thorpej 	    BMCR_LOOP|BMCR_S100);
    158  1.5  thorpej 
    159  1.5  thorpej 	nsphy_reset(sc);
    160  1.5  thorpej 
    161  1.5  thorpej 	sc->sc_capabilities = NSPHY_READ(sc, MII_BMSR) & ma->mii_capmask;
    162  1.5  thorpej 	printf("%s: ", sc->sc_mii.mii_dev.dv_xname);
    163  1.5  thorpej 	if ((sc->sc_capabilities & BMSR_MEDIAMASK) == 0)
    164  1.5  thorpej 		printf("no media present");
    165  1.5  thorpej 	else
    166  1.5  thorpej 		mii_add_media(mii, sc->sc_capabilities, sc->sc_mii.mii_inst);
    167  1.5  thorpej 	printf("\n");
    168  1.5  thorpej #undef ADD
    169  1.5  thorpej }
    170  1.5  thorpej 
    171  1.5  thorpej int
    172  1.5  thorpej nsphy_service(self, mii, cmd)
    173  1.5  thorpej 	struct mii_softc *self;
    174  1.5  thorpej 	struct mii_data *mii;
    175  1.5  thorpej 	int cmd;
    176  1.5  thorpej {
    177  1.5  thorpej 	struct nsphy_softc *sc = (struct nsphy_softc *)self;
    178  1.5  thorpej 	int reg;
    179  1.5  thorpej 
    180  1.5  thorpej 	switch (cmd) {
    181  1.5  thorpej 	case MII_POLLSTAT:
    182  1.5  thorpej 		/*
    183  1.5  thorpej 		 * If we're not polling our PHY instance, just return.
    184  1.5  thorpej 		 */
    185  1.5  thorpej 		if (IFM_INST(mii->mii_media.ifm_media) !=
    186  1.5  thorpej 		    sc->sc_mii.mii_inst)
    187  1.5  thorpej 			return (0);
    188  1.5  thorpej 		break;
    189  1.5  thorpej 
    190  1.5  thorpej 	case MII_MEDIACHG:
    191  1.5  thorpej 		/*
    192  1.5  thorpej 		 * If the media indicates a different PHY instance,
    193  1.5  thorpej 		 * isolate ourselves.
    194  1.5  thorpej 		 */
    195  1.5  thorpej 		if (IFM_INST(mii->mii_media.ifm_media) !=
    196  1.5  thorpej 		    sc->sc_mii.mii_inst) {
    197  1.5  thorpej 			reg = NSPHY_READ(sc, MII_BMCR);
    198  1.5  thorpej 			NSPHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
    199  1.5  thorpej 			return (0);
    200  1.5  thorpej 		}
    201  1.5  thorpej 
    202  1.5  thorpej 		/*
    203  1.5  thorpej 		 * If the interface is not up, don't do anything.
    204  1.5  thorpej 		 */
    205  1.5  thorpej 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
    206  1.5  thorpej 			break;
    207  1.5  thorpej 
    208  1.9  thorpej 		reg = NSPHY_READ(sc, MII_NSPHY_PCR);
    209  1.9  thorpej 
    210  1.5  thorpej 		/*
    211  1.5  thorpej 		 * Set up the PCR to use LED4 to indicate full-duplex
    212  1.5  thorpej 		 * in both 10baseT and 100baseTX modes.
    213  1.5  thorpej 		 */
    214  1.9  thorpej 		reg |= PCR_LED4MODE;
    215  1.9  thorpej 
    216  1.9  thorpej 		/*
    217  1.9  thorpej 		 * Make sure Carrier Intgrity Monitor function is
    218  1.9  thorpej 		 * disabled (normal for Node operation, but sometimes
    219  1.9  thorpej 		 * it's not set?!)
    220  1.9  thorpej 		 */
    221  1.9  thorpej 		reg |= PCR_CIMDIS;
    222  1.9  thorpej 
    223  1.9  thorpej 		/*
    224  1.9  thorpej 		 * Make sure "force link good" is not set.  It's only
    225  1.9  thorpej 		 * intended for debugging, but sometimes it's set
    226  1.9  thorpej 		 * after a reset.
    227  1.9  thorpej 		 */
    228  1.9  thorpej 		reg &= ~PCR_FLINK100;
    229  1.9  thorpej 
    230  1.9  thorpej #if 0
    231  1.9  thorpej 		/*
    232  1.9  thorpej 		 * Mystery bits which are supposedly `reserved',
    233  1.9  thorpej 		 * but we seem to need to set them when the PHY
    234  1.9  thorpej 		 * is connected to some interfaces!
    235  1.9  thorpej 		 */
    236  1.9  thorpej 		reg |= 0x0100 | 0x0400;
    237  1.9  thorpej #endif
    238  1.9  thorpej 
    239  1.9  thorpej 		NSPHY_WRITE(sc, MII_NSPHY_PCR, reg);
    240  1.5  thorpej 
    241  1.5  thorpej 		switch (IFM_SUBTYPE(mii->mii_media.ifm_media)) {
    242  1.5  thorpej 		case IFM_AUTO:
    243  1.5  thorpej 			/*
    244  1.5  thorpej 			 * If we're already in auto mode, just return.
    245  1.5  thorpej 			 */
    246  1.5  thorpej 			if (NSPHY_READ(sc, MII_BMCR) & BMCR_AUTOEN)
    247  1.5  thorpej 				return (0);
    248  1.5  thorpej 			nsphy_auto(sc);
    249  1.5  thorpej 			break;
    250  1.5  thorpej 		case IFM_100_T4:
    251  1.5  thorpej 			/*
    252  1.5  thorpej 			 * XXX Not supported as a manual setting right now.
    253  1.5  thorpej 			 */
    254  1.5  thorpej 			return (EINVAL);
    255  1.5  thorpej 		default:
    256  1.5  thorpej 			/*
    257  1.5  thorpej 			 * BMCR data is stored in the ifmedia entry.
    258  1.5  thorpej 			 */
    259  1.5  thorpej 			NSPHY_WRITE(sc, MII_ANAR,
    260  1.5  thorpej 			    mii_anar(mii->mii_media.ifm_media));
    261  1.5  thorpej 			NSPHY_WRITE(sc, MII_BMCR,
    262  1.5  thorpej 			    mii->mii_media.ifm_cur->ifm_data);
    263  1.5  thorpej 		}
    264  1.5  thorpej 		break;
    265  1.5  thorpej 
    266  1.5  thorpej 	case MII_TICK:
    267  1.5  thorpej 		/*
    268  1.5  thorpej 		 * If we're not currently selected, just return.
    269  1.5  thorpej 		 */
    270  1.5  thorpej 		if (IFM_INST(mii->mii_media.ifm_media) !=
    271  1.5  thorpej 		    sc->sc_mii.mii_inst)
    272  1.5  thorpej 			return (0);
    273  1.5  thorpej 
    274  1.5  thorpej 		/*
    275  1.5  thorpej 		 * Only used for autonegotiation.
    276  1.5  thorpej 		 */
    277  1.5  thorpej 		if (IFM_SUBTYPE(mii->mii_media.ifm_media) != IFM_AUTO)
    278  1.5  thorpej 			return (0);
    279  1.5  thorpej 
    280  1.5  thorpej 		/*
    281  1.5  thorpej 		 * Is the interface even up?
    282  1.5  thorpej 		 */
    283  1.5  thorpej 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
    284  1.5  thorpej 			return (0);
    285  1.5  thorpej 
    286  1.5  thorpej 		/*
    287  1.5  thorpej 		 * Check to see if we have link.  If we do, we don't
    288  1.5  thorpej 		 * need to restart the autonegotiation process.  Read
    289  1.5  thorpej 		 * the BMSR twice in case it's latched.
    290  1.5  thorpej 		 */
    291  1.5  thorpej 		reg = NSPHY_READ(sc, MII_BMSR) | NSPHY_READ(sc, MII_BMSR);
    292  1.5  thorpej 		if (reg & BMSR_LINK)
    293  1.5  thorpej 			return (0);
    294  1.5  thorpej 
    295  1.5  thorpej 		/*
    296  1.5  thorpej 		 * Only retry autonegotiation every 5 seconds.
    297  1.5  thorpej 		 */
    298  1.5  thorpej 		if (++sc->sc_ticks != 5)
    299  1.5  thorpej 			return (0);
    300  1.5  thorpej 
    301  1.5  thorpej 		sc->sc_ticks = 0;
    302  1.5  thorpej 		nsphy_reset(sc);
    303  1.5  thorpej 		nsphy_auto(sc);
    304  1.5  thorpej 		break;
    305  1.5  thorpej 	}
    306  1.5  thorpej 
    307  1.5  thorpej 	/* Update the media status. */
    308  1.5  thorpej 	nsphy_status(sc);
    309  1.5  thorpej 
    310  1.5  thorpej 	/* Callback if something changed. */
    311  1.5  thorpej 	if (sc->sc_active != mii->mii_media_active || cmd == MII_MEDIACHG) {
    312  1.5  thorpej 		(*mii->mii_statchg)(sc->sc_mii.mii_dev.dv_parent);
    313  1.5  thorpej 		sc->sc_active = mii->mii_media_active;
    314  1.5  thorpej 	}
    315  1.5  thorpej 	return (0);
    316  1.5  thorpej }
    317  1.5  thorpej 
    318  1.5  thorpej void
    319  1.5  thorpej nsphy_status(sc)
    320  1.5  thorpej 	struct nsphy_softc *sc;
    321  1.5  thorpej {
    322  1.5  thorpej 	struct mii_data *mii = sc->sc_mii.mii_pdata;
    323  1.8  thorpej 	int bmsr, bmcr, par, anlpar;
    324  1.5  thorpej 
    325  1.5  thorpej 	mii->mii_media_status = IFM_AVALID;
    326  1.5  thorpej 	mii->mii_media_active = IFM_ETHER;
    327  1.1   bouyer 
    328  1.5  thorpej 	bmsr = NSPHY_READ(sc, MII_BMSR) | NSPHY_READ(sc, MII_BMSR);
    329  1.5  thorpej 	if (bmsr & BMSR_LINK)
    330  1.5  thorpej 		mii->mii_media_status |= IFM_ACTIVE;
    331  1.5  thorpej 
    332  1.5  thorpej 	bmcr = NSPHY_READ(sc, MII_BMCR);
    333  1.5  thorpej 	if (bmcr & BMCR_ISO) {
    334  1.5  thorpej 		mii->mii_media_active |= IFM_NONE;
    335  1.5  thorpej 		mii->mii_media_status = 0;
    336  1.1   bouyer 		return;
    337  1.1   bouyer 	}
    338  1.5  thorpej 
    339  1.5  thorpej 	if (bmcr & BMCR_LOOP)
    340  1.5  thorpej 		mii->mii_media_active |= IFM_LOOP;
    341  1.5  thorpej 
    342  1.5  thorpej 	if (bmcr & BMCR_AUTOEN) {
    343  1.5  thorpej 		/*
    344  1.5  thorpej 		 * The PAR status bits are only valid of autonegotiation
    345  1.5  thorpej 		 * has completed (or it's disabled).
    346  1.5  thorpej 		 */
    347  1.5  thorpej 		if ((bmsr & BMSR_ACOMP) == 0) {
    348  1.7  thorpej 			/* Erg, still trying, I guess... */
    349  1.7  thorpej 			mii->mii_media_active |= IFM_NONE;
    350  1.5  thorpej 			return;
    351  1.5  thorpej 		}
    352  1.8  thorpej 
    353  1.8  thorpej 		/*
    354  1.8  thorpej 		 * Argh.  The PAR doesn't seem to indicate duplex mode
    355  1.8  thorpej 		 * properly!  Determine media based on link partner's
    356  1.8  thorpej 		 * advertised capabilities.
    357  1.8  thorpej 		 */
    358  1.8  thorpej 		if (NSPHY_READ(sc, MII_ANER) & ANER_LPAN) {
    359  1.8  thorpej 			anlpar = NSPHY_READ(sc, MII_ANAR) &
    360  1.8  thorpej 			    NSPHY_READ(sc, MII_ANLPAR);
    361  1.8  thorpej 			if (anlpar & ANLPAR_T4)
    362  1.8  thorpej 				mii->mii_media_active |= IFM_100_T4;
    363  1.8  thorpej 			else if (anlpar & ANLPAR_TX_FD)
    364  1.8  thorpej 				mii->mii_media_active |= IFM_100_TX|IFM_FDX;
    365  1.8  thorpej 			else if (anlpar & ANLPAR_TX)
    366  1.8  thorpej 				mii->mii_media_active |= IFM_100_TX;
    367  1.8  thorpej 			else if (anlpar & ANLPAR_10_FD)
    368  1.8  thorpej 				mii->mii_media_active |= IFM_10_T|IFM_FDX;
    369  1.8  thorpej 			else if (anlpar & ANLPAR_10)
    370  1.8  thorpej 				mii->mii_media_active |= IFM_10_T;
    371  1.8  thorpej 			else
    372  1.8  thorpej 				mii->mii_media_active |= IFM_NONE;
    373  1.8  thorpej 			return;
    374  1.8  thorpej 		}
    375  1.8  thorpej 
    376  1.8  thorpej 		/*
    377  1.8  thorpej 		 * Link partner is not capable of autonegotiation.
    378  1.8  thorpej 		 * We will never be in full-duplex mode if this is
    379  1.8  thorpej 		 * the case, so reading the PAR is OK.
    380  1.8  thorpej 		 */
    381  1.5  thorpej 		par = NSPHY_READ(sc, MII_NSPHY_PAR);
    382  1.5  thorpej 		if (par & PAR_10)
    383  1.5  thorpej 			mii->mii_media_active |= IFM_10_T;
    384  1.5  thorpej 		else
    385  1.5  thorpej 			mii->mii_media_active |= IFM_100_TX;
    386  1.8  thorpej #if 0
    387  1.5  thorpej 		if (par & PAR_FDX)
    388  1.5  thorpej 			mii->mii_media_active |= IFM_FDX;
    389  1.8  thorpej #endif
    390  1.5  thorpej 	} else {
    391  1.5  thorpej 		if (bmcr & BMCR_S100)
    392  1.5  thorpej 			mii->mii_media_active |= IFM_100_TX;
    393  1.5  thorpej 		else
    394  1.5  thorpej 			mii->mii_media_active |= IFM_10_T;
    395  1.5  thorpej 		if (bmcr & BMCR_FDX)
    396  1.5  thorpej 			mii->mii_media_active |= IFM_FDX;
    397  1.5  thorpej 	}
    398  1.1   bouyer }
    399  1.1   bouyer 
    400  1.2  thorpej void
    401  1.5  thorpej nsphy_auto(sc)
    402  1.5  thorpej 	struct nsphy_softc *sc;
    403  1.1   bouyer {
    404  1.5  thorpej 	int bmsr, i;
    405  1.2  thorpej 
    406  1.5  thorpej 	NSPHY_WRITE(sc, MII_ANAR,
    407  1.5  thorpej 	    BMSR_MEDIA_TO_ANAR(sc->sc_capabilities) | ANAR_CSMA);
    408  1.5  thorpej 	NSPHY_WRITE(sc, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG);
    409  1.5  thorpej 
    410  1.5  thorpej 	/* Wait 500ms for it to complete. */
    411  1.5  thorpej 	for (i = 0; i < 500; i++) {
    412  1.5  thorpej 		if ((bmsr = NSPHY_READ(sc, MII_BMSR)) & BMSR_ACOMP)
    413  1.5  thorpej 			return;
    414  1.5  thorpej 		delay(1000);
    415  1.5  thorpej 	}
    416  1.6  thorpej #if 0
    417  1.5  thorpej 	if ((bmsr & BMSR_ACOMP) == 0)
    418  1.5  thorpej 		printf("%s: autonegotiation failed to complete\n",
    419  1.5  thorpej 		    sc->sc_mii.mii_dev.dv_xname);
    420  1.6  thorpej #endif
    421  1.1   bouyer }
    422  1.1   bouyer 
    423  1.5  thorpej void
    424  1.5  thorpej nsphy_reset(sc)
    425  1.5  thorpej 	struct nsphy_softc *sc;
    426  1.5  thorpej {
    427  1.5  thorpej 	int reg, i;
    428  1.5  thorpej 
    429  1.5  thorpej 	/*
    430  1.5  thorpej 	 * The i82557 wedges if we isolate all of its PHYs!
    431  1.5  thorpej 	 */
    432  1.5  thorpej 	if (sc->sc_mii.mii_inst == 0)
    433  1.5  thorpej 		NSPHY_WRITE(sc, MII_BMCR, BMCR_RESET);
    434  1.5  thorpej 	else
    435  1.5  thorpej 		NSPHY_WRITE(sc, MII_BMCR, BMCR_RESET|BMCR_ISO);
    436  1.5  thorpej 
    437  1.5  thorpej 	/* Wait 100ms for it to complete. */
    438  1.5  thorpej 	for (i = 0; i < 100; i++) {
    439  1.5  thorpej 		reg = NSPHY_READ(sc, MII_BMCR);
    440  1.5  thorpej 		if ((reg & BMCR_RESET) == 0)
    441  1.5  thorpej 			break;
    442  1.5  thorpej 		delay(1000);
    443  1.1   bouyer 	}
    444  1.5  thorpej 
    445  1.5  thorpej 	/* Make sure the PHY is isolated. */
    446  1.5  thorpej 	if (sc->sc_mii.mii_inst != 0)
    447  1.5  thorpej 		NSPHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
    448  1.1   bouyer }
    449