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