[Feature]add MT2731_MP2_MR2_SVN388 baseline version

Change-Id: Ief04314834b31e27effab435d3ca8ba33b499059
diff --git a/src/bsp/lk/platform/or1ksim/include/platform/or1ksim.h b/src/bsp/lk/platform/or1ksim/include/platform/or1ksim.h
new file mode 100644
index 0000000..ad65b50
--- /dev/null
+++ b/src/bsp/lk/platform/or1ksim/include/platform/or1ksim.h
@@ -0,0 +1,35 @@
+/*
+ * Copyright (c) 2015 Stefan Kristiansson
+ *
+ * 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.
+ */
+#pragma once
+
+#include <reg.h>
+
+/* timer */
+#define TIMER_CLOCK_FREQ    50000000
+
+/* uart */
+#define UART1_BASE          0x90000000
+#define UART1_CLOCK_FREQ    50000000
+
+/* interrupts */
+#define MAX_INT             32
diff --git a/src/bsp/lk/platform/or1ksim/include/platform/pic.h b/src/bsp/lk/platform/or1ksim/include/platform/pic.h
new file mode 100644
index 0000000..668b613
--- /dev/null
+++ b/src/bsp/lk/platform/or1ksim/include/platform/pic.h
@@ -0,0 +1,26 @@
+/*
+ * Copyright (c) 2015 Stefan Kristiansson
+ *
+ * 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.
+ */
+#pragma once
+
+#include <platform/or1ksim.h>
+
diff --git a/src/bsp/lk/platform/or1ksim/or1ksim.cfg b/src/bsp/lk/platform/or1ksim/or1ksim.cfg
new file mode 100755
index 0000000..98ed3ca
--- /dev/null
+++ b/src/bsp/lk/platform/or1ksim/or1ksim.cfg
@@ -0,0 +1,85 @@
+section sim
+  clkcycle = 20ns
+end
+
+section VAPI
+  server_port = 50000
+  log_enabled = 0
+  vapi_log_file = "vapi.log"
+end
+
+section cpu
+  ver = 0x0
+  cfgr = 0x20
+  rev = 0x0001
+end
+
+section memory
+  name        = "RAM"
+  type        = unknown
+  baseaddr    = 0x00000000
+  size        = 0x02000000
+  delayr      = 1
+  delayw      = 1
+end
+
+section dmmu
+  enabled   = 1
+  nsets     = 64
+  nways     = 1
+  pagesize  = 8192
+  hitdelay  = 0
+  missdelay = 0
+end
+
+section immu
+  enabled   = 1
+  nsets     = 64
+  nways     = 1
+  pagesize  = 8192
+  hitdelay  = 0
+  missdelay = 0
+end
+
+section dc
+  enabled         = 1
+  nsets           = 256
+  nways           = 1
+  blocksize       = 16
+  load_hitdelay   = 0
+  load_missdelay  = 0
+  store_hitdelay  = 0
+  store_missdelay = 0
+end
+
+section ic
+  enabled   = 1
+  nsets     = 256
+  nways     = 1
+  blocksize = 16
+  hitdelay  = 0
+  missdelay = 0
+end
+
+section pic
+  enabled = 1
+  edge_trigger = 0
+end
+
+section debug
+  enabled = 0
+end
+
+section uart
+  enabled  = 1
+  baseaddr = 0x90000000
+  irq      = 2
+  16550    = 1
+end
+
+section ethernet
+  enabled  = 1
+  baseaddr = 0x92000000
+  irq      = 4
+  rtx_type = 0
+end
diff --git a/src/bsp/lk/platform/or1ksim/platform.c b/src/bsp/lk/platform/or1ksim/platform.c
new file mode 100644
index 0000000..b4a1626
--- /dev/null
+++ b/src/bsp/lk/platform/or1ksim/platform.c
@@ -0,0 +1,99 @@
+/*
+ * Copyright (c) 2015 Stefan Kristiansson
+ *
+ * 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 <reg.h>
+#include <kernel/thread.h>
+#include <kernel/vm.h>
+#include <dev/uart.h>
+#include <dev/timer/or1k_ticktimer.h>
+#include <platform.h>
+#include <platform/interrupts.h>
+#include <platform/debug.h>
+#include <platform/timer.h>
+#include <platform/or1ksim.h>
+#include <sys/types.h>
+#include <target/debugconfig.h>
+
+struct mmu_initial_mapping mmu_initial_mappings[] = {
+    /* 32 MB of RAM space */
+    {
+        .phys = 0x0,
+        .virt = KERNEL_BASE,
+        .size = 32*1024*1024,
+        .flags = 0,
+        .name = "memory"
+    },
+
+    /* peripherals */
+    {
+        .phys = 0x90000000,
+        .virt = 0x90000000,
+        .size = 0x04000000,
+        .flags = MMU_INITIAL_MAPPING_FLAG_DEVICE,
+        .name = "peripherals"
+    },
+
+    /* identity map to let the boot code run */
+    {
+        .phys = 0,
+        .virt = 0,
+        .size = 1024*1024*1024,
+        .flags = MMU_INITIAL_MAPPING_TEMPORARY
+    },
+
+    /* null entry to terminate the list */
+    { 0 }
+};
+
+static pmm_arena_t ram_arena = {
+    .name = "ram",
+    .base = 0,
+    .size = MEMSIZE,
+    .flags = PMM_ARENA_FLAG_KMAP
+};
+
+void platform_dputc(char c)
+{
+    if (c == '\n')
+        uart_putc(DEBUG_UART, '\r');
+    uart_putc(DEBUG_UART, c);
+}
+
+int platform_dgetc(char *c, bool wait)
+{
+    int _c;
+
+    if ((_c = uart_getc(DEBUG_UART, false)) < 0)
+        return -1;
+
+    *c = _c;
+    return 0;
+}
+
+void platform_early_init(void)
+{
+    uart_init_early();
+    or1k_ticktimer_init(TIMER_CLOCK_FREQ);
+#if WITH_KERNEL_VM
+    pmm_add_arena(&ram_arena);
+#endif
+}
diff --git a/src/bsp/lk/platform/or1ksim/rules.mk b/src/bsp/lk/platform/or1ksim/rules.mk
new file mode 100644
index 0000000..f878244
--- /dev/null
+++ b/src/bsp/lk/platform/or1ksim/rules.mk
@@ -0,0 +1,24 @@
+LOCAL_DIR := $(GET_LOCAL_DIR)
+
+MODULE := $(LOCAL_DIR)
+
+ARCH := or1k
+
+MODULE_DEPS += \
+	lib/cbuf \
+	dev/interrupt/or1k_pic \
+	dev/timer/or1k_ticktimer
+
+MODULE_SRCS += \
+	$(LOCAL_DIR)/platform.c \
+	$(LOCAL_DIR)/uart.c
+
+MEMBASE ?= 0x00000000
+MEMSIZE ?= 0x02000000
+
+# we have an mmu
+WITH_KERNEL_VM=1
+
+KERNEL_BASE = 0xc0000000
+
+include make/module.mk
diff --git a/src/bsp/lk/platform/or1ksim/uart.c b/src/bsp/lk/platform/or1ksim/uart.c
new file mode 100644
index 0000000..d7ebb3e
--- /dev/null
+++ b/src/bsp/lk/platform/or1ksim/uart.c
@@ -0,0 +1,175 @@
+/*
+ * Copyright (c) 2008 Travis Geiselbrecht
+ *
+ * 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 <debug.h>
+#include <reg.h>
+#include <dev/uart.h>
+#include <target/debugconfig.h>
+#include <platform/or1ksim.h>
+
+struct uart_stat {
+    addr_t base;
+    uint32_t clk_freq;
+    uint shift;
+};
+
+static struct uart_stat uart[1] = {
+    { UART1_BASE, UART1_CLOCK_FREQ, 0 },
+};
+
+static inline void write_uart_reg(int port, uint reg, unsigned char data)
+{
+    *(volatile unsigned char *)(uart[port].base + (reg << uart[port].shift)) = data;
+}
+
+static inline unsigned char read_uart_reg(int port, uint reg)
+{
+    return *(volatile unsigned char *)(uart[port].base + (reg << uart[port].shift));
+}
+
+#define UART_RHR    0
+#define UART_THR    0
+#define UART_DLL    0
+#define UART_IER    1
+#define UART_DLH    1
+#define UART_IIR    2
+#define UART_FCR    2
+#define UART_EFR    2
+#define UART_LCR    3
+#define UART_MCR    4
+#define UART_LSR    5
+#define UART_MSR    6
+#define UART_TCR    6
+#define UART_SPR    7
+#define UART_TLR    7
+#define UART_MDR1   8
+#define UART_MDR2   9
+#define UART_SFLSR  10
+#define UART_RESUME 11
+#define UART_TXFLL  10
+#define UART_TXFLH  11
+#define UART_SFREGL 12
+#define UART_SFREGH 13
+#define UART_RXFLL  12
+#define UART_RXFLH  13
+#define UART_BLR    14
+#define UART_UASR   14
+#define UART_ACREG  15
+#define UART_SCR    16
+#define UART_SSR    17
+#define UART_EBLR   18
+#define UART_MVR    19
+#define UART_SYSC   20
+
+#define LCR_8N1     0x03
+
+#define FCR_FIFO_EN 0x01        /* Fifo enable */
+#define FCR_RXSR    0x02        /* Receiver soft reset */
+#define FCR_TXSR    0x04        /* Transmitter soft reset */
+
+#define MCR_DTR     0x01
+#define MCR_RTS     0x02
+#define MCR_DMA_EN  0x04
+#define MCR_TX_DFR  0x08
+
+#define LCR_WLS_MSK 0x03        /* character length select mask */
+#define LCR_WLS_5   0x00        /* 5 bit character length */
+#define LCR_WLS_6   0x01        /* 6 bit character length */
+#define LCR_WLS_7   0x02        /* 7 bit character length */
+#define LCR_WLS_8   0x03        /* 8 bit character length */
+#define LCR_STB     0x04        /* Number of stop Bits, off = 1, on = 1.5 or 2) */
+#define LCR_PEN     0x08        /* Parity eneble */
+#define LCR_EPS     0x10        /* Even Parity Select */
+#define LCR_STKP    0x20        /* Stick Parity */
+#define LCR_SBRK    0x40        /* Set Break */
+#define LCR_BKSE    0x80        /* Bank select enable */
+
+#define LSR_DR      0x01        /* Data ready */
+#define LSR_OE      0x02        /* Overrun */
+#define LSR_PE      0x04        /* Parity error */
+#define LSR_FE      0x08        /* Framing error */
+#define LSR_BI      0x10        /* Break */
+#define LSR_THRE    0x20        /* Xmit holding register empty */
+#define LSR_TEMT    0x40        /* Xmitter empty */
+#define LSR_ERR     0x80        /* Error */
+
+#define LCRVAL LCR_8N1                  /* 8 data, 1 stop, no parity */
+#define MCRVAL (MCR_DTR | MCR_RTS)      /* RTS/DTR */
+#define FCRVAL (FCR_FIFO_EN | FCR_RXSR | FCR_TXSR)  /* Clear & enable FIFOs */
+
+void uart_init_port(int port, uint baud)
+{
+    /* clear the tx & rx fifo and disable */
+    uint16_t baud_divisor = (uart[port].clk_freq / 16 / baud);
+
+    write_uart_reg(port, UART_IER, 0);
+    write_uart_reg(port, UART_LCR, LCR_BKSE | LCRVAL); // config mode A
+    write_uart_reg(port, UART_DLL, baud_divisor & 0xff);
+    write_uart_reg(port, UART_DLH, (baud_divisor >> 8) & 0xff);
+    write_uart_reg(port, UART_LCR, LCRVAL); // operational mode
+    write_uart_reg(port, UART_MCR, MCRVAL);
+    write_uart_reg(port, UART_FCR, FCRVAL);
+}
+
+void uart_init_early(void)
+{
+    uart_init_port(DEBUG_UART, 115200);
+}
+
+void uart_init(void)
+{
+}
+
+int uart_putc(int port, char c )
+{
+    while (!(read_uart_reg(port, UART_LSR) & (1<<6))) // wait for the last char to get out
+        ;
+    write_uart_reg(port, UART_THR, c);
+    return 0;
+}
+
+int uart_getc(int port, bool wait)  /* returns -1 if no data available */
+{
+    if (wait) {
+        while (!(read_uart_reg(port, UART_LSR) & (1<<0))) // wait for data to show up in the rx fifo
+            ;
+    } else {
+        if (!(read_uart_reg(port, UART_LSR) & (1<<0)))
+            return -1;
+    }
+    return read_uart_reg(port, UART_RHR);
+}
+
+void uart_flush_tx(int port)
+{
+    while (!(read_uart_reg(port, UART_LSR) & (1<<6))) // wait for the last char to get out
+        ;
+}
+
+void uart_flush_rx(int port)
+{
+    // empty the rx fifo
+    while (read_uart_reg(port, UART_LSR) & (1<<0)) {
+        volatile char c = read_uart_reg(port, UART_RHR);
+        (void)c;
+    }
+}