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