mii_physubr.c revision 164827
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 164827 2006-12-02 15:32:34Z marius $");
42
43/*
44 * Subroutines common to all PHYs.
45 */
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#include <net/if.h>
56#include <net/if_media.h>
57
58#include <dev/mii/mii.h>
59#include <dev/mii/miivar.h>
60
61#include "miibus_if.h"
62
63/*
64 * Media to register setting conversion table.  Order matters.
65 */
66const struct mii_media mii_media_table[MII_NMEDIA] = {
67	/* None */
68	{ BMCR_ISO,		ANAR_CSMA,
69	  0, },
70
71	/* 10baseT */
72	{ BMCR_S10,		ANAR_CSMA|ANAR_10,
73	  0, },
74
75	/* 10baseT-FDX */
76	{ BMCR_S10|BMCR_FDX,	ANAR_CSMA|ANAR_10_FD,
77	  0, },
78
79	/* 100baseT4 */
80	{ BMCR_S100,		ANAR_CSMA|ANAR_T4,
81	  0, },
82
83	/* 100baseTX */
84	{ BMCR_S100,		ANAR_CSMA|ANAR_TX,
85	  0, },
86
87	/* 100baseTX-FDX */
88	{ BMCR_S100|BMCR_FDX,	ANAR_CSMA|ANAR_TX_FD,
89	  0, },
90
91	/* 1000baseX */
92	{ BMCR_S1000,		ANAR_CSMA,
93	  0, },
94
95	/* 1000baseX-FDX */
96	{ BMCR_S1000|BMCR_FDX,	ANAR_CSMA,
97	  0, },
98
99	/* 1000baseT */
100	{ BMCR_S1000,		ANAR_CSMA,
101	  GTCR_ADV_1000THDX },
102
103	/* 1000baseT-FDX */
104	{ BMCR_S1000,		ANAR_CSMA,
105	  GTCR_ADV_1000TFDX },
106};
107
108void
109mii_phy_setmedia(struct mii_softc *sc)
110{
111	struct mii_data *mii = sc->mii_pdata;
112	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
113	int bmcr, anar, gtcr;
114
115	if (IFM_SUBTYPE(ife->ifm_media) == IFM_AUTO) {
116		if ((PHY_READ(sc, MII_BMCR) & BMCR_AUTOEN) == 0 ||
117		    (sc->mii_flags & MIIF_FORCEANEG))
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
154int
155mii_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
199int
200mii_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		sc->mii_ticks = 0;	/* reset autonegotiation timer. */
218		return (0);
219	}
220
221	/* Read the status register twice; BMSR_LINK is latch-low. */
222	reg = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
223	if (reg & BMSR_LINK) {
224		sc->mii_ticks = 0;	/* reset autonegotiation timer. */
225		/* See above. */
226		return (0);
227	}
228
229	/* Announce link loss right after it happens */
230	if (sc->mii_ticks++ == 0)
231		return (0);
232
233	/* XXX: use default value if phy driver did not set mii_anegticks */
234	if (sc->mii_anegticks == 0)
235		sc->mii_anegticks = MII_ANEGTICKS_GIGE;
236
237	/* Only retry autonegotiation every mii_anegticks ticks. */
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	struct ifmedia_entry *ife = sc->mii_pdata->mii_media.ifm_cur;
251	int reg, i;
252
253	if (sc->mii_flags & MIIF_NOISOLATE)
254		reg = BMCR_RESET;
255	else
256		reg = BMCR_RESET | BMCR_ISO;
257	PHY_WRITE(sc, MII_BMCR, reg);
258
259	/* Wait 100ms for it to complete. */
260	for (i = 0; i < 100; i++) {
261		reg = PHY_READ(sc, MII_BMCR);
262		if ((reg & BMCR_RESET) == 0)
263			break;
264		DELAY(1000);
265	}
266
267	if ((sc->mii_flags & MIIF_NOISOLATE) == 0) {
268		if ((ife == NULL && sc->mii_inst != 0) ||
269		    (ife != NULL && IFM_INST(ife->ifm_media) != sc->mii_inst))
270			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
271	}
272}
273
274void
275mii_phy_down(struct mii_softc *sc)
276{
277
278}
279
280void
281mii_phy_update(struct mii_softc *sc, int cmd)
282{
283	struct mii_data *mii = sc->mii_pdata;
284
285	if (sc->mii_media_active != mii->mii_media_active ||
286	    cmd == MII_MEDIACHG) {
287		MIIBUS_STATCHG(sc->mii_dev);
288		sc->mii_media_active = mii->mii_media_active;
289	}
290	if (sc->mii_media_status != mii->mii_media_status) {
291		MIIBUS_LINKCHG(sc->mii_dev);
292		sc->mii_media_status = mii->mii_media_status;
293	}
294}
295
296/*
297 * Given an ifmedia word, return the corresponding ANAR value.
298 */
299int
300mii_anar(int media)
301{
302	int rv;
303
304	switch (media & (IFM_TMASK|IFM_NMASK|IFM_FDX)) {
305	case IFM_ETHER|IFM_10_T:
306		rv = ANAR_10|ANAR_CSMA;
307		break;
308	case IFM_ETHER|IFM_10_T|IFM_FDX:
309		rv = ANAR_10_FD|ANAR_CSMA;
310		break;
311	case IFM_ETHER|IFM_100_TX:
312		rv = ANAR_TX|ANAR_CSMA;
313		break;
314	case IFM_ETHER|IFM_100_TX|IFM_FDX:
315		rv = ANAR_TX_FD|ANAR_CSMA;
316		break;
317	case IFM_ETHER|IFM_100_T4:
318		rv = ANAR_T4|ANAR_CSMA;
319		break;
320	default:
321		rv = 0;
322		break;
323	}
324
325	return (rv);
326}
327
328/*
329 * Given a BMCR value, return the corresponding ifmedia word.
330 */
331int
332mii_media_from_bmcr(int bmcr)
333{
334	int rv = IFM_ETHER;
335
336	if (bmcr & BMCR_S100)
337		rv |= IFM_100_TX;
338	else
339		rv |= IFM_10_T;
340	if (bmcr & BMCR_FDX)
341		rv |= IFM_FDX;
342
343	return (rv);
344}
345
346/*
347 * Initialize generic PHY media based on BMSR, called when a PHY is
348 * attached.  We expect to be set up to print a comma-separated list
349 * of media names.  Does not print a newline.
350 */
351void
352mii_add_media(struct mii_softc *sc)
353{
354	const char *sep = "";
355	struct mii_data *mii;
356
357	mii = device_get_softc(sc->mii_dev);
358	if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0) {
359		printf("no media present");
360		return;
361	}
362
363#define	ADD(m, c)	ifmedia_add(&mii->mii_media, (m), (c), NULL)
364#define	PRINT(s)	printf("%s%s", sep, s); sep = ", "
365
366	if (sc->mii_capabilities & BMSR_10THDX) {
367		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, sc->mii_inst), 0);
368		PRINT("10baseT");
369	}
370	if (sc->mii_capabilities & BMSR_10TFDX) {
371		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, sc->mii_inst),
372		    BMCR_FDX);
373		PRINT("10baseT-FDX");
374	}
375	if (sc->mii_capabilities & BMSR_100TXHDX) {
376		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, sc->mii_inst),
377		    BMCR_S100);
378		PRINT("100baseTX");
379	}
380	if (sc->mii_capabilities & BMSR_100TXFDX) {
381		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, sc->mii_inst),
382		    BMCR_S100|BMCR_FDX);
383		PRINT("100baseTX-FDX");
384	}
385	if (sc->mii_capabilities & BMSR_100T4) {
386		/*
387		 * XXX How do you enable 100baseT4?  I assume we set
388		 * XXX BMCR_S100 and then assume the PHYs will take
389		 * XXX watever action is necessary to switch themselves
390		 * XXX into T4 mode.
391		 */
392		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_T4, 0, sc->mii_inst),
393		    BMCR_S100);
394		PRINT("100baseT4");
395	}
396	if (sc->mii_capabilities & BMSR_ANEG) {
397		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst),
398		    BMCR_AUTOEN);
399		PRINT("auto");
400	}
401
402
403
404#undef ADD
405#undef PRINT
406}
407
408/*
409 * Initialize generic PHY media based on BMSR, called when a PHY is
410 * attached.  We expect to be set up to print a comma-separated list
411 * of media names.  Does not print a newline.
412 */
413void
414mii_phy_add_media(struct mii_softc *sc)
415{
416	struct mii_data *mii = sc->mii_pdata;
417	const char *sep = "";
418
419	if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0 &&
420	    (sc->mii_extcapabilities & EXTSR_MEDIAMASK) == 0) {
421		printf("no media present");
422		return;
423	}
424
425	/* Set aneg timer for 10/100 media. Gigabit media handled below. */
426	sc->mii_anegticks = MII_ANEGTICKS;
427
428#define	ADD(m, c)	ifmedia_add(&mii->mii_media, (m), (c), NULL)
429#define	PRINT(s)	printf("%s%s", sep, s); sep = ", "
430
431	if ((sc->mii_flags & MIIF_NOISOLATE) == 0)
432		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
433		    MII_MEDIA_NONE);
434
435	/*
436	 * There are different interpretations for the bits in
437	 * HomePNA PHYs.  And there is really only one media type
438	 * that is supported.
439	 */
440	if (sc->mii_flags & MIIF_IS_HPNA) {
441		if (sc->mii_capabilities & BMSR_10THDX) {
442			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_HPNA_1, 0,
443					 sc->mii_inst),
444			    MII_MEDIA_10_T);
445			PRINT("HomePNA1");
446		}
447		return;
448	}
449
450	if (sc->mii_capabilities & BMSR_10THDX) {
451		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, sc->mii_inst),
452		    MII_MEDIA_10_T);
453		PRINT("10baseT");
454	}
455	if (sc->mii_capabilities & BMSR_10TFDX) {
456		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, sc->mii_inst),
457		    MII_MEDIA_10_T_FDX);
458		PRINT("10baseT-FDX");
459	}
460	if (sc->mii_capabilities & BMSR_100TXHDX) {
461		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, sc->mii_inst),
462		    MII_MEDIA_100_TX);
463		PRINT("100baseTX");
464	}
465	if (sc->mii_capabilities & BMSR_100TXFDX) {
466		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, sc->mii_inst),
467		    MII_MEDIA_100_TX_FDX);
468		PRINT("100baseTX-FDX");
469	}
470	if (sc->mii_capabilities & BMSR_100T4) {
471		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_T4, 0, sc->mii_inst),
472		    MII_MEDIA_100_T4);
473		PRINT("100baseT4");
474	}
475
476	if (sc->mii_extcapabilities & EXTSR_MEDIAMASK) {
477		/*
478		 * XXX Right now only handle 1000SX and 1000TX.  Need
479		 * XXX to handle 1000LX and 1000CX some how.
480		 */
481		if (sc->mii_extcapabilities & EXTSR_1000XHDX) {
482			sc->mii_anegticks = MII_ANEGTICKS_GIGE;
483			sc->mii_flags |= MIIF_IS_1000X;
484			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, 0,
485			    sc->mii_inst), MII_MEDIA_1000_X);
486			PRINT("1000baseSX");
487		}
488		if (sc->mii_extcapabilities & EXTSR_1000XFDX) {
489			sc->mii_anegticks = MII_ANEGTICKS_GIGE;
490			sc->mii_flags |= MIIF_IS_1000X;
491			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, IFM_FDX,
492			    sc->mii_inst), MII_MEDIA_1000_X_FDX);
493			PRINT("1000baseSX-FDX");
494		}
495
496		/*
497		 * 1000baseT media needs to be able to manipulate
498		 * master/slave mode.  We set IFM_ETH_MASTER in
499		 * the "don't care mask" and filter it out when
500		 * the media is set.
501		 *
502		 * All 1000baseT PHYs have a 1000baseT control register.
503		 */
504		if (sc->mii_extcapabilities & EXTSR_1000THDX) {
505			sc->mii_anegticks = MII_ANEGTICKS_GIGE;
506			sc->mii_flags |= MIIF_HAVE_GTCR;
507			mii->mii_media.ifm_mask |= IFM_ETH_MASTER;
508			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, 0,
509			    sc->mii_inst), MII_MEDIA_1000_T);
510			PRINT("1000baseT");
511		}
512		if (sc->mii_extcapabilities & EXTSR_1000TFDX) {
513			sc->mii_anegticks = MII_ANEGTICKS_GIGE;
514			sc->mii_flags |= MIIF_HAVE_GTCR;
515			mii->mii_media.ifm_mask |= IFM_ETH_MASTER;
516			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, IFM_FDX,
517			    sc->mii_inst), MII_MEDIA_1000_T_FDX);
518			PRINT("1000baseT-FDX");
519		}
520	}
521
522	if (sc->mii_capabilities & BMSR_ANEG) {
523		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst),
524		    MII_NMEDIA);	/* intentionally invalid index */
525		PRINT("auto");
526	}
527#undef ADD
528#undef PRINT
529}
530
531int
532mii_phy_detach(device_t dev)
533{
534	struct mii_softc *sc;
535
536	sc = device_get_softc(dev);
537	mii_phy_down(sc);
538	sc->mii_dev = NULL;
539	LIST_REMOVE(sc, mii_list);
540
541	return(0);
542}
543
544const struct mii_phydesc *
545mii_phy_match_gen(const struct mii_attach_args *ma,
546  const struct mii_phydesc *mpd, size_t len)
547{
548
549	for (; mpd->mpd_name != NULL;
550	     mpd = (const struct mii_phydesc *) ((const char *) mpd + len)) {
551		if (MII_OUI(ma->mii_id1, ma->mii_id2) == mpd->mpd_oui &&
552		    MII_MODEL(ma->mii_id2) == mpd->mpd_model)
553			return (mpd);
554	}
555	return (NULL);
556}
557
558const struct mii_phydesc *
559mii_phy_match(const struct mii_attach_args *ma, const struct mii_phydesc *mpd)
560{
561
562	return (mii_phy_match_gen(ma, mpd, sizeof(struct mii_phydesc)));
563}
564
565int
566mii_phy_dev_probe(device_t dev, const struct mii_phydesc *mpd, int mrv)
567{
568
569	mpd = mii_phy_match(device_get_ivars(dev), mpd);
570	if (mpd != NULL) {
571		device_set_desc(dev, mpd->mpd_name);
572		return (mrv);
573	}
574
575	return (ENXIO);
576}
577