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"
30 #define OSCR 0x10 /* OS Timer Count */
39 #define OSSR 0x14 /* Timer status register */
41 #define OIER 0x1c /* Interrupt enable register 3-0 to E3-E0 */
42 #define OMCR4 0xc0 /* OS Match Control registers */
52 #define PXA25X_FREQ 3686400 /* 3.6864 MHz */
53 #define PXA27X_FREQ 3250000 /* 3.25 MHz */
55 static int pxa2xx_timer4_freq
[8] = {
61 /* [5] is the "Externally supplied clock". Assign if necessary. */
65 #define TYPE_PXA2XX_TIMER "pxa2xx-timer"
66 #define PXA2XX_TIMER(obj) \
67 OBJECT_CHECK(PXA2xxTimerInfo, (obj), TYPE_PXA2XX_TIMER)
69 typedef struct PXA2xxTimerInfo PXA2xxTimerInfo
;
76 PXA2xxTimerInfo
*info
;
88 struct PXA2xxTimerInfo
{
89 SysBusDevice parent_obj
;
98 PXA2xxTimer0 timer
[4];
100 uint32_t irq_enabled
;
108 #define PXA2XX_TIMER_HAVE_TM4 0
110 static inline int pxa2xx_timer_has_tm4(PXA2xxTimerInfo
*s
)
112 return s
->flags
& (1 << PXA2XX_TIMER_HAVE_TM4
);
115 static void pxa2xx_timer_update(void *opaque
, uint64_t now_qemu
)
117 PXA2xxTimerInfo
*s
= (PXA2xxTimerInfo
*) opaque
;
123 muldiv64(now_qemu
- s
->lastload
, s
->freq
, NANOSECONDS_PER_SECOND
);
125 for (i
= 0; i
< 4; i
++) {
126 new_qemu
= now_qemu
+ muldiv64((uint32_t) (s
->timer
[i
].value
- now_vm
),
127 NANOSECONDS_PER_SECOND
, s
->freq
);
128 timer_mod(s
->timer
[i
].qtimer
, new_qemu
);
132 static void pxa2xx_timer_update4(void *opaque
, uint64_t now_qemu
, int n
)
134 PXA2xxTimerInfo
*s
= (PXA2xxTimerInfo
*) opaque
;
137 static const int counters
[8] = { 0, 0, 0, 0, 4, 4, 6, 6 };
140 if (s
->tm4
[n
].control
& (1 << 7))
143 counter
= counters
[n
];
145 if (!s
->tm4
[counter
].freq
) {
146 timer_del(s
->tm4
[n
].tm
.qtimer
);
150 now_vm
= s
->tm4
[counter
].clock
+ muldiv64(now_qemu
-
151 s
->tm4
[counter
].lastload
,
152 s
->tm4
[counter
].freq
, NANOSECONDS_PER_SECOND
);
154 new_qemu
= now_qemu
+ muldiv64((uint32_t) (s
->tm4
[n
].tm
.value
- now_vm
),
155 NANOSECONDS_PER_SECOND
, s
->tm4
[counter
].freq
);
156 timer_mod(s
->tm4
[n
].tm
.qtimer
, new_qemu
);
159 static uint64_t pxa2xx_timer_read(void *opaque
, hwaddr offset
,
162 PXA2xxTimerInfo
*s
= (PXA2xxTimerInfo
*) opaque
;
173 return s
->timer
[tm
].value
;
189 if (!pxa2xx_timer_has_tm4(s
))
191 return s
->tm4
[tm
].tm
.value
;
193 return s
->clock
+ muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) -
194 s
->lastload
, s
->freq
, NANOSECONDS_PER_SECOND
);
210 if (!pxa2xx_timer_has_tm4(s
))
213 if ((tm
== 9 - 4 || tm
== 11 - 4) && (s
->tm4
[tm
].control
& (1 << 9))) {
214 if (s
->tm4
[tm
- 1].freq
)
215 s
->snapshot
= s
->tm4
[tm
- 1].clock
+ muldiv64(
216 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) -
217 s
->tm4
[tm
- 1].lastload
,
218 s
->tm4
[tm
- 1].freq
, NANOSECONDS_PER_SECOND
);
220 s
->snapshot
= s
->tm4
[tm
- 1].clock
;
223 if (!s
->tm4
[tm
].freq
)
224 return s
->tm4
[tm
].clock
;
225 return s
->tm4
[tm
].clock
+
226 muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) -
227 s
->tm4
[tm
].lastload
, s
->tm4
[tm
].freq
,
228 NANOSECONDS_PER_SECOND
);
230 return s
->irq_enabled
;
231 case OSSR
: /* Status register */
250 if (!pxa2xx_timer_has_tm4(s
))
252 return s
->tm4
[tm
].control
;
256 qemu_log_mask(LOG_UNIMP
,
257 "%s: unknown register 0x%02" HWADDR_PRIx
"\n",
261 qemu_log_mask(LOG_GUEST_ERROR
,
262 "%s: incorrect register 0x%02" HWADDR_PRIx
"\n",
269 static void pxa2xx_timer_write(void *opaque
, hwaddr offset
,
270 uint64_t value
, unsigned size
)
273 PXA2xxTimerInfo
*s
= (PXA2xxTimerInfo
*) opaque
;
283 s
->timer
[tm
].value
= value
;
284 pxa2xx_timer_update(s
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
));
301 if (!pxa2xx_timer_has_tm4(s
))
303 s
->tm4
[tm
].tm
.value
= value
;
304 pxa2xx_timer_update4(s
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tm
);
307 s
->oldclock
= s
->clock
;
308 s
->lastload
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
310 pxa2xx_timer_update(s
, s
->lastload
);
327 if (!pxa2xx_timer_has_tm4(s
))
329 s
->tm4
[tm
].oldclock
= s
->tm4
[tm
].clock
;
330 s
->tm4
[tm
].lastload
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
331 s
->tm4
[tm
].clock
= value
;
332 pxa2xx_timer_update4(s
, s
->tm4
[tm
].lastload
, tm
);
335 s
->irq_enabled
= value
& 0xfff;
337 case OSSR
: /* Status register */
340 for (i
= 0; i
< 4; i
++, value
>>= 1)
342 qemu_irq_lower(s
->timer
[i
].irq
);
343 if (pxa2xx_timer_has_tm4(s
) && !(s
->events
& 0xff0) && value
)
344 qemu_irq_lower(s
->irq4
);
346 case OWER
: /* XXX: Reset on OSMR3 match? */
356 if (!pxa2xx_timer_has_tm4(s
))
358 s
->tm4
[tm
].control
= value
& 0x0ff;
359 /* XXX Stop if running (shouldn't happen) */
360 if ((value
& (1 << 7)) || tm
== 0)
361 s
->tm4
[tm
].freq
= pxa2xx_timer4_freq
[value
& 7];
364 pxa2xx_timer_update4(s
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tm
);
374 if (!pxa2xx_timer_has_tm4(s
))
376 s
->tm4
[tm
].control
= value
& 0x3ff;
377 /* XXX Stop if running (shouldn't happen) */
378 if ((value
& (1 << 7)) || !(tm
& 1))
380 pxa2xx_timer4_freq
[(value
& (1 << 8)) ? 0 : (value
& 7)];
383 pxa2xx_timer_update4(s
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tm
);
387 qemu_log_mask(LOG_UNIMP
,
388 "%s: unknown register 0x%02" HWADDR_PRIx
" "
389 "(value 0x%08" PRIx64
")\n", __func__
, offset
, value
);
392 qemu_log_mask(LOG_GUEST_ERROR
,
393 "%s: incorrect register 0x%02" HWADDR_PRIx
" "
394 "(value 0x%08" PRIx64
")\n", __func__
, offset
, value
);
398 static const MemoryRegionOps pxa2xx_timer_ops
= {
399 .read
= pxa2xx_timer_read
,
400 .write
= pxa2xx_timer_write
,
401 .endianness
= DEVICE_NATIVE_ENDIAN
,
404 static void pxa2xx_timer_tick(void *opaque
)
406 PXA2xxTimer0
*t
= (PXA2xxTimer0
*) opaque
;
407 PXA2xxTimerInfo
*i
= t
->info
;
409 if (i
->irq_enabled
& (1 << t
->num
)) {
410 i
->events
|= 1 << t
->num
;
411 qemu_irq_raise(t
->irq
);
417 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET
);
421 static void pxa2xx_timer_tick4(void *opaque
)
423 PXA2xxTimer4
*t
= (PXA2xxTimer4
*) opaque
;
424 PXA2xxTimerInfo
*i
= (PXA2xxTimerInfo
*) t
->tm
.info
;
426 pxa2xx_timer_tick(&t
->tm
);
427 if (t
->control
& (1 << 3))
429 if (t
->control
& (1 << 6))
430 pxa2xx_timer_update4(i
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), t
->tm
.num
- 4);
431 if (i
->events
& 0xff0)
432 qemu_irq_raise(i
->irq4
);
435 static int pxa25x_timer_post_load(void *opaque
, int version_id
)
437 PXA2xxTimerInfo
*s
= (PXA2xxTimerInfo
*) opaque
;
441 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
442 pxa2xx_timer_update(s
, now
);
444 if (pxa2xx_timer_has_tm4(s
))
445 for (i
= 0; i
< 8; i
++)
446 pxa2xx_timer_update4(s
, now
, i
);
451 static void pxa2xx_timer_init(Object
*obj
)
453 PXA2xxTimerInfo
*s
= PXA2XX_TIMER(obj
);
454 SysBusDevice
*dev
= SYS_BUS_DEVICE(obj
);
459 s
->lastload
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
462 memory_region_init_io(&s
->iomem
, obj
, &pxa2xx_timer_ops
, s
,
463 "pxa2xx-timer", 0x00001000);
464 sysbus_init_mmio(dev
, &s
->iomem
);
467 static void pxa2xx_timer_realize(DeviceState
*dev
, Error
**errp
)
469 PXA2xxTimerInfo
*s
= PXA2XX_TIMER(dev
);
470 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
473 for (i
= 0; i
< 4; i
++) {
474 s
->timer
[i
].value
= 0;
475 sysbus_init_irq(sbd
, &s
->timer
[i
].irq
);
476 s
->timer
[i
].info
= s
;
478 s
->timer
[i
].qtimer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
,
479 pxa2xx_timer_tick
, &s
->timer
[i
]);
482 if (s
->flags
& (1 << PXA2XX_TIMER_HAVE_TM4
)) {
483 sysbus_init_irq(sbd
, &s
->irq4
);
485 for (i
= 0; i
< 8; i
++) {
486 s
->tm4
[i
].tm
.value
= 0;
487 s
->tm4
[i
].tm
.info
= s
;
488 s
->tm4
[i
].tm
.num
= i
+ 4;
490 s
->tm4
[i
].control
= 0x0;
491 s
->tm4
[i
].tm
.qtimer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
,
492 pxa2xx_timer_tick4
, &s
->tm4
[i
]);
497 static const VMStateDescription vmstate_pxa2xx_timer0_regs
= {
498 .name
= "pxa2xx_timer0",
500 .minimum_version_id
= 2,
501 .fields
= (VMStateField
[]) {
502 VMSTATE_UINT32(value
, PXA2xxTimer0
),
503 VMSTATE_END_OF_LIST(),
507 static const VMStateDescription vmstate_pxa2xx_timer4_regs
= {
508 .name
= "pxa2xx_timer4",
510 .minimum_version_id
= 1,
511 .fields
= (VMStateField
[]) {
512 VMSTATE_STRUCT(tm
, PXA2xxTimer4
, 1,
513 vmstate_pxa2xx_timer0_regs
, PXA2xxTimer0
),
514 VMSTATE_INT32(oldclock
, PXA2xxTimer4
),
515 VMSTATE_INT32(clock
, PXA2xxTimer4
),
516 VMSTATE_UINT64(lastload
, PXA2xxTimer4
),
517 VMSTATE_UINT32(freq
, PXA2xxTimer4
),
518 VMSTATE_UINT32(control
, PXA2xxTimer4
),
519 VMSTATE_END_OF_LIST(),
523 static bool pxa2xx_timer_has_tm4_test(void *opaque
, int version_id
)
525 return pxa2xx_timer_has_tm4(opaque
);
528 static const VMStateDescription vmstate_pxa2xx_timer_regs
= {
529 .name
= "pxa2xx_timer",
531 .minimum_version_id
= 1,
532 .post_load
= pxa25x_timer_post_load
,
533 .fields
= (VMStateField
[]) {
534 VMSTATE_INT32(clock
, PXA2xxTimerInfo
),
535 VMSTATE_INT32(oldclock
, PXA2xxTimerInfo
),
536 VMSTATE_UINT64(lastload
, PXA2xxTimerInfo
),
537 VMSTATE_STRUCT_ARRAY(timer
, PXA2xxTimerInfo
, 4, 1,
538 vmstate_pxa2xx_timer0_regs
, PXA2xxTimer0
),
539 VMSTATE_UINT32(events
, PXA2xxTimerInfo
),
540 VMSTATE_UINT32(irq_enabled
, PXA2xxTimerInfo
),
541 VMSTATE_UINT32(reset3
, PXA2xxTimerInfo
),
542 VMSTATE_UINT32(snapshot
, PXA2xxTimerInfo
),
543 VMSTATE_STRUCT_ARRAY_TEST(tm4
, PXA2xxTimerInfo
, 8,
544 pxa2xx_timer_has_tm4_test
, 0,
545 vmstate_pxa2xx_timer4_regs
, PXA2xxTimer4
),
546 VMSTATE_END_OF_LIST(),
550 static Property pxa25x_timer_dev_properties
[] = {
551 DEFINE_PROP_UINT32("freq", PXA2xxTimerInfo
, freq
, PXA25X_FREQ
),
552 DEFINE_PROP_BIT("tm4", PXA2xxTimerInfo
, flags
,
553 PXA2XX_TIMER_HAVE_TM4
, false),
554 DEFINE_PROP_END_OF_LIST(),
557 static void pxa25x_timer_dev_class_init(ObjectClass
*klass
, void *data
)
559 DeviceClass
*dc
= DEVICE_CLASS(klass
);
561 dc
->desc
= "PXA25x timer";
562 dc
->props
= pxa25x_timer_dev_properties
;
565 static const TypeInfo pxa25x_timer_dev_info
= {
566 .name
= "pxa25x-timer",
567 .parent
= TYPE_PXA2XX_TIMER
,
568 .instance_size
= sizeof(PXA2xxTimerInfo
),
569 .class_init
= pxa25x_timer_dev_class_init
,
572 static Property pxa27x_timer_dev_properties
[] = {
573 DEFINE_PROP_UINT32("freq", PXA2xxTimerInfo
, freq
, PXA27X_FREQ
),
574 DEFINE_PROP_BIT("tm4", PXA2xxTimerInfo
, flags
,
575 PXA2XX_TIMER_HAVE_TM4
, true),
576 DEFINE_PROP_END_OF_LIST(),
579 static void pxa27x_timer_dev_class_init(ObjectClass
*klass
, void *data
)
581 DeviceClass
*dc
= DEVICE_CLASS(klass
);
583 dc
->desc
= "PXA27x timer";
584 dc
->props
= pxa27x_timer_dev_properties
;
587 static const TypeInfo pxa27x_timer_dev_info
= {
588 .name
= "pxa27x-timer",
589 .parent
= TYPE_PXA2XX_TIMER
,
590 .instance_size
= sizeof(PXA2xxTimerInfo
),
591 .class_init
= pxa27x_timer_dev_class_init
,
594 static void pxa2xx_timer_class_init(ObjectClass
*oc
, void *data
)
596 DeviceClass
*dc
= DEVICE_CLASS(oc
);
598 dc
->realize
= pxa2xx_timer_realize
;
599 dc
->vmsd
= &vmstate_pxa2xx_timer_regs
;
602 static const TypeInfo pxa2xx_timer_type_info
= {
603 .name
= TYPE_PXA2XX_TIMER
,
604 .parent
= TYPE_SYS_BUS_DEVICE
,
605 .instance_size
= sizeof(PXA2xxTimerInfo
),
606 .instance_init
= pxa2xx_timer_init
,
608 .class_init
= pxa2xx_timer_class_init
,
611 static void pxa2xx_timer_register_types(void)
613 type_register_static(&pxa2xx_timer_type_info
);
614 type_register_static(&pxa25x_timer_dev_info
);
615 type_register_static(&pxa27x_timer_dev_info
);
618 type_init(pxa2xx_timer_register_types
)