2 * QEMU PPC PREP hardware System Emulator
4 * Copyright (c) 2003-2004 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 #define BIOS_FILENAME "ppc_rom.bin"
30 #define KERNEL_LOAD_ADDR 0x01000000
31 #define INITRD_LOAD_ADDR 0x01800000
36 #if defined (HARD_DEBUG_PPC_IO) && !defined (DEBUG_PPC_IO)
40 #if defined (HARD_DEBUG_PPC_IO)
41 #define PPC_IO_DPRINTF(fmt, args...) \
43 if (loglevel & CPU_LOG_IOPORT) { \
44 fprintf(logfile, "%s: " fmt, __func__ , ##args); \
46 printf("%s : " fmt, __func__ , ##args); \
49 #elif defined (DEBUG_PPC_IO)
50 #define PPC_IO_DPRINTF(fmt, args...) \
52 if (loglevel & CPU_LOG_IOPORT) { \
53 fprintf(logfile, "%s: " fmt, __func__ , ##args); \
57 #define PPC_IO_DPRINTF(fmt, args...) do { } while (0)
60 /* Constants for devices init */
61 static const int ide_iobase
[2] = { 0x1f0, 0x170 };
62 static const int ide_iobase2
[2] = { 0x3f6, 0x376 };
63 static const int ide_irq
[2] = { 13, 13 };
65 #define NE2000_NB_MAX 6
67 static uint32_t ne2000_io
[NE2000_NB_MAX
] = { 0x300, 0x320, 0x340, 0x360, 0x280, 0x380 };
68 static int ne2000_irq
[NE2000_NB_MAX
] = { 9, 10, 11, 3, 4, 5 };
70 //static PITState *pit;
72 /* ISA IO ports bridge */
73 #define PPC_IO_BASE 0x80000000
75 /* Speaker port 0x61 */
77 int dummy_refresh_clock
;
79 static void speaker_ioport_write(void *opaque
, uint32_t addr
, uint32_t val
)
82 speaker_data_on
= (val
>> 1) & 1;
83 pit_set_gate(pit
, 2, val
& 1);
87 static uint32_t speaker_ioport_read(void *opaque
, uint32_t addr
)
91 out
= pit_get_out(pit
, 2, qemu_get_clock(vm_clock
));
92 dummy_refresh_clock
^= 1;
93 return (speaker_data_on
<< 1) | pit_get_gate(pit
, 2) | (out
<< 5) |
94 (dummy_refresh_clock
<< 4);
99 static void pic_irq_request(void *opaque
, int level
)
102 cpu_interrupt(first_cpu
, CPU_INTERRUPT_HARD
);
104 cpu_reset_interrupt(first_cpu
, CPU_INTERRUPT_HARD
);
107 /* PCI intack register */
108 /* Read-only register (?) */
109 static void _PPC_intack_write (void *opaque
, target_phys_addr_t addr
, uint32_t value
)
111 // printf("%s: 0x%08x => 0x%08x\n", __func__, addr, value);
114 static inline uint32_t _PPC_intack_read (target_phys_addr_t addr
)
118 if (addr
== 0xBFFFFFF0)
119 retval
= pic_intack_read(isa_pic
);
120 // printf("%s: 0x%08x <= %d\n", __func__, addr, retval);
125 static uint32_t PPC_intack_readb (void *opaque
, target_phys_addr_t addr
)
127 return _PPC_intack_read(addr
);
130 static uint32_t PPC_intack_readw (void *opaque
, target_phys_addr_t addr
)
132 #ifdef TARGET_WORDS_BIGENDIAN
133 return bswap16(_PPC_intack_read(addr
));
135 return _PPC_intack_read(addr
);
139 static uint32_t PPC_intack_readl (void *opaque
, target_phys_addr_t addr
)
141 #ifdef TARGET_WORDS_BIGENDIAN
142 return bswap32(_PPC_intack_read(addr
));
144 return _PPC_intack_read(addr
);
148 static CPUWriteMemoryFunc
*PPC_intack_write
[] = {
154 static CPUReadMemoryFunc
*PPC_intack_read
[] = {
160 /* PowerPC control and status registers */
166 /* Control and status */
171 /* General purpose registers */
184 /* Error diagnostic */
187 static void PPC_XCSR_writeb (void *opaque
, target_phys_addr_t addr
, uint32_t value
)
189 printf("%s: 0x%08lx => 0x%08x\n", __func__
, (long)addr
, value
);
192 static void PPC_XCSR_writew (void *opaque
, target_phys_addr_t addr
, uint32_t value
)
194 #ifdef TARGET_WORDS_BIGENDIAN
195 value
= bswap16(value
);
197 printf("%s: 0x%08lx => 0x%08x\n", __func__
, (long)addr
, value
);
200 static void PPC_XCSR_writel (void *opaque
, target_phys_addr_t addr
, uint32_t value
)
202 #ifdef TARGET_WORDS_BIGENDIAN
203 value
= bswap32(value
);
205 printf("%s: 0x%08lx => 0x%08x\n", __func__
, (long)addr
, value
);
208 static uint32_t PPC_XCSR_readb (void *opaque
, target_phys_addr_t addr
)
212 printf("%s: 0x%08lx <= %d\n", __func__
, (long)addr
, retval
);
217 static uint32_t PPC_XCSR_readw (void *opaque
, target_phys_addr_t addr
)
221 printf("%s: 0x%08lx <= %d\n", __func__
, (long)addr
, retval
);
222 #ifdef TARGET_WORDS_BIGENDIAN
223 retval
= bswap16(retval
);
229 static uint32_t PPC_XCSR_readl (void *opaque
, target_phys_addr_t addr
)
233 printf("%s: 0x%08lx <= %d\n", __func__
, (long)addr
, retval
);
234 #ifdef TARGET_WORDS_BIGENDIAN
235 retval
= bswap32(retval
);
241 static CPUWriteMemoryFunc
*PPC_XCSR_write
[] = {
247 static CPUReadMemoryFunc
*PPC_XCSR_read
[] = {
254 /* Fake super-io ports for PREP platform (Intel 82378ZB) */
255 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 // cpu_interrupt(first_cpu, CPU_INTERRUPT_RESET);
307 /* Motorola CPU configuration register : read-only */
310 /* Motorola base module feature register : read-only */
313 /* Motorola base module status register : read-only */
316 /* Hardfile light register */
318 sysctrl
->state
|= STATE_HARDFILE
;
320 sysctrl
->state
&= ~STATE_HARDFILE
;
323 /* Password protect 1 register */
324 if (sysctrl
->nvram
!= NULL
)
325 m48t59_toggle_lock(sysctrl
->nvram
, 1);
328 /* Password protect 2 register */
329 if (sysctrl
->nvram
!= NULL
)
330 m48t59_toggle_lock(sysctrl
->nvram
, 2);
333 /* L2 invalidate register */
334 // tlb_flush(first_cpu, 1);
337 /* system control register */
338 sysctrl
->syscontrol
= val
& 0x0F;
341 /* I/O map type register */
342 sysctrl
->contiguous_map
= val
& 0x01;
345 printf("ERROR: unaffected IO port write: %04lx => %02x\n",
351 static uint32_t PREP_io_800_readb (void *opaque
, uint32_t addr
)
353 sysctrl_t
*sysctrl
= opaque
;
354 uint32_t retval
= 0xFF;
358 /* Special port 92 */
362 /* Motorola CPU configuration register */
363 retval
= 0xEF; /* MPC750 */
366 /* Motorola Base module feature register */
367 retval
= 0xAD; /* No ESCC, PMC slot neither ethernet */
370 /* Motorola base module status register */
371 retval
= 0xE0; /* Standard MPC750 */
374 /* Equipment present register:
376 * no upgrade processor
377 * no cards in PCI slots
383 /* Motorola base module extended feature register */
384 retval
= 0x39; /* No USB, CF and PCI bridge. NVRAM present */
387 /* L2 invalidate: don't care */
394 /* system control register
395 * 7 - 6 / 1 - 0: L2 cache enable
397 retval
= sysctrl
->syscontrol
;
401 retval
= 0x03; /* no L2 cache */
404 /* I/O map type register */
405 retval
= sysctrl
->contiguous_map
;
408 printf("ERROR: unaffected IO port: %04lx read\n", (long)addr
);
411 PPC_IO_DPRINTF("0x%08lx <= 0x%08x\n", (long)addr
- PPC_IO_BASE
, retval
);
416 static inline target_phys_addr_t
prep_IO_address (sysctrl_t
*sysctrl
,
417 target_phys_addr_t addr
)
419 if (sysctrl
->contiguous_map
== 0) {
420 /* 64 KB contiguous space for IOs */
423 /* 8 MB non-contiguous space for IOs */
424 addr
= (addr
& 0x1F) | ((addr
& 0x007FFF000) >> 7);
430 static void PPC_prep_io_writeb (void *opaque
, target_phys_addr_t addr
,
433 sysctrl_t
*sysctrl
= opaque
;
435 addr
= prep_IO_address(sysctrl
, addr
);
436 cpu_outb(NULL
, addr
, value
);
439 static uint32_t PPC_prep_io_readb (void *opaque
, target_phys_addr_t addr
)
441 sysctrl_t
*sysctrl
= opaque
;
444 addr
= prep_IO_address(sysctrl
, addr
);
445 ret
= cpu_inb(NULL
, addr
);
450 static void PPC_prep_io_writew (void *opaque
, target_phys_addr_t addr
,
453 sysctrl_t
*sysctrl
= opaque
;
455 addr
= prep_IO_address(sysctrl
, addr
);
456 #ifdef TARGET_WORDS_BIGENDIAN
457 value
= bswap16(value
);
459 PPC_IO_DPRINTF("0x%08lx => 0x%08x\n", (long)addr
, value
);
460 cpu_outw(NULL
, addr
, value
);
463 static uint32_t PPC_prep_io_readw (void *opaque
, target_phys_addr_t addr
)
465 sysctrl_t
*sysctrl
= opaque
;
468 addr
= prep_IO_address(sysctrl
, addr
);
469 ret
= cpu_inw(NULL
, addr
);
470 #ifdef TARGET_WORDS_BIGENDIAN
473 PPC_IO_DPRINTF("0x%08lx <= 0x%08x\n", (long)addr
, ret
);
478 static void PPC_prep_io_writel (void *opaque
, target_phys_addr_t addr
,
481 sysctrl_t
*sysctrl
= opaque
;
483 addr
= prep_IO_address(sysctrl
, addr
);
484 #ifdef TARGET_WORDS_BIGENDIAN
485 value
= bswap32(value
);
487 PPC_IO_DPRINTF("0x%08lx => 0x%08x\n", (long)addr
, value
);
488 cpu_outl(NULL
, addr
, value
);
491 static uint32_t PPC_prep_io_readl (void *opaque
, target_phys_addr_t addr
)
493 sysctrl_t
*sysctrl
= opaque
;
496 addr
= prep_IO_address(sysctrl
, addr
);
497 ret
= cpu_inl(NULL
, addr
);
498 #ifdef TARGET_WORDS_BIGENDIAN
501 PPC_IO_DPRINTF("0x%08lx <= 0x%08x\n", (long)addr
, ret
);
506 CPUWriteMemoryFunc
*PPC_prep_io_write
[] = {
512 CPUReadMemoryFunc
*PPC_prep_io_read
[] = {
518 #define NVRAM_SIZE 0x2000
520 /* PowerPC PREP hardware initialisation */
521 static void ppc_prep_init(int ram_size
, int vga_ram_size
, int boot_device
,
522 DisplayState
*ds
, const char **fd_filename
, int snapshot
,
523 const char *kernel_filename
, const char *kernel_cmdline
,
524 const char *initrd_filename
)
531 int linux_boot
, i
, nb_nics1
, bios_size
;
532 unsigned long bios_offset
;
533 uint32_t kernel_base
, kernel_size
, initrd_base
, initrd_size
;
537 sysctrl
= qemu_mallocz(sizeof(sysctrl_t
));
541 linux_boot
= (kernel_filename
!= NULL
);
546 register_savevm("cpu", 0, 3, cpu_save
, cpu_load
, env
);
548 /* Register CPU as a 604 */
549 /* XXX: CPU model (or PVR) should be provided on command line */
550 // ppc_find_by_name("604r", &def);
551 // ppc_find_by_name("604e", &def);
552 ppc_find_by_name("604", &def
);
554 cpu_abort(env
, "Unable to find PowerPC CPU definition\n");
556 cpu_ppc_register(env
, def
);
557 /* Set time-base frequency to 100 Mhz */
558 cpu_ppc_tb_init(env
, 100UL * 1000UL * 1000UL);
561 cpu_register_physical_memory(0, ram_size
, IO_MEM_RAM
);
563 /* allocate and load BIOS */
564 bios_offset
= ram_size
+ vga_ram_size
;
565 snprintf(buf
, sizeof(buf
), "%s/%s", bios_dir
, BIOS_FILENAME
);
566 bios_size
= load_image(buf
, phys_ram_base
+ bios_offset
);
567 if (bios_size
< 0 || bios_size
> BIOS_SIZE
) {
568 fprintf(stderr
, "qemu: could not load PPC PREP bios '%s'\n", buf
);
571 bios_size
= (bios_size
+ 0xfff) & ~0xfff;
572 cpu_register_physical_memory((uint32_t)(-bios_size
),
573 bios_size
, bios_offset
| IO_MEM_ROM
);
576 kernel_base
= KERNEL_LOAD_ADDR
;
577 /* now we can load the kernel */
578 kernel_size
= load_image(kernel_filename
, phys_ram_base
+ kernel_base
);
579 if (kernel_size
< 0) {
580 fprintf(stderr
, "qemu: could not load kernel '%s'\n",
585 if (initrd_filename
) {
586 initrd_base
= INITRD_LOAD_ADDR
;
587 initrd_size
= load_image(initrd_filename
,
588 phys_ram_base
+ initrd_base
);
589 if (initrd_size
< 0) {
590 fprintf(stderr
, "qemu: could not load initial ram disk '%s'\n",
606 isa_mem_base
= 0xc0000000;
607 pci_bus
= pci_prep_init();
608 // pci_bus = i440fx_init();
609 /* Register 8 MB of ISA IO space (needed for non-contiguous map) */
610 PPC_io_memory
= cpu_register_io_memory(0, PPC_prep_io_read
,
611 PPC_prep_io_write
, sysctrl
);
612 cpu_register_physical_memory(0x80000000, 0x00800000, PPC_io_memory
);
614 /* init basic PC hardware */
615 vga_initialize(pci_bus
, ds
, phys_ram_base
+ ram_size
, ram_size
,
618 // openpic = openpic_init(0x00000000, 0xF0000000, 1);
619 isa_pic
= pic_init(pic_irq_request
, first_cpu
);
620 // pit = pit_init(0x40, 0);
622 serial_init(&pic_set_irq_new
, isa_pic
, 0x3f8, 4, serial_hds
[0]);
624 if (nb_nics1
> NE2000_NB_MAX
)
625 nb_nics1
= NE2000_NB_MAX
;
626 for(i
= 0; i
< nb_nics1
; i
++) {
627 if (nd_table
[0].model
== NULL
628 || strcmp(nd_table
[0].model
, "ne2k_isa") == 0) {
629 isa_ne2000_init(ne2000_io
[i
], ne2000_irq
[i
], &nd_table
[i
]);
631 fprintf(stderr
, "qemu: Unsupported NIC: %s\n", nd_table
[0].model
);
636 for(i
= 0; i
< 2; i
++) {
637 isa_ide_init(ide_iobase
[i
], ide_iobase2
[i
], ide_irq
[i
],
638 bs_table
[2 * i
], bs_table
[2 * i
+ 1]);
645 fdctrl_init(6, 2, 0, 0x3f0, fd_table
);
647 /* Register speaker port */
648 register_ioport_read(0x61, 1, 1, speaker_ioport_read
, NULL
);
649 register_ioport_write(0x61, 1, 1, speaker_ioport_write
, NULL
);
650 /* Register fake IO ports for PREP */
651 register_ioport_read(0x398, 2, 1, &PREP_io_read
, sysctrl
);
652 register_ioport_write(0x398, 2, 1, &PREP_io_write
, sysctrl
);
653 /* System control ports */
654 register_ioport_read(0x0092, 0x01, 1, &PREP_io_800_readb
, sysctrl
);
655 register_ioport_write(0x0092, 0x01, 1, &PREP_io_800_writeb
, sysctrl
);
656 register_ioport_read(0x0800, 0x52, 1, &PREP_io_800_readb
, sysctrl
);
657 register_ioport_write(0x0800, 0x52, 1, &PREP_io_800_writeb
, sysctrl
);
658 /* PCI intack location */
659 PPC_io_memory
= cpu_register_io_memory(0, PPC_intack_read
,
660 PPC_intack_write
, NULL
);
661 cpu_register_physical_memory(0xBFFFFFF0, 0x4, PPC_io_memory
);
662 /* PowerPC control and status register group */
664 PPC_io_memory
= cpu_register_io_memory(0, PPC_XCSR_read
, PPC_XCSR_write
, NULL
);
665 cpu_register_physical_memory(0xFEFF0000, 0x1000, PPC_io_memory
);
668 nvram
= m48t59_init(8, 0, 0x0074, NVRAM_SIZE
, 59);
671 sysctrl
->nvram
= nvram
;
673 /* Initialise NVRAM */
674 PPC_NVRAM_set_params(nvram
, NVRAM_SIZE
, "PREP", ram_size
, 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
);
686 QEMUMachine prep_machine
= {
688 "PowerPC PREP platform",