blob: b5cc6b968816f6c67558795db6b8f444bd54a753 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
TsiChung Liewf6afe722007-06-18 13:50:13 -05002/*
TsiChungLiew8baf62e2007-07-05 23:31:25 -05003 * Copyright (C) 2004-2007 Freescale Semiconductor, Inc.
4 * TsiChung Liew (Tsi-Chung.Liew@freescale.com)
TsiChung Liewf6afe722007-06-18 13:50:13 -05005 */
6
Wolfgang Denkc30709a2007-08-19 10:27:34 +02007
TsiChung Liewf6afe722007-06-18 13:50:13 -05008#include <command.h>
9#include <rtc.h>
TsiChungLiew8baf62e2007-07-05 23:31:25 -050010#include <asm/immap.h>
11#include <asm/rtc.h>
TsiChung Liewf6afe722007-06-18 13:50:13 -050012
TsiChung Liewf6afe722007-06-18 13:50:13 -050013#undef RTC_DEBUG
14
TsiChung Liewf6afe722007-06-18 13:50:13 -050015#define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
16#define STARTOFTIME 1970
17
Yuri Tikhonov9bacd942008-03-20 17:56:04 +030018int rtc_get(struct rtc_time *tmp)
TsiChung Liewf6afe722007-06-18 13:50:13 -050019{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020020 volatile rtc_t *rtc = (rtc_t *) (CONFIG_SYS_MCFRTC_BASE);
TsiChung Liewf6afe722007-06-18 13:50:13 -050021
22 int rtc_days, rtc_hrs, rtc_mins;
23 int tim;
24
25 rtc_days = rtc->days;
26 rtc_hrs = rtc->hourmin >> 8;
27 rtc_mins = RTC_HOURMIN_MINUTES(rtc->hourmin);
28
29 tim = (rtc_days * 24) + rtc_hrs;
30 tim = (tim * 60) + rtc_mins;
31 tim = (tim * 60) + rtc->seconds;
32
Simon Glass12994662015-04-20 12:37:18 -060033 rtc_to_tm(tim, tmp);
TsiChung Liewf6afe722007-06-18 13:50:13 -050034
35 tmp->tm_yday = 0;
36 tmp->tm_isdst = 0;
37
38#ifdef RTC_DEBUG
39 printf("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
40 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
41 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
42#endif
Yuri Tikhonov9bacd942008-03-20 17:56:04 +030043
44 return 0;
TsiChung Liewf6afe722007-06-18 13:50:13 -050045}
46
Jean-Christophe PLAGNIOL-VILLARD97a2e102008-09-01 23:06:23 +020047int rtc_set(struct rtc_time *tmp)
TsiChung Liewf6afe722007-06-18 13:50:13 -050048{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020049 volatile rtc_t *rtc = (rtc_t *) (CONFIG_SYS_MCFRTC_BASE);
TsiChung Liewf6afe722007-06-18 13:50:13 -050050
51 static int month_days[12] = {
52 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
53 };
54 int days, i, months;
55
56 if (tmp->tm_year > 2037) {
57 printf("Unable to handle. Exceeding integer limitation!\n");
58 tmp->tm_year = 2027;
59 }
60#ifdef RTC_DEBUG
61 printf("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
62 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
63 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
64#endif
65
66 /* calculate days by years */
67 for (i = STARTOFTIME, days = 0; i < tmp->tm_year; i++) {
68 days += 365 + isleap(i);
69 }
70
71 /* calculate days by months */
72 months = tmp->tm_mon - 1;
73 for (i = 0; i < months; i++) {
74 days += month_days[i];
75
76 if (i == 1)
77 days += isleap(i);
78 }
79
80 days += tmp->tm_mday - 1;
81
82 rtc->days = days;
83 rtc->hourmin = (tmp->tm_hour << 8) | tmp->tm_min;
84 rtc->seconds = tmp->tm_sec;
Jean-Christophe PLAGNIOL-VILLARD97a2e102008-09-01 23:06:23 +020085
86 return 0;
TsiChung Liewf6afe722007-06-18 13:50:13 -050087}
88
89void rtc_reset(void)
90{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020091 volatile rtc_t *rtc = (rtc_t *) (CONFIG_SYS_MCFRTC_BASE);
TsiChung Liewf6afe722007-06-18 13:50:13 -050092
93 if ((rtc->cr & RTC_CR_EN) == 0) {
94 printf("real-time-clock was stopped. Now starting...\n");
95 rtc->cr |= RTC_CR_EN;
96 }
97
98 rtc->cr |= RTC_CR_SWR;
99}