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_new(MemoryRegion
, 2);
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 memory_region_allocate_system_memory(&ram_memories
[0], NULL
, "ef405ep.ram",
169 ram_sizes
[0] = 0x08000000;
170 memory_region_init(&ram_memories
[1], NULL
, "ef405ep.ram1", 0);
171 ram_bases
[1] = 0x00000000;
172 ram_sizes
[1] = 0x00000000;
173 ram_size
= 128 * MiB
;
174 #ifdef DEBUG_BOARD_INIT
175 printf("%s: register cpu\n", __func__
);
177 env
= ppc405ep_init(sysmem
, ram_memories
, ram_bases
, ram_sizes
,
178 33333333, &pic
, kernel_filename
== NULL
? 0 : 1);
180 sram_size
= 512 * KiB
;
181 memory_region_init_ram(sram
, NULL
, "ef405ep.sram", sram_size
,
183 memory_region_add_subregion(sysmem
, 0xFFF00000, sram
);
184 /* allocate and load BIOS */
185 #ifdef DEBUG_BOARD_INIT
186 printf("%s: register BIOS\n", __func__
);
189 #ifdef USE_FLASH_BIOS
190 dinfo
= drive_get(IF_PFLASH
, 0, fl_idx
);
192 BlockBackend
*blk
= blk_by_legacy_dinfo(dinfo
);
194 bios_size
= blk_getlength(blk
);
195 fl_sectors
= (bios_size
+ 65535) >> 16;
196 #ifdef DEBUG_BOARD_INIT
197 printf("Register parallel flash %d size %lx"
198 " at addr %lx '%s' %d\n",
199 fl_idx
, bios_size
, -bios_size
,
200 blk_name(blk
), fl_sectors
);
202 pflash_cfi02_register((uint32_t)(-bios_size
),
203 NULL
, "ef405ep.bios", bios_size
,
204 blk
, 65536, fl_sectors
, 1,
205 2, 0x0001, 0x22DA, 0x0000, 0x0000, 0x555, 0x2AA,
211 #ifdef DEBUG_BOARD_INIT
212 printf("Load BIOS from file\n");
214 bios
= g_new(MemoryRegion
, 1);
215 memory_region_init_ram(bios
, NULL
, "ef405ep.bios", BIOS_SIZE
,
218 if (bios_name
== NULL
)
219 bios_name
= BIOS_FILENAME
;
220 filename
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, bios_name
);
222 bios_size
= load_image_size(filename
,
223 memory_region_get_ram_ptr(bios
),
227 error_report("Could not load PowerPC BIOS '%s'", bios_name
);
230 bios_size
= (bios_size
+ 0xfff) & ~0xfff;
231 memory_region_add_subregion(sysmem
, (uint32_t)(-bios_size
), bios
);
232 } else if (!qtest_enabled() || kernel_filename
!= NULL
) {
233 error_report("Could not load PowerPC BIOS '%s'", bios_name
);
236 /* Avoid an uninitialized variable warning */
239 memory_region_set_readonly(bios
, true);
242 #ifdef DEBUG_BOARD_INIT
243 printf("%s: register FPGA\n", __func__
);
245 ref405ep_fpga_init(sysmem
, 0xF0300000);
247 #ifdef DEBUG_BOARD_INIT
248 printf("%s: register NVRAM\n", __func__
);
250 m48t59_init(NULL
, 0xF0000000, 0, 8192, 1968, 8);
252 linux_boot
= (kernel_filename
!= NULL
);
254 #ifdef DEBUG_BOARD_INIT
255 printf("%s: load kernel\n", __func__
);
257 memset(&bd
, 0, sizeof(bd
));
258 bd
.bi_memstart
= 0x00000000;
259 bd
.bi_memsize
= ram_size
;
260 bd
.bi_flashstart
= -bios_size
;
261 bd
.bi_flashsize
= -bios_size
;
262 bd
.bi_flashoffset
= 0;
263 bd
.bi_sramstart
= 0xFFF00000;
264 bd
.bi_sramsize
= sram_size
;
266 bd
.bi_intfreq
= 133333333;
267 bd
.bi_busfreq
= 33333333;
268 bd
.bi_baudrate
= 115200;
269 bd
.bi_s_version
[0] = 'Q';
270 bd
.bi_s_version
[1] = 'M';
271 bd
.bi_s_version
[2] = 'U';
272 bd
.bi_s_version
[3] = '\0';
273 bd
.bi_r_version
[0] = 'Q';
274 bd
.bi_r_version
[1] = 'E';
275 bd
.bi_r_version
[2] = 'M';
276 bd
.bi_r_version
[3] = 'U';
277 bd
.bi_r_version
[4] = '\0';
278 bd
.bi_procfreq
= 133333333;
279 bd
.bi_plb_busfreq
= 33333333;
280 bd
.bi_pci_busfreq
= 33333333;
281 bd
.bi_opbfreq
= 33333333;
282 bdloc
= ppc405_set_bootinfo(env
, &bd
, 0x00000001);
284 kernel_base
= KERNEL_LOAD_ADDR
;
285 /* now we can load the kernel */
286 kernel_size
= load_image_targphys(kernel_filename
, kernel_base
,
287 ram_size
- kernel_base
);
288 if (kernel_size
< 0) {
289 error_report("could not load kernel '%s'", kernel_filename
);
292 printf("Load kernel size %ld at " TARGET_FMT_lx
,
293 kernel_size
, kernel_base
);
295 if (initrd_filename
) {
296 initrd_base
= INITRD_LOAD_ADDR
;
297 initrd_size
= load_image_targphys(initrd_filename
, initrd_base
,
298 ram_size
- initrd_base
);
299 if (initrd_size
< 0) {
300 error_report("could not load initial ram disk '%s'",
308 env
->gpr
[4] = initrd_base
;
309 env
->gpr
[5] = initrd_size
;
310 if (kernel_cmdline
!= NULL
) {
311 len
= strlen(kernel_cmdline
);
312 bdloc
-= ((len
+ 255) & ~255);
313 cpu_physical_memory_write(bdloc
, kernel_cmdline
, len
+ 1);
315 env
->gpr
[7] = bdloc
+ len
;
320 env
->nip
= KERNEL_LOAD_ADDR
;
328 #ifdef DEBUG_BOARD_INIT
329 printf("bdloc " RAM_ADDR_FMT
"\n", bdloc
);
330 printf("%s: Done\n", __func__
);
334 static void ref405ep_class_init(ObjectClass
*oc
, void *data
)
336 MachineClass
*mc
= MACHINE_CLASS(oc
);
338 mc
->desc
= "ref405ep";
339 mc
->init
= ref405ep_init
;
342 static const TypeInfo ref405ep_type
= {
343 .name
= MACHINE_TYPE_NAME("ref405ep"),
344 .parent
= TYPE_MACHINE
,
345 .class_init
= ref405ep_class_init
,
348 /*****************************************************************************/
349 /* AMCC Taihu evaluation board */
350 /* - PowerPC 405EP processor
351 * - SDRAM 128 MB at 0x00000000
352 * - Boot flash 2 MB at 0xFFE00000
353 * - Application flash 32 MB at 0xFC000000
356 * - 1 USB 1.1 device 0x50000000
357 * - 1 LCD display 0x50100000
358 * - 1 CPLD 0x50100000
360 * - 1 I2C thermal sensor
362 * - bit-bang SPI port using GPIOs
363 * - 1 EBC interface connector 0 0x50200000
364 * - 1 cardbus controller + expansion slot.
365 * - 1 PCI expansion slot.
367 typedef struct taihu_cpld_t taihu_cpld_t
;
368 struct taihu_cpld_t
{
373 static uint64_t taihu_cpld_read(void *opaque
, hwaddr addr
, unsigned size
)
394 static void taihu_cpld_write(void *opaque
, hwaddr addr
,
395 uint64_t value
, unsigned size
)
412 static const MemoryRegionOps taihu_cpld_ops
= {
413 .read
= taihu_cpld_read
,
414 .write
= taihu_cpld_write
,
416 .min_access_size
= 1,
417 .max_access_size
= 1,
419 .endianness
= DEVICE_NATIVE_ENDIAN
,
422 static void taihu_cpld_reset (void *opaque
)
431 static void taihu_cpld_init(MemoryRegion
*sysmem
, uint32_t base
)
434 MemoryRegion
*cpld_memory
= g_new(MemoryRegion
, 1);
436 cpld
= g_malloc0(sizeof(taihu_cpld_t
));
437 memory_region_init_io(cpld_memory
, NULL
, &taihu_cpld_ops
, cpld
, "cpld", 0x100);
438 memory_region_add_subregion(sysmem
, base
, cpld_memory
);
439 qemu_register_reset(&taihu_cpld_reset
, cpld
);
442 static void taihu_405ep_init(MachineState
*machine
)
444 ram_addr_t ram_size
= machine
->ram_size
;
445 const char *kernel_filename
= machine
->kernel_filename
;
446 const char *initrd_filename
= machine
->initrd_filename
;
449 MemoryRegion
*sysmem
= get_system_memory();
451 MemoryRegion
*ram_memories
= g_new(MemoryRegion
, 2);
452 MemoryRegion
*ram
= g_malloc0(sizeof(*ram
));
453 hwaddr ram_bases
[2], ram_sizes
[2];
455 target_ulong kernel_base
, initrd_base
;
456 long kernel_size
, initrd_size
;
458 int fl_idx
, fl_sectors
;
461 /* RAM is soldered to the board so the size cannot be changed */
462 ram_size
= 0x08000000;
463 memory_region_allocate_system_memory(ram
, NULL
, "taihu_405ep.ram",
467 ram_sizes
[0] = 0x04000000;
468 memory_region_init_alias(&ram_memories
[0], NULL
,
469 "taihu_405ep.ram-0", 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", ram
, ram_bases
[1],
476 #ifdef DEBUG_BOARD_INIT
477 printf("%s: register cpu\n", __func__
);
479 ppc405ep_init(sysmem
, ram_memories
, ram_bases
, ram_sizes
,
480 33333333, &pic
, kernel_filename
== NULL
? 0 : 1);
481 /* allocate and load BIOS */
482 #ifdef DEBUG_BOARD_INIT
483 printf("%s: register BIOS\n", __func__
);
486 #if defined(USE_FLASH_BIOS)
487 dinfo
= drive_get(IF_PFLASH
, 0, fl_idx
);
489 BlockBackend
*blk
= blk_by_legacy_dinfo(dinfo
);
491 bios_size
= blk_getlength(blk
);
492 /* XXX: should check that size is 2MB */
493 // bios_size = 2 * 1024 * 1024;
494 fl_sectors
= (bios_size
+ 65535) >> 16;
495 #ifdef DEBUG_BOARD_INIT
496 printf("Register parallel flash %d size %lx"
497 " at addr %lx '%s' %d\n",
498 fl_idx
, bios_size
, -bios_size
,
499 blk_name(blk
), fl_sectors
);
501 pflash_cfi02_register((uint32_t)(-bios_size
),
502 NULL
, "taihu_405ep.bios", bios_size
,
503 blk
, 65536, fl_sectors
, 1,
504 4, 0x0001, 0x22DA, 0x0000, 0x0000, 0x555, 0x2AA,
510 #ifdef DEBUG_BOARD_INIT
511 printf("Load BIOS from file\n");
513 if (bios_name
== NULL
)
514 bios_name
= BIOS_FILENAME
;
515 bios
= g_new(MemoryRegion
, 1);
516 memory_region_init_ram(bios
, NULL
, "taihu_405ep.bios", BIOS_SIZE
,
518 filename
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, bios_name
);
520 bios_size
= load_image_size(filename
,
521 memory_region_get_ram_ptr(bios
),
525 error_report("Could not load PowerPC BIOS '%s'", bios_name
);
528 bios_size
= (bios_size
+ 0xfff) & ~0xfff;
529 memory_region_add_subregion(sysmem
, (uint32_t)(-bios_size
), bios
);
530 } else if (!qtest_enabled()) {
531 error_report("Could not load PowerPC BIOS '%s'", bios_name
);
534 memory_region_set_readonly(bios
, true);
536 /* Register Linux flash */
537 dinfo
= drive_get(IF_PFLASH
, 0, fl_idx
);
539 BlockBackend
*blk
= blk_by_legacy_dinfo(dinfo
);
541 bios_size
= blk_getlength(blk
);
542 /* XXX: should check that size is 32MB */
543 bios_size
= 32 * MiB
;
544 fl_sectors
= (bios_size
+ 65535) >> 16;
545 #ifdef DEBUG_BOARD_INIT
546 printf("Register parallel flash %d size %lx"
547 " at addr " TARGET_FMT_lx
" '%s'\n",
548 fl_idx
, bios_size
, (target_ulong
)0xfc000000,
551 pflash_cfi02_register(0xfc000000, NULL
, "taihu_405ep.flash", bios_size
,
552 blk
, 65536, fl_sectors
, 1,
553 4, 0x0001, 0x22DA, 0x0000, 0x0000, 0x555, 0x2AA,
557 /* Register CLPD & LCD display */
558 #ifdef DEBUG_BOARD_INIT
559 printf("%s: register CPLD\n", __func__
);
561 taihu_cpld_init(sysmem
, 0x50100000);
563 linux_boot
= (kernel_filename
!= NULL
);
565 #ifdef DEBUG_BOARD_INIT
566 printf("%s: load kernel\n", __func__
);
568 kernel_base
= KERNEL_LOAD_ADDR
;
569 /* now we can load the kernel */
570 kernel_size
= load_image_targphys(kernel_filename
, kernel_base
,
571 ram_size
- kernel_base
);
572 if (kernel_size
< 0) {
573 error_report("could not load kernel '%s'", kernel_filename
);
577 if (initrd_filename
) {
578 initrd_base
= INITRD_LOAD_ADDR
;
579 initrd_size
= load_image_targphys(initrd_filename
, initrd_base
,
580 ram_size
- initrd_base
);
581 if (initrd_size
< 0) {
582 error_report("could not load initial ram disk '%s'",
596 #ifdef DEBUG_BOARD_INIT
597 printf("%s: Done\n", __func__
);
601 static void taihu_class_init(ObjectClass
*oc
, void *data
)
603 MachineClass
*mc
= MACHINE_CLASS(oc
);
606 mc
->init
= taihu_405ep_init
;
609 static const TypeInfo taihu_type
= {
610 .name
= MACHINE_TYPE_NAME("taihu"),
611 .parent
= TYPE_MACHINE
,
612 .class_init
= taihu_class_init
,
615 static void ppc405_machine_init(void)
617 type_register_static(&ref405ep_type
);
618 type_register_static(&taihu_type
);
621 type_init(ppc405_machine_init
)