1 /* $NetBSD: if_ath_pci.c,v 1.10 2005/06/22 22:07:48 martin Exp $ */ 2 3 /*- 4 * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer, 12 * without modification. 13 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 14 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any 15 * redistribution must be conditioned upon including a substantially 16 * similar Disclaimer requirement for further binary redistribution. 17 * 3. Neither the names of the above-listed copyright holders nor the names 18 * of any contributors may be used to endorse or promote products derived 19 * from this software without specific prior written permission. 20 * 21 * Alternatively, this software may be distributed under the terms of the 22 * GNU General Public License ("GPL") version 2 as published by the Free 23 * Software Foundation. 24 * 25 * NO WARRANTY 26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 28 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY 29 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 30 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, 31 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 34 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 36 * THE POSSIBILITY OF SUCH DAMAGES. 37 */ 38 39 #include <sys/cdefs.h> 40 #ifdef __FreeBSD__ 41 __FBSDID("$FreeBSD: src/sys/dev/ath/if_ath_pci.c,v 1.11 2005/01/18 18:08:16 sam Exp $"); 42 #endif 43 #ifdef __NetBSD__ 44 __KERNEL_RCSID(0, "$NetBSD: if_ath_pci.c,v 1.10 2005/06/22 22:07:48 martin Exp $"); 45 #endif 46 47 /* 48 * PCI/Cardbus front-end for the Atheros Wireless LAN controller driver. 49 */ 50 51 #include <sys/param.h> 52 #include <sys/systm.h> 53 #include <sys/mbuf.h> 54 #include <sys/malloc.h> 55 #include <sys/kernel.h> 56 #include <sys/lock.h> 57 #include <sys/socket.h> 58 #include <sys/sockio.h> 59 #include <sys/errno.h> 60 #include <sys/device.h> 61 62 #include <machine/bus.h> 63 64 #include <net/if.h> 65 #include <net/if_media.h> 66 #include <net/if_ether.h> 67 #include <net/if_llc.h> 68 #include <net/if_arp.h> 69 70 #include <net80211/ieee80211_netbsd.h> 71 #include <net80211/ieee80211_var.h> 72 73 #ifdef INET 74 #include <netinet/in.h> 75 #endif 76 77 #include <dev/ic/ath_netbsd.h> 78 #include <dev/ic/athvar.h> 79 #include <contrib/dev/ic/athhal.h> 80 81 #include <dev/pci/pcivar.h> 82 #include <dev/pci/pcireg.h> 83 #include <dev/pci/pcidevs.h> 84 85 #include <sys/device.h> 86 87 /* 88 * PCI glue. 89 */ 90 91 struct ath_pci_softc { 92 struct ath_softc sc_sc; 93 pci_chipset_tag_t sc_pc; 94 void *sc_ih; /* interrupt handler */ 95 }; 96 97 #define BS_BAR 0x10 98 #define PCIR_RETRY_TIMEOUT_REG 0x40 99 #define PCIR_RETRY_TIMEOUT_MASK 0x0000ff00 100 101 static int ath_pci_match(struct device *, struct cfdata *, void *); 102 static void ath_pci_attach(struct device *, struct device *, void *); 103 static void ath_pci_shutdown(void *); 104 static int ath_pci_detach(struct device *, int); 105 106 CFATTACH_DECL(ath_pci, 107 sizeof(struct ath_pci_softc), 108 ath_pci_match, 109 ath_pci_attach, 110 ath_pci_detach, 111 NULL); 112 113 static int 114 ath_pci_match(struct device *parent, struct cfdata *match, void *aux) 115 { 116 const char* devname; 117 struct pci_attach_args *pa = aux; 118 119 devname = ath_hal_probe(PCI_VENDOR(pa->pa_id), PCI_PRODUCT(pa->pa_id)); 120 if (devname != NULL) 121 return 1; 122 return 0; 123 } 124 125 static int 126 ath_pci_setup(struct pci_attach_args *pa) 127 { 128 pci_chipset_tag_t pc = pa->pa_pc; 129 uint32_t res; 130 /* 131 * Enable memory mapping and bus mastering. 132 */ 133 pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, 134 pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) | 135 PCI_COMMAND_MASTER_ENABLE | PCI_COMMAND_MEM_ENABLE); 136 res = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG); 137 138 if ((res & PCI_COMMAND_MEM_ENABLE) == 0) { 139 aprint_error("couldn't enable memory mapping\n"); 140 return 0; 141 } 142 if ((res & PCI_COMMAND_MASTER_ENABLE) == 0) { 143 aprint_error("couldn't enable bus mastering\n"); 144 return 0; 145 } 146 147 /* 148 * Disable retry timeout to keep PCI Tx retries from 149 * interfering with C3 CPU state. 150 */ 151 pci_conf_write(pc, pa->pa_tag, PCIR_RETRY_TIMEOUT_REG, 152 pci_conf_read(pc, pa->pa_tag, PCIR_RETRY_TIMEOUT_REG) & 153 ~PCIR_RETRY_TIMEOUT_MASK); 154 155 return 1; 156 } 157 158 static void 159 ath_pci_attach(struct device *parent, struct device *self, void *aux) 160 { 161 struct ath_pci_softc *psc = (struct ath_pci_softc *)self; 162 struct ath_softc *sc = &psc->sc_sc; 163 struct pci_attach_args *pa = aux; 164 pci_chipset_tag_t pc = pa->pa_pc; 165 bus_space_tag_t iot; 166 bus_space_handle_t ioh; 167 pci_intr_handle_t ih; 168 void *hook; 169 const char *intrstr = NULL; 170 171 psc->sc_pc = pc; 172 173 if (!ath_pci_setup(pa)) 174 goto bad; 175 176 /* 177 * Setup memory-mapping of PCI registers. 178 */ 179 if (pci_mapreg_map(pa, BS_BAR, PCI_MAPREG_TYPE_MEM, 0, &iot, &ioh, 180 NULL, NULL)) { 181 aprint_error("cannot map register space\n"); 182 goto bad; 183 } 184 sc->sc_st = iot; 185 sc->sc_sh = ioh; 186 187 sc->sc_invalid = 1; 188 189 /* 190 * Arrange interrupt line. 191 */ 192 if (pci_intr_map(pa, &ih)) { 193 aprint_error("couldn't map interrupt\n"); 194 goto bad1; 195 } 196 197 intrstr = pci_intr_string(pc, ih); 198 psc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, ath_intr, sc); 199 if (psc->sc_ih == NULL) { 200 aprint_error("couldn't map interrupt\n"); 201 goto bad2; 202 } 203 204 printf("\n"); 205 printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr); 206 207 sc->sc_dmat = pa->pa_dmat; 208 209 hook = shutdownhook_establish(ath_pci_shutdown, psc); 210 if (hook == NULL) { 211 aprint_error("couldn't make shutdown hook\n"); 212 goto bad3; 213 } 214 215 if (ath_attach(PCI_PRODUCT(pa->pa_id), sc) == 0) 216 return; 217 218 shutdownhook_disestablish(hook); 219 220 bad3: pci_intr_disestablish(pc, psc->sc_ih); 221 bad2: /* XXX */ 222 bad1: /* XXX */ 223 bad: 224 return; 225 } 226 227 static int 228 ath_pci_detach(struct device *self, int flags) 229 { 230 struct ath_pci_softc *psc = (struct ath_pci_softc *)self; 231 232 ath_detach(&psc->sc_sc); 233 pci_intr_disestablish(psc->sc_pc, psc->sc_ih); 234 235 return (0); 236 } 237 238 static void 239 ath_pci_shutdown(void *self) 240 { 241 struct ath_pci_softc *psc = (struct ath_pci_softc *)self; 242 243 ath_shutdown(&psc->sc_sc); 244 } 245