xref: /dragonfly/sys/dev/netif/wi/if_wi_pci.c (revision af79c6e5)
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: src/sys/dev/wi/if_wi_pci.c,v 1.8.2.3 2002/06/16 18:07:18 nsayer Exp $
33  * $DragonFly: src/sys/dev/netif/wi/if_wi_pci.c,v 1.3 2003/08/07 21:17:06 dillon Exp $
34  */
35 
36 /*
37  * Lucent WaveLAN/IEEE 802.11 PCMCIA driver for FreeBSD.
38  *
39  * Written by Bill Paul <wpaul@ctr.columbia.edu>
40  * Electrical Engineering Department
41  * Columbia University, New York City
42  */
43 
44 #include <sys/param.h>
45 #include <sys/kernel.h>
46 #include <sys/socket.h>
47 #include <sys/systm.h>
48 #include <sys/module.h>
49 #include <sys/bus.h>
50 
51 #include <machine/bus.h>
52 #include <machine/resource.h>
53 #include <machine/clock.h>
54 #include <sys/rman.h>
55 
56 #include <bus/pci/pcireg.h>
57 #include <bus/pci/pcivar.h>
58 
59 #include <net/if.h>
60 #include <net/if_arp.h>
61 #include <net/ethernet.h>
62 #include <net/if_media.h>
63 #include <net/if_types.h>
64 #include <net/if_ieee80211.h>
65 
66 #include "if_wavelan_ieee.h"
67 #include "wi_hostap.h"
68 #include "if_wivar.h"
69 #include "if_wireg.h"
70 
71 static int wi_pci_probe(device_t);
72 static int wi_pci_attach(device_t);
73 
74 static device_method_t wi_pci_methods[] = {
75 	/* Device interface */
76 	DEVMETHOD(device_probe,		wi_pci_probe),
77 	DEVMETHOD(device_attach,	wi_pci_attach),
78 	DEVMETHOD(device_detach,	wi_generic_detach),
79 	DEVMETHOD(device_shutdown,	wi_shutdown),
80 
81 	{ 0, 0 }
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, 0x3873, WI_BUS_PCI_NATIVE, "Intersil Prism2.5"},
99 	{0x16ab, 0x1102, WI_BUS_PCI_PLX, "Linksys WDT11"},
100 	{0x1385, 0x4100, WI_BUS_PCI_PLX, "Netgear MA301"},
101 	{0x1638, 0x1100, WI_BUS_PCI_PLX, "PRISM2STA WaveLAN"},
102 	{0x111a, 0x1023, WI_BUS_PCI_PLX, "Siemens SpeedStream"},
103 	{0x16ec, 0x3685, WI_BUS_PCI_PLX, "US Robotics 2415"},
104 	{0, 0, 0, NULL}
105 };
106 
107 DRIVER_MODULE(if_wi, pci, wi_pci_driver, wi_devclass, 0, 0);
108 
109 static int
110 wi_pci_probe(dev)
111 	device_t	dev;
112 {
113 	struct wi_softc		*sc;
114 	int i;
115 
116 	sc = device_get_softc(dev);
117 	for(i=0; pci_ids[i].vendor != 0; i++) {
118 		if ((pci_get_vendor(dev) == pci_ids[i].vendor) &&
119 			(pci_get_device(dev) == pci_ids[i].device)) {
120 			sc->wi_bus_type = pci_ids[i].bus_type;
121 			device_set_desc(dev, pci_ids[i].desc);
122 			return (0);
123 		}
124 	}
125 	return(ENXIO);
126 }
127 
128 static int
129 wi_pci_attach(device_t dev)
130 {
131 	struct wi_softc		*sc;
132 	u_int32_t		command, wanted;
133 	u_int16_t		reg;
134 	int			error;
135 	int			timeout;
136 
137 	sc = device_get_softc(dev);
138 
139 	command = pci_read_config(dev, PCIR_COMMAND, 4);
140 	wanted = PCIM_CMD_PORTEN|PCIM_CMD_MEMEN;
141 	command |= wanted;
142 	pci_write_config(dev, PCIR_COMMAND, command, 4);
143 	command = pci_read_config(dev, PCIR_COMMAND, 4);
144 	if ((command & wanted) != wanted) {
145 		device_printf(dev, "wi_pci_attach() failed to enable pci!\n");
146 		return (ENXIO);
147 	}
148 
149 	if (sc->wi_bus_type != WI_BUS_PCI_NATIVE) {
150 		error = wi_alloc(dev, WI_PCI_IORES);
151 		if (error)
152 			return (error);
153 
154 		/* Make sure interrupts are disabled. */
155 		CSR_WRITE_2(sc, WI_INT_EN, 0);
156 		CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
157 
158 		/* We have to do a magic PLX poke to enable interrupts */
159 		sc->local_rid = WI_PCI_LOCALRES;
160 		sc->local = bus_alloc_resource(dev, SYS_RES_IOPORT,
161 		    &sc->local_rid, 0, ~0, 1, RF_ACTIVE);
162 		sc->wi_localtag = rman_get_bustag(sc->local);
163 		sc->wi_localhandle = rman_get_bushandle(sc->local);
164 		command = bus_space_read_4(sc->wi_localtag, sc->wi_localhandle,
165 		    WI_LOCAL_INTCSR);
166 		command |= WI_LOCAL_INTEN;
167 		bus_space_write_4(sc->wi_localtag, sc->wi_localhandle,
168 		    WI_LOCAL_INTCSR, command);
169 		bus_release_resource(dev, SYS_RES_IOPORT, sc->local_rid,
170 		    sc->local);
171 		sc->local = NULL;
172 
173 		sc->mem_rid = WI_PCI_MEMRES;
174 		sc->mem = bus_alloc_resource(dev, SYS_RES_MEMORY, &sc->mem_rid,
175 					0, ~0, 1, RF_ACTIVE);
176 		if (sc->mem == NULL) {
177 			device_printf(dev, "couldn't allocate memory\n");
178 			wi_free(dev);
179 			return (ENXIO);
180 		}
181 		sc->wi_bmemtag = rman_get_bustag(sc->mem);
182 		sc->wi_bmemhandle = rman_get_bushandle(sc->mem);
183 
184 		/*
185 		 * From Linux driver:
186 		 * Write COR to enable PC card
187 		 * This is a subset of the protocol that the pccard bus code
188 		 * would do.
189 		 */
190 		CSM_WRITE_1(sc, WI_COR_OFFSET, WI_COR_VALUE);
191 		reg = CSM_READ_1(sc, WI_COR_OFFSET);
192 		if (reg != WI_COR_VALUE) {
193 			device_printf(dev, "CSM_READ_1(WI_COR_OFFSET) "
194 			    "wanted %d, got %d\n", WI_COR_VALUE, reg);
195 			wi_free(dev);
196 			return (ENXIO);
197 		}
198 	} else {
199 		error = wi_alloc(dev, WI_PCI_LMEMRES);
200 		if (error)
201 			return (error);
202 
203 		CSR_WRITE_2(sc, WI_HFA384X_PCICOR_OFF, 0x0080);
204 		DELAY(250000);
205 
206 		CSR_WRITE_2(sc, WI_HFA384X_PCICOR_OFF, 0x0000);
207 		DELAY(500000);
208 
209 		timeout=2000000;
210 		while ((--timeout > 0) &&
211 		    (CSR_READ_2(sc, WI_COMMAND) & WI_CMD_BUSY))
212 			DELAY(10);
213 
214 		if (timeout == 0) {
215 			device_printf(dev, "couldn't reset prism2.5 core.\n");
216 			wi_free(dev);
217 			return(ENXIO);
218 		}
219 	}
220 
221 	CSR_WRITE_2(sc, WI_HFA384X_SWSUPPORT0_OFF, WI_PRISM2STA_MAGIC);
222 	reg = CSR_READ_2(sc, WI_HFA384X_SWSUPPORT0_OFF);
223 	if (reg != WI_PRISM2STA_MAGIC) {
224 		device_printf(dev,
225 		    "CSR_READ_2(WI_HFA384X_SWSUPPORT0_OFF) "
226 		    "wanted %d, got %d\n", WI_PRISM2STA_MAGIC, reg);
227 		wi_free(dev);
228 		return (ENXIO);
229 	}
230 
231 	error = wi_generic_attach(dev);
232 	if (error != 0)
233 		return (error);
234 
235 	return (0);
236 }
237