ruephy.c revision 160076
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 */ 27 28#include <sys/cdefs.h> 29__FBSDID("$FreeBSD: head/sys/dev/mii/ruephy.c 160076 2006-07-03 02:53:40Z yongari $"); 30 31/* 32 * driver for RealTek RTL8150 internal PHY 33 */ 34 35#include <sys/param.h> 36#include <sys/systm.h> 37#include <sys/kernel.h> 38#include <sys/malloc.h> 39#include <sys/module.h> 40#include <sys/socket.h> 41#include <sys/bus.h> 42 43#include <net/if.h> 44#include <net/if_arp.h> 45#include <net/if_media.h> 46 47#include <dev/mii/mii.h> 48#include <dev/mii/miivar.h> 49#include "miidevs.h" 50 51#include <machine/bus.h> 52#include <dev/mii/ruephyreg.h> 53 54#include "miibus_if.h" 55 56static int ruephy_probe(device_t); 57static int ruephy_attach(device_t); 58 59static device_method_t ruephy_methods[] = { 60 /* device interface */ 61 DEVMETHOD(device_probe, ruephy_probe), 62 DEVMETHOD(device_attach, ruephy_attach), 63 DEVMETHOD(device_detach, mii_phy_detach), 64 DEVMETHOD(device_shutdown, bus_generic_shutdown), 65 { 0, 0 } 66}; 67 68static devclass_t ruephy_devclass; 69 70static driver_t ruephy_driver = { 71 "ruephy", 72 ruephy_methods, 73 sizeof(struct mii_softc) 74}; 75 76DRIVER_MODULE(ruephy, miibus, ruephy_driver, ruephy_devclass, 0, 0); 77 78static int ruephy_service(struct mii_softc *, struct mii_data *, int); 79static void ruephy_reset(struct mii_softc *); 80static void ruephy_status(struct mii_softc *); 81 82static int 83ruephy_probe(device_t dev) 84{ 85 struct mii_attach_args *ma; 86 device_t parent; 87 88 ma = device_get_ivars(dev); 89 parent = device_get_parent(device_get_parent(dev)); 90 91 /* 92 * RealTek RTL8150 PHY doesn't have vendor/device ID registers: 93 * the rue driver fakes up a return value of all zeros. 94 */ 95 if (MII_OUI(ma->mii_id1, ma->mii_id2) != 0 || 96 MII_MODEL(ma->mii_id2) != 0) 97 return (ENXIO); 98 99 /* 100 * Make sure the parent is an 'rue'. 101 */ 102 if (strcmp(device_get_name(parent), "rue") != 0) 103 return (ENXIO); 104 105 device_set_desc(dev, "RealTek RTL8150 internal media interface"); 106 107 return (BUS_PROBE_DEFAULT); 108} 109 110static int 111ruephy_attach(device_t dev) 112{ 113 struct mii_softc *sc; 114 struct mii_attach_args *ma; 115 struct mii_data *mii; 116 117 sc = device_get_softc(dev); 118 ma = device_get_ivars(dev); 119 sc->mii_dev = device_get_parent(dev); 120 mii = device_get_softc(sc->mii_dev); 121 122 /* 123 * The RealTek PHY can never be isolated, so never allow non-zero 124 * instances! 125 */ 126 if (mii->mii_instance != 0) { 127 device_printf(dev, "ignoring this PHY, non-zero instance\n"); 128 return (ENXIO); 129 } 130 131 LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list); 132 133 sc->mii_inst = mii->mii_instance; 134 sc->mii_phy = ma->mii_phyno; 135 sc->mii_service = ruephy_service; 136 sc->mii_pdata = mii; 137 mii->mii_instance++; 138 139 sc->mii_flags |= MIIF_NOISOLATE; 140 141#define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL) 142 143 ruephy_reset(sc); 144 145 sc->mii_capabilities = 146 PHY_READ(sc, MII_BMSR) & ma->mii_capmask; 147 device_printf(dev, " "); 148 mii_phy_add_media(sc); 149 printf("\n"); 150#undef ADD 151 152 MIIBUS_MEDIAINIT(sc->mii_dev); 153 return (0); 154} 155 156static int 157ruephy_service(struct mii_softc *sc, struct mii_data *mii, int cmd) 158{ 159 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 160 int reg; 161 162 /* 163 * We can't isolate the RealTek RTL8150 PHY, 164 * so it has to be the only one! 165 */ 166 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 167 panic("ruephy_service: can't isolate RealTek RTL8150 PHY"); 168 169 switch (cmd) { 170 case MII_POLLSTAT: 171 break; 172 173 case MII_MEDIACHG: 174 /* 175 * If the interface is not up, don't do anything. 176 */ 177 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 178 break; 179 180 switch (IFM_SUBTYPE(ife->ifm_media)) { 181 case IFM_AUTO: 182 /* 183 * If we're already in auto mode, just return. 184 */ 185 if (PHY_READ(sc, MII_BMCR) & BMCR_AUTOEN) 186 return (0); 187 (void) mii_phy_auto(sc); 188 break; 189 case IFM_100_T4: 190 /* 191 * XXX Not supported as a manual setting right now. 192 */ 193 return (EINVAL); 194 default: 195 /* 196 * BMCR data is stored in the ifmedia entry. 197 */ 198 PHY_WRITE(sc, MII_ANAR, 199 mii_anar(ife->ifm_media)); 200 PHY_WRITE(sc, MII_BMCR, ife->ifm_data); 201 } 202 break; 203 204 case MII_TICK: 205 /* 206 * Is the interface even up? 207 */ 208 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 209 return (0); 210 211 /* 212 * Only used for autonegotiation. 213 */ 214 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) 215 break; 216 217 /* 218 * Check to see if we have link. If we do, we don't 219 * need to restart the autonegotiation process. Read 220 * the MSR twice in case it's latched. 221 */ 222 reg = PHY_READ(sc, RUEPHY_MII_MSR) | 223 PHY_READ(sc, RUEPHY_MII_MSR); 224 if (reg & RUEPHY_MSR_LINK) 225 break; 226 227 /* 228 * Only retry autonegotiation every 5 seconds. 229 */ 230 if (++sc->mii_ticks <= 5) 231 break; 232 233 sc->mii_ticks = 0; 234 ruephy_reset(sc); 235 if (mii_phy_auto(sc) == EJUSTRETURN) 236 return (0); 237 break; 238 } 239 240 /* Update the media status. */ 241 ruephy_status(sc); 242 243 /* Callback if something changed. */ 244 mii_phy_update(sc, cmd); 245 246 return (0); 247} 248 249static void 250ruephy_reset(struct mii_softc *sc) 251{ 252 253 mii_phy_reset(sc); 254 255 /* 256 * XXX RealTek RTL8150 PHY doesn't set the BMCR properly after 257 * XXX reset, which breaks autonegotiation. 258 */ 259 PHY_WRITE(sc, MII_BMCR, (BMCR_S100 | BMCR_AUTOEN | BMCR_FDX)); 260} 261 262static void 263ruephy_status(struct mii_softc *phy) 264{ 265 struct mii_data *mii = phy->mii_pdata; 266 int bmsr, bmcr, msr; 267 268 mii->mii_media_status = IFM_AVALID; 269 mii->mii_media_active = IFM_ETHER; 270 271 msr = PHY_READ(phy, RUEPHY_MII_MSR) | PHY_READ(phy, RUEPHY_MII_MSR); 272 if (msr & RUEPHY_MSR_LINK) 273 mii->mii_media_status |= IFM_ACTIVE; 274 275 bmcr = PHY_READ(phy, MII_BMCR); 276 if (bmcr & BMCR_ISO) { 277 mii->mii_media_active |= IFM_NONE; 278 mii->mii_media_status = 0; 279 return; 280 } 281 282 bmsr = PHY_READ(phy, MII_BMSR) | PHY_READ(phy, MII_BMSR); 283 284 if (bmcr & BMCR_AUTOEN) { 285 if ((bmsr & BMSR_ACOMP) == 0) { 286 /* Erg, still trying, I guess... */ 287 mii->mii_media_active |= IFM_NONE; 288 return; 289 } 290 291 if (msr & RUEPHY_MSR_SPEED100) 292 mii->mii_media_active |= IFM_100_TX; 293 else 294 mii->mii_media_active |= IFM_10_T; 295 296 if (msr & RUEPHY_MSR_DUPLEX) 297 mii->mii_media_active |= IFM_FDX; 298 } else 299 mii->mii_media_active = mii_media_from_bmcr(bmcr); 300} 301