1 // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
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.
6 // Copyright(c) 2018 Intel Corporation. All rights reserved.
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>
15 * Hardware interface for HDA DSP code loader
18 #include <linux/firmware.h>
19 #include <sound/hdaudio_ext.h>
20 #include <sound/sof.h>
24 #define HDA_FW_BOOT_ATTEMPTS 3
26 static int cl_stream_prepare(struct snd_sof_dev *sdev, unsigned int format,
27 unsigned int size, struct snd_dma_buffer *dmab,
30 struct hdac_ext_stream *dsp_stream;
31 struct hdac_stream *hstream;
32 struct pci_dev *pci = to_pci_dev(sdev->dev);
35 if (direction != SNDRV_PCM_STREAM_PLAYBACK) {
36 dev_err(sdev->dev, "error: code loading DMA is playback only\n");
40 dsp_stream = hda_dsp_stream_get(sdev, direction);
43 dev_err(sdev->dev, "error: no stream available\n");
46 hstream = &dsp_stream->hstream;
47 hstream->substream = NULL;
49 /* allocate DMA buffer */
50 ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV_SG, &pci->dev, size, dmab);
52 dev_err(sdev->dev, "error: memory alloc failed: %x\n", ret);
56 hstream->period_bytes = 0;/* initialize period_bytes */
57 hstream->format_val = format;
58 hstream->bufsize = size;
60 ret = hda_dsp_stream_hw_params(sdev, dsp_stream, dmab, NULL);
62 dev_err(sdev->dev, "error: hdac prepare failed: %x\n", ret);
66 hda_dsp_stream_spib_config(sdev, dsp_stream, HDA_DSP_SPIB_ENABLE, size);
68 return hstream->stream_tag;
71 hda_dsp_stream_put(sdev, direction, hstream->stream_tag);
72 snd_dma_free_pages(dmab);
77 * first boot sequence has some extra steps. core 0 waits for power
78 * status on core 1, so power up core 1 also momentarily, keep it in
79 * reset/stall and then turn it off
81 static int cl_dsp_init(struct snd_sof_dev *sdev, const void *fwdata,
82 u32 fwsize, int stream_tag)
84 struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
85 const struct sof_intel_dsp_desc *chip = hda->desc;
90 /* step 1: power up corex */
91 ret = hda_dsp_core_power_up(sdev, chip->cores_mask);
93 dev_err(sdev->dev, "error: dsp core 0/1 power up failed\n");
97 /* DSP is powered up, set all SSPs to slave mode */
98 for (i = 0; i < chip->ssp_count; i++) {
99 snd_sof_dsp_update_bits_unlocked(sdev, HDA_DSP_BAR,
100 chip->ssp_base_offset
101 + i * SSP_DEV_MEM_SIZE
107 /* step 2: purge FW request */
108 snd_sof_dsp_write(sdev, HDA_DSP_BAR, chip->ipc_req,
109 chip->ipc_req_mask | (HDA_DSP_IPC_PURGE_FW |
110 ((stream_tag - 1) << 9)));
112 /* step 3: unset core 0 reset state & unstall/run core 0 */
113 ret = hda_dsp_core_run(sdev, HDA_DSP_CORE_MASK(0));
115 dev_err(sdev->dev, "error: dsp core start failed %d\n", ret);
120 /* step 4: wait for IPC DONE bit from ROM */
121 ret = snd_sof_dsp_read_poll_timeout(sdev, HDA_DSP_BAR,
122 chip->ipc_ack, status,
123 ((status & chip->ipc_ack_mask)
124 == chip->ipc_ack_mask),
125 HDA_DSP_REG_POLL_INTERVAL_US,
126 HDA_DSP_INIT_TIMEOUT_US);
129 dev_err(sdev->dev, "error: %s: timeout for HIPCIE done\n",
134 /* step 5: power down corex */
135 ret = hda_dsp_core_power_down(sdev,
136 chip->cores_mask & ~(HDA_DSP_CORE_MASK(0)));
138 dev_err(sdev->dev, "error: dsp core x power down failed\n");
142 /* step 6: enable IPC interrupts */
143 hda_dsp_ipc_int_enable(sdev);
145 /* step 7: wait for ROM init */
146 ret = snd_sof_dsp_read_poll_timeout(sdev, HDA_DSP_BAR,
147 HDA_DSP_SRAM_REG_ROM_STATUS, status,
148 ((status & HDA_DSP_ROM_STS_MASK)
149 == HDA_DSP_ROM_INIT),
150 HDA_DSP_REG_POLL_INTERVAL_US,
151 chip->rom_init_timeout *
157 "error: %s: timeout HDA_DSP_SRAM_REG_ROM_STATUS read\n",
161 hda_dsp_dump(sdev, SOF_DBG_REGS | SOF_DBG_PCI | SOF_DBG_MBOX);
162 hda_dsp_core_reset_power_down(sdev, chip->cores_mask);
167 static int cl_trigger(struct snd_sof_dev *sdev,
168 struct hdac_ext_stream *stream, int cmd)
170 struct hdac_stream *hstream = &stream->hstream;
171 int sd_offset = SOF_STREAM_SD_OFFSET(hstream);
173 /* code loader is special case that reuses stream ops */
175 case SNDRV_PCM_TRIGGER_START:
176 wait_event_timeout(sdev->waitq, !sdev->code_loading,
177 HDA_DSP_CL_TRIGGER_TIMEOUT);
179 snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTCTL,
181 1 << hstream->index);
183 snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR,
185 SOF_HDA_SD_CTL_DMA_START |
186 SOF_HDA_CL_DMA_SD_INT_MASK,
187 SOF_HDA_SD_CTL_DMA_START |
188 SOF_HDA_CL_DMA_SD_INT_MASK);
190 hstream->running = true;
193 return hda_dsp_stream_trigger(sdev, stream, cmd);
197 static struct hdac_ext_stream *get_stream_with_tag(struct snd_sof_dev *sdev,
200 struct hdac_bus *bus = sof_to_bus(sdev);
201 struct hdac_stream *s;
203 /* get stream with tag */
204 list_for_each_entry(s, &bus->stream_list, list) {
205 if (s->direction == SNDRV_PCM_STREAM_PLAYBACK &&
206 s->stream_tag == tag) {
207 return stream_to_hdac_ext_stream(s);
214 static int cl_cleanup(struct snd_sof_dev *sdev, struct snd_dma_buffer *dmab,
215 struct hdac_ext_stream *stream)
217 struct hdac_stream *hstream = &stream->hstream;
218 int sd_offset = SOF_STREAM_SD_OFFSET(hstream);
221 ret = hda_dsp_stream_spib_config(sdev, stream, HDA_DSP_SPIB_DISABLE, 0);
223 hda_dsp_stream_put(sdev, SNDRV_PCM_STREAM_PLAYBACK,
224 hstream->stream_tag);
225 hstream->running = 0;
226 hstream->substream = NULL;
228 /* reset BDL address */
229 snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR,
230 sd_offset + SOF_HDA_ADSP_REG_CL_SD_BDLPL, 0);
231 snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR,
232 sd_offset + SOF_HDA_ADSP_REG_CL_SD_BDLPU, 0);
234 snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR, sd_offset, 0);
235 snd_dma_free_pages(dmab);
237 hstream->bufsize = 0;
238 hstream->format_val = 0;
243 static int cl_copy_fw(struct snd_sof_dev *sdev, struct hdac_ext_stream *stream)
248 ret = cl_trigger(sdev, stream, SNDRV_PCM_TRIGGER_START);
250 dev_err(sdev->dev, "error: DMA trigger start failed\n");
254 status = snd_sof_dsp_read_poll_timeout(sdev, HDA_DSP_BAR,
255 HDA_DSP_SRAM_REG_ROM_STATUS, reg,
256 ((reg & HDA_DSP_ROM_STS_MASK)
257 == HDA_DSP_ROM_FW_ENTERED),
258 HDA_DSP_REG_POLL_INTERVAL_US,
259 HDA_DSP_BASEFW_TIMEOUT_US);
262 * even in case of errors we still need to stop the DMAs,
263 * but we return the initial error should the DMA stop also fail
268 "error: %s: timeout HDA_DSP_SRAM_REG_ROM_STATUS read\n",
272 ret = cl_trigger(sdev, stream, SNDRV_PCM_TRIGGER_STOP);
274 dev_err(sdev->dev, "error: DMA trigger stop failed\n");
282 int hda_dsp_cl_boot_firmware(struct snd_sof_dev *sdev)
284 struct snd_sof_pdata *plat_data = sdev->pdata;
285 const struct sof_dev_desc *desc = plat_data->desc;
286 const struct sof_intel_dsp_desc *chip_info;
287 struct hdac_ext_stream *stream;
288 struct firmware stripped_firmware;
289 int ret, ret1, tag, i;
291 chip_info = desc->chip_info;
293 stripped_firmware.data = plat_data->fw->data;
294 stripped_firmware.size = plat_data->fw->size;
296 /* init for booting wait */
297 init_waitqueue_head(&sdev->boot_wait);
299 /* prepare DMA for code loader stream */
300 tag = cl_stream_prepare(sdev, 0x40, stripped_firmware.size,
301 &sdev->dmab, SNDRV_PCM_STREAM_PLAYBACK);
304 dev_err(sdev->dev, "error: dma prepare for fw loading err: %x\n",
309 /* get stream with tag */
310 stream = get_stream_with_tag(sdev, tag);
313 "error: could not get stream with stream tag %d\n",
319 memcpy(sdev->dmab.area, stripped_firmware.data,
320 stripped_firmware.size);
322 /* try ROM init a few times before giving up */
323 for (i = 0; i < HDA_FW_BOOT_ATTEMPTS; i++) {
324 ret = cl_dsp_init(sdev, stripped_firmware.data,
325 stripped_firmware.size, tag);
327 /* don't retry anymore if successful */
331 dev_dbg(sdev->dev, "iteration %d of Core En/ROM load failed: %d\n",
333 dev_dbg(sdev->dev, "Error code=0x%x: FW status=0x%x\n",
334 snd_sof_dsp_read(sdev, HDA_DSP_BAR,
335 HDA_DSP_SRAM_REG_ROM_ERROR),
336 snd_sof_dsp_read(sdev, HDA_DSP_BAR,
337 HDA_DSP_SRAM_REG_ROM_STATUS));
340 if (i == HDA_FW_BOOT_ATTEMPTS) {
341 dev_err(sdev->dev, "error: dsp init failed after %d attempts with err: %d\n",
347 * at this point DSP ROM has been initialized and
348 * should be ready for code loading and firmware boot
350 ret = cl_copy_fw(sdev, stream);
352 dev_dbg(sdev->dev, "Firmware download successful, booting...\n");
354 dev_err(sdev->dev, "error: load fw failed ret: %d\n", ret);
358 * Perform codeloader stream cleanup.
359 * This should be done even if firmware loading fails.
360 * If the cleanup also fails, we return the initial error
362 ret1 = cl_cleanup(sdev, &sdev->dmab, stream);
364 dev_err(sdev->dev, "error: Code loader DSP cleanup failed\n");
366 /* set return value to indicate cleanup failure */
372 * return master core id if both fw copy
373 * and stream clean up are successful
376 return chip_info->init_core_mask;
378 /* dump dsp registers and disable DSP upon error */
380 hda_dsp_dump(sdev, SOF_DBG_REGS | SOF_DBG_PCI | SOF_DBG_MBOX);
383 snd_sof_dsp_update_bits(sdev, HDA_DSP_PP_BAR,
384 SOF_HDA_REG_PP_PPCTL,
385 SOF_HDA_PPCTL_GPROCEN, 0);
389 /* pre fw run operations */
390 int hda_dsp_pre_fw_run(struct snd_sof_dev *sdev)
392 /* disable clock gating and power gating */
393 return hda_dsp_ctrl_clock_power_gating(sdev, false);
396 /* post fw run operations */
397 int hda_dsp_post_fw_run(struct snd_sof_dev *sdev)
399 /* re-enable clock gating and power gating */
400 return hda_dsp_ctrl_clock_power_gating(sdev, true);