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.4 2003/08/27 09:38:31 rob 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 
55 #include <machine/clock.h>
56 
57 #include <net/if.h>
58 #include <net/if_media.h>
59 
60 #include "mii.h"
61 #include "miivar.h"
62 
63 #include "miibus_if.h"
64 
65 void	mii_phy_auto_timeout (void *);
66 
67 int
68 mii_phy_auto(mii, waitfor)
69 	struct mii_softc *mii;
70 	int waitfor;
71 {
72 	int bmsr, i;
73 
74 	if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) {
75 		PHY_WRITE(mii, MII_ANAR,
76 		    BMSR_MEDIA_TO_ANAR(mii->mii_capabilities) | ANAR_CSMA);
77 		PHY_WRITE(mii, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG);
78 	}
79 
80 	if (waitfor) {
81 		/* Wait 500ms for it to complete. */
82 		for (i = 0; i < 500; i++) {
83 			if ((bmsr = PHY_READ(mii, MII_BMSR)) & BMSR_ACOMP)
84 				return (0);
85 			DELAY(1000);
86 #if 0
87 		if ((bmsr & BMSR_ACOMP) == 0)
88 			printf("%s: autonegotiation failed to complete\n",
89 			    mii->mii_dev.dv_xname);
90 #endif
91 		}
92 
93 		/*
94 		 * Don't need to worry about clearing MIIF_DOINGAUTO.
95 		 * If that's set, a timeout is pending, and it will
96 		 * clear the flag.
97 		 */
98 		return (EIO);
99 	}
100 
101 	/*
102 	 * Just let it finish asynchronously.  This is for the benefit of
103 	 * the tick handler driving autonegotiation.  Don't want 500ms
104 	 * delays all the time while the system is running!
105 	 */
106 	if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) {
107 		mii->mii_flags |= MIIF_DOINGAUTO;
108 		mii->mii_auto_ch = timeout(mii_phy_auto_timeout, mii, hz >> 1);
109 	}
110 	return (EJUSTRETURN);
111 }
112 
113 void
114 mii_phy_auto_stop(sc)
115 	struct mii_softc *sc;
116 {
117 	if (sc->mii_flags & MIIF_DOINGAUTO) {
118 		sc->mii_flags &= ~MIIF_DOINGAUTO;
119 		untimeout(mii_phy_auto_timeout, sc, sc->mii_auto_ch);
120 	}
121 }
122 
123 void
124 mii_phy_auto_timeout(arg)
125 	void *arg;
126 {
127 	struct mii_softc *mii = arg;
128 	int s, bmsr;
129 
130 	s = splnet();
131 	mii->mii_flags &= ~MIIF_DOINGAUTO;
132 	bmsr = PHY_READ(mii, MII_BMSR);
133 #if 0
134 	if ((bmsr & BMSR_ACOMP) == 0)
135 		printf("%s: autonegotiation failed to complete\n",
136 		    sc->sc_dev.dv_xname);
137 #endif
138 
139 	/* Update the media status. */
140 	(void) (*mii->mii_service)(mii, mii->mii_pdata, MII_POLLSTAT);
141 	splx(s);
142 }
143 
144 void
145 mii_phy_reset(mii)
146 	struct mii_softc *mii;
147 {
148 	int reg, i;
149 
150 	if (mii->mii_flags & MIIF_NOISOLATE)
151 		reg = BMCR_RESET;
152 	else
153 		reg = BMCR_RESET | BMCR_ISO;
154 	PHY_WRITE(mii, MII_BMCR, reg);
155 
156 	/* Wait 100ms for it to complete. */
157 	for (i = 0; i < 100; i++) {
158 		reg = PHY_READ(mii, MII_BMCR);
159 		if ((reg & BMCR_RESET) == 0)
160 			break;
161 		DELAY(1000);
162 	}
163 
164 	if (mii->mii_inst != 0 && ((mii->mii_flags & MIIF_NOISOLATE) == 0))
165 		PHY_WRITE(mii, MII_BMCR, reg | BMCR_ISO);
166 }
167 
168 /*
169  * Given an ifmedia word, return the corresponding ANAR value.
170  */
171 int
172 mii_anar(media)
173 	int media;
174 {
175 	int rv;
176 
177 	switch (media & (IFM_TMASK|IFM_NMASK|IFM_FDX)) {
178 	case IFM_ETHER|IFM_10_T:
179 		rv = ANAR_10|ANAR_CSMA;
180 		break;
181 	case IFM_ETHER|IFM_10_T|IFM_FDX:
182 		rv = ANAR_10_FD|ANAR_CSMA;
183 		break;
184 	case IFM_ETHER|IFM_100_TX:
185 		rv = ANAR_TX|ANAR_CSMA;
186 		break;
187 	case IFM_ETHER|IFM_100_TX|IFM_FDX:
188 		rv = ANAR_TX_FD|ANAR_CSMA;
189 		break;
190 	case IFM_ETHER|IFM_100_T4:
191 		rv = ANAR_T4|ANAR_CSMA;
192 		break;
193 	default:
194 		rv = 0;
195 		break;
196 	}
197 
198 	return (rv);
199 }
200 
201 /*
202  * Given a BMCR value, return the corresponding ifmedia word.
203  */
204 int
205 mii_media_from_bmcr(bmcr)
206 	int bmcr;
207 {
208 	int rv = IFM_ETHER;
209 
210 	if (bmcr & BMCR_S100)
211 		rv |= IFM_100_TX;
212 	else
213 		rv |= IFM_10_T;
214 	if (bmcr & BMCR_FDX)
215 		rv |= IFM_FDX;
216 
217 	return (rv);
218 }
219 
220 /*
221  * Initialize generic PHY media based on BMSR, called when a PHY is
222  * attached.  We expect to be set up to print a comma-separated list
223  * of media names.  Does not print a newline.
224  */
225 void
226 mii_add_media(mii, bmsr, instance)
227 	struct mii_data *mii;
228 	int bmsr, instance;
229 {
230 	const char *sep = "";
231 
232 #define	ADD(m, c)	ifmedia_add(&mii->mii_media, (m), (c), NULL)
233 #define	PRINT(s)	printf("%s%s", sep, s); sep = ", "
234 
235 	if (bmsr & BMSR_10THDX) {
236 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, instance), 0);
237 		PRINT("10baseT");
238 	}
239 	if (bmsr & BMSR_10TFDX) {
240 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, instance),
241 		    BMCR_FDX);
242 		PRINT("10baseT-FDX");
243 	}
244 	if (bmsr & BMSR_100TXHDX) {
245 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, instance),
246 		    BMCR_S100);
247 		PRINT("100baseTX");
248 	}
249 	if (bmsr & BMSR_100TXFDX) {
250 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, instance),
251 		    BMCR_S100|BMCR_FDX);
252 		PRINT("100baseTX-FDX");
253 	}
254 	if (bmsr & BMSR_100T4) {
255 		/*
256 		 * XXX How do you enable 100baseT4?  I assume we set
257 		 * XXX BMCR_S100 and then assume the PHYs will take
258 		 * XXX watever action is necessary to switch themselves
259 		 * XXX into T4 mode.
260 		 */
261 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_T4, 0, instance),
262 		    BMCR_S100);
263 		PRINT("100baseT4");
264 	}
265 	if (bmsr & BMSR_ANEG) {
266 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, instance),
267 		    BMCR_AUTOEN);
268 		PRINT("auto");
269 	}
270 #undef ADD
271 #undef PRINT
272 }
273