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 "hw/ppc/ppc.h"
27 #include "hw/timer/m48t59.h"
28 #include "hw/block/flash.h"
29 #include "sysemu/sysemu.h"
30 #include "sysemu/qtest.h"
31 #include "sysemu/block-backend.h"
32 #include "hw/boards.h"
34 #include "qemu/error-report.h"
35 #include "hw/loader.h"
36 #include "sysemu/block-backend.h"
37 #include "sysemu/blockdev.h"
38 #include "exec/address-spaces.h"
40 #define BIOS_FILENAME "ppc405_rom.bin"
41 #define BIOS_SIZE (2048 * 1024)
43 #define KERNEL_LOAD_ADDR 0x00000000
44 #define INITRD_LOAD_ADDR 0x01800000
46 #define USE_FLASH_BIOS
48 //#define DEBUG_BOARD_INIT
50 /*****************************************************************************/
51 /* PPC405EP reference board (IBM) */
52 /* Standalone board with:
54 * - SDRAM (0x00000000)
55 * - Flash (0xFFF80000)
57 * - NVRAM (0xF0000000)
60 typedef struct ref405ep_fpga_t ref405ep_fpga_t;
61 struct ref405ep_fpga_t {
66 static uint32_t ref405ep_fpga_readb (void *opaque, hwaddr addr)
68 ref405ep_fpga_t *fpga;
87 static void ref405ep_fpga_writeb (void *opaque,
88 hwaddr addr, uint32_t value)
90 ref405ep_fpga_t *fpga;
105 static uint32_t ref405ep_fpga_readw (void *opaque, hwaddr addr)
109 ret = ref405ep_fpga_readb(opaque, addr) << 8;
110 ret |= ref405ep_fpga_readb(opaque, addr + 1);
115 static void ref405ep_fpga_writew (void *opaque,
116 hwaddr addr, uint32_t value)
118 ref405ep_fpga_writeb(opaque, addr, (value >> 8) & 0xFF);
119 ref405ep_fpga_writeb(opaque, addr + 1, value & 0xFF);
122 static uint32_t ref405ep_fpga_readl (void *opaque, hwaddr addr)
126 ret = ref405ep_fpga_readb(opaque, addr) << 24;
127 ret |= ref405ep_fpga_readb(opaque, addr + 1) << 16;
128 ret |= ref405ep_fpga_readb(opaque, addr + 2) << 8;
129 ret |= ref405ep_fpga_readb(opaque, addr + 3);
134 static void ref405ep_fpga_writel (void *opaque,
135 hwaddr addr, uint32_t value)
137 ref405ep_fpga_writeb(opaque, addr, (value >> 24) & 0xFF);
138 ref405ep_fpga_writeb(opaque, addr + 1, (value >> 16) & 0xFF);
139 ref405ep_fpga_writeb(opaque, addr + 2, (value >> 8) & 0xFF);
140 ref405ep_fpga_writeb(opaque, addr + 3, value & 0xFF);
143 static const MemoryRegionOps ref405ep_fpga_ops = {
146 ref405ep_fpga_readb, ref405ep_fpga_readw, ref405ep_fpga_readl,
149 ref405ep_fpga_writeb, ref405ep_fpga_writew, ref405ep_fpga_writel,
152 .endianness = DEVICE_NATIVE_ENDIAN,
155 static void ref405ep_fpga_reset (void *opaque)
157 ref405ep_fpga_t *fpga;
164 static void ref405ep_fpga_init(MemoryRegion *sysmem, uint32_t base)
166 ref405ep_fpga_t *fpga;
167 MemoryRegion *fpga_memory = g_new(MemoryRegion, 1);
169 fpga = g_malloc0(sizeof(ref405ep_fpga_t));
170 memory_region_init_io(fpga_memory, NULL, &ref405ep_fpga_ops, fpga,
172 memory_region_add_subregion(sysmem, base, fpga_memory);
173 qemu_register_reset(&ref405ep_fpga_reset, fpga);
176 static void ref405ep_init(MachineState *machine)
178 ram_addr_t ram_size = machine->ram_size;
179 const char *kernel_filename = machine->kernel_filename;
180 const char *kernel_cmdline = machine->kernel_cmdline;
181 const char *initrd_filename = machine->initrd_filename;
187 MemoryRegion *sram = g_new(MemoryRegion, 1);
189 MemoryRegion *ram_memories = g_malloc(2 * sizeof(*ram_memories));
190 hwaddr ram_bases[2], ram_sizes[2];
191 target_ulong sram_size;
194 //static int phy_addr = 1;
195 target_ulong kernel_base, initrd_base;
196 long kernel_size, initrd_size;
198 int fl_idx, fl_sectors, len;
200 MemoryRegion *sysmem = get_system_memory();
203 memory_region_allocate_system_memory(&ram_memories[0], NULL, "ef405ep.ram",
206 ram_sizes[0] = 0x08000000;
207 memory_region_init(&ram_memories[1], NULL, "ef405ep.ram1", 0);
208 ram_bases[1] = 0x00000000;
209 ram_sizes[1] = 0x00000000;
210 ram_size = 128 * 1024 * 1024;
211 #ifdef DEBUG_BOARD_INIT
212 printf("%s: register cpu\n", __func__);
214 env = ppc405ep_init(sysmem, ram_memories, ram_bases, ram_sizes,
215 33333333, &pic, kernel_filename == NULL ? 0 : 1);
217 sram_size = 512 * 1024;
218 memory_region_init_ram(sram, NULL, "ef405ep.sram", sram_size, &error_abort);
219 vmstate_register_ram_global(sram);
220 memory_region_add_subregion(sysmem, 0xFFF00000, sram);
221 /* allocate and load BIOS */
222 #ifdef DEBUG_BOARD_INIT
223 printf("%s: register BIOS\n", __func__);
226 #ifdef USE_FLASH_BIOS
227 dinfo = drive_get(IF_PFLASH, 0, fl_idx);
229 BlockBackend *blk = blk_by_legacy_dinfo(dinfo);
231 bios_size = blk_getlength(blk);
232 fl_sectors = (bios_size + 65535) >> 16;
233 #ifdef DEBUG_BOARD_INIT
234 printf("Register parallel flash %d size %lx"
235 " at addr %lx '%s' %d\n",
236 fl_idx, bios_size, -bios_size,
237 blk_name(blk), fl_sectors);
239 pflash_cfi02_register((uint32_t)(-bios_size),
240 NULL, "ef405ep.bios", bios_size,
241 blk, 65536, fl_sectors, 1,
242 2, 0x0001, 0x22DA, 0x0000, 0x0000, 0x555, 0x2AA,
248 #ifdef DEBUG_BOARD_INIT
249 printf("Load BIOS from file\n");
251 bios = g_new(MemoryRegion, 1);
252 memory_region_init_ram(bios, NULL, "ef405ep.bios", BIOS_SIZE,
254 vmstate_register_ram_global(bios);
256 if (bios_name == NULL)
257 bios_name = BIOS_FILENAME;
258 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
260 bios_size = load_image(filename, memory_region_get_ram_ptr(bios));
262 if (bios_size < 0 || bios_size > BIOS_SIZE) {
263 error_report("Could not load PowerPC BIOS '%s'", bios_name);
266 bios_size = (bios_size + 0xfff) & ~0xfff;
267 memory_region_add_subregion(sysmem, (uint32_t)(-bios_size), bios);
268 } else if (!qtest_enabled() || kernel_filename != NULL) {
269 error_report("Could not load PowerPC BIOS '%s'", bios_name);
272 /* Avoid an uninitialized variable warning */
275 memory_region_set_readonly(bios, true);
278 #ifdef DEBUG_BOARD_INIT
279 printf("%s: register FPGA\n", __func__);
281 ref405ep_fpga_init(sysmem, 0xF0300000);
283 #ifdef DEBUG_BOARD_INIT
284 printf("%s: register NVRAM\n", __func__);
286 m48t59_init(NULL, 0xF0000000, 0, 8192, 1968, 8);
288 linux_boot = (kernel_filename != NULL);
290 #ifdef DEBUG_BOARD_INIT
291 printf("%s: load kernel\n", __func__);
293 memset(&bd, 0, sizeof(bd));
294 bd.bi_memstart = 0x00000000;
295 bd.bi_memsize = ram_size;
296 bd.bi_flashstart = -bios_size;
297 bd.bi_flashsize = -bios_size;
298 bd.bi_flashoffset = 0;
299 bd.bi_sramstart = 0xFFF00000;
300 bd.bi_sramsize = sram_size;
302 bd.bi_intfreq = 133333333;
303 bd.bi_busfreq = 33333333;
304 bd.bi_baudrate = 115200;
305 bd.bi_s_version[0] = 'Q';
306 bd.bi_s_version[1] = 'M';
307 bd.bi_s_version[2] = 'U';
308 bd.bi_s_version[3] = '\0';
309 bd.bi_r_version[0] = 'Q';
310 bd.bi_r_version[1] = 'E';
311 bd.bi_r_version[2] = 'M';
312 bd.bi_r_version[3] = 'U';
313 bd.bi_r_version[4] = '\0';
314 bd.bi_procfreq = 133333333;
315 bd.bi_plb_busfreq = 33333333;
316 bd.bi_pci_busfreq = 33333333;
317 bd.bi_opbfreq = 33333333;
318 bdloc = ppc405_set_bootinfo(env, &bd, 0x00000001);
320 kernel_base = KERNEL_LOAD_ADDR;
321 /* now we can load the kernel */
322 kernel_size = load_image_targphys(kernel_filename, kernel_base,
323 ram_size - kernel_base);
324 if (kernel_size < 0) {
325 fprintf(stderr, "qemu: could not load kernel '%s'\n",
329 printf("Load kernel size %ld at " TARGET_FMT_lx,
330 kernel_size, kernel_base);
332 if (initrd_filename) {
333 initrd_base = INITRD_LOAD_ADDR;
334 initrd_size = load_image_targphys(initrd_filename, initrd_base,
335 ram_size - initrd_base);
336 if (initrd_size < 0) {
337 fprintf(stderr, "qemu: could not load initial ram disk '%s'\n",
345 env->gpr[4] = initrd_base;
346 env->gpr[5] = initrd_size;
347 if (kernel_cmdline != NULL) {
348 len = strlen(kernel_cmdline);
349 bdloc -= ((len + 255) & ~255);
350 cpu_physical_memory_write(bdloc, kernel_cmdline, len + 1);
352 env->gpr[7] = bdloc + len;
357 env->nip = KERNEL_LOAD_ADDR;
365 #ifdef DEBUG_BOARD_INIT
366 printf("bdloc " RAM_ADDR_FMT "\n", bdloc);
367 printf("%s: Done\n", __func__);
371 static QEMUMachine ref405ep_machine = {
374 .init = ref405ep_init,
377 /*****************************************************************************/
378 /* AMCC Taihu evaluation board */
379 /* - PowerPC 405EP processor
380 * - SDRAM 128 MB at 0x00000000
381 * - Boot flash 2 MB at 0xFFE00000
382 * - Application flash 32 MB at 0xFC000000
385 * - 1 USB 1.1 device 0x50000000
386 * - 1 LCD display 0x50100000
387 * - 1 CPLD 0x50100000
389 * - 1 I2C thermal sensor
391 * - bit-bang SPI port using GPIOs
392 * - 1 EBC interface connector 0 0x50200000
393 * - 1 cardbus controller + expansion slot.
394 * - 1 PCI expansion slot.
396 typedef struct taihu_cpld_t taihu_cpld_t;
397 struct taihu_cpld_t {
402 static uint32_t taihu_cpld_readb (void *opaque, hwaddr addr)
423 static void taihu_cpld_writeb (void *opaque,
424 hwaddr addr, uint32_t value)
441 static uint32_t taihu_cpld_readw (void *opaque, hwaddr addr)
445 ret = taihu_cpld_readb(opaque, addr) << 8;
446 ret |= taihu_cpld_readb(opaque, addr + 1);
451 static void taihu_cpld_writew (void *opaque,
452 hwaddr addr, uint32_t value)
454 taihu_cpld_writeb(opaque, addr, (value >> 8) & 0xFF);
455 taihu_cpld_writeb(opaque, addr + 1, value & 0xFF);
458 static uint32_t taihu_cpld_readl (void *opaque, hwaddr addr)
462 ret = taihu_cpld_readb(opaque, addr) << 24;
463 ret |= taihu_cpld_readb(opaque, addr + 1) << 16;
464 ret |= taihu_cpld_readb(opaque, addr + 2) << 8;
465 ret |= taihu_cpld_readb(opaque, addr + 3);
470 static void taihu_cpld_writel (void *opaque,
471 hwaddr addr, uint32_t value)
473 taihu_cpld_writel(opaque, addr, (value >> 24) & 0xFF);
474 taihu_cpld_writel(opaque, addr + 1, (value >> 16) & 0xFF);
475 taihu_cpld_writel(opaque, addr + 2, (value >> 8) & 0xFF);
476 taihu_cpld_writeb(opaque, addr + 3, value & 0xFF);
479 static const MemoryRegionOps taihu_cpld_ops = {
481 .read = { taihu_cpld_readb, taihu_cpld_readw, taihu_cpld_readl, },
482 .write = { taihu_cpld_writeb, taihu_cpld_writew, taihu_cpld_writel, },
484 .endianness = DEVICE_NATIVE_ENDIAN,
487 static void taihu_cpld_reset (void *opaque)
496 static void taihu_cpld_init(MemoryRegion *sysmem, uint32_t base)
499 MemoryRegion *cpld_memory = g_new(MemoryRegion, 1);
501 cpld = g_malloc0(sizeof(taihu_cpld_t));
502 memory_region_init_io(cpld_memory, NULL, &taihu_cpld_ops, cpld, "cpld", 0x100);
503 memory_region_add_subregion(sysmem, base, cpld_memory);
504 qemu_register_reset(&taihu_cpld_reset, cpld);
507 static void taihu_405ep_init(MachineState *machine)
509 ram_addr_t ram_size = machine->ram_size;
510 const char *kernel_filename = machine->kernel_filename;
511 const char *initrd_filename = machine->initrd_filename;
514 MemoryRegion *sysmem = get_system_memory();
516 MemoryRegion *ram_memories = g_malloc(2 * sizeof(*ram_memories));
517 MemoryRegion *ram = g_malloc0(sizeof(*ram));
518 hwaddr ram_bases[2], ram_sizes[2];
520 target_ulong kernel_base, initrd_base;
521 long kernel_size, initrd_size;
523 int fl_idx, fl_sectors;
526 /* RAM is soldered to the board so the size cannot be changed */
527 ram_size = 0x08000000;
528 memory_region_allocate_system_memory(ram, NULL, "taihu_405ep.ram",
532 ram_sizes[0] = 0x04000000;
533 memory_region_init_alias(&ram_memories[0], NULL,
534 "taihu_405ep.ram-0", ram, ram_bases[0],
536 ram_bases[1] = 0x04000000;
537 ram_sizes[1] = 0x04000000;
538 memory_region_init_alias(&ram_memories[1], NULL,
539 "taihu_405ep.ram-1", ram, ram_bases[1],
541 #ifdef DEBUG_BOARD_INIT
542 printf("%s: register cpu\n", __func__);
544 ppc405ep_init(sysmem, ram_memories, ram_bases, ram_sizes,
545 33333333, &pic, kernel_filename == NULL ? 0 : 1);
546 /* allocate and load BIOS */
547 #ifdef DEBUG_BOARD_INIT
548 printf("%s: register BIOS\n", __func__);
551 #if defined(USE_FLASH_BIOS)
552 dinfo = drive_get(IF_PFLASH, 0, fl_idx);
554 BlockBackend *blk = blk_by_legacy_dinfo(dinfo);
556 bios_size = blk_getlength(blk);
557 /* XXX: should check that size is 2MB */
558 // bios_size = 2 * 1024 * 1024;
559 fl_sectors = (bios_size + 65535) >> 16;
560 #ifdef DEBUG_BOARD_INIT
561 printf("Register parallel flash %d size %lx"
562 " at addr %lx '%s' %d\n",
563 fl_idx, bios_size, -bios_size,
564 blk_name(blk), fl_sectors);
566 pflash_cfi02_register((uint32_t)(-bios_size),
567 NULL, "taihu_405ep.bios", bios_size,
568 blk, 65536, fl_sectors, 1,
569 4, 0x0001, 0x22DA, 0x0000, 0x0000, 0x555, 0x2AA,
575 #ifdef DEBUG_BOARD_INIT
576 printf("Load BIOS from file\n");
578 if (bios_name == NULL)
579 bios_name = BIOS_FILENAME;
580 bios = g_new(MemoryRegion, 1);
581 memory_region_init_ram(bios, NULL, "taihu_405ep.bios", BIOS_SIZE,
583 vmstate_register_ram_global(bios);
584 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
586 bios_size = load_image(filename, memory_region_get_ram_ptr(bios));
588 if (bios_size < 0 || bios_size > BIOS_SIZE) {
589 error_report("Could not load PowerPC BIOS '%s'", bios_name);
592 bios_size = (bios_size + 0xfff) & ~0xfff;
593 memory_region_add_subregion(sysmem, (uint32_t)(-bios_size), bios);
594 } else if (!qtest_enabled()) {
595 error_report("Could not load PowerPC BIOS '%s'", bios_name);
598 memory_region_set_readonly(bios, true);
600 /* Register Linux flash */
601 dinfo = drive_get(IF_PFLASH, 0, fl_idx);
603 BlockBackend *blk = blk_by_legacy_dinfo(dinfo);
605 bios_size = blk_getlength(blk);
606 /* XXX: should check that size is 32MB */
607 bios_size = 32 * 1024 * 1024;
608 fl_sectors = (bios_size + 65535) >> 16;
609 #ifdef DEBUG_BOARD_INIT
610 printf("Register parallel flash %d size %lx"
611 " at addr " TARGET_FMT_lx " '%s'\n",
612 fl_idx, bios_size, (target_ulong)0xfc000000,
615 pflash_cfi02_register(0xfc000000, NULL, "taihu_405ep.flash", bios_size,
616 blk, 65536, fl_sectors, 1,
617 4, 0x0001, 0x22DA, 0x0000, 0x0000, 0x555, 0x2AA,
621 /* Register CLPD & LCD display */
622 #ifdef DEBUG_BOARD_INIT
623 printf("%s: register CPLD\n", __func__);
625 taihu_cpld_init(sysmem, 0x50100000);
627 linux_boot = (kernel_filename != NULL);
629 #ifdef DEBUG_BOARD_INIT
630 printf("%s: load kernel\n", __func__);
632 kernel_base = KERNEL_LOAD_ADDR;
633 /* now we can load the kernel */
634 kernel_size = load_image_targphys(kernel_filename, kernel_base,
635 ram_size - kernel_base);
636 if (kernel_size < 0) {
637 fprintf(stderr, "qemu: could not load kernel '%s'\n",
642 if (initrd_filename) {
643 initrd_base = INITRD_LOAD_ADDR;
644 initrd_size = load_image_targphys(initrd_filename, initrd_base,
645 ram_size - initrd_base);
646 if (initrd_size < 0) {
648 "qemu: could not load initial ram disk '%s'\n",
662 #ifdef DEBUG_BOARD_INIT
663 printf("%s: Done\n", __func__);
667 static QEMUMachine taihu_machine = {
670 .init = taihu_405ep_init,
673 static void ppc405_machine_init(void)
675 qemu_register_machine(&ref405ep_machine);
676 qemu_register_machine(&taihu_machine);
679 machine_init(ppc405_machine_init);