1 /*- 2 * Copyright (c) 2006 Benno Rice. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 */ 24 25 #include <sys/cdefs.h> 26 __FBSDID("$FreeBSD$"); 27 28 /* 29 * Driver for the internal PHY on the SMSC LAN91C111. 30 */ 31 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/kernel.h> 35 #include <sys/socket.h> 36 #include <sys/errno.h> 37 #include <sys/module.h> 38 #include <sys/bus.h> 39 #include <sys/malloc.h> 40 41 #include <machine/bus.h> 42 43 #include <net/if.h> 44 #include <net/if_media.h> 45 46 #include <dev/mii/mii.h> 47 #include <dev/mii/miivar.h> 48 #include "miidevs.h" 49 50 #include "miibus_if.h" 51 52 static int smcphy_probe(device_t); 53 static int smcphy_attach(device_t); 54 55 static int smcphy_service(struct mii_softc *, struct mii_data *, int); 56 static int smcphy_reset(struct mii_softc *); 57 static void smcphy_auto(struct mii_softc *); 58 59 static device_method_t smcphy_methods[] = { 60 /* device interface */ 61 DEVMETHOD(device_probe, smcphy_probe), 62 DEVMETHOD(device_attach, smcphy_attach), 63 DEVMETHOD(device_detach, mii_phy_detach), 64 DEVMETHOD(device_shutdown, bus_generic_shutdown), 65 66 { 0, 0 } 67 }; 68 69 static devclass_t smcphy_devclass; 70 71 static driver_t smcphy_driver = { 72 "smcphy", 73 smcphy_methods, 74 sizeof(struct mii_softc) 75 }; 76 77 DRIVER_MODULE(smcphy, miibus, smcphy_driver, smcphy_devclass, 0, 0); 78 79 static int 80 smcphy_probe(device_t dev) 81 { 82 struct mii_attach_args *ma; 83 84 ma = device_get_ivars(dev); 85 86 if (MII_OUI(ma->mii_id1, ma->mii_id2) != MII_OUI_SMSC || 87 MII_MODEL(ma->mii_id2) != MII_MODEL_SMSC_LAN83C183) 88 return (ENXIO); 89 90 device_set_desc(dev, MII_STR_SMSC_LAN83C183); 91 92 return (0); 93 } 94 95 static int 96 smcphy_attach(device_t dev) 97 { 98 struct mii_softc *sc; 99 struct mii_attach_args *ma; 100 struct mii_data *mii; 101 102 sc = device_get_softc(dev); 103 ma = device_get_ivars(dev); 104 sc->mii_dev = device_get_parent(dev); 105 mii = device_get_softc(sc->mii_dev); 106 LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list); 107 108 sc->mii_inst = mii->mii_instance; 109 sc->mii_phy = ma->mii_phyno; 110 sc->mii_service = smcphy_service; 111 sc->mii_pdata = mii; 112 113 mii->mii_instance++; 114 115 sc->mii_flags |= MIIF_NOISOLATE; 116 117 if (smcphy_reset(sc) != 0) { 118 device_printf(dev, "reset failed\n"); 119 } 120 121 /* Mask interrupts, we poll instead. */ 122 PHY_WRITE(sc, 0x1e, 0xffc0); 123 124 sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & ma->mii_capmask; 125 device_printf(dev, " "); 126 mii_add_media(sc); 127 printf("\n"); 128 129 MIIBUS_MEDIAINIT(sc->mii_dev); 130 mii_phy_setmedia(sc); 131 132 return (0); 133 } 134 135 static int 136 smcphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd) 137 { 138 struct ifmedia_entry *ife; 139 int reg; 140 141 ife = mii->mii_media.ifm_cur; 142 143 switch (cmd) { 144 case MII_POLLSTAT: 145 /* 146 * If we're not polling our PHY instance, just return. 147 */ 148 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 149 return (0); 150 break; 151 152 case MII_MEDIACHG: 153 /* 154 * If the media indicates a different PHY instance, 155 * isolate ourselves. 156 */ 157 if (IFM_INST(ife->ifm_media) != sc->mii_inst) { 158 reg = PHY_READ(sc, MII_BMCR); 159 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO); 160 return (0); 161 } 162 163 /* 164 * If the interface is not up, don't do anything. 165 */ 166 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 167 break; 168 169 switch (IFM_SUBTYPE(ife->ifm_media)) { 170 case IFM_AUTO: 171 smcphy_auto(sc); 172 break; 173 174 default: 175 mii_phy_setmedia(sc); 176 break; 177 } 178 179 break; 180 181 case MII_TICK: 182 /* 183 * If we're not currently selected, just return. 184 */ 185 if (IFM_INST(ife->ifm_media) != sc->mii_inst) { 186 return (0); 187 } 188 189 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) { 190 return (0); 191 } 192 193 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) { 194 break; 195 } 196 197 /* I have no idea why BMCR_ISO gets set. */ 198 reg = PHY_READ(sc, MII_BMCR); 199 if (reg & BMCR_ISO) { 200 PHY_WRITE(sc, MII_BMCR, reg & ~BMCR_ISO); 201 } 202 203 reg = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR); 204 if (reg & BMSR_LINK) { 205 sc->mii_ticks = 0; 206 break; 207 } 208 209 if (++sc->mii_ticks <= MII_ANEGTICKS) { 210 break; 211 } 212 213 sc->mii_ticks = 0; 214 if (smcphy_reset(sc) != 0) { 215 device_printf(sc->mii_dev, "reset failed\n"); 216 } 217 smcphy_auto(sc); 218 break; 219 } 220 221 /* Update the media status. */ 222 ukphy_status(sc); 223 224 /* Callback if something changed. */ 225 mii_phy_update(sc, cmd); 226 return (0); 227 } 228 229 static int 230 smcphy_reset(struct mii_softc *sc) 231 { 232 u_int bmcr; 233 int timeout; 234 235 PHY_WRITE(sc, MII_BMCR, BMCR_RESET); 236 237 for (timeout = 2; timeout > 0; timeout--) { 238 DELAY(50000); 239 bmcr = PHY_READ(sc, MII_BMCR); 240 if ((bmcr & BMCR_RESET) == 0) 241 break; 242 } 243 244 if (bmcr & BMCR_RESET) { 245 return (EIO); 246 } 247 248 PHY_WRITE(sc, MII_BMCR, 0x3000); 249 return (0); 250 } 251 252 static void 253 smcphy_auto(struct mii_softc *sc) 254 { 255 uint16_t anar; 256 257 anar = BMSR_MEDIA_TO_ANAR(sc->mii_capabilities) | 258 ANAR_CSMA; 259 if (sc->mii_flags & MIIF_DOPAUSE) 260 anar |= ANAR_FC; 261 PHY_WRITE(sc, MII_ANAR, anar); 262 /* Apparently this helps. */ 263 anar = PHY_READ(sc, MII_ANAR); 264 PHY_WRITE(sc, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG); 265 } 266