2 * Copyright (c) 2012-2017 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>
23 #include <linux/rtnetlink.h>
25 static bool use_msi = true;
26 module_param(use_msi, bool, 0444);
27 MODULE_PARM_DESC(use_msi, " Use MSI interrupt, default - true");
30 module_param(ftm_mode, bool, 0444);
31 MODULE_PARM_DESC(ftm_mode, " Set factory test mode, default - false");
34 #ifdef CONFIG_PM_SLEEP
35 static int wil6210_pm_notify(struct notifier_block *notify_block,
36 unsigned long mode, void *unused);
37 #endif /* CONFIG_PM_SLEEP */
38 #endif /* CONFIG_PM */
41 void wil_set_capabilities(struct wil6210_priv *wil)
43 const char *wil_fw_name;
44 u32 jtag_id = wil_r(wil, RGF_USER_JTAG_DEV_ID);
45 u8 chip_revision = (wil_r(wil, RGF_USER_REVISION_ID) &
46 RGF_USER_REVISION_ID_MASK);
48 bitmap_zero(wil->hw_capabilities, hw_capability_last);
49 bitmap_zero(wil->fw_capabilities, WMI_FW_CAPABILITY_MAX);
50 wil->wil_fw_name = ftm_mode ? WIL_FW_NAME_FTM_DEFAULT :
52 wil->chip_revision = chip_revision;
55 case JTAG_DEV_ID_SPARROW:
56 switch (chip_revision) {
57 case REVISION_ID_SPARROW_D0:
58 wil->hw_name = "Sparrow D0";
59 wil->hw_version = HW_VER_SPARROW_D0;
60 wil_fw_name = ftm_mode ? WIL_FW_NAME_FTM_SPARROW_PLUS :
61 WIL_FW_NAME_SPARROW_PLUS;
63 if (wil_fw_verify_file_exists(wil, wil_fw_name))
64 wil->wil_fw_name = wil_fw_name;
66 case REVISION_ID_SPARROW_B0:
67 wil->hw_name = "Sparrow B0";
68 wil->hw_version = HW_VER_SPARROW_B0;
71 wil->hw_name = "Unknown";
72 wil->hw_version = HW_VER_UNKNOWN;
77 wil_err(wil, "Unknown board hardware, chip_id 0x%08x, chip_revision 0x%08x\n",
78 jtag_id, chip_revision);
79 wil->hw_name = "Unknown";
80 wil->hw_version = HW_VER_UNKNOWN;
83 wil_info(wil, "Board hardware is %s\n", wil->hw_name);
85 /* extract FW capabilities from file without loading the FW */
86 wil_request_firmware(wil, wil->wil_fw_name, false);
88 if (test_bit(WMI_FW_CAPABILITY_RSSI_REPORTING, wil->fw_capabilities))
89 wil_to_wiphy(wil)->signal_type = CFG80211_SIGNAL_TYPE_MBM;
92 void wil_disable_irq(struct wil6210_priv *wil)
94 disable_irq(wil->pdev->irq);
97 void wil_enable_irq(struct wil6210_priv *wil)
99 enable_irq(wil->pdev->irq);
103 static int wil_if_pcie_enable(struct wil6210_priv *wil)
105 struct pci_dev *pdev = wil->pdev;
107 /* on platforms with buggy ACPI, pdev->msi_enabled may be set to
108 * allow pci_enable_device to work. This indicates INTx was not routed
109 * and only MSI should be used
111 int msi_only = pdev->msi_enabled;
112 bool _use_msi = use_msi;
113 bool wmi_only = test_bit(WMI_FW_CAPABILITY_WMI_ONLY,
114 wil->fw_capabilities);
116 wil_dbg_misc(wil, "if_pcie_enable, wmi_only %d\n", wmi_only);
118 pci_set_master(pdev);
120 wil_dbg_misc(wil, "Setup %s interrupt\n", use_msi ? "MSI" : "INTx");
122 if (use_msi && pci_enable_msi(pdev)) {
123 wil_err(wil, "pci_enable_msi failed, use INTx\n");
127 if (!_use_msi && msi_only) {
128 wil_err(wil, "Interrupt pin not routed, unable to use INTx\n");
133 rc = wil6210_init_irq(wil, pdev->irq, _use_msi);
137 /* need reset here to obtain MAC or in case of WMI-only FW, full reset
138 * and fw loading takes place
140 mutex_lock(&wil->mutex);
141 rc = wil_reset(wil, wmi_only);
142 mutex_unlock(&wil->mutex);
149 wil6210_fini_irq(wil, pdev->irq);
150 /* safe to call if no MSI */
151 pci_disable_msi(pdev);
153 pci_clear_master(pdev);
157 static int wil_if_pcie_disable(struct wil6210_priv *wil)
159 struct pci_dev *pdev = wil->pdev;
161 wil_dbg_misc(wil, "if_pcie_disable\n");
163 pci_clear_master(pdev);
164 /* disable and release IRQ */
165 wil6210_fini_irq(wil, pdev->irq);
166 /* safe to call if no MSI */
167 pci_disable_msi(pdev);
168 /* TODO: disable HW */
173 static int wil_platform_rop_ramdump(void *wil_handle, void *buf, uint32_t size)
175 struct wil6210_priv *wil = wil_handle;
180 return wil_fw_copy_crash_dump(wil, buf, size);
183 static int wil_platform_rop_fw_recovery(void *wil_handle)
185 struct wil6210_priv *wil = wil_handle;
190 wil_fw_error_recovery(wil);
195 static void wil_platform_ops_uninit(struct wil6210_priv *wil)
197 if (wil->platform_ops.uninit)
198 wil->platform_ops.uninit(wil->platform_handle);
199 memset(&wil->platform_ops, 0, sizeof(wil->platform_ops));
202 static int wil_pcie_probe(struct pci_dev *pdev, const struct pci_device_id *id)
204 struct wil6210_priv *wil;
205 struct device *dev = &pdev->dev;
207 const struct wil_platform_rops rops = {
208 .ramdump = wil_platform_rop_ramdump,
209 .fw_recovery = wil_platform_rop_fw_recovery,
211 u32 bar_size = pci_resource_len(pdev, 0);
214 dev_info(&pdev->dev, WIL_NAME
215 " device found [%04x:%04x] (rev %x) bar size 0x%x\n",
216 (int)pdev->vendor, (int)pdev->device, (int)pdev->revision,
219 if ((bar_size < WIL6210_MIN_MEM_SIZE) ||
220 (bar_size > WIL6210_MAX_MEM_SIZE)) {
221 dev_err(&pdev->dev, "Unexpected BAR0 size 0x%x\n",
226 wil = wil_if_alloc(dev);
228 rc = (int)PTR_ERR(wil);
229 dev_err(dev, "wil_if_alloc failed: %d\n", rc);
234 pci_set_drvdata(pdev, wil);
235 wil->bar_size = bar_size;
236 /* rollback to if_free */
238 wil->platform_handle =
239 wil_platform_init(&pdev->dev, &wil->platform_ops, &rops, wil);
240 if (!wil->platform_handle) {
242 wil_err(wil, "wil_platform_init failed\n");
245 /* rollback to err_plat */
247 /* device supports 48bit addresses */
248 rc = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(48));
250 dev_err(dev, "dma_set_mask_and_coherent(48) failed: %d\n", rc);
251 rc = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
254 "dma_set_mask_and_coherent(32) failed: %d\n",
259 wil->use_extended_dma_addr = 1;
262 rc = pci_enable_device(pdev);
263 if (rc && pdev->msi_enabled == 0) {
265 "pci_enable_device failed, retry with MSI only\n");
266 /* Work around for platforms that can't allocate IRQ:
267 * retry with MSI only
269 pdev->msi_enabled = 1;
270 rc = pci_enable_device(pdev);
274 "pci_enable_device failed, even with MSI only\n");
277 /* rollback to err_disable_pdev */
278 pci_set_power_state(pdev, PCI_D0);
280 rc = pci_request_region(pdev, 0, WIL_NAME);
282 wil_err(wil, "pci_request_region failed\n");
283 goto err_disable_pdev;
285 /* rollback to err_release_reg */
287 wil->csr = pci_ioremap_bar(pdev, 0);
289 wil_err(wil, "pci_ioremap_bar failed\n");
291 goto err_release_reg;
293 /* rollback to err_iounmap */
294 wil_info(wil, "CSR at %pR -> 0x%p\n", &pdev->resource[0], wil->csr);
296 wil_set_capabilities(wil);
297 wil6210_clear_irq(wil);
299 wil->keep_radio_on_during_sleep =
300 wil->platform_ops.keep_radio_on_during_sleep &&
301 wil->platform_ops.keep_radio_on_during_sleep(
302 wil->platform_handle) &&
303 test_bit(WMI_FW_CAPABILITY_D3_SUSPEND, wil->fw_capabilities);
305 wil_info(wil, "keep_radio_on_during_sleep (%d)\n",
306 wil->keep_radio_on_during_sleep);
308 /* FW should raise IRQ when ready */
309 rc = wil_if_pcie_enable(wil);
311 wil_err(wil, "Enable device failed\n");
314 /* rollback to bus_disable */
316 rc = wil_if_add(wil);
318 wil_err(wil, "wil_if_add failed: %d\n", rc);
323 #ifdef CONFIG_PM_SLEEP
324 wil->pm_notify.notifier_call = wil6210_pm_notify;
325 rc = register_pm_notifier(&wil->pm_notify);
327 /* Do not fail the driver initialization, as suspend can
328 * be prevented in a later phase if needed
330 wil_err(wil, "register_pm_notifier failed: %d\n", rc);
331 #endif /* CONFIG_PM_SLEEP */
332 #endif /* CONFIG_PM */
334 wil6210_debugfs_init(wil);
340 wil_if_pcie_disable(wil);
342 pci_iounmap(pdev, wil->csr);
344 pci_release_region(pdev, 0);
346 pci_disable_device(pdev);
348 wil_platform_ops_uninit(wil);
355 static void wil_pcie_remove(struct pci_dev *pdev)
357 struct wil6210_priv *wil = pci_get_drvdata(pdev);
358 void __iomem *csr = wil->csr;
360 wil_dbg_misc(wil, "pcie_remove\n");
363 #ifdef CONFIG_PM_SLEEP
364 unregister_pm_notifier(&wil->pm_notify);
365 #endif /* CONFIG_PM_SLEEP */
366 #endif /* CONFIG_PM */
368 wil6210_debugfs_remove(wil);
370 wil_p2p_wdev_free(wil);
373 wil_if_pcie_disable(wil);
374 pci_iounmap(pdev, csr);
375 pci_release_region(pdev, 0);
376 pci_disable_device(pdev);
377 wil_platform_ops_uninit(wil);
381 static const struct pci_device_id wil6210_pcie_ids[] = {
382 { PCI_DEVICE(0x1ae9, 0x0310) },
383 { PCI_DEVICE(0x1ae9, 0x0302) }, /* same as above, firmware broken */
384 { /* end: all zeroes */ },
386 MODULE_DEVICE_TABLE(pci, wil6210_pcie_ids);
389 #ifdef CONFIG_PM_SLEEP
391 static int wil6210_suspend(struct device *dev, bool is_runtime)
394 struct pci_dev *pdev = to_pci_dev(dev);
395 struct wil6210_priv *wil = pci_get_drvdata(pdev);
396 struct net_device *ndev = wil_to_ndev(wil);
397 bool keep_radio_on = ndev->flags & IFF_UP &&
398 wil->keep_radio_on_during_sleep;
400 wil_dbg_pm(wil, "suspend: %s\n", is_runtime ? "runtime" : "system");
402 rc = wil_can_suspend(wil, is_runtime);
406 rc = wil_suspend(wil, is_runtime, keep_radio_on);
408 wil->suspend_stats.successful_suspends++;
410 /* In case radio stays on, platform device will control
414 /* disable bus mastering */
415 pci_clear_master(pdev);
421 static int wil6210_resume(struct device *dev, bool is_runtime)
424 struct pci_dev *pdev = to_pci_dev(dev);
425 struct wil6210_priv *wil = pci_get_drvdata(pdev);
426 struct net_device *ndev = wil_to_ndev(wil);
427 bool keep_radio_on = ndev->flags & IFF_UP &&
428 wil->keep_radio_on_during_sleep;
430 wil_dbg_pm(wil, "resume: %s\n", is_runtime ? "runtime" : "system");
432 /* In case radio stays on, platform device will control
437 pci_set_master(pdev);
438 rc = wil_resume(wil, is_runtime, keep_radio_on);
440 wil_err(wil, "device failed to resume (%d)\n", rc);
441 wil->suspend_stats.failed_resumes++;
443 pci_clear_master(pdev);
445 wil->suspend_stats.successful_resumes++;
451 static int wil6210_pm_notify(struct notifier_block *notify_block,
452 unsigned long mode, void *unused)
454 struct wil6210_priv *wil = container_of(
455 notify_block, struct wil6210_priv, pm_notify);
457 enum wil_platform_event evt;
459 wil_dbg_pm(wil, "pm_notify: mode (%ld)\n", mode);
462 case PM_HIBERNATION_PREPARE:
463 case PM_SUSPEND_PREPARE:
464 case PM_RESTORE_PREPARE:
465 rc = wil_can_suspend(wil, false);
468 evt = WIL_PLATFORM_EVT_PRE_SUSPEND;
469 if (wil->platform_ops.notify)
470 rc = wil->platform_ops.notify(wil->platform_handle,
473 case PM_POST_SUSPEND:
474 case PM_POST_HIBERNATION:
475 case PM_POST_RESTORE:
476 evt = WIL_PLATFORM_EVT_POST_SUSPEND;
477 if (wil->platform_ops.notify)
478 rc = wil->platform_ops.notify(wil->platform_handle,
482 wil_dbg_pm(wil, "unhandled notify mode %ld\n", mode);
486 wil_dbg_pm(wil, "notification mode %ld: rc (%d)\n", mode, rc);
490 static int wil6210_pm_suspend(struct device *dev)
492 return wil6210_suspend(dev, false);
495 static int wil6210_pm_resume(struct device *dev)
497 return wil6210_resume(dev, false);
499 #endif /* CONFIG_PM_SLEEP */
501 #endif /* CONFIG_PM */
503 static const struct dev_pm_ops wil6210_pm_ops = {
504 SET_SYSTEM_SLEEP_PM_OPS(wil6210_pm_suspend, wil6210_pm_resume)
507 static struct pci_driver wil6210_driver = {
508 .probe = wil_pcie_probe,
509 .remove = wil_pcie_remove,
510 .id_table = wil6210_pcie_ids,
513 .pm = &wil6210_pm_ops,
517 static int __init wil6210_driver_init(void)
521 rc = wil_platform_modinit();
525 rc = pci_register_driver(&wil6210_driver);
527 wil_platform_modexit();
530 module_init(wil6210_driver_init);
532 static void __exit wil6210_driver_exit(void)
534 pci_unregister_driver(&wil6210_driver);
535 wil_platform_modexit();
537 module_exit(wil6210_driver_exit);
539 MODULE_LICENSE("Dual BSD/GPL");
540 MODULE_AUTHOR("Qualcomm Atheros <wil6210@qca.qualcomm.com>");
541 MODULE_DESCRIPTION("Driver for 60g WiFi WIL6210 card");