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