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