mlphy.c revision 105135
1/* 2 * Copyright (c) 1997, 1998, 1999 3 * Bill Paul <wpaul@ctr.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: head/sys/dev/mii/mlphy.c 105135 2002-10-14 22:31:52Z alfred $ 33 */ 34 35/* 36 * driver for Micro Linear 6692 PHYs 37 * 38 * The Micro Linear 6692 is a strange beast, and dealing with it using 39 * this code framework is tricky. The 6692 is actually a 100Mbps-only 40 * device, which means that a second PHY is required to support 10Mbps 41 * modes. However, even though the 6692 does not support 10Mbps modes, 42 * it can still advertise them when performing autonegotiation. If a 43 * 10Mbps mode is negotiated, we must program the registers of the 44 * companion PHY accordingly in addition to programming the registers 45 * of the 6692. 46 * 47 * This device also does not have vendor/device ID registers. 48 */ 49 50#include <sys/param.h> 51#include <sys/systm.h> 52#include <sys/kernel.h> 53#include <sys/socket.h> 54#include <sys/module.h> 55#include <sys/bus.h> 56#include <sys/malloc.h> 57 58#include <net/if.h> 59#include <net/if_media.h> 60 61#include <dev/mii/mii.h> 62#include <dev/mii/miivar.h> 63 64#include "miibus_if.h" 65 66#define ML_STATE_AUTO_SELF 1 67#define ML_STATE_AUTO_OTHER 2 68 69struct mlphy_softc { 70 struct mii_softc ml_mii; 71 int ml_state; 72 int ml_linked; 73}; 74 75static int mlphy_probe(device_t); 76static int mlphy_attach(device_t); 77 78static device_method_t mlphy_methods[] = { 79 /* device interface */ 80 DEVMETHOD(device_probe, mlphy_probe), 81 DEVMETHOD(device_attach, mlphy_attach), 82 DEVMETHOD(device_detach, mii_phy_detach), 83 DEVMETHOD(device_shutdown, bus_generic_shutdown), 84 { 0, 0 } 85}; 86 87static devclass_t mlphy_devclass; 88 89static driver_t mlphy_driver = { 90 "mlphy", 91 mlphy_methods, 92 sizeof(struct mlphy_softc) 93}; 94 95DRIVER_MODULE(mlphy, miibus, mlphy_driver, mlphy_devclass, 0, 0); 96 97static int mlphy_service(struct mii_softc *, struct mii_data *, int); 98static void mlphy_reset(struct mii_softc *); 99static void mlphy_status(struct mii_softc *); 100 101static int 102mlphy_probe(dev) 103 device_t dev; 104{ 105 struct mii_attach_args *ma; 106 device_t parent; 107 108 ma = device_get_ivars(dev); 109 parent = device_get_parent(device_get_parent(dev)); 110 111 /* 112 * Micro Linear PHY reports oui == 0 model == 0 113 */ 114 if (MII_OUI(ma->mii_id1, ma->mii_id2) != 0 || 115 MII_MODEL(ma->mii_id2) != 0) 116 return (ENXIO); 117 118 /* 119 * Make sure the parent is a `tl'. So far, I have only 120 * encountered the 6692 on an Olicom card with a ThunderLAN 121 * controller chip. 122 */ 123 if (strcmp(device_get_name(parent), "tl") != 0) 124 return (ENXIO); 125 126 device_set_desc(dev, "Micro Linear 6692 media interface"); 127 128 return (0); 129} 130 131static int 132mlphy_attach(dev) 133 device_t dev; 134{ 135 struct mlphy_softc *msc; 136 struct mii_softc *sc; 137 struct mii_attach_args *ma; 138 struct mii_data *mii; 139 140 msc = device_get_softc(dev); 141 sc = &msc->ml_mii; 142 ma = device_get_ivars(dev); 143 sc->mii_dev = device_get_parent(dev); 144 mii = device_get_softc(sc->mii_dev); 145 LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list); 146 147 sc->mii_inst = mii->mii_instance; 148 sc->mii_phy = ma->mii_phyno; 149 sc->mii_service = mlphy_service; 150 sc->mii_pdata = mii; 151 152 mii->mii_instance++; 153 154#define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL) 155 156#if 0 /* See above. */ 157 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst), 158 BMCR_ISO); 159#endif 160 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->mii_inst), 161 BMCR_LOOP|BMCR_S100); 162 163 sc->mii_flags &= ~MIIF_NOISOLATE; 164 mii_phy_reset(sc); 165 sc->mii_flags |= MIIF_NOISOLATE; 166 167 sc->mii_capabilities = 168 PHY_READ(sc, MII_BMSR) & ma->mii_capmask; 169 ma->mii_capmask = ~sc->mii_capabilities; 170 device_printf(dev, " "); 171 mii_add_media(sc); 172 printf("\n"); 173#undef ADD 174 MIIBUS_MEDIAINIT(sc->mii_dev); 175 return(0); 176} 177 178static int 179mlphy_service(xsc, mii, cmd) 180 struct mii_softc *xsc; 181 struct mii_data *mii; 182 int cmd; 183{ 184 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 185 int reg; 186 struct mii_softc *other = NULL; 187 struct mlphy_softc *msc = (struct mlphy_softc *)xsc; 188 struct mii_softc *sc = (struct mii_softc *)&msc->ml_mii; 189 device_t *devlist; 190 int devs, i; 191 192 /* 193 * See if there's another PHY on this bus with us. 194 * If so, we may need it for 10Mbps modes. 195 */ 196 device_get_children(msc->ml_mii.mii_dev, &devlist, &devs); 197 for (i = 0; i < devs; i++) { 198 if (strcmp(device_get_name(devlist[i]), "mlphy")) { 199 other = device_get_softc(devlist[i]); 200 break; 201 } 202 } 203 free(devlist, M_TEMP); 204 205 switch (cmd) { 206 case MII_POLLSTAT: 207 /* 208 * If we're not polling our PHY instance, just return. 209 */ 210 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 211 return (0); 212 break; 213 214 case MII_MEDIACHG: 215 /* 216 * If the media indicates a different PHY instance, 217 * isolate ourselves. 218 */ 219 if (IFM_INST(ife->ifm_media) != sc->mii_inst) { 220 reg = PHY_READ(sc, MII_BMCR); 221 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO); 222 return (0); 223 } 224 225 /* 226 * If the interface is not up, don't do anything. 227 */ 228 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 229 break; 230 231 switch (IFM_SUBTYPE(ife->ifm_media)) { 232 case IFM_AUTO: 233 /* 234 * For autonegotiation, reset and isolate the 235 * companion PHY (if any) and then do NWAY 236 * autonegotiation ourselves. 237 */ 238 msc->ml_state = ML_STATE_AUTO_SELF; 239 if (other != NULL) { 240 mii_phy_reset(other); 241 PHY_WRITE(other, MII_BMCR, BMCR_ISO); 242 } 243 (void) mii_phy_auto(sc); 244 msc->ml_linked = 0; 245 return(0); 246 break; 247 case IFM_10_T: 248 /* 249 * For 10baseT modes, reset and program the 250 * companion PHY (of any), then program ourselves 251 * to match. This will put us in pass-through 252 * mode and let the companion PHY do all the 253 * work. 254 * 255 * BMCR data is stored in the ifmedia entry. 256 */ 257 if (other != NULL) { 258 mii_phy_reset(other); 259 PHY_WRITE(other, MII_BMCR, ife->ifm_data); 260 } 261 PHY_WRITE(sc, MII_ANAR, mii_anar(ife->ifm_media)); 262 PHY_WRITE(sc, MII_BMCR, ife->ifm_data); 263 msc->ml_state = 0; 264 break; 265 case IFM_100_TX: 266 /* 267 * For 100baseTX modes, reset and isolate the 268 * companion PHY (if any), then program ourselves 269 * accordingly. 270 * 271 * BMCR data is stored in the ifmedia entry. 272 */ 273 if (other != NULL) { 274 mii_phy_reset(other); 275 PHY_WRITE(other, MII_BMCR, BMCR_ISO); 276 } 277 PHY_WRITE(sc, MII_ANAR, mii_anar(ife->ifm_media)); 278 PHY_WRITE(sc, MII_BMCR, ife->ifm_data); 279 msc->ml_state = 0; 280 break; 281 case IFM_100_T4: 282 /* 283 * XXX Not supported as a manual setting right now. 284 */ 285 return (EINVAL); 286 default: 287 break; 288 289 } 290 break; 291 292 case MII_TICK: 293 /* 294 * If we're not currently selected, just return. 295 */ 296 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 297 return (0); 298 299 /* 300 * Is the interface even up? 301 */ 302 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 303 return (0); 304 305 /* 306 * Only used for autonegotiation. 307 */ 308 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) 309 break; 310 311 /* 312 * Check to see if we have link. If we do, we don't 313 * need to restart the autonegotiation process. Read 314 * the BMSR twice in case it's latched. 315 * If we're in a 10Mbps mode, check the link of the 316 * 10Mbps PHY. Sometimes the Micro Linear PHY's 317 * linkstat bit will clear while the linkstat bit of 318 * the companion PHY will remain set. 319 */ 320 if (msc->ml_state == ML_STATE_AUTO_OTHER) { 321 reg = PHY_READ(other, MII_BMSR) | 322 PHY_READ(other, MII_BMSR); 323 } else { 324 reg = PHY_READ(sc, MII_BMSR) | 325 PHY_READ(sc, MII_BMSR); 326 } 327 328 if (reg & BMSR_LINK) { 329 if (!msc->ml_linked) { 330 msc->ml_linked = 1; 331 mlphy_status(sc); 332 } 333 break; 334 } 335 336 /* 337 * Only retry autonegotiation every 5 seconds. 338 */ 339 if (++sc->mii_ticks != 5) 340 return (0); 341 342 sc->mii_ticks = 0; 343 msc->ml_linked = 0; 344 mii->mii_media_active = IFM_NONE; 345 mii_phy_reset(sc); 346 msc->ml_state = ML_STATE_AUTO_SELF; 347 if (other != NULL) { 348 mii_phy_reset(other); 349 PHY_WRITE(other, MII_BMCR, BMCR_ISO); 350 } 351 mii_phy_auto(sc); 352 return(0); 353 } 354 355 /* Update the media status. */ 356 357 if (msc->ml_state == ML_STATE_AUTO_OTHER) { 358 int other_inst; 359 other_inst = other->mii_inst; 360 other->mii_inst = sc->mii_inst; 361 (void) (*other->mii_service)(other, mii, MII_POLLSTAT); 362 other->mii_inst = other_inst; 363 sc->mii_media_active = other->mii_media_active; 364 sc->mii_media_status = other->mii_media_status; 365 } else 366 ukphy_status(sc); 367 368 /* Callback if something changed. */ 369 mii_phy_update(sc, cmd); 370 return (0); 371} 372 373/* 374 * The Micro Linear PHY comes out of reset with the 'autoneg 375 * enable' bit set, which we don't want. 376 */ 377static void 378mlphy_reset(sc) 379 struct mii_softc *sc; 380{ 381 int reg; 382 383 mii_phy_reset(sc); 384 reg = PHY_READ(sc, MII_BMCR); 385 reg &= ~BMCR_AUTOEN; 386 PHY_WRITE(sc, MII_BMCR, reg); 387 388 return; 389} 390 391/* 392 * If we negotiate a 10Mbps mode, we need to check for an alternate 393 * PHY and make sure it's enabled and set correctly. 394 */ 395static void 396mlphy_status(sc) 397 struct mii_softc *sc; 398{ 399 struct mlphy_softc *msc = (struct mlphy_softc *)sc; 400 struct mii_data *mii = msc->ml_mii.mii_pdata; 401 struct mii_softc *other = NULL; 402 device_t *devlist; 403 int devs, i; 404 405 /* See if there's another PHY on the bus with us. */ 406 device_get_children(msc->ml_mii.mii_dev, &devlist, &devs); 407 for (i = 0; i < devs; i++) { 408 if (strcmp(device_get_name(devlist[i]), "mlphy")) { 409 other = device_get_softc(devlist[i]); 410 break; 411 } 412 } 413 free(devlist, M_TEMP); 414 415 if (other == NULL) 416 return; 417 418 ukphy_status(sc); 419 420 if (IFM_SUBTYPE(mii->mii_media_active) != IFM_10_T) { 421 msc->ml_state = ML_STATE_AUTO_SELF; 422 mii_phy_reset(other); 423 PHY_WRITE(other, MII_BMCR, BMCR_ISO); 424 } 425 426 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_10_T) { 427 msc->ml_state = ML_STATE_AUTO_OTHER; 428 mlphy_reset(&msc->ml_mii); 429 PHY_WRITE(&msc->ml_mii, MII_BMCR, BMCR_ISO); 430 mii_phy_reset(other); 431 mii_phy_auto(other); 432 } 433 434 return; 435} 436