1 /* 2 * Copyright (c) 2001 Wind River Systems 3 * Copyright (c) 2001 4 * Bill Paul <wpaul@bsdi.com>. All rights reserved. 5 * Copyright (c) 1998, 1999, 2000, 2001 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 * 3. All advertising materials mentioning features or use of this software 21 * must display the following acknowledgement: 22 * This product includes software developed by Bill Paul. 23 * 4. Neither the name of the author nor the names of any co-contributors 24 * may be used to endorse or promote products derived from this software 25 * without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 30 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD 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 37 * THE POSSIBILITY OF SUCH DAMAGE. 38 */ 39 40 /* 41 * Driver for the National Semiconductor DP83891 and DP83861 42 * 10/100/1000 PHYs. 43 * Datasheet available at: http://www.national.com/ds/DP/DP83861.pdf 44 * 45 * The DP83891 is the older NatSemi gigE PHY which isn't being sold 46 * anymore. The DP83861 is its replacement, which is an 'enhanced' 47 * firmware driven component. The major difference between the 48 * two is that the 83891 can't generate interrupts, while the 49 * 83861 can. (I think it wasn't originally designed to do this, but 50 * it can now thanks to firmware updates.) The 83861 also allows 51 * access to its internal RAM via indirect register access. 52 */ 53 54 #include <sys/cdefs.h> 55 __FBSDID("$FreeBSD$"); 56 57 #include <sys/param.h> 58 #include <sys/systm.h> 59 #include <sys/kernel.h> 60 #include <sys/socket.h> 61 #include <sys/bus.h> 62 63 #include <machine/clock.h> 64 65 #include <net/if.h> 66 #include <net/if_media.h> 67 68 #include <dev/mii/mii.h> 69 #include <dev/mii/miivar.h> 70 #include "miidevs.h" 71 72 #include <dev/mii/nsgphyreg.h> 73 74 #include "miibus_if.h" 75 76 static int nsgphy_probe(device_t); 77 static int nsgphy_attach(device_t); 78 79 static device_method_t nsgphy_methods[] = { 80 /* device interface */ 81 DEVMETHOD(device_probe, nsgphy_probe), 82 DEVMETHOD(device_attach, nsgphy_attach), 83 DEVMETHOD(device_detach, mii_phy_detach), 84 DEVMETHOD(device_shutdown, bus_generic_shutdown), 85 { 0, 0 } 86 }; 87 88 static devclass_t nsgphy_devclass; 89 90 static driver_t nsgphy_driver = { 91 "nsgphy", 92 nsgphy_methods, 93 sizeof(struct mii_softc) 94 }; 95 96 97 DRIVER_MODULE(nsgphy, miibus, nsgphy_driver, nsgphy_devclass, 0, 0); 98 99 static int nsgphy_service(struct mii_softc *, struct mii_data *,int); 100 static void nsgphy_status(struct mii_softc *); 101 102 const struct mii_phydesc gphyters[] = { 103 { MII_OUI_NATSEMI, MII_MODEL_NATSEMI_DP83861, 104 MII_STR_NATSEMI_DP83861 }, 105 106 { MII_OUI_NATSEMI, MII_MODEL_NATSEMI_DP83891, 107 MII_STR_NATSEMI_DP83891 }, 108 109 { 0, 0, 110 NULL }, 111 }; 112 113 static int 114 nsgphy_probe(device_t dev) 115 { 116 struct mii_attach_args *ma; 117 const struct mii_phydesc *mpd; 118 119 ma = device_get_ivars(dev); 120 mpd = mii_phy_match(ma, gphyters); 121 if (mpd != NULL) { 122 device_set_desc(dev, mpd->mpd_name); 123 return(0); 124 } 125 126 return(ENXIO); 127 } 128 129 static int 130 nsgphy_attach(device_t dev) 131 { 132 struct mii_softc *sc; 133 struct mii_attach_args *ma; 134 struct mii_data *mii; 135 136 sc = device_get_softc(dev); 137 ma = device_get_ivars(dev); 138 if (bootverbose) 139 device_printf(dev, "<rev. %d>\n", MII_REV(ma->mii_id2)); 140 device_printf(dev, " "); 141 sc->mii_dev = device_get_parent(dev); 142 mii = device_get_softc(sc->mii_dev); 143 LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list); 144 145 sc->mii_inst = mii->mii_instance; 146 sc->mii_phy = ma->mii_phyno; 147 sc->mii_service = nsgphy_service; 148 sc->mii_pdata = mii; 149 sc->mii_anegticks = 5; 150 151 mii->mii_instance++; 152 153 sc->mii_capabilities = (PHY_READ(sc, MII_BMSR) | 154 (BMSR_10TFDX|BMSR_10THDX)) & ma->mii_capmask; 155 if (sc->mii_capabilities & BMSR_EXTSTAT) 156 sc->mii_extcapabilities = PHY_READ(sc, MII_EXTSR); 157 158 mii_phy_add_media(sc); 159 printf("\n"); 160 161 MIIBUS_MEDIAINIT(sc->mii_dev); 162 return(0); 163 } 164 165 static int 166 nsgphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd) 167 { 168 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 169 int reg; 170 171 switch (cmd) { 172 case MII_POLLSTAT: 173 /* 174 * If we're not polling our PHY instance, just return. 175 */ 176 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 177 return (0); 178 break; 179 180 case MII_MEDIACHG: 181 /* 182 * If the media indicates a different PHY instance, 183 * isolate ourselves. 184 */ 185 if (IFM_INST(ife->ifm_media) != sc->mii_inst) { 186 reg = PHY_READ(sc, MII_BMCR); 187 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO); 188 return (0); 189 } 190 191 /* 192 * If the interface is not up, don't do anything. 193 */ 194 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 195 break; 196 197 mii_phy_setmedia(sc); 198 break; 199 200 case MII_TICK: 201 /* 202 * If we're not currently selected, just return. 203 */ 204 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 205 return (0); 206 207 if (mii_phy_tick(sc) == EJUSTRETURN) 208 return (0); 209 break; 210 } 211 212 /* Update the media status. */ 213 nsgphy_status(sc); 214 215 /* Callback if something changed. */ 216 mii_phy_update(sc, cmd); 217 return (0); 218 } 219 220 static void 221 nsgphy_status(struct mii_softc *sc) 222 { 223 struct mii_data *mii = sc->mii_pdata; 224 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 225 int bmsr, bmcr, physup, gtsr; 226 227 mii->mii_media_status = IFM_AVALID; 228 mii->mii_media_active = IFM_ETHER; 229 230 bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR); 231 232 physup = PHY_READ(sc, NSGPHY_MII_PHYSUP); 233 234 if (physup & PHY_SUP_LINK) 235 mii->mii_media_status |= IFM_ACTIVE; 236 237 bmcr = PHY_READ(sc, MII_BMCR); 238 if (bmcr & BMCR_ISO) { 239 mii->mii_media_active |= IFM_NONE; 240 mii->mii_media_status = 0; 241 return; 242 } 243 244 if (bmcr & BMCR_LOOP) 245 mii->mii_media_active |= IFM_LOOP; 246 247 if (bmcr & BMCR_AUTOEN) { 248 /* 249 * The media status bits are only valid if autonegotiation 250 * has completed (or it's disabled). 251 */ 252 if ((bmsr & BMSR_ACOMP) == 0) { 253 /* Erg, still trying, I guess... */ 254 mii->mii_media_active |= IFM_NONE; 255 return; 256 } 257 258 switch (physup & (PHY_SUP_SPEED1|PHY_SUP_SPEED0)) { 259 case PHY_SUP_SPEED1: 260 mii->mii_media_active |= IFM_1000_T; 261 gtsr = PHY_READ(sc, MII_100T2SR); 262 if (gtsr & GTSR_MS_RES) 263 mii->mii_media_active |= IFM_ETH_MASTER; 264 break; 265 266 case PHY_SUP_SPEED0: 267 mii->mii_media_active |= IFM_100_TX; 268 break; 269 270 case 0: 271 mii->mii_media_active |= IFM_10_T; 272 break; 273 274 default: 275 mii->mii_media_active |= IFM_NONE; 276 mii->mii_media_status = 0; 277 } 278 if (physup & PHY_SUP_DUPLEX) 279 mii->mii_media_active |= IFM_FDX; 280 } else 281 mii->mii_media_active = ife->ifm_media; 282 } 283