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
;
81 target_phys_addr_t base
;
86 struct pxa2xx_timer0_s timer
[4];
87 struct pxa2xx_timer4_s
*tm4
;
94 static void pxa2xx_timer_update(void *opaque
, uint64_t now_qemu
)
96 pxa2xx_timer_info
*s
= (pxa2xx_timer_info
*) opaque
;
102 muldiv64(now_qemu
- s
->lastload
, s
->freq
, ticks_per_sec
);
104 for (i
= 0; i
< 4; i
++) {
105 new_qemu
= now_qemu
+ muldiv64((uint32_t) (s
->timer
[i
].value
- now_vm
),
106 ticks_per_sec
, s
->freq
);
107 qemu_mod_timer(s
->timer
[i
].qtimer
, new_qemu
);
111 static void pxa2xx_timer_update4(void *opaque
, uint64_t now_qemu
, int n
)
113 pxa2xx_timer_info
*s
= (pxa2xx_timer_info
*) opaque
;
116 static const int counters
[8] = { 0, 0, 0, 0, 4, 4, 6, 6 };
119 if (s
->tm4
[n
].control
& (1 << 7))
122 counter
= counters
[n
];
124 if (!s
->tm4
[counter
].freq
) {
125 qemu_del_timer(s
->tm4
[n
].tm
.qtimer
);
129 now_vm
= s
->tm4
[counter
].clock
+ muldiv64(now_qemu
-
130 s
->tm4
[counter
].lastload
,
131 s
->tm4
[counter
].freq
, ticks_per_sec
);
133 new_qemu
= now_qemu
+ muldiv64((uint32_t) (s
->tm4
[n
].tm
.value
- now_vm
),
134 ticks_per_sec
, s
->tm4
[counter
].freq
);
135 qemu_mod_timer(s
->tm4
[n
].tm
.qtimer
, new_qemu
);
138 static uint32_t pxa2xx_timer_read(void *opaque
, target_phys_addr_t offset
)
140 pxa2xx_timer_info
*s
= (pxa2xx_timer_info
*) opaque
;
150 return s
->timer
[tm
].value
;
161 return s
->tm4
[tm
].tm
.value
;
163 return s
->clock
+ muldiv64(qemu_get_clock(vm_clock
) -
164 s
->lastload
, s
->freq
, ticks_per_sec
);
176 if ((tm
== 9 - 4 || tm
== 11 - 4) && (s
->tm4
[tm
].control
& (1 << 9))) {
177 if (s
->tm4
[tm
- 1].freq
)
178 s
->snapshot
= s
->tm4
[tm
- 1].clock
+ muldiv64(
179 qemu_get_clock(vm_clock
) -
180 s
->tm4
[tm
- 1].lastload
,
181 s
->tm4
[tm
- 1].freq
, ticks_per_sec
);
183 s
->snapshot
= s
->tm4
[tm
- 1].clock
;
186 if (!s
->tm4
[tm
].freq
)
187 return s
->tm4
[tm
].clock
;
188 return s
->tm4
[tm
].clock
+ muldiv64(qemu_get_clock(vm_clock
) -
189 s
->tm4
[tm
].lastload
, s
->tm4
[tm
].freq
, ticks_per_sec
);
191 return s
->irq_enabled
;
192 case OSSR
: /* Status register */
206 return s
->tm4
[tm
].control
;
211 cpu_abort(cpu_single_env
, "pxa2xx_timer_read: Bad offset "
212 REG_FMT
"\n", offset
);
218 static void pxa2xx_timer_write(void *opaque
, target_phys_addr_t offset
,
222 pxa2xx_timer_info
*s
= (pxa2xx_timer_info
*) opaque
;
231 s
->timer
[tm
].value
= value
;
232 pxa2xx_timer_update(s
, qemu_get_clock(vm_clock
));
244 s
->tm4
[tm
].tm
.value
= value
;
245 pxa2xx_timer_update4(s
, qemu_get_clock(vm_clock
), tm
);
248 s
->oldclock
= s
->clock
;
249 s
->lastload
= qemu_get_clock(vm_clock
);
251 pxa2xx_timer_update(s
, s
->lastload
);
263 s
->tm4
[tm
].oldclock
= s
->tm4
[tm
].clock
;
264 s
->tm4
[tm
].lastload
= qemu_get_clock(vm_clock
);
265 s
->tm4
[tm
].clock
= value
;
266 pxa2xx_timer_update4(s
, s
->tm4
[tm
].lastload
, tm
);
269 s
->irq_enabled
= value
& 0xfff;
271 case OSSR
: /* Status register */
273 for (i
= 0; i
< 4; i
++, value
>>= 1) {
274 if (s
->timer
[i
].level
&& (value
& 1)) {
275 s
->timer
[i
].level
= 0;
276 qemu_irq_lower(s
->timer
[i
].irq
);
280 for (i
= 0; i
< 8; i
++, value
>>= 1)
281 if (s
->tm4
[i
].tm
.level
&& (value
& 1))
282 s
->tm4
[i
].tm
.level
= 0;
283 if (!(s
->events
& 0xff0))
284 qemu_irq_lower(s
->tm4
->tm
.irq
);
287 case OWER
: /* XXX: Reset on OSMR3 match? */
296 s
->tm4
[tm
].control
= value
& 0x0ff;
297 /* XXX Stop if running (shouldn't happen) */
298 if ((value
& (1 << 7)) || tm
== 0)
299 s
->tm4
[tm
].freq
= pxa2xx_timer4_freq
[value
& 7];
302 pxa2xx_timer_update4(s
, qemu_get_clock(vm_clock
), tm
);
311 s
->tm4
[tm
].control
= value
& 0x3ff;
312 /* XXX Stop if running (shouldn't happen) */
313 if ((value
& (1 << 7)) || !(tm
& 1))
315 pxa2xx_timer4_freq
[(value
& (1 << 8)) ? 0 : (value
& 7)];
318 pxa2xx_timer_update4(s
, qemu_get_clock(vm_clock
), tm
);
323 cpu_abort(cpu_single_env
, "pxa2xx_timer_write: Bad offset "
324 REG_FMT
"\n", offset
);
328 static CPUReadMemoryFunc
*pxa2xx_timer_readfn
[] = {
334 static CPUWriteMemoryFunc
*pxa2xx_timer_writefn
[] = {
340 static void pxa2xx_timer_tick(void *opaque
)
342 struct pxa2xx_timer0_s
*t
= (struct pxa2xx_timer0_s
*) opaque
;
343 pxa2xx_timer_info
*i
= (pxa2xx_timer_info
*) t
->info
;
345 if (i
->irq_enabled
& (1 << t
->num
)) {
347 i
->events
|= 1 << t
->num
;
348 qemu_irq_raise(t
->irq
);
354 qemu_system_reset_request();
358 static void pxa2xx_timer_tick4(void *opaque
)
360 struct pxa2xx_timer4_s
*t
= (struct pxa2xx_timer4_s
*) opaque
;
361 pxa2xx_timer_info
*i
= (pxa2xx_timer_info
*) t
->tm
.info
;
363 pxa2xx_timer_tick(&t
->tm
);
364 if (t
->control
& (1 << 3))
366 if (t
->control
& (1 << 6))
367 pxa2xx_timer_update4(i
, qemu_get_clock(vm_clock
), t
->tm
.num
- 4);
370 static void pxa2xx_timer_save(QEMUFile
*f
, void *opaque
)
372 pxa2xx_timer_info
*s
= (pxa2xx_timer_info
*) opaque
;
375 qemu_put_be32s(f
, &s
->clock
);
376 qemu_put_be32s(f
, &s
->oldclock
);
377 qemu_put_be64s(f
, &s
->lastload
);
379 for (i
= 0; i
< 4; i
++) {
380 qemu_put_be32s(f
, &s
->timer
[i
].value
);
381 qemu_put_be32(f
, s
->timer
[i
].level
);
384 for (i
= 0; i
< 8; i
++) {
385 qemu_put_be32s(f
, &s
->tm4
[i
].tm
.value
);
386 qemu_put_be32(f
, s
->tm4
[i
].tm
.level
);
387 qemu_put_be32s(f
, &s
->tm4
[i
].oldclock
);
388 qemu_put_be32s(f
, &s
->tm4
[i
].clock
);
389 qemu_put_be64s(f
, &s
->tm4
[i
].lastload
);
390 qemu_put_be32s(f
, &s
->tm4
[i
].freq
);
391 qemu_put_be32s(f
, &s
->tm4
[i
].control
);
394 qemu_put_be32s(f
, &s
->events
);
395 qemu_put_be32s(f
, &s
->irq_enabled
);
396 qemu_put_be32s(f
, &s
->reset3
);
397 qemu_put_be32s(f
, &s
->snapshot
);
400 static int pxa2xx_timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
402 pxa2xx_timer_info
*s
= (pxa2xx_timer_info
*) opaque
;
406 qemu_get_be32s(f
, &s
->clock
);
407 qemu_get_be32s(f
, &s
->oldclock
);
408 qemu_get_be64s(f
, &s
->lastload
);
410 now
= qemu_get_clock(vm_clock
);
411 for (i
= 0; i
< 4; i
++) {
412 qemu_get_be32s(f
, &s
->timer
[i
].value
);
413 s
->timer
[i
].level
= qemu_get_be32(f
);
415 pxa2xx_timer_update(s
, now
);
418 for (i
= 0; i
< 8; i
++) {
419 qemu_get_be32s(f
, &s
->tm4
[i
].tm
.value
);
420 s
->tm4
[i
].tm
.level
= qemu_get_be32(f
);
421 qemu_get_be32s(f
, &s
->tm4
[i
].oldclock
);
422 qemu_get_be32s(f
, &s
->tm4
[i
].clock
);
423 qemu_get_be64s(f
, &s
->tm4
[i
].lastload
);
424 qemu_get_be32s(f
, &s
->tm4
[i
].freq
);
425 qemu_get_be32s(f
, &s
->tm4
[i
].control
);
426 pxa2xx_timer_update4(s
, now
, i
);
429 qemu_get_be32s(f
, &s
->events
);
430 qemu_get_be32s(f
, &s
->irq_enabled
);
431 qemu_get_be32s(f
, &s
->reset3
);
432 qemu_get_be32s(f
, &s
->snapshot
);
437 static pxa2xx_timer_info
*pxa2xx_timer_init(target_phys_addr_t base
,
442 pxa2xx_timer_info
*s
;
444 s
= (pxa2xx_timer_info
*) qemu_mallocz(sizeof(pxa2xx_timer_info
));
449 s
->lastload
= qemu_get_clock(vm_clock
);
452 for (i
= 0; i
< 4; i
++) {
453 s
->timer
[i
].value
= 0;
454 s
->timer
[i
].irq
= irqs
[i
];
455 s
->timer
[i
].info
= s
;
457 s
->timer
[i
].level
= 0;
458 s
->timer
[i
].qtimer
= qemu_new_timer(vm_clock
,
459 pxa2xx_timer_tick
, &s
->timer
[i
]);
462 iomemtype
= cpu_register_io_memory(0, pxa2xx_timer_readfn
,
463 pxa2xx_timer_writefn
, s
);
464 cpu_register_physical_memory(base
, 0x00001000, iomemtype
);
466 register_savevm("pxa2xx_timer", 0, 0,
467 pxa2xx_timer_save
, pxa2xx_timer_load
, s
);
472 void pxa25x_timer_init(target_phys_addr_t base
, qemu_irq
*irqs
)
474 pxa2xx_timer_info
*s
= pxa2xx_timer_init(base
, irqs
);
475 s
->freq
= PXA25X_FREQ
;
479 void pxa27x_timer_init(target_phys_addr_t base
,
480 qemu_irq
*irqs
, qemu_irq irq4
)
482 pxa2xx_timer_info
*s
= pxa2xx_timer_init(base
, irqs
);
484 s
->freq
= PXA27X_FREQ
;
485 s
->tm4
= (struct pxa2xx_timer4_s
*) qemu_mallocz(8 *
486 sizeof(struct pxa2xx_timer4_s
));
487 for (i
= 0; i
< 8; i
++) {
488 s
->tm4
[i
].tm
.value
= 0;
489 s
->tm4
[i
].tm
.irq
= irq4
;
490 s
->tm4
[i
].tm
.info
= s
;
491 s
->tm4
[i
].tm
.num
= i
+ 4;
492 s
->tm4
[i
].tm
.level
= 0;
494 s
->tm4
[i
].control
= 0x0;
495 s
->tm4
[i
].tm
.qtimer
= qemu_new_timer(vm_clock
,
496 pxa2xx_timer_tick4
, &s
->tm4
[i
]);