xref: /openbsd-src/sys/dev/pci/if_ath_pci.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*      $OpenBSD: if_ath_pci.c,v 1.19 2009/03/29 21:53:52 sthen Exp $   */
2 /*	$NetBSD: if_ath_pci.c,v 1.7 2004/06/30 05:58:17 mycroft Exp $	*/
3 
4 /*-
5  * Copyright (c) 2002-2004 Sam Leffler, Errno Consulting
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  * 3. Neither the names of the above-listed copyright holders nor the names
19  *    of any contributors may be used to endorse or promote products derived
20  *    from this software without specific prior written permission.
21  *
22  * NO WARRANTY
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
26  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
27  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
28  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
31  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
33  * THE POSSIBILITY OF SUCH DAMAGES.
34  */
35 
36 /*
37  * PCI front-end for the Atheros Wireless LAN controller driver.
38  */
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/mbuf.h>
43 #include <sys/malloc.h>
44 #include <sys/kernel.h>
45 #include <sys/lock.h>
46 #include <sys/socket.h>
47 #include <sys/sockio.h>
48 #include <sys/errno.h>
49 #include <sys/device.h>
50 #include <sys/gpio.h>
51 
52 #include <machine/bus.h>
53 
54 #include <net/if.h>
55 #include <net/if_dl.h>
56 #include <net/if_media.h>
57 #include <net/if_llc.h>
58 #include <net/if_arp.h>
59 #ifdef INET
60 #include <netinet/in.h>
61 #include <netinet/if_ether.h>
62 #endif
63 
64 #include <net80211/ieee80211_var.h>
65 #include <net80211/ieee80211_rssadapt.h>
66 
67 #include <dev/gpio/gpiovar.h>
68 
69 #include <dev/pci/pcivar.h>
70 #include <dev/pci/pcireg.h>
71 #include <dev/pci/pcidevs.h>
72 
73 #include <dev/ic/athvar.h>
74 
75 /*
76  * PCI glue.
77  */
78 
79 struct ath_pci_softc {
80 	struct ath_softc	sc_sc;
81 
82 	pci_chipset_tag_t	sc_pc;
83 	pcitag_t		sc_pcitag;
84 
85 	void			*sc_ih;		/* Interrupt handler. */
86 	void			*sc_sdhook;	/* Shutdown hook. */
87 };
88 
89 /* Base Address Register */
90 #define ATH_BAR0	0x10
91 
92 int	 ath_pci_match(struct device *, void *, void *);
93 void	 ath_pci_attach(struct device *, struct device *, void *);
94 void	 ath_pci_shutdown(void *);
95 int	 ath_pci_detach(struct device *, int);
96 
97 struct cfattach ath_pci_ca = {
98 	sizeof(struct ath_pci_softc),
99 	ath_pci_match,
100 	ath_pci_attach,
101 	ath_pci_detach
102 };
103 
104 int
105 ath_pci_match(struct device *parent, void *match, void *aux)
106 {
107 	const char* devname;
108 	struct pci_attach_args *pa = aux;
109 	pci_vendor_id_t vendor;
110 
111 	vendor = PCI_VENDOR(pa->pa_id);
112 	if (vendor == 0x128c)
113 		vendor = PCI_VENDOR_ATHEROS;
114 	devname = ath_hal_probe(vendor, PCI_PRODUCT(pa->pa_id));
115 	if (devname)
116 		return 1;
117 
118 	return 0;
119 }
120 
121 void
122 ath_pci_attach(struct device *parent, struct device *self, void *aux)
123 {
124 	struct ath_pci_softc *psc = (struct ath_pci_softc *)self;
125 	struct ath_softc *sc = &psc->sc_sc;
126 	struct pci_attach_args *pa = aux;
127 	pci_chipset_tag_t pc = pa->pa_pc;
128 	pcitag_t pt = pa->pa_tag;
129 	pci_intr_handle_t ih;
130 	pcireg_t mem_type;
131 	const char *intrstr = NULL;
132 
133 	psc->sc_pc = pc;
134 	psc->sc_pcitag = pt;
135 
136 	/*
137 	 * Setup memory-mapping of PCI registers.
138 	 */
139 	mem_type = pci_mapreg_type(pc, pa->pa_tag, ATH_BAR0);
140 	if (mem_type != PCI_MAPREG_TYPE_MEM &&
141 	    mem_type != PCI_MAPREG_MEM_TYPE_64BIT) {
142 		printf(": bad PCI register type %d\n", (int)mem_type);
143 		goto fail;
144 	}
145 	if (pci_mapreg_map(pa, ATH_BAR0, mem_type, 0, &sc->sc_st, &sc->sc_sh,
146 	    NULL, &sc->sc_ss, 0)) {
147 		printf(": can't map register space\n");
148 		goto fail;
149 	}
150 
151 	/*
152 	 * PCI Express check.
153 	 */
154 	if (pci_get_capability(pc, pa->pa_tag, PCI_CAP_PCIEXPRESS,
155 	    NULL, NULL) != 0)
156 		sc->sc_pcie = 1;
157 
158 	sc->sc_invalid = 1;
159 
160 	/*
161 	 * Arrange interrupt line.
162 	 */
163 	if (pci_intr_map(pa, &ih)) {
164 		printf(": can't map interrupt\n");
165 		goto unmap;
166 	}
167 
168 	intrstr = pci_intr_string(pc, ih);
169 	psc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, ath_intr, sc,
170 	    sc->sc_dev.dv_xname);
171 	if (psc->sc_ih == NULL) {
172 		printf(": can't map interrupt\n");
173 		goto unmap;
174 	}
175 
176 	printf(": %s\n", intrstr);
177 
178 	sc->sc_dmat = pa->pa_dmat;
179 
180 	psc->sc_sdhook = shutdownhook_establish(ath_pci_shutdown, psc);
181 	if (psc->sc_sdhook == NULL) {
182 		printf(": can't establish shutdown hook\n");
183 		goto deintr;
184 	}
185 
186 	if (ath_attach(PCI_PRODUCT(pa->pa_id), sc) == 0)
187 		return;
188 
189 	shutdownhook_disestablish(psc->sc_sdhook);
190 deintr:
191 	pci_intr_disestablish(pc, psc->sc_ih);
192 unmap:
193 	bus_space_unmap(sc->sc_st, sc->sc_sh, sc->sc_ss);
194 fail:
195 	return;
196 }
197 
198 int
199 ath_pci_detach(struct device *self, int flags)
200 {
201 	struct ath_pci_softc *psc = (struct ath_pci_softc *)self;
202 	struct ath_softc *sc = &psc->sc_sc;
203 
204 	ath_detach(&psc->sc_sc, flags);
205 
206 	if (psc->sc_ih != NULL) {
207 		pci_intr_disestablish(psc->sc_pc, psc->sc_ih);
208 		psc->sc_ih = NULL;
209 	}
210 
211 	if (psc->sc_sdhook != NULL) {
212 		shutdownhook_disestablish(psc->sc_sdhook);
213 		psc->sc_sdhook = NULL;
214 	}
215 
216 	if (sc->sc_ss != 0) {
217 		bus_space_unmap(sc->sc_st, sc->sc_sh, sc->sc_ss);
218 		sc->sc_ss = 0;
219 	}
220 
221 	return (0);
222 }
223 
224 void
225 ath_pci_shutdown(void *self)
226 {
227 	struct ath_pci_softc *psc = (struct ath_pci_softc *)self;
228 
229 	ath_shutdown(&psc->sc_sc);
230 }
231