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);
112 mii->mii_privtag = ma->mii_privtag;
113 mii->mii_priv = ma->mii_priv;
115 if (mii->mii_reset == NULL)
116 mii->mii_reset = mii_phy_reset;
117 if (mii->mii_anegticks == 0)
118 mii->mii_anegticks = MII_ANEGTICKS;
122 mii_phy_auto(struct mii_softc *sc, int waitfor)
127 * Check for 1000BASE-X. Autonegotiation is a bit
128 * different on such devices.
130 if (sc->mii_flags & MIIF_IS_1000X) {
132 if (sc->mii_extcapabilities & EXTSR_1000XFDX)
134 if (sc->mii_extcapabilities & EXTSR_1000XHDX)
137 if (sc->mii_flags & MIIF_DOPAUSE) {
138 /* XXX Asymmetric vs. symmetric? */
139 anar |= ANLPAR_X_PAUSE_TOWARDS;
141 PHY_WRITE(sc, MII_ANAR, anar);
143 anar = BMSR_MEDIA_TO_ANAR(sc->mii_capabilities) |
145 if (sc->mii_flags & MIIF_DOPAUSE) {
147 /* XXX Only 1000BASE-T has PAUSE_ASYM? */
148 if ((sc->mii_flags & MIIF_HAVE_GTCR) &&
149 (sc->mii_extcapabilities &
150 (EXTSR_1000THDX|EXTSR_1000TFDX)))
151 anar |= ANAR_X_PAUSE_ASYM;
153 PHY_WRITE(sc, MII_ANAR, anar);
154 if (sc->mii_flags & MIIF_HAVE_GTCR) {
157 if (sc->mii_extcapabilities & EXTSR_1000TFDX)
158 gtcr |= GTCR_ADV_1000TFDX;
159 if (sc->mii_extcapabilities & EXTSR_1000THDX)
160 gtcr |= GTCR_ADV_1000THDX;
162 PHY_WRITE(sc, MII_100T2CR, gtcr);
165 PHY_WRITE(sc, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG);
170 /* Wait 500ms for it to complete. */
171 for (i = 0; i < 500; i++) {
172 if (PHY_READ(sc, MII_BMSR) & BMSR_ACOMP)
178 return (EJUSTRETURN);
182 mii_phy_reset(struct mii_softc *sc)
186 if (sc->mii_flags & MIIF_NOISOLATE)
189 reg = BMCR_RESET | BMCR_ISO;
190 PHY_WRITE(sc, MII_BMCR, reg);
193 * It is best to allow a little time for the reset to settle
194 * in before we start polling the BMCR again. Notably, the
195 * DP83840A manual states that there should be a 500us delay
196 * between asserting software reset and attempting MII serial
197 * operations. Also, a DP83815 can get into a bad state on
198 * cable removal and reinsertion if we do not delay here.
202 /* Wait 100ms for it to complete. */
203 for (i = 0; i < 100; i++) {
204 reg = PHY_READ(sc, MII_BMCR);
205 if ((reg & BMCR_RESET) == 0)
210 if (sc->mii_inst != 0 && ((sc->mii_flags & MIIF_NOISOLATE) == 0))
211 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
215 * Initialize generic PHY media based on BMSR, called when a PHY is
216 * attached. We expect to be set up to print a comma-separated list
217 * of media names. Does not print a newline.
220 mii_phy_add_media(struct mii_softc *sc)
222 struct mii_data *mii = sc->mii_pdata;
223 const char *sep = "";
226 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL)
227 #define PRINT(s) kprintf("%s%s", sep, s); sep = ", "
229 if ((sc->mii_flags & MIIF_NOISOLATE) == 0)
230 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
234 * There are different interpretations for the bits in
235 * HomePNA PHYs. And there is really only one media type
238 if (sc->mii_flags & MIIF_IS_HPNA) {
239 if (sc->mii_capabilities & BMSR_10THDX) {
240 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_HPNA_1, 0,
248 if (sc->mii_capabilities & BMSR_10THDX) {
249 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, sc->mii_inst),
253 if (sc->mii_capabilities & BMSR_10TFDX) {
254 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, sc->mii_inst),
256 PRINT("10baseT-FDX");
259 if (sc->mii_capabilities & BMSR_100TXHDX) {
260 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, sc->mii_inst),
264 if (sc->mii_capabilities & BMSR_100TXFDX) {
265 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, sc->mii_inst),
266 MII_MEDIA_100_TX_FDX);
267 PRINT("100baseTX-FDX");
270 if (sc->mii_capabilities & BMSR_100T4) {
271 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_T4, 0, sc->mii_inst),
276 if (sc->mii_extcapabilities & EXTSR_MEDIAMASK) {
278 * XXX Right now only handle 1000SX and 1000TX. Need
279 * XXX to handle 1000LX and 1000CX some how.
281 * Note since it can take 5 seconds to auto-negotiate
282 * a gigabit link, we make anegticks 10 seconds for
283 * all the gigabit media types.
285 if (sc->mii_extcapabilities & EXTSR_1000XHDX) {
286 sc->mii_anegticks = MII_ANEGTICKS_GIGE;
287 sc->mii_flags |= MIIF_IS_1000X;
288 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, 0,
289 sc->mii_inst), MII_MEDIA_1000_X);
292 if (sc->mii_extcapabilities & EXTSR_1000XFDX) {
293 sc->mii_anegticks = MII_ANEGTICKS_GIGE;
294 sc->mii_flags |= MIIF_IS_1000X;
295 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, IFM_FDX,
296 sc->mii_inst), MII_MEDIA_1000_X_FDX);
297 PRINT("1000baseSX-FDX");
302 * 1000baseT media needs to be able to manipulate
303 * master/slave mode. We set IFM_ETH_MASTER in
304 * the "don't care mask" and filter it out when
307 * All 1000baseT PHYs have a 1000baseT control register.
309 if (sc->mii_extcapabilities & EXTSR_1000THDX) {
310 sc->mii_anegticks = MII_ANEGTICKS_GIGE;
311 sc->mii_flags |= MIIF_HAVE_GTCR;
312 mii->mii_media.ifm_mask |= IFM_ETH_MASTER;
313 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, 0,
314 sc->mii_inst), MII_MEDIA_1000_T);
317 if (sc->mii_extcapabilities & EXTSR_1000TFDX) {
318 sc->mii_anegticks = MII_ANEGTICKS_GIGE;
319 sc->mii_flags |= MIIF_HAVE_GTCR;
320 mii->mii_media.ifm_mask |= IFM_ETH_MASTER;
321 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, IFM_FDX,
322 sc->mii_inst), MII_MEDIA_1000_T_FDX);
323 PRINT("1000baseT-FDX");
328 if (sc->mii_capabilities & BMSR_ANEG) {
329 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst),
330 MII_NMEDIA); /* intentionally invalid index */
335 if (fdx != 0 && (sc->mii_flags & MIIF_DOPAUSE))
336 mii->mii_media.ifm_mask |= IFM_ETH_FMASK;
340 mii_phy_set_media(struct mii_softc *sc)
342 struct mii_data *mii = sc->mii_pdata;
343 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
344 int bmcr, anar, gtcr;
346 if (IFM_SUBTYPE(ife->ifm_media) == IFM_AUTO) {
348 * Force renegotiation if MIIF_DOPAUSE.
350 * XXX This is only necessary because many NICs don't
351 * XXX advertise PAUSE capabilities at boot time. Maybe
352 * XXX we should force this only once?
354 if ((PHY_READ(sc, MII_BMCR) & BMCR_AUTOEN) == 0 ||
355 (sc->mii_flags & (MIIF_FORCEANEG | MIIF_DOPAUSE)))
361 * Table index is stored in the media entry.
364 KASSERT(ife->ifm_data >= 0 && ife->ifm_data < MII_NMEDIA,
365 ("bogus ife->ifm_data (%d)", ife->ifm_data));
367 anar = mii_media_table[ife->ifm_data].mm_anar;
368 bmcr = mii_media_table[ife->ifm_data].mm_bmcr;
369 gtcr = mii_media_table[ife->ifm_data].mm_gtcr;
371 if (mii->mii_media.ifm_media & IFM_ETH_MASTER) {
372 switch (IFM_SUBTYPE(ife->ifm_media)) {
374 gtcr |= GTCR_MAN_MS|GTCR_ADV_MS;
378 panic("mii_phy_setmedia: MASTER on wrong media");
382 if (mii->mii_media.ifm_media & IFM_FLOW) {
383 if (sc->mii_flags & MIIF_IS_1000X) {
384 anar |= ANAR_X_PAUSE_SYM | ANAR_X_PAUSE_ASYM;
387 /* XXX Only 1000BASE-T has PAUSE_ASYM? */
388 if ((sc->mii_flags & MIIF_HAVE_GTCR) &&
389 (sc->mii_extcapabilities &
390 (EXTSR_1000THDX | EXTSR_1000TFDX)))
391 anar |= ANAR_X_PAUSE_ASYM;
395 if (ife->ifm_media & IFM_LOOP)
398 PHY_WRITE(sc, MII_ANAR, anar);
399 PHY_WRITE(sc, MII_BMCR, bmcr);
400 if (sc->mii_flags & MIIF_HAVE_GTCR)
401 PHY_WRITE(sc, MII_100T2CR, gtcr);
405 mii_phy_tick(struct mii_softc *sc)
407 struct mii_data *mii = sc->mii_pdata;
408 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
411 /* Just bail now if the interface is down. */
412 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
413 return (EJUSTRETURN);
416 * If we're not doing autonegotiation, we don't need to do
417 * any extra work here. However, we need to check the link
418 * status so we can generate an announcement if the status
421 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
424 /* Read the status register twice; BMSR_LINK is latch-low. */
425 reg = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
426 if (reg & BMSR_LINK) {
431 /* Reset autonegotiation timer. */
437 * Only retry autonegotiation every N seconds.
439 KKASSERT(sc->mii_anegticks > 0);
440 if (++sc->mii_ticks <= sc->mii_anegticks)
441 return (EJUSTRETURN);
444 sc->mii_reset(sc); /* Reset PHY */
445 mii_phy_auto(sc, 0); /* Ignore EJUSTRETURN */
450 mii_phy_statusmsg(struct mii_softc *sc)
452 struct mii_data *mii = sc->mii_pdata;
453 struct ifnet *ifp = mii->mii_ifp;
454 int baudrate, link_state, announce = 0;
456 if (mii->mii_media_status & IFM_AVALID) {
457 if (mii->mii_media_status & IFM_ACTIVE)
458 link_state = LINK_STATE_UP;
460 link_state = LINK_STATE_DOWN;
462 link_state = LINK_STATE_UNKNOWN;
464 baudrate = ifmedia_baudrate(mii->mii_media_active);
466 if (link_state != ifp->if_link_state) {
467 ifp->if_link_state = link_state;
469 * XXX Right here we'd like to notify protocols
470 * XXX that the link status has changed, so that
471 * XXX e.g. Duplicate Address Detection can restart.
476 if (baudrate != ifp->if_baudrate) {
477 ifp->if_baudrate = baudrate;
485 mii_phy_update(struct mii_softc *sc, int cmd)
487 struct mii_data *mii = sc->mii_pdata;
488 struct ifnet *ifp = mii->mii_ifp;
491 if (sc->mii_media_active != mii->mii_media_active ||
492 sc->mii_media_status != mii->mii_media_status ||
493 cmd == MII_MEDIACHG) {
494 announce = mii_phy_statusmsg(sc);
495 MIIBUS_STATCHG(sc->mii_dev);
496 sc->mii_media_active = mii->mii_media_active;
497 sc->mii_media_status = mii->mii_media_status;
501 if_link_state_change(ifp);
508 * Return the flow control status flag from MII_ANAR & MII_ANLPAR.
511 mii_phy_flowstatus(struct mii_softc *sc)
515 if ((sc->mii_flags & MIIF_DOPAUSE) == 0)
518 anar = PHY_READ(sc, MII_ANAR);
519 anlpar = PHY_READ(sc, MII_ANLPAR);
521 if ((anar & ANAR_X_PAUSE_SYM) & (anlpar & ANLPAR_X_PAUSE_SYM))
522 return (IFM_FLOW | IFM_ETH_TXPAUSE | IFM_ETH_RXPAUSE);
524 if ((anar & ANAR_X_PAUSE_SYM) == 0) {
525 if ((anar & ANAR_X_PAUSE_ASYM) &&
526 ((anlpar & ANLPAR_X_PAUSE_TOWARDS) ==
527 ANLPAR_X_PAUSE_TOWARDS))
528 return (IFM_FLOW | IFM_ETH_TXPAUSE);
533 if ((anar & ANAR_X_PAUSE_ASYM) == 0) {
534 if (anlpar & ANLPAR_X_PAUSE_SYM)
535 return (IFM_FLOW | IFM_ETH_TXPAUSE | IFM_ETH_RXPAUSE);
540 switch (anlpar & ANLPAR_X_PAUSE_TOWARDS) {
541 case ANLPAR_X_PAUSE_NONE:
544 case ANLPAR_X_PAUSE_ASYM:
545 return (IFM_FLOW | IFM_ETH_RXPAUSE);
548 return (IFM_FLOW | IFM_ETH_RXPAUSE | IFM_ETH_TXPAUSE);
553 const struct mii_phydesc *
554 mii_phy_match(const struct mii_attach_args *ma, const struct mii_phydesc *mpd)
556 for (; mpd->mpd_name != NULL; mpd++) {
557 if (MII_OUI(ma->mii_id1, ma->mii_id2) == mpd->mpd_oui &&
558 MII_MODEL(ma->mii_id2) == mpd->mpd_model)