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 "qemu/timer.h"
13 #include "qemu-common.h"
15 #include "hw/ptimer.h"
16 #include "qemu/main-loop.h"
18 /* Common timer implementation. */
20 #define TIMER_CTRL_ONESHOT (1 << 0)
21 #define TIMER_CTRL_32BIT (1 << 1)
22 #define TIMER_CTRL_DIV1 (0 << 2)
23 #define TIMER_CTRL_DIV16 (1 << 2)
24 #define TIMER_CTRL_DIV256 (2 << 2)
25 #define TIMER_CTRL_IE (1 << 5)
26 #define TIMER_CTRL_PERIODIC (1 << 6)
27 #define TIMER_CTRL_ENABLE (1 << 7)
38 /* Check all active timers, and schedule the next timer interrupt. */
40 static void arm_timer_update(arm_timer_state
*s
)
42 /* Update interrupts. */
43 if (s
->int_level
&& (s
->control
& TIMER_CTRL_IE
)) {
44 qemu_irq_raise(s
->irq
);
46 qemu_irq_lower(s
->irq
);
50 static uint32_t arm_timer_read(void *opaque
, hwaddr offset
)
52 arm_timer_state
*s
= (arm_timer_state
*)opaque
;
54 switch (offset
>> 2) {
55 case 0: /* TimerLoad */
56 case 6: /* TimerBGLoad */
58 case 1: /* TimerValue */
59 return ptimer_get_count(s
->timer
);
60 case 2: /* TimerControl */
62 case 4: /* TimerRIS */
64 case 5: /* TimerMIS */
65 if ((s
->control
& TIMER_CTRL_IE
) == 0)
69 qemu_log_mask(LOG_GUEST_ERROR
,
70 "%s: Bad offset %x\n", __func__
, (int)offset
);
75 /* Reset the timer limit after settings have changed. */
76 static void arm_timer_recalibrate(arm_timer_state
*s
, int reload
)
80 if ((s
->control
& (TIMER_CTRL_PERIODIC
| TIMER_CTRL_ONESHOT
)) == 0) {
82 if (s
->control
& TIMER_CTRL_32BIT
)
90 ptimer_set_limit(s
->timer
, limit
, reload
);
93 static void arm_timer_write(void *opaque
, hwaddr offset
,
96 arm_timer_state
*s
= (arm_timer_state
*)opaque
;
99 switch (offset
>> 2) {
100 case 0: /* TimerLoad */
102 arm_timer_recalibrate(s
, 1);
104 case 1: /* TimerValue */
105 /* ??? Linux seems to want to write to this readonly register.
108 case 2: /* TimerControl */
109 if (s
->control
& TIMER_CTRL_ENABLE
) {
110 /* Pause the timer if it is running. This may cause some
111 inaccuracy dure to rounding, but avoids a whole lot of other
113 ptimer_stop(s
->timer
);
117 /* ??? Need to recalculate expiry time after changing divisor. */
118 switch ((value
>> 2) & 3) {
119 case 1: freq
>>= 4; break;
120 case 2: freq
>>= 8; break;
122 arm_timer_recalibrate(s
, s
->control
& TIMER_CTRL_ENABLE
);
123 ptimer_set_freq(s
->timer
, freq
);
124 if (s
->control
& TIMER_CTRL_ENABLE
) {
125 /* Restart the timer if still enabled. */
126 ptimer_run(s
->timer
, (s
->control
& TIMER_CTRL_ONESHOT
) != 0);
129 case 3: /* TimerIntClr */
132 case 6: /* TimerBGLoad */
134 arm_timer_recalibrate(s
, 0);
137 qemu_log_mask(LOG_GUEST_ERROR
,
138 "%s: Bad offset %x\n", __func__
, (int)offset
);
143 static void arm_timer_tick(void *opaque
)
145 arm_timer_state
*s
= (arm_timer_state
*)opaque
;
150 static const VMStateDescription vmstate_arm_timer
= {
153 .minimum_version_id
= 1,
154 .fields
= (VMStateField
[]) {
155 VMSTATE_UINT32(control
, arm_timer_state
),
156 VMSTATE_UINT32(limit
, arm_timer_state
),
157 VMSTATE_INT32(int_level
, arm_timer_state
),
158 VMSTATE_PTIMER(timer
, arm_timer_state
),
159 VMSTATE_END_OF_LIST()
163 static arm_timer_state
*arm_timer_init(uint32_t freq
)
168 s
= (arm_timer_state
*)g_malloc0(sizeof(arm_timer_state
));
170 s
->control
= TIMER_CTRL_IE
;
172 bh
= qemu_bh_new(arm_timer_tick
, s
);
173 s
->timer
= ptimer_init(bh
);
174 vmstate_register(NULL
, -1, &vmstate_arm_timer
, s
);
178 /* ARM PrimeCell SP804 dual timer module.
180 * http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0271d/index.html
183 #define TYPE_SP804 "sp804"
184 #define SP804(obj) OBJECT_CHECK(SP804State, (obj), TYPE_SP804)
186 typedef struct SP804State
{
187 SysBusDevice parent_obj
;
190 arm_timer_state
*timer
[2];
191 uint32_t freq0
, freq1
;
196 static const uint8_t sp804_ids
[] = {
200 0xd, 0xf0, 0x05, 0xb1
203 /* Merge the IRQs from the two component devices. */
204 static void sp804_set_irq(void *opaque
, int irq
, int level
)
206 SP804State
*s
= (SP804State
*)opaque
;
208 s
->level
[irq
] = level
;
209 qemu_set_irq(s
->irq
, s
->level
[0] || s
->level
[1]);
212 static uint64_t sp804_read(void *opaque
, hwaddr offset
,
215 SP804State
*s
= (SP804State
*)opaque
;
218 return arm_timer_read(s
->timer
[0], offset
);
221 return arm_timer_read(s
->timer
[1], offset
- 0x20);
225 if (offset
>= 0xfe0 && offset
<= 0xffc) {
226 return sp804_ids
[(offset
- 0xfe0) >> 2];
230 /* Integration Test control registers, which we won't support */
231 case 0xf00: /* TimerITCR */
232 case 0xf04: /* TimerITOP (strictly write only but..) */
233 qemu_log_mask(LOG_UNIMP
,
234 "%s: integration test registers unimplemented\n",
239 qemu_log_mask(LOG_GUEST_ERROR
,
240 "%s: Bad offset %x\n", __func__
, (int)offset
);
244 static void sp804_write(void *opaque
, hwaddr offset
,
245 uint64_t value
, unsigned size
)
247 SP804State
*s
= (SP804State
*)opaque
;
250 arm_timer_write(s
->timer
[0], offset
, value
);
255 arm_timer_write(s
->timer
[1], offset
- 0x20, value
);
259 /* Technically we could be writing to the Test Registers, but not likely */
260 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad offset %x\n",
261 __func__
, (int)offset
);
264 static const MemoryRegionOps sp804_ops
= {
266 .write
= sp804_write
,
267 .endianness
= DEVICE_NATIVE_ENDIAN
,
270 static const VMStateDescription vmstate_sp804
= {
273 .minimum_version_id
= 1,
274 .fields
= (VMStateField
[]) {
275 VMSTATE_INT32_ARRAY(level
, SP804State
, 2),
276 VMSTATE_END_OF_LIST()
280 static void sp804_init(Object
*obj
)
282 SP804State
*s
= SP804(obj
);
283 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
285 sysbus_init_irq(sbd
, &s
->irq
);
286 memory_region_init_io(&s
->iomem
, obj
, &sp804_ops
, s
,
288 sysbus_init_mmio(sbd
, &s
->iomem
);
291 static void sp804_realize(DeviceState
*dev
, Error
**errp
)
293 SP804State
*s
= SP804(dev
);
295 s
->timer
[0] = arm_timer_init(s
->freq0
);
296 s
->timer
[1] = arm_timer_init(s
->freq1
);
297 s
->timer
[0]->irq
= qemu_allocate_irq(sp804_set_irq
, s
, 0);
298 s
->timer
[1]->irq
= qemu_allocate_irq(sp804_set_irq
, s
, 1);
301 /* Integrator/CP timer module. */
303 #define TYPE_INTEGRATOR_PIT "integrator_pit"
304 #define INTEGRATOR_PIT(obj) \
305 OBJECT_CHECK(icp_pit_state, (obj), TYPE_INTEGRATOR_PIT)
308 SysBusDevice parent_obj
;
311 arm_timer_state
*timer
[3];
314 static uint64_t icp_pit_read(void *opaque
, hwaddr offset
,
317 icp_pit_state
*s
= (icp_pit_state
*)opaque
;
320 /* ??? Don't know the PrimeCell ID for this device. */
323 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad timer %d\n", __func__
, n
);
327 return arm_timer_read(s
->timer
[n
], offset
& 0xff);
330 static void icp_pit_write(void *opaque
, hwaddr offset
,
331 uint64_t value
, unsigned size
)
333 icp_pit_state
*s
= (icp_pit_state
*)opaque
;
338 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad timer %d\n", __func__
, n
);
342 arm_timer_write(s
->timer
[n
], offset
& 0xff, value
);
345 static const MemoryRegionOps icp_pit_ops
= {
346 .read
= icp_pit_read
,
347 .write
= icp_pit_write
,
348 .endianness
= DEVICE_NATIVE_ENDIAN
,
351 static void icp_pit_init(Object
*obj
)
353 icp_pit_state
*s
= INTEGRATOR_PIT(obj
);
354 SysBusDevice
*dev
= SYS_BUS_DEVICE(obj
);
356 /* Timer 0 runs at the system clock speed (40MHz). */
357 s
->timer
[0] = arm_timer_init(40000000);
358 /* The other two timers run at 1MHz. */
359 s
->timer
[1] = arm_timer_init(1000000);
360 s
->timer
[2] = arm_timer_init(1000000);
362 sysbus_init_irq(dev
, &s
->timer
[0]->irq
);
363 sysbus_init_irq(dev
, &s
->timer
[1]->irq
);
364 sysbus_init_irq(dev
, &s
->timer
[2]->irq
);
366 memory_region_init_io(&s
->iomem
, obj
, &icp_pit_ops
, s
,
368 sysbus_init_mmio(dev
, &s
->iomem
);
369 /* This device has no state to save/restore. The component timers will
373 static const TypeInfo icp_pit_info
= {
374 .name
= TYPE_INTEGRATOR_PIT
,
375 .parent
= TYPE_SYS_BUS_DEVICE
,
376 .instance_size
= sizeof(icp_pit_state
),
377 .instance_init
= icp_pit_init
,
380 static Property sp804_properties
[] = {
381 DEFINE_PROP_UINT32("freq0", SP804State
, freq0
, 1000000),
382 DEFINE_PROP_UINT32("freq1", SP804State
, freq1
, 1000000),
383 DEFINE_PROP_END_OF_LIST(),
386 static void sp804_class_init(ObjectClass
*klass
, void *data
)
388 DeviceClass
*k
= DEVICE_CLASS(klass
);
390 k
->realize
= sp804_realize
;
391 k
->props
= sp804_properties
;
392 k
->vmsd
= &vmstate_sp804
;
395 static const TypeInfo sp804_info
= {
397 .parent
= TYPE_SYS_BUS_DEVICE
,
398 .instance_size
= sizeof(SP804State
),
399 .instance_init
= sp804_init
,
400 .class_init
= sp804_class_init
,
403 static void arm_timer_register_types(void)
405 type_register_static(&icp_pit_info
);
406 type_register_static(&sp804_info
);
409 type_init(arm_timer_register_types
)