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