ruephy.c revision 128870
16485Sache/*-
26485Sache * Copyright (c) 2001-2003, Shunsuke Akiyama <akiyama@FreeBSD.org>.
36485Sache * All rights reserved.
46485Sache *
56485Sache * Redistribution and use in source and binary forms, with or without
6227753Stheraven * modification, are permitted provided that the following conditions
7227753Stheraven * are met:
8227753Stheraven * 1. Redistributions of source code must retain the above copyright
9227753Stheraven *    notice, this list of conditions and the following disclaimer.
10227753Stheraven * 2. Redistributions in binary form must reproduce the above copyright
116485Sache *    notice, this list of conditions and the following disclaimer in the
126485Sache *    documentation and/or other materials provided with the distribution.
136485Sache *
146485Sache * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
156485Sache * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
166485Sache * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
176485Sache * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
186485Sache * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
196485Sache * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
206485Sache * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
216485Sache * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
226485Sache * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
236485Sache * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
246485Sache * SUCH DAMAGE.
256485Sache *
266485Sache */
276485Sache
286485Sache#include <sys/cdefs.h>
296485Sache__FBSDID("$FreeBSD: head/sys/dev/mii/ruephy.c 128870 2004-05-03 13:01:34Z andre $");
306485Sache
316485Sache/*
3250476Speter * driver for RealTek RTL8150 internal PHY
336485Sache */
346485Sache
3588309Sphantom#include <sys/param.h>
3688309Sphantom#include <sys/systm.h>
376485Sache#include <sys/kernel.h>
386485Sache#include <sys/malloc.h>
396485Sache#include <sys/socket.h>
406485Sache#include <sys/bus.h>
41227753Stheraven
426485Sache#include <net/if.h>
436485Sache#include <net/if_arp.h>
446485Sache#include <net/if_media.h>
45102638Sache
46142686Sru#include <dev/mii/mii.h>
476485Sache#include <dev/mii/miivar.h>
486485Sache#include "miidevs.h"
496485Sache
506485Sache#include <machine/bus.h>
516485Sache#include <dev/mii/ruephyreg.h>
526485Sache
536485Sache#include "miibus_if.h"
546485Sache
556485Sachestatic int ruephy_probe(device_t);
56101820Sachestatic int ruephy_attach(device_t);
57101820Sache
586485Sachestatic device_method_t ruephy_methods[] = {
59227753Stheraven	/* device interface */
60227753Stheraven	DEVMETHOD(device_probe,		ruephy_probe),
61227753Stheraven	DEVMETHOD(device_attach,	ruephy_attach),
62227753Stheraven	DEVMETHOD(device_detach,	mii_phy_detach),
63227753Stheraven	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
64227753Stheraven	{ 0, 0 }
65227753Stheraven};
66227753Stheraven
67227753Stheravenstatic devclass_t ruephy_devclass;
68227753Stheraven
69227753Stheravenstatic driver_t ruephy_driver = {
706485Sache	"ruephy",
7192905Sobrien	ruephy_methods,
72227753Stheraven	sizeof(struct mii_softc)
73101498Sache};
74227753Stheraven
75227753StheravenDRIVER_MODULE(ruephy, miibus, ruephy_driver, ruephy_devclass, 0, 0);
766485Sache
7792905Sobrienstatic int ruephy_service(struct mii_softc *, struct mii_data *, int);
786485Sachestatic void ruephy_reset(struct mii_softc *);
796485Sachestatic void ruephy_status(struct mii_softc *);
806485Sache
8188309Sphantomstatic int
82ruephy_probe(device_t dev)
83{
84	struct mii_attach_args *ma;
85	device_t		parent;
86
87	ma = device_get_ivars(dev);
88	parent = device_get_parent(device_get_parent(dev));
89
90	/*
91	 * RealTek RTL8150 PHY doesn't have vendor/device ID registers:
92	 * the rue driver fakes up a return value of all zeros.
93	 */
94	if (MII_OUI(ma->mii_id1, ma->mii_id2) != 0 ||
95	    MII_MODEL(ma->mii_id2) != 0)
96		return (ENXIO);
97
98	/*
99	 * Make sure the parent is an 'rue'.
100	 */
101	if (strcmp(device_get_name(parent), "rue") != 0)
102		return (ENXIO);
103
104	device_set_desc(dev, "RealTek RTL8150 internal media interface");
105
106	return (0);
107}
108
109static int
110ruephy_attach(device_t dev)
111{
112	struct mii_softc	*sc;
113	struct mii_attach_args	*ma;
114	struct mii_data		*mii;
115
116	sc = device_get_softc(dev);
117	ma = device_get_ivars(dev);
118	sc->mii_dev = device_get_parent(dev);
119	mii = device_get_softc(sc->mii_dev);
120
121	/*
122	 * The RealTek PHY can never be isolated, so never allow non-zero
123	 * instances!
124	 */
125	if (mii->mii_instance != 0) {
126		device_printf(dev, "ignoring this PHY, non-zero instance\n");
127		return (ENXIO);
128	}
129
130	LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
131
132	sc->mii_inst = mii->mii_instance;
133	sc->mii_phy = ma->mii_phyno;
134	sc->mii_service = ruephy_service;
135	sc->mii_pdata = mii;
136	mii->mii_instance++;
137
138	sc->mii_flags |= MIIF_NOISOLATE;
139
140#define	ADD(m, c)	ifmedia_add(&mii->mii_media, (m), (c), NULL)
141
142	ruephy_reset(sc);
143
144	sc->mii_capabilities =
145	    PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
146	device_printf(dev, " ");
147	mii_phy_add_media(sc);
148	printf("\n");
149#undef ADD
150
151	MIIBUS_MEDIAINIT(sc->mii_dev);
152	return (0);
153}
154
155static int
156ruephy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
157{
158	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
159	int reg;
160
161	/*
162	 * We can't isolate the RealTek RTL8150 PHY,
163	 * so it has to be the only one!
164	 */
165	if (IFM_INST(ife->ifm_media) != sc->mii_inst)
166		panic("ruephy_service: can't isolate RealTek RTL8150 PHY");
167
168	switch (cmd) {
169	case MII_POLLSTAT:
170		break;
171
172	case MII_MEDIACHG:
173		/*
174		 * If the interface is not up, don't do anything.
175		 */
176		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
177			break;
178
179		switch (IFM_SUBTYPE(ife->ifm_media)) {
180		case IFM_AUTO:
181			/*
182			 * If we're already in auto mode, just return.
183			 */
184			if (PHY_READ(sc, MII_BMCR) & BMCR_AUTOEN)
185				return (0);
186			(void) mii_phy_auto(sc);
187			break;
188		case IFM_100_T4:
189			/*
190			 * XXX Not supported as a manual setting right now.
191			 */
192			return (EINVAL);
193		default:
194			/*
195			 * BMCR data is stored in the ifmedia entry.
196			 */
197			PHY_WRITE(sc, MII_ANAR,
198			    mii_anar(ife->ifm_media));
199			PHY_WRITE(sc, MII_BMCR, ife->ifm_data);
200		}
201		break;
202
203	case MII_TICK:
204		/*
205		 * Is the interface even up?
206		 */
207		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
208			return (0);
209
210		/*
211		 * Only used for autonegotiation.
212		 */
213		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
214			break;
215
216		/*
217		 * Check to see if we have link.  If we do, we don't
218		 * need to restart the autonegotiation process.  Read
219		 * the MSR twice in case it's latched.
220		 */
221		reg = PHY_READ(sc, RUEPHY_MII_MSR) |
222		      PHY_READ(sc, RUEPHY_MII_MSR);
223		if (reg & RUEPHY_MSR_LINK)
224			break;
225
226		/*
227		 * Only retry autonegotiation every 5 seconds.
228		 */
229		if (++sc->mii_ticks <= 5)
230			break;
231
232		sc->mii_ticks = 0;
233		ruephy_reset(sc);
234		if (mii_phy_auto(sc) == EJUSTRETURN)
235			return (0);
236		break;
237	}
238
239	/* Update the media status. */
240	ruephy_status(sc);
241
242	/* Callback if something changed. */
243	mii_phy_update(sc, cmd);
244
245	return (0);
246}
247
248static void
249ruephy_reset(struct mii_softc *sc)
250{
251
252	mii_phy_reset(sc);
253
254	/*
255	 * XXX RealTek RTL8150 PHY doesn't set the BMCR properly after
256	 * XXX reset, which breaks autonegotiation.
257	 */
258	PHY_WRITE(sc, MII_BMCR, (BMCR_S100 | BMCR_AUTOEN | BMCR_FDX));
259}
260
261static void
262ruephy_status(struct mii_softc *phy)
263{
264	struct mii_data *mii = phy->mii_pdata;
265	int bmsr, bmcr, msr;
266
267	mii->mii_media_status = IFM_AVALID;
268	mii->mii_media_active = IFM_ETHER;
269
270	msr = PHY_READ(phy, RUEPHY_MII_MSR) | PHY_READ(phy, RUEPHY_MII_MSR);
271	if (msr & RUEPHY_MSR_LINK)
272		mii->mii_media_status |= IFM_ACTIVE;
273
274	bmcr = PHY_READ(phy, MII_BMCR);
275	if (bmcr & BMCR_ISO) {
276		mii->mii_media_active |= IFM_NONE;
277		mii->mii_media_status = 0;
278		return;
279	}
280
281	bmsr = PHY_READ(phy, MII_BMSR) | PHY_READ(phy, MII_BMSR);
282
283	if (bmcr & BMCR_AUTOEN) {
284		if ((bmsr & BMSR_ACOMP) == 0) {
285			/* Erg, still trying, I guess... */
286			mii->mii_media_active |= IFM_NONE;
287			return;
288		}
289
290		if (msr & RUEPHY_MSR_SPEED100)
291			mii->mii_media_active |= IFM_100_TX;
292		else
293			mii->mii_media_active |= IFM_10_T;
294
295		if (msr & RUEPHY_MSR_DUPLEX)
296			mii->mii_media_active |= IFM_FDX;
297	} else
298		mii->mii_media_active = mii_media_from_bmcr(bmcr);
299}
300