xref: /freebsd/sys/dev/mii/rgephy.c (revision 3fe25a9bfcd9c8afeb096828567353a84be37423)
1098ca2bdSWarner Losh /*-
2df57947fSPedro F. Giffuni  * SPDX-License-Identifier: BSD-4-Clause
3df57947fSPedro F. Giffuni  *
49bac70b8SBill Paul  * Copyright (c) 2003
59bac70b8SBill Paul  *	Bill Paul <wpaul@windriver.com>.  All rights reserved.
69bac70b8SBill Paul  *
79bac70b8SBill Paul  * Redistribution and use in source and binary forms, with or without
89bac70b8SBill Paul  * modification, are permitted provided that the following conditions
99bac70b8SBill Paul  * are met:
109bac70b8SBill Paul  * 1. Redistributions of source code must retain the above copyright
119bac70b8SBill Paul  *    notice, this list of conditions and the following disclaimer.
129bac70b8SBill Paul  * 2. Redistributions in binary form must reproduce the above copyright
139bac70b8SBill Paul  *    notice, this list of conditions and the following disclaimer in the
149bac70b8SBill Paul  *    documentation and/or other materials provided with the distribution.
159bac70b8SBill Paul  * 3. All advertising materials mentioning features or use of this software
169bac70b8SBill Paul  *    must display the following acknowledgement:
179bac70b8SBill Paul  *	This product includes software developed by Bill Paul.
189bac70b8SBill Paul  * 4. Neither the name of the author nor the names of any co-contributors
199bac70b8SBill Paul  *    may be used to endorse or promote products derived from this software
209bac70b8SBill Paul  *    without specific prior written permission.
219bac70b8SBill Paul  *
229bac70b8SBill Paul  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
239bac70b8SBill Paul  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
249bac70b8SBill Paul  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
259bac70b8SBill Paul  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
269bac70b8SBill Paul  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
279bac70b8SBill Paul  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
289bac70b8SBill Paul  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
299bac70b8SBill Paul  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
309bac70b8SBill Paul  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
319bac70b8SBill Paul  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
329bac70b8SBill Paul  * THE POSSIBILITY OF SUCH DAMAGE.
339bac70b8SBill Paul  */
349bac70b8SBill Paul 
3550aa1061SMarius Strobl #include <sys/cdefs.h>
369bac70b8SBill Paul /*
37c06cddfeSPyun YongHyeon  * Driver for the RealTek 8169S/8110S/8211B/8211C internal 10/100/1000 PHY.
389bac70b8SBill Paul  */
399bac70b8SBill Paul 
409bac70b8SBill Paul #include <sys/param.h>
419bac70b8SBill Paul #include <sys/systm.h>
429bac70b8SBill Paul #include <sys/kernel.h>
4341ee9f1cSPoul-Henning Kamp #include <sys/module.h>
449bac70b8SBill Paul #include <sys/socket.h>
4562d76917SMarcel Moolenaar #include <sys/taskqueue.h>
469bac70b8SBill Paul #include <sys/bus.h>
479bac70b8SBill Paul 
489bac70b8SBill Paul #include <net/if.h>
4961a3ac6eSGleb Smirnoff #include <net/if_var.h>
509bac70b8SBill Paul #include <net/if_arp.h>
519bac70b8SBill Paul #include <net/if_media.h>
529bac70b8SBill Paul 
539bac70b8SBill Paul #include <dev/mii/mii.h>
549bac70b8SBill Paul #include <dev/mii/miivar.h>
559bac70b8SBill Paul #include "miidevs.h"
569bac70b8SBill Paul 
579bac70b8SBill Paul #include <dev/mii/rgephyreg.h>
589bac70b8SBill Paul 
599bac70b8SBill Paul #include "miibus_if.h"
609bac70b8SBill Paul 
619bac70b8SBill Paul #include <machine/bus.h>
62b2d3d26fSGleb Smirnoff #include <dev/rl/if_rlreg.h>
639bac70b8SBill Paul 
649bac70b8SBill Paul static int rgephy_probe(device_t);
659bac70b8SBill Paul static int rgephy_attach(device_t);
669bac70b8SBill Paul 
679bac70b8SBill Paul static device_method_t rgephy_methods[] = {
689bac70b8SBill Paul 	/* device interface */
699bac70b8SBill Paul 	DEVMETHOD(device_probe,		rgephy_probe),
709bac70b8SBill Paul 	DEVMETHOD(device_attach,	rgephy_attach),
719bac70b8SBill Paul 	DEVMETHOD(device_detach,	mii_phy_detach),
729bac70b8SBill Paul 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
73604f5f1fSMarius Strobl 	DEVMETHOD_END
749bac70b8SBill Paul };
759bac70b8SBill Paul 
769bac70b8SBill Paul static driver_t rgephy_driver = {
779bac70b8SBill Paul 	"rgephy",
789bac70b8SBill Paul 	rgephy_methods,
793fcb7a53SMarius Strobl 	sizeof(struct mii_softc)
809bac70b8SBill Paul };
819bac70b8SBill Paul 
82f438c2ffSJohn Baldwin DRIVER_MODULE(rgephy, miibus, rgephy_driver, 0, 0);
839bac70b8SBill Paul 
849bac70b8SBill Paul static int	rgephy_service(struct mii_softc *, struct mii_data *, int);
859bac70b8SBill Paul static void	rgephy_status(struct mii_softc *);
86991ab941SMarius Strobl static int	rgephy_mii_phy_auto(struct mii_softc *, int);
879bac70b8SBill Paul static void	rgephy_reset(struct mii_softc *);
8872200a8aSPyun YongHyeon static int	rgephy_linkup(struct mii_softc *);
899bac70b8SBill Paul static void	rgephy_loop(struct mii_softc *);
909bac70b8SBill Paul static void	rgephy_load_dspcode(struct mii_softc *);
917aa69e31SPyun YongHyeon static void	rgephy_disable_eee(struct mii_softc *);
929bac70b8SBill Paul 
93a35b9333SMarius Strobl static const struct mii_phydesc rgephys[] = {
943fcb7a53SMarius Strobl 	MII_PHY_DESC(REALTEK, RTL8169S),
95cd10b400SPyun YongHyeon 	MII_PHY_DESC(REALTEK, RTL8251),
96*3fe25a9bSPeter Jeremy 	MII_PHY_DESC(REALTEK, RTL8211FVD),
97a35b9333SMarius Strobl 	MII_PHY_END
98a35b9333SMarius Strobl };
99a35b9333SMarius Strobl 
1003fcb7a53SMarius Strobl static const struct mii_phy_funcs rgephy_funcs = {
1013fcb7a53SMarius Strobl 	rgephy_service,
1023fcb7a53SMarius Strobl 	rgephy_status,
1033fcb7a53SMarius Strobl 	rgephy_reset
1043fcb7a53SMarius Strobl };
1053fcb7a53SMarius Strobl 
1069bac70b8SBill Paul static int
rgephy_probe(device_t dev)1077d830ac9SWarner Losh rgephy_probe(device_t dev)
1089bac70b8SBill Paul {
1099bac70b8SBill Paul 
110a35b9333SMarius Strobl 	return (mii_phy_dev_probe(dev, rgephys, BUS_PROBE_DEFAULT));
1119bac70b8SBill Paul }
1129bac70b8SBill Paul 
1139bac70b8SBill Paul static int
rgephy_attach(device_t dev)1147d830ac9SWarner Losh rgephy_attach(device_t dev)
1159bac70b8SBill Paul {
1169bac70b8SBill Paul 	struct mii_softc *sc;
117fed3ed71SPyun YongHyeon 	u_int flags;
1189bac70b8SBill Paul 
1193fcb7a53SMarius Strobl 	sc = device_get_softc(dev);
120fed3ed71SPyun YongHyeon 	flags = 0;
1217e310d2dSGleb Smirnoff 	if (mii_dev_mac_match(dev, "re"))
122fed3ed71SPyun YongHyeon 		flags |= MIIF_PHYPRIV0;
123a24d62b5SKevin Lo 	else if (mii_dev_mac_match(dev, "ure"))
124a24d62b5SKevin Lo 		flags |= MIIF_PHYPRIV1;
125fed3ed71SPyun YongHyeon 	mii_phy_dev_attach(dev, flags, &rgephy_funcs, 0);
1269bac70b8SBill Paul 
127991ab941SMarius Strobl 	/* RTL8169S do not report auto-sense; add manually. */
128991ab941SMarius Strobl 	sc->mii_capabilities = (PHY_READ(sc, MII_BMSR) | BMSR_ANEG) &
1293fcb7a53SMarius Strobl 	    sc->mii_capmask;
130b91805b4SMarius Strobl 	if (sc->mii_capabilities & BMSR_EXTSTAT)
131b91805b4SMarius Strobl 		sc->mii_extcapabilities = PHY_READ(sc, MII_EXTSR);
1329bac70b8SBill Paul 	device_printf(dev, " ");
133ba76315fSMarius Strobl 	mii_phy_add_media(sc);
1349bac70b8SBill Paul 	printf("\n");
13539bb2e52SMarius Strobl 	/*
13639bb2e52SMarius Strobl 	 * Allow IFM_FLAG0 to be set indicating that auto-negotiation with
13739bb2e52SMarius Strobl 	 * manual configuration, which is used to work around issues with
13839bb2e52SMarius Strobl 	 * certain setups by default, should not be triggered as it may in
13939bb2e52SMarius Strobl 	 * turn cause harm in some edge cases.
14039bb2e52SMarius Strobl 	 */
1413fcb7a53SMarius Strobl 	sc->mii_pdata->mii_media.ifm_mask |= IFM_FLAG0;
1429bac70b8SBill Paul 
1433fcb7a53SMarius Strobl 	PHY_RESET(sc);
1443fcb7a53SMarius Strobl 
1459bac70b8SBill Paul 	MIIBUS_MEDIAINIT(sc->mii_dev);
1469bac70b8SBill Paul 	return (0);
1479bac70b8SBill Paul }
1489bac70b8SBill Paul 
1499bac70b8SBill Paul static int
rgephy_service(struct mii_softc * sc,struct mii_data * mii,int cmd)1507d830ac9SWarner Losh rgephy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
1519bac70b8SBill Paul {
1529bac70b8SBill Paul 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
15372200a8aSPyun YongHyeon 	int speed, gig, anar;
1549bac70b8SBill Paul 
1559bac70b8SBill Paul 	switch (cmd) {
1569bac70b8SBill Paul 	case MII_POLLSTAT:
1579bac70b8SBill Paul 		break;
1589bac70b8SBill Paul 
1599bac70b8SBill Paul 	case MII_MEDIACHG:
1603fcb7a53SMarius Strobl 		PHY_RESET(sc);	/* XXX hardware bug work-around */
1619bac70b8SBill Paul 
1624eb561d2SPyun YongHyeon 		anar = PHY_READ(sc, RGEPHY_MII_ANAR);
163991ab941SMarius Strobl 		anar &= ~(RGEPHY_ANAR_PC | RGEPHY_ANAR_ASP |
164991ab941SMarius Strobl 		    RGEPHY_ANAR_TX_FD | RGEPHY_ANAR_TX |
1654eb561d2SPyun YongHyeon 		    RGEPHY_ANAR_10_FD | RGEPHY_ANAR_10);
1664eb561d2SPyun YongHyeon 
1679bac70b8SBill Paul 		switch (IFM_SUBTYPE(ife->ifm_media)) {
1689bac70b8SBill Paul 		case IFM_AUTO:
1699bac70b8SBill Paul #ifdef foo
1709bac70b8SBill Paul 			/*
1719bac70b8SBill Paul 			 * If we're already in auto mode, just return.
1729bac70b8SBill Paul 			 */
1739bac70b8SBill Paul 			if (PHY_READ(sc, RGEPHY_MII_BMCR) & RGEPHY_BMCR_AUTOEN)
1749bac70b8SBill Paul 				return (0);
1759bac70b8SBill Paul #endif
176991ab941SMarius Strobl 			(void)rgephy_mii_phy_auto(sc, ife->ifm_media);
1779bac70b8SBill Paul 			break;
1789bac70b8SBill Paul 		case IFM_1000_T:
1799bac70b8SBill Paul 			speed = RGEPHY_S1000;
1809bac70b8SBill Paul 			goto setit;
1819bac70b8SBill Paul 		case IFM_100_TX:
1829bac70b8SBill Paul 			speed = RGEPHY_S100;
1834eb561d2SPyun YongHyeon 			anar |= RGEPHY_ANAR_TX_FD | RGEPHY_ANAR_TX;
1849bac70b8SBill Paul 			goto setit;
1859bac70b8SBill Paul 		case IFM_10_T:
1869bac70b8SBill Paul 			speed = RGEPHY_S10;
1874eb561d2SPyun YongHyeon 			anar |= RGEPHY_ANAR_10_FD | RGEPHY_ANAR_10;
1889bac70b8SBill Paul setit:
18939bb2e52SMarius Strobl 			if ((ife->ifm_media & IFM_FLOW) != 0 &&
19039bb2e52SMarius Strobl 			    (mii->mii_media.ifm_media & IFM_FLAG0) != 0)
19139bb2e52SMarius Strobl 				return (EINVAL);
19239bb2e52SMarius Strobl 
19339bb2e52SMarius Strobl 			if ((ife->ifm_media & IFM_FDX) != 0) {
1949bac70b8SBill Paul 				speed |= RGEPHY_BMCR_FDX;
1959bac70b8SBill Paul 				gig = RGEPHY_1000CTL_AFD;
1964eb561d2SPyun YongHyeon 				anar &= ~(RGEPHY_ANAR_TX | RGEPHY_ANAR_10);
19739bb2e52SMarius Strobl 				if ((ife->ifm_media & IFM_FLOW) != 0 ||
19839bb2e52SMarius Strobl 				    (sc->mii_flags & MIIF_FORCEPAUSE) != 0)
19939bb2e52SMarius Strobl 					anar |=
20039bb2e52SMarius Strobl 					    RGEPHY_ANAR_PC | RGEPHY_ANAR_ASP;
2019bac70b8SBill Paul 			} else {
2029bac70b8SBill Paul 				gig = RGEPHY_1000CTL_AHD;
2034eb561d2SPyun YongHyeon 				anar &=
2044eb561d2SPyun YongHyeon 				    ~(RGEPHY_ANAR_TX_FD | RGEPHY_ANAR_10_FD);
2059bac70b8SBill Paul 			}
20639bb2e52SMarius Strobl 			if (IFM_SUBTYPE(ife->ifm_media) == IFM_1000_T) {
207991ab941SMarius Strobl 				gig |= RGEPHY_1000CTL_MSE;
208991ab941SMarius Strobl 				if ((ife->ifm_media & IFM_ETH_MASTER) != 0)
209991ab941SMarius Strobl 				    gig |= RGEPHY_1000CTL_MSC;
21039bb2e52SMarius Strobl 			} else {
21139bb2e52SMarius Strobl 				gig = 0;
21239bb2e52SMarius Strobl 				anar &= ~RGEPHY_ANAR_ASP;
21339bb2e52SMarius Strobl 			}
21439bb2e52SMarius Strobl 			if ((mii->mii_media.ifm_media & IFM_FLAG0) == 0)
21539bb2e52SMarius Strobl 				speed |=
21639bb2e52SMarius Strobl 				    RGEPHY_BMCR_AUTOEN | RGEPHY_BMCR_STARTNEG;
21739bb2e52SMarius Strobl 			rgephy_loop(sc);
218991ab941SMarius Strobl 			PHY_WRITE(sc, RGEPHY_MII_1000CTL, gig);
219991ab941SMarius Strobl 			PHY_WRITE(sc, RGEPHY_MII_ANAR, anar);
22039bb2e52SMarius Strobl 			PHY_WRITE(sc, RGEPHY_MII_BMCR, speed);
2219bac70b8SBill Paul 			break;
2229bac70b8SBill Paul 		case IFM_NONE:
2239bac70b8SBill Paul 			PHY_WRITE(sc, MII_BMCR, BMCR_ISO | BMCR_PDOWN);
2249bac70b8SBill Paul 			break;
2259bac70b8SBill Paul 		default:
2269bac70b8SBill Paul 			return (EINVAL);
2279bac70b8SBill Paul 		}
2289bac70b8SBill Paul 		break;
2299bac70b8SBill Paul 
2309bac70b8SBill Paul 	case MII_TICK:
2319bac70b8SBill Paul 		/*
2329bac70b8SBill Paul 		 * Only used for autonegotiation.
2339bac70b8SBill Paul 		 */
23437fd5f0fSPyun YongHyeon 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) {
23537fd5f0fSPyun YongHyeon 			sc->mii_ticks = 0;
2369bac70b8SBill Paul 			break;
23737fd5f0fSPyun YongHyeon 		}
2389bac70b8SBill Paul 
2399bac70b8SBill Paul 		/*
2409bac70b8SBill Paul 		 * Check to see if we have link.  If we do, we don't
24139bb2e52SMarius Strobl 		 * need to restart the autonegotiation process.
2429bac70b8SBill Paul 		 */
24372200a8aSPyun YongHyeon 		if (rgephy_linkup(sc) != 0) {
244648bfbe6SPyun YongHyeon 			sc->mii_ticks = 0;
245648bfbe6SPyun YongHyeon 			break;
246648bfbe6SPyun YongHyeon 		}
24737fd5f0fSPyun YongHyeon 
24837fd5f0fSPyun YongHyeon 		/* Announce link loss right after it happens. */
24937fd5f0fSPyun YongHyeon 		if (sc->mii_ticks++ == 0)
2509bac70b8SBill Paul 			break;
2519bac70b8SBill Paul 
25237fd5f0fSPyun YongHyeon 		/* Only retry autonegotiation every mii_anegticks seconds. */
25337fd5f0fSPyun YongHyeon 		if (sc->mii_ticks <= sc->mii_anegticks)
25437fd5f0fSPyun YongHyeon 			return (0);
2559bac70b8SBill Paul 
2569bac70b8SBill Paul 		sc->mii_ticks = 0;
257991ab941SMarius Strobl 		rgephy_mii_phy_auto(sc, ife->ifm_media);
25837fd5f0fSPyun YongHyeon 		break;
2599bac70b8SBill Paul 	}
2609bac70b8SBill Paul 
2619bac70b8SBill Paul 	/* Update the media status. */
2623fcb7a53SMarius Strobl 	PHY_STATUS(sc);
2639bac70b8SBill Paul 
2649bac70b8SBill Paul 	/*
2659bac70b8SBill Paul 	 * Callback if something changed. Note that we need to poke
266d5a50459SBill Paul 	 * the DSP on the RealTek PHYs if the media changes.
2679bac70b8SBill Paul 	 *
2689bac70b8SBill Paul 	 */
2699bac70b8SBill Paul 	if (sc->mii_media_active != mii->mii_media_active ||
2709bac70b8SBill Paul 	    sc->mii_media_status != mii->mii_media_status ||
2719bac70b8SBill Paul 	    cmd == MII_MEDIACHG) {
2729bac70b8SBill Paul 		rgephy_load_dspcode(sc);
2739bac70b8SBill Paul 	}
2749a54cbb9SAndre Oppermann 	mii_phy_update(sc, cmd);
2759bac70b8SBill Paul 	return (0);
2769bac70b8SBill Paul }
2779bac70b8SBill Paul 
27872200a8aSPyun YongHyeon static int
rgephy_linkup(struct mii_softc * sc)27972200a8aSPyun YongHyeon rgephy_linkup(struct mii_softc *sc)
28072200a8aSPyun YongHyeon {
28172200a8aSPyun YongHyeon 	int linkup;
28272200a8aSPyun YongHyeon 	uint16_t reg;
28372200a8aSPyun YongHyeon 
28472200a8aSPyun YongHyeon 	linkup = 0;
28572200a8aSPyun YongHyeon 	if ((sc->mii_flags & MIIF_PHYPRIV0) == 0 &&
28672200a8aSPyun YongHyeon 	    sc->mii_mpd_rev >= RGEPHY_8211B) {
287*3fe25a9bSPeter Jeremy 		if (sc->mii_mpd_rev >= RGEPHY_8211F) {
28872200a8aSPyun YongHyeon 			reg = PHY_READ(sc, RGEPHY_F_MII_SSR);
28972200a8aSPyun YongHyeon 			if (reg & RGEPHY_F_SSR_LINK)
29072200a8aSPyun YongHyeon 				linkup++;
29172200a8aSPyun YongHyeon 		} else {
29272200a8aSPyun YongHyeon 			reg = PHY_READ(sc, RGEPHY_MII_SSR);
29372200a8aSPyun YongHyeon 			if (reg & RGEPHY_SSR_LINK)
29472200a8aSPyun YongHyeon 				linkup++;
29572200a8aSPyun YongHyeon 		}
29672200a8aSPyun YongHyeon 	} else {
297a24d62b5SKevin Lo 		if (sc->mii_flags & MIIF_PHYPRIV1)
298a24d62b5SKevin Lo 			reg = PHY_READ(sc, URE_GMEDIASTAT);
299a24d62b5SKevin Lo 		else
30072200a8aSPyun YongHyeon 			reg = PHY_READ(sc, RL_GMEDIASTAT);
30172200a8aSPyun YongHyeon 		if (reg & RL_GMEDIASTAT_LINK)
30272200a8aSPyun YongHyeon 			linkup++;
30372200a8aSPyun YongHyeon 	}
30472200a8aSPyun YongHyeon 
30572200a8aSPyun YongHyeon 	return (linkup);
30672200a8aSPyun YongHyeon }
30772200a8aSPyun YongHyeon 
3089bac70b8SBill Paul static void
rgephy_status(struct mii_softc * sc)3097d830ac9SWarner Losh rgephy_status(struct mii_softc *sc)
3109bac70b8SBill Paul {
3119bac70b8SBill Paul 	struct mii_data *mii = sc->mii_pdata;
312d5a50459SBill Paul 	int bmsr, bmcr;
313648bfbe6SPyun YongHyeon 	uint16_t ssr;
3149bac70b8SBill Paul 
3159bac70b8SBill Paul 	mii->mii_media_status = IFM_AVALID;
3169bac70b8SBill Paul 	mii->mii_media_active = IFM_ETHER;
3179bac70b8SBill Paul 
31872200a8aSPyun YongHyeon 	if (rgephy_linkup(sc) != 0)
319648bfbe6SPyun YongHyeon 		mii->mii_media_status |= IFM_ACTIVE;
320648bfbe6SPyun YongHyeon 
3219bac70b8SBill Paul 	bmsr = PHY_READ(sc, RGEPHY_MII_BMSR);
3229bac70b8SBill Paul 	bmcr = PHY_READ(sc, RGEPHY_MII_BMCR);
323b455d946SPyun YongHyeon 	if (bmcr & RGEPHY_BMCR_ISO) {
324b455d946SPyun YongHyeon 		mii->mii_media_active |= IFM_NONE;
325b455d946SPyun YongHyeon 		mii->mii_media_status = 0;
326b455d946SPyun YongHyeon 		return;
327b455d946SPyun YongHyeon 	}
3289bac70b8SBill Paul 
3299bac70b8SBill Paul 	if (bmcr & RGEPHY_BMCR_LOOP)
3309bac70b8SBill Paul 		mii->mii_media_active |= IFM_LOOP;
3319bac70b8SBill Paul 
3329bac70b8SBill Paul 	if (bmcr & RGEPHY_BMCR_AUTOEN) {
3339bac70b8SBill Paul 		if ((bmsr & RGEPHY_BMSR_ACOMP) == 0) {
3349bac70b8SBill Paul 			/* Erg, still trying, I guess... */
3359bac70b8SBill Paul 			mii->mii_media_active |= IFM_NONE;
3369bac70b8SBill Paul 			return;
3379bac70b8SBill Paul 		}
3389bac70b8SBill Paul 	}
3399bac70b8SBill Paul 
34072200a8aSPyun YongHyeon 	if ((sc->mii_flags & MIIF_PHYPRIV0) == 0 &&
34172200a8aSPyun YongHyeon 	    sc->mii_mpd_rev >= RGEPHY_8211B) {
342*3fe25a9bSPeter Jeremy 		if (sc->mii_mpd_rev >= RGEPHY_8211F) {
34372200a8aSPyun YongHyeon 			ssr = PHY_READ(sc, RGEPHY_F_MII_SSR);
34472200a8aSPyun YongHyeon 			switch (ssr & RGEPHY_F_SSR_SPD_MASK) {
34572200a8aSPyun YongHyeon 			case RGEPHY_F_SSR_S1000:
34672200a8aSPyun YongHyeon 				mii->mii_media_active |= IFM_1000_T;
34772200a8aSPyun YongHyeon 				break;
34872200a8aSPyun YongHyeon 			case RGEPHY_F_SSR_S100:
34972200a8aSPyun YongHyeon 				mii->mii_media_active |= IFM_100_TX;
35072200a8aSPyun YongHyeon 				break;
35172200a8aSPyun YongHyeon 			case RGEPHY_F_SSR_S10:
35272200a8aSPyun YongHyeon 				mii->mii_media_active |= IFM_10_T;
35372200a8aSPyun YongHyeon 				break;
35472200a8aSPyun YongHyeon 			default:
35572200a8aSPyun YongHyeon 				mii->mii_media_active |= IFM_NONE;
35672200a8aSPyun YongHyeon 				break;
35772200a8aSPyun YongHyeon 			}
35872200a8aSPyun YongHyeon 			if (ssr & RGEPHY_F_SSR_FDX)
35972200a8aSPyun YongHyeon 				mii->mii_media_active |= IFM_FDX;
36072200a8aSPyun YongHyeon 			else
36172200a8aSPyun YongHyeon 				mii->mii_media_active |= IFM_HDX;
36272200a8aSPyun YongHyeon 
36372200a8aSPyun YongHyeon 		} else {
364648bfbe6SPyun YongHyeon 			ssr = PHY_READ(sc, RGEPHY_MII_SSR);
365648bfbe6SPyun YongHyeon 			switch (ssr & RGEPHY_SSR_SPD_MASK) {
366648bfbe6SPyun YongHyeon 			case RGEPHY_SSR_S1000:
367648bfbe6SPyun YongHyeon 				mii->mii_media_active |= IFM_1000_T;
368648bfbe6SPyun YongHyeon 				break;
369648bfbe6SPyun YongHyeon 			case RGEPHY_SSR_S100:
370648bfbe6SPyun YongHyeon 				mii->mii_media_active |= IFM_100_TX;
371648bfbe6SPyun YongHyeon 				break;
372648bfbe6SPyun YongHyeon 			case RGEPHY_SSR_S10:
373648bfbe6SPyun YongHyeon 				mii->mii_media_active |= IFM_10_T;
374648bfbe6SPyun YongHyeon 				break;
375648bfbe6SPyun YongHyeon 			default:
376648bfbe6SPyun YongHyeon 				mii->mii_media_active |= IFM_NONE;
377648bfbe6SPyun YongHyeon 				break;
378648bfbe6SPyun YongHyeon 			}
379648bfbe6SPyun YongHyeon 			if (ssr & RGEPHY_SSR_FDX)
380648bfbe6SPyun YongHyeon 				mii->mii_media_active |= IFM_FDX;
381648bfbe6SPyun YongHyeon 			else
382648bfbe6SPyun YongHyeon 				mii->mii_media_active |= IFM_HDX;
38372200a8aSPyun YongHyeon 		}
384648bfbe6SPyun YongHyeon 	} else {
385a24d62b5SKevin Lo 		if (sc->mii_flags & MIIF_PHYPRIV1)
386a24d62b5SKevin Lo 			bmsr = PHY_READ(sc, URE_GMEDIASTAT);
387a24d62b5SKevin Lo 		else
3889bac70b8SBill Paul 			bmsr = PHY_READ(sc, RL_GMEDIASTAT);
3899bac70b8SBill Paul 		if (bmsr & RL_GMEDIASTAT_1000MBPS)
3909bac70b8SBill Paul 			mii->mii_media_active |= IFM_1000_T;
391ed510fb0SBill Paul 		else if (bmsr & RL_GMEDIASTAT_100MBPS)
392ed510fb0SBill Paul 			mii->mii_media_active |= IFM_100_TX;
393ed510fb0SBill Paul 		else if (bmsr & RL_GMEDIASTAT_10MBPS)
394ed510fb0SBill Paul 			mii->mii_media_active |= IFM_10_T;
395ed510fb0SBill Paul 		else
396ed510fb0SBill Paul 			mii->mii_media_active |= IFM_NONE;
3979bac70b8SBill Paul 		if (bmsr & RL_GMEDIASTAT_FDX)
3989bac70b8SBill Paul 			mii->mii_media_active |= IFM_FDX;
399648bfbe6SPyun YongHyeon 		else
400648bfbe6SPyun YongHyeon 			mii->mii_media_active |= IFM_HDX;
401648bfbe6SPyun YongHyeon 	}
402991ab941SMarius Strobl 
403991ab941SMarius Strobl 	if ((mii->mii_media_active & IFM_FDX) != 0)
404991ab941SMarius Strobl 		mii->mii_media_active |= mii_phy_flowstatus(sc);
405991ab941SMarius Strobl 
406991ab941SMarius Strobl 	if ((IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T) &&
407991ab941SMarius Strobl 	    (PHY_READ(sc, RGEPHY_MII_1000STS) & RGEPHY_1000STS_MSR) != 0)
408991ab941SMarius Strobl 		mii->mii_media_active |= IFM_ETH_MASTER;
4099bac70b8SBill Paul }
4109bac70b8SBill Paul 
4119bac70b8SBill Paul static int
rgephy_mii_phy_auto(struct mii_softc * sc,int media)412991ab941SMarius Strobl rgephy_mii_phy_auto(struct mii_softc *sc, int media)
4139bac70b8SBill Paul {
414991ab941SMarius Strobl 	int anar;
415028ccec4SMarius Strobl 
416991ab941SMarius Strobl 	rgephy_loop(sc);
4173fcb7a53SMarius Strobl 	PHY_RESET(sc);
4189bac70b8SBill Paul 
419991ab941SMarius Strobl 	anar = BMSR_MEDIA_TO_ANAR(sc->mii_capabilities) | ANAR_CSMA;
420991ab941SMarius Strobl 	if ((media & IFM_FLOW) != 0 || (sc->mii_flags & MIIF_FORCEPAUSE) != 0)
421991ab941SMarius Strobl 		anar |= RGEPHY_ANAR_PC | RGEPHY_ANAR_ASP;
422991ab941SMarius Strobl 	PHY_WRITE(sc, RGEPHY_MII_ANAR, anar);
4239bac70b8SBill Paul 	DELAY(1000);
424991ab941SMarius Strobl 	PHY_WRITE(sc, RGEPHY_MII_1000CTL,
425ed510fb0SBill Paul 	    RGEPHY_1000CTL_AHD | RGEPHY_1000CTL_AFD);
4269bac70b8SBill Paul 	DELAY(1000);
427991ab941SMarius Strobl 	PHY_WRITE(sc, RGEPHY_MII_BMCR,
4289bac70b8SBill Paul 	    RGEPHY_BMCR_AUTOEN | RGEPHY_BMCR_STARTNEG);
4299bac70b8SBill Paul 	DELAY(100);
4309bac70b8SBill Paul 
4319bac70b8SBill Paul 	return (EJUSTRETURN);
4329bac70b8SBill Paul }
4339bac70b8SBill Paul 
4349bac70b8SBill Paul static void
rgephy_loop(struct mii_softc * sc)4359bac70b8SBill Paul rgephy_loop(struct mii_softc *sc)
4369bac70b8SBill Paul {
4379bac70b8SBill Paul 	int i;
4389bac70b8SBill Paul 
439cd10b400SPyun YongHyeon 	if (sc->mii_mpd_model != MII_MODEL_REALTEK_RTL8251 &&
44072200a8aSPyun YongHyeon 	    sc->mii_mpd_rev < RGEPHY_8211B) {
4419bac70b8SBill Paul 		PHY_WRITE(sc, RGEPHY_MII_BMCR, RGEPHY_BMCR_PDOWN);
4429bac70b8SBill Paul 		DELAY(1000);
443648bfbe6SPyun YongHyeon 	}
4449bac70b8SBill Paul 
4459bac70b8SBill Paul 	for (i = 0; i < 15000; i++) {
446028ccec4SMarius Strobl 		if (!(PHY_READ(sc, RGEPHY_MII_BMSR) & RGEPHY_BMSR_LINK)) {
4479bac70b8SBill Paul #if 0
4489bac70b8SBill Paul 			device_printf(sc->mii_dev, "looped %d\n", i);
4499bac70b8SBill Paul #endif
4509bac70b8SBill Paul 			break;
4519bac70b8SBill Paul 		}
4529bac70b8SBill Paul 		DELAY(10);
4539bac70b8SBill Paul 	}
4549bac70b8SBill Paul }
4559bac70b8SBill Paul 
4569bac70b8SBill Paul #define PHY_SETBIT(x, y, z) \
4579bac70b8SBill Paul 	PHY_WRITE(x, y, (PHY_READ(x, y) | (z)))
4589bac70b8SBill Paul #define PHY_CLRBIT(x, y, z) \
4599bac70b8SBill Paul 	PHY_WRITE(x, y, (PHY_READ(x, y) & ~(z)))
4609bac70b8SBill Paul 
461d5a50459SBill Paul /*
462d5a50459SBill Paul  * Initialize RealTek PHY per the datasheet. The DSP in the PHYs of
463d5a50459SBill Paul  * existing revisions of the 8169S/8110S chips need to be tuned in
464ed510fb0SBill Paul  * order to reliably negotiate a 1000Mbps link. This is only needed
465ed510fb0SBill Paul  * for rev 0 and rev 1 of the PHY. Later versions work without
466ed510fb0SBill Paul  * any fixups.
467d5a50459SBill Paul  */
4689bac70b8SBill Paul static void
rgephy_load_dspcode(struct mii_softc * sc)4699bac70b8SBill Paul rgephy_load_dspcode(struct mii_softc *sc)
4709bac70b8SBill Paul {
4719bac70b8SBill Paul 	int val;
472ed510fb0SBill Paul 
473cd10b400SPyun YongHyeon 	if (sc->mii_mpd_model == MII_MODEL_REALTEK_RTL8251 ||
47472200a8aSPyun YongHyeon 	    sc->mii_mpd_rev >= RGEPHY_8211B)
475ed510fb0SBill Paul 		return;
4769bac70b8SBill Paul 
4779bac70b8SBill Paul 	PHY_WRITE(sc, 31, 0x0001);
4789bac70b8SBill Paul 	PHY_WRITE(sc, 21, 0x1000);
4799bac70b8SBill Paul 	PHY_WRITE(sc, 24, 0x65C7);
4809bac70b8SBill Paul 	PHY_CLRBIT(sc, 4, 0x0800);
4819bac70b8SBill Paul 	val = PHY_READ(sc, 4) & 0xFFF;
4829bac70b8SBill Paul 	PHY_WRITE(sc, 4, val);
4839bac70b8SBill Paul 	PHY_WRITE(sc, 3, 0x00A1);
4849bac70b8SBill Paul 	PHY_WRITE(sc, 2, 0x0008);
4859bac70b8SBill Paul 	PHY_WRITE(sc, 1, 0x1020);
4869bac70b8SBill Paul 	PHY_WRITE(sc, 0, 0x1000);
4879bac70b8SBill Paul 	PHY_SETBIT(sc, 4, 0x0800);
4889bac70b8SBill Paul 	PHY_CLRBIT(sc, 4, 0x0800);
4899bac70b8SBill Paul 	val = (PHY_READ(sc, 4) & 0xFFF) | 0x7000;
4909bac70b8SBill Paul 	PHY_WRITE(sc, 4, val);
4919bac70b8SBill Paul 	PHY_WRITE(sc, 3, 0xFF41);
4929bac70b8SBill Paul 	PHY_WRITE(sc, 2, 0xDE60);
4939bac70b8SBill Paul 	PHY_WRITE(sc, 1, 0x0140);
4949bac70b8SBill Paul 	PHY_WRITE(sc, 0, 0x0077);
4959bac70b8SBill Paul 	val = (PHY_READ(sc, 4) & 0xFFF) | 0xA000;
4969bac70b8SBill Paul 	PHY_WRITE(sc, 4, val);
4979bac70b8SBill Paul 	PHY_WRITE(sc, 3, 0xDF01);
4989bac70b8SBill Paul 	PHY_WRITE(sc, 2, 0xDF20);
4999bac70b8SBill Paul 	PHY_WRITE(sc, 1, 0xFF95);
5009bac70b8SBill Paul 	PHY_WRITE(sc, 0, 0xFA00);
5019bac70b8SBill Paul 	val = (PHY_READ(sc, 4) & 0xFFF) | 0xB000;
5029bac70b8SBill Paul 	PHY_WRITE(sc, 4, val);
5039bac70b8SBill Paul 	PHY_WRITE(sc, 3, 0xFF41);
5049bac70b8SBill Paul 	PHY_WRITE(sc, 2, 0xDE20);
5059bac70b8SBill Paul 	PHY_WRITE(sc, 1, 0x0140);
5069bac70b8SBill Paul 	PHY_WRITE(sc, 0, 0x00BB);
5079bac70b8SBill Paul 	val = (PHY_READ(sc, 4) & 0xFFF) | 0xF000;
5089bac70b8SBill Paul 	PHY_WRITE(sc, 4, val);
5099bac70b8SBill Paul 	PHY_WRITE(sc, 3, 0xDF01);
5109bac70b8SBill Paul 	PHY_WRITE(sc, 2, 0xDF20);
5119bac70b8SBill Paul 	PHY_WRITE(sc, 1, 0xFF95);
5129bac70b8SBill Paul 	PHY_WRITE(sc, 0, 0xBF00);
5139bac70b8SBill Paul 	PHY_SETBIT(sc, 4, 0x0800);
5149bac70b8SBill Paul 	PHY_CLRBIT(sc, 4, 0x0800);
5159bac70b8SBill Paul 	PHY_WRITE(sc, 31, 0x0000);
5169bac70b8SBill Paul 
5179bac70b8SBill Paul 	DELAY(40);
5189bac70b8SBill Paul }
5199bac70b8SBill Paul 
5209bac70b8SBill Paul static void
rgephy_reset(struct mii_softc * sc)5219bac70b8SBill Paul rgephy_reset(struct mii_softc *sc)
5229bac70b8SBill Paul {
523cf402cc9SPyun YongHyeon 	uint16_t pcr, ssr;
524c06cddfeSPyun YongHyeon 
52572200a8aSPyun YongHyeon 	switch (sc->mii_mpd_rev) {
52672200a8aSPyun YongHyeon 	case RGEPHY_8211F:
527*3fe25a9bSPeter Jeremy 	case RGEPHY_8211FVD:
52872200a8aSPyun YongHyeon 		pcr = PHY_READ(sc, RGEPHY_F_MII_PCR1);
5297aa69e31SPyun YongHyeon 		pcr &= ~(RGEPHY_F_PCR1_MDI_MM | RGEPHY_F_PCR1_ALDPS_EN);
53072200a8aSPyun YongHyeon 		PHY_WRITE(sc, RGEPHY_F_MII_PCR1, pcr);
5317aa69e31SPyun YongHyeon 		rgephy_disable_eee(sc);
53272200a8aSPyun YongHyeon 		break;
53372200a8aSPyun YongHyeon 	case RGEPHY_8211C:
53472200a8aSPyun YongHyeon 		if ((sc->mii_flags & MIIF_PHYPRIV0) == 0) {
535c06cddfeSPyun YongHyeon 			/* RTL8211C(L) */
536c06cddfeSPyun YongHyeon 			ssr = PHY_READ(sc, RGEPHY_MII_SSR);
537c06cddfeSPyun YongHyeon 			if ((ssr & RGEPHY_SSR_ALDPS) != 0) {
538c06cddfeSPyun YongHyeon 				ssr &= ~RGEPHY_SSR_ALDPS;
539c06cddfeSPyun YongHyeon 				PHY_WRITE(sc, RGEPHY_MII_SSR, ssr);
540c06cddfeSPyun YongHyeon 			}
541c06cddfeSPyun YongHyeon 		}
542ce18f7cdSPyun YongHyeon 		/* FALLTHROUGH */
54372200a8aSPyun YongHyeon 	default:
54472200a8aSPyun YongHyeon 		if (sc->mii_mpd_rev >= RGEPHY_8211B) {
545cf402cc9SPyun YongHyeon 			pcr = PHY_READ(sc, RGEPHY_MII_PCR);
546cf402cc9SPyun YongHyeon 			if ((pcr & RGEPHY_PCR_MDIX_AUTO) == 0) {
547cf402cc9SPyun YongHyeon 				pcr &= ~RGEPHY_PCR_MDI_MASK;
548cf402cc9SPyun YongHyeon 				pcr |= RGEPHY_PCR_MDIX_AUTO;
549cf402cc9SPyun YongHyeon 				PHY_WRITE(sc, RGEPHY_MII_PCR, pcr);
550cf402cc9SPyun YongHyeon 			}
551cf402cc9SPyun YongHyeon 		}
55272200a8aSPyun YongHyeon 		break;
55372200a8aSPyun YongHyeon 	}
554cf402cc9SPyun YongHyeon 
5559bac70b8SBill Paul 	mii_phy_reset(sc);
5569bac70b8SBill Paul 	DELAY(1000);
5579bac70b8SBill Paul 	rgephy_load_dspcode(sc);
5589bac70b8SBill Paul }
5597aa69e31SPyun YongHyeon 
5607aa69e31SPyun YongHyeon static void
rgephy_disable_eee(struct mii_softc * sc)5617aa69e31SPyun YongHyeon rgephy_disable_eee(struct mii_softc *sc)
5627aa69e31SPyun YongHyeon {
5637aa69e31SPyun YongHyeon 	uint16_t anar;
5647aa69e31SPyun YongHyeon 
5657aa69e31SPyun YongHyeon 	PHY_WRITE(sc, RGEPHY_F_EPAGSR, 0x0000);
5667aa69e31SPyun YongHyeon 	PHY_WRITE(sc, MII_MMDACR, MMDACR_FN_ADDRESS |
5677aa69e31SPyun YongHyeon 	    (MMDACR_DADDRMASK & RGEPHY_F_MMD_DEV_7));
5687aa69e31SPyun YongHyeon 	PHY_WRITE(sc, MII_MMDAADR, RGEPHY_F_MMD_EEEAR);
5697aa69e31SPyun YongHyeon 	PHY_WRITE(sc, MII_MMDACR, MMDACR_FN_DATANPI |
5707aa69e31SPyun YongHyeon 	    (MMDACR_DADDRMASK & RGEPHY_F_MMD_DEV_7));
5717aa69e31SPyun YongHyeon 	PHY_WRITE(sc, MII_MMDAADR, 0x0000);
5727aa69e31SPyun YongHyeon 	PHY_WRITE(sc, MII_MMDACR, 0x0000);
5737aa69e31SPyun YongHyeon 	/*
5747aa69e31SPyun YongHyeon 	 * XXX
5757aa69e31SPyun YongHyeon 	 * Restart auto-negotiation to take changes effect.
5767aa69e31SPyun YongHyeon 	 * This may result in link establishment.
5777aa69e31SPyun YongHyeon 	 */
5787aa69e31SPyun YongHyeon 	anar = BMSR_MEDIA_TO_ANAR(sc->mii_capabilities) | ANAR_CSMA;
5797aa69e31SPyun YongHyeon 	PHY_WRITE(sc, RGEPHY_MII_ANAR, anar);
5807aa69e31SPyun YongHyeon 	PHY_WRITE(sc, RGEPHY_MII_1000CTL, RGEPHY_1000CTL_AHD |
5817aa69e31SPyun YongHyeon 	    RGEPHY_1000CTL_AFD);
5827aa69e31SPyun YongHyeon 	PHY_WRITE(sc, RGEPHY_MII_BMCR, RGEPHY_BMCR_RESET |
5837aa69e31SPyun YongHyeon 	    RGEPHY_BMCR_AUTOEN | RGEPHY_BMCR_STARTNEG);
5847aa69e31SPyun YongHyeon }
585