2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
5 * Copyright (c) 2010-2011 Adrian Chadd, Xenion Pty Ltd
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer,
13 * without modification.
14 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
15 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
16 * redistribution must be conditioned upon including a substantially
17 * similar Disclaimer requirement for further binary redistribution.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
23 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
24 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
25 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
28 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30 * THE POSSIBILITY OF SUCH DAMAGES.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
37 * AHB bus front-end for the Atheros Wireless LAN controller driver.
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/malloc.h>
45 #include <sys/module.h>
46 #include <sys/kernel.h>
48 #include <sys/mutex.h>
49 #include <sys/errno.h>
51 #include <machine/bus.h>
52 #include <machine/resource.h>
56 #include <sys/socket.h>
59 #include <net/if_media.h>
60 #include <net/if_arp.h>
61 #include <net/ethernet.h>
63 #include <net80211/ieee80211_var.h>
65 #include <dev/ath/if_athvar.h>
67 #include <mips/atheros/ar71xxreg.h>
68 #include <mips/atheros/ar91xxreg.h>
69 #include <mips/atheros/ar71xx_cpudef.h>
71 /* For EEPROM firmware */
72 #ifdef ATH_EEPROM_FIRMWARE
73 #include <sys/linker.h>
74 #include <sys/firmware.h>
75 #endif /* ATH_EEPROM_FIRMWARE */
81 /* number of 16 bit words */
82 #define ATH_EEPROM_DATA_SIZE 2048
84 struct ath_ahb_softc {
85 struct ath_softc sc_sc;
86 struct resource *sc_sr; /* memory resource */
87 struct resource *sc_irq; /* irq resource */
88 void *sc_ih; /* interrupt handler */
91 #define VENDOR_ATHEROS 0x168c
92 #define AR9130_DEVID 0x000b
95 ath_ahb_probe(device_t dev)
97 int vendor_id, device_id;
101 * Check if a device/vendor ID is provided in hints.
103 if (resource_int_value(device_get_name(dev), device_get_unit(dev),
104 "vendor_id", &vendor_id) != 0) {
105 vendor_id = VENDOR_ATHEROS;
108 if (resource_int_value(device_get_name(dev), device_get_unit(dev),
109 "device_id", &device_id) != 0) {
110 device_id = AR9130_DEVID;
113 device_printf(dev, "Vendor=0x%04x, Device=0x%04x\n",
117 /* Attempt to probe */
118 devname = ath_hal_probe(vendor_id, device_id);
120 if (devname != NULL) {
121 device_set_desc(dev, devname);
122 return BUS_PROBE_DEFAULT;
128 ath_ahb_intr(void *arg)
130 /* XXX TODO: check if its ours! */
131 ar71xx_device_flush_ddr(AR71XX_CPU_DDR_FLUSH_WMAC);
136 ath_ahb_attach(device_t dev)
138 struct ath_ahb_softc *psc = device_get_softc(dev);
139 struct ath_softc *sc = &psc->sc_sc;
142 int device_id, vendor_id;
143 #ifdef ATH_EEPROM_FIRMWARE
144 const struct firmware *fw = NULL;
151 psc->sc_sr = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
152 if (psc->sc_sr == NULL) {
153 device_printf(dev, "cannot map register space\n");
157 sc->sc_st = (HAL_BUS_TAG) rman_get_bustag(psc->sc_sr);
158 sc->sc_sh = (HAL_BUS_HANDLE) rman_get_bushandle(psc->sc_sr);
160 * Mark device invalid so any interrupts (shared or otherwise)
161 * that arrive before the HAL is setup are discarded.
165 #ifdef ATH_EEPROM_FIRMWARE
167 * If there's an EEPROM firmware image, load that in.
169 if (resource_string_value(device_get_name(dev), device_get_unit(dev),
170 "eeprom_firmware", &buf) == 0) {
171 device_printf(dev, "%s: looking up firmware @ '%s'\n",
174 fw = firmware_get(buf);
176 device_printf(dev, "%s: couldn't find firmware\n",
181 device_printf(dev, "%s: EEPROM firmware @ %p\n",
184 malloc(fw->datasize, M_TEMP, M_WAITOK | M_ZERO);
185 if (! sc->sc_eepromdata) {
186 device_printf(dev, "%s: can't malloc eepromdata\n",
190 memcpy(sc->sc_eepromdata, fw->data, fw->datasize);
193 #endif /* ATH_EEPROM_FIRMWARE */
196 * Arrange interrupt line.
199 psc->sc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_SHAREABLE|RF_ACTIVE);
200 if (psc->sc_irq == NULL) {
201 device_printf(dev, "could not map interrupt\n");
204 if (bus_setup_intr(dev, psc->sc_irq,
205 INTR_TYPE_NET | INTR_MPSAFE,
206 NULL, ath_ahb_intr, sc, &psc->sc_ih)) {
207 device_printf(dev, "could not establish interrupt\n");
212 * Setup DMA descriptor area.
214 if (bus_dma_tag_create(bus_get_dma_tag(dev), /* parent */
215 1, 0, /* alignment, bounds */
216 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
217 BUS_SPACE_MAXADDR, /* highaddr */
218 NULL, NULL, /* filter, filterarg */
219 0x3ffff, /* maxsize XXX */
220 ATH_MAX_SCATTER, /* nsegments */
221 0x3ffff, /* maxsegsize XXX */
222 BUS_DMA_ALLOCNOW, /* flags */
226 device_printf(dev, "cannot allocate DMA tag\n");
231 * Check if a device/vendor ID is provided in hints.
233 if (resource_int_value(device_get_name(dev), device_get_unit(dev),
234 "vendor_id", &vendor_id) != 0) {
235 vendor_id = VENDOR_ATHEROS;
238 if (resource_int_value(device_get_name(dev), device_get_unit(dev),
239 "device_id", &device_id) != 0) {
240 device_id = AR9130_DEVID;
244 ATH_PCU_LOCK_INIT(sc);
245 ATH_RX_LOCK_INIT(sc);
246 ATH_TX_LOCK_INIT(sc);
247 ATH_TXSTATUS_LOCK_INIT(sc);
249 error = ath_attach(device_id, sc);
250 if (error == 0) /* success */
253 ATH_TXSTATUS_LOCK_DESTROY(sc);
254 ATH_RX_LOCK_DESTROY(sc);
255 ATH_TX_LOCK_DESTROY(sc);
256 ATH_PCU_LOCK_DESTROY(sc);
257 ATH_LOCK_DESTROY(sc);
258 bus_dma_tag_destroy(sc->sc_dmat);
260 bus_teardown_intr(dev, psc->sc_irq, psc->sc_ih);
262 bus_release_resource(dev, SYS_RES_IRQ, 0, psc->sc_irq);
264 bus_release_resource(dev, SYS_RES_MEMORY, 0, psc->sc_sr);
267 if (sc->sc_eepromdata)
268 free(sc->sc_eepromdata, M_TEMP);
273 ath_ahb_detach(device_t dev)
275 struct ath_ahb_softc *psc = device_get_softc(dev);
276 struct ath_softc *sc = &psc->sc_sc;
278 /* check if device was removed */
279 sc->sc_invalid = !bus_child_present(dev);
283 bus_generic_detach(dev);
284 bus_teardown_intr(dev, psc->sc_irq, psc->sc_ih);
285 bus_release_resource(dev, SYS_RES_IRQ, 0, psc->sc_irq);
287 bus_dma_tag_destroy(sc->sc_dmat);
288 bus_release_resource(dev, SYS_RES_MEMORY, 0, psc->sc_sr);
290 if (sc->sc_eepromdata)
291 free(sc->sc_eepromdata, M_TEMP);
293 ATH_TXSTATUS_LOCK_DESTROY(sc);
294 ATH_RX_LOCK_DESTROY(sc);
295 ATH_TX_LOCK_DESTROY(sc);
296 ATH_PCU_LOCK_DESTROY(sc);
297 ATH_LOCK_DESTROY(sc);
303 ath_ahb_shutdown(device_t dev)
305 struct ath_ahb_softc *psc = device_get_softc(dev);
307 ath_shutdown(&psc->sc_sc);
312 ath_ahb_suspend(device_t dev)
314 struct ath_ahb_softc *psc = device_get_softc(dev);
316 ath_suspend(&psc->sc_sc);
322 ath_ahb_resume(device_t dev)
324 struct ath_ahb_softc *psc = device_get_softc(dev);
326 ath_resume(&psc->sc_sc);
331 static device_method_t ath_ahb_methods[] = {
332 /* Device interface */
333 DEVMETHOD(device_probe, ath_ahb_probe),
334 DEVMETHOD(device_attach, ath_ahb_attach),
335 DEVMETHOD(device_detach, ath_ahb_detach),
336 DEVMETHOD(device_shutdown, ath_ahb_shutdown),
337 DEVMETHOD(device_suspend, ath_ahb_suspend),
338 DEVMETHOD(device_resume, ath_ahb_resume),
341 static driver_t ath_ahb_driver = {
344 sizeof (struct ath_ahb_softc)
346 static devclass_t ath_devclass;
347 DRIVER_MODULE(if_ath_ahb, nexus, ath_ahb_driver, ath_devclass, 0, 0);
348 DRIVER_MODULE(if_ath_ahb, apb, ath_ahb_driver, ath_devclass, 0, 0);
349 MODULE_VERSION(if_ath_ahb, 1);
350 MODULE_DEPEND(if_ath_ahb, wlan, 1, 1, 1); /* 802.11 media layer */
351 MODULE_DEPEND(if_ath_ahb, ath_main, 1, 1, 1); /* if_ath driver */
352 MODULE_DEPEND(if_ath_ahb, ath_hal, 1, 1, 1); /* ath HAL */