xref: /openbsd-src/sys/dev/pci/if_ath_pci.c (revision 898184e3e61f9129feb5978fad5a8c6865f00b92)
1 /*      $OpenBSD: if_ath_pci.c,v 1.22 2010/08/27 19:44:44 deraadt 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 };
87 
88 /* Base Address Register */
89 #define ATH_BAR0	0x10
90 
91 int	 ath_pci_match(struct device *, void *, void *);
92 void	 ath_pci_attach(struct device *, struct device *, void *);
93 int	 ath_pci_detach(struct device *, int);
94 
95 struct cfattach ath_pci_ca = {
96 	sizeof(struct ath_pci_softc),
97 	ath_pci_match,
98 	ath_pci_attach,
99 	ath_pci_detach,
100 	ath_activate
101 };
102 
103 int
104 ath_pci_match(struct device *parent, void *match, void *aux)
105 {
106 	const char* devname;
107 	struct pci_attach_args *pa = aux;
108 	pci_vendor_id_t vendor;
109 
110 	vendor = PCI_VENDOR(pa->pa_id);
111 	if (vendor == 0x128c)
112 		vendor = PCI_VENDOR_ATHEROS;
113 	devname = ath_hal_probe(vendor, PCI_PRODUCT(pa->pa_id));
114 	if (devname)
115 		return 1;
116 
117 	return 0;
118 }
119 
120 void
121 ath_pci_attach(struct device *parent, struct device *self, void *aux)
122 {
123 	struct ath_pci_softc *psc = (struct ath_pci_softc *)self;
124 	struct ath_softc *sc = &psc->sc_sc;
125 	struct pci_attach_args *pa = aux;
126 	pci_chipset_tag_t pc = pa->pa_pc;
127 	pcitag_t pt = pa->pa_tag;
128 	pci_intr_handle_t ih;
129 	pcireg_t mem_type;
130 	const char *intrstr = NULL;
131 
132 	psc->sc_pc = pc;
133 	psc->sc_pcitag = pt;
134 
135 	/*
136 	 * Setup memory-mapping of PCI registers.
137 	 */
138 	mem_type = pci_mapreg_type(pc, pa->pa_tag, ATH_BAR0);
139 	if (mem_type != PCI_MAPREG_TYPE_MEM &&
140 	    mem_type != PCI_MAPREG_MEM_TYPE_64BIT) {
141 		printf(": bad PCI register type %d\n", (int)mem_type);
142 		goto fail;
143 	}
144 	if (pci_mapreg_map(pa, ATH_BAR0, mem_type, 0, &sc->sc_st, &sc->sc_sh,
145 	    NULL, &sc->sc_ss, 0)) {
146 		printf(": can't map register space\n");
147 		goto fail;
148 	}
149 
150 	/*
151 	 * PCI Express check.
152 	 */
153 	if (pci_get_capability(pc, pa->pa_tag, PCI_CAP_PCIEXPRESS,
154 	    NULL, NULL) != 0)
155 		sc->sc_pcie = 1;
156 
157 	sc->sc_invalid = 1;
158 
159 	/*
160 	 * Arrange interrupt line.
161 	 */
162 	if (pci_intr_map(pa, &ih)) {
163 		printf(": can't map interrupt\n");
164 		goto unmap;
165 	}
166 
167 	intrstr = pci_intr_string(pc, ih);
168 	psc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, ath_intr, sc,
169 	    sc->sc_dev.dv_xname);
170 	if (psc->sc_ih == NULL) {
171 		printf(": can't map interrupt\n");
172 		goto unmap;
173 	}
174 
175 	printf(": %s\n", intrstr);
176 
177 	sc->sc_dmat = pa->pa_dmat;
178 
179 	if (ath_attach(PCI_PRODUCT(pa->pa_id), sc) == 0)
180 		return;
181 
182 	pci_intr_disestablish(pc, psc->sc_ih);
183 unmap:
184 	bus_space_unmap(sc->sc_st, sc->sc_sh, sc->sc_ss);
185 fail:
186 	return;
187 }
188 
189 int
190 ath_pci_detach(struct device *self, int flags)
191 {
192 	struct ath_pci_softc *psc = (struct ath_pci_softc *)self;
193 	struct ath_softc *sc = &psc->sc_sc;
194 
195 	ath_detach(&psc->sc_sc, flags);
196 
197 	if (psc->sc_ih != NULL) {
198 		pci_intr_disestablish(psc->sc_pc, psc->sc_ih);
199 		psc->sc_ih = NULL;
200 	}
201 
202 	if (sc->sc_ss != 0) {
203 		bus_space_unmap(sc->sc_st, sc->sc_sh, sc->sc_ss);
204 		sc->sc_ss = 0;
205 	}
206 
207 	return (0);
208 }
209