1 /* $OpenBSD: lxtphy.c,v 1.16 2008/06/26 05:42:16 ray Exp $ */ 2 /* $NetBSD: lxtphy.c,v 1.19 2000/02/02 23:34:57 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. 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 Level One's LXT-970/971 ethernet 10/100 PHY 65 * datasheet from www.level1.com 66 */ 67 68 #include <sys/param.h> 69 #include <sys/systm.h> 70 #include <sys/kernel.h> 71 #include <sys/device.h> 72 #include <sys/socket.h> 73 #include <sys/errno.h> 74 75 #include <net/if.h> 76 #include <net/if_media.h> 77 78 #include <dev/mii/mii.h> 79 #include <dev/mii/miivar.h> 80 #include <dev/mii/miidevs.h> 81 82 #include <dev/mii/lxtphyreg.h> 83 84 int lxtphymatch(struct device *, void *, void *); 85 void lxtphyattach(struct device *, struct device *, void *); 86 87 struct cfattach lxtphy_ca = { 88 sizeof(struct mii_softc), lxtphymatch, lxtphyattach, mii_phy_detach, 89 mii_phy_activate 90 }; 91 92 struct cfdriver lxtphy_cd = { 93 NULL, "lxtphy", DV_DULL 94 }; 95 96 int lxtphy_service(struct mii_softc *, struct mii_data *, int); 97 void lxtphy_status(struct mii_softc *); 98 void lxtphy_reset(struct mii_softc *); 99 100 const struct mii_phy_funcs lxtphy_funcs = { 101 lxtphy_service, lxtphy_status, lxtphy_reset, 102 }; 103 104 const struct mii_phy_funcs lxtphy971_funcs = { 105 lxtphy_service, ukphy_status, lxtphy_reset, 106 }; 107 108 static const struct mii_phydesc lxtphys[] = { 109 { MII_OUI_xxLEVEL1, MII_MODEL_xxLEVEL1_LXT970, 110 MII_STR_xxLEVEL1_LXT970 }, 111 { MII_OUI_xxLEVEL1a, MII_MODEL_xxLEVEL1a_LXT971, 112 MII_STR_xxLEVEL1a_LXT971 }, 113 114 { 0, 0, 115 NULL }, 116 }; 117 118 int 119 lxtphymatch(parent, match, aux) 120 struct device *parent; 121 void *match; 122 void *aux; 123 { 124 struct mii_attach_args *ma = aux; 125 126 if (mii_phy_match(ma, lxtphys) != NULL) 127 return (10); 128 129 return (0); 130 } 131 132 void 133 lxtphyattach(parent, self, aux) 134 struct device *parent, *self; 135 void *aux; 136 { 137 struct mii_softc *sc = (struct mii_softc *)self; 138 struct mii_attach_args *ma = aux; 139 struct mii_data *mii = ma->mii_data; 140 const struct mii_phydesc *mpd; 141 142 if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_xxLEVEL1 && 143 MII_MODEL(ma->mii_id2) == MII_MODEL_xxLEVEL1_LXT970) { 144 sc->mii_funcs = &lxtphy_funcs; 145 } 146 if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_xxLEVEL1a && 147 MII_MODEL(ma->mii_id2) == MII_MODEL_xxLEVEL1a_LXT971) { 148 sc->mii_funcs = &lxtphy971_funcs; 149 } 150 151 mpd = mii_phy_match(ma, lxtphys); 152 printf(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2)); 153 154 sc->mii_inst = mii->mii_instance; 155 sc->mii_phy = ma->mii_phyno; 156 sc->mii_pdata = mii; 157 sc->mii_flags = ma->mii_flags; 158 159 PHY_RESET(sc); 160 161 sc->mii_capabilities = 162 PHY_READ(sc, MII_BMSR) & ma->mii_capmask; 163 if (sc->mii_capabilities & BMSR_MEDIAMASK) 164 mii_phy_add_media(sc); 165 } 166 167 int 168 lxtphy_service(sc, mii, cmd) 169 struct mii_softc *sc; 170 struct mii_data *mii; 171 int cmd; 172 { 173 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 174 int reg; 175 176 if ((sc->mii_dev.dv_flags & DVF_ACTIVE) == 0) 177 return (ENXIO); 178 179 switch (cmd) { 180 case MII_POLLSTAT: 181 /* 182 * If we're not polling our PHY instance, just return. 183 */ 184 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 185 return (0); 186 break; 187 188 case MII_MEDIACHG: 189 /* 190 * If the media indicates a different PHY instance, 191 * isolate ourselves. 192 */ 193 if (IFM_INST(ife->ifm_media) != sc->mii_inst) { 194 reg = PHY_READ(sc, MII_BMCR); 195 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO); 196 return (0); 197 } 198 199 /* 200 * If the interface is not up, don't do anything. 201 */ 202 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 203 break; 204 205 mii_phy_setmedia(sc); 206 break; 207 208 case MII_TICK: 209 /* 210 * If we're not currently selected, just return. 211 */ 212 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 213 return (0); 214 215 if (mii_phy_tick(sc) == EJUSTRETURN) 216 return (0); 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 lxtphy_status(sc) 234 struct mii_softc *sc; 235 { 236 struct mii_data *mii = sc->mii_pdata; 237 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 238 int bmcr, bmsr, csr; 239 240 mii->mii_media_status = IFM_AVALID; 241 mii->mii_media_active = IFM_ETHER; 242 243 /* 244 * Get link status from the CSR; we need to read the CSR 245 * for media type anyhow, and the link status in the CSR 246 * doens't latch, so fewer register reads are required. 247 */ 248 csr = PHY_READ(sc, MII_LXTPHY_CSR); 249 if (csr & CSR_LINK) 250 mii->mii_media_status |= IFM_ACTIVE; 251 252 bmcr = PHY_READ(sc, MII_BMCR); 253 if (bmcr & BMCR_ISO) { 254 mii->mii_media_active |= IFM_NONE; 255 mii->mii_media_status = 0; 256 return; 257 } 258 259 if (bmcr & BMCR_LOOP) 260 mii->mii_media_active |= IFM_LOOP; 261 262 if (bmcr & BMCR_AUTOEN) { 263 bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR); 264 if ((bmsr & BMSR_ACOMP) == 0) { 265 /* Erg, still trying, I guess... */ 266 mii->mii_media_active |= IFM_NONE; 267 return; 268 } 269 if (csr & CSR_SPEED) 270 mii->mii_media_active |= IFM_100_TX; 271 else 272 mii->mii_media_active |= IFM_10_T; 273 274 if (csr & CSR_DUPLEX) 275 mii->mii_media_active |= IFM_FDX; 276 else 277 mii->mii_media_active |= IFM_HDX; 278 } else 279 mii->mii_media_active = ife->ifm_media; 280 } 281 282 void 283 lxtphy_reset(sc) 284 struct mii_softc *sc; 285 { 286 mii_phy_reset(sc); 287 PHY_WRITE(sc, MII_LXTPHY_IER, 288 PHY_READ(sc, MII_LXTPHY_IER) & ~IER_INTEN); 289 } 290