1
2
3
4
5
6
7
8
9#include <config.h>
10#include <common.h>
11#include <rtc.h>
12#include <asm/io.h>
13
14struct ftrtc010 {
15 unsigned int sec;
16 unsigned int min;
17 unsigned int hour;
18 unsigned int day;
19 unsigned int alarm_sec;
20 unsigned int alarm_min;
21 unsigned int alarm_hour;
22 unsigned int record;
23 unsigned int cr;
24 unsigned int wsec;
25 unsigned int wmin;
26 unsigned int whour;
27 unsigned int wday;
28 unsigned int intr;
29 unsigned int div;
30 unsigned int rev;
31};
32
33
34
35
36#define FTRTC010_CR_ENABLE (1 << 0)
37#define FTRTC010_CR_INTERRUPT_SEC (1 << 1)
38#define FTRTC010_CR_INTERRUPT_MIN (1 << 2)
39#define FTRTC010_CR_INTERRUPT_HR (1 << 3)
40#define FTRTC010_CR_INTERRUPT_DAY (1 << 4)
41
42static struct ftrtc010 *rtc = (struct ftrtc010 *)CONFIG_FTRTC010_BASE;
43
44static void ftrtc010_enable(void)
45{
46 writel(FTRTC010_CR_ENABLE, &rtc->cr);
47}
48
49
50
51
52static unsigned long ftrtc010_time(void)
53{
54 unsigned long day;
55 unsigned long hour;
56 unsigned long minute;
57 unsigned long second;
58 unsigned long second2;
59
60 do {
61 second = readl(&rtc->sec);
62 day = readl(&rtc->day);
63 hour = readl(&rtc->hour);
64 minute = readl(&rtc->min);
65 second2 = readl(&rtc->sec);
66 } while (second != second2);
67
68 return day * 24 * 60 * 60 + hour * 60 * 60 + minute * 60 + second;
69}
70
71
72
73
74
75int rtc_get(struct rtc_time *tmp)
76{
77 unsigned long now;
78
79 debug("%s(): record register: %x\n",
80 __func__, readl(&rtc->record));
81
82#ifdef CONFIG_FTRTC010_PCLK
83 now = (ftrtc010_time() + readl(&rtc->record)) / RTC_DIV_COUNT;
84#else
85 now = ftrtc010_time() + readl(&rtc->record);
86#endif
87
88 rtc_to_tm(now, tmp);
89
90 return 0;
91}
92
93
94
95
96int rtc_set(struct rtc_time *tmp)
97{
98 unsigned long new;
99 unsigned long now;
100
101 debug("%s(): DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
102 __func__,
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 new = rtc_mktime(tmp);
107
108 now = ftrtc010_time();
109
110 debug("%s(): write %lx to record register\n", __func__, new - now);
111
112 writel(new - now, &rtc->record);
113
114 return 0;
115}
116
117void rtc_reset(void)
118{
119 debug("%s()\n", __func__);
120 ftrtc010_enable();
121}
122