1 /* $OpenBSD: amphy.c,v 1.13 2005/05/27 08:04:15 brad Exp $ */ 2 3 /* 4 * Copyright (c) 1997, 1998, 1999 5 * Bill Paul <wpaul@ee.columbia.edu>. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by Bill Paul. 18 * 4. Neither the name of the author nor the names of any co-contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD 26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 32 * THE POSSIBILITY OF SUCH DAMAGE. 33 * 34 * $FreeBSD: src/sys/dev/mii/amphy.c,v 1.2.2.2 2002/11/08 21:53:49 semenu Exp $ 35 * $DragonFly: src/sys/dev/netif/mii_layer/amphy.c,v 1.10 2006/08/06 10:32:23 sephe Exp $ 36 */ 37 38 /* 39 * driver for AMD AM79c873 PHYs 40 * This driver also works for the Davicom DM9101 PHY, which appears to 41 * be an AM79c873 workalike. 42 */ 43 44 #include <sys/param.h> 45 #include <sys/systm.h> 46 #include <sys/kernel.h> 47 #include <sys/socket.h> 48 #include <sys/bus.h> 49 50 #include <net/if.h> 51 #include <net/if_media.h> 52 53 #include "mii.h" 54 #include "miivar.h" 55 #include "miidevs.h" 56 57 #include "amphyreg.h" 58 59 #include "miibus_if.h" 60 61 static int amphy_probe (device_t); 62 static int amphy_attach (device_t); 63 64 static device_method_t amphy_methods[] = { 65 /* device interface */ 66 DEVMETHOD(device_probe, amphy_probe), 67 DEVMETHOD(device_attach, amphy_attach), 68 DEVMETHOD(device_detach, ukphy_detach), 69 DEVMETHOD(device_shutdown, bus_generic_shutdown), 70 { 0, 0 } 71 }; 72 73 static const struct mii_phydesc amphys[] = { 74 MII_PHYDESC(xxAMD, 79C873), 75 MII_PHYDESC(xxDAVICOM, DM9101), 76 MII_PHYDESC(DAVICOM, DM9102), 77 MII_PHYDESC(DAVICOM, DM9601), 78 MII_PHYDESC_NULL 79 }; 80 81 static devclass_t amphy_devclass; 82 83 static driver_t amphy_driver = { 84 "amphy", 85 amphy_methods, 86 sizeof(struct mii_softc) 87 }; 88 89 DRIVER_MODULE(amphy, miibus, amphy_driver, amphy_devclass, 0, 0); 90 91 static int amphy_service(struct mii_softc *, struct mii_data *, int); 92 static void amphy_status(struct mii_softc *); 93 94 static int 95 amphy_probe(device_t dev) 96 { 97 struct mii_attach_args *ma = device_get_ivars(dev); 98 const struct mii_phydesc *mpd; 99 100 mpd = mii_phy_match(ma, amphys); 101 if (mpd != NULL) { 102 device_set_desc(dev, mpd->mpd_name); 103 return (0); 104 } 105 return (ENXIO); 106 } 107 108 static int 109 amphy_attach(device_t dev) 110 { 111 struct mii_softc *sc; 112 struct mii_attach_args *ma; 113 struct mii_data *mii; 114 115 sc = device_get_softc(dev); 116 ma = device_get_ivars(dev); 117 mii_softc_init(sc, ma); 118 sc->mii_dev = device_get_parent(dev); 119 mii = device_get_softc(sc->mii_dev); 120 LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list); 121 122 sc->mii_inst = mii->mii_instance; 123 sc->mii_service = amphy_service; 124 sc->mii_pdata = mii; 125 126 sc->mii_flags |= MIIF_NOISOLATE; 127 mii->mii_instance++; 128 129 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL) 130 131 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst), 132 MII_MEDIA_NONE); 133 #if 0 134 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->mii_inst), 135 MII_MEDIA_100_TX); 136 #endif 137 138 mii_phy_reset(sc); 139 140 sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & ma->mii_capmask; 141 device_printf(dev, " "); 142 if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0) 143 printf("no media present"); 144 else 145 mii_phy_add_media(sc); 146 printf("\n"); 147 #undef ADD 148 MIIBUS_MEDIAINIT(sc->mii_dev); 149 return(0); 150 } 151 152 static int 153 amphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd) 154 { 155 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 156 int reg; 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 mii_phy_set_media(sc); 185 break; 186 187 case MII_TICK: 188 /* 189 * If we're not currently selected, just return. 190 */ 191 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 192 return (0); 193 194 if (mii_phy_tick(sc) == EJUSTRETURN) 195 return (0); 196 break; 197 } 198 199 /* Update the media status. */ 200 amphy_status(sc); 201 202 /* Callback if something changed. */ 203 mii_phy_update(sc, cmd); 204 return (0); 205 } 206 207 static void 208 amphy_status(struct mii_softc *sc) 209 { 210 struct mii_data *mii = sc->mii_pdata; 211 int bmsr, bmcr, par, anlpar; 212 213 mii->mii_media_status = IFM_AVALID; 214 mii->mii_media_active = IFM_ETHER; 215 216 bmsr = PHY_READ(sc, MII_BMSR) | 217 PHY_READ(sc, MII_BMSR); 218 if (bmsr & BMSR_LINK) 219 mii->mii_media_status |= IFM_ACTIVE; 220 221 bmcr = PHY_READ(sc, MII_BMCR); 222 if (bmcr & BMCR_ISO) { 223 mii->mii_media_active |= IFM_NONE; 224 mii->mii_media_status = 0; 225 return; 226 } 227 228 if (bmcr & BMCR_LOOP) 229 mii->mii_media_active |= IFM_LOOP; 230 231 if (bmcr & BMCR_AUTOEN) { 232 /* 233 * The PAR status bits are only valid of autonegotiation 234 * has completed (or it's disabled). 235 */ 236 if ((bmsr & BMSR_ACOMP) == 0) { 237 /* Erg, still trying, I guess... */ 238 mii->mii_media_active |= IFM_NONE; 239 return; 240 } 241 242 if (PHY_READ(sc, MII_ANER) & ANER_LPAN) { 243 anlpar = PHY_READ(sc, MII_ANAR) & 244 PHY_READ(sc, MII_ANLPAR); 245 if (anlpar & ANLPAR_T4) 246 mii->mii_media_active |= IFM_100_T4; 247 else if (anlpar & ANLPAR_TX_FD) 248 mii->mii_media_active |= IFM_100_TX|IFM_FDX; 249 else if (anlpar & ANLPAR_TX) 250 mii->mii_media_active |= IFM_100_TX; 251 else if (anlpar & ANLPAR_10_FD) 252 mii->mii_media_active |= IFM_10_T|IFM_FDX; 253 else if (anlpar & ANLPAR_10) 254 mii->mii_media_active |= IFM_10_T; 255 else 256 mii->mii_media_active |= IFM_NONE; 257 return; 258 } 259 260 /* 261 * Link partner is not capable of autonegotiation. 262 */ 263 par = PHY_READ(sc, MII_AMPHY_DSCSR); 264 if (par & DSCSR_100FDX) 265 mii->mii_media_active |= IFM_100_TX|IFM_FDX; 266 else if (par & DSCSR_100HDX) 267 mii->mii_media_active |= IFM_100_TX; 268 else if (par & DSCSR_10FDX) 269 mii->mii_media_active |= IFM_10_T|IFM_HDX; 270 else if (par & DSCSR_10HDX) 271 mii->mii_media_active |= IFM_10_T; 272 } else { 273 mii->mii_media_active = mii->mii_media.ifm_cur->ifm_media; 274 } 275 } 276