1 /* $NetBSD: gentbi.c,v 1.15 2006/03/29 07:05:24 thorpej Exp $ */
4 * Copyright (c) 1998, 1999, 2000, 2001 The NetBSD Foundation, Inc.
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.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
34 * Copyright (c) 1997 Manuel Bouyer. All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
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.
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.
58 * Driver for generic ten-bit (1000BASE-SX) interfaces, built into
59 * many Gigabit Ethernet chips.
61 * All we have to do here is correctly report speed and duplex.
64 #include <sys/cdefs.h>
65 __FBSDID("$FreeBSD$");
68 * Driver for generic unknown ten-bit interfaces(1000BASE-{LX,SX}
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>
81 #include <net/if_media.h>
83 #include <dev/mii/mii.h>
84 #include <dev/mii/miivar.h>
87 #include "miibus_if.h"
89 static int gentbi_probe(device_t);
90 static int gentbi_attach(device_t);
92 static 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),
101 static devclass_t gentbi_devclass;
103 static driver_t gentbi_driver = {
106 sizeof(struct mii_softc)
109 DRIVER_MODULE(gentbi, miibus, gentbi_driver, gentbi_devclass, 0, 0);
111 static int gentbi_service(struct mii_softc *, struct mii_data *, int);
112 static void gentbi_status(struct mii_softc *);
114 static const struct mii_phy_funcs gentbi_funcs = {
121 gentbi_probe(device_t dev)
124 struct mii_attach_args *ma;
127 parent = device_get_parent(dev);
128 ma = device_get_ivars(dev);
131 * We match as a generic TBI if:
133 * - There is no media in the BMSR.
134 * - EXTSR has only 1000X.
136 bmsr = MIIBUS_READREG(parent, ma->mii_phyno, MII_BMSR);
137 if ((bmsr & BMSR_EXTSTAT) == 0 || (bmsr & BMSR_MEDIAMASK) != 0)
140 extsr = MIIBUS_READREG(parent, ma->mii_phyno, MII_EXTSR);
141 if (extsr & (EXTSR_1000TFDX|EXTSR_1000THDX))
144 if (extsr & (EXTSR_1000XFDX|EXTSR_1000XHDX)) {
146 * We think this is a generic TBI. Return a match
147 * priority higher than ukphy, but lower than what
148 * specific drivers will return.
150 device_set_desc(dev, "Generic ten-bit interface");
151 return (BUS_PROBE_LOW_PRIORITY);
158 gentbi_attach(device_t dev)
160 struct mii_softc *sc;
162 sc = device_get_softc(dev);
164 mii_phy_dev_attach(dev, MIIF_NOMANPAUSE, &gentbi_funcs, 0);
169 * Mask out all media in the BMSR. We only are really interested
172 sc->mii_capabilities =
173 PHY_READ(sc, MII_BMSR) & sc->mii_capmask & ~BMSR_MEDIAMASK;
174 if (sc->mii_capabilities & BMSR_EXTSTAT)
175 sc->mii_extcapabilities = PHY_READ(sc, MII_EXTSR);
177 device_printf(dev, " ");
178 mii_phy_add_media(sc);
181 MIIBUS_MEDIAINIT(sc->mii_dev);
186 gentbi_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
195 * If the interface is not up, don't do anything.
197 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
200 mii_phy_setmedia(sc);
204 if (mii_phy_tick(sc) == EJUSTRETURN)
209 /* Update the media status. */
212 /* Callback if something changed. */
213 mii_phy_update(sc, cmd);
218 gentbi_status(struct mii_softc *sc)
220 struct mii_data *mii = sc->mii_pdata;
221 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
222 int bmsr, bmcr, anlpar;
224 mii->mii_media_status = IFM_AVALID;
225 mii->mii_media_active = IFM_ETHER;
227 bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
229 if (bmsr & BMSR_LINK)
230 mii->mii_media_status |= IFM_ACTIVE;
232 bmcr = PHY_READ(sc, MII_BMCR);
233 if (bmcr & BMCR_ISO) {
234 mii->mii_media_active |= IFM_NONE;
235 mii->mii_media_status = 0;
239 if (bmcr & BMCR_LOOP)
240 mii->mii_media_active |= IFM_LOOP;
242 if (bmcr & BMCR_AUTOEN) {
244 * The media status bits are only valid if autonegotiation
245 * has completed (or it's disabled).
247 if ((bmsr & BMSR_ACOMP) == 0) {
248 /* Erg, still trying, I guess... */
249 mii->mii_media_active |= IFM_NONE;
254 * The media is always 1000baseSX. Check the ANLPAR to
255 * see if we're doing full-duplex.
257 mii->mii_media_active |= IFM_1000_SX;
258 anlpar = PHY_READ(sc, MII_ANLPAR);
259 if ((sc->mii_extcapabilities & EXTSR_1000XFDX) != 0 &&
260 (anlpar & ANLPAR_X_FD) != 0)
261 mii->mii_media_active |=
262 IFM_FDX | mii_phy_flowstatus(sc);
264 mii->mii_media_active |= IFM_HDX;
266 mii->mii_media_active = ife->ifm_media;