Alexandre Belloni | cdf7545 | 2019-03-13 23:02:48 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 2 | /* |
| 3 | * RTC subsystem, initialize system time on startup |
| 4 | * |
| 5 | * Copyright (C) 2005 Tower Technologies |
| 6 | * Author: Alessandro Zummo <a.zummo@towertech.it> |
Alexandre Belloni | cdf7545 | 2019-03-13 23:02:48 +0100 | [diff] [blame] | 7 | */ |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 8 | |
Joe Perches | a737e83 | 2015-04-16 12:46:14 -0700 | [diff] [blame] | 9 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 10 | |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 11 | #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 | |
| 24 | static int __init rtc_hctosys(void) |
| 25 | { |
Uwe Kleine-König | d0ab4a4 | 2010-03-10 15:20:35 -0800 | [diff] [blame] | 26 | int err = -ENODEV; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 27 | struct rtc_time tm; |
Xunlei Pang | a6d6e1c | 2015-01-22 02:31:53 +0000 | [diff] [blame] | 28 | struct timespec64 tv64 = { |
Uwe Kleine-König | d0ab4a4 | 2010-03-10 15:20:35 -0800 | [diff] [blame] | 29 | .tv_nsec = NSEC_PER_SEC >> 1, |
| 30 | }; |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 31 | struct rtc_device *rtc = rtc_class_open(CONFIG_RTC_HCTOSYS_DEVICE); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 32 | |
Alexandre Belloni | 606cc43 | 2019-03-20 12:59:09 +0100 | [diff] [blame] | 33 | if (!rtc) { |
Joe Perches | a737e83 | 2015-04-16 12:46:14 -0700 | [diff] [blame] | 34 | pr_info("unable to open rtc device (%s)\n", |
| 35 | CONFIG_RTC_HCTOSYS_DEVICE); |
Uwe Kleine-König | d0ab4a4 | 2010-03-10 15:20:35 -0800 | [diff] [blame] | 36 | goto err_open; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 37 | } |
| 38 | |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 39 | err = rtc_read_time(rtc, &tm); |
Uwe Kleine-König | d0ab4a4 | 2010-03-10 15:20:35 -0800 | [diff] [blame] | 40 | if (err) { |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 41 | dev_err(rtc->dev.parent, |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 42 | "hctosys: unable to read the hardware clock\n"); |
Uwe Kleine-König | d0ab4a4 | 2010-03-10 15:20:35 -0800 | [diff] [blame] | 43 | goto err_read; |
Uwe Kleine-König | d0ab4a4 | 2010-03-10 15:20:35 -0800 | [diff] [blame] | 44 | } |
| 45 | |
Xunlei Pang | a6d6e1c | 2015-01-22 02:31:53 +0000 | [diff] [blame] | 46 | tv64.tv_sec = rtc_tm_to_time64(&tm); |
Uwe Kleine-König | d0ab4a4 | 2010-03-10 15:20:35 -0800 | [diff] [blame] | 47 | |
Alexandre Belloni | b3a5ac4 | 2018-03-08 23:27:31 +0100 | [diff] [blame] | 48 | #if BITS_PER_LONG == 32 |
Maciej W. Rozycki | 7ce9a99 | 2018-11-05 03:48:25 +0000 | [diff] [blame] | 49 | if (tv64.tv_sec > INT_MAX) { |
| 50 | err = -ERANGE; |
Alexandre Belloni | b3a5ac4 | 2018-03-08 23:27:31 +0100 | [diff] [blame] | 51 | goto err_read; |
Maciej W. Rozycki | 7ce9a99 | 2018-11-05 03:48:25 +0000 | [diff] [blame] | 52 | } |
Alexandre Belloni | b3a5ac4 | 2018-03-08 23:27:31 +0100 | [diff] [blame] | 53 | #endif |
| 54 | |
Xunlei Pang | a6d6e1c | 2015-01-22 02:31:53 +0000 | [diff] [blame] | 55 | err = do_settimeofday64(&tv64); |
Uwe Kleine-König | d0ab4a4 | 2010-03-10 15:20:35 -0800 | [diff] [blame] | 56 | |
Andy Shevchenko | 5548cbf | 2018-12-04 23:23:12 +0200 | [diff] [blame] | 57 | dev_info(rtc->dev.parent, "setting system clock to %ptR UTC (%lld)\n", |
| 58 | &tm, (long long)tv64.tv_sec); |
Uwe Kleine-König | d0ab4a4 | 2010-03-10 15:20:35 -0800 | [diff] [blame] | 59 | |
Uwe Kleine-König | d0ab4a4 | 2010-03-10 15:20:35 -0800 | [diff] [blame] | 60 | err_read: |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 61 | rtc_class_close(rtc); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 62 | |
Uwe Kleine-König | d0ab4a4 | 2010-03-10 15:20:35 -0800 | [diff] [blame] | 63 | err_open: |
| 64 | rtc_hctosys_ret = err; |
| 65 | |
| 66 | return err; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | late_initcall(rtc_hctosys); |