tlphy.c revision 1.57 1 1.57 tsutsui /* $NetBSD: tlphy.c,v 1.57 2008/05/06 11:35:38 tsutsui Exp $ */
2 1.7 thorpej
3 1.7 thorpej /*-
4 1.26 thorpej * Copyright (c) 1998, 1999, 2000 The NetBSD Foundation, Inc.
5 1.7 thorpej * All rights reserved.
6 1.7 thorpej *
7 1.7 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.7 thorpej * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 1.7 thorpej * NASA Ames Research Center.
10 1.7 thorpej *
11 1.7 thorpej * Redistribution and use in source and binary forms, with or without
12 1.7 thorpej * modification, are permitted provided that the following conditions
13 1.7 thorpej * are met:
14 1.7 thorpej * 1. Redistributions of source code must retain the above copyright
15 1.7 thorpej * notice, this list of conditions and the following disclaimer.
16 1.7 thorpej * 2. Redistributions in binary form must reproduce the above copyright
17 1.7 thorpej * notice, this list of conditions and the following disclaimer in the
18 1.7 thorpej * documentation and/or other materials provided with the distribution.
19 1.7 thorpej *
20 1.7 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 1.7 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 1.7 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 1.7 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 1.7 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 1.7 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 1.7 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 1.7 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 1.7 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 1.7 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 1.7 thorpej * POSSIBILITY OF SUCH DAMAGE.
31 1.7 thorpej */
32 1.7 thorpej
33 1.1 bouyer /*
34 1.1 bouyer * Copyright (c) 1997 Manuel Bouyer. All rights reserved.
35 1.1 bouyer *
36 1.1 bouyer * Redistribution and use in source and binary forms, with or without
37 1.1 bouyer * modification, are permitted provided that the following conditions
38 1.1 bouyer * are met:
39 1.1 bouyer * 1. Redistributions of source code must retain the above copyright
40 1.1 bouyer * notice, this list of conditions and the following disclaimer.
41 1.1 bouyer * 2. Redistributions in binary form must reproduce the above copyright
42 1.1 bouyer * notice, this list of conditions and the following disclaimer in the
43 1.1 bouyer * documentation and/or other materials provided with the distribution.
44 1.1 bouyer * 3. All advertising materials mentioning features or use of this software
45 1.1 bouyer * must display the following acknowledgement:
46 1.4 thorpej * This product includes software developed by Manuel Bouyer.
47 1.1 bouyer * 4. The name of the author may not be used to endorse or promote products
48 1.1 bouyer * derived from this software without specific prior written permission.
49 1.1 bouyer *
50 1.1 bouyer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
51 1.1 bouyer * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
52 1.1 bouyer * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
53 1.1 bouyer * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
54 1.1 bouyer * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
55 1.1 bouyer * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
56 1.1 bouyer * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
57 1.1 bouyer * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
58 1.1 bouyer * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
59 1.1 bouyer * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
60 1.1 bouyer */
61 1.1 bouyer
62 1.7 thorpej /*
63 1.7 thorpej * Driver for Texas Instruments's ThunderLAN PHYs
64 1.7 thorpej */
65 1.33 lukem
66 1.33 lukem #include <sys/cdefs.h>
67 1.57 tsutsui __KERNEL_RCSID(0, "$NetBSD: tlphy.c,v 1.57 2008/05/06 11:35:38 tsutsui Exp $");
68 1.1 bouyer
69 1.1 bouyer #include <sys/param.h>
70 1.1 bouyer #include <sys/systm.h>
71 1.1 bouyer #include <sys/kernel.h>
72 1.1 bouyer #include <sys/device.h>
73 1.1 bouyer #include <sys/socket.h>
74 1.17 thorpej #include <sys/errno.h>
75 1.7 thorpej
76 1.51 ad #include <sys/bus.h>
77 1.7 thorpej
78 1.1 bouyer #include <net/if.h>
79 1.1 bouyer #include <net/if_media.h>
80 1.1 bouyer
81 1.7 thorpej #include <net/if_ether.h>
82 1.1 bouyer
83 1.7 thorpej #include <dev/mii/mii.h>
84 1.7 thorpej #include <dev/mii/miivar.h>
85 1.7 thorpej #include <dev/mii/miidevs.h>
86 1.7 thorpej
87 1.7 thorpej #include <dev/mii/tlphyreg.h>
88 1.7 thorpej #include <dev/mii/tlphyvar.h>
89 1.7 thorpej
90 1.7 thorpej /* ThunderLAN PHY can only be on a ThunderLAN */
91 1.7 thorpej #include <dev/pci/if_tlvar.h>
92 1.7 thorpej
93 1.7 thorpej struct tlphy_softc {
94 1.7 thorpej struct mii_softc sc_mii; /* generic PHY */
95 1.7 thorpej int sc_tlphycap;
96 1.17 thorpej int sc_need_acomp;
97 1.7 thorpej };
98 1.1 bouyer
99 1.56 xtraeme static int tlphymatch(device_t, cfdata_t, void *);
100 1.56 xtraeme static void tlphyattach(device_t, device_t, void *);
101 1.1 bouyer
102 1.56 xtraeme CFATTACH_DECL_NEW(tlphy, sizeof(struct tlphy_softc),
103 1.37 thorpej tlphymatch, tlphyattach, mii_phy_detach, mii_phy_activate);
104 1.1 bouyer
105 1.41 thorpej static int tlphy_service(struct mii_softc *, struct mii_data *, int);
106 1.41 thorpej static int tlphy_auto(struct tlphy_softc *, int);
107 1.41 thorpej static void tlphy_acomp(struct tlphy_softc *);
108 1.41 thorpej static void tlphy_status(struct mii_softc *);
109 1.7 thorpej
110 1.41 thorpej static const struct mii_phy_funcs tlphy_funcs = {
111 1.26 thorpej tlphy_service, tlphy_status, mii_phy_reset,
112 1.26 thorpej };
113 1.26 thorpej
114 1.41 thorpej static const struct mii_phydesc tlphys[] = {
115 1.30 thorpej { MII_OUI_TI, MII_MODEL_TI_TLAN10T,
116 1.30 thorpej MII_STR_TI_TLAN10T },
117 1.30 thorpej
118 1.30 thorpej { 0, 0,
119 1.30 thorpej NULL },
120 1.30 thorpej };
121 1.30 thorpej
122 1.41 thorpej static int
123 1.56 xtraeme tlphymatch(device_t parent, cfdata_t match, void *aux)
124 1.1 bouyer {
125 1.42 perry struct mii_attach_args *ma = aux;
126 1.1 bouyer
127 1.30 thorpej if (mii_phy_match(ma, tlphys) != NULL)
128 1.15 thorpej return (10);
129 1.7 thorpej
130 1.4 thorpej return (0);
131 1.1 bouyer }
132 1.1 bouyer
133 1.41 thorpej static void
134 1.56 xtraeme tlphyattach(device_t parent, device_t self, void *aux)
135 1.1 bouyer {
136 1.57 tsutsui struct tlphy_softc *tsc = device_private(self);
137 1.57 tsutsui struct mii_softc *sc = &tsc->sc_mii;
138 1.46 thorpej struct tl_softc *tlsc = device_private(device_parent(self));
139 1.7 thorpej struct mii_attach_args *ma = aux;
140 1.7 thorpej struct mii_data *mii = ma->mii_data;
141 1.30 thorpej const struct mii_phydesc *mpd;
142 1.7 thorpej const char *sep = "";
143 1.7 thorpej
144 1.30 thorpej mpd = mii_phy_match(ma, tlphys);
145 1.39 thorpej aprint_naive(": Media interface\n");
146 1.39 thorpej aprint_normal(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2));
147 1.7 thorpej
148 1.57 tsutsui sc->mii_dev = self;
149 1.57 tsutsui sc->mii_inst = mii->mii_instance;
150 1.57 tsutsui sc->mii_phy = ma->mii_phyno;
151 1.57 tsutsui sc->mii_funcs = &tlphy_funcs;
152 1.57 tsutsui sc->mii_pdata = mii;
153 1.57 tsutsui sc->mii_flags = ma->mii_flags;
154 1.57 tsutsui sc->mii_anegticks = MII_ANEGTICKS;
155 1.7 thorpej
156 1.57 tsutsui PHY_RESET(sc);
157 1.7 thorpej
158 1.7 thorpej /*
159 1.7 thorpej * Note that if we're on a device that also supports 100baseTX,
160 1.7 thorpej * we are not going to want to use the built-in 10baseT port,
161 1.7 thorpej * since there will be another PHY on the MII wired up to the
162 1.7 thorpej * UTP connector. The parent indicates this to us by specifying
163 1.7 thorpej * the TLPHY_MEDIA_NO_10_T bit.
164 1.7 thorpej */
165 1.57 tsutsui tsc->sc_tlphycap = tlsc->tl_product->tp_tlphymedia;
166 1.57 tsutsui if ((tsc->sc_tlphycap & TLPHY_MEDIA_NO_10_T) == 0)
167 1.57 tsutsui sc->mii_capabilities =
168 1.57 tsutsui PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
169 1.7 thorpej else
170 1.57 tsutsui sc->mii_capabilities = 0;
171 1.7 thorpej
172 1.19 thorpej
173 1.7 thorpej #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL)
174 1.39 thorpej #define PRINT(str) aprint_normal("%s%s", sep, str); sep = ", "
175 1.7 thorpej
176 1.56 xtraeme aprint_normal_dev(self, "");
177 1.57 tsutsui if (tsc->sc_tlphycap) {
178 1.57 tsutsui if (tsc->sc_tlphycap & TLPHY_MEDIA_10_2) {
179 1.7 thorpej ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_2, 0,
180 1.57 tsutsui sc->mii_inst), 0);
181 1.19 thorpej PRINT("10base2");
182 1.57 tsutsui } else if (tsc->sc_tlphycap & TLPHY_MEDIA_10_5) {
183 1.7 thorpej ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_5, 0,
184 1.57 tsutsui sc->mii_inst), 0);
185 1.19 thorpej PRINT("10base5");
186 1.7 thorpej }
187 1.7 thorpej }
188 1.57 tsutsui if (sc->mii_capabilities & BMSR_MEDIAMASK) {
189 1.39 thorpej aprint_normal("%s", sep);
190 1.57 tsutsui mii_phy_add_media(sc);
191 1.57 tsutsui } else if ((tsc->sc_tlphycap &
192 1.19 thorpej (TLPHY_MEDIA_10_2 | TLPHY_MEDIA_10_5)) == 0)
193 1.39 thorpej aprint_error("no media present");
194 1.39 thorpej aprint_normal("\n");
195 1.7 thorpej #undef ADD
196 1.7 thorpej #undef PRINT
197 1.52 jmcneill
198 1.52 jmcneill if (!pmf_device_register(self, NULL, mii_phy_resume))
199 1.52 jmcneill aprint_error_dev(self, "couldn't establish power handler\n");
200 1.7 thorpej }
201 1.7 thorpej
202 1.41 thorpej static int
203 1.57 tsutsui tlphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
204 1.7 thorpej {
205 1.57 tsutsui struct tlphy_softc *tsc = (struct tlphy_softc *)sc;
206 1.11 thorpej struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
207 1.7 thorpej int reg;
208 1.21 thorpej
209 1.57 tsutsui if ((sc->mii_flags & MIIF_DOINGAUTO) == 0 && tsc->sc_need_acomp)
210 1.57 tsutsui tlphy_acomp(tsc);
211 1.17 thorpej
212 1.7 thorpej switch (cmd) {
213 1.7 thorpej case MII_POLLSTAT:
214 1.7 thorpej /*
215 1.7 thorpej * If we're not polling our PHY instance, just return.
216 1.7 thorpej */
217 1.57 tsutsui if (IFM_INST(ife->ifm_media) != sc->mii_inst)
218 1.7 thorpej return (0);
219 1.1 bouyer break;
220 1.7 thorpej
221 1.7 thorpej case MII_MEDIACHG:
222 1.7 thorpej /*
223 1.7 thorpej * If the media indicates a different PHY instance,
224 1.7 thorpej * isolate ourselves.
225 1.7 thorpej */
226 1.57 tsutsui if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
227 1.57 tsutsui reg = PHY_READ(sc, MII_BMCR);
228 1.57 tsutsui PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
229 1.7 thorpej return (0);
230 1.7 thorpej }
231 1.42 perry
232 1.7 thorpej /*
233 1.7 thorpej * If the interface is not up, don't do anything.
234 1.7 thorpej */
235 1.7 thorpej if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
236 1.7 thorpej break;
237 1.7 thorpej
238 1.11 thorpej switch (IFM_SUBTYPE(ife->ifm_media)) {
239 1.7 thorpej case IFM_AUTO:
240 1.7 thorpej /*
241 1.7 thorpej * The ThunderLAN PHY doesn't self-configure after
242 1.7 thorpej * an autonegotiation cycle, so there's no such
243 1.7 thorpej * thing as "already in auto mode".
244 1.7 thorpej */
245 1.57 tsutsui (void) tlphy_auto(tsc, 1);
246 1.7 thorpej break;
247 1.7 thorpej case IFM_10_2:
248 1.7 thorpej case IFM_10_5:
249 1.57 tsutsui PHY_WRITE(sc, MII_BMCR, 0);
250 1.57 tsutsui PHY_WRITE(sc, MII_TLPHY_CTRL, CTRL_AUISEL);
251 1.7 thorpej delay(100000);
252 1.7 thorpej break;
253 1.7 thorpej default:
254 1.57 tsutsui PHY_WRITE(sc, MII_TLPHY_CTRL, 0);
255 1.7 thorpej delay(100000);
256 1.57 tsutsui mii_phy_setmedia(sc);
257 1.7 thorpej }
258 1.1 bouyer break;
259 1.7 thorpej
260 1.7 thorpej case MII_TICK:
261 1.7 thorpej /*
262 1.7 thorpej * If we're not currently selected, just return.
263 1.7 thorpej */
264 1.57 tsutsui if (IFM_INST(ife->ifm_media) != sc->mii_inst)
265 1.7 thorpej return (0);
266 1.7 thorpej
267 1.7 thorpej /*
268 1.7 thorpej * Only used for autonegotiation.
269 1.7 thorpej */
270 1.11 thorpej if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
271 1.7 thorpej return (0);
272 1.7 thorpej
273 1.7 thorpej /*
274 1.7 thorpej * Is the interface even up?
275 1.7 thorpej */
276 1.7 thorpej if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
277 1.7 thorpej return (0);
278 1.7 thorpej
279 1.7 thorpej /*
280 1.7 thorpej * XXX WHAT ABOUT CHECKING LINK ON THE BNC/AUI?!
281 1.7 thorpej */
282 1.7 thorpej
283 1.57 tsutsui if (mii_phy_tick(sc) == EJUSTRETURN)
284 1.17 thorpej return (0);
285 1.3 christos break;
286 1.20 thorpej
287 1.20 thorpej case MII_DOWN:
288 1.57 tsutsui mii_phy_down(sc);
289 1.20 thorpej return (0);
290 1.1 bouyer }
291 1.1 bouyer
292 1.7 thorpej /* Update the media status. */
293 1.57 tsutsui mii_phy_status(sc);
294 1.7 thorpej
295 1.7 thorpej /* Callback if something changed. */
296 1.57 tsutsui mii_phy_update(sc, cmd);
297 1.7 thorpej return (0);
298 1.1 bouyer }
299 1.1 bouyer
300 1.41 thorpej static void
301 1.57 tsutsui tlphy_status(struct mii_softc *sc)
302 1.1 bouyer {
303 1.57 tsutsui struct tlphy_softc *tsc = (struct tlphy_softc *)sc;
304 1.57 tsutsui struct mii_data *mii = sc->mii_pdata;
305 1.7 thorpej int bmsr, bmcr, tlctrl;
306 1.4 thorpej
307 1.7 thorpej mii->mii_media_status = IFM_AVALID;
308 1.7 thorpej mii->mii_media_active = IFM_ETHER;
309 1.1 bouyer
310 1.57 tsutsui bmcr = PHY_READ(sc, MII_BMCR);
311 1.7 thorpej if (bmcr & BMCR_ISO) {
312 1.7 thorpej mii->mii_media_active |= IFM_NONE;
313 1.42 perry mii->mii_media_status = 0;
314 1.7 thorpej return;
315 1.1 bouyer }
316 1.1 bouyer
317 1.57 tsutsui tlctrl = PHY_READ(sc, MII_TLPHY_CTRL);
318 1.7 thorpej if (tlctrl & CTRL_AUISEL) {
319 1.57 tsutsui if (tsc->sc_tlphycap & TLPHY_MEDIA_10_2)
320 1.10 bouyer mii->mii_media_active |= IFM_10_2;
321 1.57 tsutsui else if (tsc->sc_tlphycap & TLPHY_MEDIA_10_5)
322 1.10 bouyer mii->mii_media_active |= IFM_10_5;
323 1.1 bouyer else
324 1.10 bouyer printf("%s: AUI selected with no matching media !\n",
325 1.57 tsutsui device_xname(sc->mii_dev));
326 1.31 bouyer mii->mii_media_status |= IFM_ACTIVE;
327 1.7 thorpej return;
328 1.1 bouyer }
329 1.10 bouyer
330 1.57 tsutsui bmsr = PHY_READ(sc, MII_BMSR) |
331 1.57 tsutsui PHY_READ(sc, MII_BMSR);
332 1.42 perry if (bmsr & BMSR_LINK)
333 1.10 bouyer mii->mii_media_status |= IFM_ACTIVE;
334 1.7 thorpej
335 1.7 thorpej if (bmcr & BMCR_LOOP)
336 1.7 thorpej mii->mii_media_active |= IFM_LOOP;
337 1.7 thorpej
338 1.7 thorpej /*
339 1.7 thorpej * Grr, braindead ThunderLAN PHY doesn't have any way to
340 1.7 thorpej * tell which media is actually active. (Note it also
341 1.7 thorpej * doesn't self-configure after autonegotiation.) We
342 1.7 thorpej * just have to report what's in the BMCR.
343 1.7 thorpej */
344 1.7 thorpej if (bmcr & BMCR_FDX)
345 1.7 thorpej mii->mii_media_active |= IFM_FDX;
346 1.7 thorpej mii->mii_media_active |= IFM_10_T;
347 1.1 bouyer }
348 1.1 bouyer
349 1.41 thorpej static int
350 1.57 tsutsui tlphy_auto(struct tlphy_softc *tsc, int waitfor)
351 1.17 thorpej {
352 1.57 tsutsui struct mii_softc *sc = &tsc->sc_mii;
353 1.17 thorpej int error;
354 1.17 thorpej
355 1.57 tsutsui switch ((error = mii_phy_auto(sc, waitfor))) {
356 1.17 thorpej case EIO:
357 1.17 thorpej /*
358 1.17 thorpej * Just assume we're not in full-duplex mode.
359 1.17 thorpej * XXX Check link and try AUI/BNC?
360 1.17 thorpej */
361 1.57 tsutsui PHY_WRITE(sc, MII_BMCR, 0);
362 1.17 thorpej break;
363 1.17 thorpej
364 1.17 thorpej case EJUSTRETURN:
365 1.17 thorpej /* Flag that we need to program when it completes. */
366 1.57 tsutsui tsc->sc_need_acomp = 1;
367 1.17 thorpej break;
368 1.17 thorpej
369 1.17 thorpej default:
370 1.57 tsutsui tlphy_acomp(tsc);
371 1.17 thorpej }
372 1.17 thorpej
373 1.17 thorpej return (error);
374 1.17 thorpej }
375 1.17 thorpej
376 1.41 thorpej static void
377 1.57 tsutsui tlphy_acomp(struct tlphy_softc *tsc)
378 1.1 bouyer {
379 1.57 tsutsui struct mii_softc *sc = &tsc->sc_mii;
380 1.13 thorpej int aner, anlpar;
381 1.1 bouyer
382 1.57 tsutsui tsc->sc_need_acomp = 0;
383 1.7 thorpej
384 1.7 thorpej /*
385 1.7 thorpej * Grr, braindead ThunderLAN PHY doesn't self-configure
386 1.7 thorpej * after autonegotiation. We have to do it ourselves
387 1.7 thorpej * based on the link partner status.
388 1.7 thorpej */
389 1.7 thorpej
390 1.57 tsutsui aner = PHY_READ(sc, MII_ANER);
391 1.7 thorpej if (aner & ANER_LPAN) {
392 1.57 tsutsui anlpar = PHY_READ(sc, MII_ANLPAR) &
393 1.57 tsutsui PHY_READ(sc, MII_ANAR);
394 1.7 thorpej if (anlpar & ANAR_10_FD) {
395 1.57 tsutsui PHY_WRITE(sc, MII_BMCR, BMCR_FDX);
396 1.7 thorpej return;
397 1.7 thorpej }
398 1.7 thorpej }
399 1.57 tsutsui PHY_WRITE(sc, MII_BMCR, 0);
400 1.1 bouyer }
401