xref: /openbmc/linux/sound/soc/sof/intel/hda-loader.c (revision 8354d9b4)
1d16046ffSLiam Girdwood // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
2d16046ffSLiam Girdwood //
3d16046ffSLiam Girdwood // This file is provided under a dual BSD/GPLv2 license.  When using or
4d16046ffSLiam Girdwood // redistributing this file, you may do so under either license.
5d16046ffSLiam Girdwood //
6d16046ffSLiam Girdwood // Copyright(c) 2018 Intel Corporation. All rights reserved.
7d16046ffSLiam Girdwood //
8d16046ffSLiam Girdwood // Authors: Liam Girdwood <liam.r.girdwood@linux.intel.com>
9d16046ffSLiam Girdwood //	    Ranjani Sridharan <ranjani.sridharan@linux.intel.com>
10d16046ffSLiam Girdwood //	    Rander Wang <rander.wang@intel.com>
11d16046ffSLiam Girdwood //          Keyon Jie <yang.jie@linux.intel.com>
12d16046ffSLiam Girdwood //
13d16046ffSLiam Girdwood 
14d16046ffSLiam Girdwood /*
15d16046ffSLiam Girdwood  * Hardware interface for HDA DSP code loader
16d16046ffSLiam Girdwood  */
17d16046ffSLiam Girdwood 
18d16046ffSLiam Girdwood #include <linux/firmware.h>
19d16046ffSLiam Girdwood #include <sound/hdaudio_ext.h>
20d16046ffSLiam Girdwood #include <sound/sof.h>
21d16046ffSLiam Girdwood #include "../ops.h"
22d16046ffSLiam Girdwood #include "hda.h"
23d16046ffSLiam Girdwood 
24d16046ffSLiam Girdwood #define HDA_FW_BOOT_ATTEMPTS	3
25d16046ffSLiam Girdwood 
26d16046ffSLiam Girdwood static int cl_stream_prepare(struct snd_sof_dev *sdev, unsigned int format,
27d16046ffSLiam Girdwood 			     unsigned int size, struct snd_dma_buffer *dmab,
28d16046ffSLiam Girdwood 			     int direction)
29d16046ffSLiam Girdwood {
30d16046ffSLiam Girdwood 	struct hdac_ext_stream *dsp_stream;
31d16046ffSLiam Girdwood 	struct hdac_stream *hstream;
32d16046ffSLiam Girdwood 	struct pci_dev *pci = to_pci_dev(sdev->dev);
33d16046ffSLiam Girdwood 	int ret;
34d16046ffSLiam Girdwood 
35d16046ffSLiam Girdwood 	if (direction != SNDRV_PCM_STREAM_PLAYBACK) {
36d16046ffSLiam Girdwood 		dev_err(sdev->dev, "error: code loading DMA is playback only\n");
37d16046ffSLiam Girdwood 		return -EINVAL;
38d16046ffSLiam Girdwood 	}
39d16046ffSLiam Girdwood 
40d16046ffSLiam Girdwood 	dsp_stream = hda_dsp_stream_get(sdev, direction);
41d16046ffSLiam Girdwood 
42d16046ffSLiam Girdwood 	if (!dsp_stream) {
43d16046ffSLiam Girdwood 		dev_err(sdev->dev, "error: no stream available\n");
44d16046ffSLiam Girdwood 		return -ENODEV;
45d16046ffSLiam Girdwood 	}
46d16046ffSLiam Girdwood 	hstream = &dsp_stream->hstream;
474ff5f643SKai Vehmanen 	hstream->substream = NULL;
48d16046ffSLiam Girdwood 
49d16046ffSLiam Girdwood 	/* allocate DMA buffer */
50d16046ffSLiam Girdwood 	ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV_SG, &pci->dev, size, dmab);
51d16046ffSLiam Girdwood 	if (ret < 0) {
52d16046ffSLiam Girdwood 		dev_err(sdev->dev, "error: memory alloc failed: %x\n", ret);
53d16046ffSLiam Girdwood 		goto error;
54d16046ffSLiam Girdwood 	}
55d16046ffSLiam Girdwood 
56d16046ffSLiam Girdwood 	hstream->period_bytes = 0;/* initialize period_bytes */
57d16046ffSLiam Girdwood 	hstream->format_val = format;
58d16046ffSLiam Girdwood 	hstream->bufsize = size;
59d16046ffSLiam Girdwood 
60d16046ffSLiam Girdwood 	ret = hda_dsp_stream_hw_params(sdev, dsp_stream, dmab, NULL);
61d16046ffSLiam Girdwood 	if (ret < 0) {
62d16046ffSLiam Girdwood 		dev_err(sdev->dev, "error: hdac prepare failed: %x\n", ret);
63d16046ffSLiam Girdwood 		goto error;
64d16046ffSLiam Girdwood 	}
65d16046ffSLiam Girdwood 
66d16046ffSLiam Girdwood 	hda_dsp_stream_spib_config(sdev, dsp_stream, HDA_DSP_SPIB_ENABLE, size);
67d16046ffSLiam Girdwood 
68d16046ffSLiam Girdwood 	return hstream->stream_tag;
69d16046ffSLiam Girdwood 
70d16046ffSLiam Girdwood error:
71d16046ffSLiam Girdwood 	hda_dsp_stream_put(sdev, direction, hstream->stream_tag);
72d16046ffSLiam Girdwood 	snd_dma_free_pages(dmab);
73d16046ffSLiam Girdwood 	return ret;
74d16046ffSLiam Girdwood }
75d16046ffSLiam Girdwood 
76d16046ffSLiam Girdwood /*
77d16046ffSLiam Girdwood  * first boot sequence has some extra steps. core 0 waits for power
78d16046ffSLiam Girdwood  * status on core 1, so power up core 1 also momentarily, keep it in
79d16046ffSLiam Girdwood  * reset/stall and then turn it off
80d16046ffSLiam Girdwood  */
81d16046ffSLiam Girdwood static int cl_dsp_init(struct snd_sof_dev *sdev, const void *fwdata,
82d16046ffSLiam Girdwood 		       u32 fwsize, int stream_tag)
83d16046ffSLiam Girdwood {
84d16046ffSLiam Girdwood 	struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
85d16046ffSLiam Girdwood 	const struct sof_intel_dsp_desc *chip = hda->desc;
86d16046ffSLiam Girdwood 	unsigned int status;
87d16046ffSLiam Girdwood 	int ret;
8874ed4097SZhu Yingjiang 	int i;
89d16046ffSLiam Girdwood 
90d16046ffSLiam Girdwood 	/* step 1: power up corex */
91d16046ffSLiam Girdwood 	ret = hda_dsp_core_power_up(sdev, chip->cores_mask);
92d16046ffSLiam Girdwood 	if (ret < 0) {
93d16046ffSLiam Girdwood 		dev_err(sdev->dev, "error: dsp core 0/1 power up failed\n");
94d16046ffSLiam Girdwood 		goto err;
95d16046ffSLiam Girdwood 	}
96d16046ffSLiam Girdwood 
9774ed4097SZhu Yingjiang 	/* DSP is powered up, set all SSPs to slave mode */
9874ed4097SZhu Yingjiang 	for (i = 0; i < chip->ssp_count; i++) {
9974ed4097SZhu Yingjiang 		snd_sof_dsp_update_bits_unlocked(sdev, HDA_DSP_BAR,
10074ed4097SZhu Yingjiang 						 chip->ssp_base_offset
10174ed4097SZhu Yingjiang 						 + i * SSP_DEV_MEM_SIZE
10274ed4097SZhu Yingjiang 						 + SSP_SSC1_OFFSET,
10374ed4097SZhu Yingjiang 						 SSP_SET_SLAVE,
10474ed4097SZhu Yingjiang 						 SSP_SET_SLAVE);
10574ed4097SZhu Yingjiang 	}
10674ed4097SZhu Yingjiang 
107d16046ffSLiam Girdwood 	/* step 2: purge FW request */
108d16046ffSLiam Girdwood 	snd_sof_dsp_write(sdev, HDA_DSP_BAR, chip->ipc_req,
109d16046ffSLiam Girdwood 			  chip->ipc_req_mask | (HDA_DSP_IPC_PURGE_FW |
110d16046ffSLiam Girdwood 			  ((stream_tag - 1) << 9)));
111d16046ffSLiam Girdwood 
112d16046ffSLiam Girdwood 	/* step 3: unset core 0 reset state & unstall/run core 0 */
113d16046ffSLiam Girdwood 	ret = hda_dsp_core_run(sdev, HDA_DSP_CORE_MASK(0));
114d16046ffSLiam Girdwood 	if (ret < 0) {
115d16046ffSLiam Girdwood 		dev_err(sdev->dev, "error: dsp core start failed %d\n", ret);
116d16046ffSLiam Girdwood 		ret = -EIO;
117d16046ffSLiam Girdwood 		goto err;
118d16046ffSLiam Girdwood 	}
119d16046ffSLiam Girdwood 
120d16046ffSLiam Girdwood 	/* step 4: wait for IPC DONE bit from ROM */
121d16046ffSLiam Girdwood 	ret = snd_sof_dsp_read_poll_timeout(sdev, HDA_DSP_BAR,
122d16046ffSLiam Girdwood 					    chip->ipc_ack, status,
123d16046ffSLiam Girdwood 					    ((status & chip->ipc_ack_mask)
124d16046ffSLiam Girdwood 						    == chip->ipc_ack_mask),
125d16046ffSLiam Girdwood 					    HDA_DSP_REG_POLL_INTERVAL_US,
126d16046ffSLiam Girdwood 					    HDA_DSP_INIT_TIMEOUT_US);
127d16046ffSLiam Girdwood 
128d16046ffSLiam Girdwood 	if (ret < 0) {
1296a414489SPierre-Louis Bossart 		dev_err(sdev->dev, "error: %s: timeout for HIPCIE done\n",
1306a414489SPierre-Louis Bossart 			__func__);
131d16046ffSLiam Girdwood 		goto err;
132d16046ffSLiam Girdwood 	}
133d16046ffSLiam Girdwood 
1348354d9b4SKeyon Jie 	/* set DONE bit to clear the reply IPC message */
1358354d9b4SKeyon Jie 	snd_sof_dsp_update_bits_forced(sdev, HDA_DSP_BAR,
1368354d9b4SKeyon Jie 				       chip->ipc_ack,
1378354d9b4SKeyon Jie 				       chip->ipc_ack_mask,
1388354d9b4SKeyon Jie 				       chip->ipc_ack_mask);
1398354d9b4SKeyon Jie 
140d16046ffSLiam Girdwood 	/* step 5: power down corex */
141d16046ffSLiam Girdwood 	ret = hda_dsp_core_power_down(sdev,
142d16046ffSLiam Girdwood 				  chip->cores_mask & ~(HDA_DSP_CORE_MASK(0)));
143d16046ffSLiam Girdwood 	if (ret < 0) {
144d16046ffSLiam Girdwood 		dev_err(sdev->dev, "error: dsp core x power down failed\n");
145d16046ffSLiam Girdwood 		goto err;
146d16046ffSLiam Girdwood 	}
147d16046ffSLiam Girdwood 
148d16046ffSLiam Girdwood 	/* step 6: enable IPC interrupts */
149d16046ffSLiam Girdwood 	hda_dsp_ipc_int_enable(sdev);
150d16046ffSLiam Girdwood 
151d16046ffSLiam Girdwood 	/* step 7: wait for ROM init */
152d16046ffSLiam Girdwood 	ret = snd_sof_dsp_read_poll_timeout(sdev, HDA_DSP_BAR,
153d16046ffSLiam Girdwood 					HDA_DSP_SRAM_REG_ROM_STATUS, status,
154d16046ffSLiam Girdwood 					((status & HDA_DSP_ROM_STS_MASK)
155d16046ffSLiam Girdwood 						== HDA_DSP_ROM_INIT),
156d16046ffSLiam Girdwood 					HDA_DSP_REG_POLL_INTERVAL_US,
157d16046ffSLiam Girdwood 					chip->rom_init_timeout *
158d16046ffSLiam Girdwood 					USEC_PER_MSEC);
159d16046ffSLiam Girdwood 	if (!ret)
160d16046ffSLiam Girdwood 		return 0;
161d16046ffSLiam Girdwood 
1626a414489SPierre-Louis Bossart 	dev_err(sdev->dev,
1636a414489SPierre-Louis Bossart 		"error: %s: timeout HDA_DSP_SRAM_REG_ROM_STATUS read\n",
1646a414489SPierre-Louis Bossart 		__func__);
1656a414489SPierre-Louis Bossart 
166d16046ffSLiam Girdwood err:
167d16046ffSLiam Girdwood 	hda_dsp_dump(sdev, SOF_DBG_REGS | SOF_DBG_PCI | SOF_DBG_MBOX);
168d16046ffSLiam Girdwood 	hda_dsp_core_reset_power_down(sdev, chip->cores_mask);
169d16046ffSLiam Girdwood 
170d16046ffSLiam Girdwood 	return ret;
171d16046ffSLiam Girdwood }
172d16046ffSLiam Girdwood 
173d16046ffSLiam Girdwood static int cl_trigger(struct snd_sof_dev *sdev,
174d16046ffSLiam Girdwood 		      struct hdac_ext_stream *stream, int cmd)
175d16046ffSLiam Girdwood {
176d16046ffSLiam Girdwood 	struct hdac_stream *hstream = &stream->hstream;
177d16046ffSLiam Girdwood 	int sd_offset = SOF_STREAM_SD_OFFSET(hstream);
178d16046ffSLiam Girdwood 
179d16046ffSLiam Girdwood 	/* code loader is special case that reuses stream ops */
180d16046ffSLiam Girdwood 	switch (cmd) {
181d16046ffSLiam Girdwood 	case SNDRV_PCM_TRIGGER_START:
182d16046ffSLiam Girdwood 		wait_event_timeout(sdev->waitq, !sdev->code_loading,
183d16046ffSLiam Girdwood 				   HDA_DSP_CL_TRIGGER_TIMEOUT);
184d16046ffSLiam Girdwood 
185d16046ffSLiam Girdwood 		snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTCTL,
186d16046ffSLiam Girdwood 					1 << hstream->index,
187d16046ffSLiam Girdwood 					1 << hstream->index);
188d16046ffSLiam Girdwood 
189d16046ffSLiam Girdwood 		snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR,
190d16046ffSLiam Girdwood 					sd_offset,
191d16046ffSLiam Girdwood 					SOF_HDA_SD_CTL_DMA_START |
192d16046ffSLiam Girdwood 					SOF_HDA_CL_DMA_SD_INT_MASK,
193d16046ffSLiam Girdwood 					SOF_HDA_SD_CTL_DMA_START |
194d16046ffSLiam Girdwood 					SOF_HDA_CL_DMA_SD_INT_MASK);
195d16046ffSLiam Girdwood 
196d16046ffSLiam Girdwood 		hstream->running = true;
197d16046ffSLiam Girdwood 		return 0;
198d16046ffSLiam Girdwood 	default:
199d16046ffSLiam Girdwood 		return hda_dsp_stream_trigger(sdev, stream, cmd);
200d16046ffSLiam Girdwood 	}
201d16046ffSLiam Girdwood }
202d16046ffSLiam Girdwood 
203d16046ffSLiam Girdwood static struct hdac_ext_stream *get_stream_with_tag(struct snd_sof_dev *sdev,
204d16046ffSLiam Girdwood 						   int tag)
205d16046ffSLiam Girdwood {
206d16046ffSLiam Girdwood 	struct hdac_bus *bus = sof_to_bus(sdev);
207d16046ffSLiam Girdwood 	struct hdac_stream *s;
208d16046ffSLiam Girdwood 
209d16046ffSLiam Girdwood 	/* get stream with tag */
210d16046ffSLiam Girdwood 	list_for_each_entry(s, &bus->stream_list, list) {
211d16046ffSLiam Girdwood 		if (s->direction == SNDRV_PCM_STREAM_PLAYBACK &&
212d16046ffSLiam Girdwood 		    s->stream_tag == tag) {
213d16046ffSLiam Girdwood 			return stream_to_hdac_ext_stream(s);
214d16046ffSLiam Girdwood 		}
215d16046ffSLiam Girdwood 	}
216d16046ffSLiam Girdwood 
217d16046ffSLiam Girdwood 	return NULL;
218d16046ffSLiam Girdwood }
219d16046ffSLiam Girdwood 
220d16046ffSLiam Girdwood static int cl_cleanup(struct snd_sof_dev *sdev, struct snd_dma_buffer *dmab,
221d16046ffSLiam Girdwood 		      struct hdac_ext_stream *stream)
222d16046ffSLiam Girdwood {
223d16046ffSLiam Girdwood 	struct hdac_stream *hstream = &stream->hstream;
224d16046ffSLiam Girdwood 	int sd_offset = SOF_STREAM_SD_OFFSET(hstream);
225d16046ffSLiam Girdwood 	int ret;
226d16046ffSLiam Girdwood 
227d16046ffSLiam Girdwood 	ret = hda_dsp_stream_spib_config(sdev, stream, HDA_DSP_SPIB_DISABLE, 0);
228d16046ffSLiam Girdwood 
229d16046ffSLiam Girdwood 	hda_dsp_stream_put(sdev, SNDRV_PCM_STREAM_PLAYBACK,
230d16046ffSLiam Girdwood 			   hstream->stream_tag);
231d16046ffSLiam Girdwood 	hstream->running = 0;
232d16046ffSLiam Girdwood 	hstream->substream = NULL;
233d16046ffSLiam Girdwood 
234d16046ffSLiam Girdwood 	/* reset BDL address */
235d16046ffSLiam Girdwood 	snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR,
236d16046ffSLiam Girdwood 			  sd_offset + SOF_HDA_ADSP_REG_CL_SD_BDLPL, 0);
237d16046ffSLiam Girdwood 	snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR,
238d16046ffSLiam Girdwood 			  sd_offset + SOF_HDA_ADSP_REG_CL_SD_BDLPU, 0);
239d16046ffSLiam Girdwood 
240d16046ffSLiam Girdwood 	snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR, sd_offset, 0);
241d16046ffSLiam Girdwood 	snd_dma_free_pages(dmab);
242d16046ffSLiam Girdwood 	dmab->area = NULL;
243d16046ffSLiam Girdwood 	hstream->bufsize = 0;
244d16046ffSLiam Girdwood 	hstream->format_val = 0;
245d16046ffSLiam Girdwood 
246d16046ffSLiam Girdwood 	return ret;
247d16046ffSLiam Girdwood }
248d16046ffSLiam Girdwood 
249d16046ffSLiam Girdwood static int cl_copy_fw(struct snd_sof_dev *sdev, struct hdac_ext_stream *stream)
250d16046ffSLiam Girdwood {
251d16046ffSLiam Girdwood 	unsigned int reg;
252d16046ffSLiam Girdwood 	int ret, status;
253d16046ffSLiam Girdwood 
254d16046ffSLiam Girdwood 	ret = cl_trigger(sdev, stream, SNDRV_PCM_TRIGGER_START);
255d16046ffSLiam Girdwood 	if (ret < 0) {
256d16046ffSLiam Girdwood 		dev_err(sdev->dev, "error: DMA trigger start failed\n");
257d16046ffSLiam Girdwood 		return ret;
258d16046ffSLiam Girdwood 	}
259d16046ffSLiam Girdwood 
260d16046ffSLiam Girdwood 	status = snd_sof_dsp_read_poll_timeout(sdev, HDA_DSP_BAR,
261d16046ffSLiam Girdwood 					HDA_DSP_SRAM_REG_ROM_STATUS, reg,
262d16046ffSLiam Girdwood 					((reg & HDA_DSP_ROM_STS_MASK)
263d16046ffSLiam Girdwood 						== HDA_DSP_ROM_FW_ENTERED),
264d16046ffSLiam Girdwood 					HDA_DSP_REG_POLL_INTERVAL_US,
265d16046ffSLiam Girdwood 					HDA_DSP_BASEFW_TIMEOUT_US);
266d16046ffSLiam Girdwood 
26776dc6a2bSPierre-Louis Bossart 	/*
26876dc6a2bSPierre-Louis Bossart 	 * even in case of errors we still need to stop the DMAs,
26976dc6a2bSPierre-Louis Bossart 	 * but we return the initial error should the DMA stop also fail
27076dc6a2bSPierre-Louis Bossart 	 */
27176dc6a2bSPierre-Louis Bossart 
2726a414489SPierre-Louis Bossart 	if (status < 0) {
2736a414489SPierre-Louis Bossart 		dev_err(sdev->dev,
2746a414489SPierre-Louis Bossart 			"error: %s: timeout HDA_DSP_SRAM_REG_ROM_STATUS read\n",
2756a414489SPierre-Louis Bossart 			__func__);
2766a414489SPierre-Louis Bossart 	}
2776a414489SPierre-Louis Bossart 
278d16046ffSLiam Girdwood 	ret = cl_trigger(sdev, stream, SNDRV_PCM_TRIGGER_STOP);
279d16046ffSLiam Girdwood 	if (ret < 0) {
280d16046ffSLiam Girdwood 		dev_err(sdev->dev, "error: DMA trigger stop failed\n");
28176dc6a2bSPierre-Louis Bossart 		if (!status)
28276dc6a2bSPierre-Louis Bossart 			status = ret;
283d16046ffSLiam Girdwood 	}
284d16046ffSLiam Girdwood 
285d16046ffSLiam Girdwood 	return status;
286d16046ffSLiam Girdwood }
287d16046ffSLiam Girdwood 
288d16046ffSLiam Girdwood int hda_dsp_cl_boot_firmware(struct snd_sof_dev *sdev)
289d16046ffSLiam Girdwood {
290d16046ffSLiam Girdwood 	struct snd_sof_pdata *plat_data = sdev->pdata;
291d16046ffSLiam Girdwood 	const struct sof_dev_desc *desc = plat_data->desc;
292d16046ffSLiam Girdwood 	const struct sof_intel_dsp_desc *chip_info;
293d16046ffSLiam Girdwood 	struct hdac_ext_stream *stream;
294d16046ffSLiam Girdwood 	struct firmware stripped_firmware;
295d16046ffSLiam Girdwood 	int ret, ret1, tag, i;
296d16046ffSLiam Girdwood 
297d16046ffSLiam Girdwood 	chip_info = desc->chip_info;
298d16046ffSLiam Girdwood 
299d16046ffSLiam Girdwood 	stripped_firmware.data = plat_data->fw->data;
300d16046ffSLiam Girdwood 	stripped_firmware.size = plat_data->fw->size;
301d16046ffSLiam Girdwood 
302d16046ffSLiam Girdwood 	/* init for booting wait */
303d16046ffSLiam Girdwood 	init_waitqueue_head(&sdev->boot_wait);
304d16046ffSLiam Girdwood 
305d16046ffSLiam Girdwood 	/* prepare DMA for code loader stream */
306d16046ffSLiam Girdwood 	tag = cl_stream_prepare(sdev, 0x40, stripped_firmware.size,
307d16046ffSLiam Girdwood 				&sdev->dmab, SNDRV_PCM_STREAM_PLAYBACK);
308d16046ffSLiam Girdwood 
309d16046ffSLiam Girdwood 	if (tag < 0) {
310d16046ffSLiam Girdwood 		dev_err(sdev->dev, "error: dma prepare for fw loading err: %x\n",
311d16046ffSLiam Girdwood 			tag);
312d16046ffSLiam Girdwood 		return tag;
313d16046ffSLiam Girdwood 	}
314d16046ffSLiam Girdwood 
315d16046ffSLiam Girdwood 	/* get stream with tag */
316d16046ffSLiam Girdwood 	stream = get_stream_with_tag(sdev, tag);
317d16046ffSLiam Girdwood 	if (!stream) {
318d16046ffSLiam Girdwood 		dev_err(sdev->dev,
319d16046ffSLiam Girdwood 			"error: could not get stream with stream tag %d\n",
320d16046ffSLiam Girdwood 			tag);
321d16046ffSLiam Girdwood 		ret = -ENODEV;
322d16046ffSLiam Girdwood 		goto err;
323d16046ffSLiam Girdwood 	}
324d16046ffSLiam Girdwood 
325d16046ffSLiam Girdwood 	memcpy(sdev->dmab.area, stripped_firmware.data,
326d16046ffSLiam Girdwood 	       stripped_firmware.size);
327d16046ffSLiam Girdwood 
328d16046ffSLiam Girdwood 	/* try ROM init a few times before giving up */
329d16046ffSLiam Girdwood 	for (i = 0; i < HDA_FW_BOOT_ATTEMPTS; i++) {
330d16046ffSLiam Girdwood 		ret = cl_dsp_init(sdev, stripped_firmware.data,
331d16046ffSLiam Girdwood 				  stripped_firmware.size, tag);
332d16046ffSLiam Girdwood 
333d16046ffSLiam Girdwood 		/* don't retry anymore if successful */
334d16046ffSLiam Girdwood 		if (!ret)
335d16046ffSLiam Girdwood 			break;
336d16046ffSLiam Girdwood 
337ceca2197SBard liao 		dev_dbg(sdev->dev, "iteration %d of Core En/ROM load failed: %d\n",
338ceca2197SBard liao 			i, ret);
339ceca2197SBard liao 		dev_dbg(sdev->dev, "Error code=0x%x: FW status=0x%x\n",
340d16046ffSLiam Girdwood 			snd_sof_dsp_read(sdev, HDA_DSP_BAR,
341d16046ffSLiam Girdwood 					 HDA_DSP_SRAM_REG_ROM_ERROR),
342d16046ffSLiam Girdwood 			snd_sof_dsp_read(sdev, HDA_DSP_BAR,
343d16046ffSLiam Girdwood 					 HDA_DSP_SRAM_REG_ROM_STATUS));
344d16046ffSLiam Girdwood 	}
345d16046ffSLiam Girdwood 
346d16046ffSLiam Girdwood 	if (i == HDA_FW_BOOT_ATTEMPTS) {
347d16046ffSLiam Girdwood 		dev_err(sdev->dev, "error: dsp init failed after %d attempts with err: %d\n",
348d16046ffSLiam Girdwood 			i, ret);
349d16046ffSLiam Girdwood 		goto cleanup;
350d16046ffSLiam Girdwood 	}
351d16046ffSLiam Girdwood 
352d16046ffSLiam Girdwood 	/*
353d16046ffSLiam Girdwood 	 * at this point DSP ROM has been initialized and
354d16046ffSLiam Girdwood 	 * should be ready for code loading and firmware boot
355d16046ffSLiam Girdwood 	 */
356d16046ffSLiam Girdwood 	ret = cl_copy_fw(sdev, stream);
357d16046ffSLiam Girdwood 	if (!ret)
358d16046ffSLiam Girdwood 		dev_dbg(sdev->dev, "Firmware download successful, booting...\n");
359d16046ffSLiam Girdwood 	else
360d16046ffSLiam Girdwood 		dev_err(sdev->dev, "error: load fw failed ret: %d\n", ret);
361d16046ffSLiam Girdwood 
362d16046ffSLiam Girdwood cleanup:
363d16046ffSLiam Girdwood 	/*
364d16046ffSLiam Girdwood 	 * Perform codeloader stream cleanup.
365d16046ffSLiam Girdwood 	 * This should be done even if firmware loading fails.
36676dc6a2bSPierre-Louis Bossart 	 * If the cleanup also fails, we return the initial error
367d16046ffSLiam Girdwood 	 */
368d16046ffSLiam Girdwood 	ret1 = cl_cleanup(sdev, &sdev->dmab, stream);
369d16046ffSLiam Girdwood 	if (ret1 < 0) {
370d16046ffSLiam Girdwood 		dev_err(sdev->dev, "error: Code loader DSP cleanup failed\n");
371d16046ffSLiam Girdwood 
372d16046ffSLiam Girdwood 		/* set return value to indicate cleanup failure */
37376dc6a2bSPierre-Louis Bossart 		if (!ret)
374d16046ffSLiam Girdwood 			ret = ret1;
375d16046ffSLiam Girdwood 	}
376d16046ffSLiam Girdwood 
377d16046ffSLiam Girdwood 	/*
378d16046ffSLiam Girdwood 	 * return master core id if both fw copy
379d16046ffSLiam Girdwood 	 * and stream clean up are successful
380d16046ffSLiam Girdwood 	 */
381d16046ffSLiam Girdwood 	if (!ret)
382d16046ffSLiam Girdwood 		return chip_info->init_core_mask;
383d16046ffSLiam Girdwood 
384d16046ffSLiam Girdwood 	/* dump dsp registers and disable DSP upon error */
385d16046ffSLiam Girdwood err:
386d16046ffSLiam Girdwood 	hda_dsp_dump(sdev, SOF_DBG_REGS | SOF_DBG_PCI | SOF_DBG_MBOX);
387d16046ffSLiam Girdwood 
388d16046ffSLiam Girdwood 	/* disable DSP */
389d16046ffSLiam Girdwood 	snd_sof_dsp_update_bits(sdev, HDA_DSP_PP_BAR,
390d16046ffSLiam Girdwood 				SOF_HDA_REG_PP_PPCTL,
391d16046ffSLiam Girdwood 				SOF_HDA_PPCTL_GPROCEN, 0);
392d16046ffSLiam Girdwood 	return ret;
393d16046ffSLiam Girdwood }
394d16046ffSLiam Girdwood 
395d16046ffSLiam Girdwood /* pre fw run operations */
396d16046ffSLiam Girdwood int hda_dsp_pre_fw_run(struct snd_sof_dev *sdev)
397d16046ffSLiam Girdwood {
398d16046ffSLiam Girdwood 	/* disable clock gating and power gating */
399d16046ffSLiam Girdwood 	return hda_dsp_ctrl_clock_power_gating(sdev, false);
400d16046ffSLiam Girdwood }
401d16046ffSLiam Girdwood 
402d16046ffSLiam Girdwood /* post fw run operations */
403d16046ffSLiam Girdwood int hda_dsp_post_fw_run(struct snd_sof_dev *sdev)
404d16046ffSLiam Girdwood {
405d16046ffSLiam Girdwood 	/* re-enable clock gating and power gating */
406d16046ffSLiam Girdwood 	return hda_dsp_ctrl_clock_power_gating(sdev, true);
407d16046ffSLiam Girdwood }
408