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 timeout(mii_phy_auto_timeout, mii, hz >> 1); 110 } 111 return (EJUSTRETURN); 112 } 113 114 void 115 mii_phy_auto_timeout(arg) 116 void *arg; 117 { 118 struct mii_softc *mii = arg; 119 int s, bmsr; 120 121 s = splnet(); 122 mii->mii_flags &= ~MIIF_DOINGAUTO; 123 bmsr = PHY_READ(mii, MII_BMSR); 124 #if 0 125 if ((bmsr & BMSR_ACOMP) == 0) 126 printf("%s: autonegotiation failed to complete\n", 127 sc->sc_dev.dv_xname); 128 #endif 129 130 /* Update the media status. */ 131 (void) (*mii->mii_service)(mii, mii->mii_pdata, MII_POLLSTAT); 132 splx(s); 133 } 134 135 void 136 mii_phy_reset(mii) 137 struct mii_softc *mii; 138 { 139 int reg, i; 140 141 if (mii->mii_flags & MIIF_NOISOLATE) 142 reg = BMCR_RESET; 143 else 144 reg = BMCR_RESET | BMCR_ISO; 145 PHY_WRITE(mii, MII_BMCR, reg); 146 147 /* Wait 100ms for it to complete. */ 148 for (i = 0; i < 100; i++) { 149 reg = PHY_READ(mii, MII_BMCR); 150 if ((reg & BMCR_RESET) == 0) 151 break; 152 DELAY(1000); 153 } 154 155 if (mii->mii_inst != 0 && ((mii->mii_flags & MIIF_NOISOLATE) == 0)) 156 PHY_WRITE(mii, MII_BMCR, reg | BMCR_ISO); 157 } 158 159 /* 160 * Given an ifmedia word, return the corresponding ANAR value. 161 */ 162 int 163 mii_anar(media) 164 int media; 165 { 166 int rv; 167 168 switch (media & (IFM_TMASK|IFM_NMASK|IFM_FDX)) { 169 case IFM_ETHER|IFM_10_T: 170 rv = ANAR_10|ANAR_CSMA; 171 break; 172 case IFM_ETHER|IFM_10_T|IFM_FDX: 173 rv = ANAR_10_FD|ANAR_CSMA; 174 break; 175 case IFM_ETHER|IFM_100_TX: 176 rv = ANAR_TX|ANAR_CSMA; 177 break; 178 case IFM_ETHER|IFM_100_TX|IFM_FDX: 179 rv = ANAR_TX_FD|ANAR_CSMA; 180 break; 181 case IFM_ETHER|IFM_100_T4: 182 rv = ANAR_T4|ANAR_CSMA; 183 break; 184 default: 185 rv = 0; 186 break; 187 } 188 189 return (rv); 190 } 191 192 /* 193 * Given a BMCR value, return the corresponding ifmedia word. 194 */ 195 int 196 mii_media_from_bmcr(bmcr) 197 int bmcr; 198 { 199 int rv = IFM_ETHER; 200 201 if (bmcr & BMCR_S100) 202 rv |= IFM_100_TX; 203 else 204 rv |= IFM_10_T; 205 if (bmcr & BMCR_FDX) 206 rv |= IFM_FDX; 207 208 return (rv); 209 } 210 211 /* 212 * Initialize generic PHY media based on BMSR, called when a PHY is 213 * attached. We expect to be set up to print a comma-separated list 214 * of media names. Does not print a newline. 215 */ 216 void 217 mii_add_media(mii, bmsr, instance) 218 struct mii_data *mii; 219 int bmsr, instance; 220 { 221 const char *sep = ""; 222 223 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL) 224 #define PRINT(s) printf("%s%s", sep, s); sep = ", " 225 226 if (bmsr & BMSR_10THDX) { 227 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, instance), 0); 228 PRINT("10baseT"); 229 } 230 if (bmsr & BMSR_10TFDX) { 231 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, instance), 232 BMCR_FDX); 233 PRINT("10baseT-FDX"); 234 } 235 if (bmsr & BMSR_100TXHDX) { 236 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, instance), 237 BMCR_S100); 238 PRINT("100baseTX"); 239 } 240 if (bmsr & BMSR_100TXFDX) { 241 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, instance), 242 BMCR_S100|BMCR_FDX); 243 PRINT("100baseTX-FDX"); 244 } 245 if (bmsr & BMSR_100T4) { 246 /* 247 * XXX How do you enable 100baseT4? I assume we set 248 * XXX BMCR_S100 and then assume the PHYs will take 249 * XXX watever action is necessary to switch themselves 250 * XXX into T4 mode. 251 */ 252 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_T4, 0, instance), 253 BMCR_S100); 254 PRINT("100baseT4"); 255 } 256 if (bmsr & BMSR_ANEG) { 257 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, instance), 258 BMCR_AUTOEN); 259 PRINT("auto"); 260 } 261 #undef ADD 262 #undef PRINT 263 } 264