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 * $FreeBSD: src/sys/dev/mii/mii_physubr.c,v 1.2.2.1 2000/12/12 19:29:14 wpaul Exp $ 40 * $DragonFly: src/sys/dev/netif/mii_layer/mii_physubr.c,v 1.6 2005/06/13 22:18:44 joerg Exp $ 41 */ 42 43 /* 44 * Subroutines common to all PHYs. 45 */ 46 47 #include <sys/param.h> 48 #include <sys/systm.h> 49 #include <sys/kernel.h> 50 #include <sys/socket.h> 51 #include <sys/errno.h> 52 #include <sys/module.h> 53 #include <sys/bus.h> 54 #include <sys/thread2.h> 55 56 #include <machine/clock.h> 57 58 #include <net/if.h> 59 #include <net/if_media.h> 60 61 #include "mii.h" 62 #include "miivar.h" 63 64 #include "miibus_if.h" 65 66 void mii_phy_auto_timeout (void *); 67 68 void 69 mii_softc_init(struct mii_softc *mii) 70 { 71 callout_init(&mii->mii_auto_ch); 72 } 73 74 int 75 mii_phy_auto(mii, waitfor) 76 struct mii_softc *mii; 77 int waitfor; 78 { 79 int bmsr, i; 80 81 if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) { 82 PHY_WRITE(mii, MII_ANAR, 83 BMSR_MEDIA_TO_ANAR(mii->mii_capabilities) | ANAR_CSMA); 84 PHY_WRITE(mii, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG); 85 } 86 87 if (waitfor) { 88 /* Wait 500ms for it to complete. */ 89 for (i = 0; i < 500; i++) { 90 if ((bmsr = PHY_READ(mii, MII_BMSR)) & BMSR_ACOMP) 91 return (0); 92 DELAY(1000); 93 #if 0 94 if ((bmsr & BMSR_ACOMP) == 0) 95 printf("%s: autonegotiation failed to complete\n", 96 mii->mii_dev.dv_xname); 97 #endif 98 } 99 100 /* 101 * Don't need to worry about clearing MIIF_DOINGAUTO. 102 * If that's set, a timeout is pending, and it will 103 * clear the flag. 104 */ 105 return (EIO); 106 } 107 108 /* 109 * Just let it finish asynchronously. This is for the benefit of 110 * the tick handler driving autonegotiation. Don't want 500ms 111 * delays all the time while the system is running! 112 */ 113 if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) { 114 mii->mii_flags |= MIIF_DOINGAUTO; 115 callout_reset(&mii->mii_auto_ch, hz >> 1, 116 mii_phy_auto_timeout, mii); 117 } 118 return (EJUSTRETURN); 119 } 120 121 void 122 mii_phy_auto_stop(sc) 123 struct mii_softc *sc; 124 { 125 if (sc->mii_flags & MIIF_DOINGAUTO) { 126 sc->mii_flags &= ~MIIF_DOINGAUTO; 127 callout_stop(&sc->mii_auto_ch); 128 } 129 } 130 131 void 132 mii_phy_auto_timeout(arg) 133 void *arg; 134 { 135 struct mii_softc *mii = arg; 136 int bmsr; 137 138 crit_enter(); 139 140 mii->mii_flags &= ~MIIF_DOINGAUTO; 141 bmsr = PHY_READ(mii, MII_BMSR); 142 #if 0 143 if ((bmsr & BMSR_ACOMP) == 0) 144 printf("%s: autonegotiation failed to complete\n", 145 sc->sc_dev.dv_xname); 146 #endif 147 148 /* Update the media status. */ 149 (void) (*mii->mii_service)(mii, mii->mii_pdata, MII_POLLSTAT); 150 151 crit_exit(); 152 } 153 154 void 155 mii_phy_reset(mii) 156 struct mii_softc *mii; 157 { 158 int reg, i; 159 160 if (mii->mii_flags & MIIF_NOISOLATE) 161 reg = BMCR_RESET; 162 else 163 reg = BMCR_RESET | BMCR_ISO; 164 PHY_WRITE(mii, MII_BMCR, reg); 165 166 /* Wait 100ms for it to complete. */ 167 for (i = 0; i < 100; i++) { 168 reg = PHY_READ(mii, MII_BMCR); 169 if ((reg & BMCR_RESET) == 0) 170 break; 171 DELAY(1000); 172 } 173 174 if (mii->mii_inst != 0 && ((mii->mii_flags & MIIF_NOISOLATE) == 0)) 175 PHY_WRITE(mii, MII_BMCR, reg | BMCR_ISO); 176 } 177 178 /* 179 * Given an ifmedia word, return the corresponding ANAR value. 180 */ 181 int 182 mii_anar(media) 183 int media; 184 { 185 int rv; 186 187 switch (media & (IFM_TMASK|IFM_NMASK|IFM_FDX)) { 188 case IFM_ETHER|IFM_10_T: 189 rv = ANAR_10|ANAR_CSMA; 190 break; 191 case IFM_ETHER|IFM_10_T|IFM_FDX: 192 rv = ANAR_10_FD|ANAR_CSMA; 193 break; 194 case IFM_ETHER|IFM_100_TX: 195 rv = ANAR_TX|ANAR_CSMA; 196 break; 197 case IFM_ETHER|IFM_100_TX|IFM_FDX: 198 rv = ANAR_TX_FD|ANAR_CSMA; 199 break; 200 case IFM_ETHER|IFM_100_T4: 201 rv = ANAR_T4|ANAR_CSMA; 202 break; 203 default: 204 rv = 0; 205 break; 206 } 207 208 return (rv); 209 } 210 211 /* 212 * Given a BMCR value, return the corresponding ifmedia word. 213 */ 214 int 215 mii_media_from_bmcr(bmcr) 216 int bmcr; 217 { 218 int rv = IFM_ETHER; 219 220 if (bmcr & BMCR_S100) 221 rv |= IFM_100_TX; 222 else 223 rv |= IFM_10_T; 224 if (bmcr & BMCR_FDX) 225 rv |= IFM_FDX; 226 227 return (rv); 228 } 229 230 /* 231 * Initialize generic PHY media based on BMSR, called when a PHY is 232 * attached. We expect to be set up to print a comma-separated list 233 * of media names. Does not print a newline. 234 */ 235 void 236 mii_add_media(mii, bmsr, instance) 237 struct mii_data *mii; 238 int bmsr, instance; 239 { 240 const char *sep = ""; 241 242 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL) 243 #define PRINT(s) printf("%s%s", sep, s); sep = ", " 244 245 if (bmsr & BMSR_10THDX) { 246 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, instance), 0); 247 PRINT("10baseT"); 248 } 249 if (bmsr & BMSR_10TFDX) { 250 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, instance), 251 BMCR_FDX); 252 PRINT("10baseT-FDX"); 253 } 254 if (bmsr & BMSR_100TXHDX) { 255 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, instance), 256 BMCR_S100); 257 PRINT("100baseTX"); 258 } 259 if (bmsr & BMSR_100TXFDX) { 260 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, instance), 261 BMCR_S100|BMCR_FDX); 262 PRINT("100baseTX-FDX"); 263 } 264 if (bmsr & BMSR_100T4) { 265 /* 266 * XXX How do you enable 100baseT4? I assume we set 267 * XXX BMCR_S100 and then assume the PHYs will take 268 * XXX watever action is necessary to switch themselves 269 * XXX into T4 mode. 270 */ 271 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_T4, 0, instance), 272 BMCR_S100); 273 PRINT("100baseT4"); 274 } 275 if (bmsr & BMSR_ANEG) { 276 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, instance), 277 BMCR_AUTOEN); 278 PRINT("auto"); 279 } 280 #undef ADD 281 #undef PRINT 282 } 283