mlphy.c revision 257184
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 */ 33 34#include <sys/cdefs.h> 35__FBSDID("$FreeBSD: head/sys/dev/mii/mlphy.c 257184 2013-10-26 18:40:17Z glebius $"); 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 device_t ml_dev; 74 int ml_state; 75 int ml_linked; 76}; 77 78static int mlphy_probe(device_t); 79static int mlphy_attach(device_t); 80 81static device_method_t mlphy_methods[] = { 82 /* device interface */ 83 DEVMETHOD(device_probe, mlphy_probe), 84 DEVMETHOD(device_attach, mlphy_attach), 85 DEVMETHOD(device_detach, mii_phy_detach), 86 DEVMETHOD(device_shutdown, bus_generic_shutdown), 87 DEVMETHOD_END 88}; 89 90static devclass_t mlphy_devclass; 91 92static driver_t mlphy_driver = { 93 "mlphy", 94 mlphy_methods, 95 sizeof(struct mlphy_softc) 96}; 97 98DRIVER_MODULE(mlphy, miibus, mlphy_driver, mlphy_devclass, 0, 0); 99 100static struct mii_softc *mlphy_find_other(struct mlphy_softc *); 101static int mlphy_service(struct mii_softc *, struct mii_data *, int); 102static void mlphy_reset(struct mii_softc *); 103static void mlphy_status(struct mii_softc *); 104 105static const struct mii_phy_funcs mlphy_funcs = { 106 mlphy_service, 107 mlphy_status, 108 mlphy_reset 109}; 110 111static int 112mlphy_probe(dev) 113 device_t dev; 114{ 115 struct mii_attach_args *ma; 116 117 ma = device_get_ivars(dev); 118 119 /* 120 * Micro Linear PHY reports oui == 0 model == 0 121 */ 122 if (MII_OUI(ma->mii_id1, ma->mii_id2) != 0 || 123 MII_MODEL(ma->mii_id2) != 0) 124 return (ENXIO); 125 126 /* 127 * Make sure the parent is a `tl'. So far, I have only 128 * encountered the 6692 on an Olicom card with a ThunderLAN 129 * controller chip. 130 */ 131 if (strcmp(device_get_name(device_get_parent(device_get_parent(dev))), 132 "tl") != 0) 133 return (ENXIO); 134 135 device_set_desc(dev, "Micro Linear 6692 media interface"); 136 137 return (BUS_PROBE_DEFAULT); 138} 139 140static int 141mlphy_attach(dev) 142 device_t dev; 143{ 144 struct mlphy_softc *msc; 145 struct mii_softc *sc; 146 147 msc = device_get_softc(dev); 148 sc = &msc->ml_mii; 149 msc->ml_dev = dev; 150 mii_phy_dev_attach(dev, MIIF_NOMANPAUSE, &mlphy_funcs, 0); 151 152 PHY_RESET(sc); 153 154 sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & sc->mii_capmask; 155 /* Let the companion PHY (if any) only handle the media we don't. */ 156 sc->mii_capmask = ~sc->mii_capabilities; 157 device_printf(dev, " "); 158 mii_phy_add_media(sc); 159 printf("\n"); 160 161 MIIBUS_MEDIAINIT(sc->mii_dev); 162 return (0); 163} 164 165static struct mii_softc * 166mlphy_find_other(struct mlphy_softc *msc) 167{ 168 device_t *devlist; 169 struct mii_softc *retval; 170 int i, devs; 171 172 retval = NULL; 173 if (device_get_children(msc->ml_mii.mii_dev, &devlist, &devs) != 0) 174 return (NULL); 175 for (i = 0; i < devs; i++) { 176 if (devlist[i] != msc->ml_dev) { 177 retval = device_get_softc(devlist[i]); 178 break; 179 } 180 } 181 free(devlist, M_TEMP); 182 return (retval); 183} 184 185static int 186mlphy_service(xsc, mii, cmd) 187 struct mii_softc *xsc; 188 struct mii_data *mii; 189 int cmd; 190{ 191 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 192 struct mii_softc *other = NULL; 193 struct mlphy_softc *msc = (struct mlphy_softc *)xsc; 194 struct mii_softc *sc = (struct mii_softc *)&msc->ml_mii; 195 int other_inst, reg; 196 197 /* 198 * See if there's another PHY on this bus with us. 199 * If so, we may need it for 10Mbps modes. 200 */ 201 other = mlphy_find_other(msc); 202 203 switch (cmd) { 204 case MII_POLLSTAT: 205 break; 206 207 case MII_MEDIACHG: 208 switch (IFM_SUBTYPE(ife->ifm_media)) { 209 case IFM_AUTO: 210 /* 211 * For autonegotiation, reset and isolate the 212 * companion PHY (if any) and then do NWAY 213 * autonegotiation ourselves. 214 */ 215 msc->ml_state = ML_STATE_AUTO_SELF; 216 if (other != NULL) { 217 PHY_RESET(other); 218 PHY_WRITE(other, MII_BMCR, BMCR_ISO); 219 } 220 (void)mii_phy_auto(sc); 221 msc->ml_linked = 0; 222 return (0); 223 case IFM_10_T: 224 /* 225 * For 10baseT modes, reset and program the 226 * companion PHY (of any), then program ourselves 227 * to match. This will put us in pass-through 228 * mode and let the companion PHY do all the 229 * work. 230 * 231 * BMCR data is stored in the ifmedia entry. 232 */ 233 if (other != NULL) { 234 PHY_RESET(other); 235 PHY_WRITE(other, MII_BMCR, ife->ifm_data); 236 } 237 mii_phy_setmedia(sc); 238 msc->ml_state = 0; 239 break; 240 case IFM_100_TX: 241 /* 242 * For 100baseTX modes, reset and isolate the 243 * companion PHY (if any), then program ourselves 244 * accordingly. 245 * 246 * BMCR data is stored in the ifmedia entry. 247 */ 248 if (other != NULL) { 249 PHY_RESET(other); 250 PHY_WRITE(other, MII_BMCR, BMCR_ISO); 251 } 252 mii_phy_setmedia(sc); 253 msc->ml_state = 0; 254 break; 255 default: 256 return (EINVAL); 257 258 } 259 break; 260 261 case MII_TICK: 262 /* 263 * Only used for autonegotiation. 264 */ 265 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) 266 break; 267 268 /* 269 * Check to see if we have link. If we do, we don't 270 * need to restart the autonegotiation process. Read 271 * the BMSR twice in case it's latched. 272 * If we're in a 10Mbps mode, check the link of the 273 * 10Mbps PHY. Sometimes the Micro Linear PHY's 274 * linkstat bit will clear while the linkstat bit of 275 * the companion PHY will remain set. 276 */ 277 if (msc->ml_state == ML_STATE_AUTO_OTHER) { 278 reg = PHY_READ(other, MII_BMSR) | 279 PHY_READ(other, MII_BMSR); 280 } else { 281 reg = PHY_READ(sc, MII_BMSR) | 282 PHY_READ(sc, MII_BMSR); 283 } 284 285 if (reg & BMSR_LINK) { 286 if (!msc->ml_linked) { 287 msc->ml_linked = 1; 288 PHY_STATUS(sc); 289 } 290 break; 291 } 292 293 /* 294 * Only retry autonegotiation every 5 seconds. 295 */ 296 if (++sc->mii_ticks <= MII_ANEGTICKS) 297 break; 298 299 sc->mii_ticks = 0; 300 msc->ml_linked = 0; 301 mii->mii_media_active = IFM_NONE; 302 PHY_RESET(sc); 303 msc->ml_state = ML_STATE_AUTO_SELF; 304 if (other != NULL) { 305 PHY_RESET(other); 306 PHY_WRITE(other, MII_BMCR, BMCR_ISO); 307 } 308 mii_phy_auto(sc); 309 return (0); 310 } 311 312 /* Update the media status. */ 313 314 if (msc->ml_state == ML_STATE_AUTO_OTHER) { 315 other_inst = other->mii_inst; 316 other->mii_inst = sc->mii_inst; 317 if (IFM_INST(ife->ifm_media) == other->mii_inst) 318 (void)PHY_SERVICE(other, mii, MII_POLLSTAT); 319 other->mii_inst = other_inst; 320 sc->mii_media_active = other->mii_media_active; 321 sc->mii_media_status = other->mii_media_status; 322 } else 323 ukphy_status(sc); 324 325 /* Callback if something changed. */ 326 mii_phy_update(sc, cmd); 327 return (0); 328} 329 330/* 331 * The Micro Linear PHY comes out of reset with the 'autoneg 332 * enable' bit set, which we don't want. 333 */ 334static void 335mlphy_reset(sc) 336 struct mii_softc *sc; 337{ 338 int reg; 339 340 mii_phy_reset(sc); 341 reg = PHY_READ(sc, MII_BMCR); 342 reg &= ~BMCR_AUTOEN; 343 PHY_WRITE(sc, MII_BMCR, reg); 344} 345 346/* 347 * If we negotiate a 10Mbps mode, we need to check for an alternate 348 * PHY and make sure it's enabled and set correctly. 349 */ 350static void 351mlphy_status(sc) 352 struct mii_softc *sc; 353{ 354 struct mlphy_softc *msc = (struct mlphy_softc *)sc; 355 struct mii_data *mii = msc->ml_mii.mii_pdata; 356 struct mii_softc *other = NULL; 357 358 /* See if there's another PHY on the bus with us. */ 359 other = mlphy_find_other(msc); 360 if (other == NULL) 361 return; 362 363 ukphy_status(sc); 364 365 if (IFM_SUBTYPE(mii->mii_media_active) != IFM_10_T) { 366 msc->ml_state = ML_STATE_AUTO_SELF; 367 PHY_RESET(other); 368 PHY_WRITE(other, MII_BMCR, BMCR_ISO); 369 } 370 371 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_10_T) { 372 msc->ml_state = ML_STATE_AUTO_OTHER; 373 PHY_RESET(&msc->ml_mii); 374 PHY_WRITE(&msc->ml_mii, MII_BMCR, BMCR_ISO); 375 PHY_RESET(other); 376 mii_phy_auto(other); 377 } 378} 379