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 /* PCI intack register */
100 /* Read-only register (?) */
101 static void _PPC_intack_write (void *opaque
, target_phys_addr_t addr
, uint32_t value
)
103 // printf("%s: 0x%08x => 0x%08x\n", __func__, addr, value);
106 static inline uint32_t _PPC_intack_read (target_phys_addr_t addr
)
110 if (addr
== 0xBFFFFFF0)
111 retval
= pic_intack_read(NULL
);
112 // printf("%s: 0x%08x <= %d\n", __func__, addr, retval);
117 static uint32_t PPC_intack_readb (void *opaque
, target_phys_addr_t addr
)
119 return _PPC_intack_read(addr
);
122 static uint32_t PPC_intack_readw (void *opaque
, target_phys_addr_t addr
)
124 #ifdef TARGET_WORDS_BIGENDIAN
125 return bswap16(_PPC_intack_read(addr
));
127 return _PPC_intack_read(addr
);
131 static uint32_t PPC_intack_readl (void *opaque
, target_phys_addr_t addr
)
133 #ifdef TARGET_WORDS_BIGENDIAN
134 return bswap32(_PPC_intack_read(addr
));
136 return _PPC_intack_read(addr
);
140 static CPUWriteMemoryFunc
*PPC_intack_write
[] = {
146 static CPUReadMemoryFunc
*PPC_intack_read
[] = {
152 /* PowerPC control and status registers */
158 /* Control and status */
163 /* General purpose registers */
176 /* Error diagnostic */
179 static void PPC_XCSR_writeb (void *opaque
, target_phys_addr_t addr
, uint32_t value
)
181 printf("%s: 0x%08lx => 0x%08x\n", __func__
, (long)addr
, value
);
184 static void PPC_XCSR_writew (void *opaque
, target_phys_addr_t addr
, uint32_t value
)
186 #ifdef TARGET_WORDS_BIGENDIAN
187 value
= bswap16(value
);
189 printf("%s: 0x%08lx => 0x%08x\n", __func__
, (long)addr
, value
);
192 static void PPC_XCSR_writel (void *opaque
, target_phys_addr_t addr
, uint32_t value
)
194 #ifdef TARGET_WORDS_BIGENDIAN
195 value
= bswap32(value
);
197 printf("%s: 0x%08lx => 0x%08x\n", __func__
, (long)addr
, value
);
200 static uint32_t PPC_XCSR_readb (void *opaque
, target_phys_addr_t addr
)
204 printf("%s: 0x%08lx <= %d\n", __func__
, (long)addr
, retval
);
209 static uint32_t PPC_XCSR_readw (void *opaque
, target_phys_addr_t addr
)
213 printf("%s: 0x%08lx <= %d\n", __func__
, (long)addr
, retval
);
214 #ifdef TARGET_WORDS_BIGENDIAN
215 retval
= bswap16(retval
);
221 static uint32_t PPC_XCSR_readl (void *opaque
, target_phys_addr_t addr
)
225 printf("%s: 0x%08lx <= %d\n", __func__
, (long)addr
, retval
);
226 #ifdef TARGET_WORDS_BIGENDIAN
227 retval
= bswap32(retval
);
233 static CPUWriteMemoryFunc
*PPC_XCSR_write
[] = {
239 static CPUReadMemoryFunc
*PPC_XCSR_read
[] = {
246 /* Fake super-io ports for PREP platform (Intel 82378ZB) */
247 typedef struct sysctrl_t
{
256 STATE_HARDFILE
= 0x01,
259 static sysctrl_t
*sysctrl
;
261 static void PREP_io_write (void *opaque
, uint32_t addr
, uint32_t val
)
263 sysctrl_t
*sysctrl
= opaque
;
265 PPC_IO_DPRINTF("0x%08lx => 0x%08x\n", (long)addr
- PPC_IO_BASE
, val
);
266 sysctrl
->fake_io
[addr
- 0x0398] = val
;
269 static uint32_t PREP_io_read (void *opaque
, uint32_t addr
)
271 sysctrl_t
*sysctrl
= opaque
;
273 PPC_IO_DPRINTF("0x%08lx <= 0x%08x\n", (long)addr
- PPC_IO_BASE
,
274 sysctrl
->fake_io
[addr
- 0x0398]);
275 return sysctrl
->fake_io
[addr
- 0x0398];
278 static void PREP_io_800_writeb (void *opaque
, uint32_t addr
, uint32_t val
)
280 sysctrl_t
*sysctrl
= opaque
;
282 PPC_IO_DPRINTF("0x%08lx => 0x%08x\n", (long)addr
- PPC_IO_BASE
, val
);
285 /* Special port 92 */
286 /* Check soft reset asked */
288 // cpu_interrupt(cpu_single_env, CPU_INTERRUPT_RESET);
292 printf("Little Endian mode isn't supported (yet ?)\n");
297 /* Motorola CPU configuration register : read-only */
300 /* Motorola base module feature register : read-only */
303 /* Motorola base module status register : read-only */
306 /* Hardfile light register */
308 sysctrl
->state
|= STATE_HARDFILE
;
310 sysctrl
->state
&= ~STATE_HARDFILE
;
313 /* Password protect 1 register */
314 if (sysctrl
->nvram
!= NULL
)
315 m48t59_toggle_lock(sysctrl
->nvram
, 1);
318 /* Password protect 2 register */
319 if (sysctrl
->nvram
!= NULL
)
320 m48t59_toggle_lock(sysctrl
->nvram
, 2);
323 /* L2 invalidate register */
324 // tlb_flush(cpu_single_env, 1);
327 /* system control register */
328 sysctrl
->syscontrol
= val
& 0x0F;
331 /* I/O map type register */
332 sysctrl
->contiguous_map
= val
& 0x01;
335 printf("ERROR: unaffected IO port write: %04lx => %02x\n",
341 static uint32_t PREP_io_800_readb (void *opaque
, uint32_t addr
)
343 sysctrl_t
*sysctrl
= opaque
;
344 uint32_t retval
= 0xFF;
348 /* Special port 92 */
352 /* Motorola CPU configuration register */
353 retval
= 0xEF; /* MPC750 */
356 /* Motorola Base module feature register */
357 retval
= 0xAD; /* No ESCC, PMC slot neither ethernet */
360 /* Motorola base module status register */
361 retval
= 0xE0; /* Standard MPC750 */
364 /* Equipment present register:
366 * no upgrade processor
367 * no cards in PCI slots
373 /* Motorola base module extended feature register */
374 retval
= 0x39; /* No USB, CF and PCI bridge. NVRAM present */
377 /* L2 invalidate: don't care */
384 /* system control register
385 * 7 - 6 / 1 - 0: L2 cache enable
387 retval
= sysctrl
->syscontrol
;
391 retval
= 0x03; /* no L2 cache */
394 /* I/O map type register */
395 retval
= sysctrl
->contiguous_map
;
398 printf("ERROR: unaffected IO port: %04lx read\n", (long)addr
);
401 PPC_IO_DPRINTF("0x%08lx <= 0x%08x\n", (long)addr
- PPC_IO_BASE
, retval
);
406 static inline target_phys_addr_t
prep_IO_address (sysctrl_t
*sysctrl
,
407 target_phys_addr_t addr
)
409 if (sysctrl
->contiguous_map
== 0) {
410 /* 64 KB contiguous space for IOs */
413 /* 8 MB non-contiguous space for IOs */
414 addr
= (addr
& 0x1F) | ((addr
& 0x007FFF000) >> 7);
420 static void PPC_prep_io_writeb (void *opaque
, target_phys_addr_t addr
,
423 sysctrl_t
*sysctrl
= opaque
;
425 addr
= prep_IO_address(sysctrl
, addr
);
426 cpu_outb(NULL
, addr
, value
);
429 static uint32_t PPC_prep_io_readb (void *opaque
, target_phys_addr_t addr
)
431 sysctrl_t
*sysctrl
= opaque
;
434 addr
= prep_IO_address(sysctrl
, addr
);
435 ret
= cpu_inb(NULL
, addr
);
440 static void PPC_prep_io_writew (void *opaque
, target_phys_addr_t addr
,
443 sysctrl_t
*sysctrl
= opaque
;
445 addr
= prep_IO_address(sysctrl
, addr
);
446 #ifdef TARGET_WORDS_BIGENDIAN
447 value
= bswap16(value
);
449 PPC_IO_DPRINTF("0x%08lx => 0x%08x\n", (long)addr
, value
);
450 cpu_outw(NULL
, addr
, value
);
453 static uint32_t PPC_prep_io_readw (void *opaque
, target_phys_addr_t addr
)
455 sysctrl_t
*sysctrl
= opaque
;
458 addr
= prep_IO_address(sysctrl
, addr
);
459 ret
= cpu_inw(NULL
, addr
);
460 #ifdef TARGET_WORDS_BIGENDIAN
463 PPC_IO_DPRINTF("0x%08lx <= 0x%08x\n", (long)addr
, ret
);
468 static void PPC_prep_io_writel (void *opaque
, target_phys_addr_t addr
,
471 sysctrl_t
*sysctrl
= opaque
;
473 addr
= prep_IO_address(sysctrl
, addr
);
474 #ifdef TARGET_WORDS_BIGENDIAN
475 value
= bswap32(value
);
477 PPC_IO_DPRINTF("0x%08lx => 0x%08x\n", (long)addr
, value
);
478 cpu_outl(NULL
, addr
, value
);
481 static uint32_t PPC_prep_io_readl (void *opaque
, target_phys_addr_t addr
)
483 sysctrl_t
*sysctrl
= opaque
;
486 addr
= prep_IO_address(sysctrl
, addr
);
487 ret
= cpu_inl(NULL
, addr
);
488 #ifdef TARGET_WORDS_BIGENDIAN
491 PPC_IO_DPRINTF("0x%08lx <= 0x%08x\n", (long)addr
, ret
);
496 CPUWriteMemoryFunc
*PPC_prep_io_write
[] = {
502 CPUReadMemoryFunc
*PPC_prep_io_read
[] = {
508 extern CPUPPCState
*global_env
;
510 #define NVRAM_SIZE 0x2000
512 /* PowerPC PREP hardware initialisation */
513 void ppc_prep_init(int ram_size
, int vga_ram_size
, int boot_device
,
514 DisplayState
*ds
, const char **fd_filename
, int snapshot
,
515 const char *kernel_filename
, const char *kernel_cmdline
,
516 const char *initrd_filename
)
521 int ret
, linux_boot
, i
, nb_nics1
;
522 unsigned long bios_offset
;
523 uint32_t kernel_base
, kernel_size
, initrd_base
, initrd_size
;
526 sysctrl
= qemu_mallocz(sizeof(sysctrl_t
));
530 linux_boot
= (kernel_filename
!= NULL
);
533 cpu_register_physical_memory(0, ram_size
, IO_MEM_RAM
);
535 /* allocate and load BIOS */
536 bios_offset
= ram_size
+ vga_ram_size
;
537 snprintf(buf
, sizeof(buf
), "%s/%s", bios_dir
, BIOS_FILENAME
);
538 ret
= load_image(buf
, phys_ram_base
+ bios_offset
);
539 if (ret
!= BIOS_SIZE
) {
540 fprintf(stderr
, "qemu: could not load PPC PREP bios '%s'\n", buf
);
543 cpu_register_physical_memory((uint32_t)(-BIOS_SIZE
),
544 BIOS_SIZE
, bios_offset
| IO_MEM_ROM
);
545 cpu_single_env
->nip
= 0xfffffffc;
548 kernel_base
= KERNEL_LOAD_ADDR
;
549 /* now we can load the kernel */
550 kernel_size
= load_image(kernel_filename
, phys_ram_base
+ kernel_base
);
551 if (kernel_size
< 0) {
552 fprintf(stderr
, "qemu: could not load kernel '%s'\n",
557 if (initrd_filename
) {
558 initrd_base
= INITRD_LOAD_ADDR
;
559 initrd_size
= load_image(initrd_filename
,
560 phys_ram_base
+ initrd_base
);
561 if (initrd_size
< 0) {
562 fprintf(stderr
, "qemu: could not load initial ram disk '%s'\n",
578 /* Register CPU as a 604 */
579 cpu_ppc_register(cpu_single_env
, 0x00040000);
580 /* Set time-base frequency to 100 Mhz */
581 cpu_ppc_tb_init(cpu_single_env
, 100UL * 1000UL * 1000UL);
583 isa_mem_base
= 0xc0000000;
584 pci_bus
= pci_prep_init();
585 // pci_bus = i440fx_init();
586 /* Register 8 MB of ISA IO space (needed for non-contiguous map) */
587 PPC_io_memory
= cpu_register_io_memory(0, PPC_prep_io_read
,
588 PPC_prep_io_write
, sysctrl
);
589 cpu_register_physical_memory(0x80000000, 0x00800000, PPC_io_memory
);
591 /* init basic PC hardware */
592 vga_initialize(pci_bus
, ds
, phys_ram_base
+ ram_size
, ram_size
,
595 // openpic = openpic_init(0x00000000, 0xF0000000, 1);
596 // pic_init(openpic);
598 // pit = pit_init(0x40, 0);
600 serial_init(0x3f8, 4, serial_hds
[0]);
602 if (nb_nics1
> NE2000_NB_MAX
)
603 nb_nics1
= NE2000_NB_MAX
;
604 for(i
= 0; i
< nb_nics1
; i
++) {
605 isa_ne2000_init(ne2000_io
[i
], ne2000_irq
[i
], &nd_table
[i
]);
608 for(i
= 0; i
< 2; i
++) {
609 isa_ide_init(ide_iobase
[i
], ide_iobase2
[i
], ide_irq
[i
],
610 bs_table
[2 * i
], bs_table
[2 * i
+ 1]);
617 fdctrl_init(6, 2, 0, 0x3f0, fd_table
);
619 /* Register speaker port */
620 register_ioport_read(0x61, 1, 1, speaker_ioport_read
, NULL
);
621 register_ioport_write(0x61, 1, 1, speaker_ioport_write
, NULL
);
622 /* Register fake IO ports for PREP */
623 register_ioport_read(0x398, 2, 1, &PREP_io_read
, sysctrl
);
624 register_ioport_write(0x398, 2, 1, &PREP_io_write
, sysctrl
);
625 /* System control ports */
626 register_ioport_read(0x0092, 0x01, 1, &PREP_io_800_readb
, sysctrl
);
627 register_ioport_write(0x0092, 0x01, 1, &PREP_io_800_writeb
, sysctrl
);
628 register_ioport_read(0x0800, 0x52, 1, &PREP_io_800_readb
, sysctrl
);
629 register_ioport_write(0x0800, 0x52, 1, &PREP_io_800_writeb
, sysctrl
);
630 /* PCI intack location */
631 PPC_io_memory
= cpu_register_io_memory(0, PPC_intack_read
,
632 PPC_intack_write
, NULL
);
633 cpu_register_physical_memory(0xBFFFFFF0, 0x4, PPC_io_memory
);
634 /* PowerPC control and status register group */
636 PPC_io_memory
= cpu_register_io_memory(0, PPC_XCSR_read
, PPC_XCSR_write
, NULL
);
637 cpu_register_physical_memory(0xFEFF0000, 0x1000, PPC_io_memory
);
640 nvram
= m48t59_init(8, 0, 0x0074, NVRAM_SIZE
);
643 sysctrl
->nvram
= nvram
;
645 /* Initialise NVRAM */
646 PPC_NVRAM_set_params(nvram
, NVRAM_SIZE
, "PREP", ram_size
, boot_device
,
647 kernel_base
, kernel_size
,
649 initrd_base
, initrd_size
,
650 /* XXX: need an option to load a NVRAM image */
652 graphic_width
, graphic_height
, graphic_depth
);