1 /* $NetBSD: brgphy.c,v 1.27 2006/04/27 16:43:14 jonathan 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.27 2006/04/27 16:43:14 jonathan 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, struct cfdata *match, void *aux) 186 { 187 struct mii_attach_args *ma = aux; 188 189 if (mii_phy_match(ma, brgphys) != NULL) 190 return (10); 191 192 return (0); 193 } 194 195 static void 196 brgphyattach(struct device *parent, struct device *self, void *aux) 197 { 198 struct mii_softc *sc = device_private(self); 199 struct mii_attach_args *ma = aux; 200 struct mii_data *mii = ma->mii_data; 201 const struct mii_phydesc *mpd; 202 203 mpd = mii_phy_match(ma, brgphys); 204 aprint_naive(": Media interface\n"); 205 aprint_normal(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2)); 206 207 sc->mii_inst = mii->mii_instance; 208 sc->mii_phy = ma->mii_phyno; 209 sc->mii_pdata = mii; 210 sc->mii_flags = ma->mii_flags; 211 sc->mii_anegticks = 5; 212 213 switch (MII_MODEL(ma->mii_id2)) { 214 case MII_MODEL_BROADCOM_BCM5400: 215 sc->mii_funcs = &brgphy_5401_funcs; 216 aprint_normal("%s: using BCM5401 DSP patch\n", 217 sc->mii_dev.dv_xname); 218 break; 219 220 case MII_MODEL_BROADCOM_BCM5401: 221 if (MII_REV(ma->mii_id2) == 1 || MII_REV(ma->mii_id2) == 3) { 222 sc->mii_funcs = &brgphy_5401_funcs; 223 aprint_normal("%s: using BCM5401 DSP patch\n", 224 sc->mii_dev.dv_xname); 225 } else 226 sc->mii_funcs = &brgphy_funcs; 227 break; 228 229 case MII_MODEL_BROADCOM_BCM5411: 230 sc->mii_funcs = &brgphy_5411_funcs; 231 aprint_normal("%s: using BCM5411 DSP patch\n", 232 sc->mii_dev.dv_xname); 233 break; 234 235 #ifdef notyet /* unverified, untested */ 236 case MII_MODEL_BROADCOM_BCM5703: 237 sc->mii_funcs = &brgphy_5703_funcs; 238 aprint_normal("%s: using BCM5703 DSP patch\n", 239 sc->mii_dev.dv_xname); 240 break; 241 #endif 242 243 case MII_MODEL_BROADCOM_BCM5704: 244 sc->mii_funcs = &brgphy_5704_funcs; 245 aprint_normal("%s: using BCM5704 DSP patch\n", 246 sc->mii_dev.dv_xname); 247 break; 248 249 case MII_MODEL_BROADCOM_BCM5705: 250 sc->mii_funcs = &brgphy_5705_funcs; 251 break; 252 253 case MII_MODEL_BROADCOM_BCM5714: 254 case MII_MODEL_BROADCOM_BCM5780: 255 case MII_MODEL_BROADCOM_BCM5750: 256 sc->mii_funcs = &brgphy_5750_funcs; 257 break; 258 259 default: 260 sc->mii_funcs = &brgphy_funcs; 261 break; 262 } 263 264 PHY_RESET(sc); 265 266 sc->mii_capabilities = 267 PHY_READ(sc, MII_BMSR) & ma->mii_capmask; 268 if (sc->mii_capabilities & BMSR_EXTSTAT) 269 sc->mii_extcapabilities = PHY_READ(sc, MII_EXTSR); 270 271 aprint_normal("%s: ", sc->mii_dev.dv_xname); 272 if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0 && 273 (sc->mii_extcapabilities & EXTSR_MEDIAMASK) == 0) 274 aprint_error("no media present"); 275 else 276 mii_phy_add_media(sc); 277 aprint_normal("\n"); 278 } 279 280 static int 281 brgphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd) 282 { 283 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 284 int reg; 285 286 switch (cmd) { 287 case MII_POLLSTAT: 288 /* 289 * If we're not polling our PHY instance, just return. 290 */ 291 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 292 return (0); 293 break; 294 295 case MII_MEDIACHG: 296 /* 297 * If the media indicates a different PHY instance, 298 * isolate ourselves. 299 */ 300 if (IFM_INST(ife->ifm_media) != sc->mii_inst) { 301 reg = PHY_READ(sc, MII_BMCR); 302 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO); 303 return (0); 304 } 305 306 /* 307 * If the interface is not up, don't do anything. 308 */ 309 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 310 break; 311 312 if (sc->mii_funcs != &brgphy_5705_funcs) 313 mii_phy_reset(sc); /* XXX hardware bug work-around */ 314 mii_phy_setmedia(sc); 315 break; 316 317 case MII_TICK: 318 /* 319 * If we're not currently selected, just return. 320 */ 321 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 322 return (0); 323 324 if (mii_phy_tick(sc) == EJUSTRETURN) 325 return (0); 326 break; 327 328 case MII_DOWN: 329 mii_phy_down(sc); 330 return (0); 331 } 332 333 /* Update the media status. */ 334 mii_phy_status(sc); 335 336 /* 337 * Callback if something changed. Note that we need to poke 338 * the DSP on the Broadcom PHYs if the media changes. 339 */ 340 if (sc->mii_media_active != mii->mii_media_active || 341 sc->mii_media_status != mii->mii_media_status || 342 cmd == MII_MEDIACHG) { 343 mii_phy_update(sc, cmd); 344 if (sc->mii_funcs == &brgphy_5401_funcs) 345 bcm5401_load_dspcode(sc); 346 else if (sc->mii_funcs == &brgphy_5411_funcs) 347 bcm5411_load_dspcode(sc); 348 } 349 return (0); 350 } 351 352 static void 353 brgphy_status(struct mii_softc *sc) 354 { 355 struct mii_data *mii = sc->mii_pdata; 356 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 357 int bmcr, auxsts, gtsr; 358 359 mii->mii_media_status = IFM_AVALID; 360 mii->mii_media_active = IFM_ETHER; 361 362 auxsts = PHY_READ(sc, BRGPHY_MII_AUXSTS); 363 364 if (auxsts & BRGPHY_AUXSTS_LINK) 365 mii->mii_media_status |= IFM_ACTIVE; 366 367 bmcr = PHY_READ(sc, MII_BMCR); 368 if (bmcr & BMCR_ISO) { 369 mii->mii_media_active |= IFM_NONE; 370 mii->mii_media_status = 0; 371 return; 372 } 373 374 if (bmcr & BMCR_LOOP) 375 mii->mii_media_active |= IFM_LOOP; 376 377 if (bmcr & BMCR_AUTOEN) { 378 /* 379 * The media status bits are only valid of autonegotiation 380 * has completed (or it's disabled). 381 */ 382 if ((auxsts & BRGPHY_AUXSTS_ACOMP) == 0) { 383 /* Erg, still trying, I guess... */ 384 mii->mii_media_active |= IFM_NONE; 385 return; 386 } 387 388 switch (auxsts & BRGPHY_AUXSTS_AN_RES) { 389 case BRGPHY_RES_1000FD: 390 mii->mii_media_active |= IFM_1000_T|IFM_FDX; 391 gtsr = PHY_READ(sc, MII_100T2SR); 392 if (gtsr & GTSR_MS_RES) 393 mii->mii_media_active |= IFM_ETH_MASTER; 394 break; 395 396 case BRGPHY_RES_1000HD: 397 mii->mii_media_active |= IFM_1000_T; 398 gtsr = PHY_READ(sc, MII_100T2SR); 399 if (gtsr & GTSR_MS_RES) 400 mii->mii_media_active |= IFM_ETH_MASTER; 401 break; 402 403 case BRGPHY_RES_100FD: 404 mii->mii_media_active |= IFM_100_TX|IFM_FDX; 405 break; 406 407 case BRGPHY_RES_100T4: 408 mii->mii_media_active |= IFM_100_T4; 409 break; 410 411 case BRGPHY_RES_100HD: 412 mii->mii_media_active |= IFM_100_TX; 413 break; 414 415 case BRGPHY_RES_10FD: 416 mii->mii_media_active |= IFM_10_T|IFM_FDX; 417 break; 418 419 case BRGPHY_RES_10HD: 420 mii->mii_media_active |= IFM_10_T; 421 break; 422 423 default: 424 mii->mii_media_active |= IFM_NONE; 425 mii->mii_media_status = 0; 426 } 427 if (mii->mii_media_active & IFM_FDX) 428 mii->mii_media_active |= mii_phy_flowstatus(sc); 429 } else 430 mii->mii_media_active = ife->ifm_media; 431 } 432 433 static void 434 brgphy_5401_reset(struct mii_softc *sc) 435 { 436 437 mii_phy_reset(sc); 438 bcm5401_load_dspcode(sc); 439 } 440 441 static void 442 brgphy_5411_reset(struct mii_softc *sc) 443 { 444 445 mii_phy_reset(sc); 446 bcm5411_load_dspcode(sc); 447 } 448 449 450 static void 451 brgphy_5703_reset(struct mii_softc *sc) 452 { 453 454 mii_phy_reset(sc); 455 bcm5703_load_dspcode(sc); 456 } 457 458 static void 459 brgphy_5704_reset(struct mii_softc *sc) 460 { 461 462 mii_phy_reset(sc); 463 bcm5704_load_dspcode(sc); 464 } 465 466 /* 467 * Hardware bug workaround. Do nothing since after 468 * reset the 5705 PHY would get stuck in 10/100 MII mode. 469 */ 470 471 static void 472 brgphy_5705_reset(struct mii_softc *sc) 473 { 474 } 475 476 static void 477 brgphy_5750_reset(struct mii_softc *sc) 478 { 479 mii_phy_reset(sc); 480 bcm5750_load_dspcode(sc); 481 } 482 483 /* Turn off tap power management on 5401. */ 484 static void 485 bcm5401_load_dspcode(struct mii_softc *sc) 486 { 487 static const struct { 488 int reg; 489 uint16_t val; 490 } dspcode[] = { 491 { BRGPHY_MII_AUXCTL, 0x0c20 }, 492 { BRGPHY_MII_DSP_ADDR_REG, 0x0012 }, 493 { BRGPHY_MII_DSP_RW_PORT, 0x1804 }, 494 { BRGPHY_MII_DSP_ADDR_REG, 0x0013 }, 495 { BRGPHY_MII_DSP_RW_PORT, 0x1204 }, 496 { BRGPHY_MII_DSP_ADDR_REG, 0x8006 }, 497 { BRGPHY_MII_DSP_RW_PORT, 0x0132 }, 498 { BRGPHY_MII_DSP_ADDR_REG, 0x8006 }, 499 { BRGPHY_MII_DSP_RW_PORT, 0x0232 }, 500 { BRGPHY_MII_DSP_ADDR_REG, 0x201f }, 501 { BRGPHY_MII_DSP_RW_PORT, 0x0a20 }, 502 { 0, 0 }, 503 }; 504 int i; 505 506 for (i = 0; dspcode[i].reg != 0; i++) 507 PHY_WRITE(sc, dspcode[i].reg, dspcode[i].val); 508 delay(40); 509 } 510 511 static void 512 bcm5411_load_dspcode(struct mii_softc *sc) 513 { 514 static const struct { 515 int reg; 516 uint16_t val; 517 } dspcode[] = { 518 { 0x1c, 0x8c23 }, 519 { 0x1c, 0x8ca3 }, 520 { 0x1c, 0x8c23 }, 521 { 0, 0 }, 522 }; 523 int i; 524 525 for (i = 0; dspcode[i].reg != 0; i++) 526 PHY_WRITE(sc, dspcode[i].reg, dspcode[i].val); 527 } 528 529 static void 530 bcm5703_load_dspcode(struct mii_softc *sc) 531 { 532 static const struct { 533 int reg; 534 uint16_t val; 535 } dspcode[] = { 536 { BRGPHY_MII_AUXCTL, 0x0c00 }, 537 { BRGPHY_MII_DSP_ADDR_REG, 0x201f }, 538 { BRGPHY_MII_DSP_RW_PORT, 0x2aaa }, 539 { 0, 0 }, 540 }; 541 int i; 542 543 for (i = 0; dspcode[i].reg != 0; i++) 544 PHY_WRITE(sc, dspcode[i].reg, dspcode[i].val); 545 } 546 547 static void 548 bcm5704_load_dspcode(struct mii_softc *sc) 549 { 550 static const struct { 551 int reg; 552 uint16_t val; 553 } dspcode[] = { 554 { 0x1c, 0x8d68 }, 555 { 0x1c, 0x8d68 }, 556 { 0, 0 }, 557 }; 558 int i; 559 560 for (i = 0; dspcode[i].reg != 0; i++) 561 PHY_WRITE(sc, dspcode[i].reg, dspcode[i].val); 562 } 563 564 static void 565 bcm5750_load_dspcode(struct mii_softc *sc) 566 { 567 static const struct { 568 int reg; 569 uint16_t val; 570 } dspcode[] = { 571 { BRGPHY_MII_AUXCTL, 0x0c00 }, 572 { BRGPHY_MII_DSP_ADDR_REG, 0x000a }, 573 { BRGPHY_MII_DSP_RW_PORT, 0x310b }, 574 { BRGPHY_MII_DSP_ADDR_REG, 0x201f }, 575 { BRGPHY_MII_DSP_RW_PORT, 0x9506 }, 576 { BRGPHY_MII_DSP_ADDR_REG, 0x401f }, 577 { BRGPHY_MII_DSP_RW_PORT, 0x14e2 }, 578 { BRGPHY_MII_AUXCTL, 0x0400 }, 579 { 0, 0 }, 580 }; 581 int i; 582 583 for (i = 0; dspcode[i].reg != 0; i++) 584 PHY_WRITE(sc, dspcode[i].reg, dspcode[i].val); 585 } 586