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/cdefs.h> 48 __FBSDID("$FreeBSD$"); 49 50 #include <sys/param.h> 51 #include <sys/systm.h> 52 #include <sys/kernel.h> 53 #include <sys/socket.h> 54 #include <sys/errno.h> 55 #include <sys/module.h> 56 #include <sys/bus.h> 57 58 59 #include <net/if.h> 60 #include <net/if_media.h> 61 62 #include <dev/mii/mii.h> 63 #include <dev/mii/miivar.h> 64 65 #include "miibus_if.h" 66 67 /* 68 * Media to register setting conversion table. Order matters. 69 */ 70 const struct mii_media mii_media_table[MII_NMEDIA] = { 71 /* None */ 72 { BMCR_ISO, ANAR_CSMA, 73 0, }, 74 75 /* 10baseT */ 76 { BMCR_S10, ANAR_CSMA|ANAR_10, 77 0, }, 78 79 /* 10baseT-FDX */ 80 { BMCR_S10|BMCR_FDX, ANAR_CSMA|ANAR_10_FD, 81 0, }, 82 83 /* 100baseT4 */ 84 { BMCR_S100, ANAR_CSMA|ANAR_T4, 85 0, }, 86 87 /* 100baseTX */ 88 { BMCR_S100, ANAR_CSMA|ANAR_TX, 89 0, }, 90 91 /* 100baseTX-FDX */ 92 { BMCR_S100|BMCR_FDX, ANAR_CSMA|ANAR_TX_FD, 93 0, }, 94 95 /* 1000baseX */ 96 { BMCR_S1000, ANAR_CSMA, 97 0, }, 98 99 /* 1000baseX-FDX */ 100 { BMCR_S1000|BMCR_FDX, ANAR_CSMA, 101 0, }, 102 103 /* 1000baseT */ 104 { BMCR_S1000, ANAR_CSMA, 105 GTCR_ADV_1000THDX }, 106 107 /* 1000baseT-FDX */ 108 { BMCR_S1000, ANAR_CSMA, 109 GTCR_ADV_1000TFDX }, 110 }; 111 112 void 113 mii_phy_setmedia(struct mii_softc *sc) 114 { 115 struct mii_data *mii = sc->mii_pdata; 116 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 117 int bmcr, anar, gtcr; 118 119 if (IFM_SUBTYPE(ife->ifm_media) == IFM_AUTO) { 120 if ((PHY_READ(sc, MII_BMCR) & BMCR_AUTOEN) == 0) 121 (void) mii_phy_auto(sc); 122 return; 123 } 124 125 /* 126 * Table index is stored in the media entry. 127 */ 128 129 KASSERT(ife->ifm_data >=0 && ife->ifm_data < MII_NMEDIA, 130 ("invalid ife->ifm_data (0x%x) in mii_phy_setmedia", 131 ife->ifm_data)); 132 133 anar = mii_media_table[ife->ifm_data].mm_anar; 134 bmcr = mii_media_table[ife->ifm_data].mm_bmcr; 135 gtcr = mii_media_table[ife->ifm_data].mm_gtcr; 136 137 if (mii->mii_media.ifm_media & IFM_ETH_MASTER) { 138 switch (IFM_SUBTYPE(ife->ifm_media)) { 139 case IFM_1000_T: 140 gtcr |= GTCR_MAN_MS|GTCR_ADV_MS; 141 break; 142 143 default: 144 panic("mii_phy_setmedia: MASTER on wrong media"); 145 } 146 } 147 148 if (ife->ifm_media & IFM_LOOP) 149 bmcr |= BMCR_LOOP; 150 151 PHY_WRITE(sc, MII_ANAR, anar); 152 PHY_WRITE(sc, MII_BMCR, bmcr); 153 if (sc->mii_flags & MIIF_HAVE_GTCR) 154 PHY_WRITE(sc, MII_100T2CR, gtcr); 155 } 156 157 int 158 mii_phy_auto(struct mii_softc *sc) 159 { 160 161 /* 162 * Check for 1000BASE-X. Autonegotiation is a bit 163 * different on such devices. 164 */ 165 if (sc->mii_flags & MIIF_IS_1000X) { 166 uint16_t anar = 0; 167 168 if (sc->mii_extcapabilities & EXTSR_1000XFDX) 169 anar |= ANAR_X_FD; 170 if (sc->mii_extcapabilities & EXTSR_1000XHDX) 171 anar |= ANAR_X_HD; 172 173 if (sc->mii_flags & MIIF_DOPAUSE) { 174 /* XXX Asymmetric vs. symmetric? */ 175 anar |= ANLPAR_X_PAUSE_TOWARDS; 176 } 177 178 PHY_WRITE(sc, MII_ANAR, anar); 179 } else { 180 uint16_t anar; 181 182 anar = BMSR_MEDIA_TO_ANAR(sc->mii_capabilities) | 183 ANAR_CSMA; 184 if (sc->mii_flags & MIIF_DOPAUSE) 185 anar |= ANAR_FC; 186 PHY_WRITE(sc, MII_ANAR, anar); 187 if (sc->mii_flags & MIIF_HAVE_GTCR) { 188 uint16_t gtcr = 0; 189 190 if (sc->mii_extcapabilities & EXTSR_1000TFDX) 191 gtcr |= GTCR_ADV_1000TFDX; 192 if (sc->mii_extcapabilities & EXTSR_1000THDX) 193 gtcr |= GTCR_ADV_1000THDX; 194 195 PHY_WRITE(sc, MII_100T2CR, gtcr); 196 } 197 } 198 PHY_WRITE(sc, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG); 199 return (EJUSTRETURN); 200 } 201 202 int 203 mii_phy_tick(struct mii_softc *sc) 204 { 205 struct ifmedia_entry *ife = sc->mii_pdata->mii_media.ifm_cur; 206 struct ifnet *ifp = sc->mii_pdata->mii_ifp; 207 int reg; 208 209 /* Just bail now if the interface is down. */ 210 if ((ifp->if_flags & IFF_UP) == 0) 211 return (EJUSTRETURN); 212 213 /* 214 * If we're not doing autonegotiation, we don't need to do 215 * any extra work here. However, we need to check the link 216 * status so we can generate an announcement if the status 217 * changes. 218 */ 219 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) 220 return (0); 221 222 /* Read the status register twice; BMSR_LINK is latch-low. */ 223 reg = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR); 224 if (reg & BMSR_LINK) { 225 /* 226 * See above. 227 */ 228 return (0); 229 } 230 231 /* 232 * Only retry autonegotiation every N seconds. 233 */ 234 if (sc->mii_anegticks == 0) 235 sc->mii_anegticks = 17; 236 if (++sc->mii_ticks != sc->mii_anegticks) 237 return (EJUSTRETURN); 238 239 sc->mii_ticks = 0; 240 mii_phy_reset(sc); 241 mii_phy_auto(sc); 242 return (0); 243 } 244 245 void 246 mii_phy_reset(struct mii_softc *sc) 247 { 248 int reg, i; 249 250 if (sc->mii_flags & MIIF_NOISOLATE) 251 reg = BMCR_RESET; 252 else 253 reg = BMCR_RESET | BMCR_ISO; 254 PHY_WRITE(sc, MII_BMCR, reg); 255 256 /* Wait 100ms for it to complete. */ 257 for (i = 0; i < 100; i++) { 258 reg = PHY_READ(sc, MII_BMCR); 259 if ((reg & BMCR_RESET) == 0) 260 break; 261 DELAY(1000); 262 } 263 264 if (sc->mii_inst != 0 && ((sc->mii_flags & MIIF_NOISOLATE) == 0)) 265 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO); 266 } 267 268 void 269 mii_phy_down(struct mii_softc *sc) 270 { 271 272 } 273 274 void 275 mii_phy_update(struct mii_softc *sc, int cmd) 276 { 277 struct mii_data *mii = sc->mii_pdata; 278 279 if (sc->mii_media_active != mii->mii_media_active || 280 cmd == MII_MEDIACHG) { 281 MIIBUS_STATCHG(sc->mii_dev); 282 sc->mii_media_active = mii->mii_media_active; 283 } 284 if (sc->mii_media_status != mii->mii_media_status) { 285 MIIBUS_LINKCHG(sc->mii_dev); 286 sc->mii_media_status = mii->mii_media_status; 287 } 288 } 289 290 /* 291 * Given an ifmedia word, return the corresponding ANAR value. 292 */ 293 int 294 mii_anar(media) 295 int media; 296 { 297 int rv; 298 299 switch (media & (IFM_TMASK|IFM_NMASK|IFM_FDX)) { 300 case IFM_ETHER|IFM_10_T: 301 rv = ANAR_10|ANAR_CSMA; 302 break; 303 case IFM_ETHER|IFM_10_T|IFM_FDX: 304 rv = ANAR_10_FD|ANAR_CSMA; 305 break; 306 case IFM_ETHER|IFM_100_TX: 307 rv = ANAR_TX|ANAR_CSMA; 308 break; 309 case IFM_ETHER|IFM_100_TX|IFM_FDX: 310 rv = ANAR_TX_FD|ANAR_CSMA; 311 break; 312 case IFM_ETHER|IFM_100_T4: 313 rv = ANAR_T4|ANAR_CSMA; 314 break; 315 default: 316 rv = 0; 317 break; 318 } 319 320 return (rv); 321 } 322 323 /* 324 * Given a BMCR value, return the corresponding ifmedia word. 325 */ 326 int 327 mii_media_from_bmcr(bmcr) 328 int bmcr; 329 { 330 int rv = IFM_ETHER; 331 332 if (bmcr & BMCR_S100) 333 rv |= IFM_100_TX; 334 else 335 rv |= IFM_10_T; 336 if (bmcr & BMCR_FDX) 337 rv |= IFM_FDX; 338 339 return (rv); 340 } 341 342 /* 343 * Initialize generic PHY media based on BMSR, called when a PHY is 344 * attached. We expect to be set up to print a comma-separated list 345 * of media names. Does not print a newline. 346 */ 347 void 348 mii_add_media(struct mii_softc *sc) 349 { 350 const char *sep = ""; 351 struct mii_data *mii; 352 353 mii = device_get_softc(sc->mii_dev); 354 if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0) { 355 printf("no media present"); 356 return; 357 } 358 359 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL) 360 #define PRINT(s) printf("%s%s", sep, s); sep = ", " 361 362 if (sc->mii_capabilities & BMSR_10THDX) { 363 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, sc->mii_inst), 0); 364 PRINT("10baseT"); 365 } 366 if (sc->mii_capabilities & BMSR_10TFDX) { 367 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, sc->mii_inst), 368 BMCR_FDX); 369 PRINT("10baseT-FDX"); 370 } 371 if (sc->mii_capabilities & BMSR_100TXHDX) { 372 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, sc->mii_inst), 373 BMCR_S100); 374 PRINT("100baseTX"); 375 } 376 if (sc->mii_capabilities & BMSR_100TXFDX) { 377 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, sc->mii_inst), 378 BMCR_S100|BMCR_FDX); 379 PRINT("100baseTX-FDX"); 380 } 381 if (sc->mii_capabilities & BMSR_100T4) { 382 /* 383 * XXX How do you enable 100baseT4? I assume we set 384 * XXX BMCR_S100 and then assume the PHYs will take 385 * XXX watever action is necessary to switch themselves 386 * XXX into T4 mode. 387 */ 388 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_T4, 0, sc->mii_inst), 389 BMCR_S100); 390 PRINT("100baseT4"); 391 } 392 if (sc->mii_capabilities & BMSR_ANEG) { 393 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst), 394 BMCR_AUTOEN); 395 PRINT("auto"); 396 } 397 398 399 400 #undef ADD 401 #undef PRINT 402 } 403 404 /* 405 * Initialize generic PHY media based on BMSR, called when a PHY is 406 * attached. We expect to be set up to print a comma-separated list 407 * of media names. Does not print a newline. 408 */ 409 void 410 mii_phy_add_media(struct mii_softc *sc) 411 { 412 struct mii_data *mii = sc->mii_pdata; 413 const char *sep = ""; 414 415 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL) 416 #define PRINT(s) printf("%s%s", sep, s); sep = ", " 417 418 if ((sc->mii_flags & MIIF_NOISOLATE) == 0) 419 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst), 420 MII_MEDIA_NONE); 421 422 /* 423 * There are different interpretations for the bits in 424 * HomePNA PHYs. And there is really only one media type 425 * that is supported. 426 */ 427 if (sc->mii_flags & MIIF_IS_HPNA) { 428 if (sc->mii_capabilities & BMSR_10THDX) { 429 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_HPNA_1, 0, 430 sc->mii_inst), 431 MII_MEDIA_10_T); 432 PRINT("HomePNA1"); 433 } 434 return; 435 } 436 437 if (sc->mii_capabilities & BMSR_10THDX) { 438 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, sc->mii_inst), 439 MII_MEDIA_10_T); 440 PRINT("10baseT"); 441 } 442 if (sc->mii_capabilities & BMSR_10TFDX) { 443 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, sc->mii_inst), 444 MII_MEDIA_10_T_FDX); 445 PRINT("10baseT-FDX"); 446 } 447 if (sc->mii_capabilities & BMSR_100TXHDX) { 448 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, sc->mii_inst), 449 MII_MEDIA_100_TX); 450 PRINT("100baseTX"); 451 } 452 if (sc->mii_capabilities & BMSR_100TXFDX) { 453 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, sc->mii_inst), 454 MII_MEDIA_100_TX_FDX); 455 PRINT("100baseTX-FDX"); 456 } 457 if (sc->mii_capabilities & BMSR_100T4) { 458 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_T4, 0, sc->mii_inst), 459 MII_MEDIA_100_T4); 460 PRINT("100baseT4"); 461 } 462 463 if (sc->mii_extcapabilities & EXTSR_MEDIAMASK) { 464 /* 465 * XXX Right now only handle 1000SX and 1000TX. Need 466 * XXX to handle 1000LX and 1000CX some how. 467 * 468 * Note since it can take 5 seconds to auto-negotiate 469 * a gigabit link, we make anegticks 10 seconds for 470 * all the gigabit media types. 471 */ 472 if (sc->mii_extcapabilities & EXTSR_1000XHDX) { 473 sc->mii_anegticks = 17; 474 sc->mii_flags |= MIIF_IS_1000X; 475 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, 0, 476 sc->mii_inst), MII_MEDIA_1000_X); 477 PRINT("1000baseSX"); 478 } 479 if (sc->mii_extcapabilities & EXTSR_1000XFDX) { 480 sc->mii_anegticks = 17; 481 sc->mii_flags |= MIIF_IS_1000X; 482 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, IFM_FDX, 483 sc->mii_inst), MII_MEDIA_1000_X_FDX); 484 PRINT("1000baseSX-FDX"); 485 } 486 487 /* 488 * 1000baseT media needs to be able to manipulate 489 * master/slave mode. We set IFM_ETH_MASTER in 490 * the "don't care mask" and filter it out when 491 * the media is set. 492 * 493 * All 1000baseT PHYs have a 1000baseT control register. 494 */ 495 if (sc->mii_extcapabilities & EXTSR_1000THDX) { 496 sc->mii_anegticks = 17; 497 sc->mii_flags |= MIIF_HAVE_GTCR; 498 mii->mii_media.ifm_mask |= IFM_ETH_MASTER; 499 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, 0, 500 sc->mii_inst), MII_MEDIA_1000_T); 501 PRINT("1000baseT"); 502 } 503 if (sc->mii_extcapabilities & EXTSR_1000TFDX) { 504 sc->mii_anegticks = 17; 505 sc->mii_flags |= MIIF_HAVE_GTCR; 506 mii->mii_media.ifm_mask |= IFM_ETH_MASTER; 507 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, IFM_FDX, 508 sc->mii_inst), MII_MEDIA_1000_T_FDX); 509 PRINT("1000baseT-FDX"); 510 } 511 } 512 513 if (sc->mii_capabilities & BMSR_ANEG) { 514 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst), 515 MII_NMEDIA); /* intentionally invalid index */ 516 PRINT("auto"); 517 } 518 #undef ADD 519 #undef PRINT 520 } 521 522 int 523 mii_phy_detach(device_t dev) 524 { 525 struct mii_softc *sc; 526 527 sc = device_get_softc(dev); 528 mii_phy_down(sc); 529 sc->mii_dev = NULL; 530 LIST_REMOVE(sc, mii_list); 531 532 return(0); 533 } 534 535 const struct mii_phydesc * 536 mii_phy_match(const struct mii_attach_args *ma, const struct mii_phydesc *mpd) 537 { 538 539 for (; mpd->mpd_name != NULL; mpd++) { 540 if (MII_OUI(ma->mii_id1, ma->mii_id2) == mpd->mpd_oui && 541 MII_MODEL(ma->mii_id2) == mpd->mpd_model) 542 return (mpd); 543 } 544 return (NULL); 545 } 546