1
2
3
4
5
6
7
8
9
10#include <common.h>
11#include <command.h>
12#include <rtc.h>
13
14#if defined(CONFIG_CMD_DATE)
15
16#ifndef CONFIG_SYS_RTC_PL031_BASE
17#error CONFIG_SYS_RTC_PL031_BASE is not defined!
18#endif
19
20
21
22
23#define RTC_DR 0x00
24#define RTC_MR 0x04
25#define RTC_LR 0x08
26#define RTC_CR 0x0c
27#define RTC_IMSC 0x10
28#define RTC_RIS 0x14
29#define RTC_MIS 0x18
30#define RTC_ICR 0x1c
31
32#define RTC_CR_START (1 << 0)
33
34#define RTC_WRITE_REG(addr, val) \
35 (*(volatile unsigned int *)(CONFIG_SYS_RTC_PL031_BASE + (addr)) = (val))
36#define RTC_READ_REG(addr) \
37 (*(volatile unsigned int *)(CONFIG_SYS_RTC_PL031_BASE + (addr)))
38
39static int pl031_initted = 0;
40
41
42void rtc_init(void)
43{
44 RTC_WRITE_REG(RTC_CR, RTC_CR_START);
45
46 pl031_initted = 1;
47}
48
49
50
51
52void rtc_reset(void)
53{
54 RTC_WRITE_REG(RTC_LR, 0x00);
55 if(!pl031_initted)
56 rtc_init();
57}
58
59
60
61
62int rtc_set(struct rtc_time *tmp)
63{
64 unsigned long tim;
65
66 if(!pl031_initted)
67 rtc_init();
68
69 if (tmp == NULL) {
70 puts("Error setting the date/time\n");
71 return -1;
72 }
73
74
75 tim = mktime(tmp->tm_year, tmp->tm_mon, tmp->tm_mday,
76 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
77
78 RTC_WRITE_REG(RTC_LR, tim);
79
80 return -1;
81}
82
83
84
85
86int rtc_get(struct rtc_time *tmp)
87{
88 ulong tim;
89
90 if(!pl031_initted)
91 rtc_init();
92
93 if (tmp == NULL) {
94 puts("Error getting the date/time\n");
95 return -1;
96 }
97
98 tim = RTC_READ_REG(RTC_DR);
99
100 to_tm (tim, tmp);
101
102 debug ( "Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
103 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
104 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
105
106 return 0;
107}
108
109#endif
110