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"
13 /* Common timer implementation. */
15 #define TIMER_CTRL_ONESHOT (1 << 0)
16 #define TIMER_CTRL_32BIT (1 << 1)
17 #define TIMER_CTRL_DIV1 (0 << 2)
18 #define TIMER_CTRL_DIV16 (1 << 2)
19 #define TIMER_CTRL_DIV256 (2 << 2)
20 #define TIMER_CTRL_IE (1 << 5)
21 #define TIMER_CTRL_PERIODIC (1 << 6)
22 #define TIMER_CTRL_ENABLE (1 << 7)
33 /* Check all active timers, and schedule the next timer interrupt. */
35 static void arm_timer_update(arm_timer_state
*s
)
37 /* Update interrupts. */
38 if (s
->int_level
&& (s
->control
& TIMER_CTRL_IE
)) {
39 qemu_irq_raise(s
->irq
);
41 qemu_irq_lower(s
->irq
);
45 static uint32_t arm_timer_read(void *opaque
, target_phys_addr_t offset
)
47 arm_timer_state
*s
= (arm_timer_state
*)opaque
;
49 switch (offset
>> 2) {
50 case 0: /* TimerLoad */
51 case 6: /* TimerBGLoad */
53 case 1: /* TimerValue */
54 return ptimer_get_count(s
->timer
);
55 case 2: /* TimerControl */
57 case 4: /* TimerRIS */
59 case 5: /* TimerMIS */
60 if ((s
->control
& TIMER_CTRL_IE
) == 0)
64 hw_error("%s: Bad offset %x\n", __func__
, (int)offset
);
69 /* Reset the timer limit after settings have changed. */
70 static void arm_timer_recalibrate(arm_timer_state
*s
, int reload
)
74 if ((s
->control
& (TIMER_CTRL_PERIODIC
| TIMER_CTRL_ONESHOT
)) == 0) {
76 if (s
->control
& TIMER_CTRL_32BIT
)
84 ptimer_set_limit(s
->timer
, limit
, reload
);
87 static void arm_timer_write(void *opaque
, target_phys_addr_t offset
,
90 arm_timer_state
*s
= (arm_timer_state
*)opaque
;
93 switch (offset
>> 2) {
94 case 0: /* TimerLoad */
96 arm_timer_recalibrate(s
, 1);
98 case 1: /* TimerValue */
99 /* ??? Linux seems to want to write to this readonly register.
102 case 2: /* TimerControl */
103 if (s
->control
& TIMER_CTRL_ENABLE
) {
104 /* Pause the timer if it is running. This may cause some
105 inaccuracy dure to rounding, but avoids a whole lot of other
107 ptimer_stop(s
->timer
);
111 /* ??? Need to recalculate expiry time after changing divisor. */
112 switch ((value
>> 2) & 3) {
113 case 1: freq
>>= 4; break;
114 case 2: freq
>>= 8; break;
116 arm_timer_recalibrate(s
, s
->control
& TIMER_CTRL_ENABLE
);
117 ptimer_set_freq(s
->timer
, freq
);
118 if (s
->control
& TIMER_CTRL_ENABLE
) {
119 /* Restart the timer if still enabled. */
120 ptimer_run(s
->timer
, (s
->control
& TIMER_CTRL_ONESHOT
) != 0);
123 case 3: /* TimerIntClr */
126 case 6: /* TimerBGLoad */
128 arm_timer_recalibrate(s
, 0);
131 hw_error("%s: Bad offset %x\n", __func__
, (int)offset
);
136 static void arm_timer_tick(void *opaque
)
138 arm_timer_state
*s
= (arm_timer_state
*)opaque
;
143 static const VMStateDescription vmstate_arm_timer
= {
146 .minimum_version_id
= 1,
147 .minimum_version_id_old
= 1,
148 .fields
= (VMStateField
[]) {
149 VMSTATE_UINT32(control
, arm_timer_state
),
150 VMSTATE_UINT32(limit
, arm_timer_state
),
151 VMSTATE_INT32(int_level
, arm_timer_state
),
152 VMSTATE_PTIMER(timer
, arm_timer_state
),
153 VMSTATE_END_OF_LIST()
157 static arm_timer_state
*arm_timer_init(uint32_t freq
)
162 s
= (arm_timer_state
*)g_malloc0(sizeof(arm_timer_state
));
164 s
->control
= TIMER_CTRL_IE
;
166 bh
= qemu_bh_new(arm_timer_tick
, s
);
167 s
->timer
= ptimer_init(bh
);
168 vmstate_register(NULL
, -1, &vmstate_arm_timer
, s
);
172 /* ARM PrimeCell SP804 dual timer module.
174 * http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0271d/index.html
180 arm_timer_state
*timer
[2];
185 static const uint8_t sp804_ids
[] = {
189 0xd, 0xf0, 0x05, 0xb1
192 /* Merge the IRQs from the two component devices. */
193 static void sp804_set_irq(void *opaque
, int irq
, int level
)
195 sp804_state
*s
= (sp804_state
*)opaque
;
197 s
->level
[irq
] = level
;
198 qemu_set_irq(s
->irq
, s
->level
[0] || s
->level
[1]);
201 static uint64_t sp804_read(void *opaque
, target_phys_addr_t offset
,
204 sp804_state
*s
= (sp804_state
*)opaque
;
207 return arm_timer_read(s
->timer
[0], offset
);
210 return arm_timer_read(s
->timer
[1], offset
- 0x20);
214 if (offset
>= 0xfe0 && offset
<= 0xffc) {
215 return sp804_ids
[(offset
- 0xfe0) >> 2];
219 /* Integration Test control registers, which we won't support */
220 case 0xf00: /* TimerITCR */
221 case 0xf04: /* TimerITOP (strictly write only but..) */
225 hw_error("%s: Bad offset %x\n", __func__
, (int)offset
);
229 static void sp804_write(void *opaque
, target_phys_addr_t offset
,
230 uint64_t value
, unsigned size
)
232 sp804_state
*s
= (sp804_state
*)opaque
;
235 arm_timer_write(s
->timer
[0], offset
, value
);
240 arm_timer_write(s
->timer
[1], offset
- 0x20, value
);
244 /* Technically we could be writing to the Test Registers, but not likely */
245 hw_error("%s: Bad offset %x\n", __func__
, (int)offset
);
248 static const MemoryRegionOps sp804_ops
= {
250 .write
= sp804_write
,
251 .endianness
= DEVICE_NATIVE_ENDIAN
,
254 static const VMStateDescription vmstate_sp804
= {
257 .minimum_version_id
= 1,
258 .minimum_version_id_old
= 1,
259 .fields
= (VMStateField
[]) {
260 VMSTATE_INT32_ARRAY(level
, sp804_state
, 2),
261 VMSTATE_END_OF_LIST()
265 static int sp804_init(SysBusDevice
*dev
)
267 sp804_state
*s
= FROM_SYSBUS(sp804_state
, dev
);
270 qi
= qemu_allocate_irqs(sp804_set_irq
, s
, 2);
271 sysbus_init_irq(dev
, &s
->irq
);
272 /* ??? The timers are actually configurable between 32kHz and 1MHz, but
273 we don't implement that. */
274 s
->timer
[0] = arm_timer_init(1000000);
275 s
->timer
[1] = arm_timer_init(1000000);
276 s
->timer
[0]->irq
= qi
[0];
277 s
->timer
[1]->irq
= qi
[1];
278 memory_region_init_io(&s
->iomem
, &sp804_ops
, s
, "sp804", 0x1000);
279 sysbus_init_mmio(dev
, &s
->iomem
);
280 vmstate_register(&dev
->qdev
, -1, &vmstate_sp804
, s
);
285 /* Integrator/CP timer module. */
290 arm_timer_state
*timer
[3];
293 static uint64_t icp_pit_read(void *opaque
, target_phys_addr_t offset
,
296 icp_pit_state
*s
= (icp_pit_state
*)opaque
;
299 /* ??? Don't know the PrimeCell ID for this device. */
302 hw_error("%s: Bad timer %d\n", __func__
, n
);
305 return arm_timer_read(s
->timer
[n
], offset
& 0xff);
308 static void icp_pit_write(void *opaque
, target_phys_addr_t offset
,
309 uint64_t value
, unsigned size
)
311 icp_pit_state
*s
= (icp_pit_state
*)opaque
;
316 hw_error("%s: Bad timer %d\n", __func__
, n
);
319 arm_timer_write(s
->timer
[n
], offset
& 0xff, value
);
322 static const MemoryRegionOps icp_pit_ops
= {
323 .read
= icp_pit_read
,
324 .write
= icp_pit_write
,
325 .endianness
= DEVICE_NATIVE_ENDIAN
,
328 static int icp_pit_init(SysBusDevice
*dev
)
330 icp_pit_state
*s
= FROM_SYSBUS(icp_pit_state
, dev
);
332 /* Timer 0 runs at the system clock speed (40MHz). */
333 s
->timer
[0] = arm_timer_init(40000000);
334 /* The other two timers run at 1MHz. */
335 s
->timer
[1] = arm_timer_init(1000000);
336 s
->timer
[2] = arm_timer_init(1000000);
338 sysbus_init_irq(dev
, &s
->timer
[0]->irq
);
339 sysbus_init_irq(dev
, &s
->timer
[1]->irq
);
340 sysbus_init_irq(dev
, &s
->timer
[2]->irq
);
342 memory_region_init_io(&s
->iomem
, &icp_pit_ops
, s
, "icp_pit", 0x1000);
343 sysbus_init_mmio(dev
, &s
->iomem
);
344 /* This device has no state to save/restore. The component timers will
349 static void arm_timer_register_devices(void)
351 sysbus_register_dev("integrator_pit", sizeof(icp_pit_state
), icp_pit_init
);
352 sysbus_register_dev("sp804", sizeof(sp804_state
), sp804_init
);
355 device_init(arm_timer_register_devices
)