]>
git.ipfire.org Git - people/ms/u-boot.git/blob - drivers/rtc/mcfrtc.c
2 * Copyright (C) 2004-2007 Freescale Semiconductor, Inc.
3 * TsiChung Liew (Tsi-Chung.Liew@freescale.com)
5 * SPDX-License-Identifier: GPL-2.0+
10 #if defined(CONFIG_CMD_DATE)
14 #include <asm/immap.h>
19 #ifndef CONFIG_SYS_MCFRTC_BASE
20 #error RTC_BASE is not defined!
23 #define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
24 #define STARTOFTIME 1970
26 int rtc_get(struct rtc_time
*tmp
)
28 volatile rtc_t
*rtc
= (rtc_t
*) (CONFIG_SYS_MCFRTC_BASE
);
30 int rtc_days
, rtc_hrs
, rtc_mins
;
34 rtc_hrs
= rtc
->hourmin
>> 8;
35 rtc_mins
= RTC_HOURMIN_MINUTES(rtc
->hourmin
);
37 tim
= (rtc_days
* 24) + rtc_hrs
;
38 tim
= (tim
* 60) + rtc_mins
;
39 tim
= (tim
* 60) + rtc
->seconds
;
47 printf("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
48 tmp
->tm_year
, tmp
->tm_mon
, tmp
->tm_mday
, tmp
->tm_wday
,
49 tmp
->tm_hour
, tmp
->tm_min
, tmp
->tm_sec
);
55 int rtc_set(struct rtc_time
*tmp
)
57 volatile rtc_t
*rtc
= (rtc_t
*) (CONFIG_SYS_MCFRTC_BASE
);
59 static int month_days
[12] = {
60 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
64 if (tmp
->tm_year
> 2037) {
65 printf("Unable to handle. Exceeding integer limitation!\n");
69 printf("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
70 tmp
->tm_year
, tmp
->tm_mon
, tmp
->tm_mday
, tmp
->tm_wday
,
71 tmp
->tm_hour
, tmp
->tm_min
, tmp
->tm_sec
);
74 /* calculate days by years */
75 for (i
= STARTOFTIME
, days
= 0; i
< tmp
->tm_year
; i
++) {
76 days
+= 365 + isleap(i
);
79 /* calculate days by months */
80 months
= tmp
->tm_mon
- 1;
81 for (i
= 0; i
< months
; i
++) {
82 days
+= month_days
[i
];
88 days
+= tmp
->tm_mday
- 1;
91 rtc
->hourmin
= (tmp
->tm_hour
<< 8) | tmp
->tm_min
;
92 rtc
->seconds
= tmp
->tm_sec
;
99 volatile rtc_t
*rtc
= (rtc_t
*) (CONFIG_SYS_MCFRTC_BASE
);
101 if ((rtc
->cr
& RTC_CR_EN
) == 0) {
102 printf("real-time-clock was stopped. Now starting...\n");
103 rtc
->cr
|= RTC_CR_EN
;
106 rtc
->cr
|= RTC_CR_SWR
;
109 #endif /* CONFIG_MCFRTC && CONFIG_CMD_DATE */