2 * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer,
10 * without modification.
11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13 * redistribution must be conditioned upon including a substantially
14 * similar Disclaimer requirement for further binary redistribution.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27 * THE POSSIBILITY OF SUCH DAMAGES.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
34 * PCI/Cardbus front-end for the Atheros Wireless LAN controller driver.
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/malloc.h>
41 #include <sys/module.h>
42 #include <sys/kernel.h>
44 #include <sys/mutex.h>
45 #include <sys/errno.h>
47 #include <machine/bus.h>
48 #include <machine/resource.h>
52 #include <sys/socket.h>
55 #include <net/if_media.h>
56 #include <net/if_arp.h>
57 #include <net/ethernet.h>
59 #include <net80211/ieee80211_var.h>
61 #include <dev/ath/if_athvar.h>
63 #include <dev/pci/pcivar.h>
64 #include <dev/pci/pcireg.h>
66 /* For EEPROM firmware */
67 #ifdef ATH_EEPROM_FIRMWARE
68 #include <sys/linker.h>
69 #include <sys/firmware.h>
70 #endif /* ATH_EEPROM_FIRMWARE */
76 struct ath_pci_softc {
77 struct ath_softc sc_sc;
78 struct resource *sc_sr; /* memory resource */
79 struct resource *sc_irq; /* irq resource */
80 void *sc_ih; /* interrupt handler */
84 * XXX eventually this should be some system level definition
85 * so modules will hvae probe/attach information like USB.
88 struct pci_device_id {
97 int match_populated:1;
98 int match_vendor_id:1;
99 int match_device_id:1;
100 int match_sub_vendor_id:1;
101 int match_sub_device_id:1;
104 #define PCI_VDEVICE(v, s) \
107 .match_populated = 1, \
108 .match_vendor_id = 1, \
111 #define PCI_DEVICE_SUB(v, d, dv, ds) \
112 .match_populated = 1, \
113 .vendor_id = (v), .match_vendor_id = 1, \
114 .device_id = (d), .match_device_id = 1, \
115 .sub_vendor_id = (dv), .match_sub_vendor_id = 1, \
116 .sub_device_id = (ds), .match_sub_device_id = 1
118 #define PCI_VENDOR_ID_ATHEROS 0x168c
119 #define PCI_VENDOR_ID_SAMSUNG 0x144d
120 #define PCI_VENDOR_ID_AZWAVE 0x1a3b
121 #define PCI_VENDOR_ID_FOXCONN 0x105b
122 #define PCI_VENDOR_ID_ATTANSIC 0x1969
123 #define PCI_VENDOR_ID_ASUSTEK 0x1043
124 #define PCI_VENDOR_ID_DELL 0x1028
125 #define PCI_VENDOR_ID_QMI 0x1a32
126 #define PCI_VENDOR_ID_LENOVO 0x17aa
127 #define PCI_VENDOR_ID_HP 0x103c
129 #include "if_ath_pci_devlist.h"
132 * Attempt to find a match for the given device in
133 * the given device table.
135 * Returns the device structure or NULL if no matching
136 * PCI device is found.
138 static const struct pci_device_id *
139 ath_pci_probe_device(device_t dev, const struct pci_device_id *dev_table, int nentries)
142 int vendor_id, device_id;
143 int sub_vendor_id, sub_device_id;
145 vendor_id = pci_get_vendor(dev);
146 device_id = pci_get_device(dev);
147 sub_vendor_id = pci_get_subvendor(dev);
148 sub_device_id = pci_get_subdevice(dev);
150 for (i = 0; i < nentries; i++) {
151 /* Don't match on non-populated (eg empty) entries */
152 if (! dev_table[i].match_populated)
155 if (dev_table[i].match_vendor_id &&
156 (dev_table[i].vendor_id != vendor_id))
158 if (dev_table[i].match_device_id &&
159 (dev_table[i].device_id != device_id))
161 if (dev_table[i].match_sub_vendor_id &&
162 (dev_table[i].sub_vendor_id != sub_vendor_id))
164 if (dev_table[i].match_sub_device_id &&
165 (dev_table[i].sub_device_id != sub_device_id))
169 return (&dev_table[i]);
176 #define PCIR_RETRY_TIMEOUT 0x41
177 #define PCIR_CFG_PMCSR 0x48
179 #define DEFAULT_CACHESIZE 32
182 ath_pci_setup(device_t dev)
186 /* XXX TODO: need to override the _system_ saved copies of this */
189 * If the cache line size is 0, force it to a reasonable
192 cz = pci_read_config(dev, PCIR_CACHELNSZ, 1);
194 pci_write_config(dev, PCIR_CACHELNSZ,
195 DEFAULT_CACHESIZE / 4, 1);
198 /* Override the system latency timer */
199 pci_write_config(dev, PCIR_LATTIMER, 0xa8, 1);
201 /* If a PCI NIC, force wakeup */
202 #ifdef ATH_PCI_WAKEUP_WAR
203 /* XXX TODO: don't do this for non-PCI (ie, PCIe, Cardbus!) */
206 pmcsr = pci_read_config(dev, PCIR_CFG_PMCSR, 2);
208 pci_write_config(dev, PCIR_CFG_PMCSR, pmcsr, 2);
210 pci_write_config(dev, PCIR_CFG_PMCSR, pmcsr, 2);
215 * Disable retry timeout to keep PCI Tx retries from
216 * interfering with C3 CPU state.
218 pci_write_config(dev, PCIR_RETRY_TIMEOUT, 0, 1);
222 ath_pci_probe(device_t dev)
226 devname = ath_hal_probe(pci_get_vendor(dev), pci_get_device(dev));
227 if (devname != NULL) {
228 device_set_desc(dev, devname);
229 return BUS_PROBE_DEFAULT;
235 ath_pci_attach(device_t dev)
237 struct ath_pci_softc *psc = device_get_softc(dev);
238 struct ath_softc *sc = &psc->sc_sc;
241 #ifdef ATH_EEPROM_FIRMWARE
242 const struct firmware *fw = NULL;
245 const struct pci_device_id *pd;
249 /* Do this lookup anyway; figure out what to do with it later */
250 pd = ath_pci_probe_device(dev, ath_pci_id_table, nitems(ath_pci_id_table));
252 sc->sc_pci_devinfo = pd->driver_data;
255 * Enable bus mastering.
257 pci_enable_busmaster(dev);
260 * Setup other PCI bus configuration parameters.
265 * Setup memory-mapping of PCI registers.
268 psc->sc_sr = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
270 if (psc->sc_sr == NULL) {
271 device_printf(dev, "cannot map register space\n");
274 sc->sc_st = (HAL_BUS_TAG) rman_get_bustag(psc->sc_sr);
275 sc->sc_sh = (HAL_BUS_HANDLE) rman_get_bushandle(psc->sc_sr);
277 * Mark device invalid so any interrupts (shared or otherwise)
278 * that arrive before the HAL is setup are discarded.
283 ATH_PCU_LOCK_INIT(sc);
284 ATH_RX_LOCK_INIT(sc);
285 ATH_TX_LOCK_INIT(sc);
286 ATH_TX_IC_LOCK_INIT(sc);
287 ATH_TXSTATUS_LOCK_INIT(sc);
290 * Arrange interrupt line.
293 psc->sc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
294 RF_SHAREABLE|RF_ACTIVE);
295 if (psc->sc_irq == NULL) {
296 device_printf(dev, "could not map interrupt\n");
299 if (bus_setup_intr(dev, psc->sc_irq,
300 INTR_TYPE_NET | INTR_MPSAFE,
301 NULL, ath_intr, sc, &psc->sc_ih)) {
302 device_printf(dev, "could not establish interrupt\n");
307 * Setup DMA descriptor area.
309 if (bus_dma_tag_create(bus_get_dma_tag(dev), /* parent */
310 1, 0, /* alignment, bounds */
311 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
312 BUS_SPACE_MAXADDR, /* highaddr */
313 NULL, NULL, /* filter, filterarg */
314 0x3ffff, /* maxsize XXX */
315 ATH_MAX_SCATTER, /* nsegments */
316 0x3ffff, /* maxsegsize XXX */
317 BUS_DMA_ALLOCNOW, /* flags */
321 device_printf(dev, "cannot allocate DMA tag\n");
325 #ifdef ATH_EEPROM_FIRMWARE
327 * If there's an EEPROM firmware image, load that in.
329 if (resource_string_value(device_get_name(dev), device_get_unit(dev),
330 "eeprom_firmware", &buf) == 0) {
332 device_printf(dev, "%s: looking up firmware @ '%s'\n",
335 fw = firmware_get(buf);
337 device_printf(dev, "%s: couldn't find firmware\n",
342 device_printf(dev, "%s: EEPROM firmware @ %p\n",
345 malloc(fw->datasize, M_TEMP, M_WAITOK | M_ZERO);
346 if (! sc->sc_eepromdata) {
347 device_printf(dev, "%s: can't malloc eepromdata\n",
351 memcpy(sc->sc_eepromdata, fw->data, fw->datasize);
354 #endif /* ATH_EEPROM_FIRMWARE */
356 error = ath_attach(pci_get_device(dev), sc);
357 if (error == 0) /* success */
360 #ifdef ATH_EEPROM_FIRMWARE
363 bus_dma_tag_destroy(sc->sc_dmat);
365 bus_teardown_intr(dev, psc->sc_irq, psc->sc_ih);
367 bus_release_resource(dev, SYS_RES_IRQ, 0, psc->sc_irq);
369 bus_release_resource(dev, SYS_RES_MEMORY, BS_BAR, psc->sc_sr);
371 ATH_TXSTATUS_LOCK_DESTROY(sc);
372 ATH_PCU_LOCK_DESTROY(sc);
373 ATH_RX_LOCK_DESTROY(sc);
374 ATH_TX_IC_LOCK_DESTROY(sc);
375 ATH_TX_LOCK_DESTROY(sc);
376 ATH_LOCK_DESTROY(sc);
383 ath_pci_detach(device_t dev)
385 struct ath_pci_softc *psc = device_get_softc(dev);
386 struct ath_softc *sc = &psc->sc_sc;
388 /* check if device was removed */
389 sc->sc_invalid = !bus_child_present(dev);
392 * Do a config read to clear pre-existing pci error status.
394 (void) pci_read_config(dev, PCIR_COMMAND, 4);
398 bus_generic_detach(dev);
399 bus_teardown_intr(dev, psc->sc_irq, psc->sc_ih);
400 bus_release_resource(dev, SYS_RES_IRQ, 0, psc->sc_irq);
402 bus_dma_tag_destroy(sc->sc_dmat);
403 bus_release_resource(dev, SYS_RES_MEMORY, BS_BAR, psc->sc_sr);
405 if (sc->sc_eepromdata)
406 free(sc->sc_eepromdata, M_TEMP);
408 ATH_TXSTATUS_LOCK_DESTROY(sc);
409 ATH_PCU_LOCK_DESTROY(sc);
410 ATH_RX_LOCK_DESTROY(sc);
411 ATH_TX_IC_LOCK_DESTROY(sc);
412 ATH_TX_LOCK_DESTROY(sc);
413 ATH_LOCK_DESTROY(sc);
419 ath_pci_shutdown(device_t dev)
421 struct ath_pci_softc *psc = device_get_softc(dev);
423 ath_shutdown(&psc->sc_sc);
428 ath_pci_suspend(device_t dev)
430 struct ath_pci_softc *psc = device_get_softc(dev);
432 ath_suspend(&psc->sc_sc);
438 ath_pci_resume(device_t dev)
440 struct ath_pci_softc *psc = device_get_softc(dev);
443 * Suspend/resume resets the PCI configuration space.
447 ath_resume(&psc->sc_sc);
452 static device_method_t ath_pci_methods[] = {
453 /* Device interface */
454 DEVMETHOD(device_probe, ath_pci_probe),
455 DEVMETHOD(device_attach, ath_pci_attach),
456 DEVMETHOD(device_detach, ath_pci_detach),
457 DEVMETHOD(device_shutdown, ath_pci_shutdown),
458 DEVMETHOD(device_suspend, ath_pci_suspend),
459 DEVMETHOD(device_resume, ath_pci_resume),
463 static driver_t ath_pci_driver = {
466 sizeof (struct ath_pci_softc)
468 static devclass_t ath_devclass;
469 DRIVER_MODULE(ath_pci, pci, ath_pci_driver, ath_devclass, 0, 0);
470 MODULE_VERSION(ath_pci, 1);
471 MODULE_DEPEND(ath_pci, wlan, 1, 1, 1); /* 802.11 media layer */
472 MODULE_DEPEND(ath_pci, if_ath, 1, 1, 1); /* if_ath driver */