1 /* 2 * QEMU PowerPC CHRP (Genesi/bPlan Pegasos II) hardware System Emulator 3 * 4 * Copyright (c) 2018-2021 BALATON Zoltan 5 * 6 * This work is licensed under the GNU GPL license version 2 or later. 7 * 8 */ 9 10 #include "qemu/osdep.h" 11 #include "qemu-common.h" 12 #include "qemu/units.h" 13 #include "qapi/error.h" 14 #include "hw/hw.h" 15 #include "hw/ppc/ppc.h" 16 #include "hw/sysbus.h" 17 #include "hw/pci/pci_host.h" 18 #include "hw/irq.h" 19 #include "hw/pci-host/mv64361.h" 20 #include "hw/isa/vt82c686.h" 21 #include "hw/ide/pci.h" 22 #include "hw/i2c/smbus_eeprom.h" 23 #include "hw/qdev-properties.h" 24 #include "sysemu/reset.h" 25 #include "hw/boards.h" 26 #include "hw/loader.h" 27 #include "hw/fw-path-provider.h" 28 #include "elf.h" 29 #include "qemu/log.h" 30 #include "qemu/error-report.h" 31 #include "sysemu/kvm.h" 32 #include "kvm_ppc.h" 33 #include "exec/address-spaces.h" 34 #include "trace.h" 35 #include "qemu/datadir.h" 36 #include "sysemu/device_tree.h" 37 38 #define PROM_FILENAME "pegasos2.rom" 39 #define PROM_ADDR 0xfff00000 40 #define PROM_SIZE 0x80000 41 42 #define BUS_FREQ_HZ 133333333 43 44 #define TYPE_PEGASOS2_MACHINE MACHINE_TYPE_NAME("pegasos2") 45 OBJECT_DECLARE_TYPE(Pegasos2MachineState, MachineClass, PEGASOS2_MACHINE) 46 47 struct Pegasos2MachineState { 48 MachineState parent_obj; 49 PowerPCCPU *cpu; 50 DeviceState *mv; 51 }; 52 53 static void pegasos2_cpu_reset(void *opaque) 54 { 55 PowerPCCPU *cpu = opaque; 56 57 cpu_reset(CPU(cpu)); 58 cpu->env.spr[SPR_HID1] = 7ULL << 28; 59 } 60 61 static void pegasos2_init(MachineState *machine) 62 { 63 Pegasos2MachineState *pm = PEGASOS2_MACHINE(machine); 64 CPUPPCState *env; 65 MemoryRegion *rom = g_new(MemoryRegion, 1); 66 PCIBus *pci_bus; 67 PCIDevice *dev; 68 I2CBus *i2c_bus; 69 const char *fwname = machine->firmware ?: PROM_FILENAME; 70 char *filename; 71 int sz; 72 uint8_t *spd_data; 73 74 /* init CPU */ 75 pm->cpu = POWERPC_CPU(cpu_create(machine->cpu_type)); 76 env = &pm->cpu->env; 77 if (PPC_INPUT(env) != PPC_FLAGS_INPUT_6xx) { 78 error_report("Incompatible CPU, only 6xx bus supported"); 79 exit(1); 80 } 81 82 /* Set time-base frequency */ 83 cpu_ppc_tb_init(env, BUS_FREQ_HZ / 4); 84 qemu_register_reset(pegasos2_cpu_reset, pm->cpu); 85 86 /* RAM */ 87 memory_region_add_subregion(get_system_memory(), 0, machine->ram); 88 89 /* allocate and load firmware */ 90 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, fwname); 91 if (!filename) { 92 error_report("Could not find firmware '%s'", fwname); 93 exit(1); 94 } 95 memory_region_init_rom(rom, NULL, "pegasos2.rom", PROM_SIZE, &error_fatal); 96 memory_region_add_subregion(get_system_memory(), PROM_ADDR, rom); 97 sz = load_elf(filename, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 1, 98 PPC_ELF_MACHINE, 0, 0); 99 if (sz <= 0) { 100 sz = load_image_targphys(filename, PROM_ADDR, PROM_SIZE); 101 } 102 if (sz <= 0 || sz > PROM_SIZE) { 103 error_report("Could not load firmware '%s'", filename); 104 exit(1); 105 } 106 g_free(filename); 107 108 /* Marvell Discovery II system controller */ 109 pm->mv = DEVICE(sysbus_create_simple(TYPE_MV64361, -1, 110 ((qemu_irq *)env->irq_inputs)[PPC6xx_INPUT_INT])); 111 pci_bus = mv64361_get_pci_bus(pm->mv, 1); 112 113 /* VIA VT8231 South Bridge (multifunction PCI device) */ 114 /* VT8231 function 0: PCI-to-ISA Bridge */ 115 dev = pci_create_simple_multifunction(pci_bus, PCI_DEVFN(12, 0), true, 116 TYPE_VT8231_ISA); 117 qdev_connect_gpio_out(DEVICE(dev), 0, 118 qdev_get_gpio_in_named(pm->mv, "gpp", 31)); 119 120 /* VT8231 function 1: IDE Controller */ 121 dev = pci_create_simple(pci_bus, PCI_DEVFN(12, 1), "via-ide"); 122 pci_ide_create_devs(dev); 123 124 /* VT8231 function 2-3: USB Ports */ 125 pci_create_simple(pci_bus, PCI_DEVFN(12, 2), "vt82c686b-usb-uhci"); 126 pci_create_simple(pci_bus, PCI_DEVFN(12, 3), "vt82c686b-usb-uhci"); 127 128 /* VT8231 function 4: Power Management Controller */ 129 dev = pci_create_simple(pci_bus, PCI_DEVFN(12, 4), TYPE_VT8231_PM); 130 i2c_bus = I2C_BUS(qdev_get_child_bus(DEVICE(dev), "i2c")); 131 spd_data = spd_data_generate(DDR, machine->ram_size); 132 smbus_eeprom_init_one(i2c_bus, 0x57, spd_data); 133 134 /* VT8231 function 5-6: AC97 Audio & Modem */ 135 pci_create_simple(pci_bus, PCI_DEVFN(12, 5), TYPE_VIA_AC97); 136 pci_create_simple(pci_bus, PCI_DEVFN(12, 6), TYPE_VIA_MC97); 137 138 /* other PC hardware */ 139 pci_vga_init(pci_bus); 140 } 141 142 static void pegasos2_machine_class_init(ObjectClass *oc, void *data) 143 { 144 MachineClass *mc = MACHINE_CLASS(oc); 145 146 mc->desc = "Genesi/bPlan Pegasos II"; 147 mc->init = pegasos2_init; 148 mc->block_default_type = IF_IDE; 149 mc->default_boot_order = "cd"; 150 mc->default_display = "std"; 151 mc->default_cpu_type = POWERPC_CPU_TYPE_NAME("7400_v2.9"); 152 mc->default_ram_id = "pegasos2.ram"; 153 mc->default_ram_size = 512 * MiB; 154 } 155 156 static const TypeInfo pegasos2_machine_info = { 157 .name = TYPE_PEGASOS2_MACHINE, 158 .parent = TYPE_MACHINE, 159 .class_init = pegasos2_machine_class_init, 160 .instance_size = sizeof(Pegasos2MachineState), 161 }; 162 163 static void pegasos2_machine_register_types(void) 164 { 165 type_register_static(&pegasos2_machine_info); 166 } 167 168 type_init(pegasos2_machine_register_types) 169