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 "hw/sysbus.h"
12 #include "hw/devices.h"
13 #include "hw/boards.h"
14 #include "hw/arm/arm.h"
15 #include "hw/misc/arm_integrator_debug.h"
17 #include "exec/address-spaces.h"
18 #include "sysemu/sysemu.h"
19 #include "qemu/error-report.h"
21 #define TYPE_INTEGRATOR_CM "integrator_core"
22 #define INTEGRATOR_CM(obj) \
23 OBJECT_CHECK(IntegratorCMState, (obj), TYPE_INTEGRATOR_CM)
25 typedef struct IntegratorCMState
{
27 SysBusDevice parent_obj
;
41 uint32_t cm_refcnt_offset
;
47 static uint8_t integrator_spd
[128] = {
48 128, 8, 4, 11, 9, 1, 64, 0, 2, 0xa0, 0xa0, 0, 0, 8, 0, 1,
49 0xe, 4, 0x1c, 1, 2, 0x20, 0xc0, 0, 0, 0, 0, 0x30, 0x28, 0x30, 0x28, 0x40
52 static uint64_t integratorcm_read(void *opaque
, hwaddr offset
,
55 IntegratorCMState
*s
= opaque
;
56 if (offset
>= 0x100 && offset
< 0x200) {
60 return integrator_spd
[offset
>> 2];
62 switch (offset
>> 2) {
74 if (s
->cm_lock
== 0xa05f) {
79 case 6: /* CM_LMBUSCNT */
80 /* ??? High frequency timer. */
81 hw_error("integratorcm_read: CM_LMBUSCNT");
82 case 7: /* CM_AUXOSC */
84 case 8: /* CM_SDRAM */
88 case 10: /* CM_REFCNT */
89 /* This register, CM_REFCNT, provides a 32-bit count value.
90 * The count increments at the fixed reference clock frequency of 24MHz
91 * and can be used as a real-time counter.
93 return (uint32_t)muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), 24,
94 1000) - s
->cm_refcnt_offset
;
95 case 12: /* CM_FLAGS */
97 case 14: /* CM_NVFLAGS */
99 case 16: /* CM_IRQ_STAT */
100 return s
->int_level
& s
->irq_enabled
;
101 case 17: /* CM_IRQ_RSTAT */
103 case 18: /* CM_IRQ_ENSET */
104 return s
->irq_enabled
;
105 case 20: /* CM_SOFT_INTSET */
106 return s
->int_level
& 1;
107 case 24: /* CM_FIQ_STAT */
108 return s
->int_level
& s
->fiq_enabled
;
109 case 25: /* CM_FIQ_RSTAT */
111 case 26: /* CM_FIQ_ENSET */
112 return s
->fiq_enabled
;
113 case 32: /* CM_VOLTAGE_CTL0 */
114 case 33: /* CM_VOLTAGE_CTL1 */
115 case 34: /* CM_VOLTAGE_CTL2 */
116 case 35: /* CM_VOLTAGE_CTL3 */
117 /* ??? Voltage control unimplemented. */
120 hw_error("integratorcm_read: Unimplemented offset 0x%x\n",
126 static void integratorcm_do_remap(IntegratorCMState
*s
)
128 /* Sync memory region state with CM_CTRL REMAP bit:
129 * bit 0 => flash at address 0; bit 1 => RAM
131 memory_region_set_enabled(&s
->flash
, !(s
->cm_ctrl
& 4));
134 static void integratorcm_set_ctrl(IntegratorCMState
*s
, uint32_t value
)
137 qemu_system_reset_request();
139 if ((s
->cm_ctrl
^ value
) & 1) {
140 /* (value & 1) != 0 means the green "MISC LED" is lit.
141 * We don't have any nice place to display LEDs. printf is a bad
142 * idea because Linux uses the LED as a heartbeat and the output
143 * will swamp anything else on the terminal.
146 /* Note that the RESET bit [3] always reads as zero */
147 s
->cm_ctrl
= (s
->cm_ctrl
& ~5) | (value
& 5);
148 integratorcm_do_remap(s
);
151 static void integratorcm_update(IntegratorCMState
*s
)
153 /* ??? The CPU irq/fiq is raised when either the core module or base PIC
155 if (s
->int_level
& (s
->irq_enabled
| s
->fiq_enabled
))
156 hw_error("Core module interrupt\n");
159 static void integratorcm_write(void *opaque
, hwaddr offset
,
160 uint64_t value
, unsigned size
)
162 IntegratorCMState
*s
= opaque
;
163 switch (offset
>> 2) {
165 if (s
->cm_lock
== 0xa05f)
168 case 3: /* CM_CTRL */
169 integratorcm_set_ctrl(s
, value
);
171 case 5: /* CM_LOCK */
172 s
->cm_lock
= value
& 0xffff;
174 case 7: /* CM_AUXOSC */
175 if (s
->cm_lock
== 0xa05f)
176 s
->cm_auxosc
= value
;
178 case 8: /* CM_SDRAM */
181 case 9: /* CM_INIT */
182 /* ??? This can change the memory bus frequency. */
185 case 12: /* CM_FLAGSS */
186 s
->cm_flags
|= value
;
188 case 13: /* CM_FLAGSC */
189 s
->cm_flags
&= ~value
;
191 case 14: /* CM_NVFLAGSS */
192 s
->cm_nvflags
|= value
;
194 case 15: /* CM_NVFLAGSS */
195 s
->cm_nvflags
&= ~value
;
197 case 18: /* CM_IRQ_ENSET */
198 s
->irq_enabled
|= value
;
199 integratorcm_update(s
);
201 case 19: /* CM_IRQ_ENCLR */
202 s
->irq_enabled
&= ~value
;
203 integratorcm_update(s
);
205 case 20: /* CM_SOFT_INTSET */
206 s
->int_level
|= (value
& 1);
207 integratorcm_update(s
);
209 case 21: /* CM_SOFT_INTCLR */
210 s
->int_level
&= ~(value
& 1);
211 integratorcm_update(s
);
213 case 26: /* CM_FIQ_ENSET */
214 s
->fiq_enabled
|= value
;
215 integratorcm_update(s
);
217 case 27: /* CM_FIQ_ENCLR */
218 s
->fiq_enabled
&= ~value
;
219 integratorcm_update(s
);
221 case 32: /* CM_VOLTAGE_CTL0 */
222 case 33: /* CM_VOLTAGE_CTL1 */
223 case 34: /* CM_VOLTAGE_CTL2 */
224 case 35: /* CM_VOLTAGE_CTL3 */
225 /* ??? Voltage control unimplemented. */
228 hw_error("integratorcm_write: Unimplemented offset 0x%x\n",
234 /* Integrator/CM control registers. */
236 static const MemoryRegionOps integratorcm_ops
= {
237 .read
= integratorcm_read
,
238 .write
= integratorcm_write
,
239 .endianness
= DEVICE_NATIVE_ENDIAN
,
242 static int integratorcm_init(SysBusDevice
*dev
)
244 IntegratorCMState
*s
= INTEGRATOR_CM(dev
);
246 s
->cm_osc
= 0x01000048;
247 /* ??? What should the high bits of this value be? */
248 s
->cm_auxosc
= 0x0007feff;
249 s
->cm_sdram
= 0x00011122;
250 if (s
->memsz
>= 256) {
251 integrator_spd
[31] = 64;
253 } else if (s
->memsz
>= 128) {
254 integrator_spd
[31] = 32;
256 } else if (s
->memsz
>= 64) {
257 integrator_spd
[31] = 16;
259 } else if (s
->memsz
>= 32) {
260 integrator_spd
[31] = 4;
263 integrator_spd
[31] = 2;
265 memcpy(integrator_spd
+ 73, "QEMU-MEMORY", 11);
266 s
->cm_init
= 0x00000112;
267 s
->cm_refcnt_offset
= muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), 24,
269 memory_region_init_ram(&s
->flash
, OBJECT(s
), "integrator.flash", 0x100000,
271 vmstate_register_ram_global(&s
->flash
);
273 memory_region_init_io(&s
->iomem
, OBJECT(s
), &integratorcm_ops
, s
,
274 "integratorcm", 0x00800000);
275 sysbus_init_mmio(dev
, &s
->iomem
);
277 integratorcm_do_remap(s
);
278 /* ??? Save/restore. */
282 /* Integrator/CP hardware emulation. */
283 /* Primary interrupt controller. */
285 #define TYPE_INTEGRATOR_PIC "integrator_pic"
286 #define INTEGRATOR_PIC(obj) \
287 OBJECT_CHECK(icp_pic_state, (obj), TYPE_INTEGRATOR_PIC)
289 typedef struct icp_pic_state
{
291 SysBusDevice parent_obj
;
296 uint32_t irq_enabled
;
297 uint32_t fiq_enabled
;
302 static void icp_pic_update(icp_pic_state
*s
)
306 flags
= (s
->level
& s
->irq_enabled
);
307 qemu_set_irq(s
->parent_irq
, flags
!= 0);
308 flags
= (s
->level
& s
->fiq_enabled
);
309 qemu_set_irq(s
->parent_fiq
, flags
!= 0);
312 static void icp_pic_set_irq(void *opaque
, int irq
, int level
)
314 icp_pic_state
*s
= (icp_pic_state
*)opaque
;
316 s
->level
|= 1 << irq
;
318 s
->level
&= ~(1 << irq
);
322 static uint64_t icp_pic_read(void *opaque
, hwaddr offset
,
325 icp_pic_state
*s
= (icp_pic_state
*)opaque
;
327 switch (offset
>> 2) {
328 case 0: /* IRQ_STATUS */
329 return s
->level
& s
->irq_enabled
;
330 case 1: /* IRQ_RAWSTAT */
332 case 2: /* IRQ_ENABLESET */
333 return s
->irq_enabled
;
334 case 4: /* INT_SOFTSET */
336 case 8: /* FRQ_STATUS */
337 return s
->level
& s
->fiq_enabled
;
338 case 9: /* FRQ_RAWSTAT */
340 case 10: /* FRQ_ENABLESET */
341 return s
->fiq_enabled
;
342 case 3: /* IRQ_ENABLECLR */
343 case 5: /* INT_SOFTCLR */
344 case 11: /* FRQ_ENABLECLR */
346 printf ("icp_pic_read: Bad register offset 0x%x\n", (int)offset
);
351 static void icp_pic_write(void *opaque
, hwaddr offset
,
352 uint64_t value
, unsigned size
)
354 icp_pic_state
*s
= (icp_pic_state
*)opaque
;
356 switch (offset
>> 2) {
357 case 2: /* IRQ_ENABLESET */
358 s
->irq_enabled
|= value
;
360 case 3: /* IRQ_ENABLECLR */
361 s
->irq_enabled
&= ~value
;
363 case 4: /* INT_SOFTSET */
365 icp_pic_set_irq(s
, 0, 1);
367 case 5: /* INT_SOFTCLR */
369 icp_pic_set_irq(s
, 0, 0);
371 case 10: /* FRQ_ENABLESET */
372 s
->fiq_enabled
|= value
;
374 case 11: /* FRQ_ENABLECLR */
375 s
->fiq_enabled
&= ~value
;
377 case 0: /* IRQ_STATUS */
378 case 1: /* IRQ_RAWSTAT */
379 case 8: /* FRQ_STATUS */
380 case 9: /* FRQ_RAWSTAT */
382 printf ("icp_pic_write: Bad register offset 0x%x\n", (int)offset
);
388 static const MemoryRegionOps icp_pic_ops
= {
389 .read
= icp_pic_read
,
390 .write
= icp_pic_write
,
391 .endianness
= DEVICE_NATIVE_ENDIAN
,
394 static int icp_pic_init(SysBusDevice
*sbd
)
396 DeviceState
*dev
= DEVICE(sbd
);
397 icp_pic_state
*s
= INTEGRATOR_PIC(dev
);
399 qdev_init_gpio_in(dev
, icp_pic_set_irq
, 32);
400 sysbus_init_irq(sbd
, &s
->parent_irq
);
401 sysbus_init_irq(sbd
, &s
->parent_fiq
);
402 memory_region_init_io(&s
->iomem
, OBJECT(s
), &icp_pic_ops
, s
,
403 "icp-pic", 0x00800000);
404 sysbus_init_mmio(sbd
, &s
->iomem
);
408 /* CP control registers. */
410 #define TYPE_ICP_CONTROL_REGS "icp-ctrl-regs"
411 #define ICP_CONTROL_REGS(obj) \
412 OBJECT_CHECK(ICPCtrlRegsState, (obj), TYPE_ICP_CONTROL_REGS)
414 typedef struct ICPCtrlRegsState
{
416 SysBusDevice parent_obj
;
422 uint32_t intreg_state
;
425 #define ICP_GPIO_MMC_WPROT "mmc-wprot"
426 #define ICP_GPIO_MMC_CARDIN "mmc-cardin"
428 #define ICP_INTREG_WPROT (1 << 0)
429 #define ICP_INTREG_CARDIN (1 << 3)
431 static uint64_t icp_control_read(void *opaque
, hwaddr offset
,
434 ICPCtrlRegsState
*s
= opaque
;
436 switch (offset
>> 2) {
437 case 0: /* CP_IDFIELD */
439 case 1: /* CP_FLASHPROG */
441 case 2: /* CP_INTREG */
442 return s
->intreg_state
;
443 case 3: /* CP_DECODE */
446 hw_error("icp_control_read: Bad offset %x\n", (int)offset
);
451 static void icp_control_write(void *opaque
, hwaddr offset
,
452 uint64_t value
, unsigned size
)
454 ICPCtrlRegsState
*s
= opaque
;
456 switch (offset
>> 2) {
457 case 2: /* CP_INTREG */
458 s
->intreg_state
&= ~(value
& ICP_INTREG_CARDIN
);
459 qemu_set_irq(s
->mmc_irq
, !!(s
->intreg_state
& ICP_INTREG_CARDIN
));
461 case 1: /* CP_FLASHPROG */
462 case 3: /* CP_DECODE */
463 /* Nothing interesting implemented yet. */
466 hw_error("icp_control_write: Bad offset %x\n", (int)offset
);
470 static const MemoryRegionOps icp_control_ops
= {
471 .read
= icp_control_read
,
472 .write
= icp_control_write
,
473 .endianness
= DEVICE_NATIVE_ENDIAN
,
476 static void icp_control_mmc_wprot(void *opaque
, int line
, int level
)
478 ICPCtrlRegsState
*s
= opaque
;
480 s
->intreg_state
&= ~ICP_INTREG_WPROT
;
482 s
->intreg_state
|= ICP_INTREG_WPROT
;
486 static void icp_control_mmc_cardin(void *opaque
, int line
, int level
)
488 ICPCtrlRegsState
*s
= opaque
;
490 /* line is released by writing to CP_INTREG */
492 s
->intreg_state
|= ICP_INTREG_CARDIN
;
493 qemu_set_irq(s
->mmc_irq
, 1);
497 static void icp_control_init(Object
*obj
)
499 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
500 ICPCtrlRegsState
*s
= ICP_CONTROL_REGS(obj
);
501 DeviceState
*dev
= DEVICE(obj
);
503 memory_region_init_io(&s
->iomem
, OBJECT(s
), &icp_control_ops
, s
,
504 "icp_ctrl_regs", 0x00800000);
505 sysbus_init_mmio(sbd
, &s
->iomem
);
507 qdev_init_gpio_in_named(dev
, icp_control_mmc_wprot
, ICP_GPIO_MMC_WPROT
, 1);
508 qdev_init_gpio_in_named(dev
, icp_control_mmc_cardin
,
509 ICP_GPIO_MMC_CARDIN
, 1);
510 sysbus_init_irq(sbd
, &s
->mmc_irq
);
516 static struct arm_boot_info integrator_binfo
= {
521 static void integratorcp_init(MachineState
*machine
)
523 ram_addr_t ram_size
= machine
->ram_size
;
524 const char *cpu_model
= machine
->cpu_model
;
525 const char *kernel_filename
= machine
->kernel_filename
;
526 const char *kernel_cmdline
= machine
->kernel_cmdline
;
527 const char *initrd_filename
= machine
->initrd_filename
;
531 MemoryRegion
*address_space_mem
= get_system_memory();
532 MemoryRegion
*ram
= g_new(MemoryRegion
, 1);
533 MemoryRegion
*ram_alias
= g_new(MemoryRegion
, 1);
535 DeviceState
*dev
, *sic
, *icp
;
539 cpu_model
= "arm926";
542 cpu_oc
= cpu_class_by_name(TYPE_ARM_CPU
, cpu_model
);
544 fprintf(stderr
, "Unable to find CPU definition\n");
548 cpuobj
= object_new(object_class_get_name(cpu_oc
));
550 /* By default ARM1176 CPUs have EL3 enabled. This board does not
551 * currently support EL3 so the CPU EL3 property is disabled before
554 if (object_property_find(cpuobj
, "has_el3", NULL
)) {
555 object_property_set_bool(cpuobj
, false, "has_el3", &error_fatal
);
558 object_property_set_bool(cpuobj
, true, "realized", &error_fatal
);
560 cpu
= ARM_CPU(cpuobj
);
562 memory_region_allocate_system_memory(ram
, NULL
, "integrator.ram",
564 /* ??? On a real system the first 1Mb is mapped as SSRAM or boot flash. */
565 /* ??? RAM should repeat to fill physical memory space. */
566 /* SDRAM at address zero*/
567 memory_region_add_subregion(address_space_mem
, 0, ram
);
568 /* And again at address 0x80000000 */
569 memory_region_init_alias(ram_alias
, NULL
, "ram.alias", ram
, 0, ram_size
);
570 memory_region_add_subregion(address_space_mem
, 0x80000000, ram_alias
);
572 dev
= qdev_create(NULL
, TYPE_INTEGRATOR_CM
);
573 qdev_prop_set_uint32(dev
, "memsz", ram_size
>> 20);
574 qdev_init_nofail(dev
);
575 sysbus_mmio_map((SysBusDevice
*)dev
, 0, 0x10000000);
577 dev
= sysbus_create_varargs(TYPE_INTEGRATOR_PIC
, 0x14000000,
578 qdev_get_gpio_in(DEVICE(cpu
), ARM_CPU_IRQ
),
579 qdev_get_gpio_in(DEVICE(cpu
), ARM_CPU_FIQ
),
581 for (i
= 0; i
< 32; i
++) {
582 pic
[i
] = qdev_get_gpio_in(dev
, i
);
584 sic
= sysbus_create_simple(TYPE_INTEGRATOR_PIC
, 0xca000000, pic
[26]);
585 sysbus_create_varargs("integrator_pit", 0x13000000,
586 pic
[5], pic
[6], pic
[7], NULL
);
587 sysbus_create_simple("pl031", 0x15000000, pic
[8]);
588 sysbus_create_simple("pl011", 0x16000000, pic
[1]);
589 sysbus_create_simple("pl011", 0x17000000, pic
[2]);
590 icp
= sysbus_create_simple(TYPE_ICP_CONTROL_REGS
, 0xcb000000,
591 qdev_get_gpio_in(sic
, 3));
592 sysbus_create_simple("pl050_keyboard", 0x18000000, pic
[3]);
593 sysbus_create_simple("pl050_mouse", 0x19000000, pic
[4]);
594 sysbus_create_simple(TYPE_INTEGRATOR_DEBUG
, 0x1a000000, 0);
596 dev
= sysbus_create_varargs("pl181", 0x1c000000, pic
[23], pic
[24], NULL
);
597 qdev_connect_gpio_out(dev
, 0,
598 qdev_get_gpio_in_named(icp
, ICP_GPIO_MMC_WPROT
, 0));
599 qdev_connect_gpio_out(dev
, 1,
600 qdev_get_gpio_in_named(icp
, ICP_GPIO_MMC_CARDIN
, 0));
602 if (nd_table
[0].used
)
603 smc91c111_init(&nd_table
[0], 0xc8000000, pic
[27]);
605 sysbus_create_simple("pl110", 0xc0000000, pic
[22]);
607 integrator_binfo
.ram_size
= ram_size
;
608 integrator_binfo
.kernel_filename
= kernel_filename
;
609 integrator_binfo
.kernel_cmdline
= kernel_cmdline
;
610 integrator_binfo
.initrd_filename
= initrd_filename
;
611 arm_load_kernel(cpu
, &integrator_binfo
);
614 static void integratorcp_machine_init(MachineClass
*mc
)
616 mc
->desc
= "ARM Integrator/CP (ARM926EJ-S)";
617 mc
->init
= integratorcp_init
;
620 DEFINE_MACHINE("integratorcp", integratorcp_machine_init
)
622 static Property core_properties
[] = {
623 DEFINE_PROP_UINT32("memsz", IntegratorCMState
, memsz
, 0),
624 DEFINE_PROP_END_OF_LIST(),
627 static void core_class_init(ObjectClass
*klass
, void *data
)
629 DeviceClass
*dc
= DEVICE_CLASS(klass
);
630 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
632 k
->init
= integratorcm_init
;
633 dc
->props
= core_properties
;
636 static const TypeInfo core_info
= {
637 .name
= TYPE_INTEGRATOR_CM
,
638 .parent
= TYPE_SYS_BUS_DEVICE
,
639 .instance_size
= sizeof(IntegratorCMState
),
640 .class_init
= core_class_init
,
643 static void icp_pic_class_init(ObjectClass
*klass
, void *data
)
645 SysBusDeviceClass
*sdc
= SYS_BUS_DEVICE_CLASS(klass
);
647 sdc
->init
= icp_pic_init
;
650 static const TypeInfo icp_pic_info
= {
651 .name
= TYPE_INTEGRATOR_PIC
,
652 .parent
= TYPE_SYS_BUS_DEVICE
,
653 .instance_size
= sizeof(icp_pic_state
),
654 .class_init
= icp_pic_class_init
,
657 static const TypeInfo icp_ctrl_regs_info
= {
658 .name
= TYPE_ICP_CONTROL_REGS
,
659 .parent
= TYPE_SYS_BUS_DEVICE
,
660 .instance_size
= sizeof(ICPCtrlRegsState
),
661 .instance_init
= icp_control_init
,
664 static void integratorcp_register_types(void)
666 type_register_static(&icp_pic_info
);
667 type_register_static(&core_info
);
668 type_register_static(&icp_ctrl_regs_info
);
671 type_init(integratorcp_register_types
)