blob: 48c3778046f1292c9d2c9458c7c42437d64ed80f [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>
Robert Beckett1dec7fa2020-01-31 15:07:54 +02008#include <dm/uclass.h>
Martyn Welch647155b2017-11-08 15:59:35 +00009#include <rtc.h>
10
11void check_time(void)
12{
Robert Beckett1dec7fa2020-01-31 15:07:54 +020013 struct udevice *dev;
Martyn Welch647155b2017-11-08 15:59:35 +000014 int ret, i;
15 struct rtc_time tm;
16 u8 retry = 3;
17
Robert Beckett1dec7fa2020-01-31 15:07:54 +020018 ret = uclass_get_device(UCLASS_RTC, 0, &dev);
19 if (ret) {
Ian Rayb186cfa2019-11-12 19:15:18 +000020 env_set("rtc_status", "FAIL");
Martyn Welch647155b2017-11-08 15:59:35 +000021 return;
Ian Rayb186cfa2019-11-12 19:15:18 +000022 }
Martyn Welch647155b2017-11-08 15:59:35 +000023
Martyn Welch647155b2017-11-08 15:59:35 +000024 for (i = 0; i < retry; i++) {
Robert Beckett1dec7fa2020-01-31 15:07:54 +020025 ret = dm_rtc_get(dev, &tm);
Martyn Welch647155b2017-11-08 15:59:35 +000026 if (!ret || ret == -EINVAL)
27 break;
28 }
29
Ian Rayb186cfa2019-11-12 19:15:18 +000030 if (!ret && tm.tm_year > 2037) {
Martyn Welch647155b2017-11-08 15:59:35 +000031 tm.tm_sec = 0;
32 tm.tm_min = 0;
33 tm.tm_hour = 0;
34 tm.tm_mday = 1;
35 tm.tm_wday = 2;
36 tm.tm_mon = 1;
37 tm.tm_year = 2036;
38
39 for (i = 0; i < retry; i++) {
Robert Beckett1dec7fa2020-01-31 15:07:54 +020040 ret = dm_rtc_set(dev, &tm);
Martyn Welch647155b2017-11-08 15:59:35 +000041 if (!ret)
42 break;
43 }
44
Ian Rayb186cfa2019-11-12 19:15:18 +000045 if (ret >= 0)
46 ret = 2038;
Martyn Welch647155b2017-11-08 15:59:35 +000047 }
48
Ian Rayb186cfa2019-11-12 19:15:18 +000049 if (ret < 0)
50 env_set("rtc_status", "FAIL");
51 else if (ret == 2038)
52 env_set("rtc_status", "2038");
53 else
54 env_set("rtc_status", "OK");
Martyn Welch647155b2017-11-08 15:59:35 +000055}
56