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