Home | History | Annotate | Line # | Download | only in mii
nsphy.c revision 1.46.10.1
      1  1.46.10.1     yamt /*	$NetBSD: nsphy.c,v 1.46.10.1 2006/10/22 06:06:12 yamt Exp $	*/
      2        1.5  thorpej 
      3        1.5  thorpej /*-
      4       1.27  thorpej  * Copyright (c) 1998, 1999, 2000 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.33    lukem 
     74       1.33    lukem #include <sys/cdefs.h>
     75  1.46.10.1     yamt __KERNEL_RCSID(0, "$NetBSD: nsphy.c,v 1.46.10.1 2006/10/22 06:06:12 yamt Exp $");
     76        1.1   bouyer 
     77        1.1   bouyer #include <sys/param.h>
     78        1.1   bouyer #include <sys/systm.h>
     79        1.1   bouyer #include <sys/kernel.h>
     80        1.1   bouyer #include <sys/device.h>
     81        1.1   bouyer #include <sys/socket.h>
     82       1.17  thorpej #include <sys/errno.h>
     83        1.5  thorpej 
     84        1.1   bouyer #include <net/if.h>
     85        1.1   bouyer #include <net/if_media.h>
     86        1.1   bouyer 
     87        1.5  thorpej #include <dev/mii/mii.h>
     88        1.5  thorpej #include <dev/mii/miivar.h>
     89        1.5  thorpej #include <dev/mii/miidevs.h>
     90        1.5  thorpej 
     91        1.5  thorpej #include <dev/mii/nsphyreg.h>
     92        1.5  thorpej 
     93       1.42  thorpej static int	nsphymatch(struct device *, struct cfdata *, void *);
     94       1.42  thorpej static void	nsphyattach(struct device *, struct device *, void *);
     95        1.1   bouyer 
     96       1.37  thorpej CFATTACH_DECL(nsphy, sizeof(struct mii_softc),
     97       1.38  thorpej     nsphymatch, nsphyattach, mii_phy_detach, mii_phy_activate);
     98        1.1   bouyer 
     99       1.42  thorpej static int	nsphy_service(struct mii_softc *, struct mii_data *, int);
    100       1.42  thorpej static void	nsphy_status(struct mii_softc *);
    101       1.42  thorpej static void	nsphy_reset(struct mii_softc *sc);
    102        1.5  thorpej 
    103       1.42  thorpej static const struct mii_phy_funcs nsphy_funcs = {
    104       1.40   briggs 	nsphy_service, nsphy_status, nsphy_reset,
    105       1.27  thorpej };
    106       1.27  thorpej 
    107       1.42  thorpej static const struct mii_phydesc nsphys[] = {
    108       1.31  thorpej 	{ MII_OUI_xxNATSEMI,		MII_MODEL_xxNATSEMI_DP83840,
    109       1.31  thorpej 	  MII_STR_xxNATSEMI_DP83840 },
    110       1.31  thorpej 
    111       1.31  thorpej 	{ 0,				0,
    112       1.31  thorpej 	  NULL },
    113       1.31  thorpej };
    114       1.31  thorpej 
    115       1.42  thorpej static int
    116  1.46.10.1     yamt nsphymatch(struct device *parent __unused, struct cfdata *match __unused,
    117  1.46.10.1     yamt     void *aux)
    118        1.1   bouyer {
    119        1.5  thorpej 	struct mii_attach_args *ma = aux;
    120        1.5  thorpej 
    121       1.31  thorpej 	if (mii_phy_match(ma, nsphys) != NULL)
    122       1.14  thorpej 		return (10);
    123        1.1   bouyer 
    124        1.5  thorpej 	return (0);
    125        1.1   bouyer }
    126        1.1   bouyer 
    127       1.42  thorpej static void
    128  1.46.10.1     yamt nsphyattach(struct device *parent __unused, struct device *self, void *aux)
    129        1.1   bouyer {
    130       1.46  thorpej 	struct mii_softc *sc = device_private(self);
    131        1.5  thorpej 	struct mii_attach_args *ma = aux;
    132        1.5  thorpej 	struct mii_data *mii = ma->mii_data;
    133       1.31  thorpej 	const struct mii_phydesc *mpd;
    134        1.5  thorpej 
    135       1.31  thorpej 	mpd = mii_phy_match(ma, nsphys);
    136       1.39  thorpej 	aprint_naive(": Media interface\n");
    137       1.39  thorpej 	aprint_normal(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2));
    138        1.5  thorpej 
    139       1.15  thorpej 	sc->mii_inst = mii->mii_instance;
    140       1.15  thorpej 	sc->mii_phy = ma->mii_phyno;
    141       1.27  thorpej 	sc->mii_funcs = &nsphy_funcs;
    142       1.15  thorpej 	sc->mii_pdata = mii;
    143       1.34  thorpej 	sc->mii_flags = ma->mii_flags;
    144       1.30  thorpej 	sc->mii_anegticks = 5;
    145       1.13  thorpej 
    146       1.27  thorpej 	PHY_RESET(sc);
    147        1.5  thorpej 
    148       1.15  thorpej 	sc->mii_capabilities =
    149       1.15  thorpej 	    PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
    150       1.39  thorpej 	aprint_normal("%s: ", sc->mii_dev.dv_xname);
    151       1.15  thorpej 	if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0)
    152       1.39  thorpej 		aprint_error("no media present");
    153        1.5  thorpej 	else
    154       1.22  thorpej 		mii_phy_add_media(sc);
    155       1.39  thorpej 	aprint_normal("\n");
    156        1.5  thorpej }
    157        1.5  thorpej 
    158       1.42  thorpej static int
    159       1.32  thorpej nsphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
    160        1.5  thorpej {
    161       1.10  thorpej 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
    162        1.5  thorpej 	int reg;
    163       1.21  thorpej 
    164       1.45  thorpej 	if (!device_is_active(&sc->mii_dev))
    165       1.21  thorpej 		return (ENXIO);
    166        1.5  thorpej 
    167        1.5  thorpej 	switch (cmd) {
    168        1.5  thorpej 	case MII_POLLSTAT:
    169        1.5  thorpej 		/*
    170        1.5  thorpej 		 * If we're not polling our PHY instance, just return.
    171        1.5  thorpej 		 */
    172       1.15  thorpej 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
    173        1.5  thorpej 			return (0);
    174        1.5  thorpej 		break;
    175        1.5  thorpej 
    176        1.5  thorpej 	case MII_MEDIACHG:
    177        1.5  thorpej 		/*
    178        1.5  thorpej 		 * If the media indicates a different PHY instance,
    179        1.5  thorpej 		 * isolate ourselves.
    180        1.5  thorpej 		 */
    181       1.15  thorpej 		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
    182       1.15  thorpej 			reg = PHY_READ(sc, MII_BMCR);
    183       1.15  thorpej 			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
    184        1.5  thorpej 			return (0);
    185        1.5  thorpej 		}
    186        1.5  thorpej 
    187        1.5  thorpej 		/*
    188        1.5  thorpej 		 * If the interface is not up, don't do anything.
    189        1.5  thorpej 		 */
    190        1.5  thorpej 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
    191        1.5  thorpej 			break;
    192        1.5  thorpej 
    193       1.15  thorpej 		reg = PHY_READ(sc, MII_NSPHY_PCR);
    194        1.9  thorpej 
    195        1.5  thorpej 		/*
    196        1.5  thorpej 		 * Set up the PCR to use LED4 to indicate full-duplex
    197        1.5  thorpej 		 * in both 10baseT and 100baseTX modes.
    198        1.5  thorpej 		 */
    199        1.9  thorpej 		reg |= PCR_LED4MODE;
    200        1.9  thorpej 
    201        1.9  thorpej 		/*
    202       1.41      wiz 		 * Make sure Carrier Integrity Monitor function is
    203        1.9  thorpej 		 * disabled (normal for Node operation, but sometimes
    204        1.9  thorpej 		 * it's not set?!)
    205        1.9  thorpej 		 */
    206        1.9  thorpej 		reg |= PCR_CIMDIS;
    207        1.9  thorpej 
    208        1.9  thorpej 		/*
    209       1.18  thorpej 		 * Make sure "force link good" is set to normal mode.
    210       1.18  thorpej 		 * It's only intended for debugging.
    211        1.9  thorpej 		 */
    212       1.18  thorpej 		reg |= PCR_FLINK100;
    213        1.9  thorpej 
    214        1.9  thorpej 		/*
    215        1.9  thorpej 		 * Mystery bits which are supposedly `reserved',
    216        1.9  thorpej 		 * but we seem to need to set them when the PHY
    217       1.28   martin 		 * is connected to some interfaces:
    218       1.28   martin 		 *
    219       1.28   martin 		 * 0x0400 is needed for fxp
    220       1.28   martin 		 *        (Intel EtherExpress Pro 10+/100B, 82557 chip)
    221       1.28   martin 		 *        (nsphy with a DP83840 chip)
    222       1.28   martin 		 * 0x0100 may be needed for some other card
    223        1.9  thorpej 		 */
    224        1.9  thorpej 		reg |= 0x0100 | 0x0400;
    225        1.9  thorpej 
    226       1.15  thorpej 		PHY_WRITE(sc, MII_NSPHY_PCR, reg);
    227        1.5  thorpej 
    228       1.25  thorpej 		mii_phy_setmedia(sc);
    229        1.5  thorpej 		break;
    230        1.5  thorpej 
    231        1.5  thorpej 	case MII_TICK:
    232        1.5  thorpej 		/*
    233        1.5  thorpej 		 * If we're not currently selected, just return.
    234        1.5  thorpej 		 */
    235       1.15  thorpej 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
    236        1.5  thorpej 			return (0);
    237        1.5  thorpej 
    238       1.26  thorpej 		if (mii_phy_tick(sc) == EJUSTRETURN)
    239       1.17  thorpej 			return (0);
    240        1.5  thorpej 		break;
    241       1.20  thorpej 
    242       1.20  thorpej 	case MII_DOWN:
    243       1.20  thorpej 		mii_phy_down(sc);
    244       1.20  thorpej 		return (0);
    245        1.5  thorpej 	}
    246        1.5  thorpej 
    247        1.5  thorpej 	/* Update the media status. */
    248       1.24  thorpej 	mii_phy_status(sc);
    249        1.5  thorpej 
    250        1.5  thorpej 	/* Callback if something changed. */
    251       1.26  thorpej 	mii_phy_update(sc, cmd);
    252        1.5  thorpej 	return (0);
    253        1.5  thorpej }
    254        1.5  thorpej 
    255       1.42  thorpej static void
    256       1.32  thorpej nsphy_status(struct mii_softc *sc)
    257        1.5  thorpej {
    258       1.15  thorpej 	struct mii_data *mii = sc->mii_pdata;
    259       1.19  thorpej 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
    260        1.8  thorpej 	int bmsr, bmcr, par, anlpar;
    261        1.5  thorpej 
    262        1.5  thorpej 	mii->mii_media_status = IFM_AVALID;
    263        1.5  thorpej 	mii->mii_media_active = IFM_ETHER;
    264        1.1   bouyer 
    265       1.15  thorpej 	bmsr = PHY_READ(sc, MII_BMSR) |
    266       1.15  thorpej 	    PHY_READ(sc, MII_BMSR);
    267        1.5  thorpej 	if (bmsr & BMSR_LINK)
    268        1.5  thorpej 		mii->mii_media_status |= IFM_ACTIVE;
    269        1.5  thorpej 
    270       1.15  thorpej 	bmcr = PHY_READ(sc, MII_BMCR);
    271        1.5  thorpej 	if (bmcr & BMCR_ISO) {
    272        1.5  thorpej 		mii->mii_media_active |= IFM_NONE;
    273        1.5  thorpej 		mii->mii_media_status = 0;
    274        1.1   bouyer 		return;
    275        1.1   bouyer 	}
    276        1.5  thorpej 
    277        1.5  thorpej 	if (bmcr & BMCR_LOOP)
    278        1.5  thorpej 		mii->mii_media_active |= IFM_LOOP;
    279        1.5  thorpej 
    280        1.5  thorpej 	if (bmcr & BMCR_AUTOEN) {
    281        1.5  thorpej 		/*
    282        1.5  thorpej 		 * The PAR status bits are only valid of autonegotiation
    283        1.5  thorpej 		 * has completed (or it's disabled).
    284        1.5  thorpej 		 */
    285        1.5  thorpej 		if ((bmsr & BMSR_ACOMP) == 0) {
    286        1.7  thorpej 			/* Erg, still trying, I guess... */
    287        1.7  thorpej 			mii->mii_media_active |= IFM_NONE;
    288        1.5  thorpej 			return;
    289        1.5  thorpej 		}
    290        1.8  thorpej 
    291        1.8  thorpej 		/*
    292        1.8  thorpej 		 * Argh.  The PAR doesn't seem to indicate duplex mode
    293        1.8  thorpej 		 * properly!  Determine media based on link partner's
    294        1.8  thorpej 		 * advertised capabilities.
    295        1.8  thorpej 		 */
    296       1.15  thorpej 		if (PHY_READ(sc, MII_ANER) & ANER_LPAN) {
    297       1.15  thorpej 			anlpar = PHY_READ(sc, MII_ANAR) &
    298       1.15  thorpej 			    PHY_READ(sc, MII_ANLPAR);
    299        1.8  thorpej 			if (anlpar & ANLPAR_T4)
    300        1.8  thorpej 				mii->mii_media_active |= IFM_100_T4;
    301        1.8  thorpej 			else if (anlpar & ANLPAR_TX_FD)
    302        1.8  thorpej 				mii->mii_media_active |= IFM_100_TX|IFM_FDX;
    303        1.8  thorpej 			else if (anlpar & ANLPAR_TX)
    304        1.8  thorpej 				mii->mii_media_active |= IFM_100_TX;
    305        1.8  thorpej 			else if (anlpar & ANLPAR_10_FD)
    306        1.8  thorpej 				mii->mii_media_active |= IFM_10_T|IFM_FDX;
    307        1.8  thorpej 			else if (anlpar & ANLPAR_10)
    308        1.8  thorpej 				mii->mii_media_active |= IFM_10_T;
    309        1.8  thorpej 			else
    310        1.8  thorpej 				mii->mii_media_active |= IFM_NONE;
    311        1.8  thorpej 			return;
    312        1.8  thorpej 		}
    313        1.8  thorpej 
    314        1.8  thorpej 		/*
    315        1.8  thorpej 		 * Link partner is not capable of autonegotiation.
    316        1.8  thorpej 		 * We will never be in full-duplex mode if this is
    317        1.8  thorpej 		 * the case, so reading the PAR is OK.
    318        1.8  thorpej 		 */
    319       1.15  thorpej 		par = PHY_READ(sc, MII_NSPHY_PAR);
    320        1.5  thorpej 		if (par & PAR_10)
    321        1.5  thorpej 			mii->mii_media_active |= IFM_10_T;
    322        1.5  thorpej 		else
    323        1.5  thorpej 			mii->mii_media_active |= IFM_100_TX;
    324        1.8  thorpej #if 0
    325        1.5  thorpej 		if (par & PAR_FDX)
    326        1.5  thorpej 			mii->mii_media_active |= IFM_FDX;
    327        1.8  thorpej #endif
    328       1.16  thorpej 	} else
    329       1.19  thorpej 		mii->mii_media_active = ife->ifm_media;
    330        1.1   bouyer }
    331       1.40   briggs 
    332       1.42  thorpej static void
    333       1.40   briggs nsphy_reset(struct mii_softc *sc)
    334       1.40   briggs {
    335       1.40   briggs 	int reg, i;
    336       1.40   briggs 
    337       1.40   briggs 	if (sc->mii_flags & MIIF_NOISOLATE)
    338       1.40   briggs 		reg = BMCR_RESET;
    339       1.40   briggs 	else
    340       1.40   briggs 		reg = BMCR_RESET | BMCR_ISO;
    341       1.40   briggs 	PHY_WRITE(sc, MII_BMCR, reg);
    342       1.40   briggs 
    343       1.40   briggs 	/*
    344       1.40   briggs 	 * Give it a little time to settle in case we just got power.
    345       1.40   briggs 	 * The DP83840A data sheet suggests that a soft reset not happen
    346       1.40   briggs 	 * within 500us of power being applied.  Be conservative.
    347       1.40   briggs 	 */
    348       1.40   briggs 	delay(1000);
    349       1.40   briggs 
    350       1.40   briggs 	/*
    351       1.40   briggs 	 * Wait another 2s for it to complete.
    352       1.40   briggs 	 * This is only a little overkill as under normal circumstances
    353       1.40   briggs 	 * the PHY can take up to 1s to complete reset.
    354       1.40   briggs 	 * This is also a bit odd because after a reset, the BMCR will
    355       1.40   briggs 	 * clear the reset bit and simply reports 0 even though the reset
    356       1.40   briggs 	 * is not yet complete.
    357       1.40   briggs 	 */
    358       1.40   briggs 	for (i = 0; i < 1000; i++) {
    359       1.43    perry 		reg = PHY_READ(sc, MII_BMCR);
    360       1.40   briggs 		if (reg && ((reg & BMCR_RESET) == 0))
    361       1.40   briggs 			break;
    362       1.40   briggs 		delay(2000);
    363       1.40   briggs 	}
    364       1.40   briggs 
    365       1.40   briggs 	if (sc->mii_inst != 0 && ((sc->mii_flags & MIIF_NOISOLATE) == 0)) {
    366       1.40   briggs 		PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
    367       1.40   briggs 	}
    368       1.40   briggs }
    369