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