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 */ 36 37 /* 38 * driver for AMD AM79c873 PHYs 39 * This driver also works for the Davicom DM9101 PHY, which appears to 40 * be an AM79c873 workalike. 41 */ 42 43 #include <sys/param.h> 44 #include <sys/systm.h> 45 #include <sys/kernel.h> 46 #include <sys/socket.h> 47 #include <sys/bus.h> 48 49 #include <net/if.h> 50 #include <net/if_media.h> 51 52 #include "mii.h" 53 #include "miivar.h" 54 #include "miidevs.h" 55 56 #include "amphyreg.h" 57 58 #include "miibus_if.h" 59 60 static int amphy_probe (device_t); 61 static int amphy_attach (device_t); 62 63 static device_method_t amphy_methods[] = { 64 /* device interface */ 65 DEVMETHOD(device_probe, amphy_probe), 66 DEVMETHOD(device_attach, amphy_attach), 67 DEVMETHOD(device_detach, ukphy_detach), 68 DEVMETHOD(device_shutdown, bus_generic_shutdown), 69 DEVMETHOD_END 70 }; 71 72 static const struct mii_phydesc amphys[] = { 73 MII_PHYDESC(xxAMD, 79C873), 74 MII_PHYDESC(xxDAVICOM, DM9101), 75 MII_PHYDESC(DAVICOM, DM9102), 76 MII_PHYDESC(DAVICOM, DM9601), 77 MII_PHYDESC_NULL 78 }; 79 80 static devclass_t amphy_devclass; 81 82 static driver_t amphy_driver = { 83 "amphy", 84 amphy_methods, 85 sizeof(struct mii_softc) 86 }; 87 88 DRIVER_MODULE(amphy, miibus, amphy_driver, amphy_devclass, NULL, NULL); 89 90 static int amphy_service(struct mii_softc *, struct mii_data *, int); 91 static void amphy_status(struct mii_softc *); 92 93 static int 94 amphy_probe(device_t dev) 95 { 96 struct mii_attach_args *ma = device_get_ivars(dev); 97 const struct mii_phydesc *mpd; 98 99 mpd = mii_phy_match(ma, amphys); 100 if (mpd != NULL) { 101 device_set_desc(dev, mpd->mpd_name); 102 return (0); 103 } 104 return (ENXIO); 105 } 106 107 static int 108 amphy_attach(device_t dev) 109 { 110 struct mii_softc *sc; 111 struct mii_attach_args *ma; 112 struct mii_data *mii; 113 114 sc = device_get_softc(dev); 115 ma = device_get_ivars(dev); 116 mii_softc_init(sc, ma); 117 sc->mii_dev = device_get_parent(dev); 118 mii = device_get_softc(sc->mii_dev); 119 LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list); 120 121 sc->mii_inst = mii->mii_instance; 122 sc->mii_service = amphy_service; 123 sc->mii_pdata = mii; 124 125 sc->mii_flags |= MIIF_NOISOLATE; 126 mii->mii_instance++; 127 128 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL) 129 130 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst), 131 MII_MEDIA_NONE); 132 #if 0 133 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->mii_inst), 134 MII_MEDIA_100_TX); 135 #endif 136 137 mii_phy_reset(sc); 138 139 sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & ma->mii_capmask; 140 device_printf(dev, " "); 141 if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0) 142 kprintf("no media present"); 143 else 144 mii_phy_add_media(sc); 145 kprintf("\n"); 146 #undef ADD 147 MIIBUS_MEDIAINIT(sc->mii_dev); 148 return(0); 149 } 150 151 static int 152 amphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd) 153 { 154 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 155 int reg; 156 157 switch (cmd) { 158 case MII_POLLSTAT: 159 /* 160 * If we're not polling our PHY instance, just return. 161 */ 162 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 163 return (0); 164 break; 165 166 case MII_MEDIACHG: 167 /* 168 * If the media indicates a different PHY instance, 169 * isolate ourselves. 170 */ 171 if (IFM_INST(ife->ifm_media) != sc->mii_inst) { 172 reg = PHY_READ(sc, MII_BMCR); 173 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO); 174 return (0); 175 } 176 177 /* 178 * If the interface is not up, don't do anything. 179 */ 180 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 181 break; 182 183 mii_phy_set_media(sc); 184 break; 185 186 case MII_TICK: 187 /* 188 * If we're not currently selected, just return. 189 */ 190 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 191 return (0); 192 193 if (mii_phy_tick(sc) == EJUSTRETURN) 194 return (0); 195 break; 196 } 197 198 /* Update the media status. */ 199 amphy_status(sc); 200 201 /* Callback if something changed. */ 202 mii_phy_update(sc, cmd); 203 return (0); 204 } 205 206 static void 207 amphy_status(struct mii_softc *sc) 208 { 209 struct mii_data *mii = sc->mii_pdata; 210 int bmsr, bmcr, par, anlpar; 211 212 mii->mii_media_status = IFM_AVALID; 213 mii->mii_media_active = IFM_ETHER; 214 215 bmsr = PHY_READ(sc, MII_BMSR) | 216 PHY_READ(sc, MII_BMSR); 217 if (bmsr & BMSR_LINK) 218 mii->mii_media_status |= IFM_ACTIVE; 219 220 bmcr = PHY_READ(sc, MII_BMCR); 221 if (bmcr & BMCR_ISO) { 222 mii->mii_media_active |= IFM_NONE; 223 mii->mii_media_status = 0; 224 return; 225 } 226 227 if (bmcr & BMCR_LOOP) 228 mii->mii_media_active |= IFM_LOOP; 229 230 if (bmcr & BMCR_AUTOEN) { 231 /* 232 * The PAR status bits are only valid of autonegotiation 233 * has completed (or it's disabled). 234 */ 235 if ((bmsr & BMSR_ACOMP) == 0) { 236 /* Erg, still trying, I guess... */ 237 mii->mii_media_active |= IFM_NONE; 238 return; 239 } 240 241 if (PHY_READ(sc, MII_ANER) & ANER_LPAN) { 242 anlpar = PHY_READ(sc, MII_ANAR) & 243 PHY_READ(sc, MII_ANLPAR); 244 if (anlpar & ANLPAR_T4) 245 mii->mii_media_active |= IFM_100_T4; 246 else if (anlpar & ANLPAR_TX_FD) 247 mii->mii_media_active |= IFM_100_TX|IFM_FDX; 248 else if (anlpar & ANLPAR_TX) 249 mii->mii_media_active |= IFM_100_TX; 250 else if (anlpar & ANLPAR_10_FD) 251 mii->mii_media_active |= IFM_10_T|IFM_FDX; 252 else if (anlpar & ANLPAR_10) 253 mii->mii_media_active |= IFM_10_T; 254 else 255 mii->mii_media_active |= IFM_NONE; 256 return; 257 } 258 259 /* 260 * Link partner is not capable of autonegotiation. 261 */ 262 par = PHY_READ(sc, MII_AMPHY_DSCSR); 263 if (par & DSCSR_100FDX) 264 mii->mii_media_active |= IFM_100_TX|IFM_FDX; 265 else if (par & DSCSR_100HDX) 266 mii->mii_media_active |= IFM_100_TX; 267 else if (par & DSCSR_10FDX) 268 mii->mii_media_active |= IFM_10_T|IFM_HDX; 269 else if (par & DSCSR_10HDX) 270 mii->mii_media_active |= IFM_10_T; 271 } else { 272 mii->mii_media_active = mii->mii_media.ifm_cur->ifm_media; 273 } 274 } 275