xref: /dragonfly/sys/dev/netif/mii_layer/ruephy.c (revision e3146d3a)
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  * $FreeBSD: src/sys/dev/mii/ruephy.c,v 1.1.4.1 2003/07/30 13:57:35 akiyama Exp $
27  * $DragonFly: src/sys/dev/netif/mii_layer/ruephy.c,v 1.1 2005/09/19 02:53:27 sephe Exp $
28  */
29 
30 /*
31  * driver for RealTek RTL8150 internal PHY
32  */
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/socket.h>
39 #include <sys/bus.h>
40 
41 #include <net/if.h>
42 #include <net/if_media.h>
43 
44 #include <dev/netif/mii_layer/mii.h>
45 #include <dev/netif/mii_layer/miivar.h>
46 #include <dev/netif/mii_layer/ruephyreg.h>
47 
48 #include "miibus_if.h"
49 
50 static int ruephy_probe(device_t);
51 static int ruephy_attach(device_t);
52 static int ruephy_detach(device_t);
53 
54 static device_method_t ruephy_methods[] = {
55 	/* device interface */
56 	DEVMETHOD(device_probe,		ruephy_probe),
57 	DEVMETHOD(device_attach,	ruephy_attach),
58 	DEVMETHOD(device_detach,	ruephy_detach),
59 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
60 	{ 0, 0 }
61 };
62 
63 static devclass_t ruephy_devclass;
64 
65 static driver_t ruephy_driver = {
66 	"ruephy",
67 	ruephy_methods,
68 	sizeof(struct mii_softc)
69 };
70 
71 DRIVER_MODULE(ruephy, miibus, ruephy_driver, ruephy_devclass, 0, 0);
72 
73 static int ruephy_service(struct mii_softc *, struct mii_data *, int);
74 static void ruephy_reset(struct mii_softc *);
75 static void ruephy_status(struct mii_softc *);
76 
77 static int
78 ruephy_probe(device_t dev)
79 {
80 	struct mii_attach_args *ma;
81 	device_t		parent;
82 
83 	ma = device_get_ivars(dev);
84 	parent = device_get_parent(device_get_parent(dev));
85 
86 	/*
87 	 * RealTek RTL8150 PHY doesn't have vendor/device ID registers:
88 	 * the rue driver fakes up a return value of all zeros.
89 	 */
90 	if (MII_OUI(ma->mii_id1, ma->mii_id2) != 0 ||
91 	    MII_MODEL(ma->mii_id2) != 0)
92 		return (ENXIO);
93 
94 	/*
95 	 * Make sure the parent is an 'rue'.
96 	 */
97 	if (strcmp(device_get_name(parent), "rue") != 0)
98 		return (ENXIO);
99 
100 	device_set_desc(dev, "RealTek RTL8150 internal media interface");
101 
102 	return (0);
103 }
104 
105 static int
106 ruephy_attach(device_t dev)
107 {
108 	struct mii_softc	*sc;
109 	struct mii_attach_args	*ma;
110 	struct mii_data		*mii;
111 
112 	sc = device_get_softc(dev);
113 	ma = device_get_ivars(dev);
114 	mii_softc_init(sc);
115 	sc->mii_dev = device_get_parent(dev);
116 	mii = device_get_softc(sc->mii_dev);
117 
118 	/*
119 	 * The RealTek PHY can never be isolated, so never allow non-zero
120 	 * instances!
121 	 */
122 	if (mii->mii_instance != 0) {
123 		device_printf(dev, "ignoring this PHY, non-zero instance\n");
124 		return (ENXIO);
125 	}
126 
127 	LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
128 
129 	sc->mii_inst = mii->mii_instance;
130 	sc->mii_phy = ma->mii_phyno;
131 	sc->mii_service = ruephy_service;
132 	sc->mii_pdata = mii;
133 	mii->mii_instance++;
134 
135 	sc->mii_flags |= MIIF_NOISOLATE;
136 
137 #define	ADD(m, c)	ifmedia_add(&mii->mii_media, (m), (c), NULL)
138 
139 	ruephy_reset(sc);
140 
141 	sc->mii_capabilities =
142 	    PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
143 	device_printf(dev, " ");
144 	if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0)
145 		printf("no media present");
146 	else
147 		mii_add_media(mii, sc->mii_capabilities, sc->mii_inst);
148 	printf("\n");
149 
150 #undef ADD
151 
152 	MIIBUS_MEDIAINIT(sc->mii_dev);
153 	return (0);
154 }
155 
156 static int
157 ruephy_detach(device_t dev)
158 {
159 	struct mii_softc	*sc;
160 	struct mii_data		*mii;
161 
162 	sc = device_get_softc(dev);
163 	mii = device_get_softc(device_get_softc(dev));
164 	mii_phy_auto_stop(sc);
165 	sc->mii_dev = NULL;
166 	LIST_REMOVE(sc, mii_list);
167 
168 	return (0);
169 }
170 
171 static int
172 ruephy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
173 {
174 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
175 	int reg;
176 
177 	/*
178 	 * We can't isolate the RealTek RTL8150 PHY,
179 	 * so it has to be the only one!
180 	 */
181 	if (IFM_INST(ife->ifm_media) != sc->mii_inst)
182 		panic("ruephy_service: can't isolate RealTek RTL8150 PHY");
183 
184 	switch (cmd) {
185 	case MII_POLLSTAT:
186 		break;
187 
188 	case MII_MEDIACHG:
189 		/*
190 		 * If the interface is not up, don't do anything.
191 		 */
192 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
193 			break;
194 
195 		switch (IFM_SUBTYPE(ife->ifm_media)) {
196 		case IFM_AUTO:
197 			/*
198 			 * If we're already in auto mode, just return.
199 			 */
200 			if (PHY_READ(sc, MII_BMCR) & BMCR_AUTOEN)
201 				return (0);
202 			(void) mii_phy_auto(sc, 1);
203 			break;
204 		case IFM_100_T4:
205 			/*
206 			 * XXX Not supported as a manual setting right now.
207 			 */
208 			return (EINVAL);
209 		default:
210 			/*
211 			 * BMCR data is stored in the ifmedia entry.
212 			 */
213 			PHY_WRITE(sc, MII_ANAR,
214 			    mii_anar(ife->ifm_media));
215 			PHY_WRITE(sc, MII_BMCR, ife->ifm_data);
216 		}
217 		break;
218 
219 	case MII_TICK:
220 		/*
221 		 * Is the interface even up?
222 		 */
223 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
224 			return (0);
225 
226 		/*
227 		 * Only used for autonegotiation.
228 		 */
229 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
230 			break;
231 
232 		/*
233 		 * Check to see if we have link.  If we do, we don't
234 		 * need to restart the autonegotiation process.  Read
235 		 * the MSR twice in case it's latched.
236 		 */
237 		reg = PHY_READ(sc, RUEPHY_MII_MSR) |
238 		      PHY_READ(sc, RUEPHY_MII_MSR);
239 		if (reg & RUEPHY_MSR_LINK)
240 			break;
241 
242 		/*
243 		 * Only retry autonegotiation every 5 seconds.
244 		 */
245 		if (++sc->mii_ticks != 5)
246 			return (0);
247 
248 		sc->mii_ticks = 0;
249 		ruephy_reset(sc);
250 		if (mii_phy_auto(sc, 0) == EJUSTRETURN)
251 			return (0);
252 		break;
253 	}
254 
255 	/* Update the media status. */
256 	ruephy_status(sc);
257 
258 	/* Callback if something changed. */
259 	if (sc->mii_active != mii->mii_media_active || cmd == MII_MEDIACHG) {
260 		MIIBUS_STATCHG(sc->mii_dev);
261 		sc->mii_active = mii->mii_media_active;
262 	}
263 
264 	return (0);
265 }
266 
267 static void
268 ruephy_reset(struct mii_softc *sc)
269 {
270 
271 	mii_phy_reset(sc);
272 
273 	/*
274 	 * XXX RealTek RTL8150 PHY doesn't set the BMCR properly after
275 	 * XXX reset, which breaks autonegotiation.
276 	 */
277 	PHY_WRITE(sc, MII_BMCR, (BMCR_S100 | BMCR_AUTOEN | BMCR_FDX));
278 }
279 
280 static void
281 ruephy_status(struct mii_softc *phy)
282 {
283 	struct mii_data *mii = phy->mii_pdata;
284 	int bmsr, bmcr, msr;
285 
286 	mii->mii_media_status = IFM_AVALID;
287 	mii->mii_media_active = IFM_ETHER;
288 
289 	msr = PHY_READ(phy, RUEPHY_MII_MSR) | PHY_READ(phy, RUEPHY_MII_MSR);
290 	if (msr & RUEPHY_MSR_LINK)
291 		mii->mii_media_status |= IFM_ACTIVE;
292 
293 	bmcr = PHY_READ(phy, MII_BMCR);
294 	if (bmcr & BMCR_ISO) {
295 		mii->mii_media_active |= IFM_NONE;
296 		mii->mii_media_status = 0;
297 		return;
298 	}
299 
300 	bmsr = PHY_READ(phy, MII_BMSR) | PHY_READ(phy, MII_BMSR);
301 
302 	if (bmcr & BMCR_AUTOEN) {
303 		if ((bmsr & BMSR_ACOMP) == 0) {
304 			/* Erg, still trying, I guess... */
305 			mii->mii_media_active |= IFM_NONE;
306 			return;
307 		}
308 
309 		if (msr & RUEPHY_MSR_SPEED100)
310 			mii->mii_media_active |= IFM_100_TX;
311 		else
312 			mii->mii_media_active |= IFM_10_T;
313 
314 		if (msr & RUEPHY_MSR_DUPLEX)
315 			mii->mii_media_active |= IFM_FDX;
316 	} else
317 		mii->mii_media_active = mii_media_from_bmcr(bmcr);
318 }
319