xref: /freebsd/sys/dev/mwl/if_mwl_pci.c (revision 40a8ac8f62b535d30349faf28cf47106b7041b83)
1 /*-
2  * Copyright (c) 2007-2009 Sam Leffler, Errno Consulting
3  * Copyright (c) 2007-2009 Marvell Semiconductor, Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer,
11  *    without modification.
12  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
13  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
14  *    redistribution must be conditioned upon including a substantially
15  *    similar Disclaimer requirement for further binary redistribution.
16  *
17  * NO WARRANTY
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
21  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
23  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
26  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28  * THE POSSIBILITY OF SUCH DAMAGES.
29  */
30 
31 #include <sys/cdefs.h>
32 #ifdef __FreeBSD__
33 __FBSDID("$FreeBSD$");
34 #endif
35 
36 /*
37  * PCI front-end for the Marvell Wireless LAN controller driver.
38  */
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/module.h>
43 #include <sys/kernel.h>
44 #include <sys/lock.h>
45 #include <sys/malloc.h>
46 #include <sys/mutex.h>
47 #include <sys/errno.h>
48 
49 #include <machine/bus.h>
50 #include <machine/resource.h>
51 #include <sys/bus.h>
52 #include <sys/rman.h>
53 
54 #include <sys/socket.h>
55 
56 #include <net/ethernet.h>
57 #include <net/if.h>
58 #include <net/if_media.h>
59 #include <net/if_arp.h>
60 #include <net/route.h>
61 
62 #include <net80211/ieee80211_var.h>
63 
64 #include <dev/mwl/if_mwlvar.h>
65 
66 #include <dev/pci/pcivar.h>
67 #include <dev/pci/pcireg.h>
68 
69 /*
70  * PCI glue.
71  */
72 
73 struct mwl_pci_softc {
74 	struct mwl_softc	sc_sc;
75 	struct resource		*sc_sr0;	/* BAR0 memory resource */
76 	struct resource		*sc_sr1;	/* BAR1 memory resource */
77 	struct resource		*sc_irq;	/* irq resource */
78 	void			*sc_ih;		/* interrupt handler */
79 };
80 
81 #define	BS_BAR0	0x10
82 #define	BS_BAR1	0x14
83 
84 struct mwl_pci_ident {
85 	uint16_t	vendor;
86 	uint16_t	device;
87 	const char	*name;
88 };
89 
90 static const struct mwl_pci_ident mwl_pci_ids[] = {
91 	{ 0x11ab, 0x2a02, "Marvell 88W8363" },
92 	{ 0x11ab, 0x2a03, "Marvell 88W8363" },
93 	{ 0x11ab, 0x2a0a, "Marvell 88W8363" },
94 	{ 0x11ab, 0x2a0b, "Marvell 88W8363" },
95 	{ 0x11ab, 0x2a0c, "Marvell 88W8363" },
96 	{ 0x11ab, 0x2a21, "Marvell 88W8363" },
97 	{ 0x11ab, 0x2a24, "Marvell 88W8363" },
98 
99 	{ 0, 0, NULL }
100 };
101 
102 const static struct mwl_pci_ident *
103 mwl_pci_lookup(int vendor, int device)
104 {
105 	const struct mwl_pci_ident *ident;
106 
107 	for (ident = mwl_pci_ids; ident->name != NULL; ident++)
108 		if (vendor == ident->vendor && device == ident->device)
109 			return ident;
110 	return NULL;
111 }
112 
113 static int
114 mwl_pci_probe(device_t dev)
115 {
116 	const struct mwl_pci_ident *ident;
117 
118 	ident = mwl_pci_lookup(pci_get_vendor(dev), pci_get_device(dev));
119 	if (ident != NULL) {
120 		device_set_desc(dev, ident->name);
121 		return BUS_PROBE_DEFAULT;
122 	}
123 	return ENXIO;
124 }
125 
126 static int
127 mwl_pci_attach(device_t dev)
128 {
129 	struct mwl_pci_softc *psc = device_get_softc(dev);
130 	struct mwl_softc *sc = &psc->sc_sc;
131 	int rid, error = ENXIO;
132 
133 	sc->sc_dev = dev;
134 
135 	pci_enable_busmaster(dev);
136 
137 	/*
138 	 * Setup memory-mapping of PCI registers.
139 	 */
140 	rid = BS_BAR0;
141 	psc->sc_sr0 = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
142 					    RF_ACTIVE);
143 	if (psc->sc_sr0 == NULL) {
144 		device_printf(dev, "cannot map BAR0 register space\n");
145 		goto bad;
146 	}
147 	rid = BS_BAR1;
148 	psc->sc_sr1 = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
149 					    RF_ACTIVE);
150 	if (psc->sc_sr1 == NULL) {
151 		device_printf(dev, "cannot map BAR1 register space\n");
152 		goto bad1;
153 	}
154 	sc->sc_invalid = 1;
155 
156 	/*
157 	 * Arrange interrupt line.
158 	 */
159 	rid = 0;
160 	psc->sc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
161 					     RF_SHAREABLE|RF_ACTIVE);
162 	if (psc->sc_irq == NULL) {
163 		device_printf(dev, "could not map interrupt\n");
164 		goto bad2;
165 	}
166 	if (bus_setup_intr(dev, psc->sc_irq,
167 			   INTR_TYPE_NET | INTR_MPSAFE,
168 			   NULL, mwl_intr, sc, &psc->sc_ih)) {
169 		device_printf(dev, "could not establish interrupt\n");
170 		goto bad3;
171 	}
172 
173 	/*
174 	 * Setup DMA descriptor area.
175 	 */
176 	if (bus_dma_tag_create(bus_get_dma_tag(dev),	/* parent */
177 			       1, 0,			/* alignment, bounds */
178 			       BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
179 			       BUS_SPACE_MAXADDR,	/* highaddr */
180 			       NULL, NULL,		/* filter, filterarg */
181 			       BUS_SPACE_MAXADDR,	/* maxsize */
182 			       MWL_TXDESC,		/* nsegments */
183 			       BUS_SPACE_MAXADDR,	/* maxsegsize */
184 			       0,			/* flags */
185 			       NULL,			/* lockfunc */
186 			       NULL,			/* lockarg */
187 			       &sc->sc_dmat)) {
188 		device_printf(dev, "cannot allocate DMA tag\n");
189 		goto bad4;
190 	}
191 
192 	/*
193 	 * Finish off the attach.
194 	 */
195 	MWL_LOCK_INIT(sc);
196 	sc->sc_io0t = rman_get_bustag(psc->sc_sr0);
197 	sc->sc_io0h = rman_get_bushandle(psc->sc_sr0);
198 	sc->sc_io1t = rman_get_bustag(psc->sc_sr1);
199 	sc->sc_io1h = rman_get_bushandle(psc->sc_sr1);
200 	if (mwl_attach(pci_get_device(dev), sc) == 0)
201 		return (0);
202 
203 	MWL_LOCK_DESTROY(sc);
204 	bus_dma_tag_destroy(sc->sc_dmat);
205 bad4:
206 	bus_teardown_intr(dev, psc->sc_irq, psc->sc_ih);
207 bad3:
208 	bus_release_resource(dev, SYS_RES_IRQ, 0, psc->sc_irq);
209 bad2:
210 	bus_release_resource(dev, SYS_RES_MEMORY, BS_BAR1, psc->sc_sr1);
211 bad1:
212 	bus_release_resource(dev, SYS_RES_MEMORY, BS_BAR0, psc->sc_sr0);
213 bad:
214 	return (error);
215 }
216 
217 static int
218 mwl_pci_detach(device_t dev)
219 {
220 	struct mwl_pci_softc *psc = device_get_softc(dev);
221 	struct mwl_softc *sc = &psc->sc_sc;
222 
223 	/* check if device was removed */
224 	sc->sc_invalid = !bus_child_present(dev);
225 
226 	mwl_detach(sc);
227 
228 	bus_generic_detach(dev);
229 	bus_teardown_intr(dev, psc->sc_irq, psc->sc_ih);
230 	bus_release_resource(dev, SYS_RES_IRQ, 0, psc->sc_irq);
231 
232 	bus_dma_tag_destroy(sc->sc_dmat);
233 	bus_release_resource(dev, SYS_RES_MEMORY, BS_BAR1, psc->sc_sr1);
234 	bus_release_resource(dev, SYS_RES_MEMORY, BS_BAR0, psc->sc_sr0);
235 
236 	MWL_LOCK_DESTROY(sc);
237 
238 	return (0);
239 }
240 
241 static int
242 mwl_pci_shutdown(device_t dev)
243 {
244 	struct mwl_pci_softc *psc = device_get_softc(dev);
245 
246 	mwl_shutdown(&psc->sc_sc);
247 	return (0);
248 }
249 
250 static int
251 mwl_pci_suspend(device_t dev)
252 {
253 	struct mwl_pci_softc *psc = device_get_softc(dev);
254 
255 	mwl_suspend(&psc->sc_sc);
256 
257 	return (0);
258 }
259 
260 static int
261 mwl_pci_resume(device_t dev)
262 {
263 	struct mwl_pci_softc *psc = device_get_softc(dev);
264 
265 	pci_enable_busmaster(dev);
266 
267 	mwl_resume(&psc->sc_sc);
268 
269 	return (0);
270 }
271 
272 static device_method_t mwl_pci_methods[] = {
273 	/* Device interface */
274 	DEVMETHOD(device_probe,		mwl_pci_probe),
275 	DEVMETHOD(device_attach,	mwl_pci_attach),
276 	DEVMETHOD(device_detach,	mwl_pci_detach),
277 	DEVMETHOD(device_shutdown,	mwl_pci_shutdown),
278 	DEVMETHOD(device_suspend,	mwl_pci_suspend),
279 	DEVMETHOD(device_resume,	mwl_pci_resume),
280 
281 	{ 0,0 }
282 };
283 static driver_t mwl_pci_driver = {
284 	"mwl",
285 	mwl_pci_methods,
286 	sizeof (struct mwl_pci_softc)
287 };
288 static	devclass_t mwl_devclass;
289 DRIVER_MODULE(mwl, pci, mwl_pci_driver, mwl_devclass, 0, 0);
290 MODULE_VERSION(mwl, 1);
291 MODULE_DEPEND(mwl, wlan, 1, 1, 1);		/* 802.11 media layer */
292 MODULE_DEPEND(mwl, firmware, 1, 1, 1);
293