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