xref: /openbmc/linux/sound/soc/sof/intel/hda-loader.c (revision 53ec7531)
1e149ca29SPierre-Louis Bossart // SPDX-License-Identifier: (GPL-2.0-only 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,
8253ec7531SRanjani Sridharan 		       u32 fwsize, int stream_tag, int iteration)
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) {
9353ec7531SRanjani Sridharan 		if (iteration == HDA_FW_BOOT_ATTEMPTS)
94d16046ffSLiam Girdwood 			dev_err(sdev->dev, "error: dsp core 0/1 power up failed\n");
95d16046ffSLiam Girdwood 		goto err;
96d16046ffSLiam Girdwood 	}
97d16046ffSLiam Girdwood 
9874ed4097SZhu Yingjiang 	/* DSP is powered up, set all SSPs to slave mode */
9974ed4097SZhu Yingjiang 	for (i = 0; i < chip->ssp_count; i++) {
10074ed4097SZhu Yingjiang 		snd_sof_dsp_update_bits_unlocked(sdev, HDA_DSP_BAR,
10174ed4097SZhu Yingjiang 						 chip->ssp_base_offset
10274ed4097SZhu Yingjiang 						 + i * SSP_DEV_MEM_SIZE
10374ed4097SZhu Yingjiang 						 + SSP_SSC1_OFFSET,
10474ed4097SZhu Yingjiang 						 SSP_SET_SLAVE,
10574ed4097SZhu Yingjiang 						 SSP_SET_SLAVE);
10674ed4097SZhu Yingjiang 	}
10774ed4097SZhu Yingjiang 
108d16046ffSLiam Girdwood 	/* step 2: purge FW request */
109d16046ffSLiam Girdwood 	snd_sof_dsp_write(sdev, HDA_DSP_BAR, chip->ipc_req,
110d16046ffSLiam Girdwood 			  chip->ipc_req_mask | (HDA_DSP_IPC_PURGE_FW |
111d16046ffSLiam Girdwood 			  ((stream_tag - 1) << 9)));
112d16046ffSLiam Girdwood 
113d16046ffSLiam Girdwood 	/* step 3: unset core 0 reset state & unstall/run core 0 */
114d16046ffSLiam Girdwood 	ret = hda_dsp_core_run(sdev, HDA_DSP_CORE_MASK(0));
115d16046ffSLiam Girdwood 	if (ret < 0) {
11653ec7531SRanjani Sridharan 		if (iteration == HDA_FW_BOOT_ATTEMPTS)
11753ec7531SRanjani Sridharan 			dev_err(sdev->dev,
11853ec7531SRanjani Sridharan 				"error: dsp core start failed %d\n", ret);
119d16046ffSLiam Girdwood 		ret = -EIO;
120d16046ffSLiam Girdwood 		goto err;
121d16046ffSLiam Girdwood 	}
122d16046ffSLiam Girdwood 
123d16046ffSLiam Girdwood 	/* step 4: wait for IPC DONE bit from ROM */
124d16046ffSLiam Girdwood 	ret = snd_sof_dsp_read_poll_timeout(sdev, HDA_DSP_BAR,
125d16046ffSLiam Girdwood 					    chip->ipc_ack, status,
126d16046ffSLiam Girdwood 					    ((status & chip->ipc_ack_mask)
127d16046ffSLiam Girdwood 						    == chip->ipc_ack_mask),
128d16046ffSLiam Girdwood 					    HDA_DSP_REG_POLL_INTERVAL_US,
129d16046ffSLiam Girdwood 					    HDA_DSP_INIT_TIMEOUT_US);
130d16046ffSLiam Girdwood 
131d16046ffSLiam Girdwood 	if (ret < 0) {
13253ec7531SRanjani Sridharan 		if (iteration == HDA_FW_BOOT_ATTEMPTS)
13353ec7531SRanjani Sridharan 			dev_err(sdev->dev,
13453ec7531SRanjani Sridharan 				"error: %s: timeout for HIPCIE done\n",
1356a414489SPierre-Louis Bossart 				__func__);
136d16046ffSLiam Girdwood 		goto err;
137d16046ffSLiam Girdwood 	}
138d16046ffSLiam Girdwood 
1398354d9b4SKeyon Jie 	/* set DONE bit to clear the reply IPC message */
1408354d9b4SKeyon Jie 	snd_sof_dsp_update_bits_forced(sdev, HDA_DSP_BAR,
1418354d9b4SKeyon Jie 				       chip->ipc_ack,
1428354d9b4SKeyon Jie 				       chip->ipc_ack_mask,
1438354d9b4SKeyon Jie 				       chip->ipc_ack_mask);
1448354d9b4SKeyon Jie 
145d16046ffSLiam Girdwood 	/* step 5: power down corex */
146d16046ffSLiam Girdwood 	ret = hda_dsp_core_power_down(sdev,
147d16046ffSLiam Girdwood 				  chip->cores_mask & ~(HDA_DSP_CORE_MASK(0)));
148d16046ffSLiam Girdwood 	if (ret < 0) {
14953ec7531SRanjani Sridharan 		if (iteration == HDA_FW_BOOT_ATTEMPTS)
15053ec7531SRanjani Sridharan 			dev_err(sdev->dev,
15153ec7531SRanjani Sridharan 				"error: dsp core x power down failed\n");
152d16046ffSLiam Girdwood 		goto err;
153d16046ffSLiam Girdwood 	}
154d16046ffSLiam Girdwood 
155d16046ffSLiam Girdwood 	/* step 6: enable IPC interrupts */
156d16046ffSLiam Girdwood 	hda_dsp_ipc_int_enable(sdev);
157d16046ffSLiam Girdwood 
158d16046ffSLiam Girdwood 	/* step 7: wait for ROM init */
159d16046ffSLiam Girdwood 	ret = snd_sof_dsp_read_poll_timeout(sdev, HDA_DSP_BAR,
160d16046ffSLiam Girdwood 					HDA_DSP_SRAM_REG_ROM_STATUS, status,
161d16046ffSLiam Girdwood 					((status & HDA_DSP_ROM_STS_MASK)
162d16046ffSLiam Girdwood 						== HDA_DSP_ROM_INIT),
163d16046ffSLiam Girdwood 					HDA_DSP_REG_POLL_INTERVAL_US,
164d16046ffSLiam Girdwood 					chip->rom_init_timeout *
165d16046ffSLiam Girdwood 					USEC_PER_MSEC);
166d16046ffSLiam Girdwood 	if (!ret)
167d16046ffSLiam Girdwood 		return 0;
168d16046ffSLiam Girdwood 
16953ec7531SRanjani Sridharan 	if (iteration == HDA_FW_BOOT_ATTEMPTS)
1706a414489SPierre-Louis Bossart 		dev_err(sdev->dev,
1716a414489SPierre-Louis Bossart 			"error: %s: timeout HDA_DSP_SRAM_REG_ROM_STATUS read\n",
1726a414489SPierre-Louis Bossart 			__func__);
1736a414489SPierre-Louis Bossart 
174d16046ffSLiam Girdwood err:
175d16046ffSLiam Girdwood 	hda_dsp_dump(sdev, SOF_DBG_REGS | SOF_DBG_PCI | SOF_DBG_MBOX);
176d16046ffSLiam Girdwood 	hda_dsp_core_reset_power_down(sdev, chip->cores_mask);
177d16046ffSLiam Girdwood 
178d16046ffSLiam Girdwood 	return ret;
179d16046ffSLiam Girdwood }
180d16046ffSLiam Girdwood 
181d16046ffSLiam Girdwood static int cl_trigger(struct snd_sof_dev *sdev,
182d16046ffSLiam Girdwood 		      struct hdac_ext_stream *stream, int cmd)
183d16046ffSLiam Girdwood {
184d16046ffSLiam Girdwood 	struct hdac_stream *hstream = &stream->hstream;
185d16046ffSLiam Girdwood 	int sd_offset = SOF_STREAM_SD_OFFSET(hstream);
186d16046ffSLiam Girdwood 
187d16046ffSLiam Girdwood 	/* code loader is special case that reuses stream ops */
188d16046ffSLiam Girdwood 	switch (cmd) {
189d16046ffSLiam Girdwood 	case SNDRV_PCM_TRIGGER_START:
190d16046ffSLiam Girdwood 		snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTCTL,
191d16046ffSLiam Girdwood 					1 << hstream->index,
192d16046ffSLiam Girdwood 					1 << hstream->index);
193d16046ffSLiam Girdwood 
194d16046ffSLiam Girdwood 		snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR,
195d16046ffSLiam Girdwood 					sd_offset,
196d16046ffSLiam Girdwood 					SOF_HDA_SD_CTL_DMA_START |
197d16046ffSLiam Girdwood 					SOF_HDA_CL_DMA_SD_INT_MASK,
198d16046ffSLiam Girdwood 					SOF_HDA_SD_CTL_DMA_START |
199d16046ffSLiam Girdwood 					SOF_HDA_CL_DMA_SD_INT_MASK);
200d16046ffSLiam Girdwood 
201d16046ffSLiam Girdwood 		hstream->running = true;
202d16046ffSLiam Girdwood 		return 0;
203d16046ffSLiam Girdwood 	default:
204d16046ffSLiam Girdwood 		return hda_dsp_stream_trigger(sdev, stream, cmd);
205d16046ffSLiam Girdwood 	}
206d16046ffSLiam Girdwood }
207d16046ffSLiam Girdwood 
208d16046ffSLiam Girdwood static struct hdac_ext_stream *get_stream_with_tag(struct snd_sof_dev *sdev,
209d16046ffSLiam Girdwood 						   int tag)
210d16046ffSLiam Girdwood {
211d16046ffSLiam Girdwood 	struct hdac_bus *bus = sof_to_bus(sdev);
212d16046ffSLiam Girdwood 	struct hdac_stream *s;
213d16046ffSLiam Girdwood 
214d16046ffSLiam Girdwood 	/* get stream with tag */
215d16046ffSLiam Girdwood 	list_for_each_entry(s, &bus->stream_list, list) {
216d16046ffSLiam Girdwood 		if (s->direction == SNDRV_PCM_STREAM_PLAYBACK &&
217d16046ffSLiam Girdwood 		    s->stream_tag == tag) {
218d16046ffSLiam Girdwood 			return stream_to_hdac_ext_stream(s);
219d16046ffSLiam Girdwood 		}
220d16046ffSLiam Girdwood 	}
221d16046ffSLiam Girdwood 
222d16046ffSLiam Girdwood 	return NULL;
223d16046ffSLiam Girdwood }
224d16046ffSLiam Girdwood 
225d16046ffSLiam Girdwood static int cl_cleanup(struct snd_sof_dev *sdev, struct snd_dma_buffer *dmab,
226d16046ffSLiam Girdwood 		      struct hdac_ext_stream *stream)
227d16046ffSLiam Girdwood {
228d16046ffSLiam Girdwood 	struct hdac_stream *hstream = &stream->hstream;
229d16046ffSLiam Girdwood 	int sd_offset = SOF_STREAM_SD_OFFSET(hstream);
230d16046ffSLiam Girdwood 	int ret;
231d16046ffSLiam Girdwood 
232d16046ffSLiam Girdwood 	ret = hda_dsp_stream_spib_config(sdev, stream, HDA_DSP_SPIB_DISABLE, 0);
233d16046ffSLiam Girdwood 
234d16046ffSLiam Girdwood 	hda_dsp_stream_put(sdev, SNDRV_PCM_STREAM_PLAYBACK,
235d16046ffSLiam Girdwood 			   hstream->stream_tag);
236d16046ffSLiam Girdwood 	hstream->running = 0;
237d16046ffSLiam Girdwood 	hstream->substream = NULL;
238d16046ffSLiam Girdwood 
239d16046ffSLiam Girdwood 	/* reset BDL address */
240d16046ffSLiam Girdwood 	snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR,
241d16046ffSLiam Girdwood 			  sd_offset + SOF_HDA_ADSP_REG_CL_SD_BDLPL, 0);
242d16046ffSLiam Girdwood 	snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR,
243d16046ffSLiam Girdwood 			  sd_offset + SOF_HDA_ADSP_REG_CL_SD_BDLPU, 0);
244d16046ffSLiam Girdwood 
245d16046ffSLiam Girdwood 	snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR, sd_offset, 0);
246d16046ffSLiam Girdwood 	snd_dma_free_pages(dmab);
247d16046ffSLiam Girdwood 	dmab->area = NULL;
248d16046ffSLiam Girdwood 	hstream->bufsize = 0;
249d16046ffSLiam Girdwood 	hstream->format_val = 0;
250d16046ffSLiam Girdwood 
251d16046ffSLiam Girdwood 	return ret;
252d16046ffSLiam Girdwood }
253d16046ffSLiam Girdwood 
254d16046ffSLiam Girdwood static int cl_copy_fw(struct snd_sof_dev *sdev, struct hdac_ext_stream *stream)
255d16046ffSLiam Girdwood {
256d16046ffSLiam Girdwood 	unsigned int reg;
257d16046ffSLiam Girdwood 	int ret, status;
258d16046ffSLiam Girdwood 
259d16046ffSLiam Girdwood 	ret = cl_trigger(sdev, stream, SNDRV_PCM_TRIGGER_START);
260d16046ffSLiam Girdwood 	if (ret < 0) {
261d16046ffSLiam Girdwood 		dev_err(sdev->dev, "error: DMA trigger start failed\n");
262d16046ffSLiam Girdwood 		return ret;
263d16046ffSLiam Girdwood 	}
264d16046ffSLiam Girdwood 
265d16046ffSLiam Girdwood 	status = snd_sof_dsp_read_poll_timeout(sdev, HDA_DSP_BAR,
266d16046ffSLiam Girdwood 					HDA_DSP_SRAM_REG_ROM_STATUS, reg,
267d16046ffSLiam Girdwood 					((reg & HDA_DSP_ROM_STS_MASK)
268d16046ffSLiam Girdwood 						== HDA_DSP_ROM_FW_ENTERED),
269d16046ffSLiam Girdwood 					HDA_DSP_REG_POLL_INTERVAL_US,
270d16046ffSLiam Girdwood 					HDA_DSP_BASEFW_TIMEOUT_US);
271d16046ffSLiam Girdwood 
27276dc6a2bSPierre-Louis Bossart 	/*
27376dc6a2bSPierre-Louis Bossart 	 * even in case of errors we still need to stop the DMAs,
27476dc6a2bSPierre-Louis Bossart 	 * but we return the initial error should the DMA stop also fail
27576dc6a2bSPierre-Louis Bossart 	 */
27676dc6a2bSPierre-Louis Bossart 
2776a414489SPierre-Louis Bossart 	if (status < 0) {
2786a414489SPierre-Louis Bossart 		dev_err(sdev->dev,
2796a414489SPierre-Louis Bossart 			"error: %s: timeout HDA_DSP_SRAM_REG_ROM_STATUS read\n",
2806a414489SPierre-Louis Bossart 			__func__);
2816a414489SPierre-Louis Bossart 	}
2826a414489SPierre-Louis Bossart 
283d16046ffSLiam Girdwood 	ret = cl_trigger(sdev, stream, SNDRV_PCM_TRIGGER_STOP);
284d16046ffSLiam Girdwood 	if (ret < 0) {
285d16046ffSLiam Girdwood 		dev_err(sdev->dev, "error: DMA trigger stop failed\n");
28676dc6a2bSPierre-Louis Bossart 		if (!status)
28776dc6a2bSPierre-Louis Bossart 			status = ret;
288d16046ffSLiam Girdwood 	}
289d16046ffSLiam Girdwood 
290d16046ffSLiam Girdwood 	return status;
291d16046ffSLiam Girdwood }
292d16046ffSLiam Girdwood 
293d16046ffSLiam Girdwood int hda_dsp_cl_boot_firmware(struct snd_sof_dev *sdev)
294d16046ffSLiam Girdwood {
295d16046ffSLiam Girdwood 	struct snd_sof_pdata *plat_data = sdev->pdata;
296d16046ffSLiam Girdwood 	const struct sof_dev_desc *desc = plat_data->desc;
297d16046ffSLiam Girdwood 	const struct sof_intel_dsp_desc *chip_info;
298d16046ffSLiam Girdwood 	struct hdac_ext_stream *stream;
299d16046ffSLiam Girdwood 	struct firmware stripped_firmware;
300d16046ffSLiam Girdwood 	int ret, ret1, tag, i;
301d16046ffSLiam Girdwood 
302d16046ffSLiam Girdwood 	chip_info = desc->chip_info;
303d16046ffSLiam Girdwood 
304523773b9SKarol Trzcinski 	if (plat_data->fw->size <= plat_data->fw_offset) {
30592be17a5SKarol Trzcinski 		dev_err(sdev->dev, "error: firmware size must be greater than firmware offset\n");
30692be17a5SKarol Trzcinski 		return -EINVAL;
30792be17a5SKarol Trzcinski 	}
30892be17a5SKarol Trzcinski 
30992be17a5SKarol Trzcinski 	stripped_firmware.data = plat_data->fw->data + plat_data->fw_offset;
31092be17a5SKarol Trzcinski 	stripped_firmware.size = plat_data->fw->size - plat_data->fw_offset;
311d16046ffSLiam Girdwood 
312d16046ffSLiam Girdwood 	/* init for booting wait */
313d16046ffSLiam Girdwood 	init_waitqueue_head(&sdev->boot_wait);
314d16046ffSLiam Girdwood 
315d16046ffSLiam Girdwood 	/* prepare DMA for code loader stream */
316d16046ffSLiam Girdwood 	tag = cl_stream_prepare(sdev, 0x40, stripped_firmware.size,
317d16046ffSLiam Girdwood 				&sdev->dmab, SNDRV_PCM_STREAM_PLAYBACK);
318d16046ffSLiam Girdwood 
319d16046ffSLiam Girdwood 	if (tag < 0) {
320d16046ffSLiam Girdwood 		dev_err(sdev->dev, "error: dma prepare for fw loading err: %x\n",
321d16046ffSLiam Girdwood 			tag);
322d16046ffSLiam Girdwood 		return tag;
323d16046ffSLiam Girdwood 	}
324d16046ffSLiam Girdwood 
325d16046ffSLiam Girdwood 	/* get stream with tag */
326d16046ffSLiam Girdwood 	stream = get_stream_with_tag(sdev, tag);
327d16046ffSLiam Girdwood 	if (!stream) {
328d16046ffSLiam Girdwood 		dev_err(sdev->dev,
329d16046ffSLiam Girdwood 			"error: could not get stream with stream tag %d\n",
330d16046ffSLiam Girdwood 			tag);
331d16046ffSLiam Girdwood 		ret = -ENODEV;
332d16046ffSLiam Girdwood 		goto err;
333d16046ffSLiam Girdwood 	}
334d16046ffSLiam Girdwood 
335d16046ffSLiam Girdwood 	memcpy(sdev->dmab.area, stripped_firmware.data,
336d16046ffSLiam Girdwood 	       stripped_firmware.size);
337d16046ffSLiam Girdwood 
338d16046ffSLiam Girdwood 	/* try ROM init a few times before giving up */
339d16046ffSLiam Girdwood 	for (i = 0; i < HDA_FW_BOOT_ATTEMPTS; i++) {
34053ec7531SRanjani Sridharan 		dev_dbg(sdev->dev,
34153ec7531SRanjani Sridharan 			"Attempting iteration %d of Core En/ROM load...\n", i);
34253ec7531SRanjani Sridharan 
343d16046ffSLiam Girdwood 		ret = cl_dsp_init(sdev, stripped_firmware.data,
34453ec7531SRanjani Sridharan 				  stripped_firmware.size, tag, i + 1);
345d16046ffSLiam Girdwood 
346d16046ffSLiam Girdwood 		/* don't retry anymore if successful */
347d16046ffSLiam Girdwood 		if (!ret)
348d16046ffSLiam Girdwood 			break;
349d16046ffSLiam Girdwood 	}
350d16046ffSLiam Girdwood 
351d16046ffSLiam Girdwood 	if (i == HDA_FW_BOOT_ATTEMPTS) {
352d16046ffSLiam Girdwood 		dev_err(sdev->dev, "error: dsp init failed after %d attempts with err: %d\n",
353d16046ffSLiam Girdwood 			i, ret);
35453ec7531SRanjani Sridharan 		dev_err(sdev->dev, "ROM error=0x%x: FW status=0x%x\n",
35553ec7531SRanjani Sridharan 			snd_sof_dsp_read(sdev, HDA_DSP_BAR,
35653ec7531SRanjani Sridharan 					 HDA_DSP_SRAM_REG_ROM_ERROR),
35753ec7531SRanjani Sridharan 			snd_sof_dsp_read(sdev, HDA_DSP_BAR,
35853ec7531SRanjani Sridharan 					 HDA_DSP_SRAM_REG_ROM_STATUS));
359d16046ffSLiam Girdwood 		goto cleanup;
360d16046ffSLiam Girdwood 	}
361d16046ffSLiam Girdwood 
362d16046ffSLiam Girdwood 	/*
363bbd19cdcSRander Wang 	 * When a SoundWire link is in clock stop state, a Slave
364bbd19cdcSRander Wang 	 * device may trigger in-band wakes for events such as jack
365bbd19cdcSRander Wang 	 * insertion or acoustic event detection. This event will lead
366bbd19cdcSRander Wang 	 * to a WAKEEN interrupt, handled by the PCI device and routed
367bbd19cdcSRander Wang 	 * to PME if the PCI device is in D3. The resume function in
368bbd19cdcSRander Wang 	 * audio PCI driver will be invoked by ACPI for PME event and
369bbd19cdcSRander Wang 	 * initialize the device and process WAKEEN interrupt.
370bbd19cdcSRander Wang 	 *
371bbd19cdcSRander Wang 	 * The WAKEEN interrupt should be processed ASAP to prevent an
372bbd19cdcSRander Wang 	 * interrupt flood, otherwise other interrupts, such IPC,
373bbd19cdcSRander Wang 	 * cannot work normally.  The WAKEEN is handled after the ROM
374bbd19cdcSRander Wang 	 * is initialized successfully, which ensures power rails are
375bbd19cdcSRander Wang 	 * enabled before accessing the SoundWire SHIM registers
376bbd19cdcSRander Wang 	 */
377bbd19cdcSRander Wang 	if (!sdev->first_boot)
378bbd19cdcSRander Wang 		hda_sdw_process_wakeen(sdev);
379bbd19cdcSRander Wang 
380bbd19cdcSRander Wang 	/*
381d16046ffSLiam Girdwood 	 * at this point DSP ROM has been initialized and
382d16046ffSLiam Girdwood 	 * should be ready for code loading and firmware boot
383d16046ffSLiam Girdwood 	 */
384d16046ffSLiam Girdwood 	ret = cl_copy_fw(sdev, stream);
385d16046ffSLiam Girdwood 	if (!ret)
386d16046ffSLiam Girdwood 		dev_dbg(sdev->dev, "Firmware download successful, booting...\n");
387d16046ffSLiam Girdwood 	else
388d16046ffSLiam Girdwood 		dev_err(sdev->dev, "error: load fw failed ret: %d\n", ret);
389d16046ffSLiam Girdwood 
390d16046ffSLiam Girdwood cleanup:
391d16046ffSLiam Girdwood 	/*
392d16046ffSLiam Girdwood 	 * Perform codeloader stream cleanup.
393d16046ffSLiam Girdwood 	 * This should be done even if firmware loading fails.
39476dc6a2bSPierre-Louis Bossart 	 * If the cleanup also fails, we return the initial error
395d16046ffSLiam Girdwood 	 */
396d16046ffSLiam Girdwood 	ret1 = cl_cleanup(sdev, &sdev->dmab, stream);
397d16046ffSLiam Girdwood 	if (ret1 < 0) {
398d16046ffSLiam Girdwood 		dev_err(sdev->dev, "error: Code loader DSP cleanup failed\n");
399d16046ffSLiam Girdwood 
400d16046ffSLiam Girdwood 		/* set return value to indicate cleanup failure */
40176dc6a2bSPierre-Louis Bossart 		if (!ret)
402d16046ffSLiam Girdwood 			ret = ret1;
403d16046ffSLiam Girdwood 	}
404d16046ffSLiam Girdwood 
405d16046ffSLiam Girdwood 	/*
406d16046ffSLiam Girdwood 	 * return master core id if both fw copy
407d16046ffSLiam Girdwood 	 * and stream clean up are successful
408d16046ffSLiam Girdwood 	 */
409d16046ffSLiam Girdwood 	if (!ret)
410d16046ffSLiam Girdwood 		return chip_info->init_core_mask;
411d16046ffSLiam Girdwood 
412d16046ffSLiam Girdwood 	/* dump dsp registers and disable DSP upon error */
413d16046ffSLiam Girdwood err:
414d16046ffSLiam Girdwood 	hda_dsp_dump(sdev, SOF_DBG_REGS | SOF_DBG_PCI | SOF_DBG_MBOX);
415d16046ffSLiam Girdwood 
416d16046ffSLiam Girdwood 	/* disable DSP */
417d16046ffSLiam Girdwood 	snd_sof_dsp_update_bits(sdev, HDA_DSP_PP_BAR,
418d16046ffSLiam Girdwood 				SOF_HDA_REG_PP_PPCTL,
419d16046ffSLiam Girdwood 				SOF_HDA_PPCTL_GPROCEN, 0);
420d16046ffSLiam Girdwood 	return ret;
421d16046ffSLiam Girdwood }
422d16046ffSLiam Girdwood 
423d16046ffSLiam Girdwood /* pre fw run operations */
424d16046ffSLiam Girdwood int hda_dsp_pre_fw_run(struct snd_sof_dev *sdev)
425d16046ffSLiam Girdwood {
426d16046ffSLiam Girdwood 	/* disable clock gating and power gating */
427d16046ffSLiam Girdwood 	return hda_dsp_ctrl_clock_power_gating(sdev, false);
428d16046ffSLiam Girdwood }
429d16046ffSLiam Girdwood 
430d16046ffSLiam Girdwood /* post fw run operations */
431d16046ffSLiam Girdwood int hda_dsp_post_fw_run(struct snd_sof_dev *sdev)
432d16046ffSLiam Girdwood {
43351dfed1eSPierre-Louis Bossart 	int ret;
43451dfed1eSPierre-Louis Bossart 
43551dfed1eSPierre-Louis Bossart 	if (sdev->first_boot) {
43651dfed1eSPierre-Louis Bossart 		ret = hda_sdw_startup(sdev);
43751dfed1eSPierre-Louis Bossart 		if (ret < 0) {
43851dfed1eSPierre-Louis Bossart 			dev_err(sdev->dev,
43951dfed1eSPierre-Louis Bossart 				"error: could not startup SoundWire links\n");
44051dfed1eSPierre-Louis Bossart 			return ret;
44151dfed1eSPierre-Louis Bossart 		}
44251dfed1eSPierre-Louis Bossart 	}
44351dfed1eSPierre-Louis Bossart 
44451dfed1eSPierre-Louis Bossart 	hda_sdw_int_enable(sdev, true);
44551dfed1eSPierre-Louis Bossart 
446d16046ffSLiam Girdwood 	/* re-enable clock gating and power gating */
447d16046ffSLiam Girdwood 	return hda_dsp_ctrl_clock_power_gating(sdev, true);
448d16046ffSLiam Girdwood }
449