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