1 /* 2 * Copyright (c) 2012-2015 Qualcomm Atheros, Inc. 3 * 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. 7 * 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. 15 */ 16 17 #include <linux/module.h> 18 #include <linux/pci.h> 19 #include <linux/moduleparam.h> 20 #include <linux/interrupt.h> 21 22 #include "wil6210.h" 23 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"); 27 28 static 29 void wil_set_capabilities(struct wil6210_priv *wil) 30 { 31 u32 rev_id = wil_r(wil, RGF_USER_JTAG_DEV_ID); 32 33 bitmap_zero(wil->hw_capabilities, hw_capability_last); 34 35 switch (rev_id) { 36 case JTAG_DEV_ID_SPARROW_B0: 37 wil->hw_name = "Sparrow B0"; 38 wil->hw_version = HW_VER_SPARROW_B0; 39 break; 40 default: 41 wil_err(wil, "Unknown board hardware 0x%08x\n", rev_id); 42 wil->hw_name = "Unknown"; 43 wil->hw_version = HW_VER_UNKNOWN; 44 } 45 46 wil_info(wil, "Board hardware is %s\n", wil->hw_name); 47 } 48 49 void wil_disable_irq(struct wil6210_priv *wil) 50 { 51 disable_irq(wil->pdev->irq); 52 } 53 54 void wil_enable_irq(struct wil6210_priv *wil) 55 { 56 enable_irq(wil->pdev->irq); 57 } 58 59 /* Bus ops */ 60 static int wil_if_pcie_enable(struct wil6210_priv *wil) 61 { 62 struct pci_dev *pdev = wil->pdev; 63 int rc; 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 67 */ 68 int msi_only = pdev->msi_enabled; 69 bool _use_msi = use_msi; 70 71 wil_dbg_misc(wil, "%s()\n", __func__); 72 73 pdev->msi_enabled = 0; 74 75 pci_set_master(pdev); 76 77 wil_dbg_misc(wil, "Setup %s interrupt\n", use_msi ? "MSI" : "INTx"); 78 79 if (use_msi && pci_enable_msi(pdev)) { 80 wil_err(wil, "pci_enable_msi failed, use INTx\n"); 81 _use_msi = false; 82 } 83 84 if (!_use_msi && msi_only) { 85 wil_err(wil, "Interrupt pin not routed, unable to use INTx\n"); 86 rc = -ENODEV; 87 goto stop_master; 88 } 89 90 rc = wil6210_init_irq(wil, pdev->irq, _use_msi); 91 if (rc) 92 goto stop_master; 93 94 /* need reset here to obtain MAC */ 95 mutex_lock(&wil->mutex); 96 rc = wil_reset(wil, false); 97 mutex_unlock(&wil->mutex); 98 if (rc) 99 goto release_irq; 100 101 return 0; 102 103 release_irq: 104 wil6210_fini_irq(wil, pdev->irq); 105 /* safe to call if no MSI */ 106 pci_disable_msi(pdev); 107 stop_master: 108 pci_clear_master(pdev); 109 return rc; 110 } 111 112 static int wil_if_pcie_disable(struct wil6210_priv *wil) 113 { 114 struct pci_dev *pdev = wil->pdev; 115 116 wil_dbg_misc(wil, "%s()\n", __func__); 117 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 */ 124 125 return 0; 126 } 127 128 static int wil_platform_rop_ramdump(void *wil_handle, void *buf, uint32_t size) 129 { 130 struct wil6210_priv *wil = wil_handle; 131 132 if (!wil) 133 return -EINVAL; 134 135 return wil_fw_copy_crash_dump(wil, buf, size); 136 } 137 138 static int wil_platform_rop_fw_recovery(void *wil_handle) 139 { 140 struct wil6210_priv *wil = wil_handle; 141 142 if (!wil) 143 return -EINVAL; 144 145 wil_fw_error_recovery(wil); 146 147 return 0; 148 } 149 150 static int wil_pcie_probe(struct pci_dev *pdev, const struct pci_device_id *id) 151 { 152 struct wil6210_priv *wil; 153 struct device *dev = &pdev->dev; 154 int rc; 155 const struct wil_platform_rops rops = { 156 .ramdump = wil_platform_rop_ramdump, 157 .fw_recovery = wil_platform_rop_fw_recovery, 158 }; 159 160 /* check HW */ 161 dev_info(&pdev->dev, WIL_NAME 162 " device found [%04x:%04x] (rev %x)\n", 163 (int)pdev->vendor, (int)pdev->device, (int)pdev->revision); 164 165 if (pci_resource_len(pdev, 0) != WIL6210_MEM_SIZE) { 166 dev_err(&pdev->dev, "Not " WIL_NAME "? " 167 "BAR0 size is %lu while expecting %lu\n", 168 (ulong)pci_resource_len(pdev, 0), WIL6210_MEM_SIZE); 169 return -ENODEV; 170 } 171 172 wil = wil_if_alloc(dev); 173 if (IS_ERR(wil)) { 174 rc = (int)PTR_ERR(wil); 175 dev_err(dev, "wil_if_alloc failed: %d\n", rc); 176 return rc; 177 } 178 wil->pdev = pdev; 179 pci_set_drvdata(pdev, wil); 180 /* rollback to if_free */ 181 182 wil->platform_handle = 183 wil_platform_init(&pdev->dev, &wil->platform_ops, &rops, wil); 184 if (!wil->platform_handle) { 185 rc = -ENODEV; 186 wil_err(wil, "wil_platform_init failed\n"); 187 goto if_free; 188 } 189 /* rollback to err_plat */ 190 191 rc = pci_enable_device(pdev); 192 if (rc) { 193 wil_err(wil, 194 "pci_enable_device failed, retry with MSI only\n"); 195 /* Work around for platforms that can't allocate IRQ: 196 * retry with MSI only 197 */ 198 pdev->msi_enabled = 1; 199 rc = pci_enable_device(pdev); 200 } 201 if (rc) { 202 wil_err(wil, 203 "pci_enable_device failed, even with MSI only\n"); 204 goto err_plat; 205 } 206 /* rollback to err_disable_pdev */ 207 208 rc = pci_request_region(pdev, 0, WIL_NAME); 209 if (rc) { 210 wil_err(wil, "pci_request_region failed\n"); 211 goto err_disable_pdev; 212 } 213 /* rollback to err_release_reg */ 214 215 wil->csr = pci_ioremap_bar(pdev, 0); 216 if (!wil->csr) { 217 wil_err(wil, "pci_ioremap_bar failed\n"); 218 rc = -ENODEV; 219 goto err_release_reg; 220 } 221 /* rollback to err_iounmap */ 222 wil_info(wil, "CSR at %pR -> 0x%p\n", &pdev->resource[0], wil->csr); 223 224 wil_set_capabilities(wil); 225 wil6210_clear_irq(wil); 226 227 /* FW should raise IRQ when ready */ 228 rc = wil_if_pcie_enable(wil); 229 if (rc) { 230 wil_err(wil, "Enable device failed\n"); 231 goto err_iounmap; 232 } 233 /* rollback to bus_disable */ 234 235 rc = wil_if_add(wil); 236 if (rc) { 237 wil_err(wil, "wil_if_add failed: %d\n", rc); 238 goto bus_disable; 239 } 240 241 wil6210_debugfs_init(wil); 242 243 244 return 0; 245 246 bus_disable: 247 wil_if_pcie_disable(wil); 248 err_iounmap: 249 pci_iounmap(pdev, wil->csr); 250 err_release_reg: 251 pci_release_region(pdev, 0); 252 err_disable_pdev: 253 pci_disable_device(pdev); 254 err_plat: 255 if (wil->platform_ops.uninit) 256 wil->platform_ops.uninit(wil->platform_handle); 257 if_free: 258 wil_if_free(wil); 259 260 return rc; 261 } 262 263 static void wil_pcie_remove(struct pci_dev *pdev) 264 { 265 struct wil6210_priv *wil = pci_get_drvdata(pdev); 266 void __iomem *csr = wil->csr; 267 268 wil_dbg_misc(wil, "%s()\n", __func__); 269 270 wil6210_debugfs_remove(wil); 271 wil_if_remove(wil); 272 wil_if_pcie_disable(wil); 273 pci_iounmap(pdev, csr); 274 pci_release_region(pdev, 0); 275 pci_disable_device(pdev); 276 if (wil->platform_ops.uninit) 277 wil->platform_ops.uninit(wil->platform_handle); 278 wil_p2p_wdev_free(wil); 279 wil_if_free(wil); 280 } 281 282 static const struct pci_device_id wil6210_pcie_ids[] = { 283 { PCI_DEVICE(0x1ae9, 0x0310) }, 284 { PCI_DEVICE(0x1ae9, 0x0302) }, /* same as above, firmware broken */ 285 { /* end: all zeroes */ }, 286 }; 287 MODULE_DEVICE_TABLE(pci, wil6210_pcie_ids); 288 289 #ifdef CONFIG_PM 290 #ifdef CONFIG_PM_SLEEP 291 292 static int wil6210_suspend(struct device *dev, bool is_runtime) 293 { 294 int rc = 0; 295 struct pci_dev *pdev = to_pci_dev(dev); 296 struct wil6210_priv *wil = pci_get_drvdata(pdev); 297 298 wil_dbg_pm(wil, "%s(%s)\n", __func__, 299 is_runtime ? "runtime" : "system"); 300 301 rc = wil_can_suspend(wil, is_runtime); 302 if (rc) 303 goto out; 304 305 rc = wil_suspend(wil, is_runtime); 306 if (rc) 307 goto out; 308 309 /* TODO: how do I bring card in low power state? */ 310 311 /* disable bus mastering */ 312 pci_clear_master(pdev); 313 /* PCI will call pci_save_state(pdev) and pci_prepare_to_sleep(pdev) */ 314 315 out: 316 return rc; 317 } 318 319 static int wil6210_resume(struct device *dev, bool is_runtime) 320 { 321 int rc = 0; 322 struct pci_dev *pdev = to_pci_dev(dev); 323 struct wil6210_priv *wil = pci_get_drvdata(pdev); 324 325 wil_dbg_pm(wil, "%s(%s)\n", __func__, 326 is_runtime ? "runtime" : "system"); 327 328 /* allow master */ 329 pci_set_master(pdev); 330 331 rc = wil_resume(wil, is_runtime); 332 if (rc) 333 pci_clear_master(pdev); 334 335 return rc; 336 } 337 338 static int wil6210_pm_suspend(struct device *dev) 339 { 340 return wil6210_suspend(dev, false); 341 } 342 343 static int wil6210_pm_resume(struct device *dev) 344 { 345 return wil6210_resume(dev, false); 346 } 347 #endif /* CONFIG_PM_SLEEP */ 348 349 #endif /* CONFIG_PM */ 350 351 static const struct dev_pm_ops wil6210_pm_ops = { 352 SET_SYSTEM_SLEEP_PM_OPS(wil6210_pm_suspend, wil6210_pm_resume) 353 }; 354 355 static struct pci_driver wil6210_driver = { 356 .probe = wil_pcie_probe, 357 .remove = wil_pcie_remove, 358 .id_table = wil6210_pcie_ids, 359 .name = WIL_NAME, 360 .driver = { 361 .pm = &wil6210_pm_ops, 362 }, 363 }; 364 365 static int __init wil6210_driver_init(void) 366 { 367 int rc; 368 369 rc = wil_platform_modinit(); 370 if (rc) 371 return rc; 372 373 rc = pci_register_driver(&wil6210_driver); 374 if (rc) 375 wil_platform_modexit(); 376 return rc; 377 } 378 module_init(wil6210_driver_init); 379 380 static void __exit wil6210_driver_exit(void) 381 { 382 pci_unregister_driver(&wil6210_driver); 383 wil_platform_modexit(); 384 } 385 module_exit(wil6210_driver_exit); 386 387 MODULE_LICENSE("Dual BSD/GPL"); 388 MODULE_AUTHOR("Qualcomm Atheros <wil6210@qca.qualcomm.com>"); 389 MODULE_DESCRIPTION("Driver for 60g WiFi WIL6210 card"); 390