[Feature]add MT2731_MP2_MR2_SVN388 baseline version

Change-Id: Ief04314834b31e27effab435d3ca8ba33b499059
diff --git a/src/bsp/lk/platform/microblaze/intc.c b/src/bsp/lk/platform/microblaze/intc.c
new file mode 100644
index 0000000..f70b9a7
--- /dev/null
+++ b/src/bsp/lk/platform/microblaze/intc.c
@@ -0,0 +1,115 @@
+/*
+ * Copyright (c) 2015 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 <reg.h>
+#include <err.h>
+#include <trace.h>
+#include <lk/init.h>
+#include <kernel/thread.h>
+#include <platform.h>
+#include <platform/interrupts.h>
+#include <platform/debug.h>
+#include <sys/types.h>
+#include <target/microblaze-config.h>
+
+#define LOCAL_TRACE 0
+
+#define R_ISR       0
+#define R_IPR       1
+#define R_IER       2
+#define R_IAR       3
+#define R_SIE       4
+#define R_CIE       5
+#define R_IVR       6
+#define R_MER       7
+#define R_MAX       8
+
+#define INTC_REG(reg) (*REG32(INTC_BASEADDR + (reg) * 4))
+
+static spin_lock_t lock;
+
+struct int_handler_struct {
+    int_handler handler;
+    void *arg;
+};
+
+static struct int_handler_struct int_handler_table[MAX_INT];
+
+void register_int_handler(unsigned int vector, int_handler handler, void *arg)
+{
+    LTRACEF("vector %u, handler %p, arg %p\n", vector, handler, arg);
+
+    if (vector >= MAX_INT)
+        return;
+
+    spin_lock_saved_state_t state;
+    spin_lock_irqsave(&lock, state);
+
+    int_handler_table[vector].handler = handler;
+    int_handler_table[vector].arg = arg;
+
+    spin_unlock_irqrestore(&lock, state);
+}
+
+status_t mask_interrupt(unsigned int vector)
+{
+    LTRACEF("vector %u\n", vector);
+
+    INTC_REG(R_CIE) = 1 << vector;
+
+    return NO_ERROR;
+}
+
+status_t unmask_interrupt(unsigned int vector)
+{
+    LTRACEF("vector %u\n", vector);
+
+    INTC_REG(R_SIE) = 1 << vector;
+
+    return NO_ERROR;
+}
+
+enum handler_return platform_irq_handler(void)
+{
+    enum handler_return ret = INT_NO_RESCHEDULE;
+
+    uint irq = INTC_REG(R_IVR);
+    LTRACEF("irq %u, IPR 0x%x, ISR 0x%x\n", irq, INTC_REG(R_IPR), INTC_REG(R_ISR));
+
+    if (irq < MAX_INT && int_handler_table[irq].handler)
+        ret = int_handler_table[irq].handler(int_handler_table[irq].arg);
+
+    INTC_REG(R_IAR) = 1 << irq;
+
+    return ret;
+}
+
+static void intc_init(uint level)
+{
+    LTRACE;
+
+    INTC_REG(R_CIE) = 0xffffffff;
+    INTC_REG(R_MER) = 0x3;
+}
+
+LK_INIT_HOOK(intc, intc_init, LK_INIT_LEVEL_PLATFORM_EARLY);
+
diff --git a/src/bsp/lk/platform/microblaze/platform.c b/src/bsp/lk/platform/microblaze/platform.c
new file mode 100644
index 0000000..a4adb8f
--- /dev/null
+++ b/src/bsp/lk/platform/microblaze/platform.c
@@ -0,0 +1,58 @@
+/*
+ * Copyright (c) 2015 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 <reg.h>
+#include <kernel/thread.h>
+#include <platform.h>
+#include <platform/interrupts.h>
+#include <platform/debug.h>
+#include <platform/timer.h>
+#include <sys/types.h>
+#include <target/microblaze-config.h>
+
+void uartlite_putc(char c);
+int uartlite_getc(bool wait);
+
+void platform_dputc(char c)
+{
+    if (c == '\n')
+        uartlite_putc('\r');
+    uartlite_putc(c);
+}
+
+int platform_dgetc(char *c, bool wait)
+{
+    for (;;) {
+        int ret = uartlite_getc(wait);
+        if (ret >= 0) {
+            *c = ret;
+            return 0;
+        }
+
+        if (!wait)
+            return -1;
+
+        thread_yield();
+    }
+}
+
+
diff --git a/src/bsp/lk/platform/microblaze/rules.mk b/src/bsp/lk/platform/microblaze/rules.mk
new file mode 100644
index 0000000..3d014e9
--- /dev/null
+++ b/src/bsp/lk/platform/microblaze/rules.mk
@@ -0,0 +1,21 @@
+LOCAL_DIR := $(GET_LOCAL_DIR)
+
+MODULE := $(LOCAL_DIR)
+
+ARCH := microblaze
+
+MODULE_DEPS += \
+    lib/cbuf
+
+MODULE_SRCS += \
+	$(LOCAL_DIR)/intc.c \
+	$(LOCAL_DIR)/platform.c \
+	$(LOCAL_DIR)/timer.c \
+	$(LOCAL_DIR)/uartlite.c
+
+MEMBASE ?= 0x0
+MEMSIZE ?= 0x20000	# 128KB
+
+MODULE_DEPS += \
+
+include make/module.mk
diff --git a/src/bsp/lk/platform/microblaze/timer.c b/src/bsp/lk/platform/microblaze/timer.c
new file mode 100644
index 0000000..c1ee085
--- /dev/null
+++ b/src/bsp/lk/platform/microblaze/timer.c
@@ -0,0 +1,112 @@
+/*
+ * Copyright (c) 2015 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 <reg.h>
+#include <trace.h>
+#include <err.h>
+#include <lk/init.h>
+#include <kernel/thread.h>
+#include <platform.h>
+#include <platform/timer.h>
+#include <platform/interrupts.h>
+#include <platform/debug.h>
+#include <sys/types.h>
+#include <target/microblaze-config.h>
+
+#define LOCAL_TRACE 0
+
+#define R_TCSR     0
+#define R_TLR      1
+#define R_TCR      2
+#define R_MAX      4
+
+#define TCSR_MDT        (1<<0)
+#define TCSR_UDT        (1<<1)
+#define TCSR_GENT       (1<<2)
+#define TCSR_CAPT       (1<<3)
+#define TCSR_ARHT       (1<<4)
+#define TCSR_LOAD       (1<<5)
+#define TCSR_ENIT       (1<<6)
+#define TCSR_ENT        (1<<7)
+#define TCSR_TINT       (1<<8)
+#define TCSR_PWMA       (1<<9)
+#define TCSR_ENALL      (1<<10)
+
+#define TIMER_REG(reg) (*REG32(TIMER_BASEADDR + (reg) * 4))
+
+static platform_timer_callback timer_cb;
+static void *timer_arg;
+
+static uint32_t ticks = 0;
+
+status_t platform_set_periodic_timer(platform_timer_callback callback, void *arg, lk_time_t interval)
+{
+    LTRACEF("cb %p, arg %p, interval %u\n", callback, arg, interval);
+
+    uint32_t count = ((uint64_t)TIMER_RATE * interval / 1000);
+
+    LTRACEF("count 0x%x\n", count);
+
+    timer_cb = callback;
+    timer_arg = arg;
+
+    TIMER_REG(R_TCSR) = 0;
+    TIMER_REG(R_TLR) = count;
+    TIMER_REG(R_TCSR) = TCSR_ENIT | TCSR_UDT | TCSR_ARHT | TCSR_ENT;
+
+    return NO_ERROR;
+}
+
+lk_bigtime_t current_time_hires(void)
+{
+    return (lk_bigtime_t)ticks * 10000;
+}
+
+lk_time_t current_time(void)
+{
+    return (lk_time_t)ticks * 10;
+}
+
+enum handler_return timer_irq(void *arg)
+{
+    LTRACE;
+
+    TIMER_REG(R_TCSR) |= TCSR_TINT;
+
+    ticks += 1;
+
+    enum handler_return ret = timer_cb(timer_arg, ticks * 10);
+
+    return ret;
+}
+
+static void timer_init(uint level)
+{
+    LTRACE;
+
+    register_int_handler(TIMER_IRQ, timer_irq, NULL);
+    unmask_interrupt(TIMER_IRQ);
+}
+
+LK_INIT_HOOK(timer, timer_init, LK_INIT_LEVEL_PLATFORM_EARLY + 1);
+
+
diff --git a/src/bsp/lk/platform/microblaze/uartlite.c b/src/bsp/lk/platform/microblaze/uartlite.c
new file mode 100644
index 0000000..e63bb60
--- /dev/null
+++ b/src/bsp/lk/platform/microblaze/uartlite.c
@@ -0,0 +1,113 @@
+/*
+ * Copyright (c) 2015 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 <reg.h>
+#include <trace.h>
+#include <lib/cbuf.h>
+#include <lk/init.h>
+#include <kernel/thread.h>
+#include <platform.h>
+#include <platform/interrupts.h>
+#include <sys/types.h>
+#include <target/microblaze-config.h>
+
+#define LOCAL_TRACE 0
+
+#define R_RX            0
+#define R_TX            1
+#define R_STATUS        2
+#define R_CTRL          3
+#define R_MAX           4
+
+#define STATUS_RXVALID    0x01
+#define STATUS_RXFULL     0x02
+#define STATUS_TXEMPTY    0x04
+#define STATUS_TXFULL     0x08
+#define STATUS_IE         0x10
+#define STATUS_OVERRUN    0x20
+#define STATUS_FRAME      0x40
+#define STATUS_PARITY     0x80
+
+#define CONTROL_RST_TX    0x01
+#define CONTROL_RST_RX    0x02
+#define CONTROL_IE        0x10
+
+#define UART_REG(reg) (*REG32(UARTLITE_BASEADDR + (reg) * 4))
+
+#define RXBUF_SIZE 128
+static cbuf_t uart_rx_buf;
+
+void uartlite_putc(char c)
+{
+    while (UART_REG(R_STATUS) & STATUS_TXFULL)
+        ;
+    UART_REG(R_TX) = c;
+}
+
+int uartlite_getc(bool wait)
+{
+#if 0
+    char c;
+    if (cbuf_read_char(&uart_rx_buf, &c, wait) == 1)
+        return c;
+#else
+    do {
+        if (UART_REG(R_STATUS) & STATUS_RXVALID) {
+            char c = UART_REG(R_RX);
+            return c;
+        }
+    } while (wait);
+#endif
+
+    return -1;
+}
+
+enum handler_return uartlite_irq(void *arg)
+{
+    bool resched = false;
+
+    /* while receive fifo not empty, read a char */
+    while (UART_REG(R_STATUS) & STATUS_RXVALID) {
+        char c = UART_REG(R_RX);
+        cbuf_write_char(&uart_rx_buf, c, false);
+
+        resched = true;
+    }
+
+    return resched ? INT_RESCHEDULE : INT_NO_RESCHEDULE;
+}
+
+static void uartlite_init(uint level)
+{
+    TRACE;
+
+    //UART_REG(R_CTRL) = CONTROL_RST_TX | CONTROL_RST_RX;
+//    UART_REG(R_CTRL) |= CONTROL_IE;
+
+    cbuf_initialize(&uart_rx_buf, RXBUF_SIZE);
+
+//    register_int_handler(UARTLITE_IRQ, uartlite_irq, NULL);
+//    unmask_interrupt(UARTLITE_IRQ);
+}
+
+LK_INIT_HOOK(uartlite, uartlite_init, LK_INIT_LEVEL_PLATFORM);
+