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