1 /* $NetBSD: mii_physubr.c,v 1.5 1999/08/03 19:41:49 drochner 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.
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
44 * Subroutines common to all PHYs.
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/socket.h>
51 #include <sys/errno.h>
52 #include <sys/module.h>
56 #include <net/if_media.h>
58 #include <dev/mii/mii.h>
59 #include <dev/mii/miivar.h>
61 #include "miibus_if.h"
64 * Media to register setting conversion table. Order matters.
66 const struct mii_media mii_media_table[MII_NMEDIA] = {
68 { BMCR_ISO, ANAR_CSMA,
72 { BMCR_S10, ANAR_CSMA|ANAR_10,
76 { BMCR_S10|BMCR_FDX, ANAR_CSMA|ANAR_10_FD,
80 { BMCR_S100, ANAR_CSMA|ANAR_T4,
84 { BMCR_S100, ANAR_CSMA|ANAR_TX,
88 { BMCR_S100|BMCR_FDX, ANAR_CSMA|ANAR_TX_FD,
92 { BMCR_S1000, ANAR_CSMA,
96 { BMCR_S1000|BMCR_FDX, ANAR_CSMA,
100 { BMCR_S1000, ANAR_CSMA,
104 { BMCR_S1000, ANAR_CSMA,
109 mii_phy_setmedia(struct mii_softc *sc)
111 struct mii_data *mii = sc->mii_pdata;
112 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
113 int bmcr, anar, gtcr;
115 if (IFM_SUBTYPE(ife->ifm_media) == IFM_AUTO) {
116 if ((PHY_READ(sc, MII_BMCR) & BMCR_AUTOEN) == 0 ||
117 (sc->mii_flags & MIIF_FORCEANEG))
118 (void) mii_phy_auto(sc);
123 * Table index is stored in the media entry.
126 KASSERT(ife->ifm_data >=0 && ife->ifm_data < MII_NMEDIA,
127 ("invalid ife->ifm_data (0x%x) in mii_phy_setmedia",
130 anar = mii_media_table[ife->ifm_data].mm_anar;
131 bmcr = mii_media_table[ife->ifm_data].mm_bmcr;
132 gtcr = mii_media_table[ife->ifm_data].mm_gtcr;
134 if (mii->mii_media.ifm_media & IFM_ETH_MASTER) {
135 switch (IFM_SUBTYPE(ife->ifm_media)) {
137 gtcr |= GTCR_MAN_MS|GTCR_ADV_MS;
141 panic("mii_phy_setmedia: MASTER on wrong media");
145 if (ife->ifm_media & IFM_LOOP)
148 PHY_WRITE(sc, MII_ANAR, anar);
149 PHY_WRITE(sc, MII_BMCR, bmcr);
150 if (sc->mii_flags & MIIF_HAVE_GTCR)
151 PHY_WRITE(sc, MII_100T2CR, gtcr);
155 mii_phy_auto(struct mii_softc *sc)
159 * Check for 1000BASE-X. Autonegotiation is a bit
160 * different on such devices.
162 if (sc->mii_flags & MIIF_IS_1000X) {
165 if (sc->mii_extcapabilities & EXTSR_1000XFDX)
167 if (sc->mii_extcapabilities & EXTSR_1000XHDX)
170 if (sc->mii_flags & MIIF_DOPAUSE) {
171 /* XXX Asymmetric vs. symmetric? */
172 anar |= ANLPAR_X_PAUSE_TOWARDS;
175 PHY_WRITE(sc, MII_ANAR, anar);
179 anar = BMSR_MEDIA_TO_ANAR(sc->mii_capabilities) |
181 if (sc->mii_flags & MIIF_DOPAUSE)
183 PHY_WRITE(sc, MII_ANAR, anar);
184 if (sc->mii_flags & MIIF_HAVE_GTCR) {
187 if (sc->mii_extcapabilities & EXTSR_1000TFDX)
188 gtcr |= GTCR_ADV_1000TFDX;
189 if (sc->mii_extcapabilities & EXTSR_1000THDX)
190 gtcr |= GTCR_ADV_1000THDX;
192 PHY_WRITE(sc, MII_100T2CR, gtcr);
195 PHY_WRITE(sc, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG);
196 return (EJUSTRETURN);
200 mii_phy_tick(struct mii_softc *sc)
202 struct ifmedia_entry *ife = sc->mii_pdata->mii_media.ifm_cur;
203 struct ifnet *ifp = sc->mii_pdata->mii_ifp;
206 /* Just bail now if the interface is down. */
207 if ((ifp->if_flags & IFF_UP) == 0)
208 return (EJUSTRETURN);
211 * If we're not doing autonegotiation, we don't need to do
212 * any extra work here. However, we need to check the link
213 * status so we can generate an announcement if the status
216 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) {
217 sc->mii_ticks = 0; /* reset autonegotiation timer. */
221 /* Read the status register twice; BMSR_LINK is latch-low. */
222 reg = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
223 if (reg & BMSR_LINK) {
224 sc->mii_ticks = 0; /* reset autonegotiation timer. */
229 /* Announce link loss right after it happens */
230 if (sc->mii_ticks++ == 0)
233 /* XXX: use default value if phy driver did not set mii_anegticks */
234 if (sc->mii_anegticks == 0)
235 sc->mii_anegticks = MII_ANEGTICKS_GIGE;
237 /* Only retry autonegotiation every mii_anegticks ticks. */
238 if (sc->mii_ticks <= sc->mii_anegticks)
239 return (EJUSTRETURN);
248 mii_phy_reset(struct mii_softc *sc)
250 struct ifmedia_entry *ife = sc->mii_pdata->mii_media.ifm_cur;
253 if (sc->mii_flags & MIIF_NOISOLATE)
256 reg = BMCR_RESET | BMCR_ISO;
257 PHY_WRITE(sc, MII_BMCR, reg);
259 /* Wait 100ms for it to complete. */
260 for (i = 0; i < 100; i++) {
261 reg = PHY_READ(sc, MII_BMCR);
262 if ((reg & BMCR_RESET) == 0)
267 if ((sc->mii_flags & MIIF_NOISOLATE) == 0) {
268 if ((ife == NULL && sc->mii_inst != 0) ||
269 (ife != NULL && IFM_INST(ife->ifm_media) != sc->mii_inst))
270 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
275 mii_phy_down(struct mii_softc *sc)
281 mii_phy_update(struct mii_softc *sc, int cmd)
283 struct mii_data *mii = sc->mii_pdata;
285 if (sc->mii_media_active != mii->mii_media_active ||
286 cmd == MII_MEDIACHG) {
287 MIIBUS_STATCHG(sc->mii_dev);
288 sc->mii_media_active = mii->mii_media_active;
290 if (sc->mii_media_status != mii->mii_media_status) {
291 MIIBUS_LINKCHG(sc->mii_dev);
292 sc->mii_media_status = mii->mii_media_status;
297 * Given an ifmedia word, return the corresponding ANAR value.
304 switch (media & (IFM_TMASK|IFM_NMASK|IFM_FDX)) {
305 case IFM_ETHER|IFM_10_T:
306 rv = ANAR_10|ANAR_CSMA;
308 case IFM_ETHER|IFM_10_T|IFM_FDX:
309 rv = ANAR_10_FD|ANAR_CSMA;
311 case IFM_ETHER|IFM_100_TX:
312 rv = ANAR_TX|ANAR_CSMA;
314 case IFM_ETHER|IFM_100_TX|IFM_FDX:
315 rv = ANAR_TX_FD|ANAR_CSMA;
317 case IFM_ETHER|IFM_100_T4:
318 rv = ANAR_T4|ANAR_CSMA;
329 * Initialize generic PHY media based on BMSR, called when a PHY is
330 * attached. We expect to be set up to print a comma-separated list
331 * of media names. Does not print a newline.
334 mii_add_media(struct mii_softc *sc)
336 const char *sep = "";
337 struct mii_data *mii;
339 mii = device_get_softc(sc->mii_dev);
340 if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0) {
341 printf("no media present");
345 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL)
346 #define PRINT(s) printf("%s%s", sep, s); sep = ", "
348 if (sc->mii_capabilities & BMSR_10THDX) {
349 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, sc->mii_inst), 0);
352 if (sc->mii_capabilities & BMSR_10TFDX) {
353 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, sc->mii_inst),
355 PRINT("10baseT-FDX");
357 if (sc->mii_capabilities & BMSR_100TXHDX) {
358 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, sc->mii_inst),
362 if (sc->mii_capabilities & BMSR_100TXFDX) {
363 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, sc->mii_inst),
365 PRINT("100baseTX-FDX");
367 if (sc->mii_capabilities & BMSR_100T4) {
369 * XXX How do you enable 100baseT4? I assume we set
370 * XXX BMCR_S100 and then assume the PHYs will take
371 * XXX watever action is necessary to switch themselves
374 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_T4, 0, sc->mii_inst),
378 if (sc->mii_capabilities & BMSR_ANEG) {
379 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst),
391 * Initialize generic PHY media based on BMSR, called when a PHY is
392 * attached. We expect to be set up to print a comma-separated list
393 * of media names. Does not print a newline.
396 mii_phy_add_media(struct mii_softc *sc)
398 struct mii_data *mii = sc->mii_pdata;
399 const char *sep = "";
401 if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0 &&
402 (sc->mii_extcapabilities & EXTSR_MEDIAMASK) == 0) {
403 printf("no media present");
407 /* Set aneg timer for 10/100 media. Gigabit media handled below. */
408 sc->mii_anegticks = MII_ANEGTICKS;
410 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL)
411 #define PRINT(s) printf("%s%s", sep, s); sep = ", "
413 if ((sc->mii_flags & MIIF_NOISOLATE) == 0)
414 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
418 * There are different interpretations for the bits in
419 * HomePNA PHYs. And there is really only one media type
422 if (sc->mii_flags & MIIF_IS_HPNA) {
423 if (sc->mii_capabilities & BMSR_10THDX) {
424 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_HPNA_1, 0,
432 if (sc->mii_capabilities & BMSR_10THDX) {
433 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, sc->mii_inst),
437 if (sc->mii_capabilities & BMSR_10TFDX) {
438 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, sc->mii_inst),
440 PRINT("10baseT-FDX");
442 if (sc->mii_capabilities & BMSR_100TXHDX) {
443 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, sc->mii_inst),
447 if (sc->mii_capabilities & BMSR_100TXFDX) {
448 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, sc->mii_inst),
449 MII_MEDIA_100_TX_FDX);
450 PRINT("100baseTX-FDX");
452 if (sc->mii_capabilities & BMSR_100T4) {
453 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_T4, 0, sc->mii_inst),
458 if (sc->mii_extcapabilities & EXTSR_MEDIAMASK) {
460 * XXX Right now only handle 1000SX and 1000TX. Need
461 * XXX to handle 1000LX and 1000CX some how.
463 if (sc->mii_extcapabilities & EXTSR_1000XHDX) {
464 sc->mii_anegticks = MII_ANEGTICKS_GIGE;
465 sc->mii_flags |= MIIF_IS_1000X;
466 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, 0,
467 sc->mii_inst), MII_MEDIA_1000_X);
470 if (sc->mii_extcapabilities & EXTSR_1000XFDX) {
471 sc->mii_anegticks = MII_ANEGTICKS_GIGE;
472 sc->mii_flags |= MIIF_IS_1000X;
473 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, IFM_FDX,
474 sc->mii_inst), MII_MEDIA_1000_X_FDX);
475 PRINT("1000baseSX-FDX");
479 * 1000baseT media needs to be able to manipulate
480 * master/slave mode. We set IFM_ETH_MASTER in
481 * the "don't care mask" and filter it out when
484 * All 1000baseT PHYs have a 1000baseT control register.
486 if (sc->mii_extcapabilities & EXTSR_1000THDX) {
487 sc->mii_anegticks = MII_ANEGTICKS_GIGE;
488 sc->mii_flags |= MIIF_HAVE_GTCR;
489 mii->mii_media.ifm_mask |= IFM_ETH_MASTER;
490 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, 0,
491 sc->mii_inst), MII_MEDIA_1000_T);
494 if (sc->mii_extcapabilities & EXTSR_1000TFDX) {
495 sc->mii_anegticks = MII_ANEGTICKS_GIGE;
496 sc->mii_flags |= MIIF_HAVE_GTCR;
497 mii->mii_media.ifm_mask |= IFM_ETH_MASTER;
498 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, IFM_FDX,
499 sc->mii_inst), MII_MEDIA_1000_T_FDX);
500 PRINT("1000baseT-FDX");
504 if (sc->mii_capabilities & BMSR_ANEG) {
505 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst),
506 MII_NMEDIA); /* intentionally invalid index */
514 mii_phy_detach(device_t dev)
516 struct mii_softc *sc;
518 sc = device_get_softc(dev);
521 LIST_REMOVE(sc, mii_list);
526 const struct mii_phydesc *
527 mii_phy_match_gen(const struct mii_attach_args *ma,
528 const struct mii_phydesc *mpd, size_t len)
531 for (; mpd->mpd_name != NULL;
532 mpd = (const struct mii_phydesc *) ((const char *) mpd + len)) {
533 if (MII_OUI(ma->mii_id1, ma->mii_id2) == mpd->mpd_oui &&
534 MII_MODEL(ma->mii_id2) == mpd->mpd_model)
540 const struct mii_phydesc *
541 mii_phy_match(const struct mii_attach_args *ma, const struct mii_phydesc *mpd)
544 return (mii_phy_match_gen(ma, mpd, sizeof(struct mii_phydesc)));
548 mii_phy_dev_probe(device_t dev, const struct mii_phydesc *mpd, int mrv)
551 mpd = mii_phy_match(device_get_ivars(dev), mpd);
553 device_set_desc(dev, mpd->mpd_name);