11df7d1f9SAndrew Baumann /* 21df7d1f9SAndrew Baumann * Raspberry Pi emulation (c) 2012 Gregory Estrade 31df7d1f9SAndrew Baumann * Upstreaming code cleanup [including bcm2835_*] (c) 2013 Jan Petrous 41df7d1f9SAndrew Baumann * 51df7d1f9SAndrew Baumann * Rasperry Pi 2 emulation Copyright (c) 2015, Microsoft 61df7d1f9SAndrew Baumann * Written by Andrew Baumann 71df7d1f9SAndrew Baumann * 8bade5816SPekka Enberg * Raspberry Pi 3 emulation Copyright (c) 2018 Zoltán Baldaszti 9bade5816SPekka Enberg * Upstream code cleanup (c) 2018 Pekka Enberg 10bade5816SPekka Enberg * 116111a0c0SPhilippe Mathieu-Daudé * This work is licensed under the terms of the GNU GPL, version 2 or later. 126111a0c0SPhilippe Mathieu-Daudé * See the COPYING file in the top-level directory. 131df7d1f9SAndrew Baumann */ 141df7d1f9SAndrew Baumann 15c964b660SPeter Maydell #include "qemu/osdep.h" 16ff3dcf28SPeter Maydell #include "qemu/units.h" 17f5bb124eSPhilippe Mathieu-Daudé #include "qemu/cutils.h" 18da34e65cSMarkus Armbruster #include "qapi/error.h" 190acbdb4cSPeter Maydell #include "hw/arm/boot.h" 201df7d1f9SAndrew Baumann #include "hw/arm/bcm2836.h" 21*08df0676SSergey Kambalin #include "hw/arm/raspi_platform.h" 22cd6c9977SPhilippe Mathieu-Daudé #include "hw/registerfields.h" 231df7d1f9SAndrew Baumann #include "qemu/error-report.h" 241df7d1f9SAndrew Baumann #include "hw/boards.h" 251df7d1f9SAndrew Baumann #include "hw/loader.h" 2612ec8bd5SPeter Maydell #include "hw/arm/boot.h" 27db1015e9SEduardo Habkost #include "qom/object.h" 281df7d1f9SAndrew Baumann 29*08df0676SSergey Kambalin #define TYPE_RASPI_MACHINE MACHINE_TYPE_NAME("raspi-common") 30*08df0676SSergey Kambalin OBJECT_DECLARE_SIMPLE_TYPE(RaspiMachineState, RASPI_MACHINE) 31*08df0676SSergey Kambalin 321df7d1f9SAndrew Baumann #define SMPBOOT_ADDR 0x300 /* this should leave enough space for ATAGS */ 331df7d1f9SAndrew Baumann #define MVBAR_ADDR 0x400 /* secure vectors */ 341df7d1f9SAndrew Baumann #define BOARDSETUP_ADDR (MVBAR_ADDR + 0x20) /* board setup code */ 35bade5816SPekka Enberg #define FIRMWARE_ADDR_2 0x8000 /* Pi 2 loads kernel.img here by default */ 36bade5816SPekka Enberg #define FIRMWARE_ADDR_3 0x80000 /* Pi 3 loads kernel.img here by default */ 37ff72cb6bSPeter Maydell #define SPINTABLE_ADDR 0xd8 /* Pi 3 bootloader spintable */ 381df7d1f9SAndrew Baumann 39918c81a5SPhilippe Mathieu-Daudé /* Registered machine type (matches RPi Foundation bootloader and U-Boot) */ 40918c81a5SPhilippe Mathieu-Daudé #define MACH_TYPE_BCM2708 3138 411df7d1f9SAndrew Baumann 42db1015e9SEduardo Habkost struct RaspiMachineState { 43cb57df6fSPhilippe Mathieu-Daudé /*< private >*/ 44*08df0676SSergey Kambalin RaspiBaseMachineState parent_obj; 45cb57df6fSPhilippe Mathieu-Daudé /*< public >*/ 46926dcdf0SPeter Maydell BCM283XState soc; 47db1015e9SEduardo Habkost }; 481df7d1f9SAndrew Baumann 49cd6c9977SPhilippe Mathieu-Daudé /* 50cd6c9977SPhilippe Mathieu-Daudé * Board revision codes: 51cd6c9977SPhilippe Mathieu-Daudé * www.raspberrypi.org/documentation/hardware/raspberrypi/revision-codes/ 52cd6c9977SPhilippe Mathieu-Daudé */ 53cd6c9977SPhilippe Mathieu-Daudé FIELD(REV_CODE, REVISION, 0, 4); 54cd6c9977SPhilippe Mathieu-Daudé FIELD(REV_CODE, TYPE, 4, 8); 55cd6c9977SPhilippe Mathieu-Daudé FIELD(REV_CODE, PROCESSOR, 12, 4); 56cd6c9977SPhilippe Mathieu-Daudé FIELD(REV_CODE, MANUFACTURER, 16, 4); 57cd6c9977SPhilippe Mathieu-Daudé FIELD(REV_CODE, MEMORY_SIZE, 20, 3); 58cd6c9977SPhilippe Mathieu-Daudé FIELD(REV_CODE, STYLE, 23, 1); 59cd6c9977SPhilippe Mathieu-Daudé 60696788d6SPhilippe Mathieu-Daudé typedef enum RaspiProcessorId { 61df6cf08dSPhilippe Mathieu-Daudé PROCESSOR_ID_BCM2835 = 0, 62696788d6SPhilippe Mathieu-Daudé PROCESSOR_ID_BCM2836 = 1, 63696788d6SPhilippe Mathieu-Daudé PROCESSOR_ID_BCM2837 = 2, 64696788d6SPhilippe Mathieu-Daudé } RaspiProcessorId; 65696788d6SPhilippe Mathieu-Daudé 66696788d6SPhilippe Mathieu-Daudé static const struct { 67696788d6SPhilippe Mathieu-Daudé const char *type; 68696788d6SPhilippe Mathieu-Daudé int cores_count; 69696788d6SPhilippe Mathieu-Daudé } soc_property[] = { 70df6cf08dSPhilippe Mathieu-Daudé [PROCESSOR_ID_BCM2835] = {TYPE_BCM2835, 1}, 71696788d6SPhilippe Mathieu-Daudé [PROCESSOR_ID_BCM2836] = {TYPE_BCM2836, BCM283X_NCPUS}, 72696788d6SPhilippe Mathieu-Daudé [PROCESSOR_ID_BCM2837] = {TYPE_BCM2837, BCM283X_NCPUS}, 73696788d6SPhilippe Mathieu-Daudé }; 74696788d6SPhilippe Mathieu-Daudé 75*08df0676SSergey Kambalin static void raspi_base_machine_init(MachineState *machine, 76*08df0676SSergey Kambalin BCM283XBaseState *soc); 77*08df0676SSergey Kambalin static void raspi_machine_class_common_init(MachineClass *mc, 78*08df0676SSergey Kambalin uint32_t board_rev); 79*08df0676SSergey Kambalin 80f5bb124eSPhilippe Mathieu-Daudé static uint64_t board_ram_size(uint32_t board_rev) 81f5bb124eSPhilippe Mathieu-Daudé { 82f5bb124eSPhilippe Mathieu-Daudé assert(FIELD_EX32(board_rev, REV_CODE, STYLE)); /* Only new style */ 83f5bb124eSPhilippe Mathieu-Daudé return 256 * MiB << FIELD_EX32(board_rev, REV_CODE, MEMORY_SIZE); 84f5bb124eSPhilippe Mathieu-Daudé } 85f5bb124eSPhilippe Mathieu-Daudé 86696788d6SPhilippe Mathieu-Daudé static RaspiProcessorId board_processor_id(uint32_t board_rev) 87cd6c9977SPhilippe Mathieu-Daudé { 88696788d6SPhilippe Mathieu-Daudé int proc_id = FIELD_EX32(board_rev, REV_CODE, PROCESSOR); 89696788d6SPhilippe Mathieu-Daudé 90cd6c9977SPhilippe Mathieu-Daudé assert(FIELD_EX32(board_rev, REV_CODE, STYLE)); /* Only new style */ 91696788d6SPhilippe Mathieu-Daudé assert(proc_id < ARRAY_SIZE(soc_property) && soc_property[proc_id].type); 92696788d6SPhilippe Mathieu-Daudé 93696788d6SPhilippe Mathieu-Daudé return proc_id; 94cd6c9977SPhilippe Mathieu-Daudé } 95cd6c9977SPhilippe Mathieu-Daudé 962e664b45SPhilippe Mathieu-Daudé static const char *board_soc_type(uint32_t board_rev) 972e664b45SPhilippe Mathieu-Daudé { 98696788d6SPhilippe Mathieu-Daudé return soc_property[board_processor_id(board_rev)].type; 992e664b45SPhilippe Mathieu-Daudé } 1002e664b45SPhilippe Mathieu-Daudé 101759f0f87SPhilippe Mathieu-Daudé static int cores_count(uint32_t board_rev) 102759f0f87SPhilippe Mathieu-Daudé { 103696788d6SPhilippe Mathieu-Daudé return soc_property[board_processor_id(board_rev)].cores_count; 104759f0f87SPhilippe Mathieu-Daudé } 105759f0f87SPhilippe Mathieu-Daudé 10698b541e1SPhilippe Mathieu-Daudé static const char *board_type(uint32_t board_rev) 10798b541e1SPhilippe Mathieu-Daudé { 10898b541e1SPhilippe Mathieu-Daudé static const char *types[] = { 10998b541e1SPhilippe Mathieu-Daudé "A", "B", "A+", "B+", "2B", "Alpha", "CM1", NULL, "3B", "Zero", 11098b541e1SPhilippe Mathieu-Daudé "CM3", NULL, "Zero W", "3B+", "3A+", NULL, "CM3+", "4B", 11198b541e1SPhilippe Mathieu-Daudé }; 11298b541e1SPhilippe Mathieu-Daudé assert(FIELD_EX32(board_rev, REV_CODE, STYLE)); /* Only new style */ 11398b541e1SPhilippe Mathieu-Daudé int bt = FIELD_EX32(board_rev, REV_CODE, TYPE); 11498b541e1SPhilippe Mathieu-Daudé if (bt >= ARRAY_SIZE(types) || !types[bt]) { 11598b541e1SPhilippe Mathieu-Daudé return "Unknown"; 11698b541e1SPhilippe Mathieu-Daudé } 11798b541e1SPhilippe Mathieu-Daudé return types[bt]; 11898b541e1SPhilippe Mathieu-Daudé } 11998b541e1SPhilippe Mathieu-Daudé 1201df7d1f9SAndrew Baumann static void write_smpboot(ARMCPU *cpu, const struct arm_boot_info *info) 1211df7d1f9SAndrew Baumann { 1220acbdb4cSPeter Maydell static const ARMInsnFixup smpboot[] = { 1230acbdb4cSPeter Maydell { 0xe1a0e00f }, /* mov lr, pc */ 1240acbdb4cSPeter Maydell { 0xe3a0fe00 + (BOARDSETUP_ADDR >> 4) }, /* mov pc, BOARDSETUP_ADDR */ 1250acbdb4cSPeter Maydell { 0xee100fb0 }, /* mrc p15, 0, r0, c0, c0, 5;get core ID */ 1260acbdb4cSPeter Maydell { 0xe7e10050 }, /* ubfx r0, r0, #0, #2 ;extract LSB */ 1270acbdb4cSPeter Maydell { 0xe59f5014 }, /* ldr r5, =0x400000CC ;load mbox base */ 1280acbdb4cSPeter Maydell { 0xe320f001 }, /* 1: yield */ 1290acbdb4cSPeter Maydell { 0xe7953200 }, /* ldr r3, [r5, r0, lsl #4] ;read mbox for our core */ 1300acbdb4cSPeter Maydell { 0xe3530000 }, /* cmp r3, #0 ;spin while zero */ 1310acbdb4cSPeter Maydell { 0x0afffffb }, /* beq 1b */ 1320acbdb4cSPeter Maydell { 0xe7853200 }, /* str r3, [r5, r0, lsl #4] ;clear mbox */ 1330acbdb4cSPeter Maydell { 0xe12fff13 }, /* bx r3 ;jump to target */ 1340acbdb4cSPeter Maydell { 0x400000cc }, /* (constant: mailbox 3 read/clear base) */ 1350acbdb4cSPeter Maydell { 0, FIXUP_TERMINATOR } 1361df7d1f9SAndrew Baumann }; 1370acbdb4cSPeter Maydell static const uint32_t fixupcontext[FIXUP_MAX] = { 0 }; 1381df7d1f9SAndrew Baumann 1391df7d1f9SAndrew Baumann /* check that we don't overrun board setup vectors */ 1401df7d1f9SAndrew Baumann QEMU_BUILD_BUG_ON(SMPBOOT_ADDR + sizeof(smpboot) > MVBAR_ADDR); 1411df7d1f9SAndrew Baumann /* check that board setup address is correctly relocated */ 1421df7d1f9SAndrew Baumann QEMU_BUILD_BUG_ON((BOARDSETUP_ADDR & 0xf) != 0 1431df7d1f9SAndrew Baumann || (BOARDSETUP_ADDR >> 4) >= 0x100); 1441df7d1f9SAndrew Baumann 1450acbdb4cSPeter Maydell arm_write_bootloader("raspi_smpboot", arm_boot_address_space(cpu, info), 1460acbdb4cSPeter Maydell info->smp_loader_start, smpboot, fixupcontext); 1471df7d1f9SAndrew Baumann } 1481df7d1f9SAndrew Baumann 149ff72cb6bSPeter Maydell static void write_smpboot64(ARMCPU *cpu, const struct arm_boot_info *info) 150ff72cb6bSPeter Maydell { 1510f073693SPhilippe Mathieu-Daudé AddressSpace *as = arm_boot_address_space(cpu, info); 152ff72cb6bSPeter Maydell /* Unlike the AArch32 version we don't need to call the board setup hook. 153ff72cb6bSPeter Maydell * The mechanism for doing the spin-table is also entirely different. 154ff72cb6bSPeter Maydell * We must have four 64-bit fields at absolute addresses 155ff72cb6bSPeter Maydell * 0xd8, 0xe0, 0xe8, 0xf0 in RAM, which are the flag variables for 156ff72cb6bSPeter Maydell * our CPUs, and which we must ensure are zero initialized before 157ff72cb6bSPeter Maydell * the primary CPU goes into the kernel. We put these variables inside 158ff72cb6bSPeter Maydell * a rom blob, so that the reset for ROM contents zeroes them for us. 159ff72cb6bSPeter Maydell */ 1600acbdb4cSPeter Maydell static const ARMInsnFixup smpboot[] = { 1610acbdb4cSPeter Maydell { 0xd2801b05 }, /* mov x5, 0xd8 */ 1620acbdb4cSPeter Maydell { 0xd53800a6 }, /* mrs x6, mpidr_el1 */ 1630acbdb4cSPeter Maydell { 0x924004c6 }, /* and x6, x6, #0x3 */ 1640acbdb4cSPeter Maydell { 0xd503205f }, /* spin: wfe */ 1650acbdb4cSPeter Maydell { 0xf86678a4 }, /* ldr x4, [x5,x6,lsl #3] */ 1660acbdb4cSPeter Maydell { 0xb4ffffc4 }, /* cbz x4, spin */ 1670acbdb4cSPeter Maydell { 0xd2800000 }, /* mov x0, #0x0 */ 1680acbdb4cSPeter Maydell { 0xd2800001 }, /* mov x1, #0x0 */ 1690acbdb4cSPeter Maydell { 0xd2800002 }, /* mov x2, #0x0 */ 1700acbdb4cSPeter Maydell { 0xd2800003 }, /* mov x3, #0x0 */ 1710acbdb4cSPeter Maydell { 0xd61f0080 }, /* br x4 */ 1720acbdb4cSPeter Maydell { 0, FIXUP_TERMINATOR } 173ff72cb6bSPeter Maydell }; 1740acbdb4cSPeter Maydell static const uint32_t fixupcontext[FIXUP_MAX] = { 0 }; 175ff72cb6bSPeter Maydell 176ff72cb6bSPeter Maydell static const uint64_t spintables[] = { 177ff72cb6bSPeter Maydell 0, 0, 0, 0 178ff72cb6bSPeter Maydell }; 179ff72cb6bSPeter Maydell 1800acbdb4cSPeter Maydell arm_write_bootloader("raspi_smpboot", as, info->smp_loader_start, 1810acbdb4cSPeter Maydell smpboot, fixupcontext); 1820f073693SPhilippe Mathieu-Daudé rom_add_blob_fixed_as("raspi_spintables", spintables, sizeof(spintables), 1830f073693SPhilippe Mathieu-Daudé SPINTABLE_ADDR, as); 184ff72cb6bSPeter Maydell } 185ff72cb6bSPeter Maydell 1861df7d1f9SAndrew Baumann static void write_board_setup(ARMCPU *cpu, const struct arm_boot_info *info) 1871df7d1f9SAndrew Baumann { 1881df7d1f9SAndrew Baumann arm_write_secure_board_setup_dummy_smc(cpu, info, MVBAR_ADDR); 1891df7d1f9SAndrew Baumann } 1901df7d1f9SAndrew Baumann 1911df7d1f9SAndrew Baumann static void reset_secondary(ARMCPU *cpu, const struct arm_boot_info *info) 1921df7d1f9SAndrew Baumann { 1931df7d1f9SAndrew Baumann CPUState *cs = CPU(cpu); 1941df7d1f9SAndrew Baumann cpu_set_pc(cs, info->smp_loader_start); 1951df7d1f9SAndrew Baumann } 1961df7d1f9SAndrew Baumann 197*08df0676SSergey Kambalin static void setup_boot(MachineState *machine, ARMCPU *cpu, 198*08df0676SSergey Kambalin RaspiProcessorId processor_id, size_t ram_size) 1991df7d1f9SAndrew Baumann { 200*08df0676SSergey Kambalin RaspiBaseMachineState *s = RASPI_BASE_MACHINE(machine); 2011df7d1f9SAndrew Baumann int r; 2021df7d1f9SAndrew Baumann 2030f15c6e3SPhilippe Mathieu-Daudé s->binfo.ram_size = ram_size; 20401e02f5aSPeter Maydell 205cdfaa57dSPhilippe Mathieu-Daudé if (processor_id <= PROCESSOR_ID_BCM2836) { 206cdfaa57dSPhilippe Mathieu-Daudé /* 207cdfaa57dSPhilippe Mathieu-Daudé * The BCM2835 and BCM2836 require some custom setup code to run 208cdfaa57dSPhilippe Mathieu-Daudé * in Secure mode before booting a kernel (to set up the SMC vectors 209cdfaa57dSPhilippe Mathieu-Daudé * so that we get a no-op SMC; this is used by Linux to call the 21001e02f5aSPeter Maydell * firmware for some cache maintenance operations. 211cdfaa57dSPhilippe Mathieu-Daudé * The BCM2837 doesn't need this. 21201e02f5aSPeter Maydell */ 2130f15c6e3SPhilippe Mathieu-Daudé s->binfo.board_setup_addr = BOARDSETUP_ADDR; 2140f15c6e3SPhilippe Mathieu-Daudé s->binfo.write_board_setup = write_board_setup; 2150f15c6e3SPhilippe Mathieu-Daudé s->binfo.secure_board_setup = true; 2160f15c6e3SPhilippe Mathieu-Daudé s->binfo.secure_boot = true; 21701e02f5aSPeter Maydell } 2181df7d1f9SAndrew Baumann 219cdfaa57dSPhilippe Mathieu-Daudé /* BCM2836 and BCM2837 requires SMP setup */ 220cdfaa57dSPhilippe Mathieu-Daudé if (processor_id >= PROCESSOR_ID_BCM2836) { 2210f15c6e3SPhilippe Mathieu-Daudé s->binfo.smp_loader_start = SMPBOOT_ADDR; 222cdfaa57dSPhilippe Mathieu-Daudé if (processor_id == PROCESSOR_ID_BCM2836) { 2230f15c6e3SPhilippe Mathieu-Daudé s->binfo.write_secondary_boot = write_smpboot; 224ff72cb6bSPeter Maydell } else { 2250f15c6e3SPhilippe Mathieu-Daudé s->binfo.write_secondary_boot = write_smpboot64; 226ff72cb6bSPeter Maydell } 2270f15c6e3SPhilippe Mathieu-Daudé s->binfo.secondary_cpu_reset_hook = reset_secondary; 2281df7d1f9SAndrew Baumann } 2291df7d1f9SAndrew Baumann 2301df7d1f9SAndrew Baumann /* If the user specified a "firmware" image (e.g. UEFI), we bypass 2311df7d1f9SAndrew Baumann * the normal Linux boot process 2321df7d1f9SAndrew Baumann */ 2331df7d1f9SAndrew Baumann if (machine->firmware) { 2341af70269SPhilippe Mathieu-Daudé hwaddr firmware_addr = processor_id <= PROCESSOR_ID_BCM2836 2351af70269SPhilippe Mathieu-Daudé ? FIRMWARE_ADDR_2 : FIRMWARE_ADDR_3; 2361df7d1f9SAndrew Baumann /* load the firmware image (typically kernel.img) */ 237bade5816SPekka Enberg r = load_image_targphys(machine->firmware, firmware_addr, 238bade5816SPekka Enberg ram_size - firmware_addr); 2391df7d1f9SAndrew Baumann if (r < 0) { 2401df7d1f9SAndrew Baumann error_report("Failed to load firmware from %s", machine->firmware); 2411df7d1f9SAndrew Baumann exit(1); 2421df7d1f9SAndrew Baumann } 2431df7d1f9SAndrew Baumann 2440f15c6e3SPhilippe Mathieu-Daudé s->binfo.entry = firmware_addr; 2450f15c6e3SPhilippe Mathieu-Daudé s->binfo.firmware_loaded = true; 2461df7d1f9SAndrew Baumann } 2471df7d1f9SAndrew Baumann 248*08df0676SSergey Kambalin arm_load_kernel(cpu, machine, &s->binfo); 2491df7d1f9SAndrew Baumann } 2501df7d1f9SAndrew Baumann 251*08df0676SSergey Kambalin static void raspi_base_machine_init(MachineState *machine, 252*08df0676SSergey Kambalin BCM283XBaseState *soc) 2531df7d1f9SAndrew Baumann { 254*08df0676SSergey Kambalin RaspiBaseMachineClass *mc = RASPI_BASE_MACHINE_GET_CLASS(machine); 255c318c66cSPhilippe Mathieu-Daudé uint32_t board_rev = mc->board_rev; 256f5bb124eSPhilippe Mathieu-Daudé uint64_t ram_size = board_ram_size(board_rev); 2575e9c2a8dSGrégory ESTRADE uint32_t vcram_size; 258a55b53a2SAndrew Baumann DriveInfo *di; 259a55b53a2SAndrew Baumann BlockBackend *blk; 260a55b53a2SAndrew Baumann BusState *bus; 261a55b53a2SAndrew Baumann DeviceState *carddev; 2621df7d1f9SAndrew Baumann 263f5bb124eSPhilippe Mathieu-Daudé if (machine->ram_size != ram_size) { 264f5bb124eSPhilippe Mathieu-Daudé char *size_str = size_to_str(ram_size); 265f5bb124eSPhilippe Mathieu-Daudé error_report("Invalid RAM size, should be %s", size_str); 266f5bb124eSPhilippe Mathieu-Daudé g_free(size_str); 267ff3dcf28SPeter Maydell exit(1); 268ff3dcf28SPeter Maydell } 269ff3dcf28SPeter Maydell 2701df7d1f9SAndrew Baumann /* FIXME: Remove when we have custom CPU address space support */ 271a4317ae8SIgor Mammedov memory_region_add_subregion_overlap(get_system_memory(), 0, 272a4317ae8SIgor Mammedov machine->ram, 0); 2731df7d1f9SAndrew Baumann 2741df7d1f9SAndrew Baumann /* Setup the SOC */ 275*08df0676SSergey Kambalin object_property_add_const_link(OBJECT(soc), "ram", OBJECT(machine->ram)); 276*08df0676SSergey Kambalin object_property_set_int(OBJECT(soc), "board-rev", board_rev, 277f0afa731SStephen Warren &error_abort); 278*08df0676SSergey Kambalin object_property_set_str(OBJECT(soc), "command-line", 279f802ff1eSDaniel Bertalan machine->kernel_cmdline, &error_abort); 280*08df0676SSergey Kambalin qdev_realize(DEVICE(soc), NULL, &error_fatal); 2811df7d1f9SAndrew Baumann 282a55b53a2SAndrew Baumann /* Create and plug in the SD cards */ 28364eaa820SMarkus Armbruster di = drive_get(IF_SD, 0, 0); 284a55b53a2SAndrew Baumann blk = di ? blk_by_legacy_dinfo(di) : NULL; 285*08df0676SSergey Kambalin bus = qdev_get_child_bus(DEVICE(soc), "sd-bus"); 286a55b53a2SAndrew Baumann if (bus == NULL) { 287a55b53a2SAndrew Baumann error_report("No SD bus found in SOC object"); 288a55b53a2SAndrew Baumann exit(1); 289a55b53a2SAndrew Baumann } 2903e80f690SMarkus Armbruster carddev = qdev_new(TYPE_SD_CARD); 291934df912SMarkus Armbruster qdev_prop_set_drive_err(carddev, "drive", blk, &error_fatal); 2923e80f690SMarkus Armbruster qdev_realize_and_unref(carddev, bus, &error_fatal); 293a55b53a2SAndrew Baumann 294*08df0676SSergey Kambalin vcram_size = object_property_get_uint(OBJECT(soc), "vcram-size", 2955e9c2a8dSGrégory ESTRADE &error_abort); 296*08df0676SSergey Kambalin setup_boot(machine, &soc->cpu[0].core, board_processor_id(board_rev), 297cdfaa57dSPhilippe Mathieu-Daudé machine->ram_size - vcram_size); 298bade5816SPekka Enberg } 299bade5816SPekka Enberg 300*08df0676SSergey Kambalin static void raspi_machine_init(MachineState *machine) 301*08df0676SSergey Kambalin { 302*08df0676SSergey Kambalin RaspiMachineState *s = RASPI_MACHINE(machine); 303*08df0676SSergey Kambalin RaspiBaseMachineState *s_base = RASPI_BASE_MACHINE(machine); 304*08df0676SSergey Kambalin RaspiBaseMachineClass *mc = RASPI_BASE_MACHINE_GET_CLASS(machine); 305*08df0676SSergey Kambalin BCM283XState *soc = &s->soc; 306*08df0676SSergey Kambalin 307*08df0676SSergey Kambalin s_base->binfo.board_id = MACH_TYPE_BCM2708; 308*08df0676SSergey Kambalin 309*08df0676SSergey Kambalin object_initialize_child(OBJECT(machine), "soc", soc, 310*08df0676SSergey Kambalin board_soc_type(mc->board_rev)); 311*08df0676SSergey Kambalin raspi_base_machine_init(machine, &soc->parent_obj); 312*08df0676SSergey Kambalin } 313*08df0676SSergey Kambalin 314*08df0676SSergey Kambalin void raspi_machine_class_common_init(MachineClass *mc, 315f0eeb4b6SPhilippe Mathieu-Daudé uint32_t board_rev) 3161df7d1f9SAndrew Baumann { 31762f06f71SPhilippe Mathieu-Daudé mc->desc = g_strdup_printf("Raspberry Pi %s (revision 1.%u)", 31862f06f71SPhilippe Mathieu-Daudé board_type(board_rev), 31962f06f71SPhilippe Mathieu-Daudé FIELD_EX32(board_rev, REV_CODE, REVISION)); 3201df7d1f9SAndrew Baumann mc->block_default_type = IF_SD; 3211df7d1f9SAndrew Baumann mc->no_parallel = 1; 3221df7d1f9SAndrew Baumann mc->no_floppy = 1; 3231df7d1f9SAndrew Baumann mc->no_cdrom = 1; 324759f0f87SPhilippe Mathieu-Daudé mc->default_cpus = mc->min_cpus = mc->max_cpus = cores_count(board_rev); 325975f3402SPhilippe Mathieu-Daudé mc->default_ram_size = board_ram_size(board_rev); 326a4317ae8SIgor Mammedov mc->default_ram_id = "ram"; 327a03bde36SPhilippe Mathieu-Daudé }; 328cb57df6fSPhilippe Mathieu-Daudé 329*08df0676SSergey Kambalin static void raspi_machine_class_init(MachineClass *mc, 330*08df0676SSergey Kambalin uint32_t board_rev) 331*08df0676SSergey Kambalin { 332*08df0676SSergey Kambalin raspi_machine_class_common_init(mc, board_rev); 333*08df0676SSergey Kambalin mc->init = raspi_machine_init; 334*08df0676SSergey Kambalin }; 335*08df0676SSergey Kambalin 3363c8f9927SPhilippe Mathieu-Daudé static void raspi0_machine_class_init(ObjectClass *oc, void *data) 3373c8f9927SPhilippe Mathieu-Daudé { 3383c8f9927SPhilippe Mathieu-Daudé MachineClass *mc = MACHINE_CLASS(oc); 339*08df0676SSergey Kambalin RaspiBaseMachineClass *rmc = RASPI_BASE_MACHINE_CLASS(oc); 3403c8f9927SPhilippe Mathieu-Daudé 3413c8f9927SPhilippe Mathieu-Daudé rmc->board_rev = 0x920092; /* Revision 1.2 */ 342*08df0676SSergey Kambalin raspi_machine_class_init(mc, rmc->board_rev); 3433c8f9927SPhilippe Mathieu-Daudé }; 3443c8f9927SPhilippe Mathieu-Daudé 345ac6bc6ebSPhilippe Mathieu-Daudé static void raspi1ap_machine_class_init(ObjectClass *oc, void *data) 346ac6bc6ebSPhilippe Mathieu-Daudé { 347ac6bc6ebSPhilippe Mathieu-Daudé MachineClass *mc = MACHINE_CLASS(oc); 348*08df0676SSergey Kambalin RaspiBaseMachineClass *rmc = RASPI_BASE_MACHINE_CLASS(oc); 349ac6bc6ebSPhilippe Mathieu-Daudé 350ac6bc6ebSPhilippe Mathieu-Daudé rmc->board_rev = 0x900021; /* Revision 1.1 */ 351*08df0676SSergey Kambalin raspi_machine_class_init(mc, rmc->board_rev); 352ac6bc6ebSPhilippe Mathieu-Daudé }; 353ac6bc6ebSPhilippe Mathieu-Daudé 354f0eeb4b6SPhilippe Mathieu-Daudé static void raspi2b_machine_class_init(ObjectClass *oc, void *data) 355f0eeb4b6SPhilippe Mathieu-Daudé { 356f0eeb4b6SPhilippe Mathieu-Daudé MachineClass *mc = MACHINE_CLASS(oc); 357*08df0676SSergey Kambalin RaspiBaseMachineClass *rmc = RASPI_BASE_MACHINE_CLASS(oc); 358f0eeb4b6SPhilippe Mathieu-Daudé 359f0eeb4b6SPhilippe Mathieu-Daudé rmc->board_rev = 0xa21041; 360*08df0676SSergey Kambalin raspi_machine_class_init(mc, rmc->board_rev); 361f0eeb4b6SPhilippe Mathieu-Daudé }; 362f0eeb4b6SPhilippe Mathieu-Daudé 363f0eeb4b6SPhilippe Mathieu-Daudé #ifdef TARGET_AARCH64 3645be94252SPhilippe Mathieu-Daudé static void raspi3ap_machine_class_init(ObjectClass *oc, void *data) 3655be94252SPhilippe Mathieu-Daudé { 3665be94252SPhilippe Mathieu-Daudé MachineClass *mc = MACHINE_CLASS(oc); 367*08df0676SSergey Kambalin RaspiBaseMachineClass *rmc = RASPI_BASE_MACHINE_CLASS(oc); 3685be94252SPhilippe Mathieu-Daudé 3695be94252SPhilippe Mathieu-Daudé rmc->board_rev = 0x9020e0; /* Revision 1.0 */ 370*08df0676SSergey Kambalin raspi_machine_class_init(mc, rmc->board_rev); 3715be94252SPhilippe Mathieu-Daudé }; 3725be94252SPhilippe Mathieu-Daudé 373f0eeb4b6SPhilippe Mathieu-Daudé static void raspi3b_machine_class_init(ObjectClass *oc, void *data) 374f0eeb4b6SPhilippe Mathieu-Daudé { 375f0eeb4b6SPhilippe Mathieu-Daudé MachineClass *mc = MACHINE_CLASS(oc); 376*08df0676SSergey Kambalin RaspiBaseMachineClass *rmc = RASPI_BASE_MACHINE_CLASS(oc); 377f0eeb4b6SPhilippe Mathieu-Daudé 378f0eeb4b6SPhilippe Mathieu-Daudé rmc->board_rev = 0xa02082; 379*08df0676SSergey Kambalin raspi_machine_class_init(mc, rmc->board_rev); 380f0eeb4b6SPhilippe Mathieu-Daudé }; 381f0eeb4b6SPhilippe Mathieu-Daudé #endif /* TARGET_AARCH64 */ 382f0eeb4b6SPhilippe Mathieu-Daudé 383cb57df6fSPhilippe Mathieu-Daudé static const TypeInfo raspi_machine_types[] = { 384cb57df6fSPhilippe Mathieu-Daudé { 3853c8f9927SPhilippe Mathieu-Daudé .name = MACHINE_TYPE_NAME("raspi0"), 3863c8f9927SPhilippe Mathieu-Daudé .parent = TYPE_RASPI_MACHINE, 3873c8f9927SPhilippe Mathieu-Daudé .class_init = raspi0_machine_class_init, 3883c8f9927SPhilippe Mathieu-Daudé }, { 389ac6bc6ebSPhilippe Mathieu-Daudé .name = MACHINE_TYPE_NAME("raspi1ap"), 390ac6bc6ebSPhilippe Mathieu-Daudé .parent = TYPE_RASPI_MACHINE, 391ac6bc6ebSPhilippe Mathieu-Daudé .class_init = raspi1ap_machine_class_init, 392ac6bc6ebSPhilippe Mathieu-Daudé }, { 393aa35ec22SPhilippe Mathieu-Daudé .name = MACHINE_TYPE_NAME("raspi2b"), 394cb57df6fSPhilippe Mathieu-Daudé .parent = TYPE_RASPI_MACHINE, 395f0eeb4b6SPhilippe Mathieu-Daudé .class_init = raspi2b_machine_class_init, 396cb57df6fSPhilippe Mathieu-Daudé #ifdef TARGET_AARCH64 397cb57df6fSPhilippe Mathieu-Daudé }, { 3985be94252SPhilippe Mathieu-Daudé .name = MACHINE_TYPE_NAME("raspi3ap"), 3995be94252SPhilippe Mathieu-Daudé .parent = TYPE_RASPI_MACHINE, 4005be94252SPhilippe Mathieu-Daudé .class_init = raspi3ap_machine_class_init, 4015be94252SPhilippe Mathieu-Daudé }, { 402aa35ec22SPhilippe Mathieu-Daudé .name = MACHINE_TYPE_NAME("raspi3b"), 403cb57df6fSPhilippe Mathieu-Daudé .parent = TYPE_RASPI_MACHINE, 404f0eeb4b6SPhilippe Mathieu-Daudé .class_init = raspi3b_machine_class_init, 405cb57df6fSPhilippe Mathieu-Daudé #endif 406cb57df6fSPhilippe Mathieu-Daudé }, { 407cb57df6fSPhilippe Mathieu-Daudé .name = TYPE_RASPI_MACHINE, 408*08df0676SSergey Kambalin .parent = TYPE_RASPI_BASE_MACHINE, 409cb57df6fSPhilippe Mathieu-Daudé .instance_size = sizeof(RaspiMachineState), 410*08df0676SSergey Kambalin .abstract = true, 411*08df0676SSergey Kambalin }, { 412*08df0676SSergey Kambalin .name = TYPE_RASPI_BASE_MACHINE, 413*08df0676SSergey Kambalin .parent = TYPE_MACHINE, 414*08df0676SSergey Kambalin .instance_size = sizeof(RaspiBaseMachineState), 415*08df0676SSergey Kambalin .class_size = sizeof(RaspiBaseMachineClass), 416cb57df6fSPhilippe Mathieu-Daudé .abstract = true, 417cb57df6fSPhilippe Mathieu-Daudé } 418cb57df6fSPhilippe Mathieu-Daudé }; 419cb57df6fSPhilippe Mathieu-Daudé 420cb57df6fSPhilippe Mathieu-Daudé DEFINE_TYPES(raspi_machine_types) 421