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