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 * 20d0027533SBill Paul * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21d0027533SBill Paul * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22d0027533SBill Paul * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23d0027533SBill Paul * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24d0027533SBill Paul * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25d0027533SBill Paul * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26d0027533SBill Paul * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27d0027533SBill Paul * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28d0027533SBill Paul * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29d0027533SBill Paul * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30d0027533SBill Paul * POSSIBILITY OF SUCH DAMAGE. 31d0027533SBill Paul */ 32d0027533SBill Paul 33098ca2bdSWarner Losh /*- 34d0027533SBill Paul * Copyright (c) 1997 Manuel Bouyer. All rights reserved. 35d0027533SBill Paul * 36d0027533SBill Paul * Redistribution and use in source and binary forms, with or without 37d0027533SBill Paul * modification, are permitted provided that the following conditions 38d0027533SBill Paul * are met: 39d0027533SBill Paul * 1. Redistributions of source code must retain the above copyright 40d0027533SBill Paul * notice, this list of conditions and the following disclaimer. 41d0027533SBill Paul * 2. Redistributions in binary form must reproduce the above copyright 42d0027533SBill Paul * notice, this list of conditions and the following disclaimer in the 43d0027533SBill Paul * documentation and/or other materials provided with the distribution. 44d0027533SBill Paul * 45d0027533SBill Paul * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 46d0027533SBill Paul * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 47d0027533SBill Paul * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 48d0027533SBill Paul * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 49d0027533SBill Paul * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 50d0027533SBill Paul * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 51d0027533SBill Paul * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 52d0027533SBill Paul * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 53d0027533SBill Paul * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 54d0027533SBill Paul * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 55d0027533SBill Paul */ 56d0027533SBill Paul 5750aa1061SMarius Strobl #include <sys/cdefs.h> 5850aa1061SMarius Strobl __FBSDID("$FreeBSD$"); 5950aa1061SMarius Strobl 60d0027533SBill Paul /* 61d0027533SBill Paul * driver for National Semiconductor's DP83840A ethernet 10/100 PHY 62d0027533SBill Paul * Data Sheet available from www.national.com 63d0027533SBill Paul */ 64d0027533SBill Paul 65d0027533SBill Paul #include <sys/param.h> 66d0027533SBill Paul #include <sys/systm.h> 67d0027533SBill Paul #include <sys/kernel.h> 68d0027533SBill Paul #include <sys/socket.h> 69d0027533SBill Paul #include <sys/errno.h> 70d0027533SBill Paul #include <sys/module.h> 71d0027533SBill Paul #include <sys/bus.h> 72d0027533SBill Paul 73d0027533SBill Paul #include <net/if.h> 74*61a3ac6eSGleb Smirnoff #include <net/if_var.h> 75d0027533SBill Paul #include <net/if_media.h> 76d0027533SBill Paul 77d0027533SBill Paul #include <dev/mii/mii.h> 78d0027533SBill Paul #include <dev/mii/miivar.h> 792d3ce713SDavid E. O'Brien #include "miidevs.h" 80d0027533SBill Paul 81d0027533SBill Paul #include <dev/mii/nsphyreg.h> 82d0027533SBill Paul 83d0027533SBill Paul #include "miibus_if.h" 84d0027533SBill Paul 85e51a25f8SAlfred Perlstein static int nsphy_probe(device_t); 86e51a25f8SAlfred Perlstein static int nsphy_attach(device_t); 87d0027533SBill Paul 88d0027533SBill Paul static device_method_t nsphy_methods[] = { 89d0027533SBill Paul /* device interface */ 90d0027533SBill Paul DEVMETHOD(device_probe, nsphy_probe), 91d0027533SBill Paul DEVMETHOD(device_attach, nsphy_attach), 92279fe8d1SPoul-Henning Kamp DEVMETHOD(device_detach, mii_phy_detach), 93d0027533SBill Paul DEVMETHOD(device_shutdown, bus_generic_shutdown), 94604f5f1fSMarius Strobl DEVMETHOD_END 95d0027533SBill Paul }; 96d0027533SBill Paul 97d0027533SBill Paul static devclass_t nsphy_devclass; 98d0027533SBill Paul 99d0027533SBill Paul static driver_t nsphy_driver = { 100d0027533SBill Paul "nsphy", 101d0027533SBill Paul nsphy_methods, 102d0027533SBill Paul sizeof(struct mii_softc) 103d0027533SBill Paul }; 104d0027533SBill Paul 105d0027533SBill Paul DRIVER_MODULE(nsphy, miibus, nsphy_driver, nsphy_devclass, 0, 0); 106d0027533SBill Paul 107e51a25f8SAlfred Perlstein static int nsphy_service(struct mii_softc *, struct mii_data *, int); 108e51a25f8SAlfred Perlstein static void nsphy_status(struct mii_softc *); 109fb58dc87SMarius Strobl static void nsphy_reset(struct mii_softc *); 110d0027533SBill Paul 111a35b9333SMarius Strobl static const struct mii_phydesc nsphys[] = { 1123fcb7a53SMarius Strobl MII_PHY_DESC(xxNATSEMI, DP83840), 113a35b9333SMarius Strobl MII_PHY_END 114a35b9333SMarius Strobl }; 115a35b9333SMarius Strobl 1163fcb7a53SMarius Strobl static const struct mii_phy_funcs nsphy_funcs = { 1173fcb7a53SMarius Strobl nsphy_service, 1183fcb7a53SMarius Strobl nsphy_status, 1193fcb7a53SMarius Strobl nsphy_reset 1203fcb7a53SMarius Strobl }; 1213fcb7a53SMarius Strobl 1229c1c2e99SAlfred Perlstein static int 1237d830ac9SWarner Losh nsphy_probe(device_t dev) 124d0027533SBill Paul { 125d0027533SBill Paul 126a35b9333SMarius Strobl return (mii_phy_dev_probe(dev, nsphys, BUS_PROBE_DEFAULT)); 127d0027533SBill Paul } 128d0027533SBill Paul 1299c1c2e99SAlfred Perlstein static int 1307d830ac9SWarner Losh nsphy_attach(device_t dev) 131d0027533SBill Paul { 132fb58dc87SMarius Strobl const char *nic; 1333fcb7a53SMarius Strobl u_int flags; 134d0027533SBill Paul 1353fcb7a53SMarius Strobl nic = device_get_name(device_get_parent(device_get_parent(dev))); 1363fcb7a53SMarius Strobl flags = MIIF_NOMANPAUSE; 137fb58dc87SMarius Strobl /* 1388e5d93dbSMarius Strobl * Am79C971 wedge when isolating all of their external PHYs. 139fb58dc87SMarius Strobl */ 1408e5d93dbSMarius Strobl if (strcmp(nic, "pcn") == 0) 1413fcb7a53SMarius Strobl flags |= MIIF_NOISOLATE; 1423fcb7a53SMarius Strobl mii_phy_dev_attach(dev, flags, &nsphy_funcs, 1); 143d0027533SBill Paul return (0); 144d0027533SBill Paul } 145d0027533SBill Paul 146d9730b8bSJonathan Lemon static int 1477d830ac9SWarner Losh nsphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd) 148d0027533SBill Paul { 149d0027533SBill Paul int reg; 150d0027533SBill Paul 151d0027533SBill Paul switch (cmd) { 152d0027533SBill Paul case MII_POLLSTAT: 153d0027533SBill Paul break; 154d0027533SBill Paul 155d0027533SBill Paul case MII_MEDIACHG: 156d0027533SBill Paul reg = PHY_READ(sc, MII_NSPHY_PCR); 157d0027533SBill Paul 158d0027533SBill Paul /* 159d0027533SBill Paul * Set up the PCR to use LED4 to indicate full-duplex 160d0027533SBill Paul * in both 10baseT and 100baseTX modes. 161d0027533SBill Paul */ 162d0027533SBill Paul reg |= PCR_LED4MODE; 163d0027533SBill Paul 164d0027533SBill Paul /* 1655aa54809SMarius Strobl * Make sure Carrier Integrity Monitor function is 166d0027533SBill Paul * disabled (normal for Node operation, but sometimes 167d0027533SBill Paul * it's not set?!) 168d0027533SBill Paul */ 169d0027533SBill Paul reg |= PCR_CIMDIS; 170d0027533SBill Paul 171d0027533SBill Paul /* 172d0027533SBill Paul * Make sure "force link good" is set to normal mode. 173d0027533SBill Paul * It's only intended for debugging. 174d0027533SBill Paul */ 175d0027533SBill Paul reg |= PCR_FLINK100; 176d0027533SBill Paul 177d0027533SBill Paul /* 178d0027533SBill Paul * Mystery bits which are supposedly `reserved', 179d0027533SBill Paul * but we seem to need to set them when the PHY 18032de04d5SJonathan Lemon * is connected to some interfaces: 18132de04d5SJonathan Lemon * 18232de04d5SJonathan Lemon * 0x0400 is needed for fxp 18332de04d5SJonathan Lemon * (Intel EtherExpress Pro 10+/100B, 82557 chip) 18432de04d5SJonathan Lemon * (nsphy with a DP83840 chip) 18532de04d5SJonathan Lemon * 0x0100 may be needed for some other card 186d0027533SBill Paul */ 187d0027533SBill Paul reg |= 0x0100 | 0x0400; 18832de04d5SJonathan Lemon 189fb58dc87SMarius Strobl if (strcmp(mii->mii_ifp->if_dname, "fxp") == 0) 190d0027533SBill Paul PHY_WRITE(sc, MII_NSPHY_PCR, reg); 19132de04d5SJonathan Lemon 192fb58dc87SMarius Strobl mii_phy_setmedia(sc); 193d0027533SBill Paul break; 194d0027533SBill Paul 195d0027533SBill Paul case MII_TICK: 196d9730b8bSJonathan Lemon if (mii_phy_tick(sc) == EJUSTRETURN) 197d0027533SBill Paul return (0); 198d0027533SBill Paul break; 199d0027533SBill Paul } 200d0027533SBill Paul 201d0027533SBill Paul /* Update the media status. */ 2023fcb7a53SMarius Strobl PHY_STATUS(sc); 203d0027533SBill Paul 204d0027533SBill Paul /* Callback if something changed. */ 205d9730b8bSJonathan Lemon mii_phy_update(sc, cmd); 206d0027533SBill Paul return (0); 207d0027533SBill Paul } 208d0027533SBill Paul 209d9730b8bSJonathan Lemon static void 2107d830ac9SWarner Losh nsphy_status(struct mii_softc *sc) 211d0027533SBill Paul { 212d0027533SBill Paul struct mii_data *mii = sc->mii_pdata; 213fb58dc87SMarius Strobl struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 214d0027533SBill Paul int bmsr, bmcr, par, anlpar; 215d0027533SBill Paul 216d0027533SBill Paul mii->mii_media_status = IFM_AVALID; 217d0027533SBill Paul mii->mii_media_active = IFM_ETHER; 218d0027533SBill Paul 219d0027533SBill Paul bmsr = PHY_READ(sc, MII_BMSR) | 220d0027533SBill Paul PHY_READ(sc, MII_BMSR); 221d0027533SBill Paul if (bmsr & BMSR_LINK) 222d0027533SBill Paul mii->mii_media_status |= IFM_ACTIVE; 223d0027533SBill Paul 224d0027533SBill Paul bmcr = PHY_READ(sc, MII_BMCR); 225d0027533SBill Paul if (bmcr & BMCR_ISO) { 226d0027533SBill Paul mii->mii_media_active |= IFM_NONE; 227d0027533SBill Paul mii->mii_media_status = 0; 228d0027533SBill Paul return; 229d0027533SBill Paul } 230d0027533SBill Paul 231d0027533SBill Paul if (bmcr & BMCR_LOOP) 232d0027533SBill Paul mii->mii_media_active |= IFM_LOOP; 233d0027533SBill Paul 234d0027533SBill Paul if (bmcr & BMCR_AUTOEN) { 235d0027533SBill Paul /* 23682f358ffSMarius Strobl * The PAR status bits are only valid if autonegotiation 237d0027533SBill Paul * has completed (or it's disabled). 238d0027533SBill Paul */ 239d0027533SBill Paul if ((bmsr & BMSR_ACOMP) == 0) { 240d0027533SBill Paul /* Erg, still trying, I guess... */ 241d0027533SBill Paul mii->mii_media_active |= IFM_NONE; 242d0027533SBill Paul return; 243d0027533SBill Paul } 244d0027533SBill Paul 245d0027533SBill Paul /* 246d0027533SBill Paul * Argh. The PAR doesn't seem to indicate duplex mode 247d0027533SBill Paul * properly! Determine media based on link partner's 248d0027533SBill Paul * advertised capabilities. 249d0027533SBill Paul */ 250d0027533SBill Paul if (PHY_READ(sc, MII_ANER) & ANER_LPAN) { 251d0027533SBill Paul anlpar = PHY_READ(sc, MII_ANAR) & 252d0027533SBill Paul PHY_READ(sc, MII_ANLPAR); 253d612cc59SPyun YongHyeon if (anlpar & ANLPAR_TX_FD) 254d0027533SBill Paul mii->mii_media_active |= IFM_100_TX|IFM_FDX; 255d612cc59SPyun YongHyeon else if (anlpar & ANLPAR_T4) 256d7a9ad56SMarius Strobl mii->mii_media_active |= IFM_100_T4|IFM_HDX; 257d0027533SBill Paul else if (anlpar & ANLPAR_TX) 258d7a9ad56SMarius Strobl mii->mii_media_active |= IFM_100_TX|IFM_HDX; 259d0027533SBill Paul else if (anlpar & ANLPAR_10_FD) 260d0027533SBill Paul mii->mii_media_active |= IFM_10_T|IFM_FDX; 261d0027533SBill Paul else if (anlpar & ANLPAR_10) 262d7a9ad56SMarius Strobl mii->mii_media_active |= IFM_10_T|IFM_HDX; 263d0027533SBill Paul else 264d0027533SBill Paul mii->mii_media_active |= IFM_NONE; 2653fcb7a53SMarius Strobl if ((mii->mii_media_active & IFM_FDX) != 0) 2663fcb7a53SMarius Strobl mii->mii_media_active |= 2673fcb7a53SMarius Strobl mii_phy_flowstatus(sc); 268d0027533SBill Paul return; 269d0027533SBill Paul } 270d0027533SBill Paul 271d0027533SBill Paul /* 272d0027533SBill Paul * Link partner is not capable of autonegotiation. 273d0027533SBill Paul * We will never be in full-duplex mode if this is 274d0027533SBill Paul * the case, so reading the PAR is OK. 275d0027533SBill Paul */ 276d0027533SBill Paul par = PHY_READ(sc, MII_NSPHY_PAR); 277d0027533SBill Paul if (par & PAR_10) 278d0027533SBill Paul mii->mii_media_active |= IFM_10_T; 279d0027533SBill Paul else 280d0027533SBill Paul mii->mii_media_active |= IFM_100_TX; 281d7a9ad56SMarius Strobl mii->mii_media_active |= IFM_HDX; 282d0027533SBill Paul } else 283fb58dc87SMarius Strobl mii->mii_media_active = ife->ifm_media; 284fb58dc87SMarius Strobl } 285fb58dc87SMarius Strobl 286fb58dc87SMarius Strobl static void 287fb58dc87SMarius Strobl nsphy_reset(struct mii_softc *sc) 288fb58dc87SMarius Strobl { 289fb58dc87SMarius Strobl struct ifmedia_entry *ife = sc->mii_pdata->mii_media.ifm_cur; 290fb58dc87SMarius Strobl int reg, i; 291fb58dc87SMarius Strobl 292fb58dc87SMarius Strobl if (sc->mii_flags & MIIF_NOISOLATE) 293fb58dc87SMarius Strobl reg = BMCR_RESET; 294fb58dc87SMarius Strobl else 295fb58dc87SMarius Strobl reg = BMCR_RESET | BMCR_ISO; 296fb58dc87SMarius Strobl PHY_WRITE(sc, MII_BMCR, reg); 297fb58dc87SMarius Strobl 298fb58dc87SMarius Strobl /* 29982f358ffSMarius Strobl * It is best to allow a little time for the reset to settle 30082f358ffSMarius Strobl * in before we start polling the BMCR again. Notably, the 30182f358ffSMarius Strobl * DP83840A manuals state that there should be a 500us delay 30282f358ffSMarius Strobl * between asserting software reset and attempting MII serial 30382f358ffSMarius Strobl * operations. Be conservative. 304fb58dc87SMarius Strobl */ 305fb58dc87SMarius Strobl DELAY(1000); 306fb58dc87SMarius Strobl 307fb58dc87SMarius Strobl /* 308fb58dc87SMarius Strobl * Wait another 2s for it to complete. 309fb58dc87SMarius Strobl * This is only a little overkill as under normal circumstances 310fb58dc87SMarius Strobl * the PHY can take up to 1s to complete reset. 311fb58dc87SMarius Strobl * This is also a bit odd because after a reset, the BMCR will 312fb58dc87SMarius Strobl * clear the reset bit and simply reports 0 even though the reset 313fb58dc87SMarius Strobl * is not yet complete. 314fb58dc87SMarius Strobl */ 315fb58dc87SMarius Strobl for (i = 0; i < 1000; i++) { 316fb58dc87SMarius Strobl reg = PHY_READ(sc, MII_BMCR); 317fb58dc87SMarius Strobl if (reg != 0 && (reg & BMCR_RESET) == 0) 318fb58dc87SMarius Strobl break; 319fb58dc87SMarius Strobl DELAY(2000); 320fb58dc87SMarius Strobl } 321fb58dc87SMarius Strobl 322fb58dc87SMarius Strobl if ((sc->mii_flags & MIIF_NOISOLATE) == 0) { 323fb58dc87SMarius Strobl if ((ife == NULL && sc->mii_inst != 0) || 324fb58dc87SMarius Strobl (ife != NULL && IFM_INST(ife->ifm_media) != sc->mii_inst)) 325fb58dc87SMarius Strobl PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO); 326fb58dc87SMarius Strobl } 327d0027533SBill Paul } 328