xref: /freebsd/sys/dev/mii/xmphy.c (revision 6780ab54325a71e7e70112b11657973edde8655e)
1 /*
2  * Copyright (c) 2000
3  *	Bill Paul <wpaul@ee.columbia.edu>.  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  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by Bill Paul.
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30  * THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * $FreeBSD$
33  */
34 
35 /*
36  * driver for the XaQti XMAC II's internal PHY. This is sort of
37  * like a 10/100 PHY, except the only thing we're really autoselecting
38  * here is full/half duplex. Speed is always 1000mbps.
39  */
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/socket.h>
45 #include <sys/bus.h>
46 
47 
48 #include <net/if.h>
49 #include <net/if_media.h>
50 
51 #include <dev/mii/mii.h>
52 #include <dev/mii/miivar.h>
53 #include "miidevs.h"
54 
55 #include <dev/mii/xmphyreg.h>
56 
57 #include "miibus_if.h"
58 
59 #if !defined(lint)
60 static const char rcsid[] =
61   "$FreeBSD$";
62 #endif
63 
64 static int xmphy_probe(device_t);
65 static int xmphy_attach(device_t);
66 
67 static device_method_t xmphy_methods[] = {
68 	/* device interface */
69 	DEVMETHOD(device_probe,		xmphy_probe),
70 	DEVMETHOD(device_attach,	xmphy_attach),
71 	DEVMETHOD(device_detach,	mii_phy_detach),
72 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
73 	{ 0, 0 }
74 };
75 
76 static devclass_t xmphy_devclass;
77 
78 static driver_t xmphy_driver = {
79 	"xmphy",
80 	xmphy_methods,
81 	sizeof(struct mii_softc)
82 };
83 
84 DRIVER_MODULE(xmphy, miibus, xmphy_driver, xmphy_devclass, 0, 0);
85 
86 static int	xmphy_service(struct mii_softc *, struct mii_data *, int);
87 static void	xmphy_status(struct mii_softc *);
88 static int	xmphy_mii_phy_auto(struct mii_softc *);
89 
90 static int
91 xmphy_probe(dev)
92 	device_t		dev;
93 {
94 	struct mii_attach_args *ma;
95 
96 	ma = device_get_ivars(dev);
97 
98 	if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_xxXAQTI &&
99 	    MII_MODEL(ma->mii_id2) == MII_MODEL_XAQTI_XMACII) {
100 		device_set_desc(dev, MII_STR_XAQTI_XMACII);
101 		return(0);
102 	}
103 
104 	if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_JATO &&
105 	    MII_MODEL(ma->mii_id2) == MII_MODEL_JATO_BASEX) {
106 		device_set_desc(dev, MII_STR_JATO_BASEX);
107 		return(0);
108 	}
109 
110 	return(ENXIO);
111 }
112 
113 static int
114 xmphy_attach(dev)
115 	device_t		dev;
116 {
117 	struct mii_softc *sc;
118 	struct mii_attach_args *ma;
119 	struct mii_data *mii;
120 	const char *sep = "";
121 
122 	sc = device_get_softc(dev);
123 	ma = device_get_ivars(dev);
124 	sc->mii_dev = device_get_parent(dev);
125 	mii = device_get_softc(sc->mii_dev);
126 	LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
127 
128 	sc->mii_inst = mii->mii_instance;
129 	sc->mii_phy = ma->mii_phyno;
130 	sc->mii_service = xmphy_service;
131 	sc->mii_pdata = mii;
132 
133 	sc->mii_flags |= MIIF_NOISOLATE;
134 	mii->mii_instance++;
135 
136 #define	ADD(m, c)	ifmedia_add(&mii->mii_media, (m), (c), NULL)
137 #define PRINT(s)	printf("%s%s", sep, s); sep = ", "
138 
139 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
140 	    BMCR_ISO);
141 #if 0
142 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->mii_inst),
143 	    BMCR_LOOP|BMCR_S100);
144 #endif
145 
146 	mii_phy_reset(sc);
147 
148 	device_printf(dev, " ");
149 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, 0, sc->mii_inst),
150 	    XMPHY_BMCR_FDX);
151 	PRINT("1000baseSX");
152 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, IFM_FDX, sc->mii_inst), 0);
153 	PRINT("1000baseSX-FDX");
154 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst), 0);
155 	PRINT("auto");
156 
157 	printf("\n");
158 #undef ADD
159 #undef PRINT
160 
161 	MIIBUS_MEDIAINIT(sc->mii_dev);
162 	return(0);
163 }
164 
165 static int
166 xmphy_service(sc, mii, cmd)
167 	struct mii_softc *sc;
168 	struct mii_data *mii;
169 	int cmd;
170 {
171 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
172 	int reg;
173 
174 	switch (cmd) {
175 	case MII_POLLSTAT:
176 		/*
177 		 * If we're not polling our PHY instance, just return.
178 		 */
179 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
180 			return (0);
181 		break;
182 
183 	case MII_MEDIACHG:
184 		/*
185 		 * If the media indicates a different PHY instance,
186 		 * isolate ourselves.
187 		 */
188 		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
189 			reg = PHY_READ(sc, MII_BMCR);
190 			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
191 			return (0);
192 		}
193 
194 		/*
195 		 * If the interface is not up, don't do anything.
196 		 */
197 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
198 			break;
199 
200 		switch (IFM_SUBTYPE(ife->ifm_media)) {
201 		case IFM_AUTO:
202 #ifdef foo
203 			/*
204 			 * If we're already in auto mode, just return.
205 			 */
206 			if (PHY_READ(sc, XMPHY_MII_BMCR) & XMPHY_BMCR_AUTOEN)
207 				return (0);
208 #endif
209 			(void) xmphy_mii_phy_auto(sc);
210 			break;
211 		case IFM_1000_SX:
212 			mii_phy_reset(sc);
213 			if ((ife->ifm_media & IFM_GMASK) == IFM_FDX) {
214 				PHY_WRITE(sc, XMPHY_MII_ANAR, XMPHY_ANAR_FDX);
215 				PHY_WRITE(sc, XMPHY_MII_BMCR, XMPHY_BMCR_FDX);
216 			} else {
217 				PHY_WRITE(sc, XMPHY_MII_ANAR, XMPHY_ANAR_HDX);
218 				PHY_WRITE(sc, XMPHY_MII_BMCR, 0);
219 			}
220 			break;
221 		case IFM_100_T4:
222 		case IFM_100_TX:
223 		case IFM_10_T:
224 		default:
225 			return (EINVAL);
226 		}
227 		break;
228 
229 	case MII_TICK:
230 		/*
231 		 * If we're not currently selected, just return.
232 		 */
233 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
234 			return (0);
235 
236 		/*
237 		 * Is the interface even up?
238 		 */
239 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
240 			return (0);
241 
242 		/*
243 		 * Only used for autonegotiation.
244 		 */
245 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
246 			break;
247 
248 		/*
249 		 * Check to see if we have link.  If we do, we don't
250 		 * need to restart the autonegotiation process.  Read
251 		 * the BMSR twice in case it's latched.
252 		 */
253 		reg = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
254 		if (reg & BMSR_LINK)
255 			break;
256 
257 		/*
258 		 * Only retry autonegotiation every 5 seconds.
259 		 */
260 		if (++sc->mii_ticks != 5)
261 			return (0);
262 
263 		sc->mii_ticks = 0;
264 
265 		mii_phy_reset(sc);
266 		xmphy_mii_phy_auto(sc);
267 		return(0);
268 	}
269 
270 	/* Update the media status. */
271 	xmphy_status(sc);
272 
273 	/* Callback if something changed. */
274 	mii_phy_update(sc, cmd);
275 	return (0);
276 }
277 
278 static void
279 xmphy_status(sc)
280 	struct mii_softc *sc;
281 {
282 	struct mii_data *mii = sc->mii_pdata;
283 	int bmsr, bmcr, anlpar;
284 
285 	mii->mii_media_status = IFM_AVALID;
286 	mii->mii_media_active = IFM_ETHER;
287 
288 	bmsr = PHY_READ(sc, XMPHY_MII_BMSR) |
289 	    PHY_READ(sc, XMPHY_MII_BMSR);
290 	if (bmsr & XMPHY_BMSR_LINK)
291 		mii->mii_media_status |= IFM_ACTIVE;
292 
293 	/* Do dummy read of extended status register. */
294 	bmcr = PHY_READ(sc, XMPHY_MII_EXTSTS);
295 
296 	bmcr = PHY_READ(sc, XMPHY_MII_BMCR);
297 
298 	if (bmcr & XMPHY_BMCR_LOOP)
299 		mii->mii_media_active |= IFM_LOOP;
300 
301 
302 	if (bmcr & XMPHY_BMCR_AUTOEN) {
303 		if ((bmsr & XMPHY_BMSR_ACOMP) == 0) {
304 			if (bmsr & XMPHY_BMSR_LINK) {
305 				mii->mii_media_active |= IFM_1000_SX|IFM_HDX;
306 				return;
307 			}
308 			/* Erg, still trying, I guess... */
309 			mii->mii_media_active |= IFM_NONE;
310 			return;
311 		}
312 
313 		mii->mii_media_active |= IFM_1000_SX;
314 		anlpar = PHY_READ(sc, XMPHY_MII_ANAR) &
315 		    PHY_READ(sc, XMPHY_MII_ANLPAR);
316 		if (anlpar & XMPHY_ANLPAR_FDX)
317 			mii->mii_media_active |= IFM_FDX;
318 		else
319 			mii->mii_media_active |= IFM_HDX;
320 		return;
321 	}
322 
323 	mii->mii_media_active |= IFM_1000_SX;
324 	if (bmcr & XMPHY_BMCR_FDX)
325 		mii->mii_media_active |= IFM_FDX;
326 	else
327 		mii->mii_media_active |= IFM_HDX;
328 
329 	return;
330 }
331 
332 
333 static int
334 xmphy_mii_phy_auto(mii)
335 	struct mii_softc *mii;
336 {
337 	int anar = 0;
338 
339 	anar = PHY_READ(mii, XMPHY_MII_ANAR);
340 	anar |= XMPHY_ANAR_FDX|XMPHY_ANAR_HDX;
341 	PHY_WRITE(mii, XMPHY_MII_ANAR, anar);
342 	DELAY(1000);
343 	PHY_WRITE(mii, XMPHY_MII_BMCR,
344 	    XMPHY_BMCR_AUTOEN | XMPHY_BMCR_STARTNEG);
345 
346 	return (EJUSTRETURN);
347 }
348