2 * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
3 * Copyright (c) 2010-2011 Adrian Chadd, Xenion Pty Ltd
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer,
11 * without modification.
12 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
13 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
14 * redistribution must be conditioned upon including a substantially
15 * similar Disclaimer requirement for further binary redistribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
21 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
23 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
26 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28 * THE POSSIBILITY OF SUCH DAMAGES.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
35 * AHB bus 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 <mips/atheros/ar71xxreg.h>
66 #include <mips/atheros/ar91xxreg.h>
67 #include <mips/atheros/ar71xx_cpudef.h>
69 /* For EEPROM firmware */
70 #ifdef ATH_EEPROM_FIRMWARE
71 #include <sys/linker.h>
72 #include <sys/firmware.h>
73 #endif /* ATH_EEPROM_FIRMWARE */
79 /* number of 16 bit words */
80 #define ATH_EEPROM_DATA_SIZE 2048
82 struct ath_ahb_softc {
83 struct ath_softc sc_sc;
84 struct resource *sc_sr; /* memory resource */
85 struct resource *sc_irq; /* irq resource */
86 void *sc_ih; /* interrupt handler */
89 #define VENDOR_ATHEROS 0x168c
90 #define AR9130_DEVID 0x000b
93 ath_ahb_probe(device_t dev)
95 int vendor_id, device_id;
99 * Check if a device/vendor ID is provided in hints.
101 if (resource_int_value(device_get_name(dev), device_get_unit(dev),
102 "vendor_id", &vendor_id) != 0) {
103 vendor_id = VENDOR_ATHEROS;
106 if (resource_int_value(device_get_name(dev), device_get_unit(dev),
107 "device_id", &device_id) != 0) {
108 device_id = AR9130_DEVID;
111 device_printf(dev, "Vendor=0x%04x, Device=0x%04x\n",
115 /* Attempt to probe */
116 devname = ath_hal_probe(vendor_id, device_id);
118 if (devname != NULL) {
119 device_set_desc(dev, devname);
120 return BUS_PROBE_DEFAULT;
126 ath_ahb_intr(void *arg)
128 /* XXX TODO: check if its ours! */
129 ar71xx_device_flush_ddr(AR71XX_CPU_DDR_FLUSH_WMAC);
134 ath_ahb_attach(device_t dev)
136 struct ath_ahb_softc *psc = device_get_softc(dev);
137 struct ath_softc *sc = &psc->sc_sc;
140 int device_id, vendor_id;
141 #ifdef ATH_EEPROM_FIRMWARE
142 const struct firmware *fw = NULL;
149 psc->sc_sr = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
150 if (psc->sc_sr == NULL) {
151 device_printf(dev, "cannot map register space\n");
155 sc->sc_st = (HAL_BUS_TAG) rman_get_bustag(psc->sc_sr);
156 sc->sc_sh = (HAL_BUS_HANDLE) rman_get_bushandle(psc->sc_sr);
158 * Mark device invalid so any interrupts (shared or otherwise)
159 * that arrive before the HAL is setup are discarded.
163 #ifdef ATH_EEPROM_FIRMWARE
165 * If there's an EEPROM firmware image, load that in.
167 if (resource_string_value(device_get_name(dev), device_get_unit(dev),
168 "eeprom_firmware", &buf) == 0) {
169 device_printf(dev, "%s: looking up firmware @ '%s'\n",
172 fw = firmware_get(buf);
174 device_printf(dev, "%s: couldn't find firmware\n",
179 device_printf(dev, "%s: EEPROM firmware @ %p\n",
182 malloc(fw->datasize, M_TEMP, M_WAITOK | M_ZERO);
183 if (! sc->sc_eepromdata) {
184 device_printf(dev, "%s: can't malloc eepromdata\n",
188 memcpy(sc->sc_eepromdata, fw->data, fw->datasize);
191 #endif /* ATH_EEPROM_FIRMWARE */
194 * Arrange interrupt line.
197 psc->sc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_SHAREABLE|RF_ACTIVE);
198 if (psc->sc_irq == NULL) {
199 device_printf(dev, "could not map interrupt\n");
202 if (bus_setup_intr(dev, psc->sc_irq,
203 INTR_TYPE_NET | INTR_MPSAFE,
204 NULL, ath_ahb_intr, sc, &psc->sc_ih)) {
205 device_printf(dev, "could not establish interrupt\n");
210 * Setup DMA descriptor area.
212 if (bus_dma_tag_create(bus_get_dma_tag(dev), /* parent */
213 1, 0, /* alignment, bounds */
214 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
215 BUS_SPACE_MAXADDR, /* highaddr */
216 NULL, NULL, /* filter, filterarg */
217 0x3ffff, /* maxsize XXX */
218 ATH_MAX_SCATTER, /* nsegments */
219 0x3ffff, /* maxsegsize XXX */
220 BUS_DMA_ALLOCNOW, /* flags */
224 device_printf(dev, "cannot allocate DMA tag\n");
229 * Check if a device/vendor ID is provided in hints.
231 if (resource_int_value(device_get_name(dev), device_get_unit(dev),
232 "vendor_id", &vendor_id) != 0) {
233 vendor_id = VENDOR_ATHEROS;
236 if (resource_int_value(device_get_name(dev), device_get_unit(dev),
237 "device_id", &device_id) != 0) {
238 device_id = AR9130_DEVID;
242 ATH_PCU_LOCK_INIT(sc);
243 ATH_RX_LOCK_INIT(sc);
244 ATH_TX_LOCK_INIT(sc);
245 ATH_TXSTATUS_LOCK_INIT(sc);
247 error = ath_attach(device_id, sc);
248 if (error == 0) /* success */
251 ATH_TXSTATUS_LOCK_DESTROY(sc);
252 ATH_RX_LOCK_DESTROY(sc);
253 ATH_TX_LOCK_DESTROY(sc);
254 ATH_PCU_LOCK_DESTROY(sc);
255 ATH_LOCK_DESTROY(sc);
256 bus_dma_tag_destroy(sc->sc_dmat);
258 bus_teardown_intr(dev, psc->sc_irq, psc->sc_ih);
260 bus_release_resource(dev, SYS_RES_IRQ, 0, psc->sc_irq);
262 bus_release_resource(dev, SYS_RES_MEMORY, 0, psc->sc_sr);
265 if (sc->sc_eepromdata)
266 free(sc->sc_eepromdata, M_TEMP);
271 ath_ahb_detach(device_t dev)
273 struct ath_ahb_softc *psc = device_get_softc(dev);
274 struct ath_softc *sc = &psc->sc_sc;
276 /* check if device was removed */
277 sc->sc_invalid = !bus_child_present(dev);
281 bus_generic_detach(dev);
282 bus_teardown_intr(dev, psc->sc_irq, psc->sc_ih);
283 bus_release_resource(dev, SYS_RES_IRQ, 0, psc->sc_irq);
285 bus_dma_tag_destroy(sc->sc_dmat);
286 bus_release_resource(dev, SYS_RES_MEMORY, 0, psc->sc_sr);
288 if (sc->sc_eepromdata)
289 free(sc->sc_eepromdata, M_TEMP);
291 ATH_TXSTATUS_LOCK_DESTROY(sc);
292 ATH_RX_LOCK_DESTROY(sc);
293 ATH_TX_LOCK_DESTROY(sc);
294 ATH_PCU_LOCK_DESTROY(sc);
295 ATH_LOCK_DESTROY(sc);
301 ath_ahb_shutdown(device_t dev)
303 struct ath_ahb_softc *psc = device_get_softc(dev);
305 ath_shutdown(&psc->sc_sc);
310 ath_ahb_suspend(device_t dev)
312 struct ath_ahb_softc *psc = device_get_softc(dev);
314 ath_suspend(&psc->sc_sc);
320 ath_ahb_resume(device_t dev)
322 struct ath_ahb_softc *psc = device_get_softc(dev);
324 ath_resume(&psc->sc_sc);
329 static device_method_t ath_ahb_methods[] = {
330 /* Device interface */
331 DEVMETHOD(device_probe, ath_ahb_probe),
332 DEVMETHOD(device_attach, ath_ahb_attach),
333 DEVMETHOD(device_detach, ath_ahb_detach),
334 DEVMETHOD(device_shutdown, ath_ahb_shutdown),
335 DEVMETHOD(device_suspend, ath_ahb_suspend),
336 DEVMETHOD(device_resume, ath_ahb_resume),
340 static driver_t ath_ahb_driver = {
343 sizeof (struct ath_ahb_softc)
345 static devclass_t ath_devclass;
346 DRIVER_MODULE(if_ath_ahb, nexus, ath_ahb_driver, ath_devclass, 0, 0);
347 DRIVER_MODULE(if_ath_ahb, apb, ath_ahb_driver, ath_devclass, 0, 0);
348 MODULE_VERSION(if_ath_ahb, 1);
349 MODULE_DEPEND(if_ath_ahb, wlan, 1, 1, 1); /* 802.11 media layer */
350 MODULE_DEPEND(if_ath_ahb, ath_main, 1, 1, 1); /* if_ath driver */
351 MODULE_DEPEND(if_ath_ahb, ath_hal, 1, 1, 1); /* ath HAL */