1 /*- 2 * Copyright (c) 1997, 1998, 1999 3 * Bill Paul <[email protected]>. 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$"); 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 71 struct mlphy_softc { 72 struct mii_softc ml_mii; 73 int ml_state; 74 int ml_linked; 75 }; 76 77 static int mlphy_probe(device_t); 78 static int mlphy_attach(device_t); 79 80 static 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 89 static devclass_t mlphy_devclass; 90 91 static driver_t mlphy_driver = { 92 "mlphy", 93 mlphy_methods, 94 sizeof(struct mlphy_softc) 95 }; 96 97 DRIVER_MODULE(mlphy, miibus, mlphy_driver, mlphy_devclass, 0, 0); 98 99 static int mlphy_service(struct mii_softc *, struct mii_data *, int); 100 static void mlphy_reset(struct mii_softc *); 101 static void mlphy_status(struct mii_softc *); 102 103 static int 104 mlphy_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 133 static int 134 mlphy_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_flags = miibus_get_flags(dev); 150 sc->mii_inst = mii->mii_instance++; 151 sc->mii_phy = ma->mii_phyno; 152 sc->mii_service = mlphy_service; 153 sc->mii_pdata = mii; 154 155 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL) 156 157 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->mii_inst), 158 BMCR_LOOP|BMCR_S100); 159 160 mii_phy_reset(sc); 161 162 sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & ma->mii_capmask; 163 ma->mii_capmask = ~sc->mii_capabilities; 164 device_printf(dev, " "); 165 mii_add_media(sc); 166 printf("\n"); 167 #undef ADD 168 MIIBUS_MEDIAINIT(sc->mii_dev); 169 return (0); 170 } 171 172 static struct mii_softc * 173 mlphy_find_other(device_t mii) 174 { 175 device_t *devlist; 176 struct mii_softc *retval; 177 int i, devs; 178 179 retval = NULL; 180 if (device_get_children(mii, &devlist, &devs)) 181 return (NULL); 182 for (i = 0; i < devs; i++) 183 if (strcmp(device_get_name(devlist[i]), "mlphy")) { 184 retval = device_get_softc(devlist[i]); 185 break; 186 } 187 free(devlist, M_TEMP); 188 return (retval); 189 } 190 191 static int 192 mlphy_service(xsc, mii, cmd) 193 struct mii_softc *xsc; 194 struct mii_data *mii; 195 int cmd; 196 { 197 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 198 struct mii_softc *other = NULL; 199 struct mlphy_softc *msc = (struct mlphy_softc *)xsc; 200 struct mii_softc *sc = (struct mii_softc *)&msc->ml_mii; 201 int other_inst, reg; 202 203 /* 204 * See if there's another PHY on this bus with us. 205 * If so, we may need it for 10Mbps modes. 206 */ 207 other = mlphy_find_other(msc->ml_mii.mii_dev); 208 209 switch (cmd) { 210 case MII_POLLSTAT: 211 break; 212 213 case MII_MEDIACHG: 214 /* 215 * If the interface is not up, don't do anything. 216 */ 217 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 218 break; 219 220 switch (IFM_SUBTYPE(ife->ifm_media)) { 221 case IFM_AUTO: 222 /* 223 * For autonegotiation, reset and isolate the 224 * companion PHY (if any) and then do NWAY 225 * autonegotiation ourselves. 226 */ 227 msc->ml_state = ML_STATE_AUTO_SELF; 228 if (other != NULL) { 229 mii_phy_reset(other); 230 PHY_WRITE(other, MII_BMCR, BMCR_ISO); 231 } 232 (void) mii_phy_auto(sc); 233 msc->ml_linked = 0; 234 return (0); 235 case IFM_10_T: 236 /* 237 * For 10baseT modes, reset and program the 238 * companion PHY (of any), then program ourselves 239 * to match. This will put us in pass-through 240 * mode and let the companion PHY do all the 241 * work. 242 * 243 * BMCR data is stored in the ifmedia entry. 244 */ 245 if (other != NULL) { 246 mii_phy_reset(other); 247 PHY_WRITE(other, MII_BMCR, ife->ifm_data); 248 } 249 PHY_WRITE(sc, MII_ANAR, mii_anar(ife->ifm_media)); 250 PHY_WRITE(sc, MII_BMCR, ife->ifm_data); 251 msc->ml_state = 0; 252 break; 253 case IFM_100_TX: 254 /* 255 * For 100baseTX modes, reset and isolate the 256 * companion PHY (if any), then program ourselves 257 * accordingly. 258 * 259 * BMCR data is stored in the ifmedia entry. 260 */ 261 if (other != NULL) { 262 mii_phy_reset(other); 263 PHY_WRITE(other, MII_BMCR, BMCR_ISO); 264 } 265 PHY_WRITE(sc, MII_ANAR, mii_anar(ife->ifm_media)); 266 PHY_WRITE(sc, MII_BMCR, ife->ifm_data); 267 msc->ml_state = 0; 268 break; 269 case IFM_100_T4: 270 /* 271 * XXX Not supported as a manual setting right now. 272 */ 273 return (EINVAL); 274 default: 275 break; 276 277 } 278 break; 279 280 case MII_TICK: 281 /* 282 * Is the interface even up? 283 */ 284 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 285 return (0); 286 287 /* 288 * Only used for autonegotiation. 289 */ 290 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) 291 break; 292 293 /* 294 * Check to see if we have link. If we do, we don't 295 * need to restart the autonegotiation process. Read 296 * the BMSR twice in case it's latched. 297 * If we're in a 10Mbps mode, check the link of the 298 * 10Mbps PHY. Sometimes the Micro Linear PHY's 299 * linkstat bit will clear while the linkstat bit of 300 * the companion PHY will remain set. 301 */ 302 if (msc->ml_state == ML_STATE_AUTO_OTHER) { 303 reg = PHY_READ(other, MII_BMSR) | 304 PHY_READ(other, MII_BMSR); 305 } else { 306 reg = PHY_READ(sc, MII_BMSR) | 307 PHY_READ(sc, MII_BMSR); 308 } 309 310 if (reg & BMSR_LINK) { 311 if (!msc->ml_linked) { 312 msc->ml_linked = 1; 313 mlphy_status(sc); 314 } 315 break; 316 } 317 318 /* 319 * Only retry autonegotiation every 5 seconds. 320 */ 321 if (++sc->mii_ticks <= MII_ANEGTICKS) 322 break; 323 324 sc->mii_ticks = 0; 325 msc->ml_linked = 0; 326 mii->mii_media_active = IFM_NONE; 327 mii_phy_reset(sc); 328 msc->ml_state = ML_STATE_AUTO_SELF; 329 if (other != NULL) { 330 mii_phy_reset(other); 331 PHY_WRITE(other, MII_BMCR, BMCR_ISO); 332 } 333 mii_phy_auto(sc); 334 return (0); 335 } 336 337 /* Update the media status. */ 338 339 if (msc->ml_state == ML_STATE_AUTO_OTHER) { 340 other_inst = other->mii_inst; 341 other->mii_inst = sc->mii_inst; 342 if (IFM_INST(ife->ifm_media) == other->mii_inst) 343 (void)(*other->mii_service)(other, mii, MII_POLLSTAT); 344 other->mii_inst = other_inst; 345 sc->mii_media_active = other->mii_media_active; 346 sc->mii_media_status = other->mii_media_status; 347 } else 348 ukphy_status(sc); 349 350 /* Callback if something changed. */ 351 mii_phy_update(sc, cmd); 352 return (0); 353 } 354 355 /* 356 * The Micro Linear PHY comes out of reset with the 'autoneg 357 * enable' bit set, which we don't want. 358 */ 359 static void 360 mlphy_reset(sc) 361 struct mii_softc *sc; 362 { 363 int reg; 364 365 mii_phy_reset(sc); 366 reg = PHY_READ(sc, MII_BMCR); 367 reg &= ~BMCR_AUTOEN; 368 PHY_WRITE(sc, MII_BMCR, reg); 369 } 370 371 /* 372 * If we negotiate a 10Mbps mode, we need to check for an alternate 373 * PHY and make sure it's enabled and set correctly. 374 */ 375 static void 376 mlphy_status(sc) 377 struct mii_softc *sc; 378 { 379 struct mlphy_softc *msc = (struct mlphy_softc *)sc; 380 struct mii_data *mii = msc->ml_mii.mii_pdata; 381 struct mii_softc *other = NULL; 382 383 /* See if there's another PHY on the bus with us. */ 384 other = mlphy_find_other(msc->ml_mii.mii_dev); 385 if (other == NULL) 386 return; 387 388 ukphy_status(sc); 389 390 if (IFM_SUBTYPE(mii->mii_media_active) != IFM_10_T) { 391 msc->ml_state = ML_STATE_AUTO_SELF; 392 mii_phy_reset(other); 393 PHY_WRITE(other, MII_BMCR, BMCR_ISO); 394 } 395 396 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_10_T) { 397 msc->ml_state = ML_STATE_AUTO_OTHER; 398 mlphy_reset(&msc->ml_mii); 399 PHY_WRITE(&msc->ml_mii, MII_BMCR, BMCR_ISO); 400 mii_phy_reset(other); 401 mii_phy_auto(other); 402 } 403 } 404