[msm] Enable battery charging flag based on power-on status

Determine power-on reason (e.g. due to USB cable connection,
button press, etc.) and pass androidboot.battchg_pause to kernel.

Enable flag on msm7627_* targets when power-on due to USB
cable connection.  This will trigger a pause during bootup
until user keypad input.

Change-Id: I7751d9d7bdd0e735cd5cdeeedb952879cb237005
diff --git a/app/aboot/aboot.c b/app/aboot/aboot.c
index ccc88ad..06e0b9a 100644
--- a/app/aboot/aboot.c
+++ b/app/aboot/aboot.c
@@ -61,6 +61,7 @@
 #define FASTBOOT_MODE   0x77665500
 
 static const char *emmc_cmdline = " androidboot.emmc=true";
+static const char *battchg_pause = " androidboot.battchg_pause=true";
 
 static struct udc_device surf_udc_device = {
 	.vendor_id	= 0x18d1,
@@ -113,6 +114,7 @@
 	struct ptable *ptable;
 	int cmdline_len = 0;
 	int have_cmdline = 0;
+	int pause_at_bootup = 0;
 
 	/* CORE */
 	*ptr++ = 2;
@@ -152,6 +154,10 @@
 	if (target_is_emmc_boot()) {
 		cmdline_len += strlen(emmc_cmdline);
 	}
+	if (target_pause_for_battery_charge()) {
+		pause_at_bootup = 1;
+		cmdline_len += strlen(battchg_pause);
+	}
 	if (cmdline_len > 0) {
 		const char *src;
 		char *dst;
@@ -168,6 +174,12 @@
 		if (target_is_emmc_boot()) {
 			src = emmc_cmdline;
 			if (have_cmdline) --dst;
+			have_cmdline = 1;
+			while ((*dst++ = *src++));
+		}
+		if (pause_at_bootup) {
+			src = battchg_pause;
+			if (have_cmdline) --dst;
 			while ((*dst++ = *src++));
 		}
 		ptr += (n / 4);
diff --git a/platform/msm_shared/smem.h b/platform/msm_shared/smem.h
index 73e465e..8fe0f13 100644
--- a/platform/msm_shared/smem.h
+++ b/platform/msm_shared/smem.h
@@ -93,6 +93,8 @@
 
 	SMEM_USABLE_RAM_PARTITION_TABLE = 402,
 
+	SMEM_POWER_ON_STATUS_INFO = 403,
+
 	SMEM_FIRST_VALID_TYPE = SMEM_SPINLOCK_ARRAY,
 	SMEM_LAST_VALID_TYPE = SMEM_USABLE_RAM_PARTITION_TABLE,
 } smem_mem_type_t;
@@ -157,6 +159,7 @@
 	unsigned buf;
 } __attribute__ ((__packed__));
 
-
+/* Power on reason/status info */
+#define PWR_ON_EVENT_USB_CHG 0x20
 
 #endif /* __PLATFORM_MSM_SHARED_SMEM_H */
diff --git a/target/init.c b/target/init.c
index 73fb2b3..a65a6ec 100644
--- a/target/init.c
+++ b/target/init.c
@@ -65,3 +65,8 @@
                  (unsigned enable, unsigned disconnect)
 {
 }
+
+__WEAK unsigned target_pause_for_battery_charge(void)
+{
+    return 0;
+}
diff --git a/target/msm7627_ffa/init.c b/target/msm7627_ffa/init.c
index f54a3a9..fd7295b 100644
--- a/target/msm7627_ffa/init.c
+++ b/target/msm7627_ffa/init.c
@@ -197,6 +197,29 @@
     return mode[0];
 }
 
+static unsigned target_check_power_on_reason(void)
+{
+    unsigned power_on_status = 0;
+    unsigned int status_len = sizeof(power_on_status);
+    unsigned smem_status;
+
+    smem_status = smem_read_alloc_entry(SMEM_POWER_ON_STATUS_INFO,
+                                        &power_on_status, status_len);
+    if (!smem_status)
+    {
+        dprintf(CRITICAL, "ERROR: unable to read shared memory for power on reason\n");
+    }
+
+    return power_on_status;
+}
+
+unsigned target_pause_for_battery_charge(void)
+{
+    if (target_check_power_on_reason() == PWR_ON_EVENT_USB_CHG)
+        return 1;
+    return 0;
+}
+
 void target_battery_charging_enable(unsigned enable, unsigned disconnect)
 {
 }
diff --git a/target/msm7627_surf/init.c b/target/msm7627_surf/init.c
index 7089e0c..77512f1 100644
--- a/target/msm7627_surf/init.c
+++ b/target/msm7627_surf/init.c
@@ -197,6 +197,29 @@
     return mode[0];
 }
 
+static unsigned target_check_power_on_reason(void)
+{
+    unsigned power_on_status = 0;
+    unsigned int status_len = sizeof(power_on_status);
+    unsigned smem_status;
+
+    smem_status = smem_read_alloc_entry(SMEM_POWER_ON_STATUS_INFO,
+                                        &power_on_status, status_len);
+    if (!smem_status)
+    {
+        dprintf(CRITICAL, "ERROR: unable to read shared memory for power on reason\n");
+    }
+
+    return power_on_status;
+}
+
+unsigned target_pause_for_battery_charge(void)
+{
+    if (target_check_power_on_reason() == PWR_ON_EVENT_USB_CHG)
+        return 1;
+    return 0;
+}
+
 void target_battery_charging_enable(unsigned enable, unsigned disconnect)
 {
 }