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
24 #include "qemu/osdep.h"
27 #include "hw/timer/m48t59.h"
28 #include "hw/i386/pc.h"
29 #include "hw/char/serial.h"
30 #include "hw/block/fdc.h"
32 #include "sysemu/sysemu.h"
33 #include "hw/isa/isa.h"
34 #include "hw/pci/pci.h"
35 #include "hw/pci/pci_host.h"
36 #include "hw/ppc/ppc.h"
37 #include "hw/boards.h"
38 #include "qemu/error-report.h"
41 #include "hw/loader.h"
42 #include "hw/timer/mc146818rtc.h"
43 #include "hw/isa/pc87312.h"
44 #include "sysemu/block-backend.h"
45 #include "sysemu/arch_init.h"
46 #include "sysemu/qtest.h"
47 #include "exec/address-spaces.h"
50 #include "qemu/cutils.h"
52 /* SMP is not enabled, for now */
57 #define BIOS_SIZE (1024 * 1024)
58 #define BIOS_FILENAME "ppc_rom.bin"
59 #define KERNEL_LOAD_ADDR 0x01000000
60 #define INITRD_LOAD_ADDR 0x01800000
62 /* Constants for devices init */
63 static const int ide_iobase
[2] = { 0x1f0, 0x170 };
64 static const int ide_iobase2
[2] = { 0x3f6, 0x376 };
65 static const int ide_irq
[2] = { 13, 13 };
67 #define NE2000_NB_MAX 6
69 static uint32_t ne2000_io
[NE2000_NB_MAX
] = { 0x300, 0x320, 0x340, 0x360, 0x280, 0x380 };
70 static int ne2000_irq
[NE2000_NB_MAX
] = { 9, 10, 11, 3, 4, 5 };
72 /* ISA IO ports bridge */
73 #define PPC_IO_BASE 0x80000000
75 /* PowerPC control and status registers */
81 /* Control and status */
86 /* General purpose registers */
99 /* Error diagnostic */
102 static void PPC_XCSR_writeb (void *opaque
,
103 hwaddr addr
, uint32_t value
)
105 printf("%s: 0x" TARGET_FMT_plx
" => 0x%08" PRIx32
"\n", __func__
, addr
,
109 static void PPC_XCSR_writew (void *opaque
,
110 hwaddr addr
, uint32_t value
)
112 printf("%s: 0x" TARGET_FMT_plx
" => 0x%08" PRIx32
"\n", __func__
, addr
,
116 static void PPC_XCSR_writel (void *opaque
,
117 hwaddr addr
, uint32_t value
)
119 printf("%s: 0x" TARGET_FMT_plx
" => 0x%08" PRIx32
"\n", __func__
, addr
,
123 static uint32_t PPC_XCSR_readb (void *opaque
, hwaddr addr
)
127 printf("%s: 0x" TARGET_FMT_plx
" <= %08" PRIx32
"\n", __func__
, addr
,
133 static uint32_t PPC_XCSR_readw (void *opaque
, hwaddr addr
)
137 printf("%s: 0x" TARGET_FMT_plx
" <= %08" PRIx32
"\n", __func__
, addr
,
143 static uint32_t PPC_XCSR_readl (void *opaque
, hwaddr addr
)
147 printf("%s: 0x" TARGET_FMT_plx
" <= %08" PRIx32
"\n", __func__
, addr
,
153 static const MemoryRegionOps PPC_XCSR_ops
= {
155 .read
= { PPC_XCSR_readb
, PPC_XCSR_readw
, PPC_XCSR_readl
, },
156 .write
= { PPC_XCSR_writeb
, PPC_XCSR_writew
, PPC_XCSR_writel
, },
158 .endianness
= DEVICE_LITTLE_ENDIAN
,
163 /* Fake super-io ports for PREP platform (Intel 82378ZB) */
164 typedef struct sysctrl_t
{
170 qemu_irq contiguous_map_irq
;
175 STATE_HARDFILE
= 0x01,
178 static sysctrl_t
*sysctrl
;
180 static void PREP_io_800_writeb (void *opaque
, uint32_t addr
, uint32_t val
)
182 sysctrl_t
*sysctrl
= opaque
;
184 trace_prep_io_800_writeb(addr
- PPC_IO_BASE
, val
);
187 /* Special port 92 */
188 /* Check soft reset asked */
190 qemu_irq_raise(sysctrl
->reset_irq
);
192 qemu_irq_lower(sysctrl
->reset_irq
);
202 /* Motorola CPU configuration register : read-only */
205 /* Motorola base module feature register : read-only */
208 /* Motorola base module status register : read-only */
211 /* Hardfile light register */
213 sysctrl
->state
|= STATE_HARDFILE
;
215 sysctrl
->state
&= ~STATE_HARDFILE
;
218 /* Password protect 1 register */
219 if (sysctrl
->nvram
!= NULL
) {
220 NvramClass
*k
= NVRAM_GET_CLASS(sysctrl
->nvram
);
221 (k
->toggle_lock
)(sysctrl
->nvram
, 1);
225 /* Password protect 2 register */
226 if (sysctrl
->nvram
!= NULL
) {
227 NvramClass
*k
= NVRAM_GET_CLASS(sysctrl
->nvram
);
228 (k
->toggle_lock
)(sysctrl
->nvram
, 2);
232 /* L2 invalidate register */
233 // tlb_flush(first_cpu, 1);
236 /* system control register */
237 sysctrl
->syscontrol
= val
& 0x0F;
240 /* I/O map type register */
241 sysctrl
->contiguous_map
= val
& 0x01;
242 qemu_set_irq(sysctrl
->contiguous_map_irq
, sysctrl
->contiguous_map
);
245 printf("ERROR: unaffected IO port write: %04" PRIx32
246 " => %02" PRIx32
"\n", addr
, val
);
251 static uint32_t PREP_io_800_readb (void *opaque
, uint32_t addr
)
253 sysctrl_t
*sysctrl
= opaque
;
254 uint32_t retval
= 0xFF;
258 /* Special port 92 */
259 retval
= sysctrl
->endian
<< 1;
262 /* Motorola CPU configuration register */
263 retval
= 0xEF; /* MPC750 */
266 /* Motorola Base module feature register */
267 retval
= 0xAD; /* No ESCC, PMC slot neither ethernet */
270 /* Motorola base module status register */
271 retval
= 0xE0; /* Standard MPC750 */
274 /* Equipment present register:
276 * no upgrade processor
277 * no cards in PCI slots
283 /* Motorola base module extended feature register */
284 retval
= 0x39; /* No USB, CF and PCI bridge. NVRAM present */
287 /* L2 invalidate: don't care */
294 /* system control register
295 * 7 - 6 / 1 - 0: L2 cache enable
297 retval
= sysctrl
->syscontrol
;
301 retval
= 0x03; /* no L2 cache */
304 /* I/O map type register */
305 retval
= sysctrl
->contiguous_map
;
308 printf("ERROR: unaffected IO port: %04" PRIx32
" read\n", addr
);
311 trace_prep_io_800_readb(addr
- PPC_IO_BASE
, retval
);
317 #define NVRAM_SIZE 0x2000
319 static void ppc_prep_reset(void *opaque
)
321 PowerPCCPU
*cpu
= opaque
;
326 static const MemoryRegionPortio prep_portio_list
[] = {
327 /* System control ports */
328 { 0x0092, 1, 1, .read
= PREP_io_800_readb
, .write
= PREP_io_800_writeb
, },
330 .read
= PREP_io_800_readb
, .write
= PREP_io_800_writeb
, },
331 /* Special port to get debug messages from Open-Firmware */
332 { 0x0F00, 4, 1, .write
= PPC_debug_write
, },
333 PORTIO_END_OF_LIST(),
336 static PortioList prep_port_list
;
338 /*****************************************************************************/
340 static inline uint32_t nvram_read(Nvram
*nvram
, uint32_t addr
)
342 NvramClass
*k
= NVRAM_GET_CLASS(sysctrl
->nvram
);
343 return (k
->read
)(nvram
, addr
);
346 static inline void nvram_write(Nvram
*nvram
, uint32_t addr
, uint32_t val
)
348 NvramClass
*k
= NVRAM_GET_CLASS(sysctrl
->nvram
);
349 (k
->write
)(nvram
, addr
, val
);
352 static void NVRAM_set_byte(Nvram
*nvram
, uint32_t addr
, uint8_t value
)
354 nvram_write(nvram
, addr
, value
);
357 static uint8_t NVRAM_get_byte(Nvram
*nvram
, uint32_t addr
)
359 return nvram_read(nvram
, addr
);
362 static void NVRAM_set_word(Nvram
*nvram
, uint32_t addr
, uint16_t value
)
364 nvram_write(nvram
, addr
, value
>> 8);
365 nvram_write(nvram
, addr
+ 1, value
& 0xFF);
368 static uint16_t NVRAM_get_word(Nvram
*nvram
, uint32_t addr
)
372 tmp
= nvram_read(nvram
, addr
) << 8;
373 tmp
|= nvram_read(nvram
, addr
+ 1);
378 static void NVRAM_set_lword(Nvram
*nvram
, uint32_t addr
, uint32_t value
)
380 nvram_write(nvram
, addr
, value
>> 24);
381 nvram_write(nvram
, addr
+ 1, (value
>> 16) & 0xFF);
382 nvram_write(nvram
, addr
+ 2, (value
>> 8) & 0xFF);
383 nvram_write(nvram
, addr
+ 3, value
& 0xFF);
386 static void NVRAM_set_string(Nvram
*nvram
, uint32_t addr
, const char *str
,
391 for (i
= 0; i
< max
&& str
[i
] != '\0'; i
++) {
392 nvram_write(nvram
, addr
+ i
, str
[i
]);
394 nvram_write(nvram
, addr
+ i
, str
[i
]);
395 nvram_write(nvram
, addr
+ max
- 1, '\0');
398 static uint16_t NVRAM_crc_update (uint16_t prev
, uint16_t value
)
401 uint16_t pd
, pd1
, pd2
;
406 pd2
= ((pd
>> 4) & 0x000F) ^ pd1
;
407 tmp
^= (pd1
<< 3) | (pd1
<< 8);
408 tmp
^= pd2
| (pd2
<< 7) | (pd2
<< 12);
413 static uint16_t NVRAM_compute_crc (Nvram
*nvram
, uint32_t start
, uint32_t count
)
416 uint16_t crc
= 0xFFFF;
421 for (i
= 0; i
!= count
; i
++) {
422 crc
= NVRAM_crc_update(crc
, NVRAM_get_word(nvram
, start
+ i
));
425 crc
= NVRAM_crc_update(crc
, NVRAM_get_byte(nvram
, start
+ i
) << 8);
431 #define CMDLINE_ADDR 0x017ff000
433 static int PPC_NVRAM_set_params (Nvram
*nvram
, uint16_t NVRAM_size
,
435 uint32_t RAM_size
, int boot_device
,
436 uint32_t kernel_image
, uint32_t kernel_size
,
438 uint32_t initrd_image
, uint32_t initrd_size
,
439 uint32_t NVRAM_image
,
440 int width
, int height
, int depth
)
444 /* Set parameters for Open Hack'Ware BIOS */
445 NVRAM_set_string(nvram
, 0x00, "QEMU_BIOS", 16);
446 NVRAM_set_lword(nvram
, 0x10, 0x00000002); /* structure v2 */
447 NVRAM_set_word(nvram
, 0x14, NVRAM_size
);
448 NVRAM_set_string(nvram
, 0x20, arch
, 16);
449 NVRAM_set_lword(nvram
, 0x30, RAM_size
);
450 NVRAM_set_byte(nvram
, 0x34, boot_device
);
451 NVRAM_set_lword(nvram
, 0x38, kernel_image
);
452 NVRAM_set_lword(nvram
, 0x3C, kernel_size
);
454 /* XXX: put the cmdline in NVRAM too ? */
455 pstrcpy_targphys("cmdline", CMDLINE_ADDR
, RAM_size
- CMDLINE_ADDR
,
457 NVRAM_set_lword(nvram
, 0x40, CMDLINE_ADDR
);
458 NVRAM_set_lword(nvram
, 0x44, strlen(cmdline
));
460 NVRAM_set_lword(nvram
, 0x40, 0);
461 NVRAM_set_lword(nvram
, 0x44, 0);
463 NVRAM_set_lword(nvram
, 0x48, initrd_image
);
464 NVRAM_set_lword(nvram
, 0x4C, initrd_size
);
465 NVRAM_set_lword(nvram
, 0x50, NVRAM_image
);
467 NVRAM_set_word(nvram
, 0x54, width
);
468 NVRAM_set_word(nvram
, 0x56, height
);
469 NVRAM_set_word(nvram
, 0x58, depth
);
470 crc
= NVRAM_compute_crc(nvram
, 0x00, 0xF8);
471 NVRAM_set_word(nvram
, 0xFC, crc
);
476 /* PowerPC PREP hardware initialisation */
477 static void ppc_prep_init(MachineState
*machine
)
479 ram_addr_t ram_size
= machine
->ram_size
;
480 const char *kernel_filename
= machine
->kernel_filename
;
481 const char *kernel_cmdline
= machine
->kernel_cmdline
;
482 const char *initrd_filename
= machine
->initrd_filename
;
483 const char *boot_device
= machine
->boot_order
;
484 MemoryRegion
*sysmem
= get_system_memory();
485 PowerPCCPU
*cpu
= NULL
;
486 CPUPPCState
*env
= NULL
;
489 MemoryRegion
*xcsr
= g_new(MemoryRegion
, 1);
491 int linux_boot
, i
, nb_nics1
;
492 MemoryRegion
*ram
= g_new(MemoryRegion
, 1);
493 uint32_t kernel_base
, initrd_base
;
494 long kernel_size
, initrd_size
;
496 PCIHostState
*pcihost
;
502 DriveInfo
*hd
[MAX_IDE_BUS
* MAX_IDE_DEVS
];
504 sysctrl
= g_malloc0(sizeof(sysctrl_t
));
506 linux_boot
= (kernel_filename
!= NULL
);
509 if (machine
->cpu_model
== NULL
)
510 machine
->cpu_model
= "602";
511 for (i
= 0; i
< smp_cpus
; i
++) {
512 cpu
= cpu_ppc_init(machine
->cpu_model
);
514 fprintf(stderr
, "Unable to find PowerPC CPU definition\n");
519 if (env
->flags
& POWERPC_FLAG_RTC_CLK
) {
520 /* POWER / PowerPC 601 RTC clock frequency is 7.8125 MHz */
521 cpu_ppc_tb_init(env
, 7812500UL);
523 /* Set time-base frequency to 100 Mhz */
524 cpu_ppc_tb_init(env
, 100UL * 1000UL * 1000UL);
526 qemu_register_reset(ppc_prep_reset
, cpu
);
530 memory_region_allocate_system_memory(ram
, NULL
, "ppc_prep.ram", ram_size
);
531 memory_region_add_subregion(sysmem
, 0, ram
);
534 kernel_base
= KERNEL_LOAD_ADDR
;
535 /* now we can load the kernel */
536 kernel_size
= load_image_targphys(kernel_filename
, kernel_base
,
537 ram_size
- kernel_base
);
538 if (kernel_size
< 0) {
539 error_report("could not load kernel '%s'", kernel_filename
);
543 if (initrd_filename
) {
544 initrd_base
= INITRD_LOAD_ADDR
;
545 initrd_size
= load_image_targphys(initrd_filename
, initrd_base
,
546 ram_size
- initrd_base
);
547 if (initrd_size
< 0) {
548 error_report("could not load initial ram disk '%s'",
556 ppc_boot_device
= 'm';
562 ppc_boot_device
= '\0';
563 /* For now, OHW cannot boot from the network. */
564 for (i
= 0; boot_device
[i
] != '\0'; i
++) {
565 if (boot_device
[i
] >= 'a' && boot_device
[i
] <= 'f') {
566 ppc_boot_device
= boot_device
[i
];
570 if (ppc_boot_device
== '\0') {
571 fprintf(stderr
, "No valid boot device for Mac99 machine\n");
576 if (PPC_INPUT(env
) != PPC_FLAGS_INPUT_6xx
) {
577 error_report("Only 6xx bus is supported on PREP machine");
581 dev
= qdev_create(NULL
, "raven-pcihost");
582 if (bios_name
== NULL
) {
583 bios_name
= BIOS_FILENAME
;
585 qdev_prop_set_string(dev
, "bios-name", bios_name
);
586 qdev_prop_set_uint32(dev
, "elf-machine", PPC_ELF_MACHINE
);
587 pcihost
= PCI_HOST_BRIDGE(dev
);
588 object_property_add_child(qdev_get_machine(), "raven", OBJECT(dev
), NULL
);
589 qdev_init_nofail(dev
);
590 pci_bus
= (PCIBus
*)qdev_get_child_bus(dev
, "pci.0");
591 if (pci_bus
== NULL
) {
592 fprintf(stderr
, "Couldn't create PCI host controller.\n");
595 sysctrl
->contiguous_map_irq
= qdev_get_gpio_in(dev
, 0);
597 /* PCI -> ISA bridge */
598 pci
= pci_create_simple(pci_bus
, PCI_DEVFN(1, 0), "i82378");
599 cpu
= POWERPC_CPU(first_cpu
);
600 qdev_connect_gpio_out(&pci
->qdev
, 0,
601 cpu
->env
.irq_inputs
[PPC6xx_INPUT_INT
]);
602 sysbus_connect_irq(&pcihost
->busdev
, 0, qdev_get_gpio_in(&pci
->qdev
, 9));
603 sysbus_connect_irq(&pcihost
->busdev
, 1, qdev_get_gpio_in(&pci
->qdev
, 11));
604 sysbus_connect_irq(&pcihost
->busdev
, 2, qdev_get_gpio_in(&pci
->qdev
, 9));
605 sysbus_connect_irq(&pcihost
->busdev
, 3, qdev_get_gpio_in(&pci
->qdev
, 11));
606 isa_bus
= ISA_BUS(qdev_get_child_bus(DEVICE(pci
), "isa.0"));
608 /* Super I/O (parallel + serial ports) */
609 isa
= isa_create(isa_bus
, TYPE_PC87312
);
611 qdev_prop_set_uint8(dev
, "config", 13); /* fdc, ser0, ser1, par0 */
612 qdev_init_nofail(dev
);
614 /* init basic PC hardware */
615 pci_vga_init(pci_bus
);
618 if (nb_nics1
> NE2000_NB_MAX
)
619 nb_nics1
= NE2000_NB_MAX
;
620 for(i
= 0; i
< nb_nics1
; i
++) {
621 if (nd_table
[i
].model
== NULL
) {
622 nd_table
[i
].model
= g_strdup("ne2k_isa");
624 if (strcmp(nd_table
[i
].model
, "ne2k_isa") == 0) {
625 isa_ne2000_init(isa_bus
, ne2000_io
[i
], ne2000_irq
[i
],
628 pci_nic_init_nofail(&nd_table
[i
], pci_bus
, "ne2k_pci", NULL
);
632 ide_drive_get(hd
, ARRAY_SIZE(hd
));
633 for(i
= 0; i
< MAX_IDE_BUS
; i
++) {
634 isa_ide_init(isa_bus
, ide_iobase
[i
], ide_iobase2
[i
], ide_irq
[i
],
638 isa_create_simple(isa_bus
, "i8042");
640 cpu
= POWERPC_CPU(first_cpu
);
641 sysctrl
->reset_irq
= cpu
->env
.irq_inputs
[PPC6xx_INPUT_HRESET
];
643 portio_list_init(&prep_port_list
, NULL
, prep_portio_list
, sysctrl
, "prep");
644 portio_list_add(&prep_port_list
, isa_address_space_io(isa
), 0x0);
646 /* PowerPC control and status register group */
648 memory_region_init_io(xcsr
, NULL
, &PPC_XCSR_ops
, NULL
, "ppc-xcsr", 0x1000);
649 memory_region_add_subregion(sysmem
, 0xFEFF0000, xcsr
);
652 if (machine_usb(machine
)) {
653 pci_create_simple(pci_bus
, -1, "pci-ohci");
656 m48t59
= m48t59_init_isa(isa_bus
, 0x0074, NVRAM_SIZE
, 2000, 59);
659 sysctrl
->nvram
= m48t59
;
661 /* Initialise NVRAM */
662 PPC_NVRAM_set_params(m48t59
, NVRAM_SIZE
, "PREP", ram_size
,
664 kernel_base
, kernel_size
,
666 initrd_base
, initrd_size
,
667 /* XXX: need an option to load a NVRAM image */
669 graphic_width
, graphic_height
, graphic_depth
);
672 static void prep_machine_init(MachineClass
*mc
)
674 mc
->desc
= "PowerPC PREP platform";
675 mc
->init
= ppc_prep_init
;
676 mc
->max_cpus
= MAX_CPUS
;
677 mc
->default_boot_order
= "cad";
680 DEFINE_MACHINE("prep", prep_machine_init
)