2 * ARM Integrator CP System emulation.
4 * Copyright (c) 2005-2007 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licensed under the GPL
10 #include "qemu/osdep.h"
11 #include "qapi/error.h"
12 #include "qemu-common.h"
14 #include "hw/sysbus.h"
15 #include "hw/devices.h"
16 #include "hw/boards.h"
17 #include "hw/arm/arm.h"
18 #include "hw/misc/arm_integrator_debug.h"
20 #include "exec/address-spaces.h"
21 #include "sysemu/sysemu.h"
22 #include "qemu/error-report.h"
23 #include "hw/char/pl011.h"
25 #define TYPE_INTEGRATOR_CM "integrator_core"
26 #define INTEGRATOR_CM(obj) \
27 OBJECT_CHECK(IntegratorCMState, (obj), TYPE_INTEGRATOR_CM)
29 typedef struct IntegratorCMState
{
31 SysBusDevice parent_obj
;
45 uint32_t cm_refcnt_offset
;
51 static uint8_t integrator_spd
[128] = {
52 128, 8, 4, 11, 9, 1, 64, 0, 2, 0xa0, 0xa0, 0, 0, 8, 0, 1,
53 0xe, 4, 0x1c, 1, 2, 0x20, 0xc0, 0, 0, 0, 0, 0x30, 0x28, 0x30, 0x28, 0x40
56 static uint64_t integratorcm_read(void *opaque
, hwaddr offset
,
59 IntegratorCMState
*s
= opaque
;
60 if (offset
>= 0x100 && offset
< 0x200) {
64 return integrator_spd
[offset
>> 2];
66 switch (offset
>> 2) {
78 if (s
->cm_lock
== 0xa05f) {
83 case 6: /* CM_LMBUSCNT */
84 /* ??? High frequency timer. */
85 hw_error("integratorcm_read: CM_LMBUSCNT");
86 case 7: /* CM_AUXOSC */
88 case 8: /* CM_SDRAM */
92 case 10: /* CM_REFCNT */
93 /* This register, CM_REFCNT, provides a 32-bit count value.
94 * The count increments at the fixed reference clock frequency of 24MHz
95 * and can be used as a real-time counter.
97 return (uint32_t)muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), 24,
98 1000) - s
->cm_refcnt_offset
;
99 case 12: /* CM_FLAGS */
101 case 14: /* CM_NVFLAGS */
102 return s
->cm_nvflags
;
103 case 16: /* CM_IRQ_STAT */
104 return s
->int_level
& s
->irq_enabled
;
105 case 17: /* CM_IRQ_RSTAT */
107 case 18: /* CM_IRQ_ENSET */
108 return s
->irq_enabled
;
109 case 20: /* CM_SOFT_INTSET */
110 return s
->int_level
& 1;
111 case 24: /* CM_FIQ_STAT */
112 return s
->int_level
& s
->fiq_enabled
;
113 case 25: /* CM_FIQ_RSTAT */
115 case 26: /* CM_FIQ_ENSET */
116 return s
->fiq_enabled
;
117 case 32: /* CM_VOLTAGE_CTL0 */
118 case 33: /* CM_VOLTAGE_CTL1 */
119 case 34: /* CM_VOLTAGE_CTL2 */
120 case 35: /* CM_VOLTAGE_CTL3 */
121 /* ??? Voltage control unimplemented. */
124 hw_error("integratorcm_read: Unimplemented offset 0x%x\n",
130 static void integratorcm_do_remap(IntegratorCMState
*s
)
132 /* Sync memory region state with CM_CTRL REMAP bit:
133 * bit 0 => flash at address 0; bit 1 => RAM
135 memory_region_set_enabled(&s
->flash
, !(s
->cm_ctrl
& 4));
138 static void integratorcm_set_ctrl(IntegratorCMState
*s
, uint32_t value
)
141 qemu_system_reset_request();
143 if ((s
->cm_ctrl
^ value
) & 1) {
144 /* (value & 1) != 0 means the green "MISC LED" is lit.
145 * We don't have any nice place to display LEDs. printf is a bad
146 * idea because Linux uses the LED as a heartbeat and the output
147 * will swamp anything else on the terminal.
150 /* Note that the RESET bit [3] always reads as zero */
151 s
->cm_ctrl
= (s
->cm_ctrl
& ~5) | (value
& 5);
152 integratorcm_do_remap(s
);
155 static void integratorcm_update(IntegratorCMState
*s
)
157 /* ??? The CPU irq/fiq is raised when either the core module or base PIC
159 if (s
->int_level
& (s
->irq_enabled
| s
->fiq_enabled
))
160 hw_error("Core module interrupt\n");
163 static void integratorcm_write(void *opaque
, hwaddr offset
,
164 uint64_t value
, unsigned size
)
166 IntegratorCMState
*s
= opaque
;
167 switch (offset
>> 2) {
169 if (s
->cm_lock
== 0xa05f)
172 case 3: /* CM_CTRL */
173 integratorcm_set_ctrl(s
, value
);
175 case 5: /* CM_LOCK */
176 s
->cm_lock
= value
& 0xffff;
178 case 7: /* CM_AUXOSC */
179 if (s
->cm_lock
== 0xa05f)
180 s
->cm_auxosc
= value
;
182 case 8: /* CM_SDRAM */
185 case 9: /* CM_INIT */
186 /* ??? This can change the memory bus frequency. */
189 case 12: /* CM_FLAGSS */
190 s
->cm_flags
|= value
;
192 case 13: /* CM_FLAGSC */
193 s
->cm_flags
&= ~value
;
195 case 14: /* CM_NVFLAGSS */
196 s
->cm_nvflags
|= value
;
198 case 15: /* CM_NVFLAGSS */
199 s
->cm_nvflags
&= ~value
;
201 case 18: /* CM_IRQ_ENSET */
202 s
->irq_enabled
|= value
;
203 integratorcm_update(s
);
205 case 19: /* CM_IRQ_ENCLR */
206 s
->irq_enabled
&= ~value
;
207 integratorcm_update(s
);
209 case 20: /* CM_SOFT_INTSET */
210 s
->int_level
|= (value
& 1);
211 integratorcm_update(s
);
213 case 21: /* CM_SOFT_INTCLR */
214 s
->int_level
&= ~(value
& 1);
215 integratorcm_update(s
);
217 case 26: /* CM_FIQ_ENSET */
218 s
->fiq_enabled
|= value
;
219 integratorcm_update(s
);
221 case 27: /* CM_FIQ_ENCLR */
222 s
->fiq_enabled
&= ~value
;
223 integratorcm_update(s
);
225 case 32: /* CM_VOLTAGE_CTL0 */
226 case 33: /* CM_VOLTAGE_CTL1 */
227 case 34: /* CM_VOLTAGE_CTL2 */
228 case 35: /* CM_VOLTAGE_CTL3 */
229 /* ??? Voltage control unimplemented. */
232 hw_error("integratorcm_write: Unimplemented offset 0x%x\n",
238 /* Integrator/CM control registers. */
240 static const MemoryRegionOps integratorcm_ops
= {
241 .read
= integratorcm_read
,
242 .write
= integratorcm_write
,
243 .endianness
= DEVICE_NATIVE_ENDIAN
,
246 static void integratorcm_init(Object
*obj
)
248 IntegratorCMState
*s
= INTEGRATOR_CM(obj
);
249 SysBusDevice
*dev
= SYS_BUS_DEVICE(obj
);
251 s
->cm_osc
= 0x01000048;
252 /* ??? What should the high bits of this value be? */
253 s
->cm_auxosc
= 0x0007feff;
254 s
->cm_sdram
= 0x00011122;
255 if (s
->memsz
>= 256) {
256 integrator_spd
[31] = 64;
258 } else if (s
->memsz
>= 128) {
259 integrator_spd
[31] = 32;
261 } else if (s
->memsz
>= 64) {
262 integrator_spd
[31] = 16;
264 } else if (s
->memsz
>= 32) {
265 integrator_spd
[31] = 4;
268 integrator_spd
[31] = 2;
270 memcpy(integrator_spd
+ 73, "QEMU-MEMORY", 11);
271 s
->cm_init
= 0x00000112;
272 s
->cm_refcnt_offset
= muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), 24,
274 memory_region_init_ram(&s
->flash
, obj
, "integrator.flash", 0x100000,
276 vmstate_register_ram_global(&s
->flash
);
278 memory_region_init_io(&s
->iomem
, obj
, &integratorcm_ops
, s
,
279 "integratorcm", 0x00800000);
280 sysbus_init_mmio(dev
, &s
->iomem
);
282 integratorcm_do_remap(s
);
283 /* ??? Save/restore. */
286 /* Integrator/CP hardware emulation. */
287 /* Primary interrupt controller. */
289 #define TYPE_INTEGRATOR_PIC "integrator_pic"
290 #define INTEGRATOR_PIC(obj) \
291 OBJECT_CHECK(icp_pic_state, (obj), TYPE_INTEGRATOR_PIC)
293 typedef struct icp_pic_state
{
295 SysBusDevice parent_obj
;
300 uint32_t irq_enabled
;
301 uint32_t fiq_enabled
;
306 static void icp_pic_update(icp_pic_state
*s
)
310 flags
= (s
->level
& s
->irq_enabled
);
311 qemu_set_irq(s
->parent_irq
, flags
!= 0);
312 flags
= (s
->level
& s
->fiq_enabled
);
313 qemu_set_irq(s
->parent_fiq
, flags
!= 0);
316 static void icp_pic_set_irq(void *opaque
, int irq
, int level
)
318 icp_pic_state
*s
= (icp_pic_state
*)opaque
;
320 s
->level
|= 1 << irq
;
322 s
->level
&= ~(1 << irq
);
326 static uint64_t icp_pic_read(void *opaque
, hwaddr offset
,
329 icp_pic_state
*s
= (icp_pic_state
*)opaque
;
331 switch (offset
>> 2) {
332 case 0: /* IRQ_STATUS */
333 return s
->level
& s
->irq_enabled
;
334 case 1: /* IRQ_RAWSTAT */
336 case 2: /* IRQ_ENABLESET */
337 return s
->irq_enabled
;
338 case 4: /* INT_SOFTSET */
340 case 8: /* FRQ_STATUS */
341 return s
->level
& s
->fiq_enabled
;
342 case 9: /* FRQ_RAWSTAT */
344 case 10: /* FRQ_ENABLESET */
345 return s
->fiq_enabled
;
346 case 3: /* IRQ_ENABLECLR */
347 case 5: /* INT_SOFTCLR */
348 case 11: /* FRQ_ENABLECLR */
350 printf ("icp_pic_read: Bad register offset 0x%x\n", (int)offset
);
355 static void icp_pic_write(void *opaque
, hwaddr offset
,
356 uint64_t value
, unsigned size
)
358 icp_pic_state
*s
= (icp_pic_state
*)opaque
;
360 switch (offset
>> 2) {
361 case 2: /* IRQ_ENABLESET */
362 s
->irq_enabled
|= value
;
364 case 3: /* IRQ_ENABLECLR */
365 s
->irq_enabled
&= ~value
;
367 case 4: /* INT_SOFTSET */
369 icp_pic_set_irq(s
, 0, 1);
371 case 5: /* INT_SOFTCLR */
373 icp_pic_set_irq(s
, 0, 0);
375 case 10: /* FRQ_ENABLESET */
376 s
->fiq_enabled
|= value
;
378 case 11: /* FRQ_ENABLECLR */
379 s
->fiq_enabled
&= ~value
;
381 case 0: /* IRQ_STATUS */
382 case 1: /* IRQ_RAWSTAT */
383 case 8: /* FRQ_STATUS */
384 case 9: /* FRQ_RAWSTAT */
386 printf ("icp_pic_write: Bad register offset 0x%x\n", (int)offset
);
392 static const MemoryRegionOps icp_pic_ops
= {
393 .read
= icp_pic_read
,
394 .write
= icp_pic_write
,
395 .endianness
= DEVICE_NATIVE_ENDIAN
,
398 static void icp_pic_init(Object
*obj
)
400 DeviceState
*dev
= DEVICE(obj
);
401 icp_pic_state
*s
= INTEGRATOR_PIC(obj
);
402 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
404 qdev_init_gpio_in(dev
, icp_pic_set_irq
, 32);
405 sysbus_init_irq(sbd
, &s
->parent_irq
);
406 sysbus_init_irq(sbd
, &s
->parent_fiq
);
407 memory_region_init_io(&s
->iomem
, obj
, &icp_pic_ops
, s
,
408 "icp-pic", 0x00800000);
409 sysbus_init_mmio(sbd
, &s
->iomem
);
412 /* CP control registers. */
414 #define TYPE_ICP_CONTROL_REGS "icp-ctrl-regs"
415 #define ICP_CONTROL_REGS(obj) \
416 OBJECT_CHECK(ICPCtrlRegsState, (obj), TYPE_ICP_CONTROL_REGS)
418 typedef struct ICPCtrlRegsState
{
420 SysBusDevice parent_obj
;
426 uint32_t intreg_state
;
429 #define ICP_GPIO_MMC_WPROT "mmc-wprot"
430 #define ICP_GPIO_MMC_CARDIN "mmc-cardin"
432 #define ICP_INTREG_WPROT (1 << 0)
433 #define ICP_INTREG_CARDIN (1 << 3)
435 static uint64_t icp_control_read(void *opaque
, hwaddr offset
,
438 ICPCtrlRegsState
*s
= opaque
;
440 switch (offset
>> 2) {
441 case 0: /* CP_IDFIELD */
443 case 1: /* CP_FLASHPROG */
445 case 2: /* CP_INTREG */
446 return s
->intreg_state
;
447 case 3: /* CP_DECODE */
450 hw_error("icp_control_read: Bad offset %x\n", (int)offset
);
455 static void icp_control_write(void *opaque
, hwaddr offset
,
456 uint64_t value
, unsigned size
)
458 ICPCtrlRegsState
*s
= opaque
;
460 switch (offset
>> 2) {
461 case 2: /* CP_INTREG */
462 s
->intreg_state
&= ~(value
& ICP_INTREG_CARDIN
);
463 qemu_set_irq(s
->mmc_irq
, !!(s
->intreg_state
& ICP_INTREG_CARDIN
));
465 case 1: /* CP_FLASHPROG */
466 case 3: /* CP_DECODE */
467 /* Nothing interesting implemented yet. */
470 hw_error("icp_control_write: Bad offset %x\n", (int)offset
);
474 static const MemoryRegionOps icp_control_ops
= {
475 .read
= icp_control_read
,
476 .write
= icp_control_write
,
477 .endianness
= DEVICE_NATIVE_ENDIAN
,
480 static void icp_control_mmc_wprot(void *opaque
, int line
, int level
)
482 ICPCtrlRegsState
*s
= opaque
;
484 s
->intreg_state
&= ~ICP_INTREG_WPROT
;
486 s
->intreg_state
|= ICP_INTREG_WPROT
;
490 static void icp_control_mmc_cardin(void *opaque
, int line
, int level
)
492 ICPCtrlRegsState
*s
= opaque
;
494 /* line is released by writing to CP_INTREG */
496 s
->intreg_state
|= ICP_INTREG_CARDIN
;
497 qemu_set_irq(s
->mmc_irq
, 1);
501 static void icp_control_init(Object
*obj
)
503 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
504 ICPCtrlRegsState
*s
= ICP_CONTROL_REGS(obj
);
505 DeviceState
*dev
= DEVICE(obj
);
507 memory_region_init_io(&s
->iomem
, OBJECT(s
), &icp_control_ops
, s
,
508 "icp_ctrl_regs", 0x00800000);
509 sysbus_init_mmio(sbd
, &s
->iomem
);
511 qdev_init_gpio_in_named(dev
, icp_control_mmc_wprot
, ICP_GPIO_MMC_WPROT
, 1);
512 qdev_init_gpio_in_named(dev
, icp_control_mmc_cardin
,
513 ICP_GPIO_MMC_CARDIN
, 1);
514 sysbus_init_irq(sbd
, &s
->mmc_irq
);
520 static struct arm_boot_info integrator_binfo
= {
525 static void integratorcp_init(MachineState
*machine
)
527 ram_addr_t ram_size
= machine
->ram_size
;
528 const char *cpu_model
= machine
->cpu_model
;
529 const char *kernel_filename
= machine
->kernel_filename
;
530 const char *kernel_cmdline
= machine
->kernel_cmdline
;
531 const char *initrd_filename
= machine
->initrd_filename
;
535 MemoryRegion
*address_space_mem
= get_system_memory();
536 MemoryRegion
*ram
= g_new(MemoryRegion
, 1);
537 MemoryRegion
*ram_alias
= g_new(MemoryRegion
, 1);
539 DeviceState
*dev
, *sic
, *icp
;
543 cpu_model
= "arm926";
546 cpu_oc
= cpu_class_by_name(TYPE_ARM_CPU
, cpu_model
);
548 fprintf(stderr
, "Unable to find CPU definition\n");
552 cpuobj
= object_new(object_class_get_name(cpu_oc
));
554 /* By default ARM1176 CPUs have EL3 enabled. This board does not
555 * currently support EL3 so the CPU EL3 property is disabled before
558 if (object_property_find(cpuobj
, "has_el3", NULL
)) {
559 object_property_set_bool(cpuobj
, false, "has_el3", &error_fatal
);
562 object_property_set_bool(cpuobj
, true, "realized", &error_fatal
);
564 cpu
= ARM_CPU(cpuobj
);
566 memory_region_allocate_system_memory(ram
, NULL
, "integrator.ram",
568 /* ??? On a real system the first 1Mb is mapped as SSRAM or boot flash. */
569 /* ??? RAM should repeat to fill physical memory space. */
570 /* SDRAM at address zero*/
571 memory_region_add_subregion(address_space_mem
, 0, ram
);
572 /* And again at address 0x80000000 */
573 memory_region_init_alias(ram_alias
, NULL
, "ram.alias", ram
, 0, ram_size
);
574 memory_region_add_subregion(address_space_mem
, 0x80000000, ram_alias
);
576 dev
= qdev_create(NULL
, TYPE_INTEGRATOR_CM
);
577 qdev_prop_set_uint32(dev
, "memsz", ram_size
>> 20);
578 qdev_init_nofail(dev
);
579 sysbus_mmio_map((SysBusDevice
*)dev
, 0, 0x10000000);
581 dev
= sysbus_create_varargs(TYPE_INTEGRATOR_PIC
, 0x14000000,
582 qdev_get_gpio_in(DEVICE(cpu
), ARM_CPU_IRQ
),
583 qdev_get_gpio_in(DEVICE(cpu
), ARM_CPU_FIQ
),
585 for (i
= 0; i
< 32; i
++) {
586 pic
[i
] = qdev_get_gpio_in(dev
, i
);
588 sic
= sysbus_create_simple(TYPE_INTEGRATOR_PIC
, 0xca000000, pic
[26]);
589 sysbus_create_varargs("integrator_pit", 0x13000000,
590 pic
[5], pic
[6], pic
[7], NULL
);
591 sysbus_create_simple("pl031", 0x15000000, pic
[8]);
592 pl011_create(0x16000000, pic
[1], serial_hds
[0]);
593 pl011_create(0x17000000, pic
[2], serial_hds
[1]);
594 icp
= sysbus_create_simple(TYPE_ICP_CONTROL_REGS
, 0xcb000000,
595 qdev_get_gpio_in(sic
, 3));
596 sysbus_create_simple("pl050_keyboard", 0x18000000, pic
[3]);
597 sysbus_create_simple("pl050_mouse", 0x19000000, pic
[4]);
598 sysbus_create_simple(TYPE_INTEGRATOR_DEBUG
, 0x1a000000, 0);
600 dev
= sysbus_create_varargs("pl181", 0x1c000000, pic
[23], pic
[24], NULL
);
601 qdev_connect_gpio_out(dev
, 0,
602 qdev_get_gpio_in_named(icp
, ICP_GPIO_MMC_WPROT
, 0));
603 qdev_connect_gpio_out(dev
, 1,
604 qdev_get_gpio_in_named(icp
, ICP_GPIO_MMC_CARDIN
, 0));
606 if (nd_table
[0].used
)
607 smc91c111_init(&nd_table
[0], 0xc8000000, pic
[27]);
609 sysbus_create_simple("pl110", 0xc0000000, pic
[22]);
611 integrator_binfo
.ram_size
= ram_size
;
612 integrator_binfo
.kernel_filename
= kernel_filename
;
613 integrator_binfo
.kernel_cmdline
= kernel_cmdline
;
614 integrator_binfo
.initrd_filename
= initrd_filename
;
615 arm_load_kernel(cpu
, &integrator_binfo
);
618 static void integratorcp_machine_init(MachineClass
*mc
)
620 mc
->desc
= "ARM Integrator/CP (ARM926EJ-S)";
621 mc
->init
= integratorcp_init
;
624 DEFINE_MACHINE("integratorcp", integratorcp_machine_init
)
626 static Property core_properties
[] = {
627 DEFINE_PROP_UINT32("memsz", IntegratorCMState
, memsz
, 0),
628 DEFINE_PROP_END_OF_LIST(),
631 static void core_class_init(ObjectClass
*klass
, void *data
)
633 DeviceClass
*dc
= DEVICE_CLASS(klass
);
635 dc
->props
= core_properties
;
638 static const TypeInfo core_info
= {
639 .name
= TYPE_INTEGRATOR_CM
,
640 .parent
= TYPE_SYS_BUS_DEVICE
,
641 .instance_size
= sizeof(IntegratorCMState
),
642 .instance_init
= integratorcm_init
,
643 .class_init
= core_class_init
,
646 static const TypeInfo icp_pic_info
= {
647 .name
= TYPE_INTEGRATOR_PIC
,
648 .parent
= TYPE_SYS_BUS_DEVICE
,
649 .instance_size
= sizeof(icp_pic_state
),
650 .instance_init
= icp_pic_init
,
653 static const TypeInfo icp_ctrl_regs_info
= {
654 .name
= TYPE_ICP_CONTROL_REGS
,
655 .parent
= TYPE_SYS_BUS_DEVICE
,
656 .instance_size
= sizeof(ICPCtrlRegsState
),
657 .instance_init
= icp_control_init
,
660 static void integratorcp_register_types(void)
662 type_register_static(&icp_pic_info
);
663 type_register_static(&core_info
);
664 type_register_static(&icp_ctrl_regs_info
);
667 type_init(integratorcp_register_types
)