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> |
Martyn Welch | 647155b | 2017-11-08 15:59:35 +0000 | [diff] [blame] | 8 | #include <i2c.h> |
| 9 | #include <rtc.h> |
| 10 | |
| 11 | void check_time(void) |
| 12 | { |
| 13 | int ret, i; |
| 14 | struct rtc_time tm; |
| 15 | u8 retry = 3; |
| 16 | |
| 17 | unsigned int current_i2c_bus = i2c_get_bus_num(); |
| 18 | |
| 19 | ret = i2c_set_bus_num(CONFIG_SYS_RTC_BUS_NUM); |
| 20 | if (ret < 0) |
| 21 | return; |
| 22 | |
| 23 | rtc_init(); |
| 24 | |
| 25 | for (i = 0; i < retry; i++) { |
| 26 | ret = rtc_get(&tm); |
| 27 | if (!ret || ret == -EINVAL) |
| 28 | break; |
| 29 | } |
| 30 | |
| 31 | if (ret < 0) |
| 32 | env_set("rtc_status", "RTC_ERROR"); |
| 33 | |
| 34 | if (tm.tm_year > 2037) { |
| 35 | tm.tm_sec = 0; |
| 36 | tm.tm_min = 0; |
| 37 | tm.tm_hour = 0; |
| 38 | tm.tm_mday = 1; |
| 39 | tm.tm_wday = 2; |
| 40 | tm.tm_mon = 1; |
| 41 | tm.tm_year = 2036; |
| 42 | |
| 43 | for (i = 0; i < retry; i++) { |
| 44 | ret = rtc_set(&tm); |
| 45 | if (!ret) |
| 46 | break; |
| 47 | } |
| 48 | |
| 49 | if (ret < 0) |
| 50 | env_set("rtc_status", "RTC_ERROR"); |
| 51 | } |
| 52 | |
| 53 | i2c_set_bus_num(current_i2c_bus); |
| 54 | } |
| 55 | |