xref: /freebsd-13-stable/sys/dev/ath/if_ath_ahb.c (revision 3bc80996974a61a4223eae4c1ccd47b6ee32a48a)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
5  * Copyright (c) 2010-2011 Adrian Chadd, Xenion Pty Ltd
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer,
13  *    without modification.
14  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
15  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
16  *    redistribution must be conditioned upon including a substantially
17  *    similar Disclaimer requirement for further binary redistribution.
18  *
19  * NO WARRANTY
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
23  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
24  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
25  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
28  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30  * THE POSSIBILITY OF SUCH DAMAGES.
31  */
32 
33 #include <sys/cdefs.h>
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 /* For EEPROM firmware */
70 #ifdef	ATH_EEPROM_FIRMWARE
71 #include <sys/linker.h>
72 #include <sys/firmware.h>
73 #endif	/* ATH_EEPROM_FIRMWARE */
74 
75 /*
76  * bus glue.
77  */
78 
79 /* number of 16 bit words */
80 #define	ATH_EEPROM_DATA_SIZE	2048
81 
82 struct ath_ahb_softc {
83 	struct ath_softc	sc_sc;
84 	struct resource		*sc_sr;		/* memory resource */
85 	struct resource		*sc_irq;	/* irq resource */
86 	void			*sc_ih;		/* interrupt handler */
87 };
88 
89 #define	VENDOR_ATHEROS	0x168c
90 #define	AR9130_DEVID	0x000b
91 
92 static int
ath_ahb_probe(device_t dev)93 ath_ahb_probe(device_t dev)
94 {
95 	int vendor_id, device_id;
96 	const char* devname;
97 
98 	/*
99 	 * Check if a device/vendor ID is provided in hints.
100 	 */
101 	if (resource_int_value(device_get_name(dev), device_get_unit(dev),
102 	    "vendor_id", &vendor_id) != 0) {
103 		vendor_id = VENDOR_ATHEROS;
104 	}
105 
106 	if (resource_int_value(device_get_name(dev), device_get_unit(dev),
107 	    "device_id", &device_id) != 0) {
108 		device_id = AR9130_DEVID;
109 	}
110 
111 	device_printf(dev, "Vendor=0x%04x, Device=0x%04x\n",
112 	    vendor_id & 0xffff,
113 	    device_id & 0xffff);
114 
115 	/* Attempt to probe */
116 	devname = ath_hal_probe(vendor_id, device_id);
117 
118 	if (devname != NULL) {
119 		device_set_desc(dev, devname);
120 		return BUS_PROBE_DEFAULT;
121 	}
122 	return ENXIO;
123 }
124 
125 static void
ath_ahb_intr(void * arg)126 ath_ahb_intr(void *arg)
127 {
128 	/* XXX TODO: check if its ours! */
129 	ar71xx_device_flush_ddr(AR71XX_CPU_DDR_FLUSH_WMAC);
130 	ath_intr(arg);
131 }
132 
133 static int
ath_ahb_attach(device_t dev)134 ath_ahb_attach(device_t dev)
135 {
136 	struct ath_ahb_softc *psc = device_get_softc(dev);
137 	struct ath_softc *sc = &psc->sc_sc;
138 	int error = ENXIO;
139 	int rid;
140 	int device_id, vendor_id;
141 #ifdef	ATH_EEPROM_FIRMWARE
142 	const struct firmware *fw = NULL;
143 	const char *buf;
144 #endif
145 
146 	sc->sc_dev = dev;
147 
148 	rid = 0;
149 	psc->sc_sr = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
150 	if (psc->sc_sr == NULL) {
151 		device_printf(dev, "cannot map register space\n");
152 		goto bad;
153 	}
154 
155 	sc->sc_st = (HAL_BUS_TAG) rman_get_bustag(psc->sc_sr);
156 	sc->sc_sh = (HAL_BUS_HANDLE) rman_get_bushandle(psc->sc_sr);
157 	/*
158 	 * Mark device invalid so any interrupts (shared or otherwise)
159 	 * that arrive before the HAL is setup are discarded.
160 	 */
161 	sc->sc_invalid = 1;
162 
163 #ifdef	ATH_EEPROM_FIRMWARE
164 	/*
165 	 * If there's an EEPROM firmware image, load that in.
166 	 */
167 	if (resource_string_value(device_get_name(dev), device_get_unit(dev),
168 	    "eeprom_firmware", &buf) == 0) {
169 		device_printf(dev, "%s: looking up firmware @ '%s'\n",
170 		    __func__, buf);
171 
172 		fw = firmware_get(buf);
173 		if (fw == NULL) {
174 			device_printf(dev, "%s: couldn't find firmware\n",
175 			    __func__);
176 			goto bad1;
177 		}
178 
179 		device_printf(dev, "%s: EEPROM firmware @ %p\n",
180 		    __func__, fw->data);
181 		sc->sc_eepromdata =
182 		    malloc(fw->datasize, M_TEMP, M_WAITOK | M_ZERO);
183 		if (! sc->sc_eepromdata) {
184 			device_printf(dev, "%s: can't malloc eepromdata\n",
185 			    __func__);
186 			goto bad1;
187 		}
188 		memcpy(sc->sc_eepromdata, fw->data, fw->datasize);
189 		firmware_put(fw, 0);
190 	}
191 #endif	/* ATH_EEPROM_FIRMWARE */
192 
193 	/*
194 	 * Arrange interrupt line.
195 	 */
196 	rid = 0;
197 	psc->sc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_SHAREABLE|RF_ACTIVE);
198 	if (psc->sc_irq == NULL) {
199 		device_printf(dev, "could not map interrupt\n");
200 		goto bad1;
201 	}
202 	if (bus_setup_intr(dev, psc->sc_irq,
203 			   INTR_TYPE_NET | INTR_MPSAFE,
204 			   NULL, ath_ahb_intr, sc, &psc->sc_ih)) {
205 		device_printf(dev, "could not establish interrupt\n");
206 		goto bad2;
207 	}
208 
209 	/*
210 	 * Setup DMA descriptor area.
211 	 */
212 	if (bus_dma_tag_create(bus_get_dma_tag(dev),	/* parent */
213 			       1, 0,			/* alignment, bounds */
214 			       BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
215 			       BUS_SPACE_MAXADDR,	/* highaddr */
216 			       NULL, NULL,		/* filter, filterarg */
217 			       0x3ffff,			/* maxsize XXX */
218 			       ATH_MAX_SCATTER,		/* nsegments */
219 			       0x3ffff,			/* maxsegsize XXX */
220 			       BUS_DMA_ALLOCNOW,	/* flags */
221 			       NULL,			/* lockfunc */
222 			       NULL,			/* lockarg */
223 			       &sc->sc_dmat)) {
224 		device_printf(dev, "cannot allocate DMA tag\n");
225 		goto bad3;
226 	}
227 
228 	/*
229 	 * Check if a device/vendor ID is provided in hints.
230 	 */
231 	if (resource_int_value(device_get_name(dev), device_get_unit(dev),
232 	    "vendor_id", &vendor_id) != 0) {
233 		vendor_id = VENDOR_ATHEROS;
234 	}
235 
236 	if (resource_int_value(device_get_name(dev), device_get_unit(dev),
237 	    "device_id", &device_id) != 0) {
238 		device_id = AR9130_DEVID;
239 	}
240 
241 	ATH_LOCK_INIT(sc);
242 	ATH_PCU_LOCK_INIT(sc);
243 	ATH_RX_LOCK_INIT(sc);
244 	ATH_TX_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_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_sr);
263 bad:
264 	/* XXX?! */
265 	if (sc->sc_eepromdata)
266 		free(sc->sc_eepromdata, M_TEMP);
267 	return (error);
268 }
269 
270 static int
ath_ahb_detach(device_t dev)271 ath_ahb_detach(device_t dev)
272 {
273 	struct ath_ahb_softc *psc = device_get_softc(dev);
274 	struct ath_softc *sc = &psc->sc_sc;
275 
276 	/* check if device was removed */
277 	sc->sc_invalid = !bus_child_present(dev);
278 
279 	ath_detach(sc);
280 
281 	bus_generic_detach(dev);
282 	bus_teardown_intr(dev, psc->sc_irq, psc->sc_ih);
283 	bus_release_resource(dev, SYS_RES_IRQ, 0, psc->sc_irq);
284 
285 	bus_dma_tag_destroy(sc->sc_dmat);
286 	bus_release_resource(dev, SYS_RES_MEMORY, 0, psc->sc_sr);
287 	/* XXX?! */
288 	if (sc->sc_eepromdata)
289 		free(sc->sc_eepromdata, M_TEMP);
290 
291 	ATH_TXSTATUS_LOCK_DESTROY(sc);
292 	ATH_RX_LOCK_DESTROY(sc);
293 	ATH_TX_LOCK_DESTROY(sc);
294 	ATH_PCU_LOCK_DESTROY(sc);
295 	ATH_LOCK_DESTROY(sc);
296 
297 	return (0);
298 }
299 
300 static int
ath_ahb_shutdown(device_t dev)301 ath_ahb_shutdown(device_t dev)
302 {
303 	struct ath_ahb_softc *psc = device_get_softc(dev);
304 
305 	ath_shutdown(&psc->sc_sc);
306 	return (0);
307 }
308 
309 static int
ath_ahb_suspend(device_t dev)310 ath_ahb_suspend(device_t dev)
311 {
312 	struct ath_ahb_softc *psc = device_get_softc(dev);
313 
314 	ath_suspend(&psc->sc_sc);
315 
316 	return (0);
317 }
318 
319 static int
ath_ahb_resume(device_t dev)320 ath_ahb_resume(device_t dev)
321 {
322 	struct ath_ahb_softc *psc = device_get_softc(dev);
323 
324 	ath_resume(&psc->sc_sc);
325 
326 	return (0);
327 }
328 
329 static device_method_t ath_ahb_methods[] = {
330 	/* Device interface */
331 	DEVMETHOD(device_probe,		ath_ahb_probe),
332 	DEVMETHOD(device_attach,	ath_ahb_attach),
333 	DEVMETHOD(device_detach,	ath_ahb_detach),
334 	DEVMETHOD(device_shutdown,	ath_ahb_shutdown),
335 	DEVMETHOD(device_suspend,	ath_ahb_suspend),
336 	DEVMETHOD(device_resume,	ath_ahb_resume),
337 	{ 0,0 }
338 };
339 static driver_t ath_ahb_driver = {
340 	"ath",
341 	ath_ahb_methods,
342 	sizeof (struct ath_ahb_softc)
343 };
344 static	devclass_t ath_devclass;
345 DRIVER_MODULE(if_ath_ahb, nexus, ath_ahb_driver, ath_devclass, 0, 0);
346 DRIVER_MODULE(if_ath_ahb, apb, ath_ahb_driver, ath_devclass, 0, 0);
347 MODULE_VERSION(if_ath_ahb, 1);
348 MODULE_DEPEND(if_ath_ahb, wlan, 1, 1, 1);		/* 802.11 media layer */
349 MODULE_DEPEND(if_ath_ahb, ath_main, 1, 1, 1);		/* if_ath driver */
350 MODULE_DEPEND(if_ath_ahb, ath_hal, 1, 1, 1);		/* ath HAL */
351