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.
41 * Subroutines common to all PHYs.
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/socket.h>
48 #include <sys/errno.h>
49 #include <sys/module.h>
54 #include <net/if_media.h>
56 #include <dev/mii/mii.h>
57 #include <dev/mii/miivar.h>
59 #include "miibus_if.h"
62 static const char rcsid[] =
67 * Media to register setting conversion table. Order matters.
69 const struct mii_media mii_media_table[MII_NMEDIA] = {
71 { BMCR_ISO, ANAR_CSMA,
75 { BMCR_S10, ANAR_CSMA|ANAR_10,
79 { BMCR_S10|BMCR_FDX, ANAR_CSMA|ANAR_10_FD,
83 { BMCR_S100, ANAR_CSMA|ANAR_T4,
87 { BMCR_S100, ANAR_CSMA|ANAR_TX,
91 { BMCR_S100|BMCR_FDX, ANAR_CSMA|ANAR_TX_FD,
95 { BMCR_S1000, ANAR_CSMA,
99 { BMCR_S1000|BMCR_FDX, ANAR_CSMA,
103 { BMCR_S1000, ANAR_CSMA,
107 { BMCR_S1000, ANAR_CSMA,
112 mii_phy_setmedia(struct mii_softc *sc)
114 struct mii_data *mii = sc->mii_pdata;
115 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
116 int bmcr, anar, gtcr;
118 if (IFM_SUBTYPE(ife->ifm_media) == IFM_AUTO) {
119 if ((PHY_READ(sc, MII_BMCR) & BMCR_AUTOEN) == 0)
120 (void) mii_phy_auto(sc);
125 * Table index is stored in the media entry.
128 KASSERT(ife->ifm_data >=0 && ife->ifm_data < MII_NMEDIA,
129 ("invalid ife->ifm_data (0x%x) in mii_phy_setmedia",
132 anar = mii_media_table[ife->ifm_data].mm_anar;
133 bmcr = mii_media_table[ife->ifm_data].mm_bmcr;
134 gtcr = mii_media_table[ife->ifm_data].mm_gtcr;
136 if (mii->mii_media.ifm_media & IFM_ETH_MASTER) {
137 switch (IFM_SUBTYPE(ife->ifm_media)) {
139 gtcr |= GTCR_MAN_MS|GTCR_ADV_MS;
143 panic("mii_phy_setmedia: MASTER on wrong media");
147 if (ife->ifm_media & IFM_LOOP)
150 PHY_WRITE(sc, MII_ANAR, anar);
151 PHY_WRITE(sc, MII_BMCR, bmcr);
152 if (sc->mii_flags & MIIF_HAVE_GTCR)
153 PHY_WRITE(sc, MII_100T2CR, gtcr);
157 mii_phy_auto(struct mii_softc *sc)
161 * Check for 1000BASE-X. Autonegotiation is a bit
162 * different on such devices.
164 if (sc->mii_flags & MIIF_IS_1000X) {
167 if (sc->mii_extcapabilities & EXTSR_1000XFDX)
169 if (sc->mii_extcapabilities & EXTSR_1000XHDX)
172 if (sc->mii_flags & MIIF_DOPAUSE) {
173 /* XXX Asymmetric vs. symmetric? */
174 anar |= ANLPAR_X_PAUSE_TOWARDS;
177 PHY_WRITE(sc, MII_ANAR, anar);
181 anar = BMSR_MEDIA_TO_ANAR(sc->mii_capabilities) |
183 if (sc->mii_flags & MIIF_DOPAUSE)
185 PHY_WRITE(sc, MII_ANAR, anar);
186 if (sc->mii_flags & MIIF_HAVE_GTCR) {
189 if (sc->mii_extcapabilities & EXTSR_1000TFDX)
190 gtcr |= GTCR_ADV_1000TFDX;
191 if (sc->mii_extcapabilities & EXTSR_1000THDX)
192 gtcr |= GTCR_ADV_1000THDX;
194 PHY_WRITE(sc, MII_100T2CR, gtcr);
197 PHY_WRITE(sc, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG);
198 return (EJUSTRETURN);
202 mii_phy_tick(struct mii_softc *sc)
204 struct ifmedia_entry *ife = sc->mii_pdata->mii_media.ifm_cur;
205 struct ifnet *ifp = sc->mii_pdata->mii_ifp;
208 /* Just bail now if the interface is down. */
209 if ((ifp->if_flags & IFF_UP) == 0)
210 return (EJUSTRETURN);
213 * If we're not doing autonegotiation, we don't need to do
214 * any extra work here. However, we need to check the link
215 * status so we can generate an announcement if the status
218 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
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) {
231 * Only retry autonegotiation every N seconds.
233 if (sc->mii_anegticks == 0)
234 sc->mii_anegticks = 17;
235 if (++sc->mii_ticks != sc->mii_anegticks)
236 return (EJUSTRETURN);
245 mii_phy_reset(struct mii_softc *sc)
249 if (sc->mii_flags & MIIF_NOISOLATE)
252 reg = BMCR_RESET | BMCR_ISO;
253 PHY_WRITE(sc, MII_BMCR, reg);
255 /* Wait 100ms for it to complete. */
256 for (i = 0; i < 100; i++) {
257 reg = PHY_READ(sc, MII_BMCR);
258 if ((reg & BMCR_RESET) == 0)
263 if (sc->mii_inst != 0 && ((sc->mii_flags & MIIF_NOISOLATE) == 0))
264 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
268 mii_phy_down(struct mii_softc *sc)
274 mii_phy_update(struct mii_softc *sc, int cmd)
276 struct mii_data *mii = sc->mii_pdata;
278 if (sc->mii_media_active != mii->mii_media_active ||
279 cmd == MII_MEDIACHG) {
280 MIIBUS_STATCHG(sc->mii_dev);
281 sc->mii_media_active = mii->mii_media_active;
283 if (sc->mii_media_status != mii->mii_media_status) {
284 MIIBUS_LINKCHG(sc->mii_dev);
285 sc->mii_media_status = mii->mii_media_status;
290 * Given an ifmedia word, return the corresponding ANAR value.
298 switch (media & (IFM_TMASK|IFM_NMASK|IFM_FDX)) {
299 case IFM_ETHER|IFM_10_T:
300 rv = ANAR_10|ANAR_CSMA;
302 case IFM_ETHER|IFM_10_T|IFM_FDX:
303 rv = ANAR_10_FD|ANAR_CSMA;
305 case IFM_ETHER|IFM_100_TX:
306 rv = ANAR_TX|ANAR_CSMA;
308 case IFM_ETHER|IFM_100_TX|IFM_FDX:
309 rv = ANAR_TX_FD|ANAR_CSMA;
311 case IFM_ETHER|IFM_100_T4:
312 rv = ANAR_T4|ANAR_CSMA;
323 * Given a BMCR value, return the corresponding ifmedia word.
326 mii_media_from_bmcr(bmcr)
331 if (bmcr & BMCR_S100)
342 * Initialize generic PHY media based on BMSR, called when a PHY is
343 * attached. We expect to be set up to print a comma-separated list
344 * of media names. Does not print a newline.
347 mii_add_media(struct mii_softc *sc)
349 const char *sep = "";
350 struct mii_data *mii;
352 mii = device_get_softc(sc->mii_dev);
353 if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0) {
354 printf("no media present");
358 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL)
359 #define PRINT(s) printf("%s%s", sep, s); sep = ", "
361 if (sc->mii_capabilities & BMSR_10THDX) {
362 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, sc->mii_inst), 0);
365 if (sc->mii_capabilities & BMSR_10TFDX) {
366 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, sc->mii_inst),
368 PRINT("10baseT-FDX");
370 if (sc->mii_capabilities & BMSR_100TXHDX) {
371 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, sc->mii_inst),
375 if (sc->mii_capabilities & BMSR_100TXFDX) {
376 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, sc->mii_inst),
378 PRINT("100baseTX-FDX");
380 if (sc->mii_capabilities & BMSR_100T4) {
382 * XXX How do you enable 100baseT4? I assume we set
383 * XXX BMCR_S100 and then assume the PHYs will take
384 * XXX watever action is necessary to switch themselves
387 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_T4, 0, sc->mii_inst),
391 if (sc->mii_capabilities & BMSR_ANEG) {
392 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst),
404 * Initialize generic PHY media based on BMSR, called when a PHY is
405 * attached. We expect to be set up to print a comma-separated list
406 * of media names. Does not print a newline.
409 mii_phy_add_media(struct mii_softc *sc)
411 struct mii_data *mii = sc->mii_pdata;
412 const char *sep = "";
414 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL)
415 #define PRINT(s) printf("%s%s", sep, s); sep = ", "
417 if ((sc->mii_flags & MIIF_NOISOLATE) == 0)
418 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
422 * There are different interpretations for the bits in
423 * HomePNA PHYs. And there is really only one media type
426 if (sc->mii_flags & MIIF_IS_HPNA) {
427 if (sc->mii_capabilities & BMSR_10THDX) {
428 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_HPNA_1, 0,
436 if (sc->mii_capabilities & BMSR_10THDX) {
437 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, sc->mii_inst),
441 if (sc->mii_capabilities & BMSR_10TFDX) {
442 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, sc->mii_inst),
444 PRINT("10baseT-FDX");
446 if (sc->mii_capabilities & BMSR_100TXHDX) {
447 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, sc->mii_inst),
451 if (sc->mii_capabilities & BMSR_100TXFDX) {
452 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, sc->mii_inst),
453 MII_MEDIA_100_TX_FDX);
454 PRINT("100baseTX-FDX");
456 if (sc->mii_capabilities & BMSR_100T4) {
457 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_T4, 0, sc->mii_inst),
462 if (sc->mii_extcapabilities & EXTSR_MEDIAMASK) {
464 * XXX Right now only handle 1000SX and 1000TX. Need
465 * XXX to handle 1000LX and 1000CX some how.
467 * Note since it can take 5 seconds to auto-negotiate
468 * a gigabit link, we make anegticks 10 seconds for
469 * all the gigabit media types.
471 if (sc->mii_extcapabilities & EXTSR_1000XHDX) {
472 sc->mii_anegticks = 17;
473 sc->mii_flags |= MIIF_IS_1000X;
474 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, 0,
475 sc->mii_inst), MII_MEDIA_1000_X);
478 if (sc->mii_extcapabilities & EXTSR_1000XFDX) {
479 sc->mii_anegticks = 17;
480 sc->mii_flags |= MIIF_IS_1000X;
481 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, IFM_FDX,
482 sc->mii_inst), MII_MEDIA_1000_X_FDX);
483 PRINT("1000baseSX-FDX");
487 * 1000baseT media needs to be able to manipulate
488 * master/slave mode. We set IFM_ETH_MASTER in
489 * the "don't care mask" and filter it out when
492 * All 1000baseT PHYs have a 1000baseT control register.
494 if (sc->mii_extcapabilities & EXTSR_1000THDX) {
495 sc->mii_anegticks = 17;
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, 0,
499 sc->mii_inst), MII_MEDIA_1000_T);
502 if (sc->mii_extcapabilities & EXTSR_1000TFDX) {
503 sc->mii_anegticks = 17;
504 sc->mii_flags |= MIIF_HAVE_GTCR;
505 mii->mii_media.ifm_mask |= IFM_ETH_MASTER;
506 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, IFM_FDX,
507 sc->mii_inst), MII_MEDIA_1000_T_FDX);
508 PRINT("1000baseT-FDX");
512 if (sc->mii_capabilities & BMSR_ANEG) {
513 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst),
514 MII_NMEDIA); /* intentionally invalid index */
522 mii_phy_detach(device_t dev)
524 struct mii_softc *sc;
525 struct mii_data *mii;
527 sc = device_get_softc(dev);
528 mii = device_get_softc(device_get_parent(dev));
531 LIST_REMOVE(sc, mii_list);
536 const struct mii_phydesc *
537 mii_phy_match(const struct mii_attach_args *ma, const struct mii_phydesc *mpd)
540 for (; mpd->mpd_name != NULL; mpd++) {
541 if (MII_OUI(ma->mii_id1, ma->mii_id2) == mpd->mpd_oui &&
542 MII_MODEL(ma->mii_id2) == mpd->mpd_model)