2 * ARM PrimeCell Timer modules.
4 * Copyright (c) 2005-2006 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licensed under the GPL.
10 #include "qemu/osdep.h"
11 #include "hw/sysbus.h"
12 #include "migration/vmstate.h"
13 #include "qemu/timer.h"
15 #include "hw/ptimer.h"
16 #include "hw/qdev-properties.h"
17 #include "qemu/main-loop.h"
18 #include "qemu/module.h"
21 /* Common timer implementation. */
23 #define TIMER_CTRL_ONESHOT (1 << 0)
24 #define TIMER_CTRL_32BIT (1 << 1)
25 #define TIMER_CTRL_DIV1 (0 << 2)
26 #define TIMER_CTRL_DIV16 (1 << 2)
27 #define TIMER_CTRL_DIV256 (2 << 2)
28 #define TIMER_CTRL_IE (1 << 5)
29 #define TIMER_CTRL_PERIODIC (1 << 6)
30 #define TIMER_CTRL_ENABLE (1 << 7)
41 /* Check all active timers, and schedule the next timer interrupt. */
43 static void arm_timer_update(arm_timer_state
*s
)
45 /* Update interrupts. */
46 if (s
->int_level
&& (s
->control
& TIMER_CTRL_IE
)) {
47 qemu_irq_raise(s
->irq
);
49 qemu_irq_lower(s
->irq
);
53 static uint32_t arm_timer_read(void *opaque
, hwaddr offset
)
55 arm_timer_state
*s
= (arm_timer_state
*)opaque
;
57 switch (offset
>> 2) {
58 case 0: /* TimerLoad */
59 case 6: /* TimerBGLoad */
61 case 1: /* TimerValue */
62 return ptimer_get_count(s
->timer
);
63 case 2: /* TimerControl */
65 case 4: /* TimerRIS */
67 case 5: /* TimerMIS */
68 if ((s
->control
& TIMER_CTRL_IE
) == 0)
72 qemu_log_mask(LOG_GUEST_ERROR
,
73 "%s: Bad offset %x\n", __func__
, (int)offset
);
78 /* Reset the timer limit after settings have changed. */
79 static void arm_timer_recalibrate(arm_timer_state
*s
, int reload
)
83 if ((s
->control
& (TIMER_CTRL_PERIODIC
| TIMER_CTRL_ONESHOT
)) == 0) {
85 if (s
->control
& TIMER_CTRL_32BIT
)
93 ptimer_set_limit(s
->timer
, limit
, reload
);
96 static void arm_timer_write(void *opaque
, hwaddr offset
,
99 arm_timer_state
*s
= (arm_timer_state
*)opaque
;
102 switch (offset
>> 2) {
103 case 0: /* TimerLoad */
105 arm_timer_recalibrate(s
, 1);
107 case 1: /* TimerValue */
108 /* ??? Linux seems to want to write to this readonly register.
111 case 2: /* TimerControl */
112 if (s
->control
& TIMER_CTRL_ENABLE
) {
113 /* Pause the timer if it is running. This may cause some
114 inaccuracy dure to rounding, but avoids a whole lot of other
116 ptimer_stop(s
->timer
);
120 /* ??? Need to recalculate expiry time after changing divisor. */
121 switch ((value
>> 2) & 3) {
122 case 1: freq
>>= 4; break;
123 case 2: freq
>>= 8; break;
125 arm_timer_recalibrate(s
, s
->control
& TIMER_CTRL_ENABLE
);
126 ptimer_set_freq(s
->timer
, freq
);
127 if (s
->control
& TIMER_CTRL_ENABLE
) {
128 /* Restart the timer if still enabled. */
129 ptimer_run(s
->timer
, (s
->control
& TIMER_CTRL_ONESHOT
) != 0);
132 case 3: /* TimerIntClr */
135 case 6: /* TimerBGLoad */
137 arm_timer_recalibrate(s
, 0);
140 qemu_log_mask(LOG_GUEST_ERROR
,
141 "%s: Bad offset %x\n", __func__
, (int)offset
);
146 static void arm_timer_tick(void *opaque
)
148 arm_timer_state
*s
= (arm_timer_state
*)opaque
;
153 static const VMStateDescription vmstate_arm_timer
= {
156 .minimum_version_id
= 1,
157 .fields
= (VMStateField
[]) {
158 VMSTATE_UINT32(control
, arm_timer_state
),
159 VMSTATE_UINT32(limit
, arm_timer_state
),
160 VMSTATE_INT32(int_level
, arm_timer_state
),
161 VMSTATE_PTIMER(timer
, arm_timer_state
),
162 VMSTATE_END_OF_LIST()
166 static arm_timer_state
*arm_timer_init(uint32_t freq
)
171 s
= (arm_timer_state
*)g_malloc0(sizeof(arm_timer_state
));
173 s
->control
= TIMER_CTRL_IE
;
175 bh
= qemu_bh_new(arm_timer_tick
, s
);
176 s
->timer
= ptimer_init(bh
, PTIMER_POLICY_DEFAULT
);
177 vmstate_register(NULL
, -1, &vmstate_arm_timer
, s
);
181 /* ARM PrimeCell SP804 dual timer module.
183 * http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0271d/index.html
186 #define TYPE_SP804 "sp804"
187 #define SP804(obj) OBJECT_CHECK(SP804State, (obj), TYPE_SP804)
189 typedef struct SP804State
{
190 SysBusDevice parent_obj
;
193 arm_timer_state
*timer
[2];
194 uint32_t freq0
, freq1
;
199 static const uint8_t sp804_ids
[] = {
203 0xd, 0xf0, 0x05, 0xb1
206 /* Merge the IRQs from the two component devices. */
207 static void sp804_set_irq(void *opaque
, int irq
, int level
)
209 SP804State
*s
= (SP804State
*)opaque
;
211 s
->level
[irq
] = level
;
212 qemu_set_irq(s
->irq
, s
->level
[0] || s
->level
[1]);
215 static uint64_t sp804_read(void *opaque
, hwaddr offset
,
218 SP804State
*s
= (SP804State
*)opaque
;
221 return arm_timer_read(s
->timer
[0], offset
);
224 return arm_timer_read(s
->timer
[1], offset
- 0x20);
228 if (offset
>= 0xfe0 && offset
<= 0xffc) {
229 return sp804_ids
[(offset
- 0xfe0) >> 2];
233 /* Integration Test control registers, which we won't support */
234 case 0xf00: /* TimerITCR */
235 case 0xf04: /* TimerITOP (strictly write only but..) */
236 qemu_log_mask(LOG_UNIMP
,
237 "%s: integration test registers unimplemented\n",
242 qemu_log_mask(LOG_GUEST_ERROR
,
243 "%s: Bad offset %x\n", __func__
, (int)offset
);
247 static void sp804_write(void *opaque
, hwaddr offset
,
248 uint64_t value
, unsigned size
)
250 SP804State
*s
= (SP804State
*)opaque
;
253 arm_timer_write(s
->timer
[0], offset
, value
);
258 arm_timer_write(s
->timer
[1], offset
- 0x20, value
);
262 /* Technically we could be writing to the Test Registers, but not likely */
263 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad offset %x\n",
264 __func__
, (int)offset
);
267 static const MemoryRegionOps sp804_ops
= {
269 .write
= sp804_write
,
270 .endianness
= DEVICE_NATIVE_ENDIAN
,
273 static const VMStateDescription vmstate_sp804
= {
276 .minimum_version_id
= 1,
277 .fields
= (VMStateField
[]) {
278 VMSTATE_INT32_ARRAY(level
, SP804State
, 2),
279 VMSTATE_END_OF_LIST()
283 static void sp804_init(Object
*obj
)
285 SP804State
*s
= SP804(obj
);
286 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
288 sysbus_init_irq(sbd
, &s
->irq
);
289 memory_region_init_io(&s
->iomem
, obj
, &sp804_ops
, s
,
291 sysbus_init_mmio(sbd
, &s
->iomem
);
294 static void sp804_realize(DeviceState
*dev
, Error
**errp
)
296 SP804State
*s
= SP804(dev
);
298 s
->timer
[0] = arm_timer_init(s
->freq0
);
299 s
->timer
[1] = arm_timer_init(s
->freq1
);
300 s
->timer
[0]->irq
= qemu_allocate_irq(sp804_set_irq
, s
, 0);
301 s
->timer
[1]->irq
= qemu_allocate_irq(sp804_set_irq
, s
, 1);
304 /* Integrator/CP timer module. */
306 #define TYPE_INTEGRATOR_PIT "integrator_pit"
307 #define INTEGRATOR_PIT(obj) \
308 OBJECT_CHECK(icp_pit_state, (obj), TYPE_INTEGRATOR_PIT)
311 SysBusDevice parent_obj
;
314 arm_timer_state
*timer
[3];
317 static uint64_t icp_pit_read(void *opaque
, hwaddr offset
,
320 icp_pit_state
*s
= (icp_pit_state
*)opaque
;
323 /* ??? Don't know the PrimeCell ID for this device. */
326 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad timer %d\n", __func__
, n
);
330 return arm_timer_read(s
->timer
[n
], offset
& 0xff);
333 static void icp_pit_write(void *opaque
, hwaddr offset
,
334 uint64_t value
, unsigned size
)
336 icp_pit_state
*s
= (icp_pit_state
*)opaque
;
341 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad timer %d\n", __func__
, n
);
345 arm_timer_write(s
->timer
[n
], offset
& 0xff, value
);
348 static const MemoryRegionOps icp_pit_ops
= {
349 .read
= icp_pit_read
,
350 .write
= icp_pit_write
,
351 .endianness
= DEVICE_NATIVE_ENDIAN
,
354 static void icp_pit_init(Object
*obj
)
356 icp_pit_state
*s
= INTEGRATOR_PIT(obj
);
357 SysBusDevice
*dev
= SYS_BUS_DEVICE(obj
);
359 /* Timer 0 runs at the system clock speed (40MHz). */
360 s
->timer
[0] = arm_timer_init(40000000);
361 /* The other two timers run at 1MHz. */
362 s
->timer
[1] = arm_timer_init(1000000);
363 s
->timer
[2] = arm_timer_init(1000000);
365 sysbus_init_irq(dev
, &s
->timer
[0]->irq
);
366 sysbus_init_irq(dev
, &s
->timer
[1]->irq
);
367 sysbus_init_irq(dev
, &s
->timer
[2]->irq
);
369 memory_region_init_io(&s
->iomem
, obj
, &icp_pit_ops
, s
,
371 sysbus_init_mmio(dev
, &s
->iomem
);
372 /* This device has no state to save/restore. The component timers will
376 static const TypeInfo icp_pit_info
= {
377 .name
= TYPE_INTEGRATOR_PIT
,
378 .parent
= TYPE_SYS_BUS_DEVICE
,
379 .instance_size
= sizeof(icp_pit_state
),
380 .instance_init
= icp_pit_init
,
383 static Property sp804_properties
[] = {
384 DEFINE_PROP_UINT32("freq0", SP804State
, freq0
, 1000000),
385 DEFINE_PROP_UINT32("freq1", SP804State
, freq1
, 1000000),
386 DEFINE_PROP_END_OF_LIST(),
389 static void sp804_class_init(ObjectClass
*klass
, void *data
)
391 DeviceClass
*k
= DEVICE_CLASS(klass
);
393 k
->realize
= sp804_realize
;
394 k
->props
= sp804_properties
;
395 k
->vmsd
= &vmstate_sp804
;
398 static const TypeInfo sp804_info
= {
400 .parent
= TYPE_SYS_BUS_DEVICE
,
401 .instance_size
= sizeof(SP804State
),
402 .instance_init
= sp804_init
,
403 .class_init
= sp804_class_init
,
406 static void arm_timer_register_types(void)
408 type_register_static(&icp_pit_info
);
409 type_register_static(&sp804_info
);
412 type_init(arm_timer_register_types
)