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