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/sysbus.h"
16 #include "migration/vmstate.h"
18 #include "qemu/module.h"
19 #include "qom/object.h"
20 #include "sysemu/watchdog.h"
34 #define OSCR 0x10 /* OS Timer Count */
43 #define OSSR 0x14 /* Timer status register */
45 #define OIER 0x1c /* Interrupt enable register 3-0 to E3-E0 */
46 #define OMCR4 0xc0 /* OS Match Control registers */
56 #define PXA25X_FREQ 3686400 /* 3.6864 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 OBJECT_DECLARE_SIMPLE_TYPE(PXA2xxTimerInfo
, PXA2XX_TIMER
)
77 PXA2xxTimerInfo
*info
;
89 struct PXA2xxTimerInfo
{
90 SysBusDevice parent_obj
;
99 PXA2xxTimer0 timer
[4];
101 uint32_t irq_enabled
;
109 #define PXA2XX_TIMER_HAVE_TM4 0
111 static inline int pxa2xx_timer_has_tm4(PXA2xxTimerInfo
*s
)
113 return s
->flags
& (1 << PXA2XX_TIMER_HAVE_TM4
);
116 static void pxa2xx_timer_update(void *opaque
, uint64_t now_qemu
)
118 PXA2xxTimerInfo
*s
= (PXA2xxTimerInfo
*) opaque
;
124 muldiv64(now_qemu
- s
->lastload
, s
->freq
, NANOSECONDS_PER_SECOND
);
126 for (i
= 0; i
< 4; i
++) {
127 new_qemu
= now_qemu
+ muldiv64((uint32_t) (s
->timer
[i
].value
- now_vm
),
128 NANOSECONDS_PER_SECOND
, s
->freq
);
129 timer_mod(s
->timer
[i
].qtimer
, new_qemu
);
133 static void pxa2xx_timer_update4(void *opaque
, uint64_t now_qemu
, int n
)
135 PXA2xxTimerInfo
*s
= (PXA2xxTimerInfo
*) opaque
;
138 static const int counters
[8] = { 0, 0, 0, 0, 4, 4, 6, 6 };
141 assert(n
< ARRAY_SIZE(counters
));
142 if (s
->tm4
[n
].control
& (1 << 7))
145 counter
= counters
[n
];
147 if (!s
->tm4
[counter
].freq
) {
148 timer_del(s
->tm4
[n
].tm
.qtimer
);
152 now_vm
= s
->tm4
[counter
].clock
+ muldiv64(now_qemu
-
153 s
->tm4
[counter
].lastload
,
154 s
->tm4
[counter
].freq
, NANOSECONDS_PER_SECOND
);
156 new_qemu
= now_qemu
+ muldiv64((uint32_t) (s
->tm4
[n
].tm
.value
- now_vm
),
157 NANOSECONDS_PER_SECOND
, s
->tm4
[counter
].freq
);
158 timer_mod(s
->tm4
[n
].tm
.qtimer
, new_qemu
);
161 static uint64_t pxa2xx_timer_read(void *opaque
, hwaddr offset
,
164 PXA2xxTimerInfo
*s
= (PXA2xxTimerInfo
*) opaque
;
175 return s
->timer
[tm
].value
;
191 if (!pxa2xx_timer_has_tm4(s
))
193 return s
->tm4
[tm
].tm
.value
;
195 return s
->clock
+ muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) -
196 s
->lastload
, s
->freq
, NANOSECONDS_PER_SECOND
);
212 if (!pxa2xx_timer_has_tm4(s
))
215 if ((tm
== 9 - 4 || tm
== 11 - 4) && (s
->tm4
[tm
].control
& (1 << 9))) {
216 if (s
->tm4
[tm
- 1].freq
)
217 s
->snapshot
= s
->tm4
[tm
- 1].clock
+ muldiv64(
218 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) -
219 s
->tm4
[tm
- 1].lastload
,
220 s
->tm4
[tm
- 1].freq
, NANOSECONDS_PER_SECOND
);
222 s
->snapshot
= s
->tm4
[tm
- 1].clock
;
225 if (!s
->tm4
[tm
].freq
)
226 return s
->tm4
[tm
].clock
;
227 return s
->tm4
[tm
].clock
+
228 muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) -
229 s
->tm4
[tm
].lastload
, s
->tm4
[tm
].freq
,
230 NANOSECONDS_PER_SECOND
);
232 return s
->irq_enabled
;
233 case OSSR
: /* Status register */
252 if (!pxa2xx_timer_has_tm4(s
))
254 return s
->tm4
[tm
].control
;
258 qemu_log_mask(LOG_UNIMP
,
259 "%s: unknown register 0x%02" HWADDR_PRIx
"\n",
263 qemu_log_mask(LOG_GUEST_ERROR
,
264 "%s: incorrect register 0x%02" HWADDR_PRIx
"\n",
271 static void pxa2xx_timer_write(void *opaque
, hwaddr offset
,
272 uint64_t value
, unsigned size
)
275 PXA2xxTimerInfo
*s
= (PXA2xxTimerInfo
*) opaque
;
285 s
->timer
[tm
].value
= value
;
286 pxa2xx_timer_update(s
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
));
303 if (!pxa2xx_timer_has_tm4(s
))
305 s
->tm4
[tm
].tm
.value
= value
;
306 pxa2xx_timer_update4(s
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tm
);
309 s
->oldclock
= s
->clock
;
310 s
->lastload
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
312 pxa2xx_timer_update(s
, s
->lastload
);
329 if (!pxa2xx_timer_has_tm4(s
))
331 s
->tm4
[tm
].oldclock
= s
->tm4
[tm
].clock
;
332 s
->tm4
[tm
].lastload
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
333 s
->tm4
[tm
].clock
= value
;
334 pxa2xx_timer_update4(s
, s
->tm4
[tm
].lastload
, tm
);
337 s
->irq_enabled
= value
& 0xfff;
339 case OSSR
: /* Status register */
342 for (i
= 0; i
< 4; i
++, value
>>= 1)
344 qemu_irq_lower(s
->timer
[i
].irq
);
345 if (pxa2xx_timer_has_tm4(s
) && !(s
->events
& 0xff0) && value
)
346 qemu_irq_lower(s
->irq4
);
348 case OWER
: /* XXX: Reset on OSMR3 match? */
358 if (!pxa2xx_timer_has_tm4(s
))
360 s
->tm4
[tm
].control
= value
& 0x0ff;
361 /* XXX Stop if running (shouldn't happen) */
362 if ((value
& (1 << 7)) || tm
== 0)
363 s
->tm4
[tm
].freq
= pxa2xx_timer4_freq
[value
& 7];
366 pxa2xx_timer_update4(s
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tm
);
376 if (!pxa2xx_timer_has_tm4(s
))
378 s
->tm4
[tm
].control
= value
& 0x3ff;
379 /* XXX Stop if running (shouldn't happen) */
380 if ((value
& (1 << 7)) || !(tm
& 1))
382 pxa2xx_timer4_freq
[(value
& (1 << 8)) ? 0 : (value
& 7)];
385 pxa2xx_timer_update4(s
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tm
);
389 qemu_log_mask(LOG_UNIMP
,
390 "%s: unknown register 0x%02" HWADDR_PRIx
" "
391 "(value 0x%08" PRIx64
")\n", __func__
, offset
, value
);
394 qemu_log_mask(LOG_GUEST_ERROR
,
395 "%s: incorrect register 0x%02" HWADDR_PRIx
" "
396 "(value 0x%08" PRIx64
")\n", __func__
, offset
, value
);
400 static const MemoryRegionOps pxa2xx_timer_ops
= {
401 .read
= pxa2xx_timer_read
,
402 .write
= pxa2xx_timer_write
,
403 .endianness
= DEVICE_NATIVE_ENDIAN
,
406 static void pxa2xx_timer_tick(void *opaque
)
408 PXA2xxTimer0
*t
= (PXA2xxTimer0
*) opaque
;
409 PXA2xxTimerInfo
*i
= t
->info
;
411 if (i
->irq_enabled
& (1 << t
->num
)) {
412 i
->events
|= 1 << t
->num
;
413 qemu_irq_raise(t
->irq
);
419 watchdog_perform_action();
423 static void pxa2xx_timer_tick4(void *opaque
)
425 PXA2xxTimer4
*t
= (PXA2xxTimer4
*) opaque
;
426 PXA2xxTimerInfo
*i
= (PXA2xxTimerInfo
*) t
->tm
.info
;
428 pxa2xx_timer_tick(&t
->tm
);
429 if (t
->control
& (1 << 3))
431 if (t
->control
& (1 << 6))
432 pxa2xx_timer_update4(i
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), t
->tm
.num
- 4);
433 if (i
->events
& 0xff0)
434 qemu_irq_raise(i
->irq4
);
437 static int pxa25x_timer_post_load(void *opaque
, int version_id
)
439 PXA2xxTimerInfo
*s
= (PXA2xxTimerInfo
*) opaque
;
443 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
444 pxa2xx_timer_update(s
, now
);
446 if (pxa2xx_timer_has_tm4(s
))
447 for (i
= 0; i
< 8; i
++)
448 pxa2xx_timer_update4(s
, now
, i
);
453 static void pxa2xx_timer_init(Object
*obj
)
455 PXA2xxTimerInfo
*s
= PXA2XX_TIMER(obj
);
456 SysBusDevice
*dev
= SYS_BUS_DEVICE(obj
);
461 s
->lastload
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
464 memory_region_init_io(&s
->iomem
, obj
, &pxa2xx_timer_ops
, s
,
465 "pxa2xx-timer", 0x00001000);
466 sysbus_init_mmio(dev
, &s
->iomem
);
469 static void pxa2xx_timer_realize(DeviceState
*dev
, Error
**errp
)
471 PXA2xxTimerInfo
*s
= PXA2XX_TIMER(dev
);
472 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
475 for (i
= 0; i
< 4; i
++) {
476 s
->timer
[i
].value
= 0;
477 sysbus_init_irq(sbd
, &s
->timer
[i
].irq
);
478 s
->timer
[i
].info
= s
;
480 s
->timer
[i
].qtimer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
,
481 pxa2xx_timer_tick
, &s
->timer
[i
]);
484 if (s
->flags
& (1 << PXA2XX_TIMER_HAVE_TM4
)) {
485 sysbus_init_irq(sbd
, &s
->irq4
);
487 for (i
= 0; i
< 8; i
++) {
488 s
->tm4
[i
].tm
.value
= 0;
489 s
->tm4
[i
].tm
.info
= s
;
490 s
->tm4
[i
].tm
.num
= i
+ 4;
492 s
->tm4
[i
].control
= 0x0;
493 s
->tm4
[i
].tm
.qtimer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
,
494 pxa2xx_timer_tick4
, &s
->tm4
[i
]);
499 static const VMStateDescription vmstate_pxa2xx_timer0_regs
= {
500 .name
= "pxa2xx_timer0",
502 .minimum_version_id
= 2,
503 .fields
= (const VMStateField
[]) {
504 VMSTATE_UINT32(value
, PXA2xxTimer0
),
505 VMSTATE_END_OF_LIST(),
509 static const VMStateDescription vmstate_pxa2xx_timer4_regs
= {
510 .name
= "pxa2xx_timer4",
512 .minimum_version_id
= 1,
513 .fields
= (const VMStateField
[]) {
514 VMSTATE_STRUCT(tm
, PXA2xxTimer4
, 1,
515 vmstate_pxa2xx_timer0_regs
, PXA2xxTimer0
),
516 VMSTATE_INT32(oldclock
, PXA2xxTimer4
),
517 VMSTATE_INT32(clock
, PXA2xxTimer4
),
518 VMSTATE_UINT64(lastload
, PXA2xxTimer4
),
519 VMSTATE_UINT32(freq
, PXA2xxTimer4
),
520 VMSTATE_UINT32(control
, PXA2xxTimer4
),
521 VMSTATE_END_OF_LIST(),
525 static bool pxa2xx_timer_has_tm4_test(void *opaque
, int version_id
)
527 return pxa2xx_timer_has_tm4(opaque
);
530 static const VMStateDescription vmstate_pxa2xx_timer_regs
= {
531 .name
= "pxa2xx_timer",
533 .minimum_version_id
= 1,
534 .post_load
= pxa25x_timer_post_load
,
535 .fields
= (const VMStateField
[]) {
536 VMSTATE_INT32(clock
, PXA2xxTimerInfo
),
537 VMSTATE_INT32(oldclock
, PXA2xxTimerInfo
),
538 VMSTATE_UINT64(lastload
, PXA2xxTimerInfo
),
539 VMSTATE_STRUCT_ARRAY(timer
, PXA2xxTimerInfo
, 4, 1,
540 vmstate_pxa2xx_timer0_regs
, PXA2xxTimer0
),
541 VMSTATE_UINT32(events
, PXA2xxTimerInfo
),
542 VMSTATE_UINT32(irq_enabled
, PXA2xxTimerInfo
),
543 VMSTATE_UINT32(reset3
, PXA2xxTimerInfo
),
544 VMSTATE_UINT32(snapshot
, PXA2xxTimerInfo
),
545 VMSTATE_STRUCT_ARRAY_TEST(tm4
, PXA2xxTimerInfo
, 8,
546 pxa2xx_timer_has_tm4_test
, 0,
547 vmstate_pxa2xx_timer4_regs
, PXA2xxTimer4
),
548 VMSTATE_END_OF_LIST(),
552 static Property pxa25x_timer_dev_properties
[] = {
553 DEFINE_PROP_UINT32("freq", PXA2xxTimerInfo
, freq
, PXA25X_FREQ
),
554 DEFINE_PROP_BIT("tm4", PXA2xxTimerInfo
, flags
,
555 PXA2XX_TIMER_HAVE_TM4
, false),
556 DEFINE_PROP_END_OF_LIST(),
559 static void pxa25x_timer_dev_class_init(ObjectClass
*klass
, void *data
)
561 DeviceClass
*dc
= DEVICE_CLASS(klass
);
563 dc
->desc
= "PXA25x timer";
564 device_class_set_props(dc
, pxa25x_timer_dev_properties
);
567 static const TypeInfo pxa25x_timer_dev_info
= {
568 .name
= "pxa25x-timer",
569 .parent
= TYPE_PXA2XX_TIMER
,
570 .instance_size
= sizeof(PXA2xxTimerInfo
),
571 .class_init
= pxa25x_timer_dev_class_init
,
574 static void pxa2xx_timer_class_init(ObjectClass
*oc
, void *data
)
576 DeviceClass
*dc
= DEVICE_CLASS(oc
);
578 dc
->realize
= pxa2xx_timer_realize
;
579 dc
->vmsd
= &vmstate_pxa2xx_timer_regs
;
582 static const TypeInfo pxa2xx_timer_type_info
= {
583 .name
= TYPE_PXA2XX_TIMER
,
584 .parent
= TYPE_SYS_BUS_DEVICE
,
585 .instance_size
= sizeof(PXA2xxTimerInfo
),
586 .instance_init
= pxa2xx_timer_init
,
588 .class_init
= pxa2xx_timer_class_init
,
591 static void pxa2xx_timer_register_types(void)
593 type_register_static(&pxa2xx_timer_type_info
);
594 type_register_static(&pxa25x_timer_dev_info
);
597 type_init(pxa2xx_timer_register_types
)