nsphy.c revision 1.9 1 /* $NetBSD: nsphy.c,v 1.9 1998/08/14 00:23:26 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 National Semiconductor's DP83840A ethernet 10/100 PHY
71 * Data Sheet available from www.national.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/nsphyreg.h>
89
90 struct nsphy_softc {
91 struct mii_softc sc_mii; /* generic PHY */
92 int sc_capabilities;
93 int sc_ticks;
94 int sc_active;
95 };
96
97 int nsphymatch __P((struct device *, struct cfdata *, void *));
98 void nsphyattach __P((struct device *, struct device *, void *));
99
100 struct cfattach nsphy_ca = {
101 sizeof(struct nsphy_softc), nsphymatch, nsphyattach
102 };
103
104 #define NSPHY_READ(sc, reg) \
105 (*(sc)->sc_mii.mii_pdata->mii_readreg)((sc)->sc_mii.mii_dev.dv_parent, \
106 (sc)->sc_mii.mii_phy, (reg))
107
108 #define NSPHY_WRITE(sc, reg, val) \
109 (*(sc)->sc_mii.mii_pdata->mii_writereg)((sc)->sc_mii.mii_dev.dv_parent, \
110 (sc)->sc_mii.mii_phy, (reg), (val))
111
112 int nsphy_service __P((struct mii_softc *, struct mii_data *, int));
113 void nsphy_reset __P((struct nsphy_softc *));
114 void nsphy_auto __P((struct nsphy_softc *));
115 void nsphy_status __P((struct nsphy_softc *));
116
117 int
118 nsphymatch(parent, match, aux)
119 struct device *parent;
120 struct cfdata *match;
121 void *aux;
122 {
123 struct mii_attach_args *ma = aux;
124
125 if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_NATSEMI &&
126 MII_MODEL(ma->mii_id2) == MII_MODEL_NATSEMI_DP83840)
127 return (1);
128
129 return (0);
130 }
131
132 void
133 nsphyattach(parent, self, aux)
134 struct device *parent, *self;
135 void *aux;
136 {
137 struct nsphy_softc *sc = (struct nsphy_softc *)self;
138 struct mii_attach_args *ma = aux;
139 struct mii_data *mii = ma->mii_data;
140
141 printf(": %s, rev. %d\n", MII_STR_NATSEMI_DP83840,
142 MII_REV(ma->mii_id2));
143
144 sc->sc_mii.mii_inst = mii->mii_instance;
145 sc->sc_mii.mii_phy = ma->mii_phyno;
146 sc->sc_mii.mii_service = nsphy_service;
147 sc->sc_mii.mii_pdata = mii;
148
149 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL)
150
151 #if 0
152 /* Can't do this on the i82557! */
153 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->sc_mii.mii_inst),
154 BMCR_ISO);
155 #endif
156 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->sc_mii.mii_inst),
157 BMCR_LOOP|BMCR_S100);
158
159 nsphy_reset(sc);
160
161 sc->sc_capabilities = NSPHY_READ(sc, MII_BMSR) & ma->mii_capmask;
162 printf("%s: ", sc->sc_mii.mii_dev.dv_xname);
163 if ((sc->sc_capabilities & BMSR_MEDIAMASK) == 0)
164 printf("no media present");
165 else
166 mii_add_media(mii, sc->sc_capabilities, sc->sc_mii.mii_inst);
167 printf("\n");
168 #undef ADD
169 }
170
171 int
172 nsphy_service(self, mii, cmd)
173 struct mii_softc *self;
174 struct mii_data *mii;
175 int cmd;
176 {
177 struct nsphy_softc *sc = (struct nsphy_softc *)self;
178 int reg;
179
180 switch (cmd) {
181 case MII_POLLSTAT:
182 /*
183 * If we're not polling our PHY instance, just return.
184 */
185 if (IFM_INST(mii->mii_media.ifm_media) !=
186 sc->sc_mii.mii_inst)
187 return (0);
188 break;
189
190 case MII_MEDIACHG:
191 /*
192 * If the media indicates a different PHY instance,
193 * isolate ourselves.
194 */
195 if (IFM_INST(mii->mii_media.ifm_media) !=
196 sc->sc_mii.mii_inst) {
197 reg = NSPHY_READ(sc, MII_BMCR);
198 NSPHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
199 return (0);
200 }
201
202 /*
203 * If the interface is not up, don't do anything.
204 */
205 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
206 break;
207
208 reg = NSPHY_READ(sc, MII_NSPHY_PCR);
209
210 /*
211 * Set up the PCR to use LED4 to indicate full-duplex
212 * in both 10baseT and 100baseTX modes.
213 */
214 reg |= PCR_LED4MODE;
215
216 /*
217 * Make sure Carrier Intgrity Monitor function is
218 * disabled (normal for Node operation, but sometimes
219 * it's not set?!)
220 */
221 reg |= PCR_CIMDIS;
222
223 /*
224 * Make sure "force link good" is not set. It's only
225 * intended for debugging, but sometimes it's set
226 * after a reset.
227 */
228 reg &= ~PCR_FLINK100;
229
230 #if 0
231 /*
232 * Mystery bits which are supposedly `reserved',
233 * but we seem to need to set them when the PHY
234 * is connected to some interfaces!
235 */
236 reg |= 0x0100 | 0x0400;
237 #endif
238
239 NSPHY_WRITE(sc, MII_NSPHY_PCR, reg);
240
241 switch (IFM_SUBTYPE(mii->mii_media.ifm_media)) {
242 case IFM_AUTO:
243 /*
244 * If we're already in auto mode, just return.
245 */
246 if (NSPHY_READ(sc, MII_BMCR) & BMCR_AUTOEN)
247 return (0);
248 nsphy_auto(sc);
249 break;
250 case IFM_100_T4:
251 /*
252 * XXX Not supported as a manual setting right now.
253 */
254 return (EINVAL);
255 default:
256 /*
257 * BMCR data is stored in the ifmedia entry.
258 */
259 NSPHY_WRITE(sc, MII_ANAR,
260 mii_anar(mii->mii_media.ifm_media));
261 NSPHY_WRITE(sc, MII_BMCR,
262 mii->mii_media.ifm_cur->ifm_data);
263 }
264 break;
265
266 case MII_TICK:
267 /*
268 * If we're not currently selected, just return.
269 */
270 if (IFM_INST(mii->mii_media.ifm_media) !=
271 sc->sc_mii.mii_inst)
272 return (0);
273
274 /*
275 * Only used for autonegotiation.
276 */
277 if (IFM_SUBTYPE(mii->mii_media.ifm_media) != IFM_AUTO)
278 return (0);
279
280 /*
281 * Is the interface even up?
282 */
283 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
284 return (0);
285
286 /*
287 * Check to see if we have link. If we do, we don't
288 * need to restart the autonegotiation process. Read
289 * the BMSR twice in case it's latched.
290 */
291 reg = NSPHY_READ(sc, MII_BMSR) | NSPHY_READ(sc, MII_BMSR);
292 if (reg & BMSR_LINK)
293 return (0);
294
295 /*
296 * Only retry autonegotiation every 5 seconds.
297 */
298 if (++sc->sc_ticks != 5)
299 return (0);
300
301 sc->sc_ticks = 0;
302 nsphy_reset(sc);
303 nsphy_auto(sc);
304 break;
305 }
306
307 /* Update the media status. */
308 nsphy_status(sc);
309
310 /* Callback if something changed. */
311 if (sc->sc_active != mii->mii_media_active || cmd == MII_MEDIACHG) {
312 (*mii->mii_statchg)(sc->sc_mii.mii_dev.dv_parent);
313 sc->sc_active = mii->mii_media_active;
314 }
315 return (0);
316 }
317
318 void
319 nsphy_status(sc)
320 struct nsphy_softc *sc;
321 {
322 struct mii_data *mii = sc->sc_mii.mii_pdata;
323 int bmsr, bmcr, par, anlpar;
324
325 mii->mii_media_status = IFM_AVALID;
326 mii->mii_media_active = IFM_ETHER;
327
328 bmsr = NSPHY_READ(sc, MII_BMSR) | NSPHY_READ(sc, MII_BMSR);
329 if (bmsr & BMSR_LINK)
330 mii->mii_media_status |= IFM_ACTIVE;
331
332 bmcr = NSPHY_READ(sc, MII_BMCR);
333 if (bmcr & BMCR_ISO) {
334 mii->mii_media_active |= IFM_NONE;
335 mii->mii_media_status = 0;
336 return;
337 }
338
339 if (bmcr & BMCR_LOOP)
340 mii->mii_media_active |= IFM_LOOP;
341
342 if (bmcr & BMCR_AUTOEN) {
343 /*
344 * The PAR status bits are only valid of autonegotiation
345 * has completed (or it's disabled).
346 */
347 if ((bmsr & BMSR_ACOMP) == 0) {
348 /* Erg, still trying, I guess... */
349 mii->mii_media_active |= IFM_NONE;
350 return;
351 }
352
353 /*
354 * Argh. The PAR doesn't seem to indicate duplex mode
355 * properly! Determine media based on link partner's
356 * advertised capabilities.
357 */
358 if (NSPHY_READ(sc, MII_ANER) & ANER_LPAN) {
359 anlpar = NSPHY_READ(sc, MII_ANAR) &
360 NSPHY_READ(sc, MII_ANLPAR);
361 if (anlpar & ANLPAR_T4)
362 mii->mii_media_active |= IFM_100_T4;
363 else if (anlpar & ANLPAR_TX_FD)
364 mii->mii_media_active |= IFM_100_TX|IFM_FDX;
365 else if (anlpar & ANLPAR_TX)
366 mii->mii_media_active |= IFM_100_TX;
367 else if (anlpar & ANLPAR_10_FD)
368 mii->mii_media_active |= IFM_10_T|IFM_FDX;
369 else if (anlpar & ANLPAR_10)
370 mii->mii_media_active |= IFM_10_T;
371 else
372 mii->mii_media_active |= IFM_NONE;
373 return;
374 }
375
376 /*
377 * Link partner is not capable of autonegotiation.
378 * We will never be in full-duplex mode if this is
379 * the case, so reading the PAR is OK.
380 */
381 par = NSPHY_READ(sc, MII_NSPHY_PAR);
382 if (par & PAR_10)
383 mii->mii_media_active |= IFM_10_T;
384 else
385 mii->mii_media_active |= IFM_100_TX;
386 #if 0
387 if (par & PAR_FDX)
388 mii->mii_media_active |= IFM_FDX;
389 #endif
390 } else {
391 if (bmcr & BMCR_S100)
392 mii->mii_media_active |= IFM_100_TX;
393 else
394 mii->mii_media_active |= IFM_10_T;
395 if (bmcr & BMCR_FDX)
396 mii->mii_media_active |= IFM_FDX;
397 }
398 }
399
400 void
401 nsphy_auto(sc)
402 struct nsphy_softc *sc;
403 {
404 int bmsr, i;
405
406 NSPHY_WRITE(sc, MII_ANAR,
407 BMSR_MEDIA_TO_ANAR(sc->sc_capabilities) | ANAR_CSMA);
408 NSPHY_WRITE(sc, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG);
409
410 /* Wait 500ms for it to complete. */
411 for (i = 0; i < 500; i++) {
412 if ((bmsr = NSPHY_READ(sc, MII_BMSR)) & BMSR_ACOMP)
413 return;
414 delay(1000);
415 }
416 #if 0
417 if ((bmsr & BMSR_ACOMP) == 0)
418 printf("%s: autonegotiation failed to complete\n",
419 sc->sc_mii.mii_dev.dv_xname);
420 #endif
421 }
422
423 void
424 nsphy_reset(sc)
425 struct nsphy_softc *sc;
426 {
427 int reg, i;
428
429 /*
430 * The i82557 wedges if we isolate all of its PHYs!
431 */
432 if (sc->sc_mii.mii_inst == 0)
433 NSPHY_WRITE(sc, MII_BMCR, BMCR_RESET);
434 else
435 NSPHY_WRITE(sc, MII_BMCR, BMCR_RESET|BMCR_ISO);
436
437 /* Wait 100ms for it to complete. */
438 for (i = 0; i < 100; i++) {
439 reg = NSPHY_READ(sc, MII_BMCR);
440 if ((reg & BMCR_RESET) == 0)
441 break;
442 delay(1000);
443 }
444
445 /* Make sure the PHY is isolated. */
446 if (sc->sc_mii.mii_inst != 0)
447 NSPHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
448 }
449