1 /* $NetBSD: acphy.c,v 1.30 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 Altima AC101 PHY. 40 */ 41 42 #include <sys/cdefs.h> 43 __KERNEL_RCSID(0, "$NetBSD: acphy.c,v 1.30 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/acphyreg.h> 60 61 static int acphymatch(device_t, cfdata_t, void *); 62 static void acphyattach(device_t, device_t, void *); 63 64 CFATTACH_DECL_NEW(acphy, sizeof(struct mii_softc), 65 acphymatch, acphyattach, mii_phy_detach, mii_phy_activate); 66 67 static int acphy_service(struct mii_softc *, struct mii_data *, int); 68 static void acphy_status(struct mii_softc *); 69 70 static const struct mii_phy_funcs acphy_funcs = { 71 acphy_service, acphy_status, mii_phy_reset, 72 }; 73 74 static const struct mii_phydesc acphys[] = { 75 MII_PHY_DESC(ALTIMA, AC101), 76 MII_PHY_DESC(ALTIMA, AC101L), 77 MII_PHY_DESC(ALTIMA, Am79C874), 78 MII_PHY_DESC(ALTIMA, Am79C875), 79 /* XXX This is reported to work, but it's not from any data sheet. */ 80 MII_PHY_DESC(ALTIMA, ACXXX), 81 MII_PHY_END, 82 }; 83 84 static int 85 acphymatch(device_t parent, cfdata_t match, void *aux) 86 { 87 struct mii_attach_args *ma = aux; 88 89 if (mii_phy_match(ma, acphys) != NULL) 90 return 10; 91 92 return 0; 93 } 94 95 static void 96 acphyattach(device_t parent, device_t self, void *aux) 97 { 98 struct mii_softc *sc = device_private(self); 99 struct mii_attach_args *ma = aux; 100 struct mii_data *mii = ma->mii_data; 101 const struct mii_phydesc *mpd; 102 103 mpd = mii_phy_match(ma, acphys); 104 aprint_naive(": Media interface\n"); 105 aprint_normal(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2)); 106 107 sc->mii_dev = self; 108 sc->mii_inst = mii->mii_instance; 109 sc->mii_phy = ma->mii_phyno; 110 sc->mii_funcs = &acphy_funcs; 111 sc->mii_pdata = mii; 112 sc->mii_flags = ma->mii_flags; 113 114 mii_lock(mii); 115 116 PHY_RESET(sc); 117 118 /* XXX Check MCR_FX_SEL to set MIIF_HAVE_FIBER? */ 119 120 PHY_READ(sc, MII_BMSR, &sc->mii_capabilities); 121 sc->mii_capabilities &= ma->mii_capmask; 122 123 mii_unlock(mii); 124 125 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL) 126 if (sc->mii_flags & MIIF_HAVEFIBER) { 127 aprint_normal_dev(sc->mii_dev, ""); 128 mii_lock(mii); 129 sc->mii_anegticks = MII_ANEGTICKS; 130 mii_unlock(mii); 131 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_FX, 0, sc->mii_inst), 132 MII_MEDIA_100_TX); 133 aprint_normal("100baseFX, "); 134 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_FX, IFM_FDX, sc->mii_inst), 135 MII_MEDIA_100_TX); 136 aprint_normal("100baseFX-FDX\n"); 137 } 138 #undef ADD 139 140 mii_phy_add_media(sc); 141 } 142 143 static int 144 acphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd) 145 { 146 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 147 uint16_t reg; 148 149 KASSERT(mii_locked(mii)); 150 151 switch (cmd) { 152 case MII_POLLSTAT: 153 /* If we're not polling our PHY instance, just return. */ 154 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 155 return 0; 156 break; 157 158 case MII_MEDIACHG: 159 /* 160 * If the media indicates a different PHY instance, 161 * isolate ourselves. 162 */ 163 if (IFM_INST(ife->ifm_media) != sc->mii_inst) { 164 PHY_READ(sc, MII_BMCR, ®); 165 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO); 166 return 0; 167 } 168 169 /* If the interface is not up, don't do anything. */ 170 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 171 break; 172 173 mii_phy_setmedia(sc); 174 break; 175 176 case MII_TICK: 177 /* If we're not currently selected, just return. */ 178 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 179 return 0; 180 181 if (mii_phy_tick(sc) == EJUSTRETURN) 182 return 0; 183 break; 184 185 case MII_DOWN: 186 mii_phy_down(sc); 187 return 0; 188 } 189 190 /* Update the media status. */ 191 mii_phy_status(sc); 192 193 /* Callback if something changed. */ 194 mii_phy_update(sc, cmd); 195 return 0; 196 } 197 198 static void 199 acphy_status(struct mii_softc *sc) 200 { 201 struct mii_data *mii = sc->mii_pdata; 202 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 203 uint16_t bmsr, bmcr, dr; 204 205 KASSERT(mii_locked(mii)); 206 207 mii->mii_media_status = IFM_AVALID; 208 mii->mii_media_active = IFM_ETHER; 209 210 PHY_READ(sc, MII_BMSR, &bmsr); 211 PHY_READ(sc, MII_BMSR, &bmsr); 212 PHY_READ(sc, MII_ACPHY_DR, &dr); 213 214 if (bmsr & BMSR_LINK) 215 mii->mii_media_status |= IFM_ACTIVE; 216 217 PHY_READ(sc, MII_BMCR, &bmcr); 218 if (bmcr & BMCR_ISO) { 219 mii->mii_media_active |= IFM_NONE; 220 mii->mii_media_status = 0; 221 return; 222 } 223 224 if (bmcr & BMCR_LOOP) 225 mii->mii_media_active |= IFM_LOOP; 226 227 if (bmcr & BMCR_AUTOEN) { 228 /* 229 * The media status bits are only valid if autonegotiation 230 * has completed (or it's disabled). 231 */ 232 if ((bmsr & BMSR_ACOMP) == 0) { 233 /* Erg, still trying, I guess... */ 234 mii->mii_media_active |= IFM_NONE; 235 return; 236 } 237 238 if (dr & DR_SPEED) 239 mii->mii_media_active |= IFM_100_TX; 240 else 241 mii->mii_media_active |= IFM_10_T; 242 243 if (dr & DR_DPLX) 244 mii->mii_media_active |= IFM_FDX; 245 else 246 mii->mii_media_active |= IFM_HDX; 247 } else 248 mii->mii_media_active = ife->ifm_media; 249 } 250