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"
19 #include "qom/object.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
);
79 * Reset the timer limit after settings have changed.
80 * May only be called from inside a ptimer transaction block.
82 static void arm_timer_recalibrate(arm_timer_state
*s
, int reload
)
86 if ((s
->control
& (TIMER_CTRL_PERIODIC
| TIMER_CTRL_ONESHOT
)) == 0) {
88 if (s
->control
& TIMER_CTRL_32BIT
)
96 ptimer_set_limit(s
->timer
, limit
, reload
);
99 static void arm_timer_write(void *opaque
, hwaddr offset
,
102 arm_timer_state
*s
= (arm_timer_state
*)opaque
;
105 switch (offset
>> 2) {
106 case 0: /* TimerLoad */
108 ptimer_transaction_begin(s
->timer
);
109 arm_timer_recalibrate(s
, 1);
110 ptimer_transaction_commit(s
->timer
);
112 case 1: /* TimerValue */
113 /* ??? Linux seems to want to write to this readonly register.
116 case 2: /* TimerControl */
117 ptimer_transaction_begin(s
->timer
);
118 if (s
->control
& TIMER_CTRL_ENABLE
) {
119 /* Pause the timer if it is running. This may cause some
120 inaccuracy dure to rounding, but avoids a whole lot of other
122 ptimer_stop(s
->timer
);
126 /* ??? Need to recalculate expiry time after changing divisor. */
127 switch ((value
>> 2) & 3) {
128 case 1: freq
>>= 4; break;
129 case 2: freq
>>= 8; break;
131 arm_timer_recalibrate(s
, s
->control
& TIMER_CTRL_ENABLE
);
132 ptimer_set_freq(s
->timer
, freq
);
133 if (s
->control
& TIMER_CTRL_ENABLE
) {
134 /* Restart the timer if still enabled. */
135 ptimer_run(s
->timer
, (s
->control
& TIMER_CTRL_ONESHOT
) != 0);
137 ptimer_transaction_commit(s
->timer
);
139 case 3: /* TimerIntClr */
142 case 6: /* TimerBGLoad */
144 ptimer_transaction_begin(s
->timer
);
145 arm_timer_recalibrate(s
, 0);
146 ptimer_transaction_commit(s
->timer
);
149 qemu_log_mask(LOG_GUEST_ERROR
,
150 "%s: Bad offset %x\n", __func__
, (int)offset
);
155 static void arm_timer_tick(void *opaque
)
157 arm_timer_state
*s
= (arm_timer_state
*)opaque
;
162 static const VMStateDescription vmstate_arm_timer
= {
165 .minimum_version_id
= 1,
166 .fields
= (VMStateField
[]) {
167 VMSTATE_UINT32(control
, arm_timer_state
),
168 VMSTATE_UINT32(limit
, arm_timer_state
),
169 VMSTATE_INT32(int_level
, arm_timer_state
),
170 VMSTATE_PTIMER(timer
, arm_timer_state
),
171 VMSTATE_END_OF_LIST()
175 static arm_timer_state
*arm_timer_init(uint32_t freq
)
179 s
= (arm_timer_state
*)g_malloc0(sizeof(arm_timer_state
));
181 s
->control
= TIMER_CTRL_IE
;
183 s
->timer
= ptimer_init(arm_timer_tick
, s
, PTIMER_POLICY_DEFAULT
);
184 vmstate_register(NULL
, VMSTATE_INSTANCE_ID_ANY
, &vmstate_arm_timer
, s
);
188 /* ARM PrimeCell SP804 dual timer module.
190 * http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0271d/index.html
193 #define TYPE_SP804 "sp804"
194 typedef struct SP804State SP804State
;
195 DECLARE_INSTANCE_CHECKER(SP804State
, SP804
,
199 SysBusDevice parent_obj
;
202 arm_timer_state
*timer
[2];
203 uint32_t freq0
, freq1
;
208 static const uint8_t sp804_ids
[] = {
212 0xd, 0xf0, 0x05, 0xb1
215 /* Merge the IRQs from the two component devices. */
216 static void sp804_set_irq(void *opaque
, int irq
, int level
)
218 SP804State
*s
= (SP804State
*)opaque
;
220 s
->level
[irq
] = level
;
221 qemu_set_irq(s
->irq
, s
->level
[0] || s
->level
[1]);
224 static uint64_t sp804_read(void *opaque
, hwaddr offset
,
227 SP804State
*s
= (SP804State
*)opaque
;
230 return arm_timer_read(s
->timer
[0], offset
);
233 return arm_timer_read(s
->timer
[1], offset
- 0x20);
237 if (offset
>= 0xfe0 && offset
<= 0xffc) {
238 return sp804_ids
[(offset
- 0xfe0) >> 2];
242 /* Integration Test control registers, which we won't support */
243 case 0xf00: /* TimerITCR */
244 case 0xf04: /* TimerITOP (strictly write only but..) */
245 qemu_log_mask(LOG_UNIMP
,
246 "%s: integration test registers unimplemented\n",
251 qemu_log_mask(LOG_GUEST_ERROR
,
252 "%s: Bad offset %x\n", __func__
, (int)offset
);
256 static void sp804_write(void *opaque
, hwaddr offset
,
257 uint64_t value
, unsigned size
)
259 SP804State
*s
= (SP804State
*)opaque
;
262 arm_timer_write(s
->timer
[0], offset
, value
);
267 arm_timer_write(s
->timer
[1], offset
- 0x20, value
);
271 /* Technically we could be writing to the Test Registers, but not likely */
272 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad offset %x\n",
273 __func__
, (int)offset
);
276 static const MemoryRegionOps sp804_ops
= {
278 .write
= sp804_write
,
279 .endianness
= DEVICE_NATIVE_ENDIAN
,
282 static const VMStateDescription vmstate_sp804
= {
285 .minimum_version_id
= 1,
286 .fields
= (VMStateField
[]) {
287 VMSTATE_INT32_ARRAY(level
, SP804State
, 2),
288 VMSTATE_END_OF_LIST()
292 static void sp804_init(Object
*obj
)
294 SP804State
*s
= SP804(obj
);
295 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
297 sysbus_init_irq(sbd
, &s
->irq
);
298 memory_region_init_io(&s
->iomem
, obj
, &sp804_ops
, s
,
300 sysbus_init_mmio(sbd
, &s
->iomem
);
303 static void sp804_realize(DeviceState
*dev
, Error
**errp
)
305 SP804State
*s
= SP804(dev
);
307 s
->timer
[0] = arm_timer_init(s
->freq0
);
308 s
->timer
[1] = arm_timer_init(s
->freq1
);
309 s
->timer
[0]->irq
= qemu_allocate_irq(sp804_set_irq
, s
, 0);
310 s
->timer
[1]->irq
= qemu_allocate_irq(sp804_set_irq
, s
, 1);
313 /* Integrator/CP timer module. */
315 #define TYPE_INTEGRATOR_PIT "integrator_pit"
316 typedef struct icp_pit_state icp_pit_state
;
317 DECLARE_INSTANCE_CHECKER(icp_pit_state
, INTEGRATOR_PIT
,
320 struct icp_pit_state
{
321 SysBusDevice parent_obj
;
324 arm_timer_state
*timer
[3];
327 static uint64_t icp_pit_read(void *opaque
, hwaddr offset
,
330 icp_pit_state
*s
= (icp_pit_state
*)opaque
;
333 /* ??? Don't know the PrimeCell ID for this device. */
336 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad timer %d\n", __func__
, n
);
340 return arm_timer_read(s
->timer
[n
], offset
& 0xff);
343 static void icp_pit_write(void *opaque
, hwaddr offset
,
344 uint64_t value
, unsigned size
)
346 icp_pit_state
*s
= (icp_pit_state
*)opaque
;
351 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad timer %d\n", __func__
, n
);
355 arm_timer_write(s
->timer
[n
], offset
& 0xff, value
);
358 static const MemoryRegionOps icp_pit_ops
= {
359 .read
= icp_pit_read
,
360 .write
= icp_pit_write
,
361 .endianness
= DEVICE_NATIVE_ENDIAN
,
364 static void icp_pit_init(Object
*obj
)
366 icp_pit_state
*s
= INTEGRATOR_PIT(obj
);
367 SysBusDevice
*dev
= SYS_BUS_DEVICE(obj
);
369 /* Timer 0 runs at the system clock speed (40MHz). */
370 s
->timer
[0] = arm_timer_init(40000000);
371 /* The other two timers run at 1MHz. */
372 s
->timer
[1] = arm_timer_init(1000000);
373 s
->timer
[2] = arm_timer_init(1000000);
375 sysbus_init_irq(dev
, &s
->timer
[0]->irq
);
376 sysbus_init_irq(dev
, &s
->timer
[1]->irq
);
377 sysbus_init_irq(dev
, &s
->timer
[2]->irq
);
379 memory_region_init_io(&s
->iomem
, obj
, &icp_pit_ops
, s
,
381 sysbus_init_mmio(dev
, &s
->iomem
);
382 /* This device has no state to save/restore. The component timers will
386 static const TypeInfo icp_pit_info
= {
387 .name
= TYPE_INTEGRATOR_PIT
,
388 .parent
= TYPE_SYS_BUS_DEVICE
,
389 .instance_size
= sizeof(icp_pit_state
),
390 .instance_init
= icp_pit_init
,
393 static Property sp804_properties
[] = {
394 DEFINE_PROP_UINT32("freq0", SP804State
, freq0
, 1000000),
395 DEFINE_PROP_UINT32("freq1", SP804State
, freq1
, 1000000),
396 DEFINE_PROP_END_OF_LIST(),
399 static void sp804_class_init(ObjectClass
*klass
, void *data
)
401 DeviceClass
*k
= DEVICE_CLASS(klass
);
403 k
->realize
= sp804_realize
;
404 device_class_set_props(k
, sp804_properties
);
405 k
->vmsd
= &vmstate_sp804
;
408 static const TypeInfo sp804_info
= {
410 .parent
= TYPE_SYS_BUS_DEVICE
,
411 .instance_size
= sizeof(SP804State
),
412 .instance_init
= sp804_init
,
413 .class_init
= sp804_class_init
,
416 static void arm_timer_register_types(void)
418 type_register_static(&icp_pit_info
);
419 type_register_static(&sp804_info
);
422 type_init(arm_timer_register_types
)