xref: /linux/sound/soc/intel/common/sst-dsp.c (revision 33e02dc69afbd8f1b85a51d74d72f139ba4ca623)
11802d0beSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
22106241aSJie Yang /*
32106241aSJie Yang  * Intel Smart Sound Technology (SST) DSP Core Driver
42106241aSJie Yang  *
5*5cf4ffa4SPierre-Louis Bossart  * Copyright (C) 2013, Intel Corporation
62106241aSJie Yang  */
72106241aSJie Yang 
82106241aSJie Yang #include <linux/slab.h>
92106241aSJie Yang #include <linux/export.h>
102106241aSJie Yang #include <linux/interrupt.h>
112106241aSJie Yang #include <linux/module.h>
122106241aSJie Yang #include <linux/platform_device.h>
136c47660eSAmadeusz Sławiński #include <linux/io-64-nonatomic-lo-hi.h>
147a2c52b6SSubhransu S. Prusty #include <linux/delay.h>
152106241aSJie Yang 
162106241aSJie Yang #include "sst-dsp.h"
172106241aSJie Yang #include "sst-dsp-priv.h"
182106241aSJie Yang 
192106241aSJie Yang #define CREATE_TRACE_POINTS
202106241aSJie Yang #include <trace/events/intel-sst.h>
212106241aSJie Yang 
222106241aSJie Yang /* Internal generic low-level SST IO functions - can be overidden */
sst_shim32_write(void __iomem * addr,u32 offset,u32 value)232106241aSJie Yang void sst_shim32_write(void __iomem *addr, u32 offset, u32 value)
242106241aSJie Yang {
252106241aSJie Yang 	writel(value, addr + offset);
262106241aSJie Yang }
272106241aSJie Yang EXPORT_SYMBOL_GPL(sst_shim32_write);
282106241aSJie Yang 
sst_shim32_read(void __iomem * addr,u32 offset)292106241aSJie Yang u32 sst_shim32_read(void __iomem *addr, u32 offset)
302106241aSJie Yang {
312106241aSJie Yang 	return readl(addr + offset);
322106241aSJie Yang }
332106241aSJie Yang EXPORT_SYMBOL_GPL(sst_shim32_read);
342106241aSJie Yang 
sst_shim32_write64(void __iomem * addr,u32 offset,u64 value)352106241aSJie Yang void sst_shim32_write64(void __iomem *addr, u32 offset, u64 value)
362106241aSJie Yang {
376c47660eSAmadeusz Sławiński 	writeq(value, addr + offset);
382106241aSJie Yang }
392106241aSJie Yang EXPORT_SYMBOL_GPL(sst_shim32_write64);
402106241aSJie Yang 
sst_shim32_read64(void __iomem * addr,u32 offset)412106241aSJie Yang u64 sst_shim32_read64(void __iomem *addr, u32 offset)
422106241aSJie Yang {
436c47660eSAmadeusz Sławiński 	return readq(addr + offset);
442106241aSJie Yang }
452106241aSJie Yang EXPORT_SYMBOL_GPL(sst_shim32_read64);
462106241aSJie Yang 
472106241aSJie Yang /* Public API */
sst_dsp_shim_write(struct sst_dsp * sst,u32 offset,u32 value)482106241aSJie Yang void sst_dsp_shim_write(struct sst_dsp *sst, u32 offset, u32 value)
492106241aSJie Yang {
502106241aSJie Yang 	unsigned long flags;
512106241aSJie Yang 
522106241aSJie Yang 	spin_lock_irqsave(&sst->spinlock, flags);
532106241aSJie Yang 	sst->ops->write(sst->addr.shim, offset, value);
542106241aSJie Yang 	spin_unlock_irqrestore(&sst->spinlock, flags);
552106241aSJie Yang }
562106241aSJie Yang EXPORT_SYMBOL_GPL(sst_dsp_shim_write);
572106241aSJie Yang 
sst_dsp_shim_read(struct sst_dsp * sst,u32 offset)582106241aSJie Yang u32 sst_dsp_shim_read(struct sst_dsp *sst, u32 offset)
592106241aSJie Yang {
602106241aSJie Yang 	unsigned long flags;
612106241aSJie Yang 	u32 val;
622106241aSJie Yang 
632106241aSJie Yang 	spin_lock_irqsave(&sst->spinlock, flags);
642106241aSJie Yang 	val = sst->ops->read(sst->addr.shim, offset);
652106241aSJie Yang 	spin_unlock_irqrestore(&sst->spinlock, flags);
662106241aSJie Yang 
672106241aSJie Yang 	return val;
682106241aSJie Yang }
692106241aSJie Yang EXPORT_SYMBOL_GPL(sst_dsp_shim_read);
702106241aSJie Yang 
sst_dsp_shim_write_unlocked(struct sst_dsp * sst,u32 offset,u32 value)712106241aSJie Yang void sst_dsp_shim_write_unlocked(struct sst_dsp *sst, u32 offset, u32 value)
722106241aSJie Yang {
732106241aSJie Yang 	sst->ops->write(sst->addr.shim, offset, value);
742106241aSJie Yang }
752106241aSJie Yang EXPORT_SYMBOL_GPL(sst_dsp_shim_write_unlocked);
762106241aSJie Yang 
sst_dsp_shim_read_unlocked(struct sst_dsp * sst,u32 offset)772106241aSJie Yang u32 sst_dsp_shim_read_unlocked(struct sst_dsp *sst, u32 offset)
782106241aSJie Yang {
792106241aSJie Yang 	return sst->ops->read(sst->addr.shim, offset);
802106241aSJie Yang }
812106241aSJie Yang EXPORT_SYMBOL_GPL(sst_dsp_shim_read_unlocked);
822106241aSJie Yang 
sst_dsp_shim_update_bits_unlocked(struct sst_dsp * sst,u32 offset,u32 mask,u32 value)832106241aSJie Yang int sst_dsp_shim_update_bits_unlocked(struct sst_dsp *sst, u32 offset,
842106241aSJie Yang 				u32 mask, u32 value)
852106241aSJie Yang {
862106241aSJie Yang 	bool change;
872106241aSJie Yang 	unsigned int old, new;
882106241aSJie Yang 	u32 ret;
892106241aSJie Yang 
902106241aSJie Yang 	ret = sst_dsp_shim_read_unlocked(sst, offset);
912106241aSJie Yang 
922106241aSJie Yang 	old = ret;
932106241aSJie Yang 	new = (old & (~mask)) | (value & mask);
942106241aSJie Yang 
952106241aSJie Yang 	change = (old != new);
962106241aSJie Yang 	if (change)
972106241aSJie Yang 		sst_dsp_shim_write_unlocked(sst, offset, new);
982106241aSJie Yang 
992106241aSJie Yang 	return change;
1002106241aSJie Yang }
1012106241aSJie Yang EXPORT_SYMBOL_GPL(sst_dsp_shim_update_bits_unlocked);
1022106241aSJie Yang 
103d15b1ae1SSubhransu S. Prusty /* This is for registers bits with attribute RWC */
sst_dsp_shim_update_bits_forced_unlocked(struct sst_dsp * sst,u32 offset,u32 mask,u32 value)104d15b1ae1SSubhransu S. Prusty void sst_dsp_shim_update_bits_forced_unlocked(struct sst_dsp *sst, u32 offset,
105d15b1ae1SSubhransu S. Prusty 				u32 mask, u32 value)
106d15b1ae1SSubhransu S. Prusty {
107d15b1ae1SSubhransu S. Prusty 	unsigned int old, new;
108d15b1ae1SSubhransu S. Prusty 	u32 ret;
109d15b1ae1SSubhransu S. Prusty 
110d15b1ae1SSubhransu S. Prusty 	ret = sst_dsp_shim_read_unlocked(sst, offset);
111d15b1ae1SSubhransu S. Prusty 
112d15b1ae1SSubhransu S. Prusty 	old = ret;
113d15b1ae1SSubhransu S. Prusty 	new = (old & (~mask)) | (value & mask);
114d15b1ae1SSubhransu S. Prusty 
115d15b1ae1SSubhransu S. Prusty 	sst_dsp_shim_write_unlocked(sst, offset, new);
116d15b1ae1SSubhransu S. Prusty }
117d15b1ae1SSubhransu S. Prusty EXPORT_SYMBOL_GPL(sst_dsp_shim_update_bits_forced_unlocked);
118d15b1ae1SSubhransu S. Prusty 
sst_dsp_shim_update_bits(struct sst_dsp * sst,u32 offset,u32 mask,u32 value)1192106241aSJie Yang int sst_dsp_shim_update_bits(struct sst_dsp *sst, u32 offset,
1202106241aSJie Yang 				u32 mask, u32 value)
1212106241aSJie Yang {
1222106241aSJie Yang 	unsigned long flags;
1232106241aSJie Yang 	bool change;
1242106241aSJie Yang 
1252106241aSJie Yang 	spin_lock_irqsave(&sst->spinlock, flags);
1262106241aSJie Yang 	change = sst_dsp_shim_update_bits_unlocked(sst, offset, mask, value);
1272106241aSJie Yang 	spin_unlock_irqrestore(&sst->spinlock, flags);
1282106241aSJie Yang 	return change;
1292106241aSJie Yang }
1302106241aSJie Yang EXPORT_SYMBOL_GPL(sst_dsp_shim_update_bits);
1312106241aSJie Yang 
132d15b1ae1SSubhransu S. Prusty /* This is for registers bits with attribute RWC */
sst_dsp_shim_update_bits_forced(struct sst_dsp * sst,u32 offset,u32 mask,u32 value)133d15b1ae1SSubhransu S. Prusty void sst_dsp_shim_update_bits_forced(struct sst_dsp *sst, u32 offset,
134d15b1ae1SSubhransu S. Prusty 				u32 mask, u32 value)
135d15b1ae1SSubhransu S. Prusty {
136d15b1ae1SSubhransu S. Prusty 	unsigned long flags;
137d15b1ae1SSubhransu S. Prusty 
138d15b1ae1SSubhransu S. Prusty 	spin_lock_irqsave(&sst->spinlock, flags);
139d15b1ae1SSubhransu S. Prusty 	sst_dsp_shim_update_bits_forced_unlocked(sst, offset, mask, value);
140d15b1ae1SSubhransu S. Prusty 	spin_unlock_irqrestore(&sst->spinlock, flags);
141d15b1ae1SSubhransu S. Prusty }
142d15b1ae1SSubhransu S. Prusty EXPORT_SYMBOL_GPL(sst_dsp_shim_update_bits_forced);
143d15b1ae1SSubhransu S. Prusty 
sst_dsp_register_poll(struct sst_dsp * ctx,u32 offset,u32 mask,u32 target,u32 time,char * operation)1447a2c52b6SSubhransu S. Prusty int sst_dsp_register_poll(struct sst_dsp *ctx, u32 offset, u32 mask,
1459cc8f9feSJeeja KP 			 u32 target, u32 time, char *operation)
1467a2c52b6SSubhransu S. Prusty {
1477a2c52b6SSubhransu S. Prusty 	u32 reg;
1489cc8f9feSJeeja KP 	unsigned long timeout;
1499cc8f9feSJeeja KP 	int k = 0, s = 500;
1507a2c52b6SSubhransu S. Prusty 
1517a2c52b6SSubhransu S. Prusty 	/*
1529cc8f9feSJeeja KP 	 * split the loop into sleeps of varying resolution. more accurately,
1539cc8f9feSJeeja KP 	 * the range of wakeups are:
1549cc8f9feSJeeja KP 	 * Phase 1(first 5ms): min sleep 0.5ms; max sleep 1ms.
1559cc8f9feSJeeja KP 	 * Phase 2:( 5ms to 10ms) : min sleep 0.5ms; max sleep 10ms
1569cc8f9feSJeeja KP 	 * (usleep_range (500, 1000) and usleep_range(5000, 10000) are
1579cc8f9feSJeeja KP 	 * both possible in this phase depending on whether k > 10 or not).
1589cc8f9feSJeeja KP 	 * Phase 3: (beyond 10 ms) min sleep 5ms; max sleep 10ms.
1597a2c52b6SSubhransu S. Prusty 	 */
1607a2c52b6SSubhransu S. Prusty 
1619cc8f9feSJeeja KP 	timeout = jiffies + msecs_to_jiffies(time);
162b2ca3bddSSubhransu S. Prusty 	while ((((reg = sst_dsp_shim_read_unlocked(ctx, offset)) & mask) != target)
1639cc8f9feSJeeja KP 		&& time_before(jiffies, timeout)) {
1649cc8f9feSJeeja KP 		k++;
1659cc8f9feSJeeja KP 		if (k > 10)
1669cc8f9feSJeeja KP 			s = 5000;
1677a2c52b6SSubhransu S. Prusty 
1689cc8f9feSJeeja KP 		usleep_range(s, 2*s);
1697a2c52b6SSubhransu S. Prusty 	}
1707a2c52b6SSubhransu S. Prusty 
1719cc8f9feSJeeja KP 	if ((reg & mask) == target) {
1729cc8f9feSJeeja KP 		dev_dbg(ctx->dev, "FW Poll Status: reg=%#x %s successful\n",
1739cc8f9feSJeeja KP 					reg, operation);
1749cc8f9feSJeeja KP 
1759cc8f9feSJeeja KP 		return 0;
1769cc8f9feSJeeja KP 	}
1779cc8f9feSJeeja KP 
1789cc8f9feSJeeja KP 	dev_dbg(ctx->dev, "FW Poll Status: reg=%#x %s timedout\n",
1799cc8f9feSJeeja KP 					reg, operation);
1809cc8f9feSJeeja KP 	return -ETIME;
1817a2c52b6SSubhransu S. Prusty }
1827a2c52b6SSubhransu S. Prusty EXPORT_SYMBOL_GPL(sst_dsp_register_poll);
1837a2c52b6SSubhransu S. Prusty 
sst_dsp_mailbox_init(struct sst_dsp * sst,u32 inbox_offset,size_t inbox_size,u32 outbox_offset,size_t outbox_size)1842106241aSJie Yang int sst_dsp_mailbox_init(struct sst_dsp *sst, u32 inbox_offset, size_t inbox_size,
1852106241aSJie Yang 	u32 outbox_offset, size_t outbox_size)
1862106241aSJie Yang {
1872106241aSJie Yang 	sst->mailbox.in_base = sst->addr.lpe + inbox_offset;
1882106241aSJie Yang 	sst->mailbox.out_base = sst->addr.lpe + outbox_offset;
1892106241aSJie Yang 	sst->mailbox.in_size = inbox_size;
1902106241aSJie Yang 	sst->mailbox.out_size = outbox_size;
1912106241aSJie Yang 	return 0;
1922106241aSJie Yang }
1932106241aSJie Yang EXPORT_SYMBOL_GPL(sst_dsp_mailbox_init);
1942106241aSJie Yang 
sst_dsp_outbox_write(struct sst_dsp * sst,void * message,size_t bytes)1952106241aSJie Yang void sst_dsp_outbox_write(struct sst_dsp *sst, void *message, size_t bytes)
1962106241aSJie Yang {
1972106241aSJie Yang 	u32 i;
1982106241aSJie Yang 
1992106241aSJie Yang 	trace_sst_ipc_outbox_write(bytes);
2002106241aSJie Yang 
2012106241aSJie Yang 	memcpy_toio(sst->mailbox.out_base, message, bytes);
2022106241aSJie Yang 
2032106241aSJie Yang 	for (i = 0; i < bytes; i += 4)
2042106241aSJie Yang 		trace_sst_ipc_outbox_wdata(i, *(u32 *)(message + i));
2052106241aSJie Yang }
2062106241aSJie Yang EXPORT_SYMBOL_GPL(sst_dsp_outbox_write);
2072106241aSJie Yang 
sst_dsp_outbox_read(struct sst_dsp * sst,void * message,size_t bytes)2082106241aSJie Yang void sst_dsp_outbox_read(struct sst_dsp *sst, void *message, size_t bytes)
2092106241aSJie Yang {
2102106241aSJie Yang 	u32 i;
2112106241aSJie Yang 
2122106241aSJie Yang 	trace_sst_ipc_outbox_read(bytes);
2132106241aSJie Yang 
2142106241aSJie Yang 	memcpy_fromio(message, sst->mailbox.out_base, bytes);
2152106241aSJie Yang 
2162106241aSJie Yang 	for (i = 0; i < bytes; i += 4)
2172106241aSJie Yang 		trace_sst_ipc_outbox_rdata(i, *(u32 *)(message + i));
2182106241aSJie Yang }
2192106241aSJie Yang EXPORT_SYMBOL_GPL(sst_dsp_outbox_read);
2202106241aSJie Yang 
sst_dsp_inbox_write(struct sst_dsp * sst,void * message,size_t bytes)2212106241aSJie Yang void sst_dsp_inbox_write(struct sst_dsp *sst, void *message, size_t bytes)
2222106241aSJie Yang {
2232106241aSJie Yang 	u32 i;
2242106241aSJie Yang 
2252106241aSJie Yang 	trace_sst_ipc_inbox_write(bytes);
2262106241aSJie Yang 
2272106241aSJie Yang 	memcpy_toio(sst->mailbox.in_base, message, bytes);
2282106241aSJie Yang 
2292106241aSJie Yang 	for (i = 0; i < bytes; i += 4)
2302106241aSJie Yang 		trace_sst_ipc_inbox_wdata(i, *(u32 *)(message + i));
2312106241aSJie Yang }
2322106241aSJie Yang EXPORT_SYMBOL_GPL(sst_dsp_inbox_write);
2332106241aSJie Yang 
sst_dsp_inbox_read(struct sst_dsp * sst,void * message,size_t bytes)2342106241aSJie Yang void sst_dsp_inbox_read(struct sst_dsp *sst, void *message, size_t bytes)
2352106241aSJie Yang {
2362106241aSJie Yang 	u32 i;
2372106241aSJie Yang 
2382106241aSJie Yang 	trace_sst_ipc_inbox_read(bytes);
2392106241aSJie Yang 
2402106241aSJie Yang 	memcpy_fromio(message, sst->mailbox.in_base, bytes);
2412106241aSJie Yang 
2422106241aSJie Yang 	for (i = 0; i < bytes; i += 4)
2432106241aSJie Yang 		trace_sst_ipc_inbox_rdata(i, *(u32 *)(message + i));
2442106241aSJie Yang }
2452106241aSJie Yang EXPORT_SYMBOL_GPL(sst_dsp_inbox_read);
2462106241aSJie Yang 
2472106241aSJie Yang /* Module information */
2482106241aSJie Yang MODULE_AUTHOR("Liam Girdwood");
2492106241aSJie Yang MODULE_DESCRIPTION("Intel SST Core");
2502106241aSJie Yang MODULE_LICENSE("GPL v2");
251