1 /* 2 * Copyright (c) 1997, 1998, 1999 3 * Bill Paul <wpaul@ctr.columbia.edu>. 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 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by Bill Paul. 16 * 4. Neither the name of the author nor the names of any co-contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 30 * THE POSSIBILITY OF SUCH DAMAGE. 31 * 32 * $FreeBSD$ 33 */ 34 35 /* 36 * driver for RealTek 8139 internal PHYs 37 */ 38 39 #include <sys/param.h> 40 #include <sys/systm.h> 41 #include <sys/kernel.h> 42 #include <sys/socket.h> 43 #include <sys/bus.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 51 #include "miibus_if.h" 52 53 #if !defined(lint) 54 static const char rcsid[] = 55 "$FreeBSD$"; 56 #endif 57 58 static int rlphy_probe __P((device_t)); 59 static int rlphy_attach __P((device_t)); 60 static int rlphy_detach __P((device_t)); 61 62 static device_method_t rlphy_methods[] = { 63 /* device interface */ 64 DEVMETHOD(device_probe, rlphy_probe), 65 DEVMETHOD(device_attach, rlphy_attach), 66 DEVMETHOD(device_detach, rlphy_detach), 67 DEVMETHOD(device_shutdown, bus_generic_shutdown), 68 { 0, 0 } 69 }; 70 71 static devclass_t rlphy_devclass; 72 73 static driver_t rlphy_driver = { 74 "rlphy", 75 rlphy_methods, 76 sizeof(struct mii_softc) 77 }; 78 79 DRIVER_MODULE(rlphy, miibus, rlphy_driver, rlphy_devclass, 0, 0); 80 81 int rlphy_service __P((struct mii_softc *, struct mii_data *, int)); 82 void rlphy_reset __P((struct mii_softc *)); 83 84 static int rlphy_probe(dev) 85 device_t dev; 86 { 87 struct mii_attach_args *ma; 88 device_t parent; 89 90 ma = device_get_ivars(dev); 91 parent = device_get_parent(device_get_parent(dev)); 92 93 /* 94 * RealTek PHY doesn't have vendor/device ID registers: 95 * the rl driver fakes up a return value of all zeros. 96 */ 97 if (MII_OUI(ma->mii_id1, ma->mii_id2) != 0 || 98 MII_MODEL(ma->mii_id2) != 0) 99 return (ENXIO); 100 101 /* 102 * Make sure the parent is an `rl'. 103 */ 104 if (strcmp(device_get_name(parent), "rl") != 0) 105 return (ENXIO); 106 107 device_set_desc(dev, "RealTek internal media interface"); 108 109 return (0); 110 } 111 112 static int rlphy_attach(dev) 113 device_t dev; 114 { 115 struct mii_softc *sc; 116 struct mii_attach_args *ma; 117 struct mii_data *mii; 118 119 sc = device_get_softc(dev); 120 ma = device_get_ivars(dev); 121 sc->mii_dev = device_get_parent(dev); 122 mii = device_get_softc(sc->mii_dev); 123 124 /* 125 * The RealTek PHY can never be isolated, so never allow non-zero 126 * instances! 127 */ 128 if (mii->mii_instance != 0) { 129 device_printf(dev, "ignoring this PHY, non-zero instance\n"); 130 return(ENXIO); 131 } 132 133 LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list); 134 135 sc->mii_inst = mii->mii_instance; 136 sc->mii_phy = ma->mii_phyno; 137 sc->mii_service = rlphy_service; 138 sc->mii_pdata = mii; 139 mii->mii_instance++; 140 141 sc->mii_flags |= MIIF_NOISOLATE; 142 143 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL) 144 145 #if 0 /* See above. */ 146 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst), 147 BMCR_ISO); 148 #endif 149 150 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->mii_inst), 151 BMCR_LOOP|BMCR_S100); 152 153 rlphy_reset(sc); 154 155 sc->mii_capabilities = 156 PHY_READ(sc, MII_BMSR) & ma->mii_capmask; 157 device_printf(dev, " "); 158 if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0) 159 printf("no media present"); 160 else 161 mii_add_media(mii, sc->mii_capabilities, 162 sc->mii_inst); 163 printf("\n"); 164 #undef ADD 165 MIIBUS_MEDIAINIT(sc->mii_dev); 166 return(0); 167 } 168 169 static int rlphy_detach(dev) 170 device_t dev; 171 { 172 struct mii_softc *sc; 173 struct mii_data *mii; 174 175 sc = device_get_softc(dev); 176 mii = device_get_softc(device_get_softc(dev)); 177 mii_phy_auto_stop(sc); 178 sc->mii_dev = NULL; 179 LIST_REMOVE(sc, mii_list); 180 181 return(0); 182 } 183 184 int 185 rlphy_service(sc, mii, cmd) 186 struct mii_softc *sc; 187 struct mii_data *mii; 188 int cmd; 189 { 190 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 191 192 /* 193 * We can't isolate the RealTek PHY, so it has to be the only one! 194 */ 195 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 196 panic("rlphy_service: can't isolate RealTek PHY"); 197 198 switch (cmd) { 199 case MII_POLLSTAT: 200 break; 201 202 case MII_MEDIACHG: 203 /* 204 * If the interface is not up, don't do anything. 205 */ 206 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 207 break; 208 209 switch (IFM_SUBTYPE(ife->ifm_media)) { 210 case IFM_AUTO: 211 /* 212 * If we're already in auto mode, just return. 213 */ 214 if (PHY_READ(sc, MII_BMCR) & BMCR_AUTOEN) 215 return (0); 216 (void) mii_phy_auto(sc, 0); 217 break; 218 case IFM_100_T4: 219 /* 220 * XXX Not supported as a manual setting right now. 221 */ 222 return (EINVAL); 223 default: 224 /* 225 * BMCR data is stored in the ifmedia entry. 226 */ 227 PHY_WRITE(sc, MII_ANAR, 228 mii_anar(ife->ifm_media)); 229 PHY_WRITE(sc, MII_BMCR, ife->ifm_data); 230 } 231 break; 232 233 case MII_TICK: 234 /* 235 * Only used for autonegotiation. 236 */ 237 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) 238 return (0); 239 240 /* 241 * Is the interface even up? 242 */ 243 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 244 return (0); 245 246 /* 247 * Only retry autonegotiation every 5 seconds. 248 */ 249 if (++sc->mii_ticks != 5) 250 return (0); 251 252 sc->mii_ticks = 0; 253 254 /* 255 * The RealTek PHY's autonegotiation doesn't need to be 256 * kicked; it continues in the background. 257 */ 258 break; 259 } 260 261 /* Update the media status. */ 262 ukphy_status(sc); 263 264 /* Callback if something changed. */ 265 if (sc->mii_active != mii->mii_media_active || cmd == MII_MEDIACHG) { 266 MIIBUS_STATCHG(sc->mii_dev); 267 sc->mii_active = mii->mii_media_active; 268 } 269 return (0); 270 } 271 272 void 273 rlphy_reset(sc) 274 struct mii_softc *sc; 275 { 276 277 mii_phy_reset(sc); 278 279 /* 280 * XXX RealTek PHY doesn't set the BMCR properly after 281 * XXX reset, which breaks autonegotiation. 282 */ 283 PHY_WRITE(sc, MII_BMCR, BMCR_S100|BMCR_AUTOEN|BMCR_FDX); 284 } 285