xref: /openbmc/qemu/hw/riscv/sifive_u.c (revision f5be2ccb43d3d8466f15f53b82f5fdba1684fc56)
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"
46a7240d1eSMichael Clark #include "hw/char/serial.h"
47ecdfe393SBin Meng #include "hw/cpu/cluster.h"
487b6bb66fSBin Meng #include "hw/misc/unimp.h"
4936aa285fSMarkus Armbruster #include "hw/sd/sd.h"
50145b2991SBin Meng #include "hw/ssi/ssi.h"
51a7240d1eSMichael Clark #include "target/riscv/cpu.h"
52a7240d1eSMichael Clark #include "hw/riscv/riscv_hart.h"
53a7240d1eSMichael Clark #include "hw/riscv/sifive_u.h"
540ac24d56SAlistair Francis #include "hw/riscv/boot.h"
55b609b7e3SBin Meng #include "hw/char/sifive_uart.h"
56cc63a182SAnup Patel #include "hw/intc/riscv_aclint.h"
5784fcf3c1SBin Meng #include "hw/intc/sifive_plic.h"
58a7240d1eSMichael Clark #include "chardev/char.h"
597b6bb66fSBin Meng #include "net/eth.h"
60a7240d1eSMichael Clark #include "sysemu/device_tree.h"
615133ed17SBin Meng #include "sysemu/runstate.h"
6246517dd4SMarkus Armbruster #include "sysemu/sysemu.h"
63a7240d1eSMichael Clark 
645aec3247SMichael Clark #include <libfdt.h>
655aec3247SMichael Clark 
66074ca702SBin Meng /* CLINT timebase frequency */
67074ca702SBin Meng #define CLINT_TIMEBASE_FREQ 1000000
68074ca702SBin Meng 
6973261285SBin Meng static const MemMapEntry sifive_u_memmap[] = {
7013b8c354SEduardo Habkost     [SIFIVE_U_DEV_DEBUG] =    {        0x0,      0x100 },
7113b8c354SEduardo Habkost     [SIFIVE_U_DEV_MROM] =     {     0x1000,     0xf000 },
7213b8c354SEduardo Habkost     [SIFIVE_U_DEV_CLINT] =    {  0x2000000,    0x10000 },
7313b8c354SEduardo Habkost     [SIFIVE_U_DEV_L2CC] =     {  0x2010000,     0x1000 },
7413b8c354SEduardo Habkost     [SIFIVE_U_DEV_PDMA] =     {  0x3000000,   0x100000 },
7513b8c354SEduardo Habkost     [SIFIVE_U_DEV_L2LIM] =    {  0x8000000,  0x2000000 },
7613b8c354SEduardo Habkost     [SIFIVE_U_DEV_PLIC] =     {  0xc000000,  0x4000000 },
7713b8c354SEduardo Habkost     [SIFIVE_U_DEV_PRCI] =     { 0x10000000,     0x1000 },
7813b8c354SEduardo Habkost     [SIFIVE_U_DEV_UART0] =    { 0x10010000,     0x1000 },
7913b8c354SEduardo Habkost     [SIFIVE_U_DEV_UART1] =    { 0x10011000,     0x1000 },
80ea6eaa06SAlistair Francis     [SIFIVE_U_DEV_PWM0] =     { 0x10020000,     0x1000 },
81ea6eaa06SAlistair Francis     [SIFIVE_U_DEV_PWM1] =     { 0x10021000,     0x1000 },
82145b2991SBin Meng     [SIFIVE_U_DEV_QSPI0] =    { 0x10040000,     0x1000 },
83722f1352SBin Meng     [SIFIVE_U_DEV_QSPI2] =    { 0x10050000,     0x1000 },
8413b8c354SEduardo Habkost     [SIFIVE_U_DEV_GPIO] =     { 0x10060000,     0x1000 },
8513b8c354SEduardo Habkost     [SIFIVE_U_DEV_OTP] =      { 0x10070000,     0x1000 },
8613b8c354SEduardo Habkost     [SIFIVE_U_DEV_GEM] =      { 0x10090000,     0x2000 },
8713b8c354SEduardo Habkost     [SIFIVE_U_DEV_GEM_MGMT] = { 0x100a0000,     0x1000 },
8813b8c354SEduardo Habkost     [SIFIVE_U_DEV_DMC] =      { 0x100b0000,    0x10000 },
8913b8c354SEduardo Habkost     [SIFIVE_U_DEV_FLASH0] =   { 0x20000000, 0x10000000 },
9013b8c354SEduardo Habkost     [SIFIVE_U_DEV_DRAM] =     { 0x80000000,        0x0 },
91a7240d1eSMichael Clark };
92a7240d1eSMichael Clark 
935461c4feSBin Meng #define OTP_SERIAL          1
945a7f76a3SAlistair Francis #define GEM_REVISION        0x10070109
955a7f76a3SAlistair Francis 
9673261285SBin Meng static void create_fdt(SiFiveUState *s, const MemMapEntry *memmap,
97*f5be2ccbSDaniel Henrique Barboza                        bool is_32_bit)
98a7240d1eSMichael Clark {
99*f5be2ccbSDaniel Henrique Barboza     MachineState *ms = MACHINE(s);
100*f5be2ccbSDaniel Henrique Barboza     uint64_t mem_size = ms->ram_size;
101a7240d1eSMichael Clark     void *fdt;
10260c7dfa2SDaniel Henrique Barboza     int cpu, fdt_size;
103a7240d1eSMichael Clark     uint32_t *cells;
104a7240d1eSMichael Clark     char *nodename;
1055133ed17SBin Meng     uint32_t plic_phandle, prci_phandle, gpio_phandle, phandle = 1;
1067b6bb66fSBin Meng     uint32_t hfclk_phandle, rtcclk_phandle, phy_phandle;
107cb53b283SBin Meng     static const char * const ethclk_names[2] = { "pclk", "hclk" };
1087cfbb17fSBin Meng     static const char * const clint_compat[2] = {
1097cfbb17fSBin Meng         "sifive,clint0", "riscv,clint0"
1107cfbb17fSBin Meng     };
11160bb5407SBin Meng     static const char * const plic_compat[2] = {
11260bb5407SBin Meng         "sifive,plic-1.0.0", "riscv,plic0"
11360bb5407SBin Meng     };
114a7240d1eSMichael Clark 
115f2ce39b4SPaolo Bonzini     if (ms->dtb) {
11660c7dfa2SDaniel Henrique Barboza         fdt = ms->fdt = load_device_tree(ms->dtb, &fdt_size);
117d5c90cf3SAnup Patel         if (!fdt) {
118d5c90cf3SAnup Patel             error_report("load_device_tree() failed");
119d5c90cf3SAnup Patel             exit(1);
120d5c90cf3SAnup Patel         }
121d5c90cf3SAnup Patel     } else {
12260c7dfa2SDaniel Henrique Barboza         fdt = ms->fdt = create_device_tree(&fdt_size);
123a7240d1eSMichael Clark         if (!fdt) {
124a7240d1eSMichael Clark             error_report("create_device_tree() failed");
125a7240d1eSMichael Clark             exit(1);
126a7240d1eSMichael Clark         }
127d5c90cf3SAnup Patel     }
128a7240d1eSMichael Clark 
129d372e748SBin Meng     qemu_fdt_setprop_string(fdt, "/", "model", "SiFive HiFive Unleashed A00");
130d372e748SBin Meng     qemu_fdt_setprop_string(fdt, "/", "compatible",
131d372e748SBin Meng                             "sifive,hifive-unleashed-a00");
132a7240d1eSMichael Clark     qemu_fdt_setprop_cell(fdt, "/", "#size-cells", 0x2);
133a7240d1eSMichael Clark     qemu_fdt_setprop_cell(fdt, "/", "#address-cells", 0x2);
134a7240d1eSMichael Clark 
135a7240d1eSMichael Clark     qemu_fdt_add_subnode(fdt, "/soc");
136a7240d1eSMichael Clark     qemu_fdt_setprop(fdt, "/soc", "ranges", NULL, 0);
1372a1a6f6dSAlistair Francis     qemu_fdt_setprop_string(fdt, "/soc", "compatible", "simple-bus");
138a7240d1eSMichael Clark     qemu_fdt_setprop_cell(fdt, "/soc", "#size-cells", 0x2);
139a7240d1eSMichael Clark     qemu_fdt_setprop_cell(fdt, "/soc", "#address-cells", 0x2);
140a7240d1eSMichael Clark 
141e1724d09SBin Meng     hfclk_phandle = phandle++;
142e1724d09SBin Meng     nodename = g_strdup_printf("/hfclk");
143e1724d09SBin Meng     qemu_fdt_add_subnode(fdt, nodename);
144e1724d09SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "phandle", hfclk_phandle);
145e1724d09SBin Meng     qemu_fdt_setprop_string(fdt, nodename, "clock-output-names", "hfclk");
146e1724d09SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "clock-frequency",
147e1724d09SBin Meng         SIFIVE_U_HFCLK_FREQ);
148e1724d09SBin Meng     qemu_fdt_setprop_string(fdt, nodename, "compatible", "fixed-clock");
149e1724d09SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "#clock-cells", 0x0);
150e1724d09SBin Meng     g_free(nodename);
151e1724d09SBin Meng 
152e1724d09SBin Meng     rtcclk_phandle = phandle++;
153e1724d09SBin Meng     nodename = g_strdup_printf("/rtcclk");
154e1724d09SBin Meng     qemu_fdt_add_subnode(fdt, nodename);
155e1724d09SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "phandle", rtcclk_phandle);
156e1724d09SBin Meng     qemu_fdt_setprop_string(fdt, nodename, "clock-output-names", "rtcclk");
157e1724d09SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "clock-frequency",
158e1724d09SBin Meng         SIFIVE_U_RTCCLK_FREQ);
159e1724d09SBin Meng     qemu_fdt_setprop_string(fdt, nodename, "compatible", "fixed-clock");
160e1724d09SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "#clock-cells", 0x0);
161e1724d09SBin Meng     g_free(nodename);
162e1724d09SBin Meng 
163a7240d1eSMichael Clark     nodename = g_strdup_printf("/memory@%lx",
16413b8c354SEduardo Habkost         (long)memmap[SIFIVE_U_DEV_DRAM].base);
165a7240d1eSMichael Clark     qemu_fdt_add_subnode(fdt, nodename);
166a7240d1eSMichael Clark     qemu_fdt_setprop_cells(fdt, nodename, "reg",
16713b8c354SEduardo Habkost         memmap[SIFIVE_U_DEV_DRAM].base >> 32, memmap[SIFIVE_U_DEV_DRAM].base,
168a7240d1eSMichael Clark         mem_size >> 32, mem_size);
169a7240d1eSMichael Clark     qemu_fdt_setprop_string(fdt, nodename, "device_type", "memory");
170a7240d1eSMichael Clark     g_free(nodename);
171a7240d1eSMichael Clark 
172a7240d1eSMichael Clark     qemu_fdt_add_subnode(fdt, "/cpus");
1732a8756edSMichael Clark     qemu_fdt_setprop_cell(fdt, "/cpus", "timebase-frequency",
174074ca702SBin Meng         CLINT_TIMEBASE_FREQ);
175a7240d1eSMichael Clark     qemu_fdt_setprop_cell(fdt, "/cpus", "#size-cells", 0x0);
176a7240d1eSMichael Clark     qemu_fdt_setprop_cell(fdt, "/cpus", "#address-cells", 0x1);
177a7240d1eSMichael Clark 
178ecdfe393SBin Meng     for (cpu = ms->smp.cpus - 1; cpu >= 0; cpu--) {
179382cb439SBin Meng         int cpu_phandle = phandle++;
180a7240d1eSMichael Clark         nodename = g_strdup_printf("/cpus/cpu@%d", cpu);
181a7240d1eSMichael Clark         char *intc = g_strdup_printf("/cpus/cpu@%d/interrupt-controller", cpu);
182ecdfe393SBin Meng         char *isa;
183a7240d1eSMichael Clark         qemu_fdt_add_subnode(fdt, nodename);
184ecdfe393SBin Meng         /* cpu 0 is the management hart that does not have mmu */
185ecdfe393SBin Meng         if (cpu != 0) {
1862206ffa6SAlistair Francis             if (is_32_bit) {
187e883e992SBin Meng                 qemu_fdt_setprop_string(fdt, nodename, "mmu-type", "riscv,sv32");
1882206ffa6SAlistair Francis             } else {
189a7240d1eSMichael Clark                 qemu_fdt_setprop_string(fdt, nodename, "mmu-type", "riscv,sv48");
1902206ffa6SAlistair Francis             }
191ecdfe393SBin Meng             isa = riscv_isa_string(&s->soc.u_cpus.harts[cpu - 1]);
192ecdfe393SBin Meng         } else {
193ecdfe393SBin Meng             isa = riscv_isa_string(&s->soc.e_cpus.harts[0]);
194ecdfe393SBin Meng         }
195a7240d1eSMichael Clark         qemu_fdt_setprop_string(fdt, nodename, "riscv,isa", isa);
196a7240d1eSMichael Clark         qemu_fdt_setprop_string(fdt, nodename, "compatible", "riscv");
197a7240d1eSMichael Clark         qemu_fdt_setprop_string(fdt, nodename, "status", "okay");
198a7240d1eSMichael Clark         qemu_fdt_setprop_cell(fdt, nodename, "reg", cpu);
199a7240d1eSMichael Clark         qemu_fdt_setprop_string(fdt, nodename, "device_type", "cpu");
200a7240d1eSMichael Clark         qemu_fdt_add_subnode(fdt, intc);
201382cb439SBin Meng         qemu_fdt_setprop_cell(fdt, intc, "phandle", cpu_phandle);
202a7240d1eSMichael Clark         qemu_fdt_setprop_string(fdt, intc, "compatible", "riscv,cpu-intc");
203a7240d1eSMichael Clark         qemu_fdt_setprop(fdt, intc, "interrupt-controller", NULL, 0);
204a7240d1eSMichael Clark         qemu_fdt_setprop_cell(fdt, intc, "#interrupt-cells", 1);
205a7240d1eSMichael Clark         g_free(isa);
206a7240d1eSMichael Clark         g_free(intc);
207a7240d1eSMichael Clark         g_free(nodename);
208a7240d1eSMichael Clark     }
209a7240d1eSMichael Clark 
210ecdfe393SBin Meng     cells =  g_new0(uint32_t, ms->smp.cpus * 4);
211ecdfe393SBin Meng     for (cpu = 0; cpu < ms->smp.cpus; cpu++) {
212a7240d1eSMichael Clark         nodename =
213a7240d1eSMichael Clark             g_strdup_printf("/cpus/cpu@%d/interrupt-controller", cpu);
214a7240d1eSMichael Clark         uint32_t intc_phandle = qemu_fdt_get_phandle(fdt, nodename);
215a7240d1eSMichael Clark         cells[cpu * 4 + 0] = cpu_to_be32(intc_phandle);
216a7240d1eSMichael Clark         cells[cpu * 4 + 1] = cpu_to_be32(IRQ_M_SOFT);
217a7240d1eSMichael Clark         cells[cpu * 4 + 2] = cpu_to_be32(intc_phandle);
218a7240d1eSMichael Clark         cells[cpu * 4 + 3] = cpu_to_be32(IRQ_M_TIMER);
219a7240d1eSMichael Clark         g_free(nodename);
220a7240d1eSMichael Clark     }
221a7240d1eSMichael Clark     nodename = g_strdup_printf("/soc/clint@%lx",
22213b8c354SEduardo Habkost         (long)memmap[SIFIVE_U_DEV_CLINT].base);
223a7240d1eSMichael Clark     qemu_fdt_add_subnode(fdt, nodename);
2247cfbb17fSBin Meng     qemu_fdt_setprop_string_array(fdt, nodename, "compatible",
2257cfbb17fSBin Meng         (char **)&clint_compat, ARRAY_SIZE(clint_compat));
226a7240d1eSMichael Clark     qemu_fdt_setprop_cells(fdt, nodename, "reg",
22713b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_CLINT].base,
22813b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_CLINT].size);
229a7240d1eSMichael Clark     qemu_fdt_setprop(fdt, nodename, "interrupts-extended",
230ecdfe393SBin Meng         cells, ms->smp.cpus * sizeof(uint32_t) * 4);
231a7240d1eSMichael Clark     g_free(cells);
232a7240d1eSMichael Clark     g_free(nodename);
233a7240d1eSMichael Clark 
234ea85f27dSBin Meng     nodename = g_strdup_printf("/soc/otp@%lx",
23513b8c354SEduardo Habkost         (long)memmap[SIFIVE_U_DEV_OTP].base);
236ea85f27dSBin Meng     qemu_fdt_add_subnode(fdt, nodename);
237ea85f27dSBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "fuse-count", SIFIVE_U_OTP_REG_SIZE);
238ea85f27dSBin Meng     qemu_fdt_setprop_cells(fdt, nodename, "reg",
23913b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_OTP].base,
24013b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_OTP].size);
241ea85f27dSBin Meng     qemu_fdt_setprop_string(fdt, nodename, "compatible",
242ea85f27dSBin Meng         "sifive,fu540-c000-otp");
243ea85f27dSBin Meng     g_free(nodename);
244ea85f27dSBin Meng 
245af14c840SBin Meng     prci_phandle = phandle++;
246af14c840SBin Meng     nodename = g_strdup_printf("/soc/clock-controller@%lx",
24713b8c354SEduardo Habkost         (long)memmap[SIFIVE_U_DEV_PRCI].base);
248af14c840SBin Meng     qemu_fdt_add_subnode(fdt, nodename);
249af14c840SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "phandle", prci_phandle);
250af14c840SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "#clock-cells", 0x1);
251af14c840SBin Meng     qemu_fdt_setprop_cells(fdt, nodename, "clocks",
252af14c840SBin Meng         hfclk_phandle, rtcclk_phandle);
253af14c840SBin Meng     qemu_fdt_setprop_cells(fdt, nodename, "reg",
25413b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_PRCI].base,
25513b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_PRCI].size);
256af14c840SBin Meng     qemu_fdt_setprop_string(fdt, nodename, "compatible",
257af14c840SBin Meng         "sifive,fu540-c000-prci");
258af14c840SBin Meng     g_free(nodename);
259af14c840SBin Meng 
260382cb439SBin Meng     plic_phandle = phandle++;
261ecdfe393SBin Meng     cells =  g_new0(uint32_t, ms->smp.cpus * 4 - 2);
262ecdfe393SBin Meng     for (cpu = 0; cpu < ms->smp.cpus; cpu++) {
263a7240d1eSMichael Clark         nodename =
264a7240d1eSMichael Clark             g_strdup_printf("/cpus/cpu@%d/interrupt-controller", cpu);
265a7240d1eSMichael Clark         uint32_t intc_phandle = qemu_fdt_get_phandle(fdt, nodename);
266ecdfe393SBin Meng         /* cpu 0 is the management hart that does not have S-mode */
267ecdfe393SBin Meng         if (cpu == 0) {
268ecdfe393SBin Meng             cells[0] = cpu_to_be32(intc_phandle);
269ecdfe393SBin Meng             cells[1] = cpu_to_be32(IRQ_M_EXT);
270ecdfe393SBin Meng         } else {
271ecdfe393SBin Meng             cells[cpu * 4 - 2] = cpu_to_be32(intc_phandle);
272ecdfe393SBin Meng             cells[cpu * 4 - 1] = cpu_to_be32(IRQ_M_EXT);
273a7240d1eSMichael Clark             cells[cpu * 4 + 0] = cpu_to_be32(intc_phandle);
274ecdfe393SBin Meng             cells[cpu * 4 + 1] = cpu_to_be32(IRQ_S_EXT);
275ecdfe393SBin Meng         }
276a7240d1eSMichael Clark         g_free(nodename);
277a7240d1eSMichael Clark     }
278a7240d1eSMichael Clark     nodename = g_strdup_printf("/soc/interrupt-controller@%lx",
27913b8c354SEduardo Habkost         (long)memmap[SIFIVE_U_DEV_PLIC].base);
280a7240d1eSMichael Clark     qemu_fdt_add_subnode(fdt, nodename);
281a7240d1eSMichael Clark     qemu_fdt_setprop_cell(fdt, nodename, "#interrupt-cells", 1);
28260bb5407SBin Meng     qemu_fdt_setprop_string_array(fdt, nodename, "compatible",
28360bb5407SBin Meng         (char **)&plic_compat, ARRAY_SIZE(plic_compat));
284a7240d1eSMichael Clark     qemu_fdt_setprop(fdt, nodename, "interrupt-controller", NULL, 0);
285a7240d1eSMichael Clark     qemu_fdt_setprop(fdt, nodename, "interrupts-extended",
286ecdfe393SBin Meng         cells, (ms->smp.cpus * 4 - 2) * sizeof(uint32_t));
287a7240d1eSMichael Clark     qemu_fdt_setprop_cells(fdt, nodename, "reg",
28813b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_PLIC].base,
28913b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_PLIC].size);
290724d80c8SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "riscv,ndev",
291724d80c8SBin Meng                           SIFIVE_U_PLIC_NUM_SOURCES - 1);
29204e7edd1SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "phandle", plic_phandle);
293a7240d1eSMichael Clark     plic_phandle = qemu_fdt_get_phandle(fdt, nodename);
294a7240d1eSMichael Clark     g_free(cells);
295a7240d1eSMichael Clark     g_free(nodename);
296a7240d1eSMichael Clark 
2975133ed17SBin Meng     gpio_phandle = phandle++;
2988a88b9f5SBin Meng     nodename = g_strdup_printf("/soc/gpio@%lx",
29913b8c354SEduardo Habkost         (long)memmap[SIFIVE_U_DEV_GPIO].base);
3008a88b9f5SBin Meng     qemu_fdt_add_subnode(fdt, nodename);
3015133ed17SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "phandle", gpio_phandle);
3028a88b9f5SBin Meng     qemu_fdt_setprop_cells(fdt, nodename, "clocks",
3038a88b9f5SBin Meng         prci_phandle, PRCI_CLK_TLCLK);
3048a88b9f5SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "#interrupt-cells", 2);
3058a88b9f5SBin Meng     qemu_fdt_setprop(fdt, nodename, "interrupt-controller", NULL, 0);
3068a88b9f5SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "#gpio-cells", 2);
3078a88b9f5SBin Meng     qemu_fdt_setprop(fdt, nodename, "gpio-controller", NULL, 0);
3088a88b9f5SBin Meng     qemu_fdt_setprop_cells(fdt, nodename, "reg",
30913b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_GPIO].base,
31013b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_GPIO].size);
3118a88b9f5SBin Meng     qemu_fdt_setprop_cells(fdt, nodename, "interrupts", SIFIVE_U_GPIO_IRQ0,
3128a88b9f5SBin Meng         SIFIVE_U_GPIO_IRQ1, SIFIVE_U_GPIO_IRQ2, SIFIVE_U_GPIO_IRQ3,
3138a88b9f5SBin Meng         SIFIVE_U_GPIO_IRQ4, SIFIVE_U_GPIO_IRQ5, SIFIVE_U_GPIO_IRQ6,
3148a88b9f5SBin Meng         SIFIVE_U_GPIO_IRQ7, SIFIVE_U_GPIO_IRQ8, SIFIVE_U_GPIO_IRQ9,
3158a88b9f5SBin Meng         SIFIVE_U_GPIO_IRQ10, SIFIVE_U_GPIO_IRQ11, SIFIVE_U_GPIO_IRQ12,
3168a88b9f5SBin Meng         SIFIVE_U_GPIO_IRQ13, SIFIVE_U_GPIO_IRQ14, SIFIVE_U_GPIO_IRQ15);
3178a88b9f5SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle);
3188a88b9f5SBin Meng     qemu_fdt_setprop_string(fdt, nodename, "compatible", "sifive,gpio0");
3198a88b9f5SBin Meng     g_free(nodename);
3208a88b9f5SBin Meng 
3215133ed17SBin Meng     nodename = g_strdup_printf("/gpio-restart");
3225133ed17SBin Meng     qemu_fdt_add_subnode(fdt, nodename);
3235133ed17SBin Meng     qemu_fdt_setprop_cells(fdt, nodename, "gpios", gpio_phandle, 10, 1);
3245133ed17SBin Meng     qemu_fdt_setprop_string(fdt, nodename, "compatible", "gpio-restart");
3255133ed17SBin Meng     g_free(nodename);
3265133ed17SBin Meng 
327834e027aSBin Meng     nodename = g_strdup_printf("/soc/dma@%lx",
32813b8c354SEduardo Habkost         (long)memmap[SIFIVE_U_DEV_PDMA].base);
329834e027aSBin Meng     qemu_fdt_add_subnode(fdt, nodename);
330834e027aSBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "#dma-cells", 1);
331834e027aSBin Meng     qemu_fdt_setprop_cells(fdt, nodename, "interrupts",
332834e027aSBin Meng         SIFIVE_U_PDMA_IRQ0, SIFIVE_U_PDMA_IRQ1, SIFIVE_U_PDMA_IRQ2,
333834e027aSBin Meng         SIFIVE_U_PDMA_IRQ3, SIFIVE_U_PDMA_IRQ4, SIFIVE_U_PDMA_IRQ5,
334834e027aSBin Meng         SIFIVE_U_PDMA_IRQ6, SIFIVE_U_PDMA_IRQ7);
335834e027aSBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle);
336834e027aSBin Meng     qemu_fdt_setprop_cells(fdt, nodename, "reg",
33713b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_PDMA].base,
33813b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_PDMA].size);
339834e027aSBin Meng     qemu_fdt_setprop_string(fdt, nodename, "compatible",
340834e027aSBin Meng                             "sifive,fu540-c000-pdma");
341834e027aSBin Meng     g_free(nodename);
342834e027aSBin Meng 
3436eaf9cf5SBin Meng     nodename = g_strdup_printf("/soc/cache-controller@%lx",
34413b8c354SEduardo Habkost         (long)memmap[SIFIVE_U_DEV_L2CC].base);
3456eaf9cf5SBin Meng     qemu_fdt_add_subnode(fdt, nodename);
3466eaf9cf5SBin Meng     qemu_fdt_setprop_cells(fdt, nodename, "reg",
34713b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_L2CC].base,
34813b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_L2CC].size);
3496eaf9cf5SBin Meng     qemu_fdt_setprop_cells(fdt, nodename, "interrupts",
3506eaf9cf5SBin Meng         SIFIVE_U_L2CC_IRQ0, SIFIVE_U_L2CC_IRQ1, SIFIVE_U_L2CC_IRQ2);
3516eaf9cf5SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle);
3526eaf9cf5SBin Meng     qemu_fdt_setprop(fdt, nodename, "cache-unified", NULL, 0);
3536eaf9cf5SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "cache-size", 2097152);
3546eaf9cf5SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "cache-sets", 1024);
3556eaf9cf5SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "cache-level", 2);
3566eaf9cf5SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "cache-block-size", 64);
3576eaf9cf5SBin Meng     qemu_fdt_setprop_string(fdt, nodename, "compatible",
3586eaf9cf5SBin Meng                             "sifive,fu540-c000-ccache");
3596eaf9cf5SBin Meng     g_free(nodename);
3606eaf9cf5SBin Meng 
361145b2991SBin Meng     nodename = g_strdup_printf("/soc/spi@%lx",
362722f1352SBin Meng         (long)memmap[SIFIVE_U_DEV_QSPI2].base);
363722f1352SBin Meng     qemu_fdt_add_subnode(fdt, nodename);
364722f1352SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "#size-cells", 0);
365722f1352SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "#address-cells", 1);
366722f1352SBin Meng     qemu_fdt_setprop_cells(fdt, nodename, "clocks",
367722f1352SBin Meng         prci_phandle, PRCI_CLK_TLCLK);
368722f1352SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "interrupts", SIFIVE_U_QSPI2_IRQ);
369722f1352SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle);
370722f1352SBin Meng     qemu_fdt_setprop_cells(fdt, nodename, "reg",
371722f1352SBin Meng         0x0, memmap[SIFIVE_U_DEV_QSPI2].base,
372722f1352SBin Meng         0x0, memmap[SIFIVE_U_DEV_QSPI2].size);
373722f1352SBin Meng     qemu_fdt_setprop_string(fdt, nodename, "compatible", "sifive,spi0");
374722f1352SBin Meng     g_free(nodename);
375722f1352SBin Meng 
376722f1352SBin Meng     nodename = g_strdup_printf("/soc/spi@%lx/mmc@0",
377722f1352SBin Meng         (long)memmap[SIFIVE_U_DEV_QSPI2].base);
378722f1352SBin Meng     qemu_fdt_add_subnode(fdt, nodename);
379722f1352SBin Meng     qemu_fdt_setprop(fdt, nodename, "disable-wp", NULL, 0);
380722f1352SBin Meng     qemu_fdt_setprop_cells(fdt, nodename, "voltage-ranges", 3300, 3300);
381722f1352SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "spi-max-frequency", 20000000);
382722f1352SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "reg", 0);
383722f1352SBin Meng     qemu_fdt_setprop_string(fdt, nodename, "compatible", "mmc-spi-slot");
384722f1352SBin Meng     g_free(nodename);
385722f1352SBin Meng 
386722f1352SBin Meng     nodename = g_strdup_printf("/soc/spi@%lx",
387145b2991SBin Meng         (long)memmap[SIFIVE_U_DEV_QSPI0].base);
388145b2991SBin Meng     qemu_fdt_add_subnode(fdt, nodename);
389145b2991SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "#size-cells", 0);
390145b2991SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "#address-cells", 1);
391145b2991SBin Meng     qemu_fdt_setprop_cells(fdt, nodename, "clocks",
392145b2991SBin Meng         prci_phandle, PRCI_CLK_TLCLK);
393145b2991SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "interrupts", SIFIVE_U_QSPI0_IRQ);
394145b2991SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle);
395145b2991SBin Meng     qemu_fdt_setprop_cells(fdt, nodename, "reg",
396145b2991SBin Meng         0x0, memmap[SIFIVE_U_DEV_QSPI0].base,
397145b2991SBin Meng         0x0, memmap[SIFIVE_U_DEV_QSPI0].size);
398145b2991SBin Meng     qemu_fdt_setprop_string(fdt, nodename, "compatible", "sifive,spi0");
399145b2991SBin Meng     g_free(nodename);
400145b2991SBin Meng 
401145b2991SBin Meng     nodename = g_strdup_printf("/soc/spi@%lx/flash@0",
402145b2991SBin Meng         (long)memmap[SIFIVE_U_DEV_QSPI0].base);
403145b2991SBin Meng     qemu_fdt_add_subnode(fdt, nodename);
404145b2991SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "spi-rx-bus-width", 4);
405145b2991SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "spi-tx-bus-width", 4);
406145b2991SBin Meng     qemu_fdt_setprop(fdt, nodename, "m25p,fast-read", NULL, 0);
407145b2991SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "spi-max-frequency", 50000000);
408145b2991SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "reg", 0);
409145b2991SBin Meng     qemu_fdt_setprop_string(fdt, nodename, "compatible", "jedec,spi-nor");
410145b2991SBin Meng     g_free(nodename);
411145b2991SBin Meng 
4127b6bb66fSBin Meng     phy_phandle = phandle++;
4135a7f76a3SAlistair Francis     nodename = g_strdup_printf("/soc/ethernet@%lx",
41413b8c354SEduardo Habkost         (long)memmap[SIFIVE_U_DEV_GEM].base);
4155a7f76a3SAlistair Francis     qemu_fdt_add_subnode(fdt, nodename);
4167b6bb66fSBin Meng     qemu_fdt_setprop_string(fdt, nodename, "compatible",
4177b6bb66fSBin Meng         "sifive,fu540-c000-gem");
4185a7f76a3SAlistair Francis     qemu_fdt_setprop_cells(fdt, nodename, "reg",
41913b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_GEM].base,
42013b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_GEM].size,
42113b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_GEM_MGMT].base,
42213b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_GEM_MGMT].size);
4235a7f76a3SAlistair Francis     qemu_fdt_setprop_string(fdt, nodename, "reg-names", "control");
4245a7f76a3SAlistair Francis     qemu_fdt_setprop_string(fdt, nodename, "phy-mode", "gmii");
4257b6bb66fSBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "phy-handle", phy_phandle);
42604e7edd1SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle);
42704e7edd1SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "interrupts", SIFIVE_U_GEM_IRQ);
428fe93582cSAnup Patel     qemu_fdt_setprop_cells(fdt, nodename, "clocks",
429806c64b7SBin Meng         prci_phandle, PRCI_CLK_GEMGXLPLL, prci_phandle, PRCI_CLK_GEMGXLPLL);
430cb53b283SBin Meng     qemu_fdt_setprop_string_array(fdt, nodename, "clock-names",
431cb53b283SBin Meng         (char **)&ethclk_names, ARRAY_SIZE(ethclk_names));
4327b6bb66fSBin Meng     qemu_fdt_setprop(fdt, nodename, "local-mac-address",
4337b6bb66fSBin Meng         s->soc.gem.conf.macaddr.a, ETH_ALEN);
43404e7edd1SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "#address-cells", 1);
43504e7edd1SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "#size-cells", 0);
436c3a28b5dSBin Meng 
437c3a28b5dSBin Meng     qemu_fdt_add_subnode(fdt, "/aliases");
438c3a28b5dSBin Meng     qemu_fdt_setprop_string(fdt, "/aliases", "ethernet0", nodename);
439c3a28b5dSBin Meng 
4405a7f76a3SAlistair Francis     g_free(nodename);
4415a7f76a3SAlistair Francis 
4425a7f76a3SAlistair Francis     nodename = g_strdup_printf("/soc/ethernet@%lx/ethernet-phy@0",
44313b8c354SEduardo Habkost         (long)memmap[SIFIVE_U_DEV_GEM].base);
4445a7f76a3SAlistair Francis     qemu_fdt_add_subnode(fdt, nodename);
4457b6bb66fSBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "phandle", phy_phandle);
44604e7edd1SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "reg", 0x0);
4475a7f76a3SAlistair Francis     g_free(nodename);
4485a7f76a3SAlistair Francis 
449ea6eaa06SAlistair Francis     nodename = g_strdup_printf("/soc/pwm@%lx",
450ea6eaa06SAlistair Francis         (long)memmap[SIFIVE_U_DEV_PWM0].base);
451ea6eaa06SAlistair Francis     qemu_fdt_add_subnode(fdt, nodename);
452ea6eaa06SAlistair Francis     qemu_fdt_setprop_string(fdt, nodename, "compatible", "sifive,pwm0");
453ea6eaa06SAlistair Francis     qemu_fdt_setprop_cells(fdt, nodename, "reg",
454ea6eaa06SAlistair Francis         0x0, memmap[SIFIVE_U_DEV_PWM0].base,
455ea6eaa06SAlistair Francis         0x0, memmap[SIFIVE_U_DEV_PWM0].size);
456ea6eaa06SAlistair Francis     qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle);
457ea6eaa06SAlistair Francis     qemu_fdt_setprop_cells(fdt, nodename, "interrupts",
458ea6eaa06SAlistair Francis                            SIFIVE_U_PWM0_IRQ0, SIFIVE_U_PWM0_IRQ1,
459ea6eaa06SAlistair Francis                            SIFIVE_U_PWM0_IRQ2, SIFIVE_U_PWM0_IRQ3);
460ea6eaa06SAlistair Francis     qemu_fdt_setprop_cells(fdt, nodename, "clocks",
461ea6eaa06SAlistair Francis                            prci_phandle, PRCI_CLK_TLCLK);
462ea6eaa06SAlistair Francis     qemu_fdt_setprop_cell(fdt, nodename, "#pwm-cells", 0);
463ea6eaa06SAlistair Francis     g_free(nodename);
464ea6eaa06SAlistair Francis 
465ea6eaa06SAlistair Francis     nodename = g_strdup_printf("/soc/pwm@%lx",
466ea6eaa06SAlistair Francis         (long)memmap[SIFIVE_U_DEV_PWM1].base);
467ea6eaa06SAlistair Francis     qemu_fdt_add_subnode(fdt, nodename);
468ea6eaa06SAlistair Francis     qemu_fdt_setprop_string(fdt, nodename, "compatible", "sifive,pwm0");
469ea6eaa06SAlistair Francis     qemu_fdt_setprop_cells(fdt, nodename, "reg",
470ea6eaa06SAlistair Francis         0x0, memmap[SIFIVE_U_DEV_PWM1].base,
471ea6eaa06SAlistair Francis         0x0, memmap[SIFIVE_U_DEV_PWM1].size);
472ea6eaa06SAlistair Francis     qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle);
473ea6eaa06SAlistair Francis     qemu_fdt_setprop_cells(fdt, nodename, "interrupts",
474ea6eaa06SAlistair Francis                            SIFIVE_U_PWM1_IRQ0, SIFIVE_U_PWM1_IRQ1,
475ea6eaa06SAlistair Francis                            SIFIVE_U_PWM1_IRQ2, SIFIVE_U_PWM1_IRQ3);
476ea6eaa06SAlistair Francis     qemu_fdt_setprop_cells(fdt, nodename, "clocks",
477ea6eaa06SAlistair Francis                            prci_phandle, PRCI_CLK_TLCLK);
478ea6eaa06SAlistair Francis     qemu_fdt_setprop_cell(fdt, nodename, "#pwm-cells", 0);
479ea6eaa06SAlistair Francis     g_free(nodename);
480ea6eaa06SAlistair Francis 
4815f7134d3SBin Meng     nodename = g_strdup_printf("/soc/serial@%lx",
48210b43754SAnup Patel         (long)memmap[SIFIVE_U_DEV_UART1].base);
48310b43754SAnup Patel     qemu_fdt_add_subnode(fdt, nodename);
48410b43754SAnup Patel     qemu_fdt_setprop_string(fdt, nodename, "compatible", "sifive,uart0");
48510b43754SAnup Patel     qemu_fdt_setprop_cells(fdt, nodename, "reg",
48610b43754SAnup Patel         0x0, memmap[SIFIVE_U_DEV_UART1].base,
48710b43754SAnup Patel         0x0, memmap[SIFIVE_U_DEV_UART1].size);
48810b43754SAnup Patel     qemu_fdt_setprop_cells(fdt, nodename, "clocks",
48910b43754SAnup Patel         prci_phandle, PRCI_CLK_TLCLK);
49010b43754SAnup Patel     qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle);
49110b43754SAnup Patel     qemu_fdt_setprop_cell(fdt, nodename, "interrupts", SIFIVE_U_UART1_IRQ);
49210b43754SAnup Patel 
49310b43754SAnup Patel     qemu_fdt_setprop_string(fdt, "/aliases", "serial1", nodename);
49410b43754SAnup Patel     g_free(nodename);
49510b43754SAnup Patel 
49610b43754SAnup Patel     nodename = g_strdup_printf("/soc/serial@%lx",
49713b8c354SEduardo Habkost         (long)memmap[SIFIVE_U_DEV_UART0].base);
498a7240d1eSMichael Clark     qemu_fdt_add_subnode(fdt, nodename);
499a7240d1eSMichael Clark     qemu_fdt_setprop_string(fdt, nodename, "compatible", "sifive,uart0");
500a7240d1eSMichael Clark     qemu_fdt_setprop_cells(fdt, nodename, "reg",
50113b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_UART0].base,
50213b8c354SEduardo Habkost         0x0, memmap[SIFIVE_U_DEV_UART0].size);
503806c64b7SBin Meng     qemu_fdt_setprop_cells(fdt, nodename, "clocks",
504806c64b7SBin Meng         prci_phandle, PRCI_CLK_TLCLK);
50504e7edd1SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle);
50604e7edd1SBin Meng     qemu_fdt_setprop_cell(fdt, nodename, "interrupts", SIFIVE_U_UART0_IRQ);
507a7240d1eSMichael Clark 
508a7240d1eSMichael Clark     qemu_fdt_add_subnode(fdt, "/chosen");
509a7240d1eSMichael Clark     qemu_fdt_setprop_string(fdt, "/chosen", "stdout-path", nodename);
51044e6dcd3SGuenter Roeck     qemu_fdt_setprop_string(fdt, "/aliases", "serial0", nodename);
51144e6dcd3SGuenter Roeck 
512a7240d1eSMichael Clark     g_free(nodename);
513a7240d1eSMichael Clark }
514a7240d1eSMichael Clark 
5155133ed17SBin Meng static void sifive_u_machine_reset(void *opaque, int n, int level)
5165133ed17SBin Meng {
5175133ed17SBin Meng     /* gpio pin active low triggers reset */
5185133ed17SBin Meng     if (!level) {
5195133ed17SBin Meng         qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
5205133ed17SBin Meng     }
5215133ed17SBin Meng }
5225133ed17SBin Meng 
523523e3464SAlistair Francis static void sifive_u_machine_init(MachineState *machine)
524a7240d1eSMichael Clark {
52573261285SBin Meng     const MemMapEntry *memmap = sifive_u_memmap;
526687caef1SAlistair Francis     SiFiveUState *s = RISCV_U_MACHINE(machine);
5275aec3247SMichael Clark     MemoryRegion *system_memory = get_system_memory();
5281b3a2308SAlistair Francis     MemoryRegion *flash0 = g_new(MemoryRegion, 1);
52913b8c354SEduardo Habkost     target_ulong start_addr = memmap[SIFIVE_U_DEV_DRAM].base;
53038bc4e34SAlistair Francis     target_ulong firmware_end_addr, kernel_start_addr;
5319d3f7108SDaniel Henrique Barboza     const char *firmware_name;
5328590f536SAtish Patra     uint32_t start_addr_hi32 = 0x00000000;
5335aec3247SMichael Clark     int i;
53466b1205bSAtish Patra     uint32_t fdt_load_addr;
535dc144fe1SAtish Patra     uint64_t kernel_entry;
536145b2991SBin Meng     DriveInfo *dinfo;
53736aa285fSMarkus Armbruster     BlockBackend *blk;
53836aa285fSMarkus Armbruster     DeviceState *flash_dev, *sd_dev, *card_dev;
539722f1352SBin Meng     qemu_irq flash_cs, sd_cs;
540a7240d1eSMichael Clark 
5412308092bSAlistair Francis     /* Initialize SoC */
5429fc7fc4dSMarkus Armbruster     object_initialize_child(OBJECT(machine), "soc", &s->soc, TYPE_RISCV_U_SOC);
5435325cc34SMarkus Armbruster     object_property_set_uint(OBJECT(&s->soc), "serial", s->serial,
5443ca109c3SBin Meng                              &error_abort);
545099be035SAlistair Francis     object_property_set_str(OBJECT(&s->soc), "cpu-type", machine->cpu_type,
546099be035SAlistair Francis                              &error_abort);
5478f972e5bSAlistair Francis     qdev_realize(DEVICE(&s->soc), NULL, &error_fatal);
548a7240d1eSMichael Clark 
549a7240d1eSMichael Clark     /* register RAM */
55013b8c354SEduardo Habkost     memory_region_add_subregion(system_memory, memmap[SIFIVE_U_DEV_DRAM].base,
551c188a9c4SBin Meng                                 machine->ram);
552a7240d1eSMichael Clark 
5531b3a2308SAlistair Francis     /* register QSPI0 Flash */
5541b3a2308SAlistair Francis     memory_region_init_ram(flash0, NULL, "riscv.sifive.u.flash0",
55513b8c354SEduardo Habkost                            memmap[SIFIVE_U_DEV_FLASH0].size, &error_fatal);
55613b8c354SEduardo Habkost     memory_region_add_subregion(system_memory, memmap[SIFIVE_U_DEV_FLASH0].base,
5571b3a2308SAlistair Francis                                 flash0);
5581b3a2308SAlistair Francis 
5595133ed17SBin Meng     /* register gpio-restart */
5605133ed17SBin Meng     qdev_connect_gpio_out(DEVICE(&(s->soc.gpio)), 10,
5615133ed17SBin Meng                           qemu_allocate_irq(sifive_u_machine_reset, NULL, 0));
5625133ed17SBin Meng 
563a7240d1eSMichael Clark     /* create device tree */
564*f5be2ccbSDaniel Henrique Barboza     create_fdt(s, memmap, riscv_is_32bit(&s->soc.u_cpus));
565a7240d1eSMichael Clark 
56617aad9f2SBin Meng     if (s->start_in_flash) {
56717aad9f2SBin Meng         /*
56817aad9f2SBin Meng          * If start_in_flash property is given, assign s->msel to a value
56917aad9f2SBin Meng          * that representing booting from QSPI0 memory-mapped flash.
57017aad9f2SBin Meng          *
57117aad9f2SBin Meng          * This also means that when both start_in_flash and msel properties
57217aad9f2SBin Meng          * are given, start_in_flash takes the precedence over msel.
57317aad9f2SBin Meng          *
57417aad9f2SBin Meng          * Note this is to keep backward compatibility not to break existing
57517aad9f2SBin Meng          * users that use start_in_flash property.
57617aad9f2SBin Meng          */
57717aad9f2SBin Meng         s->msel = MSEL_MEMMAP_QSPI0_FLASH;
57817aad9f2SBin Meng     }
57917aad9f2SBin Meng 
58017aad9f2SBin Meng     switch (s->msel) {
58117aad9f2SBin Meng     case MSEL_MEMMAP_QSPI0_FLASH:
58213b8c354SEduardo Habkost         start_addr = memmap[SIFIVE_U_DEV_FLASH0].base;
58317aad9f2SBin Meng         break;
58417aad9f2SBin Meng     case MSEL_L2LIM_QSPI0_FLASH:
58517aad9f2SBin Meng     case MSEL_L2LIM_QSPI2_SD:
58613b8c354SEduardo Habkost         start_addr = memmap[SIFIVE_U_DEV_L2LIM].base;
58717aad9f2SBin Meng         break;
58817aad9f2SBin Meng     default:
58913b8c354SEduardo Habkost         start_addr = memmap[SIFIVE_U_DEV_DRAM].base;
59017aad9f2SBin Meng         break;
59117aad9f2SBin Meng     }
59217aad9f2SBin Meng 
5939d3f7108SDaniel Henrique Barboza     firmware_name = riscv_default_firmware_name(&s->soc.u_cpus);
5949d3f7108SDaniel Henrique Barboza     firmware_end_addr = riscv_find_and_load_firmware(machine, firmware_name,
5959d3f7108SDaniel Henrique Barboza                                                      start_addr, NULL);
596b3042223SAlistair Francis 
597a7240d1eSMichael Clark     if (machine->kernel_filename) {
598a8259b53SAlistair Francis         kernel_start_addr = riscv_calc_kernel_start_addr(&s->soc.u_cpus,
59938bc4e34SAlistair Francis                                                          firmware_end_addr);
60038bc4e34SAlistair Francis 
60160c1f05eSDaniel Henrique Barboza         kernel_entry = riscv_load_kernel(machine, kernel_start_addr, NULL);
6020f8d4462SGuenter Roeck 
6030f8d4462SGuenter Roeck         if (machine->initrd_filename) {
6041f991461SDaniel Henrique Barboza             riscv_load_initrd(machine, kernel_entry);
6050f8d4462SGuenter Roeck         }
606b1f19f23SDaniel Henrique Barboza 
607b1f19f23SDaniel Henrique Barboza         if (machine->kernel_cmdline && *machine->kernel_cmdline) {
608b1f19f23SDaniel Henrique Barboza             qemu_fdt_setprop_string(machine->fdt, "/chosen", "bootargs",
609b1f19f23SDaniel Henrique Barboza                                     machine->kernel_cmdline);
610b1f19f23SDaniel Henrique Barboza         }
611dc144fe1SAtish Patra     } else {
612dc144fe1SAtish Patra        /*
613dc144fe1SAtish Patra         * If dynamic firmware is used, it doesn't know where is the next mode
614dc144fe1SAtish Patra         * if kernel argument is not set.
615dc144fe1SAtish Patra         */
616dc144fe1SAtish Patra         kernel_entry = 0;
617a7240d1eSMichael Clark     }
618a7240d1eSMichael Clark 
61966b1205bSAtish Patra     /* Compute the fdt load address in dram */
62013b8c354SEduardo Habkost     fdt_load_addr = riscv_load_fdt(memmap[SIFIVE_U_DEV_DRAM].base,
62160c7dfa2SDaniel Henrique Barboza                                    machine->ram_size, machine->fdt);
622a8259b53SAlistair Francis     if (!riscv_is_32bit(&s->soc.u_cpus)) {
6232206ffa6SAlistair Francis         start_addr_hi32 = (uint64_t)start_addr >> 32;
6242206ffa6SAlistair Francis     }
62566b1205bSAtish Patra 
626a7240d1eSMichael Clark     /* reset vector */
627623d53cbSBin Meng     uint32_t reset_vec[12] = {
62817aad9f2SBin Meng         s->msel,                       /* MSEL pin state */
629dc144fe1SAtish Patra         0x00000297,                    /* 1:  auipc  t0, %pcrel_hi(fw_dyn) */
630623d53cbSBin Meng         0x02c28613,                    /*     addi   a2, t0, %pcrel_lo(1b) */
631a7240d1eSMichael Clark         0xf1402573,                    /*     csrr   a0, mhartid  */
6322206ffa6SAlistair Francis         0,
6332206ffa6SAlistair Francis         0,
634a7240d1eSMichael Clark         0x00028067,                    /*     jr     t0 */
635fc41ae23SAlistair Francis         start_addr,                    /* start: .dword */
6368590f536SAtish Patra         start_addr_hi32,
63766b1205bSAtish Patra         fdt_load_addr,                 /* fdt_laddr: .dword */
63866b1205bSAtish Patra         0x00000000,
639623d53cbSBin Meng         0x00000000,
640dc144fe1SAtish Patra                                        /* fw_dyn: */
641a7240d1eSMichael Clark     };
642a8259b53SAlistair Francis     if (riscv_is_32bit(&s->soc.u_cpus)) {
6432206ffa6SAlistair Francis         reset_vec[4] = 0x0202a583;     /*     lw     a1, 32(t0) */
6442206ffa6SAlistair Francis         reset_vec[5] = 0x0182a283;     /*     lw     t0, 24(t0) */
6452206ffa6SAlistair Francis     } else {
6462206ffa6SAlistair Francis         reset_vec[4] = 0x0202b583;     /*     ld     a1, 32(t0) */
6472206ffa6SAlistair Francis         reset_vec[5] = 0x0182b283;     /*     ld     t0, 24(t0) */
6482206ffa6SAlistair Francis     }
6492206ffa6SAlistair Francis 
650a7240d1eSMichael Clark 
6515aec3247SMichael Clark     /* copy in the reset vector in little_endian byte order */
65266b1205bSAtish Patra     for (i = 0; i < ARRAY_SIZE(reset_vec); i++) {
6535aec3247SMichael Clark         reset_vec[i] = cpu_to_le32(reset_vec[i]);
6545aec3247SMichael Clark     }
6555aec3247SMichael Clark     rom_add_blob_fixed_as("mrom.reset", reset_vec, sizeof(reset_vec),
65613b8c354SEduardo Habkost                           memmap[SIFIVE_U_DEV_MROM].base, &address_space_memory);
657dc144fe1SAtish Patra 
65878936771SAlistair Francis     riscv_rom_copy_firmware_info(machine, memmap[SIFIVE_U_DEV_MROM].base,
65913b8c354SEduardo Habkost                                  memmap[SIFIVE_U_DEV_MROM].size,
660dc144fe1SAtish Patra                                  sizeof(reset_vec), kernel_entry);
661145b2991SBin Meng 
662145b2991SBin Meng     /* Connect an SPI flash to SPI0 */
663145b2991SBin Meng     flash_dev = qdev_new("is25wp256");
66464eaa820SMarkus Armbruster     dinfo = drive_get(IF_MTD, 0, 0);
665145b2991SBin Meng     if (dinfo) {
666145b2991SBin Meng         qdev_prop_set_drive_err(flash_dev, "drive",
667145b2991SBin Meng                                 blk_by_legacy_dinfo(dinfo),
668145b2991SBin Meng                                 &error_fatal);
669145b2991SBin Meng     }
670145b2991SBin Meng     qdev_realize_and_unref(flash_dev, BUS(s->soc.spi0.spi), &error_fatal);
671145b2991SBin Meng 
672145b2991SBin Meng     flash_cs = qdev_get_gpio_in_named(flash_dev, SSI_GPIO_CS, 0);
673145b2991SBin Meng     sysbus_connect_irq(SYS_BUS_DEVICE(&s->soc.spi0), 1, flash_cs);
674722f1352SBin Meng 
675722f1352SBin Meng     /* Connect an SD card to SPI2 */
676722f1352SBin Meng     sd_dev = ssi_create_peripheral(s->soc.spi2.spi, "ssi-sd");
677722f1352SBin Meng 
678722f1352SBin Meng     sd_cs = qdev_get_gpio_in_named(sd_dev, SSI_GPIO_CS, 0);
679722f1352SBin Meng     sysbus_connect_irq(SYS_BUS_DEVICE(&s->soc.spi2), 1, sd_cs);
68036aa285fSMarkus Armbruster 
68136aa285fSMarkus Armbruster     dinfo = drive_get(IF_SD, 0, 0);
68236aa285fSMarkus Armbruster     blk = dinfo ? blk_by_legacy_dinfo(dinfo) : NULL;
68336aa285fSMarkus Armbruster     card_dev = qdev_new(TYPE_SD_CARD);
68436aa285fSMarkus Armbruster     qdev_prop_set_drive_err(card_dev, "drive", blk, &error_fatal);
68536aa285fSMarkus Armbruster     qdev_prop_set_bit(card_dev, "spi", true);
68636aa285fSMarkus Armbruster     qdev_realize_and_unref(card_dev,
68736aa285fSMarkus Armbruster                            qdev_get_child_bus(sd_dev, "sd-bus"),
68836aa285fSMarkus Armbruster                            &error_fatal);
6892308092bSAlistair Francis }
6902308092bSAlistair Francis 
691523e3464SAlistair Francis static bool sifive_u_machine_get_start_in_flash(Object *obj, Error **errp)
692523e3464SAlistair Francis {
693523e3464SAlistair Francis     SiFiveUState *s = RISCV_U_MACHINE(obj);
694523e3464SAlistair Francis 
695523e3464SAlistair Francis     return s->start_in_flash;
696523e3464SAlistair Francis }
697523e3464SAlistair Francis 
698523e3464SAlistair Francis static void sifive_u_machine_set_start_in_flash(Object *obj, bool value, Error **errp)
699523e3464SAlistair Francis {
700523e3464SAlistair Francis     SiFiveUState *s = RISCV_U_MACHINE(obj);
701523e3464SAlistair Francis 
702523e3464SAlistair Francis     s->start_in_flash = value;
703523e3464SAlistair Francis }
704523e3464SAlistair Francis 
705523e3464SAlistair Francis static void sifive_u_machine_instance_init(Object *obj)
706523e3464SAlistair Francis {
707523e3464SAlistair Francis     SiFiveUState *s = RISCV_U_MACHINE(obj);
708523e3464SAlistair Francis 
709523e3464SAlistair Francis     s->start_in_flash = false;
710cfa32630SBin Meng     s->msel = 0;
71196c7fff7SBernhard Beschow     object_property_add_uint32_ptr(obj, "msel", &s->msel,
71296c7fff7SBernhard Beschow                                    OBJ_PROP_FLAG_READWRITE);
713cfa32630SBin Meng     object_property_set_description(obj, "msel",
714cfa32630SBin Meng                                     "Mode Select (MSEL[3:0]) pin state");
715cfa32630SBin Meng 
7163ca109c3SBin Meng     s->serial = OTP_SERIAL;
71796c7fff7SBernhard Beschow     object_property_add_uint32_ptr(obj, "serial", &s->serial,
71896c7fff7SBernhard Beschow                                    OBJ_PROP_FLAG_READWRITE);
7197eecec7dSMarkus Armbruster     object_property_set_description(obj, "serial", "Board serial number");
720523e3464SAlistair Francis }
721523e3464SAlistair Francis 
722523e3464SAlistair Francis static void sifive_u_machine_class_init(ObjectClass *oc, void *data)
723523e3464SAlistair Francis {
724523e3464SAlistair Francis     MachineClass *mc = MACHINE_CLASS(oc);
725523e3464SAlistair Francis 
726523e3464SAlistair Francis     mc->desc = "RISC-V Board compatible with SiFive U SDK";
727523e3464SAlistair Francis     mc->init = sifive_u_machine_init;
728523e3464SAlistair Francis     mc->max_cpus = SIFIVE_U_MANAGEMENT_CPU_COUNT + SIFIVE_U_COMPUTE_CPU_COUNT;
729523e3464SAlistair Francis     mc->min_cpus = SIFIVE_U_MANAGEMENT_CPU_COUNT + 1;
7301eaada8aSBin Meng     mc->default_cpu_type = SIFIVE_U_CPU;
731523e3464SAlistair Francis     mc->default_cpus = mc->min_cpus;
732c188a9c4SBin Meng     mc->default_ram_id = "riscv.sifive.u.ram";
733418b473eSEduardo Habkost 
734418b473eSEduardo Habkost     object_class_property_add_bool(oc, "start-in-flash",
735418b473eSEduardo Habkost                                    sifive_u_machine_get_start_in_flash,
736418b473eSEduardo Habkost                                    sifive_u_machine_set_start_in_flash);
737418b473eSEduardo Habkost     object_class_property_set_description(oc, "start-in-flash",
738418b473eSEduardo Habkost                                           "Set on to tell QEMU's ROM to jump to "
739418b473eSEduardo Habkost                                           "flash. Otherwise QEMU will jump to DRAM "
740418b473eSEduardo Habkost                                           "or L2LIM depending on the msel value");
741523e3464SAlistair Francis }
742523e3464SAlistair Francis 
743523e3464SAlistair Francis static const TypeInfo sifive_u_machine_typeinfo = {
744523e3464SAlistair Francis     .name       = MACHINE_TYPE_NAME("sifive_u"),
745523e3464SAlistair Francis     .parent     = TYPE_MACHINE,
746523e3464SAlistair Francis     .class_init = sifive_u_machine_class_init,
747523e3464SAlistair Francis     .instance_init = sifive_u_machine_instance_init,
748523e3464SAlistair Francis     .instance_size = sizeof(SiFiveUState),
749523e3464SAlistair Francis };
750523e3464SAlistair Francis 
751523e3464SAlistair Francis static void sifive_u_machine_init_register_types(void)
752523e3464SAlistair Francis {
753523e3464SAlistair Francis     type_register_static(&sifive_u_machine_typeinfo);
754523e3464SAlistair Francis }
755523e3464SAlistair Francis 
756523e3464SAlistair Francis type_init(sifive_u_machine_init_register_types)
757523e3464SAlistair Francis 
758139177b1SBin Meng static void sifive_u_soc_instance_init(Object *obj)
7592308092bSAlistair Francis {
7602308092bSAlistair Francis     SiFiveUSoCState *s = RISCV_U_SOC(obj);
7612308092bSAlistair Francis 
7629fc7fc4dSMarkus Armbruster     object_initialize_child(obj, "e-cluster", &s->e_cluster, TYPE_CPU_CLUSTER);
763ecdfe393SBin Meng     qdev_prop_set_uint32(DEVICE(&s->e_cluster), "cluster-id", 0);
764ecdfe393SBin Meng 
765db873cc5SMarkus Armbruster     object_initialize_child(OBJECT(&s->e_cluster), "e-cpus", &s->e_cpus,
76675a6ed87SMarkus Armbruster                             TYPE_RISCV_HART_ARRAY);
767ecdfe393SBin Meng     qdev_prop_set_uint32(DEVICE(&s->e_cpus), "num-harts", 1);
768ecdfe393SBin Meng     qdev_prop_set_uint32(DEVICE(&s->e_cpus), "hartid-base", 0);
769ecdfe393SBin Meng     qdev_prop_set_string(DEVICE(&s->e_cpus), "cpu-type", SIFIVE_E_CPU);
77073f6ed97SBin Meng     qdev_prop_set_uint64(DEVICE(&s->e_cpus), "resetvec", 0x1004);
771ecdfe393SBin Meng 
7729fc7fc4dSMarkus Armbruster     object_initialize_child(obj, "u-cluster", &s->u_cluster, TYPE_CPU_CLUSTER);
773ecdfe393SBin Meng     qdev_prop_set_uint32(DEVICE(&s->u_cluster), "cluster-id", 1);
774ecdfe393SBin Meng 
775db873cc5SMarkus Armbruster     object_initialize_child(OBJECT(&s->u_cluster), "u-cpus", &s->u_cpus,
77675a6ed87SMarkus Armbruster                             TYPE_RISCV_HART_ARRAY);
7775a7f76a3SAlistair Francis 
778db873cc5SMarkus Armbruster     object_initialize_child(obj, "prci", &s->prci, TYPE_SIFIVE_U_PRCI);
779db873cc5SMarkus Armbruster     object_initialize_child(obj, "otp", &s->otp, TYPE_SIFIVE_U_OTP);
780db873cc5SMarkus Armbruster     object_initialize_child(obj, "gem", &s->gem, TYPE_CADENCE_GEM);
7818a88b9f5SBin Meng     object_initialize_child(obj, "gpio", &s->gpio, TYPE_SIFIVE_GPIO);
782834e027aSBin Meng     object_initialize_child(obj, "pdma", &s->dma, TYPE_SIFIVE_PDMA);
783145b2991SBin Meng     object_initialize_child(obj, "spi0", &s->spi0, TYPE_SIFIVE_SPI);
784722f1352SBin Meng     object_initialize_child(obj, "spi2", &s->spi2, TYPE_SIFIVE_SPI);
785ea6eaa06SAlistair Francis     object_initialize_child(obj, "pwm0", &s->pwm[0], TYPE_SIFIVE_PWM);
786ea6eaa06SAlistair Francis     object_initialize_child(obj, "pwm1", &s->pwm[1], TYPE_SIFIVE_PWM);
7872308092bSAlistair Francis }
7882308092bSAlistair Francis 
789139177b1SBin Meng static void sifive_u_soc_realize(DeviceState *dev, Error **errp)
7902308092bSAlistair Francis {
791c4473127SLike Xu     MachineState *ms = MACHINE(qdev_get_machine());
7922308092bSAlistair Francis     SiFiveUSoCState *s = RISCV_U_SOC(dev);
79373261285SBin Meng     const MemMapEntry *memmap = sifive_u_memmap;
7942308092bSAlistair Francis     MemoryRegion *system_memory = get_system_memory();
7952308092bSAlistair Francis     MemoryRegion *mask_rom = g_new(MemoryRegion, 1);
796a6902ef0SAlistair Francis     MemoryRegion *l2lim_mem = g_new(MemoryRegion, 1);
79705446f41SBin Meng     char *plic_hart_config;
798ea6eaa06SAlistair Francis     int i, j;
7995a7f76a3SAlistair Francis     NICInfo *nd = &nd_table[0];
8002308092bSAlistair Francis 
801099be035SAlistair Francis     qdev_prop_set_uint32(DEVICE(&s->u_cpus), "num-harts", ms->smp.cpus - 1);
802099be035SAlistair Francis     qdev_prop_set_uint32(DEVICE(&s->u_cpus), "hartid-base", 1);
803099be035SAlistair Francis     qdev_prop_set_string(DEVICE(&s->u_cpus), "cpu-type", s->cpu_type);
804099be035SAlistair Francis     qdev_prop_set_uint64(DEVICE(&s->u_cpus), "resetvec", 0x1004);
805099be035SAlistair Francis 
80691a3387dSTsukasa OI     sysbus_realize(SYS_BUS_DEVICE(&s->e_cpus), &error_fatal);
80791a3387dSTsukasa OI     sysbus_realize(SYS_BUS_DEVICE(&s->u_cpus), &error_fatal);
808ecdfe393SBin Meng     /*
809ecdfe393SBin Meng      * The cluster must be realized after the RISC-V hart array container,
810ecdfe393SBin Meng      * as the container's CPU object is only created on realize, and the
811ecdfe393SBin Meng      * CPU must exist and have been parented into the cluster before the
812ecdfe393SBin Meng      * cluster is realized.
813ecdfe393SBin Meng      */
814ce189ab2SMarkus Armbruster     qdev_realize(DEVICE(&s->e_cluster), NULL, &error_abort);
815ce189ab2SMarkus Armbruster     qdev_realize(DEVICE(&s->u_cluster), NULL, &error_abort);
8162308092bSAlistair Francis 
8172308092bSAlistair Francis     /* boot rom */
818414c47d2SPhilippe Mathieu-Daudé     memory_region_init_rom(mask_rom, OBJECT(dev), "riscv.sifive.u.mrom",
81913b8c354SEduardo Habkost                            memmap[SIFIVE_U_DEV_MROM].size, &error_fatal);
82013b8c354SEduardo Habkost     memory_region_add_subregion(system_memory, memmap[SIFIVE_U_DEV_MROM].base,
8212308092bSAlistair Francis                                 mask_rom);
822a7240d1eSMichael Clark 
823a6902ef0SAlistair Francis     /*
824a6902ef0SAlistair Francis      * Add L2-LIM at reset size.
825a6902ef0SAlistair Francis      * This should be reduced in size as the L2 Cache Controller WayEnable
826a6902ef0SAlistair Francis      * register is incremented. Unfortunately I don't see a nice (or any) way
827a6902ef0SAlistair Francis      * to handle reducing or blocking out the L2 LIM while still allowing it
828a6902ef0SAlistair Francis      * be re returned to all enabled after a reset. For the time being, just
829a6902ef0SAlistair Francis      * leave it enabled all the time. This won't break anything, but will be
830a6902ef0SAlistair Francis      * too generous to misbehaving guests.
831a6902ef0SAlistair Francis      */
832a6902ef0SAlistair Francis     memory_region_init_ram(l2lim_mem, NULL, "riscv.sifive.u.l2lim",
83313b8c354SEduardo Habkost                            memmap[SIFIVE_U_DEV_L2LIM].size, &error_fatal);
83413b8c354SEduardo Habkost     memory_region_add_subregion(system_memory, memmap[SIFIVE_U_DEV_L2LIM].base,
835a6902ef0SAlistair Francis                                 l2lim_mem);
836a6902ef0SAlistair Francis 
83705446f41SBin Meng     /* create PLIC hart topology configuration string */
8384e8fb53cSAlistair Francis     plic_hart_config = riscv_plic_hart_config_string(ms->smp.cpus);
83905446f41SBin Meng 
840a7240d1eSMichael Clark     /* MMIO */
84113b8c354SEduardo Habkost     s->plic = sifive_plic_create(memmap[SIFIVE_U_DEV_PLIC].base,
842f436ecc3SAlistair Francis         plic_hart_config, ms->smp.cpus, 0,
843a7240d1eSMichael Clark         SIFIVE_U_PLIC_NUM_SOURCES,
844a7240d1eSMichael Clark         SIFIVE_U_PLIC_NUM_PRIORITIES,
845a7240d1eSMichael Clark         SIFIVE_U_PLIC_PRIORITY_BASE,
846a7240d1eSMichael Clark         SIFIVE_U_PLIC_PENDING_BASE,
847a7240d1eSMichael Clark         SIFIVE_U_PLIC_ENABLE_BASE,
848a7240d1eSMichael Clark         SIFIVE_U_PLIC_ENABLE_STRIDE,
849a7240d1eSMichael Clark         SIFIVE_U_PLIC_CONTEXT_BASE,
850a7240d1eSMichael Clark         SIFIVE_U_PLIC_CONTEXT_STRIDE,
85113b8c354SEduardo Habkost         memmap[SIFIVE_U_DEV_PLIC].size);
852bb8136dfSPan Nengyuan     g_free(plic_hart_config);
85313b8c354SEduardo Habkost     sifive_uart_create(system_memory, memmap[SIFIVE_U_DEV_UART0].base,
854647a70a1SAlistair Francis         serial_hd(0), qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_U_UART0_IRQ));
85513b8c354SEduardo Habkost     sifive_uart_create(system_memory, memmap[SIFIVE_U_DEV_UART1].base,
856194eef09SMichael Clark         serial_hd(1), qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_U_UART1_IRQ));
857b8fb878aSAnup Patel     riscv_aclint_swi_create(memmap[SIFIVE_U_DEV_CLINT].base, 0,
858b8fb878aSAnup Patel         ms->smp.cpus, false);
859b8fb878aSAnup Patel     riscv_aclint_mtimer_create(memmap[SIFIVE_U_DEV_CLINT].base +
860b8fb878aSAnup Patel             RISCV_ACLINT_SWI_SIZE,
861b8fb878aSAnup Patel         RISCV_ACLINT_DEFAULT_MTIMER_SIZE, 0, ms->smp.cpus,
862b8fb878aSAnup Patel         RISCV_ACLINT_DEFAULT_MTIMECMP, RISCV_ACLINT_DEFAULT_MTIME,
863074ca702SBin Meng         CLINT_TIMEBASE_FREQ, false);
8645a7f76a3SAlistair Francis 
865cbe3a8c5SMarkus Armbruster     if (!sysbus_realize(SYS_BUS_DEVICE(&s->prci), errp)) {
866cbe3a8c5SMarkus Armbruster         return;
867cbe3a8c5SMarkus Armbruster     }
86813b8c354SEduardo Habkost     sysbus_mmio_map(SYS_BUS_DEVICE(&s->prci), 0, memmap[SIFIVE_U_DEV_PRCI].base);
869af14c840SBin Meng 
8708a88b9f5SBin Meng     qdev_prop_set_uint32(DEVICE(&s->gpio), "ngpio", 16);
871cbe3a8c5SMarkus Armbruster     if (!sysbus_realize(SYS_BUS_DEVICE(&s->gpio), errp)) {
872cbe3a8c5SMarkus Armbruster         return;
873cbe3a8c5SMarkus Armbruster     }
87413b8c354SEduardo Habkost     sysbus_mmio_map(SYS_BUS_DEVICE(&s->gpio), 0, memmap[SIFIVE_U_DEV_GPIO].base);
8758a88b9f5SBin Meng 
8768a88b9f5SBin Meng     /* Pass all GPIOs to the SOC layer so they are available to the board */
8778a88b9f5SBin Meng     qdev_pass_gpios(DEVICE(&s->gpio), dev, NULL);
8788a88b9f5SBin Meng 
8798a88b9f5SBin Meng     /* Connect GPIO interrupts to the PLIC */
8808a88b9f5SBin Meng     for (i = 0; i < 16; i++) {
8818a88b9f5SBin Meng         sysbus_connect_irq(SYS_BUS_DEVICE(&s->gpio), i,
8828a88b9f5SBin Meng                            qdev_get_gpio_in(DEVICE(s->plic),
8838a88b9f5SBin Meng                                             SIFIVE_U_GPIO_IRQ0 + i));
8848a88b9f5SBin Meng     }
8858a88b9f5SBin Meng 
886834e027aSBin Meng     /* PDMA */
887834e027aSBin Meng     sysbus_realize(SYS_BUS_DEVICE(&s->dma), errp);
88813b8c354SEduardo Habkost     sysbus_mmio_map(SYS_BUS_DEVICE(&s->dma), 0, memmap[SIFIVE_U_DEV_PDMA].base);
889834e027aSBin Meng 
890834e027aSBin Meng     /* Connect PDMA interrupts to the PLIC */
891834e027aSBin Meng     for (i = 0; i < SIFIVE_PDMA_IRQS; i++) {
892834e027aSBin Meng         sysbus_connect_irq(SYS_BUS_DEVICE(&s->dma), i,
893834e027aSBin Meng                            qdev_get_gpio_in(DEVICE(s->plic),
894834e027aSBin Meng                                             SIFIVE_U_PDMA_IRQ0 + i));
895834e027aSBin Meng     }
896834e027aSBin Meng 
897fda5b000SAlistair Francis     qdev_prop_set_uint32(DEVICE(&s->otp), "serial", s->serial);
898cbe3a8c5SMarkus Armbruster     if (!sysbus_realize(SYS_BUS_DEVICE(&s->otp), errp)) {
899cbe3a8c5SMarkus Armbruster         return;
900cbe3a8c5SMarkus Armbruster     }
90113b8c354SEduardo Habkost     sysbus_mmio_map(SYS_BUS_DEVICE(&s->otp), 0, memmap[SIFIVE_U_DEV_OTP].base);
9025461c4feSBin Meng 
9037ad36e2eSMarkus Armbruster     /* FIXME use qdev NIC properties instead of nd_table[] */
9045a7f76a3SAlistair Francis     if (nd->used) {
9055a7f76a3SAlistair Francis         qemu_check_nic_model(nd, TYPE_CADENCE_GEM);
9065a7f76a3SAlistair Francis         qdev_set_nic_properties(DEVICE(&s->gem), nd);
9075a7f76a3SAlistair Francis     }
9085325cc34SMarkus Armbruster     object_property_set_int(OBJECT(&s->gem), "revision", GEM_REVISION,
9095a7f76a3SAlistair Francis                             &error_abort);
910668f62ecSMarkus Armbruster     if (!sysbus_realize(SYS_BUS_DEVICE(&s->gem), errp)) {
9115a7f76a3SAlistair Francis         return;
9125a7f76a3SAlistair Francis     }
91313b8c354SEduardo Habkost     sysbus_mmio_map(SYS_BUS_DEVICE(&s->gem), 0, memmap[SIFIVE_U_DEV_GEM].base);
9145a7f76a3SAlistair Francis     sysbus_connect_irq(SYS_BUS_DEVICE(&s->gem), 0,
9155874f0a7SBin Meng                        qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_U_GEM_IRQ));
9167b6bb66fSBin Meng 
917ea6eaa06SAlistair Francis     /* PWM */
918ea6eaa06SAlistair Francis     for (i = 0; i < 2; i++) {
919ea6eaa06SAlistair Francis         if (!sysbus_realize(SYS_BUS_DEVICE(&s->pwm[i]), errp)) {
920ea6eaa06SAlistair Francis             return;
921ea6eaa06SAlistair Francis         }
922ea6eaa06SAlistair Francis         sysbus_mmio_map(SYS_BUS_DEVICE(&s->pwm[i]), 0,
923ea6eaa06SAlistair Francis                                 memmap[SIFIVE_U_DEV_PWM0].base + (0x1000 * i));
924ea6eaa06SAlistair Francis 
925ea6eaa06SAlistair Francis         /* Connect PWM interrupts to the PLIC */
926ea6eaa06SAlistair Francis         for (j = 0; j < SIFIVE_PWM_IRQS; j++) {
927ea6eaa06SAlistair Francis             sysbus_connect_irq(SYS_BUS_DEVICE(&s->pwm[i]), j,
928ea6eaa06SAlistair Francis                                qdev_get_gpio_in(DEVICE(s->plic),
929ea6eaa06SAlistair Francis                                         SIFIVE_U_PWM0_IRQ0 + (i * 4) + j));
930ea6eaa06SAlistair Francis         }
931ea6eaa06SAlistair Francis     }
932ea6eaa06SAlistair Francis 
9337b6bb66fSBin Meng     create_unimplemented_device("riscv.sifive.u.gem-mgmt",
93413b8c354SEduardo Habkost         memmap[SIFIVE_U_DEV_GEM_MGMT].base, memmap[SIFIVE_U_DEV_GEM_MGMT].size);
9353eaea6ebSBin Meng 
9363eaea6ebSBin Meng     create_unimplemented_device("riscv.sifive.u.dmc",
93713b8c354SEduardo Habkost         memmap[SIFIVE_U_DEV_DMC].base, memmap[SIFIVE_U_DEV_DMC].size);
9386eaf9cf5SBin Meng 
9396eaf9cf5SBin Meng     create_unimplemented_device("riscv.sifive.u.l2cc",
94013b8c354SEduardo Habkost         memmap[SIFIVE_U_DEV_L2CC].base, memmap[SIFIVE_U_DEV_L2CC].size);
941145b2991SBin Meng 
942145b2991SBin Meng     sysbus_realize(SYS_BUS_DEVICE(&s->spi0), errp);
943145b2991SBin Meng     sysbus_mmio_map(SYS_BUS_DEVICE(&s->spi0), 0,
944145b2991SBin Meng                     memmap[SIFIVE_U_DEV_QSPI0].base);
945145b2991SBin Meng     sysbus_connect_irq(SYS_BUS_DEVICE(&s->spi0), 0,
946145b2991SBin Meng                        qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_U_QSPI0_IRQ));
947722f1352SBin Meng     sysbus_realize(SYS_BUS_DEVICE(&s->spi2), errp);
948722f1352SBin Meng     sysbus_mmio_map(SYS_BUS_DEVICE(&s->spi2), 0,
949722f1352SBin Meng                     memmap[SIFIVE_U_DEV_QSPI2].base);
950722f1352SBin Meng     sysbus_connect_irq(SYS_BUS_DEVICE(&s->spi2), 0,
951722f1352SBin Meng                        qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_U_QSPI2_IRQ));
952a7240d1eSMichael Clark }
953a7240d1eSMichael Clark 
954139177b1SBin Meng static Property sifive_u_soc_props[] = {
955fda5b000SAlistair Francis     DEFINE_PROP_UINT32("serial", SiFiveUSoCState, serial, OTP_SERIAL),
956099be035SAlistair Francis     DEFINE_PROP_STRING("cpu-type", SiFiveUSoCState, cpu_type),
957fda5b000SAlistair Francis     DEFINE_PROP_END_OF_LIST()
958fda5b000SAlistair Francis };
959fda5b000SAlistair Francis 
960139177b1SBin Meng static void sifive_u_soc_class_init(ObjectClass *oc, void *data)
9612308092bSAlistair Francis {
9622308092bSAlistair Francis     DeviceClass *dc = DEVICE_CLASS(oc);
9632308092bSAlistair Francis 
964139177b1SBin Meng     device_class_set_props(dc, sifive_u_soc_props);
965139177b1SBin Meng     dc->realize = sifive_u_soc_realize;
9662308092bSAlistair Francis     /* Reason: Uses serial_hds in realize function, thus can't be used twice */
9672308092bSAlistair Francis     dc->user_creatable = false;
9682308092bSAlistair Francis }
9692308092bSAlistair Francis 
970139177b1SBin Meng static const TypeInfo sifive_u_soc_type_info = {
9712308092bSAlistair Francis     .name = TYPE_RISCV_U_SOC,
9722308092bSAlistair Francis     .parent = TYPE_DEVICE,
9732308092bSAlistair Francis     .instance_size = sizeof(SiFiveUSoCState),
974139177b1SBin Meng     .instance_init = sifive_u_soc_instance_init,
975139177b1SBin Meng     .class_init = sifive_u_soc_class_init,
9762308092bSAlistair Francis };
9772308092bSAlistair Francis 
978139177b1SBin Meng static void sifive_u_soc_register_types(void)
9792308092bSAlistair Francis {
980139177b1SBin Meng     type_register_static(&sifive_u_soc_type_info);
9812308092bSAlistair Francis }
9822308092bSAlistair Francis 
983139177b1SBin Meng type_init(sifive_u_soc_register_types)
984