ruephy.c revision 114577
1/*- 2 * Copyright (c) 2001-2003, Shunsuke Akiyama <akiyama@FreeBSD.org>. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD: head/sys/dev/mii/ruephy.c 114577 2003-05-03 10:16:56Z akiyama $ 27 */ 28 29/* 30 * driver for RealTek RTL8150 internal PHY 31 */ 32 33#include <sys/param.h> 34#include <sys/systm.h> 35#include <sys/kernel.h> 36#include <sys/malloc.h> 37#include <sys/socket.h> 38#include <sys/bus.h> 39 40#include <net/if.h> 41#include <net/if_arp.h> 42#include <net/if_media.h> 43 44#include <dev/mii/mii.h> 45#include <dev/mii/miivar.h> 46#include "miidevs.h" 47 48#include <machine/bus.h> 49#include <dev/mii/ruephyreg.h> 50 51#include "miibus_if.h" 52 53static int ruephy_probe(device_t); 54static int ruephy_attach(device_t); 55 56static device_method_t ruephy_methods[] = { 57 /* device interface */ 58 DEVMETHOD(device_probe, ruephy_probe), 59 DEVMETHOD(device_attach, ruephy_attach), 60 DEVMETHOD(device_detach, mii_phy_detach), 61 DEVMETHOD(device_shutdown, bus_generic_shutdown), 62 { 0, 0 } 63}; 64 65static devclass_t ruephy_devclass; 66 67static driver_t ruephy_driver = { 68 "ruephy", 69 ruephy_methods, 70 sizeof(struct mii_softc) 71}; 72 73DRIVER_MODULE(ruephy, miibus, ruephy_driver, ruephy_devclass, 0, 0); 74 75static int ruephy_service(struct mii_softc *, struct mii_data *, int); 76static void ruephy_reset(struct mii_softc *); 77static void ruephy_status(struct mii_softc *); 78 79static int 80ruephy_probe(device_t dev) 81{ 82 struct mii_attach_args *ma; 83 device_t parent; 84 85 ma = device_get_ivars(dev); 86 parent = device_get_parent(device_get_parent(dev)); 87 88 /* 89 * RealTek RTL8150 PHY doesn't have vendor/device ID registers: 90 * the rue driver fakes up a return value of all zeros. 91 */ 92 if (MII_OUI(ma->mii_id1, ma->mii_id2) != 0 || 93 MII_MODEL(ma->mii_id2) != 0) 94 return (ENXIO); 95 96 /* 97 * Make sure the parent is an 'rue'. 98 */ 99 if (strcmp(device_get_name(parent), "rue") != 0) 100 return (ENXIO); 101 102 device_set_desc(dev, "RealTek RTL8150 internal media interface"); 103 104 return (0); 105} 106 107static int 108ruephy_attach(device_t dev) 109{ 110 struct mii_softc *sc; 111 struct mii_attach_args *ma; 112 struct mii_data *mii; 113 114 sc = device_get_softc(dev); 115 ma = device_get_ivars(dev); 116 sc->mii_dev = device_get_parent(dev); 117 mii = device_get_softc(sc->mii_dev); 118 119 /* 120 * The RealTek PHY can never be isolated, so never allow non-zero 121 * instances! 122 */ 123 if (mii->mii_instance != 0) { 124 device_printf(dev, "ignoring this PHY, non-zero instance\n"); 125 return (ENXIO); 126 } 127 128 LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list); 129 130 sc->mii_inst = mii->mii_instance; 131 sc->mii_phy = ma->mii_phyno; 132 sc->mii_service = ruephy_service; 133 sc->mii_pdata = mii; 134 mii->mii_instance++; 135 136 sc->mii_flags |= MIIF_NOISOLATE; 137 138#define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL) 139 140 ruephy_reset(sc); 141 142 sc->mii_capabilities = 143 PHY_READ(sc, MII_BMSR) & ma->mii_capmask; 144 device_printf(dev, " "); 145 mii_phy_add_media(sc); 146 printf("\n"); 147#undef ADD 148 149 MIIBUS_MEDIAINIT(sc->mii_dev); 150 return (0); 151} 152 153static int 154ruephy_service(struct mii_softc *sc, struct mii_data *mii, int cmd) 155{ 156 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 157 int reg; 158 159 /* 160 * We can't isolate the RealTek RTL8150 PHY, 161 * so it has to be the only one! 162 */ 163 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 164 panic("ruephy_service: can't isolate RealTek RTL8150 PHY"); 165 166 switch (cmd) { 167 case MII_POLLSTAT: 168 break; 169 170 case MII_MEDIACHG: 171 /* 172 * If the interface is not up, don't do anything. 173 */ 174 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 175 break; 176 177 switch (IFM_SUBTYPE(ife->ifm_media)) { 178 case IFM_AUTO: 179 /* 180 * If we're already in auto mode, just return. 181 */ 182 if (PHY_READ(sc, MII_BMCR) & BMCR_AUTOEN) 183 return (0); 184 (void) mii_phy_auto(sc); 185 break; 186 case IFM_100_T4: 187 /* 188 * XXX Not supported as a manual setting right now. 189 */ 190 return (EINVAL); 191 default: 192 /* 193 * BMCR data is stored in the ifmedia entry. 194 */ 195 PHY_WRITE(sc, MII_ANAR, 196 mii_anar(ife->ifm_media)); 197 PHY_WRITE(sc, MII_BMCR, ife->ifm_data); 198 } 199 break; 200 201 case MII_TICK: 202 /* 203 * Is the interface even up? 204 */ 205 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 206 return (0); 207 208 /* 209 * Only used for autonegotiation. 210 */ 211 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) 212 break; 213 214 /* 215 * Check to see if we have link. If we do, we don't 216 * need to restart the autonegotiation process. Read 217 * the MSR twice in case it's latched. 218 */ 219 reg = PHY_READ(sc, RUEPHY_MII_MSR) | 220 PHY_READ(sc, RUEPHY_MII_MSR); 221 if (reg & RUEPHY_MSR_LINK) 222 break; 223 224 /* 225 * Only retry autonegotiation every 5 seconds. 226 */ 227 if (++sc->mii_ticks != 5) 228 return (0); 229 230 sc->mii_ticks = 0; 231 ruephy_reset(sc); 232 if (mii_phy_auto(sc) == EJUSTRETURN) 233 return (0); 234 break; 235 } 236 237 /* Update the media status. */ 238 ruephy_status(sc); 239 240 /* Callback if something changed. */ 241 mii_phy_update(sc, cmd); 242 243 return (0); 244} 245 246static void 247ruephy_reset(struct mii_softc *sc) 248{ 249 250 mii_phy_reset(sc); 251 252 /* 253 * XXX RealTek RTL8150 PHY doesn't set the BMCR properly after 254 * XXX reset, which breaks autonegotiation. 255 */ 256 PHY_WRITE(sc, MII_BMCR, (BMCR_S100 | BMCR_AUTOEN | BMCR_FDX)); 257} 258 259static void 260ruephy_status(struct mii_softc *phy) 261{ 262 struct mii_data *mii = phy->mii_pdata; 263 int bmsr, bmcr, msr; 264 265 mii->mii_media_status = IFM_AVALID; 266 mii->mii_media_active = IFM_ETHER; 267 268 msr = PHY_READ(phy, RUEPHY_MII_MSR) | PHY_READ(phy, RUEPHY_MII_MSR); 269 if (msr & RUEPHY_MSR_LINK) 270 mii->mii_media_status |= IFM_ACTIVE; 271 272 bmcr = PHY_READ(phy, MII_BMCR); 273 if (bmcr & BMCR_ISO) { 274 mii->mii_media_active |= IFM_NONE; 275 mii->mii_media_status = 0; 276 return; 277 } 278 279 bmsr = PHY_READ(phy, MII_BMSR) | PHY_READ(phy, MII_BMSR); 280 281 if (bmcr & BMCR_AUTOEN) { 282 if ((bmsr & BMSR_ACOMP) == 0) { 283 /* Erg, still trying, I guess... */ 284 mii->mii_media_active |= IFM_NONE; 285 return; 286 } 287 288 if (msr & RUEPHY_MSR_SPEED100) 289 mii->mii_media_active |= IFM_100_TX; 290 else 291 mii->mii_media_active |= IFM_10_T; 292 293 if (msr & RUEPHY_MSR_DUPLEX) 294 mii->mii_media_active |= IFM_FDX; 295 } else 296 mii->mii_media_active = mii_media_from_bmcr(bmcr); 297} 298