From f735627eca186583d2e36658e3ee6400ba475664 Mon Sep 17 00:00:00 2001 From: Rene Straub Date: Thu, 15 Mar 2018 12:07:26 +0100 Subject: [PATCH] nrhw20: add reset button fucntionality --- board/nm/nrhw20/board.c | 180 ++++++++++++++++++++-------------------- 1 file changed, 89 insertions(+), 91 deletions(-) diff --git a/board/nm/nrhw20/board.c b/board/nm/nrhw20/board.c index ca87a0f625..4ce59bb728 100644 --- a/board/nm/nrhw20/board.c +++ b/board/nm/nrhw20/board.c @@ -397,101 +397,11 @@ err_free_gpio: #endif -#if !defined(CONFIG_SPL_BUILD) - -/* TODO: Redesign to use PMIC input */ -static int check_reset_button(void) -{ -#if 0 - int counter = 0; - int ret; - - ret = gpio_request(NETBIRD_GPIO_RESET_BUTTON, "reset button"); - if (ret < 0) { - printf("Unable to request reset button gpio\n"); - return -1; - } - - ret = gpio_direction_input(NETBIRD_GPIO_RESET_BUTTON); - if (ret < 0) { - printf("Unable to set reset button as input\n"); - return -1; - } - - /* Check if reset button is pressed for at least 2 seconds ≃ ~5s */ - do { - if (gpio_get_value(NETBIRD_GPIO_RESET_BUTTON) != 0) break; - udelay(100000); /* 100ms */ - counter++; - - if (counter==20) {/* Indicate factory reset threshold */ - gpio_set_value(NETBIRD_GPIO_LED_A, 0); - gpio_set_value(NETBIRD_GPIO_LED_B, 0); - udelay(400000); /* 400ms */ - /* let LED blink up once */ - gpio_set_value(NETBIRD_GPIO_LED_A, 1); - gpio_set_value(NETBIRD_GPIO_LED_B, 1); - udelay(400000); /* 400ms */ - gpio_set_value(NETBIRD_GPIO_LED_A, 0); - gpio_set_value(NETBIRD_GPIO_LED_B, 0); - } else if (counter==120) { /* Indicate recovery boot threshold */ - /* let LED blink up twice */ - gpio_set_value(NETBIRD_GPIO_LED_A, 1); - gpio_set_value(NETBIRD_GPIO_LED_B, 1); - udelay(400000); /* 400ms */ - gpio_set_value(NETBIRD_GPIO_LED_A, 0); - gpio_set_value(NETBIRD_GPIO_LED_B, 0); - udelay(400000); /* 400ms */ - gpio_set_value(NETBIRD_GPIO_LED_A, 1); - gpio_set_value(NETBIRD_GPIO_LED_B, 1); - udelay(400000); /* 400ms */ - gpio_set_value(NETBIRD_GPIO_LED_A, 0); - gpio_set_value(NETBIRD_GPIO_LED_B, 0); - } - } while (counter<120); - - if (counter < 20) return 0; /* Don't do anything for duration < 2s */ - - if (counter < 120) /* Do factory reset for duration between ~5s and ~15s */ - { - char new_bootargs[512]; - char *bootargs = getenv("bootargs"); - - if (bootargs==0) bootargs=""; - - printf("Do factory reset during boot...\n"); - - strncpy(new_bootargs, bootargs, sizeof(new_bootargs)); - strncat(new_bootargs, " factory-reset", sizeof(new_bootargs)); - - setenv("bootargs", new_bootargs); - - printf("bootargs = %s\n", new_bootargs); - - return 1; - } else { /* Boot into recovery for duration > 15s */ - /* set consoledev to external port */ - setenv("consoledev", "ttyS1"); - - printf("Booting recovery image...\n"); - - /* Set bootcmd to run recovery */ - setenv("bootcmd", "run recovery"); - - return 0; - } - return 0; -#else - return 0; -#endif -} -#endif - - /* TODO: Create DA9063 Accessor Module */ #define CONFIG_PMIC_I2C_BUS 0 #define CONFIG_PMIC_I2C_ADDR 0x58 /* Pages 0 and 1, Pages 2 and 3 -> 0x59 */ +#define PMIC_REG_STATUS_A 0x01 /* Status of ON_KEY, WAKE, COMP1V2, DVC */ #define PMIC_REG_GPIO_MODE0_7 0x1D /* Control register for GPIOs 0..7 */ #define PMIC_REG_GPIO_MODE8_15 0x1E /* Control register for GPIOs 8..15 */ @@ -877,6 +787,94 @@ static void shield_init(void) } #endif + +#if !defined(CONFIG_SPL_BUILD) + +static bool get_button_state(void) +{ + u8 state = 0x00; + + (void)da9093_get_reg(PMIC_REG_STATUS_A, &state); + + return (state & 0x01) == 0x01; +} + +static void blink_led(void) +{ + const int pulse_width = 400*1000; /* 400ms */ + + /* Assumes green status LED is on */ + udelay(pulse_width); + set_status_led(0, 0); + udelay(pulse_width); + set_status_led(0, 1); +} + +static int check_reset_button(void) +{ + int counter = 0; + + /* Check how long button is pressed */ + do { + if (!get_button_state()) + break; + + udelay(100*1000); /* 100ms */ + counter += 100; + + if (counter == 2000) { + /* Indicate factory reset threshold */ + blink_led(); + } + else if (counter == 12000) { + /* Indicate recovery boot threshold */ + blink_led(); + blink_led(); + } + } while (counter < 12000); + + /* Don't do anything for duration < 2s */ + if (counter < 2000) + return 0; + + /* Do factory reset for duration between ~5s and ~15s */ + if (counter < 12000) + { + char new_bootargs[512]; + char *bootargs = getenv("bootargs"); + + if (bootargs == 0) bootargs=""; + + printf("Do factory reset during boot...\n"); + + strncpy(new_bootargs, bootargs, sizeof(new_bootargs)); + strncat(new_bootargs, " factory-reset", sizeof(new_bootargs)); + + setenv("bootargs", new_bootargs); + + printf("bootargs = %s\n", new_bootargs); + + return 1; + } + + /* Boot into recovery for duration > 15s */ + else + { + /* set consoledev to external port */ + /* setenv("consoledev", "ttyS1"); */ + + printf("Booting recovery image...\n"); + + /* Set bootcmd to run recovery */ + setenv("bootcmd", "run recovery"); + + return 0; + } + + return 0; +} +#endif + int board_late_init(void) { #if !defined(CONFIG_SPL_BUILD)