From 2e3f214f5990b8575778dcbe65bc8a987d35bfec Mon Sep 17 00:00:00 2001 From: Andrew Perepech Date: Tue, 1 Apr 2025 16:57:55 -0700 Subject: [PATCH 1/7] platform/mtk: Set AFE unused reg to 0 instead of -1. MTK AFE driver check registers for 0 instead of -1 when not in use. Signed-off-by: Andrew Perepech --- src/platform/mtk/dai.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/platform/mtk/dai.c b/src/platform/mtk/dai.c index 0cc22445ca2f..ccd48f7a2da6 100644 --- a/src/platform/mtk/dai.c +++ b/src/platform/mtk/dai.c @@ -70,7 +70,7 @@ struct afe_cfg { */ static void cfg_convert(const struct afe_cfg *src, struct mtk_base_memif_data *dst) { -#define REGCVT(R) (((R) > 0) ? ((R) - MTK_AFE_BASE) : -1) +#define REGCVT(R) (((R) > 0) ? ((R) - MTK_AFE_BASE) : 0) #define COPYBIT(S, Dr, Ds) do { \ dst->Dr = REGCVT(src->S.reg); \ From fa92c53202e5b0a8adf63fc883d6cae37ca48592 Mon Sep 17 00:00:00 2001 From: Andrew Perepech Date: Mon, 19 May 2025 17:01:22 -0700 Subject: [PATCH 2/7] platform/mtk: Add FS timings for MT818X, MT8195 Add sampling rate Hz to reg value conversion tables for MT8186, MT8188 and MT8195 Signed-off-by: Andrew Perepech --- src/platform/mtk/dai.c | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/platform/mtk/dai.c b/src/platform/mtk/dai.c index ccd48f7a2da6..ad6991003fcc 100644 --- a/src/platform/mtk/dai.c +++ b/src/platform/mtk/dai.c @@ -203,6 +203,42 @@ static const struct dai_info mtk_dai_info = { static unsigned int mtk_afe_fs_timing(unsigned int rate) { static const struct { int hz, reg; } rate2reg[] = { +#if defined(CONFIG_SOC_MT8188) || defined(CONFIG_SOC_MT8195) + { 7350, 16 }, + { 8000, 0 }, + { 11025, 17 }, + { 12000, 1 }, + { 14700, 18 }, + { 16000, 2 }, + { 22050, 19 }, + { 24000, 3 }, + { 29400, 20 }, + { 32000, 4 }, + { 44100, 21 }, + { 48000, 5 }, + { 88200, 22 }, + { 96000, 6 }, + { 176400, 23 }, + { 192000, 7 }, + { 352800, 24 }, + { 384000, 8 }, +#elif defined(CONFIG_SOC_MT8186) + { 8000, 0 }, + { 11025, 1 }, + { 12000, 2 }, + { 16000, 4 }, + { 22050, 5 }, + { 24000, 6 }, + { 32000, 8 }, + { 44100, 9 }, + { 48000, 10 }, + { 88200, 11 }, + { 96000, 12 }, + { 176400, 13 }, + { 192000, 14 }, + { 352800, 7 }, + { 384000, 3 }, +#else { 8000, 0 }, { 11025, 1 }, { 12000, 2 }, @@ -218,6 +254,7 @@ static unsigned int mtk_afe_fs_timing(unsigned int rate) { 192000, 18 }, { 352800, 21 }, { 384000, 22 }, +#endif }; for (int i = 0; i < ARRAY_SIZE(rate2reg); i++) From cacbaadac815bd023c8ad340b1c37f915c0b3289 Mon Sep 17 00:00:00 2001 From: Andrew Perepech Date: Tue, 27 May 2025 15:12:48 -0700 Subject: [PATCH 3/7] zephyr: alloc: Limit heap size on MTK platforms MTK platforms are built with CONFIG_SYS_HEAP_SMALL_ONLY. If heap size is greater than 0x7fff chunk units, it throws an assert at boot. Signed-off-by: Andrew Perepech --- zephyr/lib/alloc.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/zephyr/lib/alloc.c b/zephyr/lib/alloc.c index 77c5aaeccf3e..b81de8e3ea61 100644 --- a/zephyr/lib/alloc.c +++ b/zephyr/lib/alloc.c @@ -113,7 +113,13 @@ extern char _mtk_adsp_sram_end; #define SRAM_SIZE DT_REG_SIZE(DT_NODELABEL(sram0)) #define SRAM_END (SRAM_START + SRAM_SIZE) #define heapmem ((uint8_t *)ALIGN_UP((uintptr_t)&_mtk_adsp_sram_end, PLATFORM_DCACHE_ALIGN)) + +/* Heap size is limited to 0x7fffU chunk units when CONFIG_SYS_HEAP_SMALL_ONLY is set */ +#if defined(CONFIG_SYS_HEAP_SMALL_ONLY) +#define HEAPMEM_SIZE MIN(((uint8_t *)SRAM_END - heapmem), 0x7fff * 8) +#else #define HEAPMEM_SIZE ((uint8_t *)SRAM_END - heapmem) +#endif /* CONFIG_SYS_HEAP_SMALL_ONLY */ #else From 85ab536730d5a539ae77cbd2a2d225aa26bbe5ef Mon Sep 17 00:00:00 2001 From: Andrew Perepech Date: Tue, 27 May 2025 15:21:34 -0700 Subject: [PATCH 4/7] drivers/mediatek/afe: Fix DMA buffer alignment for MT818X and MT8195 DMA buffer alignment for MT818X and MT8195 must be set to 0x40 Signed-off-by: Andrew Perepech --- src/drivers/mediatek/afe/afe-memif.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/drivers/mediatek/afe/afe-memif.c b/src/drivers/mediatek/afe/afe-memif.c index 5ca7d201757f..233eaec63748 100644 --- a/src/drivers/mediatek/afe/afe-memif.c +++ b/src/drivers/mediatek/afe/afe-memif.c @@ -437,7 +437,11 @@ static int memif_get_attribute(struct dma *dma, uint32_t type, uint32_t *value) *value = 4; break; case DMA_ATTR_BUFFER_ADDRESS_ALIGNMENT: +#if defined(CONFIG_SOC_SERIES_MT818X) || defined(CONFIG_SOC_MT8195) + *value = 64; +#else *value = 16; +#endif break; case DMA_ATTR_BUFFER_PERIOD_COUNT: *value = 4; From 18f80dcaea649dfe15ef486d0b814b1b32b21813 Mon Sep 17 00:00:00 2001 From: Andrew Perepech Date: Wed, 4 Jun 2025 12:33:21 -0700 Subject: [PATCH 5/7] platform: mtk: Add AFE buffer address translation Add AFE buffer address translation between CPU and ADSP for MT818X and MT8195 platforms. AFE buffer is allocated in heap located in ADSP SRAM. SRAM base address is not the same for CPU and ADSP address space. AFE hardware accept buffer base address in CPU address space only. Signed-off-by: Andrew Perepech --- src/platform/mtk/dai.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/platform/mtk/dai.c b/src/platform/mtk/dai.c index ad6991003fcc..cb6ebc144624 100644 --- a/src/platform/mtk/dai.c +++ b/src/platform/mtk/dai.c @@ -12,16 +12,25 @@ */ #if defined(CONFIG_SOC_MT8186) #define MTK_AFE_BASE 0x11210000 +#define SRAM_CPU_START 0x10800000 #elif defined(CONFIG_SOC_SERIES_MT818X) #define MTK_AFE_BASE 0x10b10000 +#define SRAM_CPU_START 0x10d00000 #elif defined(CONFIG_SOC_MT8195) #define MTK_AFE_BASE 0x10890000 +#define SRAM_CPU_START 0x10840000 #elif defined(CONFIG_SOC_MT8196) #define MTK_AFE_BASE 0x1a110000 +#define SRAM_CPU_START 0x1a210000 #else #error Unrecognized device #endif +#define SRAM_ADSP_START DT_REG_ADDR(DT_NODELABEL(sram0)) +#define SRAM_SIZE DT_REG_SIZE(DT_NODELABEL(sram0)) +#define SRAM_ADSP_END (SRAM_ADSP_START + SRAM_SIZE) +#define SRAM_CPU_END (SRAM_CPU_START + SRAM_SIZE) + /* Bitfield register: address, left shift amount, and number of bits */ struct afe_bitfld { uint32_t reg; @@ -199,6 +208,28 @@ static const struct dai_info mtk_dai_info = { .num_dai_types = ARRAY_SIZE(mtk_dai_types), }; +#if defined(CONFIG_SOC_SERIES_MT818X) || defined(CONFIG_SOC_MT8195) +static unsigned int mtk_afe2adsp_addr(unsigned int addr) +{ + /* CPU -> ADSP address remap */ + if ((addr >= SRAM_CPU_START) && (addr < SRAM_CPU_END)) { + addr = SRAM_ADSP_START + (addr - SRAM_CPU_START); + } + + return addr; +} + +static unsigned int mtk_adsp2afe_addr(unsigned int addr) +{ + /* ADSP -> CPU address remap */ + if ((addr >= SRAM_ADSP_START) && (addr < SRAM_ADSP_END)) { + addr = SRAM_CPU_START + (addr - SRAM_ADSP_START); + } + + return addr; +} +#endif + /* Static table of fs register values. TODO: binary search */ static unsigned int mtk_afe_fs_timing(unsigned int rate) { @@ -279,6 +310,10 @@ struct mtk_base_afe_platform mtk_afe_platform = { .dais_size = ARRAY_SIZE(mtk_dais), .afe_fs = mtk_afe_fs, .irq_fs = mtk_afe_fs_timing, +#if defined(CONFIG_SOC_SERIES_MT818X) || defined(CONFIG_SOC_MT8195) + .afe2adsp_addr = mtk_afe2adsp_addr, + .adsp2afe_addr = mtk_adsp2afe_addr, +#endif }; int mtk_dai_init(struct sof *sof) From 46e5cd98432575ce9c691d1dcf8ae2354fb7c0a3 Mon Sep 17 00:00:00 2001 From: Andrew Perepech Date: Mon, 21 Jul 2025 21:56:22 -0700 Subject: [PATCH 6/7] zephyr: lib/dma.h: Fix build error in dma-legacy.h Reinstate 'struct dma' data type for platforms compiled with CONFIG_ZEPHYR_NATIVE_DRIVERS undefined. Signed-off-by: Andrew Perepech --- zephyr/include/sof/lib/dma.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/zephyr/include/sof/lib/dma.h b/zephyr/include/sof/lib/dma.h index 1b384412b123..3c7f56d7b5f5 100644 --- a/zephyr/include/sof/lib/dma.h +++ b/zephyr/include/sof/lib/dma.h @@ -122,7 +122,11 @@ enum sof_dma_cb_status { /* Attributes have been ported to Zephyr. This condition is necessary until full support of * CONFIG_SOF_ZEPHYR_STRICT_HEADERS. */ +#ifdef CONFIG_ZEPHYR_NATIVE_DRIVERS struct sof_dma; +#else +struct dma; +#endif /** * \brief Element of SG list (as array item). @@ -194,7 +198,11 @@ struct dma_plat_data { uint32_t period_count; }; +#ifdef CONFIG_ZEPHYR_NATIVE_DRIVERS struct sof_dma { +#else +struct dma { +#endif struct dma_plat_data plat_data; struct k_spinlock lock; /**< locking mechanism */ int sref; /**< simple ref counter, guarded by lock */ @@ -206,7 +214,11 @@ struct sof_dma { }; struct dma_chan_data { +#ifdef CONFIG_ZEPHYR_NATIVE_DRIVERS struct sof_dma *dma; +#else + struct dma *dma; +#endif uint32_t status; uint32_t direction; @@ -224,14 +236,22 @@ struct dma_chan_data { }; struct dma_info { +#ifdef CONFIG_ZEPHYR_NATIVE_DRIVERS struct sof_dma *dma_array; +#else + struct dma *dma_array; +#endif size_t num_dmas; }; /* generic DMA DSP <-> Host copier */ struct dma_copy { struct dma_chan_data *chan; +#ifdef CONFIG_ZEPHYR_NATIVE_DRIVERS struct sof_dma *dmac; +#else + struct dma *dmac; +#endif }; struct audio_stream; From 46515b075c0bb9837da82364026bec1ce1530247 Mon Sep 17 00:00:00 2001 From: Andrew Perepech Date: Thu, 24 Jul 2025 13:05:50 -0700 Subject: [PATCH 7/7] boards: mtk: Fix "dai_common_copy(): nothing to copy" warning Override CONFIG_SYS_CLOCK_TICKS_PER_SEC project default setting so 1 millisecond is exactly represented by an integral number of ticks. This fixes warning: dai_comp: comp:1.2 dai_common_copy(): nothing to copy Signed-off-by: Andrew Perepech --- app/boards/mt8188_mt8188_adsp.conf | 4 ++++ app/boards/mt8195_mt8195_adsp.conf | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/app/boards/mt8188_mt8188_adsp.conf b/app/boards/mt8188_mt8188_adsp.conf index 0d44864b6144..0b77906a891f 100644 --- a/app/boards/mt8188_mt8188_adsp.conf +++ b/app/boards/mt8188_mt8188_adsp.conf @@ -4,3 +4,7 @@ # Board-level config goes in Zephyr (and ideally in DTS). App-level # config goes in prj.conf. CONFIG_MTK=y + +# Override project default setting so 1 millisecond is exactly +# represented by an integral number of ticks. +CONFIG_SYS_CLOCK_TICKS_PER_SEC=13000 diff --git a/app/boards/mt8195_mt8195_adsp.conf b/app/boards/mt8195_mt8195_adsp.conf index 0d44864b6144..0b77906a891f 100644 --- a/app/boards/mt8195_mt8195_adsp.conf +++ b/app/boards/mt8195_mt8195_adsp.conf @@ -4,3 +4,7 @@ # Board-level config goes in Zephyr (and ideally in DTS). App-level # config goes in prj.conf. CONFIG_MTK=y + +# Override project default setting so 1 millisecond is exactly +# represented by an integral number of ticks. +CONFIG_SYS_CLOCK_TICKS_PER_SEC=13000