Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Martyn Welch | 7d258bf | 2017-11-08 15:59:35 +0000 | [diff] [blame] | 2 | /* |
3 | * Copyright 2017 General Electric Company | ||||
Martyn Welch | 7d258bf | 2017-11-08 15:59:35 +0000 | [diff] [blame] | 4 | */ |
5 | |||||
Simon Glass | 5e6201b | 2019-08-01 09:46:51 -0600 | [diff] [blame] | 6 | #include <env.h> |
Robert Beckett | 53bab17 | 2020-01-31 15:07:54 +0200 | [diff] [blame] | 7 | #include <dm/uclass.h> |
Martyn Welch | 7d258bf | 2017-11-08 15:59:35 +0000 | [diff] [blame] | 8 | #include <rtc.h> |
9 | |||||
10 | void check_time(void) | ||||
11 | { | ||||
Robert Beckett | 53bab17 | 2020-01-31 15:07:54 +0200 | [diff] [blame] | 12 | struct udevice *dev; |
Martyn Welch | 7d258bf | 2017-11-08 15:59:35 +0000 | [diff] [blame] | 13 | int ret, i; |
14 | struct rtc_time tm; | ||||
15 | u8 retry = 3; | ||||
16 | |||||
Robert Beckett | 53bab17 | 2020-01-31 15:07:54 +0200 | [diff] [blame] | 17 | ret = uclass_get_device(UCLASS_RTC, 0, &dev); |
18 | if (ret) { | ||||
Ian Ray | c69217c | 2019-11-12 19:15:18 +0000 | [diff] [blame] | 19 | env_set("rtc_status", "FAIL"); |
Martyn Welch | 7d258bf | 2017-11-08 15:59:35 +0000 | [diff] [blame] | 20 | return; |
Ian Ray | c69217c | 2019-11-12 19:15:18 +0000 | [diff] [blame] | 21 | } |
Martyn Welch | 7d258bf | 2017-11-08 15:59:35 +0000 | [diff] [blame] | 22 | |
Martyn Welch | 7d258bf | 2017-11-08 15:59:35 +0000 | [diff] [blame] | 23 | for (i = 0; i < retry; i++) { |
Robert Beckett | 53bab17 | 2020-01-31 15:07:54 +0200 | [diff] [blame] | 24 | ret = dm_rtc_get(dev, &tm); |
Martyn Welch | 7d258bf | 2017-11-08 15:59:35 +0000 | [diff] [blame] | 25 | if (!ret || ret == -EINVAL) |
26 | break; | ||||
27 | } | ||||
28 | |||||
Ian Ray | c69217c | 2019-11-12 19:15:18 +0000 | [diff] [blame] | 29 | if (!ret && tm.tm_year > 2037) { |
Martyn Welch | 7d258bf | 2017-11-08 15:59:35 +0000 | [diff] [blame] | 30 | 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 Beckett | 53bab17 | 2020-01-31 15:07:54 +0200 | [diff] [blame] | 39 | ret = dm_rtc_set(dev, &tm); |
Martyn Welch | 7d258bf | 2017-11-08 15:59:35 +0000 | [diff] [blame] | 40 | if (!ret) |
41 | break; | ||||
42 | } | ||||
43 | |||||
Ian Ray | c69217c | 2019-11-12 19:15:18 +0000 | [diff] [blame] | 44 | if (ret >= 0) |
45 | ret = 2038; | ||||
Martyn Welch | 7d258bf | 2017-11-08 15:59:35 +0000 | [diff] [blame] | 46 | } |
47 | |||||
Ian Ray | c69217c | 2019-11-12 19:15:18 +0000 | [diff] [blame] | 48 | 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 Welch | 7d258bf | 2017-11-08 15:59:35 +0000 | [diff] [blame] | 54 | } |