Angel Pons | 7caffce | 2020-04-05 13:22:51 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Daisuke Nojiri | 81193e6 | 2014-11-12 14:44:13 -0800 | [diff] [blame] | 2 | |
Vadim Bendebury | 8a6e635 | 2015-03-10 13:03:58 -0700 | [diff] [blame] | 3 | #include <boardid.h> |
Kyösti Mälkki | f122696 | 2021-11-03 17:19:31 +0200 | [diff] [blame^] | 4 | #include <bootmode.h> |
Daisuke Nojiri | 81193e6 | 2014-11-12 14:44:13 -0800 | [diff] [blame] | 5 | #include <boot/coreboot_tables.h> |
| 6 | #include <console/console.h> |
| 7 | #include <delay.h> |
Vadim Bendebury | 8a6e635 | 2015-03-10 13:03:58 -0700 | [diff] [blame] | 8 | #include <drivers/i2c/ww_ring/ww_ring.h> |
Daisuke Nojiri | 81193e6 | 2014-11-12 14:44:13 -0800 | [diff] [blame] | 9 | #include <gpio.h> |
Vadim Bendebury | 8a6e635 | 2015-03-10 13:03:58 -0700 | [diff] [blame] | 10 | #include <soc/cdp.h> |
| 11 | #include <soc/gsbi.h> |
Vadim Bendebury | f61809a | 2015-02-20 12:49:47 -0800 | [diff] [blame] | 12 | #include <timer.h> |
Daisuke Nojiri | 81193e6 | 2014-11-12 14:44:13 -0800 | [diff] [blame] | 13 | |
| 14 | #define DEV_SW 15 |
Daisuke Nojiri | 81193e6 | 2014-11-12 14:44:13 -0800 | [diff] [blame] | 15 | #define REC_SW 16 |
Daisuke Nojiri | 81193e6 | 2014-11-12 14:44:13 -0800 | [diff] [blame] | 16 | #define WP_SW 17 |
Daisuke Nojiri | 81193e6 | 2014-11-12 14:44:13 -0800 | [diff] [blame] | 17 | |
| 18 | static int read_gpio(gpio_t gpio_num) |
| 19 | { |
| 20 | gpio_tlmm_config_set(gpio_num, GPIO_FUNC_DISABLE, |
| 21 | GPIO_NO_PULL, GPIO_2MA, GPIO_DISABLE); |
| 22 | udelay(10); /* Should be enough to settle. */ |
| 23 | return gpio_get(gpio_num); |
| 24 | } |
| 25 | |
| 26 | void fill_lb_gpios(struct lb_gpios *gpios) |
| 27 | { |
Julius Werner | c445b4f | 2016-03-31 17:27:05 -0700 | [diff] [blame] | 28 | struct lb_gpio chromeos_gpios[] = { |
Matt Delco | 2cb3996 | 2019-04-30 14:59:43 -0700 | [diff] [blame] | 29 | {DEV_SW, ACTIVE_LOW, read_gpio(REC_SW), "presence"}, |
Julius Werner | c445b4f | 2016-03-31 17:27:05 -0700 | [diff] [blame] | 30 | {-1, ACTIVE_LOW, 1, "power"}, |
| 31 | {-1, ACTIVE_LOW, 0, "lid"}, |
| 32 | }; |
| 33 | lb_add_gpios(gpios, chromeos_gpios, ARRAY_SIZE(chromeos_gpios)); |
Daisuke Nojiri | 81193e6 | 2014-11-12 14:44:13 -0800 | [diff] [blame] | 34 | } |
| 35 | |
Vadim Bendebury | f61809a | 2015-02-20 12:49:47 -0800 | [diff] [blame] | 36 | /* |
Vadim Bendebury | 381785d | 2015-03-12 19:47:22 -0700 | [diff] [blame] | 37 | * The recovery switch on storm is overloaded: it needs to be pressed for a |
| 38 | * certain duration at startup to signal different requests: |
| 39 | * |
| 40 | * - keeping it pressed for 8 to 16 seconds after startup signals the need for |
| 41 | * factory reset (wipeout); |
| 42 | * - keeping it pressed for longer than 16 seconds signals the need for Chrome |
| 43 | * OS recovery. |
| 44 | * |
| 45 | * The state is read once and cached for following inquiries. The below enum |
| 46 | * lists possible states. |
Vadim Bendebury | f61809a | 2015-02-20 12:49:47 -0800 | [diff] [blame] | 47 | */ |
Vadim Bendebury | 381785d | 2015-03-12 19:47:22 -0700 | [diff] [blame] | 48 | enum switch_state { |
| 49 | not_probed = -1, |
| 50 | no_req, |
| 51 | recovery_req, |
| 52 | wipeout_req |
| 53 | }; |
| 54 | |
Vadim Bendebury | 4e0de32 | 2015-03-17 15:37:54 -0700 | [diff] [blame] | 55 | static void display_pattern(int pattern) |
| 56 | { |
| 57 | if (board_id() == BOARD_ID_WHIRLWIND_SP5) |
| 58 | ww_ring_display_pattern(GSBI_ID_7, pattern); |
| 59 | } |
| 60 | |
Vadim Bendebury | 381785d | 2015-03-12 19:47:22 -0700 | [diff] [blame] | 61 | #define WIPEOUT_MODE_DELAY_MS (8 * 1000) |
| 62 | #define RECOVERY_MODE_EXTRA_DELAY_MS (8 * 1000) |
| 63 | |
| 64 | static enum switch_state get_switch_state(void) |
Daisuke Nojiri | 81193e6 | 2014-11-12 14:44:13 -0800 | [diff] [blame] | 65 | { |
Vadim Bendebury | f61809a | 2015-02-20 12:49:47 -0800 | [diff] [blame] | 66 | struct stopwatch sw; |
Vadim Bendebury | 381785d | 2015-03-12 19:47:22 -0700 | [diff] [blame] | 67 | int sampled_value; |
| 68 | static enum switch_state saved_state = not_probed; |
Vadim Bendebury | f61809a | 2015-02-20 12:49:47 -0800 | [diff] [blame] | 69 | |
Vadim Bendebury | 381785d | 2015-03-12 19:47:22 -0700 | [diff] [blame] | 70 | if (saved_state != not_probed) |
| 71 | return saved_state; |
Vadim Bendebury | f61809a | 2015-02-20 12:49:47 -0800 | [diff] [blame] | 72 | |
Joel Kitching | ae0fb76 | 2019-04-07 00:37:14 +0800 | [diff] [blame] | 73 | sampled_value = !read_gpio(REC_SW); |
Vadim Bendebury | 381785d | 2015-03-12 19:47:22 -0700 | [diff] [blame] | 74 | |
| 75 | if (!sampled_value) { |
| 76 | saved_state = no_req; |
Vadim Bendebury | 6897f4e | 2015-04-13 16:32:45 -0700 | [diff] [blame] | 77 | display_pattern(WWR_NORMAL_BOOT); |
Vadim Bendebury | 381785d | 2015-03-12 19:47:22 -0700 | [diff] [blame] | 78 | return saved_state; |
| 79 | } |
Vadim Bendebury | f61809a | 2015-02-20 12:49:47 -0800 | [diff] [blame] | 80 | |
Vadim Bendebury | 4e0de32 | 2015-03-17 15:37:54 -0700 | [diff] [blame] | 81 | display_pattern(WWR_RECOVERY_PUSHED); |
Vadim Bendebury | f61809a | 2015-02-20 12:49:47 -0800 | [diff] [blame] | 82 | printk(BIOS_INFO, "recovery button pressed\n"); |
Vadim Bendebury | 4e0de32 | 2015-03-17 15:37:54 -0700 | [diff] [blame] | 83 | |
Vadim Bendebury | 381785d | 2015-03-12 19:47:22 -0700 | [diff] [blame] | 84 | stopwatch_init_msecs_expire(&sw, WIPEOUT_MODE_DELAY_MS); |
Vadim Bendebury | f61809a | 2015-02-20 12:49:47 -0800 | [diff] [blame] | 85 | |
| 86 | do { |
Joel Kitching | ae0fb76 | 2019-04-07 00:37:14 +0800 | [diff] [blame] | 87 | sampled_value = !read_gpio(REC_SW); |
Vadim Bendebury | f61809a | 2015-02-20 12:49:47 -0800 | [diff] [blame] | 88 | if (!sampled_value) |
| 89 | break; |
| 90 | } while (!stopwatch_expired(&sw)); |
| 91 | |
Vadim Bendebury | 8a6e635 | 2015-03-10 13:03:58 -0700 | [diff] [blame] | 92 | if (sampled_value) { |
Vadim Bendebury | 4e0de32 | 2015-03-17 15:37:54 -0700 | [diff] [blame] | 93 | display_pattern(WWR_WIPEOUT_REQUEST); |
Vadim Bendebury | 381785d | 2015-03-12 19:47:22 -0700 | [diff] [blame] | 94 | printk(BIOS_INFO, "wipeout requested, checking recovery\n"); |
| 95 | stopwatch_init_msecs_expire(&sw, RECOVERY_MODE_EXTRA_DELAY_MS); |
| 96 | do { |
Joel Kitching | ae0fb76 | 2019-04-07 00:37:14 +0800 | [diff] [blame] | 97 | sampled_value = !read_gpio(REC_SW); |
Vadim Bendebury | 381785d | 2015-03-12 19:47:22 -0700 | [diff] [blame] | 98 | if (!sampled_value) |
| 99 | break; |
| 100 | } while (!stopwatch_expired(&sw)); |
| 101 | |
| 102 | if (sampled_value) { |
| 103 | saved_state = recovery_req; |
Vadim Bendebury | 4e0de32 | 2015-03-17 15:37:54 -0700 | [diff] [blame] | 104 | display_pattern(WWR_RECOVERY_REQUEST); |
Vadim Bendebury | 381785d | 2015-03-12 19:47:22 -0700 | [diff] [blame] | 105 | printk(BIOS_INFO, "recovery requested\n"); |
| 106 | } else { |
| 107 | saved_state = wipeout_req; |
| 108 | } |
| 109 | } else { |
| 110 | saved_state = no_req; |
Vadim Bendebury | 6897f4e | 2015-04-13 16:32:45 -0700 | [diff] [blame] | 111 | display_pattern(WWR_NORMAL_BOOT); |
Vadim Bendebury | 8a6e635 | 2015-03-10 13:03:58 -0700 | [diff] [blame] | 112 | } |
Vadim Bendebury | 381785d | 2015-03-12 19:47:22 -0700 | [diff] [blame] | 113 | |
| 114 | return saved_state; |
| 115 | } |
| 116 | |
| 117 | int get_recovery_mode_switch(void) |
| 118 | { |
| 119 | return get_switch_state() == recovery_req; |
| 120 | } |
| 121 | |
| 122 | int get_wipeout_mode_switch(void) |
| 123 | { |
| 124 | return get_switch_state() == wipeout_req; |
Daisuke Nojiri | 81193e6 | 2014-11-12 14:44:13 -0800 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | int get_write_protect_state(void) |
| 128 | { |
Joel Kitching | ae0fb76 | 2019-04-07 00:37:14 +0800 | [diff] [blame] | 129 | return !read_gpio(WP_SW); |
Daisuke Nojiri | 81193e6 | 2014-11-12 14:44:13 -0800 | [diff] [blame] | 130 | } |