1694c6518SBenno Rice /*- 2694c6518SBenno Rice * Copyright (c) 2006 Benno Rice. All rights reserved. 3694c6518SBenno Rice * 4694c6518SBenno Rice * Redistribution and use in source and binary forms, with or without 5694c6518SBenno Rice * modification, are permitted provided that the following conditions 6694c6518SBenno Rice * are met: 7694c6518SBenno Rice * 1. Redistributions of source code must retain the above copyright 8694c6518SBenno Rice * notice, this list of conditions and the following disclaimer. 9694c6518SBenno Rice * 2. Redistributions in binary form must reproduce the above copyright 10694c6518SBenno Rice * notice, this list of conditions and the following disclaimer in the 11694c6518SBenno Rice * documentation and/or other materials provided with the distribution. 12694c6518SBenno Rice * 13694c6518SBenno Rice * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 14694c6518SBenno Rice * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 15694c6518SBenno Rice * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 16694c6518SBenno Rice * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 17694c6518SBenno Rice * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 18694c6518SBenno Rice * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 19694c6518SBenno Rice * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 20694c6518SBenno Rice * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 21694c6518SBenno Rice * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 22694c6518SBenno Rice * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23694c6518SBenno Rice */ 24694c6518SBenno Rice 25694c6518SBenno Rice #include <sys/cdefs.h> 26694c6518SBenno Rice __FBSDID("$FreeBSD$"); 27694c6518SBenno Rice 28694c6518SBenno Rice /* 29*b011f8c4SOleksandr Tymoshenko * Driver for the SEEQ 80220 and 84220. 30*b011f8c4SOleksandr Tymoshenko * (Originally developed for the internal PHY on the SMSC LAN91C111.) 31694c6518SBenno Rice */ 32694c6518SBenno Rice 33694c6518SBenno Rice #include <sys/param.h> 34694c6518SBenno Rice #include <sys/systm.h> 35694c6518SBenno Rice #include <sys/kernel.h> 36694c6518SBenno Rice #include <sys/socket.h> 37694c6518SBenno Rice #include <sys/errno.h> 38694c6518SBenno Rice #include <sys/module.h> 39694c6518SBenno Rice #include <sys/bus.h> 40694c6518SBenno Rice #include <sys/malloc.h> 41694c6518SBenno Rice 42694c6518SBenno Rice #include <machine/bus.h> 43694c6518SBenno Rice 44694c6518SBenno Rice #include <net/if.h> 45694c6518SBenno Rice #include <net/if_media.h> 46694c6518SBenno Rice 47694c6518SBenno Rice #include <dev/mii/mii.h> 48694c6518SBenno Rice #include <dev/mii/miivar.h> 49694c6518SBenno Rice #include "miidevs.h" 50694c6518SBenno Rice 51694c6518SBenno Rice #include "miibus_if.h" 52694c6518SBenno Rice 53694c6518SBenno Rice static int smcphy_probe(device_t); 54694c6518SBenno Rice static int smcphy_attach(device_t); 55694c6518SBenno Rice 56694c6518SBenno Rice static int smcphy_service(struct mii_softc *, struct mii_data *, int); 573fcb7a53SMarius Strobl static void smcphy_reset(struct mii_softc *); 58efd4fc3fSMarius Strobl static void smcphy_auto(struct mii_softc *, int); 5934fe3828SPyun YongHyeon static void smcphy_status(struct mii_softc *); 60694c6518SBenno Rice 61694c6518SBenno Rice static device_method_t smcphy_methods[] = { 62694c6518SBenno Rice /* device interface */ 63694c6518SBenno Rice DEVMETHOD(device_probe, smcphy_probe), 64694c6518SBenno Rice DEVMETHOD(device_attach, smcphy_attach), 65694c6518SBenno Rice DEVMETHOD(device_detach, mii_phy_detach), 66694c6518SBenno Rice DEVMETHOD(device_shutdown, bus_generic_shutdown), 67604f5f1fSMarius Strobl DEVMETHOD_END 68694c6518SBenno Rice }; 69694c6518SBenno Rice 70694c6518SBenno Rice static devclass_t smcphy_devclass; 71694c6518SBenno Rice 72694c6518SBenno Rice static driver_t smcphy_driver = { 73694c6518SBenno Rice "smcphy", 74694c6518SBenno Rice smcphy_methods, 75694c6518SBenno Rice sizeof(struct mii_softc) 76694c6518SBenno Rice }; 77694c6518SBenno Rice 78694c6518SBenno Rice DRIVER_MODULE(smcphy, miibus, smcphy_driver, smcphy_devclass, 0, 0); 79694c6518SBenno Rice 80f6613debSMarius Strobl static const struct mii_phydesc smcphys[] = { 8134fe3828SPyun YongHyeon MII_PHY_DESC(SEEQ, 80220), 823fcb7a53SMarius Strobl MII_PHY_DESC(SEEQ, 84220), 83f6613debSMarius Strobl MII_PHY_END 84f6613debSMarius Strobl }; 85f6613debSMarius Strobl 8634fe3828SPyun YongHyeon static const struct mii_phy_funcs smcphy80220_funcs = { 8734fe3828SPyun YongHyeon smcphy_service, 8834fe3828SPyun YongHyeon smcphy_status, 8934fe3828SPyun YongHyeon mii_phy_reset 9034fe3828SPyun YongHyeon }; 9134fe3828SPyun YongHyeon 923fcb7a53SMarius Strobl static const struct mii_phy_funcs smcphy_funcs = { 933fcb7a53SMarius Strobl smcphy_service, 9434fe3828SPyun YongHyeon smcphy_status, 953fcb7a53SMarius Strobl smcphy_reset 963fcb7a53SMarius Strobl }; 973fcb7a53SMarius Strobl 98694c6518SBenno Rice static int 99694c6518SBenno Rice smcphy_probe(device_t dev) 100694c6518SBenno Rice { 101694c6518SBenno Rice 102f6613debSMarius Strobl return (mii_phy_dev_probe(dev, smcphys, BUS_PROBE_DEFAULT)); 103694c6518SBenno Rice } 104694c6518SBenno Rice 105694c6518SBenno Rice static int 106694c6518SBenno Rice smcphy_attach(device_t dev) 107694c6518SBenno Rice { 108694c6518SBenno Rice struct mii_softc *sc; 10934fe3828SPyun YongHyeon struct mii_attach_args *ma; 11034fe3828SPyun YongHyeon const struct mii_phy_funcs *mpf; 111694c6518SBenno Rice 112694c6518SBenno Rice sc = device_get_softc(dev); 11334fe3828SPyun YongHyeon ma = device_get_ivars(dev); 11434fe3828SPyun YongHyeon if (MII_MODEL(ma->mii_id2) == MII_MODEL_SEEQ_80220) 11534fe3828SPyun YongHyeon mpf = &smcphy80220_funcs; 11634fe3828SPyun YongHyeon else 11734fe3828SPyun YongHyeon mpf = &smcphy_funcs; 11834fe3828SPyun YongHyeon mii_phy_dev_attach(dev, MIIF_NOISOLATE | MIIF_NOMANPAUSE, mpf, 1); 119694c6518SBenno Rice mii_phy_setmedia(sc); 120694c6518SBenno Rice 121694c6518SBenno Rice return (0); 122694c6518SBenno Rice } 123694c6518SBenno Rice 124694c6518SBenno Rice static int 125694c6518SBenno Rice smcphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd) 126694c6518SBenno Rice { 127694c6518SBenno Rice struct ifmedia_entry *ife; 128694c6518SBenno Rice int reg; 129694c6518SBenno Rice 130694c6518SBenno Rice ife = mii->mii_media.ifm_cur; 131694c6518SBenno Rice 132694c6518SBenno Rice switch (cmd) { 133694c6518SBenno Rice case MII_POLLSTAT: 134694c6518SBenno Rice break; 135694c6518SBenno Rice 136694c6518SBenno Rice case MII_MEDIACHG: 137694c6518SBenno Rice /* 138694c6518SBenno Rice * If the interface is not up, don't do anything. 139694c6518SBenno Rice */ 140694c6518SBenno Rice if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 141694c6518SBenno Rice break; 142694c6518SBenno Rice 143694c6518SBenno Rice switch (IFM_SUBTYPE(ife->ifm_media)) { 144694c6518SBenno Rice case IFM_AUTO: 145efd4fc3fSMarius Strobl smcphy_auto(sc, ife->ifm_media); 146694c6518SBenno Rice break; 147694c6518SBenno Rice 148694c6518SBenno Rice default: 149694c6518SBenno Rice mii_phy_setmedia(sc); 150694c6518SBenno Rice break; 151694c6518SBenno Rice } 152694c6518SBenno Rice 153694c6518SBenno Rice break; 154694c6518SBenno Rice 155694c6518SBenno Rice case MII_TICK: 156694c6518SBenno Rice if ((mii->mii_ifp->if_flags & IFF_UP) == 0) { 157694c6518SBenno Rice return (0); 158694c6518SBenno Rice } 159694c6518SBenno Rice 160694c6518SBenno Rice if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) { 161694c6518SBenno Rice break; 162694c6518SBenno Rice } 163694c6518SBenno Rice 164694c6518SBenno Rice /* I have no idea why BMCR_ISO gets set. */ 165694c6518SBenno Rice reg = PHY_READ(sc, MII_BMCR); 166694c6518SBenno Rice if (reg & BMCR_ISO) { 167694c6518SBenno Rice PHY_WRITE(sc, MII_BMCR, reg & ~BMCR_ISO); 168694c6518SBenno Rice } 169694c6518SBenno Rice 170694c6518SBenno Rice reg = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR); 171694c6518SBenno Rice if (reg & BMSR_LINK) { 172694c6518SBenno Rice sc->mii_ticks = 0; 173694c6518SBenno Rice break; 174694c6518SBenno Rice } 175694c6518SBenno Rice 176694c6518SBenno Rice if (++sc->mii_ticks <= MII_ANEGTICKS) { 177694c6518SBenno Rice break; 178694c6518SBenno Rice } 179694c6518SBenno Rice 180694c6518SBenno Rice sc->mii_ticks = 0; 1813fcb7a53SMarius Strobl PHY_RESET(sc); 182efd4fc3fSMarius Strobl smcphy_auto(sc, ife->ifm_media); 183694c6518SBenno Rice break; 184694c6518SBenno Rice } 185694c6518SBenno Rice 186694c6518SBenno Rice /* Update the media status. */ 1873fcb7a53SMarius Strobl PHY_STATUS(sc); 188694c6518SBenno Rice 189694c6518SBenno Rice /* Callback if something changed. */ 190694c6518SBenno Rice mii_phy_update(sc, cmd); 191694c6518SBenno Rice return (0); 192694c6518SBenno Rice } 193694c6518SBenno Rice 1943fcb7a53SMarius Strobl static void 195694c6518SBenno Rice smcphy_reset(struct mii_softc *sc) 196694c6518SBenno Rice { 197694c6518SBenno Rice u_int bmcr; 198694c6518SBenno Rice int timeout; 199694c6518SBenno Rice 200694c6518SBenno Rice PHY_WRITE(sc, MII_BMCR, BMCR_RESET); 201694c6518SBenno Rice 202694c6518SBenno Rice for (timeout = 2; timeout > 0; timeout--) { 203694c6518SBenno Rice DELAY(50000); 204694c6518SBenno Rice bmcr = PHY_READ(sc, MII_BMCR); 205694c6518SBenno Rice if ((bmcr & BMCR_RESET) == 0) 206694c6518SBenno Rice break; 207694c6518SBenno Rice } 208694c6518SBenno Rice 2093fcb7a53SMarius Strobl if (bmcr & BMCR_RESET) 2103fcb7a53SMarius Strobl device_printf(sc->mii_dev, "reset failed\n"); 211694c6518SBenno Rice 212694c6518SBenno Rice PHY_WRITE(sc, MII_BMCR, 0x3000); 2133fcb7a53SMarius Strobl 2143fcb7a53SMarius Strobl /* Mask interrupts, we poll instead. */ 2153fcb7a53SMarius Strobl PHY_WRITE(sc, 0x1e, 0xffc0); 216694c6518SBenno Rice } 217694c6518SBenno Rice 218694c6518SBenno Rice static void 219efd4fc3fSMarius Strobl smcphy_auto(struct mii_softc *sc, int media) 220694c6518SBenno Rice { 221694c6518SBenno Rice uint16_t anar; 222694c6518SBenno Rice 223604f5f1fSMarius Strobl anar = BMSR_MEDIA_TO_ANAR(sc->mii_capabilities) | ANAR_CSMA; 224efd4fc3fSMarius Strobl if ((media & IFM_FLOW) != 0 || (sc->mii_flags & MIIF_FORCEPAUSE) != 0) 225694c6518SBenno Rice anar |= ANAR_FC; 226694c6518SBenno Rice PHY_WRITE(sc, MII_ANAR, anar); 227694c6518SBenno Rice /* Apparently this helps. */ 228694c6518SBenno Rice anar = PHY_READ(sc, MII_ANAR); 229694c6518SBenno Rice PHY_WRITE(sc, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG); 230694c6518SBenno Rice } 23134fe3828SPyun YongHyeon 23234fe3828SPyun YongHyeon static void 23334fe3828SPyun YongHyeon smcphy_status(struct mii_softc *sc) 23434fe3828SPyun YongHyeon { 23534fe3828SPyun YongHyeon struct mii_data *mii; 23634fe3828SPyun YongHyeon uint32_t bmcr, bmsr, status; 23734fe3828SPyun YongHyeon 23834fe3828SPyun YongHyeon mii = sc->mii_pdata; 23934fe3828SPyun YongHyeon mii->mii_media_status = IFM_AVALID; 24034fe3828SPyun YongHyeon mii->mii_media_active = IFM_ETHER; 24134fe3828SPyun YongHyeon 24234fe3828SPyun YongHyeon bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR); 24334fe3828SPyun YongHyeon if ((bmsr & BMSR_LINK) != 0) 24434fe3828SPyun YongHyeon mii->mii_media_status |= IFM_ACTIVE; 24534fe3828SPyun YongHyeon 24634fe3828SPyun YongHyeon bmcr = PHY_READ(sc, MII_BMCR); 24734fe3828SPyun YongHyeon if ((bmcr & BMCR_ISO) != 0) { 24834fe3828SPyun YongHyeon mii->mii_media_active |= IFM_NONE; 24934fe3828SPyun YongHyeon mii->mii_media_status = 0; 25034fe3828SPyun YongHyeon return; 25134fe3828SPyun YongHyeon } 25234fe3828SPyun YongHyeon 25334fe3828SPyun YongHyeon if ((bmcr & BMCR_LOOP) != 0) 25434fe3828SPyun YongHyeon mii->mii_media_active |= IFM_LOOP; 25534fe3828SPyun YongHyeon 25634fe3828SPyun YongHyeon if ((bmcr & BMCR_AUTOEN) != 0) { 25734fe3828SPyun YongHyeon if ((bmsr & BMSR_ACOMP) == 0) { 25834fe3828SPyun YongHyeon /* Erg, still trying, I guess... */ 25934fe3828SPyun YongHyeon mii->mii_media_active |= IFM_NONE; 26034fe3828SPyun YongHyeon return; 26134fe3828SPyun YongHyeon } 26234fe3828SPyun YongHyeon } 26334fe3828SPyun YongHyeon 26434fe3828SPyun YongHyeon status = PHY_READ(sc, 0x12); 26534fe3828SPyun YongHyeon if (status & 0x0080) 26634fe3828SPyun YongHyeon mii->mii_media_active |= IFM_100_TX; 26734fe3828SPyun YongHyeon else 26834fe3828SPyun YongHyeon mii->mii_media_active |= IFM_10_T; 26934fe3828SPyun YongHyeon if (status & 0x0040) 27034fe3828SPyun YongHyeon mii->mii_media_active |= IFM_FDX | mii_phy_flowstatus(sc); 27134fe3828SPyun YongHyeon else 27234fe3828SPyun YongHyeon mii->mii_media_active |= IFM_HDX; 27334fe3828SPyun YongHyeon } 274