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 (device_t); 73 static int tdkphy_attach (device_t); 74 static int tdkphy_detach (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 static int tdkphy_service(struct mii_softc *, struct mii_data *, int); 96 static void tdkphy_status(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 mii_phy_auto_stop(sc); 172 sc->mii_dev = NULL; 173 LIST_REMOVE(sc, mii_list); 174 175 return(0); 176 } 177 178 static int 179 tdkphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd) 180 { 181 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 182 int reg; 183 184 switch (cmd) { 185 case MII_POLLSTAT: 186 /* 187 * If we're not polling our PHY instance, just return. 188 */ 189 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 190 return (0); 191 break; 192 193 case MII_MEDIACHG: 194 /* 195 * If the media indicates a different PHY instance, 196 * isolate ourselves. 197 */ 198 if (IFM_INST(ife->ifm_media) != sc->mii_inst) { 199 reg = PHY_READ(sc, MII_BMCR); 200 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO); 201 return (0); 202 } 203 204 /* 205 * If the interface is not up, don't do anything. 206 */ 207 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 208 break; 209 210 switch (IFM_SUBTYPE(ife->ifm_media)) { 211 case IFM_AUTO: 212 /* 213 * If we're already in auto mode, just return. 214 */ 215 if (PHY_READ(sc, MII_BMCR) & BMCR_AUTOEN) 216 return (0); 217 (void) mii_phy_auto(sc, 1); 218 break; 219 case IFM_100_T4: 220 /* 221 * Not supported on MII 222 */ 223 return (EINVAL); 224 default: 225 /* 226 * BMCR data is stored in the ifmedia entry. 227 */ 228 PHY_WRITE(sc, MII_ANAR, 229 mii_anar(ife->ifm_media)); 230 PHY_WRITE(sc, MII_BMCR, ife->ifm_data); 231 } 232 break; 233 234 case MII_TICK: 235 /* 236 * If we're not currently selected, just return. 237 */ 238 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 239 return (0); 240 if (mii_phy_tick(sc) == EJUSTRETURN) 241 return (0); 242 break; 243 } 244 245 /* Update the media status. */ 246 tdkphy_status(sc); 247 if (sc->mii_pdata->mii_media_active & IFM_FDX) 248 PHY_WRITE(sc, MII_BMCR, PHY_READ(sc, MII_BMCR) | BMCR_FDX); 249 else 250 PHY_WRITE(sc, MII_BMCR, PHY_READ(sc, MII_BMCR) & ~BMCR_FDX); 251 252 /* Callback if something changed. */ 253 mii_phy_update(sc, cmd); 254 return (0); 255 } 256 257 static void 258 tdkphy_status(struct mii_softc *phy) 259 { 260 struct mii_data *mii = phy->mii_pdata; 261 int bmsr, bmcr, anlpar, diag; 262 263 mii->mii_media_status = IFM_AVALID; 264 mii->mii_media_active = IFM_ETHER; 265 266 bmsr = PHY_READ(phy, MII_BMSR) | PHY_READ(phy, MII_BMSR); 267 if (bmsr & BMSR_LINK) 268 mii->mii_media_status |= IFM_ACTIVE; 269 270 bmcr = PHY_READ(phy, MII_BMCR); 271 if (bmcr & BMCR_ISO) { 272 mii->mii_media_active |= IFM_NONE; 273 mii->mii_media_status = 0; 274 return; 275 } 276 277 if (bmcr & BMCR_LOOP) 278 mii->mii_media_active |= IFM_LOOP; 279 280 if (bmcr & BMCR_AUTOEN) { 281 /* 282 * NWay autonegotiation takes the highest-order common 283 * bit of the ANAR and ANLPAR (i.e. best media advertised 284 * both by us and our link partner). 285 */ 286 if ((bmsr & BMSR_ACOMP) == 0) { 287 /* Erg, still trying, I guess... */ 288 mii->mii_media_active |= IFM_NONE; 289 return; 290 } 291 292 anlpar = PHY_READ(phy, MII_ANAR) & PHY_READ(phy, MII_ANLPAR); 293 /* 294 * ANLPAR doesn't get set on my card, but we check it anyway, 295 * since it is mentioned in the 78Q2120 specs. 296 */ 297 if (anlpar & ANLPAR_T4) 298 mii->mii_media_active |= IFM_100_T4; 299 else if (anlpar & ANLPAR_TX_FD) 300 mii->mii_media_active |= IFM_100_TX|IFM_FDX; 301 else if (anlpar & ANLPAR_TX) 302 mii->mii_media_active |= IFM_100_TX; 303 else if (anlpar & ANLPAR_10_FD) 304 mii->mii_media_active |= IFM_10_T|IFM_FDX; 305 else if (anlpar & ANLPAR_10) 306 mii->mii_media_active |= IFM_10_T; 307 else { 308 /* 309 * ANLPAR isn't set, which leaves two possibilities: 310 * 1) Auto negotiation failed 311 * 2) Auto negotiation completed, but the card forgot 312 * to set ANLPAR. 313 * So we check the MII_DIAG(18) register... 314 */ 315 diag = PHY_READ(phy, MII_DIAG); 316 if (diag & DIAG_NEGFAIL) /* assume 10baseT if no neg */ 317 mii->mii_media_active |= IFM_10_T; 318 else { 319 if (diag & DIAG_DUPLEX) 320 mii->mii_media_active |= IFM_FDX; 321 if (diag & DIAG_RATE_100) 322 mii->mii_media_active |= IFM_100_TX; 323 else 324 mii->mii_media_active |= IFM_10_T; 325 } 326 } 327 } else { 328 mii->mii_media_active = mii_media_from_bmcr(bmcr); 329 } 330 } 331