blob: 4e6ef9c48066d5d5434f9b5ac9d11491b5c59af7 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Martyn Welch647155b2017-11-08 15:59:35 +00002/*
3 * Copyright 2017 General Electric Company
Martyn Welch647155b2017-11-08 15:59:35 +00004 */
5
6#include <common.h>
7#include <i2c.h>
8#include <rtc.h>
9
10void check_time(void)
11{
12 int ret, i;
13 struct rtc_time tm;
14 u8 retry = 3;
15
16 unsigned int current_i2c_bus = i2c_get_bus_num();
17
18 ret = i2c_set_bus_num(CONFIG_SYS_RTC_BUS_NUM);
19 if (ret < 0)
20 return;
21
22 rtc_init();
23
24 for (i = 0; i < retry; i++) {
25 ret = rtc_get(&tm);
26 if (!ret || ret == -EINVAL)
27 break;
28 }
29
30 if (ret < 0)
31 env_set("rtc_status", "RTC_ERROR");
32
33 if (tm.tm_year > 2037) {
34 tm.tm_sec = 0;
35 tm.tm_min = 0;
36 tm.tm_hour = 0;
37 tm.tm_mday = 1;
38 tm.tm_wday = 2;
39 tm.tm_mon = 1;
40 tm.tm_year = 2036;
41
42 for (i = 0; i < retry; i++) {
43 ret = rtc_set(&tm);
44 if (!ret)
45 break;
46 }
47
48 if (ret < 0)
49 env_set("rtc_status", "RTC_ERROR");
50 }
51
52 i2c_set_bus_num(current_i2c_bus);
53}
54