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.
10 #include "qemu/osdep.h"
12 #include "hw/qdev-properties.h"
13 #include "qemu/timer.h"
14 #include "sysemu/runstate.h"
15 #include "hw/arm/pxa.h"
16 #include "hw/sysbus.h"
17 #include "migration/vmstate.h"
19 #include "qemu/module.h"
33 #define OSCR 0x10 /* OS Timer Count */
42 #define OSSR 0x14 /* Timer status register */
44 #define OIER 0x1c /* Interrupt enable register 3-0 to E3-E0 */
45 #define OMCR4 0xc0 /* OS Match Control registers */
55 #define PXA25X_FREQ 3686400 /* 3.6864 MHz */
56 #define PXA27X_FREQ 3250000 /* 3.25 MHz */
58 static int pxa2xx_timer4_freq
[8] = {
64 /* [5] is the "Externally supplied clock". Assign if necessary. */
68 #define TYPE_PXA2XX_TIMER "pxa2xx-timer"
69 #define PXA2XX_TIMER(obj) \
70 OBJECT_CHECK(PXA2xxTimerInfo, (obj), TYPE_PXA2XX_TIMER)
72 typedef struct PXA2xxTimerInfo PXA2xxTimerInfo
;
79 PXA2xxTimerInfo
*info
;
91 struct PXA2xxTimerInfo
{
92 SysBusDevice parent_obj
;
101 PXA2xxTimer0 timer
[4];
103 uint32_t irq_enabled
;
111 #define PXA2XX_TIMER_HAVE_TM4 0
113 static inline int pxa2xx_timer_has_tm4(PXA2xxTimerInfo
*s
)
115 return s
->flags
& (1 << PXA2XX_TIMER_HAVE_TM4
);
118 static void pxa2xx_timer_update(void *opaque
, uint64_t now_qemu
)
120 PXA2xxTimerInfo
*s
= (PXA2xxTimerInfo
*) opaque
;
126 muldiv64(now_qemu
- s
->lastload
, s
->freq
, NANOSECONDS_PER_SECOND
);
128 for (i
= 0; i
< 4; i
++) {
129 new_qemu
= now_qemu
+ muldiv64((uint32_t) (s
->timer
[i
].value
- now_vm
),
130 NANOSECONDS_PER_SECOND
, s
->freq
);
131 timer_mod(s
->timer
[i
].qtimer
, new_qemu
);
135 static void pxa2xx_timer_update4(void *opaque
, uint64_t now_qemu
, int n
)
137 PXA2xxTimerInfo
*s
= (PXA2xxTimerInfo
*) opaque
;
140 static const int counters
[8] = { 0, 0, 0, 0, 4, 4, 6, 6 };
143 assert(n
< ARRAY_SIZE(counters
));
144 if (s
->tm4
[n
].control
& (1 << 7))
147 counter
= counters
[n
];
149 if (!s
->tm4
[counter
].freq
) {
150 timer_del(s
->tm4
[n
].tm
.qtimer
);
154 now_vm
= s
->tm4
[counter
].clock
+ muldiv64(now_qemu
-
155 s
->tm4
[counter
].lastload
,
156 s
->tm4
[counter
].freq
, NANOSECONDS_PER_SECOND
);
158 new_qemu
= now_qemu
+ muldiv64((uint32_t) (s
->tm4
[n
].tm
.value
- now_vm
),
159 NANOSECONDS_PER_SECOND
, s
->tm4
[counter
].freq
);
160 timer_mod(s
->tm4
[n
].tm
.qtimer
, new_qemu
);
163 static uint64_t pxa2xx_timer_read(void *opaque
, hwaddr offset
,
166 PXA2xxTimerInfo
*s
= (PXA2xxTimerInfo
*) opaque
;
177 return s
->timer
[tm
].value
;
193 if (!pxa2xx_timer_has_tm4(s
))
195 return s
->tm4
[tm
].tm
.value
;
197 return s
->clock
+ muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) -
198 s
->lastload
, s
->freq
, NANOSECONDS_PER_SECOND
);
214 if (!pxa2xx_timer_has_tm4(s
))
217 if ((tm
== 9 - 4 || tm
== 11 - 4) && (s
->tm4
[tm
].control
& (1 << 9))) {
218 if (s
->tm4
[tm
- 1].freq
)
219 s
->snapshot
= s
->tm4
[tm
- 1].clock
+ muldiv64(
220 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) -
221 s
->tm4
[tm
- 1].lastload
,
222 s
->tm4
[tm
- 1].freq
, NANOSECONDS_PER_SECOND
);
224 s
->snapshot
= s
->tm4
[tm
- 1].clock
;
227 if (!s
->tm4
[tm
].freq
)
228 return s
->tm4
[tm
].clock
;
229 return s
->tm4
[tm
].clock
+
230 muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) -
231 s
->tm4
[tm
].lastload
, s
->tm4
[tm
].freq
,
232 NANOSECONDS_PER_SECOND
);
234 return s
->irq_enabled
;
235 case OSSR
: /* Status register */
254 if (!pxa2xx_timer_has_tm4(s
))
256 return s
->tm4
[tm
].control
;
260 qemu_log_mask(LOG_UNIMP
,
261 "%s: unknown register 0x%02" HWADDR_PRIx
"\n",
265 qemu_log_mask(LOG_GUEST_ERROR
,
266 "%s: incorrect register 0x%02" HWADDR_PRIx
"\n",
273 static void pxa2xx_timer_write(void *opaque
, hwaddr offset
,
274 uint64_t value
, unsigned size
)
277 PXA2xxTimerInfo
*s
= (PXA2xxTimerInfo
*) opaque
;
287 s
->timer
[tm
].value
= value
;
288 pxa2xx_timer_update(s
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
));
305 if (!pxa2xx_timer_has_tm4(s
))
307 s
->tm4
[tm
].tm
.value
= value
;
308 pxa2xx_timer_update4(s
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tm
);
311 s
->oldclock
= s
->clock
;
312 s
->lastload
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
314 pxa2xx_timer_update(s
, s
->lastload
);
331 if (!pxa2xx_timer_has_tm4(s
))
333 s
->tm4
[tm
].oldclock
= s
->tm4
[tm
].clock
;
334 s
->tm4
[tm
].lastload
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
335 s
->tm4
[tm
].clock
= value
;
336 pxa2xx_timer_update4(s
, s
->tm4
[tm
].lastload
, tm
);
339 s
->irq_enabled
= value
& 0xfff;
341 case OSSR
: /* Status register */
344 for (i
= 0; i
< 4; i
++, value
>>= 1)
346 qemu_irq_lower(s
->timer
[i
].irq
);
347 if (pxa2xx_timer_has_tm4(s
) && !(s
->events
& 0xff0) && value
)
348 qemu_irq_lower(s
->irq4
);
350 case OWER
: /* XXX: Reset on OSMR3 match? */
360 if (!pxa2xx_timer_has_tm4(s
))
362 s
->tm4
[tm
].control
= value
& 0x0ff;
363 /* XXX Stop if running (shouldn't happen) */
364 if ((value
& (1 << 7)) || tm
== 0)
365 s
->tm4
[tm
].freq
= pxa2xx_timer4_freq
[value
& 7];
368 pxa2xx_timer_update4(s
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tm
);
378 if (!pxa2xx_timer_has_tm4(s
))
380 s
->tm4
[tm
].control
= value
& 0x3ff;
381 /* XXX Stop if running (shouldn't happen) */
382 if ((value
& (1 << 7)) || !(tm
& 1))
384 pxa2xx_timer4_freq
[(value
& (1 << 8)) ? 0 : (value
& 7)];
387 pxa2xx_timer_update4(s
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tm
);
391 qemu_log_mask(LOG_UNIMP
,
392 "%s: unknown register 0x%02" HWADDR_PRIx
" "
393 "(value 0x%08" PRIx64
")\n", __func__
, offset
, value
);
396 qemu_log_mask(LOG_GUEST_ERROR
,
397 "%s: incorrect register 0x%02" HWADDR_PRIx
" "
398 "(value 0x%08" PRIx64
")\n", __func__
, offset
, value
);
402 static const MemoryRegionOps pxa2xx_timer_ops
= {
403 .read
= pxa2xx_timer_read
,
404 .write
= pxa2xx_timer_write
,
405 .endianness
= DEVICE_NATIVE_ENDIAN
,
408 static void pxa2xx_timer_tick(void *opaque
)
410 PXA2xxTimer0
*t
= (PXA2xxTimer0
*) opaque
;
411 PXA2xxTimerInfo
*i
= t
->info
;
413 if (i
->irq_enabled
& (1 << t
->num
)) {
414 i
->events
|= 1 << t
->num
;
415 qemu_irq_raise(t
->irq
);
421 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET
);
425 static void pxa2xx_timer_tick4(void *opaque
)
427 PXA2xxTimer4
*t
= (PXA2xxTimer4
*) opaque
;
428 PXA2xxTimerInfo
*i
= (PXA2xxTimerInfo
*) t
->tm
.info
;
430 pxa2xx_timer_tick(&t
->tm
);
431 if (t
->control
& (1 << 3))
433 if (t
->control
& (1 << 6))
434 pxa2xx_timer_update4(i
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), t
->tm
.num
- 4);
435 if (i
->events
& 0xff0)
436 qemu_irq_raise(i
->irq4
);
439 static int pxa25x_timer_post_load(void *opaque
, int version_id
)
441 PXA2xxTimerInfo
*s
= (PXA2xxTimerInfo
*) opaque
;
445 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
446 pxa2xx_timer_update(s
, now
);
448 if (pxa2xx_timer_has_tm4(s
))
449 for (i
= 0; i
< 8; i
++)
450 pxa2xx_timer_update4(s
, now
, i
);
455 static void pxa2xx_timer_init(Object
*obj
)
457 PXA2xxTimerInfo
*s
= PXA2XX_TIMER(obj
);
458 SysBusDevice
*dev
= SYS_BUS_DEVICE(obj
);
463 s
->lastload
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
466 memory_region_init_io(&s
->iomem
, obj
, &pxa2xx_timer_ops
, s
,
467 "pxa2xx-timer", 0x00001000);
468 sysbus_init_mmio(dev
, &s
->iomem
);
471 static void pxa2xx_timer_realize(DeviceState
*dev
, Error
**errp
)
473 PXA2xxTimerInfo
*s
= PXA2XX_TIMER(dev
);
474 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
477 for (i
= 0; i
< 4; i
++) {
478 s
->timer
[i
].value
= 0;
479 sysbus_init_irq(sbd
, &s
->timer
[i
].irq
);
480 s
->timer
[i
].info
= s
;
482 s
->timer
[i
].qtimer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
,
483 pxa2xx_timer_tick
, &s
->timer
[i
]);
486 if (s
->flags
& (1 << PXA2XX_TIMER_HAVE_TM4
)) {
487 sysbus_init_irq(sbd
, &s
->irq4
);
489 for (i
= 0; i
< 8; i
++) {
490 s
->tm4
[i
].tm
.value
= 0;
491 s
->tm4
[i
].tm
.info
= s
;
492 s
->tm4
[i
].tm
.num
= i
+ 4;
494 s
->tm4
[i
].control
= 0x0;
495 s
->tm4
[i
].tm
.qtimer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
,
496 pxa2xx_timer_tick4
, &s
->tm4
[i
]);
501 static const VMStateDescription vmstate_pxa2xx_timer0_regs
= {
502 .name
= "pxa2xx_timer0",
504 .minimum_version_id
= 2,
505 .fields
= (VMStateField
[]) {
506 VMSTATE_UINT32(value
, PXA2xxTimer0
),
507 VMSTATE_END_OF_LIST(),
511 static const VMStateDescription vmstate_pxa2xx_timer4_regs
= {
512 .name
= "pxa2xx_timer4",
514 .minimum_version_id
= 1,
515 .fields
= (VMStateField
[]) {
516 VMSTATE_STRUCT(tm
, PXA2xxTimer4
, 1,
517 vmstate_pxa2xx_timer0_regs
, PXA2xxTimer0
),
518 VMSTATE_INT32(oldclock
, PXA2xxTimer4
),
519 VMSTATE_INT32(clock
, PXA2xxTimer4
),
520 VMSTATE_UINT64(lastload
, PXA2xxTimer4
),
521 VMSTATE_UINT32(freq
, PXA2xxTimer4
),
522 VMSTATE_UINT32(control
, PXA2xxTimer4
),
523 VMSTATE_END_OF_LIST(),
527 static bool pxa2xx_timer_has_tm4_test(void *opaque
, int version_id
)
529 return pxa2xx_timer_has_tm4(opaque
);
532 static const VMStateDescription vmstate_pxa2xx_timer_regs
= {
533 .name
= "pxa2xx_timer",
535 .minimum_version_id
= 1,
536 .post_load
= pxa25x_timer_post_load
,
537 .fields
= (VMStateField
[]) {
538 VMSTATE_INT32(clock
, PXA2xxTimerInfo
),
539 VMSTATE_INT32(oldclock
, PXA2xxTimerInfo
),
540 VMSTATE_UINT64(lastload
, PXA2xxTimerInfo
),
541 VMSTATE_STRUCT_ARRAY(timer
, PXA2xxTimerInfo
, 4, 1,
542 vmstate_pxa2xx_timer0_regs
, PXA2xxTimer0
),
543 VMSTATE_UINT32(events
, PXA2xxTimerInfo
),
544 VMSTATE_UINT32(irq_enabled
, PXA2xxTimerInfo
),
545 VMSTATE_UINT32(reset3
, PXA2xxTimerInfo
),
546 VMSTATE_UINT32(snapshot
, PXA2xxTimerInfo
),
547 VMSTATE_STRUCT_ARRAY_TEST(tm4
, PXA2xxTimerInfo
, 8,
548 pxa2xx_timer_has_tm4_test
, 0,
549 vmstate_pxa2xx_timer4_regs
, PXA2xxTimer4
),
550 VMSTATE_END_OF_LIST(),
554 static Property pxa25x_timer_dev_properties
[] = {
555 DEFINE_PROP_UINT32("freq", PXA2xxTimerInfo
, freq
, PXA25X_FREQ
),
556 DEFINE_PROP_BIT("tm4", PXA2xxTimerInfo
, flags
,
557 PXA2XX_TIMER_HAVE_TM4
, false),
558 DEFINE_PROP_END_OF_LIST(),
561 static void pxa25x_timer_dev_class_init(ObjectClass
*klass
, void *data
)
563 DeviceClass
*dc
= DEVICE_CLASS(klass
);
565 dc
->desc
= "PXA25x timer";
566 device_class_set_props(dc
, pxa25x_timer_dev_properties
);
569 static const TypeInfo pxa25x_timer_dev_info
= {
570 .name
= "pxa25x-timer",
571 .parent
= TYPE_PXA2XX_TIMER
,
572 .instance_size
= sizeof(PXA2xxTimerInfo
),
573 .class_init
= pxa25x_timer_dev_class_init
,
576 static Property pxa27x_timer_dev_properties
[] = {
577 DEFINE_PROP_UINT32("freq", PXA2xxTimerInfo
, freq
, PXA27X_FREQ
),
578 DEFINE_PROP_BIT("tm4", PXA2xxTimerInfo
, flags
,
579 PXA2XX_TIMER_HAVE_TM4
, true),
580 DEFINE_PROP_END_OF_LIST(),
583 static void pxa27x_timer_dev_class_init(ObjectClass
*klass
, void *data
)
585 DeviceClass
*dc
= DEVICE_CLASS(klass
);
587 dc
->desc
= "PXA27x timer";
588 device_class_set_props(dc
, pxa27x_timer_dev_properties
);
591 static const TypeInfo pxa27x_timer_dev_info
= {
592 .name
= "pxa27x-timer",
593 .parent
= TYPE_PXA2XX_TIMER
,
594 .instance_size
= sizeof(PXA2xxTimerInfo
),
595 .class_init
= pxa27x_timer_dev_class_init
,
598 static void pxa2xx_timer_class_init(ObjectClass
*oc
, void *data
)
600 DeviceClass
*dc
= DEVICE_CLASS(oc
);
602 dc
->realize
= pxa2xx_timer_realize
;
603 dc
->vmsd
= &vmstate_pxa2xx_timer_regs
;
606 static const TypeInfo pxa2xx_timer_type_info
= {
607 .name
= TYPE_PXA2XX_TIMER
,
608 .parent
= TYPE_SYS_BUS_DEVICE
,
609 .instance_size
= sizeof(PXA2xxTimerInfo
),
610 .instance_init
= pxa2xx_timer_init
,
612 .class_init
= pxa2xx_timer_class_init
,
615 static void pxa2xx_timer_register_types(void)
617 type_register_static(&pxa2xx_timer_type_info
);
618 type_register_static(&pxa25x_timer_dev_info
);
619 type_register_static(&pxa27x_timer_dev_info
);
622 type_init(pxa2xx_timer_register_types
)