1 /*- 2 * Principal Author: Parag Patel 3 * Copyright (c) 2001 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice unmodified, this list of conditions, and the following 11 * disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * 28 * Additonal Copyright (c) 2001 by Traakan Software under same licence. 29 * Secondary Author: Matthew Jacob 30 */ 31 32 #include <sys/cdefs.h> 33 __FBSDID("$FreeBSD$"); 34 35 /* 36 * driver for the Marvell 88E1000 series external 1000/100/10-BT PHY. 37 */ 38 39 /* 40 * Support added for the Marvell 88E1011 (Alaska) 1000/100/10baseTX and 41 * 1000baseSX PHY. 42 * Nathan Binkert <nate@openbsd.org> 43 * Jung-uk Kim <jkim@niksun.com> 44 */ 45 46 #include <sys/param.h> 47 #include <sys/systm.h> 48 #include <sys/kernel.h> 49 #include <sys/module.h> 50 #include <sys/socket.h> 51 #include <sys/bus.h> 52 53 54 #include <net/if.h> 55 #include <net/if_media.h> 56 57 #include <dev/mii/mii.h> 58 #include <dev/mii/miivar.h> 59 #include "miidevs.h" 60 61 #include <dev/mii/e1000phyreg.h> 62 63 #include "miibus_if.h" 64 65 static int e1000phy_probe(device_t); 66 static int e1000phy_attach(device_t); 67 68 struct e1000phy_softc { 69 struct mii_softc mii_sc; 70 int mii_model; 71 }; 72 73 static device_method_t e1000phy_methods[] = { 74 /* device interface */ 75 DEVMETHOD(device_probe, e1000phy_probe), 76 DEVMETHOD(device_attach, e1000phy_attach), 77 DEVMETHOD(device_detach, mii_phy_detach), 78 DEVMETHOD(device_shutdown, bus_generic_shutdown), 79 { 0, 0 } 80 }; 81 82 static devclass_t e1000phy_devclass; 83 static driver_t e1000phy_driver = { 84 "e1000phy", 85 e1000phy_methods, 86 sizeof(struct e1000phy_softc) 87 }; 88 89 DRIVER_MODULE(e1000phy, miibus, e1000phy_driver, e1000phy_devclass, 0, 0); 90 91 static int e1000phy_service(struct mii_softc *, struct mii_data *, int); 92 static void e1000phy_status(struct mii_softc *); 93 static void e1000phy_reset(struct mii_softc *); 94 static int e1000phy_mii_phy_auto(struct e1000phy_softc *); 95 96 static const struct mii_phydesc e1000phys[] = { 97 MII_PHY_DESC(MARVELL, E1000), 98 MII_PHY_DESC(MARVELL, E1011), 99 MII_PHY_DESC(MARVELL, E1000_3), 100 MII_PHY_DESC(MARVELL, E1000S), 101 MII_PHY_DESC(MARVELL, E1000_5), 102 MII_PHY_DESC(MARVELL, E1000_6), 103 MII_PHY_DESC(MARVELL, E3082), 104 MII_PHY_DESC(MARVELL, E1112), 105 MII_PHY_DESC(MARVELL, E1149), 106 MII_PHY_DESC(MARVELL, E1111), 107 MII_PHY_DESC(MARVELL, E1116), 108 MII_PHY_DESC(MARVELL, E1118), 109 MII_PHY_DESC(xxMARVELL, E1000), 110 MII_PHY_DESC(xxMARVELL, E1011), 111 MII_PHY_DESC(xxMARVELL, E1000_3), 112 MII_PHY_DESC(xxMARVELL, E1000_5), 113 MII_PHY_DESC(xxMARVELL, E1111), 114 MII_PHY_END 115 }; 116 117 static int 118 e1000phy_probe(device_t dev) 119 { 120 121 return (mii_phy_dev_probe(dev, e1000phys, BUS_PROBE_DEFAULT)); 122 } 123 124 static int 125 e1000phy_attach(device_t dev) 126 { 127 struct e1000phy_softc *esc; 128 struct mii_softc *sc; 129 struct mii_attach_args *ma; 130 struct mii_data *mii; 131 int fast_ether; 132 133 esc = device_get_softc(dev); 134 sc = &esc->mii_sc; 135 ma = device_get_ivars(dev); 136 sc->mii_dev = device_get_parent(dev); 137 mii = device_get_softc(sc->mii_dev); 138 LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list); 139 140 sc->mii_inst = mii->mii_instance; 141 sc->mii_phy = ma->mii_phyno; 142 sc->mii_service = e1000phy_service; 143 sc->mii_pdata = mii; 144 sc->mii_anegticks = MII_ANEGTICKS_GIGE; 145 mii->mii_instance++; 146 147 fast_ether = 0; 148 esc->mii_model = MII_MODEL(ma->mii_id2); 149 switch (esc->mii_model) { 150 case MII_MODEL_MARVELL_E1011: 151 case MII_MODEL_MARVELL_E1112: 152 if (PHY_READ(sc, E1000_ESSR) & E1000_ESSR_FIBER_LINK) 153 sc->mii_flags |= MIIF_HAVEFIBER; 154 break; 155 case MII_MODEL_MARVELL_E3082: 156 /* 88E3082 10/100 Fast Ethernet PHY. */ 157 sc->mii_anegticks = MII_ANEGTICKS; 158 fast_ether = 1; 159 break; 160 } 161 162 e1000phy_reset(sc); 163 164 device_printf(dev, " "); 165 166 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL) 167 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst), 168 E1000_CR_ISOLATE); 169 if ((sc->mii_flags & MIIF_HAVEFIBER) == 0) { 170 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, sc->mii_inst), 171 E1000_CR_SPEED_10); 172 printf("10baseT, "); 173 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, sc->mii_inst), 174 E1000_CR_SPEED_10 | E1000_CR_FULL_DUPLEX); 175 printf("10baseT-FDX, "); 176 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, sc->mii_inst), 177 E1000_CR_SPEED_100); 178 printf("100baseTX, "); 179 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, sc->mii_inst), 180 E1000_CR_SPEED_100 | E1000_CR_FULL_DUPLEX); 181 printf("100baseTX-FDX, "); 182 if (fast_ether == 0) { 183 /* 184 * 1000BT-simplex not supported; driver must ignore 185 * this entry, but it must be present in order to 186 * manually set full-duplex. 187 */ 188 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, 0, 189 sc->mii_inst), E1000_CR_SPEED_1000); 190 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, IFM_FDX, 191 sc->mii_inst), 192 E1000_CR_SPEED_1000 | E1000_CR_FULL_DUPLEX); 193 printf("1000baseTX-FDX, "); 194 } 195 } else { 196 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, IFM_FDX, sc->mii_inst), 197 E1000_CR_SPEED_1000 | E1000_CR_FULL_DUPLEX); 198 printf("1000baseSX-FDX, "); 199 } 200 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst), 0); 201 printf("auto\n"); 202 #undef ADD 203 204 MIIBUS_MEDIAINIT(sc->mii_dev); 205 return (0); 206 } 207 208 static void 209 e1000phy_reset(struct mii_softc *sc) 210 { 211 struct e1000phy_softc *esc; 212 uint16_t reg; 213 214 esc = (struct e1000phy_softc *)sc; 215 reg = PHY_READ(sc, E1000_SCR); 216 if ((sc->mii_flags & MIIF_HAVEFIBER) != 0) { 217 reg &= ~E1000_SCR_AUTO_X_MODE; 218 PHY_WRITE(sc, E1000_SCR, reg); 219 if (esc->mii_model == MII_MODEL_MARVELL_E1112) { 220 /* Select 1000BASE-X only mode. */ 221 PHY_WRITE(sc, E1000_EADR, 2); 222 reg = PHY_READ(sc, E1000_SCR); 223 reg &= ~E1000_SCR_MODE_MASK; 224 reg |= E1000_SCR_MODE_1000BX; 225 PHY_WRITE(sc, E1000_SCR, reg); 226 PHY_WRITE(sc, E1000_EADR, 1); 227 } 228 } else { 229 switch (esc->mii_model) { 230 case MII_MODEL_MARVELL_E1111: 231 case MII_MODEL_MARVELL_E1112: 232 case MII_MODEL_MARVELL_E1116: 233 case MII_MODEL_MARVELL_E1118: 234 case MII_MODEL_MARVELL_E1149: 235 /* Disable energy detect mode. */ 236 reg &= ~E1000_SCR_EN_DETECT_MASK; 237 reg |= E1000_SCR_AUTO_X_MODE; 238 break; 239 case MII_MODEL_MARVELL_E3082: 240 reg |= (E1000_SCR_AUTO_X_MODE >> 1); 241 break; 242 default: 243 reg &= ~E1000_SCR_AUTO_X_MODE; 244 break; 245 } 246 /* Enable CRS on TX. */ 247 reg |= E1000_SCR_ASSERT_CRS_ON_TX; 248 /* Auto correction for reversed cable polarity. */ 249 reg &= ~E1000_SCR_POLARITY_REVERSAL; 250 PHY_WRITE(sc, E1000_SCR, reg); 251 } 252 253 switch (MII_MODEL(esc->mii_model)) { 254 case MII_MODEL_MARVELL_E3082: 255 case MII_MODEL_MARVELL_E1112: 256 case MII_MODEL_MARVELL_E1116: 257 case MII_MODEL_MARVELL_E1118: 258 case MII_MODEL_MARVELL_E1149: 259 break; 260 default: 261 /* Force TX_CLK to 25MHz clock. */ 262 reg = PHY_READ(sc, E1000_ESCR); 263 reg |= E1000_ESCR_TX_CLK_25; 264 PHY_WRITE(sc, E1000_ESCR, reg); 265 break; 266 } 267 268 /* Reset the PHY so all changes take effect. */ 269 reg = PHY_READ(sc, E1000_CR); 270 reg |= E1000_CR_RESET; 271 PHY_WRITE(sc, E1000_CR, reg); 272 } 273 274 static int 275 e1000phy_service(struct mii_softc *sc, struct mii_data *mii, int cmd) 276 { 277 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 278 struct e1000phy_softc *esc = (struct e1000phy_softc *)sc; 279 uint16_t speed, gig; 280 int reg; 281 282 switch (cmd) { 283 case MII_POLLSTAT: 284 /* 285 * If we're not polling our PHY instance, just return. 286 */ 287 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 288 return (0); 289 break; 290 291 case MII_MEDIACHG: 292 /* 293 * If the media indicates a different PHY instance, 294 * isolate ourselves. 295 */ 296 if (IFM_INST(ife->ifm_media) != sc->mii_inst) { 297 reg = PHY_READ(sc, E1000_CR); 298 PHY_WRITE(sc, E1000_CR, reg | E1000_CR_ISOLATE); 299 return (0); 300 } 301 302 /* 303 * If the interface is not up, don't do anything. 304 */ 305 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 306 break; 307 308 if (IFM_SUBTYPE(ife->ifm_media) == IFM_AUTO) { 309 e1000phy_mii_phy_auto(esc); 310 break; 311 } 312 313 speed = 0; 314 switch (IFM_SUBTYPE(ife->ifm_media)) { 315 case IFM_1000_T: 316 if (esc->mii_model == MII_MODEL_MARVELL_E3082) 317 return (EINVAL); 318 speed = E1000_CR_SPEED_1000; 319 break; 320 case IFM_1000_SX: 321 if (esc->mii_model == MII_MODEL_MARVELL_E3082) 322 return (EINVAL); 323 speed = E1000_CR_SPEED_1000; 324 break; 325 case IFM_100_TX: 326 speed = E1000_CR_SPEED_100; 327 break; 328 case IFM_10_T: 329 speed = E1000_CR_SPEED_10; 330 break; 331 case IFM_NONE: 332 reg = PHY_READ(sc, E1000_CR); 333 PHY_WRITE(sc, E1000_CR, 334 reg | E1000_CR_ISOLATE | E1000_CR_POWER_DOWN); 335 goto done; 336 default: 337 return (EINVAL); 338 } 339 340 if (((ife->ifm_media & IFM_GMASK) & IFM_FDX) != 0) { 341 speed |= E1000_CR_FULL_DUPLEX; 342 gig = E1000_1GCR_1000T_FD; 343 } else 344 gig = E1000_1GCR_1000T; 345 346 reg = PHY_READ(sc, E1000_CR); 347 reg &= ~E1000_CR_AUTO_NEG_ENABLE; 348 PHY_WRITE(sc, E1000_CR, reg | E1000_CR_RESET); 349 350 /* 351 * When setting the link manually, one side must 352 * be the master and the other the slave. However 353 * ifmedia doesn't give us a good way to specify 354 * this, so we fake it by using one of the LINK 355 * flags. If LINK0 is set, we program the PHY to 356 * be a master, otherwise it's a slave. 357 */ 358 if (IFM_SUBTYPE(ife->ifm_media) == IFM_1000_T || 359 (IFM_SUBTYPE(ife->ifm_media) == IFM_1000_SX)) { 360 if ((mii->mii_ifp->if_flags & IFF_LINK0)) 361 PHY_WRITE(sc, E1000_1GCR, gig | 362 E1000_1GCR_MS_ENABLE | E1000_1GCR_MS_VALUE); 363 else 364 PHY_WRITE(sc, E1000_1GCR, gig | 365 E1000_1GCR_MS_ENABLE); 366 } else { 367 if (esc->mii_model != MII_MODEL_MARVELL_E3082) 368 PHY_WRITE(sc, E1000_1GCR, 0); 369 } 370 PHY_WRITE(sc, E1000_AR, E1000_AR_SELECTOR_FIELD); 371 PHY_WRITE(sc, E1000_CR, speed | E1000_CR_RESET); 372 done: 373 break; 374 case MII_TICK: 375 /* 376 * If we're not currently selected, just return. 377 */ 378 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 379 return (0); 380 381 /* 382 * Is the interface even up? 383 */ 384 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 385 return (0); 386 387 /* 388 * Only used for autonegotiation. 389 */ 390 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) 391 break; 392 393 /* 394 * check for link. 395 * Read the status register twice; BMSR_LINK is latch-low. 396 */ 397 reg = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR); 398 if (reg & BMSR_LINK) { 399 sc->mii_ticks = 0; 400 break; 401 } 402 403 /* Announce link loss right after it happens. */ 404 if (sc->mii_ticks <= sc->mii_anegticks) 405 return (0); 406 407 sc->mii_ticks = 0; 408 e1000phy_reset(sc); 409 e1000phy_mii_phy_auto(esc); 410 break; 411 } 412 413 /* Update the media status. */ 414 e1000phy_status(sc); 415 416 /* Callback if something changed. */ 417 mii_phy_update(sc, cmd); 418 return (0); 419 } 420 421 static void 422 e1000phy_status(struct mii_softc *sc) 423 { 424 struct mii_data *mii = sc->mii_pdata; 425 int bmsr, bmcr, esr, gsr, ssr, isr, ar, lpar; 426 427 mii->mii_media_status = IFM_AVALID; 428 mii->mii_media_active = IFM_ETHER; 429 430 bmsr = PHY_READ(sc, E1000_SR) | PHY_READ(sc, E1000_SR); 431 esr = PHY_READ(sc, E1000_ESR); 432 bmcr = PHY_READ(sc, E1000_CR); 433 ssr = PHY_READ(sc, E1000_SSR); 434 isr = PHY_READ(sc, E1000_ISR); 435 ar = PHY_READ(sc, E1000_AR); 436 lpar = PHY_READ(sc, E1000_LPAR); 437 438 if (bmsr & E1000_SR_LINK_STATUS) 439 mii->mii_media_status |= IFM_ACTIVE; 440 441 if (bmcr & E1000_CR_LOOPBACK) 442 mii->mii_media_active |= IFM_LOOP; 443 444 if ((((bmcr & E1000_CR_AUTO_NEG_ENABLE) != 0) && 445 ((bmsr & E1000_SR_AUTO_NEG_COMPLETE) == 0)) || 446 ((ssr & E1000_SSR_LINK) == 0) || 447 ((ssr & E1000_SSR_SPD_DPLX_RESOLVED) == 0)) { 448 /* Erg, still trying, I guess... */ 449 mii->mii_media_active |= IFM_NONE; 450 return; 451 } 452 453 if ((sc->mii_flags & MIIF_HAVEFIBER) == 0) { 454 if (ssr & E1000_SSR_1000MBS) 455 mii->mii_media_active |= IFM_1000_T; 456 else if (ssr & E1000_SSR_100MBS) 457 mii->mii_media_active |= IFM_100_TX; 458 else 459 mii->mii_media_active |= IFM_10_T; 460 } else { 461 if (ssr & E1000_SSR_1000MBS) 462 mii->mii_media_active |= IFM_1000_SX; 463 } 464 465 if (ssr & E1000_SSR_DUPLEX) 466 mii->mii_media_active |= IFM_FDX; 467 else 468 mii->mii_media_active |= IFM_HDX; 469 470 if ((sc->mii_flags & MIIF_HAVEFIBER) == 0) { 471 /* FLAG0==rx-flow-control FLAG1==tx-flow-control */ 472 if ((ar & E1000_AR_PAUSE) && (lpar & E1000_LPAR_PAUSE)) { 473 mii->mii_media_active |= IFM_FLAG0 | IFM_FLAG1; 474 } else if (!(ar & E1000_AR_PAUSE) && (ar & E1000_AR_ASM_DIR) && 475 (lpar & E1000_LPAR_PAUSE) && (lpar & E1000_LPAR_ASM_DIR)) { 476 mii->mii_media_active |= IFM_FLAG1; 477 } else if ((ar & E1000_AR_PAUSE) && (ar & E1000_AR_ASM_DIR) && 478 !(lpar & E1000_LPAR_PAUSE) && (lpar & E1000_LPAR_ASM_DIR)) { 479 mii->mii_media_active |= IFM_FLAG0; 480 } 481 } 482 483 /* FLAG2 : local PHY resolved to MASTER */ 484 if ((IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T) || 485 (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_SX)) { 486 PHY_READ(sc, E1000_1GSR); 487 gsr = PHY_READ(sc, E1000_1GSR); 488 if ((gsr & E1000_1GSR_MS_CONFIG_RES) != 0) 489 mii->mii_media_active |= IFM_FLAG2; 490 } 491 } 492 493 static int 494 e1000phy_mii_phy_auto(struct e1000phy_softc *esc) 495 { 496 struct mii_softc *sc; 497 498 sc = &esc->mii_sc; 499 if ((sc->mii_flags & MIIF_HAVEFIBER) == 0) 500 PHY_WRITE(sc, E1000_AR, E1000_AR_10T | E1000_AR_10T_FD | 501 E1000_AR_100TX | E1000_AR_100TX_FD | 502 E1000_AR_PAUSE | E1000_AR_ASM_DIR); 503 else 504 PHY_WRITE(sc, E1000_AR, E1000_FA_1000X_FD | E1000_FA_1000X | 505 E1000_FA_SYM_PAUSE | E1000_FA_ASYM_PAUSE); 506 if (esc->mii_model != MII_MODEL_MARVELL_E3082) 507 PHY_WRITE(sc, E1000_1GCR, 508 E1000_1GCR_1000T_FD | E1000_1GCR_1000T); 509 PHY_WRITE(sc, E1000_CR, 510 E1000_CR_AUTO_NEG_ENABLE | E1000_CR_RESTART_AUTO_NEG); 511 512 return (EJUSTRETURN); 513 } 514