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: stable/9/sys/dev/ath/if_ath_ahb.c 223032 2011-06-13 04:31:57Z adrian $");
33 
34 /*
35  * AHB bus front-end for the Atheros Wireless LAN controller driver.
36  */
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/module.h>
41 #include <sys/kernel.h>
42 #include <sys/lock.h>
43 #include <sys/mutex.h>
44 #include <sys/errno.h>
45 
46 #include <machine/bus.h>
47 #include <machine/resource.h>
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 
57 #include <net80211/ieee80211_var.h>
58 
59 #include <dev/ath/if_athvar.h>
60 
61 #include <mips/atheros/ar71xxreg.h>
62 #include <mips/atheros/ar91xxreg.h>
63 #include <mips/atheros/ar71xx_cpudef.h>
64 
65 /*
66  * bus glue.
67  */
68 
69 /* number of 16 bit words */
70 #define	ATH_EEPROM_DATA_SIZE	2048
71 
72 struct ath_ahb_softc {
73 	struct ath_softc	sc_sc;
74 	struct resource		*sc_sr;		/* memory resource */
75 	struct resource		*sc_irq;	/* irq resource */
76 	struct resource		*sc_eeprom;	/* eeprom location */
77 	void			*sc_ih;		/* interrupt handler */
78 };
79 
80 #define	VENDOR_ATHEROS	0x168c
81 #define	AR9130_DEVID	0x000b
82 
83 static int
ath_ahb_probe(device_t dev)84 ath_ahb_probe(device_t dev)
85 {
86 	const char* devname;
87 
88 	/* Atheros / ar9130 */
89 	devname = ath_hal_probe(VENDOR_ATHEROS, AR9130_DEVID);
90 
91 	if (devname != NULL) {
92 		device_set_desc(dev, devname);
93 		return BUS_PROBE_DEFAULT;
94 	}
95 	return ENXIO;
96 }
97 
98 static int
ath_ahb_attach(device_t dev)99 ath_ahb_attach(device_t dev)
100 {
101 	struct ath_ahb_softc *psc = device_get_softc(dev);
102 	struct ath_softc *sc = &psc->sc_sc;
103 	int error = ENXIO;
104 	int rid;
105 	long eepromaddr;
106 	uint8_t *p;
107 
108 	sc->sc_dev = dev;
109 
110 	rid = 0;
111 	psc->sc_sr = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
112 	if (psc->sc_sr == NULL) {
113 		device_printf(dev, "cannot map register space\n");
114 		goto bad;
115 	}
116 
117         if (resource_long_value(device_get_name(dev), device_get_unit(dev),
118             "eepromaddr", &eepromaddr) != 0) {
119 		device_printf(dev, "cannot fetch 'eepromaddr' from hints\n");
120 		goto bad0;
121         }
122 	rid = 0;
123 	device_printf(sc->sc_dev, "eeprom @ %p\n", (void *) eepromaddr);
124 	psc->sc_eeprom = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid, (uintptr_t) eepromaddr,
125 	  (uintptr_t) eepromaddr + (uintptr_t) ((ATH_EEPROM_DATA_SIZE * 2) - 1), 0, RF_ACTIVE);
126 	if (psc->sc_eeprom == NULL) {
127 		device_printf(dev, "cannot map eeprom space\n");
128 		goto bad0;
129 	}
130 
131 	/* XXX uintptr_t is a bandaid for ia64; to be fixed */
132 	sc->sc_st = (HAL_BUS_TAG)(uintptr_t) rman_get_bustag(psc->sc_sr);
133 	sc->sc_sh = (HAL_BUS_HANDLE) rman_get_bushandle(psc->sc_sr);
134 	/*
135 	 * Mark device invalid so any interrupts (shared or otherwise)
136 	 * that arrive before the HAL is setup are discarded.
137 	 */
138 	sc->sc_invalid = 1;
139 
140 	/* Copy the EEPROM data out */
141 	sc->sc_eepromdata = malloc(ATH_EEPROM_DATA_SIZE * 2, M_TEMP, M_NOWAIT | M_ZERO);
142 	if (sc->sc_eepromdata == NULL) {
143 		device_printf(dev, "cannot allocate memory for eeprom data\n");
144 		goto bad1;
145 	}
146 	device_printf(sc->sc_dev, "eeprom data @ %p\n", (void *) rman_get_bushandle(psc->sc_eeprom));
147 	/* XXX why doesn't this work? -adrian */
148 #if 0
149 	bus_space_read_multi_1(
150 	    rman_get_bustag(psc->sc_eeprom),
151 	    rman_get_bushandle(psc->sc_eeprom),
152 	    0, (u_int8_t *) sc->sc_eepromdata, ATH_EEPROM_DATA_SIZE * 2);
153 #endif
154 	p = (void *) rman_get_bushandle(psc->sc_eeprom);
155 	memcpy(sc->sc_eepromdata, p, ATH_EEPROM_DATA_SIZE * 2);
156 
157 	/*
158 	 * Arrange interrupt line.
159 	 */
160 	rid = 0;
161 	psc->sc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_SHAREABLE|RF_ACTIVE);
162 	if (psc->sc_irq == NULL) {
163 		device_printf(dev, "could not map interrupt\n");
164 		goto bad1;
165 	}
166 	if (bus_setup_intr(dev, psc->sc_irq,
167 			   INTR_TYPE_NET | INTR_MPSAFE,
168 			   NULL, ath_intr, sc, &psc->sc_ih)) {
169 		device_printf(dev, "could not establish interrupt\n");
170 		goto bad2;
171 	}
172 
173 	/*
174 	 * Setup DMA descriptor area.
175 	 */
176 	if (bus_dma_tag_create(bus_get_dma_tag(dev),	/* parent */
177 			       1, 0,			/* alignment, bounds */
178 			       BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
179 			       BUS_SPACE_MAXADDR,	/* highaddr */
180 			       NULL, NULL,		/* filter, filterarg */
181 			       0x3ffff,			/* maxsize XXX */
182 			       ATH_MAX_SCATTER,		/* nsegments */
183 			       0x3ffff,			/* maxsegsize XXX */
184 			       BUS_DMA_ALLOCNOW,	/* flags */
185 			       NULL,			/* lockfunc */
186 			       NULL,			/* lockarg */
187 			       &sc->sc_dmat)) {
188 		device_printf(dev, "cannot allocate DMA tag\n");
189 		goto bad3;
190 	}
191 
192 	ATH_LOCK_INIT(sc);
193 
194 	error = ath_attach(AR9130_DEVID, sc);
195 	if (error == 0)					/* success */
196 		return 0;
197 
198 	ATH_LOCK_DESTROY(sc);
199 	bus_dma_tag_destroy(sc->sc_dmat);
200 bad3:
201 	bus_teardown_intr(dev, psc->sc_irq, psc->sc_ih);
202 bad2:
203 	bus_release_resource(dev, SYS_RES_IRQ, 0, psc->sc_irq);
204 bad1:
205 	bus_release_resource(dev, SYS_RES_MEMORY, 0, psc->sc_eeprom);
206 bad0:
207 	bus_release_resource(dev, SYS_RES_MEMORY, 0, psc->sc_sr);
208 bad:
209 	/* XXX?! */
210 	if (sc->sc_eepromdata)
211 		free(sc->sc_eepromdata, M_TEMP);
212 	return (error);
213 }
214 
215 static int
ath_ahb_detach(device_t dev)216 ath_ahb_detach(device_t dev)
217 {
218 	struct ath_ahb_softc *psc = device_get_softc(dev);
219 	struct ath_softc *sc = &psc->sc_sc;
220 
221 	/* check if device was removed */
222 	sc->sc_invalid = !bus_child_present(dev);
223 
224 	ath_detach(sc);
225 
226 	bus_generic_detach(dev);
227 	bus_teardown_intr(dev, psc->sc_irq, psc->sc_ih);
228 	bus_release_resource(dev, SYS_RES_IRQ, 0, psc->sc_irq);
229 
230 	bus_dma_tag_destroy(sc->sc_dmat);
231 	bus_release_resource(dev, SYS_RES_MEMORY, 0, psc->sc_sr);
232 	bus_release_resource(dev, SYS_RES_MEMORY, 0, psc->sc_eeprom);
233 	/* XXX?! */
234 	if (sc->sc_eepromdata)
235 		free(sc->sc_eepromdata, M_TEMP);
236 
237 	ATH_LOCK_DESTROY(sc);
238 
239 	return (0);
240 }
241 
242 static int
ath_ahb_shutdown(device_t dev)243 ath_ahb_shutdown(device_t dev)
244 {
245 	struct ath_ahb_softc *psc = device_get_softc(dev);
246 
247 	ath_shutdown(&psc->sc_sc);
248 	return (0);
249 }
250 
251 static int
ath_ahb_suspend(device_t dev)252 ath_ahb_suspend(device_t dev)
253 {
254 	struct ath_ahb_softc *psc = device_get_softc(dev);
255 
256 	ath_suspend(&psc->sc_sc);
257 
258 	return (0);
259 }
260 
261 static int
ath_ahb_resume(device_t dev)262 ath_ahb_resume(device_t dev)
263 {
264 	struct ath_ahb_softc *psc = device_get_softc(dev);
265 
266 	ath_resume(&psc->sc_sc);
267 
268 	return (0);
269 }
270 
271 static device_method_t ath_ahb_methods[] = {
272 	/* Device interface */
273 	DEVMETHOD(device_probe,		ath_ahb_probe),
274 	DEVMETHOD(device_attach,	ath_ahb_attach),
275 	DEVMETHOD(device_detach,	ath_ahb_detach),
276 	DEVMETHOD(device_shutdown,	ath_ahb_shutdown),
277 	DEVMETHOD(device_suspend,	ath_ahb_suspend),
278 	DEVMETHOD(device_resume,	ath_ahb_resume),
279 
280 	{ 0,0 }
281 };
282 static driver_t ath_ahb_driver = {
283 	"ath",
284 	ath_ahb_methods,
285 	sizeof (struct ath_ahb_softc)
286 };
287 static	devclass_t ath_devclass;
288 DRIVER_MODULE(ath, nexus, ath_ahb_driver, ath_devclass, 0, 0);
289 MODULE_VERSION(ath, 1);
290 MODULE_DEPEND(ath, wlan, 1, 1, 1);		/* 802.11 media layer */
291 MODULE_DEPEND(ath, if_ath, 1, 1, 1);		/* if_ath driver */
292