2 * ARM PrimeCell Timer modules.
4 * Copyright (c) 2005-2006 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licenced under the GPL.
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)
39 /* Calculate the new expiry time of the given timer. */
41 static void arm_timer_reload(arm_timer_state *s)
45 s->loaded = s->expires;
46 delay = muldiv64(s->count, ticks_per_sec, s->freq);
52 /* Check all active timers, and schedule the next timer interrupt. */
54 static void arm_timer_update(arm_timer_state *s, int64_t now)
58 /* Ignore disabled timers. */
59 if ((s->control & TIMER_CTRL_ENABLE) == 0)
61 /* Ignore expired one-shot timers. */
62 if (s->count == 0 && (s->control & TIMER_CTRL_ONESHOT))
64 if (s->expires - now <= 0) {
65 /* Timer has expired. */
67 if (s->control & TIMER_CTRL_ONESHOT) {
71 if ((s->control & TIMER_CTRL_PERIODIC) == 0) {
73 if (s->control & TIMER_CTRL_32BIT)
74 s->count = 0xffffffff;
83 while (s->expires - now <= 0) {
86 /* Update interrupts. */
87 if (s->int_level && (s->control & TIMER_CTRL_IE)) {
88 pic_set_irq_new(s->pic, s->irq, 1);
90 pic_set_irq_new(s->pic, s->irq, 0);
94 if (next - s->expires < 0)
97 /* Schedule the next timer interrupt. */
99 qemu_del_timer(s->timer);
101 } else if (next != s->next_time) {
102 qemu_mod_timer(s->timer, next);
107 /* Return the current value of the timer. */
108 static uint32_t arm_timer_getcount(arm_timer_state *s, int64_t now)
115 if ((s->control & TIMER_CTRL_ENABLE) == 0)
117 left = s->expires - now;
118 period = s->expires - s->loaded;
119 /* If the timer should have expired then return 0. This can happen
120 when the host timer signal doesnt occur immediately. It's better to
121 have a timer appear to sit at zero for a while than have it wrap
122 around before the guest interrupt is raised. */
123 /* ??? Could we trigger the interrupt here? */
126 /* We need to calculate count * elapsed / period without overfowing.
127 Scale both elapsed and period so they fit in a 32-bit int. */
128 while (period != (int32_t)period) {
132 return ((uint64_t)s->count * (uint64_t)(int32_t)left)
136 uint32_t arm_timer_read(void *opaque, target_phys_addr_t offset)
138 arm_timer_state *s = (arm_timer_state *)opaque;
140 switch (offset >> 2) {
141 case 0: /* TimerLoad */
142 case 6: /* TimerBGLoad */
144 case 1: /* TimerValue */
145 return arm_timer_getcount(s, qemu_get_clock(vm_clock));
146 case 2: /* TimerControl */
148 case 4: /* TimerRIS */
150 case 5: /* TimerMIS */
151 if ((s->control & TIMER_CTRL_IE) == 0)
155 cpu_abort (cpu_single_env, "arm_timer_read: Bad offset %x\n", offset);
160 static void arm_timer_write(void *opaque, target_phys_addr_t offset,
163 arm_timer_state *s = (arm_timer_state *)opaque;
166 now = qemu_get_clock(vm_clock);
167 switch (offset >> 2) {
168 case 0: /* TimerLoad */
174 case 1: /* TimerValue */
175 /* ??? Linux seems to want to write to this readonly register.
178 case 2: /* TimerControl */
179 if (s->control & TIMER_CTRL_ENABLE) {
180 /* Pause the timer if it is running. This may cause some
181 inaccuracy dure to rounding, but avoids a whole lot of other
183 s->count = arm_timer_getcount(s, now);
186 s->freq = s->raw_freq;
187 /* ??? Need to recalculate expiry time after changing divisor. */
188 switch ((value >> 2) & 3) {
189 case 1: s->freq >>= 4; break;
190 case 2: s->freq >>= 8; break;
192 if (s->control & TIMER_CTRL_ENABLE) {
193 /* Restart the timer if still enabled. */
198 case 3: /* TimerIntClr */
201 case 6: /* TimerBGLoad */
205 cpu_abort (cpu_single_env, "arm_timer_write: Bad offset %x\n", offset);
207 arm_timer_update(s, now);
210 static void arm_timer_tick(void *opaque)
214 now = qemu_get_clock(vm_clock);
215 arm_timer_update((arm_timer_state *)opaque, now);
218 static void *arm_timer_init(uint32_t freq, void *pic, int irq)
222 s = (arm_timer_state *)qemu_mallocz(sizeof(arm_timer_state));
225 s->raw_freq = s->freq = 1000000;
226 s->control = TIMER_CTRL_IE;
227 s->count = 0xffffffff;
229 s->timer = qemu_new_timer(vm_clock, arm_timer_tick, s);
230 /* ??? Save/restore. */
234 /* ARM PrimeCell SP804 dual timer module.
235 Docs for this device don't seem to be publicly available. This
236 implementation is based on guesswork, the linux kernel sources and the
237 Integrator/CP timer modules. */
240 /* Include a pseudo-PIC device to merge the two interrupt sources. */
241 arm_pic_handler handler;
245 /* The output PIC device. */
250 static void sp804_set_irq(void *opaque, int irq, int level)
252 sp804_state *s = (sp804_state *)opaque;
254 s->level[irq] = level;
255 pic_set_irq_new(s->pic, s->irq, s->level[0] || s->level[1]);
258 static uint32_t sp804_read(void *opaque, target_phys_addr_t offset)
260 sp804_state *s = (sp804_state *)opaque;
262 /* ??? Don't know the PrimeCell ID for this device. */
265 return arm_timer_read(s->timer[0], offset);
267 return arm_timer_read(s->timer[1], offset - 0x20);
271 static void sp804_write(void *opaque, target_phys_addr_t offset,
274 sp804_state *s = (sp804_state *)opaque;
278 arm_timer_write(s->timer[0], offset, value);
280 arm_timer_write(s->timer[1], offset - 0x20, value);
284 static CPUReadMemoryFunc *sp804_readfn[] = {
290 static CPUWriteMemoryFunc *sp804_writefn[] = {
296 void sp804_init(uint32_t base, void *pic, int irq)
301 s = (sp804_state *)qemu_mallocz(sizeof(sp804_state));
302 s->handler = sp804_set_irq;
306 /* ??? The timers are actually configurable between 32kHz and 1MHz, but
307 we don't implement that. */
308 s->timer[0] = arm_timer_init(1000000, s, 0);
309 s->timer[1] = arm_timer_init(1000000, s, 1);
310 iomemtype = cpu_register_io_memory(0, sp804_readfn,
312 cpu_register_physical_memory(base, 0x00000fff, iomemtype);
313 /* ??? Save/restore. */
317 /* Integrator/CP timer module. */
324 static uint32_t icp_pit_read(void *opaque, target_phys_addr_t offset)
326 icp_pit_state *s = (icp_pit_state *)opaque;
329 /* ??? Don't know the PrimeCell ID for this device. */
333 cpu_abort(cpu_single_env, "sp804_read: Bad timer %d\n", n);
335 return arm_timer_read(s->timer[n], offset & 0xff);
338 static void icp_pit_write(void *opaque, target_phys_addr_t offset,
341 icp_pit_state *s = (icp_pit_state *)opaque;
347 cpu_abort(cpu_single_env, "sp804_write: Bad timer %d\n", n);
349 arm_timer_write(s->timer[n], offset & 0xff, value);
353 static CPUReadMemoryFunc *icp_pit_readfn[] = {
359 static CPUWriteMemoryFunc *icp_pit_writefn[] = {
365 void icp_pit_init(uint32_t base, void *pic, int irq)
370 s = (icp_pit_state *)qemu_mallocz(sizeof(icp_pit_state));
372 /* Timer 0 runs at the system clock speed (40MHz). */
373 s->timer[0] = arm_timer_init(40000000, pic, irq);
374 /* The other two timers run at 1MHz. */
375 s->timer[1] = arm_timer_init(1000000, pic, irq + 1);
376 s->timer[2] = arm_timer_init(1000000, pic, irq + 2);
378 iomemtype = cpu_register_io_memory(0, icp_pit_readfn,
380 cpu_register_physical_memory(base, 0x00000fff, iomemtype);
381 /* ??? Save/restore. */