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 "qemu/timer.h"
13 #include "sysemu/sysemu.h"
14 #include "hw/arm/pxa.h"
15 #include "hw/sysbus.h"
17 #include "qemu/module.h"
31 #define OSCR 0x10 /* OS Timer Count */
40 #define OSSR 0x14 /* Timer status register */
42 #define OIER 0x1c /* Interrupt enable register 3-0 to E3-E0 */
43 #define OMCR4 0xc0 /* OS Match Control registers */
53 #define PXA25X_FREQ 3686400 /* 3.6864 MHz */
54 #define PXA27X_FREQ 3250000 /* 3.25 MHz */
56 static int pxa2xx_timer4_freq
[8] = {
62 /* [5] is the "Externally supplied clock". Assign if necessary. */
66 #define TYPE_PXA2XX_TIMER "pxa2xx-timer"
67 #define PXA2XX_TIMER(obj) \
68 OBJECT_CHECK(PXA2xxTimerInfo, (obj), TYPE_PXA2XX_TIMER)
70 typedef struct PXA2xxTimerInfo PXA2xxTimerInfo
;
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 if (s
->tm4
[n
].control
& (1 << 7))
144 counter
= counters
[n
];
146 if (!s
->tm4
[counter
].freq
) {
147 timer_del(s
->tm4
[n
].tm
.qtimer
);
151 now_vm
= s
->tm4
[counter
].clock
+ muldiv64(now_qemu
-
152 s
->tm4
[counter
].lastload
,
153 s
->tm4
[counter
].freq
, NANOSECONDS_PER_SECOND
);
155 new_qemu
= now_qemu
+ muldiv64((uint32_t) (s
->tm4
[n
].tm
.value
- now_vm
),
156 NANOSECONDS_PER_SECOND
, s
->tm4
[counter
].freq
);
157 timer_mod(s
->tm4
[n
].tm
.qtimer
, new_qemu
);
160 static uint64_t pxa2xx_timer_read(void *opaque
, hwaddr offset
,
163 PXA2xxTimerInfo
*s
= (PXA2xxTimerInfo
*) opaque
;
174 return s
->timer
[tm
].value
;
190 if (!pxa2xx_timer_has_tm4(s
))
192 return s
->tm4
[tm
].tm
.value
;
194 return s
->clock
+ muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) -
195 s
->lastload
, s
->freq
, NANOSECONDS_PER_SECOND
);
211 if (!pxa2xx_timer_has_tm4(s
))
214 if ((tm
== 9 - 4 || tm
== 11 - 4) && (s
->tm4
[tm
].control
& (1 << 9))) {
215 if (s
->tm4
[tm
- 1].freq
)
216 s
->snapshot
= s
->tm4
[tm
- 1].clock
+ muldiv64(
217 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) -
218 s
->tm4
[tm
- 1].lastload
,
219 s
->tm4
[tm
- 1].freq
, NANOSECONDS_PER_SECOND
);
221 s
->snapshot
= s
->tm4
[tm
- 1].clock
;
224 if (!s
->tm4
[tm
].freq
)
225 return s
->tm4
[tm
].clock
;
226 return s
->tm4
[tm
].clock
+
227 muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) -
228 s
->tm4
[tm
].lastload
, s
->tm4
[tm
].freq
,
229 NANOSECONDS_PER_SECOND
);
231 return s
->irq_enabled
;
232 case OSSR
: /* Status register */
251 if (!pxa2xx_timer_has_tm4(s
))
253 return s
->tm4
[tm
].control
;
257 qemu_log_mask(LOG_UNIMP
,
258 "%s: unknown register 0x%02" HWADDR_PRIx
"\n",
262 qemu_log_mask(LOG_GUEST_ERROR
,
263 "%s: incorrect register 0x%02" HWADDR_PRIx
"\n",
270 static void pxa2xx_timer_write(void *opaque
, hwaddr offset
,
271 uint64_t value
, unsigned size
)
274 PXA2xxTimerInfo
*s
= (PXA2xxTimerInfo
*) opaque
;
284 s
->timer
[tm
].value
= value
;
285 pxa2xx_timer_update(s
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
));
302 if (!pxa2xx_timer_has_tm4(s
))
304 s
->tm4
[tm
].tm
.value
= value
;
305 pxa2xx_timer_update4(s
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tm
);
308 s
->oldclock
= s
->clock
;
309 s
->lastload
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
311 pxa2xx_timer_update(s
, s
->lastload
);
328 if (!pxa2xx_timer_has_tm4(s
))
330 s
->tm4
[tm
].oldclock
= s
->tm4
[tm
].clock
;
331 s
->tm4
[tm
].lastload
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
332 s
->tm4
[tm
].clock
= value
;
333 pxa2xx_timer_update4(s
, s
->tm4
[tm
].lastload
, tm
);
336 s
->irq_enabled
= value
& 0xfff;
338 case OSSR
: /* Status register */
341 for (i
= 0; i
< 4; i
++, value
>>= 1)
343 qemu_irq_lower(s
->timer
[i
].irq
);
344 if (pxa2xx_timer_has_tm4(s
) && !(s
->events
& 0xff0) && value
)
345 qemu_irq_lower(s
->irq4
);
347 case OWER
: /* XXX: Reset on OSMR3 match? */
357 if (!pxa2xx_timer_has_tm4(s
))
359 s
->tm4
[tm
].control
= value
& 0x0ff;
360 /* XXX Stop if running (shouldn't happen) */
361 if ((value
& (1 << 7)) || tm
== 0)
362 s
->tm4
[tm
].freq
= pxa2xx_timer4_freq
[value
& 7];
365 pxa2xx_timer_update4(s
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tm
);
375 if (!pxa2xx_timer_has_tm4(s
))
377 s
->tm4
[tm
].control
= value
& 0x3ff;
378 /* XXX Stop if running (shouldn't happen) */
379 if ((value
& (1 << 7)) || !(tm
& 1))
381 pxa2xx_timer4_freq
[(value
& (1 << 8)) ? 0 : (value
& 7)];
384 pxa2xx_timer_update4(s
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tm
);
388 qemu_log_mask(LOG_UNIMP
,
389 "%s: unknown register 0x%02" HWADDR_PRIx
" "
390 "(value 0x%08" PRIx64
")\n", __func__
, offset
, value
);
393 qemu_log_mask(LOG_GUEST_ERROR
,
394 "%s: incorrect register 0x%02" HWADDR_PRIx
" "
395 "(value 0x%08" PRIx64
")\n", __func__
, offset
, value
);
399 static const MemoryRegionOps pxa2xx_timer_ops
= {
400 .read
= pxa2xx_timer_read
,
401 .write
= pxa2xx_timer_write
,
402 .endianness
= DEVICE_NATIVE_ENDIAN
,
405 static void pxa2xx_timer_tick(void *opaque
)
407 PXA2xxTimer0
*t
= (PXA2xxTimer0
*) opaque
;
408 PXA2xxTimerInfo
*i
= t
->info
;
410 if (i
->irq_enabled
& (1 << t
->num
)) {
411 i
->events
|= 1 << t
->num
;
412 qemu_irq_raise(t
->irq
);
418 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET
);
422 static void pxa2xx_timer_tick4(void *opaque
)
424 PXA2xxTimer4
*t
= (PXA2xxTimer4
*) opaque
;
425 PXA2xxTimerInfo
*i
= (PXA2xxTimerInfo
*) t
->tm
.info
;
427 pxa2xx_timer_tick(&t
->tm
);
428 if (t
->control
& (1 << 3))
430 if (t
->control
& (1 << 6))
431 pxa2xx_timer_update4(i
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), t
->tm
.num
- 4);
432 if (i
->events
& 0xff0)
433 qemu_irq_raise(i
->irq4
);
436 static int pxa25x_timer_post_load(void *opaque
, int version_id
)
438 PXA2xxTimerInfo
*s
= (PXA2xxTimerInfo
*) opaque
;
442 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
443 pxa2xx_timer_update(s
, now
);
445 if (pxa2xx_timer_has_tm4(s
))
446 for (i
= 0; i
< 8; i
++)
447 pxa2xx_timer_update4(s
, now
, i
);
452 static void pxa2xx_timer_init(Object
*obj
)
454 PXA2xxTimerInfo
*s
= PXA2XX_TIMER(obj
);
455 SysBusDevice
*dev
= SYS_BUS_DEVICE(obj
);
460 s
->lastload
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
463 memory_region_init_io(&s
->iomem
, obj
, &pxa2xx_timer_ops
, s
,
464 "pxa2xx-timer", 0x00001000);
465 sysbus_init_mmio(dev
, &s
->iomem
);
468 static void pxa2xx_timer_realize(DeviceState
*dev
, Error
**errp
)
470 PXA2xxTimerInfo
*s
= PXA2XX_TIMER(dev
);
471 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
474 for (i
= 0; i
< 4; i
++) {
475 s
->timer
[i
].value
= 0;
476 sysbus_init_irq(sbd
, &s
->timer
[i
].irq
);
477 s
->timer
[i
].info
= s
;
479 s
->timer
[i
].qtimer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
,
480 pxa2xx_timer_tick
, &s
->timer
[i
]);
483 if (s
->flags
& (1 << PXA2XX_TIMER_HAVE_TM4
)) {
484 sysbus_init_irq(sbd
, &s
->irq4
);
486 for (i
= 0; i
< 8; i
++) {
487 s
->tm4
[i
].tm
.value
= 0;
488 s
->tm4
[i
].tm
.info
= s
;
489 s
->tm4
[i
].tm
.num
= i
+ 4;
491 s
->tm4
[i
].control
= 0x0;
492 s
->tm4
[i
].tm
.qtimer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
,
493 pxa2xx_timer_tick4
, &s
->tm4
[i
]);
498 static const VMStateDescription vmstate_pxa2xx_timer0_regs
= {
499 .name
= "pxa2xx_timer0",
501 .minimum_version_id
= 2,
502 .fields
= (VMStateField
[]) {
503 VMSTATE_UINT32(value
, PXA2xxTimer0
),
504 VMSTATE_END_OF_LIST(),
508 static const VMStateDescription vmstate_pxa2xx_timer4_regs
= {
509 .name
= "pxa2xx_timer4",
511 .minimum_version_id
= 1,
512 .fields
= (VMStateField
[]) {
513 VMSTATE_STRUCT(tm
, PXA2xxTimer4
, 1,
514 vmstate_pxa2xx_timer0_regs
, PXA2xxTimer0
),
515 VMSTATE_INT32(oldclock
, PXA2xxTimer4
),
516 VMSTATE_INT32(clock
, PXA2xxTimer4
),
517 VMSTATE_UINT64(lastload
, PXA2xxTimer4
),
518 VMSTATE_UINT32(freq
, PXA2xxTimer4
),
519 VMSTATE_UINT32(control
, PXA2xxTimer4
),
520 VMSTATE_END_OF_LIST(),
524 static bool pxa2xx_timer_has_tm4_test(void *opaque
, int version_id
)
526 return pxa2xx_timer_has_tm4(opaque
);
529 static const VMStateDescription vmstate_pxa2xx_timer_regs
= {
530 .name
= "pxa2xx_timer",
532 .minimum_version_id
= 1,
533 .post_load
= pxa25x_timer_post_load
,
534 .fields
= (VMStateField
[]) {
535 VMSTATE_INT32(clock
, PXA2xxTimerInfo
),
536 VMSTATE_INT32(oldclock
, PXA2xxTimerInfo
),
537 VMSTATE_UINT64(lastload
, PXA2xxTimerInfo
),
538 VMSTATE_STRUCT_ARRAY(timer
, PXA2xxTimerInfo
, 4, 1,
539 vmstate_pxa2xx_timer0_regs
, PXA2xxTimer0
),
540 VMSTATE_UINT32(events
, PXA2xxTimerInfo
),
541 VMSTATE_UINT32(irq_enabled
, PXA2xxTimerInfo
),
542 VMSTATE_UINT32(reset3
, PXA2xxTimerInfo
),
543 VMSTATE_UINT32(snapshot
, PXA2xxTimerInfo
),
544 VMSTATE_STRUCT_ARRAY_TEST(tm4
, PXA2xxTimerInfo
, 8,
545 pxa2xx_timer_has_tm4_test
, 0,
546 vmstate_pxa2xx_timer4_regs
, PXA2xxTimer4
),
547 VMSTATE_END_OF_LIST(),
551 static Property pxa25x_timer_dev_properties
[] = {
552 DEFINE_PROP_UINT32("freq", PXA2xxTimerInfo
, freq
, PXA25X_FREQ
),
553 DEFINE_PROP_BIT("tm4", PXA2xxTimerInfo
, flags
,
554 PXA2XX_TIMER_HAVE_TM4
, false),
555 DEFINE_PROP_END_OF_LIST(),
558 static void pxa25x_timer_dev_class_init(ObjectClass
*klass
, void *data
)
560 DeviceClass
*dc
= DEVICE_CLASS(klass
);
562 dc
->desc
= "PXA25x timer";
563 dc
->props
= pxa25x_timer_dev_properties
;
566 static const TypeInfo pxa25x_timer_dev_info
= {
567 .name
= "pxa25x-timer",
568 .parent
= TYPE_PXA2XX_TIMER
,
569 .instance_size
= sizeof(PXA2xxTimerInfo
),
570 .class_init
= pxa25x_timer_dev_class_init
,
573 static Property pxa27x_timer_dev_properties
[] = {
574 DEFINE_PROP_UINT32("freq", PXA2xxTimerInfo
, freq
, PXA27X_FREQ
),
575 DEFINE_PROP_BIT("tm4", PXA2xxTimerInfo
, flags
,
576 PXA2XX_TIMER_HAVE_TM4
, true),
577 DEFINE_PROP_END_OF_LIST(),
580 static void pxa27x_timer_dev_class_init(ObjectClass
*klass
, void *data
)
582 DeviceClass
*dc
= DEVICE_CLASS(klass
);
584 dc
->desc
= "PXA27x timer";
585 dc
->props
= pxa27x_timer_dev_properties
;
588 static const TypeInfo pxa27x_timer_dev_info
= {
589 .name
= "pxa27x-timer",
590 .parent
= TYPE_PXA2XX_TIMER
,
591 .instance_size
= sizeof(PXA2xxTimerInfo
),
592 .class_init
= pxa27x_timer_dev_class_init
,
595 static void pxa2xx_timer_class_init(ObjectClass
*oc
, void *data
)
597 DeviceClass
*dc
= DEVICE_CLASS(oc
);
599 dc
->realize
= pxa2xx_timer_realize
;
600 dc
->vmsd
= &vmstate_pxa2xx_timer_regs
;
603 static const TypeInfo pxa2xx_timer_type_info
= {
604 .name
= TYPE_PXA2XX_TIMER
,
605 .parent
= TYPE_SYS_BUS_DEVICE
,
606 .instance_size
= sizeof(PXA2xxTimerInfo
),
607 .instance_init
= pxa2xx_timer_init
,
609 .class_init
= pxa2xx_timer_class_init
,
612 static void pxa2xx_timer_register_types(void)
614 type_register_static(&pxa2xx_timer_type_info
);
615 type_register_static(&pxa25x_timer_dev_info
);
616 type_register_static(&pxa27x_timer_dev_info
);
619 type_init(pxa2xx_timer_register_types
)