ppc405_boards.c 19.0 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.
 */
P
Peter Maydell 已提交
24
#include "qemu/osdep.h"
25
#include "qemu/units.h"
26
#include "qapi/error.h"
27 28
#include "qemu-common.h"
#include "cpu.h"
29
#include "hw/hw.h"
P
Paolo Bonzini 已提交
30
#include "hw/ppc/ppc.h"
31
#include "ppc405.h"
P
Paolo Bonzini 已提交
32 33
#include "hw/timer/m48t59.h"
#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"
38
#include "qemu/log.h"
39
#include "qemu/error-report.h"
40
#include "hw/loader.h"
41
#include "exec/address-spaces.h"
J
j_mayer 已提交
42 43

#define BIOS_FILENAME "ppc405_rom.bin"
44
#define BIOS_SIZE (2 * MiB)
J
j_mayer 已提交
45 46 47 48 49 50

#define KERNEL_LOAD_ADDR 0x00000000
#define INITRD_LOAD_ADDR 0x01800000

#define USE_FLASH_BIOS

51
//#define DEBUG_BOARD_INIT
J
j_mayer 已提交
52 53 54 55 56 57 58 59 60 61 62

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

69
static uint64_t ref405ep_fpga_readb(void *opaque, hwaddr addr, unsigned size)
J
j_mayer 已提交
70
{
A
Anthony Liguori 已提交
71
    ref405ep_fpga_t *fpga;
J
j_mayer 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
    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;
}

90 91
static void ref405ep_fpga_writeb(void *opaque, hwaddr addr, uint64_t value,
                                 unsigned size)
J
j_mayer 已提交
92
{
A
Anthony Liguori 已提交
93
    ref405ep_fpga_t *fpga;
J
j_mayer 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107

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

A
Avi Kivity 已提交
108
static const MemoryRegionOps ref405ep_fpga_ops = {
109 110 111 112 113 114 115
    .read = ref405ep_fpga_readb,
    .write = ref405ep_fpga_writeb,
    .impl.min_access_size = 1,
    .impl.max_access_size = 1,
    .valid.min_access_size = 1,
    .valid.max_access_size = 4,
    .endianness = DEVICE_BIG_ENDIAN,
J
j_mayer 已提交
116 117 118 119
};

static void ref405ep_fpga_reset (void *opaque)
{
A
Anthony Liguori 已提交
120
    ref405ep_fpga_t *fpga;
J
j_mayer 已提交
121 122 123 124 125 126

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

127
static void ref405ep_fpga_init(MemoryRegion *sysmem, uint32_t base)
J
j_mayer 已提交
128
{
A
Anthony Liguori 已提交
129
    ref405ep_fpga_t *fpga;
A
Avi Kivity 已提交
130
    MemoryRegion *fpga_memory = g_new(MemoryRegion, 1);
J
j_mayer 已提交
131

132
    fpga = g_malloc0(sizeof(ref405ep_fpga_t));
133
    memory_region_init_io(fpga_memory, NULL, &ref405ep_fpga_ops, fpga,
A
Avi Kivity 已提交
134 135
                          "fpga", 0x00000100);
    memory_region_add_subregion(sysmem, base, fpga_memory);
136
    qemu_register_reset(&ref405ep_fpga_reset, fpga);
J
j_mayer 已提交
137 138
}

139
static void ref405ep_init(MachineState *machine)
J
j_mayer 已提交
140
{
141 142 143 144
    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 已提交
145
    char *filename;
A
Anthony Liguori 已提交
146
    ppc4xx_bd_info_t bd;
J
j_mayer 已提交
147 148
    CPUPPCState *env;
    qemu_irq *pic;
149
    MemoryRegion *bios;
A
Avi Kivity 已提交
150 151
    MemoryRegion *sram = g_new(MemoryRegion, 1);
    ram_addr_t bdloc;
A
Avi Kivity 已提交
152
    MemoryRegion *ram_memories = g_malloc(2 * sizeof(*ram_memories));
A
Avi Kivity 已提交
153
    hwaddr ram_bases[2], ram_sizes[2];
154 155
    target_ulong sram_size;
    long bios_size;
J
j_mayer 已提交
156 157
    //int phy_addr = 0;
    //static int phy_addr = 1;
158 159
    target_ulong kernel_base, initrd_base;
    long kernel_size, initrd_size;
J
j_mayer 已提交
160 161
    int linux_boot;
    int fl_idx, fl_sectors, len;
G
Gerd Hoffmann 已提交
162
    DriveInfo *dinfo;
A
Avi Kivity 已提交
163
    MemoryRegion *sysmem = get_system_memory();
J
j_mayer 已提交
164 165

    /* XXX: fix this */
166 167
    memory_region_allocate_system_memory(&ram_memories[0], NULL, "ef405ep.ram",
                                         0x08000000);
A
Avi Kivity 已提交
168
    ram_bases[0] = 0;
J
j_mayer 已提交
169
    ram_sizes[0] = 0x08000000;
170
    memory_region_init(&ram_memories[1], NULL, "ef405ep.ram1", 0);
J
j_mayer 已提交
171 172
    ram_bases[1] = 0x00000000;
    ram_sizes[1] = 0x00000000;
173
    ram_size = 128 * MiB;
J
j_mayer 已提交
174 175 176
#ifdef DEBUG_BOARD_INIT
    printf("%s: register cpu\n", __func__);
#endif
A
Avi Kivity 已提交
177
    env = ppc405ep_init(sysmem, ram_memories, ram_bases, ram_sizes,
178
                        33333333, &pic, kernel_filename == NULL ? 0 : 1);
J
j_mayer 已提交
179
    /* allocate SRAM */
180
    sram_size = 512 * KiB;
181
    memory_region_init_ram(sram, NULL, "ef405ep.sram", sram_size,
182
                           &error_fatal);
A
Avi Kivity 已提交
183
    memory_region_add_subregion(sysmem, 0xFFF00000, sram);
J
j_mayer 已提交
184 185 186 187 188 189
    /* 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 已提交
190 191
    dinfo = drive_get(IF_PFLASH, 0, fl_idx);
    if (dinfo) {
192
        BlockBackend *blk = blk_by_legacy_dinfo(dinfo);
193

194
        bios_size = blk_getlength(blk);
J
j_mayer 已提交
195 196
        fl_sectors = (bios_size + 65535) >> 16;
#ifdef DEBUG_BOARD_INIT
197
        printf("Register parallel flash %d size %lx"
198 199
               " at addr %lx '%s' %d\n",
               fl_idx, bios_size, -bios_size,
200
               blk_name(blk), fl_sectors);
J
j_mayer 已提交
201
#endif
202 203
        pflash_cfi02_register((uint32_t)(-bios_size),
                              NULL, "ef405ep.bios", bios_size,
204
                              blk, 65536, fl_sectors, 1,
205 206
                              2, 0x0001, 0x22DA, 0x0000, 0x0000, 0x555, 0x2AA,
                              1);
J
j_mayer 已提交
207 208 209 210 211 212 213
        fl_idx++;
    } else
#endif
    {
#ifdef DEBUG_BOARD_INIT
        printf("Load BIOS from file\n");
#endif
214
        bios = g_new(MemoryRegion, 1);
215
        memory_region_init_ram(bios, NULL, "ef405ep.bios", BIOS_SIZE,
216
                               &error_fatal);
H
Hu Tao 已提交
217

218 219
        if (bios_name == NULL)
            bios_name = BIOS_FILENAME;
P
Paul Brook 已提交
220 221
        filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
        if (filename) {
222 223 224
            bios_size = load_image_size(filename,
                                        memory_region_get_ram_ptr(bios),
                                        BIOS_SIZE);
225
            g_free(filename);
226
            if (bios_size < 0) {
227 228 229 230 231 232 233 234
                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 已提交
235
        } else {
236
            /* Avoid an uninitialized variable warning */
P
Paul Brook 已提交
237 238
            bios_size = -1;
        }
239
        memory_region_set_readonly(bios, true);
J
j_mayer 已提交
240 241 242 243 244
    }
    /* Register FPGA */
#ifdef DEBUG_BOARD_INIT
    printf("%s: register FPGA\n", __func__);
#endif
A
Avi Kivity 已提交
245
    ref405ep_fpga_init(sysmem, 0xF0300000);
J
j_mayer 已提交
246 247 248 249
    /* Register NVRAM */
#ifdef DEBUG_BOARD_INIT
    printf("%s: register NVRAM\n", __func__);
#endif
250
    m48t59_init(NULL, 0xF0000000, 0, 8192, 1968, 8);
J
j_mayer 已提交
251 252 253 254 255 256 257 258 259
    /* 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;
260
        bd.bi_flashstart = -bios_size;
J
j_mayer 已提交
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
        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;
282
        bdloc = ppc405_set_bootinfo(env, &bd, 0x00000001);
J
j_mayer 已提交
283 284 285
        env->gpr[3] = bdloc;
        kernel_base = KERNEL_LOAD_ADDR;
        /* now we can load the kernel */
P
pbrook 已提交
286 287
        kernel_size = load_image_targphys(kernel_filename, kernel_base,
                                          ram_size - kernel_base);
J
j_mayer 已提交
288
        if (kernel_size < 0) {
289
            error_report("could not load kernel '%s'", kernel_filename);
J
j_mayer 已提交
290 291
            exit(1);
        }
292
        printf("Load kernel size %ld at " TARGET_FMT_lx,
P
pbrook 已提交
293
               kernel_size, kernel_base);
J
j_mayer 已提交
294 295 296
        /* load initrd */
        if (initrd_filename) {
            initrd_base = INITRD_LOAD_ADDR;
P
pbrook 已提交
297 298
            initrd_size = load_image_targphys(initrd_filename, initrd_base,
                                              ram_size - initrd_base);
J
j_mayer 已提交
299
            if (initrd_size < 0) {
300 301
                error_report("could not load initial ram disk '%s'",
                             initrd_filename);
J
j_mayer 已提交
302 303 304 305 306 307 308 309 310 311 312
                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 已提交
313
            cpu_physical_memory_write(bdloc, kernel_cmdline, len + 1);
J
j_mayer 已提交
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
            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
329
    printf("bdloc " RAM_ADDR_FMT "\n", bdloc);
J
j_mayer 已提交
330 331 332 333
    printf("%s: Done\n", __func__);
#endif
}

334
static void ref405ep_class_init(ObjectClass *oc, void *data)
335
{
336 337
    MachineClass *mc = MACHINE_CLASS(oc);

338 339 340 341
    mc->desc = "ref405ep";
    mc->init = ref405ep_init;
}

342 343 344 345 346
static const TypeInfo ref405ep_type = {
    .name = MACHINE_TYPE_NAME("ref405ep"),
    .parent = TYPE_MACHINE,
    .class_init = ref405ep_class_init,
};
J
j_mayer 已提交
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372

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

373
static uint64_t taihu_cpld_read(void *opaque, hwaddr addr, unsigned size)
J
j_mayer 已提交
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
{
    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;
}

394 395
static void taihu_cpld_write(void *opaque, hwaddr addr,
                             uint64_t value, unsigned size)
J
j_mayer 已提交
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
{
    taihu_cpld_t *cpld;

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

A
Avi Kivity 已提交
412
static const MemoryRegionOps taihu_cpld_ops = {
413 414 415 416 417
    .read = taihu_cpld_read,
    .write = taihu_cpld_write,
    .impl = {
        .min_access_size = 1,
        .max_access_size = 1,
A
Avi Kivity 已提交
418 419
    },
    .endianness = DEVICE_NATIVE_ENDIAN,
J
j_mayer 已提交
420 421 422 423 424 425 426 427 428 429 430
};

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

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

431
static void taihu_cpld_init(MemoryRegion *sysmem, uint32_t base)
J
j_mayer 已提交
432 433
{
    taihu_cpld_t *cpld;
A
Avi Kivity 已提交
434
    MemoryRegion *cpld_memory = g_new(MemoryRegion, 1);
J
j_mayer 已提交
435

436
    cpld = g_malloc0(sizeof(taihu_cpld_t));
437
    memory_region_init_io(cpld_memory, NULL, &taihu_cpld_ops, cpld, "cpld", 0x100);
A
Avi Kivity 已提交
438
    memory_region_add_subregion(sysmem, base, cpld_memory);
439
    qemu_register_reset(&taihu_cpld_reset, cpld);
J
j_mayer 已提交
440 441
}

442
static void taihu_405ep_init(MachineState *machine)
J
j_mayer 已提交
443
{
444 445 446
    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 已提交
447
    char *filename;
J
j_mayer 已提交
448
    qemu_irq *pic;
A
Avi Kivity 已提交
449
    MemoryRegion *sysmem = get_system_memory();
450
    MemoryRegion *bios;
A
Avi Kivity 已提交
451
    MemoryRegion *ram_memories = g_malloc(2 * sizeof(*ram_memories));
H
Hu Tao 已提交
452
    MemoryRegion *ram = g_malloc0(sizeof(*ram));
A
Avi Kivity 已提交
453
    hwaddr ram_bases[2], ram_sizes[2];
454 455 456
    long bios_size;
    target_ulong kernel_base, initrd_base;
    long kernel_size, initrd_size;
J
j_mayer 已提交
457 458
    int linux_boot;
    int fl_idx, fl_sectors;
G
Gerd Hoffmann 已提交
459
    DriveInfo *dinfo;
460

J
j_mayer 已提交
461
    /* RAM is soldered to the board so the size cannot be changed */
H
Hu Tao 已提交
462 463 464 465
    ram_size = 0x08000000;
    memory_region_allocate_system_memory(ram, NULL, "taihu_405ep.ram",
                                         ram_size);

A
Avi Kivity 已提交
466
    ram_bases[0] = 0;
J
j_mayer 已提交
467
    ram_sizes[0] = 0x04000000;
H
Hu Tao 已提交
468 469 470
    memory_region_init_alias(&ram_memories[0], NULL,
                             "taihu_405ep.ram-0", ram, ram_bases[0],
                             ram_sizes[0]);
A
Avi Kivity 已提交
471
    ram_bases[1] = 0x04000000;
J
j_mayer 已提交
472
    ram_sizes[1] = 0x04000000;
H
Hu Tao 已提交
473 474 475
    memory_region_init_alias(&ram_memories[1], NULL,
                             "taihu_405ep.ram-1", ram, ram_bases[1],
                             ram_sizes[1]);
J
j_mayer 已提交
476 477 478
#ifdef DEBUG_BOARD_INIT
    printf("%s: register cpu\n", __func__);
#endif
A
Avi Kivity 已提交
479
    ppc405ep_init(sysmem, ram_memories, ram_bases, ram_sizes,
480
                  33333333, &pic, kernel_filename == NULL ? 0 : 1);
J
j_mayer 已提交
481 482 483 484 485 486
    /* 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 已提交
487 488
    dinfo = drive_get(IF_PFLASH, 0, fl_idx);
    if (dinfo) {
489
        BlockBackend *blk = blk_by_legacy_dinfo(dinfo);
490

491
        bios_size = blk_getlength(blk);
J
j_mayer 已提交
492 493 494 495
        /* XXX: should check that size is 2MB */
        //        bios_size = 2 * 1024 * 1024;
        fl_sectors = (bios_size + 65535) >> 16;
#ifdef DEBUG_BOARD_INIT
496
        printf("Register parallel flash %d size %lx"
497 498
               " at addr %lx '%s' %d\n",
               fl_idx, bios_size, -bios_size,
499
               blk_name(blk), fl_sectors);
J
j_mayer 已提交
500
#endif
501 502
        pflash_cfi02_register((uint32_t)(-bios_size),
                              NULL, "taihu_405ep.bios", bios_size,
503
                              blk, 65536, fl_sectors, 1,
504 505
                              4, 0x0001, 0x22DA, 0x0000, 0x0000, 0x555, 0x2AA,
                              1);
J
j_mayer 已提交
506 507 508 509 510 511 512
        fl_idx++;
    } else
#endif
    {
#ifdef DEBUG_BOARD_INIT
        printf("Load BIOS from file\n");
#endif
513 514
        if (bios_name == NULL)
            bios_name = BIOS_FILENAME;
515
        bios = g_new(MemoryRegion, 1);
516
        memory_region_init_ram(bios, NULL, "taihu_405ep.bios", BIOS_SIZE,
517
                               &error_fatal);
P
Paul Brook 已提交
518 519
        filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
        if (filename) {
520 521 522
            bios_size = load_image_size(filename,
                                        memory_region_get_ram_ptr(bios),
                                        BIOS_SIZE);
523
            g_free(filename);
524
            if (bios_size < 0) {
525 526 527 528 529 530 531
                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 已提交
532 533
            exit(1);
        }
534
        memory_region_set_readonly(bios, true);
J
j_mayer 已提交
535 536
    }
    /* Register Linux flash */
G
Gerd Hoffmann 已提交
537 538
    dinfo = drive_get(IF_PFLASH, 0, fl_idx);
    if (dinfo) {
539
        BlockBackend *blk = blk_by_legacy_dinfo(dinfo);
540

541
        bios_size = blk_getlength(blk);
J
j_mayer 已提交
542
        /* XXX: should check that size is 32MB */
543
        bios_size = 32 * MiB;
J
j_mayer 已提交
544 545
        fl_sectors = (bios_size + 65535) >> 16;
#ifdef DEBUG_BOARD_INIT
546
        printf("Register parallel flash %d size %lx"
547 548
               " at addr " TARGET_FMT_lx " '%s'\n",
               fl_idx, bios_size, (target_ulong)0xfc000000,
549
               blk_name(blk));
J
j_mayer 已提交
550
#endif
551
        pflash_cfi02_register(0xfc000000, NULL, "taihu_405ep.flash", bios_size,
552
                              blk, 65536, fl_sectors, 1,
553 554
                              4, 0x0001, 0x22DA, 0x0000, 0x0000, 0x555, 0x2AA,
                              1);
J
j_mayer 已提交
555 556 557 558 559 560
        fl_idx++;
    }
    /* Register CLPD & LCD display */
#ifdef DEBUG_BOARD_INIT
    printf("%s: register CPLD\n", __func__);
#endif
A
Avi Kivity 已提交
561
    taihu_cpld_init(sysmem, 0x50100000);
J
j_mayer 已提交
562 563 564 565 566 567 568 569
    /* 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 已提交
570 571
        kernel_size = load_image_targphys(kernel_filename, kernel_base,
                                          ram_size - kernel_base);
J
j_mayer 已提交
572
        if (kernel_size < 0) {
573
            error_report("could not load kernel '%s'", kernel_filename);
J
j_mayer 已提交
574 575 576 577 578
            exit(1);
        }
        /* load initrd */
        if (initrd_filename) {
            initrd_base = INITRD_LOAD_ADDR;
P
pbrook 已提交
579 580
            initrd_size = load_image_targphys(initrd_filename, initrd_base,
                                              ram_size - initrd_base);
J
j_mayer 已提交
581
            if (initrd_size < 0) {
582 583
                error_report("could not load initial ram disk '%s'",
                             initrd_filename);
J
j_mayer 已提交
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
                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
}

601
static void taihu_class_init(ObjectClass *oc, void *data)
602
{
603 604
    MachineClass *mc = MACHINE_CLASS(oc);

605 606
    mc->desc = "taihu";
    mc->init = taihu_405ep_init;
607 608
}

609 610 611 612 613 614 615 616 617 618 619 620
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);
}

621
type_init(ppc405_machine_init)