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
29 #include "sysemu/sysemu.h"
30 #include "block/block.h"
34 #include "sysemu/blockdev.h"
35 #include "exec/address-spaces.h"
37 #define BIOS_FILENAME "ppc405_rom.bin"
38 #define BIOS_SIZE (2048 * 1024)
40 #define KERNEL_LOAD_ADDR 0x00000000
41 #define INITRD_LOAD_ADDR 0x01800000
43 #define USE_FLASH_BIOS
45 #define DEBUG_BOARD_INIT
47 /*****************************************************************************/
48 /* PPC405EP reference board (IBM) */
49 /* Standalone board with:
51 * - SDRAM (0x00000000)
52 * - Flash (0xFFF80000)
54 * - NVRAM (0xF0000000)
57 typedef struct ref405ep_fpga_t ref405ep_fpga_t
;
58 struct ref405ep_fpga_t
{
63 static uint32_t ref405ep_fpga_readb (void *opaque
, hwaddr addr
)
65 ref405ep_fpga_t
*fpga
;
84 static void ref405ep_fpga_writeb (void *opaque
,
85 hwaddr addr
, uint32_t value
)
87 ref405ep_fpga_t
*fpga
;
102 static uint32_t ref405ep_fpga_readw (void *opaque
, hwaddr addr
)
106 ret
= ref405ep_fpga_readb(opaque
, addr
) << 8;
107 ret
|= ref405ep_fpga_readb(opaque
, addr
+ 1);
112 static void ref405ep_fpga_writew (void *opaque
,
113 hwaddr addr
, uint32_t value
)
115 ref405ep_fpga_writeb(opaque
, addr
, (value
>> 8) & 0xFF);
116 ref405ep_fpga_writeb(opaque
, addr
+ 1, value
& 0xFF);
119 static uint32_t ref405ep_fpga_readl (void *opaque
, hwaddr addr
)
123 ret
= ref405ep_fpga_readb(opaque
, addr
) << 24;
124 ret
|= ref405ep_fpga_readb(opaque
, addr
+ 1) << 16;
125 ret
|= ref405ep_fpga_readb(opaque
, addr
+ 2) << 8;
126 ret
|= ref405ep_fpga_readb(opaque
, addr
+ 3);
131 static void ref405ep_fpga_writel (void *opaque
,
132 hwaddr addr
, uint32_t value
)
134 ref405ep_fpga_writeb(opaque
, addr
, (value
>> 24) & 0xFF);
135 ref405ep_fpga_writeb(opaque
, addr
+ 1, (value
>> 16) & 0xFF);
136 ref405ep_fpga_writeb(opaque
, addr
+ 2, (value
>> 8) & 0xFF);
137 ref405ep_fpga_writeb(opaque
, addr
+ 3, value
& 0xFF);
140 static const MemoryRegionOps ref405ep_fpga_ops
= {
143 ref405ep_fpga_readb
, ref405ep_fpga_readw
, ref405ep_fpga_readl
,
146 ref405ep_fpga_writeb
, ref405ep_fpga_writew
, ref405ep_fpga_writel
,
149 .endianness
= DEVICE_NATIVE_ENDIAN
,
152 static void ref405ep_fpga_reset (void *opaque
)
154 ref405ep_fpga_t
*fpga
;
161 static void ref405ep_fpga_init(MemoryRegion
*sysmem
, uint32_t base
)
163 ref405ep_fpga_t
*fpga
;
164 MemoryRegion
*fpga_memory
= g_new(MemoryRegion
, 1);
166 fpga
= g_malloc0(sizeof(ref405ep_fpga_t
));
167 memory_region_init_io(fpga_memory
, &ref405ep_fpga_ops
, fpga
,
169 memory_region_add_subregion(sysmem
, base
, fpga_memory
);
170 qemu_register_reset(&ref405ep_fpga_reset
, fpga
);
173 static void ref405ep_init(QEMUMachineInitArgs
*args
)
175 ram_addr_t ram_size
= args
->ram_size
;
176 const char *kernel_filename
= args
->kernel_filename
;
177 const char *kernel_cmdline
= args
->kernel_cmdline
;
178 const char *initrd_filename
= args
->initrd_filename
;
184 MemoryRegion
*sram
= g_new(MemoryRegion
, 1);
186 MemoryRegion
*ram_memories
= g_malloc(2 * sizeof(*ram_memories
));
187 hwaddr ram_bases
[2], ram_sizes
[2];
188 target_ulong sram_size
;
191 //static int phy_addr = 1;
192 target_ulong kernel_base
, initrd_base
;
193 long kernel_size
, initrd_size
;
195 int fl_idx
, fl_sectors
, len
;
197 MemoryRegion
*sysmem
= get_system_memory();
200 memory_region_init_ram(&ram_memories
[0], "ef405ep.ram", 0x08000000);
201 vmstate_register_ram_global(&ram_memories
[0]);
203 ram_sizes
[0] = 0x08000000;
204 memory_region_init(&ram_memories
[1], "ef405ep.ram1", 0);
205 ram_bases
[1] = 0x00000000;
206 ram_sizes
[1] = 0x00000000;
207 ram_size
= 128 * 1024 * 1024;
208 #ifdef DEBUG_BOARD_INIT
209 printf("%s: register cpu\n", __func__
);
211 env
= ppc405ep_init(sysmem
, ram_memories
, ram_bases
, ram_sizes
,
212 33333333, &pic
, kernel_filename
== NULL
? 0 : 1);
214 sram_size
= 512 * 1024;
215 memory_region_init_ram(sram
, "ef405ep.sram", sram_size
);
216 vmstate_register_ram_global(sram
);
217 memory_region_add_subregion(sysmem
, 0xFFF00000, sram
);
218 /* allocate and load BIOS */
219 #ifdef DEBUG_BOARD_INIT
220 printf("%s: register BIOS\n", __func__
);
223 #ifdef USE_FLASH_BIOS
224 dinfo
= drive_get(IF_PFLASH
, 0, fl_idx
);
226 bios_size
= bdrv_getlength(dinfo
->bdrv
);
227 fl_sectors
= (bios_size
+ 65535) >> 16;
228 #ifdef DEBUG_BOARD_INIT
229 printf("Register parallel flash %d size %lx"
230 " at addr %lx '%s' %d\n",
231 fl_idx
, bios_size
, -bios_size
,
232 bdrv_get_device_name(dinfo
->bdrv
), fl_sectors
);
234 pflash_cfi02_register((uint32_t)(-bios_size
),
235 NULL
, "ef405ep.bios", bios_size
,
236 dinfo
->bdrv
, 65536, fl_sectors
, 1,
237 2, 0x0001, 0x22DA, 0x0000, 0x0000, 0x555, 0x2AA,
243 #ifdef DEBUG_BOARD_INIT
244 printf("Load BIOS from file\n");
246 bios
= g_new(MemoryRegion
, 1);
247 memory_region_init_ram(bios
, "ef405ep.bios", BIOS_SIZE
);
248 vmstate_register_ram_global(bios
);
249 if (bios_name
== NULL
)
250 bios_name
= BIOS_FILENAME
;
251 filename
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, bios_name
);
253 bios_size
= load_image(filename
, memory_region_get_ram_ptr(bios
));
258 if (bios_size
< 0 || bios_size
> BIOS_SIZE
) {
259 fprintf(stderr
, "qemu: could not load PowerPC bios '%s'\n",
263 bios_size
= (bios_size
+ 0xfff) & ~0xfff;
264 memory_region_set_readonly(bios
, true);
265 memory_region_add_subregion(sysmem
, (uint32_t)(-bios_size
), bios
);
268 #ifdef DEBUG_BOARD_INIT
269 printf("%s: register FPGA\n", __func__
);
271 ref405ep_fpga_init(sysmem
, 0xF0300000);
273 #ifdef DEBUG_BOARD_INIT
274 printf("%s: register NVRAM\n", __func__
);
276 m48t59_init(NULL
, 0xF0000000, 0, 8192, 8);
278 linux_boot
= (kernel_filename
!= NULL
);
280 #ifdef DEBUG_BOARD_INIT
281 printf("%s: load kernel\n", __func__
);
283 memset(&bd
, 0, sizeof(bd
));
284 bd
.bi_memstart
= 0x00000000;
285 bd
.bi_memsize
= ram_size
;
286 bd
.bi_flashstart
= -bios_size
;
287 bd
.bi_flashsize
= -bios_size
;
288 bd
.bi_flashoffset
= 0;
289 bd
.bi_sramstart
= 0xFFF00000;
290 bd
.bi_sramsize
= sram_size
;
292 bd
.bi_intfreq
= 133333333;
293 bd
.bi_busfreq
= 33333333;
294 bd
.bi_baudrate
= 115200;
295 bd
.bi_s_version
[0] = 'Q';
296 bd
.bi_s_version
[1] = 'M';
297 bd
.bi_s_version
[2] = 'U';
298 bd
.bi_s_version
[3] = '\0';
299 bd
.bi_r_version
[0] = 'Q';
300 bd
.bi_r_version
[1] = 'E';
301 bd
.bi_r_version
[2] = 'M';
302 bd
.bi_r_version
[3] = 'U';
303 bd
.bi_r_version
[4] = '\0';
304 bd
.bi_procfreq
= 133333333;
305 bd
.bi_plb_busfreq
= 33333333;
306 bd
.bi_pci_busfreq
= 33333333;
307 bd
.bi_opbfreq
= 33333333;
308 bdloc
= ppc405_set_bootinfo(env
, &bd
, 0x00000001);
310 kernel_base
= KERNEL_LOAD_ADDR
;
311 /* now we can load the kernel */
312 kernel_size
= load_image_targphys(kernel_filename
, kernel_base
,
313 ram_size
- kernel_base
);
314 if (kernel_size
< 0) {
315 fprintf(stderr
, "qemu: could not load kernel '%s'\n",
319 printf("Load kernel size %ld at " TARGET_FMT_lx
,
320 kernel_size
, kernel_base
);
322 if (initrd_filename
) {
323 initrd_base
= INITRD_LOAD_ADDR
;
324 initrd_size
= load_image_targphys(initrd_filename
, initrd_base
,
325 ram_size
- initrd_base
);
326 if (initrd_size
< 0) {
327 fprintf(stderr
, "qemu: could not load initial ram disk '%s'\n",
335 env
->gpr
[4] = initrd_base
;
336 env
->gpr
[5] = initrd_size
;
337 if (kernel_cmdline
!= NULL
) {
338 len
= strlen(kernel_cmdline
);
339 bdloc
-= ((len
+ 255) & ~255);
340 cpu_physical_memory_write(bdloc
, (void *)kernel_cmdline
, len
+ 1);
342 env
->gpr
[7] = bdloc
+ len
;
347 env
->nip
= KERNEL_LOAD_ADDR
;
355 #ifdef DEBUG_BOARD_INIT
356 printf("%s: Done\n", __func__
);
358 printf("bdloc " RAM_ADDR_FMT
"\n", bdloc
);
361 static QEMUMachine ref405ep_machine
= {
364 .init
= ref405ep_init
,
367 /*****************************************************************************/
368 /* AMCC Taihu evaluation board */
369 /* - PowerPC 405EP processor
370 * - SDRAM 128 MB at 0x00000000
371 * - Boot flash 2 MB at 0xFFE00000
372 * - Application flash 32 MB at 0xFC000000
375 * - 1 USB 1.1 device 0x50000000
376 * - 1 LCD display 0x50100000
377 * - 1 CPLD 0x50100000
379 * - 1 I2C thermal sensor
381 * - bit-bang SPI port using GPIOs
382 * - 1 EBC interface connector 0 0x50200000
383 * - 1 cardbus controller + expansion slot.
384 * - 1 PCI expansion slot.
386 typedef struct taihu_cpld_t taihu_cpld_t
;
387 struct taihu_cpld_t
{
392 static uint32_t taihu_cpld_readb (void *opaque
, hwaddr addr
)
413 static void taihu_cpld_writeb (void *opaque
,
414 hwaddr addr
, uint32_t value
)
431 static uint32_t taihu_cpld_readw (void *opaque
, hwaddr addr
)
435 ret
= taihu_cpld_readb(opaque
, addr
) << 8;
436 ret
|= taihu_cpld_readb(opaque
, addr
+ 1);
441 static void taihu_cpld_writew (void *opaque
,
442 hwaddr addr
, uint32_t value
)
444 taihu_cpld_writeb(opaque
, addr
, (value
>> 8) & 0xFF);
445 taihu_cpld_writeb(opaque
, addr
+ 1, value
& 0xFF);
448 static uint32_t taihu_cpld_readl (void *opaque
, hwaddr addr
)
452 ret
= taihu_cpld_readb(opaque
, addr
) << 24;
453 ret
|= taihu_cpld_readb(opaque
, addr
+ 1) << 16;
454 ret
|= taihu_cpld_readb(opaque
, addr
+ 2) << 8;
455 ret
|= taihu_cpld_readb(opaque
, addr
+ 3);
460 static void taihu_cpld_writel (void *opaque
,
461 hwaddr addr
, uint32_t value
)
463 taihu_cpld_writel(opaque
, addr
, (value
>> 24) & 0xFF);
464 taihu_cpld_writel(opaque
, addr
+ 1, (value
>> 16) & 0xFF);
465 taihu_cpld_writel(opaque
, addr
+ 2, (value
>> 8) & 0xFF);
466 taihu_cpld_writeb(opaque
, addr
+ 3, value
& 0xFF);
469 static const MemoryRegionOps taihu_cpld_ops
= {
471 .read
= { taihu_cpld_readb
, taihu_cpld_readw
, taihu_cpld_readl
, },
472 .write
= { taihu_cpld_writeb
, taihu_cpld_writew
, taihu_cpld_writel
, },
474 .endianness
= DEVICE_NATIVE_ENDIAN
,
477 static void taihu_cpld_reset (void *opaque
)
486 static void taihu_cpld_init(MemoryRegion
*sysmem
, uint32_t base
)
489 MemoryRegion
*cpld_memory
= g_new(MemoryRegion
, 1);
491 cpld
= g_malloc0(sizeof(taihu_cpld_t
));
492 memory_region_init_io(cpld_memory
, &taihu_cpld_ops
, cpld
, "cpld", 0x100);
493 memory_region_add_subregion(sysmem
, base
, cpld_memory
);
494 qemu_register_reset(&taihu_cpld_reset
, cpld
);
497 static void taihu_405ep_init(QEMUMachineInitArgs
*args
)
499 ram_addr_t ram_size
= args
->ram_size
;
500 const char *kernel_filename
= args
->kernel_filename
;
501 const char *initrd_filename
= args
->initrd_filename
;
504 MemoryRegion
*sysmem
= get_system_memory();
506 MemoryRegion
*ram_memories
= g_malloc(2 * sizeof(*ram_memories
));
507 hwaddr ram_bases
[2], ram_sizes
[2];
509 target_ulong kernel_base
, initrd_base
;
510 long kernel_size
, initrd_size
;
512 int fl_idx
, fl_sectors
;
515 /* RAM is soldered to the board so the size cannot be changed */
516 memory_region_init_ram(&ram_memories
[0],
517 "taihu_405ep.ram-0", 0x04000000);
518 vmstate_register_ram_global(&ram_memories
[0]);
520 ram_sizes
[0] = 0x04000000;
521 memory_region_init_ram(&ram_memories
[1],
522 "taihu_405ep.ram-1", 0x04000000);
523 vmstate_register_ram_global(&ram_memories
[1]);
524 ram_bases
[1] = 0x04000000;
525 ram_sizes
[1] = 0x04000000;
526 ram_size
= 0x08000000;
527 #ifdef DEBUG_BOARD_INIT
528 printf("%s: register cpu\n", __func__
);
530 ppc405ep_init(sysmem
, ram_memories
, ram_bases
, ram_sizes
,
531 33333333, &pic
, kernel_filename
== NULL
? 0 : 1);
532 /* allocate and load BIOS */
533 #ifdef DEBUG_BOARD_INIT
534 printf("%s: register BIOS\n", __func__
);
537 #if defined(USE_FLASH_BIOS)
538 dinfo
= drive_get(IF_PFLASH
, 0, fl_idx
);
540 bios_size
= bdrv_getlength(dinfo
->bdrv
);
541 /* XXX: should check that size is 2MB */
542 // bios_size = 2 * 1024 * 1024;
543 fl_sectors
= (bios_size
+ 65535) >> 16;
544 #ifdef DEBUG_BOARD_INIT
545 printf("Register parallel flash %d size %lx"
546 " at addr %lx '%s' %d\n",
547 fl_idx
, bios_size
, -bios_size
,
548 bdrv_get_device_name(dinfo
->bdrv
), fl_sectors
);
550 pflash_cfi02_register((uint32_t)(-bios_size
),
551 NULL
, "taihu_405ep.bios", bios_size
,
552 dinfo
->bdrv
, 65536, fl_sectors
, 1,
553 4, 0x0001, 0x22DA, 0x0000, 0x0000, 0x555, 0x2AA,
559 #ifdef DEBUG_BOARD_INIT
560 printf("Load BIOS from file\n");
562 if (bios_name
== NULL
)
563 bios_name
= BIOS_FILENAME
;
564 bios
= g_new(MemoryRegion
, 1);
565 memory_region_init_ram(bios
, "taihu_405ep.bios", BIOS_SIZE
);
566 vmstate_register_ram_global(bios
);
567 filename
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, bios_name
);
569 bios_size
= load_image(filename
, memory_region_get_ram_ptr(bios
));
574 if (bios_size
< 0 || bios_size
> BIOS_SIZE
) {
575 fprintf(stderr
, "qemu: could not load PowerPC bios '%s'\n",
579 bios_size
= (bios_size
+ 0xfff) & ~0xfff;
580 memory_region_set_readonly(bios
, true);
581 memory_region_add_subregion(sysmem
, (uint32_t)(-bios_size
), bios
);
583 /* Register Linux flash */
584 dinfo
= drive_get(IF_PFLASH
, 0, fl_idx
);
586 bios_size
= bdrv_getlength(dinfo
->bdrv
);
587 /* XXX: should check that size is 32MB */
588 bios_size
= 32 * 1024 * 1024;
589 fl_sectors
= (bios_size
+ 65535) >> 16;
590 #ifdef DEBUG_BOARD_INIT
591 printf("Register parallel flash %d size %lx"
592 " at addr " TARGET_FMT_lx
" '%s'\n",
593 fl_idx
, bios_size
, (target_ulong
)0xfc000000,
594 bdrv_get_device_name(dinfo
->bdrv
));
596 pflash_cfi02_register(0xfc000000, NULL
, "taihu_405ep.flash", bios_size
,
597 dinfo
->bdrv
, 65536, fl_sectors
, 1,
598 4, 0x0001, 0x22DA, 0x0000, 0x0000, 0x555, 0x2AA,
602 /* Register CLPD & LCD display */
603 #ifdef DEBUG_BOARD_INIT
604 printf("%s: register CPLD\n", __func__
);
606 taihu_cpld_init(sysmem
, 0x50100000);
608 linux_boot
= (kernel_filename
!= NULL
);
610 #ifdef DEBUG_BOARD_INIT
611 printf("%s: load kernel\n", __func__
);
613 kernel_base
= KERNEL_LOAD_ADDR
;
614 /* now we can load the kernel */
615 kernel_size
= load_image_targphys(kernel_filename
, kernel_base
,
616 ram_size
- kernel_base
);
617 if (kernel_size
< 0) {
618 fprintf(stderr
, "qemu: could not load kernel '%s'\n",
623 if (initrd_filename
) {
624 initrd_base
= INITRD_LOAD_ADDR
;
625 initrd_size
= load_image_targphys(initrd_filename
, initrd_base
,
626 ram_size
- initrd_base
);
627 if (initrd_size
< 0) {
629 "qemu: could not load initial ram disk '%s'\n",
643 #ifdef DEBUG_BOARD_INIT
644 printf("%s: Done\n", __func__
);
648 static QEMUMachine taihu_machine
= {
651 .init
= taihu_405ep_init
,
654 static void ppc405_machine_init(void)
656 qemu_register_machine(&ref405ep_machine
);
657 qemu_register_machine(&taihu_machine
);
660 machine_init(ppc405_machine_init
);