nsphyter.c revision 256281
1133852Sobrien/* $NetBSD: nsphyter.c,v 1.28 2008/01/20 07:58:19 msaitoh Exp $ */ 2150645Sjkim 3133852Sobrien/*- 4133852Sobrien * Copyright (c) 1998, 1999, 2000, 2001 The NetBSD Foundation, Inc. 5133852Sobrien * All rights reserved. 6133852Sobrien * 7133852Sobrien * This code is derived from software contributed to The NetBSD Foundation 8133852Sobrien * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 9133852Sobrien * NASA Ames Research Center. 10133852Sobrien * 11133852Sobrien * Redistribution and use in source and binary forms, with or without 12133852Sobrien * modification, are permitted provided that the following conditions 13133852Sobrien * are met: 14133852Sobrien * 1. Redistributions of source code must retain the above copyright 15133852Sobrien * notice, this list of conditions and the following disclaimer. 16133852Sobrien * 2. Redistributions in binary form must reproduce the above copyright 17133852Sobrien * notice, this list of conditions and the following disclaimer in the 18133852Sobrien * documentation and/or other materials provided with the distribution. 19133852Sobrien * 20133852Sobrien * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21133852Sobrien * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22133852Sobrien * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23133852Sobrien * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24133852Sobrien * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25133852Sobrien * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26133852Sobrien * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27133852Sobrien * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28133852Sobrien * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29133852Sobrien * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30133852Sobrien * POSSIBILITY OF SUCH DAMAGE. 31133852Sobrien */ 32133852Sobrien 33133852Sobrien/*- 34133852Sobrien * Copyright (c) 1997 Manuel Bouyer. All rights reserved. 35133852Sobrien * 36133852Sobrien * Redistribution and use in source and binary forms, with or without 37133852Sobrien * modification, are permitted provided that the following conditions 38133852Sobrien * are met: 39133852Sobrien * 1. Redistributions of source code must retain the above copyright 40133852Sobrien * notice, this list of conditions and the following disclaimer. 41133852Sobrien * 2. Redistributions in binary form must reproduce the above copyright 42173573Sjhb * notice, this list of conditions and the following disclaimer in the 43173573Sjhb * documentation and/or other materials provided with the distribution. 44133852Sobrien * 45133852Sobrien * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 46133852Sobrien * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 47133852Sobrien * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 48133852Sobrien * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 49133852Sobrien * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 50133852Sobrien * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 51133852Sobrien * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 52133852Sobrien * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 53133852Sobrien * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 54133852Sobrien * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 55133852Sobrien */ 56133852Sobrien 57133852Sobrien#include <sys/cdefs.h> 58150645Sjkim__FBSDID("$FreeBSD: stable/10/sys/dev/mii/nsphyter.c 231914 2012-02-19 12:25:58Z marius $"); 59150645Sjkim 60150645Sjkim/* 61150645Sjkim * Driver for the National Semiconductor's DP83843, DP83847 and DP83849 62150645Sjkim * `PHYTER' Ethernet 10/100 PHYs 63150645Sjkim * Data Sheets are available from http://www.national.com 64150645Sjkim * 65150645Sjkim * We also support the DP83815 `MacPHYTER' internal PHY since, for our 66150645Sjkim * purposes, they are compatible. 67152435Sjkim */ 68152435Sjkim 69152435Sjkim#include <sys/param.h> 70152435Sjkim#include <sys/systm.h> 71133852Sobrien#include <sys/bus.h> 72133852Sobrien#include <sys/errno.h> 73133852Sobrien#include <sys/kernel.h> 74133852Sobrien#include <sys/module.h> 75133852Sobrien#include <sys/socket.h> 76133852Sobrien 77150645Sjkim#include <net/if.h> 78150645Sjkim#include <net/if_media.h> 79150645Sjkim 80150645Sjkim#include <dev/mii/mii.h> 81150645Sjkim#include <dev/mii/miivar.h> 82152435Sjkim#include "miidevs.h" 83133852Sobrien 84133852Sobrien#include <dev/mii/nsphyterreg.h> 85133852Sobrien 86133852Sobrien#include "miibus_if.h" 87133852Sobrien 88163181Sjkimstatic device_probe_t nsphyter_probe; 89163181Sjkimstatic device_attach_t nsphyter_attach; 90163181Sjkim 91163181Sjkimstatic device_method_t nsphyter_methods[] = { 92133852Sobrien /* device interface */ 93133852Sobrien DEVMETHOD(device_probe, nsphyter_probe), 94133852Sobrien DEVMETHOD(device_attach, nsphyter_attach), 95133852Sobrien DEVMETHOD(device_detach, mii_phy_detach), 96141885Sanholt DEVMETHOD(device_shutdown, bus_generic_shutdown), 97133852Sobrien DEVMETHOD_END 98159075Sjkim}; 99159075Sjkim 100150645Sjkimstatic devclass_t nsphyter_devclass; 101150645Sjkim 102144517Sobrienstatic driver_t nsphyter_driver = { 103150645Sjkim "nsphyter", 104163181Sjkim nsphyter_methods, 105144517Sobrien sizeof(struct mii_softc) 106144809Sobrien}; 107150645Sjkim 108163181SjkimDRIVER_MODULE(nsphyter, miibus, nsphyter_driver, nsphyter_devclass, 0, 0); 109144809Sobrien 110133852Sobrienstatic int nsphyter_service(struct mii_softc *, struct mii_data *, int); 111133852Sobrienstatic void nsphyter_status(struct mii_softc *); 112150645Sjkimstatic void nsphyter_reset(struct mii_softc *); 113150645Sjkim 114136133Sanholtstatic const struct mii_phydesc nsphyters[] = { 115136133Sanholt MII_PHY_DESC(xxNATSEMI, DP83815), 116133852Sobrien MII_PHY_DESC(xxNATSEMI, DP83843), 117133852Sobrien MII_PHY_DESC(xxNATSEMI, DP83847), 118133852Sobrien MII_PHY_DESC(xxNATSEMI, DP83849), 119133852Sobrien MII_PHY_END 120163181Sjkim}; 121133852Sobrien 122133852Sobrienstatic const struct mii_phy_funcs nsphyter_funcs = { 123133852Sobrien nsphyter_service, 124150645Sjkim nsphyter_status, 125150645Sjkim nsphyter_reset 126150645Sjkim}; 127150645Sjkim 128150645Sjkimstatic int 129150645Sjkimnsphyter_probe(device_t dev) 130150645Sjkim{ 131163181Sjkim 132150645Sjkim return (mii_phy_dev_probe(dev, nsphyters, BUS_PROBE_DEFAULT)); 133163181Sjkim} 134150645Sjkim 135150645Sjkimstatic int 136150645Sjkimnsphyter_attach(device_t dev) 137152435Sjkim{ 138152435Sjkim 139152435Sjkim mii_phy_dev_attach(dev, MIIF_NOMANPAUSE, &nsphyter_funcs, 1); 140152435Sjkim return (0); 141152435Sjkim} 142152435Sjkim 143152435Sjkimstatic int 144152435Sjkimnsphyter_service(struct mii_softc *sc, struct mii_data *mii, int cmd) 145163181Sjkim{ 146152435Sjkim 147163181Sjkim switch (cmd) { 148152435Sjkim case MII_POLLSTAT: 149152435Sjkim break; 150152435Sjkim 151133852Sobrien case MII_MEDIACHG: 152133852Sobrien /* 153133852Sobrien * If the interface is not up, don't do anything. 154133852Sobrien */ 155133852Sobrien if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 156163181Sjkim break; 157133852Sobrien 158133852Sobrien mii_phy_setmedia(sc); 159163181Sjkim break; 160133852Sobrien 161133852Sobrien case MII_TICK: 162163181Sjkim if (mii_phy_tick(sc) == EJUSTRETURN) 163133852Sobrien return (0); 164133852Sobrien break; 165133852Sobrien } 166133852Sobrien 167133852Sobrien /* Update the media status. */ 168133852Sobrien PHY_STATUS(sc); 169133852Sobrien 170133852Sobrien /* Callback if something changed. */ 171133852Sobrien mii_phy_update(sc, cmd); 172133852Sobrien return (0); 173133852Sobrien} 174133852Sobrien 175133852Sobrienstatic void 176133852Sobriennsphyter_status(struct mii_softc *sc) 177133852Sobrien{ 178133852Sobrien struct mii_data *mii = sc->mii_pdata; 179163181Sjkim struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 180133852Sobrien int bmsr, bmcr, physts; 181133852Sobrien 182133852Sobrien mii->mii_media_status = IFM_AVALID; 183161518Sjkim mii->mii_media_active = IFM_ETHER; 184150645Sjkim 185150645Sjkim bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR); 186133852Sobrien physts = PHY_READ(sc, MII_NSPHYTER_PHYSTS); 187133852Sobrien 188163181Sjkim if ((physts & PHYSTS_LINK) != 0) 189133852Sobrien mii->mii_media_status |= IFM_ACTIVE; 190133852Sobrien 191133852Sobrien bmcr = PHY_READ(sc, MII_BMCR); 192133852Sobrien if ((bmcr & BMCR_ISO) != 0) { 193133852Sobrien mii->mii_media_active |= IFM_NONE; 194133852Sobrien mii->mii_media_status = 0; 195133852Sobrien return; 196133852Sobrien } 197133852Sobrien 198133852Sobrien if ((bmcr & BMCR_LOOP) != 0) 199133852Sobrien mii->mii_media_active |= IFM_LOOP; 200133852Sobrien 201133852Sobrien if ((bmcr & BMCR_AUTOEN) != 0) { 202133852Sobrien /* 203163181Sjkim * The media status bits are only valid if autonegotiation 204133852Sobrien * has completed (or it's disabled). 205133852Sobrien */ 206133852Sobrien if ((bmsr & BMSR_ACOMP) == 0) { 207133852Sobrien /* Erg, still trying, I guess... */ 208152435Sjkim mii->mii_media_active |= IFM_NONE; 209152435Sjkim return; 210152435Sjkim } 211152435Sjkim 212163181Sjkim if ((physts & PHYSTS_SPEED10) != 0) 213152435Sjkim mii->mii_media_active |= IFM_10_T; 214152435Sjkim else 215152435Sjkim mii->mii_media_active |= IFM_100_TX; 216152435Sjkim if ((physts & PHYSTS_DUPLEX) != 0) 217152435Sjkim mii->mii_media_active |= 218163181Sjkim IFM_FDX | mii_phy_flowstatus(sc); 219152435Sjkim else 220152435Sjkim mii->mii_media_active |= IFM_HDX; 221152435Sjkim } else 222152435Sjkim mii->mii_media_active = ife->ifm_media; 223152435Sjkim} 224152435Sjkim 225152435Sjkimstatic void 226152435Sjkimnsphyter_reset(struct mii_softc *sc) 227163181Sjkim{ 228152435Sjkim struct ifmedia_entry *ife = sc->mii_pdata->mii_media.ifm_cur; 229152435Sjkim int reg, i; 230152435Sjkim 231152435Sjkim if ((sc->mii_flags & MIIF_NOISOLATE) != 0) 232133852Sobrien reg = BMCR_RESET; 233133852Sobrien else 234133852Sobrien reg = BMCR_RESET | BMCR_ISO; 235133852Sobrien PHY_WRITE(sc, MII_BMCR, reg); 236133852Sobrien 237133852Sobrien /* 238133852Sobrien * It is best to allow a little time for the reset to settle 239133852Sobrien * in before we start polling the BMCR again. Notably, the 240133852Sobrien * DP8384{3,7} manuals state that there should be a 500us delay 241133852Sobrien * between asserting software reset and attempting MII serial 242133852Sobrien * operations. Be conservative. Also, a DP83815 can get into 243133852Sobrien * a bad state on cable removal and reinsertion if we do not 244133852Sobrien * delay here. 245133852Sobrien */ 246163181Sjkim DELAY(1000); 247133852Sobrien 248133852Sobrien /* 249133852Sobrien * Wait another 2s for it to complete. 250133852Sobrien * This is only a little overkill as under normal circumstances 251133852Sobrien * the PHY can take up to 1s to complete reset. 252133852Sobrien * This is also a bit odd because after a reset, the BMCR will 253173203Sjhb * clear the reset bit and simply reports 0 even though the reset 254133852Sobrien * is not yet complete. 255173203Sjhb */ 256133852Sobrien for (i = 0; i < 1000; i++) { 257133852Sobrien reg = PHY_READ(sc, MII_BMCR); 258133852Sobrien if (reg != 0 && (reg & BMCR_RESET) == 0) 259133852Sobrien break; 260133852Sobrien DELAY(2000); 261133852Sobrien } 262133852Sobrien 263133852Sobrien if ((sc->mii_flags & MIIF_NOISOLATE) == 0) { 264173203Sjhb if ((ife == NULL && sc->mii_inst != 0) || 265133852Sobrien (ife != NULL && IFM_INST(ife->ifm_media) != sc->mii_inst)) 266163181Sjkim PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO); 267133852Sobrien } 268133852Sobrien} 269133852Sobrien