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