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