tdkphy.c revision 95665
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: head/sys/dev/mii/tdkphy.c 95665 2002-04-28 19:01:32Z phk $ 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) 68static const char rcsid[] = 69 "$Id: tdkphy.c,v 1.3 2000/10/14 06:20:56 jon Exp $"; 70#endif 71 72static int tdkphy_probe (device_t); 73static int tdkphy_attach (device_t); 74static int tdkphy_detach (device_t); 75 76static 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 85static devclass_t tdkphy_devclass; 86 87static driver_t tdkphy_driver = { 88 "tdkphy", 89 tdkphy_methods, 90 sizeof(struct mii_softc) 91}; 92 93DRIVER_MODULE(tdkphy, miibus, tdkphy_driver, tdkphy_devclass, 0, 0); 94 95static int tdkphy_service(struct mii_softc *, struct mii_data *, int); 96static void tdkphy_status(struct mii_softc *); 97 98static int 99tdkphy_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 111static int 112tdkphy_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 mii_add_media(mii, sc->mii_capabilities, sc->mii_inst); 152 printf("\n"); 153#undef ADD 154 155 MIIBUS_MEDIAINIT(sc->mii_dev); 156 157 return(0); 158} 159 160static int tdkphy_detach(device_t dev) 161{ 162 struct mii_softc *sc; 163 struct mii_data *mii; 164 165 sc = device_get_softc(dev); 166 mii = device_get_softc(device_get_parent(dev)); 167 mii_phy_auto_stop(sc); 168 sc->mii_dev = NULL; 169 LIST_REMOVE(sc, mii_list); 170 171 return(0); 172} 173 174static int 175tdkphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd) 176{ 177 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 178 int reg; 179 180 switch (cmd) { 181 case MII_POLLSTAT: 182 /* 183 * If we're not polling our PHY instance, just return. 184 */ 185 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 186 return (0); 187 break; 188 189 case MII_MEDIACHG: 190 /* 191 * If the media indicates a different PHY instance, 192 * isolate ourselves. 193 */ 194 if (IFM_INST(ife->ifm_media) != sc->mii_inst) { 195 reg = PHY_READ(sc, MII_BMCR); 196 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO); 197 return (0); 198 } 199 200 /* 201 * If the interface is not up, don't do anything. 202 */ 203 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 204 break; 205 206 switch (IFM_SUBTYPE(ife->ifm_media)) { 207 case IFM_AUTO: 208 /* 209 * If we're already in auto mode, just return. 210 */ 211 if (PHY_READ(sc, MII_BMCR) & BMCR_AUTOEN) 212 return (0); 213 (void) mii_phy_auto(sc, 1); 214 break; 215 case IFM_100_T4: 216 /* 217 * Not supported on MII 218 */ 219 return (EINVAL); 220 default: 221 /* 222 * BMCR data is stored in the ifmedia entry. 223 */ 224 PHY_WRITE(sc, MII_ANAR, 225 mii_anar(ife->ifm_media)); 226 PHY_WRITE(sc, MII_BMCR, ife->ifm_data); 227 } 228 break; 229 230 case MII_TICK: 231 /* 232 * If we're not currently selected, just return. 233 */ 234 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 235 return (0); 236 if (mii_phy_tick(sc) == EJUSTRETURN) 237 return (0); 238 break; 239 } 240 241 /* Update the media status. */ 242 tdkphy_status(sc); 243 if (sc->mii_pdata->mii_media_active & IFM_FDX) 244 PHY_WRITE(sc, MII_BMCR, PHY_READ(sc, MII_BMCR) | BMCR_FDX); 245 else 246 PHY_WRITE(sc, MII_BMCR, PHY_READ(sc, MII_BMCR) & ~BMCR_FDX); 247 248 /* Callback if something changed. */ 249 mii_phy_update(sc, cmd); 250 return (0); 251} 252 253static void 254tdkphy_status(struct mii_softc *phy) 255{ 256 struct mii_data *mii = phy->mii_pdata; 257 int bmsr, bmcr, anlpar, diag; 258 259 mii->mii_media_status = IFM_AVALID; 260 mii->mii_media_active = IFM_ETHER; 261 262 bmsr = PHY_READ(phy, MII_BMSR) | PHY_READ(phy, MII_BMSR); 263 if (bmsr & BMSR_LINK) 264 mii->mii_media_status |= IFM_ACTIVE; 265 266 bmcr = PHY_READ(phy, MII_BMCR); 267 if (bmcr & BMCR_ISO) { 268 mii->mii_media_active |= IFM_NONE; 269 mii->mii_media_status = 0; 270 return; 271 } 272 273 if (bmcr & BMCR_LOOP) 274 mii->mii_media_active |= IFM_LOOP; 275 276 if (bmcr & BMCR_AUTOEN) { 277 /* 278 * NWay autonegotiation takes the highest-order common 279 * bit of the ANAR and ANLPAR (i.e. best media advertised 280 * both by us and our link partner). 281 */ 282 if ((bmsr & BMSR_ACOMP) == 0) { 283 /* Erg, still trying, I guess... */ 284 mii->mii_media_active |= IFM_NONE; 285 return; 286 } 287 288 anlpar = PHY_READ(phy, MII_ANAR) & PHY_READ(phy, MII_ANLPAR); 289 /* 290 * ANLPAR doesn't get set on my card, but we check it anyway, 291 * since it is mentioned in the 78Q2120 specs. 292 */ 293 if (anlpar & ANLPAR_T4) 294 mii->mii_media_active |= IFM_100_T4; 295 else if (anlpar & ANLPAR_TX_FD) 296 mii->mii_media_active |= IFM_100_TX|IFM_FDX; 297 else if (anlpar & ANLPAR_TX) 298 mii->mii_media_active |= IFM_100_TX; 299 else if (anlpar & ANLPAR_10_FD) 300 mii->mii_media_active |= IFM_10_T|IFM_FDX; 301 else if (anlpar & ANLPAR_10) 302 mii->mii_media_active |= IFM_10_T; 303 else { 304 /* 305 * ANLPAR isn't set, which leaves two possibilities: 306 * 1) Auto negotiation failed 307 * 2) Auto negotiation completed, but the card forgot 308 * to set ANLPAR. 309 * So we check the MII_DIAG(18) register... 310 */ 311 diag = PHY_READ(phy, MII_DIAG); 312 if (diag & DIAG_NEGFAIL) /* assume 10baseT if no neg */ 313 mii->mii_media_active |= IFM_10_T; 314 else { 315 if (diag & DIAG_DUPLEX) 316 mii->mii_media_active |= IFM_FDX; 317 if (diag & DIAG_RATE_100) 318 mii->mii_media_active |= IFM_100_TX; 319 else 320 mii->mii_media_active |= IFM_10_T; 321 } 322 } 323 } else { 324 mii->mii_media_active = mii_media_from_bmcr(bmcr); 325 } 326} 327