Home | History | Annotate | Line # | Download | only in mii
makphy.c revision 1.42.8.3
      1 /*	$NetBSD: makphy.c,v 1.42.8.3 2019/08/01 14:27:30 martin 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.42.8.3 2019/08/01 14:27:30 martin 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_OUI_MARVELL,		MII_MODEL_MARVELL_E1000_0,
     97 	  MII_STR_MARVELL_E1000_0 },
     98 
     99 	{ MII_OUI_MARVELL,		MII_MODEL_MARVELL_E1000_3,
    100 	  MII_STR_MARVELL_E1000_3 },
    101 
    102 	{ MII_OUI_MARVELL,		MII_MODEL_MARVELL_E1000_5,
    103 	  MII_STR_MARVELL_E1000_5 },
    104 
    105 	{ MII_OUI_MARVELL,		MII_MODEL_MARVELL_E1000_6,
    106 	  MII_STR_MARVELL_E1000_6 },
    107 
    108 	{ MII_OUI_xxMARVELL,		MII_MODEL_xxMARVELL_E1000_3,
    109 	  MII_STR_xxMARVELL_E1000_3 },
    110 
    111 	{ MII_OUI_xxMARVELL,		MII_MODEL_xxMARVELL_E1000_5,
    112 	  MII_STR_xxMARVELL_E1000_5 },
    113 
    114 	{ MII_OUI_xxMARVELL,		MII_MODEL_xxMARVELL_E1000S,
    115 	  MII_STR_xxMARVELL_E1000S },
    116 
    117 	{ MII_OUI_xxMARVELL,		MII_MODEL_xxMARVELL_E1011,
    118 	  MII_STR_xxMARVELL_E1011 },
    119 
    120 	{ MII_OUI_xxMARVELL,		MII_MODEL_xxMARVELL_E1111,
    121 	  MII_STR_xxMARVELL_E1111 },
    122 
    123 	{ MII_OUI_xxMARVELL,		MII_MODEL_xxMARVELL_E1112,
    124 	  MII_STR_xxMARVELL_E1112 },
    125 
    126 	{ MII_OUI_xxMARVELL,		MII_MODEL_xxMARVELL_E1116,
    127 	  MII_STR_xxMARVELL_E1116 },
    128 
    129 	{ MII_OUI_xxMARVELL,		MII_MODEL_xxMARVELL_E1116R,
    130 	  MII_STR_xxMARVELL_E1116R },
    131 
    132 	{ MII_OUI_xxMARVELL,		MII_MODEL_xxMARVELL_E1318S,
    133 	  MII_STR_xxMARVELL_E1318S },
    134 
    135 	{ MII_OUI_xxMARVELL,		MII_MODEL_xxMARVELL_E1118,
    136 	  MII_STR_xxMARVELL_E1118 },
    137 
    138 	{ MII_OUI_xxMARVELL,		MII_MODEL_xxMARVELL_E1145,
    139 	  MII_STR_xxMARVELL_E1145 },
    140 
    141 	{ MII_OUI_xxMARVELL,		MII_MODEL_xxMARVELL_E1149,
    142 	  MII_STR_xxMARVELL_E1149 },
    143 
    144 	{ MII_OUI_xxMARVELL,		MII_MODEL_xxMARVELL_E1149R,
    145 	  MII_STR_xxMARVELL_E1149R },
    146 
    147 	{ MII_OUI_xxMARVELL,		MII_MODEL_xxMARVELL_E1240,
    148 	  MII_STR_xxMARVELL_E1240 },
    149 
    150 	{ MII_OUI_xxMARVELL,		MII_MODEL_xxMARVELL_E1512,
    151 	  MII_STR_xxMARVELL_E1512 },
    152 
    153 	{ MII_OUI_xxMARVELL,		MII_MODEL_xxMARVELL_E1543,
    154 	  MII_STR_xxMARVELL_E1543 },
    155 
    156 	{ MII_OUI_xxMARVELL,		MII_MODEL_xxMARVELL_E3016,
    157 	  MII_STR_xxMARVELL_E3016 },
    158 
    159 	{ MII_OUI_xxMARVELL,		MII_MODEL_xxMARVELL_E3082,
    160 	  MII_STR_xxMARVELL_E3082 },
    161 
    162 	{ MII_OUI_xxMARVELL,		MII_MODEL_xxMARVELL_PHYG65G,
    163 	  MII_STR_xxMARVELL_PHYG65G },
    164 
    165 	{ 0,				0,
    166 	  NULL },
    167 };
    168 
    169 #define MAKARG_PDOWN	true	/* Power DOWN */
    170 #define MAKARG_PUP	false	/* Power UP */
    171 
    172 static bool
    173 makphy_isi210(device_t parent, struct mii_attach_args *ma)
    174 {
    175 
    176 	/* I21[01]'s model number is 0 */
    177 	if ((MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_xxMARVELL)
    178 	    && (MII_MODEL(ma->mii_id2) == MII_MODEL_xxMARVELL_I210)
    179 	    && (device_is_a(parent, "wm")))
    180 		return true;
    181 	return false;
    182 }
    183 
    184 static int
    185 makphymatch(device_t parent, cfdata_t match, void *aux)
    186 {
    187 	struct mii_attach_args *ma = aux;
    188 
    189 	if (mii_phy_match(ma, makphys) != NULL)
    190 		return 10;
    191 
    192 	if (makphy_isi210(parent, ma))
    193 		return 10;
    194 
    195 	return 0;
    196 }
    197 
    198 static void
    199 makphyattach(device_t parent, device_t self, void *aux)
    200 {
    201 	struct mii_softc *sc = device_private(self);
    202 	struct mii_attach_args *ma = aux;
    203 	struct mii_data *mii = ma->mii_data;
    204 	const struct mii_phydesc *mpd;
    205 	struct makphy_softc *maksc = (struct makphy_softc *)sc;
    206 	const char *name;
    207 	int model, val;
    208 
    209 	mpd = mii_phy_match(ma, makphys);
    210 	aprint_naive(": Media interface\n");
    211 	if (mpd)
    212 		name = mpd->mpd_name;
    213 	else if (makphy_isi210(parent, ma)) {
    214 		name = MII_STR_xxMARVELL_I210;
    215 		maksc->sc_flags |= MAKPHY_F_I210;
    216 	} else
    217 		panic("Unknown PHY");
    218 	aprint_normal(": %s, rev. %d\n", name, MII_REV(ma->mii_id2));
    219 
    220 	sc->mii_dev = self;
    221 	sc->mii_mpd_oui = MII_OUI(ma->mii_id1, ma->mii_id2);
    222 	sc->mii_mpd_model = model = MII_MODEL(ma->mii_id2);
    223 	sc->mii_mpd_rev = MII_REV(ma->mii_id2);
    224 	sc->mii_inst = mii->mii_instance;
    225 	sc->mii_phy = ma->mii_phyno;
    226 	sc->mii_funcs = &makphy_funcs;
    227 	sc->mii_pdata = mii;
    228 	sc->mii_flags = ma->mii_flags;
    229 	sc->mii_anegticks = MII_ANEGTICKS;
    230 
    231 	switch (model) {
    232 	case MII_MODEL_xxMARVELL_E1000:
    233 		if ((maksc->sc_flags & MAKPHY_F_I210) != 0)
    234 			goto page0;
    235 		/* FALLTHROUGH */
    236 	case MII_MODEL_xxMARVELL_E1000_3:
    237 	case MII_MODEL_xxMARVELL_E1000S:
    238 	case MII_MODEL_xxMARVELL_E1000_5:
    239 		/* 88E1000 series has no EADR */
    240 		break;
    241 	default:
    242 page0:
    243 		/* Make sure page 0 is selected. */
    244 		PHY_WRITE(sc, MAKPHY_EADR, 0);
    245 		break;
    246 	}
    247 
    248 	switch (model) {
    249 	case MII_MODEL_xxMARVELL_E1011:
    250 	case MII_MODEL_xxMARVELL_E1112:
    251 		val = PHY_READ(sc, MAKPHY_ESSR);
    252 		if ((val != 0) && (((u_int)val & 0x0000ffffU) != 0x0000ffffU)
    253 		    && ((val & ESSR_FIBER_LINK) != 0))
    254 			sc->mii_flags |= MIIF_HAVEFIBER;
    255 		break;
    256 	default:
    257 		break;
    258 	}
    259 
    260 	PHY_RESET(sc);
    261 
    262 	sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
    263 	if (sc->mii_capabilities & BMSR_EXTSTAT)
    264 		sc->mii_extcapabilities = PHY_READ(sc, MII_EXTSR);
    265 
    266 	aprint_normal_dev(self, "");
    267 	if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0 &&
    268 	    (sc->mii_extcapabilities & EXTSR_MEDIAMASK) == 0)
    269 		aprint_error("no media present");
    270 	else
    271 		mii_phy_add_media(sc);
    272 	aprint_normal("\n");
    273 }
    274 
    275 static void
    276 makphy_reset(struct mii_softc *sc)
    277 {
    278 	struct makphy_softc *maksc = (struct makphy_softc *)sc;
    279 	uint16_t reg;
    280 
    281 	mii_phy_reset(sc);
    282 
    283 	/*
    284 	 * Initialize PHY Specific Control Register.
    285 	 */
    286 	reg = PHY_READ(sc, MAKPHY_PSCR);
    287 
    288 	/* Assert CRS on transmit. */
    289 	switch (sc->mii_mpd_model) {
    290 	case MII_MODEL_MARVELL_E1000_0:
    291 		if ((maksc->sc_flags & MAKPHY_F_I210) != 0)
    292 			break;
    293 		/* FALLTHROUGH */
    294 	case MII_MODEL_MARVELL_E1000_3:
    295 	case MII_MODEL_MARVELL_E1000_5:
    296 	case MII_MODEL_MARVELL_E1000_6:
    297 	case MII_MODEL_xxMARVELL_E1000S:
    298 	case MII_MODEL_xxMARVELL_E1011:
    299 	case MII_MODEL_xxMARVELL_E1111:
    300 		reg |= PSCR_CRS_ON_TX;
    301 		break;
    302 	default: /* No PSCR_CRS_ON_TX bit */
    303 		break;
    304 	}
    305 
    306 	/* Enable scrambler if necessary. */
    307 	if (sc->mii_mpd_model == MII_MODEL_xxMARVELL_E3016)
    308 		reg &= ~E3016_PSCR_SCRAMBLE_DIS;
    309 
    310 	/*
    311 	 * Store next page in the Link Partner Next Page register for
    312 	 * compatibility with 802.3ab.
    313 	 */
    314 	if (sc->mii_mpd_model == MII_MODEL_xxMARVELL_E3016)
    315 		reg |= E3016_PSCR_REG8NXTPG;
    316 
    317 	PHY_WRITE(sc, MAKPHY_PSCR, reg);
    318 
    319 	/* Configure LEDs if they were left unconfigured. */
    320 	if (sc->mii_mpd_model == MII_MODEL_xxMARVELL_E3016 &&
    321 	    PHY_READ(sc, 0x16) == 0) {
    322 		reg = (0x0b << 8) | (0x05 << 4) | 0x04;	/* XXX */
    323 		PHY_WRITE(sc, 0x16, reg);
    324 	}
    325 
    326 	mii_phy_reset(sc);
    327 }
    328 
    329 static void
    330 makphy_pdown(struct mii_softc *sc, bool pdown)
    331 {
    332 	int bmcr, new;
    333 	bool need_reset = false;
    334 
    335 	/*
    336 	 * XXX
    337 	 * PSCR (register 16) should be modified on some chips.
    338 	 */
    339 
    340 	bmcr = PHY_READ(sc, MII_BMCR);
    341 	if (pdown)
    342 		new = bmcr | BMCR_PDOWN;
    343 	else
    344 		new = bmcr & ~BMCR_PDOWN;
    345 	if (bmcr != new)
    346 		need_reset = true;
    347 
    348 	if (need_reset)
    349 		new |= BMCR_RESET;
    350 	PHY_WRITE(sc, MII_BMCR, new);
    351 }
    352 
    353 static int
    354 makphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
    355 {
    356 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
    357 	int bmcr;
    358 
    359 	if (!device_is_active(sc->mii_dev))
    360 		return ENXIO;
    361 
    362 	switch (cmd) {
    363 	case MII_POLLSTAT:
    364 		/* If we're not polling our PHY instance, just return. */
    365 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
    366 			return 0;
    367 		break;
    368 
    369 	case MII_MEDIACHG:
    370 		/*
    371 		 * If the media indicates a different PHY instance,
    372 		 * isolate ourselves.
    373 		 */
    374 		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
    375 			bmcr = PHY_READ(sc, MII_BMCR);
    376 			PHY_WRITE(sc, MII_BMCR, bmcr | BMCR_ISO);
    377 			return 0;
    378 		}
    379 
    380 		/* If the interface is not up, don't do anything. */
    381 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
    382 			break;
    383 
    384 		/* Try to power up the PHY in case it's down */
    385 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_NONE)
    386 			makphy_pdown(sc, MAKARG_PUP);
    387 
    388 		mii_phy_setmedia(sc);
    389 
    390 		/*
    391 		 * If autonegitation is not enabled, we need a
    392 		 * software reset for the settings to take effect.
    393 		 */
    394 		if (IFM_SUBTYPE(ife->ifm_media) == IFM_NONE)
    395 			makphy_pdown(sc, MAKARG_PDOWN);
    396 		else if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) {
    397 			bmcr = PHY_READ(sc, MII_BMCR);
    398 			PHY_WRITE(sc, MII_BMCR, bmcr | BMCR_RESET);
    399 		}
    400 		break;
    401 
    402 	case MII_TICK:
    403 		/* If we're not currently selected, just return. */
    404 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
    405 			return 0;
    406 
    407 		if (mii_phy_tick(sc) == EJUSTRETURN)
    408 			return 0;
    409 		break;
    410 
    411 	case MII_DOWN:
    412 		mii_phy_down(sc);
    413 		return 0;
    414 	}
    415 
    416 	/* Update the media status. */
    417 	mii_phy_status(sc);
    418 
    419 	/* Callback if something changed. */
    420 	mii_phy_update(sc, cmd);
    421 	return 0;
    422 }
    423 
    424 static void
    425 makphy_status(struct mii_softc *sc)
    426 {
    427 	struct mii_data *mii = sc->mii_pdata;
    428 	int bmcr, gsr, pssr;
    429 
    430 	mii->mii_media_status = IFM_AVALID;
    431 	mii->mii_media_active = IFM_ETHER;
    432 
    433 	bmcr = PHY_READ(sc, MII_BMCR);
    434 	/* XXX FIXME: Use different page for Fiber on newer chips */
    435 	pssr = PHY_READ(sc, MAKPHY_PSSR);
    436 
    437 	if (pssr & PSSR_LINK)
    438 		mii->mii_media_status |= IFM_ACTIVE;
    439 
    440 	if (bmcr & BMCR_LOOP)
    441 		mii->mii_media_active |= IFM_LOOP;
    442 
    443 	if (bmcr & BMCR_ISO) {
    444 		mii->mii_media_active |= IFM_NONE;
    445 		mii->mii_media_status = 0;
    446 		return;
    447 	}
    448 
    449 	if ((bmcr & BMCR_AUTOEN) != 0) {
    450 		/*
    451 		 * Check Speed and Duplex Resolved bit.
    452 		 * Note that this bit is always 1 when autonego is not enabled.
    453 		 */
    454 		if (!(pssr & PSSR_RESOLVED)) {
    455 			/* Erg, still trying, I guess... */
    456 			mii->mii_media_active |= IFM_NONE;
    457 			return;
    458 		}
    459 	} else {
    460 		if ((pssr & PSSR_LINK) == 0) {
    461 			mii->mii_media_active |= IFM_NONE;
    462 			return;
    463 		}
    464 	}
    465 
    466 	/* XXX FIXME: Use different page for Fiber on newer chips */
    467 	if (sc->mii_flags & MIIF_IS_1000X) {
    468 		mii->mii_media_active |= IFM_1000_SX;
    469 	} else {
    470 		switch (PSSR_SPEED_get(pssr)) {
    471 		case SPEED_1000:
    472 			mii->mii_media_active |= IFM_1000_T;
    473 			break;
    474 		case SPEED_100:
    475 			mii->mii_media_active |= IFM_100_TX;
    476 			break;
    477 		case SPEED_10:
    478 			mii->mii_media_active |= IFM_10_T;
    479 			break;
    480 		default: /* Undefined (reserved) value */
    481 			mii->mii_media_active |= IFM_NONE;
    482 			mii->mii_media_status = 0;
    483 			return;
    484 		}
    485 	}
    486 
    487 	if (pssr & PSSR_DUPLEX)
    488 		mii->mii_media_active |= mii_phy_flowstatus(sc) | IFM_FDX;
    489 	else
    490 		mii->mii_media_active |= IFM_HDX;
    491 
    492 	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T) {
    493 		gsr = PHY_READ(sc, MII_100T2SR);
    494 		if (gsr & GTSR_MS_RES)
    495 			mii->mii_media_active |= IFM_ETH_MASTER;
    496 	}
    497 }
    498