]>
Commit | Line | Data |
---|---|---|
a171fe39 AZ |
1 | /* |
2 | * Intel XScale PXA255/270 OS Timers. | |
3 | * | |
4 | * Copyright (c) 2006 Openedhand Ltd. | |
5 | * Copyright (c) 2006 Thorsten Zitterell | |
6 | * | |
7 | * This code is licenced under the GPL. | |
8 | */ | |
9 | ||
10 | #include "vl.h" | |
11 | ||
12 | #define OSMR0 0x00 | |
13 | #define OSMR1 0x04 | |
14 | #define OSMR2 0x08 | |
15 | #define OSMR3 0x0c | |
16 | #define OSMR4 0x80 | |
17 | #define OSMR5 0x84 | |
18 | #define OSMR6 0x88 | |
19 | #define OSMR7 0x8c | |
20 | #define OSMR8 0x90 | |
21 | #define OSMR9 0x94 | |
22 | #define OSMR10 0x98 | |
23 | #define OSMR11 0x9c | |
24 | #define OSCR 0x10 /* OS Timer Count */ | |
25 | #define OSCR4 0x40 | |
26 | #define OSCR5 0x44 | |
27 | #define OSCR6 0x48 | |
28 | #define OSCR7 0x4c | |
29 | #define OSCR8 0x50 | |
30 | #define OSCR9 0x54 | |
31 | #define OSCR10 0x58 | |
32 | #define OSCR11 0x5c | |
33 | #define OSSR 0x14 /* Timer status register */ | |
34 | #define OWER 0x18 | |
35 | #define OIER 0x1c /* Interrupt enable register 3-0 to E3-E0 */ | |
36 | #define OMCR4 0xc0 /* OS Match Control registers */ | |
37 | #define OMCR5 0xc4 | |
38 | #define OMCR6 0xc8 | |
39 | #define OMCR7 0xcc | |
40 | #define OMCR8 0xd0 | |
41 | #define OMCR9 0xd4 | |
42 | #define OMCR10 0xd8 | |
43 | #define OMCR11 0xdc | |
44 | #define OSNR 0x20 | |
45 | ||
46 | #define PXA25X_FREQ 3686400 /* 3.6864 MHz */ | |
47 | #define PXA27X_FREQ 3250000 /* 3.25 MHz */ | |
48 | ||
49 | static int pxa2xx_timer4_freq[8] = { | |
50 | [0] = 0, | |
51 | [1] = 32768, | |
52 | [2] = 1000, | |
53 | [3] = 1, | |
54 | [4] = 1000000, | |
55 | /* [5] is the "Externally supplied clock". Assign if necessary. */ | |
56 | [5 ... 7] = 0, | |
57 | }; | |
58 | ||
59 | struct pxa2xx_timer0_s { | |
60 | uint32_t value; | |
61 | int level; | |
62 | qemu_irq irq; | |
63 | QEMUTimer *qtimer; | |
64 | int num; | |
65 | void *info; | |
66 | }; | |
67 | ||
68 | struct pxa2xx_timer4_s { | |
3bdd58a4 | 69 | struct pxa2xx_timer0_s tm; |
a171fe39 AZ |
70 | int32_t oldclock; |
71 | int32_t clock; | |
72 | uint64_t lastload; | |
73 | uint32_t freq; | |
74 | uint32_t control; | |
75 | }; | |
76 | ||
77 | typedef struct { | |
3f582262 | 78 | target_phys_addr_t base; |
a171fe39 AZ |
79 | int32_t clock; |
80 | int32_t oldclock; | |
81 | uint64_t lastload; | |
82 | uint32_t freq; | |
83 | struct pxa2xx_timer0_s timer[4]; | |
84 | struct pxa2xx_timer4_s *tm4; | |
85 | uint32_t events; | |
86 | uint32_t irq_enabled; | |
87 | uint32_t reset3; | |
a171fe39 AZ |
88 | uint32_t snapshot; |
89 | } pxa2xx_timer_info; | |
90 | ||
91 | static void pxa2xx_timer_update(void *opaque, uint64_t now_qemu) | |
92 | { | |
93 | pxa2xx_timer_info *s = (pxa2xx_timer_info *) opaque; | |
94 | int i; | |
95 | uint32_t now_vm; | |
96 | uint64_t new_qemu; | |
97 | ||
98 | now_vm = s->clock + | |
99 | muldiv64(now_qemu - s->lastload, s->freq, ticks_per_sec); | |
100 | ||
101 | for (i = 0; i < 4; i ++) { | |
102 | new_qemu = now_qemu + muldiv64((uint32_t) (s->timer[i].value - now_vm), | |
103 | ticks_per_sec, s->freq); | |
104 | qemu_mod_timer(s->timer[i].qtimer, new_qemu); | |
105 | } | |
106 | } | |
107 | ||
108 | static void pxa2xx_timer_update4(void *opaque, uint64_t now_qemu, int n) | |
109 | { | |
110 | pxa2xx_timer_info *s = (pxa2xx_timer_info *) opaque; | |
111 | uint32_t now_vm; | |
112 | uint64_t new_qemu; | |
113 | static const int counters[8] = { 0, 0, 0, 0, 4, 4, 6, 6 }; | |
114 | int counter; | |
115 | ||
116 | if (s->tm4[n].control & (1 << 7)) | |
117 | counter = n; | |
118 | else | |
119 | counter = counters[n]; | |
120 | ||
121 | if (!s->tm4[counter].freq) { | |
3f582262 | 122 | qemu_del_timer(s->tm4[n].tm.qtimer); |
a171fe39 AZ |
123 | return; |
124 | } | |
125 | ||
126 | now_vm = s->tm4[counter].clock + muldiv64(now_qemu - | |
127 | s->tm4[counter].lastload, | |
128 | s->tm4[counter].freq, ticks_per_sec); | |
129 | ||
3bdd58a4 | 130 | new_qemu = now_qemu + muldiv64((uint32_t) (s->tm4[n].tm.value - now_vm), |
a171fe39 | 131 | ticks_per_sec, s->tm4[counter].freq); |
3f582262 | 132 | qemu_mod_timer(s->tm4[n].tm.qtimer, new_qemu); |
a171fe39 AZ |
133 | } |
134 | ||
135 | static uint32_t pxa2xx_timer_read(void *opaque, target_phys_addr_t offset) | |
136 | { | |
137 | pxa2xx_timer_info *s = (pxa2xx_timer_info *) opaque; | |
138 | int tm = 0; | |
139 | ||
140 | offset -= s->base; | |
141 | ||
142 | switch (offset) { | |
143 | case OSMR3: tm ++; | |
144 | case OSMR2: tm ++; | |
145 | case OSMR1: tm ++; | |
146 | case OSMR0: | |
147 | return s->timer[tm].value; | |
148 | case OSMR11: tm ++; | |
149 | case OSMR10: tm ++; | |
150 | case OSMR9: tm ++; | |
151 | case OSMR8: tm ++; | |
152 | case OSMR7: tm ++; | |
153 | case OSMR6: tm ++; | |
154 | case OSMR5: tm ++; | |
155 | case OSMR4: | |
156 | if (!s->tm4) | |
157 | goto badreg; | |
3bdd58a4 | 158 | return s->tm4[tm].tm.value; |
a171fe39 AZ |
159 | case OSCR: |
160 | return s->clock + muldiv64(qemu_get_clock(vm_clock) - | |
161 | s->lastload, s->freq, ticks_per_sec); | |
162 | case OSCR11: tm ++; | |
163 | case OSCR10: tm ++; | |
164 | case OSCR9: tm ++; | |
165 | case OSCR8: tm ++; | |
166 | case OSCR7: tm ++; | |
167 | case OSCR6: tm ++; | |
168 | case OSCR5: tm ++; | |
169 | case OSCR4: | |
170 | if (!s->tm4) | |
171 | goto badreg; | |
172 | ||
173 | if ((tm == 9 - 4 || tm == 11 - 4) && (s->tm4[tm].control & (1 << 9))) { | |
174 | if (s->tm4[tm - 1].freq) | |
175 | s->snapshot = s->tm4[tm - 1].clock + muldiv64( | |
176 | qemu_get_clock(vm_clock) - | |
177 | s->tm4[tm - 1].lastload, | |
178 | s->tm4[tm - 1].freq, ticks_per_sec); | |
179 | else | |
180 | s->snapshot = s->tm4[tm - 1].clock; | |
181 | } | |
182 | ||
183 | if (!s->tm4[tm].freq) | |
184 | return s->tm4[tm].clock; | |
185 | return s->tm4[tm].clock + muldiv64(qemu_get_clock(vm_clock) - | |
186 | s->tm4[tm].lastload, s->tm4[tm].freq, ticks_per_sec); | |
187 | case OIER: | |
188 | return s->irq_enabled; | |
189 | case OSSR: /* Status register */ | |
190 | return s->events; | |
191 | case OWER: | |
192 | return s->reset3; | |
193 | case OMCR11: tm ++; | |
194 | case OMCR10: tm ++; | |
195 | case OMCR9: tm ++; | |
196 | case OMCR8: tm ++; | |
197 | case OMCR7: tm ++; | |
198 | case OMCR6: tm ++; | |
199 | case OMCR5: tm ++; | |
200 | case OMCR4: | |
201 | if (!s->tm4) | |
202 | goto badreg; | |
203 | return s->tm4[tm].control; | |
204 | case OSNR: | |
205 | return s->snapshot; | |
206 | default: | |
207 | badreg: | |
208 | cpu_abort(cpu_single_env, "pxa2xx_timer_read: Bad offset " | |
209 | REG_FMT "\n", offset); | |
210 | } | |
211 | ||
212 | return 0; | |
213 | } | |
214 | ||
215 | static void pxa2xx_timer_write(void *opaque, target_phys_addr_t offset, | |
216 | uint32_t value) | |
217 | { | |
218 | int i, tm = 0; | |
219 | pxa2xx_timer_info *s = (pxa2xx_timer_info *) opaque; | |
220 | ||
221 | offset -= s->base; | |
222 | ||
223 | switch (offset) { | |
224 | case OSMR3: tm ++; | |
225 | case OSMR2: tm ++; | |
226 | case OSMR1: tm ++; | |
227 | case OSMR0: | |
228 | s->timer[tm].value = value; | |
229 | pxa2xx_timer_update(s, qemu_get_clock(vm_clock)); | |
230 | break; | |
231 | case OSMR11: tm ++; | |
232 | case OSMR10: tm ++; | |
233 | case OSMR9: tm ++; | |
234 | case OSMR8: tm ++; | |
235 | case OSMR7: tm ++; | |
236 | case OSMR6: tm ++; | |
237 | case OSMR5: tm ++; | |
238 | case OSMR4: | |
239 | if (!s->tm4) | |
240 | goto badreg; | |
3bdd58a4 | 241 | s->tm4[tm].tm.value = value; |
a171fe39 AZ |
242 | pxa2xx_timer_update4(s, qemu_get_clock(vm_clock), tm); |
243 | break; | |
244 | case OSCR: | |
245 | s->oldclock = s->clock; | |
246 | s->lastload = qemu_get_clock(vm_clock); | |
247 | s->clock = value; | |
248 | pxa2xx_timer_update(s, s->lastload); | |
249 | break; | |
250 | case OSCR11: tm ++; | |
251 | case OSCR10: tm ++; | |
252 | case OSCR9: tm ++; | |
253 | case OSCR8: tm ++; | |
254 | case OSCR7: tm ++; | |
255 | case OSCR6: tm ++; | |
256 | case OSCR5: tm ++; | |
257 | case OSCR4: | |
258 | if (!s->tm4) | |
259 | goto badreg; | |
260 | s->tm4[tm].oldclock = s->tm4[tm].clock; | |
261 | s->tm4[tm].lastload = qemu_get_clock(vm_clock); | |
262 | s->tm4[tm].clock = value; | |
263 | pxa2xx_timer_update4(s, s->tm4[tm].lastload, tm); | |
264 | break; | |
265 | case OIER: | |
266 | s->irq_enabled = value & 0xfff; | |
267 | break; | |
268 | case OSSR: /* Status register */ | |
269 | s->events &= ~value; | |
270 | for (i = 0; i < 4; i ++, value >>= 1) { | |
271 | if (s->timer[i].level && (value & 1)) { | |
272 | s->timer[i].level = 0; | |
273 | qemu_irq_lower(s->timer[i].irq); | |
274 | } | |
275 | } | |
276 | if (s->tm4) { | |
277 | for (i = 0; i < 8; i ++, value >>= 1) | |
3bdd58a4 AZ |
278 | if (s->tm4[i].tm.level && (value & 1)) |
279 | s->tm4[i].tm.level = 0; | |
a171fe39 | 280 | if (!(s->events & 0xff0)) |
3bdd58a4 | 281 | qemu_irq_lower(s->tm4->tm.irq); |
a171fe39 AZ |
282 | } |
283 | break; | |
284 | case OWER: /* XXX: Reset on OSMR3 match? */ | |
285 | s->reset3 = value; | |
286 | break; | |
287 | case OMCR7: tm ++; | |
288 | case OMCR6: tm ++; | |
289 | case OMCR5: tm ++; | |
290 | case OMCR4: | |
291 | if (!s->tm4) | |
292 | goto badreg; | |
293 | s->tm4[tm].control = value & 0x0ff; | |
294 | /* XXX Stop if running (shouldn't happen) */ | |
295 | if ((value & (1 << 7)) || tm == 0) | |
296 | s->tm4[tm].freq = pxa2xx_timer4_freq[value & 7]; | |
297 | else { | |
298 | s->tm4[tm].freq = 0; | |
299 | pxa2xx_timer_update4(s, qemu_get_clock(vm_clock), tm); | |
300 | } | |
301 | break; | |
302 | case OMCR11: tm ++; | |
303 | case OMCR10: tm ++; | |
304 | case OMCR9: tm ++; | |
305 | case OMCR8: tm += 4; | |
306 | if (!s->tm4) | |
307 | goto badreg; | |
308 | s->tm4[tm].control = value & 0x3ff; | |
309 | /* XXX Stop if running (shouldn't happen) */ | |
310 | if ((value & (1 << 7)) || !(tm & 1)) | |
311 | s->tm4[tm].freq = | |
312 | pxa2xx_timer4_freq[(value & (1 << 8)) ? 0 : (value & 7)]; | |
313 | else { | |
314 | s->tm4[tm].freq = 0; | |
315 | pxa2xx_timer_update4(s, qemu_get_clock(vm_clock), tm); | |
316 | } | |
317 | break; | |
318 | default: | |
319 | badreg: | |
320 | cpu_abort(cpu_single_env, "pxa2xx_timer_write: Bad offset " | |
321 | REG_FMT "\n", offset); | |
322 | } | |
323 | } | |
324 | ||
325 | static CPUReadMemoryFunc *pxa2xx_timer_readfn[] = { | |
326 | pxa2xx_timer_read, | |
327 | pxa2xx_timer_read, | |
328 | pxa2xx_timer_read, | |
329 | }; | |
330 | ||
331 | static CPUWriteMemoryFunc *pxa2xx_timer_writefn[] = { | |
332 | pxa2xx_timer_write, | |
333 | pxa2xx_timer_write, | |
334 | pxa2xx_timer_write, | |
335 | }; | |
336 | ||
337 | static void pxa2xx_timer_tick(void *opaque) | |
338 | { | |
339 | struct pxa2xx_timer0_s *t = (struct pxa2xx_timer0_s *) opaque; | |
340 | pxa2xx_timer_info *i = (pxa2xx_timer_info *) t->info; | |
341 | ||
342 | if (i->irq_enabled & (1 << t->num)) { | |
343 | t->level = 1; | |
344 | i->events |= 1 << t->num; | |
345 | qemu_irq_raise(t->irq); | |
346 | } | |
347 | ||
348 | if (t->num == 3) | |
349 | if (i->reset3 & 1) { | |
350 | i->reset3 = 0; | |
3f582262 | 351 | qemu_system_reset_request(); |
a171fe39 AZ |
352 | } |
353 | } | |
354 | ||
355 | static void pxa2xx_timer_tick4(void *opaque) | |
356 | { | |
357 | struct pxa2xx_timer4_s *t = (struct pxa2xx_timer4_s *) opaque; | |
3bdd58a4 | 358 | pxa2xx_timer_info *i = (pxa2xx_timer_info *) t->tm.info; |
a171fe39 | 359 | |
3bdd58a4 | 360 | pxa2xx_timer_tick(&t->tm); |
a171fe39 AZ |
361 | if (t->control & (1 << 3)) |
362 | t->clock = 0; | |
363 | if (t->control & (1 << 6)) | |
3bdd58a4 | 364 | pxa2xx_timer_update4(i, qemu_get_clock(vm_clock), t->tm.num - 4); |
a171fe39 AZ |
365 | } |
366 | ||
367 | static pxa2xx_timer_info *pxa2xx_timer_init(target_phys_addr_t base, | |
3f582262 | 368 | qemu_irq *irqs) |
a171fe39 AZ |
369 | { |
370 | int i; | |
371 | int iomemtype; | |
372 | pxa2xx_timer_info *s; | |
373 | ||
374 | s = (pxa2xx_timer_info *) qemu_mallocz(sizeof(pxa2xx_timer_info)); | |
375 | s->base = base; | |
376 | s->irq_enabled = 0; | |
377 | s->oldclock = 0; | |
378 | s->clock = 0; | |
379 | s->lastload = qemu_get_clock(vm_clock); | |
380 | s->reset3 = 0; | |
a171fe39 AZ |
381 | |
382 | for (i = 0; i < 4; i ++) { | |
383 | s->timer[i].value = 0; | |
384 | s->timer[i].irq = irqs[i]; | |
385 | s->timer[i].info = s; | |
386 | s->timer[i].num = i; | |
387 | s->timer[i].level = 0; | |
388 | s->timer[i].qtimer = qemu_new_timer(vm_clock, | |
389 | pxa2xx_timer_tick, &s->timer[i]); | |
390 | } | |
391 | ||
392 | iomemtype = cpu_register_io_memory(0, pxa2xx_timer_readfn, | |
393 | pxa2xx_timer_writefn, s); | |
394 | cpu_register_physical_memory(base, 0x00000fff, iomemtype); | |
395 | return s; | |
396 | } | |
397 | ||
3f582262 | 398 | void pxa25x_timer_init(target_phys_addr_t base, qemu_irq *irqs) |
a171fe39 | 399 | { |
3f582262 | 400 | pxa2xx_timer_info *s = pxa2xx_timer_init(base, irqs); |
a171fe39 AZ |
401 | s->freq = PXA25X_FREQ; |
402 | s->tm4 = 0; | |
403 | } | |
404 | ||
405 | void pxa27x_timer_init(target_phys_addr_t base, | |
3f582262 | 406 | qemu_irq *irqs, qemu_irq irq4) |
a171fe39 | 407 | { |
3f582262 | 408 | pxa2xx_timer_info *s = pxa2xx_timer_init(base, irqs); |
a171fe39 AZ |
409 | int i; |
410 | s->freq = PXA27X_FREQ; | |
411 | s->tm4 = (struct pxa2xx_timer4_s *) qemu_mallocz(8 * | |
412 | sizeof(struct pxa2xx_timer4_s)); | |
413 | for (i = 0; i < 8; i ++) { | |
3bdd58a4 AZ |
414 | s->tm4[i].tm.value = 0; |
415 | s->tm4[i].tm.irq = irq4; | |
416 | s->tm4[i].tm.info = s; | |
417 | s->tm4[i].tm.num = i + 4; | |
418 | s->tm4[i].tm.level = 0; | |
a171fe39 AZ |
419 | s->tm4[i].freq = 0; |
420 | s->tm4[i].control = 0x0; | |
3bdd58a4 | 421 | s->tm4[i].tm.qtimer = qemu_new_timer(vm_clock, |
a171fe39 AZ |
422 | pxa2xx_timer_tick4, &s->tm4[i]); |
423 | } | |
424 | } |