1 /* $NetBSD: gphyter.c,v 1.27 2009/10/19 18:41:13 bouyer Exp $ */ 2 3 /*- 4 * Copyright (c) 1998, 1999, 2000, 2001 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 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 /* 34 * Copyright (c) 1997 Manuel Bouyer. All rights reserved. 35 * 36 * Redistribution and use in source and binary forms, with or without 37 * modification, are permitted provided that the following conditions 38 * are met: 39 * 1. Redistributions of source code must retain the above copyright 40 * notice, this list of conditions and the following disclaimer. 41 * 2. Redistributions in binary form must reproduce the above copyright 42 * notice, this list of conditions and the following disclaimer in the 43 * documentation and/or other materials provided with the distribution. 44 * 45 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 46 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 47 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 48 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 49 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 50 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 51 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 52 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 53 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 54 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 55 */ 56 57 /* 58 * driver for National Semiconductor's DP83891 and DP83861 `Gig PHYTER' 59 * ethernet 10/100/1000 PHYs. The DP83891 is an older, non-firmware- 60 * driven version of the DP83861. 61 * 62 * Data Sheet available from www.national.com 63 */ 64 65 #include <sys/cdefs.h> 66 __KERNEL_RCSID(0, "$NetBSD: gphyter.c,v 1.27 2009/10/19 18:41:13 bouyer Exp $"); 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/gphyterreg.h> 83 84 static int gphytermatch(device_t, cfdata_t, void *); 85 static void gphyterattach(device_t, device_t, void *); 86 87 CFATTACH_DECL_NEW(gphyter, sizeof(struct mii_softc), 88 gphytermatch, gphyterattach, mii_phy_detach, mii_phy_activate); 89 90 static int gphyter_service(struct mii_softc *, struct mii_data *, int); 91 static void gphyter_status(struct mii_softc *); 92 static void gphyter_reset(struct mii_softc *); 93 94 static const struct mii_phy_funcs gphyter_funcs = { 95 gphyter_service, gphyter_status, gphyter_reset, 96 }; 97 98 static const struct mii_phydesc gphyters[] = { 99 { MII_OUI_xxNATSEMI, MII_MODEL_xxNATSEMI_DP83861, 100 MII_STR_xxNATSEMI_DP83861 }, 101 102 { MII_OUI_xxNATSEMI, MII_MODEL_xxNATSEMI_DP83891, 103 MII_STR_xxNATSEMI_DP83891 }, 104 105 { 0, 0, 106 NULL }, 107 }; 108 109 static int 110 gphytermatch(device_t parent, cfdata_t match, void *aux) 111 { 112 struct mii_attach_args *ma = aux; 113 114 if (mii_phy_match(ma, gphyters) != NULL) 115 return (10); 116 117 return (0); 118 } 119 120 static void 121 gphyterattach(device_t parent, device_t self, void *aux) 122 { 123 struct mii_softc *sc = device_private(self); 124 struct mii_attach_args *ma = aux; 125 struct mii_data *mii = ma->mii_data; 126 const struct mii_phydesc *mpd; 127 int anar, strap; 128 129 mpd = mii_phy_match(ma, gphyters); 130 aprint_naive(": Media interface\n"); 131 aprint_normal(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2)); 132 133 sc->mii_dev = self; 134 sc->mii_inst = mii->mii_instance; 135 sc->mii_phy = ma->mii_phyno; 136 sc->mii_funcs = &gphyter_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_EXTSTAT) 146 sc->mii_extcapabilities = PHY_READ(sc, MII_EXTSR); 147 148 /* 149 * The Gig PHYTER seems to have the 10baseT BMSR bits 150 * hard-wired to 0, even though the device supports 151 * 10baseT. What we do instead is read the post-reset 152 * ANAR, who's 10baseT-related bits are set by strapping 153 * pin 180, and fake the BMSR bits. 154 */ 155 anar = PHY_READ(sc, MII_ANAR); 156 if (anar & ANAR_10) 157 sc->mii_capabilities |= (BMSR_10THDX & ma->mii_capmask); 158 if (anar & ANAR_10_FD) 159 sc->mii_capabilities |= (BMSR_10TFDX & ma->mii_capmask); 160 161 aprint_normal_dev(self, ""); 162 if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0 && 163 (sc->mii_extcapabilities & EXTSR_MEDIAMASK) == 0) 164 aprint_error("no media present"); 165 else 166 mii_phy_add_media(sc); 167 aprint_normal("\n"); 168 169 strap = PHY_READ(sc, MII_GPHYTER_STRAP); 170 aprint_normal_dev(self, "strapped to %s mode", 171 (strap & STRAP_MS_VAL) ? "master" : "slave"); 172 if (strap & STRAP_NC_MODE) 173 aprint_normal(", pre-C5 BCM5400 compat enabled"); 174 aprint_normal("\n"); 175 } 176 177 static int 178 gphyter_service(struct mii_softc *sc, struct mii_data *mii, int cmd) 179 { 180 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 181 int reg; 182 183 switch (cmd) { 184 case MII_POLLSTAT: 185 /* 186 * If we're not polling our PHY instance, just return. 187 */ 188 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 189 return (0); 190 break; 191 192 case MII_MEDIACHG: 193 /* 194 * If the media indicates a different PHY instance, 195 * isolate ourselves. 196 */ 197 if (IFM_INST(ife->ifm_media) != sc->mii_inst) { 198 reg = PHY_READ(sc, MII_BMCR); 199 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO); 200 return (0); 201 } 202 203 /* 204 * If the interface is not up, don't do anything. 205 */ 206 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 207 break; 208 209 mii_phy_setmedia(sc); 210 break; 211 212 case MII_TICK: 213 /* 214 * If we're not currently selected, just return. 215 */ 216 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 217 return (0); 218 219 if (mii_phy_tick(sc) == EJUSTRETURN) 220 return (0); 221 break; 222 223 case MII_DOWN: 224 mii_phy_down(sc); 225 return (0); 226 } 227 228 /* Update the media status. */ 229 mii_phy_status(sc); 230 231 /* Callback if something changed. */ 232 mii_phy_update(sc, cmd); 233 return (0); 234 } 235 236 static void 237 gphyter_status(struct mii_softc *sc) 238 { 239 struct mii_data *mii = sc->mii_pdata; 240 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 241 int bmsr, bmcr, physup, gtsr; 242 243 mii->mii_media_status = IFM_AVALID; 244 mii->mii_media_active = IFM_ETHER; 245 246 bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR); 247 248 physup = PHY_READ(sc, MII_GPHYTER_PHY_SUP); 249 250 if (physup & PHY_SUP_LINK) 251 mii->mii_media_status |= IFM_ACTIVE; 252 253 bmcr = PHY_READ(sc, MII_BMCR); 254 if (bmcr & BMCR_ISO) { 255 mii->mii_media_active |= IFM_NONE; 256 mii->mii_media_status = 0; 257 return; 258 } 259 260 if (bmcr & BMCR_LOOP) 261 mii->mii_media_active |= IFM_LOOP; 262 263 if (bmcr & BMCR_AUTOEN) { 264 /* 265 * The media status bits are only valid of autonegotiation 266 * has completed (or it's disabled). 267 */ 268 if ((bmsr & BMSR_ACOMP) == 0) { 269 /* Erg, still trying, I guess... */ 270 mii->mii_media_active |= IFM_NONE; 271 return; 272 } 273 274 switch (physup & (PHY_SUP_SPEED1|PHY_SUP_SPEED0)) { 275 case PHY_SUP_SPEED1: 276 mii->mii_media_active |= IFM_1000_T; 277 gtsr = PHY_READ(sc, MII_100T2SR); 278 if (gtsr & GTSR_MS_RES) 279 mii->mii_media_active |= IFM_ETH_MASTER; 280 break; 281 282 case PHY_SUP_SPEED0: 283 mii->mii_media_active |= IFM_100_TX; 284 break; 285 286 case 0: 287 mii->mii_media_active |= IFM_10_T; 288 break; 289 290 default: 291 mii->mii_media_active |= IFM_NONE; 292 mii->mii_media_status = 0; 293 } 294 if (physup & PHY_SUP_DUPLEX) 295 mii->mii_media_active |= 296 IFM_FDX | mii_phy_flowstatus(sc); 297 } else 298 mii->mii_media_active = ife->ifm_media; 299 } 300 301 void 302 gphyter_reset(struct mii_softc *sc) 303 { 304 int reg, i; 305 306 if (sc->mii_flags & MIIF_NOISOLATE) 307 reg = BMCR_RESET; 308 else 309 reg = BMCR_RESET | BMCR_ISO; 310 PHY_WRITE(sc, MII_BMCR, reg); 311 312 /* 313 * It is best to allow a little time for the reset to settle 314 * in before we start polling the BMCR again. Notably, the 315 * DP83840A manual states that there should be a 500us delay 316 * between asserting software reset and attempting MII serial 317 * operations. Also, a DP83815 can get into a bad state on 318 * cable removal and reinsertion if we do not delay here. 319 */ 320 delay(500); 321 322 /* Wait another 100ms for it to complete. */ 323 for (i = 0; i < 100; i++) { 324 reg = PHY_READ(sc, MII_BMCR); 325 if ((reg & BMCR_RESET) == 0) 326 break; 327 delay(1000); 328 } 329 330 if (sc->mii_inst != 0 && ((sc->mii_flags & MIIF_NOISOLATE) == 0)) 331 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO); 332 } 333