tlphy.c revision 1.10 1 /* $NetBSD: tlphy.c,v 1.10 1998/08/17 16:41:45 bouyer 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 Texas Instruments's ThunderLAN 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/socket.h>
78
79 #include <machine/bus.h>
80
81 #include <net/if.h>
82 #include <net/if_media.h>
83
84 #include <net/if_ether.h>
85
86 #include <dev/mii/mii.h>
87 #include <dev/mii/miivar.h>
88 #include <dev/mii/miidevs.h>
89
90 #include <dev/i2c/i2c_bus.h>
91
92 #include <dev/mii/tlphyreg.h>
93 #include <dev/mii/tlphyvar.h>
94
95 /* ThunderLAN PHY can only be on a ThunderLAN */
96 #include <dev/pci/if_tlvar.h>
97
98 struct tlphy_softc {
99 struct mii_softc sc_mii; /* generic PHY */
100 int sc_capabilities;
101 int sc_ticks;
102 int sc_tlphycap;
103 int sc_active;
104 };
105
106 int tlphymatch __P((struct device *, struct cfdata *, void *));
107 void tlphyattach __P((struct device *, struct device *, void *));
108
109 struct cfattach tlphy_ca = {
110 sizeof(struct tlphy_softc), tlphymatch, tlphyattach
111 };
112
113 #define TLPHY_READ(sc, reg) \
114 (*(sc)->sc_mii.mii_pdata->mii_readreg)((sc)->sc_mii.mii_dev.dv_parent, \
115 (sc)->sc_mii.mii_phy, (reg))
116
117 #define TLPHY_WRITE(sc, reg, val) \
118 (*(sc)->sc_mii.mii_pdata->mii_writereg)((sc)->sc_mii.mii_dev.dv_parent, \
119 (sc)->sc_mii.mii_phy, (reg), (val))
120
121 int tlphy_service __P((struct mii_softc *, struct mii_data *, int));
122 void tlphy_reset __P((struct tlphy_softc *));
123 void tlphy_auto __P((struct tlphy_softc *));
124 void tlphy_status __P((struct tlphy_softc *));
125
126 int
127 tlphymatch(parent, match, aux)
128 struct device *parent;
129 struct cfdata *match;
130 void *aux;
131 {
132 struct mii_attach_args *ma = aux;
133
134 if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_TI &&
135 MII_MODEL(ma->mii_id2) == MII_MODEL_TI_TLAN10T)
136 return (1);
137
138 return (0);
139 }
140
141 void
142 tlphyattach(parent, self, aux)
143 struct device *parent, *self;
144 void *aux;
145 {
146 struct tlphy_softc *sc = (struct tlphy_softc *)self;
147 struct tl_softc *tlsc = (struct tl_softc *)self->dv_parent;
148 struct mii_attach_args *ma = aux;
149 struct mii_data *mii = ma->mii_data;
150 const char *sep = "";
151
152 printf(": %s, rev. %d\n", MII_STR_TI_TLAN10T,
153 MII_REV(ma->mii_id2));
154
155 sc->sc_mii.mii_inst = mii->mii_instance;
156 sc->sc_mii.mii_phy = ma->mii_phyno;
157 sc->sc_mii.mii_service = tlphy_service;
158 sc->sc_mii.mii_pdata = mii;
159
160 tlphy_reset(sc);
161
162 /*
163 * Note that if we're on a device that also supports 100baseTX,
164 * we are not going to want to use the built-in 10baseT port,
165 * since there will be another PHY on the MII wired up to the
166 * UTP connector. The parent indicates this to us by specifying
167 * the TLPHY_MEDIA_NO_10_T bit.
168 */
169 sc->sc_tlphycap = tlsc->tl_product->tp_tlphymedia;
170 if ((sc->sc_tlphycap & TLPHY_MEDIA_NO_10_T) == 0)
171 sc->sc_capabilities =
172 TLPHY_READ(sc, MII_BMSR) & ma->mii_capmask;
173 else
174 sc->sc_capabilities = 0;
175
176 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL)
177
178 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->sc_mii.mii_inst),
179 BMCR_ISO);
180
181 if ((sc->sc_tlphycap & TLPHY_MEDIA_NO_10_T) == 0)
182 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_LOOP,
183 sc->sc_mii.mii_inst), BMCR_LOOP);
184
185 #define PRINT(s) printf("%s%s", sep, s); sep = ", "
186
187 printf("%s: ", sc->sc_mii.mii_dev.dv_xname);
188 if (sc->sc_tlphycap) {
189 if (sc->sc_tlphycap & TLPHY_MEDIA_10_2) {
190 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_2, 0,
191 sc->sc_mii.mii_inst), 0);
192 PRINT("10base2/BNC");
193 } else if (sc->sc_tlphycap & TLPHY_MEDIA_10_5) {
194 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_5, 0,
195 sc->sc_mii.mii_inst), 0);
196 PRINT("10base5/AUI");
197 }
198 }
199 if (sc->sc_capabilities & BMSR_MEDIAMASK) {
200 printf(sep);
201 mii_add_media(mii, sc->sc_capabilities, sc->sc_mii.mii_inst);
202 } else if ((sc->sc_tlphycap & (TLPHY_MEDIA_10_2 | TLPHY_MEDIA_10_5))
203 == 0)
204 printf("no media present");
205 printf("\n");
206 #undef ADD
207 #undef PRINT
208 }
209
210 int
211 tlphy_service(self, mii, cmd)
212 struct mii_softc *self;
213 struct mii_data *mii;
214 int cmd;
215 {
216 struct tlphy_softc *sc = (struct tlphy_softc *)self;
217 int reg;
218
219 switch (cmd) {
220 case MII_POLLSTAT:
221 /*
222 * If we're not polling our PHY instance, just return.
223 */
224 if (IFM_INST(mii->mii_media.ifm_media) !=
225 sc->sc_mii.mii_inst)
226 return (0);
227 break;
228
229 case MII_MEDIACHG:
230 /*
231 * If the media indicates a different PHY instance,
232 * isolate ourselves.
233 */
234 if (IFM_INST(mii->mii_media.ifm_media) !=
235 sc->sc_mii.mii_inst) {
236 reg = TLPHY_READ(sc, MII_BMCR);
237 TLPHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
238 return (0);
239 }
240
241 /*
242 * If the interface is not up, don't do anything.
243 */
244 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
245 break;
246
247 switch (IFM_SUBTYPE(mii->mii_media.ifm_media)) {
248 case IFM_AUTO:
249 /*
250 * The ThunderLAN PHY doesn't self-configure after
251 * an autonegotiation cycle, so there's no such
252 * thing as "already in auto mode".
253 */
254 tlphy_auto(sc);
255 break;
256 case IFM_10_2:
257 case IFM_10_5:
258 TLPHY_WRITE(sc, MII_BMCR, 0);
259 TLPHY_WRITE(sc, MII_TLPHY_CTRL, CTRL_AUISEL);
260 delay(100000);
261 break;
262 default:
263 TLPHY_WRITE(sc, MII_TLPHY_CTRL, 0);
264 delay(100000);
265 TLPHY_WRITE(sc, MII_ANAR,
266 mii_anar(mii->mii_media.ifm_media));
267 TLPHY_WRITE(sc, MII_BMCR,
268 mii->mii_media.ifm_cur->ifm_data);
269 }
270 break;
271
272 case MII_TICK:
273 /*
274 * If we're not currently selected, just return.
275 */
276 if (IFM_INST(mii->mii_media.ifm_media) !=
277 sc->sc_mii.mii_inst)
278 return (0);
279
280 /*
281 * Only used for autonegotiation.
282 */
283 if (IFM_SUBTYPE(mii->mii_media.ifm_media) != IFM_AUTO)
284 return (0);
285
286 /*
287 * Is the interface even up?
288 */
289 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
290 return (0);
291
292 /*
293 * Check to see if we have link. If we do, we don't
294 * need to restart the autonegotiation process. Read
295 * the BMSR twice in case it's latched.
296 *
297 * XXX WHAT ABOUT CHECKING LINK ON THE BNC/AUI?!
298 */
299 reg = TLPHY_READ(sc, MII_BMSR) | TLPHY_READ(sc, MII_BMSR);
300 if (reg & BMSR_LINK)
301 return (0);
302
303 /*
304 * Only retry autonegotiation every 5 seconds.
305 */
306 if (++sc->sc_ticks != 5)
307 return (0);
308
309 sc->sc_ticks = 0;
310 tlphy_reset(sc);
311 tlphy_auto(sc);
312 break;
313 }
314
315 /* Update the media status. */
316 tlphy_status(sc);
317
318 /* Callback if something changed. */
319 if (sc->sc_active != mii->mii_media_active || cmd == MII_MEDIACHG) {
320 (*mii->mii_statchg)(sc->sc_mii.mii_dev.dv_parent);
321 sc->sc_active = mii->mii_media_active;
322 }
323 return (0);
324 }
325
326 void
327 tlphy_status(sc)
328 struct tlphy_softc *sc;
329 {
330 struct mii_data *mii = sc->sc_mii.mii_pdata;
331 struct tl_softc *tlsc = (struct tl_softc *)sc->sc_mii.mii_dev.dv_parent;
332 int bmsr, bmcr, tlctrl;
333
334 mii->mii_media_status = IFM_AVALID;
335 mii->mii_media_active = IFM_ETHER;
336
337 bmcr = TLPHY_READ(sc, MII_BMCR);
338 if (bmcr & BMCR_ISO) {
339 mii->mii_media_active |= IFM_NONE;
340 mii->mii_media_status = 0;
341 return;
342 }
343
344 tlctrl = TLPHY_READ(sc, MII_TLPHY_CTRL);
345 if (tlctrl & CTRL_AUISEL) {
346 if (sc->sc_tlphycap & TLPHY_MEDIA_10_2)
347 mii->mii_media_active |= IFM_10_2;
348 else if (sc->sc_tlphycap & TLPHY_MEDIA_10_5)
349 mii->mii_media_active |= IFM_10_5;
350 else
351 printf("%s: AUI selected with no matching media !\n",
352 sc->sc_mii.mii_dev.dv_xname);
353 if (tlsc->tl_flags & TL_IFACT)
354 mii->mii_media_status |= IFM_ACTIVE;
355 return;
356 }
357
358 bmsr = TLPHY_READ(sc, MII_BMSR) | TLPHY_READ(sc, MII_BMSR);
359 if (bmsr & BMSR_LINK)
360 mii->mii_media_status |= IFM_ACTIVE;
361
362 if (bmcr & BMCR_LOOP)
363 mii->mii_media_active |= IFM_LOOP;
364
365 /*
366 * Grr, braindead ThunderLAN PHY doesn't have any way to
367 * tell which media is actually active. (Note it also
368 * doesn't self-configure after autonegotiation.) We
369 * just have to report what's in the BMCR.
370 */
371 if (bmcr & BMCR_FDX)
372 mii->mii_media_active |= IFM_FDX;
373 mii->mii_media_active |= IFM_10_T;
374 }
375
376 void
377 tlphy_auto(sc)
378 struct tlphy_softc *sc;
379 {
380 int aner, anlpar, bmsr, i;
381
382 TLPHY_WRITE(sc, MII_ANAR,
383 BMSR_MEDIA_TO_ANAR(sc->sc_capabilities) | ANAR_CSMA);
384 TLPHY_WRITE(sc, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG);
385
386 /* Wait 500ms for it to complete. */
387 for (i = 0; i < 500; i++) {
388 if ((bmsr = TLPHY_READ(sc, MII_BMSR)) & BMSR_ACOMP)
389 return;
390 delay(1000);
391 }
392 if ((bmsr & BMSR_ACOMP) == 0) {
393 #if 0
394 printf("%s: autonegotiation failed to complete\n",
395 sc->sc_mii.mii_dev.dv_xname);
396 #endif
397 goto dflt;
398 }
399
400 /*
401 * Grr, braindead ThunderLAN PHY doesn't self-configure
402 * after autonegotiation. We have to do it ourselves
403 * based on the link partner status.
404 */
405
406 aner = TLPHY_READ(sc, MII_ANER);
407 if (aner & ANER_LPAN) {
408 anlpar = TLPHY_READ(sc, MII_ANLPAR) &
409 TLPHY_READ(sc, MII_ANAR);
410 if (anlpar & ANAR_10_FD) {
411 TLPHY_WRITE(sc, MII_BMCR, BMCR_FDX);
412 return;
413 }
414 }
415
416 dflt:
417 /*
418 * Just assume we're not in full-duplex mode.
419 * XXX Check link and try AUI/BNC?
420 */
421 TLPHY_WRITE(sc, MII_BMCR, 0);
422 }
423
424 void
425 tlphy_reset(sc)
426 struct tlphy_softc *sc;
427 {
428 int reg, i;
429
430 TLPHY_WRITE(sc, MII_BMCR, BMCR_RESET|BMCR_ISO);
431
432 /* Wait 100ms for it to complete. */
433 for (i = 0; i < 100; i++) {
434 reg = TLPHY_READ(sc, MII_BMCR);
435 if ((reg & BMCR_RESET) == 0)
436 break;
437 delay(1000);
438 }
439
440 /* Make sure the PHY is isolated. */
441 if (sc->sc_mii.mii_inst != 0)
442 TLPHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
443 }
444