2 * ARM11MPCore internal peripheral emulation.
4 * Copyright (c) 2006-2007 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licensed under the GPL.
11 #include "qemu-timer.h"
13 /* ??? The MPCore TRM says the on-chip controller has 224 external IRQ lines
14 (+ 32 internal). However my test chip only exposes/reports 32.
15 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. */
30 typedef struct mpcore_priv_state
{
34 uint32_t old_timer_status
[8];
38 MemoryRegion container
;
42 /* Per-CPU private memory mapped IO. */
44 static uint64_t mpcore_scu_read(void *opaque
, target_phys_addr_t offset
,
47 mpcore_priv_state
*s
= (mpcore_priv_state
*)opaque
;
52 case 0x00: /* Control. */
53 return s
->scu_control
;
54 case 0x04: /* Configuration. */
55 id
= ((1 << s
->num_cpu
) - 1) << 4;
56 return id
| (s
->num_cpu
- 1);
57 case 0x08: /* CPU status. */
59 case 0x0c: /* Invalidate all. */
62 hw_error("mpcore_priv_read: Bad offset %x\n", (int)offset
);
66 static void mpcore_scu_write(void *opaque
, target_phys_addr_t offset
,
67 uint64_t value
, unsigned size
)
69 mpcore_priv_state
*s
= (mpcore_priv_state
*)opaque
;
73 case 0: /* Control register. */
74 s
->scu_control
= value
& 1;
76 case 0x0c: /* Invalidate all. */
77 /* This is a no-op as cache is not emulated. */
80 hw_error("mpcore_priv_read: Bad offset %x\n", (int)offset
);
84 static const MemoryRegionOps mpcore_scu_ops
= {
85 .read
= mpcore_scu_read
,
86 .write
= mpcore_scu_write
,
87 .endianness
= DEVICE_NATIVE_ENDIAN
,
90 static void mpcore_timer_irq_handler(void *opaque
, int irq
, int level
)
92 mpcore_priv_state
*s
= (mpcore_priv_state
*)opaque
;
93 if (level
&& !s
->old_timer_status
[irq
]) {
94 gic_set_pending_private(&s
->gic
, irq
>> 1, 29 + (irq
& 1));
96 s
->old_timer_status
[irq
] = level
;
99 static void mpcore_priv_map_setup(mpcore_priv_state
*s
)
102 SysBusDevice
*busdev
= sysbus_from_qdev(s
->mptimer
);
103 memory_region_init(&s
->container
, "mpcode-priv-container", 0x2000);
104 memory_region_init_io(&s
->iomem
, &mpcore_scu_ops
, s
, "mpcore-scu", 0x100);
105 memory_region_add_subregion(&s
->container
, 0, &s
->iomem
);
106 /* GIC CPU interfaces: "current CPU" at 0x100, then specific CPUs
109 for (i
= 0; i
< (s
->num_cpu
+ 1); i
++) {
110 target_phys_addr_t offset
= 0x100 + (i
* 0x100);
111 memory_region_add_subregion(&s
->container
, offset
, &s
->gic
.cpuiomem
[i
]);
113 /* Add the regions for timer and watchdog for "current CPU" and
114 * for each specific CPU.
116 s
->timer_irq
= qemu_allocate_irqs(mpcore_timer_irq_handler
,
117 s
, (s
->num_cpu
+ 1) * 2);
118 for (i
= 0; i
< (s
->num_cpu
+ 1) * 2; i
++) {
119 /* Timers at 0x600, 0x700, ...; watchdogs at 0x620, 0x720, ... */
120 target_phys_addr_t offset
= 0x600 + (i
>> 1) * 0x100 + (i
& 1) * 0x20;
121 memory_region_add_subregion(&s
->container
, offset
,
122 sysbus_mmio_get_region(busdev
, i
));
124 memory_region_add_subregion(&s
->container
, 0x1000, &s
->gic
.iomem
);
125 /* Wire up the interrupt from each watchdog and timer. */
126 for (i
= 0; i
< s
->num_cpu
* 2; i
++) {
127 sysbus_connect_irq(busdev
, i
, s
->timer_irq
[i
]);
131 static int mpcore_priv_init(SysBusDevice
*dev
)
133 mpcore_priv_state
*s
= FROM_SYSBUSGIC(mpcore_priv_state
, dev
);
135 gic_init(&s
->gic
, s
->num_cpu
);
136 s
->mptimer
= qdev_create(NULL
, "arm_mptimer");
137 qdev_prop_set_uint32(s
->mptimer
, "num-cpu", s
->num_cpu
);
138 qdev_init_nofail(s
->mptimer
);
139 mpcore_priv_map_setup(s
);
140 sysbus_init_mmio(dev
, &s
->container
);
144 /* Dummy PIC to route IRQ lines. The baseboard has 4 independent IRQ
145 controllers. The output of these, plus some of the raw input lines
146 are fed into a single SMP-aware interrupt controller on the CPU. */
151 qemu_irq rvic
[4][64];
155 /* Map baseboard IRQs onto CPU IRQ lines. */
156 static const int mpcore_irq_map
[32] = {
157 -1, -1, -1, -1, 1, 2, -1, -1,
158 -1, -1, 6, -1, 4, 5, -1, -1,
159 -1, 14, 15, 0, 7, 8, -1, -1,
160 -1, -1, -1, -1, 9, 3, -1, -1,
163 static void mpcore_rirq_set_irq(void *opaque
, int irq
, int level
)
165 mpcore_rirq_state
*s
= (mpcore_rirq_state
*)opaque
;
168 for (i
= 0; i
< 4; i
++) {
169 qemu_set_irq(s
->rvic
[i
][irq
], level
);
172 irq
= mpcore_irq_map
[irq
];
174 qemu_set_irq(s
->cpuic
[irq
], level
);
179 static int realview_mpcore_init(SysBusDevice
*dev
)
181 mpcore_rirq_state
*s
= FROM_SYSBUS(mpcore_rirq_state
, dev
);
187 priv
= qdev_create(NULL
, "arm11mpcore_priv");
188 qdev_prop_set_uint32(priv
, "num-cpu", s
->num_cpu
);
189 qdev_init_nofail(priv
);
190 s
->priv
= sysbus_from_qdev(priv
);
191 sysbus_pass_irq(dev
, s
->priv
);
192 for (i
= 0; i
< 32; i
++) {
193 s
->cpuic
[i
] = qdev_get_gpio_in(priv
, i
);
195 /* ??? IRQ routing is hardcoded to "normal" mode. */
196 for (n
= 0; n
< 4; n
++) {
197 gic
= sysbus_create_simple("realview_gic", 0x10040000 + n
* 0x10000,
199 for (i
= 0; i
< 64; i
++) {
200 s
->rvic
[n
][i
] = qdev_get_gpio_in(gic
, i
);
203 qdev_init_gpio_in(&dev
->qdev
, mpcore_rirq_set_irq
, 64);
204 sysbus_init_mmio(dev
, sysbus_mmio_get_region(s
->priv
, 0));
208 static SysBusDeviceInfo mpcore_rirq_info
= {
209 .init
= realview_mpcore_init
,
210 .qdev
.name
= "realview_mpcore",
211 .qdev
.size
= sizeof(mpcore_rirq_state
),
212 .qdev
.props
= (Property
[]) {
213 DEFINE_PROP_UINT32("num-cpu", mpcore_rirq_state
, num_cpu
, 1),
214 DEFINE_PROP_END_OF_LIST(),
218 static SysBusDeviceInfo mpcore_priv_info
= {
219 .init
= mpcore_priv_init
,
220 .qdev
.name
= "arm11mpcore_priv",
221 .qdev
.size
= sizeof(mpcore_priv_state
),
222 .qdev
.props
= (Property
[]) {
223 DEFINE_PROP_UINT32("num-cpu", mpcore_priv_state
, num_cpu
, 1),
224 DEFINE_PROP_END_OF_LIST(),
228 static void arm11mpcore_register_devices(void)
230 sysbus_register_withprop(&mpcore_rirq_info
);
231 sysbus_register_withprop(&mpcore_priv_info
);
234 device_init(arm11mpcore_register_devices
)