]> git.proxmox.com Git - mirror_qemu.git/blame - hw/arm/highbank.c
Merge tag 'pull-loongarch-20221215' of https://gitlab.com/gaosong/qemu into staging
[mirror_qemu.git] / hw / arm / highbank.c
CommitLineData
2488514c
RH
1/*
2 * Calxeda Highbank SoC emulation
3 *
4 * Copyright (c) 2010-2012 Calxeda
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2 or later, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 */
19
12b16722 20#include "qemu/osdep.h"
2c65db5e 21#include "qemu/datadir.h"
da34e65c 22#include "qapi/error.h"
83c9f4ca 23#include "hw/sysbus.h"
d6454270 24#include "migration/vmstate.h"
12ec8bd5 25#include "hw/arm/boot.h"
83c9f4ca 26#include "hw/loader.h"
1422e32d 27#include "net/net.h"
54d31236 28#include "sysemu/runstate.h"
9c17d615 29#include "sysemu/sysemu.h"
83c9f4ca 30#include "hw/boards.h"
f282f296 31#include "qemu/error-report.h"
f0d1d2c1 32#include "hw/char/pl011.h"
c2de81e2
PMD
33#include "hw/ide/ahci.h"
34#include "hw/cpu/a9mpcore.h"
35#include "hw/cpu/a15mpcore.h"
c5c752af 36#include "qemu/log.h"
db1015e9 37#include "qom/object.h"
416dd952 38#include "cpu.h"
2488514c 39
e2cddeeb
PC
40#define SMP_BOOT_ADDR 0x100
41#define SMP_BOOT_REG 0x40
42#define MPCORE_PERIPHBASE 0xfff10000
2488514c 43
40340e5f 44#define MVBAR_ADDR 0x200
716536a9 45#define BOARD_SETUP_ADDR (MVBAR_ADDR + 8 * sizeof(uint32_t))
40340e5f 46
e2cddeeb 47#define NIRQ_GIC 160
2488514c
RH
48
49/* Board init. */
2488514c 50
2488514c 51#define NUM_REGS 0x200
a8170e5e 52static void hb_regs_write(void *opaque, hwaddr offset,
2488514c
RH
53 uint64_t value, unsigned size)
54{
55 uint32_t *regs = opaque;
56
57 if (offset == 0xf00) {
58 if (value == 1 || value == 2) {
cf83f140 59 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
2488514c 60 } else if (value == 3) {
cf83f140 61 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
2488514c
RH
62 }
63 }
64
c5c752af
PP
65 if (offset / 4 >= NUM_REGS) {
66 qemu_log_mask(LOG_GUEST_ERROR,
67 "highbank: bad write offset 0x%" HWADDR_PRIx "\n", offset);
68 return;
69 }
70 regs[offset / 4] = value;
2488514c
RH
71}
72
a8170e5e 73static uint64_t hb_regs_read(void *opaque, hwaddr offset,
2488514c
RH
74 unsigned size)
75{
c5c752af 76 uint32_t value;
2488514c 77 uint32_t *regs = opaque;
c5c752af
PP
78
79 if (offset / 4 >= NUM_REGS) {
80 qemu_log_mask(LOG_GUEST_ERROR,
81 "highbank: bad read offset 0x%" HWADDR_PRIx "\n", offset);
82 return 0;
83 }
84 value = regs[offset / 4];
2488514c
RH
85
86 if ((offset == 0x100) || (offset == 0x108) || (offset == 0x10C)) {
87 value |= 0x30000000;
88 }
89
90 return value;
91}
92
93static const MemoryRegionOps hb_mem_ops = {
94 .read = hb_regs_read,
95 .write = hb_regs_write,
96 .endianness = DEVICE_NATIVE_ENDIAN,
97};
98
426533fa 99#define TYPE_HIGHBANK_REGISTERS "highbank-regs"
8063396b 100OBJECT_DECLARE_SIMPLE_TYPE(HighbankRegsState, HIGHBANK_REGISTERS)
426533fa 101
db1015e9 102struct HighbankRegsState {
426533fa
AF
103 /*< private >*/
104 SysBusDevice parent_obj;
105 /*< public >*/
106
112f2ac9 107 MemoryRegion iomem;
2488514c 108 uint32_t regs[NUM_REGS];
db1015e9 109};
2488514c 110
cfa52e09 111static const VMStateDescription vmstate_highbank_regs = {
2488514c
RH
112 .name = "highbank-regs",
113 .version_id = 0,
114 .minimum_version_id = 0,
2488514c
RH
115 .fields = (VMStateField[]) {
116 VMSTATE_UINT32_ARRAY(regs, HighbankRegsState, NUM_REGS),
117 VMSTATE_END_OF_LIST(),
118 },
119};
120
121static void highbank_regs_reset(DeviceState *dev)
122{
426533fa 123 HighbankRegsState *s = HIGHBANK_REGISTERS(dev);
2488514c
RH
124
125 s->regs[0x40] = 0x05F20121;
126 s->regs[0x41] = 0x2;
127 s->regs[0x42] = 0x05F30121;
128 s->regs[0x43] = 0x05F40121;
129}
130
ff7a27c1 131static void highbank_regs_init(Object *obj)
2488514c 132{
ff7a27c1
XZ
133 HighbankRegsState *s = HIGHBANK_REGISTERS(obj);
134 SysBusDevice *dev = SYS_BUS_DEVICE(obj);
2488514c 135
ff7a27c1 136 memory_region_init_io(&s->iomem, obj, &hb_mem_ops, s->regs,
64bde0f3 137 "highbank_regs", 0x1000);
112f2ac9 138 sysbus_init_mmio(dev, &s->iomem);
2488514c
RH
139}
140
999e12bb
AL
141static void highbank_regs_class_init(ObjectClass *klass, void *data)
142{
39bffca2 143 DeviceClass *dc = DEVICE_CLASS(klass);
999e12bb 144
39bffca2
AL
145 dc->desc = "Calxeda Highbank registers";
146 dc->vmsd = &vmstate_highbank_regs;
147 dc->reset = highbank_regs_reset;
999e12bb
AL
148}
149
8c43a6f0 150static const TypeInfo highbank_regs_info = {
426533fa 151 .name = TYPE_HIGHBANK_REGISTERS,
39bffca2
AL
152 .parent = TYPE_SYS_BUS_DEVICE,
153 .instance_size = sizeof(HighbankRegsState),
ff7a27c1 154 .instance_init = highbank_regs_init,
39bffca2 155 .class_init = highbank_regs_class_init,
2488514c
RH
156};
157
83f7d43a 158static void highbank_regs_register_types(void)
2488514c 159{
39bffca2 160 type_register_static(&highbank_regs_info);
2488514c
RH
161}
162
83f7d43a 163type_init(highbank_regs_register_types)
2488514c
RH
164
165static struct arm_boot_info highbank_binfo;
166
574f66bc
AP
167enum cxmachines {
168 CALXEDA_HIGHBANK,
b25a83f0 169 CALXEDA_MIDWAY,
574f66bc
AP
170};
171
2488514c
RH
172/* ram_size must be set to match the upper bound of memory in the
173 * device tree (linux/arch/arm/boot/dts/highbank.dts), which is
174 * normally 0xff900000 or -m 4089. When running this board on a
175 * 32-bit host, set the reg value of memory to 0xf7ff00000 in the
176 * device tree and pass -m 2047 to QEMU.
177 */
3ef96221 178static void calxeda_init(MachineState *machine, enum cxmachines machine_id)
2488514c 179{
574f66bc 180 DeviceState *dev = NULL;
2488514c 181 SysBusDevice *busdev;
2488514c
RH
182 qemu_irq pic[128];
183 int n;
cc7d44c2 184 unsigned int smp_cpus = machine->smp.cpus;
2488514c 185 qemu_irq cpu_irq[4];
5ae79fe8 186 qemu_irq cpu_fiq[4];
582c8f75
PM
187 qemu_irq cpu_virq[4];
188 qemu_irq cpu_vfiq[4];
2488514c 189 MemoryRegion *sysram;
2488514c
RH
190 MemoryRegion *sysmem;
191 char *sysboot_filename;
192
dca6eeed
PC
193 switch (machine_id) {
194 case CALXEDA_HIGHBANK:
ba1ba5cc 195 machine->cpu_type = ARM_CPU_TYPE_NAME("cortex-a9");
dca6eeed
PC
196 break;
197 case CALXEDA_MIDWAY:
ba1ba5cc 198 machine->cpu_type = ARM_CPU_TYPE_NAME("cortex-a15");
dca6eeed 199 break;
ba1ba5cc
IM
200 default:
201 assert(0);
2488514c
RH
202 }
203
204 for (n = 0; n < smp_cpus; n++) {
d097696e 205 Object *cpuobj;
c5fad12f 206 ARMCPU *cpu;
f282f296 207
ba1ba5cc 208 cpuobj = object_new(machine->cpu_type);
d097696e 209 cpu = ARM_CPU(cpuobj);
f282f296 210
5325cc34
MA
211 object_property_set_int(cpuobj, "psci-conduit", QEMU_PSCI_CONDUIT_SMC,
212 &error_abort);
40340e5f 213
efba1595 214 if (object_property_find(cpuobj, "reset-cbar")) {
5325cc34
MA
215 object_property_set_int(cpuobj, "reset-cbar", MPCORE_PERIPHBASE,
216 &error_abort);
c0f1ead9 217 }
ce189ab2 218 qdev_realize(DEVICE(cpuobj), NULL, &error_fatal);
9188dbf7 219 cpu_irq[n] = qdev_get_gpio_in(DEVICE(cpu), ARM_CPU_IRQ);
5ae79fe8 220 cpu_fiq[n] = qdev_get_gpio_in(DEVICE(cpu), ARM_CPU_FIQ);
582c8f75
PM
221 cpu_virq[n] = qdev_get_gpio_in(DEVICE(cpu), ARM_CPU_VIRQ);
222 cpu_vfiq[n] = qdev_get_gpio_in(DEVICE(cpu), ARM_CPU_VFIQ);
2488514c
RH
223 }
224
225 sysmem = get_system_memory();
2488514c 226 /* SDRAM at address zero. */
89c43bdf 227 memory_region_add_subregion(sysmem, 0, machine->ram);
2488514c
RH
228
229 sysram = g_new(MemoryRegion, 1);
eb7d1f17 230 memory_region_init_ram(sysram, NULL, "highbank.sysram", 0x8000,
f8ed85ac 231 &error_fatal);
2488514c 232 memory_region_add_subregion(sysmem, 0xfff88000, sysram);
0ad3b5d3
PB
233 if (machine->firmware != NULL) {
234 sysboot_filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, machine->firmware);
2488514c 235 if (sysboot_filename != NULL) {
60ff4e63 236 if (load_image_targphys(sysboot_filename, 0xfff88000, 0x8000) < 0) {
0ad3b5d3 237 error_report("Unable to load %s", machine->firmware);
c525436e 238 exit(1);
2488514c 239 }
6e05a12f 240 g_free(sysboot_filename);
2488514c 241 } else {
0ad3b5d3 242 error_report("Unable to find %s", machine->firmware);
c525436e 243 exit(1);
2488514c
RH
244 }
245 }
246
3ef96221 247 switch (machine_id) {
574f66bc 248 case CALXEDA_HIGHBANK:
df707969 249 dev = qdev_new("l2x0");
b25a83f0 250 busdev = SYS_BUS_DEVICE(dev);
3c6ef471 251 sysbus_realize_and_unref(busdev, &error_fatal);
b25a83f0
AP
252 sysbus_mmio_map(busdev, 0, 0xfff12000);
253
df707969 254 dev = qdev_new(TYPE_A9MPCORE_PRIV);
574f66bc 255 break;
b25a83f0 256 case CALXEDA_MIDWAY:
df707969 257 dev = qdev_new(TYPE_A15MPCORE_PRIV);
b25a83f0 258 break;
574f66bc 259 }
2488514c
RH
260 qdev_prop_set_uint32(dev, "num-cpu", smp_cpus);
261 qdev_prop_set_uint32(dev, "num-irq", NIRQ_GIC);
1356b98d 262 busdev = SYS_BUS_DEVICE(dev);
3c6ef471 263 sysbus_realize_and_unref(busdev, &error_fatal);
e2cddeeb 264 sysbus_mmio_map(busdev, 0, MPCORE_PERIPHBASE);
2488514c
RH
265 for (n = 0; n < smp_cpus; n++) {
266 sysbus_connect_irq(busdev, n, cpu_irq[n]);
5ae79fe8 267 sysbus_connect_irq(busdev, n + smp_cpus, cpu_fiq[n]);
582c8f75
PM
268 sysbus_connect_irq(busdev, n + 2 * smp_cpus, cpu_virq[n]);
269 sysbus_connect_irq(busdev, n + 3 * smp_cpus, cpu_vfiq[n]);
2488514c
RH
270 }
271
272 for (n = 0; n < 128; n++) {
273 pic[n] = qdev_get_gpio_in(dev, n);
274 }
275
df707969 276 dev = qdev_new("sp804");
2488514c
RH
277 qdev_prop_set_uint32(dev, "freq0", 150000000);
278 qdev_prop_set_uint32(dev, "freq1", 150000000);
1356b98d 279 busdev = SYS_BUS_DEVICE(dev);
3c6ef471 280 sysbus_realize_and_unref(busdev, &error_fatal);
2488514c
RH
281 sysbus_mmio_map(busdev, 0, 0xfff34000);
282 sysbus_connect_irq(busdev, 0, pic[18]);
9bca0edb 283 pl011_create(0xfff36000, pic[20], serial_hd(0));
2488514c 284
df707969 285 dev = qdev_new(TYPE_HIGHBANK_REGISTERS);
1356b98d 286 busdev = SYS_BUS_DEVICE(dev);
3c6ef471 287 sysbus_realize_and_unref(busdev, &error_fatal);
2488514c
RH
288 sysbus_mmio_map(busdev, 0, 0xfff3c000);
289
290 sysbus_create_simple("pl061", 0xfff30000, pic[14]);
291 sysbus_create_simple("pl061", 0xfff31000, pic[15]);
292 sysbus_create_simple("pl061", 0xfff32000, pic[16]);
293 sysbus_create_simple("pl061", 0xfff33000, pic[17]);
294 sysbus_create_simple("pl031", 0xfff35000, pic[19]);
295 sysbus_create_simple("pl022", 0xfff39000, pic[23]);
296
c2de81e2 297 sysbus_create_simple(TYPE_SYSBUS_AHCI, 0xffe08000, pic[83]);
2488514c 298
a005d073 299 if (nd_table[0].used) {
2488514c 300 qemu_check_nic_model(&nd_table[0], "xgmac");
df707969 301 dev = qdev_new("xgmac");
2488514c 302 qdev_set_nic_properties(dev, &nd_table[0]);
3c6ef471 303 sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
1356b98d
AF
304 sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, 0xfff50000);
305 sysbus_connect_irq(SYS_BUS_DEVICE(dev), 0, pic[77]);
306 sysbus_connect_irq(SYS_BUS_DEVICE(dev), 1, pic[78]);
307 sysbus_connect_irq(SYS_BUS_DEVICE(dev), 2, pic[79]);
2488514c
RH
308
309 qemu_check_nic_model(&nd_table[1], "xgmac");
df707969 310 dev = qdev_new("xgmac");
2488514c 311 qdev_set_nic_properties(dev, &nd_table[1]);
3c6ef471 312 sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
1356b98d
AF
313 sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, 0xfff51000);
314 sysbus_connect_irq(SYS_BUS_DEVICE(dev), 0, pic[80]);
315 sysbus_connect_irq(SYS_BUS_DEVICE(dev), 1, pic[81]);
316 sysbus_connect_irq(SYS_BUS_DEVICE(dev), 2, pic[82]);
2488514c
RH
317 }
318
2a7ae4ee
MA
319 /* TODO create and connect IDE devices for ide_drive_get() */
320
89c43bdf 321 highbank_binfo.ram_size = machine->ram_size;
2488514c
RH
322 /* highbank requires a dtb in order to boot, and the dtb will override
323 * the board ID. The following value is ignored, so set it to -1 to be
324 * clear that the value is meaningless.
325 */
326 highbank_binfo.board_id = -1;
2488514c 327 highbank_binfo.loader_start = 0;
416dd952 328 highbank_binfo.board_setup_addr = BOARD_SETUP_ADDR;
33284d48 329 highbank_binfo.psci_conduit = QEMU_PSCI_CONDUIT_SMC;
40340e5f 330
2744ece8 331 arm_load_kernel(ARM_CPU(first_cpu), machine, &highbank_binfo);
2488514c
RH
332}
333
3ef96221 334static void highbank_init(MachineState *machine)
574f66bc 335{
3ef96221 336 calxeda_init(machine, CALXEDA_HIGHBANK);
574f66bc
AP
337}
338
3ef96221 339static void midway_init(MachineState *machine)
b25a83f0 340{
3ef96221 341 calxeda_init(machine, CALXEDA_MIDWAY);
b25a83f0
AP
342}
343
8a661aea 344static void highbank_class_init(ObjectClass *oc, void *data)
e264d29d 345{
8a661aea
AF
346 MachineClass *mc = MACHINE_CLASS(oc);
347
e264d29d
EH
348 mc->desc = "Calxeda Highbank (ECX-1000)";
349 mc->init = highbank_init;
2a7ae4ee
MA
350 mc->block_default_type = IF_IDE;
351 mc->units_per_default_bus = 1;
e264d29d 352 mc->max_cpus = 4;
4672cbd7 353 mc->ignore_memory_transaction_failures = true;
89c43bdf 354 mc->default_ram_id = "highbank.dram";
e264d29d 355}
2488514c 356
8a661aea
AF
357static const TypeInfo highbank_type = {
358 .name = MACHINE_TYPE_NAME("highbank"),
359 .parent = TYPE_MACHINE,
360 .class_init = highbank_class_init,
361};
b25a83f0 362
8a661aea 363static void midway_class_init(ObjectClass *oc, void *data)
2488514c 364{
8a661aea
AF
365 MachineClass *mc = MACHINE_CLASS(oc);
366
e264d29d
EH
367 mc->desc = "Calxeda Midway (ECX-2000)";
368 mc->init = midway_init;
2a7ae4ee
MA
369 mc->block_default_type = IF_IDE;
370 mc->units_per_default_bus = 1;
e264d29d 371 mc->max_cpus = 4;
4672cbd7 372 mc->ignore_memory_transaction_failures = true;
89c43bdf 373 mc->default_ram_id = "highbank.dram";
2488514c
RH
374}
375
8a661aea
AF
376static const TypeInfo midway_type = {
377 .name = MACHINE_TYPE_NAME("midway"),
378 .parent = TYPE_MACHINE,
379 .class_init = midway_class_init,
380};
381
382static void calxeda_machines_init(void)
383{
384 type_register_static(&highbank_type);
385 type_register_static(&midway_type);
386}
387
0e6aac87 388type_init(calxeda_machines_init)