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>
57 #include <net/if_media.h>
59 #include <dev/mii/mii.h>
60 #include <dev/mii/miivar.h>
62 #include "miibus_if.h"
65 * Media to register setting conversion table. Order matters.
67 const struct mii_media mii_media_table[MII_NMEDIA] = {
69 { BMCR_ISO, ANAR_CSMA,
73 { BMCR_S10, ANAR_CSMA|ANAR_10,
77 { BMCR_S10|BMCR_FDX, ANAR_CSMA|ANAR_10_FD,
81 { BMCR_S100, ANAR_CSMA|ANAR_T4,
85 { BMCR_S100, ANAR_CSMA|ANAR_TX,
89 { BMCR_S100|BMCR_FDX, ANAR_CSMA|ANAR_TX_FD,
93 { BMCR_S1000, ANAR_CSMA,
97 { BMCR_S1000|BMCR_FDX, ANAR_CSMA,
101 { BMCR_S1000, ANAR_CSMA,
105 { BMCR_S1000, ANAR_CSMA,
110 mii_phy_setmedia(struct mii_softc *sc)
112 struct mii_data *mii = sc->mii_pdata;
113 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
114 int bmcr, anar, gtcr;
116 if (IFM_SUBTYPE(ife->ifm_media) == IFM_AUTO) {
117 if ((PHY_READ(sc, MII_BMCR) & BMCR_AUTOEN) == 0)
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)
219 /* Read the status register twice; BMSR_LINK is latch-low. */
220 reg = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
221 if (reg & BMSR_LINK) {
229 * Only retry autonegotiation every N seconds.
231 if (sc->mii_anegticks == 0) {
232 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.
297 switch (media & (IFM_TMASK|IFM_NMASK|IFM_FDX)) {
298 case IFM_ETHER|IFM_10_T:
299 rv = ANAR_10|ANAR_CSMA;
301 case IFM_ETHER|IFM_10_T|IFM_FDX:
302 rv = ANAR_10_FD|ANAR_CSMA;
304 case IFM_ETHER|IFM_100_TX:
305 rv = ANAR_TX|ANAR_CSMA;
307 case IFM_ETHER|IFM_100_TX|IFM_FDX:
308 rv = ANAR_TX_FD|ANAR_CSMA;
310 case IFM_ETHER|IFM_100_T4:
311 rv = ANAR_T4|ANAR_CSMA;
322 * Given a BMCR value, return the corresponding ifmedia word.
325 mii_media_from_bmcr(int bmcr)
329 if (bmcr & BMCR_S100)
340 * Initialize generic PHY media based on BMSR, called when a PHY is
341 * attached. We expect to be set up to print a comma-separated list
342 * of media names. Does not print a newline.
345 mii_add_media(struct mii_softc *sc)
347 const char *sep = "";
348 struct mii_data *mii;
350 mii = device_get_softc(sc->mii_dev);
351 if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0) {
352 printf("no media present");
356 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL)
357 #define PRINT(s) printf("%s%s", sep, s); sep = ", "
359 if (sc->mii_capabilities & BMSR_10THDX) {
360 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, sc->mii_inst), 0);
363 if (sc->mii_capabilities & BMSR_10TFDX) {
364 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, sc->mii_inst),
366 PRINT("10baseT-FDX");
368 if (sc->mii_capabilities & BMSR_100TXHDX) {
369 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, sc->mii_inst),
373 if (sc->mii_capabilities & BMSR_100TXFDX) {
374 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, sc->mii_inst),
376 PRINT("100baseTX-FDX");
378 if (sc->mii_capabilities & BMSR_100T4) {
380 * XXX How do you enable 100baseT4? I assume we set
381 * XXX BMCR_S100 and then assume the PHYs will take
382 * XXX watever action is necessary to switch themselves
385 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_T4, 0, sc->mii_inst),
389 if (sc->mii_capabilities & BMSR_ANEG) {
390 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst),
402 * Initialize generic PHY media based on BMSR, called when a PHY is
403 * attached. We expect to be set up to print a comma-separated list
404 * of media names. Does not print a newline.
407 mii_phy_add_media(struct mii_softc *sc)
409 struct mii_data *mii = sc->mii_pdata;
410 const char *sep = "";
412 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL)
413 #define PRINT(s) printf("%s%s", sep, s); sep = ", "
415 if ((sc->mii_flags & MIIF_NOISOLATE) == 0)
416 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
420 * There are different interpretations for the bits in
421 * HomePNA PHYs. And there is really only one media type
424 if (sc->mii_flags & MIIF_IS_HPNA) {
425 if (sc->mii_capabilities & BMSR_10THDX) {
426 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_HPNA_1, 0,
434 if (sc->mii_capabilities & BMSR_10THDX) {
435 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, sc->mii_inst),
439 if (sc->mii_capabilities & BMSR_10TFDX) {
440 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, sc->mii_inst),
442 PRINT("10baseT-FDX");
444 if (sc->mii_capabilities & BMSR_100TXHDX) {
445 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, sc->mii_inst),
449 if (sc->mii_capabilities & BMSR_100TXFDX) {
450 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, sc->mii_inst),
451 MII_MEDIA_100_TX_FDX);
452 PRINT("100baseTX-FDX");
454 if (sc->mii_capabilities & BMSR_100T4) {
455 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_T4, 0, sc->mii_inst),
460 if (sc->mii_extcapabilities & EXTSR_MEDIAMASK) {
462 * XXX Right now only handle 1000SX and 1000TX. Need
463 * XXX to handle 1000LX and 1000CX some how.
465 * Note since it can take 5 seconds to auto-negotiate
466 * a gigabit link, we make anegticks 10 seconds for
467 * all the gigabit media types.
469 if (sc->mii_extcapabilities & EXTSR_1000XHDX) {
470 sc->mii_anegticks = 17;
471 sc->mii_flags |= MIIF_IS_1000X;
472 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, 0,
473 sc->mii_inst), MII_MEDIA_1000_X);
476 if (sc->mii_extcapabilities & EXTSR_1000XFDX) {
477 sc->mii_anegticks = 17;
478 sc->mii_flags |= MIIF_IS_1000X;
479 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, IFM_FDX,
480 sc->mii_inst), MII_MEDIA_1000_X_FDX);
481 PRINT("1000baseSX-FDX");
485 * 1000baseT media needs to be able to manipulate
486 * master/slave mode. We set IFM_ETH_MASTER in
487 * the "don't care mask" and filter it out when
490 * All 1000baseT PHYs have a 1000baseT control register.
492 if (sc->mii_extcapabilities & EXTSR_1000THDX) {
493 sc->mii_anegticks = 17;
494 sc->mii_flags |= MIIF_HAVE_GTCR;
495 mii->mii_media.ifm_mask |= IFM_ETH_MASTER;
496 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, 0,
497 sc->mii_inst), MII_MEDIA_1000_T);
500 if (sc->mii_extcapabilities & EXTSR_1000TFDX) {
501 sc->mii_anegticks = 17;
502 sc->mii_flags |= MIIF_HAVE_GTCR;
503 mii->mii_media.ifm_mask |= IFM_ETH_MASTER;
504 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, IFM_FDX,
505 sc->mii_inst), MII_MEDIA_1000_T_FDX);
506 PRINT("1000baseT-FDX");
510 if (sc->mii_capabilities & BMSR_ANEG) {
511 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst),
512 MII_NMEDIA); /* intentionally invalid index */
520 mii_phy_detach(device_t dev)
522 struct mii_softc *sc;
524 sc = device_get_softc(dev);
527 LIST_REMOVE(sc, mii_list);
532 const struct mii_phydesc *
533 mii_phy_match_gen(const struct mii_attach_args *ma,
534 const struct mii_phydesc *mpd, size_t len)
537 for (; mpd->mpd_name != NULL;
538 mpd = (const struct mii_phydesc *) ((const char *) mpd + len)) {
539 if (MII_OUI(ma->mii_id1, ma->mii_id2) == mpd->mpd_oui &&
540 MII_MODEL(ma->mii_id2) == mpd->mpd_model)
546 const struct mii_phydesc *
547 mii_phy_match(const struct mii_attach_args *ma, const struct mii_phydesc *mpd)
549 return (mii_phy_match_gen(ma, mpd, sizeof(struct mii_phydesc)));