xref: /openbmc/qemu/hw/arm/raspi4b.c (revision 393770d7)
17785e8eaSSergey Kambalin /*
27785e8eaSSergey Kambalin  * Raspberry Pi 4B emulation
37785e8eaSSergey Kambalin  *
47785e8eaSSergey Kambalin  * Copyright (C) 2022 Ovchinnikov Vitalii <vitalii.ovchinnikov@auriga.com>
57785e8eaSSergey Kambalin  *
67785e8eaSSergey Kambalin  * SPDX-License-Identifier: GPL-2.0-or-later
77785e8eaSSergey Kambalin  */
87785e8eaSSergey Kambalin 
97785e8eaSSergey Kambalin #include "qemu/osdep.h"
107785e8eaSSergey Kambalin #include "qemu/units.h"
117785e8eaSSergey Kambalin #include "qemu/cutils.h"
127785e8eaSSergey Kambalin #include "qapi/error.h"
137785e8eaSSergey Kambalin #include "qapi/visitor.h"
147785e8eaSSergey Kambalin #include "hw/arm/raspi_platform.h"
157785e8eaSSergey Kambalin #include "hw/display/bcm2835_fb.h"
167785e8eaSSergey Kambalin #include "hw/registerfields.h"
177785e8eaSSergey Kambalin #include "qemu/error-report.h"
187785e8eaSSergey Kambalin #include "sysemu/device_tree.h"
197785e8eaSSergey Kambalin #include "hw/boards.h"
207785e8eaSSergey Kambalin #include "hw/loader.h"
217785e8eaSSergey Kambalin #include "hw/arm/boot.h"
227785e8eaSSergey Kambalin #include "qom/object.h"
237785e8eaSSergey Kambalin #include "hw/arm/bcm2838.h"
24bf1da4b3SSergey Kambalin #include <libfdt.h>
257785e8eaSSergey Kambalin 
267785e8eaSSergey Kambalin #define TYPE_RASPI4B_MACHINE MACHINE_TYPE_NAME("raspi4b")
277785e8eaSSergey Kambalin OBJECT_DECLARE_SIMPLE_TYPE(Raspi4bMachineState, RASPI4B_MACHINE)
287785e8eaSSergey Kambalin 
297785e8eaSSergey Kambalin struct Raspi4bMachineState {
307785e8eaSSergey Kambalin     RaspiBaseMachineState parent_obj;
317785e8eaSSergey Kambalin     BCM2838State soc;
327785e8eaSSergey Kambalin };
337785e8eaSSergey Kambalin 
34bf1da4b3SSergey Kambalin /*
35bf1da4b3SSergey Kambalin  * Add second memory region if board RAM amount exceeds VC base address
36bf1da4b3SSergey Kambalin  * (see https://datasheets.raspberrypi.com/bcm2711/bcm2711-peripherals.pdf
37bf1da4b3SSergey Kambalin  * 1.2 Address Map)
38bf1da4b3SSergey Kambalin  */
raspi_add_memory_node(void * fdt,hwaddr mem_base,hwaddr mem_len)39bf1da4b3SSergey Kambalin static int raspi_add_memory_node(void *fdt, hwaddr mem_base, hwaddr mem_len)
40bf1da4b3SSergey Kambalin {
41bf1da4b3SSergey Kambalin     int ret;
42bf1da4b3SSergey Kambalin     uint32_t acells, scells;
43bf1da4b3SSergey Kambalin     char *nodename = g_strdup_printf("/memory@%" PRIx64, mem_base);
44bf1da4b3SSergey Kambalin 
45bf1da4b3SSergey Kambalin     acells = qemu_fdt_getprop_cell(fdt, "/", "#address-cells",
46bf1da4b3SSergey Kambalin                                    NULL, &error_fatal);
47bf1da4b3SSergey Kambalin     scells = qemu_fdt_getprop_cell(fdt, "/", "#size-cells",
48bf1da4b3SSergey Kambalin                                    NULL, &error_fatal);
49bf1da4b3SSergey Kambalin     if (acells == 0 || scells == 0) {
50bf1da4b3SSergey Kambalin         fprintf(stderr, "dtb file invalid (#address-cells or #size-cells 0)\n");
51bf1da4b3SSergey Kambalin         ret = -1;
52bf1da4b3SSergey Kambalin     } else {
53bf1da4b3SSergey Kambalin         qemu_fdt_add_subnode(fdt, nodename);
54bf1da4b3SSergey Kambalin         qemu_fdt_setprop_string(fdt, nodename, "device_type", "memory");
55bf1da4b3SSergey Kambalin         ret = qemu_fdt_setprop_sized_cells(fdt, nodename, "reg",
56bf1da4b3SSergey Kambalin                                            acells, mem_base,
57bf1da4b3SSergey Kambalin                                            scells, mem_len);
58bf1da4b3SSergey Kambalin     }
59bf1da4b3SSergey Kambalin 
60bf1da4b3SSergey Kambalin     g_free(nodename);
61bf1da4b3SSergey Kambalin     return ret;
62bf1da4b3SSergey Kambalin }
63bf1da4b3SSergey Kambalin 
raspi4_modify_dtb(const struct arm_boot_info * info,void * fdt)64bf1da4b3SSergey Kambalin static void raspi4_modify_dtb(const struct arm_boot_info *info, void *fdt)
65bf1da4b3SSergey Kambalin {
66bf1da4b3SSergey Kambalin     uint64_t ram_size;
67bf1da4b3SSergey Kambalin 
68bf1da4b3SSergey Kambalin     /* Temporarily disable following devices until they are implemented */
69bf1da4b3SSergey Kambalin     const char *nodes_to_remove[] = {
70bf1da4b3SSergey Kambalin         "brcm,bcm2711-pcie",
71bf1da4b3SSergey Kambalin         "brcm,bcm2711-rng200",
72bf1da4b3SSergey Kambalin         "brcm,bcm2711-thermal",
73bf1da4b3SSergey Kambalin         "brcm,bcm2711-genet-v5",
74bf1da4b3SSergey Kambalin     };
75bf1da4b3SSergey Kambalin 
76bf1da4b3SSergey Kambalin     for (int i = 0; i < ARRAY_SIZE(nodes_to_remove); i++) {
77bf1da4b3SSergey Kambalin         const char *dev_str = nodes_to_remove[i];
78bf1da4b3SSergey Kambalin 
79bf1da4b3SSergey Kambalin         int offset = fdt_node_offset_by_compatible(fdt, -1, dev_str);
80bf1da4b3SSergey Kambalin         if (offset >= 0) {
81bf1da4b3SSergey Kambalin             if (!fdt_nop_node(fdt, offset)) {
82bf1da4b3SSergey Kambalin                 warn_report("bcm2711 dtc: %s has been disabled!", dev_str);
83bf1da4b3SSergey Kambalin             }
84bf1da4b3SSergey Kambalin         }
85bf1da4b3SSergey Kambalin     }
86bf1da4b3SSergey Kambalin 
87bf1da4b3SSergey Kambalin     ram_size = board_ram_size(info->board_id);
88bf1da4b3SSergey Kambalin 
89bf1da4b3SSergey Kambalin     if (info->ram_size > UPPER_RAM_BASE) {
90bf1da4b3SSergey Kambalin         raspi_add_memory_node(fdt, UPPER_RAM_BASE, ram_size - UPPER_RAM_BASE);
91bf1da4b3SSergey Kambalin     }
92bf1da4b3SSergey Kambalin }
93bf1da4b3SSergey Kambalin 
raspi4b_machine_init(MachineState * machine)947785e8eaSSergey Kambalin static void raspi4b_machine_init(MachineState *machine)
957785e8eaSSergey Kambalin {
967785e8eaSSergey Kambalin     Raspi4bMachineState *s = RASPI4B_MACHINE(machine);
977785e8eaSSergey Kambalin     RaspiBaseMachineState *s_base = RASPI_BASE_MACHINE(machine);
987785e8eaSSergey Kambalin     RaspiBaseMachineClass *mc = RASPI_BASE_MACHINE_GET_CLASS(machine);
997785e8eaSSergey Kambalin     BCM2838State *soc = &s->soc;
1007785e8eaSSergey Kambalin 
101bf1da4b3SSergey Kambalin     s_base->binfo.modify_dtb = raspi4_modify_dtb;
1027785e8eaSSergey Kambalin     s_base->binfo.board_id = mc->board_rev;
1037785e8eaSSergey Kambalin 
1047785e8eaSSergey Kambalin     object_initialize_child(OBJECT(machine), "soc", soc,
1057785e8eaSSergey Kambalin                             board_soc_type(mc->board_rev));
1067785e8eaSSergey Kambalin 
1077785e8eaSSergey Kambalin     raspi_base_machine_init(machine, &soc->parent_obj);
1087785e8eaSSergey Kambalin }
1097785e8eaSSergey Kambalin 
raspi4b_machine_class_init(ObjectClass * oc,void * data)1107785e8eaSSergey Kambalin static void raspi4b_machine_class_init(ObjectClass *oc, void *data)
1117785e8eaSSergey Kambalin {
1127785e8eaSSergey Kambalin     MachineClass *mc = MACHINE_CLASS(oc);
1137785e8eaSSergey Kambalin     RaspiBaseMachineClass *rmc = RASPI_BASE_MACHINE_CLASS(oc);
1147785e8eaSSergey Kambalin 
115*393770d7SCédric Le Goater #if HOST_LONG_BITS == 32
116*393770d7SCédric Le Goater     rmc->board_rev = 0xa03111; /* Revision 1.1, 1 Gb RAM */
117*393770d7SCédric Le Goater #else
1187785e8eaSSergey Kambalin     rmc->board_rev = 0xb03115; /* Revision 1.5, 2 Gb RAM */
119*393770d7SCédric Le Goater #endif
1207785e8eaSSergey Kambalin     raspi_machine_class_common_init(mc, rmc->board_rev);
1217785e8eaSSergey Kambalin     mc->init = raspi4b_machine_init;
1227785e8eaSSergey Kambalin }
1237785e8eaSSergey Kambalin 
1247785e8eaSSergey Kambalin static const TypeInfo raspi4b_machine_type = {
1257785e8eaSSergey Kambalin     .name           = TYPE_RASPI4B_MACHINE,
1267785e8eaSSergey Kambalin     .parent         = TYPE_RASPI_BASE_MACHINE,
1277785e8eaSSergey Kambalin     .instance_size  = sizeof(Raspi4bMachineState),
1287785e8eaSSergey Kambalin     .class_init     = raspi4b_machine_class_init,
1297785e8eaSSergey Kambalin };
1307785e8eaSSergey Kambalin 
raspi4b_machine_register_type(void)1317785e8eaSSergey Kambalin static void raspi4b_machine_register_type(void)
1327785e8eaSSergey Kambalin {
1337785e8eaSSergey Kambalin     type_register_static(&raspi4b_machine_type);
1347785e8eaSSergey Kambalin }
1357785e8eaSSergey Kambalin 
1367785e8eaSSergey Kambalin type_init(raspi4b_machine_register_type)
137