1 /* $NetBSD: mii_physubr.c,v 1.5 1999/08/03 19:41:49 drochner Exp $ */ 2 3 /*- 4 * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 9 * NASA Ames Research Center. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the NetBSD 22 * Foundation, Inc. and its contributors. 23 * 4. Neither the name of The NetBSD Foundation nor the names of its 24 * contributors may be used to endorse or promote products derived 25 * from this software without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 37 * POSSIBILITY OF SUCH DAMAGE. 38 */ 39 40 /* 41 * Subroutines common to all PHYs. 42 */ 43 44 #include <sys/param.h> 45 #include <sys/systm.h> 46 #include <sys/kernel.h> 47 #include <sys/socket.h> 48 #include <sys/errno.h> 49 #include <sys/module.h> 50 #include <sys/bus.h> 51 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 59 #include "miibus_if.h" 60 61 #if !defined(lint) 62 static const char rcsid[] = 63 "$FreeBSD$"; 64 #endif 65 66 void mii_phy_auto_timeout __P((void *)); 67 68 int 69 mii_phy_auto(mii, waitfor) 70 struct mii_softc *mii; 71 int waitfor; 72 { 73 int bmsr, i; 74 75 if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) { 76 PHY_WRITE(mii, MII_ANAR, 77 BMSR_MEDIA_TO_ANAR(mii->mii_capabilities) | ANAR_CSMA); 78 PHY_WRITE(mii, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG); 79 } 80 81 if (waitfor) { 82 /* Wait 500ms for it to complete. */ 83 for (i = 0; i < 500; i++) { 84 if ((bmsr = PHY_READ(mii, MII_BMSR)) & BMSR_ACOMP) 85 return (0); 86 DELAY(1000); 87 #if 0 88 if ((bmsr & BMSR_ACOMP) == 0) 89 printf("%s: autonegotiation failed to complete\n", 90 mii->mii_dev.dv_xname); 91 #endif 92 } 93 94 /* 95 * Don't need to worry about clearing MIIF_DOINGAUTO. 96 * If that's set, a timeout is pending, and it will 97 * clear the flag. 98 */ 99 return (EIO); 100 } 101 102 /* 103 * Just let it finish asynchronously. This is for the benefit of 104 * the tick handler driving autonegotiation. Don't want 500ms 105 * delays all the time while the system is running! 106 */ 107 if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) { 108 mii->mii_flags |= MIIF_DOINGAUTO; 109 mii->mii_auto_ch = timeout(mii_phy_auto_timeout, mii, hz >> 1); 110 } 111 return (EJUSTRETURN); 112 } 113 114 void 115 mii_phy_auto_stop(sc) 116 struct mii_softc *sc; 117 { 118 if (sc->mii_flags & MIIF_DOINGAUTO) { 119 sc->mii_flags &= ~MIIF_DOINGAUTO; 120 untimeout(mii_phy_auto_timeout, sc, sc->mii_auto_ch); 121 } 122 } 123 124 void 125 mii_phy_auto_timeout(arg) 126 void *arg; 127 { 128 struct mii_softc *mii = arg; 129 int s, bmsr; 130 131 s = splnet(); 132 mii->mii_flags &= ~MIIF_DOINGAUTO; 133 bmsr = PHY_READ(mii, MII_BMSR); 134 #if 0 135 if ((bmsr & BMSR_ACOMP) == 0) 136 printf("%s: autonegotiation failed to complete\n", 137 sc->sc_dev.dv_xname); 138 #endif 139 140 /* Update the media status. */ 141 (void) (*mii->mii_service)(mii, mii->mii_pdata, MII_POLLSTAT); 142 splx(s); 143 } 144 145 void 146 mii_phy_reset(mii) 147 struct mii_softc *mii; 148 { 149 int reg, i; 150 151 if (mii->mii_flags & MIIF_NOISOLATE) 152 reg = BMCR_RESET; 153 else 154 reg = BMCR_RESET | BMCR_ISO; 155 PHY_WRITE(mii, MII_BMCR, reg); 156 157 /* Wait 100ms for it to complete. */ 158 for (i = 0; i < 100; i++) { 159 reg = PHY_READ(mii, MII_BMCR); 160 if ((reg & BMCR_RESET) == 0) 161 break; 162 DELAY(1000); 163 } 164 165 if (mii->mii_inst != 0 && ((mii->mii_flags & MIIF_NOISOLATE) == 0)) 166 PHY_WRITE(mii, MII_BMCR, reg | BMCR_ISO); 167 } 168 169 /* 170 * Given an ifmedia word, return the corresponding ANAR value. 171 */ 172 int 173 mii_anar(media) 174 int media; 175 { 176 int rv; 177 178 switch (media & (IFM_TMASK|IFM_NMASK|IFM_FDX)) { 179 case IFM_ETHER|IFM_10_T: 180 rv = ANAR_10|ANAR_CSMA; 181 break; 182 case IFM_ETHER|IFM_10_T|IFM_FDX: 183 rv = ANAR_10_FD|ANAR_CSMA; 184 break; 185 case IFM_ETHER|IFM_100_TX: 186 rv = ANAR_TX|ANAR_CSMA; 187 break; 188 case IFM_ETHER|IFM_100_TX|IFM_FDX: 189 rv = ANAR_TX_FD|ANAR_CSMA; 190 break; 191 case IFM_ETHER|IFM_100_T4: 192 rv = ANAR_T4|ANAR_CSMA; 193 break; 194 default: 195 rv = 0; 196 break; 197 } 198 199 return (rv); 200 } 201 202 /* 203 * Given a BMCR value, return the corresponding ifmedia word. 204 */ 205 int 206 mii_media_from_bmcr(bmcr) 207 int bmcr; 208 { 209 int rv = IFM_ETHER; 210 211 if (bmcr & BMCR_S100) 212 rv |= IFM_100_TX; 213 else 214 rv |= IFM_10_T; 215 if (bmcr & BMCR_FDX) 216 rv |= IFM_FDX; 217 218 return (rv); 219 } 220 221 /* 222 * Initialize generic PHY media based on BMSR, called when a PHY is 223 * attached. We expect to be set up to print a comma-separated list 224 * of media names. Does not print a newline. 225 */ 226 void 227 mii_add_media(mii, bmsr, instance) 228 struct mii_data *mii; 229 int bmsr, instance; 230 { 231 const char *sep = ""; 232 233 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL) 234 #define PRINT(s) printf("%s%s", sep, s); sep = ", " 235 236 if (bmsr & BMSR_10THDX) { 237 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, instance), 0); 238 PRINT("10baseT"); 239 } 240 if (bmsr & BMSR_10TFDX) { 241 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, instance), 242 BMCR_FDX); 243 PRINT("10baseT-FDX"); 244 } 245 if (bmsr & BMSR_100TXHDX) { 246 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, instance), 247 BMCR_S100); 248 PRINT("100baseTX"); 249 } 250 if (bmsr & BMSR_100TXFDX) { 251 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, instance), 252 BMCR_S100|BMCR_FDX); 253 PRINT("100baseTX-FDX"); 254 } 255 if (bmsr & BMSR_100T4) { 256 /* 257 * XXX How do you enable 100baseT4? I assume we set 258 * XXX BMCR_S100 and then assume the PHYs will take 259 * XXX watever action is necessary to switch themselves 260 * XXX into T4 mode. 261 */ 262 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_T4, 0, instance), 263 BMCR_S100); 264 PRINT("100baseT4"); 265 } 266 if (bmsr & BMSR_ANEG) { 267 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, instance), 268 BMCR_AUTOEN); 269 PRINT("auto"); 270 } 271 #undef ADD 272 #undef PRINT 273 } 274