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 "qapi/error.h"
26 #include "qemu-common.h"
29 #include "hw/ppc/ppc.h"
31 #include "hw/timer/m48t59.h"
32 #include "hw/block/flash.h"
33 #include "sysemu/sysemu.h"
34 #include "sysemu/qtest.h"
35 #include "sysemu/block-backend.h"
36 #include "hw/boards.h"
38 #include "qemu/error-report.h"
39 #include "hw/loader.h"
40 #include "sysemu/blockdev.h"
41 #include "exec/address-spaces.h"
43 #define BIOS_FILENAME "ppc405_rom.bin"
44 #define BIOS_SIZE (2048 * 1024)
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 uint32_t ref405ep_fpga_readb (void *opaque
, hwaddr addr
)
71 ref405ep_fpga_t
*fpga
;
90 static void ref405ep_fpga_writeb (void *opaque
,
91 hwaddr addr
, uint32_t value
)
93 ref405ep_fpga_t
*fpga
;
108 static uint32_t ref405ep_fpga_readw (void *opaque
, hwaddr addr
)
112 ret
= ref405ep_fpga_readb(opaque
, addr
) << 8;
113 ret
|= ref405ep_fpga_readb(opaque
, addr
+ 1);
118 static void ref405ep_fpga_writew (void *opaque
,
119 hwaddr addr
, uint32_t value
)
121 ref405ep_fpga_writeb(opaque
, addr
, (value
>> 8) & 0xFF);
122 ref405ep_fpga_writeb(opaque
, addr
+ 1, value
& 0xFF);
125 static uint32_t ref405ep_fpga_readl (void *opaque
, hwaddr addr
)
129 ret
= ref405ep_fpga_readb(opaque
, addr
) << 24;
130 ret
|= ref405ep_fpga_readb(opaque
, addr
+ 1) << 16;
131 ret
|= ref405ep_fpga_readb(opaque
, addr
+ 2) << 8;
132 ret
|= ref405ep_fpga_readb(opaque
, addr
+ 3);
137 static void ref405ep_fpga_writel (void *opaque
,
138 hwaddr addr
, uint32_t value
)
140 ref405ep_fpga_writeb(opaque
, addr
, (value
>> 24) & 0xFF);
141 ref405ep_fpga_writeb(opaque
, addr
+ 1, (value
>> 16) & 0xFF);
142 ref405ep_fpga_writeb(opaque
, addr
+ 2, (value
>> 8) & 0xFF);
143 ref405ep_fpga_writeb(opaque
, addr
+ 3, value
& 0xFF);
146 static const MemoryRegionOps ref405ep_fpga_ops
= {
149 ref405ep_fpga_readb
, ref405ep_fpga_readw
, ref405ep_fpga_readl
,
152 ref405ep_fpga_writeb
, ref405ep_fpga_writew
, ref405ep_fpga_writel
,
155 .endianness
= DEVICE_NATIVE_ENDIAN
,
158 static void ref405ep_fpga_reset (void *opaque
)
160 ref405ep_fpga_t
*fpga
;
167 static void ref405ep_fpga_init(MemoryRegion
*sysmem
, uint32_t base
)
169 ref405ep_fpga_t
*fpga
;
170 MemoryRegion
*fpga_memory
= g_new(MemoryRegion
, 1);
172 fpga
= g_malloc0(sizeof(ref405ep_fpga_t
));
173 memory_region_init_io(fpga_memory
, NULL
, &ref405ep_fpga_ops
, fpga
,
175 memory_region_add_subregion(sysmem
, base
, fpga_memory
);
176 qemu_register_reset(&ref405ep_fpga_reset
, fpga
);
179 static void ref405ep_init(MachineState
*machine
)
181 ram_addr_t ram_size
= machine
->ram_size
;
182 const char *kernel_filename
= machine
->kernel_filename
;
183 const char *kernel_cmdline
= machine
->kernel_cmdline
;
184 const char *initrd_filename
= machine
->initrd_filename
;
190 MemoryRegion
*sram
= g_new(MemoryRegion
, 1);
192 MemoryRegion
*ram_memories
= g_malloc(2 * sizeof(*ram_memories
));
193 hwaddr ram_bases
[2], ram_sizes
[2];
194 target_ulong sram_size
;
197 //static int phy_addr = 1;
198 target_ulong kernel_base
, initrd_base
;
199 long kernel_size
, initrd_size
;
201 int fl_idx
, fl_sectors
, len
;
203 MemoryRegion
*sysmem
= get_system_memory();
206 if (!qtest_enabled()) {
207 warn_report("qemu-system-ppcemb is deprecated, "
208 "please use qemu-system-ppc instead.");
213 memory_region_allocate_system_memory(&ram_memories
[0], NULL
, "ef405ep.ram",
216 ram_sizes
[0] = 0x08000000;
217 memory_region_init(&ram_memories
[1], NULL
, "ef405ep.ram1", 0);
218 ram_bases
[1] = 0x00000000;
219 ram_sizes
[1] = 0x00000000;
220 ram_size
= 128 * 1024 * 1024;
221 #ifdef DEBUG_BOARD_INIT
222 printf("%s: register cpu\n", __func__
);
224 env
= ppc405ep_init(sysmem
, ram_memories
, ram_bases
, ram_sizes
,
225 33333333, &pic
, kernel_filename
== NULL
? 0 : 1);
227 sram_size
= 512 * 1024;
228 memory_region_init_ram(sram
, NULL
, "ef405ep.sram", sram_size
,
230 memory_region_add_subregion(sysmem
, 0xFFF00000, sram
);
231 /* allocate and load BIOS */
232 #ifdef DEBUG_BOARD_INIT
233 printf("%s: register BIOS\n", __func__
);
236 #ifdef USE_FLASH_BIOS
237 dinfo
= drive_get(IF_PFLASH
, 0, fl_idx
);
239 BlockBackend
*blk
= blk_by_legacy_dinfo(dinfo
);
241 bios_size
= blk_getlength(blk
);
242 fl_sectors
= (bios_size
+ 65535) >> 16;
243 #ifdef DEBUG_BOARD_INIT
244 printf("Register parallel flash %d size %lx"
245 " at addr %lx '%s' %d\n",
246 fl_idx
, bios_size
, -bios_size
,
247 blk_name(blk
), fl_sectors
);
249 pflash_cfi02_register((uint32_t)(-bios_size
),
250 NULL
, "ef405ep.bios", bios_size
,
251 blk
, 65536, fl_sectors
, 1,
252 2, 0x0001, 0x22DA, 0x0000, 0x0000, 0x555, 0x2AA,
258 #ifdef DEBUG_BOARD_INIT
259 printf("Load BIOS from file\n");
261 bios
= g_new(MemoryRegion
, 1);
262 memory_region_init_ram(bios
, NULL
, "ef405ep.bios", BIOS_SIZE
,
265 if (bios_name
== NULL
)
266 bios_name
= BIOS_FILENAME
;
267 filename
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, bios_name
);
269 bios_size
= load_image(filename
, memory_region_get_ram_ptr(bios
));
271 if (bios_size
< 0 || bios_size
> BIOS_SIZE
) {
272 error_report("Could not load PowerPC BIOS '%s'", bios_name
);
275 bios_size
= (bios_size
+ 0xfff) & ~0xfff;
276 memory_region_add_subregion(sysmem
, (uint32_t)(-bios_size
), bios
);
277 } else if (!qtest_enabled() || kernel_filename
!= NULL
) {
278 error_report("Could not load PowerPC BIOS '%s'", bios_name
);
281 /* Avoid an uninitialized variable warning */
284 memory_region_set_readonly(bios
, true);
287 #ifdef DEBUG_BOARD_INIT
288 printf("%s: register FPGA\n", __func__
);
290 ref405ep_fpga_init(sysmem
, 0xF0300000);
292 #ifdef DEBUG_BOARD_INIT
293 printf("%s: register NVRAM\n", __func__
);
295 m48t59_init(NULL
, 0xF0000000, 0, 8192, 1968, 8);
297 linux_boot
= (kernel_filename
!= NULL
);
299 #ifdef DEBUG_BOARD_INIT
300 printf("%s: load kernel\n", __func__
);
302 memset(&bd
, 0, sizeof(bd
));
303 bd
.bi_memstart
= 0x00000000;
304 bd
.bi_memsize
= ram_size
;
305 bd
.bi_flashstart
= -bios_size
;
306 bd
.bi_flashsize
= -bios_size
;
307 bd
.bi_flashoffset
= 0;
308 bd
.bi_sramstart
= 0xFFF00000;
309 bd
.bi_sramsize
= sram_size
;
311 bd
.bi_intfreq
= 133333333;
312 bd
.bi_busfreq
= 33333333;
313 bd
.bi_baudrate
= 115200;
314 bd
.bi_s_version
[0] = 'Q';
315 bd
.bi_s_version
[1] = 'M';
316 bd
.bi_s_version
[2] = 'U';
317 bd
.bi_s_version
[3] = '\0';
318 bd
.bi_r_version
[0] = 'Q';
319 bd
.bi_r_version
[1] = 'E';
320 bd
.bi_r_version
[2] = 'M';
321 bd
.bi_r_version
[3] = 'U';
322 bd
.bi_r_version
[4] = '\0';
323 bd
.bi_procfreq
= 133333333;
324 bd
.bi_plb_busfreq
= 33333333;
325 bd
.bi_pci_busfreq
= 33333333;
326 bd
.bi_opbfreq
= 33333333;
327 bdloc
= ppc405_set_bootinfo(env
, &bd
, 0x00000001);
329 kernel_base
= KERNEL_LOAD_ADDR
;
330 /* now we can load the kernel */
331 kernel_size
= load_image_targphys(kernel_filename
, kernel_base
,
332 ram_size
- kernel_base
);
333 if (kernel_size
< 0) {
334 error_report("could not load kernel '%s'", kernel_filename
);
337 printf("Load kernel size %ld at " TARGET_FMT_lx
,
338 kernel_size
, kernel_base
);
340 if (initrd_filename
) {
341 initrd_base
= INITRD_LOAD_ADDR
;
342 initrd_size
= load_image_targphys(initrd_filename
, initrd_base
,
343 ram_size
- initrd_base
);
344 if (initrd_size
< 0) {
345 error_report("could not load initial ram disk '%s'",
353 env
->gpr
[4] = initrd_base
;
354 env
->gpr
[5] = initrd_size
;
355 if (kernel_cmdline
!= NULL
) {
356 len
= strlen(kernel_cmdline
);
357 bdloc
-= ((len
+ 255) & ~255);
358 cpu_physical_memory_write(bdloc
, kernel_cmdline
, len
+ 1);
360 env
->gpr
[7] = bdloc
+ len
;
365 env
->nip
= KERNEL_LOAD_ADDR
;
373 #ifdef DEBUG_BOARD_INIT
374 printf("bdloc " RAM_ADDR_FMT
"\n", bdloc
);
375 printf("%s: Done\n", __func__
);
379 static void ref405ep_class_init(ObjectClass
*oc
, void *data
)
381 MachineClass
*mc
= MACHINE_CLASS(oc
);
383 mc
->desc
= "ref405ep";
384 mc
->init
= ref405ep_init
;
387 static const TypeInfo ref405ep_type
= {
388 .name
= MACHINE_TYPE_NAME("ref405ep"),
389 .parent
= TYPE_MACHINE
,
390 .class_init
= ref405ep_class_init
,
393 /*****************************************************************************/
394 /* AMCC Taihu evaluation board */
395 /* - PowerPC 405EP processor
396 * - SDRAM 128 MB at 0x00000000
397 * - Boot flash 2 MB at 0xFFE00000
398 * - Application flash 32 MB at 0xFC000000
401 * - 1 USB 1.1 device 0x50000000
402 * - 1 LCD display 0x50100000
403 * - 1 CPLD 0x50100000
405 * - 1 I2C thermal sensor
407 * - bit-bang SPI port using GPIOs
408 * - 1 EBC interface connector 0 0x50200000
409 * - 1 cardbus controller + expansion slot.
410 * - 1 PCI expansion slot.
412 typedef struct taihu_cpld_t taihu_cpld_t
;
413 struct taihu_cpld_t
{
418 static uint64_t taihu_cpld_read(void *opaque
, hwaddr addr
, unsigned size
)
439 static void taihu_cpld_write(void *opaque
, hwaddr addr
,
440 uint64_t value
, unsigned size
)
457 static const MemoryRegionOps taihu_cpld_ops
= {
458 .read
= taihu_cpld_read
,
459 .write
= taihu_cpld_write
,
461 .min_access_size
= 1,
462 .max_access_size
= 1,
464 .endianness
= DEVICE_NATIVE_ENDIAN
,
467 static void taihu_cpld_reset (void *opaque
)
476 static void taihu_cpld_init(MemoryRegion
*sysmem
, uint32_t base
)
479 MemoryRegion
*cpld_memory
= g_new(MemoryRegion
, 1);
481 cpld
= g_malloc0(sizeof(taihu_cpld_t
));
482 memory_region_init_io(cpld_memory
, NULL
, &taihu_cpld_ops
, cpld
, "cpld", 0x100);
483 memory_region_add_subregion(sysmem
, base
, cpld_memory
);
484 qemu_register_reset(&taihu_cpld_reset
, cpld
);
487 static void taihu_405ep_init(MachineState
*machine
)
489 ram_addr_t ram_size
= machine
->ram_size
;
490 const char *kernel_filename
= machine
->kernel_filename
;
491 const char *initrd_filename
= machine
->initrd_filename
;
494 MemoryRegion
*sysmem
= get_system_memory();
496 MemoryRegion
*ram_memories
= g_malloc(2 * sizeof(*ram_memories
));
497 MemoryRegion
*ram
= g_malloc0(sizeof(*ram
));
498 hwaddr ram_bases
[2], ram_sizes
[2];
500 target_ulong kernel_base
, initrd_base
;
501 long kernel_size
, initrd_size
;
503 int fl_idx
, fl_sectors
;
507 if (!qtest_enabled()) {
508 warn_report("qemu-system-ppcemb is deprecated, "
509 "please use qemu-system-ppc instead.");
513 /* RAM is soldered to the board so the size cannot be changed */
514 ram_size
= 0x08000000;
515 memory_region_allocate_system_memory(ram
, NULL
, "taihu_405ep.ram",
519 ram_sizes
[0] = 0x04000000;
520 memory_region_init_alias(&ram_memories
[0], NULL
,
521 "taihu_405ep.ram-0", ram
, ram_bases
[0],
523 ram_bases
[1] = 0x04000000;
524 ram_sizes
[1] = 0x04000000;
525 memory_region_init_alias(&ram_memories
[1], NULL
,
526 "taihu_405ep.ram-1", ram
, ram_bases
[1],
528 #ifdef DEBUG_BOARD_INIT
529 printf("%s: register cpu\n", __func__
);
531 ppc405ep_init(sysmem
, ram_memories
, ram_bases
, ram_sizes
,
532 33333333, &pic
, kernel_filename
== NULL
? 0 : 1);
533 /* allocate and load BIOS */
534 #ifdef DEBUG_BOARD_INIT
535 printf("%s: register BIOS\n", __func__
);
538 #if defined(USE_FLASH_BIOS)
539 dinfo
= drive_get(IF_PFLASH
, 0, fl_idx
);
541 BlockBackend
*blk
= blk_by_legacy_dinfo(dinfo
);
543 bios_size
= blk_getlength(blk
);
544 /* XXX: should check that size is 2MB */
545 // bios_size = 2 * 1024 * 1024;
546 fl_sectors
= (bios_size
+ 65535) >> 16;
547 #ifdef DEBUG_BOARD_INIT
548 printf("Register parallel flash %d size %lx"
549 " at addr %lx '%s' %d\n",
550 fl_idx
, bios_size
, -bios_size
,
551 blk_name(blk
), fl_sectors
);
553 pflash_cfi02_register((uint32_t)(-bios_size
),
554 NULL
, "taihu_405ep.bios", bios_size
,
555 blk
, 65536, fl_sectors
, 1,
556 4, 0x0001, 0x22DA, 0x0000, 0x0000, 0x555, 0x2AA,
562 #ifdef DEBUG_BOARD_INIT
563 printf("Load BIOS from file\n");
565 if (bios_name
== NULL
)
566 bios_name
= BIOS_FILENAME
;
567 bios
= g_new(MemoryRegion
, 1);
568 memory_region_init_ram(bios
, NULL
, "taihu_405ep.bios", BIOS_SIZE
,
570 filename
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, bios_name
);
572 bios_size
= load_image(filename
, memory_region_get_ram_ptr(bios
));
574 if (bios_size
< 0 || bios_size
> BIOS_SIZE
) {
575 error_report("Could not load PowerPC BIOS '%s'", bios_name
);
578 bios_size
= (bios_size
+ 0xfff) & ~0xfff;
579 memory_region_add_subregion(sysmem
, (uint32_t)(-bios_size
), bios
);
580 } else if (!qtest_enabled()) {
581 error_report("Could not load PowerPC BIOS '%s'", bios_name
);
584 memory_region_set_readonly(bios
, true);
586 /* Register Linux flash */
587 dinfo
= drive_get(IF_PFLASH
, 0, fl_idx
);
589 BlockBackend
*blk
= blk_by_legacy_dinfo(dinfo
);
591 bios_size
= blk_getlength(blk
);
592 /* XXX: should check that size is 32MB */
593 bios_size
= 32 * 1024 * 1024;
594 fl_sectors
= (bios_size
+ 65535) >> 16;
595 #ifdef DEBUG_BOARD_INIT
596 printf("Register parallel flash %d size %lx"
597 " at addr " TARGET_FMT_lx
" '%s'\n",
598 fl_idx
, bios_size
, (target_ulong
)0xfc000000,
601 pflash_cfi02_register(0xfc000000, NULL
, "taihu_405ep.flash", bios_size
,
602 blk
, 65536, fl_sectors
, 1,
603 4, 0x0001, 0x22DA, 0x0000, 0x0000, 0x555, 0x2AA,
607 /* Register CLPD & LCD display */
608 #ifdef DEBUG_BOARD_INIT
609 printf("%s: register CPLD\n", __func__
);
611 taihu_cpld_init(sysmem
, 0x50100000);
613 linux_boot
= (kernel_filename
!= NULL
);
615 #ifdef DEBUG_BOARD_INIT
616 printf("%s: load kernel\n", __func__
);
618 kernel_base
= KERNEL_LOAD_ADDR
;
619 /* now we can load the kernel */
620 kernel_size
= load_image_targphys(kernel_filename
, kernel_base
,
621 ram_size
- kernel_base
);
622 if (kernel_size
< 0) {
623 error_report("could not load kernel '%s'", kernel_filename
);
627 if (initrd_filename
) {
628 initrd_base
= INITRD_LOAD_ADDR
;
629 initrd_size
= load_image_targphys(initrd_filename
, initrd_base
,
630 ram_size
- initrd_base
);
631 if (initrd_size
< 0) {
632 error_report("could not load initial ram disk '%s'",
646 #ifdef DEBUG_BOARD_INIT
647 printf("%s: Done\n", __func__
);
651 static void taihu_class_init(ObjectClass
*oc
, void *data
)
653 MachineClass
*mc
= MACHINE_CLASS(oc
);
656 mc
->init
= taihu_405ep_init
;
659 static const TypeInfo taihu_type
= {
660 .name
= MACHINE_TYPE_NAME("taihu"),
661 .parent
= TYPE_MACHINE
,
662 .class_init
= taihu_class_init
,
665 static void ppc405_machine_init(void)
667 type_register_static(&ref405ep_type
);
668 type_register_static(&taihu_type
);
671 type_init(ppc405_machine_init
)