1 /* $OpenBSD: if_fxp_cardbus.c,v 1.21 2008/02/25 23:10:16 brad Exp $ */ 2 /* $NetBSD: if_fxp_cardbus.c,v 1.12 2000/05/08 18:23:36 thorpej Exp $ */ 3 4 /* 5 * Copyright (c) 1999 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by Johan Danielsson. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the NetBSD 22 * Foundation, Inc. and its contributors. 23 * 4. Neither the name of The NetBSD Foundation nor the names of its 24 * contributors may be used to endorse or promote products derived 25 * from this software without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 37 * POSSIBILITY OF SUCH DAMAGE. 38 */ 39 40 /* 41 * CardBus front-end for the Intel i8255x family of Ethernet chips. 42 */ 43 44 #include "bpfilter.h" 45 46 #include <sys/param.h> 47 #include <sys/systm.h> 48 #include <sys/mbuf.h> 49 #include <sys/socket.h> 50 #include <sys/ioctl.h> 51 #include <sys/errno.h> 52 #include <sys/malloc.h> 53 #include <sys/kernel.h> 54 #include <sys/timeout.h> 55 #include <sys/device.h> 56 57 #include <net/if.h> 58 #include <net/if_dl.h> 59 #include <net/if_types.h> 60 #include <net/if_media.h> 61 62 #include <machine/endian.h> 63 64 #if NBPFILTER > 0 65 #include <net/bpf.h> 66 #endif 67 68 #ifdef INET 69 #include <netinet/in.h> 70 #include <netinet/in_systm.h> 71 #include <netinet/in_var.h> 72 #include <netinet/ip.h> 73 #include <netinet/if_ether.h> 74 #endif 75 76 #include <machine/bus.h> 77 #include <machine/intr.h> 78 79 #include <dev/mii/miivar.h> 80 81 #include <dev/ic/fxpreg.h> 82 #include <dev/ic/fxpvar.h> 83 84 #include <dev/pci/pcivar.h> 85 #include <dev/pci/pcireg.h> 86 #include <dev/pci/pcidevs.h> 87 88 #include <dev/cardbus/cardbusvar.h> 89 90 int fxp_cardbus_match(struct device *, void *, void *); 91 void fxp_cardbus_attach(struct device *, struct device *, void *); 92 int fxp_cardbus_detach(struct device *, int); 93 void fxp_cardbus_setup(struct fxp_softc *); 94 95 struct fxp_cardbus_softc { 96 struct fxp_softc sc; 97 cardbus_devfunc_t ct; 98 cardbustag_t ct_tag; 99 pcireg_t base0_reg; 100 pcireg_t base1_reg; 101 bus_size_t size; 102 }; 103 104 struct cfattach fxp_cardbus_ca = { 105 sizeof(struct fxp_cardbus_softc), fxp_cardbus_match, fxp_cardbus_attach, 106 fxp_cardbus_detach 107 }; 108 109 const struct cardbus_matchid fxp_cardbus_devices[] = { 110 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_8255x }, 111 }; 112 113 #ifdef CBB_DEBUG 114 #define DPRINTF(X) printf X 115 #else 116 #define DPRINTF(X) 117 #endif 118 119 int 120 fxp_cardbus_match(struct device *parent, void *match, void *aux) 121 { 122 return (cardbus_matchbyid((struct cardbus_attach_args *)aux, 123 fxp_cardbus_devices, 124 sizeof(fxp_cardbus_devices)/sizeof(fxp_cardbus_devices[0]))); 125 } 126 127 void 128 fxp_cardbus_attach(struct device *parent, struct device *self, void *aux) 129 { 130 char intrstr[16]; 131 struct fxp_softc *sc = (struct fxp_softc *) self; 132 struct fxp_cardbus_softc *csc = (struct fxp_cardbus_softc *) self; 133 struct cardbus_attach_args *ca = aux; 134 struct cardbus_softc *psc = 135 (struct cardbus_softc *)sc->sc_dev.dv_parent; 136 cardbus_chipset_tag_t cc = psc->sc_cc; 137 cardbus_function_tag_t cf = psc->sc_cf; 138 bus_space_tag_t iot, memt; 139 bus_space_handle_t ioh, memh; 140 141 bus_addr_t adr; 142 bus_size_t size; 143 144 csc->ct = ca->ca_ct; 145 146 /* 147 * Map control/status registers. 148 */ 149 if (Cardbus_mapreg_map(csc->ct, CARDBUS_BASE1_REG, 150 PCI_MAPREG_TYPE_IO, 0, &iot, &ioh, &adr, &size) == 0) { 151 csc->base1_reg = adr | 1; 152 sc->sc_st = iot; 153 sc->sc_sh = ioh; 154 csc->size = size; 155 } else if (Cardbus_mapreg_map(csc->ct, CARDBUS_BASE0_REG, 156 PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT, 157 0, &memt, &memh, &adr, &size) == 0) { 158 csc->base0_reg = adr; 159 sc->sc_st = memt; 160 sc->sc_sh = memh; 161 csc->size = size; 162 } else 163 panic("%s: failed to allocate mem and io space", __func__); 164 165 sc->sc_dmat = ca->ca_dmat; 166 #if 0 167 sc->sc_enable = fxp_cardbus_enable; 168 sc->sc_disable = fxp_cardbus_disable; 169 sc->sc_enabled = 0; 170 #endif 171 172 Cardbus_function_enable(csc->ct); 173 174 fxp_cardbus_setup(sc); 175 176 /* Map and establish the interrupt. */ 177 sc->sc_ih = cardbus_intr_establish(cc, cf, psc->sc_intrline, IPL_NET, 178 fxp_intr, sc, sc->sc_dev.dv_xname); 179 if (NULL == sc->sc_ih) { 180 printf(": couldn't establish interrupt"); 181 printf("at %d\n", ca->ca_intrline); 182 return; 183 } 184 snprintf(intrstr, sizeof(intrstr), "irq %d", ca->ca_intrline); 185 186 sc->sc_revision = PCI_REVISION(ca->ca_class); 187 188 fxp_attach(sc, intrstr); 189 } 190 191 void 192 fxp_cardbus_setup(struct fxp_softc *sc) 193 { 194 struct fxp_cardbus_softc *csc = (struct fxp_cardbus_softc *) sc; 195 struct cardbus_softc *psc = 196 (struct cardbus_softc *) sc->sc_dev.dv_parent; 197 cardbus_chipset_tag_t cc = psc->sc_cc; 198 cardbus_function_tag_t cf = psc->sc_cf; 199 pcireg_t command; 200 201 csc->ct_tag = cardbus_make_tag(cc, cf, csc->ct->ct_bus, 202 csc->ct->ct_dev, csc->ct->ct_func); 203 204 command = cardbus_conf_read(cc, cf, csc->ct_tag, CARDBUS_COMMAND_STATUS_REG); 205 if (csc->base0_reg) { 206 cardbus_conf_write(cc, cf, csc->ct_tag, CARDBUS_BASE0_REG, csc->base0_reg); 207 (cf->cardbus_ctrl) (cc, CARDBUS_MEM_ENABLE); 208 command |= CARDBUS_COMMAND_MEM_ENABLE | 209 CARDBUS_COMMAND_MASTER_ENABLE; 210 } else if (csc->base1_reg) { 211 cardbus_conf_write(cc, cf, csc->ct_tag, CARDBUS_BASE1_REG, csc->base1_reg); 212 (cf->cardbus_ctrl) (cc, CARDBUS_IO_ENABLE); 213 command |= (CARDBUS_COMMAND_IO_ENABLE | 214 CARDBUS_COMMAND_MASTER_ENABLE); 215 } 216 217 (cf->cardbus_ctrl) (cc, CARDBUS_BM_ENABLE); 218 219 /* enable the card */ 220 cardbus_conf_write(cc, cf, csc->ct_tag, CARDBUS_COMMAND_STATUS_REG, command); 221 } 222 223 int fxp_detach(struct fxp_softc *); 224 225 int 226 fxp_detach(struct fxp_softc *sc) 227 { 228 struct ifnet *ifp = &sc->sc_arpcom.ac_if; 229 230 /* Unhook our tick handler. */ 231 timeout_del(&sc->stats_update_to); 232 233 /* Detach any PHYs we might have. */ 234 if (LIST_FIRST(&sc->sc_mii.mii_phys) != NULL) 235 mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY); 236 237 /* Delete any remaining media. */ 238 ifmedia_delete_instance(&sc->sc_mii.mii_media, IFM_INST_ANY); 239 240 ether_ifdetach(ifp); 241 if_detach(ifp); 242 243 if (sc->sc_sdhook != NULL) 244 shutdownhook_disestablish(sc->sc_sdhook); 245 if (sc->sc_powerhook != NULL) 246 powerhook_disestablish(sc->sc_powerhook); 247 248 return (0); 249 } 250 251 int 252 fxp_cardbus_detach(struct device *self, int flags) 253 { 254 struct fxp_softc *sc = (struct fxp_softc *) self; 255 struct fxp_cardbus_softc *csc = (struct fxp_cardbus_softc *) self; 256 struct cardbus_devfunc *ct = csc->ct; 257 int rv, reg; 258 259 #ifdef DIAGNOSTIC 260 if (ct == NULL) 261 panic("%s: data structure lacks", sc->sc_dev.dv_xname); 262 #endif 263 264 rv = fxp_detach(sc); 265 if (rv == 0) { 266 /* 267 * Unhook the interrupt handler. 268 */ 269 cardbus_intr_disestablish(ct->ct_cc, ct->ct_cf, sc->sc_ih); 270 271 /* 272 * release bus space and close window 273 */ 274 if (csc->base0_reg) 275 reg = CARDBUS_BASE0_REG; 276 else 277 reg = CARDBUS_BASE1_REG; 278 Cardbus_mapreg_unmap(ct, reg, sc->sc_st, sc->sc_sh, csc->size); 279 } 280 return (rv); 281 } 282