xref: /freebsd/sys/dev/mii/gentbi.c (revision 732a02b4e77866604a120a275c082bb6221bd2ff)
1 /*	$NetBSD: gentbi.c,v 1.15 2006/03/29 07:05:24 thorpej Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-2-Clause-NetBSD AND 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 __FBSDID("$FreeBSD$");
68 
69 /*
70  * Driver for generic unknown ten-bit interfaces(1000BASE-{LX,SX}
71  * fiber interfaces).
72  */
73 
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>
80 #include <sys/bus.h>
81 
82 #include <net/if.h>
83 #include <net/if_media.h>
84 
85 #include <dev/mii/mii.h>
86 #include <dev/mii/miivar.h>
87 #include "miidevs.h"
88 
89 #include "miibus_if.h"
90 
91 static int	gentbi_probe(device_t);
92 static int	gentbi_attach(device_t);
93 
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),
100 	DEVMETHOD_END
101 };
102 
103 static devclass_t gentbi_devclass;
104 
105 static driver_t gentbi_driver = {
106 	"gentbi",
107 	gentbi_methods,
108 	sizeof(struct mii_softc)
109 };
110 
111 DRIVER_MODULE(gentbi, miibus, gentbi_driver, gentbi_devclass, 0, 0);
112 
113 static int	gentbi_service(struct mii_softc *, struct mii_data *, int);
114 static void	gentbi_status(struct mii_softc *);
115 
116 static const struct mii_phy_funcs gentbi_funcs = {
117 	gentbi_service,
118 	gentbi_status,
119 	mii_phy_reset
120 };
121 
122 static int
123 gentbi_probe(device_t dev)
124 {
125 	device_t parent;
126 	struct mii_attach_args *ma;
127 	int bmsr, extsr;
128 
129 	parent = device_get_parent(dev);
130 	ma = device_get_ivars(dev);
131 
132 	/*
133 	 * We match as a generic TBI if:
134 	 *
135 	 *	- There is no media in the BMSR.
136 	 *	- EXTSR has only 1000X.
137 	 */
138 	bmsr = MIIBUS_READREG(parent, ma->mii_phyno, MII_BMSR);
139 	if ((bmsr & BMSR_EXTSTAT) == 0 || (bmsr & BMSR_MEDIAMASK) != 0)
140 		return (ENXIO);
141 
142 	extsr = MIIBUS_READREG(parent, ma->mii_phyno, MII_EXTSR);
143 	if (extsr & (EXTSR_1000TFDX|EXTSR_1000THDX))
144 		return (ENXIO);
145 
146 	if (extsr & (EXTSR_1000XFDX|EXTSR_1000XHDX)) {
147 		/*
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.
151 		 */
152 		device_set_desc(dev, "Generic ten-bit interface");
153 		return (BUS_PROBE_LOW_PRIORITY);
154 	}
155 
156 	return (ENXIO);
157 }
158 
159 static int
160 gentbi_attach(device_t dev)
161 {
162 	struct mii_softc *sc;
163 
164 	sc = device_get_softc(dev);
165 
166 	mii_phy_dev_attach(dev, MIIF_NOMANPAUSE, &gentbi_funcs, 0);
167 
168 	PHY_RESET(sc);
169 
170 	/*
171 	 * Mask out all media in the BMSR.  We only are really interested
172 	 * in "auto".
173 	 */
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);
178 
179 	device_printf(dev, " ");
180 	mii_phy_add_media(sc);
181 	printf("\n");
182 
183 	MIIBUS_MEDIAINIT(sc->mii_dev);
184 	return (0);
185 }
186 
187 static int
188 gentbi_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
189 {
190 
191 	switch (cmd) {
192 	case MII_POLLSTAT:
193 		break;
194 
195 	case MII_MEDIACHG:
196 		mii_phy_setmedia(sc);
197 		break;
198 
199 	case MII_TICK:
200 		if (mii_phy_tick(sc) == EJUSTRETURN)
201 			return (0);
202 		break;
203 	}
204 
205 	/* Update the media status. */
206 	PHY_STATUS(sc);
207 
208 	/* Callback if something changed. */
209 	mii_phy_update(sc, cmd);
210 	return (0);
211 }
212 
213 static void
214 gentbi_status(struct mii_softc *sc)
215 {
216 	struct mii_data *mii = sc->mii_pdata;
217 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
218 	int bmsr, bmcr, anlpar;
219 
220 	mii->mii_media_status = IFM_AVALID;
221 	mii->mii_media_active = IFM_ETHER;
222 
223 	bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
224 
225 	if (bmsr & BMSR_LINK)
226 		mii->mii_media_status |= IFM_ACTIVE;
227 
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;
232 		return;
233 	}
234 
235 	if (bmcr & BMCR_LOOP)
236 		mii->mii_media_active |= IFM_LOOP;
237 
238 	if (bmcr & BMCR_AUTOEN) {
239 		/*
240 		 * The media status bits are only valid if autonegotiation
241 		 * has completed (or it's disabled).
242 		 */
243 		if ((bmsr & BMSR_ACOMP) == 0) {
244 			/* Erg, still trying, I guess... */
245 			mii->mii_media_active |= IFM_NONE;
246 			return;
247 		}
248 
249 		/*
250 		 * The media is always 1000baseSX.  Check the ANLPAR to
251 		 * see if we're doing full-duplex.
252 		 */
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);
259 		else
260 			mii->mii_media_active |= IFM_HDX;
261 	} else
262 		mii->mii_media_active = ife->ifm_media;
263 }
264