1 /* 2 * Copyright (c) 1997, 1998, 1999 3 * Bill Paul <wpaul@ee.columbia.edu>. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by Bill Paul. 16 * 4. Neither the name of the author nor the names of any co-contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 30 * THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 /* 34 * Pseudo-driver for media selection on the Lite-On PNIC 82c168 35 * chip. The NWAY support on this chip is horribly broken, so we 36 * only support manual mode selection. This is lame, but getting 37 * NWAY to work right is amazingly difficult. 38 */ 39 40 #include <sys/cdefs.h> 41 __FBSDID("$FreeBSD$"); 42 43 #include <sys/param.h> 44 #include <sys/systm.h> 45 #include <sys/kernel.h> 46 #include <sys/socket.h> 47 #include <sys/errno.h> 48 #include <sys/lock.h> 49 #include <sys/module.h> 50 #include <sys/mutex.h> 51 #include <sys/bus.h> 52 53 #include <net/if.h> 54 #include <net/if_arp.h> 55 #include <net/if_media.h> 56 57 #include <dev/mii/mii.h> 58 #include <dev/mii/miivar.h> 59 #include "miidevs.h" 60 61 #include <machine/bus_pio.h> 62 #include <machine/bus_memio.h> 63 #include <machine/bus.h> 64 #include <machine/resource.h> 65 #include <sys/bus.h> 66 67 #include <pci/if_dcreg.h> 68 69 #include "miibus_if.h" 70 71 #define DC_SETBIT(sc, reg, x) \ 72 CSR_WRITE_4(sc, reg, \ 73 CSR_READ_4(sc, reg) | x) 74 75 #define DC_CLRBIT(sc, reg, x) \ 76 CSR_WRITE_4(sc, reg, \ 77 CSR_READ_4(sc, reg) & ~x) 78 79 static int pnphy_probe(device_t); 80 static int pnphy_attach(device_t); 81 82 static device_method_t pnphy_methods[] = { 83 /* device interface */ 84 DEVMETHOD(device_probe, pnphy_probe), 85 DEVMETHOD(device_attach, pnphy_attach), 86 DEVMETHOD(device_detach, mii_phy_detach), 87 DEVMETHOD(device_shutdown, bus_generic_shutdown), 88 { 0, 0 } 89 }; 90 91 static devclass_t pnphy_devclass; 92 93 static driver_t pnphy_driver = { 94 "pnphy", 95 pnphy_methods, 96 sizeof(struct mii_softc) 97 }; 98 99 DRIVER_MODULE(pnphy, miibus, pnphy_driver, pnphy_devclass, 0, 0); 100 101 static int pnphy_service(struct mii_softc *, struct mii_data *, int); 102 static void pnphy_status(struct mii_softc *); 103 104 static int 105 pnphy_probe(dev) 106 device_t dev; 107 { 108 struct mii_attach_args *ma; 109 110 ma = device_get_ivars(dev); 111 112 /* 113 * The dc driver will report the 82c168 vendor and device 114 * ID to let us know that it wants us to attach. 115 */ 116 if (ma->mii_id1 != DC_VENDORID_LO || 117 ma->mii_id2 != DC_DEVICEID_82C168) 118 return(ENXIO); 119 120 device_set_desc(dev, "PNIC 82c168 media interface"); 121 122 return (0); 123 } 124 125 static int 126 pnphy_attach(dev) 127 device_t dev; 128 { 129 struct mii_softc *sc; 130 struct mii_attach_args *ma; 131 struct mii_data *mii; 132 133 sc = device_get_softc(dev); 134 ma = device_get_ivars(dev); 135 sc->mii_dev = device_get_parent(dev); 136 mii = device_get_softc(sc->mii_dev); 137 LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list); 138 139 sc->mii_inst = mii->mii_instance; 140 sc->mii_phy = ma->mii_phyno; 141 sc->mii_service = pnphy_service; 142 sc->mii_pdata = mii; 143 144 sc->mii_flags |= MIIF_NOISOLATE; 145 mii->mii_instance++; 146 147 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL) 148 149 sc->mii_capabilities = 150 BMSR_100TXFDX|BMSR_100TXHDX|BMSR_10TFDX|BMSR_10THDX; 151 sc->mii_capabilities &= ma->mii_capmask; 152 device_printf(dev, " "); 153 mii_add_media(sc); 154 printf("\n"); 155 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst), 156 BMCR_ISO); 157 158 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->mii_inst), 159 BMCR_LOOP|BMCR_S100); 160 161 #undef ADD 162 163 MIIBUS_MEDIAINIT(sc->mii_dev); 164 return(0); 165 } 166 167 static int 168 pnphy_service(sc, mii, cmd) 169 struct mii_softc *sc; 170 struct mii_data *mii; 171 int cmd; 172 { 173 struct dc_softc *dc_sc; 174 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 175 176 dc_sc = mii->mii_ifp->if_softc; 177 178 switch (cmd) { 179 case MII_POLLSTAT: 180 /* 181 * If we're not polling our PHY instance, just return. 182 */ 183 if (IFM_INST(ife->ifm_media) != sc->mii_inst) { 184 return (0); 185 } 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 return (0); 195 196 /* 197 * If the interface is not up, don't do anything. 198 */ 199 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 200 break; 201 202 sc->mii_flags = 0; 203 204 switch (IFM_SUBTYPE(ife->ifm_media)) { 205 case IFM_AUTO: 206 /* NWAY is busted on this chip */ 207 case IFM_100_T4: 208 /* 209 * XXX Not supported as a manual setting right now. 210 */ 211 return (EINVAL); 212 case IFM_100_TX: 213 mii->mii_media_active = IFM_ETHER|IFM_100_TX; 214 if ((ife->ifm_media & IFM_GMASK) == IFM_FDX) 215 mii->mii_media_active |= IFM_FDX; 216 MIIBUS_STATCHG(sc->mii_dev); 217 return(0); 218 break; 219 case IFM_10_T: 220 mii->mii_media_active = IFM_ETHER|IFM_10_T; 221 if ((ife->ifm_media & IFM_GMASK) == IFM_FDX) 222 mii->mii_media_active |= IFM_FDX; 223 MIIBUS_STATCHG(sc->mii_dev); 224 return(0); 225 break; 226 default: 227 return(EINVAL); 228 break; 229 } 230 break; 231 232 case MII_TICK: 233 /* 234 * If we're not currently selected, just return. 235 */ 236 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 237 return (0); 238 239 /* 240 * Is the interface even up? 241 */ 242 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 243 return (0); 244 245 break; 246 } 247 248 /* Update the media status. */ 249 pnphy_status(sc); 250 251 /* Callback if something changed. */ 252 mii_phy_update(sc, cmd); 253 return (0); 254 } 255 256 static void 257 pnphy_status(sc) 258 struct mii_softc *sc; 259 { 260 struct mii_data *mii = sc->mii_pdata; 261 int reg; 262 struct dc_softc *dc_sc; 263 264 dc_sc = mii->mii_ifp->if_softc; 265 266 mii->mii_media_status = IFM_AVALID; 267 mii->mii_media_active = IFM_ETHER; 268 269 reg = CSR_READ_4(dc_sc, DC_ISR); 270 271 if (!(reg & DC_ISR_LINKFAIL)) 272 mii->mii_media_status |= IFM_ACTIVE; 273 274 if (CSR_READ_4(dc_sc, DC_NETCFG) & DC_NETCFG_SPEEDSEL) 275 mii->mii_media_active |= IFM_10_T; 276 else 277 mii->mii_media_active |= IFM_100_TX; 278 if (CSR_READ_4(dc_sc, DC_NETCFG) & DC_NETCFG_FULLDUPLEX) 279 mii->mii_media_active |= IFM_FDX; 280 281 return; 282 } 283