mii_physubr.c revision 1.7 1 /* $NetBSD: mii_physubr.c,v 1.7 1999/11/03 22:32:45 thorpej 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/device.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/socket.h>
49 #include <sys/errno.h>
50
51 #include <net/if.h>
52 #include <net/if_media.h>
53
54 #include <dev/mii/mii.h>
55 #include <dev/mii/miivar.h>
56
57 /*
58 * Media to register setting conversion table. Order matters.
59 */
60 const struct mii_media mii_media_table[] = {
61 { BMCR_ISO, ANAR_CSMA }, /* None */
62 { 0, ANAR_CSMA|ANAR_10 }, /* 10baseT */
63 { BMCR_FDX, ANAR_CSMA|ANAR_10_FD }, /* 10baseT-FDX */
64 { BMCR_S100, ANAR_CSMA|ANAR_T4 }, /* 100baseT4 */
65 { BMCR_S100, ANAR_CSMA|ANAR_TX }, /* 100baseTX */
66 { BMCR_S100|BMCR_FDX, ANAR_CSMA|ANAR_TX_FD }, /* 100baseTX-FDX */
67 };
68
69 void mii_phy_auto_timeout __P((void *));
70
71 void
72 mii_phy_setmedia(sc)
73 struct mii_softc *sc;
74 {
75 struct mii_data *mii = sc->mii_pdata;
76 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
77 int bmcr, anar;
78
79 /*
80 * Table index is stored in the media entry.
81 */
82
83 #ifdef DIAGNOSTIC
84 if (ife->ifm_data < 0 || ife->ifm_data >= MII_NMEDIA)
85 panic("mii_phy_setmedia");
86 #endif
87
88 anar = mii_media_table[ife->ifm_data].mm_anar;
89 bmcr = mii_media_table[ife->ifm_data].mm_bmcr;
90
91 if (ife->ifm_media & IFM_LOOP)
92 bmcr |= BMCR_LOOP;
93
94 PHY_WRITE(sc, MII_ANAR, anar);
95 PHY_WRITE(sc, MII_BMCR, bmcr);
96 }
97
98 int
99 mii_phy_auto(sc, waitfor)
100 struct mii_softc *sc;
101 int waitfor;
102 {
103 int bmsr, i;
104
105 if ((sc->mii_flags & MIIF_DOINGAUTO) == 0) {
106 PHY_WRITE(sc, MII_ANAR,
107 BMSR_MEDIA_TO_ANAR(sc->mii_capabilities) | ANAR_CSMA);
108 PHY_WRITE(sc, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG);
109 }
110
111 if (waitfor) {
112 /* Wait 500ms for it to complete. */
113 for (i = 0; i < 500; i++) {
114 if ((bmsr = PHY_READ(sc, MII_BMSR)) & BMSR_ACOMP)
115 return (0);
116 delay(1000);
117 }
118
119 /*
120 * Don't need to worry about clearing MIIF_DOINGAUTO.
121 * If that's set, a timeout is pending, and it will
122 * clear the flag.
123 */
124 return (EIO);
125 }
126
127 /*
128 * Just let it finish asynchronously. This is for the benefit of
129 * the tick handler driving autonegotiation. Don't want 500ms
130 * delays all the time while the system is running!
131 */
132 if ((sc->mii_flags & MIIF_DOINGAUTO) == 0) {
133 sc->mii_flags |= MIIF_DOINGAUTO;
134 timeout(mii_phy_auto_timeout, sc, hz >> 1);
135 }
136 return (EJUSTRETURN);
137 }
138
139 void
140 mii_phy_auto_timeout(arg)
141 void *arg;
142 {
143 struct mii_softc *sc = arg;
144 int s, bmsr;
145
146 s = splnet();
147 sc->mii_flags &= ~MIIF_DOINGAUTO;
148 bmsr = PHY_READ(sc, MII_BMSR);
149
150 /* Update the media status. */
151 (void) (*sc->mii_service)(sc, sc->mii_pdata, MII_POLLSTAT);
152 splx(s);
153 }
154
155 void
156 mii_phy_reset(sc)
157 struct mii_softc *sc;
158 {
159 int reg, i;
160
161 if (sc->mii_flags & MIIF_NOISOLATE)
162 reg = BMCR_RESET;
163 else
164 reg = BMCR_RESET | BMCR_ISO;
165 PHY_WRITE(sc, MII_BMCR, reg);
166
167 /* Wait 100ms for it to complete. */
168 for (i = 0; i < 100; i++) {
169 reg = PHY_READ(sc, MII_BMCR);
170 if ((reg & BMCR_RESET) == 0)
171 break;
172 delay(1000);
173 }
174
175 if (sc->mii_inst != 0 && ((sc->mii_flags & MIIF_NOISOLATE) == 0))
176 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
177 }
178
179 /*
180 * Initialize generic PHY media based on BMSR, called when a PHY is
181 * attached. We expect to be set up to print a comma-separated list
182 * of media names. Does not print a newline.
183 */
184 void
185 mii_add_media(sc)
186 struct mii_softc *sc;
187 {
188 struct mii_data *mii = sc->mii_pdata;
189 const char *sep = "";
190
191 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL)
192 #define PRINT(s) printf("%s%s", sep, s); sep = ", "
193
194 if ((sc->mii_flags & MIIF_NOISOLATE) == 0)
195 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
196 MII_MEDIA_NONE);
197
198 if (sc->mii_capabilities & BMSR_10THDX) {
199 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, sc->mii_inst),
200 MII_MEDIA_10_T);
201 #if 0
202 if ((sc->mii_flags & MIIF_NOLOOP) == 0)
203 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_LOOP,
204 sc->mii_inst), MII_MEDIA_10_T);
205 #endif
206 PRINT("10baseT");
207 }
208 if (sc->mii_capabilities & BMSR_10TFDX) {
209 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, sc->mii_inst),
210 MII_MEDIA_10_T_FDX);
211 PRINT("10baseT-FDX");
212 }
213 if (sc->mii_capabilities & BMSR_100TXHDX) {
214 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, sc->mii_inst),
215 MII_MEDIA_100_TX);
216 #if 0
217 if ((sc->mii_flags & MIIF_NOLOOP) == 0)
218 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP,
219 sc->mii_inst), MII_MEDIA_100_TX);
220 #endif
221 PRINT("100baseTX");
222 }
223 if (sc->mii_capabilities & BMSR_100TXFDX) {
224 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, sc->mii_inst),
225 MII_MEDIA_100_TX_FDX);
226 PRINT("100baseTX-FDX");
227 }
228 if (sc->mii_capabilities & BMSR_100T4) {
229 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_T4, 0, sc->mii_inst),
230 MII_MEDIA_100_T4);
231 #if 0
232 if ((sc->mii_flags & MIIF_NOLOOP) == 0)
233 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_T4, IFM_LOOP,
234 sc->mii_inst), MII_MEDIA_100_T4);
235 #endif
236 PRINT("100baseT4");
237 }
238 if (sc->mii_capabilities & BMSR_ANEG) {
239 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst),
240 MII_NMEDIA); /* intentionally invalid index */
241 PRINT("auto");
242 }
243 #undef ADD
244 #undef PRINT
245 }
246