2 * ARM MPCore internal peripheral emulation.
4 * Copyright (c) 2006-2007 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licenced under the GPL.
11 #include "qemu-timer.h"
13 #define MPCORE_PRIV_BASE 0x10100000
15 /* ??? The MPCore TRM says the on-chip controller has 224 external IRQ lines
16 (+ 32 internal). However my test chip only exposes/reports 32.
17 More importantly Linux falls over if more than 32 are present! */
21 gic_get_current_cpu(void)
23 return cpu_single_env->cpu_index;
28 /* MPCore private memory region. */
38 struct mpcore_priv_state *mpcore;
39 int id; /* Encodes both timer/watchdog and CPU. */
42 typedef struct mpcore_priv_state {
46 mpcore_timer_state timer[8];
51 static inline void mpcore_timer_update_irq(mpcore_timer_state *s)
53 if (s->status & ~s->old_status) {
54 gic_set_pending_private(&s->mpcore->gic, s->id >> 1, 29 + (s->id & 1));
56 s->old_status = s->status;
59 /* Return conversion factor from mpcore timer ticks to qemu timer ticks. */
60 static inline uint32_t mpcore_timer_scale(mpcore_timer_state *s)
62 return (((s->control >> 8) & 0xff) + 1) * 10;
65 static void mpcore_timer_reload(mpcore_timer_state *s, int restart)
70 s->tick = qemu_get_clock(vm_clock);
71 s->tick += (int64_t)s->count * mpcore_timer_scale(s);
72 qemu_mod_timer(s->timer, s->tick);
75 static void mpcore_timer_tick(void *opaque)
77 mpcore_timer_state *s = (mpcore_timer_state *)opaque;
81 mpcore_timer_reload(s, 0);
85 mpcore_timer_update_irq(s);
88 static uint32_t mpcore_timer_read(mpcore_timer_state *s, int offset)
95 case 4: /* Counter. */
96 if (((s->control & 1) == 0) || (s->count == 0))
98 /* Slow and ugly, but hopefully won't happen too often. */
99 val = s->tick - qemu_get_clock(vm_clock);
100 val /= mpcore_timer_scale(s);
104 case 8: /* Control. */
106 case 12: /* Interrupt status. */
113 static void mpcore_timer_write(mpcore_timer_state *s, int offset,
121 case 4: /* Counter. */
122 if ((s->control & 1) && s->count) {
123 /* Cancel the previous timer. */
124 qemu_del_timer(s->timer);
127 if (s->control & 1) {
128 mpcore_timer_reload(s, 1);
131 case 8: /* Control. */
134 if (((old & 1) == 0) && (value & 1)) {
135 if (s->count == 0 && (s->control & 2))
137 mpcore_timer_reload(s, 1);
140 case 12: /* Interrupt status. */
142 mpcore_timer_update_irq(s);
147 static void mpcore_timer_init(mpcore_priv_state *mpcore,
148 mpcore_timer_state *s, int id)
152 s->timer = qemu_new_timer(vm_clock, mpcore_timer_tick, s);
156 /* Per-CPU private memory mapped IO. */
158 static uint32_t mpcore_priv_read(void *opaque, target_phys_addr_t offset)
160 mpcore_priv_state *s = (mpcore_priv_state *)opaque;
163 if (offset < 0x100) {
166 case 0x00: /* Control. */
167 return s->scu_control;
168 case 0x04: /* Configuration. */
170 case 0x08: /* CPU status. */
172 case 0x0c: /* Invalidate all. */
177 } else if (offset < 0x600) {
178 /* Interrupt controller. */
179 if (offset < 0x200) {
180 id = gic_get_current_cpu();
182 id = (offset - 0x200) >> 8;
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;
195 return mpcore_timer_read(&s->timer[id], offset & 0xf);
198 hw_error("mpcore_priv_read: Bad offset %x\n", (int)offset);
202 static void mpcore_priv_write(void *opaque, target_phys_addr_t offset,
205 mpcore_priv_state *s = (mpcore_priv_state *)opaque;
208 if (offset < 0x100) {
211 case 0: /* Control register. */
212 s->scu_control = value & 1;
214 case 0x0c: /* Invalidate all. */
215 /* This is a no-op as cache is not emulated. */
220 } else if (offset < 0x600) {
221 /* Interrupt controller. */
222 if (offset < 0x200) {
223 id = gic_get_current_cpu();
225 id = (offset - 0x200) >> 8;
227 gic_cpu_write(&s->gic, id, offset & 0xff, value);
228 } else if (offset < 0xb00) {
230 if (offset < 0x700) {
231 id = gic_get_current_cpu();
233 id = (offset - 0x700) >> 8;
238 mpcore_timer_write(&s->timer[id], offset & 0xf, value);
243 hw_error("mpcore_priv_read: Bad offset %x\n", (int)offset);
246 static CPUReadMemoryFunc *mpcore_priv_readfn[] = {
252 static CPUWriteMemoryFunc *mpcore_priv_writefn[] = {
258 static void mpcore_priv_map(SysBusDevice *dev, target_phys_addr_t base)
260 mpcore_priv_state *s = FROM_SYSBUSGIC(mpcore_priv_state, dev);
261 cpu_register_physical_memory(base, 0x1000, s->iomemtype);
262 cpu_register_physical_memory(base + 0x1000, 0x1000, s->gic.iomemtype);
265 static void mpcore_priv_init(SysBusDevice *dev)
267 mpcore_priv_state *s = FROM_SYSBUSGIC(mpcore_priv_state, dev);
271 s->iomemtype = cpu_register_io_memory(mpcore_priv_readfn,
272 mpcore_priv_writefn, s);
273 sysbus_init_mmio_cb(dev, 0x2000, mpcore_priv_map);
274 for (i = 0; i < 8; i++) {
275 mpcore_timer_init(s, &s->timer[i], i);
279 /* Dummy PIC to route IRQ lines. The baseboard has 4 independent IRQ
280 controllers. The output of these, plus some of the raw input lines
281 are fed into a single SMP-aware interrupt controller on the CPU. */
285 qemu_irq rvic[4][64];
288 /* Map baseboard IRQs onto CPU IRQ lines. */
289 static const int mpcore_irq_map[32] = {
290 -1, -1, -1, -1, 1, 2, -1, -1,
291 -1, -1, 6, -1, 4, 5, -1, -1,
292 -1, 14, 15, 0, 7, 8, -1, -1,
293 -1, -1, -1, -1, 9, 3, -1, -1,
296 static void mpcore_rirq_set_irq(void *opaque, int irq, int level)
298 mpcore_rirq_state *s = (mpcore_rirq_state *)opaque;
301 for (i = 0; i < 4; i++) {
302 qemu_set_irq(s->rvic[i][irq], level);
305 irq = mpcore_irq_map[irq];
307 qemu_set_irq(s->cpuic[irq], level);
312 static void realview_mpcore_init(SysBusDevice *dev)
314 mpcore_rirq_state *s = FROM_SYSBUS(mpcore_rirq_state, dev);
320 priv = sysbus_create_simple("arm11mpcore_priv", MPCORE_PRIV_BASE, NULL);
321 sysbus_pass_irq(dev, sysbus_from_qdev(priv));
322 for (i = 0; i < 32; i++) {
323 s->cpuic[i] = qdev_get_gpio_in(priv, i);
325 /* ??? IRQ routing is hardcoded to "normal" mode. */
326 for (n = 0; n < 4; n++) {
327 gic = sysbus_create_simple("realview_gic", 0x10040000 + n * 0x10000,
329 for (i = 0; i < 64; i++) {
330 s->rvic[n][i] = qdev_get_gpio_in(gic, i);
333 qdev_init_gpio_in(&dev->qdev, mpcore_rirq_set_irq, 64);
336 static void mpcore_register_devices(void)
338 sysbus_register_dev("realview_mpcore", sizeof(mpcore_rirq_state),
339 realview_mpcore_init);
340 sysbus_register_dev("arm11mpcore_priv", sizeof(mpcore_priv_state),
344 device_init(mpcore_register_devices)