blob: a74d0d890600a0b65eeceefee5d2c8d7f537422d [file] [log] [blame]
Alexandre Bellonicdf75452019-03-13 23:02:48 +01001// SPDX-License-Identifier: GPL-2.0
Alessandro Zummo0c86edc2006-03-27 01:16:37 -08002/*
3 * RTC subsystem, initialize system time on startup
4 *
5 * Copyright (C) 2005 Tower Technologies
6 * Author: Alessandro Zummo <a.zummo@towertech.it>
Alexandre Bellonicdf75452019-03-13 23:02:48 +01007 */
Alessandro Zummo0c86edc2006-03-27 01:16:37 -08008
Joe Perchesa737e832015-04-16 12:46:14 -07009#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080011#include <linux/rtc.h>
12
13/* IMPORTANT: the RTC only stores whole seconds. It is arbitrary
14 * whether it stores the most close value or the value with partial
15 * seconds truncated. However, it is important that we use it to store
16 * the truncated value. This is because otherwise it is necessary,
17 * in an rtc sync function, to read both xtime.tv_sec and
18 * xtime.tv_nsec. On some processors (i.e. ARM), an atomic read
19 * of >32bits is not possible. So storing the most close value would
20 * slow down the sync API. So here we have the truncated value and
21 * the best guess is to add 0.5s.
22 */
23
24static int __init rtc_hctosys(void)
25{
Uwe Kleine-Königd0ab4a42010-03-10 15:20:35 -080026 int err = -ENODEV;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080027 struct rtc_time tm;
Xunlei Panga6d6e1c2015-01-22 02:31:53 +000028 struct timespec64 tv64 = {
Uwe Kleine-Königd0ab4a42010-03-10 15:20:35 -080029 .tv_nsec = NSEC_PER_SEC >> 1,
30 };
David Brownellab6a2d72007-05-08 00:33:30 -070031 struct rtc_device *rtc = rtc_class_open(CONFIG_RTC_HCTOSYS_DEVICE);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080032
Alexandre Belloni606cc432019-03-20 12:59:09 +010033 if (!rtc) {
Joe Perchesa737e832015-04-16 12:46:14 -070034 pr_info("unable to open rtc device (%s)\n",
35 CONFIG_RTC_HCTOSYS_DEVICE);
Uwe Kleine-Königd0ab4a42010-03-10 15:20:35 -080036 goto err_open;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080037 }
38
David Brownellab6a2d72007-05-08 00:33:30 -070039 err = rtc_read_time(rtc, &tm);
Uwe Kleine-Königd0ab4a42010-03-10 15:20:35 -080040 if (err) {
David Brownellcd966202007-05-08 00:33:40 -070041 dev_err(rtc->dev.parent,
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080042 "hctosys: unable to read the hardware clock\n");
Uwe Kleine-Königd0ab4a42010-03-10 15:20:35 -080043 goto err_read;
Uwe Kleine-Königd0ab4a42010-03-10 15:20:35 -080044 }
45
Xunlei Panga6d6e1c2015-01-22 02:31:53 +000046 tv64.tv_sec = rtc_tm_to_time64(&tm);
Uwe Kleine-Königd0ab4a42010-03-10 15:20:35 -080047
Alexandre Bellonib3a5ac42018-03-08 23:27:31 +010048#if BITS_PER_LONG == 32
Maciej W. Rozycki7ce9a992018-11-05 03:48:25 +000049 if (tv64.tv_sec > INT_MAX) {
50 err = -ERANGE;
Alexandre Bellonib3a5ac42018-03-08 23:27:31 +010051 goto err_read;
Maciej W. Rozycki7ce9a992018-11-05 03:48:25 +000052 }
Alexandre Bellonib3a5ac42018-03-08 23:27:31 +010053#endif
54
Xunlei Panga6d6e1c2015-01-22 02:31:53 +000055 err = do_settimeofday64(&tv64);
Uwe Kleine-Königd0ab4a42010-03-10 15:20:35 -080056
Andy Shevchenko5548cbf2018-12-04 23:23:12 +020057 dev_info(rtc->dev.parent, "setting system clock to %ptR UTC (%lld)\n",
58 &tm, (long long)tv64.tv_sec);
Uwe Kleine-Königd0ab4a42010-03-10 15:20:35 -080059
Uwe Kleine-Königd0ab4a42010-03-10 15:20:35 -080060err_read:
David Brownellab6a2d72007-05-08 00:33:30 -070061 rtc_class_close(rtc);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080062
Uwe Kleine-Königd0ab4a42010-03-10 15:20:35 -080063err_open:
64 rtc_hctosys_ret = err;
65
66 return err;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080067}
68
69late_initcall(rtc_hctosys);