1 /* $NetBSD: mii_physubr.c,v 1.5 1999/08/03 19:41:49 drochner Exp $ */ 2 3 /*- 4 * Copyright (c) 1998, 1999, 2000, 2001 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 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. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 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. 26 * 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. 38 */ 39 40 #include <sys/cdefs.h> 41 __FBSDID("$FreeBSD$"); 42 43 /* 44 * Subroutines common to all PHYs. 45 */ 46 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> 53 #include <sys/bus.h> 54 55 #include <net/if.h> 56 #include <net/if_media.h> 57 58 #include <dev/mii/mii.h> 59 #include <dev/mii/miivar.h> 60 61 #include "miibus_if.h" 62 63 /* 64 * Media to register setting conversion table. Order matters. 65 */ 66 const struct mii_media mii_media_table[MII_NMEDIA] = { 67 /* None */ 68 { BMCR_ISO, ANAR_CSMA, 69 0, }, 70 71 /* 10baseT */ 72 { BMCR_S10, ANAR_CSMA|ANAR_10, 73 0, }, 74 75 /* 10baseT-FDX */ 76 { BMCR_S10|BMCR_FDX, ANAR_CSMA|ANAR_10_FD, 77 0, }, 78 79 /* 100baseT4 */ 80 { BMCR_S100, ANAR_CSMA|ANAR_T4, 81 0, }, 82 83 /* 100baseTX */ 84 { BMCR_S100, ANAR_CSMA|ANAR_TX, 85 0, }, 86 87 /* 100baseTX-FDX */ 88 { BMCR_S100|BMCR_FDX, ANAR_CSMA|ANAR_TX_FD, 89 0, }, 90 91 /* 1000baseX */ 92 { BMCR_S1000, ANAR_CSMA, 93 0, }, 94 95 /* 1000baseX-FDX */ 96 { BMCR_S1000|BMCR_FDX, ANAR_CSMA, 97 0, }, 98 99 /* 1000baseT */ 100 { BMCR_S1000, ANAR_CSMA, 101 GTCR_ADV_1000THDX }, 102 103 /* 1000baseT-FDX */ 104 { BMCR_S1000, ANAR_CSMA, 105 GTCR_ADV_1000TFDX }, 106 }; 107 108 void 109 mii_phy_setmedia(struct mii_softc *sc) 110 { 111 struct mii_data *mii = sc->mii_pdata; 112 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 113 int bmcr, anar, gtcr; 114 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); 119 return; 120 } 121 122 /* 123 * Table index is stored in the media entry. 124 */ 125 126 KASSERT(ife->ifm_data >=0 && ife->ifm_data < MII_NMEDIA, 127 ("invalid ife->ifm_data (0x%x) in mii_phy_setmedia", 128 ife->ifm_data)); 129 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; 133 134 if (mii->mii_media.ifm_media & IFM_ETH_MASTER) { 135 switch (IFM_SUBTYPE(ife->ifm_media)) { 136 case IFM_1000_T: 137 gtcr |= GTCR_MAN_MS|GTCR_ADV_MS; 138 break; 139 140 default: 141 panic("mii_phy_setmedia: MASTER on wrong media"); 142 } 143 } 144 145 if (ife->ifm_media & IFM_LOOP) 146 bmcr |= BMCR_LOOP; 147 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); 152 } 153 154 int 155 mii_phy_auto(struct mii_softc *sc) 156 { 157 158 /* 159 * Check for 1000BASE-X. Autonegotiation is a bit 160 * different on such devices. 161 */ 162 if (sc->mii_flags & MIIF_IS_1000X) { 163 uint16_t anar = 0; 164 165 if (sc->mii_extcapabilities & EXTSR_1000XFDX) 166 anar |= ANAR_X_FD; 167 if (sc->mii_extcapabilities & EXTSR_1000XHDX) 168 anar |= ANAR_X_HD; 169 170 if (sc->mii_flags & MIIF_DOPAUSE) { 171 /* XXX Asymmetric vs. symmetric? */ 172 anar |= ANLPAR_X_PAUSE_TOWARDS; 173 } 174 175 PHY_WRITE(sc, MII_ANAR, anar); 176 } else { 177 uint16_t anar; 178 179 anar = BMSR_MEDIA_TO_ANAR(sc->mii_capabilities) | 180 ANAR_CSMA; 181 if (sc->mii_flags & MIIF_DOPAUSE) 182 anar |= ANAR_FC; 183 PHY_WRITE(sc, MII_ANAR, anar); 184 if (sc->mii_flags & MIIF_HAVE_GTCR) { 185 uint16_t gtcr = 0; 186 187 if (sc->mii_extcapabilities & EXTSR_1000TFDX) 188 gtcr |= GTCR_ADV_1000TFDX; 189 if (sc->mii_extcapabilities & EXTSR_1000THDX) 190 gtcr |= GTCR_ADV_1000THDX; 191 192 PHY_WRITE(sc, MII_100T2CR, gtcr); 193 } 194 } 195 PHY_WRITE(sc, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG); 196 return (EJUSTRETURN); 197 } 198 199 int 200 mii_phy_tick(struct mii_softc *sc) 201 { 202 struct ifmedia_entry *ife = sc->mii_pdata->mii_media.ifm_cur; 203 struct ifnet *ifp = sc->mii_pdata->mii_ifp; 204 int reg; 205 206 /* Just bail now if the interface is down. */ 207 if ((ifp->if_flags & IFF_UP) == 0) 208 return (EJUSTRETURN); 209 210 /* 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 214 * changes. 215 */ 216 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) { 217 sc->mii_ticks = 0; /* reset autonegotiation timer. */ 218 return (0); 219 } 220 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. */ 225 /* See above. */ 226 return (0); 227 } 228 229 /* Announce link loss right after it happens */ 230 if (sc->mii_ticks++ == 0) 231 return (0); 232 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; 236 237 /* Only retry autonegotiation every mii_anegticks ticks. */ 238 if (sc->mii_ticks <= sc->mii_anegticks) 239 return (EJUSTRETURN); 240 241 sc->mii_ticks = 0; 242 mii_phy_reset(sc); 243 mii_phy_auto(sc); 244 return (0); 245 } 246 247 void 248 mii_phy_reset(struct mii_softc *sc) 249 { 250 struct ifmedia_entry *ife = sc->mii_pdata->mii_media.ifm_cur; 251 int reg, i; 252 253 if (sc->mii_flags & MIIF_NOISOLATE) 254 reg = BMCR_RESET; 255 else 256 reg = BMCR_RESET | BMCR_ISO; 257 PHY_WRITE(sc, MII_BMCR, reg); 258 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) 263 break; 264 DELAY(1000); 265 } 266 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); 271 } 272 } 273 274 void 275 mii_phy_down(struct mii_softc *sc) 276 { 277 278 } 279 280 void 281 mii_phy_update(struct mii_softc *sc, int cmd) 282 { 283 struct mii_data *mii = sc->mii_pdata; 284 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; 289 } 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; 293 } 294 } 295 296 /* 297 * Given an ifmedia word, return the corresponding ANAR value. 298 */ 299 int 300 mii_anar(int media) 301 { 302 int rv; 303 304 switch (media & (IFM_TMASK|IFM_NMASK|IFM_FDX)) { 305 case IFM_ETHER|IFM_10_T: 306 rv = ANAR_10|ANAR_CSMA; 307 break; 308 case IFM_ETHER|IFM_10_T|IFM_FDX: 309 rv = ANAR_10_FD|ANAR_CSMA; 310 break; 311 case IFM_ETHER|IFM_100_TX: 312 rv = ANAR_TX|ANAR_CSMA; 313 break; 314 case IFM_ETHER|IFM_100_TX|IFM_FDX: 315 rv = ANAR_TX_FD|ANAR_CSMA; 316 break; 317 case IFM_ETHER|IFM_100_T4: 318 rv = ANAR_T4|ANAR_CSMA; 319 break; 320 default: 321 rv = 0; 322 break; 323 } 324 325 return (rv); 326 } 327 328 /* 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. 332 */ 333 void 334 mii_add_media(struct mii_softc *sc) 335 { 336 const char *sep = ""; 337 struct mii_data *mii; 338 339 mii = device_get_softc(sc->mii_dev); 340 if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0) { 341 printf("no media present"); 342 return; 343 } 344 345 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL) 346 #define PRINT(s) printf("%s%s", sep, s); sep = ", " 347 348 if (sc->mii_capabilities & BMSR_10THDX) { 349 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, sc->mii_inst), 0); 350 PRINT("10baseT"); 351 } 352 if (sc->mii_capabilities & BMSR_10TFDX) { 353 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, sc->mii_inst), 354 BMCR_FDX); 355 PRINT("10baseT-FDX"); 356 } 357 if (sc->mii_capabilities & BMSR_100TXHDX) { 358 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, sc->mii_inst), 359 BMCR_S100); 360 PRINT("100baseTX"); 361 } 362 if (sc->mii_capabilities & BMSR_100TXFDX) { 363 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, sc->mii_inst), 364 BMCR_S100|BMCR_FDX); 365 PRINT("100baseTX-FDX"); 366 } 367 if (sc->mii_capabilities & BMSR_100T4) { 368 /* 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 372 * XXX into T4 mode. 373 */ 374 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_T4, 0, sc->mii_inst), 375 BMCR_S100); 376 PRINT("100baseT4"); 377 } 378 if (sc->mii_capabilities & BMSR_ANEG) { 379 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst), 380 BMCR_AUTOEN); 381 PRINT("auto"); 382 } 383 384 385 386 #undef ADD 387 #undef PRINT 388 } 389 390 /* 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. 394 */ 395 void 396 mii_phy_add_media(struct mii_softc *sc) 397 { 398 struct mii_data *mii = sc->mii_pdata; 399 const char *sep = ""; 400 401 if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0 && 402 (sc->mii_extcapabilities & EXTSR_MEDIAMASK) == 0) { 403 printf("no media present"); 404 return; 405 } 406 407 /* Set aneg timer for 10/100 media. Gigabit media handled below. */ 408 sc->mii_anegticks = MII_ANEGTICKS; 409 410 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL) 411 #define PRINT(s) printf("%s%s", sep, s); sep = ", " 412 413 if ((sc->mii_flags & MIIF_NOISOLATE) == 0) 414 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst), 415 MII_MEDIA_NONE); 416 417 /* 418 * There are different interpretations for the bits in 419 * HomePNA PHYs. And there is really only one media type 420 * that is supported. 421 */ 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, 425 sc->mii_inst), 426 MII_MEDIA_10_T); 427 PRINT("HomePNA1"); 428 } 429 return; 430 } 431 432 if (sc->mii_capabilities & BMSR_10THDX) { 433 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, sc->mii_inst), 434 MII_MEDIA_10_T); 435 PRINT("10baseT"); 436 } 437 if (sc->mii_capabilities & BMSR_10TFDX) { 438 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, sc->mii_inst), 439 MII_MEDIA_10_T_FDX); 440 PRINT("10baseT-FDX"); 441 } 442 if (sc->mii_capabilities & BMSR_100TXHDX) { 443 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, sc->mii_inst), 444 MII_MEDIA_100_TX); 445 PRINT("100baseTX"); 446 } 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"); 451 } 452 if (sc->mii_capabilities & BMSR_100T4) { 453 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_T4, 0, sc->mii_inst), 454 MII_MEDIA_100_T4); 455 PRINT("100baseT4"); 456 } 457 458 if (sc->mii_extcapabilities & EXTSR_MEDIAMASK) { 459 /* 460 * XXX Right now only handle 1000SX and 1000TX. Need 461 * XXX to handle 1000LX and 1000CX some how. 462 */ 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); 468 PRINT("1000baseSX"); 469 } 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"); 476 } 477 478 /* 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 482 * the media is set. 483 * 484 * All 1000baseT PHYs have a 1000baseT control register. 485 */ 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); 492 PRINT("1000baseT"); 493 } 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"); 501 } 502 } 503 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 */ 507 PRINT("auto"); 508 } 509 #undef ADD 510 #undef PRINT 511 } 512 513 int 514 mii_phy_detach(device_t dev) 515 { 516 struct mii_softc *sc; 517 518 sc = device_get_softc(dev); 519 mii_phy_down(sc); 520 sc->mii_dev = NULL; 521 LIST_REMOVE(sc, mii_list); 522 523 return(0); 524 } 525 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) 529 { 530 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) 535 return (mpd); 536 } 537 return (NULL); 538 } 539 540 const struct mii_phydesc * 541 mii_phy_match(const struct mii_attach_args *ma, const struct mii_phydesc *mpd) 542 { 543 544 return (mii_phy_match_gen(ma, mpd, sizeof(struct mii_phydesc))); 545 } 546 547 int 548 mii_phy_dev_probe(device_t dev, const struct mii_phydesc *mpd, int mrv) 549 { 550 551 mpd = mii_phy_match(device_get_ivars(dev), mpd); 552 if (mpd != NULL) { 553 device_set_desc(dev, mpd->mpd_name); 554 return (mrv); 555 } 556 557 return (ENXIO); 558 } 559