From 501e3101dd8d3d7c8a090fcc9424bfd74dbe5927 Mon Sep 17 00:00:00 2001 From: Randall Spangler Date: Fri, 4 Aug 2017 18:42:33 -0700 Subject: [PATCH] cr50: Merge BattPrsnt device handling to wp.c The device_state module is used for debouncing GPIO inputs to determine device state. It was overkill for managing the battery present state as forwarded to the write protect pin, and split that handling between 3 files (board.c, wp.c, device_state.c). Move all of that logic into wp.c so it's easier to maintain. BUG=none BRANCH=cr50 TEST=manual plug in battery (or ground DIOM2) wp command reports WP enabled unplug battery (or pull DIOM2 high) wp command reports WP disabled Change-Id: I71ab9ce5766ecddae430c63a8b31388935a46180 Signed-off-by: Randall Spangler Reviewed-on: https://chromium-review.googlesource.com/604500 Reviewed-by: Aseda Aboagye --- board/cr50/board.c | 36 ------------------------------------ board/cr50/board.h | 3 +-- board/cr50/wp.c | 40 +++++++++++++++++++++++++++++++++++----- board/cr50/wp.h | 7 ------- 4 files changed, 36 insertions(+), 50 deletions(-) diff --git a/board/cr50/board.c b/board/cr50/board.c index 00e612c16a..9ee29d2f9d 100644 --- a/board/cr50/board.c +++ b/board/cr50/board.c @@ -1081,11 +1081,6 @@ struct device_config device_states[] = { .detect = GPIO_DETECT_EC, .name = "EC" }, - [DEVICE_BATTERY_PRESENT] = { - .deferred = NULL, - .detect = GPIO_BATT_PRES_L, - .name = "BattPrsnt" - }, }; BUILD_ASSERT(ARRAY_SIZE(device_states) == DEVICE_COUNT); @@ -1184,37 +1179,6 @@ void device_state_on(enum gpio_signal signal) /* Note: This is called for every device once a second from the HOOK task */ void board_update_device_state(enum device_type device) { - if (device == DEVICE_BATTERY_PRESENT) { - /* - * Battery present pin does not have an interrupt handler, so - * this is the only way the battery present device state - * changes. - * - * This could potentially be moved to a HOOK_SECOND handler - * in wp.c, since nothing else cares. - */ - int bp = board_battery_is_present(); - - /* - * We use BATT_PRES_L as the source for write protect. However, - * since it can be overridden by a console command, only change - * the write protect state when the battery presence pin has - * changed and we're not forcing it. - */ - if (device_set_state(device, - bp ? - DEVICE_STATE_ON : DEVICE_STATE_OFF)) { - /* - * Only update the write protect state if we're not - * forcing it. - */ - if ((GREG32(PMU, LONG_LIFE_SCRATCH1) & BOARD_FORCING_WP) - == 0) - set_wp_state(bp); - } - return; - } - if (device == DEVICE_SERVO) { /* * If we're driving the transmit line to the EC UART, then we diff --git a/board/cr50/board.h b/board/cr50/board.h index 6909180e3d..456a8c9877 100644 --- a/board/cr50/board.h +++ b/board/cr50/board.h @@ -169,12 +169,11 @@ enum usb_strings { USB_STR_COUNT }; -/* Device indexes */ +/* Device indexes for devices that require debouncing */ enum device_type { DEVICE_AP = 0, DEVICE_EC, DEVICE_SERVO, - DEVICE_BATTERY_PRESENT, DEVICE_COUNT }; diff --git a/board/cr50/wp.c b/board/cr50/wp.c index 615e42cdd5..17714307d8 100644 --- a/board/cr50/wp.c +++ b/board/cr50/wp.c @@ -35,7 +35,12 @@ int board_battery_is_present(void) return !gpio_get_level(GPIO_BATT_PRES_L); } -void set_wp_state(int asserted) +/** + * Set the current write protect state in RBOX and long life scratch register. + * + * @param asserted: 0 to disable write protect, otherwise enable write protect. + */ +static void set_wp_state(int asserted) { /* Enable writing to the long life register */ GWRITE_FIELD(PMU, LONG_LIFE_SCRATCH_WR_EN, REG1, 1); @@ -52,6 +57,33 @@ void set_wp_state(int asserted) GWRITE_FIELD(PMU, LONG_LIFE_SCRATCH_WR_EN, REG1, 0); } +/** + * Return the current WP state + * + * @return 0 if WP deasserted, 1 if WP asserted + */ +static int get_wp_state(void) +{ + /* Signal is active low, so invert */ + return !GREG32(RBOX, EC_WP_L); +} + +static void check_wp_battery_presence(void) +{ + int bp = board_battery_is_present(); + + /* If we're forcing WP, ignore battery detect */ + if (GREG32(PMU, LONG_LIFE_SCRATCH1) & BOARD_FORCING_WP) + return; + + /* Otherwise, mirror battery */ + if (bp != get_wp_state()) { + CPRINTS("WP %d", bp); + set_wp_state(bp); + } +} +DECLARE_HOOK(HOOK_SECOND, check_wp_battery_presence, HOOK_PRIO_DEFAULT); + /** * Force write protect state or follow battery presence. * @@ -78,7 +110,7 @@ static void force_write_protect(int force, int wp_en) GWRITE_FIELD(PMU, LONG_LIFE_SCRATCH_WR_EN, REG1, 0); /* Update the WP state. */ - set_wp_state(!!wp_en); + set_wp_state(wp_en); } static int command_wp(int argc, char **argv) @@ -108,11 +140,9 @@ static int command_wp(int argc, char **argv) } } - /* Invert, because active low */ - val = !GREG32(RBOX, EC_WP_L); forced = GREG32(PMU, LONG_LIFE_SCRATCH1) & BOARD_FORCING_WP; ccprintf("Flash WP: %s%s\n", forced ? "forced " : "", - val ? "enabled" : "disabled"); + get_wp_state() ? "enabled" : "disabled"); ccprintf(" at boot: "); if (ccd_get_flag(CCD_FLAG_OVERRIDE_WP_AT_BOOT)) diff --git a/board/cr50/wp.h b/board/cr50/wp.h index ff0cb174b5..6d93145ba6 100644 --- a/board/cr50/wp.h +++ b/board/cr50/wp.h @@ -15,13 +15,6 @@ */ void init_wp_state(void); -/** - * Set the current write protect state in RBOX and long life scratch register. - * - * @param asserted: 0 to disable write protect, otherwise enable write protect. - */ -void set_wp_state(int asserted); - /** * Read the FWMP value from TPM NVMEM and set the console restriction * appropriately.