xref: /openbsd/sys/dev/pci/if_ral_pci.c (revision 17df1aa7)
1 /*	$OpenBSD: if_ral_pci.c,v 1.19 2010/04/05 14:14:02 damien 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/sockio.h>
27 #include <sys/mbuf.h>
28 #include <sys/kernel.h>
29 #include <sys/socket.h>
30 #include <sys/systm.h>
31 #include <sys/malloc.h>
32 #include <sys/timeout.h>
33 #include <sys/device.h>
34 
35 #include <machine/bus.h>
36 #include <machine/intr.h>
37 
38 #include <net/if.h>
39 #include <net/if_dl.h>
40 #include <net/if_media.h>
41 
42 #include <netinet/in.h>
43 #include <netinet/if_ether.h>
44 
45 #include <net80211/ieee80211_var.h>
46 #include <net80211/ieee80211_amrr.h>
47 #include <net80211/ieee80211_radiotap.h>
48 
49 #include <dev/ic/rt2560var.h>
50 #include <dev/ic/rt2661var.h>
51 #include <dev/ic/rt2860var.h>
52 
53 #include <dev/pci/pcireg.h>
54 #include <dev/pci/pcivar.h>
55 #include <dev/pci/pcidevs.h>
56 
57 static struct ral_opns {
58 	int	(*attach)(void *, int);
59 	int	(*detach)(void *);
60 	int	(*intr)(void *);
61 
62 }  ral_rt2560_opns = {
63 	rt2560_attach,
64 	rt2560_detach,
65 	rt2560_intr
66 
67 }, ral_rt2661_opns = {
68 	rt2661_attach,
69 	rt2661_detach,
70 	rt2661_intr
71 
72 }, ral_rt2860_opns = {
73 	rt2860_attach,
74 	rt2860_detach,
75 	rt2860_intr
76 };
77 
78 struct ral_pci_softc {
79 	union {
80 		struct rt2560_softc	sc_rt2560;
81 		struct rt2661_softc	sc_rt2661;
82 		struct rt2860_softc	sc_rt2860;
83 	} u;
84 #define sc_sc	u.sc_rt2560
85 
86 	/* PCI specific goo */
87 	struct ral_opns		*sc_opns;
88 	pci_chipset_tag_t	sc_pc;
89 	void			*sc_ih;
90 	bus_size_t		sc_mapsize;
91 };
92 
93 /* Base Address Register */
94 #define RAL_PCI_BAR0	0x10
95 
96 int	ral_pci_match(struct device *, void *, void *);
97 void	ral_pci_attach(struct device *, struct device *, void *);
98 int	ral_pci_detach(struct device *, int);
99 
100 struct cfattach ral_pci_ca = {
101 	sizeof (struct ral_pci_softc), ral_pci_match, ral_pci_attach,
102 	ral_pci_detach
103 };
104 
105 const struct pci_matchid ral_pci_devices[] = {
106 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2560 },
107 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2561 },
108 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2561S },
109 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2661 },
110 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2860 },
111 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2890 },
112 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2760 },
113 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2790 },
114 	{ PCI_VENDOR_AWT,    PCI_PRODUCT_AWT_RT2890 },
115 	{ PCI_VENDOR_EDIMAX, PCI_PRODUCT_EDIMAX_RT2860_1 },
116 	{ PCI_VENDOR_EDIMAX, PCI_PRODUCT_EDIMAX_RT2860_2 },
117 	{ PCI_VENDOR_EDIMAX, PCI_PRODUCT_EDIMAX_RT2860_3 },
118 	{ PCI_VENDOR_EDIMAX, PCI_PRODUCT_EDIMAX_RT2860_4 },
119 	{ PCI_VENDOR_EDIMAX, PCI_PRODUCT_EDIMAX_RT2860_5 },
120 	{ PCI_VENDOR_EDIMAX, PCI_PRODUCT_EDIMAX_RT2860_6 },
121 	{ PCI_VENDOR_EDIMAX, PCI_PRODUCT_EDIMAX_RT2860_7 },
122 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT3062 },
123 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT3090 },
124 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT3091 },
125 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT3092 },
126 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT3562 },
127 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT3592 },
128 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT3593 }
129 };
130 
131 int
132 ral_pci_match(struct device *parent, void *match, void *aux)
133 {
134 	return (pci_matchbyid((struct pci_attach_args *)aux, ral_pci_devices,
135 	    nitems(ral_pci_devices)));
136 }
137 
138 void
139 ral_pci_attach(struct device *parent, struct device *self, void *aux)
140 {
141 	struct ral_pci_softc *psc = (struct ral_pci_softc *)self;
142 	struct rt2560_softc *sc = &psc->sc_sc;
143 	struct pci_attach_args *pa = aux;
144 	const char *intrstr;
145 	pci_intr_handle_t ih;
146 	pcireg_t memtype;
147 	int error;
148 
149 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_RALINK) {
150 		switch (PCI_PRODUCT(pa->pa_id)) {
151 		case PCI_PRODUCT_RALINK_RT2560:
152 			psc->sc_opns = &ral_rt2560_opns;
153 			break;
154 		case PCI_PRODUCT_RALINK_RT2561:
155 		case PCI_PRODUCT_RALINK_RT2561S:
156 		case PCI_PRODUCT_RALINK_RT2661:
157 			psc->sc_opns = &ral_rt2661_opns;
158 			break;
159 		default:
160 			psc->sc_opns = &ral_rt2860_opns;
161 			break;
162 		}
163 	} else {
164 		/* all other vendors are RT2860 only */
165 		psc->sc_opns = &ral_rt2860_opns;
166 	}
167 	sc->sc_dmat = pa->pa_dmat;
168 	psc->sc_pc = pa->pa_pc;
169 
170 	/* map control/status registers */
171 	memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, RAL_PCI_BAR0);
172 	error = pci_mapreg_map(pa, RAL_PCI_BAR0, memtype, 0, &sc->sc_st,
173 	    &sc->sc_sh, NULL, &psc->sc_mapsize, 0);
174 	if (error != 0) {
175 		printf(": can't map mem space\n");
176 		return;
177 	}
178 
179 	if (pci_intr_map(pa, &ih) != 0) {
180 		printf(": can't map interrupt\n");
181 		return;
182 	}
183 
184 	intrstr = pci_intr_string(psc->sc_pc, ih);
185 	psc->sc_ih = pci_intr_establish(psc->sc_pc, ih, IPL_NET,
186 	    psc->sc_opns->intr, sc, sc->sc_dev.dv_xname);
187 	if (psc->sc_ih == NULL) {
188 		printf(": can't establish interrupt");
189 		if (intrstr != NULL)
190 			printf(" at %s", intrstr);
191 		printf("\n");
192 		return;
193 	}
194 	printf(": %s", intrstr);
195 
196 	(*psc->sc_opns->attach)(sc, PCI_PRODUCT(pa->pa_id));
197 }
198 
199 int
200 ral_pci_detach(struct device *self, int flags)
201 {
202 	struct ral_pci_softc *psc = (struct ral_pci_softc *)self;
203 	struct rt2560_softc *sc = &psc->sc_sc;
204 	int error;
205 
206 	if (psc->sc_ih != NULL) {
207 		pci_intr_disestablish(psc->sc_pc, psc->sc_ih);
208 
209 		error = (*psc->sc_opns->detach)(sc);
210 		if (error != 0)
211 			return error;
212 	}
213 
214 	if (psc->sc_mapsize > 0)
215 		bus_space_unmap(sc->sc_st, sc->sc_sh, psc->sc_mapsize);
216 
217 	return 0;
218 }
219