1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2004-2007 Freescale Semiconductor, Inc.
4 * TsiChung Liew (Tsi-Chung.Liew@freescale.com)
9 #if defined(CONFIG_CMD_DATE)
13 #include <asm/immap.h>
18 #ifndef CONFIG_SYS_MCFRTC_BASE
19 #error RTC_BASE is not defined!
22 #define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
23 #define STARTOFTIME 1970
25 int rtc_get(struct rtc_time *tmp)
27 volatile rtc_t *rtc = (rtc_t *) (CONFIG_SYS_MCFRTC_BASE);
29 int rtc_days, rtc_hrs, rtc_mins;
33 rtc_hrs = rtc->hourmin >> 8;
34 rtc_mins = RTC_HOURMIN_MINUTES(rtc->hourmin);
36 tim = (rtc_days * 24) + rtc_hrs;
37 tim = (tim * 60) + rtc_mins;
38 tim = (tim * 60) + rtc->seconds;
46 printf("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
47 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
48 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
54 int rtc_set(struct rtc_time *tmp)
56 volatile rtc_t *rtc = (rtc_t *) (CONFIG_SYS_MCFRTC_BASE);
58 static int month_days[12] = {
59 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
63 if (tmp->tm_year > 2037) {
64 printf("Unable to handle. Exceeding integer limitation!\n");
68 printf("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
69 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
70 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
73 /* calculate days by years */
74 for (i = STARTOFTIME, days = 0; i < tmp->tm_year; i++) {
75 days += 365 + isleap(i);
78 /* calculate days by months */
79 months = tmp->tm_mon - 1;
80 for (i = 0; i < months; i++) {
81 days += month_days[i];
87 days += tmp->tm_mday - 1;
90 rtc->hourmin = (tmp->tm_hour << 8) | tmp->tm_min;
91 rtc->seconds = tmp->tm_sec;
98 volatile rtc_t *rtc = (rtc_t *) (CONFIG_SYS_MCFRTC_BASE);
100 if ((rtc->cr & RTC_CR_EN) == 0) {
101 printf("real-time-clock was stopped. Now starting...\n");
102 rtc->cr |= RTC_CR_EN;
105 rtc->cr |= RTC_CR_SWR;
108 #endif /* CONFIG_MCFRTC && CONFIG_CMD_DATE */