exphy.c revision 1.12 1 /* $NetBSD: exphy.c,v 1.12 1998/11/04 23:28:15 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_active;
90 };
91
92 int exphymatch __P((struct device *, struct cfdata *, void *));
93 void exphyattach __P((struct device *, struct device *, void *));
94
95 struct cfattach exphy_ca = {
96 sizeof(struct exphy_softc), exphymatch, exphyattach
97 };
98
99 int exphy_service __P((struct mii_softc *, struct mii_data *, int));
100 void exphy_reset __P((struct exphy_softc *));
101 void exphy_status __P((struct exphy_softc *));
102
103 int
104 exphymatch(parent, match, aux)
105 struct device *parent;
106 struct cfdata *match;
107 void *aux;
108 {
109 struct mii_attach_args *ma = aux;
110
111 /*
112 * Argh, 3Com PHY reports oui == 0 model == 0!
113 */
114 if (MII_OUI(ma->mii_id1, ma->mii_id2) != 0 &&
115 MII_MODEL(ma->mii_id2) != 0)
116 return (0);
117
118 /*
119 * Make sure the parent is an `ex'.
120 */
121 if (strcmp(parent->dv_cfdata->cf_driver->cd_name, "ex") != 0)
122 return (0);
123
124 return (1);
125 }
126
127 void
128 exphyattach(parent, self, aux)
129 struct device *parent, *self;
130 void *aux;
131 {
132 struct exphy_softc *sc = (struct exphy_softc *)self;
133 struct mii_attach_args *ma = aux;
134 struct mii_data *mii = ma->mii_data;
135
136 printf(": 3Com internal media interface\n");
137
138 sc->sc_mii.mii_inst = mii->mii_instance;
139 sc->sc_mii.mii_phy = ma->mii_phyno;
140 sc->sc_mii.mii_service = exphy_service;
141 sc->sc_mii.mii_pdata = mii;
142
143 /*
144 * The 3Com PHY can never be isolated, so never allow non-zero
145 * instances!
146 */
147 if (mii->mii_instance != 0) {
148 printf("%s: ignoring this PHY, non-zero instance\n",
149 sc->sc_mii.mii_dev.dv_xname);
150 return;
151 }
152 sc->sc_mii.mii_flags |= MIIF_NOISOLATE;
153
154 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL)
155
156 #if 0 /* See above. */
157 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->sc_mii.mii_inst),
158 BMCR_ISO);
159 #endif
160
161 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->sc_mii.mii_inst),
162 BMCR_LOOP|BMCR_S100);
163
164 exphy_reset(sc);
165
166 sc->sc_mii.mii_capabilities =
167 PHY_READ(&sc->sc_mii, MII_BMSR) & ma->mii_capmask;
168 printf("%s: ", sc->sc_mii.mii_dev.dv_xname);
169 if ((sc->sc_mii.mii_capabilities & BMSR_MEDIAMASK) == 0)
170 printf("no media present");
171 else
172 mii_add_media(mii, sc->sc_mii.mii_capabilities,
173 sc->sc_mii.mii_inst);
174 printf("\n");
175 #undef ADD
176 }
177
178 int
179 exphy_service(self, mii, cmd)
180 struct mii_softc *self;
181 struct mii_data *mii;
182 int cmd;
183 {
184 struct exphy_softc *sc = (struct exphy_softc *)self;
185 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
186
187 /*
188 * We can't isolate the 3Com PHY, so it has to be the only one!
189 */
190 if (IFM_INST(ife->ifm_media) != sc->sc_mii.mii_inst)
191 panic("exphy_service: can't isolate 3Com PHY");
192
193 switch (cmd) {
194 case MII_POLLSTAT:
195 break;
196
197 case MII_MEDIACHG:
198 /*
199 * If the interface is not up, don't do anything.
200 */
201 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
202 break;
203
204 switch (IFM_SUBTYPE(ife->ifm_media)) {
205 case IFM_AUTO:
206 /*
207 * If we're already in auto mode, just return.
208 */
209 if (PHY_READ(&sc->sc_mii, MII_BMCR) & BMCR_AUTOEN)
210 return (0);
211 (void) mii_phy_auto(&sc->sc_mii);
212 break;
213 case IFM_100_T4:
214 /*
215 * XXX Not supported as a manual setting right now.
216 */
217 return (EINVAL);
218 default:
219 /*
220 * BMCR data is stored in the ifmedia entry.
221 */
222 PHY_WRITE(&sc->sc_mii, MII_ANAR,
223 mii_anar(ife->ifm_media));
224 PHY_WRITE(&sc->sc_mii, MII_BMCR, ife->ifm_data);
225 }
226 break;
227
228 case MII_TICK:
229 /*
230 * Only used for autonegotiation.
231 */
232 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
233 return (0);
234
235 /*
236 * Is the interface even up?
237 */
238 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
239 return (0);
240
241 /*
242 * The 3Com PHY's autonegotiation doesn't need to be
243 * kicked; it continues in the background.
244 */
245 break;
246 }
247
248 /* Update the media status. */
249 exphy_status(sc);
250
251 /* Callback if something changed. */
252 if (sc->sc_active != mii->mii_media_active || cmd == MII_MEDIACHG) {
253 (*mii->mii_statchg)(sc->sc_mii.mii_dev.dv_parent);
254 sc->sc_active = mii->mii_media_active;
255 }
256 return (0);
257 }
258
259 void
260 exphy_status(sc)
261 struct exphy_softc *sc;
262 {
263 struct mii_data *mii = sc->sc_mii.mii_pdata;
264 int bmsr, bmcr, anlpar;
265
266 mii->mii_media_status = IFM_AVALID;
267 mii->mii_media_active = IFM_ETHER;
268
269 bmsr = PHY_READ(&sc->sc_mii, MII_BMSR) |
270 PHY_READ(&sc->sc_mii, MII_BMSR);
271 if (bmsr & BMSR_LINK)
272 mii->mii_media_status |= IFM_ACTIVE;
273
274 bmcr = PHY_READ(&sc->sc_mii, MII_BMCR);
275 if (bmcr & BMCR_ISO) {
276 mii->mii_media_active |= IFM_NONE;
277 mii->mii_media_status = 0;
278 return;
279 }
280
281 if (bmcr & BMCR_LOOP)
282 mii->mii_media_active |= IFM_LOOP;
283
284 if (bmcr & BMCR_AUTOEN) {
285 /*
286 * The 3Com PHY uses the highest-order common bit of
287 * the ANAR and ANLPAR (i.e. best media advertised
288 * both by us and our link partner).
289 */
290 if ((bmsr & BMSR_ACOMP) == 0) {
291 /* Erg, still trying, I guess... */
292 mii->mii_media_active |= IFM_NONE;
293 return;
294 }
295
296 anlpar = PHY_READ(&sc->sc_mii, MII_ANAR) &
297 PHY_READ(&sc->sc_mii, MII_ANLPAR);
298 if (anlpar & ANLPAR_T4)
299 mii->mii_media_active |= IFM_100_T4;
300 else if (anlpar & ANLPAR_TX_FD)
301 mii->mii_media_active |= IFM_100_TX|IFM_FDX;
302 else if (anlpar & ANLPAR_TX)
303 mii->mii_media_active |= IFM_100_TX;
304 else if (anlpar & ANLPAR_10_FD)
305 mii->mii_media_active |= IFM_10_T|IFM_FDX;
306 else if (anlpar & ANLPAR_10)
307 mii->mii_media_active |= IFM_10_T;
308 else
309 mii->mii_media_active |= IFM_NONE;
310 } else {
311 if (bmcr & BMCR_S100)
312 mii->mii_media_active |= IFM_100_TX;
313 else
314 mii->mii_media_active |= IFM_10_T;
315 if (bmcr & BMCR_FDX)
316 mii->mii_media_active |= IFM_FDX;
317 }
318 }
319
320 void
321 exphy_reset(sc)
322 struct exphy_softc *sc;
323 {
324
325 mii_phy_reset(&sc->sc_mii);
326
327 /*
328 * XXX 3Com PHY doesn't set the BMCR properly after
329 * XXX reset, which breaks autonegotiation.
330 */
331 PHY_WRITE(&sc->sc_mii, MII_BMCR, BMCR_S100|BMCR_AUTOEN|BMCR_FDX);
332 }
333