| 1 | /* |
| 2 | * Raspberry Pi 4B emulation |
| 3 | * |
| 4 | * Copyright (C) 2022 Ovchinnikov Vitalii <vitalii.ovchinnikov@auriga.com> |
| 5 | * |
| 6 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 7 | */ |
| 8 | |
| 9 | #include "qemu/osdep.h" |
| 10 | #include "qemu/units.h" |
| 11 | #include "qemu/cutils.h" |
| 12 | #include "qapi/error.h" |
| 13 | #include "qapi/visitor.h" |
| 14 | #include "hw/arm/machines-qom.h" |
| 15 | #include "hw/arm/raspi_platform.h" |
| 16 | #include "hw/display/bcm2835_fb.h" |
| 17 | #include "hw/core/registerfields.h" |
| 18 | #include "qemu/error-report.h" |
| 19 | #include "system/device_tree.h" |
| 20 | #include "hw/core/boards.h" |
| 21 | #include "hw/core/loader.h" |
| 22 | #include "hw/arm/boot.h" |
| 23 | #include "qom/object.h" |
| 24 | #include "hw/arm/bcm2838.h" |
| 25 | #include <libfdt.h> |
| 26 | |
| 27 | #define TYPE_RASPI4B_MACHINE MACHINE_TYPE_NAME("raspi4b") |
| 28 | OBJECT_DECLARE_SIMPLE_TYPE(Raspi4bMachineState, RASPI4B_MACHINE) |
| 29 | |
| 30 | struct Raspi4bMachineState { |
| 31 | RaspiBaseMachineState parent_obj; |
| 32 | BCM2838State soc; |
| 33 | }; |
| 34 | |
| 35 | /* |
| 36 | * Add second memory region if board RAM amount exceeds VC base address |
| 37 | * (see https://datasheets.raspberrypi.com/bcm2711/bcm2711-peripherals.pdf |
| 38 | * 1.2 Address Map) |
| 39 | */ |
| 40 | static void raspi_add_memory_node(void *fdt, hwaddr mem_base, hwaddr mem_len) |
| 41 | { |
| 42 | uint32_t acells, scells; |
| 43 | char *nodename = g_strdup_printf("/memory@%" PRIx64, mem_base); |
| 44 | |
| 45 | acells = qemu_fdt_getprop_cell(fdt, "/", "#address-cells", |
| 46 | NULL, &error_fatal); |
| 47 | scells = qemu_fdt_getprop_cell(fdt, "/", "#size-cells", |
| 48 | NULL, &error_fatal); |
| 49 | /* validated by arm_load_dtb */ |
| 50 | g_assert(acells && scells); |
| 51 | |
| 52 | qemu_fdt_add_subnode(fdt, nodename); |
| 53 | qemu_fdt_setprop_string(fdt, nodename, "device_type", "memory"); |
| 54 | qemu_fdt_setprop_sized_cells(fdt, nodename, "reg", |
| 55 | acells, mem_base, |
| 56 | scells, mem_len); |
| 57 | |
| 58 | g_free(nodename); |
| 59 | } |
| 60 | |
| 61 | static void raspi4_modify_dtb(const struct arm_boot_info *info, void *fdt) |
| 62 | { |
| 63 | uint64_t ram_size; |
| 64 | |
| 65 | /* Temporarily disable following devices until they are implemented */ |
| 66 | const char *nodes_to_remove[] = { |
| 67 | "brcm,bcm2711-pcie", |
| 68 | "brcm,bcm2711-rng200", |
| 69 | "brcm,bcm2711-thermal", |
| 70 | "brcm,bcm2711-genet-v5", |
| 71 | }; |
| 72 | |
| 73 | for (int i = 0; i < ARRAY_SIZE(nodes_to_remove); i++) { |
| 74 | const char *dev_str = nodes_to_remove[i]; |
| 75 | int offset; |
| 76 | |
| 77 | offset = fdt_node_offset_by_compatible(fdt, -1, dev_str); |
| 78 | while (offset >= 0) { |
| 79 | if (fdt_nop_node(fdt, offset) == 0) { |
| 80 | warn_report("bcm2711 dtb: %s has been disabled!", dev_str); |
| 81 | } |
| 82 | offset = fdt_node_offset_by_compatible(fdt, offset, dev_str); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | ram_size = board_ram_size(info->board_id); |
| 87 | |
| 88 | if (ram_size > UPPER_RAM_BASE) { |
| 89 | raspi_add_memory_node(fdt, UPPER_RAM_BASE, ram_size - UPPER_RAM_BASE); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | static void raspi4b_machine_init(MachineState *machine) |
| 94 | { |
| 95 | Raspi4bMachineState *s = RASPI4B_MACHINE(machine); |
| 96 | RaspiBaseMachineState *s_base = RASPI_BASE_MACHINE(machine); |
| 97 | RaspiBaseMachineClass *mc = RASPI_BASE_MACHINE_GET_CLASS(machine); |
| 98 | BCM2838State *soc = &s->soc; |
| 99 | |
| 100 | s_base->binfo.modify_dtb = raspi4_modify_dtb; |
| 101 | s_base->binfo.board_id = mc->board_rev; |
| 102 | |
| 103 | object_initialize_child(OBJECT(machine), "soc", soc, |
| 104 | board_soc_type(mc->board_rev)); |
| 105 | |
| 106 | raspi_base_machine_init(machine, &soc->parent_obj); |
| 107 | } |
| 108 | |
| 109 | static void raspi4b_machine_class_init(ObjectClass *oc, const void *data) |
| 110 | { |
| 111 | MachineClass *mc = MACHINE_CLASS(oc); |
| 112 | RaspiBaseMachineClass *rmc = RASPI_BASE_MACHINE_CLASS(oc); |
| 113 | |
| 114 | #if HOST_LONG_BITS == 32 |
| 115 | rmc->board_rev = 0xa03111; /* Revision 1.1, 1 Gb RAM */ |
| 116 | #else |
| 117 | rmc->board_rev = 0xb03115; /* Revision 1.5, 2 Gb RAM */ |
| 118 | #endif |
| 119 | raspi_machine_class_common_init(mc, rmc->board_rev); |
| 120 | mc->auto_create_sdcard = true; |
| 121 | mc->init = raspi4b_machine_init; |
| 122 | } |
| 123 | |
| 124 | static const TypeInfo raspi4b_machine_type = { |
| 125 | .name = TYPE_RASPI4B_MACHINE, |
| 126 | .parent = TYPE_RASPI_BASE_MACHINE, |
| 127 | .instance_size = sizeof(Raspi4bMachineState), |
| 128 | .class_init = raspi4b_machine_class_init, |
| 129 | .interfaces = aarch64_machine_interfaces, |
| 130 | }; |
| 131 | |
| 132 | static void raspi4b_machine_register_type(void) |
| 133 | { |
| 134 | type_register_static(&raspi4b_machine_type); |
| 135 | } |
| 136 | |
| 137 | type_init(raspi4b_machine_register_type) |