]> git.proxmox.com Git - mirror_qemu.git/blame - hw/riscv/sifive_u.c
hw/intc: Upgrade the SiFive CLINT implementation to RISC-V ACLINT
[mirror_qemu.git] / hw / riscv / sifive_u.c
CommitLineData
a7240d1e
MC
1/*
2 * QEMU RISC-V Board Compatible with SiFive Freedom U SDK
3 *
4 * Copyright (c) 2016-2017 Sagar Karandikar, sagark@eecs.berkeley.edu
5 * Copyright (c) 2017 SiFive, Inc.
7b6bb66f 6 * Copyright (c) 2019 Bin Meng <bmeng.cn@gmail.com>
a7240d1e
MC
7 *
8 * Provides a board compatible with the SiFive Freedom U SDK:
9 *
10 * 0) UART
11 * 1) CLINT (Core Level Interruptor)
12 * 2) PLIC (Platform Level Interrupt Controller)
af14c840 13 * 3) PRCI (Power, Reset, Clock, Interrupt)
8a88b9f5
BM
14 * 4) GPIO (General Purpose Input/Output Controller)
15 * 5) OTP (One-Time Programmable) memory with stored serial number
16 * 6) GEM (Gigabit Ethernet Controller) and management block
834e027a 17 * 7) DMA (Direct Memory Access Controller)
145b2991 18 * 8) SPI0 connected to an SPI flash
722f1352 19 * 9) SPI2 connected to an SD card
ea6eaa06 20 * 10) PWM0 and PWM1
a7240d1e 21 *
f3d47d58 22 * This board currently generates devicetree dynamically that indicates at least
ecdfe393 23 * two harts and up to five harts.
a7240d1e
MC
24 *
25 * This program is free software; you can redistribute it and/or modify it
26 * under the terms and conditions of the GNU General Public License,
27 * version 2 or later, as published by the Free Software Foundation.
28 *
29 * This program is distributed in the hope it will be useful, but WITHOUT
30 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
31 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
32 * more details.
33 *
34 * You should have received a copy of the GNU General Public License along with
35 * this program. If not, see <http://www.gnu.org/licenses/>.
36 */
37
38#include "qemu/osdep.h"
a7240d1e
MC
39#include "qemu/error-report.h"
40#include "qapi/error.h"
3ca109c3 41#include "qapi/visitor.h"
a7240d1e 42#include "hw/boards.h"
5133ed17 43#include "hw/irq.h"
a7240d1e
MC
44#include "hw/loader.h"
45#include "hw/sysbus.h"
46#include "hw/char/serial.h"
ecdfe393 47#include "hw/cpu/cluster.h"
7b6bb66f 48#include "hw/misc/unimp.h"
145b2991 49#include "hw/ssi/ssi.h"
a7240d1e
MC
50#include "target/riscv/cpu.h"
51#include "hw/riscv/riscv_hart.h"
a7240d1e 52#include "hw/riscv/sifive_u.h"
0ac24d56 53#include "hw/riscv/boot.h"
b609b7e3 54#include "hw/char/sifive_uart.h"
cc63a182 55#include "hw/intc/riscv_aclint.h"
84fcf3c1 56#include "hw/intc/sifive_plic.h"
a7240d1e 57#include "chardev/char.h"
7b6bb66f 58#include "net/eth.h"
a7240d1e 59#include "sysemu/device_tree.h"
5133ed17 60#include "sysemu/runstate.h"
46517dd4 61#include "sysemu/sysemu.h"
a7240d1e 62
5aec3247
MC
63#include <libfdt.h>
64
074ca702
BM
65/* CLINT timebase frequency */
66#define CLINT_TIMEBASE_FREQ 1000000
67
73261285 68static const MemMapEntry sifive_u_memmap[] = {
13b8c354
EH
69 [SIFIVE_U_DEV_DEBUG] = { 0x0, 0x100 },
70 [SIFIVE_U_DEV_MROM] = { 0x1000, 0xf000 },
71 [SIFIVE_U_DEV_CLINT] = { 0x2000000, 0x10000 },
72 [SIFIVE_U_DEV_L2CC] = { 0x2010000, 0x1000 },
73 [SIFIVE_U_DEV_PDMA] = { 0x3000000, 0x100000 },
74 [SIFIVE_U_DEV_L2LIM] = { 0x8000000, 0x2000000 },
75 [SIFIVE_U_DEV_PLIC] = { 0xc000000, 0x4000000 },
76 [SIFIVE_U_DEV_PRCI] = { 0x10000000, 0x1000 },
77 [SIFIVE_U_DEV_UART0] = { 0x10010000, 0x1000 },
78 [SIFIVE_U_DEV_UART1] = { 0x10011000, 0x1000 },
ea6eaa06
AF
79 [SIFIVE_U_DEV_PWM0] = { 0x10020000, 0x1000 },
80 [SIFIVE_U_DEV_PWM1] = { 0x10021000, 0x1000 },
145b2991 81 [SIFIVE_U_DEV_QSPI0] = { 0x10040000, 0x1000 },
722f1352 82 [SIFIVE_U_DEV_QSPI2] = { 0x10050000, 0x1000 },
13b8c354
EH
83 [SIFIVE_U_DEV_GPIO] = { 0x10060000, 0x1000 },
84 [SIFIVE_U_DEV_OTP] = { 0x10070000, 0x1000 },
85 [SIFIVE_U_DEV_GEM] = { 0x10090000, 0x2000 },
86 [SIFIVE_U_DEV_GEM_MGMT] = { 0x100a0000, 0x1000 },
87 [SIFIVE_U_DEV_DMC] = { 0x100b0000, 0x10000 },
88 [SIFIVE_U_DEV_FLASH0] = { 0x20000000, 0x10000000 },
89 [SIFIVE_U_DEV_DRAM] = { 0x80000000, 0x0 },
a7240d1e
MC
90};
91
5461c4fe 92#define OTP_SERIAL 1
5a7f76a3
AF
93#define GEM_REVISION 0x10070109
94
73261285 95static void create_fdt(SiFiveUState *s, const MemMapEntry *memmap,
2206ffa6 96 uint64_t mem_size, const char *cmdline, bool is_32_bit)
a7240d1e 97{
ecdfe393 98 MachineState *ms = MACHINE(qdev_get_machine());
a7240d1e
MC
99 void *fdt;
100 int cpu;
101 uint32_t *cells;
102 char *nodename;
5133ed17 103 uint32_t plic_phandle, prci_phandle, gpio_phandle, phandle = 1;
7b6bb66f 104 uint32_t hfclk_phandle, rtcclk_phandle, phy_phandle;
cb53b283 105 static const char * const ethclk_names[2] = { "pclk", "hclk" };
7cfbb17f
BM
106 static const char * const clint_compat[2] = {
107 "sifive,clint0", "riscv,clint0"
108 };
60bb5407
BM
109 static const char * const plic_compat[2] = {
110 "sifive,plic-1.0.0", "riscv,plic0"
111 };
a7240d1e 112
f2ce39b4
PB
113 if (ms->dtb) {
114 fdt = s->fdt = load_device_tree(ms->dtb, &s->fdt_size);
d5c90cf3
AP
115 if (!fdt) {
116 error_report("load_device_tree() failed");
117 exit(1);
118 }
119 goto update_bootargs;
120 } else {
121 fdt = s->fdt = create_device_tree(&s->fdt_size);
122 if (!fdt) {
123 error_report("create_device_tree() failed");
124 exit(1);
125 }
a7240d1e
MC
126 }
127
d372e748
BM
128 qemu_fdt_setprop_string(fdt, "/", "model", "SiFive HiFive Unleashed A00");
129 qemu_fdt_setprop_string(fdt, "/", "compatible",
130 "sifive,hifive-unleashed-a00");
a7240d1e
MC
131 qemu_fdt_setprop_cell(fdt, "/", "#size-cells", 0x2);
132 qemu_fdt_setprop_cell(fdt, "/", "#address-cells", 0x2);
133
134 qemu_fdt_add_subnode(fdt, "/soc");
135 qemu_fdt_setprop(fdt, "/soc", "ranges", NULL, 0);
2a1a6f6d 136 qemu_fdt_setprop_string(fdt, "/soc", "compatible", "simple-bus");
a7240d1e
MC
137 qemu_fdt_setprop_cell(fdt, "/soc", "#size-cells", 0x2);
138 qemu_fdt_setprop_cell(fdt, "/soc", "#address-cells", 0x2);
139
e1724d09
BM
140 hfclk_phandle = phandle++;
141 nodename = g_strdup_printf("/hfclk");
142 qemu_fdt_add_subnode(fdt, nodename);
143 qemu_fdt_setprop_cell(fdt, nodename, "phandle", hfclk_phandle);
144 qemu_fdt_setprop_string(fdt, nodename, "clock-output-names", "hfclk");
145 qemu_fdt_setprop_cell(fdt, nodename, "clock-frequency",
146 SIFIVE_U_HFCLK_FREQ);
147 qemu_fdt_setprop_string(fdt, nodename, "compatible", "fixed-clock");
148 qemu_fdt_setprop_cell(fdt, nodename, "#clock-cells", 0x0);
149 g_free(nodename);
150
151 rtcclk_phandle = phandle++;
152 nodename = g_strdup_printf("/rtcclk");
153 qemu_fdt_add_subnode(fdt, nodename);
154 qemu_fdt_setprop_cell(fdt, nodename, "phandle", rtcclk_phandle);
155 qemu_fdt_setprop_string(fdt, nodename, "clock-output-names", "rtcclk");
156 qemu_fdt_setprop_cell(fdt, nodename, "clock-frequency",
157 SIFIVE_U_RTCCLK_FREQ);
158 qemu_fdt_setprop_string(fdt, nodename, "compatible", "fixed-clock");
159 qemu_fdt_setprop_cell(fdt, nodename, "#clock-cells", 0x0);
160 g_free(nodename);
161
a7240d1e 162 nodename = g_strdup_printf("/memory@%lx",
13b8c354 163 (long)memmap[SIFIVE_U_DEV_DRAM].base);
a7240d1e
MC
164 qemu_fdt_add_subnode(fdt, nodename);
165 qemu_fdt_setprop_cells(fdt, nodename, "reg",
13b8c354 166 memmap[SIFIVE_U_DEV_DRAM].base >> 32, memmap[SIFIVE_U_DEV_DRAM].base,
a7240d1e
MC
167 mem_size >> 32, mem_size);
168 qemu_fdt_setprop_string(fdt, nodename, "device_type", "memory");
169 g_free(nodename);
170
171 qemu_fdt_add_subnode(fdt, "/cpus");
2a8756ed 172 qemu_fdt_setprop_cell(fdt, "/cpus", "timebase-frequency",
074ca702 173 CLINT_TIMEBASE_FREQ);
a7240d1e
MC
174 qemu_fdt_setprop_cell(fdt, "/cpus", "#size-cells", 0x0);
175 qemu_fdt_setprop_cell(fdt, "/cpus", "#address-cells", 0x1);
176
ecdfe393 177 for (cpu = ms->smp.cpus - 1; cpu >= 0; cpu--) {
382cb439 178 int cpu_phandle = phandle++;
a7240d1e
MC
179 nodename = g_strdup_printf("/cpus/cpu@%d", cpu);
180 char *intc = g_strdup_printf("/cpus/cpu@%d/interrupt-controller", cpu);
ecdfe393 181 char *isa;
a7240d1e 182 qemu_fdt_add_subnode(fdt, nodename);
ecdfe393
BM
183 /* cpu 0 is the management hart that does not have mmu */
184 if (cpu != 0) {
2206ffa6
AF
185 if (is_32_bit) {
186 qemu_fdt_setprop_string(fdt, nodename, "mmu-type", "riscv,sv32");
187 } else {
188 qemu_fdt_setprop_string(fdt, nodename, "mmu-type", "riscv,sv48");
189 }
ecdfe393
BM
190 isa = riscv_isa_string(&s->soc.u_cpus.harts[cpu - 1]);
191 } else {
192 isa = riscv_isa_string(&s->soc.e_cpus.harts[0]);
193 }
a7240d1e
MC
194 qemu_fdt_setprop_string(fdt, nodename, "riscv,isa", isa);
195 qemu_fdt_setprop_string(fdt, nodename, "compatible", "riscv");
196 qemu_fdt_setprop_string(fdt, nodename, "status", "okay");
197 qemu_fdt_setprop_cell(fdt, nodename, "reg", cpu);
198 qemu_fdt_setprop_string(fdt, nodename, "device_type", "cpu");
199 qemu_fdt_add_subnode(fdt, intc);
382cb439 200 qemu_fdt_setprop_cell(fdt, intc, "phandle", cpu_phandle);
a7240d1e
MC
201 qemu_fdt_setprop_string(fdt, intc, "compatible", "riscv,cpu-intc");
202 qemu_fdt_setprop(fdt, intc, "interrupt-controller", NULL, 0);
203 qemu_fdt_setprop_cell(fdt, intc, "#interrupt-cells", 1);
204 g_free(isa);
205 g_free(intc);
206 g_free(nodename);
207 }
208
ecdfe393
BM
209 cells = g_new0(uint32_t, ms->smp.cpus * 4);
210 for (cpu = 0; cpu < ms->smp.cpus; cpu++) {
a7240d1e
MC
211 nodename =
212 g_strdup_printf("/cpus/cpu@%d/interrupt-controller", cpu);
213 uint32_t intc_phandle = qemu_fdt_get_phandle(fdt, nodename);
214 cells[cpu * 4 + 0] = cpu_to_be32(intc_phandle);
215 cells[cpu * 4 + 1] = cpu_to_be32(IRQ_M_SOFT);
216 cells[cpu * 4 + 2] = cpu_to_be32(intc_phandle);
217 cells[cpu * 4 + 3] = cpu_to_be32(IRQ_M_TIMER);
218 g_free(nodename);
219 }
220 nodename = g_strdup_printf("/soc/clint@%lx",
13b8c354 221 (long)memmap[SIFIVE_U_DEV_CLINT].base);
a7240d1e 222 qemu_fdt_add_subnode(fdt, nodename);
7cfbb17f
BM
223 qemu_fdt_setprop_string_array(fdt, nodename, "compatible",
224 (char **)&clint_compat, ARRAY_SIZE(clint_compat));
a7240d1e 225 qemu_fdt_setprop_cells(fdt, nodename, "reg",
13b8c354
EH
226 0x0, memmap[SIFIVE_U_DEV_CLINT].base,
227 0x0, memmap[SIFIVE_U_DEV_CLINT].size);
a7240d1e 228 qemu_fdt_setprop(fdt, nodename, "interrupts-extended",
ecdfe393 229 cells, ms->smp.cpus * sizeof(uint32_t) * 4);
a7240d1e
MC
230 g_free(cells);
231 g_free(nodename);
232
ea85f27d 233 nodename = g_strdup_printf("/soc/otp@%lx",
13b8c354 234 (long)memmap[SIFIVE_U_DEV_OTP].base);
ea85f27d
BM
235 qemu_fdt_add_subnode(fdt, nodename);
236 qemu_fdt_setprop_cell(fdt, nodename, "fuse-count", SIFIVE_U_OTP_REG_SIZE);
237 qemu_fdt_setprop_cells(fdt, nodename, "reg",
13b8c354
EH
238 0x0, memmap[SIFIVE_U_DEV_OTP].base,
239 0x0, memmap[SIFIVE_U_DEV_OTP].size);
ea85f27d
BM
240 qemu_fdt_setprop_string(fdt, nodename, "compatible",
241 "sifive,fu540-c000-otp");
242 g_free(nodename);
243
af14c840
BM
244 prci_phandle = phandle++;
245 nodename = g_strdup_printf("/soc/clock-controller@%lx",
13b8c354 246 (long)memmap[SIFIVE_U_DEV_PRCI].base);
af14c840
BM
247 qemu_fdt_add_subnode(fdt, nodename);
248 qemu_fdt_setprop_cell(fdt, nodename, "phandle", prci_phandle);
249 qemu_fdt_setprop_cell(fdt, nodename, "#clock-cells", 0x1);
250 qemu_fdt_setprop_cells(fdt, nodename, "clocks",
251 hfclk_phandle, rtcclk_phandle);
252 qemu_fdt_setprop_cells(fdt, nodename, "reg",
13b8c354
EH
253 0x0, memmap[SIFIVE_U_DEV_PRCI].base,
254 0x0, memmap[SIFIVE_U_DEV_PRCI].size);
af14c840
BM
255 qemu_fdt_setprop_string(fdt, nodename, "compatible",
256 "sifive,fu540-c000-prci");
257 g_free(nodename);
258
382cb439 259 plic_phandle = phandle++;
ecdfe393
BM
260 cells = g_new0(uint32_t, ms->smp.cpus * 4 - 2);
261 for (cpu = 0; cpu < ms->smp.cpus; cpu++) {
a7240d1e
MC
262 nodename =
263 g_strdup_printf("/cpus/cpu@%d/interrupt-controller", cpu);
264 uint32_t intc_phandle = qemu_fdt_get_phandle(fdt, nodename);
ecdfe393
BM
265 /* cpu 0 is the management hart that does not have S-mode */
266 if (cpu == 0) {
267 cells[0] = cpu_to_be32(intc_phandle);
268 cells[1] = cpu_to_be32(IRQ_M_EXT);
269 } else {
270 cells[cpu * 4 - 2] = cpu_to_be32(intc_phandle);
271 cells[cpu * 4 - 1] = cpu_to_be32(IRQ_M_EXT);
272 cells[cpu * 4 + 0] = cpu_to_be32(intc_phandle);
273 cells[cpu * 4 + 1] = cpu_to_be32(IRQ_S_EXT);
274 }
a7240d1e
MC
275 g_free(nodename);
276 }
277 nodename = g_strdup_printf("/soc/interrupt-controller@%lx",
13b8c354 278 (long)memmap[SIFIVE_U_DEV_PLIC].base);
a7240d1e
MC
279 qemu_fdt_add_subnode(fdt, nodename);
280 qemu_fdt_setprop_cell(fdt, nodename, "#interrupt-cells", 1);
60bb5407
BM
281 qemu_fdt_setprop_string_array(fdt, nodename, "compatible",
282 (char **)&plic_compat, ARRAY_SIZE(plic_compat));
a7240d1e
MC
283 qemu_fdt_setprop(fdt, nodename, "interrupt-controller", NULL, 0);
284 qemu_fdt_setprop(fdt, nodename, "interrupts-extended",
ecdfe393 285 cells, (ms->smp.cpus * 4 - 2) * sizeof(uint32_t));
a7240d1e 286 qemu_fdt_setprop_cells(fdt, nodename, "reg",
13b8c354
EH
287 0x0, memmap[SIFIVE_U_DEV_PLIC].base,
288 0x0, memmap[SIFIVE_U_DEV_PLIC].size);
98ceee7f 289 qemu_fdt_setprop_cell(fdt, nodename, "riscv,ndev", 0x35);
04e7edd1 290 qemu_fdt_setprop_cell(fdt, nodename, "phandle", plic_phandle);
a7240d1e
MC
291 plic_phandle = qemu_fdt_get_phandle(fdt, nodename);
292 g_free(cells);
293 g_free(nodename);
294
5133ed17 295 gpio_phandle = phandle++;
8a88b9f5 296 nodename = g_strdup_printf("/soc/gpio@%lx",
13b8c354 297 (long)memmap[SIFIVE_U_DEV_GPIO].base);
8a88b9f5 298 qemu_fdt_add_subnode(fdt, nodename);
5133ed17 299 qemu_fdt_setprop_cell(fdt, nodename, "phandle", gpio_phandle);
8a88b9f5
BM
300 qemu_fdt_setprop_cells(fdt, nodename, "clocks",
301 prci_phandle, PRCI_CLK_TLCLK);
302 qemu_fdt_setprop_cell(fdt, nodename, "#interrupt-cells", 2);
303 qemu_fdt_setprop(fdt, nodename, "interrupt-controller", NULL, 0);
304 qemu_fdt_setprop_cell(fdt, nodename, "#gpio-cells", 2);
305 qemu_fdt_setprop(fdt, nodename, "gpio-controller", NULL, 0);
306 qemu_fdt_setprop_cells(fdt, nodename, "reg",
13b8c354
EH
307 0x0, memmap[SIFIVE_U_DEV_GPIO].base,
308 0x0, memmap[SIFIVE_U_DEV_GPIO].size);
8a88b9f5
BM
309 qemu_fdt_setprop_cells(fdt, nodename, "interrupts", SIFIVE_U_GPIO_IRQ0,
310 SIFIVE_U_GPIO_IRQ1, SIFIVE_U_GPIO_IRQ2, SIFIVE_U_GPIO_IRQ3,
311 SIFIVE_U_GPIO_IRQ4, SIFIVE_U_GPIO_IRQ5, SIFIVE_U_GPIO_IRQ6,
312 SIFIVE_U_GPIO_IRQ7, SIFIVE_U_GPIO_IRQ8, SIFIVE_U_GPIO_IRQ9,
313 SIFIVE_U_GPIO_IRQ10, SIFIVE_U_GPIO_IRQ11, SIFIVE_U_GPIO_IRQ12,
314 SIFIVE_U_GPIO_IRQ13, SIFIVE_U_GPIO_IRQ14, SIFIVE_U_GPIO_IRQ15);
315 qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle);
316 qemu_fdt_setprop_string(fdt, nodename, "compatible", "sifive,gpio0");
317 g_free(nodename);
318
5133ed17
BM
319 nodename = g_strdup_printf("/gpio-restart");
320 qemu_fdt_add_subnode(fdt, nodename);
321 qemu_fdt_setprop_cells(fdt, nodename, "gpios", gpio_phandle, 10, 1);
322 qemu_fdt_setprop_string(fdt, nodename, "compatible", "gpio-restart");
323 g_free(nodename);
324
834e027a 325 nodename = g_strdup_printf("/soc/dma@%lx",
13b8c354 326 (long)memmap[SIFIVE_U_DEV_PDMA].base);
834e027a
BM
327 qemu_fdt_add_subnode(fdt, nodename);
328 qemu_fdt_setprop_cell(fdt, nodename, "#dma-cells", 1);
329 qemu_fdt_setprop_cells(fdt, nodename, "interrupts",
330 SIFIVE_U_PDMA_IRQ0, SIFIVE_U_PDMA_IRQ1, SIFIVE_U_PDMA_IRQ2,
331 SIFIVE_U_PDMA_IRQ3, SIFIVE_U_PDMA_IRQ4, SIFIVE_U_PDMA_IRQ5,
332 SIFIVE_U_PDMA_IRQ6, SIFIVE_U_PDMA_IRQ7);
333 qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle);
334 qemu_fdt_setprop_cells(fdt, nodename, "reg",
13b8c354
EH
335 0x0, memmap[SIFIVE_U_DEV_PDMA].base,
336 0x0, memmap[SIFIVE_U_DEV_PDMA].size);
834e027a
BM
337 qemu_fdt_setprop_string(fdt, nodename, "compatible",
338 "sifive,fu540-c000-pdma");
339 g_free(nodename);
340
6eaf9cf5 341 nodename = g_strdup_printf("/soc/cache-controller@%lx",
13b8c354 342 (long)memmap[SIFIVE_U_DEV_L2CC].base);
6eaf9cf5
BM
343 qemu_fdt_add_subnode(fdt, nodename);
344 qemu_fdt_setprop_cells(fdt, nodename, "reg",
13b8c354
EH
345 0x0, memmap[SIFIVE_U_DEV_L2CC].base,
346 0x0, memmap[SIFIVE_U_DEV_L2CC].size);
6eaf9cf5
BM
347 qemu_fdt_setprop_cells(fdt, nodename, "interrupts",
348 SIFIVE_U_L2CC_IRQ0, SIFIVE_U_L2CC_IRQ1, SIFIVE_U_L2CC_IRQ2);
349 qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle);
350 qemu_fdt_setprop(fdt, nodename, "cache-unified", NULL, 0);
351 qemu_fdt_setprop_cell(fdt, nodename, "cache-size", 2097152);
352 qemu_fdt_setprop_cell(fdt, nodename, "cache-sets", 1024);
353 qemu_fdt_setprop_cell(fdt, nodename, "cache-level", 2);
354 qemu_fdt_setprop_cell(fdt, nodename, "cache-block-size", 64);
355 qemu_fdt_setprop_string(fdt, nodename, "compatible",
356 "sifive,fu540-c000-ccache");
357 g_free(nodename);
358
722f1352
BM
359 nodename = g_strdup_printf("/soc/spi@%lx",
360 (long)memmap[SIFIVE_U_DEV_QSPI2].base);
361 qemu_fdt_add_subnode(fdt, nodename);
362 qemu_fdt_setprop_cell(fdt, nodename, "#size-cells", 0);
363 qemu_fdt_setprop_cell(fdt, nodename, "#address-cells", 1);
364 qemu_fdt_setprop_cells(fdt, nodename, "clocks",
365 prci_phandle, PRCI_CLK_TLCLK);
366 qemu_fdt_setprop_cell(fdt, nodename, "interrupts", SIFIVE_U_QSPI2_IRQ);
367 qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle);
368 qemu_fdt_setprop_cells(fdt, nodename, "reg",
369 0x0, memmap[SIFIVE_U_DEV_QSPI2].base,
370 0x0, memmap[SIFIVE_U_DEV_QSPI2].size);
371 qemu_fdt_setprop_string(fdt, nodename, "compatible", "sifive,spi0");
372 g_free(nodename);
373
374 nodename = g_strdup_printf("/soc/spi@%lx/mmc@0",
375 (long)memmap[SIFIVE_U_DEV_QSPI2].base);
376 qemu_fdt_add_subnode(fdt, nodename);
377 qemu_fdt_setprop(fdt, nodename, "disable-wp", NULL, 0);
378 qemu_fdt_setprop_cells(fdt, nodename, "voltage-ranges", 3300, 3300);
379 qemu_fdt_setprop_cell(fdt, nodename, "spi-max-frequency", 20000000);
380 qemu_fdt_setprop_cell(fdt, nodename, "reg", 0);
381 qemu_fdt_setprop_string(fdt, nodename, "compatible", "mmc-spi-slot");
382 g_free(nodename);
383
145b2991
BM
384 nodename = g_strdup_printf("/soc/spi@%lx",
385 (long)memmap[SIFIVE_U_DEV_QSPI0].base);
386 qemu_fdt_add_subnode(fdt, nodename);
387 qemu_fdt_setprop_cell(fdt, nodename, "#size-cells", 0);
388 qemu_fdt_setprop_cell(fdt, nodename, "#address-cells", 1);
389 qemu_fdt_setprop_cells(fdt, nodename, "clocks",
390 prci_phandle, PRCI_CLK_TLCLK);
391 qemu_fdt_setprop_cell(fdt, nodename, "interrupts", SIFIVE_U_QSPI0_IRQ);
392 qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle);
393 qemu_fdt_setprop_cells(fdt, nodename, "reg",
394 0x0, memmap[SIFIVE_U_DEV_QSPI0].base,
395 0x0, memmap[SIFIVE_U_DEV_QSPI0].size);
396 qemu_fdt_setprop_string(fdt, nodename, "compatible", "sifive,spi0");
397 g_free(nodename);
398
399 nodename = g_strdup_printf("/soc/spi@%lx/flash@0",
400 (long)memmap[SIFIVE_U_DEV_QSPI0].base);
401 qemu_fdt_add_subnode(fdt, nodename);
402 qemu_fdt_setprop_cell(fdt, nodename, "spi-rx-bus-width", 4);
403 qemu_fdt_setprop_cell(fdt, nodename, "spi-tx-bus-width", 4);
404 qemu_fdt_setprop(fdt, nodename, "m25p,fast-read", NULL, 0);
405 qemu_fdt_setprop_cell(fdt, nodename, "spi-max-frequency", 50000000);
406 qemu_fdt_setprop_cell(fdt, nodename, "reg", 0);
407 qemu_fdt_setprop_string(fdt, nodename, "compatible", "jedec,spi-nor");
408 g_free(nodename);
409
7b6bb66f 410 phy_phandle = phandle++;
5a7f76a3 411 nodename = g_strdup_printf("/soc/ethernet@%lx",
13b8c354 412 (long)memmap[SIFIVE_U_DEV_GEM].base);
5a7f76a3 413 qemu_fdt_add_subnode(fdt, nodename);
7b6bb66f
BM
414 qemu_fdt_setprop_string(fdt, nodename, "compatible",
415 "sifive,fu540-c000-gem");
5a7f76a3 416 qemu_fdt_setprop_cells(fdt, nodename, "reg",
13b8c354
EH
417 0x0, memmap[SIFIVE_U_DEV_GEM].base,
418 0x0, memmap[SIFIVE_U_DEV_GEM].size,
419 0x0, memmap[SIFIVE_U_DEV_GEM_MGMT].base,
420 0x0, memmap[SIFIVE_U_DEV_GEM_MGMT].size);
5a7f76a3
AF
421 qemu_fdt_setprop_string(fdt, nodename, "reg-names", "control");
422 qemu_fdt_setprop_string(fdt, nodename, "phy-mode", "gmii");
7b6bb66f 423 qemu_fdt_setprop_cell(fdt, nodename, "phy-handle", phy_phandle);
04e7edd1
BM
424 qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle);
425 qemu_fdt_setprop_cell(fdt, nodename, "interrupts", SIFIVE_U_GEM_IRQ);
fe93582c 426 qemu_fdt_setprop_cells(fdt, nodename, "clocks",
806c64b7 427 prci_phandle, PRCI_CLK_GEMGXLPLL, prci_phandle, PRCI_CLK_GEMGXLPLL);
cb53b283
BM
428 qemu_fdt_setprop_string_array(fdt, nodename, "clock-names",
429 (char **)&ethclk_names, ARRAY_SIZE(ethclk_names));
7b6bb66f
BM
430 qemu_fdt_setprop(fdt, nodename, "local-mac-address",
431 s->soc.gem.conf.macaddr.a, ETH_ALEN);
04e7edd1
BM
432 qemu_fdt_setprop_cell(fdt, nodename, "#address-cells", 1);
433 qemu_fdt_setprop_cell(fdt, nodename, "#size-cells", 0);
c3a28b5d
BM
434
435 qemu_fdt_add_subnode(fdt, "/aliases");
436 qemu_fdt_setprop_string(fdt, "/aliases", "ethernet0", nodename);
437
5a7f76a3
AF
438 g_free(nodename);
439
440 nodename = g_strdup_printf("/soc/ethernet@%lx/ethernet-phy@0",
13b8c354 441 (long)memmap[SIFIVE_U_DEV_GEM].base);
5a7f76a3 442 qemu_fdt_add_subnode(fdt, nodename);
7b6bb66f 443 qemu_fdt_setprop_cell(fdt, nodename, "phandle", phy_phandle);
04e7edd1 444 qemu_fdt_setprop_cell(fdt, nodename, "reg", 0x0);
5a7f76a3
AF
445 g_free(nodename);
446
ea6eaa06
AF
447 nodename = g_strdup_printf("/soc/pwm@%lx",
448 (long)memmap[SIFIVE_U_DEV_PWM0].base);
449 qemu_fdt_add_subnode(fdt, nodename);
450 qemu_fdt_setprop_string(fdt, nodename, "compatible", "sifive,pwm0");
451 qemu_fdt_setprop_cells(fdt, nodename, "reg",
452 0x0, memmap[SIFIVE_U_DEV_PWM0].base,
453 0x0, memmap[SIFIVE_U_DEV_PWM0].size);
454 qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle);
455 qemu_fdt_setprop_cells(fdt, nodename, "interrupts",
456 SIFIVE_U_PWM0_IRQ0, SIFIVE_U_PWM0_IRQ1,
457 SIFIVE_U_PWM0_IRQ2, SIFIVE_U_PWM0_IRQ3);
458 qemu_fdt_setprop_cells(fdt, nodename, "clocks",
459 prci_phandle, PRCI_CLK_TLCLK);
460 qemu_fdt_setprop_cell(fdt, nodename, "#pwm-cells", 0);
461 g_free(nodename);
462
463 nodename = g_strdup_printf("/soc/pwm@%lx",
464 (long)memmap[SIFIVE_U_DEV_PWM1].base);
465 qemu_fdt_add_subnode(fdt, nodename);
466 qemu_fdt_setprop_string(fdt, nodename, "compatible", "sifive,pwm0");
467 qemu_fdt_setprop_cells(fdt, nodename, "reg",
468 0x0, memmap[SIFIVE_U_DEV_PWM1].base,
469 0x0, memmap[SIFIVE_U_DEV_PWM1].size);
470 qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle);
471 qemu_fdt_setprop_cells(fdt, nodename, "interrupts",
472 SIFIVE_U_PWM1_IRQ0, SIFIVE_U_PWM1_IRQ1,
473 SIFIVE_U_PWM1_IRQ2, SIFIVE_U_PWM1_IRQ3);
474 qemu_fdt_setprop_cells(fdt, nodename, "clocks",
475 prci_phandle, PRCI_CLK_TLCLK);
476 qemu_fdt_setprop_cell(fdt, nodename, "#pwm-cells", 0);
477 g_free(nodename);
478
10b43754
AP
479 nodename = g_strdup_printf("/soc/serial@%lx",
480 (long)memmap[SIFIVE_U_DEV_UART1].base);
481 qemu_fdt_add_subnode(fdt, nodename);
482 qemu_fdt_setprop_string(fdt, nodename, "compatible", "sifive,uart0");
483 qemu_fdt_setprop_cells(fdt, nodename, "reg",
484 0x0, memmap[SIFIVE_U_DEV_UART1].base,
485 0x0, memmap[SIFIVE_U_DEV_UART1].size);
486 qemu_fdt_setprop_cells(fdt, nodename, "clocks",
487 prci_phandle, PRCI_CLK_TLCLK);
488 qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle);
489 qemu_fdt_setprop_cell(fdt, nodename, "interrupts", SIFIVE_U_UART1_IRQ);
490
491 qemu_fdt_setprop_string(fdt, "/aliases", "serial1", nodename);
492 g_free(nodename);
493
5f7134d3 494 nodename = g_strdup_printf("/soc/serial@%lx",
13b8c354 495 (long)memmap[SIFIVE_U_DEV_UART0].base);
a7240d1e
MC
496 qemu_fdt_add_subnode(fdt, nodename);
497 qemu_fdt_setprop_string(fdt, nodename, "compatible", "sifive,uart0");
498 qemu_fdt_setprop_cells(fdt, nodename, "reg",
13b8c354
EH
499 0x0, memmap[SIFIVE_U_DEV_UART0].base,
500 0x0, memmap[SIFIVE_U_DEV_UART0].size);
806c64b7
BM
501 qemu_fdt_setprop_cells(fdt, nodename, "clocks",
502 prci_phandle, PRCI_CLK_TLCLK);
04e7edd1
BM
503 qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle);
504 qemu_fdt_setprop_cell(fdt, nodename, "interrupts", SIFIVE_U_UART0_IRQ);
a7240d1e
MC
505
506 qemu_fdt_add_subnode(fdt, "/chosen");
507 qemu_fdt_setprop_string(fdt, "/chosen", "stdout-path", nodename);
44e6dcd3
GR
508 qemu_fdt_setprop_string(fdt, "/aliases", "serial0", nodename);
509
a7240d1e 510 g_free(nodename);
d5c90cf3
AP
511
512update_bootargs:
513 if (cmdline) {
514 qemu_fdt_setprop_string(fdt, "/chosen", "bootargs", cmdline);
515 }
a7240d1e
MC
516}
517
5133ed17
BM
518static void sifive_u_machine_reset(void *opaque, int n, int level)
519{
520 /* gpio pin active low triggers reset */
521 if (!level) {
522 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
523 }
524}
525
523e3464 526static void sifive_u_machine_init(MachineState *machine)
a7240d1e 527{
73261285 528 const MemMapEntry *memmap = sifive_u_memmap;
687caef1 529 SiFiveUState *s = RISCV_U_MACHINE(machine);
5aec3247 530 MemoryRegion *system_memory = get_system_memory();
a7240d1e 531 MemoryRegion *main_mem = g_new(MemoryRegion, 1);
1b3a2308 532 MemoryRegion *flash0 = g_new(MemoryRegion, 1);
13b8c354 533 target_ulong start_addr = memmap[SIFIVE_U_DEV_DRAM].base;
38bc4e34 534 target_ulong firmware_end_addr, kernel_start_addr;
8590f536 535 uint32_t start_addr_hi32 = 0x00000000;
5aec3247 536 int i;
66b1205b 537 uint32_t fdt_load_addr;
dc144fe1 538 uint64_t kernel_entry;
145b2991 539 DriveInfo *dinfo;
722f1352
BM
540 DeviceState *flash_dev, *sd_dev;
541 qemu_irq flash_cs, sd_cs;
a7240d1e 542
2308092b 543 /* Initialize SoC */
9fc7fc4d 544 object_initialize_child(OBJECT(machine), "soc", &s->soc, TYPE_RISCV_U_SOC);
5325cc34
MA
545 object_property_set_uint(OBJECT(&s->soc), "serial", s->serial,
546 &error_abort);
099be035
AF
547 object_property_set_str(OBJECT(&s->soc), "cpu-type", machine->cpu_type,
548 &error_abort);
ce189ab2 549 qdev_realize(DEVICE(&s->soc), NULL, &error_abort);
a7240d1e
MC
550
551 /* register RAM */
552 memory_region_init_ram(main_mem, NULL, "riscv.sifive.u.ram",
553 machine->ram_size, &error_fatal);
13b8c354 554 memory_region_add_subregion(system_memory, memmap[SIFIVE_U_DEV_DRAM].base,
2308092b 555 main_mem);
a7240d1e 556
1b3a2308
AF
557 /* register QSPI0 Flash */
558 memory_region_init_ram(flash0, NULL, "riscv.sifive.u.flash0",
13b8c354
EH
559 memmap[SIFIVE_U_DEV_FLASH0].size, &error_fatal);
560 memory_region_add_subregion(system_memory, memmap[SIFIVE_U_DEV_FLASH0].base,
1b3a2308
AF
561 flash0);
562
5133ed17
BM
563 /* register gpio-restart */
564 qdev_connect_gpio_out(DEVICE(&(s->soc.gpio)), 10,
565 qemu_allocate_irq(sifive_u_machine_reset, NULL, 0));
566
a7240d1e 567 /* create device tree */
2206ffa6 568 create_fdt(s, memmap, machine->ram_size, machine->kernel_cmdline,
a8259b53 569 riscv_is_32bit(&s->soc.u_cpus));
a7240d1e 570
17aad9f2
BM
571 if (s->start_in_flash) {
572 /*
573 * If start_in_flash property is given, assign s->msel to a value
574 * that representing booting from QSPI0 memory-mapped flash.
575 *
576 * This also means that when both start_in_flash and msel properties
577 * are given, start_in_flash takes the precedence over msel.
578 *
579 * Note this is to keep backward compatibility not to break existing
580 * users that use start_in_flash property.
581 */
582 s->msel = MSEL_MEMMAP_QSPI0_FLASH;
583 }
584
585 switch (s->msel) {
586 case MSEL_MEMMAP_QSPI0_FLASH:
13b8c354 587 start_addr = memmap[SIFIVE_U_DEV_FLASH0].base;
17aad9f2
BM
588 break;
589 case MSEL_L2LIM_QSPI0_FLASH:
590 case MSEL_L2LIM_QSPI2_SD:
13b8c354 591 start_addr = memmap[SIFIVE_U_DEV_L2LIM].base;
17aad9f2
BM
592 break;
593 default:
13b8c354 594 start_addr = memmap[SIFIVE_U_DEV_DRAM].base;
17aad9f2
BM
595 break;
596 }
597
a8259b53 598 if (riscv_is_32bit(&s->soc.u_cpus)) {
2206ffa6 599 firmware_end_addr = riscv_find_and_load_firmware(machine,
a0acd0a1 600 RISCV32_BIOS_BIN, start_addr, NULL);
2206ffa6
AF
601 } else {
602 firmware_end_addr = riscv_find_and_load_firmware(machine,
a0acd0a1 603 RISCV64_BIOS_BIN, start_addr, NULL);
2206ffa6 604 }
b3042223 605
a7240d1e 606 if (machine->kernel_filename) {
a8259b53 607 kernel_start_addr = riscv_calc_kernel_start_addr(&s->soc.u_cpus,
38bc4e34
AF
608 firmware_end_addr);
609
610 kernel_entry = riscv_load_kernel(machine->kernel_filename,
611 kernel_start_addr, NULL);
0f8d4462
GR
612
613 if (machine->initrd_filename) {
614 hwaddr start;
615 hwaddr end = riscv_load_initrd(machine->initrd_filename,
616 machine->ram_size, kernel_entry,
617 &start);
9f79638e 618 qemu_fdt_setprop_cell(s->fdt, "/chosen",
0f8d4462 619 "linux,initrd-start", start);
9f79638e 620 qemu_fdt_setprop_cell(s->fdt, "/chosen", "linux,initrd-end",
0f8d4462
GR
621 end);
622 }
dc144fe1
AP
623 } else {
624 /*
625 * If dynamic firmware is used, it doesn't know where is the next mode
626 * if kernel argument is not set.
627 */
628 kernel_entry = 0;
a7240d1e
MC
629 }
630
66b1205b 631 /* Compute the fdt load address in dram */
13b8c354 632 fdt_load_addr = riscv_load_fdt(memmap[SIFIVE_U_DEV_DRAM].base,
66b1205b 633 machine->ram_size, s->fdt);
a8259b53 634 if (!riscv_is_32bit(&s->soc.u_cpus)) {
2206ffa6
AF
635 start_addr_hi32 = (uint64_t)start_addr >> 32;
636 }
66b1205b 637
a7240d1e 638 /* reset vector */
623d53cb 639 uint32_t reset_vec[12] = {
17aad9f2 640 s->msel, /* MSEL pin state */
dc144fe1 641 0x00000297, /* 1: auipc t0, %pcrel_hi(fw_dyn) */
623d53cb 642 0x02c28613, /* addi a2, t0, %pcrel_lo(1b) */
a7240d1e 643 0xf1402573, /* csrr a0, mhartid */
2206ffa6
AF
644 0,
645 0,
a7240d1e 646 0x00028067, /* jr t0 */
fc41ae23 647 start_addr, /* start: .dword */
8590f536 648 start_addr_hi32,
66b1205b 649 fdt_load_addr, /* fdt_laddr: .dword */
623d53cb 650 0x00000000,
66b1205b 651 0x00000000,
dc144fe1 652 /* fw_dyn: */
a7240d1e 653 };
a8259b53 654 if (riscv_is_32bit(&s->soc.u_cpus)) {
2206ffa6
AF
655 reset_vec[4] = 0x0202a583; /* lw a1, 32(t0) */
656 reset_vec[5] = 0x0182a283; /* lw t0, 24(t0) */
657 } else {
658 reset_vec[4] = 0x0202b583; /* ld a1, 32(t0) */
659 reset_vec[5] = 0x0182b283; /* ld t0, 24(t0) */
660 }
661
a7240d1e 662
5aec3247 663 /* copy in the reset vector in little_endian byte order */
66b1205b 664 for (i = 0; i < ARRAY_SIZE(reset_vec); i++) {
5aec3247
MC
665 reset_vec[i] = cpu_to_le32(reset_vec[i]);
666 }
667 rom_add_blob_fixed_as("mrom.reset", reset_vec, sizeof(reset_vec),
13b8c354 668 memmap[SIFIVE_U_DEV_MROM].base, &address_space_memory);
dc144fe1 669
78936771 670 riscv_rom_copy_firmware_info(machine, memmap[SIFIVE_U_DEV_MROM].base,
13b8c354 671 memmap[SIFIVE_U_DEV_MROM].size,
dc144fe1 672 sizeof(reset_vec), kernel_entry);
145b2991
BM
673
674 /* Connect an SPI flash to SPI0 */
675 flash_dev = qdev_new("is25wp256");
676 dinfo = drive_get_next(IF_MTD);
677 if (dinfo) {
678 qdev_prop_set_drive_err(flash_dev, "drive",
679 blk_by_legacy_dinfo(dinfo),
680 &error_fatal);
681 }
682 qdev_realize_and_unref(flash_dev, BUS(s->soc.spi0.spi), &error_fatal);
683
684 flash_cs = qdev_get_gpio_in_named(flash_dev, SSI_GPIO_CS, 0);
685 sysbus_connect_irq(SYS_BUS_DEVICE(&s->soc.spi0), 1, flash_cs);
722f1352
BM
686
687 /* Connect an SD card to SPI2 */
688 sd_dev = ssi_create_peripheral(s->soc.spi2.spi, "ssi-sd");
689
690 sd_cs = qdev_get_gpio_in_named(sd_dev, SSI_GPIO_CS, 0);
691 sysbus_connect_irq(SYS_BUS_DEVICE(&s->soc.spi2), 1, sd_cs);
2308092b
AF
692}
693
523e3464
AF
694static bool sifive_u_machine_get_start_in_flash(Object *obj, Error **errp)
695{
696 SiFiveUState *s = RISCV_U_MACHINE(obj);
697
698 return s->start_in_flash;
699}
700
701static void sifive_u_machine_set_start_in_flash(Object *obj, bool value, Error **errp)
702{
703 SiFiveUState *s = RISCV_U_MACHINE(obj);
704
705 s->start_in_flash = value;
706}
707
3e9667cd
BM
708static void sifive_u_machine_get_uint32_prop(Object *obj, Visitor *v,
709 const char *name, void *opaque,
710 Error **errp)
3ca109c3
BM
711{
712 visit_type_uint32(v, name, (uint32_t *)opaque, errp);
713}
714
3e9667cd
BM
715static void sifive_u_machine_set_uint32_prop(Object *obj, Visitor *v,
716 const char *name, void *opaque,
717 Error **errp)
3ca109c3
BM
718{
719 visit_type_uint32(v, name, (uint32_t *)opaque, errp);
720}
721
523e3464
AF
722static void sifive_u_machine_instance_init(Object *obj)
723{
724 SiFiveUState *s = RISCV_U_MACHINE(obj);
725
726 s->start_in_flash = false;
cfa32630
BM
727 s->msel = 0;
728 object_property_add(obj, "msel", "uint32",
729 sifive_u_machine_get_uint32_prop,
730 sifive_u_machine_set_uint32_prop, NULL, &s->msel);
731 object_property_set_description(obj, "msel",
732 "Mode Select (MSEL[3:0]) pin state");
733
3ca109c3 734 s->serial = OTP_SERIAL;
d2623129 735 object_property_add(obj, "serial", "uint32",
3e9667cd
BM
736 sifive_u_machine_get_uint32_prop,
737 sifive_u_machine_set_uint32_prop, NULL, &s->serial);
7eecec7d 738 object_property_set_description(obj, "serial", "Board serial number");
523e3464
AF
739}
740
741static void sifive_u_machine_class_init(ObjectClass *oc, void *data)
742{
743 MachineClass *mc = MACHINE_CLASS(oc);
744
745 mc->desc = "RISC-V Board compatible with SiFive U SDK";
746 mc->init = sifive_u_machine_init;
747 mc->max_cpus = SIFIVE_U_MANAGEMENT_CPU_COUNT + SIFIVE_U_COMPUTE_CPU_COUNT;
748 mc->min_cpus = SIFIVE_U_MANAGEMENT_CPU_COUNT + 1;
1eaada8a 749 mc->default_cpu_type = SIFIVE_U_CPU;
523e3464 750 mc->default_cpus = mc->min_cpus;
418b473e
EH
751
752 object_class_property_add_bool(oc, "start-in-flash",
753 sifive_u_machine_get_start_in_flash,
754 sifive_u_machine_set_start_in_flash);
755 object_class_property_set_description(oc, "start-in-flash",
756 "Set on to tell QEMU's ROM to jump to "
757 "flash. Otherwise QEMU will jump to DRAM "
758 "or L2LIM depending on the msel value");
523e3464
AF
759}
760
761static const TypeInfo sifive_u_machine_typeinfo = {
762 .name = MACHINE_TYPE_NAME("sifive_u"),
763 .parent = TYPE_MACHINE,
764 .class_init = sifive_u_machine_class_init,
765 .instance_init = sifive_u_machine_instance_init,
766 .instance_size = sizeof(SiFiveUState),
767};
768
769static void sifive_u_machine_init_register_types(void)
770{
771 type_register_static(&sifive_u_machine_typeinfo);
772}
773
774type_init(sifive_u_machine_init_register_types)
775
139177b1 776static void sifive_u_soc_instance_init(Object *obj)
2308092b
AF
777{
778 SiFiveUSoCState *s = RISCV_U_SOC(obj);
779
9fc7fc4d 780 object_initialize_child(obj, "e-cluster", &s->e_cluster, TYPE_CPU_CLUSTER);
ecdfe393
BM
781 qdev_prop_set_uint32(DEVICE(&s->e_cluster), "cluster-id", 0);
782
db873cc5
MA
783 object_initialize_child(OBJECT(&s->e_cluster), "e-cpus", &s->e_cpus,
784 TYPE_RISCV_HART_ARRAY);
ecdfe393
BM
785 qdev_prop_set_uint32(DEVICE(&s->e_cpus), "num-harts", 1);
786 qdev_prop_set_uint32(DEVICE(&s->e_cpus), "hartid-base", 0);
787 qdev_prop_set_string(DEVICE(&s->e_cpus), "cpu-type", SIFIVE_E_CPU);
73f6ed97 788 qdev_prop_set_uint64(DEVICE(&s->e_cpus), "resetvec", 0x1004);
ecdfe393 789
9fc7fc4d 790 object_initialize_child(obj, "u-cluster", &s->u_cluster, TYPE_CPU_CLUSTER);
ecdfe393
BM
791 qdev_prop_set_uint32(DEVICE(&s->u_cluster), "cluster-id", 1);
792
db873cc5
MA
793 object_initialize_child(OBJECT(&s->u_cluster), "u-cpus", &s->u_cpus,
794 TYPE_RISCV_HART_ARRAY);
5a7f76a3 795
db873cc5
MA
796 object_initialize_child(obj, "prci", &s->prci, TYPE_SIFIVE_U_PRCI);
797 object_initialize_child(obj, "otp", &s->otp, TYPE_SIFIVE_U_OTP);
798 object_initialize_child(obj, "gem", &s->gem, TYPE_CADENCE_GEM);
8a88b9f5 799 object_initialize_child(obj, "gpio", &s->gpio, TYPE_SIFIVE_GPIO);
834e027a 800 object_initialize_child(obj, "pdma", &s->dma, TYPE_SIFIVE_PDMA);
145b2991 801 object_initialize_child(obj, "spi0", &s->spi0, TYPE_SIFIVE_SPI);
722f1352 802 object_initialize_child(obj, "spi2", &s->spi2, TYPE_SIFIVE_SPI);
ea6eaa06
AF
803 object_initialize_child(obj, "pwm0", &s->pwm[0], TYPE_SIFIVE_PWM);
804 object_initialize_child(obj, "pwm1", &s->pwm[1], TYPE_SIFIVE_PWM);
2308092b
AF
805}
806
139177b1 807static void sifive_u_soc_realize(DeviceState *dev, Error **errp)
2308092b 808{
c4473127 809 MachineState *ms = MACHINE(qdev_get_machine());
2308092b 810 SiFiveUSoCState *s = RISCV_U_SOC(dev);
73261285 811 const MemMapEntry *memmap = sifive_u_memmap;
2308092b
AF
812 MemoryRegion *system_memory = get_system_memory();
813 MemoryRegion *mask_rom = g_new(MemoryRegion, 1);
a6902ef0 814 MemoryRegion *l2lim_mem = g_new(MemoryRegion, 1);
05446f41
BM
815 char *plic_hart_config;
816 size_t plic_hart_config_len;
ea6eaa06 817 int i, j;
5a7f76a3 818 NICInfo *nd = &nd_table[0];
2308092b 819
099be035
AF
820 qdev_prop_set_uint32(DEVICE(&s->u_cpus), "num-harts", ms->smp.cpus - 1);
821 qdev_prop_set_uint32(DEVICE(&s->u_cpus), "hartid-base", 1);
822 qdev_prop_set_string(DEVICE(&s->u_cpus), "cpu-type", s->cpu_type);
823 qdev_prop_set_uint64(DEVICE(&s->u_cpus), "resetvec", 0x1004);
824
db873cc5
MA
825 sysbus_realize(SYS_BUS_DEVICE(&s->e_cpus), &error_abort);
826 sysbus_realize(SYS_BUS_DEVICE(&s->u_cpus), &error_abort);
ecdfe393
BM
827 /*
828 * The cluster must be realized after the RISC-V hart array container,
829 * as the container's CPU object is only created on realize, and the
830 * CPU must exist and have been parented into the cluster before the
831 * cluster is realized.
832 */
ce189ab2
MA
833 qdev_realize(DEVICE(&s->e_cluster), NULL, &error_abort);
834 qdev_realize(DEVICE(&s->u_cluster), NULL, &error_abort);
2308092b
AF
835
836 /* boot rom */
414c47d2 837 memory_region_init_rom(mask_rom, OBJECT(dev), "riscv.sifive.u.mrom",
13b8c354
EH
838 memmap[SIFIVE_U_DEV_MROM].size, &error_fatal);
839 memory_region_add_subregion(system_memory, memmap[SIFIVE_U_DEV_MROM].base,
2308092b 840 mask_rom);
a7240d1e 841
a6902ef0
AF
842 /*
843 * Add L2-LIM at reset size.
844 * This should be reduced in size as the L2 Cache Controller WayEnable
845 * register is incremented. Unfortunately I don't see a nice (or any) way
846 * to handle reducing or blocking out the L2 LIM while still allowing it
847 * be re returned to all enabled after a reset. For the time being, just
848 * leave it enabled all the time. This won't break anything, but will be
849 * too generous to misbehaving guests.
850 */
851 memory_region_init_ram(l2lim_mem, NULL, "riscv.sifive.u.l2lim",
13b8c354
EH
852 memmap[SIFIVE_U_DEV_L2LIM].size, &error_fatal);
853 memory_region_add_subregion(system_memory, memmap[SIFIVE_U_DEV_L2LIM].base,
a6902ef0
AF
854 l2lim_mem);
855
05446f41 856 /* create PLIC hart topology configuration string */
c4473127
LX
857 plic_hart_config_len = (strlen(SIFIVE_U_PLIC_HART_CONFIG) + 1) *
858 ms->smp.cpus;
05446f41 859 plic_hart_config = g_malloc0(plic_hart_config_len);
c4473127 860 for (i = 0; i < ms->smp.cpus; i++) {
05446f41 861 if (i != 0) {
ef965ce2
BM
862 strncat(plic_hart_config, "," SIFIVE_U_PLIC_HART_CONFIG,
863 plic_hart_config_len);
864 } else {
865 strncat(plic_hart_config, "M", plic_hart_config_len);
05446f41 866 }
05446f41
BM
867 plic_hart_config_len -= (strlen(SIFIVE_U_PLIC_HART_CONFIG) + 1);
868 }
869
a7240d1e 870 /* MMIO */
13b8c354 871 s->plic = sifive_plic_create(memmap[SIFIVE_U_DEV_PLIC].base,
f436ecc3 872 plic_hart_config, ms->smp.cpus, 0,
a7240d1e
MC
873 SIFIVE_U_PLIC_NUM_SOURCES,
874 SIFIVE_U_PLIC_NUM_PRIORITIES,
875 SIFIVE_U_PLIC_PRIORITY_BASE,
876 SIFIVE_U_PLIC_PENDING_BASE,
877 SIFIVE_U_PLIC_ENABLE_BASE,
878 SIFIVE_U_PLIC_ENABLE_STRIDE,
879 SIFIVE_U_PLIC_CONTEXT_BASE,
880 SIFIVE_U_PLIC_CONTEXT_STRIDE,
13b8c354 881 memmap[SIFIVE_U_DEV_PLIC].size);
bb8136df 882 g_free(plic_hart_config);
13b8c354 883 sifive_uart_create(system_memory, memmap[SIFIVE_U_DEV_UART0].base,
647a70a1 884 serial_hd(0), qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_U_UART0_IRQ));
13b8c354 885 sifive_uart_create(system_memory, memmap[SIFIVE_U_DEV_UART1].base,
194eef09 886 serial_hd(1), qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_U_UART1_IRQ));
b8fb878a
AP
887 riscv_aclint_swi_create(memmap[SIFIVE_U_DEV_CLINT].base, 0,
888 ms->smp.cpus, false);
889 riscv_aclint_mtimer_create(memmap[SIFIVE_U_DEV_CLINT].base +
890 RISCV_ACLINT_SWI_SIZE,
891 RISCV_ACLINT_DEFAULT_MTIMER_SIZE, 0, ms->smp.cpus,
892 RISCV_ACLINT_DEFAULT_MTIMECMP, RISCV_ACLINT_DEFAULT_MTIME,
074ca702 893 CLINT_TIMEBASE_FREQ, false);
5a7f76a3 894
cbe3a8c5
MA
895 if (!sysbus_realize(SYS_BUS_DEVICE(&s->prci), errp)) {
896 return;
897 }
13b8c354 898 sysbus_mmio_map(SYS_BUS_DEVICE(&s->prci), 0, memmap[SIFIVE_U_DEV_PRCI].base);
af14c840 899
8a88b9f5 900 qdev_prop_set_uint32(DEVICE(&s->gpio), "ngpio", 16);
cbe3a8c5
MA
901 if (!sysbus_realize(SYS_BUS_DEVICE(&s->gpio), errp)) {
902 return;
903 }
13b8c354 904 sysbus_mmio_map(SYS_BUS_DEVICE(&s->gpio), 0, memmap[SIFIVE_U_DEV_GPIO].base);
8a88b9f5
BM
905
906 /* Pass all GPIOs to the SOC layer so they are available to the board */
907 qdev_pass_gpios(DEVICE(&s->gpio), dev, NULL);
908
909 /* Connect GPIO interrupts to the PLIC */
910 for (i = 0; i < 16; i++) {
911 sysbus_connect_irq(SYS_BUS_DEVICE(&s->gpio), i,
912 qdev_get_gpio_in(DEVICE(s->plic),
913 SIFIVE_U_GPIO_IRQ0 + i));
834e027a
BM
914 }
915
916 /* PDMA */
917 sysbus_realize(SYS_BUS_DEVICE(&s->dma), errp);
13b8c354 918 sysbus_mmio_map(SYS_BUS_DEVICE(&s->dma), 0, memmap[SIFIVE_U_DEV_PDMA].base);
834e027a
BM
919
920 /* Connect PDMA interrupts to the PLIC */
921 for (i = 0; i < SIFIVE_PDMA_IRQS; i++) {
922 sysbus_connect_irq(SYS_BUS_DEVICE(&s->dma), i,
923 qdev_get_gpio_in(DEVICE(s->plic),
924 SIFIVE_U_PDMA_IRQ0 + i));
8a88b9f5
BM
925 }
926
fda5b000 927 qdev_prop_set_uint32(DEVICE(&s->otp), "serial", s->serial);
cbe3a8c5
MA
928 if (!sysbus_realize(SYS_BUS_DEVICE(&s->otp), errp)) {
929 return;
930 }
13b8c354 931 sysbus_mmio_map(SYS_BUS_DEVICE(&s->otp), 0, memmap[SIFIVE_U_DEV_OTP].base);
5461c4fe 932
7ad36e2e 933 /* FIXME use qdev NIC properties instead of nd_table[] */
5a7f76a3
AF
934 if (nd->used) {
935 qemu_check_nic_model(nd, TYPE_CADENCE_GEM);
936 qdev_set_nic_properties(DEVICE(&s->gem), nd);
937 }
5325cc34 938 object_property_set_int(OBJECT(&s->gem), "revision", GEM_REVISION,
5a7f76a3 939 &error_abort);
668f62ec 940 if (!sysbus_realize(SYS_BUS_DEVICE(&s->gem), errp)) {
5a7f76a3
AF
941 return;
942 }
13b8c354 943 sysbus_mmio_map(SYS_BUS_DEVICE(&s->gem), 0, memmap[SIFIVE_U_DEV_GEM].base);
5a7f76a3 944 sysbus_connect_irq(SYS_BUS_DEVICE(&s->gem), 0,
5874f0a7 945 qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_U_GEM_IRQ));
7b6bb66f 946
ea6eaa06
AF
947 /* PWM */
948 for (i = 0; i < 2; i++) {
949 if (!sysbus_realize(SYS_BUS_DEVICE(&s->pwm[i]), errp)) {
950 return;
951 }
952 sysbus_mmio_map(SYS_BUS_DEVICE(&s->pwm[i]), 0,
953 memmap[SIFIVE_U_DEV_PWM0].base + (0x1000 * i));
954
955 /* Connect PWM interrupts to the PLIC */
956 for (j = 0; j < SIFIVE_PWM_IRQS; j++) {
957 sysbus_connect_irq(SYS_BUS_DEVICE(&s->pwm[i]), j,
958 qdev_get_gpio_in(DEVICE(s->plic),
959 SIFIVE_U_PWM0_IRQ0 + (i * 4) + j));
960 }
961 }
962
7b6bb66f 963 create_unimplemented_device("riscv.sifive.u.gem-mgmt",
13b8c354 964 memmap[SIFIVE_U_DEV_GEM_MGMT].base, memmap[SIFIVE_U_DEV_GEM_MGMT].size);
3eaea6eb
BM
965
966 create_unimplemented_device("riscv.sifive.u.dmc",
13b8c354 967 memmap[SIFIVE_U_DEV_DMC].base, memmap[SIFIVE_U_DEV_DMC].size);
6eaf9cf5
BM
968
969 create_unimplemented_device("riscv.sifive.u.l2cc",
13b8c354 970 memmap[SIFIVE_U_DEV_L2CC].base, memmap[SIFIVE_U_DEV_L2CC].size);
145b2991
BM
971
972 sysbus_realize(SYS_BUS_DEVICE(&s->spi0), errp);
973 sysbus_mmio_map(SYS_BUS_DEVICE(&s->spi0), 0,
974 memmap[SIFIVE_U_DEV_QSPI0].base);
975 sysbus_connect_irq(SYS_BUS_DEVICE(&s->spi0), 0,
976 qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_U_QSPI0_IRQ));
722f1352
BM
977 sysbus_realize(SYS_BUS_DEVICE(&s->spi2), errp);
978 sysbus_mmio_map(SYS_BUS_DEVICE(&s->spi2), 0,
979 memmap[SIFIVE_U_DEV_QSPI2].base);
980 sysbus_connect_irq(SYS_BUS_DEVICE(&s->spi2), 0,
981 qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_U_QSPI2_IRQ));
a7240d1e
MC
982}
983
139177b1 984static Property sifive_u_soc_props[] = {
fda5b000 985 DEFINE_PROP_UINT32("serial", SiFiveUSoCState, serial, OTP_SERIAL),
099be035 986 DEFINE_PROP_STRING("cpu-type", SiFiveUSoCState, cpu_type),
fda5b000
AF
987 DEFINE_PROP_END_OF_LIST()
988};
989
139177b1 990static void sifive_u_soc_class_init(ObjectClass *oc, void *data)
2308092b
AF
991{
992 DeviceClass *dc = DEVICE_CLASS(oc);
993
139177b1
BM
994 device_class_set_props(dc, sifive_u_soc_props);
995 dc->realize = sifive_u_soc_realize;
2308092b
AF
996 /* Reason: Uses serial_hds in realize function, thus can't be used twice */
997 dc->user_creatable = false;
998}
999
139177b1 1000static const TypeInfo sifive_u_soc_type_info = {
2308092b
AF
1001 .name = TYPE_RISCV_U_SOC,
1002 .parent = TYPE_DEVICE,
1003 .instance_size = sizeof(SiFiveUSoCState),
139177b1
BM
1004 .instance_init = sifive_u_soc_instance_init,
1005 .class_init = sifive_u_soc_class_init,
2308092b
AF
1006};
1007
139177b1 1008static void sifive_u_soc_register_types(void)
2308092b 1009{
139177b1 1010 type_register_static(&sifive_u_soc_type_info);
2308092b
AF
1011}
1012
139177b1 1013type_init(sifive_u_soc_register_types)