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