xref: /freebsd/sys/dev/mii/gentbi.c (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
1 /*	$NetBSD: gentbi.c,v 1.15 2006/03/29 07:05:24 thorpej Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-2-Clause
5  *
6  * Copyright (c) 1998, 1999, 2000, 2001 The NetBSD Foundation, Inc.
7  * All rights reserved.
8  *
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.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
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.
21  *
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.
33  */
34 
35 /*
36  * Copyright (c) 1997 Manuel Bouyer.  All rights reserved.
37  *
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions
40  * are met:
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.
46  *
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.
57  */
58 
59 /*
60  * Driver for generic ten-bit (1000BASE-SX) interfaces, built into
61  * many Gigabit Ethernet chips.
62  *
63  * All we have to do here is correctly report speed and duplex.
64  */
65 
66 #include <sys/cdefs.h>
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 
89 static int	gentbi_probe(device_t);
90 static int	gentbi_attach(device_t);
91 
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),
98 	DEVMETHOD_END
99 };
100 
101 static driver_t gentbi_driver = {
102 	"gentbi",
103 	gentbi_methods,
104 	sizeof(struct mii_softc)
105 };
106 
107 DRIVER_MODULE(gentbi, miibus, gentbi_driver, 0, 0);
108 
109 static int	gentbi_service(struct mii_softc *, struct mii_data *, int);
110 static void	gentbi_status(struct mii_softc *);
111 
112 static const struct mii_phy_funcs gentbi_funcs = {
113 	gentbi_service,
114 	gentbi_status,
115 	mii_phy_reset
116 };
117 
118 static int
119 gentbi_probe(device_t dev)
120 {
121 	device_t parent;
122 	struct mii_attach_args *ma;
123 	int bmsr, extsr;
124 
125 	parent = device_get_parent(dev);
126 	ma = device_get_ivars(dev);
127 
128 	/*
129 	 * We match as a generic TBI if:
130 	 *
131 	 *	- There is no media in the BMSR.
132 	 *	- EXTSR has only 1000X.
133 	 */
134 	bmsr = MIIBUS_READREG(parent, ma->mii_phyno, MII_BMSR);
135 	if ((bmsr & BMSR_EXTSTAT) == 0 || (bmsr & BMSR_MEDIAMASK) != 0)
136 		return (ENXIO);
137 
138 	extsr = MIIBUS_READREG(parent, ma->mii_phyno, MII_EXTSR);
139 	if (extsr & (EXTSR_1000TFDX|EXTSR_1000THDX))
140 		return (ENXIO);
141 
142 	if (extsr & (EXTSR_1000XFDX|EXTSR_1000XHDX)) {
143 		/*
144 		 * We think this is a generic TBI.  Return a match
145 		 * priority higher than ukphy, but lower than what
146 		 * specific drivers will return.
147 		 */
148 		device_set_desc(dev, "Generic ten-bit interface");
149 		return (BUS_PROBE_LOW_PRIORITY);
150 	}
151 
152 	return (ENXIO);
153 }
154 
155 static int
156 gentbi_attach(device_t dev)
157 {
158 	struct mii_softc *sc;
159 
160 	sc = device_get_softc(dev);
161 
162 	mii_phy_dev_attach(dev, MIIF_NOMANPAUSE, &gentbi_funcs, 0);
163 
164 	PHY_RESET(sc);
165 
166 	/*
167 	 * Mask out all media in the BMSR.  We only are really interested
168 	 * in "auto".
169 	 */
170 	sc->mii_capabilities =
171 	    PHY_READ(sc, MII_BMSR) & sc->mii_capmask & ~BMSR_MEDIAMASK;
172 	if (sc->mii_capabilities & BMSR_EXTSTAT)
173 		sc->mii_extcapabilities = PHY_READ(sc, MII_EXTSR);
174 
175 	device_printf(dev, " ");
176 	mii_phy_add_media(sc);
177 	printf("\n");
178 
179 	MIIBUS_MEDIAINIT(sc->mii_dev);
180 	return (0);
181 }
182 
183 static int
184 gentbi_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
185 {
186 
187 	switch (cmd) {
188 	case MII_POLLSTAT:
189 		break;
190 
191 	case MII_MEDIACHG:
192 		mii_phy_setmedia(sc);
193 		break;
194 
195 	case MII_TICK:
196 		if (mii_phy_tick(sc) == EJUSTRETURN)
197 			return (0);
198 		break;
199 	}
200 
201 	/* Update the media status. */
202 	PHY_STATUS(sc);
203 
204 	/* Callback if something changed. */
205 	mii_phy_update(sc, cmd);
206 	return (0);
207 }
208 
209 static void
210 gentbi_status(struct mii_softc *sc)
211 {
212 	struct mii_data *mii = sc->mii_pdata;
213 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
214 	int bmsr, bmcr, anlpar;
215 
216 	mii->mii_media_status = IFM_AVALID;
217 	mii->mii_media_active = IFM_ETHER;
218 
219 	bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
220 
221 	if (bmsr & BMSR_LINK)
222 		mii->mii_media_status |= IFM_ACTIVE;
223 
224 	bmcr = PHY_READ(sc, MII_BMCR);
225 	if (bmcr & BMCR_ISO) {
226 		mii->mii_media_active |= IFM_NONE;
227 		mii->mii_media_status = 0;
228 		return;
229 	}
230 
231 	if (bmcr & BMCR_LOOP)
232 		mii->mii_media_active |= IFM_LOOP;
233 
234 	if (bmcr & BMCR_AUTOEN) {
235 		/*
236 		 * The media status bits are only valid if autonegotiation
237 		 * has completed (or it's disabled).
238 		 */
239 		if ((bmsr & BMSR_ACOMP) == 0) {
240 			/* Erg, still trying, I guess... */
241 			mii->mii_media_active |= IFM_NONE;
242 			return;
243 		}
244 
245 		/*
246 		 * The media is always 1000baseSX.  Check the ANLPAR to
247 		 * see if we're doing full-duplex.
248 		 */
249 		mii->mii_media_active |= IFM_1000_SX;
250 		anlpar = PHY_READ(sc, MII_ANLPAR);
251 		if ((sc->mii_extcapabilities & EXTSR_1000XFDX) != 0 &&
252 		    (anlpar & ANLPAR_X_FD) != 0)
253 			mii->mii_media_active |=
254 			    IFM_FDX | mii_phy_flowstatus(sc);
255 		else
256 			mii->mii_media_active |= IFM_HDX;
257 	} else
258 		mii->mii_media_active = ife->ifm_media;
259 }
260