2 * ARM MPCore internal peripheral emulation (common code).
4 * Copyright (c) 2006-2007 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licensed 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];
44 MemoryRegion container
;
49 static inline void mpcore_timer_update_irq(mpcore_timer_state
*s
)
51 if (s
->status
& ~s
->old_status
) {
52 gic_set_pending_private(&s
->mpcore
->gic
, s
->id
>> 1, 29 + (s
->id
& 1));
54 s
->old_status
= s
->status
;
57 /* Return conversion factor from mpcore timer ticks to qemu timer ticks. */
58 static inline uint32_t mpcore_timer_scale(mpcore_timer_state
*s
)
60 return (((s
->control
>> 8) & 0xff) + 1) * 10;
63 static void mpcore_timer_reload(mpcore_timer_state
*s
, int restart
)
68 s
->tick
= qemu_get_clock_ns(vm_clock
);
69 s
->tick
+= (int64_t)s
->count
* mpcore_timer_scale(s
);
70 qemu_mod_timer(s
->timer
, s
->tick
);
73 static void mpcore_timer_tick(void *opaque
)
75 mpcore_timer_state
*s
= (mpcore_timer_state
*)opaque
;
79 mpcore_timer_reload(s
, 0);
83 mpcore_timer_update_irq(s
);
86 static uint32_t mpcore_timer_read(mpcore_timer_state
*s
, int offset
)
93 case 4: /* Counter. */
94 if (((s
->control
& 1) == 0) || (s
->count
== 0))
96 /* Slow and ugly, but hopefully won't happen too often. */
97 val
= s
->tick
- qemu_get_clock_ns(vm_clock
);
98 val
/= mpcore_timer_scale(s
);
102 case 8: /* Control. */
104 case 12: /* Interrupt status. */
111 static void mpcore_timer_write(mpcore_timer_state
*s
, int offset
,
119 case 4: /* Counter. */
120 if ((s
->control
& 1) && s
->count
) {
121 /* Cancel the previous timer. */
122 qemu_del_timer(s
->timer
);
125 if (s
->control
& 1) {
126 mpcore_timer_reload(s
, 1);
129 case 8: /* Control. */
132 if (((old
& 1) == 0) && (value
& 1)) {
133 if (s
->count
== 0 && (s
->control
& 2))
135 mpcore_timer_reload(s
, 1);
138 case 12: /* Interrupt status. */
140 mpcore_timer_update_irq(s
);
145 static void mpcore_timer_init(mpcore_priv_state
*mpcore
,
146 mpcore_timer_state
*s
, int id
)
150 s
->timer
= qemu_new_timer_ns(vm_clock
, mpcore_timer_tick
, s
);
154 /* Per-CPU private memory mapped IO. */
156 static uint64_t mpcore_priv_read(void *opaque
, target_phys_addr_t offset
,
159 mpcore_priv_state
*s
= (mpcore_priv_state
*)opaque
;
162 if (offset
< 0x100) {
165 case 0x00: /* Control. */
166 return s
->scu_control
;
167 case 0x04: /* Configuration. */
168 id
= ((1 << s
->num_cpu
) - 1) << 4;
169 return id
| (s
->num_cpu
- 1);
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;
183 if (id
>= s
->num_cpu
) {
187 return gic_cpu_read(&s
->gic
, id
, offset
& 0xff);
188 } else if (offset
< 0xb00) {
190 if (offset
< 0x700) {
191 id
= gic_get_current_cpu();
193 id
= (offset
- 0x700) >> 8;
194 if (id
>= s
->num_cpu
) {
201 return mpcore_timer_read(&s
->timer
[id
], offset
& 0xf);
204 hw_error("mpcore_priv_read: Bad offset %x\n", (int)offset
);
208 static void mpcore_priv_write(void *opaque
, target_phys_addr_t offset
,
209 uint64_t value
, unsigned size
)
211 mpcore_priv_state
*s
= (mpcore_priv_state
*)opaque
;
214 if (offset
< 0x100) {
217 case 0: /* Control register. */
218 s
->scu_control
= value
& 1;
220 case 0x0c: /* Invalidate all. */
221 /* This is a no-op as cache is not emulated. */
226 } else if (offset
< 0x600) {
227 /* Interrupt controller. */
228 if (offset
< 0x200) {
229 id
= gic_get_current_cpu();
231 id
= (offset
- 0x200) >> 8;
233 if (id
< s
->num_cpu
) {
234 gic_cpu_write(&s
->gic
, id
, offset
& 0xff, value
);
236 } else if (offset
< 0xb00) {
238 if (offset
< 0x700) {
239 id
= gic_get_current_cpu();
241 id
= (offset
- 0x700) >> 8;
243 if (id
< s
->num_cpu
) {
247 mpcore_timer_write(&s
->timer
[id
], offset
& 0xf, value
);
253 hw_error("mpcore_priv_read: Bad offset %x\n", (int)offset
);
256 static const MemoryRegionOps mpcore_priv_ops
= {
257 .read
= mpcore_priv_read
,
258 .write
= mpcore_priv_write
,
259 .endianness
= DEVICE_NATIVE_ENDIAN
,
262 static void mpcore_priv_map_setup(mpcore_priv_state
*s
)
264 memory_region_init(&s
->container
, "mpcode-priv-container", 0x2000);
265 memory_region_init_io(&s
->iomem
, &mpcore_priv_ops
, s
, "mpcode-priv",
267 memory_region_add_subregion(&s
->container
, 0, &s
->iomem
);
268 memory_region_add_subregion(&s
->container
, 0x1000, &s
->gic
.iomem
);
271 static int mpcore_priv_init(SysBusDevice
*dev
)
273 mpcore_priv_state
*s
= FROM_SYSBUSGIC(mpcore_priv_state
, dev
);
276 gic_init(&s
->gic
, s
->num_cpu
);
277 mpcore_priv_map_setup(s
);
278 sysbus_init_mmio_region(dev
, &s
->container
);
279 for (i
= 0; i
< s
->num_cpu
* 2; i
++) {
280 mpcore_timer_init(s
, &s
->timer
[i
], i
);