1 /* $OpenBSD: bmtphy.c,v 1.3 2001/06/18 06:31:59 deraadt Exp $ */ 2 /* $NetBSD: nsphy.c,v 1.25 2000/02/02 23:34:57 thorpej Exp $ */ 3 4 /*- 5 * Copyright (c) 2001 Theo de Raadt 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 /* 30 * driver for Broadcom BCM5201/5202 Mini-Theta ethernet 10/100 PHY 31 * Data Sheet available from Broadcom 32 */ 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/kernel.h> 37 #include <sys/device.h> 38 #include <sys/malloc.h> 39 #include <sys/socket.h> 40 #include <sys/errno.h> 41 42 #include <net/if.h> 43 #include <net/if_media.h> 44 45 #include <dev/mii/mii.h> 46 #include <dev/mii/miivar.h> 47 #include <dev/mii/miidevs.h> 48 49 #include <dev/mii/bmtphyreg.h> 50 51 int bmtphymatch __P((struct device *, void *, void *)); 52 void bmtphyattach __P((struct device *, struct device *, void *)); 53 54 struct cfattach bmtphy_ca = { 55 sizeof(struct mii_softc), bmtphymatch, bmtphyattach, mii_phy_detach, 56 mii_phy_activate 57 }; 58 59 struct cfdriver bmtphy_cd = { 60 NULL, "bmtphy", DV_DULL 61 }; 62 63 int bmtphy_service __P((struct mii_softc *, struct mii_data *, int)); 64 void bmtphy_status __P((struct mii_softc *)); 65 void bmtphy_reset __P((struct mii_softc *)); 66 67 int 68 bmtphymatch(parent, match, aux) 69 struct device *parent; 70 void *match; 71 void *aux; 72 { 73 struct mii_attach_args *ma = aux; 74 75 if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_BROADCOM && 76 MII_MODEL(ma->mii_id2) == MII_MODEL_BROADCOM_BCM5201) 77 return (10); 78 if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_BROADCOM && 79 MII_MODEL(ma->mii_id2) == MII_MODEL_BROADCOM_BCM5221) 80 return (10); 81 82 return (0); 83 } 84 85 void 86 bmtphyattach(parent, self, aux) 87 struct device *parent; 88 struct device *self; 89 void *aux; 90 { 91 struct mii_softc *sc = (struct mii_softc *)self; 92 struct mii_attach_args *ma = aux; 93 struct mii_data *mii = ma->mii_data; 94 char *model; 95 96 if (MII_MODEL(ma->mii_id2) == MII_MODEL_BROADCOM_BCM5201) 97 model = MII_STR_BROADCOM_BCM5201; 98 else if (MII_MODEL(ma->mii_id2) == MII_MODEL_BROADCOM_BCM5221) 99 model = MII_STR_BROADCOM_BCM5221; 100 101 printf(": %s, rev. %d\n", model, MII_REV(ma->mii_id2)); 102 103 sc->mii_inst = mii->mii_instance; 104 sc->mii_phy = ma->mii_phyno; 105 sc->mii_service = bmtphy_service; 106 sc->mii_status = bmtphy_status; 107 sc->mii_pdata = mii; 108 sc->mii_flags = mii->mii_flags; 109 110 bmtphy_reset(sc); 111 112 sc->mii_capabilities = 113 PHY_READ(sc, MII_BMSR) & ma->mii_capmask; 114 if (sc->mii_capabilities & BMSR_MEDIAMASK) 115 mii_phy_add_media(sc); 116 } 117 118 int 119 bmtphy_service(sc, mii, cmd) 120 struct mii_softc *sc; 121 struct mii_data *mii; 122 int cmd; 123 { 124 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 125 int reg; 126 127 if ((sc->mii_dev.dv_flags & DVF_ACTIVE) == 0) 128 return (ENXIO); 129 130 switch (cmd) { 131 case MII_POLLSTAT: 132 /* 133 * If we're not polling our PHY instance, just return. 134 */ 135 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 136 return (0); 137 break; 138 139 case MII_MEDIACHG: 140 /* 141 * If the media indicates a different PHY instance, 142 * isolate ourselves. 143 */ 144 if (IFM_INST(ife->ifm_media) != sc->mii_inst) { 145 reg = PHY_READ(sc, MII_BMCR); 146 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO); 147 return (0); 148 } 149 150 /* 151 * If the interface is not up, don't do anything. 152 */ 153 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 154 break; 155 156 mii_phy_setmedia(sc); 157 break; 158 159 case MII_TICK: 160 /* 161 * If we're not currently selected, just return. 162 */ 163 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 164 return (0); 165 166 /* 167 * Only used for autonegotiation. 168 */ 169 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) 170 return (0); 171 172 /* 173 * Is the interface even up? 174 */ 175 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 176 return (0); 177 178 /* 179 * Check to see if we have link. If we do, we don't 180 * need to restart the autonegotiation process. Read 181 * the BMSR twice in case it's latched. 182 */ 183 reg = PHY_READ(sc, MII_BMSR) | 184 PHY_READ(sc, MII_BMSR); 185 if (reg & BMSR_LINK) 186 return (0); 187 188 /* 189 * Only retry autonegotiation every 5 seconds. 190 */ 191 if (++sc->mii_ticks != 5) 192 return (0); 193 194 sc->mii_ticks = 0; 195 bmtphy_reset(sc); 196 if (mii_phy_auto(sc, 0) == EJUSTRETURN) 197 return (0); 198 break; 199 200 case MII_DOWN: 201 mii_phy_down(sc); 202 return (0); 203 } 204 205 /* Update the media status. */ 206 mii_phy_status(sc); 207 208 /* Callback if something changed. */ 209 mii_phy_update(sc, cmd); 210 return (0); 211 } 212 213 void 214 bmtphy_status(sc) 215 struct mii_softc *sc; 216 { 217 struct mii_data *mii = sc->mii_pdata; 218 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 219 int bmsr, bmcr, auxc; 220 221 mii->mii_media_status = IFM_AVALID; 222 mii->mii_media_active = IFM_ETHER; 223 224 bmsr = PHY_READ(sc, MII_BMSR) | 225 PHY_READ(sc, MII_BMSR); 226 if (bmsr & BMSR_LINK) 227 mii->mii_media_status |= IFM_ACTIVE; 228 229 bmcr = PHY_READ(sc, MII_BMCR); 230 if (bmcr & BMCR_ISO) { 231 mii->mii_media_active |= IFM_NONE; 232 mii->mii_media_status = 0; 233 return; 234 } 235 236 if (bmcr & BMCR_LOOP) 237 mii->mii_media_active |= IFM_LOOP; 238 239 if (bmcr & BMCR_AUTOEN) { 240 /* 241 * The later are only valid if autonegotiation 242 * has completed (or it's disabled). 243 */ 244 if ((bmsr & BMSR_ACOMP) == 0) { 245 /* Erg, still trying, I guess... */ 246 mii->mii_media_active |= IFM_NONE; 247 return; 248 } 249 250 auxc = PHY_READ(sc, MII_BMTPHY_AUXC); 251 if (auxc & AUXC_SP100) 252 mii->mii_media_active |= IFM_100_TX; 253 else 254 mii->mii_media_active |= IFM_10_T; 255 if (auxc & AUXC_FDX) 256 mii->mii_media_active |= IFM_FDX; 257 258 } else 259 mii->mii_media_active = ife->ifm_media; 260 } 261 262 void 263 bmtphy_reset(sc) 264 struct mii_softc *sc; 265 { 266 int anar; 267 268 mii_phy_reset(sc); 269 270 anar = PHY_READ(sc, MII_ANAR); 271 anar |= BMSR_MEDIA_TO_ANAR(PHY_READ(sc, MII_BMSR)); 272 PHY_WRITE(sc, MII_ANAR, anar); 273 274 /* Chip resets with FDX bit not set */ 275 PHY_WRITE(sc, MII_BMCR, PHY_READ(sc, MII_BMCR) | 276 BMCR_S100|BMCR_AUTOEN|BMCR_STARTNEG|BMCR_FDX); 277 } 278