2 * Intel XScale PXA255/270 OS Timers.
4 * Copyright (c) 2006 Openedhand Ltd.
5 * Copyright (c) 2006 Thorsten Zitterell
7 * This code is licensed under the GPL.
11 #include "qemu/timer.h"
12 #include "sysemu/sysemu.h"
13 #include "hw/arm/pxa.h"
14 #include "hw/sysbus.h"
28 #define OSCR 0x10 /* OS Timer Count */
37 #define OSSR 0x14 /* Timer status register */
39 #define OIER 0x1c /* Interrupt enable register 3-0 to E3-E0 */
40 #define OMCR4 0xc0 /* OS Match Control registers */
50 #define PXA25X_FREQ 3686400 /* 3.6864 MHz */
51 #define PXA27X_FREQ 3250000 /* 3.25 MHz */
53 static int pxa2xx_timer4_freq
[8] = {
59 /* [5] is the "Externally supplied clock". Assign if necessary. */
63 #define TYPE_PXA2XX_TIMER "pxa2xx-timer"
64 #define PXA2XX_TIMER(obj) \
65 OBJECT_CHECK(PXA2xxTimerInfo, (obj), TYPE_PXA2XX_TIMER)
67 typedef struct PXA2xxTimerInfo PXA2xxTimerInfo
;
74 PXA2xxTimerInfo
*info
;
86 struct PXA2xxTimerInfo
{
87 SysBusDevice parent_obj
;
96 PXA2xxTimer0 timer
[4];
106 #define PXA2XX_TIMER_HAVE_TM4 0
108 static inline int pxa2xx_timer_has_tm4(PXA2xxTimerInfo
*s
)
110 return s
->flags
& (1 << PXA2XX_TIMER_HAVE_TM4
);
113 static void pxa2xx_timer_update(void *opaque
, uint64_t now_qemu
)
115 PXA2xxTimerInfo
*s
= (PXA2xxTimerInfo
*) opaque
;
121 muldiv64(now_qemu
- s
->lastload
, s
->freq
, get_ticks_per_sec());
123 for (i
= 0; i
< 4; i
++) {
124 new_qemu
= now_qemu
+ muldiv64((uint32_t) (s
->timer
[i
].value
- now_vm
),
125 get_ticks_per_sec(), s
->freq
);
126 timer_mod(s
->timer
[i
].qtimer
, new_qemu
);
130 static void pxa2xx_timer_update4(void *opaque
, uint64_t now_qemu
, int n
)
132 PXA2xxTimerInfo
*s
= (PXA2xxTimerInfo
*) opaque
;
135 static const int counters
[8] = { 0, 0, 0, 0, 4, 4, 6, 6 };
138 if (s
->tm4
[n
].control
& (1 << 7))
141 counter
= counters
[n
];
143 if (!s
->tm4
[counter
].freq
) {
144 timer_del(s
->tm4
[n
].tm
.qtimer
);
148 now_vm
= s
->tm4
[counter
].clock
+ muldiv64(now_qemu
-
149 s
->tm4
[counter
].lastload
,
150 s
->tm4
[counter
].freq
, get_ticks_per_sec());
152 new_qemu
= now_qemu
+ muldiv64((uint32_t) (s
->tm4
[n
].tm
.value
- now_vm
),
153 get_ticks_per_sec(), s
->tm4
[counter
].freq
);
154 timer_mod(s
->tm4
[n
].tm
.qtimer
, new_qemu
);
157 static uint64_t pxa2xx_timer_read(void *opaque
, hwaddr offset
,
160 PXA2xxTimerInfo
*s
= (PXA2xxTimerInfo
*) opaque
;
171 return s
->timer
[tm
].value
;
187 if (!pxa2xx_timer_has_tm4(s
))
189 return s
->tm4
[tm
].tm
.value
;
191 return s
->clock
+ muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) -
192 s
->lastload
, s
->freq
, get_ticks_per_sec());
208 if (!pxa2xx_timer_has_tm4(s
))
211 if ((tm
== 9 - 4 || tm
== 11 - 4) && (s
->tm4
[tm
].control
& (1 << 9))) {
212 if (s
->tm4
[tm
- 1].freq
)
213 s
->snapshot
= s
->tm4
[tm
- 1].clock
+ muldiv64(
214 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) -
215 s
->tm4
[tm
- 1].lastload
,
216 s
->tm4
[tm
- 1].freq
, get_ticks_per_sec());
218 s
->snapshot
= s
->tm4
[tm
- 1].clock
;
221 if (!s
->tm4
[tm
].freq
)
222 return s
->tm4
[tm
].clock
;
223 return s
->tm4
[tm
].clock
+ muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) -
224 s
->tm4
[tm
].lastload
, s
->tm4
[tm
].freq
, get_ticks_per_sec());
226 return s
->irq_enabled
;
227 case OSSR
: /* Status register */
246 if (!pxa2xx_timer_has_tm4(s
))
248 return s
->tm4
[tm
].control
;
253 hw_error("pxa2xx_timer_read: Bad offset " REG_FMT
"\n", offset
);
259 static void pxa2xx_timer_write(void *opaque
, hwaddr offset
,
260 uint64_t value
, unsigned size
)
263 PXA2xxTimerInfo
*s
= (PXA2xxTimerInfo
*) opaque
;
273 s
->timer
[tm
].value
= value
;
274 pxa2xx_timer_update(s
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
));
291 if (!pxa2xx_timer_has_tm4(s
))
293 s
->tm4
[tm
].tm
.value
= value
;
294 pxa2xx_timer_update4(s
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tm
);
297 s
->oldclock
= s
->clock
;
298 s
->lastload
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
300 pxa2xx_timer_update(s
, s
->lastload
);
317 if (!pxa2xx_timer_has_tm4(s
))
319 s
->tm4
[tm
].oldclock
= s
->tm4
[tm
].clock
;
320 s
->tm4
[tm
].lastload
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
321 s
->tm4
[tm
].clock
= value
;
322 pxa2xx_timer_update4(s
, s
->tm4
[tm
].lastload
, tm
);
325 s
->irq_enabled
= value
& 0xfff;
327 case OSSR
: /* Status register */
330 for (i
= 0; i
< 4; i
++, value
>>= 1)
332 qemu_irq_lower(s
->timer
[i
].irq
);
333 if (pxa2xx_timer_has_tm4(s
) && !(s
->events
& 0xff0) && value
)
334 qemu_irq_lower(s
->irq4
);
336 case OWER
: /* XXX: Reset on OSMR3 match? */
346 if (!pxa2xx_timer_has_tm4(s
))
348 s
->tm4
[tm
].control
= value
& 0x0ff;
349 /* XXX Stop if running (shouldn't happen) */
350 if ((value
& (1 << 7)) || tm
== 0)
351 s
->tm4
[tm
].freq
= pxa2xx_timer4_freq
[value
& 7];
354 pxa2xx_timer_update4(s
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tm
);
364 if (!pxa2xx_timer_has_tm4(s
))
366 s
->tm4
[tm
].control
= value
& 0x3ff;
367 /* XXX Stop if running (shouldn't happen) */
368 if ((value
& (1 << 7)) || !(tm
& 1))
370 pxa2xx_timer4_freq
[(value
& (1 << 8)) ? 0 : (value
& 7)];
373 pxa2xx_timer_update4(s
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tm
);
378 hw_error("pxa2xx_timer_write: Bad offset " REG_FMT
"\n", offset
);
382 static const MemoryRegionOps pxa2xx_timer_ops
= {
383 .read
= pxa2xx_timer_read
,
384 .write
= pxa2xx_timer_write
,
385 .endianness
= DEVICE_NATIVE_ENDIAN
,
388 static void pxa2xx_timer_tick(void *opaque
)
390 PXA2xxTimer0
*t
= (PXA2xxTimer0
*) opaque
;
391 PXA2xxTimerInfo
*i
= t
->info
;
393 if (i
->irq_enabled
& (1 << t
->num
)) {
394 i
->events
|= 1 << t
->num
;
395 qemu_irq_raise(t
->irq
);
401 qemu_system_reset_request();
405 static void pxa2xx_timer_tick4(void *opaque
)
407 PXA2xxTimer4
*t
= (PXA2xxTimer4
*) opaque
;
408 PXA2xxTimerInfo
*i
= (PXA2xxTimerInfo
*) t
->tm
.info
;
410 pxa2xx_timer_tick(&t
->tm
);
411 if (t
->control
& (1 << 3))
413 if (t
->control
& (1 << 6))
414 pxa2xx_timer_update4(i
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), t
->tm
.num
- 4);
415 if (i
->events
& 0xff0)
416 qemu_irq_raise(i
->irq4
);
419 static int pxa25x_timer_post_load(void *opaque
, int version_id
)
421 PXA2xxTimerInfo
*s
= (PXA2xxTimerInfo
*) opaque
;
425 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
426 pxa2xx_timer_update(s
, now
);
428 if (pxa2xx_timer_has_tm4(s
))
429 for (i
= 0; i
< 8; i
++)
430 pxa2xx_timer_update4(s
, now
, i
);
435 static int pxa2xx_timer_init(SysBusDevice
*dev
)
437 PXA2xxTimerInfo
*s
= PXA2XX_TIMER(dev
);
443 s
->lastload
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
446 for (i
= 0; i
< 4; i
++) {
447 s
->timer
[i
].value
= 0;
448 sysbus_init_irq(dev
, &s
->timer
[i
].irq
);
449 s
->timer
[i
].info
= s
;
451 s
->timer
[i
].qtimer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
,
452 pxa2xx_timer_tick
, &s
->timer
[i
]);
454 if (s
->flags
& (1 << PXA2XX_TIMER_HAVE_TM4
)) {
455 sysbus_init_irq(dev
, &s
->irq4
);
457 for (i
= 0; i
< 8; i
++) {
458 s
->tm4
[i
].tm
.value
= 0;
459 s
->tm4
[i
].tm
.info
= s
;
460 s
->tm4
[i
].tm
.num
= i
+ 4;
462 s
->tm4
[i
].control
= 0x0;
463 s
->tm4
[i
].tm
.qtimer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
,
464 pxa2xx_timer_tick4
, &s
->tm4
[i
]);
468 memory_region_init_io(&s
->iomem
, OBJECT(s
), &pxa2xx_timer_ops
, s
,
469 "pxa2xx-timer", 0x00001000);
470 sysbus_init_mmio(dev
, &s
->iomem
);
475 static const VMStateDescription vmstate_pxa2xx_timer0_regs
= {
476 .name
= "pxa2xx_timer0",
478 .minimum_version_id
= 2,
479 .minimum_version_id_old
= 2,
480 .fields
= (VMStateField
[]) {
481 VMSTATE_UINT32(value
, PXA2xxTimer0
),
482 VMSTATE_END_OF_LIST(),
486 static const VMStateDescription vmstate_pxa2xx_timer4_regs
= {
487 .name
= "pxa2xx_timer4",
489 .minimum_version_id
= 1,
490 .minimum_version_id_old
= 1,
491 .fields
= (VMStateField
[]) {
492 VMSTATE_STRUCT(tm
, PXA2xxTimer4
, 1,
493 vmstate_pxa2xx_timer0_regs
, PXA2xxTimer0
),
494 VMSTATE_INT32(oldclock
, PXA2xxTimer4
),
495 VMSTATE_INT32(clock
, PXA2xxTimer4
),
496 VMSTATE_UINT64(lastload
, PXA2xxTimer4
),
497 VMSTATE_UINT32(freq
, PXA2xxTimer4
),
498 VMSTATE_UINT32(control
, PXA2xxTimer4
),
499 VMSTATE_END_OF_LIST(),
503 static bool pxa2xx_timer_has_tm4_test(void *opaque
, int version_id
)
505 return pxa2xx_timer_has_tm4(opaque
);
508 static const VMStateDescription vmstate_pxa2xx_timer_regs
= {
509 .name
= "pxa2xx_timer",
511 .minimum_version_id
= 1,
512 .minimum_version_id_old
= 1,
513 .post_load
= pxa25x_timer_post_load
,
514 .fields
= (VMStateField
[]) {
515 VMSTATE_INT32(clock
, PXA2xxTimerInfo
),
516 VMSTATE_INT32(oldclock
, PXA2xxTimerInfo
),
517 VMSTATE_UINT64(lastload
, PXA2xxTimerInfo
),
518 VMSTATE_STRUCT_ARRAY(timer
, PXA2xxTimerInfo
, 4, 1,
519 vmstate_pxa2xx_timer0_regs
, PXA2xxTimer0
),
520 VMSTATE_UINT32(events
, PXA2xxTimerInfo
),
521 VMSTATE_UINT32(irq_enabled
, PXA2xxTimerInfo
),
522 VMSTATE_UINT32(reset3
, PXA2xxTimerInfo
),
523 VMSTATE_UINT32(snapshot
, PXA2xxTimerInfo
),
524 VMSTATE_STRUCT_ARRAY_TEST(tm4
, PXA2xxTimerInfo
, 8,
525 pxa2xx_timer_has_tm4_test
, 0,
526 vmstate_pxa2xx_timer4_regs
, PXA2xxTimer4
),
527 VMSTATE_END_OF_LIST(),
531 static Property pxa25x_timer_dev_properties
[] = {
532 DEFINE_PROP_UINT32("freq", PXA2xxTimerInfo
, freq
, PXA25X_FREQ
),
533 DEFINE_PROP_BIT("tm4", PXA2xxTimerInfo
, flags
,
534 PXA2XX_TIMER_HAVE_TM4
, false),
535 DEFINE_PROP_END_OF_LIST(),
538 static void pxa25x_timer_dev_class_init(ObjectClass
*klass
, void *data
)
540 DeviceClass
*dc
= DEVICE_CLASS(klass
);
542 dc
->desc
= "PXA25x timer";
543 dc
->props
= pxa25x_timer_dev_properties
;
546 static const TypeInfo pxa25x_timer_dev_info
= {
547 .name
= "pxa25x-timer",
548 .parent
= TYPE_PXA2XX_TIMER
,
549 .instance_size
= sizeof(PXA2xxTimerInfo
),
550 .class_init
= pxa25x_timer_dev_class_init
,
553 static Property pxa27x_timer_dev_properties
[] = {
554 DEFINE_PROP_UINT32("freq", PXA2xxTimerInfo
, freq
, PXA27X_FREQ
),
555 DEFINE_PROP_BIT("tm4", PXA2xxTimerInfo
, flags
,
556 PXA2XX_TIMER_HAVE_TM4
, true),
557 DEFINE_PROP_END_OF_LIST(),
560 static void pxa27x_timer_dev_class_init(ObjectClass
*klass
, void *data
)
562 DeviceClass
*dc
= DEVICE_CLASS(klass
);
564 dc
->desc
= "PXA27x timer";
565 dc
->props
= pxa27x_timer_dev_properties
;
568 static const TypeInfo pxa27x_timer_dev_info
= {
569 .name
= "pxa27x-timer",
570 .parent
= TYPE_PXA2XX_TIMER
,
571 .instance_size
= sizeof(PXA2xxTimerInfo
),
572 .class_init
= pxa27x_timer_dev_class_init
,
575 static void pxa2xx_timer_class_init(ObjectClass
*oc
, void *data
)
577 DeviceClass
*dc
= DEVICE_CLASS(oc
);
578 SysBusDeviceClass
*sdc
= SYS_BUS_DEVICE_CLASS(oc
);
580 sdc
->init
= pxa2xx_timer_init
;
581 dc
->vmsd
= &vmstate_pxa2xx_timer_regs
;
584 static const TypeInfo pxa2xx_timer_type_info
= {
585 .name
= TYPE_PXA2XX_TIMER
,
586 .parent
= TYPE_SYS_BUS_DEVICE
,
587 .instance_size
= sizeof(PXA2xxTimerInfo
),
589 .class_init
= pxa2xx_timer_class_init
,
592 static void pxa2xx_timer_register_types(void)
594 type_register_static(&pxa2xx_timer_type_info
);
595 type_register_static(&pxa25x_timer_dev_info
);
596 type_register_static(&pxa27x_timer_dev_info
);
599 type_init(pxa2xx_timer_register_types
)