1 /* $NetBSD: nsphy.c,v 1.18 1999/07/14 23:57:36 thorpej Exp $ */ 2 3 /*- 4 * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 9 * NASA Ames Research Center. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the NetBSD 22 * Foundation, Inc. and its contributors. 23 * 4. Neither the name of The NetBSD Foundation nor the names of its 24 * contributors may be used to endorse or promote products derived 25 * from this software without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 37 * POSSIBILITY OF SUCH DAMAGE. 38 * 39 * $FreeBSD: src/sys/dev/mii/nsphy.c,v 1.2.2.5 2001/06/08 19:58:33 semenu Exp $ 40 */ 41 42 /* 43 * Copyright (c) 1997 Manuel Bouyer. All rights reserved. 44 * 45 * Redistribution and use in source and binary forms, with or without 46 * modification, are permitted provided that the following conditions 47 * are met: 48 * 1. Redistributions of source code must retain the above copyright 49 * notice, this list of conditions and the following disclaimer. 50 * 2. Redistributions in binary form must reproduce the above copyright 51 * notice, this list of conditions and the following disclaimer in the 52 * documentation and/or other materials provided with the distribution. 53 * 3. All advertising materials mentioning features or use of this software 54 * must display the following acknowledgement: 55 * This product includes software developed by Manuel Bouyer. 56 * 4. The name of the author may not be used to endorse or promote products 57 * derived from this software without specific prior written permission. 58 * 59 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 60 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 61 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 62 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 63 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 64 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 65 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 66 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 67 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 68 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 69 */ 70 71 /* 72 * driver for National Semiconductor's DP83840A ethernet 10/100 PHY 73 * Data Sheet available from www.national.com 74 */ 75 76 #include <sys/param.h> 77 #include <sys/systm.h> 78 #include <sys/kernel.h> 79 #include <sys/socket.h> 80 #include <sys/errno.h> 81 #include <sys/module.h> 82 #include <sys/bus.h> 83 84 #include <net/if.h> 85 #include <net/if_media.h> 86 87 #include "mii.h" 88 #include "miivar.h" 89 #include "miidevs.h" 90 91 #include "nsphyreg.h" 92 93 #include "miibus_if.h" 94 95 static int nsphy_probe (device_t); 96 static int nsphy_attach (device_t); 97 98 static device_method_t nsphy_methods[] = { 99 /* device interface */ 100 DEVMETHOD(device_probe, nsphy_probe), 101 DEVMETHOD(device_attach, nsphy_attach), 102 DEVMETHOD(device_detach, ukphy_detach), 103 DEVMETHOD(device_shutdown, bus_generic_shutdown), 104 DEVMETHOD_END 105 }; 106 107 static const struct mii_phydesc nsphys[] = { 108 MII_PHYDESC(NATSEMI, DP83840), 109 MII_PHYDESC_NULL 110 }; 111 112 static devclass_t nsphy_devclass; 113 114 static driver_t nsphy_driver = { 115 "nsphy", 116 nsphy_methods, 117 sizeof(struct mii_softc) 118 }; 119 120 DRIVER_MODULE(nsphy, miibus, nsphy_driver, nsphy_devclass, NULL, NULL); 121 122 static int nsphy_service(struct mii_softc *, struct mii_data *, int); 123 static void nsphy_status(struct mii_softc *); 124 static void nsphy_reset(struct mii_softc *); 125 126 static int 127 nsphy_probe(device_t dev) 128 { 129 struct mii_attach_args *ma = device_get_ivars(dev); 130 const struct mii_phydesc *mpd; 131 132 mpd = mii_phy_match(ma, nsphys); 133 if (mpd != NULL) { 134 device_set_desc(dev, mpd->mpd_name); 135 return (0); 136 } 137 return (ENXIO); 138 } 139 140 static int 141 nsphy_attach(device_t dev) 142 { 143 struct mii_softc *sc; 144 struct mii_attach_args *ma; 145 struct mii_data *mii; 146 147 sc = device_get_softc(dev); 148 ma = device_get_ivars(dev); 149 mii_softc_init(sc, ma); 150 sc->mii_dev = device_get_parent(dev); 151 mii = device_get_softc(sc->mii_dev); 152 LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list); 153 154 sc->mii_inst = mii->mii_instance; 155 sc->mii_service = nsphy_service; 156 sc->mii_reset = nsphy_reset; 157 sc->mii_pdata = mii; 158 159 #ifdef foo 160 /* 161 * i82557 wedges if all of its PHYs are isolated! 162 */ 163 if (strcmp(device_get_name(device_get_parent(sc->mii_dev)), 164 "fxp") == 0 && mii->mii_instance == 0) 165 #endif 166 167 sc->mii_flags |= MIIF_NOISOLATE; 168 mii->mii_instance++; 169 170 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL) 171 172 #if 0 173 /* Can't do this on the i82557! */ 174 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst), 175 BMCR_ISO); 176 #endif 177 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->mii_inst), 178 MII_MEDIA_100_TX); 179 180 nsphy_reset(sc); 181 182 sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & ma->mii_capmask; 183 device_printf(dev, " "); 184 if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0) 185 kprintf("no media present"); 186 else 187 mii_phy_add_media(sc); 188 kprintf("\n"); 189 #undef ADD 190 191 MIIBUS_MEDIAINIT(sc->mii_dev); 192 return(0); 193 } 194 195 static int 196 nsphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd) 197 { 198 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 199 int reg; 200 201 switch (cmd) { 202 case MII_POLLSTAT: 203 /* 204 * If we're not polling our PHY instance, just return. 205 */ 206 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 207 return (0); 208 break; 209 210 case MII_MEDIACHG: 211 /* 212 * If the media indicates a different PHY instance, 213 * isolate ourselves. 214 */ 215 if (IFM_INST(ife->ifm_media) != sc->mii_inst) { 216 reg = PHY_READ(sc, MII_BMCR); 217 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO); 218 return (0); 219 } 220 221 /* 222 * If the interface is not up, don't do anything. 223 */ 224 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 225 break; 226 227 reg = PHY_READ(sc, MII_NSPHY_PCR); 228 229 /* 230 * Set up the PCR to use LED4 to indicate full-duplex 231 * in both 10baseT and 100baseTX modes. 232 */ 233 reg |= PCR_LED4MODE; 234 235 /* 236 * Make sure Carrier Intgrity Monitor function is 237 * disabled (normal for Node operation, but sometimes 238 * it's not set?!) 239 */ 240 reg |= PCR_CIMDIS; 241 242 /* 243 * Make sure "force link good" is set to normal mode. 244 * It's only intended for debugging. 245 */ 246 reg |= PCR_FLINK100; 247 248 /* 249 * Mystery bits which are supposedly `reserved', 250 * but we seem to need to set them when the PHY 251 * is connected to some interfaces: 252 * 253 * 0x0400 is needed for fxp 254 * (Intel EtherExpress Pro 10+/100B, 82557 chip) 255 * (nsphy with a DP83840 chip) 256 * 0x0100 may be needed for some other card 257 */ 258 reg |= PCR_CONGCTRL | PCR_TXREADYSEL; 259 260 if (strcmp(device_get_name(device_get_parent(sc->mii_dev)), 261 "fxp") == 0) 262 PHY_WRITE(sc, MII_NSPHY_PCR, reg); 263 264 mii_phy_set_media(sc); 265 break; 266 267 case MII_TICK: 268 /* 269 * If we're not currently selected, just return. 270 */ 271 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 272 return (0); 273 274 if (mii_phy_tick(sc) == EJUSTRETURN) 275 return (0); 276 break; 277 } 278 279 /* Update the media status. */ 280 nsphy_status(sc); 281 282 /* Callback if something changed. */ 283 mii_phy_update(sc, cmd); 284 return (0); 285 } 286 287 static void 288 nsphy_status(struct mii_softc *sc) 289 { 290 struct mii_data *mii = sc->mii_pdata; 291 int bmsr, bmcr, par, anlpar; 292 293 mii->mii_media_status = IFM_AVALID; 294 mii->mii_media_active = IFM_ETHER; 295 296 bmsr = PHY_READ(sc, MII_BMSR) | 297 PHY_READ(sc, MII_BMSR); 298 if (bmsr & BMSR_LINK) 299 mii->mii_media_status |= IFM_ACTIVE; 300 301 bmcr = PHY_READ(sc, MII_BMCR); 302 if (bmcr & BMCR_ISO) { 303 mii->mii_media_active |= IFM_NONE; 304 mii->mii_media_status = 0; 305 return; 306 } 307 308 if (bmcr & BMCR_LOOP) 309 mii->mii_media_active |= IFM_LOOP; 310 311 if (bmcr & BMCR_AUTOEN) { 312 /* 313 * The PAR status bits are only valid of autonegotiation 314 * has completed (or it's disabled). 315 */ 316 if ((bmsr & BMSR_ACOMP) == 0) { 317 /* Erg, still trying, I guess... */ 318 mii->mii_media_active |= IFM_NONE; 319 return; 320 } 321 322 /* 323 * Argh. The PAR doesn't seem to indicate duplex mode 324 * properly! Determine media based on link partner's 325 * advertised capabilities. 326 */ 327 if (PHY_READ(sc, MII_ANER) & ANER_LPAN) { 328 anlpar = PHY_READ(sc, MII_ANAR) & 329 PHY_READ(sc, MII_ANLPAR); 330 if (anlpar & ANLPAR_T4) 331 mii->mii_media_active |= IFM_100_T4; 332 else if (anlpar & ANLPAR_TX_FD) 333 mii->mii_media_active |= IFM_100_TX|IFM_FDX; 334 else if (anlpar & ANLPAR_TX) 335 mii->mii_media_active |= IFM_100_TX; 336 else if (anlpar & ANLPAR_10_FD) 337 mii->mii_media_active |= IFM_10_T|IFM_FDX; 338 else if (anlpar & ANLPAR_10) 339 mii->mii_media_active |= IFM_10_T; 340 else 341 mii->mii_media_active |= IFM_NONE; 342 return; 343 } 344 345 /* 346 * Link partner is not capable of autonegotiation. 347 * We will never be in full-duplex mode if this is 348 * the case, so reading the PAR is OK. 349 */ 350 par = PHY_READ(sc, MII_NSPHY_PAR); 351 if (par & PAR_10) 352 mii->mii_media_active |= IFM_10_T; 353 else 354 mii->mii_media_active |= IFM_100_TX; 355 #if 0 356 if (par & PAR_FDX) 357 mii->mii_media_active |= IFM_FDX; 358 #endif 359 } else 360 mii->mii_media_active |= mii->mii_media.ifm_cur->ifm_media; 361 } 362 363 static void 364 nsphy_reset(struct mii_softc *sc) 365 { 366 int anar; 367 368 mii_phy_reset(sc); 369 anar = PHY_READ(sc, MII_ANAR); 370 anar |= BMSR_MEDIA_TO_ANAR(PHY_READ(sc, MII_BMSR)); 371 PHY_WRITE(sc, MII_ANAR, anar); 372 } 373