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 {
255 STATE_HARDFILE = 0x01,
258 static sysctrl_t *sysctrl;
260 static void PREP_io_write (void *opaque, uint32_t addr, uint32_t val)
262 sysctrl_t *sysctrl = opaque;
264 PPC_IO_DPRINTF("0x%08lx => 0x%08x\n", (long)addr - PPC_IO_BASE, val);
265 sysctrl->fake_io[addr - 0x0398] = val;
268 static uint32_t PREP_io_read (void *opaque, uint32_t addr)
270 sysctrl_t *sysctrl = opaque;
272 PPC_IO_DPRINTF("0x%08lx <= 0x%08x\n", (long)addr - PPC_IO_BASE,
273 sysctrl->fake_io[addr - 0x0398]);
274 return sysctrl->fake_io[addr - 0x0398];
277 static void PREP_io_800_writeb (void *opaque, uint32_t addr, uint32_t val)
279 sysctrl_t *sysctrl = opaque;
281 PPC_IO_DPRINTF("0x%08lx => 0x%08x\n", (long)addr - PPC_IO_BASE, val);
284 /* Special port 92 */
285 /* Check soft reset asked */
287 // cpu_interrupt(cpu_single_env, CPU_INTERRUPT_RESET);
291 printf("Little Endian mode isn't supported (yet ?)\n");
296 /* Motorola CPU configuration register : read-only */
299 /* Motorola base module feature register : read-only */
302 /* Motorola base module status register : read-only */
305 /* Hardfile light register */
307 sysctrl->state |= STATE_HARDFILE;
309 sysctrl->state &= ~STATE_HARDFILE;
312 /* Password protect 1 register */
313 if (sysctrl->nvram != NULL)
314 m48t59_toggle_lock(sysctrl->nvram, 1);
317 /* Password protect 2 register */
318 if (sysctrl->nvram != NULL)
319 m48t59_toggle_lock(sysctrl->nvram, 2);
322 /* L2 invalidate register */
323 // tlb_flush(cpu_single_env, 1);
326 /* system control register */
327 sysctrl->syscontrol = val & 0x0F;
330 /* I/O map type register */
332 printf("No support for non-continuous I/O map mode\n");
337 printf("ERROR: unaffected IO port write: %04lx => %02x\n",
343 static uint32_t PREP_io_800_readb (void *opaque, uint32_t addr)
345 sysctrl_t *sysctrl = opaque;
346 uint32_t retval = 0xFF;
350 /* Special port 92 */
354 /* Motorola CPU configuration register */
355 retval = 0xEF; /* MPC750 */
358 /* Motorola Base module feature register */
359 retval = 0xAD; /* No ESCC, PMC slot neither ethernet */
362 /* Motorola base module status register */
363 retval = 0xE0; /* Standard MPC750 */
366 /* Equipment present register:
368 * no upgrade processor
369 * no cards in PCI slots
375 /* Motorola base module extended feature register */
376 retval = 0x39; /* No USB, CF and PCI bridge. NVRAM present */
383 /* system control register
384 * 7 - 6 / 1 - 0: L2 cache enable
386 retval = sysctrl->syscontrol;
390 retval = 0x03; /* no L2 cache */
393 /* I/O map type register */
397 printf("ERROR: unaffected IO port: %04lx read\n", (long)addr);
400 PPC_IO_DPRINTF("0x%08lx <= 0x%08x\n", (long)addr - PPC_IO_BASE, retval);
405 extern CPUPPCState *global_env;
407 #define NVRAM_SIZE 0x2000
409 /* PowerPC PREP hardware initialisation */
410 void ppc_prep_init(int ram_size, int vga_ram_size, int boot_device,
411 DisplayState *ds, const char **fd_filename, int snapshot,
412 const char *kernel_filename, const char *kernel_cmdline,
413 const char *initrd_filename)
418 int ret, linux_boot, i, nb_nics1, fd;
419 unsigned long bios_offset;
420 uint32_t kernel_base, kernel_size, initrd_base, initrd_size;
423 sysctrl = qemu_mallocz(sizeof(sysctrl_t));
427 linux_boot = (kernel_filename != NULL);
430 cpu_register_physical_memory(0, ram_size, IO_MEM_RAM);
432 /* allocate and load BIOS */
433 bios_offset = ram_size + vga_ram_size;
434 snprintf(buf, sizeof(buf), "%s/%s", bios_dir, BIOS_FILENAME);
435 ret = load_image(buf, phys_ram_base + bios_offset);
436 if (ret != BIOS_SIZE) {
437 fprintf(stderr, "qemu: could not load PPC PREP bios '%s'\n", buf);
440 cpu_register_physical_memory((uint32_t)(-BIOS_SIZE),
441 BIOS_SIZE, bios_offset | IO_MEM_ROM);
442 cpu_single_env->nip = 0xfffffffc;
445 kernel_base = KERNEL_LOAD_ADDR;
446 /* now we can load the kernel */
447 kernel_size = load_image(kernel_filename, phys_ram_base + kernel_base);
448 if (kernel_size < 0) {
449 fprintf(stderr, "qemu: could not load kernel '%s'\n",
454 if (initrd_filename) {
455 initrd_base = INITRD_LOAD_ADDR;
456 initrd_size = load_image(initrd_filename,
457 phys_ram_base + initrd_base);
458 if (initrd_size < 0) {
459 fprintf(stderr, "qemu: could not load initial ram disk '%s'\n",
475 /* Register CPU as a 74x/75x */
476 cpu_ppc_register(cpu_single_env, 0x00080000);
477 /* Set time-base frequency to 100 Mhz */
478 cpu_ppc_tb_init(cpu_single_env, 100UL * 1000UL * 1000UL);
480 isa_mem_base = 0xc0000000;
481 pci_bus = pci_prep_init();
482 /* Register 64 KB of ISA IO space */
483 PPC_io_memory = cpu_register_io_memory(0, PPC_io_read, PPC_io_write, NULL);
484 cpu_register_physical_memory(0x80000000, 0x00010000, PPC_io_memory);
486 /* init basic PC hardware */
487 vga_initialize(pci_bus, ds, phys_ram_base + ram_size, ram_size,
490 // openpic = openpic_init(0x00000000, 0xF0000000, 1);
491 // pic_init(openpic);
493 // pit = pit_init(0x40, 0);
495 fd = serial_open_device();
496 serial_init(0x3f8, 4, fd);
498 if (nb_nics1 > NE2000_NB_MAX)
499 nb_nics1 = NE2000_NB_MAX;
500 for(i = 0; i < nb_nics1; i++) {
501 isa_ne2000_init(ne2000_io[i], ne2000_irq[i], &nd_table[i]);
504 for(i = 0; i < 2; i++) {
505 isa_ide_init(ide_iobase[i], ide_iobase2[i], ide_irq[i],
506 bs_table[2 * i], bs_table[2 * i + 1]);
513 fdctrl_init(6, 2, 0, 0x3f0, fd_table);
515 /* Register speaker port */
516 register_ioport_read(0x61, 1, 1, speaker_ioport_read, NULL);
517 register_ioport_write(0x61, 1, 1, speaker_ioport_write, NULL);
518 /* Register fake IO ports for PREP */
519 register_ioport_read(0x398, 2, 1, &PREP_io_read, sysctrl);
520 register_ioport_write(0x398, 2, 1, &PREP_io_write, sysctrl);
521 /* System control ports */
522 register_ioport_read(0x0092, 0x01, 1, &PREP_io_800_readb, sysctrl);
523 register_ioport_write(0x0092, 0x01, 1, &PREP_io_800_writeb, sysctrl);
524 register_ioport_read(0x0800, 0x52, 1, &PREP_io_800_readb, sysctrl);
525 register_ioport_write(0x0800, 0x52, 1, &PREP_io_800_writeb, sysctrl);
526 /* PCI intack location */
527 PPC_io_memory = cpu_register_io_memory(0, PPC_intack_read,
528 PPC_intack_write, NULL);
529 cpu_register_physical_memory(0xBFFFFFF0, 0x4, PPC_io_memory);
530 /* PowerPC control and status register group */
532 PPC_io_memory = cpu_register_io_memory(0, PPC_XCSR_read, PPC_XCSR_write, NULL);
533 cpu_register_physical_memory(0xFEFF0000, 0x1000, PPC_io_memory);
536 nvram = m48t59_init(8, 0, 0x0074, NVRAM_SIZE);
539 sysctrl->nvram = nvram;
541 /* Initialise NVRAM */
542 PPC_NVRAM_set_params(nvram, NVRAM_SIZE, "PREP", ram_size, boot_device,
543 kernel_base, kernel_size,
545 initrd_base, initrd_size,
546 /* XXX: need an option to load a NVRAM image */
548 graphic_width, graphic_height, graphic_depth);