1 /* $OpenBSD: exphy.c,v 1.19 2008/06/26 05:42:16 ray Exp $ */ 2 /* $NetBSD: exphy.c,v 1.23 2000/02/02 23:34:56 thorpej Exp $ */ 3 4 /*- 5 * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 10 * NASA Ames Research Center, and by Frank van der Linden. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 * POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 /* 35 * Copyright (c) 1997 Manuel Bouyer. All rights reserved. 36 * 37 * Redistribution and use in source and binary forms, with or without 38 * modification, are permitted provided that the following conditions 39 * are met: 40 * 1. Redistributions of source code must retain the above copyright 41 * notice, this list of conditions and the following disclaimer. 42 * 2. Redistributions in binary form must reproduce the above copyright 43 * notice, this list of conditions and the following disclaimer in the 44 * documentation and/or other materials provided with the distribution. 45 * 3. All advertising materials mentioning features or use of this software 46 * must display the following acknowledgement: 47 * This product includes software developed by Manuel Bouyer. 48 * 4. The name of the author may not be used to endorse or promote products 49 * derived from this software without specific prior written permission. 50 * 51 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 52 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 53 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 54 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 55 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 56 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 57 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 58 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 59 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 60 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 61 */ 62 63 /* 64 * driver for 3Com internal PHYs 65 */ 66 67 #include <sys/param.h> 68 #include <sys/systm.h> 69 #include <sys/kernel.h> 70 #include <sys/device.h> 71 #include <sys/socket.h> 72 73 #include <net/if.h> 74 #include <net/if_media.h> 75 76 #include <dev/mii/mii.h> 77 #include <dev/mii/miivar.h> 78 #include <dev/mii/miidevs.h> 79 80 int exphymatch(struct device *, void *, void *); 81 void exphyattach(struct device *, struct device *, void *); 82 83 struct cfattach exphy_ca = { 84 sizeof(struct mii_softc), exphymatch, exphyattach, mii_phy_detach, 85 mii_phy_activate 86 }; 87 88 struct cfdriver exphy_cd = { 89 NULL, "exphy", DV_DULL 90 }; 91 92 int exphy_service(struct mii_softc *, struct mii_data *, int); 93 void exphy_reset(struct mii_softc *); 94 95 const struct mii_phy_funcs exphy_funcs = { 96 exphy_service, ukphy_status, exphy_reset, 97 }; 98 99 int 100 exphymatch(struct device *parent, void *match, void *aux) 101 { 102 struct mii_attach_args *ma = aux; 103 104 /* 105 * Since 3com's PHY for some xl adapters is braindead and doesn't 106 * report the proper OUI/MODEL information, we have this stupid 107 * match function. 108 */ 109 if ((strcmp(parent->dv_cfdata->cf_driver->cd_name, "xl") == 0) && 110 ((MII_OUI(ma->mii_id1, ma->mii_id2) == 0 && 111 MII_MODEL(ma->mii_id2) == 0) || 112 (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_3COM && 113 MII_MODEL(ma->mii_id2) == 0))) 114 return (10); 115 116 return (0); 117 } 118 119 void 120 exphyattach(struct device *parent, struct device *self, void *aux) 121 { 122 struct mii_softc *sc = (struct mii_softc *)self; 123 struct mii_attach_args *ma = aux; 124 struct mii_data *mii = ma->mii_data; 125 126 printf(": 3Com internal media interface\n"); 127 128 sc->mii_inst = mii->mii_instance; 129 sc->mii_phy = ma->mii_phyno; 130 sc->mii_funcs = &exphy_funcs; 131 sc->mii_pdata = mii; 132 sc->mii_flags = ma->mii_flags; 133 134 sc->mii_flags |= MIIF_NOISOLATE; 135 136 /* 137 * The 3Com PHY can never be isolated, so never allow non-zero 138 * instances! 139 */ 140 if (mii->mii_instance != 0) { 141 printf("%s: ignoring this PHY, non-zero instance\n", 142 sc->mii_dev.dv_xname); 143 return; 144 } 145 146 PHY_RESET(sc); 147 148 sc->mii_capabilities = 149 PHY_READ(sc, MII_BMSR) & ma->mii_capmask; 150 if (sc->mii_capabilities & BMSR_MEDIAMASK) 151 mii_phy_add_media(sc); 152 } 153 154 int 155 exphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd) 156 { 157 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 158 159 if ((sc->mii_dev.dv_flags & DVF_ACTIVE) == 0) 160 return (ENXIO); 161 162 /* 163 * We can't isolate the 3Com PHY, so it has to be the only one! 164 */ 165 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 166 panic("exphy_service: can't isolate 3Com PHY"); 167 168 switch (cmd) { 169 case MII_POLLSTAT: 170 break; 171 172 case MII_MEDIACHG: 173 /* 174 * If the interface is not up, don't do anything. 175 */ 176 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 177 break; 178 179 mii_phy_setmedia(sc); 180 break; 181 182 case MII_TICK: 183 if (mii_phy_tick(sc) == EJUSTRETURN) 184 return (0); 185 186 break; 187 188 case MII_DOWN: 189 mii_phy_down(sc); 190 return (0); 191 } 192 193 /* Update the media status. */ 194 mii_phy_status(sc); 195 196 /* Callback if something changed. */ 197 mii_phy_update(sc, cmd); 198 return (0); 199 } 200 201 void 202 exphy_reset(struct mii_softc *sc) 203 { 204 205 mii_phy_reset(sc); 206 207 /* 208 * XXX 3Com PHY doesn't set the BMCR properly after 209 * XXX reset, which breaks autonegotiation. 210 */ 211 PHY_WRITE(sc, MII_BMCR, BMCR_S100|BMCR_AUTOEN|BMCR_FDX); 212 } 213