xref: /openbmc/linux/sound/soc/intel/skylake/cnl-sst.c (revision 2788808a)
1cb6a5528SGuneshwor Singh /*
2cb6a5528SGuneshwor Singh  * cnl-sst.c - DSP library functions for CNL platform
3cb6a5528SGuneshwor Singh  *
4cb6a5528SGuneshwor Singh  * Copyright (C) 2016-17, Intel Corporation.
5cb6a5528SGuneshwor Singh  *
6cb6a5528SGuneshwor Singh  * Author: Guneshwor Singh <guneshwor.o.singh@intel.com>
7cb6a5528SGuneshwor Singh  *
8cb6a5528SGuneshwor Singh  * Modified from:
9cb6a5528SGuneshwor Singh  *	HDA DSP library functions for SKL platform
10cb6a5528SGuneshwor Singh  *	Copyright (C) 2014-15, Intel Corporation.
11cb6a5528SGuneshwor Singh  *
12cb6a5528SGuneshwor Singh  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
13cb6a5528SGuneshwor Singh  *
14cb6a5528SGuneshwor Singh  * This program is free software; you can redistribute it and/or modify
15cb6a5528SGuneshwor Singh  * it under the terms of the GNU General Public License as version 2, as
16cb6a5528SGuneshwor Singh  * published by the Free Software Foundation.
17cb6a5528SGuneshwor Singh  *
18cb6a5528SGuneshwor Singh  * This program is distributed in the hope that it will be useful, but
19cb6a5528SGuneshwor Singh  * WITHOUT ANY WARRANTY; without even the implied warranty of
20cb6a5528SGuneshwor Singh  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21cb6a5528SGuneshwor Singh  * General Public License for more details.
22cb6a5528SGuneshwor Singh  *
23cb6a5528SGuneshwor Singh  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24cb6a5528SGuneshwor Singh  */
25cb6a5528SGuneshwor Singh 
26cb6a5528SGuneshwor Singh #include <linux/module.h>
27cb6a5528SGuneshwor Singh #include <linux/delay.h>
28cb6a5528SGuneshwor Singh #include <linux/firmware.h>
29cb6a5528SGuneshwor Singh #include <linux/device.h>
30cb6a5528SGuneshwor Singh 
31cb6a5528SGuneshwor Singh #include "../common/sst-dsp.h"
32cb6a5528SGuneshwor Singh #include "../common/sst-dsp-priv.h"
33cb6a5528SGuneshwor Singh #include "../common/sst-ipc.h"
34cb6a5528SGuneshwor Singh #include "cnl-sst-dsp.h"
35cb6a5528SGuneshwor Singh #include "skl-sst-dsp.h"
36cb6a5528SGuneshwor Singh #include "skl-sst-ipc.h"
37cb6a5528SGuneshwor Singh 
38cb6a5528SGuneshwor Singh #define CNL_FW_ROM_INIT		0x1
39cb6a5528SGuneshwor Singh #define CNL_FW_INIT		0x5
40cb6a5528SGuneshwor Singh #define CNL_IPC_PURGE		0x01004000
41cb6a5528SGuneshwor Singh #define CNL_INIT_TIMEOUT	300
42cb6a5528SGuneshwor Singh #define CNL_BASEFW_TIMEOUT	3000
43cb6a5528SGuneshwor Singh 
44cb6a5528SGuneshwor Singh #define CNL_ADSP_SRAM0_BASE	0x80000
45cb6a5528SGuneshwor Singh 
46cb6a5528SGuneshwor Singh /* Firmware status window */
47cb6a5528SGuneshwor Singh #define CNL_ADSP_FW_STATUS	CNL_ADSP_SRAM0_BASE
48cb6a5528SGuneshwor Singh #define CNL_ADSP_ERROR_CODE	(CNL_ADSP_FW_STATUS + 0x4)
49cb6a5528SGuneshwor Singh 
50cb6a5528SGuneshwor Singh #define CNL_INSTANCE_ID		0
51cb6a5528SGuneshwor Singh #define CNL_BASE_FW_MODULE_ID	0
52cb6a5528SGuneshwor Singh #define CNL_ADSP_FW_HDR_OFFSET	0x2000
53cb6a5528SGuneshwor Singh #define CNL_ROM_CTRL_DMA_ID	0x9
54cb6a5528SGuneshwor Singh 
55cb6a5528SGuneshwor Singh static int cnl_prepare_fw(struct sst_dsp *ctx, const void *fwdata, u32 fwsize)
56cb6a5528SGuneshwor Singh {
57cb6a5528SGuneshwor Singh 
58cb6a5528SGuneshwor Singh 	int ret, stream_tag;
59cb6a5528SGuneshwor Singh 
60cb6a5528SGuneshwor Singh 	stream_tag = ctx->dsp_ops.prepare(ctx->dev, 0x40, fwsize, &ctx->dmab);
61cb6a5528SGuneshwor Singh 	if (stream_tag <= 0) {
62cb6a5528SGuneshwor Singh 		dev_err(ctx->dev, "dma prepare failed: 0%#x\n", stream_tag);
63cb6a5528SGuneshwor Singh 		return stream_tag;
64cb6a5528SGuneshwor Singh 	}
65cb6a5528SGuneshwor Singh 
66cb6a5528SGuneshwor Singh 	ctx->dsp_ops.stream_tag = stream_tag;
67cb6a5528SGuneshwor Singh 	memcpy(ctx->dmab.area, fwdata, fwsize);
68cb6a5528SGuneshwor Singh 
69cb6a5528SGuneshwor Singh 	/* purge FW request */
70cb6a5528SGuneshwor Singh 	sst_dsp_shim_write(ctx, CNL_ADSP_REG_HIPCIDR,
71cb6a5528SGuneshwor Singh 			   CNL_ADSP_REG_HIPCIDR_BUSY | (CNL_IPC_PURGE |
72cb6a5528SGuneshwor Singh 			   ((stream_tag - 1) << CNL_ROM_CTRL_DMA_ID)));
73cb6a5528SGuneshwor Singh 
74cb6a5528SGuneshwor Singh 	ret = cnl_dsp_enable_core(ctx, SKL_DSP_CORE0_MASK);
75cb6a5528SGuneshwor Singh 	if (ret < 0) {
76cb6a5528SGuneshwor Singh 		dev_err(ctx->dev, "dsp boot core failed ret: %d\n", ret);
77cb6a5528SGuneshwor Singh 		ret = -EIO;
78cb6a5528SGuneshwor Singh 		goto base_fw_load_failed;
79cb6a5528SGuneshwor Singh 	}
80cb6a5528SGuneshwor Singh 
81cb6a5528SGuneshwor Singh 	/* enable interrupt */
82cb6a5528SGuneshwor Singh 	cnl_ipc_int_enable(ctx);
83cb6a5528SGuneshwor Singh 	cnl_ipc_op_int_enable(ctx);
84cb6a5528SGuneshwor Singh 
85cb6a5528SGuneshwor Singh 	ret = sst_dsp_register_poll(ctx, CNL_ADSP_FW_STATUS, CNL_FW_STS_MASK,
86cb6a5528SGuneshwor Singh 				    CNL_FW_ROM_INIT, CNL_INIT_TIMEOUT,
87cb6a5528SGuneshwor Singh 				    "rom load");
88cb6a5528SGuneshwor Singh 	if (ret < 0) {
89cb6a5528SGuneshwor Singh 		dev_err(ctx->dev, "rom init timeout, ret: %d\n", ret);
90cb6a5528SGuneshwor Singh 		goto base_fw_load_failed;
91cb6a5528SGuneshwor Singh 	}
92cb6a5528SGuneshwor Singh 
93cb6a5528SGuneshwor Singh 	return 0;
94cb6a5528SGuneshwor Singh 
95cb6a5528SGuneshwor Singh base_fw_load_failed:
96cb6a5528SGuneshwor Singh 	ctx->dsp_ops.cleanup(ctx->dev, &ctx->dmab, stream_tag);
97cb6a5528SGuneshwor Singh 	cnl_dsp_disable_core(ctx, SKL_DSP_CORE0_MASK);
98cb6a5528SGuneshwor Singh 
99cb6a5528SGuneshwor Singh 	return ret;
100cb6a5528SGuneshwor Singh }
101cb6a5528SGuneshwor Singh 
102cb6a5528SGuneshwor Singh static int sst_transfer_fw_host_dma(struct sst_dsp *ctx)
103cb6a5528SGuneshwor Singh {
104cb6a5528SGuneshwor Singh 	int ret;
105cb6a5528SGuneshwor Singh 
106cb6a5528SGuneshwor Singh 	ctx->dsp_ops.trigger(ctx->dev, true, ctx->dsp_ops.stream_tag);
107cb6a5528SGuneshwor Singh 	ret = sst_dsp_register_poll(ctx, CNL_ADSP_FW_STATUS, CNL_FW_STS_MASK,
108cb6a5528SGuneshwor Singh 				    CNL_FW_INIT, CNL_BASEFW_TIMEOUT,
109cb6a5528SGuneshwor Singh 				    "firmware boot");
110cb6a5528SGuneshwor Singh 
111cb6a5528SGuneshwor Singh 	ctx->dsp_ops.trigger(ctx->dev, false, ctx->dsp_ops.stream_tag);
112cb6a5528SGuneshwor Singh 	ctx->dsp_ops.cleanup(ctx->dev, &ctx->dmab, ctx->dsp_ops.stream_tag);
113cb6a5528SGuneshwor Singh 
114cb6a5528SGuneshwor Singh 	return ret;
115cb6a5528SGuneshwor Singh }
116cb6a5528SGuneshwor Singh 
117cb6a5528SGuneshwor Singh static int cnl_load_base_firmware(struct sst_dsp *ctx)
118cb6a5528SGuneshwor Singh {
119cb6a5528SGuneshwor Singh 	struct firmware stripped_fw;
120cb6a5528SGuneshwor Singh 	struct skl_sst *cnl = ctx->thread_context;
121cb6a5528SGuneshwor Singh 	int ret;
122cb6a5528SGuneshwor Singh 
123cb6a5528SGuneshwor Singh 	if (!ctx->fw) {
124cb6a5528SGuneshwor Singh 		ret = request_firmware(&ctx->fw, ctx->fw_name, ctx->dev);
125cb6a5528SGuneshwor Singh 		if (ret < 0) {
126cb6a5528SGuneshwor Singh 			dev_err(ctx->dev, "request firmware failed: %d\n", ret);
127cb6a5528SGuneshwor Singh 			goto cnl_load_base_firmware_failed;
128cb6a5528SGuneshwor Singh 		}
129cb6a5528SGuneshwor Singh 	}
130cb6a5528SGuneshwor Singh 
131cb6a5528SGuneshwor Singh 	/* parse uuids if first boot */
132cb6a5528SGuneshwor Singh 	if (cnl->is_first_boot) {
133cb6a5528SGuneshwor Singh 		ret = snd_skl_parse_uuids(ctx, ctx->fw,
134cb6a5528SGuneshwor Singh 					  CNL_ADSP_FW_HDR_OFFSET, 0);
135cb6a5528SGuneshwor Singh 		if (ret < 0)
136cb6a5528SGuneshwor Singh 			goto cnl_load_base_firmware_failed;
137cb6a5528SGuneshwor Singh 	}
138cb6a5528SGuneshwor Singh 
139cb6a5528SGuneshwor Singh 	stripped_fw.data = ctx->fw->data;
140cb6a5528SGuneshwor Singh 	stripped_fw.size = ctx->fw->size;
141cb6a5528SGuneshwor Singh 	skl_dsp_strip_extended_manifest(&stripped_fw);
142cb6a5528SGuneshwor Singh 
143cb6a5528SGuneshwor Singh 	ret = cnl_prepare_fw(ctx, stripped_fw.data, stripped_fw.size);
144cb6a5528SGuneshwor Singh 	if (ret < 0) {
145cb6a5528SGuneshwor Singh 		dev_err(ctx->dev, "prepare firmware failed: %d\n", ret);
146cb6a5528SGuneshwor Singh 		goto cnl_load_base_firmware_failed;
147cb6a5528SGuneshwor Singh 	}
148cb6a5528SGuneshwor Singh 
149cb6a5528SGuneshwor Singh 	ret = sst_transfer_fw_host_dma(ctx);
150cb6a5528SGuneshwor Singh 	if (ret < 0) {
151cb6a5528SGuneshwor Singh 		dev_err(ctx->dev, "transfer firmware failed: %d\n", ret);
152cb6a5528SGuneshwor Singh 		cnl_dsp_disable_core(ctx, SKL_DSP_CORE0_MASK);
153cb6a5528SGuneshwor Singh 		goto cnl_load_base_firmware_failed;
154cb6a5528SGuneshwor Singh 	}
155cb6a5528SGuneshwor Singh 
156cb6a5528SGuneshwor Singh 	ret = wait_event_timeout(cnl->boot_wait, cnl->boot_complete,
157cb6a5528SGuneshwor Singh 				 msecs_to_jiffies(SKL_IPC_BOOT_MSECS));
158cb6a5528SGuneshwor Singh 	if (ret == 0) {
159cb6a5528SGuneshwor Singh 		dev_err(ctx->dev, "FW ready timed-out\n");
160cb6a5528SGuneshwor Singh 		cnl_dsp_disable_core(ctx, SKL_DSP_CORE0_MASK);
161cb6a5528SGuneshwor Singh 		ret = -EIO;
162cb6a5528SGuneshwor Singh 		goto cnl_load_base_firmware_failed;
163cb6a5528SGuneshwor Singh 	}
164cb6a5528SGuneshwor Singh 
165cb6a5528SGuneshwor Singh 	cnl->fw_loaded = true;
166cb6a5528SGuneshwor Singh 
167cb6a5528SGuneshwor Singh 	return 0;
168cb6a5528SGuneshwor Singh 
169cb6a5528SGuneshwor Singh cnl_load_base_firmware_failed:
170cb6a5528SGuneshwor Singh 	release_firmware(ctx->fw);
171cb6a5528SGuneshwor Singh 	ctx->fw = NULL;
172cb6a5528SGuneshwor Singh 
173cb6a5528SGuneshwor Singh 	return ret;
174cb6a5528SGuneshwor Singh }
175cb6a5528SGuneshwor Singh 
176cb6a5528SGuneshwor Singh static int cnl_set_dsp_D0(struct sst_dsp *ctx, unsigned int core_id)
177cb6a5528SGuneshwor Singh {
178cb6a5528SGuneshwor Singh 	struct skl_sst *cnl = ctx->thread_context;
179cb6a5528SGuneshwor Singh 	unsigned int core_mask = SKL_DSP_CORE_MASK(core_id);
180cb6a5528SGuneshwor Singh 	struct skl_ipc_dxstate_info dx;
181cb6a5528SGuneshwor Singh 	int ret;
182cb6a5528SGuneshwor Singh 
183cb6a5528SGuneshwor Singh 	if (!cnl->fw_loaded) {
184cb6a5528SGuneshwor Singh 		cnl->boot_complete = false;
185cb6a5528SGuneshwor Singh 		ret = cnl_load_base_firmware(ctx);
186cb6a5528SGuneshwor Singh 		if (ret < 0) {
187cb6a5528SGuneshwor Singh 			dev_err(ctx->dev, "fw reload failed: %d\n", ret);
188cb6a5528SGuneshwor Singh 			return ret;
189cb6a5528SGuneshwor Singh 		}
190cb6a5528SGuneshwor Singh 
191cb6a5528SGuneshwor Singh 		cnl->cores.state[core_id] = SKL_DSP_RUNNING;
192cb6a5528SGuneshwor Singh 		return ret;
193cb6a5528SGuneshwor Singh 	}
194cb6a5528SGuneshwor Singh 
195cb6a5528SGuneshwor Singh 	ret = cnl_dsp_enable_core(ctx, core_mask);
196cb6a5528SGuneshwor Singh 	if (ret < 0) {
197cb6a5528SGuneshwor Singh 		dev_err(ctx->dev, "enable dsp core %d failed: %d\n",
198cb6a5528SGuneshwor Singh 			core_id, ret);
199cb6a5528SGuneshwor Singh 		goto err;
200cb6a5528SGuneshwor Singh 	}
201cb6a5528SGuneshwor Singh 
202cb6a5528SGuneshwor Singh 	if (core_id == SKL_DSP_CORE0_ID) {
203cb6a5528SGuneshwor Singh 		/* enable interrupt */
204cb6a5528SGuneshwor Singh 		cnl_ipc_int_enable(ctx);
205cb6a5528SGuneshwor Singh 		cnl_ipc_op_int_enable(ctx);
206cb6a5528SGuneshwor Singh 		cnl->boot_complete = false;
207cb6a5528SGuneshwor Singh 
208cb6a5528SGuneshwor Singh 		ret = wait_event_timeout(cnl->boot_wait, cnl->boot_complete,
209cb6a5528SGuneshwor Singh 					 msecs_to_jiffies(SKL_IPC_BOOT_MSECS));
210cb6a5528SGuneshwor Singh 		if (ret == 0) {
211cb6a5528SGuneshwor Singh 			dev_err(ctx->dev,
212cb6a5528SGuneshwor Singh 				"dsp boot timeout, status=%#x error=%#x\n",
213cb6a5528SGuneshwor Singh 				sst_dsp_shim_read(ctx, CNL_ADSP_FW_STATUS),
214cb6a5528SGuneshwor Singh 				sst_dsp_shim_read(ctx, CNL_ADSP_ERROR_CODE));
215cb6a5528SGuneshwor Singh 			goto err;
216cb6a5528SGuneshwor Singh 		}
217cb6a5528SGuneshwor Singh 	} else {
218cb6a5528SGuneshwor Singh 		dx.core_mask = core_mask;
219cb6a5528SGuneshwor Singh 		dx.dx_mask = core_mask;
220cb6a5528SGuneshwor Singh 
221cb6a5528SGuneshwor Singh 		ret = skl_ipc_set_dx(&cnl->ipc, CNL_INSTANCE_ID,
222cb6a5528SGuneshwor Singh 				     CNL_BASE_FW_MODULE_ID, &dx);
223cb6a5528SGuneshwor Singh 		if (ret < 0) {
224cb6a5528SGuneshwor Singh 			dev_err(ctx->dev, "set_dx failed, core: %d ret: %d\n",
225cb6a5528SGuneshwor Singh 				core_id, ret);
226cb6a5528SGuneshwor Singh 			goto err;
227cb6a5528SGuneshwor Singh 		}
228cb6a5528SGuneshwor Singh 	}
229cb6a5528SGuneshwor Singh 	cnl->cores.state[core_id] = SKL_DSP_RUNNING;
230cb6a5528SGuneshwor Singh 
231cb6a5528SGuneshwor Singh 	return 0;
232cb6a5528SGuneshwor Singh err:
233cb6a5528SGuneshwor Singh 	cnl_dsp_disable_core(ctx, core_mask);
234cb6a5528SGuneshwor Singh 
235cb6a5528SGuneshwor Singh 	return ret;
236cb6a5528SGuneshwor Singh }
237cb6a5528SGuneshwor Singh 
238cb6a5528SGuneshwor Singh static int cnl_set_dsp_D3(struct sst_dsp *ctx, unsigned int core_id)
239cb6a5528SGuneshwor Singh {
240cb6a5528SGuneshwor Singh 	struct skl_sst *cnl = ctx->thread_context;
241cb6a5528SGuneshwor Singh 	unsigned int core_mask = SKL_DSP_CORE_MASK(core_id);
242cb6a5528SGuneshwor Singh 	struct skl_ipc_dxstate_info dx;
243cb6a5528SGuneshwor Singh 	int ret;
244cb6a5528SGuneshwor Singh 
245cb6a5528SGuneshwor Singh 	dx.core_mask = core_mask;
246cb6a5528SGuneshwor Singh 	dx.dx_mask = SKL_IPC_D3_MASK;
247cb6a5528SGuneshwor Singh 
248cb6a5528SGuneshwor Singh 	ret = skl_ipc_set_dx(&cnl->ipc, CNL_INSTANCE_ID,
249cb6a5528SGuneshwor Singh 			     CNL_BASE_FW_MODULE_ID, &dx);
250cb6a5528SGuneshwor Singh 	if (ret < 0) {
251cb6a5528SGuneshwor Singh 		dev_err(ctx->dev,
252cb6a5528SGuneshwor Singh 			"dsp core %d to d3 failed; continue reset\n",
253cb6a5528SGuneshwor Singh 			core_id);
254cb6a5528SGuneshwor Singh 		cnl->fw_loaded = false;
255cb6a5528SGuneshwor Singh 	}
256cb6a5528SGuneshwor Singh 
257cb6a5528SGuneshwor Singh 	/* disable interrupts if core 0 */
258cb6a5528SGuneshwor Singh 	if (core_id == SKL_DSP_CORE0_ID) {
259cb6a5528SGuneshwor Singh 		skl_ipc_op_int_disable(ctx);
260cb6a5528SGuneshwor Singh 		skl_ipc_int_disable(ctx);
261cb6a5528SGuneshwor Singh 	}
262cb6a5528SGuneshwor Singh 
263cb6a5528SGuneshwor Singh 	ret = cnl_dsp_disable_core(ctx, core_mask);
264cb6a5528SGuneshwor Singh 	if (ret < 0) {
265cb6a5528SGuneshwor Singh 		dev_err(ctx->dev, "disable dsp core %d failed: %d\n",
266cb6a5528SGuneshwor Singh 			core_id, ret);
267cb6a5528SGuneshwor Singh 		return ret;
268cb6a5528SGuneshwor Singh 	}
269cb6a5528SGuneshwor Singh 
270cb6a5528SGuneshwor Singh 	cnl->cores.state[core_id] = SKL_DSP_RESET;
271cb6a5528SGuneshwor Singh 
272cb6a5528SGuneshwor Singh 	return ret;
273cb6a5528SGuneshwor Singh }
274cb6a5528SGuneshwor Singh 
275cb6a5528SGuneshwor Singh static unsigned int cnl_get_errno(struct sst_dsp *ctx)
276cb6a5528SGuneshwor Singh {
277cb6a5528SGuneshwor Singh 	return sst_dsp_shim_read(ctx, CNL_ADSP_ERROR_CODE);
278cb6a5528SGuneshwor Singh }
279cb6a5528SGuneshwor Singh 
2802788808aSBhumika Goyal static const struct skl_dsp_fw_ops cnl_fw_ops = {
281cb6a5528SGuneshwor Singh 	.set_state_D0 = cnl_set_dsp_D0,
282cb6a5528SGuneshwor Singh 	.set_state_D3 = cnl_set_dsp_D3,
283cb6a5528SGuneshwor Singh 	.load_fw = cnl_load_base_firmware,
284cb6a5528SGuneshwor Singh 	.get_fw_errcode = cnl_get_errno,
285cb6a5528SGuneshwor Singh };
286cb6a5528SGuneshwor Singh 
287cb6a5528SGuneshwor Singh static struct sst_ops cnl_ops = {
288cb6a5528SGuneshwor Singh 	.irq_handler = cnl_dsp_sst_interrupt,
289cb6a5528SGuneshwor Singh 	.write = sst_shim32_write,
290cb6a5528SGuneshwor Singh 	.read = sst_shim32_read,
291cb6a5528SGuneshwor Singh 	.ram_read = sst_memcpy_fromio_32,
292cb6a5528SGuneshwor Singh 	.ram_write = sst_memcpy_toio_32,
293cb6a5528SGuneshwor Singh 	.free = cnl_dsp_free,
294cb6a5528SGuneshwor Singh };
295cb6a5528SGuneshwor Singh 
296cb6a5528SGuneshwor Singh #define CNL_IPC_GLB_NOTIFY_RSP_SHIFT	29
297cb6a5528SGuneshwor Singh #define CNL_IPC_GLB_NOTIFY_RSP_MASK	0x1
298cb6a5528SGuneshwor Singh #define CNL_IPC_GLB_NOTIFY_RSP_TYPE(x)	(((x) >> CNL_IPC_GLB_NOTIFY_RSP_SHIFT) \
299cb6a5528SGuneshwor Singh 					& CNL_IPC_GLB_NOTIFY_RSP_MASK)
300cb6a5528SGuneshwor Singh 
301cb6a5528SGuneshwor Singh static irqreturn_t cnl_dsp_irq_thread_handler(int irq, void *context)
302cb6a5528SGuneshwor Singh {
303cb6a5528SGuneshwor Singh 	struct sst_dsp *dsp = context;
304cb6a5528SGuneshwor Singh 	struct skl_sst *cnl = sst_dsp_get_thread_context(dsp);
305cb6a5528SGuneshwor Singh 	struct sst_generic_ipc *ipc = &cnl->ipc;
306cb6a5528SGuneshwor Singh 	struct skl_ipc_header header = {0};
307cb6a5528SGuneshwor Singh 	u32 hipcida, hipctdr, hipctdd;
308cb6a5528SGuneshwor Singh 	int ipc_irq = 0;
309cb6a5528SGuneshwor Singh 
310cb6a5528SGuneshwor Singh 	/* here we handle ipc interrupts only */
311cb6a5528SGuneshwor Singh 	if (!(dsp->intr_status & CNL_ADSPIS_IPC))
312cb6a5528SGuneshwor Singh 		return IRQ_NONE;
313cb6a5528SGuneshwor Singh 
314cb6a5528SGuneshwor Singh 	hipcida = sst_dsp_shim_read_unlocked(dsp, CNL_ADSP_REG_HIPCIDA);
315cb6a5528SGuneshwor Singh 	hipctdr = sst_dsp_shim_read_unlocked(dsp, CNL_ADSP_REG_HIPCTDR);
316cb6a5528SGuneshwor Singh 
317cb6a5528SGuneshwor Singh 	/* reply message from dsp */
318cb6a5528SGuneshwor Singh 	if (hipcida & CNL_ADSP_REG_HIPCIDA_DONE) {
319cb6a5528SGuneshwor Singh 		sst_dsp_shim_update_bits(dsp, CNL_ADSP_REG_HIPCCTL,
320cb6a5528SGuneshwor Singh 			CNL_ADSP_REG_HIPCCTL_DONE, 0);
321cb6a5528SGuneshwor Singh 
322cb6a5528SGuneshwor Singh 		/* clear done bit - tell dsp operation is complete */
323cb6a5528SGuneshwor Singh 		sst_dsp_shim_update_bits_forced(dsp, CNL_ADSP_REG_HIPCIDA,
324cb6a5528SGuneshwor Singh 			CNL_ADSP_REG_HIPCIDA_DONE, CNL_ADSP_REG_HIPCIDA_DONE);
325cb6a5528SGuneshwor Singh 
326cb6a5528SGuneshwor Singh 		ipc_irq = 1;
327cb6a5528SGuneshwor Singh 
328cb6a5528SGuneshwor Singh 		/* unmask done interrupt */
329cb6a5528SGuneshwor Singh 		sst_dsp_shim_update_bits(dsp, CNL_ADSP_REG_HIPCCTL,
330cb6a5528SGuneshwor Singh 			CNL_ADSP_REG_HIPCCTL_DONE, CNL_ADSP_REG_HIPCCTL_DONE);
331cb6a5528SGuneshwor Singh 	}
332cb6a5528SGuneshwor Singh 
333cb6a5528SGuneshwor Singh 	/* new message from dsp */
334cb6a5528SGuneshwor Singh 	if (hipctdr & CNL_ADSP_REG_HIPCTDR_BUSY) {
335cb6a5528SGuneshwor Singh 		hipctdd = sst_dsp_shim_read_unlocked(dsp, CNL_ADSP_REG_HIPCTDD);
336cb6a5528SGuneshwor Singh 		header.primary = hipctdr;
337cb6a5528SGuneshwor Singh 		header.extension = hipctdd;
338cb6a5528SGuneshwor Singh 		dev_dbg(dsp->dev, "IPC irq: Firmware respond primary:%x",
339cb6a5528SGuneshwor Singh 						header.primary);
340cb6a5528SGuneshwor Singh 		dev_dbg(dsp->dev, "IPC irq: Firmware respond extension:%x",
341cb6a5528SGuneshwor Singh 						header.extension);
342cb6a5528SGuneshwor Singh 
343cb6a5528SGuneshwor Singh 		if (CNL_IPC_GLB_NOTIFY_RSP_TYPE(header.primary)) {
344cb6a5528SGuneshwor Singh 			/* Handle Immediate reply from DSP Core */
345cb6a5528SGuneshwor Singh 			skl_ipc_process_reply(ipc, header);
346cb6a5528SGuneshwor Singh 		} else {
347cb6a5528SGuneshwor Singh 			dev_dbg(dsp->dev, "IPC irq: Notification from firmware\n");
348cb6a5528SGuneshwor Singh 			skl_ipc_process_notification(ipc, header);
349cb6a5528SGuneshwor Singh 		}
350cb6a5528SGuneshwor Singh 		/* clear busy interrupt */
351cb6a5528SGuneshwor Singh 		sst_dsp_shim_update_bits_forced(dsp, CNL_ADSP_REG_HIPCTDR,
352cb6a5528SGuneshwor Singh 			CNL_ADSP_REG_HIPCTDR_BUSY, CNL_ADSP_REG_HIPCTDR_BUSY);
353cb6a5528SGuneshwor Singh 
354cb6a5528SGuneshwor Singh 		/* set done bit to ack dsp */
355cb6a5528SGuneshwor Singh 		sst_dsp_shim_update_bits_forced(dsp, CNL_ADSP_REG_HIPCTDA,
356cb6a5528SGuneshwor Singh 			CNL_ADSP_REG_HIPCTDA_DONE, CNL_ADSP_REG_HIPCTDA_DONE);
357cb6a5528SGuneshwor Singh 		ipc_irq = 1;
358cb6a5528SGuneshwor Singh 	}
359cb6a5528SGuneshwor Singh 
360cb6a5528SGuneshwor Singh 	if (ipc_irq == 0)
361cb6a5528SGuneshwor Singh 		return IRQ_NONE;
362cb6a5528SGuneshwor Singh 
363cb6a5528SGuneshwor Singh 	cnl_ipc_int_enable(dsp);
364cb6a5528SGuneshwor Singh 
365cb6a5528SGuneshwor Singh 	/* continue to send any remaining messages */
366cb6a5528SGuneshwor Singh 	schedule_work(&ipc->kwork);
367cb6a5528SGuneshwor Singh 
368cb6a5528SGuneshwor Singh 	return IRQ_HANDLED;
369cb6a5528SGuneshwor Singh }
370cb6a5528SGuneshwor Singh 
371cb6a5528SGuneshwor Singh static struct sst_dsp_device cnl_dev = {
372cb6a5528SGuneshwor Singh 	.thread = cnl_dsp_irq_thread_handler,
373cb6a5528SGuneshwor Singh 	.ops = &cnl_ops,
374cb6a5528SGuneshwor Singh };
375cb6a5528SGuneshwor Singh 
376cb6a5528SGuneshwor Singh static void cnl_ipc_tx_msg(struct sst_generic_ipc *ipc, struct ipc_message *msg)
377cb6a5528SGuneshwor Singh {
378cb6a5528SGuneshwor Singh 	struct skl_ipc_header *header = (struct skl_ipc_header *)(&msg->header);
379cb6a5528SGuneshwor Singh 
380cb6a5528SGuneshwor Singh 	if (msg->tx_size)
381cb6a5528SGuneshwor Singh 		sst_dsp_outbox_write(ipc->dsp, msg->tx_data, msg->tx_size);
382cb6a5528SGuneshwor Singh 	sst_dsp_shim_write_unlocked(ipc->dsp, CNL_ADSP_REG_HIPCIDD,
383cb6a5528SGuneshwor Singh 				    header->extension);
384cb6a5528SGuneshwor Singh 	sst_dsp_shim_write_unlocked(ipc->dsp, CNL_ADSP_REG_HIPCIDR,
385cb6a5528SGuneshwor Singh 				header->primary | CNL_ADSP_REG_HIPCIDR_BUSY);
386cb6a5528SGuneshwor Singh }
387cb6a5528SGuneshwor Singh 
388cb6a5528SGuneshwor Singh static bool cnl_ipc_is_dsp_busy(struct sst_dsp *dsp)
389cb6a5528SGuneshwor Singh {
390cb6a5528SGuneshwor Singh 	u32 hipcidr;
391cb6a5528SGuneshwor Singh 
392cb6a5528SGuneshwor Singh 	hipcidr = sst_dsp_shim_read_unlocked(dsp, CNL_ADSP_REG_HIPCIDR);
393cb6a5528SGuneshwor Singh 
394cb6a5528SGuneshwor Singh 	return (hipcidr & CNL_ADSP_REG_HIPCIDR_BUSY);
395cb6a5528SGuneshwor Singh }
396cb6a5528SGuneshwor Singh 
397cb6a5528SGuneshwor Singh static int cnl_ipc_init(struct device *dev, struct skl_sst *cnl)
398cb6a5528SGuneshwor Singh {
399cb6a5528SGuneshwor Singh 	struct sst_generic_ipc *ipc;
400cb6a5528SGuneshwor Singh 	int err;
401cb6a5528SGuneshwor Singh 
402cb6a5528SGuneshwor Singh 	ipc = &cnl->ipc;
403cb6a5528SGuneshwor Singh 	ipc->dsp = cnl->dsp;
404cb6a5528SGuneshwor Singh 	ipc->dev = dev;
405cb6a5528SGuneshwor Singh 
406cb6a5528SGuneshwor Singh 	ipc->tx_data_max_size = CNL_ADSP_W1_SZ;
407cb6a5528SGuneshwor Singh 	ipc->rx_data_max_size = CNL_ADSP_W0_UP_SZ;
408cb6a5528SGuneshwor Singh 
409cb6a5528SGuneshwor Singh 	err = sst_ipc_init(ipc);
410cb6a5528SGuneshwor Singh 	if (err)
411cb6a5528SGuneshwor Singh 		return err;
412cb6a5528SGuneshwor Singh 
413cb6a5528SGuneshwor Singh 	/*
414cb6a5528SGuneshwor Singh 	 * overriding tx_msg and is_dsp_busy since
415cb6a5528SGuneshwor Singh 	 * ipc registers are different for cnl
416cb6a5528SGuneshwor Singh 	 */
417cb6a5528SGuneshwor Singh 	ipc->ops.tx_msg = cnl_ipc_tx_msg;
418cb6a5528SGuneshwor Singh 	ipc->ops.tx_data_copy = skl_ipc_tx_data_copy;
419cb6a5528SGuneshwor Singh 	ipc->ops.is_dsp_busy = cnl_ipc_is_dsp_busy;
420cb6a5528SGuneshwor Singh 
421cb6a5528SGuneshwor Singh 	return 0;
422cb6a5528SGuneshwor Singh }
423cb6a5528SGuneshwor Singh 
424cb6a5528SGuneshwor Singh int cnl_sst_dsp_init(struct device *dev, void __iomem *mmio_base, int irq,
425cb6a5528SGuneshwor Singh 		     const char *fw_name, struct skl_dsp_loader_ops dsp_ops,
426cb6a5528SGuneshwor Singh 		     struct skl_sst **dsp)
427cb6a5528SGuneshwor Singh {
428cb6a5528SGuneshwor Singh 	struct skl_sst *cnl;
429cb6a5528SGuneshwor Singh 	struct sst_dsp *sst;
430cb6a5528SGuneshwor Singh 	int ret;
431cb6a5528SGuneshwor Singh 
432cb6a5528SGuneshwor Singh 	ret = skl_sst_ctx_init(dev, irq, fw_name, dsp_ops, dsp, &cnl_dev);
433cb6a5528SGuneshwor Singh 	if (ret < 0) {
434cb6a5528SGuneshwor Singh 		dev_err(dev, "%s: no device\n", __func__);
435cb6a5528SGuneshwor Singh 		return ret;
436cb6a5528SGuneshwor Singh 	}
437cb6a5528SGuneshwor Singh 
438cb6a5528SGuneshwor Singh 	cnl = *dsp;
439cb6a5528SGuneshwor Singh 	sst = cnl->dsp;
440cb6a5528SGuneshwor Singh 	sst->fw_ops = cnl_fw_ops;
441cb6a5528SGuneshwor Singh 	sst->addr.lpe = mmio_base;
442cb6a5528SGuneshwor Singh 	sst->addr.shim = mmio_base;
443cb6a5528SGuneshwor Singh 	sst->addr.sram0_base = CNL_ADSP_SRAM0_BASE;
444cb6a5528SGuneshwor Singh 	sst->addr.sram1_base = CNL_ADSP_SRAM1_BASE;
445cb6a5528SGuneshwor Singh 	sst->addr.w0_stat_sz = CNL_ADSP_W0_STAT_SZ;
446cb6a5528SGuneshwor Singh 	sst->addr.w0_up_sz = CNL_ADSP_W0_UP_SZ;
447cb6a5528SGuneshwor Singh 
448cb6a5528SGuneshwor Singh 	sst_dsp_mailbox_init(sst, (CNL_ADSP_SRAM0_BASE + CNL_ADSP_W0_STAT_SZ),
449cb6a5528SGuneshwor Singh 			     CNL_ADSP_W0_UP_SZ, CNL_ADSP_SRAM1_BASE,
450cb6a5528SGuneshwor Singh 			     CNL_ADSP_W1_SZ);
451cb6a5528SGuneshwor Singh 
452cb6a5528SGuneshwor Singh 	ret = cnl_ipc_init(dev, cnl);
453cb6a5528SGuneshwor Singh 	if (ret)
454cb6a5528SGuneshwor Singh 		return ret;
455cb6a5528SGuneshwor Singh 
456cb6a5528SGuneshwor Singh 	cnl->boot_complete = false;
457cb6a5528SGuneshwor Singh 	init_waitqueue_head(&cnl->boot_wait);
458cb6a5528SGuneshwor Singh 
459cb6a5528SGuneshwor Singh 	return 0;
460cb6a5528SGuneshwor Singh }
461cb6a5528SGuneshwor Singh EXPORT_SYMBOL_GPL(cnl_sst_dsp_init);
462cb6a5528SGuneshwor Singh 
463cb6a5528SGuneshwor Singh int cnl_sst_init_fw(struct device *dev, struct skl_sst *ctx)
464cb6a5528SGuneshwor Singh {
465cb6a5528SGuneshwor Singh 	int ret;
466cb6a5528SGuneshwor Singh 	struct sst_dsp *sst = ctx->dsp;
467cb6a5528SGuneshwor Singh 
468cb6a5528SGuneshwor Singh 	ret = ctx->dsp->fw_ops.load_fw(sst);
469cb6a5528SGuneshwor Singh 	if (ret < 0) {
470cb6a5528SGuneshwor Singh 		dev_err(dev, "load base fw failed: %d", ret);
471cb6a5528SGuneshwor Singh 		return ret;
472cb6a5528SGuneshwor Singh 	}
473cb6a5528SGuneshwor Singh 
474cb6a5528SGuneshwor Singh 	skl_dsp_init_core_state(sst);
475cb6a5528SGuneshwor Singh 
476cb6a5528SGuneshwor Singh 	ctx->is_first_boot = false;
477cb6a5528SGuneshwor Singh 
478cb6a5528SGuneshwor Singh 	return 0;
479cb6a5528SGuneshwor Singh }
480cb6a5528SGuneshwor Singh EXPORT_SYMBOL_GPL(cnl_sst_init_fw);
481cb6a5528SGuneshwor Singh 
482cb6a5528SGuneshwor Singh void cnl_sst_dsp_cleanup(struct device *dev, struct skl_sst *ctx)
483cb6a5528SGuneshwor Singh {
484cb6a5528SGuneshwor Singh 	if (ctx->dsp->fw)
485cb6a5528SGuneshwor Singh 		release_firmware(ctx->dsp->fw);
486cb6a5528SGuneshwor Singh 
487cb6a5528SGuneshwor Singh 	skl_freeup_uuid_list(ctx);
488cb6a5528SGuneshwor Singh 	cnl_ipc_free(&ctx->ipc);
489cb6a5528SGuneshwor Singh 
490cb6a5528SGuneshwor Singh 	ctx->dsp->ops->free(ctx->dsp);
491cb6a5528SGuneshwor Singh }
492cb6a5528SGuneshwor Singh EXPORT_SYMBOL_GPL(cnl_sst_dsp_cleanup);
493cb6a5528SGuneshwor Singh 
494cb6a5528SGuneshwor Singh MODULE_LICENSE("GPL v2");
495cb6a5528SGuneshwor Singh MODULE_DESCRIPTION("Intel Cannonlake IPC driver");
496