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