2 * ARM PrimeCell Timer modules.
4 * Copyright (c) 2005-2006 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licenced under the GPL.
11 #include "qemu-timer.h"
12 #include "primecell.h"
14 /* Common timer implementation. */
16 #define TIMER_CTRL_ONESHOT (1 << 0)
17 #define TIMER_CTRL_32BIT (1 << 1)
18 #define TIMER_CTRL_DIV1 (0 << 2)
19 #define TIMER_CTRL_DIV16 (1 << 2)
20 #define TIMER_CTRL_DIV256 (2 << 2)
21 #define TIMER_CTRL_IE (1 << 5)
22 #define TIMER_CTRL_PERIODIC (1 << 6)
23 #define TIMER_CTRL_ENABLE (1 << 7)
34 /* Check all active timers, and schedule the next timer interrupt. */
36 static void arm_timer_update(arm_timer_state
*s
)
38 /* Update interrupts. */
39 if (s
->int_level
&& (s
->control
& TIMER_CTRL_IE
)) {
40 qemu_irq_raise(s
->irq
);
42 qemu_irq_lower(s
->irq
);
46 static uint32_t arm_timer_read(void *opaque
, target_phys_addr_t offset
)
48 arm_timer_state
*s
= (arm_timer_state
*)opaque
;
50 switch (offset
>> 2) {
51 case 0: /* TimerLoad */
52 case 6: /* TimerBGLoad */
54 case 1: /* TimerValue */
55 return ptimer_get_count(s
->timer
);
56 case 2: /* TimerControl */
58 case 4: /* TimerRIS */
60 case 5: /* TimerMIS */
61 if ((s
->control
& TIMER_CTRL_IE
) == 0)
65 cpu_abort (cpu_single_env
, "arm_timer_read: Bad offset %x\n",
71 /* Reset the timer limit after settings have changed. */
72 static void arm_timer_recalibrate(arm_timer_state
*s
, int reload
)
76 if ((s
->control
& TIMER_CTRL_PERIODIC
) == 0) {
78 if (s
->control
& TIMER_CTRL_32BIT
)
86 ptimer_set_limit(s
->timer
, limit
, reload
);
89 static void arm_timer_write(void *opaque
, target_phys_addr_t offset
,
92 arm_timer_state
*s
= (arm_timer_state
*)opaque
;
95 switch (offset
>> 2) {
96 case 0: /* TimerLoad */
98 arm_timer_recalibrate(s
, 1);
100 case 1: /* TimerValue */
101 /* ??? Linux seems to want to write to this readonly register.
104 case 2: /* TimerControl */
105 if (s
->control
& TIMER_CTRL_ENABLE
) {
106 /* Pause the timer if it is running. This may cause some
107 inaccuracy dure to rounding, but avoids a whole lot of other
109 ptimer_stop(s
->timer
);
113 /* ??? Need to recalculate expiry time after changing divisor. */
114 switch ((value
>> 2) & 3) {
115 case 1: freq
>>= 4; break;
116 case 2: freq
>>= 8; break;
118 arm_timer_recalibrate(s
, 0);
119 ptimer_set_freq(s
->timer
, freq
);
120 if (s
->control
& TIMER_CTRL_ENABLE
) {
121 /* Restart the timer if still enabled. */
122 ptimer_run(s
->timer
, (s
->control
& TIMER_CTRL_ONESHOT
) != 0);
125 case 3: /* TimerIntClr */
128 case 6: /* TimerBGLoad */
130 arm_timer_recalibrate(s
, 0);
133 cpu_abort (cpu_single_env
, "arm_timer_write: Bad offset %x\n",
139 static void arm_timer_tick(void *opaque
)
141 arm_timer_state
*s
= (arm_timer_state
*)opaque
;
146 static void arm_timer_save(QEMUFile
*f
, void *opaque
)
148 arm_timer_state
*s
= (arm_timer_state
*)opaque
;
149 qemu_put_be32(f
, s
->control
);
150 qemu_put_be32(f
, s
->limit
);
151 qemu_put_be32(f
, s
->int_level
);
152 qemu_put_ptimer(f
, s
->timer
);
155 static int arm_timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
157 arm_timer_state
*s
= (arm_timer_state
*)opaque
;
162 s
->control
= qemu_get_be32(f
);
163 s
->limit
= qemu_get_be32(f
);
164 s
->int_level
= qemu_get_be32(f
);
165 qemu_get_ptimer(f
, s
->timer
);
169 static void *arm_timer_init(uint32_t freq
, qemu_irq irq
)
174 s
= (arm_timer_state
*)qemu_mallocz(sizeof(arm_timer_state
));
177 s
->control
= TIMER_CTRL_IE
;
179 bh
= qemu_bh_new(arm_timer_tick
, s
);
180 s
->timer
= ptimer_init(bh
);
181 register_savevm("arm_timer", -1, 1, arm_timer_save
, arm_timer_load
, s
);
185 /* ARM PrimeCell SP804 dual timer module.
186 Docs for this device don't seem to be publicly available. This
187 implementation is based on guesswork, the linux kernel sources and the
188 Integrator/CP timer modules. */
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 uint32_t sp804_read(void *opaque
, target_phys_addr_t offset
)
207 sp804_state
*s
= (sp804_state
*)opaque
;
209 /* ??? Don't know the PrimeCell ID for this device. */
211 return arm_timer_read(s
->timer
[0], offset
);
213 return arm_timer_read(s
->timer
[1], offset
- 0x20);
217 static void sp804_write(void *opaque
, target_phys_addr_t offset
,
220 sp804_state
*s
= (sp804_state
*)opaque
;
223 arm_timer_write(s
->timer
[0], offset
, value
);
225 arm_timer_write(s
->timer
[1], offset
- 0x20, value
);
229 static CPUReadMemoryFunc
*sp804_readfn
[] = {
235 static CPUWriteMemoryFunc
*sp804_writefn
[] = {
241 static void sp804_save(QEMUFile
*f
, void *opaque
)
243 sp804_state
*s
= (sp804_state
*)opaque
;
244 qemu_put_be32(f
, s
->level
[0]);
245 qemu_put_be32(f
, s
->level
[1]);
248 static int sp804_load(QEMUFile
*f
, void *opaque
, int version_id
)
250 sp804_state
*s
= (sp804_state
*)opaque
;
255 s
->level
[0] = qemu_get_be32(f
);
256 s
->level
[1] = qemu_get_be32(f
);
260 void sp804_init(uint32_t base
, qemu_irq irq
)
266 s
= (sp804_state
*)qemu_mallocz(sizeof(sp804_state
));
267 qi
= qemu_allocate_irqs(sp804_set_irq
, s
, 2);
269 /* ??? The timers are actually configurable between 32kHz and 1MHz, but
270 we don't implement that. */
271 s
->timer
[0] = arm_timer_init(1000000, qi
[0]);
272 s
->timer
[1] = arm_timer_init(1000000, qi
[1]);
273 iomemtype
= cpu_register_io_memory(0, sp804_readfn
,
275 cpu_register_physical_memory(base
, 0x00001000, iomemtype
);
276 register_savevm("sp804", -1, 1, sp804_save
, sp804_load
, s
);
280 /* Integrator/CP timer module. */
286 static uint32_t icp_pit_read(void *opaque
, target_phys_addr_t offset
)
288 icp_pit_state
*s
= (icp_pit_state
*)opaque
;
291 /* ??? Don't know the PrimeCell ID for this device. */
294 cpu_abort(cpu_single_env
, "sp804_read: Bad timer %d\n", n
);
296 return arm_timer_read(s
->timer
[n
], offset
& 0xff);
299 static void icp_pit_write(void *opaque
, target_phys_addr_t offset
,
302 icp_pit_state
*s
= (icp_pit_state
*)opaque
;
307 cpu_abort(cpu_single_env
, "sp804_write: Bad timer %d\n", n
);
309 arm_timer_write(s
->timer
[n
], offset
& 0xff, value
);
313 static CPUReadMemoryFunc
*icp_pit_readfn
[] = {
319 static CPUWriteMemoryFunc
*icp_pit_writefn
[] = {
325 void icp_pit_init(uint32_t base
, qemu_irq
*pic
, int irq
)
330 s
= (icp_pit_state
*)qemu_mallocz(sizeof(icp_pit_state
));
331 /* Timer 0 runs at the system clock speed (40MHz). */
332 s
->timer
[0] = arm_timer_init(40000000, pic
[irq
]);
333 /* The other two timers run at 1MHz. */
334 s
->timer
[1] = arm_timer_init(1000000, pic
[irq
+ 1]);
335 s
->timer
[2] = arm_timer_init(1000000, pic
[irq
+ 2]);
337 iomemtype
= cpu_register_io_memory(0, icp_pit_readfn
,
339 cpu_register_physical_memory(base
, 0x00001000, iomemtype
);
340 /* This device has no state to save/restore. The component timers will