xref: /freebsd/sys/dev/mii/e1000phy.c (revision d9730b8b5308cdb98f8c628dc0a02c8182eca8af)
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/malloc.h>
412a4339f7SMatt Jacob #include <sys/socket.h>
422a4339f7SMatt Jacob #include <sys/bus.h>
432a4339f7SMatt Jacob 
442a4339f7SMatt Jacob #include <machine/clock.h>
452a4339f7SMatt Jacob 
462a4339f7SMatt Jacob #include <net/if.h>
472a4339f7SMatt Jacob #include <net/if_media.h>
482a4339f7SMatt Jacob 
492a4339f7SMatt Jacob #include <dev/mii/mii.h>
502a4339f7SMatt Jacob #include <dev/mii/miivar.h>
512a4339f7SMatt Jacob #include <dev/mii/miidevs.h>
522a4339f7SMatt Jacob 
532a4339f7SMatt Jacob #include <dev/mii/e1000phyreg.h>
542a4339f7SMatt Jacob 
552a4339f7SMatt Jacob #include "miibus_if.h"
562a4339f7SMatt Jacob 
572a4339f7SMatt Jacob static int e1000phy_probe(device_t);
582a4339f7SMatt Jacob static int e1000phy_attach(device_t);
592a4339f7SMatt Jacob static int e1000phy_detach(device_t);
602a4339f7SMatt Jacob 
612a4339f7SMatt Jacob static device_method_t e1000phy_methods[] = {
622a4339f7SMatt Jacob 	/* device interface */
632a4339f7SMatt Jacob 	DEVMETHOD(device_probe,		e1000phy_probe),
642a4339f7SMatt Jacob 	DEVMETHOD(device_attach,	e1000phy_attach),
652a4339f7SMatt Jacob 	DEVMETHOD(device_detach,	e1000phy_detach),
662a4339f7SMatt Jacob 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
672a4339f7SMatt Jacob 	{ 0, 0 }
682a4339f7SMatt Jacob };
692a4339f7SMatt Jacob 
702a4339f7SMatt Jacob static devclass_t e1000phy_devclass;
712a4339f7SMatt Jacob static driver_t e1000phy_driver = {
722a4339f7SMatt Jacob 	"e1000phy", e1000phy_methods, sizeof (struct mii_softc)
732a4339f7SMatt Jacob };
742a4339f7SMatt Jacob DRIVER_MODULE(e1000phy, miibus, e1000phy_driver, e1000phy_devclass, 0, 0);
752a4339f7SMatt Jacob 
76d9730b8bSJonathan Lemon static int	e1000phy_service(struct mii_softc *, struct mii_data *, int);
77d9730b8bSJonathan Lemon static void	e1000phy_status(struct mii_softc *);
782a4339f7SMatt Jacob static void	e1000phy_reset(struct mii_softc *);
79d9730b8bSJonathan Lemon static int	e1000phy_mii_phy_auto(struct mii_softc *, int);
80d9730b8bSJonathan Lemon 
81d9730b8bSJonathan Lemon extern void	mii_phy_auto_timeout(void *);
822a4339f7SMatt Jacob 
832a4339f7SMatt Jacob static int e1000phy_debug = 0;
842a4339f7SMatt Jacob 
852a4339f7SMatt Jacob static int
862a4339f7SMatt Jacob e1000phy_probe(device_t	dev)
872a4339f7SMatt Jacob {
882a4339f7SMatt Jacob 	struct mii_attach_args *ma;
892a4339f7SMatt Jacob 	u_int32_t id;
902a4339f7SMatt Jacob 
912a4339f7SMatt Jacob 	ma = device_get_ivars(dev);
922a4339f7SMatt Jacob 	id = ((ma->mii_id1 << 16) | ma->mii_id2) & E1000_ID_MASK;
932a4339f7SMatt Jacob 
942a4339f7SMatt Jacob 	if (id != E1000_ID_88E1000 && id != E1000_ID_88E1000S) {
952a4339f7SMatt Jacob 		return ENXIO;
962a4339f7SMatt Jacob 	}
972a4339f7SMatt Jacob 
982a4339f7SMatt Jacob 	device_set_desc(dev, MII_STR_MARVELL_E1000);
992a4339f7SMatt Jacob 	return 0;
1002a4339f7SMatt Jacob }
1012a4339f7SMatt Jacob 
1022a4339f7SMatt Jacob static int
1032a4339f7SMatt Jacob e1000phy_attach(device_t dev)
1042a4339f7SMatt Jacob {
1052a4339f7SMatt Jacob 	struct mii_softc *sc;
1062a4339f7SMatt Jacob 	struct mii_attach_args *ma;
1072a4339f7SMatt Jacob 	struct mii_data *mii;
1082a4339f7SMatt Jacob 
1092a4339f7SMatt Jacob 	getenv_int("e1000phy_debug", &e1000phy_debug);
1102a4339f7SMatt Jacob 
1112a4339f7SMatt Jacob 	sc = device_get_softc(dev);
1122a4339f7SMatt Jacob 	ma = device_get_ivars(dev);
1132a4339f7SMatt Jacob 	sc->mii_dev = device_get_parent(dev);
1142a4339f7SMatt Jacob 	mii = device_get_softc(sc->mii_dev);
1152a4339f7SMatt Jacob 	LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
1162a4339f7SMatt Jacob 
1172a4339f7SMatt Jacob 	sc->mii_inst = mii->mii_instance;
1182a4339f7SMatt Jacob 	sc->mii_phy = ma->mii_phyno;
1192a4339f7SMatt Jacob 	sc->mii_service = e1000phy_service;
1202a4339f7SMatt Jacob 	sc->mii_pdata = mii;
1212a4339f7SMatt Jacob 
1222a4339f7SMatt Jacob 	sc->mii_flags |= MIIF_NOISOLATE;
1232a4339f7SMatt Jacob 	mii->mii_instance++;
1242a4339f7SMatt Jacob 	e1000phy_reset(sc);
1252a4339f7SMatt Jacob 
126105cb0c6SJonathan Lemon 	device_printf(dev, " ");
1272a4339f7SMatt Jacob 
128105cb0c6SJonathan Lemon #define	ADD(m, c)	ifmedia_add(&mii->mii_media, (m), (c), NULL)
129105cb0c6SJonathan Lemon /*
1302a4339f7SMatt Jacob 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
1312a4339f7SMatt Jacob 	    E1000_CR_ISOLATE);
1322a4339f7SMatt Jacob */
1332a4339f7SMatt Jacob 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, sc->mii_inst),
1342a4339f7SMatt Jacob 	    E1000_CR_SPEED_10);
135105cb0c6SJonathan Lemon 	printf("10baseT, ");
136105cb0c6SJonathan Lemon 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, sc->mii_inst),
137105cb0c6SJonathan Lemon 	    E1000_CR_SPEED_10 | E1000_CR_FULL_DUPLEX);
138105cb0c6SJonathan Lemon 	printf("10baseT-FDX, ");
139105cb0c6SJonathan Lemon 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, sc->mii_inst),
140105cb0c6SJonathan Lemon 	    E1000_CR_SPEED_100);
141105cb0c6SJonathan Lemon 	printf("100baseTX, ");
142105cb0c6SJonathan Lemon 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, sc->mii_inst),
143105cb0c6SJonathan Lemon 	    E1000_CR_SPEED_100 | E1000_CR_FULL_DUPLEX);
144105cb0c6SJonathan Lemon 	printf("100baseTX-FDX, ");
145105cb0c6SJonathan Lemon 	/*
146105cb0c6SJonathan Lemon 	 * 1000BT-simplex not supported; driver must ignore this entry,
147105cb0c6SJonathan Lemon 	 * but it must be present in order to manually set full-duplex.
148105cb0c6SJonathan Lemon 	 */
149105cb0c6SJonathan Lemon 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_TX, 0, sc->mii_inst),
150105cb0c6SJonathan Lemon 	    E1000_CR_SPEED_1000);
151105cb0c6SJonathan Lemon 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_TX, IFM_FDX, sc->mii_inst),
152105cb0c6SJonathan Lemon 	    E1000_CR_SPEED_1000 | E1000_CR_FULL_DUPLEX);
153105cb0c6SJonathan Lemon 	printf("1000baseTX-FDX, ");
1542a4339f7SMatt Jacob 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst), 0);
155105cb0c6SJonathan Lemon 	printf("auto\n");
1562a4339f7SMatt Jacob #undef ADD
1572a4339f7SMatt Jacob 
1582a4339f7SMatt Jacob 	MIIBUS_MEDIAINIT(sc->mii_dev);
1592a4339f7SMatt Jacob 	return(0);
1602a4339f7SMatt Jacob }
1612a4339f7SMatt Jacob 
1622a4339f7SMatt Jacob static int
1632a4339f7SMatt Jacob e1000phy_detach(device_t dev)
1642a4339f7SMatt Jacob {
1652a4339f7SMatt Jacob 	struct mii_softc *sc;
1662a4339f7SMatt Jacob 	struct mii_data *mii;
1672a4339f7SMatt Jacob 
1682a4339f7SMatt Jacob 	sc = device_get_softc(dev);
1692a4339f7SMatt Jacob 	mii = device_get_softc(device_get_parent(dev));
1702a4339f7SMatt Jacob 
1712a4339f7SMatt Jacob 	if (sc->mii_flags & MIIF_DOINGAUTO)
1722a4339f7SMatt Jacob 		untimeout(mii_phy_auto_timeout, sc, sc->mii_auto_ch);
1732a4339f7SMatt Jacob 
1742a4339f7SMatt Jacob 	sc->mii_dev = NULL;
1752a4339f7SMatt Jacob 	LIST_REMOVE(sc, mii_list);
1762a4339f7SMatt Jacob 
1772a4339f7SMatt Jacob 	return 0;
1782a4339f7SMatt Jacob }
1792a4339f7SMatt Jacob 
1802a4339f7SMatt Jacob static void
1812a4339f7SMatt Jacob e1000phy_reset(struct mii_softc *sc)
1822a4339f7SMatt Jacob {
1832a4339f7SMatt Jacob 	u_int32_t reg;
1842a4339f7SMatt Jacob 	int i;
1852a4339f7SMatt Jacob 
1862a4339f7SMatt Jacob 	/* initialize custom E1000 registers to magic values */
1872a4339f7SMatt Jacob 	reg = PHY_READ(sc, E1000_SCR);
1882a4339f7SMatt Jacob 	reg &= ~E1000_SCR_AUTO_X_MODE;
1892a4339f7SMatt Jacob 	PHY_WRITE(sc, E1000_SCR, reg);
1902a4339f7SMatt Jacob 
1912a4339f7SMatt Jacob 	/* normal PHY reset */
1922a4339f7SMatt Jacob 	/*mii_phy_reset(sc);*/
1932a4339f7SMatt Jacob 	reg = PHY_READ(sc, E1000_CR);
1942a4339f7SMatt Jacob 	reg |= E1000_CR_RESET;
1952a4339f7SMatt Jacob 	PHY_WRITE(sc, E1000_CR, reg);
1962a4339f7SMatt Jacob 
1972a4339f7SMatt Jacob 	for (i = 0; i < 500; i++) {
1982a4339f7SMatt Jacob 		DELAY(1);
1992a4339f7SMatt Jacob 		reg = PHY_READ(sc, E1000_CR);
2002a4339f7SMatt Jacob 		if (!(reg & E1000_CR_RESET))
2012a4339f7SMatt Jacob 			break;
2022a4339f7SMatt Jacob 	}
2032a4339f7SMatt Jacob 
2042a4339f7SMatt Jacob 	/* set more custom E1000 registers to magic values */
2052a4339f7SMatt Jacob 	reg = PHY_READ(sc, E1000_SCR);
2062a4339f7SMatt Jacob 	reg |= E1000_SCR_ASSERT_CRS_ON_TX;
2072a4339f7SMatt Jacob 	PHY_WRITE(sc, E1000_SCR, reg);
2082a4339f7SMatt Jacob 
2092a4339f7SMatt Jacob 	reg = PHY_READ(sc, E1000_ESCR);
2102a4339f7SMatt Jacob 	reg |= E1000_ESCR_TX_CLK_25;
2112a4339f7SMatt Jacob 	PHY_WRITE(sc, E1000_ESCR, reg);
2122a4339f7SMatt Jacob 
2132a4339f7SMatt Jacob 	/* even more magic to reset DSP? */
2142a4339f7SMatt Jacob 	PHY_WRITE(sc, 29, 0x1d);
2152a4339f7SMatt Jacob 	PHY_WRITE(sc, 30, 0xc1);
2162a4339f7SMatt Jacob 	PHY_WRITE(sc, 30, 0x00);
2172a4339f7SMatt Jacob }
2182a4339f7SMatt Jacob 
219d9730b8bSJonathan Lemon static int
2202a4339f7SMatt Jacob e1000phy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
2212a4339f7SMatt Jacob {
2222a4339f7SMatt Jacob 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
2232a4339f7SMatt Jacob 	int reg;
2242a4339f7SMatt Jacob 
2252a4339f7SMatt Jacob 	switch (cmd) {
2262a4339f7SMatt Jacob 	case MII_POLLSTAT:
2272a4339f7SMatt Jacob 		/*
2282a4339f7SMatt Jacob 		 * If we're not polling our PHY instance, just return.
2292a4339f7SMatt Jacob 		 */
2302a4339f7SMatt Jacob 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
2312a4339f7SMatt Jacob 			return (0);
2322a4339f7SMatt Jacob 		break;
2332a4339f7SMatt Jacob 
2342a4339f7SMatt Jacob 	case MII_MEDIACHG:
2352a4339f7SMatt Jacob 		/*
2362a4339f7SMatt Jacob 		 * If the media indicates a different PHY instance,
2372a4339f7SMatt Jacob 		 * isolate ourselves.
2382a4339f7SMatt Jacob 		 */
2392a4339f7SMatt Jacob 		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
2402a4339f7SMatt Jacob 			reg = PHY_READ(sc, E1000_CR);
2412a4339f7SMatt Jacob 			PHY_WRITE(sc, E1000_CR, reg | E1000_CR_ISOLATE);
2422a4339f7SMatt Jacob 			return (0);
2432a4339f7SMatt Jacob 		}
2442a4339f7SMatt Jacob 
2452a4339f7SMatt Jacob 		/*
2462a4339f7SMatt Jacob 		 * If the interface is not up, don't do anything.
2472a4339f7SMatt Jacob 		 */
2482a4339f7SMatt Jacob 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0) {
2492a4339f7SMatt Jacob 			break;
2502a4339f7SMatt Jacob 		}
2512a4339f7SMatt Jacob 
2522a4339f7SMatt Jacob 		switch (IFM_SUBTYPE(ife->ifm_media)) {
2532a4339f7SMatt Jacob 		case IFM_AUTO:
2542a4339f7SMatt Jacob 			/*
2552a4339f7SMatt Jacob 			 * If we're already in auto mode, just return.
2562a4339f7SMatt Jacob 			 */
2572a4339f7SMatt Jacob 			if (sc->mii_flags & MIIF_DOINGAUTO) {
2582a4339f7SMatt Jacob 				return (0);
2592a4339f7SMatt Jacob 			}
2602a4339f7SMatt Jacob 			e1000phy_reset(sc);
2612a4339f7SMatt Jacob 			(void)e1000phy_mii_phy_auto(sc, 1);
2622a4339f7SMatt Jacob 			break;
2632a4339f7SMatt Jacob 
2642a4339f7SMatt Jacob 		case IFM_1000_TX:
2652a4339f7SMatt Jacob 			if (sc->mii_flags & MIIF_DOINGAUTO)
2662a4339f7SMatt Jacob 				return (0);
2672a4339f7SMatt Jacob 
2682a4339f7SMatt Jacob 			e1000phy_reset(sc);
2692a4339f7SMatt Jacob 
2702a4339f7SMatt Jacob 			/* TODO - any other way to force 1000BT? */
2712a4339f7SMatt Jacob 			(void)e1000phy_mii_phy_auto(sc, 1);
2722a4339f7SMatt Jacob 			break;
2732a4339f7SMatt Jacob 
2742a4339f7SMatt Jacob 		case IFM_100_TX:
2752a4339f7SMatt Jacob 			e1000phy_reset(sc);
2762a4339f7SMatt Jacob 
2772a4339f7SMatt Jacob 			if ((ife->ifm_media & IFM_GMASK) == IFM_FDX) {
2782a4339f7SMatt Jacob 				PHY_WRITE(sc, E1000_CR,
2792a4339f7SMatt Jacob 				    E1000_CR_FULL_DUPLEX | E1000_CR_SPEED_100);
2802a4339f7SMatt Jacob 				PHY_WRITE(sc, E1000_AR, E1000_AR_100TX_FD);
2812a4339f7SMatt Jacob 			} else {
2822a4339f7SMatt Jacob 				PHY_WRITE(sc, E1000_CR, E1000_CR_SPEED_100);
2832a4339f7SMatt Jacob 				PHY_WRITE(sc, E1000_AR, E1000_AR_100TX);
2842a4339f7SMatt Jacob 			}
2852a4339f7SMatt Jacob 			break;
2862a4339f7SMatt Jacob 
2872a4339f7SMatt Jacob 		case IFM_10_T:
2882a4339f7SMatt Jacob 			e1000phy_reset(sc);
2892a4339f7SMatt Jacob 
2902a4339f7SMatt Jacob 			if ((ife->ifm_media & IFM_GMASK) == IFM_FDX) {
2912a4339f7SMatt Jacob 				PHY_WRITE(sc, E1000_CR,
2922a4339f7SMatt Jacob 				    E1000_CR_FULL_DUPLEX | E1000_CR_SPEED_10);
2932a4339f7SMatt Jacob 				PHY_WRITE(sc, E1000_AR, E1000_AR_10T_FD);
2942a4339f7SMatt Jacob 			} else {
2952a4339f7SMatt Jacob 				PHY_WRITE(sc, E1000_CR, E1000_CR_SPEED_10);
2962a4339f7SMatt Jacob 				PHY_WRITE(sc, E1000_AR, E1000_AR_10T);
2972a4339f7SMatt Jacob 			}
2982a4339f7SMatt Jacob 
2992a4339f7SMatt Jacob 			break;
3002a4339f7SMatt Jacob 
3012a4339f7SMatt Jacob 		default:
3022a4339f7SMatt Jacob 			return (EINVAL);
3032a4339f7SMatt Jacob 		}
3042a4339f7SMatt Jacob 
3052a4339f7SMatt Jacob 		break;
3062a4339f7SMatt Jacob 
3072a4339f7SMatt Jacob 	case MII_TICK:
3082a4339f7SMatt Jacob 		/*
3092a4339f7SMatt Jacob 		 * If we're not currently selected, just return.
3102a4339f7SMatt Jacob 		 */
3112a4339f7SMatt Jacob 		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
3122a4339f7SMatt Jacob 			return (0);
3132a4339f7SMatt Jacob 		}
3142a4339f7SMatt Jacob 
3152a4339f7SMatt Jacob 		/*
3162a4339f7SMatt Jacob 		 * Is the interface even up?
3172a4339f7SMatt Jacob 		 */
318d9730b8bSJonathan Lemon 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
3192a4339f7SMatt Jacob 			return (0);
320d9730b8bSJonathan Lemon 
321d9730b8bSJonathan Lemon 		/*
322d9730b8bSJonathan Lemon 		 * Only used for autonegotiation.
323d9730b8bSJonathan Lemon 		 */
324d9730b8bSJonathan Lemon 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
325d9730b8bSJonathan Lemon 			break;
326d9730b8bSJonathan Lemon 
327d9730b8bSJonathan Lemon 		/*
328d9730b8bSJonathan Lemon 		 * check for link.
329d9730b8bSJonathan Lemon 		 * Read the status register twice; BMSR_LINK is latch-low.
330d9730b8bSJonathan Lemon 		 */
331d9730b8bSJonathan Lemon 		reg = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
332d9730b8bSJonathan Lemon 		if (reg & BMSR_LINK)
333d9730b8bSJonathan Lemon 			break;
3342a4339f7SMatt Jacob 
3352a4339f7SMatt Jacob 		/*
3362a4339f7SMatt Jacob 		 * Only retry autonegotiation every 5 seconds.
3372a4339f7SMatt Jacob 		 */
338d9730b8bSJonathan Lemon 		if (++sc->mii_ticks != 5)
3392a4339f7SMatt Jacob 			return (0);
340d9730b8bSJonathan Lemon 
3412a4339f7SMatt Jacob 		sc->mii_ticks = 0;
3422a4339f7SMatt Jacob 		e1000phy_reset(sc);
343d9730b8bSJonathan Lemon 		if (e1000phy_mii_phy_auto(sc, 0) == EJUSTRETURN)
3442a4339f7SMatt Jacob 			return (0);
3452a4339f7SMatt Jacob 		break;
3462a4339f7SMatt Jacob 	}
3472a4339f7SMatt Jacob 
3482a4339f7SMatt Jacob 	/* Update the media status. */
3492a4339f7SMatt Jacob 	e1000phy_status(sc);
3502a4339f7SMatt Jacob 
3512a4339f7SMatt Jacob 	/* Callback if something changed. */
352d9730b8bSJonathan Lemon 	mii_phy_update(sc, cmd);
3532a4339f7SMatt Jacob 	return (0);
3542a4339f7SMatt Jacob }
3552a4339f7SMatt Jacob 
356d9730b8bSJonathan Lemon static void
3572a4339f7SMatt Jacob e1000phy_status(struct mii_softc *sc)
3582a4339f7SMatt Jacob {
3592a4339f7SMatt Jacob 	struct mii_data *mii = sc->mii_pdata;
3602a4339f7SMatt Jacob 	int bmsr, bmcr, esr, ssr, isr, ar, lpar;
3612a4339f7SMatt Jacob 
3622a4339f7SMatt Jacob 	mii->mii_media_status = IFM_AVALID;
3632a4339f7SMatt Jacob 	mii->mii_media_active = IFM_ETHER;
3642a4339f7SMatt Jacob 
3652a4339f7SMatt Jacob 	bmsr = PHY_READ(sc, E1000_SR) | PHY_READ(sc, E1000_SR);
3662a4339f7SMatt Jacob 	esr = PHY_READ(sc, E1000_ESR);
3672a4339f7SMatt Jacob 	bmcr = PHY_READ(sc, E1000_CR);
3682a4339f7SMatt Jacob 	ssr = PHY_READ(sc, E1000_SSR);
3692a4339f7SMatt Jacob 	isr = PHY_READ(sc, E1000_ISR);
3702a4339f7SMatt Jacob 	ar = PHY_READ(sc, E1000_AR);
3712a4339f7SMatt Jacob 	lpar = PHY_READ(sc, E1000_LPAR);
3722a4339f7SMatt Jacob 
3732a4339f7SMatt Jacob 	if (bmsr & E1000_SR_LINK_STATUS)
3742a4339f7SMatt Jacob 		mii->mii_media_status |= IFM_ACTIVE;
3752a4339f7SMatt Jacob 
3762a4339f7SMatt Jacob 	if (bmcr & E1000_CR_LOOPBACK)
3772a4339f7SMatt Jacob 		mii->mii_media_active |= IFM_LOOP;
3782a4339f7SMatt Jacob 
3792a4339f7SMatt Jacob 	if ((sc->mii_flags & MIIF_DOINGAUTO) &&
3802a4339f7SMatt Jacob 	    (!(bmsr & E1000_SR_AUTO_NEG_COMPLETE) || !(ssr & E1000_SSR_LINK) ||
3812a4339f7SMatt Jacob 	    !(ssr & E1000_SSR_SPD_DPLX_RESOLVED))) {
3822a4339f7SMatt Jacob 		/* Erg, still trying, I guess... */
3832a4339f7SMatt Jacob 		mii->mii_media_active |= IFM_NONE;
3842a4339f7SMatt Jacob 		return;
3852a4339f7SMatt Jacob 	}
3862a4339f7SMatt Jacob 
3872a4339f7SMatt Jacob 	if (ssr & E1000_SSR_1000MBS)
3882a4339f7SMatt Jacob 		mii->mii_media_active |= IFM_1000_TX;
3892a4339f7SMatt Jacob 	else if (ssr & E1000_SSR_100MBS)
3902a4339f7SMatt Jacob 		mii->mii_media_active |= IFM_100_TX;
3912a4339f7SMatt Jacob 	else
3922a4339f7SMatt Jacob 		mii->mii_media_active |= IFM_10_T;
3932a4339f7SMatt Jacob 
3942a4339f7SMatt Jacob 	if (ssr & E1000_SSR_DUPLEX)
3952a4339f7SMatt Jacob 		mii->mii_media_active |= IFM_FDX;
3962a4339f7SMatt Jacob 	else
3972a4339f7SMatt Jacob 		mii->mii_media_active |= IFM_HDX;
3982a4339f7SMatt Jacob 
3992a4339f7SMatt Jacob 	/* FLAG0==rx-flow-control FLAG1==tx-flow-control */
4002a4339f7SMatt Jacob 	if ((ar & E1000_AR_PAUSE) && (lpar & E1000_LPAR_PAUSE)) {
4012a4339f7SMatt Jacob 		mii->mii_media_active |= IFM_FLAG0 | IFM_FLAG1;
4022a4339f7SMatt Jacob 	} else if (!(ar & E1000_AR_PAUSE) && (ar & E1000_AR_ASM_DIR) &&
4032a4339f7SMatt Jacob 	    (lpar & E1000_LPAR_PAUSE) && (lpar & E1000_LPAR_ASM_DIR)) {
4042a4339f7SMatt Jacob 		mii->mii_media_active |= IFM_FLAG1;
4052a4339f7SMatt Jacob 	} else if ((ar & E1000_AR_PAUSE) && (ar & E1000_AR_ASM_DIR) &&
4062a4339f7SMatt Jacob 	    !(lpar & E1000_LPAR_PAUSE) && (lpar & E1000_LPAR_ASM_DIR)) {
4072a4339f7SMatt Jacob 		mii->mii_media_active |= IFM_FLAG0;
4082a4339f7SMatt Jacob 	}
4092a4339f7SMatt Jacob }
4102a4339f7SMatt Jacob 
4112a4339f7SMatt Jacob static int
4122a4339f7SMatt Jacob e1000phy_mii_phy_auto(struct mii_softc *mii, int waitfor)
4132a4339f7SMatt Jacob {
4142a4339f7SMatt Jacob 	int bmsr, i;
4152a4339f7SMatt Jacob 
4162a4339f7SMatt Jacob 	if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) {
4172a4339f7SMatt Jacob 		PHY_WRITE(mii, E1000_AR, E1000_AR_10T | E1000_AR_10T_FD |
4182a4339f7SMatt Jacob 		    E1000_AR_100TX | E1000_AR_100TX_FD |
4192a4339f7SMatt Jacob 		    E1000_AR_PAUSE | E1000_AR_ASM_DIR);
4202a4339f7SMatt Jacob 		PHY_WRITE(mii, E1000_1GCR, E1000_1GCR_1000T_FD);
4212a4339f7SMatt Jacob 		PHY_WRITE(mii, E1000_CR,
4222a4339f7SMatt Jacob 		    E1000_CR_AUTO_NEG_ENABLE | E1000_CR_RESTART_AUTO_NEG);
4232a4339f7SMatt Jacob 	}
4242a4339f7SMatt Jacob 
4252a4339f7SMatt Jacob 	if (waitfor) {
4262a4339f7SMatt Jacob 		/* Wait 500ms for it to complete. */
4272a4339f7SMatt Jacob 		for (i = 0; i < 500; i++) {
4282a4339f7SMatt Jacob 			bmsr =
4292a4339f7SMatt Jacob 			    PHY_READ(mii, E1000_SR) | PHY_READ(mii, E1000_SR);
4302a4339f7SMatt Jacob 
4312a4339f7SMatt Jacob 			if (bmsr & E1000_SR_AUTO_NEG_COMPLETE) {
4322a4339f7SMatt Jacob 				return (0);
4332a4339f7SMatt Jacob 			}
4342a4339f7SMatt Jacob 			DELAY(1000);
4352a4339f7SMatt Jacob 		}
4362a4339f7SMatt Jacob 
4372a4339f7SMatt Jacob 		/*
4382a4339f7SMatt Jacob 		 * Don't need to worry about clearing MIIF_DOINGAUTO.
4392a4339f7SMatt Jacob 		 * If that's set, a timeout is pending, and it will
4402a4339f7SMatt Jacob 		 * clear the flag. [do it anyway]
4412a4339f7SMatt Jacob 		 */
4422a4339f7SMatt Jacob 	}
4432a4339f7SMatt Jacob 
4442a4339f7SMatt Jacob 	/*
4452a4339f7SMatt Jacob 	 * Just let it finish asynchronously.  This is for the benefit of
4462a4339f7SMatt Jacob 	 * the tick handler driving autonegotiation.  Don't want 500ms
4472a4339f7SMatt Jacob 	 * delays all the time while the system is running!
4482a4339f7SMatt Jacob 	 */
4492a4339f7SMatt Jacob 	if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) {
4502a4339f7SMatt Jacob 		mii->mii_flags |= MIIF_DOINGAUTO;
4512a4339f7SMatt Jacob 		mii->mii_ticks = 0;
4522a4339f7SMatt Jacob 		mii->mii_auto_ch = timeout(mii_phy_auto_timeout, mii, hz >> 1);
4532a4339f7SMatt Jacob 	}
4542a4339f7SMatt Jacob 	return (EJUSTRETURN);
4552a4339f7SMatt Jacob }
456