2 * Copyright (C) 2004-2007 Freescale Semiconductor, Inc.
3 * TsiChung Liew (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,
26 #if defined(CONFIG_MCFRTC) && defined(CONFIG_CMD_DATE)
30 #include <asm/immap.h>
35 #ifndef CFG_MCFRTC_BASE
36 #error RTC_BASE is not defined!
39 #define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
40 #define STARTOFTIME 1970
42 void rtc_get(struct rtc_time *tmp)
44 volatile rtc_t *rtc = (rtc_t *) (CFG_MCFRTC_BASE);
46 int rtc_days, rtc_hrs, rtc_mins;
50 rtc_hrs = rtc->hourmin >> 8;
51 rtc_mins = RTC_HOURMIN_MINUTES(rtc->hourmin);
53 tim = (rtc_days * 24) + rtc_hrs;
54 tim = (tim * 60) + rtc_mins;
55 tim = (tim * 60) + rtc->seconds;
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);
69 void rtc_set(struct rtc_time *tmp)
71 volatile rtc_t *rtc = (rtc_t *) (CFG_MCFRTC_BASE);
73 static int month_days[12] = {
74 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
78 if (tmp->tm_year > 2037) {
79 printf("Unable to handle. Exceeding integer limitation!\n");
83 printf("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
84 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
85 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
88 /* calculate days by years */
89 for (i = STARTOFTIME, days = 0; i < tmp->tm_year; i++) {
90 days += 365 + isleap(i);
93 /* calculate days by months */
94 months = tmp->tm_mon - 1;
95 for (i = 0; i < months; i++) {
96 days += month_days[i];
102 days += tmp->tm_mday - 1;
105 rtc->hourmin = (tmp->tm_hour << 8) | tmp->tm_min;
106 rtc->seconds = tmp->tm_sec;
111 volatile rtc_t *rtc = (rtc_t *) (CFG_MCFRTC_BASE);
113 if ((rtc->cr & RTC_CR_EN) == 0) {
114 printf("real-time-clock was stopped. Now starting...\n");
115 rtc->cr |= RTC_CR_EN;
118 rtc->cr |= RTC_CR_SWR;
121 #endif /* CONFIG_MCFRTC && CONFIG_CMD_DATE */