xref: /openbmc/u-boot/board/ge/common/ge_common.c (revision cf0bcd7d)
1 /*
2  * Copyright 2017 General Electric Company
3  *
4  * SPDX-License-Identifier:	GPL-2.0+
5  */
6 
7 #include <common.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 		return;
22 
23 	rtc_init();
24 
25 	for (i = 0; i < retry; i++) {
26 		ret = rtc_get(&tm);
27 		if (!ret || ret == -EINVAL)
28 			break;
29 	}
30 
31 	if (ret < 0)
32 		env_set("rtc_status", "RTC_ERROR");
33 
34 	if (tm.tm_year > 2037) {
35 		tm.tm_sec  = 0;
36 		tm.tm_min  = 0;
37 		tm.tm_hour = 0;
38 		tm.tm_mday = 1;
39 		tm.tm_wday = 2;
40 		tm.tm_mon  = 1;
41 		tm.tm_year = 2036;
42 
43 		for (i = 0; i < retry; i++) {
44 			ret = rtc_set(&tm);
45 			if (!ret)
46 				break;
47 		}
48 
49 		if (ret < 0)
50 			env_set("rtc_status", "RTC_ERROR");
51 	}
52 
53 	i2c_set_bus_num(current_i2c_bus);
54 }
55 
56