mii_physubr.c revision 1.11 1 /* $NetBSD: mii_physubr.c,v 1.11 2000/02/02 17:50:45 thorpej 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 /*
58 * Media to register setting conversion table. Order matters.
59 */
60 const struct mii_media mii_media_table[] = {
61 { BMCR_ISO, ANAR_CSMA }, /* None */
62 { 0, ANAR_CSMA|ANAR_10 }, /* 10baseT */
63 { BMCR_FDX, ANAR_CSMA|ANAR_10_FD }, /* 10baseT-FDX */
64 { BMCR_S100, ANAR_CSMA|ANAR_T4 }, /* 100baseT4 */
65 { BMCR_S100, ANAR_CSMA|ANAR_TX }, /* 100baseTX */
66 { BMCR_S100|BMCR_FDX, ANAR_CSMA|ANAR_TX_FD }, /* 100baseTX-FDX */
67 };
68
69 void mii_phy_auto_timeout __P((void *));
70
71 void
72 mii_phy_setmedia(sc)
73 struct mii_softc *sc;
74 {
75 struct mii_data *mii = sc->mii_pdata;
76 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
77 int bmcr, anar;
78
79 /*
80 * Table index is stored in the media entry.
81 */
82
83 #ifdef DIAGNOSTIC
84 if (ife->ifm_data < 0 || ife->ifm_data >= MII_NMEDIA)
85 panic("mii_phy_setmedia");
86 #endif
87
88 anar = mii_media_table[ife->ifm_data].mm_anar;
89 bmcr = mii_media_table[ife->ifm_data].mm_bmcr;
90
91 if (ife->ifm_media & IFM_LOOP)
92 bmcr |= BMCR_LOOP;
93
94 PHY_WRITE(sc, MII_ANAR, anar);
95 PHY_WRITE(sc, MII_BMCR, bmcr);
96 }
97
98 int
99 mii_phy_auto(sc, waitfor)
100 struct mii_softc *sc;
101 int waitfor;
102 {
103 int bmsr, i;
104
105 if ((sc->mii_flags & MIIF_DOINGAUTO) == 0) {
106 PHY_WRITE(sc, MII_ANAR,
107 BMSR_MEDIA_TO_ANAR(sc->mii_capabilities) | ANAR_CSMA);
108 PHY_WRITE(sc, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG);
109 }
110
111 if (waitfor) {
112 /* Wait 500ms for it to complete. */
113 for (i = 0; i < 500; i++) {
114 if ((bmsr = PHY_READ(sc, MII_BMSR)) & BMSR_ACOMP)
115 return (0);
116 delay(1000);
117 }
118
119 /*
120 * Don't need to worry about clearing MIIF_DOINGAUTO.
121 * If that's set, a timeout is pending, and it will
122 * clear the flag.
123 */
124 return (EIO);
125 }
126
127 /*
128 * Just let it finish asynchronously. This is for the benefit of
129 * the tick handler driving autonegotiation. Don't want 500ms
130 * delays all the time while the system is running!
131 */
132 if ((sc->mii_flags & MIIF_DOINGAUTO) == 0) {
133 sc->mii_flags |= MIIF_DOINGAUTO;
134 timeout(mii_phy_auto_timeout, sc, hz >> 1);
135 }
136 return (EJUSTRETURN);
137 }
138
139 void
140 mii_phy_auto_timeout(arg)
141 void *arg;
142 {
143 struct mii_softc *sc = arg;
144 int s, bmsr;
145
146 if ((sc->mii_dev.dv_flags & DVF_ACTIVE) == 0)
147 return;
148
149 s = splnet();
150 sc->mii_flags &= ~MIIF_DOINGAUTO;
151 bmsr = PHY_READ(sc, MII_BMSR);
152
153 /* Update the media status. */
154 (void) (*sc->mii_service)(sc, sc->mii_pdata, MII_POLLSTAT);
155 splx(s);
156 }
157
158 void
159 mii_phy_reset(sc)
160 struct mii_softc *sc;
161 {
162 int reg, i;
163
164 if (sc->mii_flags & MIIF_NOISOLATE)
165 reg = BMCR_RESET;
166 else
167 reg = BMCR_RESET | BMCR_ISO;
168 PHY_WRITE(sc, MII_BMCR, reg);
169
170 /* Wait 100ms for it to complete. */
171 for (i = 0; i < 100; i++) {
172 reg = PHY_READ(sc, MII_BMCR);
173 if ((reg & BMCR_RESET) == 0)
174 break;
175 delay(1000);
176 }
177
178 if (sc->mii_inst != 0 && ((sc->mii_flags & MIIF_NOISOLATE) == 0))
179 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
180 }
181
182 void
183 mii_phy_down(sc)
184 struct mii_softc *sc;
185 {
186
187 if (sc->mii_flags & MIIF_DOINGAUTO) {
188 sc->mii_flags &= ~MIIF_DOINGAUTO;
189 untimeout(mii_phy_auto_timeout, sc);
190 }
191 }
192
193 void
194 mii_phy_status(sc)
195 struct mii_softc *sc;
196 {
197
198 (*sc->mii_status)(sc);
199 }
200
201 /*
202 * Initialize generic PHY media based on BMSR, called when a PHY is
203 * attached. We expect to be set up to print a comma-separated list
204 * of media names. Does not print a newline.
205 */
206 void
207 mii_phy_add_media(sc)
208 struct mii_softc *sc;
209 {
210 struct mii_data *mii = sc->mii_pdata;
211 const char *sep = "";
212
213 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL)
214 #define PRINT(s) printf("%s%s", sep, s); sep = ", "
215
216 if ((sc->mii_flags & MIIF_NOISOLATE) == 0)
217 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
218 MII_MEDIA_NONE);
219
220 if (sc->mii_capabilities & BMSR_10THDX) {
221 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, sc->mii_inst),
222 MII_MEDIA_10_T);
223 #if 0
224 if ((sc->mii_flags & MIIF_NOLOOP) == 0)
225 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_LOOP,
226 sc->mii_inst), MII_MEDIA_10_T);
227 #endif
228 PRINT("10baseT");
229 }
230 if (sc->mii_capabilities & BMSR_10TFDX) {
231 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, sc->mii_inst),
232 MII_MEDIA_10_T_FDX);
233 PRINT("10baseT-FDX");
234 }
235 if (sc->mii_capabilities & BMSR_100TXHDX) {
236 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, sc->mii_inst),
237 MII_MEDIA_100_TX);
238 #if 0
239 if ((sc->mii_flags & MIIF_NOLOOP) == 0)
240 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP,
241 sc->mii_inst), MII_MEDIA_100_TX);
242 #endif
243 PRINT("100baseTX");
244 }
245 if (sc->mii_capabilities & BMSR_100TXFDX) {
246 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, sc->mii_inst),
247 MII_MEDIA_100_TX_FDX);
248 PRINT("100baseTX-FDX");
249 }
250 if (sc->mii_capabilities & BMSR_100T4) {
251 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_T4, 0, sc->mii_inst),
252 MII_MEDIA_100_T4);
253 #if 0
254 if ((sc->mii_flags & MIIF_NOLOOP) == 0)
255 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_T4, IFM_LOOP,
256 sc->mii_inst), MII_MEDIA_100_T4);
257 #endif
258 PRINT("100baseT4");
259 }
260 if (sc->mii_capabilities & BMSR_ANEG) {
261 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst),
262 MII_NMEDIA); /* intentionally invalid index */
263 PRINT("auto");
264 }
265 #undef ADD
266 #undef PRINT
267 }
268
269 void
270 mii_phy_delete_media(sc)
271 struct mii_softc *sc;
272 {
273 struct mii_data *mii = sc->mii_pdata;
274
275 ifmedia_delete_instance(&mii->mii_media, sc->mii_inst);
276 }
277
278 int
279 mii_phy_activate(self, act)
280 struct device *self;
281 enum devact act;
282 {
283 int rv = 0;
284
285 switch (act) {
286 case DVACT_ACTIVATE:
287 rv = EOPNOTSUPP;
288 break;
289
290 case DVACT_DEACTIVATE:
291 /* Nothing special to do. */
292 break;
293 }
294
295 return (rv);
296 }
297
298 int
299 mii_phy_detach(self, flags)
300 struct device *self;
301 int flags;
302 {
303 struct mii_softc *sc = (void *) self;
304
305 if (sc->mii_flags & MIIF_DOINGAUTO)
306 untimeout(mii_phy_auto_timeout, sc);
307
308 mii_phy_delete_media(sc);
309
310 return (0);
311 }
312