blob: 06715b3d8c319c55305121e76ba7cf5fa79ce015 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// 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 * DSP Core control.
25 */
26
27int hda_dsp_core_reset_enter(struct snd_sof_dev *sdev, unsigned int core_mask)
28{
29 u32 adspcs;
30 u32 reset;
31 int ret;
32
33 /* set reset bits for cores */
34 reset = HDA_DSP_ADSPCS_CRST_MASK(core_mask);
35 snd_sof_dsp_update_bits_unlocked(sdev, HDA_DSP_BAR,
36 HDA_DSP_REG_ADSPCS,
37 reset, reset),
38
39 /* poll with timeout to check if operation successful */
40 ret = snd_sof_dsp_read_poll_timeout(sdev, HDA_DSP_BAR,
41 HDA_DSP_REG_ADSPCS, adspcs,
42 ((adspcs & reset) == reset),
43 HDA_DSP_REG_POLL_INTERVAL_US,
44 HDA_DSP_RESET_TIMEOUT_US);
45
46 /* has core entered reset ? */
47 adspcs = snd_sof_dsp_read(sdev, HDA_DSP_BAR,
48 HDA_DSP_REG_ADSPCS);
49 if ((adspcs & HDA_DSP_ADSPCS_CRST_MASK(core_mask)) !=
50 HDA_DSP_ADSPCS_CRST_MASK(core_mask)) {
51 dev_err(sdev->dev,
52 "error: reset enter failed: core_mask %x adspcs 0x%x\n",
53 core_mask, adspcs);
54 ret = -EIO;
55 }
56
57 return ret;
58}
59
60int hda_dsp_core_reset_leave(struct snd_sof_dev *sdev, unsigned int core_mask)
61{
62 unsigned int crst;
63 u32 adspcs;
64 int ret;
65
66 /* clear reset bits for cores */
67 snd_sof_dsp_update_bits_unlocked(sdev, HDA_DSP_BAR,
68 HDA_DSP_REG_ADSPCS,
69 HDA_DSP_ADSPCS_CRST_MASK(core_mask),
70 0);
71
72 /* poll with timeout to check if operation successful */
73 crst = HDA_DSP_ADSPCS_CRST_MASK(core_mask);
74 ret = snd_sof_dsp_read_poll_timeout(sdev, HDA_DSP_BAR,
75 HDA_DSP_REG_ADSPCS, adspcs,
76 !(adspcs & crst),
77 HDA_DSP_REG_POLL_INTERVAL_US,
78 HDA_DSP_RESET_TIMEOUT_US);
79
80 /* has core left reset ? */
81 adspcs = snd_sof_dsp_read(sdev, HDA_DSP_BAR,
82 HDA_DSP_REG_ADSPCS);
83 if ((adspcs & HDA_DSP_ADSPCS_CRST_MASK(core_mask)) != 0) {
84 dev_err(sdev->dev,
85 "error: reset leave failed: core_mask %x adspcs 0x%x\n",
86 core_mask, adspcs);
87 ret = -EIO;
88 }
89
90 return ret;
91}
92
93int hda_dsp_core_stall_reset(struct snd_sof_dev *sdev, unsigned int core_mask)
94{
95 /* stall core */
96 snd_sof_dsp_update_bits_unlocked(sdev, HDA_DSP_BAR,
97 HDA_DSP_REG_ADSPCS,
98 HDA_DSP_ADSPCS_CSTALL_MASK(core_mask),
99 HDA_DSP_ADSPCS_CSTALL_MASK(core_mask));
100
101 /* set reset state */
102 return hda_dsp_core_reset_enter(sdev, core_mask);
103}
104
105int hda_dsp_core_run(struct snd_sof_dev *sdev, unsigned int core_mask)
106{
107 int ret;
108
109 /* leave reset state */
110 ret = hda_dsp_core_reset_leave(sdev, core_mask);
111 if (ret < 0)
112 return ret;
113
114 /* run core */
115 dev_dbg(sdev->dev, "unstall/run core: core_mask = %x\n", core_mask);
116 snd_sof_dsp_update_bits_unlocked(sdev, HDA_DSP_BAR,
117 HDA_DSP_REG_ADSPCS,
118 HDA_DSP_ADSPCS_CSTALL_MASK(core_mask),
119 0);
120
121 /* is core now running ? */
122 if (!hda_dsp_core_is_enabled(sdev, core_mask)) {
123 hda_dsp_core_stall_reset(sdev, core_mask);
124 dev_err(sdev->dev, "error: DSP start core failed: core_mask %x\n",
125 core_mask);
126 ret = -EIO;
127 }
128
129 return ret;
130}
131
132/*
133 * Power Management.
134 */
135
136int hda_dsp_core_power_up(struct snd_sof_dev *sdev, unsigned int core_mask)
137{
138 unsigned int cpa;
139 u32 adspcs;
140 int ret;
141
142 /* update bits */
143 snd_sof_dsp_update_bits(sdev, HDA_DSP_BAR, HDA_DSP_REG_ADSPCS,
144 HDA_DSP_ADSPCS_SPA_MASK(core_mask),
145 HDA_DSP_ADSPCS_SPA_MASK(core_mask));
146
147 /* poll with timeout to check if operation successful */
148 cpa = HDA_DSP_ADSPCS_CPA_MASK(core_mask);
149 ret = snd_sof_dsp_read_poll_timeout(sdev, HDA_DSP_BAR,
150 HDA_DSP_REG_ADSPCS, adspcs,
151 (adspcs & cpa) == cpa,
152 HDA_DSP_REG_POLL_INTERVAL_US,
153 HDA_DSP_RESET_TIMEOUT_US);
154 if (ret < 0)
155 dev_err(sdev->dev, "error: timeout on core powerup\n");
156
157 /* did core power up ? */
158 adspcs = snd_sof_dsp_read(sdev, HDA_DSP_BAR,
159 HDA_DSP_REG_ADSPCS);
160 if ((adspcs & HDA_DSP_ADSPCS_CPA_MASK(core_mask)) !=
161 HDA_DSP_ADSPCS_CPA_MASK(core_mask)) {
162 dev_err(sdev->dev,
163 "error: power up core failed core_mask %xadspcs 0x%x\n",
164 core_mask, adspcs);
165 ret = -EIO;
166 }
167
168 return ret;
169}
170
171int hda_dsp_core_power_down(struct snd_sof_dev *sdev, unsigned int core_mask)
172{
173 u32 adspcs;
174
175 /* update bits */
176 snd_sof_dsp_update_bits_unlocked(sdev, HDA_DSP_BAR,
177 HDA_DSP_REG_ADSPCS,
178 HDA_DSP_ADSPCS_SPA_MASK(core_mask), 0);
179
180 return snd_sof_dsp_read_poll_timeout(sdev, HDA_DSP_BAR,
181 HDA_DSP_REG_ADSPCS, adspcs,
Olivier Deprez0e641232021-09-23 10:07:05 +0200182 !(adspcs & HDA_DSP_ADSPCS_CPA_MASK(core_mask)),
David Brazdil0f672f62019-12-10 10:32:29 +0000183 HDA_DSP_REG_POLL_INTERVAL_US,
184 HDA_DSP_PD_TIMEOUT * USEC_PER_MSEC);
185}
186
187bool hda_dsp_core_is_enabled(struct snd_sof_dev *sdev,
188 unsigned int core_mask)
189{
190 int val;
191 bool is_enable;
192
193 val = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_ADSPCS);
194
Olivier Deprez0e641232021-09-23 10:07:05 +0200195#define MASK_IS_EQUAL(v, m, field) ({ \
196 u32 _m = field(m); \
197 ((v) & _m) == _m; \
198})
199
200 is_enable = MASK_IS_EQUAL(val, core_mask, HDA_DSP_ADSPCS_CPA_MASK) &&
201 MASK_IS_EQUAL(val, core_mask, HDA_DSP_ADSPCS_SPA_MASK) &&
202 !(val & HDA_DSP_ADSPCS_CRST_MASK(core_mask)) &&
203 !(val & HDA_DSP_ADSPCS_CSTALL_MASK(core_mask));
204
205#undef MASK_IS_EQUAL
David Brazdil0f672f62019-12-10 10:32:29 +0000206
207 dev_dbg(sdev->dev, "DSP core(s) enabled? %d : core_mask %x\n",
208 is_enable, core_mask);
209
210 return is_enable;
211}
212
213int hda_dsp_enable_core(struct snd_sof_dev *sdev, unsigned int core_mask)
214{
215 int ret;
216
217 /* return if core is already enabled */
218 if (hda_dsp_core_is_enabled(sdev, core_mask))
219 return 0;
220
221 /* power up */
222 ret = hda_dsp_core_power_up(sdev, core_mask);
223 if (ret < 0) {
224 dev_err(sdev->dev, "error: dsp core power up failed: core_mask %x\n",
225 core_mask);
226 return ret;
227 }
228
229 return hda_dsp_core_run(sdev, core_mask);
230}
231
232int hda_dsp_core_reset_power_down(struct snd_sof_dev *sdev,
233 unsigned int core_mask)
234{
235 int ret;
236
237 /* place core in reset prior to power down */
238 ret = hda_dsp_core_stall_reset(sdev, core_mask);
239 if (ret < 0) {
240 dev_err(sdev->dev, "error: dsp core reset failed: core_mask %x\n",
241 core_mask);
242 return ret;
243 }
244
245 /* power down core */
246 ret = hda_dsp_core_power_down(sdev, core_mask);
247 if (ret < 0) {
248 dev_err(sdev->dev, "error: dsp core power down fail mask %x: %d\n",
249 core_mask, ret);
250 return ret;
251 }
252
253 /* make sure we are in OFF state */
254 if (hda_dsp_core_is_enabled(sdev, core_mask)) {
255 dev_err(sdev->dev, "error: dsp core disable fail mask %x: %d\n",
256 core_mask, ret);
257 ret = -EIO;
258 }
259
260 return ret;
261}
262
263void hda_dsp_ipc_int_enable(struct snd_sof_dev *sdev)
264{
265 struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
266 const struct sof_intel_dsp_desc *chip = hda->desc;
267
268 /* enable IPC DONE and BUSY interrupts */
269 snd_sof_dsp_update_bits(sdev, HDA_DSP_BAR, chip->ipc_ctl,
270 HDA_DSP_REG_HIPCCTL_DONE | HDA_DSP_REG_HIPCCTL_BUSY,
271 HDA_DSP_REG_HIPCCTL_DONE | HDA_DSP_REG_HIPCCTL_BUSY);
272
273 /* enable IPC interrupt */
274 snd_sof_dsp_update_bits(sdev, HDA_DSP_BAR, HDA_DSP_REG_ADSPIC,
275 HDA_DSP_ADSPIC_IPC, HDA_DSP_ADSPIC_IPC);
276}
277
278void hda_dsp_ipc_int_disable(struct snd_sof_dev *sdev)
279{
280 struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
281 const struct sof_intel_dsp_desc *chip = hda->desc;
282
283 /* disable IPC interrupt */
284 snd_sof_dsp_update_bits(sdev, HDA_DSP_BAR, HDA_DSP_REG_ADSPIC,
285 HDA_DSP_ADSPIC_IPC, 0);
286
287 /* disable IPC BUSY and DONE interrupt */
288 snd_sof_dsp_update_bits(sdev, HDA_DSP_BAR, chip->ipc_ctl,
289 HDA_DSP_REG_HIPCCTL_BUSY | HDA_DSP_REG_HIPCCTL_DONE, 0);
290}
291
292static int hda_suspend(struct snd_sof_dev *sdev, bool runtime_suspend)
293{
294 struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
295 const struct sof_intel_dsp_desc *chip = hda->desc;
296#if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
297 struct hdac_bus *bus = sof_to_bus(sdev);
298#endif
299 int ret;
300
301 /* disable IPC interrupts */
302 hda_dsp_ipc_int_disable(sdev);
303
304#if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
305 if (runtime_suspend)
306 hda_codec_jack_wake_enable(sdev);
307
308 /* power down all hda link */
309 snd_hdac_ext_bus_link_power_down_all(bus);
310#endif
311
312 /* power down DSP */
313 ret = hda_dsp_core_reset_power_down(sdev, chip->cores_mask);
314 if (ret < 0) {
315 dev_err(sdev->dev,
316 "error: failed to power down core during suspend\n");
317 return ret;
318 }
319
320 /* disable ppcap interrupt */
321 hda_dsp_ctrl_ppcap_enable(sdev, false);
322 hda_dsp_ctrl_ppcap_int_enable(sdev, false);
323
324 /* disable hda bus irq and streams */
325 hda_dsp_ctrl_stop_chip(sdev);
326
327 /* disable LP retention mode */
328 snd_sof_pci_update_bits(sdev, PCI_PGCTL,
329 PCI_PGCTL_LSRMD_MASK, PCI_PGCTL_LSRMD_MASK);
330
331 /* reset controller */
332 ret = hda_dsp_ctrl_link_reset(sdev, true);
333 if (ret < 0) {
334 dev_err(sdev->dev,
335 "error: failed to reset controller during suspend\n");
336 return ret;
337 }
338
339 return 0;
340}
341
342static int hda_resume(struct snd_sof_dev *sdev, bool runtime_resume)
343{
344#if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
345 struct hdac_bus *bus = sof_to_bus(sdev);
346 struct hdac_ext_link *hlink = NULL;
347#endif
348 int ret;
349
350 /*
351 * clear TCSEL to clear playback on some HD Audio
352 * codecs. PCI TCSEL is defined in the Intel manuals.
353 */
354 snd_sof_pci_update_bits(sdev, PCI_TCSEL, 0x07, 0);
355
356 /* reset and start hda controller */
357 ret = hda_dsp_ctrl_init_chip(sdev, true);
358 if (ret < 0) {
359 dev_err(sdev->dev,
360 "error: failed to start controller after resume\n");
361 return ret;
362 }
363
364#if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
365 /* check jack status */
366 if (runtime_resume)
367 hda_codec_jack_check(sdev);
368
369 /* turn off the links that were off before suspend */
370 list_for_each_entry(hlink, &bus->hlink_list, list) {
371 if (!hlink->ref_count)
372 snd_hdac_ext_bus_link_power_down(hlink);
373 }
374
375 /* check dma status and clean up CORB/RIRB buffers */
376 if (!bus->cmd_dma_state)
377 snd_hdac_bus_stop_cmd_io(bus);
378#endif
379
380 /* enable ppcap interrupt */
381 hda_dsp_ctrl_ppcap_enable(sdev, true);
382 hda_dsp_ctrl_ppcap_int_enable(sdev, true);
383
384 return 0;
385}
386
387int hda_dsp_resume(struct snd_sof_dev *sdev)
388{
389 /* init hda controller. DSP cores will be powered up during fw boot */
390 return hda_resume(sdev, false);
391}
392
393int hda_dsp_runtime_resume(struct snd_sof_dev *sdev)
394{
395 /* init hda controller. DSP cores will be powered up during fw boot */
396 return hda_resume(sdev, true);
397}
398
399int hda_dsp_runtime_idle(struct snd_sof_dev *sdev)
400{
401 struct hdac_bus *hbus = sof_to_bus(sdev);
402
403 if (hbus->codec_powered) {
404 dev_dbg(sdev->dev, "some codecs still powered (%08X), not idle\n",
405 (unsigned int)hbus->codec_powered);
406 return -EBUSY;
407 }
408
409 return 0;
410}
411
412int hda_dsp_runtime_suspend(struct snd_sof_dev *sdev)
413{
414 /* stop hda controller and power dsp off */
415 return hda_suspend(sdev, true);
416}
417
418int hda_dsp_suspend(struct snd_sof_dev *sdev)
419{
420 struct hdac_bus *bus = sof_to_bus(sdev);
421 int ret;
422
423 /* stop hda controller and power dsp off */
424 ret = hda_suspend(sdev, false);
425 if (ret < 0) {
426 dev_err(bus->dev, "error: suspending dsp\n");
427 return ret;
428 }
429
430 return 0;
431}
432
433int hda_dsp_set_hw_params_upon_resume(struct snd_sof_dev *sdev)
434{
435#if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
436 struct hdac_bus *bus = sof_to_bus(sdev);
437 struct snd_soc_pcm_runtime *rtd;
438 struct hdac_ext_stream *stream;
439 struct hdac_ext_link *link;
440 struct hdac_stream *s;
441 const char *name;
442 int stream_tag;
443
444 /* set internal flag for BE */
445 list_for_each_entry(s, &bus->stream_list, list) {
446 stream = stream_to_hdac_ext_stream(s);
447
448 /*
449 * clear stream. This should already be taken care for running
450 * streams when the SUSPEND trigger is called. But paused
451 * streams do not get suspended, so this needs to be done
452 * explicitly during suspend.
453 */
454 if (stream->link_substream) {
455 rtd = snd_pcm_substream_chip(stream->link_substream);
456 name = rtd->codec_dai->component->name;
457 link = snd_hdac_ext_bus_get_link(bus, name);
458 if (!link)
459 return -EINVAL;
460
461 stream->link_prepared = 0;
462
463 if (hdac_stream(stream)->direction ==
464 SNDRV_PCM_STREAM_CAPTURE)
465 continue;
466
467 stream_tag = hdac_stream(stream)->stream_tag;
468 snd_hdac_ext_link_clear_stream_id(link, stream_tag);
469 }
470 }
471#endif
472 return 0;
473}