nsphyter.c revision 213364
1226031Sstas/* $NetBSD: nsphyter.c,v 1.28 2008/01/20 07:58:19 msaitoh Exp $ */ 2226031Sstas 3226031Sstas/*- 4226031Sstas * Copyright (c) 1998, 1999, 2000, 2001 The NetBSD Foundation, Inc. 5226031Sstas * All rights reserved. 6226031Sstas * 7226031Sstas * This code is derived from software contributed to The NetBSD Foundation 8226031Sstas * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 9226031Sstas * NASA Ames Research Center. 10226031Sstas * 11226031Sstas * Redistribution and use in source and binary forms, with or without 12226031Sstas * modification, are permitted provided that the following conditions 13226031Sstas * are met: 14226031Sstas * 1. Redistributions of source code must retain the above copyright 15226031Sstas * notice, this list of conditions and the following disclaimer. 16226031Sstas * 2. Redistributions in binary form must reproduce the above copyright 17226031Sstas * notice, this list of conditions and the following disclaimer in the 18226031Sstas * documentation and/or other materials provided with the distribution. 19226031Sstas * 20226031Sstas * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21226031Sstas * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22226031Sstas * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23226031Sstas * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24226031Sstas * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25226031Sstas * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26226031Sstas * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27226031Sstas * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28226031Sstas * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29226031Sstas * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30226031Sstas * POSSIBILITY OF SUCH DAMAGE. 31226031Sstas */ 32226031Sstas 33226031Sstas/*- 34226031Sstas * Copyright (c) 1997 Manuel Bouyer. All rights reserved. 35226031Sstas * 36226031Sstas * Redistribution and use in source and binary forms, with or without 37226031Sstas * modification, are permitted provided that the following conditions 38226031Sstas * are met: 39226031Sstas * 1. Redistributions of source code must retain the above copyright 40226031Sstas * notice, this list of conditions and the following disclaimer. 41226031Sstas * 2. Redistributions in binary form must reproduce the above copyright 42226031Sstas * notice, this list of conditions and the following disclaimer in the 43226031Sstas * documentation and/or other materials provided with the distribution. 44226031Sstas * 45226031Sstas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 46226031Sstas * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 47226031Sstas * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 48226031Sstas * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 49226031Sstas * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 50226031Sstas * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 51226031Sstas * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 52226031Sstas * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 53226031Sstas * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 54226031Sstas * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 55226031Sstas */ 56226031Sstas 57226031Sstas#include <sys/cdefs.h> 58226031Sstas__FBSDID("$FreeBSD: head/sys/dev/mii/nsphyter.c 213364 2010-10-02 18:53:12Z marius $"); 59226031Sstas 60226031Sstas/* 61226031Sstas * driver for National Semiconductor's DP83843 `PHYTER' ethernet 10/100 PHY 62226031Sstas * Data Sheet available from www.national.com 63226031Sstas * 64226031Sstas * We also support the DP83815 `MacPHYTER' internal PHY since, for our 65226031Sstas * purposes, they are compatible. 66226031Sstas */ 67226031Sstas 68226031Sstas#include <sys/param.h> 69226031Sstas#include <sys/systm.h> 70226031Sstas#include <sys/bus.h> 71226031Sstas#include <sys/errno.h> 72226031Sstas#include <sys/kernel.h> 73226031Sstas#include <sys/module.h> 74226031Sstas#include <sys/socket.h> 75226031Sstas 76226031Sstas#include <net/if.h> 77226031Sstas#include <net/if_media.h> 78226031Sstas 79226031Sstas#include <dev/mii/mii.h> 80226031Sstas#include <dev/mii/miivar.h> 81226031Sstas#include "miidevs.h" 82226031Sstas 83226031Sstas#include <dev/mii/nsphyterreg.h> 84 85#include "miibus_if.h" 86 87static device_probe_t nsphyter_probe; 88static device_attach_t nsphyter_attach; 89 90static device_method_t nsphyter_methods[] = { 91 /* device interface */ 92 DEVMETHOD(device_probe, nsphyter_probe), 93 DEVMETHOD(device_attach, nsphyter_attach), 94 DEVMETHOD(device_detach, mii_phy_detach), 95 DEVMETHOD(device_shutdown, bus_generic_shutdown), 96 { 0, 0 } 97}; 98 99static devclass_t nsphyter_devclass; 100 101static driver_t nsphyter_driver = { 102 "nsphyter", 103 nsphyter_methods, 104 sizeof(struct mii_softc) 105}; 106 107DRIVER_MODULE(nsphyter, miibus, nsphyter_driver, nsphyter_devclass, 0, 0); 108 109static int nsphyter_service(struct mii_softc *, struct mii_data *, int); 110static void nsphyter_status(struct mii_softc *); 111static void nsphyter_reset(struct mii_softc *); 112 113static const struct mii_phydesc nsphys[] = { 114 MII_PHY_DESC(NATSEMI, DP83815), 115 MII_PHY_DESC(NATSEMI, DP83843), 116 MII_PHY_DESC(NATSEMI, DP83847), 117 MII_PHY_END 118}; 119 120static int 121nsphyter_probe(device_t dev) 122{ 123 124 return (mii_phy_dev_probe(dev, nsphys, BUS_PROBE_DEFAULT)); 125} 126 127static int 128nsphyter_attach(device_t dev) 129{ 130 struct mii_softc *sc; 131 struct mii_attach_args *ma; 132 struct mii_data *mii; 133 const char *nic; 134 135 sc = device_get_softc(dev); 136 ma = device_get_ivars(dev); 137 sc->mii_dev = device_get_parent(dev); 138 mii = ma->mii_data; 139 LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list); 140 141 sc->mii_inst = mii->mii_instance++; 142 sc->mii_phy = ma->mii_phyno; 143 sc->mii_service = nsphyter_service; 144 sc->mii_pdata = mii; 145 146#define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL) 147 148 nic = device_get_name(device_get_parent(sc->mii_dev)); 149 /* 150 * In order for MII loopback to work Am79C971 and greater PCnet 151 * chips additionally need to be placed into external loopback 152 * mode which pcn(4) doesn't do so far. 153 */ 154 if (strcmp(nic, "pcn") != 0) 155#if 1 156 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, 157 sc->mii_inst), MII_MEDIA_100_TX); 158#else 159 if (strcmp(nic, "pcn") == 0) 160 sc->mii_flags |= MIIF_NOLOOP; 161#endif 162 163 nsphyter_reset(sc); 164 165 sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & ma->mii_capmask; 166 device_printf(dev, " "); 167 mii_phy_add_media(sc); 168 printf("\n"); 169 170#undef ADD 171 172 MIIBUS_MEDIAINIT(sc->mii_dev); 173 return (0); 174} 175 176static int 177nsphyter_service(struct mii_softc *sc, struct mii_data *mii, int cmd) 178{ 179 180 switch (cmd) { 181 case MII_POLLSTAT: 182 break; 183 184 case MII_MEDIACHG: 185 /* 186 * If the interface is not up, don't do anything. 187 */ 188 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 189 break; 190 191 mii_phy_setmedia(sc); 192 break; 193 194 case MII_TICK: 195 if (mii_phy_tick(sc) == EJUSTRETURN) 196 return (0); 197 break; 198 } 199 200 /* Update the media status. */ 201 nsphyter_status(sc); 202 203 /* Callback if something changed. */ 204 mii_phy_update(sc, cmd); 205 return (0); 206} 207 208static void 209nsphyter_status(struct mii_softc *sc) 210{ 211 struct mii_data *mii = sc->mii_pdata; 212 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 213 int bmsr, bmcr, physts; 214 215 mii->mii_media_status = IFM_AVALID; 216 mii->mii_media_active = IFM_ETHER; 217 218 bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR); 219 physts = PHY_READ(sc, MII_NSPHYTER_PHYSTS); 220 221 if ((physts & PHYSTS_LINK) != 0) 222 mii->mii_media_status |= IFM_ACTIVE; 223 224 bmcr = PHY_READ(sc, MII_BMCR); 225 if ((bmcr & BMCR_ISO) != 0) { 226 mii->mii_media_active |= IFM_NONE; 227 mii->mii_media_status = 0; 228 return; 229 } 230 231 if ((bmcr & BMCR_LOOP) != 0) 232 mii->mii_media_active |= IFM_LOOP; 233 234 if ((bmcr & BMCR_AUTOEN) != 0) { 235 /* 236 * The media status bits are only valid if autonegotiation 237 * has completed (or it's disabled). 238 */ 239 if ((bmsr & BMSR_ACOMP) == 0) { 240 /* Erg, still trying, I guess... */ 241 mii->mii_media_active |= IFM_NONE; 242 return; 243 } 244 245 if ((physts & PHYSTS_SPEED10) != 0) 246 mii->mii_media_active |= IFM_10_T; 247 else 248 mii->mii_media_active |= IFM_100_TX; 249 if ((physts & PHYSTS_DUPLEX) != 0) 250#ifdef notyet 251 mii->mii_media_active |= 252 IFM_FDX | mii_phy_flowstatus(sc); 253#else 254 mii->mii_media_active |= IFM_FDX; 255#endif 256 else 257 mii->mii_media_active |= IFM_HDX; 258 } else 259 mii->mii_media_active = ife->ifm_media; 260} 261 262static void 263nsphyter_reset(struct mii_softc *sc) 264{ 265 struct ifmedia_entry *ife = sc->mii_pdata->mii_media.ifm_cur; 266 int reg, i; 267 268 if ((sc->mii_flags & MIIF_NOISOLATE) != 0) 269 reg = BMCR_RESET; 270 else 271 reg = BMCR_RESET | BMCR_ISO; 272 PHY_WRITE(sc, MII_BMCR, reg); 273 274 /* 275 * It is best to allow a little time for the reset to settle 276 * in before we start polling the BMCR again. Notably, the 277 * DP8384{3,7} manuals state that there should be a 500us delay 278 * between asserting software reset and attempting MII serial 279 * operations. Be conservative. Also, a DP83815 can get into 280 * a bad state on cable removal and reinsertion if we do not 281 * delay here. 282 */ 283 DELAY(1000); 284 285 /* 286 * Wait another 2s for it to complete. 287 * This is only a little overkill as under normal circumstances 288 * the PHY can take up to 1s to complete reset. 289 * This is also a bit odd because after a reset, the BMCR will 290 * clear the reset bit and simply reports 0 even though the reset 291 * is not yet complete. 292 */ 293 for (i = 0; i < 1000; i++) { 294 reg = PHY_READ(sc, MII_BMCR); 295 if (reg != 0 && (reg & BMCR_RESET) == 0) 296 break; 297 DELAY(2000); 298 } 299 300 if ((sc->mii_flags & MIIF_NOISOLATE) == 0) { 301 if ((ife == NULL && sc->mii_inst != 0) || 302 (ife != NULL && IFM_INST(ife->ifm_media) != sc->mii_inst)) 303 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO); 304 } 305} 306