1 /* $NetBSD: gentbi.c,v 1.15 2006/03/29 07:05:24 thorpej Exp $ */
4 * SPDX-License-Identifier: BSD-2-Clause-NetBSD AND BSD-2-Clause
6 * Copyright (c) 1998, 1999, 2000, 2001 The NetBSD Foundation, Inc.
9 * This code is derived from software contributed to The NetBSD Foundation
10 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
11 * NASA Ames Research Center.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
36 * Copyright (c) 1997 Manuel Bouyer. All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
47 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
48 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
49 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
50 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
51 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
52 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
53 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
54 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
55 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
56 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
60 * Driver for generic ten-bit (1000BASE-SX) interfaces, built into
61 * many Gigabit Ethernet chips.
63 * All we have to do here is correctly report speed and duplex.
66 #include <sys/cdefs.h>
67 __FBSDID("$FreeBSD$");
70 * Driver for generic unknown ten-bit interfaces(1000BASE-{LX,SX}
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/kernel.h>
77 #include <sys/module.h>
78 #include <sys/socket.h>
79 #include <sys/errno.h>
83 #include <net/if_media.h>
85 #include <dev/mii/mii.h>
86 #include <dev/mii/miivar.h>
89 #include "miibus_if.h"
91 static int gentbi_probe(device_t);
92 static int gentbi_attach(device_t);
94 static device_method_t gentbi_methods[] = {
95 /* device interface */
96 DEVMETHOD(device_probe, gentbi_probe),
97 DEVMETHOD(device_attach, gentbi_attach),
98 DEVMETHOD(device_detach, mii_phy_detach),
99 DEVMETHOD(device_shutdown, bus_generic_shutdown),
103 static devclass_t gentbi_devclass;
105 static driver_t gentbi_driver = {
108 sizeof(struct mii_softc)
111 DRIVER_MODULE(gentbi, miibus, gentbi_driver, gentbi_devclass, 0, 0);
113 static int gentbi_service(struct mii_softc *, struct mii_data *, int);
114 static void gentbi_status(struct mii_softc *);
116 static const struct mii_phy_funcs gentbi_funcs = {
123 gentbi_probe(device_t dev)
126 struct mii_attach_args *ma;
129 parent = device_get_parent(dev);
130 ma = device_get_ivars(dev);
133 * We match as a generic TBI if:
135 * - There is no media in the BMSR.
136 * - EXTSR has only 1000X.
138 bmsr = MIIBUS_READREG(parent, ma->mii_phyno, MII_BMSR);
139 if ((bmsr & BMSR_EXTSTAT) == 0 || (bmsr & BMSR_MEDIAMASK) != 0)
142 extsr = MIIBUS_READREG(parent, ma->mii_phyno, MII_EXTSR);
143 if (extsr & (EXTSR_1000TFDX|EXTSR_1000THDX))
146 if (extsr & (EXTSR_1000XFDX|EXTSR_1000XHDX)) {
148 * We think this is a generic TBI. Return a match
149 * priority higher than ukphy, but lower than what
150 * specific drivers will return.
152 device_set_desc(dev, "Generic ten-bit interface");
153 return (BUS_PROBE_LOW_PRIORITY);
160 gentbi_attach(device_t dev)
162 struct mii_softc *sc;
164 sc = device_get_softc(dev);
166 mii_phy_dev_attach(dev, MIIF_NOMANPAUSE, &gentbi_funcs, 0);
171 * Mask out all media in the BMSR. We only are really interested
174 sc->mii_capabilities =
175 PHY_READ(sc, MII_BMSR) & sc->mii_capmask & ~BMSR_MEDIAMASK;
176 if (sc->mii_capabilities & BMSR_EXTSTAT)
177 sc->mii_extcapabilities = PHY_READ(sc, MII_EXTSR);
179 device_printf(dev, " ");
180 mii_phy_add_media(sc);
183 MIIBUS_MEDIAINIT(sc->mii_dev);
188 gentbi_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
196 mii_phy_setmedia(sc);
200 if (mii_phy_tick(sc) == EJUSTRETURN)
205 /* Update the media status. */
208 /* Callback if something changed. */
209 mii_phy_update(sc, cmd);
214 gentbi_status(struct mii_softc *sc)
216 struct mii_data *mii = sc->mii_pdata;
217 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
218 int bmsr, bmcr, anlpar;
220 mii->mii_media_status = IFM_AVALID;
221 mii->mii_media_active = IFM_ETHER;
223 bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
225 if (bmsr & BMSR_LINK)
226 mii->mii_media_status |= IFM_ACTIVE;
228 bmcr = PHY_READ(sc, MII_BMCR);
229 if (bmcr & BMCR_ISO) {
230 mii->mii_media_active |= IFM_NONE;
231 mii->mii_media_status = 0;
235 if (bmcr & BMCR_LOOP)
236 mii->mii_media_active |= IFM_LOOP;
238 if (bmcr & BMCR_AUTOEN) {
240 * The media status bits are only valid if autonegotiation
241 * has completed (or it's disabled).
243 if ((bmsr & BMSR_ACOMP) == 0) {
244 /* Erg, still trying, I guess... */
245 mii->mii_media_active |= IFM_NONE;
250 * The media is always 1000baseSX. Check the ANLPAR to
251 * see if we're doing full-duplex.
253 mii->mii_media_active |= IFM_1000_SX;
254 anlpar = PHY_READ(sc, MII_ANLPAR);
255 if ((sc->mii_extcapabilities & EXTSR_1000XFDX) != 0 &&
256 (anlpar & ANLPAR_X_FD) != 0)
257 mii->mii_media_active |=
258 IFM_FDX | mii_phy_flowstatus(sc);
260 mii->mii_media_active |= IFM_HDX;
262 mii->mii_media_active = ife->ifm_media;