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