1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include <common.h>
16#include <command.h>
17#include <linux/compat.h>
18#include <rtc.h>
19
20#include <asm/io.h>
21#include <asm/arch/imx-regs.h>
22
23
24
25struct imxdi_regs {
26 u32 dtcmr;
27 u32 dtclr;
28 u32 dcamr;
29 u32 dcalr;
30 u32 dcr;
31 u32 dsr;
32 u32 dier;
33};
34
35#define DCAMR_UNSET 0xFFFFFFFF
36
37#define DCR_TCE (1 << 3)
38
39#define DSR_WBF (1 << 10)
40#define DSR_WNF (1 << 9)
41#define DSR_WCF (1 << 8)
42#define DSR_WEF (1 << 7)
43#define DSR_CAF (1 << 4)
44#define DSR_NVF (1 << 1)
45#define DSR_SVF (1 << 0)
46
47#define DIER_WNIE (1 << 9)
48#define DIER_WCIE (1 << 8)
49#define DIER_WEIE (1 << 7)
50#define DIER_CAIE (1 << 4)
51
52
53
54struct imxdi_data {
55 struct imxdi_regs __iomem *regs;
56 int init_done;
57};
58
59static struct imxdi_data data;
60
61
62
63
64
65
66
67
68static void clear_write_error(void)
69{
70 int cnt;
71
72 puts("### Warning: RTC - Register write error!\n");
73
74
75 __raw_writel(DSR_WEF, &data.regs->dsr);
76
77
78 for (cnt = 0; cnt < 1000; cnt++) {
79 if ((__raw_readl(&data.regs->dsr) & DSR_WEF) == 0)
80 return;
81 udelay(10);
82 }
83 puts("### Error: RTC - Cannot clear write-error flag!\n");
84}
85
86
87
88
89
90
91#define DI_WRITE_WAIT(val, reg) \
92( \
93 \
94 __raw_writel((val), &data.regs->reg), \
95 \
96 di_write_wait((val), #reg) \
97)
98static int di_write_wait(u32 val, const char *reg)
99{
100 int cnt;
101 int ret = 0;
102 int rc = 0;
103
104
105 for (cnt = 0; cnt < 100; cnt++) {
106 if ((__raw_readl(&data.regs->dsr) & (DSR_WCF | DSR_WEF)) != 0) {
107 ret = 1;
108 break;
109 }
110 udelay(10);
111 }
112 if (ret == 0)
113 printf("### Warning: RTC - Write-wait timeout "
114 "val = 0x%.8x reg = %s\n", val, reg);
115
116
117 if (__raw_readl(&data.regs->dsr) & DSR_WEF) {
118 clear_write_error();
119 rc = -1;
120 }
121
122 return rc;
123}
124
125
126
127
128static int di_init(void)
129{
130 int rc = 0;
131
132 data.regs = (struct imxdi_regs __iomem *)IMX_DRYICE_BASE;
133
134
135 __raw_writel(0, &data.regs->dier);
136
137
138 if (__raw_readl(&data.regs->dsr) & DSR_NVF) {
139 rc = DI_WRITE_WAIT(DSR_NVF | DSR_SVF, dsr);
140 if (rc)
141 goto err;
142 }
143
144
145 rc = DI_WRITE_WAIT(DCAMR_UNSET, dcamr);
146 if (rc)
147 goto err;
148 rc = DI_WRITE_WAIT(0, dcalr);
149 if (rc)
150 goto err;
151
152
153 if (__raw_readl(&data.regs->dsr) & DSR_CAF) {
154 rc = DI_WRITE_WAIT(DSR_CAF, dsr);
155 if (rc)
156 goto err;
157 }
158
159
160 if (__raw_readl(&data.regs->dtcmr) == 0) {
161 rc = DI_WRITE_WAIT(0, dtcmr);
162 if (rc)
163 goto err;
164 }
165
166
167 if (!(__raw_readl(&data.regs->dcr) & DCR_TCE)) {
168 rc = DI_WRITE_WAIT(__raw_readl(&data.regs->dcr) | DCR_TCE, dcr);
169 if (rc)
170 goto err;
171 }
172
173 data.init_done = 1;
174 return 0;
175
176err:
177 return rc;
178}
179
180int rtc_get(struct rtc_time *tmp)
181{
182 unsigned long now;
183 int rc = 0;
184
185 if (!data.init_done) {
186 rc = di_init();
187 if (rc)
188 goto err;
189 }
190
191 now = __raw_readl(&data.regs->dtcmr);
192 rtc_to_tm(now, tmp);
193
194err:
195 return rc;
196}
197
198int rtc_set(struct rtc_time *tmp)
199{
200 unsigned long now;
201 int rc;
202
203 if (!data.init_done) {
204 rc = di_init();
205 if (rc)
206 goto err;
207 }
208
209 now = rtc_mktime(tmp);
210
211 rc = DI_WRITE_WAIT(0, dtclr);
212 if (rc == 0)
213 rc = DI_WRITE_WAIT(now, dtcmr);
214
215err:
216 return rc;
217}
218
219void rtc_reset(void)
220{
221 di_init();
222}
223