nsphy.c revision 1.4
1/* $OpenBSD: nsphy.c,v 1.4 1999/01/04 04:44:05 jason Exp $ */ 2/* $NetBSD: nsphy.c,v 1.16 1998/11/05 04:08:02 thorpej Exp $ */ 3 4/*- 5 * Copyright (c) 1998 The NetBSD Foundation, Inc. 6 * All rights reserved. 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 * 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. 20 * 3. All advertising materials mentioning features or use of this software 21 * must display the following acknowledgement: 22 * This product includes software developed by the NetBSD 23 * Foundation, Inc. and its contributors. 24 * 4. Neither the name of The NetBSD Foundation nor the names of its 25 * contributors may be used to endorse or promote products derived 26 * from this software without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 29 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 31 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 32 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 38 * POSSIBILITY OF SUCH DAMAGE. 39 */ 40 41/* 42 * Copyright (c) 1997 Manuel Bouyer. All rights reserved. 43 * 44 * Redistribution and use in source and binary forms, with or without 45 * modification, are permitted provided that the following conditions 46 * are met: 47 * 1. Redistributions of source code must retain the above copyright 48 * notice, this list of conditions and the following disclaimer. 49 * 2. Redistributions in binary form must reproduce the above copyright 50 * notice, this list of conditions and the following disclaimer in the 51 * documentation and/or other materials provided with the distribution. 52 * 3. All advertising materials mentioning features or use of this software 53 * must display the following acknowledgement: 54 * This product includes software developed by Manuel Bouyer. 55 * 4. The name of the author may not be used to endorse or promote products 56 * derived from this software without specific prior written permission. 57 * 58 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 59 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 60 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 61 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 62 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 63 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 64 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 65 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 66 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 67 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 68 */ 69 70/* 71 * driver for National Semiconductor's DP83840A ethernet 10/100 PHY 72 * Data Sheet available from www.national.com 73 */ 74 75#include <sys/param.h> 76#include <sys/systm.h> 77#include <sys/kernel.h> 78#include <sys/device.h> 79#include <sys/malloc.h> 80#include <sys/socket.h> 81 82#include <net/if.h> 83#include <net/if_media.h> 84 85#include <dev/mii/mii.h> 86#include <dev/mii/miivar.h> 87#include <dev/mii/miidevs.h> 88 89#include <dev/mii/nsphyreg.h> 90 91#ifdef __NetBSD__ 92int nsphymatch __P((struct device *, struct cfdata *, void *)); 93#else 94int nsphymatch __P((struct device *, void *, void *)); 95#endif 96void nsphyattach __P((struct device *, struct device *, void *)); 97 98struct cfattach nsphy_ca = { 99 sizeof(struct mii_softc), nsphymatch, nsphyattach 100}; 101 102#ifdef __OpenBSD__ 103struct cfdriver nsphy_cd = { 104 NULL, "nsphy", DV_DULL 105}; 106#endif 107 108int nsphy_service __P((struct mii_softc *, struct mii_data *, int)); 109void nsphy_status __P((struct mii_softc *)); 110void nsphy_reset __P((struct mii_softc *)); 111 112int 113nsphymatch(parent, match, aux) 114 struct device *parent; 115#ifdef __NetBSD__ 116 struct cfdata *match; 117#else 118 void *match; 119#endif 120 void *aux; 121{ 122 struct mii_attach_args *ma = aux; 123 124 if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_NATSEMI && 125 MII_MODEL(ma->mii_id2) == MII_MODEL_NATSEMI_DP83840) 126 return (10); 127 128 return (0); 129} 130 131void 132nsphyattach(parent, self, aux) 133 struct device *parent; 134 struct device *self; 135 void *aux; 136{ 137 struct mii_softc *sc = (struct mii_softc *)self; 138 struct mii_attach_args *ma = aux; 139 struct mii_data *mii = ma->mii_data; 140 141 printf(": %s, rev. %d\n", MII_STR_NATSEMI_DP83840, 142 MII_REV(ma->mii_id2)); 143 144 sc->mii_inst = mii->mii_instance; 145 sc->mii_phy = ma->mii_phyno; 146 sc->mii_service = nsphy_service; 147 sc->mii_pdata = mii; 148 149 /* 150 * i82557 wedges if all of its PHYs are isolated! 151 */ 152 if (strcmp(parent->dv_cfdata->cf_driver->cd_name, "fxp") == 0 && 153 mii->mii_instance == 0) 154 sc->mii_flags |= MIIF_NOISOLATE; 155 156#define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL) 157 158#if 0 159 /* Can't do this on the i82557! */ 160 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst), 161 BMCR_ISO); 162#endif 163 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->mii_inst), 164 BMCR_LOOP|BMCR_S100); 165 166 nsphy_reset(sc); 167 168 sc->mii_capabilities = 169 PHY_READ(sc, MII_BMSR) & ma->mii_capmask; 170 printf("%s: ", sc->mii_dev.dv_xname); 171 if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0) 172 printf("no media present"); 173 else 174 mii_add_media(mii, sc->mii_capabilities, 175 sc->mii_inst); 176 printf("\n"); 177#undef ADD 178} 179 180int 181nsphy_service(sc, mii, cmd) 182 struct mii_softc *sc; 183 struct mii_data *mii; 184 int cmd; 185{ 186 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 187 int reg; 188 189 switch (cmd) { 190 case MII_POLLSTAT: 191 /* 192 * If we're not polling our PHY instance, just return. 193 */ 194 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 195 return (0); 196 break; 197 198 case MII_MEDIACHG: 199 /* 200 * If the media indicates a different PHY instance, 201 * isolate ourselves. 202 */ 203 if (IFM_INST(ife->ifm_media) != sc->mii_inst) { 204 reg = PHY_READ(sc, MII_BMCR); 205 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO); 206 return (0); 207 } 208 209 /* 210 * If the interface is not up, don't do anything. 211 */ 212 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 213 break; 214 215 reg = PHY_READ(sc, MII_NSPHY_PCR); 216 217 /* 218 * Set up the PCR to use LED4 to indicate full-duplex 219 * in both 10baseT and 100baseTX modes. 220 */ 221 reg |= PCR_LED4MODE; 222 223 /* 224 * Make sure Carrier Intgrity Monitor function is 225 * disabled (normal for Node operation, but sometimes 226 * it's not set?!) 227 */ 228 reg |= PCR_CIMDIS; 229 230 /* 231 * Make sure "force link good" is not set. It's only 232 * intended for debugging, but sometimes it's set 233 * after a reset. 234 */ 235 reg &= ~PCR_FLINK100; 236 237#if 0 238 /* 239 * Mystery bits which are supposedly `reserved', 240 * but we seem to need to set them when the PHY 241 * is connected to some interfaces! 242 */ 243 reg |= 0x0100 | 0x0400; 244#endif 245 246 PHY_WRITE(sc, MII_NSPHY_PCR, reg); 247 248 switch (IFM_SUBTYPE(ife->ifm_media)) { 249 case IFM_AUTO: 250 nsphy_reset(sc); 251 /* 252 * If we're already in auto mode, just return. 253 */ 254 if (PHY_READ(sc, MII_BMCR) & BMCR_AUTOEN) 255 return (0); 256 (void) mii_phy_auto(sc); 257 break; 258 case IFM_100_T4: 259 /* 260 * XXX Not supported as a manual setting right now. 261 */ 262 return (EINVAL); 263 case IFM_100_TX: 264 PHY_WRITE(sc, MII_ANAR, 265 mii_anar(ife->ifm_media)); 266 267 reg = 0; 268 reg |= BMCR_S100; 269 if ((sc->mii_flags & MIIF_NOISOLATE) == 0) 270 reg |= BMCR_ISO; 271 272 if ((ife->ifm_media & IFM_GMASK) == IFM_FDX) 273 reg |= BMCR_FDX; 274 PHY_WRITE(sc, MII_BMCR, reg); 275 delay(1000000); /* XXX too long, will adjust */ 276 277 reg &= ~BMCR_ISO; 278 PHY_WRITE(sc, MII_BMCR, reg); 279 break; 280 281 default: 282 /* 283 * BMCR data is stored in the ifmedia entry. 284 */ 285 PHY_WRITE(sc, MII_ANAR, 286 mii_anar(ife->ifm_media)); 287 PHY_WRITE(sc, MII_BMCR, ife->ifm_data); 288 } 289 break; 290 291 case MII_TICK: 292 /* 293 * If we're not currently selected, just return. 294 */ 295 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 296 return (0); 297 298 /* 299 * Only used for autonegotiation. 300 */ 301 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) 302 return (0); 303 304 /* 305 * Is the interface even up? 306 */ 307 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 308 return (0); 309 310 /* 311 * Check to see if we have link. If we do, we don't 312 * need to restart the autonegotiation process. Read 313 * the BMSR twice in case it's latched. 314 */ 315 reg = PHY_READ(sc, MII_BMSR) | 316 PHY_READ(sc, MII_BMSR); 317 if (reg & BMSR_LINK) 318 return (0); 319 320 /* 321 * Only retry autonegotiation every 5 seconds. 322 */ 323 if (++sc->mii_ticks != 5) 324 return (0); 325 326 sc->mii_ticks = 0; 327 nsphy_reset(sc); 328 (void) mii_phy_auto(sc); 329 break; 330 } 331 332 /* Update the media status. */ 333 nsphy_status(sc); 334 335 /* Callback if something changed. */ 336 if (sc->mii_active != mii->mii_media_active || cmd == MII_MEDIACHG) { 337 (*mii->mii_statchg)(sc->mii_dev.dv_parent); 338 sc->mii_active = mii->mii_media_active; 339 } 340 return (0); 341} 342 343void 344nsphy_status(sc) 345 struct mii_softc *sc; 346{ 347 struct mii_data *mii = sc->mii_pdata; 348 int bmsr, bmcr, par, anlpar; 349 350 mii->mii_media_status = IFM_AVALID; 351 mii->mii_media_active = IFM_ETHER; 352 353 bmsr = PHY_READ(sc, MII_BMSR) | 354 PHY_READ(sc, MII_BMSR); 355 if (bmsr & BMSR_LINK) 356 mii->mii_media_status |= IFM_ACTIVE; 357 358 bmcr = PHY_READ(sc, MII_BMCR); 359 if (bmcr & BMCR_ISO) { 360 mii->mii_media_active |= IFM_NONE; 361 mii->mii_media_status = 0; 362 return; 363 } 364 365 if (bmcr & BMCR_LOOP) 366 mii->mii_media_active |= IFM_LOOP; 367 368 if (bmcr & BMCR_AUTOEN) { 369 /* 370 * The PAR status bits are only valid of autonegotiation 371 * has completed (or it's disabled). 372 */ 373 if ((bmsr & BMSR_ACOMP) == 0) { 374 /* Erg, still trying, I guess... */ 375 mii->mii_media_active |= IFM_NONE; 376 return; 377 } 378 379 /* 380 * Argh. The PAR doesn't seem to indicate duplex mode 381 * properly! Determine media based on link partner's 382 * advertised capabilities. 383 */ 384 if (PHY_READ(sc, MII_ANER) & ANER_LPAN) { 385 anlpar = PHY_READ(sc, MII_ANAR) & 386 PHY_READ(sc, MII_ANLPAR); 387 if (anlpar & ANLPAR_T4) 388 mii->mii_media_active |= IFM_100_T4; 389 else if (anlpar & ANLPAR_TX_FD) 390 mii->mii_media_active |= IFM_100_TX|IFM_FDX; 391 else if (anlpar & ANLPAR_TX) 392 mii->mii_media_active |= IFM_100_TX; 393 else if (anlpar & ANLPAR_10_FD) 394 mii->mii_media_active |= IFM_10_T|IFM_FDX; 395 else if (anlpar & ANLPAR_10) 396 mii->mii_media_active |= IFM_10_T; 397 else 398 mii->mii_media_active |= IFM_NONE; 399 return; 400 } 401 402 /* 403 * Link partner is not capable of autonegotiation. 404 * We will never be in full-duplex mode if this is 405 * the case, so reading the PAR is OK. 406 */ 407 par = PHY_READ(sc, MII_NSPHY_PAR); 408 if (par & PAR_10) 409 mii->mii_media_active |= IFM_10_T; 410 else 411 mii->mii_media_active |= IFM_100_TX; 412#if 0 413 if (par & PAR_FDX) 414 mii->mii_media_active |= IFM_FDX; 415#endif 416 } else 417 mii->mii_media_active = mii_media_from_bmcr(bmcr); 418} 419 420void 421nsphy_reset(sc) 422 struct mii_softc *sc; 423{ 424 int anar; 425 426 mii_phy_reset(sc); 427 anar = PHY_READ(sc, MII_ANAR); 428 anar |= BMSR_MEDIA_TO_ANAR(PHY_READ(sc, MII_BMSR)); 429 PHY_WRITE(sc, MII_ANAR, anar); 430} 431