2 * ARM Integrator CP System emulation.
4 * Copyright (c) 2005-2006 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licenced under the GPL
13 #define KERNEL_ARGS_ADDR 0x100
14 #define KERNEL_LOAD_ADDR 0x00010000
15 #define INITRD_LOAD_ADDR 0x00800000
22 uint32_t flash_offset;
36 static uint8_t integrator_spd[128] = {
37 128, 8, 4, 11, 9, 1, 64, 0, 2, 0xa0, 0xa0, 0, 0, 8, 0, 1,
38 0xe, 4, 0x1c, 1, 2, 0x20, 0xc0, 0, 0, 0, 0, 0x30, 0x28, 0x30, 0x28, 0x40
41 static uint32_t integratorcm_read(void *opaque, target_phys_addr_t offset)
43 integratorcm_state *s = (integratorcm_state *)opaque;
45 if (offset >= 0x100 && offset < 0x200) {
49 return integrator_spd[offset >> 2];
51 switch (offset >> 2) {
63 if (s->cm_lock == 0xa05f) {
68 case 6: /* CM_LMBUSCNT */
69 /* ??? High frequency timer. */
70 cpu_abort(cpu_single_env, "integratorcm_read: CM_LMBUSCNT");
71 case 7: /* CM_AUXOSC */
73 case 8: /* CM_SDRAM */
77 case 10: /* CM_REFCT */
78 /* ??? High frequency timer. */
79 cpu_abort(cpu_single_env, "integratorcm_read: CM_REFCT");
80 case 12: /* CM_FLAGS */
82 case 14: /* CM_NVFLAGS */
84 case 16: /* CM_IRQ_STAT */
85 return s->int_level & s->irq_enabled;
86 case 17: /* CM_IRQ_RSTAT */
88 case 18: /* CM_IRQ_ENSET */
89 return s->irq_enabled;
90 case 20: /* CM_SOFT_INTSET */
91 return s->int_level & 1;
92 case 24: /* CM_FIQ_STAT */
93 return s->int_level & s->fiq_enabled;
94 case 25: /* CM_FIQ_RSTAT */
96 case 26: /* CM_FIQ_ENSET */
97 return s->fiq_enabled;
98 case 32: /* CM_VOLTAGE_CTL0 */
99 case 33: /* CM_VOLTAGE_CTL1 */
100 case 34: /* CM_VOLTAGE_CTL2 */
101 case 35: /* CM_VOLTAGE_CTL3 */
102 /* ??? Voltage control unimplemented. */
105 cpu_abort (cpu_single_env,
106 "integratorcm_read: Unimplemented offset 0x%x\n", offset);
111 static void integratorcm_do_remap(integratorcm_state *s, int flash)
114 cpu_register_physical_memory(0, 0x100000, IO_MEM_RAM);
116 cpu_register_physical_memory(0, 0x100000, s->flash_offset | IO_MEM_RAM);
118 //??? tlb_flush (cpu_single_env, 1);
121 static void integratorcm_set_ctrl(integratorcm_state *s, uint32_t value)
124 cpu_abort(cpu_single_env, "Board reset\n");
126 if ((s->cm_init ^ value) & 4) {
127 integratorcm_do_remap(s, (value & 4) == 0);
129 if ((s->cm_init ^ value) & 1) {
130 printf("Green LED %s\n", (value & 1) ? "on" : "off");
132 s->cm_init = (s->cm_init & ~ 5) | (value ^ 5);
135 static void integratorcm_update(integratorcm_state *s)
137 /* ??? The CPU irq/fiq is raised when either the core module or base PIC
139 if (s->int_level & (s->irq_enabled | s->fiq_enabled))
140 cpu_abort(cpu_single_env, "Core module interrupt\n");
143 static void integratorcm_write(void *opaque, target_phys_addr_t offset,
146 integratorcm_state *s = (integratorcm_state *)opaque;
147 offset -= 0x10000000;
148 switch (offset >> 2) {
150 if (s->cm_lock == 0xa05f)
153 case 3: /* CM_CTRL */
154 integratorcm_set_ctrl(s, value);
156 case 5: /* CM_LOCK */
157 s->cm_lock = value & 0xffff;
159 case 7: /* CM_AUXOSC */
160 if (s->cm_lock == 0xa05f)
161 s->cm_auxosc = value;
163 case 8: /* CM_SDRAM */
166 case 9: /* CM_INIT */
167 /* ??? This can change the memory bus frequency. */
170 case 12: /* CM_FLAGSS */
171 s->cm_flags |= value;
173 case 13: /* CM_FLAGSC */
174 s->cm_flags &= ~value;
176 case 14: /* CM_NVFLAGSS */
177 s->cm_nvflags |= value;
179 case 15: /* CM_NVFLAGSS */
180 s->cm_nvflags &= ~value;
182 case 18: /* CM_IRQ_ENSET */
183 s->irq_enabled |= value;
184 integratorcm_update(s);
186 case 19: /* CM_IRQ_ENCLR */
187 s->irq_enabled &= ~value;
188 integratorcm_update(s);
190 case 20: /* CM_SOFT_INTSET */
191 s->int_level |= (value & 1);
192 integratorcm_update(s);
194 case 21: /* CM_SOFT_INTCLR */
195 s->int_level &= ~(value & 1);
196 integratorcm_update(s);
198 case 26: /* CM_FIQ_ENSET */
199 s->fiq_enabled |= value;
200 integratorcm_update(s);
202 case 27: /* CM_FIQ_ENCLR */
203 s->fiq_enabled &= ~value;
204 integratorcm_update(s);
206 case 32: /* CM_VOLTAGE_CTL0 */
207 case 33: /* CM_VOLTAGE_CTL1 */
208 case 34: /* CM_VOLTAGE_CTL2 */
209 case 35: /* CM_VOLTAGE_CTL3 */
210 /* ??? Voltage control unimplemented. */
213 cpu_abort (cpu_single_env,
214 "integratorcm_write: Unimplemented offset 0x%x\n", offset);
219 /* Integrator/CM control registers. */
221 static CPUReadMemoryFunc *integratorcm_readfn[] = {
227 static CPUWriteMemoryFunc *integratorcm_writefn[] = {
233 static void integratorcm_init(int memsz, uint32_t flash_offset)
236 integratorcm_state *s;
238 s = (integratorcm_state *)qemu_mallocz(sizeof(integratorcm_state));
239 s->cm_osc = 0x01000048;
240 /* ??? What should the high bits of this value be? */
241 s->cm_auxosc = 0x0007feff;
242 s->cm_sdram = 0x00011122;
244 integrator_spd[31] = 64;
246 } else if (memsz >= 128) {
247 integrator_spd[31] = 32;
249 } else if (memsz >= 64) {
250 integrator_spd[31] = 16;
252 } else if (memsz >= 32) {
253 integrator_spd[31] = 4;
256 integrator_spd[31] = 2;
258 memcpy(integrator_spd + 73, "QEMU-MEMORY", 11);
259 s->cm_init = 0x00000112;
260 s->flash_offset = flash_offset;
262 iomemtype = cpu_register_io_memory(0, integratorcm_readfn,
263 integratorcm_writefn, s);
264 cpu_register_physical_memory(0x10000000, 0x007fffff, iomemtype);
265 integratorcm_do_remap(s, 1);
266 /* ??? Save/restore. */
269 /* Integrator/CP hardware emulation. */
270 /* Primary interrupt controller. */
272 typedef struct icp_pic_state
274 arm_pic_handler handler;
277 uint32_t irq_enabled;
278 uint32_t fiq_enabled;
284 static void icp_pic_update(icp_pic_state *s)
288 if (s->parent_irq != -1) {
289 flags = (s->level & s->irq_enabled);
290 pic_set_irq_new(s->parent, s->parent_irq, flags != 0);
292 if (s->parent_fiq != -1) {
293 flags = (s->level & s->fiq_enabled);
294 pic_set_irq_new(s->parent, s->parent_fiq, flags != 0);
298 static void icp_pic_set_irq(void *opaque, int irq, int level)
300 icp_pic_state *s = (icp_pic_state *)opaque;
302 s->level |= 1 << irq;
304 s->level &= ~(1 << irq);
308 static uint32_t icp_pic_read(void *opaque, target_phys_addr_t offset)
310 icp_pic_state *s = (icp_pic_state *)opaque;
313 switch (offset >> 2) {
314 case 0: /* IRQ_STATUS */
315 return s->level & s->irq_enabled;
316 case 1: /* IRQ_RAWSTAT */
318 case 2: /* IRQ_ENABLESET */
319 return s->irq_enabled;
320 case 4: /* INT_SOFTSET */
322 case 8: /* FRQ_STATUS */
323 return s->level & s->fiq_enabled;
324 case 9: /* FRQ_RAWSTAT */
326 case 10: /* FRQ_ENABLESET */
327 return s->fiq_enabled;
328 case 3: /* IRQ_ENABLECLR */
329 case 5: /* INT_SOFTCLR */
330 case 11: /* FRQ_ENABLECLR */
332 printf ("icp_pic_read: Bad register offset 0x%x\n", offset);
337 static void icp_pic_write(void *opaque, target_phys_addr_t offset,
340 icp_pic_state *s = (icp_pic_state *)opaque;
343 switch (offset >> 2) {
344 case 2: /* IRQ_ENABLESET */
345 s->irq_enabled |= value;
347 case 3: /* IRQ_ENABLECLR */
348 s->irq_enabled &= ~value;
350 case 4: /* INT_SOFTSET */
352 pic_set_irq_new(s, 0, 1);
354 case 5: /* INT_SOFTCLR */
356 pic_set_irq_new(s, 0, 0);
358 case 10: /* FRQ_ENABLESET */
359 s->fiq_enabled |= value;
361 case 11: /* FRQ_ENABLECLR */
362 s->fiq_enabled &= ~value;
364 case 0: /* IRQ_STATUS */
365 case 1: /* IRQ_RAWSTAT */
366 case 8: /* FRQ_STATUS */
367 case 9: /* FRQ_RAWSTAT */
369 printf ("icp_pic_write: Bad register offset 0x%x\n", offset);
375 static CPUReadMemoryFunc *icp_pic_readfn[] = {
381 static CPUWriteMemoryFunc *icp_pic_writefn[] = {
387 static icp_pic_state *icp_pic_init(uint32_t base, void *parent,
388 int parent_irq, int parent_fiq)
393 s = (icp_pic_state *)qemu_mallocz(sizeof(icp_pic_state));
396 s->handler = icp_pic_set_irq;
399 s->parent_irq = parent_irq;
400 s->parent_fiq = parent_fiq;
401 iomemtype = cpu_register_io_memory(0, icp_pic_readfn,
403 cpu_register_physical_memory(base, 0x007fffff, iomemtype);
404 /* ??? Save/restore. */
408 /* CP control registers. */
413 static uint32_t icp_control_read(void *opaque, target_phys_addr_t offset)
415 icp_control_state *s = (icp_control_state *)opaque;
417 switch (offset >> 2) {
418 case 0: /* CP_IDFIELD */
420 case 1: /* CP_FLASHPROG */
422 case 2: /* CP_INTREG */
424 case 3: /* CP_DECODE */
427 cpu_abort (cpu_single_env, "icp_control_read: Bad offset %x\n", offset);
432 static void icp_control_write(void *opaque, target_phys_addr_t offset,
435 icp_control_state *s = (icp_control_state *)opaque;
437 switch (offset >> 2) {
438 case 1: /* CP_FLASHPROG */
439 case 2: /* CP_INTREG */
440 case 3: /* CP_DECODE */
441 /* Nothing interesting implemented yet. */
444 cpu_abort (cpu_single_env, "icp_control_write: Bad offset %x\n", offset);
447 static CPUReadMemoryFunc *icp_control_readfn[] = {
453 static CPUWriteMemoryFunc *icp_control_writefn[] = {
459 static void icp_control_init(uint32_t base)
462 icp_control_state *s;
464 s = (icp_control_state *)qemu_mallocz(sizeof(icp_control_state));
465 iomemtype = cpu_register_io_memory(0, icp_control_readfn,
466 icp_control_writefn, s);
467 cpu_register_physical_memory(base, 0x007fffff, iomemtype);
469 /* ??? Save/restore. */
473 /* The worlds second smallest bootloader. Set r0-r2, then jump to kernel. */
474 static uint32_t bootloader[] = {
475 0xe3a00000, /* mov r0, #0 */
476 0xe3a01013, /* mov r1, #0x13 */
477 0xe3811c01, /* orr r1, r1, #0x100 */
478 0xe59f2000, /* ldr r2, [pc, #0] */
479 0xe59ff000, /* ldr pc, [pc, #0] */
480 0, /* Address of kernel args. Set by integratorcp_init. */
481 0 /* Kernel entry point. Set by integratorcp_init. */
484 static void set_kernel_args(uint32_t ram_size, int initrd_size,
485 const char *kernel_cmdline)
489 p = (uint32_t *)(phys_ram_base + KERNEL_ARGS_ADDR);
492 stl_raw(p++, 0x54410001);
494 stl_raw(p++, 0x1000);
498 stl_raw(p++, 0x54410002);
499 stl_raw(p++, ram_size);
504 stl_raw(p++, 0x54420005);
505 stl_raw(p++, INITRD_LOAD_ADDR);
506 stl_raw(p++, initrd_size);
508 if (kernel_cmdline && *kernel_cmdline) {
512 cmdline_size = strlen(kernel_cmdline);
513 memcpy (p + 2, kernel_cmdline, cmdline_size + 1);
514 cmdline_size = (cmdline_size >> 2) + 1;
515 stl_raw(p++, cmdline_size + 2);
516 stl_raw(p++, 0x54410009);
526 static void integratorcp_init(int ram_size, int vga_ram_size, int boot_device,
527 DisplayState *ds, const char **fd_filename, int snapshot,
528 const char *kernel_filename, const char *kernel_cmdline,
529 const char *initrd_filename, uint32_t cpuid)
532 uint32_t bios_offset;
540 cpu_arm_set_model(env, cpuid);
541 bios_offset = ram_size + vga_ram_size;
542 /* ??? On a real system the first 1Mb is mapped as SSRAM or boot flash. */
543 /* ??? RAM shoud repeat to fill physical memory space. */
544 /* SDRAM at address zero*/
545 cpu_register_physical_memory(0, ram_size, IO_MEM_RAM);
546 /* And again at address 0x80000000 */
547 cpu_register_physical_memory(0x80000000, ram_size, IO_MEM_RAM);
549 integratorcm_init(ram_size >> 20, bios_offset);
550 cpu_pic = arm_pic_init_cpu(env);
551 pic = icp_pic_init(0x14000000, cpu_pic, ARM_PIC_CPU_IRQ, ARM_PIC_CPU_FIQ);
552 icp_pic_init(0xca000000, pic, 26, -1);
553 icp_pit_init(0x13000000, pic, 5);
554 pl011_init(0x16000000, pic, 1, serial_hds[0]);
555 pl011_init(0x17000000, pic, 2, serial_hds[1]);
556 icp_control_init(0xcb000000);
557 pl050_init(0x18000000, pic, 3, 0);
558 pl050_init(0x19000000, pic, 4, 1);
559 if (nd_table[0].vlan) {
560 if (nd_table[0].model == NULL
561 || strcmp(nd_table[0].model, "smc91c111") == 0) {
562 smc91c111_init(&nd_table[0], 0xc8000000, pic, 27);
564 fprintf(stderr, "qemu: Unsupported NIC: %s\n", nd_table[0].model);
568 pl110_init(ds, 0xc0000000, pic, 22, 0);
570 /* Load the kernel. */
571 if (!kernel_filename) {
572 fprintf(stderr, "Kernel image must be specified\n");
575 kernel_size = load_image(kernel_filename,
576 phys_ram_base + KERNEL_LOAD_ADDR);
577 if (kernel_size < 0) {
578 fprintf(stderr, "qemu: could not load kernel '%s'\n", kernel_filename);
581 if (initrd_filename) {
582 initrd_size = load_image(initrd_filename,
583 phys_ram_base + INITRD_LOAD_ADDR);
584 if (initrd_size < 0) {
585 fprintf(stderr, "qemu: could not load initrd '%s'\n",
592 bootloader[5] = KERNEL_ARGS_ADDR;
593 bootloader[6] = KERNEL_LOAD_ADDR;
594 for (n = 0; n < sizeof(bootloader) / 4; n++)
595 stl_raw(phys_ram_base + (n * 4), bootloader[n]);
596 set_kernel_args(ram_size, initrd_size, kernel_cmdline);
599 static void integratorcp926_init(int ram_size, int vga_ram_size,
600 int boot_device, DisplayState *ds, const char **fd_filename, int snapshot,
601 const char *kernel_filename, const char *kernel_cmdline,
602 const char *initrd_filename)
604 integratorcp_init(ram_size, vga_ram_size, boot_device, ds, fd_filename,
605 snapshot, kernel_filename, kernel_cmdline,
606 initrd_filename, ARM_CPUID_ARM926);
609 static void integratorcp1026_init(int ram_size, int vga_ram_size,
610 int boot_device, DisplayState *ds, const char **fd_filename, int snapshot,
611 const char *kernel_filename, const char *kernel_cmdline,
612 const char *initrd_filename)
614 integratorcp_init(ram_size, vga_ram_size, boot_device, ds, fd_filename,
615 snapshot, kernel_filename, kernel_cmdline,
616 initrd_filename, ARM_CPUID_ARM1026);
619 QEMUMachine integratorcp926_machine = {
621 "ARM Integrator/CP (ARM926EJ-S)",
622 integratorcp926_init,
625 QEMUMachine integratorcp1026_machine = {
627 "ARM Integrator/CP (ARM1026EJ-S)",
628 integratorcp1026_init,