blob: d7e21deca74d5212159cd177a480f1a62dc34614 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Martyn Welch7d258bf2017-11-08 15:59:35 +00002/*
3 * Copyright 2017 General Electric Company
Martyn Welch7d258bf2017-11-08 15:59:35 +00004 */
5
6#include <common.h>
Simon Glass5e6201b2019-08-01 09:46:51 -06007#include <env.h>
Martyn Welch7d258bf2017-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);
Ian Rayc69217c2019-11-12 19:15:18 +000020 if (ret < 0) {
21 env_set("rtc_status", "FAIL");
Martyn Welch7d258bf2017-11-08 15:59:35 +000022 return;
Ian Rayc69217c2019-11-12 19:15:18 +000023 }
Martyn Welch7d258bf2017-11-08 15:59:35 +000024
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
Ian Rayc69217c2019-11-12 19:15:18 +000033 if (!ret && tm.tm_year > 2037) {
Martyn Welch7d258bf2017-11-08 15:59:35 +000034 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
Ian Rayc69217c2019-11-12 19:15:18 +000048 if (ret >= 0)
49 ret = 2038;
Martyn Welch7d258bf2017-11-08 15:59:35 +000050 }
51
Ian Rayc69217c2019-11-12 19:15:18 +000052 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
Martyn Welch7d258bf2017-11-08 15:59:35 +000059 i2c_set_bus_num(current_i2c_bus);
60}
61