From ba4c45b5eb7334c2253bc2b98303445d5a9a9845 Mon Sep 17 00:00:00 2001 From: Shiji Yang Date: Tue, 2 May 2023 19:01:42 +0800 Subject: [PATCH] failsafe: rework button detection implementation Signed-off-by: Shiji Yang --- arch/mips/mach-mt7621/Kconfig | 117 -------------- board/ralink/common/Makefile | 1 - board/ralink/common/cmd_mtkautoboot.c | 30 +++- board/ralink/common/gpio.c | 151 ------------------ board/ralink/common/gpio.h | 7 - .../ralink/mt7621_nand_rfb/mt7621_nand_rfb.c | 38 +++++ board/ralink/mt7621_rfb/mt7621_rfb.c | 38 +++++ failsafe/Kconfig | 5 +- 8 files changed, 103 insertions(+), 284 deletions(-) delete mode 100644 board/ralink/common/gpio.c delete mode 100644 board/ralink/common/gpio.h diff --git a/arch/mips/mach-mt7621/Kconfig b/arch/mips/mach-mt7621/Kconfig index 5400447e20..410ddb7b19 100644 --- a/arch/mips/mach-mt7621/Kconfig +++ b/arch/mips/mach-mt7621/Kconfig @@ -62,123 +62,6 @@ config BOARD_MT7621_NAND_TEMPLATE endchoice -menu "MT7621 GPIO Pin Mapping" - depends on MACH_MT7621 - -config MT7621_SYS_LED - bool "Enable System LED" - default y -config MT7621_SYS_LED_PIN - int "System LED GPIO Pin Number" - depends on MT7621_SYS_LED -config MT7621_SYS_LED_LEVEL - int "System LED Trigger Level" - depends on MT7621_SYS_LED - default 0 - range 0 1 - help - 0:low 1:high - -config MT7621_PWR_LED - bool "Enable Power LED" - default n -config MT7621_PWR_LED_PIN - int "Power LED GPIO Pin Number" - depends on MT7621_PWR_LED -config MT7621_PWR_LED_LEVEL - int "Power LED Trigger Level" - depends on MT7621_PWR_LED - default 0 - range 0 1 - -config MT7621_EXTRA_GPIO1 - bool "Enable Init Extra GPIO 1" - default n -config MT7621_EXTRA_GPIO1_PIN - int "Extra GPIO 1 Pin Number" - depends on MT7621_EXTRA_GPIO1 -config MT7621_EXTRA_GPIO1_LEVEL - int "Extra GPIO 1 Default Level" - depends on MT7621_EXTRA_GPIO1 - default 1 - range 0 1 - -config MT7621_EXTRA_GPIO2 - bool "Enable Init Extra GPIO 2" - default n -config MT7621_EXTRA_GPIO2_PIN - int "Extra GPIO 2 Pin Number" - depends on MT7621_EXTRA_GPIO2 -config MT7621_EXTRA_GPIO2_LEVEL - int "Extra GPIO 2 Default Level" - depends on MT7621_EXTRA_GPIO2 - default 1 - range 0 1 - -config MT7621_EXTRA_GPIO3 - bool "Enable Init Extra GPIO 3" - default n -config MT7621_EXTRA_GPIO3_PIN - int "Extra GPIO 3 Pin Number" - depends on MT7621_EXTRA_GPIO3 -config MT7621_EXTRA_GPIO3_LEVEL - int "Extra GPIO 3 Default Level" - depends on MT7621_EXTRA_GPIO3 - default 1 - range 0 1 - -config MT7621_EXTRA_GPIO4 - bool "Enable Init Extra GPIO 4" - default n -config MT7621_EXTRA_GPIO4_PIN - int "Extra GPIO 4 Pin Number" - depends on MT7621_EXTRA_GPIO4 -config MT7621_EXTRA_GPIO4_LEVEL - int "Extra GPIO 4 Default Level" - depends on MT7621_EXTRA_GPIO4 - default 1 - range 0 1 - -config MT7621_EXTRA_GPIO5 - bool "Enable Init Extra GPIO 5" - default n -config MT7621_EXTRA_GPIO5_PIN - int "Extra GPIO 5 Pin Number" - depends on MT7621_EXTRA_GPIO5 -config MT7621_EXTRA_GPIO5_LEVEL - int "Extra GPIO 5 Default Level" - depends on MT7621_EXTRA_GPIO5 - default 1 - range 0 1 - -config MT7621_RST_BTN - bool "Enable Reset Button" - default y -config MT7621_RST_BTN_PIN - int "Reset Button GPIO Pin Number" - depends on MT7621_RST_BTN -config MT7621_RST_BTN_LEVEL - int "Reset Button Trigger Level" - depends on MT7621_RST_BTN - default 0 - range 0 1 - help - 0:low 1:high - -config MT7621_WPS_BTN - bool "Enable WPS Button" - default n -config MT7621_WPS_BTN_PIN - int "WPS Button GPIO Pin Number" - depends on MT7621_WPS_BTN -config MT7621_WPS_BTN_LEVEL - int "WPS Button Trigger Level" - depends on MT7621_WPS_BTN - default 0 - range 0 1 - -endmenu - config SPL_ALIGN_TO hex "SPL alignment size" default 0x10000 if SPI_BOOT diff --git a/board/ralink/common/Makefile b/board/ralink/common/Makefile index 63a2ae665c..8400aa21fa 100644 --- a/board/ralink/common/Makefile +++ b/board/ralink/common/Makefile @@ -12,6 +12,5 @@ endif ifndef CONFIG_SPL_BUILD obj-y += cmd_mtkupgrade.o obj-y += cmd_mtkautoboot.o -obj-y += gpio.o obj-$(CONFIG_MTK_DUAL_IMAGE_SUPPORT) += dual_image.o endif diff --git a/board/ralink/common/cmd_mtkautoboot.c b/board/ralink/common/cmd_mtkautoboot.c index e7906e3c2d..ff16e49cb6 100644 --- a/board/ralink/common/cmd_mtkautoboot.c +++ b/board/ralink/common/cmd_mtkautoboot.c @@ -6,7 +6,7 @@ */ #include -#include "gpio.h" +#include struct mtk_bootmenu_entry { const char *desc; @@ -40,12 +40,32 @@ static int do_mtkautoboot(cmd_tbl_t *cmdtp, int flag, int argc, const char *delay_str; u32 delay = CONFIG_MTKAUTOBOOT_DELAY; -#ifdef CONFIG_WEBUI_FAILSAFE_ON_BUTTON - if (mt7621_is_reset_pressed()) { - printf("Enter Failsafe Mode by Press Reset Button!\n"); +#ifdef CONFIG_FAILSAFE_ON_BUTTON + for (i = 0; i < 5; i++) { + if (gpio_get_value(MT7621_BUTTON_RESET) != 0) + break; + + mdelay(100); +#ifdef MT7621_LED_STATUS1 + gpio_direction_output(MT7621_LED_STATUS1, 1); +#endif // MT7621_LED_STATUS1 +#ifdef MT7621_LED_STATUS2 + gpio_direction_output(MT7621_LED_STATUS2, 0); +#endif // MT7621_LED_STATUS2 + mdelay(100); +#ifdef MT7621_LED_STATUS1 + gpio_direction_output(MT7621_LED_STATUS1, 0); +#endif // MT7621_LED_STATUS1 +#ifdef MT7621_LED_STATUS2 + gpio_direction_output(MT7621_LED_STATUS2, 1); +#endif // MT7621_LED_STATUS2 + } + + if (i == 5) { + printf("Enter web failsafe mode by pressing reset button\n"); run_command("httpd", 0); } -#endif // CONFIG_WEBUI_FAILSAFE_ON_BUTTON +#endif // CONFIG_FAILSAFE_ON_BUTTON for (i = 0; i < ARRAY_SIZE(bootmenu_entries); i++) { snprintf(key, sizeof(key), "bootmenu_%d", i); diff --git a/board/ralink/common/gpio.c b/board/ralink/common/gpio.c deleted file mode 100644 index 546bb9c309..0000000000 --- a/board/ralink/common/gpio.c +++ /dev/null @@ -1,151 +0,0 @@ -#include -#include -#include -#include "gpio.h" - -static int gpio_input(unsigned int pin) -{ - int ret = 0; - unsigned int gpio; - int value; - char gpio_number[128]; - sprintf(gpio_number, "%d", pin); -#if defined(CONFIG_DM_GPIO) - ret = gpio_lookup_name(gpio_number, NULL, NULL, &gpio); -#else - /* turn the gpio name into a gpio number */ - gpio = name_to_gpio(gpio_number); -#endif - /* grab the pin before we tweak it */ - gpio_request(gpio, "cmd_gpio"); - if (ret && ret != -EBUSY) { - printf("gpio: requesting pin %u failed\n", gpio); - return -1; - } - - gpio_direction_input(gpio); - value = gpio_get_value(gpio); - if (ret != -EBUSY) - gpio_free(gpio); - - return value; -} - -static void gpio_output(unsigned int pin, int value) -{ - int ret = 0; - unsigned int gpio; - char gpio_number[128]; - sprintf(gpio_number, "%d", pin); -#if defined(CONFIG_DM_GPIO) - ret = gpio_lookup_name(gpio_number, NULL, NULL, &gpio); -#else - /* turn the gpio name into a gpio number */ - gpio = name_to_gpio(gpio_number); -#endif - /* grab the pin before we tweak it */ - gpio_request(gpio, "cmd_gpio"); - if (ret && ret != -EBUSY) { - printf("gpio: requesting pin %u failed\n", gpio); - return; - } - - gpio_direction_output(gpio, value); - if (ret != -EBUSY) - gpio_free(gpio); -} - -/** - * system led blink once, 200 ms - */ -static void mt7621_sys_led_blink(void) -{ -#ifdef CONFIG_MT7621_SYS_LED - gpio_output(CONFIG_MT7621_SYS_LED_PIN, !CONFIG_MT7621_SYS_LED_LEVEL); - mdelay(100); // wait 100ms - gpio_output(CONFIG_MT7621_SYS_LED_PIN, CONFIG_MT7621_SYS_LED_LEVEL); - mdelay(100); // wait 100ms -#endif // CONFIG_MT7621_SYS_LED -} - -/** - * @return 1 if button is pressed 2 seconds, else 0 - */ -static int mt7621_is_button_pressed(int gpio, int trigLevel) -{ - int count = 0; - for(count = 0; count < 10; count ++) { - int val = gpio_input(gpio); - if(val ^ trigLevel) - return 0; - mt7621_sys_led_blink(); // blink 10 times, 2 seconds - } - return 1; -} - -static void mt7621_gpio_init(void) -{ -#ifdef CONFIG_MT7621_EXTRA_GPIO1 - gpio_output(CONFIG_MT7621_EXTRA_GPIO1_PIN, CONFIG_MT7621_EXTRA_GPIO1_LEVEL); -#endif // CONFIG_MT7621_EXTRA_GPIO1 - -#ifdef CONFIG_MT7621_EXTRA_GPIO2 - gpio_output(CONFIG_MT7621_EXTRA_GPIO2_PIN, CONFIG_MT7621_EXTRA_GPIO2_LEVEL); -#endif // CONFIG_MT7621_EXTRA_GPIO2 - -#ifdef CONFIG_MT7621_EXTRA_GPIO3 - gpio_output(CONFIG_MT7621_EXTRA_GPIO3_PIN, CONFIG_MT7621_EXTRA_GPIO3_LEVEL); -#endif // CONFIG_MT7621_EXTRA_GPIO3 - -#ifdef CONFIG_MT7621_EXTRA_GPIO4 - gpio_output(CONFIG_MT7621_EXTRA_GPIO4_PIN, CONFIG_MT7621_EXTRA_GPIO4_LEVEL); -#endif // CONFIG_MT7621_EXTRA_GPIO4 - -#ifdef CONFIG_MT7621_EXTRA_GPIO5 - gpio_output(CONFIG_MT7621_EXTRA_GPIO5_PIN, CONFIG_MT7621_EXTRA_GPIO5_LEVEL); -#endif // CONFIG_MT7621_EXTRA_GPIO5 - -#ifdef CONFIG_MT7621_PWR_LED - gpio_output(CONFIG_MT7621_PWR_LED_PIN, CONFIG_MT7621_PWR_LED_LEVEL); -#endif // CONFIG_MT7621_PWR_LED - -#ifdef CONFIG_MT7621_SYS_LED - gpio_output(CONFIG_MT7621_SYS_LED_PIN, CONFIG_MT7621_SYS_LED_LEVEL); -#endif // CONFIG_MT7621_SYS_LED - -#ifdef CONFIG_MT7621_RST_BTN - gpio_input(CONFIG_MT7621_RST_BTN_PIN); -#endif // CONFIG_MT7621_RST_BTN - -#ifdef CONFIG_MT7621_WPS_BTN - gpio_input(CONFIG_MT7621_WPS_BTN_PIN); -#endif // CONFIG_MT7621_WPS_BTN -} - -#ifdef CONFIG_MT7621_RST_BTN -/** - * @return 1 if reset button is pressed 3 seconds, else 0 - */ -int mt7621_is_reset_pressed(void) -{ - return mt7621_is_button_pressed(CONFIG_MT7621_RST_BTN_PIN, CONFIG_MT7621_RST_BTN_LEVEL); -} -#endif // CONFIG_MT7621_RST_BTN - -#ifdef CONFIG_MT7621_WPS_BTN -/** - * @return 1 if wps button is pressed 3 seconds, else 0 - */ -int mt7621_is_wps_pressed(void) -{ - return mt7621_is_button_pressed(CONFIG_MT7621_WPS_BTN_PIN, CONFIG_MT7621_WPS_BTN_LEVEL); -} -#endif // CONFIG_MT7621_WPS_BTN - -#ifdef CONFIG_LAST_STAGE_INIT -int last_stage_init(void) -{ - mt7621_gpio_init(); - return 0; -} -#endif // CONFIG_LAST_STAGE_INIT diff --git a/board/ralink/common/gpio.h b/board/ralink/common/gpio.h deleted file mode 100644 index 122474ec92..0000000000 --- a/board/ralink/common/gpio.h +++ /dev/null @@ -1,7 +0,0 @@ -#ifndef _BOARD_RALINK_GPIO_H_ -#define _BOARD_RALINK_GPIO_H_ - -int mt7621_is_reset_pressed(void); -int mt7621_is_wps_pressed(void); - -#endif /* _BOARD_RALINK_GPIO_H_ */ diff --git a/board/ralink/mt7621_nand_rfb/mt7621_nand_rfb.c b/board/ralink/mt7621_nand_rfb/mt7621_nand_rfb.c index a11f6aa14f..19afff5a26 100644 --- a/board/ralink/mt7621_nand_rfb/mt7621_nand_rfb.c +++ b/board/ralink/mt7621_nand_rfb/mt7621_nand_rfb.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -22,6 +23,43 @@ static int nmbm_usable; #endif +static inline int gpio_output_init(unsigned gpio, int value, const char *label) +{ + int ret; + + ret = gpio_request(gpio, label); + if (ret && ret != -EBUSY) { + printf("gpio: requesting pin %u failed\n", gpio); + return -1; + } + + gpio_direction_output(gpio, value); + + return 0; +} + +static inline int gpio_input_init(unsigned gpio, const char *label) +{ + int ret; + + ret = gpio_request(gpio, label); + if (ret && ret != -EBUSY) { + printf("gpio: requesting pin %u failed\n", gpio); + return -1; + } + + gpio_direction_input(gpio); + + return 0; +} + +#ifdef CONFIG_LAST_STAGE_INIT +int last_stage_init(void) +{ + return 0; +} +#endif // CONFIG_LAST_STAGE_INIT + #ifdef CONFIG_DEBUG_UART_BOARD_INIT void board_debug_uart_init(void) { diff --git a/board/ralink/mt7621_rfb/mt7621_rfb.c b/board/ralink/mt7621_rfb/mt7621_rfb.c index f698ce8cf9..9c95db1545 100644 --- a/board/ralink/mt7621_rfb/mt7621_rfb.c +++ b/board/ralink/mt7621_rfb/mt7621_rfb.c @@ -10,12 +10,50 @@ #include #include #include +#include #include #include #include #include #include +static inline int gpio_output_init(unsigned gpio, int value, const char *label) +{ + int ret; + + ret = gpio_request(gpio, label); + if (ret && ret != -EBUSY) { + printf("gpio: requesting pin %u failed\n", gpio); + return -1; + } + + gpio_direction_output(gpio, value); + + return 0; +} + +static inline int gpio_input_init(unsigned gpio, const char *label) +{ + int ret; + + ret = gpio_request(gpio, label); + if (ret && ret != -EBUSY) { + printf("gpio: requesting pin %u failed\n", gpio); + return -1; + } + + gpio_direction_input(gpio); + + return 0; +} + +#ifdef CONFIG_LAST_STAGE_INIT +int last_stage_init(void) +{ + return 0; +} +#endif // CONFIG_LAST_STAGE_INIT + #ifdef CONFIG_DEBUG_UART_BOARD_INIT void board_debug_uart_init(void) { diff --git a/failsafe/Kconfig b/failsafe/Kconfig index cd1d07e4f3..76779b1055 100644 --- a/failsafe/Kconfig +++ b/failsafe/Kconfig @@ -16,9 +16,8 @@ config WEBUI_FAILSAFE_ON_AUTOBOOT_FAIL bool "Start Failsafe Web UI on autoboot failure" default n -config WEBUI_FAILSAFE_ON_BUTTON - bool "Press and hold the reset button to enter the failsafe" - depends on MT7621_RST_BTN +config FAILSAFE_ON_BUTTON + bool "Press and hold the reset button to enter the failsafe mode" default n endif # WEBUI_FAILSAFE