[Feature]add MT2731_MP2_MR2_SVN388 baseline version
Change-Id: Ief04314834b31e27effab435d3ca8ba33b499059
diff --git a/src/bsp/trustzone/teeloader/mt6771/Makefile b/src/bsp/trustzone/teeloader/mt6771/Makefile
new file mode 100644
index 0000000..4f5705d
--- /dev/null
+++ b/src/bsp/trustzone/teeloader/mt6771/Makefile
@@ -0,0 +1,54 @@
+CC := ${CROSS_COMPILE}gcc
+AR := ${CROSS_COMPILE}ar
+LD := ${CROSS_COMPILE}ld
+OBJCOPY := ${CROSS_COMPILE}objcopy
+
+LDS = tllink.lds
+
+DIR_INC = ./include
+DIR_SRC = ./src
+DIR_PREBUILT = ./prebuild
+DIR_OBJ = ${TL_RAW_OUT}/obj
+DIR_BIN = ${TL_RAW_OUT}/bin
+
+ASRCS = $(wildcard $(DIR_SRC)/*.s)
+CSRCS = $(wildcard $(DIR_SRC)/*.c)
+CSRCS += \
+ $(DIR_SRC)/drivers/tz_dapc.c \
+ $(DIR_SRC)/drivers/tz_emi_mpu.c
+
+VPATH = $(DIR_SRC):$(DIR_SRC)/drivers
+SRCS = $(ASRCS) $(CSRCS)
+AOBJS = $(patsubst %.s, $(DIR_OBJ)/%.o, $(notdir $(ASRCS)))
+COBJS = $(patsubst %.c, $(DIR_OBJ)/%.o, $(notdir $(CSRCS)))
+SOBJS = $(wildcard $(DIR_PREBUILT)/*.a)
+OBJS = $(AOBJS) $(COBJS) $(SOBJS)
+
+TARGET = teeloader
+BIN_TARGET = $(DIR_BIN)/$(TARGET)
+
+all: $(OBJS)
+ @if [ ! -d `dirname $(BIN_TARGET).elf` ] ; then \
+ mkdir -p `dirname $(BIN_TARGET).elf`; \
+ fi
+ sed "s/%BASE_ADDR%/${BASE_ADDR}/g" $(LDS) > $(DIR_OBJ)/$(LDS)
+ $(LD) --start-group $^ --end-group -T$(DIR_OBJ)/$(LDS) -o $(BIN_TARGET).elf
+ -echo "teeloader binary created"
+ $(OBJCOPY) -O binary $(BIN_TARGET).elf $(BIN_TARGET).bin
+ ./zero_padding.sh $(BIN_TARGET).bin ${TL_ALIGN_SIZE}
+
+$(DIR_OBJ)/%.o: %.c
+ @if [ ! -d `dirname $@` ] ; then \
+ mkdir -p `dirname $@`; \
+ fi
+ $(CC) -I$(DIR_INC) -DBASE_ADDR=${BASE_ADDR} -DTL_ALIGN_SIZE=${TL_ALIGN_SIZE} -DTRUSTEDOS_ENTRYPOINT=${TRUSTEDOS_ENTRYPOINT} -c $(filter %$(patsubst %.o,%.c,$(notdir $@)),$(CSRCS)) -o $@
+
+$(DIR_OBJ)/%.o: %.s
+ @if [ ! -d `dirname $@` ] ; then \
+ mkdir -p `dirname $@`; \
+ fi
+ $(CC) -c $^ -o $@
+
+.PHONY: clean
+clean:
+ -@rm -rf $(DIR_OBJ)/* $(DIR_BIN)/*
diff --git a/src/bsp/trustzone/teeloader/mt6771/include/print.h b/src/bsp/trustzone/teeloader/mt6771/include/print.h
new file mode 100644
index 0000000..1b06fb0
--- /dev/null
+++ b/src/bsp/trustzone/teeloader/mt6771/include/print.h
@@ -0,0 +1,43 @@
+/* Copyright Statement:
+ *
+ * This software/firmware and related documentation ("MediaTek Software") are
+ * protected under relevant copyright laws. The information contained herein is
+ * confidential and proprietary to MediaTek Inc. and/or its licensors. Without
+ * the prior written permission of MediaTek inc. and/or its licensors, any
+ * reproduction, modification, use or disclosure of MediaTek Software, and
+ * information contained herein, in whole or in part, shall be strictly
+ * prohibited.
+ *
+ * MediaTek Inc. (C) 2016. All rights reserved.
+ *
+ * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
+ * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
+ * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
+ * ON AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL
+ * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
+ * NONINFRINGEMENT. NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH
+ * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
+ * INCORPORATED IN, OR SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES
+ * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
+ * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
+ * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN MEDIATEK
+ * SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE
+ * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
+ * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S
+ * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE
+ * RELEASED HEREUNDER WILL BE, AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE
+ * MEDIATEK SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
+ * CHARGE PAID BY RECEIVER TO MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
+ *
+ * The following software/firmware and/or related documentation ("MediaTek
+ * Software") have been modified by MediaTek Inc. All revisions are subject to
+ * any receiver's applicable license agreements with MediaTek Inc.
+ */
+
+#ifndef __PRINT_H__
+#define __PRINT_H__
+
+void tl_printf(char *fmt, ...);
+
+#endif /* __PRINT_H__ */
diff --git a/src/bsp/trustzone/teeloader/mt6771/include/typedefs.h b/src/bsp/trustzone/teeloader/mt6771/include/typedefs.h
new file mode 100644
index 0000000..9d2a01e
--- /dev/null
+++ b/src/bsp/trustzone/teeloader/mt6771/include/typedefs.h
@@ -0,0 +1,65 @@
+/* Copyright Statement:
+ *
+ * This software/firmware and related documentation ("MediaTek Software") are
+ * protected under relevant copyright laws. The information contained herein is
+ * confidential and proprietary to MediaTek Inc. and/or its licensors. Without
+ * the prior written permission of MediaTek inc. and/or its licensors, any
+ * reproduction, modification, use or disclosure of MediaTek Software, and
+ * information contained herein, in whole or in part, shall be strictly
+ * prohibited.
+ *
+ * MediaTek Inc. (C) 2016. All rights reserved.
+ *
+ * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
+ * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
+ * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
+ * ON AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL
+ * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
+ * NONINFRINGEMENT. NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH
+ * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
+ * INCORPORATED IN, OR SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES
+ * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
+ * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
+ * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN MEDIATEK
+ * SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE
+ * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
+ * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S
+ * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE
+ * RELEASED HEREUNDER WILL BE, AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE
+ * MEDIATEK SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
+ * CHARGE PAID BY RECEIVER TO MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
+ *
+ * The following software/firmware and/or related documentation ("MediaTek
+ * Software") have been modified by MediaTek Inc. All revisions are subject to
+ * any receiver's applicable license agreements with MediaTek Inc.
+ */
+
+#ifndef __TYPEDEFS_H__
+#define __TYPEDEFS_H__
+
+typedef unsigned long ulong;
+typedef unsigned char uchar;
+typedef unsigned int uint;
+typedef signed char int8;
+typedef signed short int16;
+typedef signed long int32;
+typedef signed int intx;
+typedef unsigned char uint8;
+typedef unsigned short uint16;
+typedef unsigned long uint32;
+typedef unsigned int uintx;
+
+typedef unsigned int UINT32;
+typedef volatile unsigned int *P_U32;
+
+typedef unsigned char u8;
+typedef signed char s8;
+typedef unsigned short u16;
+typedef signed short s16;
+typedef unsigned int u32;
+typedef signed int s32;
+typedef unsigned long long u64;
+typedef signed long long s64;
+
+#endif /* __TYPEDEFS_H__ */
diff --git a/src/bsp/trustzone/teeloader/mt6771/include/tz_dapc.h b/src/bsp/trustzone/teeloader/mt6771/include/tz_dapc.h
new file mode 100644
index 0000000..257989d
--- /dev/null
+++ b/src/bsp/trustzone/teeloader/mt6771/include/tz_dapc.h
@@ -0,0 +1,169 @@
+/* Copyright Statement:
+ *
+ * This software/firmware and related documentation ("MediaTek Software") are
+ * protected under relevant copyright laws. The information contained herein is
+ * confidential and proprietary to MediaTek Inc. and/or its licensors. Without
+ * the prior written permission of MediaTek inc. and/or its licensors, any
+ * reproduction, modification, use or disclosure of MediaTek Software, and
+ * information contained herein, in whole or in part, shall be strictly
+ * prohibited.
+ *
+ * MediaTek Inc. (C) 2017. All rights reserved.
+ *
+ * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
+ * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
+ * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
+ * ON AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL
+ * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
+ * NONINFRINGEMENT. NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH
+ * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
+ * INCORPORATED IN, OR SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES
+ * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
+ * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
+ * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN MEDIATEK
+ * SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE
+ * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
+ * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S
+ * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE
+ * RELEASED HEREUNDER WILL BE, AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE
+ * MEDIATEK SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
+ * CHARGE PAID BY RECEIVER TO MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
+ *
+ * The following software/firmware and/or related documentation ("MediaTek
+ * Software") have been modified by MediaTek Inc. All revisions are subject to
+ * any receiver's applicable license agreements with MediaTek Inc.
+ */
+
+#ifndef DEVICE_APC_H
+#define DEVICE_APC_H
+
+#include "typedefs.h"
+
+#define DEVAPC_AO_INFRA_BASE 0x1000E000 // for INFRA/PERI
+#define DEVAPC_AO_MD_BASE 0x10019000 // for MD
+#define DEVAPC_AO_MM_BASE 0x1001C000 // for MM
+
+/*******************************************************************************
+ * REGISTER ADDRESS DEFINATION
+ ******************************************************************************/
+/* Device APC AO for INFRA/PERI */
+
+#define DEVAPC_AO_INFRA_MAS_DOM_0 ((volatile unsigned int*)(DEVAPC_AO_INFRA_BASE+0x0A00))
+#define DEVAPC_AO_INFRA_MAS_DOM_1 ((volatile unsigned int*)(DEVAPC_AO_INFRA_BASE+0x0A04))
+#define DEVAPC_AO_INFRA_MAS_DOM_2 ((volatile unsigned int*)(DEVAPC_AO_INFRA_BASE+0x0A08))
+#define DEVAPC_AO_INFRA_MAS_DOM_3 ((volatile unsigned int*)(DEVAPC_AO_INFRA_BASE+0x0A0C))
+
+#define DEVAPC_AO_INFRA_MAS_SEC_0 ((volatile unsigned int*)(DEVAPC_AO_INFRA_BASE+0x0B00))
+
+#define DEVAPC_AO_INFRA_DOM_RMP_0 ((volatile unsigned int*)(DEVAPC_AO_INFRA_BASE+0x0D00))
+#define DEVAPC_AO_INFRA_DOM_RMP_1 ((volatile unsigned int*)(DEVAPC_AO_INFRA_BASE+0x0D04))
+
+#define DEVAPC_AO_INFRA_APC_CON ((volatile unsigned int*)(DEVAPC_AO_INFRA_BASE+0x0F00))
+
+/* ---------------------------------------------------------------------------------------- */
+/* Device APC AO for MD */
+
+#define DEVAPC_AO_MD_DOM_RMP_0 ((volatile unsigned int*)(DEVAPC_AO_MD_BASE+0x0D00))
+
+#define DEVAPC_AO_MD_APC_CON ((volatile unsigned int*)(DEVAPC_AO_MD_BASE+0x0F00))
+
+/* ---------------------------------------------------------------------------------------- */
+/* Device APC AO for MM */
+
+#define DEVAPC_AO_MM_DOM_RMP_0 ((volatile unsigned int*)(DEVAPC_AO_MM_BASE+0x0D00))
+
+#define DEVAPC_AO_MM_APC_CON ((volatile unsigned int*)(DEVAPC_AO_MM_BASE+0x0F00))
+
+/* ---------------------------------------------------------------------------------------- */
+
+#define MOD_NO_IN_1_DEVAPC 16
+#define MASTER_NUM_INFRA 30
+
+typedef enum {
+ NON_SECURE_TRAN = 0,
+ SECURE_TRAN
+} E_TRANSACTION;
+
+typedef enum {
+ MASTER_TYPE_INFRA = 0,
+} E_MASTER_TYPE;
+
+typedef enum {
+ DOMAIN_0 = 0,
+ DOMAIN_1,
+ DOMAIN_2,
+ DOMAIN_3,
+ DOMAIN_4,
+ DOMAIN_5,
+ DOMAIN_6,
+ DOMAIN_7,
+ DOMAIN_8,
+ DOMAIN_9,
+ DOMAIN_10,
+ DOMAIN_11,
+ DOMAIN_12,
+ DOMAIN_13,
+ DOMAIN_14,
+ DOMAIN_15,
+ DOMAIN_MAX,
+} E_DOMAIN;
+
+/* Masks for Domain Control for DEVAPC */
+#define CONN2AP (0xF << 24)
+
+
+
+static inline unsigned int uffs(unsigned int x)
+{
+ unsigned int r = 1;
+
+ if (!x)
+ return 0;
+ if (!(x & 0xffff)) {
+ x >>= 16;
+ r += 16;
+ }
+ if (!(x & 0xff)) {
+ x >>= 8;
+ r += 8;
+ }
+ if (!(x & 0xf)) {
+ x >>= 4;
+ r += 4;
+ }
+ if (!(x & 3)) {
+ x >>= 2;
+ r += 2;
+ }
+ if (!(x & 1)) {
+ x >>= 1;
+ r += 1;
+ }
+ return r;
+}
+
+#define reg_read32(reg) (*(volatile u32* const)(reg))
+#define reg_write32(reg,val) ((*(volatile u32* const)(reg)) = (val))
+
+#define reg_set_bits(reg,bs) ((*(volatile u32*)(reg)) |= (u32)(bs))
+#define reg_clr_bits(reg,bs) ((*(volatile u32*)(reg)) &= ~((u32)(bs)))
+
+#define reg_set_field(reg,field,val) \
+ do { \
+ volatile unsigned int tv = reg_read32(reg); \
+ tv &= ~(field); \
+ tv |= ((val) << (uffs((unsigned int)field) - 1)); \
+ reg_write32(reg,tv); \
+ } while(0)
+
+#define reg_get_field(reg,field,val) \
+ do { \
+ volatile unsigned int tv = reg_read32(reg); \
+ val = ((tv & (field)) >> (uffs((unsigned int)field) - 1)); \
+ } while(0)
+
+
+void tz_dapc_sec_init(void);
+void tz_dapc_sec_postinit(void);
+#endif
diff --git a/src/bsp/trustzone/teeloader/mt6771/include/tz_emi_mpu.h b/src/bsp/trustzone/teeloader/mt6771/include/tz_emi_mpu.h
new file mode 100644
index 0000000..03964fd
--- /dev/null
+++ b/src/bsp/trustzone/teeloader/mt6771/include/tz_emi_mpu.h
@@ -0,0 +1,61 @@
+/* Copyright Statement:
+ *
+ * This software/firmware and related documentation ("MediaTek Software") are
+ * protected under relevant copyright laws. The information contained herein is
+ * confidential and proprietary to MediaTek Inc. and/or its licensors. Without
+ * the prior written permission of MediaTek inc. and/or its licensors, any
+ * reproduction, modification, use or disclosure of MediaTek Software, and
+ * information contained herein, in whole or in part, shall be strictly
+ * prohibited.
+ *
+ * MediaTek Inc. (C) 2017 All rights reserved.
+ *
+ * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
+ * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
+ * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
+ * ON AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL
+ * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
+ * NONINFRINGEMENT. NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH
+ * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
+ * INCORPORATED IN, OR SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES
+ * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
+ * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
+ * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN MEDIATEK
+ * SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE
+ * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
+ * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S
+ * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE
+ * RELEASED HEREUNDER WILL BE, AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE
+ * MEDIATEK SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
+ * CHARGE PAID BY RECEIVER TO MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
+ *
+ * The following software/firmware and/or related documentation ("MediaTek
+ * Software") have been modified by MediaTek Inc. All revisions are subject to
+ * any receiver's applicable license agreements with MediaTek Inc.
+ */
+
+#ifndef TZ_EMI_MPU_H
+#define TZ_EMI_MPU_H
+
+#define EMI_PHY_OFFSET (0x40000000UL)
+//#define EIGHT_DOMAIN
+
+#define NO_PROTECTION (0)
+#define SEC_RW (1)
+#define SEC_RW_NSEC_R (2)
+#define SEC_RW_NSEC_W (3)
+#define SEC_R_NSEC_R (4)
+#define FORBIDDEN (5)
+#define SEC_R_NSEC_RW (6)
+
+#define SECURE_OS_MPU_REGION_ID (0)
+#define ATF_MPU_REGION_ID (1)
+
+#ifdef EIGHT_DOMAIN
+#define SET_ACCESS_PERMISSON(d7, d6, d5, d4, d3, d2, d1, d0) (((d7) << 21) | ((d6) << 18) | ((d5) << 15) | ((d4) << 12) |((d3) << 9) | ((d2) << 6) | ((d1) << 3) | (d0))
+#else
+#define SET_ACCESS_PERMISSON(d3, d2, d1, d0) (((d3) << 9) | ((d2) << 6) | ((d1) << 3) | (d0))
+#endif
+
+#endif /* TZ_EMI_MPU_H */
diff --git a/src/bsp/trustzone/teeloader/mt6771/include/tz_emi_reg.h b/src/bsp/trustzone/teeloader/mt6771/include/tz_emi_reg.h
new file mode 100644
index 0000000..45eea66
--- /dev/null
+++ b/src/bsp/trustzone/teeloader/mt6771/include/tz_emi_reg.h
@@ -0,0 +1,97 @@
+/* Copyright Statement:
+ *
+ * This software/firmware and related documentation ("MediaTek Software") are
+ * protected under relevant copyright laws. The information contained herein is
+ * confidential and proprietary to MediaTek Inc. and/or its licensors. Without
+ * the prior written permission of MediaTek inc. and/or its licensors, any
+ * reproduction, modification, use or disclosure of MediaTek Software, and
+ * information contained herein, in whole or in part, shall be strictly
+ * prohibited.
+ *
+ * MediaTek Inc. (C) 2017. All rights reserved.
+ *
+ * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
+ * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
+ * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
+ * ON AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL
+ * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
+ * NONINFRINGEMENT. NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH
+ * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
+ * INCORPORATED IN, OR SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES
+ * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
+ * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
+ * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN MEDIATEK
+ * SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE
+ * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
+ * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S
+ * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE
+ * RELEASED HEREUNDER WILL BE, AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE
+ * MEDIATEK SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
+ * CHARGE PAID BY RECEIVER TO MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
+ *
+ * The following software/firmware and/or related documentation ("MediaTek
+ * Software") have been modified by MediaTek Inc. All revisions are subject to
+ * any receiver's applicable license agreements with MediaTek Inc.
+ */
+
+#ifndef TZ_EMI_REG_H
+#define TZ_EMI_REG_H
+
+#define EMI_MPU_BASE (0x10226000)
+
+#define EMI_MPU_SA0 ((P_U32)(EMI_MPU_BASE+0x100)) /* EMI MPU start addr 0 */
+#define EMI_MPU_SA1 ((P_U32)(EMI_MPU_BASE+0x104)) /* EMI MPU start addr 1 */
+#define EMI_MPU_SA2 ((P_U32)(EMI_MPU_BASE+0x108)) /* EMI MPU start addr 2 */
+#define EMI_MPU_SA3 ((P_U32)(EMI_MPU_BASE+0x10C)) /* EMI MPU start addr 3 */
+#define EMI_MPU_SA4 ((P_U32)(EMI_MPU_BASE+0x110)) /* EMI MPU start addr 4 */
+#define EMI_MPU_SA5 ((P_U32)(EMI_MPU_BASE+0x114)) /* EMI MPU start addr 5 */
+#define EMI_MPU_SA6 ((P_U32)(EMI_MPU_BASE+0x118)) /* EMI MPU start addr 6 */
+#define EMI_MPU_SA7 ((P_U32)(EMI_MPU_BASE+0x11C)) /* EMI MPU start addr 7 */
+
+#define EMI_MPU_EA0 ((P_U32)(EMI_MPU_BASE+0x200)) /* EMI MPU end addr 0 */
+#define EMI_MPU_EA1 ((P_U32)(EMI_MPU_BASE+0x204)) /* EMI MPU end addr 1 */
+#define EMI_MPU_EA2 ((P_U32)(EMI_MPU_BASE+0x208)) /* EMI MPU end addr 2 */
+#define EMI_MPU_EA3 ((P_U32)(EMI_MPU_BASE+0x20C)) /* EMI MPU end addr 3 */
+#define EMI_MPU_EA4 ((P_U32)(EMI_MPU_BASE+0x210)) /* EMI MPU end addr 4 */
+#define EMI_MPU_EA5 ((P_U32)(EMI_MPU_BASE+0x214)) /* EMI MPU end addr 5 */
+#define EMI_MPU_EA6 ((P_U32)(EMI_MPU_BASE+0x218)) /* EMI MPU end addr 6 */
+#define EMI_MPU_EA7 ((P_U32)(EMI_MPU_BASE+0x21C)) /* EMI MPU end addr 7 */
+
+#define EMI_MPU_APC0 ((P_U32)(EMI_MPU_BASE+0x300)) /* EMI MPU APC 0 */
+#define EMI_MPU_APC1 ((P_U32)(EMI_MPU_BASE+0x304)) /* EMI MPU APC 1 */
+#define EMI_MPU_APC2 ((P_U32)(EMI_MPU_BASE+0x308)) /* EMI MPU APC 2 */
+#define EMI_MPU_APC3 ((P_U32)(EMI_MPU_BASE+0x30C)) /* EMI MPU APC 3 */
+#define EMI_MPU_APC4 ((P_U32)(EMI_MPU_BASE+0x310)) /* EMI MPU APC 4 */
+#define EMI_MPU_APC5 ((P_U32)(EMI_MPU_BASE+0x314)) /* EMI MPU APC 5 */
+#define EMI_MPU_APC6 ((P_U32)(EMI_MPU_BASE+0x318)) /* EMI MPU APC 6 */
+#define EMI_MPU_APC7 ((P_U32)(EMI_MPU_BASE+0x31C)) /* EMI MPU APC 7 */
+
+#define EMI_MPU_CTRL_D0 ((P_U32)(EMI_MPU_BASE+0x800)) /* EMI MPU DOMAIN CTRL 0 */
+#define EMI_MPU_CTRL_D1 ((P_U32)(EMI_MPU_BASE+0x804)) /* EMI MPU DOMAIN CTRL 0 */
+#define EMI_MPU_CTRL_D2 ((P_U32)(EMI_MPU_BASE+0x808)) /* EMI MPU DOMAIN CTRL 0 */
+#define EMI_MPU_CTRL_D3 ((P_U32)(EMI_MPU_BASE+0x80C)) /* EMI MPU DOMAIN CTRL 0 */
+#define EMI_MPU_CTRL_D4 ((P_U32)(EMI_MPU_BASE+0x810)) /* EMI MPU DOMAIN CTRL 0 */
+#define EMI_MPU_CTRL_D5 ((P_U32)(EMI_MPU_BASE+0x814)) /* EMI MPU DOMAIN CTRL 0 */
+#define EMI_MPU_CTRL_D6 ((P_U32)(EMI_MPU_BASE+0x818)) /* EMI MPU DOMAIN CTRL 0 */
+#define EMI_MPU_CTRL_D7 ((P_U32)(EMI_MPU_BASE+0x81C)) /* EMI MPU DOMAIN CTRL 0 */
+
+#define EMI_MPU_CTRL_D0 ((P_U32)(EMI_MPU_BASE+0x800)) /* EMI MPU DOMAIN CTRL 0 */
+#define EMI_MPU_CTRL_D1 ((P_U32)(EMI_MPU_BASE+0x804)) /* EMI MPU DOMAIN CTRL 1 */
+#define EMI_MPU_CTRL_D2 ((P_U32)(EMI_MPU_BASE+0x808)) /* EMI MPU DOMAIN CTRL 2 */
+#define EMI_MPU_CTRL_D3 ((P_U32)(EMI_MPU_BASE+0x80C)) /* EMI MPU DOMAIN CTRL 3 */
+#define EMI_MPU_CTRL_D4 ((P_U32)(EMI_MPU_BASE+0x810)) /* EMI MPU DOMAIN CTRL 4 */
+#define EMI_MPU_CTRL_D5 ((P_U32)(EMI_MPU_BASE+0x814)) /* EMI MPU DOMAIN CTRL 5 */
+#define EMI_MPU_CTRL_D6 ((P_U32)(EMI_MPU_BASE+0x818)) /* EMI MPU DOMAIN CTRL 6 */
+#define EMI_MPU_CTRL_D7 ((P_U32)(EMI_MPU_BASE+0x81C)) /* EMI MPU DOMAIN CTRL 7 */
+
+#define EMI_MPU_MASK_D0 ((P_U32)(EMI_MPU_BASE+0x900)) /* EMI MPU DOMAIN MASK 0 */
+#define EMI_MPU_MASK_D1 ((P_U32)(EMI_MPU_BASE+0x904)) /* EMI MPU DOMAIN MASK 0 */
+#define EMI_MPU_MASK_D2 ((P_U32)(EMI_MPU_BASE+0x908)) /* EMI MPU DOMAIN MASK 0 */
+#define EMI_MPU_MASK_D3 ((P_U32)(EMI_MPU_BASE+0x90C)) /* EMI MPU DOMAIN MASK 0 */
+#define EMI_MPU_MASK_D4 ((P_U32)(EMI_MPU_BASE+0x910)) /* EMI MPU DOMAIN MASK 0 */
+#define EMI_MPU_MASK_D5 ((P_U32)(EMI_MPU_BASE+0x914)) /* EMI MPU DOMAIN MASK 0 */
+#define EMI_MPU_MASK_D6 ((P_U32)(EMI_MPU_BASE+0x918)) /* EMI MPU DOMAIN MASK 0 */
+#define EMI_MPU_MASK_D7 ((P_U32)(EMI_MPU_BASE+0x91C)) /* EMI MPU DOMAIN MASK 0 */
+
+#endif /* TZ_EMI_REG_H */
diff --git a/src/bsp/trustzone/teeloader/mt6771/include/tz_init.h b/src/bsp/trustzone/teeloader/mt6771/include/tz_init.h
new file mode 100644
index 0000000..bba9e9e
--- /dev/null
+++ b/src/bsp/trustzone/teeloader/mt6771/include/tz_init.h
@@ -0,0 +1,84 @@
+/* Copyright Statement:
+ *
+ * This software/firmware and related documentation ("MediaTek Software") are
+ * protected under relevant copyright laws. The information contained herein is
+ * confidential and proprietary to MediaTek Inc. and/or its licensors. Without
+ * the prior written permission of MediaTek inc. and/or its licensors, any
+ * reproduction, modification, use or disclosure of MediaTek Software, and
+ * information contained herein, in whole or in part, shall be strictly
+ * prohibited.
+ *
+ * MediaTek Inc. (C) 2016. All rights reserved.
+ *
+ * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
+ * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
+ * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
+ * ON AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL
+ * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
+ * NONINFRINGEMENT. NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH
+ * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
+ * INCORPORATED IN, OR SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES
+ * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
+ * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
+ * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN MEDIATEK
+ * SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE
+ * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
+ * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S
+ * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE
+ * RELEASED HEREUNDER WILL BE, AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE
+ * MEDIATEK SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
+ * CHARGE PAID BY RECEIVER TO MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
+ *
+ * The following software/firmware and/or related documentation ("MediaTek
+ * Software") have been modified by MediaTek Inc. All revisions are subject to
+ * any receiver's applicable license agreements with MediaTek Inc.
+ */
+
+#ifndef __TZ_INIT_H__
+#define __TZ_INIT_H__
+
+#include "typedefs.h"
+
+#define BL31 0x43001000UL
+#define BL33 0x41e00000UL
+#define BL31_BASE 0x43000000UL
+#define BL31_SIZE 0x00030000UL /* default is 192K Bytes */
+
+#define ATF_BOOT_ARG_ADDR (0x40000000)
+#define TEE_BOOT_ARG_ADDR (0x40001000)
+#define ATF_BOOTCFG_MAGIC (0x4D415446) // String MATF in little-endian
+
+#define DEVINFO_SIZE 4
+
+/* bootarg for ATF */
+typedef struct {
+ u64 bootarg_loc;
+ u64 bootarg_size;
+ u64 bl33_start_addr;
+ u64 tee_info_addr;
+} mtk_bl_param_t;
+
+typedef struct atf_arg_t{
+ unsigned int atf_magic;
+ unsigned int tee_support;
+ unsigned int tee_entry;
+ unsigned int tee_boot_arg_addr;
+ unsigned int hwuid[4]; // HW Unique id for t-base used
+ unsigned int atf_hrid_size; // Check this atf_hrid_size to read from HRID array
+ unsigned int HRID[8]; // HW random id for t-base used
+ unsigned int atf_log_port;
+ unsigned int atf_log_baudrate;
+ unsigned int atf_log_buf_start;
+ unsigned int atf_log_buf_size;
+ unsigned int atf_irq_num;
+ unsigned int devinfo[DEVINFO_SIZE];
+ unsigned int atf_aee_debug_buf_start;
+ unsigned int atf_aee_debug_buf_size;
+ unsigned int msg_fde_key[4]; /* size of message auth key is 16bytes(128 bits) */
+#if CFG_TEE_SUPPORT
+ unsigned int tee_rpmb_size;
+#endif
+}atf_arg_t, *atf_arg_t_ptr;
+
+#endif /* __TZ_INIT_H__ */
diff --git a/src/bsp/trustzone/teeloader/mt6771/include/uart.h b/src/bsp/trustzone/teeloader/mt6771/include/uart.h
new file mode 100644
index 0000000..8efeeca
--- /dev/null
+++ b/src/bsp/trustzone/teeloader/mt6771/include/uart.h
@@ -0,0 +1,60 @@
+/* Copyright Statement:
+ *
+ * This software/firmware and related documentation ("MediaTek Software") are
+ * protected under relevant copyright laws. The information contained herein is
+ * confidential and proprietary to MediaTek Inc. and/or its licensors. Without
+ * the prior written permission of MediaTek inc. and/or its licensors, any
+ * reproduction, modification, use or disclosure of MediaTek Software, and
+ * information contained herein, in whole or in part, shall be strictly
+ * prohibited.
+ *
+ * MediaTek Inc. (C) 2016. All rights reserved.
+ *
+ * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
+ * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
+ * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
+ * ON AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL
+ * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
+ * NONINFRINGEMENT. NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH
+ * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
+ * INCORPORATED IN, OR SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES
+ * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
+ * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
+ * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN MEDIATEK
+ * SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE
+ * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
+ * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S
+ * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE
+ * RELEASED HEREUNDER WILL BE, AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE
+ * MEDIATEK SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
+ * CHARGE PAID BY RECEIVER TO MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
+ *
+ * The following software/firmware and/or related documentation ("MediaTek
+ * Software") have been modified by MediaTek Inc. All revisions are subject to
+ * any receiver's applicable license agreements with MediaTek Inc.
+ */
+
+#ifndef __UART_H__
+#define __UART_H__
+
+typedef unsigned int uint32_t;
+typedef unsigned long uintptr_t;
+
+#define REG32(addr) ((volatile uint32_t *)(uintptr_t)(addr))
+
+#define writel(v, a) (*REG32(a) = (v))
+#define readl(a) (*REG32(a))
+
+#define UART_BASE(uart) (uart)
+#define UART_LSR(uart) (UART_BASE(uart)+0x14)
+#define UART_LSR_THRE (1 << 5)
+#define UART_THR(uart) (UART_BASE(uart)+0x0) /* Write only */
+
+#define IO_PHYS 0x10000000
+#define UART0_BASE (IO_PHYS + 0x01002000)
+
+int uart_putc(char c);
+
+#endif /* __UART_H__ */
+
diff --git a/src/bsp/trustzone/teeloader/mt6771/src/drivers/tz_dapc.c b/src/bsp/trustzone/teeloader/mt6771/src/drivers/tz_dapc.c
new file mode 100644
index 0000000..4c16464
--- /dev/null
+++ b/src/bsp/trustzone/teeloader/mt6771/src/drivers/tz_dapc.c
@@ -0,0 +1,153 @@
+/* Copyright Statement:
+ *
+ * This software/firmware and related documentation ("MediaTek Software") are
+ * protected under relevant copyright laws. The information contained herein is
+ * confidential and proprietary to MediaTek Inc. and/or its licensors. Without
+ * the prior written permission of MediaTek inc. and/or its licensors, any
+ * reproduction, modification, use or disclosure of MediaTek Software, and
+ * information contained herein, in whole or in part, shall be strictly
+ * prohibited.
+ *
+ * MediaTek Inc. (C) 2017. All rights reserved.
+ *
+ * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
+ * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
+ * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
+ * ON AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL
+ * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
+ * NONINFRINGEMENT. NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH
+ * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
+ * INCORPORATED IN, OR SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES
+ * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
+ * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
+ * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN MEDIATEK
+ * SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE
+ * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
+ * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S
+ * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE
+ * RELEASED HEREUNDER WILL BE, AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE
+ * MEDIATEK SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
+ * CHARGE PAID BY RECEIVER TO MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
+ *
+ * The following software/firmware and/or related documentation ("MediaTek
+ * Software") have been modified by MediaTek Inc. All revisions are subject to
+ * any receiver's applicable license agreements with MediaTek Inc.
+ */
+
+/*=======================================================================*/
+/* HEADER FILES */
+/*=======================================================================*/
+#include <tz_dapc.h>
+#include <print.h>
+
+#define _DEBUG_
+#define DBG_DEVAPC
+
+/* Debug message event */
+#define DBG_EVT_NONE (0x00000000U) /* No event */
+#define DBG_EVT_ERR (0x00000001U) /* ERR related event */
+#define DBG_EVT_DOM (0x00000002U) /* DOM related event */
+
+#define DBG_EVT_ALL (0xffffffffU)
+
+#define DBG_EVT_MASK (DBG_EVT_DOM)
+
+#ifdef _DEBUG_
+#define MSG(evt, fmt, args...) \
+ do { \
+ if ((DBG_EVT_##evt) & DBG_EVT_MASK) { \
+ tl_printf(fmt, ##args); \
+ } \
+ } while(0)
+
+#define MSG_FUNC_ENTRY(f) MSG(FUC, "<FUN_ENT>: %s\n", __FUNCTION__)
+#else
+#define MSG(evt, fmt, args...) do{} while(0)
+#define MSG_FUNC_ENTRY(f) do{} while(0)
+#endif
+
+/*=======================================================================*/
+/* STATIC FUNCTIONS */
+/*=======================================================================*/
+static void tz_dapc_set_master_transaction(E_MASTER_TYPE master_type, unsigned int master_index, E_TRANSACTION permisssion_control)
+{
+ volatile unsigned int *base = 0;
+ unsigned int master_register_index = 0;
+ unsigned int master_set_index = 0;
+
+ master_register_index = master_index / (MOD_NO_IN_1_DEVAPC * 2);
+ master_set_index = master_index % (MOD_NO_IN_1_DEVAPC * 2);
+
+ if (master_type == MASTER_TYPE_INFRA && master_index < MASTER_NUM_INFRA) {
+ base = (volatile unsigned int *)((unsigned int)DEVAPC_AO_INFRA_MAS_SEC_0 + master_register_index * 4);
+ } else {
+ return;
+ }
+
+ if (base != 0) {
+ reg_set_field(base, 0x1 << master_set_index, permisssion_control);
+ }
+}
+
+static void DAPC_dom_init(void)
+{
+ /* Set master domain here if needed */
+
+ /* Set CONN2AP to DOMAIN2(CONN) */
+ reg_set_field(DEVAPC_AO_INFRA_MAS_DOM_2, CONN2AP, DOMAIN_2);
+
+#ifdef DBG_DEVAPC
+ MSG(DOM, "Master Domain Setup Infra (0x%x), (0x%x), (0x%x), (0x%x)\n",
+ reg_read32(DEVAPC_AO_INFRA_MAS_DOM_0), reg_read32(DEVAPC_AO_INFRA_MAS_DOM_1),
+ reg_read32(DEVAPC_AO_INFRA_MAS_DOM_2), reg_read32(DEVAPC_AO_INFRA_MAS_DOM_3));
+#endif
+}
+
+static void DAPC_trans_init(void)
+{
+ /* Set master transaction here if needed */
+
+#ifdef DBG_DEVAPC
+ MSG(DOM, "Master Transaction Setup (0x%x)\n", reg_read32(DEVAPC_AO_INFRA_MAS_SEC_0));
+#endif
+
+}
+
+static void DAPC_slave_perm_init(void)
+{
+ /* Set slave permissions here if needed
+ * Default is NO_PROTECTION */
+}
+
+static void tz_dapc_default_setting(void)
+{
+ /* Lock DAPC to secure access only && unmask debug bit && clear VIO status */
+ reg_write32(DEVAPC_AO_INFRA_APC_CON, 0x80000001);
+ reg_write32(DEVAPC_AO_MD_APC_CON, 0x80000001);
+ reg_write32(DEVAPC_AO_MM_APC_CON, 0x80000001);
+
+ /* Set domain of masters */
+ DAPC_dom_init();
+
+ /* Set the transaction type of masters */
+ //DAPC_trans_init();
+
+ /* Set the access permission of slaves in domain 0 */
+ //DAPC_slave_perm_init();
+}
+
+
+/*=======================================================================*/
+/* INIT FUNCTIONS */
+/*=======================================================================*/
+
+void tz_dapc_sec_init(void)
+{
+ /* do initial settings */
+ tz_dapc_default_setting();
+}
+
+void tz_dapc_sec_postinit(void)
+{
+}
diff --git a/src/bsp/trustzone/teeloader/mt6771/src/drivers/tz_emi_mpu.c b/src/bsp/trustzone/teeloader/mt6771/src/drivers/tz_emi_mpu.c
new file mode 100644
index 0000000..5912860
--- /dev/null
+++ b/src/bsp/trustzone/teeloader/mt6771/src/drivers/tz_emi_mpu.c
@@ -0,0 +1,202 @@
+/* Copyright Statement:
+ *
+ * This software/firmware and related documentation ("MediaTek Software") are
+ * protected under relevant copyright laws. The information contained herein is
+ * confidential and proprietary to MediaTek Inc. and/or its licensors. Without
+ * the prior written permission of MediaTek inc. and/or its licensors, any
+ * reproduction, modification, use or disclosure of MediaTek Software, and
+ * information contained herein, in whole or in part, shall be strictly
+ * prohibited.
+ *
+ * MediaTek Inc. (C) 2017. All rights reserved.
+ *
+ * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
+ * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
+ * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
+ * ON AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL
+ * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
+ * NONINFRINGEMENT. NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH
+ * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
+ * INCORPORATED IN, OR SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES
+ * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
+ * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
+ * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN MEDIATEK
+ * SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE
+ * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
+ * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S
+ * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE
+ * RELEASED HEREUNDER WILL BE, AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE
+ * MEDIATEK SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
+ * CHARGE PAID BY RECEIVER TO MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
+ *
+ * The following software/firmware and/or related documentation ("MediaTek
+ * Software") have been modified by MediaTek Inc. All revisions are subject to
+ * any receiver's applicable license agreements with MediaTek Inc.
+ */
+
+#include "print.h"
+#include "typedefs.h"
+#include "tz_init.h"
+#include "tz_emi_mpu.h"
+#include "tz_emi_reg.h"
+
+#define MOD "[TZ_EMI_MPU]"
+
+#define readl(addr) (*(volatile unsigned int*)(addr))
+#define writel(b,addr) (*(volatile unsigned int*)(addr) = b)
+#define IOMEM(reg) (reg)
+
+#define TEE_DEBUG
+#ifdef TEE_DEBUG
+#define DBG_MSG(str, ...) do {tl_printf(str, ##__VA_ARGS__);} while(0)
+#define DBG_INFO(str, ...) do {tl_printf(str, ##__VA_ARGS__);} while(0)
+#else
+#define DBG_MSG(str, ...) do {} while(0)
+#define DBG_INFO(str, ...) do {tl_printf(str, ##__VA_ARGS__);} while(0)
+#endif
+
+
+/*
+ * emi_mpu_set_region_protection: protect a region.
+ * @start: start address of the region
+ * @end: end address of the region
+ * @region: EMI MPU region id
+ * @access_permission: EMI MPU access permission
+ * Return 0 for success, otherwise negative status code.
+ */
+int emi_mpu_set_region_protection(unsigned long start, unsigned long end, int region, unsigned int access_permission)
+{
+ int ret = 0;
+
+ if (end <= start)
+ {
+ DBG_MSG("%s, Invalid address! End address should larger than start address.\n");
+ return -1;
+ }
+
+
+ if((end >> 31) && !(start >> 31))
+ {
+ DBG_MSG("%s, Invalid address! MPU region should not across 32bit. Please divide the memory into two regions.\n");
+ return -1;
+ }
+
+ start = start - EMI_PHY_OFFSET;
+ end = end - EMI_PHY_OFFSET;
+
+
+ /*Address 64KB alignment*/
+ start = start >> 16;
+ end = end >> 16;
+
+ switch (region) {
+ case 0:
+ writel(0, EMI_MPU_APC0);
+ writel(start, EMI_MPU_SA0);
+ writel(end, EMI_MPU_EA0);
+ writel(access_permission, EMI_MPU_APC0);
+ break;
+
+ case 1:
+ writel(0, EMI_MPU_APC1);
+ writel(start, EMI_MPU_SA1);
+ writel(end, EMI_MPU_EA1);
+ writel(access_permission, EMI_MPU_APC1);
+ break;
+
+ case 2:
+ writel(0, EMI_MPU_APC2);
+ writel(start, EMI_MPU_SA2);
+ writel(end, EMI_MPU_EA2);
+ writel(access_permission, EMI_MPU_APC2);
+ break;
+
+ case 3:
+ writel(0, EMI_MPU_APC3);
+ writel(start, EMI_MPU_SA3);
+ writel(end, EMI_MPU_EA3);
+ writel(access_permission, EMI_MPU_APC3);
+ break;
+
+ case 4:
+ writel(0, EMI_MPU_APC4);
+ writel(start, EMI_MPU_SA4);
+ writel(end, EMI_MPU_EA4);
+ writel(access_permission, EMI_MPU_APC4);
+ break;
+
+ case 5:
+ writel(0, EMI_MPU_APC5);
+ writel(start, EMI_MPU_SA5);
+ writel(end, EMI_MPU_EA5);
+ writel(access_permission, EMI_MPU_APC5);
+ break;
+
+ case 6:
+ writel(0, EMI_MPU_APC6);
+ writel(start, EMI_MPU_SA6);
+ writel(end, EMI_MPU_EA6);
+ writel(access_permission, EMI_MPU_APC6);
+ break;
+
+ case 7:
+ writel(0, EMI_MPU_APC7);
+ writel(start, EMI_MPU_SA7);
+ writel(end, EMI_MPU_EA7);
+ writel(access_permission, EMI_MPU_APC7);
+ break;
+
+ default:
+ ret = -1;
+ break;
+ }
+
+ return ret;
+}
+
+void tz_emi_mpu_init(u32 start_add, u32 end_addr, u32 mpu_region)
+{
+ int ret = 0;
+ unsigned int sec_mem_mpu_attr;
+ unsigned int sec_mem_phy_start, sec_mem_phy_end;
+
+ /* Caculate start/end address */
+ sec_mem_phy_start = start_add;
+ sec_mem_phy_end = end_addr;
+
+ switch(mpu_region)
+ {
+ case SECURE_OS_MPU_REGION_ID:
+#ifdef EIGHT_DOMAIN
+ sec_mem_mpu_attr = SET_ACCESS_PERMISSON(FORBIDDEN, FORBIDDEN, FORBIDDEN, SEC_RW, FORBIDDEN, FORBIDDEN, FORBIDDEN, SEC_RW);
+#else
+ sec_mem_mpu_attr = SET_ACCESS_PERMISSON(SEC_RW, FORBIDDEN, FORBIDDEN, SEC_RW);
+#endif
+ break;
+
+ case ATF_MPU_REGION_ID:
+#ifdef EIGHT_DOMAIN
+ sec_mem_mpu_attr = SET_ACCESS_PERMISSON(FORBIDDEN, FORBIDDEN, FORBIDDEN, FORBIDDEN, FORBIDDEN, FORBIDDEN, FORBIDDEN, SEC_RW);
+#else
+ sec_mem_mpu_attr = SET_ACCESS_PERMISSON(FORBIDDEN, FORBIDDEN, FORBIDDEN, SEC_RW);
+#endif
+ break;
+
+ default:
+ DBG_MSG("%s Warning - MPU region '%d' is not supported for preloader!\n", MOD, mpu_region);
+ return;
+ }
+
+ DBG_MSG("%s MPU [0x%x-0x%x]\n", MOD, sec_mem_phy_start, sec_mem_phy_end);
+
+ ret = emi_mpu_set_region_protection(sec_mem_phy_start, /*START_ADDR*/
+ sec_mem_phy_end, /*END_ADDR*/
+ mpu_region, /*region*/
+ sec_mem_mpu_attr);
+
+ if(ret)
+ {
+ DBG_MSG("%s MPU error!!\n", MOD);
+ }
+}
diff --git a/src/bsp/trustzone/teeloader/mt6771/src/main.c b/src/bsp/trustzone/teeloader/mt6771/src/main.c
new file mode 100644
index 0000000..588fdeb
--- /dev/null
+++ b/src/bsp/trustzone/teeloader/mt6771/src/main.c
@@ -0,0 +1,116 @@
+/* Copyright Statement:
+ *
+ * This software/firmware and related documentation ("MediaTek Software") are
+ * protected under relevant copyright laws. The information contained herein is
+ * confidential and proprietary to MediaTek Inc. and/or its licensors. Without
+ * the prior written permission of MediaTek inc. and/or its licensors, any
+ * reproduction, modification, use or disclosure of MediaTek Software, and
+ * information contained herein, in whole or in part, shall be strictly
+ * prohibited.
+ *
+ * MediaTek Inc. (C) 2016. All rights reserved.
+ *
+ * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
+ * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
+ * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
+ * ON AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL
+ * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
+ * NONINFRINGEMENT. NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH
+ * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
+ * INCORPORATED IN, OR SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES
+ * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
+ * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
+ * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN MEDIATEK
+ * SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE
+ * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
+ * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S
+ * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE
+ * RELEASED HEREUNDER WILL BE, AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE
+ * MEDIATEK SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
+ * CHARGE PAID BY RECEIVER TO MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
+ *
+ * The following software/firmware and/or related documentation ("MediaTek
+ * Software") have been modified by MediaTek Inc. All revisions are subject to
+ * any receiver's applicable license agreements with MediaTek Inc.
+ */
+
+#include "typedefs.h"
+#include "tz_init.h"
+#include "tz_emi_mpu.h"
+
+typedef void (*jump_atf)(u64 addr ,u64 arg1) __attribute__ ((__noreturn__));
+
+static u64 trustzone_get_atf_boot_param_addr(void)
+{
+ return ATF_BOOT_ARG_ADDR;
+}
+
+static u64 trustzone_get_tee_boot_param_addr(void)
+{
+ return TEE_BOOT_ARG_ADDR;
+}
+
+static void set_atf_parameters(mtk_bl_param_t *atf_arg)
+{
+ atf_arg->bootarg_loc = 0;
+ atf_arg->bootarg_size = 0;
+ atf_arg->bl33_start_addr = BL33;
+ atf_arg->tee_info_addr = TEE_BOOT_ARG_ADDR;
+}
+
+static void set_tee_parameters(atf_arg_t *tee_arg)
+{
+ /* tee arguments */
+ tee_arg->atf_magic = 0x4D415446;
+ tee_arg->tee_support = 0x1;
+ tee_arg->tee_entry = TRUSTEDOS_ENTRYPOINT;
+ tee_arg->tee_boot_arg_addr = 0x43000100;
+ tee_arg->hwuid[0] = 0x55C09893;
+ tee_arg->hwuid[1] = 0x2B404DDF;
+ tee_arg->hwuid[2] = 0x3ACE08B;
+ tee_arg->hwuid[3] = 0x1092600D;
+ tee_arg->HRID[0] = 0;
+ tee_arg->HRID[1] = 0;
+ tee_arg->atf_log_port = 0x11002000;
+ tee_arg->atf_log_baudrate = 0xE1000;
+ tee_arg->atf_log_buf_start = 0x0;
+ tee_arg->atf_log_buf_size = 0x0;
+ tee_arg->atf_irq_num = 0x119; /* reserve SPI ID 249 for ATF log, which is ID 281 */
+ tee_arg->devinfo[0] = 0;
+ tee_arg->devinfo[1] = 0;
+ tee_arg->devinfo[2] = 0xFFFFFFFF;
+ tee_arg->devinfo[3] = 0xFFFFFFFF;
+ tee_arg->atf_aee_debug_buf_start = 0x0;
+ tee_arg->atf_aee_debug_buf_size = 0x0;
+}
+
+int teeloader_main(unsigned long long bl31_addr, unsigned long long bl33_addr,unsigned long long bl32_addr)
+{
+ u32 bl31_reserve = 0;
+ jump_atf atf_entry;
+
+ mtk_bl_param_t *atf_arg = (mtk_bl_param_t *)trustzone_get_atf_boot_param_addr();
+ atf_arg_t *tee_arg = (atf_arg_t *)trustzone_get_tee_boot_param_addr();
+
+#if 1
+ tz_emi_mpu_init((BL31_BASE & 0xffff0000),
+ (BL31_BASE & 0xffff0000) + BL31_SIZE - 1,
+ ATF_MPU_REGION_ID);
+#endif
+
+ tz_dapc_sec_init();
+
+ set_atf_parameters(atf_arg);
+ set_tee_parameters(tee_arg);
+
+ if(bl32_addr)
+ tee_arg->tee_entry = bl32_addr;
+
+ atf_entry = (jump_atf)BL31;
+ /* jump to tz */
+
+ (*atf_entry)(ATF_BOOT_ARG_ADDR, bl31_reserve);
+
+ return 0;
+}
diff --git a/src/bsp/trustzone/teeloader/mt6771/src/print.c b/src/bsp/trustzone/teeloader/mt6771/src/print.c
new file mode 100644
index 0000000..5105290
--- /dev/null
+++ b/src/bsp/trustzone/teeloader/mt6771/src/print.c
@@ -0,0 +1,173 @@
+/* Copyright Statement:
+ *
+ * This software/firmware and related documentation ("MediaTek Software") are
+ * protected under relevant copyright laws. The information contained herein is
+ * confidential and proprietary to MediaTek Inc. and/or its licensors. Without
+ * the prior written permission of MediaTek inc. and/or its licensors, any
+ * reproduction, modification, use or disclosure of MediaTek Software, and
+ * information contained herein, in whole or in part, shall be strictly
+ * prohibited.
+ *
+ * MediaTek Inc. (C) 2016. All rights reserved.
+ *
+ * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
+ * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
+ * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
+ * ON AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL
+ * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
+ * NONINFRINGEMENT. NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH
+ * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
+ * INCORPORATED IN, OR SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES
+ * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
+ * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
+ * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN MEDIATEK
+ * SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE
+ * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
+ * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S
+ * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE
+ * RELEASED HEREUNDER WILL BE, AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE
+ * MEDIATEK SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
+ * CHARGE PAID BY RECEIVER TO MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
+ *
+ * The following software/firmware and/or related documentation ("MediaTek
+ * Software") have been modified by MediaTek Inc. All revisions are subject to
+ * any receiver's applicable license agreements with MediaTek Inc.
+ */
+
+#include "typedefs.h"
+#include "print.h"
+#include "uart.h"
+#include <stdarg.h>
+
+static void outchar(const char c)
+{
+ uart_putc(c);
+}
+
+static void outstr(const unsigned char *s)
+{
+ while (*s) {
+ if (*s == '\n')
+ outchar('\r');
+ outchar(*s++);
+ }
+}
+
+static void outdec(unsigned long n)
+{
+ if (n >= 10) {
+ outdec(n / 10);
+ n %= 10;
+ }
+ outchar((unsigned char)(n + '0'));
+}
+
+static void outhex(unsigned long n, long depth)
+{
+ if (depth)
+ depth--;
+
+ if ((n & ~0xf) || depth) {
+ outhex(n >> 4, depth);
+ n &= 0xf;
+ }
+
+ if (n < 10) {
+ outchar((unsigned char)(n + '0'));
+ } else {
+ outchar((unsigned char)(n - 10 + 'A'));
+ }
+}
+
+void tl_vprint(char *fmt, va_list vl)
+{
+ unsigned char c;
+ unsigned int reg = 1; /* argument register number (32-bit) */
+
+ while (*fmt) {
+ c = *fmt++;
+ switch (c) {
+ case '%':
+ c = *fmt++;
+ switch (c) {
+ case 'x':
+ outhex(va_arg(vl, unsigned long), 0);
+ break;
+ case 'B':
+ outhex(va_arg(vl, unsigned long), 2);
+ break;
+ case 'H':
+ outhex(va_arg(vl, unsigned long), 4);
+ break;
+ case 'X':
+ outhex(va_arg(vl, unsigned long), 8);
+ break;
+ case 'l':
+ if (*fmt == 'l' && *(fmt + 1) == 'x') {
+ u32 ltmp;
+ u32 htmp;
+
+ ltmp = va_arg(vl, unsigned int);
+ htmp = va_arg(vl, unsigned int);
+
+ outhex(htmp, 8);
+ outhex(ltmp, 8);
+ fmt += 2;
+ }
+ break;
+ case 'd':
+ {
+ long l;
+
+ l = va_arg(vl, long);
+ if (l < 0) {
+ outchar('-');
+ l = -l;
+ }
+ outdec((unsigned long)l);
+ }
+ break;
+ case 'u':
+ outdec(va_arg(vl, unsigned long));
+ break;
+ case 's':
+ outstr((const unsigned char *)
+ va_arg(vl, char *));
+ break;
+ case '%':
+ outchar('%');
+ break;
+ case 'c':
+ c = va_arg(vl, int);
+ outchar(c);
+ break;
+ default:
+ outchar(' ');
+ break;
+ }
+ reg++; /* one argument uses 32-bit register */
+ break;
+ case '\r':
+ if (*fmt == '\n')
+ fmt++;
+ c = '\n';
+ // fall through
+ case '\n':
+ outchar('\r');
+ // fall through
+ default:
+ outchar(c);
+ }
+ }
+}
+
+void tl_printf(char *fmt, ...)
+{
+ va_list args;
+
+ va_start(args, fmt);
+ tl_vprint(fmt, args);
+ va_end(args);
+}
+
diff --git a/src/bsp/trustzone/teeloader/mt6771/src/start.s b/src/bsp/trustzone/teeloader/mt6771/src/start.s
new file mode 100644
index 0000000..aeda20a
--- /dev/null
+++ b/src/bsp/trustzone/teeloader/mt6771/src/start.s
@@ -0,0 +1,42 @@
+/* Copyright Statement:
+ *
+ * This software/firmware and related documentation ("MediaTek Software") are
+ * protected under relevant copyright laws. The information contained herein is
+ * confidential and proprietary to MediaTek Inc. and/or its licensors. Without
+ * the prior written permission of MediaTek inc. and/or its licensors, any
+ * reproduction, modification, use or disclosure of MediaTek Software, and
+ * information contained herein, in whole or in part, shall be strictly
+ * prohibited.
+ *
+ * MediaTek Inc. (C) 2016. All rights reserved.
+ *
+ * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
+ * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
+ * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
+ * ON AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL
+ * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
+ * NONINFRINGEMENT. NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH
+ * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
+ * INCORPORATED IN, OR SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES
+ * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
+ * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
+ * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN MEDIATEK
+ * SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE
+ * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
+ * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S
+ * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE
+ * RELEASED HEREUNDER WILL BE, AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE
+ * MEDIATEK SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
+ * CHARGE PAID BY RECEIVER TO MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
+ *
+ * The following software/firmware and/or related documentation ("MediaTek
+ * Software") have been modified by MediaTek Inc. All revisions are subject to
+ * any receiver's applicable license agreements with MediaTek Inc.
+ */
+
+.section .text.start
+
+.globl _start
+_start:
+ b teeloader_main
\ No newline at end of file
diff --git a/src/bsp/trustzone/teeloader/mt6771/src/uart.c b/src/bsp/trustzone/teeloader/mt6771/src/uart.c
new file mode 100644
index 0000000..b238868
--- /dev/null
+++ b/src/bsp/trustzone/teeloader/mt6771/src/uart.c
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2016 MediaTek Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#include "uart.h"
+
+int uart_putc(char c)
+{
+ while (!(readl(UART_LSR(UART0_BASE)) & UART_LSR_THRE));
+
+ if (c == '\n')
+ writel((unsigned int)'\r', UART_THR(UART0_BASE));
+
+ writel((unsigned int)c, UART_THR(UART0_BASE));
+
+ return 0;
+}
diff --git a/src/bsp/trustzone/teeloader/mt6771/tllink.lds b/src/bsp/trustzone/teeloader/mt6771/tllink.lds
new file mode 100644
index 0000000..dc5a82b
--- /dev/null
+++ b/src/bsp/trustzone/teeloader/mt6771/tllink.lds
@@ -0,0 +1,38 @@
+OUTPUT_ARCH(aarch64)
+
+ENTRY(_start)
+
+SECTIONS {
+
+ . = %BASE_ADDR%;
+ .start ALIGN(4) : {
+ *(.text.start)
+ }
+
+ . = . + 0x01FC;
+ .text ALIGN(4) : {
+ *(.text)
+ *(.text.*)
+ }
+ .rodata ALIGN(4) : {
+ *(.rodata)
+ *(.rodata.*)
+ }
+ .data ALIGN(4) : {
+ *(.data)
+ *(.data.*)
+ }
+
+ . = %BASE_ADDR%-0x100000 ;
+ .bss ALIGN(16) : {
+ _bss_start = .;
+ *(.bss)
+ *(.bss.*)
+ *(COMMON)
+ /* make _bss_end as 4 bytes alignment */
+ . = ALIGN(4);
+ _bss_end = .;
+ }
+
+}
+
diff --git a/src/bsp/trustzone/teeloader/mt6771/zero_padding.sh b/src/bsp/trustzone/teeloader/mt6771/zero_padding.sh
new file mode 100755
index 0000000..e3fb84e
--- /dev/null
+++ b/src/bsp/trustzone/teeloader/mt6771/zero_padding.sh
@@ -0,0 +1,15 @@
+#!/bin/bash
+
+FILE_PATH=$1
+ALIGNMENT=$2
+PADDING_SIZE=0
+
+FILE_SIZE=$(($(wc -c < "${FILE_PATH}")))
+REMAINDER=$((${FILE_SIZE} % ${ALIGNMENT}))
+FILE_DIR=$(dirname "${FILE_PATH}")
+if [ ${REMAINDER} -ne 0 ]; then
+ PADDING_SIZE=$((${ALIGNMENT} - ${REMAINDER}))
+ dd if=/dev/zero of=${FILE_DIR}/padding.txt bs=$PADDING_SIZE count=1
+ cat ${FILE_DIR}/padding.txt>>${FILE_PATH}
+ rm ${FILE_DIR}/padding.txt
+fi