1 /* $OpenBSD: tqphy.c,v 1.14 2008/10/25 00:10:21 brad Exp $ */ 2 /* $NetBSD: tqphy.c,v 1.9 2000/02/02 23:34:57 thorpej Exp $ */ 3 4 /* 5 * Copyright (c) 1998, 1999 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 * TDK TSC78Q2120 PHY driver 65 * 66 * Documentation available at http://www.tsc.tdk.com/lan/78Q2120.pdf . 67 */ 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 75 #include <net/if.h> 76 #include <net/if_media.h> 77 78 #include <dev/mii/mii.h> 79 #include <dev/mii/miivar.h> 80 #include <dev/mii/miidevs.h> 81 82 #include <dev/mii/tqphyreg.h> 83 84 int tqphymatch(struct device *, void *, void *); 85 void tqphyattach(struct device *, struct device *, void *); 86 int tqphydetach(struct device *, int); 87 88 struct cfattach tqphy_ca = { 89 sizeof(struct mii_softc), tqphymatch, tqphyattach, mii_phy_detach, 90 mii_phy_activate 91 }; 92 93 struct cfdriver tqphy_cd = { 94 NULL, "tqphy", DV_DULL 95 }; 96 97 int tqphy_service(struct mii_softc *, struct mii_data *, int); 98 void tqphy_status(struct mii_softc *); 99 100 const struct mii_phy_funcs tqphy_funcs = { 101 tqphy_service, tqphy_status, mii_phy_reset, 102 }; 103 104 static const struct mii_phydesc tqphys[] = { 105 { MII_OUI_TDK, MII_MODEL_TDK_78Q2120, 106 MII_STR_TDK_78Q2120 }, 107 108 { 0, 0, 109 NULL }, 110 }; 111 112 int 113 tqphymatch(struct device *parent, void *match, void *aux) 114 { 115 struct mii_attach_args *ma = aux; 116 117 if (mii_phy_match(ma, tqphys) != NULL) 118 return (10); 119 120 return (0); 121 } 122 123 void 124 tqphyattach(struct device *parent, struct device *self, void *aux) 125 { 126 struct mii_softc *sc = (struct mii_softc *)self; 127 struct mii_attach_args *ma = aux; 128 struct mii_data *mii = ma->mii_data; 129 const struct mii_phydesc *mpd; 130 131 mpd = mii_phy_match(ma, tqphys); 132 printf(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2)); 133 134 sc->mii_inst = mii->mii_instance; 135 sc->mii_phy = ma->mii_phyno; 136 sc->mii_funcs = &tqphy_funcs; 137 sc->mii_pdata = mii; 138 sc->mii_flags = ma->mii_flags; 139 140 sc->mii_flags |= MIIF_NOLOOP; 141 142 PHY_RESET(sc); 143 144 sc->mii_capabilities = 145 PHY_READ(sc, MII_BMSR) & ma->mii_capmask; 146 if (sc->mii_capabilities & BMSR_MEDIAMASK) 147 mii_phy_add_media(sc); 148 } 149 150 int 151 tqphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd) 152 { 153 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 154 int reg; 155 156 if ((sc->mii_dev.dv_flags & DVF_ACTIVE) == 0) 157 return (ENXIO); 158 159 switch (cmd) { 160 case MII_POLLSTAT: 161 /* 162 * If we're not polling our PHY instance, just return. 163 */ 164 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 165 return (0); 166 break; 167 168 case MII_MEDIACHG: 169 /* 170 * If the media indicates a different PHY instance, 171 * isolate ourselves. 172 */ 173 if (IFM_INST(ife->ifm_media) != sc->mii_inst) { 174 reg = PHY_READ(sc, MII_BMCR); 175 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO); 176 return (0); 177 } 178 179 /* 180 * If the interface is not up, don't do anything. 181 */ 182 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 183 break; 184 185 mii_phy_setmedia(sc); 186 break; 187 188 case MII_TICK: 189 /* 190 * If we're not currently selected, just return. 191 */ 192 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 193 return (0); 194 195 if (mii_phy_tick(sc) == EJUSTRETURN) 196 return (0); 197 break; 198 199 case MII_DOWN: 200 mii_phy_down(sc); 201 return (0); 202 } 203 204 /* Update the media status. */ 205 mii_phy_status(sc); 206 207 /* Callback if something changed. */ 208 mii_phy_update(sc, cmd); 209 return (0); 210 } 211 212 void 213 tqphy_status(struct mii_softc *sc) 214 { 215 struct mii_data *mii = sc->mii_pdata; 216 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 217 int bmsr, bmcr, diag; 218 219 mii->mii_media_status = IFM_AVALID; 220 mii->mii_media_active = IFM_ETHER; 221 222 bmsr = PHY_READ(sc, MII_BMSR) | 223 PHY_READ(sc, MII_BMSR); 224 if (bmsr & BMSR_LINK) 225 mii->mii_media_status |= IFM_ACTIVE; 226 227 bmcr = PHY_READ(sc, MII_BMCR); 228 if (bmcr & BMCR_ISO) { 229 mii->mii_media_active |= IFM_NONE; 230 mii->mii_media_status = 0; 231 return; 232 } 233 234 if (bmcr & BMCR_LOOP) 235 mii->mii_media_active |= IFM_LOOP; 236 237 if (bmcr & BMCR_AUTOEN) { 238 if ((bmsr & BMSR_ACOMP) == 0) { 239 /* Erg, still trying, I guess... */ 240 mii->mii_media_active |= IFM_NONE; 241 return; 242 } 243 diag = PHY_READ(sc, MII_TQPHY_DIAG); 244 if (diag & DIAG_RATE) 245 mii->mii_media_active |= IFM_100_TX; 246 else 247 mii->mii_media_active |= IFM_10_T; 248 249 if (diag & DIAG_DPLX) 250 mii->mii_media_active |= IFM_FDX; 251 else 252 mii->mii_media_active |= IFM_HDX; 253 } else 254 mii->mii_media_active = ife->ifm_media; 255 } 256