2 * ARM MPCore internal peripheral emulation (common code).
4 * Copyright (c) 2006-2007 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licenced under the GPL.
11 #include "qemu-timer.h"
16 gic_get_current_cpu(void)
18 return cpu_single_env->cpu_index;
23 /* MPCore private memory region. */
33 struct mpcore_priv_state *mpcore;
34 int id; /* Encodes both timer/watchdog and CPU. */
37 typedef struct mpcore_priv_state {
41 mpcore_timer_state timer[8];
47 static inline void mpcore_timer_update_irq(mpcore_timer_state *s)
49 if (s->status & ~s->old_status) {
50 gic_set_pending_private(&s->mpcore->gic, s->id >> 1, 29 + (s->id & 1));
52 s->old_status = s->status;
55 /* Return conversion factor from mpcore timer ticks to qemu timer ticks. */
56 static inline uint32_t mpcore_timer_scale(mpcore_timer_state *s)
58 return (((s->control >> 8) & 0xff) + 1) * 10;
61 static void mpcore_timer_reload(mpcore_timer_state *s, int restart)
66 s->tick = qemu_get_clock(vm_clock);
67 s->tick += (int64_t)s->count * mpcore_timer_scale(s);
68 qemu_mod_timer(s->timer, s->tick);
71 static void mpcore_timer_tick(void *opaque)
73 mpcore_timer_state *s = (mpcore_timer_state *)opaque;
77 mpcore_timer_reload(s, 0);
81 mpcore_timer_update_irq(s);
84 static uint32_t mpcore_timer_read(mpcore_timer_state *s, int offset)
91 case 4: /* Counter. */
92 if (((s->control & 1) == 0) || (s->count == 0))
94 /* Slow and ugly, but hopefully won't happen too often. */
95 val = s->tick - qemu_get_clock(vm_clock);
96 val /= mpcore_timer_scale(s);
100 case 8: /* Control. */
102 case 12: /* Interrupt status. */
109 static void mpcore_timer_write(mpcore_timer_state *s, int offset,
117 case 4: /* Counter. */
118 if ((s->control & 1) && s->count) {
119 /* Cancel the previous timer. */
120 qemu_del_timer(s->timer);
123 if (s->control & 1) {
124 mpcore_timer_reload(s, 1);
127 case 8: /* Control. */
130 if (((old & 1) == 0) && (value & 1)) {
131 if (s->count == 0 && (s->control & 2))
133 mpcore_timer_reload(s, 1);
136 case 12: /* Interrupt status. */
138 mpcore_timer_update_irq(s);
143 static void mpcore_timer_init(mpcore_priv_state *mpcore,
144 mpcore_timer_state *s, int id)
148 s->timer = qemu_new_timer(vm_clock, mpcore_timer_tick, s);
152 /* Per-CPU private memory mapped IO. */
154 static uint32_t mpcore_priv_read(void *opaque, target_phys_addr_t offset)
156 mpcore_priv_state *s = (mpcore_priv_state *)opaque;
159 if (offset < 0x100) {
162 case 0x00: /* Control. */
163 return s->scu_control;
164 case 0x04: /* Configuration. */
165 id = ((1 << s->num_cpu) - 1) << 4;
166 return id | (s->num_cpu - 1);
167 case 0x08: /* CPU status. */
169 case 0x0c: /* Invalidate all. */
174 } else if (offset < 0x600) {
175 /* Interrupt controller. */
176 if (offset < 0x200) {
177 id = gic_get_current_cpu();
179 id = (offset - 0x200) >> 8;
180 if (id >= s->num_cpu) {
184 return gic_cpu_read(&s->gic, id, offset & 0xff);
185 } else if (offset < 0xb00) {
187 if (offset < 0x700) {
188 id = gic_get_current_cpu();
190 id = (offset - 0x700) >> 8;
191 if (id >= s->num_cpu) {
198 return mpcore_timer_read(&s->timer[id], offset & 0xf);
201 hw_error("mpcore_priv_read: Bad offset %x\n", (int)offset);
205 static void mpcore_priv_write(void *opaque, target_phys_addr_t offset,
208 mpcore_priv_state *s = (mpcore_priv_state *)opaque;
211 if (offset < 0x100) {
214 case 0: /* Control register. */
215 s->scu_control = value & 1;
217 case 0x0c: /* Invalidate all. */
218 /* This is a no-op as cache is not emulated. */
223 } else if (offset < 0x600) {
224 /* Interrupt controller. */
225 if (offset < 0x200) {
226 id = gic_get_current_cpu();
228 id = (offset - 0x200) >> 8;
230 if (id < s->num_cpu) {
231 gic_cpu_write(&s->gic, id, offset & 0xff, value);
233 } else if (offset < 0xb00) {
235 if (offset < 0x700) {
236 id = gic_get_current_cpu();
238 id = (offset - 0x700) >> 8;
240 if (id < s->num_cpu) {
244 mpcore_timer_write(&s->timer[id], offset & 0xf, value);
250 hw_error("mpcore_priv_read: Bad offset %x\n", (int)offset);
253 static CPUReadMemoryFunc * const mpcore_priv_readfn[] = {
259 static CPUWriteMemoryFunc * const mpcore_priv_writefn[] = {
265 static void mpcore_priv_map(SysBusDevice *dev, target_phys_addr_t base)
267 mpcore_priv_state *s = FROM_SYSBUSGIC(mpcore_priv_state, dev);
268 cpu_register_physical_memory(base, 0x1000, s->iomemtype);
269 cpu_register_physical_memory(base + 0x1000, 0x1000, s->gic.iomemtype);
272 static int mpcore_priv_init(SysBusDevice *dev)
274 mpcore_priv_state *s = FROM_SYSBUSGIC(mpcore_priv_state, dev);
277 gic_init(&s->gic, s->num_cpu);
278 s->iomemtype = cpu_register_io_memory(mpcore_priv_readfn,
279 mpcore_priv_writefn, s,
280 DEVICE_NATIVE_ENDIAN);
281 sysbus_init_mmio_cb(dev, 0x2000, mpcore_priv_map);
282 for (i = 0; i < s->num_cpu * 2; i++) {
283 mpcore_timer_init(s, &s->timer[i], i);