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 "sysemu/qtest.h"
31
#include "sysemu/block-backend.h"
32
#include "hw/boards.h"
33
#include "qemu/log.h"
34
#include "qemu/error-report.h"
35
#include "hw/loader.h"
36
#include "sysemu/block-backend.h"
37
#include "sysemu/blockdev.h"
38
#include "exec/address-spaces.h"
J
j_mayer 已提交
39 40 41 42 43 44 45 46 47

#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

48
//#define DEBUG_BOARD_INIT
J
j_mayer 已提交
49 50 51 52 53 54 55 56 57 58 59

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

A
Avi Kivity 已提交
66
static uint32_t ref405ep_fpga_readb (void *opaque, hwaddr addr)
J
j_mayer 已提交
67
{
A
Anthony Liguori 已提交
68
    ref405ep_fpga_t *fpga;
J
j_mayer 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
    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 已提交
88
                                  hwaddr addr, uint32_t value)
J
j_mayer 已提交
89
{
A
Anthony Liguori 已提交
90
    ref405ep_fpga_t *fpga;
J
j_mayer 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104

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

A
Avi Kivity 已提交
105
static uint32_t ref405ep_fpga_readw (void *opaque, hwaddr addr)
J
j_mayer 已提交
106 107 108 109 110 111 112 113 114 115
{
    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 已提交
116
                                  hwaddr addr, uint32_t value)
J
j_mayer 已提交
117 118 119 120 121
{
    ref405ep_fpga_writeb(opaque, addr, (value >> 8) & 0xFF);
    ref405ep_fpga_writeb(opaque, addr + 1, value & 0xFF);
}

A
Avi Kivity 已提交
122
static uint32_t ref405ep_fpga_readl (void *opaque, hwaddr addr)
J
j_mayer 已提交
123 124 125 126 127 128 129 130 131 132 133 134
{
    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 已提交
135
                                  hwaddr addr, uint32_t value)
J
j_mayer 已提交
136
{
A
aurel32 已提交
137 138 139
    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 已提交
140 141 142
    ref405ep_fpga_writeb(opaque, addr + 3, value & 0xFF);
}

A
Avi Kivity 已提交
143 144 145 146 147 148 149 150 151 152
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 已提交
153 154 155 156
};

static void ref405ep_fpga_reset (void *opaque)
{
A
Anthony Liguori 已提交
157
    ref405ep_fpga_t *fpga;
J
j_mayer 已提交
158 159 160 161 162 163

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

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

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

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

    /* XXX: fix this */
203 204
    memory_region_allocate_system_memory(&ram_memories[0], NULL, "ef405ep.ram",
                                         0x08000000);
A
Avi Kivity 已提交
205
    ram_bases[0] = 0;
J
j_mayer 已提交
206
    ram_sizes[0] = 0x08000000;
207
    memory_region_init(&ram_memories[1], NULL, "ef405ep.ram1", 0);
J
j_mayer 已提交
208 209 210 211 212 213
    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 已提交
214
    env = ppc405ep_init(sysmem, ram_memories, ram_bases, ram_sizes,
215
                        33333333, &pic, kernel_filename == NULL ? 0 : 1);
J
j_mayer 已提交
216
    /* allocate SRAM */
P
pbrook 已提交
217
    sram_size = 512 * 1024;
218 219
    memory_region_init_ram(sram, NULL, "ef405ep.sram", sram_size,
                           &error_fatal);
H
Hu Tao 已提交
220
    vmstate_register_ram_global(sram);
A
Avi Kivity 已提交
221
    memory_region_add_subregion(sysmem, 0xFFF00000, sram);
J
j_mayer 已提交
222 223 224 225 226 227
    /* 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 已提交
228 229
    dinfo = drive_get(IF_PFLASH, 0, fl_idx);
    if (dinfo) {
230
        BlockBackend *blk = blk_by_legacy_dinfo(dinfo);
231

232
        bios_size = blk_getlength(blk);
J
j_mayer 已提交
233 234
        fl_sectors = (bios_size + 65535) >> 16;
#ifdef DEBUG_BOARD_INIT
235
        printf("Register parallel flash %d size %lx"
236 237
               " at addr %lx '%s' %d\n",
               fl_idx, bios_size, -bios_size,
238
               blk_name(blk), fl_sectors);
J
j_mayer 已提交
239
#endif
240 241
        pflash_cfi02_register((uint32_t)(-bios_size),
                              NULL, "ef405ep.bios", bios_size,
242
                              blk, 65536, fl_sectors, 1,
243 244
                              2, 0x0001, 0x22DA, 0x0000, 0x0000, 0x555, 0x2AA,
                              1);
J
j_mayer 已提交
245 246 247 248 249 250 251
        fl_idx++;
    } else
#endif
    {
#ifdef DEBUG_BOARD_INIT
        printf("Load BIOS from file\n");
#endif
252
        bios = g_new(MemoryRegion, 1);
253
        memory_region_init_ram(bios, NULL, "ef405ep.bios", BIOS_SIZE,
254
                               &error_fatal);
H
Hu Tao 已提交
255 256
        vmstate_register_ram_global(bios);

257 258
        if (bios_name == NULL)
            bios_name = BIOS_FILENAME;
P
Paul Brook 已提交
259 260
        filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
        if (filename) {
261
            bios_size = load_image(filename, memory_region_get_ram_ptr(bios));
262
            g_free(filename);
263 264 265 266 267 268 269 270 271
            if (bios_size < 0 || bios_size > BIOS_SIZE) {
                error_report("Could not load PowerPC BIOS '%s'", bios_name);
                exit(1);
            }
            bios_size = (bios_size + 0xfff) & ~0xfff;
            memory_region_add_subregion(sysmem, (uint32_t)(-bios_size), bios);
        } else if (!qtest_enabled() || kernel_filename != NULL) {
            error_report("Could not load PowerPC BIOS '%s'", bios_name);
            exit(1);
P
Paul Brook 已提交
272
        } else {
273
            /* Avoid an uninitialized variable warning */
P
Paul Brook 已提交
274 275
            bios_size = -1;
        }
276
        memory_region_set_readonly(bios, true);
J
j_mayer 已提交
277 278 279 280 281
    }
    /* Register FPGA */
#ifdef DEBUG_BOARD_INIT
    printf("%s: register FPGA\n", __func__);
#endif
A
Avi Kivity 已提交
282
    ref405ep_fpga_init(sysmem, 0xF0300000);
J
j_mayer 已提交
283 284 285 286
    /* Register NVRAM */
#ifdef DEBUG_BOARD_INIT
    printf("%s: register NVRAM\n", __func__);
#endif
287
    m48t59_init(NULL, 0xF0000000, 0, 8192, 1968, 8);
J
j_mayer 已提交
288 289 290 291 292 293 294 295 296
    /* 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;
297
        bd.bi_flashstart = -bios_size;
J
j_mayer 已提交
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
        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;
319
        bdloc = ppc405_set_bootinfo(env, &bd, 0x00000001);
J
j_mayer 已提交
320 321 322
        env->gpr[3] = bdloc;
        kernel_base = KERNEL_LOAD_ADDR;
        /* now we can load the kernel */
P
pbrook 已提交
323 324
        kernel_size = load_image_targphys(kernel_filename, kernel_base,
                                          ram_size - kernel_base);
J
j_mayer 已提交
325
        if (kernel_size < 0) {
326
            fprintf(stderr, "qemu: could not load kernel '%s'\n",
J
j_mayer 已提交
327 328 329
                    kernel_filename);
            exit(1);
        }
330
        printf("Load kernel size %ld at " TARGET_FMT_lx,
P
pbrook 已提交
331
               kernel_size, kernel_base);
J
j_mayer 已提交
332 333 334
        /* load initrd */
        if (initrd_filename) {
            initrd_base = INITRD_LOAD_ADDR;
P
pbrook 已提交
335 336
            initrd_size = load_image_targphys(initrd_filename, initrd_base,
                                              ram_size - initrd_base);
J
j_mayer 已提交
337
            if (initrd_size < 0) {
338
                fprintf(stderr, "qemu: could not load initial ram disk '%s'\n",
J
j_mayer 已提交
339 340 341 342 343 344 345 346 347 348 349 350
                        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 已提交
351
            cpu_physical_memory_write(bdloc, kernel_cmdline, len + 1);
J
j_mayer 已提交
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
            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
367
    printf("bdloc " RAM_ADDR_FMT "\n", bdloc);
J
j_mayer 已提交
368 369 370 371
    printf("%s: Done\n", __func__);
#endif
}

372
static void ref405ep_class_init(ObjectClass *oc, void *data)
373
{
374 375
    MachineClass *mc = MACHINE_CLASS(oc);

376 377 378 379
    mc->desc = "ref405ep";
    mc->init = ref405ep_init;
}

380 381 382 383 384
static const TypeInfo ref405ep_type = {
    .name = MACHINE_TYPE_NAME("ref405ep"),
    .parent = TYPE_MACHINE,
    .class_init = ref405ep_class_init,
};
J
j_mayer 已提交
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410

/*****************************************************************************/
/* 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;
};

411
static uint64_t taihu_cpld_read(void *opaque, hwaddr addr, unsigned size)
J
j_mayer 已提交
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
{
    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;
}

432 433
static void taihu_cpld_write(void *opaque, hwaddr addr,
                             uint64_t value, unsigned size)
J
j_mayer 已提交
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
{
    taihu_cpld_t *cpld;

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

A
Avi Kivity 已提交
450
static const MemoryRegionOps taihu_cpld_ops = {
451 452 453 454 455
    .read = taihu_cpld_read,
    .write = taihu_cpld_write,
    .impl = {
        .min_access_size = 1,
        .max_access_size = 1,
A
Avi Kivity 已提交
456 457
    },
    .endianness = DEVICE_NATIVE_ENDIAN,
J
j_mayer 已提交
458 459 460 461 462 463 464 465 466 467 468
};

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

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

469
static void taihu_cpld_init(MemoryRegion *sysmem, uint32_t base)
J
j_mayer 已提交
470 471
{
    taihu_cpld_t *cpld;
A
Avi Kivity 已提交
472
    MemoryRegion *cpld_memory = g_new(MemoryRegion, 1);
J
j_mayer 已提交
473

474
    cpld = g_malloc0(sizeof(taihu_cpld_t));
475
    memory_region_init_io(cpld_memory, NULL, &taihu_cpld_ops, cpld, "cpld", 0x100);
A
Avi Kivity 已提交
476
    memory_region_add_subregion(sysmem, base, cpld_memory);
477
    qemu_register_reset(&taihu_cpld_reset, cpld);
J
j_mayer 已提交
478 479
}

480
static void taihu_405ep_init(MachineState *machine)
J
j_mayer 已提交
481
{
482 483 484
    ram_addr_t ram_size = machine->ram_size;
    const char *kernel_filename = machine->kernel_filename;
    const char *initrd_filename = machine->initrd_filename;
P
Paul Brook 已提交
485
    char *filename;
J
j_mayer 已提交
486
    qemu_irq *pic;
A
Avi Kivity 已提交
487
    MemoryRegion *sysmem = get_system_memory();
488
    MemoryRegion *bios;
A
Avi Kivity 已提交
489
    MemoryRegion *ram_memories = g_malloc(2 * sizeof(*ram_memories));
H
Hu Tao 已提交
490
    MemoryRegion *ram = g_malloc0(sizeof(*ram));
A
Avi Kivity 已提交
491
    hwaddr ram_bases[2], ram_sizes[2];
492 493 494
    long bios_size;
    target_ulong kernel_base, initrd_base;
    long kernel_size, initrd_size;
J
j_mayer 已提交
495 496
    int linux_boot;
    int fl_idx, fl_sectors;
G
Gerd Hoffmann 已提交
497
    DriveInfo *dinfo;
498

J
j_mayer 已提交
499
    /* RAM is soldered to the board so the size cannot be changed */
H
Hu Tao 已提交
500 501 502 503
    ram_size = 0x08000000;
    memory_region_allocate_system_memory(ram, NULL, "taihu_405ep.ram",
                                         ram_size);

A
Avi Kivity 已提交
504
    ram_bases[0] = 0;
J
j_mayer 已提交
505
    ram_sizes[0] = 0x04000000;
H
Hu Tao 已提交
506 507 508
    memory_region_init_alias(&ram_memories[0], NULL,
                             "taihu_405ep.ram-0", ram, ram_bases[0],
                             ram_sizes[0]);
A
Avi Kivity 已提交
509
    ram_bases[1] = 0x04000000;
J
j_mayer 已提交
510
    ram_sizes[1] = 0x04000000;
H
Hu Tao 已提交
511 512 513
    memory_region_init_alias(&ram_memories[1], NULL,
                             "taihu_405ep.ram-1", ram, ram_bases[1],
                             ram_sizes[1]);
J
j_mayer 已提交
514 515 516
#ifdef DEBUG_BOARD_INIT
    printf("%s: register cpu\n", __func__);
#endif
A
Avi Kivity 已提交
517
    ppc405ep_init(sysmem, ram_memories, ram_bases, ram_sizes,
518
                  33333333, &pic, kernel_filename == NULL ? 0 : 1);
J
j_mayer 已提交
519 520 521 522 523 524
    /* 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 已提交
525 526
    dinfo = drive_get(IF_PFLASH, 0, fl_idx);
    if (dinfo) {
527
        BlockBackend *blk = blk_by_legacy_dinfo(dinfo);
528

529
        bios_size = blk_getlength(blk);
J
j_mayer 已提交
530 531 532 533
        /* XXX: should check that size is 2MB */
        //        bios_size = 2 * 1024 * 1024;
        fl_sectors = (bios_size + 65535) >> 16;
#ifdef DEBUG_BOARD_INIT
534
        printf("Register parallel flash %d size %lx"
535 536
               " at addr %lx '%s' %d\n",
               fl_idx, bios_size, -bios_size,
537
               blk_name(blk), fl_sectors);
J
j_mayer 已提交
538
#endif
539 540
        pflash_cfi02_register((uint32_t)(-bios_size),
                              NULL, "taihu_405ep.bios", bios_size,
541
                              blk, 65536, fl_sectors, 1,
542 543
                              4, 0x0001, 0x22DA, 0x0000, 0x0000, 0x555, 0x2AA,
                              1);
J
j_mayer 已提交
544 545 546 547 548 549 550
        fl_idx++;
    } else
#endif
    {
#ifdef DEBUG_BOARD_INIT
        printf("Load BIOS from file\n");
#endif
551 552
        if (bios_name == NULL)
            bios_name = BIOS_FILENAME;
553
        bios = g_new(MemoryRegion, 1);
554
        memory_region_init_ram(bios, NULL, "taihu_405ep.bios", BIOS_SIZE,
555
                               &error_fatal);
H
Hu Tao 已提交
556
        vmstate_register_ram_global(bios);
P
Paul Brook 已提交
557 558
        filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
        if (filename) {
559
            bios_size = load_image(filename, memory_region_get_ram_ptr(bios));
560
            g_free(filename);
561 562 563 564 565 566 567 568
            if (bios_size < 0 || bios_size > BIOS_SIZE) {
                error_report("Could not load PowerPC BIOS '%s'", bios_name);
                exit(1);
            }
            bios_size = (bios_size + 0xfff) & ~0xfff;
            memory_region_add_subregion(sysmem, (uint32_t)(-bios_size), bios);
        } else if (!qtest_enabled()) {
            error_report("Could not load PowerPC BIOS '%s'", bios_name);
J
j_mayer 已提交
569 570
            exit(1);
        }
571
        memory_region_set_readonly(bios, true);
J
j_mayer 已提交
572 573
    }
    /* Register Linux flash */
G
Gerd Hoffmann 已提交
574 575
    dinfo = drive_get(IF_PFLASH, 0, fl_idx);
    if (dinfo) {
576
        BlockBackend *blk = blk_by_legacy_dinfo(dinfo);
577

578
        bios_size = blk_getlength(blk);
J
j_mayer 已提交
579 580 581 582
        /* XXX: should check that size is 32MB */
        bios_size = 32 * 1024 * 1024;
        fl_sectors = (bios_size + 65535) >> 16;
#ifdef DEBUG_BOARD_INIT
583
        printf("Register parallel flash %d size %lx"
584 585
               " at addr " TARGET_FMT_lx " '%s'\n",
               fl_idx, bios_size, (target_ulong)0xfc000000,
586
               blk_name(blk));
J
j_mayer 已提交
587
#endif
588
        pflash_cfi02_register(0xfc000000, NULL, "taihu_405ep.flash", bios_size,
589
                              blk, 65536, fl_sectors, 1,
590 591
                              4, 0x0001, 0x22DA, 0x0000, 0x0000, 0x555, 0x2AA,
                              1);
J
j_mayer 已提交
592 593 594 595 596 597
        fl_idx++;
    }
    /* Register CLPD & LCD display */
#ifdef DEBUG_BOARD_INIT
    printf("%s: register CPLD\n", __func__);
#endif
A
Avi Kivity 已提交
598
    taihu_cpld_init(sysmem, 0x50100000);
J
j_mayer 已提交
599 600 601 602 603 604 605 606
    /* 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 已提交
607 608
        kernel_size = load_image_targphys(kernel_filename, kernel_base,
                                          ram_size - kernel_base);
J
j_mayer 已提交
609
        if (kernel_size < 0) {
610
            fprintf(stderr, "qemu: could not load kernel '%s'\n",
J
j_mayer 已提交
611 612 613 614 615 616
                    kernel_filename);
            exit(1);
        }
        /* load initrd */
        if (initrd_filename) {
            initrd_base = INITRD_LOAD_ADDR;
P
pbrook 已提交
617 618
            initrd_size = load_image_targphys(initrd_filename, initrd_base,
                                              ram_size - initrd_base);
J
j_mayer 已提交
619 620
            if (initrd_size < 0) {
                fprintf(stderr,
621
                        "qemu: could not load initial ram disk '%s'\n",
J
j_mayer 已提交
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
                        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
}

640
static void taihu_class_init(ObjectClass *oc, void *data)
641
{
642 643
    MachineClass *mc = MACHINE_CLASS(oc);

644 645
    mc->desc = "taihu";
    mc->init = taihu_405ep_init;
646 647
}

648 649 650 651 652 653 654 655 656 657 658 659 660
static const TypeInfo taihu_type = {
    .name = MACHINE_TYPE_NAME("taihu"),
    .parent = TYPE_MACHINE,
    .class_init = taihu_class_init,
};

static void ppc405_machine_init(void)
{
    type_register_static(&ref405ep_type);
    type_register_static(&taihu_type);
}

machine_init(ppc405_machine_init)