d7e21deca74d5212159cd177a480f1a62dc34614
[oweals/u-boot.git] / board / ge / common / ge_common.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2017 General Electric Company
4  */
5
6 #include <common.h>
7 #include <env.h>
8 #include <i2c.h>
9 #include <rtc.h>
10
11 void check_time(void)
12 {
13         int ret, i;
14         struct rtc_time tm;
15         u8 retry = 3;
16
17         unsigned int current_i2c_bus = i2c_get_bus_num();
18
19         ret = i2c_set_bus_num(CONFIG_SYS_RTC_BUS_NUM);
20         if (ret < 0) {
21                 env_set("rtc_status", "FAIL");
22                 return;
23         }
24
25         rtc_init();
26
27         for (i = 0; i < retry; i++) {
28                 ret = rtc_get(&tm);
29                 if (!ret || ret == -EINVAL)
30                         break;
31         }
32
33         if (!ret && 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                         ret = 2038;
50         }
51
52         if (ret < 0)
53                 env_set("rtc_status", "FAIL");
54         else if (ret == 2038)
55                 env_set("rtc_status", "2038");
56         else
57                 env_set("rtc_status", "OK");
58
59         i2c_set_bus_num(current_i2c_bus);
60 }
61