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