xref: /netbsd-src/sys/dev/pci/if_ath_pci.c (revision da9817918ec7e88db2912a2882967c7570a83f47)
1 /*	$NetBSD: if_ath_pci.c,v 1.33 2009/05/06 09:25:15 cegger Exp $	*/
2 
3 /*-
4  * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer,
12  *    without modification.
13  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
14  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
15  *    redistribution must be conditioned upon including a substantially
16  *    similar Disclaimer requirement for further binary redistribution.
17  * 3. Neither the names of the above-listed copyright holders nor the names
18  *    of any contributors may be used to endorse or promote products derived
19  *    from this software without specific prior written permission.
20  *
21  * Alternatively, this software may be distributed under the terms of the
22  * GNU General Public License ("GPL") version 2 as published by the Free
23  * Software Foundation.
24  *
25  * NO WARRANTY
26  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
29  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
30  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
31  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
34  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
36  * THE POSSIBILITY OF SUCH DAMAGES.
37  */
38 
39 #include <sys/cdefs.h>
40 #ifdef __FreeBSD__
41 __FBSDID("$FreeBSD: src/sys/dev/ath/if_ath_pci.c,v 1.11 2005/01/18 18:08:16 sam Exp $");
42 #endif
43 #ifdef __NetBSD__
44 __KERNEL_RCSID(0, "$NetBSD: if_ath_pci.c,v 1.33 2009/05/06 09:25:15 cegger Exp $");
45 #endif
46 
47 /*
48  * PCI/Cardbus front-end for the Atheros Wireless LAN controller driver.
49  */
50 
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/mbuf.h>
54 #include <sys/malloc.h>
55 #include <sys/kernel.h>
56 #include <sys/socket.h>
57 #include <sys/errno.h>
58 #include <sys/device.h>
59 
60 #include <net/if.h>
61 #include <net/if_media.h>
62 #include <net/if_ether.h>
63 
64 #include <net80211/ieee80211_netbsd.h>
65 #include <net80211/ieee80211_var.h>
66 
67 #ifdef INET
68 #include <netinet/in.h>
69 #endif
70 
71 #include <external/isc/atheros_hal/dist/ah.h>
72 
73 #include <dev/ic/ath_netbsd.h>
74 #include <dev/ic/athvar.h>
75 
76 #include <dev/pci/pcivar.h>
77 #include <dev/pci/pcireg.h>
78 #include <dev/pci/pcidevs.h>
79 
80 /*
81  * PCI glue.
82  */
83 
84 struct ath_pci_softc {
85 	struct ath_softc	sc_sc;
86 	pci_chipset_tag_t	sc_pc;
87 	pcitag_t		sc_pcitag;
88 	pci_intr_handle_t	sc_pih;
89 	void			*sc_ih;		/* interrupt handler */
90 	bus_space_tag_t		sc_iot;
91 	bus_space_handle_t	sc_ioh;
92 	bus_size_t		sc_mapsz;
93 };
94 
95 #define	BS_BAR	0x10
96 
97 static void ath_pci_attach(device_t, device_t, void *);
98 static int ath_pci_detach(device_t, int);
99 static int ath_pci_match(device_t, cfdata_t, void *);
100 static int ath_pci_detach(device_t, int);
101 
102 CFATTACH_DECL_NEW(ath_pci,
103     sizeof(struct ath_pci_softc),
104     ath_pci_match,
105     ath_pci_attach,
106     ath_pci_detach,
107     NULL);
108 
109 static int
110 ath_pci_match(device_t parent, cfdata_t match, void *aux)
111 {
112 	const char* devname;
113 	struct pci_attach_args *pa = aux;
114 
115 	devname = ath_hal_probe(PCI_VENDOR(pa->pa_id), PCI_PRODUCT(pa->pa_id));
116 	if (devname != NULL)
117 		return 1;
118 	return 0;
119 }
120 
121 static bool
122 ath_pci_suspend(device_t self PMF_FN_ARGS)
123 {
124 	struct ath_pci_softc *sc = device_private(self);
125 
126 	ath_suspend(&sc->sc_sc);
127 	pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
128 	sc->sc_ih = NULL;
129 
130 	return true;
131 }
132 
133 static bool
134 ath_pci_resume(device_t self PMF_FN_ARGS)
135 {
136 	struct ath_pci_softc *sc = device_private(self);
137 
138 	sc->sc_ih = pci_intr_establish(sc->sc_pc, sc->sc_pih, IPL_NET, ath_intr,
139 	    &sc->sc_sc);
140 	if (sc->sc_ih == NULL) {
141 		aprint_error_dev(self, "couldn't map interrupt\n");
142 		return false;
143 	}
144 	ath_resume(&sc->sc_sc);
145 
146 	return true;
147 }
148 
149 static int
150 ath_pci_setup(struct ath_pci_softc *sc)
151 {
152 	pcireg_t bhlc, csr, icr, lattimer;
153 	/*
154 	 * Enable memory mapping and bus mastering.
155 	 */
156 	csr = pci_conf_read(sc->sc_pc, sc->sc_pcitag, PCI_COMMAND_STATUS_REG);
157 	csr |= PCI_COMMAND_MASTER_ENABLE | PCI_COMMAND_MEM_ENABLE;
158 	pci_conf_write(sc->sc_pc, sc->sc_pcitag, PCI_COMMAND_STATUS_REG, csr);
159 	csr = pci_conf_read(sc->sc_pc, sc->sc_pcitag, PCI_COMMAND_STATUS_REG);
160 
161 	if ((csr & PCI_COMMAND_MEM_ENABLE) == 0) {
162 		aprint_error("couldn't enable memory mapping\n");
163 		return 0;
164 	}
165 	if ((csr & PCI_COMMAND_MASTER_ENABLE) == 0) {
166 		aprint_error("couldn't enable bus mastering\n");
167 		return 0;
168 	}
169 
170 	/*
171 	 * XXX Both this comment and code are replicated in
172 	 * XXX cardbus_rescan().
173 	 *
174 	 * Make sure the latency timer is set to some reasonable
175 	 * value.
176 	 *
177 	 * I will set the initial value of the Latency Timer here.
178 	 *
179 	 * While a PCI device owns the bus, its Latency Timer counts
180 	 * down bus cycles from its initial value to 0.  Minimum
181 	 * Grant tells for how long the device wants to own the
182 	 * bus once it gets access, in units of 250ns.
183 	 *
184 	 * On a 33 MHz bus, there are 8 cycles per 250ns.  So I
185 	 * multiply the Minimum Grant by 8 to find out the initial
186 	 * value of the Latency Timer.
187 	 *
188 	 * I never set a Latency Timer less than 0x10, since that
189 	 * is what the old code did.
190 	 */
191 	bhlc = pci_conf_read(sc->sc_pc, sc->sc_pcitag, PCI_BHLC_REG);
192 	icr = pci_conf_read(sc->sc_pc, sc->sc_pcitag, PCI_INTERRUPT_REG);
193 	lattimer = MAX(0x10, MIN(0xf8, 8 * PCI_MIN_GNT(icr)));
194 	if (PCI_LATTIMER(bhlc) < lattimer) {
195 		bhlc &= ~(PCI_LATTIMER_MASK << PCI_LATTIMER_SHIFT);
196 		bhlc |= (lattimer << PCI_LATTIMER_SHIFT);
197 		pci_conf_write(sc->sc_pc, sc->sc_pcitag, PCI_BHLC_REG, bhlc);
198 	}
199 	return 1;
200 }
201 
202 static void
203 ath_pci_attach(device_t parent, device_t self, void *aux)
204 {
205 	struct ath_pci_softc *psc = device_private(self);
206 	struct ath_softc *sc = &psc->sc_sc;
207 	struct pci_attach_args *pa = aux;
208 	pci_chipset_tag_t pc = pa->pa_pc;
209 	pcireg_t mem_type;
210 	const char *intrstr = NULL;
211 
212 	sc->sc_dev = self;
213 	psc->sc_pc = pc;
214 
215 	psc->sc_pcitag = pa->pa_tag;
216 
217 	if (!ath_pci_setup(psc))
218 		goto bad;
219 
220 	/*
221 	 * Setup memory-mapping of PCI registers.
222 	 */
223 	mem_type = pci_mapreg_type(pc, pa->pa_tag, BS_BAR);
224 	if (mem_type != PCI_MAPREG_TYPE_MEM &&
225 	    mem_type != PCI_MAPREG_MEM_TYPE_64BIT) {
226 		aprint_error("bad pci register type %d\n", (int)mem_type);
227 		goto bad;
228 	}
229 	if (pci_mapreg_map(pa, BS_BAR, mem_type, 0, &psc->sc_iot,
230 		&psc->sc_ioh, NULL, &psc->sc_mapsz)) {
231 		aprint_error("cannot map register space\n");
232 		goto bad;
233 	}
234 
235 	sc->sc_st = HALTAG(psc->sc_iot);
236 	sc->sc_sh = HALHANDLE(psc->sc_ioh);
237 
238 	/*
239 	 * Arrange interrupt line.
240 	 */
241 	if (pci_intr_map(pa, &psc->sc_pih)) {
242 		aprint_error("couldn't map interrupt\n");
243 		goto bad1;
244 	}
245 
246 	intrstr = pci_intr_string(pc, psc->sc_pih);
247 	psc->sc_ih = pci_intr_establish(pc, psc->sc_pih, IPL_NET, ath_intr, sc);
248 	if (psc->sc_ih == NULL) {
249 		aprint_error("couldn't map interrupt\n");
250 		goto bad2;
251 	}
252 
253 	aprint_normal("\n");
254 	aprint_verbose_dev(self, "interrupting at %s\n", intrstr);
255 
256 	sc->sc_dmat = pa->pa_dmat;
257 
258 	ATH_LOCK_INIT(sc);
259 
260 	if (ath_attach(PCI_PRODUCT(pa->pa_id), sc) != 0)
261 		goto bad3;
262 
263 	if (!pmf_device_register(self, ath_pci_suspend, ath_pci_resume))
264 		aprint_error_dev(self, "couldn't establish power handler\n");
265 	else {
266 		pmf_class_network_register(self, &sc->sc_if);
267 		pmf_device_suspend_self(self);
268 	}
269 	return;
270 bad3:
271 	ATH_LOCK_DESTROY(sc);
272 
273 	pci_intr_disestablish(pc, psc->sc_ih);
274 bad2:	/* XXX */
275 bad1:
276 	bus_space_unmap(psc->sc_iot, psc->sc_ioh, psc->sc_mapsz);
277 bad:	/* XXX */
278 	return;
279 }
280 
281 static int
282 ath_pci_detach(device_t self, int flags)
283 {
284 	struct ath_pci_softc *psc = device_private(self);
285 
286 	ath_detach(&psc->sc_sc);
287 	pmf_device_deregister(self);
288 	if (psc->sc_ih != NULL)
289 		pci_intr_disestablish(psc->sc_pc, psc->sc_ih);
290 	bus_space_unmap(psc->sc_iot, psc->sc_ioh, psc->sc_mapsz);
291 
292 	ATH_LOCK_DESTROY(&psc->sc_sc);
293 
294 	return (0);
295 }
296