]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/mii/tlphy.c
This commit was generated by cvs2svn to compensate for changes in r155094,
[FreeBSD/FreeBSD.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  * 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 #include <sys/cdefs.h>
70 __FBSDID("$FreeBSD$");
71
72 /*
73  * Driver for Texas Instruments's ThunderLAN PHYs
74  */
75
76 #include <sys/param.h>
77 #include <sys/systm.h>
78 #include <sys/kernel.h>
79 #include <sys/socket.h>
80 #include <sys/errno.h>
81 #include <sys/module.h>
82 #include <sys/bus.h>
83 #include <sys/malloc.h>
84
85 #include <machine/bus.h>
86
87 #include <net/if.h>
88 #include <net/if_media.h>
89
90 #include <dev/mii/mii.h>
91 #include <dev/mii/miivar.h>
92 #include "miidevs.h"
93
94 #include <dev/mii/tlphyreg.h>
95
96 #include "miibus_if.h"
97
98 struct tlphy_softc {
99         struct mii_softc sc_mii;                /* generic PHY */
100         int sc_need_acomp;
101 };
102
103 static int tlphy_probe(device_t);
104 static int tlphy_attach(device_t);
105
106 static device_method_t tlphy_methods[] = {
107         /* device interface */
108         DEVMETHOD(device_probe,         tlphy_probe),
109         DEVMETHOD(device_attach,        tlphy_attach),
110         DEVMETHOD(device_detach,        mii_phy_detach),
111         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
112         { 0, 0 }
113 };
114
115 static devclass_t tlphy_devclass;
116
117 static driver_t tlphy_driver = {
118         "tlphy",
119         tlphy_methods,
120         sizeof(struct tlphy_softc)
121 };
122
123 DRIVER_MODULE(tlphy, miibus, tlphy_driver, tlphy_devclass, 0, 0);
124
125 static int      tlphy_service(struct mii_softc *, struct mii_data *, int);
126 static int      tlphy_auto(struct tlphy_softc *);
127 static void     tlphy_acomp(struct tlphy_softc *);
128 static void     tlphy_status(struct tlphy_softc *);
129
130 static int
131 tlphy_probe(device_t dev)
132 {
133         struct mii_attach_args *ma;       
134
135         ma = device_get_ivars(dev);
136
137         if (MII_OUI(ma->mii_id1, ma->mii_id2) != MII_OUI_xxTI ||
138             MII_MODEL(ma->mii_id2) != MII_MODEL_xxTI_TLAN10T)
139                 return (ENXIO);
140
141         device_set_desc(dev, MII_STR_xxTI_TLAN10T);
142
143         return (0);
144 }
145
146 static int
147 tlphy_attach(device_t dev)
148 {
149         struct tlphy_softc *sc;
150         struct mii_attach_args *ma;
151         struct mii_data *mii;
152         const char *sep = "";
153         int capmask = 0xFFFFFFFF;
154
155         sc = device_get_softc(dev);
156         ma = device_get_ivars(dev);
157         sc->sc_mii.mii_dev = device_get_parent(dev);
158         mii = device_get_softc(sc->sc_mii.mii_dev);
159         LIST_INSERT_HEAD(&mii->mii_phys, &sc->sc_mii, mii_list);
160
161         sc->sc_mii.mii_inst = mii->mii_instance;
162         sc->sc_mii.mii_phy = ma->mii_phyno;
163         sc->sc_mii.mii_service = tlphy_service;
164         sc->sc_mii.mii_pdata = mii;
165
166         if (mii->mii_instance) {
167                 struct mii_softc        *other;
168                 device_t                *devlist;
169                 int                     devs, i;
170
171                 device_get_children(sc->sc_mii.mii_dev, &devlist, &devs);
172                 for (i = 0; i < devs; i++) {
173                         if (strcmp(device_get_name(devlist[i]), "tlphy")) {
174                                 other = device_get_softc(devlist[i]);
175                                 capmask &= ~other->mii_capabilities;
176                                 break;
177                         }
178                 }
179                 free(devlist, M_TEMP);
180         }
181
182         mii->mii_instance++;
183
184         sc->sc_mii.mii_flags &= ~MIIF_NOISOLATE;
185         mii_phy_reset(&sc->sc_mii);
186         sc->sc_mii.mii_flags |= MIIF_NOISOLATE;
187
188         /*
189          * Note that if we're on a device that also supports 100baseTX,
190          * we are not going to want to use the built-in 10baseT port,
191          * since there will be another PHY on the MII wired up to the
192          * UTP connector.  The parent indicates this to us by specifying
193          * the TLPHY_MEDIA_NO_10_T bit.
194          */
195         sc->sc_mii.mii_capabilities =
196             PHY_READ(&sc->sc_mii, MII_BMSR) & capmask /*ma->mii_capmask*/;
197
198 #define ADD(m, c)       ifmedia_add(&mii->mii_media, (m), (c), NULL)
199
200         ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->sc_mii.mii_inst),
201             BMCR_ISO);
202
203         ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_LOOP,
204             sc->sc_mii.mii_inst), BMCR_LOOP);
205
206 #define PRINT(s)        printf("%s%s", sep, s); sep = ", "
207
208         device_printf(dev, " ");
209         ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_2, 0, sc->sc_mii.mii_inst), 0);
210         PRINT("10base2/BNC");
211         ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_5, 0, sc->sc_mii.mii_inst), 0);
212         PRINT("10base5/AUI");
213
214         if (sc->sc_mii.mii_capabilities & BMSR_MEDIAMASK) {
215                 printf("%s", sep);
216                 mii_add_media(&sc->sc_mii);
217         }
218
219         printf("\n");
220 #undef ADD
221 #undef PRINT
222         MIIBUS_MEDIAINIT(sc->sc_mii.mii_dev);
223         return(0);
224 }
225
226 static int
227 tlphy_service(struct mii_softc *self, struct mii_data *mii, int cmd)
228 {
229         struct tlphy_softc *sc = (struct tlphy_softc *)self;
230         struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
231         int reg;
232
233         if (sc->sc_need_acomp)
234                 tlphy_acomp(sc);
235
236         switch (cmd) {
237         case MII_POLLSTAT:
238                 /*
239                  * If we're not polling our PHY instance, just return.
240                  */
241                 if (IFM_INST(ife->ifm_media) != sc->sc_mii.mii_inst)
242                         return (0);
243                 break;
244
245         case MII_MEDIACHG:
246                 /*
247                  * If the media indicates a different PHY instance,
248                  * isolate ourselves.
249                  */
250                 if (IFM_INST(ife->ifm_media) != sc->sc_mii.mii_inst) {
251                         reg = PHY_READ(&sc->sc_mii, MII_BMCR);
252                         PHY_WRITE(&sc->sc_mii, MII_BMCR, reg | BMCR_ISO);
253                         return (0);
254                 }
255                 
256                 /*
257                  * If the interface is not up, don't do anything.
258                  */
259                 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
260                         break;
261
262                 switch (IFM_SUBTYPE(ife->ifm_media)) {
263                 case IFM_AUTO:
264                         /*
265                          * The ThunderLAN PHY doesn't self-configure after
266                          * an autonegotiation cycle, so there's no such
267                          * thing as "already in auto mode".
268                          */
269                         (void) tlphy_auto(sc);
270                         break;
271                 case IFM_10_2:
272                 case IFM_10_5:
273                         PHY_WRITE(&sc->sc_mii, MII_BMCR, 0);
274                         PHY_WRITE(&sc->sc_mii, MII_TLPHY_CTRL, CTRL_AUISEL);
275                         DELAY(100000);
276                         break;
277                 default:
278                         PHY_WRITE(&sc->sc_mii, MII_TLPHY_CTRL, 0);
279                         DELAY(100000);
280                         PHY_WRITE(&sc->sc_mii, MII_ANAR,
281                             mii_anar(ife->ifm_media));
282                         PHY_WRITE(&sc->sc_mii, MII_BMCR, ife->ifm_data);
283                 }
284                 break;
285
286         case MII_TICK:
287                 /*
288                  * If we're not currently selected, just return.
289                  */
290                 if (IFM_INST(ife->ifm_media) != sc->sc_mii.mii_inst)
291                         return (0);
292
293                 /*
294                  * Is the interface even up?
295                  */
296                 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
297                         return (0);
298
299                 /*
300                  * Only used for autonegotiation.
301                  */
302                 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
303                         break;
304
305                 /*
306                  * Check to see if we have link.  If we do, we don't
307                  * need to restart the autonegotiation process.  Read
308                  * the BMSR twice in case it's latched.
309                  *
310                  * XXX WHAT ABOUT CHECKING LINK ON THE BNC/AUI?!
311                  */
312                 reg = PHY_READ(&sc->sc_mii, MII_BMSR) |
313                     PHY_READ(&sc->sc_mii, MII_BMSR);
314                 if (reg & BMSR_LINK)
315                         break;
316
317                 /*
318                  * Only retry autonegotiation every 5 seconds.
319                  */
320                 if (++sc->sc_mii.mii_ticks <= 5)
321                         break;
322
323                 sc->sc_mii.mii_ticks = 0;
324                 mii_phy_reset(&sc->sc_mii);
325                 tlphy_auto(sc);
326                 return (0);
327         }
328
329         /* Update the media status. */
330         tlphy_status(sc);
331
332         /* Callback if something changed. */
333         mii_phy_update(&sc->sc_mii, cmd);
334         return (0);
335 }
336
337 static void
338 tlphy_status(struct tlphy_softc *sc)
339 {
340         struct mii_data *mii = sc->sc_mii.mii_pdata;
341         int bmsr, bmcr, tlctrl;
342
343         mii->mii_media_status = IFM_AVALID;
344         mii->mii_media_active = IFM_ETHER;
345
346         bmcr = PHY_READ(&sc->sc_mii, MII_BMCR);
347         if (bmcr & BMCR_ISO) {
348                 mii->mii_media_active |= IFM_NONE;
349                 mii->mii_media_status = 0;  
350                 return;
351         }
352
353         tlctrl = PHY_READ(&sc->sc_mii, MII_TLPHY_CTRL);
354         if (tlctrl & CTRL_AUISEL) {
355                 mii->mii_media_status = 0;
356                 mii->mii_media_active = mii->mii_media.ifm_cur->ifm_media;
357                 return;
358         }
359
360         bmsr = PHY_READ(&sc->sc_mii, MII_BMSR) |
361             PHY_READ(&sc->sc_mii, MII_BMSR);
362         if (bmsr & BMSR_LINK)   
363                 mii->mii_media_status |= IFM_ACTIVE;
364
365         if (bmcr & BMCR_LOOP)
366                 mii->mii_media_active |= IFM_LOOP;
367
368         /*
369          * Grr, braindead ThunderLAN PHY doesn't have any way to
370          * tell which media is actually active.  (Note it also
371          * doesn't self-configure after autonegotiation.)  We
372          * just have to report what's in the BMCR.
373          */
374         if (bmcr & BMCR_FDX)
375                 mii->mii_media_active |= IFM_FDX;
376         mii->mii_media_active |= IFM_10_T;
377 }
378
379 static int
380 tlphy_auto(struct tlphy_softc *sc)
381 {
382         int error;
383
384         switch ((error = mii_phy_auto(&sc->sc_mii))) {
385         case EIO:
386                 /*
387                  * Just assume we're not in full-duplex mode.
388                  * XXX Check link and try AUI/BNC?
389                  */
390                 PHY_WRITE(&sc->sc_mii, MII_BMCR, 0);
391                 break;
392
393         case EJUSTRETURN:
394                 /* Flag that we need to program when it completes. */
395                 sc->sc_need_acomp = 1;
396                 break;
397
398         default:
399                 tlphy_acomp(sc);
400         }
401
402         return (error);
403 }
404
405 static void
406 tlphy_acomp(struct tlphy_softc *sc)
407 {
408         int aner, anlpar;
409
410         sc->sc_need_acomp = 0;
411
412         /*
413          * Grr, braindead ThunderLAN PHY doesn't self-configure
414          * after autonegotiation.  We have to do it ourselves
415          * based on the link partner status.
416          */
417
418         aner = PHY_READ(&sc->sc_mii, MII_ANER);
419         if (aner & ANER_LPAN) {
420                 anlpar = PHY_READ(&sc->sc_mii, MII_ANLPAR) &
421                     PHY_READ(&sc->sc_mii, MII_ANAR);
422                 if (anlpar & ANAR_10_FD) {
423                         PHY_WRITE(&sc->sc_mii, MII_BMCR, BMCR_FDX);
424                         return;
425                 }
426         }
427         PHY_WRITE(&sc->sc_mii, MII_BMCR, 0);
428 }