mii_physubr.c revision 1.14 1 1.14 thorpej /* $NetBSD: mii_physubr.c,v 1.14 2000/02/03 06:11:13 thorpej Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*-
4 1.3 thorpej * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.1 thorpej * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 1.1 thorpej * NASA Ames Research Center.
10 1.1 thorpej *
11 1.1 thorpej * Redistribution and use in source and binary forms, with or without
12 1.1 thorpej * modification, are permitted provided that the following conditions
13 1.1 thorpej * are met:
14 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
15 1.1 thorpej * notice, this list of conditions and the following disclaimer.
16 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
18 1.1 thorpej * documentation and/or other materials provided with the distribution.
19 1.1 thorpej * 3. All advertising materials mentioning features or use of this software
20 1.1 thorpej * must display the following acknowledgement:
21 1.1 thorpej * This product includes software developed by the NetBSD
22 1.1 thorpej * Foundation, Inc. and its contributors.
23 1.1 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
24 1.1 thorpej * contributors may be used to endorse or promote products derived
25 1.1 thorpej * from this software without specific prior written permission.
26 1.1 thorpej *
27 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 1.1 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE.
38 1.1 thorpej */
39 1.1 thorpej
40 1.1 thorpej /*
41 1.1 thorpej * Subroutines common to all PHYs.
42 1.1 thorpej */
43 1.1 thorpej
44 1.1 thorpej #include <sys/param.h>
45 1.1 thorpej #include <sys/device.h>
46 1.1 thorpej #include <sys/systm.h>
47 1.3 thorpej #include <sys/kernel.h>
48 1.1 thorpej #include <sys/socket.h>
49 1.3 thorpej #include <sys/errno.h>
50 1.1 thorpej
51 1.1 thorpej #include <net/if.h>
52 1.1 thorpej #include <net/if_media.h>
53 1.1 thorpej
54 1.1 thorpej #include <dev/mii/mii.h>
55 1.1 thorpej #include <dev/mii/miivar.h>
56 1.1 thorpej
57 1.6 thorpej /*
58 1.6 thorpej * Media to register setting conversion table. Order matters.
59 1.6 thorpej */
60 1.6 thorpej const struct mii_media mii_media_table[] = {
61 1.6 thorpej { BMCR_ISO, ANAR_CSMA }, /* None */
62 1.6 thorpej { 0, ANAR_CSMA|ANAR_10 }, /* 10baseT */
63 1.6 thorpej { BMCR_FDX, ANAR_CSMA|ANAR_10_FD }, /* 10baseT-FDX */
64 1.6 thorpej { BMCR_S100, ANAR_CSMA|ANAR_T4 }, /* 100baseT4 */
65 1.6 thorpej { BMCR_S100, ANAR_CSMA|ANAR_TX }, /* 100baseTX */
66 1.6 thorpej { BMCR_S100|BMCR_FDX, ANAR_CSMA|ANAR_TX_FD }, /* 100baseTX-FDX */
67 1.6 thorpej };
68 1.6 thorpej
69 1.3 thorpej void mii_phy_auto_timeout __P((void *));
70 1.3 thorpej
71 1.6 thorpej void
72 1.6 thorpej mii_phy_setmedia(sc)
73 1.6 thorpej struct mii_softc *sc;
74 1.6 thorpej {
75 1.6 thorpej struct mii_data *mii = sc->mii_pdata;
76 1.6 thorpej struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
77 1.6 thorpej int bmcr, anar;
78 1.12 thorpej
79 1.13 thorpej if (IFM_SUBTYPE(ife->ifm_media) == IFM_AUTO) {
80 1.14 thorpej if ((PHY_READ(sc, MII_BMCR) & BMCR_AUTOEN) == 0)
81 1.14 thorpej (void) mii_phy_auto(sc, 1);
82 1.13 thorpej return;
83 1.13 thorpej }
84 1.6 thorpej
85 1.6 thorpej /*
86 1.6 thorpej * Table index is stored in the media entry.
87 1.6 thorpej */
88 1.6 thorpej
89 1.6 thorpej #ifdef DIAGNOSTIC
90 1.6 thorpej if (ife->ifm_data < 0 || ife->ifm_data >= MII_NMEDIA)
91 1.6 thorpej panic("mii_phy_setmedia");
92 1.6 thorpej #endif
93 1.6 thorpej
94 1.6 thorpej anar = mii_media_table[ife->ifm_data].mm_anar;
95 1.6 thorpej bmcr = mii_media_table[ife->ifm_data].mm_bmcr;
96 1.6 thorpej
97 1.6 thorpej if (ife->ifm_media & IFM_LOOP)
98 1.6 thorpej bmcr |= BMCR_LOOP;
99 1.6 thorpej
100 1.6 thorpej PHY_WRITE(sc, MII_ANAR, anar);
101 1.6 thorpej PHY_WRITE(sc, MII_BMCR, bmcr);
102 1.6 thorpej }
103 1.6 thorpej
104 1.1 thorpej int
105 1.6 thorpej mii_phy_auto(sc, waitfor)
106 1.6 thorpej struct mii_softc *sc;
107 1.4 kleink int waitfor;
108 1.1 thorpej {
109 1.1 thorpej int bmsr, i;
110 1.1 thorpej
111 1.6 thorpej if ((sc->mii_flags & MIIF_DOINGAUTO) == 0) {
112 1.6 thorpej PHY_WRITE(sc, MII_ANAR,
113 1.6 thorpej BMSR_MEDIA_TO_ANAR(sc->mii_capabilities) | ANAR_CSMA);
114 1.6 thorpej PHY_WRITE(sc, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG);
115 1.3 thorpej }
116 1.3 thorpej
117 1.3 thorpej if (waitfor) {
118 1.3 thorpej /* Wait 500ms for it to complete. */
119 1.3 thorpej for (i = 0; i < 500; i++) {
120 1.6 thorpej if ((bmsr = PHY_READ(sc, MII_BMSR)) & BMSR_ACOMP)
121 1.3 thorpej return (0);
122 1.3 thorpej delay(1000);
123 1.3 thorpej }
124 1.3 thorpej
125 1.3 thorpej /*
126 1.3 thorpej * Don't need to worry about clearing MIIF_DOINGAUTO.
127 1.3 thorpej * If that's set, a timeout is pending, and it will
128 1.3 thorpej * clear the flag.
129 1.3 thorpej */
130 1.3 thorpej return (EIO);
131 1.1 thorpej }
132 1.3 thorpej
133 1.3 thorpej /*
134 1.3 thorpej * Just let it finish asynchronously. This is for the benefit of
135 1.3 thorpej * the tick handler driving autonegotiation. Don't want 500ms
136 1.3 thorpej * delays all the time while the system is running!
137 1.3 thorpej */
138 1.6 thorpej if ((sc->mii_flags & MIIF_DOINGAUTO) == 0) {
139 1.6 thorpej sc->mii_flags |= MIIF_DOINGAUTO;
140 1.6 thorpej timeout(mii_phy_auto_timeout, sc, hz >> 1);
141 1.3 thorpej }
142 1.3 thorpej return (EJUSTRETURN);
143 1.3 thorpej }
144 1.3 thorpej
145 1.3 thorpej void
146 1.3 thorpej mii_phy_auto_timeout(arg)
147 1.3 thorpej void *arg;
148 1.3 thorpej {
149 1.6 thorpej struct mii_softc *sc = arg;
150 1.3 thorpej int s, bmsr;
151 1.3 thorpej
152 1.9 thorpej if ((sc->mii_dev.dv_flags & DVF_ACTIVE) == 0)
153 1.9 thorpej return;
154 1.9 thorpej
155 1.3 thorpej s = splnet();
156 1.6 thorpej sc->mii_flags &= ~MIIF_DOINGAUTO;
157 1.6 thorpej bmsr = PHY_READ(sc, MII_BMSR);
158 1.3 thorpej
159 1.3 thorpej /* Update the media status. */
160 1.6 thorpej (void) (*sc->mii_service)(sc, sc->mii_pdata, MII_POLLSTAT);
161 1.3 thorpej splx(s);
162 1.2 thorpej }
163 1.2 thorpej
164 1.2 thorpej void
165 1.6 thorpej mii_phy_reset(sc)
166 1.6 thorpej struct mii_softc *sc;
167 1.2 thorpej {
168 1.2 thorpej int reg, i;
169 1.2 thorpej
170 1.6 thorpej if (sc->mii_flags & MIIF_NOISOLATE)
171 1.2 thorpej reg = BMCR_RESET;
172 1.2 thorpej else
173 1.2 thorpej reg = BMCR_RESET | BMCR_ISO;
174 1.6 thorpej PHY_WRITE(sc, MII_BMCR, reg);
175 1.2 thorpej
176 1.2 thorpej /* Wait 100ms for it to complete. */
177 1.2 thorpej for (i = 0; i < 100; i++) {
178 1.6 thorpej reg = PHY_READ(sc, MII_BMCR);
179 1.2 thorpej if ((reg & BMCR_RESET) == 0)
180 1.2 thorpej break;
181 1.2 thorpej delay(1000);
182 1.2 thorpej }
183 1.2 thorpej
184 1.6 thorpej if (sc->mii_inst != 0 && ((sc->mii_flags & MIIF_NOISOLATE) == 0))
185 1.6 thorpej PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
186 1.8 thorpej }
187 1.8 thorpej
188 1.8 thorpej void
189 1.8 thorpej mii_phy_down(sc)
190 1.8 thorpej struct mii_softc *sc;
191 1.8 thorpej {
192 1.8 thorpej
193 1.8 thorpej if (sc->mii_flags & MIIF_DOINGAUTO) {
194 1.8 thorpej sc->mii_flags &= ~MIIF_DOINGAUTO;
195 1.8 thorpej untimeout(mii_phy_auto_timeout, sc);
196 1.8 thorpej }
197 1.11 thorpej }
198 1.11 thorpej
199 1.11 thorpej void
200 1.11 thorpej mii_phy_status(sc)
201 1.11 thorpej struct mii_softc *sc;
202 1.11 thorpej {
203 1.11 thorpej
204 1.11 thorpej (*sc->mii_status)(sc);
205 1.5 drochner }
206 1.5 drochner
207 1.5 drochner /*
208 1.5 drochner * Initialize generic PHY media based on BMSR, called when a PHY is
209 1.5 drochner * attached. We expect to be set up to print a comma-separated list
210 1.5 drochner * of media names. Does not print a newline.
211 1.5 drochner */
212 1.5 drochner void
213 1.10 thorpej mii_phy_add_media(sc)
214 1.6 thorpej struct mii_softc *sc;
215 1.5 drochner {
216 1.6 thorpej struct mii_data *mii = sc->mii_pdata;
217 1.5 drochner const char *sep = "";
218 1.5 drochner
219 1.5 drochner #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL)
220 1.5 drochner #define PRINT(s) printf("%s%s", sep, s); sep = ", "
221 1.5 drochner
222 1.6 thorpej if ((sc->mii_flags & MIIF_NOISOLATE) == 0)
223 1.6 thorpej ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
224 1.6 thorpej MII_MEDIA_NONE);
225 1.6 thorpej
226 1.6 thorpej if (sc->mii_capabilities & BMSR_10THDX) {
227 1.6 thorpej ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, sc->mii_inst),
228 1.6 thorpej MII_MEDIA_10_T);
229 1.7 thorpej #if 0
230 1.6 thorpej if ((sc->mii_flags & MIIF_NOLOOP) == 0)
231 1.6 thorpej ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_LOOP,
232 1.6 thorpej sc->mii_inst), MII_MEDIA_10_T);
233 1.7 thorpej #endif
234 1.5 drochner PRINT("10baseT");
235 1.5 drochner }
236 1.6 thorpej if (sc->mii_capabilities & BMSR_10TFDX) {
237 1.6 thorpej ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, sc->mii_inst),
238 1.6 thorpej MII_MEDIA_10_T_FDX);
239 1.5 drochner PRINT("10baseT-FDX");
240 1.5 drochner }
241 1.6 thorpej if (sc->mii_capabilities & BMSR_100TXHDX) {
242 1.6 thorpej ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, sc->mii_inst),
243 1.6 thorpej MII_MEDIA_100_TX);
244 1.7 thorpej #if 0
245 1.6 thorpej if ((sc->mii_flags & MIIF_NOLOOP) == 0)
246 1.6 thorpej ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP,
247 1.6 thorpej sc->mii_inst), MII_MEDIA_100_TX);
248 1.7 thorpej #endif
249 1.5 drochner PRINT("100baseTX");
250 1.5 drochner }
251 1.6 thorpej if (sc->mii_capabilities & BMSR_100TXFDX) {
252 1.6 thorpej ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, sc->mii_inst),
253 1.6 thorpej MII_MEDIA_100_TX_FDX);
254 1.5 drochner PRINT("100baseTX-FDX");
255 1.5 drochner }
256 1.6 thorpej if (sc->mii_capabilities & BMSR_100T4) {
257 1.6 thorpej ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_T4, 0, sc->mii_inst),
258 1.6 thorpej MII_MEDIA_100_T4);
259 1.7 thorpej #if 0
260 1.6 thorpej if ((sc->mii_flags & MIIF_NOLOOP) == 0)
261 1.6 thorpej ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_T4, IFM_LOOP,
262 1.6 thorpej sc->mii_inst), MII_MEDIA_100_T4);
263 1.7 thorpej #endif
264 1.5 drochner PRINT("100baseT4");
265 1.5 drochner }
266 1.6 thorpej if (sc->mii_capabilities & BMSR_ANEG) {
267 1.6 thorpej ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst),
268 1.6 thorpej MII_NMEDIA); /* intentionally invalid index */
269 1.5 drochner PRINT("auto");
270 1.5 drochner }
271 1.5 drochner #undef ADD
272 1.5 drochner #undef PRINT
273 1.9 thorpej }
274 1.9 thorpej
275 1.9 thorpej void
276 1.10 thorpej mii_phy_delete_media(sc)
277 1.9 thorpej struct mii_softc *sc;
278 1.9 thorpej {
279 1.9 thorpej struct mii_data *mii = sc->mii_pdata;
280 1.9 thorpej
281 1.9 thorpej ifmedia_delete_instance(&mii->mii_media, sc->mii_inst);
282 1.9 thorpej }
283 1.9 thorpej
284 1.9 thorpej int
285 1.10 thorpej mii_phy_activate(self, act)
286 1.9 thorpej struct device *self;
287 1.9 thorpej enum devact act;
288 1.9 thorpej {
289 1.9 thorpej int rv = 0;
290 1.9 thorpej
291 1.9 thorpej switch (act) {
292 1.9 thorpej case DVACT_ACTIVATE:
293 1.9 thorpej rv = EOPNOTSUPP;
294 1.9 thorpej break;
295 1.9 thorpej
296 1.9 thorpej case DVACT_DEACTIVATE:
297 1.9 thorpej /* Nothing special to do. */
298 1.9 thorpej break;
299 1.9 thorpej }
300 1.9 thorpej
301 1.9 thorpej return (rv);
302 1.9 thorpej }
303 1.9 thorpej
304 1.9 thorpej int
305 1.10 thorpej mii_phy_detach(self, flags)
306 1.9 thorpej struct device *self;
307 1.9 thorpej int flags;
308 1.9 thorpej {
309 1.9 thorpej struct mii_softc *sc = (void *) self;
310 1.9 thorpej
311 1.9 thorpej if (sc->mii_flags & MIIF_DOINGAUTO)
312 1.9 thorpej untimeout(mii_phy_auto_timeout, sc);
313 1.9 thorpej
314 1.10 thorpej mii_phy_delete_media(sc);
315 1.9 thorpej
316 1.9 thorpej return (0);
317 1.1 thorpej }
318