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