tlphy.c revision 1.51.2.1 1 /* $NetBSD: tlphy.c,v 1.51.2.1 2008/02/18 21:05:51 mjf 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.51.2.1 2008/02/18 21:05:51 mjf 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 <sys/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,
131 void *aux)
132 {
133 struct mii_attach_args *ma = aux;
134
135 if (mii_phy_match(ma, tlphys) != NULL)
136 return (10);
137
138 return (0);
139 }
140
141 static void
142 tlphyattach(struct device *parent, struct device *self, void *aux)
143 {
144 struct tlphy_softc *sc = device_private(self);
145 struct tl_softc *tlsc = device_private(device_parent(self));
146 struct mii_attach_args *ma = aux;
147 struct mii_data *mii = ma->mii_data;
148 const struct mii_phydesc *mpd;
149 const char *sep = "";
150
151 mpd = mii_phy_match(ma, tlphys);
152 aprint_naive(": Media interface\n");
153 aprint_normal(": %s, rev. %d\n", mpd->mpd_name, 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_funcs = &tlphy_funcs;
158 sc->sc_mii.mii_pdata = mii;
159 sc->sc_mii.mii_flags = ma->mii_flags;
160 sc->sc_mii.mii_anegticks = MII_ANEGTICKS;
161
162 PHY_RESET(&sc->sc_mii);
163
164 /*
165 * Note that if we're on a device that also supports 100baseTX,
166 * we are not going to want to use the built-in 10baseT port,
167 * since there will be another PHY on the MII wired up to the
168 * UTP connector. The parent indicates this to us by specifying
169 * the TLPHY_MEDIA_NO_10_T bit.
170 */
171 sc->sc_tlphycap = tlsc->tl_product->tp_tlphymedia;
172 if ((sc->sc_tlphycap & TLPHY_MEDIA_NO_10_T) == 0)
173 sc->sc_mii.mii_capabilities =
174 PHY_READ(&sc->sc_mii, MII_BMSR) & ma->mii_capmask;
175 else
176 sc->sc_mii.mii_capabilities = 0;
177
178
179 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL)
180 #define PRINT(str) aprint_normal("%s%s", sep, str); sep = ", "
181
182 aprint_normal("%s: ", sc->sc_mii.mii_dev.dv_xname);
183 if (sc->sc_tlphycap) {
184 if (sc->sc_tlphycap & TLPHY_MEDIA_10_2) {
185 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_2, 0,
186 sc->sc_mii.mii_inst), 0);
187 PRINT("10base2");
188 } else if (sc->sc_tlphycap & TLPHY_MEDIA_10_5) {
189 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_5, 0,
190 sc->sc_mii.mii_inst), 0);
191 PRINT("10base5");
192 }
193 }
194 if (sc->sc_mii.mii_capabilities & BMSR_MEDIAMASK) {
195 aprint_normal("%s", sep);
196 mii_phy_add_media(&sc->sc_mii);
197 } else if ((sc->sc_tlphycap &
198 (TLPHY_MEDIA_10_2 | TLPHY_MEDIA_10_5)) == 0)
199 aprint_error("no media present");
200 aprint_normal("\n");
201 #undef ADD
202 #undef PRINT
203 }
204
205 static int
206 tlphy_service(struct mii_softc *self, struct mii_data *mii, 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 mii_phy_setmedia(&sc->sc_mii);
260 }
261 break;
262
263 case MII_TICK:
264 /*
265 * If we're not currently selected, just return.
266 */
267 if (IFM_INST(ife->ifm_media) != sc->sc_mii.mii_inst)
268 return (0);
269
270 /*
271 * Only used for autonegotiation.
272 */
273 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
274 return (0);
275
276 /*
277 * Is the interface even up?
278 */
279 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
280 return (0);
281
282 /*
283 * XXX WHAT ABOUT CHECKING LINK ON THE BNC/AUI?!
284 */
285
286 if (mii_phy_tick(&sc->sc_mii) == EJUSTRETURN)
287 return (0);
288 break;
289
290 case MII_DOWN:
291 mii_phy_down(&sc->sc_mii);
292 return (0);
293 }
294
295 /* Update the media status. */
296 mii_phy_status(&sc->sc_mii);
297
298 /* Callback if something changed. */
299 mii_phy_update(&sc->sc_mii, cmd);
300 return (0);
301 }
302
303 static void
304 tlphy_status(struct mii_softc *physc)
305 {
306 struct tlphy_softc *sc = (void *) physc;
307 struct mii_data *mii = sc->sc_mii.mii_pdata;
308 int bmsr, bmcr, tlctrl;
309
310 mii->mii_media_status = IFM_AVALID;
311 mii->mii_media_active = IFM_ETHER;
312
313 bmcr = PHY_READ(&sc->sc_mii, MII_BMCR);
314 if (bmcr & BMCR_ISO) {
315 mii->mii_media_active |= IFM_NONE;
316 mii->mii_media_status = 0;
317 return;
318 }
319
320 tlctrl = PHY_READ(&sc->sc_mii, MII_TLPHY_CTRL);
321 if (tlctrl & CTRL_AUISEL) {
322 if (sc->sc_tlphycap & TLPHY_MEDIA_10_2)
323 mii->mii_media_active |= IFM_10_2;
324 else if (sc->sc_tlphycap & TLPHY_MEDIA_10_5)
325 mii->mii_media_active |= IFM_10_5;
326 else
327 printf("%s: AUI selected with no matching media !\n",
328 sc->sc_mii.mii_dev.dv_xname);
329 mii->mii_media_status |= IFM_ACTIVE;
330 return;
331 }
332
333 bmsr = PHY_READ(&sc->sc_mii, MII_BMSR) |
334 PHY_READ(&sc->sc_mii, MII_BMSR);
335 if (bmsr & BMSR_LINK)
336 mii->mii_media_status |= IFM_ACTIVE;
337
338 if (bmcr & BMCR_LOOP)
339 mii->mii_media_active |= IFM_LOOP;
340
341 /*
342 * Grr, braindead ThunderLAN PHY doesn't have any way to
343 * tell which media is actually active. (Note it also
344 * doesn't self-configure after autonegotiation.) We
345 * just have to report what's in the BMCR.
346 */
347 if (bmcr & BMCR_FDX)
348 mii->mii_media_active |= IFM_FDX;
349 mii->mii_media_active |= IFM_10_T;
350 }
351
352 static int
353 tlphy_auto(struct tlphy_softc *sc, int waitfor)
354 {
355 int error;
356
357 switch ((error = mii_phy_auto(&sc->sc_mii, waitfor))) {
358 case EIO:
359 /*
360 * Just assume we're not in full-duplex mode.
361 * XXX Check link and try AUI/BNC?
362 */
363 PHY_WRITE(&sc->sc_mii, MII_BMCR, 0);
364 break;
365
366 case EJUSTRETURN:
367 /* Flag that we need to program when it completes. */
368 sc->sc_need_acomp = 1;
369 break;
370
371 default:
372 tlphy_acomp(sc);
373 }
374
375 return (error);
376 }
377
378 static void
379 tlphy_acomp(struct tlphy_softc *sc)
380 {
381 int aner, anlpar;
382
383 sc->sc_need_acomp = 0;
384
385 /*
386 * Grr, braindead ThunderLAN PHY doesn't self-configure
387 * after autonegotiation. We have to do it ourselves
388 * based on the link partner status.
389 */
390
391 aner = PHY_READ(&sc->sc_mii, MII_ANER);
392 if (aner & ANER_LPAN) {
393 anlpar = PHY_READ(&sc->sc_mii, MII_ANLPAR) &
394 PHY_READ(&sc->sc_mii, MII_ANAR);
395 if (anlpar & ANAR_10_FD) {
396 PHY_WRITE(&sc->sc_mii, MII_BMCR, BMCR_FDX);
397 return;
398 }
399 }
400 PHY_WRITE(&sc->sc_mii, MII_BMCR, 0);
401 }
402