1 /* 2 * Copyright (c) 2000 Berkeley Software Design, Inc. 3 * Copyright (c) 1997, 1998, 1999, 2000 4 * Bill Paul <wpaul@osd.bsdi.com>. 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 Bill Paul. 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 Bill Paul 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 Bill Paul OR THE VOICES IN HIS HEAD 25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 31 * THE POSSIBILITY OF SUCH DAMAGE. 32 * 33 * $FreeBSD$ 34 */ 35 36 /* 37 * driver for homePNA PHYs 38 * This is really just a stub that allows us to identify homePNA-based 39 * transceicers and display the link status. MII-based homePNA PHYs 40 * only support one media type and no autonegotiation. If we were 41 * really clever, we could tweak some of the vendor-specific registers 42 * to optimize the link. 43 */ 44 45 #include <sys/param.h> 46 #include <sys/systm.h> 47 #include <sys/kernel.h> 48 #include <sys/malloc.h> 49 #include <sys/socket.h> 50 #include <sys/errno.h> 51 #include <sys/module.h> 52 #include <sys/bus.h> 53 54 #include <net/if.h> 55 #include <net/if_media.h> 56 57 58 #include <dev/mii/mii.h> 59 #include <dev/mii/miivar.h> 60 #include <dev/mii/miidevs.h> 61 62 #include "miibus_if.h" 63 64 #if !defined(lint) 65 static const char rcsid[] = 66 "$FreeBSD$"; 67 #endif 68 69 static int pnaphy_probe __P((device_t)); 70 static int pnaphy_attach __P((device_t)); 71 static int pnaphy_detach __P((device_t)); 72 73 static device_method_t pnaphy_methods[] = { 74 /* device interface */ 75 DEVMETHOD(device_probe, pnaphy_probe), 76 DEVMETHOD(device_attach, pnaphy_attach), 77 DEVMETHOD(device_detach, pnaphy_detach), 78 DEVMETHOD(device_shutdown, bus_generic_shutdown), 79 { 0, 0 } 80 }; 81 82 static devclass_t pnaphy_devclass; 83 84 static driver_t pnaphy_driver = { 85 "pnaphy", 86 pnaphy_methods, 87 sizeof(struct mii_softc) 88 }; 89 90 DRIVER_MODULE(pnaphy, miibus, pnaphy_driver, pnaphy_devclass, 0, 0); 91 92 int pnaphy_service __P((struct mii_softc *, struct mii_data *, int)); 93 94 static int 95 pnaphy_probe(dev) 96 device_t dev; 97 { 98 99 struct mii_attach_args *ma; 100 101 ma = device_get_ivars(dev); 102 103 if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_AMD && 104 MII_MODEL(ma->mii_id2) == MII_MODEL_AMD_79c978) { 105 device_set_desc(dev, MII_STR_AMD_79c978); 106 return(0); 107 } 108 109 return(ENXIO); 110 } 111 112 static int 113 pnaphy_attach(dev) 114 device_t dev; 115 { 116 struct mii_softc *sc; 117 struct mii_attach_args *ma; 118 struct mii_data *mii; 119 const char *sep = ""; 120 121 sc = device_get_softc(dev); 122 ma = device_get_ivars(dev); 123 sc->mii_dev = device_get_parent(dev); 124 mii = device_get_softc(sc->mii_dev); 125 LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list); 126 127 sc->mii_inst = mii->mii_instance; 128 sc->mii_phy = ma->mii_phyno; 129 sc->mii_service = pnaphy_service; 130 sc->mii_pdata = mii; 131 132 mii->mii_instance++; 133 134 sc->mii_flags |= MIIF_NOISOLATE; 135 136 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL) 137 #define PRINT(s) printf("%s%s", sep, s); sep = ", " 138 139 mii_phy_reset(sc); 140 141 sc->mii_capabilities = 142 PHY_READ(sc, MII_BMSR) & ma->mii_capmask; 143 device_printf(dev, " "); 144 if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0) 145 printf("no media present"); 146 else { 147 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_homePNA, 0, sc->mii_inst), 0); 148 PRINT("HomePNA"); 149 } 150 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst), 151 BMCR_ISO); 152 153 printf("\n"); 154 155 #undef ADD 156 #undef PRINT 157 158 MIIBUS_MEDIAINIT(sc->mii_dev); 159 160 return(0); 161 } 162 163 static int pnaphy_detach(dev) 164 device_t dev; 165 { 166 struct mii_softc *sc; 167 struct mii_data *mii; 168 169 sc = device_get_softc(dev); 170 mii = device_get_softc(device_get_parent(dev)); 171 sc->mii_dev = NULL; 172 LIST_REMOVE(sc, mii_list); 173 174 return(0); 175 } 176 177 int 178 pnaphy_service(sc, mii, cmd) 179 struct mii_softc *sc; 180 struct mii_data *mii; 181 int cmd; 182 { 183 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 184 int reg; 185 186 switch (cmd) { 187 case MII_POLLSTAT: 188 /* 189 * If we're not polling our PHY instance, just return. 190 */ 191 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 192 return (0); 193 break; 194 195 case MII_MEDIACHG: 196 /* 197 * If the media indicates a different PHY instance, 198 * isolate ourselves. 199 */ 200 if (IFM_INST(ife->ifm_media) != sc->mii_inst) { 201 reg = PHY_READ(sc, MII_BMCR); 202 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO); 203 return (0); 204 } 205 206 /* 207 * If the interface is not up, don't do anything. 208 */ 209 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 210 break; 211 212 switch (IFM_SUBTYPE(ife->ifm_media)) { 213 case IFM_AUTO: 214 case IFM_10_T: 215 case IFM_100_TX: 216 case IFM_100_T4: 217 return (EINVAL); 218 default: 219 /* 220 * BMCR data is stored in the ifmedia entry. 221 */ 222 PHY_WRITE(sc, MII_ANAR, 223 mii_anar(ife->ifm_media)); 224 PHY_WRITE(sc, MII_BMCR, ife->ifm_data); 225 } 226 break; 227 228 case MII_TICK: 229 /* 230 * If we're not currently selected, just return. 231 */ 232 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 233 return (0); 234 235 /* 236 * Only used for autonegotiation. 237 */ 238 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) 239 return (0); 240 241 /* 242 * Is the interface even up? 243 */ 244 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 245 return (0); 246 247 /* 248 * Check to see if we have link. If we do, we don't 249 * need to restart the autonegotiation process. Read 250 * the BMSR twice in case it's latched. 251 */ 252 reg = PHY_READ(sc, MII_BMSR) | 253 PHY_READ(sc, MII_BMSR); 254 if (reg & BMSR_LINK) 255 return (0); 256 257 /* 258 * Only retry autonegotiation every 5 seconds. 259 */ 260 if (++sc->mii_ticks != 5) 261 return (0); 262 263 sc->mii_ticks = 0; 264 mii_phy_reset(sc); 265 if (mii_phy_auto(sc, 0) == EJUSTRETURN) 266 return (0); 267 break; 268 } 269 270 /* Update the media status. */ 271 ukphy_status(sc); 272 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_10_T) 273 mii->mii_media_active = IFM_ETHER|IFM_homePNA; 274 275 /* Callback if something changed. */ 276 if (sc->mii_active != mii->mii_media_active || cmd == MII_MEDIACHG) { 277 MIIBUS_STATCHG(sc->mii_dev); 278 sc->mii_active = mii->mii_media_active; 279 } 280 return (0); 281 } 282