From f7aec0ceb52f3e40460bfe272040b7079ab56834 Mon Sep 17 00:00:00 2001 From: Wai-Hong Tam Date: Thu, 15 Mar 2018 09:57:21 -0700 Subject: cheza: Add SDM845 power sequence for rev-0 board This is the power sequence for rev-0 board. Confirmed the behavior of reprogramming the PMIC registers to enable the instant reset and shutdown. BRANCH=none BUG=b:74395451 TEST=make buildall -j TEST=Tried the following cases: * Cold reset: $ dut-control cold_reset:on sleep:0.2 cold_reset:off Result: G3 -> S0 * Long power press to shutdown: $ dut-control pwr_button:press sleep:8.2 pwr_button:release Result: S0 -> S5 -> G3 * Long power press to power-on but then shutdown: $ dut-control pwr_button:press sleep:8.2 pwr_button:release Result: G3 -> S0 -> S5 -> G3 * Short power press to power-on: $ dut-control pwr_button:press sleep:0.2 pwr_button:release Result: G3 -> S0 * Console command: apreset Result: S0 -> S5 -> S0 * Console command: power off Result: S0 -> S5 -> G3 * Console command: power on Result: G3 -> S0 * Console command: apshutdown Result: S0 -> S5 -> G3 * Lid open to power-on: $ dut-control lid_open:no sleep:0.2 lid_open:yes Result: G3 -> S0 Change-Id: Ia9d44b1dccac66b5b580c08c6c1697ef5989b923 Signed-off-by: Wai-Hong Tam Signed-off-by: Alexandru M Stan Reviewed-on: https://chromium-review.googlesource.com/969702 --- board/cheza/board.c | 21 ++ board/cheza/board.h | 10 + board/cheza/ec.tasklist | 1 + board/cheza/gpio.inc | 5 +- include/config.h | 1 + power/build.mk | 1 + power/sdm845.c | 640 ++++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 677 insertions(+), 2 deletions(-) create mode 100644 power/sdm845.c diff --git a/board/cheza/board.c b/board/cheza/board.c index cd9e334216..f2c53de96b 100644 --- a/board/cheza/board.c +++ b/board/cheza/board.c @@ -13,6 +13,7 @@ #include "i2c.h" #include "lid_switch.h" #include "pi3usb9281.h" +#include "power.h" #include "power_button.h" #include "switch.h" @@ -33,6 +34,26 @@ const int hibernate_wake_pins_used = ARRAY_SIZE(hibernate_wake_pins); const struct adc_t adc_channels[] = {}; +/* Power signal list. Must match order of enum power_signal. */ +/* + * PMIC pulls up the AP_RST_L signal to power-on AP. Once AP is up, it then + * pulls up the PS_HOLD signal. + * + * +--> GPIO_AP_RST_L >--+ + * PMIC AP + * +--< GPIO_PS_HOLD <---+ + * + * When AP initiates shutdown, it pulls down the PS_HOLD signal to notify + * PMIC. PMIC then pulls down the AP_RST_L. + * + * TODO(b/78455067): By far, we use the AP_RST_L signal to indicate AP in a + * good state. Address the issue of AP-initiated warm reset. + */ +const struct power_signal_info power_signal_list[] = { + {GPIO_AP_RST_L, POWER_SIGNAL_ACTIVE_HIGH, "POWER_GOOD"}, +}; +BUILD_ASSERT(ARRAY_SIZE(power_signal_list) == POWER_SIGNAL_COUNT); + /* I2C port map */ const struct i2c_port_t i2c_ports[] = { {"power", I2C_PORT_POWER, 400, GPIO_I2C0_SCL, GPIO_I2C0_SDA}, diff --git a/board/cheza/board.h b/board/cheza/board.h index d4c0be0c79..19dac3bad7 100644 --- a/board/cheza/board.h +++ b/board/cheza/board.h @@ -36,6 +36,10 @@ #define CONFIG_LID_SWITCH #define CONFIG_EXTPOWER_GPIO +#define CONFIG_CHIPSET_SDM845 +#define CONFIG_CHIPSET_RESET_HOOK +#define CONFIG_POWER_COMMON + /* NPCX Features */ #define CONFIG_HIBERNATE_PSL @@ -53,6 +57,12 @@ #include "gpio_signal.h" #include "registers.h" +enum power_signal { + SDM845_POWER_GOOD = 0, + /* Number of power signals */ + POWER_SIGNAL_COUNT +}; + enum adc_channel { ADC_VBUS = -1, ADC_CH_COUNT diff --git a/board/cheza/ec.tasklist b/board/cheza/ec.tasklist index 2e6fdac6e2..fa12c7f617 100644 --- a/board/cheza/ec.tasklist +++ b/board/cheza/ec.tasklist @@ -22,5 +22,6 @@ #define CONFIG_TASK_LIST \ TASK_ALWAYS(HOOKS, hook_task, NULL, LARGER_TASK_STACK_SIZE) \ + TASK_NOTEST(CHIPSET, chipset_task, NULL, LARGER_TASK_STACK_SIZE) \ TASK_ALWAYS(HOSTCMD, host_command_task, NULL, LARGER_TASK_STACK_SIZE) \ TASK_ALWAYS(CONSOLE, console_task, NULL, LARGER_TASK_STACK_SIZE) diff --git a/board/cheza/gpio.inc b/board/cheza/gpio.inc index b824d6c7c4..8c89555bbc 100644 --- a/board/cheza/gpio.inc +++ b/board/cheza/gpio.inc @@ -13,6 +13,8 @@ GPIO_INT(VOLUME_DOWN_L, PIN(7, 0), GPIO_INT_BOTH | GPIO_PULL_UP, button_inte GPIO_INT(VOLUME_UP_L, PIN(1, 1), GPIO_INT_BOTH | GPIO_PULL_UP, button_interrupt) /* EC_VOLUP_BTN_ODL */ GPIO_INT(WP_L, PIN(A, 1), GPIO_INT_BOTH, switch_interrupt) /* EC_WP_ODL */ GPIO_INT(LID_OPEN, PIN(D, 2), GPIO_INT_BOTH, lid_interrupt) /* LID_OPEN_EC */ +/* AP_RST_L is used for PMIC and AP negotiation. Don't change its state. */ +GPIO_INT(AP_RST_L, PIN(C, 1), GPIO_INT_BOTH, power_signal_interrupt) /* * Should always make it low, to prevent AP unexpected up. @@ -21,8 +23,7 @@ GPIO_INT(LID_OPEN, PIN(D, 2), GPIO_INT_BOTH, lid_interrupt) /* LID_OPE GPIO(AC_PRESENT, PIN(0, 0), GPIO_OUT_LOW) /* ACOK_OD: PMIC power-on trigger PON_1 */ GPIO(EC_SELF_RST, PIN(E, 0), GPIO_OUT_LOW) /* Self-reset EC */ GPIO(SYS_RST_L, PIN(0, 2), GPIO_ODR_HIGH) /* PMIC reset trigger */ -/* AP_RST_L and PS_HOLD are used for PMIC and AP negotiation. Don't change their states. */ -GPIO(AP_RST_L, PIN(C, 1), GPIO_INPUT) /* AP reset trigger, controlled by PMIC only */ +/* PS_HOLD is used for PMIC and AP negotiation. Don't change its state. */ GPIO(PS_HOLD, PIN(D, 4), GPIO_INPUT) /* Indicate when AP triggers reset/shutdown */ GPIO(ENTERING_RW, PIN(E, 1), GPIO_OUT_LOW) /* EC_ENTERING_RW: Indicate when EC is entering RW code */ GPIO(CCD_MODE_ODL, PIN(E, 3), GPIO_INPUT) /* Case Closed Debug Mode */ diff --git a/include/config.h b/include/config.h index 9d8d2de1cc..4c343acbb0 100644 --- a/include/config.h +++ b/include/config.h @@ -730,6 +730,7 @@ /* TODO: Rename below config to CONFIG_CHIPSET_RK32XX */ #undef CONFIG_CHIPSET_ROCKCHIP /* Rockchip rk32xx */ #undef CONFIG_CHIPSET_SKYLAKE /* Intel Skylake (x86) */ +#undef CONFIG_CHIPSET_SDM845 /* Qualcomm SDM845 */ #undef CONFIG_CHIPSET_STONEY /* AMD Stoney (x86)*/ /* Shared chipset support; automatically gets defined below. */ diff --git a/power/build.mk b/power/build.mk index e24ea80cee..6eb5d2d454 100644 --- a/power/build.mk +++ b/power/build.mk @@ -13,6 +13,7 @@ power-$(CONFIG_CHIPSET_ECDRIVEN)+=ec_driven.o power-$(CONFIG_CHIPSET_MEDIATEK)+=mediatek.o power-$(CONFIG_CHIPSET_RK3399)+=rk3399.o power-$(CONFIG_CHIPSET_ROCKCHIP)+=rockchip.o +power-$(CONFIG_CHIPSET_SDM845)+=sdm845.o power-$(CONFIG_CHIPSET_SKYLAKE)+=skylake.o intel_x86.o power-$(CONFIG_CHIPSET_STONEY)+=stoney.o power-$(CONFIG_POWER_COMMON)+=common.o diff --git a/power/sdm845.c b/power/sdm845.c new file mode 100644 index 0000000000..4e96c1385e --- /dev/null +++ b/power/sdm845.c @@ -0,0 +1,640 @@ +/* Copyright 2018 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +/* + * SDM845 SoC power sequencing module for Chrome EC + * + * This implements the following features: + * + * - Cold reset powers on the AP + * + * When powered off: + * - Press power button turns on the AP + * - Hold power button turns on the AP, and then 8s later turns it off and + * leaves it off until pwron is released and pressed again + * - Lid open turns on the AP + * + * When powered on: + * - Holding power button for 8s powers off the AP + * - Pressing and releasing pwron within that 8s is ignored + * - If POWER_GOOD is dropped by the AP, then we power the AP off + */ + +#include "chipset.h" +#include "common.h" +#include "gpio.h" +#include "hooks.h" +#include "lid_switch.h" +#include "power.h" +#include "power_button.h" +#include "system.h" +#include "task.h" +#include "util.h" + +#define CPRINTS(format, args...) cprints(CC_CHIPSET, format, ## args) + +/* Masks for power signals */ +#define IN_POWER_GOOD POWER_SIGNAL_MASK(SDM845_POWER_GOOD) + +/* Long power key press to force shutdown */ +#define DELAY_FORCE_SHUTDOWN (8 * SECOND) + +/* + * If the power button is pressed to turn on, then held for this long, we + * power off. + * + * Normal case: User releases power button and chipset_task() goes + * into the inner loop, waiting for next event to occur (power button + * press or POWER_GOOD == 0). + */ +#define DELAY_SHUTDOWN_ON_POWER_HOLD (8 * SECOND) + +/* + * After trigger PMIC power-on, how long it triggers AP to turn on. + * Obversed that the worst case is ~150ms. Pick a safe vale. + */ +#define PMIC_POWER_AP_RESPONSE_TIMEOUT (350 * MSEC) + +/* Wait for polling the AP on signal */ +#define PMIC_POWER_AP_WAIT (1 * MSEC) + +/* Delay between power-on the system and power-on the PMIC */ +#define SYSTEM_POWER_ON_DELAY (10 * MSEC) + +/* Delay between power-off the system and all things (PMIC/AP) expected off */ +#define SYSTEM_POWER_OFF_DELAY (350 * MSEC) + +/* TODO(crosbug.com/p/25047): move to HOOK_POWER_BUTTON_CHANGE */ +/* 1 if the power button was pressed last time we checked */ +static char power_button_was_pressed; + +/* 1 if lid-open event has been detected */ +static char lid_opened; + +/* Time where we will power off, if power button still held down */ +static timestamp_t power_off_deadline; + +/* Force AP power on (used for recovery keypress) */ +static int auto_power_on; + +enum power_request_t { + POWER_REQ_NONE, + POWER_REQ_OFF, + POWER_REQ_ON, + + POWER_REQ_COUNT, +}; + +static enum power_request_t power_request; + +/** + * Return values for check_for_power_off_event(). + */ +enum power_off_event_t { + POWER_OFF_CANCEL, + POWER_OFF_BY_POWER_BUTTON_PRESSED, + POWER_OFF_BY_LONG_PRESS, + POWER_OFF_BY_POWER_GOOD_LOST, + POWER_OFF_BY_POWER_REQ, + + POWER_OFF_EVENT_COUNT, +}; + +/** + * Return values for check_for_power_on_event(). + */ +enum power_on_event_t { + POWER_ON_CANCEL, + POWER_ON_BY_IN_POWER_GOOD, + POWER_ON_BY_AUTO_POWER_ON, + POWER_ON_BY_LID_OPEN, + POWER_ON_BY_POWER_BUTTON_PRESSED, + POWER_ON_BY_POWER_REQ_NONE, + + POWER_ON_EVENT_COUNT, +}; + +static void sdm845_lid_event(void) +{ + /* Power task only cares about lid-open events */ + if (!lid_is_open()) + return; + + lid_opened = 1; + task_wake(TASK_ID_CHIPSET); +} +DECLARE_HOOK(HOOK_LID_CHANGE, sdm845_lid_event, HOOK_PRIO_DEFAULT); + +static void powerbtn_sdm845_changed(void) +{ + task_wake(TASK_ID_CHIPSET); +} +DECLARE_HOOK(HOOK_POWER_BUTTON_CHANGE, powerbtn_sdm845_changed, + HOOK_PRIO_DEFAULT); + +/** + * Set the state of the system power signals. + * + * The system power signals are the enable pins of SwitchCap and VBOB. + * They control the power of the set of PMIC chips and the AP. + * + * @param enable 1 to enable or 0 to disable + */ +static void set_system_power(int enable) +{ + CPRINTS("set_system_power(%d)", enable); + gpio_set_level(GPIO_SWITCHCAP_ON_L, enable); + gpio_set_level(GPIO_VBOB_EN, enable); +} + +/** + * Get the state of the system power signals. + * + * @return 1 if the system is powered, 0 if not + */ +static int is_system_powered(void) +{ + return gpio_get_level(GPIO_SWITCHCAP_ON_L); +} + +/** + * Get the PMIC/AP power signal. + * + * We treat the PMIC chips and the AP as a whole here. Don't deal with + * the individual chip. + * + * @return 1 if the PMIC/AP is powered, 0 if not + */ +static int is_pmic_pwron(void) +{ + /* Use PS_HOLD to indicate PMIC/AP is on/off */ + return gpio_get_level(GPIO_PS_HOLD); +} + +/** + * Wait the PMIC/AP power-on state. + * + * @param enable 1 to wait the PMIC/AP on. + 0 to wait the PMIC/AP off. + */ +static void wait_pmic_pwron(int enable) +{ + timestamp_t poll_deadline; + + /* Check the AP power status */ + if (enable == is_pmic_pwron()) + return; + + poll_deadline = get_time(); + poll_deadline.val += PMIC_POWER_AP_RESPONSE_TIMEOUT; + while (enable != is_pmic_pwron() && + get_time().val < poll_deadline.val) { + usleep(PMIC_POWER_AP_WAIT); + } + + /* Check the timeout case */ + if (enable != is_pmic_pwron()) { + if (enable) + CPRINTS("AP POWER NOT READY!"); + else + CPRINTS("AP POWER STILL UP!"); + } +} + +/** + * Set the PMIC/AP power-on state. + * + * It triggers the PMIC/AP power-on and power-off sequence. + * + * @param enable 1 to power the PMIC/AP on. + 0 to power the PMIC/AP off. + */ +static void set_pmic_pwron(int enable) +{ + CPRINTS("set_pmic_pwron(%d)", enable); + + /* Check the PMIC/AP power state */ + if (enable == is_pmic_pwron()) + return; + + /* + * Power-on sequence: + * 1. Hold down PMIC_KPD_PWR_ODL, which is a power-on trigger + * 2. PM845 pulls up AP_RST_L signal to power-on SDM845 + * 3. SDM845 pulls up PS_HOLD signal + * 4. Wait for PS_HOLD up + * 5. Release PMIC_KPD_PWR_ODL + * + * Power-off sequence: + * 1. Hold down PMIC_KPD_PWR_ODL and SYS_RST_L, which is a power-off + * trigger (requiring reprogramming PMIC registers to make + * PMIC_KPD_PWR_ODL + SYS_RST_L as a shutdown trigger) + * 2. PM845 pulls down AP_RST_L signal to power-off SDM845 (requreing + * reprogramming PMIC to set the stage-1 and stage-2 reset timers to + * 0 such that the pull down happens just after the deboucing time + * of the trigger, like 2ms) + * 3. SDM845 pulls down PS_HOLD signal + * 4. Wait for PS_HOLD down + * 5. Release PMIC_KPD_PWR_ODL and SYS_RST_L + * + * If the above PMIC registers not programmed or programmed wrong, it + * falls back to the next functions, which cuts off the system power. + */ + + gpio_set_level(GPIO_PMIC_KPD_PWR_ODL, 0); + if (!enable) + gpio_set_level(GPIO_SYS_RST_L, 0); + wait_pmic_pwron(enable); + gpio_set_level(GPIO_PMIC_KPD_PWR_ODL, 1); + if (!enable) + gpio_set_level(GPIO_SYS_RST_L, 1); +} + +enum power_state power_chipset_init(void) +{ + int init_power_state; + uint32_t reset_flags = system_get_reset_flags(); + + /* + * Force the AP shutdown unless we are doing SYSJUMP. Otherwise, + * the AP could stay in strange state. + */ + if (!(reset_flags & RESET_FLAG_SYSJUMP)) { + CPRINTS("not sysjump; forcing system shutdown"); + set_system_power(0); + init_power_state = POWER_G3; + } else { + /* In the SYSJUMP case, we check if the AP is on */ + if (power_get_signals() & IN_POWER_GOOD) { + CPRINTS("SOC ON"); + init_power_state = POWER_S0; + } else { + CPRINTS("SOC OFF"); + init_power_state = POWER_G3; + } + } + + /* Leave power off only if requested by reset flags */ + if (!(reset_flags & RESET_FLAG_AP_OFF) && + !(reset_flags & RESET_FLAG_SYSJUMP)) { + CPRINTS("auto_power_on set due to reset_flag 0x%x", + system_get_reset_flags()); + auto_power_on = 1; + } + + /* + * TODO(crosbug.com/p/28289): Wait battery stable. + * Some batteries use clock stretching feature, which requires + * more time to be stable. + */ + + return init_power_state; +} + +/*****************************************************************************/ + +/** + * Power off the AP + */ +static void power_off(void) +{ + /* Check the power off status */ + if (!is_system_powered()) + return; + + /* Call hooks before we drop power rails */ + hook_notify(HOOK_CHIPSET_SHUTDOWN); + + /* Do a graceful way to shutdown PMIC/AP first */ + set_pmic_pwron(0); + + /* Force to switch off all rails */ + set_system_power(0); + + /* Wait longer to ensure the PMIC/AP totally off */ + usleep(SYSTEM_POWER_OFF_DELAY); + + lid_opened = 0; + enable_sleep(SLEEP_MASK_AP_RUN); + CPRINTS("power shutdown complete"); +} + +/** + * Power on the AP + */ +static void power_on(void) +{ + /* + * When power_on() is called, we are at S5S3. Initialize components + * to ready state before AP is up. + */ + hook_notify(HOOK_CHIPSET_PRE_INIT); + + set_system_power(1); + usleep(SYSTEM_POWER_ON_DELAY); + set_pmic_pwron(1); + + disable_sleep(SLEEP_MASK_AP_RUN); + + CPRINTS("AP running ..."); +} + +/** + * Check if there has been a power-on event + * + * This checks all power-on event signals and returns non-zero if any have been + * triggered (with debounce taken into account). + * + * @return non-zero if there has been a power-on event, 0 if not. + */ +static int check_for_power_on_event(void) +{ + int ap_off_flag; + + ap_off_flag = system_get_reset_flags() & RESET_FLAG_AP_OFF; + system_clear_reset_flags(RESET_FLAG_AP_OFF); + /* check if system is already ON */ + if (power_get_signals() & IN_POWER_GOOD) { + if (ap_off_flag) { + CPRINTS("system is on, but RESET_FLAG_AP_OFF is on"); + return POWER_ON_CANCEL; + } + CPRINTS("system is on, thus clear auto_power_on"); + /* no need to arrange another power on */ + auto_power_on = 0; + return POWER_ON_BY_IN_POWER_GOOD; + } + if (ap_off_flag) { + CPRINTS("RESET_FLAG_AP_OFF is on"); + power_off(); + return POWER_ON_CANCEL; + } + + CPRINTS("POWER_GOOD is not asserted"); + + /* power on requested at EC startup for recovery */ + if (auto_power_on) { + auto_power_on = 0; + return POWER_ON_BY_AUTO_POWER_ON; + } + + /* Check lid open */ + if (lid_opened) { + lid_opened = 0; + return POWER_ON_BY_LID_OPEN; + } + + /* check for power button press */ + if (power_button_is_pressed()) + return POWER_ON_BY_POWER_BUTTON_PRESSED; + + if (power_request == POWER_REQ_ON) { + power_request = POWER_REQ_NONE; + return POWER_ON_BY_POWER_REQ_NONE; + } + + return POWER_OFF_CANCEL; +} + +/** + * Check for some event triggering the shutdown. + * + * It can be either a long power button press or a shutdown triggered from the + * AP and detected by reading POWER_GOOD. + * + * @return non-zero if a shutdown should happen, 0 if not + */ +static int check_for_power_off_event(void) +{ + timestamp_t now; + int pressed = 0; + + /* + * Check for power button press. + */ + if (power_button_is_pressed()) { + pressed = POWER_OFF_BY_POWER_BUTTON_PRESSED; + } else if (power_request == POWER_REQ_OFF) { + power_request = POWER_REQ_NONE; + /* return non-zero for shudown down */ + return POWER_OFF_BY_POWER_REQ; + } + + now = get_time(); + if (pressed) { + if (!power_button_was_pressed) { + power_off_deadline.val = now.val + DELAY_FORCE_SHUTDOWN; + CPRINTS("power waiting for long press %u", + power_off_deadline.le.lo); + /* Ensure we will wake up to check the power key */ + timer_arm(power_off_deadline, TASK_ID_CHIPSET); + } else if (timestamp_expired(power_off_deadline, &now)) { + power_off_deadline.val = 0; + CPRINTS("power off after long press now=%u, %u", + now.le.lo, power_off_deadline.le.lo); + return POWER_OFF_BY_LONG_PRESS; + } + } else if (power_button_was_pressed) { + CPRINTS("power off cancel"); + timer_cancel(TASK_ID_CHIPSET); + } + + power_button_was_pressed = pressed; + + /* POWER_GOOD released by AP : shutdown immediately */ + if (!power_has_signals(IN_POWER_GOOD)) { + if (power_button_was_pressed) + timer_cancel(TASK_ID_CHIPSET); + + CPRINTS("POWER_GOOD is lost"); + return POWER_OFF_BY_POWER_GOOD_LOST; + } + + return POWER_OFF_CANCEL; +} + +/*****************************************************************************/ +/* Chipset interface */ + +void chipset_force_shutdown(void) +{ + CPRINTS("EC triggered shutdown"); + power_off(); + + /* Clean-up internal variable */ + power_request = POWER_REQ_NONE; +} + +void chipset_reset(void) +{ + /* + * Before we can reprogram the PMIC to make the PMIC RESIN_N pin as + * reset pin and zero-latency. We do cold reset instead. + */ + CPRINTS("EC triggered cold reboot"); + power_off(); + + /* Issue a request to initiate a power-on sequence */ + power_request = POWER_REQ_ON; + task_wake(TASK_ID_CHIPSET); +} + +/** + * Power handler for steady states + * + * @param state Current power state + * @return Updated power state + */ +enum power_state power_handle_state(enum power_state state) +{ + int value; + static int boot_from_g3; + + switch (state) { + case POWER_G3: + boot_from_g3 = check_for_power_on_event(); + if (boot_from_g3) + return POWER_G3S5; + break; + + case POWER_G3S5: + return POWER_S5; + + case POWER_S5: + if (boot_from_g3) { + value = boot_from_g3; + boot_from_g3 = 0; + } else { + value = check_for_power_on_event(); + } + + if (value) { + CPRINTS("power on %d", value); + return POWER_S5S3; + } + break; + + case POWER_S5S3: + power_on(); + if (power_wait_signals(IN_POWER_GOOD) == EC_SUCCESS) { + CPRINTS("POWER_GOOD seen"); + if (power_button_wait_for_release( + DELAY_SHUTDOWN_ON_POWER_HOLD) == + EC_SUCCESS) { + power_button_was_pressed = 0; + + /* Call hooks now that AP is running */ + hook_notify(HOOK_CHIPSET_STARTUP); + + return POWER_S3; + } + CPRINTS("long-press button, shutdown"); + power_off(); + /* + * Since the AP may be up already, return S0S3 + * state to go through the suspend hook. + */ + return POWER_S0S3; + } + CPRINTS("POWER_GOOD not seen in time"); + set_system_power(0); + return POWER_S5; + + case POWER_S3: + if (!(power_get_signals() & IN_POWER_GOOD)) + return POWER_S3S5; + + /* Go to S3S0 directly, as don't know if it is in suspend */ + return POWER_S3S0; + + case POWER_S3S0: + hook_notify(HOOK_CHIPSET_RESUME); + return POWER_S0; + + case POWER_S0: + value = check_for_power_off_event(); + if (value) { + CPRINTS("power off %d", value); + power_off(); + return POWER_S0S3; + } + break; + + case POWER_S0S3: + /* + * If the power button is pressing, we need cancel the long + * press timer, otherwise EC will crash. + */ + if (power_button_was_pressed) + timer_cancel(TASK_ID_CHIPSET); + + /* Call hooks here since we don't know it prior to AP suspend */ + hook_notify(HOOK_CHIPSET_SUSPEND); + return POWER_S3; + + case POWER_S3S5: + power_button_wait_for_release(-1); + power_button_was_pressed = 0; + return POWER_S5; + + case POWER_S5G3: + return POWER_G3; + } + + return state; +} + +/*****************************************************************************/ +/* Console debug command */ + +static const char *power_req_name[POWER_REQ_COUNT] = { + "none", + "off", + "on", +}; + +/* Power states that we can report */ +enum power_state_t { + PSTATE_UNKNOWN, + PSTATE_OFF, + PSTATE_ON, + PSTATE_COUNT, +}; + +static const char * const state_name[] = { + "unknown", + "off", + "on", +}; + +static int command_power(int argc, char **argv) +{ + int v; + + if (argc < 2) { + enum power_state_t state; + + state = PSTATE_UNKNOWN; + if (chipset_in_state(CHIPSET_STATE_ANY_OFF)) + state = PSTATE_OFF; + if (chipset_in_state(CHIPSET_STATE_ON)) + state = PSTATE_ON; + ccprintf("%s\n", state_name[state]); + + return EC_SUCCESS; + } + + if (!parse_bool(argv[1], &v)) + return EC_ERROR_PARAM1; + + power_request = v ? POWER_REQ_ON : POWER_REQ_OFF; + ccprintf("Requesting power %s\n", power_req_name[power_request]); + task_wake(TASK_ID_CHIPSET); + + return EC_SUCCESS; +} +DECLARE_CONSOLE_COMMAND(power, command_power, + "on/off", + "Turn AP power on/off"); -- cgit v1.2.1