Home | History | Annotate | Line # | Download | only in mii
ihphy.c revision 1.10.8.1
      1 /*	$NetBSD: ihphy.c,v 1.10.8.1 2019/08/01 14:27:30 martin Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998, 1999, 2000 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, and by Frank van der Linden.
     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 Intel's 82577 (Hanksville) Ethernet 10/100/1000 PHY
     59  * Data Sheet: http://download.intel.com/design/network/datashts/319439.pdf
     60  */
     61 
     62 #include <sys/cdefs.h>
     63 __KERNEL_RCSID(0, "$NetBSD: ihphy.c,v 1.10.8.1 2019/08/01 14:27:30 martin Exp $");
     64 
     65 #include <sys/param.h>
     66 #include <sys/systm.h>
     67 #include <sys/kernel.h>
     68 #include <sys/device.h>
     69 #include <sys/socket.h>
     70 #include <sys/errno.h>
     71 
     72 #include <net/if.h>
     73 #include <net/if_media.h>
     74 
     75 #include <dev/mii/mii.h>
     76 #include <dev/mii/miivar.h>
     77 #include <dev/mii/miidevs.h>
     78 
     79 #include <dev/mii/ihphyreg.h>
     80 
     81 static int	ihphymatch(device_t, cfdata_t, void *);
     82 static void	ihphyattach(device_t, device_t, void *);
     83 
     84 CFATTACH_DECL3_NEW(ihphy, sizeof(struct mii_softc),
     85     ihphymatch, ihphyattach, mii_phy_detach, mii_phy_activate, NULL, NULL,
     86     DVF_DETACH_SHUTDOWN);
     87 
     88 static int	ihphy_service(struct mii_softc *, struct mii_data *, int);
     89 static void	ihphy_status(struct mii_softc *);
     90 static void	ihphy_reset(struct mii_softc *);
     91 
     92 static const struct mii_phy_funcs ihphy_funcs = {
     93 	ihphy_service, ihphy_status, ihphy_reset,
     94 };
     95 
     96 static const struct mii_phydesc ihphys[] = {
     97 	{ MII_OUI_INTEL,		MII_MODEL_INTEL_I82577,
     98 	  MII_STR_INTEL_I82577 },
     99 	{ MII_OUI_INTEL,		MII_MODEL_INTEL_I82579,
    100 	  MII_STR_INTEL_I82579 },
    101 	{ MII_OUI_INTEL,		MII_MODEL_INTEL_I217,
    102 	  MII_STR_INTEL_I217 },
    103 	{ MII_OUI_INTEL,		MII_MODEL_INTEL_I82580,
    104 	  MII_STR_INTEL_I82580},
    105 	{ MII_OUI_INTEL,		MII_MODEL_INTEL_I350,
    106 	  MII_STR_INTEL_I350},
    107 
    108 	{ 0,				0,
    109 	  NULL },
    110 };
    111 
    112 static int
    113 ihphymatch(device_t parent, cfdata_t match, void *aux)
    114 {
    115 	struct mii_attach_args *ma = aux;
    116 
    117 	if (mii_phy_match(ma, ihphys) != NULL)
    118 		return 10;
    119 
    120 	return 0;
    121 }
    122 
    123 static void
    124 ihphyattach(device_t parent, device_t self, void *aux)
    125 {
    126 	struct mii_softc *sc = device_private(self);
    127 	struct mii_attach_args *ma = aux;
    128 	struct mii_data *mii = ma->mii_data;
    129 	const struct mii_phydesc *mpd;
    130 	int reg;
    131 
    132 	mpd = mii_phy_match(ma, ihphys);
    133 	aprint_naive(": Media interface\n");
    134 	aprint_normal(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2));
    135 
    136 	sc->mii_dev = self;
    137 	sc->mii_inst = mii->mii_instance;
    138 	sc->mii_phy = ma->mii_phyno;
    139 	sc->mii_mpd_oui = MII_OUI(ma->mii_id1, ma->mii_id2);
    140 	sc->mii_mpd_model = MII_MODEL(ma->mii_id2);
    141 	sc->mii_mpd_rev = MII_REV(ma->mii_id2);
    142 	sc->mii_funcs = &ihphy_funcs;
    143 	sc->mii_pdata = mii;
    144 	sc->mii_flags = ma->mii_flags;
    145 	sc->mii_anegticks = MII_ANEGTICKS;
    146 
    147 	PHY_RESET(sc);
    148 
    149 	sc->mii_capabilities = 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 	aprint_normal_dev(self, "");
    153 	if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0 &&
    154 	    (sc->mii_extcapabilities & EXTSR_MEDIAMASK) == 0)
    155 		aprint_error("no media present");
    156 	else
    157 		mii_phy_add_media(sc);
    158 	aprint_normal("\n");
    159 
    160 	/* Link setup (as done by Intel's Linux driver for the 82577). */
    161 	reg = PHY_READ(sc, IHPHY_MII_CFG);
    162 	reg |= IHPHY_CFG_TX_CRS;
    163 	reg |= IHPHY_CFG_DOWN_SHIFT;
    164 	PHY_WRITE(sc, IHPHY_MII_CFG, reg);
    165 }
    166 
    167 static int
    168 ihphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
    169 {
    170 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
    171 	int reg;
    172 
    173 	switch (cmd) {
    174 	case MII_POLLSTAT:
    175 		/* If we're not polling our PHY instance, just return. */
    176 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
    177 			return 0;
    178 		break;
    179 
    180 	case MII_MEDIACHG:
    181 		/*
    182 		 * If the media indicates a different PHY instance,
    183 		 * isolate ourselves.
    184 		 */
    185 		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
    186 			reg = PHY_READ(sc, MII_BMCR);
    187 			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
    188 			return 0;
    189 		}
    190 
    191 		/* If the interface is not up, don't do anything. */
    192 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
    193 			break;
    194 
    195 		/* If media is deselected, disable link (standby). */
    196 		reg = PHY_READ(sc, IHPHY_MII_ECR);
    197 		if (IFM_SUBTYPE(ife->ifm_media) == IFM_NONE)
    198 			reg &= ~IHPHY_ECR_LNK_EN;
    199 		else
    200 			reg |= IHPHY_ECR_LNK_EN;
    201 		PHY_WRITE(sc, IHPHY_MII_ECR, reg);
    202 
    203 		/* XXX Adjust MDI/MDIX configuration?  Other settings? */
    204 
    205 		mii_phy_setmedia(sc);
    206 		break;
    207 
    208 	case MII_TICK:
    209 		/* If we're not currently selected, just return. */
    210 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
    211 			return 0;
    212 
    213 		if (mii_phy_tick(sc) == EJUSTRETURN)
    214 			return 0;
    215 		break;
    216 
    217 	case MII_DOWN:
    218 		mii_phy_down(sc);
    219 		PHY_WRITE(sc, MII_BMCR, BMCR_PDOWN);
    220 		return 0;
    221 	}
    222 
    223 	/* Update the media status. */
    224 	mii_phy_status(sc);
    225 
    226 	/* Callback if something changed. */
    227 	mii_phy_update(sc, cmd);
    228 	return 0;
    229 }
    230 
    231 static void
    232 ihphy_status(struct mii_softc *sc)
    233 {
    234 	struct mii_data *mii = sc->mii_pdata;
    235 	int esr, bmcr, gtsr;
    236 
    237 	mii->mii_media_status = IFM_AVALID;
    238 	mii->mii_media_active = IFM_ETHER;
    239 
    240 	esr = PHY_READ(sc, IHPHY_MII_ESR);
    241 
    242 	if (esr & IHPHY_ESR_LINK)
    243 		mii->mii_media_status |= IFM_ACTIVE;
    244 
    245 	bmcr = PHY_READ(sc, MII_BMCR);
    246 	if (bmcr & (BMCR_ISO | BMCR_PDOWN)) {
    247 		mii->mii_media_active |= IFM_NONE;
    248 		mii->mii_media_status = 0;
    249 		return;
    250 	}
    251 
    252 	if (bmcr & BMCR_LOOP)
    253 		mii->mii_media_active |= IFM_LOOP;
    254 
    255 	if (bmcr & BMCR_AUTOEN) {
    256 		if ((esr & IHPHY_ESR_ANEG_STAT) == 0) {
    257 			/* Erg, still trying, I guess... */
    258 			mii->mii_media_active |= IFM_NONE;
    259 			return;
    260 		}
    261 	}
    262 
    263 	switch (esr & IHPHY_ESR_SPEED) {
    264 	case IHPHY_SPEED_1000:
    265 		mii->mii_media_active |= IFM_1000_T;
    266 		gtsr = PHY_READ(sc, MII_100T2SR);
    267 		if (gtsr & GTSR_MS_RES)
    268 			mii->mii_media_active |= IFM_ETH_MASTER;
    269 		break;
    270 
    271 	case IHPHY_SPEED_100:
    272 		/* 100BASE-T2 and 100BASE-T4 are not supported. */
    273 		mii->mii_media_active |= IFM_100_TX;
    274 		break;
    275 
    276 	case IHPHY_SPEED_10:
    277 		mii->mii_media_active |= IFM_10_T;
    278 		break;
    279 
    280 	default:
    281 		mii->mii_media_active |= IFM_NONE;
    282 		mii->mii_media_status = 0;
    283 		return;
    284 	}
    285 
    286 	if (esr & IHPHY_ESR_DUPLEX)
    287 		mii->mii_media_active |= IFM_FDX | mii_phy_flowstatus(sc);
    288 	else
    289 		mii->mii_media_active |= IFM_HDX;
    290 }
    291 
    292 static void
    293 ihphy_reset(struct mii_softc *sc)
    294 {
    295 	int reg, i;
    296 
    297 	PHY_WRITE(sc, MII_BMCR, BMCR_RESET | BMCR_ISO);
    298 
    299 	/*
    300 	 * Regarding reset, the data sheet specifies (page 55):
    301 	 *
    302 	 * "After PHY reset, a delay of 10 ms is required before
    303 	 *  any register access using MDIO."
    304 	 */
    305 	delay(10000);
    306 
    307 	/* Wait another 100ms for it to complete. */
    308 	for (i = 0; i < 100; i++) {
    309 		reg = PHY_READ(sc, MII_BMCR);
    310 		if ((reg & BMCR_RESET) == 0)
    311 			break;
    312 		delay(1000);
    313 	}
    314 
    315 	if (sc->mii_inst != 0)
    316 		PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
    317 }
    318