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