18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * cnl-sst-dsp.c - CNL SST library generic function
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2016-17, Intel Corporation.
68c2ecf20Sopenharmony_ci * Author: Guneshwor Singh <guneshwor.o.singh@intel.com>
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Modified from:
98c2ecf20Sopenharmony_ci *	SKL SST library generic function
108c2ecf20Sopenharmony_ci *	Copyright (C) 2014-15, Intel Corporation.
118c2ecf20Sopenharmony_ci * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
128c2ecf20Sopenharmony_ci *
138c2ecf20Sopenharmony_ci * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
148c2ecf20Sopenharmony_ci */
158c2ecf20Sopenharmony_ci#include <linux/device.h>
168c2ecf20Sopenharmony_ci#include "../common/sst-dsp.h"
178c2ecf20Sopenharmony_ci#include "../common/sst-ipc.h"
188c2ecf20Sopenharmony_ci#include "../common/sst-dsp-priv.h"
198c2ecf20Sopenharmony_ci#include "cnl-sst-dsp.h"
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci/* various timeout values */
228c2ecf20Sopenharmony_ci#define CNL_DSP_PU_TO		50
238c2ecf20Sopenharmony_ci#define CNL_DSP_PD_TO		50
248c2ecf20Sopenharmony_ci#define CNL_DSP_RESET_TO	50
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_cistatic int
278c2ecf20Sopenharmony_cicnl_dsp_core_set_reset_state(struct sst_dsp *ctx, unsigned int core_mask)
288c2ecf20Sopenharmony_ci{
298c2ecf20Sopenharmony_ci	/* update bits */
308c2ecf20Sopenharmony_ci	sst_dsp_shim_update_bits_unlocked(ctx,
318c2ecf20Sopenharmony_ci			CNL_ADSP_REG_ADSPCS, CNL_ADSPCS_CRST(core_mask),
328c2ecf20Sopenharmony_ci			CNL_ADSPCS_CRST(core_mask));
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci	/* poll with timeout to check if operation successful */
358c2ecf20Sopenharmony_ci	return sst_dsp_register_poll(ctx,
368c2ecf20Sopenharmony_ci			CNL_ADSP_REG_ADSPCS,
378c2ecf20Sopenharmony_ci			CNL_ADSPCS_CRST(core_mask),
388c2ecf20Sopenharmony_ci			CNL_ADSPCS_CRST(core_mask),
398c2ecf20Sopenharmony_ci			CNL_DSP_RESET_TO,
408c2ecf20Sopenharmony_ci			"Set reset");
418c2ecf20Sopenharmony_ci}
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_cistatic int
448c2ecf20Sopenharmony_cicnl_dsp_core_unset_reset_state(struct sst_dsp *ctx, unsigned int core_mask)
458c2ecf20Sopenharmony_ci{
468c2ecf20Sopenharmony_ci	/* update bits */
478c2ecf20Sopenharmony_ci	sst_dsp_shim_update_bits_unlocked(ctx, CNL_ADSP_REG_ADSPCS,
488c2ecf20Sopenharmony_ci					CNL_ADSPCS_CRST(core_mask), 0);
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci	/* poll with timeout to check if operation successful */
518c2ecf20Sopenharmony_ci	return sst_dsp_register_poll(ctx,
528c2ecf20Sopenharmony_ci			CNL_ADSP_REG_ADSPCS,
538c2ecf20Sopenharmony_ci			CNL_ADSPCS_CRST(core_mask),
548c2ecf20Sopenharmony_ci			0,
558c2ecf20Sopenharmony_ci			CNL_DSP_RESET_TO,
568c2ecf20Sopenharmony_ci			"Unset reset");
578c2ecf20Sopenharmony_ci}
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_cistatic bool is_cnl_dsp_core_enable(struct sst_dsp *ctx, unsigned int core_mask)
608c2ecf20Sopenharmony_ci{
618c2ecf20Sopenharmony_ci	int val;
628c2ecf20Sopenharmony_ci	bool is_enable;
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	val = sst_dsp_shim_read_unlocked(ctx, CNL_ADSP_REG_ADSPCS);
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci	is_enable = (val & CNL_ADSPCS_CPA(core_mask)) &&
678c2ecf20Sopenharmony_ci			(val & CNL_ADSPCS_SPA(core_mask)) &&
688c2ecf20Sopenharmony_ci			!(val & CNL_ADSPCS_CRST(core_mask)) &&
698c2ecf20Sopenharmony_ci			!(val & CNL_ADSPCS_CSTALL(core_mask));
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	dev_dbg(ctx->dev, "DSP core(s) enabled? %d: core_mask %#x\n",
728c2ecf20Sopenharmony_ci		is_enable, core_mask);
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci	return is_enable;
758c2ecf20Sopenharmony_ci}
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_cistatic int cnl_dsp_reset_core(struct sst_dsp *ctx, unsigned int core_mask)
788c2ecf20Sopenharmony_ci{
798c2ecf20Sopenharmony_ci	/* stall core */
808c2ecf20Sopenharmony_ci	sst_dsp_shim_update_bits_unlocked(ctx, CNL_ADSP_REG_ADSPCS,
818c2ecf20Sopenharmony_ci			CNL_ADSPCS_CSTALL(core_mask),
828c2ecf20Sopenharmony_ci			CNL_ADSPCS_CSTALL(core_mask));
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	/* set reset state */
858c2ecf20Sopenharmony_ci	return cnl_dsp_core_set_reset_state(ctx, core_mask);
868c2ecf20Sopenharmony_ci}
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_cistatic int cnl_dsp_start_core(struct sst_dsp *ctx, unsigned int core_mask)
898c2ecf20Sopenharmony_ci{
908c2ecf20Sopenharmony_ci	int ret;
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	/* unset reset state */
938c2ecf20Sopenharmony_ci	ret = cnl_dsp_core_unset_reset_state(ctx, core_mask);
948c2ecf20Sopenharmony_ci	if (ret < 0)
958c2ecf20Sopenharmony_ci		return ret;
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	/* run core */
988c2ecf20Sopenharmony_ci	sst_dsp_shim_update_bits_unlocked(ctx, CNL_ADSP_REG_ADSPCS,
998c2ecf20Sopenharmony_ci				CNL_ADSPCS_CSTALL(core_mask), 0);
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	if (!is_cnl_dsp_core_enable(ctx, core_mask)) {
1028c2ecf20Sopenharmony_ci		cnl_dsp_reset_core(ctx, core_mask);
1038c2ecf20Sopenharmony_ci		dev_err(ctx->dev, "DSP core mask %#x enable failed\n",
1048c2ecf20Sopenharmony_ci			core_mask);
1058c2ecf20Sopenharmony_ci		ret = -EIO;
1068c2ecf20Sopenharmony_ci	}
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	return ret;
1098c2ecf20Sopenharmony_ci}
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_cistatic int cnl_dsp_core_power_up(struct sst_dsp *ctx, unsigned int core_mask)
1128c2ecf20Sopenharmony_ci{
1138c2ecf20Sopenharmony_ci	/* update bits */
1148c2ecf20Sopenharmony_ci	sst_dsp_shim_update_bits_unlocked(ctx, CNL_ADSP_REG_ADSPCS,
1158c2ecf20Sopenharmony_ci					  CNL_ADSPCS_SPA(core_mask),
1168c2ecf20Sopenharmony_ci					  CNL_ADSPCS_SPA(core_mask));
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	/* poll with timeout to check if operation successful */
1198c2ecf20Sopenharmony_ci	return sst_dsp_register_poll(ctx, CNL_ADSP_REG_ADSPCS,
1208c2ecf20Sopenharmony_ci				    CNL_ADSPCS_CPA(core_mask),
1218c2ecf20Sopenharmony_ci				    CNL_ADSPCS_CPA(core_mask),
1228c2ecf20Sopenharmony_ci				    CNL_DSP_PU_TO,
1238c2ecf20Sopenharmony_ci				    "Power up");
1248c2ecf20Sopenharmony_ci}
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_cistatic int cnl_dsp_core_power_down(struct sst_dsp *ctx, unsigned int core_mask)
1278c2ecf20Sopenharmony_ci{
1288c2ecf20Sopenharmony_ci	/* update bits */
1298c2ecf20Sopenharmony_ci	sst_dsp_shim_update_bits_unlocked(ctx, CNL_ADSP_REG_ADSPCS,
1308c2ecf20Sopenharmony_ci					CNL_ADSPCS_SPA(core_mask), 0);
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci	/* poll with timeout to check if operation successful */
1338c2ecf20Sopenharmony_ci	return sst_dsp_register_poll(ctx,
1348c2ecf20Sopenharmony_ci			CNL_ADSP_REG_ADSPCS,
1358c2ecf20Sopenharmony_ci			CNL_ADSPCS_CPA(core_mask),
1368c2ecf20Sopenharmony_ci			0,
1378c2ecf20Sopenharmony_ci			CNL_DSP_PD_TO,
1388c2ecf20Sopenharmony_ci			"Power down");
1398c2ecf20Sopenharmony_ci}
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ciint cnl_dsp_enable_core(struct sst_dsp *ctx, unsigned int core_mask)
1428c2ecf20Sopenharmony_ci{
1438c2ecf20Sopenharmony_ci	int ret;
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	/* power up */
1468c2ecf20Sopenharmony_ci	ret = cnl_dsp_core_power_up(ctx, core_mask);
1478c2ecf20Sopenharmony_ci	if (ret < 0) {
1488c2ecf20Sopenharmony_ci		dev_dbg(ctx->dev, "DSP core mask %#x power up failed",
1498c2ecf20Sopenharmony_ci			core_mask);
1508c2ecf20Sopenharmony_ci		return ret;
1518c2ecf20Sopenharmony_ci	}
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci	return cnl_dsp_start_core(ctx, core_mask);
1548c2ecf20Sopenharmony_ci}
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ciint cnl_dsp_disable_core(struct sst_dsp *ctx, unsigned int core_mask)
1578c2ecf20Sopenharmony_ci{
1588c2ecf20Sopenharmony_ci	int ret;
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci	ret = cnl_dsp_reset_core(ctx, core_mask);
1618c2ecf20Sopenharmony_ci	if (ret < 0) {
1628c2ecf20Sopenharmony_ci		dev_err(ctx->dev, "DSP core mask %#x reset failed\n",
1638c2ecf20Sopenharmony_ci			core_mask);
1648c2ecf20Sopenharmony_ci		return ret;
1658c2ecf20Sopenharmony_ci	}
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci	/* power down core*/
1688c2ecf20Sopenharmony_ci	ret = cnl_dsp_core_power_down(ctx, core_mask);
1698c2ecf20Sopenharmony_ci	if (ret < 0) {
1708c2ecf20Sopenharmony_ci		dev_err(ctx->dev, "DSP core mask %#x power down failed\n",
1718c2ecf20Sopenharmony_ci			core_mask);
1728c2ecf20Sopenharmony_ci		return ret;
1738c2ecf20Sopenharmony_ci	}
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci	if (is_cnl_dsp_core_enable(ctx, core_mask)) {
1768c2ecf20Sopenharmony_ci		dev_err(ctx->dev, "DSP core mask %#x disable failed\n",
1778c2ecf20Sopenharmony_ci			core_mask);
1788c2ecf20Sopenharmony_ci		ret = -EIO;
1798c2ecf20Sopenharmony_ci	}
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci	return ret;
1828c2ecf20Sopenharmony_ci}
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ciirqreturn_t cnl_dsp_sst_interrupt(int irq, void *dev_id)
1858c2ecf20Sopenharmony_ci{
1868c2ecf20Sopenharmony_ci	struct sst_dsp *ctx = dev_id;
1878c2ecf20Sopenharmony_ci	u32 val;
1888c2ecf20Sopenharmony_ci	irqreturn_t ret = IRQ_NONE;
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci	spin_lock(&ctx->spinlock);
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	val = sst_dsp_shim_read_unlocked(ctx, CNL_ADSP_REG_ADSPIS);
1938c2ecf20Sopenharmony_ci	ctx->intr_status = val;
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci	if (val == 0xffffffff) {
1968c2ecf20Sopenharmony_ci		spin_unlock(&ctx->spinlock);
1978c2ecf20Sopenharmony_ci		return IRQ_NONE;
1988c2ecf20Sopenharmony_ci	}
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci	if (val & CNL_ADSPIS_IPC) {
2018c2ecf20Sopenharmony_ci		cnl_ipc_int_disable(ctx);
2028c2ecf20Sopenharmony_ci		ret = IRQ_WAKE_THREAD;
2038c2ecf20Sopenharmony_ci	}
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci	spin_unlock(&ctx->spinlock);
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci	return ret;
2088c2ecf20Sopenharmony_ci}
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_civoid cnl_dsp_free(struct sst_dsp *dsp)
2118c2ecf20Sopenharmony_ci{
2128c2ecf20Sopenharmony_ci	cnl_ipc_int_disable(dsp);
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci	free_irq(dsp->irq, dsp);
2158c2ecf20Sopenharmony_ci	cnl_ipc_op_int_disable(dsp);
2168c2ecf20Sopenharmony_ci	cnl_dsp_disable_core(dsp, SKL_DSP_CORE0_MASK);
2178c2ecf20Sopenharmony_ci}
2188c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(cnl_dsp_free);
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_civoid cnl_ipc_int_enable(struct sst_dsp *ctx)
2218c2ecf20Sopenharmony_ci{
2228c2ecf20Sopenharmony_ci	sst_dsp_shim_update_bits(ctx, CNL_ADSP_REG_ADSPIC,
2238c2ecf20Sopenharmony_ci				 CNL_ADSPIC_IPC, CNL_ADSPIC_IPC);
2248c2ecf20Sopenharmony_ci}
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_civoid cnl_ipc_int_disable(struct sst_dsp *ctx)
2278c2ecf20Sopenharmony_ci{
2288c2ecf20Sopenharmony_ci	sst_dsp_shim_update_bits_unlocked(ctx, CNL_ADSP_REG_ADSPIC,
2298c2ecf20Sopenharmony_ci					  CNL_ADSPIC_IPC, 0);
2308c2ecf20Sopenharmony_ci}
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_civoid cnl_ipc_op_int_enable(struct sst_dsp *ctx)
2338c2ecf20Sopenharmony_ci{
2348c2ecf20Sopenharmony_ci	/* enable IPC DONE interrupt */
2358c2ecf20Sopenharmony_ci	sst_dsp_shim_update_bits(ctx, CNL_ADSP_REG_HIPCCTL,
2368c2ecf20Sopenharmony_ci				 CNL_ADSP_REG_HIPCCTL_DONE,
2378c2ecf20Sopenharmony_ci				 CNL_ADSP_REG_HIPCCTL_DONE);
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci	/* enable IPC BUSY interrupt */
2408c2ecf20Sopenharmony_ci	sst_dsp_shim_update_bits(ctx, CNL_ADSP_REG_HIPCCTL,
2418c2ecf20Sopenharmony_ci				 CNL_ADSP_REG_HIPCCTL_BUSY,
2428c2ecf20Sopenharmony_ci				 CNL_ADSP_REG_HIPCCTL_BUSY);
2438c2ecf20Sopenharmony_ci}
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_civoid cnl_ipc_op_int_disable(struct sst_dsp *ctx)
2468c2ecf20Sopenharmony_ci{
2478c2ecf20Sopenharmony_ci	/* disable IPC DONE interrupt */
2488c2ecf20Sopenharmony_ci	sst_dsp_shim_update_bits(ctx, CNL_ADSP_REG_HIPCCTL,
2498c2ecf20Sopenharmony_ci				 CNL_ADSP_REG_HIPCCTL_DONE, 0);
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci	/* disable IPC BUSY interrupt */
2528c2ecf20Sopenharmony_ci	sst_dsp_shim_update_bits(ctx, CNL_ADSP_REG_HIPCCTL,
2538c2ecf20Sopenharmony_ci				 CNL_ADSP_REG_HIPCCTL_BUSY, 0);
2548c2ecf20Sopenharmony_ci}
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_cibool cnl_ipc_int_status(struct sst_dsp *ctx)
2578c2ecf20Sopenharmony_ci{
2588c2ecf20Sopenharmony_ci	return sst_dsp_shim_read_unlocked(ctx, CNL_ADSP_REG_ADSPIS) &
2598c2ecf20Sopenharmony_ci							CNL_ADSPIS_IPC;
2608c2ecf20Sopenharmony_ci}
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_civoid cnl_ipc_free(struct sst_generic_ipc *ipc)
2638c2ecf20Sopenharmony_ci{
2648c2ecf20Sopenharmony_ci	cnl_ipc_op_int_disable(ipc->dsp);
2658c2ecf20Sopenharmony_ci	sst_ipc_fini(ipc);
2668c2ecf20Sopenharmony_ci}
267