Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 2 | /* |
TsiChungLiew | 8baf62e | 2007-07-05 23:31:25 -0500 | [diff] [blame] | 3 | * Copyright (C) 2004-2007 Freescale Semiconductor, Inc. |
| 4 | * TsiChung Liew (Tsi-Chung.Liew@freescale.com) |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Wolfgang Denk | c30709a | 2007-08-19 10:27:34 +0200 | [diff] [blame] | 8 | |
Michal Simek | c3e6c55 | 2008-07-14 19:45:37 +0200 | [diff] [blame] | 9 | #if defined(CONFIG_CMD_DATE) |
Wolfgang Denk | c30709a | 2007-08-19 10:27:34 +0200 | [diff] [blame] | 10 | |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 11 | #include <command.h> |
| 12 | #include <rtc.h> |
TsiChungLiew | 8baf62e | 2007-07-05 23:31:25 -0500 | [diff] [blame] | 13 | #include <asm/immap.h> |
| 14 | #include <asm/rtc.h> |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 15 | |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 16 | #undef RTC_DEBUG |
| 17 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 18 | #ifndef CONFIG_SYS_MCFRTC_BASE |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 19 | #error RTC_BASE is not defined! |
| 20 | #endif |
| 21 | |
| 22 | #define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0) |
| 23 | #define STARTOFTIME 1970 |
| 24 | |
Yuri Tikhonov | 9bacd94 | 2008-03-20 17:56:04 +0300 | [diff] [blame] | 25 | int rtc_get(struct rtc_time *tmp) |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 26 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 27 | volatile rtc_t *rtc = (rtc_t *) (CONFIG_SYS_MCFRTC_BASE); |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 28 | |
| 29 | int rtc_days, rtc_hrs, rtc_mins; |
| 30 | int tim; |
| 31 | |
| 32 | rtc_days = rtc->days; |
| 33 | rtc_hrs = rtc->hourmin >> 8; |
| 34 | rtc_mins = RTC_HOURMIN_MINUTES(rtc->hourmin); |
| 35 | |
| 36 | tim = (rtc_days * 24) + rtc_hrs; |
| 37 | tim = (tim * 60) + rtc_mins; |
| 38 | tim = (tim * 60) + rtc->seconds; |
| 39 | |
Simon Glass | 1299466 | 2015-04-20 12:37:18 -0600 | [diff] [blame] | 40 | rtc_to_tm(tim, tmp); |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 41 | |
| 42 | tmp->tm_yday = 0; |
| 43 | tmp->tm_isdst = 0; |
| 44 | |
| 45 | #ifdef RTC_DEBUG |
| 46 | printf("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n", |
| 47 | tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday, |
| 48 | tmp->tm_hour, tmp->tm_min, tmp->tm_sec); |
| 49 | #endif |
Yuri Tikhonov | 9bacd94 | 2008-03-20 17:56:04 +0300 | [diff] [blame] | 50 | |
| 51 | return 0; |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 52 | } |
| 53 | |
Jean-Christophe PLAGNIOL-VILLARD | 97a2e10 | 2008-09-01 23:06:23 +0200 | [diff] [blame] | 54 | int rtc_set(struct rtc_time *tmp) |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 55 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 56 | volatile rtc_t *rtc = (rtc_t *) (CONFIG_SYS_MCFRTC_BASE); |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 57 | |
| 58 | static int month_days[12] = { |
| 59 | 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 |
| 60 | }; |
| 61 | int days, i, months; |
| 62 | |
| 63 | if (tmp->tm_year > 2037) { |
| 64 | printf("Unable to handle. Exceeding integer limitation!\n"); |
| 65 | tmp->tm_year = 2027; |
| 66 | } |
| 67 | #ifdef RTC_DEBUG |
| 68 | printf("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n", |
| 69 | tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday, |
| 70 | tmp->tm_hour, tmp->tm_min, tmp->tm_sec); |
| 71 | #endif |
| 72 | |
| 73 | /* calculate days by years */ |
| 74 | for (i = STARTOFTIME, days = 0; i < tmp->tm_year; i++) { |
| 75 | days += 365 + isleap(i); |
| 76 | } |
| 77 | |
| 78 | /* calculate days by months */ |
| 79 | months = tmp->tm_mon - 1; |
| 80 | for (i = 0; i < months; i++) { |
| 81 | days += month_days[i]; |
| 82 | |
| 83 | if (i == 1) |
| 84 | days += isleap(i); |
| 85 | } |
| 86 | |
| 87 | days += tmp->tm_mday - 1; |
| 88 | |
| 89 | rtc->days = days; |
| 90 | rtc->hourmin = (tmp->tm_hour << 8) | tmp->tm_min; |
| 91 | rtc->seconds = tmp->tm_sec; |
Jean-Christophe PLAGNIOL-VILLARD | 97a2e10 | 2008-09-01 23:06:23 +0200 | [diff] [blame] | 92 | |
| 93 | return 0; |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | void rtc_reset(void) |
| 97 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 98 | volatile rtc_t *rtc = (rtc_t *) (CONFIG_SYS_MCFRTC_BASE); |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 99 | |
| 100 | if ((rtc->cr & RTC_CR_EN) == 0) { |
| 101 | printf("real-time-clock was stopped. Now starting...\n"); |
| 102 | rtc->cr |= RTC_CR_EN; |
| 103 | } |
| 104 | |
| 105 | rtc->cr |= RTC_CR_SWR; |
| 106 | } |
| 107 | |
TsiChungLiew | aedd3d7 | 2007-08-15 15:39:17 -0500 | [diff] [blame] | 108 | #endif /* CONFIG_MCFRTC && CONFIG_CMD_DATE */ |