xref: /qemu/hw/ppc/amigaone.c (revision ec6f3fc3)
1 /*
2  * QEMU Eyetech AmigaOne/Mai Logic Teron emulation
3  *
4  * Copyright (c) 2023 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/units.h"
12 #include "qemu/datadir.h"
13 #include "qemu/log.h"
14 #include "qemu/error-report.h"
15 #include "qapi/error.h"
16 #include "hw/ppc/ppc.h"
17 #include "hw/boards.h"
18 #include "hw/loader.h"
19 #include "hw/pci-host/articia.h"
20 #include "hw/isa/vt82c686.h"
21 #include "hw/ide/pci.h"
22 #include "hw/i2c/smbus_eeprom.h"
23 #include "hw/ppc/ppc.h"
24 #include "sysemu/qtest.h"
25 #include "sysemu/reset.h"
26 #include "kvm_ppc.h"
27 
28 #define BUS_FREQ_HZ 100000000
29 
30 /*
31  * Firmware binary available at
32  * https://www.hyperion-entertainment.com/index.php/downloads?view=files&parent=28
33  * then "tail -c 524288 updater.image >u-boot-amigaone.bin"
34  *
35  * BIOS emulator in firmware cannot run QEMU vgabios and hangs on it, use
36  * -device VGA,romfile=VGABIOS-lgpl-latest.bin
37  * from http://www.nongnu.org/vgabios/ instead.
38  */
39 #define PROM_FILENAME "u-boot-amigaone.bin"
40 #define PROM_ADDR 0xfff00000
41 #define PROM_SIZE (512 * KiB)
42 
43 static void amigaone_cpu_reset(void *opaque)
44 {
45     PowerPCCPU *cpu = opaque;
46 
47     cpu_reset(CPU(cpu));
48     cpu_ppc_tb_reset(&cpu->env);
49 }
50 
51 static void fix_spd_data(uint8_t *spd)
52 {
53     uint32_t bank_size = 4 * MiB * spd[31];
54     uint32_t rows = bank_size / spd[13] / spd[17];
55     spd[3] = ctz32(rows) - spd[4];
56 }
57 
58 static void amigaone_init(MachineState *machine)
59 {
60     PowerPCCPU *cpu;
61     CPUPPCState *env;
62     MemoryRegion *rom, *pci_mem, *mr;
63     const char *fwname = machine->firmware ?: PROM_FILENAME;
64     char *filename;
65     ssize_t sz;
66     PCIBus *pci_bus;
67     Object *via;
68     DeviceState *dev;
69     I2CBus *i2c_bus;
70     uint8_t *spd_data;
71     int i;
72 
73     /* init CPU */
74     cpu = POWERPC_CPU(cpu_create(machine->cpu_type));
75     env = &cpu->env;
76     if (PPC_INPUT(env) != PPC_FLAGS_INPUT_6xx) {
77         error_report("Incompatible CPU, only 6xx bus supported");
78         exit(1);
79     }
80     cpu_ppc_tb_init(env, BUS_FREQ_HZ / 4);
81     qemu_register_reset(amigaone_cpu_reset, cpu);
82 
83     /* RAM */
84     if (machine->ram_size > 2 * GiB) {
85         error_report("RAM size more than 2 GiB is not supported");
86         exit(1);
87     }
88     memory_region_add_subregion(get_system_memory(), 0, machine->ram);
89     if (machine->ram_size < 1 * GiB + 32 * KiB) {
90         /* Firmware uses this area for startup */
91         mr = g_new(MemoryRegion, 1);
92         memory_region_init_ram(mr, NULL, "init-cache", 32 * KiB, &error_fatal);
93         memory_region_add_subregion(get_system_memory(), 0x40000000, mr);
94     }
95 
96     /* allocate and load firmware */
97     filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, fwname);
98     if (filename) {
99         rom = g_new(MemoryRegion, 1);
100         memory_region_init_rom(rom, NULL, "rom", PROM_SIZE, &error_fatal);
101         memory_region_add_subregion(get_system_memory(), PROM_ADDR, rom);
102         sz = load_image_targphys(filename, PROM_ADDR, PROM_SIZE);
103         if (sz <= 0 || sz > PROM_SIZE) {
104             error_report("Could not load firmware '%s'", filename);
105             exit(1);
106         }
107         g_free(filename);
108     } else if (!qtest_enabled()) {
109         error_report("Could not find firmware '%s'", fwname);
110         exit(1);
111     }
112 
113     /* Articia S */
114     dev = sysbus_create_simple(TYPE_ARTICIA, 0xfe000000, NULL);
115 
116     i2c_bus = I2C_BUS(qdev_get_child_bus(dev, "smbus"));
117     if (machine->ram_size > 512 * MiB) {
118         spd_data = spd_data_generate(SDR, machine->ram_size / 2);
119     } else {
120         spd_data = spd_data_generate(SDR, machine->ram_size);
121     }
122     fix_spd_data(spd_data);
123     smbus_eeprom_init_one(i2c_bus, 0x51, spd_data);
124     if (machine->ram_size > 512 * MiB) {
125         smbus_eeprom_init_one(i2c_bus, 0x52, spd_data);
126     }
127 
128     pci_mem = sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 1);
129     mr = g_new(MemoryRegion, 1);
130     memory_region_init_alias(mr, OBJECT(dev), "pci-mem-low", pci_mem,
131                              0, 0x1000000);
132     memory_region_add_subregion(get_system_memory(), 0xfd000000, mr);
133     mr = g_new(MemoryRegion, 1);
134     memory_region_init_alias(mr, OBJECT(dev), "pci-mem-high", pci_mem,
135                              0x80000000, 0x7d000000);
136     memory_region_add_subregion(get_system_memory(), 0x80000000, mr);
137     pci_bus = PCI_BUS(qdev_get_child_bus(dev, "pci.0"));
138 
139     /* VIA VT82c686B South Bridge (multifunction PCI device) */
140     via = OBJECT(pci_create_simple_multifunction(pci_bus, PCI_DEVFN(7, 0),
141                                                  TYPE_VT82C686B_ISA));
142     object_property_add_alias(OBJECT(machine), "rtc-time",
143                               object_resolve_path_component(via, "rtc"),
144                               "date");
145     qdev_connect_gpio_out(DEVICE(via), 0,
146                           qdev_get_gpio_in(DEVICE(cpu), PPC6xx_INPUT_INT));
147     for (i = 0; i < PCI_NUM_PINS; i++) {
148         qdev_connect_gpio_out(dev, i, qdev_get_gpio_in_named(DEVICE(via),
149                                                              "pirq", i));
150     }
151     pci_ide_create_devs(PCI_DEVICE(object_resolve_path_component(via, "ide")));
152     pci_vga_init(pci_bus);
153 }
154 
155 static void amigaone_machine_init(MachineClass *mc)
156 {
157     mc->desc = "Eyetech AmigaOne/Mai Logic Teron";
158     mc->init = amigaone_init;
159     mc->block_default_type = IF_IDE;
160     mc->default_cpu_type = POWERPC_CPU_TYPE_NAME("7457_v1.2");
161     mc->default_display = "std";
162     mc->default_ram_id = "ram";
163     mc->default_ram_size = 512 * MiB;
164 }
165 
166 DEFINE_MACHINE("amigaone", amigaone_machine_init)
167