exphy.c revision 1.10 1 /* $NetBSD: exphy.c,v 1.10 1998/11/04 22:15:40 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1998 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, and by Frank van der Linden.
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 * Copyright (c) 1997 Manuel Bouyer. All rights reserved.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software
52 * must display the following acknowledgement:
53 * This product includes software developed by Manuel Bouyer.
54 * 4. The name of the author may not be used to endorse or promote products
55 * derived from this software without specific prior written permission.
56 *
57 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
58 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
59 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
60 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
61 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
62 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
63 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
64 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
65 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
66 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
67 */
68
69 /*
70 * driver for 3Com internal PHYs
71 */
72
73 #include <sys/param.h>
74 #include <sys/systm.h>
75 #include <sys/kernel.h>
76 #include <sys/device.h>
77 #include <sys/malloc.h>
78 #include <sys/socket.h>
79
80 #include <net/if.h>
81 #include <net/if_media.h>
82
83 #include <dev/mii/mii.h>
84 #include <dev/mii/miivar.h>
85 #include <dev/mii/miidevs.h>
86
87 struct exphy_softc {
88 struct mii_softc sc_mii; /* generic PHY */
89 int sc_capabilities;
90 int sc_active;
91 };
92
93 int exphymatch __P((struct device *, struct cfdata *, void *));
94 void exphyattach __P((struct device *, struct device *, void *));
95
96 struct cfattach exphy_ca = {
97 sizeof(struct exphy_softc), exphymatch, exphyattach
98 };
99
100 int exphy_service __P((struct mii_softc *, struct mii_data *, int));
101 void exphy_reset __P((struct exphy_softc *));
102 void exphy_auto __P((struct exphy_softc *));
103 void exphy_status __P((struct exphy_softc *));
104
105 int
106 exphymatch(parent, match, aux)
107 struct device *parent;
108 struct cfdata *match;
109 void *aux;
110 {
111 struct mii_attach_args *ma = aux;
112
113 /*
114 * Argh, 3Com PHY reports oui == 0 model == 0!
115 */
116 if (MII_OUI(ma->mii_id1, ma->mii_id2) != 0 &&
117 MII_MODEL(ma->mii_id2) != 0)
118 return (0);
119
120 /*
121 * Make sure the parent is an `ex'.
122 */
123 if (strcmp(parent->dv_cfdata->cf_driver->cd_name, "ex") != 0)
124 return (0);
125
126 return (1);
127 }
128
129 void
130 exphyattach(parent, self, aux)
131 struct device *parent, *self;
132 void *aux;
133 {
134 struct exphy_softc *sc = (struct exphy_softc *)self;
135 struct mii_attach_args *ma = aux;
136 struct mii_data *mii = ma->mii_data;
137
138 printf(": 3Com internal media interface\n");
139
140 sc->sc_mii.mii_inst = mii->mii_instance;
141 sc->sc_mii.mii_phy = ma->mii_phyno;
142 sc->sc_mii.mii_service = exphy_service;
143 sc->sc_mii.mii_pdata = mii;
144
145 /*
146 * The 3Com PHY can never be isolated, so never allow non-zero
147 * instances!
148 */
149 if (mii->mii_instance != 0) {
150 printf("%s: ignoring this PHY, non-zero instance\n",
151 sc->sc_mii.mii_dev.dv_xname);
152 return;
153 }
154
155 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL)
156
157 #if 0 /* See above. */
158 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->sc_mii.mii_inst),
159 BMCR_ISO);
160 #endif
161
162 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->sc_mii.mii_inst),
163 BMCR_LOOP|BMCR_S100);
164
165 exphy_reset(sc);
166
167 sc->sc_capabilities = PHY_READ(&sc->sc_mii, MII_BMSR) & ma->mii_capmask;
168 printf("%s: ", sc->sc_mii.mii_dev.dv_xname);
169 if ((sc->sc_capabilities & BMSR_MEDIAMASK) == 0)
170 printf("no media present");
171 else
172 mii_add_media(mii, sc->sc_capabilities, sc->sc_mii.mii_inst);
173 printf("\n");
174 #undef ADD
175 }
176
177 int
178 exphy_service(self, mii, cmd)
179 struct mii_softc *self;
180 struct mii_data *mii;
181 int cmd;
182 {
183 struct exphy_softc *sc = (struct exphy_softc *)self;
184 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
185
186 /*
187 * We can't isolate the 3Com PHY, so it has to be the only one!
188 */
189 if (IFM_INST(ife->ifm_media) != sc->sc_mii.mii_inst)
190 panic("exphy_service: can't isolate 3Com PHY");
191
192 switch (cmd) {
193 case MII_POLLSTAT:
194 break;
195
196 case MII_MEDIACHG:
197 /*
198 * If the interface is not up, don't do anything.
199 */
200 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
201 break;
202
203 switch (IFM_SUBTYPE(ife->ifm_media)) {
204 case IFM_AUTO:
205 /*
206 * If we're already in auto mode, just return.
207 */
208 if (PHY_READ(&sc->sc_mii, MII_BMCR) & BMCR_AUTOEN)
209 return (0);
210 exphy_auto(sc);
211 break;
212 case IFM_100_T4:
213 /*
214 * XXX Not supported as a manual setting right now.
215 */
216 return (EINVAL);
217 default:
218 /*
219 * BMCR data is stored in the ifmedia entry.
220 */
221 PHY_WRITE(&sc->sc_mii, MII_ANAR,
222 mii_anar(ife->ifm_media));
223 PHY_WRITE(&sc->sc_mii, MII_BMCR, ife->ifm_data);
224 }
225 break;
226
227 case MII_TICK:
228 /*
229 * Only used for autonegotiation.
230 */
231 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
232 return (0);
233
234 /*
235 * Is the interface even up?
236 */
237 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
238 return (0);
239
240 /*
241 * The 3Com PHY's autonegotiation doesn't need to be
242 * kicked; it continues in the background.
243 */
244 break;
245 }
246
247 /* Update the media status. */
248 exphy_status(sc);
249
250 /* Callback if something changed. */
251 if (sc->sc_active != mii->mii_media_active || cmd == MII_MEDIACHG) {
252 (*mii->mii_statchg)(sc->sc_mii.mii_dev.dv_parent);
253 sc->sc_active = mii->mii_media_active;
254 }
255 return (0);
256 }
257
258 void
259 exphy_status(sc)
260 struct exphy_softc *sc;
261 {
262 struct mii_data *mii = sc->sc_mii.mii_pdata;
263 int bmsr, bmcr, anlpar;
264
265 mii->mii_media_status = IFM_AVALID;
266 mii->mii_media_active = IFM_ETHER;
267
268 bmsr = PHY_READ(&sc->sc_mii, MII_BMSR) |
269 PHY_READ(&sc->sc_mii, MII_BMSR);
270 if (bmsr & BMSR_LINK)
271 mii->mii_media_status |= IFM_ACTIVE;
272
273 bmcr = PHY_READ(&sc->sc_mii, MII_BMCR);
274 if (bmcr & BMCR_ISO) {
275 mii->mii_media_active |= IFM_NONE;
276 mii->mii_media_status = 0;
277 return;
278 }
279
280 if (bmcr & BMCR_LOOP)
281 mii->mii_media_active |= IFM_LOOP;
282
283 if (bmcr & BMCR_AUTOEN) {
284 /*
285 * The 3Com PHY uses the highest-order common bit of
286 * the ANAR and ANLPAR (i.e. best media advertised
287 * both by us and our link partner).
288 */
289 if ((bmsr & BMSR_ACOMP) == 0) {
290 /* Erg, still trying, I guess... */
291 mii->mii_media_active |= IFM_NONE;
292 return;
293 }
294
295 anlpar = PHY_READ(&sc->sc_mii, MII_ANAR) &
296 PHY_READ(&sc->sc_mii, MII_ANLPAR);
297 if (anlpar & ANLPAR_T4)
298 mii->mii_media_active |= IFM_100_T4;
299 else if (anlpar & ANLPAR_TX_FD)
300 mii->mii_media_active |= IFM_100_TX|IFM_FDX;
301 else if (anlpar & ANLPAR_TX)
302 mii->mii_media_active |= IFM_100_TX;
303 else if (anlpar & ANLPAR_10_FD)
304 mii->mii_media_active |= IFM_10_T|IFM_FDX;
305 else if (anlpar & ANLPAR_10)
306 mii->mii_media_active |= IFM_10_T;
307 else
308 mii->mii_media_active |= IFM_NONE;
309 } else {
310 if (bmcr & BMCR_S100)
311 mii->mii_media_active |= IFM_100_TX;
312 else
313 mii->mii_media_active |= IFM_10_T;
314 if (bmcr & BMCR_FDX)
315 mii->mii_media_active |= IFM_FDX;
316 }
317 }
318
319 void
320 exphy_auto(sc)
321 struct exphy_softc *sc;
322 {
323 int bmsr, i;
324
325 PHY_WRITE(&sc->sc_mii, MII_ANAR,
326 BMSR_MEDIA_TO_ANAR(sc->sc_capabilities) | ANAR_CSMA);
327 PHY_WRITE(&sc->sc_mii, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG);
328
329 /* Wait 500ms for it to complete. */
330 for (i = 0; i < 500; i++) {
331 if ((bmsr = PHY_READ(&sc->sc_mii, MII_BMSR)) & BMSR_ACOMP)
332 return;
333 delay(1000);
334 }
335 #if 0
336 if ((bmsr & BMSR_ACOMP) == 0)
337 printf("%s: autonegotiation failed to complete\n",
338 sc->sc_mii.mii_dev.dv_xname);
339 #endif
340 }
341
342 void
343 exphy_reset(sc)
344 struct exphy_softc *sc;
345 {
346 int reg, i;
347
348 PHY_WRITE(&sc->sc_mii, MII_BMCR, BMCR_RESET);
349
350 /* Wait 100ms for it to complete. */
351 for (i = 0; i < 100; i++) {
352 reg = PHY_READ(&sc->sc_mii, MII_BMCR);
353 if ((reg & BMCR_RESET) == 0)
354 break;
355 delay(1000);
356 }
357
358 /*
359 * XXX 3Com PHY doesn't set the BMCR properly after
360 * XXX reset, which breaks autonegotiation.
361 */
362 PHY_WRITE(&sc->sc_mii, MII_BMCR, BMCR_S100|BMCR_AUTOEN|BMCR_FDX);
363 }
364