[Feature]add MT2731_MP2_MR2_SVN388 baseline version

Change-Id: Ief04314834b31e27effab435d3ca8ba33b499059
diff --git a/src/bsp/lk/platform/qemu-mips/debug.c b/src/bsp/lk/platform/qemu-mips/debug.c
new file mode 100644
index 0000000..c69c5da
--- /dev/null
+++ b/src/bsp/lk/platform/qemu-mips/debug.c
@@ -0,0 +1,110 @@
+/*
+ * Copyright (c) 2009 Corey Tabaka
+ * 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 <stdarg.h>
+#include <reg.h>
+#include <trace.h>
+#include <stdio.h>
+#include <kernel/thread.h>
+#include <lib/cbuf.h>
+#include <platform/interrupts.h>
+#include <platform/qemu-mips.h>
+
+static int uart_baud_rate = 115200;
+static int uart_io_port = 0x3f8;
+
+static cbuf_t uart_rx_buf;
+
+static enum handler_return uart_irq_handler(void *arg)
+{
+    unsigned char c;
+    bool resched = false;
+
+    while (isa_read_8(uart_io_port + 5) & (1<<0)) {
+        c = isa_read_8(uart_io_port + 0);
+        cbuf_write_char(&uart_rx_buf, c, false);
+        resched = true;
+    }
+
+    return resched ? INT_RESCHEDULE : INT_NO_RESCHEDULE;
+}
+
+void platform_init_uart(void)
+{
+    /* configure the uart */
+    int divisor = 115200 / uart_baud_rate;
+
+    /* get basic config done so that tx functions */
+    isa_write_8(uart_io_port + 3, 0x80); // set up to load divisor latch
+    isa_write_8(uart_io_port + 0, divisor & 0xff); // lsb
+    isa_write_8(uart_io_port + 1, divisor >> 8); // msb
+    isa_write_8(uart_io_port + 3, 3); // 8N1
+    isa_write_8(uart_io_port + 2, 0x07); // enable FIFO, clear, 14-byte threshold
+}
+
+void uart_init(void)
+{
+    /* finish uart init to get rx going */
+    cbuf_initialize(&uart_rx_buf, 16);
+
+    register_int_handler(0x4, uart_irq_handler, NULL);
+    unmask_interrupt(0x4);
+
+    isa_write_8(uart_io_port + 1, 0x1); // enable receive data available interrupt
+}
+
+void uart_putc(char c)
+{
+    while ((isa_read_8(uart_io_port + 5) & (1<<6)) == 0)
+        ;
+    isa_write_8(uart_io_port + 0, c);
+}
+
+int uart_getc(char *c, bool wait)
+{
+    return cbuf_read_char(&uart_rx_buf, c, wait);
+}
+
+void platform_dputc(char c)
+{
+    if (c == '\n')
+        platform_dputc('\r');
+#if WITH_CGA_CONSOLE
+    cputc(c);
+#else
+    uart_putc(c);
+#endif
+}
+
+int platform_dgetc(char *c, bool wait)
+{
+#if WITH_CGA_CONSOLE
+    int ret =  platform_read_key(c);
+    //if (ret < 0)
+    //  arch_idle();
+#else
+    int ret = uart_getc(c, wait);
+#endif
+
+    return ret;
+}
diff --git a/src/bsp/lk/platform/qemu-mips/include/platform/qemu-mips.h b/src/bsp/lk/platform/qemu-mips/include/platform/qemu-mips.h
new file mode 100644
index 0000000..5873adc
--- /dev/null
+++ b/src/bsp/lk/platform/qemu-mips/include/platform/qemu-mips.h
@@ -0,0 +1,48 @@
+/*
+ * 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.
+ */
+#pragma once
+
+#include <stdint.h>
+
+/*
+ * The plain mips target for qemu has an emulated PC style UART mapped
+ * into the ISA io port apterture at 0x14000000
+ */
+#define ISA_IO_BASE ((volatile uint8_t *)0x14000000 + 0x80000000)
+#define UART_PORT_BASE (0x3f8)
+
+static inline void isa_write_8(uint16_t port, uint8_t val)
+{
+    volatile uint8_t *addr = ISA_IO_BASE + port;
+
+    *addr = val;
+}
+
+static inline uint8_t isa_read_8(uint16_t port)
+{
+    volatile uint8_t *addr = ISA_IO_BASE + port;
+
+    return *addr;
+}
+
+#define INT_VECTORS 8
diff --git a/src/bsp/lk/platform/qemu-mips/intc.c b/src/bsp/lk/platform/qemu-mips/intc.c
new file mode 100644
index 0000000..de1f952
--- /dev/null
+++ b/src/bsp/lk/platform/qemu-mips/intc.c
@@ -0,0 +1,287 @@
+/*
+ * Copyright (c) 2009 Corey Tabaka
+ * Copyright (c) 2015 Intel Corporation
+ *
+ * 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 <sys/types.h>
+#include <debug.h>
+#include <trace.h>
+#include <err.h>
+#include <reg.h>
+#include <kernel/thread.h>
+#include <platform/interrupts.h>
+#include <arch/ops.h>
+#include <arch/mips.h>
+#include <platform/qemu-mips.h>
+
+#define LOCAL_TRACE 0
+
+static spin_lock_t lock;
+
+#define PIC1 0x20
+#define PIC2 0xA0
+
+#define ICW1 0x11
+#define ICW4 0x01
+
+#define PIC1_CMD                    0x20
+#define PIC1_DATA                   0x21
+#define PIC2_CMD                    0xA0
+#define PIC2_DATA                   0xA1
+#define PIC_READ_IRR                0x0a    /* OCW3 irq ready next CMD read */
+#define PIC_READ_ISR                0x0b    /* OCW3 irq service next CMD read */
+
+#define ICW1_ICW4   0x01        /* ICW4 (not) needed */
+#define ICW1_SINGLE 0x02        /* Single (cascade) mode */
+#define ICW1_INTERVAL4  0x04    /* Call address interval 4 (8) */
+#define ICW1_LEVEL  0x08        /* Level triggered (edge) mode */
+#define ICW1_INIT   0x10        /* Initialization */
+
+#define ICW4_8086   0x01        /* 8086/88 (MCS-80/85) mode */
+#define ICW4_AUTO   0x02        /* Auto (normal) EOI */
+#define ICW4_BUF_SLAVE  0x08    /* Buffered mode/slave */
+#define ICW4_BUF_MASTER 0x0C    /* Buffered mode/master */
+#define ICW4_SFNM   0x10        /* Special fully nested (not) */
+
+struct int_handler_struct {
+    int_handler handler;
+    void *arg;
+};
+
+#define INT_PIC2 2
+
+static struct int_handler_struct int_handler_table[INT_VECTORS];
+
+/*
+ * Cached IRQ mask (enabled/disabled)
+ */
+static uint8_t irqMask[2];
+
+/*
+ * init the PICs and remap them
+ */
+static void map(uint32_t pic1, uint32_t pic2)
+{
+    /* send ICW1 */
+    isa_write_8(PIC1, ICW1);
+    isa_write_8(PIC2, ICW1);
+
+    /* send ICW2 */
+    isa_write_8(PIC1 + 1, pic1);   /* remap */
+    isa_write_8(PIC2 + 1, pic2);   /*  pics */
+
+    /* send ICW3 */
+    isa_write_8(PIC1 + 1, 4);  /* IRQ2 -> connection to slave */
+    isa_write_8(PIC2 + 1, 2);
+
+    /* send ICW4 */
+    isa_write_8(PIC1 + 1, 2|5);
+    isa_write_8(PIC2 + 1, 2|1);
+
+    /* disable all IRQs */
+    isa_write_8(PIC1 + 1, 0xff);
+    isa_write_8(PIC2 + 1, 0xff);
+
+    irqMask[0] = 0xff;
+    irqMask[1] = 0xff;
+}
+
+static void enable(unsigned int vector, bool enable)
+{
+    if (vector < 8) {
+        uint8_t bit = 1 << vector;
+
+        if (enable && (irqMask[0] & bit)) {
+            irqMask[0] = isa_read_8(PIC1 + 1);
+            irqMask[0] &= ~bit;
+            isa_write_8(PIC1 + 1, irqMask[0]);
+            irqMask[0] = isa_read_8(PIC1 + 1);
+        } else if (!enable && !(irqMask[0] & bit)) {
+            irqMask[0] = isa_read_8(PIC1 + 1);
+            irqMask[0] |= bit;
+            isa_write_8(PIC1 + 1, irqMask[0]);
+            irqMask[0] = isa_read_8(PIC1 + 1);
+        }
+    } else if (vector < 16) {
+        vector -= 8;
+
+        uint8_t bit = 1 << vector;
+
+        if (enable && (irqMask[1] & bit)) {
+            irqMask[1] = isa_read_8(PIC2 + 1);
+            irqMask[1] &= ~bit;
+            isa_write_8(PIC2 + 1, irqMask[1]);
+            irqMask[1] = isa_read_8(PIC2 + 1);
+        } else if (!enable && !(irqMask[1] & bit)) {
+            irqMask[1] = isa_read_8(PIC2 + 1);
+            irqMask[1] |= bit;
+            isa_write_8(PIC2 + 1, irqMask[1]);
+            irqMask[1] = isa_read_8(PIC2 + 1);
+        }
+
+        bit = 1 << INT_PIC2;
+
+        if (irqMask[1] != 0xff && (irqMask[0] & bit)) {
+            irqMask[0] = isa_read_8(PIC1 + 1);
+            irqMask[0] &= ~bit;
+            isa_write_8(PIC1 + 1, irqMask[0]);
+            irqMask[0] = isa_read_8(PIC1 + 1);
+        } else if (irqMask[1] == 0 && !(irqMask[0] & bit)) {
+            irqMask[0] = isa_read_8(PIC1 + 1);
+            irqMask[0] |= bit;
+            isa_write_8(PIC1 + 1, irqMask[0]);
+            irqMask[0] = isa_read_8(PIC1 + 1);
+        }
+    }
+}
+
+static void issueEOI(unsigned int vector)
+{
+    if (vector < 8) {
+        isa_write_8(PIC1, 0x20);
+    } else if (vector < 16) {
+        isa_write_8(PIC2, 0x20);
+        isa_write_8(PIC1, 0x20);   // must issue both for the second PIC
+    }
+}
+
+/* Helper func */
+static uint16_t __pic_get_irq_reg(uint ocw3)
+{
+    /* OCW3 to PIC CMD to get the register values.  PIC2 is chained, and
+     * represents IRQs 8-15.  PIC1 is IRQs 0-7, with 2 being the chain */
+    isa_write_8(PIC1_CMD, ocw3);
+    isa_write_8(PIC2_CMD, ocw3);
+    return (isa_read_8(PIC2_CMD) << 8) | isa_read_8(PIC1_CMD);
+}
+
+/* Returns the combined value of the cascaded PICs irq request register */
+static uint16_t pic_get_irr(void)
+{
+    return __pic_get_irq_reg(PIC_READ_IRR);
+}
+
+/* Returns the combined value of the cascaded PICs in-service register */
+static uint16_t pic_get_isr(void)
+{
+    return __pic_get_irq_reg(PIC_READ_ISR);
+}
+
+void platform_init_interrupts(void)
+{
+    // rebase the PIC out of the way of processor exceptions
+    map(0, 8);
+}
+
+status_t mask_interrupt(unsigned int vector)
+{
+    if (vector >= INT_VECTORS)
+        return ERR_INVALID_ARGS;
+
+    LTRACEF("vector %d\n", vector);
+
+    spin_lock_saved_state_t state;
+    spin_lock_irqsave(&lock, state);
+
+    enable(vector, false);
+
+    spin_unlock_irqrestore(&lock, state);
+
+    return NO_ERROR;
+}
+
+void platform_mask_irqs(void)
+{
+    irqMask[0] = isa_read_8(PIC1 + 1);
+    irqMask[1] = isa_read_8(PIC2 + 1);
+
+    isa_write_8(PIC1 + 1, 0xff);
+    isa_write_8(PIC2 + 1, 0xff);
+
+    irqMask[0] = isa_read_8(PIC1 + 1);
+    irqMask[1] = isa_read_8(PIC2 + 1);
+}
+
+status_t unmask_interrupt(unsigned int vector)
+{
+    if (vector >= INT_VECTORS)
+        return ERR_INVALID_ARGS;
+
+    LTRACEF("vector %d\n", vector);
+
+    spin_lock_saved_state_t state;
+    spin_lock_irqsave(&lock, state);
+
+    enable(vector, true);
+
+    spin_unlock_irqrestore(&lock, state);
+
+    return NO_ERROR;
+}
+
+enum handler_return platform_irq(struct mips_iframe *iframe, uint vector)
+{
+    // figure out which irq is pending
+    // issue OCW3 poll commands to PIC1 and (potentially) PIC2
+    isa_write_8(PIC1_CMD, (1<<3) | (1<<2));
+    uint8_t val = isa_read_8(PIC1_CMD);
+    if ((val & 0x80) == 0) {
+        // spurious?
+        return INT_NO_RESCHEDULE;
+    }
+    val &= ~0x80;
+    if (val == INT_PIC2) {
+        isa_write_8(PIC2_CMD, (1<<3) | (1<<2));
+        val = isa_read_8(PIC2_CMD);
+        if ((val & 0x80) == 0) {
+            // spurious?
+            return INT_NO_RESCHEDULE;
+        }
+        val &= ~0x80;
+    }
+    vector = val;
+    LTRACEF("poll vector 0x%x\n", vector);
+
+    THREAD_STATS_INC(interrupts);
+
+    // deliver the interrupt
+    enum handler_return ret = INT_NO_RESCHEDULE;
+
+    if (int_handler_table[vector].handler)
+        ret = int_handler_table[vector].handler(int_handler_table[vector].arg);
+
+    return ret;
+}
+
+void register_int_handler(unsigned int vector, int_handler handler, void *arg)
+{
+    if (vector >= INT_VECTORS)
+        panic("register_int_handler: vector out of range %d\n", vector);
+
+    spin_lock_saved_state_t state;
+    spin_lock_irqsave(&lock, state);
+
+    int_handler_table[vector].arg = arg;
+    int_handler_table[vector].handler = handler;
+
+    spin_unlock_irqrestore(&lock, state);
+}
+
diff --git a/src/bsp/lk/platform/qemu-mips/platform.c b/src/bsp/lk/platform/qemu-mips/platform.c
new file mode 100644
index 0000000..6296325
--- /dev/null
+++ b/src/bsp/lk/platform/qemu-mips/platform.c
@@ -0,0 +1,50 @@
+/*
+ * 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 <sys/types.h>
+#include <kernel/thread.h>
+#include <platform.h>
+#include <platform/interrupts.h>
+#include <platform/debug.h>
+#include <platform/timer.h>
+#include <platform/qemu-mips.h>
+#include <arch/mips.h>
+
+extern void platform_init_interrupts(void);
+extern void platform_init_uart(void);
+extern void uart_init(void);
+
+void platform_early_init(void)
+{
+    platform_init_interrupts();
+    platform_init_uart();
+
+    mips_init_timer(100000000);
+    mips_enable_irq(2);
+}
+
+void platform_init(void)
+{
+    uart_init();
+}
+
diff --git a/src/bsp/lk/platform/qemu-mips/rules.mk b/src/bsp/lk/platform/qemu-mips/rules.mk
new file mode 100644
index 0000000..aa689d5
--- /dev/null
+++ b/src/bsp/lk/platform/qemu-mips/rules.mk
@@ -0,0 +1,21 @@
+LOCAL_DIR := $(GET_LOCAL_DIR)
+
+MODULE := $(LOCAL_DIR)
+
+ARCH := mips
+MIPS_CPU := m14k
+
+MODULE_DEPS += \
+    lib/cbuf
+
+MODULE_SRCS += \
+	$(LOCAL_DIR)/debug.c \
+	$(LOCAL_DIR)/intc.c \
+	$(LOCAL_DIR)/platform.c
+
+MEMBASE ?= 0x80000000 # not exactly correct but gets us going for now
+MEMSIZE ?= 0x01000000 # 16MB
+
+MODULE_DEPS += \
+
+include make/module.mk