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