xref: /openbmc/qemu/hw/ppc/amigaone.c (revision d349d5ab)
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_ADDR 0xfff00000
40 #define PROM_SIZE (512 * KiB)
41 
42 /* AmigaOS calls this routine from ROM, use this if no firmware loaded */
43 static const char dummy_fw[] = {
44     0x38, 0x00, 0x00, 0x08, /* li      r0,8 */
45     0x7c, 0x09, 0x03, 0xa6, /* mtctr   r0 */
46     0x54, 0x63, 0xf8, 0x7e, /* srwi    r3,r3,1 */
47     0x42, 0x00, 0xff, 0xfc, /* bdnz    0x8 */
48     0x7c, 0x63, 0x18, 0xf8, /* not     r3,r3 */
49     0x4e, 0x80, 0x00, 0x20, /* blr */
50 };
51 
52 static void amigaone_cpu_reset(void *opaque)
53 {
54     PowerPCCPU *cpu = opaque;
55 
56     cpu_reset(CPU(cpu));
57     cpu_ppc_tb_reset(&cpu->env);
58 }
59 
60 static void fix_spd_data(uint8_t *spd)
61 {
62     uint32_t bank_size = 4 * MiB * spd[31];
63     uint32_t rows = bank_size / spd[13] / spd[17];
64     spd[3] = ctz32(rows) - spd[4];
65 }
66 
67 static void amigaone_init(MachineState *machine)
68 {
69     PowerPCCPU *cpu;
70     CPUPPCState *env;
71     MemoryRegion *rom, *pci_mem, *mr;
72     ssize_t sz;
73     PCIBus *pci_bus;
74     Object *via;
75     DeviceState *dev;
76     I2CBus *i2c_bus;
77     uint8_t *spd_data;
78     int i;
79 
80     /* init CPU */
81     cpu = POWERPC_CPU(cpu_create(machine->cpu_type));
82     env = &cpu->env;
83     if (PPC_INPUT(env) != PPC_FLAGS_INPUT_6xx) {
84         error_report("Incompatible CPU, only 6xx bus supported");
85         exit(1);
86     }
87     cpu_ppc_tb_init(env, BUS_FREQ_HZ / 4);
88     qemu_register_reset(amigaone_cpu_reset, cpu);
89 
90     /* RAM */
91     if (machine->ram_size > 2 * GiB) {
92         error_report("RAM size more than 2 GiB is not supported");
93         exit(1);
94     }
95     memory_region_add_subregion(get_system_memory(), 0, machine->ram);
96     if (machine->ram_size < 1 * GiB + 32 * KiB) {
97         /* Firmware uses this area for startup */
98         mr = g_new(MemoryRegion, 1);
99         memory_region_init_ram(mr, NULL, "init-cache", 32 * KiB, &error_fatal);
100         memory_region_add_subregion(get_system_memory(), 0x40000000, mr);
101     }
102 
103     /* allocate and load firmware */
104     rom = g_new(MemoryRegion, 1);
105     memory_region_init_rom(rom, NULL, "rom", PROM_SIZE, &error_fatal);
106     memory_region_add_subregion(get_system_memory(), PROM_ADDR, rom);
107     if (!machine->firmware) {
108         rom_add_blob_fixed("dummy-fw", dummy_fw, sizeof(dummy_fw),
109                            PROM_ADDR + PROM_SIZE - 0x80);
110     } else {
111         g_autofree char *filename = qemu_find_file(QEMU_FILE_TYPE_BIOS,
112                                                    machine->firmware);
113         if (!filename) {
114             error_report("Could not find firmware '%s'", machine->firmware);
115             exit(1);
116         }
117         sz = load_image_targphys(filename, PROM_ADDR, PROM_SIZE);
118         if (sz <= 0 || sz > PROM_SIZE) {
119             error_report("Could not load firmware '%s'", filename);
120             exit(1);
121         }
122     }
123 
124     /* Articia S */
125     dev = sysbus_create_simple(TYPE_ARTICIA, 0xfe000000, NULL);
126 
127     i2c_bus = I2C_BUS(qdev_get_child_bus(dev, "smbus"));
128     if (machine->ram_size > 512 * MiB) {
129         spd_data = spd_data_generate(SDR, machine->ram_size / 2);
130     } else {
131         spd_data = spd_data_generate(SDR, machine->ram_size);
132     }
133     fix_spd_data(spd_data);
134     smbus_eeprom_init_one(i2c_bus, 0x51, spd_data);
135     if (machine->ram_size > 512 * MiB) {
136         smbus_eeprom_init_one(i2c_bus, 0x52, spd_data);
137     }
138 
139     pci_mem = sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 1);
140     mr = g_new(MemoryRegion, 1);
141     memory_region_init_alias(mr, OBJECT(dev), "pci-mem-low", pci_mem,
142                              0, 0x1000000);
143     memory_region_add_subregion(get_system_memory(), 0xfd000000, mr);
144     mr = g_new(MemoryRegion, 1);
145     memory_region_init_alias(mr, OBJECT(dev), "pci-mem-high", pci_mem,
146                              0x80000000, 0x7d000000);
147     memory_region_add_subregion(get_system_memory(), 0x80000000, mr);
148     pci_bus = PCI_BUS(qdev_get_child_bus(dev, "pci.0"));
149 
150     /* VIA VT82c686B South Bridge (multifunction PCI device) */
151     via = OBJECT(pci_create_simple_multifunction(pci_bus, PCI_DEVFN(7, 0),
152                                                  TYPE_VT82C686B_ISA));
153     object_property_add_alias(OBJECT(machine), "rtc-time",
154                               object_resolve_path_component(via, "rtc"),
155                               "date");
156     qdev_connect_gpio_out(DEVICE(via), 0,
157                           qdev_get_gpio_in(DEVICE(cpu), PPC6xx_INPUT_INT));
158     for (i = 0; i < PCI_NUM_PINS; i++) {
159         qdev_connect_gpio_out(dev, i, qdev_get_gpio_in_named(DEVICE(via),
160                                                              "pirq", i));
161     }
162     pci_ide_create_devs(PCI_DEVICE(object_resolve_path_component(via, "ide")));
163     pci_vga_init(pci_bus);
164 }
165 
166 static void amigaone_machine_init(MachineClass *mc)
167 {
168     mc->desc = "Eyetech AmigaOne/Mai Logic Teron";
169     mc->init = amigaone_init;
170     mc->block_default_type = IF_IDE;
171     mc->default_cpu_type = POWERPC_CPU_TYPE_NAME("7457_v1.2");
172     mc->default_display = "std";
173     mc->default_ram_id = "ram";
174     mc->default_ram_size = 512 * MiB;
175 }
176 
177 DEFINE_MACHINE("amigaone", amigaone_machine_init)
178