[Feature]add MT2731_MP2_MR2_SVN388 baseline version

Change-Id: Ief04314834b31e27effab435d3ca8ba33b499059
diff --git a/src/bsp/lk/lib/wrapper/include/mtk_wrapper.h b/src/bsp/lk/lib/wrapper/include/mtk_wrapper.h
new file mode 100644
index 0000000..0cff17d
--- /dev/null
+++ b/src/bsp/lk/lib/wrapper/include/mtk_wrapper.h
@@ -0,0 +1,108 @@
+/*
+ * Copyright (c) 2018 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.
+ */
+
+/**
+ * @file mtk_wrapper.h
+ * @brief Header file of LK 2.0 wrapper for post-init
+ *
+ * LK 2.0 wrapper includes interface functions, which provides software
+ * environment functions. Several interfaces, virtual/physical address
+ * translation, muxtex/semaphore and debug are provided to execute the
+ * corresponding system calls.
+ */
+
+#pragma once
+
+#include <stddef.h>
+#include <stdint.h>
+#include <kernel/mutex.h>
+#include <kernel/semaphore.h>
+#include <kernel/vm.h>
+#include <debug.h>
+#include <platform.h>
+
+#ifndef __REG_H
+/* low level macros for accessing memory mapped hardware registers */
+#define REG64(addr) ((volatile uint64_t *)(uintptr_t)(addr))
+#define REG32(addr) ((volatile uint32_t *)(uintptr_t)(addr))
+#define REG16(addr) ((volatile uint16_t *)(uintptr_t)(addr))
+#define REG8(addr) ((volatile uint8_t *)(uintptr_t)(addr))
+
+#define RMWREG64(addr, startbit, width, val) *REG64(addr) = (*REG64(addr) & ~(((1<<(width)) - 1) << (startbit))) | ((val) << (startbit))
+#define RMWREG32(addr, startbit, width, val) *REG32(addr) = (*REG32(addr) & ~(((1<<(width)) - 1) << (startbit))) | ((val) << (startbit))
+#define RMWREG16(addr, startbit, width, val) *REG16(addr) = (*REG16(addr) & ~(((1<<(width)) - 1) << (startbit))) | ((val) << (startbit))
+#define RMWREG8(addr, startbit, width, val) *REG8(addr) = (*REG8(addr) & ~(((1<<(width)) - 1) << (startbit))) | ((val) << (startbit))
+
+#define writel(v, a) (*REG32(a) = (v))
+#define readl(a) (*REG32(a))
+#define writeb(v, a) (*REG8(a) = (v))
+#define readb(a) (*REG8(a))
+#endif
+
+#ifndef __DEBUG_H
+#if !defined(LK_DEBUGLEVEL)
+#define LK_DEBUGLEVEL 0
+#endif
+
+/* debug levels */
+#define CRITICAL 0
+#define ALWAYS 0
+#define INFO 1
+#define SPEW 2
+
+#define dprintf(level, x...) do { if ((level) <= LK_DEBUGLEVEL) { _dprintf(x); } } while (0)
+#endif
+
+enum wrap_handler_return {
+    WRAP_INT_NO_RESCHEDULE = 0,
+    WRAP_INT_RESCHEDULE,
+};
+
+typedef mutex_t wrap_mutex;
+typedef semaphore_t wrap_sem;
+typedef enum wrap_handler_return (*wrap_int_handler)(void *arg);
+
+//mutex interface functions
+void plat_wrap_mutex_init(wrap_mutex *m);
+void plat_wrap_mutex_destroy(wrap_mutex *m);
+status_t plat_wrap_mutex_acquire_timeout(wrap_mutex *m, uint32_t timeout);
+status_t plat_wrap_mutex_acquire(wrap_mutex *m);
+status_t plat_wrap_mutex_release(wrap_mutex *m);
+//semaphore interface functions
+void plat_wrap_sem_init(wrap_sem *sem, unsigned int value);
+void plat_wrap_sem_destroy(wrap_sem *sem);
+int plat_wrap_sem_post(wrap_sem *sem);
+status_t plat_wrap_sem_wait(wrap_sem *sem);
+status_t plat_wrap_sem_trywait(wrap_sem *sem);
+status_t plat_wrap_sem_timedwait(wrap_sem *sem, uint32_t timeout);
+//virtual/physical address translation function
+void *plat_wrap_paddr_to_kvaddr(unsigned long pa);
+unsigned long plat_wrap_kvaddr_to_paddr(void *ptr);
+//system delay function
+void plat_wrap_delay(uint32_t usecs);
+//interrupt function
+void plat_wrap_register_int_handler(unsigned int vector, wrap_int_handler handler, void *arg);
+int plat_wrap_unmask_interrupt(unsigned int vector);
+int plat_wrap_mask_interrupt(unsigned int vector);
+void plat_wrap_irq_set_polarity(unsigned int vector, unsigned int polarity);
+void plat_wrap_irq_set_sensitive(unsigned int vector, unsigned int sens);
diff --git a/src/bsp/lk/lib/wrapper/mtk_wrapper.c b/src/bsp/lk/lib/wrapper/mtk_wrapper.c
new file mode 100644
index 0000000..f29b821
--- /dev/null
+++ b/src/bsp/lk/lib/wrapper/mtk_wrapper.c
@@ -0,0 +1,168 @@
+/*
+ * Copyright (c) 2018 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 <mtk_wrapper.h>
+#include <trace.h>
+#include <platform/interrupts.h>
+#include <platform/mt_irq.h>
+
+#define LOCAL_TRACE 0
+
+/* mutex*/
+void plat_wrap_mutex_init(wrap_mutex *m)
+{
+    LTRACEF("init mutex\n");
+
+    mutex_init(m);
+}
+
+void plat_wrap_mutex_destroy(wrap_mutex *m)
+{
+    LTRACEF("destroy mutex\n");
+
+    mutex_destroy(m);
+}
+
+status_t plat_wrap_mutex_acquire_timeout(wrap_mutex *m, uint32_t timeout)
+{
+    LTRACEF("mutex acquire timeout\n");
+
+    return mutex_acquire_timeout(m, timeout);
+}
+
+status_t plat_wrap_mutex_acquire(wrap_mutex *m)
+{
+    LTRACEF("mutex acquire\n");
+
+    return mutex_acquire(m);
+}
+
+status_t plat_wrap_mutex_release(wrap_mutex *m)
+{
+    LTRACEF("release mutex\n");
+
+    return mutex_release(m);
+}
+
+/* semaphore*/
+void plat_wrap_sem_init(wrap_sem *sem, unsigned int value)
+{
+    LTRACEF("init semaphore\n");
+
+    sem_init(sem, value);
+}
+
+void plat_wrap_sem_destroy(wrap_sem *sem)
+{
+    LTRACEF("destroy semaphore\n");
+
+    sem_destroy(sem);
+}
+
+int plat_wrap_sem_post(wrap_sem *sem)
+{
+    LTRACEF("semaphore post\n");
+
+    return sem_post(sem, true);
+}
+
+status_t plat_wrap_sem_wait(wrap_sem *sem)
+{
+    LTRACEF("semaphore wait\n");
+
+    return sem_wait(sem);
+}
+
+status_t plat_wrap_sem_trywait(wrap_sem *sem)
+{
+    LTRACEF("semaphore trywait\n");
+
+    return sem_trywait(sem);
+}
+
+status_t plat_wrap_sem_timedwait(wrap_sem *sem, uint32_t timeout)
+{
+    LTRACEF("semaphore timedwait\n");
+
+    return sem_timedwait(sem, timeout);
+}
+
+/* vm*/
+void *plat_wrap_paddr_to_kvaddr(unsigned long pa)
+{
+    LTRACEF("physical address translate to virtual address\n");
+
+    return paddr_to_kvaddr(pa);
+}
+
+unsigned long plat_wrap_kvaddr_to_paddr(void *ptr)
+{
+    LTRACEF("physical address translate to virtual address\n");
+
+    return kvaddr_to_paddr(ptr);
+}
+
+/* timer*/
+//delay usecs
+void plat_wrap_delay(uint32_t usecs)
+{
+    LTRACEF("system delay\n");
+
+    spin(usecs);
+}
+
+/* interrupt*/
+void plat_wrap_register_int_handler(unsigned int vector, wrap_int_handler handler, void *arg)
+{
+    LTRACEF("register interrupt\n");
+
+    register_int_handler(vector, (int_handler)handler, arg);
+}
+
+int plat_wrap_unmask_interrupt(unsigned int vector)
+{
+    LTRACEF("enable interrupt\n");
+
+    return unmask_interrupt(vector);
+}
+
+int plat_wrap_mask_interrupt(unsigned int vector)
+{
+    LTRACEF("disable interrupt\n");
+
+    return mask_interrupt(vector);
+}
+
+void plat_wrap_irq_set_polarity(unsigned int vector, unsigned int polarity)
+{
+    LTRACEF("set interrupt polarity\n");
+
+    mt_irq_set_polarity(vector, polarity);
+}
+
+void plat_wrap_irq_set_sensitive(unsigned int vector, unsigned int sens)
+{
+    LTRACEF("set interrupt sensitive\n");
+
+    mt_irq_set_sens(vector, sens);
+}
diff --git a/src/bsp/lk/lib/wrapper/rules.mk b/src/bsp/lk/lib/wrapper/rules.mk
new file mode 100644
index 0000000..513ab67
--- /dev/null
+++ b/src/bsp/lk/lib/wrapper/rules.mk
@@ -0,0 +1,17 @@
+LOCAL_DIR := $(GET_LOCAL_DIR)
+MODULE := $(LOCAL_DIR)
+
+MODULE_DEPS := \
+        lib/libc \
+        lib/debug \
+
+GLOBAL_INCLUDES += \
+    $(LKROOT)/../lk_ext_mod/platform/mt2712/include \
+
+MODULES += \
+    $(LKROOT)/../lk_ext_mod/platform/mt2712/drivers \
+
+MODULE_SRCS += \
+    $(LOCAL_DIR)/mtk_wrapper.c \
+
+include make/module.mk
diff --git a/src/bsp/lk/lib/wrapper/test/rules.mk b/src/bsp/lk/lib/wrapper/test/rules.mk
new file mode 100644
index 0000000..f60e31f
--- /dev/null
+++ b/src/bsp/lk/lib/wrapper/test/rules.mk
@@ -0,0 +1,12 @@
+LOCAL_DIR := $(GET_LOCAL_DIR)
+
+MODULE := $(LOCAL_DIR)
+
+MODULE_DEPS := \
+    lib/wrapper \
+    lib/unittest \
+
+MODULE_SRCS := \
+    $(LOCAL_DIR)/wrapper_test.c
+
+include make/module.mk
diff --git a/src/bsp/lk/lib/wrapper/test/wrapper_test.c b/src/bsp/lk/lib/wrapper/test/wrapper_test.c
new file mode 100644
index 0000000..33ce1d2
--- /dev/null
+++ b/src/bsp/lk/lib/wrapper/test/wrapper_test.c
@@ -0,0 +1,171 @@
+/*
+ * Copyright (c) 2017 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 <err.h>
+#include <mtk_wrapper.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unittest.h>
+#include <platform.h>
+
+/**
+ * lib/unittest defines several macros for unittest,
+ * EXPECT_EQ, EXPECT_NEQ, EXPECT_LE, EXPECT_LT, EXPECT_GE, EXPECT_GT,
+ * EXPECT_TRUE, EXPECT_FALSE, EXPECT_BYTES_EQ, EXPECT_BYTES_NE, EXPECT_EQ_LL,
+ * ASSERT_NOT_NULL.
+ * For detail, please refer to lib/unittest/include/unittest.h
+ */
+//irq function unittest
+bool timerEnable = false;
+#define ARM_GENERIC_TIMER_PHYSICAL_INT 30
+
+static enum wrap_handler_return wrap_plat_platform_timer_handle(void *arg)
+{
+    timerEnable = true;
+    plat_wrap_mask_interrupt(ARM_GENERIC_TIMER_PHYSICAL_INT);
+    return WRAP_INT_NO_RESCHEDULE;
+}
+
+static bool test_plat_wrap_irq_function(void)
+{
+#define WRAP_LEVEL_SENSITIVE 1
+#define WRAP_POLARITY_LOW   0
+    int count = 5;
+
+    plat_wrap_irq_set_polarity(ARM_GENERIC_TIMER_PHYSICAL_INT, WRAP_LEVEL_SENSITIVE);
+    plat_wrap_irq_set_sensitive(ARM_GENERIC_TIMER_PHYSICAL_INT, WRAP_POLARITY_LOW);
+    plat_wrap_register_int_handler(ARM_GENERIC_TIMER_PHYSICAL_INT, &wrap_plat_platform_timer_handle, NULL);
+    plat_wrap_unmask_interrupt(ARM_GENERIC_TIMER_PHYSICAL_INT);
+    while (1) {
+        if (count > 0) {
+            plat_wrap_delay(1000000);
+            if (timerEnable) {
+                break;
+            }
+            count --;
+        } else
+            return false;
+    }
+
+    return true;
+}
+
+//delay function unittest
+static bool test_plat_wrap_delay_function(void)
+{
+    BEGIN_TEST;
+
+    unsigned long long startTime = 0;
+    unsigned long long endTime = 0;
+    int delayTime = 1000000; //one second
+    int delayThreshold = 500;
+
+    startTime = current_time_hires();
+    plat_wrap_delay(1000000);
+    endTime = current_time_hires();
+    EXPECT_LE((delayTime - delayThreshold), endTime - startTime, "delay time is less than expected value");
+    EXPECT_GE((delayTime + delayThreshold), endTime - startTime, "delay time is greater than expected value");
+
+    END_TEST;
+}
+
+//address translate function unittest
+static bool test_plat_wrap_addr_trans_function(void)
+{
+#define EMI_PHY_BASE (0x10203000)
+
+    BEGIN_TEST;
+
+    unsigned long paddr;
+    void *vaddr = plat_wrap_paddr_to_kvaddr(EMI_PHY_BASE);
+    paddr = plat_wrap_kvaddr_to_paddr(vaddr);
+    EXPECT_EQ(EMI_PHY_BASE, paddr, "fail to virtual/phyical address translate");
+
+    END_TEST;
+}
+
+//mutex function unittest
+wrap_mutex m;
+
+static bool test_plat_wrap_mutex_function(void)
+{
+    BEGIN_TEST;
+
+    plat_wrap_mutex_init(&m);
+    EXPECT_EQ(NO_ERROR, plat_wrap_mutex_acquire(&m), "fail to mutex acquire");
+    EXPECT_EQ(NO_ERROR, plat_wrap_mutex_release(&m), "fail to mutex release");
+    plat_wrap_mutex_destroy(&m);
+
+    END_TEST;
+}
+
+//semphore function unittest
+wrap_sem sem;
+static bool test_plat_wrap_sem_function(void)
+{
+    BEGIN_TEST;
+
+    unsigned int value = 1;
+    plat_wrap_sem_init(&sem, value);
+    EXPECT_EQ(NO_ERROR, plat_wrap_sem_wait(&sem), "fail to semaphore wait function");
+    EXPECT_EQ(NO_ERROR, plat_wrap_sem_post(&sem), "fail to semaphore post function");
+    plat_wrap_sem_destroy(&sem);
+
+    END_TEST;
+}
+
+static bool test_plat_wrap_sem_trywait_function(void)
+{
+    BEGIN_TEST;
+
+    unsigned int value = 1;
+    plat_wrap_sem_init(&sem, value);
+    EXPECT_EQ(NO_ERROR, plat_wrap_sem_trywait(&sem), "fail to semaphore trywait function");
+    EXPECT_EQ(NO_ERROR, plat_wrap_sem_post(&sem), "fail to semaphore post function");
+    plat_wrap_sem_destroy(&sem);
+
+    END_TEST;
+}
+
+static bool test_plat_wrap_sem_timedwait_function(void)
+{
+    BEGIN_TEST;
+
+    unsigned int value = 1;
+    uint32_t timeout = 1;
+    plat_wrap_sem_init(&sem, value);
+    EXPECT_EQ(NO_ERROR, plat_wrap_sem_timedwait(&sem, timeout), "fail to semaphore timedwait function");
+    EXPECT_EQ(NO_ERROR, plat_wrap_sem_post(&sem), "fail to semaphore post function");
+    plat_wrap_sem_destroy(&sem);
+
+    END_TEST;
+}
+
+BEGIN_TEST_CASE(wrapper_tests);
+RUN_TEST(test_plat_wrap_irq_function);
+RUN_TEST(test_plat_wrap_delay_function);
+RUN_TEST(test_plat_wrap_addr_trans_function);
+RUN_TEST(test_plat_wrap_mutex_function);
+RUN_TEST(test_plat_wrap_sem_function);
+RUN_TEST(test_plat_wrap_sem_trywait_function);
+RUN_TEST(test_plat_wrap_sem_timedwait_function);
+END_TEST_CASE(wrapper_tests);