nsphyter.c revision 231914
1295373Sdteske/* $NetBSD: nsphyter.c,v 1.28 2008/01/20 07:58:19 msaitoh Exp $ */ 2295373Sdteske 3295373Sdteske/*- 4295373Sdteske * Copyright (c) 1998, 1999, 2000, 2001 The NetBSD Foundation, Inc. 5295373Sdteske * All rights reserved. 6295373Sdteske * 7295373Sdteske * This code is derived from software contributed to The NetBSD Foundation 8295373Sdteske * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 9295373Sdteske * NASA Ames Research Center. 10295373Sdteske * 11295373Sdteske * Redistribution and use in source and binary forms, with or without 12295373Sdteske * modification, are permitted provided that the following conditions 13295373Sdteske * are met: 14295373Sdteske * 1. Redistributions of source code must retain the above copyright 15295373Sdteske * notice, this list of conditions and the following disclaimer. 16295373Sdteske * 2. Redistributions in binary form must reproduce the above copyright 17295373Sdteske * notice, this list of conditions and the following disclaimer in the 18295373Sdteske * documentation and/or other materials provided with the distribution. 19295373Sdteske * 20295373Sdteske * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21295373Sdteske * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22295373Sdteske * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23295373Sdteske * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24295373Sdteske * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25295373Sdteske * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26295373Sdteske * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27295373Sdteske * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28295373Sdteske * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29295373Sdteske * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30295373Sdteske * POSSIBILITY OF SUCH DAMAGE. 31295373Sdteske */ 32295373Sdteske 33295373Sdteske/*- 34295373Sdteske * Copyright (c) 1997 Manuel Bouyer. All rights reserved. 35295373Sdteske * 36295400Sdteske * Redistribution and use in source and binary forms, with or without 37295400Sdteske * modification, are permitted provided that the following conditions 38295400Sdteske * are met: 39295373Sdteske * 1. Redistributions of source code must retain the above copyright 40295400Sdteske * notice, this list of conditions and the following disclaimer. 41295400Sdteske * 2. Redistributions in binary form must reproduce the above copyright 42295373Sdteske * notice, this list of conditions and the following disclaimer in the 43295373Sdteske * documentation and/or other materials provided with the distribution. 44295373Sdteske * 45295373Sdteske * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 46295373Sdteske * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 47295373Sdteske * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 48295373Sdteske * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 49295373Sdteske * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 50295373Sdteske * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 51295373Sdteske * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 52295373Sdteske * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 53295373Sdteske * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 54295373Sdteske * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 55295373Sdteske */ 56295373Sdteske 57295373Sdteske#include <sys/cdefs.h> 58295373Sdteske__FBSDID("$FreeBSD: head/sys/dev/mii/nsphyter.c 231914 2012-02-19 12:25:58Z marius $"); 59295373Sdteske 60295373Sdteske/* 61295373Sdteske * Driver for the National Semiconductor's DP83843, DP83847 and DP83849 62295373Sdteske * `PHYTER' Ethernet 10/100 PHYs 63295373Sdteske * Data Sheets are available from http://www.national.com 64295373Sdteske * 65295373Sdteske * We also support the DP83815 `MacPHYTER' internal PHY since, for our 66295373Sdteske * purposes, they are compatible. 67295373Sdteske */ 68295373Sdteske 69295373Sdteske#include <sys/param.h> 70295373Sdteske#include <sys/systm.h> 71295373Sdteske#include <sys/bus.h> 72295373Sdteske#include <sys/errno.h> 73295373Sdteske#include <sys/kernel.h> 74295373Sdteske#include <sys/module.h> 75295373Sdteske#include <sys/socket.h> 76295373Sdteske 77295373Sdteske#include <net/if.h> 78295373Sdteske#include <net/if_media.h> 79295373Sdteske 80295373Sdteske#include <dev/mii/mii.h> 81295373Sdteske#include <dev/mii/miivar.h> 82295373Sdteske#include "miidevs.h" 83295373Sdteske 84295373Sdteske#include <dev/mii/nsphyterreg.h> 85295373Sdteske 86295373Sdteske#include "miibus_if.h" 87295373Sdteske 88295373Sdteskestatic device_probe_t nsphyter_probe; 89295373Sdteskestatic device_attach_t nsphyter_attach; 90295373Sdteske 91295373Sdteskestatic device_method_t nsphyter_methods[] = { 92295373Sdteske /* device interface */ 93295373Sdteske DEVMETHOD(device_probe, nsphyter_probe), 94295373Sdteske DEVMETHOD(device_attach, nsphyter_attach), 95295373Sdteske DEVMETHOD(device_detach, mii_phy_detach), 96295373Sdteske DEVMETHOD(device_shutdown, bus_generic_shutdown), 97295373Sdteske DEVMETHOD_END 98295373Sdteske}; 99295373Sdteske 100295373Sdteskestatic devclass_t nsphyter_devclass; 101295373Sdteske 102295373Sdteskestatic driver_t nsphyter_driver = { 103295373Sdteske "nsphyter", 104295373Sdteske nsphyter_methods, 105295373Sdteske sizeof(struct mii_softc) 106295373Sdteske}; 107295373Sdteske 108295373SdteskeDRIVER_MODULE(nsphyter, miibus, nsphyter_driver, nsphyter_devclass, 0, 0); 109295373Sdteske 110295373Sdteskestatic int nsphyter_service(struct mii_softc *, struct mii_data *, int); 111295373Sdteskestatic void nsphyter_status(struct mii_softc *); 112295373Sdteskestatic void nsphyter_reset(struct mii_softc *); 113295373Sdteske 114295373Sdteskestatic const struct mii_phydesc nsphyters[] = { 115295373Sdteske MII_PHY_DESC(xxNATSEMI, DP83815), 116295373Sdteske MII_PHY_DESC(xxNATSEMI, DP83843), 117295373Sdteske MII_PHY_DESC(xxNATSEMI, DP83847), 118295373Sdteske MII_PHY_DESC(xxNATSEMI, DP83849), 119295373Sdteske MII_PHY_END 120295373Sdteske}; 121295373Sdteske 122295373Sdteskestatic const struct mii_phy_funcs nsphyter_funcs = { 123295373Sdteske nsphyter_service, 124295373Sdteske nsphyter_status, 125295441Sdteske nsphyter_reset 126295373Sdteske}; 127295373Sdteske 128295373Sdteskestatic int 129295373Sdteskensphyter_probe(device_t dev) 130295373Sdteske{ 131295373Sdteske 132295373Sdteske return (mii_phy_dev_probe(dev, nsphyters, BUS_PROBE_DEFAULT)); 133295373Sdteske} 134295373Sdteske 135295373Sdteskestatic int 136295373Sdteskensphyter_attach(device_t dev) 137295373Sdteske{ 138295373Sdteske 139295373Sdteske mii_phy_dev_attach(dev, MIIF_NOMANPAUSE, &nsphyter_funcs, 1); 140295373Sdteske return (0); 141295373Sdteske} 142295373Sdteske 143295373Sdteskestatic int 144295373Sdteskensphyter_service(struct mii_softc *sc, struct mii_data *mii, int cmd) 145295373Sdteske{ 146295373Sdteske 147295373Sdteske switch (cmd) { 148295373Sdteske case MII_POLLSTAT: 149295373Sdteske break; 150295373Sdteske 151295373Sdteske case MII_MEDIACHG: 152295373Sdteske /* 153295373Sdteske * If the interface is not up, don't do anything. 154295373Sdteske */ 155295373Sdteske if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 156295373Sdteske break; 157295373Sdteske 158295373Sdteske mii_phy_setmedia(sc); 159295373Sdteske break; 160295373Sdteske 161295373Sdteske case MII_TICK: 162295373Sdteske if (mii_phy_tick(sc) == EJUSTRETURN) 163295373Sdteske return (0); 164295373Sdteske break; 165295373Sdteske } 166295373Sdteske 167295373Sdteske /* Update the media status. */ 168295373Sdteske PHY_STATUS(sc); 169295373Sdteske 170295373Sdteske /* Callback if something changed. */ 171295373Sdteske mii_phy_update(sc, cmd); 172295373Sdteske return (0); 173295546Sdteske} 174295546Sdteske 175295546Sdteskestatic void 176295546Sdteskensphyter_status(struct mii_softc *sc) 177295546Sdteske{ 178295546Sdteske struct mii_data *mii = sc->mii_pdata; 179295546Sdteske struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 180295546Sdteske int bmsr, bmcr, physts; 181295546Sdteske 182295546Sdteske mii->mii_media_status = IFM_AVALID; 183295546Sdteske mii->mii_media_active = IFM_ETHER; 184295546Sdteske 185295546Sdteske bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR); 186295546Sdteske physts = PHY_READ(sc, MII_NSPHYTER_PHYSTS); 187295546Sdteske 188295546Sdteske if ((physts & PHYSTS_LINK) != 0) 189295546Sdteske mii->mii_media_status |= IFM_ACTIVE; 190295546Sdteske 191295546Sdteske bmcr = PHY_READ(sc, MII_BMCR); 192295546Sdteske if ((bmcr & BMCR_ISO) != 0) { 193295546Sdteske mii->mii_media_active |= IFM_NONE; 194295546Sdteske mii->mii_media_status = 0; 195295546Sdteske return; 196295546Sdteske } 197295546Sdteske 198295546Sdteske if ((bmcr & BMCR_LOOP) != 0) 199295546Sdteske mii->mii_media_active |= IFM_LOOP; 200295546Sdteske 201295546Sdteske if ((bmcr & BMCR_AUTOEN) != 0) { 202295546Sdteske /* 203295546Sdteske * The media status bits are only valid if autonegotiation 204295546Sdteske * has completed (or it's disabled). 205295546Sdteske */ 206295546Sdteske if ((bmsr & BMSR_ACOMP) == 0) { 207295546Sdteske /* Erg, still trying, I guess... */ 208295546Sdteske mii->mii_media_active |= IFM_NONE; 209295546Sdteske return; 210295546Sdteske } 211295546Sdteske 212295546Sdteske if ((physts & PHYSTS_SPEED10) != 0) 213295546Sdteske mii->mii_media_active |= IFM_10_T; 214295546Sdteske else 215295546Sdteske mii->mii_media_active |= IFM_100_TX; 216295546Sdteske if ((physts & PHYSTS_DUPLEX) != 0) 217295546Sdteske mii->mii_media_active |= 218295546Sdteske IFM_FDX | mii_phy_flowstatus(sc); 219295546Sdteske else 220295546Sdteske mii->mii_media_active |= IFM_HDX; 221295546Sdteske } else 222295546Sdteske mii->mii_media_active = ife->ifm_media; 223295546Sdteske} 224295546Sdteske 225295546Sdteskestatic void 226295546Sdteskensphyter_reset(struct mii_softc *sc) 227295546Sdteske{ 228295546Sdteske struct ifmedia_entry *ife = sc->mii_pdata->mii_media.ifm_cur; 229295546Sdteske int reg, i; 230295546Sdteske 231295546Sdteske if ((sc->mii_flags & MIIF_NOISOLATE) != 0) 232295546Sdteske reg = BMCR_RESET; 233295546Sdteske else 234295546Sdteske reg = BMCR_RESET | BMCR_ISO; 235295546Sdteske PHY_WRITE(sc, MII_BMCR, reg); 236295546Sdteske 237295546Sdteske /* 238295546Sdteske * It is best to allow a little time for the reset to settle 239295546Sdteske * in before we start polling the BMCR again. Notably, the 240295546Sdteske * DP8384{3,7} manuals state that there should be a 500us delay 241295546Sdteske * between asserting software reset and attempting MII serial 242295546Sdteske * operations. Be conservative. Also, a DP83815 can get into 243295546Sdteske * a bad state on cable removal and reinsertion if we do not 244295546Sdteske * delay here. 245295546Sdteske */ 246295546Sdteske DELAY(1000); 247295546Sdteske 248295546Sdteske /* 249295546Sdteske * Wait another 2s for it to complete. 250295546Sdteske * This is only a little overkill as under normal circumstances 251295546Sdteske * the PHY can take up to 1s to complete reset. 252295546Sdteske * This is also a bit odd because after a reset, the BMCR will 253295546Sdteske * clear the reset bit and simply reports 0 even though the reset 254295546Sdteske * is not yet complete. 255295546Sdteske */ 256295546Sdteske for (i = 0; i < 1000; i++) { 257295546Sdteske reg = PHY_READ(sc, MII_BMCR); 258295373Sdteske if (reg != 0 && (reg & BMCR_RESET) == 0) 259295373Sdteske break; 260295373Sdteske DELAY(2000); 261295373Sdteske } 262295373Sdteske 263295373Sdteske if ((sc->mii_flags & MIIF_NOISOLATE) == 0) { 264295373Sdteske if ((ife == NULL && sc->mii_inst != 0) || 265295373Sdteske (ife != NULL && IFM_INST(ife->ifm_media) != sc->mii_inst)) 266295373Sdteske PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO); 267295373Sdteske } 268295373Sdteske} 269295373Sdteske