Home | History | Annotate | Line # | Download | only in mii
makphy.c revision 1.67
      1 /*	$NetBSD: makphy.c,v 1.67 2020/10/20 08:53:34 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 the Marvell 88E1000 ``Alaska'' 10/100/1000 PHY.
     59  */
     60 
     61 #include <sys/cdefs.h>
     62 __KERNEL_RCSID(0, "$NetBSD: makphy.c,v 1.67 2020/10/20 08:53:34 msaitoh Exp $");
     63 
     64 #include <sys/param.h>
     65 #include <sys/systm.h>
     66 #include <sys/kernel.h>
     67 #include <sys/device.h>
     68 #include <sys/socket.h>
     69 #include <sys/errno.h>
     70 
     71 #include <net/if.h>
     72 #include <net/if_media.h>
     73 
     74 #include <dev/mii/mii.h>
     75 #include <dev/mii/miivar.h>
     76 #include <dev/mii/miidevs.h>
     77 
     78 #include <dev/mii/makphyreg.h>
     79 #include <dev/mii/makphyvar.h>
     80 
     81 static int	makphymatch(device_t, cfdata_t, void *);
     82 static void	makphyattach(device_t, device_t, void *);
     83 
     84 CFATTACH_DECL_NEW(makphy, sizeof(struct makphy_softc),
     85     makphymatch, makphyattach, mii_phy_detach, mii_phy_activate);
     86 
     87 static int	makphy_service(struct mii_softc *, struct mii_data *, int);
     88 static void	makphy_status(struct mii_softc *);
     89 static void	makphy_reset(struct mii_softc *);
     90 
     91 static const struct mii_phy_funcs makphy_funcs = {
     92 	makphy_service, makphy_status, makphy_reset,
     93 };
     94 
     95 static const struct mii_phydesc makphys[] = {
     96 	MII_PHY_DESC(MARVELL, E1000_0),
     97 	MII_PHY_DESC(MARVELL, E1000_3),
     98 	MII_PHY_DESC(MARVELL, E1000_5),
     99 	MII_PHY_DESC(MARVELL, E1000_6),
    100 	MII_PHY_DESC(xxMARVELL, E1000_3),
    101 	MII_PHY_DESC(xxMARVELL, E1000_5),
    102 	MII_PHY_DESC(xxMARVELL, E1000S),
    103 	MII_PHY_DESC(xxMARVELL, E1011),
    104 	MII_PHY_DESC(xxMARVELL, E1101),
    105 	MII_PHY_DESC(xxMARVELL, E1111),
    106 	MII_PHY_DESC(xxMARVELL, E1112),
    107 	MII_PHY_DESC(xxMARVELL, E1116),
    108 	MII_PHY_DESC(xxMARVELL, E1116R),
    109 	MII_PHY_DESC(xxMARVELL, E1118),
    110 	MII_PHY_DESC(xxMARVELL, E1145),
    111 	MII_PHY_DESC(xxMARVELL, E1149),
    112 	MII_PHY_DESC(xxMARVELL, E1149R),
    113 	MII_PHY_DESC(xxMARVELL, E1240),
    114 	MII_PHY_DESC(xxMARVELL, E1318S),
    115 	MII_PHY_DESC(xxMARVELL, E1512),
    116 	MII_PHY_DESC(xxMARVELL, E1543),
    117 	MII_PHY_DESC(xxMARVELL, E3016),
    118 	MII_PHY_DESC(xxMARVELL, E3082),
    119 	MII_PHY_DESC(xxMARVELL, PHYG65G),
    120 	MII_PHY_DESC(xxMARVELL, I347),
    121 	MII_PHY_END,
    122 };
    123 
    124 #define MAKARG_PDOWN	true	/* Power DOWN */
    125 #define MAKARG_PUP	false	/* Power UP */
    126 
    127 static bool
    128 makphy_isi210(device_t parent, struct mii_attach_args *ma)
    129 {
    130 
    131 	/* I21[01]'s model number is 0 */
    132 	if ((MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_xxMARVELL)
    133 	    && (MII_MODEL(ma->mii_id2) == MII_MODEL_xxMARVELL_I210)
    134 	    && (device_is_a(parent, "wm")))
    135 		return true;
    136 	return false;
    137 }
    138 
    139 static int
    140 makphymatch(device_t parent, cfdata_t match, void *aux)
    141 {
    142 	struct mii_attach_args *ma = aux;
    143 
    144 	if (mii_phy_match(ma, makphys) != NULL)
    145 		return 10;
    146 
    147 	if (makphy_isi210(parent, ma))
    148 		return 10;
    149 
    150 	return 0;
    151 }
    152 
    153 static void
    154 makphyattach(device_t parent, device_t self, void *aux)
    155 {
    156 	struct mii_softc *sc = device_private(self);
    157 	struct mii_attach_args *ma = aux;
    158 	struct mii_data *mii = ma->mii_data;
    159 	const struct mii_phydesc *mpd;
    160 	struct makphy_softc *maksc = (struct makphy_softc *)sc;
    161 	const char *name;
    162 	uint16_t reg, model;
    163 
    164 	mpd = mii_phy_match(ma, makphys);
    165 	aprint_naive(": Media interface\n");
    166 	if (mpd)
    167 		name = mpd->mpd_name;
    168 	else if (makphy_isi210(parent, ma)) {
    169 		name = MII_STR_xxMARVELL_I210;
    170 		maksc->sc_flags |= MAKPHY_F_I210;
    171 	} else
    172 		panic("Unknown PHY");
    173 	aprint_normal(": %s, rev. %d\n", name, MII_REV(ma->mii_id2));
    174 
    175 	sc->mii_dev = self;
    176 	sc->mii_mpd_oui = MII_OUI(ma->mii_id1, ma->mii_id2);
    177 	sc->mii_mpd_model = model = MII_MODEL(ma->mii_id2);
    178 	sc->mii_mpd_rev = MII_REV(ma->mii_id2);
    179 	sc->mii_inst = mii->mii_instance;
    180 	sc->mii_phy = ma->mii_phyno;
    181 	sc->mii_funcs = &makphy_funcs;
    182 	sc->mii_pdata = mii;
    183 	sc->mii_flags = ma->mii_flags;
    184 
    185 	mii_lock(mii);
    186 
    187 	switch (model) {
    188 	case MII_MODEL_xxMARVELL_E1000:
    189 		if ((maksc->sc_flags & MAKPHY_F_I210) != 0)
    190 			goto page0;
    191 		/* FALLTHROUGH */
    192 	case MII_MODEL_xxMARVELL_E1000_3:
    193 	case MII_MODEL_xxMARVELL_E1000S:
    194 	case MII_MODEL_xxMARVELL_E1000_5:
    195 		/* 88E1000 series has no EADR */
    196 		break;
    197 	default:
    198 page0:
    199 		/* Make sure page 0 is selected. */
    200 		if (PHY_WRITE(sc, MAKPHY_EADR, 0) != 0)
    201 			aprint_verbose_dev(self,
    202 			    "Failed to access EADR. Are you an emulator?\n");
    203 		break;
    204 	}
    205 
    206 	PHY_RESET(sc);
    207 
    208 	PHY_READ(sc, MII_BMSR, &sc->mii_capabilities);
    209 	sc->mii_capabilities &= ma->mii_capmask;
    210 	if (sc->mii_capabilities & BMSR_EXTSTAT)
    211 		PHY_READ(sc, MII_EXTSR, &sc->mii_extcapabilities);
    212 
    213 	if (((sc->mii_extcapabilities & (EXTSR_1000TFDX | EXTSR_1000THDX))
    214 		!= 0)
    215 	    && ((sc->mii_extcapabilities & (EXTSR_1000XFDX | EXTSR_1000XHDX))
    216 		!= 0)) {
    217 		bool fiberonly = false, copperonly = false;
    218 
    219 		/* Both copper and fiber are set. check MODE[] */
    220 		switch (sc->mii_mpd_model) {
    221 		case MII_MODEL_xxMARVELL_E1011:
    222 		case MII_MODEL_xxMARVELL_E1111:
    223 			/* These devices have ESSR register */
    224 			PHY_READ(sc, MAKPHY_ESSR, &reg);
    225 			if ((reg & ESSR_AUTOSEL_DISABLE) != 0) {
    226 				switch (reg & ESSR_HWCFG_MODE) {
    227 				case ESSR_RTBI_FIBER:
    228 				case ESSR_RGMII_FIBER:
    229 				case ESSR_RGMII_SGMII: /* right? */
    230 				case ESSR_TBI_FIBER:
    231 				case ESSR_GMII_FIBER:
    232 					fiberonly = true;
    233 					break;
    234 				case ESSR_SGMII_WC_COPPER:
    235 				case ESSR_SGMII_WOC_COPPER:
    236 				case ESSR_RTBI_COPPER:
    237 				case ESSR_RGMII_COPPER:
    238 				case ESSR_GMII_COPPER:
    239 					copperonly = true;
    240 				default:
    241 					break;
    242 				}
    243 			}
    244 			break;
    245 		default:
    246 			break;
    247 		}
    248 		if (fiberonly || copperonly)
    249 			aprint_debug_dev(self, "both copper and fiber are set "
    250 			    "but MODE[] is %s only.\n",
    251 			    fiberonly ? "fiber" : "copper");
    252 		if (fiberonly)
    253 			sc->mii_extcapabilities
    254 			    &= ~(EXTSR_1000TFDX | EXTSR_1000THDX);
    255 		else if (copperonly) {
    256 			sc->mii_extcapabilities
    257 			    &= ~(EXTSR_1000XFDX | EXTSR_1000XHDX);
    258 			sc->mii_flags &= ~MIIF_IS_1000X;
    259 		}
    260 	}
    261 	mii_unlock(mii);
    262 	mii_phy_add_media(sc);
    263 }
    264 
    265 static void
    266 makphy_reset(struct mii_softc *sc)
    267 {
    268 	struct makphy_softc *maksc = (struct makphy_softc *)sc;
    269 	uint16_t reg;
    270 
    271 	KASSERT(mii_locked(sc->mii_pdata));
    272 
    273 	mii_phy_reset(sc);
    274 
    275 	/* Initialize PHY Specific Control Register. */
    276 	PHY_READ(sc, MAKPHY_PSCR, &reg);
    277 
    278 	/* Assert CRS on transmit. */
    279 	switch (sc->mii_mpd_model) {
    280 	case MII_MODEL_MARVELL_E1000_0:
    281 		if ((maksc->sc_flags & MAKPHY_F_I210) != 0)
    282 			break;
    283 		/* FALLTHROUGH */
    284 	case MII_MODEL_MARVELL_E1000_3:
    285 	case MII_MODEL_MARVELL_E1000_5:
    286 	case MII_MODEL_MARVELL_E1000_6:
    287 	case MII_MODEL_xxMARVELL_E1000S:
    288 	case MII_MODEL_xxMARVELL_E1011:
    289 	case MII_MODEL_xxMARVELL_E1111:
    290 		reg |= PSCR_CRS_ON_TX;
    291 		break;
    292 	default: /* No PSCR_CRS_ON_TX bit */
    293 		break;
    294 	}
    295 
    296 	/* Enable scrambler if necessary. */
    297 	if (sc->mii_mpd_model == MII_MODEL_xxMARVELL_E3016)
    298 		reg &= ~E3016_PSCR_SCRAMBLE_DIS;
    299 
    300 	/*
    301 	 * Store next page in the Link Partner Next Page register for
    302 	 * compatibility with 802.3ab.
    303 	 */
    304 	if (sc->mii_mpd_model == MII_MODEL_xxMARVELL_E3016)
    305 		reg |= E3016_PSCR_REG8NXTPG;
    306 
    307 	PHY_WRITE(sc, MAKPHY_PSCR, reg);
    308 
    309 	/* Configure LEDs if they were left unconfigured. */
    310 	if (sc->mii_mpd_model == MII_MODEL_xxMARVELL_E3016) {
    311 		PHY_READ(sc, 0x16, &reg);
    312 		if (reg == 0) {
    313 			reg = (0x0b << 8) | (0x05 << 4) | 0x04;	/* XXX */
    314 			PHY_WRITE(sc, 0x16, reg);
    315 		}
    316 	}
    317 
    318 	mii_phy_reset(sc);
    319 }
    320 
    321 static void
    322 makphy_pdown(struct mii_softc *sc, bool pdown)
    323 {
    324 	uint16_t bmcr, new;
    325 	bool need_reset = false;
    326 
    327 	/*
    328 	 * XXX
    329 	 * PSCR (register 16) should be modified on some chips.
    330 	 */
    331 
    332 	PHY_READ(sc, MII_BMCR, &bmcr);
    333 	if (pdown)
    334 		new = bmcr | BMCR_PDOWN;
    335 	else
    336 		new = bmcr & ~BMCR_PDOWN;
    337 	if (bmcr != new)
    338 		need_reset = true;
    339 
    340 	if (need_reset)
    341 		new |= BMCR_RESET;
    342 	PHY_WRITE(sc, MII_BMCR, new);
    343 }
    344 
    345 static int
    346 makphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
    347 {
    348 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
    349 	uint16_t bmcr;
    350 
    351 	if (!device_is_active(sc->mii_dev))
    352 		return ENXIO;
    353 
    354 	KASSERT(mii_locked(mii));
    355 
    356 	switch (cmd) {
    357 	case MII_POLLSTAT:
    358 		/* If we're not polling our PHY instance, just return. */
    359 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
    360 			return 0;
    361 		break;
    362 
    363 	case MII_MEDIACHG:
    364 		/*
    365 		 * If the media indicates a different PHY instance,
    366 		 * isolate ourselves.
    367 		 */
    368 		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
    369 			PHY_READ(sc, MII_BMCR, &bmcr);
    370 			PHY_WRITE(sc, MII_BMCR, bmcr | BMCR_ISO);
    371 			return 0;
    372 		}
    373 
    374 		/* If the interface is not up, don't do anything. */
    375 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
    376 			break;
    377 
    378 		/* Try to power up the PHY in case it's down */
    379 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_NONE)
    380 			makphy_pdown(sc, MAKARG_PUP);
    381 
    382 		mii_phy_setmedia(sc);
    383 
    384 		/*
    385 		 * If autonegitation is not enabled, we need a
    386 		 * software reset for the settings to take effect.
    387 		 */
    388 		if (IFM_SUBTYPE(ife->ifm_media) == IFM_NONE)
    389 			makphy_pdown(sc, MAKARG_PDOWN);
    390 		else if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) {
    391 			PHY_READ(sc, MII_BMCR, &bmcr);
    392 			PHY_WRITE(sc, MII_BMCR, bmcr | BMCR_RESET);
    393 		}
    394 		break;
    395 
    396 	case MII_TICK:
    397 		/* If we're not currently selected, just return. */
    398 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
    399 			return 0;
    400 
    401 		if (mii_phy_tick(sc) == EJUSTRETURN)
    402 			return 0;
    403 		break;
    404 
    405 	case MII_DOWN:
    406 		mii_phy_down(sc);
    407 		return 0;
    408 	}
    409 
    410 	/* Update the media status. */
    411 	mii_phy_status(sc);
    412 
    413 	/* Callback if something changed. */
    414 	mii_phy_update(sc, cmd);
    415 	return 0;
    416 }
    417 
    418 static void
    419 makphy_status(struct mii_softc *sc)
    420 {
    421 	struct mii_data *mii = sc->mii_pdata;
    422 	uint16_t bmcr, gsr, pssr, essr;
    423 
    424 	KASSERT(mii_locked(mii));
    425 
    426 	mii->mii_media_status = IFM_AVALID;
    427 	mii->mii_media_active = IFM_ETHER;
    428 
    429 	PHY_READ(sc, MII_BMCR, &bmcr);
    430 	/* XXX FIXME: Use different page for Fiber on newer chips */
    431 	PHY_READ(sc, MAKPHY_PSSR, &pssr);
    432 
    433 	if (pssr & MAKPHY_PSSR_LINK)
    434 		mii->mii_media_status |= IFM_ACTIVE;
    435 
    436 	if (bmcr & BMCR_LOOP)
    437 		mii->mii_media_active |= IFM_LOOP;
    438 
    439 	if (bmcr & BMCR_ISO) {
    440 		mii->mii_media_active |= IFM_NONE;
    441 		mii->mii_media_status = 0;
    442 		return;
    443 	}
    444 
    445 	if ((bmcr & BMCR_AUTOEN) != 0) {
    446 		/*
    447 		 * Check Speed and Duplex Resolved bit.
    448 		 * Note that this bit is always 1 when autonego is not enabled.
    449 		 */
    450 		if (!(pssr & MAKPHY_PSSR_RESOLVED)) {
    451 			/* Erg, still trying, I guess... */
    452 			mii->mii_media_active |= IFM_NONE;
    453 			return;
    454 		}
    455 	} else {
    456 		if ((pssr & MAKPHY_PSSR_LINK) == 0) {
    457 			mii->mii_media_active |= IFM_NONE;
    458 			return;
    459 		}
    460 	}
    461 
    462 	/*
    463 	 * XXX The following code support Fiber/Copper auto select mode
    464 	 * only for 88E1011, 88E1111 and 88E1112. For other chips, the document
    465 	 * is required.
    466 	 */
    467 	if (sc->mii_flags & MIIF_IS_1000X) {
    468 		/* Not in Fiber/Copper auto select mode */
    469 		mii->mii_media_active |= IFM_1000_SX;
    470 	} else if ((sc->mii_mpd_model == MII_MODEL_xxMARVELL_E1011) ||
    471 	    (sc->mii_mpd_model == MII_MODEL_xxMARVELL_E1111)) {
    472 		/* Fiber/Copper auto select mode */
    473 
    474 		PHY_READ(sc, MAKPHY_ESSR, &essr);
    475 		if ((essr & ESSR_FIBER_LINK) == 0)
    476 			goto copper;
    477 
    478 		/* XXX Assume 1000BASE-SX only */
    479 		mii->mii_media_active |= IFM_1000_SX;
    480 	} else if (sc->mii_mpd_model == MII_MODEL_xxMARVELL_E1112) {
    481 		/* Fiber/Copper auto select mode */
    482 
    483 		PHY_READ(sc, MAKPHY_PSSR, &pssr);
    484 		if ((pssr & MAKPHY_PSSR_RESOLUTION_FIBER) == 0)
    485 			goto copper;
    486 
    487 		switch (MAKPHY_PSSR_SPEED_get(pssr)) {
    488 		case SPEED_1000:
    489 			mii->mii_media_active |= IFM_1000_SX;
    490 			break;
    491 		case SPEED_100:
    492 			mii->mii_media_active |= IFM_100_FX;
    493 			break;
    494 		default: /* Undefined (reserved) value */
    495 			mii->mii_media_active |= IFM_NONE;
    496 			mii->mii_media_status = 0;
    497 			return;
    498 		}
    499 	} else {
    500 copper:
    501 		switch (MAKPHY_PSSR_SPEED_get(pssr)) {
    502 		case SPEED_1000:
    503 			mii->mii_media_active |= IFM_1000_T;
    504 			break;
    505 		case SPEED_100:
    506 			mii->mii_media_active |= IFM_100_TX;
    507 			break;
    508 		case SPEED_10:
    509 			mii->mii_media_active |= IFM_10_T;
    510 			break;
    511 		default: /* Undefined (reserved) value */
    512 			mii->mii_media_active |= IFM_NONE;
    513 			mii->mii_media_status = 0;
    514 			return;
    515 		}
    516 	}
    517 
    518 	if (pssr & MAKPHY_PSSR_DUPLEX)
    519 		mii->mii_media_active |= mii_phy_flowstatus(sc) | IFM_FDX;
    520 	else
    521 		mii->mii_media_active |= IFM_HDX;
    522 
    523 	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T) {
    524 		PHY_READ(sc, MII_100T2SR, &gsr);
    525 		if (gsr & GTSR_MS_RES)
    526 			mii->mii_media_active |= IFM_ETH_MASTER;
    527 	}
    528 }
    529