1 /* $NetBSD: mii_physubr.c,v 1.49 2006/03/29 07:05:24 thorpej Exp $ */
4 * Copyright (c) 1998, 1999, 2000, 2001 The NetBSD Foundation, Inc.
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.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
39 * $FreeBSD: src/sys/dev/mii/mii_physubr.c,v 1.2.2.1 2000/12/12 19:29:14 wpaul Exp $
43 * Subroutines common to all PHYs.
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/socket.h>
50 #include <sys/errno.h>
51 #include <sys/module.h>
53 #include <sys/thread2.h>
56 #include <net/if_media.h>
61 #include "miibus_if.h"
63 const struct mii_media mii_media_table[MII_NMEDIA] = {
64 [MII_MEDIA_NONE] = { .mm_bmcr = BMCR_ISO,
68 [MII_MEDIA_10_T] = { .mm_bmcr = BMCR_S10,
69 .mm_anar = ANAR_CSMA | ANAR_10,
72 [MII_MEDIA_10_T_FDX] = { .mm_bmcr = BMCR_S10 | BMCR_FDX,
73 .mm_anar = ANAR_CSMA | ANAR_10_FD,
76 [MII_MEDIA_100_T4] = { .mm_bmcr = BMCR_S100,
77 .mm_anar = ANAR_CSMA | ANAR_T4,
80 [MII_MEDIA_100_TX] = { .mm_bmcr = BMCR_S100,
81 .mm_anar = ANAR_CSMA | ANAR_TX,
84 [MII_MEDIA_100_TX_FDX] = { .mm_bmcr = BMCR_S100 | BMCR_FDX,
85 .mm_anar = ANAR_CSMA | ANAR_TX_FD,
88 [MII_MEDIA_1000_X] = { .mm_bmcr = BMCR_S1000,
92 [MII_MEDIA_1000_X_FDX] = { .mm_bmcr = BMCR_S1000 | BMCR_FDX,
96 [MII_MEDIA_1000_T] = { .mm_bmcr = BMCR_S1000,
98 .mm_gtcr = GTCR_ADV_1000THDX },
100 [MII_MEDIA_1000_T_FDX] = { .mm_bmcr = BMCR_S1000,
101 .mm_anar = ANAR_CSMA,
102 .mm_gtcr = GTCR_ADV_1000TFDX }
106 mii_softc_init(struct mii_softc *mii, struct mii_attach_args *ma)
108 mii->mii_phy = ma->mii_phyno;
109 mii->mii_flags |= ma->mii_flags;
110 mii->mii_model = MII_MODEL(ma->mii_id2);
111 mii->mii_rev = MII_REV(ma->mii_id2);
113 if (mii->mii_reset == NULL)
114 mii->mii_reset = mii_phy_reset;
115 if (mii->mii_anegticks == 0)
116 mii->mii_anegticks = MII_ANEGTICKS;
120 mii_phy_auto(struct mii_softc *sc, int waitfor)
125 * Check for 1000BASE-X. Autonegotiation is a bit
126 * different on such devices.
128 if (sc->mii_flags & MIIF_IS_1000X) {
130 if (sc->mii_extcapabilities & EXTSR_1000XFDX)
132 if (sc->mii_extcapabilities & EXTSR_1000XHDX)
135 if (sc->mii_flags & MIIF_DOPAUSE) {
136 /* XXX Asymmetric vs. symmetric? */
137 anar |= ANLPAR_X_PAUSE_TOWARDS;
139 PHY_WRITE(sc, MII_ANAR, anar);
141 anar = BMSR_MEDIA_TO_ANAR(sc->mii_capabilities) |
143 if (sc->mii_flags & MIIF_DOPAUSE) {
145 /* XXX Only 1000BASE-T has PAUSE_ASYM? */
146 if ((sc->mii_flags & MIIF_HAVE_GTCR) &&
147 (sc->mii_extcapabilities &
148 (EXTSR_1000THDX|EXTSR_1000TFDX)))
149 anar |= ANAR_X_PAUSE_ASYM;
151 PHY_WRITE(sc, MII_ANAR, anar);
152 if (sc->mii_flags & MIIF_HAVE_GTCR) {
155 if (sc->mii_extcapabilities & EXTSR_1000TFDX)
156 gtcr |= GTCR_ADV_1000TFDX;
157 if (sc->mii_extcapabilities & EXTSR_1000THDX)
158 gtcr |= GTCR_ADV_1000THDX;
160 PHY_WRITE(sc, MII_100T2CR, gtcr);
163 PHY_WRITE(sc, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG);
168 /* Wait 500ms for it to complete. */
169 for (i = 0; i < 500; i++) {
170 if (PHY_READ(sc, MII_BMSR) & BMSR_ACOMP)
176 return (EJUSTRETURN);
180 mii_phy_reset(struct mii_softc *sc)
184 if (sc->mii_flags & MIIF_NOISOLATE)
187 reg = BMCR_RESET | BMCR_ISO;
188 PHY_WRITE(sc, MII_BMCR, reg);
191 * It is best to allow a little time for the reset to settle
192 * in before we start polling the BMCR again. Notably, the
193 * DP83840A manual states that there should be a 500us delay
194 * between asserting software reset and attempting MII serial
195 * operations. Also, a DP83815 can get into a bad state on
196 * cable removal and reinsertion if we do not delay here.
200 /* Wait 100ms for it to complete. */
201 for (i = 0; i < 100; i++) {
202 reg = PHY_READ(sc, MII_BMCR);
203 if ((reg & BMCR_RESET) == 0)
208 if (sc->mii_inst != 0 && ((sc->mii_flags & MIIF_NOISOLATE) == 0))
209 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
213 * Initialize generic PHY media based on BMSR, called when a PHY is
214 * attached. We expect to be set up to print a comma-separated list
215 * of media names. Does not print a newline.
218 mii_phy_add_media(struct mii_softc *sc)
220 struct mii_data *mii = sc->mii_pdata;
221 const char *sep = "";
224 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL)
225 #define PRINT(s) kprintf("%s%s", sep, s); sep = ", "
227 if ((sc->mii_flags & MIIF_NOISOLATE) == 0)
228 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
232 * There are different interpretations for the bits in
233 * HomePNA PHYs. And there is really only one media type
236 if (sc->mii_flags & MIIF_IS_HPNA) {
237 if (sc->mii_capabilities & BMSR_10THDX) {
238 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_HPNA_1, 0,
246 if (sc->mii_capabilities & BMSR_10THDX) {
247 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, sc->mii_inst),
251 if (sc->mii_capabilities & BMSR_10TFDX) {
252 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, sc->mii_inst),
254 PRINT("10baseT-FDX");
257 if (sc->mii_capabilities & BMSR_100TXHDX) {
258 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, sc->mii_inst),
262 if (sc->mii_capabilities & BMSR_100TXFDX) {
263 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, sc->mii_inst),
264 MII_MEDIA_100_TX_FDX);
265 PRINT("100baseTX-FDX");
268 if (sc->mii_capabilities & BMSR_100T4) {
269 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_T4, 0, sc->mii_inst),
274 if (sc->mii_extcapabilities & EXTSR_MEDIAMASK) {
276 * XXX Right now only handle 1000SX and 1000TX. Need
277 * XXX to handle 1000LX and 1000CX some how.
279 * Note since it can take 5 seconds to auto-negotiate
280 * a gigabit link, we make anegticks 10 seconds for
281 * all the gigabit media types.
283 if (sc->mii_extcapabilities & EXTSR_1000XHDX) {
284 sc->mii_anegticks = MII_ANEGTICKS_GIGE;
285 sc->mii_flags |= MIIF_IS_1000X;
286 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, 0,
287 sc->mii_inst), MII_MEDIA_1000_X);
290 if (sc->mii_extcapabilities & EXTSR_1000XFDX) {
291 sc->mii_anegticks = MII_ANEGTICKS_GIGE;
292 sc->mii_flags |= MIIF_IS_1000X;
293 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, IFM_FDX,
294 sc->mii_inst), MII_MEDIA_1000_X_FDX);
295 PRINT("1000baseSX-FDX");
300 * 1000baseT media needs to be able to manipulate
301 * master/slave mode. We set IFM_ETH_MASTER in
302 * the "don't care mask" and filter it out when
305 * All 1000baseT PHYs have a 1000baseT control register.
307 if (sc->mii_extcapabilities & EXTSR_1000THDX) {
308 sc->mii_anegticks = MII_ANEGTICKS_GIGE;
309 sc->mii_flags |= MIIF_HAVE_GTCR;
310 mii->mii_media.ifm_mask |= IFM_ETH_MASTER;
311 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, 0,
312 sc->mii_inst), MII_MEDIA_1000_T);
315 if (sc->mii_extcapabilities & EXTSR_1000TFDX) {
316 sc->mii_anegticks = MII_ANEGTICKS_GIGE;
317 sc->mii_flags |= MIIF_HAVE_GTCR;
318 mii->mii_media.ifm_mask |= IFM_ETH_MASTER;
319 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, IFM_FDX,
320 sc->mii_inst), MII_MEDIA_1000_T_FDX);
321 PRINT("1000baseT-FDX");
326 if (sc->mii_capabilities & BMSR_ANEG) {
327 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst),
328 MII_NMEDIA); /* intentionally invalid index */
333 if (fdx != 0 && (sc->mii_flags & MIIF_DOPAUSE))
334 mii->mii_media.ifm_mask |= IFM_ETH_FMASK;
338 mii_phy_set_media(struct mii_softc *sc)
340 struct mii_data *mii = sc->mii_pdata;
341 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
342 int bmcr, anar, gtcr;
344 if (IFM_SUBTYPE(ife->ifm_media) == IFM_AUTO) {
346 * Force renegotiation if MIIF_DOPAUSE.
348 * XXX This is only necessary because many NICs don't
349 * XXX advertise PAUSE capabilities at boot time. Maybe
350 * XXX we should force this only once?
352 if ((PHY_READ(sc, MII_BMCR) & BMCR_AUTOEN) == 0 ||
353 (sc->mii_flags & (MIIF_FORCEANEG | MIIF_DOPAUSE)))
359 * Table index is stored in the media entry.
362 KASSERT(ife->ifm_data >= 0 && ife->ifm_data < MII_NMEDIA,
363 ("bogus ife->ifm_data (%d)", ife->ifm_data));
365 anar = mii_media_table[ife->ifm_data].mm_anar;
366 bmcr = mii_media_table[ife->ifm_data].mm_bmcr;
367 gtcr = mii_media_table[ife->ifm_data].mm_gtcr;
369 if (mii->mii_media.ifm_media & IFM_ETH_MASTER) {
370 switch (IFM_SUBTYPE(ife->ifm_media)) {
372 gtcr |= GTCR_MAN_MS|GTCR_ADV_MS;
376 panic("mii_phy_setmedia: MASTER on wrong media");
380 if (mii->mii_media.ifm_media & IFM_FLOW) {
381 if (sc->mii_flags & MIIF_IS_1000X) {
382 anar |= ANAR_X_PAUSE_SYM | ANAR_X_PAUSE_ASYM;
385 /* XXX Only 1000BASE-T has PAUSE_ASYM? */
386 if ((sc->mii_flags & MIIF_HAVE_GTCR) &&
387 (sc->mii_extcapabilities &
388 (EXTSR_1000THDX | EXTSR_1000TFDX)))
389 anar |= ANAR_X_PAUSE_ASYM;
393 if (ife->ifm_media & IFM_LOOP)
396 PHY_WRITE(sc, MII_ANAR, anar);
397 PHY_WRITE(sc, MII_BMCR, bmcr);
398 if (sc->mii_flags & MIIF_HAVE_GTCR)
399 PHY_WRITE(sc, MII_100T2CR, gtcr);
403 mii_phy_tick(struct mii_softc *sc)
405 struct mii_data *mii = sc->mii_pdata;
406 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
409 /* Just bail now if the interface is down. */
410 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
411 return (EJUSTRETURN);
414 * If we're not doing autonegotiation, we don't need to do
415 * any extra work here. However, we need to check the link
416 * status so we can generate an announcement if the status
419 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
422 /* Read the status register twice; BMSR_LINK is latch-low. */
423 reg = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
424 if (reg & BMSR_LINK) {
429 /* Reset autonegotiation timer. */
435 * Only retry autonegotiation every N seconds.
437 KKASSERT(sc->mii_anegticks > 0);
438 if (++sc->mii_ticks <= sc->mii_anegticks)
439 return (EJUSTRETURN);
442 sc->mii_reset(sc); /* Reset PHY */
443 mii_phy_auto(sc, 0); /* Ignore EJUSTRETURN */
448 mii_phy_statusmsg(struct mii_softc *sc)
450 struct mii_data *mii = sc->mii_pdata;
451 struct ifnet *ifp = mii->mii_ifp;
452 int baudrate, link_state, announce = 0;
454 if (mii->mii_media_status & IFM_AVALID) {
455 if (mii->mii_media_status & IFM_ACTIVE)
456 link_state = LINK_STATE_UP;
458 link_state = LINK_STATE_DOWN;
460 link_state = LINK_STATE_UNKNOWN;
462 baudrate = ifmedia_baudrate(mii->mii_media_active);
464 if (link_state != ifp->if_link_state) {
465 ifp->if_link_state = link_state;
467 * XXX Right here we'd like to notify protocols
468 * XXX that the link status has changed, so that
469 * XXX e.g. Duplicate Address Detection can restart.
474 if (baudrate != ifp->if_baudrate) {
475 ifp->if_baudrate = baudrate;
483 mii_phy_update(struct mii_softc *sc, int cmd)
485 struct mii_data *mii = sc->mii_pdata;
486 struct ifnet *ifp = mii->mii_ifp;
489 if (sc->mii_media_active != mii->mii_media_active ||
490 sc->mii_media_status != mii->mii_media_status ||
491 cmd == MII_MEDIACHG) {
492 announce = mii_phy_statusmsg(sc);
493 MIIBUS_STATCHG(sc->mii_dev);
494 sc->mii_media_active = mii->mii_media_active;
495 sc->mii_media_status = mii->mii_media_status;
499 if_link_state_change(ifp);
506 * Return the flow control status flag from MII_ANAR & MII_ANLPAR.
509 mii_phy_flowstatus(struct mii_softc *sc)
513 if ((sc->mii_flags & MIIF_DOPAUSE) == 0)
516 anar = PHY_READ(sc, MII_ANAR);
517 anlpar = PHY_READ(sc, MII_ANLPAR);
519 if ((anar & ANAR_X_PAUSE_SYM) & (anlpar & ANLPAR_X_PAUSE_SYM))
520 return (IFM_FLOW | IFM_ETH_TXPAUSE | IFM_ETH_RXPAUSE);
522 if ((anar & ANAR_X_PAUSE_SYM) == 0) {
523 if ((anar & ANAR_X_PAUSE_ASYM) &&
524 ((anlpar & ANLPAR_X_PAUSE_TOWARDS) ==
525 ANLPAR_X_PAUSE_TOWARDS))
526 return (IFM_FLOW | IFM_ETH_TXPAUSE);
531 if ((anar & ANAR_X_PAUSE_ASYM) == 0) {
532 if (anlpar & ANLPAR_X_PAUSE_SYM)
533 return (IFM_FLOW | IFM_ETH_TXPAUSE | IFM_ETH_RXPAUSE);
538 switch (anlpar & ANLPAR_X_PAUSE_TOWARDS) {
539 case ANLPAR_X_PAUSE_NONE:
542 case ANLPAR_X_PAUSE_ASYM:
543 return (IFM_FLOW | IFM_ETH_RXPAUSE);
546 return (IFM_FLOW | IFM_ETH_RXPAUSE | IFM_ETH_TXPAUSE);
551 const struct mii_phydesc *
552 mii_phy_match(const struct mii_attach_args *ma, const struct mii_phydesc *mpd)
554 for (; mpd->mpd_name != NULL; mpd++) {
555 if (MII_OUI(ma->mii_id1, ma->mii_id2) == mpd->mpd_oui &&
556 MII_MODEL(ma->mii_id2) == mpd->mpd_model)