Debian Codegraph @

src/ec/google/chromeec/switches.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #include <bootmode.h>
4 #include <ec/google/chromeec/ec.h>
5 #include <elog.h>
6 #include <security/vboot/misc.h>
7
8 #if CONFIG(EC_GOOGLE_CHROMEEC_LPC)
9 /*
10 * Retrieves the state of the lid switch.
11 *
12 * The get_lid_switch() function checks if lid switch functionality is enabled
13 * in the coreboot configuration. If the VBOOT_LID_SWITCH Kconfig option is
14 * enabled, it retrieves the lid switch state from the Embedded Controller (EC).
15 *
16 * If the GBB_FLAG_DISABLE_LID_SHUTDOWN Kconfig option is enabled, this function
17 * will always return 1 (lid open), effectively faking the lid status. This is
18 * intended for stages post-romstage, particularly during display-related
19 * initialization, where lid-based shutdown should be suppressed.
20 *
21 * @return 1 if the lid is open, 0 if the lid is closed, or -1 if the lid switch
22 * functionality is not configured (VBOOT_LID_SWITCH is disabled).
23 */
24 int get_lid_switch(void)
25 {
26 if (!CONFIG(VBOOT_LID_SWITCH))
27 return -1;
28
29 /* If GBB_FLAG_DISABLE_LID_SHUTDOWN is enabled, fake the lid status as always open. */
30 if (!ENV_ROMSTAGE_OR_BEFORE && vboot_is_gbb_flag_set(VB2_GBB_FLAG_DISABLE_LID_SHUTDOWN))
31 return 1; /* Always return 1 (lid open) */
32
33 return !!(google_chromeec_get_switches() & EC_SWITCH_LID_OPEN);
34 }
35 #endif
36
37 int get_recovery_mode_switch(void)
38 {
39 /* Check for dedicated recovery switch first. */
40 if (CONFIG(EC_GOOGLE_CHROMEEC_LPC) &&
41 (google_chromeec_get_switches() & EC_SWITCH_DEDICATED_RECOVERY))
42 return 1;
43
44 /* Check if the EC has posted the keyboard recovery event. */
45 return !!(google_chromeec_get_events_b() &
46 EC_HOST_EVENT_MASK(EC_HOST_EVENT_KEYBOARD_RECOVERY));
47 }
48
49 int get_recovery_mode_retrain_switch(void)
50 {
51 /*
52 * Check if the EC has posted the keyboard recovery event with memory
53 * retrain.
54 */
55 return !!(google_chromeec_get_events_b() &
56 EC_HOST_EVENT_MASK(EC_HOST_EVENT_KEYBOARD_RECOVERY_HW_REINIT));
57 }
58
59 static void elog_add_recovery_mode_switch_event(void)
60 {
61 uint64_t events = google_chromeec_get_events_b();
62 uint8_t event_byte = EC_HOST_EVENT_KEYBOARD_RECOVERY;
63
64 if (!(events & EC_HOST_EVENT_MASK(EC_HOST_EVENT_KEYBOARD_RECOVERY)))
65 return;
66
67 if (events & EC_HOST_EVENT_MASK(EC_HOST_EVENT_KEYBOARD_RECOVERY_HW_REINIT))
68 event_byte = EC_HOST_EVENT_KEYBOARD_RECOVERY_HW_REINIT;
69
70 elog_add_event_byte(ELOG_TYPE_EC_EVENT, event_byte);
71 }
72
73 int clear_recovery_mode_switch(void)
74 {
75 /* Log elog event before clearing */
76 elog_add_recovery_mode_switch_event();
77
78 /* Clear all host event bits requesting recovery mode. */
79 return google_chromeec_clear_events_b(
80 EC_HOST_EVENT_MASK(EC_HOST_EVENT_KEYBOARD_RECOVERY) |
81 EC_HOST_EVENT_MASK(EC_HOST_EVENT_KEYBOARD_RECOVERY_HW_REINIT));
82 }