1 /* $OpenBSD: amphy.c,v 1.2 2000/11/20 16:22:15 jason 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.3 2000/04/19 14:57:29 phk 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/device.h> 47 #include <sys/malloc.h> 48 #include <sys/socket.h> 49 50 #include <net/if.h> 51 #include <net/if_media.h> 52 53 #include <dev/mii/mii.h> 54 #include <dev/mii/miivar.h> 55 #include <dev/mii/miidevs.h> 56 57 #include <dev/mii/amphyreg.h> 58 59 int amphymatch __P((struct device *, void *, void *)); 60 void amphyattach __P((struct device *, struct device *, void *)); 61 62 struct cfattach amphy_ca = { 63 sizeof(struct mii_softc), amphymatch, amphyattach, mii_phy_detach, 64 mii_phy_activate 65 }; 66 67 struct cfdriver amphy_cd = { 68 NULL, "amphy", DV_DULL 69 }; 70 71 int amphy_service __P((struct mii_softc *, struct mii_data *, int)); 72 void amphy_status __P((struct mii_softc *)); 73 74 int 75 amphymatch(parent, match, aux) 76 struct device *parent; 77 void *match, *aux; 78 { 79 struct mii_attach_args *ma = aux; 80 81 if ((MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_xxAMD && 82 MII_MODEL(ma->mii_id2) == MII_MODEL_xxAMD_79C873) || 83 (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_xxDAVICOM && 84 MII_MODEL(ma->mii_id2) == MII_MODEL_xxDAVICOM_DM9101)) 85 return(10); 86 87 return(0); 88 } 89 90 void 91 amphyattach(parent, self, aux) 92 struct device *parent, *self; 93 void *aux; 94 { 95 struct mii_softc *sc = (struct mii_softc *)self; 96 struct mii_attach_args *ma = aux; 97 struct mii_data *mii = ma->mii_data; 98 99 printf(": %s, rev. %d\n", MII_STR_xxAMD_79C873, 100 MII_REV(ma->mii_id2)); 101 102 sc->mii_inst = mii->mii_instance; 103 sc->mii_phy = ma->mii_phyno; 104 sc->mii_service = amphy_service; 105 sc->mii_status = amphy_status; 106 sc->mii_pdata = mii; 107 sc->mii_flags = mii->mii_flags; 108 109 sc->mii_flags |= MIIF_NOISOLATE; 110 111 mii_phy_reset(sc); 112 113 sc->mii_capabilities = 114 PHY_READ(sc, MII_BMSR) & ma->mii_capmask; 115 if (sc->mii_capabilities & BMSR_MEDIAMASK) 116 mii_phy_add_media(sc); 117 } 118 119 int 120 amphy_service(sc, mii, cmd) 121 struct mii_softc *sc; 122 struct mii_data *mii; 123 int cmd; 124 { 125 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 126 int reg; 127 128 switch (cmd) { 129 case MII_POLLSTAT: 130 /* 131 * If we're not polling our PHY instance, just return. 132 */ 133 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 134 return (0); 135 break; 136 137 case MII_MEDIACHG: 138 /* 139 * If the media indicates a different PHY instance, 140 * isolate ourselves. 141 */ 142 if (IFM_INST(ife->ifm_media) != sc->mii_inst) { 143 reg = PHY_READ(sc, MII_BMCR); 144 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO); 145 return (0); 146 } 147 148 /* 149 * If the interface is not up, don't do anything. 150 */ 151 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 152 break; 153 154 mii_phy_setmedia(sc); 155 break; 156 157 case MII_TICK: 158 /* 159 * If we're not currently selected, just return. 160 */ 161 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 162 return (0); 163 164 if (mii_phy_tick(sc) == EJUSTRETURN) 165 return (0); 166 break; 167 case MII_DOWN: 168 mii_phy_down(sc); 169 return (0); 170 } 171 172 /* Update the media status. */ 173 amphy_status(sc); 174 175 /* Callback if something changed. */ 176 mii_phy_update(sc, cmd); 177 return (0); 178 } 179 180 void 181 amphy_status(sc) 182 struct mii_softc *sc; 183 { 184 struct mii_data *mii = sc->mii_pdata; 185 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 186 int bmsr, bmcr, par, anlpar; 187 188 mii->mii_media_status = IFM_AVALID; 189 mii->mii_media_active = IFM_ETHER; 190 191 bmsr = PHY_READ(sc, MII_BMSR) | 192 PHY_READ(sc, MII_BMSR); 193 if (bmsr & BMSR_LINK) 194 mii->mii_media_status |= IFM_ACTIVE; 195 196 bmcr = PHY_READ(sc, MII_BMCR); 197 if (bmcr & BMCR_ISO) { 198 mii->mii_media_active |= IFM_NONE; 199 mii->mii_media_status = 0; 200 return; 201 } 202 203 if (bmcr & BMCR_LOOP) 204 mii->mii_media_active |= IFM_LOOP; 205 206 if (bmcr & BMCR_AUTOEN) { 207 /* 208 * The PAR status bits are only valid of autonegotiation 209 * has completed (or it's disabled). 210 */ 211 if ((bmsr & BMSR_ACOMP) == 0) { 212 /* Erg, still trying, I guess... */ 213 mii->mii_media_active |= IFM_NONE; 214 return; 215 } 216 217 if (PHY_READ(sc, MII_ANER) & ANER_LPAN) { 218 anlpar = PHY_READ(sc, MII_ANAR) & 219 PHY_READ(sc, MII_ANLPAR); 220 if (anlpar & ANLPAR_T4) 221 mii->mii_media_active |= IFM_100_T4; 222 else if (anlpar & ANLPAR_TX_FD) 223 mii->mii_media_active |= IFM_100_TX|IFM_FDX; 224 else if (anlpar & ANLPAR_TX) 225 mii->mii_media_active |= IFM_100_TX; 226 else if (anlpar & ANLPAR_10_FD) 227 mii->mii_media_active |= IFM_10_T|IFM_FDX; 228 else if (anlpar & ANLPAR_10) 229 mii->mii_media_active |= IFM_10_T; 230 else 231 mii->mii_media_active |= IFM_NONE; 232 return; 233 } 234 235 /* 236 * Link partner is not capable of autonegotiation. 237 */ 238 par = PHY_READ(sc, MII_AMPHY_DSCSR); 239 if (par & DSCSR_100FDX) 240 mii->mii_media_active |= IFM_100_TX|IFM_FDX; 241 else if (par & DSCSR_100HDX) 242 mii->mii_media_active |= IFM_100_TX; 243 else if (par & DSCSR_10FDX) 244 mii->mii_media_active |= IFM_10_T|IFM_HDX; 245 else if (par & DSCSR_10HDX) 246 mii->mii_media_active |= IFM_10_T; 247 } else 248 mii->mii_media_active = ife->ifm_media; 249 } 250