Merge from vendor branch GCC:
[dragonfly.git] / sys / dev / netif / wi / if_wi_pci.c
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.22 2004/03/17 17:50:48 njl Exp $
33  * $DragonFly: src/sys/dev/netif/wi/if_wi_pci.c,v 1.8 2005/12/16 21:05:48 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 #include <sys/thread.h>
51 #include <sys/serialize.h>
52 #include <sys/thread2.h>
53
54 #include <machine/bus.h>
55 #include <machine/resource.h>
56 #include <machine/clock.h>
57 #include <sys/rman.h>
58
59 #include <bus/pci/pcireg.h>
60 #include <bus/pci/pcivar.h>
61
62 #include <net/if.h>
63 #include <net/if_arp.h>
64 #include <net/ethernet.h>
65 #include <net/if_media.h>
66 #include <net/if_types.h>
67
68 #include <netproto/802_11/ieee80211_var.h>
69 #include <netproto/802_11/ieee80211_radiotap.h>
70 #include <netproto/802_11/if_wavelan_ieee.h>
71
72 #include <dev/netif/wi/if_wireg.h>
73 #include <dev/netif/wi/if_wivar.h>
74
75 static int wi_pci_probe(device_t);
76 static int wi_pci_attach(device_t);
77 static int wi_pci_suspend(device_t);
78 static int wi_pci_resume(device_t);
79
80 static device_method_t wi_pci_methods[] = {
81         /* Device interface */
82         DEVMETHOD(device_probe,         wi_pci_probe),
83         DEVMETHOD(device_attach,        wi_pci_attach),
84         DEVMETHOD(device_detach,        wi_detach),
85         DEVMETHOD(device_shutdown,      wi_shutdown),
86         DEVMETHOD(device_suspend,       wi_pci_suspend),
87         DEVMETHOD(device_resume,        wi_pci_resume),
88
89         { 0, 0 }
90 };
91
92 static driver_t wi_pci_driver = {
93         "wi",
94         wi_pci_methods,
95         sizeof(struct wi_softc)
96 };
97
98 static struct wi_pci_card {
99         unsigned int vendor,device;
100         int bus_type;
101         char *desc;
102 } wi_pci_cards[] = {
103         /* Sorted by description */
104         {0x10b7, 0x7770, WI_BUS_PCI_PLX, "3Com Airconnect"},
105         {0x16ab, 0x1101, WI_BUS_PCI_PLX, "GLPRISM2 WaveLAN"},
106         {0x1260, 0x3872, WI_BUS_PCI_NATIVE, "Intersil Prism3"},
107         {0x1260, 0x3873, WI_BUS_PCI_NATIVE, "Intersil Prism2.5"},
108         {0x16ab, 0x1102, WI_BUS_PCI_PLX, "Linksys WDT11"},
109         {0x1385, 0x4100, WI_BUS_PCI_PLX, "Netgear MA301"},
110         {0x1638, 0x1100, WI_BUS_PCI_PLX, "PRISM2STA WaveLAN"},
111         {0x111a, 0x1023, WI_BUS_PCI_PLX, "Siemens SpeedStream"},
112         {0x10b5, 0x9050, WI_BUS_PCI_PLX, "SMC 2602W"},
113         {0x16ec, 0x3685, WI_BUS_PCI_PLX, "US Robotics 2415"},
114         {0x4033, 0x7001, WI_BUS_PCI_PLX, "Addtron AWA-100 PCI"},
115         {0, 0, 0, NULL}
116 };
117
118 DRIVER_MODULE(wi, pci, wi_pci_driver, wi_devclass, 0, 0);
119 MODULE_DEPEND(wi, pci, 1, 1, 1);
120 MODULE_DEPEND(wi, wlan, 1, 1, 1);
121
122 static int
123 wi_pci_probe(device_t dev)
124 {
125         struct wi_softc *sc;
126         struct wi_pci_card *p;
127         uint16_t vendor, device;
128
129         vendor = pci_get_vendor(dev);
130         device = pci_get_device(dev);
131         for (p = wi_pci_cards; p->vendor != NULL; p++) {
132                 if (vendor == p->vendor && device == p->device) {
133                         sc = device_get_softc(dev);
134                         sc->wi_bus_type = p->bus_type;
135                         device_set_desc(dev, p->desc);
136                         return (0);
137                 }
138         }
139         return(ENXIO);
140 }
141
142 static int
143 wi_pci_attach(device_t dev)
144 {
145         struct wi_softc         *sc;
146         u_int16_t               reg;
147         int                     error;
148
149         sc = device_get_softc(dev);
150
151         if (sc->wi_bus_type != WI_BUS_PCI_NATIVE) {
152                 uint32_t command;
153
154                 error = wi_alloc(dev, WI_PCI_IORES);
155                 if (error)
156                         return (error);
157
158                 /* Make sure interrupts are disabled. */
159                 CSR_WRITE_2(sc, WI_INT_EN, 0);
160                 CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
161
162                 /* We have to do a magic PLX poke to enable interrupts */
163                 sc->local_rid = WI_PCI_LOCALRES;
164                 sc->local = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
165                     &sc->local_rid, RF_ACTIVE);
166                 sc->wi_localtag = rman_get_bustag(sc->local);
167                 sc->wi_localhandle = rman_get_bushandle(sc->local);
168                 command = bus_space_read_4(sc->wi_localtag, sc->wi_localhandle,
169                     WI_LOCAL_INTCSR);
170                 command |= WI_LOCAL_INTEN;
171                 bus_space_write_4(sc->wi_localtag, sc->wi_localhandle,
172                     WI_LOCAL_INTCSR, command);
173                 bus_release_resource(dev, SYS_RES_IOPORT, sc->local_rid,
174                     sc->local);
175                 sc->local = NULL;
176
177                 sc->mem_rid = WI_PCI_MEMRES;
178                 sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
179                                         &sc->mem_rid, RF_ACTIVE);
180                 if (sc->mem == NULL) {
181                         device_printf(dev, "couldn't allocate memory\n");
182                         wi_free(dev);
183                         return (ENXIO);
184                 }
185                 sc->wi_bmemtag = rman_get_bustag(sc->mem);
186                 sc->wi_bmemhandle = rman_get_bushandle(sc->mem);
187
188                 /*
189                  * From Linux driver:
190                  * Write COR to enable PC card
191                  * This is a subset of the protocol that the pccard bus code
192                  * would do.
193                  */
194                 CSM_WRITE_1(sc, WI_COR_OFFSET, WI_COR_VALUE); 
195                 reg = CSM_READ_1(sc, WI_COR_OFFSET);
196                 if (reg != WI_COR_VALUE) {
197                         device_printf(dev, "CSM_READ_1(WI_COR_OFFSET) "
198                             "wanted %d, got %d\n", WI_COR_VALUE, reg);
199                         wi_free(dev);
200                         return (ENXIO);
201                 }
202         } else {
203                 int timeout;
204
205                 error = wi_alloc(dev, WI_PCI_LMEMRES);
206                 if (error)
207                         return (error);
208
209                 CSR_WRITE_2(sc, WI_HFA384X_PCICOR_OFF, 0x0080);
210                 DELAY(250000);
211
212                 CSR_WRITE_2(sc, WI_HFA384X_PCICOR_OFF, 0x0000);
213                 DELAY(500000);
214
215                 timeout=2000000;
216                 while ((--timeout > 0) &&
217                     (CSR_READ_2(sc, WI_COMMAND) & WI_CMD_BUSY))
218                         DELAY(10);
219
220                 if (timeout == 0) {
221                         device_printf(dev, "couldn't reset prism2.5 core.\n");
222                         wi_free(dev);
223                         return(ENXIO);
224                 }
225         }
226
227         CSR_WRITE_2(sc, WI_HFA384X_SWSUPPORT0_OFF, WI_PRISM2STA_MAGIC);
228         reg = CSR_READ_2(sc, WI_HFA384X_SWSUPPORT0_OFF);
229         if (reg != WI_PRISM2STA_MAGIC) {
230                 device_printf(dev,
231                     "CSR_READ_2(WI_HFA384X_SWSUPPORT0_OFF) "
232                     "wanted %d, got %d\n", WI_PRISM2STA_MAGIC, reg);
233                 wi_free(dev);
234                 return (ENXIO);
235         }
236
237         return wi_attach(dev);
238 }
239
240 static int
241 wi_pci_suspend(device_t dev)
242 {
243         struct wi_softc         *sc;
244         struct ifnet *ifp;
245
246         sc = device_get_softc(dev);
247         ifp = &sc->sc_if;
248
249         lwkt_serialize_enter(ifp->if_serializer);
250         wi_stop(ifp, 1);
251         lwkt_serialize_exit(ifp->if_serializer);
252         
253         return 0;
254 }
255
256 static int
257 wi_pci_resume(device_t dev)
258 {
259         struct wi_softc *sc;
260         struct ifnet *ifp;
261
262         sc = device_get_softc(dev);
263         ifp = &sc->sc_if;
264
265         lwkt_serialize_enter(ifp->if_serializer);
266
267         if (sc->wi_bus_type != WI_BUS_PCI_NATIVE) {
268                 lwkt_serialize_exit(ifp->if_serializer);
269                 return 0;
270         }
271
272         if (ifp->if_flags & IFF_UP) {
273                 ifp->if_init(ifp->if_softc);
274                 if (ifp->if_flags & IFF_RUNNING)
275                         ifp->if_start(ifp);
276         }
277
278         lwkt_serialize_exit(ifp->if_serializer);
279
280         return 0;
281 }