1 /* $NetBSD: if_ral_pci.c,v 1.26 2022/09/25 17:52:25 thorpej Exp $ */
2 /* $OpenBSD: if_ral_pci.c,v 1.24 2015/11/24 17:11:39 mpi Exp $ */
3
4 /*-
5 * Copyright (c) 2005-2010 Damien Bergamini <damien.bergamini@free.fr>
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20 /*
21 * PCI front-end for the Ralink RT2560/RT2561/RT2860/RT3090 driver.
22 */
23 #include <sys/cdefs.h>
24 __KERNEL_RCSID(0, "$NetBSD: if_ral_pci.c,v 1.26 2022/09/25 17:52:25 thorpej Exp $");
25
26
27 #include <sys/param.h>
28 #include <sys/sockio.h>
29 #include <sys/mbuf.h>
30 #include <sys/kernel.h>
31 #include <sys/socket.h>
32 #include <sys/systm.h>
33 #include <sys/device.h>
34
35 #include <sys/bus.h>
36 #include <sys/intr.h>
37
38 #include <net/if.h>
39 #include <net/if_dl.h>
40 #include <net/if_media.h>
41 #include <net/if_ether.h>
42
43 #include <netinet/in.h>
44
45 #include <net80211/ieee80211_var.h>
46 #include <net80211/ieee80211_amrr.h>
47 #include <net80211/ieee80211_rssadapt.h>
48 #include <net80211/ieee80211_radiotap.h>
49
50 #include <dev/ic/rt2560var.h>
51 #include <dev/ic/rt2661var.h>
52 #include <dev/ic/rt2860var.h>
53
54 #include <dev/pci/pcireg.h>
55 #include <dev/pci/pcivar.h>
56 #include <dev/pci/pcidevs.h>
57
58 static struct ral_opns {
59 int (*attach)(void *, int);
60 int (*detach)(void *);
61 int (*intr)(void *);
62
63 } ral_rt2560_opns = {
64 rt2560_attach,
65 rt2560_detach,
66 rt2560_intr
67
68 }, ral_rt2661_opns = {
69 rt2661_attach,
70 rt2661_detach,
71 rt2661_intr
72
73 }, ral_rt2860_opns = {
74 rt2860_attach,
75 rt2860_detach,
76 rt2860_intr
77 };
78
79 struct ral_pci_softc {
80 union {
81 struct rt2560_softc sc_rt2560;
82 struct rt2661_softc sc_rt2661;
83 struct rt2860_softc sc_rt2860;
84 } u;
85 #define sc_sc u.sc_rt2560
86
87 /* PCI specific goo */
88 struct ral_opns *sc_opns;
89 pci_chipset_tag_t sc_pc;
90 void *sc_ih;
91 bus_size_t sc_mapsize;
92 };
93
94 /* Base Address Register */
95 #define RAL_PCI_BAR0 PCI_BAR(0)
96
97 int ral_pci_match(device_t, cfdata_t, void *);
98 void ral_pci_attach(device_t, device_t, void *);
99 int ral_pci_detach(device_t, int);
100
101 CFATTACH_DECL_NEW(ral_pci, sizeof (struct ral_pci_softc),
102 ral_pci_match, ral_pci_attach, ral_pci_detach, NULL);
103
104 static const struct ral_pci_matchid {
105 pci_vendor_id_t ral_vendor;
106 pci_product_id_t ral_product;
107 } ral_pci_devices[] = {
108 { PCI_VENDOR_AWT, PCI_PRODUCT_AWT_RT2890 },
109 { PCI_VENDOR_EDIMAX, PCI_PRODUCT_EDIMAX_RT2860_1 },
110 { PCI_VENDOR_EDIMAX, PCI_PRODUCT_EDIMAX_RT2860_2 },
111 { PCI_VENDOR_EDIMAX, PCI_PRODUCT_EDIMAX_RT2860_3 },
112 { PCI_VENDOR_EDIMAX, PCI_PRODUCT_EDIMAX_RT2860_4 },
113 { PCI_VENDOR_EDIMAX, PCI_PRODUCT_EDIMAX_RT2860_5 },
114 { PCI_VENDOR_EDIMAX, PCI_PRODUCT_EDIMAX_RT2860_6 },
115 { PCI_VENDOR_EDIMAX, PCI_PRODUCT_EDIMAX_RT2860_7 },
116 { PCI_VENDOR_EDIMAX, PCI_PRODUCT_EDIMAX_RT3591_1 },
117 { PCI_VENDOR_EDIMAX, PCI_PRODUCT_EDIMAX_RT3591_2 },
118 { PCI_VENDOR_MSI, PCI_PRODUCT_AWT_RT2890 },
119 { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2560 },
120 { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2561 },
121 { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2561S },
122 { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2661 },
123 { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2760 },
124 { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2790 },
125 { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2860 },
126 { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2890 },
127 { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT3060 },
128 { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT3062 },
129 { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT3090 },
130 { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT3091 },
131 { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT3092 },
132 { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT3562 },
133 { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT3592 },
134 { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT3593 },
135 { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT5360 },
136 { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT5362 },
137 { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT5390_1 },
138 { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT5390_2 },
139 { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT5390_3 },
140 { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT5390_4 },
141 { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT5390_5 },
142 };
143
144 int
ral_pci_match(device_t parent,cfdata_t cfdata,void * aux)145 ral_pci_match(device_t parent, cfdata_t cfdata,
146 void *aux)
147 {
148 struct pci_attach_args *pa = aux;
149
150 for (size_t i = 0; i < __arraycount(ral_pci_devices); i++) {
151 const struct ral_pci_matchid *ral = &ral_pci_devices[i];
152 if (PCI_VENDOR(pa->pa_id) == ral->ral_vendor &&
153 PCI_PRODUCT(pa->pa_id) == ral->ral_product)
154 return 1;
155 }
156
157 return 0;
158 }
159
160 void
ral_pci_attach(device_t parent,device_t self,void * aux)161 ral_pci_attach(device_t parent, device_t self, void *aux)
162 {
163 struct ral_pci_softc *psc = device_private(self);
164 struct rt2560_softc *sc = &psc->sc_sc;
165 const struct pci_attach_args *pa = aux;
166 const char *intrstr;
167 bus_addr_t base;
168 pci_intr_handle_t ih;
169 pcireg_t memtype, reg;
170 int error;
171 char intrbuf[PCI_INTRSTR_LEN];
172
173 pci_aprint_devinfo(pa, NULL);
174
175 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_RALINK) {
176 switch (PCI_PRODUCT(pa->pa_id)) {
177 case PCI_PRODUCT_RALINK_RT2560:
178 psc->sc_opns = &ral_rt2560_opns;
179 break;
180 case PCI_PRODUCT_RALINK_RT2561:
181 case PCI_PRODUCT_RALINK_RT2561S:
182 case PCI_PRODUCT_RALINK_RT2661:
183 psc->sc_opns = &ral_rt2661_opns;
184 break;
185 default:
186 psc->sc_opns = &ral_rt2860_opns;
187 break;
188 }
189 } else {
190 /* all other vendors are RT2860 only */
191 psc->sc_opns = &ral_rt2860_opns;
192 }
193
194 sc->sc_dev = self;
195 sc->sc_dmat = pa->pa_dmat;
196 psc->sc_pc = pa->pa_pc;
197
198 /* enable the appropriate bits in the PCI CSR */
199 reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
200 reg |= PCI_COMMAND_MASTER_ENABLE | PCI_COMMAND_MEM_ENABLE;
201 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, reg);
202
203 /* map control/status registers */
204 memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, RAL_PCI_BAR0);
205 error = pci_mapreg_map(pa, RAL_PCI_BAR0, memtype, 0, &sc->sc_st,
206 &sc->sc_sh, &base, &psc->sc_mapsize);
207
208 if (error != 0) {
209 aprint_error(": could not map memory space\n");
210 return;
211 }
212
213 if (pci_intr_map(pa, &ih) != 0) {
214 aprint_error(": could not map interrupt\n");
215 return;
216 }
217
218 intrstr = pci_intr_string(psc->sc_pc, ih, intrbuf, sizeof(intrbuf));
219 psc->sc_ih = pci_intr_establish_xname(psc->sc_pc, ih, IPL_NET,
220 psc->sc_opns->intr, sc, device_xname(self));
221
222 if (psc->sc_ih == NULL) {
223 aprint_error(": could not establish interrupt");
224 if (intrstr != NULL)
225 aprint_error(" at %s", intrstr);
226 aprint_error("\n");
227 return;
228 }
229 aprint_normal_dev(sc->sc_dev, "interrupting at %s\n", intrstr);
230
231 (*psc->sc_opns->attach)(sc, PCI_PRODUCT(pa->pa_id));
232 }
233
234 int
ral_pci_detach(device_t self,int flags)235 ral_pci_detach(device_t self, int flags)
236 {
237 struct ral_pci_softc *psc = device_private(self);
238 struct rt2560_softc *sc = &psc->sc_sc;
239 int error;
240
241 if (psc->sc_ih != NULL) {
242 pci_intr_disestablish(psc->sc_pc, psc->sc_ih);
243
244 error = (*psc->sc_opns->detach)(sc);
245 if (error != 0)
246 return error;
247 }
248
249 if (psc->sc_mapsize > 0)
250 bus_space_unmap(sc->sc_st, sc->sc_sh, psc->sc_mapsize);
251
252 return 0;
253 }
254