mlphy.c revision 213229
11832Swollman/*- 250473Speter * Copyright (c) 1997, 1998, 1999 31832Swollman * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved. 41832Swollman * 51832Swollman * Redistribution and use in source and binary forms, with or without 632624Swpaul * modification, are permitted provided that the following conditions 71832Swollman * are met: 826208Swpaul * 1. Redistributions of source code must retain the above copyright 916123Swpaul * notice, this list of conditions and the following disclaimer. 1026208Swpaul * 2. Redistributions in binary form must reproduce the above copyright 1126208Swpaul * notice, this list of conditions and the following disclaimer in the 1226208Swpaul * documentation and/or other materials provided with the distribution. 131832Swollman * 3. All advertising materials mentioning features or use of this software 1426208Swpaul * must display the following acknowledgement: 1526208Swpaul * This product includes software developed by Bill Paul. 1626208Swpaul * 4. Neither the name of the author nor the names of any co-contributors 171832Swollman * may be used to endorse or promote products derived from this software 181832Swollman * without specific prior written permission. 191832Swollman * 2096462Sru * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 2196462Sru * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2296462Sru * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2396462Sru * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD 2496462Sru * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 251832Swollman * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 261832Swollman * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 2739429Sobrien * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 281832Swollman * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 2932551Sbde * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 3032551Sbde * THE POSSIBILITY OF SUCH DAMAGE. 311832Swollman * 32 */ 33 34#include <sys/cdefs.h> 35__FBSDID("$FreeBSD: head/sys/dev/mii/mlphy.c 213229 2010-09-27 20:31:03Z marius $"); 36 37/* 38 * driver for Micro Linear 6692 PHYs 39 * 40 * The Micro Linear 6692 is a strange beast, and dealing with it using 41 * this code framework is tricky. The 6692 is actually a 100Mbps-only 42 * device, which means that a second PHY is required to support 10Mbps 43 * modes. However, even though the 6692 does not support 10Mbps modes, 44 * it can still advertise them when performing autonegotiation. If a 45 * 10Mbps mode is negotiated, we must program the registers of the 46 * companion PHY accordingly in addition to programming the registers 47 * of the 6692. 48 * 49 * This device also does not have vendor/device ID registers. 50 */ 51 52#include <sys/param.h> 53#include <sys/systm.h> 54#include <sys/kernel.h> 55#include <sys/socket.h> 56#include <sys/module.h> 57#include <sys/bus.h> 58#include <sys/malloc.h> 59 60#include <net/if.h> 61#include <net/if_media.h> 62 63#include <dev/mii/mii.h> 64#include <dev/mii/miivar.h> 65 66#include "miibus_if.h" 67 68#define ML_STATE_AUTO_SELF 1 69#define ML_STATE_AUTO_OTHER 2 70 71struct mlphy_softc { 72 struct mii_softc ml_mii; 73 int ml_state; 74 int ml_linked; 75}; 76 77static int mlphy_probe(device_t); 78static int mlphy_attach(device_t); 79 80static device_method_t mlphy_methods[] = { 81 /* device interface */ 82 DEVMETHOD(device_probe, mlphy_probe), 83 DEVMETHOD(device_attach, mlphy_attach), 84 DEVMETHOD(device_detach, mii_phy_detach), 85 DEVMETHOD(device_shutdown, bus_generic_shutdown), 86 { 0, 0 } 87}; 88 89static devclass_t mlphy_devclass; 90 91static driver_t mlphy_driver = { 92 "mlphy", 93 mlphy_methods, 94 sizeof(struct mlphy_softc) 95}; 96 97DRIVER_MODULE(mlphy, miibus, mlphy_driver, mlphy_devclass, 0, 0); 98 99static int mlphy_service(struct mii_softc *, struct mii_data *, int); 100static void mlphy_reset(struct mii_softc *); 101static void mlphy_status(struct mii_softc *); 102 103static int 104mlphy_probe(dev) 105 device_t dev; 106{ 107 struct mii_attach_args *ma; 108 device_t parent; 109 110 ma = device_get_ivars(dev); 111 parent = device_get_parent(device_get_parent(dev)); 112 113 /* 114 * Micro Linear PHY reports oui == 0 model == 0 115 */ 116 if (MII_OUI(ma->mii_id1, ma->mii_id2) != 0 || 117 MII_MODEL(ma->mii_id2) != 0) 118 return (ENXIO); 119 120 /* 121 * Make sure the parent is a `tl'. So far, I have only 122 * encountered the 6692 on an Olicom card with a ThunderLAN 123 * controller chip. 124 */ 125 if (strcmp(device_get_name(parent), "tl") != 0) 126 return (ENXIO); 127 128 device_set_desc(dev, "Micro Linear 6692 media interface"); 129 130 return (BUS_PROBE_DEFAULT); 131} 132 133static int 134mlphy_attach(dev) 135 device_t dev; 136{ 137 struct mlphy_softc *msc; 138 struct mii_softc *sc; 139 struct mii_attach_args *ma; 140 struct mii_data *mii; 141 142 msc = device_get_softc(dev); 143 sc = &msc->ml_mii; 144 ma = device_get_ivars(dev); 145 sc->mii_dev = device_get_parent(dev); 146 mii = ma->mii_data; 147 LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list); 148 149 sc->mii_inst = mii->mii_instance; 150 sc->mii_phy = ma->mii_phyno; 151 sc->mii_service = mlphy_service; 152 sc->mii_pdata = mii; 153 154 mii->mii_instance++; 155 156#define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL) 157 158#if 0 /* See above. */ 159 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst), 160 BMCR_ISO); 161#endif 162 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->mii_inst), 163 BMCR_LOOP|BMCR_S100); 164 165 sc->mii_flags &= ~MIIF_NOISOLATE; 166 mii_phy_reset(sc); 167 sc->mii_flags |= MIIF_NOISOLATE; 168 169 sc->mii_capabilities = 170 PHY_READ(sc, MII_BMSR) & ma->mii_capmask; 171 ma->mii_capmask = ~sc->mii_capabilities; 172 device_printf(dev, " "); 173 mii_add_media(sc); 174 printf("\n"); 175#undef ADD 176 MIIBUS_MEDIAINIT(sc->mii_dev); 177 return (0); 178} 179 180static struct mii_softc * 181mlphy_find_other(device_t mii) 182{ 183 device_t *devlist; 184 struct mii_softc *retval; 185 int i, devs; 186 187 retval = NULL; 188 if (device_get_children(mii, &devlist, &devs)) 189 return (NULL); 190 for (i = 0; i < devs; i++) 191 if (strcmp(device_get_name(devlist[i]), "mlphy")) { 192 retval = device_get_softc(devlist[i]); 193 break; 194 } 195 free(devlist, M_TEMP); 196 return (retval); 197} 198 199static int 200mlphy_service(xsc, mii, cmd) 201 struct mii_softc *xsc; 202 struct mii_data *mii; 203 int cmd; 204{ 205 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 206 struct mii_softc *other = NULL; 207 struct mlphy_softc *msc = (struct mlphy_softc *)xsc; 208 struct mii_softc *sc = (struct mii_softc *)&msc->ml_mii; 209 int other_inst, reg; 210 211 /* 212 * See if there's another PHY on this bus with us. 213 * If so, we may need it for 10Mbps modes. 214 */ 215 other = mlphy_find_other(msc->ml_mii.mii_dev); 216 217 switch (cmd) { 218 case MII_POLLSTAT: 219 /* 220 * If we're not polling our PHY instance, just return. 221 */ 222 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 223 return (0); 224 break; 225 226 case MII_MEDIACHG: 227 /* 228 * If the media indicates a different PHY instance, 229 * isolate ourselves. 230 */ 231 if (IFM_INST(ife->ifm_media) != sc->mii_inst) { 232 reg = PHY_READ(sc, MII_BMCR); 233 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO); 234 return (0); 235 } 236 237 /* 238 * If the interface is not up, don't do anything. 239 */ 240 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 241 break; 242 243 switch (IFM_SUBTYPE(ife->ifm_media)) { 244 case IFM_AUTO: 245 /* 246 * For autonegotiation, reset and isolate the 247 * companion PHY (if any) and then do NWAY 248 * autonegotiation ourselves. 249 */ 250 msc->ml_state = ML_STATE_AUTO_SELF; 251 if (other != NULL) { 252 mii_phy_reset(other); 253 PHY_WRITE(other, MII_BMCR, BMCR_ISO); 254 } 255 (void) mii_phy_auto(sc); 256 msc->ml_linked = 0; 257 return (0); 258 case IFM_10_T: 259 /* 260 * For 10baseT modes, reset and program the 261 * companion PHY (of any), then program ourselves 262 * to match. This will put us in pass-through 263 * mode and let the companion PHY do all the 264 * work. 265 * 266 * BMCR data is stored in the ifmedia entry. 267 */ 268 if (other != NULL) { 269 mii_phy_reset(other); 270 PHY_WRITE(other, MII_BMCR, ife->ifm_data); 271 } 272 PHY_WRITE(sc, MII_ANAR, mii_anar(ife->ifm_media)); 273 PHY_WRITE(sc, MII_BMCR, ife->ifm_data); 274 msc->ml_state = 0; 275 break; 276 case IFM_100_TX: 277 /* 278 * For 100baseTX modes, reset and isolate the 279 * companion PHY (if any), then program ourselves 280 * accordingly. 281 * 282 * BMCR data is stored in the ifmedia entry. 283 */ 284 if (other != NULL) { 285 mii_phy_reset(other); 286 PHY_WRITE(other, MII_BMCR, BMCR_ISO); 287 } 288 PHY_WRITE(sc, MII_ANAR, mii_anar(ife->ifm_media)); 289 PHY_WRITE(sc, MII_BMCR, ife->ifm_data); 290 msc->ml_state = 0; 291 break; 292 case IFM_100_T4: 293 /* 294 * XXX Not supported as a manual setting right now. 295 */ 296 return (EINVAL); 297 default: 298 break; 299 300 } 301 break; 302 303 case MII_TICK: 304 /* 305 * If we're not currently selected, just return. 306 */ 307 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 308 return (0); 309 310 /* 311 * Is the interface even up? 312 */ 313 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 314 return (0); 315 316 /* 317 * Only used for autonegotiation. 318 */ 319 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) 320 break; 321 322 /* 323 * Check to see if we have link. If we do, we don't 324 * need to restart the autonegotiation process. Read 325 * the BMSR twice in case it's latched. 326 * If we're in a 10Mbps mode, check the link of the 327 * 10Mbps PHY. Sometimes the Micro Linear PHY's 328 * linkstat bit will clear while the linkstat bit of 329 * the companion PHY will remain set. 330 */ 331 if (msc->ml_state == ML_STATE_AUTO_OTHER) { 332 reg = PHY_READ(other, MII_BMSR) | 333 PHY_READ(other, MII_BMSR); 334 } else { 335 reg = PHY_READ(sc, MII_BMSR) | 336 PHY_READ(sc, MII_BMSR); 337 } 338 339 if (reg & BMSR_LINK) { 340 if (!msc->ml_linked) { 341 msc->ml_linked = 1; 342 mlphy_status(sc); 343 } 344 break; 345 } 346 347 /* 348 * Only retry autonegotiation every 5 seconds. 349 */ 350 if (++sc->mii_ticks <= MII_ANEGTICKS) 351 break; 352 353 sc->mii_ticks = 0; 354 msc->ml_linked = 0; 355 mii->mii_media_active = IFM_NONE; 356 mii_phy_reset(sc); 357 msc->ml_state = ML_STATE_AUTO_SELF; 358 if (other != NULL) { 359 mii_phy_reset(other); 360 PHY_WRITE(other, MII_BMCR, BMCR_ISO); 361 } 362 mii_phy_auto(sc); 363 return (0); 364 } 365 366 /* Update the media status. */ 367 368 if (msc->ml_state == ML_STATE_AUTO_OTHER) { 369 other_inst = other->mii_inst; 370 other->mii_inst = sc->mii_inst; 371 (void) (*other->mii_service)(other, mii, MII_POLLSTAT); 372 other->mii_inst = other_inst; 373 sc->mii_media_active = other->mii_media_active; 374 sc->mii_media_status = other->mii_media_status; 375 } else 376 ukphy_status(sc); 377 378 /* Callback if something changed. */ 379 mii_phy_update(sc, cmd); 380 return (0); 381} 382 383/* 384 * The Micro Linear PHY comes out of reset with the 'autoneg 385 * enable' bit set, which we don't want. 386 */ 387static void 388mlphy_reset(sc) 389 struct mii_softc *sc; 390{ 391 int reg; 392 393 mii_phy_reset(sc); 394 reg = PHY_READ(sc, MII_BMCR); 395 reg &= ~BMCR_AUTOEN; 396 PHY_WRITE(sc, MII_BMCR, reg); 397} 398 399/* 400 * If we negotiate a 10Mbps mode, we need to check for an alternate 401 * PHY and make sure it's enabled and set correctly. 402 */ 403static void 404mlphy_status(sc) 405 struct mii_softc *sc; 406{ 407 struct mlphy_softc *msc = (struct mlphy_softc *)sc; 408 struct mii_data *mii = msc->ml_mii.mii_pdata; 409 struct mii_softc *other = NULL; 410 411 /* See if there's another PHY on the bus with us. */ 412 other = mlphy_find_other(msc->ml_mii.mii_dev); 413 if (other == NULL) 414 return; 415 416 ukphy_status(sc); 417 418 if (IFM_SUBTYPE(mii->mii_media_active) != IFM_10_T) { 419 msc->ml_state = ML_STATE_AUTO_SELF; 420 mii_phy_reset(other); 421 PHY_WRITE(other, MII_BMCR, BMCR_ISO); 422 } 423 424 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_10_T) { 425 msc->ml_state = ML_STATE_AUTO_OTHER; 426 mlphy_reset(&msc->ml_mii); 427 PHY_WRITE(&msc->ml_mii, MII_BMCR, BMCR_ISO); 428 mii_phy_reset(other); 429 mii_phy_auto(other); 430 } 431} 432