mii_physubr.c revision 214608
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 214608 2010-10-31 22:59:49Z 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 */
59static const 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) != 0)
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) != 0) {
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			printf("mii_phy_setmedia: MASTER on wrong media\n");
135		}
136	}
137
138	if ((ife->ifm_media & IFM_LOOP) != 0)
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) != 0)
144		PHY_WRITE(sc, MII_100T2CR, gtcr);
145}
146
147int
148mii_phy_auto(struct mii_softc *sc)
149{
150	int anar, gtcr;
151
152	/*
153	 * Check for 1000BASE-X.  Autonegotiation is a bit
154	 * different on such devices.
155	 */
156	if ((sc->mii_flags & MIIF_IS_1000X) != 0) {
157		anar = 0;
158		if ((sc->mii_extcapabilities & EXTSR_1000XFDX) != 0)
159			anar |= ANAR_X_FD;
160		if ((sc->mii_extcapabilities & EXTSR_1000XHDX) != 0)
161			anar |= ANAR_X_HD;
162
163		if ((sc->mii_flags & MIIF_DOPAUSE) != 0) {
164			/* XXX Asymmetric vs. symmetric? */
165			anar |= ANLPAR_X_PAUSE_TOWARDS;
166		}
167		PHY_WRITE(sc, MII_ANAR, anar);
168	} else {
169		anar = BMSR_MEDIA_TO_ANAR(sc->mii_capabilities) |
170		    ANAR_CSMA;
171		if ((sc->mii_flags & MIIF_DOPAUSE) != 0)
172			anar |= ANAR_FC;
173		PHY_WRITE(sc, MII_ANAR, anar);
174		if ((sc->mii_flags & MIIF_HAVE_GTCR) != 0) {
175			gtcr = 0;
176			if ((sc->mii_extcapabilities & EXTSR_1000TFDX) != 0)
177				gtcr |= GTCR_ADV_1000TFDX;
178			if ((sc->mii_extcapabilities & EXTSR_1000THDX) != 0)
179				gtcr |= GTCR_ADV_1000THDX;
180			PHY_WRITE(sc, MII_100T2CR, gtcr);
181		}
182	}
183	PHY_WRITE(sc, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG);
184	return (EJUSTRETURN);
185}
186
187int
188mii_phy_tick(struct mii_softc *sc)
189{
190	struct ifmedia_entry *ife = sc->mii_pdata->mii_media.ifm_cur;
191	struct ifnet *ifp = sc->mii_pdata->mii_ifp;
192	int reg;
193
194	/* Just bail now if the interface is down. */
195	if ((ifp->if_flags & IFF_UP) == 0)
196		return (EJUSTRETURN);
197
198	/*
199	 * If we're not doing autonegotiation, we don't need to do
200	 * any extra work here.  However, we need to check the link
201	 * status so we can generate an announcement if the status
202	 * changes.
203	 */
204	if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) {
205		sc->mii_ticks = 0;	/* reset autonegotiation timer. */
206		return (0);
207	}
208
209	/* Read the status register twice; BMSR_LINK is latch-low. */
210	reg = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
211	if ((reg & BMSR_LINK) != 0) {
212		sc->mii_ticks = 0;	/* reset autonegotiation timer. */
213		/* See above. */
214		return (0);
215	}
216
217	/* Announce link loss right after it happens */
218	if (sc->mii_ticks++ == 0)
219		return (0);
220
221	/* XXX: use default value if phy driver did not set mii_anegticks */
222	if (sc->mii_anegticks == 0)
223		sc->mii_anegticks = MII_ANEGTICKS_GIGE;
224
225	/* Only retry autonegotiation every mii_anegticks ticks. */
226	if (sc->mii_ticks <= sc->mii_anegticks)
227		return (EJUSTRETURN);
228
229	sc->mii_ticks = 0;
230	mii_phy_reset(sc);
231	mii_phy_auto(sc);
232	return (0);
233}
234
235void
236mii_phy_reset(struct mii_softc *sc)
237{
238	struct ifmedia_entry *ife = sc->mii_pdata->mii_media.ifm_cur;
239	int reg, i;
240
241	if ((sc->mii_flags & MIIF_NOISOLATE) != 0)
242		reg = BMCR_RESET;
243	else
244		reg = BMCR_RESET | BMCR_ISO;
245	PHY_WRITE(sc, MII_BMCR, reg);
246
247	/* Wait 100ms for it to complete. */
248	for (i = 0; i < 100; i++) {
249		reg = PHY_READ(sc, MII_BMCR);
250		if ((reg & BMCR_RESET) == 0)
251			break;
252		DELAY(1000);
253	}
254
255	if ((sc->mii_flags & MIIF_NOISOLATE) == 0) {
256		if ((ife == NULL && sc->mii_inst != 0) ||
257		    (ife != NULL && IFM_INST(ife->ifm_media) != sc->mii_inst))
258			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
259	}
260}
261
262void
263mii_phy_down(struct mii_softc *sc)
264{
265
266}
267
268void
269mii_phy_update(struct mii_softc *sc, int cmd)
270{
271	struct mii_data *mii = sc->mii_pdata;
272
273	if (sc->mii_media_active != mii->mii_media_active ||
274	    cmd == MII_MEDIACHG) {
275		MIIBUS_STATCHG(sc->mii_dev);
276		sc->mii_media_active = mii->mii_media_active;
277	}
278	if (sc->mii_media_status != mii->mii_media_status) {
279		MIIBUS_LINKCHG(sc->mii_dev);
280		sc->mii_media_status = mii->mii_media_status;
281	}
282}
283
284/*
285 * Initialize generic PHY media based on BMSR, called when a PHY is
286 * attached.  We expect to be set up to print a comma-separated list
287 * of media names.  Does not print a newline.
288 */
289void
290mii_phy_add_media(struct mii_softc *sc)
291{
292	struct mii_data *mii = sc->mii_pdata;
293	const char *sep = "";
294
295	if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0 &&
296	    (sc->mii_extcapabilities & EXTSR_MEDIAMASK) == 0) {
297		printf("no media present");
298		return;
299	}
300
301	/*
302	 * Set the autonegotiation timer for 10/100 media.  Gigabit media is
303	 * handled below.
304	 */
305	sc->mii_anegticks = MII_ANEGTICKS;
306
307#define	ADD(m, c)	ifmedia_add(&mii->mii_media, (m), (c), NULL)
308#define	PRINT(s)	printf("%s%s", sep, s); sep = ", "
309
310	if ((sc->mii_flags & MIIF_NOISOLATE) == 0)
311		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
312		    MII_MEDIA_NONE);
313
314	/*
315	 * There are different interpretations for the bits in
316	 * HomePNA PHYs.  And there is really only one media type
317	 * that is supported.
318	 */
319	if ((sc->mii_flags & MIIF_IS_HPNA) != 0) {
320		if ((sc->mii_capabilities & BMSR_10THDX) != 0) {
321			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_HPNA_1, 0,
322			    sc->mii_inst), MII_MEDIA_10_T);
323			PRINT("HomePNA1");
324		}
325		return;
326	}
327
328	if ((sc->mii_capabilities & BMSR_10THDX) != 0) {
329		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, sc->mii_inst),
330		    MII_MEDIA_10_T);
331		PRINT("10baseT");
332	}
333	if ((sc->mii_capabilities & BMSR_10TFDX) != 0) {
334		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, sc->mii_inst),
335		    MII_MEDIA_10_T_FDX);
336		PRINT("10baseT-FDX");
337	}
338	if ((sc->mii_capabilities & BMSR_100TXHDX) != 0) {
339		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, sc->mii_inst),
340		    MII_MEDIA_100_TX);
341		PRINT("100baseTX");
342	}
343	if ((sc->mii_capabilities & BMSR_100TXFDX) != 0) {
344		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, sc->mii_inst),
345		    MII_MEDIA_100_TX_FDX);
346		PRINT("100baseTX-FDX");
347	}
348	if ((sc->mii_capabilities & BMSR_100T4) != 0) {
349		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_T4, 0, sc->mii_inst),
350		    MII_MEDIA_100_T4);
351		PRINT("100baseT4");
352	}
353
354	if ((sc->mii_extcapabilities & EXTSR_MEDIAMASK) != 0) {
355		/*
356		 * XXX Right now only handle 1000SX and 1000TX.  Need
357		 * XXX to handle 1000LX and 1000CX somehow.
358		 */
359		if ((sc->mii_extcapabilities & EXTSR_1000XHDX) != 0) {
360			sc->mii_anegticks = MII_ANEGTICKS_GIGE;
361			sc->mii_flags |= MIIF_IS_1000X;
362			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, 0,
363			    sc->mii_inst), MII_MEDIA_1000_X);
364			PRINT("1000baseSX");
365		}
366		if ((sc->mii_extcapabilities & EXTSR_1000XFDX) != 0) {
367			sc->mii_anegticks = MII_ANEGTICKS_GIGE;
368			sc->mii_flags |= MIIF_IS_1000X;
369			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, IFM_FDX,
370			    sc->mii_inst), MII_MEDIA_1000_X_FDX);
371			PRINT("1000baseSX-FDX");
372		}
373
374		/*
375		 * 1000baseT media needs to be able to manipulate
376		 * master/slave mode.  We set IFM_ETH_MASTER in
377		 * the "don't care mask" and filter it out when
378		 * the media is set.
379		 *
380		 * All 1000baseT PHYs have a 1000baseT control register.
381		 */
382		if ((sc->mii_extcapabilities & EXTSR_1000THDX) != 0) {
383			sc->mii_anegticks = MII_ANEGTICKS_GIGE;
384			sc->mii_flags |= MIIF_HAVE_GTCR;
385			mii->mii_media.ifm_mask |= IFM_ETH_MASTER;
386			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, 0,
387			    sc->mii_inst), MII_MEDIA_1000_T);
388			PRINT("1000baseT");
389		}
390		if ((sc->mii_extcapabilities & EXTSR_1000TFDX) != 0) {
391			sc->mii_anegticks = MII_ANEGTICKS_GIGE;
392			sc->mii_flags |= MIIF_HAVE_GTCR;
393			mii->mii_media.ifm_mask |= IFM_ETH_MASTER;
394			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, IFM_FDX,
395			    sc->mii_inst), MII_MEDIA_1000_T_FDX);
396			PRINT("1000baseT-FDX");
397		}
398	}
399
400	if ((sc->mii_capabilities & BMSR_ANEG) != 0) {
401		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst),
402		    MII_NMEDIA);	/* intentionally invalid index */
403		PRINT("auto");
404	}
405#undef ADD
406#undef PRINT
407}
408
409int
410mii_phy_detach(device_t dev)
411{
412	struct mii_softc *sc;
413
414	sc = device_get_softc(dev);
415	mii_phy_down(sc);
416	sc->mii_dev = NULL;
417	LIST_REMOVE(sc, mii_list);
418	return (0);
419}
420
421const struct mii_phydesc *
422mii_phy_match_gen(const struct mii_attach_args *ma,
423  const struct mii_phydesc *mpd, size_t len)
424{
425
426	for (; mpd->mpd_name != NULL;
427	     mpd = (const struct mii_phydesc *) ((const char *) mpd + len)) {
428		if (MII_OUI(ma->mii_id1, ma->mii_id2) == mpd->mpd_oui &&
429		    MII_MODEL(ma->mii_id2) == mpd->mpd_model)
430			return (mpd);
431	}
432	return (NULL);
433}
434
435const struct mii_phydesc *
436mii_phy_match(const struct mii_attach_args *ma, const struct mii_phydesc *mpd)
437{
438
439	return (mii_phy_match_gen(ma, mpd, sizeof(struct mii_phydesc)));
440}
441
442int
443mii_phy_dev_probe(device_t dev, const struct mii_phydesc *mpd, int mrv)
444{
445
446	mpd = mii_phy_match(device_get_ivars(dev), mpd);
447	if (mpd != NULL) {
448		device_set_desc(dev, mpd->mpd_name);
449		return (mrv);
450	}
451	return (ENXIO);
452}
453