1 /* 2 * Copyright (c) 2001 Wind River Systems 3 * Copyright (c) 2001 4 * Bill Paul <wpaul@bsdi.com>. 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, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed by Bill Paul. 17 * 4. Neither the name of the author nor the names of any co-contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD 25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 31 * THE POSSIBILITY OF SUCH DAMAGE. 32 * 33 * $FreeBSD: src/sys/dev/mii/nsgphy.c,v 1.1.2.3 2002/11/08 21:53:49 semenu Exp $ 34 * $DragonFly: src/sys/dev/netif/mii_layer/nsgphy.c,v 1.4 2003/08/27 09:38:31 rob Exp $ 35 * 36 * $FreeBSD: src/sys/dev/mii/nsgphy.c,v 1.1.2.3 2002/11/08 21:53:49 semenu Exp $ 37 */ 38 39 /* 40 * Driver for the National Semiconductor DP83891 and DP83861 41 * 10/100/1000 PHYs. 42 * Datasheet available at: http://www.national.com/ds/DP/DP83861.pdf 43 * 44 * The DP83891 is the older NatSemi gigE PHY which isn't being sold 45 * anymore. The DP83861 is its replacement, which is an 'enhanced' 46 * firmware driven component. The major difference between the 47 * two is that the 83891 can't generate interrupts, while the 48 * 83861 can. (I think it wasn't originally designed to do this, but 49 * it can now thanks to firmware updates.) The 83861 also allows 50 * access to its internal RAM via indirect register access. 51 */ 52 53 #include <sys/param.h> 54 #include <sys/systm.h> 55 #include <sys/kernel.h> 56 #include <sys/socket.h> 57 #include <sys/bus.h> 58 59 #include <machine/clock.h> 60 61 #include <net/if.h> 62 #include <net/if_media.h> 63 64 #include "mii.h" 65 #include "miivar.h" 66 #include "miidevs.h" 67 68 #include "nsgphyreg.h" 69 70 #include "miibus_if.h" 71 72 static int nsgphy_probe (device_t); 73 static int nsgphy_attach (device_t); 74 static int nsgphy_detach (device_t); 75 76 static device_method_t nsgphy_methods[] = { 77 /* device interface */ 78 DEVMETHOD(device_probe, nsgphy_probe), 79 DEVMETHOD(device_attach, nsgphy_attach), 80 DEVMETHOD(device_detach, nsgphy_detach), 81 DEVMETHOD(device_shutdown, bus_generic_shutdown), 82 { 0, 0 } 83 }; 84 85 static devclass_t nsgphy_devclass; 86 87 static driver_t nsgphy_driver = { 88 "nsgphy", 89 nsgphy_methods, 90 sizeof(struct mii_softc) 91 }; 92 93 DRIVER_MODULE(nsgphy, miibus, nsgphy_driver, nsgphy_devclass, 0, 0); 94 95 int nsgphy_service (struct mii_softc *, struct mii_data *, int); 96 void nsgphy_status (struct mii_softc *); 97 98 static int nsgphy_mii_phy_auto (struct mii_softc *, int); 99 extern void mii_phy_auto_timeout (void *); 100 101 static int nsgphy_probe(dev) 102 device_t dev; 103 { 104 struct mii_attach_args *ma; 105 106 ma = device_get_ivars(dev); 107 108 if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_NATSEMI) { 109 if (MII_MODEL(ma->mii_id2) == MII_MODEL_NATSEMI_DP83891) { 110 device_set_desc(dev, MII_STR_NATSEMI_DP83891); 111 return(0); 112 } 113 if (MII_MODEL(ma->mii_id2) == MII_MODEL_NATSEMI_DP83861) { 114 device_set_desc(dev, MII_STR_NATSEMI_DP83861); 115 return(0); 116 } 117 } 118 119 return(ENXIO); 120 } 121 122 static int nsgphy_attach(dev) 123 device_t dev; 124 { 125 struct mii_softc *sc; 126 struct mii_attach_args *ma; 127 struct mii_data *mii; 128 const char *sep = ""; 129 130 sc = device_get_softc(dev); 131 ma = device_get_ivars(dev); 132 sc->mii_dev = device_get_parent(dev); 133 mii = device_get_softc(sc->mii_dev); 134 LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list); 135 136 sc->mii_inst = mii->mii_instance; 137 sc->mii_phy = ma->mii_phyno; 138 sc->mii_service = nsgphy_service; 139 sc->mii_pdata = mii; 140 141 sc->mii_flags |= MIIF_NOISOLATE; 142 mii->mii_instance++; 143 144 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL) 145 #define PRINT(s) printf("%s%s", sep, s); sep = ", " 146 147 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst), 148 BMCR_ISO); 149 #if 0 150 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->mii_inst), 151 BMCR_LOOP|BMCR_S100); 152 #endif 153 154 mii_phy_reset(sc); 155 156 device_printf(dev, " "); 157 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_TX, IFM_FDX, sc->mii_inst), 158 NSGPHY_S1000|NSGPHY_BMCR_FDX); 159 PRINT("1000baseTX-FDX"); 160 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_TX, 0, sc->mii_inst), 161 NSGPHY_S1000); 162 PRINT("1000baseTX"); 163 sc->mii_capabilities = 164 (PHY_READ(sc, MII_BMSR) | 165 (BMSR_10TFDX|BMSR_10THDX)) & ma->mii_capmask; 166 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, sc->mii_inst), 167 NSGPHY_S100|NSGPHY_BMCR_FDX); 168 PRINT("100baseTX-FDX"); 169 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, sc->mii_inst), NSGPHY_S100); 170 PRINT("100baseTX"); 171 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, sc->mii_inst), 172 NSGPHY_S10|NSGPHY_BMCR_FDX); 173 PRINT("10baseT-FDX"); 174 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, sc->mii_inst), NSGPHY_S10); 175 PRINT("10baseT"); 176 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst), 0); 177 PRINT("auto"); 178 printf("\n"); 179 #undef ADD 180 #undef PRINT 181 182 MIIBUS_MEDIAINIT(sc->mii_dev); 183 return(0); 184 } 185 186 static int nsgphy_detach(dev) 187 device_t dev; 188 { 189 struct mii_softc *sc; 190 struct mii_data *mii; 191 192 sc = device_get_softc(dev); 193 mii = device_get_softc(device_get_parent(dev)); 194 if (sc->mii_flags & MIIF_DOINGAUTO) 195 untimeout(mii_phy_auto_timeout, sc, sc->mii_auto_ch); 196 sc->mii_dev = NULL; 197 LIST_REMOVE(sc, mii_list); 198 199 return(0); 200 } 201 int 202 nsgphy_service(sc, mii, cmd) 203 struct mii_softc *sc; 204 struct mii_data *mii; 205 int cmd; 206 { 207 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 208 int reg; 209 210 switch (cmd) { 211 case MII_POLLSTAT: 212 /* 213 * If we're not polling our PHY instance, just return. 214 */ 215 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 216 return (0); 217 break; 218 219 case MII_MEDIACHG: 220 /* 221 * If the media indicates a different PHY instance, 222 * isolate ourselves. 223 */ 224 if (IFM_INST(ife->ifm_media) != sc->mii_inst) { 225 reg = PHY_READ(sc, MII_BMCR); 226 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO); 227 return (0); 228 } 229 230 /* 231 * If the interface is not up, don't do anything. 232 */ 233 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 234 break; 235 236 237 switch (IFM_SUBTYPE(ife->ifm_media)) { 238 case IFM_AUTO: 239 #ifdef foo 240 /* 241 * If we're already in auto mode, just return. 242 */ 243 if (PHY_READ(sc, NSGPHY_MII_BMCR) & NSGPHY_BMCR_AUTOEN) 244 return (0); 245 #endif 246 (void) nsgphy_mii_phy_auto(sc, 0); 247 break; 248 case IFM_1000_TX: 249 if ((ife->ifm_media & IFM_GMASK) == IFM_FDX) { 250 PHY_WRITE(sc, NSGPHY_MII_BMCR, 251 NSGPHY_BMCR_FDX|NSGPHY_BMCR_SPD1); 252 } else { 253 PHY_WRITE(sc, NSGPHY_MII_BMCR, 254 NSGPHY_BMCR_SPD1); 255 } 256 PHY_WRITE(sc, NSGPHY_MII_ANAR, NSGPHY_SEL_TYPE); 257 258 /* 259 * When setting the link manually, one side must 260 * be the master and the other the slave. However 261 * ifmedia doesn't give us a good way to specify 262 * this, so we fake it by using one of the LINK 263 * flags. If LINK0 is set, we program the PHY to 264 * be a master, otherwise it's a slave. 265 */ 266 if ((mii->mii_ifp->if_flags & IFF_LINK0)) { 267 PHY_WRITE(sc, NSGPHY_MII_1000CTL, 268 NSGPHY_1000CTL_MSE|NSGPHY_1000CTL_MSC); 269 } else { 270 PHY_WRITE(sc, NSGPHY_MII_1000CTL, 271 NSGPHY_1000CTL_MSE); 272 } 273 break; 274 case IFM_100_T4: 275 /* 276 * XXX Not supported as a manual setting right now. 277 */ 278 return (EINVAL); 279 case IFM_NONE: 280 PHY_WRITE(sc, MII_BMCR, BMCR_ISO|BMCR_PDOWN); 281 break; 282 default: 283 /* 284 * BMCR data is stored in the ifmedia entry. 285 */ 286 PHY_WRITE(sc, MII_ANAR, 287 mii_anar(ife->ifm_media)); 288 PHY_WRITE(sc, MII_BMCR, ife->ifm_data); 289 break; 290 } 291 break; 292 293 case MII_TICK: 294 /* 295 * If we're not currently selected, just return. 296 */ 297 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 298 return (0); 299 300 /* 301 * Only used for autonegotiation. 302 */ 303 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) 304 return (0); 305 306 /* 307 * Is the interface even up? 308 */ 309 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 310 return (0); 311 312 /* 313 * Only retry autonegotiation every 17 seconds. 314 * Actually, for gigE PHYs, we should wait longer, since 315 * 5 seconds is the mimimum time the documentation 316 * says to wait for a 1000mbps link to be established. 317 */ 318 if (++sc->mii_ticks != 17) 319 return (0); 320 321 sc->mii_ticks = 0; 322 323 /* 324 * Check to see if we have link. 325 */ 326 reg = PHY_READ(sc, NSGPHY_MII_PHYSUP); 327 if (reg & NSGPHY_PHYSUP_LNKSTS) 328 break; 329 330 mii_phy_reset(sc); 331 if (nsgphy_mii_phy_auto(sc, 0) == EJUSTRETURN) 332 return(0); 333 break; 334 } 335 336 /* Update the media status. */ 337 nsgphy_status(sc); 338 339 /* Callback if something changed. */ 340 if (sc->mii_active != mii->mii_media_active || cmd == MII_MEDIACHG) { 341 MIIBUS_STATCHG(sc->mii_dev); 342 sc->mii_active = mii->mii_media_active; 343 } 344 return (0); 345 } 346 347 void 348 nsgphy_status(sc) 349 struct mii_softc *sc; 350 { 351 struct mii_data *mii = sc->mii_pdata; 352 int bmsr, bmcr, physup, anlpar, gstat; 353 354 mii->mii_media_status = IFM_AVALID; 355 mii->mii_media_active = IFM_ETHER; 356 357 bmsr = PHY_READ(sc, NSGPHY_MII_BMSR); 358 physup = PHY_READ(sc, NSGPHY_MII_PHYSUP); 359 if (physup & NSGPHY_PHYSUP_LNKSTS) 360 mii->mii_media_status |= IFM_ACTIVE; 361 362 bmcr = PHY_READ(sc, NSGPHY_MII_BMCR); 363 364 if (bmcr & NSGPHY_BMCR_LOOP) 365 mii->mii_media_active |= IFM_LOOP; 366 367 if (bmcr & NSGPHY_BMCR_AUTOEN) { 368 if ((bmsr & NSGPHY_BMSR_ACOMP) == 0) { 369 /* Erg, still trying, I guess... */ 370 mii->mii_media_active |= IFM_NONE; 371 return; 372 } 373 anlpar = PHY_READ(sc, NSGPHY_MII_ANLPAR); 374 gstat = PHY_READ(sc, NSGPHY_MII_1000STS); 375 if (gstat & NSGPHY_1000STS_LPFD) 376 mii->mii_media_active |= IFM_1000_TX|IFM_FDX; 377 else if (gstat & NSGPHY_1000STS_LPHD) 378 mii->mii_media_active |= IFM_1000_TX|IFM_HDX; 379 else if (anlpar & NSGPHY_ANLPAR_100T4) 380 mii->mii_media_active |= IFM_100_T4; 381 else if (anlpar & NSGPHY_ANLPAR_100FDX) 382 mii->mii_media_active |= IFM_100_TX|IFM_FDX; 383 else if (anlpar & NSGPHY_ANLPAR_100HDX) 384 mii->mii_media_active |= IFM_100_TX; 385 else if (anlpar & NSGPHY_ANLPAR_10FDX) 386 mii->mii_media_active |= IFM_10_T|IFM_FDX; 387 else if (anlpar & NSGPHY_ANLPAR_10HDX) 388 mii->mii_media_active |= IFM_10_T|IFM_HDX; 389 else 390 mii->mii_media_active |= IFM_NONE; 391 return; 392 } 393 394 switch(bmcr & (NSGPHY_BMCR_SPD1|NSGPHY_BMCR_SPD0)) { 395 case NSGPHY_S1000: 396 mii->mii_media_active |= IFM_1000_TX; 397 break; 398 case NSGPHY_S100: 399 mii->mii_media_active |= IFM_100_TX; 400 break; 401 case NSGPHY_S10: 402 mii->mii_media_active |= IFM_10_T; 403 break; 404 default: 405 break; 406 } 407 408 if (bmcr & NSGPHY_BMCR_FDX) 409 mii->mii_media_active |= IFM_FDX; 410 else 411 mii->mii_media_active |= IFM_HDX; 412 413 return; 414 } 415 416 417 static int 418 nsgphy_mii_phy_auto(mii, waitfor) 419 struct mii_softc *mii; 420 int waitfor; 421 { 422 int bmsr, ktcr = 0, i; 423 424 if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) { 425 mii_phy_reset(mii); 426 PHY_WRITE(mii, NSGPHY_MII_BMCR, 0); 427 DELAY(1000); 428 ktcr = PHY_READ(mii, NSGPHY_MII_1000CTL); 429 PHY_WRITE(mii, NSGPHY_MII_1000CTL, ktcr | 430 (NSGPHY_1000CTL_AFD|NSGPHY_1000CTL_AHD)); 431 ktcr = PHY_READ(mii, NSGPHY_MII_1000CTL); 432 DELAY(1000); 433 PHY_WRITE(mii, NSGPHY_MII_ANAR, 434 BMSR_MEDIA_TO_ANAR(mii->mii_capabilities) | ANAR_CSMA); 435 DELAY(1000); 436 PHY_WRITE(mii, NSGPHY_MII_BMCR, 437 NSGPHY_BMCR_AUTOEN | NSGPHY_BMCR_STARTNEG); 438 } 439 440 if (waitfor) { 441 /* Wait 500ms for it to complete. */ 442 for (i = 0; i < 500; i++) { 443 if ((bmsr = PHY_READ(mii, NSGPHY_MII_BMSR)) & 444 NSGPHY_BMSR_ACOMP) 445 return (0); 446 DELAY(1000); 447 #if 0 448 if ((bmsr & BMSR_ACOMP) == 0) 449 printf("%s: autonegotiation failed to complete\n", 450 mii->mii_dev.dv_xname); 451 #endif 452 } 453 454 /* 455 * Don't need to worry about clearing MIIF_DOINGAUTO. 456 * If that's set, a timeout is pending, and it will 457 * clear the flag. 458 */ 459 return (EIO); 460 } 461 462 /* 463 * Just let it finish asynchronously. This is for the benefit of 464 * the tick handler driving autonegotiation. Don't want 500ms 465 * delays all the time while the system is running! 466 */ 467 if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) { 468 mii->mii_flags |= MIIF_DOINGAUTO; 469 mii->mii_auto_ch = timeout(mii_phy_auto_timeout, mii, hz >> 1); 470 } 471 return (EJUSTRETURN); 472 } 473