1 /* $NetBSD: tlphy.c,v 1.18 1999/05/14 11:40:28 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 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 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 THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 /*- 34 * Copyright (c) 1997 Manuel Bouyer. All rights reserved. 35 * 36 * Redistribution and use in source and binary forms, with or without 37 * modification, are permitted provided that the following conditions 38 * are met: 39 * 1. Redistributions of source code must retain the above copyright 40 * notice, this list of conditions and the following disclaimer. 41 * 2. Redistributions in binary form must reproduce the above copyright 42 * notice, this list of conditions and the following disclaimer in the 43 * documentation and/or other materials provided with the distribution. 44 * 45 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 46 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 47 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 48 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 49 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 50 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 51 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 52 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 53 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 54 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 55 */ 56 57 #include <sys/cdefs.h> 58 __FBSDID("$FreeBSD$"); 59 60 /* 61 * Driver for Texas Instruments's ThunderLAN PHYs 62 */ 63 64 #include <sys/param.h> 65 #include <sys/systm.h> 66 #include <sys/kernel.h> 67 #include <sys/socket.h> 68 #include <sys/errno.h> 69 #include <sys/module.h> 70 #include <sys/bus.h> 71 #include <sys/malloc.h> 72 73 #include <machine/bus.h> 74 75 #include <net/if.h> 76 #include <net/if_media.h> 77 78 #include <dev/mii/mii.h> 79 #include <dev/mii/miivar.h> 80 #include "miidevs.h" 81 82 #include <dev/mii/tlphyreg.h> 83 84 #include "miibus_if.h" 85 86 struct tlphy_softc { 87 struct mii_softc sc_mii; /* generic PHY */ 88 int sc_need_acomp; 89 }; 90 91 static int tlphy_probe(device_t); 92 static int tlphy_attach(device_t); 93 94 static device_method_t tlphy_methods[] = { 95 /* device interface */ 96 DEVMETHOD(device_probe, tlphy_probe), 97 DEVMETHOD(device_attach, tlphy_attach), 98 DEVMETHOD(device_detach, mii_phy_detach), 99 DEVMETHOD(device_shutdown, bus_generic_shutdown), 100 { 0, 0 } 101 }; 102 103 static devclass_t tlphy_devclass; 104 105 static driver_t tlphy_driver = { 106 "tlphy", 107 tlphy_methods, 108 sizeof(struct tlphy_softc) 109 }; 110 111 DRIVER_MODULE(tlphy, miibus, tlphy_driver, tlphy_devclass, 0, 0); 112 113 static int tlphy_service(struct mii_softc *, struct mii_data *, int); 114 static int tlphy_auto(struct tlphy_softc *); 115 static void tlphy_acomp(struct tlphy_softc *); 116 static void tlphy_status(struct tlphy_softc *); 117 118 static const struct mii_phydesc tlphys[] = { 119 MII_PHY_DESC(xxTI, TLAN10T), 120 MII_PHY_END 121 }; 122 123 static int 124 tlphy_probe(device_t dev) 125 { 126 127 return (mii_phy_dev_probe(dev, tlphys, BUS_PROBE_DEFAULT)); 128 } 129 130 static int 131 tlphy_attach(device_t dev) 132 { 133 device_t *devlist; 134 struct tlphy_softc *sc; 135 struct mii_softc *other; 136 struct mii_attach_args *ma; 137 struct mii_data *mii; 138 const char *sep = ""; 139 int capmask, devs, i; 140 141 sc = device_get_softc(dev); 142 ma = device_get_ivars(dev); 143 sc->sc_mii.mii_dev = device_get_parent(dev); 144 mii = device_get_softc(sc->sc_mii.mii_dev); 145 LIST_INSERT_HEAD(&mii->mii_phys, &sc->sc_mii, mii_list); 146 147 sc->sc_mii.mii_flags = miibus_get_flags(dev); 148 sc->sc_mii.mii_inst = mii->mii_instance; 149 sc->sc_mii.mii_phy = ma->mii_phyno; 150 sc->sc_mii.mii_service = tlphy_service; 151 sc->sc_mii.mii_pdata = mii; 152 153 capmask = BMSR_DEFCAPMASK; 154 if (mii->mii_instance && 155 device_get_children(sc->sc_mii.mii_dev, &devlist, &devs) == 0) { 156 for (i = 0; i < devs; i++) { 157 if (strcmp(device_get_name(devlist[i]), "tlphy")) { 158 other = device_get_softc(devlist[i]); 159 capmask &= ~other->mii_capabilities; 160 break; 161 } 162 } 163 free(devlist, M_TEMP); 164 } 165 166 mii->mii_instance++; 167 168 mii_phy_reset(&sc->sc_mii); 169 170 /* 171 * Note that if we're on a device that also supports 100baseTX, 172 * we are not going to want to use the built-in 10baseT port, 173 * since there will be another PHY on the MII wired up to the 174 * UTP connector. The parent indicates this to us by specifying 175 * the TLPHY_MEDIA_NO_10_T bit. 176 */ 177 sc->sc_mii.mii_capabilities = 178 PHY_READ(&sc->sc_mii, MII_BMSR) & capmask; 179 180 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL) 181 182 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->sc_mii.mii_inst), 183 BMCR_ISO); 184 185 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_LOOP, 186 sc->sc_mii.mii_inst), BMCR_LOOP); 187 188 #define PRINT(s) printf("%s%s", sep, s); sep = ", " 189 190 device_printf(dev, " "); 191 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_2, 0, sc->sc_mii.mii_inst), 0); 192 PRINT("10base2/BNC"); 193 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_5, 0, sc->sc_mii.mii_inst), 0); 194 PRINT("10base5/AUI"); 195 196 if (sc->sc_mii.mii_capabilities & BMSR_MEDIAMASK) { 197 printf("%s", sep); 198 mii_add_media(&sc->sc_mii); 199 } 200 201 printf("\n"); 202 #undef ADD 203 #undef PRINT 204 MIIBUS_MEDIAINIT(sc->sc_mii.mii_dev); 205 return (0); 206 } 207 208 static int 209 tlphy_service(struct mii_softc *self, struct mii_data *mii, int cmd) 210 { 211 struct tlphy_softc *sc = (struct tlphy_softc *)self; 212 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 213 int reg; 214 215 if (sc->sc_need_acomp) 216 tlphy_acomp(sc); 217 218 switch (cmd) { 219 case MII_POLLSTAT: 220 break; 221 222 case MII_MEDIACHG: 223 /* 224 * If the interface is not up, don't do anything. 225 */ 226 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 227 break; 228 229 switch (IFM_SUBTYPE(ife->ifm_media)) { 230 case IFM_AUTO: 231 /* 232 * The ThunderLAN PHY doesn't self-configure after 233 * an autonegotiation cycle, so there's no such 234 * thing as "already in auto mode". 235 */ 236 (void) tlphy_auto(sc); 237 break; 238 case IFM_10_2: 239 case IFM_10_5: 240 PHY_WRITE(&sc->sc_mii, MII_BMCR, 0); 241 PHY_WRITE(&sc->sc_mii, MII_TLPHY_CTRL, CTRL_AUISEL); 242 DELAY(100000); 243 break; 244 default: 245 PHY_WRITE(&sc->sc_mii, MII_TLPHY_CTRL, 0); 246 DELAY(100000); 247 PHY_WRITE(&sc->sc_mii, MII_ANAR, 248 mii_anar(ife->ifm_media)); 249 PHY_WRITE(&sc->sc_mii, MII_BMCR, ife->ifm_data); 250 } 251 break; 252 253 case MII_TICK: 254 /* 255 * Is the interface even up? 256 */ 257 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 258 return (0); 259 260 /* 261 * Only used for autonegotiation. 262 */ 263 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) 264 break; 265 266 /* 267 * Check to see if we have link. If we do, we don't 268 * need to restart the autonegotiation process. Read 269 * the BMSR twice in case it's latched. 270 * 271 * XXX WHAT ABOUT CHECKING LINK ON THE BNC/AUI?! 272 */ 273 reg = PHY_READ(&sc->sc_mii, MII_BMSR) | 274 PHY_READ(&sc->sc_mii, MII_BMSR); 275 if (reg & BMSR_LINK) 276 break; 277 278 /* 279 * Only retry autonegotiation every 5 seconds. 280 */ 281 if (++sc->sc_mii.mii_ticks <= MII_ANEGTICKS) 282 break; 283 284 sc->sc_mii.mii_ticks = 0; 285 mii_phy_reset(&sc->sc_mii); 286 tlphy_auto(sc); 287 return (0); 288 } 289 290 /* Update the media status. */ 291 tlphy_status(sc); 292 293 /* Callback if something changed. */ 294 mii_phy_update(&sc->sc_mii, cmd); 295 return (0); 296 } 297 298 static void 299 tlphy_status(struct tlphy_softc *sc) 300 { 301 struct mii_data *mii = sc->sc_mii.mii_pdata; 302 int bmsr, bmcr, tlctrl; 303 304 mii->mii_media_status = IFM_AVALID; 305 mii->mii_media_active = IFM_ETHER; 306 307 bmcr = PHY_READ(&sc->sc_mii, MII_BMCR); 308 if (bmcr & BMCR_ISO) { 309 mii->mii_media_active |= IFM_NONE; 310 mii->mii_media_status = 0; 311 return; 312 } 313 314 tlctrl = PHY_READ(&sc->sc_mii, MII_TLPHY_CTRL); 315 if (tlctrl & CTRL_AUISEL) { 316 mii->mii_media_status = 0; 317 mii->mii_media_active = mii->mii_media.ifm_cur->ifm_media; 318 return; 319 } 320 321 bmsr = PHY_READ(&sc->sc_mii, MII_BMSR) | 322 PHY_READ(&sc->sc_mii, MII_BMSR); 323 if (bmsr & BMSR_LINK) 324 mii->mii_media_status |= IFM_ACTIVE; 325 326 if (bmcr & BMCR_LOOP) 327 mii->mii_media_active |= IFM_LOOP; 328 329 /* 330 * Grr, braindead ThunderLAN PHY doesn't have any way to 331 * tell which media is actually active. (Note it also 332 * doesn't self-configure after autonegotiation.) We 333 * just have to report what's in the BMCR. 334 */ 335 if (bmcr & BMCR_FDX) 336 mii->mii_media_active |= IFM_FDX; 337 else 338 mii->mii_media_active |= IFM_HDX; 339 mii->mii_media_active |= IFM_10_T; 340 } 341 342 static int 343 tlphy_auto(struct tlphy_softc *sc) 344 { 345 int error; 346 347 switch ((error = mii_phy_auto(&sc->sc_mii))) { 348 case EIO: 349 /* 350 * Just assume we're not in full-duplex mode. 351 * XXX Check link and try AUI/BNC? 352 */ 353 PHY_WRITE(&sc->sc_mii, MII_BMCR, 0); 354 break; 355 356 case EJUSTRETURN: 357 /* Flag that we need to program when it completes. */ 358 sc->sc_need_acomp = 1; 359 break; 360 361 default: 362 tlphy_acomp(sc); 363 } 364 365 return (error); 366 } 367 368 static void 369 tlphy_acomp(struct tlphy_softc *sc) 370 { 371 int aner, anlpar; 372 373 sc->sc_need_acomp = 0; 374 375 /* 376 * Grr, braindead ThunderLAN PHY doesn't self-configure 377 * after autonegotiation. We have to do it ourselves 378 * based on the link partner status. 379 */ 380 381 aner = PHY_READ(&sc->sc_mii, MII_ANER); 382 if (aner & ANER_LPAN) { 383 anlpar = PHY_READ(&sc->sc_mii, MII_ANLPAR) & 384 PHY_READ(&sc->sc_mii, MII_ANAR); 385 if (anlpar & ANAR_10_FD) { 386 PHY_WRITE(&sc->sc_mii, MII_BMCR, BMCR_FDX); 387 return; 388 } 389 } 390 PHY_WRITE(&sc->sc_mii, MII_BMCR, 0); 391 } 392