1*5d6e3d42Snate /* $OpenBSD: ukphy.c,v 1.7 2000/08/26 20:04:18 nate Exp $ */ 2*5d6e3d42Snate /* $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 * 3. All advertising materials mentioning features or use of this software 21bf11b014Sjason * must display the following acknowledgement: 22bf11b014Sjason * This product includes software developed by the NetBSD 23bf11b014Sjason * Foundation, Inc. and its contributors. 24bf11b014Sjason * 4. Neither the name of The NetBSD Foundation nor the names of its 25bf11b014Sjason * contributors may be used to endorse or promote products derived 26bf11b014Sjason * from this software without specific prior written permission. 27bf11b014Sjason * 28bf11b014Sjason * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 29bf11b014Sjason * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 30bf11b014Sjason * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 31bf11b014Sjason * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 32bf11b014Sjason * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 33bf11b014Sjason * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 34bf11b014Sjason * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 35bf11b014Sjason * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 36bf11b014Sjason * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 37bf11b014Sjason * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 38bf11b014Sjason * POSSIBILITY OF SUCH DAMAGE. 39bf11b014Sjason */ 40bf11b014Sjason 41bf11b014Sjason /* 42bf11b014Sjason * Copyright (c) 1997 Manuel Bouyer. All rights reserved. 43bf11b014Sjason * 44bf11b014Sjason * Redistribution and use in source and binary forms, with or without 45bf11b014Sjason * modification, are permitted provided that the following conditions 46bf11b014Sjason * are met: 47bf11b014Sjason * 1. Redistributions of source code must retain the above copyright 48bf11b014Sjason * notice, this list of conditions and the following disclaimer. 49bf11b014Sjason * 2. Redistributions in binary form must reproduce the above copyright 50bf11b014Sjason * notice, this list of conditions and the following disclaimer in the 51bf11b014Sjason * documentation and/or other materials provided with the distribution. 52bf11b014Sjason * 3. All advertising materials mentioning features or use of this software 53bf11b014Sjason * must display the following acknowledgement: 54bf11b014Sjason * This product includes software developed by Manuel Bouyer. 55bf11b014Sjason * 4. The name of the author may not be used to endorse or promote products 56bf11b014Sjason * derived from this software without specific prior written permission. 57bf11b014Sjason * 58bf11b014Sjason * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 59bf11b014Sjason * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 60bf11b014Sjason * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 61bf11b014Sjason * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 62bf11b014Sjason * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 63bf11b014Sjason * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 64bf11b014Sjason * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 65bf11b014Sjason * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 66bf11b014Sjason * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 67bf11b014Sjason * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 68bf11b014Sjason */ 69bf11b014Sjason 70bf11b014Sjason /* 71bf11b014Sjason * driver for generic unknown PHYs 72bf11b014Sjason */ 73bf11b014Sjason 74bf11b014Sjason #include <sys/param.h> 75bf11b014Sjason #include <sys/systm.h> 76bf11b014Sjason #include <sys/kernel.h> 77bf11b014Sjason #include <sys/device.h> 78bf11b014Sjason #include <sys/malloc.h> 79bf11b014Sjason #include <sys/socket.h> 80b2e83a74Sjason #include <sys/errno.h> 81bf11b014Sjason 82bf11b014Sjason #include <net/if.h> 83bf11b014Sjason #include <net/if_media.h> 84bf11b014Sjason 85bf11b014Sjason #include <dev/mii/mii.h> 86bf11b014Sjason #include <dev/mii/miivar.h> 87bf11b014Sjason 88bf11b014Sjason int ukphymatch __P((struct device *, void *, void *)); 89bf11b014Sjason void ukphyattach __P((struct device *, struct device *, void *)); 90bf11b014Sjason 91bf11b014Sjason struct cfattach ukphy_ca = { 92*5d6e3d42Snate sizeof(struct mii_softc), ukphymatch, ukphyattach, mii_phy_detach, 93*5d6e3d42Snate mii_phy_activate 94bf11b014Sjason }; 95bf11b014Sjason 96bf11b014Sjason struct cfdriver ukphy_cd = { 97bf11b014Sjason NULL, "ukphy", DV_DULL 98bf11b014Sjason }; 99bf11b014Sjason 100bf11b014Sjason int ukphy_service __P((struct mii_softc *, struct mii_data *, int)); 101bf11b014Sjason 102bf11b014Sjason int 103bf11b014Sjason ukphymatch(parent, match, aux) 104bf11b014Sjason struct device *parent; 105bf11b014Sjason void *match; 106bf11b014Sjason void *aux; 107bf11b014Sjason { 108bf11b014Sjason 109bf11b014Sjason /* 110bf11b014Sjason * We know something is here, so always match at a low priority. 111bf11b014Sjason */ 112bf11b014Sjason return (1); 113bf11b014Sjason } 114bf11b014Sjason 115bf11b014Sjason void 116bf11b014Sjason ukphyattach(parent, self, aux) 117bf11b014Sjason struct device *parent, *self; 118bf11b014Sjason void *aux; 119bf11b014Sjason { 120bf11b014Sjason struct mii_softc *sc = (struct mii_softc *)self; 121bf11b014Sjason struct mii_attach_args *ma = aux; 122bf11b014Sjason struct mii_data *mii = ma->mii_data; 123bf11b014Sjason 124bf11b014Sjason printf(": Generic IEEE 802.3u media interface\n"); 125bf11b014Sjason printf("%s: OUI 0x%06x, model 0x%04x, rev. %d\n", 126bf11b014Sjason sc->mii_dev.dv_xname, MII_OUI(ma->mii_id1, ma->mii_id2), 127bf11b014Sjason MII_MODEL(ma->mii_id2), MII_REV(ma->mii_id2)); 128bf11b014Sjason 129bf11b014Sjason sc->mii_inst = mii->mii_instance; 130bf11b014Sjason sc->mii_phy = ma->mii_phyno; 131bf11b014Sjason sc->mii_service = ukphy_service; 132*5d6e3d42Snate sc->mii_status = ukphy_status; 133bf11b014Sjason sc->mii_pdata = mii; 134*5d6e3d42Snate sc->mii_flags = mii->mii_flags; 135*5d6e3d42Snate 136*5d6e3d42Snate /* 137*5d6e3d42Snate * Don't do loopback on unknown PHYs. It might confuse some of them. 138*5d6e3d42Snate */ 139*5d6e3d42Snate sc->mii_flags |= MIIF_NOLOOP; 140bf11b014Sjason 141bf11b014Sjason mii_phy_reset(sc); 142bf11b014Sjason 143bf11b014Sjason sc->mii_capabilities = 144bf11b014Sjason PHY_READ(sc, MII_BMSR) & ma->mii_capmask; 145*5d6e3d42Snate printf("%s: ", sc->mii_dev.dv_xname); 146*5d6e3d42Snate if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0) 147*5d6e3d42Snate printf("no media present"); 148*5d6e3d42Snate else 149*5d6e3d42Snate mii_phy_add_media(sc); 150*5d6e3d42Snate printf("\n"); 151bf11b014Sjason } 152bf11b014Sjason 153bf11b014Sjason int 154bf11b014Sjason ukphy_service(sc, mii, cmd) 155bf11b014Sjason struct mii_softc *sc; 156bf11b014Sjason struct mii_data *mii; 157bf11b014Sjason int cmd; 158bf11b014Sjason { 159bf11b014Sjason struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 160bf11b014Sjason int reg; 161bf11b014Sjason 162*5d6e3d42Snate if ((sc->mii_dev.dv_flags & DVF_ACTIVE) == 0) 163*5d6e3d42Snate return (ENXIO); 164*5d6e3d42Snate 165bf11b014Sjason switch (cmd) { 166bf11b014Sjason case MII_POLLSTAT: 167bf11b014Sjason /* 168bf11b014Sjason * If we're not polling our PHY instance, just return. 169bf11b014Sjason */ 170bf11b014Sjason if (IFM_INST(ife->ifm_media) != sc->mii_inst) 171bf11b014Sjason return (0); 172bf11b014Sjason break; 173bf11b014Sjason 174bf11b014Sjason case MII_MEDIACHG: 175bf11b014Sjason /* 176bf11b014Sjason * If the media indicates a different PHY instance, 177bf11b014Sjason * isolate ourselves. 178bf11b014Sjason */ 179bf11b014Sjason if (IFM_INST(ife->ifm_media) != sc->mii_inst) { 180bf11b014Sjason reg = PHY_READ(sc, MII_BMCR); 181bf11b014Sjason PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO); 182bf11b014Sjason return (0); 183bf11b014Sjason } 184bf11b014Sjason 185bf11b014Sjason /* 186bf11b014Sjason * If the interface is not up, don't do anything. 187bf11b014Sjason */ 188bf11b014Sjason if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 189bf11b014Sjason break; 190bf11b014Sjason 1915c7e8fc0Sjason mii_phy_setmedia(sc); 192bf11b014Sjason break; 193bf11b014Sjason 194bf11b014Sjason case MII_TICK: 195bf11b014Sjason /* 196bf11b014Sjason * If we're not currently selected, just return. 197bf11b014Sjason */ 198bf11b014Sjason if (IFM_INST(ife->ifm_media) != sc->mii_inst) 199bf11b014Sjason return (0); 200bf11b014Sjason 201bf11b014Sjason /* 202bf11b014Sjason * Only used for autonegotiation. 203bf11b014Sjason */ 204bf11b014Sjason if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) 205bf11b014Sjason return (0); 206bf11b014Sjason 207*5d6e3d42Snate if (mii_phy_tick(sc) == EJUSTRETURN) 208b2e83a74Sjason return (0); 209bf11b014Sjason break; 2105c7e8fc0Sjason 2115c7e8fc0Sjason case MII_DOWN: 2125c7e8fc0Sjason mii_phy_down(sc); 2135c7e8fc0Sjason return (0); 214bf11b014Sjason } 215bf11b014Sjason 216bf11b014Sjason /* Update the media status. */ 217*5d6e3d42Snate mii_phy_status(sc); 218bf11b014Sjason 219bf11b014Sjason /* Callback if something changed. */ 220*5d6e3d42Snate mii_phy_update(sc, cmd); 221bf11b014Sjason return (0); 222bf11b014Sjason } 223