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