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