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