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 /*****************************************************************************/
52 /* PPC405EP reference board (IBM) */
53 /* Standalone board with:
55 * - SDRAM (0x00000000)
56 * - Flash (0xFFF80000)
58 * - NVRAM (0xF0000000)
61 typedef struct ref405ep_fpga_t ref405ep_fpga_t;
62 struct ref405ep_fpga_t {
67 static uint64_t ref405ep_fpga_readb(void *opaque, hwaddr addr, unsigned size)
69 ref405ep_fpga_t *fpga;
88 static void ref405ep_fpga_writeb(void *opaque, hwaddr addr, uint64_t value,
91 ref405ep_fpga_t *fpga;
106 static const MemoryRegionOps ref405ep_fpga_ops = {
107 .read = ref405ep_fpga_readb,
108 .write = ref405ep_fpga_writeb,
109 .impl.min_access_size = 1,
110 .impl.max_access_size = 1,
111 .valid.min_access_size = 1,
112 .valid.max_access_size = 4,
113 .endianness = DEVICE_BIG_ENDIAN,
116 static void ref405ep_fpga_reset (void *opaque)
118 ref405ep_fpga_t *fpga;
125 static void ref405ep_fpga_init(MemoryRegion *sysmem, uint32_t base)
127 ref405ep_fpga_t *fpga;
128 MemoryRegion *fpga_memory = g_new(MemoryRegion, 1);
130 fpga = g_malloc0(sizeof(ref405ep_fpga_t));
131 memory_region_init_io(fpga_memory, NULL, &ref405ep_fpga_ops, fpga,
133 memory_region_add_subregion(sysmem, base, fpga_memory);
134 qemu_register_reset(&ref405ep_fpga_reset, fpga);
137 static void ref405ep_init(MachineState *machine)
139 ram_addr_t ram_size = machine->ram_size;
140 const char *kernel_filename = machine->kernel_filename;
141 const char *kernel_cmdline = machine->kernel_cmdline;
142 const char *initrd_filename = machine->initrd_filename;
148 MemoryRegion *sram = g_new(MemoryRegion, 1);
150 MemoryRegion *ram_memories = g_new(MemoryRegion, 2);
151 hwaddr ram_bases[2], ram_sizes[2];
152 target_ulong sram_size;
155 //static int phy_addr = 1;
156 target_ulong kernel_base, initrd_base;
157 long kernel_size, initrd_size;
159 int fl_idx, fl_sectors, len;
161 MemoryRegion *sysmem = get_system_memory();
164 memory_region_allocate_system_memory(&ram_memories[0], NULL, "ef405ep.ram",
167 ram_sizes[0] = 0x08000000;
168 memory_region_init(&ram_memories[1], NULL, "ef405ep.ram1", 0);
169 ram_bases[1] = 0x00000000;
170 ram_sizes[1] = 0x00000000;
171 ram_size = 128 * MiB;
172 env = ppc405ep_init(sysmem, ram_memories, ram_bases, ram_sizes,
173 33333333, &pic, kernel_filename == NULL ? 0 : 1);
175 sram_size = 512 * KiB;
176 memory_region_init_ram(sram, NULL, "ef405ep.sram", sram_size,
178 memory_region_add_subregion(sysmem, 0xFFF00000, sram);
179 /* allocate and load BIOS */
181 #ifdef USE_FLASH_BIOS
182 dinfo = drive_get(IF_PFLASH, 0, fl_idx);
184 BlockBackend *blk = blk_by_legacy_dinfo(dinfo);
186 bios_size = blk_getlength(blk);
187 fl_sectors = (bios_size + 65535) >> 16;
188 pflash_cfi02_register((uint32_t)(-bios_size),
189 NULL, "ef405ep.bios", bios_size,
190 blk, 65536, fl_sectors, 1,
191 2, 0x0001, 0x22DA, 0x0000, 0x0000, 0x555, 0x2AA,
197 bios = g_new(MemoryRegion, 1);
198 memory_region_init_ram(bios, NULL, "ef405ep.bios", BIOS_SIZE,
201 if (bios_name == NULL)
202 bios_name = BIOS_FILENAME;
203 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
205 bios_size = load_image_size(filename,
206 memory_region_get_ram_ptr(bios),
210 error_report("Could not load PowerPC BIOS '%s'", bios_name);
213 bios_size = (bios_size + 0xfff) & ~0xfff;
214 memory_region_add_subregion(sysmem, (uint32_t)(-bios_size), bios);
215 } else if (!qtest_enabled() || kernel_filename != NULL) {
216 error_report("Could not load PowerPC BIOS '%s'", bios_name);
219 /* Avoid an uninitialized variable warning */
222 memory_region_set_readonly(bios, true);
225 ref405ep_fpga_init(sysmem, 0xF0300000);
227 m48t59_init(NULL, 0xF0000000, 0, 8192, 1968, 8);
229 linux_boot = (kernel_filename != NULL);
231 memset(&bd, 0, sizeof(bd));
232 bd.bi_memstart = 0x00000000;
233 bd.bi_memsize = ram_size;
234 bd.bi_flashstart = -bios_size;
235 bd.bi_flashsize = -bios_size;
236 bd.bi_flashoffset = 0;
237 bd.bi_sramstart = 0xFFF00000;
238 bd.bi_sramsize = sram_size;
240 bd.bi_intfreq = 133333333;
241 bd.bi_busfreq = 33333333;
242 bd.bi_baudrate = 115200;
243 bd.bi_s_version[0] = 'Q';
244 bd.bi_s_version[1] = 'M';
245 bd.bi_s_version[2] = 'U';
246 bd.bi_s_version[3] = '\0';
247 bd.bi_r_version[0] = 'Q';
248 bd.bi_r_version[1] = 'E';
249 bd.bi_r_version[2] = 'M';
250 bd.bi_r_version[3] = 'U';
251 bd.bi_r_version[4] = '\0';
252 bd.bi_procfreq = 133333333;
253 bd.bi_plb_busfreq = 33333333;
254 bd.bi_pci_busfreq = 33333333;
255 bd.bi_opbfreq = 33333333;
256 bdloc = ppc405_set_bootinfo(env, &bd, 0x00000001);
258 kernel_base = KERNEL_LOAD_ADDR;
259 /* now we can load the kernel */
260 kernel_size = load_image_targphys(kernel_filename, kernel_base,
261 ram_size - kernel_base);
262 if (kernel_size < 0) {
263 error_report("could not load kernel '%s'", kernel_filename);
266 printf("Load kernel size %ld at " TARGET_FMT_lx,
267 kernel_size, kernel_base);
269 if (initrd_filename) {
270 initrd_base = INITRD_LOAD_ADDR;
271 initrd_size = load_image_targphys(initrd_filename, initrd_base,
272 ram_size - initrd_base);
273 if (initrd_size < 0) {
274 error_report("could not load initial ram disk '%s'",
282 env->gpr[4] = initrd_base;
283 env->gpr[5] = initrd_size;
284 if (kernel_cmdline != NULL) {
285 len = strlen(kernel_cmdline);
286 bdloc -= ((len + 255) & ~255);
287 cpu_physical_memory_write(bdloc, kernel_cmdline, len + 1);
289 env->gpr[7] = bdloc + len;
294 env->nip = KERNEL_LOAD_ADDR;
304 static void ref405ep_class_init(ObjectClass *oc, void *data)
306 MachineClass *mc = MACHINE_CLASS(oc);
308 mc->desc = "ref405ep";
309 mc->init = ref405ep_init;
312 static const TypeInfo ref405ep_type = {
313 .name = MACHINE_TYPE_NAME("ref405ep"),
314 .parent = TYPE_MACHINE,
315 .class_init = ref405ep_class_init,
318 /*****************************************************************************/
319 /* AMCC Taihu evaluation board */
320 /* - PowerPC 405EP processor
321 * - SDRAM 128 MB at 0x00000000
322 * - Boot flash 2 MB at 0xFFE00000
323 * - Application flash 32 MB at 0xFC000000
326 * - 1 USB 1.1 device 0x50000000
327 * - 1 LCD display 0x50100000
328 * - 1 CPLD 0x50100000
330 * - 1 I2C thermal sensor
332 * - bit-bang SPI port using GPIOs
333 * - 1 EBC interface connector 0 0x50200000
334 * - 1 cardbus controller + expansion slot.
335 * - 1 PCI expansion slot.
337 typedef struct taihu_cpld_t taihu_cpld_t;
338 struct taihu_cpld_t {
343 static uint64_t taihu_cpld_read(void *opaque, hwaddr addr, unsigned size)
364 static void taihu_cpld_write(void *opaque, hwaddr addr,
365 uint64_t value, unsigned size)
382 static const MemoryRegionOps taihu_cpld_ops = {
383 .read = taihu_cpld_read,
384 .write = taihu_cpld_write,
386 .min_access_size = 1,
387 .max_access_size = 1,
389 .endianness = DEVICE_NATIVE_ENDIAN,
392 static void taihu_cpld_reset (void *opaque)
401 static void taihu_cpld_init(MemoryRegion *sysmem, uint32_t base)
404 MemoryRegion *cpld_memory = g_new(MemoryRegion, 1);
406 cpld = g_malloc0(sizeof(taihu_cpld_t));
407 memory_region_init_io(cpld_memory, NULL, &taihu_cpld_ops, cpld, "cpld", 0x100);
408 memory_region_add_subregion(sysmem, base, cpld_memory);
409 qemu_register_reset(&taihu_cpld_reset, cpld);
412 static void taihu_405ep_init(MachineState *machine)
414 ram_addr_t ram_size = machine->ram_size;
415 const char *kernel_filename = machine->kernel_filename;
416 const char *initrd_filename = machine->initrd_filename;
419 MemoryRegion *sysmem = get_system_memory();
421 MemoryRegion *ram_memories = g_new(MemoryRegion, 2);
422 MemoryRegion *ram = g_malloc0(sizeof(*ram));
423 hwaddr ram_bases[2], ram_sizes[2];
425 target_ulong kernel_base, initrd_base;
426 long kernel_size, initrd_size;
428 int fl_idx, fl_sectors;
431 /* RAM is soldered to the board so the size cannot be changed */
432 ram_size = 0x08000000;
433 memory_region_allocate_system_memory(ram, NULL, "taihu_405ep.ram",
437 ram_sizes[0] = 0x04000000;
438 memory_region_init_alias(&ram_memories[0], NULL,
439 "taihu_405ep.ram-0", ram, ram_bases[0],
441 ram_bases[1] = 0x04000000;
442 ram_sizes[1] = 0x04000000;
443 memory_region_init_alias(&ram_memories[1], NULL,
444 "taihu_405ep.ram-1", ram, ram_bases[1],
446 ppc405ep_init(sysmem, ram_memories, ram_bases, ram_sizes,
447 33333333, &pic, kernel_filename == NULL ? 0 : 1);
448 /* allocate and load BIOS */
450 #if defined(USE_FLASH_BIOS)
451 dinfo = drive_get(IF_PFLASH, 0, fl_idx);
453 BlockBackend *blk = blk_by_legacy_dinfo(dinfo);
455 bios_size = blk_getlength(blk);
456 /* XXX: should check that size is 2MB */
457 // bios_size = 2 * 1024 * 1024;
458 fl_sectors = (bios_size + 65535) >> 16;
459 pflash_cfi02_register((uint32_t)(-bios_size),
460 NULL, "taihu_405ep.bios", bios_size,
461 blk, 65536, fl_sectors, 1,
462 4, 0x0001, 0x22DA, 0x0000, 0x0000, 0x555, 0x2AA,
468 if (bios_name == NULL)
469 bios_name = BIOS_FILENAME;
470 bios = g_new(MemoryRegion, 1);
471 memory_region_init_ram(bios, NULL, "taihu_405ep.bios", BIOS_SIZE,
473 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
475 bios_size = load_image_size(filename,
476 memory_region_get_ram_ptr(bios),
480 error_report("Could not load PowerPC BIOS '%s'", bios_name);
483 bios_size = (bios_size + 0xfff) & ~0xfff;
484 memory_region_add_subregion(sysmem, (uint32_t)(-bios_size), bios);
485 } else if (!qtest_enabled()) {
486 error_report("Could not load PowerPC BIOS '%s'", bios_name);
489 memory_region_set_readonly(bios, true);
491 /* Register Linux flash */
492 dinfo = drive_get(IF_PFLASH, 0, fl_idx);
494 BlockBackend *blk = blk_by_legacy_dinfo(dinfo);
496 bios_size = blk_getlength(blk);
497 /* XXX: should check that size is 32MB */
498 bios_size = 32 * MiB;
499 fl_sectors = (bios_size + 65535) >> 16;
500 pflash_cfi02_register(0xfc000000, NULL, "taihu_405ep.flash", bios_size,
501 blk, 65536, fl_sectors, 1,
502 4, 0x0001, 0x22DA, 0x0000, 0x0000, 0x555, 0x2AA,
506 /* Register CLPD & LCD display */
507 taihu_cpld_init(sysmem, 0x50100000);
509 linux_boot = (kernel_filename != NULL);
511 kernel_base = KERNEL_LOAD_ADDR;
512 /* now we can load the kernel */
513 kernel_size = load_image_targphys(kernel_filename, kernel_base,
514 ram_size - kernel_base);
515 if (kernel_size < 0) {
516 error_report("could not load kernel '%s'", kernel_filename);
520 if (initrd_filename) {
521 initrd_base = INITRD_LOAD_ADDR;
522 initrd_size = load_image_targphys(initrd_filename, initrd_base,
523 ram_size - initrd_base);
524 if (initrd_size < 0) {
525 error_report("could not load initial ram disk '%s'",
541 static void taihu_class_init(ObjectClass *oc, void *data)
543 MachineClass *mc = MACHINE_CLASS(oc);
546 mc->init = taihu_405ep_init;
549 static const TypeInfo taihu_type = {
550 .name = MACHINE_TYPE_NAME("taihu"),
551 .parent = TYPE_MACHINE,
552 .class_init = taihu_class_init,
555 static void ppc405_machine_init(void)
557 type_register_static(&ref405ep_type);
558 type_register_static(&taihu_type);
561 type_init(ppc405_machine_init)