skl-sst-dsp.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. * skl-sst-dsp.c - SKL SST library generic function
  3. *
  4. * Copyright (C) 2014-15, Intel Corporation.
  5. * Author:Rafal Redzimski <rafal.f.redzimski@intel.com>
  6. * Jeeja KP <jeeja.kp@intel.com>
  7. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as version 2, as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. */
  18. #include <sound/pcm.h>
  19. #include "../common/sst-dsp.h"
  20. #include "../common/sst-ipc.h"
  21. #include "../common/sst-dsp-priv.h"
  22. #include "skl-sst-ipc.h"
  23. /* various timeout values */
  24. #define SKL_DSP_PU_TO 50
  25. #define SKL_DSP_PD_TO 50
  26. #define SKL_DSP_RESET_TO 50
  27. void skl_dsp_set_state_locked(struct sst_dsp *ctx, int state)
  28. {
  29. mutex_lock(&ctx->mutex);
  30. ctx->sst_state = state;
  31. mutex_unlock(&ctx->mutex);
  32. }
  33. static int skl_dsp_core_set_reset_state(struct sst_dsp *ctx)
  34. {
  35. int ret;
  36. /* update bits */
  37. sst_dsp_shim_update_bits_unlocked(ctx,
  38. SKL_ADSP_REG_ADSPCS, SKL_ADSPCS_CRST_MASK,
  39. SKL_ADSPCS_CRST(SKL_DSP_CORES_MASK));
  40. /* poll with timeout to check if operation successful */
  41. ret = sst_dsp_register_poll(ctx,
  42. SKL_ADSP_REG_ADSPCS,
  43. SKL_ADSPCS_CRST_MASK,
  44. SKL_ADSPCS_CRST(SKL_DSP_CORES_MASK),
  45. SKL_DSP_RESET_TO,
  46. "Set reset");
  47. if ((sst_dsp_shim_read_unlocked(ctx, SKL_ADSP_REG_ADSPCS) &
  48. SKL_ADSPCS_CRST(SKL_DSP_CORES_MASK)) !=
  49. SKL_ADSPCS_CRST(SKL_DSP_CORES_MASK)) {
  50. dev_err(ctx->dev, "Set reset state failed\n");
  51. ret = -EIO;
  52. }
  53. return ret;
  54. }
  55. static int skl_dsp_core_unset_reset_state(struct sst_dsp *ctx)
  56. {
  57. int ret;
  58. dev_dbg(ctx->dev, "In %s\n", __func__);
  59. /* update bits */
  60. sst_dsp_shim_update_bits_unlocked(ctx, SKL_ADSP_REG_ADSPCS,
  61. SKL_ADSPCS_CRST_MASK, 0);
  62. /* poll with timeout to check if operation successful */
  63. ret = sst_dsp_register_poll(ctx,
  64. SKL_ADSP_REG_ADSPCS,
  65. SKL_ADSPCS_CRST_MASK,
  66. 0,
  67. SKL_DSP_RESET_TO,
  68. "Unset reset");
  69. if ((sst_dsp_shim_read_unlocked(ctx, SKL_ADSP_REG_ADSPCS) &
  70. SKL_ADSPCS_CRST(SKL_DSP_CORES_MASK)) != 0) {
  71. dev_err(ctx->dev, "Unset reset state failed\n");
  72. ret = -EIO;
  73. }
  74. return ret;
  75. }
  76. static bool is_skl_dsp_core_enable(struct sst_dsp *ctx)
  77. {
  78. int val;
  79. bool is_enable;
  80. val = sst_dsp_shim_read_unlocked(ctx, SKL_ADSP_REG_ADSPCS);
  81. is_enable = ((val & SKL_ADSPCS_CPA(SKL_DSP_CORES_MASK)) &&
  82. (val & SKL_ADSPCS_SPA(SKL_DSP_CORES_MASK)) &&
  83. !(val & SKL_ADSPCS_CRST(SKL_DSP_CORES_MASK)) &&
  84. !(val & SKL_ADSPCS_CSTALL(SKL_DSP_CORES_MASK)));
  85. dev_dbg(ctx->dev, "DSP core is enabled=%d\n", is_enable);
  86. return is_enable;
  87. }
  88. static int skl_dsp_reset_core(struct sst_dsp *ctx)
  89. {
  90. /* stall core */
  91. sst_dsp_shim_write_unlocked(ctx, SKL_ADSP_REG_ADSPCS,
  92. sst_dsp_shim_read_unlocked(ctx, SKL_ADSP_REG_ADSPCS) &
  93. SKL_ADSPCS_CSTALL(SKL_DSP_CORES_MASK));
  94. /* set reset state */
  95. return skl_dsp_core_set_reset_state(ctx);
  96. }
  97. static int skl_dsp_start_core(struct sst_dsp *ctx)
  98. {
  99. int ret;
  100. /* unset reset state */
  101. ret = skl_dsp_core_unset_reset_state(ctx);
  102. if (ret < 0) {
  103. dev_dbg(ctx->dev, "dsp unset reset fails\n");
  104. return ret;
  105. }
  106. /* run core */
  107. dev_dbg(ctx->dev, "run core...\n");
  108. sst_dsp_shim_write_unlocked(ctx, SKL_ADSP_REG_ADSPCS,
  109. sst_dsp_shim_read_unlocked(ctx, SKL_ADSP_REG_ADSPCS) &
  110. ~SKL_ADSPCS_CSTALL(SKL_DSP_CORES_MASK));
  111. if (!is_skl_dsp_core_enable(ctx)) {
  112. skl_dsp_reset_core(ctx);
  113. dev_err(ctx->dev, "DSP core enable failed\n");
  114. ret = -EIO;
  115. }
  116. return ret;
  117. }
  118. static int skl_dsp_core_power_up(struct sst_dsp *ctx)
  119. {
  120. int ret;
  121. /* update bits */
  122. sst_dsp_shim_update_bits_unlocked(ctx, SKL_ADSP_REG_ADSPCS,
  123. SKL_ADSPCS_SPA_MASK, SKL_ADSPCS_SPA(SKL_DSP_CORES_MASK));
  124. /* poll with timeout to check if operation successful */
  125. ret = sst_dsp_register_poll(ctx,
  126. SKL_ADSP_REG_ADSPCS,
  127. SKL_ADSPCS_CPA_MASK,
  128. SKL_ADSPCS_CPA(SKL_DSP_CORES_MASK),
  129. SKL_DSP_PU_TO,
  130. "Power up");
  131. if ((sst_dsp_shim_read_unlocked(ctx, SKL_ADSP_REG_ADSPCS) &
  132. SKL_ADSPCS_CPA(SKL_DSP_CORES_MASK)) !=
  133. SKL_ADSPCS_CPA(SKL_DSP_CORES_MASK)) {
  134. dev_err(ctx->dev, "DSP core power up failed\n");
  135. ret = -EIO;
  136. }
  137. return ret;
  138. }
  139. static int skl_dsp_core_power_down(struct sst_dsp *ctx)
  140. {
  141. /* update bits */
  142. sst_dsp_shim_update_bits_unlocked(ctx, SKL_ADSP_REG_ADSPCS,
  143. SKL_ADSPCS_SPA_MASK, 0);
  144. /* poll with timeout to check if operation successful */
  145. return sst_dsp_register_poll(ctx,
  146. SKL_ADSP_REG_ADSPCS,
  147. SKL_ADSPCS_CPA_MASK,
  148. 0,
  149. SKL_DSP_PD_TO,
  150. "Power down");
  151. }
  152. static int skl_dsp_enable_core(struct sst_dsp *ctx)
  153. {
  154. int ret;
  155. /* power up */
  156. ret = skl_dsp_core_power_up(ctx);
  157. if (ret < 0) {
  158. dev_dbg(ctx->dev, "dsp core power up failed\n");
  159. return ret;
  160. }
  161. return skl_dsp_start_core(ctx);
  162. }
  163. int skl_dsp_disable_core(struct sst_dsp *ctx)
  164. {
  165. int ret;
  166. ret = skl_dsp_reset_core(ctx);
  167. if (ret < 0) {
  168. dev_err(ctx->dev, "dsp core reset failed\n");
  169. return ret;
  170. }
  171. /* power down core*/
  172. ret = skl_dsp_core_power_down(ctx);
  173. if (ret < 0) {
  174. dev_err(ctx->dev, "dsp core power down failed\n");
  175. return ret;
  176. }
  177. if (is_skl_dsp_core_enable(ctx)) {
  178. dev_err(ctx->dev, "DSP core disable failed\n");
  179. ret = -EIO;
  180. }
  181. return ret;
  182. }
  183. int skl_dsp_boot(struct sst_dsp *ctx)
  184. {
  185. int ret;
  186. if (is_skl_dsp_core_enable(ctx)) {
  187. dev_dbg(ctx->dev, "dsp core is already enabled, so reset the dap core\n");
  188. ret = skl_dsp_reset_core(ctx);
  189. if (ret < 0) {
  190. dev_err(ctx->dev, "dsp reset failed\n");
  191. return ret;
  192. }
  193. ret = skl_dsp_start_core(ctx);
  194. if (ret < 0) {
  195. dev_err(ctx->dev, "dsp start failed\n");
  196. return ret;
  197. }
  198. } else {
  199. dev_dbg(ctx->dev, "disable and enable to make sure DSP is invalid state\n");
  200. ret = skl_dsp_disable_core(ctx);
  201. if (ret < 0) {
  202. dev_err(ctx->dev, "dsp disable core failes\n");
  203. return ret;
  204. }
  205. ret = skl_dsp_enable_core(ctx);
  206. }
  207. return ret;
  208. }
  209. irqreturn_t skl_dsp_sst_interrupt(int irq, void *dev_id)
  210. {
  211. struct sst_dsp *ctx = dev_id;
  212. u32 val;
  213. irqreturn_t result = IRQ_NONE;
  214. spin_lock(&ctx->spinlock);
  215. val = sst_dsp_shim_read_unlocked(ctx, SKL_ADSP_REG_ADSPIS);
  216. ctx->intr_status = val;
  217. if (val == 0xffffffff) {
  218. spin_unlock(&ctx->spinlock);
  219. return IRQ_NONE;
  220. }
  221. if (val & SKL_ADSPIS_IPC) {
  222. skl_ipc_int_disable(ctx);
  223. result = IRQ_WAKE_THREAD;
  224. }
  225. if (val & SKL_ADSPIS_CL_DMA) {
  226. skl_cldma_int_disable(ctx);
  227. result = IRQ_WAKE_THREAD;
  228. }
  229. spin_unlock(&ctx->spinlock);
  230. return result;
  231. }
  232. int skl_dsp_wake(struct sst_dsp *ctx)
  233. {
  234. return ctx->fw_ops.set_state_D0(ctx);
  235. }
  236. EXPORT_SYMBOL_GPL(skl_dsp_wake);
  237. int skl_dsp_sleep(struct sst_dsp *ctx)
  238. {
  239. return ctx->fw_ops.set_state_D3(ctx);
  240. }
  241. EXPORT_SYMBOL_GPL(skl_dsp_sleep);
  242. struct sst_dsp *skl_dsp_ctx_init(struct device *dev,
  243. struct sst_dsp_device *sst_dev, int irq)
  244. {
  245. int ret;
  246. struct sst_dsp *sst;
  247. sst = devm_kzalloc(dev, sizeof(*sst), GFP_KERNEL);
  248. if (sst == NULL)
  249. return NULL;
  250. spin_lock_init(&sst->spinlock);
  251. mutex_init(&sst->mutex);
  252. sst->dev = dev;
  253. sst->sst_dev = sst_dev;
  254. sst->irq = irq;
  255. sst->ops = sst_dev->ops;
  256. sst->thread_context = sst_dev->thread_context;
  257. /* Initialise SST Audio DSP */
  258. if (sst->ops->init) {
  259. ret = sst->ops->init(sst, NULL);
  260. if (ret < 0)
  261. return NULL;
  262. }
  263. /* Register the ISR */
  264. ret = request_threaded_irq(sst->irq, sst->ops->irq_handler,
  265. sst_dev->thread, IRQF_SHARED, "AudioDSP", sst);
  266. if (ret) {
  267. dev_err(sst->dev, "unable to grab threaded IRQ %d, disabling device\n",
  268. sst->irq);
  269. return NULL;
  270. }
  271. return sst;
  272. }
  273. void skl_dsp_free(struct sst_dsp *dsp)
  274. {
  275. skl_ipc_int_disable(dsp);
  276. free_irq(dsp->irq, dsp);
  277. skl_dsp_disable_core(dsp);
  278. }
  279. EXPORT_SYMBOL_GPL(skl_dsp_free);
  280. bool is_skl_dsp_running(struct sst_dsp *ctx)
  281. {
  282. return (ctx->sst_state == SKL_DSP_RUNNING);
  283. }
  284. EXPORT_SYMBOL_GPL(is_skl_dsp_running);