[board][cleo] Add support for the cleo board

Add new boot shim, flashing and signing scripts for cleo.
Cleo shares same board driver as mt8167s_ref.
As we add support for cleo specific features, the mt8167s_ref
driver can do things differently based on platform VID and PID.

TEST: boot on mt8167s_ref and cleo.
Change-Id: I567a9f7af0b7b8bb5ec35046048b9ae057a71e2f
diff --git a/kernel/target/arm64/board/cleo/boot-shim-config.h b/kernel/target/arm64/board/cleo/boot-shim-config.h
new file mode 100644
index 0000000..8f30ee2
--- /dev/null
+++ b/kernel/target/arm64/board/cleo/boot-shim-config.h
@@ -0,0 +1,128 @@
+// 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
+
+#define WDT_MODE        0x10007000
+#define WDT_MODE_EN     (1 << 0)
+#define WDT_MODE_KEY    0x22000000
+
+static void disable_watchdog(void) {
+    volatile uint32_t* wdt_mode = (uint32_t*)WDT_MODE;
+    uint32_t tmp = *wdt_mode;
+    tmp &= ~WDT_MODE_EN;
+    tmp |= WDT_MODE_KEY;
+    *wdt_mode = tmp;
+}
+
+static const zbi_cpu_config_t cpu_config = {
+    .cluster_count = 1,
+    .clusters = {
+        {
+            .cpu_count = 4,
+        },
+    },
+};
+
+static const zbi_mem_range_t mem_config[] = {
+    {
+        .type = ZBI_MEM_RANGE_RAM,
+        .paddr = 0x40000000,
+        .length = 0x40000000, // 1GB
+    },
+    {
+        .type = ZBI_MEM_RANGE_PERIPHERAL,
+        // This is not the entire peripheral range, but enough to cover what we use in the kernel.
+        .paddr = 0x10000000,
+        .length = 0x01200000,
+    },
+    {
+        // Memory to reserve to avoid stomping on bootloader data.
+        .type = ZBI_MEM_RANGE_RESERVED,
+        .paddr = 0x40000000,
+        .length = 0x80000,
+    },
+    {
+        // mt8167-atf-reserved-memory
+        .type = ZBI_MEM_RANGE_RESERVED,
+        .paddr = 0x43000000,
+        .length = 0x30000,
+    },
+    {
+        // ram_console
+        .type = ZBI_MEM_RANGE_RESERVED,
+        .paddr = 0x44400000,
+        .length = 0x10000,
+    },
+    {
+        // pstore
+        .type = ZBI_MEM_RANGE_RESERVED,
+        .paddr = 0x44410000,
+        .length = 0xe0000,
+    },
+    {
+        // minirdump
+        .type = ZBI_MEM_RANGE_RESERVED,
+        .paddr = 0x444f0000,
+        .length = 0x10000,
+    },
+};
+
+static const dcfg_soc_uart_t uart_driver = {
+    .soc_mmio_phys = 0x10200000,
+    .uart_mmio_phys = 0x11005000,
+    .irq = 84 + 32,
+};
+
+static const dcfg_arm_gicv2_driver_t gicv2_driver = {
+    .mmio_phys = 0x10310000,
+    .gicd_offset = 0x00000,
+    .gicc_offset = 0x1f000,
+    .gich_offset = 0x30000,
+    .gicv_offset = 0x50000,
+    .ipi_base = 5,
+};
+
+static const dcfg_arm_psci_driver_t psci_driver = {
+    .use_hvc = false,
+};
+
+static const dcfg_arm_generic_timer_driver_t timer_driver = {
+    .irq_phys = 16 + 14,    // PHYS_NONSECURE_PPI: GIC_PPI 14
+    .irq_virt = 16 + 11,    // VIRT_PPI: GIC_PPI 11
+};
+
+static const zbi_platform_id_t platform_id = {
+    .vid = PDEV_VID_GOOGLE,
+    .pid = PDEV_PID_CLEO,
+    .board_name = "cleo",
+};
+
+static void append_board_boot_item(zbi_header_t* bootdata) {
+    // Disable watchdog timer for now.
+    // TODO - remove this after we have a proper watchdog driver in userspace.
+    disable_watchdog();
+
+    // 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/cleo/package-image.sh b/kernel/target/arm64/board/cleo/package-image.sh
new file mode 100755
index 0000000..b427669
--- /dev/null
+++ b/kernel/target/arm64/board/cleo/package-image.sh
@@ -0,0 +1,17 @@
+#!/usr/bin/env bash
+
+# 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
+
+set -eo pipefail
+
+DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
+ZIRCON_DIR="${DIR}/../../../../.."
+SCRIPTS_DIR="${ZIRCON_DIR}/scripts"
+
+"${SCRIPTS_DIR}/package-image.sh" -a -b cleo \
+    -d "$ZIRCON_DIR/kernel/target/arm64/dtb/dummy-device-tree.dtb" -D append \
+    -g -M 0x40000000 $@
diff --git a/kernel/target/arm64/board/cleo/rules.mk b/kernel/target/arm64/board/cleo/rules.mk
new file mode 100644
index 0000000..6fff8ac
--- /dev/null
+++ b/kernel/target/arm64/board/cleo/rules.mk
@@ -0,0 +1,9 @@
+# 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
+
+PLATFORM_BOARD_NAME := cleo
+
+include kernel/target/arm64/boot-shim/rules.mk
diff --git a/kernel/target/arm64/boot-shim/cleo-uart.c b/kernel/target/arm64/boot-shim/cleo-uart.c
new file mode 100644
index 0000000..0a93ab4
--- /dev/null
+++ b/kernel/target/arm64/boot-shim/cleo-uart.c
@@ -0,0 +1,6 @@
+// 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.
+
+// Cleo is also mt8167s.
+#include "mt8167s_ref-uart.c"
diff --git a/scripts/flash-cleo b/scripts/flash-cleo
new file mode 100755
index 0000000..744b2a7
--- /dev/null
+++ b/scripts/flash-cleo
@@ -0,0 +1,36 @@
+#!/usr/bin/env bash
+
+# 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
+
+set -eo pipefail
+
+SCRIPTS_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
+ZIRCON_DIR="${SCRIPTS_DIR}/.."
+EXTRA_ARGS=()
+
+function HELP {
+    echo "help:"
+    echo "-m            : Add mexec option to command line"
+    echo "-h for help"
+    exit 1
+}
+
+while getopts "mh" FLAG; do
+    case $FLAG in
+        m) EXTRA_ARGS+=(-m);;
+        h) HELP;;
+        \?)
+            echo unrecognized option
+            HELP
+            ;;
+    esac
+done
+shift $((OPTIND-1))
+
+"${ZIRCON_DIR}/kernel/target/arm64/board/cleo/package-image.sh" -B "${ZIRCON_DIR}/build-arm64" "${EXTRA_ARGS[@]}"
+
+"${SCRIPTS_DIR}/flash-avb" -b cleo "$@"
diff --git a/system/dev/board/mt8167s_ref/mt8167.cpp b/system/dev/board/mt8167s_ref/mt8167.cpp
index 73c1da0..21d7c77 100644
--- a/system/dev/board/mt8167s_ref/mt8167.cpp
+++ b/system/dev/board/mt8167s_ref/mt8167.cpp
@@ -133,9 +133,12 @@
 
 } // namespace board_mt8167
 
-ZIRCON_DRIVER_BEGIN(mt8167, board_mt8167::driver_ops, "zircon", "0.1", 3)
+ZIRCON_DRIVER_BEGIN(mt8167, board_mt8167::driver_ops, "zircon", "0.1", 6)
     BI_ABORT_IF(NE, BIND_PROTOCOL, ZX_PROTOCOL_PBUS),
-    BI_ABORT_IF(NE, BIND_PLATFORM_DEV_VID, PDEV_VID_MEDIATEK),
+    BI_GOTO_IF(NE, BIND_PLATFORM_DEV_VID, PDEV_VID_MEDIATEK, 0),
     BI_MATCH_IF(EQ, BIND_PLATFORM_DEV_PID, PDEV_PID_MEDIATEK_8167S_REF),
+    BI_LABEL(0),
+    BI_ABORT_IF(NE, BIND_PLATFORM_DEV_VID, PDEV_VID_GOOGLE),
+    BI_MATCH_IF(EQ, BIND_PLATFORM_DEV_PID, PDEV_PID_CLEO),
 ZIRCON_DRIVER_END(mt8167)
 
diff --git a/system/dev/bus/platform/platform-bus.cpp b/system/dev/bus/platform/platform-bus.cpp
index 8e03373..8ce1076 100644
--- a/system/dev/bus/platform/platform-bus.cpp
+++ b/system/dev/bus/platform/platform-bus.cpp
@@ -337,6 +337,9 @@
             board_info_.board_revision = 0;
             got_platform_id = true;
 
+            zxlogf(INFO, "platform bus: VID: %u PID: %u board: \"%s\"\n", platform_id.vid,
+                   platform_id.pid, platform_id.board_name);
+
             // Publish board name to sysinfo driver
             status = device_publish_metadata(parent(), "/dev/misc/sysinfo",
                                              DEVICE_METADATA_BOARD_NAME, platform_id.board_name,
diff --git a/system/ulib/ddk/include/ddk/platform-defs.h b/system/ulib/ddk/include/ddk/platform-defs.h
index 4070989..b72ae7f 100644
--- a/system/ulib/ddk/include/ddk/platform-defs.h
+++ b/system/ulib/ddk/include/ddk/platform-defs.h
@@ -58,6 +58,7 @@
 #define PDEV_PID_ASTRO              3
 #define PDEV_PID_MADRONE            4
 #define PDEV_PID_SHERLOCK           5
+#define PDEV_PID_CLEO               6
 
 #define PDEV_DID_GAUSS_AUDIO_IN     1
 #define PDEV_DID_GAUSS_AUDIO_OUT    2