gentbi.c revision 213229
1/*	$NetBSD: gentbi.c,v 1.15 2006/03/29 07:05:24 thorpej 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/*
34 * Copyright (c) 1997 Manuel Bouyer.  All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 *    notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 *    notice, this list of conditions and the following disclaimer in the
43 *    documentation and/or other materials provided with the distribution.
44 *
45 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
46 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
47 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
48 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
49 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
50 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
51 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
52 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
53 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
54 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
55 */
56
57/*
58 * Driver for generic ten-bit (1000BASE-SX) interfaces, built in to
59 * many Gigabit Ethernet chips.
60 *
61 * All we have to do here is correctly report speed and duplex.
62 */
63
64#include <sys/cdefs.h>
65__FBSDID("$FreeBSD: head/sys/dev/mii/gentbi.c 213229 2010-09-27 20:31:03Z marius $");
66
67/*
68 * Driver for generic unknown ten-bit interfaces(1000BASE-{LX,SX}
69 * fiber interfaces).
70 */
71
72#include <sys/param.h>
73#include <sys/systm.h>
74#include <sys/kernel.h>
75#include <sys/module.h>
76#include <sys/socket.h>
77#include <sys/errno.h>
78#include <sys/bus.h>
79
80#include <net/if.h>
81#include <net/if_media.h>
82
83#include <dev/mii/mii.h>
84#include <dev/mii/miivar.h>
85#include "miidevs.h"
86
87#include "miibus_if.h"
88
89static int	gentbi_probe(device_t);
90static int	gentbi_attach(device_t);
91
92static device_method_t gentbi_methods[] = {
93	/* device interface */
94	DEVMETHOD(device_probe,		gentbi_probe),
95	DEVMETHOD(device_attach,	gentbi_attach),
96	DEVMETHOD(device_detach,	mii_phy_detach),
97	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
98	{0, 0}
99};
100
101static devclass_t gentbi_devclass;
102
103static driver_t gentbi_driver = {
104	"gentbi",
105	gentbi_methods,
106	sizeof(struct mii_softc)
107};
108
109DRIVER_MODULE(gentbi, miibus, gentbi_driver, gentbi_devclass, 0, 0);
110
111static int	gentbi_service(struct mii_softc *, struct mii_data *, int);
112static void	gentbi_status(struct mii_softc *);
113
114static int
115gentbi_probe(device_t dev)
116{
117	device_t parent;
118	struct mii_attach_args *ma;
119	int bmsr, extsr;
120
121	parent = device_get_parent(dev);
122	ma = device_get_ivars(dev);
123
124	/*
125	 * We match as a generic TBI if:
126	 *
127	 *	- There is no media in the BMSR.
128	 *	- EXTSR has only 1000X.
129	 */
130	bmsr = MIIBUS_READREG(parent, ma->mii_phyno, MII_BMSR);
131	if ((bmsr & BMSR_EXTSTAT) == 0 || (bmsr & BMSR_MEDIAMASK) != 0)
132		return (ENXIO);
133
134	extsr = MIIBUS_READREG(parent, ma->mii_phyno, MII_EXTSR);
135	if (extsr & (EXTSR_1000TFDX|EXTSR_1000THDX))
136		return (ENXIO);
137
138	if (extsr & (EXTSR_1000XFDX|EXTSR_1000XHDX)) {
139		/*
140		 * We think this is a generic TBI.  Return a match
141		 * priority higher than ukphy, but lower than what
142		 * specific drivers will return.
143		 */
144		device_set_desc(dev, "Generic ten-bit interface");
145		return (BUS_PROBE_LOW_PRIORITY);
146	}
147
148	return (ENXIO);
149}
150
151static int
152gentbi_attach(device_t dev)
153{
154	struct mii_softc *sc;
155	struct mii_attach_args *ma;
156	struct mii_data *mii;
157
158	sc = device_get_softc(dev);
159	ma = device_get_ivars(dev);
160	sc->mii_dev = device_get_parent(dev);
161	mii = ma->mii_data;
162	LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
163
164	if (bootverbose)
165		device_printf(dev, "OUI 0x%06x, model 0x%04x, rev. %d\n",
166		    MII_OUI(ma->mii_id1, ma->mii_id2),
167		    MII_MODEL(ma->mii_id2), MII_REV(ma->mii_id2));
168
169	sc->mii_inst = mii->mii_instance;
170	sc->mii_phy = ma->mii_phyno;
171	sc->mii_service = gentbi_service;
172	sc->mii_pdata = mii;
173
174	mii->mii_instance++;
175
176	mii_phy_reset(sc);
177
178	/*
179	 * Mask out all media in the BMSR.  We only are really interested
180	 * in "auto".
181	 */
182	sc->mii_capabilities =
183	    PHY_READ(sc, MII_BMSR) & ma->mii_capmask & ~BMSR_MEDIAMASK;
184	if (sc->mii_capabilities & BMSR_EXTSTAT)
185		sc->mii_extcapabilities = PHY_READ(sc, MII_EXTSR);
186
187	device_printf(dev, " ");
188	mii_phy_add_media(sc);
189	printf("\n");
190
191	MIIBUS_MEDIAINIT(sc->mii_dev);
192	return (0);
193}
194
195static int
196gentbi_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
197{
198	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
199	int reg;
200
201	switch (cmd) {
202	case MII_POLLSTAT:
203		/*
204		 * If we're not polling our PHY instance, just return.
205		 */
206		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
207			return (0);
208		break;
209
210	case MII_MEDIACHG:
211		/*
212		 * If the media indicates a different PHY instance,
213		 * isolate ourselves.
214		 */
215		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
216			reg = PHY_READ(sc, MII_BMCR);
217			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
218			return (0);
219		}
220
221		/*
222		 * If the interface is not up, don't do anything.
223		 */
224		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
225			break;
226
227		mii_phy_setmedia(sc);
228		break;
229
230	case MII_TICK:
231		/*
232		 * If we're not currently selected, just return.
233		 */
234		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
235			return (0);
236
237		if (mii_phy_tick(sc) == EJUSTRETURN)
238			return (0);
239		break;
240	}
241
242	/* Update the media status. */
243	gentbi_status(sc);
244
245	/* Callback if something changed. */
246	mii_phy_update(sc, cmd);
247	return (0);
248}
249
250static void
251gentbi_status(struct mii_softc *sc)
252{
253	struct mii_data *mii = sc->mii_pdata;
254	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
255	int bmsr, bmcr, anlpar;
256
257	mii->mii_media_status = IFM_AVALID;
258	mii->mii_media_active = IFM_ETHER;
259
260	bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
261
262	if (bmsr & BMSR_LINK)
263		mii->mii_media_status |= IFM_ACTIVE;
264
265	bmcr = PHY_READ(sc, MII_BMCR);
266	if (bmcr & BMCR_ISO) {
267		mii->mii_media_active |= IFM_NONE;
268		mii->mii_media_status = 0;
269		return;
270	}
271
272	if (bmcr & BMCR_LOOP)
273		mii->mii_media_active |= IFM_LOOP;
274
275	if (bmcr & BMCR_AUTOEN) {
276		/*
277		 * The media status bits are only valid if autonegotiation
278		 * has completed (or it's disabled).
279		 */
280		if ((bmsr & BMSR_ACOMP) == 0) {
281			/* Erg, still trying, I guess... */
282			mii->mii_media_active |= IFM_NONE;
283			return;
284		}
285
286		/*
287		 * The media is always 1000baseSX.  Check the ANLPAR to
288		 * see if we're doing full-duplex.
289		 */
290		mii->mii_media_active |= IFM_1000_SX;
291
292		anlpar = PHY_READ(sc, MII_ANLPAR);
293		if ((sc->mii_extcapabilities & EXTSR_1000XFDX) != 0 &&
294		    (anlpar & ANLPAR_X_FD) != 0)
295			mii->mii_media_active |= IFM_FDX;
296	} else
297		mii->mii_media_active = ife->ifm_media;
298}
299