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, 972206ffa6SAlistair Francis uint64_t mem_size, const char *cmdline, bool is_32_bit) 98a7240d1eSMichael Clark { 99ecdfe393SBin Meng MachineState *ms = MACHINE(qdev_get_machine()); 100a7240d1eSMichael Clark void *fdt; 101a7240d1eSMichael Clark 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 114f2ce39b4SPaolo Bonzini if (ms->dtb) { 115f2ce39b4SPaolo Bonzini fdt = s->fdt = load_device_tree(ms->dtb, &s->fdt_size); 116d5c90cf3SAnup Patel if (!fdt) { 117d5c90cf3SAnup Patel error_report("load_device_tree() failed"); 118d5c90cf3SAnup Patel exit(1); 119d5c90cf3SAnup Patel } 120d5c90cf3SAnup Patel goto update_bootargs; 121d5c90cf3SAnup Patel } else { 122a7240d1eSMichael Clark fdt = s->fdt = create_device_tree(&s->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); 29098ceee7fSAlistair Francis qemu_fdt_setprop_cell(fdt, nodename, "riscv,ndev", 0x35); 29104e7edd1SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "phandle", plic_phandle); 292a7240d1eSMichael Clark plic_phandle = qemu_fdt_get_phandle(fdt, nodename); 293a7240d1eSMichael Clark g_free(cells); 294a7240d1eSMichael Clark g_free(nodename); 295a7240d1eSMichael Clark 2965133ed17SBin Meng gpio_phandle = phandle++; 2978a88b9f5SBin Meng nodename = g_strdup_printf("/soc/gpio@%lx", 29813b8c354SEduardo Habkost (long)memmap[SIFIVE_U_DEV_GPIO].base); 2998a88b9f5SBin Meng qemu_fdt_add_subnode(fdt, nodename); 3005133ed17SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "phandle", gpio_phandle); 3018a88b9f5SBin Meng qemu_fdt_setprop_cells(fdt, nodename, "clocks", 3028a88b9f5SBin Meng prci_phandle, PRCI_CLK_TLCLK); 3038a88b9f5SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "#interrupt-cells", 2); 3048a88b9f5SBin Meng qemu_fdt_setprop(fdt, nodename, "interrupt-controller", NULL, 0); 3058a88b9f5SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "#gpio-cells", 2); 3068a88b9f5SBin Meng qemu_fdt_setprop(fdt, nodename, "gpio-controller", NULL, 0); 3078a88b9f5SBin Meng qemu_fdt_setprop_cells(fdt, nodename, "reg", 30813b8c354SEduardo Habkost 0x0, memmap[SIFIVE_U_DEV_GPIO].base, 30913b8c354SEduardo Habkost 0x0, memmap[SIFIVE_U_DEV_GPIO].size); 3108a88b9f5SBin Meng qemu_fdt_setprop_cells(fdt, nodename, "interrupts", SIFIVE_U_GPIO_IRQ0, 3118a88b9f5SBin Meng SIFIVE_U_GPIO_IRQ1, SIFIVE_U_GPIO_IRQ2, SIFIVE_U_GPIO_IRQ3, 3128a88b9f5SBin Meng SIFIVE_U_GPIO_IRQ4, SIFIVE_U_GPIO_IRQ5, SIFIVE_U_GPIO_IRQ6, 3138a88b9f5SBin Meng SIFIVE_U_GPIO_IRQ7, SIFIVE_U_GPIO_IRQ8, SIFIVE_U_GPIO_IRQ9, 3148a88b9f5SBin Meng SIFIVE_U_GPIO_IRQ10, SIFIVE_U_GPIO_IRQ11, SIFIVE_U_GPIO_IRQ12, 3158a88b9f5SBin Meng SIFIVE_U_GPIO_IRQ13, SIFIVE_U_GPIO_IRQ14, SIFIVE_U_GPIO_IRQ15); 3168a88b9f5SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle); 3178a88b9f5SBin Meng qemu_fdt_setprop_string(fdt, nodename, "compatible", "sifive,gpio0"); 3188a88b9f5SBin Meng g_free(nodename); 3198a88b9f5SBin Meng 3205133ed17SBin Meng nodename = g_strdup_printf("/gpio-restart"); 3215133ed17SBin Meng qemu_fdt_add_subnode(fdt, nodename); 3225133ed17SBin Meng qemu_fdt_setprop_cells(fdt, nodename, "gpios", gpio_phandle, 10, 1); 3235133ed17SBin Meng qemu_fdt_setprop_string(fdt, nodename, "compatible", "gpio-restart"); 3245133ed17SBin Meng g_free(nodename); 3255133ed17SBin Meng 326834e027aSBin Meng nodename = g_strdup_printf("/soc/dma@%lx", 32713b8c354SEduardo Habkost (long)memmap[SIFIVE_U_DEV_PDMA].base); 328834e027aSBin Meng qemu_fdt_add_subnode(fdt, nodename); 329834e027aSBin Meng qemu_fdt_setprop_cell(fdt, nodename, "#dma-cells", 1); 330834e027aSBin Meng qemu_fdt_setprop_cells(fdt, nodename, "interrupts", 331834e027aSBin Meng SIFIVE_U_PDMA_IRQ0, SIFIVE_U_PDMA_IRQ1, SIFIVE_U_PDMA_IRQ2, 332834e027aSBin Meng SIFIVE_U_PDMA_IRQ3, SIFIVE_U_PDMA_IRQ4, SIFIVE_U_PDMA_IRQ5, 333834e027aSBin Meng SIFIVE_U_PDMA_IRQ6, SIFIVE_U_PDMA_IRQ7); 334834e027aSBin Meng qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle); 335834e027aSBin Meng qemu_fdt_setprop_cells(fdt, nodename, "reg", 33613b8c354SEduardo Habkost 0x0, memmap[SIFIVE_U_DEV_PDMA].base, 33713b8c354SEduardo Habkost 0x0, memmap[SIFIVE_U_DEV_PDMA].size); 338834e027aSBin Meng qemu_fdt_setprop_string(fdt, nodename, "compatible", 339834e027aSBin Meng "sifive,fu540-c000-pdma"); 340834e027aSBin Meng g_free(nodename); 341834e027aSBin Meng 3426eaf9cf5SBin Meng nodename = g_strdup_printf("/soc/cache-controller@%lx", 34313b8c354SEduardo Habkost (long)memmap[SIFIVE_U_DEV_L2CC].base); 3446eaf9cf5SBin Meng qemu_fdt_add_subnode(fdt, nodename); 3456eaf9cf5SBin Meng qemu_fdt_setprop_cells(fdt, nodename, "reg", 34613b8c354SEduardo Habkost 0x0, memmap[SIFIVE_U_DEV_L2CC].base, 34713b8c354SEduardo Habkost 0x0, memmap[SIFIVE_U_DEV_L2CC].size); 3486eaf9cf5SBin Meng qemu_fdt_setprop_cells(fdt, nodename, "interrupts", 3496eaf9cf5SBin Meng SIFIVE_U_L2CC_IRQ0, SIFIVE_U_L2CC_IRQ1, SIFIVE_U_L2CC_IRQ2); 3506eaf9cf5SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle); 3516eaf9cf5SBin Meng qemu_fdt_setprop(fdt, nodename, "cache-unified", NULL, 0); 3526eaf9cf5SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "cache-size", 2097152); 3536eaf9cf5SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "cache-sets", 1024); 3546eaf9cf5SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "cache-level", 2); 3556eaf9cf5SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "cache-block-size", 64); 3566eaf9cf5SBin Meng qemu_fdt_setprop_string(fdt, nodename, "compatible", 3576eaf9cf5SBin Meng "sifive,fu540-c000-ccache"); 3586eaf9cf5SBin Meng g_free(nodename); 3596eaf9cf5SBin Meng 360145b2991SBin Meng nodename = g_strdup_printf("/soc/spi@%lx", 361722f1352SBin Meng (long)memmap[SIFIVE_U_DEV_QSPI2].base); 362722f1352SBin Meng qemu_fdt_add_subnode(fdt, nodename); 363722f1352SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "#size-cells", 0); 364722f1352SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "#address-cells", 1); 365722f1352SBin Meng qemu_fdt_setprop_cells(fdt, nodename, "clocks", 366722f1352SBin Meng prci_phandle, PRCI_CLK_TLCLK); 367722f1352SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "interrupts", SIFIVE_U_QSPI2_IRQ); 368722f1352SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle); 369722f1352SBin Meng qemu_fdt_setprop_cells(fdt, nodename, "reg", 370722f1352SBin Meng 0x0, memmap[SIFIVE_U_DEV_QSPI2].base, 371722f1352SBin Meng 0x0, memmap[SIFIVE_U_DEV_QSPI2].size); 372722f1352SBin Meng qemu_fdt_setprop_string(fdt, nodename, "compatible", "sifive,spi0"); 373722f1352SBin Meng g_free(nodename); 374722f1352SBin Meng 375722f1352SBin Meng nodename = g_strdup_printf("/soc/spi@%lx/mmc@0", 376722f1352SBin Meng (long)memmap[SIFIVE_U_DEV_QSPI2].base); 377722f1352SBin Meng qemu_fdt_add_subnode(fdt, nodename); 378722f1352SBin Meng qemu_fdt_setprop(fdt, nodename, "disable-wp", NULL, 0); 379722f1352SBin Meng qemu_fdt_setprop_cells(fdt, nodename, "voltage-ranges", 3300, 3300); 380722f1352SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "spi-max-frequency", 20000000); 381722f1352SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "reg", 0); 382722f1352SBin Meng qemu_fdt_setprop_string(fdt, nodename, "compatible", "mmc-spi-slot"); 383722f1352SBin Meng g_free(nodename); 384722f1352SBin Meng 385722f1352SBin Meng nodename = g_strdup_printf("/soc/spi@%lx", 386145b2991SBin Meng (long)memmap[SIFIVE_U_DEV_QSPI0].base); 387145b2991SBin Meng qemu_fdt_add_subnode(fdt, nodename); 388145b2991SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "#size-cells", 0); 389145b2991SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "#address-cells", 1); 390145b2991SBin Meng qemu_fdt_setprop_cells(fdt, nodename, "clocks", 391145b2991SBin Meng prci_phandle, PRCI_CLK_TLCLK); 392145b2991SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "interrupts", SIFIVE_U_QSPI0_IRQ); 393145b2991SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle); 394145b2991SBin Meng qemu_fdt_setprop_cells(fdt, nodename, "reg", 395145b2991SBin Meng 0x0, memmap[SIFIVE_U_DEV_QSPI0].base, 396145b2991SBin Meng 0x0, memmap[SIFIVE_U_DEV_QSPI0].size); 397145b2991SBin Meng qemu_fdt_setprop_string(fdt, nodename, "compatible", "sifive,spi0"); 398145b2991SBin Meng g_free(nodename); 399145b2991SBin Meng 400145b2991SBin Meng nodename = g_strdup_printf("/soc/spi@%lx/flash@0", 401145b2991SBin Meng (long)memmap[SIFIVE_U_DEV_QSPI0].base); 402145b2991SBin Meng qemu_fdt_add_subnode(fdt, nodename); 403145b2991SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "spi-rx-bus-width", 4); 404145b2991SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "spi-tx-bus-width", 4); 405145b2991SBin Meng qemu_fdt_setprop(fdt, nodename, "m25p,fast-read", NULL, 0); 406145b2991SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "spi-max-frequency", 50000000); 407145b2991SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "reg", 0); 408145b2991SBin Meng qemu_fdt_setprop_string(fdt, nodename, "compatible", "jedec,spi-nor"); 409145b2991SBin Meng g_free(nodename); 410145b2991SBin Meng 4117b6bb66fSBin Meng phy_phandle = phandle++; 4125a7f76a3SAlistair Francis nodename = g_strdup_printf("/soc/ethernet@%lx", 41313b8c354SEduardo Habkost (long)memmap[SIFIVE_U_DEV_GEM].base); 4145a7f76a3SAlistair Francis qemu_fdt_add_subnode(fdt, nodename); 4157b6bb66fSBin Meng qemu_fdt_setprop_string(fdt, nodename, "compatible", 4167b6bb66fSBin Meng "sifive,fu540-c000-gem"); 4175a7f76a3SAlistair Francis qemu_fdt_setprop_cells(fdt, nodename, "reg", 41813b8c354SEduardo Habkost 0x0, memmap[SIFIVE_U_DEV_GEM].base, 41913b8c354SEduardo Habkost 0x0, memmap[SIFIVE_U_DEV_GEM].size, 42013b8c354SEduardo Habkost 0x0, memmap[SIFIVE_U_DEV_GEM_MGMT].base, 42113b8c354SEduardo Habkost 0x0, memmap[SIFIVE_U_DEV_GEM_MGMT].size); 4225a7f76a3SAlistair Francis qemu_fdt_setprop_string(fdt, nodename, "reg-names", "control"); 4235a7f76a3SAlistair Francis qemu_fdt_setprop_string(fdt, nodename, "phy-mode", "gmii"); 4247b6bb66fSBin Meng qemu_fdt_setprop_cell(fdt, nodename, "phy-handle", phy_phandle); 42504e7edd1SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle); 42604e7edd1SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "interrupts", SIFIVE_U_GEM_IRQ); 427fe93582cSAnup Patel qemu_fdt_setprop_cells(fdt, nodename, "clocks", 428806c64b7SBin Meng prci_phandle, PRCI_CLK_GEMGXLPLL, prci_phandle, PRCI_CLK_GEMGXLPLL); 429cb53b283SBin Meng qemu_fdt_setprop_string_array(fdt, nodename, "clock-names", 430cb53b283SBin Meng (char **)ðclk_names, ARRAY_SIZE(ethclk_names)); 4317b6bb66fSBin Meng qemu_fdt_setprop(fdt, nodename, "local-mac-address", 4327b6bb66fSBin Meng s->soc.gem.conf.macaddr.a, ETH_ALEN); 43304e7edd1SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "#address-cells", 1); 43404e7edd1SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "#size-cells", 0); 435c3a28b5dSBin Meng 436c3a28b5dSBin Meng qemu_fdt_add_subnode(fdt, "/aliases"); 437c3a28b5dSBin Meng qemu_fdt_setprop_string(fdt, "/aliases", "ethernet0", nodename); 438c3a28b5dSBin Meng 4395a7f76a3SAlistair Francis g_free(nodename); 4405a7f76a3SAlistair Francis 4415a7f76a3SAlistair Francis nodename = g_strdup_printf("/soc/ethernet@%lx/ethernet-phy@0", 44213b8c354SEduardo Habkost (long)memmap[SIFIVE_U_DEV_GEM].base); 4435a7f76a3SAlistair Francis qemu_fdt_add_subnode(fdt, nodename); 4447b6bb66fSBin Meng qemu_fdt_setprop_cell(fdt, nodename, "phandle", phy_phandle); 44504e7edd1SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "reg", 0x0); 4465a7f76a3SAlistair Francis g_free(nodename); 4475a7f76a3SAlistair Francis 448ea6eaa06SAlistair Francis nodename = g_strdup_printf("/soc/pwm@%lx", 449ea6eaa06SAlistair Francis (long)memmap[SIFIVE_U_DEV_PWM0].base); 450ea6eaa06SAlistair Francis qemu_fdt_add_subnode(fdt, nodename); 451ea6eaa06SAlistair Francis qemu_fdt_setprop_string(fdt, nodename, "compatible", "sifive,pwm0"); 452ea6eaa06SAlistair Francis qemu_fdt_setprop_cells(fdt, nodename, "reg", 453ea6eaa06SAlistair Francis 0x0, memmap[SIFIVE_U_DEV_PWM0].base, 454ea6eaa06SAlistair Francis 0x0, memmap[SIFIVE_U_DEV_PWM0].size); 455ea6eaa06SAlistair Francis qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle); 456ea6eaa06SAlistair Francis qemu_fdt_setprop_cells(fdt, nodename, "interrupts", 457ea6eaa06SAlistair Francis SIFIVE_U_PWM0_IRQ0, SIFIVE_U_PWM0_IRQ1, 458ea6eaa06SAlistair Francis SIFIVE_U_PWM0_IRQ2, SIFIVE_U_PWM0_IRQ3); 459ea6eaa06SAlistair Francis qemu_fdt_setprop_cells(fdt, nodename, "clocks", 460ea6eaa06SAlistair Francis prci_phandle, PRCI_CLK_TLCLK); 461ea6eaa06SAlistair Francis qemu_fdt_setprop_cell(fdt, nodename, "#pwm-cells", 0); 462ea6eaa06SAlistair Francis g_free(nodename); 463ea6eaa06SAlistair Francis 464ea6eaa06SAlistair Francis nodename = g_strdup_printf("/soc/pwm@%lx", 465ea6eaa06SAlistair Francis (long)memmap[SIFIVE_U_DEV_PWM1].base); 466ea6eaa06SAlistair Francis qemu_fdt_add_subnode(fdt, nodename); 467ea6eaa06SAlistair Francis qemu_fdt_setprop_string(fdt, nodename, "compatible", "sifive,pwm0"); 468ea6eaa06SAlistair Francis qemu_fdt_setprop_cells(fdt, nodename, "reg", 469ea6eaa06SAlistair Francis 0x0, memmap[SIFIVE_U_DEV_PWM1].base, 470ea6eaa06SAlistair Francis 0x0, memmap[SIFIVE_U_DEV_PWM1].size); 471ea6eaa06SAlistair Francis qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle); 472ea6eaa06SAlistair Francis qemu_fdt_setprop_cells(fdt, nodename, "interrupts", 473ea6eaa06SAlistair Francis SIFIVE_U_PWM1_IRQ0, SIFIVE_U_PWM1_IRQ1, 474ea6eaa06SAlistair Francis SIFIVE_U_PWM1_IRQ2, SIFIVE_U_PWM1_IRQ3); 475ea6eaa06SAlistair Francis qemu_fdt_setprop_cells(fdt, nodename, "clocks", 476ea6eaa06SAlistair Francis prci_phandle, PRCI_CLK_TLCLK); 477ea6eaa06SAlistair Francis qemu_fdt_setprop_cell(fdt, nodename, "#pwm-cells", 0); 478ea6eaa06SAlistair Francis g_free(nodename); 479ea6eaa06SAlistair Francis 4805f7134d3SBin Meng nodename = g_strdup_printf("/soc/serial@%lx", 48110b43754SAnup Patel (long)memmap[SIFIVE_U_DEV_UART1].base); 48210b43754SAnup Patel qemu_fdt_add_subnode(fdt, nodename); 48310b43754SAnup Patel qemu_fdt_setprop_string(fdt, nodename, "compatible", "sifive,uart0"); 48410b43754SAnup Patel qemu_fdt_setprop_cells(fdt, nodename, "reg", 48510b43754SAnup Patel 0x0, memmap[SIFIVE_U_DEV_UART1].base, 48610b43754SAnup Patel 0x0, memmap[SIFIVE_U_DEV_UART1].size); 48710b43754SAnup Patel qemu_fdt_setprop_cells(fdt, nodename, "clocks", 48810b43754SAnup Patel prci_phandle, PRCI_CLK_TLCLK); 48910b43754SAnup Patel qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle); 49010b43754SAnup Patel qemu_fdt_setprop_cell(fdt, nodename, "interrupts", SIFIVE_U_UART1_IRQ); 49110b43754SAnup Patel 49210b43754SAnup Patel qemu_fdt_setprop_string(fdt, "/aliases", "serial1", nodename); 49310b43754SAnup Patel g_free(nodename); 49410b43754SAnup Patel 49510b43754SAnup Patel nodename = g_strdup_printf("/soc/serial@%lx", 49613b8c354SEduardo Habkost (long)memmap[SIFIVE_U_DEV_UART0].base); 497a7240d1eSMichael Clark qemu_fdt_add_subnode(fdt, nodename); 498a7240d1eSMichael Clark qemu_fdt_setprop_string(fdt, nodename, "compatible", "sifive,uart0"); 499a7240d1eSMichael Clark qemu_fdt_setprop_cells(fdt, nodename, "reg", 50013b8c354SEduardo Habkost 0x0, memmap[SIFIVE_U_DEV_UART0].base, 50113b8c354SEduardo Habkost 0x0, memmap[SIFIVE_U_DEV_UART0].size); 502806c64b7SBin Meng qemu_fdt_setprop_cells(fdt, nodename, "clocks", 503806c64b7SBin Meng prci_phandle, PRCI_CLK_TLCLK); 50404e7edd1SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle); 50504e7edd1SBin Meng qemu_fdt_setprop_cell(fdt, nodename, "interrupts", SIFIVE_U_UART0_IRQ); 506a7240d1eSMichael Clark 507a7240d1eSMichael Clark qemu_fdt_add_subnode(fdt, "/chosen"); 508a7240d1eSMichael Clark qemu_fdt_setprop_string(fdt, "/chosen", "stdout-path", nodename); 50944e6dcd3SGuenter Roeck qemu_fdt_setprop_string(fdt, "/aliases", "serial0", nodename); 51044e6dcd3SGuenter Roeck 511a7240d1eSMichael Clark g_free(nodename); 512d5c90cf3SAnup Patel 513d5c90cf3SAnup Patel update_bootargs: 51458303fc0SBin Meng if (cmdline && *cmdline) { 515d5c90cf3SAnup Patel qemu_fdt_setprop_string(fdt, "/chosen", "bootargs", cmdline); 516d5c90cf3SAnup Patel } 517a7240d1eSMichael Clark } 518a7240d1eSMichael Clark 5195133ed17SBin Meng static void sifive_u_machine_reset(void *opaque, int n, int level) 5205133ed17SBin Meng { 5215133ed17SBin Meng /* gpio pin active low triggers reset */ 5225133ed17SBin Meng if (!level) { 5235133ed17SBin Meng qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET); 5245133ed17SBin Meng } 5255133ed17SBin Meng } 5265133ed17SBin Meng 527523e3464SAlistair Francis static void sifive_u_machine_init(MachineState *machine) 528a7240d1eSMichael Clark { 52973261285SBin Meng const MemMapEntry *memmap = sifive_u_memmap; 530687caef1SAlistair Francis SiFiveUState *s = RISCV_U_MACHINE(machine); 5315aec3247SMichael Clark MemoryRegion *system_memory = get_system_memory(); 5321b3a2308SAlistair Francis MemoryRegion *flash0 = g_new(MemoryRegion, 1); 53313b8c354SEduardo Habkost target_ulong start_addr = memmap[SIFIVE_U_DEV_DRAM].base; 53438bc4e34SAlistair Francis target_ulong firmware_end_addr, kernel_start_addr; 5358590f536SAtish Patra uint32_t start_addr_hi32 = 0x00000000; 5365aec3247SMichael Clark int i; 53766b1205bSAtish Patra uint32_t fdt_load_addr; 538dc144fe1SAtish Patra uint64_t kernel_entry; 539145b2991SBin Meng DriveInfo *dinfo; 54036aa285fSMarkus Armbruster BlockBackend *blk; 54136aa285fSMarkus Armbruster DeviceState *flash_dev, *sd_dev, *card_dev; 542722f1352SBin Meng qemu_irq flash_cs, sd_cs; 543a7240d1eSMichael Clark 5442308092bSAlistair Francis /* Initialize SoC */ 5459fc7fc4dSMarkus Armbruster object_initialize_child(OBJECT(machine), "soc", &s->soc, TYPE_RISCV_U_SOC); 5465325cc34SMarkus Armbruster object_property_set_uint(OBJECT(&s->soc), "serial", s->serial, 5473ca109c3SBin Meng &error_abort); 548099be035SAlistair Francis object_property_set_str(OBJECT(&s->soc), "cpu-type", machine->cpu_type, 549099be035SAlistair Francis &error_abort); 5508f972e5bSAlistair Francis qdev_realize(DEVICE(&s->soc), NULL, &error_fatal); 551a7240d1eSMichael Clark 552a7240d1eSMichael Clark /* register RAM */ 55313b8c354SEduardo Habkost memory_region_add_subregion(system_memory, memmap[SIFIVE_U_DEV_DRAM].base, 554c188a9c4SBin Meng machine->ram); 555a7240d1eSMichael Clark 5561b3a2308SAlistair Francis /* register QSPI0 Flash */ 5571b3a2308SAlistair Francis memory_region_init_ram(flash0, NULL, "riscv.sifive.u.flash0", 55813b8c354SEduardo Habkost memmap[SIFIVE_U_DEV_FLASH0].size, &error_fatal); 55913b8c354SEduardo Habkost memory_region_add_subregion(system_memory, memmap[SIFIVE_U_DEV_FLASH0].base, 5601b3a2308SAlistair Francis flash0); 5611b3a2308SAlistair Francis 5625133ed17SBin Meng /* register gpio-restart */ 5635133ed17SBin Meng qdev_connect_gpio_out(DEVICE(&(s->soc.gpio)), 10, 5645133ed17SBin Meng qemu_allocate_irq(sifive_u_machine_reset, NULL, 0)); 5655133ed17SBin Meng 566a7240d1eSMichael Clark /* create device tree */ 5672206ffa6SAlistair Francis create_fdt(s, memmap, machine->ram_size, machine->kernel_cmdline, 568a8259b53SAlistair Francis riscv_is_32bit(&s->soc.u_cpus)); 569a7240d1eSMichael Clark 57017aad9f2SBin Meng if (s->start_in_flash) { 57117aad9f2SBin Meng /* 57217aad9f2SBin Meng * If start_in_flash property is given, assign s->msel to a value 57317aad9f2SBin Meng * that representing booting from QSPI0 memory-mapped flash. 57417aad9f2SBin Meng * 57517aad9f2SBin Meng * This also means that when both start_in_flash and msel properties 57617aad9f2SBin Meng * are given, start_in_flash takes the precedence over msel. 57717aad9f2SBin Meng * 57817aad9f2SBin Meng * Note this is to keep backward compatibility not to break existing 57917aad9f2SBin Meng * users that use start_in_flash property. 58017aad9f2SBin Meng */ 58117aad9f2SBin Meng s->msel = MSEL_MEMMAP_QSPI0_FLASH; 58217aad9f2SBin Meng } 58317aad9f2SBin Meng 58417aad9f2SBin Meng switch (s->msel) { 58517aad9f2SBin Meng case MSEL_MEMMAP_QSPI0_FLASH: 58613b8c354SEduardo Habkost start_addr = memmap[SIFIVE_U_DEV_FLASH0].base; 58717aad9f2SBin Meng break; 58817aad9f2SBin Meng case MSEL_L2LIM_QSPI0_FLASH: 58917aad9f2SBin Meng case MSEL_L2LIM_QSPI2_SD: 59013b8c354SEduardo Habkost start_addr = memmap[SIFIVE_U_DEV_L2LIM].base; 59117aad9f2SBin Meng break; 59217aad9f2SBin Meng default: 59313b8c354SEduardo Habkost start_addr = memmap[SIFIVE_U_DEV_DRAM].base; 59417aad9f2SBin Meng break; 59517aad9f2SBin Meng } 59617aad9f2SBin Meng 597a8259b53SAlistair Francis if (riscv_is_32bit(&s->soc.u_cpus)) { 5982206ffa6SAlistair Francis firmware_end_addr = riscv_find_and_load_firmware(machine, 599a0acd0a1SBin Meng RISCV32_BIOS_BIN, start_addr, NULL); 6002206ffa6SAlistair Francis } else { 6012206ffa6SAlistair Francis firmware_end_addr = riscv_find_and_load_firmware(machine, 602a0acd0a1SBin Meng RISCV64_BIOS_BIN, start_addr, NULL); 6032206ffa6SAlistair Francis } 604b3042223SAlistair Francis 605a7240d1eSMichael Clark if (machine->kernel_filename) { 606a8259b53SAlistair Francis kernel_start_addr = riscv_calc_kernel_start_addr(&s->soc.u_cpus, 60738bc4e34SAlistair Francis firmware_end_addr); 60838bc4e34SAlistair Francis 60938bc4e34SAlistair Francis kernel_entry = riscv_load_kernel(machine->kernel_filename, 61038bc4e34SAlistair Francis kernel_start_addr, NULL); 6110f8d4462SGuenter Roeck 6120f8d4462SGuenter Roeck if (machine->initrd_filename) { 6130f8d4462SGuenter Roeck hwaddr start; 6140f8d4462SGuenter Roeck hwaddr end = riscv_load_initrd(machine->initrd_filename, 6150f8d4462SGuenter Roeck machine->ram_size, kernel_entry, 6160f8d4462SGuenter Roeck &start); 6179f79638eSBin Meng qemu_fdt_setprop_cell(s->fdt, "/chosen", 6180f8d4462SGuenter Roeck "linux,initrd-start", start); 6199f79638eSBin Meng qemu_fdt_setprop_cell(s->fdt, "/chosen", "linux,initrd-end", 6200f8d4462SGuenter Roeck end); 6210f8d4462SGuenter Roeck } 622dc144fe1SAtish Patra } else { 623dc144fe1SAtish Patra /* 624dc144fe1SAtish Patra * If dynamic firmware is used, it doesn't know where is the next mode 625dc144fe1SAtish Patra * if kernel argument is not set. 626dc144fe1SAtish Patra */ 627dc144fe1SAtish Patra kernel_entry = 0; 628a7240d1eSMichael Clark } 629a7240d1eSMichael Clark 63066b1205bSAtish Patra /* Compute the fdt load address in dram */ 63113b8c354SEduardo Habkost fdt_load_addr = riscv_load_fdt(memmap[SIFIVE_U_DEV_DRAM].base, 63266b1205bSAtish Patra machine->ram_size, s->fdt); 633a8259b53SAlistair Francis if (!riscv_is_32bit(&s->soc.u_cpus)) { 6342206ffa6SAlistair Francis start_addr_hi32 = (uint64_t)start_addr >> 32; 6352206ffa6SAlistair Francis } 63666b1205bSAtish Patra 637a7240d1eSMichael Clark /* reset vector */ 638623d53cbSBin Meng uint32_t reset_vec[12] = { 63917aad9f2SBin Meng s->msel, /* MSEL pin state */ 640dc144fe1SAtish Patra 0x00000297, /* 1: auipc t0, %pcrel_hi(fw_dyn) */ 641623d53cbSBin Meng 0x02c28613, /* addi a2, t0, %pcrel_lo(1b) */ 642a7240d1eSMichael Clark 0xf1402573, /* csrr a0, mhartid */ 6432206ffa6SAlistair Francis 0, 6442206ffa6SAlistair Francis 0, 645a7240d1eSMichael Clark 0x00028067, /* jr t0 */ 646fc41ae23SAlistair Francis start_addr, /* start: .dword */ 6478590f536SAtish Patra start_addr_hi32, 64866b1205bSAtish Patra fdt_load_addr, /* fdt_laddr: .dword */ 64966b1205bSAtish Patra 0x00000000, 650623d53cbSBin Meng 0x00000000, 651dc144fe1SAtish Patra /* fw_dyn: */ 652a7240d1eSMichael Clark }; 653a8259b53SAlistair Francis if (riscv_is_32bit(&s->soc.u_cpus)) { 6542206ffa6SAlistair Francis reset_vec[4] = 0x0202a583; /* lw a1, 32(t0) */ 6552206ffa6SAlistair Francis reset_vec[5] = 0x0182a283; /* lw t0, 24(t0) */ 6562206ffa6SAlistair Francis } else { 6572206ffa6SAlistair Francis reset_vec[4] = 0x0202b583; /* ld a1, 32(t0) */ 6582206ffa6SAlistair Francis reset_vec[5] = 0x0182b283; /* ld t0, 24(t0) */ 6592206ffa6SAlistair Francis } 6602206ffa6SAlistair Francis 661a7240d1eSMichael Clark 6625aec3247SMichael Clark /* copy in the reset vector in little_endian byte order */ 66366b1205bSAtish Patra for (i = 0; i < ARRAY_SIZE(reset_vec); i++) { 6645aec3247SMichael Clark reset_vec[i] = cpu_to_le32(reset_vec[i]); 6655aec3247SMichael Clark } 6665aec3247SMichael Clark rom_add_blob_fixed_as("mrom.reset", reset_vec, sizeof(reset_vec), 66713b8c354SEduardo Habkost memmap[SIFIVE_U_DEV_MROM].base, &address_space_memory); 668dc144fe1SAtish Patra 66978936771SAlistair Francis riscv_rom_copy_firmware_info(machine, memmap[SIFIVE_U_DEV_MROM].base, 67013b8c354SEduardo Habkost memmap[SIFIVE_U_DEV_MROM].size, 671dc144fe1SAtish Patra sizeof(reset_vec), kernel_entry); 672145b2991SBin Meng 673145b2991SBin Meng /* Connect an SPI flash to SPI0 */ 674145b2991SBin Meng flash_dev = qdev_new("is25wp256"); 67564eaa820SMarkus Armbruster dinfo = drive_get(IF_MTD, 0, 0); 676145b2991SBin Meng if (dinfo) { 677145b2991SBin Meng qdev_prop_set_drive_err(flash_dev, "drive", 678145b2991SBin Meng blk_by_legacy_dinfo(dinfo), 679145b2991SBin Meng &error_fatal); 680145b2991SBin Meng } 681145b2991SBin Meng qdev_realize_and_unref(flash_dev, BUS(s->soc.spi0.spi), &error_fatal); 682145b2991SBin Meng 683145b2991SBin Meng flash_cs = qdev_get_gpio_in_named(flash_dev, SSI_GPIO_CS, 0); 684145b2991SBin Meng sysbus_connect_irq(SYS_BUS_DEVICE(&s->soc.spi0), 1, flash_cs); 685722f1352SBin Meng 686722f1352SBin Meng /* Connect an SD card to SPI2 */ 687722f1352SBin Meng sd_dev = ssi_create_peripheral(s->soc.spi2.spi, "ssi-sd"); 688722f1352SBin Meng 689722f1352SBin Meng sd_cs = qdev_get_gpio_in_named(sd_dev, SSI_GPIO_CS, 0); 690722f1352SBin Meng sysbus_connect_irq(SYS_BUS_DEVICE(&s->soc.spi2), 1, sd_cs); 69136aa285fSMarkus Armbruster 69236aa285fSMarkus Armbruster dinfo = drive_get(IF_SD, 0, 0); 69336aa285fSMarkus Armbruster blk = dinfo ? blk_by_legacy_dinfo(dinfo) : NULL; 69436aa285fSMarkus Armbruster card_dev = qdev_new(TYPE_SD_CARD); 69536aa285fSMarkus Armbruster qdev_prop_set_drive_err(card_dev, "drive", blk, &error_fatal); 69636aa285fSMarkus Armbruster qdev_prop_set_bit(card_dev, "spi", true); 69736aa285fSMarkus Armbruster qdev_realize_and_unref(card_dev, 69836aa285fSMarkus Armbruster qdev_get_child_bus(sd_dev, "sd-bus"), 69936aa285fSMarkus Armbruster &error_fatal); 7002308092bSAlistair Francis } 7012308092bSAlistair Francis 702523e3464SAlistair Francis static bool sifive_u_machine_get_start_in_flash(Object *obj, Error **errp) 703523e3464SAlistair Francis { 704523e3464SAlistair Francis SiFiveUState *s = RISCV_U_MACHINE(obj); 705523e3464SAlistair Francis 706523e3464SAlistair Francis return s->start_in_flash; 707523e3464SAlistair Francis } 708523e3464SAlistair Francis 709523e3464SAlistair Francis static void sifive_u_machine_set_start_in_flash(Object *obj, bool value, Error **errp) 710523e3464SAlistair Francis { 711523e3464SAlistair Francis SiFiveUState *s = RISCV_U_MACHINE(obj); 712523e3464SAlistair Francis 713523e3464SAlistair Francis s->start_in_flash = value; 714523e3464SAlistair Francis } 715523e3464SAlistair Francis 716523e3464SAlistair Francis static void sifive_u_machine_instance_init(Object *obj) 717523e3464SAlistair Francis { 718523e3464SAlistair Francis SiFiveUState *s = RISCV_U_MACHINE(obj); 719523e3464SAlistair Francis 720523e3464SAlistair Francis s->start_in_flash = false; 721cfa32630SBin Meng s->msel = 0; 722*96c7fff7SBernhard Beschow object_property_add_uint32_ptr(obj, "msel", &s->msel, 723*96c7fff7SBernhard Beschow OBJ_PROP_FLAG_READWRITE); 724cfa32630SBin Meng object_property_set_description(obj, "msel", 725cfa32630SBin Meng "Mode Select (MSEL[3:0]) pin state"); 726cfa32630SBin Meng 7273ca109c3SBin Meng s->serial = OTP_SERIAL; 728*96c7fff7SBernhard Beschow object_property_add_uint32_ptr(obj, "serial", &s->serial, 729*96c7fff7SBernhard Beschow OBJ_PROP_FLAG_READWRITE); 7307eecec7dSMarkus Armbruster object_property_set_description(obj, "serial", "Board serial number"); 731523e3464SAlistair Francis } 732523e3464SAlistair Francis 733523e3464SAlistair Francis static void sifive_u_machine_class_init(ObjectClass *oc, void *data) 734523e3464SAlistair Francis { 735523e3464SAlistair Francis MachineClass *mc = MACHINE_CLASS(oc); 736523e3464SAlistair Francis 737523e3464SAlistair Francis mc->desc = "RISC-V Board compatible with SiFive U SDK"; 738523e3464SAlistair Francis mc->init = sifive_u_machine_init; 739523e3464SAlistair Francis mc->max_cpus = SIFIVE_U_MANAGEMENT_CPU_COUNT + SIFIVE_U_COMPUTE_CPU_COUNT; 740523e3464SAlistair Francis mc->min_cpus = SIFIVE_U_MANAGEMENT_CPU_COUNT + 1; 7411eaada8aSBin Meng mc->default_cpu_type = SIFIVE_U_CPU; 742523e3464SAlistair Francis mc->default_cpus = mc->min_cpus; 743c188a9c4SBin Meng mc->default_ram_id = "riscv.sifive.u.ram"; 744418b473eSEduardo Habkost 745418b473eSEduardo Habkost object_class_property_add_bool(oc, "start-in-flash", 746418b473eSEduardo Habkost sifive_u_machine_get_start_in_flash, 747418b473eSEduardo Habkost sifive_u_machine_set_start_in_flash); 748418b473eSEduardo Habkost object_class_property_set_description(oc, "start-in-flash", 749418b473eSEduardo Habkost "Set on to tell QEMU's ROM to jump to " 750418b473eSEduardo Habkost "flash. Otherwise QEMU will jump to DRAM " 751418b473eSEduardo Habkost "or L2LIM depending on the msel value"); 752523e3464SAlistair Francis } 753523e3464SAlistair Francis 754523e3464SAlistair Francis static const TypeInfo sifive_u_machine_typeinfo = { 755523e3464SAlistair Francis .name = MACHINE_TYPE_NAME("sifive_u"), 756523e3464SAlistair Francis .parent = TYPE_MACHINE, 757523e3464SAlistair Francis .class_init = sifive_u_machine_class_init, 758523e3464SAlistair Francis .instance_init = sifive_u_machine_instance_init, 759523e3464SAlistair Francis .instance_size = sizeof(SiFiveUState), 760523e3464SAlistair Francis }; 761523e3464SAlistair Francis 762523e3464SAlistair Francis static void sifive_u_machine_init_register_types(void) 763523e3464SAlistair Francis { 764523e3464SAlistair Francis type_register_static(&sifive_u_machine_typeinfo); 765523e3464SAlistair Francis } 766523e3464SAlistair Francis 767523e3464SAlistair Francis type_init(sifive_u_machine_init_register_types) 768523e3464SAlistair Francis 769139177b1SBin Meng static void sifive_u_soc_instance_init(Object *obj) 7702308092bSAlistair Francis { 7712308092bSAlistair Francis SiFiveUSoCState *s = RISCV_U_SOC(obj); 7722308092bSAlistair Francis 7739fc7fc4dSMarkus Armbruster object_initialize_child(obj, "e-cluster", &s->e_cluster, TYPE_CPU_CLUSTER); 774ecdfe393SBin Meng qdev_prop_set_uint32(DEVICE(&s->e_cluster), "cluster-id", 0); 775ecdfe393SBin Meng 776db873cc5SMarkus Armbruster object_initialize_child(OBJECT(&s->e_cluster), "e-cpus", &s->e_cpus, 77775a6ed87SMarkus Armbruster TYPE_RISCV_HART_ARRAY); 778ecdfe393SBin Meng qdev_prop_set_uint32(DEVICE(&s->e_cpus), "num-harts", 1); 779ecdfe393SBin Meng qdev_prop_set_uint32(DEVICE(&s->e_cpus), "hartid-base", 0); 780ecdfe393SBin Meng qdev_prop_set_string(DEVICE(&s->e_cpus), "cpu-type", SIFIVE_E_CPU); 78173f6ed97SBin Meng qdev_prop_set_uint64(DEVICE(&s->e_cpus), "resetvec", 0x1004); 782ecdfe393SBin Meng 7839fc7fc4dSMarkus Armbruster object_initialize_child(obj, "u-cluster", &s->u_cluster, TYPE_CPU_CLUSTER); 784ecdfe393SBin Meng qdev_prop_set_uint32(DEVICE(&s->u_cluster), "cluster-id", 1); 785ecdfe393SBin Meng 786db873cc5SMarkus Armbruster object_initialize_child(OBJECT(&s->u_cluster), "u-cpus", &s->u_cpus, 78775a6ed87SMarkus Armbruster TYPE_RISCV_HART_ARRAY); 7885a7f76a3SAlistair Francis 789db873cc5SMarkus Armbruster object_initialize_child(obj, "prci", &s->prci, TYPE_SIFIVE_U_PRCI); 790db873cc5SMarkus Armbruster object_initialize_child(obj, "otp", &s->otp, TYPE_SIFIVE_U_OTP); 791db873cc5SMarkus Armbruster object_initialize_child(obj, "gem", &s->gem, TYPE_CADENCE_GEM); 7928a88b9f5SBin Meng object_initialize_child(obj, "gpio", &s->gpio, TYPE_SIFIVE_GPIO); 793834e027aSBin Meng object_initialize_child(obj, "pdma", &s->dma, TYPE_SIFIVE_PDMA); 794145b2991SBin Meng object_initialize_child(obj, "spi0", &s->spi0, TYPE_SIFIVE_SPI); 795722f1352SBin Meng object_initialize_child(obj, "spi2", &s->spi2, TYPE_SIFIVE_SPI); 796ea6eaa06SAlistair Francis object_initialize_child(obj, "pwm0", &s->pwm[0], TYPE_SIFIVE_PWM); 797ea6eaa06SAlistair Francis object_initialize_child(obj, "pwm1", &s->pwm[1], TYPE_SIFIVE_PWM); 7982308092bSAlistair Francis } 7992308092bSAlistair Francis 800139177b1SBin Meng static void sifive_u_soc_realize(DeviceState *dev, Error **errp) 8012308092bSAlistair Francis { 802c4473127SLike Xu MachineState *ms = MACHINE(qdev_get_machine()); 8032308092bSAlistair Francis SiFiveUSoCState *s = RISCV_U_SOC(dev); 80473261285SBin Meng const MemMapEntry *memmap = sifive_u_memmap; 8052308092bSAlistair Francis MemoryRegion *system_memory = get_system_memory(); 8062308092bSAlistair Francis MemoryRegion *mask_rom = g_new(MemoryRegion, 1); 807a6902ef0SAlistair Francis MemoryRegion *l2lim_mem = g_new(MemoryRegion, 1); 80805446f41SBin Meng char *plic_hart_config; 809ea6eaa06SAlistair Francis int i, j; 8105a7f76a3SAlistair Francis NICInfo *nd = &nd_table[0]; 8112308092bSAlistair Francis 812099be035SAlistair Francis qdev_prop_set_uint32(DEVICE(&s->u_cpus), "num-harts", ms->smp.cpus - 1); 813099be035SAlistair Francis qdev_prop_set_uint32(DEVICE(&s->u_cpus), "hartid-base", 1); 814099be035SAlistair Francis qdev_prop_set_string(DEVICE(&s->u_cpus), "cpu-type", s->cpu_type); 815099be035SAlistair Francis qdev_prop_set_uint64(DEVICE(&s->u_cpus), "resetvec", 0x1004); 816099be035SAlistair Francis 81791a3387dSTsukasa OI sysbus_realize(SYS_BUS_DEVICE(&s->e_cpus), &error_fatal); 81891a3387dSTsukasa OI sysbus_realize(SYS_BUS_DEVICE(&s->u_cpus), &error_fatal); 819ecdfe393SBin Meng /* 820ecdfe393SBin Meng * The cluster must be realized after the RISC-V hart array container, 821ecdfe393SBin Meng * as the container's CPU object is only created on realize, and the 822ecdfe393SBin Meng * CPU must exist and have been parented into the cluster before the 823ecdfe393SBin Meng * cluster is realized. 824ecdfe393SBin Meng */ 825ce189ab2SMarkus Armbruster qdev_realize(DEVICE(&s->e_cluster), NULL, &error_abort); 826ce189ab2SMarkus Armbruster qdev_realize(DEVICE(&s->u_cluster), NULL, &error_abort); 8272308092bSAlistair Francis 8282308092bSAlistair Francis /* boot rom */ 829414c47d2SPhilippe Mathieu-Daudé memory_region_init_rom(mask_rom, OBJECT(dev), "riscv.sifive.u.mrom", 83013b8c354SEduardo Habkost memmap[SIFIVE_U_DEV_MROM].size, &error_fatal); 83113b8c354SEduardo Habkost memory_region_add_subregion(system_memory, memmap[SIFIVE_U_DEV_MROM].base, 8322308092bSAlistair Francis mask_rom); 833a7240d1eSMichael Clark 834a6902ef0SAlistair Francis /* 835a6902ef0SAlistair Francis * Add L2-LIM at reset size. 836a6902ef0SAlistair Francis * This should be reduced in size as the L2 Cache Controller WayEnable 837a6902ef0SAlistair Francis * register is incremented. Unfortunately I don't see a nice (or any) way 838a6902ef0SAlistair Francis * to handle reducing or blocking out the L2 LIM while still allowing it 839a6902ef0SAlistair Francis * be re returned to all enabled after a reset. For the time being, just 840a6902ef0SAlistair Francis * leave it enabled all the time. This won't break anything, but will be 841a6902ef0SAlistair Francis * too generous to misbehaving guests. 842a6902ef0SAlistair Francis */ 843a6902ef0SAlistair Francis memory_region_init_ram(l2lim_mem, NULL, "riscv.sifive.u.l2lim", 84413b8c354SEduardo Habkost memmap[SIFIVE_U_DEV_L2LIM].size, &error_fatal); 84513b8c354SEduardo Habkost memory_region_add_subregion(system_memory, memmap[SIFIVE_U_DEV_L2LIM].base, 846a6902ef0SAlistair Francis l2lim_mem); 847a6902ef0SAlistair Francis 84805446f41SBin Meng /* create PLIC hart topology configuration string */ 8494e8fb53cSAlistair Francis plic_hart_config = riscv_plic_hart_config_string(ms->smp.cpus); 85005446f41SBin Meng 851a7240d1eSMichael Clark /* MMIO */ 85213b8c354SEduardo Habkost s->plic = sifive_plic_create(memmap[SIFIVE_U_DEV_PLIC].base, 853f436ecc3SAlistair Francis plic_hart_config, ms->smp.cpus, 0, 854a7240d1eSMichael Clark SIFIVE_U_PLIC_NUM_SOURCES, 855a7240d1eSMichael Clark SIFIVE_U_PLIC_NUM_PRIORITIES, 856a7240d1eSMichael Clark SIFIVE_U_PLIC_PRIORITY_BASE, 857a7240d1eSMichael Clark SIFIVE_U_PLIC_PENDING_BASE, 858a7240d1eSMichael Clark SIFIVE_U_PLIC_ENABLE_BASE, 859a7240d1eSMichael Clark SIFIVE_U_PLIC_ENABLE_STRIDE, 860a7240d1eSMichael Clark SIFIVE_U_PLIC_CONTEXT_BASE, 861a7240d1eSMichael Clark SIFIVE_U_PLIC_CONTEXT_STRIDE, 86213b8c354SEduardo Habkost memmap[SIFIVE_U_DEV_PLIC].size); 863bb8136dfSPan Nengyuan g_free(plic_hart_config); 86413b8c354SEduardo Habkost sifive_uart_create(system_memory, memmap[SIFIVE_U_DEV_UART0].base, 865647a70a1SAlistair Francis serial_hd(0), qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_U_UART0_IRQ)); 86613b8c354SEduardo Habkost sifive_uart_create(system_memory, memmap[SIFIVE_U_DEV_UART1].base, 867194eef09SMichael Clark serial_hd(1), qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_U_UART1_IRQ)); 868b8fb878aSAnup Patel riscv_aclint_swi_create(memmap[SIFIVE_U_DEV_CLINT].base, 0, 869b8fb878aSAnup Patel ms->smp.cpus, false); 870b8fb878aSAnup Patel riscv_aclint_mtimer_create(memmap[SIFIVE_U_DEV_CLINT].base + 871b8fb878aSAnup Patel RISCV_ACLINT_SWI_SIZE, 872b8fb878aSAnup Patel RISCV_ACLINT_DEFAULT_MTIMER_SIZE, 0, ms->smp.cpus, 873b8fb878aSAnup Patel RISCV_ACLINT_DEFAULT_MTIMECMP, RISCV_ACLINT_DEFAULT_MTIME, 874074ca702SBin Meng CLINT_TIMEBASE_FREQ, false); 8755a7f76a3SAlistair Francis 876cbe3a8c5SMarkus Armbruster if (!sysbus_realize(SYS_BUS_DEVICE(&s->prci), errp)) { 877cbe3a8c5SMarkus Armbruster return; 878cbe3a8c5SMarkus Armbruster } 87913b8c354SEduardo Habkost sysbus_mmio_map(SYS_BUS_DEVICE(&s->prci), 0, memmap[SIFIVE_U_DEV_PRCI].base); 880af14c840SBin Meng 8818a88b9f5SBin Meng qdev_prop_set_uint32(DEVICE(&s->gpio), "ngpio", 16); 882cbe3a8c5SMarkus Armbruster if (!sysbus_realize(SYS_BUS_DEVICE(&s->gpio), errp)) { 883cbe3a8c5SMarkus Armbruster return; 884cbe3a8c5SMarkus Armbruster } 88513b8c354SEduardo Habkost sysbus_mmio_map(SYS_BUS_DEVICE(&s->gpio), 0, memmap[SIFIVE_U_DEV_GPIO].base); 8868a88b9f5SBin Meng 8878a88b9f5SBin Meng /* Pass all GPIOs to the SOC layer so they are available to the board */ 8888a88b9f5SBin Meng qdev_pass_gpios(DEVICE(&s->gpio), dev, NULL); 8898a88b9f5SBin Meng 8908a88b9f5SBin Meng /* Connect GPIO interrupts to the PLIC */ 8918a88b9f5SBin Meng for (i = 0; i < 16; i++) { 8928a88b9f5SBin Meng sysbus_connect_irq(SYS_BUS_DEVICE(&s->gpio), i, 8938a88b9f5SBin Meng qdev_get_gpio_in(DEVICE(s->plic), 8948a88b9f5SBin Meng SIFIVE_U_GPIO_IRQ0 + i)); 8958a88b9f5SBin Meng } 8968a88b9f5SBin Meng 897834e027aSBin Meng /* PDMA */ 898834e027aSBin Meng sysbus_realize(SYS_BUS_DEVICE(&s->dma), errp); 89913b8c354SEduardo Habkost sysbus_mmio_map(SYS_BUS_DEVICE(&s->dma), 0, memmap[SIFIVE_U_DEV_PDMA].base); 900834e027aSBin Meng 901834e027aSBin Meng /* Connect PDMA interrupts to the PLIC */ 902834e027aSBin Meng for (i = 0; i < SIFIVE_PDMA_IRQS; i++) { 903834e027aSBin Meng sysbus_connect_irq(SYS_BUS_DEVICE(&s->dma), i, 904834e027aSBin Meng qdev_get_gpio_in(DEVICE(s->plic), 905834e027aSBin Meng SIFIVE_U_PDMA_IRQ0 + i)); 906834e027aSBin Meng } 907834e027aSBin Meng 908fda5b000SAlistair Francis qdev_prop_set_uint32(DEVICE(&s->otp), "serial", s->serial); 909cbe3a8c5SMarkus Armbruster if (!sysbus_realize(SYS_BUS_DEVICE(&s->otp), errp)) { 910cbe3a8c5SMarkus Armbruster return; 911cbe3a8c5SMarkus Armbruster } 91213b8c354SEduardo Habkost sysbus_mmio_map(SYS_BUS_DEVICE(&s->otp), 0, memmap[SIFIVE_U_DEV_OTP].base); 9135461c4feSBin Meng 9147ad36e2eSMarkus Armbruster /* FIXME use qdev NIC properties instead of nd_table[] */ 9155a7f76a3SAlistair Francis if (nd->used) { 9165a7f76a3SAlistair Francis qemu_check_nic_model(nd, TYPE_CADENCE_GEM); 9175a7f76a3SAlistair Francis qdev_set_nic_properties(DEVICE(&s->gem), nd); 9185a7f76a3SAlistair Francis } 9195325cc34SMarkus Armbruster object_property_set_int(OBJECT(&s->gem), "revision", GEM_REVISION, 9205a7f76a3SAlistair Francis &error_abort); 921668f62ecSMarkus Armbruster if (!sysbus_realize(SYS_BUS_DEVICE(&s->gem), errp)) { 9225a7f76a3SAlistair Francis return; 9235a7f76a3SAlistair Francis } 92413b8c354SEduardo Habkost sysbus_mmio_map(SYS_BUS_DEVICE(&s->gem), 0, memmap[SIFIVE_U_DEV_GEM].base); 9255a7f76a3SAlistair Francis sysbus_connect_irq(SYS_BUS_DEVICE(&s->gem), 0, 9265874f0a7SBin Meng qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_U_GEM_IRQ)); 9277b6bb66fSBin Meng 928ea6eaa06SAlistair Francis /* PWM */ 929ea6eaa06SAlistair Francis for (i = 0; i < 2; i++) { 930ea6eaa06SAlistair Francis if (!sysbus_realize(SYS_BUS_DEVICE(&s->pwm[i]), errp)) { 931ea6eaa06SAlistair Francis return; 932ea6eaa06SAlistair Francis } 933ea6eaa06SAlistair Francis sysbus_mmio_map(SYS_BUS_DEVICE(&s->pwm[i]), 0, 934ea6eaa06SAlistair Francis memmap[SIFIVE_U_DEV_PWM0].base + (0x1000 * i)); 935ea6eaa06SAlistair Francis 936ea6eaa06SAlistair Francis /* Connect PWM interrupts to the PLIC */ 937ea6eaa06SAlistair Francis for (j = 0; j < SIFIVE_PWM_IRQS; j++) { 938ea6eaa06SAlistair Francis sysbus_connect_irq(SYS_BUS_DEVICE(&s->pwm[i]), j, 939ea6eaa06SAlistair Francis qdev_get_gpio_in(DEVICE(s->plic), 940ea6eaa06SAlistair Francis SIFIVE_U_PWM0_IRQ0 + (i * 4) + j)); 941ea6eaa06SAlistair Francis } 942ea6eaa06SAlistair Francis } 943ea6eaa06SAlistair Francis 9447b6bb66fSBin Meng create_unimplemented_device("riscv.sifive.u.gem-mgmt", 94513b8c354SEduardo Habkost memmap[SIFIVE_U_DEV_GEM_MGMT].base, memmap[SIFIVE_U_DEV_GEM_MGMT].size); 9463eaea6ebSBin Meng 9473eaea6ebSBin Meng create_unimplemented_device("riscv.sifive.u.dmc", 94813b8c354SEduardo Habkost memmap[SIFIVE_U_DEV_DMC].base, memmap[SIFIVE_U_DEV_DMC].size); 9496eaf9cf5SBin Meng 9506eaf9cf5SBin Meng create_unimplemented_device("riscv.sifive.u.l2cc", 95113b8c354SEduardo Habkost memmap[SIFIVE_U_DEV_L2CC].base, memmap[SIFIVE_U_DEV_L2CC].size); 952145b2991SBin Meng 953145b2991SBin Meng sysbus_realize(SYS_BUS_DEVICE(&s->spi0), errp); 954145b2991SBin Meng sysbus_mmio_map(SYS_BUS_DEVICE(&s->spi0), 0, 955145b2991SBin Meng memmap[SIFIVE_U_DEV_QSPI0].base); 956145b2991SBin Meng sysbus_connect_irq(SYS_BUS_DEVICE(&s->spi0), 0, 957145b2991SBin Meng qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_U_QSPI0_IRQ)); 958722f1352SBin Meng sysbus_realize(SYS_BUS_DEVICE(&s->spi2), errp); 959722f1352SBin Meng sysbus_mmio_map(SYS_BUS_DEVICE(&s->spi2), 0, 960722f1352SBin Meng memmap[SIFIVE_U_DEV_QSPI2].base); 961722f1352SBin Meng sysbus_connect_irq(SYS_BUS_DEVICE(&s->spi2), 0, 962722f1352SBin Meng qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_U_QSPI2_IRQ)); 963a7240d1eSMichael Clark } 964a7240d1eSMichael Clark 965139177b1SBin Meng static Property sifive_u_soc_props[] = { 966fda5b000SAlistair Francis DEFINE_PROP_UINT32("serial", SiFiveUSoCState, serial, OTP_SERIAL), 967099be035SAlistair Francis DEFINE_PROP_STRING("cpu-type", SiFiveUSoCState, cpu_type), 968fda5b000SAlistair Francis DEFINE_PROP_END_OF_LIST() 969fda5b000SAlistair Francis }; 970fda5b000SAlistair Francis 971139177b1SBin Meng static void sifive_u_soc_class_init(ObjectClass *oc, void *data) 9722308092bSAlistair Francis { 9732308092bSAlistair Francis DeviceClass *dc = DEVICE_CLASS(oc); 9742308092bSAlistair Francis 975139177b1SBin Meng device_class_set_props(dc, sifive_u_soc_props); 976139177b1SBin Meng dc->realize = sifive_u_soc_realize; 9772308092bSAlistair Francis /* Reason: Uses serial_hds in realize function, thus can't be used twice */ 9782308092bSAlistair Francis dc->user_creatable = false; 9792308092bSAlistair Francis } 9802308092bSAlistair Francis 981139177b1SBin Meng static const TypeInfo sifive_u_soc_type_info = { 9822308092bSAlistair Francis .name = TYPE_RISCV_U_SOC, 9832308092bSAlistair Francis .parent = TYPE_DEVICE, 9842308092bSAlistair Francis .instance_size = sizeof(SiFiveUSoCState), 985139177b1SBin Meng .instance_init = sifive_u_soc_instance_init, 986139177b1SBin Meng .class_init = sifive_u_soc_class_init, 9872308092bSAlistair Francis }; 9882308092bSAlistair Francis 989139177b1SBin Meng static void sifive_u_soc_register_types(void) 9902308092bSAlistair Francis { 991139177b1SBin Meng type_register_static(&sifive_u_soc_type_info); 9922308092bSAlistair Francis } 9932308092bSAlistair Francis 994139177b1SBin Meng type_init(sifive_u_soc_register_types) 995