1 /* $NetBSD: amhphy.c,v 1.26 2020/03/15 23:04:50 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.26 2020/03/15 23:04:50 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/socket.h> 50 #include <sys/errno.h> 51 52 #include <net/if.h> 53 #include <net/if_media.h> 54 55 #include <dev/mii/mii.h> 56 #include <dev/mii/miivar.h> 57 #include <dev/mii/miidevs.h> 58 59 #include <dev/mii/amhphyreg.h> 60 61 static int amhphymatch(device_t, cfdata_t, void *); 62 static void amhphyattach(device_t, device_t, void *); 63 64 CFATTACH_DECL_NEW(amhphy, sizeof(struct mii_softc), 65 amhphymatch, amhphyattach, mii_phy_detach, mii_phy_activate); 66 67 static int amhphy_service(struct mii_softc *, struct mii_data *, int); 68 static void amhphy_status(struct mii_softc *); 69 70 static const struct mii_phy_funcs amhphy_funcs = { 71 amhphy_service, amhphy_status, mii_phy_reset, 72 }; 73 74 static const struct mii_phydesc amhphys[] = { 75 MII_PHY_DESC(yyAMD, 79c901), 76 MII_PHY_END, 77 }; 78 79 static int 80 amhphymatch(device_t parent, cfdata_t match, void *aux) 81 { 82 struct mii_attach_args *ma = aux; 83 84 if (mii_phy_match(ma, amhphys) != NULL) 85 return 10; 86 87 return 0; 88 } 89 90 static void 91 amhphyattach(device_t parent, device_t self, void *aux) 92 { 93 struct mii_softc *sc = device_private(self); 94 struct mii_attach_args *ma = aux; 95 struct mii_data *mii = ma->mii_data; 96 const struct mii_phydesc *mpd; 97 98 mpd = mii_phy_match(ma, amhphys); 99 aprint_naive(": Media interface\n"); 100 aprint_normal(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2)); 101 102 sc->mii_dev = self; 103 sc->mii_inst = mii->mii_instance; 104 sc->mii_phy = ma->mii_phyno; 105 sc->mii_funcs = &amhphy_funcs; 106 sc->mii_pdata = mii; 107 sc->mii_flags = ma->mii_flags; 108 109 mii_lock(mii); 110 111 PHY_RESET(sc); 112 113 PHY_READ(sc, MII_BMSR, &sc->mii_capabilities); 114 sc->mii_capabilities &= ma->mii_capmask; 115 116 mii_unlock(mii); 117 118 mii_phy_add_media(sc); 119 } 120 121 static int 122 amhphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd) 123 { 124 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 125 uint16_t reg; 126 127 KASSERT(mii_locked(mii)); 128 129 switch (cmd) { 130 case MII_POLLSTAT: 131 /* If we're not polling our PHY instance, just return. */ 132 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 133 return 0; 134 break; 135 136 case MII_MEDIACHG: 137 /* 138 * If the media indicates a different PHY instance, 139 * isolate ourselves. 140 */ 141 if (IFM_INST(ife->ifm_media) != sc->mii_inst) { 142 PHY_READ(sc, MII_BMCR, ®); 143 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO); 144 return 0; 145 } 146 147 /* If the interface is not up, don't do anything. */ 148 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 149 break; 150 151 mii_phy_setmedia(sc); 152 break; 153 154 case MII_TICK: 155 /* If we're not currently selected, just return. */ 156 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 157 return 0; 158 159 if (mii_phy_tick(sc) == EJUSTRETURN) 160 return 0; 161 break; 162 163 case MII_DOWN: 164 mii_phy_down(sc); 165 return 0; 166 } 167 168 /* Update the media status. */ 169 mii_phy_status(sc); 170 171 /* Callback if something changed. */ 172 mii_phy_update(sc, cmd); 173 return 0; 174 } 175 176 static void 177 amhphy_status(struct mii_softc *sc) 178 { 179 struct mii_data *mii = sc->mii_pdata; 180 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 181 uint16_t bmsr, bmcr, ssr; 182 183 KASSERT(mii_locked(mii)); 184 185 mii->mii_media_status = IFM_AVALID; 186 mii->mii_media_active = IFM_ETHER; 187 188 PHY_READ(sc, MII_BMSR, &bmsr); 189 PHY_READ(sc, MII_AMHPHY_SSR, &ssr); 190 191 if (ssr & SSR_LS) 192 mii->mii_media_status |= IFM_ACTIVE; 193 194 PHY_READ(sc, MII_BMCR, &bmcr); 195 if (bmcr & BMCR_ISO) { 196 mii->mii_media_active |= IFM_NONE; 197 mii->mii_media_status = 0; 198 return; 199 } 200 201 if (bmcr & BMCR_LOOP) 202 mii->mii_media_active |= IFM_LOOP; 203 204 if (bmcr & BMCR_AUTOEN) { 205 /* 206 * The media status bits are only valid if autonegotiation 207 * has completed (or it's disabled). 208 */ 209 if ((bmsr & BMSR_ACOMP) == 0) { 210 /* Erg, still trying, I guess... */ 211 mii->mii_media_active |= IFM_NONE; 212 return; 213 } 214 215 if (ssr & SSR_S) { 216 /* 217 * This should't really ever happen, since it's 218 * a 10BASE-T only PHY. But the bit exists, according 219 * to the documentation, so we pay attention to it. 220 */ 221 mii->mii_media_active |= IFM_100_TX; 222 } else 223 mii->mii_media_active |= IFM_10_T; 224 if (ssr & SSR_FD) 225 mii->mii_media_active |= IFM_FDX; 226 else 227 mii->mii_media_active |= IFM_HDX; 228 } else 229 mii->mii_media_active = ife->ifm_media; 230 } 231