1 /* 2 * Copyright (c) 2000,2001 Jonathan Chen. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions, and the following disclaimer, 10 * without modification, immediately at the beginning of the file. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in 13 * the documentation and/or other materials provided with the 14 * distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 /* 33 * Driver for the TDK 78Q2120 MII 34 * 35 * References: 36 * Datasheet for the 78Q2120 - http://www.tsc.tdk.com/lan/78q2120.pdf 37 * Most of this code stolen from ukphy.c 38 */ 39 40 /* 41 * The TDK 78Q2120 is found on some Xircom X3201 based cardbus cards. It's just 42 * like any other normal phy, except it does auto negotiation in a different 43 * way. 44 */ 45 46 #include <sys/cdefs.h> 47 __FBSDID("$FreeBSD$"); 48 49 #include <sys/param.h> 50 #include <sys/systm.h> 51 #include <sys/kernel.h> 52 #include <sys/socket.h> 53 #include <sys/errno.h> 54 #include <sys/module.h> 55 #include <sys/bus.h> 56 57 #include <net/if.h> 58 #include <net/if_media.h> 59 60 #include <machine/clock.h> 61 62 #include <dev/mii/mii.h> 63 #include <dev/mii/miivar.h> 64 #include "miidevs.h" 65 66 #include <dev/mii/tdkphyreg.h> 67 68 #include "miibus_if.h" 69 70 #if 0 71 #if !defined(lint) 72 static const char rcsid[] = 73 "$Id: tdkphy.c,v 1.3 2000/10/14 06:20:56 jon Exp $"; 74 #endif 75 #endif 76 77 static int tdkphy_probe(device_t); 78 static int tdkphy_attach(device_t); 79 80 static device_method_t tdkphy_methods[] = { 81 /* device interface */ 82 DEVMETHOD(device_probe, tdkphy_probe), 83 DEVMETHOD(device_attach, tdkphy_attach), 84 DEVMETHOD(device_detach, mii_phy_detach), 85 DEVMETHOD(device_shutdown, bus_generic_shutdown), 86 { 0, 0 } 87 }; 88 89 static devclass_t tdkphy_devclass; 90 91 static driver_t tdkphy_driver = { 92 "tdkphy", 93 tdkphy_methods, 94 sizeof(struct mii_softc) 95 }; 96 97 DRIVER_MODULE(tdkphy, miibus, tdkphy_driver, tdkphy_devclass, 0, 0); 98 99 static int tdkphy_service(struct mii_softc *, struct mii_data *, int); 100 static void tdkphy_status(struct mii_softc *); 101 102 static int 103 tdkphy_probe(device_t dev) 104 { 105 struct mii_attach_args *ma; 106 ma = device_get_ivars(dev); 107 if ((MII_OUI(ma->mii_id1, ma->mii_id2) != MII_OUI_TDK || 108 MII_MODEL(ma->mii_id2) != MII_MODEL_TDK_78Q2120)) 109 return (ENXIO); 110 111 device_set_desc(dev, MII_STR_TDK_78Q2120); 112 return (0); 113 } 114 115 static int 116 tdkphy_attach(device_t dev) 117 { 118 struct mii_softc *sc; 119 struct mii_attach_args *ma; 120 struct mii_data *mii; 121 sc = device_get_softc(dev); 122 ma = device_get_ivars(dev); 123 sc->mii_dev = device_get_parent(dev); 124 mii = device_get_softc(sc->mii_dev); 125 LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list); 126 127 if (bootverbose) 128 device_printf(dev, "OUI 0x%06x, model 0x%04x, rev. %d\n", 129 MII_OUI(ma->mii_id1, ma->mii_id2), 130 MII_MODEL(ma->mii_id2), MII_REV(ma->mii_id2)); 131 132 sc->mii_inst = mii->mii_instance; 133 sc->mii_phy = ma->mii_phyno; 134 sc->mii_service = tdkphy_service; 135 sc->mii_pdata = mii; 136 137 mii->mii_instance++; 138 139 sc->mii_flags |= MIIF_NOISOLATE; 140 141 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL) 142 143 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst), 144 BMCR_ISO); 145 #if 0 146 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->mii_inst), 147 BMCR_LOOP|BMCR_S100); 148 #endif 149 150 mii_phy_reset(sc); 151 152 sc->mii_capabilities = 153 PHY_READ(sc, MII_BMSR) & ma->mii_capmask; 154 device_printf(dev, " "); 155 mii_add_media(sc); 156 printf("\n"); 157 #undef ADD 158 159 MIIBUS_MEDIAINIT(sc->mii_dev); 160 161 return(0); 162 } 163 164 static int 165 tdkphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd) 166 { 167 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 168 int reg; 169 170 switch (cmd) { 171 case MII_POLLSTAT: 172 /* 173 * If we're not polling our PHY instance, just return. 174 */ 175 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 176 return (0); 177 break; 178 179 case MII_MEDIACHG: 180 /* 181 * If the media indicates a different PHY instance, 182 * isolate ourselves. 183 */ 184 if (IFM_INST(ife->ifm_media) != sc->mii_inst) { 185 reg = PHY_READ(sc, MII_BMCR); 186 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO); 187 return (0); 188 } 189 190 /* 191 * If the interface is not up, don't do anything. 192 */ 193 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 194 break; 195 196 switch (IFM_SUBTYPE(ife->ifm_media)) { 197 case IFM_AUTO: 198 /* 199 * If we're already in auto mode, just return. 200 */ 201 if (PHY_READ(sc, MII_BMCR) & BMCR_AUTOEN) 202 return (0); 203 (void) mii_phy_auto(sc); 204 break; 205 case IFM_100_T4: 206 /* 207 * Not supported on MII 208 */ 209 return (EINVAL); 210 default: 211 /* 212 * BMCR data is stored in the ifmedia entry. 213 */ 214 PHY_WRITE(sc, MII_ANAR, 215 mii_anar(ife->ifm_media)); 216 PHY_WRITE(sc, MII_BMCR, ife->ifm_data); 217 } 218 break; 219 220 case MII_TICK: 221 /* 222 * If we're not currently selected, just return. 223 */ 224 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 225 return (0); 226 if (mii_phy_tick(sc) == EJUSTRETURN) 227 return (0); 228 break; 229 } 230 231 /* Update the media status. */ 232 tdkphy_status(sc); 233 if (sc->mii_pdata->mii_media_active & IFM_FDX) 234 PHY_WRITE(sc, MII_BMCR, PHY_READ(sc, MII_BMCR) | BMCR_FDX); 235 else 236 PHY_WRITE(sc, MII_BMCR, PHY_READ(sc, MII_BMCR) & ~BMCR_FDX); 237 238 /* Callback if something changed. */ 239 mii_phy_update(sc, cmd); 240 return (0); 241 } 242 243 static void 244 tdkphy_status(struct mii_softc *phy) 245 { 246 struct mii_data *mii = phy->mii_pdata; 247 int bmsr, bmcr, anlpar, diag; 248 249 mii->mii_media_status = IFM_AVALID; 250 mii->mii_media_active = IFM_ETHER; 251 252 bmsr = PHY_READ(phy, MII_BMSR) | PHY_READ(phy, MII_BMSR); 253 if (bmsr & BMSR_LINK) 254 mii->mii_media_status |= IFM_ACTIVE; 255 256 bmcr = PHY_READ(phy, MII_BMCR); 257 if (bmcr & BMCR_ISO) { 258 mii->mii_media_active |= IFM_NONE; 259 mii->mii_media_status = 0; 260 return; 261 } 262 263 if (bmcr & BMCR_LOOP) 264 mii->mii_media_active |= IFM_LOOP; 265 266 if (bmcr & BMCR_AUTOEN) { 267 /* 268 * NWay autonegotiation takes the highest-order common 269 * bit of the ANAR and ANLPAR (i.e. best media advertised 270 * both by us and our link partner). 271 */ 272 if ((bmsr & BMSR_ACOMP) == 0) { 273 /* Erg, still trying, I guess... */ 274 mii->mii_media_active |= IFM_NONE; 275 return; 276 } 277 278 anlpar = PHY_READ(phy, MII_ANAR) & PHY_READ(phy, MII_ANLPAR); 279 /* 280 * ANLPAR doesn't get set on my card, but we check it anyway, 281 * since it is mentioned in the 78Q2120 specs. 282 */ 283 if (anlpar & ANLPAR_T4) 284 mii->mii_media_active |= IFM_100_T4; 285 else if (anlpar & ANLPAR_TX_FD) 286 mii->mii_media_active |= IFM_100_TX|IFM_FDX; 287 else if (anlpar & ANLPAR_TX) 288 mii->mii_media_active |= IFM_100_TX; 289 else if (anlpar & ANLPAR_10_FD) 290 mii->mii_media_active |= IFM_10_T|IFM_FDX; 291 else if (anlpar & ANLPAR_10) 292 mii->mii_media_active |= IFM_10_T; 293 else { 294 /* 295 * ANLPAR isn't set, which leaves two possibilities: 296 * 1) Auto negotiation failed 297 * 2) Auto negotiation completed, but the card forgot 298 * to set ANLPAR. 299 * So we check the MII_DIAG(18) register... 300 */ 301 diag = PHY_READ(phy, MII_DIAG); 302 if (diag & DIAG_NEGFAIL) /* assume 10baseT if no neg */ 303 mii->mii_media_active |= IFM_10_T; 304 else { 305 if (diag & DIAG_DUPLEX) 306 mii->mii_media_active |= IFM_FDX; 307 if (diag & DIAG_RATE_100) 308 mii->mii_media_active |= IFM_100_TX; 309 else 310 mii->mii_media_active |= IFM_10_T; 311 } 312 } 313 } else { 314 mii->mii_media_active = mii_media_from_bmcr(bmcr); 315 } 316 } 317