xref: /openbmc/linux/sound/soc/sof/intel/hda-ctrl.c (revision e6f45ea2)
1 // SPDX-License-Identifier: (GPL-2.0 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 <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 /*
15  * Hardware interface for generic Intel audio DSP HDA IP
16  */
17 
18 #include <sound/hdaudio_ext.h>
19 #include <sound/hda_register.h>
20 #include "../ops.h"
21 #include "hda.h"
22 
23 /*
24  * HDA Operations.
25  */
26 
27 int hda_dsp_ctrl_link_reset(struct snd_sof_dev *sdev, bool reset)
28 {
29 	unsigned long timeout;
30 	u32 gctl = 0;
31 	u32 val;
32 
33 	/* 0 to enter reset and 1 to exit reset */
34 	val = reset ? 0 : SOF_HDA_GCTL_RESET;
35 
36 	/* enter/exit HDA controller reset */
37 	snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR, SOF_HDA_GCTL,
38 				SOF_HDA_GCTL_RESET, val);
39 
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 
49 	/* enter/exit reset failed */
50 	dev_err(sdev->dev, "error: failed to %s HDA controller gctl 0x%x\n",
51 		reset ? "reset" : "ready", gctl);
52 	return -EIO;
53 }
54 
55 int hda_dsp_ctrl_get_caps(struct snd_sof_dev *sdev)
56 {
57 	struct hdac_bus *bus = sof_to_bus(sdev);
58 	u32 cap, offset, feature;
59 	int count = 0;
60 
61 	offset = snd_sof_dsp_read(sdev, HDA_DSP_HDA_BAR, SOF_HDA_LLCH);
62 
63 	do {
64 		cap = snd_sof_dsp_read(sdev, HDA_DSP_HDA_BAR, offset);
65 
66 		dev_dbg(sdev->dev, "checking for capabilities at offset 0x%x\n",
67 			offset & SOF_HDA_CAP_NEXT_MASK);
68 
69 		feature = (cap & SOF_HDA_CAP_ID_MASK) >> SOF_HDA_CAP_ID_OFF;
70 
71 		switch (feature) {
72 		case SOF_HDA_PP_CAP_ID:
73 			dev_dbg(sdev->dev, "found DSP capability at 0x%x\n",
74 				offset);
75 			bus->ppcap = bus->remap_addr + offset;
76 			sdev->bar[HDA_DSP_PP_BAR] = bus->ppcap;
77 			break;
78 		case SOF_HDA_SPIB_CAP_ID:
79 			dev_dbg(sdev->dev, "found SPIB capability at 0x%x\n",
80 				offset);
81 			bus->spbcap = bus->remap_addr + offset;
82 			sdev->bar[HDA_DSP_SPIB_BAR] = bus->spbcap;
83 			break;
84 		case SOF_HDA_DRSM_CAP_ID:
85 			dev_dbg(sdev->dev, "found DRSM capability at 0x%x\n",
86 				offset);
87 			bus->drsmcap = bus->remap_addr + offset;
88 			sdev->bar[HDA_DSP_DRSM_BAR] = bus->drsmcap;
89 			break;
90 		case SOF_HDA_GTS_CAP_ID:
91 			dev_dbg(sdev->dev, "found GTS capability at 0x%x\n",
92 				offset);
93 			bus->gtscap = bus->remap_addr + offset;
94 			break;
95 		case SOF_HDA_ML_CAP_ID:
96 			dev_dbg(sdev->dev, "found ML capability at 0x%x\n",
97 				offset);
98 			bus->mlcap = bus->remap_addr + offset;
99 			break;
100 		default:
101 			dev_vdbg(sdev->dev, "found capability %d at 0x%x\n",
102 				 feature, offset);
103 			break;
104 		}
105 
106 		offset = cap & SOF_HDA_CAP_NEXT_MASK;
107 	} while (count++ <= SOF_HDA_MAX_CAPS && offset);
108 
109 	return 0;
110 }
111 
112 void hda_dsp_ctrl_ppcap_enable(struct snd_sof_dev *sdev, bool enable)
113 {
114 	u32 val = enable ? SOF_HDA_PPCTL_GPROCEN : 0;
115 
116 	snd_sof_dsp_update_bits(sdev, HDA_DSP_PP_BAR, SOF_HDA_REG_PP_PPCTL,
117 				SOF_HDA_PPCTL_GPROCEN, val);
118 }
119 
120 void hda_dsp_ctrl_ppcap_int_enable(struct snd_sof_dev *sdev, bool enable)
121 {
122 	u32 val	= enable ? SOF_HDA_PPCTL_PIE : 0;
123 
124 	snd_sof_dsp_update_bits(sdev, HDA_DSP_PP_BAR, SOF_HDA_REG_PP_PPCTL,
125 				SOF_HDA_PPCTL_PIE, val);
126 }
127 
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 
132 	snd_sof_pci_update_bits(sdev, PCI_CGCTL, PCI_CGCTL_MISCBDCGE_MASK, val);
133 }
134 
135 /*
136  * enable/disable audio dsp clock gating and power gating bits.
137  * This allows the HW to opportunistically power and clock gate
138  * the audio dsp when it is idle
139  */
140 int hda_dsp_ctrl_clock_power_gating(struct snd_sof_dev *sdev, bool enable)
141 {
142 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
143 	struct hdac_bus *bus = sof_to_bus(sdev);
144 #endif
145 	u32 val;
146 
147 	/* enable/disable audio dsp clock gating */
148 	val = enable ? PCI_CGCTL_ADSPDCGE : 0;
149 	snd_sof_pci_update_bits(sdev, PCI_CGCTL, PCI_CGCTL_ADSPDCGE, val);
150 
151 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
152 	/* enable/disable L1 support */
153 	val = enable ? SOF_HDA_VS_EM2_L1SEN : 0;
154 	snd_hdac_chip_updatel(bus, VS_EM2, SOF_HDA_VS_EM2_L1SEN, val);
155 #endif
156 
157 	/* enable/disable audio dsp power gating */
158 	val = enable ? 0 : PCI_PGCTL_ADSPPGD;
159 	snd_sof_pci_update_bits(sdev, PCI_PGCTL, PCI_PGCTL_ADSPPGD, val);
160 
161 	return 0;
162 }
163 
164 int hda_dsp_ctrl_init_chip(struct snd_sof_dev *sdev, bool full_reset)
165 {
166 	struct hdac_bus *bus = sof_to_bus(sdev);
167 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
168 	struct hdac_ext_link *hlink;
169 #endif
170 	struct hdac_stream *stream;
171 	int sd_offset, ret = 0;
172 
173 	if (bus->chip_init)
174 		return 0;
175 
176 	hda_dsp_ctrl_misc_clock_gating(sdev, false);
177 
178 	if (full_reset) {
179 		/* reset HDA controller */
180 		ret = hda_dsp_ctrl_link_reset(sdev, true);
181 		if (ret < 0) {
182 			dev_err(sdev->dev, "error: failed to reset HDA controller\n");
183 			return ret;
184 		}
185 
186 		usleep_range(500, 1000);
187 
188 		/* exit HDA controller reset */
189 		ret = hda_dsp_ctrl_link_reset(sdev, false);
190 		if (ret < 0) {
191 			dev_err(sdev->dev, "error: failed to exit HDA controller reset\n");
192 			return ret;
193 		}
194 
195 		usleep_range(1000, 1200);
196 	}
197 
198 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
199 	/* check to see if controller is ready */
200 	if (!snd_hdac_chip_readb(bus, GCTL)) {
201 		dev_dbg(bus->dev, "controller not ready!\n");
202 		return -EBUSY;
203 	}
204 
205 	/* Accept unsolicited responses */
206 	snd_hdac_chip_updatel(bus, GCTL, AZX_GCTL_UNSOL, AZX_GCTL_UNSOL);
207 
208 	/* detect codecs */
209 	if (!bus->codec_mask) {
210 		bus->codec_mask = snd_hdac_chip_readw(bus, STATESTS);
211 		dev_dbg(bus->dev, "codec_mask = 0x%lx\n", bus->codec_mask);
212 	}
213 #endif
214 
215 	/* clear stream status */
216 	list_for_each_entry(stream, &bus->stream_list, list) {
217 		sd_offset = SOF_STREAM_SD_OFFSET(stream);
218 		snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR,
219 				  sd_offset + SOF_HDA_ADSP_REG_CL_SD_STS,
220 				  SOF_HDA_CL_DMA_SD_INT_MASK);
221 	}
222 
223 	/* clear WAKESTS */
224 	snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR, SOF_HDA_WAKESTS,
225 			  SOF_HDA_WAKESTS_INT_MASK);
226 
227 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
228 	/* clear rirb status */
229 	snd_hdac_chip_writeb(bus, RIRBSTS, RIRB_INT_MASK);
230 #endif
231 
232 	/* clear interrupt status register */
233 	snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTSTS,
234 			  SOF_HDA_INT_CTRL_EN | SOF_HDA_INT_ALL_STREAM);
235 
236 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
237 	/* initialize the codec command I/O */
238 	snd_hdac_bus_init_cmd_io(bus);
239 #endif
240 
241 	/* enable CIE and GIE interrupts */
242 	snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTCTL,
243 				SOF_HDA_INT_CTRL_EN | SOF_HDA_INT_GLOBAL_EN,
244 				SOF_HDA_INT_CTRL_EN | SOF_HDA_INT_GLOBAL_EN);
245 
246 	/* program the position buffer */
247 	if (bus->use_posbuf && bus->posbuf.addr) {
248 		snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR, SOF_HDA_ADSP_DPLBASE,
249 				  (u32)bus->posbuf.addr);
250 		snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR, SOF_HDA_ADSP_DPUBASE,
251 				  upper_32_bits(bus->posbuf.addr));
252 	}
253 
254 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
255 	/* Reset stream-to-link mapping */
256 	list_for_each_entry(hlink, &bus->hlink_list, list)
257 		writel(0, hlink->ml_addr + AZX_REG_ML_LOSIDV);
258 #endif
259 
260 	bus->chip_init = true;
261 
262 	hda_dsp_ctrl_misc_clock_gating(sdev, true);
263 
264 	return ret;
265 }
266 
267 void hda_dsp_ctrl_stop_chip(struct snd_sof_dev *sdev)
268 {
269 	struct hdac_bus *bus = sof_to_bus(sdev);
270 	struct hdac_stream *stream;
271 	int sd_offset;
272 
273 	if (!bus->chip_init)
274 		return;
275 
276 	/* disable interrupts in stream descriptor */
277 	list_for_each_entry(stream, &bus->stream_list, list) {
278 		sd_offset = SOF_STREAM_SD_OFFSET(stream);
279 		snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR,
280 					sd_offset +
281 					SOF_HDA_ADSP_REG_CL_SD_CTL,
282 					SOF_HDA_CL_DMA_SD_INT_MASK,
283 					0);
284 	}
285 
286 	/* disable SIE for all streams */
287 	snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTCTL,
288 				SOF_HDA_INT_ALL_STREAM,	0);
289 
290 	/* disable controller CIE and GIE */
291 	snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTCTL,
292 				SOF_HDA_INT_CTRL_EN | SOF_HDA_INT_GLOBAL_EN,
293 				0);
294 
295 	/* clear stream status */
296 	list_for_each_entry(stream, &bus->stream_list, list) {
297 		sd_offset = SOF_STREAM_SD_OFFSET(stream);
298 		snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR,
299 				  sd_offset + SOF_HDA_ADSP_REG_CL_SD_STS,
300 				  SOF_HDA_CL_DMA_SD_INT_MASK);
301 	}
302 
303 	/* clear WAKESTS */
304 	snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR, SOF_HDA_WAKESTS,
305 			  SOF_HDA_WAKESTS_INT_MASK);
306 
307 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
308 	/* clear rirb status */
309 	snd_hdac_chip_writeb(bus, RIRBSTS, RIRB_INT_MASK);
310 #endif
311 
312 	/* clear interrupt status register */
313 	snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTSTS,
314 			  SOF_HDA_INT_CTRL_EN | SOF_HDA_INT_ALL_STREAM);
315 
316 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
317 	/* disable CORB/RIRB */
318 	snd_hdac_bus_stop_cmd_io(bus);
319 #endif
320 	/* disable position buffer */
321 	if (bus->posbuf.addr) {
322 		snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR,
323 				  SOF_HDA_ADSP_DPLBASE, 0);
324 		snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR,
325 				  SOF_HDA_ADSP_DPUBASE, 0);
326 	}
327 
328 	bus->chip_init = false;
329 }
330