xref: /openbmc/qemu/hw/riscv/sifive_u.c (revision 074ca702e64dcea15c9c3b2c1931351cf397debe)
1a7240d1eSMichael Clark /*
2a7240d1eSMichael Clark  * QEMU RISC-V Board Compatible with SiFive Freedom U SDK
3a7240d1eSMichael Clark  *
4a7240d1eSMichael Clark  * Copyright (c) 2016-2017 Sagar Karandikar, sagark@eecs.berkeley.edu
5a7240d1eSMichael Clark  * Copyright (c) 2017 SiFive, Inc.
67b6bb66fSBin Meng  * Copyright (c) 2019 Bin Meng <bmeng.cn@gmail.com>
7a7240d1eSMichael Clark  *
8a7240d1eSMichael Clark  * Provides a board compatible with the SiFive Freedom U SDK:
9a7240d1eSMichael Clark  *
10a7240d1eSMichael Clark  * 0) UART
11a7240d1eSMichael Clark  * 1) CLINT (Core Level Interruptor)
12a7240d1eSMichael Clark  * 2) PLIC (Platform Level Interrupt Controller)
13af14c840SBin Meng  * 3) PRCI (Power, Reset, Clock, Interrupt)
148a88b9f5SBin Meng  * 4) GPIO (General Purpose Input/Output Controller)
158a88b9f5SBin Meng  * 5) OTP (One-Time Programmable) memory with stored serial number
168a88b9f5SBin Meng  * 6) GEM (Gigabit Ethernet Controller) and management block
17834e027aSBin Meng  * 7) DMA (Direct Memory Access Controller)
18145b2991SBin Meng  * 8) SPI0 connected to an SPI flash
19722f1352SBin Meng  * 9) SPI2 connected to an SD card
20a7240d1eSMichael Clark  *
21f3d47d58SBin Meng  * This board currently generates devicetree dynamically that indicates at least
22ecdfe393SBin Meng  * two harts and up to five harts.
23a7240d1eSMichael Clark  *
24a7240d1eSMichael Clark  * This program is free software; you can redistribute it and/or modify it
25a7240d1eSMichael Clark  * under the terms and conditions of the GNU General Public License,
26a7240d1eSMichael Clark  * version 2 or later, as published by the Free Software Foundation.
27a7240d1eSMichael Clark  *
28a7240d1eSMichael Clark  * This program is distributed in the hope it will be useful, but WITHOUT
29a7240d1eSMichael Clark  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
30a7240d1eSMichael Clark  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
31a7240d1eSMichael Clark  * more details.
32a7240d1eSMichael Clark  *
33a7240d1eSMichael Clark  * You should have received a copy of the GNU General Public License along with
34a7240d1eSMichael Clark  * this program.  If not, see <http://www.gnu.org/licenses/>.
35a7240d1eSMichael Clark  */
36a7240d1eSMichael Clark 
37a7240d1eSMichael Clark #include "qemu/osdep.h"
38a7240d1eSMichael Clark #include "qemu/error-report.h"
39a7240d1eSMichael Clark #include "qapi/error.h"
403ca109c3SBin Meng #include "qapi/visitor.h"
41a7240d1eSMichael Clark #include "hw/boards.h"
425133ed17SBin Meng #include "hw/irq.h"
43a7240d1eSMichael Clark #include "hw/loader.h"
44a7240d1eSMichael Clark #include "hw/sysbus.h"
45a7240d1eSMichael Clark #include "hw/char/serial.h"
46ecdfe393SBin Meng #include "hw/cpu/cluster.h"
477b6bb66fSBin Meng #include "hw/misc/unimp.h"
48145b2991SBin Meng #include "hw/ssi/ssi.h"
49a7240d1eSMichael Clark #include "target/riscv/cpu.h"
50a7240d1eSMichael Clark #include "hw/riscv/riscv_hart.h"
51a7240d1eSMichael Clark #include "hw/riscv/sifive_u.h"
520ac24d56SAlistair Francis #include "hw/riscv/boot.h"
53b609b7e3SBin Meng #include "hw/char/sifive_uart.h"
54406fafd5SBin Meng #include "hw/intc/sifive_clint.h"
5584fcf3c1SBin Meng #include "hw/intc/sifive_plic.h"
56a7240d1eSMichael Clark #include "chardev/char.h"
577b6bb66fSBin Meng #include "net/eth.h"
58a7240d1eSMichael Clark #include "sysemu/arch_init.h"
59a7240d1eSMichael Clark #include "sysemu/device_tree.h"
605133ed17SBin Meng #include "sysemu/runstate.h"
6146517dd4SMarkus Armbruster #include "sysemu/sysemu.h"
62a7240d1eSMichael Clark 
635aec3247SMichael Clark #include <libfdt.h>
645aec3247SMichael Clark 
65*074ca702SBin Meng /* CLINT timebase frequency */
66*074ca702SBin Meng #define CLINT_TIMEBASE_FREQ 1000000
67*074ca702SBin Meng 
6873261285SBin Meng static const MemMapEntry sifive_u_memmap[] = {
6913b8c354SEduardo Habkost     [SIFIVE_U_DEV_DEBUG] =    {        0x0,      0x100 },
7013b8c354SEduardo Habkost     [SIFIVE_U_DEV_MROM] =     {     0x1000,     0xf000 },
7113b8c354SEduardo Habkost     [SIFIVE_U_DEV_CLINT] =    {  0x2000000,    0x10000 },
7213b8c354SEduardo Habkost     [SIFIVE_U_DEV_L2CC] =     {  0x2010000,     0x1000 },
7313b8c354SEduardo Habkost     [SIFIVE_U_DEV_PDMA] =     {  0x3000000,   0x100000 },
7413b8c354SEduardo Habkost     [SIFIVE_U_DEV_L2LIM] =    {  0x8000000,  0x2000000 },
7513b8c354SEduardo Habkost     [SIFIVE_U_DEV_PLIC] =     {  0xc000000,  0x4000000 },
7613b8c354SEduardo Habkost     [SIFIVE_U_DEV_PRCI] =     { 0x10000000,     0x1000 },
7713b8c354SEduardo Habkost     [SIFIVE_U_DEV_UART0] =    { 0x10010000,     0x1000 },
7813b8c354SEduardo Habkost     [SIFIVE_U_DEV_UART1] =    { 0x10011000,     0x1000 },
79145b2991SBin Meng     [SIFIVE_U_DEV_QSPI0] =    { 0x10040000,     0x1000 },
80722f1352SBin Meng     [SIFIVE_U_DEV_QSPI2] =    { 0x10050000,     0x1000 },
8113b8c354SEduardo Habkost     [SIFIVE_U_DEV_GPIO] =     { 0x10060000,     0x1000 },
8213b8c354SEduardo Habkost     [SIFIVE_U_DEV_OTP] =      { 0x10070000,     0x1000 },
8313b8c354SEduardo Habkost     [SIFIVE_U_DEV_GEM] =      { 0x10090000,     0x2000 },
8413b8c354SEduardo Habkost     [SIFIVE_U_DEV_GEM_MGMT] = { 0x100a0000,     0x1000 },
8513b8c354SEduardo Habkost     [SIFIVE_U_DEV_DMC] =      { 0x100b0000,    0x10000 },
8613b8c354SEduardo Habkost     [SIFIVE_U_DEV_FLASH0] =   { 0x20000000, 0x10000000 },
8713b8c354SEduardo Habkost     [SIFIVE_U_DEV_DRAM] =     { 0x80000000,        0x0 },
88a7240d1eSMichael Clark };
89a7240d1eSMichael Clark 
905461c4feSBin Meng #define OTP_SERIAL          1
915a7f76a3SAlistair Francis #define GEM_REVISION        0x10070109
925a7f76a3SAlistair Francis 
9373261285SBin Meng static void create_fdt(SiFiveUState *s, const MemMapEntry *memmap,
942206ffa6SAlistair Francis                        uint64_t mem_size, const char *cmdline, bool is_32_bit)
95a7240d1eSMichael Clark {
96ecdfe393SBin Meng     MachineState *ms = MACHINE(qdev_get_machine());
97a7240d1eSMichael Clark     void *fdt;
98a7240d1eSMichael Clark     int cpu;
99a7240d1eSMichael Clark     uint32_t *cells;
100a7240d1eSMichael Clark     char *nodename;
1015133ed17SBin Meng     uint32_t plic_phandle, prci_phandle, gpio_phandle, phandle = 1;
1027b6bb66fSBin Meng     uint32_t hfclk_phandle, rtcclk_phandle, phy_phandle;
103cb53b283SBin Meng     static const char * const ethclk_names[2] = { "pclk", "hclk" };
1047cfbb17fSBin Meng     static const char * const clint_compat[2] = {
1057cfbb17fSBin Meng         "sifive,clint0", "riscv,clint0"
1067cfbb17fSBin Meng     };
10760bb5407SBin Meng     static const char * const plic_compat[2] = {
10860bb5407SBin Meng         "sifive,plic-1.0.0", "riscv,plic0"
10960bb5407SBin Meng     };
110a7240d1eSMichael Clark 
111f2ce39b4SPaolo Bonzini     if (ms->dtb) {
112f2ce39b4SPaolo Bonzini         fdt = s->fdt = load_device_tree(ms->dtb, &s->fdt_size);
113d5c90cf3SAnup Patel         if (!fdt) {
114d5c90cf3SAnup Patel             error_report("load_device_tree() failed");
115d5c90cf3SAnup Patel             exit(1);
116d5c90cf3SAnup Patel         }
117d5c90cf3SAnup Patel         goto update_bootargs;
118d5c90cf3SAnup Patel     } else {
119a7240d1eSMichael Clark         fdt = s->fdt = create_device_tree(&s->fdt_size);
120a7240d1eSMichael Clark         if (!fdt) {
121a7240d1eSMichael Clark             error_report("create_device_tree() failed");
122a7240d1eSMichael Clark             exit(1);
123a7240d1eSMichael Clark         }
124d5c90cf3SAnup Patel     }
125a7240d1eSMichael Clark 
126d372e748SBin Meng     qemu_fdt_setprop_string(fdt, "/", "model", "SiFive HiFive Unleashed A00");
127d372e748SBin Meng     qemu_fdt_setprop_string(fdt, "/", "compatible",
128d372e748SBin Meng                             "sifive,hifive-unleashed-a00");
129a7240d1eSMichael Clark     qemu_fdt_setprop_cell(fdt, "/", "#size-cells", 0x2);
130a7240d1eSMichael Clark     qemu_fdt_setprop_cell(fdt, "/", "#address-cells", 0x2);
131a7240d1eSMichael Clark 
132a7240d1eSMichael Clark     qemu_fdt_add_subnode(fdt, "/soc");
133a7240d1eSMichael Clark     qemu_fdt_setprop(fdt, "/soc", "ranges", NULL, 0);
1342a1a6f6dSAlistair Francis     qemu_fdt_setprop_string(fdt, "/soc", "compatible", "simple-bus");
135a7240d1eSMichael Clark     qemu_fdt_setprop_cell(fdt, "/soc", "#size-cells", 0x2);
136a7240d1eSMichael Clark     qemu_fdt_setprop_cell(fdt, "/soc", "#address-cells", 0x2);
137a7240d1eSMichael Clark 
138e1724d09SBin Meng     hfclk_phandle = phandle++;
139e1724d09SBin Meng     nodename = g_strdup_printf("/hfclk");
140e1724d09SBin Meng     qemu_fdt_add_subnode(fdt, nodename);
141e1724d09SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "phandle", hfclk_phandle);
142e1724d09SBin Meng     qemu_fdt_setprop_string(fdt, nodename, "clock-output-names", "hfclk");
143e1724d09SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "clock-frequency",
144e1724d09SBin Meng         SIFIVE_U_HFCLK_FREQ);
145e1724d09SBin Meng     qemu_fdt_setprop_string(fdt, nodename, "compatible", "fixed-clock");
146e1724d09SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "#clock-cells", 0x0);
147e1724d09SBin Meng     g_free(nodename);
148e1724d09SBin Meng 
149e1724d09SBin Meng     rtcclk_phandle = phandle++;
150e1724d09SBin Meng     nodename = g_strdup_printf("/rtcclk");
151e1724d09SBin Meng     qemu_fdt_add_subnode(fdt, nodename);
152e1724d09SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "phandle", rtcclk_phandle);
153e1724d09SBin Meng     qemu_fdt_setprop_string(fdt, nodename, "clock-output-names", "rtcclk");
154e1724d09SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "clock-frequency",
155e1724d09SBin Meng         SIFIVE_U_RTCCLK_FREQ);
156e1724d09SBin Meng     qemu_fdt_setprop_string(fdt, nodename, "compatible", "fixed-clock");
157e1724d09SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "#clock-cells", 0x0);
158e1724d09SBin Meng     g_free(nodename);
159e1724d09SBin Meng 
160a7240d1eSMichael Clark     nodename = g_strdup_printf("/memory@%lx",
16113b8c354SEduardo Habkost         (long)memmap[SIFIVE_U_DEV_DRAM].base);
162a7240d1eSMichael Clark     qemu_fdt_add_subnode(fdt, nodename);
163a7240d1eSMichael Clark     qemu_fdt_setprop_cells(fdt, nodename, "reg",
16413b8c354SEduardo Habkost         memmap[SIFIVE_U_DEV_DRAM].base >> 32, memmap[SIFIVE_U_DEV_DRAM].base,
165a7240d1eSMichael Clark         mem_size >> 32, mem_size);
166a7240d1eSMichael Clark     qemu_fdt_setprop_string(fdt, nodename, "device_type", "memory");
167a7240d1eSMichael Clark     g_free(nodename);
168a7240d1eSMichael Clark 
169a7240d1eSMichael Clark     qemu_fdt_add_subnode(fdt, "/cpus");
1702a8756edSMichael Clark     qemu_fdt_setprop_cell(fdt, "/cpus", "timebase-frequency",
171*074ca702SBin Meng         CLINT_TIMEBASE_FREQ);
172a7240d1eSMichael Clark     qemu_fdt_setprop_cell(fdt, "/cpus", "#size-cells", 0x0);
173a7240d1eSMichael Clark     qemu_fdt_setprop_cell(fdt, "/cpus", "#address-cells", 0x1);
174a7240d1eSMichael Clark 
175ecdfe393SBin Meng     for (cpu = ms->smp.cpus - 1; cpu >= 0; cpu--) {
176382cb439SBin Meng         int cpu_phandle = phandle++;
177a7240d1eSMichael Clark         nodename = g_strdup_printf("/cpus/cpu@%d", cpu);
178a7240d1eSMichael Clark         char *intc = g_strdup_printf("/cpus/cpu@%d/interrupt-controller", cpu);
179ecdfe393SBin Meng         char *isa;
180a7240d1eSMichael Clark         qemu_fdt_add_subnode(fdt, nodename);
181ecdfe393SBin Meng         /* cpu 0 is the management hart that does not have mmu */
182ecdfe393SBin Meng         if (cpu != 0) {
1832206ffa6SAlistair Francis             if (is_32_bit) {
184e883e992SBin Meng                 qemu_fdt_setprop_string(fdt, nodename, "mmu-type", "riscv,sv32");
1852206ffa6SAlistair Francis             } else {
186a7240d1eSMichael Clark                 qemu_fdt_setprop_string(fdt, nodename, "mmu-type", "riscv,sv48");
1872206ffa6SAlistair Francis             }
188ecdfe393SBin Meng             isa = riscv_isa_string(&s->soc.u_cpus.harts[cpu - 1]);
189ecdfe393SBin Meng         } else {
190ecdfe393SBin Meng             isa = riscv_isa_string(&s->soc.e_cpus.harts[0]);
191ecdfe393SBin Meng         }
192a7240d1eSMichael Clark         qemu_fdt_setprop_string(fdt, nodename, "riscv,isa", isa);
193a7240d1eSMichael Clark         qemu_fdt_setprop_string(fdt, nodename, "compatible", "riscv");
194a7240d1eSMichael Clark         qemu_fdt_setprop_string(fdt, nodename, "status", "okay");
195a7240d1eSMichael Clark         qemu_fdt_setprop_cell(fdt, nodename, "reg", cpu);
196a7240d1eSMichael Clark         qemu_fdt_setprop_string(fdt, nodename, "device_type", "cpu");
197a7240d1eSMichael Clark         qemu_fdt_add_subnode(fdt, intc);
198382cb439SBin Meng         qemu_fdt_setprop_cell(fdt, intc, "phandle", cpu_phandle);
199a7240d1eSMichael Clark         qemu_fdt_setprop_string(fdt, intc, "compatible", "riscv,cpu-intc");
200a7240d1eSMichael Clark         qemu_fdt_setprop(fdt, intc, "interrupt-controller", NULL, 0);
201a7240d1eSMichael Clark         qemu_fdt_setprop_cell(fdt, intc, "#interrupt-cells", 1);
202a7240d1eSMichael Clark         g_free(isa);
203a7240d1eSMichael Clark         g_free(intc);
204a7240d1eSMichael Clark         g_free(nodename);
205a7240d1eSMichael Clark     }
206a7240d1eSMichael Clark 
207ecdfe393SBin Meng     cells =  g_new0(uint32_t, ms->smp.cpus * 4);
208ecdfe393SBin Meng     for (cpu = 0; cpu < ms->smp.cpus; cpu++) {
209a7240d1eSMichael Clark         nodename =
210a7240d1eSMichael Clark             g_strdup_printf("/cpus/cpu@%d/interrupt-controller", cpu);
211a7240d1eSMichael Clark         uint32_t intc_phandle = qemu_fdt_get_phandle(fdt, nodename);
212a7240d1eSMichael Clark         cells[cpu * 4 + 0] = cpu_to_be32(intc_phandle);
213a7240d1eSMichael Clark         cells[cpu * 4 + 1] = cpu_to_be32(IRQ_M_SOFT);
214a7240d1eSMichael Clark         cells[cpu * 4 + 2] = cpu_to_be32(intc_phandle);
215a7240d1eSMichael Clark         cells[cpu * 4 + 3] = cpu_to_be32(IRQ_M_TIMER);
216a7240d1eSMichael Clark         g_free(nodename);
217a7240d1eSMichael Clark     }
218a7240d1eSMichael Clark     nodename = g_strdup_printf("/soc/clint@%lx",
21913b8c354SEduardo Habkost         (long)memmap[SIFIVE_U_DEV_CLINT].base);
220a7240d1eSMichael Clark     qemu_fdt_add_subnode(fdt, nodename);
2217cfbb17fSBin Meng     qemu_fdt_setprop_string_array(fdt, nodename, "compatible",
2227cfbb17fSBin Meng         (char **)&clint_compat, ARRAY_SIZE(clint_compat));
223a7240d1eSMichael Clark     qemu_fdt_setprop_cells(fdt, nodename, "reg",
22413b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_CLINT].base,
22513b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_CLINT].size);
226a7240d1eSMichael Clark     qemu_fdt_setprop(fdt, nodename, "interrupts-extended",
227ecdfe393SBin Meng         cells, ms->smp.cpus * sizeof(uint32_t) * 4);
228a7240d1eSMichael Clark     g_free(cells);
229a7240d1eSMichael Clark     g_free(nodename);
230a7240d1eSMichael Clark 
231ea85f27dSBin Meng     nodename = g_strdup_printf("/soc/otp@%lx",
23213b8c354SEduardo Habkost         (long)memmap[SIFIVE_U_DEV_OTP].base);
233ea85f27dSBin Meng     qemu_fdt_add_subnode(fdt, nodename);
234ea85f27dSBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "fuse-count", SIFIVE_U_OTP_REG_SIZE);
235ea85f27dSBin Meng     qemu_fdt_setprop_cells(fdt, nodename, "reg",
23613b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_OTP].base,
23713b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_OTP].size);
238ea85f27dSBin Meng     qemu_fdt_setprop_string(fdt, nodename, "compatible",
239ea85f27dSBin Meng         "sifive,fu540-c000-otp");
240ea85f27dSBin Meng     g_free(nodename);
241ea85f27dSBin Meng 
242af14c840SBin Meng     prci_phandle = phandle++;
243af14c840SBin Meng     nodename = g_strdup_printf("/soc/clock-controller@%lx",
24413b8c354SEduardo Habkost         (long)memmap[SIFIVE_U_DEV_PRCI].base);
245af14c840SBin Meng     qemu_fdt_add_subnode(fdt, nodename);
246af14c840SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "phandle", prci_phandle);
247af14c840SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "#clock-cells", 0x1);
248af14c840SBin Meng     qemu_fdt_setprop_cells(fdt, nodename, "clocks",
249af14c840SBin Meng         hfclk_phandle, rtcclk_phandle);
250af14c840SBin Meng     qemu_fdt_setprop_cells(fdt, nodename, "reg",
25113b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_PRCI].base,
25213b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_PRCI].size);
253af14c840SBin Meng     qemu_fdt_setprop_string(fdt, nodename, "compatible",
254af14c840SBin Meng         "sifive,fu540-c000-prci");
255af14c840SBin Meng     g_free(nodename);
256af14c840SBin Meng 
257382cb439SBin Meng     plic_phandle = phandle++;
258ecdfe393SBin Meng     cells =  g_new0(uint32_t, ms->smp.cpus * 4 - 2);
259ecdfe393SBin Meng     for (cpu = 0; cpu < ms->smp.cpus; cpu++) {
260a7240d1eSMichael Clark         nodename =
261a7240d1eSMichael Clark             g_strdup_printf("/cpus/cpu@%d/interrupt-controller", cpu);
262a7240d1eSMichael Clark         uint32_t intc_phandle = qemu_fdt_get_phandle(fdt, nodename);
263ecdfe393SBin Meng         /* cpu 0 is the management hart that does not have S-mode */
264ecdfe393SBin Meng         if (cpu == 0) {
265ecdfe393SBin Meng             cells[0] = cpu_to_be32(intc_phandle);
266ecdfe393SBin Meng             cells[1] = cpu_to_be32(IRQ_M_EXT);
267ecdfe393SBin Meng         } else {
268ecdfe393SBin Meng             cells[cpu * 4 - 2] = cpu_to_be32(intc_phandle);
269ecdfe393SBin Meng             cells[cpu * 4 - 1] = cpu_to_be32(IRQ_M_EXT);
270a7240d1eSMichael Clark             cells[cpu * 4 + 0] = cpu_to_be32(intc_phandle);
271ecdfe393SBin Meng             cells[cpu * 4 + 1] = cpu_to_be32(IRQ_S_EXT);
272ecdfe393SBin Meng         }
273a7240d1eSMichael Clark         g_free(nodename);
274a7240d1eSMichael Clark     }
275a7240d1eSMichael Clark     nodename = g_strdup_printf("/soc/interrupt-controller@%lx",
27613b8c354SEduardo Habkost         (long)memmap[SIFIVE_U_DEV_PLIC].base);
277a7240d1eSMichael Clark     qemu_fdt_add_subnode(fdt, nodename);
278a7240d1eSMichael Clark     qemu_fdt_setprop_cell(fdt, nodename, "#interrupt-cells", 1);
27960bb5407SBin Meng     qemu_fdt_setprop_string_array(fdt, nodename, "compatible",
28060bb5407SBin Meng         (char **)&plic_compat, ARRAY_SIZE(plic_compat));
281a7240d1eSMichael Clark     qemu_fdt_setprop(fdt, nodename, "interrupt-controller", NULL, 0);
282a7240d1eSMichael Clark     qemu_fdt_setprop(fdt, nodename, "interrupts-extended",
283ecdfe393SBin Meng         cells, (ms->smp.cpus * 4 - 2) * sizeof(uint32_t));
284a7240d1eSMichael Clark     qemu_fdt_setprop_cells(fdt, nodename, "reg",
28513b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_PLIC].base,
28613b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_PLIC].size);
28798ceee7fSAlistair Francis     qemu_fdt_setprop_cell(fdt, nodename, "riscv,ndev", 0x35);
28804e7edd1SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "phandle", plic_phandle);
289a7240d1eSMichael Clark     plic_phandle = qemu_fdt_get_phandle(fdt, nodename);
290a7240d1eSMichael Clark     g_free(cells);
291a7240d1eSMichael Clark     g_free(nodename);
292a7240d1eSMichael Clark 
2935133ed17SBin Meng     gpio_phandle = phandle++;
2948a88b9f5SBin Meng     nodename = g_strdup_printf("/soc/gpio@%lx",
29513b8c354SEduardo Habkost         (long)memmap[SIFIVE_U_DEV_GPIO].base);
2968a88b9f5SBin Meng     qemu_fdt_add_subnode(fdt, nodename);
2975133ed17SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "phandle", gpio_phandle);
2988a88b9f5SBin Meng     qemu_fdt_setprop_cells(fdt, nodename, "clocks",
2998a88b9f5SBin Meng         prci_phandle, PRCI_CLK_TLCLK);
3008a88b9f5SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "#interrupt-cells", 2);
3018a88b9f5SBin Meng     qemu_fdt_setprop(fdt, nodename, "interrupt-controller", NULL, 0);
3028a88b9f5SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "#gpio-cells", 2);
3038a88b9f5SBin Meng     qemu_fdt_setprop(fdt, nodename, "gpio-controller", NULL, 0);
3048a88b9f5SBin Meng     qemu_fdt_setprop_cells(fdt, nodename, "reg",
30513b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_GPIO].base,
30613b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_GPIO].size);
3078a88b9f5SBin Meng     qemu_fdt_setprop_cells(fdt, nodename, "interrupts", SIFIVE_U_GPIO_IRQ0,
3088a88b9f5SBin Meng         SIFIVE_U_GPIO_IRQ1, SIFIVE_U_GPIO_IRQ2, SIFIVE_U_GPIO_IRQ3,
3098a88b9f5SBin Meng         SIFIVE_U_GPIO_IRQ4, SIFIVE_U_GPIO_IRQ5, SIFIVE_U_GPIO_IRQ6,
3108a88b9f5SBin Meng         SIFIVE_U_GPIO_IRQ7, SIFIVE_U_GPIO_IRQ8, SIFIVE_U_GPIO_IRQ9,
3118a88b9f5SBin Meng         SIFIVE_U_GPIO_IRQ10, SIFIVE_U_GPIO_IRQ11, SIFIVE_U_GPIO_IRQ12,
3128a88b9f5SBin Meng         SIFIVE_U_GPIO_IRQ13, SIFIVE_U_GPIO_IRQ14, SIFIVE_U_GPIO_IRQ15);
3138a88b9f5SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle);
3148a88b9f5SBin Meng     qemu_fdt_setprop_string(fdt, nodename, "compatible", "sifive,gpio0");
3158a88b9f5SBin Meng     g_free(nodename);
3168a88b9f5SBin Meng 
3175133ed17SBin Meng     nodename = g_strdup_printf("/gpio-restart");
3185133ed17SBin Meng     qemu_fdt_add_subnode(fdt, nodename);
3195133ed17SBin Meng     qemu_fdt_setprop_cells(fdt, nodename, "gpios", gpio_phandle, 10, 1);
3205133ed17SBin Meng     qemu_fdt_setprop_string(fdt, nodename, "compatible", "gpio-restart");
3215133ed17SBin Meng     g_free(nodename);
3225133ed17SBin Meng 
323834e027aSBin Meng     nodename = g_strdup_printf("/soc/dma@%lx",
32413b8c354SEduardo Habkost         (long)memmap[SIFIVE_U_DEV_PDMA].base);
325834e027aSBin Meng     qemu_fdt_add_subnode(fdt, nodename);
326834e027aSBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "#dma-cells", 1);
327834e027aSBin Meng     qemu_fdt_setprop_cells(fdt, nodename, "interrupts",
328834e027aSBin Meng         SIFIVE_U_PDMA_IRQ0, SIFIVE_U_PDMA_IRQ1, SIFIVE_U_PDMA_IRQ2,
329834e027aSBin Meng         SIFIVE_U_PDMA_IRQ3, SIFIVE_U_PDMA_IRQ4, SIFIVE_U_PDMA_IRQ5,
330834e027aSBin Meng         SIFIVE_U_PDMA_IRQ6, SIFIVE_U_PDMA_IRQ7);
331834e027aSBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle);
332834e027aSBin Meng     qemu_fdt_setprop_cells(fdt, nodename, "reg",
33313b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_PDMA].base,
33413b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_PDMA].size);
335834e027aSBin Meng     qemu_fdt_setprop_string(fdt, nodename, "compatible",
336834e027aSBin Meng                             "sifive,fu540-c000-pdma");
337834e027aSBin Meng     g_free(nodename);
338834e027aSBin Meng 
3396eaf9cf5SBin Meng     nodename = g_strdup_printf("/soc/cache-controller@%lx",
34013b8c354SEduardo Habkost         (long)memmap[SIFIVE_U_DEV_L2CC].base);
3416eaf9cf5SBin Meng     qemu_fdt_add_subnode(fdt, nodename);
3426eaf9cf5SBin Meng     qemu_fdt_setprop_cells(fdt, nodename, "reg",
34313b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_L2CC].base,
34413b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_L2CC].size);
3456eaf9cf5SBin Meng     qemu_fdt_setprop_cells(fdt, nodename, "interrupts",
3466eaf9cf5SBin Meng         SIFIVE_U_L2CC_IRQ0, SIFIVE_U_L2CC_IRQ1, SIFIVE_U_L2CC_IRQ2);
3476eaf9cf5SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle);
3486eaf9cf5SBin Meng     qemu_fdt_setprop(fdt, nodename, "cache-unified", NULL, 0);
3496eaf9cf5SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "cache-size", 2097152);
3506eaf9cf5SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "cache-sets", 1024);
3516eaf9cf5SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "cache-level", 2);
3526eaf9cf5SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "cache-block-size", 64);
3536eaf9cf5SBin Meng     qemu_fdt_setprop_string(fdt, nodename, "compatible",
3546eaf9cf5SBin Meng                             "sifive,fu540-c000-ccache");
3556eaf9cf5SBin Meng     g_free(nodename);
3566eaf9cf5SBin Meng 
357145b2991SBin Meng     nodename = g_strdup_printf("/soc/spi@%lx",
358722f1352SBin Meng         (long)memmap[SIFIVE_U_DEV_QSPI2].base);
359722f1352SBin Meng     qemu_fdt_add_subnode(fdt, nodename);
360722f1352SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "#size-cells", 0);
361722f1352SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "#address-cells", 1);
362722f1352SBin Meng     qemu_fdt_setprop_cells(fdt, nodename, "clocks",
363722f1352SBin Meng         prci_phandle, PRCI_CLK_TLCLK);
364722f1352SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "interrupts", SIFIVE_U_QSPI2_IRQ);
365722f1352SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle);
366722f1352SBin Meng     qemu_fdt_setprop_cells(fdt, nodename, "reg",
367722f1352SBin Meng         0x0, memmap[SIFIVE_U_DEV_QSPI2].base,
368722f1352SBin Meng         0x0, memmap[SIFIVE_U_DEV_QSPI2].size);
369722f1352SBin Meng     qemu_fdt_setprop_string(fdt, nodename, "compatible", "sifive,spi0");
370722f1352SBin Meng     g_free(nodename);
371722f1352SBin Meng 
372722f1352SBin Meng     nodename = g_strdup_printf("/soc/spi@%lx/mmc@0",
373722f1352SBin Meng         (long)memmap[SIFIVE_U_DEV_QSPI2].base);
374722f1352SBin Meng     qemu_fdt_add_subnode(fdt, nodename);
375722f1352SBin Meng     qemu_fdt_setprop(fdt, nodename, "disable-wp", NULL, 0);
376722f1352SBin Meng     qemu_fdt_setprop_cells(fdt, nodename, "voltage-ranges", 3300, 3300);
377722f1352SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "spi-max-frequency", 20000000);
378722f1352SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "reg", 0);
379722f1352SBin Meng     qemu_fdt_setprop_string(fdt, nodename, "compatible", "mmc-spi-slot");
380722f1352SBin Meng     g_free(nodename);
381722f1352SBin Meng 
382722f1352SBin Meng     nodename = g_strdup_printf("/soc/spi@%lx",
383145b2991SBin Meng         (long)memmap[SIFIVE_U_DEV_QSPI0].base);
384145b2991SBin Meng     qemu_fdt_add_subnode(fdt, nodename);
385145b2991SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "#size-cells", 0);
386145b2991SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "#address-cells", 1);
387145b2991SBin Meng     qemu_fdt_setprop_cells(fdt, nodename, "clocks",
388145b2991SBin Meng         prci_phandle, PRCI_CLK_TLCLK);
389145b2991SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "interrupts", SIFIVE_U_QSPI0_IRQ);
390145b2991SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle);
391145b2991SBin Meng     qemu_fdt_setprop_cells(fdt, nodename, "reg",
392145b2991SBin Meng         0x0, memmap[SIFIVE_U_DEV_QSPI0].base,
393145b2991SBin Meng         0x0, memmap[SIFIVE_U_DEV_QSPI0].size);
394145b2991SBin Meng     qemu_fdt_setprop_string(fdt, nodename, "compatible", "sifive,spi0");
395145b2991SBin Meng     g_free(nodename);
396145b2991SBin Meng 
397145b2991SBin Meng     nodename = g_strdup_printf("/soc/spi@%lx/flash@0",
398145b2991SBin Meng         (long)memmap[SIFIVE_U_DEV_QSPI0].base);
399145b2991SBin Meng     qemu_fdt_add_subnode(fdt, nodename);
400145b2991SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "spi-rx-bus-width", 4);
401145b2991SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "spi-tx-bus-width", 4);
402145b2991SBin Meng     qemu_fdt_setprop(fdt, nodename, "m25p,fast-read", NULL, 0);
403145b2991SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "spi-max-frequency", 50000000);
404145b2991SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "reg", 0);
405145b2991SBin Meng     qemu_fdt_setprop_string(fdt, nodename, "compatible", "jedec,spi-nor");
406145b2991SBin Meng     g_free(nodename);
407145b2991SBin Meng 
4087b6bb66fSBin Meng     phy_phandle = phandle++;
4095a7f76a3SAlistair Francis     nodename = g_strdup_printf("/soc/ethernet@%lx",
41013b8c354SEduardo Habkost         (long)memmap[SIFIVE_U_DEV_GEM].base);
4115a7f76a3SAlistair Francis     qemu_fdt_add_subnode(fdt, nodename);
4127b6bb66fSBin Meng     qemu_fdt_setprop_string(fdt, nodename, "compatible",
4137b6bb66fSBin Meng         "sifive,fu540-c000-gem");
4145a7f76a3SAlistair Francis     qemu_fdt_setprop_cells(fdt, nodename, "reg",
41513b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_GEM].base,
41613b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_GEM].size,
41713b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_GEM_MGMT].base,
41813b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_GEM_MGMT].size);
4195a7f76a3SAlistair Francis     qemu_fdt_setprop_string(fdt, nodename, "reg-names", "control");
4205a7f76a3SAlistair Francis     qemu_fdt_setprop_string(fdt, nodename, "phy-mode", "gmii");
4217b6bb66fSBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "phy-handle", phy_phandle);
42204e7edd1SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle);
42304e7edd1SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "interrupts", SIFIVE_U_GEM_IRQ);
424fe93582cSAnup Patel     qemu_fdt_setprop_cells(fdt, nodename, "clocks",
425806c64b7SBin Meng         prci_phandle, PRCI_CLK_GEMGXLPLL, prci_phandle, PRCI_CLK_GEMGXLPLL);
426cb53b283SBin Meng     qemu_fdt_setprop_string_array(fdt, nodename, "clock-names",
427cb53b283SBin Meng         (char **)&ethclk_names, ARRAY_SIZE(ethclk_names));
4287b6bb66fSBin Meng     qemu_fdt_setprop(fdt, nodename, "local-mac-address",
4297b6bb66fSBin Meng         s->soc.gem.conf.macaddr.a, ETH_ALEN);
43004e7edd1SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "#address-cells", 1);
43104e7edd1SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "#size-cells", 0);
432c3a28b5dSBin Meng 
433c3a28b5dSBin Meng     qemu_fdt_add_subnode(fdt, "/aliases");
434c3a28b5dSBin Meng     qemu_fdt_setprop_string(fdt, "/aliases", "ethernet0", nodename);
435c3a28b5dSBin Meng 
4365a7f76a3SAlistair Francis     g_free(nodename);
4375a7f76a3SAlistair Francis 
4385a7f76a3SAlistair Francis     nodename = g_strdup_printf("/soc/ethernet@%lx/ethernet-phy@0",
43913b8c354SEduardo Habkost         (long)memmap[SIFIVE_U_DEV_GEM].base);
4405a7f76a3SAlistair Francis     qemu_fdt_add_subnode(fdt, nodename);
4417b6bb66fSBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "phandle", phy_phandle);
44204e7edd1SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "reg", 0x0);
4435a7f76a3SAlistair Francis     g_free(nodename);
4445a7f76a3SAlistair Francis 
4455f7134d3SBin Meng     nodename = g_strdup_printf("/soc/serial@%lx",
44610b43754SAnup Patel         (long)memmap[SIFIVE_U_DEV_UART1].base);
44710b43754SAnup Patel     qemu_fdt_add_subnode(fdt, nodename);
44810b43754SAnup Patel     qemu_fdt_setprop_string(fdt, nodename, "compatible", "sifive,uart0");
44910b43754SAnup Patel     qemu_fdt_setprop_cells(fdt, nodename, "reg",
45010b43754SAnup Patel         0x0, memmap[SIFIVE_U_DEV_UART1].base,
45110b43754SAnup Patel         0x0, memmap[SIFIVE_U_DEV_UART1].size);
45210b43754SAnup Patel     qemu_fdt_setprop_cells(fdt, nodename, "clocks",
45310b43754SAnup Patel         prci_phandle, PRCI_CLK_TLCLK);
45410b43754SAnup Patel     qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle);
45510b43754SAnup Patel     qemu_fdt_setprop_cell(fdt, nodename, "interrupts", SIFIVE_U_UART1_IRQ);
45610b43754SAnup Patel 
45710b43754SAnup Patel     qemu_fdt_setprop_string(fdt, "/aliases", "serial1", nodename);
45810b43754SAnup Patel     g_free(nodename);
45910b43754SAnup Patel 
46010b43754SAnup Patel     nodename = g_strdup_printf("/soc/serial@%lx",
46113b8c354SEduardo Habkost         (long)memmap[SIFIVE_U_DEV_UART0].base);
462a7240d1eSMichael Clark     qemu_fdt_add_subnode(fdt, nodename);
463a7240d1eSMichael Clark     qemu_fdt_setprop_string(fdt, nodename, "compatible", "sifive,uart0");
464a7240d1eSMichael Clark     qemu_fdt_setprop_cells(fdt, nodename, "reg",
46513b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_UART0].base,
46613b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_UART0].size);
467806c64b7SBin Meng     qemu_fdt_setprop_cells(fdt, nodename, "clocks",
468806c64b7SBin Meng         prci_phandle, PRCI_CLK_TLCLK);
46904e7edd1SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle);
47004e7edd1SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "interrupts", SIFIVE_U_UART0_IRQ);
471a7240d1eSMichael Clark 
472a7240d1eSMichael Clark     qemu_fdt_add_subnode(fdt, "/chosen");
473a7240d1eSMichael Clark     qemu_fdt_setprop_string(fdt, "/chosen", "stdout-path", nodename);
47444e6dcd3SGuenter Roeck     qemu_fdt_setprop_string(fdt, "/aliases", "serial0", nodename);
47544e6dcd3SGuenter Roeck 
476a7240d1eSMichael Clark     g_free(nodename);
477d5c90cf3SAnup Patel 
478d5c90cf3SAnup Patel update_bootargs:
479d5c90cf3SAnup Patel     if (cmdline) {
480d5c90cf3SAnup Patel         qemu_fdt_setprop_string(fdt, "/chosen", "bootargs", cmdline);
481d5c90cf3SAnup Patel     }
482a7240d1eSMichael Clark }
483a7240d1eSMichael Clark 
4845133ed17SBin Meng static void sifive_u_machine_reset(void *opaque, int n, int level)
4855133ed17SBin Meng {
4865133ed17SBin Meng     /* gpio pin active low triggers reset */
4875133ed17SBin Meng     if (!level) {
4885133ed17SBin Meng         qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
4895133ed17SBin Meng     }
4905133ed17SBin Meng }
4915133ed17SBin Meng 
492523e3464SAlistair Francis static void sifive_u_machine_init(MachineState *machine)
493a7240d1eSMichael Clark {
49473261285SBin Meng     const MemMapEntry *memmap = sifive_u_memmap;
495687caef1SAlistair Francis     SiFiveUState *s = RISCV_U_MACHINE(machine);
4965aec3247SMichael Clark     MemoryRegion *system_memory = get_system_memory();
497a7240d1eSMichael Clark     MemoryRegion *main_mem = g_new(MemoryRegion, 1);
4981b3a2308SAlistair Francis     MemoryRegion *flash0 = g_new(MemoryRegion, 1);
49913b8c354SEduardo Habkost     target_ulong start_addr = memmap[SIFIVE_U_DEV_DRAM].base;
50038bc4e34SAlistair Francis     target_ulong firmware_end_addr, kernel_start_addr;
5018590f536SAtish Patra     uint32_t start_addr_hi32 = 0x00000000;
5025aec3247SMichael Clark     int i;
50366b1205bSAtish Patra     uint32_t fdt_load_addr;
504dc144fe1SAtish Patra     uint64_t kernel_entry;
505145b2991SBin Meng     DriveInfo *dinfo;
506722f1352SBin Meng     DeviceState *flash_dev, *sd_dev;
507722f1352SBin Meng     qemu_irq flash_cs, sd_cs;
508a7240d1eSMichael Clark 
5092308092bSAlistair Francis     /* Initialize SoC */
5109fc7fc4dSMarkus Armbruster     object_initialize_child(OBJECT(machine), "soc", &s->soc, TYPE_RISCV_U_SOC);
5115325cc34SMarkus Armbruster     object_property_set_uint(OBJECT(&s->soc), "serial", s->serial,
5123ca109c3SBin Meng                              &error_abort);
513099be035SAlistair Francis     object_property_set_str(OBJECT(&s->soc), "cpu-type", machine->cpu_type,
514099be035SAlistair Francis                              &error_abort);
515ce189ab2SMarkus Armbruster     qdev_realize(DEVICE(&s->soc), NULL, &error_abort);
516a7240d1eSMichael Clark 
517a7240d1eSMichael Clark     /* register RAM */
518a7240d1eSMichael Clark     memory_region_init_ram(main_mem, NULL, "riscv.sifive.u.ram",
519a7240d1eSMichael Clark                            machine->ram_size, &error_fatal);
52013b8c354SEduardo Habkost     memory_region_add_subregion(system_memory, memmap[SIFIVE_U_DEV_DRAM].base,
521a7240d1eSMichael Clark                                 main_mem);
522a7240d1eSMichael Clark 
5231b3a2308SAlistair Francis     /* register QSPI0 Flash */
5241b3a2308SAlistair Francis     memory_region_init_ram(flash0, NULL, "riscv.sifive.u.flash0",
52513b8c354SEduardo Habkost                            memmap[SIFIVE_U_DEV_FLASH0].size, &error_fatal);
52613b8c354SEduardo Habkost     memory_region_add_subregion(system_memory, memmap[SIFIVE_U_DEV_FLASH0].base,
5271b3a2308SAlistair Francis                                 flash0);
5281b3a2308SAlistair Francis 
5295133ed17SBin Meng     /* register gpio-restart */
5305133ed17SBin Meng     qdev_connect_gpio_out(DEVICE(&(s->soc.gpio)), 10,
5315133ed17SBin Meng                           qemu_allocate_irq(sifive_u_machine_reset, NULL, 0));
5325133ed17SBin Meng 
533a7240d1eSMichael Clark     /* create device tree */
5342206ffa6SAlistair Francis     create_fdt(s, memmap, machine->ram_size, machine->kernel_cmdline,
535a8259b53SAlistair Francis                riscv_is_32bit(&s->soc.u_cpus));
536a7240d1eSMichael Clark 
53717aad9f2SBin Meng     if (s->start_in_flash) {
53817aad9f2SBin Meng         /*
53917aad9f2SBin Meng          * If start_in_flash property is given, assign s->msel to a value
54017aad9f2SBin Meng          * that representing booting from QSPI0 memory-mapped flash.
54117aad9f2SBin Meng          *
54217aad9f2SBin Meng          * This also means that when both start_in_flash and msel properties
54317aad9f2SBin Meng          * are given, start_in_flash takes the precedence over msel.
54417aad9f2SBin Meng          *
54517aad9f2SBin Meng          * Note this is to keep backward compatibility not to break existing
54617aad9f2SBin Meng          * users that use start_in_flash property.
54717aad9f2SBin Meng          */
54817aad9f2SBin Meng         s->msel = MSEL_MEMMAP_QSPI0_FLASH;
54917aad9f2SBin Meng     }
55017aad9f2SBin Meng 
55117aad9f2SBin Meng     switch (s->msel) {
55217aad9f2SBin Meng     case MSEL_MEMMAP_QSPI0_FLASH:
55313b8c354SEduardo Habkost         start_addr = memmap[SIFIVE_U_DEV_FLASH0].base;
55417aad9f2SBin Meng         break;
55517aad9f2SBin Meng     case MSEL_L2LIM_QSPI0_FLASH:
55617aad9f2SBin Meng     case MSEL_L2LIM_QSPI2_SD:
55713b8c354SEduardo Habkost         start_addr = memmap[SIFIVE_U_DEV_L2LIM].base;
55817aad9f2SBin Meng         break;
55917aad9f2SBin Meng     default:
56013b8c354SEduardo Habkost         start_addr = memmap[SIFIVE_U_DEV_DRAM].base;
56117aad9f2SBin Meng         break;
56217aad9f2SBin Meng     }
56317aad9f2SBin Meng 
564a8259b53SAlistair Francis     if (riscv_is_32bit(&s->soc.u_cpus)) {
5652206ffa6SAlistair Francis         firmware_end_addr = riscv_find_and_load_firmware(machine,
566a0acd0a1SBin Meng                                     RISCV32_BIOS_BIN, start_addr, NULL);
5672206ffa6SAlistair Francis     } else {
5682206ffa6SAlistair Francis         firmware_end_addr = riscv_find_and_load_firmware(machine,
569a0acd0a1SBin Meng                                     RISCV64_BIOS_BIN, start_addr, NULL);
5702206ffa6SAlistair Francis     }
571b3042223SAlistair Francis 
572a7240d1eSMichael Clark     if (machine->kernel_filename) {
573a8259b53SAlistair Francis         kernel_start_addr = riscv_calc_kernel_start_addr(&s->soc.u_cpus,
57438bc4e34SAlistair Francis                                                          firmware_end_addr);
57538bc4e34SAlistair Francis 
57638bc4e34SAlistair Francis         kernel_entry = riscv_load_kernel(machine->kernel_filename,
57738bc4e34SAlistair Francis                                          kernel_start_addr, NULL);
5780f8d4462SGuenter Roeck 
5790f8d4462SGuenter Roeck         if (machine->initrd_filename) {
5800f8d4462SGuenter Roeck             hwaddr start;
5810f8d4462SGuenter Roeck             hwaddr end = riscv_load_initrd(machine->initrd_filename,
5820f8d4462SGuenter Roeck                                            machine->ram_size, kernel_entry,
5830f8d4462SGuenter Roeck                                            &start);
5849f79638eSBin Meng             qemu_fdt_setprop_cell(s->fdt, "/chosen",
5850f8d4462SGuenter Roeck                                   "linux,initrd-start", start);
5869f79638eSBin Meng             qemu_fdt_setprop_cell(s->fdt, "/chosen", "linux,initrd-end",
5870f8d4462SGuenter Roeck                                   end);
5880f8d4462SGuenter Roeck         }
589dc144fe1SAtish Patra     } else {
590dc144fe1SAtish Patra        /*
591dc144fe1SAtish Patra         * If dynamic firmware is used, it doesn't know where is the next mode
592dc144fe1SAtish Patra         * if kernel argument is not set.
593dc144fe1SAtish Patra         */
594dc144fe1SAtish Patra         kernel_entry = 0;
595a7240d1eSMichael Clark     }
596a7240d1eSMichael Clark 
59766b1205bSAtish Patra     /* Compute the fdt load address in dram */
59813b8c354SEduardo Habkost     fdt_load_addr = riscv_load_fdt(memmap[SIFIVE_U_DEV_DRAM].base,
59966b1205bSAtish Patra                                    machine->ram_size, s->fdt);
600a8259b53SAlistair Francis     if (!riscv_is_32bit(&s->soc.u_cpus)) {
6012206ffa6SAlistair Francis         start_addr_hi32 = (uint64_t)start_addr >> 32;
6022206ffa6SAlistair Francis     }
60366b1205bSAtish Patra 
604a7240d1eSMichael Clark     /* reset vector */
60566b1205bSAtish Patra     uint32_t reset_vec[11] = {
60617aad9f2SBin Meng         s->msel,                       /* MSEL pin state */
607dc144fe1SAtish Patra         0x00000297,                    /* 1:  auipc  t0, %pcrel_hi(fw_dyn) */
608dc144fe1SAtish Patra         0x02828613,                    /*     addi   a2, t0, %pcrel_lo(1b) */
609a7240d1eSMichael Clark         0xf1402573,                    /*     csrr   a0, mhartid  */
6102206ffa6SAlistair Francis         0,
6112206ffa6SAlistair Francis         0,
612a7240d1eSMichael Clark         0x00028067,                    /*     jr     t0 */
613fc41ae23SAlistair Francis         start_addr,                    /* start: .dword */
6148590f536SAtish Patra         start_addr_hi32,
61566b1205bSAtish Patra         fdt_load_addr,                 /* fdt_laddr: .dword */
61666b1205bSAtish Patra         0x00000000,
617dc144fe1SAtish Patra                                        /* fw_dyn: */
618a7240d1eSMichael Clark     };
619a8259b53SAlistair Francis     if (riscv_is_32bit(&s->soc.u_cpus)) {
6202206ffa6SAlistair Francis         reset_vec[4] = 0x0202a583;     /*     lw     a1, 32(t0) */
6212206ffa6SAlistair Francis         reset_vec[5] = 0x0182a283;     /*     lw     t0, 24(t0) */
6222206ffa6SAlistair Francis     } else {
6232206ffa6SAlistair Francis         reset_vec[4] = 0x0202b583;     /*     ld     a1, 32(t0) */
6242206ffa6SAlistair Francis         reset_vec[5] = 0x0182b283;     /*     ld     t0, 24(t0) */
6252206ffa6SAlistair Francis     }
6262206ffa6SAlistair Francis 
627a7240d1eSMichael Clark 
6285aec3247SMichael Clark     /* copy in the reset vector in little_endian byte order */
62966b1205bSAtish Patra     for (i = 0; i < ARRAY_SIZE(reset_vec); i++) {
6305aec3247SMichael Clark         reset_vec[i] = cpu_to_le32(reset_vec[i]);
6315aec3247SMichael Clark     }
6325aec3247SMichael Clark     rom_add_blob_fixed_as("mrom.reset", reset_vec, sizeof(reset_vec),
63313b8c354SEduardo Habkost                           memmap[SIFIVE_U_DEV_MROM].base, &address_space_memory);
634dc144fe1SAtish Patra 
63578936771SAlistair Francis     riscv_rom_copy_firmware_info(machine, memmap[SIFIVE_U_DEV_MROM].base,
63613b8c354SEduardo Habkost                                  memmap[SIFIVE_U_DEV_MROM].size,
637dc144fe1SAtish Patra                                  sizeof(reset_vec), kernel_entry);
638145b2991SBin Meng 
639145b2991SBin Meng     /* Connect an SPI flash to SPI0 */
640145b2991SBin Meng     flash_dev = qdev_new("is25wp256");
641145b2991SBin Meng     dinfo = drive_get_next(IF_MTD);
642145b2991SBin Meng     if (dinfo) {
643145b2991SBin Meng         qdev_prop_set_drive_err(flash_dev, "drive",
644145b2991SBin Meng                                 blk_by_legacy_dinfo(dinfo),
645145b2991SBin Meng                                 &error_fatal);
646145b2991SBin Meng     }
647145b2991SBin Meng     qdev_realize_and_unref(flash_dev, BUS(s->soc.spi0.spi), &error_fatal);
648145b2991SBin Meng 
649145b2991SBin Meng     flash_cs = qdev_get_gpio_in_named(flash_dev, SSI_GPIO_CS, 0);
650145b2991SBin Meng     sysbus_connect_irq(SYS_BUS_DEVICE(&s->soc.spi0), 1, flash_cs);
651722f1352SBin Meng 
652722f1352SBin Meng     /* Connect an SD card to SPI2 */
653722f1352SBin Meng     sd_dev = ssi_create_peripheral(s->soc.spi2.spi, "ssi-sd");
654722f1352SBin Meng 
655722f1352SBin Meng     sd_cs = qdev_get_gpio_in_named(sd_dev, SSI_GPIO_CS, 0);
656722f1352SBin Meng     sysbus_connect_irq(SYS_BUS_DEVICE(&s->soc.spi2), 1, sd_cs);
6572308092bSAlistair Francis }
6582308092bSAlistair Francis 
659523e3464SAlistair Francis static bool sifive_u_machine_get_start_in_flash(Object *obj, Error **errp)
660523e3464SAlistair Francis {
661523e3464SAlistair Francis     SiFiveUState *s = RISCV_U_MACHINE(obj);
662523e3464SAlistair Francis 
663523e3464SAlistair Francis     return s->start_in_flash;
664523e3464SAlistair Francis }
665523e3464SAlistair Francis 
666523e3464SAlistair Francis static void sifive_u_machine_set_start_in_flash(Object *obj, bool value, Error **errp)
667523e3464SAlistair Francis {
668523e3464SAlistair Francis     SiFiveUState *s = RISCV_U_MACHINE(obj);
669523e3464SAlistair Francis 
670523e3464SAlistair Francis     s->start_in_flash = value;
671523e3464SAlistair Francis }
672523e3464SAlistair Francis 
6733e9667cdSBin Meng static void sifive_u_machine_get_uint32_prop(Object *obj, Visitor *v,
6743e9667cdSBin Meng                                              const char *name, void *opaque,
6753e9667cdSBin Meng                                              Error **errp)
6763ca109c3SBin Meng {
6773ca109c3SBin Meng     visit_type_uint32(v, name, (uint32_t *)opaque, errp);
6783ca109c3SBin Meng }
6793ca109c3SBin Meng 
6803e9667cdSBin Meng static void sifive_u_machine_set_uint32_prop(Object *obj, Visitor *v,
6813e9667cdSBin Meng                                              const char *name, void *opaque,
6823e9667cdSBin Meng                                              Error **errp)
6833ca109c3SBin Meng {
6843ca109c3SBin Meng     visit_type_uint32(v, name, (uint32_t *)opaque, errp);
6853ca109c3SBin Meng }
6863ca109c3SBin Meng 
687523e3464SAlistair Francis static void sifive_u_machine_instance_init(Object *obj)
688523e3464SAlistair Francis {
689523e3464SAlistair Francis     SiFiveUState *s = RISCV_U_MACHINE(obj);
690523e3464SAlistair Francis 
691523e3464SAlistair Francis     s->start_in_flash = false;
692cfa32630SBin Meng     s->msel = 0;
693cfa32630SBin Meng     object_property_add(obj, "msel", "uint32",
694cfa32630SBin Meng                         sifive_u_machine_get_uint32_prop,
695cfa32630SBin Meng                         sifive_u_machine_set_uint32_prop, NULL, &s->msel);
696cfa32630SBin Meng     object_property_set_description(obj, "msel",
697cfa32630SBin Meng                                     "Mode Select (MSEL[3:0]) pin state");
698cfa32630SBin Meng 
6993ca109c3SBin Meng     s->serial = OTP_SERIAL;
700d2623129SMarkus Armbruster     object_property_add(obj, "serial", "uint32",
7013e9667cdSBin Meng                         sifive_u_machine_get_uint32_prop,
7023e9667cdSBin Meng                         sifive_u_machine_set_uint32_prop, NULL, &s->serial);
7037eecec7dSMarkus Armbruster     object_property_set_description(obj, "serial", "Board serial number");
704523e3464SAlistair Francis }
705523e3464SAlistair Francis 
706523e3464SAlistair Francis static void sifive_u_machine_class_init(ObjectClass *oc, void *data)
707523e3464SAlistair Francis {
708523e3464SAlistair Francis     MachineClass *mc = MACHINE_CLASS(oc);
709523e3464SAlistair Francis 
710523e3464SAlistair Francis     mc->desc = "RISC-V Board compatible with SiFive U SDK";
711523e3464SAlistair Francis     mc->init = sifive_u_machine_init;
712523e3464SAlistair Francis     mc->max_cpus = SIFIVE_U_MANAGEMENT_CPU_COUNT + SIFIVE_U_COMPUTE_CPU_COUNT;
713523e3464SAlistair Francis     mc->min_cpus = SIFIVE_U_MANAGEMENT_CPU_COUNT + 1;
7141eaada8aSBin Meng     mc->default_cpu_type = SIFIVE_U_CPU;
715523e3464SAlistair Francis     mc->default_cpus = mc->min_cpus;
716418b473eSEduardo Habkost 
717418b473eSEduardo Habkost     object_class_property_add_bool(oc, "start-in-flash",
718418b473eSEduardo Habkost                                    sifive_u_machine_get_start_in_flash,
719418b473eSEduardo Habkost                                    sifive_u_machine_set_start_in_flash);
720418b473eSEduardo Habkost     object_class_property_set_description(oc, "start-in-flash",
721418b473eSEduardo Habkost                                           "Set on to tell QEMU's ROM to jump to "
722418b473eSEduardo Habkost                                           "flash. Otherwise QEMU will jump to DRAM "
723418b473eSEduardo Habkost                                           "or L2LIM depending on the msel value");
724523e3464SAlistair Francis }
725523e3464SAlistair Francis 
726523e3464SAlistair Francis static const TypeInfo sifive_u_machine_typeinfo = {
727523e3464SAlistair Francis     .name       = MACHINE_TYPE_NAME("sifive_u"),
728523e3464SAlistair Francis     .parent     = TYPE_MACHINE,
729523e3464SAlistair Francis     .class_init = sifive_u_machine_class_init,
730523e3464SAlistair Francis     .instance_init = sifive_u_machine_instance_init,
731523e3464SAlistair Francis     .instance_size = sizeof(SiFiveUState),
732523e3464SAlistair Francis };
733523e3464SAlistair Francis 
734523e3464SAlistair Francis static void sifive_u_machine_init_register_types(void)
735523e3464SAlistair Francis {
736523e3464SAlistair Francis     type_register_static(&sifive_u_machine_typeinfo);
737523e3464SAlistair Francis }
738523e3464SAlistair Francis 
739523e3464SAlistair Francis type_init(sifive_u_machine_init_register_types)
740523e3464SAlistair Francis 
741139177b1SBin Meng static void sifive_u_soc_instance_init(Object *obj)
7422308092bSAlistair Francis {
7432308092bSAlistair Francis     SiFiveUSoCState *s = RISCV_U_SOC(obj);
7442308092bSAlistair Francis 
7459fc7fc4dSMarkus Armbruster     object_initialize_child(obj, "e-cluster", &s->e_cluster, TYPE_CPU_CLUSTER);
746ecdfe393SBin Meng     qdev_prop_set_uint32(DEVICE(&s->e_cluster), "cluster-id", 0);
747ecdfe393SBin Meng 
748db873cc5SMarkus Armbruster     object_initialize_child(OBJECT(&s->e_cluster), "e-cpus", &s->e_cpus,
74975a6ed87SMarkus Armbruster                             TYPE_RISCV_HART_ARRAY);
750ecdfe393SBin Meng     qdev_prop_set_uint32(DEVICE(&s->e_cpus), "num-harts", 1);
751ecdfe393SBin Meng     qdev_prop_set_uint32(DEVICE(&s->e_cpus), "hartid-base", 0);
752ecdfe393SBin Meng     qdev_prop_set_string(DEVICE(&s->e_cpus), "cpu-type", SIFIVE_E_CPU);
75373f6ed97SBin Meng     qdev_prop_set_uint64(DEVICE(&s->e_cpus), "resetvec", 0x1004);
754ecdfe393SBin Meng 
7559fc7fc4dSMarkus Armbruster     object_initialize_child(obj, "u-cluster", &s->u_cluster, TYPE_CPU_CLUSTER);
756ecdfe393SBin Meng     qdev_prop_set_uint32(DEVICE(&s->u_cluster), "cluster-id", 1);
757ecdfe393SBin Meng 
758db873cc5SMarkus Armbruster     object_initialize_child(OBJECT(&s->u_cluster), "u-cpus", &s->u_cpus,
75975a6ed87SMarkus Armbruster                             TYPE_RISCV_HART_ARRAY);
7605a7f76a3SAlistair Francis 
761db873cc5SMarkus Armbruster     object_initialize_child(obj, "prci", &s->prci, TYPE_SIFIVE_U_PRCI);
762db873cc5SMarkus Armbruster     object_initialize_child(obj, "otp", &s->otp, TYPE_SIFIVE_U_OTP);
763db873cc5SMarkus Armbruster     object_initialize_child(obj, "gem", &s->gem, TYPE_CADENCE_GEM);
7648a88b9f5SBin Meng     object_initialize_child(obj, "gpio", &s->gpio, TYPE_SIFIVE_GPIO);
765834e027aSBin Meng     object_initialize_child(obj, "pdma", &s->dma, TYPE_SIFIVE_PDMA);
766145b2991SBin Meng     object_initialize_child(obj, "spi0", &s->spi0, TYPE_SIFIVE_SPI);
767722f1352SBin Meng     object_initialize_child(obj, "spi2", &s->spi2, TYPE_SIFIVE_SPI);
7682308092bSAlistair Francis }
7692308092bSAlistair Francis 
770139177b1SBin Meng static void sifive_u_soc_realize(DeviceState *dev, Error **errp)
7712308092bSAlistair Francis {
772c4473127SLike Xu     MachineState *ms = MACHINE(qdev_get_machine());
7732308092bSAlistair Francis     SiFiveUSoCState *s = RISCV_U_SOC(dev);
77473261285SBin Meng     const MemMapEntry *memmap = sifive_u_memmap;
7752308092bSAlistair Francis     MemoryRegion *system_memory = get_system_memory();
7762308092bSAlistair Francis     MemoryRegion *mask_rom = g_new(MemoryRegion, 1);
777a6902ef0SAlistair Francis     MemoryRegion *l2lim_mem = g_new(MemoryRegion, 1);
77805446f41SBin Meng     char *plic_hart_config;
77905446f41SBin Meng     size_t plic_hart_config_len;
7805a7f76a3SAlistair Francis     int i;
7815a7f76a3SAlistair Francis     NICInfo *nd = &nd_table[0];
7822308092bSAlistair Francis 
783099be035SAlistair Francis     qdev_prop_set_uint32(DEVICE(&s->u_cpus), "num-harts", ms->smp.cpus - 1);
784099be035SAlistair Francis     qdev_prop_set_uint32(DEVICE(&s->u_cpus), "hartid-base", 1);
785099be035SAlistair Francis     qdev_prop_set_string(DEVICE(&s->u_cpus), "cpu-type", s->cpu_type);
786099be035SAlistair Francis     qdev_prop_set_uint64(DEVICE(&s->u_cpus), "resetvec", 0x1004);
787099be035SAlistair Francis 
788db873cc5SMarkus Armbruster     sysbus_realize(SYS_BUS_DEVICE(&s->e_cpus), &error_abort);
789db873cc5SMarkus Armbruster     sysbus_realize(SYS_BUS_DEVICE(&s->u_cpus), &error_abort);
790ecdfe393SBin Meng     /*
791ecdfe393SBin Meng      * The cluster must be realized after the RISC-V hart array container,
792ecdfe393SBin Meng      * as the container's CPU object is only created on realize, and the
793ecdfe393SBin Meng      * CPU must exist and have been parented into the cluster before the
794ecdfe393SBin Meng      * cluster is realized.
795ecdfe393SBin Meng      */
796ce189ab2SMarkus Armbruster     qdev_realize(DEVICE(&s->e_cluster), NULL, &error_abort);
797ce189ab2SMarkus Armbruster     qdev_realize(DEVICE(&s->u_cluster), NULL, &error_abort);
7982308092bSAlistair Francis 
7992308092bSAlistair Francis     /* boot rom */
800414c47d2SPhilippe Mathieu-Daudé     memory_region_init_rom(mask_rom, OBJECT(dev), "riscv.sifive.u.mrom",
80113b8c354SEduardo Habkost                            memmap[SIFIVE_U_DEV_MROM].size, &error_fatal);
80213b8c354SEduardo Habkost     memory_region_add_subregion(system_memory, memmap[SIFIVE_U_DEV_MROM].base,
8032308092bSAlistair Francis                                 mask_rom);
804a7240d1eSMichael Clark 
805a6902ef0SAlistair Francis     /*
806a6902ef0SAlistair Francis      * Add L2-LIM at reset size.
807a6902ef0SAlistair Francis      * This should be reduced in size as the L2 Cache Controller WayEnable
808a6902ef0SAlistair Francis      * register is incremented. Unfortunately I don't see a nice (or any) way
809a6902ef0SAlistair Francis      * to handle reducing or blocking out the L2 LIM while still allowing it
810a6902ef0SAlistair Francis      * be re returned to all enabled after a reset. For the time being, just
811a6902ef0SAlistair Francis      * leave it enabled all the time. This won't break anything, but will be
812a6902ef0SAlistair Francis      * too generous to misbehaving guests.
813a6902ef0SAlistair Francis      */
814a6902ef0SAlistair Francis     memory_region_init_ram(l2lim_mem, NULL, "riscv.sifive.u.l2lim",
81513b8c354SEduardo Habkost                            memmap[SIFIVE_U_DEV_L2LIM].size, &error_fatal);
81613b8c354SEduardo Habkost     memory_region_add_subregion(system_memory, memmap[SIFIVE_U_DEV_L2LIM].base,
817a6902ef0SAlistair Francis                                 l2lim_mem);
818a6902ef0SAlistair Francis 
81905446f41SBin Meng     /* create PLIC hart topology configuration string */
820c4473127SLike Xu     plic_hart_config_len = (strlen(SIFIVE_U_PLIC_HART_CONFIG) + 1) *
821c4473127SLike Xu                            ms->smp.cpus;
82205446f41SBin Meng     plic_hart_config = g_malloc0(plic_hart_config_len);
823c4473127SLike Xu     for (i = 0; i < ms->smp.cpus; i++) {
82405446f41SBin Meng         if (i != 0) {
825ef965ce2SBin Meng             strncat(plic_hart_config, "," SIFIVE_U_PLIC_HART_CONFIG,
82605446f41SBin Meng                     plic_hart_config_len);
827ef965ce2SBin Meng         } else {
828ef965ce2SBin Meng             strncat(plic_hart_config, "M", plic_hart_config_len);
829ef965ce2SBin Meng         }
83005446f41SBin Meng         plic_hart_config_len -= (strlen(SIFIVE_U_PLIC_HART_CONFIG) + 1);
83105446f41SBin Meng     }
83205446f41SBin Meng 
833a7240d1eSMichael Clark     /* MMIO */
83413b8c354SEduardo Habkost     s->plic = sifive_plic_create(memmap[SIFIVE_U_DEV_PLIC].base,
835c9270e10SAnup Patel         plic_hart_config, 0,
836a7240d1eSMichael Clark         SIFIVE_U_PLIC_NUM_SOURCES,
837a7240d1eSMichael Clark         SIFIVE_U_PLIC_NUM_PRIORITIES,
838a7240d1eSMichael Clark         SIFIVE_U_PLIC_PRIORITY_BASE,
839a7240d1eSMichael Clark         SIFIVE_U_PLIC_PENDING_BASE,
840a7240d1eSMichael Clark         SIFIVE_U_PLIC_ENABLE_BASE,
841a7240d1eSMichael Clark         SIFIVE_U_PLIC_ENABLE_STRIDE,
842a7240d1eSMichael Clark         SIFIVE_U_PLIC_CONTEXT_BASE,
843a7240d1eSMichael Clark         SIFIVE_U_PLIC_CONTEXT_STRIDE,
84413b8c354SEduardo Habkost         memmap[SIFIVE_U_DEV_PLIC].size);
845bb8136dfSPan Nengyuan     g_free(plic_hart_config);
84613b8c354SEduardo Habkost     sifive_uart_create(system_memory, memmap[SIFIVE_U_DEV_UART0].base,
847647a70a1SAlistair Francis         serial_hd(0), qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_U_UART0_IRQ));
84813b8c354SEduardo Habkost     sifive_uart_create(system_memory, memmap[SIFIVE_U_DEV_UART1].base,
849194eef09SMichael Clark         serial_hd(1), qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_U_UART1_IRQ));
85013b8c354SEduardo Habkost     sifive_clint_create(memmap[SIFIVE_U_DEV_CLINT].base,
85113b8c354SEduardo Habkost         memmap[SIFIVE_U_DEV_CLINT].size, 0, ms->smp.cpus,
852a47ef6e9SBin Meng         SIFIVE_SIP_BASE, SIFIVE_TIMECMP_BASE, SIFIVE_TIME_BASE,
853*074ca702SBin Meng         CLINT_TIMEBASE_FREQ, false);
8545a7f76a3SAlistair Francis 
855cbe3a8c5SMarkus Armbruster     if (!sysbus_realize(SYS_BUS_DEVICE(&s->prci), errp)) {
856cbe3a8c5SMarkus Armbruster         return;
857cbe3a8c5SMarkus Armbruster     }
85813b8c354SEduardo Habkost     sysbus_mmio_map(SYS_BUS_DEVICE(&s->prci), 0, memmap[SIFIVE_U_DEV_PRCI].base);
859af14c840SBin Meng 
8608a88b9f5SBin Meng     qdev_prop_set_uint32(DEVICE(&s->gpio), "ngpio", 16);
861cbe3a8c5SMarkus Armbruster     if (!sysbus_realize(SYS_BUS_DEVICE(&s->gpio), errp)) {
862cbe3a8c5SMarkus Armbruster         return;
863cbe3a8c5SMarkus Armbruster     }
86413b8c354SEduardo Habkost     sysbus_mmio_map(SYS_BUS_DEVICE(&s->gpio), 0, memmap[SIFIVE_U_DEV_GPIO].base);
8658a88b9f5SBin Meng 
8668a88b9f5SBin Meng     /* Pass all GPIOs to the SOC layer so they are available to the board */
8678a88b9f5SBin Meng     qdev_pass_gpios(DEVICE(&s->gpio), dev, NULL);
8688a88b9f5SBin Meng 
8698a88b9f5SBin Meng     /* Connect GPIO interrupts to the PLIC */
8708a88b9f5SBin Meng     for (i = 0; i < 16; i++) {
8718a88b9f5SBin Meng         sysbus_connect_irq(SYS_BUS_DEVICE(&s->gpio), i,
8728a88b9f5SBin Meng                            qdev_get_gpio_in(DEVICE(s->plic),
8738a88b9f5SBin Meng                                             SIFIVE_U_GPIO_IRQ0 + i));
8748a88b9f5SBin Meng     }
8758a88b9f5SBin Meng 
876834e027aSBin Meng     /* PDMA */
877834e027aSBin Meng     sysbus_realize(SYS_BUS_DEVICE(&s->dma), errp);
87813b8c354SEduardo Habkost     sysbus_mmio_map(SYS_BUS_DEVICE(&s->dma), 0, memmap[SIFIVE_U_DEV_PDMA].base);
879834e027aSBin Meng 
880834e027aSBin Meng     /* Connect PDMA interrupts to the PLIC */
881834e027aSBin Meng     for (i = 0; i < SIFIVE_PDMA_IRQS; i++) {
882834e027aSBin Meng         sysbus_connect_irq(SYS_BUS_DEVICE(&s->dma), i,
883834e027aSBin Meng                            qdev_get_gpio_in(DEVICE(s->plic),
884834e027aSBin Meng                                             SIFIVE_U_PDMA_IRQ0 + i));
885834e027aSBin Meng     }
886834e027aSBin Meng 
887fda5b000SAlistair Francis     qdev_prop_set_uint32(DEVICE(&s->otp), "serial", s->serial);
888cbe3a8c5SMarkus Armbruster     if (!sysbus_realize(SYS_BUS_DEVICE(&s->otp), errp)) {
889cbe3a8c5SMarkus Armbruster         return;
890cbe3a8c5SMarkus Armbruster     }
89113b8c354SEduardo Habkost     sysbus_mmio_map(SYS_BUS_DEVICE(&s->otp), 0, memmap[SIFIVE_U_DEV_OTP].base);
8925461c4feSBin Meng 
8937ad36e2eSMarkus Armbruster     /* FIXME use qdev NIC properties instead of nd_table[] */
8945a7f76a3SAlistair Francis     if (nd->used) {
8955a7f76a3SAlistair Francis         qemu_check_nic_model(nd, TYPE_CADENCE_GEM);
8965a7f76a3SAlistair Francis         qdev_set_nic_properties(DEVICE(&s->gem), nd);
8975a7f76a3SAlistair Francis     }
8985325cc34SMarkus Armbruster     object_property_set_int(OBJECT(&s->gem), "revision", GEM_REVISION,
8995a7f76a3SAlistair Francis                             &error_abort);
900668f62ecSMarkus Armbruster     if (!sysbus_realize(SYS_BUS_DEVICE(&s->gem), errp)) {
9015a7f76a3SAlistair Francis         return;
9025a7f76a3SAlistair Francis     }
90313b8c354SEduardo Habkost     sysbus_mmio_map(SYS_BUS_DEVICE(&s->gem), 0, memmap[SIFIVE_U_DEV_GEM].base);
9045a7f76a3SAlistair Francis     sysbus_connect_irq(SYS_BUS_DEVICE(&s->gem), 0,
9055874f0a7SBin Meng                        qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_U_GEM_IRQ));
9067b6bb66fSBin Meng 
9077b6bb66fSBin Meng     create_unimplemented_device("riscv.sifive.u.gem-mgmt",
90813b8c354SEduardo Habkost         memmap[SIFIVE_U_DEV_GEM_MGMT].base, memmap[SIFIVE_U_DEV_GEM_MGMT].size);
9093eaea6ebSBin Meng 
9103eaea6ebSBin Meng     create_unimplemented_device("riscv.sifive.u.dmc",
91113b8c354SEduardo Habkost         memmap[SIFIVE_U_DEV_DMC].base, memmap[SIFIVE_U_DEV_DMC].size);
9126eaf9cf5SBin Meng 
9136eaf9cf5SBin Meng     create_unimplemented_device("riscv.sifive.u.l2cc",
91413b8c354SEduardo Habkost         memmap[SIFIVE_U_DEV_L2CC].base, memmap[SIFIVE_U_DEV_L2CC].size);
915145b2991SBin Meng 
916145b2991SBin Meng     sysbus_realize(SYS_BUS_DEVICE(&s->spi0), errp);
917145b2991SBin Meng     sysbus_mmio_map(SYS_BUS_DEVICE(&s->spi0), 0,
918145b2991SBin Meng                     memmap[SIFIVE_U_DEV_QSPI0].base);
919145b2991SBin Meng     sysbus_connect_irq(SYS_BUS_DEVICE(&s->spi0), 0,
920145b2991SBin Meng                        qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_U_QSPI0_IRQ));
921722f1352SBin Meng     sysbus_realize(SYS_BUS_DEVICE(&s->spi2), errp);
922722f1352SBin Meng     sysbus_mmio_map(SYS_BUS_DEVICE(&s->spi2), 0,
923722f1352SBin Meng                     memmap[SIFIVE_U_DEV_QSPI2].base);
924722f1352SBin Meng     sysbus_connect_irq(SYS_BUS_DEVICE(&s->spi2), 0,
925722f1352SBin Meng                        qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_U_QSPI2_IRQ));
926a7240d1eSMichael Clark }
927a7240d1eSMichael Clark 
928139177b1SBin Meng static Property sifive_u_soc_props[] = {
929fda5b000SAlistair Francis     DEFINE_PROP_UINT32("serial", SiFiveUSoCState, serial, OTP_SERIAL),
930099be035SAlistair Francis     DEFINE_PROP_STRING("cpu-type", SiFiveUSoCState, cpu_type),
931fda5b000SAlistair Francis     DEFINE_PROP_END_OF_LIST()
932fda5b000SAlistair Francis };
933fda5b000SAlistair Francis 
934139177b1SBin Meng static void sifive_u_soc_class_init(ObjectClass *oc, void *data)
9352308092bSAlistair Francis {
9362308092bSAlistair Francis     DeviceClass *dc = DEVICE_CLASS(oc);
9372308092bSAlistair Francis 
938139177b1SBin Meng     device_class_set_props(dc, sifive_u_soc_props);
939139177b1SBin Meng     dc->realize = sifive_u_soc_realize;
9402308092bSAlistair Francis     /* Reason: Uses serial_hds in realize function, thus can't be used twice */
9412308092bSAlistair Francis     dc->user_creatable = false;
9422308092bSAlistair Francis }
9432308092bSAlistair Francis 
944139177b1SBin Meng static const TypeInfo sifive_u_soc_type_info = {
9452308092bSAlistair Francis     .name = TYPE_RISCV_U_SOC,
9462308092bSAlistair Francis     .parent = TYPE_DEVICE,
9472308092bSAlistair Francis     .instance_size = sizeof(SiFiveUSoCState),
948139177b1SBin Meng     .instance_init = sifive_u_soc_instance_init,
949139177b1SBin Meng     .class_init = sifive_u_soc_class_init,
9502308092bSAlistair Francis };
9512308092bSAlistair Francis 
952139177b1SBin Meng static void sifive_u_soc_register_types(void)
9532308092bSAlistair Francis {
954139177b1SBin Meng     type_register_static(&sifive_u_soc_type_info);
9552308092bSAlistair Francis }
9562308092bSAlistair Francis 
957139177b1SBin Meng type_init(sifive_u_soc_register_types)
958