2 * QEMU PPC PREP hardware System Emulator
4 * Copyright (c) 2003-2007 Jocelyn Mayer
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 //#define HARD_DEBUG_PPC_IO
27 //#define DEBUG_PPC_IO
29 /* SMP is not enabled, for now */
32 #define BIOS_FILENAME "ppc_rom.bin"
33 #define KERNEL_LOAD_ADDR 0x01000000
34 #define INITRD_LOAD_ADDR 0x01800000
39 #if defined (HARD_DEBUG_PPC_IO) && !defined (DEBUG_PPC_IO)
43 #if defined (HARD_DEBUG_PPC_IO)
44 #define PPC_IO_DPRINTF(fmt, args...) \
46 if (loglevel & CPU_LOG_IOPORT) { \
47 fprintf(logfile, "%s: " fmt, __func__ , ##args); \
49 printf("%s : " fmt, __func__ , ##args); \
52 #elif defined (DEBUG_PPC_IO)
53 #define PPC_IO_DPRINTF(fmt, args...) \
55 if (loglevel & CPU_LOG_IOPORT) { \
56 fprintf(logfile, "%s: " fmt, __func__ , ##args); \
60 #define PPC_IO_DPRINTF(fmt, args...) do { } while (0)
63 /* Constants for devices init */
64 static const int ide_iobase[2] = { 0x1f0, 0x170 };
65 static const int ide_iobase2[2] = { 0x3f6, 0x376 };
66 static const int ide_irq[2] = { 13, 13 };
68 #define NE2000_NB_MAX 6
70 static uint32_t ne2000_io[NE2000_NB_MAX] = { 0x300, 0x320, 0x340, 0x360, 0x280, 0x380 };
71 static int ne2000_irq[NE2000_NB_MAX] = { 9, 10, 11, 3, 4, 5 };
73 //static PITState *pit;
75 /* ISA IO ports bridge */
76 #define PPC_IO_BASE 0x80000000
78 /* Speaker port 0x61 */
80 int dummy_refresh_clock;
82 static void speaker_ioport_write (void *opaque, uint32_t addr, uint32_t val)
85 speaker_data_on = (val >> 1) & 1;
86 pit_set_gate(pit, 2, val & 1);
90 static uint32_t speaker_ioport_read (void *opaque, uint32_t addr)
94 out = pit_get_out(pit, 2, qemu_get_clock(vm_clock));
95 dummy_refresh_clock ^= 1;
96 return (speaker_data_on << 1) | pit_get_gate(pit, 2) | (out << 5) |
97 (dummy_refresh_clock << 4);
102 /* PCI intack register */
103 /* Read-only register (?) */
104 static void _PPC_intack_write (void *opaque,
105 target_phys_addr_t addr, uint32_t value)
107 // printf("%s: 0x%08x => 0x%08x\n", __func__, addr, value);
110 static always_inline uint32_t _PPC_intack_read (target_phys_addr_t addr)
114 if (addr == 0xBFFFFFF0)
115 retval = pic_intack_read(isa_pic);
116 // printf("%s: 0x%08x <= %d\n", __func__, addr, retval);
121 static uint32_t PPC_intack_readb (void *opaque, target_phys_addr_t addr)
123 return _PPC_intack_read(addr);
126 static uint32_t PPC_intack_readw (void *opaque, target_phys_addr_t addr)
128 #ifdef TARGET_WORDS_BIGENDIAN
129 return bswap16(_PPC_intack_read(addr));
131 return _PPC_intack_read(addr);
135 static uint32_t PPC_intack_readl (void *opaque, target_phys_addr_t addr)
137 #ifdef TARGET_WORDS_BIGENDIAN
138 return bswap32(_PPC_intack_read(addr));
140 return _PPC_intack_read(addr);
144 static CPUWriteMemoryFunc *PPC_intack_write[] = {
150 static CPUReadMemoryFunc *PPC_intack_read[] = {
156 /* PowerPC control and status registers */
162 /* Control and status */
167 /* General purpose registers */
180 /* Error diagnostic */
183 static void PPC_XCSR_writeb (void *opaque,
184 target_phys_addr_t addr, uint32_t value)
186 printf("%s: 0x%08lx => 0x%08x\n", __func__, (long)addr, value);
189 static void PPC_XCSR_writew (void *opaque,
190 target_phys_addr_t addr, uint32_t value)
192 #ifdef TARGET_WORDS_BIGENDIAN
193 value = bswap16(value);
195 printf("%s: 0x%08lx => 0x%08x\n", __func__, (long)addr, value);
198 static void PPC_XCSR_writel (void *opaque,
199 target_phys_addr_t addr, uint32_t value)
201 #ifdef TARGET_WORDS_BIGENDIAN
202 value = bswap32(value);
204 printf("%s: 0x%08lx => 0x%08x\n", __func__, (long)addr, value);
207 static uint32_t PPC_XCSR_readb (void *opaque, target_phys_addr_t addr)
211 printf("%s: 0x%08lx <= %d\n", __func__, (long)addr, retval);
216 static uint32_t PPC_XCSR_readw (void *opaque, target_phys_addr_t addr)
220 printf("%s: 0x%08lx <= %d\n", __func__, (long)addr, retval);
221 #ifdef TARGET_WORDS_BIGENDIAN
222 retval = bswap16(retval);
228 static uint32_t PPC_XCSR_readl (void *opaque, target_phys_addr_t addr)
232 printf("%s: 0x%08lx <= %d\n", __func__, (long)addr, retval);
233 #ifdef TARGET_WORDS_BIGENDIAN
234 retval = bswap32(retval);
240 static CPUWriteMemoryFunc *PPC_XCSR_write[] = {
246 static CPUReadMemoryFunc *PPC_XCSR_read[] = {
253 /* Fake super-io ports for PREP platform (Intel 82378ZB) */
254 typedef struct sysctrl_t {
265 STATE_HARDFILE = 0x01,
268 static sysctrl_t *sysctrl;
270 static void PREP_io_write (void *opaque, uint32_t addr, uint32_t val)
272 sysctrl_t *sysctrl = opaque;
274 PPC_IO_DPRINTF("0x%08lx => 0x%08x\n", (long)addr - PPC_IO_BASE, val);
275 sysctrl->fake_io[addr - 0x0398] = val;
278 static uint32_t PREP_io_read (void *opaque, uint32_t addr)
280 sysctrl_t *sysctrl = opaque;
282 PPC_IO_DPRINTF("0x%08lx <= 0x%08x\n", (long)addr - PPC_IO_BASE,
283 sysctrl->fake_io[addr - 0x0398]);
284 return sysctrl->fake_io[addr - 0x0398];
287 static void PREP_io_800_writeb (void *opaque, uint32_t addr, uint32_t val)
289 sysctrl_t *sysctrl = opaque;
291 PPC_IO_DPRINTF("0x%08lx => 0x%08x\n", (long)addr - PPC_IO_BASE, val);
294 /* Special port 92 */
295 /* Check soft reset asked */
297 qemu_irq_raise(sysctrl->reset_irq);
299 qemu_irq_lower(sysctrl->reset_irq);
309 /* Motorola CPU configuration register : read-only */
312 /* Motorola base module feature register : read-only */
315 /* Motorola base module status register : read-only */
318 /* Hardfile light register */
320 sysctrl->state |= STATE_HARDFILE;
322 sysctrl->state &= ~STATE_HARDFILE;
325 /* Password protect 1 register */
326 if (sysctrl->nvram != NULL)
327 m48t59_toggle_lock(sysctrl->nvram, 1);
330 /* Password protect 2 register */
331 if (sysctrl->nvram != NULL)
332 m48t59_toggle_lock(sysctrl->nvram, 2);
335 /* L2 invalidate register */
336 // tlb_flush(first_cpu, 1);
339 /* system control register */
340 sysctrl->syscontrol = val & 0x0F;
343 /* I/O map type register */
344 sysctrl->contiguous_map = val & 0x01;
347 printf("ERROR: unaffected IO port write: %04lx => %02x\n",
353 static uint32_t PREP_io_800_readb (void *opaque, uint32_t addr)
355 sysctrl_t *sysctrl = opaque;
356 uint32_t retval = 0xFF;
360 /* Special port 92 */
364 /* Motorola CPU configuration register */
365 retval = 0xEF; /* MPC750 */
368 /* Motorola Base module feature register */
369 retval = 0xAD; /* No ESCC, PMC slot neither ethernet */
372 /* Motorola base module status register */
373 retval = 0xE0; /* Standard MPC750 */
376 /* Equipment present register:
378 * no upgrade processor
379 * no cards in PCI slots
385 /* Motorola base module extended feature register */
386 retval = 0x39; /* No USB, CF and PCI bridge. NVRAM present */
389 /* L2 invalidate: don't care */
396 /* system control register
397 * 7 - 6 / 1 - 0: L2 cache enable
399 retval = sysctrl->syscontrol;
403 retval = 0x03; /* no L2 cache */
406 /* I/O map type register */
407 retval = sysctrl->contiguous_map;
410 printf("ERROR: unaffected IO port: %04lx read\n", (long)addr);
413 PPC_IO_DPRINTF("0x%08lx <= 0x%08x\n", (long)addr - PPC_IO_BASE, retval);
418 static always_inline target_phys_addr_t prep_IO_address (sysctrl_t *sysctrl,
422 if (sysctrl->contiguous_map == 0) {
423 /* 64 KB contiguous space for IOs */
426 /* 8 MB non-contiguous space for IOs */
427 addr = (addr & 0x1F) | ((addr & 0x007FFF000) >> 7);
433 static void PPC_prep_io_writeb (void *opaque, target_phys_addr_t addr,
436 sysctrl_t *sysctrl = opaque;
438 addr = prep_IO_address(sysctrl, addr);
439 cpu_outb(NULL, addr, value);
442 static uint32_t PPC_prep_io_readb (void *opaque, target_phys_addr_t addr)
444 sysctrl_t *sysctrl = opaque;
447 addr = prep_IO_address(sysctrl, addr);
448 ret = cpu_inb(NULL, addr);
453 static void PPC_prep_io_writew (void *opaque, target_phys_addr_t addr,
456 sysctrl_t *sysctrl = opaque;
458 addr = prep_IO_address(sysctrl, addr);
459 #ifdef TARGET_WORDS_BIGENDIAN
460 value = bswap16(value);
462 PPC_IO_DPRINTF("0x%08lx => 0x%08x\n", (long)addr, value);
463 cpu_outw(NULL, addr, value);
466 static uint32_t PPC_prep_io_readw (void *opaque, target_phys_addr_t addr)
468 sysctrl_t *sysctrl = opaque;
471 addr = prep_IO_address(sysctrl, addr);
472 ret = cpu_inw(NULL, addr);
473 #ifdef TARGET_WORDS_BIGENDIAN
476 PPC_IO_DPRINTF("0x%08lx <= 0x%08x\n", (long)addr, ret);
481 static void PPC_prep_io_writel (void *opaque, target_phys_addr_t addr,
484 sysctrl_t *sysctrl = opaque;
486 addr = prep_IO_address(sysctrl, addr);
487 #ifdef TARGET_WORDS_BIGENDIAN
488 value = bswap32(value);
490 PPC_IO_DPRINTF("0x%08lx => 0x%08x\n", (long)addr, value);
491 cpu_outl(NULL, addr, value);
494 static uint32_t PPC_prep_io_readl (void *opaque, target_phys_addr_t addr)
496 sysctrl_t *sysctrl = opaque;
499 addr = prep_IO_address(sysctrl, addr);
500 ret = cpu_inl(NULL, addr);
501 #ifdef TARGET_WORDS_BIGENDIAN
504 PPC_IO_DPRINTF("0x%08lx <= 0x%08x\n", (long)addr, ret);
509 CPUWriteMemoryFunc *PPC_prep_io_write[] = {
515 CPUReadMemoryFunc *PPC_prep_io_read[] = {
521 #define NVRAM_SIZE 0x2000
523 /* PowerPC PREP hardware initialisation */
524 static void ppc_prep_init (int ram_size, int vga_ram_size, const char *boot_device,
525 DisplayState *ds, const char **fd_filename,
526 int snapshot, const char *kernel_filename,
527 const char *kernel_cmdline,
528 const char *initrd_filename,
529 const char *cpu_model)
531 CPUState *env, *envs[MAX_CPUS];
536 int linux_boot, i, nb_nics1, bios_size;
537 unsigned long bios_offset;
538 uint32_t kernel_base, kernel_size, initrd_base, initrd_size;
541 int ppc_boot_device = boot_device[0];
543 sysctrl = qemu_mallocz(sizeof(sysctrl_t));
547 linux_boot = (kernel_filename != NULL);
550 if (cpu_model == NULL)
551 cpu_model = "default";
552 for (i = 0; i < smp_cpus; i++) {
553 env = cpu_init(cpu_model);
555 fprintf(stderr, "Unable to find PowerPC CPU definition\n");
558 /* Set time-base frequency to 100 Mhz */
559 cpu_ppc_tb_init(env, 100UL * 1000UL * 1000UL);
560 qemu_register_reset(&cpu_ppc_reset, env);
561 register_savevm("cpu", 0, 3, cpu_save, cpu_load, env);
566 cpu_register_physical_memory(0, ram_size, IO_MEM_RAM);
568 /* allocate and load BIOS */
569 bios_offset = ram_size + vga_ram_size;
570 if (bios_name == NULL)
571 bios_name = BIOS_FILENAME;
572 snprintf(buf, sizeof(buf), "%s/%s", bios_dir, bios_name);
573 bios_size = load_image(buf, phys_ram_base + bios_offset);
574 if (bios_size < 0 || bios_size > BIOS_SIZE) {
575 cpu_abort(env, "qemu: could not load PPC PREP bios '%s'\n", buf);
578 if (env->nip < 0xFFF80000 && bios_size < 0x00100000) {
579 cpu_abort(env, "PowerPC 601 / 620 / 970 need a 1MB BIOS\n");
581 bios_size = (bios_size + 0xfff) & ~0xfff;
582 cpu_register_physical_memory((uint32_t)(-bios_size),
583 bios_size, bios_offset | IO_MEM_ROM);
586 kernel_base = KERNEL_LOAD_ADDR;
587 /* now we can load the kernel */
588 kernel_size = load_image(kernel_filename, phys_ram_base + kernel_base);
589 if (kernel_size < 0) {
590 cpu_abort(env, "qemu: could not load kernel '%s'\n",
595 if (initrd_filename) {
596 initrd_base = INITRD_LOAD_ADDR;
597 initrd_size = load_image(initrd_filename,
598 phys_ram_base + initrd_base);
599 if (initrd_size < 0) {
600 cpu_abort(env, "qemu: could not load initial ram disk '%s'\n",
608 ppc_boot_device = 'm';
616 isa_mem_base = 0xc0000000;
617 if (PPC_INPUT(env) != PPC_FLAGS_INPUT_6xx) {
618 cpu_abort(env, "Only 6xx bus is supported on PREP machine\n");
621 i8259 = i8259_init(first_cpu->irq_inputs[PPC6xx_INPUT_INT]);
622 pci_bus = pci_prep_init(i8259);
623 // pci_bus = i440fx_init();
624 /* Register 8 MB of ISA IO space (needed for non-contiguous map) */
625 PPC_io_memory = cpu_register_io_memory(0, PPC_prep_io_read,
626 PPC_prep_io_write, sysctrl);
627 cpu_register_physical_memory(0x80000000, 0x00800000, PPC_io_memory);
629 /* init basic PC hardware */
630 pci_vga_init(pci_bus, ds, phys_ram_base + ram_size, ram_size,
632 // openpic = openpic_init(0x00000000, 0xF0000000, 1);
633 // pit = pit_init(0x40, i8259[0]);
634 rtc_init(0x70, i8259[8]);
636 serial_init(0x3f8, i8259[4], serial_hds[0]);
638 if (nb_nics1 > NE2000_NB_MAX)
639 nb_nics1 = NE2000_NB_MAX;
640 for(i = 0; i < nb_nics1; i++) {
641 if (nd_table[i].model == NULL
642 || strcmp(nd_table[i].model, "ne2k_isa") == 0) {
643 isa_ne2000_init(ne2000_io[i], i8259[ne2000_irq[i]], &nd_table[i]);
645 pci_nic_init(pci_bus, &nd_table[i], -1);
649 for(i = 0; i < 2; i++) {
650 isa_ide_init(ide_iobase[i], ide_iobase2[i], i8259[ide_irq[i]],
651 bs_table[2 * i], bs_table[2 * i + 1]);
653 i8042_init(i8259[1], i8259[12], 0x60);
658 fdctrl_init(i8259[6], 2, 0, 0x3f0, fd_table);
660 /* Register speaker port */
661 register_ioport_read(0x61, 1, 1, speaker_ioport_read, NULL);
662 register_ioport_write(0x61, 1, 1, speaker_ioport_write, NULL);
663 /* Register fake IO ports for PREP */
664 sysctrl->reset_irq = first_cpu->irq_inputs[PPC6xx_INPUT_HRESET];
665 register_ioport_read(0x398, 2, 1, &PREP_io_read, sysctrl);
666 register_ioport_write(0x398, 2, 1, &PREP_io_write, sysctrl);
667 /* System control ports */
668 register_ioport_read(0x0092, 0x01, 1, &PREP_io_800_readb, sysctrl);
669 register_ioport_write(0x0092, 0x01, 1, &PREP_io_800_writeb, sysctrl);
670 register_ioport_read(0x0800, 0x52, 1, &PREP_io_800_readb, sysctrl);
671 register_ioport_write(0x0800, 0x52, 1, &PREP_io_800_writeb, sysctrl);
672 /* PCI intack location */
673 PPC_io_memory = cpu_register_io_memory(0, PPC_intack_read,
674 PPC_intack_write, NULL);
675 cpu_register_physical_memory(0xBFFFFFF0, 0x4, PPC_io_memory);
676 /* PowerPC control and status register group */
678 PPC_io_memory = cpu_register_io_memory(0, PPC_XCSR_read, PPC_XCSR_write,
680 cpu_register_physical_memory(0xFEFF0000, 0x1000, PPC_io_memory);
684 usb_ohci_init_pci(pci_bus, 3, -1);
687 m48t59 = m48t59_init(i8259[8], 0, 0x0074, NVRAM_SIZE, 59);
690 sysctrl->nvram = m48t59;
692 /* Initialise NVRAM */
693 nvram.opaque = m48t59;
694 nvram.read_fn = &m48t59_read;
695 nvram.write_fn = &m48t59_write;
696 PPC_NVRAM_set_params(&nvram, NVRAM_SIZE, "PREP", ram_size, ppc_boot_device,
697 kernel_base, kernel_size,
699 initrd_base, initrd_size,
700 /* XXX: need an option to load a NVRAM image */
702 graphic_width, graphic_height, graphic_depth);
704 /* Special port to get debug messages from Open-Firmware */
705 register_ioport_write(0x0F00, 4, 1, &PPC_debug_write, NULL);
708 QEMUMachine prep_machine = {
710 "PowerPC PREP platform",