1 /* $NetBSD: brgphy.c,v 1.28 2006/10/12 01:31:25 christos 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 * Copyright (c) 1997 Manuel Bouyer. All rights reserved. 42 * 43 * Redistribution and use in source and binary forms, with or without 44 * modification, are permitted provided that the following conditions 45 * are met: 46 * 1. Redistributions of source code must retain the above copyright 47 * notice, this list of conditions and the following disclaimer. 48 * 2. Redistributions in binary form must reproduce the above copyright 49 * notice, this list of conditions and the following disclaimer in the 50 * documentation and/or other materials provided with the distribution. 51 * 3. All advertising materials mentioning features or use of this software 52 * must display the following acknowledgement: 53 * This product includes software developed by Manuel Bouyer. 54 * 4. The name of the author may not be used to endorse or promote products 55 * derived from this software without specific prior written permission. 56 * 57 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 58 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 59 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 60 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 61 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 62 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 63 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 64 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 65 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 66 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 67 */ 68 69 /* 70 * driver for the Broadcom BCM5400 Gig-E PHY. 71 * 72 * Programming information for this PHY was gleaned from FreeBSD 73 * (they were apparently able to get a datasheet from Broadcom). 74 */ 75 76 #include <sys/cdefs.h> 77 __KERNEL_RCSID(0, "$NetBSD: brgphy.c,v 1.28 2006/10/12 01:31:25 christos Exp $"); 78 79 #include <sys/param.h> 80 #include <sys/systm.h> 81 #include <sys/kernel.h> 82 #include <sys/device.h> 83 #include <sys/socket.h> 84 #include <sys/errno.h> 85 86 #include <net/if.h> 87 #include <net/if_media.h> 88 89 #include <dev/mii/mii.h> 90 #include <dev/mii/miivar.h> 91 #include <dev/mii/miidevs.h> 92 93 #include <dev/mii/brgphyreg.h> 94 95 static int brgphymatch(struct device *, struct cfdata *, void *); 96 static void brgphyattach(struct device *, struct device *, void *); 97 98 CFATTACH_DECL(brgphy, sizeof(struct mii_softc), 99 brgphymatch, brgphyattach, mii_phy_detach, mii_phy_activate); 100 101 static int brgphy_service(struct mii_softc *, struct mii_data *, int); 102 static void brgphy_status(struct mii_softc *); 103 104 static void brgphy_5401_reset(struct mii_softc *); 105 static void brgphy_5411_reset(struct mii_softc *); 106 static void brgphy_5703_reset(struct mii_softc *); 107 static void brgphy_5704_reset(struct mii_softc *); 108 static void brgphy_5705_reset(struct mii_softc *); 109 static void brgphy_5750_reset(struct mii_softc *); 110 111 static const struct mii_phy_funcs brgphy_funcs = { 112 brgphy_service, brgphy_status, mii_phy_reset, 113 }; 114 115 static const struct mii_phy_funcs brgphy_5401_funcs = { 116 brgphy_service, brgphy_status, brgphy_5401_reset, 117 }; 118 119 static const struct mii_phy_funcs brgphy_5411_funcs = { 120 brgphy_service, brgphy_status, brgphy_5411_reset, 121 }; 122 123 static const struct mii_phy_funcs brgphy_5703_funcs = { 124 brgphy_service, brgphy_status, brgphy_5703_reset, 125 }; 126 127 static const struct mii_phy_funcs brgphy_5704_funcs = { 128 brgphy_service, brgphy_status, brgphy_5704_reset, 129 }; 130 131 static const struct mii_phy_funcs brgphy_5705_funcs = { 132 brgphy_service, brgphy_status, brgphy_5705_reset, 133 }; 134 135 const struct mii_phy_funcs brgphy_5750_funcs = { 136 brgphy_service, brgphy_status, brgphy_5750_reset, 137 }; 138 139 140 static const struct mii_phydesc brgphys[] = { 141 { MII_OUI_BROADCOM, MII_MODEL_BROADCOM_BCM5400, 142 MII_STR_BROADCOM_BCM5400 }, 143 144 { MII_OUI_BROADCOM, MII_MODEL_BROADCOM_BCM5401, 145 MII_STR_BROADCOM_BCM5401 }, 146 147 { MII_OUI_BROADCOM, MII_MODEL_BROADCOM_BCM5411, 148 MII_STR_BROADCOM_BCM5411 }, 149 150 { MII_OUI_BROADCOM, MII_MODEL_BROADCOM_BCM5421, 151 MII_STR_BROADCOM_BCM5421 }, 152 153 { MII_OUI_BROADCOM, MII_MODEL_BROADCOM_BCM5701, 154 MII_STR_BROADCOM_BCM5701 }, 155 156 { MII_OUI_BROADCOM, MII_MODEL_BROADCOM_BCM5703, 157 MII_STR_BROADCOM_BCM5703 }, 158 159 { MII_OUI_BROADCOM, MII_MODEL_BROADCOM_BCM5704, 160 MII_STR_BROADCOM_BCM5704 }, 161 162 { MII_OUI_BROADCOM, MII_MODEL_BROADCOM_BCM5705, 163 MII_STR_BROADCOM_BCM5705 }, 164 165 { MII_OUI_BROADCOM, MII_MODEL_BROADCOM_BCM5714, 166 MII_STR_BROADCOM_BCM5714 }, 167 168 { MII_OUI_BROADCOM, MII_MODEL_BROADCOM_BCM5750, 169 MII_STR_BROADCOM_BCM5750 }, 170 171 { MII_OUI_BROADCOM, MII_MODEL_BROADCOM_BCM5780, 172 MII_STR_BROADCOM_BCM5780 }, 173 174 { 0, 0, 175 NULL }, 176 }; 177 178 static void bcm5401_load_dspcode(struct mii_softc *); 179 static void bcm5411_load_dspcode(struct mii_softc *); 180 static void bcm5703_load_dspcode(struct mii_softc *); 181 static void bcm5704_load_dspcode(struct mii_softc *); 182 static void bcm5750_load_dspcode(struct mii_softc *); 183 184 static int 185 brgphymatch(struct device *parent __unused, struct cfdata *match __unused, 186 void *aux) 187 { 188 struct mii_attach_args *ma = aux; 189 190 if (mii_phy_match(ma, brgphys) != NULL) 191 return (10); 192 193 return (0); 194 } 195 196 static void 197 brgphyattach(struct device *parent __unused, struct device *self, void *aux) 198 { 199 struct mii_softc *sc = device_private(self); 200 struct mii_attach_args *ma = aux; 201 struct mii_data *mii = ma->mii_data; 202 const struct mii_phydesc *mpd; 203 204 mpd = mii_phy_match(ma, brgphys); 205 aprint_naive(": Media interface\n"); 206 aprint_normal(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2)); 207 208 sc->mii_inst = mii->mii_instance; 209 sc->mii_phy = ma->mii_phyno; 210 sc->mii_pdata = mii; 211 sc->mii_flags = ma->mii_flags; 212 sc->mii_anegticks = 5; 213 214 switch (MII_MODEL(ma->mii_id2)) { 215 case MII_MODEL_BROADCOM_BCM5400: 216 sc->mii_funcs = &brgphy_5401_funcs; 217 aprint_normal("%s: using BCM5401 DSP patch\n", 218 sc->mii_dev.dv_xname); 219 break; 220 221 case MII_MODEL_BROADCOM_BCM5401: 222 if (MII_REV(ma->mii_id2) == 1 || MII_REV(ma->mii_id2) == 3) { 223 sc->mii_funcs = &brgphy_5401_funcs; 224 aprint_normal("%s: using BCM5401 DSP patch\n", 225 sc->mii_dev.dv_xname); 226 } else 227 sc->mii_funcs = &brgphy_funcs; 228 break; 229 230 case MII_MODEL_BROADCOM_BCM5411: 231 sc->mii_funcs = &brgphy_5411_funcs; 232 aprint_normal("%s: using BCM5411 DSP patch\n", 233 sc->mii_dev.dv_xname); 234 break; 235 236 #ifdef notyet /* unverified, untested */ 237 case MII_MODEL_BROADCOM_BCM5703: 238 sc->mii_funcs = &brgphy_5703_funcs; 239 aprint_normal("%s: using BCM5703 DSP patch\n", 240 sc->mii_dev.dv_xname); 241 break; 242 #endif 243 244 case MII_MODEL_BROADCOM_BCM5704: 245 sc->mii_funcs = &brgphy_5704_funcs; 246 aprint_normal("%s: using BCM5704 DSP patch\n", 247 sc->mii_dev.dv_xname); 248 break; 249 250 case MII_MODEL_BROADCOM_BCM5705: 251 sc->mii_funcs = &brgphy_5705_funcs; 252 break; 253 254 case MII_MODEL_BROADCOM_BCM5714: 255 case MII_MODEL_BROADCOM_BCM5780: 256 case MII_MODEL_BROADCOM_BCM5750: 257 sc->mii_funcs = &brgphy_5750_funcs; 258 break; 259 260 default: 261 sc->mii_funcs = &brgphy_funcs; 262 break; 263 } 264 265 PHY_RESET(sc); 266 267 sc->mii_capabilities = 268 PHY_READ(sc, MII_BMSR) & ma->mii_capmask; 269 if (sc->mii_capabilities & BMSR_EXTSTAT) 270 sc->mii_extcapabilities = PHY_READ(sc, MII_EXTSR); 271 272 aprint_normal("%s: ", sc->mii_dev.dv_xname); 273 if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0 && 274 (sc->mii_extcapabilities & EXTSR_MEDIAMASK) == 0) 275 aprint_error("no media present"); 276 else 277 mii_phy_add_media(sc); 278 aprint_normal("\n"); 279 } 280 281 static int 282 brgphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd) 283 { 284 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 285 int reg; 286 287 switch (cmd) { 288 case MII_POLLSTAT: 289 /* 290 * If we're not polling our PHY instance, just return. 291 */ 292 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 293 return (0); 294 break; 295 296 case MII_MEDIACHG: 297 /* 298 * If the media indicates a different PHY instance, 299 * isolate ourselves. 300 */ 301 if (IFM_INST(ife->ifm_media) != sc->mii_inst) { 302 reg = PHY_READ(sc, MII_BMCR); 303 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO); 304 return (0); 305 } 306 307 /* 308 * If the interface is not up, don't do anything. 309 */ 310 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 311 break; 312 313 if (sc->mii_funcs != &brgphy_5705_funcs) 314 mii_phy_reset(sc); /* XXX hardware bug work-around */ 315 mii_phy_setmedia(sc); 316 break; 317 318 case MII_TICK: 319 /* 320 * If we're not currently selected, just return. 321 */ 322 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 323 return (0); 324 325 if (mii_phy_tick(sc) == EJUSTRETURN) 326 return (0); 327 break; 328 329 case MII_DOWN: 330 mii_phy_down(sc); 331 return (0); 332 } 333 334 /* Update the media status. */ 335 mii_phy_status(sc); 336 337 /* 338 * Callback if something changed. Note that we need to poke 339 * the DSP on the Broadcom PHYs if the media changes. 340 */ 341 if (sc->mii_media_active != mii->mii_media_active || 342 sc->mii_media_status != mii->mii_media_status || 343 cmd == MII_MEDIACHG) { 344 mii_phy_update(sc, cmd); 345 if (sc->mii_funcs == &brgphy_5401_funcs) 346 bcm5401_load_dspcode(sc); 347 else if (sc->mii_funcs == &brgphy_5411_funcs) 348 bcm5411_load_dspcode(sc); 349 } 350 return (0); 351 } 352 353 static void 354 brgphy_status(struct mii_softc *sc) 355 { 356 struct mii_data *mii = sc->mii_pdata; 357 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 358 int bmcr, auxsts, gtsr; 359 360 mii->mii_media_status = IFM_AVALID; 361 mii->mii_media_active = IFM_ETHER; 362 363 auxsts = PHY_READ(sc, BRGPHY_MII_AUXSTS); 364 365 if (auxsts & BRGPHY_AUXSTS_LINK) 366 mii->mii_media_status |= IFM_ACTIVE; 367 368 bmcr = PHY_READ(sc, MII_BMCR); 369 if (bmcr & BMCR_ISO) { 370 mii->mii_media_active |= IFM_NONE; 371 mii->mii_media_status = 0; 372 return; 373 } 374 375 if (bmcr & BMCR_LOOP) 376 mii->mii_media_active |= IFM_LOOP; 377 378 if (bmcr & BMCR_AUTOEN) { 379 /* 380 * The media status bits are only valid of autonegotiation 381 * has completed (or it's disabled). 382 */ 383 if ((auxsts & BRGPHY_AUXSTS_ACOMP) == 0) { 384 /* Erg, still trying, I guess... */ 385 mii->mii_media_active |= IFM_NONE; 386 return; 387 } 388 389 switch (auxsts & BRGPHY_AUXSTS_AN_RES) { 390 case BRGPHY_RES_1000FD: 391 mii->mii_media_active |= IFM_1000_T|IFM_FDX; 392 gtsr = PHY_READ(sc, MII_100T2SR); 393 if (gtsr & GTSR_MS_RES) 394 mii->mii_media_active |= IFM_ETH_MASTER; 395 break; 396 397 case BRGPHY_RES_1000HD: 398 mii->mii_media_active |= IFM_1000_T; 399 gtsr = PHY_READ(sc, MII_100T2SR); 400 if (gtsr & GTSR_MS_RES) 401 mii->mii_media_active |= IFM_ETH_MASTER; 402 break; 403 404 case BRGPHY_RES_100FD: 405 mii->mii_media_active |= IFM_100_TX|IFM_FDX; 406 break; 407 408 case BRGPHY_RES_100T4: 409 mii->mii_media_active |= IFM_100_T4; 410 break; 411 412 case BRGPHY_RES_100HD: 413 mii->mii_media_active |= IFM_100_TX; 414 break; 415 416 case BRGPHY_RES_10FD: 417 mii->mii_media_active |= IFM_10_T|IFM_FDX; 418 break; 419 420 case BRGPHY_RES_10HD: 421 mii->mii_media_active |= IFM_10_T; 422 break; 423 424 default: 425 mii->mii_media_active |= IFM_NONE; 426 mii->mii_media_status = 0; 427 } 428 if (mii->mii_media_active & IFM_FDX) 429 mii->mii_media_active |= mii_phy_flowstatus(sc); 430 } else 431 mii->mii_media_active = ife->ifm_media; 432 } 433 434 static void 435 brgphy_5401_reset(struct mii_softc *sc) 436 { 437 438 mii_phy_reset(sc); 439 bcm5401_load_dspcode(sc); 440 } 441 442 static void 443 brgphy_5411_reset(struct mii_softc *sc) 444 { 445 446 mii_phy_reset(sc); 447 bcm5411_load_dspcode(sc); 448 } 449 450 451 static void 452 brgphy_5703_reset(struct mii_softc *sc) 453 { 454 455 mii_phy_reset(sc); 456 bcm5703_load_dspcode(sc); 457 } 458 459 static void 460 brgphy_5704_reset(struct mii_softc *sc) 461 { 462 463 mii_phy_reset(sc); 464 bcm5704_load_dspcode(sc); 465 } 466 467 /* 468 * Hardware bug workaround. Do nothing since after 469 * reset the 5705 PHY would get stuck in 10/100 MII mode. 470 */ 471 472 static void 473 brgphy_5705_reset(struct mii_softc *sc __unused) 474 { 475 } 476 477 static void 478 brgphy_5750_reset(struct mii_softc *sc) 479 { 480 mii_phy_reset(sc); 481 bcm5750_load_dspcode(sc); 482 } 483 484 /* Turn off tap power management on 5401. */ 485 static void 486 bcm5401_load_dspcode(struct mii_softc *sc) 487 { 488 static const struct { 489 int reg; 490 uint16_t val; 491 } dspcode[] = { 492 { BRGPHY_MII_AUXCTL, 0x0c20 }, 493 { BRGPHY_MII_DSP_ADDR_REG, 0x0012 }, 494 { BRGPHY_MII_DSP_RW_PORT, 0x1804 }, 495 { BRGPHY_MII_DSP_ADDR_REG, 0x0013 }, 496 { BRGPHY_MII_DSP_RW_PORT, 0x1204 }, 497 { BRGPHY_MII_DSP_ADDR_REG, 0x8006 }, 498 { BRGPHY_MII_DSP_RW_PORT, 0x0132 }, 499 { BRGPHY_MII_DSP_ADDR_REG, 0x8006 }, 500 { BRGPHY_MII_DSP_RW_PORT, 0x0232 }, 501 { BRGPHY_MII_DSP_ADDR_REG, 0x201f }, 502 { BRGPHY_MII_DSP_RW_PORT, 0x0a20 }, 503 { 0, 0 }, 504 }; 505 int i; 506 507 for (i = 0; dspcode[i].reg != 0; i++) 508 PHY_WRITE(sc, dspcode[i].reg, dspcode[i].val); 509 delay(40); 510 } 511 512 static void 513 bcm5411_load_dspcode(struct mii_softc *sc) 514 { 515 static const struct { 516 int reg; 517 uint16_t val; 518 } dspcode[] = { 519 { 0x1c, 0x8c23 }, 520 { 0x1c, 0x8ca3 }, 521 { 0x1c, 0x8c23 }, 522 { 0, 0 }, 523 }; 524 int i; 525 526 for (i = 0; dspcode[i].reg != 0; i++) 527 PHY_WRITE(sc, dspcode[i].reg, dspcode[i].val); 528 } 529 530 static void 531 bcm5703_load_dspcode(struct mii_softc *sc) 532 { 533 static const struct { 534 int reg; 535 uint16_t val; 536 } dspcode[] = { 537 { BRGPHY_MII_AUXCTL, 0x0c00 }, 538 { BRGPHY_MII_DSP_ADDR_REG, 0x201f }, 539 { BRGPHY_MII_DSP_RW_PORT, 0x2aaa }, 540 { 0, 0 }, 541 }; 542 int i; 543 544 for (i = 0; dspcode[i].reg != 0; i++) 545 PHY_WRITE(sc, dspcode[i].reg, dspcode[i].val); 546 } 547 548 static void 549 bcm5704_load_dspcode(struct mii_softc *sc) 550 { 551 static const struct { 552 int reg; 553 uint16_t val; 554 } dspcode[] = { 555 { 0x1c, 0x8d68 }, 556 { 0x1c, 0x8d68 }, 557 { 0, 0 }, 558 }; 559 int i; 560 561 for (i = 0; dspcode[i].reg != 0; i++) 562 PHY_WRITE(sc, dspcode[i].reg, dspcode[i].val); 563 } 564 565 static void 566 bcm5750_load_dspcode(struct mii_softc *sc) 567 { 568 static const struct { 569 int reg; 570 uint16_t val; 571 } dspcode[] = { 572 { BRGPHY_MII_AUXCTL, 0x0c00 }, 573 { BRGPHY_MII_DSP_ADDR_REG, 0x000a }, 574 { BRGPHY_MII_DSP_RW_PORT, 0x310b }, 575 { BRGPHY_MII_DSP_ADDR_REG, 0x201f }, 576 { BRGPHY_MII_DSP_RW_PORT, 0x9506 }, 577 { BRGPHY_MII_DSP_ADDR_REG, 0x401f }, 578 { BRGPHY_MII_DSP_RW_PORT, 0x14e2 }, 579 { BRGPHY_MII_AUXCTL, 0x0400 }, 580 { 0, 0 }, 581 }; 582 int i; 583 584 for (i = 0; dspcode[i].reg != 0; i++) 585 PHY_WRITE(sc, dspcode[i].reg, dspcode[i].val); 586 } 587