xref: /openbmc/u-boot/board/ge/common/ge_common.c (revision d66ff4ba)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2017 General Electric Company
4  */
5 
6 #include <common.h>
7 #include <i2c.h>
8 #include <rtc.h>
9 
10 void 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 
55