Angel Pons | 4d94ae4 | 2020-04-05 13:21:31 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Varadarajan Narayanan | 3bbd901 | 2016-03-02 13:15:47 +0530 | [diff] [blame] | 2 | |
| 3 | #include <boardid.h> |
Kyösti Mälkki | f122696 | 2021-11-03 17:19:31 +0200 | [diff] [blame^] | 4 | #include <bootmode.h> |
Varadarajan Narayanan | 3bbd901 | 2016-03-02 13:15:47 +0530 | [diff] [blame] | 5 | #include <boot/coreboot_tables.h> |
| 6 | #include <console/console.h> |
| 7 | #include <delay.h> |
| 8 | #include <drivers/i2c/ww_ring/ww_ring.h> |
| 9 | #include <gpio.h> |
| 10 | #include <soc/cdp.h> |
Varadarajan Narayanan | 2596764 | 2016-03-08 15:02:56 +0530 | [diff] [blame] | 11 | #include <soc/blsp.h> |
Varadarajan Narayanan | 3bbd901 | 2016-03-02 13:15:47 +0530 | [diff] [blame] | 12 | #include <timer.h> |
Varadarajan Narayanan | 3bbd901 | 2016-03-02 13:15:47 +0530 | [diff] [blame] | 13 | |
Joel Kitching | 99625b0 | 2019-03-21 00:30:38 +0800 | [diff] [blame] | 14 | #define PP_SW 41 |
Varadarajan Narayanan | 3bbd901 | 2016-03-02 13:15:47 +0530 | [diff] [blame] | 15 | |
Kan Yan | 041bc76 | 2016-07-14 18:05:20 -0700 | [diff] [blame] | 16 | static int get_rec_sw_gpio_pin(void) |
| 17 | { |
| 18 | uint8_t board_rev = board_id(); |
Kan Yan | d1a0051 | 2016-07-28 18:55:26 -0700 | [diff] [blame] | 19 | |
Kan Yan | 041bc76 | 2016-07-14 18:05:20 -0700 | [diff] [blame] | 20 | switch (board_rev) { |
Kan Yan | 08492f7 | 2016-07-21 10:51:47 -0700 | [diff] [blame] | 21 | case BOARD_ID_GALE_PROTO: |
Kan Yan | 041bc76 | 2016-07-14 18:05:20 -0700 | [diff] [blame] | 22 | case BOARD_ID_GALE_EVT: |
Kan Yan | d1a0051 | 2016-07-28 18:55:26 -0700 | [diff] [blame] | 23 | case BOARD_ID_GALE_EVT2_0: |
| 24 | case BOARD_ID_GALE_EVT2_1: |
Kan Yan | 041bc76 | 2016-07-14 18:05:20 -0700 | [diff] [blame] | 25 | return 7; |
| 26 | case BOARD_ID_GALE_EVT3: |
| 27 | default: |
| 28 | return 57; |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | static int get_wp_status_gpio_pin(void) |
| 33 | { |
| 34 | uint8_t board_rev = board_id(); |
| 35 | switch (board_rev) { |
Kan Yan | 08492f7 | 2016-07-21 10:51:47 -0700 | [diff] [blame] | 36 | case BOARD_ID_GALE_PROTO: |
Kan Yan | 041bc76 | 2016-07-14 18:05:20 -0700 | [diff] [blame] | 37 | case BOARD_ID_GALE_EVT: |
Kan Yan | d1a0051 | 2016-07-28 18:55:26 -0700 | [diff] [blame] | 38 | case BOARD_ID_GALE_EVT2_0: |
| 39 | case BOARD_ID_GALE_EVT2_1: |
Kan Yan | 041bc76 | 2016-07-14 18:05:20 -0700 | [diff] [blame] | 40 | return 6; |
| 41 | case BOARD_ID_GALE_EVT3: |
| 42 | default: |
| 43 | return 53; |
| 44 | } |
| 45 | } |
Varadarajan Narayanan | 3bbd901 | 2016-03-02 13:15:47 +0530 | [diff] [blame] | 46 | static int read_gpio(gpio_t gpio_num) |
| 47 | { |
| 48 | gpio_tlmm_config_set(gpio_num, GPIO_FUNC_DISABLE, |
| 49 | GPIO_NO_PULL, GPIO_2MA, GPIO_DISABLE); |
| 50 | udelay(10); /* Should be enough to settle. */ |
| 51 | return gpio_get(gpio_num); |
| 52 | } |
| 53 | |
| 54 | void fill_lb_gpios(struct lb_gpios *gpios) |
| 55 | { |
| 56 | struct lb_gpio chromeos_gpios[] = { |
Joel Kitching | ae0fb76 | 2019-04-07 00:37:14 +0800 | [diff] [blame] | 57 | {PP_SW, ACTIVE_LOW, read_gpio(PP_SW), "presence"}, |
Varadarajan Narayanan | 3bbd901 | 2016-03-02 13:15:47 +0530 | [diff] [blame] | 58 | {-1, ACTIVE_LOW, 1, "power"}, |
| 59 | {-1, ACTIVE_LOW, 0, "lid"}, |
| 60 | }; |
| 61 | lb_add_gpios(gpios, chromeos_gpios, ARRAY_SIZE(chromeos_gpios)); |
| 62 | } |
| 63 | |
Varadarajan Narayanan | 3bbd901 | 2016-03-02 13:15:47 +0530 | [diff] [blame] | 64 | /* |
| 65 | * The recovery switch on storm is overloaded: it needs to be pressed for a |
| 66 | * certain duration at startup to signal different requests: |
| 67 | * |
| 68 | * - keeping it pressed for 8 to 16 seconds after startup signals the need for |
| 69 | * factory reset (wipeout); |
| 70 | * - keeping it pressed for longer than 16 seconds signals the need for Chrome |
| 71 | * OS recovery. |
| 72 | * |
| 73 | * The state is read once and cached for following inquiries. The below enum |
| 74 | * lists possible states. |
| 75 | */ |
| 76 | enum switch_state { |
| 77 | not_probed = -1, |
| 78 | no_req, |
| 79 | recovery_req, |
| 80 | wipeout_req |
| 81 | }; |
| 82 | |
| 83 | static void display_pattern(int pattern) |
| 84 | { |
Suresh Rajashekara | 6e69bfd | 2016-05-18 17:48:33 -0700 | [diff] [blame] | 85 | ww_ring_display_pattern(BLSP_QUP_ID_3, pattern); |
Varadarajan Narayanan | 3bbd901 | 2016-03-02 13:15:47 +0530 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | #define WIPEOUT_MODE_DELAY_MS (8 * 1000) |
| 89 | #define RECOVERY_MODE_EXTRA_DELAY_MS (8 * 1000) |
| 90 | |
| 91 | static enum switch_state get_switch_state(void) |
| 92 | { |
| 93 | struct stopwatch sw; |
| 94 | int sampled_value; |
Kan Yan | 041bc76 | 2016-07-14 18:05:20 -0700 | [diff] [blame] | 95 | uint8_t rec_sw; |
Varadarajan Narayanan | 3bbd901 | 2016-03-02 13:15:47 +0530 | [diff] [blame] | 96 | static enum switch_state saved_state = not_probed; |
| 97 | |
| 98 | if (saved_state != not_probed) |
| 99 | return saved_state; |
| 100 | |
Kan Yan | 041bc76 | 2016-07-14 18:05:20 -0700 | [diff] [blame] | 101 | rec_sw = get_rec_sw_gpio_pin(); |
Joel Kitching | ae0fb76 | 2019-04-07 00:37:14 +0800 | [diff] [blame] | 102 | sampled_value = !read_gpio(rec_sw); |
Varadarajan Narayanan | 3bbd901 | 2016-03-02 13:15:47 +0530 | [diff] [blame] | 103 | |
| 104 | if (!sampled_value) { |
| 105 | saved_state = no_req; |
| 106 | display_pattern(WWR_NORMAL_BOOT); |
| 107 | return saved_state; |
| 108 | } |
| 109 | |
| 110 | display_pattern(WWR_RECOVERY_PUSHED); |
| 111 | printk(BIOS_INFO, "recovery button pressed\n"); |
| 112 | |
| 113 | stopwatch_init_msecs_expire(&sw, WIPEOUT_MODE_DELAY_MS); |
| 114 | |
| 115 | do { |
Joel Kitching | ae0fb76 | 2019-04-07 00:37:14 +0800 | [diff] [blame] | 116 | sampled_value = !read_gpio(rec_sw); |
Varadarajan Narayanan | 3bbd901 | 2016-03-02 13:15:47 +0530 | [diff] [blame] | 117 | if (!sampled_value) |
| 118 | break; |
| 119 | } while (!stopwatch_expired(&sw)); |
| 120 | |
| 121 | if (sampled_value) { |
| 122 | display_pattern(WWR_WIPEOUT_REQUEST); |
| 123 | printk(BIOS_INFO, "wipeout requested, checking recovery\n"); |
| 124 | stopwatch_init_msecs_expire(&sw, RECOVERY_MODE_EXTRA_DELAY_MS); |
| 125 | do { |
Joel Kitching | ae0fb76 | 2019-04-07 00:37:14 +0800 | [diff] [blame] | 126 | sampled_value = !read_gpio(rec_sw); |
Varadarajan Narayanan | 3bbd901 | 2016-03-02 13:15:47 +0530 | [diff] [blame] | 127 | if (!sampled_value) |
| 128 | break; |
| 129 | } while (!stopwatch_expired(&sw)); |
| 130 | |
| 131 | if (sampled_value) { |
| 132 | saved_state = recovery_req; |
| 133 | display_pattern(WWR_RECOVERY_REQUEST); |
| 134 | printk(BIOS_INFO, "recovery requested\n"); |
| 135 | } else { |
| 136 | saved_state = wipeout_req; |
| 137 | } |
| 138 | } else { |
| 139 | saved_state = no_req; |
| 140 | display_pattern(WWR_NORMAL_BOOT); |
| 141 | } |
| 142 | |
| 143 | return saved_state; |
| 144 | } |
| 145 | |
| 146 | int get_recovery_mode_switch(void) |
| 147 | { |
| 148 | return get_switch_state() == recovery_req; |
| 149 | } |
| 150 | |
| 151 | int get_wipeout_mode_switch(void) |
| 152 | { |
| 153 | return get_switch_state() == wipeout_req; |
| 154 | } |
| 155 | |
| 156 | int get_write_protect_state(void) |
| 157 | { |
Joel Kitching | ae0fb76 | 2019-04-07 00:37:14 +0800 | [diff] [blame] | 158 | return !read_gpio(get_wp_status_gpio_pin()); |
Varadarajan Narayanan | 3bbd901 | 2016-03-02 13:15:47 +0530 | [diff] [blame] | 159 | } |