Do a major clean-up of the BUSDMA architecture. A large number of
[dfdiff.git] / sys / dev / netif / rtw / if_rtw_pci.c
blobfc0e85b28355842bd0d0d8ec714adbd7762305a9
1 /*
2 * Copyright (c) 2006 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Sepherosa Ziehau <sepherosa@gmail.com>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
34 * $NetBSD: if_rtw_pci.c,v 1.4 2005/12/04 17:44:02 christos Exp $
35 * $DragonFly: src/sys/dev/netif/rtw/if_rtw_pci.c,v 1.2 2006/10/25 20:55:58 dillon Exp $
39 * Copyright (c) 1998, 1999, 2000, 2002 The NetBSD Foundation, Inc.
40 * All rights reserved.
42 * This code is derived from software contributed to The NetBSD Foundation
43 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
44 * NASA Ames Research Center; Charles M. Hannum; and David Young.
46 * Redistribution and use in source and binary forms, with or without
47 * modification, are permitted provided that the following conditions
48 * are met:
49 * 1. Redistributions of source code must retain the above copyright
50 * notice, this list of conditions and the following disclaimer.
51 * 2. Redistributions in binary form must reproduce the above copyright
52 * notice, this list of conditions and the following disclaimer in the
53 * documentation and/or other materials provided with the distribution.
54 * 3. All advertising materials mentioning features or use of this software
55 * must display the following acknowledgement:
56 * This product includes software developed by the NetBSD
57 * Foundation, Inc. and its contributors.
58 * 4. Neither the name of The NetBSD Foundation nor the names of its
59 * contributors may be used to endorse or promote products derived
60 * from this software without specific prior written permission.
62 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
63 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
64 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
65 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
66 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
67 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
68 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
69 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
70 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
71 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
72 * POSSIBILITY OF SUCH DAMAGE.
76 * PCI bus front-end for the Realtek RTL8180 802.11 MAC/BBP chip.
79 #include <sys/param.h>
80 #include <sys/kernel.h>
81 #include <sys/bus.h>
82 #include <sys/rman.h>
83 #include <sys/socket.h>
85 #include <bus/pci/pcivar.h>
86 #include <bus/pci/pcireg.h>
87 #include <bus/pci/pcidevs.h>
89 #include <net/if.h>
90 #include <net/if_arp.h>
91 #include <net/if_media.h>
93 #include <netproto/802_11/ieee80211_var.h>
94 #include <netproto/802_11/ieee80211_radiotap.h>
96 #include "rtwreg.h"
97 #include "sa2400reg.h"
98 #include "rtwvar.h"
101 * PCI configuration space registers
103 #define RTW_PCI_IOBA 0x10 /* i/o mapped base */
104 #define RTW_PCI_MMBA 0x14 /* memory mapped base */
106 static const struct rtw_pci_reg {
107 int reg_type;
108 int reg_rid;
109 } rtw_pci_regs[] = {
110 /* Prefer IO memory over IO port */
111 { SYS_RES_MEMORY, RTW_PCI_MMBA },
112 { SYS_RES_IOPORT, RTW_PCI_IOBA }
115 static const struct rtw_pci_product {
116 uint16_t app_vendor; /* PCI vendor ID */
117 uint16_t app_product; /* PCI product ID */
118 const char *app_product_name;
119 } rtw_pci_products[] = {
120 { PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8180,
121 "Realtek RTL8180 802.11 MAC/BBP" },
122 { PCI_VENDOR_BELKIN, PCI_PRODUCT_BELKIN_F5D6001,
123 "Belkin F5D6001" },
125 { 0, 0, NULL }
128 static int rtw_pci_probe(device_t);
129 static int rtw_pci_attach(device_t);
130 static int rtw_pci_detach(device_t);
131 static int rtw_pci_shutdown(device_t);
133 static device_method_t rtw_pci_methods[] = {
134 DEVMETHOD(device_probe, rtw_pci_probe),
135 DEVMETHOD(device_attach, rtw_pci_attach),
136 DEVMETHOD(device_detach, rtw_pci_detach),
137 DEVMETHOD(device_shutdown, rtw_pci_shutdown),
138 #if 0
139 DEVMETHOD(device_suspend, rtw_pci_suspend),
140 DEVMETHOD(device_resume, rtw_pci_resume),
141 #endif
142 { 0, 0 }
145 static driver_t rtw_pci_driver = {
146 "rtw",
147 rtw_pci_methods,
148 sizeof(struct rtw_softc)
151 DRIVER_MODULE(rtw, pci, rtw_pci_driver, rtw_devclass, 0, 0);
152 DRIVER_MODULE(rtw, cardbus, rtw_pci_driver, rtw_devclass, 0, 0);
154 MODULE_DEPEND(rtw, wlan, 1, 1, 1);
155 MODULE_DEPEND(rtw, wlan_ratectl_onoe, 1, 1, 1);
156 MODULE_DEPEND(rtw, pci, 1, 1, 1);
157 MODULE_DEPEND(rtw, cardbus, 1, 1, 1);
159 static int
160 rtw_pci_probe(device_t dev)
162 const struct rtw_pci_product *app;
163 uint16_t vid, did;
165 vid = pci_get_vendor(dev);
166 did = pci_get_device(dev);
167 for (app = rtw_pci_products; app->app_product_name != NULL; app++) {
168 if (vid == app->app_vendor && did == app->app_product) {
169 device_set_desc(dev, app->app_product_name);
170 return 0;
173 return ENXIO;
176 static int
177 rtw_pci_attach(device_t dev)
179 struct rtw_softc *sc = device_get_softc(dev);
180 struct rtw_regs *regs = &sc->sc_regs;
181 int i, error;
184 * No power management hooks.
185 * XXX Maybe we should add some!
187 sc->sc_flags |= RTW_F_ENABLED;
189 sc->sc_rev = pci_get_revid(dev);
191 #ifndef BURN_BRIDGES
192 if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
193 uint32_t mem, port, irq;
195 mem = pci_read_config(dev, RTW_PCI_MMBA, 4);
196 port = pci_read_config(dev, RTW_PCI_IOBA, 4);
197 irq = pci_read_config(dev, PCIR_INTLINE, 4);
199 device_printf(dev, "chip is in D%d power mode "
200 "-- setting to D0\n", pci_get_powerstate(dev));
202 pci_set_powerstate(dev, PCI_POWERSTATE_D0);
204 pci_write_config(dev, RTW_PCI_MMBA, mem, 4);
205 pci_write_config(dev, RTW_PCI_IOBA, port, 4);
206 pci_write_config(dev, PCIR_INTLINE, irq, 4);
208 #endif /* !BURN_BRIDGES */
210 /* Enable PCI bus master */
211 pci_enable_busmaster(dev);
213 /* Allocate IO memory/port */
214 #define N(arr) sizeof(arr) / sizeof(arr[0])
215 for (i = 0; i < N(rtw_pci_regs); ++i) {
216 regs->r_rid = rtw_pci_regs[i].reg_rid;
217 regs->r_type = rtw_pci_regs[i].reg_type;
218 regs->r_res = bus_alloc_resource_any(dev, regs->r_type,
219 &regs->r_rid, RF_ACTIVE);
220 if (regs->r_res != NULL)
221 break;
223 #undef N
224 if (regs->r_res == NULL) {
225 device_printf(dev, "can't allocate IO mem/port\n");
226 return ENXIO;
228 regs->r_bh = rman_get_bushandle(regs->r_res);
229 regs->r_bt = rman_get_bustag(regs->r_res);
231 error = rtw_attach(dev);
232 if (error)
233 rtw_pci_detach(dev);
234 return error;
237 static int
238 rtw_pci_detach(device_t dev)
240 struct rtw_softc *sc = device_get_softc(dev);
241 struct rtw_regs *regs = &sc->sc_regs;
243 if (device_is_attached(dev))
244 rtw_detach(dev);
246 if (regs->r_res != NULL) {
247 bus_release_resource(dev, regs->r_type, regs->r_rid,
248 regs->r_res);
250 return 0;
253 static int
254 rtw_pci_shutdown(device_t dev)
256 rtw_stop(device_get_softc(dev), 1);
257 return 0;