ahb.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  1. // SPDX-License-Identifier: ISC
  2. /*
  3. * Copyright (c) 2016-2017 Qualcomm Atheros, Inc. All rights reserved.
  4. * Copyright (c) 2015 The Linux Foundation. All rights reserved.
  5. */
  6. #include <linux/module.h>
  7. #include <linux/of.h>
  8. #include <linux/of_device.h>
  9. #include <linux/clk.h>
  10. #include <linux/reset.h>
  11. #include "core.h"
  12. #include "debug.h"
  13. #include "pci.h"
  14. #include "ahb.h"
  15. static const struct of_device_id ath10k_ahb_of_match[] = {
  16. { .compatible = "qcom,ipq4019-wifi",
  17. .data = (void *)ATH10K_HW_QCA4019
  18. },
  19. { }
  20. };
  21. MODULE_DEVICE_TABLE(of, ath10k_ahb_of_match);
  22. #define QCA4019_SRAM_ADDR 0x000C0000
  23. #define QCA4019_SRAM_LEN 0x00040000 /* 256 kb */
  24. static inline struct ath10k_ahb *ath10k_ahb_priv(struct ath10k *ar)
  25. {
  26. return &((struct ath10k_pci *)ar->drv_priv)->ahb[0];
  27. }
  28. static void ath10k_ahb_write32(struct ath10k *ar, u32 offset, u32 value)
  29. {
  30. struct ath10k_ahb *ar_ahb = ath10k_ahb_priv(ar);
  31. iowrite32(value, ar_ahb->mem + offset);
  32. }
  33. static u32 ath10k_ahb_read32(struct ath10k *ar, u32 offset)
  34. {
  35. struct ath10k_ahb *ar_ahb = ath10k_ahb_priv(ar);
  36. return ioread32(ar_ahb->mem + offset);
  37. }
  38. static u32 ath10k_ahb_gcc_read32(struct ath10k *ar, u32 offset)
  39. {
  40. struct ath10k_ahb *ar_ahb = ath10k_ahb_priv(ar);
  41. return ioread32(ar_ahb->gcc_mem + offset);
  42. }
  43. static void ath10k_ahb_tcsr_write32(struct ath10k *ar, u32 offset, u32 value)
  44. {
  45. struct ath10k_ahb *ar_ahb = ath10k_ahb_priv(ar);
  46. iowrite32(value, ar_ahb->tcsr_mem + offset);
  47. }
  48. static u32 ath10k_ahb_tcsr_read32(struct ath10k *ar, u32 offset)
  49. {
  50. struct ath10k_ahb *ar_ahb = ath10k_ahb_priv(ar);
  51. return ioread32(ar_ahb->tcsr_mem + offset);
  52. }
  53. static u32 ath10k_ahb_soc_read32(struct ath10k *ar, u32 addr)
  54. {
  55. return ath10k_ahb_read32(ar, RTC_SOC_BASE_ADDRESS + addr);
  56. }
  57. static int ath10k_ahb_get_num_banks(struct ath10k *ar)
  58. {
  59. if (ar->hw_rev == ATH10K_HW_QCA4019)
  60. return 1;
  61. ath10k_warn(ar, "unknown number of banks, assuming 1\n");
  62. return 1;
  63. }
  64. static int ath10k_ahb_clock_init(struct ath10k *ar)
  65. {
  66. struct ath10k_ahb *ar_ahb = ath10k_ahb_priv(ar);
  67. struct device *dev;
  68. dev = &ar_ahb->pdev->dev;
  69. ar_ahb->cmd_clk = devm_clk_get(dev, "wifi_wcss_cmd");
  70. if (IS_ERR_OR_NULL(ar_ahb->cmd_clk)) {
  71. ath10k_err(ar, "failed to get cmd clk: %ld\n",
  72. PTR_ERR(ar_ahb->cmd_clk));
  73. return ar_ahb->cmd_clk ? PTR_ERR(ar_ahb->cmd_clk) : -ENODEV;
  74. }
  75. ar_ahb->ref_clk = devm_clk_get(dev, "wifi_wcss_ref");
  76. if (IS_ERR_OR_NULL(ar_ahb->ref_clk)) {
  77. ath10k_err(ar, "failed to get ref clk: %ld\n",
  78. PTR_ERR(ar_ahb->ref_clk));
  79. return ar_ahb->ref_clk ? PTR_ERR(ar_ahb->ref_clk) : -ENODEV;
  80. }
  81. ar_ahb->rtc_clk = devm_clk_get(dev, "wifi_wcss_rtc");
  82. if (IS_ERR_OR_NULL(ar_ahb->rtc_clk)) {
  83. ath10k_err(ar, "failed to get rtc clk: %ld\n",
  84. PTR_ERR(ar_ahb->rtc_clk));
  85. return ar_ahb->rtc_clk ? PTR_ERR(ar_ahb->rtc_clk) : -ENODEV;
  86. }
  87. return 0;
  88. }
  89. static void ath10k_ahb_clock_deinit(struct ath10k *ar)
  90. {
  91. struct ath10k_ahb *ar_ahb = ath10k_ahb_priv(ar);
  92. ar_ahb->cmd_clk = NULL;
  93. ar_ahb->ref_clk = NULL;
  94. ar_ahb->rtc_clk = NULL;
  95. }
  96. static int ath10k_ahb_clock_enable(struct ath10k *ar)
  97. {
  98. struct ath10k_ahb *ar_ahb = ath10k_ahb_priv(ar);
  99. int ret;
  100. if (IS_ERR_OR_NULL(ar_ahb->cmd_clk) ||
  101. IS_ERR_OR_NULL(ar_ahb->ref_clk) ||
  102. IS_ERR_OR_NULL(ar_ahb->rtc_clk)) {
  103. ath10k_err(ar, "clock(s) is/are not initialized\n");
  104. ret = -EIO;
  105. goto out;
  106. }
  107. ret = clk_prepare_enable(ar_ahb->cmd_clk);
  108. if (ret) {
  109. ath10k_err(ar, "failed to enable cmd clk: %d\n", ret);
  110. goto out;
  111. }
  112. ret = clk_prepare_enable(ar_ahb->ref_clk);
  113. if (ret) {
  114. ath10k_err(ar, "failed to enable ref clk: %d\n", ret);
  115. goto err_cmd_clk_disable;
  116. }
  117. ret = clk_prepare_enable(ar_ahb->rtc_clk);
  118. if (ret) {
  119. ath10k_err(ar, "failed to enable rtc clk: %d\n", ret);
  120. goto err_ref_clk_disable;
  121. }
  122. return 0;
  123. err_ref_clk_disable:
  124. clk_disable_unprepare(ar_ahb->ref_clk);
  125. err_cmd_clk_disable:
  126. clk_disable_unprepare(ar_ahb->cmd_clk);
  127. out:
  128. return ret;
  129. }
  130. static void ath10k_ahb_clock_disable(struct ath10k *ar)
  131. {
  132. struct ath10k_ahb *ar_ahb = ath10k_ahb_priv(ar);
  133. clk_disable_unprepare(ar_ahb->cmd_clk);
  134. clk_disable_unprepare(ar_ahb->ref_clk);
  135. clk_disable_unprepare(ar_ahb->rtc_clk);
  136. }
  137. static int ath10k_ahb_rst_ctrl_init(struct ath10k *ar)
  138. {
  139. struct ath10k_ahb *ar_ahb = ath10k_ahb_priv(ar);
  140. struct device *dev;
  141. dev = &ar_ahb->pdev->dev;
  142. ar_ahb->core_cold_rst = devm_reset_control_get_exclusive(dev,
  143. "wifi_core_cold");
  144. if (IS_ERR(ar_ahb->core_cold_rst)) {
  145. ath10k_err(ar, "failed to get core cold rst ctrl: %ld\n",
  146. PTR_ERR(ar_ahb->core_cold_rst));
  147. return PTR_ERR(ar_ahb->core_cold_rst);
  148. }
  149. ar_ahb->radio_cold_rst = devm_reset_control_get_exclusive(dev,
  150. "wifi_radio_cold");
  151. if (IS_ERR(ar_ahb->radio_cold_rst)) {
  152. ath10k_err(ar, "failed to get radio cold rst ctrl: %ld\n",
  153. PTR_ERR(ar_ahb->radio_cold_rst));
  154. return PTR_ERR(ar_ahb->radio_cold_rst);
  155. }
  156. ar_ahb->radio_warm_rst = devm_reset_control_get_exclusive(dev,
  157. "wifi_radio_warm");
  158. if (IS_ERR(ar_ahb->radio_warm_rst)) {
  159. ath10k_err(ar, "failed to get radio warm rst ctrl: %ld\n",
  160. PTR_ERR(ar_ahb->radio_warm_rst));
  161. return PTR_ERR(ar_ahb->radio_warm_rst);
  162. }
  163. ar_ahb->radio_srif_rst = devm_reset_control_get_exclusive(dev,
  164. "wifi_radio_srif");
  165. if (IS_ERR(ar_ahb->radio_srif_rst)) {
  166. ath10k_err(ar, "failed to get radio srif rst ctrl: %ld\n",
  167. PTR_ERR(ar_ahb->radio_srif_rst));
  168. return PTR_ERR(ar_ahb->radio_srif_rst);
  169. }
  170. ar_ahb->cpu_init_rst = devm_reset_control_get_exclusive(dev,
  171. "wifi_cpu_init");
  172. if (IS_ERR(ar_ahb->cpu_init_rst)) {
  173. ath10k_err(ar, "failed to get cpu init rst ctrl: %ld\n",
  174. PTR_ERR(ar_ahb->cpu_init_rst));
  175. return PTR_ERR(ar_ahb->cpu_init_rst);
  176. }
  177. return 0;
  178. }
  179. static void ath10k_ahb_rst_ctrl_deinit(struct ath10k *ar)
  180. {
  181. struct ath10k_ahb *ar_ahb = ath10k_ahb_priv(ar);
  182. ar_ahb->core_cold_rst = NULL;
  183. ar_ahb->radio_cold_rst = NULL;
  184. ar_ahb->radio_warm_rst = NULL;
  185. ar_ahb->radio_srif_rst = NULL;
  186. ar_ahb->cpu_init_rst = NULL;
  187. }
  188. static int ath10k_ahb_release_reset(struct ath10k *ar)
  189. {
  190. struct ath10k_ahb *ar_ahb = ath10k_ahb_priv(ar);
  191. int ret;
  192. if (IS_ERR_OR_NULL(ar_ahb->radio_cold_rst) ||
  193. IS_ERR_OR_NULL(ar_ahb->radio_warm_rst) ||
  194. IS_ERR_OR_NULL(ar_ahb->radio_srif_rst) ||
  195. IS_ERR_OR_NULL(ar_ahb->cpu_init_rst)) {
  196. ath10k_err(ar, "rst ctrl(s) is/are not initialized\n");
  197. return -EINVAL;
  198. }
  199. ret = reset_control_deassert(ar_ahb->radio_cold_rst);
  200. if (ret) {
  201. ath10k_err(ar, "failed to deassert radio cold rst: %d\n", ret);
  202. return ret;
  203. }
  204. ret = reset_control_deassert(ar_ahb->radio_warm_rst);
  205. if (ret) {
  206. ath10k_err(ar, "failed to deassert radio warm rst: %d\n", ret);
  207. return ret;
  208. }
  209. ret = reset_control_deassert(ar_ahb->radio_srif_rst);
  210. if (ret) {
  211. ath10k_err(ar, "failed to deassert radio srif rst: %d\n", ret);
  212. return ret;
  213. }
  214. ret = reset_control_deassert(ar_ahb->cpu_init_rst);
  215. if (ret) {
  216. ath10k_err(ar, "failed to deassert cpu init rst: %d\n", ret);
  217. return ret;
  218. }
  219. return 0;
  220. }
  221. static void ath10k_ahb_halt_axi_bus(struct ath10k *ar, u32 haltreq_reg,
  222. u32 haltack_reg)
  223. {
  224. unsigned long timeout;
  225. u32 val;
  226. /* Issue halt axi bus request */
  227. val = ath10k_ahb_tcsr_read32(ar, haltreq_reg);
  228. val |= AHB_AXI_BUS_HALT_REQ;
  229. ath10k_ahb_tcsr_write32(ar, haltreq_reg, val);
  230. /* Wait for axi bus halted ack */
  231. timeout = jiffies + msecs_to_jiffies(ATH10K_AHB_AXI_BUS_HALT_TIMEOUT);
  232. do {
  233. val = ath10k_ahb_tcsr_read32(ar, haltack_reg);
  234. if (val & AHB_AXI_BUS_HALT_ACK)
  235. break;
  236. mdelay(1);
  237. } while (time_before(jiffies, timeout));
  238. if (!(val & AHB_AXI_BUS_HALT_ACK)) {
  239. ath10k_err(ar, "failed to halt axi bus: %d\n", val);
  240. return;
  241. }
  242. ath10k_dbg(ar, ATH10K_DBG_AHB, "axi bus halted\n");
  243. }
  244. static void ath10k_ahb_halt_chip(struct ath10k *ar)
  245. {
  246. struct ath10k_ahb *ar_ahb = ath10k_ahb_priv(ar);
  247. u32 core_id, glb_cfg_reg, haltreq_reg, haltack_reg;
  248. u32 val;
  249. int ret;
  250. if (IS_ERR_OR_NULL(ar_ahb->core_cold_rst) ||
  251. IS_ERR_OR_NULL(ar_ahb->radio_cold_rst) ||
  252. IS_ERR_OR_NULL(ar_ahb->radio_warm_rst) ||
  253. IS_ERR_OR_NULL(ar_ahb->radio_srif_rst) ||
  254. IS_ERR_OR_NULL(ar_ahb->cpu_init_rst)) {
  255. ath10k_err(ar, "rst ctrl(s) is/are not initialized\n");
  256. return;
  257. }
  258. core_id = ath10k_ahb_read32(ar, ATH10K_AHB_WLAN_CORE_ID_REG);
  259. switch (core_id) {
  260. case 0:
  261. glb_cfg_reg = ATH10K_AHB_TCSR_WIFI0_GLB_CFG;
  262. haltreq_reg = ATH10K_AHB_TCSR_WCSS0_HALTREQ;
  263. haltack_reg = ATH10K_AHB_TCSR_WCSS0_HALTACK;
  264. break;
  265. case 1:
  266. glb_cfg_reg = ATH10K_AHB_TCSR_WIFI1_GLB_CFG;
  267. haltreq_reg = ATH10K_AHB_TCSR_WCSS1_HALTREQ;
  268. haltack_reg = ATH10K_AHB_TCSR_WCSS1_HALTACK;
  269. break;
  270. default:
  271. ath10k_err(ar, "invalid core id %d found, skipping reset sequence\n",
  272. core_id);
  273. return;
  274. }
  275. ath10k_ahb_halt_axi_bus(ar, haltreq_reg, haltack_reg);
  276. val = ath10k_ahb_tcsr_read32(ar, glb_cfg_reg);
  277. val |= TCSR_WIFIX_GLB_CFG_DISABLE_CORE_CLK;
  278. ath10k_ahb_tcsr_write32(ar, glb_cfg_reg, val);
  279. ret = reset_control_assert(ar_ahb->core_cold_rst);
  280. if (ret)
  281. ath10k_err(ar, "failed to assert core cold rst: %d\n", ret);
  282. msleep(1);
  283. ret = reset_control_assert(ar_ahb->radio_cold_rst);
  284. if (ret)
  285. ath10k_err(ar, "failed to assert radio cold rst: %d\n", ret);
  286. msleep(1);
  287. ret = reset_control_assert(ar_ahb->radio_warm_rst);
  288. if (ret)
  289. ath10k_err(ar, "failed to assert radio warm rst: %d\n", ret);
  290. msleep(1);
  291. ret = reset_control_assert(ar_ahb->radio_srif_rst);
  292. if (ret)
  293. ath10k_err(ar, "failed to assert radio srif rst: %d\n", ret);
  294. msleep(1);
  295. ret = reset_control_assert(ar_ahb->cpu_init_rst);
  296. if (ret)
  297. ath10k_err(ar, "failed to assert cpu init rst: %d\n", ret);
  298. msleep(10);
  299. /* Clear halt req and core clock disable req before
  300. * deasserting wifi core reset.
  301. */
  302. val = ath10k_ahb_tcsr_read32(ar, haltreq_reg);
  303. val &= ~AHB_AXI_BUS_HALT_REQ;
  304. ath10k_ahb_tcsr_write32(ar, haltreq_reg, val);
  305. val = ath10k_ahb_tcsr_read32(ar, glb_cfg_reg);
  306. val &= ~TCSR_WIFIX_GLB_CFG_DISABLE_CORE_CLK;
  307. ath10k_ahb_tcsr_write32(ar, glb_cfg_reg, val);
  308. ret = reset_control_deassert(ar_ahb->core_cold_rst);
  309. if (ret)
  310. ath10k_err(ar, "failed to deassert core cold rst: %d\n", ret);
  311. ath10k_dbg(ar, ATH10K_DBG_AHB, "core %d reset done\n", core_id);
  312. }
  313. static irqreturn_t ath10k_ahb_interrupt_handler(int irq, void *arg)
  314. {
  315. struct ath10k *ar = arg;
  316. if (!ath10k_pci_irq_pending(ar))
  317. return IRQ_NONE;
  318. ath10k_pci_disable_and_clear_legacy_irq(ar);
  319. ath10k_pci_irq_msi_fw_mask(ar);
  320. napi_schedule(&ar->napi);
  321. return IRQ_HANDLED;
  322. }
  323. static int ath10k_ahb_request_irq_legacy(struct ath10k *ar)
  324. {
  325. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  326. struct ath10k_ahb *ar_ahb = ath10k_ahb_priv(ar);
  327. int ret;
  328. ret = request_irq(ar_ahb->irq,
  329. ath10k_ahb_interrupt_handler,
  330. IRQF_SHARED, "ath10k_ahb", ar);
  331. if (ret) {
  332. ath10k_warn(ar, "failed to request legacy irq %d: %d\n",
  333. ar_ahb->irq, ret);
  334. return ret;
  335. }
  336. ar_pci->oper_irq_mode = ATH10K_PCI_IRQ_LEGACY;
  337. return 0;
  338. }
  339. static void ath10k_ahb_release_irq_legacy(struct ath10k *ar)
  340. {
  341. struct ath10k_ahb *ar_ahb = ath10k_ahb_priv(ar);
  342. free_irq(ar_ahb->irq, ar);
  343. }
  344. static void ath10k_ahb_irq_disable(struct ath10k *ar)
  345. {
  346. ath10k_ce_disable_interrupts(ar);
  347. ath10k_pci_disable_and_clear_legacy_irq(ar);
  348. }
  349. static int ath10k_ahb_resource_init(struct ath10k *ar)
  350. {
  351. struct ath10k_ahb *ar_ahb = ath10k_ahb_priv(ar);
  352. struct platform_device *pdev;
  353. struct resource *res;
  354. int ret;
  355. pdev = ar_ahb->pdev;
  356. ar_ahb->mem = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
  357. if (IS_ERR(ar_ahb->mem)) {
  358. ath10k_err(ar, "mem ioremap error\n");
  359. ret = PTR_ERR(ar_ahb->mem);
  360. goto out;
  361. }
  362. ar_ahb->mem_len = resource_size(res);
  363. ar_ahb->gcc_mem = ioremap(ATH10K_GCC_REG_BASE,
  364. ATH10K_GCC_REG_SIZE);
  365. if (!ar_ahb->gcc_mem) {
  366. ath10k_err(ar, "gcc mem ioremap error\n");
  367. ret = -ENOMEM;
  368. goto err_mem_unmap;
  369. }
  370. ar_ahb->tcsr_mem = ioremap(ATH10K_TCSR_REG_BASE,
  371. ATH10K_TCSR_REG_SIZE);
  372. if (!ar_ahb->tcsr_mem) {
  373. ath10k_err(ar, "tcsr mem ioremap error\n");
  374. ret = -ENOMEM;
  375. goto err_gcc_mem_unmap;
  376. }
  377. ret = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
  378. if (ret) {
  379. ath10k_err(ar, "failed to set 32-bit dma mask: %d\n", ret);
  380. goto err_tcsr_mem_unmap;
  381. }
  382. ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
  383. if (ret) {
  384. ath10k_err(ar, "failed to set 32-bit consistent dma: %d\n",
  385. ret);
  386. goto err_tcsr_mem_unmap;
  387. }
  388. ret = ath10k_ahb_clock_init(ar);
  389. if (ret)
  390. goto err_tcsr_mem_unmap;
  391. ret = ath10k_ahb_rst_ctrl_init(ar);
  392. if (ret)
  393. goto err_clock_deinit;
  394. ar_ahb->irq = platform_get_irq_byname(pdev, "legacy");
  395. if (ar_ahb->irq < 0) {
  396. ath10k_err(ar, "failed to get irq number: %d\n", ar_ahb->irq);
  397. ret = ar_ahb->irq;
  398. goto err_clock_deinit;
  399. }
  400. ath10k_dbg(ar, ATH10K_DBG_BOOT, "irq: %d\n", ar_ahb->irq);
  401. ath10k_dbg(ar, ATH10K_DBG_BOOT, "mem: 0x%pK mem_len: %lu gcc mem: 0x%pK tcsr_mem: 0x%pK\n",
  402. ar_ahb->mem, ar_ahb->mem_len,
  403. ar_ahb->gcc_mem, ar_ahb->tcsr_mem);
  404. return 0;
  405. err_clock_deinit:
  406. ath10k_ahb_clock_deinit(ar);
  407. err_tcsr_mem_unmap:
  408. iounmap(ar_ahb->tcsr_mem);
  409. err_gcc_mem_unmap:
  410. ar_ahb->tcsr_mem = NULL;
  411. iounmap(ar_ahb->gcc_mem);
  412. err_mem_unmap:
  413. ar_ahb->gcc_mem = NULL;
  414. devm_iounmap(&pdev->dev, ar_ahb->mem);
  415. out:
  416. ar_ahb->mem = NULL;
  417. return ret;
  418. }
  419. static void ath10k_ahb_resource_deinit(struct ath10k *ar)
  420. {
  421. struct ath10k_ahb *ar_ahb = ath10k_ahb_priv(ar);
  422. struct device *dev;
  423. dev = &ar_ahb->pdev->dev;
  424. if (ar_ahb->mem)
  425. devm_iounmap(dev, ar_ahb->mem);
  426. if (ar_ahb->gcc_mem)
  427. iounmap(ar_ahb->gcc_mem);
  428. if (ar_ahb->tcsr_mem)
  429. iounmap(ar_ahb->tcsr_mem);
  430. ar_ahb->mem = NULL;
  431. ar_ahb->gcc_mem = NULL;
  432. ar_ahb->tcsr_mem = NULL;
  433. ath10k_ahb_clock_deinit(ar);
  434. ath10k_ahb_rst_ctrl_deinit(ar);
  435. }
  436. static int ath10k_ahb_prepare_device(struct ath10k *ar)
  437. {
  438. u32 val;
  439. int ret;
  440. ret = ath10k_ahb_clock_enable(ar);
  441. if (ret) {
  442. ath10k_err(ar, "failed to enable clocks\n");
  443. return ret;
  444. }
  445. /* Clock for the target is supplied from outside of target (ie,
  446. * external clock module controlled by the host). Target needs
  447. * to know what frequency target cpu is configured which is needed
  448. * for target internal use. Read target cpu frequency info from
  449. * gcc register and write into target's scratch register where
  450. * target expects this information.
  451. */
  452. val = ath10k_ahb_gcc_read32(ar, ATH10K_AHB_GCC_FEPLL_PLL_DIV);
  453. ath10k_ahb_write32(ar, ATH10K_AHB_WIFI_SCRATCH_5_REG, val);
  454. ret = ath10k_ahb_release_reset(ar);
  455. if (ret)
  456. goto err_clk_disable;
  457. ath10k_ahb_irq_disable(ar);
  458. ath10k_ahb_write32(ar, FW_INDICATOR_ADDRESS, FW_IND_HOST_READY);
  459. ret = ath10k_pci_wait_for_target_init(ar);
  460. if (ret)
  461. goto err_halt_chip;
  462. return 0;
  463. err_halt_chip:
  464. ath10k_ahb_halt_chip(ar);
  465. err_clk_disable:
  466. ath10k_ahb_clock_disable(ar);
  467. return ret;
  468. }
  469. static int ath10k_ahb_chip_reset(struct ath10k *ar)
  470. {
  471. int ret;
  472. ath10k_ahb_halt_chip(ar);
  473. ath10k_ahb_clock_disable(ar);
  474. ret = ath10k_ahb_prepare_device(ar);
  475. if (ret)
  476. return ret;
  477. return 0;
  478. }
  479. static int ath10k_ahb_wake_target_cpu(struct ath10k *ar)
  480. {
  481. u32 addr, val;
  482. addr = SOC_CORE_BASE_ADDRESS | CORE_CTRL_ADDRESS;
  483. val = ath10k_ahb_read32(ar, addr);
  484. val |= ATH10K_AHB_CORE_CTRL_CPU_INTR_MASK;
  485. ath10k_ahb_write32(ar, addr, val);
  486. return 0;
  487. }
  488. static int ath10k_ahb_hif_start(struct ath10k *ar)
  489. {
  490. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot ahb hif start\n");
  491. ath10k_core_napi_enable(ar);
  492. ath10k_ce_enable_interrupts(ar);
  493. ath10k_pci_enable_legacy_irq(ar);
  494. ath10k_pci_rx_post(ar);
  495. return 0;
  496. }
  497. static void ath10k_ahb_hif_stop(struct ath10k *ar)
  498. {
  499. struct ath10k_ahb *ar_ahb = ath10k_ahb_priv(ar);
  500. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot ahb hif stop\n");
  501. ath10k_ahb_irq_disable(ar);
  502. synchronize_irq(ar_ahb->irq);
  503. ath10k_core_napi_sync_disable(ar);
  504. ath10k_pci_flush(ar);
  505. }
  506. static int ath10k_ahb_hif_power_up(struct ath10k *ar,
  507. enum ath10k_firmware_mode fw_mode)
  508. {
  509. int ret;
  510. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot ahb hif power up\n");
  511. ret = ath10k_ahb_chip_reset(ar);
  512. if (ret) {
  513. ath10k_err(ar, "failed to reset chip: %d\n", ret);
  514. goto out;
  515. }
  516. ret = ath10k_pci_init_pipes(ar);
  517. if (ret) {
  518. ath10k_err(ar, "failed to initialize CE: %d\n", ret);
  519. goto out;
  520. }
  521. ret = ath10k_pci_init_config(ar);
  522. if (ret) {
  523. ath10k_err(ar, "failed to setup init config: %d\n", ret);
  524. goto err_ce_deinit;
  525. }
  526. ret = ath10k_ahb_wake_target_cpu(ar);
  527. if (ret) {
  528. ath10k_err(ar, "could not wake up target CPU: %d\n", ret);
  529. goto err_ce_deinit;
  530. }
  531. return 0;
  532. err_ce_deinit:
  533. ath10k_pci_ce_deinit(ar);
  534. out:
  535. return ret;
  536. }
  537. static u32 ath10k_ahb_qca4019_targ_cpu_to_ce_addr(struct ath10k *ar, u32 addr)
  538. {
  539. u32 val = 0, region = addr & 0xfffff;
  540. val = ath10k_pci_read32(ar, PCIE_BAR_REG_ADDRESS);
  541. if (region >= QCA4019_SRAM_ADDR && region <=
  542. (QCA4019_SRAM_ADDR + QCA4019_SRAM_LEN)) {
  543. /* SRAM contents for QCA4019 can be directly accessed and
  544. * no conversions are required
  545. */
  546. val |= region;
  547. } else {
  548. val |= 0x100000 | region;
  549. }
  550. return val;
  551. }
  552. static const struct ath10k_hif_ops ath10k_ahb_hif_ops = {
  553. .tx_sg = ath10k_pci_hif_tx_sg,
  554. .diag_read = ath10k_pci_hif_diag_read,
  555. .diag_write = ath10k_pci_diag_write_mem,
  556. .exchange_bmi_msg = ath10k_pci_hif_exchange_bmi_msg,
  557. .start = ath10k_ahb_hif_start,
  558. .stop = ath10k_ahb_hif_stop,
  559. .map_service_to_pipe = ath10k_pci_hif_map_service_to_pipe,
  560. .get_default_pipe = ath10k_pci_hif_get_default_pipe,
  561. .send_complete_check = ath10k_pci_hif_send_complete_check,
  562. .get_free_queue_number = ath10k_pci_hif_get_free_queue_number,
  563. .power_up = ath10k_ahb_hif_power_up,
  564. .power_down = ath10k_pci_hif_power_down,
  565. .read32 = ath10k_ahb_read32,
  566. .write32 = ath10k_ahb_write32,
  567. };
  568. static const struct ath10k_bus_ops ath10k_ahb_bus_ops = {
  569. .read32 = ath10k_ahb_read32,
  570. .write32 = ath10k_ahb_write32,
  571. .get_num_banks = ath10k_ahb_get_num_banks,
  572. };
  573. static int ath10k_ahb_probe(struct platform_device *pdev)
  574. {
  575. struct ath10k *ar;
  576. struct ath10k_ahb *ar_ahb;
  577. struct ath10k_pci *ar_pci;
  578. enum ath10k_hw_rev hw_rev;
  579. size_t size;
  580. int ret;
  581. struct ath10k_bus_params bus_params = {};
  582. hw_rev = (enum ath10k_hw_rev)of_device_get_match_data(&pdev->dev);
  583. if (!hw_rev) {
  584. dev_err(&pdev->dev, "OF data missing\n");
  585. return -EINVAL;
  586. }
  587. size = sizeof(*ar_pci) + sizeof(*ar_ahb);
  588. ar = ath10k_core_create(size, &pdev->dev, ATH10K_BUS_AHB,
  589. hw_rev, &ath10k_ahb_hif_ops);
  590. if (!ar) {
  591. dev_err(&pdev->dev, "failed to allocate core\n");
  592. return -ENOMEM;
  593. }
  594. ath10k_dbg(ar, ATH10K_DBG_BOOT, "ahb probe\n");
  595. ar_pci = ath10k_pci_priv(ar);
  596. ar_ahb = ath10k_ahb_priv(ar);
  597. ar_ahb->pdev = pdev;
  598. platform_set_drvdata(pdev, ar);
  599. ret = ath10k_ahb_resource_init(ar);
  600. if (ret)
  601. goto err_core_destroy;
  602. ar->dev_id = 0;
  603. ar_pci->mem = ar_ahb->mem;
  604. ar_pci->mem_len = ar_ahb->mem_len;
  605. ar_pci->ar = ar;
  606. ar_pci->ce.bus_ops = &ath10k_ahb_bus_ops;
  607. ar_pci->targ_cpu_to_ce_addr = ath10k_ahb_qca4019_targ_cpu_to_ce_addr;
  608. ar->ce_priv = &ar_pci->ce;
  609. ret = ath10k_pci_setup_resource(ar);
  610. if (ret) {
  611. ath10k_err(ar, "failed to setup resource: %d\n", ret);
  612. goto err_resource_deinit;
  613. }
  614. ath10k_pci_init_napi(ar);
  615. ret = ath10k_ahb_request_irq_legacy(ar);
  616. if (ret)
  617. goto err_free_pipes;
  618. ret = ath10k_ahb_prepare_device(ar);
  619. if (ret)
  620. goto err_free_irq;
  621. ath10k_pci_ce_deinit(ar);
  622. bus_params.dev_type = ATH10K_DEV_TYPE_LL;
  623. bus_params.chip_id = ath10k_ahb_soc_read32(ar, SOC_CHIP_ID_ADDRESS);
  624. if (bus_params.chip_id == 0xffffffff) {
  625. ath10k_err(ar, "failed to get chip id\n");
  626. ret = -ENODEV;
  627. goto err_halt_device;
  628. }
  629. ret = ath10k_core_register(ar, &bus_params);
  630. if (ret) {
  631. ath10k_err(ar, "failed to register driver core: %d\n", ret);
  632. goto err_halt_device;
  633. }
  634. return 0;
  635. err_halt_device:
  636. ath10k_ahb_halt_chip(ar);
  637. ath10k_ahb_clock_disable(ar);
  638. err_free_irq:
  639. ath10k_ahb_release_irq_legacy(ar);
  640. err_free_pipes:
  641. ath10k_pci_release_resource(ar);
  642. err_resource_deinit:
  643. ath10k_ahb_resource_deinit(ar);
  644. err_core_destroy:
  645. ath10k_core_destroy(ar);
  646. platform_set_drvdata(pdev, NULL);
  647. return ret;
  648. }
  649. static int ath10k_ahb_remove(struct platform_device *pdev)
  650. {
  651. struct ath10k *ar = platform_get_drvdata(pdev);
  652. struct ath10k_ahb *ar_ahb;
  653. if (!ar)
  654. return -EINVAL;
  655. ar_ahb = ath10k_ahb_priv(ar);
  656. if (!ar_ahb)
  657. return -EINVAL;
  658. ath10k_dbg(ar, ATH10K_DBG_AHB, "ahb remove\n");
  659. ath10k_core_unregister(ar);
  660. ath10k_ahb_irq_disable(ar);
  661. ath10k_ahb_release_irq_legacy(ar);
  662. ath10k_pci_release_resource(ar);
  663. ath10k_ahb_halt_chip(ar);
  664. ath10k_ahb_clock_disable(ar);
  665. ath10k_ahb_resource_deinit(ar);
  666. ath10k_core_destroy(ar);
  667. platform_set_drvdata(pdev, NULL);
  668. return 0;
  669. }
  670. static struct platform_driver ath10k_ahb_driver = {
  671. .driver = {
  672. .name = "ath10k_ahb",
  673. .of_match_table = ath10k_ahb_of_match,
  674. },
  675. .probe = ath10k_ahb_probe,
  676. .remove = ath10k_ahb_remove,
  677. };
  678. int ath10k_ahb_init(void)
  679. {
  680. int ret;
  681. ret = platform_driver_register(&ath10k_ahb_driver);
  682. if (ret)
  683. printk(KERN_ERR "failed to register ath10k ahb driver: %d\n",
  684. ret);
  685. return ret;
  686. }
  687. void ath10k_ahb_exit(void)
  688. {
  689. platform_driver_unregister(&ath10k_ahb_driver);
  690. }