Patrick Rudolph has uploaded this change for review. ( https://review.coreboot.org/23585
Change subject: libpayload/drivers/nvram: Add function to write RTC ......................................................................
libpayload/drivers/nvram: Add function to write RTC
Add a function to set the RTC to provided struct tm.
Change-Id: I17b4c1ee0dcc649738ac6a7400b087d07213eaf0 Signed-off-by: Patrick Rudolph siro@das-labor.org --- M payloads/libpayload/drivers/nvram.c M payloads/libpayload/include/libpayload.h 2 files changed, 52 insertions(+), 0 deletions(-)
git pull ssh://review.coreboot.org:29418/coreboot refs/changes/85/23585/1
diff --git a/payloads/libpayload/drivers/nvram.c b/payloads/libpayload/drivers/nvram.c index 9d3a9cd..6e2ebdc 100644 --- a/payloads/libpayload/drivers/nvram.c +++ b/payloads/libpayload/drivers/nvram.c @@ -160,3 +160,54 @@ if (time->tm_year < 80) time->tm_year += 100; } + +/** + * Write the current time and date to the RTC + * + * @param time A pointer to a broken-down time structure + */ +void rtc_write_clock(struct tm *time) +{ + u16 timeout = 10000; + u8 statusB; + u8 reg8; + + while (nvram_updating()) + if (!timeout--) + return; + + statusB = nvram_read(NVRAM_RTC_STATUSB); + + if (time->tm_year > 100) + time->tm_year -= 100; + + if (!(statusB & 0x04)) { + nvram_write(dec2bcd(time->tm_mon + 1), NVRAM_RTC_MONTH); + nvram_write(dec2bcd(time->tm_sec), NVRAM_RTC_SECONDS); + nvram_write(dec2bcd(time->tm_min), NVRAM_RTC_MINUTES); + nvram_write(dec2bcd(time->tm_mday), NVRAM_RTC_DAY); + if (!(statusB & 0x02)) { + if (time->tm_hour > 12) + reg8 = dec2bcd(time->tm_hour - 12) | 0x80; + else + reg8 = dec2bcd(time->tm_hour); + } else + reg8 = dec2bcd(time->tm_hour); + nvram_write(reg8, NVRAM_RTC_HOURS); + nvram_write(dec2bcd(time->tm_year), NVRAM_RTC_YEAR); + } else { + nvram_write(time->tm_mon + 1, NVRAM_RTC_MONTH); + nvram_write(time->tm_sec, NVRAM_RTC_SECONDS); + nvram_write(time->tm_min, NVRAM_RTC_MINUTES); + nvram_write(time->tm_mday, NVRAM_RTC_DAY); + if (!(statusB & 0x02)) { + if (time->tm_hour > 12) + reg8 = (time->tm_hour - 12) | 0x80; + else + reg8 = time->tm_hour; + } else + reg8 = time->tm_hour; + nvram_write(reg8, NVRAM_RTC_HOURS); + nvram_write(time->tm_year, NVRAM_RTC_YEAR); + } +} diff --git a/payloads/libpayload/include/libpayload.h b/payloads/libpayload/include/libpayload.h index 23226f3..5671efd 100644 --- a/payloads/libpayload/include/libpayload.h +++ b/payloads/libpayload/include/libpayload.h @@ -125,6 +125,7 @@ void nvram_write(u8 val, u8 addr); int nvram_updating(void); void rtc_read_clock(struct tm *tm); +void rtc_write_clock(struct tm *tm); /** @} */
/**