From 39f5e2782061835650a80832a4a058921bd79568 Mon Sep 17 00:00:00 2001
From: Gavin Liu <gavin.liu@mediatek.corp-partner.google.com>
Date: Tue, 31 Dec 2024 14:34:00 +0800
Subject: [PATCH] feat(mt8196): add Mediatek EMI stub implementation for mt8196

Implement stub functions for the EMI driver to ensure that the build
can pass when a prebuilt library is not available.

Change-Id: I296945a3df6766a3a133cd385a1e5038ca979403
Signed-off-by: Gavin Liu <gavin.liu@mediatek.corp-partner.google.com>
---
 plat/mediatek/drivers/emi/common/emi.h      | 16 ++++++++++++++++
 plat/mediatek/drivers/emi/emi_stub.c        | 15 +++++++++++++++
 plat/mediatek/drivers/emi/rules.mk          | 15 +++++++++++++++
 plat/mediatek/include/mtk_sip_def.h         |  7 +++++--
 plat/mediatek/mt8196/include/platform_def.h | 17 +++++++++++++++--
 plat/mediatek/mt8196/platform.mk            |  1 +
 6 files changed, 67 insertions(+), 4 deletions(-)
 create mode 100644 plat/mediatek/drivers/emi/common/emi.h
 create mode 100644 plat/mediatek/drivers/emi/emi_stub.c
 create mode 100644 plat/mediatek/drivers/emi/rules.mk

diff --git a/plat/mediatek/drivers/emi/common/emi.h b/plat/mediatek/drivers/emi/common/emi.h
new file mode 100644
index 000000000..eb2a0d3a7
--- /dev/null
+++ b/plat/mediatek/drivers/emi/common/emi.h
@@ -0,0 +1,16 @@
+/*
+ * Copyright (c) 2025, Mediatek Inc. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#ifndef EMI_H
+#define EMI_H
+
+#include <stdint.h>
+
+#define EMI_MPU_ALIGN_BITS	12
+
+uint64_t sip_emi_mpu_set_protection(u_register_t start, u_register_t end, u_register_t region);
+
+#endif /* EMI_H */
diff --git a/plat/mediatek/drivers/emi/emi_stub.c b/plat/mediatek/drivers/emi/emi_stub.c
new file mode 100644
index 000000000..3682bf7b3
--- /dev/null
+++ b/plat/mediatek/drivers/emi/emi_stub.c
@@ -0,0 +1,15 @@
+/*
+ * Copyright (c) 2025, Mediatek Inc. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+#include <common/debug.h>
+
+#include "common/emi.h"
+#include <mtk_sip_svc.h>
+
+uint64_t sip_emi_mpu_set_protection(u_register_t start, u_register_t end,
+				    u_register_t region)
+{
+	return MTK_SIP_E_NOT_SUPPORTED;
+}
diff --git a/plat/mediatek/drivers/emi/rules.mk b/plat/mediatek/drivers/emi/rules.mk
new file mode 100644
index 000000000..9f462bbdc
--- /dev/null
+++ b/plat/mediatek/drivers/emi/rules.mk
@@ -0,0 +1,15 @@
+#
+# Copyright (c) 2025, MediaTek Inc. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+
+LOCAL_DIR := $(call GET_LOCAL_DIR)
+
+MODULE := emi
+
+ifeq ($(MTKLIB_PATH),)
+LOCAL_SRCS-y := $(LOCAL_DIR)/emi_stub.c
+endif
+
+$(eval $(call MAKE_MODULE,$(MODULE),$(LOCAL_SRCS-y),$(MTK_BL)))
diff --git a/plat/mediatek/include/mtk_sip_def.h b/plat/mediatek/include/mtk_sip_def.h
index ff12408cf..6f496d259 100644
--- a/plat/mediatek/include/mtk_sip_def.h
+++ b/plat/mediatek/include/mtk_sip_def.h
@@ -13,6 +13,7 @@
 	_func(MTK_SIP_KERNEL_DFD, 0x205) \
 	_func(MTK_SIP_KERNEL_MSDC, 0x273) \
 	_func(MTK_SIP_VCORE_CONTROL, 0x506) \
+	_func(MTK_SIP_EMIDBG_CONTROL, 0x50B) \
 	_func(MTK_SIP_IOMMU_CONTROL, 0x514) \
 	_func(MTK_SIP_AUDIO_CONTROL, 0x517) \
 	_func(MTK_SIP_APUSYS_CONTROL, 0x51E) \
@@ -21,9 +22,11 @@
 	_func(MTK_SIP_KERNEL_VCP_CONTROL, 0x52C)
 
 #define MTK_SIP_SMC_FROM_S_EL1_TABLE(_func) \
-	_func(MTK_SIP_TEE_MPU_PERM_SET, 0x031)
+	_func(MTK_SIP_TEE_MPU_PERM_SET, 0x031) \
+	_func(MTK_SIP_TEE_EMI_MPU_CONTROL, 0x048)
 
 #define MTK_SIP_SMC_FROM_BL33_TABLE(_func) \
-	_func(MTK_SIP_KERNEL_BOOT, 0x115)
+	_func(MTK_SIP_KERNEL_BOOT, 0x115) \
+	_func(MTK_SIP_BL_EMIMPU_CONTROL, 0x415)
 
 #endif /* MTK_SIP_DEF_H */
diff --git a/plat/mediatek/mt8196/include/platform_def.h b/plat/mediatek/mt8196/include/platform_def.h
index 4f4089030..6b6416ac0 100644
--- a/plat/mediatek/mt8196/include/platform_def.h
+++ b/plat/mediatek/mt8196/include/platform_def.h
@@ -169,8 +169,21 @@
 /*******************************************************************************
  * EMI MPU related constants
  *******************************************************************************/
-#define EMI_MPU_BASE		(IO_PHYS + 0x00428000)
-#define SUB_EMI_MPU_BASE	(IO_PHYS + 0x00528000)
+#define EMI_MPU_BASE			(IO_PHYS + 0x00428000)
+#define SUB_EMI_MPU_BASE		(IO_PHYS + 0x00528000)
+#define EMI_SLB_BASE			(IO_PHYS + 0x0042e000)
+#define SUB_EMI_SLB_BASE		(IO_PHYS + 0x0052e000)
+#define CHN0_EMI_APB_BASE		(IO_PHYS + 0x00201000)
+#define CHN1_EMI_APB_BASE		(IO_PHYS + 0x00205000)
+#define CHN2_EMI_APB_BASE		(IO_PHYS + 0x00209000)
+#define CHN3_EMI_APB_BASE		(IO_PHYS + 0x0020D000)
+#define EMI_APB_BASE			(IO_PHYS + 0x00429000)
+#define INFRA_EMI_DEBUG_CFG_BASE	(IO_PHYS + 0x00425000)
+#define NEMI_SMPU_BASE			(IO_PHYS + 0x0042f000)
+#define SEMI_SMPU_BASE			(IO_PHYS + 0x0052f000)
+#define SUB_EMI_APB_BASE		(IO_PHYS + 0x00529000)
+#define SUB_INFRA_EMI_DEBUG_CFG_BASE	(IO_PHYS + 0x00525000)
+#define SUB_INFRACFG_AO_MEM_BASE	(IO_PHYS + 0x00504000)
 
 /*******************************************************************************
  * System counter frequency related constants
diff --git a/plat/mediatek/mt8196/platform.mk b/plat/mediatek/mt8196/platform.mk
index 3c827c951..30cca3cf2 100644
--- a/plat/mediatek/mt8196/platform.mk
+++ b/plat/mediatek/mt8196/platform.mk
@@ -27,6 +27,7 @@ MODULES-y += $(MTK_PLAT)/lib/mtk_init
 MODULES-y += $(MTK_PLAT)/lib/pm
 MODULES-y += $(MTK_PLAT)/drivers/apusys
 MODULES-y += $(MTK_PLAT)/drivers/dp
+MODULES-y += $(MTK_PLAT)/drivers/emi
 MODULES-y += $(MTK_PLAT)/drivers/mcusys
 MODULES-y += $(MTK_PLAT)/drivers/timer
 MODULES-y += $(MTK_PLAT)/drivers/vcp