xref: /openbmc/linux/sound/soc/intel/skylake/skl-sst.c (revision aecf6fd878eba5182665cccb943205be4c9a0337)
1a750ba5fSSubhransu S. Prusty /*
2a750ba5fSSubhransu S. Prusty  * skl-sst.c - HDA DSP library functions for SKL platform
3a750ba5fSSubhransu S. Prusty  *
4a750ba5fSSubhransu S. Prusty  * Copyright (C) 2014-15, Intel Corporation.
5a750ba5fSSubhransu S. Prusty  * Author:Rafal Redzimski <rafal.f.redzimski@intel.com>
6a750ba5fSSubhransu S. Prusty  *	Jeeja KP <jeeja.kp@intel.com>
7a750ba5fSSubhransu S. Prusty  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8a750ba5fSSubhransu S. Prusty  *
9a750ba5fSSubhransu S. Prusty  * This program is free software; you can redistribute it and/or modify
10a750ba5fSSubhransu S. Prusty  * it under the terms of the GNU General Public License as version 2, as
11a750ba5fSSubhransu S. Prusty  * published by the Free Software Foundation.
12a750ba5fSSubhransu S. Prusty  *
13a750ba5fSSubhransu S. Prusty  * This program is distributed in the hope that it will be useful, but
14a750ba5fSSubhransu S. Prusty  * WITHOUT ANY WARRANTY; without even the implied warranty of
15a750ba5fSSubhransu S. Prusty  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16a750ba5fSSubhransu S. Prusty  * General Public License for more details.
17a750ba5fSSubhransu S. Prusty  */
18a750ba5fSSubhransu S. Prusty 
19a750ba5fSSubhransu S. Prusty #include <linux/module.h>
20a750ba5fSSubhransu S. Prusty #include <linux/delay.h>
21a750ba5fSSubhransu S. Prusty #include <linux/device.h>
22a750ba5fSSubhransu S. Prusty #include "../common/sst-dsp.h"
23a750ba5fSSubhransu S. Prusty #include "../common/sst-dsp-priv.h"
24a750ba5fSSubhransu S. Prusty #include "../common/sst-ipc.h"
25a750ba5fSSubhransu S. Prusty #include "skl-sst-ipc.h"
26a750ba5fSSubhransu S. Prusty 
27a750ba5fSSubhransu S. Prusty #define SKL_BASEFW_TIMEOUT	300
28a750ba5fSSubhransu S. Prusty #define SKL_INIT_TIMEOUT	1000
29a750ba5fSSubhransu S. Prusty 
30a750ba5fSSubhransu S. Prusty /* Intel HD Audio SRAM Window 0*/
31a750ba5fSSubhransu S. Prusty #define SKL_ADSP_SRAM0_BASE	0x8000
32a750ba5fSSubhransu S. Prusty 
33a750ba5fSSubhransu S. Prusty /* Firmware status window */
34a750ba5fSSubhransu S. Prusty #define SKL_ADSP_FW_STATUS	SKL_ADSP_SRAM0_BASE
35a750ba5fSSubhransu S. Prusty #define SKL_ADSP_ERROR_CODE	(SKL_ADSP_FW_STATUS + 0x4)
36a750ba5fSSubhransu S. Prusty 
37a750ba5fSSubhransu S. Prusty #define SKL_INSTANCE_ID		0
38a750ba5fSSubhransu S. Prusty #define SKL_BASE_FW_MODULE_ID	0
39a750ba5fSSubhransu S. Prusty 
40a750ba5fSSubhransu S. Prusty static bool skl_check_fw_status(struct sst_dsp *ctx, u32 status)
41a750ba5fSSubhransu S. Prusty {
42a750ba5fSSubhransu S. Prusty 	u32 cur_sts;
43a750ba5fSSubhransu S. Prusty 
44a750ba5fSSubhransu S. Prusty 	cur_sts = sst_dsp_shim_read(ctx, SKL_ADSP_FW_STATUS) & SKL_FW_STS_MASK;
45a750ba5fSSubhransu S. Prusty 
46a750ba5fSSubhransu S. Prusty 	return (cur_sts == status);
47a750ba5fSSubhransu S. Prusty }
48a750ba5fSSubhransu S. Prusty 
49a750ba5fSSubhransu S. Prusty static int skl_transfer_firmware(struct sst_dsp *ctx,
50a750ba5fSSubhransu S. Prusty 		const void *basefw, u32 base_fw_size)
51a750ba5fSSubhransu S. Prusty {
52a750ba5fSSubhransu S. Prusty 	int ret = 0;
53a750ba5fSSubhransu S. Prusty 
54a750ba5fSSubhransu S. Prusty 	ret = ctx->cl_dev.ops.cl_copy_to_dmabuf(ctx, basefw, base_fw_size);
55a750ba5fSSubhransu S. Prusty 	if (ret < 0)
56a750ba5fSSubhransu S. Prusty 		return ret;
57a750ba5fSSubhransu S. Prusty 
58a750ba5fSSubhransu S. Prusty 	ret = sst_dsp_register_poll(ctx,
59a750ba5fSSubhransu S. Prusty 			SKL_ADSP_FW_STATUS,
60a750ba5fSSubhransu S. Prusty 			SKL_FW_STS_MASK,
61a750ba5fSSubhransu S. Prusty 			SKL_FW_RFW_START,
62a750ba5fSSubhransu S. Prusty 			SKL_BASEFW_TIMEOUT,
63a750ba5fSSubhransu S. Prusty 			"Firmware boot");
64a750ba5fSSubhransu S. Prusty 
65a750ba5fSSubhransu S. Prusty 	ctx->cl_dev.ops.cl_stop_dma(ctx);
66a750ba5fSSubhransu S. Prusty 
67a750ba5fSSubhransu S. Prusty 	return ret;
68a750ba5fSSubhransu S. Prusty }
69a750ba5fSSubhransu S. Prusty 
70a750ba5fSSubhransu S. Prusty static int skl_load_base_firmware(struct sst_dsp *ctx)
71a750ba5fSSubhransu S. Prusty {
72a750ba5fSSubhransu S. Prusty 	int ret = 0, i;
73a750ba5fSSubhransu S. Prusty 	struct skl_sst *skl = ctx->thread_context;
74a750ba5fSSubhransu S. Prusty 	u32 reg;
75a750ba5fSSubhransu S. Prusty 
7684c9e283SJeeja KP 	skl->boot_complete = false;
7784c9e283SJeeja KP 	init_waitqueue_head(&skl->boot_wait);
7884c9e283SJeeja KP 
7984c9e283SJeeja KP 	if (ctx->fw == NULL) {
80*aecf6fd8SVinod Koul 		ret = request_firmware(&ctx->fw, ctx->fw_name, ctx->dev);
81a750ba5fSSubhransu S. Prusty 		if (ret < 0) {
82a750ba5fSSubhransu S. Prusty 			dev_err(ctx->dev, "Request firmware failed %d\n", ret);
83a750ba5fSSubhransu S. Prusty 			skl_dsp_disable_core(ctx);
84a750ba5fSSubhransu S. Prusty 			return -EIO;
85a750ba5fSSubhransu S. Prusty 		}
8684c9e283SJeeja KP 	}
8784c9e283SJeeja KP 
8884c9e283SJeeja KP 	ret = skl_dsp_boot(ctx);
8984c9e283SJeeja KP 	if (ret < 0) {
9084c9e283SJeeja KP 		dev_err(ctx->dev, "Boot dsp core failed ret: %d", ret);
9184c9e283SJeeja KP 		goto skl_load_base_firmware_failed;
9284c9e283SJeeja KP 	}
9384c9e283SJeeja KP 
9484c9e283SJeeja KP 	ret = skl_cldma_prepare(ctx);
9584c9e283SJeeja KP 	if (ret < 0) {
9684c9e283SJeeja KP 		dev_err(ctx->dev, "CL dma prepare failed : %d", ret);
9784c9e283SJeeja KP 		goto skl_load_base_firmware_failed;
9884c9e283SJeeja KP 	}
99a750ba5fSSubhransu S. Prusty 
100a750ba5fSSubhransu S. Prusty 	/* enable Interrupt */
101a750ba5fSSubhransu S. Prusty 	skl_ipc_int_enable(ctx);
102a750ba5fSSubhransu S. Prusty 	skl_ipc_op_int_enable(ctx);
103a750ba5fSSubhransu S. Prusty 
104a750ba5fSSubhransu S. Prusty 	/* check ROM Status */
105a750ba5fSSubhransu S. Prusty 	for (i = SKL_INIT_TIMEOUT; i > 0; --i) {
106a750ba5fSSubhransu S. Prusty 		if (skl_check_fw_status(ctx, SKL_FW_INIT)) {
107a750ba5fSSubhransu S. Prusty 			dev_dbg(ctx->dev,
108a750ba5fSSubhransu S. Prusty 				"ROM loaded, we can continue with FW loading\n");
109a750ba5fSSubhransu S. Prusty 			break;
110a750ba5fSSubhransu S. Prusty 		}
111a750ba5fSSubhransu S. Prusty 		mdelay(1);
112a750ba5fSSubhransu S. Prusty 	}
113a750ba5fSSubhransu S. Prusty 	if (!i) {
114a750ba5fSSubhransu S. Prusty 		reg = sst_dsp_shim_read(ctx, SKL_ADSP_FW_STATUS);
115a750ba5fSSubhransu S. Prusty 		dev_err(ctx->dev,
116a750ba5fSSubhransu S. Prusty 			"Timeout waiting for ROM init done, reg:0x%x\n", reg);
117a750ba5fSSubhransu S. Prusty 		ret = -EIO;
118a750ba5fSSubhransu S. Prusty 		goto skl_load_base_firmware_failed;
119a750ba5fSSubhransu S. Prusty 	}
120a750ba5fSSubhransu S. Prusty 
12184c9e283SJeeja KP 	ret = skl_transfer_firmware(ctx, ctx->fw->data, ctx->fw->size);
122a750ba5fSSubhransu S. Prusty 	if (ret < 0) {
123a750ba5fSSubhransu S. Prusty 		dev_err(ctx->dev, "Transfer firmware failed%d\n", ret);
124a750ba5fSSubhransu S. Prusty 		goto skl_load_base_firmware_failed;
125a750ba5fSSubhransu S. Prusty 	} else {
126a750ba5fSSubhransu S. Prusty 		ret = wait_event_timeout(skl->boot_wait, skl->boot_complete,
127a750ba5fSSubhransu S. Prusty 					msecs_to_jiffies(SKL_IPC_BOOT_MSECS));
128a750ba5fSSubhransu S. Prusty 		if (ret == 0) {
129a750ba5fSSubhransu S. Prusty 			dev_err(ctx->dev, "DSP boot failed, FW Ready timed-out\n");
130a750ba5fSSubhransu S. Prusty 			ret = -EIO;
131a750ba5fSSubhransu S. Prusty 			goto skl_load_base_firmware_failed;
132a750ba5fSSubhransu S. Prusty 		}
133a750ba5fSSubhransu S. Prusty 
134a750ba5fSSubhransu S. Prusty 		dev_dbg(ctx->dev, "Download firmware successful%d\n", ret);
135a750ba5fSSubhransu S. Prusty 		skl_dsp_set_state_locked(ctx, SKL_DSP_RUNNING);
136a750ba5fSSubhransu S. Prusty 	}
137a750ba5fSSubhransu S. Prusty 	return 0;
138a750ba5fSSubhransu S. Prusty 
139a750ba5fSSubhransu S. Prusty skl_load_base_firmware_failed:
140a750ba5fSSubhransu S. Prusty 	skl_dsp_disable_core(ctx);
14184c9e283SJeeja KP 	release_firmware(ctx->fw);
14284c9e283SJeeja KP 	ctx->fw = NULL;
143a750ba5fSSubhransu S. Prusty 	return ret;
144a750ba5fSSubhransu S. Prusty }
145a750ba5fSSubhransu S. Prusty 
146a750ba5fSSubhransu S. Prusty static int skl_set_dsp_D0(struct sst_dsp *ctx)
147a750ba5fSSubhransu S. Prusty {
148a750ba5fSSubhransu S. Prusty 	int ret;
149a750ba5fSSubhransu S. Prusty 
150a750ba5fSSubhransu S. Prusty 	ret = skl_load_base_firmware(ctx);
151a750ba5fSSubhransu S. Prusty 	if (ret < 0) {
152a750ba5fSSubhransu S. Prusty 		dev_err(ctx->dev, "unable to load firmware\n");
153a750ba5fSSubhransu S. Prusty 		return ret;
154a750ba5fSSubhransu S. Prusty 	}
155a750ba5fSSubhransu S. Prusty 
156a750ba5fSSubhransu S. Prusty 	skl_dsp_set_state_locked(ctx, SKL_DSP_RUNNING);
157a750ba5fSSubhransu S. Prusty 
158a750ba5fSSubhransu S. Prusty 	return ret;
159a750ba5fSSubhransu S. Prusty }
160a750ba5fSSubhransu S. Prusty 
161a750ba5fSSubhransu S. Prusty static int skl_set_dsp_D3(struct sst_dsp *ctx)
162a750ba5fSSubhransu S. Prusty {
163a750ba5fSSubhransu S. Prusty 	int ret;
164a750ba5fSSubhransu S. Prusty 	struct skl_ipc_dxstate_info dx;
165a750ba5fSSubhransu S. Prusty 	struct skl_sst *skl = ctx->thread_context;
166a750ba5fSSubhransu S. Prusty 
167a750ba5fSSubhransu S. Prusty 	dev_dbg(ctx->dev, "In %s:\n", __func__);
168a750ba5fSSubhransu S. Prusty 	mutex_lock(&ctx->mutex);
169a750ba5fSSubhransu S. Prusty 	if (!is_skl_dsp_running(ctx)) {
170a750ba5fSSubhransu S. Prusty 		mutex_unlock(&ctx->mutex);
171a750ba5fSSubhransu S. Prusty 		return 0;
172a750ba5fSSubhransu S. Prusty 	}
173a750ba5fSSubhransu S. Prusty 	mutex_unlock(&ctx->mutex);
174a750ba5fSSubhransu S. Prusty 
175a750ba5fSSubhransu S. Prusty 	dx.core_mask = SKL_DSP_CORE0_MASK;
176a750ba5fSSubhransu S. Prusty 	dx.dx_mask = SKL_IPC_D3_MASK;
177a750ba5fSSubhransu S. Prusty 	ret = skl_ipc_set_dx(&skl->ipc, SKL_INSTANCE_ID, SKL_BASE_FW_MODULE_ID, &dx);
178a750ba5fSSubhransu S. Prusty 	if (ret < 0) {
179a750ba5fSSubhransu S. Prusty 		dev_err(ctx->dev, "Failed to set DSP to D3 state\n");
180a750ba5fSSubhransu S. Prusty 		return ret;
181a750ba5fSSubhransu S. Prusty 	}
182a750ba5fSSubhransu S. Prusty 
183a750ba5fSSubhransu S. Prusty 	ret = skl_dsp_disable_core(ctx);
184a750ba5fSSubhransu S. Prusty 	if (ret < 0) {
185a750ba5fSSubhransu S. Prusty 		dev_err(ctx->dev, "disable dsp core failed ret: %d\n", ret);
186a750ba5fSSubhransu S. Prusty 		ret = -EIO;
187a750ba5fSSubhransu S. Prusty 	}
188a750ba5fSSubhransu S. Prusty 	skl_dsp_set_state_locked(ctx, SKL_DSP_RESET);
189a750ba5fSSubhransu S. Prusty 
19084c9e283SJeeja KP 	/* disable Interrupt */
19184c9e283SJeeja KP 	ctx->cl_dev.ops.cl_cleanup_controller(ctx);
19284c9e283SJeeja KP 	skl_cldma_int_disable(ctx);
19384c9e283SJeeja KP 	skl_ipc_op_int_disable(ctx);
19484c9e283SJeeja KP 	skl_ipc_int_disable(ctx);
19584c9e283SJeeja KP 
196a750ba5fSSubhransu S. Prusty 	return ret;
197a750ba5fSSubhransu S. Prusty }
198a750ba5fSSubhransu S. Prusty 
199a750ba5fSSubhransu S. Prusty static unsigned int skl_get_errorcode(struct sst_dsp *ctx)
200a750ba5fSSubhransu S. Prusty {
201a750ba5fSSubhransu S. Prusty 	 return sst_dsp_shim_read(ctx, SKL_ADSP_ERROR_CODE);
202a750ba5fSSubhransu S. Prusty }
203a750ba5fSSubhransu S. Prusty 
204a750ba5fSSubhransu S. Prusty static struct skl_dsp_fw_ops skl_fw_ops = {
205a750ba5fSSubhransu S. Prusty 	.set_state_D0 = skl_set_dsp_D0,
206a750ba5fSSubhransu S. Prusty 	.set_state_D3 = skl_set_dsp_D3,
207a750ba5fSSubhransu S. Prusty 	.load_fw = skl_load_base_firmware,
208a750ba5fSSubhransu S. Prusty 	.get_fw_errcode = skl_get_errorcode,
209a750ba5fSSubhransu S. Prusty };
210a750ba5fSSubhransu S. Prusty 
211a750ba5fSSubhransu S. Prusty static struct sst_ops skl_ops = {
212a750ba5fSSubhransu S. Prusty 	.irq_handler = skl_dsp_sst_interrupt,
213a750ba5fSSubhransu S. Prusty 	.write = sst_shim32_write,
214a750ba5fSSubhransu S. Prusty 	.read = sst_shim32_read,
215a750ba5fSSubhransu S. Prusty 	.ram_read = sst_memcpy_fromio_32,
216a750ba5fSSubhransu S. Prusty 	.ram_write = sst_memcpy_toio_32,
217a750ba5fSSubhransu S. Prusty 	.free = skl_dsp_free,
218a750ba5fSSubhransu S. Prusty };
219a750ba5fSSubhransu S. Prusty 
220a750ba5fSSubhransu S. Prusty static struct sst_dsp_device skl_dev = {
221a750ba5fSSubhransu S. Prusty 	.thread = skl_dsp_irq_thread_handler,
222a750ba5fSSubhransu S. Prusty 	.ops = &skl_ops,
223a750ba5fSSubhransu S. Prusty };
224a750ba5fSSubhransu S. Prusty 
225a750ba5fSSubhransu S. Prusty int skl_sst_dsp_init(struct device *dev, void __iomem *mmio_base, int irq,
226*aecf6fd8SVinod Koul 		const char *fw_name, struct skl_dsp_loader_ops dsp_ops, struct skl_sst **dsp)
227a750ba5fSSubhransu S. Prusty {
228a750ba5fSSubhransu S. Prusty 	struct skl_sst *skl;
229a750ba5fSSubhransu S. Prusty 	struct sst_dsp *sst;
230a750ba5fSSubhransu S. Prusty 	int ret;
231a750ba5fSSubhransu S. Prusty 
232a750ba5fSSubhransu S. Prusty 	skl = devm_kzalloc(dev, sizeof(*skl), GFP_KERNEL);
233a750ba5fSSubhransu S. Prusty 	if (skl == NULL)
234a750ba5fSSubhransu S. Prusty 		return -ENOMEM;
235a750ba5fSSubhransu S. Prusty 
236a750ba5fSSubhransu S. Prusty 	skl->dev = dev;
237a750ba5fSSubhransu S. Prusty 	skl_dev.thread_context = skl;
238a750ba5fSSubhransu S. Prusty 
239a750ba5fSSubhransu S. Prusty 	skl->dsp = skl_dsp_ctx_init(dev, &skl_dev, irq);
240a750ba5fSSubhransu S. Prusty 	if (!skl->dsp) {
241a750ba5fSSubhransu S. Prusty 		dev_err(skl->dev, "%s: no device\n", __func__);
242a750ba5fSSubhransu S. Prusty 		return -ENODEV;
243a750ba5fSSubhransu S. Prusty 	}
244a750ba5fSSubhransu S. Prusty 
245a750ba5fSSubhransu S. Prusty 	sst = skl->dsp;
246a750ba5fSSubhransu S. Prusty 
247*aecf6fd8SVinod Koul 	sst->fw_name = fw_name;
248a750ba5fSSubhransu S. Prusty 	sst->addr.lpe = mmio_base;
249a750ba5fSSubhransu S. Prusty 	sst->addr.shim = mmio_base;
250a750ba5fSSubhransu S. Prusty 	sst_dsp_mailbox_init(sst, (SKL_ADSP_SRAM0_BASE + SKL_ADSP_W0_STAT_SZ),
251a750ba5fSSubhransu S. Prusty 			SKL_ADSP_W0_UP_SZ, SKL_ADSP_SRAM1_BASE, SKL_ADSP_W1_SZ);
252a750ba5fSSubhransu S. Prusty 
253a750ba5fSSubhransu S. Prusty 	sst->dsp_ops = dsp_ops;
254a750ba5fSSubhransu S. Prusty 	sst->fw_ops = skl_fw_ops;
255a750ba5fSSubhransu S. Prusty 
256a750ba5fSSubhransu S. Prusty 	ret = skl_ipc_init(dev, skl);
257a750ba5fSSubhransu S. Prusty 	if (ret)
258a750ba5fSSubhransu S. Prusty 		return ret;
259a750ba5fSSubhransu S. Prusty 
260a750ba5fSSubhransu S. Prusty 	ret = sst->fw_ops.load_fw(sst);
261a750ba5fSSubhransu S. Prusty 	if (ret < 0) {
262a750ba5fSSubhransu S. Prusty 		dev_err(dev, "Load base fw failed : %d", ret);
263b4fe965fSSubhransu S. Prusty 		goto cleanup;
264a750ba5fSSubhransu S. Prusty 	}
265a750ba5fSSubhransu S. Prusty 
266a750ba5fSSubhransu S. Prusty 	if (dsp)
267a750ba5fSSubhransu S. Prusty 		*dsp = skl;
268a750ba5fSSubhransu S. Prusty 
269b4fe965fSSubhransu S. Prusty 	return ret;
270a750ba5fSSubhransu S. Prusty 
271b4fe965fSSubhransu S. Prusty cleanup:
272b4fe965fSSubhransu S. Prusty 	skl_sst_dsp_cleanup(dev, skl);
273a750ba5fSSubhransu S. Prusty 	return ret;
274a750ba5fSSubhransu S. Prusty }
275a750ba5fSSubhransu S. Prusty EXPORT_SYMBOL_GPL(skl_sst_dsp_init);
276a750ba5fSSubhransu S. Prusty 
277a750ba5fSSubhransu S. Prusty void skl_sst_dsp_cleanup(struct device *dev, struct skl_sst *ctx)
278a750ba5fSSubhransu S. Prusty {
279a750ba5fSSubhransu S. Prusty 	skl_ipc_free(&ctx->ipc);
280a750ba5fSSubhransu S. Prusty 	ctx->dsp->cl_dev.ops.cl_cleanup_controller(ctx->dsp);
281a750ba5fSSubhransu S. Prusty 	ctx->dsp->ops->free(ctx->dsp);
282a750ba5fSSubhransu S. Prusty }
283a750ba5fSSubhransu S. Prusty EXPORT_SYMBOL_GPL(skl_sst_dsp_cleanup);
284a750ba5fSSubhransu S. Prusty 
285a750ba5fSSubhransu S. Prusty MODULE_LICENSE("GPL v2");
286a750ba5fSSubhransu S. Prusty MODULE_DESCRIPTION("Intel Skylake IPC driver");
287