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
24 #include "qemu/osdep.h"
25 #include "qemu/units.h"
26 #include "qapi/error.h"
27 #include "qemu-common.h"
30 #include "hw/ppc/ppc.h"
32 #include "hw/timer/m48t59.h"
33 #include "hw/block/flash.h"
34 #include "sysemu/sysemu.h"
35 #include "sysemu/qtest.h"
36 #include "sysemu/block-backend.h"
37 #include "hw/boards.h"
39 #include "qemu/error-report.h"
40 #include "hw/loader.h"
41 #include "exec/address-spaces.h"
43 #define BIOS_FILENAME "ppc405_rom.bin"
44 #define BIOS_SIZE (2 * MiB)
46 #define KERNEL_LOAD_ADDR 0x00000000
47 #define INITRD_LOAD_ADDR 0x01800000
49 #define USE_FLASH_BIOS
51 //#define DEBUG_BOARD_INIT
53 /*****************************************************************************/
54 /* PPC405EP reference board (IBM) */
55 /* Standalone board with:
57 * - SDRAM (0x00000000)
58 * - Flash (0xFFF80000)
60 * - NVRAM (0xF0000000)
63 typedef struct ref405ep_fpga_t ref405ep_fpga_t;
64 struct ref405ep_fpga_t {
69 static uint64_t ref405ep_fpga_readb(void *opaque, hwaddr addr, unsigned size)
71 ref405ep_fpga_t *fpga;
90 static void ref405ep_fpga_writeb(void *opaque, hwaddr addr, uint64_t value,
93 ref405ep_fpga_t *fpga;
108 static const MemoryRegionOps ref405ep_fpga_ops = {
109 .read = ref405ep_fpga_readb,
110 .write = ref405ep_fpga_writeb,
111 .impl.min_access_size = 1,
112 .impl.max_access_size = 1,
113 .valid.min_access_size = 1,
114 .valid.max_access_size = 4,
115 .endianness = DEVICE_BIG_ENDIAN,
118 static void ref405ep_fpga_reset (void *opaque)
120 ref405ep_fpga_t *fpga;
127 static void ref405ep_fpga_init(MemoryRegion *sysmem, uint32_t base)
129 ref405ep_fpga_t *fpga;
130 MemoryRegion *fpga_memory = g_new(MemoryRegion, 1);
132 fpga = g_malloc0(sizeof(ref405ep_fpga_t));
133 memory_region_init_io(fpga_memory, NULL, &ref405ep_fpga_ops, fpga,
135 memory_region_add_subregion(sysmem, base, fpga_memory);
136 qemu_register_reset(&ref405ep_fpga_reset, fpga);
139 static void ref405ep_init(MachineState *machine)
141 ram_addr_t ram_size = machine->ram_size;
142 const char *kernel_filename = machine->kernel_filename;
143 const char *kernel_cmdline = machine->kernel_cmdline;
144 const char *initrd_filename = machine->initrd_filename;
150 MemoryRegion *sram = g_new(MemoryRegion, 1);
152 MemoryRegion *ram_memories = g_malloc(2 * sizeof(*ram_memories));
153 hwaddr ram_bases[2], ram_sizes[2];
154 target_ulong sram_size;
157 //static int phy_addr = 1;
158 target_ulong kernel_base, initrd_base;
159 long kernel_size, initrd_size;
161 int fl_idx, fl_sectors, len;
163 MemoryRegion *sysmem = get_system_memory();
166 if (!qtest_enabled()) {
167 warn_report("qemu-system-ppcemb is deprecated, "
168 "please use qemu-system-ppc instead.");
173 memory_region_allocate_system_memory(&ram_memories[0], NULL, "ef405ep.ram",
176 ram_sizes[0] = 0x08000000;
177 memory_region_init(&ram_memories[1], NULL, "ef405ep.ram1", 0);
178 ram_bases[1] = 0x00000000;
179 ram_sizes[1] = 0x00000000;
180 ram_size = 128 * MiB;
181 #ifdef DEBUG_BOARD_INIT
182 printf("%s: register cpu\n", __func__);
184 env = ppc405ep_init(sysmem, ram_memories, ram_bases, ram_sizes,
185 33333333, &pic, kernel_filename == NULL ? 0 : 1);
187 sram_size = 512 * KiB;
188 memory_region_init_ram(sram, NULL, "ef405ep.sram", sram_size,
190 memory_region_add_subregion(sysmem, 0xFFF00000, sram);
191 /* allocate and load BIOS */
192 #ifdef DEBUG_BOARD_INIT
193 printf("%s: register BIOS\n", __func__);
196 #ifdef USE_FLASH_BIOS
197 dinfo = drive_get(IF_PFLASH, 0, fl_idx);
199 BlockBackend *blk = blk_by_legacy_dinfo(dinfo);
201 bios_size = blk_getlength(blk);
202 fl_sectors = (bios_size + 65535) >> 16;
203 #ifdef DEBUG_BOARD_INIT
204 printf("Register parallel flash %d size %lx"
205 " at addr %lx '%s' %d\n",
206 fl_idx, bios_size, -bios_size,
207 blk_name(blk), fl_sectors);
209 pflash_cfi02_register((uint32_t)(-bios_size),
210 NULL, "ef405ep.bios", bios_size,
211 blk, 65536, fl_sectors, 1,
212 2, 0x0001, 0x22DA, 0x0000, 0x0000, 0x555, 0x2AA,
218 #ifdef DEBUG_BOARD_INIT
219 printf("Load BIOS from file\n");
221 bios = g_new(MemoryRegion, 1);
222 memory_region_init_ram(bios, NULL, "ef405ep.bios", BIOS_SIZE,
225 if (bios_name == NULL)
226 bios_name = BIOS_FILENAME;
227 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
229 bios_size = load_image(filename, memory_region_get_ram_ptr(bios));
231 if (bios_size < 0 || bios_size > BIOS_SIZE) {
232 error_report("Could not load PowerPC BIOS '%s'", bios_name);
235 bios_size = (bios_size + 0xfff) & ~0xfff;
236 memory_region_add_subregion(sysmem, (uint32_t)(-bios_size), bios);
237 } else if (!qtest_enabled() || kernel_filename != NULL) {
238 error_report("Could not load PowerPC BIOS '%s'", bios_name);
241 /* Avoid an uninitialized variable warning */
244 memory_region_set_readonly(bios, true);
247 #ifdef DEBUG_BOARD_INIT
248 printf("%s: register FPGA\n", __func__);
250 ref405ep_fpga_init(sysmem, 0xF0300000);
252 #ifdef DEBUG_BOARD_INIT
253 printf("%s: register NVRAM\n", __func__);
255 m48t59_init(NULL, 0xF0000000, 0, 8192, 1968, 8);
257 linux_boot = (kernel_filename != NULL);
259 #ifdef DEBUG_BOARD_INIT
260 printf("%s: load kernel\n", __func__);
262 memset(&bd, 0, sizeof(bd));
263 bd.bi_memstart = 0x00000000;
264 bd.bi_memsize = ram_size;
265 bd.bi_flashstart = -bios_size;
266 bd.bi_flashsize = -bios_size;
267 bd.bi_flashoffset = 0;
268 bd.bi_sramstart = 0xFFF00000;
269 bd.bi_sramsize = sram_size;
271 bd.bi_intfreq = 133333333;
272 bd.bi_busfreq = 33333333;
273 bd.bi_baudrate = 115200;
274 bd.bi_s_version[0] = 'Q';
275 bd.bi_s_version[1] = 'M';
276 bd.bi_s_version[2] = 'U';
277 bd.bi_s_version[3] = '\0';
278 bd.bi_r_version[0] = 'Q';
279 bd.bi_r_version[1] = 'E';
280 bd.bi_r_version[2] = 'M';
281 bd.bi_r_version[3] = 'U';
282 bd.bi_r_version[4] = '\0';
283 bd.bi_procfreq = 133333333;
284 bd.bi_plb_busfreq = 33333333;
285 bd.bi_pci_busfreq = 33333333;
286 bd.bi_opbfreq = 33333333;
287 bdloc = ppc405_set_bootinfo(env, &bd, 0x00000001);
289 kernel_base = KERNEL_LOAD_ADDR;
290 /* now we can load the kernel */
291 kernel_size = load_image_targphys(kernel_filename, kernel_base,
292 ram_size - kernel_base);
293 if (kernel_size < 0) {
294 error_report("could not load kernel '%s'", kernel_filename);
297 printf("Load kernel size %ld at " TARGET_FMT_lx,
298 kernel_size, kernel_base);
300 if (initrd_filename) {
301 initrd_base = INITRD_LOAD_ADDR;
302 initrd_size = load_image_targphys(initrd_filename, initrd_base,
303 ram_size - initrd_base);
304 if (initrd_size < 0) {
305 error_report("could not load initial ram disk '%s'",
313 env->gpr[4] = initrd_base;
314 env->gpr[5] = initrd_size;
315 if (kernel_cmdline != NULL) {
316 len = strlen(kernel_cmdline);
317 bdloc -= ((len + 255) & ~255);
318 cpu_physical_memory_write(bdloc, kernel_cmdline, len + 1);
320 env->gpr[7] = bdloc + len;
325 env->nip = KERNEL_LOAD_ADDR;
333 #ifdef DEBUG_BOARD_INIT
334 printf("bdloc " RAM_ADDR_FMT "\n", bdloc);
335 printf("%s: Done\n", __func__);
339 static void ref405ep_class_init(ObjectClass *oc, void *data)
341 MachineClass *mc = MACHINE_CLASS(oc);
343 mc->desc = "ref405ep";
344 mc->init = ref405ep_init;
347 static const TypeInfo ref405ep_type = {
348 .name = MACHINE_TYPE_NAME("ref405ep"),
349 .parent = TYPE_MACHINE,
350 .class_init = ref405ep_class_init,
353 /*****************************************************************************/
354 /* AMCC Taihu evaluation board */
355 /* - PowerPC 405EP processor
356 * - SDRAM 128 MB at 0x00000000
357 * - Boot flash 2 MB at 0xFFE00000
358 * - Application flash 32 MB at 0xFC000000
361 * - 1 USB 1.1 device 0x50000000
362 * - 1 LCD display 0x50100000
363 * - 1 CPLD 0x50100000
365 * - 1 I2C thermal sensor
367 * - bit-bang SPI port using GPIOs
368 * - 1 EBC interface connector 0 0x50200000
369 * - 1 cardbus controller + expansion slot.
370 * - 1 PCI expansion slot.
372 typedef struct taihu_cpld_t taihu_cpld_t;
373 struct taihu_cpld_t {
378 static uint64_t taihu_cpld_read(void *opaque, hwaddr addr, unsigned size)
399 static void taihu_cpld_write(void *opaque, hwaddr addr,
400 uint64_t value, unsigned size)
417 static const MemoryRegionOps taihu_cpld_ops = {
418 .read = taihu_cpld_read,
419 .write = taihu_cpld_write,
421 .min_access_size = 1,
422 .max_access_size = 1,
424 .endianness = DEVICE_NATIVE_ENDIAN,
427 static void taihu_cpld_reset (void *opaque)
436 static void taihu_cpld_init(MemoryRegion *sysmem, uint32_t base)
439 MemoryRegion *cpld_memory = g_new(MemoryRegion, 1);
441 cpld = g_malloc0(sizeof(taihu_cpld_t));
442 memory_region_init_io(cpld_memory, NULL, &taihu_cpld_ops, cpld, "cpld", 0x100);
443 memory_region_add_subregion(sysmem, base, cpld_memory);
444 qemu_register_reset(&taihu_cpld_reset, cpld);
447 static void taihu_405ep_init(MachineState *machine)
449 ram_addr_t ram_size = machine->ram_size;
450 const char *kernel_filename = machine->kernel_filename;
451 const char *initrd_filename = machine->initrd_filename;
454 MemoryRegion *sysmem = get_system_memory();
456 MemoryRegion *ram_memories = g_malloc(2 * sizeof(*ram_memories));
457 MemoryRegion *ram = g_malloc0(sizeof(*ram));
458 hwaddr ram_bases[2], ram_sizes[2];
460 target_ulong kernel_base, initrd_base;
461 long kernel_size, initrd_size;
463 int fl_idx, fl_sectors;
467 if (!qtest_enabled()) {
468 warn_report("qemu-system-ppcemb is deprecated, "
469 "please use qemu-system-ppc instead.");
473 /* RAM is soldered to the board so the size cannot be changed */
474 ram_size = 0x08000000;
475 memory_region_allocate_system_memory(ram, NULL, "taihu_405ep.ram",
479 ram_sizes[0] = 0x04000000;
480 memory_region_init_alias(&ram_memories[0], NULL,
481 "taihu_405ep.ram-0", ram, ram_bases[0],
483 ram_bases[1] = 0x04000000;
484 ram_sizes[1] = 0x04000000;
485 memory_region_init_alias(&ram_memories[1], NULL,
486 "taihu_405ep.ram-1", ram, ram_bases[1],
488 #ifdef DEBUG_BOARD_INIT
489 printf("%s: register cpu\n", __func__);
491 ppc405ep_init(sysmem, ram_memories, ram_bases, ram_sizes,
492 33333333, &pic, kernel_filename == NULL ? 0 : 1);
493 /* allocate and load BIOS */
494 #ifdef DEBUG_BOARD_INIT
495 printf("%s: register BIOS\n", __func__);
498 #if defined(USE_FLASH_BIOS)
499 dinfo = drive_get(IF_PFLASH, 0, fl_idx);
501 BlockBackend *blk = blk_by_legacy_dinfo(dinfo);
503 bios_size = blk_getlength(blk);
504 /* XXX: should check that size is 2MB */
505 // bios_size = 2 * 1024 * 1024;
506 fl_sectors = (bios_size + 65535) >> 16;
507 #ifdef DEBUG_BOARD_INIT
508 printf("Register parallel flash %d size %lx"
509 " at addr %lx '%s' %d\n",
510 fl_idx, bios_size, -bios_size,
511 blk_name(blk), fl_sectors);
513 pflash_cfi02_register((uint32_t)(-bios_size),
514 NULL, "taihu_405ep.bios", bios_size,
515 blk, 65536, fl_sectors, 1,
516 4, 0x0001, 0x22DA, 0x0000, 0x0000, 0x555, 0x2AA,
522 #ifdef DEBUG_BOARD_INIT
523 printf("Load BIOS from file\n");
525 if (bios_name == NULL)
526 bios_name = BIOS_FILENAME;
527 bios = g_new(MemoryRegion, 1);
528 memory_region_init_ram(bios, NULL, "taihu_405ep.bios", BIOS_SIZE,
530 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
532 bios_size = load_image(filename, memory_region_get_ram_ptr(bios));
534 if (bios_size < 0 || bios_size > BIOS_SIZE) {
535 error_report("Could not load PowerPC BIOS '%s'", bios_name);
538 bios_size = (bios_size + 0xfff) & ~0xfff;
539 memory_region_add_subregion(sysmem, (uint32_t)(-bios_size), bios);
540 } else if (!qtest_enabled()) {
541 error_report("Could not load PowerPC BIOS '%s'", bios_name);
544 memory_region_set_readonly(bios, true);
546 /* Register Linux flash */
547 dinfo = drive_get(IF_PFLASH, 0, fl_idx);
549 BlockBackend *blk = blk_by_legacy_dinfo(dinfo);
551 bios_size = blk_getlength(blk);
552 /* XXX: should check that size is 32MB */
553 bios_size = 32 * MiB;
554 fl_sectors = (bios_size + 65535) >> 16;
555 #ifdef DEBUG_BOARD_INIT
556 printf("Register parallel flash %d size %lx"
557 " at addr " TARGET_FMT_lx " '%s'\n",
558 fl_idx, bios_size, (target_ulong)0xfc000000,
561 pflash_cfi02_register(0xfc000000, NULL, "taihu_405ep.flash", bios_size,
562 blk, 65536, fl_sectors, 1,
563 4, 0x0001, 0x22DA, 0x0000, 0x0000, 0x555, 0x2AA,
567 /* Register CLPD & LCD display */
568 #ifdef DEBUG_BOARD_INIT
569 printf("%s: register CPLD\n", __func__);
571 taihu_cpld_init(sysmem, 0x50100000);
573 linux_boot = (kernel_filename != NULL);
575 #ifdef DEBUG_BOARD_INIT
576 printf("%s: load kernel\n", __func__);
578 kernel_base = KERNEL_LOAD_ADDR;
579 /* now we can load the kernel */
580 kernel_size = load_image_targphys(kernel_filename, kernel_base,
581 ram_size - kernel_base);
582 if (kernel_size < 0) {
583 error_report("could not load kernel '%s'", kernel_filename);
587 if (initrd_filename) {
588 initrd_base = INITRD_LOAD_ADDR;
589 initrd_size = load_image_targphys(initrd_filename, initrd_base,
590 ram_size - initrd_base);
591 if (initrd_size < 0) {
592 error_report("could not load initial ram disk '%s'",
606 #ifdef DEBUG_BOARD_INIT
607 printf("%s: Done\n", __func__);
611 static void taihu_class_init(ObjectClass *oc, void *data)
613 MachineClass *mc = MACHINE_CLASS(oc);
616 mc->init = taihu_405ep_init;
619 static const TypeInfo taihu_type = {
620 .name = MACHINE_TYPE_NAME("taihu"),
621 .parent = TYPE_MACHINE,
622 .class_init = taihu_class_init,
625 static void ppc405_machine_init(void)
627 type_register_static(&ref405ep_type);
628 type_register_static(&taihu_type);
631 type_init(ppc405_machine_init)