2 * QEMU PowerPC 405 evaluation boards emulation
4 * Copyright (c) 2007 Jocelyn Mayer
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "qemu/osdep.h"
26 #include "qemu/units.h"
27 #include "qapi/error.h"
28 #include "qemu-common.h"
29 #include "qemu/datadir.h"
31 #include "hw/ppc/ppc.h"
32 #include "hw/qdev-properties.h"
33 #include "hw/sysbus.h"
35 #include "hw/rtc/m48t59.h"
36 #include "hw/block/flash.h"
37 #include "sysemu/qtest.h"
38 #include "sysemu/reset.h"
39 #include "sysemu/block-backend.h"
40 #include "hw/boards.h"
41 #include "qemu/error-report.h"
42 #include "hw/loader.h"
43 #include "qemu/cutils.h"
46 #define BIOS_FILENAME "ppc405_rom.bin"
47 #define BIOS_SIZE (2 * MiB)
49 #define KERNEL_LOAD_ADDR 0x01000000
50 #define INITRD_LOAD_ADDR 0x01800000
52 #define USE_FLASH_BIOS
54 /*****************************************************************************/
55 /* PPC405EP reference board (IBM) */
56 /* Standalone board with:
58 * - SDRAM (0x00000000)
59 * - Flash (0xFFF80000)
61 * - NVRAM (0xF0000000)
64 typedef struct ref405ep_fpga_t ref405ep_fpga_t
;
65 struct ref405ep_fpga_t
{
70 static uint64_t ref405ep_fpga_readb(void *opaque
, hwaddr addr
, unsigned size
)
72 ref405ep_fpga_t
*fpga
;
91 static void ref405ep_fpga_writeb(void *opaque
, hwaddr addr
, uint64_t value
,
94 ref405ep_fpga_t
*fpga
;
109 static const MemoryRegionOps ref405ep_fpga_ops
= {
110 .read
= ref405ep_fpga_readb
,
111 .write
= ref405ep_fpga_writeb
,
112 .impl
.min_access_size
= 1,
113 .impl
.max_access_size
= 1,
114 .valid
.min_access_size
= 1,
115 .valid
.max_access_size
= 4,
116 .endianness
= DEVICE_BIG_ENDIAN
,
119 static void ref405ep_fpga_reset (void *opaque
)
121 ref405ep_fpga_t
*fpga
;
128 static void ref405ep_fpga_init(MemoryRegion
*sysmem
, uint32_t base
)
130 ref405ep_fpga_t
*fpga
;
131 MemoryRegion
*fpga_memory
= g_new(MemoryRegion
, 1);
133 fpga
= g_malloc0(sizeof(ref405ep_fpga_t
));
134 memory_region_init_io(fpga_memory
, NULL
, &ref405ep_fpga_ops
, fpga
,
136 memory_region_add_subregion(sysmem
, base
, fpga_memory
);
137 qemu_register_reset(&ref405ep_fpga_reset
, fpga
);
141 * CPU reset handler when booting directly from a loaded kernel
143 static struct boot_info
{
146 uint32_t initrd_base
;
147 uint32_t initrd_size
;
148 uint32_t cmdline_base
;
149 uint32_t cmdline_size
;
152 static void main_cpu_reset(void *opaque
)
154 PowerPCCPU
*cpu
= opaque
;
155 CPUPPCState
*env
= &cpu
->env
;
156 struct boot_info
*bi
= env
->load_info
;
160 /* stack: top of sram */
161 env
->gpr
[1] = PPC405EP_SRAM_BASE
+ PPC405EP_SRAM_SIZE
- 8;
163 /* Tune our boot state */
164 env
->gpr
[3] = bi
->bdloc
;
165 env
->gpr
[4] = bi
->initrd_base
;
166 env
->gpr
[5] = bi
->initrd_base
+ bi
->initrd_size
;
167 env
->gpr
[6] = bi
->cmdline_base
;
168 env
->gpr
[7] = bi
->cmdline_size
;
170 env
->nip
= bi
->entry
;
173 static void boot_from_kernel(MachineState
*machine
, PowerPCCPU
*cpu
)
175 CPUPPCState
*env
= &cpu
->env
;
184 bdloc
= ppc405_set_bootinfo(env
, machine
->ram_size
);
185 boot_info
.bdloc
= bdloc
;
187 kernel_size
= load_elf(machine
->kernel_filename
, NULL
, NULL
, NULL
,
188 &boot_entry
, &kernel_base
, NULL
, NULL
,
189 1, PPC_ELF_MACHINE
, 0, 0);
190 if (kernel_size
< 0) {
191 error_report("Could not load kernel '%s' : %s",
192 machine
->kernel_filename
, load_elf_strerror(kernel_size
));
195 boot_info
.entry
= boot_entry
;
198 if (machine
->initrd_filename
) {
199 initrd_base
= INITRD_LOAD_ADDR
;
200 initrd_size
= load_image_targphys(machine
->initrd_filename
, initrd_base
,
201 machine
->ram_size
- initrd_base
);
202 if (initrd_size
< 0) {
203 error_report("could not load initial ram disk '%s'",
204 machine
->initrd_filename
);
208 boot_info
.initrd_base
= initrd_base
;
209 boot_info
.initrd_size
= initrd_size
;
212 if (machine
->kernel_cmdline
) {
213 len
= strlen(machine
->kernel_cmdline
);
214 bdloc
-= ((len
+ 255) & ~255);
215 cpu_physical_memory_write(bdloc
, machine
->kernel_cmdline
, len
+ 1);
216 boot_info
.cmdline_base
= bdloc
;
217 boot_info
.cmdline_size
= bdloc
+ len
;
220 /* Install our custom reset handler to start from Linux */
221 qemu_register_reset(main_cpu_reset
, cpu
);
222 env
->load_info
= &boot_info
;
225 static void ref405ep_init(MachineState
*machine
)
227 MachineClass
*mc
= MACHINE_GET_CLASS(machine
);
228 const char *kernel_filename
= machine
->kernel_filename
;
232 MemoryRegion
*sram
= g_new(MemoryRegion
, 1);
233 MemoryRegion
*ram_memories
= g_new(MemoryRegion
, 2);
234 hwaddr ram_bases
[2], ram_sizes
[2];
235 MemoryRegion
*sysmem
= get_system_memory();
238 if (machine
->ram_size
!= mc
->default_ram_size
) {
239 char *sz
= size_to_str(mc
->default_ram_size
);
240 error_report("Invalid RAM size, should be %s", sz
);
246 memory_region_init_alias(&ram_memories
[0], NULL
, "ef405ep.ram.alias",
247 machine
->ram
, 0, machine
->ram_size
);
249 ram_sizes
[0] = machine
->ram_size
;
250 memory_region_init(&ram_memories
[1], NULL
, "ef405ep.ram1", 0);
251 ram_bases
[1] = 0x00000000;
252 ram_sizes
[1] = 0x00000000;
254 cpu
= ppc405ep_init(sysmem
, ram_memories
, ram_bases
, ram_sizes
,
255 33333333, &uicdev
, kernel_filename
== NULL
? 0 : 1);
258 memory_region_init_ram(sram
, NULL
, "ef405ep.sram", PPC405EP_SRAM_SIZE
,
260 memory_region_add_subregion(sysmem
, PPC405EP_SRAM_BASE
, sram
);
262 /* allocate and load BIOS */
263 if (machine
->firmware
) {
264 MemoryRegion
*bios
= g_new(MemoryRegion
, 1);
265 g_autofree
char *filename
;
268 memory_region_init_rom(bios
, NULL
, "ef405ep.bios", BIOS_SIZE
,
271 filename
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, machine
->firmware
);
273 error_report("Could not find firmware '%s'", machine
->firmware
);
277 bios_size
= load_image_size(filename
,
278 memory_region_get_ram_ptr(bios
),
281 error_report("Could not load PowerPC BIOS '%s'", machine
->firmware
);
285 bios_size
= (bios_size
+ 0xfff) & ~0xfff;
286 memory_region_add_subregion(sysmem
, (uint32_t)(-bios_size
), bios
);
290 ref405ep_fpga_init(sysmem
, PPC405EP_FPGA_BASE
);
292 dev
= qdev_new("sysbus-m48t08");
293 qdev_prop_set_int32(dev
, "base-year", 1968);
294 s
= SYS_BUS_DEVICE(dev
);
295 sysbus_realize_and_unref(s
, &error_fatal
);
296 sysbus_mmio_map(s
, 0, PPC405EP_NVRAM_BASE
);
298 /* Load kernel and initrd using U-Boot images */
299 if (kernel_filename
&& machine
->firmware
) {
300 target_ulong kernel_base
, initrd_base
;
301 long kernel_size
, initrd_size
;
303 kernel_base
= KERNEL_LOAD_ADDR
;
304 kernel_size
= load_image_targphys(kernel_filename
, kernel_base
,
305 machine
->ram_size
- kernel_base
);
306 if (kernel_size
< 0) {
307 error_report("could not load kernel '%s'", kernel_filename
);
312 if (machine
->initrd_filename
) {
313 initrd_base
= INITRD_LOAD_ADDR
;
314 initrd_size
= load_image_targphys(machine
->initrd_filename
,
316 machine
->ram_size
- initrd_base
);
317 if (initrd_size
< 0) {
318 error_report("could not load initial ram disk '%s'",
319 machine
->initrd_filename
);
324 /* Load ELF kernel and rootfs.cpio */
325 } else if (kernel_filename
&& !machine
->firmware
) {
326 boot_from_kernel(machine
, cpu
);
330 static void ref405ep_class_init(ObjectClass
*oc
, void *data
)
332 MachineClass
*mc
= MACHINE_CLASS(oc
);
334 mc
->desc
= "ref405ep";
335 mc
->init
= ref405ep_init
;
336 mc
->default_ram_size
= 0x08000000;
337 mc
->default_ram_id
= "ef405ep.ram";
340 static const TypeInfo ref405ep_type
= {
341 .name
= MACHINE_TYPE_NAME("ref405ep"),
342 .parent
= TYPE_MACHINE
,
343 .class_init
= ref405ep_class_init
,
346 /*****************************************************************************/
347 /* AMCC Taihu evaluation board */
348 /* - PowerPC 405EP processor
349 * - SDRAM 128 MB at 0x00000000
350 * - Boot flash 2 MB at 0xFFE00000
351 * - Application flash 32 MB at 0xFC000000
354 * - 1 USB 1.1 device 0x50000000
355 * - 1 LCD display 0x50100000
356 * - 1 CPLD 0x50100000
358 * - 1 I2C thermal sensor
360 * - bit-bang SPI port using GPIOs
361 * - 1 EBC interface connector 0 0x50200000
362 * - 1 cardbus controller + expansion slot.
363 * - 1 PCI expansion slot.
365 typedef struct taihu_cpld_t taihu_cpld_t
;
366 struct taihu_cpld_t
{
371 static uint64_t taihu_cpld_read(void *opaque
, hwaddr addr
, unsigned size
)
392 static void taihu_cpld_write(void *opaque
, hwaddr addr
,
393 uint64_t value
, unsigned size
)
410 static const MemoryRegionOps taihu_cpld_ops
= {
411 .read
= taihu_cpld_read
,
412 .write
= taihu_cpld_write
,
414 .min_access_size
= 1,
415 .max_access_size
= 1,
417 .endianness
= DEVICE_NATIVE_ENDIAN
,
420 static void taihu_cpld_reset (void *opaque
)
429 static void taihu_cpld_init(MemoryRegion
*sysmem
, uint32_t base
)
432 MemoryRegion
*cpld_memory
= g_new(MemoryRegion
, 1);
434 cpld
= g_malloc0(sizeof(taihu_cpld_t
));
435 memory_region_init_io(cpld_memory
, NULL
, &taihu_cpld_ops
, cpld
, "cpld", 0x100);
436 memory_region_add_subregion(sysmem
, base
, cpld_memory
);
437 qemu_register_reset(&taihu_cpld_reset
, cpld
);
440 static void taihu_405ep_init(MachineState
*machine
)
442 MachineClass
*mc
= MACHINE_GET_CLASS(machine
);
443 const char *bios_name
= machine
->firmware
?: BIOS_FILENAME
;
444 const char *kernel_filename
= machine
->kernel_filename
;
445 const char *initrd_filename
= machine
->initrd_filename
;
447 MemoryRegion
*sysmem
= get_system_memory();
449 MemoryRegion
*ram_memories
= g_new(MemoryRegion
, 2);
450 hwaddr ram_bases
[2], ram_sizes
[2];
452 target_ulong kernel_base
, initrd_base
;
453 long kernel_size
, initrd_size
;
459 if (machine
->ram_size
!= mc
->default_ram_size
) {
460 char *sz
= size_to_str(mc
->default_ram_size
);
461 error_report("Invalid RAM size, should be %s", sz
);
467 ram_sizes
[0] = 0x04000000;
468 memory_region_init_alias(&ram_memories
[0], NULL
,
469 "taihu_405ep.ram-0", machine
->ram
, ram_bases
[0],
471 ram_bases
[1] = 0x04000000;
472 ram_sizes
[1] = 0x04000000;
473 memory_region_init_alias(&ram_memories
[1], NULL
,
474 "taihu_405ep.ram-1", machine
->ram
, ram_bases
[1],
476 ppc405ep_init(sysmem
, ram_memories
, ram_bases
, ram_sizes
,
477 33333333, &uicdev
, kernel_filename
== NULL
? 0 : 1);
478 /* allocate and load BIOS */
480 #if defined(USE_FLASH_BIOS)
481 dinfo
= drive_get(IF_PFLASH
, 0, fl_idx
);
484 pflash_cfi02_register(0xFFE00000,
485 "taihu_405ep.bios", bios_size
,
486 blk_by_legacy_dinfo(dinfo
),
488 4, 0x0001, 0x22DA, 0x0000, 0x0000, 0x555, 0x2AA,
494 bios
= g_new(MemoryRegion
, 1);
495 memory_region_init_rom(bios
, NULL
, "taihu_405ep.bios", BIOS_SIZE
,
497 filename
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, bios_name
);
499 bios_size
= load_image_size(filename
,
500 memory_region_get_ram_ptr(bios
),
504 error_report("Could not load PowerPC BIOS '%s'", bios_name
);
507 bios_size
= (bios_size
+ 0xfff) & ~0xfff;
508 memory_region_add_subregion(sysmem
, (uint32_t)(-bios_size
), bios
);
509 } else if (!qtest_enabled()) {
510 error_report("Could not load PowerPC BIOS '%s'", bios_name
);
514 /* Register Linux flash */
515 dinfo
= drive_get(IF_PFLASH
, 0, fl_idx
);
517 bios_size
= 32 * MiB
;
518 pflash_cfi02_register(0xfc000000, "taihu_405ep.flash", bios_size
,
519 blk_by_legacy_dinfo(dinfo
),
521 4, 0x0001, 0x22DA, 0x0000, 0x0000, 0x555, 0x2AA,
525 /* Register CLPD & LCD display */
526 taihu_cpld_init(sysmem
, 0x50100000);
528 linux_boot
= (kernel_filename
!= NULL
);
530 kernel_base
= KERNEL_LOAD_ADDR
;
531 /* now we can load the kernel */
532 kernel_size
= load_image_targphys(kernel_filename
, kernel_base
,
533 machine
->ram_size
- kernel_base
);
534 if (kernel_size
< 0) {
535 error_report("could not load kernel '%s'", kernel_filename
);
539 if (initrd_filename
) {
540 initrd_base
= INITRD_LOAD_ADDR
;
541 initrd_size
= load_image_targphys(initrd_filename
, initrd_base
,
542 machine
->ram_size
- initrd_base
);
543 if (initrd_size
< 0) {
544 error_report("could not load initial ram disk '%s'",
560 static void taihu_class_init(ObjectClass
*oc
, void *data
)
562 MachineClass
*mc
= MACHINE_CLASS(oc
);
565 mc
->init
= taihu_405ep_init
;
566 mc
->default_ram_size
= 0x08000000;
567 mc
->default_ram_id
= "taihu_405ep.ram";
568 mc
->deprecation_reason
= "incomplete, use 'ref405ep' instead";
571 static const TypeInfo taihu_type
= {
572 .name
= MACHINE_TYPE_NAME("taihu"),
573 .parent
= TYPE_MACHINE
,
574 .class_init
= taihu_class_init
,
577 static void ppc405_machine_init(void)
579 type_register_static(&ref405ep_type
);
580 type_register_static(&taihu_type
);
583 type_init(ppc405_machine_init
)