From 9ceaa9d8e0706a4a7f416e76d8b0248579b54939 Mon Sep 17 00:00:00 2001 From: Shelley Chen Date: Fri, 10 Jun 2016 10:08:01 -0700 Subject: rk3399: kevin: Adding get_rtc_alarm functionality. Adding ability to get # seconds before rtc alarm goes off. BUG=chrome-os-partner:52218 BRANCH=None TEST=ectool rtcgetalarm w/o setting returns Alarm not set. ectool rtcsetalarm 30; ectool rtcgetalarm to make sure counting down to 0. After alarm goes off, rtcgetalarm should return alarm not set again. rtcsetalarm 30; rtcgetalarm to check alarm is set. rtcsetalarm 0; should disable alarm. Use rtcgetalarm to ensure that alarm is disabled. Change-Id: I176b12fe2dda08eedd23ea33dc64785f09f1d9ae Signed-off-by: Shelley Chen Reviewed-on: https://chromium-review.googlesource.com/353331 Reviewed-by: Shawn N --- chip/npcx/system.c | 43 +++++++++++++++++++++++++++++++++++++++++-- util/ectool.c | 30 ++++++++++++++++++++++++++---- 2 files changed, 67 insertions(+), 6 deletions(-) diff --git a/chip/npcx/system.c b/chip/npcx/system.c index 13695acf73..f014acb455 100644 --- a/chip/npcx/system.c +++ b/chip/npcx/system.c @@ -412,13 +412,20 @@ static char system_to_hex(uint8_t x) /*****************************************************************************/ /* IC specific low-level driver */ -/* Microsecond will be ignore for hardware limitation */ +/* + * Microseconds will be ignored. The WTC register only + * stores wakeup time in seconds. + * Set seconds = 0 to disable the alarm + */ +#define EC_RTC_ALARM_CLEAR 0 void system_set_rtc_alarm(uint32_t seconds, uint32_t microseconds) { uint32_t cur_secs, alarm_secs; - if (seconds == 0) + if (seconds == EC_RTC_ALARM_CLEAR) { + system_reset_rtc_alarm(); return; + } /* Get current clock */ cur_secs = NPCX_TTC; @@ -458,6 +465,25 @@ void system_reset_rtc_alarm(void) task_disable_irq(NPCX_IRQ_MTC_WKINTAD_0); } +/* + * Return the seconds remaining before the RTC alarm goes off. + * Returns 0 if alarm is not set. + */ +uint32_t system_get_rtc_alarm(void) +{ + /* + * Return 0: + * 1. If alarm is not set to go off, OR + * 2. If alarm is set and has already gone off + */ + if (!IS_BIT_SET(NPCX_WTC, NPCX_WTC_WIE) || + IS_BIT_SET(NPCX_WTC, NPCX_WTC_PTO)) { + return 0; + } + /* Get seconds before alarm goes off */ + return (NPCX_WTC - NPCX_TTC) & MTC_ALARM_MASK; +} + /** * Enable hibernate interrupt */ @@ -780,6 +806,19 @@ DECLARE_HOST_COMMAND(EC_CMD_RTC_SET_ALARM, system_rtc_set_alarm, EC_VER_MASK(0)); +static int system_rtc_get_alarm(struct host_cmd_handler_args *args) +{ + struct ec_response_rtc *r = args->response; + + r->time = system_get_rtc_alarm(); + args->response_size = sizeof(*r); + + return EC_RES_SUCCESS; +} +DECLARE_HOST_COMMAND(EC_CMD_RTC_GET_ALARM, + system_rtc_get_alarm, + EC_VER_MASK(0)); + #ifdef CONFIG_EXTERNAL_STORAGE void system_jump_to_booter(void) { diff --git a/util/ectool.c b/util/ectool.c index 8de07f9eee..4b83f3fc04 100644 --- a/util/ectool.c +++ b/util/ectool.c @@ -189,12 +189,14 @@ const char help_str[] = " Reads a pattern from the EC via LPC\n" " reboot_ec [at-shutdown]\n" " Reboot EC to RO or RW\n" - " rtcalarm \n" - " Set real-time clock alarm to go off in seconds\n" " rtcget\n" " Print real-time clock\n" + " rtcgetalarm\n" + " Print # of seconds before real-time clock alarm goes off.\n" " rtcset