xref: /freebsd/sys/dev/mii/brgphy.c (revision 77a0943ded95b9e6438f7db70c4a28e4d93946d4)
1 /*
2  * Copyright (c) 2000
3  *	Bill Paul <wpaul@ee.columbia.edu>.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by Bill Paul.
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30  * THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * $FreeBSD$
33  */
34 
35 /*
36  * Driver for the Broadcom BCR5400 1000baseTX PHY. Speed is always
37  * 1000mbps; all we need to negotiate here is full or half duplex.
38  */
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/malloc.h>
44 #include <sys/socket.h>
45 #include <sys/bus.h>
46 
47 
48 #include <net/if.h>
49 #include <net/if_media.h>
50 
51 #include <dev/mii/mii.h>
52 #include <dev/mii/miivar.h>
53 #include <dev/mii/miidevs.h>
54 
55 #include <dev/mii/brgphyreg.h>
56 
57 #include "miibus_if.h"
58 
59 #if !defined(lint)
60 static const char rcsid[] =
61   "$FreeBSD$";
62 #endif
63 
64 static int brgphy_probe		__P((device_t));
65 static int brgphy_attach		__P((device_t));
66 static int brgphy_detach		__P((device_t));
67 
68 static device_method_t brgphy_methods[] = {
69 	/* device interface */
70 	DEVMETHOD(device_probe,		brgphy_probe),
71 	DEVMETHOD(device_attach,	brgphy_attach),
72 	DEVMETHOD(device_detach,	brgphy_detach),
73 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
74 	{ 0, 0 }
75 };
76 
77 static devclass_t brgphy_devclass;
78 
79 static driver_t brgphy_driver = {
80 	"brgphy",
81 	brgphy_methods,
82 	sizeof(struct mii_softc)
83 };
84 
85 DRIVER_MODULE(brgphy, miibus, brgphy_driver, brgphy_devclass, 0, 0);
86 
87 int	brgphy_service __P((struct mii_softc *, struct mii_data *, int));
88 void	brgphy_status __P((struct mii_softc *));
89 
90 static int	brgphy_mii_phy_auto __P((struct mii_softc *, int));
91 extern void	mii_phy_auto_timeout __P((void *));
92 
93 static int brgphy_probe(dev)
94 	device_t		dev;
95 {
96 	struct mii_attach_args *ma;
97 
98 	ma = device_get_ivars(dev);
99 
100 	if (MII_OUI(ma->mii_id1, ma->mii_id2) != MII_OUI_xxBROADCOM ||
101 	    MII_MODEL(ma->mii_id2) != MII_MODEL_xxBROADCOM_BCM5400)
102 		return(ENXIO);
103 
104 	device_set_desc(dev, MII_STR_xxBROADCOM_BCM5400);
105 
106 	return(0);
107 }
108 
109 static int brgphy_attach(dev)
110 	device_t		dev;
111 {
112 	struct mii_softc *sc;
113 	struct mii_attach_args *ma;
114 	struct mii_data *mii;
115 	const char *sep = "";
116 
117 	sc = device_get_softc(dev);
118 	ma = device_get_ivars(dev);
119 	sc->mii_dev = device_get_parent(dev);
120 	mii = device_get_softc(sc->mii_dev);
121 	LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
122 
123 	sc->mii_inst = mii->mii_instance;
124 	sc->mii_phy = ma->mii_phyno;
125 	sc->mii_service = brgphy_service;
126 	sc->mii_pdata = mii;
127 
128 	sc->mii_flags |= MIIF_NOISOLATE;
129 	mii->mii_instance++;
130 
131 #define	ADD(m, c)	ifmedia_add(&mii->mii_media, (m), (c), NULL)
132 #define PRINT(s)	printf("%s%s", sep, s); sep = ", "
133 
134 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
135 	    BMCR_ISO);
136 #if 0
137 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->mii_inst),
138 	    BMCR_LOOP|BMCR_S100);
139 #endif
140 
141 	mii_phy_reset(sc);
142 
143 	device_printf(dev, " ");
144 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_TX, 0, sc->mii_inst),
145 	    BRGPHY_BMCR_FDX);
146 	PRINT("1000baseTX");
147 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_TX, IFM_FDX, sc->mii_inst), 0);
148 	PRINT("1000baseTX-FDX");
149 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst), 0);
150 	PRINT("auto");
151 
152 	printf("\n");
153 #undef ADD
154 #undef PRINT
155 
156 	MIIBUS_MEDIAINIT(sc->mii_dev);
157 	return(0);
158 }
159 
160 static int brgphy_detach(dev)
161 	device_t		dev;
162 {
163 	struct mii_softc *sc;
164 	struct mii_data *mii;
165 
166 	sc = device_get_softc(dev);
167 	mii = device_get_softc(device_get_parent(dev));
168 	sc->mii_dev = NULL;
169 	LIST_REMOVE(sc, mii_list);
170 
171 	return(0);
172 }
173 int
174 brgphy_service(sc, mii, cmd)
175 	struct mii_softc *sc;
176 	struct mii_data *mii;
177 	int cmd;
178 {
179 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
180 	int reg;
181 
182 	switch (cmd) {
183 	case MII_POLLSTAT:
184 		/*
185 		 * If we're not polling our PHY instance, just return.
186 		 */
187 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
188 			return (0);
189 		break;
190 
191 	case MII_MEDIACHG:
192 		/*
193 		 * If the media indicates a different PHY instance,
194 		 * isolate ourselves.
195 		 */
196 		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
197 			reg = PHY_READ(sc, MII_BMCR);
198 			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
199 			return (0);
200 		}
201 
202 		/*
203 		 * If the interface is not up, don't do anything.
204 		 */
205 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
206 			break;
207 
208 		PHY_WRITE(sc, BRGPHY_MII_PHY_EXTCTL,
209 		    BRGPHY_PHY_EXTCTL_HIGH_LA|BRGPHY_PHY_EXTCTL_EN_LTR);
210 		PHY_WRITE(sc, BRGPHY_MII_AUXCTL,
211 		    BRGPHY_AUXCTL_LONG_PKT|BRGPHY_AUXCTL_TX_TST);
212 		PHY_WRITE(sc, BRGPHY_MII_IMR, 0xFF00);
213 
214 		switch (IFM_SUBTYPE(ife->ifm_media)) {
215 		case IFM_AUTO:
216 #ifdef foo
217 			/*
218 			 * If we're already in auto mode, just return.
219 			 */
220 			if (PHY_READ(sc, BRGPHY_MII_BMCR) & BRGPHY_BMCR_AUTOEN)
221 				return (0);
222 #endif
223 			(void) brgphy_mii_phy_auto(sc, 1);
224 			break;
225 		case IFM_1000_TX:
226 			if ((ife->ifm_media & IFM_GMASK) == IFM_FDX) {
227 				PHY_WRITE(sc, BRGPHY_MII_BMCR,
228 				    BRGPHY_BMCR_FDX|BRGPHY_BMCR_SPD1);
229 			} else {
230 				PHY_WRITE(sc, BRGPHY_MII_BMCR,
231 				    BRGPHY_BMCR_SPD1);
232 			}
233 			PHY_WRITE(sc, BRGPHY_MII_ANAR, BRGPHY_SEL_TYPE);
234 
235 			/*
236 			 * When settning the link manually, one side must
237 			 * be the master and the other the slave. However
238 			 * ifmedia doesn't give us a good way to specify
239 			 * this, so we fake it by using one of the LINK
240 			 * flags. If LINK0 is set, we program the PHY to
241 			 * be a master, otherwise it's a slave.
242 			 */
243 			if ((mii->mii_ifp->if_flags & IFF_LINK0)) {
244 				PHY_WRITE(sc, BRGPHY_MII_1000CTL,
245 				    BRGPHY_1000CTL_MSE|BRGPHY_1000CTL_MSC);
246 			} else {
247 				PHY_WRITE(sc, BRGPHY_MII_1000CTL,
248 				    BRGPHY_1000CTL_MSE);
249 			}
250 			break;
251 		case IFM_100_T4:
252 		case IFM_100_TX:
253 		case IFM_10_T:
254 		default:
255 			return (EINVAL);
256 		}
257 		break;
258 
259 	case MII_TICK:
260 		/*
261 		 * If we're not currently selected, just return.
262 		 */
263 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
264 			return (0);
265 
266 		/*
267 		 * Only used for autonegotiation.
268 		 */
269 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
270 			return (0);
271 
272 		/*
273 		 * Is the interface even up?
274 		 */
275 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
276 			return (0);
277 
278 		/*
279 		 * Only retry autonegotiation every 5 seconds.
280 		 */
281 		if (++sc->mii_ticks != 5)
282 			return (0);
283 
284 		sc->mii_ticks = 0;
285 
286 		/*
287 		 * Check to see if we have link.  If we do, we don't
288 		 * need to restart the autonegotiation process.  Read
289 		 * the BMSR twice in case it's latched.
290 		 */
291 		reg = PHY_READ(sc, BRGPHY_MII_AUXSTS);
292 		if (reg & BRGPHY_AUXSTS_LINK)
293 			break;
294 
295 		mii_phy_reset(sc);
296 		if (brgphy_mii_phy_auto(sc, 0) == EJUSTRETURN)
297 			return(0);
298 		break;
299 	}
300 
301 	/* Update the media status. */
302 	brgphy_status(sc);
303 
304 	/* Callback if something changed. */
305 	if (sc->mii_active != mii->mii_media_active || cmd == MII_MEDIACHG) {
306 		MIIBUS_STATCHG(sc->mii_dev);
307 		sc->mii_active = mii->mii_media_active;
308 	}
309 	return (0);
310 }
311 
312 void
313 brgphy_status(sc)
314 	struct mii_softc *sc;
315 {
316 	struct mii_data *mii = sc->mii_pdata;
317 	int bmsr, bmcr, anlpar;
318 
319 	mii->mii_media_status = IFM_AVALID;
320 	mii->mii_media_active = IFM_ETHER;
321 
322 	bmsr = PHY_READ(sc, BRGPHY_MII_BMSR);
323 	if (PHY_READ(sc, BRGPHY_MII_AUXSTS) & BRGPHY_AUXSTS_LINK)
324 		mii->mii_media_status |= IFM_ACTIVE;
325 
326 	bmcr = PHY_READ(sc, BRGPHY_MII_BMCR);
327 
328 	if (bmcr & BRGPHY_BMCR_LOOP)
329 		mii->mii_media_active |= IFM_LOOP;
330 
331 	if (bmcr & BRGPHY_BMCR_AUTOEN) {
332 		if ((bmsr & BRGPHY_BMSR_ACOMP) == 0) {
333 			/* Erg, still trying, I guess... */
334 			mii->mii_media_active |= IFM_NONE;
335 			return;
336 		}
337 
338 		mii->mii_media_active |= IFM_1000_TX;
339 		anlpar = PHY_READ(sc, BRGPHY_MII_AUXSTS);
340 		if ((anlpar & BRGPHY_AUXSTS_AN_RES) == BRGPHY_RES_1000FD)
341 			mii->mii_media_active |= IFM_FDX;
342 		if ((anlpar & BRGPHY_AUXSTS_AN_RES) == BRGPHY_RES_1000HD)
343 			mii->mii_media_active |= IFM_HDX;
344 		return;
345 	}
346 
347 	mii->mii_media_active |= IFM_1000_TX;
348 	if (bmcr & BRGPHY_BMCR_FDX)
349 		mii->mii_media_active |= IFM_FDX;
350 	else
351 		mii->mii_media_active |= IFM_HDX;
352 
353 	return;
354 }
355 
356 
357 static int
358 brgphy_mii_phy_auto(mii, waitfor)
359 	struct mii_softc *mii;
360 	int waitfor;
361 {
362 	int bmsr, i;
363 
364 	if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) {
365 		PHY_WRITE(mii, BRGPHY_MII_1000CTL,
366 		    BRGPHY_1000CTL_AFD|BRGPHY_1000CTL_AHD);
367 		PHY_WRITE(mii, BRGPHY_MII_ANAR, BRGPHY_SEL_TYPE);
368 		PHY_WRITE(mii, BRGPHY_MII_BMCR,
369 		    BRGPHY_BMCR_AUTOEN | BRGPHY_BMCR_STARTNEG);
370 		PHY_WRITE(mii, BRGPHY_MII_IMR, 0xFF00);
371 	}
372 
373 	if (waitfor) {
374 		/* Wait 500ms for it to complete. */
375 		for (i = 0; i < 500; i++) {
376 			if ((bmsr = PHY_READ(mii, BRGPHY_MII_BMSR)) &
377 			    BRGPHY_BMSR_ACOMP)
378 				return (0);
379 			DELAY(1000);
380 #if 0
381 		if ((bmsr & BMSR_ACOMP) == 0)
382 			printf("%s: autonegotiation failed to complete\n",
383 			    mii->mii_dev.dv_xname);
384 #endif
385 		}
386 
387 		/*
388 		 * Don't need to worry about clearing MIIF_DOINGAUTO.
389 		 * If that's set, a timeout is pending, and it will
390 		 * clear the flag.
391 		 */
392 		return (EIO);
393 	}
394 
395 	/*
396 	 * Just let it finish asynchronously.  This is for the benefit of
397 	 * the tick handler driving autonegotiation.  Don't want 500ms
398 	 * delays all the time while the system is running!
399 	 */
400 	if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) {
401 		mii->mii_flags |= MIIF_DOINGAUTO;
402 		timeout(mii_phy_auto_timeout, mii, hz >> 1);
403 	}
404 	return (EJUSTRETURN);
405 }
406