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
39 #include "mc146818rtc.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 //static ISADevice *pit;
88 /* ISA IO ports bridge */
89 #define PPC_IO_BASE 0x80000000
92 /* Speaker port 0x61 */
93 static int speaker_data_on;
94 static int dummy_refresh_clock;
97 static void speaker_ioport_write (void *opaque, uint32_t addr, uint32_t val)
100 speaker_data_on = (val >> 1) & 1;
101 pit_set_gate(pit, 2, val & 1);
105 static uint32_t speaker_ioport_read (void *opaque, uint32_t addr)
109 out = pit_get_out(pit, 2, qemu_get_clock_ns(vm_clock));
110 dummy_refresh_clock ^= 1;
111 return (speaker_data_on << 1) | pit_get_gate(pit, 2) | (out << 5) |
112 (dummy_refresh_clock << 4);
117 /* PCI intack register */
118 /* Read-only register (?) */
119 static void _PPC_intack_write (void *opaque,
120 target_phys_addr_t addr, uint32_t value)
123 printf("%s: 0x" TARGET_FMT_plx " => 0x%08" PRIx32 "\n", __func__, addr,
128 static inline uint32_t _PPC_intack_read(target_phys_addr_t addr)
132 if ((addr & 0xf) == 0)
133 retval = pic_intack_read(isa_pic);
135 printf("%s: 0x" TARGET_FMT_plx " <= %08" PRIx32 "\n", __func__, addr,
142 static uint32_t PPC_intack_readb (void *opaque, target_phys_addr_t addr)
144 return _PPC_intack_read(addr);
147 static uint32_t PPC_intack_readw (void *opaque, target_phys_addr_t addr)
149 return _PPC_intack_read(addr);
152 static uint32_t PPC_intack_readl (void *opaque, target_phys_addr_t addr)
154 return _PPC_intack_read(addr);
157 static CPUWriteMemoryFunc * const PPC_intack_write[] = {
163 static CPUReadMemoryFunc * const PPC_intack_read[] = {
169 /* PowerPC control and status registers */
175 /* Control and status */
180 /* General purpose registers */
193 /* Error diagnostic */
196 static void PPC_XCSR_writeb (void *opaque,
197 target_phys_addr_t addr, uint32_t value)
199 printf("%s: 0x" TARGET_FMT_plx " => 0x%08" PRIx32 "\n", __func__, addr,
203 static void PPC_XCSR_writew (void *opaque,
204 target_phys_addr_t addr, uint32_t value)
206 printf("%s: 0x" TARGET_FMT_plx " => 0x%08" PRIx32 "\n", __func__, addr,
210 static void PPC_XCSR_writel (void *opaque,
211 target_phys_addr_t addr, uint32_t value)
213 printf("%s: 0x" TARGET_FMT_plx " => 0x%08" PRIx32 "\n", __func__, addr,
217 static uint32_t PPC_XCSR_readb (void *opaque, target_phys_addr_t addr)
221 printf("%s: 0x" TARGET_FMT_plx " <= %08" PRIx32 "\n", __func__, addr,
227 static uint32_t PPC_XCSR_readw (void *opaque, target_phys_addr_t addr)
231 printf("%s: 0x" TARGET_FMT_plx " <= %08" PRIx32 "\n", __func__, addr,
237 static uint32_t PPC_XCSR_readl (void *opaque, target_phys_addr_t addr)
241 printf("%s: 0x" TARGET_FMT_plx " <= %08" PRIx32 "\n", __func__, addr,
247 static CPUWriteMemoryFunc * const PPC_XCSR_write[] = {
253 static CPUReadMemoryFunc * const PPC_XCSR_read[] = {
260 /* Fake super-io ports for PREP platform (Intel 82378ZB) */
261 typedef struct sysctrl_t {
272 STATE_HARDFILE = 0x01,
275 static sysctrl_t *sysctrl;
277 static void PREP_io_write (void *opaque, uint32_t addr, uint32_t val)
279 sysctrl_t *sysctrl = opaque;
281 PPC_IO_DPRINTF("0x%08" PRIx32 " => 0x%02" PRIx32 "\n", addr - PPC_IO_BASE,
283 sysctrl->fake_io[addr - 0x0398] = val;
286 static uint32_t PREP_io_read (void *opaque, uint32_t addr)
288 sysctrl_t *sysctrl = opaque;
290 PPC_IO_DPRINTF("0x%08" PRIx32 " <= 0x%02" PRIx32 "\n", addr - PPC_IO_BASE,
291 sysctrl->fake_io[addr - 0x0398]);
292 return sysctrl->fake_io[addr - 0x0398];
295 static void PREP_io_800_writeb (void *opaque, uint32_t addr, uint32_t val)
297 sysctrl_t *sysctrl = opaque;
299 PPC_IO_DPRINTF("0x%08" PRIx32 " => 0x%02" PRIx32 "\n",
300 addr - PPC_IO_BASE, val);
303 /* Special port 92 */
304 /* Check soft reset asked */
306 qemu_irq_raise(sysctrl->reset_irq);
308 qemu_irq_lower(sysctrl->reset_irq);
318 /* Motorola CPU configuration register : read-only */
321 /* Motorola base module feature register : read-only */
324 /* Motorola base module status register : read-only */
327 /* Hardfile light register */
329 sysctrl->state |= STATE_HARDFILE;
331 sysctrl->state &= ~STATE_HARDFILE;
334 /* Password protect 1 register */
335 if (sysctrl->nvram != NULL)
336 m48t59_toggle_lock(sysctrl->nvram, 1);
339 /* Password protect 2 register */
340 if (sysctrl->nvram != NULL)
341 m48t59_toggle_lock(sysctrl->nvram, 2);
344 /* L2 invalidate register */
345 // tlb_flush(first_cpu, 1);
348 /* system control register */
349 sysctrl->syscontrol = val & 0x0F;
352 /* I/O map type register */
353 sysctrl->contiguous_map = val & 0x01;
356 printf("ERROR: unaffected IO port write: %04" PRIx32
357 " => %02" PRIx32"\n", addr, val);
362 static uint32_t PREP_io_800_readb (void *opaque, uint32_t addr)
364 sysctrl_t *sysctrl = opaque;
365 uint32_t retval = 0xFF;
369 /* Special port 92 */
373 /* Motorola CPU configuration register */
374 retval = 0xEF; /* MPC750 */
377 /* Motorola Base module feature register */
378 retval = 0xAD; /* No ESCC, PMC slot neither ethernet */
381 /* Motorola base module status register */
382 retval = 0xE0; /* Standard MPC750 */
385 /* Equipment present register:
387 * no upgrade processor
388 * no cards in PCI slots
394 /* Motorola base module extended feature register */
395 retval = 0x39; /* No USB, CF and PCI bridge. NVRAM present */
398 /* L2 invalidate: don't care */
405 /* system control register
406 * 7 - 6 / 1 - 0: L2 cache enable
408 retval = sysctrl->syscontrol;
412 retval = 0x03; /* no L2 cache */
415 /* I/O map type register */
416 retval = sysctrl->contiguous_map;
419 printf("ERROR: unaffected IO port: %04" PRIx32 " read\n", addr);
422 PPC_IO_DPRINTF("0x%08" PRIx32 " <= 0x%02" PRIx32 "\n",
423 addr - PPC_IO_BASE, retval);
428 static inline target_phys_addr_t prep_IO_address(sysctrl_t *sysctrl,
429 target_phys_addr_t addr)
431 if (sysctrl->contiguous_map == 0) {
432 /* 64 KB contiguous space for IOs */
435 /* 8 MB non-contiguous space for IOs */
436 addr = (addr & 0x1F) | ((addr & 0x007FFF000) >> 7);
442 static void PPC_prep_io_writeb (void *opaque, target_phys_addr_t addr,
445 sysctrl_t *sysctrl = opaque;
447 addr = prep_IO_address(sysctrl, addr);
448 cpu_outb(addr, value);
451 static uint32_t PPC_prep_io_readb (void *opaque, target_phys_addr_t addr)
453 sysctrl_t *sysctrl = opaque;
456 addr = prep_IO_address(sysctrl, addr);
462 static void PPC_prep_io_writew (void *opaque, target_phys_addr_t addr,
465 sysctrl_t *sysctrl = opaque;
467 addr = prep_IO_address(sysctrl, addr);
468 PPC_IO_DPRINTF("0x" TARGET_FMT_plx " => 0x%08" PRIx32 "\n", addr, value);
469 cpu_outw(addr, value);
472 static uint32_t PPC_prep_io_readw (void *opaque, target_phys_addr_t addr)
474 sysctrl_t *sysctrl = opaque;
477 addr = prep_IO_address(sysctrl, addr);
479 PPC_IO_DPRINTF("0x" TARGET_FMT_plx " <= 0x%08" PRIx32 "\n", addr, ret);
484 static void PPC_prep_io_writel (void *opaque, target_phys_addr_t addr,
487 sysctrl_t *sysctrl = opaque;
489 addr = prep_IO_address(sysctrl, addr);
490 PPC_IO_DPRINTF("0x" TARGET_FMT_plx " => 0x%08" PRIx32 "\n", addr, value);
491 cpu_outl(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);
501 PPC_IO_DPRINTF("0x" TARGET_FMT_plx " <= 0x%08" PRIx32 "\n", addr, ret);
506 static CPUWriteMemoryFunc * const PPC_prep_io_write[] = {
512 static CPUReadMemoryFunc * const PPC_prep_io_read[] = {
518 #define NVRAM_SIZE 0x2000
520 static void cpu_request_exit(void *opaque, int irq, int level)
522 CPUState *env = cpu_single_env;
529 /* PowerPC PREP hardware initialisation */
530 static void ppc_prep_init (ram_addr_t ram_size,
531 const char *boot_device,
532 const char *kernel_filename,
533 const char *kernel_cmdline,
534 const char *initrd_filename,
535 const char *cpu_model)
537 CPUState *env = NULL;
542 int linux_boot, i, nb_nics1, bios_size;
543 ram_addr_t ram_offset, bios_offset;
544 uint32_t kernel_base, initrd_base;
545 long kernel_size, initrd_size;
548 qemu_irq *cpu_exit_irq;
550 DriveInfo *hd[MAX_IDE_BUS * MAX_IDE_DEVS];
551 DriveInfo *fd[MAX_FD];
553 sysctrl = qemu_mallocz(sizeof(sysctrl_t));
555 linux_boot = (kernel_filename != NULL);
558 if (cpu_model == NULL)
560 for (i = 0; i < smp_cpus; i++) {
561 env = cpu_init(cpu_model);
563 fprintf(stderr, "Unable to find PowerPC CPU definition\n");
566 if (env->flags & POWERPC_FLAG_RTC_CLK) {
567 /* POWER / PowerPC 601 RTC clock frequency is 7.8125 MHz */
568 cpu_ppc_tb_init(env, 7812500UL);
570 /* Set time-base frequency to 100 Mhz */
571 cpu_ppc_tb_init(env, 100UL * 1000UL * 1000UL);
573 qemu_register_reset((QEMUResetHandler*)&cpu_reset, env);
577 ram_offset = qemu_ram_alloc(NULL, "ppc_prep.ram", ram_size);
578 cpu_register_physical_memory(0, ram_size, ram_offset);
580 /* allocate and load BIOS */
581 bios_offset = qemu_ram_alloc(NULL, "ppc_prep.bios", BIOS_SIZE);
582 if (bios_name == NULL)
583 bios_name = BIOS_FILENAME;
584 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
586 bios_size = get_image_size(filename);
590 if (bios_size > 0 && bios_size <= BIOS_SIZE) {
591 target_phys_addr_t bios_addr;
592 bios_size = (bios_size + 0xfff) & ~0xfff;
593 bios_addr = (uint32_t)(-bios_size);
594 cpu_register_physical_memory(bios_addr, bios_size,
595 bios_offset | IO_MEM_ROM);
596 bios_size = load_image_targphys(filename, bios_addr, bios_size);
598 if (bios_size < 0 || bios_size > BIOS_SIZE) {
599 hw_error("qemu: could not load PPC PREP bios '%s'\n", bios_name);
606 kernel_base = KERNEL_LOAD_ADDR;
607 /* now we can load the kernel */
608 kernel_size = load_image_targphys(kernel_filename, kernel_base,
609 ram_size - kernel_base);
610 if (kernel_size < 0) {
611 hw_error("qemu: could not load kernel '%s'\n", kernel_filename);
615 if (initrd_filename) {
616 initrd_base = INITRD_LOAD_ADDR;
617 initrd_size = load_image_targphys(initrd_filename, initrd_base,
618 ram_size - initrd_base);
619 if (initrd_size < 0) {
620 hw_error("qemu: could not load initial ram disk '%s'\n",
627 ppc_boot_device = 'm';
633 ppc_boot_device = '\0';
634 /* For now, OHW cannot boot from the network. */
635 for (i = 0; boot_device[i] != '\0'; i++) {
636 if (boot_device[i] >= 'a' && boot_device[i] <= 'f') {
637 ppc_boot_device = boot_device[i];
641 if (ppc_boot_device == '\0') {
642 fprintf(stderr, "No valid boot device for Mac99 machine\n");
647 isa_mem_base = 0xc0000000;
648 if (PPC_INPUT(env) != PPC_FLAGS_INPUT_6xx) {
649 hw_error("Only 6xx bus is supported on PREP machine\n");
651 i8259 = i8259_init(first_cpu->irq_inputs[PPC6xx_INPUT_INT]);
652 pci_bus = pci_prep_init(i8259, get_system_memory());
653 /* Hmm, prep has no pci-isa bridge ??? */
656 // pci_bus = i440fx_init();
657 /* Register 8 MB of ISA IO space (needed for non-contiguous map) */
658 PPC_io_memory = cpu_register_io_memory(PPC_prep_io_read,
659 PPC_prep_io_write, sysctrl,
660 DEVICE_LITTLE_ENDIAN);
661 cpu_register_physical_memory(0x80000000, 0x00800000, PPC_io_memory);
663 /* init basic PC hardware */
664 pci_vga_init(pci_bus);
665 // openpic = openpic_init(0x00000000, 0xF0000000, 1);
666 // pit = pit_init(0x40, 0);
667 rtc_init(2000, NULL);
670 serial_isa_init(0, serial_hds[0]);
672 if (nb_nics1 > NE2000_NB_MAX)
673 nb_nics1 = NE2000_NB_MAX;
674 for(i = 0; i < nb_nics1; i++) {
675 if (nd_table[i].model == NULL) {
676 nd_table[i].model = qemu_strdup("ne2k_isa");
678 if (strcmp(nd_table[i].model, "ne2k_isa") == 0) {
679 isa_ne2000_init(ne2000_io[i], ne2000_irq[i], &nd_table[i]);
681 pci_nic_init_nofail(&nd_table[i], "ne2k_pci", NULL);
685 ide_drive_get(hd, MAX_IDE_BUS);
686 for(i = 0; i < MAX_IDE_BUS; i++) {
687 isa_ide_init(ide_iobase[i], ide_iobase2[i], ide_irq[i],
691 isa_create_simple("i8042");
693 cpu_exit_irq = qemu_allocate_irqs(cpu_request_exit, NULL, 1);
694 DMA_init(1, cpu_exit_irq);
698 for(i = 0; i < MAX_FD; i++) {
699 fd[i] = drive_get(IF_FLOPPY, 0, i);
703 /* Register speaker port */
704 register_ioport_read(0x61, 1, 1, speaker_ioport_read, NULL);
705 register_ioport_write(0x61, 1, 1, speaker_ioport_write, NULL);
706 /* Register fake IO ports for PREP */
707 sysctrl->reset_irq = first_cpu->irq_inputs[PPC6xx_INPUT_HRESET];
708 register_ioport_read(0x398, 2, 1, &PREP_io_read, sysctrl);
709 register_ioport_write(0x398, 2, 1, &PREP_io_write, sysctrl);
710 /* System control ports */
711 register_ioport_read(0x0092, 0x01, 1, &PREP_io_800_readb, sysctrl);
712 register_ioport_write(0x0092, 0x01, 1, &PREP_io_800_writeb, sysctrl);
713 register_ioport_read(0x0800, 0x52, 1, &PREP_io_800_readb, sysctrl);
714 register_ioport_write(0x0800, 0x52, 1, &PREP_io_800_writeb, sysctrl);
715 /* PCI intack location */
716 PPC_io_memory = cpu_register_io_memory(PPC_intack_read,
717 PPC_intack_write, NULL,
718 DEVICE_LITTLE_ENDIAN);
719 cpu_register_physical_memory(0xBFFFFFF0, 0x4, PPC_io_memory);
720 /* PowerPC control and status register group */
722 PPC_io_memory = cpu_register_io_memory(PPC_XCSR_read, PPC_XCSR_write,
723 NULL, DEVICE_LITTLE_ENDIAN);
724 cpu_register_physical_memory(0xFEFF0000, 0x1000, PPC_io_memory);
728 usb_ohci_init_pci(pci_bus, -1);
731 m48t59 = m48t59_init(i8259[8], 0, 0x0074, NVRAM_SIZE, 59);
734 sysctrl->nvram = m48t59;
736 /* Initialise NVRAM */
737 nvram.opaque = m48t59;
738 nvram.read_fn = &m48t59_read;
739 nvram.write_fn = &m48t59_write;
740 PPC_NVRAM_set_params(&nvram, NVRAM_SIZE, "PREP", ram_size, ppc_boot_device,
741 kernel_base, kernel_size,
743 initrd_base, initrd_size,
744 /* XXX: need an option to load a NVRAM image */
746 graphic_width, graphic_height, graphic_depth);
748 /* Special port to get debug messages from Open-Firmware */
749 register_ioport_write(0x0F00, 4, 1, &PPC_debug_write, NULL);
752 static QEMUMachine prep_machine = {
754 .desc = "PowerPC PREP platform",
755 .init = ppc_prep_init,
756 .max_cpus = MAX_CPUS,
759 static void prep_machine_init(void)
761 qemu_register_machine(&prep_machine);
764 machine_init(prep_machine_init);