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