blob: b9ec4a16db1f6b6fd113c5661a28aa0e9153eeaa [file] [log] [blame]
Alessandro Zummo0c86edc2006-03-27 01:16:37 -08001/*
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
Joe Perchesa737e832015-04-16 12:46:14 -070012#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080014#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{
Uwe Kleine-Königd0ab4a42010-03-10 15:20:35 -080029 int err = -ENODEV;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080030 struct rtc_time tm;
Xunlei Panga6d6e1c2015-01-22 02:31:53 +000031 struct timespec64 tv64 = {
Uwe Kleine-Königd0ab4a42010-03-10 15:20:35 -080032 .tv_nsec = NSEC_PER_SEC >> 1,
33 };
David Brownellab6a2d72007-05-08 00:33:30 -070034 struct rtc_device *rtc = rtc_class_open(CONFIG_RTC_HCTOSYS_DEVICE);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080035
David Brownellab6a2d72007-05-08 00:33:30 -070036 if (rtc == NULL) {
Joe Perchesa737e832015-04-16 12:46:14 -070037 pr_info("unable to open rtc device (%s)\n",
38 CONFIG_RTC_HCTOSYS_DEVICE);
Uwe Kleine-Königd0ab4a42010-03-10 15:20:35 -080039 goto err_open;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080040 }
41
David Brownellab6a2d72007-05-08 00:33:30 -070042 err = rtc_read_time(rtc, &tm);
Uwe Kleine-Königd0ab4a42010-03-10 15:20:35 -080043 if (err) {
David Brownellcd966202007-05-08 00:33:40 -070044 dev_err(rtc->dev.parent,
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080045 "hctosys: unable to read the hardware clock\n");
Uwe Kleine-Königd0ab4a42010-03-10 15:20:35 -080046 goto err_read;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080047
Uwe Kleine-Königd0ab4a42010-03-10 15:20:35 -080048 }
49
Xunlei Panga6d6e1c2015-01-22 02:31:53 +000050 tv64.tv_sec = rtc_tm_to_time64(&tm);
Uwe Kleine-Königd0ab4a42010-03-10 15:20:35 -080051
Alexandre Bellonidd6b3e02018-03-08 23:27:31 +010052#if BITS_PER_LONG == 32
Maciej W. Rozyckic616a932018-11-05 03:48:25 +000053 if (tv64.tv_sec > INT_MAX) {
54 err = -ERANGE;
Alexandre Bellonidd6b3e02018-03-08 23:27:31 +010055 goto err_read;
Maciej W. Rozyckic616a932018-11-05 03:48:25 +000056 }
Alexandre Bellonidd6b3e02018-03-08 23:27:31 +010057#endif
58
Xunlei Panga6d6e1c2015-01-22 02:31:53 +000059 err = do_settimeofday64(&tv64);
Uwe Kleine-Königd0ab4a42010-03-10 15:20:35 -080060
61 dev_info(rtc->dev.parent,
62 "setting system clock to "
Xunlei Panga6d6e1c2015-01-22 02:31:53 +000063 "%d-%02d-%02d %02d:%02d:%02d UTC (%lld)\n",
Uwe Kleine-Königd0ab4a42010-03-10 15:20:35 -080064 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
65 tm.tm_hour, tm.tm_min, tm.tm_sec,
Xunlei Panga6d6e1c2015-01-22 02:31:53 +000066 (long long) tv64.tv_sec);
Uwe Kleine-Königd0ab4a42010-03-10 15:20:35 -080067
Uwe Kleine-Königd0ab4a42010-03-10 15:20:35 -080068err_read:
David Brownellab6a2d72007-05-08 00:33:30 -070069 rtc_class_close(rtc);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080070
Uwe Kleine-Königd0ab4a42010-03-10 15:20:35 -080071err_open:
72 rtc_hctosys_ret = err;
73
74 return err;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080075}
76
77late_initcall(rtc_hctosys);