xref: /freebsd/sys/dev/mii/e1000phy.c (revision 2d3ce7133ad8cd5bfd8ac6b3c3d42dd3e493e911)
12a4339f7SMatt Jacob /* $FreeBSD$ */
22a4339f7SMatt Jacob /*
32a4339f7SMatt Jacob  * Principal Author: Parag Patel
42a4339f7SMatt Jacob  * Copyright (c) 2001
52a4339f7SMatt Jacob  * All rights reserved.
62a4339f7SMatt Jacob  *
72a4339f7SMatt Jacob  * Redistribution and use in source and binary forms, with or without
82a4339f7SMatt Jacob  * modification, are permitted provided that the following conditions
92a4339f7SMatt Jacob  * are met:
102a4339f7SMatt Jacob  * 1. Redistributions of source code must retain the above copyright
112a4339f7SMatt Jacob  *    notice unmodified, this list of conditions, and the following
122a4339f7SMatt Jacob  *    disclaimer.
132a4339f7SMatt Jacob  * 2. Redistributions in binary form must reproduce the above copyright
142a4339f7SMatt Jacob  *    notice, this list of conditions and the following disclaimer in the
152a4339f7SMatt Jacob  *    documentation and/or other materials provided with the distribution.
162a4339f7SMatt Jacob  *
172a4339f7SMatt Jacob  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
182a4339f7SMatt Jacob  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
192a4339f7SMatt Jacob  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
202a4339f7SMatt Jacob  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
212a4339f7SMatt Jacob  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
222a4339f7SMatt Jacob  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
232a4339f7SMatt Jacob  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
242a4339f7SMatt Jacob  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
252a4339f7SMatt Jacob  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
262a4339f7SMatt Jacob  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
272a4339f7SMatt Jacob  * SUCH DAMAGE.
282a4339f7SMatt Jacob  *
292a4339f7SMatt Jacob  * Additonal Copyright (c) 2001 by Traakan Software under same licence.
302a4339f7SMatt Jacob  * Secondary Author: Matthew Jacob
312a4339f7SMatt Jacob  */
322a4339f7SMatt Jacob 
332a4339f7SMatt Jacob /*
342a4339f7SMatt Jacob  * driver for the Marvell 88E1000 series external 1000/100/10-BT PHY.
352a4339f7SMatt Jacob  */
362a4339f7SMatt Jacob 
372a4339f7SMatt Jacob #include <sys/param.h>
382a4339f7SMatt Jacob #include <sys/systm.h>
392a4339f7SMatt Jacob #include <sys/kernel.h>
402a4339f7SMatt Jacob #include <sys/socket.h>
412a4339f7SMatt Jacob #include <sys/bus.h>
422a4339f7SMatt Jacob 
432a4339f7SMatt Jacob #include <machine/clock.h>
442a4339f7SMatt Jacob 
452a4339f7SMatt Jacob #include <net/if.h>
462a4339f7SMatt Jacob #include <net/if_media.h>
472a4339f7SMatt Jacob 
482a4339f7SMatt Jacob #include <dev/mii/mii.h>
492a4339f7SMatt Jacob #include <dev/mii/miivar.h>
502d3ce713SDavid E. O'Brien #include "miidevs.h"
512a4339f7SMatt Jacob 
522a4339f7SMatt Jacob #include <dev/mii/e1000phyreg.h>
532a4339f7SMatt Jacob 
542a4339f7SMatt Jacob #include "miibus_if.h"
552a4339f7SMatt Jacob 
562a4339f7SMatt Jacob static int e1000phy_probe(device_t);
572a4339f7SMatt Jacob static int e1000phy_attach(device_t);
582a4339f7SMatt Jacob 
592a4339f7SMatt Jacob static device_method_t e1000phy_methods[] = {
602a4339f7SMatt Jacob 	/* device interface */
612a4339f7SMatt Jacob 	DEVMETHOD(device_probe,		e1000phy_probe),
622a4339f7SMatt Jacob 	DEVMETHOD(device_attach,	e1000phy_attach),
63279fe8d1SPoul-Henning Kamp 	DEVMETHOD(device_detach,	mii_phy_detach),
642a4339f7SMatt Jacob 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
652a4339f7SMatt Jacob 	{ 0, 0 }
662a4339f7SMatt Jacob };
672a4339f7SMatt Jacob 
682a4339f7SMatt Jacob static devclass_t e1000phy_devclass;
692a4339f7SMatt Jacob static driver_t e1000phy_driver = {
702a4339f7SMatt Jacob 	"e1000phy", e1000phy_methods, sizeof (struct mii_softc)
712a4339f7SMatt Jacob };
722a4339f7SMatt Jacob DRIVER_MODULE(e1000phy, miibus, e1000phy_driver, e1000phy_devclass, 0, 0);
732a4339f7SMatt Jacob 
74d9730b8bSJonathan Lemon static int	e1000phy_service(struct mii_softc *, struct mii_data *, int);
75d9730b8bSJonathan Lemon static void	e1000phy_status(struct mii_softc *);
762a4339f7SMatt Jacob static void	e1000phy_reset(struct mii_softc *);
77fd94424cSPoul-Henning Kamp static int	e1000phy_mii_phy_auto(struct mii_softc *);
782a4339f7SMatt Jacob 
792a4339f7SMatt Jacob static int e1000phy_debug = 0;
802a4339f7SMatt Jacob 
812a4339f7SMatt Jacob static int
822a4339f7SMatt Jacob e1000phy_probe(device_t	dev)
832a4339f7SMatt Jacob {
842a4339f7SMatt Jacob 	struct mii_attach_args *ma;
852a4339f7SMatt Jacob 	u_int32_t id;
862a4339f7SMatt Jacob 
872a4339f7SMatt Jacob 	ma = device_get_ivars(dev);
882a4339f7SMatt Jacob 	id = ((ma->mii_id1 << 16) | ma->mii_id2) & E1000_ID_MASK;
892a4339f7SMatt Jacob 
902a4339f7SMatt Jacob 	if (id != E1000_ID_88E1000 && id != E1000_ID_88E1000S) {
912a4339f7SMatt Jacob 		return ENXIO;
922a4339f7SMatt Jacob 	}
932a4339f7SMatt Jacob 
942a4339f7SMatt Jacob 	device_set_desc(dev, MII_STR_MARVELL_E1000);
952a4339f7SMatt Jacob 	return 0;
962a4339f7SMatt Jacob }
972a4339f7SMatt Jacob 
982a4339f7SMatt Jacob static int
992a4339f7SMatt Jacob e1000phy_attach(device_t dev)
1002a4339f7SMatt Jacob {
1012a4339f7SMatt Jacob 	struct mii_softc *sc;
1022a4339f7SMatt Jacob 	struct mii_attach_args *ma;
1032a4339f7SMatt Jacob 	struct mii_data *mii;
1042a4339f7SMatt Jacob 
1052a4339f7SMatt Jacob 	getenv_int("e1000phy_debug", &e1000phy_debug);
1062a4339f7SMatt Jacob 
1072a4339f7SMatt Jacob 	sc = device_get_softc(dev);
1082a4339f7SMatt Jacob 	ma = device_get_ivars(dev);
1092a4339f7SMatt Jacob 	sc->mii_dev = device_get_parent(dev);
1102a4339f7SMatt Jacob 	mii = device_get_softc(sc->mii_dev);
1112a4339f7SMatt Jacob 	LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
1122a4339f7SMatt Jacob 
1132a4339f7SMatt Jacob 	sc->mii_inst = mii->mii_instance;
1142a4339f7SMatt Jacob 	sc->mii_phy = ma->mii_phyno;
1152a4339f7SMatt Jacob 	sc->mii_service = e1000phy_service;
1162a4339f7SMatt Jacob 	sc->mii_pdata = mii;
1172a4339f7SMatt Jacob 
1182a4339f7SMatt Jacob 	sc->mii_flags |= MIIF_NOISOLATE;
1192a4339f7SMatt Jacob 	mii->mii_instance++;
1202a4339f7SMatt Jacob 	e1000phy_reset(sc);
1212a4339f7SMatt Jacob 
122105cb0c6SJonathan Lemon 	device_printf(dev, " ");
1232a4339f7SMatt Jacob 
124105cb0c6SJonathan Lemon #define	ADD(m, c)	ifmedia_add(&mii->mii_media, (m), (c), NULL)
125105cb0c6SJonathan Lemon /*
1262a4339f7SMatt Jacob 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
1272a4339f7SMatt Jacob 	    E1000_CR_ISOLATE);
1282a4339f7SMatt Jacob */
1292a4339f7SMatt Jacob 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, sc->mii_inst),
1302a4339f7SMatt Jacob 	    E1000_CR_SPEED_10);
131105cb0c6SJonathan Lemon 	printf("10baseT, ");
132105cb0c6SJonathan Lemon 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, sc->mii_inst),
133105cb0c6SJonathan Lemon 	    E1000_CR_SPEED_10 | E1000_CR_FULL_DUPLEX);
134105cb0c6SJonathan Lemon 	printf("10baseT-FDX, ");
135105cb0c6SJonathan Lemon 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, sc->mii_inst),
136105cb0c6SJonathan Lemon 	    E1000_CR_SPEED_100);
137105cb0c6SJonathan Lemon 	printf("100baseTX, ");
138105cb0c6SJonathan Lemon 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, sc->mii_inst),
139105cb0c6SJonathan Lemon 	    E1000_CR_SPEED_100 | E1000_CR_FULL_DUPLEX);
140105cb0c6SJonathan Lemon 	printf("100baseTX-FDX, ");
141105cb0c6SJonathan Lemon 	/*
142105cb0c6SJonathan Lemon 	 * 1000BT-simplex not supported; driver must ignore this entry,
143105cb0c6SJonathan Lemon 	 * but it must be present in order to manually set full-duplex.
144105cb0c6SJonathan Lemon 	 */
145b418ad5cSPoul-Henning Kamp 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, 0, sc->mii_inst),
146105cb0c6SJonathan Lemon 	    E1000_CR_SPEED_1000);
147b418ad5cSPoul-Henning Kamp 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, IFM_FDX, sc->mii_inst),
148105cb0c6SJonathan Lemon 	    E1000_CR_SPEED_1000 | E1000_CR_FULL_DUPLEX);
149105cb0c6SJonathan Lemon 	printf("1000baseTX-FDX, ");
1502a4339f7SMatt Jacob 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst), 0);
151105cb0c6SJonathan Lemon 	printf("auto\n");
1522a4339f7SMatt Jacob #undef ADD
1532a4339f7SMatt Jacob 
1542a4339f7SMatt Jacob 	MIIBUS_MEDIAINIT(sc->mii_dev);
1552a4339f7SMatt Jacob 	return(0);
1562a4339f7SMatt Jacob }
1572a4339f7SMatt Jacob 
1582a4339f7SMatt Jacob static void
1592a4339f7SMatt Jacob e1000phy_reset(struct mii_softc *sc)
1602a4339f7SMatt Jacob {
1612a4339f7SMatt Jacob 	u_int32_t reg;
1622a4339f7SMatt Jacob 	int i;
1632a4339f7SMatt Jacob 
1642a4339f7SMatt Jacob 	/* initialize custom E1000 registers to magic values */
1652a4339f7SMatt Jacob 	reg = PHY_READ(sc, E1000_SCR);
1662a4339f7SMatt Jacob 	reg &= ~E1000_SCR_AUTO_X_MODE;
1672a4339f7SMatt Jacob 	PHY_WRITE(sc, E1000_SCR, reg);
1682a4339f7SMatt Jacob 
1692a4339f7SMatt Jacob 	/* normal PHY reset */
1702a4339f7SMatt Jacob 	/*mii_phy_reset(sc);*/
1712a4339f7SMatt Jacob 	reg = PHY_READ(sc, E1000_CR);
1722a4339f7SMatt Jacob 	reg |= E1000_CR_RESET;
1732a4339f7SMatt Jacob 	PHY_WRITE(sc, E1000_CR, reg);
1742a4339f7SMatt Jacob 
1752a4339f7SMatt Jacob 	for (i = 0; i < 500; i++) {
1762a4339f7SMatt Jacob 		DELAY(1);
1772a4339f7SMatt Jacob 		reg = PHY_READ(sc, E1000_CR);
1782a4339f7SMatt Jacob 		if (!(reg & E1000_CR_RESET))
1792a4339f7SMatt Jacob 			break;
1802a4339f7SMatt Jacob 	}
1812a4339f7SMatt Jacob 
1822a4339f7SMatt Jacob 	/* set more custom E1000 registers to magic values */
1832a4339f7SMatt Jacob 	reg = PHY_READ(sc, E1000_SCR);
1842a4339f7SMatt Jacob 	reg |= E1000_SCR_ASSERT_CRS_ON_TX;
1852a4339f7SMatt Jacob 	PHY_WRITE(sc, E1000_SCR, reg);
1862a4339f7SMatt Jacob 
1872a4339f7SMatt Jacob 	reg = PHY_READ(sc, E1000_ESCR);
1882a4339f7SMatt Jacob 	reg |= E1000_ESCR_TX_CLK_25;
1892a4339f7SMatt Jacob 	PHY_WRITE(sc, E1000_ESCR, reg);
1902a4339f7SMatt Jacob 
1912a4339f7SMatt Jacob 	/* even more magic to reset DSP? */
1922a4339f7SMatt Jacob 	PHY_WRITE(sc, 29, 0x1d);
1932a4339f7SMatt Jacob 	PHY_WRITE(sc, 30, 0xc1);
1942a4339f7SMatt Jacob 	PHY_WRITE(sc, 30, 0x00);
1952a4339f7SMatt Jacob }
1962a4339f7SMatt Jacob 
197d9730b8bSJonathan Lemon static int
1982a4339f7SMatt Jacob e1000phy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
1992a4339f7SMatt Jacob {
2002a4339f7SMatt Jacob 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
2012a4339f7SMatt Jacob 	int reg;
2022a4339f7SMatt Jacob 
2032a4339f7SMatt Jacob 	switch (cmd) {
2042a4339f7SMatt Jacob 	case MII_POLLSTAT:
2052a4339f7SMatt Jacob 		/*
2062a4339f7SMatt Jacob 		 * If we're not polling our PHY instance, just return.
2072a4339f7SMatt Jacob 		 */
2082a4339f7SMatt Jacob 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
2092a4339f7SMatt Jacob 			return (0);
2102a4339f7SMatt Jacob 		break;
2112a4339f7SMatt Jacob 
2122a4339f7SMatt Jacob 	case MII_MEDIACHG:
2132a4339f7SMatt Jacob 		/*
2142a4339f7SMatt Jacob 		 * If the media indicates a different PHY instance,
2152a4339f7SMatt Jacob 		 * isolate ourselves.
2162a4339f7SMatt Jacob 		 */
2172a4339f7SMatt Jacob 		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
2182a4339f7SMatt Jacob 			reg = PHY_READ(sc, E1000_CR);
2192a4339f7SMatt Jacob 			PHY_WRITE(sc, E1000_CR, reg | E1000_CR_ISOLATE);
2202a4339f7SMatt Jacob 			return (0);
2212a4339f7SMatt Jacob 		}
2222a4339f7SMatt Jacob 
2232a4339f7SMatt Jacob 		/*
2242a4339f7SMatt Jacob 		 * If the interface is not up, don't do anything.
2252a4339f7SMatt Jacob 		 */
2262a4339f7SMatt Jacob 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0) {
2272a4339f7SMatt Jacob 			break;
2282a4339f7SMatt Jacob 		}
2292a4339f7SMatt Jacob 
2302a4339f7SMatt Jacob 		switch (IFM_SUBTYPE(ife->ifm_media)) {
2312a4339f7SMatt Jacob 		case IFM_AUTO:
2322a4339f7SMatt Jacob 			e1000phy_reset(sc);
233fd94424cSPoul-Henning Kamp 			(void)e1000phy_mii_phy_auto(sc);
2342a4339f7SMatt Jacob 			break;
2352a4339f7SMatt Jacob 
236b418ad5cSPoul-Henning Kamp 		case IFM_1000_T:
2372a4339f7SMatt Jacob 			e1000phy_reset(sc);
2382a4339f7SMatt Jacob 
2392a4339f7SMatt Jacob 			/* TODO - any other way to force 1000BT? */
240fd94424cSPoul-Henning Kamp 			(void)e1000phy_mii_phy_auto(sc);
2412a4339f7SMatt Jacob 			break;
2422a4339f7SMatt Jacob 
2432a4339f7SMatt Jacob 		case IFM_100_TX:
2442a4339f7SMatt Jacob 			e1000phy_reset(sc);
2452a4339f7SMatt Jacob 
2462a4339f7SMatt Jacob 			if ((ife->ifm_media & IFM_GMASK) == IFM_FDX) {
2472a4339f7SMatt Jacob 				PHY_WRITE(sc, E1000_CR,
2482a4339f7SMatt Jacob 				    E1000_CR_FULL_DUPLEX | E1000_CR_SPEED_100);
2492a4339f7SMatt Jacob 				PHY_WRITE(sc, E1000_AR, E1000_AR_100TX_FD);
2502a4339f7SMatt Jacob 			} else {
2512a4339f7SMatt Jacob 				PHY_WRITE(sc, E1000_CR, E1000_CR_SPEED_100);
2522a4339f7SMatt Jacob 				PHY_WRITE(sc, E1000_AR, E1000_AR_100TX);
2532a4339f7SMatt Jacob 			}
2542a4339f7SMatt Jacob 			break;
2552a4339f7SMatt Jacob 
2562a4339f7SMatt Jacob 		case IFM_10_T:
2572a4339f7SMatt Jacob 			e1000phy_reset(sc);
2582a4339f7SMatt Jacob 
2592a4339f7SMatt Jacob 			if ((ife->ifm_media & IFM_GMASK) == IFM_FDX) {
2602a4339f7SMatt Jacob 				PHY_WRITE(sc, E1000_CR,
2612a4339f7SMatt Jacob 				    E1000_CR_FULL_DUPLEX | E1000_CR_SPEED_10);
2622a4339f7SMatt Jacob 				PHY_WRITE(sc, E1000_AR, E1000_AR_10T_FD);
2632a4339f7SMatt Jacob 			} else {
2642a4339f7SMatt Jacob 				PHY_WRITE(sc, E1000_CR, E1000_CR_SPEED_10);
2652a4339f7SMatt Jacob 				PHY_WRITE(sc, E1000_AR, E1000_AR_10T);
2662a4339f7SMatt Jacob 			}
2672a4339f7SMatt Jacob 
2682a4339f7SMatt Jacob 			break;
2692a4339f7SMatt Jacob 
2702a4339f7SMatt Jacob 		default:
2712a4339f7SMatt Jacob 			return (EINVAL);
2722a4339f7SMatt Jacob 		}
2732a4339f7SMatt Jacob 
2742a4339f7SMatt Jacob 		break;
2752a4339f7SMatt Jacob 
2762a4339f7SMatt Jacob 	case MII_TICK:
2772a4339f7SMatt Jacob 		/*
2782a4339f7SMatt Jacob 		 * If we're not currently selected, just return.
2792a4339f7SMatt Jacob 		 */
2802a4339f7SMatt Jacob 		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
2812a4339f7SMatt Jacob 			return (0);
2822a4339f7SMatt Jacob 		}
2832a4339f7SMatt Jacob 
2842a4339f7SMatt Jacob 		/*
2852a4339f7SMatt Jacob 		 * Is the interface even up?
2862a4339f7SMatt Jacob 		 */
287d9730b8bSJonathan Lemon 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
2882a4339f7SMatt Jacob 			return (0);
289d9730b8bSJonathan Lemon 
290d9730b8bSJonathan Lemon 		/*
291d9730b8bSJonathan Lemon 		 * Only used for autonegotiation.
292d9730b8bSJonathan Lemon 		 */
293d9730b8bSJonathan Lemon 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
294d9730b8bSJonathan Lemon 			break;
295d9730b8bSJonathan Lemon 
296d9730b8bSJonathan Lemon 		/*
297d9730b8bSJonathan Lemon 		 * check for link.
298d9730b8bSJonathan Lemon 		 * Read the status register twice; BMSR_LINK is latch-low.
299d9730b8bSJonathan Lemon 		 */
300d9730b8bSJonathan Lemon 		reg = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
301d9730b8bSJonathan Lemon 		if (reg & BMSR_LINK)
302d9730b8bSJonathan Lemon 			break;
3032a4339f7SMatt Jacob 
3042a4339f7SMatt Jacob 		/*
3052a4339f7SMatt Jacob 		 * Only retry autonegotiation every 5 seconds.
3062a4339f7SMatt Jacob 		 */
307d9730b8bSJonathan Lemon 		if (++sc->mii_ticks != 5)
3082a4339f7SMatt Jacob 			return (0);
309d9730b8bSJonathan Lemon 
3102a4339f7SMatt Jacob 		sc->mii_ticks = 0;
3112a4339f7SMatt Jacob 		e1000phy_reset(sc);
312fd94424cSPoul-Henning Kamp 		e1000phy_mii_phy_auto(sc);
3132a4339f7SMatt Jacob 		return (0);
3142a4339f7SMatt Jacob 	}
3152a4339f7SMatt Jacob 
3162a4339f7SMatt Jacob 	/* Update the media status. */
3172a4339f7SMatt Jacob 	e1000phy_status(sc);
3182a4339f7SMatt Jacob 
3192a4339f7SMatt Jacob 	/* Callback if something changed. */
320d9730b8bSJonathan Lemon 	mii_phy_update(sc, cmd);
3212a4339f7SMatt Jacob 	return (0);
3222a4339f7SMatt Jacob }
3232a4339f7SMatt Jacob 
324d9730b8bSJonathan Lemon static void
3252a4339f7SMatt Jacob e1000phy_status(struct mii_softc *sc)
3262a4339f7SMatt Jacob {
3272a4339f7SMatt Jacob 	struct mii_data *mii = sc->mii_pdata;
3282a4339f7SMatt Jacob 	int bmsr, bmcr, esr, ssr, isr, ar, lpar;
3292a4339f7SMatt Jacob 
3302a4339f7SMatt Jacob 	mii->mii_media_status = IFM_AVALID;
3312a4339f7SMatt Jacob 	mii->mii_media_active = IFM_ETHER;
3322a4339f7SMatt Jacob 
3332a4339f7SMatt Jacob 	bmsr = PHY_READ(sc, E1000_SR) | PHY_READ(sc, E1000_SR);
3342a4339f7SMatt Jacob 	esr = PHY_READ(sc, E1000_ESR);
3352a4339f7SMatt Jacob 	bmcr = PHY_READ(sc, E1000_CR);
3362a4339f7SMatt Jacob 	ssr = PHY_READ(sc, E1000_SSR);
3372a4339f7SMatt Jacob 	isr = PHY_READ(sc, E1000_ISR);
3382a4339f7SMatt Jacob 	ar = PHY_READ(sc, E1000_AR);
3392a4339f7SMatt Jacob 	lpar = PHY_READ(sc, E1000_LPAR);
3402a4339f7SMatt Jacob 
3412a4339f7SMatt Jacob 	if (bmsr & E1000_SR_LINK_STATUS)
3422a4339f7SMatt Jacob 		mii->mii_media_status |= IFM_ACTIVE;
3432a4339f7SMatt Jacob 
3442a4339f7SMatt Jacob 	if (bmcr & E1000_CR_LOOPBACK)
3452a4339f7SMatt Jacob 		mii->mii_media_active |= IFM_LOOP;
3462a4339f7SMatt Jacob 
347fd94424cSPoul-Henning Kamp 	if ((!(bmsr & E1000_SR_AUTO_NEG_COMPLETE) || !(ssr & E1000_SSR_LINK) ||
3482a4339f7SMatt Jacob 	    !(ssr & E1000_SSR_SPD_DPLX_RESOLVED))) {
3492a4339f7SMatt Jacob 		/* Erg, still trying, I guess... */
3502a4339f7SMatt Jacob 		mii->mii_media_active |= IFM_NONE;
3512a4339f7SMatt Jacob 		return;
3522a4339f7SMatt Jacob 	}
3532a4339f7SMatt Jacob 
3542a4339f7SMatt Jacob 	if (ssr & E1000_SSR_1000MBS)
355b418ad5cSPoul-Henning Kamp 		mii->mii_media_active |= IFM_1000_T;
3562a4339f7SMatt Jacob 	else if (ssr & E1000_SSR_100MBS)
3572a4339f7SMatt Jacob 		mii->mii_media_active |= IFM_100_TX;
3582a4339f7SMatt Jacob 	else
3592a4339f7SMatt Jacob 		mii->mii_media_active |= IFM_10_T;
3602a4339f7SMatt Jacob 
3612a4339f7SMatt Jacob 	if (ssr & E1000_SSR_DUPLEX)
3622a4339f7SMatt Jacob 		mii->mii_media_active |= IFM_FDX;
3632a4339f7SMatt Jacob 	else
3642a4339f7SMatt Jacob 		mii->mii_media_active |= IFM_HDX;
3652a4339f7SMatt Jacob 
3662a4339f7SMatt Jacob 	/* FLAG0==rx-flow-control FLAG1==tx-flow-control */
3672a4339f7SMatt Jacob 	if ((ar & E1000_AR_PAUSE) && (lpar & E1000_LPAR_PAUSE)) {
3682a4339f7SMatt Jacob 		mii->mii_media_active |= IFM_FLAG0 | IFM_FLAG1;
3692a4339f7SMatt Jacob 	} else if (!(ar & E1000_AR_PAUSE) && (ar & E1000_AR_ASM_DIR) &&
3702a4339f7SMatt Jacob 	    (lpar & E1000_LPAR_PAUSE) && (lpar & E1000_LPAR_ASM_DIR)) {
3712a4339f7SMatt Jacob 		mii->mii_media_active |= IFM_FLAG1;
3722a4339f7SMatt Jacob 	} else if ((ar & E1000_AR_PAUSE) && (ar & E1000_AR_ASM_DIR) &&
3732a4339f7SMatt Jacob 	    !(lpar & E1000_LPAR_PAUSE) && (lpar & E1000_LPAR_ASM_DIR)) {
3742a4339f7SMatt Jacob 		mii->mii_media_active |= IFM_FLAG0;
3752a4339f7SMatt Jacob 	}
3762a4339f7SMatt Jacob }
3772a4339f7SMatt Jacob 
3782a4339f7SMatt Jacob static int
379fd94424cSPoul-Henning Kamp e1000phy_mii_phy_auto(struct mii_softc *mii)
3802a4339f7SMatt Jacob {
3812a4339f7SMatt Jacob 
3822a4339f7SMatt Jacob 	PHY_WRITE(mii, E1000_AR, E1000_AR_10T | E1000_AR_10T_FD |
3832a4339f7SMatt Jacob 	    E1000_AR_100TX | E1000_AR_100TX_FD |
3842a4339f7SMatt Jacob 	    E1000_AR_PAUSE | E1000_AR_ASM_DIR);
3852a4339f7SMatt Jacob 	PHY_WRITE(mii, E1000_1GCR, E1000_1GCR_1000T_FD);
3862a4339f7SMatt Jacob 	PHY_WRITE(mii, E1000_CR,
3872a4339f7SMatt Jacob 	    E1000_CR_AUTO_NEG_ENABLE | E1000_CR_RESTART_AUTO_NEG);
3882a4339f7SMatt Jacob 
3892a4339f7SMatt Jacob 	return (EJUSTRETURN);
3902a4339f7SMatt Jacob }
391