1 /*	$NetBSD: mii_physubr.c,v 1.5 1999/08/03 19:41:49 drochner Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9  * NASA Ames Research Center.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the NetBSD
22  *	Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  *
39  * $FreeBSD: src/sys/dev/mii/mii_physubr.c,v 1.2.2.1 2000/12/12 19:29:14 wpaul Exp $
40  * $DragonFly: src/sys/dev/netif/mii_layer/mii_physubr.c,v 1.10 2005/10/31 12:49:05 sephe Exp $
41  */
42 
43 /*
44  * Subroutines common to all PHYs.
45  */
46 
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/socket.h>
51 #include <sys/errno.h>
52 #include <sys/module.h>
53 #include <sys/bus.h>
54 #include <sys/thread2.h>
55 
56 #include <machine/clock.h>
57 
58 #include <net/if.h>
59 #include <net/if_media.h>
60 
61 #include "mii.h"
62 #include "miivar.h"
63 
64 #include "miibus_if.h"
65 
66 void	mii_phy_auto_timeout (void *);
67 
68 void
69 mii_softc_init(struct mii_softc *mii, struct mii_attach_args *ma)
70 {
71 	callout_init(&mii->mii_auto_ch);
72 	mii->mii_phy = ma->mii_phyno;
73 	mii->mii_flags |= ma->mii_flags;
74 }
75 
76 int
77 mii_phy_auto(mii, waitfor)
78 	struct mii_softc *mii;
79 	int waitfor;
80 {
81 	int bmsr, i;
82 
83 	if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) {
84 		PHY_WRITE(mii, MII_ANAR, mii_bmsr_media_to_anar(mii));
85 		PHY_WRITE(mii, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG);
86 	}
87 
88 	if (waitfor) {
89 		/* Wait 500ms for it to complete. */
90 		for (i = 0; i < 500; i++) {
91 			if ((bmsr = PHY_READ(mii, MII_BMSR)) & BMSR_ACOMP)
92 				return (0);
93 			DELAY(1000);
94 #if 0
95 		if ((bmsr & BMSR_ACOMP) == 0)
96 			printf("%s: autonegotiation failed to complete\n",
97 			    mii->mii_dev.dv_xname);
98 #endif
99 		}
100 
101 		/*
102 		 * Don't need to worry about clearing MIIF_DOINGAUTO.
103 		 * If that's set, a timeout is pending, and it will
104 		 * clear the flag.
105 		 */
106 		return (EIO);
107 	}
108 
109 	/*
110 	 * Just let it finish asynchronously.  This is for the benefit of
111 	 * the tick handler driving autonegotiation.  Don't want 500ms
112 	 * delays all the time while the system is running!
113 	 */
114 	if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) {
115 		mii->mii_flags |= MIIF_DOINGAUTO;
116 		callout_reset(&mii->mii_auto_ch, hz >> 1,
117 				mii_phy_auto_timeout, mii);
118 	}
119 	return (EJUSTRETURN);
120 }
121 
122 void
123 mii_phy_auto_stop(sc)
124 	struct mii_softc *sc;
125 {
126 	if (sc->mii_flags & MIIF_DOINGAUTO) {
127 		sc->mii_flags &= ~MIIF_DOINGAUTO;
128 		callout_stop(&sc->mii_auto_ch);
129 	}
130 }
131 
132 void
133 mii_phy_auto_timeout(arg)
134 	void *arg;
135 {
136 	struct mii_softc *mii = arg;
137 	int bmsr;
138 
139 	crit_enter();
140 
141 	mii->mii_flags &= ~MIIF_DOINGAUTO;
142 	bmsr = PHY_READ(mii, MII_BMSR);
143 #if 0
144 	if ((bmsr & BMSR_ACOMP) == 0)
145 		printf("%s: autonegotiation failed to complete\n",
146 		    sc->sc_dev.dv_xname);
147 #endif
148 
149 	/* Update the media status. */
150 	(void) (*mii->mii_service)(mii, mii->mii_pdata, MII_POLLSTAT);
151 
152 	crit_exit();
153 }
154 
155 void
156 mii_phy_reset(mii)
157 	struct mii_softc *mii;
158 {
159 	int reg, i;
160 
161 	if (mii->mii_flags & MIIF_NOISOLATE)
162 		reg = BMCR_RESET;
163 	else
164 		reg = BMCR_RESET | BMCR_ISO;
165 	PHY_WRITE(mii, MII_BMCR, reg);
166 
167 	/* Wait 100ms for it to complete. */
168 	for (i = 0; i < 100; i++) {
169 		reg = PHY_READ(mii, MII_BMCR);
170 		if ((reg & BMCR_RESET) == 0)
171 			break;
172 		DELAY(1000);
173 	}
174 
175 	if (mii->mii_inst != 0 && ((mii->mii_flags & MIIF_NOISOLATE) == 0))
176 		PHY_WRITE(mii, MII_BMCR, reg | BMCR_ISO);
177 }
178 
179 /*
180  * Given an ifmedia word, return the corresponding ANAR value.
181  */
182 int
183 mii_anar(int media)
184 {
185 	int rv;
186 
187 	rv = ANAR_CSMA;
188 	switch (media & (IFM_TMASK|IFM_NMASK|IFM_FDX)) {
189 	case IFM_ETHER|IFM_1000_T:
190 		rv |= ANAR_1000;
191 		break;
192 	case IFM_ETHER|IFM_1000_T|IFM_FDX:
193 		rv |= ANAR_1000_FD;
194 		break;
195 	case IFM_ETHER|IFM_10_T:
196 		rv |= ANAR_10;
197 		break;
198 	case IFM_ETHER|IFM_10_T|IFM_FDX:
199 		rv |= ANAR_10_FD;
200 		break;
201 	case IFM_ETHER|IFM_100_TX:
202 		rv |= ANAR_TX;
203 		break;
204 	case IFM_ETHER|IFM_100_TX|IFM_FDX:
205 		rv |= ANAR_TX_FD;
206 		break;
207 	case IFM_ETHER|IFM_100_T4:
208 		rv |= ANAR_T4;
209 		break;
210 	default:
211 		rv = 0;
212 		break;
213 	}
214 	return (rv);
215 }
216 
217 /*
218  * Given a BMCR value, return the corresponding ifmedia word.
219  */
220 int
221 mii_media_from_bmcr(bmcr)
222 	int bmcr;
223 {
224 	int rv = IFM_ETHER;
225 
226 	if (bmcr & BMCR_S100)
227 		rv |= IFM_100_TX;
228 	else
229 		rv |= IFM_10_T;
230 	if (bmcr & BMCR_FDX)
231 		rv |= IFM_FDX;
232 
233 	return (rv);
234 }
235 
236 /*
237  * Initialize generic PHY media based on BMSR, called when a PHY is
238  * attached.  We expect to be set up to print a comma-separated list
239  * of media names.  Does not print a newline.
240  */
241 void
242 mii_add_media(struct mii_softc *sc, int bmsr)
243 {
244 	struct mii_data *mii = sc->mii_pdata;
245 	int instance = sc->mii_inst;
246 	const char *sep = "";
247 
248 #define	ADD(m, c)	ifmedia_add(&mii->mii_media, (m), (c), NULL)
249 #define	PRINT(s)	printf("%s%s", sep, s); sep = ", "
250 
251 	if (bmsr & BMSR_10THDX) {
252 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, instance), 0);
253 		PRINT("10baseT");
254 	}
255 	if (bmsr & BMSR_10TFDX) {
256 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, instance),
257 		    BMCR_FDX);
258 		PRINT("10baseT-FDX");
259 	}
260 	if (bmsr & BMSR_100TXHDX) {
261 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, instance),
262 		    BMCR_S100);
263 		PRINT("100baseTX");
264 	}
265 	if (bmsr & BMSR_100TXFDX) {
266 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, instance),
267 		    BMCR_S100|BMCR_FDX);
268 		PRINT("100baseTX-FDX");
269 	}
270 	if (bmsr & BMSR_100T4) {
271 		/*
272 		 * XXX How do you enable 100baseT4?  I assume we set
273 		 * XXX BMCR_S100 and then assume the PHYs will take
274 		 * XXX watever action is necessary to switch themselves
275 		 * XXX into T4 mode.
276 		 */
277 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_T4, 0, instance),
278 		    BMCR_S100);
279 		PRINT("100baseT4");
280 	}
281 	if (sc->mii_flags & MIIF_IS_1000X) {
282 		if (bmsr & BMSR_1000) {
283 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, IFM_FDX,
284 			    instance), BMCR_S1000);
285 			PRINT("1000baseT-FDX");
286 		}
287 	}
288 	if (bmsr & BMSR_ANEG) {
289 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, instance),
290 		    BMCR_AUTOEN);
291 		PRINT("auto");
292 	}
293 #undef ADD
294 #undef PRINT
295 }
296 
297 int
298 mii_bmsr_media_to_anar(struct mii_softc *mii)
299 {
300 	int bmsr = mii->mii_capabilities;
301  	int res = ANAR_CSMA;
302 
303 	if (bmsr & BMSR_100T4)
304 		res |= ANAR_T4;
305 	if (bmsr & BMSR_100TXFDX)
306 		res |= ANAR_TX_FD;
307 	if (bmsr & BMSR_100TXHDX)
308 		res |= ANAR_TX;
309 	if (bmsr & BMSR_10TFDX)
310 		res |= ANAR_10_FD;
311 	if (bmsr & BMSR_10THDX)
312 		res |= ANAR_10;
313 	if (mii->mii_flags & MIIF_IS_1000X) {
314 		if (bmsr & BMSR_1000)
315 			res |= ANAR_1000_FD;
316 	}
317 	return (res);
318 }
319