xref: /freebsd/sys/dev/mii/mii_physubr.c (revision b52f49a9a0f22207ad5130ad8faba08de3ed23d8)
1 /*	$NetBSD: mii_physubr.c,v 1.5 1999/08/03 19:41:49 drochner Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998, 1999, 2000, 2001 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9  * NASA Ames Research Center.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the NetBSD
22  *	Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 /*
41  * Subroutines common to all PHYs.
42  */
43 
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
46 
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/socket.h>
51 #include <sys/errno.h>
52 #include <sys/module.h>
53 #include <sys/bus.h>
54 
55 
56 #include <net/if.h>
57 #include <net/if_media.h>
58 
59 #include <dev/mii/mii.h>
60 #include <dev/mii/miivar.h>
61 
62 #include "miibus_if.h"
63 
64 /*
65  * Media to register setting conversion table.  Order matters.
66  */
67 const struct mii_media mii_media_table[MII_NMEDIA] = {
68 	/* None */
69 	{ BMCR_ISO,		ANAR_CSMA,
70 	  0, },
71 
72 	/* 10baseT */
73 	{ BMCR_S10,		ANAR_CSMA|ANAR_10,
74 	  0, },
75 
76 	/* 10baseT-FDX */
77 	{ BMCR_S10|BMCR_FDX,	ANAR_CSMA|ANAR_10_FD,
78 	  0, },
79 
80 	/* 100baseT4 */
81 	{ BMCR_S100,		ANAR_CSMA|ANAR_T4,
82 	  0, },
83 
84 	/* 100baseTX */
85 	{ BMCR_S100,		ANAR_CSMA|ANAR_TX,
86 	  0, },
87 
88 	/* 100baseTX-FDX */
89 	{ BMCR_S100|BMCR_FDX,	ANAR_CSMA|ANAR_TX_FD,
90 	  0, },
91 
92 	/* 1000baseX */
93 	{ BMCR_S1000,		ANAR_CSMA,
94 	  0, },
95 
96 	/* 1000baseX-FDX */
97 	{ BMCR_S1000|BMCR_FDX,	ANAR_CSMA,
98 	  0, },
99 
100 	/* 1000baseT */
101 	{ BMCR_S1000,		ANAR_CSMA,
102 	  GTCR_ADV_1000THDX },
103 
104 	/* 1000baseT-FDX */
105 	{ BMCR_S1000,		ANAR_CSMA,
106 	  GTCR_ADV_1000TFDX },
107 };
108 
109 void
110 mii_phy_setmedia(struct mii_softc *sc)
111 {
112 	struct mii_data *mii = sc->mii_pdata;
113 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
114 	int bmcr, anar, gtcr;
115 
116 	if (IFM_SUBTYPE(ife->ifm_media) == IFM_AUTO) {
117 		if ((PHY_READ(sc, MII_BMCR) & BMCR_AUTOEN) == 0)
118 			(void) mii_phy_auto(sc);
119 		return;
120 	}
121 
122 	/*
123 	 * Table index is stored in the media entry.
124 	 */
125 
126 	KASSERT(ife->ifm_data >=0 && ife->ifm_data < MII_NMEDIA,
127 	    ("invalid ife->ifm_data (0x%x) in mii_phy_setmedia",
128 	    ife->ifm_data));
129 
130 	anar = mii_media_table[ife->ifm_data].mm_anar;
131 	bmcr = mii_media_table[ife->ifm_data].mm_bmcr;
132 	gtcr = mii_media_table[ife->ifm_data].mm_gtcr;
133 
134 	if (mii->mii_media.ifm_media & IFM_ETH_MASTER) {
135 		switch (IFM_SUBTYPE(ife->ifm_media)) {
136 		case IFM_1000_T:
137 			gtcr |= GTCR_MAN_MS|GTCR_ADV_MS;
138 			break;
139 
140 		default:
141 			panic("mii_phy_setmedia: MASTER on wrong media");
142 		}
143 	}
144 
145 	if (ife->ifm_media & IFM_LOOP)
146 		bmcr |= BMCR_LOOP;
147 
148 	PHY_WRITE(sc, MII_ANAR, anar);
149 	PHY_WRITE(sc, MII_BMCR, bmcr);
150 	if (sc->mii_flags & MIIF_HAVE_GTCR)
151 		PHY_WRITE(sc, MII_100T2CR, gtcr);
152 }
153 
154 int
155 mii_phy_auto(struct mii_softc *sc)
156 {
157 
158 	/*
159 	 * Check for 1000BASE-X.  Autonegotiation is a bit
160 	 * different on such devices.
161 	 */
162 	if (sc->mii_flags & MIIF_IS_1000X) {
163 		uint16_t anar = 0;
164 
165 		if (sc->mii_extcapabilities & EXTSR_1000XFDX)
166 			anar |= ANAR_X_FD;
167 		if (sc->mii_extcapabilities & EXTSR_1000XHDX)
168 			anar |= ANAR_X_HD;
169 
170 		if (sc->mii_flags & MIIF_DOPAUSE) {
171 			/* XXX Asymmetric vs. symmetric? */
172 			anar |= ANLPAR_X_PAUSE_TOWARDS;
173 		}
174 
175 		PHY_WRITE(sc, MII_ANAR, anar);
176 	} else {
177 		uint16_t anar;
178 
179 		anar = BMSR_MEDIA_TO_ANAR(sc->mii_capabilities) |
180 		    ANAR_CSMA;
181 		if (sc->mii_flags & MIIF_DOPAUSE)
182 			anar |= ANAR_FC;
183 		PHY_WRITE(sc, MII_ANAR, anar);
184 		if (sc->mii_flags & MIIF_HAVE_GTCR) {
185 			uint16_t gtcr = 0;
186 
187 			if (sc->mii_extcapabilities & EXTSR_1000TFDX)
188 				gtcr |= GTCR_ADV_1000TFDX;
189 			if (sc->mii_extcapabilities & EXTSR_1000THDX)
190 				gtcr |= GTCR_ADV_1000THDX;
191 
192 			PHY_WRITE(sc, MII_100T2CR, gtcr);
193 		}
194 	}
195 	PHY_WRITE(sc, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG);
196 	return (EJUSTRETURN);
197 }
198 
199 int
200 mii_phy_tick(struct mii_softc *sc)
201 {
202 	struct ifmedia_entry *ife = sc->mii_pdata->mii_media.ifm_cur;
203 	struct ifnet *ifp = sc->mii_pdata->mii_ifp;
204 	int reg;
205 
206 	/* Just bail now if the interface is down. */
207 	if ((ifp->if_flags & IFF_UP) == 0)
208 		return (EJUSTRETURN);
209 
210 	/*
211 	 * If we're not doing autonegotiation, we don't need to do
212 	 * any extra work here.  However, we need to check the link
213 	 * status so we can generate an announcement if the status
214 	 * changes.
215 	 */
216 	if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
217 		return (0);
218 
219 	/* Read the status register twice; BMSR_LINK is latch-low. */
220 	reg = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
221 	if (reg & BMSR_LINK) {
222 		/*
223 		 * See above.
224 		 */
225 		return (0);
226 	}
227 
228 	/*
229 	 * Only retry autonegotiation every N seconds.
230 	 */
231 	if (sc->mii_anegticks == 0)
232 		sc->mii_anegticks = 17;
233 	if (++sc->mii_ticks != sc->mii_anegticks)
234 		return (EJUSTRETURN);
235 
236 	sc->mii_ticks = 0;
237 	mii_phy_reset(sc);
238 	mii_phy_auto(sc);
239 	return (0);
240 }
241 
242 void
243 mii_phy_reset(struct mii_softc *sc)
244 {
245 	int reg, i;
246 
247 	if (sc->mii_flags & MIIF_NOISOLATE)
248 		reg = BMCR_RESET;
249 	else
250 		reg = BMCR_RESET | BMCR_ISO;
251 	PHY_WRITE(sc, MII_BMCR, reg);
252 
253 	/* Wait 100ms for it to complete. */
254 	for (i = 0; i < 100; i++) {
255 		reg = PHY_READ(sc, MII_BMCR);
256 		if ((reg & BMCR_RESET) == 0)
257 			break;
258 		DELAY(1000);
259 	}
260 
261 	if (sc->mii_inst != 0 && ((sc->mii_flags & MIIF_NOISOLATE) == 0))
262 		PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
263 }
264 
265 void
266 mii_phy_down(struct mii_softc *sc)
267 {
268 
269 }
270 
271 void
272 mii_phy_update(struct mii_softc *sc, int cmd)
273 {
274 	struct mii_data *mii = sc->mii_pdata;
275 
276 	if (sc->mii_media_active != mii->mii_media_active ||
277 	    cmd == MII_MEDIACHG) {
278 		MIIBUS_STATCHG(sc->mii_dev);
279 		sc->mii_media_active = mii->mii_media_active;
280 	}
281 	if (sc->mii_media_status != mii->mii_media_status) {
282 		MIIBUS_LINKCHG(sc->mii_dev);
283 		sc->mii_media_status = mii->mii_media_status;
284 	}
285 }
286 
287 /*
288  * Given an ifmedia word, return the corresponding ANAR value.
289  */
290 int
291 mii_anar(media)
292 	int media;
293 {
294 	int rv;
295 
296 	switch (media & (IFM_TMASK|IFM_NMASK|IFM_FDX)) {
297 	case IFM_ETHER|IFM_10_T:
298 		rv = ANAR_10|ANAR_CSMA;
299 		break;
300 	case IFM_ETHER|IFM_10_T|IFM_FDX:
301 		rv = ANAR_10_FD|ANAR_CSMA;
302 		break;
303 	case IFM_ETHER|IFM_100_TX:
304 		rv = ANAR_TX|ANAR_CSMA;
305 		break;
306 	case IFM_ETHER|IFM_100_TX|IFM_FDX:
307 		rv = ANAR_TX_FD|ANAR_CSMA;
308 		break;
309 	case IFM_ETHER|IFM_100_T4:
310 		rv = ANAR_T4|ANAR_CSMA;
311 		break;
312 	default:
313 		rv = 0;
314 		break;
315 	}
316 
317 	return (rv);
318 }
319 
320 /*
321  * Given a BMCR value, return the corresponding ifmedia word.
322  */
323 int
324 mii_media_from_bmcr(bmcr)
325 	int bmcr;
326 {
327 	int rv = IFM_ETHER;
328 
329 	if (bmcr & BMCR_S100)
330 		rv |= IFM_100_TX;
331 	else
332 		rv |= IFM_10_T;
333 	if (bmcr & BMCR_FDX)
334 		rv |= IFM_FDX;
335 
336 	return (rv);
337 }
338 
339 /*
340  * Initialize generic PHY media based on BMSR, called when a PHY is
341  * attached.  We expect to be set up to print a comma-separated list
342  * of media names.  Does not print a newline.
343  */
344 void
345 mii_add_media(struct mii_softc *sc)
346 {
347 	const char *sep = "";
348 	struct mii_data *mii;
349 
350 	mii = device_get_softc(sc->mii_dev);
351 	if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0) {
352 		printf("no media present");
353 		return;
354 	}
355 
356 #define	ADD(m, c)	ifmedia_add(&mii->mii_media, (m), (c), NULL)
357 #define	PRINT(s)	printf("%s%s", sep, s); sep = ", "
358 
359 	if (sc->mii_capabilities & BMSR_10THDX) {
360 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, sc->mii_inst), 0);
361 		PRINT("10baseT");
362 	}
363 	if (sc->mii_capabilities & BMSR_10TFDX) {
364 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, sc->mii_inst),
365 		    BMCR_FDX);
366 		PRINT("10baseT-FDX");
367 	}
368 	if (sc->mii_capabilities & BMSR_100TXHDX) {
369 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, sc->mii_inst),
370 		    BMCR_S100);
371 		PRINT("100baseTX");
372 	}
373 	if (sc->mii_capabilities & BMSR_100TXFDX) {
374 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, sc->mii_inst),
375 		    BMCR_S100|BMCR_FDX);
376 		PRINT("100baseTX-FDX");
377 	}
378 	if (sc->mii_capabilities & BMSR_100T4) {
379 		/*
380 		 * XXX How do you enable 100baseT4?  I assume we set
381 		 * XXX BMCR_S100 and then assume the PHYs will take
382 		 * XXX watever action is necessary to switch themselves
383 		 * XXX into T4 mode.
384 		 */
385 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_T4, 0, sc->mii_inst),
386 		    BMCR_S100);
387 		PRINT("100baseT4");
388 	}
389 	if (sc->mii_capabilities & BMSR_ANEG) {
390 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst),
391 		    BMCR_AUTOEN);
392 		PRINT("auto");
393 	}
394 
395 
396 
397 #undef ADD
398 #undef PRINT
399 }
400 
401 /*
402  * Initialize generic PHY media based on BMSR, called when a PHY is
403  * attached.  We expect to be set up to print a comma-separated list
404  * of media names.  Does not print a newline.
405  */
406 void
407 mii_phy_add_media(struct mii_softc *sc)
408 {
409 	struct mii_data *mii = sc->mii_pdata;
410 	const char *sep = "";
411 
412 #define	ADD(m, c)	ifmedia_add(&mii->mii_media, (m), (c), NULL)
413 #define	PRINT(s)	printf("%s%s", sep, s); sep = ", "
414 
415 	if ((sc->mii_flags & MIIF_NOISOLATE) == 0)
416 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
417 		    MII_MEDIA_NONE);
418 
419 	/*
420 	 * There are different interpretations for the bits in
421 	 * HomePNA PHYs.  And there is really only one media type
422 	 * that is supported.
423 	 */
424 	if (sc->mii_flags & MIIF_IS_HPNA) {
425 		if (sc->mii_capabilities & BMSR_10THDX) {
426 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_HPNA_1, 0,
427 					 sc->mii_inst),
428 			    MII_MEDIA_10_T);
429 			PRINT("HomePNA1");
430 		}
431 		return;
432 	}
433 
434 	if (sc->mii_capabilities & BMSR_10THDX) {
435 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, sc->mii_inst),
436 		    MII_MEDIA_10_T);
437 		PRINT("10baseT");
438 	}
439 	if (sc->mii_capabilities & BMSR_10TFDX) {
440 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, sc->mii_inst),
441 		    MII_MEDIA_10_T_FDX);
442 		PRINT("10baseT-FDX");
443 	}
444 	if (sc->mii_capabilities & BMSR_100TXHDX) {
445 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, sc->mii_inst),
446 		    MII_MEDIA_100_TX);
447 		PRINT("100baseTX");
448 	}
449 	if (sc->mii_capabilities & BMSR_100TXFDX) {
450 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, sc->mii_inst),
451 		    MII_MEDIA_100_TX_FDX);
452 		PRINT("100baseTX-FDX");
453 	}
454 	if (sc->mii_capabilities & BMSR_100T4) {
455 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_T4, 0, sc->mii_inst),
456 		    MII_MEDIA_100_T4);
457 		PRINT("100baseT4");
458 	}
459 
460 	if (sc->mii_extcapabilities & EXTSR_MEDIAMASK) {
461 		/*
462 		 * XXX Right now only handle 1000SX and 1000TX.  Need
463 		 * XXX to handle 1000LX and 1000CX some how.
464 		 *
465 		 * Note since it can take 5 seconds to auto-negotiate
466 		 * a gigabit link, we make anegticks 10 seconds for
467 		 * all the gigabit media types.
468 		 */
469 		if (sc->mii_extcapabilities & EXTSR_1000XHDX) {
470 			sc->mii_anegticks = 17;
471 			sc->mii_flags |= MIIF_IS_1000X;
472 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, 0,
473 			    sc->mii_inst), MII_MEDIA_1000_X);
474 			PRINT("1000baseSX");
475 		}
476 		if (sc->mii_extcapabilities & EXTSR_1000XFDX) {
477 			sc->mii_anegticks = 17;
478 			sc->mii_flags |= MIIF_IS_1000X;
479 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, IFM_FDX,
480 			    sc->mii_inst), MII_MEDIA_1000_X_FDX);
481 			PRINT("1000baseSX-FDX");
482 		}
483 
484 		/*
485 		 * 1000baseT media needs to be able to manipulate
486 		 * master/slave mode.  We set IFM_ETH_MASTER in
487 		 * the "don't care mask" and filter it out when
488 		 * the media is set.
489 		 *
490 		 * All 1000baseT PHYs have a 1000baseT control register.
491 		 */
492 		if (sc->mii_extcapabilities & EXTSR_1000THDX) {
493 			sc->mii_anegticks = 17;
494 			sc->mii_flags |= MIIF_HAVE_GTCR;
495 			mii->mii_media.ifm_mask |= IFM_ETH_MASTER;
496 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, 0,
497 			    sc->mii_inst), MII_MEDIA_1000_T);
498 			PRINT("1000baseT");
499 		}
500 		if (sc->mii_extcapabilities & EXTSR_1000TFDX) {
501 			sc->mii_anegticks = 17;
502 			sc->mii_flags |= MIIF_HAVE_GTCR;
503 			mii->mii_media.ifm_mask |= IFM_ETH_MASTER;
504 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, IFM_FDX,
505 			    sc->mii_inst), MII_MEDIA_1000_T_FDX);
506 			PRINT("1000baseT-FDX");
507 		}
508 	}
509 
510 	if (sc->mii_capabilities & BMSR_ANEG) {
511 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst),
512 		    MII_NMEDIA);	/* intentionally invalid index */
513 		PRINT("auto");
514 	}
515 #undef ADD
516 #undef PRINT
517 }
518 
519 int
520 mii_phy_detach(device_t dev)
521 {
522 	struct mii_softc *sc;
523 
524 	sc = device_get_softc(dev);
525 	mii_phy_down(sc);
526 	sc->mii_dev = NULL;
527 	LIST_REMOVE(sc, mii_list);
528 
529 	return(0);
530 }
531 
532 const struct mii_phydesc *
533 mii_phy_match(const struct mii_attach_args *ma, const struct mii_phydesc *mpd)
534 {
535 
536 	for (; mpd->mpd_name != NULL; mpd++) {
537 		if (MII_OUI(ma->mii_id1, ma->mii_id2) == mpd->mpd_oui &&
538 		    MII_MODEL(ma->mii_id2) == mpd->mpd_model)
539 			return (mpd);
540 	}
541 	return (NULL);
542 }
543