blob: 5c62ecca8c89febc5ea506d9cdd6d9a3492a0e36 [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
Simon Glass5e6201b2019-08-01 09:46:51 -06006#include <env.h>
Robert Beckett53bab172020-01-31 15:07:54 +02007#include <dm/uclass.h>
Martyn Welch7d258bf2017-11-08 15:59:35 +00008#include <rtc.h>
9
10void check_time(void)
11{
Robert Beckett53bab172020-01-31 15:07:54 +020012 struct udevice *dev;
Martyn Welch7d258bf2017-11-08 15:59:35 +000013 int ret, i;
14 struct rtc_time tm;
15 u8 retry = 3;
16
Robert Beckett53bab172020-01-31 15:07:54 +020017 ret = uclass_get_device(UCLASS_RTC, 0, &dev);
18 if (ret) {
Ian Rayc69217c2019-11-12 19:15:18 +000019 env_set("rtc_status", "FAIL");
Martyn Welch7d258bf2017-11-08 15:59:35 +000020 return;
Ian Rayc69217c2019-11-12 19:15:18 +000021 }
Martyn Welch7d258bf2017-11-08 15:59:35 +000022
Martyn Welch7d258bf2017-11-08 15:59:35 +000023 for (i = 0; i < retry; i++) {
Robert Beckett53bab172020-01-31 15:07:54 +020024 ret = dm_rtc_get(dev, &tm);
Martyn Welch7d258bf2017-11-08 15:59:35 +000025 if (!ret || ret == -EINVAL)
26 break;
27 }
28
Ian Rayc69217c2019-11-12 19:15:18 +000029 if (!ret && tm.tm_year > 2037) {
Martyn Welch7d258bf2017-11-08 15:59:35 +000030 tm.tm_sec = 0;
31 tm.tm_min = 0;
32 tm.tm_hour = 0;
33 tm.tm_mday = 1;
34 tm.tm_wday = 2;
35 tm.tm_mon = 1;
36 tm.tm_year = 2036;
37
38 for (i = 0; i < retry; i++) {
Robert Beckett53bab172020-01-31 15:07:54 +020039 ret = dm_rtc_set(dev, &tm);
Martyn Welch7d258bf2017-11-08 15:59:35 +000040 if (!ret)
41 break;
42 }
43
Ian Rayc69217c2019-11-12 19:15:18 +000044 if (ret >= 0)
45 ret = 2038;
Martyn Welch7d258bf2017-11-08 15:59:35 +000046 }
47
Ian Rayc69217c2019-11-12 19:15:18 +000048 if (ret < 0)
49 env_set("rtc_status", "FAIL");
50 else if (ret == 2038)
51 env_set("rtc_status", "2038");
52 else
53 env_set("rtc_status", "OK");
Martyn Welch7d258bf2017-11-08 15:59:35 +000054}