1d0027533SBill Paul /* $NetBSD: nsphy.c,v 1.18 1999/07/14 23:57:36 thorpej Exp $ */ 2d0027533SBill Paul 3d0027533SBill Paul /*- 4d0027533SBill Paul * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc. 5d0027533SBill Paul * All rights reserved. 6d0027533SBill Paul * 7d0027533SBill Paul * This code is derived from software contributed to The NetBSD Foundation 8d0027533SBill Paul * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 9d0027533SBill Paul * NASA Ames Research Center. 10d0027533SBill Paul * 11d0027533SBill Paul * Redistribution and use in source and binary forms, with or without 12d0027533SBill Paul * modification, are permitted provided that the following conditions 13d0027533SBill Paul * are met: 14d0027533SBill Paul * 1. Redistributions of source code must retain the above copyright 15d0027533SBill Paul * notice, this list of conditions and the following disclaimer. 16d0027533SBill Paul * 2. Redistributions in binary form must reproduce the above copyright 17d0027533SBill Paul * notice, this list of conditions and the following disclaimer in the 18d0027533SBill Paul * documentation and/or other materials provided with the distribution. 19d0027533SBill Paul * 3. All advertising materials mentioning features or use of this software 20d0027533SBill Paul * must display the following acknowledgement: 21d0027533SBill Paul * This product includes software developed by the NetBSD 22d0027533SBill Paul * Foundation, Inc. and its contributors. 23d0027533SBill Paul * 4. Neither the name of The NetBSD Foundation nor the names of its 24d0027533SBill Paul * contributors may be used to endorse or promote products derived 25d0027533SBill Paul * from this software without specific prior written permission. 26d0027533SBill Paul * 27d0027533SBill Paul * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 28d0027533SBill Paul * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 29d0027533SBill Paul * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 30d0027533SBill Paul * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 31d0027533SBill Paul * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 32d0027533SBill Paul * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 33d0027533SBill Paul * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 34d0027533SBill Paul * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 35d0027533SBill Paul * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 36d0027533SBill Paul * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 37d0027533SBill Paul * POSSIBILITY OF SUCH DAMAGE. 38d0027533SBill Paul */ 39d0027533SBill Paul 40d0027533SBill Paul /* 41d0027533SBill Paul * Copyright (c) 1997 Manuel Bouyer. All rights reserved. 42d0027533SBill Paul * 43d0027533SBill Paul * Redistribution and use in source and binary forms, with or without 44d0027533SBill Paul * modification, are permitted provided that the following conditions 45d0027533SBill Paul * are met: 46d0027533SBill Paul * 1. Redistributions of source code must retain the above copyright 47d0027533SBill Paul * notice, this list of conditions and the following disclaimer. 48d0027533SBill Paul * 2. Redistributions in binary form must reproduce the above copyright 49d0027533SBill Paul * notice, this list of conditions and the following disclaimer in the 50d0027533SBill Paul * documentation and/or other materials provided with the distribution. 51d0027533SBill Paul * 3. All advertising materials mentioning features or use of this software 52d0027533SBill Paul * must display the following acknowledgement: 53d0027533SBill Paul * This product includes software developed by Manuel Bouyer. 54d0027533SBill Paul * 4. The name of the author may not be used to endorse or promote products 55d0027533SBill Paul * derived from this software without specific prior written permission. 56d0027533SBill Paul * 57d0027533SBill Paul * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 58d0027533SBill Paul * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 59d0027533SBill Paul * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 60d0027533SBill Paul * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 61d0027533SBill Paul * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 62d0027533SBill Paul * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 63d0027533SBill Paul * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 64d0027533SBill Paul * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 65d0027533SBill Paul * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 66d0027533SBill Paul * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 67d0027533SBill Paul */ 68d0027533SBill Paul 69d0027533SBill Paul /* 70d0027533SBill Paul * driver for National Semiconductor's DP83840A ethernet 10/100 PHY 71d0027533SBill Paul * Data Sheet available from www.national.com 72d0027533SBill Paul */ 73d0027533SBill Paul 74d0027533SBill Paul #include <sys/param.h> 75d0027533SBill Paul #include <sys/systm.h> 76d0027533SBill Paul #include <sys/kernel.h> 77d0027533SBill Paul #include <sys/socket.h> 78d0027533SBill Paul #include <sys/errno.h> 79d0027533SBill Paul #include <sys/module.h> 80d0027533SBill Paul #include <sys/bus.h> 81d0027533SBill Paul 82d0027533SBill Paul #include <net/if.h> 83d0027533SBill Paul #include <net/if_media.h> 84d0027533SBill Paul 85d0027533SBill Paul #include <dev/mii/mii.h> 86d0027533SBill Paul #include <dev/mii/miivar.h> 87d0027533SBill Paul #include <dev/mii/miidevs.h> 88d0027533SBill Paul 89d0027533SBill Paul #include <dev/mii/nsphyreg.h> 90d0027533SBill Paul 91d0027533SBill Paul #include "miibus_if.h" 92d0027533SBill Paul 93d0027533SBill Paul #if !defined(lint) 94d0027533SBill Paul static const char rcsid[] = 95c3aac50fSPeter Wemm "$FreeBSD$"; 96d0027533SBill Paul #endif 97d0027533SBill Paul 98e51a25f8SAlfred Perlstein static int nsphy_probe(device_t); 99e51a25f8SAlfred Perlstein static int nsphy_attach(device_t); 100d0027533SBill Paul 101d0027533SBill Paul static device_method_t nsphy_methods[] = { 102d0027533SBill Paul /* device interface */ 103d0027533SBill Paul DEVMETHOD(device_probe, nsphy_probe), 104d0027533SBill Paul DEVMETHOD(device_attach, nsphy_attach), 105279fe8d1SPoul-Henning Kamp DEVMETHOD(device_detach, mii_phy_detach), 106d0027533SBill Paul DEVMETHOD(device_shutdown, bus_generic_shutdown), 107d0027533SBill Paul { 0, 0 } 108d0027533SBill Paul }; 109d0027533SBill Paul 110d0027533SBill Paul static devclass_t nsphy_devclass; 111d0027533SBill Paul 112d0027533SBill Paul static driver_t nsphy_driver = { 113d0027533SBill Paul "nsphy", 114d0027533SBill Paul nsphy_methods, 115d0027533SBill Paul sizeof(struct mii_softc) 116d0027533SBill Paul }; 117d0027533SBill Paul 118d0027533SBill Paul DRIVER_MODULE(nsphy, miibus, nsphy_driver, nsphy_devclass, 0, 0); 119d0027533SBill Paul 120e51a25f8SAlfred Perlstein static int nsphy_service(struct mii_softc *, struct mii_data *, int); 121e51a25f8SAlfred Perlstein static void nsphy_status(struct mii_softc *); 122d0027533SBill Paul 1239c1c2e99SAlfred Perlstein static int 1249c1c2e99SAlfred Perlstein nsphy_probe(dev) 125d0027533SBill Paul device_t dev; 126d0027533SBill Paul { 127d0027533SBill Paul struct mii_attach_args *ma; 128d0027533SBill Paul 129d0027533SBill Paul ma = device_get_ivars(dev); 130d0027533SBill Paul 131b7240781SSemen Ustimenko if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_NATSEMI && 132b7240781SSemen Ustimenko MII_MODEL(ma->mii_id2) == MII_MODEL_NATSEMI_DP83840) { 133d0027533SBill Paul device_set_desc(dev, MII_STR_NATSEMI_DP83840); 134b7240781SSemen Ustimenko } else 135b7240781SSemen Ustimenko return (ENXIO); 136d0027533SBill Paul 137d0027533SBill Paul return (0); 138d0027533SBill Paul } 139d0027533SBill Paul 1409c1c2e99SAlfred Perlstein static int 1419c1c2e99SAlfred Perlstein nsphy_attach(dev) 142d0027533SBill Paul device_t dev; 143d0027533SBill Paul { 144d0027533SBill Paul struct mii_softc *sc; 145d0027533SBill Paul struct mii_attach_args *ma; 146d0027533SBill Paul struct mii_data *mii; 147d0027533SBill Paul 148d0027533SBill Paul sc = device_get_softc(dev); 149d0027533SBill Paul ma = device_get_ivars(dev); 150d0027533SBill Paul sc->mii_dev = device_get_parent(dev); 151d0027533SBill Paul mii = device_get_softc(sc->mii_dev); 152d0027533SBill Paul LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list); 153d0027533SBill Paul 154d0027533SBill Paul sc->mii_inst = mii->mii_instance; 155d0027533SBill Paul sc->mii_phy = ma->mii_phyno; 156d0027533SBill Paul sc->mii_service = nsphy_service; 157d0027533SBill Paul sc->mii_pdata = mii; 158d0027533SBill Paul 159d0027533SBill Paul #ifdef foo 160d0027533SBill Paul /* 161d0027533SBill Paul * i82557 wedges if all of its PHYs are isolated! 162d0027533SBill Paul */ 163d0027533SBill Paul if (strcmp(device_get_name(device_get_parent(sc->mii_dev)), 164d0027533SBill Paul "fxp") == 0 && mii->mii_instance == 0) 165d0027533SBill Paul #endif 166d0027533SBill Paul 167d0027533SBill Paul sc->mii_flags |= MIIF_NOISOLATE; 168d0027533SBill Paul mii->mii_instance++; 169d0027533SBill Paul 170d0027533SBill Paul #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL) 171d0027533SBill Paul 172d0027533SBill Paul #if 0 173d0027533SBill Paul /* Can't do this on the i82557! */ 174d0027533SBill Paul ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst), 175d0027533SBill Paul BMCR_ISO); 176d0027533SBill Paul #endif 177d0027533SBill Paul ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->mii_inst), 178d0027533SBill Paul BMCR_LOOP|BMCR_S100); 179d0027533SBill Paul 180d0027533SBill Paul mii_phy_reset(sc); 181d0027533SBill Paul 182d0027533SBill Paul sc->mii_capabilities = 183d0027533SBill Paul PHY_READ(sc, MII_BMSR) & ma->mii_capmask; 184d0027533SBill Paul device_printf(dev, " "); 18507dd9383SPoul-Henning Kamp mii_add_media(sc); 186d0027533SBill Paul printf("\n"); 187d0027533SBill Paul #undef ADD 188d0027533SBill Paul 189d0027533SBill Paul MIIBUS_MEDIAINIT(sc->mii_dev); 190d0027533SBill Paul return(0); 191d0027533SBill Paul } 192d0027533SBill Paul 193d9730b8bSJonathan Lemon static int 194d0027533SBill Paul nsphy_service(sc, mii, cmd) 195d0027533SBill Paul struct mii_softc *sc; 196d0027533SBill Paul struct mii_data *mii; 197d0027533SBill Paul int cmd; 198d0027533SBill Paul { 199d0027533SBill Paul struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 200d0027533SBill Paul int reg; 201d0027533SBill Paul 202d0027533SBill Paul switch (cmd) { 203d0027533SBill Paul case MII_POLLSTAT: 204d0027533SBill Paul /* 205d0027533SBill Paul * If we're not polling our PHY instance, just return. 206d0027533SBill Paul */ 207d0027533SBill Paul if (IFM_INST(ife->ifm_media) != sc->mii_inst) 208d0027533SBill Paul return (0); 209d0027533SBill Paul break; 210d0027533SBill Paul 211d0027533SBill Paul case MII_MEDIACHG: 212d0027533SBill Paul /* 213d0027533SBill Paul * If the media indicates a different PHY instance, 214d0027533SBill Paul * isolate ourselves. 215d0027533SBill Paul */ 216d0027533SBill Paul if (IFM_INST(ife->ifm_media) != sc->mii_inst) { 217d0027533SBill Paul reg = PHY_READ(sc, MII_BMCR); 218d0027533SBill Paul PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO); 219d0027533SBill Paul return (0); 220d0027533SBill Paul } 221d0027533SBill Paul 222d0027533SBill Paul /* 223d0027533SBill Paul * If the interface is not up, don't do anything. 224d0027533SBill Paul */ 225d0027533SBill Paul if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 226d0027533SBill Paul break; 227d0027533SBill Paul 228d0027533SBill Paul reg = PHY_READ(sc, MII_NSPHY_PCR); 229d0027533SBill Paul 230d0027533SBill Paul /* 231d0027533SBill Paul * Set up the PCR to use LED4 to indicate full-duplex 232d0027533SBill Paul * in both 10baseT and 100baseTX modes. 233d0027533SBill Paul */ 234d0027533SBill Paul reg |= PCR_LED4MODE; 235d0027533SBill Paul 236d0027533SBill Paul /* 237d0027533SBill Paul * Make sure Carrier Intgrity Monitor function is 238d0027533SBill Paul * disabled (normal for Node operation, but sometimes 239d0027533SBill Paul * it's not set?!) 240d0027533SBill Paul */ 241d0027533SBill Paul reg |= PCR_CIMDIS; 242d0027533SBill Paul 243d0027533SBill Paul /* 244d0027533SBill Paul * Make sure "force link good" is set to normal mode. 245d0027533SBill Paul * It's only intended for debugging. 246d0027533SBill Paul */ 247d0027533SBill Paul reg |= PCR_FLINK100; 248d0027533SBill Paul 249d0027533SBill Paul /* 250d0027533SBill Paul * Mystery bits which are supposedly `reserved', 251d0027533SBill Paul * but we seem to need to set them when the PHY 25232de04d5SJonathan Lemon * is connected to some interfaces: 25332de04d5SJonathan Lemon * 25432de04d5SJonathan Lemon * 0x0400 is needed for fxp 25532de04d5SJonathan Lemon * (Intel EtherExpress Pro 10+/100B, 82557 chip) 25632de04d5SJonathan Lemon * (nsphy with a DP83840 chip) 25732de04d5SJonathan Lemon * 0x0100 may be needed for some other card 258d0027533SBill Paul */ 259d0027533SBill Paul reg |= 0x0100 | 0x0400; 26032de04d5SJonathan Lemon 261e46e7fa2SJonathan Lemon if (strcmp(device_get_name(device_get_parent(sc->mii_dev)), 262e46e7fa2SJonathan Lemon "fxp") == 0) 263d0027533SBill Paul PHY_WRITE(sc, MII_NSPHY_PCR, reg); 26432de04d5SJonathan Lemon 265d0027533SBill Paul switch (IFM_SUBTYPE(ife->ifm_media)) { 266d0027533SBill Paul case IFM_AUTO: 267d0027533SBill Paul /* 268d0027533SBill Paul * If we're already in auto mode, just return. 269d0027533SBill Paul */ 270d0027533SBill Paul if (PHY_READ(sc, MII_BMCR) & BMCR_AUTOEN) 271d0027533SBill Paul return (0); 272fd94424cSPoul-Henning Kamp (void) mii_phy_auto(sc); 273d0027533SBill Paul break; 274d0027533SBill Paul case IFM_100_T4: 275d0027533SBill Paul /* 276d0027533SBill Paul * XXX Not supported as a manual setting right now. 277d0027533SBill Paul */ 278d0027533SBill Paul return (EINVAL); 279d0027533SBill Paul default: 280d0027533SBill Paul /* 281d0027533SBill Paul * BMCR data is stored in the ifmedia entry. 282d0027533SBill Paul */ 283d0027533SBill Paul PHY_WRITE(sc, MII_ANAR, 284d0027533SBill Paul mii_anar(ife->ifm_media)); 285d0027533SBill Paul PHY_WRITE(sc, MII_BMCR, ife->ifm_data); 286d0027533SBill Paul } 287d0027533SBill Paul break; 288d0027533SBill Paul 289d0027533SBill Paul case MII_TICK: 290d0027533SBill Paul /* 291d0027533SBill Paul * If we're not currently selected, just return. 292d0027533SBill Paul */ 293d0027533SBill Paul if (IFM_INST(ife->ifm_media) != sc->mii_inst) 294d0027533SBill Paul return (0); 295d9730b8bSJonathan Lemon if (mii_phy_tick(sc) == EJUSTRETURN) 296d0027533SBill Paul return (0); 297d0027533SBill Paul break; 298d0027533SBill Paul } 299d0027533SBill Paul 300d0027533SBill Paul /* Update the media status. */ 301d0027533SBill Paul nsphy_status(sc); 302d0027533SBill Paul 303d0027533SBill Paul /* Callback if something changed. */ 304d9730b8bSJonathan Lemon mii_phy_update(sc, cmd); 305d0027533SBill Paul return (0); 306d0027533SBill Paul } 307d0027533SBill Paul 308d9730b8bSJonathan Lemon static void 309d0027533SBill Paul nsphy_status(sc) 310d0027533SBill Paul struct mii_softc *sc; 311d0027533SBill Paul { 312d0027533SBill Paul struct mii_data *mii = sc->mii_pdata; 313d0027533SBill Paul int bmsr, bmcr, par, anlpar; 314d0027533SBill Paul 315d0027533SBill Paul mii->mii_media_status = IFM_AVALID; 316d0027533SBill Paul mii->mii_media_active = IFM_ETHER; 317d0027533SBill Paul 318d0027533SBill Paul bmsr = PHY_READ(sc, MII_BMSR) | 319d0027533SBill Paul PHY_READ(sc, MII_BMSR); 320d0027533SBill Paul if (bmsr & BMSR_LINK) 321d0027533SBill Paul mii->mii_media_status |= IFM_ACTIVE; 322d0027533SBill Paul 323d0027533SBill Paul bmcr = PHY_READ(sc, MII_BMCR); 324d0027533SBill Paul if (bmcr & BMCR_ISO) { 325d0027533SBill Paul mii->mii_media_active |= IFM_NONE; 326d0027533SBill Paul mii->mii_media_status = 0; 327d0027533SBill Paul return; 328d0027533SBill Paul } 329d0027533SBill Paul 330d0027533SBill Paul if (bmcr & BMCR_LOOP) 331d0027533SBill Paul mii->mii_media_active |= IFM_LOOP; 332d0027533SBill Paul 333d0027533SBill Paul if (bmcr & BMCR_AUTOEN) { 334d0027533SBill Paul /* 335d0027533SBill Paul * The PAR status bits are only valid of autonegotiation 336d0027533SBill Paul * has completed (or it's disabled). 337d0027533SBill Paul */ 338d0027533SBill Paul if ((bmsr & BMSR_ACOMP) == 0) { 339d0027533SBill Paul /* Erg, still trying, I guess... */ 340d0027533SBill Paul mii->mii_media_active |= IFM_NONE; 341d0027533SBill Paul return; 342d0027533SBill Paul } 343d0027533SBill Paul 344d0027533SBill Paul /* 345d0027533SBill Paul * Argh. The PAR doesn't seem to indicate duplex mode 346d0027533SBill Paul * properly! Determine media based on link partner's 347d0027533SBill Paul * advertised capabilities. 348d0027533SBill Paul */ 349d0027533SBill Paul if (PHY_READ(sc, MII_ANER) & ANER_LPAN) { 350d0027533SBill Paul anlpar = PHY_READ(sc, MII_ANAR) & 351d0027533SBill Paul PHY_READ(sc, MII_ANLPAR); 352d0027533SBill Paul if (anlpar & ANLPAR_T4) 353d0027533SBill Paul mii->mii_media_active |= IFM_100_T4; 354d0027533SBill Paul else if (anlpar & ANLPAR_TX_FD) 355d0027533SBill Paul mii->mii_media_active |= IFM_100_TX|IFM_FDX; 356d0027533SBill Paul else if (anlpar & ANLPAR_TX) 357d0027533SBill Paul mii->mii_media_active |= IFM_100_TX; 358d0027533SBill Paul else if (anlpar & ANLPAR_10_FD) 359d0027533SBill Paul mii->mii_media_active |= IFM_10_T|IFM_FDX; 360d0027533SBill Paul else if (anlpar & ANLPAR_10) 361d0027533SBill Paul mii->mii_media_active |= IFM_10_T; 362d0027533SBill Paul else 363d0027533SBill Paul mii->mii_media_active |= IFM_NONE; 364d0027533SBill Paul return; 365d0027533SBill Paul } 366d0027533SBill Paul 367d0027533SBill Paul /* 368d0027533SBill Paul * Link partner is not capable of autonegotiation. 369d0027533SBill Paul * We will never be in full-duplex mode if this is 370d0027533SBill Paul * the case, so reading the PAR is OK. 371d0027533SBill Paul */ 372d0027533SBill Paul par = PHY_READ(sc, MII_NSPHY_PAR); 373d0027533SBill Paul if (par & PAR_10) 374d0027533SBill Paul mii->mii_media_active |= IFM_10_T; 375d0027533SBill Paul else 376d0027533SBill Paul mii->mii_media_active |= IFM_100_TX; 377d0027533SBill Paul #if 0 378d0027533SBill Paul if (par & PAR_FDX) 379d0027533SBill Paul mii->mii_media_active |= IFM_FDX; 380d0027533SBill Paul #endif 381d0027533SBill Paul } else 382b7240781SSemen Ustimenko mii->mii_media_active |= mii_media_from_bmcr(bmcr); 383d0027533SBill Paul } 384