blob: 6437afc7bd0aaee59a60cc692b81eb557aa06330 [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>
Robert Beckett53bab172020-01-31 15:07:54 +02008#include <dm/uclass.h>
Martyn Welch7d258bf2017-11-08 15:59:35 +00009#include <rtc.h>
10
11void check_time(void)
12{
Robert Beckett53bab172020-01-31 15:07:54 +020013 struct udevice *dev;
Martyn Welch7d258bf2017-11-08 15:59:35 +000014 int ret, i;
15 struct rtc_time tm;
16 u8 retry = 3;
17
Robert Beckett53bab172020-01-31 15:07:54 +020018 ret = uclass_get_device(UCLASS_RTC, 0, &dev);
19 if (ret) {
Ian Rayc69217c2019-11-12 19:15:18 +000020 env_set("rtc_status", "FAIL");
Martyn Welch7d258bf2017-11-08 15:59:35 +000021 return;
Ian Rayc69217c2019-11-12 19:15:18 +000022 }
Martyn Welch7d258bf2017-11-08 15:59:35 +000023
Martyn Welch7d258bf2017-11-08 15:59:35 +000024 for (i = 0; i < retry; i++) {
Robert Beckett53bab172020-01-31 15:07:54 +020025 ret = dm_rtc_get(dev, &tm);
Martyn Welch7d258bf2017-11-08 15:59:35 +000026 if (!ret || ret == -EINVAL)
27 break;
28 }
29
Ian Rayc69217c2019-11-12 19:15:18 +000030 if (!ret && tm.tm_year > 2037) {
Martyn Welch7d258bf2017-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 Beckett53bab172020-01-31 15:07:54 +020040 ret = dm_rtc_set(dev, &tm);
Martyn Welch7d258bf2017-11-08 15:59:35 +000041 if (!ret)
42 break;
43 }
44
Ian Rayc69217c2019-11-12 19:15:18 +000045 if (ret >= 0)
46 ret = 2038;
Martyn Welch7d258bf2017-11-08 15:59:35 +000047 }
48
Ian Rayc69217c2019-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 Welch7d258bf2017-11-08 15:59:35 +000055}