xref: /freebsd/sys/dev/mii/bmtphy.c (revision 7660b554bc59a07be0431c17e0e33815818baa69)
1 /*
2  * Copyright (c) 1998, 1999, 2000, 2001 The NetBSD Foundation, Inc.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to The NetBSD Foundation
6  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
7  * NASA Ames Research Center.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *      This product includes software developed by the NetBSD
20  *      Foundation, Inc. and its contributors.
21  * 4. Neither the name of The NetBSD Foundation nor the names of its
22  *    contributors may be used to endorse or promote products derived
23  *    from this software without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40 
41 /*
42  * Copyright (c) 1997 Manuel Bouyer.  All rights reserved.
43  *
44  * Redistribution and use in source and binary forms, with or without
45  * modification, are permitted provided that the following conditions
46  * are met:
47  * 1. Redistributions of source code must retain the above copyright
48  *    notice, this list of conditions and the following disclaimer.
49  * 2. Redistributions in binary form must reproduce the above copyright
50  *    notice, this list of conditions and the following disclaimer in the
51  *    documentation and/or other materials provided with the distribution.
52  * 3. All advertising materials mentioning features or use of this software
53  *    must display the following acknowledgement:
54  *      This product includes software developed by Manuel Bouyer.
55  * 4. The name of the author may not be used to endorse or promote products
56  *    derived from this software without specific prior written permission.
57  *
58  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
59  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
60  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
61  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
62  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
63  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
64  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
65  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
66  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
67  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
68  *
69  *	from: NetBSD: bmtphy.c,v 1.8 2002/07/03 06:25:50 simonb Exp
70  *
71  */
72 
73 /*
74  * Driver for the Broadcom BCM5201/BCM5202 "Mini-Theta" PHYs.  This also
75  * drives the PHY on the 3Com 3c905C.  The 3c905C's PHY is described in
76  * the 3c905C data sheet.
77  */
78 
79 #include <sys/param.h>
80 #include <sys/systm.h>
81 #include <sys/kernel.h>
82 #include <sys/socket.h>
83 #include <sys/bus.h>
84 
85 #include <net/if.h>
86 #include <net/if_media.h>
87 
88 #include <dev/mii/mii.h>
89 #include <dev/mii/miivar.h>
90 #include "miidevs.h"
91 
92 #include <dev/mii/bmtphyreg.h>
93 
94 #include "miibus_if.h"
95 
96 static int	bmtphy_probe(device_t);
97 static int	bmtphy_attach(device_t);
98 
99 static device_method_t bmtphy_methods[] = {
100 	/* Device interface */
101 	DEVMETHOD(device_probe,		bmtphy_probe),
102 	DEVMETHOD(device_attach,	bmtphy_attach),
103 	DEVMETHOD(device_detach,	mii_phy_detach),
104 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
105 
106 	{ 0, 0 }
107 };
108 
109 static devclass_t	bmtphy_devclass;
110 
111 static driver_t	bmtphy_driver = {
112 	"bmtphy",
113 	bmtphy_methods,
114 	sizeof(struct mii_softc)
115 };
116 
117 DRIVER_MODULE(bmtphy, miibus, bmtphy_driver, bmtphy_devclass, 0, 0);
118 
119 static int	bmtphy_service(struct mii_softc *, struct mii_data *, int);
120 static void	bmtphy_status(struct mii_softc *);
121 
122 static int
123 bmtphy_probe(device_t dev)
124 {
125 	struct	mii_attach_args *ma;
126 	int	rval;
127 
128 	ma = device_get_ivars(dev);
129 	rval = 0;
130 
131 	if (MII_OUI(ma->mii_id1, ma->mii_id2) != MII_OUI_BROADCOM)
132 		return (ENXIO);
133 
134 	switch (MII_MODEL(ma->mii_id2)) {
135 	case MII_MODEL_BROADCOM_3C905B:
136 		device_set_desc(dev, MII_STR_BROADCOM_3C905B);
137 		rval = -10;	/* Let exphy take precedence. */
138 		break;
139 	case MII_MODEL_BROADCOM_3C905C:
140 		device_set_desc(dev, MII_STR_BROADCOM_3C905C);
141 		rval = -10;	/* Let exphy take precedence. */
142 		break;
143 	case MII_MODEL_BROADCOM_BCM5201:
144 		device_set_desc(dev, MII_STR_BROADCOM_BCM5201);
145 		break;
146 	case MII_MODEL_BROADCOM_BCM5221:
147 		device_set_desc(dev, MII_STR_BROADCOM_BCM5221);
148 		break;
149 	case MII_MODEL_BROADCOM_BCM4401:
150 		device_set_desc(dev, MII_STR_BROADCOM_BCM4401);
151 		break;
152 	default:
153 		return (ENXIO);
154 	}
155 
156 	return (rval);
157 }
158 
159 static int
160 bmtphy_attach(device_t dev)
161 {
162 	struct	mii_softc *sc;
163 	struct	mii_attach_args *ma;
164 	struct	mii_data *mii;
165 
166 	sc = device_get_softc(dev);
167 	ma = device_get_ivars(dev);
168 	sc->mii_dev = device_get_parent(dev);
169 	mii = device_get_softc(sc->mii_dev);
170 	LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
171 
172 	sc->mii_inst = mii->mii_instance;
173 	sc->mii_phy = ma->mii_phyno;
174 	sc->mii_service = bmtphy_service;
175 	sc->mii_pdata = mii;
176 
177 	mii_phy_reset(sc);
178 
179 	mii->mii_instance++;
180 
181 	sc->mii_capabilities =
182 	    PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
183 	device_printf(dev, " ");
184 	if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0)
185 		printf("no media present");
186 	else
187 		mii_phy_add_media(sc);
188 
189 	printf("\n");
190 
191 	MIIBUS_MEDIAINIT(sc->mii_dev);
192 
193 	return (0);
194 }
195 
196 static int
197 bmtphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
198 {
199 	struct	ifmedia_entry *ife;
200 	int	reg;
201 
202 	ife = mii->mii_media.ifm_cur;
203 
204 	switch (cmd) {
205 	case MII_POLLSTAT:
206 		/*
207 		 * If we're not polling our PHY instance, just return.
208 		 */
209 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
210 			return (0);
211 		break;
212 
213 	case MII_MEDIACHG:
214 		/*
215 		 * If the media indicates a different PHY instance,
216 		 * isolate ourselves.
217 		 */
218 		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
219 			reg = PHY_READ(sc, MII_BMCR);
220 			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
221 			return (0);
222 		}
223 
224 		/*
225 		 * If the interface is not up, don't do anything.
226 		 */
227 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
228 			break;
229 
230 		mii_phy_setmedia(sc);
231 		break;
232 
233 	case MII_TICK:
234 		/*
235 		 * If we're not currently selected, just return.
236 		 */
237 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
238 			return (0);
239 		if (mii_phy_tick(sc) == EJUSTRETURN)
240 			return (0);
241 		break;
242 	}
243 
244 	/* Update the media status. */
245 	bmtphy_status(sc);
246 
247 	/* Callback if something changed. */
248 	mii_phy_update(sc, cmd);
249 
250 	return (0);
251 }
252 
253 static void
254 bmtphy_status(struct mii_softc *sc)
255 {
256 	struct	mii_data *mii;
257 	struct	ifmedia_entry *ife;
258 	int	bmsr, bmcr, aux_csr;
259 
260 	mii = sc->mii_pdata;
261 	ife = mii->mii_media.ifm_cur;
262 
263 	mii->mii_media_status = IFM_AVALID;
264 	mii->mii_media_active = IFM_ETHER;
265 
266 	bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
267 	aux_csr = PHY_READ(sc, MII_BMTPHY_AUX_CSR);
268 
269 	if (bmsr & BMSR_LINK)
270 		mii->mii_media_status |= IFM_ACTIVE;
271 
272 	bmcr = PHY_READ(sc, MII_BMCR);
273 	if (bmcr & BMCR_ISO) {
274 		mii->mii_media_active |= IFM_NONE;
275 		mii->mii_media_status = 0;
276 		return;
277 	}
278 
279 	if (bmcr & BMCR_LOOP)
280 		mii->mii_media_active |= IFM_LOOP;
281 
282 	if (bmcr & BMCR_AUTOEN) {
283 		/*
284 		 * The media status bits are only valid if autonegotiation
285 		 * has completed (or it's disabled).
286 		 */
287 		if ((bmsr & BMSR_ACOMP) == 0) {
288 			/* Erg, still trying, I guess... */
289 			mii->mii_media_active |= IFM_NONE;
290 			return;
291 		}
292 
293 		if (aux_csr & AUX_CSR_SPEED)
294 			mii->mii_media_active |= IFM_100_TX;
295 		else
296 			mii->mii_media_active |= IFM_10_T;
297 		if (aux_csr & AUX_CSR_FDX)
298 			mii->mii_media_active |= IFM_FDX;
299 	} else
300 		mii->mii_media_active = ife->ifm_media;
301 }
302