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 75 static device_method_t tdkphy_methods[] = { 76 /* device interface */ 77 DEVMETHOD(device_probe, tdkphy_probe), 78 DEVMETHOD(device_attach, tdkphy_attach), 79 DEVMETHOD(device_detach, mii_phy_detach), 80 DEVMETHOD(device_shutdown, bus_generic_shutdown), 81 { 0, 0 } 82 }; 83 84 static devclass_t tdkphy_devclass; 85 86 static driver_t tdkphy_driver = { 87 "tdkphy", 88 tdkphy_methods, 89 sizeof(struct mii_softc) 90 }; 91 92 DRIVER_MODULE(tdkphy, miibus, tdkphy_driver, tdkphy_devclass, 0, 0); 93 94 static int tdkphy_service(struct mii_softc *, struct mii_data *, int); 95 static void tdkphy_status(struct mii_softc *); 96 97 static int 98 tdkphy_probe(device_t dev) 99 { 100 struct mii_attach_args *ma; 101 ma = device_get_ivars(dev); 102 if ((MII_OUI(ma->mii_id1, ma->mii_id2) != MII_OUI_TDK || 103 MII_MODEL(ma->mii_id2) != MII_MODEL_TDK_78Q2120)) 104 return (ENXIO); 105 106 device_set_desc(dev, MII_STR_TDK_78Q2120); 107 return (0); 108 } 109 110 static int 111 tdkphy_attach(device_t dev) 112 { 113 struct mii_softc *sc; 114 struct mii_attach_args *ma; 115 struct mii_data *mii; 116 sc = device_get_softc(dev); 117 ma = device_get_ivars(dev); 118 sc->mii_dev = device_get_parent(dev); 119 mii = device_get_softc(sc->mii_dev); 120 LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list); 121 122 if (bootverbose) 123 device_printf(dev, "OUI 0x%06x, model 0x%04x, rev. %d\n", 124 MII_OUI(ma->mii_id1, ma->mii_id2), 125 MII_MODEL(ma->mii_id2), MII_REV(ma->mii_id2)); 126 127 sc->mii_inst = mii->mii_instance; 128 sc->mii_phy = ma->mii_phyno; 129 sc->mii_service = tdkphy_service; 130 sc->mii_pdata = mii; 131 132 mii->mii_instance++; 133 134 sc->mii_flags |= MIIF_NOISOLATE; 135 136 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL) 137 138 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst), 139 BMCR_ISO); 140 #if 0 141 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->mii_inst), 142 BMCR_LOOP|BMCR_S100); 143 #endif 144 145 mii_phy_reset(sc); 146 147 sc->mii_capabilities = 148 PHY_READ(sc, MII_BMSR) & ma->mii_capmask; 149 device_printf(dev, " "); 150 mii_add_media(sc); 151 printf("\n"); 152 #undef ADD 153 154 MIIBUS_MEDIAINIT(sc->mii_dev); 155 156 return(0); 157 } 158 159 static int 160 tdkphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd) 161 { 162 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 163 int reg; 164 165 switch (cmd) { 166 case MII_POLLSTAT: 167 /* 168 * If we're not polling our PHY instance, just return. 169 */ 170 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 171 return (0); 172 break; 173 174 case MII_MEDIACHG: 175 /* 176 * If the media indicates a different PHY instance, 177 * isolate ourselves. 178 */ 179 if (IFM_INST(ife->ifm_media) != sc->mii_inst) { 180 reg = PHY_READ(sc, MII_BMCR); 181 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO); 182 return (0); 183 } 184 185 /* 186 * If the interface is not up, don't do anything. 187 */ 188 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 189 break; 190 191 switch (IFM_SUBTYPE(ife->ifm_media)) { 192 case IFM_AUTO: 193 /* 194 * If we're already in auto mode, just return. 195 */ 196 if (PHY_READ(sc, MII_BMCR) & BMCR_AUTOEN) 197 return (0); 198 (void) mii_phy_auto(sc); 199 break; 200 case IFM_100_T4: 201 /* 202 * Not supported on MII 203 */ 204 return (EINVAL); 205 default: 206 /* 207 * BMCR data is stored in the ifmedia entry. 208 */ 209 PHY_WRITE(sc, MII_ANAR, 210 mii_anar(ife->ifm_media)); 211 PHY_WRITE(sc, MII_BMCR, ife->ifm_data); 212 } 213 break; 214 215 case MII_TICK: 216 /* 217 * If we're not currently selected, just return. 218 */ 219 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 220 return (0); 221 if (mii_phy_tick(sc) == EJUSTRETURN) 222 return (0); 223 break; 224 } 225 226 /* Update the media status. */ 227 tdkphy_status(sc); 228 if (sc->mii_pdata->mii_media_active & IFM_FDX) 229 PHY_WRITE(sc, MII_BMCR, PHY_READ(sc, MII_BMCR) | BMCR_FDX); 230 else 231 PHY_WRITE(sc, MII_BMCR, PHY_READ(sc, MII_BMCR) & ~BMCR_FDX); 232 233 /* Callback if something changed. */ 234 mii_phy_update(sc, cmd); 235 return (0); 236 } 237 238 static void 239 tdkphy_status(struct mii_softc *phy) 240 { 241 struct mii_data *mii = phy->mii_pdata; 242 int bmsr, bmcr, anlpar, diag; 243 244 mii->mii_media_status = IFM_AVALID; 245 mii->mii_media_active = IFM_ETHER; 246 247 bmsr = PHY_READ(phy, MII_BMSR) | PHY_READ(phy, MII_BMSR); 248 if (bmsr & BMSR_LINK) 249 mii->mii_media_status |= IFM_ACTIVE; 250 251 bmcr = PHY_READ(phy, MII_BMCR); 252 if (bmcr & BMCR_ISO) { 253 mii->mii_media_active |= IFM_NONE; 254 mii->mii_media_status = 0; 255 return; 256 } 257 258 if (bmcr & BMCR_LOOP) 259 mii->mii_media_active |= IFM_LOOP; 260 261 if (bmcr & BMCR_AUTOEN) { 262 /* 263 * NWay autonegotiation takes the highest-order common 264 * bit of the ANAR and ANLPAR (i.e. best media advertised 265 * both by us and our link partner). 266 */ 267 if ((bmsr & BMSR_ACOMP) == 0) { 268 /* Erg, still trying, I guess... */ 269 mii->mii_media_active |= IFM_NONE; 270 return; 271 } 272 273 anlpar = PHY_READ(phy, MII_ANAR) & PHY_READ(phy, MII_ANLPAR); 274 /* 275 * ANLPAR doesn't get set on my card, but we check it anyway, 276 * since it is mentioned in the 78Q2120 specs. 277 */ 278 if (anlpar & ANLPAR_T4) 279 mii->mii_media_active |= IFM_100_T4; 280 else if (anlpar & ANLPAR_TX_FD) 281 mii->mii_media_active |= IFM_100_TX|IFM_FDX; 282 else if (anlpar & ANLPAR_TX) 283 mii->mii_media_active |= IFM_100_TX; 284 else if (anlpar & ANLPAR_10_FD) 285 mii->mii_media_active |= IFM_10_T|IFM_FDX; 286 else if (anlpar & ANLPAR_10) 287 mii->mii_media_active |= IFM_10_T; 288 else { 289 /* 290 * ANLPAR isn't set, which leaves two possibilities: 291 * 1) Auto negotiation failed 292 * 2) Auto negotiation completed, but the card forgot 293 * to set ANLPAR. 294 * So we check the MII_DIAG(18) register... 295 */ 296 diag = PHY_READ(phy, MII_DIAG); 297 if (diag & DIAG_NEGFAIL) /* assume 10baseT if no neg */ 298 mii->mii_media_active |= IFM_10_T; 299 else { 300 if (diag & DIAG_DUPLEX) 301 mii->mii_media_active |= IFM_FDX; 302 if (diag & DIAG_RATE_100) 303 mii->mii_media_active |= IFM_100_TX; 304 else 305 mii->mii_media_active |= IFM_10_T; 306 } 307 } 308 } else { 309 mii->mii_media_active = mii_media_from_bmcr(bmcr); 310 } 311 } 312