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.14 2004/05/30 17:57:40 phk Exp $ 30 */ 31 32 /* 33 * driver for Intel 82553 and 82555 PHYs 34 */ 35 36 #include <sys/param.h> 37 #include <sys/systm.h> 38 #include <sys/kernel.h> 39 #include <sys/socket.h> 40 #include <sys/bus.h> 41 42 #include <net/if.h> 43 #include <net/if_var.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_service(struct mii_softc *, struct mii_data *, int); 57 static void inphy_status(struct mii_softc *); 58 59 static device_method_t inphy_methods[] = { 60 /* device interface */ 61 DEVMETHOD(device_probe, inphy_probe), 62 DEVMETHOD(device_attach, inphy_attach), 63 DEVMETHOD(device_detach, ukphy_detach), 64 DEVMETHOD(device_shutdown, bus_generic_shutdown), 65 DEVMETHOD_END 66 }; 67 68 static const struct mii_phydesc inphys[] = { 69 MII_PHYDESC(xxINTEL, I82553AB), /* Intel 82553 A/B steppings */ 70 MII_PHYDESC(INTEL, I82555), 71 MII_PHYDESC(INTEL, I82553C), 72 MII_PHYDESC(INTEL, I82562EM), 73 MII_PHYDESC(INTEL, I82562ET), 74 MII_PHYDESC_NULL 75 }; 76 77 static devclass_t inphy_devclass; 78 79 static driver_t inphy_driver = { 80 "inphy", 81 inphy_methods, 82 sizeof(struct mii_softc) 83 }; 84 85 DRIVER_MODULE(inphy, miibus, inphy_driver, inphy_devclass, NULL, NULL); 86 87 static int 88 inphy_probe(device_t dev) 89 { 90 struct mii_attach_args *ma = device_get_ivars(dev); 91 const struct mii_phydesc *mpd; 92 93 mpd = mii_phy_match(ma, inphys); 94 if (mpd != NULL) { 95 device_set_desc(dev, mpd->mpd_name); 96 return (0); 97 } 98 return (ENXIO); 99 } 100 101 static int 102 inphy_attach(device_t dev) 103 { 104 struct mii_softc *sc; 105 struct mii_attach_args *ma; 106 struct mii_data *mii; 107 108 sc = device_get_softc(dev); 109 ma = device_get_ivars(dev); 110 mii_softc_init(sc, ma); 111 sc->mii_dev = device_get_parent(dev); 112 mii = device_get_softc(sc->mii_dev); 113 LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list); 114 115 sc->mii_inst = mii->mii_instance; 116 sc->mii_service = inphy_service; 117 sc->mii_pdata = mii; 118 mii->mii_instance++; 119 120 #if 0 121 sc->mii_flags |= MIIF_NOISOLATE; 122 #endif 123 124 ifmedia_add(&mii->mii_media, 125 IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->mii_inst), 126 MII_MEDIA_100_TX, NULL); 127 128 mii_phy_reset(sc); 129 130 sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & ma->mii_capmask; 131 132 device_printf(dev, " "); 133 if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0) 134 kprintf("no media present"); 135 else 136 mii_phy_add_media(sc); 137 kprintf("\n"); 138 139 MIIBUS_MEDIAINIT(sc->mii_dev); 140 return (0); 141 } 142 143 static int 144 inphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd) 145 { 146 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 147 int reg; 148 149 switch (cmd) { 150 case MII_POLLSTAT: 151 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 152 return (0); 153 break; 154 155 case MII_MEDIACHG: 156 if (IFM_INST(ife->ifm_media) != sc->mii_inst) { 157 reg = PHY_READ(sc, MII_BMCR); 158 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO); 159 return (0); 160 } 161 162 /* 163 * If the interface is not up, don't do anything. 164 */ 165 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 166 break; 167 168 mii_phy_set_media(sc); 169 break; 170 171 case MII_TICK: 172 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 173 return (0); 174 175 if (mii_phy_tick(sc) == EJUSTRETURN) 176 return (0); 177 break; 178 } 179 180 /* Update the media status. */ 181 inphy_status(sc); 182 183 /* Callback if something changed. */ 184 mii_phy_update(sc, cmd); 185 return (0); 186 } 187 188 static void 189 inphy_status(struct mii_softc *sc) 190 { 191 struct mii_data *mii = sc->mii_pdata; 192 int bmsr, bmcr, scr; 193 194 mii->mii_media_status = IFM_AVALID; 195 mii->mii_media_active = IFM_ETHER; 196 197 bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR); 198 if (bmsr & BMSR_LINK) 199 mii->mii_media_status |= IFM_ACTIVE; 200 201 bmcr = PHY_READ(sc, MII_BMCR); 202 if (bmcr & BMCR_ISO) { 203 mii->mii_media_active |= IFM_NONE; 204 mii->mii_media_status = 0; 205 return; 206 } 207 208 if (bmcr & BMCR_LOOP) 209 mii->mii_media_active |= IFM_LOOP; 210 211 if (bmcr & BMCR_AUTOEN) { 212 if ((bmsr & BMSR_ACOMP) == 0) { 213 mii->mii_media_active |= IFM_NONE; 214 return; 215 } 216 217 scr = PHY_READ(sc, MII_INPHY_SCR); 218 if ((bmsr & BMSR_100T4) && (scr & SCR_T4)) 219 mii->mii_media_active |= IFM_100_T4; 220 else if (scr & SCR_S100) 221 mii->mii_media_active |= IFM_100_TX; 222 else 223 mii->mii_media_active |= IFM_10_T; 224 if (scr & SCR_FDX) 225 mii->mii_media_active |= IFM_FDX; 226 } else { 227 mii->mii_media_active = mii->mii_media.ifm_cur->ifm_media; 228 } 229 } 230