1 /* $NetBSD: urlphy.c,v 1.40 2023/02/22 08:09:09 msaitoh Exp $ */ 2 /* 3 * Copyright (c) 2001, 2002 4 * Shingo WATANABE <nabe@nabechan.org>. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. Neither the name of the author nor the names of any co-contributors 15 * may be used to endorse or promote products derived from this software 16 * without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 * 30 */ 31 32 /* 33 * driver for Realtek RL8150L internal phy 34 */ 35 36 #include <sys/cdefs.h> 37 __KERNEL_RCSID(0, "$NetBSD: urlphy.c,v 1.40 2023/02/22 08:09:09 msaitoh Exp $"); 38 39 #include <sys/param.h> 40 #include <sys/systm.h> 41 #include <sys/kernel.h> 42 #include <sys/device.h> 43 #include <sys/socket.h> 44 45 #include <net/if.h> 46 #include <net/if_media.h> 47 48 #include <dev/mii/mii.h> 49 #include <dev/mii/miivar.h> 50 #include <dev/mii/miidevs.h> 51 #include <dev/mii/urlphyreg.h> 52 53 #ifdef URLPHY_DEBUG 54 #define DPRINTF(x) if (urlphydebug) printf x 55 #define DPRINTFN(n,x) if (urlphydebug>(n)) printf x 56 int urlphydebug = URLPHY_DEBUG; 57 #else 58 #define DPRINTF(x) 59 #define DPRINTFN(n,x) 60 #endif 61 62 static int urlphy_match(device_t, cfdata_t, void *); 63 static void urlphy_attach(device_t, device_t, void *); 64 65 CFATTACH_DECL_NEW(urlphy, sizeof(struct mii_softc), 66 urlphy_match, urlphy_attach, mii_phy_detach, mii_phy_activate); 67 68 static int urlphy_service(struct mii_softc *, struct mii_data *, int); 69 static void urlphy_status(struct mii_softc *); 70 71 static const struct mii_phy_funcs urlphy_funcs = { 72 urlphy_service, urlphy_status, mii_phy_reset, 73 }; 74 75 static int 76 urlphy_match(device_t parent, cfdata_t match, void *aux) 77 { 78 struct mii_attach_args *ma = aux; 79 80 /* RTL8150 reports OUI == 0, MODEL == 0 */ 81 if (MII_OUI(ma->mii_id1, ma->mii_id2) != 0 && 82 MII_MODEL(ma->mii_id2) != 0) 83 return 0; 84 85 /* Make sure the parent is an 'url' device. */ 86 if (!device_is_a(parent, "url")) 87 return 0; 88 89 return 10; 90 } 91 92 static void 93 urlphy_attach(device_t parent, device_t self, void *aux) 94 { 95 struct mii_softc *sc = device_private(self); 96 struct mii_attach_args *ma = aux; 97 struct mii_data *mii = ma->mii_data; 98 99 aprint_naive(": Media interface\n"); 100 aprint_normal(": Realtek RTL8150L internal media interface\n"); 101 102 DPRINTF(("%s: %s: enter\n", device_xname(self), __func__)); 103 104 sc->mii_dev = self; 105 sc->mii_inst = mii->mii_instance; 106 sc->mii_phy = ma->mii_phyno; 107 sc->mii_funcs = &urlphy_funcs; 108 sc->mii_pdata = mii; 109 sc->mii_flags = ma->mii_flags; 110 111 /* Don't do loopback on this PHY. */ 112 sc->mii_flags |= MIIF_NOLOOP; 113 /* Don't do isolate on this PHY. */ 114 sc->mii_flags |= MIIF_NOISOLATE; 115 116 if (mii->mii_instance != 0) { 117 aprint_error_dev(self, 118 "ignoring this PHY, non-zero instance\n"); 119 return; 120 } 121 122 mii_lock(mii); 123 124 PHY_RESET(sc); 125 126 PHY_READ(sc, MII_BMSR, &sc->mii_capabilities); 127 sc->mii_capabilities &= ma->mii_capmask; 128 129 mii_unlock(mii); 130 131 mii_phy_add_media(sc); 132 } 133 134 static int 135 urlphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd) 136 { 137 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 138 uint16_t reg; 139 140 DPRINTF(("%s: %s: enter\n", device_xname(sc->mii_dev), __func__)); 141 142 switch (cmd) { 143 case MII_POLLSTAT: 144 /* If we're not polling our PHY instance, just return. */ 145 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 146 return 0; 147 break; 148 149 case MII_MEDIACHG: 150 /* If we're not currently selected, just return. */ 151 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 152 return 0; 153 154 /* If the interface is not up, don't do anything. */ 155 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 156 break; 157 158 mii_phy_setmedia(sc); 159 break; 160 161 case MII_TICK: 162 /* If we're not currently selected, just return. */ 163 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 164 return 0; 165 166 /* Just bail now if the interface is down. */ 167 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 168 return 0; 169 170 /* 171 * If we're not doing autonegotiation, we don't need to do 172 * any extra work here. However, we need to check the link 173 * status so we can generate an announcement if the status 174 * changes. 175 */ 176 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) 177 break; 178 179 /* Read the status register twice; MSR_LINK is latch-low. */ 180 PHY_READ(sc, URLPHY_MSR, ®); 181 PHY_READ(sc, URLPHY_MSR, ®); 182 if (reg & URLPHY_MSR_LINK) { 183 /* 184 * Reset autonegotiation timer to 0 in case the link 185 * goes down in the next tick. 186 */ 187 sc->mii_ticks = 0; 188 /* See above. */ 189 break; 190 } 191 192 /* 193 * mii_ticks == 0 means it's the first tick after changing the 194 * media or the link became down since the last tick (see 195 * above), so break to update the status. 196 */ 197 if (sc->mii_ticks++ == 0) 198 break; 199 200 /* Only retry autonegotiation every N seconds. */ 201 KASSERT(sc->mii_anegticks != 0); 202 if (sc->mii_ticks < sc->mii_anegticks) 203 return 0; 204 205 if (mii_phy_auto_restart(sc) == EJUSTRETURN) 206 return 0; 207 208 break; 209 210 case MII_DOWN: 211 mii_phy_down(sc); 212 return 0; 213 } 214 215 /* Update the media status. */ 216 mii_phy_status(sc); 217 218 /* Callback if something changed. */ 219 mii_phy_update(sc, cmd); 220 221 return 0; 222 } 223 224 static void 225 urlphy_status(struct mii_softc *sc) 226 { 227 struct mii_data *mii = sc->mii_pdata; 228 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 229 uint16_t msr, bmsr, bmcr; 230 231 DPRINTF(("%s: %s: enter\n", device_xname(sc->mii_dev), __func__)); 232 233 mii->mii_media_status = IFM_AVALID; 234 mii->mii_media_active = IFM_ETHER; 235 236 /* 237 * The link status bit is not exist in the BMSR register, 238 * so we need to read the MSR register to get link status. 239 */ 240 PHY_READ(sc, URLPHY_MSR, &msr); 241 PHY_READ(sc, URLPHY_MSR, &msr); 242 if (msr & URLPHY_MSR_LINK) 243 mii->mii_media_status |= IFM_ACTIVE; 244 245 DPRINTF(("%s: %s: link %s\n", device_xname(sc->mii_dev), __func__, 246 mii->mii_media_status & IFM_ACTIVE ? "up" : "down")); 247 248 PHY_READ(sc, MII_BMCR, &bmcr); 249 if (bmcr & BMCR_AUTOEN) { 250 PHY_READ(sc, MII_BMSR, &bmsr); 251 PHY_READ(sc, MII_BMSR, &bmsr); 252 if ((bmsr & BMSR_ACOMP) == 0) { 253 /* Erg, still trying, I guess... */ 254 mii->mii_media_active |= IFM_NONE; 255 return; 256 } 257 258 if (msr & URLPHY_MSR_SPEED_100) 259 mii->mii_media_active |= IFM_100_TX; 260 else 261 mii->mii_media_active |= IFM_10_T; 262 263 if (msr & URLPHY_MSR_DUPLEX) 264 mii->mii_media_active |= IFM_FDX; 265 else 266 mii->mii_media_active |= IFM_HDX; 267 } else 268 mii->mii_media_active = ife->ifm_media; 269 } 270