rb5: Add support for Qcom Robotics RB5 development kit

* Add new lunch target "rb5-userdebug"
* Boots android-mainline (3aa53d85a061) to UI
* ADB works
* If you are having display issues on your brand new RB5
  kit then run the following set of adb commands to update
  the lt9611uxc firmware version:
    adb wait-for-device; adb root;
    adb shell "echo 1 > /sys/bus/i2c/devices/5-002b/lt9611uxc_firmware"
  Check installer/rb5/README for more details.

Signed-off-by: Amit Pundir <amit.pundir@linaro.org>
Change-Id: I2917de0375e95e17a88446474fdb256af769f73d
diff --git a/Android.mk b/Android.mk
index 12ec650..ed787c7 100644
--- a/Android.mk
+++ b/Android.mk
@@ -20,7 +20,7 @@
 # to only building on ARM if they include assembly. Individual makefiles
 # are responsible for having their own logic, for fine-grained control.
 
-ifneq ($(filter db845c pixel3_mainline, $(TARGET_BOARD_PLATFORM)),)
+ifneq ($(filter db845c pixel3_mainline rb5, $(TARGET_BOARD_PLATFORM)),)
 
 LOCAL_PATH := $(call my-dir)
 
diff --git a/AndroidProducts.mk b/AndroidProducts.mk
index 84cf20f..e5d63dd 100644
--- a/AndroidProducts.mk
+++ b/AndroidProducts.mk
@@ -11,8 +11,10 @@
 
 PRODUCT_MAKEFILES := \
     $(LOCAL_DIR)/db845c.mk \
+    $(LOCAL_DIR)/rb5.mk \
     $(LOCAL_DIR)/pixel3_mainline.mk
 
 COMMON_LUNCH_CHOICES := \
     db845c-userdebug \
+    rb5-userdebug \
     pixel3_mainline-userdebug
diff --git a/installer/rb5/README b/installer/rb5/README
new file mode 100644
index 0000000..ee59bdb
--- /dev/null
+++ b/installer/rb5/README
@@ -0,0 +1,70 @@
+These instructions will help you flashing the prebuilt bootloader
+and AOSP images(built from sources) on rb5.
+
+First, make sure you have built the AOSP images for RB5 by
+running the rb5-userdebug lunch target.
+
+
+Boot the board into fastboot mode:
+----------------------------------
+The easiest way to boot into fastboot mode is to run something
+like "adb reboot bootloader" from the host, or
+"reboot bootloader" on the device.  However, this
+functionality may depend on the software installed on the board
+to work, so you may need to manually enter fastboot mode as
+follows.
+
+To manually enter fastboot mode:
+--------------------------------
+* To manually boot the board into fastboot mode, press DOWN arrow
+  key over serial UART console when restarting the board.
+
+                         OR
+
+* Follow the instructions here
+https://www.96boards.org/documentation/consumer/dragonboard/qualcomm-robotics-rb5/installation/board-recovery.md.html#booting-into-fastboot
+
+
+Flash the board:
+----------------
+Once the board is in fastboot mode, you can then flash bootloader
+binaries and AOSP images by running the following script from
+device/linaro/dragonboard/installer/rb5/ directory:
+
+  $ ./flash-all-aosp.sh
+
+
+Troubleshooting:
+----------------
+* If you have flashed AOSP images and you can see the boot logs
+  on Serial prompt but no UI then try updating the lt9611uxc
+  firmware version. Factory shipped RB5 has an older version of
+  lt9611uxc firmware flashed on it and it is incompatible with
+  upstream kernel. Try updating the firmware version by running
+  the following adb shell commands:
+
+  $ adb wait-for-device; adb root; adb shell "echo 1 > /sys/bus/i2c/devices/5-002b/lt9611uxc_firmware"
+
+* Make sure the USB-C cable is connected. As it has to be
+  removed to fully power the device down, it is easy to forget
+  to reconnect it!
+
+* If you cannot get the board into fastboot mode, or for some
+  reason the flash-all-aosp.sh script does not work, you can do
+  a full recovery flash of the board using the following
+  instructions.
+
+
+Recovery:
+--------
+First boot into recovery mode by follwing the recovery
+instructions from here:
+https://www.96boards.org/documentation/consumer/dragonboard/qualcomm-robotics-rb5/installation/board-recovery.md.html
+
+Then run the recovery script from the
+device/linaro/dragonboard/installer/rb5/ directory:
+
+  $ ./recovery.sh
+
+This will do a recovery flash of the ptable and bootloader
+binaries.
diff --git a/installer/rb5/flash-all-aosp.sh b/installer/rb5/flash-all-aosp.sh
new file mode 100755
index 0000000..9eea95b
--- /dev/null
+++ b/installer/rb5/flash-all-aosp.sh
@@ -0,0 +1,54 @@
+#!/bin/bash
+
+INSTALLER_DIR="`dirname ${0}`"
+
+FIRMWARE_DIR="rb5-bootloader-ufs-aosp"
+
+# for cases that don't run "lunch rb5-userdebug"
+if [ -z "${ANDROID_BUILD_TOP}" ]; then
+    ANDROID_BUILD_TOP=${INSTALLER_DIR}/../../../../../
+    ANDROID_PRODUCT_OUT="${ANDROID_BUILD_TOP}/out/target/product/rb5"
+fi
+
+if [ ! -d "${ANDROID_PRODUCT_OUT}" ]; then
+    echo "FLASH-ALL-AOSP: error in locating out/target directory, check if it exist"
+    exit
+fi
+
+echo "android out dir:${ANDROID_PRODUCT_OUT}"
+
+# TODO: Pull one-time recovery/qdl path out of standard install
+# Flash bootloader firmware files
+if [ ! -d "${INSTALLER_DIR}/${FIRMWARE_DIR}/" ]; then
+    echo "FLASH-ALL-AOSP: No firmware directory? Make sure binaries have been provided"
+    exit
+fi
+
+pushd "${INSTALLER_DIR}/${FIRMWARE_DIR}" > /dev/null
+./flashall
+popd > /dev/null
+
+echo "FLASH-ALL-AOSP: Flash boot img"
+fastboot flash boot "${ANDROID_PRODUCT_OUT}"/boot.img
+echo "FLASH-ALL-AOSP: Flash super/dynamic image"
+fastboot flash super "${ANDROID_PRODUCT_OUT}"/super.img
+echo "FLASH-ALL-AOSP: Flash userdata image"
+fastboot flash userdata "${ANDROID_PRODUCT_OUT}"/userdata.img
+echo "FLASH-ALL-AOSP: Flash vendor_boot image"
+fastboot flash vendor_boot "${ANDROID_PRODUCT_OUT}"/vendor_boot.img
+echo "FLASH-ALL-AOSP: Formatting metadata"
+fastboot format:ext4 metadata
+
+fastboot reboot
+
+echo "FLASH-ALL-AOSP: Updating lt9611uxc firmware version"
+adb wait-for-device
+VERSION=`adb shell su 0 cat /sys/bus/i2c/devices/5-002b/lt9611uxc_firmware`
+if [ "$VERSION" -lt "43" ] ; then
+    echo "FLASH-ALL-AOSP: lt9611uxc 5-002b: Updating firmware... May take up to 120 seconds. Do not switch off the device"
+    adb shell "echo 1 | su 0 tee /sys/bus/i2c/devices/5-002b/lt9611uxc_firmware > /dev/null"
+    echo "FLASH-ALL-AOSP: lt9611uxc 5-002b: Firmware updates successfully"
+    echo "FLASH-ALL-AOSP: Rebooting"
+    adb reboot
+fi
+echo "FLASH-ALL-AOSP: Done"
diff --git a/installer/rb5/recovery.sh b/installer/rb5/recovery.sh
new file mode 100755
index 0000000..5541209
--- /dev/null
+++ b/installer/rb5/recovery.sh
@@ -0,0 +1,35 @@
+#!/bin/bash
+
+INSTALLER_DIR="`dirname ${0}`"
+
+QDL="`readlink -f ${INSTALLER_DIR}/qdl`"
+FIRMWARE_DIR="rb5-bootloader-ufs-aosp"
+
+# for cases that don't run "lunch rb5-userdebug"
+if [ -z "${ANDROID_BUILD_TOP}" ]; then
+    ANDROID_BUILD_TOP=${INSTALLER_DIR}/../../../../../
+    ANDROID_PRODUCT_OUT="${ANDROID_BUILD_TOP}/out/target/product/rb5"
+fi
+
+if [ ! -d "${ANDROID_PRODUCT_OUT}" ]; then
+    echo "RECOVERY: error in locating out directory, check if it exist"
+    exit
+fi
+
+echo "android out dir:${ANDROID_PRODUCT_OUT}"
+
+# TODO: Pull one-time recovery/qdl path out of standard install
+# Flash bootloader firmware files
+if [ ! -d "${INSTALLER_DIR}/${FIRMWARE_DIR}/" ]; then
+    echo "RECOVERY: No firmware directory? Make sure binaries have been provided"
+    exit
+fi
+
+pushd "${INSTALLER_DIR}/${FIRMWARE_DIR}" > /dev/null
+sudo "${QDL}" prog_firehose_ddr.elf rawprogram?.xml patch?.xml
+popd > /dev/null
+
+echo
+echo
+echo "RECOVERY: Please boot the RB5 into fastboot mode, and use the flash-all-aosp.sh script!"
+echo
diff --git a/rb5.mk b/rb5.mk
new file mode 100644
index 0000000..06e648e
--- /dev/null
+++ b/rb5.mk
@@ -0,0 +1,26 @@
+ifndef TARGET_KERNEL_USE
+TARGET_KERNEL_USE := mainline
+endif
+RB5_KERNEL_DIR := device/linaro/dragonboard-kernel/android-$(TARGET_KERNEL_USE)
+
+# Inherit the full_base and device configurations
+$(call inherit-product, $(SRC_TARGET_DIR)/product/core_64_bit.mk)
+$(call inherit-product, device/linaro/dragonboard/rb5/device.mk)
+$(call inherit-product, device/linaro/dragonboard/device-common.mk)
+$(call inherit-product, $(SRC_TARGET_DIR)/product/full_base.mk)
+
+# Product overrides
+PRODUCT_NAME := rb5
+PRODUCT_DEVICE := rb5
+PRODUCT_BRAND := Android
+
+ifndef RB5_USES_GKI
+  RB5_USES_GKI := true
+endif
+
+ifeq ($(RB5_USES_GKI), true)
+  RB5_MODS := $(wildcard $(RB5_KERNEL_DIR)/*.ko)
+  ifneq ($(RB5_MODS),)
+    BOARD_VENDOR_RAMDISK_KERNEL_MODULES += $(RB5_MODS)
+  endif
+endif
diff --git a/rb5/BoardConfig.mk b/rb5/BoardConfig.mk
new file mode 100644
index 0000000..415cff1
--- /dev/null
+++ b/rb5/BoardConfig.mk
@@ -0,0 +1,30 @@
+include device/linaro/dragonboard/BoardConfigCommon.mk
+
+# Board Information
+TARGET_BOOTLOADER_BOARD_NAME := rb5
+TARGET_BOARD_PLATFORM := rb5
+
+TARGET_NO_KERNEL := false
+BOARD_KERNEL_BASE := 0x80000000
+BOARD_KERNEL_PAGESIZE := 4096
+
+BOARD_INCLUDE_DTB_IN_BOOTIMG := true
+BOARD_BOOT_HEADER_VERSION := 3
+BOARD_MKBOOTIMG_ARGS := --header_version $(BOARD_BOOT_HEADER_VERSION)
+
+BOARD_KERNEL_CMDLINE := earlycon firmware_class.path=/vendor/firmware/ androidboot.hardware=rb5
+BOARD_KERNEL_CMDLINE += init=/init androidboot.boot_devices=soc@0/1d84000.ufshc printk.devkmsg=on
+BOARD_KERNEL_CMDLINE += pcie_pme=nomsi #For WiFi to work
+BOARD_KERNEL_CMDLINE += deferred_probe_timeout=30
+
+# Image Configuration
+BOARD_BOOTIMAGE_PARTITION_SIZE := 67108864 #64M
+BOARD_VENDOR_BOOTIMAGE_PARTITION_SIZE := 67108864 #64M
+BOARD_USERDATAIMAGE_PARTITION_SIZE := 21474836480
+BOARD_FLASH_BLOCK_SIZE := 131072
+
+# Super/Dynamic partition
+BOARD_SUPER_PARTITION_SIZE := 12437225472
+BOARD_DB_DYNAMIC_PARTITIONS_SIZE := 12433031168 # Reserve 4M for DAP metadata
+BOARD_SUPER_PARTITION_METADATA_DEVICE := super
+BOARD_SUPER_IMAGE_IN_UPDATE_PACKAGE := true
diff --git a/rb5/device.mk b/rb5/device.mk
new file mode 100644
index 0000000..cab614f
--- /dev/null
+++ b/rb5/device.mk
@@ -0,0 +1,69 @@
+#
+# Copyright (C) 2011 The Android Open-Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+PRODUCT_SOONG_NAMESPACES += \
+    device/linaro/dragonboard
+
+# setup dalvik vm configs
+$(call inherit-product, frameworks/native/build/tablet-10in-xhdpi-2048-dalvik-heap.mk)
+
+# Enable Virtual A/B
+AB_OTA_UPDATER := true
+AB_OTA_PARTITIONS += \
+    product \
+    system \
+    system_ext \
+    vendor
+
+$(call inherit-product, $(SRC_TARGET_DIR)/product/virtual_ab_ota/launch_with_vendor_ramdisk.mk)
+
+PRODUCT_COPY_FILES := \
+    $(RB5_KERNEL_DIR)/Image.gz:kernel \
+    $(RB5_KERNEL_DIR)/qrb5165-rb5.dtb:dtb.img \
+    $(LOCAL_PATH)/mixer_paths.xml:$(TARGET_COPY_OUT_VENDOR)/etc/mixer_paths.xml \
+    device/linaro/dragonboard/fstab.common:$(TARGET_COPY_OUT_RAMDISK)/fstab.rb5 \
+    device/linaro/dragonboard/fstab.common:$(TARGET_COPY_OUT_VENDOR)/etc/fstab.rb5 \
+    device/linaro/dragonboard/init.common.rc:$(TARGET_COPY_OUT_VENDOR)/etc/init/init.rb5.rc \
+    device/linaro/dragonboard/init.common.usb.rc:$(TARGET_COPY_OUT_VENDOR)/etc/init/init.rb5.usb.rc \
+    device/linaro/dragonboard/common.kl:$(TARGET_COPY_OUT_VENDOR)/usr/keylayout/rb5.kl
+
+# Build generic Audio HAL
+PRODUCT_PACKAGES := audio.primary.rb5
+
+# BootControl HAL
+PRODUCT_PACKAGES += \
+    android.hardware.boot@1.2-impl \
+    android.hardware.boot@1.2-impl.recovery \
+    android.hardware.boot@1.2-service
+
+PRODUCT_PACKAGES += \
+    pd-mapper \
+    qrtr-ns \
+    qrtr-cfg \
+    qrtr-lookup \
+    rmtfs \
+    tqftpserv
+
+PRODUCT_COPY_FILES += \
+    device/linaro/dragonboard/qcom/init.qcom.rc:$(TARGET_COPY_OUT_VENDOR)/etc/init/init.qcom.rc
+
+PRODUCT_VENDOR_PROPERTIES += ro.soc.manufacturer=Qualcomm
+PRODUCT_VENDOR_PROPERTIES += ro.soc.model=QRB5165
+
+PRODUCT_PROPERTY_OVERRIDES += ro.sf.lcd_density=160
+
+# Copy firmware files
+$(call inherit-product-if-exists, $(LOCAL_PATH)/firmware/device.mk)
diff --git a/rb5/mixer_paths.xml b/rb5/mixer_paths.xml
new file mode 100644
index 0000000..86e67fc
--- /dev/null
+++ b/rb5/mixer_paths.xml
@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="ISO-8859-1"?>
+<mixer>
+	<!-- Enable HDMI out -->
+	<ctl name="TERT_MI2S_RX Audio Mixer MultiMedia1" value="1" />
+</mixer>
diff --git a/sepolicy/file_contexts b/sepolicy/file_contexts
index 52f8668..1022689 100644
--- a/sepolicy/file_contexts
+++ b/sepolicy/file_contexts
@@ -13,11 +13,15 @@
 /dev/ttyMSM0				u:object_r:console_device:s0
 
 /sys/bus/platform/drivers/qcom-q6v5-mss							u:object_r:sysfs_mss:s0
+/sys/module/qcom_q6v5_mss								u:object_r:sysfs_mss:s0
 /sys/devices/platform/88f00000.memory/rmtfs						u:object_r:sysfs_rmtfs:s0
 
 /sys/devices/platform/soc@0/4080000.remoteproc						u:object_r:sysfs_remoteproc:s0
+/sys/devices/platform/soc@0/8300000.remoteproc						u:object_r:sysfs_remoteproc:s0
+/sys/devices/platform/soc@0/17300000.remoteproc						u:object_r:sysfs_remoteproc:s0
 /sys/devices/platform/soc@0/ae00000.mdss						u:object_r:sysfs_gpu:s0
 
+#wakeups on db845c
 /sys/devices/platform/soc@0/1c00000.pci/pci0000:00/0000:00:00.0/0000:01:00.0/wakeup/wakeup1						u:object_r:sysfs_wakeup:s0
 /sys/devices/platform/soc@0/a6f8800.usb/wakeup/wakeup2											u:object_r:sysfs_wakeup:s0
 /sys/devices/platform/soc@0/a8f8800.usb/wakeup/wakeup3											u:object_r:sysfs_wakeup:s0
@@ -26,6 +30,12 @@
 /sys/devices/platform/soc@0/c440000.spmi/spmi-0/0-00/c440000.spmi:pmic@0:rtc@6000/wakeup/wakeup6					u:object_r:sysfs_wakeup:s0
 /sys/devices/platform/soc@0/c440000.spmi/spmi-0/0-00/c440000.spmi:pmic@0:rtc@6000/rtc/rtc0/alarmtimer.2.auto/wakeup/wakeup7		u:object_r:sysfs_wakeup:s0
 
+#wakeups on RB5
+/sys/devices/platform/soc@0/a6f8800.usb/wakeup/wakeup0											u:object_r:sysfs_wakeup:s0
+/sys/devices/platform/soc@0/a8f8800.usb/wakeup/wakeup1											u:object_r:sysfs_wakeup:s0
+/sys/devices/platform/soc@0/c440000.spmi/spmi-0/0-00/c440000.spmi:pmic@0:rtc@6000/wakeup/wakeup3					u:object_r:sysfs_wakeup:s0
+/sys/devices/platform/soc@0/c440000.spmi/spmi-0/0-00/c440000.spmi:pmic@0:rtc@6000/rtc/rtc0/alarmtimer.1.auto/wakeup/wakeup4		u:object_r:sysfs_wakeup:s0
+
 /sys/class/remoteproc									u:object_r:sysfs_remoteproc:s0
 /sys/devices/platform/remoteproc-adsp/remoteproc					u:object_r:sysfs_remoteproc:s0
 /sys/devices/platform/remoteproc-cdsp/remoteproc					u:object_r:sysfs_remoteproc:s0
diff --git a/sepolicy/genfs_contexts b/sepolicy/genfs_contexts
index 8f18ea7..95ce0cc 100644
--- a/sepolicy/genfs_contexts
+++ b/sepolicy/genfs_contexts
@@ -2,8 +2,12 @@
 genfscon sysfs   /devices/platform/remoteproc-adsp/remoteproc					u:object_r:sysfs_remoteproc:s0
 genfscon sysfs   /devices/platform/remoteproc-cdsp/remoteproc					u:object_r:sysfs_remoteproc:s0
 genfscon sysfs   /devices/platform/soc@0/4080000.remoteproc					u:object_r:sysfs_remoteproc:s0
+genfscon sysfs   /devices/platform/soc@0/8300000.remoteproc					u:object_r:sysfs_remoteproc:s0
+genfscon sysfs   /devices/platform/soc@0/17300000.remoteproc					u:object_r:sysfs_remoteproc:s0
 genfscon sysfs   /devices/platform/soc@0/ae00000.mdss						u:object_r:sysfs_gpu:s0
 genfscon sysfs   /class/udc									u:object_r:sysfs_udc:s0
+
+#wakeups on db845c
 genfscon sysfs   /devices/platform/soc@0/1c00000.pci/pci0000:00/0000:00:00.0/0000:01:00.0/wakeup/wakeup1					u:object_r:sysfs_wakeup:s0
 genfscon sysfs   /devices/platform/soc@0/a6f8800.usb/wakeup/wakeup2										u:object_r:sysfs_wakeup:s0
 genfscon sysfs   /devices/platform/soc@0/a8f8800.usb/wakeup/wakeup3										u:object_r:sysfs_wakeup:s0
@@ -11,3 +15,9 @@
 genfscon sysfs   /devices/platform/soc@0/c440000.spmi/spmi-0/0-00/c440000.spmi:pmic@0:pon@800/c440000.spmi:pmic@0:pon@800:resin/wakeup/wakeup5	u:object_r:sysfs_wakeup:s0
 genfscon sysfs   /devices/platform/soc@0/c440000.spmi/spmi-0/0-00/c440000.spmi:pmic@0:rtc@6000/wakeup/wakeup6					u:object_r:sysfs_wakeup:s0
 genfscon sysfs   /devices/platform/soc@0/c440000.spmi/spmi-0/0-00/c440000.spmi:pmic@0:rtc@6000/rtc/rtc0/alarmtimer.2.auto/wakeup/wakeup7	u:object_r:sysfs_wakeup:s0
+
+#wakeups on RB5
+genfscon sysfs   /devices/platform/soc@0/a6f8800.usb/wakeup/wakeup0										u:object_r:sysfs_wakeup:s0
+genfscon sysfs   /devices/platform/soc@0/a8f8800.usb/wakeup/wakeup1										u:object_r:sysfs_wakeup:s0
+genfscon sysfs   /devices/platform/soc@0/c440000.spmi/spmi-0/0-00/c440000.spmi:pmic@0:rtc@6000/wakeup/wakeup3					u:object_r:sysfs_wakeup:s0
+genfscon sysfs   /devices/platform/soc@0/c440000.spmi/spmi-0/0-00/c440000.spmi:pmic@0:rtc@6000/rtc/rtc0/alarmtimer.1.auto/wakeup/wakeup4	u:object_r:sysfs_wakeup:s0