2 * ARM PrimeCell Timer modules.
4 * Copyright (c) 2005-2006 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licensed under the GPL.
11 #include "qemu-timer.h"
12 #include "qemu-common.h"
16 /* Common timer implementation. */
18 #define TIMER_CTRL_ONESHOT (1 << 0)
19 #define TIMER_CTRL_32BIT (1 << 1)
20 #define TIMER_CTRL_DIV1 (0 << 2)
21 #define TIMER_CTRL_DIV16 (1 << 2)
22 #define TIMER_CTRL_DIV256 (2 << 2)
23 #define TIMER_CTRL_IE (1 << 5)
24 #define TIMER_CTRL_PERIODIC (1 << 6)
25 #define TIMER_CTRL_ENABLE (1 << 7)
36 /* Check all active timers, and schedule the next timer interrupt. */
38 static void arm_timer_update(arm_timer_state
*s
)
40 /* Update interrupts. */
41 if (s
->int_level
&& (s
->control
& TIMER_CTRL_IE
)) {
42 qemu_irq_raise(s
->irq
);
44 qemu_irq_lower(s
->irq
);
48 static uint32_t arm_timer_read(void *opaque
, target_phys_addr_t offset
)
50 arm_timer_state
*s
= (arm_timer_state
*)opaque
;
52 switch (offset
>> 2) {
53 case 0: /* TimerLoad */
54 case 6: /* TimerBGLoad */
56 case 1: /* TimerValue */
57 return ptimer_get_count(s
->timer
);
58 case 2: /* TimerControl */
60 case 4: /* TimerRIS */
62 case 5: /* TimerMIS */
63 if ((s
->control
& TIMER_CTRL_IE
) == 0)
67 hw_error("%s: Bad offset %x\n", __func__
, (int)offset
);
72 /* Reset the timer limit after settings have changed. */
73 static void arm_timer_recalibrate(arm_timer_state
*s
, int reload
)
77 if ((s
->control
& (TIMER_CTRL_PERIODIC
| TIMER_CTRL_ONESHOT
)) == 0) {
79 if (s
->control
& TIMER_CTRL_32BIT
)
87 ptimer_set_limit(s
->timer
, limit
, reload
);
90 static void arm_timer_write(void *opaque
, target_phys_addr_t offset
,
93 arm_timer_state
*s
= (arm_timer_state
*)opaque
;
96 switch (offset
>> 2) {
97 case 0: /* TimerLoad */
99 arm_timer_recalibrate(s
, 1);
101 case 1: /* TimerValue */
102 /* ??? Linux seems to want to write to this readonly register.
105 case 2: /* TimerControl */
106 if (s
->control
& TIMER_CTRL_ENABLE
) {
107 /* Pause the timer if it is running. This may cause some
108 inaccuracy dure to rounding, but avoids a whole lot of other
110 ptimer_stop(s
->timer
);
114 /* ??? Need to recalculate expiry time after changing divisor. */
115 switch ((value
>> 2) & 3) {
116 case 1: freq
>>= 4; break;
117 case 2: freq
>>= 8; break;
119 arm_timer_recalibrate(s
, s
->control
& TIMER_CTRL_ENABLE
);
120 ptimer_set_freq(s
->timer
, freq
);
121 if (s
->control
& TIMER_CTRL_ENABLE
) {
122 /* Restart the timer if still enabled. */
123 ptimer_run(s
->timer
, (s
->control
& TIMER_CTRL_ONESHOT
) != 0);
126 case 3: /* TimerIntClr */
129 case 6: /* TimerBGLoad */
131 arm_timer_recalibrate(s
, 0);
134 hw_error("%s: Bad offset %x\n", __func__
, (int)offset
);
139 static void arm_timer_tick(void *opaque
)
141 arm_timer_state
*s
= (arm_timer_state
*)opaque
;
146 static const VMStateDescription vmstate_arm_timer
= {
149 .minimum_version_id
= 1,
150 .minimum_version_id_old
= 1,
151 .fields
= (VMStateField
[]) {
152 VMSTATE_UINT32(control
, arm_timer_state
),
153 VMSTATE_UINT32(limit
, arm_timer_state
),
154 VMSTATE_INT32(int_level
, arm_timer_state
),
155 VMSTATE_PTIMER(timer
, arm_timer_state
),
156 VMSTATE_END_OF_LIST()
160 static arm_timer_state
*arm_timer_init(uint32_t freq
)
165 s
= (arm_timer_state
*)g_malloc0(sizeof(arm_timer_state
));
167 s
->control
= TIMER_CTRL_IE
;
169 bh
= qemu_bh_new(arm_timer_tick
, s
);
170 s
->timer
= ptimer_init(bh
);
171 vmstate_register(NULL
, -1, &vmstate_arm_timer
, s
);
175 /* ARM PrimeCell SP804 dual timer module.
177 * http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0271d/index.html
183 arm_timer_state
*timer
[2];
184 uint32_t freq0
, freq1
;
189 static const uint8_t sp804_ids
[] = {
193 0xd, 0xf0, 0x05, 0xb1
196 /* Merge the IRQs from the two component devices. */
197 static void sp804_set_irq(void *opaque
, int irq
, int level
)
199 sp804_state
*s
= (sp804_state
*)opaque
;
201 s
->level
[irq
] = level
;
202 qemu_set_irq(s
->irq
, s
->level
[0] || s
->level
[1]);
205 static uint64_t sp804_read(void *opaque
, target_phys_addr_t offset
,
208 sp804_state
*s
= (sp804_state
*)opaque
;
211 return arm_timer_read(s
->timer
[0], offset
);
214 return arm_timer_read(s
->timer
[1], offset
- 0x20);
218 if (offset
>= 0xfe0 && offset
<= 0xffc) {
219 return sp804_ids
[(offset
- 0xfe0) >> 2];
223 /* Integration Test control registers, which we won't support */
224 case 0xf00: /* TimerITCR */
225 case 0xf04: /* TimerITOP (strictly write only but..) */
229 hw_error("%s: Bad offset %x\n", __func__
, (int)offset
);
233 static void sp804_write(void *opaque
, target_phys_addr_t offset
,
234 uint64_t value
, unsigned size
)
236 sp804_state
*s
= (sp804_state
*)opaque
;
239 arm_timer_write(s
->timer
[0], offset
, value
);
244 arm_timer_write(s
->timer
[1], offset
- 0x20, value
);
248 /* Technically we could be writing to the Test Registers, but not likely */
249 hw_error("%s: Bad offset %x\n", __func__
, (int)offset
);
252 static const MemoryRegionOps sp804_ops
= {
254 .write
= sp804_write
,
255 .endianness
= DEVICE_NATIVE_ENDIAN
,
258 static const VMStateDescription vmstate_sp804
= {
261 .minimum_version_id
= 1,
262 .minimum_version_id_old
= 1,
263 .fields
= (VMStateField
[]) {
264 VMSTATE_INT32_ARRAY(level
, sp804_state
, 2),
265 VMSTATE_END_OF_LIST()
269 static int sp804_init(SysBusDevice
*dev
)
271 sp804_state
*s
= FROM_SYSBUS(sp804_state
, dev
);
274 qi
= qemu_allocate_irqs(sp804_set_irq
, s
, 2);
275 sysbus_init_irq(dev
, &s
->irq
);
276 s
->timer
[0] = arm_timer_init(s
->freq0
);
277 s
->timer
[1] = arm_timer_init(s
->freq1
);
278 s
->timer
[0]->irq
= qi
[0];
279 s
->timer
[1]->irq
= qi
[1];
280 memory_region_init_io(&s
->iomem
, &sp804_ops
, s
, "sp804", 0x1000);
281 sysbus_init_mmio(dev
, &s
->iomem
);
282 vmstate_register(&dev
->qdev
, -1, &vmstate_sp804
, s
);
286 /* Integrator/CP timer module. */
291 arm_timer_state
*timer
[3];
294 static uint64_t icp_pit_read(void *opaque
, target_phys_addr_t offset
,
297 icp_pit_state
*s
= (icp_pit_state
*)opaque
;
300 /* ??? Don't know the PrimeCell ID for this device. */
303 hw_error("%s: Bad timer %d\n", __func__
, n
);
306 return arm_timer_read(s
->timer
[n
], offset
& 0xff);
309 static void icp_pit_write(void *opaque
, target_phys_addr_t offset
,
310 uint64_t value
, unsigned size
)
312 icp_pit_state
*s
= (icp_pit_state
*)opaque
;
317 hw_error("%s: Bad timer %d\n", __func__
, n
);
320 arm_timer_write(s
->timer
[n
], offset
& 0xff, value
);
323 static const MemoryRegionOps icp_pit_ops
= {
324 .read
= icp_pit_read
,
325 .write
= icp_pit_write
,
326 .endianness
= DEVICE_NATIVE_ENDIAN
,
329 static int icp_pit_init(SysBusDevice
*dev
)
331 icp_pit_state
*s
= FROM_SYSBUS(icp_pit_state
, dev
);
333 /* Timer 0 runs at the system clock speed (40MHz). */
334 s
->timer
[0] = arm_timer_init(40000000);
335 /* The other two timers run at 1MHz. */
336 s
->timer
[1] = arm_timer_init(1000000);
337 s
->timer
[2] = arm_timer_init(1000000);
339 sysbus_init_irq(dev
, &s
->timer
[0]->irq
);
340 sysbus_init_irq(dev
, &s
->timer
[1]->irq
);
341 sysbus_init_irq(dev
, &s
->timer
[2]->irq
);
343 memory_region_init_io(&s
->iomem
, &icp_pit_ops
, s
, "icp_pit", 0x1000);
344 sysbus_init_mmio(dev
, &s
->iomem
);
345 /* This device has no state to save/restore. The component timers will
350 static void icp_pit_class_init(ObjectClass
*klass
, void *data
)
352 SysBusDeviceClass
*sdc
= SYS_BUS_DEVICE_CLASS(klass
);
354 sdc
->init
= icp_pit_init
;
357 static TypeInfo icp_pit_info
= {
358 .name
= "integrator_pit",
359 .parent
= TYPE_SYS_BUS_DEVICE
,
360 .instance_size
= sizeof(icp_pit_state
),
361 .class_init
= icp_pit_class_init
,
364 static Property sp804_properties
[] = {
365 DEFINE_PROP_UINT32("freq0", sp804_state
, freq0
, 1000000),
366 DEFINE_PROP_UINT32("freq1", sp804_state
, freq1
, 1000000),
367 DEFINE_PROP_END_OF_LIST(),
370 static void sp804_class_init(ObjectClass
*klass
, void *data
)
372 SysBusDeviceClass
*sdc
= SYS_BUS_DEVICE_CLASS(klass
);
373 DeviceClass
*k
= DEVICE_CLASS(klass
);
375 sdc
->init
= sp804_init
;
376 k
->props
= sp804_properties
;
379 static TypeInfo sp804_info
= {
381 .parent
= TYPE_SYS_BUS_DEVICE
,
382 .instance_size
= sizeof(sp804_state
),
383 .class_init
= sp804_class_init
,
386 static void arm_timer_register_types(void)
388 type_register_static(&icp_pit_info
);
389 type_register_static(&sp804_info
);
392 type_init(arm_timer_register_types
)