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, 42 * also spotted on some 3C575 cards. It's just like any other normal 43 * phy, except it does auto negotiation in a different way. 44 */ 45 46 #include <sys/param.h> 47 #include <sys/systm.h> 48 #include <sys/kernel.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 58 #include <dev/mii/mii.h> 59 #include <dev/mii/miivar.h> 60 #include "miidevs.h" 61 62 #include <dev/mii/tdkphyreg.h> 63 64 #include "miibus_if.h" 65 66 #if 0 67 #if !defined(lint) 68 static const char rcsid[] = 69 "$Id: tdkphy.c,v 1.18 2006/05/16 14:36:28 phk Exp $"; 70 #endif 71 #endif 72 73 static int tdkphy_probe(device_t); 74 static int tdkphy_attach(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, mii_phy_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 (BUS_PROBE_DEFAULT); 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 mii_add_media(sc); 152 printf("\n"); 153 #undef ADD 154 155 MIIBUS_MEDIAINIT(sc->mii_dev); 156 157 return(0); 158 } 159 160 static int 161 tdkphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd) 162 { 163 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 164 int reg; 165 166 switch (cmd) { 167 case MII_POLLSTAT: 168 /* 169 * If we're not polling our PHY instance, just return. 170 */ 171 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 172 return (0); 173 break; 174 175 case MII_MEDIACHG: 176 /* 177 * If the media indicates a different PHY instance, 178 * isolate ourselves. 179 */ 180 if (IFM_INST(ife->ifm_media) != sc->mii_inst) { 181 reg = PHY_READ(sc, MII_BMCR); 182 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO); 183 return (0); 184 } 185 186 /* 187 * If the interface is not up, don't do anything. 188 */ 189 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 190 break; 191 192 switch (IFM_SUBTYPE(ife->ifm_media)) { 193 case IFM_AUTO: 194 /* 195 * If we're already in auto mode, just return. 196 */ 197 if (PHY_READ(sc, MII_BMCR) & BMCR_AUTOEN) 198 return (0); 199 (void) mii_phy_auto(sc); 200 break; 201 case IFM_100_T4: 202 /* 203 * Not supported on MII 204 */ 205 return (EINVAL); 206 default: 207 /* 208 * BMCR data is stored in the ifmedia entry. 209 */ 210 PHY_WRITE(sc, MII_ANAR, 211 mii_anar(ife->ifm_media)); 212 PHY_WRITE(sc, MII_BMCR, ife->ifm_data); 213 } 214 break; 215 216 case MII_TICK: 217 /* 218 * If we're not currently selected, just return. 219 */ 220 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 221 return (0); 222 if (mii_phy_tick(sc) == EJUSTRETURN) 223 return (0); 224 break; 225 } 226 227 /* Update the media status. */ 228 tdkphy_status(sc); 229 if (sc->mii_pdata->mii_media_active & IFM_FDX) 230 PHY_WRITE(sc, MII_BMCR, PHY_READ(sc, MII_BMCR) | BMCR_FDX); 231 else 232 PHY_WRITE(sc, MII_BMCR, PHY_READ(sc, MII_BMCR) & ~BMCR_FDX); 233 234 /* Callback if something changed. */ 235 mii_phy_update(sc, cmd); 236 return (0); 237 } 238 239 static void 240 tdkphy_status(struct mii_softc *phy) 241 { 242 struct mii_data *mii = phy->mii_pdata; 243 int bmsr, bmcr, anlpar, diag; 244 245 mii->mii_media_status = IFM_AVALID; 246 mii->mii_media_active = IFM_ETHER; 247 248 bmsr = PHY_READ(phy, MII_BMSR) | PHY_READ(phy, MII_BMSR); 249 if (bmsr & BMSR_LINK) 250 mii->mii_media_status |= IFM_ACTIVE; 251 252 bmcr = PHY_READ(phy, MII_BMCR); 253 if (bmcr & BMCR_ISO) { 254 mii->mii_media_active |= IFM_NONE; 255 mii->mii_media_status = 0; 256 return; 257 } 258 259 if (bmcr & BMCR_LOOP) 260 mii->mii_media_active |= IFM_LOOP; 261 262 if (bmcr & BMCR_AUTOEN) { 263 /* 264 * NWay autonegotiation takes the highest-order common 265 * bit of the ANAR and ANLPAR (i.e. best media advertised 266 * both by us and our link partner). 267 */ 268 if ((bmsr & BMSR_ACOMP) == 0) { 269 /* Erg, still trying, I guess... */ 270 mii->mii_media_active |= IFM_NONE; 271 return; 272 } 273 274 anlpar = PHY_READ(phy, MII_ANAR) & PHY_READ(phy, MII_ANLPAR); 275 /* 276 * ANLPAR doesn't get set on my card, but we check it anyway, 277 * since it is mentioned in the 78Q2120 specs. 278 */ 279 if (anlpar & ANLPAR_T4) 280 mii->mii_media_active |= IFM_100_T4; 281 else if (anlpar & ANLPAR_TX_FD) 282 mii->mii_media_active |= IFM_100_TX|IFM_FDX; 283 else if (anlpar & ANLPAR_TX) 284 mii->mii_media_active |= IFM_100_TX; 285 else if (anlpar & ANLPAR_10_FD) 286 mii->mii_media_active |= IFM_10_T|IFM_FDX; 287 else if (anlpar & ANLPAR_10) 288 mii->mii_media_active |= IFM_10_T; 289 else { 290 /* 291 * ANLPAR isn't set, which leaves two possibilities: 292 * 1) Auto negotiation failed 293 * 2) Auto negotiation completed, but the card forgot 294 * to set ANLPAR. 295 * So we check the MII_DIAG(18) register... 296 */ 297 diag = PHY_READ(phy, MII_DIAG); 298 if (diag & DIAG_NEGFAIL) /* assume 10baseT if no neg */ 299 mii->mii_media_active |= IFM_10_T; 300 else { 301 if (diag & DIAG_DUPLEX) 302 mii->mii_media_active |= IFM_FDX; 303 if (diag & DIAG_RATE_100) 304 mii->mii_media_active |= IFM_100_TX; 305 else 306 mii->mii_media_active |= IFM_10_T; 307 } 308 } 309 } else { 310 mii->mii_media_active = mii_media_from_bmcr(bmcr); 311 } 312 } 313