]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/rtc/hctosys.c
UBUNTU: upstream stable to v4.14.156, v4.19.86
[mirror_ubuntu-bionic-kernel.git] / drivers / rtc / hctosys.c
CommitLineData
0c86edc0
AZ
1/*
2 * RTC subsystem, initialize system time on startup
3 *
4 * Copyright (C) 2005 Tower Technologies
5 * Author: Alessandro Zummo <a.zummo@towertech.it>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10*/
11
a737e835
JP
12#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
0c86edc0
AZ
14#include <linux/rtc.h>
15
16/* IMPORTANT: the RTC only stores whole seconds. It is arbitrary
17 * whether it stores the most close value or the value with partial
18 * seconds truncated. However, it is important that we use it to store
19 * the truncated value. This is because otherwise it is necessary,
20 * in an rtc sync function, to read both xtime.tv_sec and
21 * xtime.tv_nsec. On some processors (i.e. ARM), an atomic read
22 * of >32bits is not possible. So storing the most close value would
23 * slow down the sync API. So here we have the truncated value and
24 * the best guess is to add 0.5s.
25 */
26
27static int __init rtc_hctosys(void)
28{
d0ab4a4d 29 int err = -ENODEV;
0c86edc0 30 struct rtc_time tm;
a6d6e1c8 31 struct timespec64 tv64 = {
d0ab4a4d
UKK
32 .tv_nsec = NSEC_PER_SEC >> 1,
33 };
ab6a2d70 34 struct rtc_device *rtc = rtc_class_open(CONFIG_RTC_HCTOSYS_DEVICE);
0c86edc0 35
ab6a2d70 36 if (rtc == NULL) {
a737e835
JP
37 pr_info("unable to open rtc device (%s)\n",
38 CONFIG_RTC_HCTOSYS_DEVICE);
d0ab4a4d 39 goto err_open;
0c86edc0
AZ
40 }
41
ab6a2d70 42 err = rtc_read_time(rtc, &tm);
d0ab4a4d
UKK
43 if (err) {
44 dev_err(rtc->dev.parent,
45 "hctosys: unable to read the hardware clock\n");
46 goto err_read;
0c86edc0 47
d0ab4a4d 48 }
0c86edc0 49
a6d6e1c8 50 tv64.tv_sec = rtc_tm_to_time64(&tm);
0c86edc0 51
810aba6a 52#if BITS_PER_LONG == 32
4e53a21b
MR
53 if (tv64.tv_sec > INT_MAX) {
54 err = -ERANGE;
810aba6a 55 goto err_read;
4e53a21b 56 }
810aba6a
AB
57#endif
58
a6d6e1c8 59 err = do_settimeofday64(&tv64);
0c86edc0 60
d0ab4a4d
UKK
61 dev_info(rtc->dev.parent,
62 "setting system clock to "
a6d6e1c8 63 "%d-%02d-%02d %02d:%02d:%02d UTC (%lld)\n",
d0ab4a4d
UKK
64 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
65 tm.tm_hour, tm.tm_min, tm.tm_sec,
a6d6e1c8 66 (long long) tv64.tv_sec);
d0ab4a4d 67
d0ab4a4d 68err_read:
ab6a2d70 69 rtc_class_close(rtc);
0c86edc0 70
d0ab4a4d
UKK
71err_open:
72 rtc_hctosys_ret = err;
73
74 return err;
0c86edc0
AZ
75}
76
77late_initcall(rtc_hctosys);