1 /* 2 * Copyright (c) 2000 3 * Bill Paul <wpaul@ee.columbia.edu>. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by Bill Paul. 16 * 4. Neither the name of the author nor the names of any co-contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 30 * THE POSSIBILITY OF SUCH DAMAGE. 31 * 32 * $FreeBSD$ 33 */ 34 35 /* 36 * driver for the XaQti XMAC II's internal PHY. This is sort of 37 * like a 10/100 PHY, except the only thing we're really autoselecting 38 * here is full/half duplex. Speed is always 1000mbps. 39 */ 40 41 #include <sys/param.h> 42 #include <sys/systm.h> 43 #include <sys/kernel.h> 44 #include <sys/malloc.h> 45 #include <sys/socket.h> 46 #include <sys/bus.h> 47 48 #include <machine/clock.h> 49 50 #include <net/if.h> 51 #include <net/if_media.h> 52 53 #include <dev/mii/mii.h> 54 #include <dev/mii/miivar.h> 55 #include <dev/mii/miidevs.h> 56 57 #include <dev/mii/xmphyreg.h> 58 59 #include "miibus_if.h" 60 61 #if !defined(lint) 62 static const char rcsid[] = 63 "$FreeBSD$"; 64 #endif 65 66 static int xmphy_probe __P((device_t)); 67 static int xmphy_attach __P((device_t)); 68 static int xmphy_detach __P((device_t)); 69 70 static device_method_t xmphy_methods[] = { 71 /* device interface */ 72 DEVMETHOD(device_probe, xmphy_probe), 73 DEVMETHOD(device_attach, xmphy_attach), 74 DEVMETHOD(device_detach, xmphy_detach), 75 DEVMETHOD(device_shutdown, bus_generic_shutdown), 76 { 0, 0 } 77 }; 78 79 static devclass_t xmphy_devclass; 80 81 static driver_t xmphy_driver = { 82 "xmphy", 83 xmphy_methods, 84 sizeof(struct mii_softc) 85 }; 86 87 DRIVER_MODULE(xmphy, miibus, xmphy_driver, xmphy_devclass, 0, 0); 88 89 int xmphy_service __P((struct mii_softc *, struct mii_data *, int)); 90 void xmphy_status __P((struct mii_softc *)); 91 92 static int xmphy_mii_phy_auto __P((struct mii_softc *, int)); 93 extern void mii_phy_auto_timeout __P((void *)); 94 95 static int xmphy_probe(dev) 96 device_t dev; 97 { 98 struct mii_attach_args *ma; 99 100 ma = device_get_ivars(dev); 101 102 if (MII_OUI(ma->mii_id1, ma->mii_id2) != MII_OUI_xxXAQTI || 103 MII_MODEL(ma->mii_id2) != MII_MODEL_XAQTI_XMACII) 104 return(ENXIO); 105 106 device_set_desc(dev, MII_STR_XAQTI_XMACII); 107 108 return(0); 109 } 110 111 static int xmphy_attach(dev) 112 device_t dev; 113 { 114 struct mii_softc *sc; 115 struct mii_attach_args *ma; 116 struct mii_data *mii; 117 const char *sep = ""; 118 119 sc = device_get_softc(dev); 120 ma = device_get_ivars(dev); 121 sc->mii_dev = device_get_parent(dev); 122 mii = device_get_softc(sc->mii_dev); 123 LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list); 124 125 sc->mii_inst = mii->mii_instance; 126 sc->mii_phy = ma->mii_phyno; 127 sc->mii_service = xmphy_service; 128 sc->mii_pdata = mii; 129 130 sc->mii_flags |= MIIF_NOISOLATE; 131 mii->mii_instance++; 132 133 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL) 134 #define PRINT(s) printf("%s%s", sep, s); sep = ", " 135 136 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst), 137 BMCR_ISO); 138 #if 0 139 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->mii_inst), 140 BMCR_LOOP|BMCR_S100); 141 #endif 142 143 mii_phy_reset(sc); 144 145 device_printf(dev, " "); 146 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, 0, sc->mii_inst), 147 XMPHY_BMCR_FDX); 148 PRINT("1000baseSX"); 149 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, IFM_FDX, sc->mii_inst), 0); 150 PRINT("1000baseSX-FDX"); 151 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst), 0); 152 PRINT("auto"); 153 154 printf("\n"); 155 #undef ADD 156 #undef PRINT 157 158 MIIBUS_MEDIAINIT(sc->mii_dev); 159 return(0); 160 } 161 162 static int xmphy_detach(dev) 163 device_t dev; 164 { 165 struct mii_softc *sc; 166 struct mii_data *mii; 167 168 sc = device_get_softc(dev); 169 mii = device_get_softc(device_get_parent(dev)); 170 sc->mii_dev = NULL; 171 LIST_REMOVE(sc, mii_list); 172 173 return(0); 174 } 175 int 176 xmphy_service(sc, mii, cmd) 177 struct mii_softc *sc; 178 struct mii_data *mii; 179 int cmd; 180 { 181 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 182 int reg; 183 184 switch (cmd) { 185 case MII_POLLSTAT: 186 /* 187 * If we're not polling our PHY instance, just return. 188 */ 189 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 190 return (0); 191 break; 192 193 case MII_MEDIACHG: 194 /* 195 * If the media indicates a different PHY instance, 196 * isolate ourselves. 197 */ 198 if (IFM_INST(ife->ifm_media) != sc->mii_inst) { 199 reg = PHY_READ(sc, MII_BMCR); 200 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO); 201 return (0); 202 } 203 204 /* 205 * If the interface is not up, don't do anything. 206 */ 207 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 208 break; 209 210 switch (IFM_SUBTYPE(ife->ifm_media)) { 211 case IFM_AUTO: 212 /* 213 * If we're already in auto mode, just return. 214 */ 215 if (PHY_READ(sc, XMPHY_MII_BMCR) & XMPHY_BMCR_AUTOEN) 216 return (0); 217 (void) xmphy_mii_phy_auto(sc, 1); 218 break; 219 case IFM_1000_SX: 220 mii_phy_reset(sc); 221 if ((ife->ifm_media & IFM_GMASK) == IFM_FDX) { 222 PHY_WRITE(sc, XMPHY_MII_ANAR, XMPHY_ANAR_FDX); 223 PHY_WRITE(sc, XMPHY_MII_BMCR, XMPHY_BMCR_FDX); 224 } else { 225 PHY_WRITE(sc, XMPHY_MII_ANAR, XMPHY_ANAR_HDX); 226 PHY_WRITE(sc, XMPHY_MII_BMCR, 0); 227 } 228 break; 229 case IFM_100_T4: 230 case IFM_100_TX: 231 case IFM_10_T: 232 default: 233 return (EINVAL); 234 } 235 break; 236 237 case MII_TICK: 238 /* 239 * If we're not currently selected, just return. 240 */ 241 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 242 return (0); 243 244 /* 245 * Only used for autonegotiation. 246 */ 247 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) 248 return (0); 249 250 /* 251 * Is the interface even up? 252 */ 253 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 254 return (0); 255 256 /* 257 * Only retry autonegotiation every 5 seconds. 258 */ 259 if (++sc->mii_ticks != 5) 260 return (0); 261 262 sc->mii_ticks = 0; 263 264 /* 265 * Check to see if we have link. If we do, we don't 266 * need to restart the autonegotiation process. Read 267 * the BMSR twice in case it's latched. 268 */ 269 reg = PHY_READ(sc, XMPHY_MII_BMSR) | 270 PHY_READ(sc, XMPHY_MII_BMSR); 271 if (reg & XMPHY_BMSR_LINK) 272 break; 273 274 mii_phy_reset(sc); 275 if (xmphy_mii_phy_auto(sc, 0) == EJUSTRETURN) 276 return(0); 277 break; 278 } 279 280 /* Update the media status. */ 281 xmphy_status(sc); 282 283 /* Callback if something changed. */ 284 if (sc->mii_active != mii->mii_media_active || cmd == MII_MEDIACHG) { 285 MIIBUS_STATCHG(sc->mii_dev); 286 sc->mii_active = mii->mii_media_active; 287 } 288 return (0); 289 } 290 291 void 292 xmphy_status(sc) 293 struct mii_softc *sc; 294 { 295 struct mii_data *mii = sc->mii_pdata; 296 int bmsr, bmcr, anlpar; 297 298 mii->mii_media_status = IFM_AVALID; 299 mii->mii_media_active = IFM_ETHER; 300 301 bmsr = PHY_READ(sc, XMPHY_MII_BMSR) | 302 PHY_READ(sc, XMPHY_MII_BMSR); 303 if (bmsr & XMPHY_BMSR_LINK) 304 mii->mii_media_status |= IFM_ACTIVE; 305 306 /* Do dummy read of extended status register. */ 307 bmcr = PHY_READ(sc, XMPHY_MII_EXTSTS); 308 309 bmcr = PHY_READ(sc, XMPHY_MII_BMCR); 310 311 if (bmcr & XMPHY_BMCR_LOOP) 312 mii->mii_media_active |= IFM_LOOP; 313 314 315 if (bmcr & XMPHY_BMCR_AUTOEN) { 316 if ((bmsr & XMPHY_BMSR_ACOMP) == 0) { 317 if (bmsr & XMPHY_BMSR_LINK) { 318 mii->mii_media_active |= IFM_1000_SX|IFM_HDX; 319 return; 320 } 321 /* Erg, still trying, I guess... */ 322 mii->mii_media_active |= IFM_NONE; 323 return; 324 } 325 326 mii->mii_media_active |= IFM_1000_SX; 327 anlpar = PHY_READ(sc, XMPHY_MII_ANAR) & 328 PHY_READ(sc, XMPHY_MII_ANLPAR); 329 if (anlpar & XMPHY_ANLPAR_FDX) 330 mii->mii_media_active |= IFM_FDX; 331 else 332 mii->mii_media_active |= IFM_HDX; 333 return; 334 } 335 336 mii->mii_media_active |= IFM_1000_SX; 337 if (bmcr & XMPHY_BMCR_FDX) 338 mii->mii_media_active |= IFM_FDX; 339 else 340 mii->mii_media_active |= IFM_HDX; 341 342 return; 343 } 344 345 346 static int 347 xmphy_mii_phy_auto(mii, waitfor) 348 struct mii_softc *mii; 349 int waitfor; 350 { 351 int bmsr, i; 352 353 if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) { 354 PHY_WRITE(mii, XMPHY_MII_ANAR, 355 XMPHY_ANAR_FDX|XMPHY_ANAR_HDX); 356 PHY_WRITE(mii, XMPHY_MII_BMCR, 357 XMPHY_BMCR_AUTOEN | XMPHY_BMCR_STARTNEG); 358 } 359 360 if (waitfor) { 361 /* Wait 500ms for it to complete. */ 362 for (i = 0; i < 500; i++) { 363 if ((bmsr = PHY_READ(mii, XMPHY_MII_BMSR)) & 364 XMPHY_BMSR_ACOMP) 365 return (0); 366 DELAY(1000); 367 #if 0 368 if ((bmsr & BMSR_ACOMP) == 0) 369 printf("%s: autonegotiation failed to complete\n", 370 mii->mii_dev.dv_xname); 371 #endif 372 } 373 374 /* 375 * Don't need to worry about clearing MIIF_DOINGAUTO. 376 * If that's set, a timeout is pending, and it will 377 * clear the flag. 378 */ 379 return (EIO); 380 } 381 382 /* 383 * Just let it finish asynchronously. This is for the benefit of 384 * the tick handler driving autonegotiation. Don't want 500ms 385 * delays all the time while the system is running! 386 */ 387 if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) { 388 mii->mii_flags |= MIIF_DOINGAUTO; 389 timeout(mii_phy_auto_timeout, mii, hz >> 1); 390 } 391 return (EJUSTRETURN); 392 } 393