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 <rspangler@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/604500
Reviewed-by: Aseda Aboagye <aaboagye@chromium.org>
This commit is contained in:
Randall Spangler
2017-08-04 18:42:33 -07:00
committed by chrome-bot
parent bb66df5399
commit 501e3101dd
4 changed files with 36 additions and 50 deletions

View File

@@ -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

View File

@@ -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
};

View File

@@ -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))

View File

@@ -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.