xref: /openbsd-src/sys/dev/pci/if_ral_pci.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: if_ral_pci.c,v 1.15 2009/03/29 21:53:52 sthen Exp $  */
2 
3 /*-
4  * Copyright (c) 2005-2007
5  *	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/RT2661/RT2860 driver.
22  */
23 
24 #include "bpfilter.h"
25 
26 #include <sys/param.h>
27 #include <sys/sockio.h>
28 #include <sys/mbuf.h>
29 #include <sys/kernel.h>
30 #include <sys/socket.h>
31 #include <sys/systm.h>
32 #include <sys/malloc.h>
33 #include <sys/timeout.h>
34 #include <sys/device.h>
35 
36 #include <machine/bus.h>
37 #include <machine/intr.h>
38 
39 #include <net/if.h>
40 #include <net/if_dl.h>
41 #include <net/if_media.h>
42 
43 #include <netinet/in.h>
44 #include <netinet/if_ether.h>
45 
46 #include <net80211/ieee80211_var.h>
47 #include <net80211/ieee80211_amrr.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	0x10
96 
97 int	ral_pci_match(struct device *, void *, void *);
98 void	ral_pci_attach(struct device *, struct device *, void *);
99 int	ral_pci_detach(struct device *, int);
100 
101 struct cfattach ral_pci_ca = {
102 	sizeof (struct ral_pci_softc), ral_pci_match, ral_pci_attach,
103 	ral_pci_detach
104 };
105 
106 const struct pci_matchid ral_pci_devices[] = {
107 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2560  },
108 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2561  },
109 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2561S },
110 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2661  },
111 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2860  },
112 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2890  },
113 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2760  },
114 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2790  },
115 	{ PCI_VENDOR_AWT,    PCI_PRODUCT_AWT_RT2890     }
116 };
117 
118 int
119 ral_pci_match(struct device *parent, void *match, void *aux)
120 {
121 	return (pci_matchbyid((struct pci_attach_args *)aux, ral_pci_devices,
122 	    nitems(ral_pci_devices)));
123 }
124 
125 void
126 ral_pci_attach(struct device *parent, struct device *self, void *aux)
127 {
128 	struct ral_pci_softc *psc = (struct ral_pci_softc *)self;
129 	struct rt2560_softc *sc = &psc->sc_sc;
130 	struct pci_attach_args *pa = aux;
131 	const char *intrstr;
132 	pci_intr_handle_t ih;
133 	pcireg_t memtype;
134 	int error;
135 
136 	switch (PCI_PRODUCT(pa->pa_id)) {
137 	case PCI_PRODUCT_RALINK_RT2560:
138 		psc->sc_opns = &ral_rt2560_opns;
139 		break;
140 	case PCI_PRODUCT_RALINK_RT2561:
141 	case PCI_PRODUCT_RALINK_RT2561S:
142 	case PCI_PRODUCT_RALINK_RT2661:
143 		psc->sc_opns = &ral_rt2661_opns;
144 		break;
145 	case PCI_PRODUCT_RALINK_RT2860:
146 	case PCI_PRODUCT_RALINK_RT2890:
147 	case PCI_PRODUCT_RALINK_RT2760:
148 	case PCI_PRODUCT_RALINK_RT2790:
149 	case PCI_PRODUCT_AWT_RT2890:
150 		psc->sc_opns = &ral_rt2860_opns;
151 		break;
152 	}
153 
154 	sc->sc_dmat = pa->pa_dmat;
155 	psc->sc_pc = pa->pa_pc;
156 
157 	/* map control/status registers */
158 	memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, RAL_PCI_BAR0);
159 	error = pci_mapreg_map(pa, RAL_PCI_BAR0, memtype, 0, &sc->sc_st,
160 	    &sc->sc_sh, NULL, &psc->sc_mapsize, 0);
161 	if (error != 0) {
162 		printf(": can't map mem space\n");
163 		return;
164 	}
165 
166 	if (pci_intr_map(pa, &ih) != 0) {
167 		printf(": can't map interrupt\n");
168 		return;
169 	}
170 
171 	intrstr = pci_intr_string(psc->sc_pc, ih);
172 	psc->sc_ih = pci_intr_establish(psc->sc_pc, ih, IPL_NET,
173 	    psc->sc_opns->intr, sc, sc->sc_dev.dv_xname);
174 	if (psc->sc_ih == NULL) {
175 		printf(": can't establish interrupt");
176 		if (intrstr != NULL)
177 			printf(" at %s", intrstr);
178 		printf("\n");
179 		return;
180 	}
181 	printf(": %s", intrstr);
182 
183 	(*psc->sc_opns->attach)(sc, PCI_PRODUCT(pa->pa_id));
184 }
185 
186 int
187 ral_pci_detach(struct device *self, int flags)
188 {
189 	struct ral_pci_softc *psc = (struct ral_pci_softc *)self;
190 	struct rt2560_softc *sc = &psc->sc_sc;
191 	int error;
192 
193 	if (psc->sc_ih != NULL) {
194 		error = (*psc->sc_opns->detach)(sc);
195 		if (error != 0)
196 			return error;
197 
198 		pci_intr_disestablish(psc->sc_pc, psc->sc_ih);
199 		psc->sc_ih = NULL;
200 	}
201 
202 	if (psc->sc_mapsize > 0)
203 		bus_space_unmap(sc->sc_st, sc->sc_sh, psc->sc_mapsize);
204 
205 	return 0;
206 }
207