master
c 138 lines 4.71 KB
Raw
1 /*
2 * Orange Pi emulation
3 *
4 * Copyright (C) 2019 Niek Linnenbank <nieklinnenbank@gmail.com>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "qemu/osdep.h"
21 #include "qemu/units.h"
22 #include "system/address-spaces.h"
23 #include "qapi/error.h"
24 #include "qemu/error-report.h"
25 #include "hw/core/boards.h"
26 #include "hw/core/qdev-properties.h"
27 #include "hw/arm/allwinner-h3.h"
28 #include "hw/arm/boot.h"
29 #include "hw/arm/machines-qom.h"
30
31 #define TYPE_ORANGEPI_MACHINE MACHINE_TYPE_NAME("orangepi-pc")
32 OBJECT_DECLARE_SIMPLE_TYPE(OrangePiMachineState, ORANGEPI_MACHINE)
33
34 struct OrangePiMachineState {
35 MachineState parent;
36
37 struct arm_boot_info bootinfo;
38 };
39
40 static void orangepi_init(MachineState *machine)
41 {
42 OrangePiMachineState *opms = ORANGEPI_MACHINE(machine);
43 AwH3State *h3;
44 DriveInfo *di;
45 BlockBackend *blk;
46 BusState *bus;
47 DeviceState *carddev;
48
49 /* BIOS is not supported by this board */
50 if (machine->firmware) {
51 error_report("BIOS not supported for this machine");
52 exit(1);
53 }
54
55 /* This board has fixed size RAM */
56 if (machine->ram_size != 1 * GiB) {
57 error_report("This machine can only be used with 1GiB of RAM");
58 exit(1);
59 }
60
61 h3 = AW_H3(object_new(TYPE_AW_H3));
62 object_property_add_child(OBJECT(machine), "soc", OBJECT(h3));
63 object_unref(OBJECT(h3));
64
65 /* Setup timer properties */
66 object_property_set_int(OBJECT(h3), "clk0-freq", 32768, &error_abort);
67 object_property_set_int(OBJECT(h3), "clk1-freq", 24 * 1000 * 1000,
68 &error_abort);
69
70 /* Setup SID properties. Currently using a default fixed SID identifier. */
71 if (qemu_uuid_is_null(&h3->sid.identifier)) {
72 qdev_prop_set_string(DEVICE(h3), "identifier",
73 "02c00081-1111-2222-3333-000044556677");
74 } else if (ldl_be_p(&h3->sid.identifier.data[0]) != 0x02c00081) {
75 warn_report("Security Identifier value does not include H3 prefix");
76 }
77
78 /* Setup EMAC properties */
79 object_property_set_int(OBJECT(&h3->emac), "phy-addr", 1, &error_abort);
80
81 /* DRAMC */
82 object_property_set_uint(OBJECT(h3), "ram-addr", h3->memmap[AW_H3_DEV_SDRAM],
83 &error_abort);
84 object_property_set_int(OBJECT(h3), "ram-size", machine->ram_size / MiB,
85 &error_abort);
86
87 /* Mark H3 object realized */
88 qdev_realize(DEVICE(h3), NULL, &error_abort);
89
90 /* Retrieve SD bus */
91 di = drive_get(IF_SD, 0, 0);
92 blk = di ? blk_by_legacy_dinfo(di) : NULL;
93 bus = qdev_get_child_bus(DEVICE(h3), "sd-bus");
94
95 /* Plug in SD card */
96 carddev = qdev_new(TYPE_SD_CARD);
97 qdev_prop_set_drive_err(carddev, "drive", blk, &error_fatal);
98 qdev_realize_and_unref(carddev, bus, &error_fatal);
99
100 /* SDRAM */
101 memory_region_add_subregion(get_system_memory(), h3->memmap[AW_H3_DEV_SDRAM],
102 machine->ram);
103
104 /* Load target kernel or start using BootROM */
105 if (!machine->kernel_filename && blk && blk_is_available(blk)) {
106 /* Use Boot ROM to copy data from SD card to SRAM */
107 allwinner_h3_bootrom_setup(h3, blk);
108 }
109 opms->bootinfo.loader_start = h3->memmap[AW_H3_DEV_SDRAM];
110 opms->bootinfo.ram_size = machine->ram_size;
111 opms->bootinfo.psci_conduit = QEMU_PSCI_CONDUIT_SMC;
112 arm_load_kernel(&h3->cpus[0], machine, &opms->bootinfo);
113 }
114
115 static void orangepi_machine_init(MachineClass *mc)
116 {
117 static const char * const valid_cpu_types[] = {
118 ARM_CPU_TYPE_NAME("cortex-a7"),
119 NULL
120 };
121
122 mc->desc = "Orange Pi PC (Cortex-A7)";
123 mc->init = orangepi_init;
124 mc->block_default_type = IF_SD;
125 mc->units_per_default_bus = 1;
126 mc->min_cpus = AW_H3_NUM_CPUS;
127 mc->max_cpus = AW_H3_NUM_CPUS;
128 mc->default_cpus = AW_H3_NUM_CPUS;
129 mc->default_cpu_type = ARM_CPU_TYPE_NAME("cortex-a7");
130 mc->valid_cpu_types = valid_cpu_types;
131 mc->default_ram_size = 1 * GiB;
132 mc->default_ram_id = "orangepi.ram";
133 mc->auto_create_sdcard = true;
134 }
135
136 DEFINE_MACHINE_EXTENDED("orangepi-pc", MACHINE, OrangePiMachineState,
137 orangepi_machine_init, false,
138 arm_machine_interfaces)