-
Notifications
You must be signed in to change notification settings - Fork 8
/
omap_TMR.c
executable file
·127 lines (94 loc) · 2.56 KB
/
omap_TMR.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
//(c) uARM project https://github.com/uARM-Palm/uARM [email protected]
#include "omap_TMR.h"
#include "omap_IC.h"
#include <string.h>
#include <stdlib.h>
#include "util.h"
#include "mem.h"
#define OMAP_TMR_SIZE 0x10
struct OmapTmr {
struct OmapMisc *misc;
struct SocIc *ic;
int8_t irqNo;
uint8_t prescaleCtr, ctrl;
uint32_t counter, reloadVal;
//cpu clock rescaler;
uint8_t cpuCtr;
};
static bool omapTmrPrvMemAccessF(void* userData, uint32_t pa, uint_fast8_t size, bool write, void* buf)
{
struct OmapTmr *tmr = (struct OmapTmr*)userData;
uint32_t val = 0;
if (size != 4) {
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_TMR_SIZE) >> 2;
if (write)
val = *(uint32_t*)buf;
switch (pa) {
case 0x00 / 4:
if (write) {
if (!(tmr->ctrl & 1) && (val & 1)) //starting
tmr->counter = tmr->reloadVal;
tmr->ctrl = val & 0x7f;
}
else
val = tmr->ctrl;
break;
case 0x04 / 4:
if (write)
tmr->reloadVal = val;
else
return false;
break;
case 0x08 / 4:
if (write)
return false;
else
val = tmr->counter;
break;
default:
return false;
}
if (!write)
*(uint32_t*)buf = val;
return true;
}
struct OmapTmr* omapTmrInit(struct ArmMem *physMem, struct SocIc *ic, struct OmapMisc *misc, uint32_t base, int8_t irqNo)
{
struct OmapTmr *tmr = (struct OmapTmr*)malloc(sizeof(*tmr));
if (!tmr)
ERR("cannot alloc TMR@0x%08lx", (unsigned long)base);
memset(tmr, 0, sizeof (*tmr));
tmr->misc = misc;
tmr->ic = ic;
tmr->irqNo = irqNo;
if (!memRegionAdd(physMem, base, OMAP_TMR_SIZE, omapTmrPrvMemAccessF, tmr))
ERR("cannot add TMR@0x%08lx to MEM\n", (unsigned long)base);
return tmr;
}
bool omapMiscIsTimerAtCpuSpeed(struct OmapMisc *misc); //timers either run as cpu speed or 12mhz speed
void omapTmrPeriodic(struct OmapTmr *tmr)
{
if (!(tmr->ctrl & 1))
return;
if (!omapMiscIsTimerAtCpuSpeed(tmr->misc) && ++tmr->cpuCtr < 8) //pretend cpu is at 96 mhz
return;
tmr->cpuCtr = 0;
if ((++tmr->prescaleCtr >= (2 << ((tmr->ctrl >> 2) & 7)))) {
tmr->prescaleCtr = 0;
if (tmr->counter)
tmr->counter--;
else {
if (tmr->ctrl & 0x02)
tmr->counter = tmr->reloadVal;
else
tmr->ctrl &=~ 1;
if (tmr->irqNo >= 0) {
socIcInt(tmr->ic, tmr->irqNo, true); //edge triggered
socIcInt(tmr->ic, tmr->irqNo, false);
}
}
}
}