hda-ctrl.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
  2. //
  3. // This file is provided under a dual BSD/GPLv2 license. When using or
  4. // redistributing this file, you may do so under either license.
  5. //
  6. // Copyright(c) 2018 Intel Corporation
  7. //
  8. // Authors: Liam Girdwood <liam.r.girdwood@linux.intel.com>
  9. // Ranjani Sridharan <ranjani.sridharan@linux.intel.com>
  10. // Rander Wang <rander.wang@intel.com>
  11. // Keyon Jie <yang.jie@linux.intel.com>
  12. //
  13. /*
  14. * Hardware interface for generic Intel audio DSP HDA IP
  15. */
  16. #include <linux/module.h>
  17. #include <sound/hdaudio_ext.h>
  18. #include <sound/hda_register.h>
  19. #include <sound/hda_component.h>
  20. #include <sound/hda-mlink.h>
  21. #include "../ops.h"
  22. #include "hda.h"
  23. /*
  24. * HDA Operations.
  25. */
  26. int hda_dsp_ctrl_link_reset(struct snd_sof_dev *sdev, bool reset)
  27. {
  28. unsigned long timeout;
  29. u32 gctl = 0;
  30. u32 val;
  31. /* 0 to enter reset and 1 to exit reset */
  32. val = reset ? 0 : SOF_HDA_GCTL_RESET;
  33. /* enter/exit HDA controller reset */
  34. snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR, SOF_HDA_GCTL,
  35. SOF_HDA_GCTL_RESET, val);
  36. /* wait to enter/exit reset */
  37. timeout = jiffies + msecs_to_jiffies(HDA_DSP_CTRL_RESET_TIMEOUT);
  38. while (time_before(jiffies, timeout)) {
  39. gctl = snd_sof_dsp_read(sdev, HDA_DSP_HDA_BAR, SOF_HDA_GCTL);
  40. if ((gctl & SOF_HDA_GCTL_RESET) == val)
  41. return 0;
  42. usleep_range(500, 1000);
  43. }
  44. /* enter/exit reset failed */
  45. dev_err(sdev->dev, "error: failed to %s HDA controller gctl 0x%x\n",
  46. reset ? "reset" : "ready", gctl);
  47. return -EIO;
  48. }
  49. int hda_dsp_ctrl_get_caps(struct snd_sof_dev *sdev)
  50. {
  51. struct hdac_bus *bus = sof_to_bus(sdev);
  52. u32 cap, offset, feature;
  53. int count = 0;
  54. int ret;
  55. /*
  56. * On some devices, one reset cycle is necessary before reading
  57. * capabilities
  58. */
  59. ret = hda_dsp_ctrl_link_reset(sdev, true);
  60. if (ret < 0)
  61. return ret;
  62. ret = hda_dsp_ctrl_link_reset(sdev, false);
  63. if (ret < 0)
  64. return ret;
  65. offset = snd_sof_dsp_read(sdev, HDA_DSP_HDA_BAR, SOF_HDA_LLCH);
  66. do {
  67. dev_dbg(sdev->dev, "checking for capabilities at offset 0x%x\n",
  68. offset & SOF_HDA_CAP_NEXT_MASK);
  69. cap = snd_sof_dsp_read(sdev, HDA_DSP_HDA_BAR, offset);
  70. if (cap == -1) {
  71. dev_dbg(bus->dev, "Invalid capability reg read\n");
  72. break;
  73. }
  74. feature = (cap & SOF_HDA_CAP_ID_MASK) >> SOF_HDA_CAP_ID_OFF;
  75. switch (feature) {
  76. case SOF_HDA_PP_CAP_ID:
  77. dev_dbg(sdev->dev, "found DSP capability at 0x%x\n",
  78. offset);
  79. bus->ppcap = bus->remap_addr + offset;
  80. sdev->bar[HDA_DSP_PP_BAR] = bus->ppcap;
  81. break;
  82. case SOF_HDA_SPIB_CAP_ID:
  83. dev_dbg(sdev->dev, "found SPIB capability at 0x%x\n",
  84. offset);
  85. bus->spbcap = bus->remap_addr + offset;
  86. sdev->bar[HDA_DSP_SPIB_BAR] = bus->spbcap;
  87. break;
  88. case SOF_HDA_DRSM_CAP_ID:
  89. dev_dbg(sdev->dev, "found DRSM capability at 0x%x\n",
  90. offset);
  91. bus->drsmcap = bus->remap_addr + offset;
  92. sdev->bar[HDA_DSP_DRSM_BAR] = bus->drsmcap;
  93. break;
  94. case SOF_HDA_GTS_CAP_ID:
  95. dev_dbg(sdev->dev, "found GTS capability at 0x%x\n",
  96. offset);
  97. bus->gtscap = bus->remap_addr + offset;
  98. break;
  99. case SOF_HDA_ML_CAP_ID:
  100. dev_dbg(sdev->dev, "found ML capability at 0x%x\n",
  101. offset);
  102. bus->mlcap = bus->remap_addr + offset;
  103. break;
  104. default:
  105. dev_dbg(sdev->dev, "found capability %d at 0x%x\n",
  106. feature, offset);
  107. break;
  108. }
  109. offset = cap & SOF_HDA_CAP_NEXT_MASK;
  110. } while (count++ <= SOF_HDA_MAX_CAPS && offset);
  111. return 0;
  112. }
  113. EXPORT_SYMBOL_NS(hda_dsp_ctrl_get_caps, SND_SOC_SOF_INTEL_HDA_COMMON);
  114. void hda_dsp_ctrl_ppcap_enable(struct snd_sof_dev *sdev, bool enable)
  115. {
  116. u32 val = enable ? SOF_HDA_PPCTL_GPROCEN : 0;
  117. snd_sof_dsp_update_bits(sdev, HDA_DSP_PP_BAR, SOF_HDA_REG_PP_PPCTL,
  118. SOF_HDA_PPCTL_GPROCEN, val);
  119. }
  120. EXPORT_SYMBOL_NS(hda_dsp_ctrl_ppcap_enable, SND_SOC_SOF_INTEL_HDA_COMMON);
  121. void hda_dsp_ctrl_ppcap_int_enable(struct snd_sof_dev *sdev, bool enable)
  122. {
  123. u32 val = enable ? SOF_HDA_PPCTL_PIE : 0;
  124. snd_sof_dsp_update_bits(sdev, HDA_DSP_PP_BAR, SOF_HDA_REG_PP_PPCTL,
  125. SOF_HDA_PPCTL_PIE, val);
  126. }
  127. EXPORT_SYMBOL_NS(hda_dsp_ctrl_ppcap_int_enable, SND_SOC_SOF_INTEL_HDA_COMMON);
  128. void hda_dsp_ctrl_misc_clock_gating(struct snd_sof_dev *sdev, bool enable)
  129. {
  130. u32 val = enable ? PCI_CGCTL_MISCBDCGE_MASK : 0;
  131. snd_sof_pci_update_bits(sdev, PCI_CGCTL, PCI_CGCTL_MISCBDCGE_MASK, val);
  132. }
  133. /*
  134. * enable/disable audio dsp clock gating and power gating bits.
  135. * This allows the HW to opportunistically power and clock gate
  136. * the audio dsp when it is idle
  137. */
  138. int hda_dsp_ctrl_clock_power_gating(struct snd_sof_dev *sdev, bool enable)
  139. {
  140. struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
  141. u32 val;
  142. /* enable/disable audio dsp clock gating */
  143. val = enable ? PCI_CGCTL_ADSPDCGE : 0;
  144. snd_sof_pci_update_bits(sdev, PCI_CGCTL, PCI_CGCTL_ADSPDCGE, val);
  145. /* disable the DMI link when requested. But enable only if it wasn't disabled previously */
  146. val = enable ? HDA_VS_INTEL_EM2_L1SEN : 0;
  147. if (!enable || !hda->l1_disabled)
  148. snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR, HDA_VS_INTEL_EM2,
  149. HDA_VS_INTEL_EM2_L1SEN, val);
  150. /* enable/disable audio dsp power gating */
  151. val = enable ? 0 : PCI_PGCTL_ADSPPGD;
  152. snd_sof_pci_update_bits(sdev, PCI_PGCTL, PCI_PGCTL_ADSPPGD, val);
  153. return 0;
  154. }
  155. EXPORT_SYMBOL_NS(hda_dsp_ctrl_clock_power_gating, SND_SOC_SOF_INTEL_HDA_COMMON);
  156. int hda_dsp_ctrl_init_chip(struct snd_sof_dev *sdev)
  157. {
  158. struct hdac_bus *bus = sof_to_bus(sdev);
  159. struct hdac_stream *stream;
  160. int sd_offset, ret = 0;
  161. u32 gctl;
  162. if (bus->chip_init)
  163. return 0;
  164. hda_codec_set_codec_wakeup(sdev, true);
  165. hda_dsp_ctrl_misc_clock_gating(sdev, false);
  166. /* clear WAKE_STS if not in reset */
  167. gctl = snd_sof_dsp_read(sdev, HDA_DSP_HDA_BAR, SOF_HDA_GCTL);
  168. if (gctl & SOF_HDA_GCTL_RESET)
  169. snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR,
  170. SOF_HDA_WAKESTS, SOF_HDA_WAKESTS_INT_MASK);
  171. /* reset HDA controller */
  172. ret = hda_dsp_ctrl_link_reset(sdev, true);
  173. if (ret < 0) {
  174. dev_err(sdev->dev, "error: failed to reset HDA controller\n");
  175. goto err;
  176. }
  177. usleep_range(500, 1000);
  178. /* exit HDA controller reset */
  179. ret = hda_dsp_ctrl_link_reset(sdev, false);
  180. if (ret < 0) {
  181. dev_err(sdev->dev, "error: failed to exit HDA controller reset\n");
  182. goto err;
  183. }
  184. usleep_range(1000, 1200);
  185. hda_codec_detect_mask(sdev);
  186. /* clear stream status */
  187. list_for_each_entry(stream, &bus->stream_list, list) {
  188. sd_offset = SOF_STREAM_SD_OFFSET(stream);
  189. snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR,
  190. sd_offset + SOF_HDA_ADSP_REG_SD_STS,
  191. SOF_HDA_CL_DMA_SD_INT_MASK);
  192. }
  193. /* clear WAKESTS */
  194. snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR, SOF_HDA_WAKESTS,
  195. bus->codec_mask);
  196. hda_codec_rirb_status_clear(sdev);
  197. /* clear interrupt status register */
  198. snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTSTS,
  199. SOF_HDA_INT_CTRL_EN | SOF_HDA_INT_ALL_STREAM);
  200. hda_codec_init_cmd_io(sdev);
  201. /* enable CIE and GIE interrupts */
  202. snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTCTL,
  203. SOF_HDA_INT_CTRL_EN | SOF_HDA_INT_GLOBAL_EN,
  204. SOF_HDA_INT_CTRL_EN | SOF_HDA_INT_GLOBAL_EN);
  205. /* program the position buffer */
  206. if (bus->use_posbuf && bus->posbuf.addr) {
  207. snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR, SOF_HDA_ADSP_DPLBASE,
  208. (u32)bus->posbuf.addr);
  209. snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR, SOF_HDA_ADSP_DPUBASE,
  210. upper_32_bits(bus->posbuf.addr));
  211. }
  212. hda_bus_ml_reset_losidv(bus);
  213. bus->chip_init = true;
  214. err:
  215. hda_dsp_ctrl_misc_clock_gating(sdev, true);
  216. hda_codec_set_codec_wakeup(sdev, false);
  217. return ret;
  218. }
  219. EXPORT_SYMBOL_NS(hda_dsp_ctrl_init_chip, SND_SOC_SOF_INTEL_HDA_COMMON);
  220. void hda_dsp_ctrl_stop_chip(struct snd_sof_dev *sdev)
  221. {
  222. struct hdac_bus *bus = sof_to_bus(sdev);
  223. struct hdac_stream *stream;
  224. int sd_offset;
  225. if (!bus->chip_init)
  226. return;
  227. /* disable interrupts in stream descriptor */
  228. list_for_each_entry(stream, &bus->stream_list, list) {
  229. sd_offset = SOF_STREAM_SD_OFFSET(stream);
  230. snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR,
  231. sd_offset +
  232. SOF_HDA_ADSP_REG_SD_CTL,
  233. SOF_HDA_CL_DMA_SD_INT_MASK,
  234. 0);
  235. }
  236. /* disable SIE for all streams */
  237. snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTCTL,
  238. SOF_HDA_INT_ALL_STREAM, 0);
  239. /* disable controller CIE and GIE */
  240. snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTCTL,
  241. SOF_HDA_INT_CTRL_EN | SOF_HDA_INT_GLOBAL_EN,
  242. 0);
  243. /* clear stream status */
  244. list_for_each_entry(stream, &bus->stream_list, list) {
  245. sd_offset = SOF_STREAM_SD_OFFSET(stream);
  246. snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR,
  247. sd_offset + SOF_HDA_ADSP_REG_SD_STS,
  248. SOF_HDA_CL_DMA_SD_INT_MASK);
  249. }
  250. /* clear WAKESTS */
  251. snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR, SOF_HDA_WAKESTS,
  252. SOF_HDA_WAKESTS_INT_MASK);
  253. hda_codec_rirb_status_clear(sdev);
  254. /* clear interrupt status register */
  255. snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTSTS,
  256. SOF_HDA_INT_CTRL_EN | SOF_HDA_INT_ALL_STREAM);
  257. hda_codec_stop_cmd_io(sdev);
  258. /* disable position buffer */
  259. if (bus->use_posbuf && bus->posbuf.addr) {
  260. snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR,
  261. SOF_HDA_ADSP_DPLBASE, 0);
  262. snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR,
  263. SOF_HDA_ADSP_DPUBASE, 0);
  264. }
  265. bus->chip_init = false;
  266. }
  267. MODULE_LICENSE("Dual BSD/GPL");
  268. MODULE_DESCRIPTION("SOF helpers for HDaudio platforms");
  269. MODULE_IMPORT_NS(SND_SOC_SOF_HDA_MLINK);
  270. MODULE_IMPORT_NS(SND_SOC_SOF_HDA_AUDIO_CODEC);
  271. MODULE_IMPORT_NS(SND_SOC_SOF_HDA_AUDIO_CODEC_I915);