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/module.h"
20 /* Common timer implementation. */
22 #define TIMER_CTRL_ONESHOT (1 << 0)
23 #define TIMER_CTRL_32BIT (1 << 1)
24 #define TIMER_CTRL_DIV1 (0 << 2)
25 #define TIMER_CTRL_DIV16 (1 << 2)
26 #define TIMER_CTRL_DIV256 (2 << 2)
27 #define TIMER_CTRL_IE (1 << 5)
28 #define TIMER_CTRL_PERIODIC (1 << 6)
29 #define TIMER_CTRL_ENABLE (1 << 7)
40 /* Check all active timers, and schedule the next timer interrupt. */
42 static void arm_timer_update(arm_timer_state
*s
)
44 /* Update interrupts. */
45 if (s
->int_level
&& (s
->control
& TIMER_CTRL_IE
)) {
46 qemu_irq_raise(s
->irq
);
48 qemu_irq_lower(s
->irq
);
52 static uint32_t arm_timer_read(void *opaque
, hwaddr offset
)
54 arm_timer_state
*s
= (arm_timer_state
*)opaque
;
56 switch (offset
>> 2) {
57 case 0: /* TimerLoad */
58 case 6: /* TimerBGLoad */
60 case 1: /* TimerValue */
61 return ptimer_get_count(s
->timer
);
62 case 2: /* TimerControl */
64 case 4: /* TimerRIS */
66 case 5: /* TimerMIS */
67 if ((s
->control
& TIMER_CTRL_IE
) == 0)
71 qemu_log_mask(LOG_GUEST_ERROR
,
72 "%s: Bad offset %x\n", __func__
, (int)offset
);
78 * Reset the timer limit after settings have changed.
79 * May only be called from inside a ptimer transaction block.
81 static void arm_timer_recalibrate(arm_timer_state
*s
, int reload
)
85 if ((s
->control
& (TIMER_CTRL_PERIODIC
| TIMER_CTRL_ONESHOT
)) == 0) {
87 if (s
->control
& TIMER_CTRL_32BIT
)
95 ptimer_set_limit(s
->timer
, limit
, reload
);
98 static void arm_timer_write(void *opaque
, hwaddr offset
,
101 arm_timer_state
*s
= (arm_timer_state
*)opaque
;
104 switch (offset
>> 2) {
105 case 0: /* TimerLoad */
107 ptimer_transaction_begin(s
->timer
);
108 arm_timer_recalibrate(s
, 1);
109 ptimer_transaction_commit(s
->timer
);
111 case 1: /* TimerValue */
112 /* ??? Linux seems to want to write to this readonly register.
115 case 2: /* TimerControl */
116 ptimer_transaction_begin(s
->timer
);
117 if (s
->control
& TIMER_CTRL_ENABLE
) {
118 /* Pause the timer if it is running. This may cause some
119 inaccuracy dure to rounding, but avoids a whole lot of other
121 ptimer_stop(s
->timer
);
125 /* ??? Need to recalculate expiry time after changing divisor. */
126 switch ((value
>> 2) & 3) {
127 case 1: freq
>>= 4; break;
128 case 2: freq
>>= 8; break;
130 arm_timer_recalibrate(s
, s
->control
& TIMER_CTRL_ENABLE
);
131 ptimer_set_freq(s
->timer
, freq
);
132 if (s
->control
& TIMER_CTRL_ENABLE
) {
133 /* Restart the timer if still enabled. */
134 ptimer_run(s
->timer
, (s
->control
& TIMER_CTRL_ONESHOT
) != 0);
136 ptimer_transaction_commit(s
->timer
);
138 case 3: /* TimerIntClr */
141 case 6: /* TimerBGLoad */
143 ptimer_transaction_begin(s
->timer
);
144 arm_timer_recalibrate(s
, 0);
145 ptimer_transaction_commit(s
->timer
);
148 qemu_log_mask(LOG_GUEST_ERROR
,
149 "%s: Bad offset %x\n", __func__
, (int)offset
);
154 static void arm_timer_tick(void *opaque
)
156 arm_timer_state
*s
= (arm_timer_state
*)opaque
;
161 static const VMStateDescription vmstate_arm_timer
= {
164 .minimum_version_id
= 1,
165 .fields
= (VMStateField
[]) {
166 VMSTATE_UINT32(control
, arm_timer_state
),
167 VMSTATE_UINT32(limit
, arm_timer_state
),
168 VMSTATE_INT32(int_level
, arm_timer_state
),
169 VMSTATE_PTIMER(timer
, arm_timer_state
),
170 VMSTATE_END_OF_LIST()
174 static arm_timer_state
*arm_timer_init(uint32_t freq
)
178 s
= (arm_timer_state
*)g_malloc0(sizeof(arm_timer_state
));
180 s
->control
= TIMER_CTRL_IE
;
182 s
->timer
= ptimer_init(arm_timer_tick
, s
, PTIMER_POLICY_DEFAULT
);
183 vmstate_register(NULL
, VMSTATE_INSTANCE_ID_ANY
, &vmstate_arm_timer
, s
);
187 /* ARM PrimeCell SP804 dual timer module.
189 * http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0271d/index.html
192 #define TYPE_SP804 "sp804"
193 #define SP804(obj) OBJECT_CHECK(SP804State, (obj), TYPE_SP804)
195 typedef struct SP804State
{
196 SysBusDevice parent_obj
;
199 arm_timer_state
*timer
[2];
200 uint32_t freq0
, freq1
;
205 static const uint8_t sp804_ids
[] = {
209 0xd, 0xf0, 0x05, 0xb1
212 /* Merge the IRQs from the two component devices. */
213 static void sp804_set_irq(void *opaque
, int irq
, int level
)
215 SP804State
*s
= (SP804State
*)opaque
;
217 s
->level
[irq
] = level
;
218 qemu_set_irq(s
->irq
, s
->level
[0] || s
->level
[1]);
221 static uint64_t sp804_read(void *opaque
, hwaddr offset
,
224 SP804State
*s
= (SP804State
*)opaque
;
227 return arm_timer_read(s
->timer
[0], offset
);
230 return arm_timer_read(s
->timer
[1], offset
- 0x20);
234 if (offset
>= 0xfe0 && offset
<= 0xffc) {
235 return sp804_ids
[(offset
- 0xfe0) >> 2];
239 /* Integration Test control registers, which we won't support */
240 case 0xf00: /* TimerITCR */
241 case 0xf04: /* TimerITOP (strictly write only but..) */
242 qemu_log_mask(LOG_UNIMP
,
243 "%s: integration test registers unimplemented\n",
248 qemu_log_mask(LOG_GUEST_ERROR
,
249 "%s: Bad offset %x\n", __func__
, (int)offset
);
253 static void sp804_write(void *opaque
, hwaddr offset
,
254 uint64_t value
, unsigned size
)
256 SP804State
*s
= (SP804State
*)opaque
;
259 arm_timer_write(s
->timer
[0], offset
, value
);
264 arm_timer_write(s
->timer
[1], offset
- 0x20, value
);
268 /* Technically we could be writing to the Test Registers, but not likely */
269 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad offset %x\n",
270 __func__
, (int)offset
);
273 static const MemoryRegionOps sp804_ops
= {
275 .write
= sp804_write
,
276 .endianness
= DEVICE_NATIVE_ENDIAN
,
279 static const VMStateDescription vmstate_sp804
= {
282 .minimum_version_id
= 1,
283 .fields
= (VMStateField
[]) {
284 VMSTATE_INT32_ARRAY(level
, SP804State
, 2),
285 VMSTATE_END_OF_LIST()
289 static void sp804_init(Object
*obj
)
291 SP804State
*s
= SP804(obj
);
292 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
294 sysbus_init_irq(sbd
, &s
->irq
);
295 memory_region_init_io(&s
->iomem
, obj
, &sp804_ops
, s
,
297 sysbus_init_mmio(sbd
, &s
->iomem
);
300 static void sp804_realize(DeviceState
*dev
, Error
**errp
)
302 SP804State
*s
= SP804(dev
);
304 s
->timer
[0] = arm_timer_init(s
->freq0
);
305 s
->timer
[1] = arm_timer_init(s
->freq1
);
306 s
->timer
[0]->irq
= qemu_allocate_irq(sp804_set_irq
, s
, 0);
307 s
->timer
[1]->irq
= qemu_allocate_irq(sp804_set_irq
, s
, 1);
310 /* Integrator/CP timer module. */
312 #define TYPE_INTEGRATOR_PIT "integrator_pit"
313 #define INTEGRATOR_PIT(obj) \
314 OBJECT_CHECK(icp_pit_state, (obj), TYPE_INTEGRATOR_PIT)
317 SysBusDevice parent_obj
;
320 arm_timer_state
*timer
[3];
323 static uint64_t icp_pit_read(void *opaque
, hwaddr offset
,
326 icp_pit_state
*s
= (icp_pit_state
*)opaque
;
329 /* ??? Don't know the PrimeCell ID for this device. */
332 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad timer %d\n", __func__
, n
);
336 return arm_timer_read(s
->timer
[n
], offset
& 0xff);
339 static void icp_pit_write(void *opaque
, hwaddr offset
,
340 uint64_t value
, unsigned size
)
342 icp_pit_state
*s
= (icp_pit_state
*)opaque
;
347 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad timer %d\n", __func__
, n
);
351 arm_timer_write(s
->timer
[n
], offset
& 0xff, value
);
354 static const MemoryRegionOps icp_pit_ops
= {
355 .read
= icp_pit_read
,
356 .write
= icp_pit_write
,
357 .endianness
= DEVICE_NATIVE_ENDIAN
,
360 static void icp_pit_init(Object
*obj
)
362 icp_pit_state
*s
= INTEGRATOR_PIT(obj
);
363 SysBusDevice
*dev
= SYS_BUS_DEVICE(obj
);
365 /* Timer 0 runs at the system clock speed (40MHz). */
366 s
->timer
[0] = arm_timer_init(40000000);
367 /* The other two timers run at 1MHz. */
368 s
->timer
[1] = arm_timer_init(1000000);
369 s
->timer
[2] = arm_timer_init(1000000);
371 sysbus_init_irq(dev
, &s
->timer
[0]->irq
);
372 sysbus_init_irq(dev
, &s
->timer
[1]->irq
);
373 sysbus_init_irq(dev
, &s
->timer
[2]->irq
);
375 memory_region_init_io(&s
->iomem
, obj
, &icp_pit_ops
, s
,
377 sysbus_init_mmio(dev
, &s
->iomem
);
378 /* This device has no state to save/restore. The component timers will
382 static const TypeInfo icp_pit_info
= {
383 .name
= TYPE_INTEGRATOR_PIT
,
384 .parent
= TYPE_SYS_BUS_DEVICE
,
385 .instance_size
= sizeof(icp_pit_state
),
386 .instance_init
= icp_pit_init
,
389 static Property sp804_properties
[] = {
390 DEFINE_PROP_UINT32("freq0", SP804State
, freq0
, 1000000),
391 DEFINE_PROP_UINT32("freq1", SP804State
, freq1
, 1000000),
392 DEFINE_PROP_END_OF_LIST(),
395 static void sp804_class_init(ObjectClass
*klass
, void *data
)
397 DeviceClass
*k
= DEVICE_CLASS(klass
);
399 k
->realize
= sp804_realize
;
400 device_class_set_props(k
, sp804_properties
);
401 k
->vmsd
= &vmstate_sp804
;
404 static const TypeInfo sp804_info
= {
406 .parent
= TYPE_SYS_BUS_DEVICE
,
407 .instance_size
= sizeof(SP804State
),
408 .instance_init
= sp804_init
,
409 .class_init
= sp804_class_init
,
412 static void arm_timer_register_types(void)
414 type_register_static(&icp_pit_info
);
415 type_register_static(&sp804_info
);
418 type_init(arm_timer_register_types
)