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