xref: /dragonfly/sys/dev/netif/wi/if_wi_pci.c (revision 25a2db75)
1 /*-
2  * Copyright (c) 1997, 1998, 1999
3  *	Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by Bill Paul.
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30  * THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * $FreeBSD: head/sys/dev/wi/if_wi_pci.c 181209 2008-08-02 20:45:28Z imp $
33  */
34 
35 /*
36  * Lucent WaveLAN/IEEE 802.11 PCMCIA driver for FreeBSD.
37  *
38  * Written by Bill Paul <wpaul@ctr.columbia.edu>
39  * Electrical Engineering Department
40  * Columbia University, New York City
41  */
42 
43 #include <sys/param.h>
44 #include <sys/kernel.h>
45 #include <sys/socket.h>
46 #include <sys/systm.h>
47 #include <sys/module.h>
48 #include <sys/bus.h>
49 #include <sys/rman.h>
50 
51 #include <bus/pci/pcireg.h>
52 #include <bus/pci/pcivar.h>
53 
54 #include <net/if.h>
55 #include <net/if_arp.h>
56 #include <net/ethernet.h>
57 #include <net/if_media.h>
58 #include <net/if_types.h>
59 
60 #include <netproto/802_11/ieee80211_var.h>
61 #include <netproto/802_11/ieee80211_radiotap.h>
62 
63 #include <dev/netif/wi/if_wavelan_ieee.h>
64 #include <dev/netif/wi/if_wireg.h>
65 #include <dev/netif/wi/if_wivar.h>
66 
67 static int wi_pci_probe(device_t);
68 static int wi_pci_attach(device_t);
69 static int wi_pci_suspend(device_t);
70 static int wi_pci_resume(device_t);
71 
72 static device_method_t wi_pci_methods[] = {
73 	/* Device interface */
74 	DEVMETHOD(device_probe,		wi_pci_probe),
75 	DEVMETHOD(device_attach,	wi_pci_attach),
76 	DEVMETHOD(device_detach,	wi_detach),
77 	DEVMETHOD(device_shutdown,	wi_shutdown),
78 	DEVMETHOD(device_suspend,	wi_pci_suspend),
79 	DEVMETHOD(device_resume,	wi_pci_resume),
80 
81 	DEVMETHOD_END
82 };
83 
84 static driver_t wi_pci_driver = {
85 	"wi",
86 	wi_pci_methods,
87 	sizeof(struct wi_softc)
88 };
89 
90 static struct {
91 	unsigned int vendor,device;
92 	int bus_type;
93 	char *desc;
94 } pci_ids[] = {
95 	/* Sorted by description */
96 	{0x10b7, 0x7770, WI_BUS_PCI_PLX, "3Com Airconnect"},
97 	{0x16ab, 0x1101, WI_BUS_PCI_PLX, "GLPRISM2 WaveLAN"},
98 	{0x1260, 0x3872, WI_BUS_PCI_NATIVE, "Intersil Prism3"},
99 	{0x1260, 0x3873, WI_BUS_PCI_NATIVE, "Intersil Prism2.5"},
100 	{0x16ab, 0x1102, WI_BUS_PCI_PLX, "Linksys WDT11"},
101 	{0x1385, 0x4100, WI_BUS_PCI_PLX, "Netgear MA301"},
102 	{0x1638, 0x1100, WI_BUS_PCI_PLX, "PRISM2STA WaveLAN"},
103 	{0x111a, 0x1023, WI_BUS_PCI_PLX, "Siemens SpeedStream"},
104 	{0x10b5, 0x9050, WI_BUS_PCI_PLX, "SMC 2602W"},
105 	{0x16ec, 0x3685, WI_BUS_PCI_PLX, "US Robotics 2415"},
106 	{0x4033, 0x7001, WI_BUS_PCI_PLX, "Addtron AWA-100 PCI"},
107 	{0, 0, 0, NULL}
108 };
109 
110 DRIVER_MODULE(wi, pci, wi_pci_driver, wi_devclass, NULL, NULL);
111 MODULE_DEPEND(wi, pci, 1, 1, 1);
112 MODULE_DEPEND(wi, wlan, 1, 1, 1);
113 
114 static int
115 wi_pci_probe(device_t dev)
116 {
117 	struct wi_softc		*sc;
118 	int i;
119 
120 	wlan_serialize_enter();
121 	sc = device_get_softc(dev);
122 	for(i=0; pci_ids[i].vendor != 0; i++) {
123 		if ((pci_get_vendor(dev) == pci_ids[i].vendor) &&
124 			(pci_get_device(dev) == pci_ids[i].device)) {
125 			sc->wi_bus_type = pci_ids[i].bus_type;
126 			device_set_desc(dev, pci_ids[i].desc);
127 			wlan_serialize_exit();
128 			return (BUS_PROBE_DEFAULT);
129 		}
130 	}
131 	wlan_serialize_exit();
132 	return(ENXIO);
133 }
134 
135 static int
136 wi_pci_attach(device_t dev)
137 {
138 	struct wi_softc		*sc;
139 	u_int32_t		command, wanted;
140 	u_int16_t		reg;
141 	int			error;
142 	int			timeout;
143 
144 	wlan_serialize_enter();
145 	sc = device_get_softc(dev);
146 
147 	command = pci_read_config(dev, PCIR_COMMAND, 4);
148 	wanted = PCIM_CMD_PORTEN|PCIM_CMD_MEMEN;
149 	command |= wanted;
150 	pci_write_config(dev, PCIR_COMMAND, command, 4);
151 	command = pci_read_config(dev, PCIR_COMMAND, 4);
152 	if ((command & wanted) != wanted) {
153 		device_printf(dev, "wi_pci_attach() failed to enable pci!\n");
154 		wlan_serialize_exit();
155 		return (ENXIO);
156 	}
157 
158 	if (sc->wi_bus_type != WI_BUS_PCI_NATIVE) {
159 		error = wi_alloc(dev, WI_PCI_IORES);
160 		if (error) {
161 			wlan_serialize_exit();
162 			return (error);
163 		}
164 
165 		/* Make sure interrupts are disabled. */
166 		CSR_WRITE_2(sc, WI_INT_EN, 0);
167 		CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
168 
169 		/* We have to do a magic PLX poke to enable interrupts */
170 		sc->local_rid = WI_PCI_LOCALRES;
171 		sc->local = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
172 		    &sc->local_rid, RF_ACTIVE);
173 		sc->wi_localtag = rman_get_bustag(sc->local);
174 		sc->wi_localhandle = rman_get_bushandle(sc->local);
175 		command = bus_space_read_4(sc->wi_localtag, sc->wi_localhandle,
176 		    WI_LOCAL_INTCSR);
177 		command |= WI_LOCAL_INTEN;
178 		bus_space_write_4(sc->wi_localtag, sc->wi_localhandle,
179 		    WI_LOCAL_INTCSR, command);
180 		bus_release_resource(dev, SYS_RES_IOPORT, sc->local_rid,
181 		    sc->local);
182 		sc->local = NULL;
183 
184 		sc->mem_rid = WI_PCI_MEMRES;
185 		sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
186 					&sc->mem_rid, RF_ACTIVE);
187 		if (sc->mem == NULL) {
188 			device_printf(dev, "couldn't allocate memory\n");
189 			wi_free(dev);
190 			wlan_serialize_exit();
191 			return (ENXIO);
192 		}
193 		sc->wi_bmemtag = rman_get_bustag(sc->mem);
194 		sc->wi_bmemhandle = rman_get_bushandle(sc->mem);
195 
196 		/*
197 		 * Write COR to enable PC card
198 		 * This is a subset of the protocol that the pccard bus code
199 		 * would do.  In theory, we should parse the CIS to find the
200 		 * COR offset.  In practice, the COR_OFFSET is always 0x3e0.
201 		 */
202 		CSM_WRITE_1(sc, WI_COR_OFFSET, WI_COR_VALUE);
203 		reg = CSM_READ_1(sc, WI_COR_OFFSET);
204 		if (reg != WI_COR_VALUE) {
205 			device_printf(dev, "CSM_READ_1(WI_COR_OFFSET) "
206 			    "wanted %d, got %d\n", WI_COR_VALUE, reg);
207 			wi_free(dev);
208 			wlan_serialize_exit();
209 			return (ENXIO);
210 		}
211 	} else {
212 		error = wi_alloc(dev, WI_PCI_LMEMRES);
213 		if (error) {
214 			wlan_serialize_exit();
215 			return (error);
216 		}
217 
218 		CSR_WRITE_2(sc, WI_PCICOR_OFF, WI_PCICOR_RESET);
219 		DELAY(250000);
220 
221 		CSR_WRITE_2(sc, WI_PCICOR_OFF, 0x0000);
222 		DELAY(500000);
223 
224 		timeout=2000000;
225 		while ((--timeout > 0) &&
226 		    (CSR_READ_2(sc, WI_COMMAND) & WI_CMD_BUSY))
227 			DELAY(10);
228 
229 		if (timeout == 0) {
230 			device_printf(dev, "couldn't reset prism pci core.\n");
231 			wi_free(dev);
232 			wlan_serialize_exit();
233 			return(ENXIO);
234 		}
235 	}
236 
237 	CSR_WRITE_2(sc, WI_HFA384X_SWSUPPORT0_OFF, WI_PRISM2STA_MAGIC);
238 	reg = CSR_READ_2(sc, WI_HFA384X_SWSUPPORT0_OFF);
239 	if (reg != WI_PRISM2STA_MAGIC) {
240 		device_printf(dev,
241 		    "CSR_READ_2(WI_HFA384X_SWSUPPORT0_OFF) "
242 		    "wanted %d, got %d\n", WI_PRISM2STA_MAGIC, reg);
243 		wi_free(dev);
244 		wlan_serialize_exit();
245 		return (ENXIO);
246 	}
247 
248 	error = wi_attach(dev);
249 	if (error != 0)
250 		wi_free(dev);
251 	wlan_serialize_exit();
252 	return (error);
253 }
254 
255 static int
256 wi_pci_suspend(device_t dev)
257 {
258 	struct wi_softc	*sc = device_get_softc(dev);
259 
260 	wlan_serialize_enter();
261 	wi_stop(sc, 1);
262 	wlan_serialize_exit();
263 
264 	return (0);
265 }
266 
267 static int
268 wi_pci_resume(device_t dev)
269 {
270 	struct wi_softc	*sc = device_get_softc(dev);
271 	struct ifnet *ifp = sc->sc_ifp;
272 
273 	wlan_serialize_enter();
274 	if (sc->wi_bus_type != WI_BUS_PCI_NATIVE) {
275 		wlan_serialize_exit();
276 		return (0);
277 	}
278 
279 	if (ifp->if_flags & IFF_UP) {
280 		ifp->if_init(ifp->if_softc);
281 		if (ifp->if_flags & IFF_RUNNING)
282 			if_devstart(ifp);
283 	}
284 	wlan_serialize_exit();
285 
286 	return (0);
287 }
288