-
Notifications
You must be signed in to change notification settings - Fork 138
/
timer.c
257 lines (213 loc) · 7.55 KB
/
timer.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
#include "openiboot.h"
#include "timer.h"
#include "clock.h"
#include "interrupt.h"
#include "hardware/timer.h"
const TimerRegisters HWTimers[] = {
{ TIMER + TIMER_0 + TIMER_CONFIG, TIMER + TIMER_0 + TIMER_STATE, TIMER + TIMER_0 + TIMER_COUNT_BUFFER,
TIMER + TIMER_0 + TIMER_COUNT_BUFFER2, TIMER + TIMER_0 + TIMER_PRESCALER, TIMER + TIMER_0 + TIMER_UNKNOWN3 },
{ TIMER + TIMER_1 + TIMER_CONFIG, TIMER + TIMER_1 + TIMER_STATE, TIMER + TIMER_1 + TIMER_COUNT_BUFFER,
TIMER + TIMER_1 + TIMER_COUNT_BUFFER2, TIMER + TIMER_1 + TIMER_PRESCALER, TIMER + TIMER_1 + TIMER_UNKNOWN3 },
{ TIMER + TIMER_2 + TIMER_CONFIG, TIMER + TIMER_2 + TIMER_STATE, TIMER + TIMER_2 + TIMER_COUNT_BUFFER,
TIMER + TIMER_2 + TIMER_COUNT_BUFFER2, TIMER + TIMER_2 + TIMER_PRESCALER, TIMER + TIMER_2 + TIMER_UNKNOWN3 },
{ TIMER + TIMER_3 + TIMER_CONFIG, TIMER + TIMER_3 + TIMER_STATE, TIMER + TIMER_3 + TIMER_COUNT_BUFFER,
TIMER + TIMER_3 + TIMER_COUNT_BUFFER2, TIMER + TIMER_3 + TIMER_PRESCALER, TIMER + TIMER_3 + TIMER_UNKNOWN3 },
{ TIMER + TIMER_4 + TIMER_CONFIG, TIMER + TIMER_4 + TIMER_STATE, TIMER + TIMER_4 + TIMER_COUNT_BUFFER,
TIMER + TIMER_4 + TIMER_COUNT_BUFFER2, TIMER + TIMER_4 + TIMER_PRESCALER, TIMER + TIMER_4 + TIMER_UNKNOWN3 },
{ TIMER + TIMER_5 + TIMER_CONFIG, TIMER + TIMER_5 + TIMER_STATE, TIMER + TIMER_5 + TIMER_COUNT_BUFFER,
TIMER + TIMER_5 + TIMER_COUNT_BUFFER2, TIMER + TIMER_5 + TIMER_PRESCALER, TIMER + TIMER_5 + TIMER_UNKNOWN3 },
{ TIMER + TIMER_6 + TIMER_CONFIG, TIMER + TIMER_6 + TIMER_STATE, TIMER + TIMER_6 + TIMER_COUNT_BUFFER,
TIMER + TIMER_6 + TIMER_COUNT_BUFFER2, TIMER + TIMER_6 + TIMER_PRESCALER, TIMER + TIMER_6 + TIMER_UNKNOWN3 }
};
TimerInfo Timers[7];
//static void timerIRQHandler(uint32_t token);
//static void callTimerHandler(int timer_id, uint32_t flags);
int RTCHasInit;
/*static void timer_init_rtc() {
SET_REG(TIMER + TIMER_UNKREG0, TIMER_UNKREG0_RESET1);
SET_REG(TIMER + TIMER_UNKREG2, TIMER_UNKREG2_RESET);
SET_REG(TIMER + TIMER_UNKREG1, TIMER_UNKREG1_RESET);
SET_REG(TIMER + TIMER_UNKREG4, TIMER_UNKREG4_RESET);
SET_REG(TIMER + TIMER_UNKREG3, TIMER_UNKREG3_RESET);
SET_REG(TIMER + TIMER_UNKREG0, TIMER_UNKREG0_RESET2);
RTCHasInit = TRUE;
}*/
int timer_setup() {
// Let's just assume this is timer setup...
// Why? It sets TicksPerSec, 2G/IPHONE_3G timer_setup did that, too.
int jep = 1;
if (jep) {
SET_REG(0xBF500004, GET_REG(0xBF500004) | 0x80000000);
}
SET_REG(0xBF101218, 0xEF0000);
SET_REG(0xBF10121C, 0xFC020408);
SET_REG(0xBF101220, 0x27C0011);
SET_REG(0xBF101228, 0x1830006);
SET_REG(0xBF101230, 0x12F0006);
SET_REG(0xBF101224, 0x20404);
SET_REG(0xBF10122C, 0x20404);
SET_REG(0xBF101234, 0x20409);
SET_REG(0xBF101200, 0);
SET_REG(0xBF101000, 0x3F);
return 0;
}
int timer_init(int timer_id, uint32_t interval, uint32_t interval2, uint32_t prescaler, uint32_t z, Boolean option24, Boolean option28, Boolean option11, Boolean option5, Boolean interrupts) {
if(timer_id >= NUM_TIMERS || timer_id < 0) {
return -1;
}
/* need to turn it off, since we're messing with the settings */
timer_on_off(timer_id, OFF);
uint32_t config;
if(interrupts)
config = 0x7000; /* set bits INT0_EN, INT1_EN, OVF_EN */
else
config = 0;
/* these two options are only supported on timers 4, 5, 6 */
if(timer_id >= TIMER_Separator) {
config |= (option24 ? (1 << 24) : 0) | (option28 ? 1 << 28: 0);
}
/* set the rest of the options */
config |= (Timers[timer_id].divider << 8)
| (z << 3)
| (option5 ? (1 << 5) : 0)
| (Timers[timer_id].option6 ? (1 << 6) : 0)
| (option11 ? (1 << 11) : 0);
SET_REG(HWTimers[timer_id].config, config);
SET_REG(HWTimers[timer_id].count_buffer, interval);
SET_REG(HWTimers[timer_id].count_buffer2, interval2);
SET_REG(HWTimers[timer_id].prescaler, prescaler);
// apply the settings
SET_REG(HWTimers[timer_id].state, TIMER_STATE_MANUALUPDATE);
return 0;
}
int timer_setup_clk(int timer_id, int type, int divider, uint32_t unknown1) {
if(type == 2) {
Timers[timer_id].option6 = FALSE;
Timers[timer_id].divider = 6;
} else {
if(type == 1) {
Timers[timer_id].option6 = TRUE;
} else {
Timers[timer_id].option6 = FALSE;
}
/* translate divider into divider code */
switch(divider) {
case 1:
Timers[timer_id].divider = TIMER_DIVIDER1;
break;
case 2:
Timers[timer_id].divider = TIMER_DIVIDER2;
break;
case 4:
Timers[timer_id].divider = TIMER_DIVIDER4;
break;
case 16:
Timers[timer_id].divider = TIMER_DIVIDER16;
break;
case 64:
Timers[timer_id].divider = TIMER_DIVIDER64;
break;
default:
/* invalid divider */
return -1;
}
}
Timers[timer_id].unknown1 = unknown1;
return 0;
}
int timer_stop_all() {
int i;
for(i = 0; i < NUM_TIMERS; i++) {
timer_on_off(i, OFF);
}
timer_on_off(NUM_TIMERS, OFF);
return 0;
}
int timer_on_off(int timer_id, OnOff on_off) {
if(timer_id < NUM_TIMERS) {
if(on_off == ON) {
SET_REG(HWTimers[timer_id].state, TIMER_STATE_START);
} else {
SET_REG(HWTimers[timer_id].state, TIMER_STATE_STOP);
}
return 0;
} else if(timer_id == NUM_TIMERS) {
if(on_off == ON) {
// clear bits 0, 1, 2, 3
SET_REG(TIMER + TIMER_UNKREG0, GET_REG(TIMER + TIMER_UNKREG0) & ~(0xF));
} else {
// set bits 1, 3
SET_REG(TIMER + TIMER_UNKREG0, GET_REG(TIMER + TIMER_UNKREG0) | 0xA);
}
return 0;
} else {
/* invalid timer id */
return -1;
}
}
/*static void timerIRQHandler(uint32_t token) {
//dump_memory(0x3e200000, 0x100);
// this function does not implement incrementing a counter at dword_18022B28 like Apple's
uint32_t stat = GET_REG(TIMER + TIMER_IRQSTAT);
// signal timer is being handled
volatile register uint32_t discard = GET_REG(TIMER + TIMER_IRQLATCH); discard --;
if(stat & TIMER_SPECIALTIMER_BIT0) {
SET_REG(TIMER + TIMER_UNKREG0, GET_REG(TIMER + TIMER_UNKREG0) | TIMER_SPECIALTIMER_BIT0);
}
if(stat & TIMER_SPECIALTIMER_BIT1) {
SET_REG(TIMER + TIMER_UNKREG0, GET_REG(TIMER + TIMER_UNKREG0) | TIMER_SPECIALTIMER_BIT1);
}
int i;
for(i = TIMER_Separator; i < NUM_TIMERS; i++) {
callTimerHandler(i, stat >> (8 * (NUM_TIMERS - i - 1)));
}
// signal timer has been handled
SET_REG(TIMER + TIMER_IRQLATCH, stat);
}*/
/*static void callTimerHandler(int timer_id, uint32_t flags) {
if((flags & (1 << 2)) != 0) {
if(Timers[timer_id].handler1)
Timers[timer_id].handler1();
}
if((flags & (1 << 1)) != 0) {
if(Timers[timer_id].handler3)
Timers[timer_id].handler3();
}
if((flags & (1 << 0)) != 0) {
if(Timers[timer_id].handler2)
Timers[timer_id].handler2();
}
}*/
uint64_t timer_get_system_microtime() {
uint64_t ticks;
uint64_t sec_divisor;
timer_get_rtc_ticks(&ticks, &sec_divisor);
return (ticks * uSecPerSec)/sec_divisor;
}
void timer_get_rtc_ticks(uint64_t* ticks, uint64_t* sec_divisor) {
register uint32_t ticksHigh;
register uint32_t ticksLow;
register uint32_t ticksHigh2;
/* try to get a good read where the lower bits remain the same after reading the higher bits */
do {
ticksHigh = GET_REG(TIMER + TIMER_TICKSHIGH);
ticksLow = GET_REG(TIMER + TIMER_TICKSLOW);
ticksHigh2 = GET_REG(TIMER + TIMER_TICKSHIGH);
} while(ticksHigh != ticksHigh2);
*ticks = (((uint64_t)ticksHigh) << 32) | ticksLow;
*sec_divisor = TicksPerSec;
}
void udelay(uint64_t delay) {
if(!RTCHasInit) {
return;
}
uint64_t startTime = timer_get_system_microtime();
// loop while elapsed time is less than requested delay
while((timer_get_system_microtime() - startTime) < delay);
}
int has_elapsed(uint64_t startTime, uint64_t elapsedTime) {
if((timer_get_system_microtime() - startTime) >= elapsedTime)
return TRUE;
else
return FALSE;
}