xref: /dragonfly/sys/dev/netif/mii_layer/nsgphy.c (revision 1847e88f)
1 /*
2  * Copyright (c) 2001 Wind River Systems
3  * Copyright (c) 2001
4  *	Bill Paul <wpaul@bsdi.com>.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed by Bill Paul.
17  * 4. Neither the name of the author nor the names of any co-contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31  * THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  * $FreeBSD: src/sys/dev/mii/nsgphy.c,v 1.1.2.3 2002/11/08 21:53:49 semenu Exp $
34  * $DragonFly: src/sys/dev/netif/mii_layer/nsgphy.c,v 1.12 2005/12/11 01:54:08 swildner Exp $
35  */
36 
37 /*
38  * Driver for the National Semiconductor DP83891 and DP83861
39  * 10/100/1000 PHYs.
40  * Datasheet available at: http://www.national.com/ds/DP/DP83861.pdf
41  *
42  * The DP83891 is the older NatSemi gigE PHY which isn't being sold
43  * anymore. The DP83861 is its replacement, which is an 'enhanced'
44  * firmware driven component. The major difference between the
45  * two is that the 83891 can't generate interrupts, while the
46  * 83861 can. (I think it wasn't originally designed to do this, but
47  * it can now thanks to firmware updates.) The 83861 also allows
48  * access to its internal RAM via indirect register access.
49  */
50 
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/kernel.h>
54 #include <sys/socket.h>
55 #include <sys/bus.h>
56 
57 #include <machine/clock.h>
58 
59 #include <net/if.h>
60 #include <net/if_media.h>
61 
62 #include "mii.h"
63 #include "miivar.h"
64 #include "miidevs.h"
65 
66 #include "nsgphyreg.h"
67 
68 #include "miibus_if.h"
69 
70 static int nsgphy_probe		(device_t);
71 static int nsgphy_attach	(device_t);
72 static int nsgphy_detach	(device_t);
73 
74 static device_method_t nsgphy_methods[] = {
75 	/* device interface */
76 	DEVMETHOD(device_probe,		nsgphy_probe),
77 	DEVMETHOD(device_attach,	nsgphy_attach),
78 	DEVMETHOD(device_detach,	nsgphy_detach),
79 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
80 	{ 0, 0 }
81 };
82 
83 static devclass_t nsgphy_devclass;
84 
85 static driver_t nsgphy_driver = {
86 	"nsgphy",
87 	nsgphy_methods,
88 	sizeof(struct mii_softc)
89 };
90 
91 DRIVER_MODULE(nsgphy, miibus, nsgphy_driver, nsgphy_devclass, 0, 0);
92 
93 int	nsgphy_service (struct mii_softc *, struct mii_data *, int);
94 void	nsgphy_status (struct mii_softc *);
95 
96 static int	nsgphy_mii_phy_auto (struct mii_softc *, int);
97 extern void	mii_phy_auto_timeout (void *);
98 
99 static int
100 nsgphy_probe(device_t dev)
101 {
102 	struct mii_attach_args *ma;
103 
104 	ma = device_get_ivars(dev);
105 
106 	if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_NATSEMI) {
107 		if (MII_MODEL(ma->mii_id2) == MII_MODEL_NATSEMI_DP83891) {
108 			device_set_desc(dev, MII_STR_NATSEMI_DP83891);
109 			return(0);
110 		}
111 		if (MII_MODEL(ma->mii_id2) == MII_MODEL_NATSEMI_DP83861) {
112 			device_set_desc(dev, MII_STR_NATSEMI_DP83861);
113 			return(0);
114 		}
115 	}
116 
117 	return(ENXIO);
118 }
119 
120 static int
121 nsgphy_attach(device_t dev)
122 {
123 	struct mii_softc *sc;
124 	struct mii_attach_args *ma;
125 	struct mii_data *mii;
126 	const char *sep = "";
127 
128 	sc = device_get_softc(dev);
129 	ma = device_get_ivars(dev);
130 	mii_softc_init(sc, ma);
131 	sc->mii_dev = device_get_parent(dev);
132 	mii = device_get_softc(sc->mii_dev);
133 	LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
134 
135 	sc->mii_inst = mii->mii_instance;
136 	sc->mii_service = nsgphy_service;
137 	sc->mii_pdata = mii;
138 
139 	sc->mii_flags |= MIIF_NOISOLATE;
140 	mii->mii_instance++;
141 
142 #define	ADD(m, c)	ifmedia_add(&mii->mii_media, (m), (c), NULL)
143 #define PRINT(s)	printf("%s%s", sep, s); sep = ", "
144 
145 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
146 	    BMCR_ISO);
147 #if 0
148 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->mii_inst),
149 	    BMCR_LOOP|BMCR_S100);
150 #endif
151 
152 	mii_phy_reset(sc);
153 
154 	device_printf(dev, " ");
155 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, IFM_FDX, sc->mii_inst),
156 	    NSGPHY_S1000|NSGPHY_BMCR_FDX);
157 	PRINT("1000baseTX-FDX");
158 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, 0, sc->mii_inst),
159 	    NSGPHY_S1000);
160 	PRINT("1000baseTX");
161 	sc->mii_capabilities =
162 	    (PHY_READ(sc, MII_BMSR) |
163 	    (BMSR_10TFDX|BMSR_10THDX)) & ma->mii_capmask;
164 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, sc->mii_inst),
165 	    NSGPHY_S100|NSGPHY_BMCR_FDX);
166 	PRINT("100baseTX-FDX");
167 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, sc->mii_inst), NSGPHY_S100);
168 	PRINT("100baseTX");
169 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, sc->mii_inst),
170 	    NSGPHY_S10|NSGPHY_BMCR_FDX);
171 	PRINT("10baseT-FDX");
172 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, sc->mii_inst), NSGPHY_S10);
173 	PRINT("10baseT");
174 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst), 0);
175 	PRINT("auto");
176 	printf("\n");
177 #undef ADD
178 #undef PRINT
179 
180 	MIIBUS_MEDIAINIT(sc->mii_dev);
181 	return(0);
182 }
183 
184 static int
185 nsgphy_detach(device_t dev)
186 {
187 	struct mii_softc *sc;
188 	struct mii_data *mii;
189 
190 	sc = device_get_softc(dev);
191 	mii = device_get_softc(device_get_parent(dev));
192 	if (sc->mii_flags & MIIF_DOINGAUTO)
193 		callout_stop(&sc->mii_auto_ch);
194 	sc->mii_dev = NULL;
195 	LIST_REMOVE(sc, mii_list);
196 
197 	return(0);
198 }
199 
200 int
201 nsgphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
202 {
203 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
204 	int reg;
205 
206 	switch (cmd) {
207 	case MII_POLLSTAT:
208 		/*
209 		 * If we're not polling our PHY instance, just return.
210 		 */
211 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
212 			return (0);
213 		break;
214 
215 	case MII_MEDIACHG:
216 		/*
217 		 * If the media indicates a different PHY instance,
218 		 * isolate ourselves.
219 		 */
220 		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
221 			reg = PHY_READ(sc, MII_BMCR);
222 			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
223 			return (0);
224 		}
225 
226 		/*
227 		 * If the interface is not up, don't do anything.
228 		 */
229 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
230 			break;
231 
232 
233 		switch (IFM_SUBTYPE(ife->ifm_media)) {
234 		case IFM_AUTO:
235 #ifdef foo
236 			/*
237 			 * If we're already in auto mode, just return.
238 			 */
239 			if (PHY_READ(sc, NSGPHY_MII_BMCR) & NSGPHY_BMCR_AUTOEN)
240 				return (0);
241 #endif
242 			(void) nsgphy_mii_phy_auto(sc, 0);
243 			break;
244 		case IFM_1000_T:
245 			if ((ife->ifm_media & IFM_GMASK) == IFM_FDX) {
246 				PHY_WRITE(sc, NSGPHY_MII_BMCR,
247 				    NSGPHY_BMCR_FDX|NSGPHY_BMCR_SPD1);
248 			} else {
249 				PHY_WRITE(sc, NSGPHY_MII_BMCR,
250 				    NSGPHY_BMCR_SPD1);
251 			}
252 			PHY_WRITE(sc, NSGPHY_MII_ANAR, NSGPHY_SEL_TYPE);
253 
254 			/*
255 			 * When setting the link manually, one side must
256 			 * be the master and the other the slave. However
257 			 * ifmedia doesn't give us a good way to specify
258 			 * this, so we fake it by using one of the LINK
259 			 * flags. If LINK0 is set, we program the PHY to
260 			 * be a master, otherwise it's a slave.
261 			 */
262 			if ((mii->mii_ifp->if_flags & IFF_LINK0)) {
263 				PHY_WRITE(sc, NSGPHY_MII_1000CTL,
264 				    NSGPHY_1000CTL_MSE|NSGPHY_1000CTL_MSC);
265 			} else {
266 				PHY_WRITE(sc, NSGPHY_MII_1000CTL,
267 				    NSGPHY_1000CTL_MSE);
268 			}
269 			break;
270 		case IFM_100_T4:
271 			/*
272 			 * XXX Not supported as a manual setting right now.
273 			 */
274 			return (EINVAL);
275 		case IFM_NONE:
276 			PHY_WRITE(sc, MII_BMCR, BMCR_ISO|BMCR_PDOWN);
277 			break;
278 		default:
279 			/*
280 			 * BMCR data is stored in the ifmedia entry.
281 			 */
282 			PHY_WRITE(sc, MII_ANAR,
283 			    mii_anar(ife->ifm_media));
284 			PHY_WRITE(sc, MII_BMCR, ife->ifm_data);
285 			break;
286 		}
287 		break;
288 
289 	case MII_TICK:
290 		/*
291 		 * If we're not currently selected, just return.
292 		 */
293 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
294 			return (0);
295 
296 		/*
297 		 * Only used for autonegotiation.
298 		 */
299 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
300 			return (0);
301 
302 		/*
303 		 * Is the interface even up?
304 		 */
305 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
306 			return (0);
307 
308 		/*
309 		 * Only retry autonegotiation every 17 seconds.
310 		 * Actually, for gigE PHYs, we should wait longer, since
311 		 * 5 seconds is the mimimum time the documentation
312 		 * says to wait for a 1000mbps link to be established.
313 		 */
314 		if (++sc->mii_ticks != 17)
315 			return (0);
316 
317 		sc->mii_ticks = 0;
318 
319 		/*
320 		 * Check to see if we have link.
321 		 */
322 		reg = PHY_READ(sc, NSGPHY_MII_PHYSUP);
323 		if (reg & NSGPHY_PHYSUP_LNKSTS)
324 			break;
325 
326 		mii_phy_reset(sc);
327 		if (nsgphy_mii_phy_auto(sc, 0) == EJUSTRETURN)
328 			return(0);
329 		break;
330 	}
331 
332 	/* Update the media status. */
333 	nsgphy_status(sc);
334 
335 	/* Callback if something changed. */
336 	if (sc->mii_active != mii->mii_media_active || cmd == MII_MEDIACHG) {
337 		MIIBUS_STATCHG(sc->mii_dev);
338 		sc->mii_active = mii->mii_media_active;
339 	}
340 	return (0);
341 }
342 
343 void
344 nsgphy_status(struct mii_softc *sc)
345 {
346 	struct mii_data *mii = sc->mii_pdata;
347 	int bmsr, bmcr, physup, anlpar, gstat;
348 
349 	mii->mii_media_status = IFM_AVALID;
350 	mii->mii_media_active = IFM_ETHER;
351 
352 	bmsr = PHY_READ(sc, NSGPHY_MII_BMSR);
353 	physup = PHY_READ(sc, NSGPHY_MII_PHYSUP);
354 	if (physup & NSGPHY_PHYSUP_LNKSTS)
355 		mii->mii_media_status |= IFM_ACTIVE;
356 
357 	bmcr = PHY_READ(sc, NSGPHY_MII_BMCR);
358 
359 	if (bmcr & NSGPHY_BMCR_LOOP)
360 		mii->mii_media_active |= IFM_LOOP;
361 
362 	if (bmcr & NSGPHY_BMCR_AUTOEN) {
363 		if ((bmsr & NSGPHY_BMSR_ACOMP) == 0) {
364 			/* Erg, still trying, I guess... */
365 			mii->mii_media_active |= IFM_NONE;
366 			return;
367 		}
368 		anlpar = PHY_READ(sc, NSGPHY_MII_ANLPAR);
369 		gstat = PHY_READ(sc, NSGPHY_MII_1000STS);
370 		if (gstat & NSGPHY_1000STS_LPFD)
371 			mii->mii_media_active |= IFM_1000_T | IFM_FDX;
372 		else if (gstat & NSGPHY_1000STS_LPHD)
373 			mii->mii_media_active |= IFM_1000_T | IFM_HDX;
374 		else if (anlpar & NSGPHY_ANLPAR_100T4)
375 			mii->mii_media_active |= IFM_100_T4;
376 		else if (anlpar & NSGPHY_ANLPAR_100FDX)
377 			mii->mii_media_active |= IFM_100_TX|IFM_FDX;
378 		else if (anlpar & NSGPHY_ANLPAR_100HDX)
379 			mii->mii_media_active |= IFM_100_TX;
380 		else if (anlpar & NSGPHY_ANLPAR_10FDX)
381 			mii->mii_media_active |= IFM_10_T|IFM_FDX;
382 		else if (anlpar & NSGPHY_ANLPAR_10HDX)
383 			mii->mii_media_active |= IFM_10_T|IFM_HDX;
384 		else
385 			mii->mii_media_active |= IFM_NONE;
386 		return;
387 	}
388 
389 	switch(bmcr & (NSGPHY_BMCR_SPD1|NSGPHY_BMCR_SPD0)) {
390 	case NSGPHY_S1000:
391 		mii->mii_media_active |= IFM_1000_T;
392 		break;
393 	case NSGPHY_S100:
394 		mii->mii_media_active |= IFM_100_TX;
395 		break;
396 	case NSGPHY_S10:
397 		mii->mii_media_active |= IFM_10_T;
398 		break;
399 	default:
400 		break;
401 	}
402 
403 	if (bmcr & NSGPHY_BMCR_FDX)
404 		mii->mii_media_active |= IFM_FDX;
405 	else
406 		mii->mii_media_active |= IFM_HDX;
407 
408 	return;
409 }
410 
411 
412 static int
413 nsgphy_mii_phy_auto(struct mii_softc *mii, int waitfor)
414 {
415 	int bmsr, ktcr = 0, i;
416 
417 	if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) {
418 		mii_phy_reset(mii);
419 		PHY_WRITE(mii, NSGPHY_MII_BMCR, 0);
420 		DELAY(1000);
421 		ktcr = PHY_READ(mii, NSGPHY_MII_1000CTL);
422 		PHY_WRITE(mii, NSGPHY_MII_1000CTL, ktcr |
423 		    (NSGPHY_1000CTL_AFD|NSGPHY_1000CTL_AHD));
424 		ktcr = PHY_READ(mii, NSGPHY_MII_1000CTL);
425 		DELAY(1000);
426 		PHY_WRITE(mii, NSGPHY_MII_ANAR, mii_bmsr_media_to_anar(mii));
427 		DELAY(1000);
428 		PHY_WRITE(mii, NSGPHY_MII_BMCR,
429 		    NSGPHY_BMCR_AUTOEN | NSGPHY_BMCR_STARTNEG);
430 	}
431 
432 	if (waitfor) {
433 		/* Wait 500ms for it to complete. */
434 		for (i = 0; i < 500; i++) {
435 			if ((bmsr = PHY_READ(mii, NSGPHY_MII_BMSR)) &
436 			    NSGPHY_BMSR_ACOMP)
437 				return (0);
438 			DELAY(1000);
439 #if 0
440 		if ((bmsr & BMSR_ACOMP) == 0)
441 			printf("%s: autonegotiation failed to complete\n",
442 			    mii->mii_dev.dv_xname);
443 #endif
444 		}
445 
446 		/*
447 		 * Don't need to worry about clearing MIIF_DOINGAUTO.
448 		 * If that's set, a timeout is pending, and it will
449 		 * clear the flag.
450 		 */
451 		return (EIO);
452 	}
453 
454 	/*
455 	 * Just let it finish asynchronously.  This is for the benefit of
456 	 * the tick handler driving autonegotiation.  Don't want 500ms
457 	 * delays all the time while the system is running!
458 	 */
459 	if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) {
460 		mii->mii_flags |= MIIF_DOINGAUTO;
461 		callout_reset(&mii->mii_auto_ch, hz >> 1,
462 				mii_phy_auto_timeout, mii);
463 	}
464 	return (EJUSTRETURN);
465 }
466