Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 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 | |
| 12 | #include <linux/rtc.h> |
| 13 | |
| 14 | /* IMPORTANT: the RTC only stores whole seconds. It is arbitrary |
| 15 | * whether it stores the most close value or the value with partial |
| 16 | * seconds truncated. However, it is important that we use it to store |
| 17 | * the truncated value. This is because otherwise it is necessary, |
| 18 | * in an rtc sync function, to read both xtime.tv_sec and |
| 19 | * xtime.tv_nsec. On some processors (i.e. ARM), an atomic read |
| 20 | * of >32bits is not possible. So storing the most close value would |
| 21 | * slow down the sync API. So here we have the truncated value and |
| 22 | * the best guess is to add 0.5s. |
| 23 | */ |
| 24 | |
| 25 | static int __init rtc_hctosys(void) |
| 26 | { |
Uwe Kleine-König | d0ab4a4 | 2010-03-10 15:20:35 -0800 | [diff] [blame] | 27 | int err = -ENODEV; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 28 | struct rtc_time tm; |
Xunlei Pang | a6d6e1c | 2015-01-22 02:31:53 +0000 | [diff] [blame] | 29 | struct timespec64 tv64 = { |
Uwe Kleine-König | d0ab4a4 | 2010-03-10 15:20:35 -0800 | [diff] [blame] | 30 | .tv_nsec = NSEC_PER_SEC >> 1, |
| 31 | }; |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 32 | struct rtc_device *rtc = rtc_class_open(CONFIG_RTC_HCTOSYS_DEVICE); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 33 | |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 34 | if (rtc == NULL) { |
Uwe Kleine-König | d0ab4a4 | 2010-03-10 15:20:35 -0800 | [diff] [blame] | 35 | pr_err("%s: unable to open rtc device (%s)\n", |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 36 | __FILE__, CONFIG_RTC_HCTOSYS_DEVICE); |
Uwe Kleine-König | d0ab4a4 | 2010-03-10 15:20:35 -0800 | [diff] [blame] | 37 | goto err_open; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 38 | } |
| 39 | |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 40 | err = rtc_read_time(rtc, &tm); |
Uwe Kleine-König | d0ab4a4 | 2010-03-10 15:20:35 -0800 | [diff] [blame] | 41 | if (err) { |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 42 | dev_err(rtc->dev.parent, |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 43 | "hctosys: unable to read the hardware clock\n"); |
Uwe Kleine-König | d0ab4a4 | 2010-03-10 15:20:35 -0800 | [diff] [blame] | 44 | goto err_read; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 45 | |
Uwe Kleine-König | d0ab4a4 | 2010-03-10 15:20:35 -0800 | [diff] [blame] | 46 | } |
| 47 | |
Xunlei Pang | a6d6e1c | 2015-01-22 02:31:53 +0000 | [diff] [blame] | 48 | tv64.tv_sec = rtc_tm_to_time64(&tm); |
Uwe Kleine-König | d0ab4a4 | 2010-03-10 15:20:35 -0800 | [diff] [blame] | 49 | |
Xunlei Pang | a6d6e1c | 2015-01-22 02:31:53 +0000 | [diff] [blame] | 50 | err = do_settimeofday64(&tv64); |
Uwe Kleine-König | d0ab4a4 | 2010-03-10 15:20:35 -0800 | [diff] [blame] | 51 | |
| 52 | dev_info(rtc->dev.parent, |
| 53 | "setting system clock to " |
Xunlei Pang | a6d6e1c | 2015-01-22 02:31:53 +0000 | [diff] [blame] | 54 | "%d-%02d-%02d %02d:%02d:%02d UTC (%lld)\n", |
Uwe Kleine-König | d0ab4a4 | 2010-03-10 15:20:35 -0800 | [diff] [blame] | 55 | tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, |
| 56 | tm.tm_hour, tm.tm_min, tm.tm_sec, |
Xunlei Pang | a6d6e1c | 2015-01-22 02:31:53 +0000 | [diff] [blame] | 57 | (long long) tv64.tv_sec); |
Uwe Kleine-König | d0ab4a4 | 2010-03-10 15:20:35 -0800 | [diff] [blame] | 58 | |
Uwe Kleine-König | d0ab4a4 | 2010-03-10 15:20:35 -0800 | [diff] [blame] | 59 | err_read: |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 60 | rtc_class_close(rtc); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 61 | |
Uwe Kleine-König | d0ab4a4 | 2010-03-10 15:20:35 -0800 | [diff] [blame] | 62 | err_open: |
| 63 | rtc_hctosys_ret = err; |
| 64 | |
| 65 | return err; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | late_initcall(rtc_hctosys); |