tlphy.c revision 1.50.22.3 1 /* $NetBSD: tlphy.c,v 1.50.22.3 2007/12/08 16:21:20 jmcneill 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.50.22.3 2007/12/08 16:21:20 jmcneill 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 if (!pmf_device_register(self, NULL, mii_phy_resume))
205 aprint_error_dev(self, "couldn't establish power handler\n");
206 }
207
208 static int
209 tlphy_service(struct mii_softc *self, struct mii_data *mii, int cmd)
210 {
211 struct tlphy_softc *sc = (struct tlphy_softc *) self;
212 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
213 int reg;
214
215 if (!device_is_active(&sc->sc_mii.mii_dev))
216 return (ENXIO);
217
218 if ((sc->sc_mii.mii_flags & MIIF_DOINGAUTO) == 0 && sc->sc_need_acomp)
219 tlphy_acomp(sc);
220
221 switch (cmd) {
222 case MII_POLLSTAT:
223 /*
224 * If we're not polling our PHY instance, just return.
225 */
226 if (IFM_INST(ife->ifm_media) != sc->sc_mii.mii_inst)
227 return (0);
228 break;
229
230 case MII_MEDIACHG:
231 /*
232 * If the media indicates a different PHY instance,
233 * isolate ourselves.
234 */
235 if (IFM_INST(ife->ifm_media) != sc->sc_mii.mii_inst) {
236 reg = PHY_READ(&sc->sc_mii, MII_BMCR);
237 PHY_WRITE(&sc->sc_mii, MII_BMCR, reg | BMCR_ISO);
238 return (0);
239 }
240
241 /*
242 * If the interface is not up, don't do anything.
243 */
244 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
245 break;
246
247 switch (IFM_SUBTYPE(ife->ifm_media)) {
248 case IFM_AUTO:
249 /*
250 * The ThunderLAN PHY doesn't self-configure after
251 * an autonegotiation cycle, so there's no such
252 * thing as "already in auto mode".
253 */
254 (void) tlphy_auto(sc, 1);
255 break;
256 case IFM_10_2:
257 case IFM_10_5:
258 PHY_WRITE(&sc->sc_mii, MII_BMCR, 0);
259 PHY_WRITE(&sc->sc_mii, MII_TLPHY_CTRL, CTRL_AUISEL);
260 delay(100000);
261 break;
262 default:
263 PHY_WRITE(&sc->sc_mii, MII_TLPHY_CTRL, 0);
264 delay(100000);
265 mii_phy_setmedia(&sc->sc_mii);
266 }
267 break;
268
269 case MII_TICK:
270 /*
271 * If we're not currently selected, just return.
272 */
273 if (IFM_INST(ife->ifm_media) != sc->sc_mii.mii_inst)
274 return (0);
275
276 /*
277 * Only used for autonegotiation.
278 */
279 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
280 return (0);
281
282 /*
283 * Is the interface even up?
284 */
285 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
286 return (0);
287
288 /*
289 * XXX WHAT ABOUT CHECKING LINK ON THE BNC/AUI?!
290 */
291
292 if (mii_phy_tick(&sc->sc_mii) == EJUSTRETURN)
293 return (0);
294 break;
295
296 case MII_DOWN:
297 mii_phy_down(&sc->sc_mii);
298 return (0);
299 }
300
301 /* Update the media status. */
302 mii_phy_status(&sc->sc_mii);
303
304 /* Callback if something changed. */
305 mii_phy_update(&sc->sc_mii, cmd);
306 return (0);
307 }
308
309 static void
310 tlphy_status(struct mii_softc *physc)
311 {
312 struct tlphy_softc *sc = (void *) physc;
313 struct mii_data *mii = sc->sc_mii.mii_pdata;
314 int bmsr, bmcr, tlctrl;
315
316 mii->mii_media_status = IFM_AVALID;
317 mii->mii_media_active = IFM_ETHER;
318
319 bmcr = PHY_READ(&sc->sc_mii, MII_BMCR);
320 if (bmcr & BMCR_ISO) {
321 mii->mii_media_active |= IFM_NONE;
322 mii->mii_media_status = 0;
323 return;
324 }
325
326 tlctrl = PHY_READ(&sc->sc_mii, MII_TLPHY_CTRL);
327 if (tlctrl & CTRL_AUISEL) {
328 if (sc->sc_tlphycap & TLPHY_MEDIA_10_2)
329 mii->mii_media_active |= IFM_10_2;
330 else if (sc->sc_tlphycap & TLPHY_MEDIA_10_5)
331 mii->mii_media_active |= IFM_10_5;
332 else
333 printf("%s: AUI selected with no matching media !\n",
334 sc->sc_mii.mii_dev.dv_xname);
335 mii->mii_media_status |= IFM_ACTIVE;
336 return;
337 }
338
339 bmsr = PHY_READ(&sc->sc_mii, MII_BMSR) |
340 PHY_READ(&sc->sc_mii, MII_BMSR);
341 if (bmsr & BMSR_LINK)
342 mii->mii_media_status |= IFM_ACTIVE;
343
344 if (bmcr & BMCR_LOOP)
345 mii->mii_media_active |= IFM_LOOP;
346
347 /*
348 * Grr, braindead ThunderLAN PHY doesn't have any way to
349 * tell which media is actually active. (Note it also
350 * doesn't self-configure after autonegotiation.) We
351 * just have to report what's in the BMCR.
352 */
353 if (bmcr & BMCR_FDX)
354 mii->mii_media_active |= IFM_FDX;
355 mii->mii_media_active |= IFM_10_T;
356 }
357
358 static int
359 tlphy_auto(struct tlphy_softc *sc, 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 static void
385 tlphy_acomp(struct tlphy_softc *sc)
386 {
387 int aner, anlpar;
388
389 sc->sc_need_acomp = 0;
390
391 /*
392 * Grr, braindead ThunderLAN PHY doesn't self-configure
393 * after autonegotiation. We have to do it ourselves
394 * based on the link partner status.
395 */
396
397 aner = PHY_READ(&sc->sc_mii, MII_ANER);
398 if (aner & ANER_LPAN) {
399 anlpar = PHY_READ(&sc->sc_mii, MII_ANLPAR) &
400 PHY_READ(&sc->sc_mii, MII_ANAR);
401 if (anlpar & ANAR_10_FD) {
402 PHY_WRITE(&sc->sc_mii, MII_BMCR, BMCR_FDX);
403 return;
404 }
405 }
406 PHY_WRITE(&sc->sc_mii, MII_BMCR, 0);
407 }
408