2 * skl-sst.c - HDA DSP library functions for SKL platform
4 * Copyright (C) 2014-15, Intel Corporation.
5 * Author:Rafal Redzimski <rafal.f.redzimski@intel.com>
6 * Jeeja KP <jeeja.kp@intel.com>
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as version 2, as
11 * published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
19 #include <linux/module.h>
20 #include <linux/delay.h>
21 #include <linux/device.h>
22 #include <linux/err.h>
23 #include "../common/sst-dsp.h"
24 #include "../common/sst-dsp-priv.h"
25 #include "../common/sst-ipc.h"
26 #include "skl-sst-ipc.h"
28 #define SKL_BASEFW_TIMEOUT 300
29 #define SKL_INIT_TIMEOUT 1000
31 /* Intel HD Audio SRAM Window 0*/
32 #define SKL_ADSP_SRAM0_BASE 0x8000
34 /* Firmware status window */
35 #define SKL_ADSP_FW_STATUS SKL_ADSP_SRAM0_BASE
36 #define SKL_ADSP_ERROR_CODE (SKL_ADSP_FW_STATUS + 0x4)
38 #define SKL_INSTANCE_ID 0
39 #define SKL_BASE_FW_MODULE_ID 0
41 #define SKL_NUM_MODULES 1
43 static bool skl_check_fw_status(struct sst_dsp *ctx, u32 status)
47 cur_sts = sst_dsp_shim_read(ctx, SKL_ADSP_FW_STATUS) & SKL_FW_STS_MASK;
49 return (cur_sts == status);
52 static int skl_transfer_firmware(struct sst_dsp *ctx,
53 const void *basefw, u32 base_fw_size)
57 ret = ctx->cl_dev.ops.cl_copy_to_dmabuf(ctx, basefw, base_fw_size);
61 ret = sst_dsp_register_poll(ctx,
68 ctx->cl_dev.ops.cl_stop_dma(ctx);
73 static int skl_load_base_firmware(struct sst_dsp *ctx)
76 struct skl_sst *skl = ctx->thread_context;
79 skl->boot_complete = false;
80 init_waitqueue_head(&skl->boot_wait);
82 if (ctx->fw == NULL) {
83 ret = request_firmware(&ctx->fw, ctx->fw_name, ctx->dev);
85 dev_err(ctx->dev, "Request firmware failed %d\n", ret);
86 skl_dsp_disable_core(ctx);
91 ret = skl_dsp_boot(ctx);
93 dev_err(ctx->dev, "Boot dsp core failed ret: %d", ret);
94 goto skl_load_base_firmware_failed;
97 ret = skl_cldma_prepare(ctx);
99 dev_err(ctx->dev, "CL dma prepare failed : %d", ret);
100 goto skl_load_base_firmware_failed;
103 /* enable Interrupt */
104 skl_ipc_int_enable(ctx);
105 skl_ipc_op_int_enable(ctx);
107 /* check ROM Status */
108 for (i = SKL_INIT_TIMEOUT; i > 0; --i) {
109 if (skl_check_fw_status(ctx, SKL_FW_INIT)) {
111 "ROM loaded, we can continue with FW loading\n");
117 reg = sst_dsp_shim_read(ctx, SKL_ADSP_FW_STATUS);
119 "Timeout waiting for ROM init done, reg:0x%x\n", reg);
121 goto transfer_firmware_failed;
124 ret = skl_transfer_firmware(ctx, ctx->fw->data, ctx->fw->size);
126 dev_err(ctx->dev, "Transfer firmware failed%d\n", ret);
127 goto transfer_firmware_failed;
129 ret = wait_event_timeout(skl->boot_wait, skl->boot_complete,
130 msecs_to_jiffies(SKL_IPC_BOOT_MSECS));
132 dev_err(ctx->dev, "DSP boot failed, FW Ready timed-out\n");
134 goto transfer_firmware_failed;
137 dev_dbg(ctx->dev, "Download firmware successful%d\n", ret);
138 skl_dsp_set_state_locked(ctx, SKL_DSP_RUNNING);
141 transfer_firmware_failed:
142 ctx->cl_dev.ops.cl_cleanup_controller(ctx);
143 skl_load_base_firmware_failed:
144 skl_dsp_disable_core(ctx);
145 release_firmware(ctx->fw);
150 static int skl_set_dsp_D0(struct sst_dsp *ctx)
154 ret = skl_load_base_firmware(ctx);
156 dev_err(ctx->dev, "unable to load firmware\n");
160 skl_dsp_set_state_locked(ctx, SKL_DSP_RUNNING);
165 static int skl_set_dsp_D3(struct sst_dsp *ctx)
168 struct skl_ipc_dxstate_info dx;
169 struct skl_sst *skl = ctx->thread_context;
171 dev_dbg(ctx->dev, "In %s:\n", __func__);
172 mutex_lock(&ctx->mutex);
173 if (!is_skl_dsp_running(ctx)) {
174 mutex_unlock(&ctx->mutex);
177 mutex_unlock(&ctx->mutex);
179 dx.core_mask = SKL_DSP_CORE0_MASK;
180 dx.dx_mask = SKL_IPC_D3_MASK;
181 ret = skl_ipc_set_dx(&skl->ipc, SKL_INSTANCE_ID, SKL_BASE_FW_MODULE_ID, &dx);
184 "D3 request to FW failed, continuing reset: %d", ret);
186 /* disable Interrupt */
187 ctx->cl_dev.ops.cl_cleanup_controller(ctx);
188 skl_cldma_int_disable(ctx);
189 skl_ipc_op_int_disable(ctx);
190 skl_ipc_int_disable(ctx);
192 ret = skl_dsp_disable_core(ctx);
194 dev_err(ctx->dev, "disable dsp core failed ret: %d\n", ret);
197 skl_dsp_set_state_locked(ctx, SKL_DSP_RESET);
202 static unsigned int skl_get_errorcode(struct sst_dsp *ctx)
204 return sst_dsp_shim_read(ctx, SKL_ADSP_ERROR_CODE);
208 * since get/set_module are called from DAPM context,
209 * we don't need lock for usage count
211 static int skl_get_module(struct sst_dsp *ctx, u16 mod_id)
213 struct skl_module_table *module;
215 list_for_each_entry(module, &ctx->module_list, list) {
216 if (module->mod_info->mod_id == mod_id)
217 return ++module->usage_cnt;
223 static int skl_put_module(struct sst_dsp *ctx, u16 mod_id)
225 struct skl_module_table *module;
227 list_for_each_entry(module, &ctx->module_list, list) {
228 if (module->mod_info->mod_id == mod_id)
229 return --module->usage_cnt;
235 static struct skl_module_table *skl_fill_module_table(struct sst_dsp *ctx,
236 char *mod_name, int mod_id)
238 const struct firmware *fw;
239 struct skl_module_table *skl_module;
243 ret = request_firmware(&fw, mod_name, ctx->dev);
245 dev_err(ctx->dev, "Request Module %s failed :%d\n",
250 skl_module = devm_kzalloc(ctx->dev, sizeof(*skl_module), GFP_KERNEL);
251 if (skl_module == NULL) {
252 release_firmware(fw);
256 size = sizeof(*skl_module->mod_info);
257 skl_module->mod_info = devm_kzalloc(ctx->dev, size, GFP_KERNEL);
258 if (skl_module->mod_info == NULL) {
259 release_firmware(fw);
263 skl_module->mod_info->mod_id = mod_id;
264 skl_module->mod_info->fw = fw;
265 list_add(&skl_module->list, &ctx->module_list);
270 /* get a module from it's unique ID */
271 static struct skl_module_table *skl_module_get_from_id(
272 struct sst_dsp *ctx, u16 mod_id)
274 struct skl_module_table *module;
276 if (list_empty(&ctx->module_list)) {
277 dev_err(ctx->dev, "Module list is empty\n");
281 list_for_each_entry(module, &ctx->module_list, list) {
282 if (module->mod_info->mod_id == mod_id)
289 static int skl_transfer_module(struct sst_dsp *ctx,
290 struct skl_load_module_info *module)
293 struct skl_sst *skl = ctx->thread_context;
295 ret = ctx->cl_dev.ops.cl_copy_to_dmabuf(ctx, module->fw->data,
300 ret = skl_ipc_load_modules(&skl->ipc, SKL_NUM_MODULES,
301 (void *)&module->mod_id);
303 dev_err(ctx->dev, "Failed to Load module: %d\n", ret);
305 ctx->cl_dev.ops.cl_stop_dma(ctx);
310 static int skl_load_module(struct sst_dsp *ctx, u16 mod_id, char *guid)
312 struct skl_module_table *module_entry = NULL;
314 char mod_name[64]; /* guid str = 32 chars + 4 hyphens */
316 snprintf(mod_name, sizeof(mod_name), "%s%s%s",
317 "intel/dsp_fw_", guid, ".bin");
319 module_entry = skl_module_get_from_id(ctx, mod_id);
320 if (module_entry == NULL) {
321 module_entry = skl_fill_module_table(ctx, mod_name, mod_id);
322 if (module_entry == NULL) {
323 dev_err(ctx->dev, "Failed to Load module\n");
328 if (!module_entry->usage_cnt) {
329 ret = skl_transfer_module(ctx, module_entry->mod_info);
331 dev_err(ctx->dev, "Failed to Load module\n");
336 ret = skl_get_module(ctx, mod_id);
341 static int skl_unload_module(struct sst_dsp *ctx, u16 mod_id)
344 struct skl_sst *skl = ctx->thread_context;
347 usage_cnt = skl_put_module(ctx, mod_id);
349 dev_err(ctx->dev, "Module bad usage cnt!:%d\n", usage_cnt);
352 ret = skl_ipc_unload_modules(&skl->ipc,
353 SKL_NUM_MODULES, &mod_id);
355 dev_err(ctx->dev, "Failed to UnLoad module\n");
356 skl_get_module(ctx, mod_id);
363 static void skl_clear_module_table(struct sst_dsp *ctx)
365 struct skl_module_table *module, *tmp;
367 if (list_empty(&ctx->module_list))
370 list_for_each_entry_safe(module, tmp, &ctx->module_list, list) {
371 list_del(&module->list);
372 release_firmware(module->mod_info->fw);
376 static struct skl_dsp_fw_ops skl_fw_ops = {
377 .set_state_D0 = skl_set_dsp_D0,
378 .set_state_D3 = skl_set_dsp_D3,
379 .load_fw = skl_load_base_firmware,
380 .get_fw_errcode = skl_get_errorcode,
381 .load_mod = skl_load_module,
382 .unload_mod = skl_unload_module,
385 static struct sst_ops skl_ops = {
386 .irq_handler = skl_dsp_sst_interrupt,
387 .write = sst_shim32_write,
388 .read = sst_shim32_read,
389 .ram_read = sst_memcpy_fromio_32,
390 .ram_write = sst_memcpy_toio_32,
391 .free = skl_dsp_free,
394 static struct sst_dsp_device skl_dev = {
395 .thread = skl_dsp_irq_thread_handler,
399 int skl_sst_dsp_init(struct device *dev, void __iomem *mmio_base, int irq,
400 const char *fw_name, struct skl_dsp_loader_ops dsp_ops, struct skl_sst **dsp)
406 skl = devm_kzalloc(dev, sizeof(*skl), GFP_KERNEL);
411 skl_dev.thread_context = skl;
413 skl->dsp = skl_dsp_ctx_init(dev, &skl_dev, irq);
415 dev_err(skl->dev, "%s: no device\n", __func__);
421 sst->fw_name = fw_name;
422 sst->addr.lpe = mmio_base;
423 sst->addr.shim = mmio_base;
424 sst_dsp_mailbox_init(sst, (SKL_ADSP_SRAM0_BASE + SKL_ADSP_W0_STAT_SZ),
425 SKL_ADSP_W0_UP_SZ, SKL_ADSP_SRAM1_BASE, SKL_ADSP_W1_SZ);
427 INIT_LIST_HEAD(&sst->module_list);
428 sst->dsp_ops = dsp_ops;
429 sst->fw_ops = skl_fw_ops;
431 ret = skl_ipc_init(dev, skl);
435 ret = sst->fw_ops.load_fw(sst);
437 dev_err(dev, "Load base fw failed : %d", ret);
447 skl_sst_dsp_cleanup(dev, skl);
450 EXPORT_SYMBOL_GPL(skl_sst_dsp_init);
452 void skl_sst_dsp_cleanup(struct device *dev, struct skl_sst *ctx)
454 skl_clear_module_table(ctx->dsp);
455 skl_ipc_free(&ctx->ipc);
456 ctx->dsp->ops->free(ctx->dsp);
458 EXPORT_SYMBOL_GPL(skl_sst_dsp_cleanup);
460 MODULE_LICENSE("GPL v2");
461 MODULE_DESCRIPTION("Intel Skylake IPC driver");