1 /* $NetBSD: gentbi.c,v 1.15 2006/03/29 07:05:24 thorpej Exp $ */ 2 3 /*- 4 * SPDX-License-Identifier: BSD-2-Clause-NetBSD AND BSD-2-Clause 5 * 6 * Copyright (c) 1998, 1999, 2000, 2001 The NetBSD Foundation, Inc. 7 * All rights reserved. 8 * 9 * This code is derived from software contributed to The NetBSD Foundation 10 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 11 * NASA Ames Research Center. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 * POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 /* 36 * Copyright (c) 1997 Manuel Bouyer. All rights reserved. 37 * 38 * Redistribution and use in source and binary forms, with or without 39 * modification, are permitted provided that the following conditions 40 * are met: 41 * 1. Redistributions of source code must retain the above copyright 42 * notice, this list of conditions and the following disclaimer. 43 * 2. Redistributions in binary form must reproduce the above copyright 44 * notice, this list of conditions and the following disclaimer in the 45 * documentation and/or other materials provided with the distribution. 46 * 47 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 48 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 49 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 50 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 51 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 52 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 53 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 54 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 55 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 56 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 57 */ 58 59 /* 60 * Driver for generic ten-bit (1000BASE-SX) interfaces, built into 61 * many Gigabit Ethernet chips. 62 * 63 * All we have to do here is correctly report speed and duplex. 64 */ 65 66 #include <sys/cdefs.h> 67 __FBSDID("$FreeBSD$"); 68 69 /* 70 * Driver for generic unknown ten-bit interfaces(1000BASE-{LX,SX} 71 * fiber interfaces). 72 */ 73 74 #include <sys/param.h> 75 #include <sys/systm.h> 76 #include <sys/kernel.h> 77 #include <sys/module.h> 78 #include <sys/socket.h> 79 #include <sys/errno.h> 80 #include <sys/bus.h> 81 82 #include <net/if.h> 83 #include <net/if_media.h> 84 85 #include <dev/mii/mii.h> 86 #include <dev/mii/miivar.h> 87 #include "miidevs.h" 88 89 #include "miibus_if.h" 90 91 static int gentbi_probe(device_t); 92 static int gentbi_attach(device_t); 93 94 static device_method_t gentbi_methods[] = { 95 /* device interface */ 96 DEVMETHOD(device_probe, gentbi_probe), 97 DEVMETHOD(device_attach, gentbi_attach), 98 DEVMETHOD(device_detach, mii_phy_detach), 99 DEVMETHOD(device_shutdown, bus_generic_shutdown), 100 DEVMETHOD_END 101 }; 102 103 static driver_t gentbi_driver = { 104 "gentbi", 105 gentbi_methods, 106 sizeof(struct mii_softc) 107 }; 108 109 DRIVER_MODULE(gentbi, miibus, gentbi_driver, 0, 0); 110 111 static int gentbi_service(struct mii_softc *, struct mii_data *, int); 112 static void gentbi_status(struct mii_softc *); 113 114 static const struct mii_phy_funcs gentbi_funcs = { 115 gentbi_service, 116 gentbi_status, 117 mii_phy_reset 118 }; 119 120 static int 121 gentbi_probe(device_t dev) 122 { 123 device_t parent; 124 struct mii_attach_args *ma; 125 int bmsr, extsr; 126 127 parent = device_get_parent(dev); 128 ma = device_get_ivars(dev); 129 130 /* 131 * We match as a generic TBI if: 132 * 133 * - There is no media in the BMSR. 134 * - EXTSR has only 1000X. 135 */ 136 bmsr = MIIBUS_READREG(parent, ma->mii_phyno, MII_BMSR); 137 if ((bmsr & BMSR_EXTSTAT) == 0 || (bmsr & BMSR_MEDIAMASK) != 0) 138 return (ENXIO); 139 140 extsr = MIIBUS_READREG(parent, ma->mii_phyno, MII_EXTSR); 141 if (extsr & (EXTSR_1000TFDX|EXTSR_1000THDX)) 142 return (ENXIO); 143 144 if (extsr & (EXTSR_1000XFDX|EXTSR_1000XHDX)) { 145 /* 146 * We think this is a generic TBI. Return a match 147 * priority higher than ukphy, but lower than what 148 * specific drivers will return. 149 */ 150 device_set_desc(dev, "Generic ten-bit interface"); 151 return (BUS_PROBE_LOW_PRIORITY); 152 } 153 154 return (ENXIO); 155 } 156 157 static int 158 gentbi_attach(device_t dev) 159 { 160 struct mii_softc *sc; 161 162 sc = device_get_softc(dev); 163 164 mii_phy_dev_attach(dev, MIIF_NOMANPAUSE, &gentbi_funcs, 0); 165 166 PHY_RESET(sc); 167 168 /* 169 * Mask out all media in the BMSR. We only are really interested 170 * in "auto". 171 */ 172 sc->mii_capabilities = 173 PHY_READ(sc, MII_BMSR) & sc->mii_capmask & ~BMSR_MEDIAMASK; 174 if (sc->mii_capabilities & BMSR_EXTSTAT) 175 sc->mii_extcapabilities = PHY_READ(sc, MII_EXTSR); 176 177 device_printf(dev, " "); 178 mii_phy_add_media(sc); 179 printf("\n"); 180 181 MIIBUS_MEDIAINIT(sc->mii_dev); 182 return (0); 183 } 184 185 static int 186 gentbi_service(struct mii_softc *sc, struct mii_data *mii, int cmd) 187 { 188 189 switch (cmd) { 190 case MII_POLLSTAT: 191 break; 192 193 case MII_MEDIACHG: 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 PHY_STATUS(sc); 205 206 /* Callback if something changed. */ 207 mii_phy_update(sc, cmd); 208 return (0); 209 } 210 211 static void 212 gentbi_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 bmsr, bmcr, anlpar; 217 218 mii->mii_media_status = IFM_AVALID; 219 mii->mii_media_active = IFM_ETHER; 220 221 bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR); 222 223 if (bmsr & BMSR_LINK) 224 mii->mii_media_status |= IFM_ACTIVE; 225 226 bmcr = PHY_READ(sc, MII_BMCR); 227 if (bmcr & BMCR_ISO) { 228 mii->mii_media_active |= IFM_NONE; 229 mii->mii_media_status = 0; 230 return; 231 } 232 233 if (bmcr & BMCR_LOOP) 234 mii->mii_media_active |= IFM_LOOP; 235 236 if (bmcr & BMCR_AUTOEN) { 237 /* 238 * The media status bits are only valid if autonegotiation 239 * has completed (or it's disabled). 240 */ 241 if ((bmsr & BMSR_ACOMP) == 0) { 242 /* Erg, still trying, I guess... */ 243 mii->mii_media_active |= IFM_NONE; 244 return; 245 } 246 247 /* 248 * The media is always 1000baseSX. Check the ANLPAR to 249 * see if we're doing full-duplex. 250 */ 251 mii->mii_media_active |= IFM_1000_SX; 252 anlpar = PHY_READ(sc, MII_ANLPAR); 253 if ((sc->mii_extcapabilities & EXTSR_1000XFDX) != 0 && 254 (anlpar & ANLPAR_X_FD) != 0) 255 mii->mii_media_active |= 256 IFM_FDX | mii_phy_flowstatus(sc); 257 else 258 mii->mii_media_active |= IFM_HDX; 259 } else 260 mii->mii_media_active = ife->ifm_media; 261 } 262