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