2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
22 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
24 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
27 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
29 * THE POSSIBILITY OF SUCH DAMAGES.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
36 * PCI/Cardbus front-end for the Atheros Wireless LAN controller driver.
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/malloc.h>
43 #include <sys/module.h>
44 #include <sys/kernel.h>
46 #include <sys/mutex.h>
47 #include <sys/errno.h>
49 #include <machine/bus.h>
50 #include <machine/resource.h>
54 #include <sys/socket.h>
57 #include <net/if_media.h>
58 #include <net/if_arp.h>
59 #include <net/ethernet.h>
61 #include <net80211/ieee80211_var.h>
63 #include <dev/ath/if_athvar.h>
65 #include <dev/pci/pcivar.h>
66 #include <dev/pci/pcireg.h>
68 /* For EEPROM firmware */
69 #ifdef ATH_EEPROM_FIRMWARE
70 #include <sys/linker.h>
71 #include <sys/firmware.h>
72 #endif /* ATH_EEPROM_FIRMWARE */
78 struct ath_pci_softc {
79 struct ath_softc sc_sc;
80 struct resource *sc_sr; /* memory resource */
81 struct resource *sc_irq; /* irq resource */
82 void *sc_ih; /* interrupt handler */
85 #define PCI_VDEVICE(v, d) \
88 #define PCI_DEVICE_SUB(v, d, sv, sd) \
89 PCI_DEV(v, d), PCI_SUBDEV(sv, sd)
91 #define PCI_VENDOR_ID_ATHEROS 0x168c
92 #define PCI_VENDOR_ID_SAMSUNG 0x144d
93 #define PCI_VENDOR_ID_AZWAVE 0x1a3b
94 #define PCI_VENDOR_ID_FOXCONN 0x105b
95 #define PCI_VENDOR_ID_ATTANSIC 0x1969
96 #define PCI_VENDOR_ID_ASUSTEK 0x1043
97 #define PCI_VENDOR_ID_DELL 0x1028
98 #define PCI_VENDOR_ID_QMI 0x1a32
99 #define PCI_VENDOR_ID_LENOVO 0x17aa
100 #define PCI_VENDOR_ID_HP 0x103c
102 #include "if_ath_pci_devlist.h"
105 #define PCIR_RETRY_TIMEOUT 0x41
106 #define PCIR_CFG_PMCSR 0x48
108 #define DEFAULT_CACHESIZE 32
111 ath_pci_setup(device_t dev)
115 /* XXX TODO: need to override the _system_ saved copies of this */
118 * If the cache line size is 0, force it to a reasonable
121 cz = pci_read_config(dev, PCIR_CACHELNSZ, 1);
123 pci_write_config(dev, PCIR_CACHELNSZ,
124 DEFAULT_CACHESIZE / 4, 1);
127 /* Override the system latency timer */
128 pci_write_config(dev, PCIR_LATTIMER, 0xa8, 1);
130 /* If a PCI NIC, force wakeup */
131 #ifdef ATH_PCI_WAKEUP_WAR
132 /* XXX TODO: don't do this for non-PCI (ie, PCIe, Cardbus!) */
135 pmcsr = pci_read_config(dev, PCIR_CFG_PMCSR, 2);
137 pci_write_config(dev, PCIR_CFG_PMCSR, pmcsr, 2);
139 pci_write_config(dev, PCIR_CFG_PMCSR, pmcsr, 2);
144 * Disable retry timeout to keep PCI Tx retries from
145 * interfering with C3 CPU state.
147 pci_write_config(dev, PCIR_RETRY_TIMEOUT, 0, 1);
151 ath_pci_probe(device_t dev)
155 devname = ath_hal_probe(pci_get_vendor(dev), pci_get_device(dev));
156 if (devname != NULL) {
157 device_set_desc(dev, devname);
158 return BUS_PROBE_DEFAULT;
164 ath_pci_attach(device_t dev)
166 struct ath_pci_softc *psc = device_get_softc(dev);
167 struct ath_softc *sc = &psc->sc_sc;
170 #ifdef ATH_EEPROM_FIRMWARE
171 const struct firmware *fw = NULL;
174 const struct pci_device_table *pd;
178 /* Do this lookup anyway; figure out what to do with it later */
179 pd = PCI_MATCH(dev, ath_pci_id_table);
181 sc->sc_pci_devinfo = pd->driver_data;
184 * Enable bus mastering.
186 pci_enable_busmaster(dev);
189 * Setup other PCI bus configuration parameters.
194 * Setup memory-mapping of PCI registers.
197 psc->sc_sr = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
199 if (psc->sc_sr == NULL) {
200 device_printf(dev, "cannot map register space\n");
203 sc->sc_st = (HAL_BUS_TAG) rman_get_bustag(psc->sc_sr);
204 sc->sc_sh = (HAL_BUS_HANDLE) rman_get_bushandle(psc->sc_sr);
206 * Mark device invalid so any interrupts (shared or otherwise)
207 * that arrive before the HAL is setup are discarded.
212 ATH_PCU_LOCK_INIT(sc);
213 ATH_RX_LOCK_INIT(sc);
214 ATH_TX_LOCK_INIT(sc);
215 ATH_TXSTATUS_LOCK_INIT(sc);
218 * Arrange interrupt line.
221 psc->sc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
222 RF_SHAREABLE|RF_ACTIVE);
223 if (psc->sc_irq == NULL) {
224 device_printf(dev, "could not map interrupt\n");
227 if (bus_setup_intr(dev, psc->sc_irq,
228 INTR_TYPE_NET | INTR_MPSAFE,
229 NULL, ath_intr, sc, &psc->sc_ih)) {
230 device_printf(dev, "could not establish interrupt\n");
235 * Setup DMA descriptor area.
237 if (bus_dma_tag_create(bus_get_dma_tag(dev), /* parent */
238 1, 0, /* alignment, bounds */
239 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
240 BUS_SPACE_MAXADDR, /* highaddr */
241 NULL, NULL, /* filter, filterarg */
242 0x3ffff, /* maxsize XXX */
243 ATH_MAX_SCATTER, /* nsegments */
244 0x3ffff, /* maxsegsize XXX */
245 BUS_DMA_ALLOCNOW, /* flags */
249 device_printf(dev, "cannot allocate DMA tag\n");
253 #ifdef ATH_EEPROM_FIRMWARE
255 * If there's an EEPROM firmware image, load that in.
257 if (resource_string_value(device_get_name(dev), device_get_unit(dev),
258 "eeprom_firmware", &buf) == 0) {
260 device_printf(dev, "%s: looking up firmware @ '%s'\n",
263 fw = firmware_get(buf);
265 device_printf(dev, "%s: couldn't find firmware\n",
270 device_printf(dev, "%s: EEPROM firmware @ %p\n",
273 malloc(fw->datasize, M_TEMP, M_WAITOK | M_ZERO);
274 if (! sc->sc_eepromdata) {
275 device_printf(dev, "%s: can't malloc eepromdata\n",
279 memcpy(sc->sc_eepromdata, fw->data, fw->datasize);
282 #endif /* ATH_EEPROM_FIRMWARE */
284 error = ath_attach(pci_get_device(dev), sc);
285 if (error == 0) /* success */
288 #ifdef ATH_EEPROM_FIRMWARE
291 bus_dma_tag_destroy(sc->sc_dmat);
293 bus_teardown_intr(dev, psc->sc_irq, psc->sc_ih);
295 bus_release_resource(dev, SYS_RES_IRQ, 0, psc->sc_irq);
297 bus_release_resource(dev, SYS_RES_MEMORY, BS_BAR, psc->sc_sr);
299 ATH_TXSTATUS_LOCK_DESTROY(sc);
300 ATH_PCU_LOCK_DESTROY(sc);
301 ATH_RX_LOCK_DESTROY(sc);
302 ATH_TX_LOCK_DESTROY(sc);
303 ATH_LOCK_DESTROY(sc);
310 ath_pci_detach(device_t dev)
312 struct ath_pci_softc *psc = device_get_softc(dev);
313 struct ath_softc *sc = &psc->sc_sc;
315 /* check if device was removed */
316 sc->sc_invalid = !bus_child_present(dev);
319 * Do a config read to clear pre-existing pci error status.
321 (void) pci_read_config(dev, PCIR_COMMAND, 4);
325 bus_generic_detach(dev);
326 bus_teardown_intr(dev, psc->sc_irq, psc->sc_ih);
327 bus_release_resource(dev, SYS_RES_IRQ, 0, psc->sc_irq);
329 bus_dma_tag_destroy(sc->sc_dmat);
330 bus_release_resource(dev, SYS_RES_MEMORY, BS_BAR, psc->sc_sr);
332 if (sc->sc_eepromdata)
333 free(sc->sc_eepromdata, M_TEMP);
335 ATH_TXSTATUS_LOCK_DESTROY(sc);
336 ATH_PCU_LOCK_DESTROY(sc);
337 ATH_RX_LOCK_DESTROY(sc);
338 ATH_TX_LOCK_DESTROY(sc);
339 ATH_LOCK_DESTROY(sc);
345 ath_pci_shutdown(device_t dev)
347 struct ath_pci_softc *psc = device_get_softc(dev);
349 ath_shutdown(&psc->sc_sc);
354 ath_pci_suspend(device_t dev)
356 struct ath_pci_softc *psc = device_get_softc(dev);
358 ath_suspend(&psc->sc_sc);
364 ath_pci_resume(device_t dev)
366 struct ath_pci_softc *psc = device_get_softc(dev);
369 * Suspend/resume resets the PCI configuration space.
373 ath_resume(&psc->sc_sc);
378 static device_method_t ath_pci_methods[] = {
379 /* Device interface */
380 DEVMETHOD(device_probe, ath_pci_probe),
381 DEVMETHOD(device_attach, ath_pci_attach),
382 DEVMETHOD(device_detach, ath_pci_detach),
383 DEVMETHOD(device_shutdown, ath_pci_shutdown),
384 DEVMETHOD(device_suspend, ath_pci_suspend),
385 DEVMETHOD(device_resume, ath_pci_resume),
388 static driver_t ath_pci_driver = {
391 sizeof (struct ath_pci_softc)
393 static devclass_t ath_devclass;
394 DRIVER_MODULE(if_ath_pci, pci, ath_pci_driver, ath_devclass, 0, 0);
395 MODULE_VERSION(if_ath_pci, 1);
396 MODULE_DEPEND(if_ath_pci, wlan, 1, 1, 1); /* 802.11 media layer */
397 MODULE_DEPEND(if_ath_pci, ath_main, 1, 1, 1); /* if_ath driver */
398 MODULE_DEPEND(if_ath_pci, ath_hal, 1, 1, 1); /* ath HAL */