1 /*- 2 * Copyright (c) 2006, Pyun YongHyeon <yongari@FreeBSD.org> 3 * 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 unmodified, this list of conditions, and the following 10 * disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 * 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 /* 33 * Driver for the IC Plus IP1000A 10/100/1000 PHY. 34 */ 35 36 #include <sys/param.h> 37 #include <sys/systm.h> 38 #include <sys/kernel.h> 39 #include <sys/module.h> 40 #include <sys/socket.h> 41 #include <sys/bus.h> 42 43 #include <net/if.h> 44 #include <net/if_media.h> 45 46 #include <dev/mii/mii.h> 47 #include <dev/mii/miivar.h> 48 #include "miidevs.h" 49 50 #include <dev/mii/ip1000phyreg.h> 51 52 #include "miibus_if.h" 53 54 #include <machine/bus.h> 55 #include <dev/stge/if_stgereg.h> 56 57 static int ip1000phy_probe(device_t); 58 static int ip1000phy_attach(device_t); 59 60 static device_method_t ip1000phy_methods[] = { 61 /* device interface */ 62 DEVMETHOD(device_probe, ip1000phy_probe), 63 DEVMETHOD(device_attach, ip1000phy_attach), 64 DEVMETHOD(device_detach, mii_phy_detach), 65 DEVMETHOD(device_shutdown, bus_generic_shutdown), 66 { 0, 0 } 67 }; 68 69 static devclass_t ip1000phy_devclass; 70 static driver_t ip1000phy_driver = { 71 "ip1000phy", 72 ip1000phy_methods, 73 sizeof (struct mii_softc) 74 }; 75 76 DRIVER_MODULE(ip1000phy, miibus, ip1000phy_driver, ip1000phy_devclass, 0, 0); 77 78 static int ip1000phy_service(struct mii_softc *, struct mii_data *, int); 79 static void ip1000phy_status(struct mii_softc *); 80 static void ip1000phy_reset(struct mii_softc *); 81 static int ip1000phy_mii_phy_auto(struct mii_softc *); 82 83 static const struct mii_phydesc ip1000phys[] = { 84 MII_PHY_DESC(ICPLUS, IP1000A), 85 MII_PHY_END 86 }; 87 88 static int 89 ip1000phy_probe(device_t dev) 90 { 91 92 return (mii_phy_dev_probe(dev, ip1000phys, BUS_PROBE_DEFAULT)); 93 } 94 95 static int 96 ip1000phy_attach(device_t dev) 97 { 98 struct mii_softc *sc; 99 struct mii_attach_args *ma; 100 struct mii_data *mii; 101 102 sc = device_get_softc(dev); 103 ma = device_get_ivars(dev); 104 sc->mii_dev = device_get_parent(dev); 105 mii = device_get_softc(sc->mii_dev); 106 LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list); 107 108 sc->mii_inst = mii->mii_instance; 109 sc->mii_phy = ma->mii_phyno; 110 sc->mii_service = ip1000phy_service; 111 sc->mii_pdata = mii; 112 sc->mii_anegticks = MII_ANEGTICKS_GIGE; 113 sc->mii_flags |= MIIF_NOISOLATE; 114 115 mii->mii_instance++; 116 117 device_printf(dev, " "); 118 119 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL) 120 121 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst), 122 BMCR_ISO); 123 124 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, sc->mii_inst), 125 IP1000PHY_BMCR_10); 126 printf("10baseT, "); 127 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, sc->mii_inst), 128 IP1000PHY_BMCR_10 | IP1000PHY_BMCR_FDX); 129 printf("10baseT-FDX, "); 130 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, sc->mii_inst), 131 IP1000PHY_BMCR_100); 132 printf("100baseTX, "); 133 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, sc->mii_inst), 134 IP1000PHY_BMCR_100 | IP1000PHY_BMCR_FDX); 135 printf("100baseTX-FDX, "); 136 /* 1000baseT half-duplex, really supported? */ 137 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, 0, sc->mii_inst), 138 IP1000PHY_BMCR_1000); 139 printf("1000baseTX, "); 140 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, IFM_FDX, sc->mii_inst), 141 IP1000PHY_BMCR_1000 | IP1000PHY_BMCR_FDX); 142 printf("1000baseTX-FDX, "); 143 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst), 0); 144 printf("auto\n"); 145 #undef ADD 146 147 ip1000phy_reset(sc); 148 149 MIIBUS_MEDIAINIT(sc->mii_dev); 150 return(0); 151 } 152 153 static int 154 ip1000phy_service(struct mii_softc *sc, struct mii_data *mii, int cmd) 155 { 156 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 157 uint32_t gig, reg, speed; 158 159 switch (cmd) { 160 case MII_POLLSTAT: 161 /* 162 * If we're not polling our PHY instance, just return. 163 */ 164 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 165 return (0); 166 break; 167 168 case MII_MEDIACHG: 169 /* 170 * If the media indicates a different PHY instance, 171 * isolate ourselves. 172 */ 173 if (IFM_INST(ife->ifm_media) != sc->mii_inst) { 174 reg = PHY_READ(sc, IP1000PHY_MII_BMCR); 175 PHY_WRITE(sc, IP1000PHY_MII_BMCR, 176 reg | IP1000PHY_BMCR_ISO); 177 return (0); 178 } 179 180 /* 181 * If the interface is not up, don't do anything. 182 */ 183 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) { 184 break; 185 } 186 187 ip1000phy_reset(sc); 188 switch (IFM_SUBTYPE(ife->ifm_media)) { 189 case IFM_AUTO: 190 (void)ip1000phy_mii_phy_auto(sc); 191 goto done; 192 break; 193 194 case IFM_1000_T: 195 /* 196 * XXX 197 * Manual 1000baseT setting doesn't seem to work. 198 */ 199 speed = IP1000PHY_BMCR_1000; 200 break; 201 202 case IFM_100_TX: 203 speed = IP1000PHY_BMCR_100; 204 break; 205 206 case IFM_10_T: 207 speed = IP1000PHY_BMCR_10; 208 break; 209 210 default: 211 return (EINVAL); 212 } 213 214 if (((ife->ifm_media & IFM_GMASK) & IFM_FDX) != 0) { 215 speed |= IP1000PHY_BMCR_FDX; 216 gig = IP1000PHY_1000CR_1000T_FDX; 217 } else 218 gig = IP1000PHY_1000CR_1000T; 219 220 PHY_WRITE(sc, IP1000PHY_MII_1000CR, 0); 221 PHY_WRITE(sc, IP1000PHY_MII_BMCR, speed); 222 223 if (IFM_SUBTYPE(ife->ifm_media) != IFM_1000_T) 224 break; 225 226 PHY_WRITE(sc, IP1000PHY_MII_1000CR, gig); 227 PHY_WRITE(sc, IP1000PHY_MII_BMCR, speed); 228 229 /* 230 * When setting the link manually, one side must 231 * be the master and the other the slave. However 232 * ifmedia doesn't give us a good way to specify 233 * this, so we fake it by using one of the LINK 234 * flags. If LINK0 is set, we program the PHY to 235 * be a master, otherwise it's a slave. 236 */ 237 if ((mii->mii_ifp->if_flags & IFF_LINK0)) 238 PHY_WRITE(sc, IP1000PHY_MII_1000CR, gig | 239 IP1000PHY_1000CR_MASTER | 240 IP1000PHY_1000CR_MMASTER | 241 IP1000PHY_1000CR_MANUAL); 242 else 243 PHY_WRITE(sc, IP1000PHY_MII_1000CR, gig | 244 IP1000PHY_1000CR_MASTER | 245 IP1000PHY_1000CR_MANUAL); 246 247 done: 248 break; 249 250 case MII_TICK: 251 /* 252 * If we're not currently selected, just return. 253 */ 254 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 255 return (0); 256 /* 257 * Is the interface even up? 258 */ 259 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 260 return (0); 261 262 /* 263 * Only used for autonegotiation. 264 */ 265 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) { 266 sc->mii_ticks = 0; 267 break; 268 } 269 270 /* 271 * check for link. 272 */ 273 reg = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR); 274 if (reg & BMSR_LINK) { 275 sc->mii_ticks = 0; 276 break; 277 } 278 279 /* Announce link loss right after it happens */ 280 if (sc->mii_ticks++ == 0) 281 break; 282 283 /* 284 * Only retry autonegotiation every mii_anegticks seconds. 285 */ 286 if (sc->mii_ticks <= sc->mii_anegticks) 287 return (0); 288 289 sc->mii_ticks = 0; 290 ip1000phy_mii_phy_auto(sc); 291 break; 292 } 293 294 /* Update the media status. */ 295 ip1000phy_status(sc); 296 297 /* Callback if something changed. */ 298 mii_phy_update(sc, cmd); 299 return (0); 300 } 301 302 static void 303 ip1000phy_status(struct mii_softc *sc) 304 { 305 struct mii_data *mii = sc->mii_pdata; 306 uint32_t bmsr, bmcr, stat; 307 uint32_t ar, lpar; 308 309 mii->mii_media_status = IFM_AVALID; 310 mii->mii_media_active = IFM_ETHER; 311 312 bmsr = PHY_READ(sc, IP1000PHY_MII_BMSR) | 313 PHY_READ(sc, IP1000PHY_MII_BMSR); 314 if ((bmsr & IP1000PHY_BMSR_LINK) != 0) 315 mii->mii_media_status |= IFM_ACTIVE; 316 317 bmcr = PHY_READ(sc, IP1000PHY_MII_BMCR); 318 if ((bmcr & IP1000PHY_BMCR_LOOP) != 0) 319 mii->mii_media_active |= IFM_LOOP; 320 321 if ((bmcr & IP1000PHY_BMCR_AUTOEN) != 0) { 322 if ((bmsr & IP1000PHY_BMSR_ANEGCOMP) == 0) { 323 /* Erg, still trying, I guess... */ 324 mii->mii_media_active |= IFM_NONE; 325 return; 326 } 327 } 328 329 stat = PHY_READ(sc, STGE_PhyCtrl); 330 switch (PC_LinkSpeed(stat)) { 331 case PC_LinkSpeed_Down: 332 mii->mii_media_active |= IFM_NONE; 333 return; 334 case PC_LinkSpeed_10: 335 mii->mii_media_active |= IFM_10_T; 336 break; 337 case PC_LinkSpeed_100: 338 mii->mii_media_active |= IFM_100_TX; 339 break; 340 case PC_LinkSpeed_1000: 341 mii->mii_media_active |= IFM_1000_T; 342 break; 343 } 344 if ((stat & PC_PhyDuplexStatus) != 0) 345 mii->mii_media_active |= IFM_FDX; 346 else 347 mii->mii_media_active |= IFM_HDX; 348 349 ar = PHY_READ(sc, IP1000PHY_MII_ANAR); 350 lpar = PHY_READ(sc, IP1000PHY_MII_ANLPAR); 351 352 /* 353 * FLAG0 : Rx flow-control 354 * FLAG1 : Tx flow-control 355 */ 356 if ((ar & IP1000PHY_ANAR_PAUSE) && (lpar & IP1000PHY_ANLPAR_PAUSE)) 357 mii->mii_media_active |= IFM_FLAG0 | IFM_FLAG1; 358 else if (!(ar & IP1000PHY_ANAR_PAUSE) && (ar & IP1000PHY_ANAR_APAUSE) && 359 (lpar & IP1000PHY_ANLPAR_PAUSE) && (lpar & IP1000PHY_ANLPAR_APAUSE)) 360 mii->mii_media_active |= IFM_FLAG1; 361 else if ((ar & IP1000PHY_ANAR_PAUSE) && (ar & IP1000PHY_ANAR_APAUSE) && 362 !(lpar & IP1000PHY_ANLPAR_PAUSE) && 363 (lpar & IP1000PHY_ANLPAR_APAUSE)) { 364 mii->mii_media_active |= IFM_FLAG0; 365 } 366 367 /* 368 * FLAG2 : local PHY resolved to MASTER 369 */ 370 if ((mii->mii_media_active & IFM_1000_T) != 0) { 371 stat = PHY_READ(sc, IP1000PHY_MII_1000SR); 372 if ((stat & IP1000PHY_1000SR_MASTER) != 0) 373 mii->mii_media_active |= IFM_FLAG2; 374 } 375 } 376 377 static int 378 ip1000phy_mii_phy_auto(struct mii_softc *mii) 379 { 380 uint32_t reg; 381 382 PHY_WRITE(mii, IP1000PHY_MII_ANAR, 383 IP1000PHY_ANAR_10T | IP1000PHY_ANAR_10T_FDX | 384 IP1000PHY_ANAR_100TX | IP1000PHY_ANAR_100TX_FDX | 385 IP1000PHY_ANAR_PAUSE | IP1000PHY_ANAR_APAUSE); 386 reg = IP1000PHY_1000CR_1000T | IP1000PHY_1000CR_1000T_FDX; 387 reg |= IP1000PHY_1000CR_MASTER; 388 PHY_WRITE(mii, IP1000PHY_MII_1000CR, reg); 389 PHY_WRITE(mii, IP1000PHY_MII_BMCR, (IP1000PHY_BMCR_FDX | 390 IP1000PHY_BMCR_AUTOEN | IP1000PHY_BMCR_STARTNEG)); 391 392 return (EJUSTRETURN); 393 } 394 395 static void 396 ip1000phy_load_dspcode(struct mii_softc *sc) 397 { 398 399 PHY_WRITE(sc, 31, 0x0001); 400 PHY_WRITE(sc, 27, 0x01e0); 401 PHY_WRITE(sc, 31, 0x0002); 402 PHY_WRITE(sc, 27, 0xeb8e); 403 PHY_WRITE(sc, 31, 0x0000); 404 PHY_WRITE(sc, 30, 0x005e); 405 PHY_WRITE(sc, 9, 0x0700); 406 407 DELAY(50); 408 } 409 410 static void 411 ip1000phy_reset(struct mii_softc *sc) 412 { 413 struct stge_softc *stge_sc; 414 struct mii_data *mii; 415 uint32_t reg; 416 417 mii_phy_reset(sc); 418 419 /* clear autoneg/full-duplex as we don't want it after reset */ 420 reg = PHY_READ(sc, IP1000PHY_MII_BMCR); 421 reg &= ~(IP1000PHY_BMCR_AUTOEN | IP1000PHY_BMCR_FDX); 422 PHY_WRITE(sc, MII_BMCR, reg); 423 424 mii = sc->mii_pdata; 425 /* 426 * XXX There should be more general way to pass PHY specific 427 * data via mii interface. 428 */ 429 if (strcmp(mii->mii_ifp->if_dname, "stge") == 0) { 430 stge_sc = mii->mii_ifp->if_softc; 431 if (stge_sc->sc_rev >= 0x40 && stge_sc->sc_rev <= 0x4e) 432 ip1000phy_load_dspcode(sc); 433 } 434 } 435