sherlock shim

Change-Id: I6e070cfc22697beadcf2390d020549229ee64778
diff --git a/kernel/target/arm64/board/sherlock/boot-shim-config.h b/kernel/target/arm64/board/sherlock/boot-shim-config.h
new file mode 100644
index 0000000..646351b
--- /dev/null
+++ b/kernel/target/arm64/board/sherlock/boot-shim-config.h
@@ -0,0 +1,97 @@
+// Copyright 2018 The Fuchsia Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#define HAS_DEVICE_TREE 1
+
+static const zbi_cpu_config_t cpu_config = {
+    .cluster_count = 2,
+    .clusters = {
+        {
+            .cpu_count = 2,
+        },
+        {
+            .cpu_count = 4,
+        },
+    },
+};
+
+static const zbi_mem_range_t mem_config[] = {
+    {
+        .type = ZBI_MEM_RANGE_RAM,
+        .paddr = 0,
+        .length = 0x80000000, // 2GB
+    },
+    {
+        .type = ZBI_MEM_RANGE_PERIPHERAL,
+        .paddr = 0xc0000000,
+        .length = 0x20000000,
+    },
+    // linux,meson-fb
+    {
+        .type = ZBI_MEM_RANGE_RESERVED,
+        .paddr = 0x7f800000,
+        .length = 0x800000,
+    },
+    // linux,secmon
+    {
+        .type = ZBI_MEM_RANGE_RESERVED,
+        .paddr = 0x05000000,
+        .length = 0x2400000,
+    },
+};
+
+static const dcfg_simple_t uart_driver = {
+    .mmio_phys = 0xff803000,
+    .irq = 225,
+};
+
+static const dcfg_arm_gicv2_driver_t gicv2_driver = {
+    .mmio_phys = 0xffc00000,
+    .gicd_offset = 0x1000,
+    .gicc_offset = 0x2000,
+    .gich_offset = 0x4000,
+    .gicv_offset = 0x6000,
+    .ipi_base = 5,
+};
+
+static const dcfg_arm_psci_driver_t psci_driver = {
+    .use_hvc = false,
+    .reboot_args = { 1, 0, 0 },
+    .reboot_bootloader_args = { 4, 0, 0 },
+    .reboot_recovery_args = { 2, 0, 0 },
+};
+
+static const dcfg_arm_generic_timer_driver_t timer_driver = {
+    .irq_phys = 30,
+};
+
+static const zbi_platform_id_t platform_id = {
+    .vid = PDEV_VID_GOOGLE,
+    .pid = PDEV_PID_SHERLOCK,
+    .board_name = "sherlock",
+};
+
+static void append_board_boot_item(zbi_header_t* bootdata) {
+    // add CPU configuration
+    append_boot_item(bootdata, ZBI_TYPE_CPU_CONFIG, 0, &cpu_config,
+                    sizeof(zbi_cpu_config_t) +
+                    sizeof(zbi_cpu_cluster_t) * cpu_config.cluster_count);
+
+    // add memory configuration
+    append_boot_item(bootdata, ZBI_TYPE_MEM_CONFIG, 0, &mem_config,
+                    sizeof(zbi_mem_range_t) * countof(mem_config));
+
+    // add kernel drivers
+    append_boot_item(bootdata, ZBI_TYPE_KERNEL_DRIVER, KDRV_MT8167_UART, &uart_driver,
+                    sizeof(uart_driver));
+    append_boot_item(bootdata, ZBI_TYPE_KERNEL_DRIVER, KDRV_ARM_GIC_V2, &gicv2_driver,
+                    sizeof(gicv2_driver));
+    append_boot_item(bootdata, ZBI_TYPE_KERNEL_DRIVER, KDRV_ARM_PSCI, &psci_driver,
+                    sizeof(psci_driver));
+    append_boot_item(bootdata, ZBI_TYPE_KERNEL_DRIVER, KDRV_ARM_GENERIC_TIMER, &timer_driver,
+                    sizeof(timer_driver));
+
+    // add platform ID
+    append_boot_item(bootdata, ZBI_TYPE_PLATFORM_ID, 0, &platform_id, sizeof(platform_id));
+}
diff --git a/kernel/target/arm64/board/sherlock/rules.mk b/kernel/target/arm64/board/sherlock/rules.mk
new file mode 100644
index 0000000..bb895e7
--- /dev/null
+++ b/kernel/target/arm64/board/sherlock/rules.mk
@@ -0,0 +1,19 @@
+# Copyright 2018 The Fuchsia Authors
+#
+# Use of this source code is governed by a MIT-style
+# license that can be found in the LICENSE file or at
+# https://opensource.org/licenses/MIT
+
+LOCAL_DIR := $(GET_LOCAL_DIR)
+
+PLATFORM_BOARD_NAME := sherlock
+
+PLATFORM_USE_SHIM := true
+PLATFORM_USE_MKKDTB := true
+PLATFORM_USE_MKBOOTIMG := true
+PLATFORM_KERNEL_OFFSET := 0x01080000
+PLATFORM_MEMBASE := 0x00000000
+PLATFORM_CMDLINE := netsvc.netboot=true
+PLATFORM_BOOT_PARTITION_SIZE := 33554432
+
+include make/board.mk
diff --git a/kernel/target/arm64/boot-shim/debug.h b/kernel/target/arm64/boot-shim/debug.h
index 3d9db66..5c268e6 100644
--- a/kernel/target/arm64/boot-shim/debug.h
+++ b/kernel/target/arm64/boot-shim/debug.h
@@ -7,7 +7,7 @@
 #include <stdint.h>
 
 // Uncomment to enable debug UART.
-// #define DEBUG_UART 1
+#define DEBUG_UART 1
 
 // Board specific.
 void uart_pputc(char c);
diff --git a/kernel/target/arm64/boot-shim/sherlock-uart.c b/kernel/target/arm64/boot-shim/sherlock-uart.c
new file mode 100644
index 0000000..00f197d
--- /dev/null
+++ b/kernel/target/arm64/boot-shim/sherlock-uart.c
@@ -0,0 +1,18 @@
+// Copyright 2018 The Fuchsia Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include <stdint.h>
+#include "debug.h"
+
+#define S905_UART_WFIFO             (0x0)
+#define S905_UART_STATUS            (0xc)
+#define S905_UART_STATUS_TXFULL     (1 << 21)
+
+#define UARTREG(reg) (*(volatile uint32_t*)(0xff803000 + (reg)))
+
+void uart_pputc(char c) {
+    while (UARTREG(S905_UART_STATUS) & S905_UART_STATUS_TXFULL)
+        ;
+    UARTREG(S905_UART_WFIFO) = c;
+}
diff --git a/make/board.mk b/make/board.mk
index 2475cc7..6d04981 100644
--- a/make/board.mk
+++ b/make/board.mk
@@ -84,9 +84,17 @@
 GENERATED += $(BOARD_ZIRCON_ZBOOTIMAGE)
 EXTRA_BUILDDEPS += $(BOARD_ZIRCON_ZBOOTIMAGE)
 
+ifeq ($(PLATFORM_USE_MKKDTB),true)
+KDTBTOOL=$(BUILDDIR)/tools/mkkdtb
+
+$(BOARD_ZIRCON_ZBOOTIMAGE_DTB): $(BOARD_ZIRCON_ZBOOTIMAGE) $(DUMMY_DTB) $(KDTBTOOL)
+	$(call BUILDECHO,generating $@)
+	$(NOECHO)$(KDTBTOOL) $(BOARD_ZIRCON_ZBOOTIMAGE) $(DUMMY_DTB) $@
+else
 $(BOARD_ZIRCON_ZBOOTIMAGE_DTB): $(BOARD_ZIRCON_ZBOOTIMAGE) $(DUMMY_DTB)
 	$(call BUILDECHO,generating $@)
 	$(NOECHO)cat $(BOARD_ZIRCON_ZBOOTIMAGE) $(DUMMY_DTB) > $@
+endif
 
 GENERATED += $(BOARD_ZIRCON_ZBOOTIMAGE_DTB)
 EXTRA_BUILDDEPS += $(BOARD_ZIRCON_ZBOOTIMAGE_DTB)