TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 1 | /* |
TsiChungLiew | 8baf62e | 2007-07-05 23:31:25 -0500 | [diff] [blame] | 2 | * Copyright (C) 2004-2007 Freescale Semiconductor, Inc. |
| 3 | * TsiChung Liew (Tsi-Chung.Liew@freescale.com) |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 4 | * |
| 5 | * See file CREDITS for list of people who contributed to this |
| 6 | * project. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of |
| 11 | * the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 21 | * MA 02111-1307 USA |
| 22 | */ |
| 23 | |
| 24 | #include <common.h> |
Wolfgang Denk | c30709a | 2007-08-19 10:27:34 +0200 | [diff] [blame] | 25 | |
Michal Simek | c3e6c55 | 2008-07-14 19:45:37 +0200 | [diff] [blame] | 26 | #if defined(CONFIG_CMD_DATE) |
Wolfgang Denk | c30709a | 2007-08-19 10:27:34 +0200 | [diff] [blame] | 27 | |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 28 | #include <command.h> |
| 29 | #include <rtc.h> |
TsiChungLiew | 8baf62e | 2007-07-05 23:31:25 -0500 | [diff] [blame] | 30 | #include <asm/immap.h> |
| 31 | #include <asm/rtc.h> |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 32 | |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 33 | #undef RTC_DEBUG |
| 34 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 35 | #ifndef CONFIG_SYS_MCFRTC_BASE |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 36 | #error RTC_BASE is not defined! |
| 37 | #endif |
| 38 | |
| 39 | #define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0) |
| 40 | #define STARTOFTIME 1970 |
| 41 | |
Yuri Tikhonov | 9bacd94 | 2008-03-20 17:56:04 +0300 | [diff] [blame] | 42 | int rtc_get(struct rtc_time *tmp) |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 43 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 44 | volatile rtc_t *rtc = (rtc_t *) (CONFIG_SYS_MCFRTC_BASE); |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 45 | |
| 46 | int rtc_days, rtc_hrs, rtc_mins; |
| 47 | int tim; |
| 48 | |
| 49 | rtc_days = rtc->days; |
| 50 | rtc_hrs = rtc->hourmin >> 8; |
| 51 | rtc_mins = RTC_HOURMIN_MINUTES(rtc->hourmin); |
| 52 | |
| 53 | tim = (rtc_days * 24) + rtc_hrs; |
| 54 | tim = (tim * 60) + rtc_mins; |
| 55 | tim = (tim * 60) + rtc->seconds; |
| 56 | |
| 57 | to_tm(tim, tmp); |
| 58 | |
| 59 | tmp->tm_yday = 0; |
| 60 | tmp->tm_isdst = 0; |
| 61 | |
| 62 | #ifdef RTC_DEBUG |
| 63 | printf("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n", |
| 64 | tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday, |
| 65 | tmp->tm_hour, tmp->tm_min, tmp->tm_sec); |
| 66 | #endif |
Yuri Tikhonov | 9bacd94 | 2008-03-20 17:56:04 +0300 | [diff] [blame] | 67 | |
| 68 | return 0; |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 69 | } |
| 70 | |
Jean-Christophe PLAGNIOL-VILLARD | 97a2e10 | 2008-09-01 23:06:23 +0200 | [diff] [blame] | 71 | int rtc_set(struct rtc_time *tmp) |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 72 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 73 | volatile rtc_t *rtc = (rtc_t *) (CONFIG_SYS_MCFRTC_BASE); |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 74 | |
| 75 | static int month_days[12] = { |
| 76 | 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 |
| 77 | }; |
| 78 | int days, i, months; |
| 79 | |
| 80 | if (tmp->tm_year > 2037) { |
| 81 | printf("Unable to handle. Exceeding integer limitation!\n"); |
| 82 | tmp->tm_year = 2027; |
| 83 | } |
| 84 | #ifdef RTC_DEBUG |
| 85 | printf("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n", |
| 86 | tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday, |
| 87 | tmp->tm_hour, tmp->tm_min, tmp->tm_sec); |
| 88 | #endif |
| 89 | |
| 90 | /* calculate days by years */ |
| 91 | for (i = STARTOFTIME, days = 0; i < tmp->tm_year; i++) { |
| 92 | days += 365 + isleap(i); |
| 93 | } |
| 94 | |
| 95 | /* calculate days by months */ |
| 96 | months = tmp->tm_mon - 1; |
| 97 | for (i = 0; i < months; i++) { |
| 98 | days += month_days[i]; |
| 99 | |
| 100 | if (i == 1) |
| 101 | days += isleap(i); |
| 102 | } |
| 103 | |
| 104 | days += tmp->tm_mday - 1; |
| 105 | |
| 106 | rtc->days = days; |
| 107 | rtc->hourmin = (tmp->tm_hour << 8) | tmp->tm_min; |
| 108 | rtc->seconds = tmp->tm_sec; |
Jean-Christophe PLAGNIOL-VILLARD | 97a2e10 | 2008-09-01 23:06:23 +0200 | [diff] [blame] | 109 | |
| 110 | return 0; |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | void rtc_reset(void) |
| 114 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 115 | volatile rtc_t *rtc = (rtc_t *) (CONFIG_SYS_MCFRTC_BASE); |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 116 | |
| 117 | if ((rtc->cr & RTC_CR_EN) == 0) { |
| 118 | printf("real-time-clock was stopped. Now starting...\n"); |
| 119 | rtc->cr |= RTC_CR_EN; |
| 120 | } |
| 121 | |
| 122 | rtc->cr |= RTC_CR_SWR; |
| 123 | } |
| 124 | |
TsiChungLiew | aedd3d7 | 2007-08-15 15:39:17 -0500 | [diff] [blame] | 125 | #endif /* CONFIG_MCFRTC && CONFIG_CMD_DATE */ |