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