1/*-
2 * Copyright (c) 2001-2003, Shunsuke Akiyama <akiyama@FreeBSD.org>.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 */
27
28#include <sys/cdefs.h>
29__FBSDID("$FreeBSD$");
30
31/*
32 * driver for RealTek RTL8150 internal PHY
33 */
34
35#include <sys/param.h>
36#include <sys/systm.h>
37#include <sys/kernel.h>
38#include <sys/malloc.h>
39#include <sys/module.h>
40#include <sys/socket.h>
41#include <sys/bus.h>
42
43#include <net/if.h>
44#include <net/if_arp.h>
45#include <net/if_media.h>
46
47#include <dev/mii/mii.h>
48#include <dev/mii/miivar.h>
49#include "miidevs.h"
50
51#include <dev/usb/net/ruephyreg.h>
52
53#include "miibus_if.h"
54
55static int ruephy_probe(device_t);
56static int ruephy_attach(device_t);
57
58static device_method_t ruephy_methods[] = {
59	/* device interface */
60	DEVMETHOD(device_probe,		ruephy_probe),
61	DEVMETHOD(device_attach,	ruephy_attach),
62	DEVMETHOD(device_detach,	mii_phy_detach),
63	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
64	DEVMETHOD_END
65};
66
67static devclass_t ruephy_devclass;
68
69static driver_t ruephy_driver = {
70	.name = "ruephy",
71	.methods = ruephy_methods,
72	.size = sizeof(struct mii_softc)
73};
74
75DRIVER_MODULE(ruephy, miibus, ruephy_driver, ruephy_devclass, 0, 0);
76
77static int ruephy_service(struct mii_softc *, struct mii_data *, int);
78static void ruephy_reset(struct mii_softc *);
79static void ruephy_status(struct mii_softc *);
80
81/*
82 * The RealTek RTL8150 internal PHY doesn't have vendor/device ID
83 * registers; rue(4) fakes up a return value of all zeros.
84 */
85static const struct mii_phydesc ruephys[] = {
86	{ 0, 0, "RealTek RTL8150 internal media interface" },
87	MII_PHY_END
88};
89
90static const struct mii_phy_funcs ruephy_funcs = {
91	ruephy_service,
92	ruephy_status,
93	ruephy_reset
94};
95
96static int
97ruephy_probe(device_t dev)
98{
99
100	if (strcmp(device_get_name(device_get_parent(device_get_parent(dev))),
101	    "rue") == 0)
102		return (mii_phy_dev_probe(dev, ruephys, BUS_PROBE_DEFAULT));
103	return (ENXIO);
104}
105
106static int
107ruephy_attach(device_t dev)
108{
109
110	mii_phy_dev_attach(dev, MIIF_NOISOLATE | MIIF_NOMANPAUSE,
111	    &ruephy_funcs, 1);
112	return (0);
113}
114
115static int
116ruephy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
117{
118	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
119	int reg;
120
121	switch (cmd) {
122	case MII_POLLSTAT:
123		break;
124
125	case MII_MEDIACHG:
126		/*
127		 * If the interface is not up, don't do anything.
128		 */
129		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
130			break;
131
132		mii_phy_setmedia(sc);
133		break;
134
135	case MII_TICK:
136		/*
137		 * Is the interface even up?
138		 */
139		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
140			return (0);
141
142		/*
143		 * Only used for autonegotiation.
144		 */
145		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
146			break;
147
148		/*
149		 * Check to see if we have link.  If we do, we don't
150		 * need to restart the autonegotiation process.  Read
151		 * the MSR twice in case it's latched.
152		 */
153		reg = PHY_READ(sc, RUEPHY_MII_MSR) |
154		    PHY_READ(sc, RUEPHY_MII_MSR);
155		if (reg & RUEPHY_MSR_LINK)
156			break;
157
158		/* Only retry autonegotiation every mii_anegticks seconds. */
159		if (sc->mii_ticks <= sc->mii_anegticks)
160			break;
161
162		sc->mii_ticks = 0;
163		PHY_RESET(sc);
164		if (mii_phy_auto(sc) == EJUSTRETURN)
165			return (0);
166		break;
167	}
168
169	/* Update the media status. */
170	PHY_STATUS(sc);
171
172	/* Callback if something changed. */
173	mii_phy_update(sc, cmd);
174
175	return (0);
176}
177
178static void
179ruephy_reset(struct mii_softc *sc)
180{
181
182	mii_phy_reset(sc);
183
184	/*
185	 * XXX RealTek RTL8150 PHY doesn't set the BMCR properly after
186	 * XXX reset, which breaks autonegotiation.
187	 */
188	PHY_WRITE(sc, MII_BMCR, (BMCR_S100 | BMCR_AUTOEN | BMCR_FDX));
189}
190
191static void
192ruephy_status(struct mii_softc *phy)
193{
194	struct mii_data *mii = phy->mii_pdata;
195	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
196	int bmsr, bmcr, msr;
197
198	mii->mii_media_status = IFM_AVALID;
199	mii->mii_media_active = IFM_ETHER;
200
201	msr = PHY_READ(phy, RUEPHY_MII_MSR) | PHY_READ(phy, RUEPHY_MII_MSR);
202	if (msr & RUEPHY_MSR_LINK)
203		mii->mii_media_status |= IFM_ACTIVE;
204
205	bmcr = PHY_READ(phy, MII_BMCR);
206	if (bmcr & BMCR_ISO) {
207		mii->mii_media_active |= IFM_NONE;
208		mii->mii_media_status = 0;
209		return;
210	}
211
212	bmsr = PHY_READ(phy, MII_BMSR) | PHY_READ(phy, MII_BMSR);
213	if (bmcr & BMCR_AUTOEN) {
214		if ((bmsr & BMSR_ACOMP) == 0) {
215			/* Erg, still trying, I guess... */
216			mii->mii_media_active |= IFM_NONE;
217			return;
218		}
219
220		if (msr & RUEPHY_MSR_SPEED100)
221			mii->mii_media_active |= IFM_100_TX;
222		else
223			mii->mii_media_active |= IFM_10_T;
224
225		if (msr & RUEPHY_MSR_DUPLEX)
226			mii->mii_media_active |=
227			    IFM_FDX | mii_phy_flowstatus(phy);
228		else
229			mii->mii_media_active |= IFM_HDX;
230	} else
231		mii->mii_media_active = ife->ifm_media;
232}
233