1 1.4 andvar /* $NetBSD: smscphy.c,v 1.4 2021/08/25 21:50:29 andvar Exp $ */ 2 1.4 andvar 3 1.1 msaitoh /*- 4 1.1 msaitoh * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 5 1.1 msaitoh * 6 1.1 msaitoh * Copyright (c) 2006 Benno Rice. All rights reserved. 7 1.1 msaitoh * 8 1.1 msaitoh * Redistribution and use in source and binary forms, with or without 9 1.1 msaitoh * modification, are permitted provided that the following conditions 10 1.1 msaitoh * are met: 11 1.1 msaitoh * 1. Redistributions of source code must retain the above copyright 12 1.1 msaitoh * notice, this list of conditions and the following disclaimer. 13 1.1 msaitoh * 2. Redistributions in binary form must reproduce the above copyright 14 1.1 msaitoh * notice, this list of conditions and the following disclaimer in the 15 1.1 msaitoh * documentation and/or other materials provided with the distribution. 16 1.1 msaitoh * 17 1.1 msaitoh * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 1.1 msaitoh * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 1.1 msaitoh * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 1.1 msaitoh * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 1.1 msaitoh * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 1.1 msaitoh * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 1.1 msaitoh * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 1.1 msaitoh * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 1.1 msaitoh * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 1.1 msaitoh * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 1.1 msaitoh */ 28 1.1 msaitoh 29 1.1 msaitoh /* $FreeBSD: head/sys/dev/mii/smscphy.c 326255 2017-11-27 14:52:40Z pfg $ */ 30 1.1 msaitoh 31 1.1 msaitoh /* 32 1.1 msaitoh * Driver for the SMSC LAN8710A 33 1.1 msaitoh */ 34 1.1 msaitoh 35 1.4 andvar #include <sys/cdefs.h> 36 1.4 andvar __KERNEL_RCSID(0, "$NetBSD: smscphy.c,v 1.4 2021/08/25 21:50:29 andvar Exp $"); 37 1.4 andvar 38 1.1 msaitoh #include <sys/param.h> 39 1.1 msaitoh #include <sys/systm.h> 40 1.1 msaitoh #include <sys/kernel.h> 41 1.1 msaitoh #include <sys/device.h> 42 1.1 msaitoh #include <sys/socket.h> 43 1.1 msaitoh #include <sys/errno.h> 44 1.1 msaitoh 45 1.1 msaitoh #include <net/if.h> 46 1.1 msaitoh #include <net/if_media.h> 47 1.1 msaitoh 48 1.1 msaitoh #include <dev/mii/mii.h> 49 1.1 msaitoh #include <dev/mii/miivar.h> 50 1.1 msaitoh #include <dev/mii/miidevs.h> 51 1.1 msaitoh 52 1.1 msaitoh /* PHY special control/status register */ 53 1.1 msaitoh #define SMSCPHY_SPCSR 0x1f 54 1.1 msaitoh #define SPCSR_SPDIND_10 0x0004 55 1.1 msaitoh #define SPCSR_SPDIND_100 0x0008 56 1.1 msaitoh #define SPCSR_SPDIND_SPDMASK 0x000c 57 1.1 msaitoh #define SPCSR_SPDIND_FDX 0x0010 58 1.1 msaitoh 59 1.1 msaitoh static int smscphy_match(device_t, cfdata_t, void *); 60 1.1 msaitoh static void smscphy_attach(device_t, device_t, void *); 61 1.1 msaitoh 62 1.1 msaitoh CFATTACH_DECL_NEW(smscphy, sizeof (struct mii_softc), 63 1.1 msaitoh smscphy_match, smscphy_attach, mii_phy_detach, mii_phy_activate); 64 1.1 msaitoh 65 1.1 msaitoh static void smscphy_power(struct mii_softc *, bool); 66 1.1 msaitoh static int smscphy_service(struct mii_softc *, struct mii_data *, int); 67 1.1 msaitoh static void smscphy_auto(struct mii_softc *, int); 68 1.1 msaitoh static void smscphy_status(struct mii_softc *); 69 1.1 msaitoh 70 1.1 msaitoh static const struct mii_phydesc smscphys[] = { 71 1.1 msaitoh MII_PHY_DESC(SMSC, LAN8700), 72 1.1 msaitoh MII_PHY_DESC(SMSC, LAN8710_LAN8720), 73 1.1 msaitoh MII_PHY_END 74 1.1 msaitoh }; 75 1.1 msaitoh 76 1.1 msaitoh static const struct mii_phy_funcs smscphy_funcs = { 77 1.1 msaitoh smscphy_service, 78 1.1 msaitoh smscphy_status, 79 1.1 msaitoh mii_phy_reset 80 1.1 msaitoh }; 81 1.1 msaitoh 82 1.1 msaitoh static int 83 1.1 msaitoh smscphy_match(device_t dev, cfdata_t match, void *aux) 84 1.1 msaitoh { 85 1.1 msaitoh struct mii_attach_args *ma = aux; 86 1.1 msaitoh 87 1.1 msaitoh if (mii_phy_match(ma, smscphys) != NULL) 88 1.1 msaitoh return 10; 89 1.1 msaitoh 90 1.1 msaitoh return 0; 91 1.1 msaitoh } 92 1.1 msaitoh 93 1.1 msaitoh static void 94 1.1 msaitoh smscphy_attach(device_t parent, device_t self, void *aux) 95 1.1 msaitoh { 96 1.1 msaitoh struct mii_softc *sc = device_private(self); 97 1.1 msaitoh struct mii_attach_args *ma = aux; 98 1.1 msaitoh struct mii_data *mii = ma->mii_data; 99 1.1 msaitoh const struct mii_phydesc *mpd; 100 1.1 msaitoh 101 1.1 msaitoh mpd = mii_phy_match(ma, smscphys); 102 1.1 msaitoh aprint_naive(": Media interface\n"); 103 1.1 msaitoh aprint_normal(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2)); 104 1.1 msaitoh 105 1.1 msaitoh sc->mii_dev = self; 106 1.1 msaitoh sc->mii_inst = mii->mii_instance; 107 1.1 msaitoh sc->mii_phy = ma->mii_phyno; 108 1.1 msaitoh sc->mii_funcs = &smscphy_funcs; 109 1.1 msaitoh sc->mii_mpd_oui = MII_OUI(ma->mii_id1, ma->mii_id2); 110 1.1 msaitoh sc->mii_mpd_model = MII_MODEL(ma->mii_id2); 111 1.1 msaitoh sc->mii_mpd_rev = MII_REV(ma->mii_id2); 112 1.1 msaitoh sc->mii_pdata = mii; 113 1.1 msaitoh sc->mii_flags = ma->mii_flags; 114 1.1 msaitoh 115 1.3 thorpej mii_lock(mii); 116 1.3 thorpej 117 1.1 msaitoh PHY_RESET(sc); 118 1.1 msaitoh 119 1.1 msaitoh PHY_READ(sc, MII_BMSR, &sc->mii_capabilities); 120 1.1 msaitoh sc->mii_capabilities &= ma->mii_capmask; 121 1.2 msaitoh 122 1.3 thorpej mii_unlock(mii); 123 1.3 thorpej 124 1.2 msaitoh mii_phy_add_media(sc); 125 1.1 msaitoh } 126 1.1 msaitoh 127 1.1 msaitoh static void 128 1.1 msaitoh smscphy_power(struct mii_softc *sc, bool power) 129 1.1 msaitoh { 130 1.1 msaitoh uint16_t bmcr, new; 131 1.1 msaitoh 132 1.1 msaitoh PHY_READ(sc, MII_BMCR, &bmcr); 133 1.1 msaitoh if (power) 134 1.1 msaitoh new = bmcr & ~BMCR_PDOWN; 135 1.1 msaitoh else 136 1.1 msaitoh new = bmcr | BMCR_PDOWN; 137 1.1 msaitoh if (bmcr != new) 138 1.1 msaitoh PHY_WRITE(sc, MII_BMCR, new); 139 1.1 msaitoh } 140 1.1 msaitoh 141 1.1 msaitoh static int 142 1.1 msaitoh smscphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd) 143 1.1 msaitoh { 144 1.1 msaitoh struct ifmedia_entry *ife; 145 1.1 msaitoh uint16_t reg; 146 1.1 msaitoh 147 1.3 thorpej KASSERT(mii_locked(mii)); 148 1.3 thorpej 149 1.1 msaitoh ife = mii->mii_media.ifm_cur; 150 1.1 msaitoh 151 1.1 msaitoh switch (cmd) { 152 1.1 msaitoh case MII_POLLSTAT: 153 1.1 msaitoh break; 154 1.1 msaitoh 155 1.1 msaitoh case MII_MEDIACHG: 156 1.1 msaitoh /* Try to power up the PHY in case it's down */ 157 1.1 msaitoh if (IFM_SUBTYPE(ife->ifm_media) != IFM_NONE) 158 1.1 msaitoh smscphy_power(sc, true); 159 1.1 msaitoh 160 1.1 msaitoh switch (IFM_SUBTYPE(ife->ifm_media)) { 161 1.1 msaitoh case IFM_AUTO: 162 1.1 msaitoh smscphy_auto(sc, ife->ifm_media); 163 1.1 msaitoh break; 164 1.1 msaitoh 165 1.1 msaitoh default: 166 1.1 msaitoh mii_phy_setmedia(sc); 167 1.1 msaitoh if (IFM_SUBTYPE(ife->ifm_media) == IFM_NONE) 168 1.1 msaitoh smscphy_power(sc, false); 169 1.1 msaitoh break; 170 1.1 msaitoh } 171 1.1 msaitoh break; 172 1.1 msaitoh 173 1.1 msaitoh case MII_TICK: 174 1.1 msaitoh if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) 175 1.1 msaitoh break; 176 1.1 msaitoh 177 1.1 msaitoh PHY_READ(sc, MII_BMSR, ®); 178 1.1 msaitoh PHY_READ(sc, MII_BMSR, ®); 179 1.1 msaitoh if (reg & BMSR_LINK) { 180 1.1 msaitoh sc->mii_ticks = 0; 181 1.1 msaitoh break; 182 1.1 msaitoh } 183 1.1 msaitoh 184 1.1 msaitoh if (++sc->mii_ticks <= MII_ANEGTICKS) 185 1.1 msaitoh break; 186 1.1 msaitoh 187 1.1 msaitoh PHY_RESET(sc); 188 1.1 msaitoh smscphy_auto(sc, ife->ifm_media); 189 1.1 msaitoh break; 190 1.1 msaitoh } 191 1.1 msaitoh 192 1.1 msaitoh /* Update the media status. */ 193 1.1 msaitoh PHY_STATUS(sc); 194 1.1 msaitoh 195 1.1 msaitoh /* Callback if something changed. */ 196 1.1 msaitoh mii_phy_update(sc, cmd); 197 1.1 msaitoh return 0; 198 1.1 msaitoh } 199 1.1 msaitoh 200 1.1 msaitoh static void 201 1.1 msaitoh smscphy_auto(struct mii_softc *sc, int media) 202 1.1 msaitoh { 203 1.1 msaitoh uint16_t anar; 204 1.1 msaitoh 205 1.1 msaitoh sc->mii_ticks = 0; 206 1.1 msaitoh anar = BMSR_MEDIA_TO_ANAR(sc->mii_capabilities) | ANAR_CSMA; 207 1.1 msaitoh if ((media & IFM_FLOW) != 0) 208 1.1 msaitoh anar |= ANAR_FC; 209 1.1 msaitoh PHY_WRITE(sc, MII_ANAR, anar); 210 1.1 msaitoh /* Apparently this helps. */ 211 1.1 msaitoh PHY_READ(sc, MII_ANAR, &anar); 212 1.1 msaitoh PHY_WRITE(sc, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG); 213 1.1 msaitoh } 214 1.1 msaitoh 215 1.1 msaitoh static void 216 1.1 msaitoh smscphy_status(struct mii_softc *sc) 217 1.1 msaitoh { 218 1.1 msaitoh struct mii_data *mii = sc->mii_pdata; 219 1.1 msaitoh uint16_t bmcr, bmsr, status; 220 1.1 msaitoh 221 1.3 thorpej KASSERT(mii_locked(mii)); 222 1.3 thorpej 223 1.1 msaitoh mii->mii_media_status = IFM_AVALID; 224 1.1 msaitoh mii->mii_media_active = IFM_ETHER; 225 1.1 msaitoh 226 1.1 msaitoh PHY_READ(sc, MII_BMSR, &bmsr); 227 1.1 msaitoh PHY_READ(sc, MII_BMSR, &bmsr); 228 1.1 msaitoh if ((bmsr & BMSR_LINK) != 0) 229 1.1 msaitoh mii->mii_media_status |= IFM_ACTIVE; 230 1.1 msaitoh 231 1.1 msaitoh PHY_READ(sc, MII_BMCR, &bmcr); 232 1.1 msaitoh if ((bmcr & BMCR_ISO) != 0) { 233 1.1 msaitoh mii->mii_media_active |= IFM_NONE; 234 1.1 msaitoh mii->mii_media_status = 0; 235 1.1 msaitoh return; 236 1.1 msaitoh } 237 1.1 msaitoh 238 1.1 msaitoh if ((bmcr & BMCR_LOOP) != 0) 239 1.1 msaitoh mii->mii_media_active |= IFM_LOOP; 240 1.1 msaitoh 241 1.1 msaitoh if ((bmcr & BMCR_AUTOEN) != 0) { 242 1.1 msaitoh if ((bmsr & BMSR_ACOMP) == 0) { 243 1.1 msaitoh /* Erg, still trying, I guess... */ 244 1.1 msaitoh mii->mii_media_active |= IFM_NONE; 245 1.1 msaitoh return; 246 1.1 msaitoh } 247 1.1 msaitoh } 248 1.1 msaitoh 249 1.1 msaitoh PHY_READ(sc, SMSCPHY_SPCSR, &status); 250 1.1 msaitoh if ((status & SPCSR_SPDIND_SPDMASK) == SPCSR_SPDIND_100) 251 1.1 msaitoh mii->mii_media_active |= IFM_100_TX; 252 1.1 msaitoh else 253 1.1 msaitoh mii->mii_media_active |= IFM_10_T; 254 1.1 msaitoh if (status & SPCSR_SPDIND_FDX) 255 1.1 msaitoh mii->mii_media_active |= IFM_FDX | mii_phy_flowstatus(sc); 256 1.1 msaitoh else 257 1.1 msaitoh mii->mii_media_active |= IFM_HDX; 258 1.1 msaitoh } 259