Home | History | Annotate | Line # | Download | only in mii
gphyter.c revision 1.25
      1 /*	$NetBSD: gphyter.c,v 1.25 2008/05/05 01:37:56 tsutsui 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  *
     20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  * POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * Copyright (c) 1997 Manuel Bouyer.  All rights reserved.
     35  *
     36  * Redistribution and use in source and binary forms, with or without
     37  * modification, are permitted provided that the following conditions
     38  * are met:
     39  * 1. Redistributions of source code must retain the above copyright
     40  *    notice, this list of conditions and the following disclaimer.
     41  * 2. Redistributions in binary form must reproduce the above copyright
     42  *    notice, this list of conditions and the following disclaimer in the
     43  *    documentation and/or other materials provided with the distribution.
     44  * 3. All advertising materials mentioning features or use of this software
     45  *    must display the following acknowledgement:
     46  *	This product includes software developed by Manuel Bouyer.
     47  * 4. The name of the author may not be used to endorse or promote products
     48  *    derived from this software without specific prior written permission.
     49  *
     50  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     51  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     52  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     53  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     54  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     55  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     56  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     57  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     58  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     59  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     60  */
     61 
     62 /*
     63  * driver for National Semiconductor's DP83891 and DP83861 `Gig PHYTER'
     64  * ethernet 10/100/1000 PHYs.  The DP83891 is an older, non-firmware-
     65  * driven version of the DP83861.
     66  *
     67  * Data Sheet available from www.national.com
     68  */
     69 
     70 #include <sys/cdefs.h>
     71 __KERNEL_RCSID(0, "$NetBSD: gphyter.c,v 1.25 2008/05/05 01:37:56 tsutsui Exp $");
     72 
     73 #include <sys/param.h>
     74 #include <sys/systm.h>
     75 #include <sys/kernel.h>
     76 #include <sys/device.h>
     77 #include <sys/socket.h>
     78 #include <sys/errno.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 #include <dev/mii/gphyterreg.h>
     88 
     89 static int	gphytermatch(device_t, cfdata_t, void *);
     90 static void	gphyterattach(device_t, device_t, void *);
     91 
     92 CFATTACH_DECL_NEW(gphyter, sizeof(struct mii_softc),
     93     gphytermatch, gphyterattach, mii_phy_detach, mii_phy_activate);
     94 
     95 static int	gphyter_service(struct mii_softc *, struct mii_data *, int);
     96 static void	gphyter_status(struct mii_softc *);
     97 static void	gphyter_reset(struct mii_softc *);
     98 
     99 static const struct mii_phy_funcs gphyter_funcs = {
    100 	gphyter_service, gphyter_status, gphyter_reset,
    101 };
    102 
    103 static const struct mii_phydesc gphyters[] = {
    104 	{ MII_OUI_xxNATSEMI,		MII_MODEL_xxNATSEMI_DP83861,
    105 	  MII_STR_xxNATSEMI_DP83861 },
    106 
    107 	{ MII_OUI_xxNATSEMI,		MII_MODEL_xxNATSEMI_DP83891,
    108 	  MII_STR_xxNATSEMI_DP83891 },
    109 
    110 	{ 0,				0,
    111 	  NULL },
    112 };
    113 
    114 static int
    115 gphytermatch(device_t parent, cfdata_t match, void *aux)
    116 {
    117 	struct mii_attach_args *ma = aux;
    118 
    119 	if (mii_phy_match(ma, gphyters) != NULL)
    120 		return (10);
    121 
    122 	return (0);
    123 }
    124 
    125 static void
    126 gphyterattach(device_t parent, device_t self, void *aux)
    127 {
    128 	struct mii_softc *sc = device_private(self);
    129 	struct mii_attach_args *ma = aux;
    130 	struct mii_data *mii = ma->mii_data;
    131 	const struct mii_phydesc *mpd;
    132 	int anar, strap;
    133 
    134 	mpd = mii_phy_match(ma, gphyters);
    135 	aprint_naive(": Media interface\n");
    136 	aprint_normal(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2));
    137 
    138 	sc->mii_dev = self;
    139 	sc->mii_inst = mii->mii_instance;
    140 	sc->mii_phy = ma->mii_phyno;
    141 	sc->mii_funcs = &gphyter_funcs;
    142 	sc->mii_pdata = mii;
    143 	sc->mii_flags = ma->mii_flags;
    144 	sc->mii_anegticks = MII_ANEGTICKS;
    145 
    146 	PHY_RESET(sc);
    147 
    148 	sc->mii_capabilities =
    149 	    PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
    150 	if (sc->mii_capabilities & BMSR_EXTSTAT)
    151 		sc->mii_extcapabilities = PHY_READ(sc, MII_EXTSR);
    152 
    153 	/*
    154 	 * The Gig PHYTER seems to have the 10baseT BMSR bits
    155 	 * hard-wired to 0, even though the device supports
    156 	 * 10baseT.  What we do instead is read the post-reset
    157 	 * ANAR, who's 10baseT-related bits are set by strapping
    158 	 * pin 180, and fake the BMSR bits.
    159 	 */
    160 	anar = PHY_READ(sc, MII_ANAR);
    161 	if (anar & ANAR_10)
    162 		sc->mii_capabilities |= (BMSR_10THDX & ma->mii_capmask);
    163 	if (anar & ANAR_10_FD)
    164 		sc->mii_capabilities |= (BMSR_10TFDX & ma->mii_capmask);
    165 
    166 	aprint_normal_dev(self, "");
    167 	if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0 &&
    168 	    (sc->mii_extcapabilities & EXTSR_MEDIAMASK) == 0)
    169 		aprint_error("no media present");
    170 	else
    171 		mii_phy_add_media(sc);
    172 	aprint_normal("\n");
    173 
    174 	strap = PHY_READ(sc, MII_GPHYTER_STRAP);
    175 	aprint_normal_dev(self, "strapped to %s mode",
    176 	    (strap & STRAP_MS_VAL) ? "master" : "slave");
    177 	if (strap & STRAP_NC_MODE)
    178 		aprint_normal(", pre-C5 BCM5400 compat enabled");
    179 	aprint_normal("\n");
    180 
    181 	if (!pmf_device_register(self, NULL, mii_phy_resume))
    182 		aprint_error_dev(self, "couldn't establish power handler\n");
    183 }
    184 
    185 static int
    186 gphyter_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
    187 {
    188 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
    189 	int reg;
    190 
    191 	switch (cmd) {
    192 	case MII_POLLSTAT:
    193 		/*
    194 		 * If we're not polling our PHY instance, just return.
    195 		 */
    196 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
    197 			return (0);
    198 		break;
    199 
    200 	case MII_MEDIACHG:
    201 		/*
    202 		 * If the media indicates a different PHY instance,
    203 		 * isolate ourselves.
    204 		 */
    205 		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
    206 			reg = PHY_READ(sc, MII_BMCR);
    207 			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
    208 			return (0);
    209 		}
    210 
    211 		/*
    212 		 * If the interface is not up, don't do anything.
    213 		 */
    214 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
    215 			break;
    216 
    217 		mii_phy_setmedia(sc);
    218 		break;
    219 
    220 	case MII_TICK:
    221 		/*
    222 		 * If we're not currently selected, just return.
    223 		 */
    224 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
    225 			return (0);
    226 
    227 		if (mii_phy_tick(sc) == EJUSTRETURN)
    228 			return (0);
    229 		break;
    230 
    231 	case MII_DOWN:
    232 		mii_phy_down(sc);
    233 		return (0);
    234 	}
    235 
    236 	/* Update the media status. */
    237 	mii_phy_status(sc);
    238 
    239 	/* Callback if something changed. */
    240 	mii_phy_update(sc, cmd);
    241 	return (0);
    242 }
    243 
    244 static void
    245 gphyter_status(struct mii_softc *sc)
    246 {
    247 	struct mii_data *mii = sc->mii_pdata;
    248 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
    249 	int bmsr, bmcr, physup, gtsr;
    250 
    251 	mii->mii_media_status = IFM_AVALID;
    252 	mii->mii_media_active = IFM_ETHER;
    253 
    254 	bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
    255 
    256 	physup = PHY_READ(sc, MII_GPHYTER_PHY_SUP);
    257 
    258 	if (physup & PHY_SUP_LINK)
    259 		mii->mii_media_status |= IFM_ACTIVE;
    260 
    261 	bmcr = PHY_READ(sc, MII_BMCR);
    262 	if (bmcr & BMCR_ISO) {
    263 		mii->mii_media_active |= IFM_NONE;
    264 		mii->mii_media_status = 0;
    265 		return;
    266 	}
    267 
    268 	if (bmcr & BMCR_LOOP)
    269 		mii->mii_media_active |= IFM_LOOP;
    270 
    271 	if (bmcr & BMCR_AUTOEN) {
    272 		/*
    273 		 * The media status bits are only valid of autonegotiation
    274 		 * has completed (or it's disabled).
    275 		 */
    276 		if ((bmsr & BMSR_ACOMP) == 0) {
    277 			/* Erg, still trying, I guess... */
    278 			mii->mii_media_active |= IFM_NONE;
    279 			return;
    280 		}
    281 
    282 		switch (physup & (PHY_SUP_SPEED1|PHY_SUP_SPEED0)) {
    283 		case PHY_SUP_SPEED1:
    284 			mii->mii_media_active |= IFM_1000_T;
    285 			gtsr = PHY_READ(sc, MII_100T2SR);
    286 			if (gtsr & GTSR_MS_RES)
    287 				mii->mii_media_active |= IFM_ETH_MASTER;
    288 			break;
    289 
    290 		case PHY_SUP_SPEED0:
    291 			mii->mii_media_active |= IFM_100_TX;
    292 			break;
    293 
    294 		case 0:
    295 			mii->mii_media_active |= IFM_10_T;
    296 			break;
    297 
    298 		default:
    299 			mii->mii_media_active |= IFM_NONE;
    300 			mii->mii_media_status = 0;
    301 		}
    302 		if (physup & PHY_SUP_DUPLEX)
    303 			mii->mii_media_active |=
    304 			    IFM_FDX | mii_phy_flowstatus(sc);
    305 	} else
    306 		mii->mii_media_active = ife->ifm_media;
    307 }
    308 
    309 void
    310 gphyter_reset(struct mii_softc *sc)
    311 {
    312 	int reg, i;
    313 
    314 	if (sc->mii_flags & MIIF_NOISOLATE)
    315 		reg = BMCR_RESET;
    316 	else
    317 		reg = BMCR_RESET | BMCR_ISO;
    318 	PHY_WRITE(sc, MII_BMCR, reg);
    319 
    320 	/*
    321 	 * It is best to allow a little time for the reset to settle
    322 	 * in before we start polling the BMCR again.  Notably, the
    323 	 * DP83840A manual states that there should be a 500us delay
    324 	 * between asserting software reset and attempting MII serial
    325 	 * operations.  Also, a DP83815 can get into a bad state on
    326 	 * cable removal and reinsertion if we do not delay here.
    327 	 */
    328 	delay(500);
    329 
    330 	/* Wait another 100ms for it to complete. */
    331 	for (i = 0; i < 100; i++) {
    332 		reg = PHY_READ(sc, MII_BMCR);
    333 		if ((reg & BMCR_RESET) == 0)
    334 			break;
    335 		delay(1000);
    336 	}
    337 
    338 	if (sc->mii_inst != 0 && ((sc->mii_flags & MIIF_NOISOLATE) == 0))
    339 		PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
    340 }
    341