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 * $FreeBSD$ 29 */ 30 31 /* 32 * Driver for the TDK 78Q2120 MII 33 * 34 * References: 35 * Datasheet for the 78Q2120 - http://www.tsc.tdk.com/lan/78q2120.pdf 36 * Most of this code stolen from ukphy.c 37 */ 38 39 /* 40 * The TDK 78Q2120 is found on some Xircom X3201 based cardbus cards. It's just 41 * like any other normal phy, except it does auto negotiation in a different 42 * way. 43 */ 44 45 #include <sys/param.h> 46 #include <sys/systm.h> 47 #include <sys/kernel.h> 48 #include <sys/malloc.h> 49 #include <sys/socket.h> 50 #include <sys/errno.h> 51 #include <sys/module.h> 52 #include <sys/bus.h> 53 54 #include <net/if.h> 55 #include <net/if_media.h> 56 57 #include <machine/clock.h> 58 59 #include <dev/mii/mii.h> 60 #include <dev/mii/miivar.h> 61 #include <dev/mii/miidevs.h> 62 63 #include <dev/mii/tdkphyreg.h> 64 65 #include "miibus_if.h" 66 67 #if !defined(lint) 68 static const char rcsid[] = 69 "$Id: tdkphy.c,v 1.3 2000/10/14 06:20:56 jon Exp $"; 70 #endif 71 72 static int tdkphy_probe __P((device_t)); 73 static int tdkphy_attach __P((device_t)); 74 static int tdkphy_detach __P((device_t)); 75 76 static device_method_t tdkphy_methods[] = { 77 /* device interface */ 78 DEVMETHOD(device_probe, tdkphy_probe), 79 DEVMETHOD(device_attach, tdkphy_attach), 80 DEVMETHOD(device_detach, tdkphy_detach), 81 DEVMETHOD(device_shutdown, bus_generic_shutdown), 82 { 0, 0 } 83 }; 84 85 static devclass_t tdkphy_devclass; 86 87 static driver_t tdkphy_driver = { 88 "tdkphy", 89 tdkphy_methods, 90 sizeof(struct mii_softc) 91 }; 92 93 DRIVER_MODULE(tdkphy, miibus, tdkphy_driver, tdkphy_devclass, 0, 0); 94 95 int tdkphy_service __P((struct mii_softc *, struct mii_data *, int)); 96 void tdkphy_status __P((struct mii_softc *)); 97 98 static int 99 tdkphy_probe(device_t dev) 100 { 101 struct mii_attach_args *ma; 102 ma = device_get_ivars(dev); 103 if ((MII_OUI(ma->mii_id1, ma->mii_id2) != MII_OUI_TDK || 104 MII_MODEL(ma->mii_id2) != MII_MODEL_TDK_78Q2120)) 105 return (ENXIO); 106 107 device_set_desc(dev, MII_STR_TDK_78Q2120); 108 return (0); 109 } 110 111 static int 112 tdkphy_attach(device_t dev) 113 { 114 struct mii_softc *sc; 115 struct mii_attach_args *ma; 116 struct mii_data *mii; 117 sc = device_get_softc(dev); 118 ma = device_get_ivars(dev); 119 sc->mii_dev = device_get_parent(dev); 120 mii = device_get_softc(sc->mii_dev); 121 LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list); 122 123 if (bootverbose) 124 device_printf(dev, "OUI 0x%06x, model 0x%04x, rev. %d\n", 125 MII_OUI(ma->mii_id1, ma->mii_id2), 126 MII_MODEL(ma->mii_id2), MII_REV(ma->mii_id2)); 127 128 sc->mii_inst = mii->mii_instance; 129 sc->mii_phy = ma->mii_phyno; 130 sc->mii_service = tdkphy_service; 131 sc->mii_pdata = mii; 132 133 mii->mii_instance++; 134 135 sc->mii_flags |= MIIF_NOISOLATE; 136 137 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL) 138 139 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst), 140 BMCR_ISO); 141 #if 0 142 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->mii_inst), 143 BMCR_LOOP|BMCR_S100); 144 #endif 145 146 mii_phy_reset(sc); 147 148 sc->mii_capabilities = 149 PHY_READ(sc, MII_BMSR) & ma->mii_capmask; 150 device_printf(dev, " "); 151 if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0) 152 printf("no media present"); 153 else 154 mii_add_media(mii, sc->mii_capabilities, 155 sc->mii_inst); 156 printf("\n"); 157 #undef ADD 158 159 MIIBUS_MEDIAINIT(sc->mii_dev); 160 161 return(0); 162 } 163 164 static int tdkphy_detach(device_t dev) 165 { 166 struct mii_softc *sc; 167 struct mii_data *mii; 168 169 sc = device_get_softc(dev); 170 mii = device_get_softc(device_get_parent(dev)); 171 sc->mii_dev = NULL; 172 LIST_REMOVE(sc, mii_list); 173 174 return(0); 175 } 176 177 int 178 tdkphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd) 179 { 180 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 181 int reg; 182 183 switch (cmd) { 184 case MII_POLLSTAT: 185 /* 186 * If we're not polling our PHY instance, just return. 187 */ 188 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 189 return (0); 190 break; 191 192 case MII_MEDIACHG: 193 /* 194 * If the media indicates a different PHY instance, 195 * isolate ourselves. 196 */ 197 if (IFM_INST(ife->ifm_media) != sc->mii_inst) { 198 reg = PHY_READ(sc, MII_BMCR); 199 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO); 200 return (0); 201 } 202 203 /* 204 * If the interface is not up, don't do anything. 205 */ 206 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 207 break; 208 209 switch (IFM_SUBTYPE(ife->ifm_media)) { 210 case IFM_AUTO: 211 /* 212 * If we're already in auto mode, just return. 213 */ 214 if (PHY_READ(sc, MII_BMCR) & BMCR_AUTOEN) 215 return (0); 216 (void) mii_phy_auto(sc, 1); 217 break; 218 case IFM_100_T4: 219 /* 220 * Not supported on MII 221 */ 222 return (EINVAL); 223 default: 224 /* 225 * BMCR data is stored in the ifmedia entry. 226 */ 227 PHY_WRITE(sc, MII_ANAR, 228 mii_anar(ife->ifm_media)); 229 PHY_WRITE(sc, MII_BMCR, ife->ifm_data); 230 } 231 break; 232 233 case MII_TICK: 234 /* 235 * If we're not currently selected, just return. 236 */ 237 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 238 return (0); 239 240 /* 241 * Only used for autonegotiation. 242 */ 243 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) 244 return (0); 245 246 /* 247 * Is the interface even up? 248 */ 249 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 250 return (0); 251 252 /* 253 * Check to see if we have link. If we do, we don't 254 * need to restart the autonegotiation process. Read 255 * the BMSR twice in case it's latched. 256 */ 257 reg = PHY_READ(sc, MII_BMSR) | 258 PHY_READ(sc, MII_BMSR); 259 if (reg & BMSR_LINK) 260 return (0); 261 262 /* 263 * Only retry autonegotiation every 5 seconds. 264 */ 265 if (++sc->mii_ticks != 5) 266 return (0); 267 268 sc->mii_ticks = 0; 269 mii_phy_reset(sc); 270 if (mii_phy_auto(sc, 0) == EJUSTRETURN) 271 return (0); 272 break; 273 } 274 275 /* Update the media status. */ 276 tdkphy_status(sc); 277 if (sc->mii_pdata->mii_media_active & IFM_FDX) 278 PHY_WRITE(sc, MII_BMCR, PHY_READ(sc, MII_BMCR) | BMCR_FDX); 279 else 280 PHY_WRITE(sc, MII_BMCR, PHY_READ(sc, MII_BMCR) & ~BMCR_FDX); 281 /* Callback if something changed. */ 282 if (sc->mii_active != mii->mii_media_active || cmd == MII_MEDIACHG) { 283 MIIBUS_STATCHG(sc->mii_dev); 284 sc->mii_active = mii->mii_media_active; 285 } 286 return (0); 287 } 288 289 void 290 tdkphy_status(struct mii_softc *phy) 291 { 292 struct mii_data *mii = phy->mii_pdata; 293 int bmsr, bmcr, anlpar, diag; 294 295 mii->mii_media_status = IFM_AVALID; 296 mii->mii_media_active = IFM_ETHER; 297 298 bmsr = PHY_READ(phy, MII_BMSR) | PHY_READ(phy, MII_BMSR); 299 if (bmsr & BMSR_LINK) 300 mii->mii_media_status |= IFM_ACTIVE; 301 302 bmcr = PHY_READ(phy, MII_BMCR); 303 if (bmcr & BMCR_ISO) { 304 mii->mii_media_active |= IFM_NONE; 305 mii->mii_media_status = 0; 306 return; 307 } 308 309 if (bmcr & BMCR_LOOP) 310 mii->mii_media_active |= IFM_LOOP; 311 312 if (bmcr & BMCR_AUTOEN) { 313 /* 314 * NWay autonegotiation takes the highest-order common 315 * bit of the ANAR and ANLPAR (i.e. best media advertised 316 * both by us and our link partner). 317 */ 318 if ((bmsr & BMSR_ACOMP) == 0) { 319 /* Erg, still trying, I guess... */ 320 mii->mii_media_active |= IFM_NONE; 321 return; 322 } 323 324 anlpar = PHY_READ(phy, MII_ANAR) & PHY_READ(phy, MII_ANLPAR); 325 /* 326 * ANLPAR doesn't get set on my card, but we check it anyway, 327 * since it is mentioned in the 78Q2120 specs. 328 */ 329 if (anlpar & ANLPAR_T4) 330 mii->mii_media_active |= IFM_100_T4; 331 else if (anlpar & ANLPAR_TX_FD) 332 mii->mii_media_active |= IFM_100_TX|IFM_FDX; 333 else if (anlpar & ANLPAR_TX) 334 mii->mii_media_active |= IFM_100_TX; 335 else if (anlpar & ANLPAR_10_FD) 336 mii->mii_media_active |= IFM_10_T|IFM_FDX; 337 else if (anlpar & ANLPAR_10) 338 mii->mii_media_active |= IFM_10_T; 339 else { 340 /* 341 * ANLPAR isn't set, which leaves two possibilities: 342 * 1) Auto negotiation failed 343 * 2) Auto negotiation completed, but the card forgot 344 * to set ANLPAR. 345 * So we check the MII_DIAG(18) register... 346 */ 347 diag = PHY_READ(phy, MII_DIAG); 348 if (diag & DIAG_NEGFAIL) /* assume 10baseT if no neg */ 349 mii->mii_media_active |= IFM_10_T; 350 else { 351 if (diag & DIAG_DUPLEX) 352 mii->mii_media_active |= IFM_FDX; 353 if (diag & DIAG_RATE_100) 354 mii->mii_media_active |= IFM_100_TX; 355 else 356 mii->mii_media_active |= IFM_10_T; 357 } 358 } 359 } else { 360 mii->mii_media_active = mii_media_from_bmcr(bmcr); 361 } 362 } 363