kernel中如果定义CONFIG_RTC_SYSTOHC,则会编译systohc.c
obj-$(CONFIG_RTC_SYSTOHC) += systohc.o
systohc.c 中只有一个函数,会在kernel 初始化的后期自动执行late_initcall(rtc_hctosys);
static int __init rtc_hctosys(void)
{
int err = -ENODEV;
struct rtc_time tm;
struct timespec64 tv64 = {
.tv_nsec = NSEC_PER_SEC >> 1,
};
struct rtc_device *rtc = rtc_class_open(CONFIG_RTC_HCTOSYS_DEVICE);
if (rtc == NULL) {
pr_info("unable to open rtc device (%s)
",
CONFIG_RTC_HCTOSYS_DEVICE);
goto err_open;
}
err = rtc_read_time(rtc, &tm);
if (err) {
dev_err(rtc->dev.parent,
"hctosys: unable to read the hardware clock
");
goto err_read;
}
tv64.tv_sec = rtc_tm_to_time64(&tm);
err = do_settimeofday64(&tv64);
dev_info(rtc->dev.parent,
"setting system clock to "
"%d-%02d-%02d %02d:%02d:%02d UTC (%lld)
",
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
tm.tm_hour, tm.tm_min, tm.tm_sec,
(long long) tv64.tv_sec);
err_read:
rtc_class_close(rtc);
err_open:
rtc_hctosys_ret = err;
return err;
}
这个函数最主要的作用是打开rtc driver,rtc_class_open(CONFIG_RTC_HCTOSYS_DEVICE);
读取时间
rtc_read_time(rtc, &tm);
将时间写到kernel中
err = do_settimeofday64(&tv64);
最后会输出log
rtc-efi rtc-efi: setting system clock to 2016-11-24 06:48:02 UTC (1479970082)