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