-
Notifications
You must be signed in to change notification settings - Fork 8
/
omap_32kTMR.c
executable file
·121 lines (95 loc) · 2.44 KB
/
omap_32kTMR.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
//(c) uARM project https://github.com/uARM-Palm/uARM [email protected]
#include "omap_32kTMR.h"
#include "omap_IC.h"
#include <string.h>
#include <stdlib.h>
#include "util.h"
#include "mem.h"
#define OMAP_32kTMR_BASE 0xFFFB9000UL
#define OMAP_32kTMR_SIZE 0x10
struct Omap32kTmr {
struct SocIc *ic;
uint8_t cr;
uint32_t counter, reloadVal;
};
static bool omap32kTmrPrvMemAccessF(void* userData, uint32_t pa, uint_fast8_t size, bool write, void* buf)
{
struct Omap32kTmr *tmr = (struct Omap32kTmr*)userData;
uint32_t val = 0;
if ((size != 2 && 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_32kTMR_SIZE) >> 2;
if (write)
val = (size == 2) ? *(uint16_t*)buf : *(uint32_t*)buf;
switch (pa) {
case 0x00 / 4:
if (write)
tmr->reloadVal = val & 0xfffffful;
else
val = tmr->reloadVal;
break;
case 0x04 / 4:
if (write)
return false;
else
val = tmr->counter;
break;
case 0x08 / 4:
if (write) {
if ((val & 1) && !(tmr->cr & 1)) //just turned on - reload the reload value now!
tmr->counter = tmr->reloadVal;
tmr->cr = val & 0x0d;
if (val & 2)
tmr->counter = tmr->reloadVal;
}
else
val = tmr->cr;
break;
default:
return false;
}
if (!write) {
if (size == 2)
*(uint16_t*)buf = val;
else
*(uint32_t*)buf = val;
}
return true;
}
struct Omap32kTmr* omap32kTmrInit(struct ArmMem *physMem, struct SocIc *ic)
{
struct Omap32kTmr *tmr = (struct Omap32kTmr*)malloc(sizeof(*tmr));
if (!tmr)
ERR("cannot alloc 32kTMR");
memset(tmr, 0, sizeof (*tmr));
tmr->ic = ic;
tmr->cr = 8;
tmr->counter = 0xfffffful;
tmr->reloadVal = 0xfffffful;
if (!memRegionAdd(physMem, OMAP_32kTMR_BASE, OMAP_32kTMR_SIZE, omap32kTmrPrvMemAccessF, tmr))
ERR("cannot add 32kTMR to MEM\n");
return tmr;
}
void omap32kTmrPeriodic(struct Omap32kTmr *tmr)
{
if (tmr->cr & 1) {
if (tmr->counter)
tmr->counter--;
else {
if (tmr->cr & 0x08) {
tmr->counter = tmr->reloadVal;
if (!tmr->reloadVal)
ERR("reload to zero?\n");
}
else
tmr->cr &=~ 1;
if (tmr->cr & 4) {
//edge
socIcInt(tmr->ic, OMAP_I_TIMER32K, true);
socIcInt(tmr->ic, OMAP_I_TIMER32K, false);
}
}
}
}