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>
73 /* number of 16 bit words */
74 #define ATH_EEPROM_DATA_SIZE 2048
76 struct ath_ahb_softc {
77 struct ath_softc sc_sc;
78 struct resource *sc_sr; /* memory resource */
79 struct resource *sc_irq; /* irq resource */
80 struct resource *sc_eeprom; /* eeprom location */
81 void *sc_ih; /* interrupt handler */
84 #define VENDOR_ATHEROS 0x168c
85 #define AR9130_DEVID 0x000b
88 ath_ahb_probe(device_t dev)
90 int vendor_id, device_id;
94 * Check if a device/vendor ID is provided in hints.
96 if (resource_int_value(device_get_name(dev), device_get_unit(dev),
97 "vendor_id", &vendor_id) != 0) {
98 vendor_id = VENDOR_ATHEROS;
101 if (resource_int_value(device_get_name(dev), device_get_unit(dev),
102 "device_id", &device_id) != 0) {
103 device_id = AR9130_DEVID;
106 device_printf(dev, "Vendor=0x%04x, Device=0x%04x\n",
110 /* Attempt to probe */
111 devname = ath_hal_probe(vendor_id, device_id);
113 if (devname != NULL) {
114 device_set_desc(dev, devname);
115 return BUS_PROBE_DEFAULT;
121 ath_ahb_attach(device_t dev)
123 struct ath_ahb_softc *psc = device_get_softc(dev);
124 struct ath_softc *sc = &psc->sc_sc;
130 int device_id, vendor_id;
135 psc->sc_sr = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
136 if (psc->sc_sr == NULL) {
137 device_printf(dev, "cannot map register space\n");
141 if (resource_long_value(device_get_name(dev), device_get_unit(dev),
142 "eepromaddr", &eepromaddr) != 0) {
143 device_printf(dev, "cannot fetch 'eepromaddr' from hints\n");
148 * The default EEPROM size is 2048 * 16 bit words.
149 * Later EEPROM/OTP/flash regions may be quite a bit bigger.
151 if (resource_int_value(device_get_name(dev), device_get_unit(dev),
152 "eepromsize", &eepromsize) != 0) {
153 eepromsize = ATH_EEPROM_DATA_SIZE * 2;
157 device_printf(sc->sc_dev, "eeprom @ %p (%d bytes)\n",
158 (void *) eepromaddr, eepromsize);
160 * XXX this assumes that the parent device is the nexus
161 * and will just pass through requests for all of memory.
163 * Later on, when this has to attach off of the actual
164 * AHB, this won't work.
166 * Ideally this would be done in machdep code in mips/atheros/
167 * and it'd expose the EEPROM via the firmware interface,
168 * so the ath/ath_ahb drivers can be loaded as modules
171 psc->sc_eeprom = bus_alloc_resource(dev, SYS_RES_MEMORY,
172 &rid, (uintptr_t) eepromaddr,
173 (uintptr_t) eepromaddr + (uintptr_t) (eepromsize - 1), 0, RF_ACTIVE);
174 if (psc->sc_eeprom == NULL) {
175 device_printf(dev, "cannot map eeprom space\n");
179 sc->sc_st = (HAL_BUS_TAG) rman_get_bustag(psc->sc_sr);
180 sc->sc_sh = (HAL_BUS_HANDLE) rman_get_bushandle(psc->sc_sr);
182 * Mark device invalid so any interrupts (shared or otherwise)
183 * that arrive before the HAL is setup are discarded.
187 /* Copy the EEPROM data out */
188 sc->sc_eepromdata = malloc(eepromsize, M_TEMP, M_NOWAIT | M_ZERO);
189 if (sc->sc_eepromdata == NULL) {
190 device_printf(dev, "cannot allocate memory for eeprom data\n");
193 device_printf(sc->sc_dev, "eeprom data @ %p\n", (void *) rman_get_bushandle(psc->sc_eeprom));
194 /* XXX why doesn't this work? -adrian */
196 bus_space_read_multi_1(
197 rman_get_bustag(psc->sc_eeprom),
198 rman_get_bushandle(psc->sc_eeprom),
199 0, (u_int8_t *) sc->sc_eepromdata, eepromsize);
201 p = (void *) rman_get_bushandle(psc->sc_eeprom);
202 memcpy(sc->sc_eepromdata, p, eepromsize);
205 * Arrange interrupt line.
208 psc->sc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_SHAREABLE|RF_ACTIVE);
209 if (psc->sc_irq == NULL) {
210 device_printf(dev, "could not map interrupt\n");
213 if (bus_setup_intr(dev, psc->sc_irq,
214 INTR_TYPE_NET | INTR_MPSAFE,
215 NULL, ath_intr, sc, &psc->sc_ih)) {
216 device_printf(dev, "could not establish interrupt\n");
221 * Setup DMA descriptor area.
223 if (bus_dma_tag_create(bus_get_dma_tag(dev), /* parent */
224 1, 0, /* alignment, bounds */
225 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
226 BUS_SPACE_MAXADDR, /* highaddr */
227 NULL, NULL, /* filter, filterarg */
228 0x3ffff, /* maxsize XXX */
229 ATH_MAX_SCATTER, /* nsegments */
230 0x3ffff, /* maxsegsize XXX */
231 BUS_DMA_ALLOCNOW, /* flags */
235 device_printf(dev, "cannot allocate DMA tag\n");
240 * Check if a device/vendor ID is provided in hints.
242 if (resource_int_value(device_get_name(dev), device_get_unit(dev),
243 "vendor_id", &vendor_id) != 0) {
244 vendor_id = VENDOR_ATHEROS;
247 if (resource_int_value(device_get_name(dev), device_get_unit(dev),
248 "device_id", &device_id) != 0) {
249 device_id = AR9130_DEVID;
253 ATH_PCU_LOCK_INIT(sc);
254 ATH_RX_LOCK_INIT(sc);
255 ATH_TX_LOCK_INIT(sc);
256 ATH_TX_IC_LOCK_INIT(sc);
257 ATH_TXSTATUS_LOCK_INIT(sc);
259 error = ath_attach(device_id, sc);
260 if (error == 0) /* success */
263 ATH_TXSTATUS_LOCK_DESTROY(sc);
264 ATH_RX_LOCK_DESTROY(sc);
265 ATH_TX_LOCK_DESTROY(sc);
266 ATH_TX_IC_LOCK_DESTROY(sc);
267 ATH_PCU_LOCK_DESTROY(sc);
268 ATH_LOCK_DESTROY(sc);
269 bus_dma_tag_destroy(sc->sc_dmat);
271 bus_teardown_intr(dev, psc->sc_irq, psc->sc_ih);
273 bus_release_resource(dev, SYS_RES_IRQ, 0, psc->sc_irq);
275 bus_release_resource(dev, SYS_RES_MEMORY, 0, psc->sc_eeprom);
277 bus_release_resource(dev, SYS_RES_MEMORY, 0, psc->sc_sr);
280 if (sc->sc_eepromdata)
281 free(sc->sc_eepromdata, M_TEMP);
286 ath_ahb_detach(device_t dev)
288 struct ath_ahb_softc *psc = device_get_softc(dev);
289 struct ath_softc *sc = &psc->sc_sc;
291 /* check if device was removed */
292 sc->sc_invalid = !bus_child_present(dev);
296 bus_generic_detach(dev);
297 bus_teardown_intr(dev, psc->sc_irq, psc->sc_ih);
298 bus_release_resource(dev, SYS_RES_IRQ, 0, psc->sc_irq);
300 bus_dma_tag_destroy(sc->sc_dmat);
301 bus_release_resource(dev, SYS_RES_MEMORY, 0, psc->sc_sr);
302 bus_release_resource(dev, SYS_RES_MEMORY, 0, psc->sc_eeprom);
304 if (sc->sc_eepromdata)
305 free(sc->sc_eepromdata, M_TEMP);
307 ATH_TXSTATUS_LOCK_DESTROY(sc);
308 ATH_RX_LOCK_DESTROY(sc);
309 ATH_TX_LOCK_DESTROY(sc);
310 ATH_TX_IC_LOCK_DESTROY(sc);
311 ATH_PCU_LOCK_DESTROY(sc);
312 ATH_LOCK_DESTROY(sc);
318 ath_ahb_shutdown(device_t dev)
320 struct ath_ahb_softc *psc = device_get_softc(dev);
322 ath_shutdown(&psc->sc_sc);
327 ath_ahb_suspend(device_t dev)
329 struct ath_ahb_softc *psc = device_get_softc(dev);
331 ath_suspend(&psc->sc_sc);
337 ath_ahb_resume(device_t dev)
339 struct ath_ahb_softc *psc = device_get_softc(dev);
341 ath_resume(&psc->sc_sc);
346 static device_method_t ath_ahb_methods[] = {
347 /* Device interface */
348 DEVMETHOD(device_probe, ath_ahb_probe),
349 DEVMETHOD(device_attach, ath_ahb_attach),
350 DEVMETHOD(device_detach, ath_ahb_detach),
351 DEVMETHOD(device_shutdown, ath_ahb_shutdown),
352 DEVMETHOD(device_suspend, ath_ahb_suspend),
353 DEVMETHOD(device_resume, ath_ahb_resume),
357 static driver_t ath_ahb_driver = {
360 sizeof (struct ath_ahb_softc)
362 static devclass_t ath_devclass;
363 DRIVER_MODULE(ath, nexus, ath_ahb_driver, ath_devclass, 0, 0);
364 DRIVER_MODULE(ath, apb, ath_ahb_driver, ath_devclass, 0, 0);
365 MODULE_VERSION(ath, 1);
366 MODULE_DEPEND(ath, wlan, 1, 1, 1); /* 802.11 media layer */
367 MODULE_DEPEND(ath, if_ath, 1, 1, 1); /* if_ath driver */