1 /* $OpenBSD: lxtphy.c,v 1.17 2010/07/23 07:47:13 jsg 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 * 46 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 47 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 48 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 49 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 50 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 51 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 52 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 53 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 54 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 55 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 56 */ 57 58 /* 59 * driver for Level One's LXT-970/971 ethernet 10/100 PHY 60 * datasheet from www.level1.com 61 */ 62 63 #include <sys/param.h> 64 #include <sys/systm.h> 65 #include <sys/kernel.h> 66 #include <sys/device.h> 67 #include <sys/socket.h> 68 #include <sys/errno.h> 69 70 #include <net/if.h> 71 #include <net/if_media.h> 72 73 #include <dev/mii/mii.h> 74 #include <dev/mii/miivar.h> 75 #include <dev/mii/miidevs.h> 76 77 #include <dev/mii/lxtphyreg.h> 78 79 int lxtphymatch(struct device *, void *, void *); 80 void lxtphyattach(struct device *, struct device *, void *); 81 82 struct cfattach lxtphy_ca = { 83 sizeof(struct mii_softc), lxtphymatch, lxtphyattach, mii_phy_detach, 84 mii_phy_activate 85 }; 86 87 struct cfdriver lxtphy_cd = { 88 NULL, "lxtphy", DV_DULL 89 }; 90 91 int lxtphy_service(struct mii_softc *, struct mii_data *, int); 92 void lxtphy_status(struct mii_softc *); 93 void lxtphy_reset(struct mii_softc *); 94 95 const struct mii_phy_funcs lxtphy_funcs = { 96 lxtphy_service, lxtphy_status, lxtphy_reset, 97 }; 98 99 const struct mii_phy_funcs lxtphy971_funcs = { 100 lxtphy_service, ukphy_status, lxtphy_reset, 101 }; 102 103 static const struct mii_phydesc lxtphys[] = { 104 { MII_OUI_xxLEVEL1, MII_MODEL_xxLEVEL1_LXT970, 105 MII_STR_xxLEVEL1_LXT970 }, 106 { MII_OUI_xxLEVEL1a, MII_MODEL_xxLEVEL1a_LXT971, 107 MII_STR_xxLEVEL1a_LXT971 }, 108 109 { 0, 0, 110 NULL }, 111 }; 112 113 int 114 lxtphymatch(parent, match, aux) 115 struct device *parent; 116 void *match; 117 void *aux; 118 { 119 struct mii_attach_args *ma = aux; 120 121 if (mii_phy_match(ma, lxtphys) != NULL) 122 return (10); 123 124 return (0); 125 } 126 127 void 128 lxtphyattach(parent, self, aux) 129 struct device *parent, *self; 130 void *aux; 131 { 132 struct mii_softc *sc = (struct mii_softc *)self; 133 struct mii_attach_args *ma = aux; 134 struct mii_data *mii = ma->mii_data; 135 const struct mii_phydesc *mpd; 136 137 if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_xxLEVEL1 && 138 MII_MODEL(ma->mii_id2) == MII_MODEL_xxLEVEL1_LXT970) { 139 sc->mii_funcs = &lxtphy_funcs; 140 } 141 if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_xxLEVEL1a && 142 MII_MODEL(ma->mii_id2) == MII_MODEL_xxLEVEL1a_LXT971) { 143 sc->mii_funcs = &lxtphy971_funcs; 144 } 145 146 mpd = mii_phy_match(ma, lxtphys); 147 printf(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2)); 148 149 sc->mii_inst = mii->mii_instance; 150 sc->mii_phy = ma->mii_phyno; 151 sc->mii_pdata = mii; 152 sc->mii_flags = ma->mii_flags; 153 154 PHY_RESET(sc); 155 156 sc->mii_capabilities = 157 PHY_READ(sc, MII_BMSR) & ma->mii_capmask; 158 if (sc->mii_capabilities & BMSR_MEDIAMASK) 159 mii_phy_add_media(sc); 160 } 161 162 int 163 lxtphy_service(sc, mii, cmd) 164 struct mii_softc *sc; 165 struct mii_data *mii; 166 int cmd; 167 { 168 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 169 int reg; 170 171 if ((sc->mii_dev.dv_flags & DVF_ACTIVE) == 0) 172 return (ENXIO); 173 174 switch (cmd) { 175 case MII_POLLSTAT: 176 /* 177 * If we're not polling our PHY instance, just return. 178 */ 179 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 180 return (0); 181 break; 182 183 case MII_MEDIACHG: 184 /* 185 * If the media indicates a different PHY instance, 186 * isolate ourselves. 187 */ 188 if (IFM_INST(ife->ifm_media) != sc->mii_inst) { 189 reg = PHY_READ(sc, MII_BMCR); 190 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO); 191 return (0); 192 } 193 194 /* 195 * If the interface is not up, don't do anything. 196 */ 197 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 198 break; 199 200 mii_phy_setmedia(sc); 201 break; 202 203 case MII_TICK: 204 /* 205 * If we're not currently selected, just return. 206 */ 207 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 208 return (0); 209 210 if (mii_phy_tick(sc) == EJUSTRETURN) 211 return (0); 212 break; 213 214 case MII_DOWN: 215 mii_phy_down(sc); 216 return (0); 217 } 218 219 /* Update the media status. */ 220 mii_phy_status(sc); 221 222 /* Callback if something changed. */ 223 mii_phy_update(sc, cmd); 224 return (0); 225 } 226 227 void 228 lxtphy_status(sc) 229 struct mii_softc *sc; 230 { 231 struct mii_data *mii = sc->mii_pdata; 232 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 233 int bmcr, bmsr, csr; 234 235 mii->mii_media_status = IFM_AVALID; 236 mii->mii_media_active = IFM_ETHER; 237 238 /* 239 * Get link status from the CSR; we need to read the CSR 240 * for media type anyhow, and the link status in the CSR 241 * doens't latch, so fewer register reads are required. 242 */ 243 csr = PHY_READ(sc, MII_LXTPHY_CSR); 244 if (csr & CSR_LINK) 245 mii->mii_media_status |= IFM_ACTIVE; 246 247 bmcr = PHY_READ(sc, MII_BMCR); 248 if (bmcr & BMCR_ISO) { 249 mii->mii_media_active |= IFM_NONE; 250 mii->mii_media_status = 0; 251 return; 252 } 253 254 if (bmcr & BMCR_LOOP) 255 mii->mii_media_active |= IFM_LOOP; 256 257 if (bmcr & BMCR_AUTOEN) { 258 bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR); 259 if ((bmsr & BMSR_ACOMP) == 0) { 260 /* Erg, still trying, I guess... */ 261 mii->mii_media_active |= IFM_NONE; 262 return; 263 } 264 if (csr & CSR_SPEED) 265 mii->mii_media_active |= IFM_100_TX; 266 else 267 mii->mii_media_active |= IFM_10_T; 268 269 if (csr & CSR_DUPLEX) 270 mii->mii_media_active |= IFM_FDX; 271 else 272 mii->mii_media_active |= IFM_HDX; 273 } else 274 mii->mii_media_active = ife->ifm_media; 275 } 276 277 void 278 lxtphy_reset(sc) 279 struct mii_softc *sc; 280 { 281 mii_phy_reset(sc); 282 PHY_WRITE(sc, MII_LXTPHY_IER, 283 PHY_READ(sc, MII_LXTPHY_IER) & ~IER_INTEN); 284 } 285