hda-ctrl.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. All rights reserved.
  7. //
  8. // Authors: Liam Girdwood <[email protected]>
  9. // Ranjani Sridharan <[email protected]>
  10. // Rander Wang <[email protected]>
  11. // Keyon Jie <[email protected]>
  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 "../ops.h"
  21. #include "hda.h"
  22. #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
  23. static int hda_codec_mask = -1;
  24. module_param_named(codec_mask, hda_codec_mask, int, 0444);
  25. MODULE_PARM_DESC(codec_mask, "SOF HDA codec mask for probing");
  26. #endif
  27. /*
  28. * HDA Operations.
  29. */
  30. int hda_dsp_ctrl_link_reset(struct snd_sof_dev *sdev, bool reset)
  31. {
  32. unsigned long timeout;
  33. u32 gctl = 0;
  34. u32 val;
  35. /* 0 to enter reset and 1 to exit reset */
  36. val = reset ? 0 : SOF_HDA_GCTL_RESET;
  37. /* enter/exit HDA controller reset */
  38. snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR, SOF_HDA_GCTL,
  39. SOF_HDA_GCTL_RESET, val);
  40. /* wait to enter/exit reset */
  41. timeout = jiffies + msecs_to_jiffies(HDA_DSP_CTRL_RESET_TIMEOUT);
  42. while (time_before(jiffies, timeout)) {
  43. gctl = snd_sof_dsp_read(sdev, HDA_DSP_HDA_BAR, SOF_HDA_GCTL);
  44. if ((gctl & SOF_HDA_GCTL_RESET) == val)
  45. return 0;
  46. usleep_range(500, 1000);
  47. }
  48. /* enter/exit reset failed */
  49. dev_err(sdev->dev, "error: failed to %s HDA controller gctl 0x%x\n",
  50. reset ? "reset" : "ready", gctl);
  51. return -EIO;
  52. }
  53. int hda_dsp_ctrl_get_caps(struct snd_sof_dev *sdev)
  54. {
  55. struct hdac_bus *bus = sof_to_bus(sdev);
  56. u32 cap, offset, feature;
  57. int count = 0;
  58. int ret;
  59. /*
  60. * On some devices, one reset cycle is necessary before reading
  61. * capabilities
  62. */
  63. ret = hda_dsp_ctrl_link_reset(sdev, true);
  64. if (ret < 0)
  65. return ret;
  66. ret = hda_dsp_ctrl_link_reset(sdev, false);
  67. if (ret < 0)
  68. return ret;
  69. offset = snd_sof_dsp_read(sdev, HDA_DSP_HDA_BAR, SOF_HDA_LLCH);
  70. do {
  71. dev_dbg(sdev->dev, "checking for capabilities at offset 0x%x\n",
  72. offset & SOF_HDA_CAP_NEXT_MASK);
  73. cap = snd_sof_dsp_read(sdev, HDA_DSP_HDA_BAR, offset);
  74. if (cap == -1) {
  75. dev_dbg(bus->dev, "Invalid capability reg read\n");
  76. break;
  77. }
  78. feature = (cap & SOF_HDA_CAP_ID_MASK) >> SOF_HDA_CAP_ID_OFF;
  79. switch (feature) {
  80. case SOF_HDA_PP_CAP_ID:
  81. dev_dbg(sdev->dev, "found DSP capability at 0x%x\n",
  82. offset);
  83. bus->ppcap = bus->remap_addr + offset;
  84. sdev->bar[HDA_DSP_PP_BAR] = bus->ppcap;
  85. break;
  86. case SOF_HDA_SPIB_CAP_ID:
  87. dev_dbg(sdev->dev, "found SPIB capability at 0x%x\n",
  88. offset);
  89. bus->spbcap = bus->remap_addr + offset;
  90. sdev->bar[HDA_DSP_SPIB_BAR] = bus->spbcap;
  91. break;
  92. case SOF_HDA_DRSM_CAP_ID:
  93. dev_dbg(sdev->dev, "found DRSM capability at 0x%x\n",
  94. offset);
  95. bus->drsmcap = bus->remap_addr + offset;
  96. sdev->bar[HDA_DSP_DRSM_BAR] = bus->drsmcap;
  97. break;
  98. case SOF_HDA_GTS_CAP_ID:
  99. dev_dbg(sdev->dev, "found GTS capability at 0x%x\n",
  100. offset);
  101. bus->gtscap = bus->remap_addr + offset;
  102. break;
  103. case SOF_HDA_ML_CAP_ID:
  104. dev_dbg(sdev->dev, "found ML capability at 0x%x\n",
  105. offset);
  106. bus->mlcap = bus->remap_addr + offset;
  107. break;
  108. default:
  109. dev_dbg(sdev->dev, "found capability %d at 0x%x\n",
  110. feature, offset);
  111. break;
  112. }
  113. offset = cap & SOF_HDA_CAP_NEXT_MASK;
  114. } while (count++ <= SOF_HDA_MAX_CAPS && offset);
  115. return 0;
  116. }
  117. void hda_dsp_ctrl_ppcap_enable(struct snd_sof_dev *sdev, bool enable)
  118. {
  119. u32 val = enable ? SOF_HDA_PPCTL_GPROCEN : 0;
  120. snd_sof_dsp_update_bits(sdev, HDA_DSP_PP_BAR, SOF_HDA_REG_PP_PPCTL,
  121. SOF_HDA_PPCTL_GPROCEN, val);
  122. }
  123. void hda_dsp_ctrl_ppcap_int_enable(struct snd_sof_dev *sdev, bool enable)
  124. {
  125. u32 val = enable ? SOF_HDA_PPCTL_PIE : 0;
  126. snd_sof_dsp_update_bits(sdev, HDA_DSP_PP_BAR, SOF_HDA_REG_PP_PPCTL,
  127. SOF_HDA_PPCTL_PIE, val);
  128. }
  129. void hda_dsp_ctrl_misc_clock_gating(struct snd_sof_dev *sdev, bool enable)
  130. {
  131. u32 val = enable ? PCI_CGCTL_MISCBDCGE_MASK : 0;
  132. snd_sof_pci_update_bits(sdev, PCI_CGCTL, PCI_CGCTL_MISCBDCGE_MASK, val);
  133. }
  134. /*
  135. * enable/disable audio dsp clock gating and power gating bits.
  136. * This allows the HW to opportunistically power and clock gate
  137. * the audio dsp when it is idle
  138. */
  139. int hda_dsp_ctrl_clock_power_gating(struct snd_sof_dev *sdev, bool enable)
  140. {
  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. /* enable/disable DMI Link L1 support */
  146. val = enable ? HDA_VS_INTEL_EM2_L1SEN : 0;
  147. snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR, HDA_VS_INTEL_EM2,
  148. HDA_VS_INTEL_EM2_L1SEN, val);
  149. /* enable/disable audio dsp power gating */
  150. val = enable ? 0 : PCI_PGCTL_ADSPPGD;
  151. snd_sof_pci_update_bits(sdev, PCI_PGCTL, PCI_PGCTL_ADSPPGD, val);
  152. return 0;
  153. }
  154. int hda_dsp_ctrl_init_chip(struct snd_sof_dev *sdev, bool full_reset)
  155. {
  156. struct hdac_bus *bus = sof_to_bus(sdev);
  157. #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
  158. struct hdac_ext_link *hlink;
  159. #endif
  160. struct hdac_stream *stream;
  161. int sd_offset, ret = 0;
  162. if (bus->chip_init)
  163. return 0;
  164. #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
  165. snd_hdac_set_codec_wakeup(bus, true);
  166. #endif
  167. hda_dsp_ctrl_misc_clock_gating(sdev, false);
  168. if (full_reset) {
  169. /* reset HDA controller */
  170. ret = hda_dsp_ctrl_link_reset(sdev, true);
  171. if (ret < 0) {
  172. dev_err(sdev->dev, "error: failed to reset HDA controller\n");
  173. goto err;
  174. }
  175. usleep_range(500, 1000);
  176. /* exit HDA controller reset */
  177. ret = hda_dsp_ctrl_link_reset(sdev, false);
  178. if (ret < 0) {
  179. dev_err(sdev->dev, "error: failed to exit HDA controller reset\n");
  180. goto err;
  181. }
  182. usleep_range(1000, 1200);
  183. }
  184. #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
  185. /* check to see if controller is ready */
  186. if (!snd_hdac_chip_readb(bus, GCTL)) {
  187. dev_dbg(bus->dev, "controller not ready!\n");
  188. ret = -EBUSY;
  189. goto err;
  190. }
  191. /* Accept unsolicited responses */
  192. snd_hdac_chip_updatel(bus, GCTL, AZX_GCTL_UNSOL, AZX_GCTL_UNSOL);
  193. /* detect codecs */
  194. if (!bus->codec_mask) {
  195. bus->codec_mask = snd_hdac_chip_readw(bus, STATESTS);
  196. dev_dbg(bus->dev, "codec_mask = 0x%lx\n", bus->codec_mask);
  197. }
  198. if (hda_codec_mask != -1) {
  199. bus->codec_mask &= hda_codec_mask;
  200. dev_dbg(bus->dev, "filtered codec_mask = 0x%lx\n",
  201. bus->codec_mask);
  202. }
  203. #endif
  204. /* clear stream status */
  205. list_for_each_entry(stream, &bus->stream_list, list) {
  206. sd_offset = SOF_STREAM_SD_OFFSET(stream);
  207. snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR,
  208. sd_offset + SOF_HDA_ADSP_REG_CL_SD_STS,
  209. SOF_HDA_CL_DMA_SD_INT_MASK);
  210. }
  211. /* clear WAKESTS */
  212. snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR, SOF_HDA_WAKESTS,
  213. SOF_HDA_WAKESTS_INT_MASK);
  214. #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
  215. /* clear rirb status */
  216. snd_hdac_chip_writeb(bus, RIRBSTS, RIRB_INT_MASK);
  217. #endif
  218. /* clear interrupt status register */
  219. snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTSTS,
  220. SOF_HDA_INT_CTRL_EN | SOF_HDA_INT_ALL_STREAM);
  221. #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
  222. /* initialize the codec command I/O */
  223. snd_hdac_bus_init_cmd_io(bus);
  224. #endif
  225. /* enable CIE and GIE interrupts */
  226. snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTCTL,
  227. SOF_HDA_INT_CTRL_EN | SOF_HDA_INT_GLOBAL_EN,
  228. SOF_HDA_INT_CTRL_EN | SOF_HDA_INT_GLOBAL_EN);
  229. /* program the position buffer */
  230. if (bus->use_posbuf && bus->posbuf.addr) {
  231. snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR, SOF_HDA_ADSP_DPLBASE,
  232. (u32)bus->posbuf.addr);
  233. snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR, SOF_HDA_ADSP_DPUBASE,
  234. upper_32_bits(bus->posbuf.addr));
  235. }
  236. #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
  237. /* Reset stream-to-link mapping */
  238. list_for_each_entry(hlink, &bus->hlink_list, list)
  239. writel(0, hlink->ml_addr + AZX_REG_ML_LOSIDV);
  240. #endif
  241. bus->chip_init = true;
  242. err:
  243. hda_dsp_ctrl_misc_clock_gating(sdev, true);
  244. #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
  245. snd_hdac_set_codec_wakeup(bus, false);
  246. #endif
  247. return ret;
  248. }
  249. void hda_dsp_ctrl_stop_chip(struct snd_sof_dev *sdev)
  250. {
  251. struct hdac_bus *bus = sof_to_bus(sdev);
  252. struct hdac_stream *stream;
  253. int sd_offset;
  254. if (!bus->chip_init)
  255. return;
  256. /* disable interrupts in stream descriptor */
  257. list_for_each_entry(stream, &bus->stream_list, list) {
  258. sd_offset = SOF_STREAM_SD_OFFSET(stream);
  259. snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR,
  260. sd_offset +
  261. SOF_HDA_ADSP_REG_CL_SD_CTL,
  262. SOF_HDA_CL_DMA_SD_INT_MASK,
  263. 0);
  264. }
  265. /* disable SIE for all streams */
  266. snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTCTL,
  267. SOF_HDA_INT_ALL_STREAM, 0);
  268. /* disable controller CIE and GIE */
  269. snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTCTL,
  270. SOF_HDA_INT_CTRL_EN | SOF_HDA_INT_GLOBAL_EN,
  271. 0);
  272. /* clear stream status */
  273. list_for_each_entry(stream, &bus->stream_list, list) {
  274. sd_offset = SOF_STREAM_SD_OFFSET(stream);
  275. snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR,
  276. sd_offset + SOF_HDA_ADSP_REG_CL_SD_STS,
  277. SOF_HDA_CL_DMA_SD_INT_MASK);
  278. }
  279. /* clear WAKESTS */
  280. snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR, SOF_HDA_WAKESTS,
  281. SOF_HDA_WAKESTS_INT_MASK);
  282. #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
  283. /* clear rirb status */
  284. snd_hdac_chip_writeb(bus, RIRBSTS, RIRB_INT_MASK);
  285. #endif
  286. /* clear interrupt status register */
  287. snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTSTS,
  288. SOF_HDA_INT_CTRL_EN | SOF_HDA_INT_ALL_STREAM);
  289. #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
  290. /* disable CORB/RIRB */
  291. snd_hdac_bus_stop_cmd_io(bus);
  292. #endif
  293. /* disable position buffer */
  294. if (bus->use_posbuf && bus->posbuf.addr) {
  295. snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR,
  296. SOF_HDA_ADSP_DPLBASE, 0);
  297. snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR,
  298. SOF_HDA_ADSP_DPUBASE, 0);
  299. }
  300. bus->chip_init = false;
  301. }