pcie_bus.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 int use_msi = 1;
  22. module_param(use_msi, int, S_IRUGO);
  23. MODULE_PARM_DESC(use_msi,
  24. " Use MSI interrupt: "
  25. "0 - don't, 1 - (default) - single, or 3");
  26. static bool debug_fw; /* = false; */
  27. module_param(debug_fw, bool, S_IRUGO);
  28. MODULE_PARM_DESC(debug_fw, " load driver if FW not ready. For FW debug");
  29. void wil_disable_irq(struct wil6210_priv *wil)
  30. {
  31. int irq = wil->pdev->irq;
  32. disable_irq(irq);
  33. if (wil->n_msi == 3) {
  34. disable_irq(irq + 1);
  35. disable_irq(irq + 2);
  36. }
  37. }
  38. void wil_enable_irq(struct wil6210_priv *wil)
  39. {
  40. int irq = wil->pdev->irq;
  41. enable_irq(irq);
  42. if (wil->n_msi == 3) {
  43. enable_irq(irq + 1);
  44. enable_irq(irq + 2);
  45. }
  46. }
  47. /* Bus ops */
  48. static int wil_if_pcie_enable(struct wil6210_priv *wil)
  49. {
  50. struct pci_dev *pdev = wil->pdev;
  51. int rc;
  52. /* on platforms with buggy ACPI, pdev->msi_enabled may be set to
  53. * allow pci_enable_device to work. This indicates INTx was not routed
  54. * and only MSI should be used
  55. */
  56. int msi_only = pdev->msi_enabled;
  57. wil_dbg_misc(wil, "%s()\n", __func__);
  58. pdev->msi_enabled = 0;
  59. pci_set_master(pdev);
  60. /*
  61. * how many MSI interrupts to request?
  62. */
  63. switch (use_msi) {
  64. case 3:
  65. case 1:
  66. wil_dbg_misc(wil, "Setup %d MSI interrupts\n", use_msi);
  67. break;
  68. case 0:
  69. wil_dbg_misc(wil, "MSI interrupts disabled, use INTx\n");
  70. break;
  71. default:
  72. wil_err(wil, "Invalid use_msi=%d, default to 1\n", use_msi);
  73. use_msi = 1;
  74. }
  75. if (use_msi == 3 && pci_enable_msi_range(pdev, 3, 3) < 0) {
  76. wil_err(wil, "3 MSI mode failed, try 1 MSI\n");
  77. use_msi = 1;
  78. }
  79. if (use_msi == 1 && pci_enable_msi(pdev)) {
  80. wil_err(wil, "pci_enable_msi failed, use INTx\n");
  81. use_msi = 0;
  82. }
  83. wil->n_msi = use_msi;
  84. if ((wil->n_msi == 0) && msi_only) {
  85. wil_err(wil, "Interrupt pin not routed, unable to use INTx\n");
  86. rc = -ENODEV;
  87. goto stop_master;
  88. }
  89. rc = wil6210_init_irq(wil, pdev->irq);
  90. if (rc)
  91. goto stop_master;
  92. /* need reset here to obtain MAC */
  93. mutex_lock(&wil->mutex);
  94. rc = wil_reset(wil);
  95. mutex_unlock(&wil->mutex);
  96. if (debug_fw)
  97. rc = 0;
  98. if (rc)
  99. goto release_irq;
  100. return 0;
  101. release_irq:
  102. wil6210_fini_irq(wil, pdev->irq);
  103. /* safe to call if no MSI */
  104. pci_disable_msi(pdev);
  105. stop_master:
  106. pci_clear_master(pdev);
  107. return rc;
  108. }
  109. static int wil_if_pcie_disable(struct wil6210_priv *wil)
  110. {
  111. struct pci_dev *pdev = wil->pdev;
  112. wil_dbg_misc(wil, "%s()\n", __func__);
  113. pci_clear_master(pdev);
  114. /* disable and release IRQ */
  115. wil6210_fini_irq(wil, pdev->irq);
  116. /* safe to call if no MSI */
  117. pci_disable_msi(pdev);
  118. /* TODO: disable HW */
  119. return 0;
  120. }
  121. static int wil_pcie_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  122. {
  123. struct wil6210_priv *wil;
  124. struct device *dev = &pdev->dev;
  125. void __iomem *csr;
  126. struct wil_board *board = (struct wil_board *)id->driver_data;
  127. int rc;
  128. /* check HW */
  129. dev_info(&pdev->dev, WIL_NAME
  130. " \"%s\" device found [%04x:%04x] (rev %x)\n", board->name,
  131. (int)pdev->vendor, (int)pdev->device, (int)pdev->revision);
  132. if (pci_resource_len(pdev, 0) != WIL6210_MEM_SIZE) {
  133. dev_err(&pdev->dev, "Not " WIL_NAME "? "
  134. "BAR0 size is %lu while expecting %lu\n",
  135. (ulong)pci_resource_len(pdev, 0), WIL6210_MEM_SIZE);
  136. return -ENODEV;
  137. }
  138. rc = pci_enable_device(pdev);
  139. if (rc) {
  140. dev_err(&pdev->dev,
  141. "pci_enable_device failed, retry with MSI only\n");
  142. /* Work around for platforms that can't allocate IRQ:
  143. * retry with MSI only
  144. */
  145. pdev->msi_enabled = 1;
  146. rc = pci_enable_device(pdev);
  147. }
  148. if (rc)
  149. return -ENODEV;
  150. /* rollback to err_disable_pdev */
  151. rc = pci_request_region(pdev, 0, WIL_NAME);
  152. if (rc) {
  153. dev_err(&pdev->dev, "pci_request_region failed\n");
  154. goto err_disable_pdev;
  155. }
  156. /* rollback to err_release_reg */
  157. csr = pci_ioremap_bar(pdev, 0);
  158. if (!csr) {
  159. dev_err(&pdev->dev, "pci_ioremap_bar failed\n");
  160. rc = -ENODEV;
  161. goto err_release_reg;
  162. }
  163. /* rollback to err_iounmap */
  164. dev_info(&pdev->dev, "CSR at %pR -> 0x%p\n", &pdev->resource[0], csr);
  165. wil = wil_if_alloc(dev, csr);
  166. if (IS_ERR(wil)) {
  167. rc = (int)PTR_ERR(wil);
  168. dev_err(dev, "wil_if_alloc failed: %d\n", rc);
  169. goto err_iounmap;
  170. }
  171. /* rollback to if_free */
  172. pci_set_drvdata(pdev, wil);
  173. wil->pdev = pdev;
  174. wil->board = board;
  175. wil6210_clear_irq(wil);
  176. wil->platform_handle =
  177. wil_platform_init(&pdev->dev, &wil->platform_ops);
  178. /* FW should raise IRQ when ready */
  179. rc = wil_if_pcie_enable(wil);
  180. if (rc) {
  181. wil_err(wil, "Enable device failed\n");
  182. goto if_free;
  183. }
  184. /* rollback to bus_disable */
  185. rc = wil_if_add(wil);
  186. if (rc) {
  187. wil_err(wil, "wil_if_add failed: %d\n", rc);
  188. goto bus_disable;
  189. }
  190. wil6210_debugfs_init(wil);
  191. /* check FW is alive */
  192. wmi_echo(wil);
  193. return 0;
  194. bus_disable:
  195. wil_if_pcie_disable(wil);
  196. if_free:
  197. if (wil->platform_ops.uninit)
  198. wil->platform_ops.uninit(wil->platform_handle);
  199. wil_if_free(wil);
  200. err_iounmap:
  201. pci_iounmap(pdev, csr);
  202. err_release_reg:
  203. pci_release_region(pdev, 0);
  204. err_disable_pdev:
  205. pci_disable_device(pdev);
  206. return rc;
  207. }
  208. static void wil_pcie_remove(struct pci_dev *pdev)
  209. {
  210. struct wil6210_priv *wil = pci_get_drvdata(pdev);
  211. void __iomem *csr = wil->csr;
  212. wil_dbg_misc(wil, "%s()\n", __func__);
  213. wil6210_debugfs_remove(wil);
  214. wil_if_remove(wil);
  215. wil_if_pcie_disable(wil);
  216. if (wil->platform_ops.uninit)
  217. wil->platform_ops.uninit(wil->platform_handle);
  218. wil_if_free(wil);
  219. pci_iounmap(pdev, csr);
  220. pci_release_region(pdev, 0);
  221. pci_disable_device(pdev);
  222. }
  223. static const struct wil_board wil_board_marlon = {
  224. .board = WIL_BOARD_MARLON,
  225. .name = "marlon",
  226. };
  227. static const struct wil_board wil_board_sparrow = {
  228. .board = WIL_BOARD_SPARROW,
  229. .name = "sparrow",
  230. };
  231. static const struct pci_device_id wil6210_pcie_ids[] = {
  232. { PCI_DEVICE(0x1ae9, 0x0301),
  233. .driver_data = (kernel_ulong_t)&wil_board_marlon },
  234. { PCI_DEVICE(0x1ae9, 0x0310),
  235. .driver_data = (kernel_ulong_t)&wil_board_sparrow },
  236. { PCI_DEVICE(0x1ae9, 0x0302), /* same as above, firmware broken */
  237. .driver_data = (kernel_ulong_t)&wil_board_sparrow },
  238. { /* end: all zeroes */ },
  239. };
  240. MODULE_DEVICE_TABLE(pci, wil6210_pcie_ids);
  241. static struct pci_driver wil6210_driver = {
  242. .probe = wil_pcie_probe,
  243. .remove = wil_pcie_remove,
  244. .id_table = wil6210_pcie_ids,
  245. .name = WIL_NAME,
  246. };
  247. module_pci_driver(wil6210_driver);
  248. MODULE_LICENSE("Dual BSD/GPL");
  249. MODULE_AUTHOR("Qualcomm Atheros <wil6210@qca.qualcomm.com>");
  250. MODULE_DESCRIPTION("Driver for 60g WiFi WIL6210 card");