1 /* 2 * Copyright (c) 2000 3 * Bill Paul <wpaul@ee.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$ 33 */ 34 35 /* 36 * Driver for the Broadcom BCR5400 1000baseTX PHY. Speed is always 37 * 1000mbps; all we need to negotiate here is full or half duplex. 38 */ 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/kernel.h> 43 #include <sys/malloc.h> 44 #include <sys/socket.h> 45 #include <sys/bus.h> 46 47 #include <machine/clock.h> 48 49 #include <net/if.h> 50 #include <net/if_media.h> 51 52 #include <dev/mii/mii.h> 53 #include <dev/mii/miivar.h> 54 #include <dev/mii/miidevs.h> 55 56 #include <dev/mii/brgphyreg.h> 57 58 #include "miibus_if.h" 59 60 #if !defined(lint) 61 static const char rcsid[] = 62 "$FreeBSD$"; 63 #endif 64 65 static int brgphy_probe (device_t); 66 static int brgphy_attach (device_t); 67 68 static device_method_t brgphy_methods[] = { 69 /* device interface */ 70 DEVMETHOD(device_probe, brgphy_probe), 71 DEVMETHOD(device_attach, brgphy_attach), 72 DEVMETHOD(device_detach, mii_phy_detach), 73 DEVMETHOD(device_shutdown, bus_generic_shutdown), 74 { 0, 0 } 75 }; 76 77 static devclass_t brgphy_devclass; 78 79 static driver_t brgphy_driver = { 80 "brgphy", 81 brgphy_methods, 82 sizeof(struct mii_softc) 83 }; 84 85 DRIVER_MODULE(brgphy, miibus, brgphy_driver, brgphy_devclass, 0, 0); 86 87 static int brgphy_service(struct mii_softc *, struct mii_data *, int); 88 static void brgphy_status(struct mii_softc *); 89 static int brgphy_mii_phy_auto(struct mii_softc *); 90 91 static int brgphy_probe(dev) 92 device_t dev; 93 { 94 struct mii_attach_args *ma; 95 96 ma = device_get_ivars(dev); 97 98 if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_xxBROADCOM && 99 MII_MODEL(ma->mii_id2) == MII_MODEL_xxBROADCOM_BCM5400) { 100 device_set_desc(dev, MII_STR_xxBROADCOM_BCM5400); 101 return(0); 102 } 103 104 if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_xxBROADCOM && 105 MII_MODEL(ma->mii_id2) == MII_MODEL_xxBROADCOM_BCM5401) { 106 device_set_desc(dev, MII_STR_xxBROADCOM_BCM5401); 107 return(0); 108 } 109 110 if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_xxBROADCOM && 111 MII_MODEL(ma->mii_id2) == MII_MODEL_xxBROADCOM_BCM5411) { 112 device_set_desc(dev, MII_STR_xxBROADCOM_BCM5411); 113 return(0); 114 } 115 116 if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_xxBROADCOM && 117 MII_MODEL(ma->mii_id2) == MII_MODEL_xxBROADCOM_BCM5701) { 118 device_set_desc(dev, MII_STR_xxBROADCOM_BCM5701); 119 return(0); 120 } 121 122 return(ENXIO); 123 } 124 125 static int brgphy_attach(dev) 126 device_t dev; 127 { 128 struct mii_softc *sc; 129 struct mii_attach_args *ma; 130 struct mii_data *mii; 131 const char *sep = ""; 132 133 sc = device_get_softc(dev); 134 ma = device_get_ivars(dev); 135 sc->mii_dev = device_get_parent(dev); 136 mii = device_get_softc(sc->mii_dev); 137 LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list); 138 139 sc->mii_inst = mii->mii_instance; 140 sc->mii_phy = ma->mii_phyno; 141 sc->mii_service = brgphy_service; 142 sc->mii_pdata = mii; 143 144 sc->mii_flags |= MIIF_NOISOLATE; 145 mii->mii_instance++; 146 147 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL) 148 #define PRINT(s) printf("%s%s", sep, s); sep = ", " 149 150 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst), 151 BMCR_ISO); 152 #if 0 153 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->mii_inst), 154 BMCR_LOOP|BMCR_S100); 155 #endif 156 157 mii_phy_reset(sc); 158 159 160 sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & ma->mii_capmask; 161 sc->mii_capabilities &= ~BMSR_ANEG; 162 device_printf(dev, " "); 163 mii_add_media(sc); 164 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, 0, sc->mii_inst), 165 BRGPHY_BMCR_FDX); 166 PRINT(", 1000baseTX"); 167 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, IFM_FDX, sc->mii_inst), 0); 168 PRINT("1000baseTX-FDX"); 169 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst), 0); 170 PRINT("auto"); 171 172 printf("\n"); 173 #undef ADD 174 #undef PRINT 175 176 MIIBUS_MEDIAINIT(sc->mii_dev); 177 return(0); 178 } 179 180 static int 181 brgphy_service(sc, mii, cmd) 182 struct mii_softc *sc; 183 struct mii_data *mii; 184 int cmd; 185 { 186 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 187 int reg, speed; 188 189 switch (cmd) { 190 case MII_POLLSTAT: 191 /* 192 * If we're not polling our PHY instance, just return. 193 */ 194 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 195 return (0); 196 break; 197 198 case MII_MEDIACHG: 199 /* 200 * If the media indicates a different PHY instance, 201 * isolate ourselves. 202 */ 203 if (IFM_INST(ife->ifm_media) != sc->mii_inst) { 204 reg = PHY_READ(sc, MII_BMCR); 205 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO); 206 return (0); 207 } 208 209 /* 210 * If the interface is not up, don't do anything. 211 */ 212 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 213 break; 214 215 PHY_WRITE(sc, BRGPHY_MII_PHY_EXTCTL, 216 BRGPHY_PHY_EXTCTL_HIGH_LA|BRGPHY_PHY_EXTCTL_EN_LTR); 217 PHY_WRITE(sc, BRGPHY_MII_AUXCTL, 218 BRGPHY_AUXCTL_LONG_PKT|BRGPHY_AUXCTL_TX_TST); 219 PHY_WRITE(sc, BRGPHY_MII_IMR, 0xFF00); 220 221 switch (IFM_SUBTYPE(ife->ifm_media)) { 222 case IFM_AUTO: 223 #ifdef foo 224 /* 225 * If we're already in auto mode, just return. 226 */ 227 if (PHY_READ(sc, BRGPHY_MII_BMCR) & BRGPHY_BMCR_AUTOEN) 228 return (0); 229 #endif 230 (void) brgphy_mii_phy_auto(sc); 231 break; 232 case IFM_1000_T: 233 speed = BRGPHY_S1000; 234 goto setit; 235 case IFM_100_TX: 236 speed = BRGPHY_S100; 237 goto setit; 238 case IFM_10_T: 239 speed = BRGPHY_S10; 240 setit: 241 if ((ife->ifm_media & IFM_GMASK) == IFM_FDX) { 242 PHY_WRITE(sc, BRGPHY_MII_BMCR, 243 BRGPHY_BMCR_FDX|speed); 244 } else { 245 PHY_WRITE(sc, BRGPHY_MII_BMCR, speed); 246 } 247 PHY_WRITE(sc, BRGPHY_MII_ANAR, BRGPHY_SEL_TYPE); 248 249 if (IFM_SUBTYPE(ife->ifm_media) != IFM_1000_T) 250 break; 251 252 /* 253 * When settning the link manually, one side must 254 * be the master and the other the slave. However 255 * ifmedia doesn't give us a good way to specify 256 * this, so we fake it by using one of the LINK 257 * flags. If LINK0 is set, we program the PHY to 258 * be a master, otherwise it's a slave. 259 */ 260 if ((mii->mii_ifp->if_flags & IFF_LINK0)) { 261 PHY_WRITE(sc, BRGPHY_MII_1000CTL, 262 BRGPHY_1000CTL_MSE|BRGPHY_1000CTL_MSC); 263 } else { 264 PHY_WRITE(sc, BRGPHY_MII_1000CTL, 265 BRGPHY_1000CTL_MSE); 266 } 267 break; 268 #ifdef foo 269 case IFM_NONE: 270 PHY_WRITE(sc, MII_BMCR, BMCR_ISO|BMCR_PDOWN); 271 break; 272 #endif 273 case IFM_100_T4: 274 default: 275 return (EINVAL); 276 } 277 break; 278 279 case MII_TICK: 280 /* 281 * If we're not currently selected, just return. 282 */ 283 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 284 return (0); 285 286 /* 287 * Is the interface even up? 288 */ 289 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 290 return (0); 291 292 /* 293 * Only used for autonegotiation. 294 */ 295 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) 296 break; 297 298 /* 299 * Check to see if we have link. If we do, we don't 300 * need to restart the autonegotiation process. Read 301 * the BMSR twice in case it's latched. 302 */ 303 reg = PHY_READ(sc, BRGPHY_MII_AUXSTS); 304 if (reg & BRGPHY_AUXSTS_LINK) 305 break; 306 307 /* 308 * Only retry autonegotiation every 5 seconds. 309 */ 310 if (++sc->mii_ticks != 5) 311 return (0); 312 313 sc->mii_ticks = 0; 314 mii_phy_reset(sc); 315 brgphy_mii_phy_auto(sc); 316 return (0); 317 } 318 319 /* Update the media status. */ 320 brgphy_status(sc); 321 322 /* Callback if something changed. */ 323 mii_phy_update(sc, cmd); 324 return (0); 325 } 326 327 static void 328 brgphy_status(sc) 329 struct mii_softc *sc; 330 { 331 struct mii_data *mii = sc->mii_pdata; 332 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 333 int bmsr, bmcr; 334 335 mii->mii_media_status = IFM_AVALID; 336 mii->mii_media_active = IFM_ETHER; 337 338 bmsr = PHY_READ(sc, BRGPHY_MII_BMSR); 339 if (PHY_READ(sc, BRGPHY_MII_AUXSTS) & BRGPHY_AUXSTS_LINK) 340 mii->mii_media_status |= IFM_ACTIVE; 341 342 bmcr = PHY_READ(sc, BRGPHY_MII_BMCR); 343 344 if (bmcr & BRGPHY_BMCR_LOOP) 345 mii->mii_media_active |= IFM_LOOP; 346 347 if (bmcr & BRGPHY_BMCR_AUTOEN) { 348 if ((bmsr & BRGPHY_BMSR_ACOMP) == 0) { 349 /* Erg, still trying, I guess... */ 350 mii->mii_media_active |= IFM_NONE; 351 return; 352 } 353 354 switch (PHY_READ(sc, BRGPHY_MII_AUXSTS) & 355 BRGPHY_AUXSTS_AN_RES) { 356 case BRGPHY_RES_1000FD: 357 mii->mii_media_active |= IFM_1000_T | IFM_FDX; 358 break; 359 case BRGPHY_RES_1000HD: 360 mii->mii_media_active |= IFM_1000_T | IFM_HDX; 361 break; 362 case BRGPHY_RES_100FD: 363 mii->mii_media_active |= IFM_100_TX | IFM_FDX; 364 break; 365 case BRGPHY_RES_100T4: 366 mii->mii_media_active |= IFM_100_T4; 367 break; 368 case BRGPHY_RES_100HD: 369 mii->mii_media_active |= IFM_100_TX | IFM_HDX; 370 break; 371 case BRGPHY_RES_10FD: 372 mii->mii_media_active |= IFM_10_T | IFM_FDX; 373 break; 374 case BRGPHY_RES_10HD: 375 mii->mii_media_active |= IFM_10_T | IFM_HDX; 376 break; 377 default: 378 mii->mii_media_active |= IFM_NONE; 379 break; 380 } 381 return; 382 } 383 384 mii->mii_media_active = ife->ifm_media; 385 386 return; 387 } 388 389 390 static int 391 brgphy_mii_phy_auto(mii) 392 struct mii_softc *mii; 393 { 394 int ktcr = 0; 395 396 mii_phy_reset(mii); 397 PHY_WRITE(mii, BRGPHY_MII_BMCR, 0); 398 DELAY(1000); 399 ktcr = PHY_READ(mii, BRGPHY_MII_1000CTL); 400 PHY_WRITE(mii, BRGPHY_MII_1000CTL, ktcr | 401 BRGPHY_1000CTL_AFD|BRGPHY_1000CTL_AHD); 402 ktcr = PHY_READ(mii, BRGPHY_MII_1000CTL); 403 DELAY(1000); 404 PHY_WRITE(mii, BRGPHY_MII_ANAR, 405 BMSR_MEDIA_TO_ANAR(mii->mii_capabilities) | ANAR_CSMA); 406 DELAY(1000); 407 PHY_WRITE(mii, BRGPHY_MII_BMCR, 408 BRGPHY_BMCR_AUTOEN | BRGPHY_BMCR_STARTNEG); 409 PHY_WRITE(mii, BRGPHY_MII_IMR, 0xFF00); 410 return (EJUSTRETURN); 411 } 412