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