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