lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [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> |
xf.li | df7f8ba | 2024-09-12 23:53:34 -0700 | [diff] [blame^] | 13 | #include "pub_debug_info.h" |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 14 | |
| 15 | /* IMPORTANT: the RTC only stores whole seconds. It is arbitrary |
| 16 | * whether it stores the most close value or the value with partial |
| 17 | * seconds truncated. However, it is important that we use it to store |
| 18 | * the truncated value. This is because otherwise it is necessary, |
| 19 | * in an rtc sync function, to read both xtime.tv_sec and |
| 20 | * xtime.tv_nsec. On some processors (i.e. ARM), an atomic read |
| 21 | * of >32bits is not possible. So storing the most close value would |
| 22 | * slow down the sync API. So here we have the truncated value and |
| 23 | * the best guess is to add 0.5s. |
| 24 | */ |
| 25 | |
| 26 | int rtc_hctosys_ret = -ENODEV; |
| 27 | |
| 28 | static int __init rtc_hctosys(void) |
| 29 | { |
| 30 | int err = -ENODEV; |
| 31 | struct rtc_time tm; |
| 32 | struct timespec tv = { |
| 33 | .tv_nsec = NSEC_PER_SEC >> 1, |
| 34 | }; |
| 35 | struct rtc_device *rtc = rtc_class_open(CONFIG_RTC_HCTOSYS_DEVICE); |
| 36 | |
| 37 | if (rtc == NULL) { |
| 38 | pr_err("%s: unable to open rtc device (%s)\n", |
| 39 | __FILE__, CONFIG_RTC_HCTOSYS_DEVICE); |
| 40 | goto err_open; |
| 41 | } |
| 42 | |
| 43 | err = rtc_read_time(rtc, &tm); |
| 44 | if (err) { |
| 45 | dev_err(rtc->dev.parent, |
| 46 | "hctosys: unable to read the hardware clock\n"); |
| 47 | goto err_read; |
| 48 | |
| 49 | } |
| 50 | |
| 51 | err = rtc_valid_tm(&tm); |
| 52 | if (err) { |
| 53 | dev_err(rtc->dev.parent, |
| 54 | "hctosys: invalid date/time\n"); |
| 55 | goto err_invalid; |
| 56 | } |
| 57 | |
| 58 | rtc_tm_to_time(&tm, &tv.tv_sec); |
| 59 | |
| 60 | do_settimeofday(&tv); |
xf.li | df7f8ba | 2024-09-12 23:53:34 -0700 | [diff] [blame^] | 61 | sc_debug_info_record("hctosys", "time synchronization successful!\r\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 62 | dev_info(rtc->dev.parent, |
| 63 | "setting system clock to " |
| 64 | "%d-%02d-%02d %02d:%02d:%02d UTC (%u)\n", |
| 65 | tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, |
| 66 | tm.tm_hour, tm.tm_min, tm.tm_sec, |
| 67 | (unsigned int) tv.tv_sec); |
| 68 | |
| 69 | err_invalid: |
| 70 | err_read: |
| 71 | rtc_class_close(rtc); |
| 72 | |
| 73 | err_open: |
| 74 | rtc_hctosys_ret = err; |
| 75 | |
| 76 | return err; |
| 77 | } |
| 78 | |
| 79 | late_initcall(rtc_hctosys); |