-
Notifications
You must be signed in to change notification settings - Fork 8
/
omap_RTC.c
executable file
·358 lines (282 loc) · 7.11 KB
/
omap_RTC.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
//(c) uARM project https://github.com/uARM-Palm/uARM uARM@dmitry.gr
#include "omap_RTC.h"
#include "omap_IC.h"
#include <string.h>
#include <stdlib.h>
#include "util.h"
#include "mem.h"
#define OMAP_RTC_BASE 0xFFFB4800UL
#define OMAP_RTC_SIZE 256
struct Time {
uint8_t sec, min, hr, day, month, yr;
};
struct OmapRtc {
struct Time now;
uint8_t nowWeekday;
struct Time alarm;
uint8_t ctrl, sta, intr, compL, compH;
struct SocIc *ic;
};
static void omapRtcPrvIrqUpdate(struct OmapRtc *rtc, bool sendEvtIrqs)
{
bool tick = false;
if (!(rtc->intr & 8) || !(rtc->sta & 0x40))
socIcInt(rtc->ic, OMAP_I_RTC_ALM, false);
if (sendEvtIrqs && (rtc->intr & 4)) {
switch (rtc->intr & 3){
case 0: //every second
tick = true;
break;
case 1: //every minute
tick = !rtc->now.sec;
break;
case 2: //every hour
tick = !rtc->now.min;
break;
case 3: //every day
tick = rtc->now.hr == ((rtc->ctrl & 8) ? 12 : 0);
break;
}
if (tick) {
//edge
socIcInt(rtc->ic, OMAP_I_RTC_TICK, true);
socIcInt(rtc->ic, OMAP_I_RTC_TICK, false);
}
}
}
static uint_fast8_t omapRtcPrvNumCurMonthDays(struct OmapRtc *rtc, struct Time *tm)
{
uint_fast8_t month = 10 * (tm->month >> 4) + (tm->month & 0x0f);
uint_fast8_t year = 10 * (tm->yr >> 4) + (tm->yr & 0x0f);
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
case 4:
case 6:
case 9:
case 11:
return 30;
case 2:
return (year % 4) ? 28 : 29;
default:
__builtin_unreachable();
return 0;
}
}
static void omapRtcPrvNormalizeBcd(uint8_t *valP, uint_fast8_t min, uint_fast8_t max)
{
uint_fast8_t val = 10 * (*valP >> 4) + (*valP & 0x0f);
if (val < min)
val = min;
else if (val > max)
val = max;
*valP = (val / 10) * 16 + val % 10;
}
static void omapRtcPrvNormalizeTime(struct OmapRtc *rtc, struct Time *tm)
{
uint8_t monthDays;
omapRtcPrvNormalizeBcd(&tm->sec, 0, 59);
omapRtcPrvNormalizeBcd(&tm->min, 0, 59);
if (rtc->ctrl & 8) { //12h
uint8_t amPm = tm->hr & 0x80;
tm->hr &= 0x7f;
omapRtcPrvNormalizeBcd(&tm->hr, 1, 12);
tm->hr |= amPm;
}
else
omapRtcPrvNormalizeBcd(&tm->hr, 0, 23);
omapRtcPrvNormalizeBcd(&tm->yr, 0, 99);
omapRtcPrvNormalizeBcd(&tm->month, 1, 12);
omapRtcPrvNormalizeBcd(&tm->day, 1, omapRtcPrvNumCurMonthDays(rtc, tm));
}
static uint_fast8_t omapRtcPrvHours12to24(uint_fast8_t hr)
{
uint_fast8_t tm = 10 * ((hr & 0x70) >> 4) + (hr & 0x0f);
if (tm == 12)
tm = 0;
if (hr & 0x80)
tm += 12;
return (tm / 10) * 16 + tm % 10;
}
static uint_fast8_t omapRtcPrvHours24to12(uint_fast8_t hr)
{
uint_fast8_t amPm = 0;
hr = (hr / 16 * 10) + hr % 16;
if (hr >= 12) {
amPm = 0x80;
hr -= 12;
}
if (!hr)
hr = 12;
return hr / 10 * 16 + hr % 10 + amPm;
}
static bool omapRtcPrvMemAccessF(void* userData, uint32_t pa, uint_fast8_t size, bool write, void* buf)
{
struct OmapRtc *rtc = (struct OmapRtc*)userData;
uint_fast8_t val = 0;
if ((size != 1 && size != 4) || (pa & 3)) {
fprintf(stderr, "%s: Unexpected %s of %u bytes to 0x%08lx\n", __func__, write ? "write" : "read", size, (unsigned long)pa);
return false;
}
pa = (pa - OMAP_RTC_BASE) >> 2;
if (write)
val = size == 1 ? *(uint8_t*)buf : *(uint32_t*)buf;
if (pa < 0x40 / 4) {
struct Time *tm = (pa >= 0x20 / 4) ? &rtc->alarm : &rtc->now;
uint8_t *datumP = NULL;
switch (pa % (0x20 / 4)) {
case 0x00 / 4:
datumP = &tm->sec;
break;
case 0x04 / 4:
datumP = &tm->min;
break;
case 0x08 / 4:
datumP = &tm->hr;
break;
case 0x0c / 4:
datumP = &tm->day;
break;
case 0x10 / 4:
datumP = &tm->month;
break;
case 0x14 / 4:
datumP = &tm->yr;
break;
case 0x18 / 4:
if (tm == &rtc->now)
datumP = &rtc->nowWeekday;
break;
}
if (!datumP)
return false;
if (write) {
*datumP = val;
omapRtcPrvNormalizeTime(rtc, tm);
rtc->nowWeekday %= 7;
omapRtcPrvIrqUpdate(rtc, false);
}
else
val = *datumP;
}
else switch (pa) {
case 0x40 / 4:
if (write) {
if ((val ^ rtc->ctrl) & 8) { //12/24 hr mode changed
if (val & 8) { //went to 12h mode
rtc->now.hr = omapRtcPrvHours24to12(rtc->now.hr);
rtc->alarm.hr = omapRtcPrvHours24to12(rtc->alarm.hr);
}
else {
rtc->now.hr = omapRtcPrvHours12to24(rtc->now.hr);
rtc->alarm.hr = omapRtcPrvHours12to24(rtc->alarm.hr);
}
}
rtc->ctrl = val & 0x7f;
}
else
val = rtc->ctrl;
break;
case 0x44 / 4:
if (write) {
rtc->sta &=~ (val & 0xc0);
omapRtcPrvIrqUpdate(rtc, false);
}
else
val = rtc->sta;
break;
case 0x48 / 4:
if (write) {
rtc->intr = val & 0x0f;
omapRtcPrvIrqUpdate(rtc, false);
}
else
val = rtc->intr;
break;
case 0x4c / 4:
if (write)
rtc->compL = val;
else
val = rtc->compL;
break;
case 0x50 / 4:
if (write)
rtc->compH = val;
else
val = rtc->compH;
break;
default:
return false;
}
if (!write) {
if (size == 1)
*(uint8_t*)buf = val;
else
*(uint32_t*)buf = val;
}
return true;
}
struct OmapRtc* omapRtcInit(struct ArmMem *physMem, struct SocIc *ic)
{
struct OmapRtc *rtc = (struct OmapRtc*)malloc(sizeof(*rtc));
if (!rtc)
ERR("cannot alloc RTC");
memset(rtc, 0, sizeof (*rtc));
rtc->ic = ic;
rtc->sta = 0x80;
rtc->now.sec = 1;
rtc->now.min = 1;
rtc->now.hr = 8;
rtc->now.day = 0x28;
rtc->now.month = 3;
rtc->now.yr = 0x88;
rtc->nowWeekday = 0;
if (!memRegionAdd(physMem, OMAP_RTC_BASE, OMAP_RTC_SIZE, omapRtcPrvMemAccessF, rtc))
ERR("cannot add RTC to MEM\n");
return rtc;
}
//return true if overflowed
static bool omapRtcPrvBcdIncrement(uint8_t *valP, uint_fast8_t min, uint_fast8_t max)
{
uint_fast8_t val = 10 * (*valP >> 4) + (*valP & 0x0f);
bool ret = false;
if (val == max) {
ret = true;
val = min;
}
else
val++;
*valP = (val / 10) * 16 + val % 10;
return ret;
}
void omapRtcPeriodic(struct OmapRtc *rtc)
{
//time ticks
if (omapRtcPrvBcdIncrement(&rtc->now.sec, 0, 59) && omapRtcPrvBcdIncrement(&rtc->now.min, 0, 59)) {
bool goOn;
if (rtc->ctrl & 8) { //12 h
uint8_t hr = omapRtcPrvHours12to24(rtc->now.hr);
goOn = omapRtcPrvBcdIncrement(&hr, 0, 23);
rtc->now.hr = omapRtcPrvHours24to12(hr);
}
else
goOn = omapRtcPrvBcdIncrement(&rtc->now.hr, 0, 23);
if (goOn && omapRtcPrvBcdIncrement(&rtc->now.day, 0, omapRtcPrvNumCurMonthDays(rtc, &rtc->now))) {
//next month
if (omapRtcPrvBcdIncrement(&rtc->now.month, 1, 12) && omapRtcPrvBcdIncrement(&rtc->now.yr, 0, 99))
ERR("year overflows");
}
}
if (!memcmp(&rtc->now, &rtc->alarm, sizeof(struct Time))) {
rtc->sta |= 0x40;
if (rtc->intr & 8)
socIcInt(rtc->ic, OMAP_I_RTC_ALM, true);
}
omapRtcPrvIrqUpdate(rtc, true);
}