Home | History | Annotate | Line # | Download | only in mii
makphy.c revision 1.52
      1 /*	$NetBSD: makphy.c,v 1.52 2019/01/22 03:42:27 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.52 2019/01/22 03:42:27 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 	uint16_t reg;
    187 
    188 	mpd = mii_phy_match(ma, makphys);
    189 	aprint_naive(": Media interface\n");
    190 	aprint_normal(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2));
    191 
    192 	sc->mii_dev = self;
    193 	sc->mii_mpd_oui = MII_OUI(ma->mii_id1, ma->mii_id2);
    194 	sc->mii_mpd_model = MII_MODEL(ma->mii_id2);
    195 	sc->mii_mpd_rev = MII_REV(ma->mii_id2);
    196 	sc->mii_inst = mii->mii_instance;
    197 	sc->mii_phy = ma->mii_phyno;
    198 	sc->mii_funcs = &makphy_funcs;
    199 	sc->mii_pdata = mii;
    200 	sc->mii_flags = ma->mii_flags;
    201 	sc->mii_anegticks = MII_ANEGTICKS;
    202 
    203 	/* Make sure page 0 is selected. */
    204 	PHY_WRITE(sc, MAKPHY_EADR, 0);
    205 
    206 	switch (sc->mii_mpd_model) {
    207 	case MII_MODEL_xxMARVELL_E1011:
    208 	case MII_MODEL_xxMARVELL_E1112:
    209 		PHY_READ(sc, MAKPHY_ESSR, &reg);
    210 		if (reg & ESSR_FIBER_LINK)
    211 			sc->mii_flags |= MIIF_HAVEFIBER;
    212 		break;
    213 	default:
    214 		break;
    215 	}
    216 
    217 	PHY_RESET(sc);
    218 
    219 	PHY_READ(sc, MII_BMSR, &sc->mii_capabilities);
    220 	sc->mii_capabilities &= ma->mii_capmask;
    221 	if (sc->mii_capabilities & BMSR_EXTSTAT)
    222 		PHY_READ(sc, MII_EXTSR, &sc->mii_extcapabilities);
    223 
    224 	aprint_normal_dev(self, "");
    225 	if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0 &&
    226 	    (sc->mii_extcapabilities & EXTSR_MEDIAMASK) == 0)
    227 		aprint_error("no media present");
    228 	else
    229 		mii_phy_add_media(sc);
    230 	aprint_normal("\n");
    231 }
    232 
    233 static void
    234 makphy_reset(struct mii_softc *sc)
    235 {
    236 	uint16_t reg;
    237 
    238 	mii_phy_reset(sc);
    239 
    240 	/*
    241 	 * Initialize PHY Specific Control Register.
    242 	 */
    243 	PHY_READ(sc, MAKPHY_PSCR, &reg);
    244 
    245 	/* Assert CRS on transmit. */
    246 	switch (sc->mii_mpd_model) {
    247 	case MII_MODEL_MARVELL_E1000_0:
    248 	case MII_MODEL_MARVELL_E1000_3:
    249 	case MII_MODEL_MARVELL_E1000_5:
    250 	case MII_MODEL_MARVELL_E1000_6:
    251 	case MII_MODEL_xxMARVELL_E1000S:
    252 	case MII_MODEL_xxMARVELL_E1011:
    253 	case MII_MODEL_xxMARVELL_E1111:
    254 		reg |= PSCR_CRS_ON_TX;
    255 		break;
    256 	default: /* No PSCR_CRS_ON_TX bit */
    257 		break;
    258 	}
    259 
    260 	/* Enable scrambler if necessary. */
    261 	if (sc->mii_mpd_model == MII_MODEL_xxMARVELL_E3016)
    262 		reg &= ~E3016_PSCR_SCRAMBLE_DIS;
    263 
    264 	/*
    265 	 * Store next page in the Link Partner Next Page register for
    266 	 * compatibility with 802.3ab.
    267 	 */
    268 	if (sc->mii_mpd_model == MII_MODEL_xxMARVELL_E3016)
    269 		reg |= E3016_PSCR_REG8NXTPG;
    270 
    271 	PHY_WRITE(sc, MAKPHY_PSCR, reg);
    272 
    273 	/* Configure LEDs if they were left unconfigured. */
    274 	if (sc->mii_mpd_model == MII_MODEL_xxMARVELL_E3016) {
    275 		PHY_READ(sc, 0x16, &reg);
    276 		if (reg == 0) {
    277 			reg = (0x0b << 8) | (0x05 << 4) | 0x04;	/* XXX */
    278 			PHY_WRITE(sc, 0x16, reg);
    279 		}
    280 	}
    281 
    282 	mii_phy_reset(sc);
    283 }
    284 
    285 static void
    286 makphy_pdown(struct mii_softc *sc, bool pdown)
    287 {
    288 	uint16_t bmcr, new;
    289 	bool need_reset = false;
    290 
    291 	/*
    292 	 * XXX
    293 	 * PSCR (register 16) should be modified on some chips.
    294 	 */
    295 
    296 	PHY_READ(sc, MII_BMCR, &bmcr);
    297 	if (pdown)
    298 		new = bmcr | BMCR_PDOWN;
    299 	else
    300 		new = bmcr & ~BMCR_PDOWN;
    301 	if (bmcr != new)
    302 		need_reset = true;
    303 
    304 	if (need_reset)
    305 		new |= BMCR_RESET;
    306 	PHY_WRITE(sc, MII_BMCR, new);
    307 }
    308 
    309 static int
    310 makphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
    311 {
    312 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
    313 	uint16_t bmcr;
    314 
    315 	if (!device_is_active(sc->mii_dev))
    316 		return (ENXIO);
    317 
    318 	switch (cmd) {
    319 	case MII_POLLSTAT:
    320 		/*
    321 		 * If we're not polling our PHY instance, just return.
    322 		 */
    323 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
    324 			return (0);
    325 		break;
    326 
    327 	case MII_MEDIACHG:
    328 		/*
    329 		 * If the media indicates a different PHY instance,
    330 		 * isolate ourselves.
    331 		 */
    332 		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
    333 			PHY_READ(sc, MII_BMCR, &bmcr);
    334 			PHY_WRITE(sc, MII_BMCR, bmcr | BMCR_ISO);
    335 			return (0);
    336 		}
    337 
    338 		/*
    339 		 * If the interface is not up, don't do anything.
    340 		 */
    341 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
    342 			break;
    343 
    344 		/* Try to power up the PHY in case it's down */
    345 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_NONE)
    346 			makphy_pdown(sc, MAKARG_PUP);
    347 
    348 		mii_phy_setmedia(sc);
    349 
    350 		/*
    351 		 * If autonegitation is not enabled, we need a
    352 		 * software reset for the settings to take effect.
    353 		 */
    354 		if (IFM_SUBTYPE(ife->ifm_media) == IFM_NONE)
    355 			makphy_pdown(sc, MAKARG_PDOWN);
    356 		else if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) {
    357 			PHY_READ(sc, MII_BMCR, &bmcr);
    358 			PHY_WRITE(sc, MII_BMCR, bmcr | BMCR_RESET);
    359 		}
    360 		break;
    361 
    362 	case MII_TICK:
    363 		/*
    364 		 * If we're not currently selected, just return.
    365 		 */
    366 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
    367 			return (0);
    368 
    369 		if (mii_phy_tick(sc) == EJUSTRETURN)
    370 			return (0);
    371 		break;
    372 
    373 	case MII_DOWN:
    374 		mii_phy_down(sc);
    375 		return (0);
    376 	}
    377 
    378 	/* Update the media status. */
    379 	mii_phy_status(sc);
    380 
    381 	/* Callback if something changed. */
    382 	mii_phy_update(sc, cmd);
    383 	return (0);
    384 }
    385 
    386 static void
    387 makphy_status(struct mii_softc *sc)
    388 {
    389 	struct mii_data *mii = sc->mii_pdata;
    390 	uint16_t bmcr, gsr, pssr;
    391 
    392 	mii->mii_media_status = IFM_AVALID;
    393 	mii->mii_media_active = IFM_ETHER;
    394 
    395 	PHY_READ(sc, MII_BMCR, &bmcr);
    396 	/* XXX FIXME: Use different page for Fiber on newer chips */
    397 	PHY_READ(sc, MAKPHY_PSSR, &pssr);
    398 
    399 	if (pssr & PSSR_LINK)
    400 		mii->mii_media_status |= IFM_ACTIVE;
    401 
    402 	if (bmcr & BMCR_LOOP)
    403 		mii->mii_media_active |= IFM_LOOP;
    404 
    405 	if (bmcr & BMCR_ISO) {
    406 		mii->mii_media_active |= IFM_NONE;
    407 		mii->mii_media_status = 0;
    408 		return;
    409 	}
    410 
    411 	if ((bmcr & BMCR_AUTOEN) != 0) {
    412 		/*
    413 		 * Check Speed and Duplex Resolved bit.
    414 		 * Note that this bit is always 1 when autonego is not enabled.
    415 		 */
    416 		if (!(pssr & PSSR_RESOLVED)) {
    417 			/* Erg, still trying, I guess... */
    418 			mii->mii_media_active |= IFM_NONE;
    419 			return;
    420 		}
    421 	} else {
    422 		if ((pssr & PSSR_LINK) == 0) {
    423 			mii->mii_media_active |= IFM_NONE;
    424 			return;
    425 		}
    426 	}
    427 
    428 	/* XXX FIXME: Use different page for Fiber on newer chips */
    429 	if (sc->mii_flags & MIIF_IS_1000X) {
    430 		mii->mii_media_active |= IFM_1000_SX;
    431 	} else {
    432 		switch (PSSR_SPEED_get(pssr)) {
    433 		case SPEED_1000:
    434 			mii->mii_media_active |= IFM_1000_T;
    435 			break;
    436 		case SPEED_100:
    437 			mii->mii_media_active |= IFM_100_TX;
    438 			break;
    439 		case SPEED_10:
    440 			mii->mii_media_active |= IFM_10_T;
    441 			break;
    442 		default: /* Undefined (reserved) value */
    443 			mii->mii_media_active |= IFM_NONE;
    444 			mii->mii_media_status = 0;
    445 			return;
    446 		}
    447 	}
    448 
    449 	if (pssr & PSSR_DUPLEX)
    450 		mii->mii_media_active |= mii_phy_flowstatus(sc) | IFM_FDX;
    451 	else
    452 		mii->mii_media_active |= IFM_HDX;
    453 
    454 	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T) {
    455 		PHY_READ(sc, MII_100T2SR, &gsr);
    456 		if (gsr & GTSR_MS_RES)
    457 			mii->mii_media_active |= IFM_ETH_MASTER;
    458 	}
    459 }
    460