pcie_bus.c 7.1 KB

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