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