platform: msm_shared: integrate dload & normal reboot reason in a function

Integrate dload & normal reboot reason in a function, so that it could
avoid the normal reboot reason macro collision with dload mode.

Change-Id: Icc7c9d465e6c993d71e84bd793c5650bf51a4d28
diff --git a/platform/msm_shared/debug.c b/platform/msm_shared/debug.c
index b8c3266..7c5a9b1 100644
--- a/platform/msm_shared/debug.c
+++ b/platform/msm_shared/debug.c
@@ -1,7 +1,7 @@
 /*
  * Copyright (c) 2009, Google Inc.
  * All rights reserved.
- * Copyright (c) 2009-2015, The Linux Foundation. All rights reserved.
+ * Copyright (c) 2009-2016, 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
@@ -151,16 +151,9 @@
 #if PON_VIB_SUPPORT
 	vib_turn_off();
 #endif
-	if (set_download_mode(NORMAL_DLOAD) == 0)
-	{
-		dprintf(CRITICAL, "HALT: reboot into dload mode...\n");
-		reboot_device(DLOAD);
-		dprintf(CRITICAL, "HALT: reboot_device failed\n");
-	}
-	else
-	{
-		dprintf(CRITICAL, "HALT: set_download_mode not supported\n");
-	}
+	dprintf(CRITICAL, "HALT: reboot into dload mode...\n");
+	reboot_device(NORMAL_DLOAD);
+
 	dprintf(CRITICAL, "HALT: spinning forever...\n");
 	for (;;) ;
 }
diff --git a/platform/msm_shared/dload_util.c b/platform/msm_shared/dload_util.c
index 7455a19..58f498a 100644
--- a/platform/msm_shared/dload_util.c
+++ b/platform/msm_shared/dload_util.c
@@ -1,4 +1,4 @@
-/* Copyright (c) 2013, The Linux Foundation. All rights reserved.
+/* Copyright (c) 2013, 2016, 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
@@ -38,7 +38,7 @@
 #define EMERGENCY_DLOAD_COOKIE_2    0x77777777
 
 void dload_util_write_cookie(uint32_t target_dload_mode_addr,
-		enum dload_mode mode)
+		enum reboot_reason mode)
 {
 	if (mode == NORMAL_DLOAD)
 	{
@@ -46,7 +46,7 @@
 		writel(NORMAL_DLOAD_COOKIE_1,
 				target_dload_mode_addr + sizeof(uint32_t));
 	}
-	else
+	else if (mode == EMERGENCY_DLOAD)
 	{
 		writel(EMERGENCY_DLOAD_COOKIE_0, target_dload_mode_addr);
 		writel(EMERGENCY_DLOAD_COOKIE_1,
diff --git a/platform/msm_shared/include/dload_util.h b/platform/msm_shared/include/dload_util.h
index d8e02b0..8f691e5 100644
--- a/platform/msm_shared/include/dload_util.h
+++ b/platform/msm_shared/include/dload_util.h
@@ -1,4 +1,4 @@
-/* Copyright (c) 2013, The Linux Foundation. All rights reserved.
+/* Copyright (c) 2013, 2016, 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
@@ -30,16 +30,9 @@
 #define __DLOAD_UTIL_H
 
 #include <sys/types.h>
-
-#define DLOAD 1
-
-enum dload_mode {
-	NORMAL_DLOAD,
-	EMERGENCY_DLOAD,
-	NORMAL_MODE
-};
+#include <reboot.h>
 
 void dload_util_write_cookie(uint32_t target_dload_mode_addr,
-		enum dload_mode mode);
+		enum reboot_reason mode);
 
 #endif
diff --git a/platform/msm_shared/include/scm.h b/platform/msm_shared/include/scm.h
index 2d454ed..8d81b29 100644
--- a/platform/msm_shared/include/scm.h
+++ b/platform/msm_shared/include/scm.h
@@ -1,4 +1,4 @@
-/* Copyright (c) 2011-2015, The Linux Foundation. All rights reserved.
+/* Copyright (c) 2011-2016, 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
@@ -29,6 +29,8 @@
 #ifndef __SCM_H__
 #define __SCM_H__
 
+#include <reboot.h>
+
 /* ARM SCM format support related flags */
 #define SIP_SVC_CALLS                          0x02000000
 #define MAKE_SIP_SCM_CMD(svc_id, cmd_id)       ((((svc_id << 8) | (cmd_id)) & 0xFFFF) | SIP_SVC_CALLS)
@@ -461,7 +463,7 @@
 /* Is armv8 supported */
 bool is_scm_armv8_support();
 
-int scm_dload_mode(int mode);
+int scm_dload_mode(enum reboot_reason mode);
 int scm_device_enter_dload();
 int scm_call2_atomic(uint32_t svc, uint32_t cmd, uint32_t arg1, uint32_t arg2);
 uint32_t scm_io_write(uint32_t address, uint32_t val);
diff --git a/platform/msm_shared/reboot.c b/platform/msm_shared/reboot.c
index 4c21d11..9071096 100644
--- a/platform/msm_shared/reboot.c
+++ b/platform/msm_shared/reboot.c
@@ -1,4 +1,4 @@
-/* Copyright (c) 2015, The Linux Foundation. All rights reserved.
+/* Copyright (c) 2015-2016, 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
@@ -76,7 +76,7 @@
 	hard_restart_reason = REG_READ(PON_SOFT_RB_SPARE);
 	REG_WRITE(PON_SOFT_RB_SPARE, hard_restart_reason & 0x03);
 
-	/* Extract the bits 5 to 7 and return */
+	/* Extract the bits 2 to 7 and return */
 	return (hard_restart_reason & 0xFC) >> 2;
 }
 
@@ -103,11 +103,11 @@
 	uint8_t value;
 #endif
 
-	/* Need to clear the SW_RESET_ENTRY register and
-	 * write to the BOOT_MISC_REG for known reset cases
-	 */
-	if(reboot_reason != DLOAD)
-		scm_dload_mode(NORMAL_MODE);
+	/* Set cookie for dload mode */
+	if(set_download_mode(reboot_reason)) {
+		dprintf(CRITICAL, "HALT: set_download_mode not supported\n");
+		return;
+	}
 
 #if USE_PON_REBOOT_REG
 	value = REG_READ(PON_SOFT_RB_SPARE);
@@ -120,9 +120,10 @@
 	 * For other cases do a hard reset
 	 */
 #if USE_PON_REBOOT_REG
-	if(reboot_reason == DLOAD)
+	if(reboot_reason == NORMAL_DLOAD || reboot_reason == EMERGENCY_DLOAD)
 #else
-	if(reboot_reason == FASTBOOT_MODE || (reboot_reason == DLOAD) || (reboot_reason == RECOVERY_MODE))
+	if(reboot_reason == FASTBOOT_MODE || reboot_reason == NORMAL_DLOAD ||
+		reboot_reason == EMERGENCY_DLOAD || reboot_reason == RECOVERY_MODE)
 #endif
 		reset_type = PON_PSHOLD_WARM_RESET;
 	else
diff --git a/platform/msm_shared/reboot.h b/platform/msm_shared/reboot.h
index 4773255..58085df 100644
--- a/platform/msm_shared/reboot.h
+++ b/platform/msm_shared/reboot.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, The Linux Foundation. All rights reserved.
+ * Copyright (c) 2015-2016, 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
@@ -28,21 +28,30 @@
  */
 #ifndef __REBOOT_H__
 #define __REBOOT_H__
+
+enum reboot_reason {
 #if USE_PON_REBOOT_REG
-#define RECOVERY_MODE     0x01
-#define FASTBOOT_MODE     0x02
-#define ALARM_BOOT        0x03
-#define DM_VERITY_LOGGING   0x04
-#define DM_VERITY_ENFORCING 0x05
-#define DM_VERITY_KEYSCLEAR 0x06
+	/* hard reset reason */
+	REBOOT_MODE_UNKNOWN	= 0x00,
+	RECOVERY_MODE		= 0x01,
+	FASTBOOT_MODE		= 0x02,
+	ALARM_BOOT		= 0x03,
+	DM_VERITY_LOGGING	= 0x04,
+	DM_VERITY_ENFORCING	= 0x05,
+	DM_VERITY_KEYSCLEAR	= 0x06,
 #else
-#define FASTBOOT_MODE     0x77665500
-#define RECOVERY_MODE     0x77665502
-#define ALARM_BOOT        0x77665503
-#define DM_VERITY_LOGGING    0x77665508
-#define DM_VERITY_ENFORCING  0x77665509
-#define DM_VERITY_KEYSCLEAR  0x7766550A
+	REBOOT_MODE_UNKNOWN	= 0x00,
+	RECOVERY_MODE		= 0x77665502,
+	FASTBOOT_MODE		= 0x77665500,
+	ALARM_BOOT		= 0x77665503,
+	DM_VERITY_LOGGING	= 0x77665508,
+	DM_VERITY_ENFORCING	= 0x77665509,
+	DM_VERITY_KEYSCLEAR	= 0x7766550A,
 #endif
+	/* warm reset start from  0xF0000000 */
+	NORMAL_DLOAD		= 0xF0000001,
+	EMERGENCY_DLOAD,
+};
 
 #define RTC_TRG           4
 #define PON_SOFT_RB_SPARE 0x88F
diff --git a/platform/msm_shared/scm.c b/platform/msm_shared/scm.c
index f9a7377..b46a504 100644
--- a/platform/msm_shared/scm.c
+++ b/platform/msm_shared/scm.c
@@ -1,4 +1,4 @@
-/* Copyright (c) 2011-2015, The Linux Foundation. All rights reserved.
+/* Copyright (c) 2011-2016, 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
@@ -1333,7 +1333,7 @@
 }
 
 #if PLATFORM_USE_SCM_DLOAD
-int scm_dload_mode(int mode)
+int scm_dload_mode(enum reboot_reason mode)
 {
 	int ret = 0;
 	uint32_t dload_type;