xref: /openbsd/sys/dev/pci/if_ath_pci.c (revision 73471bf0)
1 /*      $OpenBSD: if_ath_pci.c,v 1.26 2015/11/24 17:11:39 mpi 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_media.h>
56 #include <netinet/in.h>
57 #include <netinet/if_ether.h>
58 
59 #include <net80211/ieee80211_var.h>
60 #include <net80211/ieee80211_rssadapt.h>
61 
62 #include <dev/gpio/gpiovar.h>
63 
64 #include <dev/pci/pcivar.h>
65 #include <dev/pci/pcireg.h>
66 #include <dev/pci/pcidevs.h>
67 
68 #include <dev/ic/athvar.h>
69 
70 /*
71  * PCI glue.
72  */
73 
74 struct ath_pci_softc {
75 	struct ath_softc	sc_sc;
76 
77 	pci_chipset_tag_t	sc_pc;
78 	pcitag_t		sc_pcitag;
79 
80 	void			*sc_ih;		/* Interrupt handler. */
81 };
82 
83 /* Base Address Register */
84 #define ATH_BAR0	0x10
85 
86 int	 ath_pci_match(struct device *, void *, void *);
87 void	 ath_pci_attach(struct device *, struct device *, void *);
88 int	 ath_pci_detach(struct device *, int);
89 
90 struct cfattach ath_pci_ca = {
91 	sizeof(struct ath_pci_softc),
92 	ath_pci_match,
93 	ath_pci_attach,
94 	ath_pci_detach,
95 	ath_activate
96 };
97 
98 int
99 ath_pci_match(struct device *parent, void *match, void *aux)
100 {
101 	const char* devname;
102 	struct pci_attach_args *pa = aux;
103 	pci_vendor_id_t vendor;
104 
105 	vendor = PCI_VENDOR(pa->pa_id);
106 	if (vendor == 0x128c)
107 		vendor = PCI_VENDOR_ATHEROS;
108 	devname = ath_hal_probe(vendor, PCI_PRODUCT(pa->pa_id));
109 	if (devname)
110 		return 1;
111 
112 	return 0;
113 }
114 
115 void
116 ath_pci_attach(struct device *parent, struct device *self, void *aux)
117 {
118 	struct ath_pci_softc *psc = (struct ath_pci_softc *)self;
119 	struct ath_softc *sc = &psc->sc_sc;
120 	struct pci_attach_args *pa = aux;
121 	pci_chipset_tag_t pc = pa->pa_pc;
122 	pcitag_t pt = pa->pa_tag;
123 	pci_intr_handle_t ih;
124 	pcireg_t mem_type;
125 	const char *intrstr = NULL;
126 
127 	psc->sc_pc = pc;
128 	psc->sc_pcitag = pt;
129 
130 	/*
131 	 * Setup memory-mapping of PCI registers.
132 	 */
133 	mem_type = pci_mapreg_type(pc, pa->pa_tag, ATH_BAR0);
134 	if (mem_type != PCI_MAPREG_TYPE_MEM &&
135 	    mem_type != PCI_MAPREG_MEM_TYPE_64BIT) {
136 		printf(": bad PCI register type %d\n", (int)mem_type);
137 		goto fail;
138 	}
139 	if (pci_mapreg_map(pa, ATH_BAR0, mem_type, 0, &sc->sc_st, &sc->sc_sh,
140 	    NULL, &sc->sc_ss, 0)) {
141 		printf(": can't map register space\n");
142 		goto fail;
143 	}
144 
145 	/*
146 	 * PCI Express check.
147 	 */
148 	if (pci_get_capability(pc, pa->pa_tag, PCI_CAP_PCIEXPRESS,
149 	    NULL, NULL) != 0)
150 		sc->sc_pcie = 1;
151 
152 	sc->sc_invalid = 1;
153 
154 	/*
155 	 * Arrange interrupt line.
156 	 */
157 	if (pci_intr_map(pa, &ih)) {
158 		printf(": can't map interrupt\n");
159 		goto unmap;
160 	}
161 
162 	intrstr = pci_intr_string(pc, ih);
163 	psc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, ath_intr, sc,
164 	    sc->sc_dev.dv_xname);
165 	if (psc->sc_ih == NULL) {
166 		printf(": can't map interrupt\n");
167 		goto unmap;
168 	}
169 
170 	printf(": %s\n", intrstr);
171 
172 	sc->sc_dmat = pa->pa_dmat;
173 
174 	if (ath_attach(PCI_PRODUCT(pa->pa_id), sc) == 0)
175 		return;
176 
177 	pci_intr_disestablish(pc, psc->sc_ih);
178 unmap:
179 	bus_space_unmap(sc->sc_st, sc->sc_sh, sc->sc_ss);
180 fail:
181 	return;
182 }
183 
184 int
185 ath_pci_detach(struct device *self, int flags)
186 {
187 	struct ath_pci_softc *psc = (struct ath_pci_softc *)self;
188 	struct ath_softc *sc = &psc->sc_sc;
189 
190 	ath_detach(&psc->sc_sc, flags);
191 
192 	if (psc->sc_ih != NULL) {
193 		pci_intr_disestablish(psc->sc_pc, psc->sc_ih);
194 		psc->sc_ih = NULL;
195 	}
196 
197 	if (sc->sc_ss != 0) {
198 		bus_space_unmap(sc->sc_st, sc->sc_sh, sc->sc_ss);
199 		sc->sc_ss = 0;
200 	}
201 
202 	return (0);
203 }
204