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