xref: /openbmc/qemu/hw/riscv/sifive_u.c (revision 92ec7805190313c9e628f8fc4eb4f932c15247bd)
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
20ea6eaa06SAlistair Francis  * 10) PWM0 and PWM1
21a7240d1eSMichael Clark  *
22f3d47d58SBin Meng  * This board currently generates devicetree dynamically that indicates at least
23ecdfe393SBin Meng  * two harts and up to five harts.
24a7240d1eSMichael Clark  *
25a7240d1eSMichael Clark  * This program is free software; you can redistribute it and/or modify it
26a7240d1eSMichael Clark  * under the terms and conditions of the GNU General Public License,
27a7240d1eSMichael Clark  * version 2 or later, as published by the Free Software Foundation.
28a7240d1eSMichael Clark  *
29a7240d1eSMichael Clark  * This program is distributed in the hope it will be useful, but WITHOUT
30a7240d1eSMichael Clark  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
31a7240d1eSMichael Clark  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
32a7240d1eSMichael Clark  * more details.
33a7240d1eSMichael Clark  *
34a7240d1eSMichael Clark  * You should have received a copy of the GNU General Public License along with
35a7240d1eSMichael Clark  * this program.  If not, see <http://www.gnu.org/licenses/>.
36a7240d1eSMichael Clark  */
37a7240d1eSMichael Clark 
38a7240d1eSMichael Clark #include "qemu/osdep.h"
39a7240d1eSMichael Clark #include "qemu/error-report.h"
40a7240d1eSMichael Clark #include "qapi/error.h"
413ca109c3SBin Meng #include "qapi/visitor.h"
42a7240d1eSMichael Clark #include "hw/boards.h"
435133ed17SBin Meng #include "hw/irq.h"
44a7240d1eSMichael Clark #include "hw/loader.h"
45a7240d1eSMichael Clark #include "hw/sysbus.h"
46ecdfe393SBin Meng #include "hw/cpu/cluster.h"
477b6bb66fSBin Meng #include "hw/misc/unimp.h"
4836aa285fSMarkus Armbruster #include "hw/sd/sd.h"
49145b2991SBin Meng #include "hw/ssi/ssi.h"
50a7240d1eSMichael Clark #include "target/riscv/cpu.h"
51a7240d1eSMichael Clark #include "hw/riscv/riscv_hart.h"
52a7240d1eSMichael Clark #include "hw/riscv/sifive_u.h"
530ac24d56SAlistair Francis #include "hw/riscv/boot.h"
54b609b7e3SBin Meng #include "hw/char/sifive_uart.h"
55cc63a182SAnup Patel #include "hw/intc/riscv_aclint.h"
5684fcf3c1SBin Meng #include "hw/intc/sifive_plic.h"
57a7240d1eSMichael Clark #include "chardev/char.h"
587b6bb66fSBin Meng #include "net/eth.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 
65074ca702SBin Meng /* CLINT timebase frequency */
66074ca702SBin Meng #define CLINT_TIMEBASE_FREQ 1000000
67074ca702SBin 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 },
79ea6eaa06SAlistair Francis     [SIFIVE_U_DEV_PWM0] =     { 0x10020000,     0x1000 },
80ea6eaa06SAlistair Francis     [SIFIVE_U_DEV_PWM1] =     { 0x10021000,     0x1000 },
81145b2991SBin Meng     [SIFIVE_U_DEV_QSPI0] =    { 0x10040000,     0x1000 },
82722f1352SBin Meng     [SIFIVE_U_DEV_QSPI2] =    { 0x10050000,     0x1000 },
8313b8c354SEduardo Habkost     [SIFIVE_U_DEV_GPIO] =     { 0x10060000,     0x1000 },
8413b8c354SEduardo Habkost     [SIFIVE_U_DEV_OTP] =      { 0x10070000,     0x1000 },
8513b8c354SEduardo Habkost     [SIFIVE_U_DEV_GEM] =      { 0x10090000,     0x2000 },
8613b8c354SEduardo Habkost     [SIFIVE_U_DEV_GEM_MGMT] = { 0x100a0000,     0x1000 },
8713b8c354SEduardo Habkost     [SIFIVE_U_DEV_DMC] =      { 0x100b0000,    0x10000 },
8813b8c354SEduardo Habkost     [SIFIVE_U_DEV_FLASH0] =   { 0x20000000, 0x10000000 },
8913b8c354SEduardo Habkost     [SIFIVE_U_DEV_DRAM] =     { 0x80000000,        0x0 },
90a7240d1eSMichael Clark };
91a7240d1eSMichael Clark 
925461c4feSBin Meng #define OTP_SERIAL          1
935a7f76a3SAlistair Francis #define GEM_REVISION        0x10070109
945a7f76a3SAlistair Francis 
create_fdt(SiFiveUState * s,const MemMapEntry * memmap,bool is_32_bit)9573261285SBin Meng static void create_fdt(SiFiveUState *s, const MemMapEntry *memmap,
96f5be2ccbSDaniel Henrique Barboza                        bool is_32_bit)
97a7240d1eSMichael Clark {
98f5be2ccbSDaniel Henrique Barboza     MachineState *ms = MACHINE(s);
99f5be2ccbSDaniel Henrique Barboza     uint64_t mem_size = ms->ram_size;
100a7240d1eSMichael Clark     void *fdt;
101fc9ec362SBin Meng     int cpu;
102a7240d1eSMichael Clark     uint32_t *cells;
103a7240d1eSMichael Clark     char *nodename;
1045133ed17SBin Meng     uint32_t plic_phandle, prci_phandle, gpio_phandle, phandle = 1;
1057b6bb66fSBin Meng     uint32_t hfclk_phandle, rtcclk_phandle, phy_phandle;
106cb53b283SBin Meng     static const char * const ethclk_names[2] = { "pclk", "hclk" };
1077cfbb17fSBin Meng     static const char * const clint_compat[2] = {
1087cfbb17fSBin Meng         "sifive,clint0", "riscv,clint0"
1097cfbb17fSBin Meng     };
11060bb5407SBin Meng     static const char * const plic_compat[2] = {
11160bb5407SBin Meng         "sifive,plic-1.0.0", "riscv,plic0"
11260bb5407SBin Meng     };
113a7240d1eSMichael Clark 
114fc9ec362SBin Meng     fdt = ms->fdt = create_device_tree(&s->fdt_size);
115a7240d1eSMichael Clark     if (!fdt) {
116a7240d1eSMichael Clark         error_report("create_device_tree() failed");
117a7240d1eSMichael Clark         exit(1);
118a7240d1eSMichael Clark     }
119a7240d1eSMichael Clark 
120d372e748SBin Meng     qemu_fdt_setprop_string(fdt, "/", "model", "SiFive HiFive Unleashed A00");
121d372e748SBin Meng     qemu_fdt_setprop_string(fdt, "/", "compatible",
122d372e748SBin Meng                             "sifive,hifive-unleashed-a00");
123a7240d1eSMichael Clark     qemu_fdt_setprop_cell(fdt, "/", "#size-cells", 0x2);
124a7240d1eSMichael Clark     qemu_fdt_setprop_cell(fdt, "/", "#address-cells", 0x2);
125a7240d1eSMichael Clark 
126a7240d1eSMichael Clark     qemu_fdt_add_subnode(fdt, "/soc");
127a7240d1eSMichael Clark     qemu_fdt_setprop(fdt, "/soc", "ranges", NULL, 0);
1282a1a6f6dSAlistair Francis     qemu_fdt_setprop_string(fdt, "/soc", "compatible", "simple-bus");
129a7240d1eSMichael Clark     qemu_fdt_setprop_cell(fdt, "/soc", "#size-cells", 0x2);
130a7240d1eSMichael Clark     qemu_fdt_setprop_cell(fdt, "/soc", "#address-cells", 0x2);
131a7240d1eSMichael Clark 
132e1724d09SBin Meng     hfclk_phandle = phandle++;
133e1724d09SBin Meng     nodename = g_strdup_printf("/hfclk");
134e1724d09SBin Meng     qemu_fdt_add_subnode(fdt, nodename);
135e1724d09SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "phandle", hfclk_phandle);
136e1724d09SBin Meng     qemu_fdt_setprop_string(fdt, nodename, "clock-output-names", "hfclk");
137e1724d09SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "clock-frequency",
138e1724d09SBin Meng         SIFIVE_U_HFCLK_FREQ);
139e1724d09SBin Meng     qemu_fdt_setprop_string(fdt, nodename, "compatible", "fixed-clock");
140e1724d09SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "#clock-cells", 0x0);
141e1724d09SBin Meng     g_free(nodename);
142e1724d09SBin Meng 
143e1724d09SBin Meng     rtcclk_phandle = phandle++;
144e1724d09SBin Meng     nodename = g_strdup_printf("/rtcclk");
145e1724d09SBin Meng     qemu_fdt_add_subnode(fdt, nodename);
146e1724d09SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "phandle", rtcclk_phandle);
147e1724d09SBin Meng     qemu_fdt_setprop_string(fdt, nodename, "clock-output-names", "rtcclk");
148e1724d09SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "clock-frequency",
149e1724d09SBin Meng         SIFIVE_U_RTCCLK_FREQ);
150e1724d09SBin Meng     qemu_fdt_setprop_string(fdt, nodename, "compatible", "fixed-clock");
151e1724d09SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "#clock-cells", 0x0);
152e1724d09SBin Meng     g_free(nodename);
153e1724d09SBin Meng 
154a7240d1eSMichael Clark     nodename = g_strdup_printf("/memory@%lx",
15513b8c354SEduardo Habkost         (long)memmap[SIFIVE_U_DEV_DRAM].base);
156a7240d1eSMichael Clark     qemu_fdt_add_subnode(fdt, nodename);
157a7240d1eSMichael Clark     qemu_fdt_setprop_cells(fdt, nodename, "reg",
15813b8c354SEduardo Habkost         memmap[SIFIVE_U_DEV_DRAM].base >> 32, memmap[SIFIVE_U_DEV_DRAM].base,
159a7240d1eSMichael Clark         mem_size >> 32, mem_size);
160a7240d1eSMichael Clark     qemu_fdt_setprop_string(fdt, nodename, "device_type", "memory");
161a7240d1eSMichael Clark     g_free(nodename);
162a7240d1eSMichael Clark 
163a7240d1eSMichael Clark     qemu_fdt_add_subnode(fdt, "/cpus");
1642a8756edSMichael Clark     qemu_fdt_setprop_cell(fdt, "/cpus", "timebase-frequency",
165074ca702SBin Meng         CLINT_TIMEBASE_FREQ);
166a7240d1eSMichael Clark     qemu_fdt_setprop_cell(fdt, "/cpus", "#size-cells", 0x0);
167a7240d1eSMichael Clark     qemu_fdt_setprop_cell(fdt, "/cpus", "#address-cells", 0x1);
168a7240d1eSMichael Clark 
169ecdfe393SBin Meng     for (cpu = ms->smp.cpus - 1; cpu >= 0; cpu--) {
170382cb439SBin Meng         int cpu_phandle = phandle++;
171a7240d1eSMichael Clark         nodename = g_strdup_printf("/cpus/cpu@%d", cpu);
172a7240d1eSMichael Clark         char *intc = g_strdup_printf("/cpus/cpu@%d/interrupt-controller", cpu);
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             }
1811c8e491cSConor Dooley             riscv_isa_write_fdt(&s->soc.u_cpus.harts[cpu - 1], fdt, nodename);
182ecdfe393SBin Meng         } else {
1831c8e491cSConor Dooley             riscv_isa_write_fdt(&s->soc.e_cpus.harts[0], fdt, nodename);
184ecdfe393SBin Meng         }
185a7240d1eSMichael Clark         qemu_fdt_setprop_string(fdt, nodename, "compatible", "riscv");
186a7240d1eSMichael Clark         qemu_fdt_setprop_string(fdt, nodename, "status", "okay");
187a7240d1eSMichael Clark         qemu_fdt_setprop_cell(fdt, nodename, "reg", cpu);
188a7240d1eSMichael Clark         qemu_fdt_setprop_string(fdt, nodename, "device_type", "cpu");
189a7240d1eSMichael Clark         qemu_fdt_add_subnode(fdt, intc);
190382cb439SBin Meng         qemu_fdt_setprop_cell(fdt, intc, "phandle", cpu_phandle);
191a7240d1eSMichael Clark         qemu_fdt_setprop_string(fdt, intc, "compatible", "riscv,cpu-intc");
192a7240d1eSMichael Clark         qemu_fdt_setprop(fdt, intc, "interrupt-controller", NULL, 0);
193a7240d1eSMichael Clark         qemu_fdt_setprop_cell(fdt, intc, "#interrupt-cells", 1);
194a7240d1eSMichael Clark         g_free(intc);
195a7240d1eSMichael Clark         g_free(nodename);
196a7240d1eSMichael Clark     }
197a7240d1eSMichael Clark 
198ecdfe393SBin Meng     cells =  g_new0(uint32_t, ms->smp.cpus * 4);
199ecdfe393SBin Meng     for (cpu = 0; cpu < ms->smp.cpus; cpu++) {
200a7240d1eSMichael Clark         nodename =
201a7240d1eSMichael Clark             g_strdup_printf("/cpus/cpu@%d/interrupt-controller", cpu);
202a7240d1eSMichael Clark         uint32_t intc_phandle = qemu_fdt_get_phandle(fdt, nodename);
203a7240d1eSMichael Clark         cells[cpu * 4 + 0] = cpu_to_be32(intc_phandle);
204a7240d1eSMichael Clark         cells[cpu * 4 + 1] = cpu_to_be32(IRQ_M_SOFT);
205a7240d1eSMichael Clark         cells[cpu * 4 + 2] = cpu_to_be32(intc_phandle);
206a7240d1eSMichael Clark         cells[cpu * 4 + 3] = cpu_to_be32(IRQ_M_TIMER);
207a7240d1eSMichael Clark         g_free(nodename);
208a7240d1eSMichael Clark     }
209a7240d1eSMichael Clark     nodename = g_strdup_printf("/soc/clint@%lx",
21013b8c354SEduardo Habkost         (long)memmap[SIFIVE_U_DEV_CLINT].base);
211a7240d1eSMichael Clark     qemu_fdt_add_subnode(fdt, nodename);
2127cfbb17fSBin Meng     qemu_fdt_setprop_string_array(fdt, nodename, "compatible",
2137cfbb17fSBin Meng         (char **)&clint_compat, ARRAY_SIZE(clint_compat));
214a7240d1eSMichael Clark     qemu_fdt_setprop_cells(fdt, nodename, "reg",
21513b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_CLINT].base,
21613b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_CLINT].size);
217a7240d1eSMichael Clark     qemu_fdt_setprop(fdt, nodename, "interrupts-extended",
218ecdfe393SBin Meng         cells, ms->smp.cpus * sizeof(uint32_t) * 4);
219a7240d1eSMichael Clark     g_free(cells);
220a7240d1eSMichael Clark     g_free(nodename);
221a7240d1eSMichael Clark 
222ea85f27dSBin Meng     nodename = g_strdup_printf("/soc/otp@%lx",
22313b8c354SEduardo Habkost         (long)memmap[SIFIVE_U_DEV_OTP].base);
224ea85f27dSBin Meng     qemu_fdt_add_subnode(fdt, nodename);
225ea85f27dSBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "fuse-count", SIFIVE_U_OTP_REG_SIZE);
226ea85f27dSBin Meng     qemu_fdt_setprop_cells(fdt, nodename, "reg",
22713b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_OTP].base,
22813b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_OTP].size);
229ea85f27dSBin Meng     qemu_fdt_setprop_string(fdt, nodename, "compatible",
230ea85f27dSBin Meng         "sifive,fu540-c000-otp");
231ea85f27dSBin Meng     g_free(nodename);
232ea85f27dSBin Meng 
233af14c840SBin Meng     prci_phandle = phandle++;
234af14c840SBin Meng     nodename = g_strdup_printf("/soc/clock-controller@%lx",
23513b8c354SEduardo Habkost         (long)memmap[SIFIVE_U_DEV_PRCI].base);
236af14c840SBin Meng     qemu_fdt_add_subnode(fdt, nodename);
237af14c840SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "phandle", prci_phandle);
238af14c840SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "#clock-cells", 0x1);
239af14c840SBin Meng     qemu_fdt_setprop_cells(fdt, nodename, "clocks",
240af14c840SBin Meng         hfclk_phandle, rtcclk_phandle);
241af14c840SBin Meng     qemu_fdt_setprop_cells(fdt, nodename, "reg",
24213b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_PRCI].base,
24313b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_PRCI].size);
244af14c840SBin Meng     qemu_fdt_setprop_string(fdt, nodename, "compatible",
245af14c840SBin Meng         "sifive,fu540-c000-prci");
246af14c840SBin Meng     g_free(nodename);
247af14c840SBin Meng 
248382cb439SBin Meng     plic_phandle = phandle++;
249ecdfe393SBin Meng     cells =  g_new0(uint32_t, ms->smp.cpus * 4 - 2);
250ecdfe393SBin Meng     for (cpu = 0; cpu < ms->smp.cpus; cpu++) {
251a7240d1eSMichael Clark         nodename =
252a7240d1eSMichael Clark             g_strdup_printf("/cpus/cpu@%d/interrupt-controller", cpu);
253a7240d1eSMichael Clark         uint32_t intc_phandle = qemu_fdt_get_phandle(fdt, nodename);
254ecdfe393SBin Meng         /* cpu 0 is the management hart that does not have S-mode */
255ecdfe393SBin Meng         if (cpu == 0) {
256ecdfe393SBin Meng             cells[0] = cpu_to_be32(intc_phandle);
257ecdfe393SBin Meng             cells[1] = cpu_to_be32(IRQ_M_EXT);
258ecdfe393SBin Meng         } else {
259ecdfe393SBin Meng             cells[cpu * 4 - 2] = cpu_to_be32(intc_phandle);
260ecdfe393SBin Meng             cells[cpu * 4 - 1] = cpu_to_be32(IRQ_M_EXT);
261a7240d1eSMichael Clark             cells[cpu * 4 + 0] = cpu_to_be32(intc_phandle);
262ecdfe393SBin Meng             cells[cpu * 4 + 1] = cpu_to_be32(IRQ_S_EXT);
263ecdfe393SBin Meng         }
264a7240d1eSMichael Clark         g_free(nodename);
265a7240d1eSMichael Clark     }
266a7240d1eSMichael Clark     nodename = g_strdup_printf("/soc/interrupt-controller@%lx",
26713b8c354SEduardo Habkost         (long)memmap[SIFIVE_U_DEV_PLIC].base);
268a7240d1eSMichael Clark     qemu_fdt_add_subnode(fdt, nodename);
269a7240d1eSMichael Clark     qemu_fdt_setprop_cell(fdt, nodename, "#interrupt-cells", 1);
27060bb5407SBin Meng     qemu_fdt_setprop_string_array(fdt, nodename, "compatible",
27160bb5407SBin Meng         (char **)&plic_compat, ARRAY_SIZE(plic_compat));
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);
278724d80c8SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "riscv,ndev",
279724d80c8SBin Meng                           SIFIVE_U_PLIC_NUM_SOURCES - 1);
28004e7edd1SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "phandle", plic_phandle);
281a7240d1eSMichael Clark     plic_phandle = qemu_fdt_get_phandle(fdt, nodename);
282a7240d1eSMichael Clark     g_free(cells);
283a7240d1eSMichael Clark     g_free(nodename);
284a7240d1eSMichael Clark 
2855133ed17SBin Meng     gpio_phandle = phandle++;
2868a88b9f5SBin Meng     nodename = g_strdup_printf("/soc/gpio@%lx",
28713b8c354SEduardo Habkost         (long)memmap[SIFIVE_U_DEV_GPIO].base);
2888a88b9f5SBin Meng     qemu_fdt_add_subnode(fdt, nodename);
2895133ed17SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "phandle", gpio_phandle);
2908a88b9f5SBin Meng     qemu_fdt_setprop_cells(fdt, nodename, "clocks",
2918a88b9f5SBin Meng         prci_phandle, PRCI_CLK_TLCLK);
2928a88b9f5SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "#interrupt-cells", 2);
2938a88b9f5SBin Meng     qemu_fdt_setprop(fdt, nodename, "interrupt-controller", NULL, 0);
2948a88b9f5SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "#gpio-cells", 2);
2958a88b9f5SBin Meng     qemu_fdt_setprop(fdt, nodename, "gpio-controller", NULL, 0);
2968a88b9f5SBin Meng     qemu_fdt_setprop_cells(fdt, nodename, "reg",
29713b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_GPIO].base,
29813b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_GPIO].size);
2998a88b9f5SBin Meng     qemu_fdt_setprop_cells(fdt, nodename, "interrupts", SIFIVE_U_GPIO_IRQ0,
3008a88b9f5SBin Meng         SIFIVE_U_GPIO_IRQ1, SIFIVE_U_GPIO_IRQ2, SIFIVE_U_GPIO_IRQ3,
3018a88b9f5SBin Meng         SIFIVE_U_GPIO_IRQ4, SIFIVE_U_GPIO_IRQ5, SIFIVE_U_GPIO_IRQ6,
3028a88b9f5SBin Meng         SIFIVE_U_GPIO_IRQ7, SIFIVE_U_GPIO_IRQ8, SIFIVE_U_GPIO_IRQ9,
3038a88b9f5SBin Meng         SIFIVE_U_GPIO_IRQ10, SIFIVE_U_GPIO_IRQ11, SIFIVE_U_GPIO_IRQ12,
3048a88b9f5SBin Meng         SIFIVE_U_GPIO_IRQ13, SIFIVE_U_GPIO_IRQ14, SIFIVE_U_GPIO_IRQ15);
3058a88b9f5SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle);
3068a88b9f5SBin Meng     qemu_fdt_setprop_string(fdt, nodename, "compatible", "sifive,gpio0");
3078a88b9f5SBin Meng     g_free(nodename);
3088a88b9f5SBin Meng 
3095133ed17SBin Meng     nodename = g_strdup_printf("/gpio-restart");
3105133ed17SBin Meng     qemu_fdt_add_subnode(fdt, nodename);
3115133ed17SBin Meng     qemu_fdt_setprop_cells(fdt, nodename, "gpios", gpio_phandle, 10, 1);
3125133ed17SBin Meng     qemu_fdt_setprop_string(fdt, nodename, "compatible", "gpio-restart");
3135133ed17SBin Meng     g_free(nodename);
3145133ed17SBin Meng 
315834e027aSBin Meng     nodename = g_strdup_printf("/soc/dma@%lx",
31613b8c354SEduardo Habkost         (long)memmap[SIFIVE_U_DEV_PDMA].base);
317834e027aSBin Meng     qemu_fdt_add_subnode(fdt, nodename);
318834e027aSBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "#dma-cells", 1);
319834e027aSBin Meng     qemu_fdt_setprop_cells(fdt, nodename, "interrupts",
320834e027aSBin Meng         SIFIVE_U_PDMA_IRQ0, SIFIVE_U_PDMA_IRQ1, SIFIVE_U_PDMA_IRQ2,
321834e027aSBin Meng         SIFIVE_U_PDMA_IRQ3, SIFIVE_U_PDMA_IRQ4, SIFIVE_U_PDMA_IRQ5,
322834e027aSBin Meng         SIFIVE_U_PDMA_IRQ6, SIFIVE_U_PDMA_IRQ7);
323834e027aSBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle);
324834e027aSBin Meng     qemu_fdt_setprop_cells(fdt, nodename, "reg",
32513b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_PDMA].base,
32613b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_PDMA].size);
327834e027aSBin Meng     qemu_fdt_setprop_string(fdt, nodename, "compatible",
328834e027aSBin Meng                             "sifive,fu540-c000-pdma");
329834e027aSBin Meng     g_free(nodename);
330834e027aSBin Meng 
3316eaf9cf5SBin Meng     nodename = g_strdup_printf("/soc/cache-controller@%lx",
33213b8c354SEduardo Habkost         (long)memmap[SIFIVE_U_DEV_L2CC].base);
3336eaf9cf5SBin Meng     qemu_fdt_add_subnode(fdt, nodename);
3346eaf9cf5SBin Meng     qemu_fdt_setprop_cells(fdt, nodename, "reg",
33513b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_L2CC].base,
33613b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_L2CC].size);
3376eaf9cf5SBin Meng     qemu_fdt_setprop_cells(fdt, nodename, "interrupts",
3386eaf9cf5SBin Meng         SIFIVE_U_L2CC_IRQ0, SIFIVE_U_L2CC_IRQ1, SIFIVE_U_L2CC_IRQ2);
3396eaf9cf5SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle);
3406eaf9cf5SBin Meng     qemu_fdt_setprop(fdt, nodename, "cache-unified", NULL, 0);
3416eaf9cf5SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "cache-size", 2097152);
3426eaf9cf5SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "cache-sets", 1024);
3436eaf9cf5SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "cache-level", 2);
3446eaf9cf5SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "cache-block-size", 64);
3456eaf9cf5SBin Meng     qemu_fdt_setprop_string(fdt, nodename, "compatible",
3466eaf9cf5SBin Meng                             "sifive,fu540-c000-ccache");
3476eaf9cf5SBin Meng     g_free(nodename);
3486eaf9cf5SBin Meng 
349145b2991SBin Meng     nodename = g_strdup_printf("/soc/spi@%lx",
350722f1352SBin Meng         (long)memmap[SIFIVE_U_DEV_QSPI2].base);
351722f1352SBin Meng     qemu_fdt_add_subnode(fdt, nodename);
352722f1352SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "#size-cells", 0);
353722f1352SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "#address-cells", 1);
354722f1352SBin Meng     qemu_fdt_setprop_cells(fdt, nodename, "clocks",
355722f1352SBin Meng         prci_phandle, PRCI_CLK_TLCLK);
356722f1352SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "interrupts", SIFIVE_U_QSPI2_IRQ);
357722f1352SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle);
358722f1352SBin Meng     qemu_fdt_setprop_cells(fdt, nodename, "reg",
359722f1352SBin Meng         0x0, memmap[SIFIVE_U_DEV_QSPI2].base,
360722f1352SBin Meng         0x0, memmap[SIFIVE_U_DEV_QSPI2].size);
361722f1352SBin Meng     qemu_fdt_setprop_string(fdt, nodename, "compatible", "sifive,spi0");
362722f1352SBin Meng     g_free(nodename);
363722f1352SBin Meng 
364722f1352SBin Meng     nodename = g_strdup_printf("/soc/spi@%lx/mmc@0",
365722f1352SBin Meng         (long)memmap[SIFIVE_U_DEV_QSPI2].base);
366722f1352SBin Meng     qemu_fdt_add_subnode(fdt, nodename);
367722f1352SBin Meng     qemu_fdt_setprop(fdt, nodename, "disable-wp", NULL, 0);
368722f1352SBin Meng     qemu_fdt_setprop_cells(fdt, nodename, "voltage-ranges", 3300, 3300);
369722f1352SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "spi-max-frequency", 20000000);
370722f1352SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "reg", 0);
371722f1352SBin Meng     qemu_fdt_setprop_string(fdt, nodename, "compatible", "mmc-spi-slot");
372722f1352SBin Meng     g_free(nodename);
373722f1352SBin Meng 
374722f1352SBin Meng     nodename = g_strdup_printf("/soc/spi@%lx",
375145b2991SBin Meng         (long)memmap[SIFIVE_U_DEV_QSPI0].base);
376145b2991SBin Meng     qemu_fdt_add_subnode(fdt, nodename);
377145b2991SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "#size-cells", 0);
378145b2991SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "#address-cells", 1);
379145b2991SBin Meng     qemu_fdt_setprop_cells(fdt, nodename, "clocks",
380145b2991SBin Meng         prci_phandle, PRCI_CLK_TLCLK);
381145b2991SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "interrupts", SIFIVE_U_QSPI0_IRQ);
382145b2991SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle);
383145b2991SBin Meng     qemu_fdt_setprop_cells(fdt, nodename, "reg",
384145b2991SBin Meng         0x0, memmap[SIFIVE_U_DEV_QSPI0].base,
385145b2991SBin Meng         0x0, memmap[SIFIVE_U_DEV_QSPI0].size);
386145b2991SBin Meng     qemu_fdt_setprop_string(fdt, nodename, "compatible", "sifive,spi0");
387145b2991SBin Meng     g_free(nodename);
388145b2991SBin Meng 
389145b2991SBin Meng     nodename = g_strdup_printf("/soc/spi@%lx/flash@0",
390145b2991SBin Meng         (long)memmap[SIFIVE_U_DEV_QSPI0].base);
391145b2991SBin Meng     qemu_fdt_add_subnode(fdt, nodename);
392145b2991SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "spi-rx-bus-width", 4);
393145b2991SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "spi-tx-bus-width", 4);
394145b2991SBin Meng     qemu_fdt_setprop(fdt, nodename, "m25p,fast-read", NULL, 0);
395145b2991SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "spi-max-frequency", 50000000);
396145b2991SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "reg", 0);
397145b2991SBin Meng     qemu_fdt_setprop_string(fdt, nodename, "compatible", "jedec,spi-nor");
398145b2991SBin Meng     g_free(nodename);
399145b2991SBin Meng 
4007b6bb66fSBin Meng     phy_phandle = phandle++;
4015a7f76a3SAlistair Francis     nodename = g_strdup_printf("/soc/ethernet@%lx",
40213b8c354SEduardo Habkost         (long)memmap[SIFIVE_U_DEV_GEM].base);
4035a7f76a3SAlistair Francis     qemu_fdt_add_subnode(fdt, nodename);
4047b6bb66fSBin Meng     qemu_fdt_setprop_string(fdt, nodename, "compatible",
4057b6bb66fSBin Meng         "sifive,fu540-c000-gem");
4065a7f76a3SAlistair Francis     qemu_fdt_setprop_cells(fdt, nodename, "reg",
40713b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_GEM].base,
40813b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_GEM].size,
40913b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_GEM_MGMT].base,
41013b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_GEM_MGMT].size);
4115a7f76a3SAlistair Francis     qemu_fdt_setprop_string(fdt, nodename, "reg-names", "control");
4125a7f76a3SAlistair Francis     qemu_fdt_setprop_string(fdt, nodename, "phy-mode", "gmii");
4137b6bb66fSBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "phy-handle", phy_phandle);
41404e7edd1SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle);
41504e7edd1SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "interrupts", SIFIVE_U_GEM_IRQ);
416fe93582cSAnup Patel     qemu_fdt_setprop_cells(fdt, nodename, "clocks",
417806c64b7SBin Meng         prci_phandle, PRCI_CLK_GEMGXLPLL, prci_phandle, PRCI_CLK_GEMGXLPLL);
418cb53b283SBin Meng     qemu_fdt_setprop_string_array(fdt, nodename, "clock-names",
419cb53b283SBin Meng         (char **)&ethclk_names, ARRAY_SIZE(ethclk_names));
4207b6bb66fSBin Meng     qemu_fdt_setprop(fdt, nodename, "local-mac-address",
4217b6bb66fSBin Meng         s->soc.gem.conf.macaddr.a, ETH_ALEN);
42204e7edd1SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "#address-cells", 1);
42304e7edd1SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "#size-cells", 0);
424c3a28b5dSBin Meng 
425c3a28b5dSBin Meng     qemu_fdt_add_subnode(fdt, "/aliases");
426c3a28b5dSBin Meng     qemu_fdt_setprop_string(fdt, "/aliases", "ethernet0", nodename);
427c3a28b5dSBin Meng 
4285a7f76a3SAlistair Francis     g_free(nodename);
4295a7f76a3SAlistair Francis 
4305a7f76a3SAlistair Francis     nodename = g_strdup_printf("/soc/ethernet@%lx/ethernet-phy@0",
43113b8c354SEduardo Habkost         (long)memmap[SIFIVE_U_DEV_GEM].base);
4325a7f76a3SAlistair Francis     qemu_fdt_add_subnode(fdt, nodename);
4337b6bb66fSBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "phandle", phy_phandle);
43404e7edd1SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "reg", 0x0);
4355a7f76a3SAlistair Francis     g_free(nodename);
4365a7f76a3SAlistair Francis 
437ea6eaa06SAlistair Francis     nodename = g_strdup_printf("/soc/pwm@%lx",
438ea6eaa06SAlistair Francis         (long)memmap[SIFIVE_U_DEV_PWM0].base);
439ea6eaa06SAlistair Francis     qemu_fdt_add_subnode(fdt, nodename);
440ea6eaa06SAlistair Francis     qemu_fdt_setprop_string(fdt, nodename, "compatible", "sifive,pwm0");
441ea6eaa06SAlistair Francis     qemu_fdt_setprop_cells(fdt, nodename, "reg",
442ea6eaa06SAlistair Francis         0x0, memmap[SIFIVE_U_DEV_PWM0].base,
443ea6eaa06SAlistair Francis         0x0, memmap[SIFIVE_U_DEV_PWM0].size);
444ea6eaa06SAlistair Francis     qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle);
445ea6eaa06SAlistair Francis     qemu_fdt_setprop_cells(fdt, nodename, "interrupts",
446ea6eaa06SAlistair Francis                            SIFIVE_U_PWM0_IRQ0, SIFIVE_U_PWM0_IRQ1,
447ea6eaa06SAlistair Francis                            SIFIVE_U_PWM0_IRQ2, SIFIVE_U_PWM0_IRQ3);
448ea6eaa06SAlistair Francis     qemu_fdt_setprop_cells(fdt, nodename, "clocks",
449ea6eaa06SAlistair Francis                            prci_phandle, PRCI_CLK_TLCLK);
450ea6eaa06SAlistair Francis     qemu_fdt_setprop_cell(fdt, nodename, "#pwm-cells", 0);
451ea6eaa06SAlistair Francis     g_free(nodename);
452ea6eaa06SAlistair Francis 
453ea6eaa06SAlistair Francis     nodename = g_strdup_printf("/soc/pwm@%lx",
454ea6eaa06SAlistair Francis         (long)memmap[SIFIVE_U_DEV_PWM1].base);
455ea6eaa06SAlistair Francis     qemu_fdt_add_subnode(fdt, nodename);
456ea6eaa06SAlistair Francis     qemu_fdt_setprop_string(fdt, nodename, "compatible", "sifive,pwm0");
457ea6eaa06SAlistair Francis     qemu_fdt_setprop_cells(fdt, nodename, "reg",
458ea6eaa06SAlistair Francis         0x0, memmap[SIFIVE_U_DEV_PWM1].base,
459ea6eaa06SAlistair Francis         0x0, memmap[SIFIVE_U_DEV_PWM1].size);
460ea6eaa06SAlistair Francis     qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle);
461ea6eaa06SAlistair Francis     qemu_fdt_setprop_cells(fdt, nodename, "interrupts",
462ea6eaa06SAlistair Francis                            SIFIVE_U_PWM1_IRQ0, SIFIVE_U_PWM1_IRQ1,
463ea6eaa06SAlistair Francis                            SIFIVE_U_PWM1_IRQ2, SIFIVE_U_PWM1_IRQ3);
464ea6eaa06SAlistair Francis     qemu_fdt_setprop_cells(fdt, nodename, "clocks",
465ea6eaa06SAlistair Francis                            prci_phandle, PRCI_CLK_TLCLK);
466ea6eaa06SAlistair Francis     qemu_fdt_setprop_cell(fdt, nodename, "#pwm-cells", 0);
467ea6eaa06SAlistair Francis     g_free(nodename);
468ea6eaa06SAlistair Francis 
4695f7134d3SBin Meng     nodename = g_strdup_printf("/soc/serial@%lx",
47010b43754SAnup Patel         (long)memmap[SIFIVE_U_DEV_UART1].base);
47110b43754SAnup Patel     qemu_fdt_add_subnode(fdt, nodename);
47210b43754SAnup Patel     qemu_fdt_setprop_string(fdt, nodename, "compatible", "sifive,uart0");
47310b43754SAnup Patel     qemu_fdt_setprop_cells(fdt, nodename, "reg",
47410b43754SAnup Patel         0x0, memmap[SIFIVE_U_DEV_UART1].base,
47510b43754SAnup Patel         0x0, memmap[SIFIVE_U_DEV_UART1].size);
47610b43754SAnup Patel     qemu_fdt_setprop_cells(fdt, nodename, "clocks",
47710b43754SAnup Patel         prci_phandle, PRCI_CLK_TLCLK);
47810b43754SAnup Patel     qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle);
47910b43754SAnup Patel     qemu_fdt_setprop_cell(fdt, nodename, "interrupts", SIFIVE_U_UART1_IRQ);
48010b43754SAnup Patel 
48110b43754SAnup Patel     qemu_fdt_setprop_string(fdt, "/aliases", "serial1", nodename);
48210b43754SAnup Patel     g_free(nodename);
48310b43754SAnup Patel 
48410b43754SAnup Patel     nodename = g_strdup_printf("/soc/serial@%lx",
48513b8c354SEduardo Habkost         (long)memmap[SIFIVE_U_DEV_UART0].base);
486a7240d1eSMichael Clark     qemu_fdt_add_subnode(fdt, nodename);
487a7240d1eSMichael Clark     qemu_fdt_setprop_string(fdt, nodename, "compatible", "sifive,uart0");
488a7240d1eSMichael Clark     qemu_fdt_setprop_cells(fdt, nodename, "reg",
48913b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_UART0].base,
49013b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_UART0].size);
491806c64b7SBin Meng     qemu_fdt_setprop_cells(fdt, nodename, "clocks",
492806c64b7SBin Meng         prci_phandle, PRCI_CLK_TLCLK);
49304e7edd1SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle);
49404e7edd1SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "interrupts", SIFIVE_U_UART0_IRQ);
495a7240d1eSMichael Clark 
496a7240d1eSMichael Clark     qemu_fdt_add_subnode(fdt, "/chosen");
497a7240d1eSMichael Clark     qemu_fdt_setprop_string(fdt, "/chosen", "stdout-path", nodename);
49844e6dcd3SGuenter Roeck     qemu_fdt_setprop_string(fdt, "/aliases", "serial0", nodename);
49944e6dcd3SGuenter Roeck 
500a7240d1eSMichael Clark     g_free(nodename);
501a7240d1eSMichael Clark }
502a7240d1eSMichael Clark 
sifive_u_machine_reset(void * opaque,int n,int level)5035133ed17SBin Meng static void sifive_u_machine_reset(void *opaque, int n, int level)
5045133ed17SBin Meng {
5055133ed17SBin Meng     /* gpio pin active low triggers reset */
5065133ed17SBin Meng     if (!level) {
5075133ed17SBin Meng         qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
5085133ed17SBin Meng     }
5095133ed17SBin Meng }
5105133ed17SBin Meng 
sifive_u_machine_init(MachineState * machine)511523e3464SAlistair Francis static void sifive_u_machine_init(MachineState *machine)
512a7240d1eSMichael Clark {
51373261285SBin Meng     const MemMapEntry *memmap = sifive_u_memmap;
514687caef1SAlistair Francis     SiFiveUState *s = RISCV_U_MACHINE(machine);
5155aec3247SMichael Clark     MemoryRegion *system_memory = get_system_memory();
5161b3a2308SAlistair Francis     MemoryRegion *flash0 = g_new(MemoryRegion, 1);
51755c13659SSamuel Holland     hwaddr start_addr = memmap[SIFIVE_U_DEV_DRAM].base;
51838bc4e34SAlistair Francis     target_ulong firmware_end_addr, kernel_start_addr;
5199d3f7108SDaniel Henrique Barboza     const char *firmware_name;
5208590f536SAtish Patra     uint32_t start_addr_hi32 = 0x00000000;
5215aec3247SMichael Clark     int i;
52266b1205bSAtish Patra     uint32_t fdt_load_addr;
523dc144fe1SAtish Patra     uint64_t kernel_entry;
524145b2991SBin Meng     DriveInfo *dinfo;
52536aa285fSMarkus Armbruster     BlockBackend *blk;
52636aa285fSMarkus Armbruster     DeviceState *flash_dev, *sd_dev, *card_dev;
527722f1352SBin Meng     qemu_irq flash_cs, sd_cs;
528a7240d1eSMichael Clark 
5292308092bSAlistair Francis     /* Initialize SoC */
5309fc7fc4dSMarkus Armbruster     object_initialize_child(OBJECT(machine), "soc", &s->soc, TYPE_RISCV_U_SOC);
5315325cc34SMarkus Armbruster     object_property_set_uint(OBJECT(&s->soc), "serial", s->serial,
5323ca109c3SBin Meng                              &error_abort);
533099be035SAlistair Francis     object_property_set_str(OBJECT(&s->soc), "cpu-type", machine->cpu_type,
534099be035SAlistair Francis                              &error_abort);
5358f972e5bSAlistair Francis     qdev_realize(DEVICE(&s->soc), NULL, &error_fatal);
536a7240d1eSMichael Clark 
537a7240d1eSMichael Clark     /* register RAM */
53813b8c354SEduardo Habkost     memory_region_add_subregion(system_memory, memmap[SIFIVE_U_DEV_DRAM].base,
539c188a9c4SBin Meng                                 machine->ram);
540a7240d1eSMichael Clark 
5411b3a2308SAlistair Francis     /* register QSPI0 Flash */
5421b3a2308SAlistair Francis     memory_region_init_ram(flash0, NULL, "riscv.sifive.u.flash0",
54313b8c354SEduardo Habkost                            memmap[SIFIVE_U_DEV_FLASH0].size, &error_fatal);
54413b8c354SEduardo Habkost     memory_region_add_subregion(system_memory, memmap[SIFIVE_U_DEV_FLASH0].base,
5451b3a2308SAlistair Francis                                 flash0);
5461b3a2308SAlistair Francis 
5475133ed17SBin Meng     /* register gpio-restart */
5485133ed17SBin Meng     qdev_connect_gpio_out(DEVICE(&(s->soc.gpio)), 10,
5495133ed17SBin Meng                           qemu_allocate_irq(sifive_u_machine_reset, NULL, 0));
5505133ed17SBin Meng 
551fc9ec362SBin Meng     /* load/create device tree */
552fc9ec362SBin Meng     if (machine->dtb) {
553fc9ec362SBin Meng         machine->fdt = load_device_tree(machine->dtb, &s->fdt_size);
554fc9ec362SBin Meng         if (!machine->fdt) {
555fc9ec362SBin Meng             error_report("load_device_tree() failed");
556fc9ec362SBin Meng             exit(1);
557fc9ec362SBin Meng         }
558fc9ec362SBin Meng     } else {
559f5be2ccbSDaniel Henrique Barboza         create_fdt(s, memmap, riscv_is_32bit(&s->soc.u_cpus));
560fc9ec362SBin Meng     }
561a7240d1eSMichael Clark 
56217aad9f2SBin Meng     if (s->start_in_flash) {
56317aad9f2SBin Meng         /*
56417aad9f2SBin Meng          * If start_in_flash property is given, assign s->msel to a value
56517aad9f2SBin Meng          * that representing booting from QSPI0 memory-mapped flash.
56617aad9f2SBin Meng          *
56717aad9f2SBin Meng          * This also means that when both start_in_flash and msel properties
56817aad9f2SBin Meng          * are given, start_in_flash takes the precedence over msel.
56917aad9f2SBin Meng          *
57017aad9f2SBin Meng          * Note this is to keep backward compatibility not to break existing
57117aad9f2SBin Meng          * users that use start_in_flash property.
57217aad9f2SBin Meng          */
57317aad9f2SBin Meng         s->msel = MSEL_MEMMAP_QSPI0_FLASH;
57417aad9f2SBin Meng     }
57517aad9f2SBin Meng 
57617aad9f2SBin Meng     switch (s->msel) {
57717aad9f2SBin Meng     case MSEL_MEMMAP_QSPI0_FLASH:
57813b8c354SEduardo Habkost         start_addr = memmap[SIFIVE_U_DEV_FLASH0].base;
57917aad9f2SBin Meng         break;
58017aad9f2SBin Meng     case MSEL_L2LIM_QSPI0_FLASH:
58117aad9f2SBin Meng     case MSEL_L2LIM_QSPI2_SD:
58213b8c354SEduardo Habkost         start_addr = memmap[SIFIVE_U_DEV_L2LIM].base;
58317aad9f2SBin Meng         break;
58417aad9f2SBin Meng     default:
58513b8c354SEduardo Habkost         start_addr = memmap[SIFIVE_U_DEV_DRAM].base;
58617aad9f2SBin Meng         break;
58717aad9f2SBin Meng     }
58817aad9f2SBin Meng 
5899d3f7108SDaniel Henrique Barboza     firmware_name = riscv_default_firmware_name(&s->soc.u_cpus);
5909d3f7108SDaniel Henrique Barboza     firmware_end_addr = riscv_find_and_load_firmware(machine, firmware_name,
59155c13659SSamuel Holland                                                      &start_addr, NULL);
592b3042223SAlistair Francis 
593a7240d1eSMichael Clark     if (machine->kernel_filename) {
594a8259b53SAlistair Francis         kernel_start_addr = riscv_calc_kernel_start_addr(&s->soc.u_cpus,
59538bc4e34SAlistair Francis                                                          firmware_end_addr);
59638bc4e34SAlistair Francis 
59762c5bc34SDaniel Henrique Barboza         kernel_entry = riscv_load_kernel(machine, &s->soc.u_cpus,
598487d73fcSDaniel Henrique Barboza                                          kernel_start_addr, true, NULL);
599dc144fe1SAtish Patra     } else {
600dc144fe1SAtish Patra        /*
601dc144fe1SAtish Patra         * If dynamic firmware is used, it doesn't know where is the next mode
602dc144fe1SAtish Patra         * if kernel argument is not set.
603dc144fe1SAtish Patra         */
604dc144fe1SAtish Patra         kernel_entry = 0;
605a7240d1eSMichael Clark     }
606a7240d1eSMichael Clark 
607bc2c0153SDaniel Henrique Barboza     fdt_load_addr = riscv_compute_fdt_addr(memmap[SIFIVE_U_DEV_DRAM].base,
6084b402886SDaniel Henrique Barboza                                            memmap[SIFIVE_U_DEV_DRAM].size,
6094b402886SDaniel Henrique Barboza                                            machine);
610bc2c0153SDaniel Henrique Barboza     riscv_load_fdt(fdt_load_addr, machine->fdt);
611bc2c0153SDaniel Henrique Barboza 
612a8259b53SAlistair Francis     if (!riscv_is_32bit(&s->soc.u_cpus)) {
6132206ffa6SAlistair Francis         start_addr_hi32 = (uint64_t)start_addr >> 32;
6142206ffa6SAlistair Francis     }
61566b1205bSAtish Patra 
616a7240d1eSMichael Clark     /* reset vector */
617623d53cbSBin Meng     uint32_t reset_vec[12] = {
61817aad9f2SBin Meng         s->msel,                       /* MSEL pin state */
619dc144fe1SAtish Patra         0x00000297,                    /* 1:  auipc  t0, %pcrel_hi(fw_dyn) */
620623d53cbSBin Meng         0x02c28613,                    /*     addi   a2, t0, %pcrel_lo(1b) */
621a7240d1eSMichael Clark         0xf1402573,                    /*     csrr   a0, mhartid  */
6222206ffa6SAlistair Francis         0,
6232206ffa6SAlistair Francis         0,
624a7240d1eSMichael Clark         0x00028067,                    /*     jr     t0 */
625fc41ae23SAlistair Francis         start_addr,                    /* start: .dword */
6268590f536SAtish Patra         start_addr_hi32,
62766b1205bSAtish Patra         fdt_load_addr,                 /* fdt_laddr: .dword */
62866b1205bSAtish Patra         0x00000000,
629623d53cbSBin Meng         0x00000000,
630dc144fe1SAtish Patra                                        /* fw_dyn: */
631a7240d1eSMichael Clark     };
632a8259b53SAlistair Francis     if (riscv_is_32bit(&s->soc.u_cpus)) {
6332206ffa6SAlistair Francis         reset_vec[4] = 0x0202a583;     /*     lw     a1, 32(t0) */
6342206ffa6SAlistair Francis         reset_vec[5] = 0x0182a283;     /*     lw     t0, 24(t0) */
6352206ffa6SAlistair Francis     } else {
6362206ffa6SAlistair Francis         reset_vec[4] = 0x0202b583;     /*     ld     a1, 32(t0) */
6372206ffa6SAlistair Francis         reset_vec[5] = 0x0182b283;     /*     ld     t0, 24(t0) */
6382206ffa6SAlistair Francis     }
6392206ffa6SAlistair Francis 
640a7240d1eSMichael Clark 
6415aec3247SMichael Clark     /* copy in the reset vector in little_endian byte order */
64266b1205bSAtish Patra     for (i = 0; i < ARRAY_SIZE(reset_vec); i++) {
6435aec3247SMichael Clark         reset_vec[i] = cpu_to_le32(reset_vec[i]);
6445aec3247SMichael Clark     }
6455aec3247SMichael Clark     rom_add_blob_fixed_as("mrom.reset", reset_vec, sizeof(reset_vec),
64613b8c354SEduardo Habkost                           memmap[SIFIVE_U_DEV_MROM].base, &address_space_memory);
647dc144fe1SAtish Patra 
648*65838488STANG Tiancheng     riscv_rom_copy_firmware_info(machine, &s->soc.u_cpus,
649*65838488STANG Tiancheng                                  memmap[SIFIVE_U_DEV_MROM].base,
65013b8c354SEduardo Habkost                                  memmap[SIFIVE_U_DEV_MROM].size,
651dc144fe1SAtish Patra                                  sizeof(reset_vec), kernel_entry);
652145b2991SBin Meng 
653145b2991SBin Meng     /* Connect an SPI flash to SPI0 */
654145b2991SBin Meng     flash_dev = qdev_new("is25wp256");
65564eaa820SMarkus Armbruster     dinfo = drive_get(IF_MTD, 0, 0);
656145b2991SBin Meng     if (dinfo) {
657145b2991SBin Meng         qdev_prop_set_drive_err(flash_dev, "drive",
658145b2991SBin Meng                                 blk_by_legacy_dinfo(dinfo),
659145b2991SBin Meng                                 &error_fatal);
660145b2991SBin Meng     }
661145b2991SBin Meng     qdev_realize_and_unref(flash_dev, BUS(s->soc.spi0.spi), &error_fatal);
662145b2991SBin Meng 
663145b2991SBin Meng     flash_cs = qdev_get_gpio_in_named(flash_dev, SSI_GPIO_CS, 0);
664145b2991SBin Meng     sysbus_connect_irq(SYS_BUS_DEVICE(&s->soc.spi0), 1, flash_cs);
665722f1352SBin Meng 
666722f1352SBin Meng     /* Connect an SD card to SPI2 */
667722f1352SBin Meng     sd_dev = ssi_create_peripheral(s->soc.spi2.spi, "ssi-sd");
668722f1352SBin Meng 
669722f1352SBin Meng     sd_cs = qdev_get_gpio_in_named(sd_dev, SSI_GPIO_CS, 0);
670722f1352SBin Meng     sysbus_connect_irq(SYS_BUS_DEVICE(&s->soc.spi2), 1, sd_cs);
67136aa285fSMarkus Armbruster 
67236aa285fSMarkus Armbruster     dinfo = drive_get(IF_SD, 0, 0);
67336aa285fSMarkus Armbruster     blk = dinfo ? blk_by_legacy_dinfo(dinfo) : NULL;
674c3287c0fSCédric Le Goater     card_dev = qdev_new(TYPE_SD_CARD_SPI);
67536aa285fSMarkus Armbruster     qdev_prop_set_drive_err(card_dev, "drive", blk, &error_fatal);
67636aa285fSMarkus Armbruster     qdev_realize_and_unref(card_dev,
67736aa285fSMarkus Armbruster                            qdev_get_child_bus(sd_dev, "sd-bus"),
67836aa285fSMarkus Armbruster                            &error_fatal);
6792308092bSAlistair Francis }
6802308092bSAlistair Francis 
sifive_u_machine_get_start_in_flash(Object * obj,Error ** errp)681523e3464SAlistair Francis static bool sifive_u_machine_get_start_in_flash(Object *obj, Error **errp)
682523e3464SAlistair Francis {
683523e3464SAlistair Francis     SiFiveUState *s = RISCV_U_MACHINE(obj);
684523e3464SAlistair Francis 
685523e3464SAlistair Francis     return s->start_in_flash;
686523e3464SAlistair Francis }
687523e3464SAlistair Francis 
sifive_u_machine_set_start_in_flash(Object * obj,bool value,Error ** errp)688523e3464SAlistair Francis static void sifive_u_machine_set_start_in_flash(Object *obj, bool value, Error **errp)
689523e3464SAlistair Francis {
690523e3464SAlistair Francis     SiFiveUState *s = RISCV_U_MACHINE(obj);
691523e3464SAlistair Francis 
692523e3464SAlistair Francis     s->start_in_flash = value;
693523e3464SAlistair Francis }
694523e3464SAlistair Francis 
sifive_u_machine_instance_init(Object * obj)695523e3464SAlistair Francis static void sifive_u_machine_instance_init(Object *obj)
696523e3464SAlistair Francis {
697523e3464SAlistair Francis     SiFiveUState *s = RISCV_U_MACHINE(obj);
698523e3464SAlistair Francis 
699523e3464SAlistair Francis     s->start_in_flash = false;
700cfa32630SBin Meng     s->msel = 0;
70196c7fff7SBernhard Beschow     object_property_add_uint32_ptr(obj, "msel", &s->msel,
70296c7fff7SBernhard Beschow                                    OBJ_PROP_FLAG_READWRITE);
703cfa32630SBin Meng     object_property_set_description(obj, "msel",
704cfa32630SBin Meng                                     "Mode Select (MSEL[3:0]) pin state");
705cfa32630SBin Meng 
7063ca109c3SBin Meng     s->serial = OTP_SERIAL;
70796c7fff7SBernhard Beschow     object_property_add_uint32_ptr(obj, "serial", &s->serial,
70896c7fff7SBernhard Beschow                                    OBJ_PROP_FLAG_READWRITE);
7097eecec7dSMarkus Armbruster     object_property_set_description(obj, "serial", "Board serial number");
710523e3464SAlistair Francis }
711523e3464SAlistair Francis 
sifive_u_machine_class_init(ObjectClass * oc,void * data)712523e3464SAlistair Francis static void sifive_u_machine_class_init(ObjectClass *oc, void *data)
713523e3464SAlistair Francis {
714523e3464SAlistair Francis     MachineClass *mc = MACHINE_CLASS(oc);
715523e3464SAlistair Francis 
716523e3464SAlistair Francis     mc->desc = "RISC-V Board compatible with SiFive U SDK";
717523e3464SAlistair Francis     mc->init = sifive_u_machine_init;
718523e3464SAlistair Francis     mc->max_cpus = SIFIVE_U_MANAGEMENT_CPU_COUNT + SIFIVE_U_COMPUTE_CPU_COUNT;
719523e3464SAlistair Francis     mc->min_cpus = SIFIVE_U_MANAGEMENT_CPU_COUNT + 1;
7201eaada8aSBin Meng     mc->default_cpu_type = SIFIVE_U_CPU;
721523e3464SAlistair Francis     mc->default_cpus = mc->min_cpus;
722c188a9c4SBin Meng     mc->default_ram_id = "riscv.sifive.u.ram";
723418b473eSEduardo Habkost 
724418b473eSEduardo Habkost     object_class_property_add_bool(oc, "start-in-flash",
725418b473eSEduardo Habkost                                    sifive_u_machine_get_start_in_flash,
726418b473eSEduardo Habkost                                    sifive_u_machine_set_start_in_flash);
727418b473eSEduardo Habkost     object_class_property_set_description(oc, "start-in-flash",
728418b473eSEduardo Habkost                                           "Set on to tell QEMU's ROM to jump to "
729418b473eSEduardo Habkost                                           "flash. Otherwise QEMU will jump to DRAM "
730418b473eSEduardo Habkost                                           "or L2LIM depending on the msel value");
731523e3464SAlistair Francis }
732523e3464SAlistair Francis 
733523e3464SAlistair Francis static const TypeInfo sifive_u_machine_typeinfo = {
734523e3464SAlistair Francis     .name       = MACHINE_TYPE_NAME("sifive_u"),
735523e3464SAlistair Francis     .parent     = TYPE_MACHINE,
736523e3464SAlistair Francis     .class_init = sifive_u_machine_class_init,
737523e3464SAlistair Francis     .instance_init = sifive_u_machine_instance_init,
738523e3464SAlistair Francis     .instance_size = sizeof(SiFiveUState),
739523e3464SAlistair Francis };
740523e3464SAlistair Francis 
sifive_u_machine_init_register_types(void)741523e3464SAlistair Francis static void sifive_u_machine_init_register_types(void)
742523e3464SAlistair Francis {
743523e3464SAlistair Francis     type_register_static(&sifive_u_machine_typeinfo);
744523e3464SAlistair Francis }
745523e3464SAlistair Francis 
type_init(sifive_u_machine_init_register_types)746523e3464SAlistair Francis type_init(sifive_u_machine_init_register_types)
747523e3464SAlistair Francis 
748139177b1SBin Meng static void sifive_u_soc_instance_init(Object *obj)
7492308092bSAlistair Francis {
7502308092bSAlistair Francis     SiFiveUSoCState *s = RISCV_U_SOC(obj);
7512308092bSAlistair Francis 
7529fc7fc4dSMarkus Armbruster     object_initialize_child(obj, "e-cluster", &s->e_cluster, TYPE_CPU_CLUSTER);
753ecdfe393SBin Meng     qdev_prop_set_uint32(DEVICE(&s->e_cluster), "cluster-id", 0);
754ecdfe393SBin Meng 
755db873cc5SMarkus Armbruster     object_initialize_child(OBJECT(&s->e_cluster), "e-cpus", &s->e_cpus,
75675a6ed87SMarkus Armbruster                             TYPE_RISCV_HART_ARRAY);
757ecdfe393SBin Meng     qdev_prop_set_uint32(DEVICE(&s->e_cpus), "num-harts", 1);
758ecdfe393SBin Meng     qdev_prop_set_uint32(DEVICE(&s->e_cpus), "hartid-base", 0);
759ecdfe393SBin Meng     qdev_prop_set_string(DEVICE(&s->e_cpus), "cpu-type", SIFIVE_E_CPU);
76073f6ed97SBin Meng     qdev_prop_set_uint64(DEVICE(&s->e_cpus), "resetvec", 0x1004);
761ecdfe393SBin Meng 
7629fc7fc4dSMarkus Armbruster     object_initialize_child(obj, "u-cluster", &s->u_cluster, TYPE_CPU_CLUSTER);
763ecdfe393SBin Meng     qdev_prop_set_uint32(DEVICE(&s->u_cluster), "cluster-id", 1);
764ecdfe393SBin Meng 
765db873cc5SMarkus Armbruster     object_initialize_child(OBJECT(&s->u_cluster), "u-cpus", &s->u_cpus,
76675a6ed87SMarkus Armbruster                             TYPE_RISCV_HART_ARRAY);
7675a7f76a3SAlistair Francis 
768db873cc5SMarkus Armbruster     object_initialize_child(obj, "prci", &s->prci, TYPE_SIFIVE_U_PRCI);
769db873cc5SMarkus Armbruster     object_initialize_child(obj, "otp", &s->otp, TYPE_SIFIVE_U_OTP);
770db873cc5SMarkus Armbruster     object_initialize_child(obj, "gem", &s->gem, TYPE_CADENCE_GEM);
7718a88b9f5SBin Meng     object_initialize_child(obj, "gpio", &s->gpio, TYPE_SIFIVE_GPIO);
772834e027aSBin Meng     object_initialize_child(obj, "pdma", &s->dma, TYPE_SIFIVE_PDMA);
773145b2991SBin Meng     object_initialize_child(obj, "spi0", &s->spi0, TYPE_SIFIVE_SPI);
774722f1352SBin Meng     object_initialize_child(obj, "spi2", &s->spi2, TYPE_SIFIVE_SPI);
775ea6eaa06SAlistair Francis     object_initialize_child(obj, "pwm0", &s->pwm[0], TYPE_SIFIVE_PWM);
776ea6eaa06SAlistair Francis     object_initialize_child(obj, "pwm1", &s->pwm[1], TYPE_SIFIVE_PWM);
7772308092bSAlistair Francis }
7782308092bSAlistair Francis 
sifive_u_soc_realize(DeviceState * dev,Error ** errp)779139177b1SBin Meng static void sifive_u_soc_realize(DeviceState *dev, Error **errp)
7802308092bSAlistair Francis {
781c4473127SLike Xu     MachineState *ms = MACHINE(qdev_get_machine());
7822308092bSAlistair Francis     SiFiveUSoCState *s = RISCV_U_SOC(dev);
78373261285SBin Meng     const MemMapEntry *memmap = sifive_u_memmap;
7842308092bSAlistair Francis     MemoryRegion *system_memory = get_system_memory();
7852308092bSAlistair Francis     MemoryRegion *mask_rom = g_new(MemoryRegion, 1);
786a6902ef0SAlistair Francis     MemoryRegion *l2lim_mem = g_new(MemoryRegion, 1);
78705446f41SBin Meng     char *plic_hart_config;
788ea6eaa06SAlistair Francis     int i, j;
7892308092bSAlistair Francis 
790099be035SAlistair Francis     qdev_prop_set_uint32(DEVICE(&s->u_cpus), "num-harts", ms->smp.cpus - 1);
791099be035SAlistair Francis     qdev_prop_set_uint32(DEVICE(&s->u_cpus), "hartid-base", 1);
792099be035SAlistair Francis     qdev_prop_set_string(DEVICE(&s->u_cpus), "cpu-type", s->cpu_type);
793099be035SAlistair Francis     qdev_prop_set_uint64(DEVICE(&s->u_cpus), "resetvec", 0x1004);
794099be035SAlistair Francis 
79591a3387dSTsukasa OI     sysbus_realize(SYS_BUS_DEVICE(&s->e_cpus), &error_fatal);
79691a3387dSTsukasa OI     sysbus_realize(SYS_BUS_DEVICE(&s->u_cpus), &error_fatal);
797ecdfe393SBin Meng     /*
798ecdfe393SBin Meng      * The cluster must be realized after the RISC-V hart array container,
799ecdfe393SBin Meng      * as the container's CPU object is only created on realize, and the
800ecdfe393SBin Meng      * CPU must exist and have been parented into the cluster before the
801ecdfe393SBin Meng      * cluster is realized.
802ecdfe393SBin Meng      */
803ce189ab2SMarkus Armbruster     qdev_realize(DEVICE(&s->e_cluster), NULL, &error_abort);
804ce189ab2SMarkus Armbruster     qdev_realize(DEVICE(&s->u_cluster), NULL, &error_abort);
8052308092bSAlistair Francis 
8062308092bSAlistair Francis     /* boot rom */
807414c47d2SPhilippe Mathieu-Daudé     memory_region_init_rom(mask_rom, OBJECT(dev), "riscv.sifive.u.mrom",
80813b8c354SEduardo Habkost                            memmap[SIFIVE_U_DEV_MROM].size, &error_fatal);
80913b8c354SEduardo Habkost     memory_region_add_subregion(system_memory, memmap[SIFIVE_U_DEV_MROM].base,
8102308092bSAlistair Francis                                 mask_rom);
811a7240d1eSMichael Clark 
812a6902ef0SAlistair Francis     /*
813a6902ef0SAlistair Francis      * Add L2-LIM at reset size.
814a6902ef0SAlistair Francis      * This should be reduced in size as the L2 Cache Controller WayEnable
815a6902ef0SAlistair Francis      * register is incremented. Unfortunately I don't see a nice (or any) way
816a6902ef0SAlistair Francis      * to handle reducing or blocking out the L2 LIM while still allowing it
817a6902ef0SAlistair Francis      * be re returned to all enabled after a reset. For the time being, just
818a6902ef0SAlistair Francis      * leave it enabled all the time. This won't break anything, but will be
819a6902ef0SAlistair Francis      * too generous to misbehaving guests.
820a6902ef0SAlistair Francis      */
821a6902ef0SAlistair Francis     memory_region_init_ram(l2lim_mem, NULL, "riscv.sifive.u.l2lim",
82213b8c354SEduardo Habkost                            memmap[SIFIVE_U_DEV_L2LIM].size, &error_fatal);
82313b8c354SEduardo Habkost     memory_region_add_subregion(system_memory, memmap[SIFIVE_U_DEV_L2LIM].base,
824a6902ef0SAlistair Francis                                 l2lim_mem);
825a6902ef0SAlistair Francis 
82605446f41SBin Meng     /* create PLIC hart topology configuration string */
8274e8fb53cSAlistair Francis     plic_hart_config = riscv_plic_hart_config_string(ms->smp.cpus);
82805446f41SBin Meng 
829a7240d1eSMichael Clark     /* MMIO */
83013b8c354SEduardo Habkost     s->plic = sifive_plic_create(memmap[SIFIVE_U_DEV_PLIC].base,
831f436ecc3SAlistair Francis         plic_hart_config, ms->smp.cpus, 0,
832a7240d1eSMichael Clark         SIFIVE_U_PLIC_NUM_SOURCES,
833a7240d1eSMichael Clark         SIFIVE_U_PLIC_NUM_PRIORITIES,
834a7240d1eSMichael Clark         SIFIVE_U_PLIC_PRIORITY_BASE,
835a7240d1eSMichael Clark         SIFIVE_U_PLIC_PENDING_BASE,
836a7240d1eSMichael Clark         SIFIVE_U_PLIC_ENABLE_BASE,
837a7240d1eSMichael Clark         SIFIVE_U_PLIC_ENABLE_STRIDE,
838a7240d1eSMichael Clark         SIFIVE_U_PLIC_CONTEXT_BASE,
839a7240d1eSMichael Clark         SIFIVE_U_PLIC_CONTEXT_STRIDE,
84013b8c354SEduardo Habkost         memmap[SIFIVE_U_DEV_PLIC].size);
841bb8136dfSPan Nengyuan     g_free(plic_hart_config);
84213b8c354SEduardo Habkost     sifive_uart_create(system_memory, memmap[SIFIVE_U_DEV_UART0].base,
843647a70a1SAlistair Francis         serial_hd(0), qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_U_UART0_IRQ));
84413b8c354SEduardo Habkost     sifive_uart_create(system_memory, memmap[SIFIVE_U_DEV_UART1].base,
845194eef09SMichael Clark         serial_hd(1), qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_U_UART1_IRQ));
846b8fb878aSAnup Patel     riscv_aclint_swi_create(memmap[SIFIVE_U_DEV_CLINT].base, 0,
847b8fb878aSAnup Patel         ms->smp.cpus, false);
848b8fb878aSAnup Patel     riscv_aclint_mtimer_create(memmap[SIFIVE_U_DEV_CLINT].base +
849b8fb878aSAnup Patel             RISCV_ACLINT_SWI_SIZE,
850b8fb878aSAnup Patel         RISCV_ACLINT_DEFAULT_MTIMER_SIZE, 0, ms->smp.cpus,
851b8fb878aSAnup Patel         RISCV_ACLINT_DEFAULT_MTIMECMP, RISCV_ACLINT_DEFAULT_MTIME,
852074ca702SBin Meng         CLINT_TIMEBASE_FREQ, false);
8535a7f76a3SAlistair Francis 
854cbe3a8c5SMarkus Armbruster     if (!sysbus_realize(SYS_BUS_DEVICE(&s->prci), errp)) {
855cbe3a8c5SMarkus Armbruster         return;
856cbe3a8c5SMarkus Armbruster     }
85713b8c354SEduardo Habkost     sysbus_mmio_map(SYS_BUS_DEVICE(&s->prci), 0, memmap[SIFIVE_U_DEV_PRCI].base);
858af14c840SBin Meng 
8598a88b9f5SBin Meng     qdev_prop_set_uint32(DEVICE(&s->gpio), "ngpio", 16);
860cbe3a8c5SMarkus Armbruster     if (!sysbus_realize(SYS_BUS_DEVICE(&s->gpio), errp)) {
861cbe3a8c5SMarkus Armbruster         return;
862cbe3a8c5SMarkus Armbruster     }
86313b8c354SEduardo Habkost     sysbus_mmio_map(SYS_BUS_DEVICE(&s->gpio), 0, memmap[SIFIVE_U_DEV_GPIO].base);
8648a88b9f5SBin Meng 
8658a88b9f5SBin Meng     /* Pass all GPIOs to the SOC layer so they are available to the board */
8668a88b9f5SBin Meng     qdev_pass_gpios(DEVICE(&s->gpio), dev, NULL);
8678a88b9f5SBin Meng 
8688a88b9f5SBin Meng     /* Connect GPIO interrupts to the PLIC */
8698a88b9f5SBin Meng     for (i = 0; i < 16; i++) {
8708a88b9f5SBin Meng         sysbus_connect_irq(SYS_BUS_DEVICE(&s->gpio), i,
8718a88b9f5SBin Meng                            qdev_get_gpio_in(DEVICE(s->plic),
8728a88b9f5SBin Meng                                             SIFIVE_U_GPIO_IRQ0 + i));
8738a88b9f5SBin Meng     }
8748a88b9f5SBin Meng 
875834e027aSBin Meng     /* PDMA */
876834e027aSBin Meng     sysbus_realize(SYS_BUS_DEVICE(&s->dma), errp);
87713b8c354SEduardo Habkost     sysbus_mmio_map(SYS_BUS_DEVICE(&s->dma), 0, memmap[SIFIVE_U_DEV_PDMA].base);
878834e027aSBin Meng 
879834e027aSBin Meng     /* Connect PDMA interrupts to the PLIC */
880834e027aSBin Meng     for (i = 0; i < SIFIVE_PDMA_IRQS; i++) {
881834e027aSBin Meng         sysbus_connect_irq(SYS_BUS_DEVICE(&s->dma), i,
882834e027aSBin Meng                            qdev_get_gpio_in(DEVICE(s->plic),
883834e027aSBin Meng                                             SIFIVE_U_PDMA_IRQ0 + i));
884834e027aSBin Meng     }
885834e027aSBin Meng 
886fda5b000SAlistair Francis     qdev_prop_set_uint32(DEVICE(&s->otp), "serial", s->serial);
887cbe3a8c5SMarkus Armbruster     if (!sysbus_realize(SYS_BUS_DEVICE(&s->otp), errp)) {
888cbe3a8c5SMarkus Armbruster         return;
889cbe3a8c5SMarkus Armbruster     }
89013b8c354SEduardo Habkost     sysbus_mmio_map(SYS_BUS_DEVICE(&s->otp), 0, memmap[SIFIVE_U_DEV_OTP].base);
8915461c4feSBin Meng 
8920a7549dbSDavid Woodhouse     qemu_configure_nic_device(DEVICE(&s->gem), true, NULL);
8935325cc34SMarkus Armbruster     object_property_set_int(OBJECT(&s->gem), "revision", GEM_REVISION,
8945a7f76a3SAlistair Francis                             &error_abort);
895668f62ecSMarkus Armbruster     if (!sysbus_realize(SYS_BUS_DEVICE(&s->gem), errp)) {
8965a7f76a3SAlistair Francis         return;
8975a7f76a3SAlistair Francis     }
89813b8c354SEduardo Habkost     sysbus_mmio_map(SYS_BUS_DEVICE(&s->gem), 0, memmap[SIFIVE_U_DEV_GEM].base);
8995a7f76a3SAlistair Francis     sysbus_connect_irq(SYS_BUS_DEVICE(&s->gem), 0,
9005874f0a7SBin Meng                        qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_U_GEM_IRQ));
9017b6bb66fSBin Meng 
902ea6eaa06SAlistair Francis     /* PWM */
903ea6eaa06SAlistair Francis     for (i = 0; i < 2; i++) {
904ea6eaa06SAlistair Francis         if (!sysbus_realize(SYS_BUS_DEVICE(&s->pwm[i]), errp)) {
905ea6eaa06SAlistair Francis             return;
906ea6eaa06SAlistair Francis         }
907ea6eaa06SAlistair Francis         sysbus_mmio_map(SYS_BUS_DEVICE(&s->pwm[i]), 0,
908ea6eaa06SAlistair Francis                                 memmap[SIFIVE_U_DEV_PWM0].base + (0x1000 * i));
909ea6eaa06SAlistair Francis 
910ea6eaa06SAlistair Francis         /* Connect PWM interrupts to the PLIC */
911ea6eaa06SAlistair Francis         for (j = 0; j < SIFIVE_PWM_IRQS; j++) {
912ea6eaa06SAlistair Francis             sysbus_connect_irq(SYS_BUS_DEVICE(&s->pwm[i]), j,
913ea6eaa06SAlistair Francis                                qdev_get_gpio_in(DEVICE(s->plic),
914ea6eaa06SAlistair Francis                                         SIFIVE_U_PWM0_IRQ0 + (i * 4) + j));
915ea6eaa06SAlistair Francis         }
916ea6eaa06SAlistair Francis     }
917ea6eaa06SAlistair Francis 
9187b6bb66fSBin Meng     create_unimplemented_device("riscv.sifive.u.gem-mgmt",
91913b8c354SEduardo Habkost         memmap[SIFIVE_U_DEV_GEM_MGMT].base, memmap[SIFIVE_U_DEV_GEM_MGMT].size);
9203eaea6ebSBin Meng 
9213eaea6ebSBin Meng     create_unimplemented_device("riscv.sifive.u.dmc",
92213b8c354SEduardo Habkost         memmap[SIFIVE_U_DEV_DMC].base, memmap[SIFIVE_U_DEV_DMC].size);
9236eaf9cf5SBin Meng 
9246eaf9cf5SBin Meng     create_unimplemented_device("riscv.sifive.u.l2cc",
92513b8c354SEduardo Habkost         memmap[SIFIVE_U_DEV_L2CC].base, memmap[SIFIVE_U_DEV_L2CC].size);
926145b2991SBin Meng 
927145b2991SBin Meng     sysbus_realize(SYS_BUS_DEVICE(&s->spi0), errp);
928145b2991SBin Meng     sysbus_mmio_map(SYS_BUS_DEVICE(&s->spi0), 0,
929145b2991SBin Meng                     memmap[SIFIVE_U_DEV_QSPI0].base);
930145b2991SBin Meng     sysbus_connect_irq(SYS_BUS_DEVICE(&s->spi0), 0,
931145b2991SBin Meng                        qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_U_QSPI0_IRQ));
932722f1352SBin Meng     sysbus_realize(SYS_BUS_DEVICE(&s->spi2), errp);
933722f1352SBin Meng     sysbus_mmio_map(SYS_BUS_DEVICE(&s->spi2), 0,
934722f1352SBin Meng                     memmap[SIFIVE_U_DEV_QSPI2].base);
935722f1352SBin Meng     sysbus_connect_irq(SYS_BUS_DEVICE(&s->spi2), 0,
936722f1352SBin Meng                        qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_U_QSPI2_IRQ));
937a7240d1eSMichael Clark }
938a7240d1eSMichael Clark 
939139177b1SBin Meng static Property sifive_u_soc_props[] = {
940fda5b000SAlistair Francis     DEFINE_PROP_UINT32("serial", SiFiveUSoCState, serial, OTP_SERIAL),
941099be035SAlistair Francis     DEFINE_PROP_STRING("cpu-type", SiFiveUSoCState, cpu_type),
942fda5b000SAlistair Francis     DEFINE_PROP_END_OF_LIST()
943fda5b000SAlistair Francis };
944fda5b000SAlistair Francis 
sifive_u_soc_class_init(ObjectClass * oc,void * data)945139177b1SBin Meng static void sifive_u_soc_class_init(ObjectClass *oc, void *data)
9462308092bSAlistair Francis {
9472308092bSAlistair Francis     DeviceClass *dc = DEVICE_CLASS(oc);
9482308092bSAlistair Francis 
949139177b1SBin Meng     device_class_set_props(dc, sifive_u_soc_props);
950139177b1SBin Meng     dc->realize = sifive_u_soc_realize;
9512308092bSAlistair Francis     /* Reason: Uses serial_hds in realize function, thus can't be used twice */
9522308092bSAlistair Francis     dc->user_creatable = false;
9532308092bSAlistair Francis }
9542308092bSAlistair Francis 
955139177b1SBin Meng static const TypeInfo sifive_u_soc_type_info = {
9562308092bSAlistair Francis     .name = TYPE_RISCV_U_SOC,
9572308092bSAlistair Francis     .parent = TYPE_DEVICE,
9582308092bSAlistair Francis     .instance_size = sizeof(SiFiveUSoCState),
959139177b1SBin Meng     .instance_init = sifive_u_soc_instance_init,
960139177b1SBin Meng     .class_init = sifive_u_soc_class_init,
9612308092bSAlistair Francis };
9622308092bSAlistair Francis 
sifive_u_soc_register_types(void)963139177b1SBin Meng static void sifive_u_soc_register_types(void)
9642308092bSAlistair Francis {
965139177b1SBin Meng     type_register_static(&sifive_u_soc_type_info);
9662308092bSAlistair Francis }
9672308092bSAlistair Francis 
968139177b1SBin Meng type_init(sifive_u_soc_register_types)
969