ppc_prep.c 21.5 KB
Newer Older
1
/*
2
 * QEMU PPC PREP hardware System Emulator
3
 *
4
 * Copyright (c) 2003-2007 Jocelyn Mayer
5
 *
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 * 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.
23
 */
P
pbrook 已提交
24 25 26 27 28 29 30 31
#include "hw.h"
#include "nvram.h"
#include "pc.h"
#include "fdc.h"
#include "net.h"
#include "sysemu.h"
#include "isa.h"
#include "pci.h"
32
#include "pci_host.h"
P
pbrook 已提交
33 34
#include "ppc.h"
#include "boards.h"
B
blueswir1 已提交
35
#include "qemu-log.h"
G
Gerd Hoffmann 已提交
36
#include "ide.h"
B
Blue Swirl 已提交
37
#include "loader.h"
38
#include "mc146818rtc.h"
B
Blue Swirl 已提交
39
#include "blockdev.h"
40
#include "exec-memory.h"
41

42
//#define HARD_DEBUG_PPC_IO
43
//#define DEBUG_PPC_IO
44

45 46 47
/* SMP is not enabled, for now */
#define MAX_CPUS 1

T
ths 已提交
48 49
#define MAX_IDE_BUS 2

50
#define BIOS_SIZE (1024 * 1024)
B
bellard 已提交
51 52 53
#define BIOS_FILENAME "ppc_rom.bin"
#define KERNEL_LOAD_ADDR 0x01000000
#define INITRD_LOAD_ADDR 0x01800000
B
bellard 已提交
54

55 56 57 58 59
#if defined (HARD_DEBUG_PPC_IO) && !defined (DEBUG_PPC_IO)
#define DEBUG_PPC_IO
#endif

#if defined (HARD_DEBUG_PPC_IO)
60
#define PPC_IO_DPRINTF(fmt, ...)                         \
61
do {                                                     \
62
    if (qemu_loglevel_mask(CPU_LOG_IOPORT)) {            \
63
        qemu_log("%s: " fmt, __func__ , ## __VA_ARGS__); \
64
    } else {                                             \
65
        printf("%s : " fmt, __func__ , ## __VA_ARGS__);  \
66 67 68
    }                                                    \
} while (0)
#elif defined (DEBUG_PPC_IO)
69 70
#define PPC_IO_DPRINTF(fmt, ...) \
qemu_log_mask(CPU_LOG_IOPORT, fmt, ## __VA_ARGS__)
71
#else
72
#define PPC_IO_DPRINTF(fmt, ...) do { } while (0)
73 74
#endif

B
bellard 已提交
75
/* Constants for devices init */
76 77 78 79 80 81 82 83
static const int ide_iobase[2] = { 0x1f0, 0x170 };
static const int ide_iobase2[2] = { 0x3f6, 0x376 };
static const int ide_irq[2] = { 13, 13 };

#define NE2000_NB_MAX 6

static uint32_t ne2000_io[NE2000_NB_MAX] = { 0x300, 0x320, 0x340, 0x360, 0x280, 0x380 };
static int ne2000_irq[NE2000_NB_MAX] = { 9, 10, 11, 3, 4, 5 };
84

B
bellard 已提交
85
/* ISA IO ports bridge */
86 87
#define PPC_IO_BASE 0x80000000

B
bellard 已提交
88 89
/* PCI intack register */
/* Read-only register (?) */
A
Avi Kivity 已提交
90 91
static void PPC_intack_write (void *opaque, target_phys_addr_t addr,
                              uint64_t value, unsigned size)
B
bellard 已提交
92
{
93
#if 0
A
Avi Kivity 已提交
94
    printf("%s: 0x" TARGET_FMT_plx " => 0x%08" PRIx64 "\n", __func__, addr,
95 96
           value);
#endif
B
bellard 已提交
97 98
}

A
Avi Kivity 已提交
99 100
static uint64_t PPC_intack_read(void *opaque, target_phys_addr_t addr,
                                unsigned size)
B
bellard 已提交
101 102 103
{
    uint32_t retval = 0;

104
    if ((addr & 0xf) == 0)
105
        retval = pic_read_irq(isa_pic);
106 107 108 109
#if 0
    printf("%s: 0x" TARGET_FMT_plx " <= %08" PRIx32 "\n", __func__, addr,
           retval);
#endif
B
bellard 已提交
110 111 112 113

    return retval;
}

A
Avi Kivity 已提交
114 115 116 117
static const MemoryRegionOps PPC_intack_ops = {
    .read = PPC_intack_read,
    .write = PPC_intack_write,
    .endianness = DEVICE_LITTLE_ENDIAN,
B
bellard 已提交
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
};

/* PowerPC control and status registers */
#if 0 // Not used
static struct {
    /* IDs */
    uint32_t veni_devi;
    uint32_t revi;
    /* Control and status */
    uint32_t gcsr;
    uint32_t xcfr;
    uint32_t ct32;
    uint32_t mcsr;
    /* General purpose registers */
    uint32_t gprg[6];
    /* Exceptions */
    uint32_t feen;
    uint32_t fest;
    uint32_t fema;
    uint32_t fecl;
    uint32_t eeen;
    uint32_t eest;
    uint32_t eecl;
    uint32_t eeint;
    uint32_t eemck0;
    uint32_t eemck1;
    /* Error diagnostic */
} XCSR;

147
static void PPC_XCSR_writeb (void *opaque,
A
Anthony Liguori 已提交
148
                             target_phys_addr_t addr, uint32_t value)
B
bellard 已提交
149
{
150 151
    printf("%s: 0x" TARGET_FMT_plx " => 0x%08" PRIx32 "\n", __func__, addr,
           value);
B
bellard 已提交
152 153
}

154
static void PPC_XCSR_writew (void *opaque,
A
Anthony Liguori 已提交
155
                             target_phys_addr_t addr, uint32_t value)
156
{
157 158
    printf("%s: 0x" TARGET_FMT_plx " => 0x%08" PRIx32 "\n", __func__, addr,
           value);
159 160
}

161
static void PPC_XCSR_writel (void *opaque,
A
Anthony Liguori 已提交
162
                             target_phys_addr_t addr, uint32_t value)
163
{
164 165
    printf("%s: 0x" TARGET_FMT_plx " => 0x%08" PRIx32 "\n", __func__, addr,
           value);
166 167
}

A
Anthony Liguori 已提交
168
static uint32_t PPC_XCSR_readb (void *opaque, target_phys_addr_t addr)
B
bellard 已提交
169 170
{
    uint32_t retval = 0;
171

172 173
    printf("%s: 0x" TARGET_FMT_plx " <= %08" PRIx32 "\n", __func__, addr,
           retval);
174

B
bellard 已提交
175 176 177
    return retval;
}

A
Anthony Liguori 已提交
178
static uint32_t PPC_XCSR_readw (void *opaque, target_phys_addr_t addr)
179
{
B
bellard 已提交
180 181
    uint32_t retval = 0;

182 183
    printf("%s: 0x" TARGET_FMT_plx " <= %08" PRIx32 "\n", __func__, addr,
           retval);
B
bellard 已提交
184 185

    return retval;
186 187
}

A
Anthony Liguori 已提交
188
static uint32_t PPC_XCSR_readl (void *opaque, target_phys_addr_t addr)
189 190 191
{
    uint32_t retval = 0;

192 193
    printf("%s: 0x" TARGET_FMT_plx " <= %08" PRIx32 "\n", __func__, addr,
           retval);
194 195 196 197

    return retval;
}

A
Avi Kivity 已提交
198 199 200 201 202 203
static const MemoryRegionOps PPC_XCSR_ops = {
    .old_mmio = {
        .read = { PPC_XCSR_readb, PPC_XCSR_readw, PPC_XCSR_readl, },
        .write = { PPC_XCSR_writeb, PPC_XCSR_writew, PPC_XCSR_writel, },
    },
    .endianness = DEVICE_LITTLE_ENDIAN,
204 205
};

B
bellard 已提交
206
#endif
207

B
bellard 已提交
208
/* Fake super-io ports for PREP platform (Intel 82378ZB) */
A
Anthony Liguori 已提交
209
typedef struct sysctrl_t {
J
j_mayer 已提交
210
    qemu_irq reset_irq;
211
    M48t59State *nvram;
B
bellard 已提交
212 213 214
    uint8_t state;
    uint8_t syscontrol;
    uint8_t fake_io[2];
B
bellard 已提交
215
    int contiguous_map;
B
bellard 已提交
216
    int endian;
A
Anthony Liguori 已提交
217
} sysctrl_t;
218

B
bellard 已提交
219 220
enum {
    STATE_HARDFILE = 0x01,
221 222
};

A
Anthony Liguori 已提交
223
static sysctrl_t *sysctrl;
224

225
static void PREP_io_write (void *opaque, uint32_t addr, uint32_t val)
226
{
A
Anthony Liguori 已提交
227
    sysctrl_t *sysctrl = opaque;
B
bellard 已提交
228

229 230
    PPC_IO_DPRINTF("0x%08" PRIx32 " => 0x%02" PRIx32 "\n", addr - PPC_IO_BASE,
                   val);
B
bellard 已提交
231
    sysctrl->fake_io[addr - 0x0398] = val;
232 233
}

234
static uint32_t PREP_io_read (void *opaque, uint32_t addr)
235
{
A
Anthony Liguori 已提交
236
    sysctrl_t *sysctrl = opaque;
237

238
    PPC_IO_DPRINTF("0x%08" PRIx32 " <= 0x%02" PRIx32 "\n", addr - PPC_IO_BASE,
B
bellard 已提交
239 240 241
                   sysctrl->fake_io[addr - 0x0398]);
    return sysctrl->fake_io[addr - 0x0398];
}
242

243
static void PREP_io_800_writeb (void *opaque, uint32_t addr, uint32_t val)
244
{
A
Anthony Liguori 已提交
245
    sysctrl_t *sysctrl = opaque;
B
bellard 已提交
246

247 248
    PPC_IO_DPRINTF("0x%08" PRIx32 " => 0x%02" PRIx32 "\n",
                   addr - PPC_IO_BASE, val);
249 250 251 252
    switch (addr) {
    case 0x0092:
        /* Special port 92 */
        /* Check soft reset asked */
B
bellard 已提交
253
        if (val & 0x01) {
J
j_mayer 已提交
254 255 256
            qemu_irq_raise(sysctrl->reset_irq);
        } else {
            qemu_irq_lower(sysctrl->reset_irq);
257 258
        }
        /* Check LE mode */
B
bellard 已提交
259
        if (val & 0x02) {
B
bellard 已提交
260 261 262
            sysctrl->endian = 1;
        } else {
            sysctrl->endian = 0;
263 264
        }
        break;
B
bellard 已提交
265 266 267 268 269 270 271 272 273
    case 0x0800:
        /* Motorola CPU configuration register : read-only */
        break;
    case 0x0802:
        /* Motorola base module feature register : read-only */
        break;
    case 0x0803:
        /* Motorola base module status register : read-only */
        break;
274
    case 0x0808:
B
bellard 已提交
275 276 277 278 279
        /* Hardfile light register */
        if (val & 1)
            sysctrl->state |= STATE_HARDFILE;
        else
            sysctrl->state &= ~STATE_HARDFILE;
280 281 282
        break;
    case 0x0810:
        /* Password protect 1 register */
B
bellard 已提交
283 284
        if (sysctrl->nvram != NULL)
            m48t59_toggle_lock(sysctrl->nvram, 1);
285 286 287
        break;
    case 0x0812:
        /* Password protect 2 register */
B
bellard 已提交
288 289
        if (sysctrl->nvram != NULL)
            m48t59_toggle_lock(sysctrl->nvram, 2);
290 291
        break;
    case 0x0814:
B
bellard 已提交
292
        /* L2 invalidate register */
B
bellard 已提交
293
        //        tlb_flush(first_cpu, 1);
294 295 296
        break;
    case 0x081C:
        /* system control register */
B
bellard 已提交
297
        sysctrl->syscontrol = val & 0x0F;
298 299 300
        break;
    case 0x0850:
        /* I/O map type register */
B
bellard 已提交
301
        sysctrl->contiguous_map = val & 0x01;
302 303
        break;
    default:
304 305
        printf("ERROR: unaffected IO port write: %04" PRIx32
               " => %02" PRIx32"\n", addr, val);
306 307 308 309
        break;
    }
}

310
static uint32_t PREP_io_800_readb (void *opaque, uint32_t addr)
311
{
A
Anthony Liguori 已提交
312
    sysctrl_t *sysctrl = opaque;
313 314 315 316 317
    uint32_t retval = 0xFF;

    switch (addr) {
    case 0x0092:
        /* Special port 92 */
B
bellard 已提交
318 319 320 321 322 323 324 325 326 327 328 329 330
        retval = 0x00;
        break;
    case 0x0800:
        /* Motorola CPU configuration register */
        retval = 0xEF; /* MPC750 */
        break;
    case 0x0802:
        /* Motorola Base module feature register */
        retval = 0xAD; /* No ESCC, PMC slot neither ethernet */
        break;
    case 0x0803:
        /* Motorola base module status register */
        retval = 0xE0; /* Standard MPC750 */
331 332 333 334 335 336 337 338
        break;
    case 0x080C:
        /* Equipment present register:
         *  no L2 cache
         *  no upgrade processor
         *  no cards in PCI slots
         *  SCSI fuse is bad
         */
B
bellard 已提交
339 340 341 342 343
        retval = 0x3C;
        break;
    case 0x0810:
        /* Motorola base module extended feature register */
        retval = 0x39; /* No USB, CF and PCI bridge. NVRAM present */
344
        break;
B
bellard 已提交
345 346 347
    case 0x0814:
        /* L2 invalidate: don't care */
        break;
348 349 350 351 352 353 354 355
    case 0x0818:
        /* Keylock */
        retval = 0x00;
        break;
    case 0x081C:
        /* system control register
         * 7 - 6 / 1 - 0: L2 cache enable
         */
B
bellard 已提交
356
        retval = sysctrl->syscontrol;
357 358 359 360 361 362 363
        break;
    case 0x0823:
        /* */
        retval = 0x03; /* no L2 cache */
        break;
    case 0x0850:
        /* I/O map type register */
B
bellard 已提交
364
        retval = sysctrl->contiguous_map;
365 366
        break;
    default:
367
        printf("ERROR: unaffected IO port: %04" PRIx32 " read\n", addr);
368 369
        break;
    }
370 371
    PPC_IO_DPRINTF("0x%08" PRIx32 " <= 0x%02" PRIx32 "\n",
                   addr - PPC_IO_BASE, retval);
372 373 374 375

    return retval;
}

A
Anthony Liguori 已提交
376 377
static inline target_phys_addr_t prep_IO_address(sysctrl_t *sysctrl,
                                                 target_phys_addr_t addr)
B
bellard 已提交
378 379 380 381 382 383 384 385 386 387 388 389
{
    if (sysctrl->contiguous_map == 0) {
        /* 64 KB contiguous space for IOs */
        addr &= 0xFFFF;
    } else {
        /* 8 MB non-contiguous space for IOs */
        addr = (addr & 0x1F) | ((addr & 0x007FFF000) >> 7);
    }

    return addr;
}

A
Anthony Liguori 已提交
390
static void PPC_prep_io_writeb (void *opaque, target_phys_addr_t addr,
B
bellard 已提交
391 392
                                uint32_t value)
{
A
Anthony Liguori 已提交
393
    sysctrl_t *sysctrl = opaque;
B
bellard 已提交
394 395

    addr = prep_IO_address(sysctrl, addr);
396
    cpu_outb(addr, value);
B
bellard 已提交
397 398
}

A
Anthony Liguori 已提交
399
static uint32_t PPC_prep_io_readb (void *opaque, target_phys_addr_t addr)
B
bellard 已提交
400
{
A
Anthony Liguori 已提交
401
    sysctrl_t *sysctrl = opaque;
B
bellard 已提交
402 403 404
    uint32_t ret;

    addr = prep_IO_address(sysctrl, addr);
405
    ret = cpu_inb(addr);
B
bellard 已提交
406 407 408 409

    return ret;
}

A
Anthony Liguori 已提交
410
static void PPC_prep_io_writew (void *opaque, target_phys_addr_t addr,
B
bellard 已提交
411 412
                                uint32_t value)
{
A
Anthony Liguori 已提交
413
    sysctrl_t *sysctrl = opaque;
B
bellard 已提交
414 415

    addr = prep_IO_address(sysctrl, addr);
416
    PPC_IO_DPRINTF("0x" TARGET_FMT_plx " => 0x%08" PRIx32 "\n", addr, value);
417
    cpu_outw(addr, value);
B
bellard 已提交
418 419
}

A
Anthony Liguori 已提交
420
static uint32_t PPC_prep_io_readw (void *opaque, target_phys_addr_t addr)
B
bellard 已提交
421
{
A
Anthony Liguori 已提交
422
    sysctrl_t *sysctrl = opaque;
B
bellard 已提交
423 424 425
    uint32_t ret;

    addr = prep_IO_address(sysctrl, addr);
426
    ret = cpu_inw(addr);
427
    PPC_IO_DPRINTF("0x" TARGET_FMT_plx " <= 0x%08" PRIx32 "\n", addr, ret);
B
bellard 已提交
428 429 430 431

    return ret;
}

A
Anthony Liguori 已提交
432
static void PPC_prep_io_writel (void *opaque, target_phys_addr_t addr,
B
bellard 已提交
433 434
                                uint32_t value)
{
A
Anthony Liguori 已提交
435
    sysctrl_t *sysctrl = opaque;
B
bellard 已提交
436 437

    addr = prep_IO_address(sysctrl, addr);
438
    PPC_IO_DPRINTF("0x" TARGET_FMT_plx " => 0x%08" PRIx32 "\n", addr, value);
439
    cpu_outl(addr, value);
B
bellard 已提交
440 441
}

A
Anthony Liguori 已提交
442
static uint32_t PPC_prep_io_readl (void *opaque, target_phys_addr_t addr)
B
bellard 已提交
443
{
A
Anthony Liguori 已提交
444
    sysctrl_t *sysctrl = opaque;
B
bellard 已提交
445 446 447
    uint32_t ret;

    addr = prep_IO_address(sysctrl, addr);
448
    ret = cpu_inl(addr);
449
    PPC_IO_DPRINTF("0x" TARGET_FMT_plx " <= 0x%08" PRIx32 "\n", addr, ret);
B
bellard 已提交
450 451 452 453

    return ret;
}

A
Avi Kivity 已提交
454 455 456 457 458 459
static const MemoryRegionOps PPC_prep_io_ops = {
    .old_mmio = {
        .read = { PPC_prep_io_readb, PPC_prep_io_readw, PPC_prep_io_readl },
        .write = { PPC_prep_io_writeb, PPC_prep_io_writew, PPC_prep_io_writel },
    },
    .endianness = DEVICE_LITTLE_ENDIAN,
B
bellard 已提交
460 461
};

B
bellard 已提交
462
#define NVRAM_SIZE        0x2000
463

B
Blue Swirl 已提交
464 465
static void cpu_request_exit(void *opaque, int irq, int level)
{
A
Andreas Färber 已提交
466
    CPUPPCState *env = cpu_single_env;
B
Blue Swirl 已提交
467 468 469 470 471 472

    if (env && level) {
        cpu_exit(env);
    }
}

473 474
static void ppc_prep_reset(void *opaque)
{
A
Andreas Färber 已提交
475
    CPUPPCState *env = opaque;
476 477 478 479

    cpu_state_reset(env);
}

480
/* PowerPC PREP hardware initialisation */
A
Anthony Liguori 已提交
481
static void ppc_prep_init (ram_addr_t ram_size,
482
                           const char *boot_device,
483
                           const char *kernel_filename,
484 485 486
                           const char *kernel_cmdline,
                           const char *initrd_filename,
                           const char *cpu_model)
487
{
A
Avi Kivity 已提交
488
    MemoryRegion *sysmem = get_system_memory();
A
Andreas Färber 已提交
489
    CPUPPCState *env = NULL;
P
Paul Brook 已提交
490
    char *filename;
A
Anthony Liguori 已提交
491
    nvram_t nvram;
492
    M48t59State *m48t59;
A
Avi Kivity 已提交
493 494 495 496 497
    MemoryRegion *PPC_io_memory = g_new(MemoryRegion, 1);
    MemoryRegion *intack = g_new(MemoryRegion, 1);
#if 0
    MemoryRegion *xcsr = g_new(MemoryRegion, 1);
#endif
B
bellard 已提交
498
    int linux_boot, i, nb_nics1, bios_size;
A
Avi Kivity 已提交
499 500
    MemoryRegion *ram = g_new(MemoryRegion, 1);
    MemoryRegion *bios = g_new(MemoryRegion, 1);
501 502
    uint32_t kernel_base, initrd_base;
    long kernel_size, initrd_size;
503 504 505
    DeviceState *dev;
    SysBusDevice *sys;
    PCIHostState *pcihost;
B
bellard 已提交
506
    PCIBus *pci_bus;
507
    PCIDevice *pci;
508
    ISABus *isa_bus;
B
Blue Swirl 已提交
509
    qemu_irq *cpu_exit_irq;
510
    int ppc_boot_device;
511
    DriveInfo *hd[MAX_IDE_BUS * MAX_IDE_DEVS];
G
Gerd Hoffmann 已提交
512
    DriveInfo *fd[MAX_FD];
B
bellard 已提交
513

514
    sysctrl = g_malloc0(sizeof(sysctrl_t));
515 516

    linux_boot = (kernel_filename != NULL);
J
j_mayer 已提交
517

B
bellard 已提交
518
    /* init CPUs */
519
    if (cpu_model == NULL)
G
Gerd Hoffmann 已提交
520
        cpu_model = "602";
521
    for (i = 0; i < smp_cpus; i++) {
B
bellard 已提交
522 523 524 525 526
        env = cpu_init(cpu_model);
        if (!env) {
            fprintf(stderr, "Unable to find PowerPC CPU definition\n");
            exit(1);
        }
527 528 529 530 531 532 533
        if (env->flags & POWERPC_FLAG_RTC_CLK) {
            /* POWER / PowerPC 601 RTC clock frequency is 7.8125 MHz */
            cpu_ppc_tb_init(env, 7812500UL);
        } else {
            /* Set time-base frequency to 100 Mhz */
            cpu_ppc_tb_init(env, 100UL * 1000UL * 1000UL);
        }
534
        qemu_register_reset(ppc_prep_reset, env);
535
    }
536 537

    /* allocate RAM */
538 539
    memory_region_init_ram(ram, "ppc_prep.ram", ram_size);
    vmstate_register_ram_global(ram);
A
Avi Kivity 已提交
540
    memory_region_add_subregion(sysmem, 0, ram);
B
blueswir1 已提交
541

B
bellard 已提交
542
    /* allocate and load BIOS */
543
    memory_region_init_ram(bios, "ppc_prep.bios", BIOS_SIZE);
544 545
    memory_region_set_readonly(bios, true);
    memory_region_add_subregion(sysmem, (uint32_t)(-BIOS_SIZE), bios);
546
    vmstate_register_ram_global(bios);
547 548
    if (bios_name == NULL)
        bios_name = BIOS_FILENAME;
P
Paul Brook 已提交
549 550 551 552 553 554
    filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
    if (filename) {
        bios_size = get_image_size(filename);
    } else {
        bios_size = -1;
    }
555
    if (bios_size > 0 && bios_size <= BIOS_SIZE) {
A
Anthony Liguori 已提交
556
        target_phys_addr_t bios_addr;
557 558
        bios_size = (bios_size + 0xfff) & ~0xfff;
        bios_addr = (uint32_t)(-bios_size);
P
Paul Brook 已提交
559
        bios_size = load_image_targphys(filename, bios_addr, bios_size);
560
    }
B
bellard 已提交
561
    if (bios_size < 0 || bios_size > BIOS_SIZE) {
P
Paul Brook 已提交
562 563 564
        hw_error("qemu: could not load PPC PREP bios '%s'\n", bios_name);
    }
    if (filename) {
565
        g_free(filename);
B
bellard 已提交
566
    }
567

568
    if (linux_boot) {
B
bellard 已提交
569
        kernel_base = KERNEL_LOAD_ADDR;
570
        /* now we can load the kernel */
571 572
        kernel_size = load_image_targphys(kernel_filename, kernel_base,
                                          ram_size - kernel_base);
B
bellard 已提交
573
        if (kernel_size < 0) {
P
Paul Brook 已提交
574
            hw_error("qemu: could not load kernel '%s'\n", kernel_filename);
575 576 577 578
            exit(1);
        }
        /* load initrd */
        if (initrd_filename) {
B
bellard 已提交
579
            initrd_base = INITRD_LOAD_ADDR;
580 581
            initrd_size = load_image_targphys(initrd_filename, initrd_base,
                                              ram_size - initrd_base);
582
            if (initrd_size < 0) {
P
Paul Brook 已提交
583
                hw_error("qemu: could not load initial ram disk '%s'\n",
J
j_mayer 已提交
584
                          initrd_filename);
585
            }
B
bellard 已提交
586 587 588
        } else {
            initrd_base = 0;
            initrd_size = 0;
589
        }
590
        ppc_boot_device = 'm';
591
    } else {
B
bellard 已提交
592 593 594 595
        kernel_base = 0;
        kernel_size = 0;
        initrd_base = 0;
        initrd_size = 0;
596 597
        ppc_boot_device = '\0';
        /* For now, OHW cannot boot from the network. */
J
j_mayer 已提交
598 599 600
        for (i = 0; boot_device[i] != '\0'; i++) {
            if (boot_device[i] >= 'a' && boot_device[i] <= 'f') {
                ppc_boot_device = boot_device[i];
601
                break;
J
j_mayer 已提交
602
            }
603 604 605 606 607
        }
        if (ppc_boot_device == '\0') {
            fprintf(stderr, "No valid boot device for Mac99 machine\n");
            exit(1);
        }
608 609
    }

610
    if (PPC_INPUT(env) != PPC_FLAGS_INPUT_6xx) {
P
Paul Brook 已提交
611
        hw_error("Only 6xx bus is supported on PREP machine\n");
612
    }
613 614 615 616 617

    dev = qdev_create(NULL, "raven-pcihost");
    sys = sysbus_from_qdev(dev);
    pcihost = DO_UPCAST(PCIHostState, busdev, sys);
    pcihost->address_space = get_system_memory();
618
    object_property_add_child(qdev_get_machine(), "raven", OBJECT(dev), NULL);
619
    qdev_init_nofail(dev);
620 621 622 623 624 625
    pci_bus = (PCIBus *)qdev_get_child_bus(dev, "pci.0");
    if (pci_bus == NULL) {
        fprintf(stderr, "Couldn't create PCI host controller.\n");
        exit(1);
    }

626 627 628 629 630 631 632 633 634 635 636 637
    /* PCI -> ISA bridge */
    pci = pci_create_simple(pci_bus, PCI_DEVFN(1, 0), "i82378");
    cpu_exit_irq = qemu_allocate_irqs(cpu_request_exit, NULL, 1);
    qdev_connect_gpio_out(&pci->qdev, 0,
                          first_cpu->irq_inputs[PPC6xx_INPUT_INT]);
    qdev_connect_gpio_out(&pci->qdev, 1, *cpu_exit_irq);
    sysbus_connect_irq(&pcihost->busdev, 0, qdev_get_gpio_in(&pci->qdev, 9));
    sysbus_connect_irq(&pcihost->busdev, 1, qdev_get_gpio_in(&pci->qdev, 11));
    sysbus_connect_irq(&pcihost->busdev, 2, qdev_get_gpio_in(&pci->qdev, 9));
    sysbus_connect_irq(&pcihost->busdev, 3, qdev_get_gpio_in(&pci->qdev, 11));
    isa_bus = DO_UPCAST(ISABus, qbus, qdev_get_child_bus(&pci->qdev, "isa.0"));

B
bellard 已提交
638
    /* Register 8 MB of ISA IO space (needed for non-contiguous map) */
A
Avi Kivity 已提交
639 640 641
    memory_region_init_io(PPC_io_memory, &PPC_prep_io_ops, sysctrl,
                          "ppc-io", 0x00800000);
    memory_region_add_subregion(sysmem, 0x80000000, PPC_io_memory);
B
bellard 已提交
642

643
    /* init basic PC hardware */
G
Gerd Hoffmann 已提交
644
    pci_vga_init(pci_bus);
645

G
Gerd Hoffmann 已提交
646
    if (serial_hds[0])
647
        serial_isa_init(isa_bus, 0, serial_hds[0]);
648 649 650 651
    nb_nics1 = nb_nics;
    if (nb_nics1 > NE2000_NB_MAX)
        nb_nics1 = NE2000_NB_MAX;
    for(i = 0; i < nb_nics1; i++) {
652
        if (nd_table[i].model == NULL) {
653
	    nd_table[i].model = g_strdup("ne2k_isa");
654 655
        }
        if (strcmp(nd_table[i].model, "ne2k_isa") == 0) {
656 657
            isa_ne2000_init(isa_bus, ne2000_io[i], ne2000_irq[i],
                            &nd_table[i]);
658
        } else {
659
            pci_nic_init_nofail(&nd_table[i], "ne2k_pci", NULL);
660
        }
661 662
    }

663
    ide_drive_get(hd, MAX_IDE_BUS);
664
    for(i = 0; i < MAX_IDE_BUS; i++) {
665
        isa_ide_init(isa_bus, ide_iobase[i], ide_iobase2[i], ide_irq[i],
T
ths 已提交
666 667
                     hd[2 * i],
		     hd[2 * i + 1]);
668
    }
669
    isa_create_simple(isa_bus, "i8042");
B
Blue Swirl 已提交
670

671 672
    //    SB16_init();

T
ths 已提交
673
    for(i = 0; i < MAX_FD; i++) {
G
Gerd Hoffmann 已提交
674
        fd[i] = drive_get(IF_FLOPPY, 0, i);
T
ths 已提交
675
    }
676
    fdctrl_init_isa(isa_bus, fd);
677 678

    /* Register fake IO ports for PREP */
J
j_mayer 已提交
679
    sysctrl->reset_irq = first_cpu->irq_inputs[PPC6xx_INPUT_HRESET];
B
bellard 已提交
680 681
    register_ioport_read(0x398, 2, 1, &PREP_io_read, sysctrl);
    register_ioport_write(0x398, 2, 1, &PREP_io_write, sysctrl);
682
    /* System control ports */
B
bellard 已提交
683 684 685 686 687
    register_ioport_read(0x0092, 0x01, 1, &PREP_io_800_readb, sysctrl);
    register_ioport_write(0x0092, 0x01, 1, &PREP_io_800_writeb, sysctrl);
    register_ioport_read(0x0800, 0x52, 1, &PREP_io_800_readb, sysctrl);
    register_ioport_write(0x0800, 0x52, 1, &PREP_io_800_writeb, sysctrl);
    /* PCI intack location */
A
Avi Kivity 已提交
688 689
    memory_region_init_io(intack, &PPC_intack_ops, NULL, "ppc-intack", 4);
    memory_region_add_subregion(sysmem, 0xBFFFFFF0, intack);
B
bellard 已提交
690
    /* PowerPC control and status register group */
B
bellard 已提交
691
#if 0
A
Avi Kivity 已提交
692 693
    memory_region_init_io(xcsr, &PPC_XCSR_ops, NULL, "ppc-xcsr", 0x1000);
    memory_region_add_subregion(sysmem, 0xFEFF0000, xcsr);
B
bellard 已提交
694
#endif
695

P
pbrook 已提交
696
    if (usb_enabled) {
697
        pci_create_simple(pci_bus, -1, "pci-ohci");
P
pbrook 已提交
698 699
    }

A
Andreas Färber 已提交
700
    m48t59 = m48t59_init_isa(isa_bus, 0x0074, NVRAM_SIZE, 59);
J
j_mayer 已提交
701
    if (m48t59 == NULL)
B
bellard 已提交
702
        return;
J
j_mayer 已提交
703
    sysctrl->nvram = m48t59;
B
bellard 已提交
704 705

    /* Initialise NVRAM */
J
j_mayer 已提交
706 707 708
    nvram.opaque = m48t59;
    nvram.read_fn = &m48t59_read;
    nvram.write_fn = &m48t59_write;
709
    PPC_NVRAM_set_params(&nvram, NVRAM_SIZE, "PREP", ram_size, ppc_boot_device,
B
bellard 已提交
710
                         kernel_base, kernel_size,
B
bellard 已提交
711
                         kernel_cmdline,
B
bellard 已提交
712 713
                         initrd_base, initrd_size,
                         /* XXX: need an option to load a NVRAM image */
B
bellard 已提交
714 715
                         0,
                         graphic_width, graphic_height, graphic_depth);
B
bellard 已提交
716 717 718

    /* Special port to get debug messages from Open-Firmware */
    register_ioport_write(0x0F00, 4, 1, &PPC_debug_write, NULL);
719
}
B
bellard 已提交
720

721
static QEMUMachine prep_machine = {
722 723 724
    .name = "prep",
    .desc = "PowerPC PREP platform",
    .init = ppc_prep_init,
B
balrog 已提交
725
    .max_cpus = MAX_CPUS,
B
bellard 已提交
726
};
727 728 729 730 731 732 733

static void prep_machine_init(void)
{
    qemu_register_machine(&prep_machine);
}

machine_init(prep_machine_init);