xref: /openbmc/qemu/hw/riscv/sifive_u.c (revision 145b299139da92fb1b1048b393865bc96597d6b9)
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)
18*145b2991SBin Meng  * 8) SPI0 connected to an SPI flash
19a7240d1eSMichael Clark  *
20f3d47d58SBin Meng  * This board currently generates devicetree dynamically that indicates at least
21ecdfe393SBin Meng  * two harts and up to five harts.
22a7240d1eSMichael Clark  *
23a7240d1eSMichael Clark  * This program is free software; you can redistribute it and/or modify it
24a7240d1eSMichael Clark  * under the terms and conditions of the GNU General Public License,
25a7240d1eSMichael Clark  * version 2 or later, as published by the Free Software Foundation.
26a7240d1eSMichael Clark  *
27a7240d1eSMichael Clark  * This program is distributed in the hope it will be useful, but WITHOUT
28a7240d1eSMichael Clark  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
29a7240d1eSMichael Clark  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
30a7240d1eSMichael Clark  * more details.
31a7240d1eSMichael Clark  *
32a7240d1eSMichael Clark  * You should have received a copy of the GNU General Public License along with
33a7240d1eSMichael Clark  * this program.  If not, see <http://www.gnu.org/licenses/>.
34a7240d1eSMichael Clark  */
35a7240d1eSMichael Clark 
36a7240d1eSMichael Clark #include "qemu/osdep.h"
37a7240d1eSMichael Clark #include "qemu/log.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"
48*145b2991SBin 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 
65a7240d1eSMichael Clark static const struct MemmapEntry {
66a7240d1eSMichael Clark     hwaddr base;
67a7240d1eSMichael Clark     hwaddr size;
68a7240d1eSMichael Clark } 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 },
79*145b2991SBin Meng     [SIFIVE_U_DEV_QSPI0] =    { 0x10040000,     0x1000 },
8013b8c354SEduardo Habkost     [SIFIVE_U_DEV_GPIO] =     { 0x10060000,     0x1000 },
8113b8c354SEduardo Habkost     [SIFIVE_U_DEV_OTP] =      { 0x10070000,     0x1000 },
8213b8c354SEduardo Habkost     [SIFIVE_U_DEV_GEM] =      { 0x10090000,     0x2000 },
8313b8c354SEduardo Habkost     [SIFIVE_U_DEV_GEM_MGMT] = { 0x100a0000,     0x1000 },
8413b8c354SEduardo Habkost     [SIFIVE_U_DEV_DMC] =      { 0x100b0000,    0x10000 },
8513b8c354SEduardo Habkost     [SIFIVE_U_DEV_FLASH0] =   { 0x20000000, 0x10000000 },
8613b8c354SEduardo Habkost     [SIFIVE_U_DEV_DRAM] =     { 0x80000000,        0x0 },
87a7240d1eSMichael Clark };
88a7240d1eSMichael Clark 
895461c4feSBin Meng #define OTP_SERIAL          1
905a7f76a3SAlistair Francis #define GEM_REVISION        0x10070109
915a7f76a3SAlistair Francis 
929f79638eSBin Meng static void create_fdt(SiFiveUState *s, const struct MemmapEntry *memmap,
932206ffa6SAlistair Francis                        uint64_t mem_size, const char *cmdline, bool is_32_bit)
94a7240d1eSMichael Clark {
95ecdfe393SBin Meng     MachineState *ms = MACHINE(qdev_get_machine());
96a7240d1eSMichael Clark     void *fdt;
97a7240d1eSMichael Clark     int cpu;
98a7240d1eSMichael Clark     uint32_t *cells;
99a7240d1eSMichael Clark     char *nodename;
100806c64b7SBin Meng     char ethclk_names[] = "pclk\0hclk";
1015133ed17SBin Meng     uint32_t plic_phandle, prci_phandle, gpio_phandle, phandle = 1;
1027b6bb66fSBin Meng     uint32_t hfclk_phandle, rtcclk_phandle, phy_phandle;
103a7240d1eSMichael Clark 
104f2ce39b4SPaolo Bonzini     if (ms->dtb) {
105f2ce39b4SPaolo Bonzini         fdt = s->fdt = load_device_tree(ms->dtb, &s->fdt_size);
106d5c90cf3SAnup Patel         if (!fdt) {
107d5c90cf3SAnup Patel             error_report("load_device_tree() failed");
108d5c90cf3SAnup Patel             exit(1);
109d5c90cf3SAnup Patel         }
110d5c90cf3SAnup Patel         goto update_bootargs;
111d5c90cf3SAnup Patel     } else {
112a7240d1eSMichael Clark         fdt = s->fdt = create_device_tree(&s->fdt_size);
113a7240d1eSMichael Clark         if (!fdt) {
114a7240d1eSMichael Clark             error_report("create_device_tree() failed");
115a7240d1eSMichael Clark             exit(1);
116a7240d1eSMichael Clark         }
117d5c90cf3SAnup Patel     }
118a7240d1eSMichael Clark 
119d372e748SBin Meng     qemu_fdt_setprop_string(fdt, "/", "model", "SiFive HiFive Unleashed A00");
120d372e748SBin Meng     qemu_fdt_setprop_string(fdt, "/", "compatible",
121d372e748SBin Meng                             "sifive,hifive-unleashed-a00");
122a7240d1eSMichael Clark     qemu_fdt_setprop_cell(fdt, "/", "#size-cells", 0x2);
123a7240d1eSMichael Clark     qemu_fdt_setprop_cell(fdt, "/", "#address-cells", 0x2);
124a7240d1eSMichael Clark 
125a7240d1eSMichael Clark     qemu_fdt_add_subnode(fdt, "/soc");
126a7240d1eSMichael Clark     qemu_fdt_setprop(fdt, "/soc", "ranges", NULL, 0);
1272a1a6f6dSAlistair Francis     qemu_fdt_setprop_string(fdt, "/soc", "compatible", "simple-bus");
128a7240d1eSMichael Clark     qemu_fdt_setprop_cell(fdt, "/soc", "#size-cells", 0x2);
129a7240d1eSMichael Clark     qemu_fdt_setprop_cell(fdt, "/soc", "#address-cells", 0x2);
130a7240d1eSMichael Clark 
131e1724d09SBin Meng     hfclk_phandle = phandle++;
132e1724d09SBin Meng     nodename = g_strdup_printf("/hfclk");
133e1724d09SBin Meng     qemu_fdt_add_subnode(fdt, nodename);
134e1724d09SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "phandle", hfclk_phandle);
135e1724d09SBin Meng     qemu_fdt_setprop_string(fdt, nodename, "clock-output-names", "hfclk");
136e1724d09SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "clock-frequency",
137e1724d09SBin Meng         SIFIVE_U_HFCLK_FREQ);
138e1724d09SBin Meng     qemu_fdt_setprop_string(fdt, nodename, "compatible", "fixed-clock");
139e1724d09SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "#clock-cells", 0x0);
140e1724d09SBin Meng     g_free(nodename);
141e1724d09SBin Meng 
142e1724d09SBin Meng     rtcclk_phandle = phandle++;
143e1724d09SBin Meng     nodename = g_strdup_printf("/rtcclk");
144e1724d09SBin Meng     qemu_fdt_add_subnode(fdt, nodename);
145e1724d09SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "phandle", rtcclk_phandle);
146e1724d09SBin Meng     qemu_fdt_setprop_string(fdt, nodename, "clock-output-names", "rtcclk");
147e1724d09SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "clock-frequency",
148e1724d09SBin Meng         SIFIVE_U_RTCCLK_FREQ);
149e1724d09SBin Meng     qemu_fdt_setprop_string(fdt, nodename, "compatible", "fixed-clock");
150e1724d09SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "#clock-cells", 0x0);
151e1724d09SBin Meng     g_free(nodename);
152e1724d09SBin Meng 
153a7240d1eSMichael Clark     nodename = g_strdup_printf("/memory@%lx",
15413b8c354SEduardo Habkost         (long)memmap[SIFIVE_U_DEV_DRAM].base);
155a7240d1eSMichael Clark     qemu_fdt_add_subnode(fdt, nodename);
156a7240d1eSMichael Clark     qemu_fdt_setprop_cells(fdt, nodename, "reg",
15713b8c354SEduardo Habkost         memmap[SIFIVE_U_DEV_DRAM].base >> 32, memmap[SIFIVE_U_DEV_DRAM].base,
158a7240d1eSMichael Clark         mem_size >> 32, mem_size);
159a7240d1eSMichael Clark     qemu_fdt_setprop_string(fdt, nodename, "device_type", "memory");
160a7240d1eSMichael Clark     g_free(nodename);
161a7240d1eSMichael Clark 
162a7240d1eSMichael Clark     qemu_fdt_add_subnode(fdt, "/cpus");
1632a8756edSMichael Clark     qemu_fdt_setprop_cell(fdt, "/cpus", "timebase-frequency",
1642a8756edSMichael Clark         SIFIVE_CLINT_TIMEBASE_FREQ);
165a7240d1eSMichael Clark     qemu_fdt_setprop_cell(fdt, "/cpus", "#size-cells", 0x0);
166a7240d1eSMichael Clark     qemu_fdt_setprop_cell(fdt, "/cpus", "#address-cells", 0x1);
167a7240d1eSMichael Clark 
168ecdfe393SBin Meng     for (cpu = ms->smp.cpus - 1; cpu >= 0; cpu--) {
169382cb439SBin Meng         int cpu_phandle = phandle++;
170a7240d1eSMichael Clark         nodename = g_strdup_printf("/cpus/cpu@%d", cpu);
171a7240d1eSMichael Clark         char *intc = g_strdup_printf("/cpus/cpu@%d/interrupt-controller", cpu);
172ecdfe393SBin Meng         char *isa;
173a7240d1eSMichael Clark         qemu_fdt_add_subnode(fdt, nodename);
174ecdfe393SBin Meng         /* cpu 0 is the management hart that does not have mmu */
175ecdfe393SBin Meng         if (cpu != 0) {
1762206ffa6SAlistair Francis             if (is_32_bit) {
177e883e992SBin Meng                 qemu_fdt_setprop_string(fdt, nodename, "mmu-type", "riscv,sv32");
1782206ffa6SAlistair Francis             } else {
179a7240d1eSMichael Clark                 qemu_fdt_setprop_string(fdt, nodename, "mmu-type", "riscv,sv48");
1802206ffa6SAlistair Francis             }
181ecdfe393SBin Meng             isa = riscv_isa_string(&s->soc.u_cpus.harts[cpu - 1]);
182ecdfe393SBin Meng         } else {
183ecdfe393SBin Meng             isa = riscv_isa_string(&s->soc.e_cpus.harts[0]);
184ecdfe393SBin Meng         }
185a7240d1eSMichael Clark         qemu_fdt_setprop_string(fdt, nodename, "riscv,isa", isa);
186a7240d1eSMichael Clark         qemu_fdt_setprop_string(fdt, nodename, "compatible", "riscv");
187a7240d1eSMichael Clark         qemu_fdt_setprop_string(fdt, nodename, "status", "okay");
188a7240d1eSMichael Clark         qemu_fdt_setprop_cell(fdt, nodename, "reg", cpu);
189a7240d1eSMichael Clark         qemu_fdt_setprop_string(fdt, nodename, "device_type", "cpu");
190a7240d1eSMichael Clark         qemu_fdt_add_subnode(fdt, intc);
191382cb439SBin Meng         qemu_fdt_setprop_cell(fdt, intc, "phandle", cpu_phandle);
192a7240d1eSMichael Clark         qemu_fdt_setprop_string(fdt, intc, "compatible", "riscv,cpu-intc");
193a7240d1eSMichael Clark         qemu_fdt_setprop(fdt, intc, "interrupt-controller", NULL, 0);
194a7240d1eSMichael Clark         qemu_fdt_setprop_cell(fdt, intc, "#interrupt-cells", 1);
195a7240d1eSMichael Clark         g_free(isa);
196a7240d1eSMichael Clark         g_free(intc);
197a7240d1eSMichael Clark         g_free(nodename);
198a7240d1eSMichael Clark     }
199a7240d1eSMichael Clark 
200ecdfe393SBin Meng     cells =  g_new0(uint32_t, ms->smp.cpus * 4);
201ecdfe393SBin Meng     for (cpu = 0; cpu < ms->smp.cpus; cpu++) {
202a7240d1eSMichael Clark         nodename =
203a7240d1eSMichael Clark             g_strdup_printf("/cpus/cpu@%d/interrupt-controller", cpu);
204a7240d1eSMichael Clark         uint32_t intc_phandle = qemu_fdt_get_phandle(fdt, nodename);
205a7240d1eSMichael Clark         cells[cpu * 4 + 0] = cpu_to_be32(intc_phandle);
206a7240d1eSMichael Clark         cells[cpu * 4 + 1] = cpu_to_be32(IRQ_M_SOFT);
207a7240d1eSMichael Clark         cells[cpu * 4 + 2] = cpu_to_be32(intc_phandle);
208a7240d1eSMichael Clark         cells[cpu * 4 + 3] = cpu_to_be32(IRQ_M_TIMER);
209a7240d1eSMichael Clark         g_free(nodename);
210a7240d1eSMichael Clark     }
211a7240d1eSMichael Clark     nodename = g_strdup_printf("/soc/clint@%lx",
21213b8c354SEduardo Habkost         (long)memmap[SIFIVE_U_DEV_CLINT].base);
213a7240d1eSMichael Clark     qemu_fdt_add_subnode(fdt, nodename);
214a7240d1eSMichael Clark     qemu_fdt_setprop_string(fdt, nodename, "compatible", "riscv,clint0");
215a7240d1eSMichael Clark     qemu_fdt_setprop_cells(fdt, nodename, "reg",
21613b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_CLINT].base,
21713b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_CLINT].size);
218a7240d1eSMichael Clark     qemu_fdt_setprop(fdt, nodename, "interrupts-extended",
219ecdfe393SBin Meng         cells, ms->smp.cpus * sizeof(uint32_t) * 4);
220a7240d1eSMichael Clark     g_free(cells);
221a7240d1eSMichael Clark     g_free(nodename);
222a7240d1eSMichael Clark 
223ea85f27dSBin Meng     nodename = g_strdup_printf("/soc/otp@%lx",
22413b8c354SEduardo Habkost         (long)memmap[SIFIVE_U_DEV_OTP].base);
225ea85f27dSBin Meng     qemu_fdt_add_subnode(fdt, nodename);
226ea85f27dSBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "fuse-count", SIFIVE_U_OTP_REG_SIZE);
227ea85f27dSBin Meng     qemu_fdt_setprop_cells(fdt, nodename, "reg",
22813b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_OTP].base,
22913b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_OTP].size);
230ea85f27dSBin Meng     qemu_fdt_setprop_string(fdt, nodename, "compatible",
231ea85f27dSBin Meng         "sifive,fu540-c000-otp");
232ea85f27dSBin Meng     g_free(nodename);
233ea85f27dSBin Meng 
234af14c840SBin Meng     prci_phandle = phandle++;
235af14c840SBin Meng     nodename = g_strdup_printf("/soc/clock-controller@%lx",
23613b8c354SEduardo Habkost         (long)memmap[SIFIVE_U_DEV_PRCI].base);
237af14c840SBin Meng     qemu_fdt_add_subnode(fdt, nodename);
238af14c840SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "phandle", prci_phandle);
239af14c840SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "#clock-cells", 0x1);
240af14c840SBin Meng     qemu_fdt_setprop_cells(fdt, nodename, "clocks",
241af14c840SBin Meng         hfclk_phandle, rtcclk_phandle);
242af14c840SBin Meng     qemu_fdt_setprop_cells(fdt, nodename, "reg",
24313b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_PRCI].base,
24413b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_PRCI].size);
245af14c840SBin Meng     qemu_fdt_setprop_string(fdt, nodename, "compatible",
246af14c840SBin Meng         "sifive,fu540-c000-prci");
247af14c840SBin Meng     g_free(nodename);
248af14c840SBin Meng 
249382cb439SBin Meng     plic_phandle = phandle++;
250ecdfe393SBin Meng     cells =  g_new0(uint32_t, ms->smp.cpus * 4 - 2);
251ecdfe393SBin Meng     for (cpu = 0; cpu < ms->smp.cpus; cpu++) {
252a7240d1eSMichael Clark         nodename =
253a7240d1eSMichael Clark             g_strdup_printf("/cpus/cpu@%d/interrupt-controller", cpu);
254a7240d1eSMichael Clark         uint32_t intc_phandle = qemu_fdt_get_phandle(fdt, nodename);
255ecdfe393SBin Meng         /* cpu 0 is the management hart that does not have S-mode */
256ecdfe393SBin Meng         if (cpu == 0) {
257ecdfe393SBin Meng             cells[0] = cpu_to_be32(intc_phandle);
258ecdfe393SBin Meng             cells[1] = cpu_to_be32(IRQ_M_EXT);
259ecdfe393SBin Meng         } else {
260ecdfe393SBin Meng             cells[cpu * 4 - 2] = cpu_to_be32(intc_phandle);
261ecdfe393SBin Meng             cells[cpu * 4 - 1] = cpu_to_be32(IRQ_M_EXT);
262a7240d1eSMichael Clark             cells[cpu * 4 + 0] = cpu_to_be32(intc_phandle);
263ecdfe393SBin Meng             cells[cpu * 4 + 1] = cpu_to_be32(IRQ_S_EXT);
264ecdfe393SBin Meng         }
265a7240d1eSMichael Clark         g_free(nodename);
266a7240d1eSMichael Clark     }
267a7240d1eSMichael Clark     nodename = g_strdup_printf("/soc/interrupt-controller@%lx",
26813b8c354SEduardo Habkost         (long)memmap[SIFIVE_U_DEV_PLIC].base);
269a7240d1eSMichael Clark     qemu_fdt_add_subnode(fdt, nodename);
270a7240d1eSMichael Clark     qemu_fdt_setprop_cell(fdt, nodename, "#interrupt-cells", 1);
271a7240d1eSMichael Clark     qemu_fdt_setprop_string(fdt, nodename, "compatible", "riscv,plic0");
272a7240d1eSMichael Clark     qemu_fdt_setprop(fdt, nodename, "interrupt-controller", NULL, 0);
273a7240d1eSMichael Clark     qemu_fdt_setprop(fdt, nodename, "interrupts-extended",
274ecdfe393SBin Meng         cells, (ms->smp.cpus * 4 - 2) * sizeof(uint32_t));
275a7240d1eSMichael Clark     qemu_fdt_setprop_cells(fdt, nodename, "reg",
27613b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_PLIC].base,
27713b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_PLIC].size);
27898ceee7fSAlistair Francis     qemu_fdt_setprop_cell(fdt, nodename, "riscv,ndev", 0x35);
27904e7edd1SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "phandle", plic_phandle);
280a7240d1eSMichael Clark     plic_phandle = qemu_fdt_get_phandle(fdt, nodename);
281a7240d1eSMichael Clark     g_free(cells);
282a7240d1eSMichael Clark     g_free(nodename);
283a7240d1eSMichael Clark 
2845133ed17SBin Meng     gpio_phandle = phandle++;
2858a88b9f5SBin Meng     nodename = g_strdup_printf("/soc/gpio@%lx",
28613b8c354SEduardo Habkost         (long)memmap[SIFIVE_U_DEV_GPIO].base);
2878a88b9f5SBin Meng     qemu_fdt_add_subnode(fdt, nodename);
2885133ed17SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "phandle", gpio_phandle);
2898a88b9f5SBin Meng     qemu_fdt_setprop_cells(fdt, nodename, "clocks",
2908a88b9f5SBin Meng         prci_phandle, PRCI_CLK_TLCLK);
2918a88b9f5SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "#interrupt-cells", 2);
2928a88b9f5SBin Meng     qemu_fdt_setprop(fdt, nodename, "interrupt-controller", NULL, 0);
2938a88b9f5SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "#gpio-cells", 2);
2948a88b9f5SBin Meng     qemu_fdt_setprop(fdt, nodename, "gpio-controller", NULL, 0);
2958a88b9f5SBin Meng     qemu_fdt_setprop_cells(fdt, nodename, "reg",
29613b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_GPIO].base,
29713b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_GPIO].size);
2988a88b9f5SBin Meng     qemu_fdt_setprop_cells(fdt, nodename, "interrupts", SIFIVE_U_GPIO_IRQ0,
2998a88b9f5SBin Meng         SIFIVE_U_GPIO_IRQ1, SIFIVE_U_GPIO_IRQ2, SIFIVE_U_GPIO_IRQ3,
3008a88b9f5SBin Meng         SIFIVE_U_GPIO_IRQ4, SIFIVE_U_GPIO_IRQ5, SIFIVE_U_GPIO_IRQ6,
3018a88b9f5SBin Meng         SIFIVE_U_GPIO_IRQ7, SIFIVE_U_GPIO_IRQ8, SIFIVE_U_GPIO_IRQ9,
3028a88b9f5SBin Meng         SIFIVE_U_GPIO_IRQ10, SIFIVE_U_GPIO_IRQ11, SIFIVE_U_GPIO_IRQ12,
3038a88b9f5SBin Meng         SIFIVE_U_GPIO_IRQ13, SIFIVE_U_GPIO_IRQ14, SIFIVE_U_GPIO_IRQ15);
3048a88b9f5SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle);
3058a88b9f5SBin Meng     qemu_fdt_setprop_string(fdt, nodename, "compatible", "sifive,gpio0");
3068a88b9f5SBin Meng     g_free(nodename);
3078a88b9f5SBin Meng 
3085133ed17SBin Meng     nodename = g_strdup_printf("/gpio-restart");
3095133ed17SBin Meng     qemu_fdt_add_subnode(fdt, nodename);
3105133ed17SBin Meng     qemu_fdt_setprop_cells(fdt, nodename, "gpios", gpio_phandle, 10, 1);
3115133ed17SBin Meng     qemu_fdt_setprop_string(fdt, nodename, "compatible", "gpio-restart");
3125133ed17SBin Meng     g_free(nodename);
3135133ed17SBin Meng 
314834e027aSBin Meng     nodename = g_strdup_printf("/soc/dma@%lx",
31513b8c354SEduardo Habkost         (long)memmap[SIFIVE_U_DEV_PDMA].base);
316834e027aSBin Meng     qemu_fdt_add_subnode(fdt, nodename);
317834e027aSBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "#dma-cells", 1);
318834e027aSBin Meng     qemu_fdt_setprop_cells(fdt, nodename, "interrupts",
319834e027aSBin Meng         SIFIVE_U_PDMA_IRQ0, SIFIVE_U_PDMA_IRQ1, SIFIVE_U_PDMA_IRQ2,
320834e027aSBin Meng         SIFIVE_U_PDMA_IRQ3, SIFIVE_U_PDMA_IRQ4, SIFIVE_U_PDMA_IRQ5,
321834e027aSBin Meng         SIFIVE_U_PDMA_IRQ6, SIFIVE_U_PDMA_IRQ7);
322834e027aSBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle);
323834e027aSBin Meng     qemu_fdt_setprop_cells(fdt, nodename, "reg",
32413b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_PDMA].base,
32513b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_PDMA].size);
326834e027aSBin Meng     qemu_fdt_setprop_string(fdt, nodename, "compatible",
327834e027aSBin Meng                             "sifive,fu540-c000-pdma");
328834e027aSBin Meng     g_free(nodename);
329834e027aSBin Meng 
3306eaf9cf5SBin Meng     nodename = g_strdup_printf("/soc/cache-controller@%lx",
33113b8c354SEduardo Habkost         (long)memmap[SIFIVE_U_DEV_L2CC].base);
3326eaf9cf5SBin Meng     qemu_fdt_add_subnode(fdt, nodename);
3336eaf9cf5SBin Meng     qemu_fdt_setprop_cells(fdt, nodename, "reg",
33413b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_L2CC].base,
33513b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_L2CC].size);
3366eaf9cf5SBin Meng     qemu_fdt_setprop_cells(fdt, nodename, "interrupts",
3376eaf9cf5SBin Meng         SIFIVE_U_L2CC_IRQ0, SIFIVE_U_L2CC_IRQ1, SIFIVE_U_L2CC_IRQ2);
3386eaf9cf5SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle);
3396eaf9cf5SBin Meng     qemu_fdt_setprop(fdt, nodename, "cache-unified", NULL, 0);
3406eaf9cf5SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "cache-size", 2097152);
3416eaf9cf5SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "cache-sets", 1024);
3426eaf9cf5SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "cache-level", 2);
3436eaf9cf5SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "cache-block-size", 64);
3446eaf9cf5SBin Meng     qemu_fdt_setprop_string(fdt, nodename, "compatible",
3456eaf9cf5SBin Meng                             "sifive,fu540-c000-ccache");
3466eaf9cf5SBin Meng     g_free(nodename);
3476eaf9cf5SBin Meng 
348*145b2991SBin Meng     nodename = g_strdup_printf("/soc/spi@%lx",
349*145b2991SBin Meng         (long)memmap[SIFIVE_U_DEV_QSPI0].base);
350*145b2991SBin Meng     qemu_fdt_add_subnode(fdt, nodename);
351*145b2991SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "#size-cells", 0);
352*145b2991SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "#address-cells", 1);
353*145b2991SBin Meng     qemu_fdt_setprop_cells(fdt, nodename, "clocks",
354*145b2991SBin Meng         prci_phandle, PRCI_CLK_TLCLK);
355*145b2991SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "interrupts", SIFIVE_U_QSPI0_IRQ);
356*145b2991SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle);
357*145b2991SBin Meng     qemu_fdt_setprop_cells(fdt, nodename, "reg",
358*145b2991SBin Meng         0x0, memmap[SIFIVE_U_DEV_QSPI0].base,
359*145b2991SBin Meng         0x0, memmap[SIFIVE_U_DEV_QSPI0].size);
360*145b2991SBin Meng     qemu_fdt_setprop_string(fdt, nodename, "compatible", "sifive,spi0");
361*145b2991SBin Meng     g_free(nodename);
362*145b2991SBin Meng 
363*145b2991SBin Meng     nodename = g_strdup_printf("/soc/spi@%lx/flash@0",
364*145b2991SBin Meng         (long)memmap[SIFIVE_U_DEV_QSPI0].base);
365*145b2991SBin Meng     qemu_fdt_add_subnode(fdt, nodename);
366*145b2991SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "spi-rx-bus-width", 4);
367*145b2991SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "spi-tx-bus-width", 4);
368*145b2991SBin Meng     qemu_fdt_setprop(fdt, nodename, "m25p,fast-read", NULL, 0);
369*145b2991SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "spi-max-frequency", 50000000);
370*145b2991SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "reg", 0);
371*145b2991SBin Meng     qemu_fdt_setprop_string(fdt, nodename, "compatible", "jedec,spi-nor");
372*145b2991SBin Meng     g_free(nodename);
373*145b2991SBin Meng 
3747b6bb66fSBin Meng     phy_phandle = phandle++;
3755a7f76a3SAlistair Francis     nodename = g_strdup_printf("/soc/ethernet@%lx",
37613b8c354SEduardo Habkost         (long)memmap[SIFIVE_U_DEV_GEM].base);
3775a7f76a3SAlistair Francis     qemu_fdt_add_subnode(fdt, nodename);
3787b6bb66fSBin Meng     qemu_fdt_setprop_string(fdt, nodename, "compatible",
3797b6bb66fSBin Meng         "sifive,fu540-c000-gem");
3805a7f76a3SAlistair Francis     qemu_fdt_setprop_cells(fdt, nodename, "reg",
38113b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_GEM].base,
38213b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_GEM].size,
38313b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_GEM_MGMT].base,
38413b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_GEM_MGMT].size);
3855a7f76a3SAlistair Francis     qemu_fdt_setprop_string(fdt, nodename, "reg-names", "control");
3865a7f76a3SAlistair Francis     qemu_fdt_setprop_string(fdt, nodename, "phy-mode", "gmii");
3877b6bb66fSBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "phy-handle", phy_phandle);
38804e7edd1SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle);
38904e7edd1SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "interrupts", SIFIVE_U_GEM_IRQ);
390fe93582cSAnup Patel     qemu_fdt_setprop_cells(fdt, nodename, "clocks",
391806c64b7SBin Meng         prci_phandle, PRCI_CLK_GEMGXLPLL, prci_phandle, PRCI_CLK_GEMGXLPLL);
39204ece4f8SGuenter Roeck     qemu_fdt_setprop(fdt, nodename, "clock-names", ethclk_names,
393fe93582cSAnup Patel         sizeof(ethclk_names));
3947b6bb66fSBin Meng     qemu_fdt_setprop(fdt, nodename, "local-mac-address",
3957b6bb66fSBin Meng         s->soc.gem.conf.macaddr.a, ETH_ALEN);
39604e7edd1SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "#address-cells", 1);
39704e7edd1SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "#size-cells", 0);
398c3a28b5dSBin Meng 
399c3a28b5dSBin Meng     qemu_fdt_add_subnode(fdt, "/aliases");
400c3a28b5dSBin Meng     qemu_fdt_setprop_string(fdt, "/aliases", "ethernet0", nodename);
401c3a28b5dSBin Meng 
4025a7f76a3SAlistair Francis     g_free(nodename);
4035a7f76a3SAlistair Francis 
4045a7f76a3SAlistair Francis     nodename = g_strdup_printf("/soc/ethernet@%lx/ethernet-phy@0",
40513b8c354SEduardo Habkost         (long)memmap[SIFIVE_U_DEV_GEM].base);
4065a7f76a3SAlistair Francis     qemu_fdt_add_subnode(fdt, nodename);
4077b6bb66fSBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "phandle", phy_phandle);
40804e7edd1SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "reg", 0x0);
4095a7f76a3SAlistair Francis     g_free(nodename);
4105a7f76a3SAlistair Francis 
4115f7134d3SBin Meng     nodename = g_strdup_printf("/soc/serial@%lx",
41210b43754SAnup Patel         (long)memmap[SIFIVE_U_DEV_UART1].base);
41310b43754SAnup Patel     qemu_fdt_add_subnode(fdt, nodename);
41410b43754SAnup Patel     qemu_fdt_setprop_string(fdt, nodename, "compatible", "sifive,uart0");
41510b43754SAnup Patel     qemu_fdt_setprop_cells(fdt, nodename, "reg",
41610b43754SAnup Patel         0x0, memmap[SIFIVE_U_DEV_UART1].base,
41710b43754SAnup Patel         0x0, memmap[SIFIVE_U_DEV_UART1].size);
41810b43754SAnup Patel     qemu_fdt_setprop_cells(fdt, nodename, "clocks",
41910b43754SAnup Patel         prci_phandle, PRCI_CLK_TLCLK);
42010b43754SAnup Patel     qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle);
42110b43754SAnup Patel     qemu_fdt_setprop_cell(fdt, nodename, "interrupts", SIFIVE_U_UART1_IRQ);
42210b43754SAnup Patel 
42310b43754SAnup Patel     qemu_fdt_setprop_string(fdt, "/aliases", "serial1", nodename);
42410b43754SAnup Patel     g_free(nodename);
42510b43754SAnup Patel 
42610b43754SAnup Patel     nodename = g_strdup_printf("/soc/serial@%lx",
42713b8c354SEduardo Habkost         (long)memmap[SIFIVE_U_DEV_UART0].base);
428a7240d1eSMichael Clark     qemu_fdt_add_subnode(fdt, nodename);
429a7240d1eSMichael Clark     qemu_fdt_setprop_string(fdt, nodename, "compatible", "sifive,uart0");
430a7240d1eSMichael Clark     qemu_fdt_setprop_cells(fdt, nodename, "reg",
43113b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_UART0].base,
43213b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_UART0].size);
433806c64b7SBin Meng     qemu_fdt_setprop_cells(fdt, nodename, "clocks",
434806c64b7SBin Meng         prci_phandle, PRCI_CLK_TLCLK);
43504e7edd1SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle);
43604e7edd1SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "interrupts", SIFIVE_U_UART0_IRQ);
437a7240d1eSMichael Clark 
438a7240d1eSMichael Clark     qemu_fdt_add_subnode(fdt, "/chosen");
439a7240d1eSMichael Clark     qemu_fdt_setprop_string(fdt, "/chosen", "stdout-path", nodename);
44044e6dcd3SGuenter Roeck     qemu_fdt_setprop_string(fdt, "/aliases", "serial0", nodename);
44144e6dcd3SGuenter Roeck 
442a7240d1eSMichael Clark     g_free(nodename);
443d5c90cf3SAnup Patel 
444d5c90cf3SAnup Patel update_bootargs:
445d5c90cf3SAnup Patel     if (cmdline) {
446d5c90cf3SAnup Patel         qemu_fdt_setprop_string(fdt, "/chosen", "bootargs", cmdline);
447d5c90cf3SAnup Patel     }
448a7240d1eSMichael Clark }
449a7240d1eSMichael Clark 
4505133ed17SBin Meng static void sifive_u_machine_reset(void *opaque, int n, int level)
4515133ed17SBin Meng {
4525133ed17SBin Meng     /* gpio pin active low triggers reset */
4535133ed17SBin Meng     if (!level) {
4545133ed17SBin Meng         qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
4555133ed17SBin Meng     }
4565133ed17SBin Meng }
4575133ed17SBin Meng 
458523e3464SAlistair Francis static void sifive_u_machine_init(MachineState *machine)
459a7240d1eSMichael Clark {
460a7240d1eSMichael Clark     const struct MemmapEntry *memmap = sifive_u_memmap;
461687caef1SAlistair Francis     SiFiveUState *s = RISCV_U_MACHINE(machine);
4625aec3247SMichael Clark     MemoryRegion *system_memory = get_system_memory();
463a7240d1eSMichael Clark     MemoryRegion *main_mem = g_new(MemoryRegion, 1);
4641b3a2308SAlistair Francis     MemoryRegion *flash0 = g_new(MemoryRegion, 1);
46513b8c354SEduardo Habkost     target_ulong start_addr = memmap[SIFIVE_U_DEV_DRAM].base;
46638bc4e34SAlistair Francis     target_ulong firmware_end_addr, kernel_start_addr;
4678590f536SAtish Patra     uint32_t start_addr_hi32 = 0x00000000;
4685aec3247SMichael Clark     int i;
46966b1205bSAtish Patra     uint32_t fdt_load_addr;
470dc144fe1SAtish Patra     uint64_t kernel_entry;
471*145b2991SBin Meng     DriveInfo *dinfo;
472*145b2991SBin Meng     DeviceState *flash_dev;
473*145b2991SBin Meng     qemu_irq flash_cs;
474a7240d1eSMichael Clark 
4752308092bSAlistair Francis     /* Initialize SoC */
4769fc7fc4dSMarkus Armbruster     object_initialize_child(OBJECT(machine), "soc", &s->soc, TYPE_RISCV_U_SOC);
4775325cc34SMarkus Armbruster     object_property_set_uint(OBJECT(&s->soc), "serial", s->serial,
4783ca109c3SBin Meng                              &error_abort);
479099be035SAlistair Francis     object_property_set_str(OBJECT(&s->soc), "cpu-type", machine->cpu_type,
480099be035SAlistair Francis                              &error_abort);
481ce189ab2SMarkus Armbruster     qdev_realize(DEVICE(&s->soc), NULL, &error_abort);
482a7240d1eSMichael Clark 
483a7240d1eSMichael Clark     /* register RAM */
484a7240d1eSMichael Clark     memory_region_init_ram(main_mem, NULL, "riscv.sifive.u.ram",
485a7240d1eSMichael Clark                            machine->ram_size, &error_fatal);
48613b8c354SEduardo Habkost     memory_region_add_subregion(system_memory, memmap[SIFIVE_U_DEV_DRAM].base,
487a7240d1eSMichael Clark                                 main_mem);
488a7240d1eSMichael Clark 
4891b3a2308SAlistair Francis     /* register QSPI0 Flash */
4901b3a2308SAlistair Francis     memory_region_init_ram(flash0, NULL, "riscv.sifive.u.flash0",
49113b8c354SEduardo Habkost                            memmap[SIFIVE_U_DEV_FLASH0].size, &error_fatal);
49213b8c354SEduardo Habkost     memory_region_add_subregion(system_memory, memmap[SIFIVE_U_DEV_FLASH0].base,
4931b3a2308SAlistair Francis                                 flash0);
4941b3a2308SAlistair Francis 
4955133ed17SBin Meng     /* register gpio-restart */
4965133ed17SBin Meng     qdev_connect_gpio_out(DEVICE(&(s->soc.gpio)), 10,
4975133ed17SBin Meng                           qemu_allocate_irq(sifive_u_machine_reset, NULL, 0));
4985133ed17SBin Meng 
499a7240d1eSMichael Clark     /* create device tree */
5002206ffa6SAlistair Francis     create_fdt(s, memmap, machine->ram_size, machine->kernel_cmdline,
501a8259b53SAlistair Francis                riscv_is_32bit(&s->soc.u_cpus));
502a7240d1eSMichael Clark 
50317aad9f2SBin Meng     if (s->start_in_flash) {
50417aad9f2SBin Meng         /*
50517aad9f2SBin Meng          * If start_in_flash property is given, assign s->msel to a value
50617aad9f2SBin Meng          * that representing booting from QSPI0 memory-mapped flash.
50717aad9f2SBin Meng          *
50817aad9f2SBin Meng          * This also means that when both start_in_flash and msel properties
50917aad9f2SBin Meng          * are given, start_in_flash takes the precedence over msel.
51017aad9f2SBin Meng          *
51117aad9f2SBin Meng          * Note this is to keep backward compatibility not to break existing
51217aad9f2SBin Meng          * users that use start_in_flash property.
51317aad9f2SBin Meng          */
51417aad9f2SBin Meng         s->msel = MSEL_MEMMAP_QSPI0_FLASH;
51517aad9f2SBin Meng     }
51617aad9f2SBin Meng 
51717aad9f2SBin Meng     switch (s->msel) {
51817aad9f2SBin Meng     case MSEL_MEMMAP_QSPI0_FLASH:
51913b8c354SEduardo Habkost         start_addr = memmap[SIFIVE_U_DEV_FLASH0].base;
52017aad9f2SBin Meng         break;
52117aad9f2SBin Meng     case MSEL_L2LIM_QSPI0_FLASH:
52217aad9f2SBin Meng     case MSEL_L2LIM_QSPI2_SD:
52313b8c354SEduardo Habkost         start_addr = memmap[SIFIVE_U_DEV_L2LIM].base;
52417aad9f2SBin Meng         break;
52517aad9f2SBin Meng     default:
52613b8c354SEduardo Habkost         start_addr = memmap[SIFIVE_U_DEV_DRAM].base;
52717aad9f2SBin Meng         break;
52817aad9f2SBin Meng     }
52917aad9f2SBin Meng 
530a8259b53SAlistair Francis     if (riscv_is_32bit(&s->soc.u_cpus)) {
5312206ffa6SAlistair Francis         firmware_end_addr = riscv_find_and_load_firmware(machine,
5322206ffa6SAlistair Francis                                     "opensbi-riscv32-generic-fw_dynamic.bin",
53338bc4e34SAlistair Francis                                     start_addr, NULL);
5342206ffa6SAlistair Francis     } else {
5352206ffa6SAlistair Francis         firmware_end_addr = riscv_find_and_load_firmware(machine,
5362206ffa6SAlistair Francis                                     "opensbi-riscv64-generic-fw_dynamic.bin",
5372206ffa6SAlistair Francis                                     start_addr, NULL);
5382206ffa6SAlistair Francis     }
539b3042223SAlistair Francis 
540a7240d1eSMichael Clark     if (machine->kernel_filename) {
541a8259b53SAlistair Francis         kernel_start_addr = riscv_calc_kernel_start_addr(&s->soc.u_cpus,
54238bc4e34SAlistair Francis                                                          firmware_end_addr);
54338bc4e34SAlistair Francis 
54438bc4e34SAlistair Francis         kernel_entry = riscv_load_kernel(machine->kernel_filename,
54538bc4e34SAlistair Francis                                          kernel_start_addr, NULL);
5460f8d4462SGuenter Roeck 
5470f8d4462SGuenter Roeck         if (machine->initrd_filename) {
5480f8d4462SGuenter Roeck             hwaddr start;
5490f8d4462SGuenter Roeck             hwaddr end = riscv_load_initrd(machine->initrd_filename,
5500f8d4462SGuenter Roeck                                            machine->ram_size, kernel_entry,
5510f8d4462SGuenter Roeck                                            &start);
5529f79638eSBin Meng             qemu_fdt_setprop_cell(s->fdt, "/chosen",
5530f8d4462SGuenter Roeck                                   "linux,initrd-start", start);
5549f79638eSBin Meng             qemu_fdt_setprop_cell(s->fdt, "/chosen", "linux,initrd-end",
5550f8d4462SGuenter Roeck                                   end);
5560f8d4462SGuenter Roeck         }
557dc144fe1SAtish Patra     } else {
558dc144fe1SAtish Patra        /*
559dc144fe1SAtish Patra         * If dynamic firmware is used, it doesn't know where is the next mode
560dc144fe1SAtish Patra         * if kernel argument is not set.
561dc144fe1SAtish Patra         */
562dc144fe1SAtish Patra         kernel_entry = 0;
563a7240d1eSMichael Clark     }
564a7240d1eSMichael Clark 
56566b1205bSAtish Patra     /* Compute the fdt load address in dram */
56613b8c354SEduardo Habkost     fdt_load_addr = riscv_load_fdt(memmap[SIFIVE_U_DEV_DRAM].base,
56766b1205bSAtish Patra                                    machine->ram_size, s->fdt);
568a8259b53SAlistair Francis     if (!riscv_is_32bit(&s->soc.u_cpus)) {
5692206ffa6SAlistair Francis         start_addr_hi32 = (uint64_t)start_addr >> 32;
5702206ffa6SAlistair Francis     }
57166b1205bSAtish Patra 
572a7240d1eSMichael Clark     /* reset vector */
57366b1205bSAtish Patra     uint32_t reset_vec[11] = {
57417aad9f2SBin Meng         s->msel,                       /* MSEL pin state */
575dc144fe1SAtish Patra         0x00000297,                    /* 1:  auipc  t0, %pcrel_hi(fw_dyn) */
576dc144fe1SAtish Patra         0x02828613,                    /*     addi   a2, t0, %pcrel_lo(1b) */
577a7240d1eSMichael Clark         0xf1402573,                    /*     csrr   a0, mhartid  */
5782206ffa6SAlistair Francis         0,
5792206ffa6SAlistair Francis         0,
580a7240d1eSMichael Clark         0x00028067,                    /*     jr     t0 */
581fc41ae23SAlistair Francis         start_addr,                    /* start: .dword */
5828590f536SAtish Patra         start_addr_hi32,
58366b1205bSAtish Patra         fdt_load_addr,                 /* fdt_laddr: .dword */
58466b1205bSAtish Patra         0x00000000,
585dc144fe1SAtish Patra                                        /* fw_dyn: */
586a7240d1eSMichael Clark     };
587a8259b53SAlistair Francis     if (riscv_is_32bit(&s->soc.u_cpus)) {
5882206ffa6SAlistair Francis         reset_vec[4] = 0x0202a583;     /*     lw     a1, 32(t0) */
5892206ffa6SAlistair Francis         reset_vec[5] = 0x0182a283;     /*     lw     t0, 24(t0) */
5902206ffa6SAlistair Francis     } else {
5912206ffa6SAlistair Francis         reset_vec[4] = 0x0202b583;     /*     ld     a1, 32(t0) */
5922206ffa6SAlistair Francis         reset_vec[5] = 0x0182b283;     /*     ld     t0, 24(t0) */
5932206ffa6SAlistair Francis     }
5942206ffa6SAlistair Francis 
595a7240d1eSMichael Clark 
5965aec3247SMichael Clark     /* copy in the reset vector in little_endian byte order */
59766b1205bSAtish Patra     for (i = 0; i < ARRAY_SIZE(reset_vec); i++) {
5985aec3247SMichael Clark         reset_vec[i] = cpu_to_le32(reset_vec[i]);
5995aec3247SMichael Clark     }
6005aec3247SMichael Clark     rom_add_blob_fixed_as("mrom.reset", reset_vec, sizeof(reset_vec),
60113b8c354SEduardo Habkost                           memmap[SIFIVE_U_DEV_MROM].base, &address_space_memory);
602dc144fe1SAtish Patra 
60378936771SAlistair Francis     riscv_rom_copy_firmware_info(machine, memmap[SIFIVE_U_DEV_MROM].base,
60413b8c354SEduardo Habkost                                  memmap[SIFIVE_U_DEV_MROM].size,
605dc144fe1SAtish Patra                                  sizeof(reset_vec), kernel_entry);
606*145b2991SBin Meng 
607*145b2991SBin Meng     /* Connect an SPI flash to SPI0 */
608*145b2991SBin Meng     flash_dev = qdev_new("is25wp256");
609*145b2991SBin Meng     dinfo = drive_get_next(IF_MTD);
610*145b2991SBin Meng     if (dinfo) {
611*145b2991SBin Meng         qdev_prop_set_drive_err(flash_dev, "drive",
612*145b2991SBin Meng                                 blk_by_legacy_dinfo(dinfo),
613*145b2991SBin Meng                                 &error_fatal);
614*145b2991SBin Meng     }
615*145b2991SBin Meng     qdev_realize_and_unref(flash_dev, BUS(s->soc.spi0.spi), &error_fatal);
616*145b2991SBin Meng 
617*145b2991SBin Meng     flash_cs = qdev_get_gpio_in_named(flash_dev, SSI_GPIO_CS, 0);
618*145b2991SBin Meng     sysbus_connect_irq(SYS_BUS_DEVICE(&s->soc.spi0), 1, flash_cs);
6192308092bSAlistair Francis }
6202308092bSAlistair Francis 
621523e3464SAlistair Francis static bool sifive_u_machine_get_start_in_flash(Object *obj, Error **errp)
622523e3464SAlistair Francis {
623523e3464SAlistair Francis     SiFiveUState *s = RISCV_U_MACHINE(obj);
624523e3464SAlistair Francis 
625523e3464SAlistair Francis     return s->start_in_flash;
626523e3464SAlistair Francis }
627523e3464SAlistair Francis 
628523e3464SAlistair Francis static void sifive_u_machine_set_start_in_flash(Object *obj, bool value, Error **errp)
629523e3464SAlistair Francis {
630523e3464SAlistair Francis     SiFiveUState *s = RISCV_U_MACHINE(obj);
631523e3464SAlistair Francis 
632523e3464SAlistair Francis     s->start_in_flash = value;
633523e3464SAlistair Francis }
634523e3464SAlistair Francis 
6353e9667cdSBin Meng static void sifive_u_machine_get_uint32_prop(Object *obj, Visitor *v,
6363e9667cdSBin Meng                                              const char *name, void *opaque,
6373e9667cdSBin Meng                                              Error **errp)
6383ca109c3SBin Meng {
6393ca109c3SBin Meng     visit_type_uint32(v, name, (uint32_t *)opaque, errp);
6403ca109c3SBin Meng }
6413ca109c3SBin Meng 
6423e9667cdSBin Meng static void sifive_u_machine_set_uint32_prop(Object *obj, Visitor *v,
6433e9667cdSBin Meng                                              const char *name, void *opaque,
6443e9667cdSBin Meng                                              Error **errp)
6453ca109c3SBin Meng {
6463ca109c3SBin Meng     visit_type_uint32(v, name, (uint32_t *)opaque, errp);
6473ca109c3SBin Meng }
6483ca109c3SBin Meng 
649523e3464SAlistair Francis static void sifive_u_machine_instance_init(Object *obj)
650523e3464SAlistair Francis {
651523e3464SAlistair Francis     SiFiveUState *s = RISCV_U_MACHINE(obj);
652523e3464SAlistair Francis 
653523e3464SAlistair Francis     s->start_in_flash = false;
654cfa32630SBin Meng     s->msel = 0;
655cfa32630SBin Meng     object_property_add(obj, "msel", "uint32",
656cfa32630SBin Meng                         sifive_u_machine_get_uint32_prop,
657cfa32630SBin Meng                         sifive_u_machine_set_uint32_prop, NULL, &s->msel);
658cfa32630SBin Meng     object_property_set_description(obj, "msel",
659cfa32630SBin Meng                                     "Mode Select (MSEL[3:0]) pin state");
660cfa32630SBin Meng 
6613ca109c3SBin Meng     s->serial = OTP_SERIAL;
662d2623129SMarkus Armbruster     object_property_add(obj, "serial", "uint32",
6633e9667cdSBin Meng                         sifive_u_machine_get_uint32_prop,
6643e9667cdSBin Meng                         sifive_u_machine_set_uint32_prop, NULL, &s->serial);
6657eecec7dSMarkus Armbruster     object_property_set_description(obj, "serial", "Board serial number");
666523e3464SAlistair Francis }
667523e3464SAlistair Francis 
668523e3464SAlistair Francis static void sifive_u_machine_class_init(ObjectClass *oc, void *data)
669523e3464SAlistair Francis {
670523e3464SAlistair Francis     MachineClass *mc = MACHINE_CLASS(oc);
671523e3464SAlistair Francis 
672523e3464SAlistair Francis     mc->desc = "RISC-V Board compatible with SiFive U SDK";
673523e3464SAlistair Francis     mc->init = sifive_u_machine_init;
674523e3464SAlistair Francis     mc->max_cpus = SIFIVE_U_MANAGEMENT_CPU_COUNT + SIFIVE_U_COMPUTE_CPU_COUNT;
675523e3464SAlistair Francis     mc->min_cpus = SIFIVE_U_MANAGEMENT_CPU_COUNT + 1;
6761eaada8aSBin Meng     mc->default_cpu_type = SIFIVE_U_CPU;
677523e3464SAlistair Francis     mc->default_cpus = mc->min_cpus;
678418b473eSEduardo Habkost 
679418b473eSEduardo Habkost     object_class_property_add_bool(oc, "start-in-flash",
680418b473eSEduardo Habkost                                    sifive_u_machine_get_start_in_flash,
681418b473eSEduardo Habkost                                    sifive_u_machine_set_start_in_flash);
682418b473eSEduardo Habkost     object_class_property_set_description(oc, "start-in-flash",
683418b473eSEduardo Habkost                                           "Set on to tell QEMU's ROM to jump to "
684418b473eSEduardo Habkost                                           "flash. Otherwise QEMU will jump to DRAM "
685418b473eSEduardo Habkost                                           "or L2LIM depending on the msel value");
686523e3464SAlistair Francis }
687523e3464SAlistair Francis 
688523e3464SAlistair Francis static const TypeInfo sifive_u_machine_typeinfo = {
689523e3464SAlistair Francis     .name       = MACHINE_TYPE_NAME("sifive_u"),
690523e3464SAlistair Francis     .parent     = TYPE_MACHINE,
691523e3464SAlistair Francis     .class_init = sifive_u_machine_class_init,
692523e3464SAlistair Francis     .instance_init = sifive_u_machine_instance_init,
693523e3464SAlistair Francis     .instance_size = sizeof(SiFiveUState),
694523e3464SAlistair Francis };
695523e3464SAlistair Francis 
696523e3464SAlistair Francis static void sifive_u_machine_init_register_types(void)
697523e3464SAlistair Francis {
698523e3464SAlistair Francis     type_register_static(&sifive_u_machine_typeinfo);
699523e3464SAlistair Francis }
700523e3464SAlistair Francis 
701523e3464SAlistair Francis type_init(sifive_u_machine_init_register_types)
702523e3464SAlistair Francis 
703139177b1SBin Meng static void sifive_u_soc_instance_init(Object *obj)
7042308092bSAlistair Francis {
7052308092bSAlistair Francis     SiFiveUSoCState *s = RISCV_U_SOC(obj);
7062308092bSAlistair Francis 
7079fc7fc4dSMarkus Armbruster     object_initialize_child(obj, "e-cluster", &s->e_cluster, TYPE_CPU_CLUSTER);
708ecdfe393SBin Meng     qdev_prop_set_uint32(DEVICE(&s->e_cluster), "cluster-id", 0);
709ecdfe393SBin Meng 
710db873cc5SMarkus Armbruster     object_initialize_child(OBJECT(&s->e_cluster), "e-cpus", &s->e_cpus,
71175a6ed87SMarkus Armbruster                             TYPE_RISCV_HART_ARRAY);
712ecdfe393SBin Meng     qdev_prop_set_uint32(DEVICE(&s->e_cpus), "num-harts", 1);
713ecdfe393SBin Meng     qdev_prop_set_uint32(DEVICE(&s->e_cpus), "hartid-base", 0);
714ecdfe393SBin Meng     qdev_prop_set_string(DEVICE(&s->e_cpus), "cpu-type", SIFIVE_E_CPU);
71573f6ed97SBin Meng     qdev_prop_set_uint64(DEVICE(&s->e_cpus), "resetvec", 0x1004);
716ecdfe393SBin Meng 
7179fc7fc4dSMarkus Armbruster     object_initialize_child(obj, "u-cluster", &s->u_cluster, TYPE_CPU_CLUSTER);
718ecdfe393SBin Meng     qdev_prop_set_uint32(DEVICE(&s->u_cluster), "cluster-id", 1);
719ecdfe393SBin Meng 
720db873cc5SMarkus Armbruster     object_initialize_child(OBJECT(&s->u_cluster), "u-cpus", &s->u_cpus,
72175a6ed87SMarkus Armbruster                             TYPE_RISCV_HART_ARRAY);
7225a7f76a3SAlistair Francis 
723db873cc5SMarkus Armbruster     object_initialize_child(obj, "prci", &s->prci, TYPE_SIFIVE_U_PRCI);
724db873cc5SMarkus Armbruster     object_initialize_child(obj, "otp", &s->otp, TYPE_SIFIVE_U_OTP);
725db873cc5SMarkus Armbruster     object_initialize_child(obj, "gem", &s->gem, TYPE_CADENCE_GEM);
7268a88b9f5SBin Meng     object_initialize_child(obj, "gpio", &s->gpio, TYPE_SIFIVE_GPIO);
727834e027aSBin Meng     object_initialize_child(obj, "pdma", &s->dma, TYPE_SIFIVE_PDMA);
728*145b2991SBin Meng     object_initialize_child(obj, "spi0", &s->spi0, TYPE_SIFIVE_SPI);
7292308092bSAlistair Francis }
7302308092bSAlistair Francis 
731139177b1SBin Meng static void sifive_u_soc_realize(DeviceState *dev, Error **errp)
7322308092bSAlistair Francis {
733c4473127SLike Xu     MachineState *ms = MACHINE(qdev_get_machine());
7342308092bSAlistair Francis     SiFiveUSoCState *s = RISCV_U_SOC(dev);
7352308092bSAlistair Francis     const struct MemmapEntry *memmap = sifive_u_memmap;
7362308092bSAlistair Francis     MemoryRegion *system_memory = get_system_memory();
7372308092bSAlistair Francis     MemoryRegion *mask_rom = g_new(MemoryRegion, 1);
738a6902ef0SAlistair Francis     MemoryRegion *l2lim_mem = g_new(MemoryRegion, 1);
73905446f41SBin Meng     char *plic_hart_config;
74005446f41SBin Meng     size_t plic_hart_config_len;
7415a7f76a3SAlistair Francis     int i;
7425a7f76a3SAlistair Francis     NICInfo *nd = &nd_table[0];
7432308092bSAlistair Francis 
744099be035SAlistair Francis     qdev_prop_set_uint32(DEVICE(&s->u_cpus), "num-harts", ms->smp.cpus - 1);
745099be035SAlistair Francis     qdev_prop_set_uint32(DEVICE(&s->u_cpus), "hartid-base", 1);
746099be035SAlistair Francis     qdev_prop_set_string(DEVICE(&s->u_cpus), "cpu-type", s->cpu_type);
747099be035SAlistair Francis     qdev_prop_set_uint64(DEVICE(&s->u_cpus), "resetvec", 0x1004);
748099be035SAlistair Francis 
749db873cc5SMarkus Armbruster     sysbus_realize(SYS_BUS_DEVICE(&s->e_cpus), &error_abort);
750db873cc5SMarkus Armbruster     sysbus_realize(SYS_BUS_DEVICE(&s->u_cpus), &error_abort);
751ecdfe393SBin Meng     /*
752ecdfe393SBin Meng      * The cluster must be realized after the RISC-V hart array container,
753ecdfe393SBin Meng      * as the container's CPU object is only created on realize, and the
754ecdfe393SBin Meng      * CPU must exist and have been parented into the cluster before the
755ecdfe393SBin Meng      * cluster is realized.
756ecdfe393SBin Meng      */
757ce189ab2SMarkus Armbruster     qdev_realize(DEVICE(&s->e_cluster), NULL, &error_abort);
758ce189ab2SMarkus Armbruster     qdev_realize(DEVICE(&s->u_cluster), NULL, &error_abort);
7592308092bSAlistair Francis 
7602308092bSAlistair Francis     /* boot rom */
761414c47d2SPhilippe Mathieu-Daudé     memory_region_init_rom(mask_rom, OBJECT(dev), "riscv.sifive.u.mrom",
76213b8c354SEduardo Habkost                            memmap[SIFIVE_U_DEV_MROM].size, &error_fatal);
76313b8c354SEduardo Habkost     memory_region_add_subregion(system_memory, memmap[SIFIVE_U_DEV_MROM].base,
7642308092bSAlistair Francis                                 mask_rom);
765a7240d1eSMichael Clark 
766a6902ef0SAlistair Francis     /*
767a6902ef0SAlistair Francis      * Add L2-LIM at reset size.
768a6902ef0SAlistair Francis      * This should be reduced in size as the L2 Cache Controller WayEnable
769a6902ef0SAlistair Francis      * register is incremented. Unfortunately I don't see a nice (or any) way
770a6902ef0SAlistair Francis      * to handle reducing or blocking out the L2 LIM while still allowing it
771a6902ef0SAlistair Francis      * be re returned to all enabled after a reset. For the time being, just
772a6902ef0SAlistair Francis      * leave it enabled all the time. This won't break anything, but will be
773a6902ef0SAlistair Francis      * too generous to misbehaving guests.
774a6902ef0SAlistair Francis      */
775a6902ef0SAlistair Francis     memory_region_init_ram(l2lim_mem, NULL, "riscv.sifive.u.l2lim",
77613b8c354SEduardo Habkost                            memmap[SIFIVE_U_DEV_L2LIM].size, &error_fatal);
77713b8c354SEduardo Habkost     memory_region_add_subregion(system_memory, memmap[SIFIVE_U_DEV_L2LIM].base,
778a6902ef0SAlistair Francis                                 l2lim_mem);
779a6902ef0SAlistair Francis 
78005446f41SBin Meng     /* create PLIC hart topology configuration string */
781c4473127SLike Xu     plic_hart_config_len = (strlen(SIFIVE_U_PLIC_HART_CONFIG) + 1) *
782c4473127SLike Xu                            ms->smp.cpus;
78305446f41SBin Meng     plic_hart_config = g_malloc0(plic_hart_config_len);
784c4473127SLike Xu     for (i = 0; i < ms->smp.cpus; i++) {
78505446f41SBin Meng         if (i != 0) {
786ef965ce2SBin Meng             strncat(plic_hart_config, "," SIFIVE_U_PLIC_HART_CONFIG,
78705446f41SBin Meng                     plic_hart_config_len);
788ef965ce2SBin Meng         } else {
789ef965ce2SBin Meng             strncat(plic_hart_config, "M", plic_hart_config_len);
790ef965ce2SBin Meng         }
79105446f41SBin Meng         plic_hart_config_len -= (strlen(SIFIVE_U_PLIC_HART_CONFIG) + 1);
79205446f41SBin Meng     }
79305446f41SBin Meng 
794a7240d1eSMichael Clark     /* MMIO */
79513b8c354SEduardo Habkost     s->plic = sifive_plic_create(memmap[SIFIVE_U_DEV_PLIC].base,
796c9270e10SAnup Patel         plic_hart_config, 0,
797a7240d1eSMichael Clark         SIFIVE_U_PLIC_NUM_SOURCES,
798a7240d1eSMichael Clark         SIFIVE_U_PLIC_NUM_PRIORITIES,
799a7240d1eSMichael Clark         SIFIVE_U_PLIC_PRIORITY_BASE,
800a7240d1eSMichael Clark         SIFIVE_U_PLIC_PENDING_BASE,
801a7240d1eSMichael Clark         SIFIVE_U_PLIC_ENABLE_BASE,
802a7240d1eSMichael Clark         SIFIVE_U_PLIC_ENABLE_STRIDE,
803a7240d1eSMichael Clark         SIFIVE_U_PLIC_CONTEXT_BASE,
804a7240d1eSMichael Clark         SIFIVE_U_PLIC_CONTEXT_STRIDE,
80513b8c354SEduardo Habkost         memmap[SIFIVE_U_DEV_PLIC].size);
806bb8136dfSPan Nengyuan     g_free(plic_hart_config);
80713b8c354SEduardo Habkost     sifive_uart_create(system_memory, memmap[SIFIVE_U_DEV_UART0].base,
808647a70a1SAlistair Francis         serial_hd(0), qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_U_UART0_IRQ));
80913b8c354SEduardo Habkost     sifive_uart_create(system_memory, memmap[SIFIVE_U_DEV_UART1].base,
810194eef09SMichael Clark         serial_hd(1), qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_U_UART1_IRQ));
81113b8c354SEduardo Habkost     sifive_clint_create(memmap[SIFIVE_U_DEV_CLINT].base,
81213b8c354SEduardo Habkost         memmap[SIFIVE_U_DEV_CLINT].size, 0, ms->smp.cpus,
813a47ef6e9SBin Meng         SIFIVE_SIP_BASE, SIFIVE_TIMECMP_BASE, SIFIVE_TIME_BASE,
814a47ef6e9SBin Meng         SIFIVE_CLINT_TIMEBASE_FREQ, false);
8155a7f76a3SAlistair Francis 
816cbe3a8c5SMarkus Armbruster     if (!sysbus_realize(SYS_BUS_DEVICE(&s->prci), errp)) {
817cbe3a8c5SMarkus Armbruster         return;
818cbe3a8c5SMarkus Armbruster     }
81913b8c354SEduardo Habkost     sysbus_mmio_map(SYS_BUS_DEVICE(&s->prci), 0, memmap[SIFIVE_U_DEV_PRCI].base);
820af14c840SBin Meng 
8218a88b9f5SBin Meng     qdev_prop_set_uint32(DEVICE(&s->gpio), "ngpio", 16);
822cbe3a8c5SMarkus Armbruster     if (!sysbus_realize(SYS_BUS_DEVICE(&s->gpio), errp)) {
823cbe3a8c5SMarkus Armbruster         return;
824cbe3a8c5SMarkus Armbruster     }
82513b8c354SEduardo Habkost     sysbus_mmio_map(SYS_BUS_DEVICE(&s->gpio), 0, memmap[SIFIVE_U_DEV_GPIO].base);
8268a88b9f5SBin Meng 
8278a88b9f5SBin Meng     /* Pass all GPIOs to the SOC layer so they are available to the board */
8288a88b9f5SBin Meng     qdev_pass_gpios(DEVICE(&s->gpio), dev, NULL);
8298a88b9f5SBin Meng 
8308a88b9f5SBin Meng     /* Connect GPIO interrupts to the PLIC */
8318a88b9f5SBin Meng     for (i = 0; i < 16; i++) {
8328a88b9f5SBin Meng         sysbus_connect_irq(SYS_BUS_DEVICE(&s->gpio), i,
8338a88b9f5SBin Meng                            qdev_get_gpio_in(DEVICE(s->plic),
8348a88b9f5SBin Meng                                             SIFIVE_U_GPIO_IRQ0 + i));
8358a88b9f5SBin Meng     }
8368a88b9f5SBin Meng 
837834e027aSBin Meng     /* PDMA */
838834e027aSBin Meng     sysbus_realize(SYS_BUS_DEVICE(&s->dma), errp);
83913b8c354SEduardo Habkost     sysbus_mmio_map(SYS_BUS_DEVICE(&s->dma), 0, memmap[SIFIVE_U_DEV_PDMA].base);
840834e027aSBin Meng 
841834e027aSBin Meng     /* Connect PDMA interrupts to the PLIC */
842834e027aSBin Meng     for (i = 0; i < SIFIVE_PDMA_IRQS; i++) {
843834e027aSBin Meng         sysbus_connect_irq(SYS_BUS_DEVICE(&s->dma), i,
844834e027aSBin Meng                            qdev_get_gpio_in(DEVICE(s->plic),
845834e027aSBin Meng                                             SIFIVE_U_PDMA_IRQ0 + i));
846834e027aSBin Meng     }
847834e027aSBin Meng 
848fda5b000SAlistair Francis     qdev_prop_set_uint32(DEVICE(&s->otp), "serial", s->serial);
849cbe3a8c5SMarkus Armbruster     if (!sysbus_realize(SYS_BUS_DEVICE(&s->otp), errp)) {
850cbe3a8c5SMarkus Armbruster         return;
851cbe3a8c5SMarkus Armbruster     }
85213b8c354SEduardo Habkost     sysbus_mmio_map(SYS_BUS_DEVICE(&s->otp), 0, memmap[SIFIVE_U_DEV_OTP].base);
8535461c4feSBin Meng 
8547ad36e2eSMarkus Armbruster     /* FIXME use qdev NIC properties instead of nd_table[] */
8555a7f76a3SAlistair Francis     if (nd->used) {
8565a7f76a3SAlistair Francis         qemu_check_nic_model(nd, TYPE_CADENCE_GEM);
8575a7f76a3SAlistair Francis         qdev_set_nic_properties(DEVICE(&s->gem), nd);
8585a7f76a3SAlistair Francis     }
8595325cc34SMarkus Armbruster     object_property_set_int(OBJECT(&s->gem), "revision", GEM_REVISION,
8605a7f76a3SAlistair Francis                             &error_abort);
861668f62ecSMarkus Armbruster     if (!sysbus_realize(SYS_BUS_DEVICE(&s->gem), errp)) {
8625a7f76a3SAlistair Francis         return;
8635a7f76a3SAlistair Francis     }
86413b8c354SEduardo Habkost     sysbus_mmio_map(SYS_BUS_DEVICE(&s->gem), 0, memmap[SIFIVE_U_DEV_GEM].base);
8655a7f76a3SAlistair Francis     sysbus_connect_irq(SYS_BUS_DEVICE(&s->gem), 0,
8665874f0a7SBin Meng                        qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_U_GEM_IRQ));
8677b6bb66fSBin Meng 
8687b6bb66fSBin Meng     create_unimplemented_device("riscv.sifive.u.gem-mgmt",
86913b8c354SEduardo Habkost         memmap[SIFIVE_U_DEV_GEM_MGMT].base, memmap[SIFIVE_U_DEV_GEM_MGMT].size);
8703eaea6ebSBin Meng 
8713eaea6ebSBin Meng     create_unimplemented_device("riscv.sifive.u.dmc",
87213b8c354SEduardo Habkost         memmap[SIFIVE_U_DEV_DMC].base, memmap[SIFIVE_U_DEV_DMC].size);
8736eaf9cf5SBin Meng 
8746eaf9cf5SBin Meng     create_unimplemented_device("riscv.sifive.u.l2cc",
87513b8c354SEduardo Habkost         memmap[SIFIVE_U_DEV_L2CC].base, memmap[SIFIVE_U_DEV_L2CC].size);
876*145b2991SBin Meng 
877*145b2991SBin Meng     sysbus_realize(SYS_BUS_DEVICE(&s->spi0), errp);
878*145b2991SBin Meng     sysbus_mmio_map(SYS_BUS_DEVICE(&s->spi0), 0,
879*145b2991SBin Meng                     memmap[SIFIVE_U_DEV_QSPI0].base);
880*145b2991SBin Meng     sysbus_connect_irq(SYS_BUS_DEVICE(&s->spi0), 0,
881*145b2991SBin Meng                        qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_U_QSPI0_IRQ));
882a7240d1eSMichael Clark }
883a7240d1eSMichael Clark 
884139177b1SBin Meng static Property sifive_u_soc_props[] = {
885fda5b000SAlistair Francis     DEFINE_PROP_UINT32("serial", SiFiveUSoCState, serial, OTP_SERIAL),
886099be035SAlistair Francis     DEFINE_PROP_STRING("cpu-type", SiFiveUSoCState, cpu_type),
887fda5b000SAlistair Francis     DEFINE_PROP_END_OF_LIST()
888fda5b000SAlistair Francis };
889fda5b000SAlistair Francis 
890139177b1SBin Meng static void sifive_u_soc_class_init(ObjectClass *oc, void *data)
8912308092bSAlistair Francis {
8922308092bSAlistair Francis     DeviceClass *dc = DEVICE_CLASS(oc);
8932308092bSAlistair Francis 
894139177b1SBin Meng     device_class_set_props(dc, sifive_u_soc_props);
895139177b1SBin Meng     dc->realize = sifive_u_soc_realize;
8962308092bSAlistair Francis     /* Reason: Uses serial_hds in realize function, thus can't be used twice */
8972308092bSAlistair Francis     dc->user_creatable = false;
8982308092bSAlistair Francis }
8992308092bSAlistair Francis 
900139177b1SBin Meng static const TypeInfo sifive_u_soc_type_info = {
9012308092bSAlistair Francis     .name = TYPE_RISCV_U_SOC,
9022308092bSAlistair Francis     .parent = TYPE_DEVICE,
9032308092bSAlistair Francis     .instance_size = sizeof(SiFiveUSoCState),
904139177b1SBin Meng     .instance_init = sifive_u_soc_instance_init,
905139177b1SBin Meng     .class_init = sifive_u_soc_class_init,
9062308092bSAlistair Francis };
9072308092bSAlistair Francis 
908139177b1SBin Meng static void sifive_u_soc_register_types(void)
9092308092bSAlistair Francis {
910139177b1SBin Meng     type_register_static(&sifive_u_soc_type_info);
9112308092bSAlistair Francis }
9122308092bSAlistair Francis 
913139177b1SBin Meng type_init(sifive_u_soc_register_types)
914