iotests/129: Do not check @busy
[qemu.git] / hw / riscv / sifive_e.c
blob59bac4cc9ad5125b5fb25eae7db73696136099e6
1 /*
2 * QEMU RISC-V Board Compatible with SiFive Freedom E SDK
4 * Copyright (c) 2017 SiFive, Inc.
6 * Provides a board compatible with the SiFive Freedom E SDK:
8 * 0) UART
9 * 1) CLINT (Core Level Interruptor)
10 * 2) PLIC (Platform Level Interrupt Controller)
11 * 3) PRCI (Power, Reset, Clock, Interrupt)
12 * 4) Registers emulated as RAM: AON, GPIO, QSPI, PWM
13 * 5) Flash memory emulated as RAM
15 * The Mask ROM reset vector jumps to the flash payload at 0x2040_0000.
16 * The OTP ROM and Flash boot code will be emulated in a future version.
18 * This program is free software; you can redistribute it and/or modify it
19 * under the terms and conditions of the GNU General Public License,
20 * version 2 or later, as published by the Free Software Foundation.
22 * This program is distributed in the hope it will be useful, but WITHOUT
23 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
24 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
25 * more details.
27 * You should have received a copy of the GNU General Public License along with
28 * this program. If not, see <http://www.gnu.org/licenses/>.
31 #include "qemu/osdep.h"
32 #include "qemu/log.h"
33 #include "qemu/error-report.h"
34 #include "qapi/error.h"
35 #include "hw/boards.h"
36 #include "hw/loader.h"
37 #include "hw/sysbus.h"
38 #include "hw/char/serial.h"
39 #include "hw/misc/unimp.h"
40 #include "target/riscv/cpu.h"
41 #include "hw/riscv/riscv_hart.h"
42 #include "hw/riscv/sifive_e.h"
43 #include "hw/riscv/boot.h"
44 #include "hw/char/sifive_uart.h"
45 #include "hw/intc/sifive_clint.h"
46 #include "hw/intc/sifive_plic.h"
47 #include "hw/misc/sifive_e_prci.h"
48 #include "chardev/char.h"
49 #include "sysemu/arch_init.h"
50 #include "sysemu/sysemu.h"
51 #include "exec/address-spaces.h"
53 static const struct MemmapEntry {
54 hwaddr base;
55 hwaddr size;
56 } sifive_e_memmap[] = {
57 [SIFIVE_E_DEV_DEBUG] = { 0x0, 0x1000 },
58 [SIFIVE_E_DEV_MROM] = { 0x1000, 0x2000 },
59 [SIFIVE_E_DEV_OTP] = { 0x20000, 0x2000 },
60 [SIFIVE_E_DEV_CLINT] = { 0x2000000, 0x10000 },
61 [SIFIVE_E_DEV_PLIC] = { 0xc000000, 0x4000000 },
62 [SIFIVE_E_DEV_AON] = { 0x10000000, 0x8000 },
63 [SIFIVE_E_DEV_PRCI] = { 0x10008000, 0x8000 },
64 [SIFIVE_E_DEV_OTP_CTRL] = { 0x10010000, 0x1000 },
65 [SIFIVE_E_DEV_GPIO0] = { 0x10012000, 0x1000 },
66 [SIFIVE_E_DEV_UART0] = { 0x10013000, 0x1000 },
67 [SIFIVE_E_DEV_QSPI0] = { 0x10014000, 0x1000 },
68 [SIFIVE_E_DEV_PWM0] = { 0x10015000, 0x1000 },
69 [SIFIVE_E_DEV_UART1] = { 0x10023000, 0x1000 },
70 [SIFIVE_E_DEV_QSPI1] = { 0x10024000, 0x1000 },
71 [SIFIVE_E_DEV_PWM1] = { 0x10025000, 0x1000 },
72 [SIFIVE_E_DEV_QSPI2] = { 0x10034000, 0x1000 },
73 [SIFIVE_E_DEV_PWM2] = { 0x10035000, 0x1000 },
74 [SIFIVE_E_DEV_XIP] = { 0x20000000, 0x20000000 },
75 [SIFIVE_E_DEV_DTIM] = { 0x80000000, 0x4000 }
78 static void sifive_e_machine_init(MachineState *machine)
80 const struct MemmapEntry *memmap = sifive_e_memmap;
82 SiFiveEState *s = RISCV_E_MACHINE(machine);
83 MemoryRegion *sys_mem = get_system_memory();
84 MemoryRegion *main_mem = g_new(MemoryRegion, 1);
85 int i;
87 /* Initialize SoC */
88 object_initialize_child(OBJECT(machine), "soc", &s->soc, TYPE_RISCV_E_SOC);
89 qdev_realize(DEVICE(&s->soc), NULL, &error_abort);
91 /* Data Tightly Integrated Memory */
92 memory_region_init_ram(main_mem, NULL, "riscv.sifive.e.ram",
93 memmap[SIFIVE_E_DEV_DTIM].size, &error_fatal);
94 memory_region_add_subregion(sys_mem,
95 memmap[SIFIVE_E_DEV_DTIM].base, main_mem);
97 /* Mask ROM reset vector */
98 uint32_t reset_vec[4];
100 if (s->revb) {
101 reset_vec[1] = 0x200102b7; /* 0x1004: lui t0,0x20010 */
102 } else {
103 reset_vec[1] = 0x204002b7; /* 0x1004: lui t0,0x20400 */
105 reset_vec[2] = 0x00028067; /* 0x1008: jr t0 */
107 reset_vec[0] = reset_vec[3] = 0;
109 /* copy in the reset vector in little_endian byte order */
110 for (i = 0; i < sizeof(reset_vec) >> 2; i++) {
111 reset_vec[i] = cpu_to_le32(reset_vec[i]);
113 rom_add_blob_fixed_as("mrom.reset", reset_vec, sizeof(reset_vec),
114 memmap[SIFIVE_E_DEV_MROM].base, &address_space_memory);
116 if (machine->kernel_filename) {
117 riscv_load_kernel(machine->kernel_filename,
118 memmap[SIFIVE_E_DEV_DTIM].base, NULL);
122 static bool sifive_e_machine_get_revb(Object *obj, Error **errp)
124 SiFiveEState *s = RISCV_E_MACHINE(obj);
126 return s->revb;
129 static void sifive_e_machine_set_revb(Object *obj, bool value, Error **errp)
131 SiFiveEState *s = RISCV_E_MACHINE(obj);
133 s->revb = value;
136 static void sifive_e_machine_instance_init(Object *obj)
138 SiFiveEState *s = RISCV_E_MACHINE(obj);
140 s->revb = false;
143 static void sifive_e_machine_class_init(ObjectClass *oc, void *data)
145 MachineClass *mc = MACHINE_CLASS(oc);
147 mc->desc = "RISC-V Board compatible with SiFive E SDK";
148 mc->init = sifive_e_machine_init;
149 mc->max_cpus = 1;
150 mc->default_cpu_type = SIFIVE_E_CPU;
152 object_class_property_add_bool(oc, "revb", sifive_e_machine_get_revb,
153 sifive_e_machine_set_revb);
154 object_class_property_set_description(oc, "revb",
155 "Set on to tell QEMU that it should model "
156 "the revB HiFive1 board");
159 static const TypeInfo sifive_e_machine_typeinfo = {
160 .name = MACHINE_TYPE_NAME("sifive_e"),
161 .parent = TYPE_MACHINE,
162 .class_init = sifive_e_machine_class_init,
163 .instance_init = sifive_e_machine_instance_init,
164 .instance_size = sizeof(SiFiveEState),
167 static void sifive_e_machine_init_register_types(void)
169 type_register_static(&sifive_e_machine_typeinfo);
172 type_init(sifive_e_machine_init_register_types)
174 static void sifive_e_soc_init(Object *obj)
176 MachineState *ms = MACHINE(qdev_get_machine());
177 SiFiveESoCState *s = RISCV_E_SOC(obj);
179 object_initialize_child(obj, "cpus", &s->cpus, TYPE_RISCV_HART_ARRAY);
180 object_property_set_int(OBJECT(&s->cpus), "num-harts", ms->smp.cpus,
181 &error_abort);
182 object_property_set_int(OBJECT(&s->cpus), "resetvec", 0x1004, &error_abort);
183 object_initialize_child(obj, "riscv.sifive.e.gpio0", &s->gpio,
184 TYPE_SIFIVE_GPIO);
187 static void sifive_e_soc_realize(DeviceState *dev, Error **errp)
189 MachineState *ms = MACHINE(qdev_get_machine());
190 const struct MemmapEntry *memmap = sifive_e_memmap;
191 SiFiveESoCState *s = RISCV_E_SOC(dev);
192 MemoryRegion *sys_mem = get_system_memory();
194 object_property_set_str(OBJECT(&s->cpus), "cpu-type", ms->cpu_type,
195 &error_abort);
196 sysbus_realize(SYS_BUS_DEVICE(&s->cpus), &error_abort);
198 /* Mask ROM */
199 memory_region_init_rom(&s->mask_rom, OBJECT(dev), "riscv.sifive.e.mrom",
200 memmap[SIFIVE_E_DEV_MROM].size, &error_fatal);
201 memory_region_add_subregion(sys_mem,
202 memmap[SIFIVE_E_DEV_MROM].base, &s->mask_rom);
204 /* MMIO */
205 s->plic = sifive_plic_create(memmap[SIFIVE_E_DEV_PLIC].base,
206 (char *)SIFIVE_E_PLIC_HART_CONFIG, 0,
207 SIFIVE_E_PLIC_NUM_SOURCES,
208 SIFIVE_E_PLIC_NUM_PRIORITIES,
209 SIFIVE_E_PLIC_PRIORITY_BASE,
210 SIFIVE_E_PLIC_PENDING_BASE,
211 SIFIVE_E_PLIC_ENABLE_BASE,
212 SIFIVE_E_PLIC_ENABLE_STRIDE,
213 SIFIVE_E_PLIC_CONTEXT_BASE,
214 SIFIVE_E_PLIC_CONTEXT_STRIDE,
215 memmap[SIFIVE_E_DEV_PLIC].size);
216 sifive_clint_create(memmap[SIFIVE_E_DEV_CLINT].base,
217 memmap[SIFIVE_E_DEV_CLINT].size, 0, ms->smp.cpus,
218 SIFIVE_SIP_BASE, SIFIVE_TIMECMP_BASE, SIFIVE_TIME_BASE,
219 SIFIVE_CLINT_TIMEBASE_FREQ, false);
220 create_unimplemented_device("riscv.sifive.e.aon",
221 memmap[SIFIVE_E_DEV_AON].base, memmap[SIFIVE_E_DEV_AON].size);
222 sifive_e_prci_create(memmap[SIFIVE_E_DEV_PRCI].base);
224 /* GPIO */
226 if (!sysbus_realize(SYS_BUS_DEVICE(&s->gpio), errp)) {
227 return;
230 /* Map GPIO registers */
231 sysbus_mmio_map(SYS_BUS_DEVICE(&s->gpio), 0, memmap[SIFIVE_E_DEV_GPIO0].base);
233 /* Pass all GPIOs to the SOC layer so they are available to the board */
234 qdev_pass_gpios(DEVICE(&s->gpio), dev, NULL);
236 /* Connect GPIO interrupts to the PLIC */
237 for (int i = 0; i < 32; i++) {
238 sysbus_connect_irq(SYS_BUS_DEVICE(&s->gpio), i,
239 qdev_get_gpio_in(DEVICE(s->plic),
240 SIFIVE_E_GPIO0_IRQ0 + i));
243 sifive_uart_create(sys_mem, memmap[SIFIVE_E_DEV_UART0].base,
244 serial_hd(0), qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_E_UART0_IRQ));
245 create_unimplemented_device("riscv.sifive.e.qspi0",
246 memmap[SIFIVE_E_DEV_QSPI0].base, memmap[SIFIVE_E_DEV_QSPI0].size);
247 create_unimplemented_device("riscv.sifive.e.pwm0",
248 memmap[SIFIVE_E_DEV_PWM0].base, memmap[SIFIVE_E_DEV_PWM0].size);
249 sifive_uart_create(sys_mem, memmap[SIFIVE_E_DEV_UART1].base,
250 serial_hd(1), qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_E_UART1_IRQ));
251 create_unimplemented_device("riscv.sifive.e.qspi1",
252 memmap[SIFIVE_E_DEV_QSPI1].base, memmap[SIFIVE_E_DEV_QSPI1].size);
253 create_unimplemented_device("riscv.sifive.e.pwm1",
254 memmap[SIFIVE_E_DEV_PWM1].base, memmap[SIFIVE_E_DEV_PWM1].size);
255 create_unimplemented_device("riscv.sifive.e.qspi2",
256 memmap[SIFIVE_E_DEV_QSPI2].base, memmap[SIFIVE_E_DEV_QSPI2].size);
257 create_unimplemented_device("riscv.sifive.e.pwm2",
258 memmap[SIFIVE_E_DEV_PWM2].base, memmap[SIFIVE_E_DEV_PWM2].size);
260 /* Flash memory */
261 memory_region_init_rom(&s->xip_mem, OBJECT(dev), "riscv.sifive.e.xip",
262 memmap[SIFIVE_E_DEV_XIP].size, &error_fatal);
263 memory_region_add_subregion(sys_mem, memmap[SIFIVE_E_DEV_XIP].base,
264 &s->xip_mem);
267 static void sifive_e_soc_class_init(ObjectClass *oc, void *data)
269 DeviceClass *dc = DEVICE_CLASS(oc);
271 dc->realize = sifive_e_soc_realize;
272 /* Reason: Uses serial_hds in realize function, thus can't be used twice */
273 dc->user_creatable = false;
276 static const TypeInfo sifive_e_soc_type_info = {
277 .name = TYPE_RISCV_E_SOC,
278 .parent = TYPE_DEVICE,
279 .instance_size = sizeof(SiFiveESoCState),
280 .instance_init = sifive_e_soc_init,
281 .class_init = sifive_e_soc_class_init,
284 static void sifive_e_soc_register_types(void)
286 type_register_static(&sifive_e_soc_type_info);
289 type_init(sifive_e_soc_register_types)