1 /*- 2 * Copyright (c) 2001 Jonathan Lemon 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, 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. Neither the name of the author nor the names of any co-contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * $FreeBSD: src/sys/dev/mii/inphy.c,v 1.4.2.2 2002/11/08 21:53:49 semenu Exp $ 30 * $DragonFly: src/sys/dev/netif/mii_layer/inphy.c,v 1.4 2004/09/18 19:32:59 dillon Exp $ 31 */ 32 33 /* 34 * driver for Intel 82553 and 82555 PHYs 35 */ 36 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/kernel.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 "mii.h" 47 #include "miivar.h" 48 #include "miidevs.h" 49 50 #include "inphyreg.h" 51 52 #include "miibus_if.h" 53 54 static int inphy_probe(device_t dev); 55 static int inphy_attach(device_t dev); 56 static int inphy_detach(device_t dev); 57 58 static device_method_t inphy_methods[] = { 59 /* device interface */ 60 DEVMETHOD(device_probe, inphy_probe), 61 DEVMETHOD(device_attach, inphy_attach), 62 DEVMETHOD(device_detach, inphy_detach), 63 DEVMETHOD(device_shutdown, bus_generic_shutdown), 64 { 0, 0 } 65 }; 66 67 static devclass_t inphy_devclass; 68 69 static driver_t inphy_driver = { 70 "inphy", 71 inphy_methods, 72 sizeof(struct mii_softc) 73 }; 74 75 DRIVER_MODULE(inphy, miibus, inphy_driver, inphy_devclass, 0, 0); 76 77 int inphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd); 78 void inphy_status(struct mii_softc *sc); 79 80 81 static int 82 inphy_probe(device_t dev) 83 { 84 struct mii_attach_args *ma; 85 device_t parent; 86 87 ma = device_get_ivars(dev); 88 parent = device_get_parent(device_get_parent(dev)); 89 90 /* Intel 82553 A/B steppings */ 91 if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_xxINTEL && 92 MII_MODEL(ma->mii_id2) == MII_MODEL_xxINTEL_I82553AB) { 93 device_set_desc(dev, MII_STR_xxINTEL_I82553AB); 94 return (0); 95 } 96 97 if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_INTEL) { 98 switch (MII_MODEL(ma->mii_id2)) { 99 case MII_MODEL_INTEL_I82555: 100 device_set_desc(dev, MII_STR_INTEL_I82555); 101 return (0); 102 case MII_MODEL_INTEL_I82553C: 103 device_set_desc(dev, MII_STR_INTEL_I82553C); 104 return (0); 105 case MII_MODEL_INTEL_I82562EM: 106 device_set_desc(dev, MII_STR_INTEL_I82562EM); 107 return (0); 108 case MII_MODEL_INTEL_I82562ET: 109 device_set_desc(dev, MII_STR_INTEL_I82562ET); 110 return (0); 111 } 112 } 113 114 return (ENXIO); 115 } 116 117 static int 118 inphy_attach(device_t dev) 119 { 120 struct mii_softc *sc; 121 struct mii_attach_args *ma; 122 struct mii_data *mii; 123 124 sc = device_get_softc(dev); 125 ma = device_get_ivars(dev); 126 mii_softc_init(sc); 127 sc->mii_dev = device_get_parent(dev); 128 mii = device_get_softc(sc->mii_dev); 129 LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list); 130 131 sc->mii_inst = mii->mii_instance; 132 sc->mii_phy = ma->mii_phyno; 133 sc->mii_service = inphy_service; 134 sc->mii_pdata = mii; 135 mii->mii_instance++; 136 137 #if 0 138 sc->mii_flags |= MIIF_NOISOLATE; 139 #endif 140 141 ifmedia_add(&mii->mii_media, 142 IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->mii_inst), 143 BMCR_LOOP|BMCR_S100, NULL); 144 145 mii_phy_reset(sc); 146 147 sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & ma->mii_capmask; 148 device_printf(dev, " "); 149 if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0) 150 printf("no media present"); 151 else 152 mii_add_media(mii, sc->mii_capabilities, sc->mii_inst); 153 printf("\n"); 154 155 MIIBUS_MEDIAINIT(sc->mii_dev); 156 157 return (0); 158 } 159 160 static int 161 inphy_detach(device_t dev) 162 { 163 struct mii_softc *sc; 164 struct mii_data *mii; 165 166 sc = device_get_softc(dev); 167 mii = device_get_softc(device_get_softc(dev)); 168 mii_phy_auto_stop(sc); 169 sc->mii_dev = NULL; 170 LIST_REMOVE(sc, mii_list); 171 172 return (0); 173 } 174 175 int 176 inphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd) 177 { 178 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 179 int reg; 180 181 switch (cmd) { 182 case MII_POLLSTAT: 183 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 184 return (0); 185 break; 186 187 case MII_MEDIACHG: 188 if (IFM_INST(ife->ifm_media) != sc->mii_inst) { 189 reg = PHY_READ(sc, MII_BMCR); 190 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO); 191 return (0); 192 } 193 194 /* 195 * If the interface is not up, don't do anything. 196 */ 197 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 198 break; 199 200 switch (IFM_SUBTYPE(ife->ifm_media)) { 201 case IFM_AUTO: 202 /* 203 * If we're already in auto mode, just return. 204 */ 205 if (PHY_READ(sc, MII_BMCR) & BMCR_AUTOEN) 206 return (0); 207 (void) mii_phy_auto(sc, 0); 208 break; 209 case IFM_100_T4: 210 /* 211 * XXX Not supported as a manual setting right now. 212 */ 213 return (EINVAL); 214 default: 215 /* 216 * BMCR data is stored in the ifmedia entry. 217 */ 218 PHY_WRITE(sc, MII_ANAR, mii_anar(ife->ifm_media)); 219 PHY_WRITE(sc, MII_BMCR, ife->ifm_data); 220 } 221 break; 222 223 case MII_TICK: 224 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 225 return (0); 226 227 /* 228 * Is the interface even up? 229 */ 230 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 231 return (0); 232 233 /* 234 * Only used for autonegotiation. 235 */ 236 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) 237 return (0); 238 239 /* 240 * check for link. 241 * Read the status register twice; BMSR_LINK is latch-low. 242 */ 243 reg = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR); 244 if (reg & BMSR_LINK) 245 return (0); 246 247 /* 248 * Only retry autonegotiation every 5 seconds. 249 */ 250 if (++sc->mii_ticks != 5) 251 return (0); 252 253 sc->mii_ticks = 0; 254 mii_phy_reset(sc); 255 if (mii_phy_auto(sc, 0) == EJUSTRETURN) 256 return (0); 257 break; 258 } 259 260 /* Update the media status. */ 261 inphy_status(sc); 262 263 /* Callback if something changed. */ 264 if (sc->mii_active != mii->mii_media_active || cmd == MII_MEDIACHG) { 265 MIIBUS_STATCHG(sc->mii_dev); 266 sc->mii_active = mii->mii_media_active; 267 } 268 return (0); 269 } 270 271 void 272 inphy_status(struct mii_softc *sc) 273 { 274 struct mii_data *mii = sc->mii_pdata; 275 int bmsr, bmcr, scr; 276 277 mii->mii_media_status = IFM_AVALID; 278 mii->mii_media_active = IFM_ETHER; 279 280 bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR); 281 if (bmsr & BMSR_LINK) 282 mii->mii_media_status |= IFM_ACTIVE; 283 284 bmcr = PHY_READ(sc, MII_BMCR); 285 if (bmcr & BMCR_ISO) { 286 mii->mii_media_active |= IFM_NONE; 287 mii->mii_media_status = 0; 288 return; 289 } 290 291 if (bmcr & BMCR_LOOP) 292 mii->mii_media_active |= IFM_LOOP; 293 294 if (bmcr & BMCR_AUTOEN) { 295 if ((bmsr & BMSR_ACOMP) == 0) { 296 mii->mii_media_active |= IFM_NONE; 297 return; 298 } 299 300 scr = PHY_READ(sc, MII_INPHY_SCR); 301 if (scr & SCR_S100) 302 mii->mii_media_active |= IFM_100_TX; 303 else 304 mii->mii_media_active |= IFM_10_T; 305 if (scr & SCR_FDX) 306 mii->mii_media_active |= IFM_FDX; 307 } else 308 mii->mii_media_active |= mii_media_from_bmcr(bmcr); 309 } 310