2 * Intel XScale PXA255/270 OS Timers.
4 * Copyright (c) 2006 Openedhand Ltd.
5 * Copyright (c) 2006 Thorsten Zitterell
7 * This code is licenced under the GPL.
11 #include "qemu-timer.h"
27 #define OSCR 0x10 /* OS Timer Count */
36 #define OSSR 0x14 /* Timer status register */
38 #define OIER 0x1c /* Interrupt enable register 3-0 to E3-E0 */
39 #define OMCR4 0xc0 /* OS Match Control registers */
49 #define PXA25X_FREQ 3686400 /* 3.6864 MHz */
50 #define PXA27X_FREQ 3250000 /* 3.25 MHz */
52 static int pxa2xx_timer4_freq
[8] = {
58 /* [5] is the "Externally supplied clock". Assign if necessary. */
62 struct pxa2xx_timer0_s
{
71 struct pxa2xx_timer4_s
{
72 struct pxa2xx_timer0_s tm
;
85 struct pxa2xx_timer0_s timer
[4];
86 struct pxa2xx_timer4_s
*tm4
;
93 static void pxa2xx_timer_update(void *opaque
, uint64_t now_qemu
)
95 pxa2xx_timer_info
*s
= (pxa2xx_timer_info
*) opaque
;
101 muldiv64(now_qemu
- s
->lastload
, s
->freq
, ticks_per_sec
);
103 for (i
= 0; i
< 4; i
++) {
104 new_qemu
= now_qemu
+ muldiv64((uint32_t) (s
->timer
[i
].value
- now_vm
),
105 ticks_per_sec
, s
->freq
);
106 qemu_mod_timer(s
->timer
[i
].qtimer
, new_qemu
);
110 static void pxa2xx_timer_update4(void *opaque
, uint64_t now_qemu
, int n
)
112 pxa2xx_timer_info
*s
= (pxa2xx_timer_info
*) opaque
;
115 static const int counters
[8] = { 0, 0, 0, 0, 4, 4, 6, 6 };
118 if (s
->tm4
[n
].control
& (1 << 7))
121 counter
= counters
[n
];
123 if (!s
->tm4
[counter
].freq
) {
124 qemu_del_timer(s
->tm4
[n
].tm
.qtimer
);
128 now_vm
= s
->tm4
[counter
].clock
+ muldiv64(now_qemu
-
129 s
->tm4
[counter
].lastload
,
130 s
->tm4
[counter
].freq
, ticks_per_sec
);
132 new_qemu
= now_qemu
+ muldiv64((uint32_t) (s
->tm4
[n
].tm
.value
- now_vm
),
133 ticks_per_sec
, s
->tm4
[counter
].freq
);
134 qemu_mod_timer(s
->tm4
[n
].tm
.qtimer
, new_qemu
);
137 static uint32_t pxa2xx_timer_read(void *opaque
, target_phys_addr_t offset
)
139 pxa2xx_timer_info
*s
= (pxa2xx_timer_info
*) opaque
;
147 return s
->timer
[tm
].value
;
158 return s
->tm4
[tm
].tm
.value
;
160 return s
->clock
+ muldiv64(qemu_get_clock(vm_clock
) -
161 s
->lastload
, s
->freq
, ticks_per_sec
);
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
);
180 s
->snapshot
= s
->tm4
[tm
- 1].clock
;
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
);
188 return s
->irq_enabled
;
189 case OSSR
: /* Status register */
203 return s
->tm4
[tm
].control
;
208 cpu_abort(cpu_single_env
, "pxa2xx_timer_read: Bad offset "
209 REG_FMT
"\n", offset
);
215 static void pxa2xx_timer_write(void *opaque
, target_phys_addr_t offset
,
219 pxa2xx_timer_info
*s
= (pxa2xx_timer_info
*) opaque
;
226 s
->timer
[tm
].value
= value
;
227 pxa2xx_timer_update(s
, qemu_get_clock(vm_clock
));
239 s
->tm4
[tm
].tm
.value
= value
;
240 pxa2xx_timer_update4(s
, qemu_get_clock(vm_clock
), tm
);
243 s
->oldclock
= s
->clock
;
244 s
->lastload
= qemu_get_clock(vm_clock
);
246 pxa2xx_timer_update(s
, s
->lastload
);
258 s
->tm4
[tm
].oldclock
= s
->tm4
[tm
].clock
;
259 s
->tm4
[tm
].lastload
= qemu_get_clock(vm_clock
);
260 s
->tm4
[tm
].clock
= value
;
261 pxa2xx_timer_update4(s
, s
->tm4
[tm
].lastload
, tm
);
264 s
->irq_enabled
= value
& 0xfff;
266 case OSSR
: /* Status register */
268 for (i
= 0; i
< 4; i
++, value
>>= 1) {
269 if (s
->timer
[i
].level
&& (value
& 1)) {
270 s
->timer
[i
].level
= 0;
271 qemu_irq_lower(s
->timer
[i
].irq
);
275 for (i
= 0; i
< 8; i
++, value
>>= 1)
276 if (s
->tm4
[i
].tm
.level
&& (value
& 1))
277 s
->tm4
[i
].tm
.level
= 0;
278 if (!(s
->events
& 0xff0))
279 qemu_irq_lower(s
->tm4
->tm
.irq
);
282 case OWER
: /* XXX: Reset on OSMR3 match? */
291 s
->tm4
[tm
].control
= value
& 0x0ff;
292 /* XXX Stop if running (shouldn't happen) */
293 if ((value
& (1 << 7)) || tm
== 0)
294 s
->tm4
[tm
].freq
= pxa2xx_timer4_freq
[value
& 7];
297 pxa2xx_timer_update4(s
, qemu_get_clock(vm_clock
), tm
);
306 s
->tm4
[tm
].control
= value
& 0x3ff;
307 /* XXX Stop if running (shouldn't happen) */
308 if ((value
& (1 << 7)) || !(tm
& 1))
310 pxa2xx_timer4_freq
[(value
& (1 << 8)) ? 0 : (value
& 7)];
313 pxa2xx_timer_update4(s
, qemu_get_clock(vm_clock
), tm
);
318 cpu_abort(cpu_single_env
, "pxa2xx_timer_write: Bad offset "
319 REG_FMT
"\n", offset
);
323 static CPUReadMemoryFunc
*pxa2xx_timer_readfn
[] = {
329 static CPUWriteMemoryFunc
*pxa2xx_timer_writefn
[] = {
335 static void pxa2xx_timer_tick(void *opaque
)
337 struct pxa2xx_timer0_s
*t
= (struct pxa2xx_timer0_s
*) opaque
;
338 pxa2xx_timer_info
*i
= (pxa2xx_timer_info
*) t
->info
;
340 if (i
->irq_enabled
& (1 << t
->num
)) {
342 i
->events
|= 1 << t
->num
;
343 qemu_irq_raise(t
->irq
);
349 qemu_system_reset_request();
353 static void pxa2xx_timer_tick4(void *opaque
)
355 struct pxa2xx_timer4_s
*t
= (struct pxa2xx_timer4_s
*) opaque
;
356 pxa2xx_timer_info
*i
= (pxa2xx_timer_info
*) t
->tm
.info
;
358 pxa2xx_timer_tick(&t
->tm
);
359 if (t
->control
& (1 << 3))
361 if (t
->control
& (1 << 6))
362 pxa2xx_timer_update4(i
, qemu_get_clock(vm_clock
), t
->tm
.num
- 4);
365 static void pxa2xx_timer_save(QEMUFile
*f
, void *opaque
)
367 pxa2xx_timer_info
*s
= (pxa2xx_timer_info
*) opaque
;
370 qemu_put_be32s(f
, (uint32_t *) &s
->clock
);
371 qemu_put_be32s(f
, (uint32_t *) &s
->oldclock
);
372 qemu_put_be64s(f
, &s
->lastload
);
374 for (i
= 0; i
< 4; i
++) {
375 qemu_put_be32s(f
, &s
->timer
[i
].value
);
376 qemu_put_be32(f
, s
->timer
[i
].level
);
379 for (i
= 0; i
< 8; i
++) {
380 qemu_put_be32s(f
, &s
->tm4
[i
].tm
.value
);
381 qemu_put_be32(f
, s
->tm4
[i
].tm
.level
);
382 qemu_put_sbe32s(f
, &s
->tm4
[i
].oldclock
);
383 qemu_put_sbe32s(f
, &s
->tm4
[i
].clock
);
384 qemu_put_be64s(f
, &s
->tm4
[i
].lastload
);
385 qemu_put_be32s(f
, &s
->tm4
[i
].freq
);
386 qemu_put_be32s(f
, &s
->tm4
[i
].control
);
389 qemu_put_be32s(f
, &s
->events
);
390 qemu_put_be32s(f
, &s
->irq_enabled
);
391 qemu_put_be32s(f
, &s
->reset3
);
392 qemu_put_be32s(f
, &s
->snapshot
);
395 static int pxa2xx_timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
397 pxa2xx_timer_info
*s
= (pxa2xx_timer_info
*) opaque
;
401 qemu_get_be32s(f
, (uint32_t *) &s
->clock
);
402 qemu_get_be32s(f
, (uint32_t *) &s
->oldclock
);
403 qemu_get_be64s(f
, &s
->lastload
);
405 now
= qemu_get_clock(vm_clock
);
406 for (i
= 0; i
< 4; i
++) {
407 qemu_get_be32s(f
, &s
->timer
[i
].value
);
408 s
->timer
[i
].level
= qemu_get_be32(f
);
410 pxa2xx_timer_update(s
, now
);
413 for (i
= 0; i
< 8; i
++) {
414 qemu_get_be32s(f
, &s
->tm4
[i
].tm
.value
);
415 s
->tm4
[i
].tm
.level
= qemu_get_be32(f
);
416 qemu_get_sbe32s(f
, &s
->tm4
[i
].oldclock
);
417 qemu_get_sbe32s(f
, &s
->tm4
[i
].clock
);
418 qemu_get_be64s(f
, &s
->tm4
[i
].lastload
);
419 qemu_get_be32s(f
, &s
->tm4
[i
].freq
);
420 qemu_get_be32s(f
, &s
->tm4
[i
].control
);
421 pxa2xx_timer_update4(s
, now
, i
);
424 qemu_get_be32s(f
, &s
->events
);
425 qemu_get_be32s(f
, &s
->irq_enabled
);
426 qemu_get_be32s(f
, &s
->reset3
);
427 qemu_get_be32s(f
, &s
->snapshot
);
432 static pxa2xx_timer_info
*pxa2xx_timer_init(target_phys_addr_t base
,
437 pxa2xx_timer_info
*s
;
439 s
= (pxa2xx_timer_info
*) qemu_mallocz(sizeof(pxa2xx_timer_info
));
443 s
->lastload
= qemu_get_clock(vm_clock
);
446 for (i
= 0; i
< 4; i
++) {
447 s
->timer
[i
].value
= 0;
448 s
->timer
[i
].irq
= irqs
[i
];
449 s
->timer
[i
].info
= s
;
451 s
->timer
[i
].level
= 0;
452 s
->timer
[i
].qtimer
= qemu_new_timer(vm_clock
,
453 pxa2xx_timer_tick
, &s
->timer
[i
]);
456 iomemtype
= cpu_register_io_memory(0, pxa2xx_timer_readfn
,
457 pxa2xx_timer_writefn
, s
);
458 cpu_register_physical_memory(base
, 0x00001000, iomemtype
);
460 register_savevm("pxa2xx_timer", 0, 0,
461 pxa2xx_timer_save
, pxa2xx_timer_load
, s
);
466 void pxa25x_timer_init(target_phys_addr_t base
, qemu_irq
*irqs
)
468 pxa2xx_timer_info
*s
= pxa2xx_timer_init(base
, irqs
);
469 s
->freq
= PXA25X_FREQ
;
473 void pxa27x_timer_init(target_phys_addr_t base
,
474 qemu_irq
*irqs
, qemu_irq irq4
)
476 pxa2xx_timer_info
*s
= pxa2xx_timer_init(base
, irqs
);
478 s
->freq
= PXA27X_FREQ
;
479 s
->tm4
= (struct pxa2xx_timer4_s
*) qemu_mallocz(8 *
480 sizeof(struct pxa2xx_timer4_s
));
481 for (i
= 0; i
< 8; i
++) {
482 s
->tm4
[i
].tm
.value
= 0;
483 s
->tm4
[i
].tm
.irq
= irq4
;
484 s
->tm4
[i
].tm
.info
= s
;
485 s
->tm4
[i
].tm
.num
= i
+ 4;
486 s
->tm4
[i
].tm
.level
= 0;
488 s
->tm4
[i
].control
= 0x0;
489 s
->tm4
[i
].tm
.qtimer
= qemu_new_timer(vm_clock
,
490 pxa2xx_timer_tick4
, &s
->tm4
[i
]);