pci.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  1. // SPDX-License-Identifier: BSD-3-Clause-Clear
  2. /*
  3. * Copyright (c) 2019-2020 The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2021-2022, Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/module.h>
  7. #include <linux/msi.h>
  8. #include <linux/pci.h>
  9. #include <linux/of.h>
  10. #include "pci.h"
  11. #include "core.h"
  12. #include "hif.h"
  13. #include "mhi.h"
  14. #include "debug.h"
  15. #include "pcic.h"
  16. #define ATH11K_PCI_BAR_NUM 0
  17. #define ATH11K_PCI_DMA_MASK 32
  18. #define TCSR_SOC_HW_VERSION 0x0224
  19. #define TCSR_SOC_HW_VERSION_MAJOR_MASK GENMASK(11, 8)
  20. #define TCSR_SOC_HW_VERSION_MINOR_MASK GENMASK(7, 0)
  21. #define QCA6390_DEVICE_ID 0x1101
  22. #define QCN9074_DEVICE_ID 0x1104
  23. #define WCN6855_DEVICE_ID 0x1103
  24. static const struct pci_device_id ath11k_pci_id_table[] = {
  25. { PCI_VDEVICE(QCOM, QCA6390_DEVICE_ID) },
  26. { PCI_VDEVICE(QCOM, WCN6855_DEVICE_ID) },
  27. { PCI_VDEVICE(QCOM, QCN9074_DEVICE_ID) },
  28. {0}
  29. };
  30. MODULE_DEVICE_TABLE(pci, ath11k_pci_id_table);
  31. static int ath11k_pci_bus_wake_up(struct ath11k_base *ab)
  32. {
  33. struct ath11k_pci *ab_pci = ath11k_pci_priv(ab);
  34. return mhi_device_get_sync(ab_pci->mhi_ctrl->mhi_dev);
  35. }
  36. static void ath11k_pci_bus_release(struct ath11k_base *ab)
  37. {
  38. struct ath11k_pci *ab_pci = ath11k_pci_priv(ab);
  39. mhi_device_put(ab_pci->mhi_ctrl->mhi_dev);
  40. }
  41. static u32 ath11k_pci_get_window_start(struct ath11k_base *ab, u32 offset)
  42. {
  43. if (!ab->hw_params.static_window_map)
  44. return ATH11K_PCI_WINDOW_START;
  45. if ((offset ^ HAL_SEQ_WCSS_UMAC_OFFSET) < ATH11K_PCI_WINDOW_RANGE_MASK)
  46. /* if offset lies within DP register range, use 3rd window */
  47. return 3 * ATH11K_PCI_WINDOW_START;
  48. else if ((offset ^ HAL_SEQ_WCSS_UMAC_CE0_SRC_REG(ab)) <
  49. ATH11K_PCI_WINDOW_RANGE_MASK)
  50. /* if offset lies within CE register range, use 2nd window */
  51. return 2 * ATH11K_PCI_WINDOW_START;
  52. else
  53. return ATH11K_PCI_WINDOW_START;
  54. }
  55. static inline void ath11k_pci_select_window(struct ath11k_pci *ab_pci, u32 offset)
  56. {
  57. struct ath11k_base *ab = ab_pci->ab;
  58. u32 window = FIELD_GET(ATH11K_PCI_WINDOW_VALUE_MASK, offset);
  59. lockdep_assert_held(&ab_pci->window_lock);
  60. if (window != ab_pci->register_window) {
  61. iowrite32(ATH11K_PCI_WINDOW_ENABLE_BIT | window,
  62. ab->mem + ATH11K_PCI_WINDOW_REG_ADDRESS);
  63. ioread32(ab->mem + ATH11K_PCI_WINDOW_REG_ADDRESS);
  64. ab_pci->register_window = window;
  65. }
  66. }
  67. static void
  68. ath11k_pci_window_write32(struct ath11k_base *ab, u32 offset, u32 value)
  69. {
  70. struct ath11k_pci *ab_pci = ath11k_pci_priv(ab);
  71. u32 window_start;
  72. window_start = ath11k_pci_get_window_start(ab, offset);
  73. if (window_start == ATH11K_PCI_WINDOW_START) {
  74. spin_lock_bh(&ab_pci->window_lock);
  75. ath11k_pci_select_window(ab_pci, offset);
  76. iowrite32(value, ab->mem + window_start +
  77. (offset & ATH11K_PCI_WINDOW_RANGE_MASK));
  78. spin_unlock_bh(&ab_pci->window_lock);
  79. } else {
  80. iowrite32(value, ab->mem + window_start +
  81. (offset & ATH11K_PCI_WINDOW_RANGE_MASK));
  82. }
  83. }
  84. static u32 ath11k_pci_window_read32(struct ath11k_base *ab, u32 offset)
  85. {
  86. struct ath11k_pci *ab_pci = ath11k_pci_priv(ab);
  87. u32 window_start, val;
  88. window_start = ath11k_pci_get_window_start(ab, offset);
  89. if (window_start == ATH11K_PCI_WINDOW_START) {
  90. spin_lock_bh(&ab_pci->window_lock);
  91. ath11k_pci_select_window(ab_pci, offset);
  92. val = ioread32(ab->mem + window_start +
  93. (offset & ATH11K_PCI_WINDOW_RANGE_MASK));
  94. spin_unlock_bh(&ab_pci->window_lock);
  95. } else {
  96. val = ioread32(ab->mem + window_start +
  97. (offset & ATH11K_PCI_WINDOW_RANGE_MASK));
  98. }
  99. return val;
  100. }
  101. int ath11k_pci_get_msi_irq(struct ath11k_base *ab, unsigned int vector)
  102. {
  103. struct pci_dev *pci_dev = to_pci_dev(ab->dev);
  104. return pci_irq_vector(pci_dev, vector);
  105. }
  106. static const struct ath11k_pci_ops ath11k_pci_ops_qca6390 = {
  107. .wakeup = ath11k_pci_bus_wake_up,
  108. .release = ath11k_pci_bus_release,
  109. .get_msi_irq = ath11k_pci_get_msi_irq,
  110. .window_write32 = ath11k_pci_window_write32,
  111. .window_read32 = ath11k_pci_window_read32,
  112. };
  113. static const struct ath11k_pci_ops ath11k_pci_ops_qcn9074 = {
  114. .wakeup = NULL,
  115. .release = NULL,
  116. .get_msi_irq = ath11k_pci_get_msi_irq,
  117. .window_write32 = ath11k_pci_window_write32,
  118. .window_read32 = ath11k_pci_window_read32,
  119. };
  120. static const struct ath11k_msi_config msi_config_one_msi = {
  121. .total_vectors = 1,
  122. .total_users = 4,
  123. .users = (struct ath11k_msi_user[]) {
  124. { .name = "MHI", .num_vectors = 3, .base_vector = 0 },
  125. { .name = "CE", .num_vectors = 1, .base_vector = 0 },
  126. { .name = "WAKE", .num_vectors = 1, .base_vector = 0 },
  127. { .name = "DP", .num_vectors = 1, .base_vector = 0 },
  128. },
  129. };
  130. static inline void ath11k_pci_select_static_window(struct ath11k_pci *ab_pci)
  131. {
  132. u32 umac_window;
  133. u32 ce_window;
  134. u32 window;
  135. umac_window = FIELD_GET(ATH11K_PCI_WINDOW_VALUE_MASK, HAL_SEQ_WCSS_UMAC_OFFSET);
  136. ce_window = FIELD_GET(ATH11K_PCI_WINDOW_VALUE_MASK, HAL_CE_WFSS_CE_REG_BASE);
  137. window = (umac_window << 12) | (ce_window << 6);
  138. iowrite32(ATH11K_PCI_WINDOW_ENABLE_BIT | window,
  139. ab_pci->ab->mem + ATH11K_PCI_WINDOW_REG_ADDRESS);
  140. }
  141. static void ath11k_pci_soc_global_reset(struct ath11k_base *ab)
  142. {
  143. u32 val, delay;
  144. val = ath11k_pcic_read32(ab, PCIE_SOC_GLOBAL_RESET);
  145. val |= PCIE_SOC_GLOBAL_RESET_V;
  146. ath11k_pcic_write32(ab, PCIE_SOC_GLOBAL_RESET, val);
  147. /* TODO: exact time to sleep is uncertain */
  148. delay = 10;
  149. mdelay(delay);
  150. /* Need to toggle V bit back otherwise stuck in reset status */
  151. val &= ~PCIE_SOC_GLOBAL_RESET_V;
  152. ath11k_pcic_write32(ab, PCIE_SOC_GLOBAL_RESET, val);
  153. mdelay(delay);
  154. val = ath11k_pcic_read32(ab, PCIE_SOC_GLOBAL_RESET);
  155. if (val == 0xffffffff)
  156. ath11k_warn(ab, "link down error during global reset\n");
  157. }
  158. static void ath11k_pci_clear_dbg_registers(struct ath11k_base *ab)
  159. {
  160. u32 val;
  161. /* read cookie */
  162. val = ath11k_pcic_read32(ab, PCIE_Q6_COOKIE_ADDR);
  163. ath11k_dbg(ab, ATH11K_DBG_PCI, "cookie:0x%x\n", val);
  164. val = ath11k_pcic_read32(ab, WLAON_WARM_SW_ENTRY);
  165. ath11k_dbg(ab, ATH11K_DBG_PCI, "WLAON_WARM_SW_ENTRY 0x%x\n", val);
  166. /* TODO: exact time to sleep is uncertain */
  167. mdelay(10);
  168. /* write 0 to WLAON_WARM_SW_ENTRY to prevent Q6 from
  169. * continuing warm path and entering dead loop.
  170. */
  171. ath11k_pcic_write32(ab, WLAON_WARM_SW_ENTRY, 0);
  172. mdelay(10);
  173. val = ath11k_pcic_read32(ab, WLAON_WARM_SW_ENTRY);
  174. ath11k_dbg(ab, ATH11K_DBG_PCI, "WLAON_WARM_SW_ENTRY 0x%x\n", val);
  175. /* A read clear register. clear the register to prevent
  176. * Q6 from entering wrong code path.
  177. */
  178. val = ath11k_pcic_read32(ab, WLAON_SOC_RESET_CAUSE_REG);
  179. ath11k_dbg(ab, ATH11K_DBG_PCI, "soc reset cause:%d\n", val);
  180. }
  181. static int ath11k_pci_set_link_reg(struct ath11k_base *ab,
  182. u32 offset, u32 value, u32 mask)
  183. {
  184. u32 v;
  185. int i;
  186. v = ath11k_pcic_read32(ab, offset);
  187. if ((v & mask) == value)
  188. return 0;
  189. for (i = 0; i < 10; i++) {
  190. ath11k_pcic_write32(ab, offset, (v & ~mask) | value);
  191. v = ath11k_pcic_read32(ab, offset);
  192. if ((v & mask) == value)
  193. return 0;
  194. mdelay(2);
  195. }
  196. ath11k_warn(ab, "failed to set pcie link register 0x%08x: 0x%08x != 0x%08x\n",
  197. offset, v & mask, value);
  198. return -ETIMEDOUT;
  199. }
  200. static int ath11k_pci_fix_l1ss(struct ath11k_base *ab)
  201. {
  202. int ret;
  203. ret = ath11k_pci_set_link_reg(ab,
  204. PCIE_QSERDES_COM_SYSCLK_EN_SEL_REG(ab),
  205. PCIE_QSERDES_COM_SYSCLK_EN_SEL_VAL,
  206. PCIE_QSERDES_COM_SYSCLK_EN_SEL_MSK);
  207. if (ret) {
  208. ath11k_warn(ab, "failed to set sysclk: %d\n", ret);
  209. return ret;
  210. }
  211. ret = ath11k_pci_set_link_reg(ab,
  212. PCIE_PCS_OSC_DTCT_CONFIG1_REG(ab),
  213. PCIE_PCS_OSC_DTCT_CONFIG1_VAL,
  214. PCIE_PCS_OSC_DTCT_CONFIG_MSK);
  215. if (ret) {
  216. ath11k_warn(ab, "failed to set dtct config1 error: %d\n", ret);
  217. return ret;
  218. }
  219. ret = ath11k_pci_set_link_reg(ab,
  220. PCIE_PCS_OSC_DTCT_CONFIG2_REG(ab),
  221. PCIE_PCS_OSC_DTCT_CONFIG2_VAL,
  222. PCIE_PCS_OSC_DTCT_CONFIG_MSK);
  223. if (ret) {
  224. ath11k_warn(ab, "failed to set dtct config2: %d\n", ret);
  225. return ret;
  226. }
  227. ret = ath11k_pci_set_link_reg(ab,
  228. PCIE_PCS_OSC_DTCT_CONFIG4_REG(ab),
  229. PCIE_PCS_OSC_DTCT_CONFIG4_VAL,
  230. PCIE_PCS_OSC_DTCT_CONFIG_MSK);
  231. if (ret) {
  232. ath11k_warn(ab, "failed to set dtct config4: %d\n", ret);
  233. return ret;
  234. }
  235. return 0;
  236. }
  237. static void ath11k_pci_enable_ltssm(struct ath11k_base *ab)
  238. {
  239. u32 val;
  240. int i;
  241. val = ath11k_pcic_read32(ab, PCIE_PCIE_PARF_LTSSM);
  242. /* PCIE link seems very unstable after the Hot Reset*/
  243. for (i = 0; val != PARM_LTSSM_VALUE && i < 5; i++) {
  244. if (val == 0xffffffff)
  245. mdelay(5);
  246. ath11k_pcic_write32(ab, PCIE_PCIE_PARF_LTSSM, PARM_LTSSM_VALUE);
  247. val = ath11k_pcic_read32(ab, PCIE_PCIE_PARF_LTSSM);
  248. }
  249. ath11k_dbg(ab, ATH11K_DBG_PCI, "pci ltssm 0x%x\n", val);
  250. val = ath11k_pcic_read32(ab, GCC_GCC_PCIE_HOT_RST);
  251. val |= GCC_GCC_PCIE_HOT_RST_VAL;
  252. ath11k_pcic_write32(ab, GCC_GCC_PCIE_HOT_RST, val);
  253. val = ath11k_pcic_read32(ab, GCC_GCC_PCIE_HOT_RST);
  254. ath11k_dbg(ab, ATH11K_DBG_PCI, "pci pcie_hot_rst 0x%x\n", val);
  255. mdelay(5);
  256. }
  257. static void ath11k_pci_clear_all_intrs(struct ath11k_base *ab)
  258. {
  259. /* This is a WAR for PCIE Hotreset.
  260. * When target receive Hotreset, but will set the interrupt.
  261. * So when download SBL again, SBL will open Interrupt and
  262. * receive it, and crash immediately.
  263. */
  264. ath11k_pcic_write32(ab, PCIE_PCIE_INT_ALL_CLEAR, PCIE_INT_CLEAR_ALL);
  265. }
  266. static void ath11k_pci_set_wlaon_pwr_ctrl(struct ath11k_base *ab)
  267. {
  268. u32 val;
  269. val = ath11k_pcic_read32(ab, WLAON_QFPROM_PWR_CTRL_REG);
  270. val &= ~QFPROM_PWR_CTRL_VDD4BLOW_MASK;
  271. ath11k_pcic_write32(ab, WLAON_QFPROM_PWR_CTRL_REG, val);
  272. }
  273. static void ath11k_pci_force_wake(struct ath11k_base *ab)
  274. {
  275. ath11k_pcic_write32(ab, PCIE_SOC_WAKE_PCIE_LOCAL_REG, 1);
  276. mdelay(5);
  277. }
  278. static void ath11k_pci_sw_reset(struct ath11k_base *ab, bool power_on)
  279. {
  280. mdelay(100);
  281. if (power_on) {
  282. ath11k_pci_enable_ltssm(ab);
  283. ath11k_pci_clear_all_intrs(ab);
  284. ath11k_pci_set_wlaon_pwr_ctrl(ab);
  285. if (ab->hw_params.fix_l1ss)
  286. ath11k_pci_fix_l1ss(ab);
  287. }
  288. ath11k_mhi_clear_vector(ab);
  289. ath11k_pci_clear_dbg_registers(ab);
  290. ath11k_pci_soc_global_reset(ab);
  291. ath11k_mhi_set_mhictrl_reset(ab);
  292. }
  293. static void ath11k_pci_init_qmi_ce_config(struct ath11k_base *ab)
  294. {
  295. struct ath11k_qmi_ce_cfg *cfg = &ab->qmi.ce_cfg;
  296. cfg->tgt_ce = ab->hw_params.target_ce_config;
  297. cfg->tgt_ce_len = ab->hw_params.target_ce_count;
  298. cfg->svc_to_ce_map = ab->hw_params.svc_to_ce_map;
  299. cfg->svc_to_ce_map_len = ab->hw_params.svc_to_ce_map_len;
  300. ab->qmi.service_ins_id = ab->hw_params.qmi_service_ins_id;
  301. ath11k_ce_get_shadow_config(ab, &cfg->shadow_reg_v2,
  302. &cfg->shadow_reg_v2_len);
  303. }
  304. static void ath11k_pci_msi_config(struct ath11k_pci *ab_pci, bool enable)
  305. {
  306. struct pci_dev *dev = ab_pci->pdev;
  307. u16 control;
  308. pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control);
  309. if (enable)
  310. control |= PCI_MSI_FLAGS_ENABLE;
  311. else
  312. control &= ~PCI_MSI_FLAGS_ENABLE;
  313. pci_write_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, control);
  314. }
  315. static void ath11k_pci_msi_enable(struct ath11k_pci *ab_pci)
  316. {
  317. ath11k_pci_msi_config(ab_pci, true);
  318. }
  319. static void ath11k_pci_msi_disable(struct ath11k_pci *ab_pci)
  320. {
  321. ath11k_pci_msi_config(ab_pci, false);
  322. }
  323. static int ath11k_pci_alloc_msi(struct ath11k_pci *ab_pci)
  324. {
  325. struct ath11k_base *ab = ab_pci->ab;
  326. const struct ath11k_msi_config *msi_config = ab->pci.msi.config;
  327. struct pci_dev *pci_dev = ab_pci->pdev;
  328. struct msi_desc *msi_desc;
  329. int num_vectors;
  330. int ret;
  331. num_vectors = pci_alloc_irq_vectors(pci_dev,
  332. msi_config->total_vectors,
  333. msi_config->total_vectors,
  334. PCI_IRQ_MSI);
  335. if (num_vectors == msi_config->total_vectors) {
  336. set_bit(ATH11K_FLAG_MULTI_MSI_VECTORS, &ab->dev_flags);
  337. } else {
  338. num_vectors = pci_alloc_irq_vectors(ab_pci->pdev,
  339. 1,
  340. 1,
  341. PCI_IRQ_MSI);
  342. if (num_vectors < 0) {
  343. ret = -EINVAL;
  344. goto reset_msi_config;
  345. }
  346. clear_bit(ATH11K_FLAG_MULTI_MSI_VECTORS, &ab->dev_flags);
  347. ab->pci.msi.config = &msi_config_one_msi;
  348. ath11k_dbg(ab, ATH11K_DBG_PCI, "request MSI one vector\n");
  349. }
  350. ath11k_info(ab, "MSI vectors: %d\n", num_vectors);
  351. ath11k_pci_msi_disable(ab_pci);
  352. msi_desc = irq_get_msi_desc(ab_pci->pdev->irq);
  353. if (!msi_desc) {
  354. ath11k_err(ab, "msi_desc is NULL!\n");
  355. ret = -EINVAL;
  356. goto free_msi_vector;
  357. }
  358. ab->pci.msi.ep_base_data = msi_desc->msg.data;
  359. pci_read_config_dword(pci_dev, pci_dev->msi_cap + PCI_MSI_ADDRESS_LO,
  360. &ab->pci.msi.addr_lo);
  361. if (msi_desc->pci.msi_attrib.is_64) {
  362. pci_read_config_dword(pci_dev, pci_dev->msi_cap + PCI_MSI_ADDRESS_HI,
  363. &ab->pci.msi.addr_hi);
  364. } else {
  365. ab->pci.msi.addr_hi = 0;
  366. }
  367. ath11k_dbg(ab, ATH11K_DBG_PCI, "msi base data is %d\n", ab->pci.msi.ep_base_data);
  368. return 0;
  369. free_msi_vector:
  370. pci_free_irq_vectors(ab_pci->pdev);
  371. reset_msi_config:
  372. return ret;
  373. }
  374. static void ath11k_pci_free_msi(struct ath11k_pci *ab_pci)
  375. {
  376. pci_free_irq_vectors(ab_pci->pdev);
  377. }
  378. static int ath11k_pci_config_msi_data(struct ath11k_pci *ab_pci)
  379. {
  380. struct msi_desc *msi_desc;
  381. msi_desc = irq_get_msi_desc(ab_pci->pdev->irq);
  382. if (!msi_desc) {
  383. ath11k_err(ab_pci->ab, "msi_desc is NULL!\n");
  384. pci_free_irq_vectors(ab_pci->pdev);
  385. return -EINVAL;
  386. }
  387. ab_pci->ab->pci.msi.ep_base_data = msi_desc->msg.data;
  388. ath11k_dbg(ab_pci->ab, ATH11K_DBG_PCI, "pci after request_irq msi_ep_base_data %d\n",
  389. ab_pci->ab->pci.msi.ep_base_data);
  390. return 0;
  391. }
  392. static int ath11k_pci_claim(struct ath11k_pci *ab_pci, struct pci_dev *pdev)
  393. {
  394. struct ath11k_base *ab = ab_pci->ab;
  395. u16 device_id;
  396. int ret = 0;
  397. pci_read_config_word(pdev, PCI_DEVICE_ID, &device_id);
  398. if (device_id != ab_pci->dev_id) {
  399. ath11k_err(ab, "pci device id mismatch: 0x%x 0x%x\n",
  400. device_id, ab_pci->dev_id);
  401. ret = -EIO;
  402. goto out;
  403. }
  404. ret = pci_assign_resource(pdev, ATH11K_PCI_BAR_NUM);
  405. if (ret) {
  406. ath11k_err(ab, "failed to assign pci resource: %d\n", ret);
  407. goto out;
  408. }
  409. ret = pci_enable_device(pdev);
  410. if (ret) {
  411. ath11k_err(ab, "failed to enable pci device: %d\n", ret);
  412. goto out;
  413. }
  414. ret = pci_request_region(pdev, ATH11K_PCI_BAR_NUM, "ath11k_pci");
  415. if (ret) {
  416. ath11k_err(ab, "failed to request pci region: %d\n", ret);
  417. goto disable_device;
  418. }
  419. ret = dma_set_mask_and_coherent(&pdev->dev,
  420. DMA_BIT_MASK(ATH11K_PCI_DMA_MASK));
  421. if (ret) {
  422. ath11k_err(ab, "failed to set pci dma mask to %d: %d\n",
  423. ATH11K_PCI_DMA_MASK, ret);
  424. goto release_region;
  425. }
  426. pci_set_master(pdev);
  427. ab->mem_len = pci_resource_len(pdev, ATH11K_PCI_BAR_NUM);
  428. ab->mem = pci_iomap(pdev, ATH11K_PCI_BAR_NUM, 0);
  429. if (!ab->mem) {
  430. ath11k_err(ab, "failed to map pci bar %d\n", ATH11K_PCI_BAR_NUM);
  431. ret = -EIO;
  432. goto clear_master;
  433. }
  434. ath11k_dbg(ab, ATH11K_DBG_BOOT, "boot pci_mem 0x%pK\n", ab->mem);
  435. return 0;
  436. clear_master:
  437. pci_clear_master(pdev);
  438. release_region:
  439. pci_release_region(pdev, ATH11K_PCI_BAR_NUM);
  440. disable_device:
  441. pci_disable_device(pdev);
  442. out:
  443. return ret;
  444. }
  445. static void ath11k_pci_free_region(struct ath11k_pci *ab_pci)
  446. {
  447. struct ath11k_base *ab = ab_pci->ab;
  448. struct pci_dev *pci_dev = ab_pci->pdev;
  449. pci_iounmap(pci_dev, ab->mem);
  450. ab->mem = NULL;
  451. pci_clear_master(pci_dev);
  452. pci_release_region(pci_dev, ATH11K_PCI_BAR_NUM);
  453. if (pci_is_enabled(pci_dev))
  454. pci_disable_device(pci_dev);
  455. }
  456. static void ath11k_pci_aspm_disable(struct ath11k_pci *ab_pci)
  457. {
  458. struct ath11k_base *ab = ab_pci->ab;
  459. pcie_capability_read_word(ab_pci->pdev, PCI_EXP_LNKCTL,
  460. &ab_pci->link_ctl);
  461. ath11k_dbg(ab, ATH11K_DBG_PCI, "pci link_ctl 0x%04x L0s %d L1 %d\n",
  462. ab_pci->link_ctl,
  463. u16_get_bits(ab_pci->link_ctl, PCI_EXP_LNKCTL_ASPM_L0S),
  464. u16_get_bits(ab_pci->link_ctl, PCI_EXP_LNKCTL_ASPM_L1));
  465. /* disable L0s and L1 */
  466. pcie_capability_clear_word(ab_pci->pdev, PCI_EXP_LNKCTL,
  467. PCI_EXP_LNKCTL_ASPMC);
  468. set_bit(ATH11K_PCI_ASPM_RESTORE, &ab_pci->flags);
  469. }
  470. static void ath11k_pci_aspm_restore(struct ath11k_pci *ab_pci)
  471. {
  472. if (test_and_clear_bit(ATH11K_PCI_ASPM_RESTORE, &ab_pci->flags))
  473. pcie_capability_clear_and_set_word(ab_pci->pdev, PCI_EXP_LNKCTL,
  474. PCI_EXP_LNKCTL_ASPMC,
  475. ab_pci->link_ctl &
  476. PCI_EXP_LNKCTL_ASPMC);
  477. }
  478. static int ath11k_pci_power_up(struct ath11k_base *ab)
  479. {
  480. struct ath11k_pci *ab_pci = ath11k_pci_priv(ab);
  481. int ret;
  482. ab_pci->register_window = 0;
  483. clear_bit(ATH11K_FLAG_DEVICE_INIT_DONE, &ab->dev_flags);
  484. ath11k_pci_sw_reset(ab_pci->ab, true);
  485. /* Disable ASPM during firmware download due to problems switching
  486. * to AMSS state.
  487. */
  488. ath11k_pci_aspm_disable(ab_pci);
  489. ath11k_pci_msi_enable(ab_pci);
  490. ret = ath11k_mhi_start(ab_pci);
  491. if (ret) {
  492. ath11k_err(ab, "failed to start mhi: %d\n", ret);
  493. return ret;
  494. }
  495. if (ab->hw_params.static_window_map)
  496. ath11k_pci_select_static_window(ab_pci);
  497. return 0;
  498. }
  499. static void ath11k_pci_power_down(struct ath11k_base *ab)
  500. {
  501. struct ath11k_pci *ab_pci = ath11k_pci_priv(ab);
  502. /* restore aspm in case firmware bootup fails */
  503. ath11k_pci_aspm_restore(ab_pci);
  504. ath11k_pci_force_wake(ab_pci->ab);
  505. ath11k_pci_msi_disable(ab_pci);
  506. ath11k_mhi_stop(ab_pci);
  507. clear_bit(ATH11K_FLAG_DEVICE_INIT_DONE, &ab->dev_flags);
  508. ath11k_pci_sw_reset(ab_pci->ab, false);
  509. }
  510. static int ath11k_pci_hif_suspend(struct ath11k_base *ab)
  511. {
  512. struct ath11k_pci *ar_pci = ath11k_pci_priv(ab);
  513. return ath11k_mhi_suspend(ar_pci);
  514. }
  515. static int ath11k_pci_hif_resume(struct ath11k_base *ab)
  516. {
  517. struct ath11k_pci *ar_pci = ath11k_pci_priv(ab);
  518. return ath11k_mhi_resume(ar_pci);
  519. }
  520. static void ath11k_pci_hif_ce_irq_enable(struct ath11k_base *ab)
  521. {
  522. ath11k_pcic_ce_irqs_enable(ab);
  523. }
  524. static void ath11k_pci_hif_ce_irq_disable(struct ath11k_base *ab)
  525. {
  526. ath11k_pcic_ce_irq_disable_sync(ab);
  527. }
  528. static int ath11k_pci_start(struct ath11k_base *ab)
  529. {
  530. struct ath11k_pci *ab_pci = ath11k_pci_priv(ab);
  531. /* TODO: for now don't restore ASPM in case of single MSI
  532. * vector as MHI register reading in M2 causes system hang.
  533. */
  534. if (test_bit(ATH11K_FLAG_MULTI_MSI_VECTORS, &ab->dev_flags))
  535. ath11k_pci_aspm_restore(ab_pci);
  536. else
  537. ath11k_info(ab, "leaving PCI ASPM disabled to avoid MHI M2 problems\n");
  538. ath11k_pcic_start(ab);
  539. return 0;
  540. }
  541. static const struct ath11k_hif_ops ath11k_pci_hif_ops = {
  542. .start = ath11k_pci_start,
  543. .stop = ath11k_pcic_stop,
  544. .read32 = ath11k_pcic_read32,
  545. .write32 = ath11k_pcic_write32,
  546. .read = ath11k_pcic_read,
  547. .power_down = ath11k_pci_power_down,
  548. .power_up = ath11k_pci_power_up,
  549. .suspend = ath11k_pci_hif_suspend,
  550. .resume = ath11k_pci_hif_resume,
  551. .irq_enable = ath11k_pcic_ext_irq_enable,
  552. .irq_disable = ath11k_pcic_ext_irq_disable,
  553. .get_msi_address = ath11k_pcic_get_msi_address,
  554. .get_user_msi_vector = ath11k_pcic_get_user_msi_assignment,
  555. .map_service_to_pipe = ath11k_pcic_map_service_to_pipe,
  556. .ce_irq_enable = ath11k_pci_hif_ce_irq_enable,
  557. .ce_irq_disable = ath11k_pci_hif_ce_irq_disable,
  558. .get_ce_msi_idx = ath11k_pcic_get_ce_msi_idx,
  559. };
  560. static void ath11k_pci_read_hw_version(struct ath11k_base *ab, u32 *major, u32 *minor)
  561. {
  562. u32 soc_hw_version;
  563. soc_hw_version = ath11k_pcic_read32(ab, TCSR_SOC_HW_VERSION);
  564. *major = FIELD_GET(TCSR_SOC_HW_VERSION_MAJOR_MASK,
  565. soc_hw_version);
  566. *minor = FIELD_GET(TCSR_SOC_HW_VERSION_MINOR_MASK,
  567. soc_hw_version);
  568. ath11k_dbg(ab, ATH11K_DBG_PCI, "pci tcsr_soc_hw_version major %d minor %d\n",
  569. *major, *minor);
  570. }
  571. static int ath11k_pci_set_irq_affinity_hint(struct ath11k_pci *ab_pci,
  572. const struct cpumask *m)
  573. {
  574. if (test_bit(ATH11K_FLAG_MULTI_MSI_VECTORS, &ab_pci->ab->dev_flags))
  575. return 0;
  576. return irq_set_affinity_hint(ab_pci->pdev->irq, m);
  577. }
  578. static int ath11k_pci_probe(struct pci_dev *pdev,
  579. const struct pci_device_id *pci_dev)
  580. {
  581. struct ath11k_base *ab;
  582. struct ath11k_pci *ab_pci;
  583. u32 soc_hw_version_major, soc_hw_version_minor, addr;
  584. const struct ath11k_pci_ops *pci_ops;
  585. int ret;
  586. ab = ath11k_core_alloc(&pdev->dev, sizeof(*ab_pci), ATH11K_BUS_PCI);
  587. if (!ab) {
  588. dev_err(&pdev->dev, "failed to allocate ath11k base\n");
  589. return -ENOMEM;
  590. }
  591. ab->dev = &pdev->dev;
  592. pci_set_drvdata(pdev, ab);
  593. ab_pci = ath11k_pci_priv(ab);
  594. ab_pci->dev_id = pci_dev->device;
  595. ab_pci->ab = ab;
  596. ab_pci->pdev = pdev;
  597. ab->hif.ops = &ath11k_pci_hif_ops;
  598. pci_set_drvdata(pdev, ab);
  599. spin_lock_init(&ab_pci->window_lock);
  600. /* Set fixed_mem_region to true for platforms support reserved memory
  601. * from DT. If memory is reserved from DT for FW, ath11k driver need not
  602. * allocate memory.
  603. */
  604. ret = of_property_read_u32(ab->dev->of_node, "memory-region", &addr);
  605. if (!ret)
  606. set_bit(ATH11K_FLAG_FIXED_MEM_RGN, &ab->dev_flags);
  607. ret = ath11k_pci_claim(ab_pci, pdev);
  608. if (ret) {
  609. ath11k_err(ab, "failed to claim device: %d\n", ret);
  610. goto err_free_core;
  611. }
  612. ath11k_dbg(ab, ATH11K_DBG_BOOT, "pci probe %04x:%04x %04x:%04x\n",
  613. pdev->vendor, pdev->device,
  614. pdev->subsystem_vendor, pdev->subsystem_device);
  615. ab->id.vendor = pdev->vendor;
  616. ab->id.device = pdev->device;
  617. ab->id.subsystem_vendor = pdev->subsystem_vendor;
  618. ab->id.subsystem_device = pdev->subsystem_device;
  619. switch (pci_dev->device) {
  620. case QCA6390_DEVICE_ID:
  621. ath11k_pci_read_hw_version(ab, &soc_hw_version_major,
  622. &soc_hw_version_minor);
  623. switch (soc_hw_version_major) {
  624. case 2:
  625. ab->hw_rev = ATH11K_HW_QCA6390_HW20;
  626. break;
  627. default:
  628. dev_err(&pdev->dev, "Unsupported QCA6390 SOC hardware version: %d %d\n",
  629. soc_hw_version_major, soc_hw_version_minor);
  630. ret = -EOPNOTSUPP;
  631. goto err_pci_free_region;
  632. }
  633. pci_ops = &ath11k_pci_ops_qca6390;
  634. break;
  635. case QCN9074_DEVICE_ID:
  636. pci_ops = &ath11k_pci_ops_qcn9074;
  637. ab->hw_rev = ATH11K_HW_QCN9074_HW10;
  638. break;
  639. case WCN6855_DEVICE_ID:
  640. ab->id.bdf_search = ATH11K_BDF_SEARCH_BUS_AND_BOARD;
  641. ath11k_pci_read_hw_version(ab, &soc_hw_version_major,
  642. &soc_hw_version_minor);
  643. switch (soc_hw_version_major) {
  644. case 2:
  645. switch (soc_hw_version_minor) {
  646. case 0x00:
  647. case 0x01:
  648. ab->hw_rev = ATH11K_HW_WCN6855_HW20;
  649. break;
  650. case 0x10:
  651. case 0x11:
  652. ab->hw_rev = ATH11K_HW_WCN6855_HW21;
  653. break;
  654. default:
  655. goto unsupported_wcn6855_soc;
  656. }
  657. break;
  658. default:
  659. unsupported_wcn6855_soc:
  660. dev_err(&pdev->dev, "Unsupported WCN6855 SOC hardware version: %d %d\n",
  661. soc_hw_version_major, soc_hw_version_minor);
  662. ret = -EOPNOTSUPP;
  663. goto err_pci_free_region;
  664. }
  665. pci_ops = &ath11k_pci_ops_qca6390;
  666. break;
  667. default:
  668. dev_err(&pdev->dev, "Unknown PCI device found: 0x%x\n",
  669. pci_dev->device);
  670. ret = -EOPNOTSUPP;
  671. goto err_pci_free_region;
  672. }
  673. ret = ath11k_pcic_register_pci_ops(ab, pci_ops);
  674. if (ret) {
  675. ath11k_err(ab, "failed to register PCI ops: %d\n", ret);
  676. goto err_pci_free_region;
  677. }
  678. ret = ath11k_pcic_init_msi_config(ab);
  679. if (ret) {
  680. ath11k_err(ab, "failed to init msi config: %d\n", ret);
  681. goto err_pci_free_region;
  682. }
  683. ret = ath11k_pci_alloc_msi(ab_pci);
  684. if (ret) {
  685. ath11k_err(ab, "failed to enable msi: %d\n", ret);
  686. goto err_pci_free_region;
  687. }
  688. ret = ath11k_core_pre_init(ab);
  689. if (ret)
  690. goto err_pci_disable_msi;
  691. ret = ath11k_pci_set_irq_affinity_hint(ab_pci, cpumask_of(0));
  692. if (ret) {
  693. ath11k_err(ab, "failed to set irq affinity %d\n", ret);
  694. goto err_pci_disable_msi;
  695. }
  696. ret = ath11k_mhi_register(ab_pci);
  697. if (ret) {
  698. ath11k_err(ab, "failed to register mhi: %d\n", ret);
  699. goto err_irq_affinity_cleanup;
  700. }
  701. ret = ath11k_hal_srng_init(ab);
  702. if (ret)
  703. goto err_mhi_unregister;
  704. ret = ath11k_ce_alloc_pipes(ab);
  705. if (ret) {
  706. ath11k_err(ab, "failed to allocate ce pipes: %d\n", ret);
  707. goto err_hal_srng_deinit;
  708. }
  709. ath11k_pci_init_qmi_ce_config(ab);
  710. ret = ath11k_pcic_config_irq(ab);
  711. if (ret) {
  712. ath11k_err(ab, "failed to config irq: %d\n", ret);
  713. goto err_ce_free;
  714. }
  715. /* kernel may allocate a dummy vector before request_irq and
  716. * then allocate a real vector when request_irq is called.
  717. * So get msi_data here again to avoid spurious interrupt
  718. * as msi_data will configured to srngs.
  719. */
  720. ret = ath11k_pci_config_msi_data(ab_pci);
  721. if (ret) {
  722. ath11k_err(ab, "failed to config msi_data: %d\n", ret);
  723. goto err_free_irq;
  724. }
  725. ret = ath11k_core_init(ab);
  726. if (ret) {
  727. ath11k_err(ab, "failed to init core: %d\n", ret);
  728. goto err_free_irq;
  729. }
  730. return 0;
  731. err_free_irq:
  732. ath11k_pcic_free_irq(ab);
  733. err_ce_free:
  734. ath11k_ce_free_pipes(ab);
  735. err_hal_srng_deinit:
  736. ath11k_hal_srng_deinit(ab);
  737. err_mhi_unregister:
  738. ath11k_mhi_unregister(ab_pci);
  739. err_irq_affinity_cleanup:
  740. ath11k_pci_set_irq_affinity_hint(ab_pci, NULL);
  741. err_pci_disable_msi:
  742. ath11k_pci_free_msi(ab_pci);
  743. err_pci_free_region:
  744. ath11k_pci_free_region(ab_pci);
  745. err_free_core:
  746. ath11k_core_free(ab);
  747. return ret;
  748. }
  749. static void ath11k_pci_remove(struct pci_dev *pdev)
  750. {
  751. struct ath11k_base *ab = pci_get_drvdata(pdev);
  752. struct ath11k_pci *ab_pci = ath11k_pci_priv(ab);
  753. ath11k_pci_set_irq_affinity_hint(ab_pci, NULL);
  754. if (test_bit(ATH11K_FLAG_QMI_FAIL, &ab->dev_flags)) {
  755. ath11k_pci_power_down(ab);
  756. ath11k_debugfs_soc_destroy(ab);
  757. ath11k_qmi_deinit_service(ab);
  758. goto qmi_fail;
  759. }
  760. set_bit(ATH11K_FLAG_UNREGISTERING, &ab->dev_flags);
  761. ath11k_core_deinit(ab);
  762. qmi_fail:
  763. ath11k_mhi_unregister(ab_pci);
  764. ath11k_pcic_free_irq(ab);
  765. ath11k_pci_free_msi(ab_pci);
  766. ath11k_pci_free_region(ab_pci);
  767. ath11k_hal_srng_deinit(ab);
  768. ath11k_ce_free_pipes(ab);
  769. ath11k_core_free(ab);
  770. }
  771. static void ath11k_pci_shutdown(struct pci_dev *pdev)
  772. {
  773. struct ath11k_base *ab = pci_get_drvdata(pdev);
  774. struct ath11k_pci *ab_pci = ath11k_pci_priv(ab);
  775. ath11k_pci_set_irq_affinity_hint(ab_pci, NULL);
  776. ath11k_pci_power_down(ab);
  777. }
  778. static __maybe_unused int ath11k_pci_pm_suspend(struct device *dev)
  779. {
  780. struct ath11k_base *ab = dev_get_drvdata(dev);
  781. int ret;
  782. if (test_bit(ATH11K_FLAG_QMI_FAIL, &ab->dev_flags)) {
  783. ath11k_dbg(ab, ATH11K_DBG_BOOT, "boot skipping pci suspend as qmi is not initialised\n");
  784. return 0;
  785. }
  786. ret = ath11k_core_suspend(ab);
  787. if (ret)
  788. ath11k_warn(ab, "failed to suspend core: %d\n", ret);
  789. return 0;
  790. }
  791. static __maybe_unused int ath11k_pci_pm_resume(struct device *dev)
  792. {
  793. struct ath11k_base *ab = dev_get_drvdata(dev);
  794. int ret;
  795. if (test_bit(ATH11K_FLAG_QMI_FAIL, &ab->dev_flags)) {
  796. ath11k_dbg(ab, ATH11K_DBG_BOOT, "boot skipping pci resume as qmi is not initialised\n");
  797. return 0;
  798. }
  799. ret = ath11k_core_resume(ab);
  800. if (ret)
  801. ath11k_warn(ab, "failed to resume core: %d\n", ret);
  802. return ret;
  803. }
  804. static SIMPLE_DEV_PM_OPS(ath11k_pci_pm_ops,
  805. ath11k_pci_pm_suspend,
  806. ath11k_pci_pm_resume);
  807. static struct pci_driver ath11k_pci_driver = {
  808. .name = "ath11k_pci",
  809. .id_table = ath11k_pci_id_table,
  810. .probe = ath11k_pci_probe,
  811. .remove = ath11k_pci_remove,
  812. .shutdown = ath11k_pci_shutdown,
  813. #ifdef CONFIG_PM
  814. .driver.pm = &ath11k_pci_pm_ops,
  815. #endif
  816. };
  817. static int ath11k_pci_init(void)
  818. {
  819. int ret;
  820. ret = pci_register_driver(&ath11k_pci_driver);
  821. if (ret)
  822. pr_err("failed to register ath11k pci driver: %d\n",
  823. ret);
  824. return ret;
  825. }
  826. module_init(ath11k_pci_init);
  827. static void ath11k_pci_exit(void)
  828. {
  829. pci_unregister_driver(&ath11k_pci_driver);
  830. }
  831. module_exit(ath11k_pci_exit);
  832. MODULE_DESCRIPTION("Driver support for Qualcomm Technologies 802.11ax WLAN PCIe devices");
  833. MODULE_LICENSE("Dual BSD/GPL");
  834. /* QCA639x 2.0 firmware files */
  835. MODULE_FIRMWARE(ATH11K_FW_DIR "/QCA6390/hw2.0/" ATH11K_BOARD_API2_FILE);
  836. MODULE_FIRMWARE(ATH11K_FW_DIR "/QCA6390/hw2.0/" ATH11K_AMSS_FILE);
  837. MODULE_FIRMWARE(ATH11K_FW_DIR "/QCA6390/hw2.0/" ATH11K_M3_FILE);