1 /* $NetBSD: tlphy.c,v 1.58 2008/11/17 03:04:27 dyoung Exp $ */ 2 3 /*- 4 * Copyright (c) 1998, 1999, 2000 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 * 3. All advertising materials mentioning features or use of this software 45 * must display the following acknowledgement: 46 * This product includes software developed by Manuel Bouyer. 47 * 4. The name of the author may not be used to endorse or promote products 48 * derived from this software without specific prior written permission. 49 * 50 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 51 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 52 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 53 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 54 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 55 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 56 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 57 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 58 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 59 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 60 */ 61 62 /* 63 * Driver for Texas Instruments's ThunderLAN PHYs 64 */ 65 66 #include <sys/cdefs.h> 67 __KERNEL_RCSID(0, "$NetBSD: tlphy.c,v 1.58 2008/11/17 03:04:27 dyoung Exp $"); 68 69 #include <sys/param.h> 70 #include <sys/systm.h> 71 #include <sys/kernel.h> 72 #include <sys/device.h> 73 #include <sys/socket.h> 74 #include <sys/errno.h> 75 76 #include <sys/bus.h> 77 78 #include <net/if.h> 79 #include <net/if_media.h> 80 81 #include <net/if_ether.h> 82 83 #include <dev/mii/mii.h> 84 #include <dev/mii/miivar.h> 85 #include <dev/mii/miidevs.h> 86 87 #include <dev/mii/tlphyreg.h> 88 #include <dev/mii/tlphyvar.h> 89 90 /* ThunderLAN PHY can only be on a ThunderLAN */ 91 #include <dev/pci/if_tlvar.h> 92 93 struct tlphy_softc { 94 struct mii_softc sc_mii; /* generic PHY */ 95 int sc_tlphycap; 96 int sc_need_acomp; 97 }; 98 99 static int tlphymatch(device_t, cfdata_t, void *); 100 static void tlphyattach(device_t, device_t, void *); 101 102 CFATTACH_DECL_NEW(tlphy, sizeof(struct tlphy_softc), 103 tlphymatch, tlphyattach, mii_phy_detach, mii_phy_activate); 104 105 static int tlphy_service(struct mii_softc *, struct mii_data *, int); 106 static int tlphy_auto(struct tlphy_softc *, int); 107 static void tlphy_acomp(struct tlphy_softc *); 108 static void tlphy_status(struct mii_softc *); 109 110 static const struct mii_phy_funcs tlphy_funcs = { 111 tlphy_service, tlphy_status, mii_phy_reset, 112 }; 113 114 static const struct mii_phydesc tlphys[] = { 115 { MII_OUI_TI, MII_MODEL_TI_TLAN10T, 116 MII_STR_TI_TLAN10T }, 117 118 { 0, 0, 119 NULL }, 120 }; 121 122 static int 123 tlphymatch(device_t parent, cfdata_t match, void *aux) 124 { 125 struct mii_attach_args *ma = aux; 126 127 if (mii_phy_match(ma, tlphys) != NULL) 128 return (10); 129 130 return (0); 131 } 132 133 static void 134 tlphyattach(device_t parent, device_t self, void *aux) 135 { 136 struct tlphy_softc *tsc = device_private(self); 137 struct mii_softc *sc = &tsc->sc_mii; 138 struct tl_softc *tlsc = device_private(device_parent(self)); 139 struct mii_attach_args *ma = aux; 140 struct mii_data *mii = ma->mii_data; 141 const struct mii_phydesc *mpd; 142 const char *sep = ""; 143 144 mpd = mii_phy_match(ma, tlphys); 145 aprint_naive(": Media interface\n"); 146 aprint_normal(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2)); 147 148 sc->mii_dev = self; 149 sc->mii_inst = mii->mii_instance; 150 sc->mii_phy = ma->mii_phyno; 151 sc->mii_funcs = &tlphy_funcs; 152 sc->mii_pdata = mii; 153 sc->mii_flags = ma->mii_flags; 154 sc->mii_anegticks = MII_ANEGTICKS; 155 156 PHY_RESET(sc); 157 158 /* 159 * Note that if we're on a device that also supports 100baseTX, 160 * we are not going to want to use the built-in 10baseT port, 161 * since there will be another PHY on the MII wired up to the 162 * UTP connector. The parent indicates this to us by specifying 163 * the TLPHY_MEDIA_NO_10_T bit. 164 */ 165 tsc->sc_tlphycap = tlsc->tl_product->tp_tlphymedia; 166 if ((tsc->sc_tlphycap & TLPHY_MEDIA_NO_10_T) == 0) 167 sc->mii_capabilities = 168 PHY_READ(sc, MII_BMSR) & ma->mii_capmask; 169 else 170 sc->mii_capabilities = 0; 171 172 173 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL) 174 #define PRINT(str) aprint_normal("%s%s", sep, str); sep = ", " 175 176 aprint_normal_dev(self, ""); 177 if (tsc->sc_tlphycap) { 178 if (tsc->sc_tlphycap & TLPHY_MEDIA_10_2) { 179 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_2, 0, 180 sc->mii_inst), 0); 181 PRINT("10base2"); 182 } else if (tsc->sc_tlphycap & TLPHY_MEDIA_10_5) { 183 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_5, 0, 184 sc->mii_inst), 0); 185 PRINT("10base5"); 186 } 187 } 188 if (sc->mii_capabilities & BMSR_MEDIAMASK) { 189 aprint_normal("%s", sep); 190 mii_phy_add_media(sc); 191 } else if ((tsc->sc_tlphycap & 192 (TLPHY_MEDIA_10_2 | TLPHY_MEDIA_10_5)) == 0) 193 aprint_error("no media present"); 194 else if (!pmf_device_register(self, NULL, mii_phy_resume)) { 195 aprint_normal("\n"); 196 aprint_error_dev(self, "couldn't establish power handler"); 197 } 198 aprint_normal("\n"); 199 #undef ADD 200 #undef PRINT 201 } 202 203 static int 204 tlphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd) 205 { 206 struct tlphy_softc *tsc = (struct tlphy_softc *)sc; 207 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 208 int reg; 209 210 if ((sc->mii_flags & MIIF_DOINGAUTO) == 0 && tsc->sc_need_acomp) 211 tlphy_acomp(tsc); 212 213 switch (cmd) { 214 case MII_POLLSTAT: 215 /* 216 * If we're not polling our PHY instance, just return. 217 */ 218 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 219 return (0); 220 break; 221 222 case MII_MEDIACHG: 223 /* 224 * If the media indicates a different PHY instance, 225 * isolate ourselves. 226 */ 227 if (IFM_INST(ife->ifm_media) != sc->mii_inst) { 228 reg = PHY_READ(sc, MII_BMCR); 229 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO); 230 return (0); 231 } 232 233 /* 234 * If the interface is not up, don't do anything. 235 */ 236 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 237 break; 238 239 switch (IFM_SUBTYPE(ife->ifm_media)) { 240 case IFM_AUTO: 241 /* 242 * The ThunderLAN PHY doesn't self-configure after 243 * an autonegotiation cycle, so there's no such 244 * thing as "already in auto mode". 245 */ 246 (void) tlphy_auto(tsc, 1); 247 break; 248 case IFM_10_2: 249 case IFM_10_5: 250 PHY_WRITE(sc, MII_BMCR, 0); 251 PHY_WRITE(sc, MII_TLPHY_CTRL, CTRL_AUISEL); 252 delay(100000); 253 break; 254 default: 255 PHY_WRITE(sc, MII_TLPHY_CTRL, 0); 256 delay(100000); 257 mii_phy_setmedia(sc); 258 } 259 break; 260 261 case MII_TICK: 262 /* 263 * If we're not currently selected, just return. 264 */ 265 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 266 return (0); 267 268 /* 269 * Only used for autonegotiation. 270 */ 271 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) 272 return (0); 273 274 /* 275 * Is the interface even up? 276 */ 277 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 278 return (0); 279 280 /* 281 * XXX WHAT ABOUT CHECKING LINK ON THE BNC/AUI?! 282 */ 283 284 if (mii_phy_tick(sc) == EJUSTRETURN) 285 return (0); 286 break; 287 288 case MII_DOWN: 289 mii_phy_down(sc); 290 return (0); 291 } 292 293 /* Update the media status. */ 294 mii_phy_status(sc); 295 296 /* Callback if something changed. */ 297 mii_phy_update(sc, cmd); 298 return (0); 299 } 300 301 static void 302 tlphy_status(struct mii_softc *sc) 303 { 304 struct tlphy_softc *tsc = (struct tlphy_softc *)sc; 305 struct mii_data *mii = sc->mii_pdata; 306 int bmsr, bmcr, tlctrl; 307 308 mii->mii_media_status = IFM_AVALID; 309 mii->mii_media_active = IFM_ETHER; 310 311 bmcr = PHY_READ(sc, MII_BMCR); 312 if (bmcr & BMCR_ISO) { 313 mii->mii_media_active |= IFM_NONE; 314 mii->mii_media_status = 0; 315 return; 316 } 317 318 tlctrl = PHY_READ(sc, MII_TLPHY_CTRL); 319 if (tlctrl & CTRL_AUISEL) { 320 if (tsc->sc_tlphycap & TLPHY_MEDIA_10_2) 321 mii->mii_media_active |= IFM_10_2; 322 else if (tsc->sc_tlphycap & TLPHY_MEDIA_10_5) 323 mii->mii_media_active |= IFM_10_5; 324 else 325 printf("%s: AUI selected with no matching media !\n", 326 device_xname(sc->mii_dev)); 327 mii->mii_media_status |= IFM_ACTIVE; 328 return; 329 } 330 331 bmsr = PHY_READ(sc, MII_BMSR) | 332 PHY_READ(sc, MII_BMSR); 333 if (bmsr & BMSR_LINK) 334 mii->mii_media_status |= IFM_ACTIVE; 335 336 if (bmcr & BMCR_LOOP) 337 mii->mii_media_active |= IFM_LOOP; 338 339 /* 340 * Grr, braindead ThunderLAN PHY doesn't have any way to 341 * tell which media is actually active. (Note it also 342 * doesn't self-configure after autonegotiation.) We 343 * just have to report what's in the BMCR. 344 */ 345 if (bmcr & BMCR_FDX) 346 mii->mii_media_active |= IFM_FDX; 347 mii->mii_media_active |= IFM_10_T; 348 } 349 350 static int 351 tlphy_auto(struct tlphy_softc *tsc, int waitfor) 352 { 353 struct mii_softc *sc = &tsc->sc_mii; 354 int error; 355 356 switch ((error = mii_phy_auto(sc, waitfor))) { 357 case EIO: 358 /* 359 * Just assume we're not in full-duplex mode. 360 * XXX Check link and try AUI/BNC? 361 */ 362 PHY_WRITE(sc, MII_BMCR, 0); 363 break; 364 365 case EJUSTRETURN: 366 /* Flag that we need to program when it completes. */ 367 tsc->sc_need_acomp = 1; 368 break; 369 370 default: 371 tlphy_acomp(tsc); 372 } 373 374 return (error); 375 } 376 377 static void 378 tlphy_acomp(struct tlphy_softc *tsc) 379 { 380 struct mii_softc *sc = &tsc->sc_mii; 381 int aner, anlpar; 382 383 tsc->sc_need_acomp = 0; 384 385 /* 386 * Grr, braindead ThunderLAN PHY doesn't self-configure 387 * after autonegotiation. We have to do it ourselves 388 * based on the link partner status. 389 */ 390 391 aner = PHY_READ(sc, MII_ANER); 392 if (aner & ANER_LPAN) { 393 anlpar = PHY_READ(sc, MII_ANLPAR) & 394 PHY_READ(sc, MII_ANAR); 395 if (anlpar & ANAR_10_FD) { 396 PHY_WRITE(sc, MII_BMCR, BMCR_FDX); 397 return; 398 } 399 } 400 PHY_WRITE(sc, MII_BMCR, 0); 401 } 402