nsgphy.c (95707) | nsgphy.c (95718) |
---|---|
1/* 2 * Copyright (c) 2001 Wind River Systems 3 * Copyright (c) 2001 4 * Bill Paul <wpaul@bsdi.com>. All rights reserved. | 1/* 2 * Copyright (c) 2001 Wind River Systems 3 * Copyright (c) 2001 4 * Bill Paul <wpaul@bsdi.com>. All rights reserved. |
5 * Copyright (c) 1998, 1999, 2000, 2001 The NetBSD Foundation, Inc. 6 * All rights reserved. |
|
5 * | 7 * |
8 * This code is derived from software contributed to The NetBSD Foundation 9 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 10 * NASA Ames Research Center. 11 * |
|
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. --- 11 unchanged lines hidden (view full) --- 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 * | 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. --- 11 unchanged lines hidden (view full) --- 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 37 * THE POSSIBILITY OF SUCH DAMAGE. 38 * |
33 * $FreeBSD: head/sys/dev/mii/nsgphy.c 95707 2002-04-29 07:18:26Z phk $ | 39 * $FreeBSD: head/sys/dev/mii/nsgphy.c 95718 2002-04-29 11:57:30Z phk $ |
34 */ 35 36/* 37 * Driver for the National Semiconductor DP83891 and DP83861 38 * 10/100/1000 PHYs. 39 * Datasheet available at: http://www.national.com/ds/DP/DP83861.pdf 40 * 41 * The DP83891 is the older NatSemi gigE PHY which isn't being sold --- 22 unchanged lines hidden (view full) --- 64#include <dev/mii/miidevs.h> 65 66#include <dev/mii/nsgphyreg.h> 67 68#include "miibus_if.h" 69 70#if !defined(lint) 71static const char rcsid[] = | 40 */ 41 42/* 43 * Driver for the National Semiconductor DP83891 and DP83861 44 * 10/100/1000 PHYs. 45 * Datasheet available at: http://www.national.com/ds/DP/DP83861.pdf 46 * 47 * The DP83891 is the older NatSemi gigE PHY which isn't being sold --- 22 unchanged lines hidden (view full) --- 70#include <dev/mii/miidevs.h> 71 72#include <dev/mii/nsgphyreg.h> 73 74#include "miibus_if.h" 75 76#if !defined(lint) 77static const char rcsid[] = |
72 "$FreeBSD: head/sys/dev/mii/nsgphy.c 95707 2002-04-29 07:18:26Z phk $"; | 78 "$FreeBSD: head/sys/dev/mii/nsgphy.c 95718 2002-04-29 11:57:30Z phk $"; |
73#endif 74 75static int nsgphy_probe (device_t); 76static int nsgphy_attach (device_t); 77static int nsgphy_detach (device_t); 78 79static device_method_t nsgphy_methods[] = { 80 /* device interface */ --- 11 unchanged lines hidden (view full) --- 92 nsgphy_methods, 93 sizeof(struct mii_softc) 94}; 95 96DRIVER_MODULE(nsgphy, miibus, nsgphy_driver, nsgphy_devclass, 0, 0); 97 98static int nsgphy_service(struct mii_softc *, struct mii_data *,int); 99static void nsgphy_status(struct mii_softc *); | 79#endif 80 81static int nsgphy_probe (device_t); 82static int nsgphy_attach (device_t); 83static int nsgphy_detach (device_t); 84 85static device_method_t nsgphy_methods[] = { 86 /* device interface */ --- 11 unchanged lines hidden (view full) --- 98 nsgphy_methods, 99 sizeof(struct mii_softc) 100}; 101 102DRIVER_MODULE(nsgphy, miibus, nsgphy_driver, nsgphy_devclass, 0, 0); 103 104static int nsgphy_service(struct mii_softc *, struct mii_data *,int); 105static void nsgphy_status(struct mii_softc *); |
100static int nsgphy_mii_phy_auto(struct mii_softc *, int); | |
101extern void mii_phy_auto_timeout(void *); 102 103static int 104nsgphy_probe(device_t dev) 105{ 106 struct mii_attach_args *ma; 107 108 ma = device_get_ivars(dev); --- 13 unchanged lines hidden (view full) --- 122} 123 124static int 125nsgphy_attach(device_t dev) 126{ 127 struct mii_softc *sc; 128 struct mii_attach_args *ma; 129 struct mii_data *mii; | 106extern void mii_phy_auto_timeout(void *); 107 108static int 109nsgphy_probe(device_t dev) 110{ 111 struct mii_attach_args *ma; 112 113 ma = device_get_ivars(dev); --- 13 unchanged lines hidden (view full) --- 127} 128 129static int 130nsgphy_attach(device_t dev) 131{ 132 struct mii_softc *sc; 133 struct mii_attach_args *ma; 134 struct mii_data *mii; |
130 const char *sep = ""; | |
131 132 sc = device_get_softc(dev); 133 ma = device_get_ivars(dev); 134 sc->mii_dev = device_get_parent(dev); 135 mii = device_get_softc(sc->mii_dev); 136 LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list); 137 138 sc->mii_inst = mii->mii_instance; 139 sc->mii_phy = ma->mii_phyno; 140 sc->mii_service = nsgphy_service; 141 sc->mii_pdata = mii; | 135 136 sc = device_get_softc(dev); 137 ma = device_get_ivars(dev); 138 sc->mii_dev = device_get_parent(dev); 139 mii = device_get_softc(sc->mii_dev); 140 LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list); 141 142 sc->mii_inst = mii->mii_instance; 143 sc->mii_phy = ma->mii_phyno; 144 sc->mii_service = nsgphy_service; 145 sc->mii_pdata = mii; |
146 sc->mii_anegticks = 5; |
|
142 | 147 |
143 sc->mii_flags |= MIIF_NOISOLATE; | |
144 mii->mii_instance++; 145 | 148 mii->mii_instance++; 149 |
146#define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL) 147#define PRINT(s) printf("%s%s", sep, s); sep = ", " 148 149 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst), 150 BMCR_ISO); 151#if 0 152 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->mii_inst), 153 BMCR_LOOP|BMCR_S100); 154#endif 155 156 mii_phy_reset(sc); 157 158 device_printf(dev, " "); 159 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, IFM_FDX, sc->mii_inst), 160 BMCR_S1000|BMCR_FDX); 161 PRINT("1000baseTX-FDX"); 162 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, 0, sc->mii_inst), 163 BMCR_S1000); 164 PRINT("1000baseTX"); | |
165 sc->mii_capabilities = (PHY_READ(sc, MII_BMSR) | 166 (BMSR_10TFDX|BMSR_10THDX)) & ma->mii_capmask; 167 if (sc->mii_capabilities & BMSR_EXTSTAT) 168 sc->mii_extcapabilities = PHY_READ(sc, MII_EXTSR); | 150 sc->mii_capabilities = (PHY_READ(sc, MII_BMSR) | 151 (BMSR_10TFDX|BMSR_10THDX)) & ma->mii_capmask; 152 if (sc->mii_capabilities & BMSR_EXTSTAT) 153 sc->mii_extcapabilities = PHY_READ(sc, MII_EXTSR); |
169 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, sc->mii_inst), 170 BMCR_S100|BMCR_FDX); 171 PRINT("100baseTX-FDX"); 172 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, sc->mii_inst), BMCR_S100); 173 PRINT("100baseTX"); 174 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, sc->mii_inst), 175 BMCR_S10|BMCR_FDX); 176 PRINT("10baseT-FDX"); 177 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, sc->mii_inst), BMCR_S10); 178 PRINT("10baseT"); 179 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst), 0); 180 PRINT("auto"); | 154 155 mii_phy_add_media(sc); |
181 printf("\n"); | 156 printf("\n"); |
182#undef ADD 183#undef PRINT | |
184 185 MIIBUS_MEDIAINIT(sc->mii_dev); 186 return(0); 187} 188 189static int 190nsgphy_detach(device_t dev) 191{ --- 37 unchanged lines hidden (view full) --- 229 } 230 231 /* 232 * If the interface is not up, don't do anything. 233 */ 234 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 235 break; 236 | 157 158 MIIBUS_MEDIAINIT(sc->mii_dev); 159 return(0); 160} 161 162static int 163nsgphy_detach(device_t dev) 164{ --- 37 unchanged lines hidden (view full) --- 202 } 203 204 /* 205 * If the interface is not up, don't do anything. 206 */ 207 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 208 break; 209 |
237 238 switch (IFM_SUBTYPE(ife->ifm_media)) { 239 case IFM_AUTO: 240#ifdef foo 241 /* 242 * If we're already in auto mode, just return. 243 */ 244 if (PHY_READ(sc, MII_BMCR) & BMCR_AUTOEN) 245 return (0); 246#endif 247 (void) nsgphy_mii_phy_auto(sc, 0); 248 break; 249 case IFM_1000_T: 250 if ((ife->ifm_media & IFM_GMASK) == IFM_FDX) { 251 PHY_WRITE(sc, MII_BMCR, 252 BMCR_FDX|BMCR_SPEED1); 253 } else { 254 PHY_WRITE(sc, MII_BMCR, 255 BMCR_SPEED1); 256 } 257 PHY_WRITE(sc, MII_ANAR, ANAR_CSMA); 258 259 /* 260 * When setting the link manually, one side must 261 * be the master and the other the slave. However 262 * ifmedia doesn't give us a good way to specify 263 * this, so we fake it by using one of the LINK 264 * flags. If LINK0 is set, we program the PHY to 265 * be a master, otherwise it's a slave. 266 */ 267 if ((mii->mii_ifp->if_flags & IFF_LINK0)) { 268 PHY_WRITE(sc, MII_100T2CR, 269 GTCR_MAN_MS|GTCR_ADV_MS); 270 } else { 271 PHY_WRITE(sc, MII_100T2CR, GTCR_MAN_MS); 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, mii_anar(ife->ifm_media)); 287 PHY_WRITE(sc, MII_BMCR, ife->ifm_data); 288 break; 289 } | 210 mii_phy_setmedia(sc); |
290 break; 291 292 case MII_TICK: 293 /* 294 * If we're not currently selected, just return. 295 */ 296 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 297 return (0); 298 | 211 break; 212 213 case MII_TICK: 214 /* 215 * If we're not currently selected, just return. 216 */ 217 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 218 return (0); 219 |
299 /* 300 * Is the interface even up? 301 */ 302 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) | 220 if (mii_phy_tick(sc) == EJUSTRETURN) |
303 return (0); | 221 return (0); |
304 305 /* 306 * Only used for autonegotiation. 307 */ 308 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) 309 break; 310 311 /* 312 * Check to see if we have link. 313 */ 314 reg = PHY_READ(sc, NSGPHY_MII_PHYSUP); 315 if (reg & NSGPHY_PHYSUP_LNKSTS) 316 break; 317 318 /* 319 * Only retry autonegotiation every 5 seconds. 320 * Actually, for gigE PHYs, we should wait longer, since 321 * 5 seconds is the mimimum time the documentation 322 * says to wait for a 1000mbps link to be established. 323 */ 324 if (++sc->mii_ticks != 10) 325 return (0); 326 327 sc->mii_ticks = 0; 328 329 mii_phy_reset(sc); 330 if (nsgphy_mii_phy_auto(sc, 0) == EJUSTRETURN) 331 return(0); | |
332 break; 333 } 334 335 /* Update the media status. */ 336 nsgphy_status(sc); 337 338 /* Callback if something changed. */ 339 mii_phy_update(sc, cmd); 340 return (0); 341} 342 343static void 344nsgphy_status(struct mii_softc *sc) 345{ 346 struct mii_data *mii = sc->mii_pdata; | 222 break; 223 } 224 225 /* Update the media status. */ 226 nsgphy_status(sc); 227 228 /* Callback if something changed. */ 229 mii_phy_update(sc, cmd); 230 return (0); 231} 232 233static void 234nsgphy_status(struct mii_softc *sc) 235{ 236 struct mii_data *mii = sc->mii_pdata; |
347 int bmsr, bmcr, physup, anlpar, gstat; | 237 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 238 int bmsr, bmcr, physup, gtsr; |
348 349 mii->mii_media_status = IFM_AVALID; 350 mii->mii_media_active = IFM_ETHER; 351 | 239 240 mii->mii_media_status = IFM_AVALID; 241 mii->mii_media_active = IFM_ETHER; 242 |
352 bmsr = PHY_READ(sc, MII_BMSR); | 243 bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR); |
353 354 physup = PHY_READ(sc, NSGPHY_MII_PHYSUP); 355 | 244 245 physup = PHY_READ(sc, NSGPHY_MII_PHYSUP); 246 |
356 if (physup & NSGPHY_PHYSUP_LNKSTS) | 247 if (physup & PHY_SUP_LINK) |
357 mii->mii_media_status |= IFM_ACTIVE; 358 359 bmcr = PHY_READ(sc, MII_BMCR); | 248 mii->mii_media_status |= IFM_ACTIVE; 249 250 bmcr = PHY_READ(sc, MII_BMCR); |
251 if (bmcr & BMCR_ISO) { 252 mii->mii_media_active |= IFM_NONE; 253 mii->mii_media_status = 0; 254 return; 255 } |
|
360 361 if (bmcr & BMCR_LOOP) 362 mii->mii_media_active |= IFM_LOOP; 363 364 if (bmcr & BMCR_AUTOEN) { 365 /* 366 * The media status bits are only valid if autonegotiation 367 * has completed (or it's disabled). 368 */ 369 if ((bmsr & BMSR_ACOMP) == 0) { 370 /* Erg, still trying, I guess... */ 371 mii->mii_media_active |= IFM_NONE; 372 return; 373 } | 256 257 if (bmcr & BMCR_LOOP) 258 mii->mii_media_active |= IFM_LOOP; 259 260 if (bmcr & BMCR_AUTOEN) { 261 /* 262 * The media status bits are only valid if autonegotiation 263 * has completed (or it's disabled). 264 */ 265 if ((bmsr & BMSR_ACOMP) == 0) { 266 /* Erg, still trying, I guess... */ 267 mii->mii_media_active |= IFM_NONE; 268 return; 269 } |
374 anlpar = PHY_READ(sc, MII_ANLPAR); 375 gstat = PHY_READ(sc, MII_100T2SR); 376 if (gstat & GTSR_LP_1000TFDX) 377 mii->mii_media_active |= IFM_1000_T|IFM_FDX; 378 else if (gstat & GTSR_LP_1000THDX) 379 mii->mii_media_active |= IFM_1000_T|IFM_HDX; 380 else if (anlpar & ANLPAR_T4) 381 mii->mii_media_active |= IFM_100_T4; 382 else if (anlpar & ANLPAR_TX_FD) 383 mii->mii_media_active |= IFM_100_TX|IFM_FDX; 384 else if (anlpar & ANLPAR_TX) 385 mii->mii_media_active |= IFM_100_TX; 386 else if (anlpar & ANLPAR_10_FD) 387 mii->mii_media_active |= IFM_10_T|IFM_FDX; 388 else if (anlpar & ANLPAR_10) 389 mii->mii_media_active |= IFM_10_T|IFM_HDX; 390 else 391 mii->mii_media_active |= IFM_NONE; 392 return; 393 } | |
394 | 270 |
395 switch(bmcr & (BMCR_SPEED1|BMCR_SPEED0)) { 396 case BMCR_S1000: 397 mii->mii_media_active |= IFM_1000_T; 398 break; 399 case BMCR_S100: 400 mii->mii_media_active |= IFM_100_TX; 401 break; 402 case BMCR_S10: 403 mii->mii_media_active |= IFM_10_T; 404 break; 405 default: 406 break; 407 } | 271 switch (physup & (PHY_SUP_SPEED1|PHY_SUP_SPEED0)) { 272 case PHY_SUP_SPEED1: 273 mii->mii_media_active |= IFM_1000_T; 274 gtsr = PHY_READ(sc, MII_100T2SR); 275 if (gtsr & GTSR_MS_RES) 276 mii->mii_media_active |= IFM_ETH_MASTER; 277 break; |
408 | 278 |
409 if (bmcr & BMCR_FDX) 410 mii->mii_media_active |= IFM_FDX; 411 else 412 mii->mii_media_active |= IFM_HDX; | 279 case PHY_SUP_SPEED0: 280 mii->mii_media_active |= IFM_100_TX; 281 break; |
413 | 282 |
414 return; 415} | 283 case 0: 284 mii->mii_media_active |= IFM_10_T; 285 break; |
416 | 286 |
417 418static int 419nsgphy_mii_phy_auto(struct mii_softc *mii, int waitfor) 420{ 421 int bmsr, ktcr = 0, i; 422 423 if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) { 424 mii_phy_reset(mii); 425 PHY_WRITE(mii, MII_BMCR, 0); 426 DELAY(1000); 427 ktcr = PHY_READ(mii, MII_100T2CR); 428 PHY_WRITE(mii, MII_100T2CR, ktcr | 429 (GTCR_ADV_1000TFDX|GTCR_ADV_1000THDX)); 430 ktcr = PHY_READ(mii, MII_100T2CR); 431 DELAY(1000); 432 PHY_WRITE(mii, MII_ANAR, 433 BMSR_MEDIA_TO_ANAR(mii->mii_capabilities) | ANAR_CSMA); 434 DELAY(1000); 435 PHY_WRITE(mii, MII_BMCR, 436 BMCR_AUTOEN | BMCR_STARTNEG); 437 } 438 439 if (waitfor) { 440 /* Wait 500ms for it to complete. */ 441 for (i = 0; i < 500; i++) { 442 if ((bmsr = PHY_READ(mii, MII_BMSR)) & BMSR_ACOMP) 443 return (0); 444 DELAY(1000); 445#if 0 446 if ((bmsr & BMSR_ACOMP) == 0) 447 printf("%s: autonegotiation failed to complete\n", 448 mii->mii_dev.dv_xname); 449#endif | 287 default: 288 mii->mii_media_active |= IFM_NONE; 289 mii->mii_media_status = 0; |
450 } | 290 } |
451 452 /* 453 * Don't need to worry about clearing MIIF_DOINGAUTO. 454 * If that's set, a timeout is pending, and it will 455 * clear the flag. 456 */ 457 return (EIO); 458 } 459 460 /* 461 * Just let it finish asynchronously. This is for the benefit of 462 * the tick handler driving autonegotiation. Don't want 500ms 463 * delays all the time while the system is running! 464 */ 465 if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) { 466 mii->mii_flags |= MIIF_DOINGAUTO; 467 mii->mii_auto_ch = timeout(mii_phy_auto_timeout, mii, hz >> 1); 468 } 469 return (EJUSTRETURN); | 291 if (physup & PHY_SUP_DUPLEX) 292 mii->mii_media_active |= IFM_FDX; 293 } else 294 mii->mii_media_active = ife->ifm_media; |
470} | 295} |