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