]> CyberLeo.Net >> Repos - FreeBSD/releng/8.2.git/blob - sys/dev/mii/tlphy.c
Copy stable/8 to releng/8.2 in preparation for FreeBSD-8.2 release.
[FreeBSD/releng/8.2.git] / sys / dev / mii / tlphy.c
1 /*      $NetBSD: tlphy.c,v 1.18 1999/05/14 11:40:28 drochner Exp $      */
2
3 /*-
4  * Copyright (c) 1998, 1999 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  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 /*-
34  * Copyright (c) 1997 Manuel Bouyer.  All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  *
45  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
46  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
47  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
48  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
49  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
50  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
51  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
52  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
53  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
54  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
55  */
56
57 #include <sys/cdefs.h>
58 __FBSDID("$FreeBSD$");
59
60 /*
61  * Driver for Texas Instruments's ThunderLAN PHYs
62  */
63
64 #include <sys/param.h>
65 #include <sys/systm.h>
66 #include <sys/kernel.h>
67 #include <sys/socket.h>
68 #include <sys/errno.h>
69 #include <sys/module.h>
70 #include <sys/bus.h>
71 #include <sys/malloc.h>
72
73 #include <machine/bus.h>
74
75 #include <net/if.h>
76 #include <net/if_media.h>
77
78 #include <dev/mii/mii.h>
79 #include <dev/mii/miivar.h>
80 #include "miidevs.h"
81
82 #include <dev/mii/tlphyreg.h>
83
84 #include "miibus_if.h"
85
86 struct tlphy_softc {
87         struct mii_softc sc_mii;                /* generic PHY */
88         int sc_need_acomp;
89 };
90
91 static int tlphy_probe(device_t);
92 static int tlphy_attach(device_t);
93
94 static device_method_t tlphy_methods[] = {
95         /* device interface */
96         DEVMETHOD(device_probe,         tlphy_probe),
97         DEVMETHOD(device_attach,        tlphy_attach),
98         DEVMETHOD(device_detach,        mii_phy_detach),
99         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
100         { 0, 0 }
101 };
102
103 static devclass_t tlphy_devclass;
104
105 static driver_t tlphy_driver = {
106         "tlphy",
107         tlphy_methods,
108         sizeof(struct tlphy_softc)
109 };
110
111 DRIVER_MODULE(tlphy, miibus, tlphy_driver, tlphy_devclass, 0, 0);
112
113 static int      tlphy_service(struct mii_softc *, struct mii_data *, int);
114 static int      tlphy_auto(struct tlphy_softc *);
115 static void     tlphy_acomp(struct tlphy_softc *);
116 static void     tlphy_status(struct tlphy_softc *);
117
118 static const struct mii_phydesc tlphys[] = {
119         MII_PHY_DESC(xxTI, TLAN10T),
120         MII_PHY_END
121 };
122
123 static int
124 tlphy_probe(device_t dev)
125 {
126
127         if (strcmp(device_get_name(device_get_parent(device_get_parent(dev))),
128             "tl") != 0)
129                 return (ENXIO);
130         return (mii_phy_dev_probe(dev, tlphys, BUS_PROBE_DEFAULT));
131 }
132
133 static int
134 tlphy_attach(device_t dev)
135 {
136         device_t *devlist;
137         struct tlphy_softc *sc;
138         struct mii_softc *other;
139         struct mii_attach_args *ma;
140         struct mii_data *mii;
141         const char *sep = "";
142         int capmask, devs, i;
143
144         sc = device_get_softc(dev);
145         ma = device_get_ivars(dev);
146         sc->sc_mii.mii_dev = device_get_parent(dev);
147         mii = device_get_softc(sc->sc_mii.mii_dev);
148         LIST_INSERT_HEAD(&mii->mii_phys, &sc->sc_mii, mii_list);
149
150         sc->sc_mii.mii_flags = miibus_get_flags(dev);
151         sc->sc_mii.mii_inst = mii->mii_instance;
152         sc->sc_mii.mii_phy = ma->mii_phyno;
153         sc->sc_mii.mii_service = tlphy_service;
154         sc->sc_mii.mii_pdata = mii;
155
156         /*
157          * Note that if we're on a device that also supports 100baseTX,
158          * we are not going to want to use the built-in 10baseT port,
159          * since there will be another PHY on the MII wired up to the
160          * UTP connector.
161          */
162         capmask = BMSR_DEFCAPMASK;
163         if (mii->mii_instance &&
164             device_get_children(sc->sc_mii.mii_dev, &devlist, &devs) == 0) {
165                 for (i = 0; i < devs; i++) {
166                         if (devlist[i] != dev) {
167                                 other = device_get_softc(devlist[i]);
168                                 capmask &= ~other->mii_capabilities;
169                                 break;
170                         }
171                 }
172                 free(devlist, M_TEMP);
173         }
174
175         mii->mii_instance++;
176
177         mii_phy_reset(&sc->sc_mii);
178
179         sc->sc_mii.mii_capabilities =
180             PHY_READ(&sc->sc_mii, MII_BMSR) & capmask;
181
182 #define ADD(m, c)       ifmedia_add(&mii->mii_media, (m), (c), NULL)
183
184         ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_LOOP, sc->sc_mii.mii_inst),
185             MII_MEDIA_100_TX);
186
187 #define PRINT(s)        printf("%s%s", sep, s); sep = ", "
188
189         if ((sc->sc_mii.mii_flags & (MIIF_MACPRIV0 | MIIF_MACPRIV1)) != 0 &&
190             (sc->sc_mii.mii_capabilities & BMSR_MEDIAMASK) != 0)
191                 device_printf(dev, " ");
192         if ((sc->sc_mii.mii_flags & MIIF_MACPRIV0) != 0) {
193                 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_2, 0, sc->sc_mii.mii_inst),
194                     0);
195                 PRINT("10base2/BNC");
196         }
197         if ((sc->sc_mii.mii_flags & MIIF_MACPRIV1) != 0) {
198                 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_5, 0, sc->sc_mii.mii_inst),
199                     0);
200                 PRINT("10base5/AUI");
201         }
202         if ((sc->sc_mii.mii_capabilities & BMSR_MEDIAMASK) != 0) {
203                 printf("%s", sep);
204                 mii_phy_add_media(&sc->sc_mii);
205         }
206         if ((sc->sc_mii.mii_flags & (MIIF_MACPRIV0 | MIIF_MACPRIV1)) != 0 &&
207             (sc->sc_mii.mii_capabilities & BMSR_MEDIAMASK) != 0)
208                 printf("\n");
209 #undef ADD
210 #undef PRINT
211         MIIBUS_MEDIAINIT(sc->sc_mii.mii_dev);
212         return (0);
213 }
214
215 static int
216 tlphy_service(struct mii_softc *self, struct mii_data *mii, int cmd)
217 {
218         struct tlphy_softc *sc = (struct tlphy_softc *)self;
219         struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
220         int reg;
221
222         if (sc->sc_need_acomp)
223                 tlphy_acomp(sc);
224
225         switch (cmd) {
226         case MII_POLLSTAT:
227                 break;
228
229         case MII_MEDIACHG:
230                 /*
231                  * If the interface is not up, don't do anything.
232                  */
233                 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
234                         break;
235
236                 switch (IFM_SUBTYPE(ife->ifm_media)) {
237                 case IFM_AUTO:
238                         /*
239                          * The ThunderLAN PHY doesn't self-configure after
240                          * an autonegotiation cycle, so there's no such
241                          * thing as "already in auto mode".
242                          */
243                         (void)tlphy_auto(sc);
244                         break;
245                 case IFM_10_2:
246                 case IFM_10_5:
247                         PHY_WRITE(&sc->sc_mii, MII_BMCR, 0);
248                         PHY_WRITE(&sc->sc_mii, MII_TLPHY_CTRL, CTRL_AUISEL);
249                         DELAY(100000);
250                         break;
251                 default:
252                         PHY_WRITE(&sc->sc_mii, MII_TLPHY_CTRL, 0);
253                         DELAY(100000);
254                         mii_phy_setmedia(&sc->sc_mii);
255                 }
256                 break;
257
258         case MII_TICK:
259                 /*
260                  * Is the interface even up?
261                  */
262                 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
263                         return (0);
264
265                 /*
266                  * Only used for autonegotiation.
267                  */
268                 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
269                         break;
270
271                 /*
272                  * Check to see if we have link.  If we do, we don't
273                  * need to restart the autonegotiation process.  Read
274                  * the BMSR twice in case it's latched.
275                  *
276                  * XXX WHAT ABOUT CHECKING LINK ON THE BNC/AUI?!
277                  */
278                 reg = PHY_READ(&sc->sc_mii, MII_BMSR) |
279                     PHY_READ(&sc->sc_mii, MII_BMSR);
280                 if (reg & BMSR_LINK)
281                         break;
282
283                 /*
284                  * Only retry autonegotiation every 5 seconds.
285                  */
286                 if (++sc->sc_mii.mii_ticks <= MII_ANEGTICKS)
287                         break;
288
289                 sc->sc_mii.mii_ticks = 0;
290                 mii_phy_reset(&sc->sc_mii);
291                 (void)tlphy_auto(sc);
292                 return (0);
293         }
294
295         /* Update the media status. */
296         tlphy_status(sc);
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 tlphy_softc *sc)
305 {
306         struct mii_data *mii = sc->sc_mii.mii_pdata;
307         int bmsr, bmcr, tlctrl;
308
309         mii->mii_media_status = IFM_AVALID;
310         mii->mii_media_active = IFM_ETHER;
311
312         bmcr = PHY_READ(&sc->sc_mii, MII_BMCR);
313         if (bmcr & BMCR_ISO) {
314                 mii->mii_media_active |= IFM_NONE;
315                 mii->mii_media_status = 0;
316                 return;
317         }
318
319         tlctrl = PHY_READ(&sc->sc_mii, MII_TLPHY_CTRL);
320         if (tlctrl & CTRL_AUISEL) {
321                 mii->mii_media_status = 0;
322                 mii->mii_media_active = mii->mii_media.ifm_cur->ifm_media;
323                 return;
324         }
325
326         bmsr = PHY_READ(&sc->sc_mii, MII_BMSR) |
327             PHY_READ(&sc->sc_mii, MII_BMSR);
328         if (bmsr & BMSR_LINK)
329                 mii->mii_media_status |= IFM_ACTIVE;
330
331         if (bmcr & BMCR_LOOP)
332                 mii->mii_media_active |= IFM_LOOP;
333
334         /*
335          * Grr, braindead ThunderLAN PHY doesn't have any way to
336          * tell which media is actually active.  (Note it also
337          * doesn't self-configure after autonegotiation.)  We
338          * just have to report what's in the BMCR.
339          */
340         if (bmcr & BMCR_FDX)
341                 mii->mii_media_active |= IFM_FDX;
342         else
343                 mii->mii_media_active |= IFM_HDX;
344         mii->mii_media_active |= IFM_10_T;
345 }
346
347 static int
348 tlphy_auto(struct tlphy_softc *sc)
349 {
350         int error;
351
352         switch ((error = mii_phy_auto(&sc->sc_mii))) {
353         case EIO:
354                 /*
355                  * Just assume we're not in full-duplex mode.
356                  * XXX Check link and try AUI/BNC?
357                  */
358                 PHY_WRITE(&sc->sc_mii, MII_BMCR, 0);
359                 break;
360
361         case EJUSTRETURN:
362                 /* Flag that we need to program when it completes. */
363                 sc->sc_need_acomp = 1;
364                 break;
365
366         default:
367                 tlphy_acomp(sc);
368         }
369
370         return (error);
371 }
372
373 static void
374 tlphy_acomp(struct tlphy_softc *sc)
375 {
376         int aner, anlpar;
377
378         sc->sc_need_acomp = 0;
379
380         /*
381          * Grr, braindead ThunderLAN PHY doesn't self-configure
382          * after autonegotiation.  We have to do it ourselves
383          * based on the link partner status.
384          */
385
386         aner = PHY_READ(&sc->sc_mii, MII_ANER);
387         if (aner & ANER_LPAN) {
388                 anlpar = PHY_READ(&sc->sc_mii, MII_ANLPAR) &
389                     PHY_READ(&sc->sc_mii, MII_ANAR);
390                 if (anlpar & ANAR_10_FD) {
391                         PHY_WRITE(&sc->sc_mii, MII_BMCR, BMCR_FDX);
392                         return;
393                 }
394         }
395         PHY_WRITE(&sc->sc_mii, MII_BMCR, 0);
396 }