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>
23 #include <linux/rtnetlink.h>
25 static bool use_msi
= true;
26 module_param(use_msi
, bool, S_IRUGO
);
27 MODULE_PARM_DESC(use_msi
, " Use MSI interrupt, default - true");
30 #ifdef CONFIG_PM_SLEEP
31 static int wil6210_pm_notify(struct notifier_block
*notify_block
,
32 unsigned long mode
, void *unused
);
33 #endif /* CONFIG_PM_SLEEP */
34 #endif /* CONFIG_PM */
37 void wil_set_capabilities(struct wil6210_priv
*wil
)
39 u32 rev_id
= wil_r(wil
, RGF_USER_JTAG_DEV_ID
);
41 bitmap_zero(wil
->hw_capabilities
, hw_capability_last
);
42 bitmap_zero(wil
->fw_capabilities
, WMI_FW_CAPABILITY_MAX
);
45 case JTAG_DEV_ID_SPARROW_B0
:
46 wil
->hw_name
= "Sparrow B0";
47 wil
->hw_version
= HW_VER_SPARROW_B0
;
50 wil_err(wil
, "Unknown board hardware 0x%08x\n", rev_id
);
51 wil
->hw_name
= "Unknown";
52 wil
->hw_version
= HW_VER_UNKNOWN
;
55 wil_info(wil
, "Board hardware is %s\n", wil
->hw_name
);
57 /* extract FW capabilities from file without loading the FW */
58 wil_request_firmware(wil
, WIL_FW_NAME
, false);
61 void wil_disable_irq(struct wil6210_priv
*wil
)
63 disable_irq(wil
->pdev
->irq
);
66 void wil_enable_irq(struct wil6210_priv
*wil
)
68 enable_irq(wil
->pdev
->irq
);
72 static int wil_if_pcie_enable(struct wil6210_priv
*wil
)
74 struct pci_dev
*pdev
= wil
->pdev
;
76 /* on platforms with buggy ACPI, pdev->msi_enabled may be set to
77 * allow pci_enable_device to work. This indicates INTx was not routed
78 * and only MSI should be used
80 int msi_only
= pdev
->msi_enabled
;
81 bool _use_msi
= use_msi
;
83 wil_dbg_misc(wil
, "%s()\n", __func__
);
85 pdev
->msi_enabled
= 0;
89 wil_dbg_misc(wil
, "Setup %s interrupt\n", use_msi
? "MSI" : "INTx");
91 if (use_msi
&& pci_enable_msi(pdev
)) {
92 wil_err(wil
, "pci_enable_msi failed, use INTx\n");
96 if (!_use_msi
&& msi_only
) {
97 wil_err(wil
, "Interrupt pin not routed, unable to use INTx\n");
102 rc
= wil6210_init_irq(wil
, pdev
->irq
, _use_msi
);
106 /* need reset here to obtain MAC */
107 mutex_lock(&wil
->mutex
);
108 rc
= wil_reset(wil
, false);
109 mutex_unlock(&wil
->mutex
);
116 wil6210_fini_irq(wil
, pdev
->irq
);
117 /* safe to call if no MSI */
118 pci_disable_msi(pdev
);
120 pci_clear_master(pdev
);
124 static int wil_if_pcie_disable(struct wil6210_priv
*wil
)
126 struct pci_dev
*pdev
= wil
->pdev
;
128 wil_dbg_misc(wil
, "%s()\n", __func__
);
130 pci_clear_master(pdev
);
131 /* disable and release IRQ */
132 wil6210_fini_irq(wil
, pdev
->irq
);
133 /* safe to call if no MSI */
134 pci_disable_msi(pdev
);
135 /* TODO: disable HW */
140 static int wil_platform_rop_ramdump(void *wil_handle
, void *buf
, uint32_t size
)
142 struct wil6210_priv
*wil
= wil_handle
;
147 return wil_fw_copy_crash_dump(wil
, buf
, size
);
150 static int wil_platform_rop_fw_recovery(void *wil_handle
)
152 struct wil6210_priv
*wil
= wil_handle
;
157 wil_fw_error_recovery(wil
);
162 static int wil_pcie_probe(struct pci_dev
*pdev
, const struct pci_device_id
*id
)
164 struct wil6210_priv
*wil
;
165 struct device
*dev
= &pdev
->dev
;
167 const struct wil_platform_rops rops
= {
168 .ramdump
= wil_platform_rop_ramdump
,
169 .fw_recovery
= wil_platform_rop_fw_recovery
,
173 dev_info(&pdev
->dev
, WIL_NAME
174 " device found [%04x:%04x] (rev %x)\n",
175 (int)pdev
->vendor
, (int)pdev
->device
, (int)pdev
->revision
);
177 if (pci_resource_len(pdev
, 0) != WIL6210_MEM_SIZE
) {
178 dev_err(&pdev
->dev
, "Not " WIL_NAME
"? "
179 "BAR0 size is %lu while expecting %lu\n",
180 (ulong
)pci_resource_len(pdev
, 0), WIL6210_MEM_SIZE
);
184 wil
= wil_if_alloc(dev
);
186 rc
= (int)PTR_ERR(wil
);
187 dev_err(dev
, "wil_if_alloc failed: %d\n", rc
);
191 pci_set_drvdata(pdev
, wil
);
192 /* rollback to if_free */
194 wil
->platform_handle
=
195 wil_platform_init(&pdev
->dev
, &wil
->platform_ops
, &rops
, wil
);
196 if (!wil
->platform_handle
) {
198 wil_err(wil
, "wil_platform_init failed\n");
201 /* rollback to err_plat */
203 rc
= pci_enable_device(pdev
);
206 "pci_enable_device failed, retry with MSI only\n");
207 /* Work around for platforms that can't allocate IRQ:
208 * retry with MSI only
210 pdev
->msi_enabled
= 1;
211 rc
= pci_enable_device(pdev
);
215 "pci_enable_device failed, even with MSI only\n");
218 /* rollback to err_disable_pdev */
220 rc
= pci_request_region(pdev
, 0, WIL_NAME
);
222 wil_err(wil
, "pci_request_region failed\n");
223 goto err_disable_pdev
;
225 /* rollback to err_release_reg */
227 wil
->csr
= pci_ioremap_bar(pdev
, 0);
229 wil_err(wil
, "pci_ioremap_bar failed\n");
231 goto err_release_reg
;
233 /* rollback to err_iounmap */
234 wil_info(wil
, "CSR at %pR -> 0x%p\n", &pdev
->resource
[0], wil
->csr
);
236 wil_set_capabilities(wil
);
237 wil6210_clear_irq(wil
);
239 /* FW should raise IRQ when ready */
240 rc
= wil_if_pcie_enable(wil
);
242 wil_err(wil
, "Enable device failed\n");
245 /* rollback to bus_disable */
247 rc
= wil_if_add(wil
);
249 wil_err(wil
, "wil_if_add failed: %d\n", rc
);
254 #ifdef CONFIG_PM_SLEEP
255 wil
->pm_notify
.notifier_call
= wil6210_pm_notify
;
256 rc
= register_pm_notifier(&wil
->pm_notify
);
258 /* Do not fail the driver initialization, as suspend can
259 * be prevented in a later phase if needed
261 wil_err(wil
, "register_pm_notifier failed: %d\n", rc
);
262 #endif /* CONFIG_PM_SLEEP */
263 #endif /* CONFIG_PM */
265 wil6210_debugfs_init(wil
);
271 wil_if_pcie_disable(wil
);
273 pci_iounmap(pdev
, wil
->csr
);
275 pci_release_region(pdev
, 0);
277 pci_disable_device(pdev
);
279 if (wil
->platform_ops
.uninit
)
280 wil
->platform_ops
.uninit(wil
->platform_handle
);
287 static void wil_pcie_remove(struct pci_dev
*pdev
)
289 struct wil6210_priv
*wil
= pci_get_drvdata(pdev
);
290 void __iomem
*csr
= wil
->csr
;
292 wil_dbg_misc(wil
, "%s()\n", __func__
);
295 #ifdef CONFIG_PM_SLEEP
296 unregister_pm_notifier(&wil
->pm_notify
);
297 #endif /* CONFIG_PM_SLEEP */
298 #endif /* CONFIG_PM */
300 wil6210_debugfs_remove(wil
);
302 wil_p2p_wdev_free(wil
);
305 wil_if_pcie_disable(wil
);
306 pci_iounmap(pdev
, csr
);
307 pci_release_region(pdev
, 0);
308 pci_disable_device(pdev
);
309 if (wil
->platform_ops
.uninit
)
310 wil
->platform_ops
.uninit(wil
->platform_handle
);
314 static const struct pci_device_id wil6210_pcie_ids
[] = {
315 { PCI_DEVICE(0x1ae9, 0x0310) },
316 { PCI_DEVICE(0x1ae9, 0x0302) }, /* same as above, firmware broken */
317 { /* end: all zeroes */ },
319 MODULE_DEVICE_TABLE(pci
, wil6210_pcie_ids
);
322 #ifdef CONFIG_PM_SLEEP
324 static int wil6210_suspend(struct device
*dev
, bool is_runtime
)
327 struct pci_dev
*pdev
= to_pci_dev(dev
);
328 struct wil6210_priv
*wil
= pci_get_drvdata(pdev
);
330 wil_dbg_pm(wil
, "%s(%s)\n", __func__
,
331 is_runtime
? "runtime" : "system");
333 rc
= wil_can_suspend(wil
, is_runtime
);
337 rc
= wil_suspend(wil
, is_runtime
);
341 /* TODO: how do I bring card in low power state? */
343 /* disable bus mastering */
344 pci_clear_master(pdev
);
345 /* PCI will call pci_save_state(pdev) and pci_prepare_to_sleep(pdev) */
351 static int wil6210_resume(struct device
*dev
, bool is_runtime
)
354 struct pci_dev
*pdev
= to_pci_dev(dev
);
355 struct wil6210_priv
*wil
= pci_get_drvdata(pdev
);
357 wil_dbg_pm(wil
, "%s(%s)\n", __func__
,
358 is_runtime
? "runtime" : "system");
361 pci_set_master(pdev
);
363 rc
= wil_resume(wil
, is_runtime
);
365 pci_clear_master(pdev
);
370 static int wil6210_pm_notify(struct notifier_block
*notify_block
,
371 unsigned long mode
, void *unused
)
373 struct wil6210_priv
*wil
= container_of(
374 notify_block
, struct wil6210_priv
, pm_notify
);
376 enum wil_platform_event evt
;
378 wil_dbg_pm(wil
, "%s: mode (%ld)\n", __func__
, mode
);
381 case PM_HIBERNATION_PREPARE
:
382 case PM_SUSPEND_PREPARE
:
383 case PM_RESTORE_PREPARE
:
384 rc
= wil_can_suspend(wil
, false);
387 evt
= WIL_PLATFORM_EVT_PRE_SUSPEND
;
388 if (wil
->platform_ops
.notify
)
389 rc
= wil
->platform_ops
.notify(wil
->platform_handle
,
392 case PM_POST_SUSPEND
:
393 case PM_POST_HIBERNATION
:
394 case PM_POST_RESTORE
:
395 evt
= WIL_PLATFORM_EVT_POST_SUSPEND
;
396 if (wil
->platform_ops
.notify
)
397 rc
= wil
->platform_ops
.notify(wil
->platform_handle
,
401 wil_dbg_pm(wil
, "unhandled notify mode %ld\n", mode
);
405 wil_dbg_pm(wil
, "notification mode %ld: rc (%d)\n", mode
, rc
);
409 static int wil6210_pm_suspend(struct device
*dev
)
411 return wil6210_suspend(dev
, false);
414 static int wil6210_pm_resume(struct device
*dev
)
416 return wil6210_resume(dev
, false);
418 #endif /* CONFIG_PM_SLEEP */
420 #endif /* CONFIG_PM */
422 static const struct dev_pm_ops wil6210_pm_ops
= {
423 SET_SYSTEM_SLEEP_PM_OPS(wil6210_pm_suspend
, wil6210_pm_resume
)
426 static struct pci_driver wil6210_driver
= {
427 .probe
= wil_pcie_probe
,
428 .remove
= wil_pcie_remove
,
429 .id_table
= wil6210_pcie_ids
,
432 .pm
= &wil6210_pm_ops
,
436 static int __init
wil6210_driver_init(void)
440 rc
= wil_platform_modinit();
444 rc
= pci_register_driver(&wil6210_driver
);
446 wil_platform_modexit();
449 module_init(wil6210_driver_init
);
451 static void __exit
wil6210_driver_exit(void)
453 pci_unregister_driver(&wil6210_driver
);
454 wil_platform_modexit();
456 module_exit(wil6210_driver_exit
);
458 MODULE_LICENSE("Dual BSD/GPL");
459 MODULE_AUTHOR("Qualcomm Atheros <wil6210@qca.qualcomm.com>");
460 MODULE_DESCRIPTION("Driver for 60g WiFi WIL6210 card");