1139749Simp/*-
250702Swpaul * Copyright (c) 1997, 1998, 1999
350702Swpaul *	Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
450702Swpaul *
550702Swpaul * Redistribution and use in source and binary forms, with or without
650702Swpaul * modification, are permitted provided that the following conditions
750702Swpaul * are met:
850702Swpaul * 1. Redistributions of source code must retain the above copyright
950702Swpaul *    notice, this list of conditions and the following disclaimer.
1050702Swpaul * 2. Redistributions in binary form must reproduce the above copyright
1150702Swpaul *    notice, this list of conditions and the following disclaimer in the
1250702Swpaul *    documentation and/or other materials provided with the distribution.
1350702Swpaul * 3. All advertising materials mentioning features or use of this software
1450702Swpaul *    must display the following acknowledgement:
1550702Swpaul *	This product includes software developed by Bill Paul.
1650702Swpaul * 4. Neither the name of the author nor the names of any co-contributors
1750702Swpaul *    may be used to endorse or promote products derived from this software
1850702Swpaul *    without specific prior written permission.
1950702Swpaul *
2050702Swpaul * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
2150702Swpaul * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2250702Swpaul * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2350702Swpaul * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
2450702Swpaul * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2550702Swpaul * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2650702Swpaul * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2750702Swpaul * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2850702Swpaul * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2950702Swpaul * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
3050702Swpaul * THE POSSIBILITY OF SUCH DAMAGE.
3150702Swpaul */
3250702Swpaul
33119418Sobrien#include <sys/cdefs.h>
34119418Sobrien__FBSDID("$FreeBSD$");
35119418Sobrien
3650702Swpaul/*
3750702Swpaul * driver for RealTek 8139 internal PHYs
3850702Swpaul */
3950702Swpaul
4050702Swpaul#include <sys/param.h>
4150702Swpaul#include <sys/systm.h>
4250702Swpaul#include <sys/kernel.h>
43129876Sphk#include <sys/module.h>
4450702Swpaul#include <sys/socket.h>
4550702Swpaul#include <sys/bus.h>
4650702Swpaul
4750702Swpaul#include <net/if.h>
4894149Swpaul#include <net/if_arp.h>
4950702Swpaul#include <net/if_media.h>
5050702Swpaul
5150702Swpaul#include <dev/mii/mii.h>
5250702Swpaul#include <dev/mii/miivar.h>
53109514Sobrien#include "miidevs.h"
5450702Swpaul
5594149Swpaul#include <machine/bus.h>
5694149Swpaul#include <pci/if_rlreg.h>
5794149Swpaul
5850702Swpaul#include "miibus_if.h"
5950702Swpaul
60105135Salfredstatic int rlphy_probe(device_t);
61105135Salfredstatic int rlphy_attach(device_t);
6250702Swpaul
6350702Swpaulstatic device_method_t rlphy_methods[] = {
6450702Swpaul	/* device interface */
6550702Swpaul	DEVMETHOD(device_probe,		rlphy_probe),
6650702Swpaul	DEVMETHOD(device_attach,	rlphy_attach),
6795722Sphk	DEVMETHOD(device_detach,	mii_phy_detach),
6850702Swpaul	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
69227908Smarius	DEVMETHOD_END
7050702Swpaul};
7150702Swpaul
7250702Swpaulstatic devclass_t rlphy_devclass;
7350702Swpaul
7450702Swpaulstatic driver_t rlphy_driver = {
7550702Swpaul	"rlphy",
7650702Swpaul	rlphy_methods,
77221407Smarius	sizeof(struct mii_softc)
7850702Swpaul};
7950702Swpaul
8050702SwpaulDRIVER_MODULE(rlphy, miibus, rlphy_driver, rlphy_devclass, 0, 0);
8150702Swpaul
8292739Salfredstatic int	rlphy_service(struct mii_softc *, struct mii_data *, int);
8394149Swpaulstatic void	rlphy_status(struct mii_softc *);
8450702Swpaul
85165991Smarius/*
86165991Smarius * RealTek internal PHYs don't have vendor/device ID registers;
87165991Smarius * re(4) and rl(4) fake up a return value of all zeros.
88165991Smarius */
89165991Smariusstatic const struct mii_phydesc rlintphys[] = {
90165991Smarius	{ 0, 0, "RealTek internal media interface" },
91165991Smarius	MII_PHY_END
92165991Smarius};
93165991Smarius
94164827Smariusstatic const struct mii_phydesc rlphys[] = {
95221407Smarius	MII_PHY_DESC(yyREALTEK, RTL8201L),
96221407Smarius	MII_PHY_DESC(REALTEK, RTL8201E),
97221407Smarius	MII_PHY_DESC(xxICPLUS, IP101),
98164827Smarius	MII_PHY_END
99164827Smarius};
100164827Smarius
101221407Smariusstatic const struct mii_phy_funcs rlphy_funcs = {
102221407Smarius	rlphy_service,
103221407Smarius	rlphy_status,
104221407Smarius	mii_phy_reset
105221407Smarius};
106221407Smarius
107105135Salfredstatic int
108150763Simprlphy_probe(device_t dev)
10950702Swpaul{
110164827Smarius	const char *nic;
111164827Smarius	int rv;
11250702Swpaul
113164827Smarius	rv = mii_phy_dev_probe(dev, rlphys, BUS_PROBE_DEFAULT);
114164827Smarius	if (rv <= 0)
115164827Smarius		return (rv);
11650702Swpaul
117164827Smarius	nic = device_get_name(device_get_parent(device_get_parent(dev)));
118166164Smarius	if (strcmp(nic, "rl") == 0 || strcmp(nic, "re") == 0)
119165991Smarius		return (mii_phy_dev_probe(dev, rlintphys, BUS_PROBE_DEFAULT));
120165991Smarius	return (ENXIO);
12150702Swpaul}
12250702Swpaul
123105135Salfredstatic int
124150763Simprlphy_attach(device_t dev)
12550702Swpaul{
12650702Swpaul
127213364Smarius	/*
128213364Smarius	 * The RealTek PHY can never be isolated.
129213364Smarius	 */
130221407Smarius	mii_phy_dev_attach(dev, MIIF_NOISOLATE | MIIF_NOMANPAUSE,
131221407Smarius	    &rlphy_funcs, 1);
132164711Smarius	return (0);
13350702Swpaul}
13450702Swpaul
13584145Sjlemonstatic int
136150763Simprlphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
13750702Swpaul{
13850702Swpaul
13950702Swpaul	switch (cmd) {
14050702Swpaul	case MII_POLLSTAT:
14150702Swpaul		break;
14250702Swpaul
14350702Swpaul	case MII_MEDIACHG:
14450702Swpaul		/*
14550702Swpaul		 * If the interface is not up, don't do anything.
14650702Swpaul		 */
14750702Swpaul		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
14850702Swpaul			break;
14950702Swpaul
150164711Smarius		mii_phy_setmedia(sc);
15150702Swpaul		break;
15250702Swpaul
15350702Swpaul	case MII_TICK:
15450702Swpaul		/*
15550702Swpaul		 * Is the interface even up?
15650702Swpaul		 */
15750702Swpaul		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
15850702Swpaul			return (0);
15950702Swpaul
16050702Swpaul		/*
16150702Swpaul		 * The RealTek PHY's autonegotiation doesn't need to be
16250702Swpaul		 * kicked; it continues in the background.
16350702Swpaul		 */
16450702Swpaul		break;
16550702Swpaul	}
16650702Swpaul
16750702Swpaul	/* Update the media status. */
168221407Smarius	PHY_STATUS(sc);
16950702Swpaul
17050702Swpaul	/* Callback if something changed. */
17184145Sjlemon	mii_phy_update(sc, cmd);
17250702Swpaul	return (0);
17350702Swpaul}
17494149Swpaul
175104094Sphkstatic void
176150763Simprlphy_status(struct mii_softc *phy)
17794149Swpaul{
17894149Swpaul	struct mii_data *mii = phy->mii_pdata;
179164711Smarius	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
18094149Swpaul	int bmsr, bmcr, anlpar;
18194149Swpaul
18294149Swpaul	mii->mii_media_status = IFM_AVALID;
18394149Swpaul	mii->mii_media_active = IFM_ETHER;
18494149Swpaul
18594149Swpaul	bmsr = PHY_READ(phy, MII_BMSR) | PHY_READ(phy, MII_BMSR);
18694149Swpaul	if (bmsr & BMSR_LINK)
18794149Swpaul		mii->mii_media_status |= IFM_ACTIVE;
18894149Swpaul
18994149Swpaul	bmcr = PHY_READ(phy, MII_BMCR);
19094149Swpaul	if (bmcr & BMCR_ISO) {
19194149Swpaul		mii->mii_media_active |= IFM_NONE;
19294149Swpaul		mii->mii_media_status = 0;
19394149Swpaul		return;
19494149Swpaul	}
19594149Swpaul
19694149Swpaul	if (bmcr & BMCR_LOOP)
19794149Swpaul		mii->mii_media_active |= IFM_LOOP;
19894149Swpaul
19994149Swpaul	if (bmcr & BMCR_AUTOEN) {
20094149Swpaul		/*
20194149Swpaul		 * NWay autonegotiation takes the highest-order common
20294149Swpaul		 * bit of the ANAR and ANLPAR (i.e. best media advertised
20394149Swpaul		 * both by us and our link partner).
20494149Swpaul		 */
20594149Swpaul		if ((bmsr & BMSR_ACOMP) == 0) {
20694149Swpaul			/* Erg, still trying, I guess... */
20794149Swpaul			mii->mii_media_active |= IFM_NONE;
20894149Swpaul			return;
20994149Swpaul		}
21094149Swpaul
21194149Swpaul		if ((anlpar = PHY_READ(phy, MII_ANAR) &
21294149Swpaul		    PHY_READ(phy, MII_ANLPAR))) {
213173665Syongari			if (anlpar & ANLPAR_TX_FD)
214173665Syongari				mii->mii_media_active |= IFM_100_TX|IFM_FDX;
215173665Syongari			else if (anlpar & ANLPAR_T4)
216213384Smarius				mii->mii_media_active |= IFM_100_T4|IFM_HDX;
21794149Swpaul			else if (anlpar & ANLPAR_TX)
218213384Smarius				mii->mii_media_active |= IFM_100_TX|IFM_HDX;
21994149Swpaul			else if (anlpar & ANLPAR_10_FD)
22094149Swpaul				mii->mii_media_active |= IFM_10_T|IFM_FDX;
22194149Swpaul			else if (anlpar & ANLPAR_10)
222213384Smarius				mii->mii_media_active |= IFM_10_T|IFM_HDX;
22394149Swpaul			else
224164711Smarius				mii->mii_media_active |= IFM_NONE;
225221407Smarius			if ((mii->mii_media_active & IFM_FDX) != 0)
226221407Smarius				mii->mii_media_active |=
227221407Smarius				    mii_phy_flowstatus(phy);
22894149Swpaul			return;
22994149Swpaul		}
23094149Swpaul		/*
23194149Swpaul		 * If the other side doesn't support NWAY, then the
23294149Swpaul		 * best we can do is determine if we have a 10Mbps or
233164711Smarius		 * 100Mbps link. There's no way to know if the link
23494149Swpaul		 * is full or half duplex, so we default to half duplex
23594149Swpaul		 * and hope that the user is clever enough to manually
23694149Swpaul		 * change the media settings if we're wrong.
23794149Swpaul		 */
23894149Swpaul
23994149Swpaul		/*
24094149Swpaul		 * The RealTek PHY supports non-NWAY link speed
24194149Swpaul		 * detection, however it does not report the link
24294149Swpaul		 * detection results via the ANLPAR or BMSR registers.
24394149Swpaul		 * (What? RealTek doesn't do things the way everyone
24494149Swpaul		 * else does? I'm just shocked, shocked I tell you.)
24594149Swpaul		 * To determine the link speed, we have to do one
24694149Swpaul		 * of two things:
24794149Swpaul		 *
248221407Smarius		 * - If this is a standalone RealTek RTL8201(L) or
249221407Smarius		 *   workalike PHY, we can determine the link speed by
250221407Smarius		 *   testing bit 0 in the magic, vendor-specific register
251221407Smarius		 *   at offset 0x19.
25294149Swpaul		 *
25394149Swpaul		 * - If this is a RealTek MAC with integrated PHY, we
25494149Swpaul		 *   can test the 'SPEED10' bit of the MAC's media status
25594149Swpaul		 *   register.
25694149Swpaul		 */
257221407Smarius		if (!(phy->mii_mpd_model == 0 && phy->mii_mpd_rev == 0)) {
25894149Swpaul			if (PHY_READ(phy, 0x0019) & 0x01)
25994149Swpaul				mii->mii_media_active |= IFM_100_TX;
26094149Swpaul			else
26194149Swpaul				mii->mii_media_active |= IFM_10_T;
26294149Swpaul		} else {
26394149Swpaul			if (PHY_READ(phy, RL_MEDIASTAT) &
26494149Swpaul			    RL_MEDIASTAT_SPEED10)
26594149Swpaul				mii->mii_media_active |= IFM_10_T;
26694149Swpaul			else
26794149Swpaul				mii->mii_media_active |= IFM_100_TX;
26894149Swpaul		}
269213384Smarius		mii->mii_media_active |= IFM_HDX;
27094149Swpaul	} else
271164711Smarius		mii->mii_media_active = ife->ifm_media;
27294149Swpaul}
273