nsphy.c revision 129842
150120Swpaul/* $NetBSD: nsphy.c,v 1.18 1999/07/14 23:57:36 thorpej Exp $ */ 250120Swpaul 350120Swpaul/*- 450120Swpaul * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc. 550120Swpaul * All rights reserved. 650120Swpaul * 750120Swpaul * This code is derived from software contributed to The NetBSD Foundation 850120Swpaul * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 950120Swpaul * NASA Ames Research Center. 1050120Swpaul * 1150120Swpaul * Redistribution and use in source and binary forms, with or without 1250120Swpaul * modification, are permitted provided that the following conditions 1350120Swpaul * are met: 1450120Swpaul * 1. Redistributions of source code must retain the above copyright 1550120Swpaul * notice, this list of conditions and the following disclaimer. 1650120Swpaul * 2. Redistributions in binary form must reproduce the above copyright 1750120Swpaul * notice, this list of conditions and the following disclaimer in the 1850120Swpaul * documentation and/or other materials provided with the distribution. 1950120Swpaul * 3. All advertising materials mentioning features or use of this software 2050120Swpaul * must display the following acknowledgement: 2150120Swpaul * This product includes software developed by the NetBSD 2250120Swpaul * Foundation, Inc. and its contributors. 2350120Swpaul * 4. Neither the name of The NetBSD Foundation nor the names of its 2450120Swpaul * contributors may be used to endorse or promote products derived 2550120Swpaul * from this software without specific prior written permission. 2650120Swpaul * 2750120Swpaul * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 2850120Swpaul * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 2950120Swpaul * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 3050120Swpaul * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 3150120Swpaul * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 3250120Swpaul * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 3350120Swpaul * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 3450120Swpaul * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 3550120Swpaul * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 3650120Swpaul * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 3750120Swpaul * POSSIBILITY OF SUCH DAMAGE. 3850120Swpaul */ 3950120Swpaul 40119418Sobrien#include <sys/cdefs.h> 41119418Sobrien__FBSDID("$FreeBSD: head/sys/dev/mii/nsphy.c 129842 2004-05-29 16:54:59Z marius $"); 42119418Sobrien 4350120Swpaul/* 4450120Swpaul * Copyright (c) 1997 Manuel Bouyer. All rights reserved. 4550120Swpaul * 4650120Swpaul * Redistribution and use in source and binary forms, with or without 4750120Swpaul * modification, are permitted provided that the following conditions 4850120Swpaul * are met: 4950120Swpaul * 1. Redistributions of source code must retain the above copyright 5050120Swpaul * notice, this list of conditions and the following disclaimer. 5150120Swpaul * 2. Redistributions in binary form must reproduce the above copyright 5250120Swpaul * notice, this list of conditions and the following disclaimer in the 5350120Swpaul * documentation and/or other materials provided with the distribution. 5450120Swpaul * 3. All advertising materials mentioning features or use of this software 5550120Swpaul * must display the following acknowledgement: 5650120Swpaul * This product includes software developed by Manuel Bouyer. 5750120Swpaul * 4. The name of the author may not be used to endorse or promote products 5850120Swpaul * derived from this software without specific prior written permission. 5950120Swpaul * 6050120Swpaul * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 6150120Swpaul * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 6250120Swpaul * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 6350120Swpaul * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 6450120Swpaul * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 6550120Swpaul * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 6650120Swpaul * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 6750120Swpaul * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 6850120Swpaul * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 6950120Swpaul * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 7050120Swpaul */ 7150120Swpaul 7250120Swpaul/* 7350120Swpaul * driver for National Semiconductor's DP83840A ethernet 10/100 PHY 7450120Swpaul * Data Sheet available from www.national.com 7550120Swpaul */ 7650120Swpaul 77113038Sobrien#include <sys/cdefs.h> 78113038Sobrien__FBSDID("$FreeBSD: head/sys/dev/mii/nsphy.c 129842 2004-05-29 16:54:59Z marius $"); 79113038Sobrien 8050120Swpaul#include <sys/param.h> 8150120Swpaul#include <sys/systm.h> 8250120Swpaul#include <sys/kernel.h> 8350120Swpaul#include <sys/socket.h> 8450120Swpaul#include <sys/errno.h> 8550120Swpaul#include <sys/module.h> 8650120Swpaul#include <sys/bus.h> 8750120Swpaul 8850120Swpaul#include <net/if.h> 8950120Swpaul#include <net/if_media.h> 9050120Swpaul 9150120Swpaul#include <dev/mii/mii.h> 9250120Swpaul#include <dev/mii/miivar.h> 93109514Sobrien#include "miidevs.h" 9450120Swpaul 9550120Swpaul#include <dev/mii/nsphyreg.h> 9650120Swpaul 9750120Swpaul#include "miibus_if.h" 9850120Swpaul 99105135Salfredstatic int nsphy_probe(device_t); 100105135Salfredstatic int nsphy_attach(device_t); 10150120Swpaul 10250120Swpaulstatic device_method_t nsphy_methods[] = { 10350120Swpaul /* device interface */ 10450120Swpaul DEVMETHOD(device_probe, nsphy_probe), 10550120Swpaul DEVMETHOD(device_attach, nsphy_attach), 10695722Sphk DEVMETHOD(device_detach, mii_phy_detach), 10750120Swpaul DEVMETHOD(device_shutdown, bus_generic_shutdown), 10850120Swpaul { 0, 0 } 10950120Swpaul}; 11050120Swpaul 11150120Swpaulstatic devclass_t nsphy_devclass; 11250120Swpaul 11350120Swpaulstatic driver_t nsphy_driver = { 11450120Swpaul "nsphy", 11550120Swpaul nsphy_methods, 11650120Swpaul sizeof(struct mii_softc) 11750120Swpaul}; 11850120Swpaul 11950120SwpaulDRIVER_MODULE(nsphy, miibus, nsphy_driver, nsphy_devclass, 0, 0); 12050120Swpaul 12192739Salfredstatic int nsphy_service(struct mii_softc *, struct mii_data *, int); 12292739Salfredstatic void nsphy_status(struct mii_softc *); 12350120Swpaul 124105135Salfredstatic int 125105135Salfrednsphy_probe(dev) 12650120Swpaul device_t dev; 12750120Swpaul{ 12850120Swpaul struct mii_attach_args *ma; 12950120Swpaul 13050120Swpaul ma = device_get_ivars(dev); 13150120Swpaul 13259153Ssemenu if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_NATSEMI && 13359153Ssemenu MII_MODEL(ma->mii_id2) == MII_MODEL_NATSEMI_DP83840) { 13459153Ssemenu device_set_desc(dev, MII_STR_NATSEMI_DP83840); 13559153Ssemenu } else 13650120Swpaul return (ENXIO); 13750120Swpaul 13850120Swpaul return (0); 13950120Swpaul} 14050120Swpaul 141105135Salfredstatic int 142105135Salfrednsphy_attach(dev) 14350120Swpaul device_t dev; 14450120Swpaul{ 14550120Swpaul struct mii_softc *sc; 14650120Swpaul struct mii_attach_args *ma; 14750120Swpaul struct mii_data *mii; 14850120Swpaul 14950120Swpaul sc = device_get_softc(dev); 15050120Swpaul ma = device_get_ivars(dev); 15150120Swpaul sc->mii_dev = device_get_parent(dev); 15250120Swpaul mii = device_get_softc(sc->mii_dev); 15350120Swpaul LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list); 15450120Swpaul 15550120Swpaul sc->mii_inst = mii->mii_instance; 15650120Swpaul sc->mii_phy = ma->mii_phyno; 15750120Swpaul sc->mii_service = nsphy_service; 15850120Swpaul sc->mii_pdata = mii; 15950120Swpaul 16050120Swpaul#ifdef foo 16150120Swpaul /* 16250120Swpaul * i82557 wedges if all of its PHYs are isolated! 16350120Swpaul */ 16450120Swpaul if (strcmp(device_get_name(device_get_parent(sc->mii_dev)), 16550120Swpaul "fxp") == 0 && mii->mii_instance == 0) 16650120Swpaul#endif 16750120Swpaul 16850120Swpaul sc->mii_flags |= MIIF_NOISOLATE; 16950120Swpaul mii->mii_instance++; 17050120Swpaul 17150120Swpaul#define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL) 17250120Swpaul 17350120Swpaul#if 0 17450120Swpaul /* Can't do this on the i82557! */ 17550120Swpaul ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst), 17650120Swpaul BMCR_ISO); 17750120Swpaul#endif 17850120Swpaul ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->mii_inst), 17950120Swpaul BMCR_LOOP|BMCR_S100); 18050120Swpaul 18150120Swpaul mii_phy_reset(sc); 18250120Swpaul 18350120Swpaul sc->mii_capabilities = 18450120Swpaul PHY_READ(sc, MII_BMSR) & ma->mii_capmask; 18550120Swpaul device_printf(dev, " "); 18695667Sphk mii_add_media(sc); 18750120Swpaul printf("\n"); 18850120Swpaul#undef ADD 18950120Swpaul 19050120Swpaul MIIBUS_MEDIAINIT(sc->mii_dev); 19150120Swpaul return(0); 19250120Swpaul} 19350120Swpaul 19484145Sjlemonstatic int 19550120Swpaulnsphy_service(sc, mii, cmd) 19650120Swpaul struct mii_softc *sc; 19750120Swpaul struct mii_data *mii; 19850120Swpaul int cmd; 19950120Swpaul{ 20050120Swpaul struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 20150120Swpaul int reg; 20250120Swpaul 20350120Swpaul switch (cmd) { 20450120Swpaul case MII_POLLSTAT: 20550120Swpaul /* 20650120Swpaul * If we're not polling our PHY instance, just return. 20750120Swpaul */ 20850120Swpaul if (IFM_INST(ife->ifm_media) != sc->mii_inst) 20950120Swpaul return (0); 21050120Swpaul break; 21150120Swpaul 21250120Swpaul case MII_MEDIACHG: 21350120Swpaul /* 21450120Swpaul * If the media indicates a different PHY instance, 21550120Swpaul * isolate ourselves. 21650120Swpaul */ 21750120Swpaul if (IFM_INST(ife->ifm_media) != sc->mii_inst) { 21850120Swpaul reg = PHY_READ(sc, MII_BMCR); 21950120Swpaul PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO); 22050120Swpaul return (0); 22150120Swpaul } 22250120Swpaul 22350120Swpaul /* 22450120Swpaul * If the interface is not up, don't do anything. 22550120Swpaul */ 22650120Swpaul if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 22750120Swpaul break; 22850120Swpaul 22950120Swpaul reg = PHY_READ(sc, MII_NSPHY_PCR); 23050120Swpaul 23150120Swpaul /* 23250120Swpaul * Set up the PCR to use LED4 to indicate full-duplex 23350120Swpaul * in both 10baseT and 100baseTX modes. 23450120Swpaul */ 23550120Swpaul reg |= PCR_LED4MODE; 23650120Swpaul 23750120Swpaul /* 238129842Smarius * Make sure Carrier Integrity Monitor function is 23950120Swpaul * disabled (normal for Node operation, but sometimes 24050120Swpaul * it's not set?!) 24150120Swpaul */ 24250120Swpaul reg |= PCR_CIMDIS; 24350120Swpaul 24450120Swpaul /* 24550120Swpaul * Make sure "force link good" is set to normal mode. 24650120Swpaul * It's only intended for debugging. 24750120Swpaul */ 24850120Swpaul reg |= PCR_FLINK100; 24950120Swpaul 25050120Swpaul /* 25150120Swpaul * Mystery bits which are supposedly `reserved', 25250120Swpaul * but we seem to need to set them when the PHY 25374353Sjlemon * is connected to some interfaces: 25474353Sjlemon * 25574353Sjlemon * 0x0400 is needed for fxp 25674353Sjlemon * (Intel EtherExpress Pro 10+/100B, 82557 chip) 25774353Sjlemon * (nsphy with a DP83840 chip) 25874353Sjlemon * 0x0100 may be needed for some other card 25950120Swpaul */ 26050120Swpaul reg |= 0x0100 | 0x0400; 26174353Sjlemon 26277634Sjlemon if (strcmp(device_get_name(device_get_parent(sc->mii_dev)), 26377634Sjlemon "fxp") == 0) 26477634Sjlemon PHY_WRITE(sc, MII_NSPHY_PCR, reg); 26574353Sjlemon 26650120Swpaul switch (IFM_SUBTYPE(ife->ifm_media)) { 26750120Swpaul case IFM_AUTO: 26850120Swpaul /* 26950120Swpaul * If we're already in auto mode, just return. 27050120Swpaul */ 27150120Swpaul if (PHY_READ(sc, MII_BMCR) & BMCR_AUTOEN) 27250120Swpaul return (0); 27396026Sphk (void) mii_phy_auto(sc); 27450120Swpaul break; 27550120Swpaul case IFM_100_T4: 27650120Swpaul /* 27750120Swpaul * XXX Not supported as a manual setting right now. 27850120Swpaul */ 27950120Swpaul return (EINVAL); 28050120Swpaul default: 28150120Swpaul /* 28250120Swpaul * BMCR data is stored in the ifmedia entry. 28350120Swpaul */ 28450120Swpaul PHY_WRITE(sc, MII_ANAR, 28550120Swpaul mii_anar(ife->ifm_media)); 28650120Swpaul PHY_WRITE(sc, MII_BMCR, ife->ifm_data); 28750120Swpaul } 28850120Swpaul break; 28950120Swpaul 29050120Swpaul case MII_TICK: 29150120Swpaul /* 29250120Swpaul * If we're not currently selected, just return. 29350120Swpaul */ 29450120Swpaul if (IFM_INST(ife->ifm_media) != sc->mii_inst) 29550120Swpaul return (0); 29684145Sjlemon if (mii_phy_tick(sc) == EJUSTRETURN) 29750120Swpaul return (0); 29850120Swpaul break; 29950120Swpaul } 30050120Swpaul 30150120Swpaul /* Update the media status. */ 30250120Swpaul nsphy_status(sc); 30350120Swpaul 30450120Swpaul /* Callback if something changed. */ 30584145Sjlemon mii_phy_update(sc, cmd); 30650120Swpaul return (0); 30750120Swpaul} 30850120Swpaul 30984145Sjlemonstatic void 31050120Swpaulnsphy_status(sc) 31150120Swpaul struct mii_softc *sc; 31250120Swpaul{ 31350120Swpaul struct mii_data *mii = sc->mii_pdata; 31450120Swpaul int bmsr, bmcr, par, anlpar; 31550120Swpaul 31650120Swpaul mii->mii_media_status = IFM_AVALID; 31750120Swpaul mii->mii_media_active = IFM_ETHER; 31850120Swpaul 31950120Swpaul bmsr = PHY_READ(sc, MII_BMSR) | 32050120Swpaul PHY_READ(sc, MII_BMSR); 32150120Swpaul if (bmsr & BMSR_LINK) 32250120Swpaul mii->mii_media_status |= IFM_ACTIVE; 32350120Swpaul 32450120Swpaul bmcr = PHY_READ(sc, MII_BMCR); 32550120Swpaul if (bmcr & BMCR_ISO) { 32650120Swpaul mii->mii_media_active |= IFM_NONE; 32750120Swpaul mii->mii_media_status = 0; 32850120Swpaul return; 32950120Swpaul } 33050120Swpaul 33150120Swpaul if (bmcr & BMCR_LOOP) 33250120Swpaul mii->mii_media_active |= IFM_LOOP; 33350120Swpaul 33450120Swpaul if (bmcr & BMCR_AUTOEN) { 33550120Swpaul /* 33650120Swpaul * The PAR status bits are only valid of autonegotiation 33750120Swpaul * has completed (or it's disabled). 33850120Swpaul */ 33950120Swpaul if ((bmsr & BMSR_ACOMP) == 0) { 34050120Swpaul /* Erg, still trying, I guess... */ 34150120Swpaul mii->mii_media_active |= IFM_NONE; 34250120Swpaul return; 34350120Swpaul } 34450120Swpaul 34550120Swpaul /* 34650120Swpaul * Argh. The PAR doesn't seem to indicate duplex mode 34750120Swpaul * properly! Determine media based on link partner's 34850120Swpaul * advertised capabilities. 34950120Swpaul */ 35050120Swpaul if (PHY_READ(sc, MII_ANER) & ANER_LPAN) { 35150120Swpaul anlpar = PHY_READ(sc, MII_ANAR) & 35250120Swpaul PHY_READ(sc, MII_ANLPAR); 35350120Swpaul if (anlpar & ANLPAR_T4) 35450120Swpaul mii->mii_media_active |= IFM_100_T4; 35550120Swpaul else if (anlpar & ANLPAR_TX_FD) 35650120Swpaul mii->mii_media_active |= IFM_100_TX|IFM_FDX; 35750120Swpaul else if (anlpar & ANLPAR_TX) 35850120Swpaul mii->mii_media_active |= IFM_100_TX; 35950120Swpaul else if (anlpar & ANLPAR_10_FD) 36050120Swpaul mii->mii_media_active |= IFM_10_T|IFM_FDX; 36150120Swpaul else if (anlpar & ANLPAR_10) 36250120Swpaul mii->mii_media_active |= IFM_10_T; 36350120Swpaul else 36450120Swpaul mii->mii_media_active |= IFM_NONE; 36550120Swpaul return; 36650120Swpaul } 36750120Swpaul 36850120Swpaul /* 36950120Swpaul * Link partner is not capable of autonegotiation. 37050120Swpaul * We will never be in full-duplex mode if this is 37150120Swpaul * the case, so reading the PAR is OK. 37250120Swpaul */ 37350120Swpaul par = PHY_READ(sc, MII_NSPHY_PAR); 37450120Swpaul if (par & PAR_10) 37550120Swpaul mii->mii_media_active |= IFM_10_T; 37650120Swpaul else 37750120Swpaul mii->mii_media_active |= IFM_100_TX; 37850120Swpaul#if 0 37950120Swpaul if (par & PAR_FDX) 38050120Swpaul mii->mii_media_active |= IFM_FDX; 38150120Swpaul#endif 38250120Swpaul } else 38359153Ssemenu mii->mii_media_active |= mii_media_from_bmcr(bmcr); 38450120Swpaul} 385