1 /* $FreeBSD: src/sys/dev/mii/e1000phy.c,v 1.1.2.2 2002/11/08 21:53:49 semenu Exp $ */ 2 /* $DragonFly: src/sys/dev/netif/mii_layer/e1000phy.c,v 1.6 2005/02/14 16:21:34 joerg Exp $ */ 3 /* 4 * Principal Author: Parag Patel 5 * Copyright (c) 2001 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice unmodified, this list of conditions, and the following 13 * disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 * 30 * Additonal Copyright (c) 2001 by Traakan Software under same licence. 31 * Secondary Author: Matthew Jacob 32 */ 33 34 /* 35 * driver for the Marvell 88E1000 series external 1000/100/10-BT PHY. 36 */ 37 38 /* 39 * Support added for the Marvell 88E1011 (Alaska) 1000/100/10baseTX and 40 * 1000baseSX PHY. 41 * Nathan Binkert <nate@openbsd.org> 42 * Jung-uk Kim <jkim@niksun.com> 43 */ 44 45 #include <sys/param.h> 46 #include <sys/systm.h> 47 #include <sys/kernel.h> 48 #include <sys/socket.h> 49 #include <sys/bus.h> 50 51 #include <machine/clock.h> 52 53 #include <net/if.h> 54 #include <net/if_media.h> 55 56 #include "mii.h" 57 #include "miivar.h" 58 #include "miidevs.h" 59 60 #include "e1000phyreg.h" 61 62 #include "miibus_if.h" 63 64 static int e1000phy_probe(device_t); 65 static int e1000phy_attach(device_t); 66 static int e1000phy_detach(device_t); 67 68 static device_method_t e1000phy_methods[] = { 69 /* device interface */ 70 DEVMETHOD(device_probe, e1000phy_probe), 71 DEVMETHOD(device_attach, e1000phy_attach), 72 DEVMETHOD(device_detach, e1000phy_detach), 73 DEVMETHOD(device_shutdown, bus_generic_shutdown), 74 { 0, 0 } 75 }; 76 77 static devclass_t e1000phy_devclass; 78 static driver_t e1000phy_driver = { 79 "e1000phy", e1000phy_methods, sizeof (struct mii_softc) 80 }; 81 DRIVER_MODULE(e1000phy, miibus, e1000phy_driver, e1000phy_devclass, 0, 0); 82 83 int e1000phy_service(struct mii_softc *, struct mii_data *, int); 84 void e1000phy_status(struct mii_softc *); 85 86 static int e1000phy_mii_phy_auto(struct mii_softc *, int); 87 extern void mii_phy_auto_timeout(void *); 88 static void e1000phy_reset(struct mii_softc *); 89 90 static int e1000phy_debug = 0; 91 92 static int 93 e1000phy_probe(device_t dev) 94 { 95 struct mii_attach_args *ma; 96 u_int32_t id; 97 98 ma = device_get_ivars(dev); 99 id = ((ma->mii_id1 << 16) | ma->mii_id2) & E1000_ID_MASK; 100 if (id != E1000_ID_88E1000 101 && id != E1000_ID_88E1000S 102 && id != E1000_ID_88E1011) { 103 return ENXIO; 104 } 105 106 device_set_desc(dev, MII_STR_MARVELL_E1000); 107 return 0; 108 } 109 110 static int 111 e1000phy_attach(device_t dev) 112 { 113 struct mii_softc *sc; 114 struct mii_attach_args *ma; 115 struct mii_data *mii; 116 const char *sep = ""; 117 u_int32_t id; 118 119 getenv_int("e1000phy_debug", &e1000phy_debug); 120 121 sc = device_get_softc(dev); 122 ma = device_get_ivars(dev); 123 mii_softc_init(sc); 124 sc->mii_dev = device_get_parent(dev); 125 mii = device_get_softc(sc->mii_dev); 126 LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list); 127 128 sc->mii_inst = mii->mii_instance; 129 sc->mii_phy = ma->mii_phyno; 130 sc->mii_service = e1000phy_service; 131 sc->mii_pdata = mii; 132 sc->mii_flags |= MIIF_NOISOLATE; 133 134 id = ((ma->mii_id1 << 16) | ma->mii_id2) & E1000_ID_MASK; 135 if (id == E1000_ID_88E1011 136 && (PHY_READ(sc, E1000_ESSR) & E1000_ESSR_FIBER_LINK)) 137 sc->mii_flags |= MIIF_HAVEFIBER; 138 mii->mii_instance++; 139 e1000phy_reset(sc); 140 141 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL) 142 #define PRINT(s) printf("%s%s", sep, s); sep = ", " 143 144 #if 0 145 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst), 146 E1000_CR_ISOLATE); 147 #endif 148 149 device_printf(dev, " "); 150 if ((sc->mii_flags & MIIF_HAVEFIBER) == 0) { 151 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, IFM_FDX, sc->mii_inst), 152 E1000_CR_SPEED_1000 | E1000_CR_FULL_DUPLEX); 153 PRINT("1000baseTX-FDX"); 154 /* 155 TODO - apparently 1000BT-simplex not supported? 156 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, 0, sc->mii_inst), 157 E1000_CR_SPEED_1000); 158 PRINT("1000baseTX"); 159 */ 160 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, sc->mii_inst), 161 E1000_CR_SPEED_100 | E1000_CR_FULL_DUPLEX); 162 PRINT("100baseTX-FDX"); 163 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, sc->mii_inst), 164 E1000_CR_SPEED_100); 165 PRINT("100baseTX"); 166 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, sc->mii_inst), 167 E1000_CR_SPEED_10 | E1000_CR_FULL_DUPLEX); 168 PRINT("10baseTX-FDX"); 169 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, sc->mii_inst), 170 E1000_CR_SPEED_10); 171 PRINT("10baseTX"); 172 } else { 173 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, IFM_FDX, sc->mii_inst), 174 E1000_CR_SPEED_1000); 175 PRINT("1000baseSX-FDX"); 176 } 177 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst), 0); 178 PRINT("auto"); 179 180 printf("\n"); 181 #undef ADD 182 #undef PRINT 183 184 MIIBUS_MEDIAINIT(sc->mii_dev); 185 return(0); 186 } 187 188 static int 189 e1000phy_detach(device_t dev) 190 { 191 struct mii_softc *sc; 192 struct mii_data *mii; 193 194 sc = device_get_softc(dev); 195 mii = device_get_softc(device_get_parent(dev)); 196 197 if (sc->mii_flags & MIIF_DOINGAUTO) 198 callout_stop(&sc->mii_auto_ch); 199 200 sc->mii_dev = NULL; 201 LIST_REMOVE(sc, mii_list); 202 203 return 0; 204 } 205 206 static void 207 e1000phy_reset(struct mii_softc *sc) 208 { 209 u_int32_t reg; 210 int i; 211 212 /* initialize custom E1000 registers to magic values */ 213 reg = PHY_READ(sc, E1000_SCR); 214 reg &= ~E1000_SCR_AUTO_X_MODE; 215 PHY_WRITE(sc, E1000_SCR, reg); 216 217 /* normal PHY reset */ 218 /*mii_phy_reset(sc);*/ 219 reg = PHY_READ(sc, E1000_CR); 220 reg |= E1000_CR_RESET; 221 PHY_WRITE(sc, E1000_CR, reg); 222 223 for (i = 0; i < 500; i++) { 224 DELAY(1); 225 reg = PHY_READ(sc, E1000_CR); 226 if (!(reg & E1000_CR_RESET)) 227 break; 228 } 229 230 /* set more custom E1000 registers to magic values */ 231 reg = PHY_READ(sc, E1000_SCR); 232 reg |= E1000_SCR_ASSERT_CRS_ON_TX; 233 PHY_WRITE(sc, E1000_SCR, reg); 234 235 reg = PHY_READ(sc, E1000_ESCR); 236 reg |= E1000_ESCR_TX_CLK_25; 237 PHY_WRITE(sc, E1000_ESCR, reg); 238 239 /* even more magic to reset DSP? */ 240 PHY_WRITE(sc, 29, 0x1d); 241 PHY_WRITE(sc, 30, 0xc1); 242 PHY_WRITE(sc, 30, 0x00); 243 } 244 245 int 246 e1000phy_service(struct mii_softc *sc, struct mii_data *mii, int cmd) 247 { 248 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 249 int reg; 250 251 switch (cmd) { 252 case MII_POLLSTAT: 253 /* 254 * If we're not polling our PHY instance, just return. 255 */ 256 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 257 return (0); 258 break; 259 260 case MII_MEDIACHG: 261 /* 262 * If the media indicates a different PHY instance, 263 * isolate ourselves. 264 */ 265 if (IFM_INST(ife->ifm_media) != sc->mii_inst) { 266 reg = PHY_READ(sc, E1000_CR); 267 PHY_WRITE(sc, E1000_CR, reg | E1000_CR_ISOLATE); 268 return (0); 269 } 270 271 /* 272 * If the interface is not up, don't do anything. 273 */ 274 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) { 275 break; 276 } 277 278 switch (IFM_SUBTYPE(ife->ifm_media)) { 279 case IFM_AUTO: 280 /* 281 * If we're already in auto mode, just return. 282 */ 283 if (sc->mii_flags & MIIF_DOINGAUTO) { 284 return (0); 285 } 286 e1000phy_reset(sc); 287 (void)e1000phy_mii_phy_auto(sc, 1); 288 break; 289 290 case IFM_1000_SX: 291 e1000phy_reset(sc); 292 293 PHY_WRITE(sc, E1000_CR, 294 E1000_CR_FULL_DUPLEX | E1000_CR_SPEED_1000); 295 PHY_WRITE(sc, E1000_AR, E1000_FA_1000X_FD); 296 break; 297 298 case IFM_1000_T: 299 if (sc->mii_flags & MIIF_DOINGAUTO) 300 return (0); 301 302 e1000phy_reset(sc); 303 304 /* TODO - any other way to force 1000BT? */ 305 (void)e1000phy_mii_phy_auto(sc, 1); 306 break; 307 308 case IFM_100_TX: 309 e1000phy_reset(sc); 310 311 if ((ife->ifm_media & IFM_GMASK) == IFM_FDX) { 312 PHY_WRITE(sc, E1000_CR, 313 E1000_CR_FULL_DUPLEX | E1000_CR_SPEED_100); 314 PHY_WRITE(sc, E1000_AR, E1000_AR_100TX_FD); 315 } else { 316 PHY_WRITE(sc, E1000_CR, E1000_CR_SPEED_100); 317 PHY_WRITE(sc, E1000_AR, E1000_AR_100TX); 318 } 319 break; 320 321 case IFM_10_T: 322 e1000phy_reset(sc); 323 324 if ((ife->ifm_media & IFM_GMASK) == IFM_FDX) { 325 PHY_WRITE(sc, E1000_CR, 326 E1000_CR_FULL_DUPLEX | E1000_CR_SPEED_10); 327 PHY_WRITE(sc, E1000_AR, E1000_AR_10T_FD); 328 } else { 329 PHY_WRITE(sc, E1000_CR, E1000_CR_SPEED_10); 330 PHY_WRITE(sc, E1000_AR, E1000_AR_10T); 331 } 332 333 break; 334 335 default: 336 return (EINVAL); 337 } 338 339 break; 340 341 case MII_TICK: 342 /* 343 * If we're not currently selected, just return. 344 */ 345 if (IFM_INST(ife->ifm_media) != sc->mii_inst) { 346 return (0); 347 } 348 349 /* 350 * Only used for autonegotiation. 351 */ 352 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) { 353 return (0); 354 } 355 356 /* 357 * Is the interface even up? 358 */ 359 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) { 360 return (0); 361 } 362 363 /* 364 * Only retry autonegotiation every 5 seconds. 365 */ 366 if (++(sc->mii_ticks) != 5) { 367 return (0); 368 } 369 sc->mii_ticks = 0; 370 371 /* 372 * Check to see if we have link. If we do, we don't 373 * need to restart the autonegotiation process. Read 374 * the BMSR twice in case it's latched. 375 */ 376 reg = PHY_READ(sc, E1000_SR) | PHY_READ(sc, E1000_SR); 377 378 if (reg & E1000_SR_LINK_STATUS) 379 break; 380 381 e1000phy_reset(sc); 382 383 if (e1000phy_mii_phy_auto(sc, 0) == EJUSTRETURN) { 384 return(0); 385 } 386 387 break; 388 } 389 390 /* Update the media status. */ 391 e1000phy_status(sc); 392 393 /* Callback if something changed. */ 394 if (sc->mii_active != mii->mii_media_active || cmd == MII_MEDIACHG) { 395 MIIBUS_STATCHG(sc->mii_dev); 396 sc->mii_active = mii->mii_media_active; 397 } 398 399 return (0); 400 } 401 402 void 403 e1000phy_status(struct mii_softc *sc) 404 { 405 struct mii_data *mii = sc->mii_pdata; 406 int bmsr, bmcr, esr, ssr, isr, ar, lpar; 407 408 mii->mii_media_status = IFM_AVALID; 409 mii->mii_media_active = IFM_ETHER; 410 411 bmsr = PHY_READ(sc, E1000_SR) | PHY_READ(sc, E1000_SR); 412 esr = PHY_READ(sc, E1000_ESR); 413 bmcr = PHY_READ(sc, E1000_CR); 414 ssr = PHY_READ(sc, E1000_SSR); 415 isr = PHY_READ(sc, E1000_ISR); 416 ar = PHY_READ(sc, E1000_AR); 417 lpar = PHY_READ(sc, E1000_LPAR); 418 419 if (bmsr & E1000_SR_LINK_STATUS) 420 mii->mii_media_status |= IFM_ACTIVE; 421 422 if (bmcr & E1000_CR_LOOPBACK) 423 mii->mii_media_active |= IFM_LOOP; 424 425 if ((sc->mii_flags & MIIF_DOINGAUTO) && 426 (!(bmsr & E1000_SR_AUTO_NEG_COMPLETE) || !(ssr & E1000_SSR_LINK) || 427 !(ssr & E1000_SSR_SPD_DPLX_RESOLVED))) { 428 /* Erg, still trying, I guess... */ 429 mii->mii_media_active |= IFM_NONE; 430 return; 431 } 432 433 if ((sc->mii_flags & MIIF_HAVEFIBER) == 0) { 434 if (ssr & E1000_SSR_1000MBS) 435 mii->mii_media_active |= IFM_1000_T; 436 else if (ssr & E1000_SSR_100MBS) 437 mii->mii_media_active |= IFM_100_TX; 438 else 439 mii->mii_media_active |= IFM_10_T; 440 } else { 441 if (ssr & E1000_SSR_1000MBS) 442 mii->mii_media_active |= IFM_1000_SX; 443 } 444 445 if (ssr & E1000_SSR_DUPLEX) 446 mii->mii_media_active |= IFM_FDX; 447 else 448 mii->mii_media_active |= IFM_HDX; 449 450 if ((sc->mii_flags & MIIF_HAVEFIBER) == 0) { 451 /* FLAG0==rx-flow-control FLAG1==tx-flow-control */ 452 if ((ar & E1000_AR_PAUSE) && (lpar & E1000_LPAR_PAUSE)) { 453 mii->mii_media_active |= IFM_FLAG0 | IFM_FLAG1; 454 } else if (!(ar & E1000_AR_PAUSE) && (ar & E1000_AR_ASM_DIR) && 455 (lpar & E1000_LPAR_PAUSE) && (lpar & E1000_LPAR_ASM_DIR)) { 456 mii->mii_media_active |= IFM_FLAG1; 457 } else if ((ar & E1000_AR_PAUSE) && (ar & E1000_AR_ASM_DIR) && 458 !(lpar & E1000_LPAR_PAUSE) && (lpar & E1000_LPAR_ASM_DIR)) { 459 mii->mii_media_active |= IFM_FLAG0; 460 } 461 } 462 } 463 464 static int 465 e1000phy_mii_phy_auto(struct mii_softc *mii, int waitfor) 466 { 467 int bmsr, i; 468 469 if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) { 470 if ((mii->mii_flags & MIIF_HAVEFIBER) == 0) { 471 PHY_WRITE(mii, E1000_AR, E1000_AR_10T | E1000_AR_10T_FD | 472 E1000_AR_100TX | E1000_AR_100TX_FD | 473 E1000_AR_PAUSE | E1000_AR_ASM_DIR); 474 PHY_WRITE(mii, E1000_1GCR, E1000_1GCR_1000T_FD); 475 PHY_WRITE(mii, E1000_CR, 476 E1000_CR_AUTO_NEG_ENABLE | E1000_CR_RESTART_AUTO_NEG); 477 } 478 } 479 480 if (waitfor) { 481 /* Wait 5 seconds for it to complete. */ 482 for (i = 0; i < 5000; i++) { 483 bmsr = 484 PHY_READ(mii, E1000_SR) | PHY_READ(mii, E1000_SR); 485 486 if (bmsr & E1000_SR_AUTO_NEG_COMPLETE) { 487 return (0); 488 } 489 DELAY(1000); 490 } 491 492 /* 493 * Don't need to worry about clearing MIIF_DOINGAUTO. 494 * If that's set, a timeout is pending, and it will 495 * clear the flag. [do it anyway] 496 */ 497 } 498 499 /* 500 * Just let it finish asynchronously. This is for the benefit of 501 * the tick handler driving autonegotiation. Don't want 500ms 502 * delays all the time while the system is running! 503 */ 504 if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) { 505 mii->mii_flags |= MIIF_DOINGAUTO; 506 mii->mii_ticks = 0; 507 callout_reset(&mii->mii_auto_ch, 5 * hz, 508 mii_phy_auto_timeout, mii); 509 } 510 return (EJUSTRETURN); 511 } 512