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)
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 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 cpu_abort (cpu_single_env, "arm_timer_read: Bad offset %x\n",
70 /* Reset the timer limit after settings have changed. */
71 static void arm_timer_recalibrate(arm_timer_state *s, int reload)
75 if ((s->control & TIMER_CTRL_PERIODIC) == 0) {
77 if (s->control & TIMER_CTRL_32BIT)
85 ptimer_set_limit(s->timer, limit, reload);
88 static void arm_timer_write(void *opaque, target_phys_addr_t offset,
91 arm_timer_state *s = (arm_timer_state *)opaque;
94 switch (offset >> 2) {
95 case 0: /* TimerLoad */
97 arm_timer_recalibrate(s, 1);
99 case 1: /* TimerValue */
100 /* ??? Linux seems to want to write to this readonly register.
103 case 2: /* TimerControl */
104 if (s->control & TIMER_CTRL_ENABLE) {
105 /* Pause the timer if it is running. This may cause some
106 inaccuracy dure to rounding, but avoids a whole lot of other
108 ptimer_stop(s->timer);
112 /* ??? Need to recalculate expiry time after changing divisor. */
113 switch ((value >> 2) & 3) {
114 case 1: freq >>= 4; break;
115 case 2: freq >>= 8; break;
117 arm_timer_recalibrate(s, 0);
118 ptimer_set_freq(s->timer, freq);
119 if (s->control & TIMER_CTRL_ENABLE) {
120 /* Restart the timer if still enabled. */
121 ptimer_run(s->timer, (s->control & TIMER_CTRL_ONESHOT) != 0);
124 case 3: /* TimerIntClr */
127 case 6: /* TimerBGLoad */
129 arm_timer_recalibrate(s, 0);
132 cpu_abort (cpu_single_env, "arm_timer_write: Bad offset %x\n",
138 static void arm_timer_tick(void *opaque)
140 arm_timer_state *s = (arm_timer_state *)opaque;
145 static void *arm_timer_init(uint32_t freq, qemu_irq irq)
150 s = (arm_timer_state *)qemu_mallocz(sizeof(arm_timer_state));
153 s->control = TIMER_CTRL_IE;
155 bh = qemu_bh_new(arm_timer_tick, s);
156 s->timer = ptimer_init(bh);
157 /* ??? Save/restore. */
161 /* ARM PrimeCell SP804 dual timer module.
162 Docs for this device don't seem to be publicly available. This
163 implementation is based on guesswork, the linux kernel sources and the
164 Integrator/CP timer modules. */
173 /* Merge the IRQs from the two component devices. */
174 static void sp804_set_irq(void *opaque, int irq, int level)
176 sp804_state *s = (sp804_state *)opaque;
178 s->level[irq] = level;
179 qemu_set_irq(s->irq, s->level[0] || s->level[1]);
182 static uint32_t sp804_read(void *opaque, target_phys_addr_t offset)
184 sp804_state *s = (sp804_state *)opaque;
186 /* ??? Don't know the PrimeCell ID for this device. */
189 return arm_timer_read(s->timer[0], offset);
191 return arm_timer_read(s->timer[1], offset - 0x20);
195 static void sp804_write(void *opaque, target_phys_addr_t offset,
198 sp804_state *s = (sp804_state *)opaque;
202 arm_timer_write(s->timer[0], offset, value);
204 arm_timer_write(s->timer[1], offset - 0x20, value);
208 static CPUReadMemoryFunc *sp804_readfn[] = {
214 static CPUWriteMemoryFunc *sp804_writefn[] = {
220 void sp804_init(uint32_t base, qemu_irq irq)
226 s = (sp804_state *)qemu_mallocz(sizeof(sp804_state));
227 qi = qemu_allocate_irqs(sp804_set_irq, s, 2);
230 /* ??? The timers are actually configurable between 32kHz and 1MHz, but
231 we don't implement that. */
232 s->timer[0] = arm_timer_init(1000000, qi[0]);
233 s->timer[1] = arm_timer_init(1000000, qi[1]);
234 iomemtype = cpu_register_io_memory(0, sp804_readfn,
236 cpu_register_physical_memory(base, 0x00001000, iomemtype);
237 /* ??? Save/restore. */
241 /* Integrator/CP timer module. */
248 static uint32_t icp_pit_read(void *opaque, target_phys_addr_t offset)
250 icp_pit_state *s = (icp_pit_state *)opaque;
253 /* ??? Don't know the PrimeCell ID for this device. */
257 cpu_abort(cpu_single_env, "sp804_read: Bad timer %d\n", n);
259 return arm_timer_read(s->timer[n], offset & 0xff);
262 static void icp_pit_write(void *opaque, target_phys_addr_t offset,
265 icp_pit_state *s = (icp_pit_state *)opaque;
271 cpu_abort(cpu_single_env, "sp804_write: Bad timer %d\n", n);
273 arm_timer_write(s->timer[n], offset & 0xff, value);
277 static CPUReadMemoryFunc *icp_pit_readfn[] = {
283 static CPUWriteMemoryFunc *icp_pit_writefn[] = {
289 void icp_pit_init(uint32_t base, qemu_irq *pic, int irq)
294 s = (icp_pit_state *)qemu_mallocz(sizeof(icp_pit_state));
296 /* Timer 0 runs at the system clock speed (40MHz). */
297 s->timer[0] = arm_timer_init(40000000, pic[irq]);
298 /* The other two timers run at 1MHz. */
299 s->timer[1] = arm_timer_init(1000000, pic[irq + 1]);
300 s->timer[2] = arm_timer_init(1000000, pic[irq + 2]);
302 iomemtype = cpu_register_io_memory(0, icp_pit_readfn,
304 cpu_register_physical_memory(base, 0x00001000, iomemtype);
305 /* ??? Save/restore. */