lxtphy.c revision 213364
1/* OpenBSD: lxtphy.c,v 1.5 2000/08/26 20:04:17 nate 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#include <sys/cdefs.h> 59__FBSDID("$FreeBSD: head/sys/dev/mii/lxtphy.c 213364 2010-10-02 18:53:12Z marius $"); 60 61/* 62 * driver for Level One's LXT-970 ethernet 10/100 PHY 63 * datasheet from www.level1.com 64 */ 65 66#include <sys/param.h> 67#include <sys/systm.h> 68#include <sys/kernel.h> 69#include <sys/socket.h> 70#include <sys/errno.h> 71#include <sys/module.h> 72#include <sys/bus.h> 73 74#include <net/if.h> 75#include <net/if_media.h> 76 77#include <dev/mii/mii.h> 78#include <dev/mii/miivar.h> 79#include "miidevs.h" 80 81#include <dev/mii/lxtphyreg.h> 82 83#include "miibus_if.h" 84 85static int lxtphy_probe(device_t); 86static int lxtphy_attach(device_t); 87 88static device_method_t lxtphy_methods[] = { 89 /* device interface */ 90 DEVMETHOD(device_probe, lxtphy_probe), 91 DEVMETHOD(device_attach, lxtphy_attach), 92 DEVMETHOD(device_detach, mii_phy_detach), 93 DEVMETHOD(device_shutdown, bus_generic_shutdown), 94 { 0, 0 } 95}; 96 97static devclass_t lxtphy_devclass; 98 99static driver_t lxtphy_driver = { 100 "lxtphy", 101 lxtphy_methods, 102 sizeof(struct mii_softc) 103}; 104 105DRIVER_MODULE(lxtphy, miibus, lxtphy_driver, lxtphy_devclass, 0, 0); 106 107static int lxtphy_service(struct mii_softc *, struct mii_data *, int); 108static void lxtphy_status(struct mii_softc *); 109static void lxtphy_set_tp(struct mii_softc *); 110static void lxtphy_set_fx(struct mii_softc *); 111 112static const struct mii_phydesc lxtphys[] = { 113 MII_PHY_DESC(xxLEVEL1, LXT970), 114 MII_PHY_END 115}; 116 117static int 118lxtphy_probe(device_t dev) 119{ 120 121 return (mii_phy_dev_probe(dev, lxtphys, BUS_PROBE_DEFAULT)); 122} 123 124static int 125lxtphy_attach(device_t dev) 126{ 127 struct mii_softc *sc; 128 struct mii_attach_args *ma; 129 struct mii_data *mii; 130 const char *nic; 131 132 sc = device_get_softc(dev); 133 ma = device_get_ivars(dev); 134 sc->mii_dev = device_get_parent(dev); 135 mii = ma->mii_data; 136 LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list); 137 138 sc->mii_inst = mii->mii_instance++; 139 sc->mii_phy = ma->mii_phyno; 140 sc->mii_service = lxtphy_service; 141 sc->mii_pdata = mii; 142 143 mii_phy_reset(sc); 144 145 sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & ma->mii_capmask; 146 device_printf(dev, " "); 147 148 /* 149 * On Apple BMAC controllers, we end up in a weird state 150 * of partially-completed autonegotiation on boot. So 151 * force autonegotation to try again. 152 */ 153 nic = device_get_name(device_get_parent(sc->mii_dev)); 154 if (strcmp(nic, "bm") == 0) 155 sc->mii_flags |= MIIF_FORCEANEG | MIIF_NOISOLATE; 156 157#define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL) 158 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_FX, 0, sc->mii_inst), 159 MII_MEDIA_100_TX); 160 printf("100baseFX, "); 161 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_FX, IFM_FDX, sc->mii_inst), 162 MII_MEDIA_100_TX_FDX); 163 printf("100baseFX-FDX, "); 164#undef ADD 165 166 mii_phy_add_media(sc); 167 printf("\n"); 168 169 MIIBUS_MEDIAINIT(sc->mii_dev); 170 return (0); 171} 172 173static int 174lxtphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd) 175{ 176 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 177 178 switch (cmd) { 179 case MII_POLLSTAT: 180 break; 181 182 case MII_MEDIACHG: 183 /* 184 * If the interface is not up, don't do anything. 185 */ 186 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 187 break; 188 189 if (IFM_SUBTYPE(ife->ifm_media) == IFM_100_FX) 190 lxtphy_set_fx(sc); 191 else 192 lxtphy_set_tp(sc); 193 194 mii_phy_setmedia(sc); 195 break; 196 197 case MII_TICK: 198 if (mii_phy_tick(sc) == EJUSTRETURN) 199 return (0); 200 break; 201 } 202 203 /* Update the media status. */ 204 lxtphy_status(sc); 205 206 /* Callback if something changed. */ 207 mii_phy_update(sc, cmd); 208 return (0); 209} 210 211static void 212lxtphy_status(struct mii_softc *sc) 213{ 214 struct mii_data *mii = sc->mii_pdata; 215 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 216 int bmcr, bmsr, csr; 217 218 mii->mii_media_status = IFM_AVALID; 219 mii->mii_media_active = IFM_ETHER; 220 221 /* 222 * Get link status from the CSR; we need to read the CSR 223 * for media type anyhow, and the link status in the CSR 224 * doens't latch, so fewer register reads are required. 225 */ 226 csr = PHY_READ(sc, MII_LXTPHY_CSR); 227 if (csr & CSR_LINK) 228 mii->mii_media_status |= IFM_ACTIVE; 229 230 bmcr = PHY_READ(sc, MII_BMCR); 231 if (bmcr & BMCR_ISO) { 232 mii->mii_media_active |= IFM_NONE; 233 mii->mii_media_status = 0; 234 return; 235 } 236 237 if (bmcr & BMCR_LOOP) 238 mii->mii_media_active |= IFM_LOOP; 239 240 if (bmcr & BMCR_AUTOEN) { 241 bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR); 242 if ((bmsr & BMSR_ACOMP) == 0) { 243 /* Erg, still trying, I guess... */ 244 mii->mii_media_active |= IFM_NONE; 245 return; 246 } 247 if (csr & CSR_SPEED) 248 mii->mii_media_active |= IFM_100_TX; 249 else 250 mii->mii_media_active |= IFM_10_T; 251 if (csr & CSR_DUPLEX) 252 mii->mii_media_active |= IFM_FDX; 253 } else 254 mii->mii_media_active = ife->ifm_media; 255} 256 257static void 258lxtphy_set_tp(struct mii_softc *sc) 259{ 260 int cfg; 261 262 cfg = PHY_READ(sc, MII_LXTPHY_CONFIG); 263 cfg &= ~CONFIG_100BASEFX; 264 PHY_WRITE(sc, MII_LXTPHY_CONFIG, cfg); 265} 266 267static void 268lxtphy_set_fx(struct mii_softc *sc) 269{ 270 int cfg; 271 272 cfg = PHY_READ(sc, MII_LXTPHY_CONFIG); 273 cfg |= CONFIG_100BASEFX; 274 PHY_WRITE(sc, MII_LXTPHY_CONFIG, cfg); 275} 276