blob: c7eb53fb46a3256f3cd290cad60c8f6fb458fa83 [file] [log] [blame]
TsiChung Liew8e585f02007-06-18 13:50:13 -05001/*
2 * (C) Copyright 2007
3 * Freescale Semiconductor Tsi-Chung.Liew@freescale.com
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>
25#include <command.h>
26#include <rtc.h>
27
28#include <asm/mcfrtc.h>
29
30#if defined(CONFIG_MCFRTC) && (CONFIG_COMMANDS & CFG_CMD_DATE)
31
32#undef RTC_DEBUG
33
34#ifndef CFG_MCFRTC_BASE
35#error RTC_BASE is not defined!
36#endif
37
38#define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
39#define STARTOFTIME 1970
40
41void rtc_get(struct rtc_time *tmp)
42{
43 volatile rtc_t *rtc = (rtc_t *) (CFG_MCFRTC_BASE);
44
45 int rtc_days, rtc_hrs, rtc_mins;
46 int tim;
47
48 rtc_days = rtc->days;
49 rtc_hrs = rtc->hourmin >> 8;
50 rtc_mins = RTC_HOURMIN_MINUTES(rtc->hourmin);
51
52 tim = (rtc_days * 24) + rtc_hrs;
53 tim = (tim * 60) + rtc_mins;
54 tim = (tim * 60) + rtc->seconds;
55
56 to_tm(tim, tmp);
57
58 tmp->tm_yday = 0;
59 tmp->tm_isdst = 0;
60
61#ifdef RTC_DEBUG
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);
65#endif
66}
67
68void rtc_set(struct rtc_time *tmp)
69{
70 volatile rtc_t *rtc = (rtc_t *) (CFG_MCFRTC_BASE);
71
72 static int month_days[12] = {
73 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
74 };
75 int days, i, months;
76
77 if (tmp->tm_year > 2037) {
78 printf("Unable to handle. Exceeding integer limitation!\n");
79 tmp->tm_year = 2027;
80 }
81#ifdef RTC_DEBUG
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);
85#endif
86
87 /* calculate days by years */
88 for (i = STARTOFTIME, days = 0; i < tmp->tm_year; i++) {
89 days += 365 + isleap(i);
90 }
91
92 /* calculate days by months */
93 months = tmp->tm_mon - 1;
94 for (i = 0; i < months; i++) {
95 days += month_days[i];
96
97 if (i == 1)
98 days += isleap(i);
99 }
100
101 days += tmp->tm_mday - 1;
102
103 rtc->days = days;
104 rtc->hourmin = (tmp->tm_hour << 8) | tmp->tm_min;
105 rtc->seconds = tmp->tm_sec;
106}
107
108void rtc_reset(void)
109{
110 volatile rtc_t *rtc = (rtc_t *) (CFG_MCFRTC_BASE);
111
112 if ((rtc->cr & RTC_CR_EN) == 0) {
113 printf("real-time-clock was stopped. Now starting...\n");
114 rtc->cr |= RTC_CR_EN;
115 }
116
117 rtc->cr |= RTC_CR_SWR;
118}
119
120#endif /* CONFIG_MCFRTC && CFG_CMD_DATE */