xref: /dragonfly/sys/dev/netif/ath/ath/if_ath_pci.c (revision 685c703c)
1 /*
2  * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
3  * 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  *    without modification.
11  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13  *    redistribution must be conditioned upon including a substantially
14  *    similar Disclaimer requirement for further binary redistribution.
15  * 3. Neither the names of the above-listed copyright holders nor the names
16  *    of any contributors may be used to endorse or promote products derived
17  *    from this software without specific prior written permission.
18  *
19  * Alternatively, this software may be distributed under the terms of the
20  * GNU General Public License ("GPL") version 2 as published by the Free
21  * Software Foundation.
22  *
23  * NO WARRANTY
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
27  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
28  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
29  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
32  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
34  * THE POSSIBILITY OF SUCH DAMAGES.
35  *
36  * $FreeBSD: src/sys/dev/ath/if_ath_pci.c,v 1.12 2005/03/05 19:06:12 imp Exp $
37  * $DragonFly: src/sys/dev/netif/ath/ath/if_ath_pci.c,v 1.1 2006/07/13 09:15:22 sephe Exp $
38  */
39 
40 /*
41  * PCI/Cardbus front-end for the Atheros Wireless LAN controller driver.
42  */
43 
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/module.h>
47 #include <sys/kernel.h>
48 #include <sys/errno.h>
49 #include <sys/sysctl.h>
50 
51 #include <machine/bus.h>
52 #include <machine/resource.h>
53 #include <sys/bus.h>
54 #include <sys/rman.h>
55 
56 #include <sys/socket.h>
57 
58 #include <net/if.h>
59 #include <net/if_media.h>
60 #include <net/if_arp.h>
61 
62 #include <netproto/802_11/ieee80211_var.h>
63 
64 #include <bus/pci/pcivar.h>
65 #include <bus/pci/pcireg.h>
66 
67 #include <dev/netif/ath/ath/if_athvar.h>
68 #include <contrib/dev/ath/ah.h>
69 
70 static int	ath_pci_probe(device_t);
71 static int	ath_pci_attach(device_t);
72 static int	ath_pci_detach(device_t);
73 static int	ath_pci_shutdown(device_t);
74 static int	ath_pci_suspend(device_t);
75 static int	ath_pci_resume(device_t);
76 
77 static void	ath_pci_setup(device_t);
78 
79 /*
80  * PCI glue.
81  */
82 
83 struct ath_pci_softc {
84 	struct ath_softc	sc_sc;
85 	struct resource		*sc_sr;		/* memory resource */
86 	int			sc_srid;
87 };
88 
89 #define	BS_BAR	0x10
90 #define	PCIR_RETRY_TIMEOUT	0x41
91 
92 static int
93 ath_pci_probe(device_t dev)
94 {
95 	const char* devname;
96 
97 	devname = ath_hal_probe(pci_get_vendor(dev), pci_get_device(dev));
98 	if (devname != NULL) {
99 		device_set_desc(dev, devname);
100 		return 0;
101 	}
102 	return ENXIO;
103 }
104 
105 static void
106 ath_pci_setup(device_t dev)
107 {
108 	/*
109 	 * Enable memory mapping and bus mastering.
110 	 */
111 	pci_enable_busmaster(dev);
112 
113 	/*
114 	 * Disable retry timeout to keep PCI Tx retries from
115 	 * interfering with C3 CPU state.
116 	 */
117 	pci_write_config(dev, PCIR_RETRY_TIMEOUT, 0, 1);
118 }
119 
120 static int
121 ath_pci_attach(device_t dev)
122 {
123 	struct ath_pci_softc *psc = device_get_softc(dev);
124 	struct ath_softc *sc = &psc->sc_sc;
125 	int error;
126 
127 	sc->sc_dev = dev;
128 
129 	ath_pci_setup(dev);
130 
131 	/*
132 	 * Setup memory-mapping of PCI registers.
133 	 */
134 	psc->sc_srid = BS_BAR;
135 	psc->sc_sr = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &psc->sc_srid,
136 					    RF_ACTIVE);
137 	if (psc->sc_sr == NULL) {
138 		device_printf(dev, "cannot map register space\n");
139 		return ENXIO;
140 	}
141 	sc->sc_st = rman_get_bustag(psc->sc_sr);
142 	sc->sc_sh = rman_get_bushandle(psc->sc_sr);
143 
144 	/*
145 	 * Setup DMA descriptor area.
146 	 */
147 	error = bus_dma_tag_create(NULL, 1, 0,
148 				   BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
149 				   NULL, NULL, 0x3ffff/* maxsize XXX */,
150 				   ATH_MAX_SCATTER, BUS_SPACE_MAXADDR,
151 				   BUS_DMA_ALLOCNOW, &sc->sc_dmat);
152 	if (error) {
153 		device_printf(dev, "cannot allocate DMA tag\n");
154 		goto back;
155 	}
156 
157 	error = ath_attach(pci_get_device(dev), sc);
158 
159 back:
160 	if (error)
161 		ath_pci_detach(dev);
162 	return error;
163 }
164 
165 static int
166 ath_pci_detach(device_t dev)
167 {
168 	struct ath_pci_softc *psc = device_get_softc(dev);
169 	struct ath_softc *sc = &psc->sc_sc;
170 
171 	/* XXX check if device was removed */
172 	sc->sc_invalid = !bus_child_present(dev);
173 
174 	if (device_is_attached(dev))
175 		ath_detach(sc);
176 
177 	bus_generic_detach(dev);
178 
179 	if (sc->sc_dmat != NULL)
180 		bus_dma_tag_destroy(sc->sc_dmat);
181 
182 	if (psc->sc_sr != NULL) {
183 		bus_release_resource(dev, SYS_RES_MEMORY, psc->sc_srid,
184 				     psc->sc_sr);
185 	}
186 	return 0;
187 }
188 
189 static int
190 ath_pci_shutdown(device_t dev)
191 {
192 	struct ath_pci_softc *psc = device_get_softc(dev);
193 
194 	ath_shutdown(&psc->sc_sc);
195 	return (0);
196 }
197 
198 static int
199 ath_pci_suspend(device_t dev)
200 {
201 	struct ath_pci_softc *psc = device_get_softc(dev);
202 
203 	ath_suspend(&psc->sc_sc);
204 	return (0);
205 }
206 
207 static int
208 ath_pci_resume(device_t dev)
209 {
210 	struct ath_pci_softc *psc = device_get_softc(dev);
211 
212 	ath_pci_setup(dev);
213 	ath_resume(&psc->sc_sc);
214 	return (0);
215 }
216 
217 static device_method_t ath_pci_methods[] = {
218 	/* Device interface */
219 	DEVMETHOD(device_probe,		ath_pci_probe),
220 	DEVMETHOD(device_attach,	ath_pci_attach),
221 	DEVMETHOD(device_detach,	ath_pci_detach),
222 	DEVMETHOD(device_shutdown,	ath_pci_shutdown),
223 	DEVMETHOD(device_suspend,	ath_pci_suspend),
224 	DEVMETHOD(device_resume,	ath_pci_resume),
225 
226 	{ 0, 0 }
227 };
228 
229 static driver_t ath_pci_driver = {
230 	"ath",
231 	ath_pci_methods,
232 	sizeof (struct ath_pci_softc)
233 };
234 
235 static	devclass_t ath_devclass;
236 
237 DRIVER_MODULE(if_ath, pci, ath_pci_driver, ath_devclass, 0, 0);
238 DRIVER_MODULE(if_ath, cardbus, ath_pci_driver, ath_devclass, 0, 0);
239 
240 MODULE_VERSION(if_ath, 1);
241 
242 MODULE_DEPEND(if_ath, ath_hal, 1, 1, 1);	/* Atheros HAL */
243 MODULE_DEPEND(if_ath, ath_rate, 1, 1, 1);	/* rate control algorithm */
244 MODULE_DEPEND(if_ath, wlan, 1, 1, 1);		/* 802.11 media layer */
245