1 /* $NetBSD: amhphy.c,v 1.4 2002/03/25 20:51:24 thorpej Exp $ */ 2 3 /* 4 * Copyright 2001 Wasabi Systems, Inc. 5 * All rights reserved. 6 * 7 * Written by Jason R. Thorpe for Wasabi Systems, Inc. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed for the NetBSD Project by 20 * Wasabi Systems, Inc. 21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse 22 * or promote products derived from this software without specific prior 23 * written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC 29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 35 * POSSIBILITY OF SUCH DAMAGE. 36 */ 37 38 /* 39 * Driver for the 10BASE-T portion of the AMD Am79c901 PHY. 40 */ 41 42 #include <sys/cdefs.h> 43 __KERNEL_RCSID(0, "$NetBSD: amhphy.c,v 1.4 2002/03/25 20:51:24 thorpej Exp $"); 44 45 #include <sys/param.h> 46 #include <sys/systm.h> 47 #include <sys/kernel.h> 48 #include <sys/device.h> 49 #include <sys/malloc.h> 50 #include <sys/socket.h> 51 #include <sys/errno.h> 52 53 #include <net/if.h> 54 #include <net/if_media.h> 55 56 #include <dev/mii/mii.h> 57 #include <dev/mii/miivar.h> 58 #include <dev/mii/miidevs.h> 59 60 #include <dev/mii/amhphyreg.h> 61 62 int amhphymatch(struct device *, struct cfdata *, void *); 63 void amhphyattach(struct device *, struct device *, void *); 64 65 struct cfattach amhphy_ca = { 66 sizeof(struct mii_softc), amhphymatch, amhphyattach, 67 mii_phy_detach, mii_phy_activate 68 }; 69 70 int amhphy_service(struct mii_softc *, struct mii_data *, int); 71 void amhphy_status(struct mii_softc *); 72 73 const struct mii_phy_funcs amhphy_funcs = { 74 amhphy_service, amhphy_status, mii_phy_reset, 75 }; 76 77 const struct mii_phydesc amhphys[] = { 78 { MII_OUI_yyAMD, MII_MODEL_yyAMD_79c901, 79 MII_STR_yyAMD_79c901 }, 80 81 { 0, 0, 82 NULL }, 83 }; 84 85 int 86 amhphymatch(struct device *parent, struct cfdata *match, void *aux) 87 { 88 struct mii_attach_args *ma = aux; 89 90 if (mii_phy_match(ma, amhphys) != NULL) 91 return (10); 92 93 return (0); 94 } 95 96 void 97 amhphyattach(struct device *parent, struct device *self, void *aux) 98 { 99 struct mii_softc *sc = (struct mii_softc *)self; 100 struct mii_attach_args *ma = aux; 101 struct mii_data *mii = ma->mii_data; 102 const struct mii_phydesc *mpd; 103 104 mpd = mii_phy_match(ma, amhphys); 105 printf(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2)); 106 107 sc->mii_inst = mii->mii_instance; 108 sc->mii_phy = ma->mii_phyno; 109 sc->mii_funcs = &amhphy_funcs; 110 sc->mii_pdata = mii; 111 sc->mii_flags = ma->mii_flags; 112 sc->mii_anegticks = 5; 113 114 PHY_RESET(sc); 115 116 sc->mii_capabilities = 117 PHY_READ(sc, MII_BMSR) & ma->mii_capmask; 118 printf("%s: ", sc->mii_dev.dv_xname); 119 if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0) 120 printf("no media present"); 121 else 122 mii_phy_add_media(sc); 123 printf("\n"); 124 } 125 126 int 127 amhphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd) 128 { 129 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 130 int reg; 131 132 switch (cmd) { 133 case MII_POLLSTAT: 134 /* 135 * If we're not polling our PHY instance, just return. 136 */ 137 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 138 return (0); 139 break; 140 141 case MII_MEDIACHG: 142 /* 143 * If the media indicates a different PHY instance, 144 * isolate ourselves. 145 */ 146 if (IFM_INST(ife->ifm_media) != sc->mii_inst) { 147 reg = PHY_READ(sc, MII_BMCR); 148 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO); 149 return (0); 150 } 151 152 /* 153 * If the interface is not up, don't do anything. 154 */ 155 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 156 break; 157 158 mii_phy_setmedia(sc); 159 break; 160 161 case MII_TICK: 162 /* 163 * If we're not currently selected, just return. 164 */ 165 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 166 return (0); 167 168 if (mii_phy_tick(sc) == EJUSTRETURN) 169 return (0); 170 break; 171 172 case MII_DOWN: 173 mii_phy_down(sc); 174 return (0); 175 } 176 177 /* Update the media status. */ 178 mii_phy_status(sc); 179 180 /* Callback if something changed. */ 181 mii_phy_update(sc, cmd); 182 return (0); 183 } 184 185 void 186 amhphy_status(struct mii_softc *sc) 187 { 188 struct mii_data *mii = sc->mii_pdata; 189 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 190 int bmsr, bmcr, ssr; 191 192 mii->mii_media_status = IFM_AVALID; 193 mii->mii_media_active = IFM_ETHER; 194 195 bmsr = PHY_READ(sc, MII_BMSR); 196 ssr = PHY_READ(sc, MII_AMHPHY_SSR); 197 198 if (ssr & SSR_LS) 199 mii->mii_media_status |= IFM_ACTIVE; 200 201 bmcr = PHY_READ(sc, MII_BMCR); 202 if (bmcr & BMCR_ISO) { 203 mii->mii_media_active |= IFM_NONE; 204 mii->mii_media_status = 0; 205 return; 206 } 207 208 if (bmcr & BMCR_LOOP) 209 mii->mii_media_active |= IFM_LOOP; 210 211 if (bmcr & BMCR_AUTOEN) { 212 /* 213 * The media status bits are only valid of autonegotiation 214 * has completed (or it's disabled). 215 */ 216 if ((bmsr & BMSR_ACOMP) == 0) { 217 /* Erg, still trying, I guess... */ 218 mii->mii_media_active |= IFM_NONE; 219 return; 220 } 221 222 if (ssr & SSR_S) { 223 /* 224 * This should't really ever happen, since it's 225 * a 10BASE-T only PHY. But the bit exists, 226 * according to the documentation, so we pay 227 * attention to it. 228 */ 229 mii->mii_media_active |= IFM_100_TX; 230 } else 231 mii->mii_media_active |= IFM_10_T; 232 if (ssr & SSR_FD) 233 mii->mii_media_active |= IFM_FDX; 234 } else 235 mii->mii_media_active = ife->ifm_media; 236 } 237