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