mii_physubr.c revision 1.10 1 /* $NetBSD: mii_physubr.c,v 1.10 2000/02/02 08:05:33 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 /*
194 * Initialize generic PHY media based on BMSR, called when a PHY is
195 * attached. We expect to be set up to print a comma-separated list
196 * of media names. Does not print a newline.
197 */
198 void
199 mii_phy_add_media(sc)
200 struct mii_softc *sc;
201 {
202 struct mii_data *mii = sc->mii_pdata;
203 const char *sep = "";
204
205 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL)
206 #define PRINT(s) printf("%s%s", sep, s); sep = ", "
207
208 if ((sc->mii_flags & MIIF_NOISOLATE) == 0)
209 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
210 MII_MEDIA_NONE);
211
212 if (sc->mii_capabilities & BMSR_10THDX) {
213 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, sc->mii_inst),
214 MII_MEDIA_10_T);
215 #if 0
216 if ((sc->mii_flags & MIIF_NOLOOP) == 0)
217 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_LOOP,
218 sc->mii_inst), MII_MEDIA_10_T);
219 #endif
220 PRINT("10baseT");
221 }
222 if (sc->mii_capabilities & BMSR_10TFDX) {
223 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, sc->mii_inst),
224 MII_MEDIA_10_T_FDX);
225 PRINT("10baseT-FDX");
226 }
227 if (sc->mii_capabilities & BMSR_100TXHDX) {
228 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, sc->mii_inst),
229 MII_MEDIA_100_TX);
230 #if 0
231 if ((sc->mii_flags & MIIF_NOLOOP) == 0)
232 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP,
233 sc->mii_inst), MII_MEDIA_100_TX);
234 #endif
235 PRINT("100baseTX");
236 }
237 if (sc->mii_capabilities & BMSR_100TXFDX) {
238 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, sc->mii_inst),
239 MII_MEDIA_100_TX_FDX);
240 PRINT("100baseTX-FDX");
241 }
242 if (sc->mii_capabilities & BMSR_100T4) {
243 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_T4, 0, sc->mii_inst),
244 MII_MEDIA_100_T4);
245 #if 0
246 if ((sc->mii_flags & MIIF_NOLOOP) == 0)
247 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_T4, IFM_LOOP,
248 sc->mii_inst), MII_MEDIA_100_T4);
249 #endif
250 PRINT("100baseT4");
251 }
252 if (sc->mii_capabilities & BMSR_ANEG) {
253 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst),
254 MII_NMEDIA); /* intentionally invalid index */
255 PRINT("auto");
256 }
257 #undef ADD
258 #undef PRINT
259 }
260
261 void
262 mii_phy_delete_media(sc)
263 struct mii_softc *sc;
264 {
265 struct mii_data *mii = sc->mii_pdata;
266
267 ifmedia_delete_instance(&mii->mii_media, sc->mii_inst);
268 }
269
270 int
271 mii_phy_activate(self, act)
272 struct device *self;
273 enum devact act;
274 {
275 int rv = 0;
276
277 switch (act) {
278 case DVACT_ACTIVATE:
279 rv = EOPNOTSUPP;
280 break;
281
282 case DVACT_DEACTIVATE:
283 /* Nothing special to do. */
284 break;
285 }
286
287 return (rv);
288 }
289
290 int
291 mii_phy_detach(self, flags)
292 struct device *self;
293 int flags;
294 {
295 struct mii_softc *sc = (void *) self;
296
297 if (sc->mii_flags & MIIF_DOINGAUTO)
298 untimeout(mii_phy_auto_timeout, sc);
299
300 mii_phy_delete_media(sc);
301
302 return (0);
303 }
304