Merge remote-tracking branch master into MHB

* master:
  app: aboot: move display shutdown before uninit
  platform: msm8996: Add QUSB ref clock
  platform: msm_shared: Update SIGDET_LVL for qmp phy
  platform: msm_shared: Update phy sequence for qusb
  platform: msm_shared: Add support for pmic type API
  dev: pmi8994: Add support to read battery voltage from sram
  app: aboot: Add support for tests framework

Change-Id: I78dd0fb4e2006328dbf1449f102dca779d60f164
diff --git a/app/aboot/aboot.c b/app/aboot/aboot.c
index 4911344..5067181 100644
--- a/app/aboot/aboot.c
+++ b/app/aboot/aboot.c
@@ -92,6 +92,7 @@
 #include "secapp_loader.h"
 #include <menu_keys_detect.h>
 #include <display_menu.h>
+#include "fastboot_test.h"
 
 extern  bool target_use_signed_kernel(void);
 extern void platform_uninit(void);
@@ -192,6 +193,7 @@
 static char *target_boot_params = NULL;
 static bool boot_reason_alarm;
 static bool devinfo_present = true;
+bool boot_into_fastboot = false;
 
 /* Assuming unauthorized kernel image by default */
 static int auth_kernel_img = 0;
@@ -1108,7 +1110,11 @@
 		device.is_unlocked,
 		device.is_tampered);
 
-	if(target_use_signed_kernel() && (!device.is_unlocked))
+	/* Change the condition a little bit to include the test framework support.
+	 * We would never reach this point if device is in fastboot mode, even if we did
+	 * that means we are in test mode, so execute kernel authentication part for the
+	 * tests */
+	if((target_use_signed_kernel() && (!device.is_unlocked)) || boot_into_fastboot)
 	{
 		offset = imagesize_actual;
 		if (check_aboot_addr_range_overlap((uint32_t)image_addr + offset, page_size))
@@ -1125,6 +1131,9 @@
 		}
 
 		verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
+		/* The purpose of our test is done here */
+		if (boot_into_fastboot && auth_kernel_img)
+			return 0;
 	} else {
 		second_actual  = ROUND_TO_PAGE(hdr->second_size,  page_mask);
 		#ifdef TZ_SAVE_KERNEL_HASH
@@ -3439,6 +3448,9 @@
 						{"oem disable-charger-screen", cmd_oem_disable_charger_screen},
 						{"oem off-mode-charge", cmd_oem_off_mode_charger},
 						{"oem select-display-panel", cmd_oem_select_display_panel},
+#if UNITTEST_FW_SUPPORT
+						{"oem run-tests", cmd_oem_runtests},
+#endif
 #endif
 						};
 
@@ -3492,7 +3504,6 @@
 void aboot_init(const struct app_descriptor *app)
 {
 	unsigned reboot_mode = 0;
-	bool boot_into_fastboot = false;
 
 	/* Setup page size information for nv storage */
 	if (target_is_emmc_boot())
diff --git a/app/aboot/fastboot_test.c b/app/aboot/fastboot_test.c
new file mode 100644
index 0000000..f32435c
--- /dev/null
+++ b/app/aboot/fastboot_test.c
@@ -0,0 +1,92 @@
+/* Copyright (c) 2015, The Linux Foundation. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are
+met:
+    * Redistributions of source code must retain the above copyright
+      notice, this list of conditions and the following disclaimer.
+    * Redistributions in binary form must reproduce the above
+      copyright notice, this list of conditions and the following
+      disclaimer in the documentation and/or other materials provided
+      with the distribution.
+    * Neither the name of The Linux Foundation nor the names of its
+      contributors may be used to endorse or promote products derived
+      from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+
+#include <stdlib.h>
+#include <debug.h>
+#include "devinfo.h"
+#include "fastboot.h"
+#include "fastboot_test.h"
+#include <app/tests.h>
+#if USE_RPMB_FOR_DEVINFO
+#include <rpmb.h>
+#endif
+/* main function that calls into the tests */
+
+extern void ramdump_table_map();
+extern void kauth_test();
+
+void cmd_oem_runtests()
+{
+	dprintf(INFO, "Running LK tests ... \n");
+#if LPAE
+	ramdump_table_map();
+#endif
+
+#if USE_RPMB_FOR_DEVINFO
+	dprintf(INFO, "Running RPMB test case, please make sure RPMB key is provisioned ...\n");
+	struct device_info *write_info = memalign(PAGE_SIZE, 4096);
+	struct device_info *read_info = memalign(PAGE_SIZE, 4096);
+	if((read_device_info_rpmb((void*) read_info, PAGE_SIZE)) < 0)
+		dprintf(INFO, "RPMB READ TEST : [ FAIL ]\n");
+
+	write_info->is_unlocked = !read_info->is_unlocked;
+
+	if((write_device_info_rpmb((void*) write_info, PAGE_SIZE)) < 0)
+		dprintf(INFO, "RPMB WRITE TEST : [ FAIL ]\n");
+
+	if((read_device_info_rpmb((void*) read_info, PAGE_SIZE)) < 0)
+		dprintf(INFO, "RPMB READ TEST : [ FAIL ]\n");
+
+	if (read_info->is_unlocked == write_info->is_unlocked)
+		dprintf(INFO, "RPMB READ/WRITE TEST: [ PASS ]\n");
+	else
+		dprintf(INFO, "RPMB READ/WRITE TEST: [ FAIL ]\n");
+
+	free(read_info);
+	free(write_info);
+#endif
+
+#if VERIFIED_BOOT
+	if (!boot_linux_from_mmc())
+		dprintf(INFO, "Verifid Boot authentication test: [ PASS ]\n");
+	else
+		dprintf(INFO, "Verifid Boot authentication test: [ FAIL ]\n");
+
+	kauth_test();
+#endif
+
+	if (!thread_tests())
+		dprintf(INFO, "Thread Test: [ PASS ]\n");
+	else
+		dprintf(INFO, "Thread Test: [ FAIL ]\n");
+
+	printf_tests();
+
+	fastboot_okay("");
+}
diff --git a/app/aboot/fastboot_test.h b/app/aboot/fastboot_test.h
new file mode 100644
index 0000000..005bfa7
--- /dev/null
+++ b/app/aboot/fastboot_test.h
@@ -0,0 +1,36 @@
+/* Copyright (c) 2015, The Linux Foundation. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are
+met:
+    * Redistributions of source code must retain the above copyright
+      notice, this list of conditions and the following disclaimer.
+    * Redistributions in binary form must reproduce the above
+      copyright notice, this list of conditions and the following
+      disclaimer in the documentation and/or other materials provided
+      with the distribution.
+    * Neither the name of The Linux Foundation nor the names of its
+      contributors may be used to endorse or promote products derived
+      from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+
+#ifndef __FASTBOOT_TEST_H__
+#define __FASTBOOT_TEST_H__
+#include <sys/types.h>
+extern void ramdump_table_map();
+void cmd_oem_runtests();
+extern int boot_linux_from_mmc();
+#endif
diff --git a/app/aboot/rules.mk b/app/aboot/rules.mk
index 24bca43..09a5152 100644
--- a/app/aboot/rules.mk
+++ b/app/aboot/rules.mk
@@ -11,6 +11,11 @@
 	$(LOCAL_DIR)/fastboot.o \
 	$(LOCAL_DIR)/recovery.o
 
+ifeq ($(ENABLE_UNITTEST_FW), 1)
+OBJS += \
+	$(LOCAL_DIR)/fastboot_test.o
+endif
+
 ifeq ($(ENABLE_MDTP_SUPPORT),1)
 OBJS += \
 	$(LOCAL_DIR)/mdtp.o \
diff --git a/app/mmutest/mmu_test.c b/app/mmutest/mmu_test.c
index 849a9e6..27ed628 100644
--- a/app/mmutest/mmu_test.c
+++ b/app/mmutest/mmu_test.c
@@ -29,6 +29,7 @@
 #include <sys/types.h>
 #include <debug.h>
 #include <arch/arm/mmu.h>
+#include <arch/ops.h>
 #include <mmu.h>
 #include <string.h>
 #include <smem.h>
diff --git a/app/mmutest/rules.mk b/app/mmutest/rules.mk
index 78918a1..5094f65 100644
--- a/app/mmutest/rules.mk
+++ b/app/mmutest/rules.mk
@@ -1,6 +1,6 @@
 LOCAL_DIR := $(GET_LOCAL_DIR)
 
-INCLUDES += -I$(LK_TOP_DIR)/platform/msm_shared/include
+INCLUDES += -I$(LOCAL_DIR)/include  -I$(LK_TOP_DIR)/platform/msm_shared/include
 
 OBJS += \
 	$(LOCAL_DIR)/mmu_test.o
diff --git a/app/rules.mk b/app/rules.mk
index e564fe0..0c06ce6 100644
--- a/app/rules.mk
+++ b/app/rules.mk
@@ -3,6 +3,11 @@
 MODULES += \
 	lib/openssl
 
+ifeq ($(ENABLE_UNITTEST_FW), 1)
+MODULES += \
+	app/tests
+endif
+
 OBJS += \
 	$(LOCAL_DIR)/app.o
 
diff --git a/app/tests/adc_tests.c b/app/tests/adc_tests.c
deleted file mode 100644
index 5f2d511..0000000
--- a/app/tests/adc_tests.c
+++ /dev/null
@@ -1,61 +0,0 @@
-/*
- * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are
- * met:
- *     * Redistributions of source code must retain the above copyright
- *       notice, this list of conditions and the following disclaimer.
- *     * Redistributions in binary form must reproduce the above
- *       copyright notice, this list of conditions and the following
- *       disclaimer in the documentation and/or other materials provided
- *       with the distribution.
- *     * Neither the name of The Linux Foundation, Inc. nor the names of its
- *       contributors may be used to endorse or promote products derived
- *       from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
- * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
- * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
- * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
- * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
- * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#include <pm8x41_adc.h>
-#include <debug.h>
-
-void adc_test()
-{
-	uint32_t vadc_chan1;
-	uint32_t vadc_chan2;
-	uint16_t iusb = 3250000;
-	uint16_t ibat = 2500000;
-	uint16_t batt_id_chan = 49;
-	uint16_t vbat_sns_chan = 6;
-
-	/*
-	 * TEST: Read the voltage on batt_id & vbat_sns channels
-	 */
-	vadc_chan1 = pm8x41_adc_channel_read(batt_id_chan);
-	dprintf(INFO, "The channel [%ud] voltage is :%ud\n",batt_id_chan, vadc_chan1);
-
-	vadc_chan2 = pm8x41_adc_channel_read(vbat_sns_chan);
-	dprintf(INFO, "The channel [%ud] voltage is :%ud\n",vbat_sns_chan, vadc_chan2);
-
-	/*
-	 * TEST: Set the IUSB & IBAT max values
-	 */
-
-	if (!pm8x41_iusb_max_config(iusb))
-		dprintf(INFO, "Iusb max current is set\n");
-
-	if (!pm8x41_ibat_max_config(ibat))
-		dprintf(INFO, "Ibat max current is set\n");
-
-}
diff --git a/app/tests/led_tests.c b/app/tests/led_tests.c
deleted file mode 100644
index 11ef9f2..0000000
--- a/app/tests/led_tests.c
+++ /dev/null
@@ -1,72 +0,0 @@
-/*
- * Copyright (c) 2011, The Linux Foundation. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are
- * met:
- *     * Redistributions of source code must retain the above copyright
- *       notice, this list of conditions and the following disclaimer.
- *     * Redistributions in binary form must reproduce the above
- *       copyright notice, this list of conditions and the following
- *       disclaimer in the documentation and/or other materials provided
- *       with the distribution.
- *     * Neither the name of The Linux Foundation nor the names of its
- *       contributors may be used to endorse or promote products derived
- *       from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
- * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
- * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
- * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
- * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
- * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#include<dev/pm8921_leds.h>
-#include<dev/pm8921.h>
-
-void led_tests()
-{
-uint32_t duty_us, period_us;
-
-    /* 50% Duty cycle */
-    duty_us = 500000;
-    period_us = 1000000;
-
-    /* Configure PM8921_ID_LED_0 from PWM2*/
-    pm8921_config_led_current(PM8921_ID_LED_0, 2, PWM2, 1);
-
-    /* PWM2 for PM8921_ID_LED_0 is LPG 5
-     * Configure and enable lpg5
-     */
-    pm_set_pwm_config(5, duty_us, period_us, &pmic);
-    pm_pwm_channel_enable(5, &pmic);
-
-    /* Configure and enable lpg0 for  panel backlight*/
-    pm_set_pwm_config(0, duty_us, period_us, &pmic);
-    pm_pwm_channel_enable(0, &pmic);
-
-    mdelay(10000);
-
-    /* Configure PM8921_ID_LED_1 also from PWM2*/
-    pm8921_config_led_current(PM8921_ID_LED_1, 2, PWM2, 1);
-    mdelay(10000);
-
-    /* Disable PM8921_ID_LED_0 */
-    pm8921_config_led_current(PM8921_ID_LED_0, 2, 2, 0);
-
-    /* Turn on GPIO 24 through LPG 0
-     * Will be reconfigured during display_init
-     */
-    panel_backlight_on_pwm();
-
-    mdelay(10000);
-
-    /* Disable PM8921_ID_LED_1 */
-    pm8921_config_led_current(PM8921_ID_LED_1, 2, 2, 0);
-}
diff --git a/app/tests/rules.mk b/app/tests/rules.mk
index f8151ac..ec79a7f 100644
--- a/app/tests/rules.mk
+++ b/app/tests/rules.mk
@@ -5,7 +5,9 @@
 OBJS += \
 	$(LOCAL_DIR)/tests.o \
 	$(LOCAL_DIR)/thread_tests.o \
-	$(LOCAL_DIR)/printf_tests.o \
-	$(LOCAL_DIR)/i2c_tests.o \
-	$(LOCAL_DIR)/adc_tests.o \
+	$(LOCAL_DIR)/printf_tests.o
+
+ifeq ($(VERIFIED_BOOT),1)
+OBJS += \
 	$(LOCAL_DIR)/kauth_test.o
+endif
diff --git a/dev/pmic/pmi8994/pm_app_smbchg.c b/dev/pmic/pmi8994/pm_app_smbchg.c
index 4b9693b..c3a03be 100644
--- a/dev/pmic/pmi8994/pm_app_smbchg.c
+++ b/dev/pmic/pmi8994/pm_app_smbchg.c
@@ -46,6 +46,9 @@
 #include <sys/types.h>
 #include <target.h>
 #include <pm8x41.h>
+#include <bits.h>
+#include <board.h>
+#include <smem.h>
 
 /*===========================================================================
 
@@ -80,13 +83,15 @@
 static bool display_initialized;
 static bool charge_in_progress;
 static bool display_shutdown_in_prgs;
+static bool pm_app_read_from_sram;
 
 char panel_name[256];
 
 pm_err_flag_type pm_smbchg_get_charger_path(uint32 device_index, pm_smbchg_usb_chgpth_pwr_pth_type* charger_path);
 pm_err_flag_type pm_appsbl_chg_config_vbat_low_threshold(uint32 device_index, pm_smbchg_specific_data_type *chg_param_ptr);
 static void display_thread_initialize();
-
+static void pm_app_ima_read_voltage(uint32_t *);
+static void pm_app_pmi8994_read_voltage(uint32_t *voltage);
 /*===========================================================================
 
                      FUNCTION IMPLEMENTATION
@@ -179,6 +184,20 @@
          err_flag |= pm_fg_adc_usr_get_calibrated_vbat(device_index, &vbat_adc); //Read calibrated vbatt ADC
          if ( err_flag != PM_ERR_FLAG__SUCCESS )  { break;}
 
+		/* FG_ADC hardware reports values that are off by ~120 to 200 mV, this results in boot up failures
+		 * on devices that boot up with battery close to threshold value. If the FG_ADC voltage is less than
+		 * threshold then read the voltage from a more accurate source FG SRAM to ascertain the voltage is indeed low.
+		 */
+		if (!pm_app_read_from_sram && (vbat_adc <= bootup_threshold))
+		{
+			if (board_pmic_type(PMIC_IS_PMI8996))
+				pm_app_ima_read_voltage(&vbat_adc);
+			else
+				pm_app_pmi8994_read_voltage(&vbat_adc);
+
+			pm_app_read_from_sram = true;
+		}
+
          //Check if ADC reading is within limit
          if ( vbat_adc >=  bootup_threshold)  //Compaire it with SW bootup threshold
          {
@@ -674,3 +693,160 @@
 		is_thread_start = true;
 	}
 }
+
+static void pm_app_wait_for_iacs_ready(uint32_t sid)
+{
+	uint8_t iacs;
+	int max_retry = 100;
+
+	udelay(50);
+	pm_comm_read_byte(sid, 0x4454, &iacs, 0);
+	while ((iacs & 0x02) == 0)
+	{
+		max_retry--;
+		pm_comm_read_byte(2, 0x4454, &iacs, 0);
+		mdelay(5);
+		if (!max_retry)
+		{
+			dprintf(CRITICAL, "Error: IACS not ready, shutting down\n");
+			shutdown_device();
+		}
+	}
+}
+
+static int pm_app_check_for_ima_exception(uint32_t sid)
+{
+	uint8_t ima_err_sts;
+	uint8_t ima_exception_sts;
+
+	pm_comm_read_byte(sid, 0x445f, &ima_err_sts, 0);
+	pm_comm_read_byte(sid, 0x4455, &ima_exception_sts, 0);
+
+	if (ima_err_sts != 0 || (ima_exception_sts & 0x1) == 1)
+	{
+		uint8_t ima_hw_sts;
+		pm_comm_read_byte(sid, 0x4456, &ima_hw_sts, 0);
+		dprintf(CRITICAL, "ima_err_sts: %x\tima_exception_sts:%x\tima_hw_sts:%x\n", ima_err_sts, ima_exception_sts, ima_hw_sts);
+		return 1;
+	}
+
+	return 0;
+}
+
+static void pm_app_ima_read_voltage(uint32_t *voltage)
+{
+	uint8_t start_beat_count;
+	uint8_t end_beat_count;
+	uint8_t vbat;
+	uint64_t vbat_adc = 0;
+	uint32_t sid = 2; //sid for pmi8996
+	int max_retry = 5;
+
+retry:
+	//Request IMA access
+	pm_comm_write_byte(sid, 0x4450, 0xA0, 0);
+	// Single read configure
+	pm_comm_write_byte(sid, 0x4451, 0x00, 0);
+
+	pm_app_wait_for_iacs_ready(sid);
+
+	//configure SRAM access
+	pm_comm_write_byte(sid, 0x4461, 0xCC, 0);
+	pm_comm_write_byte(sid, 0x4462, 0x05, 0);
+
+	pm_app_wait_for_iacs_ready(sid);
+
+	pm_comm_read_byte(sid, 0x4457, &start_beat_count, 0);
+
+	//Read the voltage
+	pm_comm_read_byte(sid, 0x4467, &vbat, 0);
+	vbat_adc = vbat;
+	pm_comm_read_byte(sid, 0x4468, &vbat, 0);
+	vbat_adc |= (vbat << 8);
+	pm_comm_read_byte(sid, 0x4469, &vbat, 0);
+	vbat_adc |= (vbat << 16);
+	pm_comm_read_byte(sid, 0x446A, &vbat, 0);
+	vbat_adc |= (vbat << 24);
+
+	pm_app_wait_for_iacs_ready(sid);
+
+	//Look for any errors
+	if(pm_app_check_for_ima_exception(sid))
+		goto err;
+
+	pm_comm_read_byte(sid, 0x4457, &end_beat_count, 0);
+
+	if (start_beat_count != end_beat_count)
+	{
+		max_retry--;
+		if (!max_retry)
+			goto err;
+		goto retry;
+	}
+
+	//Release the ima access
+	pm_comm_write_byte(2, 0x4450, 0x00, 0);
+
+	//extract the byte1 & byte2 and convert to mv
+	vbat_adc = ((vbat_adc & 0x00ffff00) >> 8) * 152587;
+	*voltage = vbat_adc / 1000000;
+	return;
+
+err:
+	dprintf(CRITICAL, "Failed to Read the Voltage from IMA, shutting down\n");
+	shutdown_device();
+}
+
+static void pm_app_pmi8994_read_voltage(uint32_t *voltage)
+{
+	uint8_t val = 0;
+	uint8_t vbat;
+	uint64_t vbat_adc = 0;
+	uint32_t sid = 2; //sid for pmi8994
+	int max_retry = 100;
+
+	pm_comm_read_byte(sid, 0x4440, &val, 0);
+
+	//Request for FG access
+	if ((val & BIT(7)) != 1)
+		pm_comm_write_byte(sid, 0x4440, 0x80, 0);
+
+	pm_comm_read_byte(sid, 0x4410, &val, 0);
+	while((val & 0x1) == 0)
+	{
+		//sleep and retry again, this takes up to 1.5 seconds
+		max_retry--;
+		mdelay(100);
+		pm_comm_read_byte(sid, 0x4410, &val, 0);
+		if (!max_retry)
+		{
+		  dprintf(CRITICAL, "Error: Failed to read from Fuel Guage, Shutting down\n");
+		  shutdown_device();
+		}
+	}
+
+	//configure single read access
+	pm_comm_write_byte(sid, 0x4441, 0x00, 0);
+	//configure SRAM for voltage shadow
+	pm_comm_write_byte(sid, 0x4442, 0xCC, 0);
+	pm_comm_write_byte(sid, 0x4443, 0x05, 0);
+
+	//Read voltage from SRAM
+	pm_comm_read_byte(sid, 0x444c, &vbat, 0);
+	vbat_adc = vbat;
+	pm_comm_read_byte(sid, 0x444d, &vbat, 0);
+	vbat_adc |= (vbat << 8);
+	pm_comm_read_byte(sid, 0x444e, &vbat, 0);
+	vbat_adc |= (vbat << 16);
+	pm_comm_read_byte(sid, 0x444f, &vbat, 0);
+	vbat_adc |= (vbat << 24);
+
+	//clean up to relase sram access
+	pm_comm_write_byte(sid, 0x4440, 0x00, 0);
+	//extract byte 1 & byte 2
+	vbat_adc = ((vbat_adc & 0x00ffff00) >> 8) * 152587;
+
+	//convert the voltage to mv
+	*voltage = vbat_adc / 1000000;
+}
+
diff --git a/platform/msm8996/include/platform/iomap.h b/platform/msm8996/include/platform/iomap.h
index 8aa0fab..2a31c8f 100644
--- a/platform/msm8996/include/platform/iomap.h
+++ b/platform/msm8996/include/platform/iomap.h
@@ -80,6 +80,7 @@
 
 #define AHB2_PHY_BASE               0x7416000
 #define PERIPH_SS_AHB2PHY_TOP_CFG   (AHB2_PHY_BASE + 0x10)
+#define GCC_RX2_USB2_CLKREF_EN      0x00388014
 
 /* Clocks */
 #define CLK_CTL_BASE                0x300000
diff --git a/platform/msm_shared/board.c b/platform/msm_shared/board.c
index 1265f29..8839a8a 100644
--- a/platform/msm_shared/board.c
+++ b/platform/msm_shared/board.c
@@ -400,6 +400,21 @@
 	return 0;
 }
 
+bool board_pmic_type(uint32_t type)
+{
+	uint8_t i;
+	if (format_major == 0x0 && format_minor >= 0x0B)
+	{
+		for (i = 0; i < SMEM_MAX_PMIC_DEVICES; i++)
+		{
+			if (type == (board.pmic_info_array[i].pmic_type & 0x0000ffff))
+				return true;
+		}
+	}
+
+	return false;
+}
+
 uint32_t board_pmic_target(uint8_t num_ent)
 {
 	if (format_major == 0x0 && num_ent < SMEM_MAX_PMIC_DEVICES)
diff --git a/platform/msm_shared/include/board.h b/platform/msm_shared/include/board.h
index 4d4c7a9..9a5db02 100644
--- a/platform/msm_shared/include/board.h
+++ b/platform/msm_shared/include/board.h
@@ -90,5 +90,5 @@
 
 uint32_t board_foundry_id(void);
 void board_update_boot_dev(uint32_t);
-
+bool board_pmic_type(uint32_t type);
 #endif
diff --git a/platform/msm_shared/include/qusb2_phy.h b/platform/msm_shared/include/qusb2_phy.h
index 1d5199a..fc971c8 100644
--- a/platform/msm_shared/include/qusb2_phy.h
+++ b/platform/msm_shared/include/qusb2_phy.h
@@ -34,6 +34,7 @@
 
 #define QUSB2PHY_PORT_POWERDOWN     (QUSB2_PHY_BASE + 0x000000B4)
 #define QUSB2PHY_PORT_UTMI_CTRL2    (QUSB2_PHY_BASE + 0x000000C4)
+#define QUSB2PHY_PLL_TEST           (QUSB2_PHY_BASE + 0x00000004)
 #define QUSB2PHY_PLL_TUNE           (QUSB2_PHY_BASE + 0x00000008)
 #define QUSB2PHY_PLL_USER_CTL1      (QUSB2_PHY_BASE + 0x0000000C)
 #define QUSB2PHY_PLL_USER_CTL2      (QUSB2_PHY_BASE + 0x00000010)
@@ -42,6 +43,8 @@
 #define QUSB2PHY_PORT_TUNE3         (QUSB2_PHY_BASE + 0x00000088)
 #define QUSB2PHY_PORT_TUNE4         (QUSB2_PHY_BASE + 0x0000008C)
 #define QUSB2PHY_PORT_TEST2         (QUSB2_PHY_BASE + 0x0000009C)
+#define QUSB2PHY_PLL_PWR_CTL        (QUSB2_PHY_BASE + 0x00000018)
+#define QUSB2PHY_PLL_AUTOPGM_CTL1   (QUSB2_PHY_BASE + 0x0000001C)
 
 
 #endif
diff --git a/platform/msm_shared/qmp_usb30_phy.c b/platform/msm_shared/qmp_usb30_phy.c
index 91aed89..56b009a 100644
--- a/platform/msm_shared/qmp_usb30_phy.c
+++ b/platform/msm_shared/qmp_usb30_phy.c
@@ -107,7 +107,7 @@
 	{0x508, 0x77}, /* QSERDES_RX_RX_EQ_OFFSET_ADAPTOR_CNTRL1 */
 	{0x50c, 0x80}, /* QSERDES_RX_RX_OFFSET_ADAPTOR_CNTRL2 */
 	{0x514, 0x03}, /* QSERDES_RX_SIGDET_CNTRL */
-	{0x518, 0x1b}, /* QSERDES_RX_SIGDET_LVL */
+	{0x518, 0x18}, /* QSERDES_RX_SIGDET_LVL */
 	{0x51c, 0x16}, /* QSERDES_RX_SIGDET_DEGLITCH_CNTRL */
 
 	/* Tx settings */
@@ -135,6 +135,7 @@
 struct qmp_reg qmp_misc_settings_rev2[] =
 {
 	{0x178, 0x01}, /* QSERDES_COM_HSCLK_SEL */
+	{0x518, 0x1B}, /* QSERDES_RX_SIGDET_LVL */
 	{0xC4, 0x15}, /* USB3PHY_QSERDES_COM_RESCODE_DIV_NUM */
 	{0x1B8, 0x1F}, /* QSERDES_COM_CMN_MISC2 */
 };
diff --git a/platform/msm_shared/qusb2_phy.c b/platform/msm_shared/qusb2_phy.c
index 871bbcc..8981df7 100644
--- a/platform/msm_shared/qusb2_phy.c
+++ b/platform/msm_shared/qusb2_phy.c
@@ -77,12 +77,15 @@
 			tune2 = (tune2 & 0x0f) | (fuse_val << 4);
 #endif
 		writel(tune2, QUSB2PHY_PORT_TUNE2);
-		writel(0x93, QUSB2PHY_PORT_TUNE3);
+		writel(0x83, QUSB2PHY_PORT_TUNE3);
 		writel(0xC0, QUSB2PHY_PORT_TUNE4);
 		writel(0x30, QUSB2PHY_PLL_TUNE);
 		writel(0x79, QUSB2PHY_PLL_USER_CTL1);
 		writel(0x21, QUSB2PHY_PLL_USER_CTL2);
 		writel(0x14, QUSB2PHY_PORT_TEST2);
+		writel(0x80, QUSB2PHY_PLL_TEST);
+		writel(0x9F, QUSB2PHY_PLL_AUTOPGM_CTL1);
+		writel(0x00, QUSB2PHY_PLL_PWR_CTL);
 	}
 	else
 	{
diff --git a/platform/msm_shared/smem.h b/platform/msm_shared/smem.h
index 5e3ae4b..278a0eb 100644
--- a/platform/msm_shared/smem.h
+++ b/platform/msm_shared/smem.h
@@ -141,7 +141,6 @@
 	PMIC_IS_PMI8950   = 17,
 	PMIC_IS_PMI8994   = 10,
 	PMIC_IS_PMI8996   = 19,
-
 } pm_model_type_bfly;
 
 struct smem_board_info_v3 {
diff --git a/project/msm8996.mk b/project/msm8996.mk
index 6a3973f..4c5d6a1 100644
--- a/project/msm8996.mk
+++ b/project/msm8996.mk
@@ -103,3 +103,8 @@
 
 #enable battery voltage check
 DEFINES += CHECK_BAT_VOLTAGE=1
+# Enable unit test FW
+ENABLE_UNITTEST_FW=1
+ifeq ($(ENABLE_UNITTEST_FW),1)
+DEFINES += UNITTEST_FW_SUPPORT=1
+endif