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
35 #include "exec-memory.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
, target_phys_addr_t addr
)
65 ref405ep_fpga_t
*fpga
;
84 static void ref405ep_fpga_writeb (void *opaque
,
85 target_phys_addr_t addr
, uint32_t value
)
87 ref405ep_fpga_t
*fpga
;
102 static uint32_t ref405ep_fpga_readw (void *opaque
, target_phys_addr_t 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 target_phys_addr_t 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
, target_phys_addr_t 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 target_phys_addr_t 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 CPUReadMemoryFunc
* const ref405ep_fpga_read
[] = {
141 &ref405ep_fpga_readb
,
142 &ref405ep_fpga_readw
,
143 &ref405ep_fpga_readl
,
146 static CPUWriteMemoryFunc
* const ref405ep_fpga_write
[] = {
147 &ref405ep_fpga_writeb
,
148 &ref405ep_fpga_writew
,
149 &ref405ep_fpga_writel
,
152 static void ref405ep_fpga_reset (void *opaque
)
154 ref405ep_fpga_t
*fpga
;
161 static void ref405ep_fpga_init (uint32_t base
)
163 ref405ep_fpga_t
*fpga
;
166 fpga
= g_malloc0(sizeof(ref405ep_fpga_t
));
167 fpga_memory
= cpu_register_io_memory(ref405ep_fpga_read
,
168 ref405ep_fpga_write
, fpga
,
169 DEVICE_NATIVE_ENDIAN
);
170 cpu_register_physical_memory(base
, 0x00000100, fpga_memory
);
171 qemu_register_reset(&ref405ep_fpga_reset
, fpga
);
174 static void ref405ep_init (ram_addr_t ram_size
,
175 const char *boot_device
,
176 const char *kernel_filename
,
177 const char *kernel_cmdline
,
178 const char *initrd_filename
,
179 const char *cpu_model
)
186 ram_addr_t sram_offset
, bdloc
;
187 MemoryRegion
*ram_memories
= g_malloc(2 * sizeof(*ram_memories
));
188 target_phys_addr_t ram_bases
[2], ram_sizes
[2];
189 target_ulong sram_size
;
192 //static int phy_addr = 1;
193 target_ulong kernel_base
, initrd_base
;
194 long kernel_size
, initrd_size
;
196 int fl_idx
, fl_sectors
, len
;
200 memory_region_init_ram(&ram_memories
[0], NULL
, "ef405ep.ram", 0x08000000);
202 ram_sizes
[0] = 0x08000000;
203 memory_region_init(&ram_memories
[1], "ef405ep.ram1", 0);
204 ram_bases
[1] = 0x00000000;
205 ram_sizes
[1] = 0x00000000;
206 ram_size
= 128 * 1024 * 1024;
207 #ifdef DEBUG_BOARD_INIT
208 printf("%s: register cpu\n", __func__
);
210 env
= ppc405ep_init(ram_memories
, ram_bases
, ram_sizes
, 33333333, &pic
,
211 kernel_filename
== NULL
? 0 : 1);
213 sram_size
= 512 * 1024;
214 sram_offset
= qemu_ram_alloc(NULL
, "ef405ep.sram", sram_size
);
215 #ifdef DEBUG_BOARD_INIT
216 printf("%s: register SRAM at offset %08lx\n", __func__
, sram_offset
);
218 cpu_register_physical_memory(0xFFF00000, sram_size
,
219 sram_offset
| IO_MEM_RAM
);
220 /* allocate and load BIOS */
221 #ifdef DEBUG_BOARD_INIT
222 printf("%s: register BIOS\n", __func__
);
225 #ifdef USE_FLASH_BIOS
226 dinfo
= drive_get(IF_PFLASH
, 0, fl_idx
);
228 bios_size
= bdrv_getlength(dinfo
->bdrv
);
229 fl_sectors
= (bios_size
+ 65535) >> 16;
230 #ifdef DEBUG_BOARD_INIT
231 printf("Register parallel flash %d size %lx"
232 " at addr %lx '%s' %d\n",
233 fl_idx
, bios_size
, -bios_size
,
234 bdrv_get_device_name(dinfo
->bdrv
), fl_sectors
);
236 pflash_cfi02_register((uint32_t)(-bios_size
),
237 NULL
, "ef405ep.bios", bios_size
,
238 dinfo
->bdrv
, 65536, fl_sectors
, 1,
239 2, 0x0001, 0x22DA, 0x0000, 0x0000, 0x555, 0x2AA,
245 #ifdef DEBUG_BOARD_INIT
246 printf("Load BIOS from file\n");
248 bios
= g_new(MemoryRegion
, 1);
249 memory_region_init_ram(bios
, NULL
, "ef405ep.bios", BIOS_SIZE
);
250 if (bios_name
== NULL
)
251 bios_name
= BIOS_FILENAME
;
252 filename
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, bios_name
);
254 bios_size
= load_image(filename
, memory_region_get_ram_ptr(bios
));
259 if (bios_size
< 0 || bios_size
> BIOS_SIZE
) {
260 fprintf(stderr
, "qemu: could not load PowerPC bios '%s'\n",
264 bios_size
= (bios_size
+ 0xfff) & ~0xfff;
265 memory_region_set_readonly(bios
, true);
266 memory_region_add_subregion(get_system_memory(),
267 (uint32_t)(-bios_size
), bios
);
270 #ifdef DEBUG_BOARD_INIT
271 printf("%s: register FPGA\n", __func__
);
273 ref405ep_fpga_init(0xF0300000);
275 #ifdef DEBUG_BOARD_INIT
276 printf("%s: register NVRAM\n", __func__
);
278 m48t59_init(NULL
, 0xF0000000, 0, 8192, 8);
280 linux_boot
= (kernel_filename
!= NULL
);
282 #ifdef DEBUG_BOARD_INIT
283 printf("%s: load kernel\n", __func__
);
285 memset(&bd
, 0, sizeof(bd
));
286 bd
.bi_memstart
= 0x00000000;
287 bd
.bi_memsize
= ram_size
;
288 bd
.bi_flashstart
= -bios_size
;
289 bd
.bi_flashsize
= -bios_size
;
290 bd
.bi_flashoffset
= 0;
291 bd
.bi_sramstart
= 0xFFF00000;
292 bd
.bi_sramsize
= sram_size
;
294 bd
.bi_intfreq
= 133333333;
295 bd
.bi_busfreq
= 33333333;
296 bd
.bi_baudrate
= 115200;
297 bd
.bi_s_version
[0] = 'Q';
298 bd
.bi_s_version
[1] = 'M';
299 bd
.bi_s_version
[2] = 'U';
300 bd
.bi_s_version
[3] = '\0';
301 bd
.bi_r_version
[0] = 'Q';
302 bd
.bi_r_version
[1] = 'E';
303 bd
.bi_r_version
[2] = 'M';
304 bd
.bi_r_version
[3] = 'U';
305 bd
.bi_r_version
[4] = '\0';
306 bd
.bi_procfreq
= 133333333;
307 bd
.bi_plb_busfreq
= 33333333;
308 bd
.bi_pci_busfreq
= 33333333;
309 bd
.bi_opbfreq
= 33333333;
310 bdloc
= ppc405_set_bootinfo(env
, &bd
, 0x00000001);
312 kernel_base
= KERNEL_LOAD_ADDR
;
313 /* now we can load the kernel */
314 kernel_size
= load_image_targphys(kernel_filename
, kernel_base
,
315 ram_size
- kernel_base
);
316 if (kernel_size
< 0) {
317 fprintf(stderr
, "qemu: could not load kernel '%s'\n",
321 printf("Load kernel size %ld at " TARGET_FMT_lx
,
322 kernel_size
, kernel_base
);
324 if (initrd_filename
) {
325 initrd_base
= INITRD_LOAD_ADDR
;
326 initrd_size
= load_image_targphys(initrd_filename
, initrd_base
,
327 ram_size
- initrd_base
);
328 if (initrd_size
< 0) {
329 fprintf(stderr
, "qemu: could not load initial ram disk '%s'\n",
337 env
->gpr
[4] = initrd_base
;
338 env
->gpr
[5] = initrd_size
;
339 if (kernel_cmdline
!= NULL
) {
340 len
= strlen(kernel_cmdline
);
341 bdloc
-= ((len
+ 255) & ~255);
342 cpu_physical_memory_write(bdloc
, (void *)kernel_cmdline
, len
+ 1);
344 env
->gpr
[7] = bdloc
+ len
;
349 env
->nip
= KERNEL_LOAD_ADDR
;
357 #ifdef DEBUG_BOARD_INIT
358 printf("%s: Done\n", __func__
);
360 printf("bdloc %016lx\n", (unsigned long)bdloc
);
363 static QEMUMachine ref405ep_machine
= {
366 .init
= ref405ep_init
,
369 /*****************************************************************************/
370 /* AMCC Taihu evaluation board */
371 /* - PowerPC 405EP processor
372 * - SDRAM 128 MB at 0x00000000
373 * - Boot flash 2 MB at 0xFFE00000
374 * - Application flash 32 MB at 0xFC000000
377 * - 1 USB 1.1 device 0x50000000
378 * - 1 LCD display 0x50100000
379 * - 1 CPLD 0x50100000
381 * - 1 I2C thermal sensor
383 * - bit-bang SPI port using GPIOs
384 * - 1 EBC interface connector 0 0x50200000
385 * - 1 cardbus controller + expansion slot.
386 * - 1 PCI expansion slot.
388 typedef struct taihu_cpld_t taihu_cpld_t
;
389 struct taihu_cpld_t
{
394 static uint32_t taihu_cpld_readb (void *opaque
, target_phys_addr_t addr
)
415 static void taihu_cpld_writeb (void *opaque
,
416 target_phys_addr_t addr
, uint32_t value
)
433 static uint32_t taihu_cpld_readw (void *opaque
, target_phys_addr_t addr
)
437 ret
= taihu_cpld_readb(opaque
, addr
) << 8;
438 ret
|= taihu_cpld_readb(opaque
, addr
+ 1);
443 static void taihu_cpld_writew (void *opaque
,
444 target_phys_addr_t addr
, uint32_t value
)
446 taihu_cpld_writeb(opaque
, addr
, (value
>> 8) & 0xFF);
447 taihu_cpld_writeb(opaque
, addr
+ 1, value
& 0xFF);
450 static uint32_t taihu_cpld_readl (void *opaque
, target_phys_addr_t addr
)
454 ret
= taihu_cpld_readb(opaque
, addr
) << 24;
455 ret
|= taihu_cpld_readb(opaque
, addr
+ 1) << 16;
456 ret
|= taihu_cpld_readb(opaque
, addr
+ 2) << 8;
457 ret
|= taihu_cpld_readb(opaque
, addr
+ 3);
462 static void taihu_cpld_writel (void *opaque
,
463 target_phys_addr_t addr
, uint32_t value
)
465 taihu_cpld_writel(opaque
, addr
, (value
>> 24) & 0xFF);
466 taihu_cpld_writel(opaque
, addr
+ 1, (value
>> 16) & 0xFF);
467 taihu_cpld_writel(opaque
, addr
+ 2, (value
>> 8) & 0xFF);
468 taihu_cpld_writeb(opaque
, addr
+ 3, value
& 0xFF);
471 static CPUReadMemoryFunc
* const taihu_cpld_read
[] = {
477 static CPUWriteMemoryFunc
* const taihu_cpld_write
[] = {
483 static void taihu_cpld_reset (void *opaque
)
492 static void taihu_cpld_init (uint32_t base
)
497 cpld
= g_malloc0(sizeof(taihu_cpld_t
));
498 cpld_memory
= cpu_register_io_memory(taihu_cpld_read
,
499 taihu_cpld_write
, cpld
,
500 DEVICE_NATIVE_ENDIAN
);
501 cpu_register_physical_memory(base
, 0x00000100, cpld_memory
);
502 qemu_register_reset(&taihu_cpld_reset
, cpld
);
505 static void taihu_405ep_init(ram_addr_t ram_size
,
506 const char *boot_device
,
507 const char *kernel_filename
,
508 const char *kernel_cmdline
,
509 const char *initrd_filename
,
510 const char *cpu_model
)
515 MemoryRegion
*ram_memories
= g_malloc(2 * sizeof(*ram_memories
));
516 target_phys_addr_t ram_bases
[2], ram_sizes
[2];
518 target_ulong kernel_base
, initrd_base
;
519 long kernel_size
, initrd_size
;
521 int fl_idx
, fl_sectors
;
524 /* RAM is soldered to the board so the size cannot be changed */
525 memory_region_init_ram(&ram_memories
[0], NULL
,
526 "taihu_405ep.ram-0", 0x04000000);
528 ram_sizes
[0] = 0x04000000;
529 memory_region_init_ram(&ram_memories
[1], NULL
,
530 "taihu_405ep.ram-1", 0x04000000);
531 ram_bases
[1] = 0x04000000;
532 ram_sizes
[1] = 0x04000000;
533 ram_size
= 0x08000000;
534 #ifdef DEBUG_BOARD_INIT
535 printf("%s: register cpu\n", __func__
);
537 ppc405ep_init(ram_memories
, ram_bases
, ram_sizes
, 33333333, &pic
,
538 kernel_filename
== NULL
? 0 : 1);
539 /* allocate and load BIOS */
540 #ifdef DEBUG_BOARD_INIT
541 printf("%s: register BIOS\n", __func__
);
544 #if defined(USE_FLASH_BIOS)
545 dinfo
= drive_get(IF_PFLASH
, 0, fl_idx
);
547 bios_size
= bdrv_getlength(dinfo
->bdrv
);
548 /* XXX: should check that size is 2MB */
549 // bios_size = 2 * 1024 * 1024;
550 fl_sectors
= (bios_size
+ 65535) >> 16;
551 #ifdef DEBUG_BOARD_INIT
552 printf("Register parallel flash %d size %lx"
553 " at addr %lx '%s' %d\n",
554 fl_idx
, bios_size
, -bios_size
,
555 bdrv_get_device_name(dinfo
->bdrv
), fl_sectors
);
557 pflash_cfi02_register((uint32_t)(-bios_size
),
558 NULL
, "taihu_405ep.bios", bios_size
,
559 dinfo
->bdrv
, 65536, fl_sectors
, 1,
560 4, 0x0001, 0x22DA, 0x0000, 0x0000, 0x555, 0x2AA,
566 #ifdef DEBUG_BOARD_INIT
567 printf("Load BIOS from file\n");
569 if (bios_name
== NULL
)
570 bios_name
= BIOS_FILENAME
;
571 bios
= g_new(MemoryRegion
, 1);
572 memory_region_init_ram(bios
, NULL
, "taihu_405ep.bios", BIOS_SIZE
);
573 filename
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, bios_name
);
575 bios_size
= load_image(filename
, memory_region_get_ram_ptr(bios
));
580 if (bios_size
< 0 || bios_size
> BIOS_SIZE
) {
581 fprintf(stderr
, "qemu: could not load PowerPC bios '%s'\n",
585 bios_size
= (bios_size
+ 0xfff) & ~0xfff;
586 memory_region_set_readonly(bios
, true);
587 memory_region_add_subregion(get_system_memory(), (uint32_t)(-bios_size
),
590 /* Register Linux flash */
591 dinfo
= drive_get(IF_PFLASH
, 0, fl_idx
);
593 bios_size
= bdrv_getlength(dinfo
->bdrv
);
594 /* XXX: should check that size is 32MB */
595 bios_size
= 32 * 1024 * 1024;
596 fl_sectors
= (bios_size
+ 65535) >> 16;
597 #ifdef DEBUG_BOARD_INIT
598 printf("Register parallel flash %d size %lx"
599 " at addr " TARGET_FMT_lx
" '%s'\n",
600 fl_idx
, bios_size
, (target_ulong
)0xfc000000,
601 bdrv_get_device_name(dinfo
->bdrv
));
603 pflash_cfi02_register(0xfc000000, NULL
, "taihu_405ep.flash", bios_size
,
604 dinfo
->bdrv
, 65536, fl_sectors
, 1,
605 4, 0x0001, 0x22DA, 0x0000, 0x0000, 0x555, 0x2AA,
609 /* Register CLPD & LCD display */
610 #ifdef DEBUG_BOARD_INIT
611 printf("%s: register CPLD\n", __func__
);
613 taihu_cpld_init(0x50100000);
615 linux_boot
= (kernel_filename
!= NULL
);
617 #ifdef DEBUG_BOARD_INIT
618 printf("%s: load kernel\n", __func__
);
620 kernel_base
= KERNEL_LOAD_ADDR
;
621 /* now we can load the kernel */
622 kernel_size
= load_image_targphys(kernel_filename
, kernel_base
,
623 ram_size
- kernel_base
);
624 if (kernel_size
< 0) {
625 fprintf(stderr
, "qemu: could not load kernel '%s'\n",
630 if (initrd_filename
) {
631 initrd_base
= INITRD_LOAD_ADDR
;
632 initrd_size
= load_image_targphys(initrd_filename
, initrd_base
,
633 ram_size
- initrd_base
);
634 if (initrd_size
< 0) {
636 "qemu: could not load initial ram disk '%s'\n",
650 #ifdef DEBUG_BOARD_INIT
651 printf("%s: Done\n", __func__
);
655 static QEMUMachine taihu_machine
= {
658 .init
= taihu_405ep_init
,
661 static void ppc405_machine_init(void)
663 qemu_register_machine(&ref405ep_machine
);
664 qemu_register_machine(&taihu_machine
);
667 machine_init(ppc405_machine_init
);