ppc405_boards.c 20.1 KB
Newer Older
J
j_mayer 已提交
1 2
/*
 * QEMU PowerPC 405 evaluation boards emulation
3
 *
J
j_mayer 已提交
4
 * Copyright (c) 2007 Jocelyn Mayer
5
 *
J
j_mayer 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
24
#include "hw/hw.h"
P
Paolo Bonzini 已提交
25
#include "hw/ppc/ppc.h"
26
#include "ppc405.h"
P
Paolo Bonzini 已提交
27 28
#include "hw/timer/m48t59.h"
#include "hw/block/flash.h"
29
#include "sysemu/sysemu.h"
30
#include "block/block.h"
31
#include "hw/boards.h"
32
#include "qemu/log.h"
33
#include "hw/loader.h"
34
#include "sysemu/blockdev.h"
35
#include "exec/address-spaces.h"
J
j_mayer 已提交
36 37 38 39 40 41 42 43 44

#define BIOS_FILENAME "ppc405_rom.bin"
#define BIOS_SIZE (2048 * 1024)

#define KERNEL_LOAD_ADDR 0x00000000
#define INITRD_LOAD_ADDR 0x01800000

#define USE_FLASH_BIOS

45
//#define DEBUG_BOARD_INIT
J
j_mayer 已提交
46 47 48 49 50 51 52 53 54 55 56

/*****************************************************************************/
/* PPC405EP reference board (IBM) */
/* Standalone board with:
 * - PowerPC 405EP CPU
 * - SDRAM (0x00000000)
 * - Flash (0xFFF80000)
 * - SRAM  (0xFFF00000)
 * - NVRAM (0xF0000000)
 * - FPGA  (0xF0300000)
 */
A
Anthony Liguori 已提交
57 58
typedef struct ref405ep_fpga_t ref405ep_fpga_t;
struct ref405ep_fpga_t {
J
j_mayer 已提交
59 60 61 62
    uint8_t reg0;
    uint8_t reg1;
};

A
Avi Kivity 已提交
63
static uint32_t ref405ep_fpga_readb (void *opaque, hwaddr addr)
J
j_mayer 已提交
64
{
A
Anthony Liguori 已提交
65
    ref405ep_fpga_t *fpga;
J
j_mayer 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
    uint32_t ret;

    fpga = opaque;
    switch (addr) {
    case 0x0:
        ret = fpga->reg0;
        break;
    case 0x1:
        ret = fpga->reg1;
        break;
    default:
        ret = 0;
        break;
    }

    return ret;
}

static void ref405ep_fpga_writeb (void *opaque,
A
Avi Kivity 已提交
85
                                  hwaddr addr, uint32_t value)
J
j_mayer 已提交
86
{
A
Anthony Liguori 已提交
87
    ref405ep_fpga_t *fpga;
J
j_mayer 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101

    fpga = opaque;
    switch (addr) {
    case 0x0:
        /* Read only */
        break;
    case 0x1:
        fpga->reg1 = value;
        break;
    default:
        break;
    }
}

A
Avi Kivity 已提交
102
static uint32_t ref405ep_fpga_readw (void *opaque, hwaddr addr)
J
j_mayer 已提交
103 104 105 106 107 108 109 110 111 112
{
    uint32_t ret;

    ret = ref405ep_fpga_readb(opaque, addr) << 8;
    ret |= ref405ep_fpga_readb(opaque, addr + 1);

    return ret;
}

static void ref405ep_fpga_writew (void *opaque,
A
Avi Kivity 已提交
113
                                  hwaddr addr, uint32_t value)
J
j_mayer 已提交
114 115 116 117 118
{
    ref405ep_fpga_writeb(opaque, addr, (value >> 8) & 0xFF);
    ref405ep_fpga_writeb(opaque, addr + 1, value & 0xFF);
}

A
Avi Kivity 已提交
119
static uint32_t ref405ep_fpga_readl (void *opaque, hwaddr addr)
J
j_mayer 已提交
120 121 122 123 124 125 126 127 128 129 130 131
{
    uint32_t ret;

    ret = ref405ep_fpga_readb(opaque, addr) << 24;
    ret |= ref405ep_fpga_readb(opaque, addr + 1) << 16;
    ret |= ref405ep_fpga_readb(opaque, addr + 2) << 8;
    ret |= ref405ep_fpga_readb(opaque, addr + 3);

    return ret;
}

static void ref405ep_fpga_writel (void *opaque,
A
Avi Kivity 已提交
132
                                  hwaddr addr, uint32_t value)
J
j_mayer 已提交
133
{
A
aurel32 已提交
134 135 136
    ref405ep_fpga_writeb(opaque, addr, (value >> 24) & 0xFF);
    ref405ep_fpga_writeb(opaque, addr + 1, (value >> 16) & 0xFF);
    ref405ep_fpga_writeb(opaque, addr + 2, (value >> 8) & 0xFF);
J
j_mayer 已提交
137 138 139
    ref405ep_fpga_writeb(opaque, addr + 3, value & 0xFF);
}

A
Avi Kivity 已提交
140 141 142 143 144 145 146 147 148 149
static const MemoryRegionOps ref405ep_fpga_ops = {
    .old_mmio = {
        .read = {
            ref405ep_fpga_readb, ref405ep_fpga_readw, ref405ep_fpga_readl,
        },
        .write = {
            ref405ep_fpga_writeb, ref405ep_fpga_writew, ref405ep_fpga_writel,
        },
    },
    .endianness = DEVICE_NATIVE_ENDIAN,
J
j_mayer 已提交
150 151 152 153
};

static void ref405ep_fpga_reset (void *opaque)
{
A
Anthony Liguori 已提交
154
    ref405ep_fpga_t *fpga;
J
j_mayer 已提交
155 156 157 158 159 160

    fpga = opaque;
    fpga->reg0 = 0x00;
    fpga->reg1 = 0x0F;
}

161
static void ref405ep_fpga_init(MemoryRegion *sysmem, uint32_t base)
J
j_mayer 已提交
162
{
A
Anthony Liguori 已提交
163
    ref405ep_fpga_t *fpga;
A
Avi Kivity 已提交
164
    MemoryRegion *fpga_memory = g_new(MemoryRegion, 1);
J
j_mayer 已提交
165

166
    fpga = g_malloc0(sizeof(ref405ep_fpga_t));
167
    memory_region_init_io(fpga_memory, NULL, &ref405ep_fpga_ops, fpga,
A
Avi Kivity 已提交
168 169
                          "fpga", 0x00000100);
    memory_region_add_subregion(sysmem, base, fpga_memory);
170
    qemu_register_reset(&ref405ep_fpga_reset, fpga);
J
j_mayer 已提交
171 172
}

173
static void ref405ep_init(QEMUMachineInitArgs *args)
J
j_mayer 已提交
174
{
175 176 177 178
    ram_addr_t ram_size = args->ram_size;
    const char *kernel_filename = args->kernel_filename;
    const char *kernel_cmdline = args->kernel_cmdline;
    const char *initrd_filename = args->initrd_filename;
P
Paul Brook 已提交
179
    char *filename;
A
Anthony Liguori 已提交
180
    ppc4xx_bd_info_t bd;
J
j_mayer 已提交
181 182
    CPUPPCState *env;
    qemu_irq *pic;
183
    MemoryRegion *bios;
A
Avi Kivity 已提交
184 185
    MemoryRegion *sram = g_new(MemoryRegion, 1);
    ram_addr_t bdloc;
A
Avi Kivity 已提交
186
    MemoryRegion *ram_memories = g_malloc(2 * sizeof(*ram_memories));
A
Avi Kivity 已提交
187
    hwaddr ram_bases[2], ram_sizes[2];
188 189
    target_ulong sram_size;
    long bios_size;
J
j_mayer 已提交
190 191
    //int phy_addr = 0;
    //static int phy_addr = 1;
192 193
    target_ulong kernel_base, initrd_base;
    long kernel_size, initrd_size;
J
j_mayer 已提交
194 195
    int linux_boot;
    int fl_idx, fl_sectors, len;
G
Gerd Hoffmann 已提交
196
    DriveInfo *dinfo;
A
Avi Kivity 已提交
197
    MemoryRegion *sysmem = get_system_memory();
J
j_mayer 已提交
198 199

    /* XXX: fix this */
200
    memory_region_init_ram(&ram_memories[0], NULL, "ef405ep.ram", 0x08000000);
201
    vmstate_register_ram_global(&ram_memories[0]);
A
Avi Kivity 已提交
202
    ram_bases[0] = 0;
J
j_mayer 已提交
203
    ram_sizes[0] = 0x08000000;
204
    memory_region_init(&ram_memories[1], NULL, "ef405ep.ram1", 0);
J
j_mayer 已提交
205 206 207 208 209 210
    ram_bases[1] = 0x00000000;
    ram_sizes[1] = 0x00000000;
    ram_size = 128 * 1024 * 1024;
#ifdef DEBUG_BOARD_INIT
    printf("%s: register cpu\n", __func__);
#endif
A
Avi Kivity 已提交
211
    env = ppc405ep_init(sysmem, ram_memories, ram_bases, ram_sizes,
212
                        33333333, &pic, kernel_filename == NULL ? 0 : 1);
J
j_mayer 已提交
213
    /* allocate SRAM */
P
pbrook 已提交
214
    sram_size = 512 * 1024;
215
    memory_region_init_ram(sram, NULL, "ef405ep.sram", sram_size);
216
    vmstate_register_ram_global(sram);
A
Avi Kivity 已提交
217
    memory_region_add_subregion(sysmem, 0xFFF00000, sram);
J
j_mayer 已提交
218 219 220 221 222 223
    /* allocate and load BIOS */
#ifdef DEBUG_BOARD_INIT
    printf("%s: register BIOS\n", __func__);
#endif
    fl_idx = 0;
#ifdef USE_FLASH_BIOS
G
Gerd Hoffmann 已提交
224 225 226
    dinfo = drive_get(IF_PFLASH, 0, fl_idx);
    if (dinfo) {
        bios_size = bdrv_getlength(dinfo->bdrv);
J
j_mayer 已提交
227 228
        fl_sectors = (bios_size + 65535) >> 16;
#ifdef DEBUG_BOARD_INIT
229
        printf("Register parallel flash %d size %lx"
230 231
               " at addr %lx '%s' %d\n",
               fl_idx, bios_size, -bios_size,
G
Gerd Hoffmann 已提交
232
               bdrv_get_device_name(dinfo->bdrv), fl_sectors);
J
j_mayer 已提交
233
#endif
234 235
        pflash_cfi02_register((uint32_t)(-bios_size),
                              NULL, "ef405ep.bios", bios_size,
G
Gerd Hoffmann 已提交
236
                              dinfo->bdrv, 65536, fl_sectors, 1,
237 238
                              2, 0x0001, 0x22DA, 0x0000, 0x0000, 0x555, 0x2AA,
                              1);
J
j_mayer 已提交
239 240 241 242 243 244 245
        fl_idx++;
    } else
#endif
    {
#ifdef DEBUG_BOARD_INIT
        printf("Load BIOS from file\n");
#endif
246
        bios = g_new(MemoryRegion, 1);
247
        memory_region_init_ram(bios, NULL, "ef405ep.bios", BIOS_SIZE);
248
        vmstate_register_ram_global(bios);
249 250
        if (bios_name == NULL)
            bios_name = BIOS_FILENAME;
P
Paul Brook 已提交
251 252
        filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
        if (filename) {
253
            bios_size = load_image(filename, memory_region_get_ram_ptr(bios));
254
            g_free(filename);
P
Paul Brook 已提交
255 256 257
        } else {
            bios_size = -1;
        }
J
j_mayer 已提交
258
        if (bios_size < 0 || bios_size > BIOS_SIZE) {
P
Paul Brook 已提交
259 260
            fprintf(stderr, "qemu: could not load PowerPC bios '%s'\n",
                    bios_name);
J
j_mayer 已提交
261 262 263
            exit(1);
        }
        bios_size = (bios_size + 0xfff) & ~0xfff;
264
        memory_region_set_readonly(bios, true);
A
Avi Kivity 已提交
265
        memory_region_add_subregion(sysmem, (uint32_t)(-bios_size), bios);
J
j_mayer 已提交
266 267 268 269 270
    }
    /* Register FPGA */
#ifdef DEBUG_BOARD_INIT
    printf("%s: register FPGA\n", __func__);
#endif
A
Avi Kivity 已提交
271
    ref405ep_fpga_init(sysmem, 0xF0300000);
J
j_mayer 已提交
272 273 274 275 276 277 278 279 280 281 282 283 284 285
    /* Register NVRAM */
#ifdef DEBUG_BOARD_INIT
    printf("%s: register NVRAM\n", __func__);
#endif
    m48t59_init(NULL, 0xF0000000, 0, 8192, 8);
    /* Load kernel */
    linux_boot = (kernel_filename != NULL);
    if (linux_boot) {
#ifdef DEBUG_BOARD_INIT
        printf("%s: load kernel\n", __func__);
#endif
        memset(&bd, 0, sizeof(bd));
        bd.bi_memstart = 0x00000000;
        bd.bi_memsize = ram_size;
286
        bd.bi_flashstart = -bios_size;
J
j_mayer 已提交
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
        bd.bi_flashsize = -bios_size;
        bd.bi_flashoffset = 0;
        bd.bi_sramstart = 0xFFF00000;
        bd.bi_sramsize = sram_size;
        bd.bi_bootflags = 0;
        bd.bi_intfreq = 133333333;
        bd.bi_busfreq = 33333333;
        bd.bi_baudrate = 115200;
        bd.bi_s_version[0] = 'Q';
        bd.bi_s_version[1] = 'M';
        bd.bi_s_version[2] = 'U';
        bd.bi_s_version[3] = '\0';
        bd.bi_r_version[0] = 'Q';
        bd.bi_r_version[1] = 'E';
        bd.bi_r_version[2] = 'M';
        bd.bi_r_version[3] = 'U';
        bd.bi_r_version[4] = '\0';
        bd.bi_procfreq = 133333333;
        bd.bi_plb_busfreq = 33333333;
        bd.bi_pci_busfreq = 33333333;
        bd.bi_opbfreq = 33333333;
308
        bdloc = ppc405_set_bootinfo(env, &bd, 0x00000001);
J
j_mayer 已提交
309 310 311
        env->gpr[3] = bdloc;
        kernel_base = KERNEL_LOAD_ADDR;
        /* now we can load the kernel */
P
pbrook 已提交
312 313
        kernel_size = load_image_targphys(kernel_filename, kernel_base,
                                          ram_size - kernel_base);
J
j_mayer 已提交
314
        if (kernel_size < 0) {
315
            fprintf(stderr, "qemu: could not load kernel '%s'\n",
J
j_mayer 已提交
316 317 318
                    kernel_filename);
            exit(1);
        }
319
        printf("Load kernel size %ld at " TARGET_FMT_lx,
P
pbrook 已提交
320
               kernel_size, kernel_base);
J
j_mayer 已提交
321 322 323
        /* load initrd */
        if (initrd_filename) {
            initrd_base = INITRD_LOAD_ADDR;
P
pbrook 已提交
324 325
            initrd_size = load_image_targphys(initrd_filename, initrd_base,
                                              ram_size - initrd_base);
J
j_mayer 已提交
326
            if (initrd_size < 0) {
327
                fprintf(stderr, "qemu: could not load initial ram disk '%s'\n",
J
j_mayer 已提交
328 329 330 331 332 333 334 335 336 337 338 339
                        initrd_filename);
                exit(1);
            }
        } else {
            initrd_base = 0;
            initrd_size = 0;
        }
        env->gpr[4] = initrd_base;
        env->gpr[5] = initrd_size;
        if (kernel_cmdline != NULL) {
            len = strlen(kernel_cmdline);
            bdloc -= ((len + 255) & ~255);
S
Stefan Weil 已提交
340
            cpu_physical_memory_write(bdloc, kernel_cmdline, len + 1);
J
j_mayer 已提交
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
            env->gpr[6] = bdloc;
            env->gpr[7] = bdloc + len;
        } else {
            env->gpr[6] = 0;
            env->gpr[7] = 0;
        }
        env->nip = KERNEL_LOAD_ADDR;
    } else {
        kernel_base = 0;
        kernel_size = 0;
        initrd_base = 0;
        initrd_size = 0;
        bdloc = 0;
    }
#ifdef DEBUG_BOARD_INIT
356
    printf("bdloc " RAM_ADDR_FMT "\n", bdloc);
J
j_mayer 已提交
357 358 359 360
    printf("%s: Done\n", __func__);
#endif
}

361
static QEMUMachine ref405ep_machine = {
362 363 364
    .name = "ref405ep",
    .desc = "ref405ep",
    .init = ref405ep_init,
365
    DEFAULT_MACHINE_OPTIONS,
J
j_mayer 已提交
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
};

/*****************************************************************************/
/* AMCC Taihu evaluation board */
/* - PowerPC 405EP processor
 * - SDRAM               128 MB at 0x00000000
 * - Boot flash          2 MB   at 0xFFE00000
 * - Application flash   32 MB  at 0xFC000000
 * - 2 serial ports
 * - 2 ethernet PHY
 * - 1 USB 1.1 device    0x50000000
 * - 1 LCD display       0x50100000
 * - 1 CPLD              0x50100000
 * - 1 I2C EEPROM
 * - 1 I2C thermal sensor
 * - a set of LEDs
 * - bit-bang SPI port using GPIOs
 * - 1 EBC interface connector 0 0x50200000
 * - 1 cardbus controller + expansion slot.
 * - 1 PCI expansion slot.
 */
typedef struct taihu_cpld_t taihu_cpld_t;
struct taihu_cpld_t {
    uint8_t reg0;
    uint8_t reg1;
};

A
Avi Kivity 已提交
393
static uint32_t taihu_cpld_readb (void *opaque, hwaddr addr)
J
j_mayer 已提交
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
{
    taihu_cpld_t *cpld;
    uint32_t ret;

    cpld = opaque;
    switch (addr) {
    case 0x0:
        ret = cpld->reg0;
        break;
    case 0x1:
        ret = cpld->reg1;
        break;
    default:
        ret = 0;
        break;
    }

    return ret;
}

static void taihu_cpld_writeb (void *opaque,
A
Avi Kivity 已提交
415
                               hwaddr addr, uint32_t value)
J
j_mayer 已提交
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
{
    taihu_cpld_t *cpld;

    cpld = opaque;
    switch (addr) {
    case 0x0:
        /* Read only */
        break;
    case 0x1:
        cpld->reg1 = value;
        break;
    default:
        break;
    }
}

A
Avi Kivity 已提交
432
static uint32_t taihu_cpld_readw (void *opaque, hwaddr addr)
J
j_mayer 已提交
433 434 435 436 437 438 439 440 441 442
{
    uint32_t ret;

    ret = taihu_cpld_readb(opaque, addr) << 8;
    ret |= taihu_cpld_readb(opaque, addr + 1);

    return ret;
}

static void taihu_cpld_writew (void *opaque,
A
Avi Kivity 已提交
443
                               hwaddr addr, uint32_t value)
J
j_mayer 已提交
444 445 446 447 448
{
    taihu_cpld_writeb(opaque, addr, (value >> 8) & 0xFF);
    taihu_cpld_writeb(opaque, addr + 1, value & 0xFF);
}

A
Avi Kivity 已提交
449
static uint32_t taihu_cpld_readl (void *opaque, hwaddr addr)
J
j_mayer 已提交
450 451 452 453 454 455 456 457 458 459 460 461
{
    uint32_t ret;

    ret = taihu_cpld_readb(opaque, addr) << 24;
    ret |= taihu_cpld_readb(opaque, addr + 1) << 16;
    ret |= taihu_cpld_readb(opaque, addr + 2) << 8;
    ret |= taihu_cpld_readb(opaque, addr + 3);

    return ret;
}

static void taihu_cpld_writel (void *opaque,
A
Avi Kivity 已提交
462
                               hwaddr addr, uint32_t value)
J
j_mayer 已提交
463 464 465 466 467 468 469
{
    taihu_cpld_writel(opaque, addr, (value >> 24) & 0xFF);
    taihu_cpld_writel(opaque, addr + 1, (value >> 16) & 0xFF);
    taihu_cpld_writel(opaque, addr + 2, (value >> 8) & 0xFF);
    taihu_cpld_writeb(opaque, addr + 3, value & 0xFF);
}

A
Avi Kivity 已提交
470 471 472 473 474 475
static const MemoryRegionOps taihu_cpld_ops = {
    .old_mmio = {
        .read = { taihu_cpld_readb, taihu_cpld_readw, taihu_cpld_readl, },
        .write = { taihu_cpld_writeb, taihu_cpld_writew, taihu_cpld_writel, },
    },
    .endianness = DEVICE_NATIVE_ENDIAN,
J
j_mayer 已提交
476 477 478 479 480 481 482 483 484 485 486
};

static void taihu_cpld_reset (void *opaque)
{
    taihu_cpld_t *cpld;

    cpld = opaque;
    cpld->reg0 = 0x01;
    cpld->reg1 = 0x80;
}

487
static void taihu_cpld_init(MemoryRegion *sysmem, uint32_t base)
J
j_mayer 已提交
488 489
{
    taihu_cpld_t *cpld;
A
Avi Kivity 已提交
490
    MemoryRegion *cpld_memory = g_new(MemoryRegion, 1);
J
j_mayer 已提交
491

492
    cpld = g_malloc0(sizeof(taihu_cpld_t));
493
    memory_region_init_io(cpld_memory, NULL, &taihu_cpld_ops, cpld, "cpld", 0x100);
A
Avi Kivity 已提交
494
    memory_region_add_subregion(sysmem, base, cpld_memory);
495
    qemu_register_reset(&taihu_cpld_reset, cpld);
J
j_mayer 已提交
496 497
}

498
static void taihu_405ep_init(QEMUMachineInitArgs *args)
J
j_mayer 已提交
499
{
500 501 502
    ram_addr_t ram_size = args->ram_size;
    const char *kernel_filename = args->kernel_filename;
    const char *initrd_filename = args->initrd_filename;
P
Paul Brook 已提交
503
    char *filename;
J
j_mayer 已提交
504
    qemu_irq *pic;
A
Avi Kivity 已提交
505
    MemoryRegion *sysmem = get_system_memory();
506
    MemoryRegion *bios;
A
Avi Kivity 已提交
507
    MemoryRegion *ram_memories = g_malloc(2 * sizeof(*ram_memories));
A
Avi Kivity 已提交
508
    hwaddr ram_bases[2], ram_sizes[2];
509 510 511
    long bios_size;
    target_ulong kernel_base, initrd_base;
    long kernel_size, initrd_size;
J
j_mayer 已提交
512 513
    int linux_boot;
    int fl_idx, fl_sectors;
G
Gerd Hoffmann 已提交
514
    DriveInfo *dinfo;
515

J
j_mayer 已提交
516
    /* RAM is soldered to the board so the size cannot be changed */
517
    memory_region_init_ram(&ram_memories[0], NULL,
A
Avi Kivity 已提交
518
                           "taihu_405ep.ram-0", 0x04000000);
519
    vmstate_register_ram_global(&ram_memories[0]);
A
Avi Kivity 已提交
520
    ram_bases[0] = 0;
J
j_mayer 已提交
521
    ram_sizes[0] = 0x04000000;
522
    memory_region_init_ram(&ram_memories[1], NULL,
A
Avi Kivity 已提交
523
                           "taihu_405ep.ram-1", 0x04000000);
524
    vmstate_register_ram_global(&ram_memories[1]);
A
Avi Kivity 已提交
525
    ram_bases[1] = 0x04000000;
J
j_mayer 已提交
526
    ram_sizes[1] = 0x04000000;
P
pbrook 已提交
527
    ram_size = 0x08000000;
J
j_mayer 已提交
528 529 530
#ifdef DEBUG_BOARD_INIT
    printf("%s: register cpu\n", __func__);
#endif
A
Avi Kivity 已提交
531
    ppc405ep_init(sysmem, ram_memories, ram_bases, ram_sizes,
532
                  33333333, &pic, kernel_filename == NULL ? 0 : 1);
J
j_mayer 已提交
533 534 535 536 537 538
    /* allocate and load BIOS */
#ifdef DEBUG_BOARD_INIT
    printf("%s: register BIOS\n", __func__);
#endif
    fl_idx = 0;
#if defined(USE_FLASH_BIOS)
G
Gerd Hoffmann 已提交
539 540 541
    dinfo = drive_get(IF_PFLASH, 0, fl_idx);
    if (dinfo) {
        bios_size = bdrv_getlength(dinfo->bdrv);
J
j_mayer 已提交
542 543 544 545
        /* XXX: should check that size is 2MB */
        //        bios_size = 2 * 1024 * 1024;
        fl_sectors = (bios_size + 65535) >> 16;
#ifdef DEBUG_BOARD_INIT
546
        printf("Register parallel flash %d size %lx"
547 548
               " at addr %lx '%s' %d\n",
               fl_idx, bios_size, -bios_size,
G
Gerd Hoffmann 已提交
549
               bdrv_get_device_name(dinfo->bdrv), fl_sectors);
J
j_mayer 已提交
550
#endif
551 552
        pflash_cfi02_register((uint32_t)(-bios_size),
                              NULL, "taihu_405ep.bios", bios_size,
G
Gerd Hoffmann 已提交
553
                              dinfo->bdrv, 65536, fl_sectors, 1,
554 555
                              4, 0x0001, 0x22DA, 0x0000, 0x0000, 0x555, 0x2AA,
                              1);
J
j_mayer 已提交
556 557 558 559 560 561 562
        fl_idx++;
    } else
#endif
    {
#ifdef DEBUG_BOARD_INIT
        printf("Load BIOS from file\n");
#endif
563 564
        if (bios_name == NULL)
            bios_name = BIOS_FILENAME;
565
        bios = g_new(MemoryRegion, 1);
566
        memory_region_init_ram(bios, NULL, "taihu_405ep.bios", BIOS_SIZE);
567
        vmstate_register_ram_global(bios);
P
Paul Brook 已提交
568 569
        filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
        if (filename) {
570
            bios_size = load_image(filename, memory_region_get_ram_ptr(bios));
571
            g_free(filename);
P
Paul Brook 已提交
572 573 574
        } else {
            bios_size = -1;
        }
J
j_mayer 已提交
575
        if (bios_size < 0 || bios_size > BIOS_SIZE) {
P
Paul Brook 已提交
576 577
            fprintf(stderr, "qemu: could not load PowerPC bios '%s'\n",
                    bios_name);
J
j_mayer 已提交
578 579 580
            exit(1);
        }
        bios_size = (bios_size + 0xfff) & ~0xfff;
581
        memory_region_set_readonly(bios, true);
A
Avi Kivity 已提交
582
        memory_region_add_subregion(sysmem, (uint32_t)(-bios_size), bios);
J
j_mayer 已提交
583 584
    }
    /* Register Linux flash */
G
Gerd Hoffmann 已提交
585 586 587
    dinfo = drive_get(IF_PFLASH, 0, fl_idx);
    if (dinfo) {
        bios_size = bdrv_getlength(dinfo->bdrv);
J
j_mayer 已提交
588 589 590 591
        /* XXX: should check that size is 32MB */
        bios_size = 32 * 1024 * 1024;
        fl_sectors = (bios_size + 65535) >> 16;
#ifdef DEBUG_BOARD_INIT
592
        printf("Register parallel flash %d size %lx"
593 594
               " at addr " TARGET_FMT_lx " '%s'\n",
               fl_idx, bios_size, (target_ulong)0xfc000000,
G
Gerd Hoffmann 已提交
595
               bdrv_get_device_name(dinfo->bdrv));
J
j_mayer 已提交
596
#endif
597
        pflash_cfi02_register(0xfc000000, NULL, "taihu_405ep.flash", bios_size,
G
Gerd Hoffmann 已提交
598
                              dinfo->bdrv, 65536, fl_sectors, 1,
599 600
                              4, 0x0001, 0x22DA, 0x0000, 0x0000, 0x555, 0x2AA,
                              1);
J
j_mayer 已提交
601 602 603 604 605 606
        fl_idx++;
    }
    /* Register CLPD & LCD display */
#ifdef DEBUG_BOARD_INIT
    printf("%s: register CPLD\n", __func__);
#endif
A
Avi Kivity 已提交
607
    taihu_cpld_init(sysmem, 0x50100000);
J
j_mayer 已提交
608 609 610 611 612 613 614 615
    /* Load kernel */
    linux_boot = (kernel_filename != NULL);
    if (linux_boot) {
#ifdef DEBUG_BOARD_INIT
        printf("%s: load kernel\n", __func__);
#endif
        kernel_base = KERNEL_LOAD_ADDR;
        /* now we can load the kernel */
P
pbrook 已提交
616 617
        kernel_size = load_image_targphys(kernel_filename, kernel_base,
                                          ram_size - kernel_base);
J
j_mayer 已提交
618
        if (kernel_size < 0) {
619
            fprintf(stderr, "qemu: could not load kernel '%s'\n",
J
j_mayer 已提交
620 621 622 623 624 625
                    kernel_filename);
            exit(1);
        }
        /* load initrd */
        if (initrd_filename) {
            initrd_base = INITRD_LOAD_ADDR;
P
pbrook 已提交
626 627
            initrd_size = load_image_targphys(initrd_filename, initrd_base,
                                              ram_size - initrd_base);
J
j_mayer 已提交
628 629
            if (initrd_size < 0) {
                fprintf(stderr,
630
                        "qemu: could not load initial ram disk '%s'\n",
J
j_mayer 已提交
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
                        initrd_filename);
                exit(1);
            }
        } else {
            initrd_base = 0;
            initrd_size = 0;
        }
    } else {
        kernel_base = 0;
        kernel_size = 0;
        initrd_base = 0;
        initrd_size = 0;
    }
#ifdef DEBUG_BOARD_INIT
    printf("%s: Done\n", __func__);
#endif
}

649
static QEMUMachine taihu_machine = {
P
pbrook 已提交
650 651 652
    .name = "taihu",
    .desc = "taihu",
    .init = taihu_405ep_init,
653
    DEFAULT_MACHINE_OPTIONS,
J
j_mayer 已提交
654
};
655 656 657 658 659 660 661 662

static void ppc405_machine_init(void)
{
    qemu_register_machine(&ref405ep_machine);
    qemu_register_machine(&taihu_machine);
}

machine_init(ppc405_machine_init);