blob: e10638ec7dd3249b526adaa1ac01094de92af7c5 [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
7#include <common.h>
Wolfgang Denkc30709a2007-08-19 10:27:34 +02008
TsiChung Liewf6afe722007-06-18 13:50:13 -05009#include <command.h>
10#include <rtc.h>
TsiChungLiew8baf62e2007-07-05 23:31:25 -050011#include <asm/immap.h>
12#include <asm/rtc.h>
TsiChung Liewf6afe722007-06-18 13:50:13 -050013
TsiChung Liewf6afe722007-06-18 13:50:13 -050014#undef RTC_DEBUG
15
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020016#ifndef CONFIG_SYS_MCFRTC_BASE
TsiChung Liewf6afe722007-06-18 13:50:13 -050017#error RTC_BASE is not defined!
18#endif
19
20#define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
21#define STARTOFTIME 1970
22
Yuri Tikhonov9bacd942008-03-20 17:56:04 +030023int rtc_get(struct rtc_time *tmp)
TsiChung Liewf6afe722007-06-18 13:50:13 -050024{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020025 volatile rtc_t *rtc = (rtc_t *) (CONFIG_SYS_MCFRTC_BASE);
TsiChung Liewf6afe722007-06-18 13:50:13 -050026
27 int rtc_days, rtc_hrs, rtc_mins;
28 int tim;
29
30 rtc_days = rtc->days;
31 rtc_hrs = rtc->hourmin >> 8;
32 rtc_mins = RTC_HOURMIN_MINUTES(rtc->hourmin);
33
34 tim = (rtc_days * 24) + rtc_hrs;
35 tim = (tim * 60) + rtc_mins;
36 tim = (tim * 60) + rtc->seconds;
37
Simon Glass12994662015-04-20 12:37:18 -060038 rtc_to_tm(tim, tmp);
TsiChung Liewf6afe722007-06-18 13:50:13 -050039
40 tmp->tm_yday = 0;
41 tmp->tm_isdst = 0;
42
43#ifdef RTC_DEBUG
44 printf("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
45 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
46 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
47#endif
Yuri Tikhonov9bacd942008-03-20 17:56:04 +030048
49 return 0;
TsiChung Liewf6afe722007-06-18 13:50:13 -050050}
51
Jean-Christophe PLAGNIOL-VILLARD97a2e102008-09-01 23:06:23 +020052int rtc_set(struct rtc_time *tmp)
TsiChung Liewf6afe722007-06-18 13:50:13 -050053{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020054 volatile rtc_t *rtc = (rtc_t *) (CONFIG_SYS_MCFRTC_BASE);
TsiChung Liewf6afe722007-06-18 13:50:13 -050055
56 static int month_days[12] = {
57 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
58 };
59 int days, i, months;
60
61 if (tmp->tm_year > 2037) {
62 printf("Unable to handle. Exceeding integer limitation!\n");
63 tmp->tm_year = 2027;
64 }
65#ifdef RTC_DEBUG
66 printf("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
67 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
68 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
69#endif
70
71 /* calculate days by years */
72 for (i = STARTOFTIME, days = 0; i < tmp->tm_year; i++) {
73 days += 365 + isleap(i);
74 }
75
76 /* calculate days by months */
77 months = tmp->tm_mon - 1;
78 for (i = 0; i < months; i++) {
79 days += month_days[i];
80
81 if (i == 1)
82 days += isleap(i);
83 }
84
85 days += tmp->tm_mday - 1;
86
87 rtc->days = days;
88 rtc->hourmin = (tmp->tm_hour << 8) | tmp->tm_min;
89 rtc->seconds = tmp->tm_sec;
Jean-Christophe PLAGNIOL-VILLARD97a2e102008-09-01 23:06:23 +020090
91 return 0;
TsiChung Liewf6afe722007-06-18 13:50:13 -050092}
93
94void rtc_reset(void)
95{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020096 volatile rtc_t *rtc = (rtc_t *) (CONFIG_SYS_MCFRTC_BASE);
TsiChung Liewf6afe722007-06-18 13:50:13 -050097
98 if ((rtc->cr & RTC_CR_EN) == 0) {
99 printf("real-time-clock was stopped. Now starting...\n");
100 rtc->cr |= RTC_CR_EN;
101 }
102
103 rtc->cr |= RTC_CR_SWR;
104}