2 * Copyright (c) 2007 Marvell Semiconductor, Inc.
3 * Copyright (c) 2007 Sam Leffler, Errno Consulting
4 * Copyright (c) 2008 Weongyo Jeong <weongyo@freebsd.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer,
12 * without modification.
13 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
14 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
15 * redistribution must be conditioned upon including a substantially
16 * similar Disclaimer requirement for further binary redistribution.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
22 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
24 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
27 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
29 * THE POSSIBILITY OF SUCH DAMAGES.
32 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
38 * PCI front-end for the Marvell 88W8335 Wireless LAN controller driver.
41 #include <sys/param.h>
42 #include <sys/kernel.h>
43 #include <sys/module.h>
44 #include <sys/socket.h>
45 #include <sys/sysctl.h>
47 #include <machine/bus.h>
52 #include <net/if_media.h>
54 #include <net80211/ieee80211_var.h>
56 #include <dev/malo/if_malo.h>
58 #include <dev/pci/pcivar.h>
59 #include <dev/pci/pcireg.h>
65 #define MALO_RESOURCE_MAX 2
66 #define MALO_MSI_MESSAGES 1
68 struct malo_pci_softc {
69 struct malo_softc malo_sc;
70 struct resource_spec *malo_mem_spec;
71 struct resource *malo_res_mem[MALO_RESOURCE_MAX];
72 struct resource_spec *malo_irq_spec;
73 struct resource *malo_res_irq[MALO_MSI_MESSAGES];
74 void *malo_intrhand[MALO_MSI_MESSAGES];
81 SYSCTL_DECL(_hw_malo);
82 SYSCTL_NODE(_hw_malo, OID_AUTO, pci, CTLFLAG_RD, 0,
83 "Marvell 88W8335 driver PCI parameters");
85 static int msi_disable = 0; /* MSI disabled */
86 SYSCTL_INT(_hw_malo_pci, OID_AUTO, msi_disable, CTLFLAG_RW, &msi_disable,
88 TUNABLE_INT("hw.malo.pci.msi_disable", &msi_disable);
91 * Devices supported by this driver.
93 #define VENDORID_MARVELL 0X11AB
94 #define DEVICEID_MRVL_88W8310 0X1FA7
95 #define DEVICEID_MRVL_88W8335R1 0X1FAA
96 #define DEVICEID_MRVL_88W8335R2 0X1FAB
98 static struct malo_product {
100 uint16_t mp_deviceid;
102 } malo_products[] = {
103 { VENDORID_MARVELL, DEVICEID_MRVL_88W8310,
104 "Marvell Libertas 88W8310 802.11g Wireless Adapter" },
105 { VENDORID_MARVELL, DEVICEID_MRVL_88W8335R1,
106 "Marvell Libertas 88W8335 802.11g Wireless Adapter" },
107 { VENDORID_MARVELL, DEVICEID_MRVL_88W8335R2,
108 "Marvell Libertas 88W8335 802.11g Wireless Adapter" }
111 static struct resource_spec malo_res_spec_mem[] = {
112 { SYS_RES_MEMORY, PCIR_BAR(0), RF_ACTIVE },
113 { SYS_RES_MEMORY, PCIR_BAR(1), RF_ACTIVE },
117 static struct resource_spec malo_res_spec_legacy[] = {
118 { SYS_RES_IRQ, 0, RF_ACTIVE | RF_SHAREABLE },
122 static struct resource_spec malo_res_spec_msi[] = {
123 { SYS_RES_IRQ, 1, RF_ACTIVE },
127 static int malo_pci_detach(device_t);
130 malo_pci_probe(device_t dev)
132 #define N(a) (sizeof(a) / sizeof((a)[0]))
133 struct malo_product *mp;
134 uint16_t vendor, devid;
137 vendor = pci_get_vendor(dev);
138 devid = pci_get_device(dev);
141 for (i = 0; i < N(malo_products); i++, mp++) {
142 if (vendor == mp->mp_vendorid && devid == mp->mp_deviceid) {
143 device_set_desc(dev, mp->mp_name);
144 return (BUS_PROBE_DEFAULT);
153 malo_pci_setup(device_t dev)
157 * Enable memory mapping and bus mastering.
159 if (pci_enable_busmaster(dev) != 0)
161 if (pci_enable_io(dev, SYS_RES_MEMORY) != 0)
168 malo_pci_attach(device_t dev)
170 int error = ENXIO, i, msic, reg;
171 struct malo_pci_softc *psc = device_get_softc(dev);
172 struct malo_softc *sc = &psc->malo_sc;
177 * Enable memory mapping and bus mastering.
179 if (malo_pci_setup(dev))
183 * Setup memory-mapping of PCI registers.
185 psc->malo_mem_spec = malo_res_spec_mem;
186 error = bus_alloc_resources(dev, psc->malo_mem_spec, psc->malo_res_mem);
188 device_printf(dev, "couldn't allocate memory resources\n");
193 * Arrange and allocate interrupt line.
195 sc->malo_invalid = 1;
197 if (pci_find_extcap(dev, PCIY_EXPRESS, ®) == 0) {
198 msic = pci_msi_count(dev);
200 device_printf(dev, "MSI count : %d\n", msic);
204 psc->malo_irq_spec = malo_res_spec_legacy;
205 if (msic == MALO_MSI_MESSAGES && msi_disable == 0) {
206 if (pci_alloc_msi(dev, &msic) == 0) {
207 if (msic == MALO_MSI_MESSAGES) {
208 device_printf(dev, "Using %d MSI messages\n",
210 psc->malo_irq_spec = malo_res_spec_msi;
213 pci_release_msi(dev);
217 error = bus_alloc_resources(dev, psc->malo_irq_spec, psc->malo_res_irq);
219 device_printf(dev, "couldn't allocate IRQ resources\n");
223 if (psc->malo_msi == 0)
224 error = bus_setup_intr(dev, psc->malo_res_irq[0],
225 INTR_TYPE_NET | INTR_MPSAFE, malo_intr, NULL, sc,
226 &psc->malo_intrhand[0]);
228 for (i = 0; i < MALO_MSI_MESSAGES; i++) {
229 error = bus_setup_intr(dev, psc->malo_res_irq[i],
230 INTR_TYPE_NET | INTR_MPSAFE, malo_intr, NULL, sc,
231 &psc->malo_intrhand[i]);
238 * Setup DMA descriptor area.
240 if (bus_dma_tag_create(bus_get_dma_tag(dev), /* parent */
241 1, 0, /* alignment, bounds */
242 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
243 BUS_SPACE_MAXADDR, /* highaddr */
244 NULL, NULL, /* filter, filterarg */
245 BUS_SPACE_MAXADDR, /* maxsize */
247 BUS_SPACE_MAXADDR, /* maxsegsize */
252 device_printf(dev, "cannot allocate DMA tag\n");
256 sc->malo_io0t = rman_get_bustag(psc->malo_res_mem[0]);
257 sc->malo_io0h = rman_get_bushandle(psc->malo_res_mem[0]);
258 sc->malo_io1t = rman_get_bustag(psc->malo_res_mem[1]);
259 sc->malo_io1h = rman_get_bushandle(psc->malo_res_mem[1]);
261 error = malo_attach(pci_get_device(dev), sc);
269 bus_dma_tag_destroy(sc->malo_dmat);
271 if (psc->malo_msi == 0)
272 bus_teardown_intr(dev, psc->malo_res_irq[0],
273 psc->malo_intrhand[0]);
275 for (i = 0; i < MALO_MSI_MESSAGES; i++)
276 bus_teardown_intr(dev, psc->malo_res_irq[i],
277 psc->malo_intrhand[i]);
279 bus_release_resources(dev, psc->malo_irq_spec, psc->malo_res_irq);
281 if (psc->malo_msi != 0)
282 pci_release_msi(dev);
283 bus_release_resources(dev, psc->malo_mem_spec, psc->malo_res_mem);
289 malo_pci_detach(device_t dev)
292 struct malo_pci_softc *psc = device_get_softc(dev);
293 struct malo_softc *sc = &psc->malo_sc;
295 /* check if device was removed */
296 sc->malo_invalid = !bus_child_present(dev);
300 bus_generic_detach(dev);
302 if (psc->malo_msi == 0)
303 bus_teardown_intr(dev, psc->malo_res_irq[0],
304 psc->malo_intrhand[0]);
306 for (i = 0; i < MALO_MSI_MESSAGES; i++)
307 bus_teardown_intr(dev, psc->malo_res_irq[i],
308 psc->malo_intrhand[i]);
310 pci_release_msi(dev);
313 bus_release_resources(dev, psc->malo_irq_spec, psc->malo_res_irq);
314 bus_dma_tag_destroy(sc->malo_dmat);
315 bus_release_resources(dev, psc->malo_mem_spec, psc->malo_res_mem);
321 malo_pci_shutdown(device_t dev)
323 struct malo_pci_softc *psc = device_get_softc(dev);
325 malo_shutdown(&psc->malo_sc);
331 malo_pci_suspend(device_t dev)
333 struct malo_pci_softc *psc = device_get_softc(dev);
335 malo_suspend(&psc->malo_sc);
341 malo_pci_resume(device_t dev)
343 struct malo_pci_softc *psc = device_get_softc(dev);
345 if (!malo_pci_setup(dev))
348 malo_resume(&psc->malo_sc);
353 static device_method_t malo_pci_methods[] = {
354 /* Device interface */
355 DEVMETHOD(device_probe, malo_pci_probe),
356 DEVMETHOD(device_attach, malo_pci_attach),
357 DEVMETHOD(device_detach, malo_pci_detach),
358 DEVMETHOD(device_shutdown, malo_pci_shutdown),
359 DEVMETHOD(device_suspend, malo_pci_suspend),
360 DEVMETHOD(device_resume, malo_pci_resume),
364 static driver_t malo_pci_driver = {
367 sizeof(struct malo_pci_softc)
370 static devclass_t malo_devclass;
371 DRIVER_MODULE(malo, pci, malo_pci_driver, malo_devclass, 0, 0);
372 MODULE_VERSION(malo, 1);
373 MODULE_DEPEND(malo, wlan, 1, 1, 1); /* 802.11 media layer */
374 MODULE_DEPEND(malo, firmware, 1, 1, 1);