]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/mii/tlphy.c
Use __FBSDID().
[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 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
42
43 /*
44  * Copyright (c) 1997 Manuel Bouyer.  All rights reserved.
45  *
46  * Redistribution and use in source and binary forms, with or without
47  * modification, are permitted provided that the following conditions
48  * are met:
49  * 1. Redistributions of source code must retain the above copyright
50  *    notice, this list of conditions and the following disclaimer.
51  * 2. Redistributions in binary form must reproduce the above copyright
52  *    notice, this list of conditions and the following disclaimer in the
53  *    documentation and/or other materials provided with the distribution.
54  * 3. All advertising materials mentioning features or use of this software
55  *    must display the following acknowledgement:
56  *      This product includes software developed by Manuel Bouyer.
57  * 4. The name of the author may not be used to endorse or promote products
58  *    derived from this software without specific prior written permission.
59  *
60  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
61  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
62  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
63  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
64  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
65  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
66  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
67  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
68  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
69  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
70  */
71
72 /*
73  * Driver for Texas Instruments's ThunderLAN PHYs
74  */
75
76 #include <sys/cdefs.h>
77 __FBSDID("$FreeBSD$");
78
79 #include <sys/param.h>
80 #include <sys/systm.h>
81 #include <sys/kernel.h>
82 #include <sys/socket.h>
83 #include <sys/errno.h>
84 #include <sys/module.h>
85 #include <sys/bus.h>
86 #include <sys/malloc.h>
87
88 #include <machine/bus.h>
89
90 #include <net/if.h>
91 #include <net/if_media.h>
92
93 #include <dev/mii/mii.h>
94 #include <dev/mii/miivar.h>
95 #include "miidevs.h"
96
97 #include <dev/mii/tlphyreg.h>
98
99 #include "miibus_if.h"
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(device_t);
107 static int tlphy_attach(device_t);
108
109 static device_method_t tlphy_methods[] = {
110         /* device interface */
111         DEVMETHOD(device_probe,         tlphy_probe),
112         DEVMETHOD(device_attach,        tlphy_attach),
113         DEVMETHOD(device_detach,        mii_phy_detach),
114         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
115         { 0, 0 }
116 };
117
118 static devclass_t tlphy_devclass;
119
120 static driver_t tlphy_driver = {
121         "tlphy",
122         tlphy_methods,
123         sizeof(struct tlphy_softc)
124 };
125
126 DRIVER_MODULE(tlphy, miibus, tlphy_driver, tlphy_devclass, 0, 0);
127
128 static int      tlphy_service(struct mii_softc *, struct mii_data *, int);
129 static int      tlphy_auto(struct tlphy_softc *);
130 static void     tlphy_acomp(struct tlphy_softc *);
131 static void     tlphy_status(struct tlphy_softc *);
132
133 static int
134 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
151 tlphy_attach(dev)
152         device_t                dev;
153 {
154         struct tlphy_softc *sc;
155         struct mii_attach_args *ma;
156         struct mii_data *mii;
157         const char *sep = "";
158         int capmask = 0xFFFFFFFF;
159
160         sc = device_get_softc(dev);
161         ma = device_get_ivars(dev);
162         sc->sc_mii.mii_dev = device_get_parent(dev);
163         mii = device_get_softc(sc->sc_mii.mii_dev);
164         LIST_INSERT_HEAD(&mii->mii_phys, &sc->sc_mii, mii_list);
165
166         sc->sc_mii.mii_inst = mii->mii_instance;
167         sc->sc_mii.mii_phy = ma->mii_phyno;
168         sc->sc_mii.mii_service = tlphy_service;
169         sc->sc_mii.mii_pdata = mii;
170
171         if (mii->mii_instance) {
172                 struct mii_softc        *other;
173                 device_t                *devlist;
174                 int                     devs, i;
175
176                 device_get_children(sc->sc_mii.mii_dev, &devlist, &devs);
177                 for (i = 0; i < devs; i++) {
178                         if (strcmp(device_get_name(devlist[i]), "tlphy")) {
179                                 other = device_get_softc(devlist[i]);
180                                 capmask &= ~other->mii_capabilities;
181                                 break;
182                         }
183                 }
184                 free(devlist, M_TEMP);
185         }
186
187         mii->mii_instance++;
188
189         sc->sc_mii.mii_flags &= ~MIIF_NOISOLATE;
190         mii_phy_reset(&sc->sc_mii);
191         sc->sc_mii.mii_flags |= MIIF_NOISOLATE;
192
193         /*
194          * Note that if we're on a device that also supports 100baseTX,
195          * we are not going to want to use the built-in 10baseT port,
196          * since there will be another PHY on the MII wired up to the
197          * UTP connector.  The parent indicates this to us by specifying
198          * the TLPHY_MEDIA_NO_10_T bit.
199          */
200         sc->sc_mii.mii_capabilities =
201             PHY_READ(&sc->sc_mii, MII_BMSR) & capmask /*ma->mii_capmask*/;
202
203 #define ADD(m, c)       ifmedia_add(&mii->mii_media, (m), (c), NULL)
204
205         ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->sc_mii.mii_inst),
206             BMCR_ISO);
207
208         ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_LOOP,
209             sc->sc_mii.mii_inst), BMCR_LOOP);
210
211 #define PRINT(s)        printf("%s%s", sep, s); sep = ", "
212
213         device_printf(dev, " ");
214         ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_2, 0, sc->sc_mii.mii_inst), 0);
215         PRINT("10base2/BNC");
216         ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_5, 0, sc->sc_mii.mii_inst), 0);
217         PRINT("10base5/AUI");
218
219         if (sc->sc_mii.mii_capabilities & BMSR_MEDIAMASK) {
220                 printf("%s", sep);
221                 mii_add_media(&sc->sc_mii);
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
232 tlphy_service(self, mii, cmd)
233         struct mii_softc *self;
234         struct mii_data *mii;
235         int cmd;
236 {
237         struct tlphy_softc *sc = (struct tlphy_softc *)self;
238         struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
239         int reg;
240
241         if (sc->sc_need_acomp)
242                 tlphy_acomp(sc);
243
244         switch (cmd) {
245         case MII_POLLSTAT:
246                 /*
247                  * If we're not polling our PHY instance, just return.
248                  */
249                 if (IFM_INST(ife->ifm_media) != sc->sc_mii.mii_inst)
250                         return (0);
251                 break;
252
253         case MII_MEDIACHG:
254                 /*
255                  * If the media indicates a different PHY instance,
256                  * isolate ourselves.
257                  */
258                 if (IFM_INST(ife->ifm_media) != sc->sc_mii.mii_inst) {
259                         reg = PHY_READ(&sc->sc_mii, MII_BMCR);
260                         PHY_WRITE(&sc->sc_mii, MII_BMCR, reg | BMCR_ISO);
261                         return (0);
262                 }
263                 
264                 /*
265                  * If the interface is not up, don't do anything.
266                  */
267                 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
268                         break;
269
270                 switch (IFM_SUBTYPE(ife->ifm_media)) {
271                 case IFM_AUTO:
272                         /*
273                          * The ThunderLAN PHY doesn't self-configure after
274                          * an autonegotiation cycle, so there's no such
275                          * thing as "already in auto mode".
276                          */
277                         (void) tlphy_auto(sc);
278                         break;
279                 case IFM_10_2:
280                 case IFM_10_5:
281                         PHY_WRITE(&sc->sc_mii, MII_BMCR, 0);
282                         PHY_WRITE(&sc->sc_mii, MII_TLPHY_CTRL, CTRL_AUISEL);
283                         DELAY(100000);
284                         break;
285                 default:
286                         PHY_WRITE(&sc->sc_mii, MII_TLPHY_CTRL, 0);
287                         DELAY(100000);
288                         PHY_WRITE(&sc->sc_mii, MII_ANAR,
289                             mii_anar(ife->ifm_media));
290                         PHY_WRITE(&sc->sc_mii, MII_BMCR, ife->ifm_data);
291                 }
292                 break;
293
294         case MII_TICK:
295                 /*
296                  * If we're not currently selected, just return.
297                  */
298                 if (IFM_INST(ife->ifm_media) != sc->sc_mii.mii_inst)
299                         return (0);
300
301                 /*
302                  * Is the interface even up?
303                  */
304                 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
305                         return (0);
306
307                 /*
308                  * Only used for autonegotiation.
309                  */
310                 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
311                         break;
312
313                 /*
314                  * Check to see if we have link.  If we do, we don't
315                  * need to restart the autonegotiation process.  Read
316                  * the BMSR twice in case it's latched.
317                  *
318                  * XXX WHAT ABOUT CHECKING LINK ON THE BNC/AUI?!
319                  */
320                 reg = PHY_READ(&sc->sc_mii, MII_BMSR) |
321                     PHY_READ(&sc->sc_mii, MII_BMSR);
322                 if (reg & BMSR_LINK)
323                         break;
324
325                 /*
326                  * Only retry autonegotiation every 5 seconds.
327                  */
328                 if (++sc->sc_mii.mii_ticks != 5)
329                         return (0);
330
331                 sc->sc_mii.mii_ticks = 0;
332                 mii_phy_reset(&sc->sc_mii);
333                 tlphy_auto(sc);
334                 return (0);
335         }
336
337         /* Update the media status. */
338         tlphy_status(sc);
339
340         /* Callback if something changed. */
341         mii_phy_update(&sc->sc_mii, cmd);
342         return (0);
343 }
344
345 static void
346 tlphy_status(sc)
347         struct tlphy_softc *sc;
348 {
349         struct mii_data *mii = sc->sc_mii.mii_pdata;
350         int bmsr, bmcr, tlctrl;
351
352         mii->mii_media_status = IFM_AVALID;
353         mii->mii_media_active = IFM_ETHER;
354
355         bmcr = PHY_READ(&sc->sc_mii, MII_BMCR);
356         if (bmcr & BMCR_ISO) {
357                 mii->mii_media_active |= IFM_NONE;
358                 mii->mii_media_status = 0;  
359                 return;
360         }
361
362         tlctrl = PHY_READ(&sc->sc_mii, MII_TLPHY_CTRL);
363         if (tlctrl & CTRL_AUISEL) {
364                 mii->mii_media_status = 0;
365                 mii->mii_media_active = mii->mii_media.ifm_cur->ifm_media;
366                 return;
367         }
368
369         bmsr = PHY_READ(&sc->sc_mii, MII_BMSR) |
370             PHY_READ(&sc->sc_mii, MII_BMSR);
371         if (bmsr & BMSR_LINK)   
372                 mii->mii_media_status |= IFM_ACTIVE;
373
374         if (bmcr & BMCR_LOOP)
375                 mii->mii_media_active |= IFM_LOOP;
376
377         /*
378          * Grr, braindead ThunderLAN PHY doesn't have any way to
379          * tell which media is actually active.  (Note it also
380          * doesn't self-configure after autonegotiation.)  We
381          * just have to report what's in the BMCR.
382          */
383         if (bmcr & BMCR_FDX)
384                 mii->mii_media_active |= IFM_FDX;
385         mii->mii_media_active |= IFM_10_T;
386 }
387
388 static int
389 tlphy_auto(sc)
390         struct tlphy_softc *sc;
391 {
392         int error;
393
394         switch ((error = mii_phy_auto(&sc->sc_mii))) {
395         case EIO:
396                 /*
397                  * Just assume we're not in full-duplex mode.
398                  * XXX Check link and try AUI/BNC?
399                  */
400                 PHY_WRITE(&sc->sc_mii, MII_BMCR, 0);
401                 break;
402
403         case EJUSTRETURN:
404                 /* Flag that we need to program when it completes. */
405                 sc->sc_need_acomp = 1;
406                 break;
407
408         default:
409                 tlphy_acomp(sc);
410         }
411
412         return (error);
413 }
414
415 static void
416 tlphy_acomp(sc)
417         struct tlphy_softc *sc;
418 {
419         int aner, anlpar;
420
421         sc->sc_need_acomp = 0;
422
423         /*
424          * Grr, braindead ThunderLAN PHY doesn't self-configure
425          * after autonegotiation.  We have to do it ourselves
426          * based on the link partner status.
427          */
428
429         aner = PHY_READ(&sc->sc_mii, MII_ANER);
430         if (aner & ANER_LPAN) {
431                 anlpar = PHY_READ(&sc->sc_mii, MII_ANLPAR) &
432                     PHY_READ(&sc->sc_mii, MII_ANAR);
433                 if (anlpar & ANAR_10_FD) {
434                         PHY_WRITE(&sc->sc_mii, MII_BMCR, BMCR_FDX);
435                         return;
436                 }
437         }
438         PHY_WRITE(&sc->sc_mii, MII_BMCR, 0);
439 }