xref: /freebsd/sys/dev/mii/gentbi.c (revision 685dc743dc3b5645e34836464128e1c0558b404b)
1972fbe5aSPyun YongHyeon /*	$NetBSD: gentbi.c,v 1.15 2006/03/29 07:05:24 thorpej Exp $	*/
2972fbe5aSPyun YongHyeon 
3972fbe5aSPyun YongHyeon /*-
4*eebd9d53SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
5718cf2ccSPedro F. Giffuni  *
6972fbe5aSPyun YongHyeon  * Copyright (c) 1998, 1999, 2000, 2001 The NetBSD Foundation, Inc.
7972fbe5aSPyun YongHyeon  * All rights reserved.
8972fbe5aSPyun YongHyeon  *
9972fbe5aSPyun YongHyeon  * This code is derived from software contributed to The NetBSD Foundation
10972fbe5aSPyun YongHyeon  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
11972fbe5aSPyun YongHyeon  * NASA Ames Research Center.
12972fbe5aSPyun YongHyeon  *
13972fbe5aSPyun YongHyeon  * Redistribution and use in source and binary forms, with or without
14972fbe5aSPyun YongHyeon  * modification, are permitted provided that the following conditions
15972fbe5aSPyun YongHyeon  * are met:
16972fbe5aSPyun YongHyeon  * 1. Redistributions of source code must retain the above copyright
17972fbe5aSPyun YongHyeon  *    notice, this list of conditions and the following disclaimer.
18972fbe5aSPyun YongHyeon  * 2. Redistributions in binary form must reproduce the above copyright
19972fbe5aSPyun YongHyeon  *    notice, this list of conditions and the following disclaimer in the
20972fbe5aSPyun YongHyeon  *    documentation and/or other materials provided with the distribution.
21972fbe5aSPyun YongHyeon  *
22972fbe5aSPyun YongHyeon  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23972fbe5aSPyun YongHyeon  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24972fbe5aSPyun YongHyeon  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25972fbe5aSPyun YongHyeon  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26972fbe5aSPyun YongHyeon  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27972fbe5aSPyun YongHyeon  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28972fbe5aSPyun YongHyeon  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29972fbe5aSPyun YongHyeon  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30972fbe5aSPyun YongHyeon  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31972fbe5aSPyun YongHyeon  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32972fbe5aSPyun YongHyeon  * POSSIBILITY OF SUCH DAMAGE.
33972fbe5aSPyun YongHyeon  */
34972fbe5aSPyun YongHyeon 
35972fbe5aSPyun YongHyeon /*
36972fbe5aSPyun YongHyeon  * Copyright (c) 1997 Manuel Bouyer.  All rights reserved.
37972fbe5aSPyun YongHyeon  *
38972fbe5aSPyun YongHyeon  * Redistribution and use in source and binary forms, with or without
39972fbe5aSPyun YongHyeon  * modification, are permitted provided that the following conditions
40972fbe5aSPyun YongHyeon  * are met:
41972fbe5aSPyun YongHyeon  * 1. Redistributions of source code must retain the above copyright
42972fbe5aSPyun YongHyeon  *    notice, this list of conditions and the following disclaimer.
43972fbe5aSPyun YongHyeon  * 2. Redistributions in binary form must reproduce the above copyright
44972fbe5aSPyun YongHyeon  *    notice, this list of conditions and the following disclaimer in the
45972fbe5aSPyun YongHyeon  *    documentation and/or other materials provided with the distribution.
46972fbe5aSPyun YongHyeon  *
47972fbe5aSPyun YongHyeon  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
48972fbe5aSPyun YongHyeon  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
49972fbe5aSPyun YongHyeon  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
50972fbe5aSPyun YongHyeon  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
51972fbe5aSPyun YongHyeon  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
52972fbe5aSPyun YongHyeon  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
53972fbe5aSPyun YongHyeon  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
54972fbe5aSPyun YongHyeon  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
55972fbe5aSPyun YongHyeon  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
56972fbe5aSPyun YongHyeon  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
57972fbe5aSPyun YongHyeon  */
58972fbe5aSPyun YongHyeon 
59972fbe5aSPyun YongHyeon /*
60972fbe5aSPyun YongHyeon  * Driver for generic ten-bit (1000BASE-SX) interfaces, built into
61972fbe5aSPyun YongHyeon  * many Gigabit Ethernet chips.
62972fbe5aSPyun YongHyeon  *
63972fbe5aSPyun YongHyeon  * All we have to do here is correctly report speed and duplex.
64972fbe5aSPyun YongHyeon  */
65972fbe5aSPyun YongHyeon 
66972fbe5aSPyun YongHyeon #include <sys/cdefs.h>
67972fbe5aSPyun YongHyeon /*
68972fbe5aSPyun YongHyeon  * Driver for generic unknown ten-bit interfaces(1000BASE-{LX,SX}
69972fbe5aSPyun YongHyeon  * fiber interfaces).
70972fbe5aSPyun YongHyeon  */
71972fbe5aSPyun YongHyeon 
72972fbe5aSPyun YongHyeon #include <sys/param.h>
73972fbe5aSPyun YongHyeon #include <sys/systm.h>
74972fbe5aSPyun YongHyeon #include <sys/kernel.h>
75972fbe5aSPyun YongHyeon #include <sys/module.h>
76972fbe5aSPyun YongHyeon #include <sys/socket.h>
77972fbe5aSPyun YongHyeon #include <sys/errno.h>
78972fbe5aSPyun YongHyeon #include <sys/bus.h>
79972fbe5aSPyun YongHyeon 
80972fbe5aSPyun YongHyeon #include <net/if.h>
81972fbe5aSPyun YongHyeon #include <net/if_media.h>
82972fbe5aSPyun YongHyeon 
83972fbe5aSPyun YongHyeon #include <dev/mii/mii.h>
84972fbe5aSPyun YongHyeon #include <dev/mii/miivar.h>
85972fbe5aSPyun YongHyeon #include "miidevs.h"
86972fbe5aSPyun YongHyeon 
87972fbe5aSPyun YongHyeon #include "miibus_if.h"
88972fbe5aSPyun YongHyeon 
89972fbe5aSPyun YongHyeon static int	gentbi_probe(device_t);
90972fbe5aSPyun YongHyeon static int	gentbi_attach(device_t);
91972fbe5aSPyun YongHyeon 
92972fbe5aSPyun YongHyeon static device_method_t gentbi_methods[] = {
93972fbe5aSPyun YongHyeon 	/* device interface */
94972fbe5aSPyun YongHyeon 	DEVMETHOD(device_probe,		gentbi_probe),
95972fbe5aSPyun YongHyeon 	DEVMETHOD(device_attach,	gentbi_attach),
96972fbe5aSPyun YongHyeon 	DEVMETHOD(device_detach,	mii_phy_detach),
97972fbe5aSPyun YongHyeon 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
98604f5f1fSMarius Strobl 	DEVMETHOD_END
99972fbe5aSPyun YongHyeon };
100972fbe5aSPyun YongHyeon 
101972fbe5aSPyun YongHyeon static driver_t gentbi_driver = {
102972fbe5aSPyun YongHyeon 	"gentbi",
103972fbe5aSPyun YongHyeon 	gentbi_methods,
104972fbe5aSPyun YongHyeon 	sizeof(struct mii_softc)
105972fbe5aSPyun YongHyeon };
106972fbe5aSPyun YongHyeon 
107f438c2ffSJohn Baldwin DRIVER_MODULE(gentbi, miibus, gentbi_driver, 0, 0);
108972fbe5aSPyun YongHyeon 
109972fbe5aSPyun YongHyeon static int	gentbi_service(struct mii_softc *, struct mii_data *, int);
110972fbe5aSPyun YongHyeon static void	gentbi_status(struct mii_softc *);
111972fbe5aSPyun YongHyeon 
1123fcb7a53SMarius Strobl static const struct mii_phy_funcs gentbi_funcs = {
1133fcb7a53SMarius Strobl 	gentbi_service,
1143fcb7a53SMarius Strobl 	gentbi_status,
1153fcb7a53SMarius Strobl 	mii_phy_reset
1163fcb7a53SMarius Strobl };
1173fcb7a53SMarius Strobl 
118972fbe5aSPyun YongHyeon static int
gentbi_probe(device_t dev)119972fbe5aSPyun YongHyeon gentbi_probe(device_t dev)
120972fbe5aSPyun YongHyeon {
121972fbe5aSPyun YongHyeon 	device_t parent;
122972fbe5aSPyun YongHyeon 	struct mii_attach_args *ma;
123972fbe5aSPyun YongHyeon 	int bmsr, extsr;
124972fbe5aSPyun YongHyeon 
125972fbe5aSPyun YongHyeon 	parent = device_get_parent(dev);
126972fbe5aSPyun YongHyeon 	ma = device_get_ivars(dev);
127972fbe5aSPyun YongHyeon 
128972fbe5aSPyun YongHyeon 	/*
129972fbe5aSPyun YongHyeon 	 * We match as a generic TBI if:
130972fbe5aSPyun YongHyeon 	 *
131972fbe5aSPyun YongHyeon 	 *	- There is no media in the BMSR.
132972fbe5aSPyun YongHyeon 	 *	- EXTSR has only 1000X.
133972fbe5aSPyun YongHyeon 	 */
134972fbe5aSPyun YongHyeon 	bmsr = MIIBUS_READREG(parent, ma->mii_phyno, MII_BMSR);
135972fbe5aSPyun YongHyeon 	if ((bmsr & BMSR_EXTSTAT) == 0 || (bmsr & BMSR_MEDIAMASK) != 0)
136972fbe5aSPyun YongHyeon 		return (ENXIO);
137972fbe5aSPyun YongHyeon 
138972fbe5aSPyun YongHyeon 	extsr = MIIBUS_READREG(parent, ma->mii_phyno, MII_EXTSR);
139972fbe5aSPyun YongHyeon 	if (extsr & (EXTSR_1000TFDX|EXTSR_1000THDX))
140972fbe5aSPyun YongHyeon 		return (ENXIO);
141972fbe5aSPyun YongHyeon 
142972fbe5aSPyun YongHyeon 	if (extsr & (EXTSR_1000XFDX|EXTSR_1000XHDX)) {
143972fbe5aSPyun YongHyeon 		/*
144972fbe5aSPyun YongHyeon 		 * We think this is a generic TBI.  Return a match
145972fbe5aSPyun YongHyeon 		 * priority higher than ukphy, but lower than what
146972fbe5aSPyun YongHyeon 		 * specific drivers will return.
147972fbe5aSPyun YongHyeon 		 */
148972fbe5aSPyun YongHyeon 		device_set_desc(dev, "Generic ten-bit interface");
149972fbe5aSPyun YongHyeon 		return (BUS_PROBE_LOW_PRIORITY);
150972fbe5aSPyun YongHyeon 	}
151972fbe5aSPyun YongHyeon 
152972fbe5aSPyun YongHyeon 	return (ENXIO);
153972fbe5aSPyun YongHyeon }
154972fbe5aSPyun YongHyeon 
155972fbe5aSPyun YongHyeon static int
gentbi_attach(device_t dev)156972fbe5aSPyun YongHyeon gentbi_attach(device_t dev)
157972fbe5aSPyun YongHyeon {
158972fbe5aSPyun YongHyeon 	struct mii_softc *sc;
159972fbe5aSPyun YongHyeon 
160972fbe5aSPyun YongHyeon 	sc = device_get_softc(dev);
161972fbe5aSPyun YongHyeon 
1623fcb7a53SMarius Strobl 	mii_phy_dev_attach(dev, MIIF_NOMANPAUSE, &gentbi_funcs, 0);
163972fbe5aSPyun YongHyeon 
1643fcb7a53SMarius Strobl 	PHY_RESET(sc);
165972fbe5aSPyun YongHyeon 
166972fbe5aSPyun YongHyeon 	/*
167972fbe5aSPyun YongHyeon 	 * Mask out all media in the BMSR.  We only are really interested
168972fbe5aSPyun YongHyeon 	 * in "auto".
169972fbe5aSPyun YongHyeon 	 */
170972fbe5aSPyun YongHyeon 	sc->mii_capabilities =
1713fcb7a53SMarius Strobl 	    PHY_READ(sc, MII_BMSR) & sc->mii_capmask & ~BMSR_MEDIAMASK;
172972fbe5aSPyun YongHyeon 	if (sc->mii_capabilities & BMSR_EXTSTAT)
173972fbe5aSPyun YongHyeon 		sc->mii_extcapabilities = PHY_READ(sc, MII_EXTSR);
174972fbe5aSPyun YongHyeon 
175972fbe5aSPyun YongHyeon 	device_printf(dev, " ");
176972fbe5aSPyun YongHyeon 	mii_phy_add_media(sc);
177972fbe5aSPyun YongHyeon 	printf("\n");
178972fbe5aSPyun YongHyeon 
179e30d3a0cSDoug White 	MIIBUS_MEDIAINIT(sc->mii_dev);
180972fbe5aSPyun YongHyeon 	return (0);
181972fbe5aSPyun YongHyeon }
182972fbe5aSPyun YongHyeon 
183972fbe5aSPyun YongHyeon static int
gentbi_service(struct mii_softc * sc,struct mii_data * mii,int cmd)184972fbe5aSPyun YongHyeon gentbi_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
185972fbe5aSPyun YongHyeon {
186972fbe5aSPyun YongHyeon 
187972fbe5aSPyun YongHyeon 	switch (cmd) {
188972fbe5aSPyun YongHyeon 	case MII_POLLSTAT:
189972fbe5aSPyun YongHyeon 		break;
190972fbe5aSPyun YongHyeon 
191972fbe5aSPyun YongHyeon 	case MII_MEDIACHG:
192972fbe5aSPyun YongHyeon 		mii_phy_setmedia(sc);
193972fbe5aSPyun YongHyeon 		break;
194972fbe5aSPyun YongHyeon 
195972fbe5aSPyun YongHyeon 	case MII_TICK:
196972fbe5aSPyun YongHyeon 		if (mii_phy_tick(sc) == EJUSTRETURN)
197972fbe5aSPyun YongHyeon 			return (0);
198972fbe5aSPyun YongHyeon 		break;
199972fbe5aSPyun YongHyeon 	}
200972fbe5aSPyun YongHyeon 
201972fbe5aSPyun YongHyeon 	/* Update the media status. */
2023fcb7a53SMarius Strobl 	PHY_STATUS(sc);
203972fbe5aSPyun YongHyeon 
204972fbe5aSPyun YongHyeon 	/* Callback if something changed. */
205972fbe5aSPyun YongHyeon 	mii_phy_update(sc, cmd);
206972fbe5aSPyun YongHyeon 	return (0);
207972fbe5aSPyun YongHyeon }
208972fbe5aSPyun YongHyeon 
209972fbe5aSPyun YongHyeon static void
gentbi_status(struct mii_softc * sc)210972fbe5aSPyun YongHyeon gentbi_status(struct mii_softc *sc)
211972fbe5aSPyun YongHyeon {
212972fbe5aSPyun YongHyeon 	struct mii_data *mii = sc->mii_pdata;
213972fbe5aSPyun YongHyeon 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
214972fbe5aSPyun YongHyeon 	int bmsr, bmcr, anlpar;
215972fbe5aSPyun YongHyeon 
216972fbe5aSPyun YongHyeon 	mii->mii_media_status = IFM_AVALID;
217972fbe5aSPyun YongHyeon 	mii->mii_media_active = IFM_ETHER;
218972fbe5aSPyun YongHyeon 
219972fbe5aSPyun YongHyeon 	bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
220972fbe5aSPyun YongHyeon 
221972fbe5aSPyun YongHyeon 	if (bmsr & BMSR_LINK)
222972fbe5aSPyun YongHyeon 		mii->mii_media_status |= IFM_ACTIVE;
223972fbe5aSPyun YongHyeon 
224972fbe5aSPyun YongHyeon 	bmcr = PHY_READ(sc, MII_BMCR);
225972fbe5aSPyun YongHyeon 	if (bmcr & BMCR_ISO) {
226972fbe5aSPyun YongHyeon 		mii->mii_media_active |= IFM_NONE;
227972fbe5aSPyun YongHyeon 		mii->mii_media_status = 0;
228972fbe5aSPyun YongHyeon 		return;
229972fbe5aSPyun YongHyeon 	}
230972fbe5aSPyun YongHyeon 
231972fbe5aSPyun YongHyeon 	if (bmcr & BMCR_LOOP)
232972fbe5aSPyun YongHyeon 		mii->mii_media_active |= IFM_LOOP;
233972fbe5aSPyun YongHyeon 
234972fbe5aSPyun YongHyeon 	if (bmcr & BMCR_AUTOEN) {
235972fbe5aSPyun YongHyeon 		/*
2362a6a6e5cSMarius Strobl 		 * The media status bits are only valid if autonegotiation
237972fbe5aSPyun YongHyeon 		 * has completed (or it's disabled).
238972fbe5aSPyun YongHyeon 		 */
239972fbe5aSPyun YongHyeon 		if ((bmsr & BMSR_ACOMP) == 0) {
240972fbe5aSPyun YongHyeon 			/* Erg, still trying, I guess... */
241972fbe5aSPyun YongHyeon 			mii->mii_media_active |= IFM_NONE;
242972fbe5aSPyun YongHyeon 			return;
243972fbe5aSPyun YongHyeon 		}
244972fbe5aSPyun YongHyeon 
245972fbe5aSPyun YongHyeon 		/*
246972fbe5aSPyun YongHyeon 		 * The media is always 1000baseSX.  Check the ANLPAR to
247972fbe5aSPyun YongHyeon 		 * see if we're doing full-duplex.
248972fbe5aSPyun YongHyeon 		 */
249972fbe5aSPyun YongHyeon 		mii->mii_media_active |= IFM_1000_SX;
250972fbe5aSPyun YongHyeon 		anlpar = PHY_READ(sc, MII_ANLPAR);
251972fbe5aSPyun YongHyeon 		if ((sc->mii_extcapabilities & EXTSR_1000XFDX) != 0 &&
252972fbe5aSPyun YongHyeon 		    (anlpar & ANLPAR_X_FD) != 0)
253991ab941SMarius Strobl 			mii->mii_media_active |=
254991ab941SMarius Strobl 			    IFM_FDX | mii_phy_flowstatus(sc);
255d7a9ad56SMarius Strobl 		else
256d7a9ad56SMarius Strobl 			mii->mii_media_active |= IFM_HDX;
257972fbe5aSPyun YongHyeon 	} else
258972fbe5aSPyun YongHyeon 		mii->mii_media_active = ife->ifm_media;
259972fbe5aSPyun YongHyeon }
260