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 33 /* 34 * driver for the XaQti XMAC II's internal PHY. This is sort of 35 * like a 10/100 PHY, except the only thing we're really autoselecting 36 * here is full/half duplex. Speed is always 1000mbps. 37 */ 38 39 #include <sys/cdefs.h> 40 __FBSDID("$FreeBSD$"); 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/kernel.h> 45 #include <sys/socket.h> 46 #include <sys/bus.h> 47 48 #include <net/if.h> 49 #include <net/if_media.h> 50 51 #include <dev/mii/mii.h> 52 #include <dev/mii/miivar.h> 53 #include "miidevs.h" 54 55 #include <dev/mii/xmphyreg.h> 56 57 #include "miibus_if.h" 58 59 static int xmphy_probe(device_t); 60 static int xmphy_attach(device_t); 61 62 static device_method_t xmphy_methods[] = { 63 /* device interface */ 64 DEVMETHOD(device_probe, xmphy_probe), 65 DEVMETHOD(device_attach, xmphy_attach), 66 DEVMETHOD(device_detach, mii_phy_detach), 67 DEVMETHOD(device_shutdown, bus_generic_shutdown), 68 { 0, 0 } 69 }; 70 71 static devclass_t xmphy_devclass; 72 73 static driver_t xmphy_driver = { 74 "xmphy", 75 xmphy_methods, 76 sizeof(struct mii_softc) 77 }; 78 79 DRIVER_MODULE(xmphy, miibus, xmphy_driver, xmphy_devclass, 0, 0); 80 81 static int xmphy_service(struct mii_softc *, struct mii_data *, int); 82 static void xmphy_status(struct mii_softc *); 83 static int xmphy_mii_phy_auto(struct mii_softc *); 84 85 static int 86 xmphy_probe(dev) 87 device_t dev; 88 { 89 struct mii_attach_args *ma; 90 91 ma = device_get_ivars(dev); 92 93 if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_xxXAQTI && 94 MII_MODEL(ma->mii_id2) == MII_MODEL_XAQTI_XMACII) { 95 device_set_desc(dev, MII_STR_XAQTI_XMACII); 96 return(0); 97 } 98 99 if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_JATO && 100 MII_MODEL(ma->mii_id2) == MII_MODEL_JATO_BASEX) { 101 device_set_desc(dev, MII_STR_JATO_BASEX); 102 return(0); 103 } 104 105 return(ENXIO); 106 } 107 108 static int 109 xmphy_attach(dev) 110 device_t dev; 111 { 112 struct mii_softc *sc; 113 struct mii_attach_args *ma; 114 struct mii_data *mii; 115 const char *sep = ""; 116 117 sc = device_get_softc(dev); 118 ma = device_get_ivars(dev); 119 sc->mii_dev = device_get_parent(dev); 120 mii = device_get_softc(sc->mii_dev); 121 LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list); 122 123 sc->mii_inst = mii->mii_instance; 124 sc->mii_phy = ma->mii_phyno; 125 sc->mii_service = xmphy_service; 126 sc->mii_pdata = mii; 127 128 sc->mii_flags |= MIIF_NOISOLATE; 129 mii->mii_instance++; 130 131 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL) 132 #define PRINT(s) printf("%s%s", sep, s); sep = ", " 133 134 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst), 135 BMCR_ISO); 136 #if 0 137 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->mii_inst), 138 BMCR_LOOP|BMCR_S100); 139 #endif 140 141 mii_phy_reset(sc); 142 143 device_printf(dev, " "); 144 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, 0, sc->mii_inst), 145 XMPHY_BMCR_FDX); 146 PRINT("1000baseSX"); 147 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, IFM_FDX, sc->mii_inst), 0); 148 PRINT("1000baseSX-FDX"); 149 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst), 0); 150 PRINT("auto"); 151 152 printf("\n"); 153 #undef ADD 154 #undef PRINT 155 156 MIIBUS_MEDIAINIT(sc->mii_dev); 157 return(0); 158 } 159 160 static int 161 xmphy_service(sc, mii, cmd) 162 struct mii_softc *sc; 163 struct mii_data *mii; 164 int cmd; 165 { 166 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 167 int reg; 168 169 switch (cmd) { 170 case MII_POLLSTAT: 171 /* 172 * If we're not polling our PHY instance, just return. 173 */ 174 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 175 return (0); 176 break; 177 178 case MII_MEDIACHG: 179 /* 180 * If the media indicates a different PHY instance, 181 * isolate ourselves. 182 */ 183 if (IFM_INST(ife->ifm_media) != sc->mii_inst) { 184 reg = PHY_READ(sc, MII_BMCR); 185 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO); 186 return (0); 187 } 188 189 /* 190 * If the interface is not up, don't do anything. 191 */ 192 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 193 break; 194 195 switch (IFM_SUBTYPE(ife->ifm_media)) { 196 case IFM_AUTO: 197 #ifdef foo 198 /* 199 * If we're already in auto mode, just return. 200 */ 201 if (PHY_READ(sc, XMPHY_MII_BMCR) & XMPHY_BMCR_AUTOEN) 202 return (0); 203 #endif 204 (void) xmphy_mii_phy_auto(sc); 205 break; 206 case IFM_1000_SX: 207 mii_phy_reset(sc); 208 if ((ife->ifm_media & IFM_GMASK) == IFM_FDX) { 209 PHY_WRITE(sc, XMPHY_MII_ANAR, XMPHY_ANAR_FDX); 210 PHY_WRITE(sc, XMPHY_MII_BMCR, XMPHY_BMCR_FDX); 211 } else { 212 PHY_WRITE(sc, XMPHY_MII_ANAR, XMPHY_ANAR_HDX); 213 PHY_WRITE(sc, XMPHY_MII_BMCR, 0); 214 } 215 break; 216 case IFM_100_T4: 217 case IFM_100_TX: 218 case IFM_10_T: 219 default: 220 return (EINVAL); 221 } 222 break; 223 224 case MII_TICK: 225 /* 226 * If we're not currently selected, just return. 227 */ 228 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 229 return (0); 230 231 /* 232 * Is the interface even up? 233 */ 234 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 235 return (0); 236 237 /* 238 * Only used for autonegotiation. 239 */ 240 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) 241 break; 242 243 /* 244 * Check to see if we have link. If we do, we don't 245 * need to restart the autonegotiation process. Read 246 * the BMSR twice in case it's latched. 247 */ 248 reg = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR); 249 if (reg & BMSR_LINK) 250 break; 251 252 /* 253 * Only retry autonegotiation every 5 seconds. 254 */ 255 if (++sc->mii_ticks != 5) 256 return (0); 257 258 sc->mii_ticks = 0; 259 260 mii_phy_reset(sc); 261 xmphy_mii_phy_auto(sc); 262 return(0); 263 } 264 265 /* Update the media status. */ 266 xmphy_status(sc); 267 268 /* Callback if something changed. */ 269 mii_phy_update(sc, cmd); 270 return (0); 271 } 272 273 static void 274 xmphy_status(sc) 275 struct mii_softc *sc; 276 { 277 struct mii_data *mii = sc->mii_pdata; 278 int bmsr, bmcr, anlpar; 279 280 mii->mii_media_status = IFM_AVALID; 281 mii->mii_media_active = IFM_ETHER; 282 283 bmsr = PHY_READ(sc, XMPHY_MII_BMSR) | 284 PHY_READ(sc, XMPHY_MII_BMSR); 285 if (bmsr & XMPHY_BMSR_LINK) 286 mii->mii_media_status |= IFM_ACTIVE; 287 288 /* Do dummy read of extended status register. */ 289 bmcr = PHY_READ(sc, XMPHY_MII_EXTSTS); 290 291 bmcr = PHY_READ(sc, XMPHY_MII_BMCR); 292 293 if (bmcr & XMPHY_BMCR_LOOP) 294 mii->mii_media_active |= IFM_LOOP; 295 296 297 if (bmcr & XMPHY_BMCR_AUTOEN) { 298 if ((bmsr & XMPHY_BMSR_ACOMP) == 0) { 299 if (bmsr & XMPHY_BMSR_LINK) { 300 mii->mii_media_active |= IFM_1000_SX|IFM_HDX; 301 return; 302 } 303 /* Erg, still trying, I guess... */ 304 mii->mii_media_active |= IFM_NONE; 305 return; 306 } 307 308 mii->mii_media_active |= IFM_1000_SX; 309 anlpar = PHY_READ(sc, XMPHY_MII_ANAR) & 310 PHY_READ(sc, XMPHY_MII_ANLPAR); 311 if (anlpar & XMPHY_ANLPAR_FDX) 312 mii->mii_media_active |= IFM_FDX; 313 else 314 mii->mii_media_active |= IFM_HDX; 315 return; 316 } 317 318 mii->mii_media_active |= IFM_1000_SX; 319 if (bmcr & XMPHY_BMCR_FDX) 320 mii->mii_media_active |= IFM_FDX; 321 else 322 mii->mii_media_active |= IFM_HDX; 323 324 return; 325 } 326 327 328 static int 329 xmphy_mii_phy_auto(mii) 330 struct mii_softc *mii; 331 { 332 int anar = 0; 333 334 anar = PHY_READ(mii, XMPHY_MII_ANAR); 335 anar |= XMPHY_ANAR_FDX|XMPHY_ANAR_HDX; 336 PHY_WRITE(mii, XMPHY_MII_ANAR, anar); 337 DELAY(1000); 338 PHY_WRITE(mii, XMPHY_MII_BMCR, 339 XMPHY_BMCR_AUTOEN | XMPHY_BMCR_STARTNEG); 340 341 return (EJUSTRETURN); 342 } 343