blob: bc90b091f1954faecf087f2094c6094e31606da2 [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
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
Uwe Kleine-Königd0ab4a42010-03-10 15:20:35 -080025int rtc_hctosys_ret = -ENODEV;
26
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080027static 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;
Uwe Kleine-Königd0ab4a42010-03-10 15:20:35 -080031 struct timespec tv = {
32 .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) {
Uwe Kleine-Königd0ab4a42010-03-10 15:20:35 -080037 pr_err("%s: unable to open rtc device (%s)\n",
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080038 __FILE__, 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
50 err = rtc_valid_tm(&tm);
51 if (err) {
52 dev_err(rtc->dev.parent,
53 "hctosys: invalid date/time\n");
54 goto err_invalid;
55 }
56
57 rtc_tm_to_time(&tm, &tv.tv_sec);
58
59 do_settimeofday(&tv);
60
61 dev_info(rtc->dev.parent,
62 "setting system clock to "
63 "%d-%02d-%02d %02d:%02d:%02d UTC (%u)\n",
64 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
65 tm.tm_hour, tm.tm_min, tm.tm_sec,
66 (unsigned int) tv.tv_sec);
67
68err_invalid:
69err_read:
David Brownellab6a2d72007-05-08 00:33:30 -070070 rtc_class_close(rtc);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080071
Uwe Kleine-Königd0ab4a42010-03-10 15:20:35 -080072err_open:
73 rtc_hctosys_ret = err;
74
75 return err;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080076}
77
78late_initcall(rtc_hctosys);