xref: /freebsd/sys/dev/mii/brgphy.c (revision 4b2eaea43fec8e8792be611dea204071a10b655a)
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/socket.h>
44 #include <sys/bus.h>
45 
46 #include <machine/clock.h>
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 "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(device_t);
65 static int brgphy_attach(device_t);
66 
67 static device_method_t brgphy_methods[] = {
68 	/* device interface */
69 	DEVMETHOD(device_probe,		brgphy_probe),
70 	DEVMETHOD(device_attach,	brgphy_attach),
71 	DEVMETHOD(device_detach,	mii_phy_detach),
72 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
73 	{ 0, 0 }
74 };
75 
76 static devclass_t brgphy_devclass;
77 
78 static driver_t brgphy_driver = {
79 	"brgphy",
80 	brgphy_methods,
81 	sizeof(struct mii_softc)
82 };
83 
84 DRIVER_MODULE(brgphy, miibus, brgphy_driver, brgphy_devclass, 0, 0);
85 
86 static int	brgphy_service(struct mii_softc *, struct mii_data *, int);
87 static void	brgphy_status(struct mii_softc *);
88 static int	brgphy_mii_phy_auto(struct mii_softc *);
89 
90 static int
91 brgphy_probe(dev)
92 	device_t		dev;
93 {
94 	struct mii_attach_args *ma;
95 
96 	ma = device_get_ivars(dev);
97 
98 	if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_xxBROADCOM &&
99 	    MII_MODEL(ma->mii_id2) == MII_MODEL_xxBROADCOM_BCM5400) {
100 		device_set_desc(dev, MII_STR_xxBROADCOM_BCM5400);
101 		return(0);
102 	}
103 
104 	if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_xxBROADCOM &&
105 	    MII_MODEL(ma->mii_id2) == MII_MODEL_xxBROADCOM_BCM5401) {
106 		device_set_desc(dev, MII_STR_xxBROADCOM_BCM5401);
107 		return(0);
108 	}
109 
110 	if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_xxBROADCOM &&
111 	    MII_MODEL(ma->mii_id2) == MII_MODEL_xxBROADCOM_BCM5411) {
112 		device_set_desc(dev, MII_STR_xxBROADCOM_BCM5411);
113 		return(0);
114 	}
115 
116 	if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_xxBROADCOM &&
117 	    MII_MODEL(ma->mii_id2) == MII_MODEL_xxBROADCOM_BCM5701) {
118 		device_set_desc(dev, MII_STR_xxBROADCOM_BCM5701);
119 		return(0);
120 	}
121 
122 	if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_xxBROADCOM &&
123 	    MII_MODEL(ma->mii_id2) == MII_MODEL_xxBROADCOM_BCM5703) {
124 		device_set_desc(dev, MII_STR_xxBROADCOM_BCM5703);
125 		return(0);
126 	}
127 
128 	return(ENXIO);
129 }
130 
131 static int
132 brgphy_attach(dev)
133 	device_t		dev;
134 {
135 	struct mii_softc *sc;
136 	struct mii_attach_args *ma;
137 	struct mii_data *mii;
138 	const char *sep = "";
139 
140 	sc = device_get_softc(dev);
141 	ma = device_get_ivars(dev);
142 	sc->mii_dev = device_get_parent(dev);
143 	mii = device_get_softc(sc->mii_dev);
144 	LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
145 
146 	sc->mii_inst = mii->mii_instance;
147 	sc->mii_phy = ma->mii_phyno;
148 	sc->mii_service = brgphy_service;
149 	sc->mii_pdata = mii;
150 
151 	sc->mii_flags |= MIIF_NOISOLATE;
152 	mii->mii_instance++;
153 
154 #define	ADD(m, c)	ifmedia_add(&mii->mii_media, (m), (c), NULL)
155 #define PRINT(s)	printf("%s%s", sep, s); sep = ", "
156 
157 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
158 	    BMCR_ISO);
159 #if 0
160 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->mii_inst),
161 	    BMCR_LOOP|BMCR_S100);
162 #endif
163 
164 	mii_phy_reset(sc);
165 
166 
167 	sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
168 	sc->mii_capabilities &= ~BMSR_ANEG;
169 	device_printf(dev, " ");
170 	mii_add_media(sc);
171 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, 0, sc->mii_inst),
172 	    BRGPHY_BMCR_FDX);
173 	PRINT(", 1000baseTX");
174 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, IFM_FDX, sc->mii_inst), 0);
175 	PRINT("1000baseTX-FDX");
176 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst), 0);
177 	PRINT("auto");
178 
179 	printf("\n");
180 #undef ADD
181 #undef PRINT
182 
183 	MIIBUS_MEDIAINIT(sc->mii_dev);
184 	return(0);
185 }
186 
187 static int
188 brgphy_service(sc, mii, cmd)
189 	struct mii_softc *sc;
190 	struct mii_data *mii;
191 	int cmd;
192 {
193 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
194 	int reg, speed;
195 
196 	switch (cmd) {
197 	case MII_POLLSTAT:
198 		/*
199 		 * If we're not polling our PHY instance, just return.
200 		 */
201 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
202 			return (0);
203 		break;
204 
205 	case MII_MEDIACHG:
206 		/*
207 		 * If the media indicates a different PHY instance,
208 		 * isolate ourselves.
209 		 */
210 		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
211 			reg = PHY_READ(sc, MII_BMCR);
212 			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
213 			return (0);
214 		}
215 
216 		/*
217 		 * If the interface is not up, don't do anything.
218 		 */
219 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
220 			break;
221 
222 		PHY_WRITE(sc, BRGPHY_MII_PHY_EXTCTL,
223 		    BRGPHY_PHY_EXTCTL_HIGH_LA|BRGPHY_PHY_EXTCTL_EN_LTR);
224 		PHY_WRITE(sc, BRGPHY_MII_AUXCTL,
225 		    BRGPHY_AUXCTL_LONG_PKT|BRGPHY_AUXCTL_TX_TST);
226 		PHY_WRITE(sc, BRGPHY_MII_IMR, 0xFF00);
227 
228 		switch (IFM_SUBTYPE(ife->ifm_media)) {
229 		case IFM_AUTO:
230 #ifdef foo
231 			/*
232 			 * If we're already in auto mode, just return.
233 			 */
234 			if (PHY_READ(sc, BRGPHY_MII_BMCR) & BRGPHY_BMCR_AUTOEN)
235 				return (0);
236 #endif
237 			(void) brgphy_mii_phy_auto(sc);
238 			break;
239 		case IFM_1000_T:
240 			speed = BRGPHY_S1000;
241 			goto setit;
242 		case IFM_100_TX:
243 			speed = BRGPHY_S100;
244 			goto setit;
245 		case IFM_10_T:
246 			speed = BRGPHY_S10;
247 setit:
248 			if ((ife->ifm_media & IFM_GMASK) == IFM_FDX) {
249 				PHY_WRITE(sc, BRGPHY_MII_BMCR,
250 				    BRGPHY_BMCR_FDX|speed);
251 			} else {
252 				PHY_WRITE(sc, BRGPHY_MII_BMCR, speed);
253 			}
254 			PHY_WRITE(sc, BRGPHY_MII_ANAR, BRGPHY_SEL_TYPE);
255 
256 			if (IFM_SUBTYPE(ife->ifm_media) != IFM_1000_T)
257 				break;
258 
259 			/*
260 			 * When settning the link manually, one side must
261 			 * be the master and the other the slave. However
262 			 * ifmedia doesn't give us a good way to specify
263 			 * this, so we fake it by using one of the LINK
264 			 * flags. If LINK0 is set, we program the PHY to
265 			 * be a master, otherwise it's a slave.
266 			 */
267 			if ((mii->mii_ifp->if_flags & IFF_LINK0)) {
268 				PHY_WRITE(sc, BRGPHY_MII_1000CTL,
269 				    BRGPHY_1000CTL_MSE|BRGPHY_1000CTL_MSC);
270 			} else {
271 				PHY_WRITE(sc, BRGPHY_MII_1000CTL,
272 				    BRGPHY_1000CTL_MSE);
273 			}
274 			break;
275 #ifdef foo
276 		case IFM_NONE:
277 			PHY_WRITE(sc, MII_BMCR, BMCR_ISO|BMCR_PDOWN);
278 			break;
279 #endif
280 		case IFM_100_T4:
281 		default:
282 			return (EINVAL);
283 		}
284 		break;
285 
286 	case MII_TICK:
287 		/*
288 		 * If we're not currently selected, just return.
289 		 */
290 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
291 			return (0);
292 
293 		/*
294 		 * Is the interface even up?
295 		 */
296 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
297 			return (0);
298 
299 		/*
300 		 * Only used for autonegotiation.
301 		 */
302 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
303 			break;
304 
305 		/*
306 		 * Check to see if we have link.  If we do, we don't
307 		 * need to restart the autonegotiation process.  Read
308 		 * the BMSR twice in case it's latched.
309 		 */
310 		reg = PHY_READ(sc, BRGPHY_MII_AUXSTS);
311 		if (reg & BRGPHY_AUXSTS_LINK)
312 			break;
313 
314 		/*
315 		 * Only retry autonegotiation every 5 seconds.
316 		 */
317 		if (++sc->mii_ticks != 5)
318 			return (0);
319 
320 		sc->mii_ticks = 0;
321 		mii_phy_reset(sc);
322 		brgphy_mii_phy_auto(sc);
323 		return (0);
324 	}
325 
326 	/* Update the media status. */
327 	brgphy_status(sc);
328 
329 	/* Callback if something changed. */
330 	mii_phy_update(sc, cmd);
331 	return (0);
332 }
333 
334 static void
335 brgphy_status(sc)
336 	struct mii_softc *sc;
337 {
338 	struct mii_data *mii = sc->mii_pdata;
339 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
340 	int bmsr, bmcr;
341 
342 	mii->mii_media_status = IFM_AVALID;
343 	mii->mii_media_active = IFM_ETHER;
344 
345 	bmsr = PHY_READ(sc, BRGPHY_MII_BMSR);
346 	if (PHY_READ(sc, BRGPHY_MII_AUXSTS) & BRGPHY_AUXSTS_LINK)
347 		mii->mii_media_status |= IFM_ACTIVE;
348 
349 	bmcr = PHY_READ(sc, BRGPHY_MII_BMCR);
350 
351 	if (bmcr & BRGPHY_BMCR_LOOP)
352 		mii->mii_media_active |= IFM_LOOP;
353 
354 	if (bmcr & BRGPHY_BMCR_AUTOEN) {
355 		if ((bmsr & BRGPHY_BMSR_ACOMP) == 0) {
356 			/* Erg, still trying, I guess... */
357 			mii->mii_media_active |= IFM_NONE;
358 			return;
359 		}
360 
361 		switch (PHY_READ(sc, BRGPHY_MII_AUXSTS) &
362 		    BRGPHY_AUXSTS_AN_RES) {
363 		case BRGPHY_RES_1000FD:
364 			mii->mii_media_active |= IFM_1000_T | IFM_FDX;
365 			break;
366 		case BRGPHY_RES_1000HD:
367 			mii->mii_media_active |= IFM_1000_T | IFM_HDX;
368 			break;
369 		case BRGPHY_RES_100FD:
370 			mii->mii_media_active |= IFM_100_TX | IFM_FDX;
371 			break;
372 		case BRGPHY_RES_100T4:
373 			mii->mii_media_active |= IFM_100_T4;
374 			break;
375 		case BRGPHY_RES_100HD:
376 			mii->mii_media_active |= IFM_100_TX | IFM_HDX;
377 			break;
378 		case BRGPHY_RES_10FD:
379 			mii->mii_media_active |= IFM_10_T | IFM_FDX;
380 			break;
381 		case BRGPHY_RES_10HD:
382 			mii->mii_media_active |= IFM_10_T | IFM_HDX;
383 			break;
384 		default:
385 			mii->mii_media_active |= IFM_NONE;
386 			break;
387 		}
388 		return;
389 	}
390 
391 	mii->mii_media_active = ife->ifm_media;
392 
393 	return;
394 }
395 
396 
397 static int
398 brgphy_mii_phy_auto(mii)
399 	struct mii_softc *mii;
400 {
401 	int ktcr = 0;
402 
403 	mii_phy_reset(mii);
404 	PHY_WRITE(mii, BRGPHY_MII_BMCR, 0);
405 	DELAY(1000);
406 	ktcr = PHY_READ(mii, BRGPHY_MII_1000CTL);
407 	PHY_WRITE(mii, BRGPHY_MII_1000CTL, ktcr |
408 	    BRGPHY_1000CTL_AFD|BRGPHY_1000CTL_AHD);
409 	ktcr = PHY_READ(mii, BRGPHY_MII_1000CTL);
410 	DELAY(1000);
411 	PHY_WRITE(mii, BRGPHY_MII_ANAR,
412 	    BMSR_MEDIA_TO_ANAR(mii->mii_capabilities) | ANAR_CSMA);
413 	DELAY(1000);
414 	PHY_WRITE(mii, BRGPHY_MII_BMCR,
415 	    BRGPHY_BMCR_AUTOEN | BRGPHY_BMCR_STARTNEG);
416 	PHY_WRITE(mii, BRGPHY_MII_IMR, 0xFF00);
417 	return (EJUSTRETURN);
418 }
419