2 * Copyright (c) 2012-2016 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>
21 #include <linux/suspend.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 #ifdef CONFIG_PM_SLEEP
30 static int wil6210_pm_notify(struct notifier_block *notify_block,
31 unsigned long mode, void *unused);
32 #endif /* CONFIG_PM_SLEEP */
33 #endif /* CONFIG_PM */
36 void wil_set_capabilities(struct wil6210_priv *wil)
38 u32 rev_id = wil_r(wil, RGF_USER_JTAG_DEV_ID);
40 bitmap_zero(wil->hw_capabilities, hw_capability_last);
43 case JTAG_DEV_ID_SPARROW_B0:
44 wil->hw_name = "Sparrow B0";
45 wil->hw_version = HW_VER_SPARROW_B0;
48 wil_err(wil, "Unknown board hardware 0x%08x\n", rev_id);
49 wil->hw_name = "Unknown";
50 wil->hw_version = HW_VER_UNKNOWN;
53 wil_info(wil, "Board hardware is %s\n", wil->hw_name);
56 void wil_disable_irq(struct wil6210_priv *wil)
58 disable_irq(wil->pdev->irq);
61 void wil_enable_irq(struct wil6210_priv *wil)
63 enable_irq(wil->pdev->irq);
67 static int wil_if_pcie_enable(struct wil6210_priv *wil)
69 struct pci_dev *pdev = wil->pdev;
71 /* on platforms with buggy ACPI, pdev->msi_enabled may be set to
72 * allow pci_enable_device to work. This indicates INTx was not routed
73 * and only MSI should be used
75 int msi_only = pdev->msi_enabled;
76 bool _use_msi = use_msi;
78 wil_dbg_misc(wil, "%s()\n", __func__);
80 pdev->msi_enabled = 0;
84 wil_dbg_misc(wil, "Setup %s interrupt\n", use_msi ? "MSI" : "INTx");
86 if (use_msi && pci_enable_msi(pdev)) {
87 wil_err(wil, "pci_enable_msi failed, use INTx\n");
91 if (!_use_msi && msi_only) {
92 wil_err(wil, "Interrupt pin not routed, unable to use INTx\n");
97 rc = wil6210_init_irq(wil, pdev->irq, _use_msi);
101 /* need reset here to obtain MAC */
102 mutex_lock(&wil->mutex);
103 rc = wil_reset(wil, false);
104 mutex_unlock(&wil->mutex);
111 wil6210_fini_irq(wil, pdev->irq);
112 /* safe to call if no MSI */
113 pci_disable_msi(pdev);
115 pci_clear_master(pdev);
119 static int wil_if_pcie_disable(struct wil6210_priv *wil)
121 struct pci_dev *pdev = wil->pdev;
123 wil_dbg_misc(wil, "%s()\n", __func__);
125 pci_clear_master(pdev);
126 /* disable and release IRQ */
127 wil6210_fini_irq(wil, pdev->irq);
128 /* safe to call if no MSI */
129 pci_disable_msi(pdev);
130 /* TODO: disable HW */
135 static int wil_platform_rop_ramdump(void *wil_handle, void *buf, uint32_t size)
137 struct wil6210_priv *wil = wil_handle;
142 return wil_fw_copy_crash_dump(wil, buf, size);
145 static int wil_platform_rop_fw_recovery(void *wil_handle)
147 struct wil6210_priv *wil = wil_handle;
152 wil_fw_error_recovery(wil);
157 static int wil_pcie_probe(struct pci_dev *pdev, const struct pci_device_id *id)
159 struct wil6210_priv *wil;
160 struct device *dev = &pdev->dev;
162 const struct wil_platform_rops rops = {
163 .ramdump = wil_platform_rop_ramdump,
164 .fw_recovery = wil_platform_rop_fw_recovery,
168 dev_info(&pdev->dev, WIL_NAME
169 " device found [%04x:%04x] (rev %x)\n",
170 (int)pdev->vendor, (int)pdev->device, (int)pdev->revision);
172 if (pci_resource_len(pdev, 0) != WIL6210_MEM_SIZE) {
173 dev_err(&pdev->dev, "Not " WIL_NAME "? "
174 "BAR0 size is %lu while expecting %lu\n",
175 (ulong)pci_resource_len(pdev, 0), WIL6210_MEM_SIZE);
179 wil = wil_if_alloc(dev);
181 rc = (int)PTR_ERR(wil);
182 dev_err(dev, "wil_if_alloc failed: %d\n", rc);
186 pci_set_drvdata(pdev, wil);
187 /* rollback to if_free */
189 wil->platform_handle =
190 wil_platform_init(&pdev->dev, &wil->platform_ops, &rops, wil);
191 if (!wil->platform_handle) {
193 wil_err(wil, "wil_platform_init failed\n");
196 /* rollback to err_plat */
198 rc = pci_enable_device(pdev);
201 "pci_enable_device failed, retry with MSI only\n");
202 /* Work around for platforms that can't allocate IRQ:
203 * retry with MSI only
205 pdev->msi_enabled = 1;
206 rc = pci_enable_device(pdev);
210 "pci_enable_device failed, even with MSI only\n");
213 /* rollback to err_disable_pdev */
215 rc = pci_request_region(pdev, 0, WIL_NAME);
217 wil_err(wil, "pci_request_region failed\n");
218 goto err_disable_pdev;
220 /* rollback to err_release_reg */
222 wil->csr = pci_ioremap_bar(pdev, 0);
224 wil_err(wil, "pci_ioremap_bar failed\n");
226 goto err_release_reg;
228 /* rollback to err_iounmap */
229 wil_info(wil, "CSR at %pR -> 0x%p\n", &pdev->resource[0], wil->csr);
231 wil_set_capabilities(wil);
232 wil6210_clear_irq(wil);
234 /* FW should raise IRQ when ready */
235 rc = wil_if_pcie_enable(wil);
237 wil_err(wil, "Enable device failed\n");
240 /* rollback to bus_disable */
242 rc = wil_if_add(wil);
244 wil_err(wil, "wil_if_add failed: %d\n", rc);
249 #ifdef CONFIG_PM_SLEEP
250 wil->pm_notify.notifier_call = wil6210_pm_notify;
251 rc = register_pm_notifier(&wil->pm_notify);
253 /* Do not fail the driver initialization, as suspend can
254 * be prevented in a later phase if needed
256 wil_err(wil, "register_pm_notifier failed: %d\n", rc);
257 #endif /* CONFIG_PM_SLEEP */
258 #endif /* CONFIG_PM */
260 wil6210_debugfs_init(wil);
266 wil_if_pcie_disable(wil);
268 pci_iounmap(pdev, wil->csr);
270 pci_release_region(pdev, 0);
272 pci_disable_device(pdev);
274 if (wil->platform_ops.uninit)
275 wil->platform_ops.uninit(wil->platform_handle);
282 static void wil_pcie_remove(struct pci_dev *pdev)
284 struct wil6210_priv *wil = pci_get_drvdata(pdev);
285 void __iomem *csr = wil->csr;
287 wil_dbg_misc(wil, "%s()\n", __func__);
290 #ifdef CONFIG_PM_SLEEP
291 unregister_pm_notifier(&wil->pm_notify);
292 #endif /* CONFIG_PM_SLEEP */
293 #endif /* CONFIG_PM */
295 wil6210_debugfs_remove(wil);
297 wil_if_pcie_disable(wil);
298 pci_iounmap(pdev, csr);
299 pci_release_region(pdev, 0);
300 pci_disable_device(pdev);
301 if (wil->platform_ops.uninit)
302 wil->platform_ops.uninit(wil->platform_handle);
303 wil_p2p_wdev_free(wil);
307 static const struct pci_device_id wil6210_pcie_ids[] = {
308 { PCI_DEVICE(0x1ae9, 0x0310) },
309 { PCI_DEVICE(0x1ae9, 0x0302) }, /* same as above, firmware broken */
310 { /* end: all zeroes */ },
312 MODULE_DEVICE_TABLE(pci, wil6210_pcie_ids);
315 #ifdef CONFIG_PM_SLEEP
317 static int wil6210_suspend(struct device *dev, bool is_runtime)
320 struct pci_dev *pdev = to_pci_dev(dev);
321 struct wil6210_priv *wil = pci_get_drvdata(pdev);
323 wil_dbg_pm(wil, "%s(%s)\n", __func__,
324 is_runtime ? "runtime" : "system");
326 rc = wil_can_suspend(wil, is_runtime);
330 rc = wil_suspend(wil, is_runtime);
334 /* TODO: how do I bring card in low power state? */
336 /* disable bus mastering */
337 pci_clear_master(pdev);
338 /* PCI will call pci_save_state(pdev) and pci_prepare_to_sleep(pdev) */
344 static int wil6210_resume(struct device *dev, bool is_runtime)
347 struct pci_dev *pdev = to_pci_dev(dev);
348 struct wil6210_priv *wil = pci_get_drvdata(pdev);
350 wil_dbg_pm(wil, "%s(%s)\n", __func__,
351 is_runtime ? "runtime" : "system");
354 pci_set_master(pdev);
356 rc = wil_resume(wil, is_runtime);
358 pci_clear_master(pdev);
363 static int wil6210_pm_notify(struct notifier_block *notify_block,
364 unsigned long mode, void *unused)
366 struct wil6210_priv *wil = container_of(
367 notify_block, struct wil6210_priv, pm_notify);
369 enum wil_platform_event evt;
371 wil_dbg_pm(wil, "%s: mode (%ld)\n", __func__, mode);
374 case PM_HIBERNATION_PREPARE:
375 case PM_SUSPEND_PREPARE:
376 case PM_RESTORE_PREPARE:
377 rc = wil_can_suspend(wil, false);
380 evt = WIL_PLATFORM_EVT_PRE_SUSPEND;
381 if (wil->platform_ops.notify)
382 rc = wil->platform_ops.notify(wil->platform_handle,
385 case PM_POST_SUSPEND:
386 case PM_POST_HIBERNATION:
387 case PM_POST_RESTORE:
388 evt = WIL_PLATFORM_EVT_POST_SUSPEND;
389 if (wil->platform_ops.notify)
390 rc = wil->platform_ops.notify(wil->platform_handle,
394 wil_dbg_pm(wil, "unhandled notify mode %ld\n", mode);
398 wil_dbg_pm(wil, "notification mode %ld: rc (%d)\n", mode, rc);
402 static int wil6210_pm_suspend(struct device *dev)
404 return wil6210_suspend(dev, false);
407 static int wil6210_pm_resume(struct device *dev)
409 return wil6210_resume(dev, false);
411 #endif /* CONFIG_PM_SLEEP */
413 #endif /* CONFIG_PM */
415 static const struct dev_pm_ops wil6210_pm_ops = {
416 SET_SYSTEM_SLEEP_PM_OPS(wil6210_pm_suspend, wil6210_pm_resume)
419 static struct pci_driver wil6210_driver = {
420 .probe = wil_pcie_probe,
421 .remove = wil_pcie_remove,
422 .id_table = wil6210_pcie_ids,
425 .pm = &wil6210_pm_ops,
429 static int __init wil6210_driver_init(void)
433 rc = wil_platform_modinit();
437 rc = pci_register_driver(&wil6210_driver);
439 wil_platform_modexit();
442 module_init(wil6210_driver_init);
444 static void __exit wil6210_driver_exit(void)
446 pci_unregister_driver(&wil6210_driver);
447 wil_platform_modexit();
449 module_exit(wil6210_driver_exit);
451 MODULE_LICENSE("Dual BSD/GPL");
452 MODULE_AUTHOR("Qualcomm Atheros <wil6210@qca.qualcomm.com>");
453 MODULE_DESCRIPTION("Driver for 60g WiFi WIL6210 card");