blob: 501c8b2daf2aa0b5426f1f3f323844d5f5dcc9d6 [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>
Simon Glass9fb625c2019-08-01 09:46:51 -06007#include <env.h>
Martyn Welch647155b2017-11-08 15:59:35 +00008#include <i2c.h>
9#include <rtc.h>
10
11void 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