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