atphy.c revision 215298
1139749Simp/*- 2129794Stackerman * Copyright (c) 2008, Pyun YongHyeon <yongari@FreeBSD.org> 3129794Stackerman * All rights reserved. 4129794Stackerman * 5129794Stackerman * Redistribution and use in source and binary forms, with or without 6129794Stackerman * modification, are permitted provided that the following conditions 7129794Stackerman * are met: 8129794Stackerman * 1. Redistributions of source code must retain the above copyright 9129794Stackerman * notice unmodified, this list of conditions, and the following 10129794Stackerman * disclaimer. 11129794Stackerman * 2. Redistributions in binary form must reproduce the above copyright 12129794Stackerman * notice, this list of conditions and the following disclaimer in the 13129794Stackerman * documentation and/or other materials provided with the distribution. 14129794Stackerman * 15129794Stackerman * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16129794Stackerman * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17129794Stackerman * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18129794Stackerman * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19129794Stackerman * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20129794Stackerman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21129794Stackerman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22129794Stackerman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23129794Stackerman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24129794Stackerman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25129794Stackerman * SUCH DAMAGE. 26129794Stackerman */ 27129794Stackerman 28129794Stackerman#include <sys/cdefs.h> 29129794Stackerman__FBSDID("$FreeBSD: head/sys/dev/mii/atphy.c 215298 2010-11-14 13:31:01Z marius $"); 30129794Stackerman 31129794Stackerman/* 32129794Stackerman * Driver for the Attansic/Atheros F1 10/100/1000 PHY. 33129794Stackerman */ 34129794Stackerman 35129794Stackerman#include <sys/param.h> 36129794Stackerman#include <sys/systm.h> 37129794Stackerman#include <sys/kernel.h> 38129794Stackerman#include <sys/module.h> 39129794Stackerman#include <sys/socket.h> 40129794Stackerman#include <sys/bus.h> 41129794Stackerman 42129794Stackerman#include <net/if.h> 43129794Stackerman#include <net/if_media.h> 44129794Stackerman 45129794Stackerman#include <dev/mii/mii.h> 46129794Stackerman#include <dev/mii/miivar.h> 47129794Stackerman#include "miidevs.h" 48129794Stackerman 49129794Stackerman#include <dev/mii/atphyreg.h> 50129794Stackerman 51129794Stackerman#include "miibus_if.h" 52129794Stackerman 53129794Stackermanstatic int atphy_probe(device_t); 54129794Stackermanstatic int atphy_attach(device_t); 55129794Stackerman 56129794Stackermanstruct atphy_softc { 57129794Stackerman struct mii_softc mii_sc; 58129794Stackerman int mii_oui; 59129794Stackerman int mii_model; 60129794Stackerman int mii_rev; 61129794Stackerman}; 62129794Stackerman 63129794Stackermanstatic device_method_t atphy_methods[] = { 64129794Stackerman /* Device interface. */ 65129794Stackerman DEVMETHOD(device_probe, atphy_probe), 66129794Stackerman DEVMETHOD(device_attach, atphy_attach), 67129794Stackerman DEVMETHOD(device_detach, mii_phy_detach), 68129794Stackerman DEVMETHOD(device_shutdown, bus_generic_shutdown), 69129794Stackerman { NULL, NULL } 70129794Stackerman}; 71129794Stackerman 72129794Stackermanstatic devclass_t atphy_devclass; 73129794Stackermanstatic driver_t atphy_driver = { 74129794Stackerman "atphy", 75129794Stackerman atphy_methods, 76129794Stackerman sizeof(struct atphy_softc) 77129794Stackerman}; 78129794Stackerman 79129794StackermanDRIVER_MODULE(atphy, miibus, atphy_driver, atphy_devclass, 0, 0); 80129794Stackerman 81129794Stackermanstatic int atphy_service(struct mii_softc *, struct mii_data *, int); 82129794Stackermanstatic void atphy_status(struct mii_softc *); 83129794Stackermanstatic void atphy_reset(struct mii_softc *); 84129794Stackermanstatic uint16_t atphy_anar(struct ifmedia_entry *); 85129794Stackermanstatic int atphy_setmedia(struct mii_softc *, int); 86129794Stackerman 87129794Stackermanstatic const struct mii_phydesc atphys[] = { 88129794Stackerman MII_PHY_DESC(ATHEROS, F1), 89129794Stackerman MII_PHY_DESC(ATHEROS, F1_7), 90129794Stackerman MII_PHY_DESC(ATHEROS, F2), 91129794Stackerman MII_PHY_END 92129794Stackerman}; 93129794Stackerman 94129794Stackermanstatic int 95129794Stackermanatphy_probe(device_t dev) 96129794Stackerman{ 97129794Stackerman 98129794Stackerman return (mii_phy_dev_probe(dev, atphys, BUS_PROBE_DEFAULT)); 99129794Stackerman} 100129794Stackerman 101129794Stackermanstatic int 102129794Stackermanatphy_attach(device_t dev) 103129794Stackerman{ 104129794Stackerman struct atphy_softc *asc; 105129794Stackerman struct mii_softc *sc; 106129794Stackerman struct mii_attach_args *ma; 107129794Stackerman struct mii_data *mii; 108129794Stackerman 109129794Stackerman asc = device_get_softc(dev); 110129794Stackerman sc = &asc->mii_sc; 111129794Stackerman ma = device_get_ivars(dev); 112129794Stackerman sc->mii_dev = device_get_parent(dev); 113129794Stackerman mii = ma->mii_data; 114129794Stackerman LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list); 115129794Stackerman 116129794Stackerman sc->mii_flags = miibus_get_flags(dev); 117129794Stackerman sc->mii_inst = mii->mii_instance++; 118129794Stackerman sc->mii_phy = ma->mii_phyno; 119129794Stackerman sc->mii_service = atphy_service; 120129794Stackerman sc->mii_pdata = mii; 121 122 asc->mii_oui = MII_OUI(ma->mii_id1, ma->mii_id2); 123 asc->mii_model = MII_MODEL(ma->mii_id2); 124 asc->mii_rev = MII_REV(ma->mii_id2); 125 if (bootverbose) 126 device_printf(dev, "OUI 0x%06x, model 0x%04x, rev. %d\n", 127 asc->mii_oui, asc->mii_model, asc->mii_rev); 128 129 atphy_reset(sc); 130 131 sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & ma->mii_capmask; 132 if (sc->mii_capabilities & BMSR_EXTSTAT) 133 sc->mii_extcapabilities = PHY_READ(sc, MII_EXTSR); 134 device_printf(dev, " "); 135 mii_phy_add_media(sc); 136 printf("\n"); 137 138 MIIBUS_MEDIAINIT(sc->mii_dev); 139 return (0); 140} 141 142static int 143atphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd) 144{ 145 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 146 uint16_t anar, bmcr, bmsr; 147 148 switch (cmd) { 149 case MII_POLLSTAT: 150 break; 151 152 case MII_MEDIACHG: 153 /* 154 * If the interface is not up, don't do anything. 155 */ 156 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 157 break; 158 159 if (IFM_SUBTYPE(ife->ifm_media) == IFM_AUTO || 160 IFM_SUBTYPE(ife->ifm_media) == IFM_1000_T) { 161 atphy_setmedia(sc, ife->ifm_media); 162 break; 163 } 164 165 bmcr = 0; 166 switch (IFM_SUBTYPE(ife->ifm_media)) { 167 case IFM_100_TX: 168 bmcr = BMCR_S100; 169 break; 170 case IFM_10_T: 171 bmcr = BMCR_S10; 172 break; 173 case IFM_NONE: 174 bmcr = PHY_READ(sc, MII_BMCR); 175 /* 176 * XXX 177 * Due to an unknown reason powering down PHY resulted 178 * in unexpected results such as inaccessibility of 179 * hardware of freshly rebooted system. Disable 180 * powering down PHY until I got more information for 181 * Attansic/Atheros PHY hardwares. 182 */ 183 PHY_WRITE(sc, MII_BMCR, bmcr | BMCR_ISO); 184 goto done; 185 default: 186 return (EINVAL); 187 } 188 189 anar = atphy_anar(ife); 190 if (((ife->ifm_media & IFM_GMASK) & IFM_FDX) != 0) { 191 bmcr |= BMCR_FDX; 192 if (((ife->ifm_media & IFM_GMASK) & IFM_FLOW) != 0 || 193 (sc->mii_flags & MIIF_FORCEPAUSE) != 0) 194 anar |= ANAR_PAUSE_TOWARDS; 195 } 196 197 if ((sc->mii_extcapabilities & (EXTSR_1000TFDX | 198 EXTSR_1000THDX)) != 0) 199 PHY_WRITE(sc, MII_100T2CR, 0); 200 PHY_WRITE(sc, MII_ANAR, anar | ANAR_CSMA); 201 202 /* 203 * Reset the PHY so all changes take effect. 204 */ 205 PHY_WRITE(sc, MII_BMCR, bmcr | BMCR_RESET | BMCR_AUTOEN | 206 BMCR_STARTNEG); 207done: 208 break; 209 210 case MII_TICK: 211 /* 212 * Is the interface even up? 213 */ 214 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 215 return (0); 216 217 /* 218 * Only used for autonegotiation. 219 */ 220 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) { 221 sc->mii_ticks = 0; 222 break; 223 } 224 225 /* 226 * Check for link. 227 * Read the status register twice; BMSR_LINK is latch-low. 228 */ 229 bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR); 230 if (bmsr & BMSR_LINK) { 231 sc->mii_ticks = 0; 232 break; 233 } 234 235 /* Announce link loss right after it happens. */ 236 if (sc->mii_ticks++ == 0) 237 break; 238 if (sc->mii_ticks <= sc->mii_anegticks) 239 return (0); 240 241 sc->mii_ticks = 0; 242 atphy_setmedia(sc, ife->ifm_media); 243 break; 244 } 245 246 /* Update the media status. */ 247 atphy_status(sc); 248 249 /* Callback if something changed. */ 250 mii_phy_update(sc, cmd); 251 return (0); 252} 253 254static void 255atphy_status(struct mii_softc *sc) 256{ 257 struct mii_data *mii = sc->mii_pdata; 258 uint32_t bmsr, bmcr, ssr; 259 260 mii->mii_media_status = IFM_AVALID; 261 mii->mii_media_active = IFM_ETHER; 262 263 bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR); 264 if ((bmsr & BMSR_LINK) != 0) 265 mii->mii_media_status |= IFM_ACTIVE; 266 267 bmcr = PHY_READ(sc, MII_BMCR); 268 if ((bmcr & BMCR_ISO) != 0) { 269 mii->mii_media_active |= IFM_NONE; 270 mii->mii_media_status = 0; 271 return; 272 } 273 274 if ((bmcr & BMCR_LOOP) != 0) 275 mii->mii_media_active |= IFM_LOOP; 276 277 ssr = PHY_READ(sc, ATPHY_SSR); 278 if ((ssr & ATPHY_SSR_SPD_DPLX_RESOLVED) == 0) { 279 /* Erg, still trying, I guess... */ 280 mii->mii_media_active |= IFM_NONE; 281 return; 282 } 283 284 switch (ssr & ATPHY_SSR_SPEED_MASK) { 285 case ATPHY_SSR_1000MBS: 286 mii->mii_media_active |= IFM_1000_T; 287 /* 288 * atphy(4) has a valid link so reset mii_ticks. 289 * Resetting mii_ticks is needed in order to 290 * detect link loss after auto-negotiation. 291 */ 292 sc->mii_ticks = 0; 293 break; 294 case ATPHY_SSR_100MBS: 295 mii->mii_media_active |= IFM_100_TX; 296 sc->mii_ticks = 0; 297 break; 298 case ATPHY_SSR_10MBS: 299 mii->mii_media_active |= IFM_10_T; 300 sc->mii_ticks = 0; 301 break; 302 default: 303 mii->mii_media_active |= IFM_NONE; 304 return; 305 } 306 307 if ((ssr & ATPHY_SSR_DUPLEX) != 0) 308 mii->mii_media_active |= IFM_FDX | mii_phy_flowstatus(sc); 309 else 310 mii->mii_media_active |= IFM_HDX; 311 312 if ((IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T) && 313 (PHY_READ(sc, MII_100T2SR) & GTSR_MS_RES) != 0) 314 mii->mii_media_active |= IFM_ETH_MASTER; 315} 316 317static void 318atphy_reset(struct mii_softc *sc) 319{ 320 struct atphy_softc *asc; 321 uint32_t reg; 322 int i; 323 324 asc = (struct atphy_softc *)sc; 325 326 /* Take PHY out of power down mode. */ 327 PHY_WRITE(sc, 29, 0x29); 328 PHY_WRITE(sc, 30, 0); 329 330 reg = PHY_READ(sc, ATPHY_SCR); 331 /* Enable automatic crossover. */ 332 reg |= ATPHY_SCR_AUTO_X_MODE; 333 /* Disable power down. */ 334 reg &= ~ATPHY_SCR_MAC_PDOWN; 335 /* Enable CRS on Tx. */ 336 reg |= ATPHY_SCR_ASSERT_CRS_ON_TX; 337 /* Auto correction for reversed cable polarity. */ 338 reg |= ATPHY_SCR_POLARITY_REVERSAL; 339 PHY_WRITE(sc, ATPHY_SCR, reg); 340 341 /* Workaround F1 bug to reset phy. */ 342 atphy_setmedia(sc, sc->mii_pdata->mii_media.ifm_cur->ifm_media); 343 344 for (i = 0; i < 1000; i++) { 345 DELAY(1); 346 if ((PHY_READ(sc, MII_BMCR) & BMCR_RESET) == 0) 347 break; 348 } 349} 350 351static uint16_t 352atphy_anar(struct ifmedia_entry *ife) 353{ 354 uint16_t anar; 355 356 anar = 0; 357 switch (IFM_SUBTYPE(ife->ifm_media)) { 358 case IFM_AUTO: 359 anar |= ANAR_TX_FD | ANAR_TX | ANAR_10_FD | ANAR_10; 360 return (anar); 361 case IFM_1000_T: 362 return (anar); 363 case IFM_100_TX: 364 anar |= ANAR_TX; 365 break; 366 case IFM_10_T: 367 anar |= ANAR_10; 368 break; 369 default: 370 return (0); 371 } 372 373 if (((ife->ifm_media & IFM_GMASK) & IFM_FDX) != 0) { 374 if (IFM_SUBTYPE(ife->ifm_media) == IFM_100_TX) 375 anar |= ANAR_TX_FD; 376 else 377 anar |= ANAR_10_FD; 378 } 379 380 return (anar); 381} 382 383static int 384atphy_setmedia(struct mii_softc *sc, int media) 385{ 386 uint16_t anar; 387 388 anar = BMSR_MEDIA_TO_ANAR(sc->mii_capabilities) | ANAR_CSMA; 389 if (((IFM_SUBTYPE(media) == IFM_AUTO || 390 ((media & IFM_GMASK) & IFM_FDX) != 0) && 391 ((media & IFM_GMASK) & IFM_FLOW) != 0) || 392 (sc->mii_flags & MIIF_FORCEPAUSE) != 0) 393 anar |= ANAR_PAUSE_TOWARDS; 394 PHY_WRITE(sc, MII_ANAR, anar); 395 if ((sc->mii_extcapabilities & (EXTSR_1000TFDX | EXTSR_1000THDX)) != 0) 396 PHY_WRITE(sc, MII_100T2CR, GTCR_ADV_1000TFDX | 397 GTCR_ADV_1000THDX); 398 PHY_WRITE(sc, MII_BMCR, BMCR_RESET | BMCR_AUTOEN | BMCR_STARTNEG); 399 400 return (EJUSTRETURN); 401} 402