1 /* $NetBSD: if_ath_pci.c,v 1.6 2004/04/30 22:19:33 dyoung Exp $ */ 2 3 /*- 4 * Copyright (c) 2002-2004 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.8 2004/04/02 23:57:10 sam Exp $"); 42 #endif 43 #ifdef __NetBSD__ 44 __KERNEL_RCSID(0, "$NetBSD: if_ath_pci.c,v 1.6 2004/04/30 22:19:33 dyoung Exp $"); 45 #endif 46 47 /* 48 * PCI/Cardbus front-end for the Atheros Wireless LAN controller driver. 49 */ 50 51 #include "opt_inet.h" 52 53 #include <sys/param.h> 54 #include <sys/systm.h> 55 #include <sys/mbuf.h> 56 #include <sys/malloc.h> 57 #include <sys/kernel.h> 58 #include <sys/lock.h> 59 #include <sys/socket.h> 60 #include <sys/sockio.h> 61 #include <sys/errno.h> 62 63 #include <machine/bus.h> 64 65 #include <net/if.h> 66 #include <net/if_dl.h> 67 #include <net/if_media.h> 68 #include <net/if_ether.h> 69 #include <net/if_llc.h> 70 #include <net/if_arp.h> 71 72 #include <net80211/ieee80211_compat.h> 73 #include <net80211/ieee80211_var.h> 74 75 #ifdef INET 76 #include <netinet/in.h> 77 #endif 78 79 #include <dev/ic/athcompat.h> 80 #include <dev/ic/athvar.h> 81 #include <../contrib/sys/dev/ic/athhal.h> 82 83 #include <dev/pci/pcivar.h> 84 #include <dev/pci/pcireg.h> 85 #include <dev/pci/pcidevs.h> 86 87 #include <sys/device.h> 88 89 /* 90 * PCI glue. 91 */ 92 93 struct ath_pci_softc { 94 struct ath_softc sc_sc; 95 #ifdef __FreeBSD__ 96 struct resource *sc_sr; /* memory resource */ 97 struct resource *sc_irq; /* irq resource */ 98 #else 99 pci_chipset_tag_t sc_pc; 100 #endif 101 void *sc_ih; /* intererupt handler */ 102 u_int8_t sc_saved_intline; 103 u_int8_t sc_saved_cachelinesz; 104 u_int8_t sc_saved_lattimer; 105 }; 106 107 #define BS_BAR 0x10 108 109 static int ath_pci_match(struct device *, struct cfdata *, void *); 110 static void ath_pci_attach(struct device *, struct device *, void *); 111 static void ath_pci_shutdown(void *); 112 static int ath_pci_detach(struct device *, int); 113 114 CFATTACH_DECL(ath_pci, 115 sizeof(struct ath_pci_softc), 116 ath_pci_match, 117 ath_pci_attach, 118 ath_pci_detach, 119 NULL); 120 121 /* 122 * translate some product code. it is a workaround until HAL gets updated. 123 */ 124 static u_int16_t 125 ath_product(pcireg_t pa_id) 126 { 127 u_int16_t prodid; 128 129 prodid = PCI_PRODUCT(pa_id); 130 switch (prodid) { 131 case 0x1014: /* IBM 31P9702 minipci a/b/g card */ 132 prodid = PCI_PRODUCT_ATHEROS_AR5212; 133 break; 134 default: 135 break; 136 } 137 return prodid; 138 } 139 140 static int 141 ath_pci_match(struct device *parent, struct cfdata *match, void *aux) 142 { 143 const char* devname; 144 struct pci_attach_args *pa = aux; 145 146 devname = ath_hal_probe(PCI_VENDOR(pa->pa_id), ath_product(pa->pa_id)); 147 if (devname) 148 return 1; 149 150 return 0; 151 } 152 153 static void 154 ath_pci_attach(struct device *parent, struct device *self, void *aux) 155 { 156 struct ath_pci_softc *psc = (struct ath_pci_softc *)self; 157 struct ath_softc *sc = &psc->sc_sc; 158 u_int32_t res; 159 struct pci_attach_args *pa = aux; 160 pci_chipset_tag_t pc = pa->pa_pc; 161 bus_space_tag_t iot; 162 bus_space_handle_t ioh; 163 pci_intr_handle_t ih; 164 void *hook; 165 const char *intrstr = NULL; 166 167 psc->sc_pc = pc; 168 169 pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, 170 pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) | 171 PCI_COMMAND_MASTER_ENABLE | PCI_COMMAND_MEM_ENABLE); 172 res = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG); 173 174 if ((res & PCI_COMMAND_MEM_ENABLE) == 0) { 175 aprint_error("couldn't enable memory mapping\n"); 176 goto bad; 177 } 178 179 if ((res & PCI_COMMAND_MASTER_ENABLE) == 0) { 180 aprint_error("couldn't enable bus mastering\n"); 181 goto bad; 182 } 183 184 /* 185 * Setup memory-mapping of PCI registers. 186 */ 187 if (pci_mapreg_map(pa, BS_BAR, PCI_MAPREG_TYPE_MEM, 0, &iot, &ioh, 188 NULL, NULL)) { 189 aprint_error("cannot map register space\n"); 190 goto bad; 191 } 192 sc->sc_st = iot; 193 sc->sc_sh = ioh; 194 195 sc->sc_invalid = 1; 196 197 /* 198 * Arrange interrupt line. 199 */ 200 if (pci_intr_map(pa, &ih)) { 201 aprint_error("couldn't map interrupt\n"); 202 goto bad1; 203 } 204 205 intrstr = pci_intr_string(pc, ih); 206 psc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, ath_intr, sc); 207 if (psc->sc_ih == NULL) { 208 aprint_error("couldn't map interrupt\n"); 209 goto bad2; 210 } 211 212 printf("\n"); 213 printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr); 214 215 sc->sc_dmat = pa->pa_dmat; 216 217 hook = shutdownhook_establish(ath_pci_shutdown, psc); 218 if (hook == NULL) { 219 aprint_error("couldn't make shutdown hook\n"); 220 goto bad3; 221 } 222 223 if (ath_attach(ath_product(pa->pa_id), sc) == 0) 224 return; 225 226 shutdownhook_disestablish(hook); 227 228 bad3: pci_intr_disestablish(pc, psc->sc_ih); 229 bad2: /* XXX */ 230 bad1: /* XXX */ 231 bad: 232 return; 233 } 234 235 static int 236 ath_pci_detach(struct device *self, int flags) 237 { 238 struct ath_pci_softc *psc = (struct ath_pci_softc *)self; 239 240 ath_detach(&psc->sc_sc); 241 pci_intr_disestablish(psc->sc_pc, psc->sc_ih); 242 243 return (0); 244 } 245 246 static void 247 ath_pci_shutdown(void *self) 248 { 249 struct ath_pci_softc *psc = (struct ath_pci_softc *)self; 250 251 ath_shutdown(&psc->sc_sc); 252 } 253