2 * Copyright (c) 2012-2014 Qualcomm Atheros, Inc.
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <linux/module.h>
18 #include <linux/pci.h>
19 #include <linux/moduleparam.h>
20 #include <linux/interrupt.h>
24 static bool use_msi = true;
25 module_param(use_msi, bool, S_IRUGO);
26 MODULE_PARM_DESC(use_msi, " Use MSI interrupt, default - true");
29 void wil_set_capabilities(struct wil6210_priv *wil)
31 u32 rev_id = wil_r(wil, RGF_USER_JTAG_DEV_ID);
33 bitmap_zero(wil->hw_capabilities, hw_capability_last);
36 case JTAG_DEV_ID_SPARROW_B0:
37 wil->hw_name = "Sparrow B0";
38 wil->hw_version = HW_VER_SPARROW_B0;
41 wil_err(wil, "Unknown board hardware 0x%08x\n", rev_id);
42 wil->hw_name = "Unknown";
43 wil->hw_version = HW_VER_UNKNOWN;
46 wil_info(wil, "Board hardware is %s\n", wil->hw_name);
49 void wil_disable_irq(struct wil6210_priv *wil)
51 disable_irq(wil->pdev->irq);
54 void wil_enable_irq(struct wil6210_priv *wil)
56 enable_irq(wil->pdev->irq);
60 static int wil_if_pcie_enable(struct wil6210_priv *wil)
62 struct pci_dev *pdev = wil->pdev;
64 /* on platforms with buggy ACPI, pdev->msi_enabled may be set to
65 * allow pci_enable_device to work. This indicates INTx was not routed
66 * and only MSI should be used
68 int msi_only = pdev->msi_enabled;
69 bool _use_msi = use_msi;
71 wil_dbg_misc(wil, "%s()\n", __func__);
73 pdev->msi_enabled = 0;
77 wil_dbg_misc(wil, "Setup %s interrupt\n", use_msi ? "MSI" : "INTx");
79 if (use_msi && pci_enable_msi(pdev)) {
80 wil_err(wil, "pci_enable_msi failed, use INTx\n");
84 if (!_use_msi && msi_only) {
85 wil_err(wil, "Interrupt pin not routed, unable to use INTx\n");
90 rc = wil6210_init_irq(wil, pdev->irq, _use_msi);
94 /* need reset here to obtain MAC */
95 mutex_lock(&wil->mutex);
96 rc = wil_reset(wil, false);
97 mutex_unlock(&wil->mutex);
104 wil6210_fini_irq(wil, pdev->irq);
105 /* safe to call if no MSI */
106 pci_disable_msi(pdev);
108 pci_clear_master(pdev);
112 static int wil_if_pcie_disable(struct wil6210_priv *wil)
114 struct pci_dev *pdev = wil->pdev;
116 wil_dbg_misc(wil, "%s()\n", __func__);
118 pci_clear_master(pdev);
119 /* disable and release IRQ */
120 wil6210_fini_irq(wil, pdev->irq);
121 /* safe to call if no MSI */
122 pci_disable_msi(pdev);
123 /* TODO: disable HW */
128 static int wil_pcie_probe(struct pci_dev *pdev, const struct pci_device_id *id)
130 struct wil6210_priv *wil;
131 struct device *dev = &pdev->dev;
135 dev_info(&pdev->dev, WIL_NAME
136 " device found [%04x:%04x] (rev %x)\n",
137 (int)pdev->vendor, (int)pdev->device, (int)pdev->revision);
139 if (pci_resource_len(pdev, 0) != WIL6210_MEM_SIZE) {
140 dev_err(&pdev->dev, "Not " WIL_NAME "? "
141 "BAR0 size is %lu while expecting %lu\n",
142 (ulong)pci_resource_len(pdev, 0), WIL6210_MEM_SIZE);
146 wil = wil_if_alloc(dev);
148 rc = (int)PTR_ERR(wil);
149 dev_err(dev, "wil_if_alloc failed: %d\n", rc);
153 pci_set_drvdata(pdev, wil);
154 /* rollback to if_free */
156 wil->platform_handle =
157 wil_platform_init(&pdev->dev, &wil->platform_ops);
158 if (!wil->platform_handle) {
160 wil_err(wil, "wil_platform_init failed\n");
163 /* rollback to err_plat */
165 rc = pci_enable_device(pdev);
168 "pci_enable_device failed, retry with MSI only\n");
169 /* Work around for platforms that can't allocate IRQ:
170 * retry with MSI only
172 pdev->msi_enabled = 1;
173 rc = pci_enable_device(pdev);
177 "pci_enable_device failed, even with MSI only\n");
180 /* rollback to err_disable_pdev */
182 rc = pci_request_region(pdev, 0, WIL_NAME);
184 wil_err(wil, "pci_request_region failed\n");
185 goto err_disable_pdev;
187 /* rollback to err_release_reg */
189 wil->csr = pci_ioremap_bar(pdev, 0);
191 wil_err(wil, "pci_ioremap_bar failed\n");
193 goto err_release_reg;
195 /* rollback to err_iounmap */
196 wil_info(wil, "CSR at %pR -> 0x%p\n", &pdev->resource[0], wil->csr);
198 wil_set_capabilities(wil);
199 wil6210_clear_irq(wil);
201 /* FW should raise IRQ when ready */
202 rc = wil_if_pcie_enable(wil);
204 wil_err(wil, "Enable device failed\n");
207 /* rollback to bus_disable */
209 rc = wil_if_add(wil);
211 wil_err(wil, "wil_if_add failed: %d\n", rc);
215 wil6210_debugfs_init(wil);
221 wil_if_pcie_disable(wil);
223 pci_iounmap(pdev, wil->csr);
225 pci_release_region(pdev, 0);
227 pci_disable_device(pdev);
229 if (wil->platform_ops.uninit)
230 wil->platform_ops.uninit(wil->platform_handle);
237 static void wil_pcie_remove(struct pci_dev *pdev)
239 struct wil6210_priv *wil = pci_get_drvdata(pdev);
240 void __iomem *csr = wil->csr;
242 wil_dbg_misc(wil, "%s()\n", __func__);
244 wil6210_debugfs_remove(wil);
246 wil_if_pcie_disable(wil);
247 pci_iounmap(pdev, csr);
248 pci_release_region(pdev, 0);
249 pci_disable_device(pdev);
250 if (wil->platform_ops.uninit)
251 wil->platform_ops.uninit(wil->platform_handle);
255 static const struct pci_device_id wil6210_pcie_ids[] = {
256 { PCI_DEVICE(0x1ae9, 0x0310) },
257 { PCI_DEVICE(0x1ae9, 0x0302) }, /* same as above, firmware broken */
258 { /* end: all zeroes */ },
260 MODULE_DEVICE_TABLE(pci, wil6210_pcie_ids);
263 #ifdef CONFIG_PM_SLEEP
265 static int wil6210_suspend(struct device *dev, bool is_runtime)
268 struct pci_dev *pdev = to_pci_dev(dev);
269 struct wil6210_priv *wil = pci_get_drvdata(pdev);
271 wil_dbg_pm(wil, "%s(%s)\n", __func__,
272 is_runtime ? "runtime" : "system");
274 rc = wil_can_suspend(wil, is_runtime);
278 rc = wil_suspend(wil, is_runtime);
282 /* TODO: how do I bring card in low power state? */
284 /* disable bus mastering */
285 pci_clear_master(pdev);
286 /* PCI will call pci_save_state(pdev) and pci_prepare_to_sleep(pdev) */
292 static int wil6210_resume(struct device *dev, bool is_runtime)
295 struct pci_dev *pdev = to_pci_dev(dev);
296 struct wil6210_priv *wil = pci_get_drvdata(pdev);
298 wil_dbg_pm(wil, "%s(%s)\n", __func__,
299 is_runtime ? "runtime" : "system");
302 pci_set_master(pdev);
304 rc = wil_resume(wil, is_runtime);
306 pci_clear_master(pdev);
311 static int wil6210_pm_suspend(struct device *dev)
313 return wil6210_suspend(dev, false);
316 static int wil6210_pm_resume(struct device *dev)
318 return wil6210_resume(dev, false);
320 #endif /* CONFIG_PM_SLEEP */
322 #endif /* CONFIG_PM */
324 static const struct dev_pm_ops wil6210_pm_ops = {
325 SET_SYSTEM_SLEEP_PM_OPS(wil6210_pm_suspend, wil6210_pm_resume)
328 static struct pci_driver wil6210_driver = {
329 .probe = wil_pcie_probe,
330 .remove = wil_pcie_remove,
331 .id_table = wil6210_pcie_ids,
334 .pm = &wil6210_pm_ops,
338 static int __init wil6210_driver_init(void)
342 rc = wil_platform_modinit();
346 rc = pci_register_driver(&wil6210_driver);
348 wil_platform_modexit();
351 module_init(wil6210_driver_init);
353 static void __exit wil6210_driver_exit(void)
355 pci_unregister_driver(&wil6210_driver);
356 wil_platform_modexit();
358 module_exit(wil6210_driver_exit);
360 MODULE_LICENSE("Dual BSD/GPL");
361 MODULE_AUTHOR("Qualcomm Atheros <wil6210@qca.qualcomm.com>");
362 MODULE_DESCRIPTION("Driver for 60g WiFi WIL6210 card");