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
38 #include "mc146818rtc.h"
40 #include "arch_init.h"
41 #include "exec-memory.h"
43 //#define HARD_DEBUG_PPC_IO
44 //#define DEBUG_PPC_IO
46 /* SMP is not enabled, for now */
51 #define BIOS_SIZE (1024 * 1024)
52 #define BIOS_FILENAME "ppc_rom.bin"
53 #define KERNEL_LOAD_ADDR 0x01000000
54 #define INITRD_LOAD_ADDR 0x01800000
56 #if defined (HARD_DEBUG_PPC_IO) && !defined (DEBUG_PPC_IO)
60 #if defined (HARD_DEBUG_PPC_IO)
61 #define PPC_IO_DPRINTF(fmt, ...) \
63 if (qemu_loglevel_mask(CPU_LOG_IOPORT)) { \
64 qemu_log("%s: " fmt, __func__ , ## __VA_ARGS__); \
66 printf("%s : " fmt, __func__ , ## __VA_ARGS__); \
69 #elif defined (DEBUG_PPC_IO)
70 #define PPC_IO_DPRINTF(fmt, ...) \
71 qemu_log_mask(CPU_LOG_IOPORT, fmt, ## __VA_ARGS__)
73 #define PPC_IO_DPRINTF(fmt, ...) do { } while (0)
76 /* Constants for devices init */
77 static const int ide_iobase
[2] = { 0x1f0, 0x170 };
78 static const int ide_iobase2
[2] = { 0x3f6, 0x376 };
79 static const int ide_irq
[2] = { 13, 13 };
81 #define NE2000_NB_MAX 6
83 static uint32_t ne2000_io
[NE2000_NB_MAX
] = { 0x300, 0x320, 0x340, 0x360, 0x280, 0x380 };
84 static int ne2000_irq
[NE2000_NB_MAX
] = { 9, 10, 11, 3, 4, 5 };
86 /* ISA IO ports bridge */
87 #define PPC_IO_BASE 0x80000000
89 /* PowerPC control and status registers */
95 /* Control and status */
100 /* General purpose registers */
113 /* Error diagnostic */
116 static void PPC_XCSR_writeb (void *opaque
,
117 target_phys_addr_t addr
, uint32_t value
)
119 printf("%s: 0x" TARGET_FMT_plx
" => 0x%08" PRIx32
"\n", __func__
, addr
,
123 static void PPC_XCSR_writew (void *opaque
,
124 target_phys_addr_t addr
, uint32_t value
)
126 printf("%s: 0x" TARGET_FMT_plx
" => 0x%08" PRIx32
"\n", __func__
, addr
,
130 static void PPC_XCSR_writel (void *opaque
,
131 target_phys_addr_t addr
, uint32_t value
)
133 printf("%s: 0x" TARGET_FMT_plx
" => 0x%08" PRIx32
"\n", __func__
, addr
,
137 static uint32_t PPC_XCSR_readb (void *opaque
, target_phys_addr_t addr
)
141 printf("%s: 0x" TARGET_FMT_plx
" <= %08" PRIx32
"\n", __func__
, addr
,
147 static uint32_t PPC_XCSR_readw (void *opaque
, target_phys_addr_t addr
)
151 printf("%s: 0x" TARGET_FMT_plx
" <= %08" PRIx32
"\n", __func__
, addr
,
157 static uint32_t PPC_XCSR_readl (void *opaque
, target_phys_addr_t addr
)
161 printf("%s: 0x" TARGET_FMT_plx
" <= %08" PRIx32
"\n", __func__
, addr
,
167 static const MemoryRegionOps PPC_XCSR_ops
= {
169 .read
= { PPC_XCSR_readb
, PPC_XCSR_readw
, PPC_XCSR_readl
, },
170 .write
= { PPC_XCSR_writeb
, PPC_XCSR_writew
, PPC_XCSR_writel
, },
172 .endianness
= DEVICE_LITTLE_ENDIAN
,
177 /* Fake super-io ports for PREP platform (Intel 82378ZB) */
178 typedef struct sysctrl_t
{
189 STATE_HARDFILE
= 0x01,
192 static sysctrl_t
*sysctrl
;
194 static void PREP_io_write (void *opaque
, uint32_t addr
, uint32_t val
)
196 sysctrl_t
*sysctrl
= opaque
;
198 PPC_IO_DPRINTF("0x%08" PRIx32
" => 0x%02" PRIx32
"\n", addr
- PPC_IO_BASE
,
200 sysctrl
->fake_io
[addr
- 0x0398] = val
;
203 static uint32_t PREP_io_read (void *opaque
, uint32_t addr
)
205 sysctrl_t
*sysctrl
= opaque
;
207 PPC_IO_DPRINTF("0x%08" PRIx32
" <= 0x%02" PRIx32
"\n", addr
- PPC_IO_BASE
,
208 sysctrl
->fake_io
[addr
- 0x0398]);
209 return sysctrl
->fake_io
[addr
- 0x0398];
212 static void PREP_io_800_writeb (void *opaque
, uint32_t addr
, uint32_t val
)
214 sysctrl_t
*sysctrl
= opaque
;
216 PPC_IO_DPRINTF("0x%08" PRIx32
" => 0x%02" PRIx32
"\n",
217 addr
- PPC_IO_BASE
, val
);
220 /* Special port 92 */
221 /* Check soft reset asked */
223 qemu_irq_raise(sysctrl
->reset_irq
);
225 qemu_irq_lower(sysctrl
->reset_irq
);
235 /* Motorola CPU configuration register : read-only */
238 /* Motorola base module feature register : read-only */
241 /* Motorola base module status register : read-only */
244 /* Hardfile light register */
246 sysctrl
->state
|= STATE_HARDFILE
;
248 sysctrl
->state
&= ~STATE_HARDFILE
;
251 /* Password protect 1 register */
252 if (sysctrl
->nvram
!= NULL
)
253 m48t59_toggle_lock(sysctrl
->nvram
, 1);
256 /* Password protect 2 register */
257 if (sysctrl
->nvram
!= NULL
)
258 m48t59_toggle_lock(sysctrl
->nvram
, 2);
261 /* L2 invalidate register */
262 // tlb_flush(first_cpu, 1);
265 /* system control register */
266 sysctrl
->syscontrol
= val
& 0x0F;
269 /* I/O map type register */
270 sysctrl
->contiguous_map
= val
& 0x01;
273 printf("ERROR: unaffected IO port write: %04" PRIx32
274 " => %02" PRIx32
"\n", addr
, val
);
279 static uint32_t PREP_io_800_readb (void *opaque
, uint32_t addr
)
281 sysctrl_t
*sysctrl
= opaque
;
282 uint32_t retval
= 0xFF;
286 /* Special port 92 */
290 /* Motorola CPU configuration register */
291 retval
= 0xEF; /* MPC750 */
294 /* Motorola Base module feature register */
295 retval
= 0xAD; /* No ESCC, PMC slot neither ethernet */
298 /* Motorola base module status register */
299 retval
= 0xE0; /* Standard MPC750 */
302 /* Equipment present register:
304 * no upgrade processor
305 * no cards in PCI slots
311 /* Motorola base module extended feature register */
312 retval
= 0x39; /* No USB, CF and PCI bridge. NVRAM present */
315 /* L2 invalidate: don't care */
322 /* system control register
323 * 7 - 6 / 1 - 0: L2 cache enable
325 retval
= sysctrl
->syscontrol
;
329 retval
= 0x03; /* no L2 cache */
332 /* I/O map type register */
333 retval
= sysctrl
->contiguous_map
;
336 printf("ERROR: unaffected IO port: %04" PRIx32
" read\n", addr
);
339 PPC_IO_DPRINTF("0x%08" PRIx32
" <= 0x%02" PRIx32
"\n",
340 addr
- PPC_IO_BASE
, retval
);
345 static inline target_phys_addr_t
prep_IO_address(sysctrl_t
*sysctrl
,
346 target_phys_addr_t addr
)
348 if (sysctrl
->contiguous_map
== 0) {
349 /* 64 KB contiguous space for IOs */
352 /* 8 MB non-contiguous space for IOs */
353 addr
= (addr
& 0x1F) | ((addr
& 0x007FFF000) >> 7);
359 static void PPC_prep_io_writeb (void *opaque
, target_phys_addr_t addr
,
362 sysctrl_t
*sysctrl
= opaque
;
364 addr
= prep_IO_address(sysctrl
, addr
);
365 cpu_outb(addr
, value
);
368 static uint32_t PPC_prep_io_readb (void *opaque
, target_phys_addr_t addr
)
370 sysctrl_t
*sysctrl
= opaque
;
373 addr
= prep_IO_address(sysctrl
, addr
);
379 static void PPC_prep_io_writew (void *opaque
, target_phys_addr_t addr
,
382 sysctrl_t
*sysctrl
= opaque
;
384 addr
= prep_IO_address(sysctrl
, addr
);
385 PPC_IO_DPRINTF("0x" TARGET_FMT_plx
" => 0x%08" PRIx32
"\n", addr
, value
);
386 cpu_outw(addr
, value
);
389 static uint32_t PPC_prep_io_readw (void *opaque
, target_phys_addr_t addr
)
391 sysctrl_t
*sysctrl
= opaque
;
394 addr
= prep_IO_address(sysctrl
, addr
);
396 PPC_IO_DPRINTF("0x" TARGET_FMT_plx
" <= 0x%08" PRIx32
"\n", addr
, ret
);
401 static void PPC_prep_io_writel (void *opaque
, target_phys_addr_t addr
,
404 sysctrl_t
*sysctrl
= opaque
;
406 addr
= prep_IO_address(sysctrl
, addr
);
407 PPC_IO_DPRINTF("0x" TARGET_FMT_plx
" => 0x%08" PRIx32
"\n", addr
, value
);
408 cpu_outl(addr
, value
);
411 static uint32_t PPC_prep_io_readl (void *opaque
, target_phys_addr_t addr
)
413 sysctrl_t
*sysctrl
= opaque
;
416 addr
= prep_IO_address(sysctrl
, addr
);
418 PPC_IO_DPRINTF("0x" TARGET_FMT_plx
" <= 0x%08" PRIx32
"\n", addr
, ret
);
423 static const MemoryRegionOps PPC_prep_io_ops
= {
425 .read
= { PPC_prep_io_readb
, PPC_prep_io_readw
, PPC_prep_io_readl
},
426 .write
= { PPC_prep_io_writeb
, PPC_prep_io_writew
, PPC_prep_io_writel
},
428 .endianness
= DEVICE_LITTLE_ENDIAN
,
431 #define NVRAM_SIZE 0x2000
433 static void cpu_request_exit(void *opaque
, int irq
, int level
)
435 CPUPPCState
*env
= cpu_single_env
;
442 static void ppc_prep_reset(void *opaque
)
444 CPUPPCState
*env
= opaque
;
446 cpu_state_reset(env
);
449 /* PowerPC PREP hardware initialisation */
450 static void ppc_prep_init (ram_addr_t ram_size
,
451 const char *boot_device
,
452 const char *kernel_filename
,
453 const char *kernel_cmdline
,
454 const char *initrd_filename
,
455 const char *cpu_model
)
457 MemoryRegion
*sysmem
= get_system_memory();
458 CPUPPCState
*env
= NULL
;
462 MemoryRegion
*PPC_io_memory
= g_new(MemoryRegion
, 1);
464 MemoryRegion
*xcsr
= g_new(MemoryRegion
, 1);
466 int linux_boot
, i
, nb_nics1
, bios_size
;
467 MemoryRegion
*ram
= g_new(MemoryRegion
, 1);
468 MemoryRegion
*bios
= g_new(MemoryRegion
, 1);
469 uint32_t kernel_base
, initrd_base
;
470 long kernel_size
, initrd_size
;
473 PCIHostState
*pcihost
;
477 qemu_irq
*cpu_exit_irq
;
479 DriveInfo
*hd
[MAX_IDE_BUS
* MAX_IDE_DEVS
];
480 DriveInfo
*fd
[MAX_FD
];
482 sysctrl
= g_malloc0(sizeof(sysctrl_t
));
484 linux_boot
= (kernel_filename
!= NULL
);
487 if (cpu_model
== NULL
)
489 for (i
= 0; i
< smp_cpus
; i
++) {
490 env
= cpu_init(cpu_model
);
492 fprintf(stderr
, "Unable to find PowerPC CPU definition\n");
495 if (env
->flags
& POWERPC_FLAG_RTC_CLK
) {
496 /* POWER / PowerPC 601 RTC clock frequency is 7.8125 MHz */
497 cpu_ppc_tb_init(env
, 7812500UL);
499 /* Set time-base frequency to 100 Mhz */
500 cpu_ppc_tb_init(env
, 100UL * 1000UL * 1000UL);
502 qemu_register_reset(ppc_prep_reset
, env
);
506 memory_region_init_ram(ram
, "ppc_prep.ram", ram_size
);
507 vmstate_register_ram_global(ram
);
508 memory_region_add_subregion(sysmem
, 0, ram
);
510 /* allocate and load BIOS */
511 memory_region_init_ram(bios
, "ppc_prep.bios", BIOS_SIZE
);
512 memory_region_set_readonly(bios
, true);
513 memory_region_add_subregion(sysmem
, (uint32_t)(-BIOS_SIZE
), bios
);
514 vmstate_register_ram_global(bios
);
515 if (bios_name
== NULL
)
516 bios_name
= BIOS_FILENAME
;
517 filename
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, bios_name
);
519 bios_size
= get_image_size(filename
);
523 if (bios_size
> 0 && bios_size
<= BIOS_SIZE
) {
524 target_phys_addr_t bios_addr
;
525 bios_size
= (bios_size
+ 0xfff) & ~0xfff;
526 bios_addr
= (uint32_t)(-bios_size
);
527 bios_size
= load_image_targphys(filename
, bios_addr
, bios_size
);
529 if (bios_size
< 0 || bios_size
> BIOS_SIZE
) {
530 hw_error("qemu: could not load PPC PREP bios '%s'\n", bios_name
);
537 kernel_base
= KERNEL_LOAD_ADDR
;
538 /* now we can load the kernel */
539 kernel_size
= load_image_targphys(kernel_filename
, kernel_base
,
540 ram_size
- kernel_base
);
541 if (kernel_size
< 0) {
542 hw_error("qemu: could not load kernel '%s'\n", kernel_filename
);
546 if (initrd_filename
) {
547 initrd_base
= INITRD_LOAD_ADDR
;
548 initrd_size
= load_image_targphys(initrd_filename
, initrd_base
,
549 ram_size
- initrd_base
);
550 if (initrd_size
< 0) {
551 hw_error("qemu: could not load initial ram disk '%s'\n",
558 ppc_boot_device
= 'm';
564 ppc_boot_device
= '\0';
565 /* For now, OHW cannot boot from the network. */
566 for (i
= 0; boot_device
[i
] != '\0'; i
++) {
567 if (boot_device
[i
] >= 'a' && boot_device
[i
] <= 'f') {
568 ppc_boot_device
= boot_device
[i
];
572 if (ppc_boot_device
== '\0') {
573 fprintf(stderr
, "No valid boot device for Mac99 machine\n");
578 if (PPC_INPUT(env
) != PPC_FLAGS_INPUT_6xx
) {
579 hw_error("Only 6xx bus is supported on PREP machine\n");
582 dev
= qdev_create(NULL
, "raven-pcihost");
583 sys
= sysbus_from_qdev(dev
);
584 pcihost
= DO_UPCAST(PCIHostState
, busdev
, sys
);
585 pcihost
->address_space
= get_system_memory();
586 object_property_add_child(qdev_get_machine(), "raven", OBJECT(dev
), NULL
);
587 qdev_init_nofail(dev
);
588 pci_bus
= (PCIBus
*)qdev_get_child_bus(dev
, "pci.0");
589 if (pci_bus
== NULL
) {
590 fprintf(stderr
, "Couldn't create PCI host controller.\n");
594 /* PCI -> ISA bridge */
595 pci
= pci_create_simple(pci_bus
, PCI_DEVFN(1, 0), "i82378");
596 cpu_exit_irq
= qemu_allocate_irqs(cpu_request_exit
, NULL
, 1);
597 qdev_connect_gpio_out(&pci
->qdev
, 0,
598 first_cpu
->irq_inputs
[PPC6xx_INPUT_INT
]);
599 qdev_connect_gpio_out(&pci
->qdev
, 1, *cpu_exit_irq
);
600 sysbus_connect_irq(&pcihost
->busdev
, 0, qdev_get_gpio_in(&pci
->qdev
, 9));
601 sysbus_connect_irq(&pcihost
->busdev
, 1, qdev_get_gpio_in(&pci
->qdev
, 11));
602 sysbus_connect_irq(&pcihost
->busdev
, 2, qdev_get_gpio_in(&pci
->qdev
, 9));
603 sysbus_connect_irq(&pcihost
->busdev
, 3, qdev_get_gpio_in(&pci
->qdev
, 11));
604 isa_bus
= DO_UPCAST(ISABus
, qbus
, qdev_get_child_bus(&pci
->qdev
, "isa.0"));
606 /* Register 8 MB of ISA IO space (needed for non-contiguous map) */
607 memory_region_init_io(PPC_io_memory
, &PPC_prep_io_ops
, sysctrl
,
608 "ppc-io", 0x00800000);
609 memory_region_add_subregion(sysmem
, 0x80000000, PPC_io_memory
);
611 /* init basic PC hardware */
612 pci_vga_init(pci_bus
);
615 serial_isa_init(isa_bus
, 0, serial_hds
[0]);
617 if (nb_nics1
> NE2000_NB_MAX
)
618 nb_nics1
= NE2000_NB_MAX
;
619 for(i
= 0; i
< nb_nics1
; i
++) {
620 if (nd_table
[i
].model
== NULL
) {
621 nd_table
[i
].model
= g_strdup("ne2k_isa");
623 if (strcmp(nd_table
[i
].model
, "ne2k_isa") == 0) {
624 isa_ne2000_init(isa_bus
, ne2000_io
[i
], ne2000_irq
[i
],
627 pci_nic_init_nofail(&nd_table
[i
], "ne2k_pci", NULL
);
631 ide_drive_get(hd
, MAX_IDE_BUS
);
632 for(i
= 0; i
< MAX_IDE_BUS
; i
++) {
633 isa_ide_init(isa_bus
, ide_iobase
[i
], ide_iobase2
[i
], ide_irq
[i
],
637 isa_create_simple(isa_bus
, "i8042");
641 for(i
= 0; i
< MAX_FD
; i
++) {
642 fd
[i
] = drive_get(IF_FLOPPY
, 0, i
);
644 fdctrl_init_isa(isa_bus
, fd
);
646 /* Register fake IO ports for PREP */
647 sysctrl
->reset_irq
= first_cpu
->irq_inputs
[PPC6xx_INPUT_HRESET
];
648 register_ioport_read(0x398, 2, 1, &PREP_io_read
, sysctrl
);
649 register_ioport_write(0x398, 2, 1, &PREP_io_write
, sysctrl
);
650 /* System control ports */
651 register_ioport_read(0x0092, 0x01, 1, &PREP_io_800_readb
, sysctrl
);
652 register_ioport_write(0x0092, 0x01, 1, &PREP_io_800_writeb
, sysctrl
);
653 register_ioport_read(0x0800, 0x52, 1, &PREP_io_800_readb
, sysctrl
);
654 register_ioport_write(0x0800, 0x52, 1, &PREP_io_800_writeb
, sysctrl
);
655 /* PowerPC control and status register group */
657 memory_region_init_io(xcsr
, &PPC_XCSR_ops
, NULL
, "ppc-xcsr", 0x1000);
658 memory_region_add_subregion(sysmem
, 0xFEFF0000, xcsr
);
662 pci_create_simple(pci_bus
, -1, "pci-ohci");
665 m48t59
= m48t59_init_isa(isa_bus
, 0x0074, NVRAM_SIZE
, 59);
668 sysctrl
->nvram
= m48t59
;
670 /* Initialise NVRAM */
671 nvram
.opaque
= m48t59
;
672 nvram
.read_fn
= &m48t59_read
;
673 nvram
.write_fn
= &m48t59_write
;
674 PPC_NVRAM_set_params(&nvram
, NVRAM_SIZE
, "PREP", ram_size
, ppc_boot_device
,
675 kernel_base
, kernel_size
,
677 initrd_base
, initrd_size
,
678 /* XXX: need an option to load a NVRAM image */
680 graphic_width
, graphic_height
, graphic_depth
);
682 /* Special port to get debug messages from Open-Firmware */
683 register_ioport_write(0x0F00, 4, 1, &PPC_debug_write
, NULL
);
685 /* Initialize audio subsystem */
686 audio_init(isa_bus
, pci_bus
);
689 static QEMUMachine prep_machine
= {
691 .desc
= "PowerPC PREP platform",
692 .init
= ppc_prep_init
,
693 .max_cpus
= MAX_CPUS
,
696 static void prep_machine_init(void)
698 qemu_register_machine(&prep_machine
);
701 machine_init(prep_machine_init
);