Deleted Added
full compact
xmphy.c (92739) xmphy.c (95722)
1/*
2 * Copyright (c) 2000
3 * Bill Paul <wpaul@ee.columbia.edu>. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Bill Paul.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30 * THE POSSIBILITY OF SUCH DAMAGE.
31 *
1/*
2 * Copyright (c) 2000
3 * Bill Paul <wpaul@ee.columbia.edu>. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Bill Paul.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30 * THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * $FreeBSD: head/sys/dev/mii/xmphy.c 92739 2002-03-20 02:08:01Z alfred $
32 * $FreeBSD: head/sys/dev/mii/xmphy.c 95722 2002-04-29 13:07:38Z phk $
33 */
34
35/*
36 * driver for the XaQti XMAC II's internal PHY. This is sort of
37 * like a 10/100 PHY, except the only thing we're really autoselecting
38 * here is full/half duplex. Speed is always 1000mbps.
39 */
40
41#include <sys/param.h>
42#include <sys/systm.h>
43#include <sys/kernel.h>
44#include <sys/malloc.h>
45#include <sys/socket.h>
46#include <sys/bus.h>
47
48
49#include <net/if.h>
50#include <net/if_media.h>
51
52#include <dev/mii/mii.h>
53#include <dev/mii/miivar.h>
54#include <dev/mii/miidevs.h>
55
56#include <dev/mii/xmphyreg.h>
57
58#include "miibus_if.h"
59
60#if !defined(lint)
61static const char rcsid[] =
33 */
34
35/*
36 * driver for the XaQti XMAC II's internal PHY. This is sort of
37 * like a 10/100 PHY, except the only thing we're really autoselecting
38 * here is full/half duplex. Speed is always 1000mbps.
39 */
40
41#include <sys/param.h>
42#include <sys/systm.h>
43#include <sys/kernel.h>
44#include <sys/malloc.h>
45#include <sys/socket.h>
46#include <sys/bus.h>
47
48
49#include <net/if.h>
50#include <net/if_media.h>
51
52#include <dev/mii/mii.h>
53#include <dev/mii/miivar.h>
54#include <dev/mii/miidevs.h>
55
56#include <dev/mii/xmphyreg.h>
57
58#include "miibus_if.h"
59
60#if !defined(lint)
61static const char rcsid[] =
62 "$FreeBSD: head/sys/dev/mii/xmphy.c 92739 2002-03-20 02:08:01Z alfred $";
62 "$FreeBSD: head/sys/dev/mii/xmphy.c 95722 2002-04-29 13:07:38Z phk $";
63#endif
64
65static int xmphy_probe (device_t);
66static int xmphy_attach (device_t);
63#endif
64
65static int xmphy_probe (device_t);
66static int xmphy_attach (device_t);
67static int xmphy_detach (device_t);
68
69static device_method_t xmphy_methods[] = {
70 /* device interface */
71 DEVMETHOD(device_probe, xmphy_probe),
72 DEVMETHOD(device_attach, xmphy_attach),
67
68static device_method_t xmphy_methods[] = {
69 /* device interface */
70 DEVMETHOD(device_probe, xmphy_probe),
71 DEVMETHOD(device_attach, xmphy_attach),
73 DEVMETHOD(device_detach, xmphy_detach),
72 DEVMETHOD(device_detach, mii_phy_detach),
74 DEVMETHOD(device_shutdown, bus_generic_shutdown),
75 { 0, 0 }
76};
77
78static devclass_t xmphy_devclass;
79
80static driver_t xmphy_driver = {
81 "xmphy",
82 xmphy_methods,
83 sizeof(struct mii_softc)
84};
85
86DRIVER_MODULE(xmphy, miibus, xmphy_driver, xmphy_devclass, 0, 0);
87
88static int xmphy_service(struct mii_softc *, struct mii_data *, int);
89static void xmphy_status(struct mii_softc *);
90static int xmphy_mii_phy_auto(struct mii_softc *, int);
91extern void mii_phy_auto_timeout(void *);
92
93static int xmphy_probe(dev)
94 device_t dev;
95{
96 struct mii_attach_args *ma;
97
98 ma = device_get_ivars(dev);
99
100 if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_xxXAQTI &&
101 MII_MODEL(ma->mii_id2) == MII_MODEL_XAQTI_XMACII) {
102 device_set_desc(dev, MII_STR_XAQTI_XMACII);
103 return(0);
104 }
105
106 if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_JATO &&
107 MII_MODEL(ma->mii_id2) == MII_MODEL_JATO_BASEX) {
108 device_set_desc(dev, MII_STR_JATO_BASEX);
109 return(0);
110 }
111
112 return(ENXIO);
113}
114
115static int xmphy_attach(dev)
116 device_t dev;
117{
118 struct mii_softc *sc;
119 struct mii_attach_args *ma;
120 struct mii_data *mii;
121 const char *sep = "";
122
123 sc = device_get_softc(dev);
124 ma = device_get_ivars(dev);
125 sc->mii_dev = device_get_parent(dev);
126 mii = device_get_softc(sc->mii_dev);
127 LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
128
129 sc->mii_inst = mii->mii_instance;
130 sc->mii_phy = ma->mii_phyno;
131 sc->mii_service = xmphy_service;
132 sc->mii_pdata = mii;
133
134 sc->mii_flags |= MIIF_NOISOLATE;
135 mii->mii_instance++;
136
137#define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL)
138#define PRINT(s) printf("%s%s", sep, s); sep = ", "
139
140 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
141 BMCR_ISO);
142#if 0
143 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->mii_inst),
144 BMCR_LOOP|BMCR_S100);
145#endif
146
147 mii_phy_reset(sc);
148
149 device_printf(dev, " ");
150 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, 0, sc->mii_inst),
151 XMPHY_BMCR_FDX);
152 PRINT("1000baseSX");
153 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, IFM_FDX, sc->mii_inst), 0);
154 PRINT("1000baseSX-FDX");
155 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst), 0);
156 PRINT("auto");
157
158 printf("\n");
159#undef ADD
160#undef PRINT
161
162 MIIBUS_MEDIAINIT(sc->mii_dev);
163 return(0);
164}
165
73 DEVMETHOD(device_shutdown, bus_generic_shutdown),
74 { 0, 0 }
75};
76
77static devclass_t xmphy_devclass;
78
79static driver_t xmphy_driver = {
80 "xmphy",
81 xmphy_methods,
82 sizeof(struct mii_softc)
83};
84
85DRIVER_MODULE(xmphy, miibus, xmphy_driver, xmphy_devclass, 0, 0);
86
87static int xmphy_service(struct mii_softc *, struct mii_data *, int);
88static void xmphy_status(struct mii_softc *);
89static int xmphy_mii_phy_auto(struct mii_softc *, int);
90extern void mii_phy_auto_timeout(void *);
91
92static int xmphy_probe(dev)
93 device_t dev;
94{
95 struct mii_attach_args *ma;
96
97 ma = device_get_ivars(dev);
98
99 if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_xxXAQTI &&
100 MII_MODEL(ma->mii_id2) == MII_MODEL_XAQTI_XMACII) {
101 device_set_desc(dev, MII_STR_XAQTI_XMACII);
102 return(0);
103 }
104
105 if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_JATO &&
106 MII_MODEL(ma->mii_id2) == MII_MODEL_JATO_BASEX) {
107 device_set_desc(dev, MII_STR_JATO_BASEX);
108 return(0);
109 }
110
111 return(ENXIO);
112}
113
114static int xmphy_attach(dev)
115 device_t dev;
116{
117 struct mii_softc *sc;
118 struct mii_attach_args *ma;
119 struct mii_data *mii;
120 const char *sep = "";
121
122 sc = device_get_softc(dev);
123 ma = device_get_ivars(dev);
124 sc->mii_dev = device_get_parent(dev);
125 mii = device_get_softc(sc->mii_dev);
126 LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
127
128 sc->mii_inst = mii->mii_instance;
129 sc->mii_phy = ma->mii_phyno;
130 sc->mii_service = xmphy_service;
131 sc->mii_pdata = mii;
132
133 sc->mii_flags |= MIIF_NOISOLATE;
134 mii->mii_instance++;
135
136#define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL)
137#define PRINT(s) printf("%s%s", sep, s); sep = ", "
138
139 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
140 BMCR_ISO);
141#if 0
142 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->mii_inst),
143 BMCR_LOOP|BMCR_S100);
144#endif
145
146 mii_phy_reset(sc);
147
148 device_printf(dev, " ");
149 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, 0, sc->mii_inst),
150 XMPHY_BMCR_FDX);
151 PRINT("1000baseSX");
152 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, IFM_FDX, sc->mii_inst), 0);
153 PRINT("1000baseSX-FDX");
154 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst), 0);
155 PRINT("auto");
156
157 printf("\n");
158#undef ADD
159#undef PRINT
160
161 MIIBUS_MEDIAINIT(sc->mii_dev);
162 return(0);
163}
164
166static int xmphy_detach(dev)
167 device_t dev;
168{
169 struct mii_softc *sc;
170 struct mii_data *mii;
171
172 sc = device_get_softc(dev);
173 mii = device_get_softc(device_get_parent(dev));
174 if (sc->mii_flags & MIIF_DOINGAUTO)
175 untimeout(mii_phy_auto_timeout, sc, sc->mii_auto_ch);
176 sc->mii_dev = NULL;
177 LIST_REMOVE(sc, mii_list);
178
179 return(0);
180}
181
182static int
183xmphy_service(sc, mii, cmd)
184 struct mii_softc *sc;
185 struct mii_data *mii;
186 int cmd;
187{
188 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
189 int reg;
190
191 switch (cmd) {
192 case MII_POLLSTAT:
193 /*
194 * If we're not polling our PHY instance, just return.
195 */
196 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
197 return (0);
198 break;
199
200 case MII_MEDIACHG:
201 /*
202 * If the media indicates a different PHY instance,
203 * isolate ourselves.
204 */
205 if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
206 reg = PHY_READ(sc, MII_BMCR);
207 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
208 return (0);
209 }
210
211 /*
212 * If the interface is not up, don't do anything.
213 */
214 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
215 break;
216
217 switch (IFM_SUBTYPE(ife->ifm_media)) {
218 case IFM_AUTO:
219#ifdef foo
220 /*
221 * If we're already in auto mode, just return.
222 */
223 if (PHY_READ(sc, XMPHY_MII_BMCR) & XMPHY_BMCR_AUTOEN)
224 return (0);
225#endif
226 (void) xmphy_mii_phy_auto(sc, 1);
227 break;
228 case IFM_1000_SX:
229 mii_phy_reset(sc);
230 if ((ife->ifm_media & IFM_GMASK) == IFM_FDX) {
231 PHY_WRITE(sc, XMPHY_MII_ANAR, XMPHY_ANAR_FDX);
232 PHY_WRITE(sc, XMPHY_MII_BMCR, XMPHY_BMCR_FDX);
233 } else {
234 PHY_WRITE(sc, XMPHY_MII_ANAR, XMPHY_ANAR_HDX);
235 PHY_WRITE(sc, XMPHY_MII_BMCR, 0);
236 }
237 break;
238 case IFM_100_T4:
239 case IFM_100_TX:
240 case IFM_10_T:
241 default:
242 return (EINVAL);
243 }
244 break;
245
246 case MII_TICK:
247 /*
248 * If we're not currently selected, just return.
249 */
250 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
251 return (0);
252
253 /*
254 * Is the interface even up?
255 */
256 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
257 return (0);
258
259 /*
260 * Only used for autonegotiation.
261 */
262 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
263 break;
264
265 /*
266 * Check to see if we have link. If we do, we don't
267 * need to restart the autonegotiation process. Read
268 * the BMSR twice in case it's latched.
269 */
270 reg = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
271 if (reg & BMSR_LINK)
272 break;
273
274 /*
275 * Only retry autonegotiation every 5 seconds.
276 */
277 if (++sc->mii_ticks != 5)
278 return (0);
279
280 sc->mii_ticks = 0;
281
282 mii_phy_reset(sc);
283 if (xmphy_mii_phy_auto(sc, 0) == EJUSTRETURN)
284 return(0);
285 break;
286 }
287
288 /* Update the media status. */
289 xmphy_status(sc);
290
291 /* Callback if something changed. */
292 mii_phy_update(sc, cmd);
293 return (0);
294}
295
296static void
297xmphy_status(sc)
298 struct mii_softc *sc;
299{
300 struct mii_data *mii = sc->mii_pdata;
301 int bmsr, bmcr, anlpar;
302
303 mii->mii_media_status = IFM_AVALID;
304 mii->mii_media_active = IFM_ETHER;
305
306 bmsr = PHY_READ(sc, XMPHY_MII_BMSR) |
307 PHY_READ(sc, XMPHY_MII_BMSR);
308 if (bmsr & XMPHY_BMSR_LINK)
309 mii->mii_media_status |= IFM_ACTIVE;
310
311 /* Do dummy read of extended status register. */
312 bmcr = PHY_READ(sc, XMPHY_MII_EXTSTS);
313
314 bmcr = PHY_READ(sc, XMPHY_MII_BMCR);
315
316 if (bmcr & XMPHY_BMCR_LOOP)
317 mii->mii_media_active |= IFM_LOOP;
318
319
320 if (bmcr & XMPHY_BMCR_AUTOEN) {
321 if ((bmsr & XMPHY_BMSR_ACOMP) == 0) {
322 if (bmsr & XMPHY_BMSR_LINK) {
323 mii->mii_media_active |= IFM_1000_SX|IFM_HDX;
324 return;
325 }
326 /* Erg, still trying, I guess... */
327 mii->mii_media_active |= IFM_NONE;
328 return;
329 }
330
331 mii->mii_media_active |= IFM_1000_SX;
332 anlpar = PHY_READ(sc, XMPHY_MII_ANAR) &
333 PHY_READ(sc, XMPHY_MII_ANLPAR);
334 if (anlpar & XMPHY_ANLPAR_FDX)
335 mii->mii_media_active |= IFM_FDX;
336 else
337 mii->mii_media_active |= IFM_HDX;
338 return;
339 }
340
341 mii->mii_media_active |= IFM_1000_SX;
342 if (bmcr & XMPHY_BMCR_FDX)
343 mii->mii_media_active |= IFM_FDX;
344 else
345 mii->mii_media_active |= IFM_HDX;
346
347 return;
348}
349
350
351static int
352xmphy_mii_phy_auto(mii, waitfor)
353 struct mii_softc *mii;
354 int waitfor;
355{
356 int bmsr, anar = 0, i;
357
358 if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) {
359 anar = PHY_READ(mii, XMPHY_MII_ANAR);
360 anar |= XMPHY_ANAR_FDX|XMPHY_ANAR_HDX;
361 PHY_WRITE(mii, XMPHY_MII_ANAR, anar);
362 DELAY(1000);
363 PHY_WRITE(mii, XMPHY_MII_BMCR,
364 XMPHY_BMCR_AUTOEN | XMPHY_BMCR_STARTNEG);
365 }
366
367 if (waitfor) {
368 /* Wait 500ms for it to complete. */
369 for (i = 0; i < 500; i++) {
370 if ((bmsr = PHY_READ(mii, XMPHY_MII_BMSR)) &
371 XMPHY_BMSR_ACOMP)
372 return (0);
373 DELAY(1000);
374#if 0
375 if ((bmsr & BMSR_ACOMP) == 0)
376 printf("%s: autonegotiation failed to complete\n",
377 mii->mii_dev.dv_xname);
378#endif
379 }
380
381 /*
382 * Don't need to worry about clearing MIIF_DOINGAUTO.
383 * If that's set, a timeout is pending, and it will
384 * clear the flag.
385 */
386 return (EIO);
387 }
388
389 /*
390 * Just let it finish asynchronously. This is for the benefit of
391 * the tick handler driving autonegotiation. Don't want 500ms
392 * delays all the time while the system is running!
393 */
394 if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) {
395 mii->mii_flags |= MIIF_DOINGAUTO;
396 mii->mii_auto_ch = timeout(mii_phy_auto_timeout, mii, hz >> 1);
397 }
398 return (EJUSTRETURN);
399}
165static int
166xmphy_service(sc, mii, cmd)
167 struct mii_softc *sc;
168 struct mii_data *mii;
169 int cmd;
170{
171 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
172 int reg;
173
174 switch (cmd) {
175 case MII_POLLSTAT:
176 /*
177 * If we're not polling our PHY instance, just return.
178 */
179 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
180 return (0);
181 break;
182
183 case MII_MEDIACHG:
184 /*
185 * If the media indicates a different PHY instance,
186 * isolate ourselves.
187 */
188 if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
189 reg = PHY_READ(sc, MII_BMCR);
190 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
191 return (0);
192 }
193
194 /*
195 * If the interface is not up, don't do anything.
196 */
197 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
198 break;
199
200 switch (IFM_SUBTYPE(ife->ifm_media)) {
201 case IFM_AUTO:
202#ifdef foo
203 /*
204 * If we're already in auto mode, just return.
205 */
206 if (PHY_READ(sc, XMPHY_MII_BMCR) & XMPHY_BMCR_AUTOEN)
207 return (0);
208#endif
209 (void) xmphy_mii_phy_auto(sc, 1);
210 break;
211 case IFM_1000_SX:
212 mii_phy_reset(sc);
213 if ((ife->ifm_media & IFM_GMASK) == IFM_FDX) {
214 PHY_WRITE(sc, XMPHY_MII_ANAR, XMPHY_ANAR_FDX);
215 PHY_WRITE(sc, XMPHY_MII_BMCR, XMPHY_BMCR_FDX);
216 } else {
217 PHY_WRITE(sc, XMPHY_MII_ANAR, XMPHY_ANAR_HDX);
218 PHY_WRITE(sc, XMPHY_MII_BMCR, 0);
219 }
220 break;
221 case IFM_100_T4:
222 case IFM_100_TX:
223 case IFM_10_T:
224 default:
225 return (EINVAL);
226 }
227 break;
228
229 case MII_TICK:
230 /*
231 * If we're not currently selected, just return.
232 */
233 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
234 return (0);
235
236 /*
237 * Is the interface even up?
238 */
239 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
240 return (0);
241
242 /*
243 * Only used for autonegotiation.
244 */
245 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
246 break;
247
248 /*
249 * Check to see if we have link. If we do, we don't
250 * need to restart the autonegotiation process. Read
251 * the BMSR twice in case it's latched.
252 */
253 reg = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
254 if (reg & BMSR_LINK)
255 break;
256
257 /*
258 * Only retry autonegotiation every 5 seconds.
259 */
260 if (++sc->mii_ticks != 5)
261 return (0);
262
263 sc->mii_ticks = 0;
264
265 mii_phy_reset(sc);
266 if (xmphy_mii_phy_auto(sc, 0) == EJUSTRETURN)
267 return(0);
268 break;
269 }
270
271 /* Update the media status. */
272 xmphy_status(sc);
273
274 /* Callback if something changed. */
275 mii_phy_update(sc, cmd);
276 return (0);
277}
278
279static void
280xmphy_status(sc)
281 struct mii_softc *sc;
282{
283 struct mii_data *mii = sc->mii_pdata;
284 int bmsr, bmcr, anlpar;
285
286 mii->mii_media_status = IFM_AVALID;
287 mii->mii_media_active = IFM_ETHER;
288
289 bmsr = PHY_READ(sc, XMPHY_MII_BMSR) |
290 PHY_READ(sc, XMPHY_MII_BMSR);
291 if (bmsr & XMPHY_BMSR_LINK)
292 mii->mii_media_status |= IFM_ACTIVE;
293
294 /* Do dummy read of extended status register. */
295 bmcr = PHY_READ(sc, XMPHY_MII_EXTSTS);
296
297 bmcr = PHY_READ(sc, XMPHY_MII_BMCR);
298
299 if (bmcr & XMPHY_BMCR_LOOP)
300 mii->mii_media_active |= IFM_LOOP;
301
302
303 if (bmcr & XMPHY_BMCR_AUTOEN) {
304 if ((bmsr & XMPHY_BMSR_ACOMP) == 0) {
305 if (bmsr & XMPHY_BMSR_LINK) {
306 mii->mii_media_active |= IFM_1000_SX|IFM_HDX;
307 return;
308 }
309 /* Erg, still trying, I guess... */
310 mii->mii_media_active |= IFM_NONE;
311 return;
312 }
313
314 mii->mii_media_active |= IFM_1000_SX;
315 anlpar = PHY_READ(sc, XMPHY_MII_ANAR) &
316 PHY_READ(sc, XMPHY_MII_ANLPAR);
317 if (anlpar & XMPHY_ANLPAR_FDX)
318 mii->mii_media_active |= IFM_FDX;
319 else
320 mii->mii_media_active |= IFM_HDX;
321 return;
322 }
323
324 mii->mii_media_active |= IFM_1000_SX;
325 if (bmcr & XMPHY_BMCR_FDX)
326 mii->mii_media_active |= IFM_FDX;
327 else
328 mii->mii_media_active |= IFM_HDX;
329
330 return;
331}
332
333
334static int
335xmphy_mii_phy_auto(mii, waitfor)
336 struct mii_softc *mii;
337 int waitfor;
338{
339 int bmsr, anar = 0, i;
340
341 if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) {
342 anar = PHY_READ(mii, XMPHY_MII_ANAR);
343 anar |= XMPHY_ANAR_FDX|XMPHY_ANAR_HDX;
344 PHY_WRITE(mii, XMPHY_MII_ANAR, anar);
345 DELAY(1000);
346 PHY_WRITE(mii, XMPHY_MII_BMCR,
347 XMPHY_BMCR_AUTOEN | XMPHY_BMCR_STARTNEG);
348 }
349
350 if (waitfor) {
351 /* Wait 500ms for it to complete. */
352 for (i = 0; i < 500; i++) {
353 if ((bmsr = PHY_READ(mii, XMPHY_MII_BMSR)) &
354 XMPHY_BMSR_ACOMP)
355 return (0);
356 DELAY(1000);
357#if 0
358 if ((bmsr & BMSR_ACOMP) == 0)
359 printf("%s: autonegotiation failed to complete\n",
360 mii->mii_dev.dv_xname);
361#endif
362 }
363
364 /*
365 * Don't need to worry about clearing MIIF_DOINGAUTO.
366 * If that's set, a timeout is pending, and it will
367 * clear the flag.
368 */
369 return (EIO);
370 }
371
372 /*
373 * Just let it finish asynchronously. This is for the benefit of
374 * the tick handler driving autonegotiation. Don't want 500ms
375 * delays all the time while the system is running!
376 */
377 if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) {
378 mii->mii_flags |= MIIF_DOINGAUTO;
379 mii->mii_auto_ch = timeout(mii_phy_auto_timeout, mii, hz >> 1);
380 }
381 return (EJUSTRETURN);
382}