xref: /qemu/hw/arm/raspi.c (revision 3c8f9927fd435bb8d4865c0f261ed206e14e139a)
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"
194771d756SPaolo Bonzini #include "cpu.h"
201df7d1f9SAndrew Baumann #include "hw/arm/bcm2836.h"
21cd6c9977SPhilippe Mathieu-Daudé #include "hw/registerfields.h"
221df7d1f9SAndrew Baumann #include "qemu/error-report.h"
231df7d1f9SAndrew Baumann #include "hw/boards.h"
241df7d1f9SAndrew Baumann #include "hw/loader.h"
2512ec8bd5SPeter Maydell #include "hw/arm/boot.h"
261df7d1f9SAndrew Baumann #include "sysemu/sysemu.h"
27db1015e9SEduardo Habkost #include "qom/object.h"
281df7d1f9SAndrew Baumann 
291df7d1f9SAndrew Baumann #define SMPBOOT_ADDR    0x300 /* this should leave enough space for ATAGS */
301df7d1f9SAndrew Baumann #define MVBAR_ADDR      0x400 /* secure vectors */
311df7d1f9SAndrew Baumann #define BOARDSETUP_ADDR (MVBAR_ADDR + 0x20) /* board setup code */
32bade5816SPekka Enberg #define FIRMWARE_ADDR_2 0x8000 /* Pi 2 loads kernel.img here by default */
33bade5816SPekka Enberg #define FIRMWARE_ADDR_3 0x80000 /* Pi 3 loads kernel.img here by default */
34ff72cb6bSPeter Maydell #define SPINTABLE_ADDR  0xd8 /* Pi 3 bootloader spintable */
351df7d1f9SAndrew Baumann 
36918c81a5SPhilippe Mathieu-Daudé /* Registered machine type (matches RPi Foundation bootloader and U-Boot) */
37918c81a5SPhilippe Mathieu-Daudé #define MACH_TYPE_BCM2708   3138
381df7d1f9SAndrew Baumann 
39db1015e9SEduardo Habkost struct RaspiMachineState {
40cb57df6fSPhilippe Mathieu-Daudé     /*< private >*/
41cb57df6fSPhilippe Mathieu-Daudé     MachineState parent_obj;
42cb57df6fSPhilippe Mathieu-Daudé     /*< public >*/
43926dcdf0SPeter Maydell     BCM283XState soc;
440f15c6e3SPhilippe Mathieu-Daudé     struct arm_boot_info binfo;
45db1015e9SEduardo Habkost };
46db1015e9SEduardo Habkost typedef struct RaspiMachineState RaspiMachineState;
47cb57df6fSPhilippe Mathieu-Daudé 
48db1015e9SEduardo Habkost struct RaspiMachineClass {
49cb57df6fSPhilippe Mathieu-Daudé     /*< private >*/
50cb57df6fSPhilippe Mathieu-Daudé     MachineClass parent_obj;
51cb57df6fSPhilippe Mathieu-Daudé     /*< public >*/
52c318c66cSPhilippe Mathieu-Daudé     uint32_t board_rev;
53db1015e9SEduardo Habkost };
54db1015e9SEduardo Habkost typedef struct RaspiMachineClass RaspiMachineClass;
55cb57df6fSPhilippe Mathieu-Daudé 
56cb57df6fSPhilippe Mathieu-Daudé #define TYPE_RASPI_MACHINE       MACHINE_TYPE_NAME("raspi-common")
578110fa1dSEduardo Habkost DECLARE_OBJ_CHECKERS(RaspiMachineState, RaspiMachineClass,
588110fa1dSEduardo Habkost                      RASPI_MACHINE, TYPE_RASPI_MACHINE)
59cb57df6fSPhilippe Mathieu-Daudé 
601df7d1f9SAndrew Baumann 
61cd6c9977SPhilippe Mathieu-Daudé /*
62cd6c9977SPhilippe Mathieu-Daudé  * Board revision codes:
63cd6c9977SPhilippe Mathieu-Daudé  * www.raspberrypi.org/documentation/hardware/raspberrypi/revision-codes/
64cd6c9977SPhilippe Mathieu-Daudé  */
65cd6c9977SPhilippe Mathieu-Daudé FIELD(REV_CODE, REVISION,           0, 4);
66cd6c9977SPhilippe Mathieu-Daudé FIELD(REV_CODE, TYPE,               4, 8);
67cd6c9977SPhilippe Mathieu-Daudé FIELD(REV_CODE, PROCESSOR,         12, 4);
68cd6c9977SPhilippe Mathieu-Daudé FIELD(REV_CODE, MANUFACTURER,      16, 4);
69cd6c9977SPhilippe Mathieu-Daudé FIELD(REV_CODE, MEMORY_SIZE,       20, 3);
70cd6c9977SPhilippe Mathieu-Daudé FIELD(REV_CODE, STYLE,             23, 1);
71cd6c9977SPhilippe Mathieu-Daudé 
72696788d6SPhilippe Mathieu-Daudé typedef enum RaspiProcessorId {
73df6cf08dSPhilippe Mathieu-Daudé     PROCESSOR_ID_BCM2835 = 0,
74696788d6SPhilippe Mathieu-Daudé     PROCESSOR_ID_BCM2836 = 1,
75696788d6SPhilippe Mathieu-Daudé     PROCESSOR_ID_BCM2837 = 2,
76696788d6SPhilippe Mathieu-Daudé } RaspiProcessorId;
77696788d6SPhilippe Mathieu-Daudé 
78696788d6SPhilippe Mathieu-Daudé static const struct {
79696788d6SPhilippe Mathieu-Daudé     const char *type;
80696788d6SPhilippe Mathieu-Daudé     int cores_count;
81696788d6SPhilippe Mathieu-Daudé } soc_property[] = {
82df6cf08dSPhilippe Mathieu-Daudé     [PROCESSOR_ID_BCM2835] = {TYPE_BCM2835, 1},
83696788d6SPhilippe Mathieu-Daudé     [PROCESSOR_ID_BCM2836] = {TYPE_BCM2836, BCM283X_NCPUS},
84696788d6SPhilippe Mathieu-Daudé     [PROCESSOR_ID_BCM2837] = {TYPE_BCM2837, BCM283X_NCPUS},
85696788d6SPhilippe Mathieu-Daudé };
86696788d6SPhilippe Mathieu-Daudé 
87f5bb124eSPhilippe Mathieu-Daudé static uint64_t board_ram_size(uint32_t board_rev)
88f5bb124eSPhilippe Mathieu-Daudé {
89f5bb124eSPhilippe Mathieu-Daudé     assert(FIELD_EX32(board_rev, REV_CODE, STYLE)); /* Only new style */
90f5bb124eSPhilippe Mathieu-Daudé     return 256 * MiB << FIELD_EX32(board_rev, REV_CODE, MEMORY_SIZE);
91f5bb124eSPhilippe Mathieu-Daudé }
92f5bb124eSPhilippe Mathieu-Daudé 
93696788d6SPhilippe Mathieu-Daudé static RaspiProcessorId board_processor_id(uint32_t board_rev)
94cd6c9977SPhilippe Mathieu-Daudé {
95696788d6SPhilippe Mathieu-Daudé     int proc_id = FIELD_EX32(board_rev, REV_CODE, PROCESSOR);
96696788d6SPhilippe Mathieu-Daudé 
97cd6c9977SPhilippe Mathieu-Daudé     assert(FIELD_EX32(board_rev, REV_CODE, STYLE)); /* Only new style */
98696788d6SPhilippe Mathieu-Daudé     assert(proc_id < ARRAY_SIZE(soc_property) && soc_property[proc_id].type);
99696788d6SPhilippe Mathieu-Daudé 
100696788d6SPhilippe Mathieu-Daudé     return proc_id;
101cd6c9977SPhilippe Mathieu-Daudé }
102cd6c9977SPhilippe Mathieu-Daudé 
1032e664b45SPhilippe Mathieu-Daudé static const char *board_soc_type(uint32_t board_rev)
1042e664b45SPhilippe Mathieu-Daudé {
105696788d6SPhilippe Mathieu-Daudé     return soc_property[board_processor_id(board_rev)].type;
1062e664b45SPhilippe Mathieu-Daudé }
1072e664b45SPhilippe Mathieu-Daudé 
108759f0f87SPhilippe Mathieu-Daudé static int cores_count(uint32_t board_rev)
109759f0f87SPhilippe Mathieu-Daudé {
110696788d6SPhilippe Mathieu-Daudé     return soc_property[board_processor_id(board_rev)].cores_count;
111759f0f87SPhilippe Mathieu-Daudé }
112759f0f87SPhilippe Mathieu-Daudé 
11398b541e1SPhilippe Mathieu-Daudé static const char *board_type(uint32_t board_rev)
11498b541e1SPhilippe Mathieu-Daudé {
11598b541e1SPhilippe Mathieu-Daudé     static const char *types[] = {
11698b541e1SPhilippe Mathieu-Daudé         "A", "B", "A+", "B+", "2B", "Alpha", "CM1", NULL, "3B", "Zero",
11798b541e1SPhilippe Mathieu-Daudé         "CM3", NULL, "Zero W", "3B+", "3A+", NULL, "CM3+", "4B",
11898b541e1SPhilippe Mathieu-Daudé     };
11998b541e1SPhilippe Mathieu-Daudé     assert(FIELD_EX32(board_rev, REV_CODE, STYLE)); /* Only new style */
12098b541e1SPhilippe Mathieu-Daudé     int bt = FIELD_EX32(board_rev, REV_CODE, TYPE);
12198b541e1SPhilippe Mathieu-Daudé     if (bt >= ARRAY_SIZE(types) || !types[bt]) {
12298b541e1SPhilippe Mathieu-Daudé         return "Unknown";
12398b541e1SPhilippe Mathieu-Daudé     }
12498b541e1SPhilippe Mathieu-Daudé     return types[bt];
12598b541e1SPhilippe Mathieu-Daudé }
12698b541e1SPhilippe Mathieu-Daudé 
1271df7d1f9SAndrew Baumann static void write_smpboot(ARMCPU *cpu, const struct arm_boot_info *info)
1281df7d1f9SAndrew Baumann {
1291df7d1f9SAndrew Baumann     static const uint32_t smpboot[] = {
1301df7d1f9SAndrew Baumann         0xe1a0e00f, /*    mov     lr, pc */
1311df7d1f9SAndrew Baumann         0xe3a0fe00 + (BOARDSETUP_ADDR >> 4), /* mov pc, BOARDSETUP_ADDR */
1321df7d1f9SAndrew Baumann         0xee100fb0, /*    mrc     p15, 0, r0, c0, c0, 5;get core ID */
1331df7d1f9SAndrew Baumann         0xe7e10050, /*    ubfx    r0, r0, #0, #2       ;extract LSB */
1341df7d1f9SAndrew Baumann         0xe59f5014, /*    ldr     r5, =0x400000CC      ;load mbox base */
1351df7d1f9SAndrew Baumann         0xe320f001, /* 1: yield */
1361df7d1f9SAndrew Baumann         0xe7953200, /*    ldr     r3, [r5, r0, lsl #4] ;read mbox for our core*/
1371df7d1f9SAndrew Baumann         0xe3530000, /*    cmp     r3, #0               ;spin while zero */
1381df7d1f9SAndrew Baumann         0x0afffffb, /*    beq     1b */
1391df7d1f9SAndrew Baumann         0xe7853200, /*    str     r3, [r5, r0, lsl #4] ;clear mbox */
1401df7d1f9SAndrew Baumann         0xe12fff13, /*    bx      r3                   ;jump to target */
1411df7d1f9SAndrew Baumann         0x400000cc, /* (constant: mailbox 3 read/clear base) */
1421df7d1f9SAndrew Baumann     };
1431df7d1f9SAndrew Baumann 
1441df7d1f9SAndrew Baumann     /* check that we don't overrun board setup vectors */
1451df7d1f9SAndrew Baumann     QEMU_BUILD_BUG_ON(SMPBOOT_ADDR + sizeof(smpboot) > MVBAR_ADDR);
1461df7d1f9SAndrew Baumann     /* check that board setup address is correctly relocated */
1471df7d1f9SAndrew Baumann     QEMU_BUILD_BUG_ON((BOARDSETUP_ADDR & 0xf) != 0
1481df7d1f9SAndrew Baumann                       || (BOARDSETUP_ADDR >> 4) >= 0x100);
1491df7d1f9SAndrew Baumann 
1500f073693SPhilippe Mathieu-Daudé     rom_add_blob_fixed_as("raspi_smpboot", smpboot, sizeof(smpboot),
1510f073693SPhilippe Mathieu-Daudé                           info->smp_loader_start,
1520f073693SPhilippe Mathieu-Daudé                           arm_boot_address_space(cpu, info));
1531df7d1f9SAndrew Baumann }
1541df7d1f9SAndrew Baumann 
155ff72cb6bSPeter Maydell static void write_smpboot64(ARMCPU *cpu, const struct arm_boot_info *info)
156ff72cb6bSPeter Maydell {
1570f073693SPhilippe Mathieu-Daudé     AddressSpace *as = arm_boot_address_space(cpu, info);
158ff72cb6bSPeter Maydell     /* Unlike the AArch32 version we don't need to call the board setup hook.
159ff72cb6bSPeter Maydell      * The mechanism for doing the spin-table is also entirely different.
160ff72cb6bSPeter Maydell      * We must have four 64-bit fields at absolute addresses
161ff72cb6bSPeter Maydell      * 0xd8, 0xe0, 0xe8, 0xf0 in RAM, which are the flag variables for
162ff72cb6bSPeter Maydell      * our CPUs, and which we must ensure are zero initialized before
163ff72cb6bSPeter Maydell      * the primary CPU goes into the kernel. We put these variables inside
164ff72cb6bSPeter Maydell      * a rom blob, so that the reset for ROM contents zeroes them for us.
165ff72cb6bSPeter Maydell      */
166ff72cb6bSPeter Maydell     static const uint32_t smpboot[] = {
167ff72cb6bSPeter Maydell         0xd2801b05, /*        mov     x5, 0xd8 */
168ff72cb6bSPeter Maydell         0xd53800a6, /*        mrs     x6, mpidr_el1 */
169ff72cb6bSPeter Maydell         0x924004c6, /*        and     x6, x6, #0x3 */
170ff72cb6bSPeter Maydell         0xd503205f, /* spin:  wfe */
171ff72cb6bSPeter Maydell         0xf86678a4, /*        ldr     x4, [x5,x6,lsl #3] */
172ff72cb6bSPeter Maydell         0xb4ffffc4, /*        cbz     x4, spin */
173ff72cb6bSPeter Maydell         0xd2800000, /*        mov     x0, #0x0 */
174ff72cb6bSPeter Maydell         0xd2800001, /*        mov     x1, #0x0 */
175ff72cb6bSPeter Maydell         0xd2800002, /*        mov     x2, #0x0 */
176ff72cb6bSPeter Maydell         0xd2800003, /*        mov     x3, #0x0 */
177ff72cb6bSPeter Maydell         0xd61f0080, /*        br      x4 */
178ff72cb6bSPeter Maydell     };
179ff72cb6bSPeter Maydell 
180ff72cb6bSPeter Maydell     static const uint64_t spintables[] = {
181ff72cb6bSPeter Maydell         0, 0, 0, 0
182ff72cb6bSPeter Maydell     };
183ff72cb6bSPeter Maydell 
1840f073693SPhilippe Mathieu-Daudé     rom_add_blob_fixed_as("raspi_smpboot", smpboot, sizeof(smpboot),
1850f073693SPhilippe Mathieu-Daudé                           info->smp_loader_start, as);
1860f073693SPhilippe Mathieu-Daudé     rom_add_blob_fixed_as("raspi_spintables", spintables, sizeof(spintables),
1870f073693SPhilippe Mathieu-Daudé                           SPINTABLE_ADDR, as);
188ff72cb6bSPeter Maydell }
189ff72cb6bSPeter Maydell 
1901df7d1f9SAndrew Baumann static void write_board_setup(ARMCPU *cpu, const struct arm_boot_info *info)
1911df7d1f9SAndrew Baumann {
1921df7d1f9SAndrew Baumann     arm_write_secure_board_setup_dummy_smc(cpu, info, MVBAR_ADDR);
1931df7d1f9SAndrew Baumann }
1941df7d1f9SAndrew Baumann 
1951df7d1f9SAndrew Baumann static void reset_secondary(ARMCPU *cpu, const struct arm_boot_info *info)
1961df7d1f9SAndrew Baumann {
1971df7d1f9SAndrew Baumann     CPUState *cs = CPU(cpu);
1981df7d1f9SAndrew Baumann     cpu_set_pc(cs, info->smp_loader_start);
1991df7d1f9SAndrew Baumann }
2001df7d1f9SAndrew Baumann 
201cdfaa57dSPhilippe Mathieu-Daudé static void setup_boot(MachineState *machine, RaspiProcessorId processor_id,
202cdfaa57dSPhilippe Mathieu-Daudé                        size_t ram_size)
2031df7d1f9SAndrew Baumann {
20402058e4bSPhilippe Mathieu-Daudé     RaspiMachineState *s = RASPI_MACHINE(machine);
2051df7d1f9SAndrew Baumann     int r;
2061df7d1f9SAndrew Baumann 
2070f15c6e3SPhilippe Mathieu-Daudé     s->binfo.board_id = MACH_TYPE_BCM2708;
2080f15c6e3SPhilippe Mathieu-Daudé     s->binfo.ram_size = ram_size;
2090f15c6e3SPhilippe Mathieu-Daudé     s->binfo.nb_cpus = machine->smp.cpus;
21001e02f5aSPeter Maydell 
211cdfaa57dSPhilippe Mathieu-Daudé     if (processor_id <= PROCESSOR_ID_BCM2836) {
212cdfaa57dSPhilippe Mathieu-Daudé         /*
213cdfaa57dSPhilippe Mathieu-Daudé          * The BCM2835 and BCM2836 require some custom setup code to run
214cdfaa57dSPhilippe Mathieu-Daudé          * in Secure mode before booting a kernel (to set up the SMC vectors
215cdfaa57dSPhilippe Mathieu-Daudé          * so that we get a no-op SMC; this is used by Linux to call the
21601e02f5aSPeter Maydell          * firmware for some cache maintenance operations.
217cdfaa57dSPhilippe Mathieu-Daudé          * The BCM2837 doesn't need this.
21801e02f5aSPeter Maydell          */
2190f15c6e3SPhilippe Mathieu-Daudé         s->binfo.board_setup_addr = BOARDSETUP_ADDR;
2200f15c6e3SPhilippe Mathieu-Daudé         s->binfo.write_board_setup = write_board_setup;
2210f15c6e3SPhilippe Mathieu-Daudé         s->binfo.secure_board_setup = true;
2220f15c6e3SPhilippe Mathieu-Daudé         s->binfo.secure_boot = true;
22301e02f5aSPeter Maydell     }
2241df7d1f9SAndrew Baumann 
225cdfaa57dSPhilippe Mathieu-Daudé     /* BCM2836 and BCM2837 requires SMP setup */
226cdfaa57dSPhilippe Mathieu-Daudé     if (processor_id >= PROCESSOR_ID_BCM2836) {
2270f15c6e3SPhilippe Mathieu-Daudé         s->binfo.smp_loader_start = SMPBOOT_ADDR;
228cdfaa57dSPhilippe Mathieu-Daudé         if (processor_id == PROCESSOR_ID_BCM2836) {
2290f15c6e3SPhilippe Mathieu-Daudé             s->binfo.write_secondary_boot = write_smpboot;
230ff72cb6bSPeter Maydell         } else {
2310f15c6e3SPhilippe Mathieu-Daudé             s->binfo.write_secondary_boot = write_smpboot64;
232ff72cb6bSPeter Maydell         }
2330f15c6e3SPhilippe Mathieu-Daudé         s->binfo.secondary_cpu_reset_hook = reset_secondary;
2341df7d1f9SAndrew Baumann     }
2351df7d1f9SAndrew Baumann 
2361df7d1f9SAndrew Baumann     /* If the user specified a "firmware" image (e.g. UEFI), we bypass
2371df7d1f9SAndrew Baumann      * the normal Linux boot process
2381df7d1f9SAndrew Baumann      */
2391df7d1f9SAndrew Baumann     if (machine->firmware) {
2401af70269SPhilippe Mathieu-Daudé         hwaddr firmware_addr = processor_id <= PROCESSOR_ID_BCM2836
2411af70269SPhilippe Mathieu-Daudé                              ? FIRMWARE_ADDR_2 : FIRMWARE_ADDR_3;
2421df7d1f9SAndrew Baumann         /* load the firmware image (typically kernel.img) */
243bade5816SPekka Enberg         r = load_image_targphys(machine->firmware, firmware_addr,
244bade5816SPekka Enberg                                 ram_size - firmware_addr);
2451df7d1f9SAndrew Baumann         if (r < 0) {
2461df7d1f9SAndrew Baumann             error_report("Failed to load firmware from %s", machine->firmware);
2471df7d1f9SAndrew Baumann             exit(1);
2481df7d1f9SAndrew Baumann         }
2491df7d1f9SAndrew Baumann 
2500f15c6e3SPhilippe Mathieu-Daudé         s->binfo.entry = firmware_addr;
2510f15c6e3SPhilippe Mathieu-Daudé         s->binfo.firmware_loaded = true;
2521df7d1f9SAndrew Baumann     }
2531df7d1f9SAndrew Baumann 
2540f15c6e3SPhilippe Mathieu-Daudé     arm_load_kernel(&s->soc.cpu[0].core, machine, &s->binfo);
2551df7d1f9SAndrew Baumann }
2561df7d1f9SAndrew Baumann 
25713c4e2c0SPhilippe Mathieu-Daudé static void raspi_machine_init(MachineState *machine)
2581df7d1f9SAndrew Baumann {
259c318c66cSPhilippe Mathieu-Daudé     RaspiMachineClass *mc = RASPI_MACHINE_GET_CLASS(machine);
260cb57df6fSPhilippe Mathieu-Daudé     RaspiMachineState *s = RASPI_MACHINE(machine);
261c318c66cSPhilippe Mathieu-Daudé     uint32_t board_rev = mc->board_rev;
262f5bb124eSPhilippe Mathieu-Daudé     uint64_t ram_size = board_ram_size(board_rev);
2635e9c2a8dSGrégory ESTRADE     uint32_t vcram_size;
264a55b53a2SAndrew Baumann     DriveInfo *di;
265a55b53a2SAndrew Baumann     BlockBackend *blk;
266a55b53a2SAndrew Baumann     BusState *bus;
267a55b53a2SAndrew Baumann     DeviceState *carddev;
2681df7d1f9SAndrew Baumann 
269f5bb124eSPhilippe Mathieu-Daudé     if (machine->ram_size != ram_size) {
270f5bb124eSPhilippe Mathieu-Daudé         char *size_str = size_to_str(ram_size);
271f5bb124eSPhilippe Mathieu-Daudé         error_report("Invalid RAM size, should be %s", size_str);
272f5bb124eSPhilippe Mathieu-Daudé         g_free(size_str);
273ff3dcf28SPeter Maydell         exit(1);
274ff3dcf28SPeter Maydell     }
275ff3dcf28SPeter Maydell 
2761df7d1f9SAndrew Baumann     /* FIXME: Remove when we have custom CPU address space support */
277a4317ae8SIgor Mammedov     memory_region_add_subregion_overlap(get_system_memory(), 0,
278a4317ae8SIgor Mammedov                                         machine->ram, 0);
2791df7d1f9SAndrew Baumann 
2801df7d1f9SAndrew Baumann     /* Setup the SOC */
2819fc7fc4dSMarkus Armbruster     object_initialize_child(OBJECT(machine), "soc", &s->soc,
2829fc7fc4dSMarkus Armbruster                             board_soc_type(board_rev));
283d2623129SMarkus Armbruster     object_property_add_const_link(OBJECT(&s->soc), "ram", OBJECT(machine->ram));
2845325cc34SMarkus Armbruster     object_property_set_int(OBJECT(&s->soc), "board-rev", board_rev,
285f0afa731SStephen Warren                             &error_abort);
286ce189ab2SMarkus Armbruster     qdev_realize(DEVICE(&s->soc), NULL, &error_abort);
2871df7d1f9SAndrew Baumann 
288a55b53a2SAndrew Baumann     /* Create and plug in the SD cards */
289a55b53a2SAndrew Baumann     di = drive_get_next(IF_SD);
290a55b53a2SAndrew Baumann     blk = di ? blk_by_legacy_dinfo(di) : NULL;
291a55b53a2SAndrew Baumann     bus = qdev_get_child_bus(DEVICE(&s->soc), "sd-bus");
292a55b53a2SAndrew Baumann     if (bus == NULL) {
293a55b53a2SAndrew Baumann         error_report("No SD bus found in SOC object");
294a55b53a2SAndrew Baumann         exit(1);
295a55b53a2SAndrew Baumann     }
2963e80f690SMarkus Armbruster     carddev = qdev_new(TYPE_SD_CARD);
297934df912SMarkus Armbruster     qdev_prop_set_drive_err(carddev, "drive", blk, &error_fatal);
2983e80f690SMarkus Armbruster     qdev_realize_and_unref(carddev, bus, &error_fatal);
299a55b53a2SAndrew Baumann 
300c5c6c47cSMarc-André Lureau     vcram_size = object_property_get_uint(OBJECT(&s->soc), "vcram-size",
3015e9c2a8dSGrégory ESTRADE                                           &error_abort);
302cdfaa57dSPhilippe Mathieu-Daudé     setup_boot(machine, board_processor_id(mc->board_rev),
303cdfaa57dSPhilippe Mathieu-Daudé                machine->ram_size - vcram_size);
304bade5816SPekka Enberg }
305bade5816SPekka Enberg 
306f0eeb4b6SPhilippe Mathieu-Daudé static void raspi_machine_class_common_init(MachineClass *mc,
307f0eeb4b6SPhilippe Mathieu-Daudé                                             uint32_t board_rev)
3081df7d1f9SAndrew Baumann {
30962f06f71SPhilippe Mathieu-Daudé     mc->desc = g_strdup_printf("Raspberry Pi %s (revision 1.%u)",
31062f06f71SPhilippe Mathieu-Daudé                                board_type(board_rev),
31162f06f71SPhilippe Mathieu-Daudé                                FIELD_EX32(board_rev, REV_CODE, REVISION));
31213c4e2c0SPhilippe Mathieu-Daudé     mc->init = raspi_machine_init;
3131df7d1f9SAndrew Baumann     mc->block_default_type = IF_SD;
3141df7d1f9SAndrew Baumann     mc->no_parallel = 1;
3151df7d1f9SAndrew Baumann     mc->no_floppy = 1;
3161df7d1f9SAndrew Baumann     mc->no_cdrom = 1;
317759f0f87SPhilippe Mathieu-Daudé     mc->default_cpus = mc->min_cpus = mc->max_cpus = cores_count(board_rev);
318975f3402SPhilippe Mathieu-Daudé     mc->default_ram_size = board_ram_size(board_rev);
319a4317ae8SIgor Mammedov     mc->default_ram_id = "ram";
320a03bde36SPhilippe Mathieu-Daudé };
321cb57df6fSPhilippe Mathieu-Daudé 
322*3c8f9927SPhilippe Mathieu-Daudé static void raspi0_machine_class_init(ObjectClass *oc, void *data)
323*3c8f9927SPhilippe Mathieu-Daudé {
324*3c8f9927SPhilippe Mathieu-Daudé     MachineClass *mc = MACHINE_CLASS(oc);
325*3c8f9927SPhilippe Mathieu-Daudé     RaspiMachineClass *rmc = RASPI_MACHINE_CLASS(oc);
326*3c8f9927SPhilippe Mathieu-Daudé 
327*3c8f9927SPhilippe Mathieu-Daudé     rmc->board_rev = 0x920092; /* Revision 1.2 */
328*3c8f9927SPhilippe Mathieu-Daudé     raspi_machine_class_common_init(mc, rmc->board_rev);
329*3c8f9927SPhilippe Mathieu-Daudé };
330*3c8f9927SPhilippe Mathieu-Daudé 
331ac6bc6ebSPhilippe Mathieu-Daudé static void raspi1ap_machine_class_init(ObjectClass *oc, void *data)
332ac6bc6ebSPhilippe Mathieu-Daudé {
333ac6bc6ebSPhilippe Mathieu-Daudé     MachineClass *mc = MACHINE_CLASS(oc);
334ac6bc6ebSPhilippe Mathieu-Daudé     RaspiMachineClass *rmc = RASPI_MACHINE_CLASS(oc);
335ac6bc6ebSPhilippe Mathieu-Daudé 
336ac6bc6ebSPhilippe Mathieu-Daudé     rmc->board_rev = 0x900021; /* Revision 1.1 */
337ac6bc6ebSPhilippe Mathieu-Daudé     raspi_machine_class_common_init(mc, rmc->board_rev);
338ac6bc6ebSPhilippe Mathieu-Daudé };
339ac6bc6ebSPhilippe Mathieu-Daudé 
340f0eeb4b6SPhilippe Mathieu-Daudé static void raspi2b_machine_class_init(ObjectClass *oc, void *data)
341f0eeb4b6SPhilippe Mathieu-Daudé {
342f0eeb4b6SPhilippe Mathieu-Daudé     MachineClass *mc = MACHINE_CLASS(oc);
343f0eeb4b6SPhilippe Mathieu-Daudé     RaspiMachineClass *rmc = RASPI_MACHINE_CLASS(oc);
344f0eeb4b6SPhilippe Mathieu-Daudé 
345aa35ec22SPhilippe Mathieu-Daudé     mc->alias = "raspi2";
346f0eeb4b6SPhilippe Mathieu-Daudé     rmc->board_rev = 0xa21041;
347f0eeb4b6SPhilippe Mathieu-Daudé     raspi_machine_class_common_init(mc, rmc->board_rev);
348f0eeb4b6SPhilippe Mathieu-Daudé };
349f0eeb4b6SPhilippe Mathieu-Daudé 
350f0eeb4b6SPhilippe Mathieu-Daudé #ifdef TARGET_AARCH64
351f0eeb4b6SPhilippe Mathieu-Daudé static void raspi3b_machine_class_init(ObjectClass *oc, void *data)
352f0eeb4b6SPhilippe Mathieu-Daudé {
353f0eeb4b6SPhilippe Mathieu-Daudé     MachineClass *mc = MACHINE_CLASS(oc);
354f0eeb4b6SPhilippe Mathieu-Daudé     RaspiMachineClass *rmc = RASPI_MACHINE_CLASS(oc);
355f0eeb4b6SPhilippe Mathieu-Daudé 
356aa35ec22SPhilippe Mathieu-Daudé     mc->alias = "raspi3";
357f0eeb4b6SPhilippe Mathieu-Daudé     rmc->board_rev = 0xa02082;
358f0eeb4b6SPhilippe Mathieu-Daudé     raspi_machine_class_common_init(mc, rmc->board_rev);
359f0eeb4b6SPhilippe Mathieu-Daudé };
360f0eeb4b6SPhilippe Mathieu-Daudé #endif /* TARGET_AARCH64 */
361f0eeb4b6SPhilippe Mathieu-Daudé 
362cb57df6fSPhilippe Mathieu-Daudé static const TypeInfo raspi_machine_types[] = {
363cb57df6fSPhilippe Mathieu-Daudé     {
364*3c8f9927SPhilippe Mathieu-Daudé         .name           = MACHINE_TYPE_NAME("raspi0"),
365*3c8f9927SPhilippe Mathieu-Daudé         .parent         = TYPE_RASPI_MACHINE,
366*3c8f9927SPhilippe Mathieu-Daudé         .class_init     = raspi0_machine_class_init,
367*3c8f9927SPhilippe Mathieu-Daudé     }, {
368ac6bc6ebSPhilippe Mathieu-Daudé         .name           = MACHINE_TYPE_NAME("raspi1ap"),
369ac6bc6ebSPhilippe Mathieu-Daudé         .parent         = TYPE_RASPI_MACHINE,
370ac6bc6ebSPhilippe Mathieu-Daudé         .class_init     = raspi1ap_machine_class_init,
371ac6bc6ebSPhilippe Mathieu-Daudé     }, {
372aa35ec22SPhilippe Mathieu-Daudé         .name           = MACHINE_TYPE_NAME("raspi2b"),
373cb57df6fSPhilippe Mathieu-Daudé         .parent         = TYPE_RASPI_MACHINE,
374f0eeb4b6SPhilippe Mathieu-Daudé         .class_init     = raspi2b_machine_class_init,
375cb57df6fSPhilippe Mathieu-Daudé #ifdef TARGET_AARCH64
376cb57df6fSPhilippe Mathieu-Daudé     }, {
377aa35ec22SPhilippe Mathieu-Daudé         .name           = MACHINE_TYPE_NAME("raspi3b"),
378cb57df6fSPhilippe Mathieu-Daudé         .parent         = TYPE_RASPI_MACHINE,
379f0eeb4b6SPhilippe Mathieu-Daudé         .class_init     = raspi3b_machine_class_init,
380cb57df6fSPhilippe Mathieu-Daudé #endif
381cb57df6fSPhilippe Mathieu-Daudé     }, {
382cb57df6fSPhilippe Mathieu-Daudé         .name           = TYPE_RASPI_MACHINE,
383cb57df6fSPhilippe Mathieu-Daudé         .parent         = TYPE_MACHINE,
384cb57df6fSPhilippe Mathieu-Daudé         .instance_size  = sizeof(RaspiMachineState),
385cb57df6fSPhilippe Mathieu-Daudé         .class_size     = sizeof(RaspiMachineClass),
386cb57df6fSPhilippe Mathieu-Daudé         .abstract       = true,
387cb57df6fSPhilippe Mathieu-Daudé     }
388cb57df6fSPhilippe Mathieu-Daudé };
389cb57df6fSPhilippe Mathieu-Daudé 
390cb57df6fSPhilippe Mathieu-Daudé DEFINE_TYPES(raspi_machine_types)
391