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