Merge from vendor branch NTPD:
[dragonfly.git] / sys / dev / netif / mii_layer / 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  * $FreeBSD: src/sys/dev/mii/mii_physubr.c,v 1.2.2.1 2000/12/12 19:29:14 wpaul Exp $
40  * $DragonFly: src/sys/dev/netif/mii_layer/mii_physubr.c,v 1.5 2004/09/18 19:32:59 dillon Exp $
41  */
42
43 /*
44  * Subroutines common to all PHYs.
45  */
46
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/socket.h>
51 #include <sys/errno.h>
52 #include <sys/module.h>
53 #include <sys/bus.h>
54
55 #include <machine/clock.h>
56
57 #include <net/if.h>
58 #include <net/if_media.h>
59
60 #include "mii.h"
61 #include "miivar.h"
62
63 #include "miibus_if.h"
64
65 void    mii_phy_auto_timeout (void *);
66
67 void
68 mii_softc_init(struct mii_softc *mii)
69 {
70         callout_init(&mii->mii_auto_ch);
71 }
72
73 int
74 mii_phy_auto(mii, waitfor)
75         struct mii_softc *mii;
76         int waitfor;
77 {
78         int bmsr, i;
79
80         if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) {
81                 PHY_WRITE(mii, MII_ANAR,
82                     BMSR_MEDIA_TO_ANAR(mii->mii_capabilities) | ANAR_CSMA);
83                 PHY_WRITE(mii, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG);
84         }
85
86         if (waitfor) {
87                 /* Wait 500ms for it to complete. */
88                 for (i = 0; i < 500; i++) {
89                         if ((bmsr = PHY_READ(mii, MII_BMSR)) & BMSR_ACOMP)
90                                 return (0);
91                         DELAY(1000);
92 #if 0
93                 if ((bmsr & BMSR_ACOMP) == 0)
94                         printf("%s: autonegotiation failed to complete\n",
95                             mii->mii_dev.dv_xname);
96 #endif
97                 }
98
99                 /*
100                  * Don't need to worry about clearing MIIF_DOINGAUTO.
101                  * If that's set, a timeout is pending, and it will
102                  * clear the flag.
103                  */
104                 return (EIO);
105         }
106
107         /*
108          * Just let it finish asynchronously.  This is for the benefit of
109          * the tick handler driving autonegotiation.  Don't want 500ms
110          * delays all the time while the system is running!
111          */
112         if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) {
113                 mii->mii_flags |= MIIF_DOINGAUTO;
114                 callout_reset(&mii->mii_auto_ch, hz >> 1,
115                                 mii_phy_auto_timeout, mii);
116         }
117         return (EJUSTRETURN);
118 }
119
120 void
121 mii_phy_auto_stop(sc)
122         struct mii_softc *sc;
123 {
124         if (sc->mii_flags & MIIF_DOINGAUTO) {
125                 sc->mii_flags &= ~MIIF_DOINGAUTO;
126                 callout_stop(&sc->mii_auto_ch);
127         }
128 }
129
130 void
131 mii_phy_auto_timeout(arg)
132         void *arg;
133 {
134         struct mii_softc *mii = arg;
135         int s, bmsr;
136
137         s = splnet();
138         mii->mii_flags &= ~MIIF_DOINGAUTO;
139         bmsr = PHY_READ(mii, MII_BMSR);
140 #if 0
141         if ((bmsr & BMSR_ACOMP) == 0)
142                 printf("%s: autonegotiation failed to complete\n",
143                     sc->sc_dev.dv_xname);
144 #endif
145
146         /* Update the media status. */
147         (void) (*mii->mii_service)(mii, mii->mii_pdata, MII_POLLSTAT);
148         splx(s);
149 }
150
151 void
152 mii_phy_reset(mii)
153         struct mii_softc *mii;
154 {
155         int reg, i;
156
157         if (mii->mii_flags & MIIF_NOISOLATE)
158                 reg = BMCR_RESET;
159         else
160                 reg = BMCR_RESET | BMCR_ISO;
161         PHY_WRITE(mii, MII_BMCR, reg);
162
163         /* Wait 100ms for it to complete. */
164         for (i = 0; i < 100; i++) {
165                 reg = PHY_READ(mii, MII_BMCR); 
166                 if ((reg & BMCR_RESET) == 0)
167                         break;
168                 DELAY(1000);
169         }
170
171         if (mii->mii_inst != 0 && ((mii->mii_flags & MIIF_NOISOLATE) == 0))
172                 PHY_WRITE(mii, MII_BMCR, reg | BMCR_ISO);
173 }
174
175 /*
176  * Given an ifmedia word, return the corresponding ANAR value.
177  */
178 int
179 mii_anar(media)
180         int media;
181 {
182         int rv;
183
184         switch (media & (IFM_TMASK|IFM_NMASK|IFM_FDX)) {
185         case IFM_ETHER|IFM_10_T:
186                 rv = ANAR_10|ANAR_CSMA;
187                 break;
188         case IFM_ETHER|IFM_10_T|IFM_FDX:
189                 rv = ANAR_10_FD|ANAR_CSMA;
190                 break;
191         case IFM_ETHER|IFM_100_TX:
192                 rv = ANAR_TX|ANAR_CSMA;
193                 break;
194         case IFM_ETHER|IFM_100_TX|IFM_FDX:
195                 rv = ANAR_TX_FD|ANAR_CSMA;
196                 break;
197         case IFM_ETHER|IFM_100_T4:
198                 rv = ANAR_T4|ANAR_CSMA;
199                 break;
200         default:
201                 rv = 0;
202                 break;
203         }
204
205         return (rv);
206 }
207
208 /*
209  * Given a BMCR value, return the corresponding ifmedia word.
210  */
211 int
212 mii_media_from_bmcr(bmcr)
213         int bmcr;
214 {
215         int rv = IFM_ETHER;
216
217         if (bmcr & BMCR_S100)
218                 rv |= IFM_100_TX;
219         else
220                 rv |= IFM_10_T;
221         if (bmcr & BMCR_FDX)
222                 rv |= IFM_FDX;
223
224         return (rv);
225 }
226
227 /*
228  * Initialize generic PHY media based on BMSR, called when a PHY is
229  * attached.  We expect to be set up to print a comma-separated list
230  * of media names.  Does not print a newline.
231  */
232 void
233 mii_add_media(mii, bmsr, instance)
234         struct mii_data *mii;
235         int bmsr, instance;
236 {
237         const char *sep = "";
238
239 #define ADD(m, c)       ifmedia_add(&mii->mii_media, (m), (c), NULL)
240 #define PRINT(s)        printf("%s%s", sep, s); sep = ", "
241
242         if (bmsr & BMSR_10THDX) {
243                 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, instance), 0);
244                 PRINT("10baseT");
245         }
246         if (bmsr & BMSR_10TFDX) {
247                 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, instance),
248                     BMCR_FDX);
249                 PRINT("10baseT-FDX");
250         }
251         if (bmsr & BMSR_100TXHDX) {
252                 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, instance),
253                     BMCR_S100);
254                 PRINT("100baseTX");
255         }
256         if (bmsr & BMSR_100TXFDX) {
257                 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, instance),
258                     BMCR_S100|BMCR_FDX);
259                 PRINT("100baseTX-FDX");
260         }
261         if (bmsr & BMSR_100T4) {
262                 /*
263                  * XXX How do you enable 100baseT4?  I assume we set
264                  * XXX BMCR_S100 and then assume the PHYs will take
265                  * XXX watever action is necessary to switch themselves
266                  * XXX into T4 mode.
267                  */
268                 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_T4, 0, instance),
269                     BMCR_S100);
270                 PRINT("100baseT4");
271         }
272         if (bmsr & BMSR_ANEG) {
273                 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, instance),
274                     BMCR_AUTOEN);
275                 PRINT("auto");
276         }
277 #undef ADD
278 #undef PRINT
279 }