pcie_bus.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. * Copyright (c) 2012-2014 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. #include <linux/module.h>
  17. #include <linux/pci.h>
  18. #include <linux/moduleparam.h>
  19. #include <linux/interrupt.h>
  20. #include "wil6210.h"
  21. static bool use_msi = true;
  22. module_param(use_msi, bool, S_IRUGO);
  23. MODULE_PARM_DESC(use_msi, " Use MSI interrupt, default - true");
  24. static
  25. void wil_set_capabilities(struct wil6210_priv *wil)
  26. {
  27. u32 rev_id = wil_r(wil, RGF_USER_JTAG_DEV_ID);
  28. bitmap_zero(wil->hw_capabilities, hw_capability_last);
  29. switch (rev_id) {
  30. case JTAG_DEV_ID_SPARROW_B0:
  31. wil->hw_name = "Sparrow B0";
  32. wil->hw_version = HW_VER_SPARROW_B0;
  33. break;
  34. default:
  35. wil_err(wil, "Unknown board hardware 0x%08x\n", rev_id);
  36. wil->hw_name = "Unknown";
  37. wil->hw_version = HW_VER_UNKNOWN;
  38. }
  39. wil_info(wil, "Board hardware is %s\n", wil->hw_name);
  40. }
  41. void wil_disable_irq(struct wil6210_priv *wil)
  42. {
  43. disable_irq(wil->pdev->irq);
  44. }
  45. void wil_enable_irq(struct wil6210_priv *wil)
  46. {
  47. enable_irq(wil->pdev->irq);
  48. }
  49. /* Bus ops */
  50. static int wil_if_pcie_enable(struct wil6210_priv *wil)
  51. {
  52. struct pci_dev *pdev = wil->pdev;
  53. int rc;
  54. /* on platforms with buggy ACPI, pdev->msi_enabled may be set to
  55. * allow pci_enable_device to work. This indicates INTx was not routed
  56. * and only MSI should be used
  57. */
  58. int msi_only = pdev->msi_enabled;
  59. bool _use_msi = use_msi;
  60. wil_dbg_misc(wil, "%s()\n", __func__);
  61. pdev->msi_enabled = 0;
  62. pci_set_master(pdev);
  63. wil_dbg_misc(wil, "Setup %s interrupt\n", use_msi ? "MSI" : "INTx");
  64. if (use_msi && pci_enable_msi(pdev)) {
  65. wil_err(wil, "pci_enable_msi failed, use INTx\n");
  66. _use_msi = false;
  67. }
  68. if (!_use_msi && msi_only) {
  69. wil_err(wil, "Interrupt pin not routed, unable to use INTx\n");
  70. rc = -ENODEV;
  71. goto stop_master;
  72. }
  73. rc = wil6210_init_irq(wil, pdev->irq, _use_msi);
  74. if (rc)
  75. goto stop_master;
  76. /* need reset here to obtain MAC */
  77. mutex_lock(&wil->mutex);
  78. rc = wil_reset(wil, false);
  79. mutex_unlock(&wil->mutex);
  80. if (rc)
  81. goto release_irq;
  82. return 0;
  83. release_irq:
  84. wil6210_fini_irq(wil, pdev->irq);
  85. /* safe to call if no MSI */
  86. pci_disable_msi(pdev);
  87. stop_master:
  88. pci_clear_master(pdev);
  89. return rc;
  90. }
  91. static int wil_if_pcie_disable(struct wil6210_priv *wil)
  92. {
  93. struct pci_dev *pdev = wil->pdev;
  94. wil_dbg_misc(wil, "%s()\n", __func__);
  95. pci_clear_master(pdev);
  96. /* disable and release IRQ */
  97. wil6210_fini_irq(wil, pdev->irq);
  98. /* safe to call if no MSI */
  99. pci_disable_msi(pdev);
  100. /* TODO: disable HW */
  101. return 0;
  102. }
  103. static int wil_pcie_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  104. {
  105. struct wil6210_priv *wil;
  106. struct device *dev = &pdev->dev;
  107. int rc;
  108. /* check HW */
  109. dev_info(&pdev->dev, WIL_NAME
  110. " device found [%04x:%04x] (rev %x)\n",
  111. (int)pdev->vendor, (int)pdev->device, (int)pdev->revision);
  112. if (pci_resource_len(pdev, 0) != WIL6210_MEM_SIZE) {
  113. dev_err(&pdev->dev, "Not " WIL_NAME "? "
  114. "BAR0 size is %lu while expecting %lu\n",
  115. (ulong)pci_resource_len(pdev, 0), WIL6210_MEM_SIZE);
  116. return -ENODEV;
  117. }
  118. wil = wil_if_alloc(dev);
  119. if (IS_ERR(wil)) {
  120. rc = (int)PTR_ERR(wil);
  121. dev_err(dev, "wil_if_alloc failed: %d\n", rc);
  122. return rc;
  123. }
  124. wil->pdev = pdev;
  125. pci_set_drvdata(pdev, wil);
  126. /* rollback to if_free */
  127. wil->platform_handle =
  128. wil_platform_init(&pdev->dev, &wil->platform_ops);
  129. if (!wil->platform_handle) {
  130. rc = -ENODEV;
  131. wil_err(wil, "wil_platform_init failed\n");
  132. goto if_free;
  133. }
  134. /* rollback to err_plat */
  135. rc = pci_enable_device(pdev);
  136. if (rc) {
  137. wil_err(wil,
  138. "pci_enable_device failed, retry with MSI only\n");
  139. /* Work around for platforms that can't allocate IRQ:
  140. * retry with MSI only
  141. */
  142. pdev->msi_enabled = 1;
  143. rc = pci_enable_device(pdev);
  144. }
  145. if (rc) {
  146. wil_err(wil,
  147. "pci_enable_device failed, even with MSI only\n");
  148. goto err_plat;
  149. }
  150. /* rollback to err_disable_pdev */
  151. rc = pci_request_region(pdev, 0, WIL_NAME);
  152. if (rc) {
  153. wil_err(wil, "pci_request_region failed\n");
  154. goto err_disable_pdev;
  155. }
  156. /* rollback to err_release_reg */
  157. wil->csr = pci_ioremap_bar(pdev, 0);
  158. if (!wil->csr) {
  159. wil_err(wil, "pci_ioremap_bar failed\n");
  160. rc = -ENODEV;
  161. goto err_release_reg;
  162. }
  163. /* rollback to err_iounmap */
  164. wil_info(wil, "CSR at %pR -> 0x%p\n", &pdev->resource[0], wil->csr);
  165. wil_set_capabilities(wil);
  166. wil6210_clear_irq(wil);
  167. /* FW should raise IRQ when ready */
  168. rc = wil_if_pcie_enable(wil);
  169. if (rc) {
  170. wil_err(wil, "Enable device failed\n");
  171. goto err_iounmap;
  172. }
  173. /* rollback to bus_disable */
  174. rc = wil_if_add(wil);
  175. if (rc) {
  176. wil_err(wil, "wil_if_add failed: %d\n", rc);
  177. goto bus_disable;
  178. }
  179. wil6210_debugfs_init(wil);
  180. return 0;
  181. bus_disable:
  182. wil_if_pcie_disable(wil);
  183. err_iounmap:
  184. pci_iounmap(pdev, wil->csr);
  185. err_release_reg:
  186. pci_release_region(pdev, 0);
  187. err_disable_pdev:
  188. pci_disable_device(pdev);
  189. err_plat:
  190. if (wil->platform_ops.uninit)
  191. wil->platform_ops.uninit(wil->platform_handle);
  192. if_free:
  193. wil_if_free(wil);
  194. return rc;
  195. }
  196. static void wil_pcie_remove(struct pci_dev *pdev)
  197. {
  198. struct wil6210_priv *wil = pci_get_drvdata(pdev);
  199. void __iomem *csr = wil->csr;
  200. wil_dbg_misc(wil, "%s()\n", __func__);
  201. wil6210_debugfs_remove(wil);
  202. wil_if_remove(wil);
  203. wil_if_pcie_disable(wil);
  204. pci_iounmap(pdev, csr);
  205. pci_release_region(pdev, 0);
  206. pci_disable_device(pdev);
  207. if (wil->platform_ops.uninit)
  208. wil->platform_ops.uninit(wil->platform_handle);
  209. wil_if_free(wil);
  210. }
  211. static const struct pci_device_id wil6210_pcie_ids[] = {
  212. { PCI_DEVICE(0x1ae9, 0x0310) },
  213. { PCI_DEVICE(0x1ae9, 0x0302) }, /* same as above, firmware broken */
  214. { /* end: all zeroes */ },
  215. };
  216. MODULE_DEVICE_TABLE(pci, wil6210_pcie_ids);
  217. #ifdef CONFIG_PM
  218. #ifdef CONFIG_PM_SLEEP
  219. static int wil6210_suspend(struct device *dev, bool is_runtime)
  220. {
  221. int rc = 0;
  222. struct pci_dev *pdev = to_pci_dev(dev);
  223. struct wil6210_priv *wil = pci_get_drvdata(pdev);
  224. wil_dbg_pm(wil, "%s(%s)\n", __func__,
  225. is_runtime ? "runtime" : "system");
  226. rc = wil_can_suspend(wil, is_runtime);
  227. if (rc)
  228. goto out;
  229. rc = wil_suspend(wil, is_runtime);
  230. if (rc)
  231. goto out;
  232. /* TODO: how do I bring card in low power state? */
  233. /* disable bus mastering */
  234. pci_clear_master(pdev);
  235. /* PCI will call pci_save_state(pdev) and pci_prepare_to_sleep(pdev) */
  236. out:
  237. return rc;
  238. }
  239. static int wil6210_resume(struct device *dev, bool is_runtime)
  240. {
  241. int rc = 0;
  242. struct pci_dev *pdev = to_pci_dev(dev);
  243. struct wil6210_priv *wil = pci_get_drvdata(pdev);
  244. wil_dbg_pm(wil, "%s(%s)\n", __func__,
  245. is_runtime ? "runtime" : "system");
  246. /* allow master */
  247. pci_set_master(pdev);
  248. rc = wil_resume(wil, is_runtime);
  249. if (rc)
  250. pci_clear_master(pdev);
  251. return rc;
  252. }
  253. static int wil6210_pm_suspend(struct device *dev)
  254. {
  255. return wil6210_suspend(dev, false);
  256. }
  257. static int wil6210_pm_resume(struct device *dev)
  258. {
  259. return wil6210_resume(dev, false);
  260. }
  261. #endif /* CONFIG_PM_SLEEP */
  262. #endif /* CONFIG_PM */
  263. static const struct dev_pm_ops wil6210_pm_ops = {
  264. SET_SYSTEM_SLEEP_PM_OPS(wil6210_pm_suspend, wil6210_pm_resume)
  265. };
  266. static struct pci_driver wil6210_driver = {
  267. .probe = wil_pcie_probe,
  268. .remove = wil_pcie_remove,
  269. .id_table = wil6210_pcie_ids,
  270. .name = WIL_NAME,
  271. .driver = {
  272. .pm = &wil6210_pm_ops,
  273. },
  274. };
  275. static int __init wil6210_driver_init(void)
  276. {
  277. int rc;
  278. rc = wil_platform_modinit();
  279. if (rc)
  280. return rc;
  281. rc = pci_register_driver(&wil6210_driver);
  282. if (rc)
  283. wil_platform_modexit();
  284. return rc;
  285. }
  286. module_init(wil6210_driver_init);
  287. static void __exit wil6210_driver_exit(void)
  288. {
  289. pci_unregister_driver(&wil6210_driver);
  290. wil_platform_modexit();
  291. }
  292. module_exit(wil6210_driver_exit);
  293. MODULE_LICENSE("Dual BSD/GPL");
  294. MODULE_AUTHOR("Qualcomm Atheros <wil6210@qca.qualcomm.com>");
  295. MODULE_DESCRIPTION("Driver for 60g WiFi WIL6210 card");