1*fa9fb3edSderaadt /* $OpenBSD: ukphy.c,v 1.21 2013/12/28 03:30:41 deraadt Exp $ */ 25d6e3d42Snate /* $NetBSD: ukphy.c,v 1.9 2000/02/02 23:34:57 thorpej Exp $ */ 3bf11b014Sjason 4bf11b014Sjason /*- 5b2e83a74Sjason * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc. 6bf11b014Sjason * All rights reserved. 7bf11b014Sjason * 8bf11b014Sjason * This code is derived from software contributed to The NetBSD Foundation 9bf11b014Sjason * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 10bf11b014Sjason * NASA Ames Research Center, and by Frank van der Linden. 11bf11b014Sjason * 12bf11b014Sjason * Redistribution and use in source and binary forms, with or without 13bf11b014Sjason * modification, are permitted provided that the following conditions 14bf11b014Sjason * are met: 15bf11b014Sjason * 1. Redistributions of source code must retain the above copyright 16bf11b014Sjason * notice, this list of conditions and the following disclaimer. 17bf11b014Sjason * 2. Redistributions in binary form must reproduce the above copyright 18bf11b014Sjason * notice, this list of conditions and the following disclaimer in the 19bf11b014Sjason * documentation and/or other materials provided with the distribution. 20bf11b014Sjason * 21bf11b014Sjason * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 22bf11b014Sjason * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 23bf11b014Sjason * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 24bf11b014Sjason * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 25bf11b014Sjason * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26bf11b014Sjason * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27bf11b014Sjason * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28bf11b014Sjason * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29bf11b014Sjason * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30bf11b014Sjason * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31bf11b014Sjason * POSSIBILITY OF SUCH DAMAGE. 32bf11b014Sjason */ 33bf11b014Sjason 34bf11b014Sjason /* 35bf11b014Sjason * Copyright (c) 1997 Manuel Bouyer. All rights reserved. 36bf11b014Sjason * 37bf11b014Sjason * Redistribution and use in source and binary forms, with or without 38bf11b014Sjason * modification, are permitted provided that the following conditions 39bf11b014Sjason * are met: 40bf11b014Sjason * 1. Redistributions of source code must retain the above copyright 41bf11b014Sjason * notice, this list of conditions and the following disclaimer. 42bf11b014Sjason * 2. Redistributions in binary form must reproduce the above copyright 43bf11b014Sjason * notice, this list of conditions and the following disclaimer in the 44bf11b014Sjason * documentation and/or other materials provided with the distribution. 45bf11b014Sjason * 46bf11b014Sjason * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 47bf11b014Sjason * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 48bf11b014Sjason * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 49bf11b014Sjason * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 50bf11b014Sjason * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 51bf11b014Sjason * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 52bf11b014Sjason * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 53bf11b014Sjason * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 54bf11b014Sjason * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 55bf11b014Sjason * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 56bf11b014Sjason */ 57bf11b014Sjason 58bf11b014Sjason /* 59bf11b014Sjason * driver for generic unknown PHYs 60bf11b014Sjason */ 61bf11b014Sjason 62bf11b014Sjason #include <sys/param.h> 63bf11b014Sjason #include <sys/systm.h> 64bf11b014Sjason #include <sys/kernel.h> 65bf11b014Sjason #include <sys/device.h> 66bf11b014Sjason #include <sys/socket.h> 67b2e83a74Sjason #include <sys/errno.h> 68bf11b014Sjason 69bf11b014Sjason #include <net/if.h> 70bf11b014Sjason #include <net/if_media.h> 71bf11b014Sjason 72bf11b014Sjason #include <dev/mii/mii.h> 73bf11b014Sjason #include <dev/mii/miivar.h> 74bf11b014Sjason 75c4071fd1Smillert int ukphymatch(struct device *, void *, void *); 76c4071fd1Smillert void ukphyattach(struct device *, struct device *, void *); 77bf11b014Sjason 78bf11b014Sjason struct cfattach ukphy_ca = { 79*fa9fb3edSderaadt sizeof(struct mii_softc), ukphymatch, ukphyattach, mii_phy_detach 80bf11b014Sjason }; 81bf11b014Sjason 82bf11b014Sjason struct cfdriver ukphy_cd = { 83bf11b014Sjason NULL, "ukphy", DV_DULL 84bf11b014Sjason }; 85bf11b014Sjason 86c4071fd1Smillert int ukphy_service(struct mii_softc *, struct mii_data *, int); 87bf11b014Sjason 8878a92591Sbrad const struct mii_phy_funcs ukphy_funcs = { 8978a92591Sbrad ukphy_service, ukphy_status, mii_phy_reset, 9078a92591Sbrad }; 9178a92591Sbrad 92bf11b014Sjason int 93c43900ffSbrad ukphymatch(struct device *parent, void *match, void *aux) 94bf11b014Sjason { 95bf11b014Sjason 96bf11b014Sjason /* 97bf11b014Sjason * We know something is here, so always match at a low priority. 98bf11b014Sjason */ 99bf11b014Sjason return (1); 100bf11b014Sjason } 101bf11b014Sjason 102bf11b014Sjason void 103c43900ffSbrad ukphyattach(struct device *parent, struct device *self, void *aux) 104bf11b014Sjason { 105bf11b014Sjason struct mii_softc *sc = (struct mii_softc *)self; 106bf11b014Sjason struct mii_attach_args *ma = aux; 107bf11b014Sjason struct mii_data *mii = ma->mii_data; 108bf11b014Sjason 10969feb0e2Sbrad printf(": Generic IEEE 802.3u media interface, rev. %d:", 11069feb0e2Sbrad MII_REV(ma->mii_id2)); 1119b9295b8Smaja printf(" OUI 0x%06x, model 0x%04x\n", 11269feb0e2Sbrad MII_OUI(ma->mii_id1, ma->mii_id2), MII_MODEL(ma->mii_id2)); 113bf11b014Sjason 114bf11b014Sjason sc->mii_inst = mii->mii_instance; 115bf11b014Sjason sc->mii_phy = ma->mii_phyno; 11678a92591Sbrad sc->mii_funcs = &ukphy_funcs; 117bf11b014Sjason sc->mii_pdata = mii; 118d240c9bfSbrad sc->mii_flags = ma->mii_flags; 1195d6e3d42Snate 1205d6e3d42Snate /* 1215d6e3d42Snate * Don't do loopback on unknown PHYs. It might confuse some of them. 1225d6e3d42Snate */ 1235d6e3d42Snate sc->mii_flags |= MIIF_NOLOOP; 124bf11b014Sjason 12578a92591Sbrad PHY_RESET(sc); 126bf11b014Sjason 127bf11b014Sjason sc->mii_capabilities = 128bf11b014Sjason PHY_READ(sc, MII_BMSR) & ma->mii_capmask; 129ebf88804Snate if (sc->mii_capabilities & BMSR_EXTSTAT) 130ebf88804Snate sc->mii_extcapabilities = PHY_READ(sc, MII_EXTSR); 131ebf88804Snate if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0 && 132ebf88804Snate (sc->mii_extcapabilities & EXTSR_MEDIAMASK) == 0) 133344b3c31Sjason printf("%s: no media present\n", sc->mii_dev.dv_xname); 1345d6e3d42Snate else 1355d6e3d42Snate mii_phy_add_media(sc); 136bf11b014Sjason } 137bf11b014Sjason 138bf11b014Sjason int 139c43900ffSbrad ukphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd) 140bf11b014Sjason { 141bf11b014Sjason struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 142bf11b014Sjason int reg; 143bf11b014Sjason 1445d6e3d42Snate if ((sc->mii_dev.dv_flags & DVF_ACTIVE) == 0) 1455d6e3d42Snate return (ENXIO); 1465d6e3d42Snate 147bf11b014Sjason switch (cmd) { 148bf11b014Sjason case MII_POLLSTAT: 149bf11b014Sjason /* 150bf11b014Sjason * If we're not polling our PHY instance, just return. 151bf11b014Sjason */ 152bf11b014Sjason if (IFM_INST(ife->ifm_media) != sc->mii_inst) 153bf11b014Sjason return (0); 154bf11b014Sjason break; 155bf11b014Sjason 156bf11b014Sjason case MII_MEDIACHG: 157bf11b014Sjason /* 158bf11b014Sjason * If the media indicates a different PHY instance, 159bf11b014Sjason * isolate ourselves. 160bf11b014Sjason */ 161bf11b014Sjason if (IFM_INST(ife->ifm_media) != sc->mii_inst) { 162bf11b014Sjason reg = PHY_READ(sc, MII_BMCR); 163bf11b014Sjason PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO); 164bf11b014Sjason return (0); 165bf11b014Sjason } 166bf11b014Sjason 167bf11b014Sjason /* 168bf11b014Sjason * If the interface is not up, don't do anything. 169bf11b014Sjason */ 170bf11b014Sjason if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 171bf11b014Sjason break; 172bf11b014Sjason 1735c7e8fc0Sjason mii_phy_setmedia(sc); 174bf11b014Sjason break; 175bf11b014Sjason 176bf11b014Sjason case MII_TICK: 177bf11b014Sjason /* 178bf11b014Sjason * If we're not currently selected, just return. 179bf11b014Sjason */ 180bf11b014Sjason if (IFM_INST(ife->ifm_media) != sc->mii_inst) 181bf11b014Sjason return (0); 182bf11b014Sjason 1835d6e3d42Snate if (mii_phy_tick(sc) == EJUSTRETURN) 184b2e83a74Sjason return (0); 185bf11b014Sjason break; 1865c7e8fc0Sjason 1875c7e8fc0Sjason case MII_DOWN: 1885c7e8fc0Sjason mii_phy_down(sc); 1895c7e8fc0Sjason return (0); 190bf11b014Sjason } 191bf11b014Sjason 192bf11b014Sjason /* Update the media status. */ 1935d6e3d42Snate mii_phy_status(sc); 194bf11b014Sjason 195bf11b014Sjason /* Callback if something changed. */ 1965d6e3d42Snate mii_phy_update(sc, cmd); 197bf11b014Sjason return (0); 198bf11b014Sjason } 199