]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/mii/mii_physubr.c
Edging ever closer to NetBSD...
[FreeBSD/FreeBSD.git] / sys / dev / mii / mii_physubr.c
1 /*      $NetBSD: mii_physubr.c,v 1.5 1999/08/03 19:41:49 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  * Subroutines common to all PHYs.
42  */
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/socket.h>
48 #include <sys/errno.h>
49 #include <sys/module.h>
50 #include <sys/bus.h>
51
52
53 #include <net/if.h>
54 #include <net/if_media.h>
55
56 #include <dev/mii/mii.h>
57 #include <dev/mii/miivar.h>
58
59 #include "miibus_if.h"
60
61 #if !defined(lint)
62 static const char rcsid[] =
63   "$FreeBSD$";
64 #endif
65
66 void    mii_phy_auto_timeout(void *);
67
68 int
69 mii_phy_auto(mii, waitfor)
70         struct mii_softc *mii;
71         int waitfor;
72 {
73         int bmsr, i;
74
75         if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) {
76                 PHY_WRITE(mii, MII_ANAR,
77                     BMSR_MEDIA_TO_ANAR(mii->mii_capabilities) | ANAR_CSMA);
78                 PHY_WRITE(mii, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG);
79         }
80
81         if (waitfor) {
82                 /* Wait 500ms for it to complete. */
83                 for (i = 0; i < 500; i++) {
84                         if ((bmsr = PHY_READ(mii, MII_BMSR)) & BMSR_ACOMP)
85                                 return (0);
86                         DELAY(1000);
87 #if 0
88                 if ((bmsr & BMSR_ACOMP) == 0)
89                         printf("%s: autonegotiation failed to complete\n",
90                             mii->mii_dev.dv_xname);
91 #endif
92                 }
93
94                 /*
95                  * Don't need to worry about clearing MIIF_DOINGAUTO.
96                  * If that's set, a timeout is pending, and it will
97                  * clear the flag.
98                  */
99                 return (EIO);
100         }
101
102         /*
103          * Just let it finish asynchronously.  This is for the benefit of
104          * the tick handler driving autonegotiation.  Don't want 500ms
105          * delays all the time while the system is running!
106          */
107         if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) {
108                 mii->mii_flags |= MIIF_DOINGAUTO;
109                 mii->mii_auto_ch = timeout(mii_phy_auto_timeout, mii, hz >> 1);
110         }
111         return (EJUSTRETURN);
112 }
113
114 void
115 mii_phy_auto_stop(sc)
116         struct mii_softc *sc;
117 {
118         if (sc->mii_flags & MIIF_DOINGAUTO) {
119                 sc->mii_flags &= ~MIIF_DOINGAUTO;
120                 untimeout(mii_phy_auto_timeout, sc, sc->mii_auto_ch);
121         }
122 }
123
124 void
125 mii_phy_auto_timeout(arg)
126         void *arg;
127 {
128         struct mii_softc *mii = arg;
129         int s, bmsr;
130
131         s = splnet();
132         mii->mii_flags &= ~MIIF_DOINGAUTO;
133         bmsr = PHY_READ(mii, MII_BMSR);
134 #if 0
135         if ((bmsr & BMSR_ACOMP) == 0)
136                 printf("%s: autonegotiation failed to complete\n",
137                     sc->sc_dev.dv_xname);
138 #endif
139
140         /* Update the media status. */
141         (void) (*mii->mii_service)(mii, mii->mii_pdata, MII_POLLSTAT);
142         splx(s);
143 }
144
145 int
146 mii_phy_tick(sc)
147         struct mii_softc *sc;
148 {
149         struct ifmedia_entry *ife = sc->mii_pdata->mii_media.ifm_cur;
150         struct ifnet *ifp = sc->mii_pdata->mii_ifp;
151         int reg;
152
153         /*
154          * Is the interface even up?
155          */
156         if ((ifp->if_flags & IFF_UP) == 0)
157                 return (EJUSTRETURN);
158
159         /*
160          * If we're not doing autonegotiation, we don't need to do
161          * any extra work here.  However, we need to check the link
162          * status so we can generate an announcement if the status
163          * changes.
164          */
165         if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
166                 return (0);
167
168         /*
169          * check for link.
170          * Read the status register twice; BMSR_LINK is latch-low.
171          */
172         reg = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
173         if (reg & BMSR_LINK)
174                 return (0);
175
176         /*
177          * Only retry autonegotiation every 5 seconds.
178          */
179         if (++sc->mii_ticks != 5)
180                 return (EJUSTRETURN);
181
182         sc->mii_ticks = 0;
183         mii_phy_reset(sc);
184         if (mii_phy_auto(sc, 0) == EJUSTRETURN)
185                 return (EJUSTRETURN);
186
187         /*
188          * Might need to generate a status message if autonegotiation
189          * failed.
190          */
191         return (0);
192 }
193
194 void
195 mii_phy_reset(mii)
196         struct mii_softc *mii;
197 {
198         int reg, i;
199
200         if (mii->mii_flags & MIIF_NOISOLATE)
201                 reg = BMCR_RESET;
202         else
203                 reg = BMCR_RESET | BMCR_ISO;
204         PHY_WRITE(mii, MII_BMCR, reg);
205
206         /* Wait 100ms for it to complete. */
207         for (i = 0; i < 100; i++) {
208                 reg = PHY_READ(mii, MII_BMCR); 
209                 if ((reg & BMCR_RESET) == 0)
210                         break;
211                 DELAY(1000);
212         }
213
214         if (mii->mii_inst != 0 && ((mii->mii_flags & MIIF_NOISOLATE) == 0))
215                 PHY_WRITE(mii, MII_BMCR, reg | BMCR_ISO);
216 }
217
218 void
219 mii_phy_update(sc, cmd)
220         struct mii_softc *sc;
221         int cmd;
222 {
223         struct mii_data *mii = sc->mii_pdata;
224
225         if (sc->mii_media_active != mii->mii_media_active || cmd == MII_MEDIACHG) {
226                 MIIBUS_STATCHG(sc->mii_dev);
227                 sc->mii_media_active = mii->mii_media_active;
228         }
229         if (sc->mii_media_status != mii->mii_media_status) {
230                 MIIBUS_LINKCHG(sc->mii_dev);
231                 sc->mii_media_status = mii->mii_media_status;
232         }
233 }
234
235 /*
236  * Given an ifmedia word, return the corresponding ANAR value.
237  */
238 int
239 mii_anar(media)
240         int media;
241 {
242         int rv;
243
244         switch (media & (IFM_TMASK|IFM_NMASK|IFM_FDX)) {
245         case IFM_ETHER|IFM_10_T:
246                 rv = ANAR_10|ANAR_CSMA;
247                 break;
248         case IFM_ETHER|IFM_10_T|IFM_FDX:
249                 rv = ANAR_10_FD|ANAR_CSMA;
250                 break;
251         case IFM_ETHER|IFM_100_TX:
252                 rv = ANAR_TX|ANAR_CSMA;
253                 break;
254         case IFM_ETHER|IFM_100_TX|IFM_FDX:
255                 rv = ANAR_TX_FD|ANAR_CSMA;
256                 break;
257         case IFM_ETHER|IFM_100_T4:
258                 rv = ANAR_T4|ANAR_CSMA;
259                 break;
260         default:
261                 rv = 0;
262                 break;
263         }
264
265         return (rv);
266 }
267
268 /*
269  * Given a BMCR value, return the corresponding ifmedia word.
270  */
271 int
272 mii_media_from_bmcr(bmcr)
273         int bmcr;
274 {
275         int rv = IFM_ETHER;
276
277         if (bmcr & BMCR_S100)
278                 rv |= IFM_100_TX;
279         else
280                 rv |= IFM_10_T;
281         if (bmcr & BMCR_FDX)
282                 rv |= IFM_FDX;
283
284         return (rv);
285 }
286
287 /*
288  * Initialize generic PHY media based on BMSR, called when a PHY is
289  * attached.  We expect to be set up to print a comma-separated list
290  * of media names.  Does not print a newline.
291  */
292 void
293 mii_add_media(struct mii_softc *sc)
294 {
295         const char *sep = "";
296         int bmsr, instance;
297         struct mii_data *mii;
298
299         bmsr = sc->mii_capabilities;
300         instance = sc->mii_inst;
301         mii = device_get_softc(sc->mii_dev);
302         if ((bmsr & BMSR_MEDIAMASK) == 0) {
303                 printf("no media present");
304                 return;
305         }
306
307 #define ADD(m, c)       ifmedia_add(&mii->mii_media, (m), (c), NULL)
308 #define PRINT(s)        printf("%s%s", sep, s); sep = ", "
309
310         if (bmsr & BMSR_10THDX) {
311                 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, instance), 0);
312                 PRINT("10baseT");
313         }
314         if (bmsr & BMSR_10TFDX) {
315                 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, instance),
316                     BMCR_FDX);
317                 PRINT("10baseT-FDX");
318         }
319         if (bmsr & BMSR_100TXHDX) {
320                 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, instance),
321                     BMCR_S100);
322                 PRINT("100baseTX");
323         }
324         if (bmsr & BMSR_100TXFDX) {
325                 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, instance),
326                     BMCR_S100|BMCR_FDX);
327                 PRINT("100baseTX-FDX");
328         }
329         if (bmsr & BMSR_100T4) {
330                 /*
331                  * XXX How do you enable 100baseT4?  I assume we set
332                  * XXX BMCR_S100 and then assume the PHYs will take
333                  * XXX watever action is necessary to switch themselves
334                  * XXX into T4 mode.
335                  */
336                 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_T4, 0, instance),
337                     BMCR_S100);
338                 PRINT("100baseT4");
339         }
340         if (bmsr & BMSR_ANEG) {
341                 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, instance),
342                     BMCR_AUTOEN);
343                 PRINT("auto");
344         }
345
346
347
348 #undef ADD
349 #undef PRINT
350 }