Spacecast: Check factory reset button to enter recovery mode

Recovery mode is selected by pressing the factory reset button contiuously
for 5 seconds after the device is powered on.

Change-Id: Ie39b2907fd846689ae790aeba34d36359624d052
Google-Bug-Id: 22507079
diff --git a/arch/arm/boards/optimus/optimus.c b/arch/arm/boards/optimus/optimus.c
index 01e993f..5b759ae 100644
--- a/arch/arm/boards/optimus/optimus.c
+++ b/arch/arm/boards/optimus/optimus.c
@@ -64,6 +64,8 @@
 #ifdef CONFIG_SPI
 //Legacy spi
 
+static bool recovery_mode = false;
+
 static int c2k_spi_cs[4] = {0};
 
 static struct c2k_spi_master c2k_spi_1_data = {
@@ -314,6 +316,28 @@
 uint32_t tpm_init(void);
 #endif
 
+static int is_factory_reset_pressed(void) {
+	return !comcerto_gpio_read(GPIO_6);
+}
+
+static int is_factory_reset_pressed_continuously(int period_ms) {
+	int i;
+
+	for (i = 0; i < period_ms / 10; i++) {
+		if (!is_factory_reset_pressed()) {
+			return 0;
+		}
+
+		mdelay(10);
+	}
+
+	return 1;
+}
+
+int is_recovery_mode() {
+	return recovery_mode;
+}
+
 static int c2000_device_init(void)
 {
 #ifdef	CONFIG_COMCERTO_BOOTLOADER
@@ -477,6 +501,14 @@
 		protect_file("/dev/env0", 1);
 	}
 
+	if (get_board_id() == SPACECAST_BOARD_ID) {
+		if (is_factory_reset_pressed()) {
+			printf("Factory reset button is pressed, checking for recovery mode ...\n");
+			recovery_mode = is_factory_reset_pressed_continuously(5000);
+			printf("Recovery mode is%s active\n", (recovery_mode? "" : " not"));
+		}
+	}
+
 #ifdef	CONFIG_TPM
 	if (tpm_init() != TPM_SUCCESS) {
 		printf("TPM initialization failed\n");