3 * Freescale Semiconductor Tsi-Chung.Liew@freescale.com
5 * See file CREDITS for list of people who contributed to this
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.
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.
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,
28 #include <asm/mcfrtc.h>
30 #if defined(CONFIG_MCFRTC) && (CONFIG_COMMANDS & CFG_CMD_DATE)
34 #ifndef CFG_MCFRTC_BASE
35 #error RTC_BASE is not defined!
38 #define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
39 #define STARTOFTIME 1970
41 void rtc_get(struct rtc_time *tmp)
43 volatile rtc_t *rtc = (rtc_t *) (CFG_MCFRTC_BASE);
45 int rtc_days, rtc_hrs, rtc_mins;
49 rtc_hrs = rtc->hourmin >> 8;
50 rtc_mins = RTC_HOURMIN_MINUTES(rtc->hourmin);
52 tim = (rtc_days * 24) + rtc_hrs;
53 tim = (tim * 60) + rtc_mins;
54 tim = (tim * 60) + rtc->seconds;
62 printf("Get 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);
68 void rtc_set(struct rtc_time *tmp)
70 volatile rtc_t *rtc = (rtc_t *) (CFG_MCFRTC_BASE);
72 static int month_days[12] = {
73 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
77 if (tmp->tm_year > 2037) {
78 printf("Unable to handle. Exceeding integer limitation!\n");
82 printf("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
83 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
84 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
87 /* calculate days by years */
88 for (i = STARTOFTIME, days = 0; i < tmp->tm_year; i++) {
89 days += 365 + isleap(i);
92 /* calculate days by months */
93 months = tmp->tm_mon - 1;
94 for (i = 0; i < months; i++) {
95 days += month_days[i];
101 days += tmp->tm_mday - 1;
104 rtc->hourmin = (tmp->tm_hour << 8) | tmp->tm_min;
105 rtc->seconds = tmp->tm_sec;
110 volatile rtc_t *rtc = (rtc_t *) (CFG_MCFRTC_BASE);
112 if ((rtc->cr & RTC_CR_EN) == 0) {
113 printf("real-time-clock was stopped. Now starting...\n");
114 rtc->cr |= RTC_CR_EN;
117 rtc->cr |= RTC_CR_SWR;
120 #endif /* CONFIG_MCFRTC && CFG_CMD_DATE */