dcphy.c revision 1.4
1/* $OpenBSD: dcphy.c,v 1.4 2000/09/07 20:10:21 aaron Exp $ */ 2 3/* 4 * Copyright (c) 1997, 1998, 1999 5 * Bill Paul <wpaul@ee.columbia.edu>. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by Bill Paul. 18 * 4. Neither the name of the author nor the names of any co-contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD 26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 32 * THE POSSIBILITY OF SUCH DAMAGE. 33 * 34 * $FreeBSD: src/sys/dev/mii/dcphy.c,v 1.5 2000/06/05 19:37:15 wpaul Exp $ 35 */ 36 37/* 38 * Pseudo-driver for internal NWAY support on DEC 21143 and workalike 39 * controllers. Technically we're abusing the miibus code to handle 40 * media selection and NWAY support here since there is no MII 41 * interface. However the logical operations are roughly the same, 42 * and the alternative is to create a fake MII interface in the driver, 43 * which is harder to do. 44 */ 45 46#include <sys/param.h> 47#include <sys/device.h> 48#include <sys/systm.h> 49#include <sys/kernel.h> 50#include <sys/socket.h> 51#include <sys/errno.h> 52#include <sys/socket.h> 53 54#include <net/if.h> 55#include <net/if_dl.h> 56#include <net/if_types.h> 57#include <net/if_media.h> 58#include <netinet/in.h> 59#include <netinet/if_ether.h> 60 61#include <dev/mii/mii.h> 62#include <dev/mii/miivar.h> 63#include <dev/mii/miidevs.h> 64 65#include <machine/bus.h> 66 67#include <dev/pci/pcivar.h> 68 69#include <dev/ic/dcreg.h> 70 71#define DC_SETBIT(sc, reg, x) \ 72 CSR_WRITE_4(sc, reg, \ 73 CSR_READ_4(sc, reg) | x) 74 75#define DC_CLRBIT(sc, reg, x) \ 76 CSR_WRITE_4(sc, reg, \ 77 CSR_READ_4(sc, reg) & ~x) 78 79#define MIIF_AUTOTIMEOUT 0x0004 80 81/* 82 * This is the subsystem ID for the built-in 21143 ethernet 83 * in several Compaq Presario systems. Apparently these are 84 * 10Mbps only, so we need to treat them specially. 85 */ 86#define COMPAQ_PRESARIO_ID 0xb0bb0e11 87 88int dcphy_match __P((struct device *, void *, void *)); 89void dcphy_attach __P((struct device *, struct device *, void *)); 90 91struct cfattach dcphy_ca = { 92 sizeof(struct mii_softc), dcphy_match, dcphy_attach, mii_phy_detach, 93 mii_phy_activate 94}; 95 96struct cfdriver dcphy_cd = { 97 NULL, "dcphy", DV_DULL 98}; 99 100int dcphy_service __P((struct mii_softc *, struct mii_data *, int)); 101void dcphy_status __P((struct mii_softc *)); 102int dcphy_auto __P((struct mii_softc *, int)); 103void dcphy_reset __P((struct mii_softc *)); 104 105int 106dcphy_match(parent, match, aux) 107 struct device *parent; 108 void *match, *aux; 109{ 110 struct mii_attach_args *ma = aux; 111 112 /* 113 * The dc driver will report the 21143 vendor and device 114 * ID to let us know that it wants us to attach. 115 */ 116 if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_xxDEC && 117 MII_MODEL(ma->mii_id2) == MII_MODEL_xxDEC_xxDC) 118 return (10); 119 120 return (0); 121} 122 123void 124dcphy_attach(parent, self, aux) 125 struct device *parent; 126 struct device *self; 127 void *aux; 128{ 129 struct mii_softc *sc = (struct mii_softc *)self; 130 struct mii_attach_args *ma = aux; 131 struct mii_data *mii = ma->mii_data; 132 struct dc_softc *dc_sc; 133 134 printf(": internal PHY\n"); 135 sc->mii_inst = mii->mii_instance; 136 sc->mii_phy = ma->mii_phyno; 137 sc->mii_service = dcphy_service; 138 sc->mii_status = dcphy_status; 139 sc->mii_pdata = mii; 140 sc->mii_flags = mii->mii_flags; 141 142 sc->mii_flags |= MIIF_NOISOLATE; 143 mii->mii_instance++; 144 145 dc_sc = mii->mii_ifp->if_softc; 146 CSR_WRITE_4(dc_sc, DC_10BTSTAT, 0); 147 CSR_WRITE_4(dc_sc, DC_10BTCTRL, 0); 148 149#define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL) 150 151 switch(dc_sc->dc_csid) { 152 case COMPAQ_PRESARIO_ID: 153 /* Example of how to only allow 10Mbps modes. */ 154 sc->mii_capabilities = BMSR_ANEG|BMSR_10TFDX|BMSR_10THDX; 155 break; 156 default: 157 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, 158 sc->mii_inst), BMCR_LOOP|BMCR_S100); 159 160 sc->mii_capabilities = 161 BMSR_ANEG|BMSR_100TXFDX|BMSR_100TXHDX| 162 BMSR_10TFDX|BMSR_10THDX; 163 break; 164 } 165 166 sc->mii_capabilities &= ma->mii_capmask; 167 if (sc->mii_capabilities & BMSR_MEDIAMASK) 168 mii_phy_add_media(sc); 169#undef ADD 170} 171 172int 173dcphy_service(sc, mii, cmd) 174 struct mii_softc *sc; 175 struct mii_data *mii; 176 int cmd; 177{ 178 struct dc_softc *dc_sc; 179 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 180 int reg; 181 u_int32_t mode; 182 183 if ((sc->mii_dev.dv_flags & DVF_ACTIVE) == 0) 184 return (ENXIO); 185 186 dc_sc = mii->mii_ifp->if_softc; 187 188 switch (cmd) { 189 case MII_POLLSTAT: 190 /* 191 * If we're not polling our PHY instance, just return. 192 */ 193 if (IFM_INST(ife->ifm_media) != sc->mii_inst) { 194 return (0); 195 } 196 break; 197 198 case MII_MEDIACHG: 199 /* 200 * If the media indicates a different PHY instance, 201 * isolate ourselves. 202 */ 203 if (IFM_INST(ife->ifm_media) != sc->mii_inst) { 204 return (0); 205 } 206 207 /* 208 * If the interface is not up, don't do anything. 209 */ 210 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 211 break; 212 213 sc->mii_flags = 0; 214 mii->mii_media_active = IFM_NONE; 215 mode = CSR_READ_4(dc_sc, DC_NETCFG); 216 mode &= ~(DC_NETCFG_FULLDUPLEX|DC_NETCFG_PORTSEL| 217 DC_NETCFG_PCS|DC_NETCFG_SCRAMBLER|DC_NETCFG_SPEEDSEL); 218 219 switch (IFM_SUBTYPE(ife->ifm_media)) { 220 case IFM_AUTO: 221 /*dcphy_reset(sc);*/ 222 sc->mii_flags &= ~MIIF_DOINGAUTO; 223 (void) dcphy_auto(sc, 0); 224 break; 225 case IFM_100_T4: 226 /* 227 * XXX Not supported as a manual setting right now. 228 */ 229 return (EINVAL); 230 case IFM_100_TX: 231 dcphy_reset(sc); 232 DC_CLRBIT(dc_sc, DC_10BTCTRL, DC_TCTL_AUTONEGENBL); 233 mode |= DC_NETCFG_PORTSEL|DC_NETCFG_PCS| 234 DC_NETCFG_SCRAMBLER; 235 if ((ife->ifm_media & IFM_GMASK) == IFM_FDX) 236 mode |= DC_NETCFG_FULLDUPLEX; 237 else 238 mode &= ~DC_NETCFG_FULLDUPLEX; 239 CSR_WRITE_4(dc_sc, DC_NETCFG, mode); 240 break; 241 case IFM_10_T: 242 DC_CLRBIT(dc_sc, DC_SIARESET, DC_SIA_RESET); 243 DC_CLRBIT(dc_sc, DC_10BTCTRL, 0xFFFF); 244 if ((ife->ifm_media & IFM_GMASK) == IFM_FDX) 245 DC_SETBIT(dc_sc, DC_10BTCTRL, 0x7F3D); 246 else 247 DC_SETBIT(dc_sc, DC_10BTCTRL, 0x7F3F); 248 DC_SETBIT(dc_sc, DC_SIARESET, DC_SIA_RESET); 249 DC_CLRBIT(dc_sc, DC_10BTCTRL, DC_TCTL_AUTONEGENBL); 250 mode &= ~DC_NETCFG_PORTSEL; 251 mode |= DC_NETCFG_SPEEDSEL; 252 if ((ife->ifm_media & IFM_GMASK) == IFM_FDX) 253 mode |= DC_NETCFG_FULLDUPLEX; 254 else 255 mode &= ~DC_NETCFG_FULLDUPLEX; 256 CSR_WRITE_4(dc_sc, DC_NETCFG, mode); 257 break; 258 default: 259 return(EINVAL); 260 break; 261 } 262 break; 263 264 case MII_TICK: 265 /* 266 * If we're not currently selected, just return. 267 */ 268 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 269 return (0); 270 271 /* 272 * Only used for autonegotiation. 273 */ 274 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) 275 return (0); 276 277 /* 278 * Is the interface even up? 279 */ 280 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 281 return (0); 282 283 reg = CSR_READ_4(dc_sc, DC_10BTSTAT) & 284 (DC_TSTAT_LS10|DC_TSTAT_LS100); 285 286 if (!(reg & DC_TSTAT_LS10) || !(reg & DC_TSTAT_LS100)) 287 return (0); 288 289 /* 290 * Only retry autonegotiation every 5 seconds. 291 */ 292 if (++sc->mii_ticks != 50) 293 return (0); 294 295 sc->mii_ticks = 0; 296 /*if (DC_IS_INTEL(dc_sc))*/ 297 sc->mii_flags &= ~MIIF_DOINGAUTO; 298 dcphy_auto(sc, 0); 299 300 break; 301 } 302 303 /* Update the media status. */ 304 mii_phy_status(sc); 305 306 /* Callback if something changed. */ 307 mii_phy_update(sc, cmd); 308 return (0); 309} 310 311void 312dcphy_status(sc) 313 struct mii_softc *sc; 314{ 315 struct mii_data *mii = sc->mii_pdata; 316 int reg, anlpar, tstat = 0; 317 struct dc_softc *dc_sc; 318 319 dc_sc = mii->mii_ifp->if_softc; 320 321 mii->mii_media_status = IFM_AVALID; 322 mii->mii_media_active = IFM_ETHER; 323 324 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 325 return; 326 327 reg = CSR_READ_4(dc_sc, DC_10BTSTAT) & 328 (DC_TSTAT_LS10|DC_TSTAT_LS100); 329 330 if (!(reg & DC_TSTAT_LS10) || !(reg & DC_TSTAT_LS100)) 331 mii->mii_media_status |= IFM_ACTIVE; 332 333 if (CSR_READ_4(dc_sc, DC_10BTCTRL) & DC_TCTL_AUTONEGENBL) { 334 /* Erg, still trying, I guess... */ 335 tstat = CSR_READ_4(dc_sc, DC_10BTSTAT); 336 if ((tstat & DC_TSTAT_ANEGSTAT) != DC_ASTAT_AUTONEGCMP) { 337 if ((DC_IS_MACRONIX(dc_sc) || DC_IS_PNICII(dc_sc)) && 338 (tstat & DC_TSTAT_ANEGSTAT) == DC_ASTAT_DISABLE) 339 goto skip; 340 mii->mii_media_active |= IFM_NONE; 341 return; 342 } 343 344 if (tstat & DC_TSTAT_LP_CAN_NWAY) { 345 anlpar = tstat >> 16; 346 if (anlpar & ANLPAR_T4 && 347 sc->mii_capabilities & BMSR_100TXHDX) 348 mii->mii_media_active |= IFM_100_T4; 349 else if (anlpar & ANLPAR_TX_FD && 350 sc->mii_capabilities & BMSR_100TXFDX) 351 mii->mii_media_active |= IFM_100_TX|IFM_FDX; 352 else if (anlpar & ANLPAR_TX && 353 sc->mii_capabilities & BMSR_100TXHDX) 354 mii->mii_media_active |= IFM_100_TX; 355 else if (anlpar & ANLPAR_10_FD) 356 mii->mii_media_active |= IFM_10_T|IFM_FDX; 357 else if (anlpar & ANLPAR_10) 358 mii->mii_media_active |= IFM_10_T; 359 else 360 mii->mii_media_active |= IFM_NONE; 361 if (DC_IS_INTEL(dc_sc)) 362 DC_CLRBIT(dc_sc, DC_10BTCTRL, 363 DC_TCTL_AUTONEGENBL); 364 return; 365 } 366 /* 367 * If the other side doesn't support NWAY, then the 368 * best we can do is determine if we have a 10Mbps or 369 * 100Mbps link. There's no way to know if the link 370 * is full or half duplex, so we default to half duplex 371 * and hope that the user is clever enough to manually 372 * change the media settings if we're wrong. 373 */ 374 if (!(reg & DC_TSTAT_LS100)) 375 mii->mii_media_active |= IFM_100_TX; 376 else if (!(reg & DC_TSTAT_LS10)) 377 mii->mii_media_active |= IFM_10_T; 378 else 379 mii->mii_media_active |= IFM_NONE; 380 if (DC_IS_INTEL(dc_sc)) 381 DC_CLRBIT(dc_sc, DC_10BTCTRL, DC_TCTL_AUTONEGENBL); 382 return; 383 } 384 385skip: 386 if (CSR_READ_4(dc_sc, DC_NETCFG) & DC_NETCFG_SCRAMBLER) 387 mii->mii_media_active |= IFM_100_TX; 388 else 389 mii->mii_media_active |= IFM_10_T; 390 if (CSR_READ_4(dc_sc, DC_NETCFG) & DC_NETCFG_FULLDUPLEX) 391 mii->mii_media_active |= IFM_FDX; 392 393 return; 394} 395 396int 397dcphy_auto(mii, waitfor) 398 struct mii_softc *mii; 399 int waitfor; 400{ 401 int i; 402 struct dc_softc *sc; 403 404 sc = mii->mii_pdata->mii_ifp->if_softc; 405 406 if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) { 407 DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL); 408 DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_FULLDUPLEX); 409 DC_CLRBIT(sc, DC_SIARESET, DC_SIA_RESET); 410 if (mii->mii_capabilities & BMSR_100TXHDX) 411 CSR_WRITE_4(sc, DC_10BTCTRL, 0x3FFFF); 412 else 413 CSR_WRITE_4(sc, DC_10BTCTRL, 0xFFFF); 414 DC_SETBIT(sc, DC_SIARESET, DC_SIA_RESET); 415 DC_SETBIT(sc, DC_10BTCTRL, DC_TCTL_AUTONEGENBL); 416 DC_SETBIT(sc, DC_10BTSTAT, DC_ASTAT_TXDISABLE); 417 } 418 419 if (waitfor) { 420 /* Wait 500ms for it to complete. */ 421 for (i = 0; i < 500; i++) { 422 if ((CSR_READ_4(sc, DC_10BTSTAT) & DC_TSTAT_ANEGSTAT) 423 == DC_ASTAT_AUTONEGCMP) 424 return(0); 425 DELAY(1000); 426 } 427 /* 428 * Don't need to worry about clearing MIIF_DOINGAUTO. 429 * If that's set, a timeout is pending, and it will 430 * clear the flag. 431 */ 432 return(EIO); 433 } 434 435 /* 436 * Just let it finish asynchronously. This is for the benefit of 437 * the tick handler driving autonegotiation. Don't want 500ms 438 * delays all the time while the system is running! 439 */ 440 if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) 441 mii->mii_flags |= MIIF_DOINGAUTO; 442 443 return(EJUSTRETURN); 444} 445 446void 447dcphy_reset(mii) 448 struct mii_softc *mii; 449{ 450 struct dc_softc *sc; 451 452 sc = mii->mii_pdata->mii_ifp->if_softc; 453 454 DC_CLRBIT(sc, DC_SIARESET, DC_SIA_RESET); 455 DELAY(1000); 456 DC_SETBIT(sc, DC_SIARESET, DC_SIA_RESET); 457 458 return; 459} 460