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 SMSC LAN8710A 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 smscphy_probe(device_t); 53 static int smscphy_attach(device_t); 54 55 static int smscphy_service(struct mii_softc *, struct mii_data *, int); 56 static void smscphy_auto(struct mii_softc *, int); 57 static void smscphy_status(struct mii_softc *); 58 59 static device_method_t smscphy_methods[] = { 60 /* device interface */ 61 DEVMETHOD(device_probe, smscphy_probe), 62 DEVMETHOD(device_attach, smscphy_attach), 63 DEVMETHOD(device_detach, mii_phy_detach), 64 DEVMETHOD(device_shutdown, bus_generic_shutdown), 65 DEVMETHOD_END 66 }; 67 68 static devclass_t smscphy_devclass; 69 70 static driver_t smscphy_driver = { 71 "smscphy", 72 smscphy_methods, 73 sizeof(struct mii_softc) 74 }; 75 76 DRIVER_MODULE(smscphy, miibus, smscphy_driver, smscphy_devclass, 0, 0); 77 78 static const struct mii_phydesc smscphys[] = { 79 MII_PHY_DESC(SMC, LAN8710A), 80 MII_PHY_END 81 }; 82 83 static const struct mii_phy_funcs smscphy_funcs = { 84 smscphy_service, 85 smscphy_status, 86 mii_phy_reset 87 }; 88 89 static int 90 smscphy_probe(device_t dev) 91 { 92 93 return (mii_phy_dev_probe(dev, smscphys, BUS_PROBE_DEFAULT)); 94 } 95 96 static int 97 smscphy_attach(device_t dev) 98 { 99 struct mii_softc *sc; 100 struct mii_attach_args *ma; 101 const struct mii_phy_funcs *mpf; 102 103 sc = device_get_softc(dev); 104 ma = device_get_ivars(dev); 105 mpf = &smscphy_funcs; 106 mii_phy_dev_attach(dev, MIIF_NOISOLATE | MIIF_NOMANPAUSE, mpf, 1); 107 mii_phy_setmedia(sc); 108 109 return (0); 110 } 111 112 static int 113 smscphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd) 114 { 115 struct ifmedia_entry *ife; 116 int reg; 117 118 ife = mii->mii_media.ifm_cur; 119 120 switch (cmd) { 121 case MII_POLLSTAT: 122 break; 123 124 case MII_MEDIACHG: 125 /* 126 * If the interface is not up, don't do anything. 127 */ 128 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 129 break; 130 131 switch (IFM_SUBTYPE(ife->ifm_media)) { 132 case IFM_AUTO: 133 smscphy_auto(sc, ife->ifm_media); 134 break; 135 136 default: 137 mii_phy_setmedia(sc); 138 break; 139 } 140 141 break; 142 143 case MII_TICK: 144 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) { 145 return (0); 146 } 147 148 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) { 149 break; 150 } 151 152 /* I have no idea why BMCR_ISO gets set. */ 153 reg = PHY_READ(sc, MII_BMCR); 154 if (reg & BMCR_ISO) { 155 PHY_WRITE(sc, MII_BMCR, reg & ~BMCR_ISO); 156 } 157 158 reg = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR); 159 if (reg & BMSR_LINK) { 160 sc->mii_ticks = 0; 161 break; 162 } 163 164 if (++sc->mii_ticks <= MII_ANEGTICKS) { 165 break; 166 } 167 168 sc->mii_ticks = 0; 169 PHY_RESET(sc); 170 smscphy_auto(sc, ife->ifm_media); 171 break; 172 } 173 174 /* Update the media status. */ 175 PHY_STATUS(sc); 176 177 /* Callback if something changed. */ 178 mii_phy_update(sc, cmd); 179 return (0); 180 } 181 182 static void 183 smscphy_auto(struct mii_softc *sc, int media) 184 { 185 uint16_t anar; 186 187 anar = BMSR_MEDIA_TO_ANAR(sc->mii_capabilities) | ANAR_CSMA; 188 if ((media & IFM_FLOW) != 0 || (sc->mii_flags & MIIF_FORCEPAUSE) != 0) 189 anar |= ANAR_FC; 190 PHY_WRITE(sc, MII_ANAR, anar); 191 /* Apparently this helps. */ 192 anar = PHY_READ(sc, MII_ANAR); 193 PHY_WRITE(sc, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG); 194 } 195 196 static void 197 smscphy_status(struct mii_softc *sc) 198 { 199 struct mii_data *mii; 200 uint32_t bmcr, bmsr, status; 201 202 mii = sc->mii_pdata; 203 mii->mii_media_status = IFM_AVALID; 204 mii->mii_media_active = IFM_ETHER; 205 206 bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR); 207 if ((bmsr & BMSR_LINK) != 0) 208 mii->mii_media_status |= IFM_ACTIVE; 209 210 bmcr = PHY_READ(sc, MII_BMCR); 211 if ((bmcr & BMCR_ISO) != 0) { 212 mii->mii_media_active |= IFM_NONE; 213 mii->mii_media_status = 0; 214 return; 215 } 216 217 if ((bmcr & BMCR_LOOP) != 0) 218 mii->mii_media_active |= IFM_LOOP; 219 220 if ((bmcr & BMCR_AUTOEN) != 0) { 221 if ((bmsr & BMSR_ACOMP) == 0) { 222 /* Erg, still trying, I guess... */ 223 mii->mii_media_active |= IFM_NONE; 224 return; 225 } 226 } 227 228 status = PHY_READ(sc, 0x1F); 229 if (status & 0x0008) 230 mii->mii_media_active |= IFM_100_TX; 231 else 232 mii->mii_media_active |= IFM_10_T; 233 if (status & 0x0010) 234 mii->mii_media_active |= IFM_FDX | mii_phy_flowstatus(sc); 235 else 236 mii->mii_media_active |= IFM_HDX; 237 } 238