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 "exec-memory.h"
42 //#define HARD_DEBUG_PPC_IO
43 //#define DEBUG_PPC_IO
45 /* SMP is not enabled, for now */
50 #define BIOS_SIZE (1024 * 1024)
51 #define BIOS_FILENAME "ppc_rom.bin"
52 #define KERNEL_LOAD_ADDR 0x01000000
53 #define INITRD_LOAD_ADDR 0x01800000
55 #if defined (HARD_DEBUG_PPC_IO) && !defined (DEBUG_PPC_IO)
59 #if defined (HARD_DEBUG_PPC_IO)
60 #define PPC_IO_DPRINTF(fmt, ...) \
62 if (qemu_loglevel_mask(CPU_LOG_IOPORT)) { \
63 qemu_log("%s: " fmt, __func__ , ## __VA_ARGS__); \
65 printf("%s : " fmt, __func__ , ## __VA_ARGS__); \
68 #elif defined (DEBUG_PPC_IO)
69 #define PPC_IO_DPRINTF(fmt, ...) \
70 qemu_log_mask(CPU_LOG_IOPORT, fmt, ## __VA_ARGS__)
72 #define PPC_IO_DPRINTF(fmt, ...) do { } while (0)
75 /* Constants for devices init */
76 static const int ide_iobase
[2] = { 0x1f0, 0x170 };
77 static const int ide_iobase2
[2] = { 0x3f6, 0x376 };
78 static const int ide_irq
[2] = { 13, 13 };
80 #define NE2000_NB_MAX 6
82 static uint32_t ne2000_io
[NE2000_NB_MAX
] = { 0x300, 0x320, 0x340, 0x360, 0x280, 0x380 };
83 static int ne2000_irq
[NE2000_NB_MAX
] = { 9, 10, 11, 3, 4, 5 };
85 /* ISA IO ports bridge */
86 #define PPC_IO_BASE 0x80000000
88 /* PCI intack register */
89 /* Read-only register (?) */
90 static void PPC_intack_write (void *opaque
, target_phys_addr_t addr
,
91 uint64_t value
, unsigned size
)
94 printf("%s: 0x" TARGET_FMT_plx
" => 0x%08" PRIx64
"\n", __func__
, addr
,
99 static uint64_t PPC_intack_read(void *opaque
, target_phys_addr_t addr
,
104 if ((addr
& 0xf) == 0)
105 retval
= pic_read_irq(isa_pic
);
107 printf("%s: 0x" TARGET_FMT_plx
" <= %08" PRIx32
"\n", __func__
, addr
,
114 static const MemoryRegionOps PPC_intack_ops
= {
115 .read
= PPC_intack_read
,
116 .write
= PPC_intack_write
,
117 .endianness
= DEVICE_LITTLE_ENDIAN
,
120 /* PowerPC control and status registers */
126 /* Control and status */
131 /* General purpose registers */
144 /* Error diagnostic */
147 static void PPC_XCSR_writeb (void *opaque
,
148 target_phys_addr_t addr
, uint32_t value
)
150 printf("%s: 0x" TARGET_FMT_plx
" => 0x%08" PRIx32
"\n", __func__
, addr
,
154 static void PPC_XCSR_writew (void *opaque
,
155 target_phys_addr_t addr
, uint32_t value
)
157 printf("%s: 0x" TARGET_FMT_plx
" => 0x%08" PRIx32
"\n", __func__
, addr
,
161 static void PPC_XCSR_writel (void *opaque
,
162 target_phys_addr_t addr
, uint32_t value
)
164 printf("%s: 0x" TARGET_FMT_plx
" => 0x%08" PRIx32
"\n", __func__
, addr
,
168 static uint32_t PPC_XCSR_readb (void *opaque
, target_phys_addr_t addr
)
172 printf("%s: 0x" TARGET_FMT_plx
" <= %08" PRIx32
"\n", __func__
, addr
,
178 static uint32_t PPC_XCSR_readw (void *opaque
, target_phys_addr_t addr
)
182 printf("%s: 0x" TARGET_FMT_plx
" <= %08" PRIx32
"\n", __func__
, addr
,
188 static uint32_t PPC_XCSR_readl (void *opaque
, target_phys_addr_t addr
)
192 printf("%s: 0x" TARGET_FMT_plx
" <= %08" PRIx32
"\n", __func__
, addr
,
198 static const MemoryRegionOps PPC_XCSR_ops
= {
200 .read
= { PPC_XCSR_readb
, PPC_XCSR_readw
, PPC_XCSR_readl
, },
201 .write
= { PPC_XCSR_writeb
, PPC_XCSR_writew
, PPC_XCSR_writel
, },
203 .endianness
= DEVICE_LITTLE_ENDIAN
,
208 /* Fake super-io ports for PREP platform (Intel 82378ZB) */
209 typedef struct sysctrl_t
{
220 STATE_HARDFILE
= 0x01,
223 static sysctrl_t
*sysctrl
;
225 static void PREP_io_write (void *opaque
, uint32_t addr
, uint32_t val
)
227 sysctrl_t
*sysctrl
= opaque
;
229 PPC_IO_DPRINTF("0x%08" PRIx32
" => 0x%02" PRIx32
"\n", addr
- PPC_IO_BASE
,
231 sysctrl
->fake_io
[addr
- 0x0398] = val
;
234 static uint32_t PREP_io_read (void *opaque
, uint32_t addr
)
236 sysctrl_t
*sysctrl
= opaque
;
238 PPC_IO_DPRINTF("0x%08" PRIx32
" <= 0x%02" PRIx32
"\n", addr
- PPC_IO_BASE
,
239 sysctrl
->fake_io
[addr
- 0x0398]);
240 return sysctrl
->fake_io
[addr
- 0x0398];
243 static void PREP_io_800_writeb (void *opaque
, uint32_t addr
, uint32_t val
)
245 sysctrl_t
*sysctrl
= opaque
;
247 PPC_IO_DPRINTF("0x%08" PRIx32
" => 0x%02" PRIx32
"\n",
248 addr
- PPC_IO_BASE
, val
);
251 /* Special port 92 */
252 /* Check soft reset asked */
254 qemu_irq_raise(sysctrl
->reset_irq
);
256 qemu_irq_lower(sysctrl
->reset_irq
);
266 /* Motorola CPU configuration register : read-only */
269 /* Motorola base module feature register : read-only */
272 /* Motorola base module status register : read-only */
275 /* Hardfile light register */
277 sysctrl
->state
|= STATE_HARDFILE
;
279 sysctrl
->state
&= ~STATE_HARDFILE
;
282 /* Password protect 1 register */
283 if (sysctrl
->nvram
!= NULL
)
284 m48t59_toggle_lock(sysctrl
->nvram
, 1);
287 /* Password protect 2 register */
288 if (sysctrl
->nvram
!= NULL
)
289 m48t59_toggle_lock(sysctrl
->nvram
, 2);
292 /* L2 invalidate register */
293 // tlb_flush(first_cpu, 1);
296 /* system control register */
297 sysctrl
->syscontrol
= val
& 0x0F;
300 /* I/O map type register */
301 sysctrl
->contiguous_map
= val
& 0x01;
304 printf("ERROR: unaffected IO port write: %04" PRIx32
305 " => %02" PRIx32
"\n", addr
, val
);
310 static uint32_t PREP_io_800_readb (void *opaque
, uint32_t addr
)
312 sysctrl_t
*sysctrl
= opaque
;
313 uint32_t retval
= 0xFF;
317 /* Special port 92 */
321 /* Motorola CPU configuration register */
322 retval
= 0xEF; /* MPC750 */
325 /* Motorola Base module feature register */
326 retval
= 0xAD; /* No ESCC, PMC slot neither ethernet */
329 /* Motorola base module status register */
330 retval
= 0xE0; /* Standard MPC750 */
333 /* Equipment present register:
335 * no upgrade processor
336 * no cards in PCI slots
342 /* Motorola base module extended feature register */
343 retval
= 0x39; /* No USB, CF and PCI bridge. NVRAM present */
346 /* L2 invalidate: don't care */
353 /* system control register
354 * 7 - 6 / 1 - 0: L2 cache enable
356 retval
= sysctrl
->syscontrol
;
360 retval
= 0x03; /* no L2 cache */
363 /* I/O map type register */
364 retval
= sysctrl
->contiguous_map
;
367 printf("ERROR: unaffected IO port: %04" PRIx32
" read\n", addr
);
370 PPC_IO_DPRINTF("0x%08" PRIx32
" <= 0x%02" PRIx32
"\n",
371 addr
- PPC_IO_BASE
, retval
);
376 static inline target_phys_addr_t
prep_IO_address(sysctrl_t
*sysctrl
,
377 target_phys_addr_t addr
)
379 if (sysctrl
->contiguous_map
== 0) {
380 /* 64 KB contiguous space for IOs */
383 /* 8 MB non-contiguous space for IOs */
384 addr
= (addr
& 0x1F) | ((addr
& 0x007FFF000) >> 7);
390 static void PPC_prep_io_writeb (void *opaque
, target_phys_addr_t addr
,
393 sysctrl_t
*sysctrl
= opaque
;
395 addr
= prep_IO_address(sysctrl
, addr
);
396 cpu_outb(addr
, value
);
399 static uint32_t PPC_prep_io_readb (void *opaque
, target_phys_addr_t addr
)
401 sysctrl_t
*sysctrl
= opaque
;
404 addr
= prep_IO_address(sysctrl
, addr
);
410 static void PPC_prep_io_writew (void *opaque
, target_phys_addr_t addr
,
413 sysctrl_t
*sysctrl
= opaque
;
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
);
420 static uint32_t PPC_prep_io_readw (void *opaque
, target_phys_addr_t addr
)
422 sysctrl_t
*sysctrl
= opaque
;
425 addr
= prep_IO_address(sysctrl
, addr
);
427 PPC_IO_DPRINTF("0x" TARGET_FMT_plx
" <= 0x%08" PRIx32
"\n", addr
, ret
);
432 static void PPC_prep_io_writel (void *opaque
, target_phys_addr_t addr
,
435 sysctrl_t
*sysctrl
= opaque
;
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
);
442 static uint32_t PPC_prep_io_readl (void *opaque
, target_phys_addr_t addr
)
444 sysctrl_t
*sysctrl
= opaque
;
447 addr
= prep_IO_address(sysctrl
, addr
);
449 PPC_IO_DPRINTF("0x" TARGET_FMT_plx
" <= 0x%08" PRIx32
"\n", addr
, ret
);
454 static const MemoryRegionOps PPC_prep_io_ops
= {
456 .read
= { PPC_prep_io_readb
, PPC_prep_io_readw
, PPC_prep_io_readl
},
457 .write
= { PPC_prep_io_writeb
, PPC_prep_io_writew
, PPC_prep_io_writel
},
459 .endianness
= DEVICE_LITTLE_ENDIAN
,
462 #define NVRAM_SIZE 0x2000
464 static void cpu_request_exit(void *opaque
, int irq
, int level
)
466 CPUState
*env
= cpu_single_env
;
473 /* PowerPC PREP hardware initialisation */
474 static void ppc_prep_init (ram_addr_t ram_size
,
475 const char *boot_device
,
476 const char *kernel_filename
,
477 const char *kernel_cmdline
,
478 const char *initrd_filename
,
479 const char *cpu_model
)
481 MemoryRegion
*sysmem
= get_system_memory();
482 CPUState
*env
= NULL
;
486 MemoryRegion
*PPC_io_memory
= g_new(MemoryRegion
, 1);
487 MemoryRegion
*intack
= g_new(MemoryRegion
, 1);
489 MemoryRegion
*xcsr
= g_new(MemoryRegion
, 1);
491 int linux_boot
, i
, nb_nics1
, bios_size
;
492 MemoryRegion
*ram
= g_new(MemoryRegion
, 1);
493 MemoryRegion
*bios
= g_new(MemoryRegion
, 1);
494 uint32_t kernel_base
, initrd_base
;
495 long kernel_size
, initrd_size
;
498 PCIHostState
*pcihost
;
502 qemu_irq
*cpu_exit_irq
;
504 DriveInfo
*hd
[MAX_IDE_BUS
* MAX_IDE_DEVS
];
505 DriveInfo
*fd
[MAX_FD
];
507 sysctrl
= g_malloc0(sizeof(sysctrl_t
));
509 linux_boot
= (kernel_filename
!= NULL
);
512 if (cpu_model
== NULL
)
514 for (i
= 0; i
< smp_cpus
; i
++) {
515 env
= cpu_init(cpu_model
);
517 fprintf(stderr
, "Unable to find PowerPC CPU definition\n");
520 if (env
->flags
& POWERPC_FLAG_RTC_CLK
) {
521 /* POWER / PowerPC 601 RTC clock frequency is 7.8125 MHz */
522 cpu_ppc_tb_init(env
, 7812500UL);
524 /* Set time-base frequency to 100 Mhz */
525 cpu_ppc_tb_init(env
, 100UL * 1000UL * 1000UL);
527 qemu_register_reset((QEMUResetHandler
*)&cpu_reset
, env
);
531 memory_region_init_ram(ram
, "ppc_prep.ram", ram_size
);
532 vmstate_register_ram_global(ram
);
533 memory_region_add_subregion(sysmem
, 0, ram
);
535 /* allocate and load BIOS */
536 memory_region_init_ram(bios
, "ppc_prep.bios", BIOS_SIZE
);
537 memory_region_set_readonly(bios
, true);
538 memory_region_add_subregion(sysmem
, (uint32_t)(-BIOS_SIZE
), bios
);
539 vmstate_register_ram_global(bios
);
540 if (bios_name
== NULL
)
541 bios_name
= BIOS_FILENAME
;
542 filename
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, bios_name
);
544 bios_size
= get_image_size(filename
);
548 if (bios_size
> 0 && bios_size
<= BIOS_SIZE
) {
549 target_phys_addr_t bios_addr
;
550 bios_size
= (bios_size
+ 0xfff) & ~0xfff;
551 bios_addr
= (uint32_t)(-bios_size
);
552 bios_size
= load_image_targphys(filename
, bios_addr
, bios_size
);
554 if (bios_size
< 0 || bios_size
> BIOS_SIZE
) {
555 hw_error("qemu: could not load PPC PREP bios '%s'\n", bios_name
);
562 kernel_base
= KERNEL_LOAD_ADDR
;
563 /* now we can load the kernel */
564 kernel_size
= load_image_targphys(kernel_filename
, kernel_base
,
565 ram_size
- kernel_base
);
566 if (kernel_size
< 0) {
567 hw_error("qemu: could not load kernel '%s'\n", kernel_filename
);
571 if (initrd_filename
) {
572 initrd_base
= INITRD_LOAD_ADDR
;
573 initrd_size
= load_image_targphys(initrd_filename
, initrd_base
,
574 ram_size
- initrd_base
);
575 if (initrd_size
< 0) {
576 hw_error("qemu: could not load initial ram disk '%s'\n",
583 ppc_boot_device
= 'm';
589 ppc_boot_device
= '\0';
590 /* For now, OHW cannot boot from the network. */
591 for (i
= 0; boot_device
[i
] != '\0'; i
++) {
592 if (boot_device
[i
] >= 'a' && boot_device
[i
] <= 'f') {
593 ppc_boot_device
= boot_device
[i
];
597 if (ppc_boot_device
== '\0') {
598 fprintf(stderr
, "No valid boot device for Mac99 machine\n");
603 if (PPC_INPUT(env
) != PPC_FLAGS_INPUT_6xx
) {
604 hw_error("Only 6xx bus is supported on PREP machine\n");
607 dev
= qdev_create(NULL
, "raven-pcihost");
608 sys
= sysbus_from_qdev(dev
);
609 pcihost
= DO_UPCAST(PCIHostState
, busdev
, sys
);
610 pcihost
->address_space
= get_system_memory();
611 qdev_init_nofail(dev
);
612 object_property_add_child(object_get_root(), "raven", OBJECT(dev
), NULL
);
613 pci_bus
= (PCIBus
*)qdev_get_child_bus(dev
, "pci.0");
614 if (pci_bus
== NULL
) {
615 fprintf(stderr
, "Couldn't create PCI host controller.\n");
619 /* PCI -> ISA bridge */
620 pci
= pci_create_simple(pci_bus
, PCI_DEVFN(1, 0), "i82378");
621 cpu_exit_irq
= qemu_allocate_irqs(cpu_request_exit
, NULL
, 1);
622 qdev_connect_gpio_out(&pci
->qdev
, 0,
623 first_cpu
->irq_inputs
[PPC6xx_INPUT_INT
]);
624 qdev_connect_gpio_out(&pci
->qdev
, 1, *cpu_exit_irq
);
625 sysbus_connect_irq(&pcihost
->busdev
, 0, qdev_get_gpio_in(&pci
->qdev
, 9));
626 sysbus_connect_irq(&pcihost
->busdev
, 1, qdev_get_gpio_in(&pci
->qdev
, 11));
627 sysbus_connect_irq(&pcihost
->busdev
, 2, qdev_get_gpio_in(&pci
->qdev
, 9));
628 sysbus_connect_irq(&pcihost
->busdev
, 3, qdev_get_gpio_in(&pci
->qdev
, 11));
629 isa_bus
= DO_UPCAST(ISABus
, qbus
, qdev_get_child_bus(&pci
->qdev
, "isa.0"));
631 /* Register 8 MB of ISA IO space (needed for non-contiguous map) */
632 memory_region_init_io(PPC_io_memory
, &PPC_prep_io_ops
, sysctrl
,
633 "ppc-io", 0x00800000);
634 memory_region_add_subregion(sysmem
, 0x80000000, PPC_io_memory
);
636 /* init basic PC hardware */
637 pci_vga_init(pci_bus
);
640 serial_isa_init(isa_bus
, 0, serial_hds
[0]);
642 if (nb_nics1
> NE2000_NB_MAX
)
643 nb_nics1
= NE2000_NB_MAX
;
644 for(i
= 0; i
< nb_nics1
; i
++) {
645 if (nd_table
[i
].model
== NULL
) {
646 nd_table
[i
].model
= g_strdup("ne2k_isa");
648 if (strcmp(nd_table
[i
].model
, "ne2k_isa") == 0) {
649 isa_ne2000_init(isa_bus
, ne2000_io
[i
], ne2000_irq
[i
],
652 pci_nic_init_nofail(&nd_table
[i
], "ne2k_pci", NULL
);
656 ide_drive_get(hd
, MAX_IDE_BUS
);
657 for(i
= 0; i
< MAX_IDE_BUS
; i
++) {
658 isa_ide_init(isa_bus
, ide_iobase
[i
], ide_iobase2
[i
], ide_irq
[i
],
662 isa_create_simple(isa_bus
, "i8042");
666 for(i
= 0; i
< MAX_FD
; i
++) {
667 fd
[i
] = drive_get(IF_FLOPPY
, 0, i
);
669 fdctrl_init_isa(isa_bus
, fd
);
671 /* Register fake IO ports for PREP */
672 sysctrl
->reset_irq
= first_cpu
->irq_inputs
[PPC6xx_INPUT_HRESET
];
673 register_ioport_read(0x398, 2, 1, &PREP_io_read
, sysctrl
);
674 register_ioport_write(0x398, 2, 1, &PREP_io_write
, sysctrl
);
675 /* System control ports */
676 register_ioport_read(0x0092, 0x01, 1, &PREP_io_800_readb
, sysctrl
);
677 register_ioport_write(0x0092, 0x01, 1, &PREP_io_800_writeb
, sysctrl
);
678 register_ioport_read(0x0800, 0x52, 1, &PREP_io_800_readb
, sysctrl
);
679 register_ioport_write(0x0800, 0x52, 1, &PREP_io_800_writeb
, sysctrl
);
680 /* PCI intack location */
681 memory_region_init_io(intack
, &PPC_intack_ops
, NULL
, "ppc-intack", 4);
682 memory_region_add_subregion(sysmem
, 0xBFFFFFF0, intack
);
683 /* PowerPC control and status register group */
685 memory_region_init_io(xcsr
, &PPC_XCSR_ops
, NULL
, "ppc-xcsr", 0x1000);
686 memory_region_add_subregion(sysmem
, 0xFEFF0000, xcsr
);
690 pci_create_simple(pci_bus
, -1, "pci-ohci");
693 m48t59
= m48t59_init_isa(isa_bus
, 0x0074, NVRAM_SIZE
, 59);
696 sysctrl
->nvram
= m48t59
;
698 /* Initialise NVRAM */
699 nvram
.opaque
= m48t59
;
700 nvram
.read_fn
= &m48t59_read
;
701 nvram
.write_fn
= &m48t59_write
;
702 PPC_NVRAM_set_params(&nvram
, NVRAM_SIZE
, "PREP", ram_size
, ppc_boot_device
,
703 kernel_base
, kernel_size
,
705 initrd_base
, initrd_size
,
706 /* XXX: need an option to load a NVRAM image */
708 graphic_width
, graphic_height
, graphic_depth
);
710 /* Special port to get debug messages from Open-Firmware */
711 register_ioport_write(0x0F00, 4, 1, &PPC_debug_write
, NULL
);
714 static QEMUMachine prep_machine
= {
716 .desc
= "PowerPC PREP platform",
717 .init
= ppc_prep_init
,
718 .max_cpus
= MAX_CPUS
,
721 static void prep_machine_init(void)
723 qemu_register_machine(&prep_machine
);
726 machine_init(prep_machine_init
);