xref: /openbmc/qemu/hw/ppc/pegasos2.c (revision 278f064e)
1 /*
2  * QEMU PowerPC CHRP (Genesi/bPlan Pegasos II) hardware System Emulator
3  *
4  * Copyright (c) 2018-2020 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 static void pegasos2_cpu_reset(void *opaque)
45 {
46     PowerPCCPU *cpu = opaque;
47 
48     cpu_reset(CPU(cpu));
49     cpu->env.spr[SPR_HID1] = 7ULL << 28;
50 }
51 
52 static void pegasos2_init(MachineState *machine)
53 {
54     PowerPCCPU *cpu = NULL;
55     MemoryRegion *rom = g_new(MemoryRegion, 1);
56     DeviceState *mv;
57     PCIBus *pci_bus;
58     PCIDevice *dev;
59     I2CBus *i2c_bus;
60     const char *fwname = machine->firmware ?: PROM_FILENAME;
61     char *filename;
62     int sz;
63     uint8_t *spd_data;
64 
65     /* init CPU */
66     cpu = POWERPC_CPU(cpu_create(machine->cpu_type));
67     if (PPC_INPUT(&cpu->env) != PPC_FLAGS_INPUT_6xx) {
68         error_report("Incompatible CPU, only 6xx bus supported");
69         exit(1);
70     }
71 
72     /* Set time-base frequency */
73     cpu_ppc_tb_init(&cpu->env, BUS_FREQ_HZ / 4);
74     qemu_register_reset(pegasos2_cpu_reset, cpu);
75 
76     /* RAM */
77     memory_region_add_subregion(get_system_memory(), 0, machine->ram);
78 
79     /* allocate and load firmware */
80     filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, fwname);
81     if (!filename) {
82         error_report("Could not find firmware '%s'", fwname);
83         exit(1);
84     }
85     memory_region_init_rom(rom, NULL, "pegasos2.rom", PROM_SIZE, &error_fatal);
86     memory_region_add_subregion(get_system_memory(), PROM_ADDR, rom);
87     sz = load_elf(filename, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 1,
88                   PPC_ELF_MACHINE, 0, 0);
89     if (sz <= 0) {
90         sz = load_image_targphys(filename, PROM_ADDR, PROM_SIZE);
91     }
92     if (sz <= 0 || sz > PROM_SIZE) {
93         error_report("Could not load firmware '%s'", filename);
94         exit(1);
95     }
96     g_free(filename);
97 
98     /* Marvell Discovery II system controller */
99     mv = DEVICE(sysbus_create_simple(TYPE_MV64361, -1,
100                         ((qemu_irq *)cpu->env.irq_inputs)[PPC6xx_INPUT_INT]));
101     pci_bus = mv64361_get_pci_bus(mv, 1);
102 
103     /* VIA VT8231 South Bridge (multifunction PCI device) */
104     /* VT8231 function 0: PCI-to-ISA Bridge */
105     dev = pci_create_simple_multifunction(pci_bus, PCI_DEVFN(12, 0), true,
106                                           TYPE_VT8231_ISA);
107     qdev_connect_gpio_out(DEVICE(dev), 0,
108                           qdev_get_gpio_in_named(mv, "gpp", 31));
109 
110     /* VT8231 function 1: IDE Controller */
111     dev = pci_create_simple(pci_bus, PCI_DEVFN(12, 1), "via-ide");
112     pci_ide_create_devs(dev);
113 
114     /* VT8231 function 2-3: USB Ports */
115     pci_create_simple(pci_bus, PCI_DEVFN(12, 2), "vt82c686b-usb-uhci");
116     pci_create_simple(pci_bus, PCI_DEVFN(12, 3), "vt82c686b-usb-uhci");
117 
118     /* VT8231 function 4: Power Management Controller */
119     dev = pci_create_simple(pci_bus, PCI_DEVFN(12, 4), TYPE_VT8231_PM);
120     i2c_bus = I2C_BUS(qdev_get_child_bus(DEVICE(dev), "i2c"));
121     spd_data = spd_data_generate(DDR, machine->ram_size);
122     smbus_eeprom_init_one(i2c_bus, 0x57, spd_data);
123 
124     /* VT8231 function 5-6: AC97 Audio & Modem */
125     pci_create_simple(pci_bus, PCI_DEVFN(12, 5), TYPE_VIA_AC97);
126     pci_create_simple(pci_bus, PCI_DEVFN(12, 6), TYPE_VIA_MC97);
127 
128     /* other PC hardware */
129     pci_vga_init(pci_bus);
130 }
131 
132 static void pegasos2_machine(MachineClass *mc)
133 {
134     mc->desc = "Genesi/bPlan Pegasos II";
135     mc->init = pegasos2_init;
136     mc->block_default_type = IF_IDE;
137     mc->default_boot_order = "cd";
138     mc->default_display = "std";
139     mc->default_cpu_type = POWERPC_CPU_TYPE_NAME("7400_v2.9");
140     mc->default_ram_id = "pegasos2.ram";
141     mc->default_ram_size = 512 * MiB;
142 }
143 
144 DEFINE_MACHINE("pegasos2", pegasos2_machine)
145