[Feature]add MT2731_MP2_MR2_SVN388 baseline version

Change-Id: Ief04314834b31e27effab435d3ca8ba33b499059
diff --git a/src/bsp/lk/target/qemu-m4/include/target/debugconfig.h b/src/bsp/lk/target/qemu-m4/include/target/debugconfig.h
new file mode 100644
index 0000000..4a2b79d
--- /dev/null
+++ b/src/bsp/lk/target/qemu-m4/include/target/debugconfig.h
@@ -0,0 +1,29 @@
+/*
+ * Copyright (c) 2012 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.
+ */
+#ifndef __TARGET_DEBUGCONFIG_H
+#define __TARGET_DEBUGCONFIG_H
+
+// UART 3 is the console
+#define DEBUG_UART 3
+
+#endif
diff --git a/src/bsp/lk/target/qemu-m4/include/target/m4display.h b/src/bsp/lk/target/qemu-m4/include/target/m4display.h
new file mode 100644
index 0000000..e51fd28
--- /dev/null
+++ b/src/bsp/lk/target/qemu-m4/include/target/m4display.h
@@ -0,0 +1,26 @@
+/*
+ * Copyright (c) 2015 Gurjant Kalsi <me@gurjantkalsi.com>
+ *
+ * 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
+
+void init_display(void);
diff --git a/src/bsp/lk/target/qemu-m4/init.c b/src/bsp/lk/target/qemu-m4/init.c
new file mode 100644
index 0000000..c0bb36f
--- /dev/null
+++ b/src/bsp/lk/target/qemu-m4/init.c
@@ -0,0 +1,51 @@
+/*
+ * Copyright (c) 2012 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 <err.h>
+#include <debug.h>
+#include <trace.h>
+#include <target.h>
+#include <compiler.h>
+#include <dev/gpio.h>
+#include <stm32f4xx_usart.h>
+#include <stm32f4xx_rcc.h>
+#include <stm32f4xx_gpio.h>
+#include <platform/stm32.h>
+#include <platform/gpio.h>
+#include <target/debugconfig.h>
+#include <target/m4display.h>
+
+void target_early_init(void)
+{
+    stm32_debug_early_init();
+}
+
+void target_init(void)
+{
+    TRACE_ENTRY;
+
+    stm32_debug_init();
+
+    init_display();
+
+    TRACE_EXIT;
+}
\ No newline at end of file
diff --git a/src/bsp/lk/target/qemu-m4/m4display.c b/src/bsp/lk/target/qemu-m4/m4display.c
new file mode 100644
index 0000000..c038dfe
--- /dev/null
+++ b/src/bsp/lk/target/qemu-m4/m4display.c
@@ -0,0 +1,217 @@
+/*
+ * Copyright (c) 2015 Gurjant Kalsi <me@gurjantkalsi.com>
+ *
+ * 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 <target/m4display.h>
+
+#include <stdio.h>
+#include <trace.h>
+#include <err.h>
+
+#include <stm32f4xx.h>
+#include <stm32f4xx_spi.h>
+#include <stm32f4xx_gpio.h>
+#include <stm32f4xx_rcc.h>
+
+#include <dev/display.h>
+#include <dev/gpio.h>
+#include <lib/gfx.h>
+#include <platform/gpio.h>
+
+#define LOCAL_TRACE 0
+
+#define CMD_DISPLAY_NULL        0x00
+#define CMD_DISPLAY_SET_PARAM   0x01
+#define CMD_DISPLAY_OFF         0x02
+#define CMD_DISPLAY_ON          0x03
+#define CMD_DISPLAY_DRAW_SCENE  0x04
+#define CMD_DISPLAY_FRAME_BEGIN 0x05
+
+#define SCENE_BLACK  0x00
+#define SCENE_SPLASH 0x01
+#define SCENE_UPDATE 0x02
+#define SCENE_ERROR  0x03
+
+#define M4DISPLAY_WIDTH  180
+#define M4DISPLAY_HEIGHT 180
+
+static uint8_t framebuffer[M4DISPLAY_HEIGHT][M4DISPLAY_WIDTH];
+
+static const char programming_header[] = "  Lattice\0iCEcube2 2014.08.26723\0Part: iCE40LP1K-CM36\0Date: Jan 30 2015 15:11:";
+
+static void chip_select(bool val)
+{
+    if (val) {
+        gpio_set(GPIO(GPIO_PORT_G, 8), true);
+    } else {
+        gpio_set(GPIO(GPIO_PORT_G, 8), false);
+    }
+}
+
+static void reset(bool val)
+{
+    if (val) {
+        gpio_set(GPIO(GPIO_PORT_G, 15), true);
+    } else {
+        gpio_set(GPIO(GPIO_PORT_G, 15), false);
+    }
+}
+
+static void setup_pins(void)
+{
+
+    GPIO_InitTypeDef GPIO_InitStruct;
+
+    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG, ENABLE);
+
+    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_6 | GPIO_Pin_5;
+    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
+    GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
+    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
+    GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
+    GPIO_Init(GPIOA, &GPIO_InitStruct);
+
+    // connect SPI6 pins to SPI alternate function
+    GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI6);
+    GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_SPI6);
+    GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI6);
+
+    // enable clock for used IO pins
+    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG, ENABLE);
+
+    /* Configure the chip select pin
+       in this case we will use PE7 */
+    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_8;
+    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
+    GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
+    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
+    GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
+    GPIO_Init(GPIOG, &GPIO_InitStruct);
+
+    GPIOE->BSRRL |= GPIO_Pin_7; // set PE7 high
+
+    // Setup display CS Pin
+    gpio_config(GPIO(GPIO_PORT_G, 8), GPIO_OUTPUT);
+
+    // Setup display reset pin
+    gpio_config(GPIO(GPIO_PORT_G, 15), GPIO_OUTPUT);
+
+    // enable peripheral clock
+    RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI6, ENABLE);
+}
+
+void init_display(void)
+{
+    // Setting up pins.
+    setup_pins();
+
+    SPI_InitTypeDef init_struct;
+
+    init_struct.SPI_Direction           = SPI_Direction_1Line_Tx;
+    init_struct.SPI_Mode                = SPI_Mode_Master;
+    init_struct.SPI_DataSize            = SPI_DataSize_8b;
+    init_struct.SPI_CPOL                = SPI_CPOL_Low;
+    init_struct.SPI_CPHA                = SPI_CPHA_1Edge;
+    init_struct.SPI_NSS                 = SPI_NSS_Hard;
+    init_struct.SPI_BaudRatePrescaler   = SPI_BaudRatePrescaler_8;
+    init_struct.SPI_FirstBit            = SPI_FirstBit_MSB;
+
+    SPI_Init(SPI6, &init_struct);
+
+    SPI_Cmd(SPI6, ENABLE); // enable SPI6
+
+    chip_select(true);
+    reset(true);
+    chip_select(false);
+
+    const uint8_t draw_splash_cmds[] = { 0x04, 0x01 };
+
+    for (size_t i = 0; i < countof(draw_splash_cmds); i++) {
+        SPI_I2S_SendData(SPI6, draw_splash_cmds[i]);
+    }
+
+    chip_select(true);
+    chip_select(false);
+
+    uint8_t enable_display_cmds[] = { 0x03 };
+
+    for (size_t i = 0; i < countof(enable_display_cmds); i++) {
+        SPI_I2S_SendData(SPI6, enable_display_cmds[i]);
+    }
+
+    chip_select(true);
+    reset(false);
+    chip_select(false);
+    reset(true);
+
+
+    for (size_t i = 0; i < countof(programming_header); i++) {
+        SPI_I2S_SendData(SPI6, programming_header[i]);
+    }
+
+    chip_select(true);
+}
+
+static void s4lcd_flush(uint starty, uint endy)
+{
+
+    chip_select(false);
+
+    uint8_t frame_begin_cmd[] = { 0x05 };
+
+    for (size_t i = 0; i < countof(frame_begin_cmd); i++) {
+        SPI_I2S_SendData(SPI6, frame_begin_cmd[i]);
+    }
+
+    size_t msb_idx = M4DISPLAY_WIDTH / 2;
+
+    uint8_t scramble_buf[M4DISPLAY_WIDTH];
+    for (int i = 0; i < M4DISPLAY_HEIGHT; i++) {
+        uint8_t *lsb = scramble_buf;
+        uint8_t *msb = &(scramble_buf[msb_idx]);
+        for (int j = 0; j < M4DISPLAY_WIDTH; j += 2) {
+
+            *msb++ = ((framebuffer[i][j] & 0b01010100) | ((framebuffer[i][j+1] & 0b01010100) << 1)) >> 2;
+            *lsb++ = ((framebuffer[i][j] & 0b10101000) | ((framebuffer[i][j+1] & 0b10101000) >> 1)) >> 2;
+        }
+
+        for (int j = 0; j < M4DISPLAY_WIDTH; j++) {
+            SPI_I2S_SendData(SPI6, scramble_buf[j]);
+        }
+    }
+
+    chip_select(true);
+}
+
+status_t display_get_info(struct display_info *info)
+{
+    LTRACEF("display_info %p\n", info);
+
+    info->framebuffer = (void *)framebuffer;
+    info->format = GFX_FORMAT_RGB_2220;
+    info->width = M4DISPLAY_WIDTH;
+    info->height = M4DISPLAY_HEIGHT;
+    info->stride = M4DISPLAY_WIDTH;
+    info->flush = s4lcd_flush;
+
+    return NO_ERROR;
+}
diff --git a/src/bsp/lk/target/qemu-m4/rules.mk b/src/bsp/lk/target/qemu-m4/rules.mk
new file mode 100644
index 0000000..253c56d
--- /dev/null
+++ b/src/bsp/lk/target/qemu-m4/rules.mk
@@ -0,0 +1,26 @@
+LOCAL_DIR := $(GET_LOCAL_DIR)
+
+MODULE := $(LOCAL_DIR)
+
+STM32_CHIP := stm32f407
+
+PLATFORM := stm32f4xx
+
+GLOBAL_DEFINES += \
+	ENABLE_UART2=1 \
+	ENABLE_UART3=1 \
+	HSE_VALUE=8000000 \
+	PLL_M_VALUE=8 \
+	PLL_N_VALUE=336 \
+	PLL_P_VALUE=2 \
+    PKTBUF_POOL_SIZE=16
+
+MODULE_SRCS += \
+	$(LOCAL_DIR)/init.c \
+	$(LOCAL_DIR)/m4display.c
+
+MODULE_DEPS += \
+    lib/gfx
+
+include make/module.mk
+