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 /* 41 * Subroutines common to all PHYs. 42 */ 43 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> 50 #include <sys/bus.h> 51 52 53 #include <net/if.h> 54 #include <net/if_media.h> 55 56 #include <dev/mii/mii.h> 57 #include <dev/mii/miivar.h> 58 59 #include "miibus_if.h" 60 61 #if !defined(lint) 62 static const char rcsid[] = 63 "$FreeBSD$"; 64 #endif 65 66 /* 67 * Media to register setting conversion table. Order matters. 68 */ 69 const struct mii_media mii_media_table[MII_NMEDIA] = { 70 /* None */ 71 { BMCR_ISO, ANAR_CSMA, 72 0, }, 73 74 /* 10baseT */ 75 { BMCR_S10, ANAR_CSMA|ANAR_10, 76 0, }, 77 78 /* 10baseT-FDX */ 79 { BMCR_S10|BMCR_FDX, ANAR_CSMA|ANAR_10_FD, 80 0, }, 81 82 /* 100baseT4 */ 83 { BMCR_S100, ANAR_CSMA|ANAR_T4, 84 0, }, 85 86 /* 100baseTX */ 87 { BMCR_S100, ANAR_CSMA|ANAR_TX, 88 0, }, 89 90 /* 100baseTX-FDX */ 91 { BMCR_S100|BMCR_FDX, ANAR_CSMA|ANAR_TX_FD, 92 0, }, 93 94 /* 1000baseX */ 95 { BMCR_S1000, ANAR_CSMA, 96 0, }, 97 98 /* 1000baseX-FDX */ 99 { BMCR_S1000|BMCR_FDX, ANAR_CSMA, 100 0, }, 101 102 /* 1000baseT */ 103 { BMCR_S1000, ANAR_CSMA, 104 GTCR_ADV_1000THDX }, 105 106 /* 1000baseT-FDX */ 107 { BMCR_S1000, ANAR_CSMA, 108 GTCR_ADV_1000TFDX }, 109 }; 110 111 void 112 mii_phy_setmedia(struct mii_softc *sc) 113 { 114 struct mii_data *mii = sc->mii_pdata; 115 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 116 int bmcr, anar, gtcr; 117 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); 121 return; 122 } 123 124 /* 125 * Table index is stored in the media entry. 126 */ 127 128 KASSERT(ife->ifm_data >=0 && ife->ifm_data < MII_NMEDIA, 129 ("invalid ife->ifm_data (0x%x) in mii_phy_setmedia", 130 ife->ifm_data)); 131 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; 135 136 if (mii->mii_media.ifm_media & IFM_ETH_MASTER) { 137 switch (IFM_SUBTYPE(ife->ifm_media)) { 138 case IFM_1000_T: 139 gtcr |= GTCR_MAN_MS|GTCR_ADV_MS; 140 break; 141 142 default: 143 panic("mii_phy_setmedia: MASTER on wrong media"); 144 } 145 } 146 147 if (ife->ifm_media & IFM_LOOP) 148 bmcr |= BMCR_LOOP; 149 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); 154 } 155 156 int 157 mii_phy_auto(struct mii_softc *sc) 158 { 159 160 /* 161 * Check for 1000BASE-X. Autonegotiation is a bit 162 * different on such devices. 163 */ 164 if (sc->mii_flags & MIIF_IS_1000X) { 165 uint16_t anar = 0; 166 167 if (sc->mii_extcapabilities & EXTSR_1000XFDX) 168 anar |= ANAR_X_FD; 169 if (sc->mii_extcapabilities & EXTSR_1000XHDX) 170 anar |= ANAR_X_HD; 171 172 if (sc->mii_flags & MIIF_DOPAUSE) { 173 /* XXX Asymmetric vs. symmetric? */ 174 anar |= ANLPAR_X_PAUSE_TOWARDS; 175 } 176 177 PHY_WRITE(sc, MII_ANAR, anar); 178 } else { 179 uint16_t anar; 180 181 anar = BMSR_MEDIA_TO_ANAR(sc->mii_capabilities) | 182 ANAR_CSMA; 183 if (sc->mii_flags & MIIF_DOPAUSE) 184 anar |= ANAR_FC; 185 PHY_WRITE(sc, MII_ANAR, anar); 186 if (sc->mii_flags & MIIF_HAVE_GTCR) { 187 uint16_t gtcr = 0; 188 189 if (sc->mii_extcapabilities & EXTSR_1000TFDX) 190 gtcr |= GTCR_ADV_1000TFDX; 191 if (sc->mii_extcapabilities & EXTSR_1000THDX) 192 gtcr |= GTCR_ADV_1000THDX; 193 194 PHY_WRITE(sc, MII_100T2CR, gtcr); 195 } 196 } 197 PHY_WRITE(sc, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG); 198 return (EJUSTRETURN); 199 } 200 201 int 202 mii_phy_tick(struct mii_softc *sc) 203 { 204 struct ifmedia_entry *ife = sc->mii_pdata->mii_media.ifm_cur; 205 struct ifnet *ifp = sc->mii_pdata->mii_ifp; 206 int reg; 207 208 /* Just bail now if the interface is down. */ 209 if ((ifp->if_flags & IFF_UP) == 0) 210 return (EJUSTRETURN); 211 212 /* 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 216 * changes. 217 */ 218 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) 219 return (0); 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 /* 225 * See above. 226 */ 227 return (0); 228 } 229 230 /* 231 * Only retry autonegotiation every N seconds. 232 */ 233 if (sc->mii_anegticks == 0) 234 sc->mii_anegticks = 17; 235 if (++sc->mii_ticks != sc->mii_anegticks) 236 return (EJUSTRETURN); 237 238 sc->mii_ticks = 0; 239 mii_phy_reset(sc); 240 mii_phy_auto(sc); 241 return (0); 242 } 243 244 void 245 mii_phy_reset(struct mii_softc *sc) 246 { 247 int reg, i; 248 249 if (sc->mii_flags & MIIF_NOISOLATE) 250 reg = BMCR_RESET; 251 else 252 reg = BMCR_RESET | BMCR_ISO; 253 PHY_WRITE(sc, MII_BMCR, reg); 254 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) 259 break; 260 DELAY(1000); 261 } 262 263 if (sc->mii_inst != 0 && ((sc->mii_flags & MIIF_NOISOLATE) == 0)) 264 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO); 265 } 266 267 void 268 mii_phy_down(struct mii_softc *sc) 269 { 270 271 } 272 273 void 274 mii_phy_update(struct mii_softc *sc, int cmd) 275 { 276 struct mii_data *mii = sc->mii_pdata; 277 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; 282 } 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; 286 } 287 } 288 289 /* 290 * Given an ifmedia word, return the corresponding ANAR value. 291 */ 292 int 293 mii_anar(media) 294 int media; 295 { 296 int rv; 297 298 switch (media & (IFM_TMASK|IFM_NMASK|IFM_FDX)) { 299 case IFM_ETHER|IFM_10_T: 300 rv = ANAR_10|ANAR_CSMA; 301 break; 302 case IFM_ETHER|IFM_10_T|IFM_FDX: 303 rv = ANAR_10_FD|ANAR_CSMA; 304 break; 305 case IFM_ETHER|IFM_100_TX: 306 rv = ANAR_TX|ANAR_CSMA; 307 break; 308 case IFM_ETHER|IFM_100_TX|IFM_FDX: 309 rv = ANAR_TX_FD|ANAR_CSMA; 310 break; 311 case IFM_ETHER|IFM_100_T4: 312 rv = ANAR_T4|ANAR_CSMA; 313 break; 314 default: 315 rv = 0; 316 break; 317 } 318 319 return (rv); 320 } 321 322 /* 323 * Given a BMCR value, return the corresponding ifmedia word. 324 */ 325 int 326 mii_media_from_bmcr(bmcr) 327 int bmcr; 328 { 329 int rv = IFM_ETHER; 330 331 if (bmcr & BMCR_S100) 332 rv |= IFM_100_TX; 333 else 334 rv |= IFM_10_T; 335 if (bmcr & BMCR_FDX) 336 rv |= IFM_FDX; 337 338 return (rv); 339 } 340 341 /* 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. 345 */ 346 void 347 mii_add_media(struct mii_softc *sc) 348 { 349 const char *sep = ""; 350 struct mii_data *mii; 351 352 mii = device_get_softc(sc->mii_dev); 353 if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0) { 354 printf("no media present"); 355 return; 356 } 357 358 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL) 359 #define PRINT(s) printf("%s%s", sep, s); sep = ", " 360 361 if (sc->mii_capabilities & BMSR_10THDX) { 362 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, sc->mii_inst), 0); 363 PRINT("10baseT"); 364 } 365 if (sc->mii_capabilities & BMSR_10TFDX) { 366 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, sc->mii_inst), 367 BMCR_FDX); 368 PRINT("10baseT-FDX"); 369 } 370 if (sc->mii_capabilities & BMSR_100TXHDX) { 371 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, sc->mii_inst), 372 BMCR_S100); 373 PRINT("100baseTX"); 374 } 375 if (sc->mii_capabilities & BMSR_100TXFDX) { 376 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, sc->mii_inst), 377 BMCR_S100|BMCR_FDX); 378 PRINT("100baseTX-FDX"); 379 } 380 if (sc->mii_capabilities & BMSR_100T4) { 381 /* 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 385 * XXX into T4 mode. 386 */ 387 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_T4, 0, sc->mii_inst), 388 BMCR_S100); 389 PRINT("100baseT4"); 390 } 391 if (sc->mii_capabilities & BMSR_ANEG) { 392 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst), 393 BMCR_AUTOEN); 394 PRINT("auto"); 395 } 396 397 398 399 #undef ADD 400 #undef PRINT 401 } 402 403 /* 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. 407 */ 408 void 409 mii_phy_add_media(struct mii_softc *sc) 410 { 411 struct mii_data *mii = sc->mii_pdata; 412 const char *sep = ""; 413 414 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL) 415 #define PRINT(s) printf("%s%s", sep, s); sep = ", " 416 417 if ((sc->mii_flags & MIIF_NOISOLATE) == 0) 418 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst), 419 MII_MEDIA_NONE); 420 421 /* 422 * There are different interpretations for the bits in 423 * HomePNA PHYs. And there is really only one media type 424 * that is supported. 425 */ 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, 429 sc->mii_inst), 430 MII_MEDIA_10_T); 431 PRINT("HomePNA1"); 432 } 433 return; 434 } 435 436 if (sc->mii_capabilities & BMSR_10THDX) { 437 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, sc->mii_inst), 438 MII_MEDIA_10_T); 439 PRINT("10baseT"); 440 } 441 if (sc->mii_capabilities & BMSR_10TFDX) { 442 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, sc->mii_inst), 443 MII_MEDIA_10_T_FDX); 444 PRINT("10baseT-FDX"); 445 } 446 if (sc->mii_capabilities & BMSR_100TXHDX) { 447 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, sc->mii_inst), 448 MII_MEDIA_100_TX); 449 PRINT("100baseTX"); 450 } 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"); 455 } 456 if (sc->mii_capabilities & BMSR_100T4) { 457 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_T4, 0, sc->mii_inst), 458 MII_MEDIA_100_T4); 459 PRINT("100baseT4"); 460 } 461 462 if (sc->mii_extcapabilities & EXTSR_MEDIAMASK) { 463 /* 464 * XXX Right now only handle 1000SX and 1000TX. Need 465 * XXX to handle 1000LX and 1000CX some how. 466 * 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. 470 */ 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); 476 PRINT("1000baseSX"); 477 } 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"); 484 } 485 486 /* 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 490 * the media is set. 491 * 492 * All 1000baseT PHYs have a 1000baseT control register. 493 */ 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); 500 PRINT("1000baseT"); 501 } 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"); 509 } 510 } 511 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 */ 515 PRINT("auto"); 516 } 517 #undef ADD 518 #undef PRINT 519 } 520 521 int 522 mii_phy_detach(device_t dev) 523 { 524 struct mii_softc *sc; 525 struct mii_data *mii; 526 527 sc = device_get_softc(dev); 528 mii = device_get_softc(device_get_parent(dev)); 529 mii_phy_down(sc); 530 sc->mii_dev = NULL; 531 LIST_REMOVE(sc, mii_list); 532 533 return(0); 534 } 535 536 const struct mii_phydesc * 537 mii_phy_match(const struct mii_attach_args *ma, const struct mii_phydesc *mpd) 538 { 539 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) 543 return (mpd); 544 } 545 return (NULL); 546 } 547