18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Intel Smart Sound Technology (SST) DSP Core Driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2013, Intel Corporation. All rights reserved.
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/slab.h>
98c2ecf20Sopenharmony_ci#include <linux/export.h>
108c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
118c2ecf20Sopenharmony_ci#include <linux/module.h>
128c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
138c2ecf20Sopenharmony_ci#include <linux/io-64-nonatomic-lo-hi.h>
148c2ecf20Sopenharmony_ci#include <linux/delay.h>
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#include "sst-dsp.h"
178c2ecf20Sopenharmony_ci#include "sst-dsp-priv.h"
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#define CREATE_TRACE_POINTS
208c2ecf20Sopenharmony_ci#include <trace/events/intel-sst.h>
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci/* Internal generic low-level SST IO functions - can be overidden */
238c2ecf20Sopenharmony_civoid sst_shim32_write(void __iomem *addr, u32 offset, u32 value)
248c2ecf20Sopenharmony_ci{
258c2ecf20Sopenharmony_ci	writel(value, addr + offset);
268c2ecf20Sopenharmony_ci}
278c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(sst_shim32_write);
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ciu32 sst_shim32_read(void __iomem *addr, u32 offset)
308c2ecf20Sopenharmony_ci{
318c2ecf20Sopenharmony_ci	return readl(addr + offset);
328c2ecf20Sopenharmony_ci}
338c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(sst_shim32_read);
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_civoid sst_shim32_write64(void __iomem *addr, u32 offset, u64 value)
368c2ecf20Sopenharmony_ci{
378c2ecf20Sopenharmony_ci	writeq(value, addr + offset);
388c2ecf20Sopenharmony_ci}
398c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(sst_shim32_write64);
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ciu64 sst_shim32_read64(void __iomem *addr, u32 offset)
428c2ecf20Sopenharmony_ci{
438c2ecf20Sopenharmony_ci	return readq(addr + offset);
448c2ecf20Sopenharmony_ci}
458c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(sst_shim32_read64);
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci/* Public API */
488c2ecf20Sopenharmony_civoid sst_dsp_shim_write(struct sst_dsp *sst, u32 offset, u32 value)
498c2ecf20Sopenharmony_ci{
508c2ecf20Sopenharmony_ci	unsigned long flags;
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci	spin_lock_irqsave(&sst->spinlock, flags);
538c2ecf20Sopenharmony_ci	sst->ops->write(sst->addr.shim, offset, value);
548c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&sst->spinlock, flags);
558c2ecf20Sopenharmony_ci}
568c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(sst_dsp_shim_write);
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ciu32 sst_dsp_shim_read(struct sst_dsp *sst, u32 offset)
598c2ecf20Sopenharmony_ci{
608c2ecf20Sopenharmony_ci	unsigned long flags;
618c2ecf20Sopenharmony_ci	u32 val;
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci	spin_lock_irqsave(&sst->spinlock, flags);
648c2ecf20Sopenharmony_ci	val = sst->ops->read(sst->addr.shim, offset);
658c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&sst->spinlock, flags);
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci	return val;
688c2ecf20Sopenharmony_ci}
698c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(sst_dsp_shim_read);
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_civoid sst_dsp_shim_write_unlocked(struct sst_dsp *sst, u32 offset, u32 value)
728c2ecf20Sopenharmony_ci{
738c2ecf20Sopenharmony_ci	sst->ops->write(sst->addr.shim, offset, value);
748c2ecf20Sopenharmony_ci}
758c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(sst_dsp_shim_write_unlocked);
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ciu32 sst_dsp_shim_read_unlocked(struct sst_dsp *sst, u32 offset)
788c2ecf20Sopenharmony_ci{
798c2ecf20Sopenharmony_ci	return sst->ops->read(sst->addr.shim, offset);
808c2ecf20Sopenharmony_ci}
818c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(sst_dsp_shim_read_unlocked);
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ciint sst_dsp_shim_update_bits_unlocked(struct sst_dsp *sst, u32 offset,
848c2ecf20Sopenharmony_ci				u32 mask, u32 value)
858c2ecf20Sopenharmony_ci{
868c2ecf20Sopenharmony_ci	bool change;
878c2ecf20Sopenharmony_ci	unsigned int old, new;
888c2ecf20Sopenharmony_ci	u32 ret;
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	ret = sst_dsp_shim_read_unlocked(sst, offset);
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	old = ret;
938c2ecf20Sopenharmony_ci	new = (old & (~mask)) | (value & mask);
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	change = (old != new);
968c2ecf20Sopenharmony_ci	if (change)
978c2ecf20Sopenharmony_ci		sst_dsp_shim_write_unlocked(sst, offset, new);
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci	return change;
1008c2ecf20Sopenharmony_ci}
1018c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(sst_dsp_shim_update_bits_unlocked);
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci/* This is for registers bits with attribute RWC */
1048c2ecf20Sopenharmony_civoid sst_dsp_shim_update_bits_forced_unlocked(struct sst_dsp *sst, u32 offset,
1058c2ecf20Sopenharmony_ci				u32 mask, u32 value)
1068c2ecf20Sopenharmony_ci{
1078c2ecf20Sopenharmony_ci	unsigned int old, new;
1088c2ecf20Sopenharmony_ci	u32 ret;
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	ret = sst_dsp_shim_read_unlocked(sst, offset);
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	old = ret;
1138c2ecf20Sopenharmony_ci	new = (old & (~mask)) | (value & mask);
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	sst_dsp_shim_write_unlocked(sst, offset, new);
1168c2ecf20Sopenharmony_ci}
1178c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(sst_dsp_shim_update_bits_forced_unlocked);
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ciint sst_dsp_shim_update_bits(struct sst_dsp *sst, u32 offset,
1208c2ecf20Sopenharmony_ci				u32 mask, u32 value)
1218c2ecf20Sopenharmony_ci{
1228c2ecf20Sopenharmony_ci	unsigned long flags;
1238c2ecf20Sopenharmony_ci	bool change;
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	spin_lock_irqsave(&sst->spinlock, flags);
1268c2ecf20Sopenharmony_ci	change = sst_dsp_shim_update_bits_unlocked(sst, offset, mask, value);
1278c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&sst->spinlock, flags);
1288c2ecf20Sopenharmony_ci	return change;
1298c2ecf20Sopenharmony_ci}
1308c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(sst_dsp_shim_update_bits);
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci/* This is for registers bits with attribute RWC */
1338c2ecf20Sopenharmony_civoid sst_dsp_shim_update_bits_forced(struct sst_dsp *sst, u32 offset,
1348c2ecf20Sopenharmony_ci				u32 mask, u32 value)
1358c2ecf20Sopenharmony_ci{
1368c2ecf20Sopenharmony_ci	unsigned long flags;
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	spin_lock_irqsave(&sst->spinlock, flags);
1398c2ecf20Sopenharmony_ci	sst_dsp_shim_update_bits_forced_unlocked(sst, offset, mask, value);
1408c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&sst->spinlock, flags);
1418c2ecf20Sopenharmony_ci}
1428c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(sst_dsp_shim_update_bits_forced);
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ciint sst_dsp_register_poll(struct sst_dsp *ctx, u32 offset, u32 mask,
1458c2ecf20Sopenharmony_ci			 u32 target, u32 time, char *operation)
1468c2ecf20Sopenharmony_ci{
1478c2ecf20Sopenharmony_ci	u32 reg;
1488c2ecf20Sopenharmony_ci	unsigned long timeout;
1498c2ecf20Sopenharmony_ci	int k = 0, s = 500;
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci	/*
1528c2ecf20Sopenharmony_ci	 * split the loop into sleeps of varying resolution. more accurately,
1538c2ecf20Sopenharmony_ci	 * the range of wakeups are:
1548c2ecf20Sopenharmony_ci	 * Phase 1(first 5ms): min sleep 0.5ms; max sleep 1ms.
1558c2ecf20Sopenharmony_ci	 * Phase 2:( 5ms to 10ms) : min sleep 0.5ms; max sleep 10ms
1568c2ecf20Sopenharmony_ci	 * (usleep_range (500, 1000) and usleep_range(5000, 10000) are
1578c2ecf20Sopenharmony_ci	 * both possible in this phase depending on whether k > 10 or not).
1588c2ecf20Sopenharmony_ci	 * Phase 3: (beyond 10 ms) min sleep 5ms; max sleep 10ms.
1598c2ecf20Sopenharmony_ci	 */
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	timeout = jiffies + msecs_to_jiffies(time);
1628c2ecf20Sopenharmony_ci	while ((((reg = sst_dsp_shim_read_unlocked(ctx, offset)) & mask) != target)
1638c2ecf20Sopenharmony_ci		&& time_before(jiffies, timeout)) {
1648c2ecf20Sopenharmony_ci		k++;
1658c2ecf20Sopenharmony_ci		if (k > 10)
1668c2ecf20Sopenharmony_ci			s = 5000;
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci		usleep_range(s, 2*s);
1698c2ecf20Sopenharmony_ci	}
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci	if ((reg & mask) == target) {
1728c2ecf20Sopenharmony_ci		dev_dbg(ctx->dev, "FW Poll Status: reg=%#x %s successful\n",
1738c2ecf20Sopenharmony_ci					reg, operation);
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci		return 0;
1768c2ecf20Sopenharmony_ci	}
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci	dev_dbg(ctx->dev, "FW Poll Status: reg=%#x %s timedout\n",
1798c2ecf20Sopenharmony_ci					reg, operation);
1808c2ecf20Sopenharmony_ci	return -ETIME;
1818c2ecf20Sopenharmony_ci}
1828c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(sst_dsp_register_poll);
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ciint sst_dsp_mailbox_init(struct sst_dsp *sst, u32 inbox_offset, size_t inbox_size,
1858c2ecf20Sopenharmony_ci	u32 outbox_offset, size_t outbox_size)
1868c2ecf20Sopenharmony_ci{
1878c2ecf20Sopenharmony_ci	sst->mailbox.in_base = sst->addr.lpe + inbox_offset;
1888c2ecf20Sopenharmony_ci	sst->mailbox.out_base = sst->addr.lpe + outbox_offset;
1898c2ecf20Sopenharmony_ci	sst->mailbox.in_size = inbox_size;
1908c2ecf20Sopenharmony_ci	sst->mailbox.out_size = outbox_size;
1918c2ecf20Sopenharmony_ci	return 0;
1928c2ecf20Sopenharmony_ci}
1938c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(sst_dsp_mailbox_init);
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_civoid sst_dsp_outbox_write(struct sst_dsp *sst, void *message, size_t bytes)
1968c2ecf20Sopenharmony_ci{
1978c2ecf20Sopenharmony_ci	u32 i;
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci	trace_sst_ipc_outbox_write(bytes);
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci	memcpy_toio(sst->mailbox.out_base, message, bytes);
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci	for (i = 0; i < bytes; i += 4)
2048c2ecf20Sopenharmony_ci		trace_sst_ipc_outbox_wdata(i, *(u32 *)(message + i));
2058c2ecf20Sopenharmony_ci}
2068c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(sst_dsp_outbox_write);
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_civoid sst_dsp_outbox_read(struct sst_dsp *sst, void *message, size_t bytes)
2098c2ecf20Sopenharmony_ci{
2108c2ecf20Sopenharmony_ci	u32 i;
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci	trace_sst_ipc_outbox_read(bytes);
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci	memcpy_fromio(message, sst->mailbox.out_base, bytes);
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci	for (i = 0; i < bytes; i += 4)
2178c2ecf20Sopenharmony_ci		trace_sst_ipc_outbox_rdata(i, *(u32 *)(message + i));
2188c2ecf20Sopenharmony_ci}
2198c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(sst_dsp_outbox_read);
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_civoid sst_dsp_inbox_write(struct sst_dsp *sst, void *message, size_t bytes)
2228c2ecf20Sopenharmony_ci{
2238c2ecf20Sopenharmony_ci	u32 i;
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci	trace_sst_ipc_inbox_write(bytes);
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci	memcpy_toio(sst->mailbox.in_base, message, bytes);
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci	for (i = 0; i < bytes; i += 4)
2308c2ecf20Sopenharmony_ci		trace_sst_ipc_inbox_wdata(i, *(u32 *)(message + i));
2318c2ecf20Sopenharmony_ci}
2328c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(sst_dsp_inbox_write);
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_civoid sst_dsp_inbox_read(struct sst_dsp *sst, void *message, size_t bytes)
2358c2ecf20Sopenharmony_ci{
2368c2ecf20Sopenharmony_ci	u32 i;
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci	trace_sst_ipc_inbox_read(bytes);
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci	memcpy_fromio(message, sst->mailbox.in_base, bytes);
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci	for (i = 0; i < bytes; i += 4)
2438c2ecf20Sopenharmony_ci		trace_sst_ipc_inbox_rdata(i, *(u32 *)(message + i));
2448c2ecf20Sopenharmony_ci}
2458c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(sst_dsp_inbox_read);
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci/* Module information */
2488c2ecf20Sopenharmony_ciMODULE_AUTHOR("Liam Girdwood");
2498c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Intel SST Core");
2508c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
251