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