inphy.c revision 1.7 1 /* $NetBSD: inphy.c,v 1.7 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.
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 Intel's i82555 ethernet 10/100 PHY
71 * Data Sheet available from www.intel.com
72 */
73
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/kernel.h>
77 #include <sys/device.h>
78 #include <sys/malloc.h>
79 #include <sys/socket.h>
80
81 #include <net/if.h>
82 #include <net/if_media.h>
83
84 #include <dev/mii/mii.h>
85 #include <dev/mii/miivar.h>
86 #include <dev/mii/miidevs.h>
87
88 #include <dev/mii/inphyreg.h>
89
90 struct inphy_softc {
91 struct mii_softc sc_mii; /* generic PHY */
92 int sc_ticks;
93 int sc_active;
94 };
95
96 int inphymatch __P((struct device *, struct cfdata *, void *));
97 void inphyattach __P((struct device *, struct device *, void *));
98
99 struct cfattach inphy_ca = {
100 sizeof(struct inphy_softc), inphymatch, inphyattach
101 };
102
103 int inphy_service __P((struct mii_softc *, struct mii_data *, int));
104 void inphy_status __P((struct inphy_softc *));
105
106 int
107 inphymatch(parent, match, aux)
108 struct device *parent;
109 struct cfdata *match;
110 void *aux;
111 {
112 struct mii_attach_args *ma = aux;
113
114 if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_INTEL &&
115 MII_MODEL(ma->mii_id2) == MII_MODEL_INTEL_I82555)
116 return (1);
117
118 return (0);
119 }
120
121 void
122 inphyattach(parent, self, aux)
123 struct device *parent, *self;
124 void *aux;
125 {
126 struct inphy_softc *sc = (struct inphy_softc *)self;
127 struct mii_attach_args *ma = aux;
128 struct mii_data *mii = ma->mii_data;
129
130 printf(": %s, rev. %d\n", MII_STR_INTEL_I82555,
131 MII_REV(ma->mii_id2));
132
133 sc->sc_mii.mii_inst = mii->mii_instance;
134 sc->sc_mii.mii_phy = ma->mii_phyno;
135 sc->sc_mii.mii_service = inphy_service;
136 sc->sc_mii.mii_pdata = mii;
137
138 /*
139 * i82557 wedges if all of its PHYs are isolated!
140 */
141 if (strcmp(parent->dv_cfdata->cf_driver->cd_name, "fxp") == 0 &&
142 mii->mii_instance == 0)
143 sc->sc_mii.mii_flags |= MIIF_NOISOLATE;
144
145 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL)
146
147 if ((sc->sc_mii.mii_flags & MIIF_NOISOLATE) == 0)
148 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->sc_mii.mii_inst),
149 BMCR_ISO);
150
151 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->sc_mii.mii_inst),
152 BMCR_LOOP|BMCR_S100);
153
154 mii_phy_reset(&sc->sc_mii);
155
156 sc->sc_mii.mii_capabilities =
157 PHY_READ(&sc->sc_mii, MII_BMSR) & ma->mii_capmask;
158 printf("%s: ", sc->sc_mii.mii_dev.dv_xname);
159 if ((sc->sc_mii.mii_capabilities & BMSR_MEDIAMASK) == 0)
160 printf("no media present");
161 else
162 mii_add_media(mii, sc->sc_mii.mii_capabilities,
163 sc->sc_mii.mii_inst);
164 printf("\n");
165 #undef ADD
166 }
167
168 int
169 inphy_service(self, mii, cmd)
170 struct mii_softc *self;
171 struct mii_data *mii;
172 int cmd;
173 {
174 struct inphy_softc *sc = (struct inphy_softc *)self;
175 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
176 int reg;
177
178 switch (cmd) {
179 case MII_POLLSTAT:
180 /*
181 * If we're not polling our PHY instance, just return.
182 */
183 if (IFM_INST(ife->ifm_media) != sc->sc_mii.mii_inst)
184 return (0);
185 break;
186
187 case MII_MEDIACHG:
188 /*
189 * If the media indicates a different PHY instance,
190 * isolate ourselves.
191 */
192 if (IFM_INST(ife->ifm_media) != sc->sc_mii.mii_inst) {
193 reg = PHY_READ(&sc->sc_mii, MII_BMCR);
194 PHY_WRITE(&sc->sc_mii, MII_BMCR, reg | BMCR_ISO);
195 return (0);
196 }
197
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 * If we're not currently selected, just return.
231 */
232 if (IFM_INST(ife->ifm_media) != sc->sc_mii.mii_inst)
233 return (0);
234
235 /*
236 * Only used for autonegotiation.
237 */
238 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
239 return (0);
240
241 /*
242 * Is the interface even up?
243 */
244 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
245 return (0);
246
247 /*
248 * Check to see if we have link. If we do, we don't
249 * need to restart the autonegotiation process. Read
250 * the BMSR twice in case it's latched.
251 */
252 reg = PHY_READ(&sc->sc_mii, MII_BMSR) |
253 PHY_READ(&sc->sc_mii, MII_BMSR);
254 if (reg & BMSR_LINK)
255 return (0);
256
257 /*
258 * Only retry autonegotiation every 5 seconds.
259 */
260 if (++sc->sc_ticks != 5)
261 return (0);
262
263 sc->sc_ticks = 0;
264 mii_phy_reset(&sc->sc_mii);
265 (void) mii_phy_auto(&sc->sc_mii);
266 break;
267 }
268
269 /* Update the media status. */
270 inphy_status(sc);
271
272 /* Callback if something changed. */
273 if (sc->sc_active != mii->mii_media_active || cmd == MII_MEDIACHG) {
274 (*mii->mii_statchg)(sc->sc_mii.mii_dev.dv_parent);
275 sc->sc_active = mii->mii_media_active;
276 }
277 return (0);
278 }
279
280 void
281 inphy_status(sc)
282 struct inphy_softc *sc;
283 {
284 struct mii_data *mii = sc->sc_mii.mii_pdata;
285 int bmsr, bmcr, scr;
286
287 mii->mii_media_status = IFM_AVALID;
288 mii->mii_media_active = IFM_ETHER;
289
290 bmsr = PHY_READ(&sc->sc_mii, MII_BMSR) |
291 PHY_READ(&sc->sc_mii, MII_BMSR);
292 if (bmsr & BMSR_LINK)
293 mii->mii_media_status |= IFM_ACTIVE;
294
295 bmcr = PHY_READ(&sc->sc_mii, MII_BMCR);
296 if (bmcr & BMCR_ISO) {
297 mii->mii_media_active |= IFM_NONE;
298 mii->mii_media_status = 0;
299 return;
300 }
301
302 if (bmcr & BMCR_LOOP)
303 mii->mii_media_active |= IFM_LOOP;
304
305 if (bmcr & BMCR_AUTOEN) {
306 if ((bmsr & BMSR_ACOMP) == 0) {
307 /* Erg, still trying, I guess... */
308 mii->mii_media_active |= IFM_NONE;
309 return;
310 }
311 scr = PHY_READ(&sc->sc_mii, MII_INPHY_SCR);
312 if (scr & SCR_T4)
313 mii->mii_media_active |= IFM_100_T4;
314 else if (scr & SCR_S100)
315 mii->mii_media_active |= IFM_100_TX;
316 else
317 mii->mii_media_active |= IFM_10_T;
318 if (scr & SCR_FDX)
319 mii->mii_media_active |= IFM_FDX;
320 } else {
321 if (bmcr & BMCR_S100)
322 mii->mii_media_active |= IFM_100_TX;
323 else
324 mii->mii_media_active |= IFM_10_T;
325 if (bmcr & BMCR_FDX)
326 mii->mii_media_active |= IFM_FDX;
327 }
328 }
329