ath - Basic #include adjustments
[dragonfly.git] / sys / dev / netif / ath / ath / if_ath_ahb.c
1 /*-
2  * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
3  * Copyright (c) 2010-2011 Adrian Chadd, Xenion Pty Ltd
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
16  *
17  * NO WARRANTY
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.
29  */
30
31 #include <sys/cdefs.h>
32
33 /*
34  * AHB bus front-end for the Atheros Wireless LAN controller driver.
35  */
36
37 #include "opt_ath.h"
38
39 #include <sys/param.h>
40 #include <sys/systm.h> 
41 #include <sys/malloc.h>
42 #include <sys/module.h>
43 #include <sys/kernel.h>
44 #include <sys/lock.h>
45 #include <sys/mutex.h>
46 #include <sys/errno.h>
47
48 #include <sys/bus.h>
49 #include <sys/rman.h>
50
51 #include <sys/socket.h>
52  
53 #include <net/if.h>
54 #include <net/if_media.h>
55 #include <net/if_arp.h>
56 #include <net/ethernet.h>
57
58 #include <netproto/802_11/ieee80211_var.h>
59
60 #include <dev/netif/ath/ath/if_athvar.h>
61
62 #include <mips/atheros/ar71xxreg.h>
63 #include <mips/atheros/ar91xxreg.h>
64 #include <mips/atheros/ar71xx_cpudef.h>
65
66 #include <machine/resource.h>
67
68 /*
69  * bus glue.
70  */
71
72 /* number of 16 bit words */
73 #define ATH_EEPROM_DATA_SIZE    2048
74
75 struct ath_ahb_softc {
76         struct ath_softc        sc_sc;
77         struct resource         *sc_sr;         /* memory resource */
78         struct resource         *sc_irq;        /* irq resource */
79         struct resource         *sc_eeprom;     /* eeprom location */
80         void                    *sc_ih;         /* interrupt handler */
81 };
82
83 #define VENDOR_ATHEROS  0x168c
84 #define AR9130_DEVID    0x000b
85
86 static int
87 ath_ahb_probe(device_t dev)
88 {
89         int vendor_id, device_id;
90         const char* devname;
91
92         /*
93          * Check if a device/vendor ID is provided in hints.
94          */
95         if (resource_int_value(device_get_name(dev), device_get_unit(dev),
96             "vendor_id", &vendor_id) != 0) {
97                 vendor_id = VENDOR_ATHEROS;
98         }
99
100         if (resource_int_value(device_get_name(dev), device_get_unit(dev),
101             "device_id", &device_id) != 0) {
102                 device_id = AR9130_DEVID;
103         }
104
105         device_printf(dev, "Vendor=0x%04x, Device=0x%04x\n",
106             vendor_id & 0xffff,
107             device_id & 0xffff);
108
109         /* Attempt to probe */
110         devname = ath_hal_probe(vendor_id, device_id);
111
112         if (devname != NULL) {
113                 device_set_desc(dev, devname);
114                 return BUS_PROBE_DEFAULT;
115         }
116         return ENXIO;
117 }
118
119 static int
120 ath_ahb_attach(device_t dev)
121 {
122         struct ath_ahb_softc *psc = device_get_softc(dev);
123         struct ath_softc *sc = &psc->sc_sc;
124         int error = ENXIO;
125         int rid;
126         long eepromaddr;
127         int eepromsize;
128         uint8_t *p;
129         int device_id, vendor_id;
130
131         sc->sc_dev = dev;
132
133         rid = 0;
134         psc->sc_sr = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
135         if (psc->sc_sr == NULL) {
136                 device_printf(dev, "cannot map register space\n");
137                 goto bad;
138         }
139
140         if (resource_long_value(device_get_name(dev), device_get_unit(dev),
141             "eepromaddr", &eepromaddr) != 0) {
142                 device_printf(dev, "cannot fetch 'eepromaddr' from hints\n");
143                 goto bad0;
144         }
145
146         /*
147          * The default EEPROM size is 2048 * 16 bit words.
148          * Later EEPROM/OTP/flash regions may be quite a bit bigger.
149          */
150         if (resource_int_value(device_get_name(dev), device_get_unit(dev),
151             "eepromsize", &eepromsize) != 0) {
152                 eepromsize = ATH_EEPROM_DATA_SIZE * 2;
153         }
154
155
156         rid = 0;
157         device_printf(sc->sc_dev, "eeprom @ %p (%d bytes)\n",
158             (void *) eepromaddr, eepromsize);
159         psc->sc_eeprom = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid, (uintptr_t) eepromaddr,
160           (uintptr_t) eepromaddr + (uintptr_t) (eepromsize - 1), 0, RF_ACTIVE);
161         if (psc->sc_eeprom == NULL) {
162                 device_printf(dev, "cannot map eeprom space\n");
163                 goto bad0;
164         }
165
166         /* XXX uintptr_t is a bandaid for ia64; to be fixed */
167         sc->sc_st = (HAL_BUS_TAG)(uintptr_t) rman_get_bustag(psc->sc_sr);
168         sc->sc_sh = (HAL_BUS_HANDLE) rman_get_bushandle(psc->sc_sr);
169         /*
170          * Mark device invalid so any interrupts (shared or otherwise)
171          * that arrive before the HAL is setup are discarded.
172          */
173         sc->sc_invalid = 1;
174
175         /* Copy the EEPROM data out */
176         sc->sc_eepromdata = malloc(eepromsize, M_TEMP, M_NOWAIT | M_ZERO);
177         if (sc->sc_eepromdata == NULL) {
178                 device_printf(dev, "cannot allocate memory for eeprom data\n");
179                 goto bad1;
180         }
181         device_printf(sc->sc_dev, "eeprom data @ %p\n", (void *) rman_get_bushandle(psc->sc_eeprom));
182         /* XXX why doesn't this work? -adrian */
183 #if 0
184         bus_space_read_multi_1(
185             rman_get_bustag(psc->sc_eeprom),
186             rman_get_bushandle(psc->sc_eeprom),
187             0, (u_int8_t *) sc->sc_eepromdata, eepromsize);
188 #endif
189         p = (void *) rman_get_bushandle(psc->sc_eeprom);
190         memcpy(sc->sc_eepromdata, p, eepromsize);
191
192         /*
193          * Arrange interrupt line.
194          */
195         rid = 0;
196         psc->sc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_SHAREABLE|RF_ACTIVE);
197         if (psc->sc_irq == NULL) {
198                 device_printf(dev, "could not map interrupt\n");
199                 goto bad1;
200         }
201         if (bus_setup_intr(dev, psc->sc_irq,
202                            INTR_TYPE_NET | INTR_MPSAFE,
203                            NULL, ath_intr, sc, &psc->sc_ih)) {
204                 device_printf(dev, "could not establish interrupt\n");
205                 goto bad2;
206         }
207
208         /*
209          * Setup DMA descriptor area.
210          */
211         if (bus_dma_tag_create(bus_get_dma_tag(dev),    /* parent */
212                                1, 0,                    /* alignment, bounds */
213                                BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
214                                BUS_SPACE_MAXADDR,       /* highaddr */
215                                NULL, NULL,              /* filter, filterarg */
216                                0x3ffff,                 /* maxsize XXX */
217                                ATH_MAX_SCATTER,         /* nsegments */
218                                0x3ffff,                 /* maxsegsize XXX */
219                                BUS_DMA_ALLOCNOW,        /* flags */
220                                NULL,                    /* lockfunc */
221                                NULL,                    /* lockarg */
222                                &sc->sc_dmat)) {
223                 device_printf(dev, "cannot allocate DMA tag\n");
224                 goto bad3;
225         }
226
227         /*
228          * Check if a device/vendor ID is provided in hints.
229          */
230         if (resource_int_value(device_get_name(dev), device_get_unit(dev),
231             "vendor_id", &vendor_id) != 0) {
232                 vendor_id = VENDOR_ATHEROS;
233         }
234
235         if (resource_int_value(device_get_name(dev), device_get_unit(dev),
236             "device_id", &device_id) != 0) {
237                 device_id = AR9130_DEVID;
238         }
239
240         ATH_LOCK_INIT(sc);
241         ATH_PCU_LOCK_INIT(sc);
242         ATH_RX_LOCK_INIT(sc);
243         ATH_TX_LOCK_INIT(sc);
244         ATH_TX_IC_LOCK_INIT(sc);
245         ATH_TXSTATUS_LOCK_INIT(sc);
246
247         error = ath_attach(device_id, sc);
248         if (error == 0)                                 /* success */
249                 return 0;
250
251         ATH_TXSTATUS_LOCK_DESTROY(sc);
252         ATH_RX_LOCK_DESTROY(sc);
253         ATH_TX_LOCK_DESTROY(sc);
254         ATH_TX_IC_LOCK_DESTROY(sc);
255         ATH_PCU_LOCK_DESTROY(sc);
256         ATH_LOCK_DESTROY(sc);
257         bus_dma_tag_destroy(sc->sc_dmat);
258 bad3:
259         bus_teardown_intr(dev, psc->sc_irq, psc->sc_ih);
260 bad2:
261         bus_release_resource(dev, SYS_RES_IRQ, 0, psc->sc_irq);
262 bad1:
263         bus_release_resource(dev, SYS_RES_MEMORY, 0, psc->sc_eeprom);
264 bad0:
265         bus_release_resource(dev, SYS_RES_MEMORY, 0, psc->sc_sr);
266 bad:
267         /* XXX?! */
268         if (sc->sc_eepromdata)
269                 free(sc->sc_eepromdata, M_TEMP);
270         return (error);
271 }
272
273 static int
274 ath_ahb_detach(device_t dev)
275 {
276         struct ath_ahb_softc *psc = device_get_softc(dev);
277         struct ath_softc *sc = &psc->sc_sc;
278
279         /* check if device was removed */
280         sc->sc_invalid = !bus_child_present(dev);
281
282         ath_detach(sc);
283
284         bus_generic_detach(dev);
285         bus_teardown_intr(dev, psc->sc_irq, psc->sc_ih);
286         bus_release_resource(dev, SYS_RES_IRQ, 0, psc->sc_irq);
287
288         bus_dma_tag_destroy(sc->sc_dmat);
289         bus_release_resource(dev, SYS_RES_MEMORY, 0, psc->sc_sr);
290         bus_release_resource(dev, SYS_RES_MEMORY, 0, psc->sc_eeprom);
291         /* XXX?! */
292         if (sc->sc_eepromdata)
293                 free(sc->sc_eepromdata, M_TEMP);
294
295         ATH_TXSTATUS_LOCK_DESTROY(sc);
296         ATH_RX_LOCK_DESTROY(sc);
297         ATH_TX_LOCK_DESTROY(sc);
298         ATH_TX_IC_LOCK_DESTROY(sc);
299         ATH_PCU_LOCK_DESTROY(sc);
300         ATH_LOCK_DESTROY(sc);
301
302         return (0);
303 }
304
305 static int
306 ath_ahb_shutdown(device_t dev)
307 {
308         struct ath_ahb_softc *psc = device_get_softc(dev);
309
310         ath_shutdown(&psc->sc_sc);
311         return (0);
312 }
313
314 static int
315 ath_ahb_suspend(device_t dev)
316 {
317         struct ath_ahb_softc *psc = device_get_softc(dev);
318
319         ath_suspend(&psc->sc_sc);
320
321         return (0);
322 }
323
324 static int
325 ath_ahb_resume(device_t dev)
326 {
327         struct ath_ahb_softc *psc = device_get_softc(dev);
328
329         ath_resume(&psc->sc_sc);
330
331         return (0);
332 }
333
334 static device_method_t ath_ahb_methods[] = {
335         /* Device interface */
336         DEVMETHOD(device_probe,         ath_ahb_probe),
337         DEVMETHOD(device_attach,        ath_ahb_attach),
338         DEVMETHOD(device_detach,        ath_ahb_detach),
339         DEVMETHOD(device_shutdown,      ath_ahb_shutdown),
340         DEVMETHOD(device_suspend,       ath_ahb_suspend),
341         DEVMETHOD(device_resume,        ath_ahb_resume),
342
343         { 0,0 }
344 };
345 static driver_t ath_ahb_driver = {
346         "ath",
347         ath_ahb_methods,
348         sizeof (struct ath_ahb_softc)
349 };
350 static  devclass_t ath_devclass;
351 DRIVER_MODULE(ath, nexus, ath_ahb_driver, ath_devclass, 0, 0);
352 MODULE_VERSION(ath, 1);
353 MODULE_DEPEND(ath, wlan, 1, 1, 1);              /* 802.11 media layer */
354 MODULE_DEPEND(ath, if_ath, 1, 1, 1);            /* if_ath driver */