1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * (C) Copyright 2008
4 * Gururaja Hebbar gururajakr@sanyo.co.in
5 *
6 * reference linux-2.6.20.6/drivers/rtc/rtc-pl031.c
7 */
8
9 #include <common.h>
10 #include <command.h>
11 #include <rtc.h>
12
13 #if defined(CONFIG_CMD_DATE)
14
15 #ifndef CONFIG_SYS_RTC_PL031_BASE
16 #error CONFIG_SYS_RTC_PL031_BASE is not defined!
17 #endif
18
19 /*
20 * Register definitions
21 */
22 #define RTC_DR 0x00 /* Data read register */
23 #define RTC_MR 0x04 /* Match register */
24 #define RTC_LR 0x08 /* Data load register */
25 #define RTC_CR 0x0c /* Control register */
26 #define RTC_IMSC 0x10 /* Interrupt mask and set register */
27 #define RTC_RIS 0x14 /* Raw interrupt status register */
28 #define RTC_MIS 0x18 /* Masked interrupt status register */
29 #define RTC_ICR 0x1c /* Interrupt clear register */
30
31 #define RTC_CR_START (1 << 0)
32
33 #define RTC_WRITE_REG(addr, val) \
34 (*(volatile unsigned int *)(CONFIG_SYS_RTC_PL031_BASE + (addr)) = (val))
35 #define RTC_READ_REG(addr) \
36 (*(volatile unsigned int *)(CONFIG_SYS_RTC_PL031_BASE + (addr)))
37
38 static int pl031_initted = 0;
39
40 /* Enable RTC Start in Control register*/
rtc_init(void)41 void rtc_init(void)
42 {
43 RTC_WRITE_REG(RTC_CR, RTC_CR_START);
44
45 pl031_initted = 1;
46 }
47
48 /*
49 * Reset the RTC. We set the date back to 1970-01-01.
50 */
rtc_reset(void)51 void rtc_reset(void)
52 {
53 RTC_WRITE_REG(RTC_LR, 0x00);
54 if(!pl031_initted)
55 rtc_init();
56 }
57
58 /*
59 * Set the RTC
60 */
rtc_set(struct rtc_time * tmp)61 int rtc_set(struct rtc_time *tmp)
62 {
63 unsigned long tim;
64
65 if(!pl031_initted)
66 rtc_init();
67
68 if (tmp == NULL) {
69 puts("Error setting the date/time\n");
70 return -1;
71 }
72
73 /* Calculate number of seconds this incoming time represents */
74 tim = rtc_mktime(tmp);
75
76 RTC_WRITE_REG(RTC_LR, tim);
77
78 return -1;
79 }
80
81 /*
82 * Get the current time from the RTC
83 */
rtc_get(struct rtc_time * tmp)84 int rtc_get(struct rtc_time *tmp)
85 {
86 ulong tim;
87
88 if(!pl031_initted)
89 rtc_init();
90
91 if (tmp == NULL) {
92 puts("Error getting the date/time\n");
93 return -1;
94 }
95
96 tim = RTC_READ_REG(RTC_DR);
97
98 rtc_to_tm(tim, tmp);
99
100 debug ( "Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
101 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
102 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
103
104 return 0;
105 }
106
107 #endif
108