1 /* $NetBSD: acphy.c,v 1.16 2006/03/29 07:05:24 thorpej Exp $ */ 2 3 /* 4 * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 9 * NASA Ames Research Center. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the NetBSD 22 * Foundation, Inc. and its contributors. 23 * 4. Neither the name of The NetBSD Foundation nor the names of its 24 * contributors may be used to endorse or promote products derived 25 * from this software without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 37 * POSSIBILITY OF SUCH DAMAGE. 38 * 39 * $FreeBSD: src/sys/dev/mii/acphy.c,v 1.2.2.2 2002/10/21 21:20:19 semenu Exp $ 40 */ 41 42 /* 43 * Copyright (c) 1997 Manuel Bouyer. All rights reserved. 44 * 45 * Redistribution and use in source and binary forms, with or without 46 * modification, are permitted provided that the following conditions 47 * are met: 48 * 1. Redistributions of source code must retain the above copyright 49 * notice, this list of conditions and the following disclaimer. 50 * 2. Redistributions in binary form must reproduce the above copyright 51 * notice, this list of conditions and the following disclaimer in the 52 * documentation and/or other materials provided with the distribution. 53 * 3. All advertising materials mentioning features or use of this software 54 * must display the following acknowledgement: 55 * This product includes software developed by Manuel Bouyer. 56 * 4. The name of the author may not be used to endorse or promote products 57 * derived from this software without specific prior written permission. 58 * 59 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 60 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 61 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 62 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 63 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 64 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 65 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 66 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 67 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 68 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 69 */ 70 71 /* 72 * Driver for Altima AC101 10/100 PHY 73 */ 74 75 #include <sys/param.h> 76 #include <sys/systm.h> 77 #include <sys/kernel.h> 78 #include <sys/socket.h> 79 #include <sys/errno.h> 80 #include <sys/module.h> 81 #include <sys/bus.h> 82 83 #include <net/if.h> 84 #include <net/if_media.h> 85 86 #include "mii.h" 87 #include "miivar.h" 88 #include "miidevs.h" 89 90 #include "acphyreg.h" 91 92 #include "miibus_if.h" 93 94 static int acphy_probe (device_t); 95 static int acphy_attach (device_t); 96 97 static device_method_t acphy_methods[] = { 98 /* device interface */ 99 DEVMETHOD(device_probe, acphy_probe), 100 DEVMETHOD(device_attach, acphy_attach), 101 DEVMETHOD(device_detach, ukphy_detach), 102 DEVMETHOD(device_shutdown, bus_generic_shutdown), 103 { 0, 0 } 104 }; 105 106 static const struct mii_phydesc acphys[] = { 107 MII_PHYDESC(xxALTIMA, AC101), 108 MII_PHYDESC(xxALTIMA, AC101L), 109 MII_PHYDESC(xxALTIMA, AC_UNKNOWN), 110 MII_PHYDESC(xxALTIMA, Am79C875), 111 MII_PHYDESC_NULL 112 }; 113 114 static devclass_t acphy_devclass; 115 116 static driver_t acphy_driver = { 117 "acphy", 118 acphy_methods, 119 sizeof(struct mii_softc) 120 }; 121 122 DRIVER_MODULE(acphy, miibus, acphy_driver, acphy_devclass, NULL, NULL); 123 124 static int acphy_service(struct mii_softc *, struct mii_data *, int); 125 static void acphy_reset(struct mii_softc *); 126 static void acphy_status(struct mii_softc *); 127 128 static int 129 acphy_probe(device_t dev) 130 { 131 struct mii_attach_args *ma = device_get_ivars(dev); 132 const struct mii_phydesc *mpd; 133 134 mpd = mii_phy_match(ma, acphys); 135 if (mpd != NULL) { 136 device_set_desc(dev, mpd->mpd_name); 137 return (0); 138 } 139 return (ENXIO); 140 } 141 142 static int 143 acphy_attach(device_t dev) 144 { 145 struct mii_softc *sc; 146 struct mii_attach_args *ma; 147 struct mii_data *mii; 148 149 sc = device_get_softc(dev); 150 ma = device_get_ivars(dev); 151 mii_softc_init(sc, ma); 152 sc->mii_dev = device_get_parent(dev); 153 mii = device_get_softc(sc->mii_dev); 154 LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list); 155 156 sc->mii_inst = mii->mii_instance; 157 sc->mii_service = acphy_service; 158 sc->mii_reset = acphy_reset; 159 sc->mii_pdata = mii; 160 sc->mii_flags |= MIIF_NOISOLATE; 161 162 acphy_reset(sc); 163 164 mii->mii_instance++; 165 166 sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & ma->mii_capmask; 167 device_printf(dev, " "); 168 if (sc->mii_capabilities & BMSR_MEDIAMASK) 169 mii_phy_add_media(sc); 170 else 171 kprintf("no media present"); 172 kprintf("\n"); 173 174 MIIBUS_MEDIAINIT(sc->mii_dev); 175 return (0); 176 } 177 178 static int 179 acphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd) 180 { 181 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 182 int reg; 183 184 /* 185 * If we're not selected, then do nothing, just isolate and power 186 * down, if changing media. 187 */ 188 if (IFM_INST(ife->ifm_media) != sc->mii_inst) { 189 if (cmd == MII_MEDIACHG) { 190 reg = PHY_READ(sc, MII_BMCR); 191 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO | BMCR_PDOWN); 192 } 193 194 return (0); 195 } 196 197 switch (cmd) { 198 case MII_POLLSTAT: 199 break; 200 201 case MII_MEDIACHG: 202 /* 203 * If the interface is not up, don't do anything. 204 */ 205 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 206 break; 207 208 mii_phy_set_media(sc); 209 break; 210 211 case MII_TICK: 212 /* 213 * Is the interface even up? 214 */ 215 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 216 return (0); 217 218 /* 219 * Only used for autonegotiation. 220 */ 221 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) 222 break; 223 224 /* 225 * This PHY's autonegotiation doesn't need to be kicked. 226 */ 227 break; 228 } 229 230 /* Update the media status. */ 231 acphy_status(sc); 232 233 /* Callback if something changed. */ 234 mii_phy_update(sc, cmd); 235 return (0); 236 } 237 238 static void 239 acphy_status(struct mii_softc *sc) 240 { 241 struct mii_data *mii = sc->mii_pdata; 242 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 243 int bmsr, bmcr; 244 245 mii->mii_media_status = IFM_AVALID; 246 mii->mii_media_active = IFM_ETHER; 247 248 bmsr = PHY_READ(sc, MII_BMSR) | 249 PHY_READ(sc, MII_BMSR); 250 if (bmsr & BMSR_LINK) 251 mii->mii_media_status |= IFM_ACTIVE; 252 253 bmcr = PHY_READ(sc, MII_BMCR); 254 if (bmcr & BMCR_ISO) { 255 mii->mii_media_active |= IFM_NONE; 256 mii->mii_media_status = 0; 257 return; 258 } 259 260 if (bmcr & BMCR_LOOP) 261 mii->mii_media_active |= IFM_LOOP; 262 263 if (bmcr & BMCR_AUTOEN) { 264 int diag; 265 266 if ((bmsr & BMSR_ACOMP) == 0) { 267 /* Erg, still trying, I guess... */ 268 mii->mii_media_active |= IFM_NONE; 269 return; 270 } 271 272 diag = PHY_READ(sc, MII_ACPHY_DIAG); 273 if (diag & AC_DIAG_SPEED) 274 mii->mii_media_active |= IFM_100_TX; 275 else 276 mii->mii_media_active |= IFM_10_T; 277 278 if (diag & AC_DIAG_DUPLEX) 279 mii->mii_media_active |= IFM_FDX; 280 } else { 281 mii->mii_media_active = ife->ifm_media; 282 } 283 } 284 285 static void 286 acphy_reset(struct mii_softc *sc) 287 { 288 mii_phy_reset(sc); 289 PHY_WRITE(sc, MII_ACPHY_INT, 0); 290 } 291