1 /* $OpenBSD: dcphy.c,v 1.8 2002/10/20 16:46:28 henning 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.6 2000/10/05 17:36:14 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 88 int dcphy_match(struct device *, void *, void *); 89 void dcphy_attach(struct device *, struct device *, void *); 90 91 struct cfattach dcphy_ca = { 92 sizeof(struct mii_softc), dcphy_match, dcphy_attach, mii_phy_detach, 93 mii_phy_activate 94 }; 95 96 struct cfdriver dcphy_cd = { 97 NULL, "dcphy", DV_DULL 98 }; 99 100 int dcphy_service(struct mii_softc *, struct mii_data *, int); 101 void dcphy_status(struct mii_softc *); 102 int dcphy_auto(struct mii_softc *, int); 103 void dcphy_reset(struct mii_softc *); 104 105 int 106 dcphy_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 123 void 124 dcphy_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 if (dc_sc->dc_pmode == DC_PMODE_SIA) { 158 sc->mii_capabilities = 159 BMSR_ANEG|BMSR_10TFDX|BMSR_10THDX; 160 } else { 161 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, 162 sc->mii_inst), BMCR_LOOP|BMCR_S100); 163 164 sc->mii_capabilities = 165 BMSR_ANEG|BMSR_100TXFDX|BMSR_100TXHDX| 166 BMSR_10TFDX|BMSR_10THDX; 167 } 168 break; 169 } 170 171 if (dc_sc->dc_type == DC_TYPE_21145) 172 sc->mii_capabilities = BMSR_10THDX; 173 174 sc->mii_capabilities &= ma->mii_capmask; 175 if (sc->mii_capabilities & BMSR_MEDIAMASK) 176 mii_phy_add_media(sc); 177 #undef ADD 178 } 179 180 int 181 dcphy_service(sc, mii, cmd) 182 struct mii_softc *sc; 183 struct mii_data *mii; 184 int cmd; 185 { 186 struct dc_softc *dc_sc; 187 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 188 int reg; 189 u_int32_t mode; 190 191 if ((sc->mii_dev.dv_flags & DVF_ACTIVE) == 0) 192 return (ENXIO); 193 194 dc_sc = mii->mii_ifp->if_softc; 195 196 switch (cmd) { 197 case MII_POLLSTAT: 198 /* 199 * If we're not polling our PHY instance, just return. 200 */ 201 if (IFM_INST(ife->ifm_media) != sc->mii_inst) { 202 return (0); 203 } 204 break; 205 206 case MII_MEDIACHG: 207 /* 208 * If the media indicates a different PHY instance, 209 * isolate ourselves. 210 */ 211 if (IFM_INST(ife->ifm_media) != sc->mii_inst) { 212 return (0); 213 } 214 215 /* 216 * If the interface is not up, don't do anything. 217 */ 218 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 219 break; 220 221 sc->mii_flags = 0; 222 mii->mii_media_active = IFM_NONE; 223 mode = CSR_READ_4(dc_sc, DC_NETCFG); 224 mode &= ~(DC_NETCFG_FULLDUPLEX|DC_NETCFG_PORTSEL| 225 DC_NETCFG_PCS|DC_NETCFG_SCRAMBLER|DC_NETCFG_SPEEDSEL); 226 227 switch (IFM_SUBTYPE(ife->ifm_media)) { 228 case IFM_AUTO: 229 /*dcphy_reset(sc);*/ 230 sc->mii_flags &= ~MIIF_DOINGAUTO; 231 (void) dcphy_auto(sc, 0); 232 break; 233 case IFM_100_T4: 234 /* 235 * XXX Not supported as a manual setting right now. 236 */ 237 return (EINVAL); 238 case IFM_100_TX: 239 dcphy_reset(sc); 240 DC_CLRBIT(dc_sc, DC_10BTCTRL, DC_TCTL_AUTONEGENBL); 241 mode |= DC_NETCFG_PORTSEL|DC_NETCFG_PCS| 242 DC_NETCFG_SCRAMBLER; 243 if ((ife->ifm_media & IFM_GMASK) == IFM_FDX) 244 mode |= DC_NETCFG_FULLDUPLEX; 245 else 246 mode &= ~DC_NETCFG_FULLDUPLEX; 247 CSR_WRITE_4(dc_sc, DC_NETCFG, mode); 248 break; 249 case IFM_10_T: 250 DC_CLRBIT(dc_sc, DC_SIARESET, DC_SIA_RESET); 251 DC_CLRBIT(dc_sc, DC_10BTCTRL, 0xFFFF); 252 if ((ife->ifm_media & IFM_GMASK) == IFM_FDX) 253 DC_SETBIT(dc_sc, DC_10BTCTRL, 0x7F3D); 254 else 255 DC_SETBIT(dc_sc, DC_10BTCTRL, 0x7F3F); 256 DC_SETBIT(dc_sc, DC_SIARESET, DC_SIA_RESET); 257 DC_CLRBIT(dc_sc, DC_10BTCTRL, DC_TCTL_AUTONEGENBL); 258 mode &= ~DC_NETCFG_PORTSEL; 259 mode |= DC_NETCFG_SPEEDSEL; 260 if ((ife->ifm_media & IFM_GMASK) == IFM_FDX) 261 mode |= DC_NETCFG_FULLDUPLEX; 262 else 263 mode &= ~DC_NETCFG_FULLDUPLEX; 264 CSR_WRITE_4(dc_sc, DC_NETCFG, mode); 265 break; 266 default: 267 return(EINVAL); 268 break; 269 } 270 break; 271 272 case MII_TICK: 273 /* 274 * If we're not currently selected, just return. 275 */ 276 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 277 return (0); 278 279 /* 280 * Only used for autonegotiation. 281 */ 282 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) 283 return (0); 284 285 /* 286 * Is the interface even up? 287 */ 288 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 289 return (0); 290 291 reg = CSR_READ_4(dc_sc, DC_10BTSTAT) & 292 (DC_TSTAT_LS10|DC_TSTAT_LS100); 293 294 if (!(reg & DC_TSTAT_LS10) || !(reg & DC_TSTAT_LS100)) 295 return (0); 296 297 /* 298 * Only retry autonegotiation every 5 seconds. 299 */ 300 if (++sc->mii_ticks != 50) 301 return (0); 302 303 sc->mii_ticks = 0; 304 /*if (DC_IS_INTEL(dc_sc))*/ 305 sc->mii_flags &= ~MIIF_DOINGAUTO; 306 dcphy_auto(sc, 0); 307 308 break; 309 } 310 311 /* Update the media status. */ 312 mii_phy_status(sc); 313 314 /* Callback if something changed. */ 315 mii_phy_update(sc, cmd); 316 return (0); 317 } 318 319 void 320 dcphy_status(sc) 321 struct mii_softc *sc; 322 { 323 struct mii_data *mii = sc->mii_pdata; 324 int reg, anlpar, tstat = 0; 325 struct dc_softc *dc_sc; 326 327 dc_sc = mii->mii_ifp->if_softc; 328 329 mii->mii_media_status = IFM_AVALID; 330 mii->mii_media_active = IFM_ETHER; 331 332 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 333 return; 334 335 reg = CSR_READ_4(dc_sc, DC_10BTSTAT) & 336 (DC_TSTAT_LS10|DC_TSTAT_LS100); 337 338 if (!(reg & DC_TSTAT_LS10) || !(reg & DC_TSTAT_LS100)) 339 mii->mii_media_status |= IFM_ACTIVE; 340 341 if (CSR_READ_4(dc_sc, DC_10BTCTRL) & DC_TCTL_AUTONEGENBL) { 342 /* Erg, still trying, I guess... */ 343 tstat = CSR_READ_4(dc_sc, DC_10BTSTAT); 344 if ((tstat & DC_TSTAT_ANEGSTAT) != DC_ASTAT_AUTONEGCMP) { 345 if ((DC_IS_MACRONIX(dc_sc) || DC_IS_PNICII(dc_sc)) && 346 (tstat & DC_TSTAT_ANEGSTAT) == DC_ASTAT_DISABLE) 347 goto skip; 348 mii->mii_media_active |= IFM_NONE; 349 return; 350 } 351 352 if (tstat & DC_TSTAT_LP_CAN_NWAY) { 353 anlpar = tstat >> 16; 354 if (anlpar & ANLPAR_T4 && 355 sc->mii_capabilities & BMSR_100TXHDX) 356 mii->mii_media_active |= IFM_100_T4; 357 else if (anlpar & ANLPAR_TX_FD && 358 sc->mii_capabilities & BMSR_100TXFDX) 359 mii->mii_media_active |= IFM_100_TX|IFM_FDX; 360 else if (anlpar & ANLPAR_TX && 361 sc->mii_capabilities & BMSR_100TXHDX) 362 mii->mii_media_active |= IFM_100_TX; 363 else if (anlpar & ANLPAR_10_FD) 364 mii->mii_media_active |= IFM_10_T|IFM_FDX; 365 else if (anlpar & ANLPAR_10) 366 mii->mii_media_active |= IFM_10_T; 367 else 368 mii->mii_media_active |= IFM_NONE; 369 if (DC_IS_INTEL(dc_sc)) 370 DC_CLRBIT(dc_sc, DC_10BTCTRL, 371 DC_TCTL_AUTONEGENBL); 372 return; 373 } 374 /* 375 * If the other side doesn't support NWAY, then the 376 * best we can do is determine if we have a 10Mbps or 377 * 100Mbps link. There's no way to know if the link 378 * is full or half duplex, so we default to half duplex 379 * and hope that the user is clever enough to manually 380 * change the media settings if we're wrong. 381 */ 382 if (!(reg & DC_TSTAT_LS100)) 383 mii->mii_media_active |= IFM_100_TX; 384 else if (!(reg & DC_TSTAT_LS10)) 385 mii->mii_media_active |= IFM_10_T; 386 else 387 mii->mii_media_active |= IFM_NONE; 388 if (DC_IS_INTEL(dc_sc)) 389 DC_CLRBIT(dc_sc, DC_10BTCTRL, DC_TCTL_AUTONEGENBL); 390 return; 391 } 392 393 skip: 394 395 if (CSR_READ_4(dc_sc, DC_NETCFG) & DC_NETCFG_SPEEDSEL) 396 mii->mii_media_active |= IFM_10_T; 397 else 398 mii->mii_media_active |= IFM_100_TX; 399 if (CSR_READ_4(dc_sc, DC_NETCFG) & DC_NETCFG_FULLDUPLEX) 400 mii->mii_media_active |= IFM_FDX; 401 402 return; 403 } 404 405 int 406 dcphy_auto(mii, waitfor) 407 struct mii_softc *mii; 408 int waitfor; 409 { 410 int i; 411 struct dc_softc *sc; 412 413 sc = mii->mii_pdata->mii_ifp->if_softc; 414 415 if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) { 416 DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL); 417 DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_FULLDUPLEX); 418 DC_CLRBIT(sc, DC_SIARESET, DC_SIA_RESET); 419 if (mii->mii_capabilities & BMSR_100TXHDX) 420 CSR_WRITE_4(sc, DC_10BTCTRL, 0x3FFFF); 421 else 422 CSR_WRITE_4(sc, DC_10BTCTRL, 0xFFFF); 423 DC_SETBIT(sc, DC_SIARESET, DC_SIA_RESET); 424 DC_SETBIT(sc, DC_10BTCTRL, DC_TCTL_AUTONEGENBL); 425 DC_SETBIT(sc, DC_10BTSTAT, DC_ASTAT_TXDISABLE); 426 } 427 428 if (waitfor) { 429 /* Wait 500ms for it to complete. */ 430 for (i = 0; i < 500; i++) { 431 if ((CSR_READ_4(sc, DC_10BTSTAT) & DC_TSTAT_ANEGSTAT) 432 == DC_ASTAT_AUTONEGCMP) 433 return(0); 434 DELAY(1000); 435 } 436 /* 437 * Don't need to worry about clearing MIIF_DOINGAUTO. 438 * If that's set, a timeout is pending, and it will 439 * clear the flag. 440 */ 441 return(EIO); 442 } 443 444 /* 445 * Just let it finish asynchronously. This is for the benefit of 446 * the tick handler driving autonegotiation. Don't want 500ms 447 * delays all the time while the system is running! 448 */ 449 if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) 450 mii->mii_flags |= MIIF_DOINGAUTO; 451 452 return(EJUSTRETURN); 453 } 454 455 void 456 dcphy_reset(mii) 457 struct mii_softc *mii; 458 { 459 struct dc_softc *sc; 460 461 sc = mii->mii_pdata->mii_ifp->if_softc; 462 463 DC_CLRBIT(sc, DC_SIARESET, DC_SIA_RESET); 464 DELAY(1000); 465 DC_SETBIT(sc, DC_SIARESET, DC_SIA_RESET); 466 467 return; 468 } 469